w32fns.c 236 KB
Newer Older
1
/* Graphical user interface functions for the Microsoft Windows API.
2

3
Copyright (C) 1989, 1992-2012  Free Software Foundation, Inc.
Geoff Voelker's avatar
Geoff Voelker committed
4 5 6

This file is part of GNU Emacs.

7
GNU Emacs is free software: you can redistribute it and/or modify
Geoff Voelker's avatar
Geoff Voelker committed
8
it under the terms of the GNU General Public License as published by
9 10
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Geoff Voelker's avatar
Geoff Voelker committed
11 12 13 14 15 16 17

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
18
along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
Geoff Voelker's avatar
Geoff Voelker committed
19 20 21 22

/* Added by Kevin Gallo */

#include <config.h>
Geoff Voelker's avatar
Geoff Voelker committed
23 24

#include <signal.h>
Geoff Voelker's avatar
Geoff Voelker committed
25
#include <stdio.h>
Geoff Voelker's avatar
Geoff Voelker committed
26 27
#include <limits.h>
#include <errno.h>
Jason Rumney's avatar
Jason Rumney committed
28
#include <math.h>
Geoff Voelker's avatar
Geoff Voelker committed
29 30 31 32 33

#include "lisp.h"
#include "w32term.h"
#include "frame.h"
#include "window.h"
34
#include "character.h"
Geoff Voelker's avatar
Geoff Voelker committed
35
#include "buffer.h"
36
#include "intervals.h"
37 38
#include "dispextern.h"
#include "keyboard.h"
Geoff Voelker's avatar
Geoff Voelker committed
39
#include "blockinput.h"
40
#include "epaths.h"
41
#include "charset.h"
42
#include "coding.h"
43
#include "ccl.h"
44
#include "fontset.h"
45
#include "systime.h"
46
#include "termhooks.h"
Daniel Colascione's avatar
Daniel Colascione committed
47

48
#include "w32heap.h"
Daniel Colascione's avatar
Daniel Colascione committed
49 50 51 52

#if CYGWIN
#include "cygw32.h"
#else
Eli Zaretskii's avatar
Eli Zaretskii committed
53
#include "w32.h"
Daniel Colascione's avatar
Daniel Colascione committed
54
#endif
55 56

#include "bitmaps/gray.xbm"
Geoff Voelker's avatar
Geoff Voelker committed
57

58
#include <commctrl.h>
Geoff Voelker's avatar
Geoff Voelker committed
59
#include <commdlg.h>
Richard M. Stallman's avatar
Richard M. Stallman committed
60
#include <shellapi.h>
61
#include <ctype.h>
Kenichi Handa's avatar
Kenichi Handa committed
62
#include <winspool.h>
63
#include <objbase.h>
Geoff Voelker's avatar
Geoff Voelker committed
64

65
#include <dlgs.h>
Jason Rumney's avatar
Jason Rumney committed
66
#include <imm.h>
67

68
#include "font.h"
69
#include "w32font.h"
70

71 72 73 74
#ifndef FOF_NO_CONNECTED_ELEMENTS
#define FOF_NO_CONNECTED_ELEMENTS 0x2000
#endif

75 76
void syms_of_w32fns (void);
void globals_of_w32fns (void);
77

78 79
extern void free_frame_menubar (struct frame *);
extern double atof (const char *);
80 81 82 83
extern int w32_console_toggle_lock_key (int, Lisp_Object);
extern void w32_menu_display_help (HWND, HMENU, UINT, UINT);
extern void w32_free_menu_strings (HWND);
extern const char *map_w32_filename (const char *, const char **);
Daniel Colascione's avatar
Daniel Colascione committed
84
extern char * w32_strerror (int error_no);
85

86
/* If non-NULL, a handle to a frame where to display the hourglass cursor.  */
87 88
static HWND hourglass_hwnd = NULL;

89 90 91 92
#ifndef IDC_HAND
#define IDC_HAND MAKEINTRESOURCE(32649)
#endif

Geoff Voelker's avatar
Geoff Voelker committed
93
/* Nonzero if using Windows.  */
94

Geoff Voelker's avatar
Geoff Voelker committed
95 96
static int w32_in_use;

Geoff Voelker's avatar
Geoff Voelker committed
97 98
Lisp_Object Qsuppress_icon;
Lisp_Object Qundefined_color;
99
Lisp_Object Qcancel_timer;
100
Lisp_Object Qfont_param;
101 102 103 104 105 106 107 108
Lisp_Object Qhyper;
Lisp_Object Qsuper;
Lisp_Object Qmeta;
Lisp_Object Qalt;
Lisp_Object Qctrl;
Lisp_Object Qcontrol;
Lisp_Object Qshift;

109

110 111 112 113
/* Prefix for system colors.  */
#define SYSTEM_COLOR_PREFIX "System"
#define SYSTEM_COLOR_PREFIX_LEN (sizeof (SYSTEM_COLOR_PREFIX) - 1)

114 115 116 117 118 119
/* State variables for emulating a three button mouse. */
#define LMOUSE 1
#define MMOUSE 2
#define RMOUSE 4

static int button_state = 0;
120
static W32Msg saved_mouse_button_msg;
121
static unsigned mouse_button_timer = 0;	/* non-zero when timer is active */
122
static W32Msg saved_mouse_move_msg;
123
static unsigned mouse_move_timer = 0;
124

125 126
/* Window that is tracking the mouse.  */
static HWND track_mouse_window;
127

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
/* Multi-monitor API definitions that are not pulled from the headers
   since we are compiling for NT 4.  */
#ifndef MONITOR_DEFAULT_TO_NEAREST
#define MONITOR_DEFAULT_TO_NEAREST 2
#endif
/* MinGW headers define MONITORINFO unconditionally, but MSVC ones don't.
   To avoid a compile error on one or the other, redefine with a new name.  */
struct MONITOR_INFO
{
    DWORD   cbSize;
    RECT    rcMonitor;
    RECT    rcWork;
    DWORD   dwFlags;
};

143
/* Reportedly, MSVC does not have this in its headers.  */
144
#if defined (_MSC_VER) && _WIN32_WINNT < 0x0500
145 146 147
DECLARE_HANDLE(HMONITOR);
#endif

148 149
typedef BOOL (WINAPI * TrackMouseEvent_Proc)
  (IN OUT LPTRACKMOUSEEVENT lpEventTrack);
Jason Rumney's avatar
Jason Rumney committed
150 151 152
typedef LONG (WINAPI * ImmGetCompositionString_Proc)
  (IN HIMC context, IN DWORD index, OUT LPVOID buffer, IN DWORD bufLen);
typedef HIMC (WINAPI * ImmGetContext_Proc) (IN HWND window);
153 154 155
typedef HWND (WINAPI * ImmReleaseContext_Proc) (IN HWND wnd, IN HIMC context);
typedef HWND (WINAPI * ImmSetCompositionWindow_Proc) (IN HIMC context,
						      IN COMPOSITIONFORM *form);
156 157 158
typedef HMONITOR (WINAPI * MonitorFromPoint_Proc) (IN POINT pt, IN DWORD flags);
typedef BOOL (WINAPI * GetMonitorInfo_Proc)
  (IN HMONITOR monitor, OUT struct MONITOR_INFO* info);
159

160
TrackMouseEvent_Proc track_mouse_event_fn = NULL;
Jason Rumney's avatar
Jason Rumney committed
161 162
ImmGetCompositionString_Proc get_composition_string_fn = NULL;
ImmGetContext_Proc get_ime_context_fn = NULL;
163 164
ImmReleaseContext_Proc release_ime_context_fn = NULL;
ImmSetCompositionWindow_Proc set_ime_composition_window_fn = NULL;
165 166
MonitorFromPoint_Proc monitor_from_point_fn = NULL;
GetMonitorInfo_Proc get_monitor_info_fn = NULL;
Jason Rumney's avatar
Jason Rumney committed
167

Daniel Colascione's avatar
Daniel Colascione committed
168 169 170
#ifdef NTGUI_UNICODE
#define unicode_append_menu AppendMenuW
#else /* !NTGUI_UNICODE */
171
extern AppendMenuW_Proc unicode_append_menu;
Daniel Colascione's avatar
Daniel Colascione committed
172
#endif /* NTGUI_UNICODE */
173

Jason Rumney's avatar
Jason Rumney committed
174 175 176
/* Flag to selectively ignore WM_IME_CHAR messages.  */
static int ignore_ime_char = 0;

177
/* W95 mousewheel handler */
Juanma Barranquero's avatar
Juanma Barranquero committed
178
unsigned int msh_mousewheel = 0;
179

180
/* Timers */
181 182
#define MOUSE_BUTTON_ID	1
#define MOUSE_MOVE_ID	2
183 184 185 186 187
#define MENU_FREE_ID 3
/* The delay (milliseconds) before a menu is freed after WM_EXITMENULOOP
   is received.  */
#define MENU_FREE_DELAY 1000
static unsigned menu_free_timer = 0;
188

189
#ifdef GLYPH_DEBUG
Paul Eggert's avatar
Paul Eggert committed
190
static int image_cache_refcount, dpyinfo_refcount;
191 192
#endif

193
static HWND w32_visible_system_caret_hwnd;
194

195 196
static int w32_unicode_gui;

197 198
/* From w32menu.c  */
extern HMENU current_popup_menu;
199
static int menubar_in_use = 0;
200

201
/* From w32uniscribe.c  */
202
extern void syms_of_w32uniscribe (void);
203 204
extern int uniscribe_available;

205
/* Function prototypes for hourglass support.  */
206 207
static void w32_show_hourglass (struct frame *);
static void w32_hide_hourglass (void);
208

Daniel Colascione's avatar
Daniel Colascione committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
#ifdef WINDOWSNT
/* From w32inevet.c */
extern int faked_key;
#endif /* WINDOWSNT */

/* This gives us the page size and the size of the allocation unit on NT.  */
SYSTEM_INFO sysinfo_cache;

/* This gives us version, build, and platform identification.  */
OSVERSIONINFO osinfo_cache;

unsigned long syspage_mask = 0;

/* The major and minor versions of NT.  */
int w32_major_version;
int w32_minor_version;
int w32_build_number;

/* Distinguish between Windows NT and Windows 95.  */
int os_subtype;

#ifdef HAVE_NTGUI
HINSTANCE hinst = NULL;
#endif

static unsigned int sound_type = 0xFFFFFFFF;
#define MB_EMACS_SILENT (0xFFFFFFFF - 1)
236

Geoff Voelker's avatar
Geoff Voelker committed
237

Geoff Voelker's avatar
Geoff Voelker committed
238 239
/* Error if we are not connected to MS-Windows.  */
void
240
check_w32 (void)
Geoff Voelker's avatar
Geoff Voelker committed
241 242 243 244 245 246 247
{
  if (! w32_in_use)
    error ("MS-Windows not in use or not initialized");
}

/* Nonzero if we can use mouse menus.
   You should not call this unless HAVE_MENUS is defined.  */
Juanma Barranquero's avatar
Juanma Barranquero committed
248

Geoff Voelker's avatar
Geoff Voelker committed
249
int
250
have_menus_p (void)
Geoff Voelker's avatar
Geoff Voelker committed
251 252 253 254
{
  return w32_in_use;
}

Geoff Voelker's avatar
Geoff Voelker committed
255
/* Extract a frame as a FRAME_PTR, defaulting to the selected frame
256
   and checking validity for W32.  */
Geoff Voelker's avatar
Geoff Voelker committed
257 258

FRAME_PTR
259
check_x_frame (Lisp_Object frame)
Geoff Voelker's avatar
Geoff Voelker committed
260 261 262 263
{
  FRAME_PTR f;

  if (NILP (frame))
264
    frame = selected_frame;
265
  CHECK_LIVE_FRAME (frame);
266
  f = XFRAME (frame);
267
  if (! FRAME_W32_P (f))
268
    error ("Non-W32 frame used");
Geoff Voelker's avatar
Geoff Voelker committed
269 270 271
  return f;
}

Juanma Barranquero's avatar
Juanma Barranquero committed
272
/* Let the user specify a display with a frame.
273
   nil stands for the selected frame--or, if that is not a w32 frame,
Geoff Voelker's avatar
Geoff Voelker committed
274 275
   the first display on the list.  */

276
struct w32_display_info *
277
check_x_display_info (Lisp_Object frame)
Geoff Voelker's avatar
Geoff Voelker committed
278 279 280
{
  if (NILP (frame))
    {
281
      struct frame *sf = XFRAME (selected_frame);
Juanma Barranquero's avatar
Juanma Barranquero committed
282

283 284
      if (FRAME_W32_P (sf) && FRAME_LIVE_P (sf))
	return FRAME_W32_DISPLAY_INFO (sf);
Geoff Voelker's avatar
Geoff Voelker committed
285
      else
286
	return &one_w32_display_info;
Geoff Voelker's avatar
Geoff Voelker committed
287 288 289 290 291 292 293
    }
  else if (STRINGP (frame))
    return x_display_info_for_name (frame);
  else
    {
      FRAME_PTR f;

294
      CHECK_LIVE_FRAME (frame);
Geoff Voelker's avatar
Geoff Voelker committed
295
      f = XFRAME (frame);
296
      if (! FRAME_W32_P (f))
297
	error ("Non-W32 frame used");
298
      return FRAME_W32_DISPLAY_INFO (f);
Geoff Voelker's avatar
Geoff Voelker committed
299 300 301
    }
}

302
/* Return the Emacs frame-object corresponding to an w32 window.
Geoff Voelker's avatar
Geoff Voelker committed
303 304 305 306 307
   It could be the frame's main window or an icon window.  */

/* This function can be called during GC, so use GC_xxx type test macros.  */

struct frame *
308
x_window_to_frame (struct w32_display_info *dpyinfo, HWND wdesc)
Geoff Voelker's avatar
Geoff Voelker committed
309 310 311 312
{
  Lisp_Object tail, frame;
  struct frame *f;

313
  for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
Geoff Voelker's avatar
Geoff Voelker committed
314
    {
315
      frame = XCAR (tail);
316
      if (!FRAMEP (frame))
Geoff Voelker's avatar
Geoff Voelker committed
317 318
        continue;
      f = XFRAME (frame);
319
      if (!FRAME_W32_P (f) || FRAME_W32_DISPLAY_INFO (f) != dpyinfo)
Geoff Voelker's avatar
Geoff Voelker committed
320
	continue;
321

322
      if (FRAME_W32_WINDOW (f) == wdesc)
Geoff Voelker's avatar
Geoff Voelker committed
323 324 325 326 327 328
        return f;
    }
  return 0;
}


329 330 331 332
static Lisp_Object unwind_create_frame (Lisp_Object);
static Lisp_Object unwind_create_tip_frame (Lisp_Object);
static void my_create_window (struct frame *);
static void my_create_tip_window (struct frame *);
333

334
/* TODO: Native Input Method support; see x_create_im.  */
335 336 337 338 339 340 341 342 343 344 345 346
void x_set_foreground_color (struct frame *, Lisp_Object, Lisp_Object);
void x_set_background_color (struct frame *, Lisp_Object, Lisp_Object);
void x_set_mouse_color (struct frame *, Lisp_Object, Lisp_Object);
void x_set_cursor_color (struct frame *, Lisp_Object, Lisp_Object);
void x_set_border_color (struct frame *, Lisp_Object, Lisp_Object);
void x_set_cursor_type (struct frame *, Lisp_Object, Lisp_Object);
void x_set_icon_type (struct frame *, Lisp_Object, Lisp_Object);
void x_set_icon_name (struct frame *, Lisp_Object, Lisp_Object);
void x_explicitly_set_name (struct frame *, Lisp_Object, Lisp_Object);
void x_set_menu_bar_lines (struct frame *, Lisp_Object, Lisp_Object);
void x_set_title (struct frame *, Lisp_Object, Lisp_Object);
void x_set_tool_bar_lines (struct frame *, Lisp_Object, Lisp_Object);
Geoff Voelker's avatar
Geoff Voelker committed
347 348 349 350 351 352 353 354 355




/* Store the screen positions of frame F into XPTR and YPTR.
   These are the positions of the containing window manager window,
   not Emacs's own window.  */

void
356
x_real_positions (FRAME_PTR f, int *xptr, int *yptr)
Geoff Voelker's avatar
Geoff Voelker committed
357 358
{
  POINT pt;
Jason Rumney's avatar
Jason Rumney committed
359
  RECT rect;
360

361 362
  /* Get the bounds of the WM window.  */
  GetWindowRect (FRAME_W32_WINDOW (f), &rect);
Jason Rumney's avatar
Jason Rumney committed
363

364 365
  pt.x = 0;
  pt.y = 0;
Geoff Voelker's avatar
Geoff Voelker committed
366

367 368
  /* Convert (0, 0) in the client area to screen co-ordinates.  */
  ClientToScreen (FRAME_W32_WINDOW (f), &pt);
Geoff Voelker's avatar
Geoff Voelker committed
369

Jason Rumney's avatar
Jason Rumney committed
370
  /* Remember x_pixels_diff and y_pixels_diff.  */
371 372
  f->x_pixels_diff = pt.x - rect.left;
  f->y_pixels_diff = pt.y - rect.top;
Jason Rumney's avatar
Jason Rumney committed
373

374 375
  *xptr = rect.left;
  *yptr = rect.top;
Geoff Voelker's avatar
Geoff Voelker committed
376 377 378 379
}



380 381
DEFUN ("w32-define-rgb-color", Fw32_define_rgb_color,
       Sw32_define_rgb_color, 4, 4, 0,
382
       doc: /* Convert RGB numbers to a Windows color reference and associate with NAME.
383
This adds or updates a named color to `w32-color-map', making it
384 385
available for use.  The original entry's RGB ref is returned, or nil
if the entry is new.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
386
  (Lisp_Object red, Lisp_Object green, Lisp_Object blue, Lisp_Object name)
Geoff Voelker's avatar
Geoff Voelker committed
387
{
388 389 390 391
  Lisp_Object rgb;
  Lisp_Object oldrgb = Qnil;
  Lisp_Object entry;

392 393 394 395
  CHECK_NUMBER (red);
  CHECK_NUMBER (green);
  CHECK_NUMBER (blue);
  CHECK_STRING (name);
Geoff Voelker's avatar
Geoff Voelker committed
396

397
  XSETINT (rgb, RGB (XUINT (red), XUINT (green), XUINT (blue)));
Geoff Voelker's avatar
Geoff Voelker committed
398

399
  block_input ();
Geoff Voelker's avatar
Geoff Voelker committed
400

401 402
  /* replace existing entry in w32-color-map or add new entry. */
  entry = Fassoc (name, Vw32_color_map);
403 404 405
  if (NILP (entry))
    {
      entry = Fcons (name, rgb);
406
      Vw32_color_map = Fcons (entry, Vw32_color_map);
407 408 409 410 411 412 413
    }
  else
    {
      oldrgb = Fcdr (entry);
      Fsetcdr (entry, rgb);
    }

414
  unblock_input ();
415 416

  return (oldrgb);
Geoff Voelker's avatar
Geoff Voelker committed
417 418
}

419
/* The default colors for the w32 color map */
Juanma Barranquero's avatar
Juanma Barranquero committed
420
typedef struct colormap_t
Geoff Voelker's avatar
Geoff Voelker committed
421 422 423 424 425
{
  char *name;
  COLORREF colorref;
} colormap_t;

Juanma Barranquero's avatar
Juanma Barranquero committed
426
colormap_t w32_color_map[] =
Geoff Voelker's avatar
Geoff Voelker committed
427
{
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
  {"snow"                      , PALETTERGB (255,250,250)},
  {"ghost white"               , PALETTERGB (248,248,255)},
  {"GhostWhite"                , PALETTERGB (248,248,255)},
  {"white smoke"               , PALETTERGB (245,245,245)},
  {"WhiteSmoke"                , PALETTERGB (245,245,245)},
  {"gainsboro"                 , PALETTERGB (220,220,220)},
  {"floral white"              , PALETTERGB (255,250,240)},
  {"FloralWhite"               , PALETTERGB (255,250,240)},
  {"old lace"                  , PALETTERGB (253,245,230)},
  {"OldLace"                   , PALETTERGB (253,245,230)},
  {"linen"                     , PALETTERGB (250,240,230)},
  {"antique white"             , PALETTERGB (250,235,215)},
  {"AntiqueWhite"              , PALETTERGB (250,235,215)},
  {"papaya whip"               , PALETTERGB (255,239,213)},
  {"PapayaWhip"                , PALETTERGB (255,239,213)},
  {"blanched almond"           , PALETTERGB (255,235,205)},
  {"BlanchedAlmond"            , PALETTERGB (255,235,205)},
  {"bisque"                    , PALETTERGB (255,228,196)},
  {"peach puff"                , PALETTERGB (255,218,185)},
  {"PeachPuff"                 , PALETTERGB (255,218,185)},
  {"navajo white"              , PALETTERGB (255,222,173)},
  {"NavajoWhite"               , PALETTERGB (255,222,173)},
  {"moccasin"                  , PALETTERGB (255,228,181)},
  {"cornsilk"                  , PALETTERGB (255,248,220)},
  {"ivory"                     , PALETTERGB (255,255,240)},
  {"lemon chiffon"             , PALETTERGB (255,250,205)},
  {"LemonChiffon"              , PALETTERGB (255,250,205)},
  {"seashell"                  , PALETTERGB (255,245,238)},
  {"honeydew"                  , PALETTERGB (240,255,240)},
  {"mint cream"                , PALETTERGB (245,255,250)},
  {"MintCream"                 , PALETTERGB (245,255,250)},
  {"azure"                     , PALETTERGB (240,255,255)},
  {"alice blue"                , PALETTERGB (240,248,255)},
  {"AliceBlue"                 , PALETTERGB (240,248,255)},
  {"lavender"                  , PALETTERGB (230,230,250)},
  {"lavender blush"            , PALETTERGB (255,240,245)},
  {"LavenderBlush"             , PALETTERGB (255,240,245)},
  {"misty rose"                , PALETTERGB (255,228,225)},
  {"MistyRose"                 , PALETTERGB (255,228,225)},
  {"white"                     , PALETTERGB (255,255,255)},
  {"black"                     , PALETTERGB (  0,  0,  0)},
  {"dark slate gray"           , PALETTERGB ( 47, 79, 79)},
  {"DarkSlateGray"             , PALETTERGB ( 47, 79, 79)},
  {"dark slate grey"           , PALETTERGB ( 47, 79, 79)},
  {"DarkSlateGrey"             , PALETTERGB ( 47, 79, 79)},
  {"dim gray"                  , PALETTERGB (105,105,105)},
  {"DimGray"                   , PALETTERGB (105,105,105)},
  {"dim grey"                  , PALETTERGB (105,105,105)},
  {"DimGrey"                   , PALETTERGB (105,105,105)},
  {"slate gray"                , PALETTERGB (112,128,144)},
  {"SlateGray"                 , PALETTERGB (112,128,144)},
  {"slate grey"                , PALETTERGB (112,128,144)},
  {"SlateGrey"                 , PALETTERGB (112,128,144)},
  {"light slate gray"          , PALETTERGB (119,136,153)},
  {"LightSlateGray"            , PALETTERGB (119,136,153)},
  {"light slate grey"          , PALETTERGB (119,136,153)},
  {"LightSlateGrey"            , PALETTERGB (119,136,153)},
  {"gray"                      , PALETTERGB (190,190,190)},
  {"grey"                      , PALETTERGB (190,190,190)},
  {"light grey"                , PALETTERGB (211,211,211)},
  {"LightGrey"                 , PALETTERGB (211,211,211)},
  {"light gray"                , PALETTERGB (211,211,211)},
  {"LightGray"                 , PALETTERGB (211,211,211)},
  {"midnight blue"             , PALETTERGB ( 25, 25,112)},
  {"MidnightBlue"              , PALETTERGB ( 25, 25,112)},
  {"navy"                      , PALETTERGB (  0,  0,128)},
  {"navy blue"                 , PALETTERGB (  0,  0,128)},
  {"NavyBlue"                  , PALETTERGB (  0,  0,128)},
  {"cornflower blue"           , PALETTERGB (100,149,237)},
  {"CornflowerBlue"            , PALETTERGB (100,149,237)},
  {"dark slate blue"           , PALETTERGB ( 72, 61,139)},
  {"DarkSlateBlue"             , PALETTERGB ( 72, 61,139)},
  {"slate blue"                , PALETTERGB (106, 90,205)},
  {"SlateBlue"                 , PALETTERGB (106, 90,205)},
  {"medium slate blue"         , PALETTERGB (123,104,238)},
  {"MediumSlateBlue"           , PALETTERGB (123,104,238)},
  {"light slate blue"          , PALETTERGB (132,112,255)},
  {"LightSlateBlue"            , PALETTERGB (132,112,255)},
  {"medium blue"               , PALETTERGB (  0,  0,205)},
  {"MediumBlue"                , PALETTERGB (  0,  0,205)},
  {"royal blue"                , PALETTERGB ( 65,105,225)},
  {"RoyalBlue"                 , PALETTERGB ( 65,105,225)},
  {"blue"                      , PALETTERGB (  0,  0,255)},
  {"dodger blue"               , PALETTERGB ( 30,144,255)},
  {"DodgerBlue"                , PALETTERGB ( 30,144,255)},
  {"deep sky blue"             , PALETTERGB (  0,191,255)},
  {"DeepSkyBlue"               , PALETTERGB (  0,191,255)},
  {"sky blue"                  , PALETTERGB (135,206,235)},
  {"SkyBlue"                   , PALETTERGB (135,206,235)},
  {"light sky blue"            , PALETTERGB (135,206,250)},
  {"LightSkyBlue"              , PALETTERGB (135,206,250)},
  {"steel blue"                , PALETTERGB ( 70,130,180)},
  {"SteelBlue"                 , PALETTERGB ( 70,130,180)},
  {"light steel blue"          , PALETTERGB (176,196,222)},
  {"LightSteelBlue"            , PALETTERGB (176,196,222)},
  {"light blue"                , PALETTERGB (173,216,230)},
  {"LightBlue"                 , PALETTERGB (173,216,230)},
  {"powder blue"               , PALETTERGB (176,224,230)},
  {"PowderBlue"                , PALETTERGB (176,224,230)},
  {"pale turquoise"            , PALETTERGB (175,238,238)},
  {"PaleTurquoise"             , PALETTERGB (175,238,238)},
  {"dark turquoise"            , PALETTERGB (  0,206,209)},
  {"DarkTurquoise"             , PALETTERGB (  0,206,209)},
  {"medium turquoise"          , PALETTERGB ( 72,209,204)},
  {"MediumTurquoise"           , PALETTERGB ( 72,209,204)},
  {"turquoise"                 , PALETTERGB ( 64,224,208)},
  {"cyan"                      , PALETTERGB (  0,255,255)},
  {"light cyan"                , PALETTERGB (224,255,255)},
  {"LightCyan"                 , PALETTERGB (224,255,255)},
  {"cadet blue"                , PALETTERGB ( 95,158,160)},
  {"CadetBlue"                 , PALETTERGB ( 95,158,160)},
  {"medium aquamarine"         , PALETTERGB (102,205,170)},
  {"MediumAquamarine"          , PALETTERGB (102,205,170)},
  {"aquamarine"                , PALETTERGB (127,255,212)},
  {"dark green"                , PALETTERGB (  0,100,  0)},
  {"DarkGreen"                 , PALETTERGB (  0,100,  0)},
  {"dark olive green"          , PALETTERGB ( 85,107, 47)},
  {"DarkOliveGreen"            , PALETTERGB ( 85,107, 47)},
  {"dark sea green"            , PALETTERGB (143,188,143)},
  {"DarkSeaGreen"              , PALETTERGB (143,188,143)},
  {"sea green"                 , PALETTERGB ( 46,139, 87)},
  {"SeaGreen"                  , PALETTERGB ( 46,139, 87)},
  {"medium sea green"          , PALETTERGB ( 60,179,113)},
  {"MediumSeaGreen"            , PALETTERGB ( 60,179,113)},
  {"light sea green"           , PALETTERGB ( 32,178,170)},
  {"LightSeaGreen"             , PALETTERGB ( 32,178,170)},
  {"pale green"                , PALETTERGB (152,251,152)},
  {"PaleGreen"                 , PALETTERGB (152,251,152)},
  {"spring green"              , PALETTERGB (  0,255,127)},
  {"SpringGreen"               , PALETTERGB (  0,255,127)},
  {"lawn green"                , PALETTERGB (124,252,  0)},
  {"LawnGreen"                 , PALETTERGB (124,252,  0)},
  {"green"                     , PALETTERGB (  0,255,  0)},
  {"chartreuse"                , PALETTERGB (127,255,  0)},
  {"medium spring green"       , PALETTERGB (  0,250,154)},
  {"MediumSpringGreen"         , PALETTERGB (  0,250,154)},
  {"green yellow"              , PALETTERGB (173,255, 47)},
  {"GreenYellow"               , PALETTERGB (173,255, 47)},
  {"lime green"                , PALETTERGB ( 50,205, 50)},
  {"LimeGreen"                 , PALETTERGB ( 50,205, 50)},
  {"yellow green"              , PALETTERGB (154,205, 50)},
  {"YellowGreen"               , PALETTERGB (154,205, 50)},
  {"forest green"              , PALETTERGB ( 34,139, 34)},
  {"ForestGreen"               , PALETTERGB ( 34,139, 34)},
  {"olive drab"                , PALETTERGB (107,142, 35)},
  {"OliveDrab"                 , PALETTERGB (107,142, 35)},
  {"dark khaki"                , PALETTERGB (189,183,107)},
  {"DarkKhaki"                 , PALETTERGB (189,183,107)},
  {"khaki"                     , PALETTERGB (240,230,140)},
  {"pale goldenrod"            , PALETTERGB (238,232,170)},
  {"PaleGoldenrod"             , PALETTERGB (238,232,170)},
  {"light goldenrod yellow"    , PALETTERGB (250,250,210)},
  {"LightGoldenrodYellow"      , PALETTERGB (250,250,210)},
  {"light yellow"              , PALETTERGB (255,255,224)},
  {"LightYellow"               , PALETTERGB (255,255,224)},
  {"yellow"                    , PALETTERGB (255,255,  0)},
  {"gold"                      , PALETTERGB (255,215,  0)},
  {"light goldenrod"           , PALETTERGB (238,221,130)},
  {"LightGoldenrod"            , PALETTERGB (238,221,130)},
  {"goldenrod"                 , PALETTERGB (218,165, 32)},
  {"dark goldenrod"            , PALETTERGB (184,134, 11)},
  {"DarkGoldenrod"             , PALETTERGB (184,134, 11)},
  {"rosy brown"                , PALETTERGB (188,143,143)},
  {"RosyBrown"                 , PALETTERGB (188,143,143)},
  {"indian red"                , PALETTERGB (205, 92, 92)},
  {"IndianRed"                 , PALETTERGB (205, 92, 92)},
  {"saddle brown"              , PALETTERGB (139, 69, 19)},
  {"SaddleBrown"               , PALETTERGB (139, 69, 19)},
  {"sienna"                    , PALETTERGB (160, 82, 45)},
  {"peru"                      , PALETTERGB (205,133, 63)},
  {"burlywood"                 , PALETTERGB (222,184,135)},
  {"beige"                     , PALETTERGB (245,245,220)},
  {"wheat"                     , PALETTERGB (245,222,179)},
  {"sandy brown"               , PALETTERGB (244,164, 96)},
  {"SandyBrown"                , PALETTERGB (244,164, 96)},
  {"tan"                       , PALETTERGB (210,180,140)},
  {"chocolate"                 , PALETTERGB (210,105, 30)},
  {"firebrick"                 , PALETTERGB (178,34, 34)},
  {"brown"                     , PALETTERGB (165,42, 42)},
  {"dark salmon"               , PALETTERGB (233,150,122)},
  {"DarkSalmon"                , PALETTERGB (233,150,122)},
  {"salmon"                    , PALETTERGB (250,128,114)},
  {"light salmon"              , PALETTERGB (255,160,122)},
  {"LightSalmon"               , PALETTERGB (255,160,122)},
  {"orange"                    , PALETTERGB (255,165,  0)},
  {"dark orange"               , PALETTERGB (255,140,  0)},
  {"DarkOrange"                , PALETTERGB (255,140,  0)},
  {"coral"                     , PALETTERGB (255,127, 80)},
  {"light coral"               , PALETTERGB (240,128,128)},
  {"LightCoral"                , PALETTERGB (240,128,128)},
  {"tomato"                    , PALETTERGB (255, 99, 71)},
  {"orange red"                , PALETTERGB (255, 69,  0)},
  {"OrangeRed"                 , PALETTERGB (255, 69,  0)},
  {"red"                       , PALETTERGB (255,  0,  0)},
  {"hot pink"                  , PALETTERGB (255,105,180)},
  {"HotPink"                   , PALETTERGB (255,105,180)},
  {"deep pink"                 , PALETTERGB (255, 20,147)},
  {"DeepPink"                  , PALETTERGB (255, 20,147)},
  {"pink"                      , PALETTERGB (255,192,203)},
  {"light pink"                , PALETTERGB (255,182,193)},
  {"LightPink"                 , PALETTERGB (255,182,193)},
  {"pale violet red"           , PALETTERGB (219,112,147)},
  {"PaleVioletRed"             , PALETTERGB (219,112,147)},
  {"maroon"                    , PALETTERGB (176, 48, 96)},
  {"medium violet red"         , PALETTERGB (199, 21,133)},
  {"MediumVioletRed"           , PALETTERGB (199, 21,133)},
  {"violet red"                , PALETTERGB (208, 32,144)},
  {"VioletRed"                 , PALETTERGB (208, 32,144)},
  {"magenta"                   , PALETTERGB (255,  0,255)},
  {"violet"                    , PALETTERGB (238,130,238)},
  {"plum"                      , PALETTERGB (221,160,221)},
  {"orchid"                    , PALETTERGB (218,112,214)},
  {"medium orchid"             , PALETTERGB (186, 85,211)},
  {"MediumOrchid"              , PALETTERGB (186, 85,211)},
  {"dark orchid"               , PALETTERGB (153, 50,204)},
  {"DarkOrchid"                , PALETTERGB (153, 50,204)},
  {"dark violet"               , PALETTERGB (148,  0,211)},
  {"DarkViolet"                , PALETTERGB (148,  0,211)},
  {"blue violet"               , PALETTERGB (138, 43,226)},
  {"BlueViolet"                , PALETTERGB (138, 43,226)},
  {"purple"                    , PALETTERGB (160, 32,240)},
  {"medium purple"             , PALETTERGB (147,112,219)},
  {"MediumPurple"              , PALETTERGB (147,112,219)},
  {"thistle"                   , PALETTERGB (216,191,216)},
  {"gray0"                     , PALETTERGB (  0,  0,  0)},
  {"grey0"                     , PALETTERGB (  0,  0,  0)},
  {"dark grey"                 , PALETTERGB (169,169,169)},
  {"DarkGrey"                  , PALETTERGB (169,169,169)},
  {"dark gray"                 , PALETTERGB (169,169,169)},
  {"DarkGray"                  , PALETTERGB (169,169,169)},
  {"dark blue"                 , PALETTERGB (  0,  0,139)},
  {"DarkBlue"                  , PALETTERGB (  0,  0,139)},
  {"dark cyan"                 , PALETTERGB (  0,139,139)},
  {"DarkCyan"                  , PALETTERGB (  0,139,139)},
  {"dark magenta"              , PALETTERGB (139,  0,139)},
  {"DarkMagenta"               , PALETTERGB (139,  0,139)},
  {"dark red"                  , PALETTERGB (139,  0,  0)},
  {"DarkRed"                   , PALETTERGB (139,  0,  0)},
  {"light green"               , PALETTERGB (144,238,144)},
  {"LightGreen"                , PALETTERGB (144,238,144)},
Geoff Voelker's avatar
Geoff Voelker committed
668 669
};

670 671
static Lisp_Object
w32_default_color_map (void)
Geoff Voelker's avatar
Geoff Voelker committed
672 673
{
  int i;
674
  colormap_t *pc = w32_color_map;
Geoff Voelker's avatar
Geoff Voelker committed
675
  Lisp_Object cmap;
Juanma Barranquero's avatar
Juanma Barranquero committed
676

677
  block_input ();
Juanma Barranquero's avatar
Juanma Barranquero committed
678

Geoff Voelker's avatar
Geoff Voelker committed
679
  cmap = Qnil;
Juanma Barranquero's avatar
Juanma Barranquero committed
680 681

  for (i = 0; i < sizeof (w32_color_map) / sizeof (w32_color_map[0]);
Geoff Voelker's avatar
Geoff Voelker committed
682 683 684 685
       pc++, i++)
    cmap = Fcons (Fcons (build_string (pc->name),
			 make_number (pc->colorref)),
		  cmap);
Juanma Barranquero's avatar
Juanma Barranquero committed
686

687
  unblock_input ();
Juanma Barranquero's avatar
Juanma Barranquero committed
688

Geoff Voelker's avatar
Geoff Voelker committed
689 690 691
  return (cmap);
}

692 693 694 695 696 697 698
DEFUN ("w32-default-color-map", Fw32_default_color_map, Sw32_default_color_map,
       0, 0, 0, doc: /* Return the default color map.  */)
  (void)
{
  return w32_default_color_map ();
}

699
static Lisp_Object
700
w32_color_map_lookup (const char *colorname)
701 702 703
{
  Lisp_Object tail, ret = Qnil;

704
  block_input ();
705

706
  for (tail = Vw32_color_map; CONSP (tail); tail = XCDR (tail))
707 708 709
    {
      register Lisp_Object elt, tem;

710
      elt = XCAR (tail);
711 712
      if (!CONSP (elt)) continue;

713
      tem = XCAR (elt);
714

715
      if (lstrcmpi (SDATA (tem), colorname) == 0)
716
	{
717
	  ret = Fcdr (elt);
718 719 720 721 722 723
	  break;
	}

      QUIT;
    }

724
  unblock_input ();
725 726 727 728

  return ret;
}

729 730

static void
731
add_system_logical_colors_to_map (Lisp_Object *system_colors)
732 733 734 735
{
  HKEY colors_key;

  /* Other registry operations are done with input blocked.  */
736
  block_input ();
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773

  /* Look for "Control Panel/Colors" under User and Machine registry
     settings.  */
  if (RegOpenKeyEx (HKEY_CURRENT_USER, "Control Panel\\Colors", 0,
		    KEY_READ, &colors_key) == ERROR_SUCCESS
      || RegOpenKeyEx (HKEY_LOCAL_MACHINE, "Control Panel\\Colors", 0,
		       KEY_READ, &colors_key) == ERROR_SUCCESS)
    {
      /* List all keys.  */
      char color_buffer[64];
      char full_name_buffer[MAX_PATH + SYSTEM_COLOR_PREFIX_LEN];
      int index = 0;
      DWORD name_size, color_size;
      char *name_buffer = full_name_buffer + SYSTEM_COLOR_PREFIX_LEN;

      name_size = sizeof (full_name_buffer) - SYSTEM_COLOR_PREFIX_LEN;
      color_size = sizeof (color_buffer);

      strcpy (full_name_buffer, SYSTEM_COLOR_PREFIX);

      while (RegEnumValueA (colors_key, index, name_buffer, &name_size,
			    NULL, NULL, color_buffer, &color_size)
	     == ERROR_SUCCESS)
	{
	  int r, g, b;
	  if (sscanf (color_buffer, " %u %u %u", &r, &g, &b) == 3)
	    *system_colors = Fcons (Fcons (build_string (full_name_buffer),
					   make_number (RGB (r, g, b))),
				    *system_colors);

	  name_size = sizeof (full_name_buffer) - SYSTEM_COLOR_PREFIX_LEN;
	  color_size = sizeof (color_buffer);
	  index++;
	}
      RegCloseKey (colors_key);
    }

774
  unblock_input ();
775 776 777
}


778
static Lisp_Object
779
x_to_w32_color (const char * colorname)
Geoff Voelker's avatar
Geoff Voelker committed
780
{
781 782
  register Lisp_Object ret = Qnil;

783
  block_input ();
Geoff Voelker's avatar
Geoff Voelker committed
784 785 786 787

  if (colorname[0] == '#')
    {
      /* Could be an old-style RGB Device specification.  */
788 789
      int size = strlen (colorname + 1);
      char *color = alloca (size + 1);
Juanma Barranquero's avatar
Juanma Barranquero committed
790

791
      strcpy (color, colorname + 1);
Geoff Voelker's avatar
Geoff Voelker committed
792 793 794 795 796 797 798
      if (size == 3 || size == 6 || size == 9 || size == 12)
	{
	  UINT colorval;
	  int i, pos;
	  pos = 0;
	  size /= 3;
	  colorval = 0;
Juanma Barranquero's avatar
Juanma Barranquero committed
799

Geoff Voelker's avatar
Geoff Voelker committed
800 801 802 803 804 805 806 807 808
	  for (i = 0; i < 3; i++)
	    {
	      char *end;
	      char t;
	      unsigned long value;

	      /* The check for 'x' in the following conditional takes into
		 account the fact that strtol allows a "0x" in front of
		 our numbers, and we don't.  */
809
	      if (!isxdigit (color[0]) || color[1] == 'x')
Geoff Voelker's avatar
Geoff Voelker committed
810 811 812
		break;
	      t = color[size];
	      color[size] = '\0';
813
	      value = strtoul (color, &end, 16);
Geoff Voelker's avatar
Geoff Voelker committed
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
	      color[size] = t;
	      if (errno == ERANGE || end - color != size)
		break;
	      switch (size)
		{
		case 1:
		  value = value * 0x10;
		  break;
		case 2:
		  break;
		case 3:
		  value /= 0x10;
		  break;
		case 4:
		  value /= 0x100;
		  break;
		}
	      colorval |= (value << pos);
	      pos += 0x8;
	      if (i == 2)
		{
835
		  unblock_input ();
836 837
		  XSETINT (ret, colorval);
		  return ret;
Geoff Voelker's avatar
Geoff Voelker committed
838 839 840 841 842
		}
	      color = end;
	    }
	}
    }
843
  else if (strnicmp (colorname, "rgb:", 4) == 0)
Geoff Voelker's avatar
Geoff Voelker committed
844
    {
845
      const char *color;
Geoff Voelker's avatar
Geoff Voelker committed
846 847 848 849 850 851 852 853 854 855
      UINT colorval;
      int i, pos;
      pos = 0;

      colorval = 0;
      color = colorname + 4;
      for (i = 0; i < 3; i++)
	{
	  char *end;
	  unsigned long value;
Juanma Barranquero's avatar
Juanma Barranquero committed
856

Geoff Voelker's avatar
Geoff Voelker committed
857 858 859
	  /* The check for 'x' in the following conditional takes into
	     account the fact that strtol allows a "0x" in front of
	     our numbers, and we don't.  */
860
	  if (!isxdigit (color[0]) || color[1] == 'x')
Geoff Voelker's avatar
Geoff Voelker committed
861
	    break;
862
	  value = strtoul (color, &end, 16);
Geoff Voelker's avatar
Geoff Voelker committed
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
	  if (errno == ERANGE)
	    break;
	  switch (end - color)
	    {
	    case 1:
	      value = value * 0x10 + value;
	      break;
	    case 2:
	      break;
	    case 3:
	      value /= 0x10;
	      break;
	    case 4:
	      value /= 0x100;
	      break;
	    default:
	      value = ULONG_MAX;
	    }
	  if (value == ULONG_MAX)
	    break;
	  colorval |= (value << pos);
	  pos += 0x8;
	  if (i == 2)
	    {
	      if (*end != '\0')
		break;
889
	      unblock_input ();
890 891
	      XSETINT (ret, colorval);
	      return ret;
Geoff Voelker's avatar
Geoff Voelker committed
892 893 894 895 896 897
	    }
	  if (*end != '/')
	    break;
	  color = end + 1;
	}
    }
898
  else if (strnicmp (colorname, "rgbi:", 5) == 0)
Geoff Voelker's avatar
Geoff Voelker committed
899 900
    {
      /* This is an RGB Intensity specification.  */
901
      const char *color;
Geoff Voelker's avatar
Geoff Voelker committed
902 903 904 905 906 907 908 909 910 911 912 913
      UINT colorval;
      int i, pos;
      pos = 0;

      colorval = 0;
      color = colorname + 5;
      for (i = 0; i < 3; i++)
	{
	  char *end;
	  double value;
	  UINT val;

914
	  value = strtod (color, &end);
Geoff Voelker's avatar
Geoff Voelker committed
915 916 917 918 919
	  if (errno == ERANGE)
	    break;
	  if (value < 0.0 || value > 1.0)
	    break;
	  val = (UINT)(0x100 * value);
Juanma Barranquero's avatar
Juanma Barranquero committed
920
	  /* We used 0x100 instead of 0xFF to give a continuous
Geoff Voelker's avatar
Geoff Voelker committed
921 922 923 924 925 926 927 928 929 930
             range between 0.0 and 1.0 inclusive.  The next statement
             fixes the 1.0 case.  */
	  if (val == 0x100)
	    val = 0xFF;
	  colorval |= (val << pos);
	  pos += 0x8;
	  if (i == 2)
	    {
	      if (*end != '\0')
		break;
931
	      unblock_input ();
932 933
	      XSETINT (ret, colorval);
	      return ret;
Geoff Voelker's avatar
Geoff Voelker committed
934 935 936 937 938 939 940 941 942
	    }
	  if (*end != '/')
	    break;
	  color = end + 1;
	}
    }
  /* I am not going to attempt to handle any of the CIE color schemes
     or TekHVC, since I don't know the algorithms for conversion to
     RGB.  */
943 944

  /* If we fail to lookup the color name in w32_color_map, then check the
Juanma Barranquero's avatar
Juanma Barranquero committed
945
     colorname to see if it can be crudely approximated: If the X color
946 947 948
     ends in a number (e.g., "darkseagreen2"), strip the number and
     return the result of looking up the base color name.  */
  ret = w32_color_map_lookup (colorname);
Juanma Barranquero's avatar
Juanma Barranquero committed
949
  if (NILP (ret))
Geoff Voelker's avatar
Geoff Voelker committed
950
    {
951
      int len = strlen (colorname);
Geoff Voelker's avatar
Geoff Voelker committed
952

Juanma Barranquero's avatar
Juanma Barranquero committed
953
      if (isdigit (colorname[len - 1]))
954
	{
955
	  char *ptr, *approx = alloca (len + 1);
Geoff Voelker's avatar
Geoff Voelker committed
956

957 958
	  strcpy (approx, colorname);
	  ptr = &approx[len - 1];
Juanma Barranquero's avatar
Juanma Barranquero committed
959
	  while (ptr > approx && isdigit (*ptr))
960
	      *ptr-- = '\0';
Geoff Voelker's avatar
Geoff Voelker committed
961

962
	  ret = w32_color_map_lookup (approx);
Geoff Voelker's avatar
Geoff Voelker committed
963 964
	}
    }
Juanma Barranquero's avatar
Juanma Barranquero committed
965

966
  unblock_input ();
Geoff Voelker's avatar
Geoff Voelker committed
967 968 969
  return ret;
}

970
void
971
w32_regenerate_palette (FRAME_PTR f)
972
{
973
  struct w32_palette_entry * list;
974 975 976 977 978
  LOGPALETTE *          log_palette;
  HPALETTE              new_palette;
  int                   i;

  /* don't bother trying to create palette if not supported */
979
  if (! FRAME_W32_DISPLAY_INFO (f)->has_palette)
980 981 982 983
    return;

  log_palette = (LOGPALETTE *)
    alloca (sizeof (LOGPALETTE) +
984
	     FRAME_W32_DISPLAY_INFO (f)->num_colors * sizeof (PALETTEENTRY));
985
  log_palette->palVersion = 0x300;
986
  log_palette->palNumEntries = FRAME_W32_DISPLAY_INFO (f)->num_colors;
987

988
  list = FRAME_W32_DISPLAY_INFO (f)->color_list;
989
  for (i = 0;
990
       i < FRAME_W32_DISPLAY_INFO (f)->num_colors;
991 992 993 994 995 996 997
       i++, list = list->next)
    log_palette->palPalEntry[i] = list->entry;

  new_palette = CreatePalette (log_palette);

  enter_crit ();

998 999 1000
  if (FRAME_W32_DISPLAY_INFO (f)->palette)
    DeleteObject (FRAME_W32_DISPLAY_INFO (f)->palette);
  FRAME_W32_DISPLAY_INFO (f)->palette = new_palette;
1001 1002 1003 1004 1005 1006 1007

  /* Realize display palette and garbage all frames. */
  release_frame_dc (f, get_frame_dc (f));

  leave_crit ();
}

1008 1009
#define W32_COLOR(pe)  RGB (pe.peRed, pe.peGreen, pe.peBlue)
#define SET_W32_COLOR(pe, color) \
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
  do \
    { \
      pe.peRed = GetRValue (color); \
      pe.peGreen = GetGValue (color); \
      pe.peBlue = GetBValue (color); \
      pe.peFlags = 0; \
    } while (0)

#if 0
/* Keep these around in case we ever want to track color usage. */
void
1021
w32_map_color (FRAME_PTR f, COLORREF color)
1022
{
1023
  struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
1024

1025
  if (NILP (Vw32_enable_palette))
1026 1027 1028 1029 1030
    return;

  /* check if color is already mapped */
  while (list)
    {
1031
      if (W32_COLOR (list->entry) == color)
1032 1033 1034 1035 1036 1037 1038 1039
        {
	  ++list->refcount;
	  return;
	}
      list = list->next;
    }

  /* not already mapped, so add to list and recreate Windows palette */
Dmitry Antipov's avatar
Dmitry Antipov committed
1040
  list = xmalloc (sizeof (struct w32_palette_entry));
1041
  SET_W32_COLOR (list->entry, color);
1042
  list->refcount = 1;
1043 1044 1045
  list->next = FRAME_W32_DISPLAY_INFO (f)->color_list;
  FRAME_W32_DISPLAY_INFO (f)->color_list = list;
  FRAME_W32_DISPLAY_INFO (f)->num_colors++;
1046 1047

  /* set flag that palette must be regenerated */
1048
  FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
1049 1050 1051
}

void
1052
w32_unmap_color (FRAME_PTR f, COLORREF color)
1053
{
1054 1055
  struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
  struct w32_palette_entry **prev = &FRAME_W32_DISPLAY_INFO (f)->color_list;
1056

1057
  if (NILP (Vw32_enable_palette))
1058 1059 1060 1061 1062
    return;

  /* check if color is already mapped */
  while (list)
    {
1063
      if (W32_COLOR (list->entry) == color)
1064 1065 1066 1067 1068
        {
	  if (--list->refcount == 0)
	    {
	      *prev = list->next;
	      xfree (list);
1069
	      FRAME_W32_DISPLAY_INFO (f)->num_colors--;
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
	      break;
	    }
	  else
	    return;
	}
      prev = &list->next;
      list = list->next;
    }

  /* set flag that palette must be regenerated */
1080
  FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
1081 1082 1083
}
#endif

1084 1085 1086 1087

/* Gamma-correct COLOR on frame F.  */

void
1088
gamma_correct (struct frame *f, COLORREF *color)
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
{
  if (f->gamma)
    {
      *color = PALETTERGB (
        pow (GetRValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
        pow (GetGValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
        pow (GetBValue (*color) / 255.0, f->gamma) * 255.0 + 0.5);
    }
}


Geoff Voelker's avatar
Geoff Voelker committed
1100 1101 1102 1103 1104
/* Decide if color named COLOR is valid for the display associated with
   the selected frame; if so, return the rgb values in COLOR_DEF.
   If ALLOC is nonzero, allocate a new colormap cell.  */

int
Daniel Colascione's avatar
Daniel Colascione committed
1105
w32_defined_color (FRAME_PTR f, const char *color, XColor *color_def, int alloc)
Geoff Voelker's avatar
Geoff Voelker committed
1106 1107
{
  register Lisp_Object tem;
1108
  COLORREF w32_color_ref;
1109

1110
  tem = x_to_w32_color (color);
1111

Juanma Barranquero's avatar
Juanma Barranquero committed
1112
  if (!NILP (tem))
Geoff Voelker's avatar
Geoff Voelker committed
1113
    {
1114 1115 1116 1117 1118 1119 1120
      if (f)
        {
          /* Apply gamma correction.  */
          w32_color_ref = XUINT (tem);
          gamma_correct (f, &w32_color_ref);
          XSETINT (tem, w32_color_ref);
        }
1121 1122

      /* Map this color to the palette if it is enabled. */
1123
      if (!NILP (Vw32_enable_palette))
1124
	{
1125
	  struct w32_palette_entry * entry =
1126
	    one_w32_display_info.color_list;
1127
	  struct w32_palette_entry ** prev =
1128
	    &one_w32_display_info.color_list;
Juanma Barranquero's avatar
Juanma Barranquero committed
1129

1130 1131 1132
	  /* check if color is already mapped */
	  while (entry)
	    {
1133
	      if (W32_COLOR (entry->entry) == XUINT (tem))
1134 1135 1136 1137 1138 1139 1140 1141
		break;
	      prev = &entry->next;
	      entry = entry->next;
	    }

	  if (entry == NULL && alloc)
	    {
	      /* not already mapped, so add to list */
Dmitry Antipov's avatar
Dmitry Antipov committed
1142
	      entry = xmalloc (sizeof (struct w32_palette_entry));
1143
	      SET_W32_COLOR (entry->entry, XUINT (tem));
1144 1145
	      entry->next = NULL;
	      *prev = entry;
1146
	      one_w32_display_info.num_colors++;
1147 1148

	      /* set flag that palette must be regenerated */
1149
	      one_w32_display_info.regen_palette = TRUE;
1150 1151 1152 1153 1154
	    }
	}
      /* Ensure COLORREF value is snapped to nearest color in (default)
	 palette by simulating the PALETTERGB macro.  This works whether
	 or not the display device has a palette. */
1155 1156 1157
      w32_color_ref = XUINT (tem) | 0x2000000;

      color_def->pixel = w32_color_ref;
1158 1159 1160
      color_def->red = GetRValue (w32_color_ref) * 256;
      color_def->green = GetGValue (w32_color_ref) * 256;
      color_def->blue = GetBValue (w32_color_ref) * 256;
1161

Geoff Voelker's avatar
Geoff Voelker committed
1162
      return 1;
1163
    }
Juanma Barranquero's avatar
Juanma Barranquero committed
1164
  else
1165 1166 1167
    {
      return 0;
    }
Geoff Voelker's avatar
Geoff Voelker committed
1168 1169 1170 1171 1172 1173 1174 1175
}

/* Given a string ARG naming a color, compute a pixel value from it
   suitable for screen F.
   If F is not a color screen, return DEF (default) regardless of what
   ARG says.  */

int
1176
x_decode_color (FRAME_PTR f, Lisp_Object arg, int def)
Geoff Voelker's avatar
Geoff Voelker committed
1177
{
1178
  XColor cdef;
Geoff Voelker's avatar
Geoff Voelker committed
1179

1180
  CHECK_STRING (arg);
Geoff Voelker's avatar
Geoff Voelker committed
1181

1182
  if (strcmp (SDATA (arg), "black") == 0)
Geoff Voelker's avatar
Geoff Voelker committed
1183
    return BLACK_PIX_DEFAULT (f);
1184
  else if (strcmp (SDATA (arg), "white") == 0)
Geoff Voelker's avatar
Geoff Voelker committed
1185 1186
    return WHITE_PIX_DEFAULT (f);

1187
  if ((FRAME_W32_DISPLAY_INFO (f)->n_planes * FRAME_W32_DISPLAY_INFO (f)->n_cbits) == 1)
Geoff Voelker's avatar
Geoff Voelker committed
1188 1189
    return def;

1190
  /* w32_defined_color is responsible for coping with failures
Geoff Voelker's avatar
Geoff Voelker committed
1191
     by looking for a near-miss.  */
1192
  if (w32_defined_color (f, SDATA (arg), &cdef, 1))
1193
    return cdef.pixel;
Geoff Voelker's avatar
Geoff Voelker committed
1194 1195 1196 1197 1198

  /* defined_color failed; return an ultimate default.  */
  return def;
}

1199 1200


Geoff Voelker's avatar
Geoff Voelker committed
1201 1202 1203
/* Functions called only from `x_set_frame_param'
   to set individual parameters.

1204
   If FRAME_W32_WINDOW (f) is 0,
Geoff Voelker's avatar
Geoff Voelker committed
1205 1206 1207 1208 1209
   the frame is being created and its window does not exist yet.
   In that case, just record the parameter's new value
   in the standard place; do not attempt to change the window.  */

void
1210
x_set_foreground_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
Geoff Voelker's avatar