* alloc.c: Do not define struct catchtag.
[bpt/emacs.git] / src / w32fns.c
CommitLineData
e9e23e23 1/* Graphical user interface functions for the Microsoft W32 API.
0b5538bd 2 Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
76b6f707 3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
8cabe764 4 Free Software Foundation, Inc.
ee78dc32
GV
5
6This file is part of GNU Emacs.
7
9ec0b715 8GNU Emacs is free software: you can redistribute it and/or modify
ee78dc32 9it under the terms of the GNU General Public License as published by
9ec0b715
GM
10the Free Software Foundation, either version 3 of the License, or
11(at your option) any later version.
ee78dc32
GV
12
13GNU Emacs is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
9ec0b715 19along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
ee78dc32
GV
20
21/* Added by Kevin Gallo */
22
ee78dc32 23#include <config.h>
1edf84e7
GV
24
25#include <signal.h>
ee78dc32 26#include <stdio.h>
1edf84e7
GV
27#include <limits.h>
28#include <errno.h>
b00afeae 29#include <math.h>
d7306fe6 30#include <setjmp.h>
ee78dc32
GV
31
32#include "lisp.h"
33#include "w32term.h"
34#include "frame.h"
35#include "window.h"
36#include "buffer.h"
6fc2811b 37#include "intervals.h"
10b4bc33
JR
38#include "dispextern.h"
39#include "keyboard.h"
ee78dc32 40#include "blockinput.h"
57bda87a 41#include "epaths.h"
10b4bc33
JR
42#include "character.h"
43#include "charset.h"
4587b026 44#include "coding.h"
3545439c 45#include "ccl.h"
10b4bc33 46#include "fontset.h"
6fc2811b 47#include "systime.h"
10b4bc33
JR
48#include "termhooks.h"
49#include "w32heap.h"
6fc2811b
JR
50
51#include "bitmaps/gray.xbm"
ee78dc32 52
60860eb3 53#include <commctrl.h>
ee78dc32 54#include <commdlg.h>
cb9e33d4 55#include <shellapi.h>
6fc2811b 56#include <ctype.h>
6b61353c 57#include <winspool.h>
d5781bb6 58#include <objbase.h>
ee78dc32 59
1030b26b 60#include <dlgs.h>
820eff5a 61#include <imm.h>
1030b26b
JR
62#define FILE_NAME_TEXT_FIELD edt1
63
a1fe5c00 64#include "font.h"
1cc06b86 65#include "w32font.h"
a1fe5c00 66
6cf29fe8
JR
67#ifndef FOF_NO_CONNECTED_ELEMENTS
68#define FOF_NO_CONNECTED_ELEMENTS 0x2000
69#endif
70
9785d95b
BK
71void syms_of_w32fns ();
72void globals_of_w32fns ();
73
ee78dc32 74extern void free_frame_menubar ();
6fc2811b 75extern double atof ();
9eb16b62
JR
76extern int w32_console_toggle_lock_key P_ ((int, Lisp_Object));
77extern void w32_menu_display_help P_ ((HWND, HMENU, UINT, UINT));
78extern void w32_free_menu_strings P_ ((HWND));
dfe66c5b 79extern const char *map_w32_filename P_ ((const char *, const char **));
9eb16b62 80
5ac45f98 81extern int quit_char;
ee78dc32 82
ccc2d29c
GV
83extern char *lispy_function_keys[];
84
ee78dc32 85/* The colormap for converting color names to RGB values */
fbd6baed 86Lisp_Object Vw32_color_map;
ee78dc32 87
da36a4d6 88/* Non nil if alt key presses are passed on to Windows. */
fbd6baed 89Lisp_Object Vw32_pass_alt_to_system;
da36a4d6 90
8c205c63
RS
91/* Non nil if alt key is translated to meta_modifier, nil if it is translated
92 to alt_modifier. */
fbd6baed 93Lisp_Object Vw32_alt_is_meta;
8c205c63 94
7d081355 95/* If non-zero, the windows virtual key code for an alternative quit key. */
2ba49441 96int w32_quit_key;
7d081355 97
ccc2d29c
GV
98/* Non nil if left window key events are passed on to Windows (this only
99 affects whether "tapping" the key opens the Start menu). */
100Lisp_Object Vw32_pass_lwindow_to_system;
101
102/* Non nil if right window key events are passed on to Windows (this
103 only affects whether "tapping" the key opens the Start menu). */
104Lisp_Object Vw32_pass_rwindow_to_system;
105
adcc3809
GV
106/* Virtual key code used to generate "phantom" key presses in order
107 to stop system from acting on Windows key events. */
108Lisp_Object Vw32_phantom_key_code;
109
ccc2d29c
GV
110/* Modifier associated with the left "Windows" key, or nil to act as a
111 normal key. */
112Lisp_Object Vw32_lwindow_modifier;
113
114/* Modifier associated with the right "Windows" key, or nil to act as a
115 normal key. */
116Lisp_Object Vw32_rwindow_modifier;
117
118/* Modifier associated with the "Apps" key, or nil to act as a normal
119 key. */
120Lisp_Object Vw32_apps_modifier;
121
122/* Value is nil if Num Lock acts as a function key. */
123Lisp_Object Vw32_enable_num_lock;
124
125/* Value is nil if Caps Lock acts as a function key. */
126Lisp_Object Vw32_enable_caps_lock;
127
128/* Modifier associated with Scroll Lock, or nil to act as a normal key. */
129Lisp_Object Vw32_scroll_lock_modifier;
da36a4d6 130
7ce9aaca 131/* Switch to control whether we inhibit requests for synthesized bold
6fc2811b 132 and italic versions of fonts. */
d84b082d 133int w32_enable_synthesized_fonts;
5ac45f98
GV
134
135/* Enable palette management. */
fbd6baed 136Lisp_Object Vw32_enable_palette;
5ac45f98
GV
137
138/* Control how close left/right button down events must be to
139 be converted to a middle button down event. */
2ba49441 140int w32_mouse_button_tolerance;
5ac45f98 141
84fb1139
KH
142/* Minimum interval between mouse movement (and scroll bar drag)
143 events that are passed on to the event loop. */
2ba49441 144int w32_mouse_move_interval;
84fb1139 145
74214547 146/* Flag to indicate if XBUTTON events should be passed on to Windows. */
74084731 147static int w32_pass_extra_mouse_buttons_to_system;
74214547 148
0b151762 149/* Flag to indicate if media keys should be passed on to Windows. */
74084731 150static int w32_pass_multimedia_buttons_to_system;
0b151762 151
ee78dc32
GV
152/* Non nil if no window manager is in use. */
153Lisp_Object Vx_no_window_manager;
154
d148e14d
JR
155/* If non-zero, a w32 timer that, when it expires, displays an
156 hourglass cursor on all frames. */
157static unsigned hourglass_timer = 0;
158static HWND hourglass_hwnd = NULL;
159
5f004711 160#if 0 /* TODO: Mouse cursor customization. */
ee78dc32
GV
161/* The background and shape of the mouse pointer, and shape when not
162 over text or in the modeline. */
163Lisp_Object Vx_pointer_shape, Vx_nontext_pointer_shape, Vx_mode_pointer_shape;
829b13e2 164Lisp_Object Vx_hourglass_pointer_shape, Vx_window_horizontal_drag_shape;
6fc2811b 165
ee78dc32 166/* The shape when over mouse-sensitive text. */
dfff8a69 167
ee78dc32 168Lisp_Object Vx_sensitive_text_pointer_shape;
5f004711 169#endif
ee78dc32 170
c9b2104d
JR
171#ifndef IDC_HAND
172#define IDC_HAND MAKEINTRESOURCE(32649)
173#endif
174
ee78dc32
GV
175/* Color of chars displayed in cursor box. */
176Lisp_Object Vx_cursor_fore_pixel;
177
1edf84e7 178/* Nonzero if using Windows. */
dfff8a69 179
1edf84e7
GV
180static int w32_in_use;
181
4587b026 182/* Regexp matching a font name whose width is the same as `PIXEL_SIZE'. */
dfff8a69 183
4587b026
GV
184Lisp_Object Vx_pixel_size_width_font_regexp;
185
33d52f9c
GV
186/* Alist of bdf fonts and the files that define them. */
187Lisp_Object Vw32_bdf_filename_alist;
188
f46e6225 189/* A flag to control whether fonts are matched strictly or not. */
74084731 190static int w32_strict_fontnames;
1075afa9 191
c0611964
AI
192/* A flag to control whether we should only repaint if GetUpdateRect
193 indicates there is an update region. */
74084731 194static int w32_strict_painting;
c0611964 195
ee78dc32 196Lisp_Object Qnone;
ee78dc32 197Lisp_Object Qsuppress_icon;
ee78dc32 198Lisp_Object Qundefined_color;
dc220243 199Lisp_Object Qcancel_timer;
27129af9 200Lisp_Object Qfont_param;
adcc3809
GV
201Lisp_Object Qhyper;
202Lisp_Object Qsuper;
203Lisp_Object Qmeta;
204Lisp_Object Qalt;
205Lisp_Object Qctrl;
206Lisp_Object Qcontrol;
207Lisp_Object Qshift;
208
dfff8a69 209
4bf91535 210/* The ANSI codepage. */
2ba49441 211int w32_ansi_code_page;
4bf91535 212
5a8a15ec
JR
213/* Prefix for system colors. */
214#define SYSTEM_COLOR_PREFIX "System"
215#define SYSTEM_COLOR_PREFIX_LEN (sizeof (SYSTEM_COLOR_PREFIX) - 1)
216
5ac45f98
GV
217/* State variables for emulating a three button mouse. */
218#define LMOUSE 1
219#define MMOUSE 2
220#define RMOUSE 4
221
222static int button_state = 0;
fbd6baed 223static W32Msg saved_mouse_button_msg;
48094ace 224static unsigned mouse_button_timer = 0; /* non-zero when timer is active */
fbd6baed 225static W32Msg saved_mouse_move_msg;
48094ace 226static unsigned mouse_move_timer = 0;
84fb1139 227
9eb16b62
JR
228/* Window that is tracking the mouse. */
229static HWND track_mouse_window;
f60ae425 230
64f0809d
JR
231/* Multi-monitor API definitions that are not pulled from the headers
232 since we are compiling for NT 4. */
233#ifndef MONITOR_DEFAULT_TO_NEAREST
234#define MONITOR_DEFAULT_TO_NEAREST 2
235#endif
236/* MinGW headers define MONITORINFO unconditionally, but MSVC ones don't.
237 To avoid a compile error on one or the other, redefine with a new name. */
238struct MONITOR_INFO
239{
240 DWORD cbSize;
241 RECT rcMonitor;
242 RECT rcWork;
243 DWORD dwFlags;
244};
245
8e764ce0
EZ
246/* Reportedly, VS 6 does not have this in its headers. */
247#if defined(_MSC_VER) && _MSC_VER < 1300
248DECLARE_HANDLE(HMONITOR);
249#endif
250
ccc0fdaa
JR
251typedef BOOL (WINAPI * TrackMouseEvent_Proc)
252 (IN OUT LPTRACKMOUSEEVENT lpEventTrack);
820eff5a
JR
253typedef LONG (WINAPI * ImmGetCompositionString_Proc)
254 (IN HIMC context, IN DWORD index, OUT LPVOID buffer, IN DWORD bufLen);
255typedef HIMC (WINAPI * ImmGetContext_Proc) (IN HWND window);
c902b920
JR
256typedef HWND (WINAPI * ImmReleaseContext_Proc) (IN HWND wnd, IN HIMC context);
257typedef HWND (WINAPI * ImmSetCompositionWindow_Proc) (IN HIMC context,
258 IN COMPOSITIONFORM *form);
64f0809d
JR
259typedef HMONITOR (WINAPI * MonitorFromPoint_Proc) (IN POINT pt, IN DWORD flags);
260typedef BOOL (WINAPI * GetMonitorInfo_Proc)
261 (IN HMONITOR monitor, OUT struct MONITOR_INFO* info);
f60ae425 262
ccc0fdaa
JR
263TrackMouseEvent_Proc track_mouse_event_fn = NULL;
264ClipboardSequence_Proc clipboard_sequence_fn = NULL;
820eff5a
JR
265ImmGetCompositionString_Proc get_composition_string_fn = NULL;
266ImmGetContext_Proc get_ime_context_fn = NULL;
c902b920
JR
267ImmReleaseContext_Proc release_ime_context_fn = NULL;
268ImmSetCompositionWindow_Proc set_ime_composition_window_fn = NULL;
64f0809d
JR
269MonitorFromPoint_Proc monitor_from_point_fn = NULL;
270GetMonitorInfo_Proc get_monitor_info_fn = NULL;
820eff5a 271
b4005349 272extern AppendMenuW_Proc unicode_append_menu;
9eb16b62 273
820eff5a
JR
274/* Flag to selectively ignore WM_IME_CHAR messages. */
275static int ignore_ime_char = 0;
276
93fbe8b7 277/* W95 mousewheel handler */
7d0393cf 278unsigned int msh_mousewheel = 0;
93fbe8b7 279
48094ace 280/* Timers */
84fb1139
KH
281#define MOUSE_BUTTON_ID 1
282#define MOUSE_MOVE_ID 2
48094ace 283#define MENU_FREE_ID 3
d148e14d 284#define HOURGLASS_ID 4
48094ace
JR
285/* The delay (milliseconds) before a menu is freed after WM_EXITMENULOOP
286 is received. */
287#define MENU_FREE_DELAY 1000
288static unsigned menu_free_timer = 0;
5ac45f98 289
ee78dc32 290/* The below are defined in frame.c. */
dfff8a69 291
ee78dc32
GV
292extern Lisp_Object Vwindow_system_version;
293
937e601e
AI
294#ifdef GLYPH_DEBUG
295int image_cache_refcount, dpyinfo_refcount;
296#endif
297
298
fbd6baed 299/* From w32term.c. */
2ba49441 300extern int w32_num_mouse_buttons;
ccc2d29c 301extern Lisp_Object Vw32_recognize_altgr;
5ac45f98 302
65906840 303extern HWND w32_system_caret_hwnd;
93f2ca61 304
65906840
JR
305extern int w32_system_caret_height;
306extern int w32_system_caret_x;
307extern int w32_system_caret_y;
93f2ca61
JR
308extern int w32_use_visible_system_caret;
309
d285988b 310static HWND w32_visible_system_caret_hwnd;
65906840 311
5d22ded9
JR
312/* From w32menu.c */
313extern HMENU current_popup_menu;
58e55497 314static int menubar_in_use = 0;
5d22ded9 315
cbfedb1c 316/* From w32uniscribe.c */
cbfedb1c
JR
317extern void syms_of_w32uniscribe ();
318extern int uniscribe_available;
cbfedb1c 319
d148e14d 320/* Function prototypes for hourglass support. */
1391cd54
JR
321static void w32_show_hourglass P_ ((struct frame *));
322static void w32_hide_hourglass P_ ((void));
d148e14d
JR
323
324
ee78dc32 325\f
1edf84e7
GV
326/* Error if we are not connected to MS-Windows. */
327void
328check_w32 ()
329{
330 if (! w32_in_use)
331 error ("MS-Windows not in use or not initialized");
332}
333
334/* Nonzero if we can use mouse menus.
335 You should not call this unless HAVE_MENUS is defined. */
7d0393cf 336
1edf84e7
GV
337int
338have_menus_p ()
339{
340 return w32_in_use;
341}
342
ee78dc32 343/* Extract a frame as a FRAME_PTR, defaulting to the selected frame
fbd6baed 344 and checking validity for W32. */
ee78dc32
GV
345
346FRAME_PTR
347check_x_frame (frame)
348 Lisp_Object frame;
349{
350 FRAME_PTR f;
351
352 if (NILP (frame))
6fc2811b 353 frame = selected_frame;
b7826503 354 CHECK_LIVE_FRAME (frame);
6fc2811b 355 f = XFRAME (frame);
fbd6baed 356 if (! FRAME_W32_P (f))
21517c3d 357 error ("Non-W32 frame used");
ee78dc32
GV
358 return f;
359}
360
7d0393cf 361/* Let the user specify a display with a frame.
fbd6baed 362 nil stands for the selected frame--or, if that is not a w32 frame,
ee78dc32
GV
363 the first display on the list. */
364
6d906347 365struct w32_display_info *
ee78dc32
GV
366check_x_display_info (frame)
367 Lisp_Object frame;
368{
369 if (NILP (frame))
370 {
6fc2811b 371 struct frame *sf = XFRAME (selected_frame);
7d0393cf 372
6fc2811b
JR
373 if (FRAME_W32_P (sf) && FRAME_LIVE_P (sf))
374 return FRAME_W32_DISPLAY_INFO (sf);
ee78dc32 375 else
fbd6baed 376 return &one_w32_display_info;
ee78dc32
GV
377 }
378 else if (STRINGP (frame))
379 return x_display_info_for_name (frame);
380 else
381 {
382 FRAME_PTR f;
383
b7826503 384 CHECK_LIVE_FRAME (frame);
ee78dc32 385 f = XFRAME (frame);
fbd6baed 386 if (! FRAME_W32_P (f))
21517c3d 387 error ("Non-W32 frame used");
fbd6baed 388 return FRAME_W32_DISPLAY_INFO (f);
ee78dc32
GV
389 }
390}
391\f
fbd6baed 392/* Return the Emacs frame-object corresponding to an w32 window.
ee78dc32
GV
393 It could be the frame's main window or an icon window. */
394
395/* This function can be called during GC, so use GC_xxx type test macros. */
396
397struct frame *
398x_window_to_frame (dpyinfo, wdesc)
fbd6baed 399 struct w32_display_info *dpyinfo;
ee78dc32
GV
400 HWND wdesc;
401{
402 Lisp_Object tail, frame;
403 struct frame *f;
404
8e50cc2d 405 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
ee78dc32 406 {
8e713be6 407 frame = XCAR (tail);
8e50cc2d 408 if (!FRAMEP (frame))
ee78dc32
GV
409 continue;
410 f = XFRAME (frame);
2d764c78 411 if (!FRAME_W32_P (f) || FRAME_W32_DISPLAY_INFO (f) != dpyinfo)
ee78dc32 412 continue;
f79e6790 413
fbd6baed 414 if (FRAME_W32_WINDOW (f) == wdesc)
ee78dc32
GV
415 return f;
416 }
417 return 0;
418}
419
420\f
937e601e
AI
421static Lisp_Object unwind_create_frame P_ ((Lisp_Object));
422static Lisp_Object unwind_create_tip_frame P_ ((Lisp_Object));
0962822d
JB
423static void my_create_window P_ ((struct frame *));
424static void my_create_tip_window P_ ((struct frame *));
6d906347 425
767b1ff0 426/* TODO: Native Input Method support; see x_create_im. */
6fc2811b
JR
427void x_set_foreground_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
428void x_set_background_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
429void x_set_mouse_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
430void x_set_cursor_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
431void x_set_border_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
432void x_set_cursor_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
433void x_set_icon_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
434void x_set_icon_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
6fc2811b 435void x_explicitly_set_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
6fc2811b 436void x_set_menu_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
6fc2811b 437void x_set_title P_ ((struct frame *, Lisp_Object, Lisp_Object));
6fc2811b 438void x_set_tool_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
3cf3436e
JR
439static void x_edge_detection P_ ((struct frame *, struct image *, Lisp_Object,
440 Lisp_Object));
ee78dc32 441
ee78dc32 442
ee78dc32 443\f
ee78dc32
GV
444
445/* Store the screen positions of frame F into XPTR and YPTR.
446 These are the positions of the containing window manager window,
447 not Emacs's own window. */
448
449void
450x_real_positions (f, xptr, yptr)
451 FRAME_PTR f;
452 int *xptr, *yptr;
453{
454 POINT pt;
f7b9d4d1 455 RECT rect;
3c190163 456
ee04257d
JR
457 /* Get the bounds of the WM window. */
458 GetWindowRect (FRAME_W32_WINDOW (f), &rect);
f7b9d4d1 459
ee04257d
JR
460 pt.x = 0;
461 pt.y = 0;
ee78dc32 462
ee04257d
JR
463 /* Convert (0, 0) in the client area to screen co-ordinates. */
464 ClientToScreen (FRAME_W32_WINDOW (f), &pt);
ee78dc32 465
f7b9d4d1 466 /* Remember x_pixels_diff and y_pixels_diff. */
be786000
KS
467 f->x_pixels_diff = pt.x - rect.left;
468 f->y_pixels_diff = pt.y - rect.top;
f7b9d4d1 469
ee04257d
JR
470 *xptr = rect.left;
471 *yptr = rect.top;
ee78dc32
GV
472}
473
ee78dc32
GV
474\f
475
74e1aeec
JR
476DEFUN ("w32-define-rgb-color", Fw32_define_rgb_color,
477 Sw32_define_rgb_color, 4, 4, 0,
23f250f4 478 doc: /* Convert RGB numbers to a Windows color reference and associate with NAME.
02b39a28 479This adds or updates a named color to `w32-color-map', making it
74e1aeec
JR
480available for use. The original entry's RGB ref is returned, or nil
481if the entry is new. */)
5ac45f98
GV
482 (red, green, blue, name)
483 Lisp_Object red, green, blue, name;
ee78dc32 484{
5ac45f98
GV
485 Lisp_Object rgb;
486 Lisp_Object oldrgb = Qnil;
487 Lisp_Object entry;
488
b7826503
PJ
489 CHECK_NUMBER (red);
490 CHECK_NUMBER (green);
491 CHECK_NUMBER (blue);
492 CHECK_STRING (name);
ee78dc32 493
74084731 494 XSETINT (rgb, RGB (XUINT (red), XUINT (green), XUINT (blue)));
ee78dc32 495
5ac45f98 496 BLOCK_INPUT;
ee78dc32 497
fbd6baed
GV
498 /* replace existing entry in w32-color-map or add new entry. */
499 entry = Fassoc (name, Vw32_color_map);
5ac45f98
GV
500 if (NILP (entry))
501 {
502 entry = Fcons (name, rgb);
fbd6baed 503 Vw32_color_map = Fcons (entry, Vw32_color_map);
5ac45f98
GV
504 }
505 else
506 {
507 oldrgb = Fcdr (entry);
508 Fsetcdr (entry, rgb);
509 }
510
511 UNBLOCK_INPUT;
512
513 return (oldrgb);
ee78dc32
GV
514}
515
fbd6baed 516/* The default colors for the w32 color map */
7d0393cf 517typedef struct colormap_t
ee78dc32
GV
518{
519 char *name;
520 COLORREF colorref;
521} colormap_t;
522
7d0393cf 523colormap_t w32_color_map[] =
ee78dc32 524{
1da8a614
GV
525 {"snow" , PALETTERGB (255,250,250)},
526 {"ghost white" , PALETTERGB (248,248,255)},
527 {"GhostWhite" , PALETTERGB (248,248,255)},
528 {"white smoke" , PALETTERGB (245,245,245)},
529 {"WhiteSmoke" , PALETTERGB (245,245,245)},
530 {"gainsboro" , PALETTERGB (220,220,220)},
531 {"floral white" , PALETTERGB (255,250,240)},
532 {"FloralWhite" , PALETTERGB (255,250,240)},
533 {"old lace" , PALETTERGB (253,245,230)},
534 {"OldLace" , PALETTERGB (253,245,230)},
535 {"linen" , PALETTERGB (250,240,230)},
536 {"antique white" , PALETTERGB (250,235,215)},
537 {"AntiqueWhite" , PALETTERGB (250,235,215)},
538 {"papaya whip" , PALETTERGB (255,239,213)},
539 {"PapayaWhip" , PALETTERGB (255,239,213)},
540 {"blanched almond" , PALETTERGB (255,235,205)},
541 {"BlanchedAlmond" , PALETTERGB (255,235,205)},
542 {"bisque" , PALETTERGB (255,228,196)},
543 {"peach puff" , PALETTERGB (255,218,185)},
544 {"PeachPuff" , PALETTERGB (255,218,185)},
545 {"navajo white" , PALETTERGB (255,222,173)},
546 {"NavajoWhite" , PALETTERGB (255,222,173)},
547 {"moccasin" , PALETTERGB (255,228,181)},
548 {"cornsilk" , PALETTERGB (255,248,220)},
549 {"ivory" , PALETTERGB (255,255,240)},
550 {"lemon chiffon" , PALETTERGB (255,250,205)},
551 {"LemonChiffon" , PALETTERGB (255,250,205)},
552 {"seashell" , PALETTERGB (255,245,238)},
553 {"honeydew" , PALETTERGB (240,255,240)},
554 {"mint cream" , PALETTERGB (245,255,250)},
555 {"MintCream" , PALETTERGB (245,255,250)},
556 {"azure" , PALETTERGB (240,255,255)},
557 {"alice blue" , PALETTERGB (240,248,255)},
558 {"AliceBlue" , PALETTERGB (240,248,255)},
559 {"lavender" , PALETTERGB (230,230,250)},
560 {"lavender blush" , PALETTERGB (255,240,245)},
561 {"LavenderBlush" , PALETTERGB (255,240,245)},
562 {"misty rose" , PALETTERGB (255,228,225)},
563 {"MistyRose" , PALETTERGB (255,228,225)},
564 {"white" , PALETTERGB (255,255,255)},
565 {"black" , PALETTERGB ( 0, 0, 0)},
566 {"dark slate gray" , PALETTERGB ( 47, 79, 79)},
567 {"DarkSlateGray" , PALETTERGB ( 47, 79, 79)},
568 {"dark slate grey" , PALETTERGB ( 47, 79, 79)},
569 {"DarkSlateGrey" , PALETTERGB ( 47, 79, 79)},
570 {"dim gray" , PALETTERGB (105,105,105)},
571 {"DimGray" , PALETTERGB (105,105,105)},
572 {"dim grey" , PALETTERGB (105,105,105)},
573 {"DimGrey" , PALETTERGB (105,105,105)},
574 {"slate gray" , PALETTERGB (112,128,144)},
575 {"SlateGray" , PALETTERGB (112,128,144)},
576 {"slate grey" , PALETTERGB (112,128,144)},
577 {"SlateGrey" , PALETTERGB (112,128,144)},
578 {"light slate gray" , PALETTERGB (119,136,153)},
579 {"LightSlateGray" , PALETTERGB (119,136,153)},
580 {"light slate grey" , PALETTERGB (119,136,153)},
581 {"LightSlateGrey" , PALETTERGB (119,136,153)},
582 {"gray" , PALETTERGB (190,190,190)},
583 {"grey" , PALETTERGB (190,190,190)},
584 {"light grey" , PALETTERGB (211,211,211)},
585 {"LightGrey" , PALETTERGB (211,211,211)},
586 {"light gray" , PALETTERGB (211,211,211)},
587 {"LightGray" , PALETTERGB (211,211,211)},
588 {"midnight blue" , PALETTERGB ( 25, 25,112)},
589 {"MidnightBlue" , PALETTERGB ( 25, 25,112)},
590 {"navy" , PALETTERGB ( 0, 0,128)},
591 {"navy blue" , PALETTERGB ( 0, 0,128)},
592 {"NavyBlue" , PALETTERGB ( 0, 0,128)},
593 {"cornflower blue" , PALETTERGB (100,149,237)},
594 {"CornflowerBlue" , PALETTERGB (100,149,237)},
595 {"dark slate blue" , PALETTERGB ( 72, 61,139)},
596 {"DarkSlateBlue" , PALETTERGB ( 72, 61,139)},
597 {"slate blue" , PALETTERGB (106, 90,205)},
598 {"SlateBlue" , PALETTERGB (106, 90,205)},
599 {"medium slate blue" , PALETTERGB (123,104,238)},
600 {"MediumSlateBlue" , PALETTERGB (123,104,238)},
601 {"light slate blue" , PALETTERGB (132,112,255)},
602 {"LightSlateBlue" , PALETTERGB (132,112,255)},
603 {"medium blue" , PALETTERGB ( 0, 0,205)},
604 {"MediumBlue" , PALETTERGB ( 0, 0,205)},
605 {"royal blue" , PALETTERGB ( 65,105,225)},
606 {"RoyalBlue" , PALETTERGB ( 65,105,225)},
607 {"blue" , PALETTERGB ( 0, 0,255)},
608 {"dodger blue" , PALETTERGB ( 30,144,255)},
609 {"DodgerBlue" , PALETTERGB ( 30,144,255)},
610 {"deep sky blue" , PALETTERGB ( 0,191,255)},
611 {"DeepSkyBlue" , PALETTERGB ( 0,191,255)},
612 {"sky blue" , PALETTERGB (135,206,235)},
613 {"SkyBlue" , PALETTERGB (135,206,235)},
614 {"light sky blue" , PALETTERGB (135,206,250)},
615 {"LightSkyBlue" , PALETTERGB (135,206,250)},
616 {"steel blue" , PALETTERGB ( 70,130,180)},
617 {"SteelBlue" , PALETTERGB ( 70,130,180)},
618 {"light steel blue" , PALETTERGB (176,196,222)},
619 {"LightSteelBlue" , PALETTERGB (176,196,222)},
620 {"light blue" , PALETTERGB (173,216,230)},
621 {"LightBlue" , PALETTERGB (173,216,230)},
622 {"powder blue" , PALETTERGB (176,224,230)},
623 {"PowderBlue" , PALETTERGB (176,224,230)},
624 {"pale turquoise" , PALETTERGB (175,238,238)},
625 {"PaleTurquoise" , PALETTERGB (175,238,238)},
626 {"dark turquoise" , PALETTERGB ( 0,206,209)},
627 {"DarkTurquoise" , PALETTERGB ( 0,206,209)},
628 {"medium turquoise" , PALETTERGB ( 72,209,204)},
629 {"MediumTurquoise" , PALETTERGB ( 72,209,204)},
630 {"turquoise" , PALETTERGB ( 64,224,208)},
631 {"cyan" , PALETTERGB ( 0,255,255)},
632 {"light cyan" , PALETTERGB (224,255,255)},
633 {"LightCyan" , PALETTERGB (224,255,255)},
634 {"cadet blue" , PALETTERGB ( 95,158,160)},
635 {"CadetBlue" , PALETTERGB ( 95,158,160)},
636 {"medium aquamarine" , PALETTERGB (102,205,170)},
637 {"MediumAquamarine" , PALETTERGB (102,205,170)},
638 {"aquamarine" , PALETTERGB (127,255,212)},
639 {"dark green" , PALETTERGB ( 0,100, 0)},
640 {"DarkGreen" , PALETTERGB ( 0,100, 0)},
641 {"dark olive green" , PALETTERGB ( 85,107, 47)},
642 {"DarkOliveGreen" , PALETTERGB ( 85,107, 47)},
643 {"dark sea green" , PALETTERGB (143,188,143)},
644 {"DarkSeaGreen" , PALETTERGB (143,188,143)},
645 {"sea green" , PALETTERGB ( 46,139, 87)},
646 {"SeaGreen" , PALETTERGB ( 46,139, 87)},
647 {"medium sea green" , PALETTERGB ( 60,179,113)},
648 {"MediumSeaGreen" , PALETTERGB ( 60,179,113)},
649 {"light sea green" , PALETTERGB ( 32,178,170)},
650 {"LightSeaGreen" , PALETTERGB ( 32,178,170)},
651 {"pale green" , PALETTERGB (152,251,152)},
652 {"PaleGreen" , PALETTERGB (152,251,152)},
653 {"spring green" , PALETTERGB ( 0,255,127)},
654 {"SpringGreen" , PALETTERGB ( 0,255,127)},
655 {"lawn green" , PALETTERGB (124,252, 0)},
656 {"LawnGreen" , PALETTERGB (124,252, 0)},
657 {"green" , PALETTERGB ( 0,255, 0)},
658 {"chartreuse" , PALETTERGB (127,255, 0)},
659 {"medium spring green" , PALETTERGB ( 0,250,154)},
660 {"MediumSpringGreen" , PALETTERGB ( 0,250,154)},
661 {"green yellow" , PALETTERGB (173,255, 47)},
662 {"GreenYellow" , PALETTERGB (173,255, 47)},
663 {"lime green" , PALETTERGB ( 50,205, 50)},
664 {"LimeGreen" , PALETTERGB ( 50,205, 50)},
665 {"yellow green" , PALETTERGB (154,205, 50)},
666 {"YellowGreen" , PALETTERGB (154,205, 50)},
667 {"forest green" , PALETTERGB ( 34,139, 34)},
668 {"ForestGreen" , PALETTERGB ( 34,139, 34)},
669 {"olive drab" , PALETTERGB (107,142, 35)},
670 {"OliveDrab" , PALETTERGB (107,142, 35)},
671 {"dark khaki" , PALETTERGB (189,183,107)},
672 {"DarkKhaki" , PALETTERGB (189,183,107)},
673 {"khaki" , PALETTERGB (240,230,140)},
674 {"pale goldenrod" , PALETTERGB (238,232,170)},
675 {"PaleGoldenrod" , PALETTERGB (238,232,170)},
676 {"light goldenrod yellow" , PALETTERGB (250,250,210)},
677 {"LightGoldenrodYellow" , PALETTERGB (250,250,210)},
678 {"light yellow" , PALETTERGB (255,255,224)},
679 {"LightYellow" , PALETTERGB (255,255,224)},
680 {"yellow" , PALETTERGB (255,255, 0)},
681 {"gold" , PALETTERGB (255,215, 0)},
682 {"light goldenrod" , PALETTERGB (238,221,130)},
683 {"LightGoldenrod" , PALETTERGB (238,221,130)},
684 {"goldenrod" , PALETTERGB (218,165, 32)},
685 {"dark goldenrod" , PALETTERGB (184,134, 11)},
686 {"DarkGoldenrod" , PALETTERGB (184,134, 11)},
687 {"rosy brown" , PALETTERGB (188,143,143)},
688 {"RosyBrown" , PALETTERGB (188,143,143)},
689 {"indian red" , PALETTERGB (205, 92, 92)},
690 {"IndianRed" , PALETTERGB (205, 92, 92)},
691 {"saddle brown" , PALETTERGB (139, 69, 19)},
692 {"SaddleBrown" , PALETTERGB (139, 69, 19)},
693 {"sienna" , PALETTERGB (160, 82, 45)},
694 {"peru" , PALETTERGB (205,133, 63)},
695 {"burlywood" , PALETTERGB (222,184,135)},
696 {"beige" , PALETTERGB (245,245,220)},
697 {"wheat" , PALETTERGB (245,222,179)},
698 {"sandy brown" , PALETTERGB (244,164, 96)},
699 {"SandyBrown" , PALETTERGB (244,164, 96)},
700 {"tan" , PALETTERGB (210,180,140)},
701 {"chocolate" , PALETTERGB (210,105, 30)},
702 {"firebrick" , PALETTERGB (178,34, 34)},
703 {"brown" , PALETTERGB (165,42, 42)},
704 {"dark salmon" , PALETTERGB (233,150,122)},
705 {"DarkSalmon" , PALETTERGB (233,150,122)},
706 {"salmon" , PALETTERGB (250,128,114)},
707 {"light salmon" , PALETTERGB (255,160,122)},
708 {"LightSalmon" , PALETTERGB (255,160,122)},
709 {"orange" , PALETTERGB (255,165, 0)},
710 {"dark orange" , PALETTERGB (255,140, 0)},
711 {"DarkOrange" , PALETTERGB (255,140, 0)},
712 {"coral" , PALETTERGB (255,127, 80)},
713 {"light coral" , PALETTERGB (240,128,128)},
714 {"LightCoral" , PALETTERGB (240,128,128)},
715 {"tomato" , PALETTERGB (255, 99, 71)},
716 {"orange red" , PALETTERGB (255, 69, 0)},
717 {"OrangeRed" , PALETTERGB (255, 69, 0)},
718 {"red" , PALETTERGB (255, 0, 0)},
719 {"hot pink" , PALETTERGB (255,105,180)},
720 {"HotPink" , PALETTERGB (255,105,180)},
721 {"deep pink" , PALETTERGB (255, 20,147)},
722 {"DeepPink" , PALETTERGB (255, 20,147)},
723 {"pink" , PALETTERGB (255,192,203)},
724 {"light pink" , PALETTERGB (255,182,193)},
725 {"LightPink" , PALETTERGB (255,182,193)},
726 {"pale violet red" , PALETTERGB (219,112,147)},
727 {"PaleVioletRed" , PALETTERGB (219,112,147)},
728 {"maroon" , PALETTERGB (176, 48, 96)},
729 {"medium violet red" , PALETTERGB (199, 21,133)},
730 {"MediumVioletRed" , PALETTERGB (199, 21,133)},
731 {"violet red" , PALETTERGB (208, 32,144)},
732 {"VioletRed" , PALETTERGB (208, 32,144)},
733 {"magenta" , PALETTERGB (255, 0,255)},
734 {"violet" , PALETTERGB (238,130,238)},
735 {"plum" , PALETTERGB (221,160,221)},
736 {"orchid" , PALETTERGB (218,112,214)},
737 {"medium orchid" , PALETTERGB (186, 85,211)},
738 {"MediumOrchid" , PALETTERGB (186, 85,211)},
739 {"dark orchid" , PALETTERGB (153, 50,204)},
740 {"DarkOrchid" , PALETTERGB (153, 50,204)},
741 {"dark violet" , PALETTERGB (148, 0,211)},
742 {"DarkViolet" , PALETTERGB (148, 0,211)},
743 {"blue violet" , PALETTERGB (138, 43,226)},
744 {"BlueViolet" , PALETTERGB (138, 43,226)},
745 {"purple" , PALETTERGB (160, 32,240)},
746 {"medium purple" , PALETTERGB (147,112,219)},
747 {"MediumPurple" , PALETTERGB (147,112,219)},
748 {"thistle" , PALETTERGB (216,191,216)},
749 {"gray0" , PALETTERGB ( 0, 0, 0)},
750 {"grey0" , PALETTERGB ( 0, 0, 0)},
751 {"dark grey" , PALETTERGB (169,169,169)},
752 {"DarkGrey" , PALETTERGB (169,169,169)},
753 {"dark gray" , PALETTERGB (169,169,169)},
754 {"DarkGray" , PALETTERGB (169,169,169)},
755 {"dark blue" , PALETTERGB ( 0, 0,139)},
756 {"DarkBlue" , PALETTERGB ( 0, 0,139)},
757 {"dark cyan" , PALETTERGB ( 0,139,139)},
758 {"DarkCyan" , PALETTERGB ( 0,139,139)},
759 {"dark magenta" , PALETTERGB (139, 0,139)},
760 {"DarkMagenta" , PALETTERGB (139, 0,139)},
761 {"dark red" , PALETTERGB (139, 0, 0)},
762 {"DarkRed" , PALETTERGB (139, 0, 0)},
763 {"light green" , PALETTERGB (144,238,144)},
764 {"LightGreen" , PALETTERGB (144,238,144)},
ee78dc32
GV
765};
766
fbd6baed 767DEFUN ("w32-default-color-map", Fw32_default_color_map, Sw32_default_color_map,
74e1aeec 768 0, 0, 0, doc: /* Return the default color map. */)
ee78dc32
GV
769 ()
770{
771 int i;
fbd6baed 772 colormap_t *pc = w32_color_map;
ee78dc32 773 Lisp_Object cmap;
7d0393cf 774
ee78dc32 775 BLOCK_INPUT;
7d0393cf 776
ee78dc32 777 cmap = Qnil;
7d0393cf
JB
778
779 for (i = 0; i < sizeof (w32_color_map) / sizeof (w32_color_map[0]);
ee78dc32
GV
780 pc++, i++)
781 cmap = Fcons (Fcons (build_string (pc->name),
782 make_number (pc->colorref)),
783 cmap);
7d0393cf 784
ee78dc32 785 UNBLOCK_INPUT;
7d0393cf 786
ee78dc32
GV
787 return (cmap);
788}
ee78dc32 789
74084731 790static Lisp_Object
fbd6baed 791w32_to_x_color (rgb)
ee78dc32
GV
792 Lisp_Object rgb;
793{
794 Lisp_Object color;
7d0393cf 795
b7826503 796 CHECK_NUMBER (rgb);
7d0393cf 797
ee78dc32 798 BLOCK_INPUT;
7d0393cf 799
fbd6baed 800 color = Frassq (rgb, Vw32_color_map);
7d0393cf 801
ee78dc32 802 UNBLOCK_INPUT;
7d0393cf 803
ee78dc32
GV
804 if (!NILP (color))
805 return (Fcar (color));
806 else
807 return Qnil;
808}
809
2ba49441 810static Lisp_Object
5d7fed93
GV
811w32_color_map_lookup (colorname)
812 char *colorname;
813{
814 Lisp_Object tail, ret = Qnil;
815
816 BLOCK_INPUT;
817
99784d63 818 for (tail = Vw32_color_map; CONSP (tail); tail = XCDR (tail))
5d7fed93
GV
819 {
820 register Lisp_Object elt, tem;
821
99784d63 822 elt = XCAR (tail);
5d7fed93
GV
823 if (!CONSP (elt)) continue;
824
825 tem = Fcar (elt);
826
d5db4077 827 if (lstrcmpi (SDATA (tem), colorname) == 0)
5d7fed93 828 {
2ba49441 829 ret = Fcdr (elt);
5d7fed93
GV
830 break;
831 }
832
833 QUIT;
834 }
835
836
837 UNBLOCK_INPUT;
838
839 return ret;
840}
841
5a8a15ec
JR
842
843static void
844add_system_logical_colors_to_map (system_colors)
845 Lisp_Object *system_colors;
846{
847 HKEY colors_key;
848
849 /* Other registry operations are done with input blocked. */
850 BLOCK_INPUT;
851
852 /* Look for "Control Panel/Colors" under User and Machine registry
853 settings. */
854 if (RegOpenKeyEx (HKEY_CURRENT_USER, "Control Panel\\Colors", 0,
855 KEY_READ, &colors_key) == ERROR_SUCCESS
856 || RegOpenKeyEx (HKEY_LOCAL_MACHINE, "Control Panel\\Colors", 0,
857 KEY_READ, &colors_key) == ERROR_SUCCESS)
858 {
859 /* List all keys. */
860 char color_buffer[64];
861 char full_name_buffer[MAX_PATH + SYSTEM_COLOR_PREFIX_LEN];
862 int index = 0;
863 DWORD name_size, color_size;
864 char *name_buffer = full_name_buffer + SYSTEM_COLOR_PREFIX_LEN;
865
866 name_size = sizeof (full_name_buffer) - SYSTEM_COLOR_PREFIX_LEN;
867 color_size = sizeof (color_buffer);
868
869 strcpy (full_name_buffer, SYSTEM_COLOR_PREFIX);
870
871 while (RegEnumValueA (colors_key, index, name_buffer, &name_size,
872 NULL, NULL, color_buffer, &color_size)
873 == ERROR_SUCCESS)
874 {
875 int r, g, b;
876 if (sscanf (color_buffer, " %u %u %u", &r, &g, &b) == 3)
877 *system_colors = Fcons (Fcons (build_string (full_name_buffer),
878 make_number (RGB (r, g, b))),
879 *system_colors);
880
881 name_size = sizeof (full_name_buffer) - SYSTEM_COLOR_PREFIX_LEN;
882 color_size = sizeof (color_buffer);
883 index++;
884 }
885 RegCloseKey (colors_key);
886 }
887
888 UNBLOCK_INPUT;
889}
890
891
2ba49441 892static Lisp_Object
fbd6baed 893x_to_w32_color (colorname)
ee78dc32
GV
894 char * colorname;
895{
8edb0a6f
JR
896 register Lisp_Object ret = Qnil;
897
ee78dc32 898 BLOCK_INPUT;
1edf84e7
GV
899
900 if (colorname[0] == '#')
901 {
902 /* Could be an old-style RGB Device specification. */
903 char *color;
904 int size;
905 color = colorname + 1;
7d0393cf 906
74084731 907 size = strlen (color);
1edf84e7
GV
908 if (size == 3 || size == 6 || size == 9 || size == 12)
909 {
910 UINT colorval;
911 int i, pos;
912 pos = 0;
913 size /= 3;
914 colorval = 0;
7d0393cf 915
1edf84e7
GV
916 for (i = 0; i < 3; i++)
917 {
918 char *end;
919 char t;
920 unsigned long value;
921
922 /* The check for 'x' in the following conditional takes into
923 account the fact that strtol allows a "0x" in front of
924 our numbers, and we don't. */
74084731 925 if (!isxdigit (color[0]) || color[1] == 'x')
1edf84e7
GV
926 break;
927 t = color[size];
928 color[size] = '\0';
74084731 929 value = strtoul (color, &end, 16);
1edf84e7
GV
930 color[size] = t;
931 if (errno == ERANGE || end - color != size)
932 break;
933 switch (size)
934 {
935 case 1:
936 value = value * 0x10;
937 break;
938 case 2:
939 break;
940 case 3:
941 value /= 0x10;
942 break;
943 case 4:
944 value /= 0x100;
945 break;
946 }
947 colorval |= (value << pos);
948 pos += 0x8;
949 if (i == 2)
950 {
951 UNBLOCK_INPUT;
2ba49441
JR
952 XSETINT (ret, colorval);
953 return ret;
1edf84e7
GV
954 }
955 color = end;
956 }
957 }
958 }
74084731 959 else if (strnicmp (colorname, "rgb:", 4) == 0)
1edf84e7
GV
960 {
961 char *color;
962 UINT colorval;
963 int i, pos;
964 pos = 0;
965
966 colorval = 0;
967 color = colorname + 4;
968 for (i = 0; i < 3; i++)
969 {
970 char *end;
971 unsigned long value;
7d0393cf 972
1edf84e7
GV
973 /* The check for 'x' in the following conditional takes into
974 account the fact that strtol allows a "0x" in front of
975 our numbers, and we don't. */
74084731 976 if (!isxdigit (color[0]) || color[1] == 'x')
1edf84e7 977 break;
74084731 978 value = strtoul (color, &end, 16);
1edf84e7
GV
979 if (errno == ERANGE)
980 break;
981 switch (end - color)
982 {
983 case 1:
984 value = value * 0x10 + value;
985 break;
986 case 2:
987 break;
988 case 3:
989 value /= 0x10;
990 break;
991 case 4:
992 value /= 0x100;
993 break;
994 default:
995 value = ULONG_MAX;
996 }
997 if (value == ULONG_MAX)
998 break;
999 colorval |= (value << pos);
1000 pos += 0x8;
1001 if (i == 2)
1002 {
1003 if (*end != '\0')
1004 break;
1005 UNBLOCK_INPUT;
2ba49441
JR
1006 XSETINT (ret, colorval);
1007 return ret;
1edf84e7
GV
1008 }
1009 if (*end != '/')
1010 break;
1011 color = end + 1;
1012 }
1013 }
74084731 1014 else if (strnicmp (colorname, "rgbi:", 5) == 0)
1edf84e7
GV
1015 {
1016 /* This is an RGB Intensity specification. */
1017 char *color;
1018 UINT colorval;
1019 int i, pos;
1020 pos = 0;
1021
1022 colorval = 0;
1023 color = colorname + 5;
1024 for (i = 0; i < 3; i++)
1025 {
1026 char *end;
1027 double value;
1028 UINT val;
1029
74084731 1030 value = strtod (color, &end);
1edf84e7
GV
1031 if (errno == ERANGE)
1032 break;
1033 if (value < 0.0 || value > 1.0)
1034 break;
1035 val = (UINT)(0x100 * value);
7d0393cf 1036 /* We used 0x100 instead of 0xFF to give a continuous
1edf84e7
GV
1037 range between 0.0 and 1.0 inclusive. The next statement
1038 fixes the 1.0 case. */
1039 if (val == 0x100)
1040 val = 0xFF;
1041 colorval |= (val << pos);
1042 pos += 0x8;
1043 if (i == 2)
1044 {
1045 if (*end != '\0')
1046 break;
1047 UNBLOCK_INPUT;
2ba49441
JR
1048 XSETINT (ret, colorval);
1049 return ret;
1edf84e7
GV
1050 }
1051 if (*end != '/')
1052 break;
1053 color = end + 1;
1054 }
1055 }
1056 /* I am not going to attempt to handle any of the CIE color schemes
1057 or TekHVC, since I don't know the algorithms for conversion to
1058 RGB. */
f695b4b1
GV
1059
1060 /* If we fail to lookup the color name in w32_color_map, then check the
7d0393cf 1061 colorname to see if it can be crudely approximated: If the X color
f695b4b1
GV
1062 ends in a number (e.g., "darkseagreen2"), strip the number and
1063 return the result of looking up the base color name. */
1064 ret = w32_color_map_lookup (colorname);
7d0393cf 1065 if (NILP (ret))
ee78dc32 1066 {
f695b4b1 1067 int len = strlen (colorname);
ee78dc32 1068
7d0393cf 1069 if (isdigit (colorname[len - 1]))
f695b4b1 1070 {
8b77111c 1071 char *ptr, *approx = alloca (len + 1);
ee78dc32 1072
f695b4b1
GV
1073 strcpy (approx, colorname);
1074 ptr = &approx[len - 1];
7d0393cf 1075 while (ptr > approx && isdigit (*ptr))
f695b4b1 1076 *ptr-- = '\0';
ee78dc32 1077
f695b4b1 1078 ret = w32_color_map_lookup (approx);
ee78dc32 1079 }
ee78dc32 1080 }
7d0393cf 1081
ee78dc32 1082 UNBLOCK_INPUT;
ee78dc32
GV
1083 return ret;
1084}
1085
5ac45f98 1086void
fbd6baed 1087w32_regenerate_palette (FRAME_PTR f)
5ac45f98 1088{
fbd6baed 1089 struct w32_palette_entry * list;
5ac45f98
GV
1090 LOGPALETTE * log_palette;
1091 HPALETTE new_palette;
1092 int i;
1093
1094 /* don't bother trying to create palette if not supported */
fbd6baed 1095 if (! FRAME_W32_DISPLAY_INFO (f)->has_palette)
5ac45f98
GV
1096 return;
1097
1098 log_palette = (LOGPALETTE *)
1099 alloca (sizeof (LOGPALETTE) +
fbd6baed 1100 FRAME_W32_DISPLAY_INFO (f)->num_colors * sizeof (PALETTEENTRY));
5ac45f98 1101 log_palette->palVersion = 0x300;
fbd6baed 1102 log_palette->palNumEntries = FRAME_W32_DISPLAY_INFO (f)->num_colors;
5ac45f98 1103
fbd6baed 1104 list = FRAME_W32_DISPLAY_INFO (f)->color_list;
5ac45f98 1105 for (i = 0;
fbd6baed 1106 i < FRAME_W32_DISPLAY_INFO (f)->num_colors;
5ac45f98
GV
1107 i++, list = list->next)
1108 log_palette->palPalEntry[i] = list->entry;
1109
1110 new_palette = CreatePalette (log_palette);
1111
1112 enter_crit ();
1113
fbd6baed
GV
1114 if (FRAME_W32_DISPLAY_INFO (f)->palette)
1115 DeleteObject (FRAME_W32_DISPLAY_INFO (f)->palette);
1116 FRAME_W32_DISPLAY_INFO (f)->palette = new_palette;
5ac45f98
GV
1117
1118 /* Realize display palette and garbage all frames. */
1119 release_frame_dc (f, get_frame_dc (f));
1120
1121 leave_crit ();
1122}
1123
fbd6baed
GV
1124#define W32_COLOR(pe) RGB (pe.peRed, pe.peGreen, pe.peBlue)
1125#define SET_W32_COLOR(pe, color) \
5ac45f98
GV
1126 do \
1127 { \
1128 pe.peRed = GetRValue (color); \
1129 pe.peGreen = GetGValue (color); \
1130 pe.peBlue = GetBValue (color); \
1131 pe.peFlags = 0; \
1132 } while (0)
1133
1134#if 0
1135/* Keep these around in case we ever want to track color usage. */
1136void
fbd6baed 1137w32_map_color (FRAME_PTR f, COLORREF color)
5ac45f98 1138{
fbd6baed 1139 struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
5ac45f98 1140
fbd6baed 1141 if (NILP (Vw32_enable_palette))
5ac45f98
GV
1142 return;
1143
1144 /* check if color is already mapped */
1145 while (list)
1146 {
fbd6baed 1147 if (W32_COLOR (list->entry) == color)
5ac45f98
GV
1148 {
1149 ++list->refcount;
1150 return;
1151 }
1152 list = list->next;
1153 }
1154
1155 /* not already mapped, so add to list and recreate Windows palette */
fbd6baed
GV
1156 list = (struct w32_palette_entry *)
1157 xmalloc (sizeof (struct w32_palette_entry));
1158 SET_W32_COLOR (list->entry, color);
5ac45f98 1159 list->refcount = 1;
fbd6baed
GV
1160 list->next = FRAME_W32_DISPLAY_INFO (f)->color_list;
1161 FRAME_W32_DISPLAY_INFO (f)->color_list = list;
1162 FRAME_W32_DISPLAY_INFO (f)->num_colors++;
5ac45f98
GV
1163
1164 /* set flag that palette must be regenerated */
fbd6baed 1165 FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
5ac45f98
GV
1166}
1167
1168void
fbd6baed 1169w32_unmap_color (FRAME_PTR f, COLORREF color)
5ac45f98 1170{
fbd6baed
GV
1171 struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
1172 struct w32_palette_entry **prev = &FRAME_W32_DISPLAY_INFO (f)->color_list;
5ac45f98 1173
fbd6baed 1174 if (NILP (Vw32_enable_palette))
5ac45f98
GV
1175 return;
1176
1177 /* check if color is already mapped */
1178 while (list)
1179 {
fbd6baed 1180 if (W32_COLOR (list->entry) == color)
5ac45f98
GV
1181 {
1182 if (--list->refcount == 0)
1183 {
1184 *prev = list->next;
1185 xfree (list);
fbd6baed 1186 FRAME_W32_DISPLAY_INFO (f)->num_colors--;
5ac45f98
GV
1187 break;
1188 }
1189 else
1190 return;
1191 }
1192 prev = &list->next;
1193 list = list->next;
1194 }
1195
1196 /* set flag that palette must be regenerated */
fbd6baed 1197 FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
5ac45f98
GV
1198}
1199#endif
1200
6fc2811b
JR
1201
1202/* Gamma-correct COLOR on frame F. */
1203
1204void
1205gamma_correct (f, color)
1206 struct frame *f;
1207 COLORREF *color;
1208{
1209 if (f->gamma)
1210 {
1211 *color = PALETTERGB (
1212 pow (GetRValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
1213 pow (GetGValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
1214 pow (GetBValue (*color) / 255.0, f->gamma) * 255.0 + 0.5);
1215 }
1216}
1217
1218
ee78dc32
GV
1219/* Decide if color named COLOR is valid for the display associated with
1220 the selected frame; if so, return the rgb values in COLOR_DEF.
1221 If ALLOC is nonzero, allocate a new colormap cell. */
1222
1223int
6fc2811b 1224w32_defined_color (f, color, color_def, alloc)
ee78dc32
GV
1225 FRAME_PTR f;
1226 char *color;
6fc2811b 1227 XColor *color_def;
ee78dc32
GV
1228 int alloc;
1229{
1230 register Lisp_Object tem;
6fc2811b 1231 COLORREF w32_color_ref;
3c190163 1232
fbd6baed 1233 tem = x_to_w32_color (color);
3c190163 1234
7d0393cf 1235 if (!NILP (tem))
ee78dc32 1236 {
d88c567c
JR
1237 if (f)
1238 {
1239 /* Apply gamma correction. */
1240 w32_color_ref = XUINT (tem);
1241 gamma_correct (f, &w32_color_ref);
1242 XSETINT (tem, w32_color_ref);
1243 }
9badad41
JR
1244
1245 /* Map this color to the palette if it is enabled. */
fbd6baed 1246 if (!NILP (Vw32_enable_palette))
5ac45f98 1247 {
fbd6baed 1248 struct w32_palette_entry * entry =
d88c567c 1249 one_w32_display_info.color_list;
fbd6baed 1250 struct w32_palette_entry ** prev =
d88c567c 1251 &one_w32_display_info.color_list;
7d0393cf 1252
5ac45f98
GV
1253 /* check if color is already mapped */
1254 while (entry)
1255 {
fbd6baed 1256 if (W32_COLOR (entry->entry) == XUINT (tem))
5ac45f98
GV
1257 break;
1258 prev = &entry->next;
1259 entry = entry->next;
1260 }
1261
1262 if (entry == NULL && alloc)
1263 {
1264 /* not already mapped, so add to list */
fbd6baed
GV
1265 entry = (struct w32_palette_entry *)
1266 xmalloc (sizeof (struct w32_palette_entry));
1267 SET_W32_COLOR (entry->entry, XUINT (tem));
5ac45f98
GV
1268 entry->next = NULL;
1269 *prev = entry;
d88c567c 1270 one_w32_display_info.num_colors++;
5ac45f98
GV
1271
1272 /* set flag that palette must be regenerated */
d88c567c 1273 one_w32_display_info.regen_palette = TRUE;
5ac45f98
GV
1274 }
1275 }
1276 /* Ensure COLORREF value is snapped to nearest color in (default)
1277 palette by simulating the PALETTERGB macro. This works whether
1278 or not the display device has a palette. */
6fc2811b
JR
1279 w32_color_ref = XUINT (tem) | 0x2000000;
1280
6fc2811b 1281 color_def->pixel = w32_color_ref;
197edd35
JR
1282 color_def->red = GetRValue (w32_color_ref) * 256;
1283 color_def->green = GetGValue (w32_color_ref) * 256;
1284 color_def->blue = GetBValue (w32_color_ref) * 256;
6fc2811b 1285
ee78dc32 1286 return 1;
5ac45f98 1287 }
7d0393cf 1288 else
3c190163
GV
1289 {
1290 return 0;
1291 }
ee78dc32
GV
1292}
1293
1294/* Given a string ARG naming a color, compute a pixel value from it
1295 suitable for screen F.
1296 If F is not a color screen, return DEF (default) regardless of what
1297 ARG says. */
1298
1299int
1300x_decode_color (f, arg, def)
1301 FRAME_PTR f;
1302 Lisp_Object arg;
1303 int def;
1304{
6fc2811b 1305 XColor cdef;
ee78dc32 1306
b7826503 1307 CHECK_STRING (arg);
ee78dc32 1308
d5db4077 1309 if (strcmp (SDATA (arg), "black") == 0)
ee78dc32 1310 return BLACK_PIX_DEFAULT (f);
d5db4077 1311 else if (strcmp (SDATA (arg), "white") == 0)
ee78dc32
GV
1312 return WHITE_PIX_DEFAULT (f);
1313
fbd6baed 1314 if ((FRAME_W32_DISPLAY_INFO (f)->n_planes * FRAME_W32_DISPLAY_INFO (f)->n_cbits) == 1)
ee78dc32
GV
1315 return def;
1316
6fc2811b 1317 /* w32_defined_color is responsible for coping with failures
ee78dc32 1318 by looking for a near-miss. */
d5db4077 1319 if (w32_defined_color (f, SDATA (arg), &cdef, 1))
6fc2811b 1320 return cdef.pixel;
ee78dc32
GV
1321
1322 /* defined_color failed; return an ultimate default. */
1323 return def;
1324}
1325\f
6fc2811b
JR
1326
1327
ee78dc32
GV
1328/* Functions called only from `x_set_frame_param'
1329 to set individual parameters.
1330
fbd6baed 1331 If FRAME_W32_WINDOW (f) is 0,
ee78dc32
GV
1332 the frame is being created and its window does not exist yet.
1333 In that case, just record the parameter's new value
1334 in the standard place; do not attempt to change the window. */
1335
1336void
1337x_set_foreground_color (f, arg, oldval)
1338 struct frame *f;
1339 Lisp_Object arg, oldval;
1340{
3cf3436e
JR
1341 struct w32_output *x = f->output_data.w32;
1342 PIX_TYPE fg, old_fg;
1343
1344 fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1345 old_fg = FRAME_FOREGROUND_PIXEL (f);
1346 FRAME_FOREGROUND_PIXEL (f) = fg;
5ac45f98 1347
fbd6baed 1348 if (FRAME_W32_WINDOW (f) != 0)
ee78dc32 1349 {
3cf3436e
JR
1350 if (x->cursor_pixel == old_fg)
1351 x->cursor_pixel = fg;
1352
6fc2811b 1353 update_face_from_frame_parameter (f, Qforeground_color, arg);
ee78dc32
GV
1354 if (FRAME_VISIBLE_P (f))
1355 redraw_frame (f);
1356 }
1357}
1358
1359void
1360x_set_background_color (f, arg, oldval)
1361 struct frame *f;
1362 Lisp_Object arg, oldval;
1363{
6fc2811b 1364 FRAME_BACKGROUND_PIXEL (f)
ee78dc32
GV
1365 = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
1366
fbd6baed 1367 if (FRAME_W32_WINDOW (f) != 0)
ee78dc32 1368 {
6fc2811b
JR
1369 SetWindowLong (FRAME_W32_WINDOW (f), WND_BACKGROUND_INDEX,
1370 FRAME_BACKGROUND_PIXEL (f));
ee78dc32 1371
6fc2811b 1372 update_face_from_frame_parameter (f, Qbackground_color, arg);
ee78dc32
GV
1373
1374 if (FRAME_VISIBLE_P (f))
1375 redraw_frame (f);
1376 }
1377}
1378
1379void
1380x_set_mouse_color (f, arg, oldval)
1381 struct frame *f;
1382 Lisp_Object arg, oldval;
1383{
7d63e5e3 1384 Cursor cursor, nontext_cursor, mode_cursor, hand_cursor;
dfc465d3 1385 int count;
ee78dc32
GV
1386 int mask_color;
1387
1388 if (!EQ (Qnil, arg))
fbd6baed 1389 f->output_data.w32->mouse_pixel
ee78dc32 1390 = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
6fc2811b
JR
1391 mask_color = FRAME_BACKGROUND_PIXEL (f);
1392
1393 /* Don't let pointers be invisible. */
fbd6baed 1394 if (mask_color == f->output_data.w32->mouse_pixel
6fc2811b
JR
1395 && mask_color == FRAME_BACKGROUND_PIXEL (f))
1396 f->output_data.w32->mouse_pixel = FRAME_FOREGROUND_PIXEL (f);
ee78dc32 1397
5f004711 1398#if 0 /* TODO : Mouse cursor customization. */
ee78dc32
GV
1399 BLOCK_INPUT;
1400
1401 /* It's not okay to crash if the user selects a screwy cursor. */
fadca6c6 1402 count = x_catch_errors (FRAME_W32_DISPLAY (f));
ee78dc32
GV
1403
1404 if (!EQ (Qnil, Vx_pointer_shape))
1405 {
b7826503 1406 CHECK_NUMBER (Vx_pointer_shape);
fbd6baed 1407 cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XINT (Vx_pointer_shape));
ee78dc32
GV
1408 }
1409 else
fbd6baed
GV
1410 cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
1411 x_check_errors (FRAME_W32_DISPLAY (f), "bad text pointer cursor: %s");
ee78dc32
GV
1412
1413 if (!EQ (Qnil, Vx_nontext_pointer_shape))
1414 {
b7826503 1415 CHECK_NUMBER (Vx_nontext_pointer_shape);
fbd6baed 1416 nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
ee78dc32
GV
1417 XINT (Vx_nontext_pointer_shape));
1418 }
1419 else
fbd6baed
GV
1420 nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_left_ptr);
1421 x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
ee78dc32 1422
0af913d7 1423 if (!EQ (Qnil, Vx_hourglass_pointer_shape))
6fc2811b 1424 {
b7826503 1425 CHECK_NUMBER (Vx_hourglass_pointer_shape);
0af913d7
GM
1426 hourglass_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1427 XINT (Vx_hourglass_pointer_shape));
6fc2811b
JR
1428 }
1429 else
0af913d7 1430 hourglass_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_watch);
6fc2811b 1431 x_check_errors (FRAME_W32_DISPLAY (f), "bad busy pointer cursor: %s");
7d0393cf 1432
6fc2811b 1433 x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
ee78dc32
GV
1434 if (!EQ (Qnil, Vx_mode_pointer_shape))
1435 {
b7826503 1436 CHECK_NUMBER (Vx_mode_pointer_shape);
fbd6baed 1437 mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
ee78dc32
GV
1438 XINT (Vx_mode_pointer_shape));
1439 }
1440 else
fbd6baed
GV
1441 mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
1442 x_check_errors (FRAME_W32_DISPLAY (f), "bad modeline pointer cursor: %s");
ee78dc32
GV
1443
1444 if (!EQ (Qnil, Vx_sensitive_text_pointer_shape))
1445 {
b7826503 1446 CHECK_NUMBER (Vx_sensitive_text_pointer_shape);
7d63e5e3 1447 hand_cursor
fbd6baed 1448 = XCreateFontCursor (FRAME_W32_DISPLAY (f),
ee78dc32
GV
1449 XINT (Vx_sensitive_text_pointer_shape));
1450 }
1451 else
7d63e5e3 1452 hand_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_crosshair);
ee78dc32 1453
4694d762
JR
1454 if (!NILP (Vx_window_horizontal_drag_shape))
1455 {
b7826503 1456 CHECK_NUMBER (Vx_window_horizontal_drag_shape);
4694d762
JR
1457 horizontal_drag_cursor
1458 = XCreateFontCursor (FRAME_X_DISPLAY (f),
1459 XINT (Vx_window_horizontal_drag_shape));
1460 }
1461 else
1462 horizontal_drag_cursor
1463 = XCreateFontCursor (FRAME_X_DISPLAY (f), XC_sb_h_double_arrow);
1464
ee78dc32 1465 /* Check and report errors with the above calls. */
fbd6baed 1466 x_check_errors (FRAME_W32_DISPLAY (f), "can't set cursor shape: %s");
fadca6c6 1467 x_uncatch_errors (FRAME_W32_DISPLAY (f), count);
ee78dc32
GV
1468
1469 {
1470 XColor fore_color, back_color;
1471
fbd6baed 1472 fore_color.pixel = f->output_data.w32->mouse_pixel;
ee78dc32 1473 back_color.pixel = mask_color;
fbd6baed
GV
1474 XQueryColor (FRAME_W32_DISPLAY (f),
1475 DefaultColormap (FRAME_W32_DISPLAY (f),
1476 DefaultScreen (FRAME_W32_DISPLAY (f))),
ee78dc32 1477 &fore_color);
fbd6baed
GV
1478 XQueryColor (FRAME_W32_DISPLAY (f),
1479 DefaultColormap (FRAME_W32_DISPLAY (f),
1480 DefaultScreen (FRAME_W32_DISPLAY (f))),
ee78dc32 1481 &back_color);
fbd6baed 1482 XRecolorCursor (FRAME_W32_DISPLAY (f), cursor,
ee78dc32 1483 &fore_color, &back_color);
fbd6baed 1484 XRecolorCursor (FRAME_W32_DISPLAY (f), nontext_cursor,
ee78dc32 1485 &fore_color, &back_color);
fbd6baed 1486 XRecolorCursor (FRAME_W32_DISPLAY (f), mode_cursor,
ee78dc32 1487 &fore_color, &back_color);
7d63e5e3 1488 XRecolorCursor (FRAME_W32_DISPLAY (f), hand_cursor,
ee78dc32 1489 &fore_color, &back_color);
0af913d7 1490 XRecolorCursor (FRAME_W32_DISPLAY (f), hourglass_cursor,
6fc2811b 1491 &fore_color, &back_color);
ee78dc32
GV
1492 }
1493
fbd6baed 1494 if (FRAME_W32_WINDOW (f) != 0)
6fc2811b 1495 XDefineCursor (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), cursor);
ee78dc32 1496
fbd6baed
GV
1497 if (cursor != f->output_data.w32->text_cursor && f->output_data.w32->text_cursor != 0)
1498 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->text_cursor);
1499 f->output_data.w32->text_cursor = cursor;
1500
1501 if (nontext_cursor != f->output_data.w32->nontext_cursor
1502 && f->output_data.w32->nontext_cursor != 0)
1503 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->nontext_cursor);
1504 f->output_data.w32->nontext_cursor = nontext_cursor;
1505
0af913d7
GM
1506 if (hourglass_cursor != f->output_data.w32->hourglass_cursor
1507 && f->output_data.w32->hourglass_cursor != 0)
1508 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->hourglass_cursor);
1509 f->output_data.w32->hourglass_cursor = hourglass_cursor;
6fc2811b 1510
fbd6baed
GV
1511 if (mode_cursor != f->output_data.w32->modeline_cursor
1512 && f->output_data.w32->modeline_cursor != 0)
1513 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->modeline_cursor);
1514 f->output_data.w32->modeline_cursor = mode_cursor;
7d0393cf 1515
7d63e5e3
KS
1516 if (hand_cursor != f->output_data.w32->hand_cursor
1517 && f->output_data.w32->hand_cursor != 0)
1518 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->hand_cursor);
1519 f->output_data.w32->hand_cursor = hand_cursor;
fbd6baed
GV
1520
1521 XFlush (FRAME_W32_DISPLAY (f));
ee78dc32 1522 UNBLOCK_INPUT;
6fc2811b
JR
1523
1524 update_face_from_frame_parameter (f, Qmouse_color, arg);
767b1ff0 1525#endif /* TODO */
ee78dc32
GV
1526}
1527
1528void
1529x_set_cursor_color (f, arg, oldval)
1530 struct frame *f;
1531 Lisp_Object arg, oldval;
1532{
70a0239a 1533 unsigned long fore_pixel, pixel;
ee78dc32 1534
dfff8a69 1535 if (!NILP (Vx_cursor_fore_pixel))
ee78dc32 1536 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
70a0239a 1537 WHITE_PIX_DEFAULT (f));
ee78dc32 1538 else
6fc2811b 1539 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
70a0239a 1540
6759f872 1541 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
7d0393cf 1542
ee78dc32 1543 /* Make sure that the cursor color differs from the background color. */
70a0239a 1544 if (pixel == FRAME_BACKGROUND_PIXEL (f))
ee78dc32 1545 {
70a0239a
JR
1546 pixel = f->output_data.w32->mouse_pixel;
1547 if (pixel == fore_pixel)
6fc2811b 1548 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
ee78dc32 1549 }
70a0239a 1550
ac849ba4 1551 f->output_data.w32->cursor_foreground_pixel = fore_pixel;
70a0239a 1552 f->output_data.w32->cursor_pixel = pixel;
ee78dc32 1553
fbd6baed 1554 if (FRAME_W32_WINDOW (f) != 0)
ee78dc32 1555 {
0327b4cc
JR
1556 BLOCK_INPUT;
1557 /* Update frame's cursor_gc. */
1558 f->output_data.w32->cursor_gc->foreground = fore_pixel;
1559 f->output_data.w32->cursor_gc->background = pixel;
1560
1561 UNBLOCK_INPUT;
1562
ee78dc32
GV
1563 if (FRAME_VISIBLE_P (f))
1564 {
70a0239a
JR
1565 x_update_cursor (f, 0);
1566 x_update_cursor (f, 1);
ee78dc32
GV
1567 }
1568 }
6fc2811b
JR
1569
1570 update_face_from_frame_parameter (f, Qcursor_color, arg);
ee78dc32
GV
1571}
1572
33d52f9c
GV
1573/* Set the border-color of frame F to pixel value PIX.
1574 Note that this does not fully take effect if done before
7d0393cf 1575 F has a window. */
6d906347 1576
33d52f9c
GV
1577void
1578x_set_border_pixel (f, pix)
1579 struct frame *f;
1580 int pix;
1581{
6d906347 1582
33d52f9c
GV
1583 f->output_data.w32->border_pixel = pix;
1584
be786000 1585 if (FRAME_W32_WINDOW (f) != 0 && f->border_width > 0)
33d52f9c
GV
1586 {
1587 if (FRAME_VISIBLE_P (f))
1588 redraw_frame (f);
1589 }
1590}
1591
ee78dc32
GV
1592/* Set the border-color of frame F to value described by ARG.
1593 ARG can be a string naming a color.
1594 The border-color is used for the border that is drawn by the server.
1595 Note that this does not fully take effect if done before
1596 F has a window; it must be redone when the window is created. */
1597
1598void
1599x_set_border_color (f, arg, oldval)
1600 struct frame *f;
1601 Lisp_Object arg, oldval;
1602{
ee78dc32
GV
1603 int pix;
1604
b7826503 1605 CHECK_STRING (arg);
ee78dc32 1606 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
ee78dc32 1607 x_set_border_pixel (f, pix);
6fc2811b 1608 update_face_from_frame_parameter (f, Qborder_color, arg);
ee78dc32
GV
1609}
1610
dfff8a69
JR
1611
1612void
1613x_set_cursor_type (f, arg, oldval)
1614 FRAME_PTR f;
1615 Lisp_Object arg, oldval;
1616{
50e363e6 1617 set_frame_cursor_types (f, arg);
ee78dc32 1618
623cdbf2 1619 /* Make sure the cursor gets redrawn. */
c922a224 1620 cursor_type_changed = 1;
ee78dc32 1621}
dfff8a69 1622\f
ee78dc32
GV
1623void
1624x_set_icon_type (f, arg, oldval)
1625 struct frame *f;
1626 Lisp_Object arg, oldval;
1627{
ee78dc32
GV
1628 int result;
1629
eb7576ce
GV
1630 if (NILP (arg) && NILP (oldval))
1631 return;
1632
7d0393cf 1633 if (STRINGP (arg) && STRINGP (oldval)
eb7576ce
GV
1634 && EQ (Fstring_equal (oldval, arg), Qt))
1635 return;
1636
1637 if (SYMBOLP (arg) && SYMBOLP (oldval) && EQ (arg, oldval))
ee78dc32
GV
1638 return;
1639
1640 BLOCK_INPUT;
ee78dc32 1641
eb7576ce 1642 result = x_bitmap_icon (f, arg);
ee78dc32
GV
1643 if (result)
1644 {
1645 UNBLOCK_INPUT;
1646 error ("No icon window available");
1647 }
1648
ee78dc32 1649 UNBLOCK_INPUT;
ee78dc32
GV
1650}
1651
ee78dc32
GV
1652void
1653x_set_icon_name (f, arg, oldval)
1654 struct frame *f;
1655 Lisp_Object arg, oldval;
1656{
ee78dc32
GV
1657 if (STRINGP (arg))
1658 {
1659 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1660 return;
1661 }
3f65d6f5 1662 else if (!NILP (arg) || NILP (oldval))
ee78dc32
GV
1663 return;
1664
1665 f->icon_name = arg;
1666
1667#if 0
fbd6baed 1668 if (f->output_data.w32->icon_bitmap != 0)
ee78dc32
GV
1669 return;
1670
1671 BLOCK_INPUT;
1672
1673 result = x_text_icon (f,
d5db4077
KR
1674 (char *) SDATA ((!NILP (f->icon_name)
1675 ? f->icon_name
1676 : !NILP (f->title)
1677 ? f->title
1678 : f->name)));
ee78dc32
GV
1679
1680 if (result)
1681 {
1682 UNBLOCK_INPUT;
1683 error ("No icon window available");
1684 }
1685
1686 /* If the window was unmapped (and its icon was mapped),
1687 the new icon is not mapped, so map the window in its stead. */
1688 if (FRAME_VISIBLE_P (f))
1689 {
1690#ifdef USE_X_TOOLKIT
fbd6baed 1691 XtPopup (f->output_data.w32->widget, XtGrabNone);
ee78dc32 1692#endif
fbd6baed 1693 XMapWindow (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f));
ee78dc32
GV
1694 }
1695
fbd6baed 1696 XFlush (FRAME_W32_DISPLAY (f));
ee78dc32
GV
1697 UNBLOCK_INPUT;
1698#endif
1699}
1700
a1258667 1701\f
ee78dc32
GV
1702void
1703x_set_menu_bar_lines (f, value, oldval)
1704 struct frame *f;
1705 Lisp_Object value, oldval;
1706{
1707 int nlines;
1708 int olines = FRAME_MENU_BAR_LINES (f);
1709
1710 /* Right now, menu bars don't work properly in minibuf-only frames;
1711 most of the commands try to apply themselves to the minibuffer
6fc2811b 1712 frame itself, and get an error because you can't switch buffers
ee78dc32
GV
1713 in or split the minibuffer window. */
1714 if (FRAME_MINIBUF_ONLY_P (f))
1715 return;
1716
1717 if (INTEGERP (value))
1718 nlines = XINT (value);
1719 else
1720 nlines = 0;
1721
1722 FRAME_MENU_BAR_LINES (f) = 0;
1723 if (nlines)
1724 FRAME_EXTERNAL_MENU_BAR (f) = 1;
1725 else
1726 {
1727 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
1728 free_frame_menubar (f);
1729 FRAME_EXTERNAL_MENU_BAR (f) = 0;
1edf84e7
GV
1730
1731 /* Adjust the frame size so that the client (text) dimensions
1732 remain the same. This depends on FRAME_EXTERNAL_MENU_BAR being
1733 set correctly. */
be786000 1734 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
6fc2811b 1735 do_pending_window_change (0);
ee78dc32 1736 }
6fc2811b
JR
1737 adjust_glyphs (f);
1738}
1739
1740
1741/* Set the number of lines used for the tool bar of frame F to VALUE.
1742 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
1743 is the old number of tool bar lines. This function changes the
1744 height of all windows on frame F to match the new tool bar height.
1745 The frame's height doesn't change. */
1746
1747void
1748x_set_tool_bar_lines (f, value, oldval)
1749 struct frame *f;
1750 Lisp_Object value, oldval;
1751{
36f8209a
JR
1752 int delta, nlines, root_height;
1753 Lisp_Object root_window;
6fc2811b 1754
dc220243
JR
1755 /* Treat tool bars like menu bars. */
1756 if (FRAME_MINIBUF_ONLY_P (f))
1757 return;
1758
6fc2811b
JR
1759 /* Use VALUE only if an integer >= 0. */
1760 if (INTEGERP (value) && XINT (value) >= 0)
1761 nlines = XFASTINT (value);
1762 else
1763 nlines = 0;
1764
1765 /* Make sure we redisplay all windows in this frame. */
1766 ++windows_or_buffers_changed;
1767
1768 delta = nlines - FRAME_TOOL_BAR_LINES (f);
36f8209a
JR
1769
1770 /* Don't resize the tool-bar to more than we have room for. */
1771 root_window = FRAME_ROOT_WINDOW (f);
be786000 1772 root_height = WINDOW_TOTAL_LINES (XWINDOW (root_window));
36f8209a
JR
1773 if (root_height - delta < 1)
1774 {
1775 delta = root_height - 1;
1776 nlines = FRAME_TOOL_BAR_LINES (f) + delta;
1777 }
1778
6fc2811b 1779 FRAME_TOOL_BAR_LINES (f) = nlines;
6d906347 1780 change_window_heights (root_window, delta);
6fc2811b 1781 adjust_glyphs (f);
36f8209a
JR
1782
1783 /* We also have to make sure that the internal border at the top of
1784 the frame, below the menu bar or tool bar, is redrawn when the
1785 tool bar disappears. This is so because the internal border is
1786 below the tool bar if one is displayed, but is below the menu bar
1787 if there isn't a tool bar. The tool bar draws into the area
1788 below the menu bar. */
1789 if (FRAME_W32_WINDOW (f) && FRAME_TOOL_BAR_LINES (f) == 0)
1790 {
2dc8b986 1791 clear_frame (f);
36f8209a 1792 clear_current_matrices (f);
36f8209a
JR
1793 }
1794
1795 /* If the tool bar gets smaller, the internal border below it
1796 has to be cleared. It was formerly part of the display
1797 of the larger tool bar, and updating windows won't clear it. */
1798 if (delta < 0)
1799 {
1800 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
be786000
KS
1801 int width = FRAME_PIXEL_WIDTH (f);
1802 int y = nlines * FRAME_LINE_HEIGHT (f);
36f8209a
JR
1803
1804 BLOCK_INPUT;
1805 {
1806 HDC hdc = get_frame_dc (f);
1807 w32_clear_area (f, hdc, 0, y, width, height);
1808 release_frame_dc (f, hdc);
1809 }
1810 UNBLOCK_INPUT;
3cf3436e
JR
1811
1812 if (WINDOWP (f->tool_bar_window))
1813 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
36f8209a 1814 }
ee78dc32
GV
1815}
1816
6fc2811b 1817
ee78dc32 1818/* Change the name of frame F to NAME. If NAME is nil, set F's name to
fbd6baed 1819 w32_id_name.
ee78dc32
GV
1820
1821 If EXPLICIT is non-zero, that indicates that lisp code is setting the
1822 name; if NAME is a string, set F's name to NAME and set
1823 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1824
1825 If EXPLICIT is zero, that indicates that Emacs redisplay code is
1826 suggesting a new name, which lisp code should override; if
1827 F->explicit_name is set, ignore the new name; otherwise, set it. */
1828
1829void
1830x_set_name (f, name, explicit)
1831 struct frame *f;
1832 Lisp_Object name;
1833 int explicit;
1834{
7d0393cf 1835 /* Make sure that requests from lisp code override requests from
ee78dc32
GV
1836 Emacs redisplay code. */
1837 if (explicit)
1838 {
1839 /* If we're switching from explicit to implicit, we had better
1840 update the mode lines and thereby update the title. */
1841 if (f->explicit_name && NILP (name))
1842 update_mode_lines = 1;
1843
1844 f->explicit_name = ! NILP (name);
1845 }
1846 else if (f->explicit_name)
1847 return;
1848
fbd6baed 1849 /* If NAME is nil, set the name to the w32_id_name. */
ee78dc32
GV
1850 if (NILP (name))
1851 {
1852 /* Check for no change needed in this very common case
1853 before we do any consing. */
fbd6baed 1854 if (!strcmp (FRAME_W32_DISPLAY_INFO (f)->w32_id_name,
d5db4077 1855 SDATA (f->name)))
ee78dc32 1856 return;
fbd6baed 1857 name = build_string (FRAME_W32_DISPLAY_INFO (f)->w32_id_name);
ee78dc32
GV
1858 }
1859 else
b7826503 1860 CHECK_STRING (name);
ee78dc32
GV
1861
1862 /* Don't change the name if it's already NAME. */
1863 if (! NILP (Fstring_equal (name, f->name)))
1864 return;
1865
1edf84e7
GV
1866 f->name = name;
1867
1868 /* For setting the frame title, the title parameter should override
1869 the name parameter. */
1870 if (! NILP (f->title))
1871 name = f->title;
1872
fbd6baed 1873 if (FRAME_W32_WINDOW (f))
ee78dc32 1874 {
6fc2811b 1875 if (STRING_MULTIBYTE (name))
dfff8a69 1876 name = ENCODE_SYSTEM (name);
6fc2811b 1877
ee78dc32 1878 BLOCK_INPUT;
74084731 1879 SetWindowText (FRAME_W32_WINDOW (f), SDATA (name));
ee78dc32
GV
1880 UNBLOCK_INPUT;
1881 }
ee78dc32
GV
1882}
1883
1884/* This function should be called when the user's lisp code has
1885 specified a name for the frame; the name will override any set by the
1886 redisplay code. */
1887void
1888x_explicitly_set_name (f, arg, oldval)
1889 FRAME_PTR f;
1890 Lisp_Object arg, oldval;
1891{
1892 x_set_name (f, arg, 1);
1893}
1894
1895/* This function should be called by Emacs redisplay code to set the
1896 name; names set this way will never override names set by the user's
1897 lisp code. */
1898void
1899x_implicitly_set_name (f, arg, oldval)
1900 FRAME_PTR f;
1901 Lisp_Object arg, oldval;
1902{
1903 x_set_name (f, arg, 0);
1904}
1edf84e7
GV
1905\f
1906/* Change the title of frame F to NAME.
40aa4c27 1907 If NAME is nil, use the frame name as the title. */
ee78dc32 1908
1edf84e7 1909void
6fc2811b 1910x_set_title (f, name, old_name)
1edf84e7 1911 struct frame *f;
6fc2811b 1912 Lisp_Object name, old_name;
1edf84e7
GV
1913{
1914 /* Don't change the title if it's already NAME. */
1915 if (EQ (name, f->title))
1916 return;
1917
1918 update_mode_lines = 1;
1919
1920 f->title = name;
1921
1922 if (NILP (name))
1923 name = f->name;
1924
1925 if (FRAME_W32_WINDOW (f))
1926 {
6fc2811b 1927 if (STRING_MULTIBYTE (name))
dfff8a69 1928 name = ENCODE_SYSTEM (name);
6fc2811b 1929
1edf84e7 1930 BLOCK_INPUT;
74084731 1931 SetWindowText (FRAME_W32_WINDOW (f), SDATA (name));
1edf84e7
GV
1932 UNBLOCK_INPUT;
1933 }
1934}
ee78dc32 1935
ee78dc32 1936
19f093e5 1937void x_set_scroll_bar_default_width (f)
ee78dc32 1938 struct frame *f;
ee78dc32 1939{
be786000 1940 int wid = FRAME_COLUMN_WIDTH (f);
6fc2811b 1941
be786000
KS
1942 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = GetSystemMetrics (SM_CXVSCROLL);
1943 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) +
1944 wid - 1) / wid;
ee78dc32 1945}
6d906347 1946
ee78dc32 1947\f
3faa984f 1948/* Subroutines for creating a frame. */
ee78dc32 1949
c9b2104d
JR
1950Cursor
1951w32_load_cursor (LPCTSTR name)
1952{
1953 /* Try first to load cursor from application resource. */
74084731 1954 Cursor cursor = LoadImage ((HINSTANCE) GetModuleHandle (NULL),
c9b2104d
JR
1955 name, IMAGE_CURSOR, 0, 0,
1956 LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_SHARED);
1957 if (!cursor)
1958 {
1959 /* Then try to load a shared predefined cursor. */
1960 cursor = LoadImage (NULL, name, IMAGE_CURSOR, 0, 0,
1961 LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_SHARED);
1962 }
1963 return cursor;
1964}
ee78dc32 1965
fbd6baed 1966extern LRESULT CALLBACK w32_wnd_proc ();
ee78dc32 1967
74084731 1968static BOOL
fbd6baed 1969w32_init_class (hinst)
ee78dc32
GV
1970 HINSTANCE hinst;
1971{
1972 WNDCLASS wc;
1973
5ac45f98 1974 wc.style = CS_HREDRAW | CS_VREDRAW;
fbd6baed 1975 wc.lpfnWndProc = (WNDPROC) w32_wnd_proc;
ee78dc32
GV
1976 wc.cbClsExtra = 0;
1977 wc.cbWndExtra = WND_EXTRA_BYTES;
1978 wc.hInstance = hinst;
1979 wc.hIcon = LoadIcon (hinst, EMACS_CLASS);
c9b2104d 1980 wc.hCursor = w32_load_cursor (IDC_ARROW);
4587b026 1981 wc.hbrBackground = NULL; /* GetStockObject (WHITE_BRUSH); */
ee78dc32
GV
1982 wc.lpszMenuName = NULL;
1983 wc.lpszClassName = EMACS_CLASS;
1984
1985 return (RegisterClass (&wc));
1986}
1987
74084731 1988static HWND
fbd6baed 1989w32_createscrollbar (f, bar)
ee78dc32
GV
1990 struct frame *f;
1991 struct scroll_bar * bar;
1992{
1993 return (CreateWindow ("SCROLLBAR", "", SBS_VERT | WS_CHILD | WS_VISIBLE,
1994 /* Position and size of scroll bar. */
74084731
JB
1995 XINT (bar->left) + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
1996 XINT (bar->top),
1997 XINT (bar->width) - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
1998 XINT (bar->height),
fbd6baed 1999 FRAME_W32_WINDOW (f),
ee78dc32
GV
2000 NULL,
2001 hinst,
2002 NULL));
2003}
2004
74084731 2005static void
fbd6baed 2006w32_createwindow (f)
ee78dc32
GV
2007 struct frame *f;
2008{
2009 HWND hwnd;
1edf84e7 2010 RECT rect;
df70725f
EZ
2011 Lisp_Object top = Qunbound;
2012 Lisp_Object left = Qunbound;
3faa984f 2013 struct w32_display_info *dpyinfo = &one_w32_display_info;
1edf84e7
GV
2014
2015 rect.left = rect.top = 0;
be786000
KS
2016 rect.right = FRAME_PIXEL_WIDTH (f);
2017 rect.bottom = FRAME_PIXEL_HEIGHT (f);
7d0393cf 2018
1edf84e7
GV
2019 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
2020 FRAME_EXTERNAL_MENU_BAR (f));
7d0393cf 2021
ee78dc32 2022 /* Do first time app init */
7d0393cf 2023
ee78dc32
GV
2024 if (!hprevinst)
2025 {
fbd6baed 2026 w32_init_class (hinst);
ee78dc32 2027 }
7d0393cf 2028
2770d589
JR
2029 if (f->size_hint_flags & USPosition || f->size_hint_flags & PPosition)
2030 {
2031 XSETINT (left, f->left_pos);
2032 XSETINT (top, f->top_pos);
2033 }
2034 else if (EQ (left, Qunbound) && EQ (top, Qunbound))
df70725f
EZ
2035 {
2036 /* When called with RES_TYPE_NUMBER, w32_get_arg will return zero
2037 for anything that is not a number and is not Qunbound. */
3faa984f
JR
2038 left = x_get_arg (dpyinfo, Qnil, Qleft, "left", "Left", RES_TYPE_NUMBER);
2039 top = x_get_arg (dpyinfo, Qnil, Qtop, "top", "Top", RES_TYPE_NUMBER);
df70725f 2040 }
48b62d10 2041
1edf84e7
GV
2042 FRAME_W32_WINDOW (f) = hwnd
2043 = CreateWindow (EMACS_CLASS,
2044 f->namebuf,
9ead1b60 2045 f->output_data.w32->dwStyle | WS_CLIPCHILDREN,
48b62d10
EZ
2046 EQ (left, Qunbound) ? CW_USEDEFAULT : XINT (left),
2047 EQ (top, Qunbound) ? CW_USEDEFAULT : XINT (top),
1edf84e7
GV
2048 rect.right - rect.left,
2049 rect.bottom - rect.top,
2050 NULL,
2051 NULL,
2052 hinst,
2053 NULL);
2054
ee78dc32
GV
2055 if (hwnd)
2056 {
be786000
KS
2057 SetWindowLong (hwnd, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
2058 SetWindowLong (hwnd, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
2059 SetWindowLong (hwnd, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
2060 SetWindowLong (hwnd, WND_SCROLLBAR_INDEX, f->scroll_bar_actual_width);
6fc2811b 2061 SetWindowLong (hwnd, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
ee78dc32 2062
cb9e33d4
RS
2063 /* Enable drag-n-drop. */
2064 DragAcceptFiles (hwnd, TRUE);
7d0393cf 2065
5ac45f98
GV
2066 /* Do this to discard the default setting specified by our parent. */
2067 ShowWindow (hwnd, SW_HIDE);
1771bb6e
EZ
2068
2069 /* Update frame positions. */
2070 GetWindowRect (hwnd, &rect);
2071 f->left_pos = rect.left;
2072 f->top_pos = rect.top;
3c190163 2073 }
3c190163
GV
2074}
2075
74084731 2076static void
ee78dc32 2077my_post_msg (wmsg, hwnd, msg, wParam, lParam)
fbd6baed 2078 W32Msg * wmsg;
ee78dc32
GV
2079 HWND hwnd;
2080 UINT msg;
2081 WPARAM wParam;
2082 LPARAM lParam;
2083{
2084 wmsg->msg.hwnd = hwnd;
2085 wmsg->msg.message = msg;
2086 wmsg->msg.wParam = wParam;
2087 wmsg->msg.lParam = lParam;
2088 wmsg->msg.time = GetMessageTime ();
2089
2090 post_msg (wmsg);
2091}
2092
e9e23e23 2093/* GetKeyState and MapVirtualKey on Windows 95 do not actually distinguish
a1a80b40
GV
2094 between left and right keys as advertised. We test for this
2095 support dynamically, and set a flag when the support is absent. If
2096 absent, we keep track of the left and right control and alt keys
2097 ourselves. This is particularly necessary on keyboards that rely
2098 upon the AltGr key, which is represented as having the left control
2099 and right alt keys pressed. For these keyboards, we need to know
2100 when the left alt key has been pressed in addition to the AltGr key
2101 so that we can properly support M-AltGr-key sequences (such as M-@
2102 on Swedish keyboards). */
2103
2104#define EMACS_LCONTROL 0
2105#define EMACS_RCONTROL 1
2106#define EMACS_LMENU 2
2107#define EMACS_RMENU 3
2108
2109static int modifiers[4];
2110static int modifiers_recorded;
2111static int modifier_key_support_tested;
2112
2113static void
2114test_modifier_support (unsigned int wparam)
2115{
2116 unsigned int l, r;
2117
2118 if (wparam != VK_CONTROL && wparam != VK_MENU)
2119 return;
2120 if (wparam == VK_CONTROL)
2121 {
2122 l = VK_LCONTROL;
2123 r = VK_RCONTROL;
2124 }
2125 else
2126 {
2127 l = VK_LMENU;
2128 r = VK_RMENU;
2129 }
2130 if (!(GetKeyState (l) & 0x8000) && !(GetKeyState (r) & 0x8000))
2131 modifiers_recorded = 1;
2132 else
2133 modifiers_recorded = 0;
2134 modifier_key_support_tested = 1;
2135}
2136
2137static void
2138record_keydown (unsigned int wparam, unsigned int lparam)
2139{
2140 int i;
2141
2142 if (!modifier_key_support_tested)
2143 test_modifier_support (wparam);
2144
2145 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
2146 return;
2147
2148 if (wparam == VK_CONTROL)
2149 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
2150 else
2151 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
2152
2153 modifiers[i] = 1;
2154}
2155
2156static void
2157record_keyup (unsigned int wparam, unsigned int lparam)
2158{
2159 int i;
2160
2161 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
2162 return;
2163
2164 if (wparam == VK_CONTROL)
2165 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
2166 else
2167 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
2168
2169 modifiers[i] = 0;
2170}
2171
da36a4d6 2172/* Emacs can lose focus while a modifier key has been pressed. When
7d0393cf 2173 it regains focus, be conservative and clear all modifiers since
da36a4d6
GV
2174 we cannot reconstruct the left and right modifier state. */
2175static void
2176reset_modifiers ()
2177{
8681157a
RS
2178 SHORT ctrl, alt;
2179
adcc3809
GV
2180 if (GetFocus () == NULL)
2181 /* Emacs doesn't have keyboard focus. Do nothing. */
da36a4d6 2182 return;
8681157a
RS
2183
2184 ctrl = GetAsyncKeyState (VK_CONTROL);
2185 alt = GetAsyncKeyState (VK_MENU);
2186
8681157a
RS
2187 if (!(ctrl & 0x08000))
2188 /* Clear any recorded control modifier state. */
2189 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
2190
2191 if (!(alt & 0x08000))
2192 /* Clear any recorded alt modifier state. */
2193 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
2194
adcc3809
GV
2195 /* Update the state of all modifier keys, because modifiers used in
2196 hot-key combinations can get stuck on if Emacs loses focus as a
2197 result of a hot-key being pressed. */
2198 {
2199 BYTE keystate[256];
2200
2201#define CURRENT_STATE(key) ((GetAsyncKeyState (key) & 0x8000) >> 8)
2202
2203 GetKeyboardState (keystate);
2204 keystate[VK_SHIFT] = CURRENT_STATE (VK_SHIFT);
2205 keystate[VK_CONTROL] = CURRENT_STATE (VK_CONTROL);
2206 keystate[VK_LCONTROL] = CURRENT_STATE (VK_LCONTROL);
2207 keystate[VK_RCONTROL] = CURRENT_STATE (VK_RCONTROL);
2208 keystate[VK_MENU] = CURRENT_STATE (VK_MENU);
2209 keystate[VK_LMENU] = CURRENT_STATE (VK_LMENU);
2210 keystate[VK_RMENU] = CURRENT_STATE (VK_RMENU);
2211 keystate[VK_LWIN] = CURRENT_STATE (VK_LWIN);
2212 keystate[VK_RWIN] = CURRENT_STATE (VK_RWIN);
2213 keystate[VK_APPS] = CURRENT_STATE (VK_APPS);
2214 SetKeyboardState (keystate);
2215 }
da36a4d6
GV
2216}
2217
7830e24b
RS
2218/* Synchronize modifier state with what is reported with the current
2219 keystroke. Even if we cannot distinguish between left and right
2220 modifier keys, we know that, if no modifiers are set, then neither
2221 the left or right modifier should be set. */
2222static void
2223sync_modifiers ()
2224{
2225 if (!modifiers_recorded)
2226 return;
2227
7d0393cf 2228 if (!(GetKeyState (VK_CONTROL) & 0x8000))
7830e24b
RS
2229 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
2230
7d0393cf 2231 if (!(GetKeyState (VK_MENU) & 0x8000))
7830e24b
RS
2232 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
2233}
2234
a1a80b40
GV
2235static int
2236modifier_set (int vkey)
2237{
ccc2d29c 2238 if (vkey == VK_CAPITAL || vkey == VK_SCROLL)
891560d6 2239 return (GetKeyState (vkey) & 0x1);
a1a80b40
GV
2240 if (!modifiers_recorded)
2241 return (GetKeyState (vkey) & 0x8000);
2242
2243 switch (vkey)
2244 {
2245 case VK_LCONTROL:
2246 return modifiers[EMACS_LCONTROL];
2247 case VK_RCONTROL:
2248 return modifiers[EMACS_RCONTROL];
2249 case VK_LMENU:
2250 return modifiers[EMACS_LMENU];
2251 case VK_RMENU:
2252 return modifiers[EMACS_RMENU];
a1a80b40
GV
2253 }
2254 return (GetKeyState (vkey) & 0x8000);
2255}
2256
ccc2d29c
GV
2257/* Convert between the modifier bits W32 uses and the modifier bits
2258 Emacs uses. */
2259
2260unsigned int
2261w32_key_to_modifier (int key)
2262{
2263 Lisp_Object key_mapping;
2264
2265 switch (key)
2266 {
2267 case VK_LWIN:
2268 key_mapping = Vw32_lwindow_modifier;
2269 break;
2270 case VK_RWIN:
2271 key_mapping = Vw32_rwindow_modifier;
2272 break;
2273 case VK_APPS:
2274 key_mapping = Vw32_apps_modifier;
2275 break;
2276 case VK_SCROLL:
2277 key_mapping = Vw32_scroll_lock_modifier;
2278 break;
2279 default:
2280 key_mapping = Qnil;
2281 }
2282
adcc3809
GV
2283 /* NB. This code runs in the input thread, asychronously to the lisp
2284 thread, so we must be careful to ensure access to lisp data is
2285 thread-safe. The following code is safe because the modifier
2286 variable values are updated atomically from lisp and symbols are
2287 not relocated by GC. Also, we don't have to worry about seeing GC
2288 markbits here. */
2289 if (EQ (key_mapping, Qhyper))
ccc2d29c 2290 return hyper_modifier;
adcc3809 2291 if (EQ (key_mapping, Qsuper))
ccc2d29c 2292 return super_modifier;
adcc3809 2293 if (EQ (key_mapping, Qmeta))
ccc2d29c 2294 return meta_modifier;
adcc3809 2295 if (EQ (key_mapping, Qalt))
ccc2d29c 2296 return alt_modifier;
adcc3809 2297 if (EQ (key_mapping, Qctrl))
ccc2d29c 2298 return ctrl_modifier;
adcc3809 2299 if (EQ (key_mapping, Qcontrol)) /* synonym for ctrl */
ccc2d29c 2300 return ctrl_modifier;
adcc3809 2301 if (EQ (key_mapping, Qshift))
ccc2d29c
GV
2302 return shift_modifier;
2303
2304 /* Don't generate any modifier if not explicitly requested. */
2305 return 0;
2306}
2307
74084731 2308static unsigned int
ccc2d29c
GV
2309w32_get_modifiers ()
2310{
2311 return ((modifier_set (VK_SHIFT) ? shift_modifier : 0) |
2312 (modifier_set (VK_CONTROL) ? ctrl_modifier : 0) |
2313 (modifier_set (VK_LWIN) ? w32_key_to_modifier (VK_LWIN) : 0) |
2314 (modifier_set (VK_RWIN) ? w32_key_to_modifier (VK_RWIN) : 0) |
2315 (modifier_set (VK_APPS) ? w32_key_to_modifier (VK_APPS) : 0) |
2316 (modifier_set (VK_SCROLL) ? w32_key_to_modifier (VK_SCROLL) : 0) |
2317 (modifier_set (VK_MENU) ?
2318 ((NILP (Vw32_alt_is_meta)) ? alt_modifier : meta_modifier) : 0));
2319}
2320
a1a80b40
GV
2321/* We map the VK_* modifiers into console modifier constants
2322 so that we can use the same routines to handle both console
2323 and window input. */
2324
2325static int
ccc2d29c 2326construct_console_modifiers ()
a1a80b40
GV
2327{
2328 int mods;
2329
a1a80b40
GV
2330 mods = 0;
2331 mods |= (modifier_set (VK_SHIFT)) ? SHIFT_PRESSED : 0;
2332 mods |= (modifier_set (VK_CAPITAL)) ? CAPSLOCK_ON : 0;
ccc2d29c
GV
2333 mods |= (modifier_set (VK_SCROLL)) ? SCROLLLOCK_ON : 0;
2334 mods |= (modifier_set (VK_NUMLOCK)) ? NUMLOCK_ON : 0;
a1a80b40
GV
2335 mods |= (modifier_set (VK_LCONTROL)) ? LEFT_CTRL_PRESSED : 0;
2336 mods |= (modifier_set (VK_RCONTROL)) ? RIGHT_CTRL_PRESSED : 0;
2337 mods |= (modifier_set (VK_LMENU)) ? LEFT_ALT_PRESSED : 0;
2338 mods |= (modifier_set (VK_RMENU)) ? RIGHT_ALT_PRESSED : 0;
ccc2d29c
GV
2339 mods |= (modifier_set (VK_LWIN)) ? LEFT_WIN_PRESSED : 0;
2340 mods |= (modifier_set (VK_RWIN)) ? RIGHT_WIN_PRESSED : 0;
2341 mods |= (modifier_set (VK_APPS)) ? APPS_PRESSED : 0;
a1a80b40
GV
2342
2343 return mods;
2344}
2345
ccc2d29c
GV
2346static int
2347w32_get_key_modifiers (unsigned int wparam, unsigned int lparam)
da36a4d6 2348{
ccc2d29c
GV
2349 int mods;
2350
2351 /* Convert to emacs modifiers. */
2352 mods = w32_kbd_mods_to_emacs (construct_console_modifiers (), wparam);
2353
2354 return mods;
2355}
da36a4d6 2356
ccc2d29c
GV
2357unsigned int
2358map_keypad_keys (unsigned int virt_key, unsigned int extended)
2359{
2360 if (virt_key < VK_CLEAR || virt_key > VK_DELETE)
2361 return virt_key;
da36a4d6 2362
ccc2d29c 2363 if (virt_key == VK_RETURN)
da36a4d6
GV
2364 return (extended ? VK_NUMPAD_ENTER : VK_RETURN);
2365
ccc2d29c
GV
2366 if (virt_key >= VK_PRIOR && virt_key <= VK_DOWN)
2367 return (!extended ? (VK_NUMPAD_PRIOR + (virt_key - VK_PRIOR)) : virt_key);
2368
2369 if (virt_key == VK_INSERT || virt_key == VK_DELETE)
2370 return (!extended ? (VK_NUMPAD_INSERT + (virt_key - VK_INSERT)) : virt_key);
2371
2372 if (virt_key == VK_CLEAR)
2373 return (!extended ? VK_NUMPAD_CLEAR : virt_key);
2374
2375 return virt_key;
2376}
2377
2378/* List of special key combinations which w32 would normally capture,
74084731 2379 but Emacs should grab instead. Not directly visible to lisp, to
ccc2d29c
GV
2380 simplify synchronization. Each item is an integer encoding a virtual
2381 key code and modifier combination to capture. */
74084731 2382static Lisp_Object w32_grabbed_keys;
ccc2d29c 2383
74084731 2384#define HOTKEY(vk, mods) make_number (((vk) & 255) | ((mods) << 8))
ccc2d29c
GV
2385#define HOTKEY_ID(k) (XFASTINT (k) & 0xbfff)
2386#define HOTKEY_VK_CODE(k) (XFASTINT (k) & 255)
2387#define HOTKEY_MODIFIERS(k) (XFASTINT (k) >> 8)
2388
2ba49441
JR
2389#define RAW_HOTKEY_ID(k) ((k) & 0xbfff)
2390#define RAW_HOTKEY_VK_CODE(k) ((k) & 255)
2391#define RAW_HOTKEY_MODIFIERS(k) ((k) >> 8)
2392
ccc2d29c
GV
2393/* Register hot-keys for reserved key combinations when Emacs has
2394 keyboard focus, since this is the only way Emacs can receive key
2395 combinations like Alt-Tab which are used by the system. */
2396
2397static void
2398register_hot_keys (hwnd)
2399 HWND hwnd;
2400{
2401 Lisp_Object keylist;
2402
8e50cc2d
SM
2403 /* Use CONSP, since we are called asynchronously. */
2404 for (keylist = w32_grabbed_keys; CONSP (keylist); keylist = XCDR (keylist))
ccc2d29c
GV
2405 {
2406 Lisp_Object key = XCAR (keylist);
2407
2408 /* Deleted entries get set to nil. */
2409 if (!INTEGERP (key))
2410 continue;
2411
2412 RegisterHotKey (hwnd, HOTKEY_ID (key),
2413 HOTKEY_MODIFIERS (key), HOTKEY_VK_CODE (key));
2414 }
2415}
2416
2417static void
2418unregister_hot_keys (hwnd)
2419 HWND hwnd;
2420{
2421 Lisp_Object keylist;
2422
8e50cc2d 2423 for (keylist = w32_grabbed_keys; CONSP (keylist); keylist = XCDR (keylist))
ccc2d29c
GV
2424 {
2425 Lisp_Object key = XCAR (keylist);
2426
2427 if (!INTEGERP (key))
2428 continue;
2429
2430 UnregisterHotKey (hwnd, HOTKEY_ID (key));
2431 }
2432}
2433
5ac45f98
GV
2434/* Main message dispatch loop. */
2435
1edf84e7
GV
2436static void
2437w32_msg_pump (deferred_msg * msg_buf)
5ac45f98
GV
2438{
2439 MSG msg;
ccc2d29c
GV
2440 int result;
2441 HWND focus_window;
93fbe8b7
GV
2442
2443 msh_mousewheel = RegisterWindowMessage (MSH_MOUSEWHEEL);
7d0393cf 2444
5ac45f98
GV
2445 while (GetMessage (&msg, NULL, 0, 0))
2446 {
2447 if (msg.hwnd == NULL)
2448 {
2449 switch (msg.message)
2450 {
3ef68e6b
AI
2451 case WM_NULL:
2452 /* Produced by complete_deferred_msg; just ignore. */
2453 break;
5ac45f98 2454 case WM_EMACS_CREATEWINDOW:
d5781bb6
JR
2455 /* Initialize COM for this window. Even though we don't use it,
2456 some third party shell extensions can cause it to be used in
2457 system dialogs, which causes a crash if it is not initialized.
2458 This is a known bug in Windows, which was fixed long ago, but
2459 the patch for XP is not publically available until XP SP3,
2460 and older versions will never be patched. */
2461 CoInitialize (NULL);
fbd6baed 2462 w32_createwindow ((struct frame *) msg.wParam);
1edf84e7
GV
2463 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2464 abort ();
5ac45f98 2465 break;
dfdb4047
GV
2466 case WM_EMACS_SETLOCALE:
2467 SetThreadLocale (msg.wParam);
2468 /* Reply is not expected. */
2469 break;
ccc2d29c
GV
2470 case WM_EMACS_SETKEYBOARDLAYOUT:
2471 result = (int) ActivateKeyboardLayout ((HKL) msg.wParam, 0);
2472 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
2473 result, 0))
2474 abort ();
2475 break;
2476 case WM_EMACS_REGISTER_HOT_KEY:
2477 focus_window = GetFocus ();
2478 if (focus_window != NULL)
2479 RegisterHotKey (focus_window,
2ba49441
JR
2480 RAW_HOTKEY_ID (msg.wParam),
2481 RAW_HOTKEY_MODIFIERS (msg.wParam),
2482 RAW_HOTKEY_VK_CODE (msg.wParam));
ccc2d29c
GV
2483 /* Reply is not expected. */
2484 break;
2485 case WM_EMACS_UNREGISTER_HOT_KEY:
2486 focus_window = GetFocus ();
2487 if (focus_window != NULL)
2ba49441 2488 UnregisterHotKey (focus_window, RAW_HOTKEY_ID (msg.wParam));
adcc3809
GV
2489 /* Mark item as erased. NB: this code must be
2490 thread-safe. The next line is okay because the cons
2491 cell is never made into garbage and is not relocated by
2492 GC. */
2ba49441 2493 XSETCAR ((Lisp_Object) ((EMACS_INT) msg.lParam), Qnil);
ccc2d29c
GV
2494 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2495 abort ();
2496 break;
adcc3809
GV
2497 case WM_EMACS_TOGGLE_LOCK_KEY:
2498 {
2499 int vk_code = (int) msg.wParam;
2500 int cur_state = (GetKeyState (vk_code) & 1);
2ba49441 2501 Lisp_Object new_state = (Lisp_Object) ((EMACS_INT) msg.lParam);
adcc3809
GV
2502
2503 /* NB: This code must be thread-safe. It is safe to
2504 call NILP because symbols are not relocated by GC,
2505 and pointer here is not touched by GC (so the markbit
2506 can't be set). Numbers are safe because they are
2507 immediate values. */
2508 if (NILP (new_state)
2509 || (NUMBERP (new_state)
8edb0a6f 2510 && ((XUINT (new_state)) & 1) != cur_state))
adcc3809
GV
2511 {
2512 one_w32_display_info.faked_key = vk_code;
2513
2514 keybd_event ((BYTE) vk_code,
2515 (BYTE) MapVirtualKey (vk_code, 0),
2516 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2517 keybd_event ((BYTE) vk_code,
2518 (BYTE) MapVirtualKey (vk_code, 0),
2519 KEYEVENTF_EXTENDEDKEY | 0, 0);
2520 keybd_event ((BYTE) vk_code,
2521 (BYTE) MapVirtualKey (vk_code, 0),
2522 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2523 cur_state = !cur_state;
2524 }
2525 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
2526 cur_state, 0))
2527 abort ();
2528 }
2529 break;
5dff811e
JR
2530#ifdef MSG_DEBUG
2531 /* Broadcast messages make it here, so you need to be looking
2532 for something in particular for this to be useful. */
1edf84e7 2533 default:
1edf84e7 2534 DebPrint (("msg %x not expected by w32_msg_pump\n", msg.message));
5dff811e 2535#endif
5ac45f98
GV
2536 }
2537 }
2538 else
2539 {
2540 DispatchMessage (&msg);
2541 }
1edf84e7
GV
2542
2543 /* Exit nested loop when our deferred message has completed. */
2544 if (msg_buf->completed)
2545 break;
5ac45f98 2546 }
1edf84e7
GV
2547}
2548
2549deferred_msg * deferred_msg_head;
2550
2551static deferred_msg *
2552find_deferred_msg (HWND hwnd, UINT msg)
2553{
2554 deferred_msg * item;
2555
2556 /* Don't actually need synchronization for read access, since
2557 modification of single pointer is always atomic. */
2558 /* enter_crit (); */
2559
2560 for (item = deferred_msg_head; item != NULL; item = item->next)
2561 if (item->w32msg.msg.hwnd == hwnd
2562 && item->w32msg.msg.message == msg)
2563 break;
2564
2565 /* leave_crit (); */
2566
2567 return item;
2568}
2569
2570static LRESULT
2571send_deferred_msg (deferred_msg * msg_buf,
2572 HWND hwnd,
2573 UINT msg,
2574 WPARAM wParam,
2575 LPARAM lParam)
2576{
2577 /* Only input thread can send deferred messages. */
2578 if (GetCurrentThreadId () != dwWindowsThreadId)
2579 abort ();
2580
2581 /* It is an error to send a message that is already deferred. */
2582 if (find_deferred_msg (hwnd, msg) != NULL)
2583 abort ();
2584
2585 /* Enforced synchronization is not needed because this is the only
2586 function that alters deferred_msg_head, and the following critical
2587 section is guaranteed to only be serially reentered (since only the
2588 input thread can call us). */
2589
2590 /* enter_crit (); */
2591
2592 msg_buf->completed = 0;
2593 msg_buf->next = deferred_msg_head;
2594 deferred_msg_head = msg_buf;
2595 my_post_msg (&msg_buf->w32msg, hwnd, msg, wParam, lParam);
2596
2597 /* leave_crit (); */
2598
2599 /* Start a new nested message loop to process other messages until
2600 this one is completed. */
2601 w32_msg_pump (msg_buf);
2602
2603 deferred_msg_head = msg_buf->next;
2604
2605 return msg_buf->result;
2606}
2607
2608void
2609complete_deferred_msg (HWND hwnd, UINT msg, LRESULT result)
2610{
2611 deferred_msg * msg_buf = find_deferred_msg (hwnd, msg);
2612
2613 if (msg_buf == NULL)
74084731 2614 /* Message may have been cancelled, so don't abort. */
3ef68e6b 2615 return;
1edf84e7
GV
2616
2617 msg_buf->result = result;
2618 msg_buf->completed = 1;
2619
2620 /* Ensure input thread is woken so it notices the completion. */
2621 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
2622}
2623
74084731 2624static void
3ef68e6b
AI
2625cancel_all_deferred_msgs ()
2626{
2627 deferred_msg * item;
2628
2629 /* Don't actually need synchronization for read access, since
2630 modification of single pointer is always atomic. */
2631 /* enter_crit (); */
2632
2633 for (item = deferred_msg_head; item != NULL; item = item->next)
2634 {
2635 item->result = 0;
2636 item->completed = 1;
2637 }
2638
2639 /* leave_crit (); */
2640
2641 /* Ensure input thread is woken so it notices the completion. */
2642 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
2643}
1edf84e7 2644
992dfd90
JR
2645DWORD WINAPI
2646w32_msg_worker (void *arg)
1edf84e7
GV
2647{
2648 MSG msg;
2649 deferred_msg dummy_buf;
2650
2651 /* Ensure our message queue is created */
7d0393cf 2652
1edf84e7 2653 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
7d0393cf 2654
1edf84e7
GV
2655 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2656 abort ();
2657
2658 memset (&dummy_buf, 0, sizeof (dummy_buf));
2659 dummy_buf.w32msg.msg.hwnd = NULL;
2660 dummy_buf.w32msg.msg.message = WM_NULL;
2661
23f250f4 2662 /* This is the initial message loop which should only exit when the
1edf84e7
GV
2663 application quits. */
2664 w32_msg_pump (&dummy_buf);
2665
2666 return 0;
5ac45f98
GV
2667}
2668
f0c947b5
JR
2669static void
2670signal_user_input ()
2671{
2672 /* Interrupt any lisp that wants to be interrupted by input. */
2673 if (!NILP (Vthrow_on_input))
2674 {
2675 Vquit_flag = Vthrow_on_input;
2676 /* If we're inside a function that wants immediate quits,
2677 do it now. */
2678 if (immediate_quit && NILP (Vinhibit_quit))
2679 {
2680 immediate_quit = 0;
2681 QUIT;
2682 }
2683 }
2684}
2685
2686
3ef68e6b
AI
2687static void
2688post_character_message (hwnd, msg, wParam, lParam, modifiers)
2689 HWND hwnd;
2690 UINT msg;
2691 WPARAM wParam;
2692 LPARAM lParam;
2693 DWORD modifiers;
2694
2695{
2696 W32Msg wmsg;
2697
2698 wmsg.dwModifiers = modifiers;
2699
2700 /* Detect quit_char and set quit-flag directly. Note that we
2701 still need to post a message to ensure the main thread will be
74084731 2702 woken up if blocked in sys_select, but we do NOT want to post
3ef68e6b
AI
2703 the quit_char message itself (because it will usually be as if
2704 the user had typed quit_char twice). Instead, we post a dummy
2705 message that has no particular effect. */
2706 {
2707 int c = wParam;
2708 if (isalpha (c) && wmsg.dwModifiers == ctrl_modifier)
2709 c = make_ctrl_char (c) & 0377;
7d081355
AI
2710 if (c == quit_char
2711 || (wmsg.dwModifiers == 0 &&
2ba49441 2712 w32_quit_key && wParam == w32_quit_key))
3ef68e6b
AI
2713 {
2714 Vquit_flag = Qt;
2715
2716 /* The choice of message is somewhat arbitrary, as long as
2717 the main thread handler just ignores it. */
2718 msg = WM_NULL;
2719
2720 /* Interrupt any blocking system calls. */
2721 signal_quit ();
2722
2723 /* As a safety precaution, forcibly complete any deferred
2724 messages. This is a kludge, but I don't see any particularly
2725 clean way to handle the situation where a deferred message is
2726 "dropped" in the lisp thread, and will thus never be
2727 completed, eg. by the user trying to activate the menubar
2728 when the lisp thread is busy, and then typing C-g when the
2729 menubar doesn't open promptly (with the result that the
2730 menubar never responds at all because the deferred
2731 WM_INITMENU message is never completed). Another problem
2732 situation is when the lisp thread calls SendMessage (to send
2733 a window manager command) when a message has been deferred;
2734 the lisp thread gets blocked indefinitely waiting for the
2735 deferred message to be completed, which itself is waiting for
2736 the lisp thread to respond.
2737
2738 Note that we don't want to block the input thread waiting for
2739 a reponse from the lisp thread (although that would at least
2740 solve the deadlock problem above), because we want to be able
2741 to receive C-g to interrupt the lisp thread. */
2742 cancel_all_deferred_msgs ();
2743 }
f0c947b5
JR
2744 else
2745 signal_user_input ();
3ef68e6b
AI
2746 }
2747
2748 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2749}
2750
ee78dc32
GV
2751/* Main window procedure */
2752
7d0393cf 2753LRESULT CALLBACK
fbd6baed 2754w32_wnd_proc (hwnd, msg, wParam, lParam)
ee78dc32
GV
2755 HWND hwnd;
2756 UINT msg;
2757 WPARAM wParam;
2758 LPARAM lParam;
2759{
2760 struct frame *f;
fbd6baed
GV
2761 struct w32_display_info *dpyinfo = &one_w32_display_info;
2762 W32Msg wmsg;
84fb1139 2763 int windows_translate;
576ba81c 2764 int key;
84fb1139 2765
a6085637
KH
2766 /* Note that it is okay to call x_window_to_frame, even though we are
2767 not running in the main lisp thread, because frame deletion
2768 requires the lisp thread to synchronize with this thread. Thus, if
2769 a frame struct is returned, it can be used without concern that the
2770 lisp thread might make it disappear while we are using it.
2771
2772 NB. Walking the frame list in this thread is safe (as long as
2773 writes of Lisp_Object slots are atomic, which they are on Windows).
2774 Although delete-frame can destructively modify the frame list while
2775 we are walking it, a garbage collection cannot occur until after
2776 delete-frame has synchronized with this thread.
2777
2778 It is also safe to use functions that make GDI calls, such as
fbd6baed 2779 w32_clear_rect, because these functions must obtain a DC handle
a6085637
KH
2780 from the frame struct using get_frame_dc which is thread-aware. */
2781
7d0393cf 2782 switch (msg)
ee78dc32
GV
2783 {
2784 case WM_ERASEBKGND:
a6085637
KH
2785 f = x_window_to_frame (dpyinfo, hwnd);
2786 if (f)
2787 {
9badad41 2788 HDC hdc = get_frame_dc (f);
a6085637 2789 GetUpdateRect (hwnd, &wmsg.rect, FALSE);
9badad41
JR
2790 w32_clear_rect (f, hdc, &wmsg.rect);
2791 release_frame_dc (f, hdc);
ce6059da
AI
2792
2793#if defined (W32_DEBUG_DISPLAY)
18f0b342
AI
2794 DebPrint (("WM_ERASEBKGND (frame %p): erasing %d,%d-%d,%d\n",
2795 f,
2796 wmsg.rect.left, wmsg.rect.top,
2797 wmsg.rect.right, wmsg.rect.bottom));
ce6059da 2798#endif /* W32_DEBUG_DISPLAY */
a6085637 2799 }
5ac45f98
GV
2800 return 1;
2801 case WM_PALETTECHANGED:
2802 /* ignore our own changes */
2803 if ((HWND)wParam != hwnd)
2804 {
a6085637
KH
2805 f = x_window_to_frame (dpyinfo, hwnd);
2806 if (f)
2807 /* get_frame_dc will realize our palette and force all
2808 frames to be redrawn if needed. */
2809 release_frame_dc (f, get_frame_dc (f));
5ac45f98
GV
2810 }
2811 return 0;
ee78dc32 2812 case WM_PAINT:
ce6059da 2813 {
55dcfc15
AI
2814 PAINTSTRUCT paintStruct;
2815 RECT update_rect;
aa35b6ad 2816 bzero (&update_rect, sizeof (update_rect));
55dcfc15 2817
18f0b342
AI
2818 f = x_window_to_frame (dpyinfo, hwnd);
2819 if (f == 0)
2820 {
2821 DebPrint (("WM_PAINT received for unknown window %p\n", hwnd));
2822 return 0;
2823 }
2824
55dcfc15
AI
2825 /* MSDN Docs say not to call BeginPaint if GetUpdateRect
2826 fails. Apparently this can happen under some
2827 circumstances. */
aa35b6ad 2828 if (GetUpdateRect (hwnd, &update_rect, FALSE) || !w32_strict_painting)
55dcfc15
AI
2829 {
2830 enter_crit ();
2831 BeginPaint (hwnd, &paintStruct);
2832
aa35b6ad
JR
2833 /* The rectangles returned by GetUpdateRect and BeginPaint
2834 do not always match. Play it safe by assuming both areas
2835 are invalid. */
2836 UnionRect (&(wmsg.rect), &update_rect, &(paintStruct.rcPaint));
55dcfc15
AI
2837
2838#if defined (W32_DEBUG_DISPLAY)
18f0b342
AI
2839 DebPrint (("WM_PAINT (frame %p): painting %d,%d-%d,%d\n",
2840 f,
2841 wmsg.rect.left, wmsg.rect.top,
2842 wmsg.rect.right, wmsg.rect.bottom));
2843 DebPrint ((" [update region is %d,%d-%d,%d]\n",
55dcfc15
AI
2844 update_rect.left, update_rect.top,
2845 update_rect.right, update_rect.bottom));
2846#endif
2847 EndPaint (hwnd, &paintStruct);
2848 leave_crit ();
2849
f7b146dc
JR
2850 /* Change the message type to prevent Windows from
2851 combining WM_PAINT messages in the Lisp thread's queue,
2852 since Windows assumes that each message queue is
2853 dedicated to one frame and does not bother checking
2854 that hwnd matches before combining them. */
2855 my_post_msg (&wmsg, hwnd, WM_EMACS_PAINT, wParam, lParam);
7d0393cf 2856
55dcfc15
AI
2857 return 0;
2858 }
c0611964
AI
2859
2860 /* If GetUpdateRect returns 0 (meaning there is no update
2861 region), assume the whole window needs to be repainted. */
74084731 2862 GetClientRect (hwnd, &wmsg.rect);
c0611964
AI
2863 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2864 return 0;
ee78dc32 2865 }
a1a80b40 2866
ccc2d29c
GV
2867 case WM_INPUTLANGCHANGE:
2868 /* Inform lisp thread of keyboard layout changes. */
2869 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2870
2871 /* Clear dead keys in the keyboard state; for simplicity only
2872 preserve modifier key states. */
2873 {
2874 int i;
2875 BYTE keystate[256];
2876
2877 GetKeyboardState (keystate);
2878 for (i = 0; i < 256; i++)
2879 if (1
2880 && i != VK_SHIFT
2881 && i != VK_LSHIFT
2882 && i != VK_RSHIFT
2883 && i != VK_CAPITAL
2884 && i != VK_NUMLOCK
2885 && i != VK_SCROLL
2886 && i != VK_CONTROL
2887 && i != VK_LCONTROL
2888 && i != VK_RCONTROL
2889 && i != VK_MENU
2890 && i != VK_LMENU
2891 && i != VK_RMENU
2892 && i != VK_LWIN
2893 && i != VK_RWIN)
2894 keystate[i] = 0;
2895 SetKeyboardState (keystate);
2896 }
2897 goto dflt;
2898
2899 case WM_HOTKEY:
2900 /* Synchronize hot keys with normal input. */
2901 PostMessage (hwnd, WM_KEYDOWN, HIWORD (lParam), 0);
2902 return (0);
2903
a1a80b40
GV
2904 case WM_KEYUP:
2905 case WM_SYSKEYUP:
2906 record_keyup (wParam, lParam);
2907 goto dflt;
2908
ee78dc32
GV
2909 case WM_KEYDOWN:
2910 case WM_SYSKEYDOWN:
ccc2d29c
GV
2911 /* Ignore keystrokes we fake ourself; see below. */
2912 if (dpyinfo->faked_key == wParam)
2913 {
2914 dpyinfo->faked_key = 0;
576ba81c
AI
2915 /* Make sure TranslateMessage sees them though (as long as
2916 they don't produce WM_CHAR messages). This ensures that
2917 indicator lights are toggled promptly on Windows 9x, for
2918 example. */
bf254037 2919 if (wParam < 256 && lispy_function_keys[wParam])
576ba81c
AI
2920 {
2921 windows_translate = 1;
2922 goto translate;
2923 }
2924 return 0;
ccc2d29c
GV
2925 }
2926
7830e24b
RS
2927 /* Synchronize modifiers with current keystroke. */
2928 sync_modifiers ();
a1a80b40 2929 record_keydown (wParam, lParam);
ccc2d29c 2930 wParam = map_keypad_keys (wParam, (lParam & 0x1000000L) != 0);
84fb1139
KH
2931
2932 windows_translate = 0;
ccc2d29c
GV
2933
2934 switch (wParam)
2935 {
2936 case VK_LWIN:
2937 if (NILP (Vw32_pass_lwindow_to_system))
2938 {
2939 /* Prevent system from acting on keyup (which opens the
2940 Start menu if no other key was pressed) by simulating a
2941 press of Space which we will ignore. */
2942 if (GetAsyncKeyState (wParam) & 1)
2943 {
adcc3809 2944 if (NUMBERP (Vw32_phantom_key_code))
576ba81c 2945 key = XUINT (Vw32_phantom_key_code) & 255;
adcc3809 2946 else
576ba81c
AI
2947 key = VK_SPACE;
2948 dpyinfo->faked_key = key;
2949 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
ccc2d29c
GV
2950 }
2951 }
2952 if (!NILP (Vw32_lwindow_modifier))
2953 return 0;
2954 break;
2955 case VK_RWIN:
2956 if (NILP (Vw32_pass_rwindow_to_system))
2957 {
2958 if (GetAsyncKeyState (wParam) & 1)
2959 {
adcc3809 2960 if (NUMBERP (Vw32_phantom_key_code))
576ba81c 2961 key = XUINT (Vw32_phantom_key_code) & 255;
adcc3809 2962 else
576ba81c
AI
2963 key = VK_SPACE;
2964 dpyinfo->faked_key = key;
2965 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
ccc2d29c
GV
2966 }
2967 }
2968 if (!NILP (Vw32_rwindow_modifier))
2969 return 0;
2970 break;
576ba81c 2971 case VK_APPS:
ccc2d29c
GV
2972 if (!NILP (Vw32_apps_modifier))
2973 return 0;
2974 break;
2975 case VK_MENU:
7d0393cf 2976 if (NILP (Vw32_pass_alt_to_system))
adcc3809
GV
2977 /* Prevent DefWindowProc from activating the menu bar if an
2978 Alt key is pressed and released by itself. */
ccc2d29c 2979 return 0;
84fb1139 2980 windows_translate = 1;
ccc2d29c 2981 break;
7d0393cf 2982 case VK_CAPITAL:
ccc2d29c
GV
2983 /* Decide whether to treat as modifier or function key. */
2984 if (NILP (Vw32_enable_caps_lock))
2985 goto disable_lock_key;
adcc3809
GV
2986 windows_translate = 1;
2987 break;
ccc2d29c
GV
2988 case VK_NUMLOCK:
2989 /* Decide whether to treat as modifier or function key. */
2990 if (NILP (Vw32_enable_num_lock))
2991 goto disable_lock_key;
adcc3809
GV
2992 windows_translate = 1;
2993 break;
ccc2d29c
GV
2994 case VK_SCROLL:
2995 /* Decide whether to treat as modifier or function key. */
2996 if (NILP (Vw32_scroll_lock_modifier))
2997 goto disable_lock_key;
adcc3809
GV
2998 windows_translate = 1;
2999 break;
ccc2d29c 3000 disable_lock_key:
adcc3809
GV
3001 /* Ensure the appropriate lock key state (and indicator light)
3002 remains in the same state. We do this by faking another
3003 press of the relevant key. Apparently, this really is the
3004 only way to toggle the state of the indicator lights. */
3005 dpyinfo->faked_key = wParam;
3006 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
3007 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
3008 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
3009 KEYEVENTF_EXTENDEDKEY | 0, 0);
3010 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
3011 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
3012 /* Ensure indicator lights are updated promptly on Windows 9x
3013 (TranslateMessage apparently does this), after forwarding
3014 input event. */
3015 post_character_message (hwnd, msg, wParam, lParam,
3016 w32_get_key_modifiers (wParam, lParam));
3017 windows_translate = 1;
ccc2d29c 3018 break;
7d0393cf 3019 case VK_CONTROL:
ccc2d29c
GV
3020 case VK_SHIFT:
3021 case VK_PROCESSKEY: /* Generated by IME. */
3022 windows_translate = 1;
3023 break;
adcc3809
GV
3024 case VK_CANCEL:
3025 /* Windows maps Ctrl-Pause (aka Ctrl-Break) into VK_CANCEL,
3026 which is confusing for purposes of key binding; convert
3027 VK_CANCEL events into VK_PAUSE events. */
3028 wParam = VK_PAUSE;
3029 break;
3030 case VK_PAUSE:
3031 /* Windows maps Ctrl-NumLock into VK_PAUSE, which is confusing
3032 for purposes of key binding; convert these back into
3033 VK_NUMLOCK events, at least when we want to see NumLock key
3034 presses. (Note that there is never any possibility that
3035 VK_PAUSE with Ctrl really is C-Pause as per above.) */
3036 if (NILP (Vw32_enable_num_lock) && modifier_set (VK_CONTROL))
3037 wParam = VK_NUMLOCK;
3038 break;
ccc2d29c
GV
3039 default:
3040 /* If not defined as a function key, change it to a WM_CHAR message. */
bf254037 3041 if (wParam > 255 || !lispy_function_keys[wParam])
ccc2d29c 3042 {
adcc3809
GV
3043 DWORD modifiers = construct_console_modifiers ();
3044
ccc2d29c
GV
3045 if (!NILP (Vw32_recognize_altgr)
3046 && modifier_set (VK_LCONTROL) && modifier_set (VK_RMENU))
3047 {
3048 /* Always let TranslateMessage handle AltGr key chords;
3049 for some reason, ToAscii doesn't always process AltGr
3050 chords correctly. */
3051 windows_translate = 1;
3052 }
adcc3809 3053 else if ((modifiers & (~SHIFT_PRESSED & ~CAPSLOCK_ON)) != 0)
ccc2d29c 3054 {
adcc3809
GV
3055 /* Handle key chords including any modifiers other
3056 than shift directly, in order to preserve as much
3057 modifier information as possible. */
ccc2d29c
GV
3058 if ('A' <= wParam && wParam <= 'Z')
3059 {
3060 /* Don't translate modified alphabetic keystrokes,
3061 so the user doesn't need to constantly switch
3062 layout to type control or meta keystrokes when
3063 the normal layout translates alphabetic
3064 characters to non-ascii characters. */
3065 if (!modifier_set (VK_SHIFT))
3066 wParam += ('a' - 'A');
3067 msg = WM_CHAR;
3068 }
3069 else
3070 {
3071 /* Try to handle other keystrokes by determining the
3072 base character (ie. translating the base key plus
3073 shift modifier). */
3074 int add;
3075 int isdead = 0;
3076 KEY_EVENT_RECORD key;
7d0393cf 3077
ccc2d29c
GV
3078 key.bKeyDown = TRUE;
3079 key.wRepeatCount = 1;
3080 key.wVirtualKeyCode = wParam;
3081 key.wVirtualScanCode = (lParam & 0xFF0000) >> 16;
3082 key.uChar.AsciiChar = 0;
adcc3809 3083 key.dwControlKeyState = modifiers;
ccc2d29c
GV
3084
3085 add = w32_kbd_patch_key (&key);
3086 /* 0 means an unrecognised keycode, negative means
3087 dead key. Ignore both. */
3088 while (--add >= 0)
3089 {
3090 /* Forward asciified character sequence. */
3091 post_character_message
a313b291
JR
3092 (hwnd, WM_CHAR,
3093 (unsigned char) key.uChar.AsciiChar, lParam,
ccc2d29c
GV
3094 w32_get_key_modifiers (wParam, lParam));
3095 w32_kbd_patch_key (&key);
3096 }
3097 return 0;
3098 }
3099 }
3100 else
3101 {
3102 /* Let TranslateMessage handle everything else. */
3103 windows_translate = 1;
3104 }
3105 }
3106 }
a1a80b40 3107
adcc3809 3108 translate:
84fb1139
KH
3109 if (windows_translate)
3110 {
e9e23e23 3111 MSG windows_msg = { hwnd, msg, wParam, lParam, 0, {0,0} };
e9e23e23
GV
3112 windows_msg.time = GetMessageTime ();
3113 TranslateMessage (&windows_msg);
84fb1139
KH
3114 goto dflt;
3115 }
3116
ee78dc32 3117 /* Fall through */
7d0393cf 3118
ee78dc32
GV
3119 case WM_SYSCHAR:
3120 case WM_CHAR:
ccc2d29c
GV
3121 post_character_message (hwnd, msg, wParam, lParam,
3122 w32_get_key_modifiers (wParam, lParam));
ee78dc32 3123 break;
da36a4d6 3124
820eff5a
JR
3125 case WM_UNICHAR:
3126 /* WM_UNICHAR looks promising from the docs, but the exact
3127 circumstances in which TranslateMessage sends it is one of those
3128 Microsoft secret API things that EU and US courts are supposed
3129 to have put a stop to already. Spy++ shows it being sent to Notepad
3130 and other MS apps, but never to Emacs.
3131
3132 Some third party IMEs send it in accordance with the official
3133 documentation though, so handle it here.
3134
3135 UNICODE_NOCHAR is used to test for support for this message.
3136 TRUE indicates that the message is supported. */
3137 if (wParam == UNICODE_NOCHAR)
3138 return TRUE;
3139
3140 {
3141 W32Msg wmsg;
3142 wmsg.dwModifiers = w32_get_key_modifiers (wParam, lParam);
3143 signal_user_input ();
3144 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3145 }
3146 break;
3147
3148 case WM_IME_CHAR:
3149 /* If we can't get the IME result as unicode, use default processing,
3150 which will at least allow characters decodable in the system locale
3151 get through. */
3152 if (!get_composition_string_fn)
3153 goto dflt;
3154
3155 else if (!ignore_ime_char)
3156 {
3157 wchar_t * buffer;
3158 int size, i;
3159 W32Msg wmsg;
3160 HIMC context = get_ime_context_fn (hwnd);
3161 wmsg.dwModifiers = w32_get_key_modifiers (wParam, lParam);
3162 /* Get buffer size. */
3163 size = get_composition_string_fn (context, GCS_RESULTSTR, buffer, 0);
3164 buffer = alloca(size);
3165 size = get_composition_string_fn (context, GCS_RESULTSTR,
3166 buffer, size);
c902b920
JR
3167 release_ime_context_fn (hwnd, context);
3168
820eff5a
JR
3169 signal_user_input ();
3170 for (i = 0; i < size / sizeof (wchar_t); i++)
3171 {
3172 my_post_msg (&wmsg, hwnd, WM_UNICHAR, (WPARAM) buffer[i],
3173 lParam);
3174 }
2c93b248
JR
3175 /* Ignore the messages for the rest of the
3176 characters in the string that was output above. */
3177 ignore_ime_char = (size / sizeof (wchar_t)) - 1;
820eff5a 3178 }
2c93b248
JR
3179 else
3180 ignore_ime_char--;
3181
820eff5a
JR
3182 break;
3183
c902b920
JR
3184 case WM_IME_STARTCOMPOSITION:
3185 if (!set_ime_composition_window_fn)
3186 goto dflt;
3187 else
3188 {
3189 COMPOSITIONFORM form;
3190 HIMC context;
3191 struct window *w;
3192
3193 if (!context)
3194 break;
3195
3196 f = x_window_to_frame (dpyinfo, hwnd);
3197 w = XWINDOW (FRAME_SELECTED_WINDOW (f));
3198
3199 form.dwStyle = CFS_RECT;
3200 form.ptCurrentPos.x = w32_system_caret_x;
3201 form.ptCurrentPos.y = w32_system_caret_y;
3202
3203 form.rcArea.left = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, 0);
3204 form.rcArea.top = (WINDOW_TOP_EDGE_Y (w)
3205 + WINDOW_HEADER_LINE_HEIGHT (w));
3206 form.rcArea.right = (WINDOW_BOX_RIGHT_EDGE_X (w)
3207 - WINDOW_RIGHT_MARGIN_WIDTH (w)
3208 - WINDOW_RIGHT_FRINGE_WIDTH (w));
3209 form.rcArea.bottom = (WINDOW_BOTTOM_EDGE_Y (w)
3210 - WINDOW_MODE_LINE_HEIGHT (w));
3211
3212 context = get_ime_context_fn (hwnd);
3213 set_ime_composition_window_fn (context, &form);
3214 release_ime_context_fn (hwnd, context);
3215 }
3216 break;
3217
820eff5a
JR
3218 case WM_IME_ENDCOMPOSITION:
3219 ignore_ime_char = 0;
3220 goto dflt;
3221
5ac45f98
GV
3222 /* Simulate middle mouse button events when left and right buttons
3223 are used together, but only if user has two button mouse. */
ee78dc32 3224 case WM_LBUTTONDOWN:
5ac45f98 3225 case WM_RBUTTONDOWN:
2ba49441 3226 if (w32_num_mouse_buttons > 2)
5ac45f98
GV
3227 goto handle_plain_button;
3228
3229 {
3230 int this = (msg == WM_LBUTTONDOWN) ? LMOUSE : RMOUSE;
3231 int other = (msg == WM_LBUTTONDOWN) ? RMOUSE : LMOUSE;
3232
3cb20f4a
RS
3233 if (button_state & this)
3234 return 0;
5ac45f98
GV
3235
3236 if (button_state == 0)
3237 SetCapture (hwnd);
3238
3239 button_state |= this;
3240
3241 if (button_state & other)
3242 {
84fb1139 3243 if (mouse_button_timer)
5ac45f98 3244 {
84fb1139
KH
3245 KillTimer (hwnd, mouse_button_timer);
3246 mouse_button_timer = 0;
5ac45f98
GV
3247
3248 /* Generate middle mouse event instead. */
3249 msg = WM_MBUTTONDOWN;
3250 button_state |= MMOUSE;
3251 }
3252 else if (button_state & MMOUSE)
3253 {
3254 /* Ignore button event if we've already generated a
3255 middle mouse down event. This happens if the
3256 user releases and press one of the two buttons
3257 after we've faked a middle mouse event. */
3258 return 0;
3259 }
3260 else
3261 {
3262 /* Flush out saved message. */
84fb1139 3263 post_msg (&saved_mouse_button_msg);
5ac45f98 3264 }
fbd6baed 3265 wmsg.dwModifiers = w32_get_modifiers ();
5ac45f98 3266 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
f5f69b6b 3267 signal_user_input ();
5ac45f98
GV
3268
3269 /* Clear message buffer. */
84fb1139 3270 saved_mouse_button_msg.msg.hwnd = 0;
5ac45f98
GV
3271 }
3272 else
3273 {
3274 /* Hold onto message for now. */
84fb1139 3275 mouse_button_timer =
adcc3809 3276 SetTimer (hwnd, MOUSE_BUTTON_ID,
2ba49441 3277 w32_mouse_button_tolerance, NULL);
84fb1139
KH
3278 saved_mouse_button_msg.msg.hwnd = hwnd;
3279 saved_mouse_button_msg.msg.message = msg;
3280 saved_mouse_button_msg.msg.wParam = wParam;
3281 saved_mouse_button_msg.msg.lParam = lParam;
3282 saved_mouse_button_msg.msg.time = GetMessageTime ();
fbd6baed 3283 saved_mouse_button_msg.dwModifiers = w32_get_modifiers ();
5ac45f98
GV
3284 }
3285 }
3286 return 0;
3287
ee78dc32 3288 case WM_LBUTTONUP:
5ac45f98 3289 case WM_RBUTTONUP:
2ba49441 3290 if (w32_num_mouse_buttons > 2)
5ac45f98
GV
3291 goto handle_plain_button;
3292
3293 {
3294 int this = (msg == WM_LBUTTONUP) ? LMOUSE : RMOUSE;
3295 int other = (msg == WM_LBUTTONUP) ? RMOUSE : LMOUSE;
3296
3cb20f4a
RS
3297 if ((button_state & this) == 0)
3298 return 0;
5ac45f98
GV
3299
3300 button_state &= ~this;
3301
3302 if (button_state & MMOUSE)
3303 {
3304 /* Only generate event when second button is released. */
3305 if ((button_state & other) == 0)
3306 {
3307 msg = WM_MBUTTONUP;
3308 button_state &= ~MMOUSE;
3309
3310 if (button_state) abort ();
3311 }
3312 else
3313 return 0;
3314 }
3315 else
3316 {
3317 /* Flush out saved message if necessary. */
84fb1139 3318 if (saved_mouse_button_msg.msg.hwnd)
5ac45f98 3319 {
84fb1139 3320 post_msg (&saved_mouse_button_msg);
5ac45f98
GV
3321 }
3322 }
fbd6baed 3323 wmsg.dwModifiers = w32_get_modifiers ();
5ac45f98 3324 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
f5f69b6b 3325 signal_user_input ();
5ac45f98
GV
3326
3327 /* Always clear message buffer and cancel timer. */
84fb1139
KH
3328 saved_mouse_button_msg.msg.hwnd = 0;
3329 KillTimer (hwnd, mouse_button_timer);
3330 mouse_button_timer = 0;
5ac45f98
GV
3331
3332 if (button_state == 0)
3333 ReleaseCapture ();
3334 }
3335 return 0;
3336
74214547
JR
3337 case WM_XBUTTONDOWN:
3338 case WM_XBUTTONUP:
3339 if (w32_pass_extra_mouse_buttons_to_system)
3340 goto dflt;
3341 /* else fall through and process them. */
ee78dc32
GV
3342 case WM_MBUTTONDOWN:
3343 case WM_MBUTTONUP:
5ac45f98 3344 handle_plain_button:
ee78dc32
GV
3345 {
3346 BOOL up;
1edf84e7 3347 int button;
ee78dc32 3348
b48f9276
EZ
3349 /* Ignore middle and extra buttons as long as the menu is active. */
3350 f = x_window_to_frame (dpyinfo, hwnd);
3351 if (f && f->output_data.w32->menubar_active)
3352 return 0;
3353
74214547 3354 if (parse_button (msg, HIWORD (wParam), &button, &up))
ee78dc32
GV
3355 {
3356 if (up) ReleaseCapture ();
3357 else SetCapture (hwnd);
7d0393cf 3358 button = (button == 0) ? LMOUSE :
1edf84e7
GV
3359 ((button == 1) ? MMOUSE : RMOUSE);
3360 if (up)
3361 button_state &= ~button;
3362 else
3363 button_state |= button;
ee78dc32
GV
3364 }
3365 }
7d0393cf 3366
fbd6baed 3367 wmsg.dwModifiers = w32_get_modifiers ();
ee78dc32 3368 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
f0c947b5 3369 signal_user_input ();
74214547
JR
3370
3371 /* Need to return true for XBUTTON messages, false for others,
3372 to indicate that we processed the message. */
3373 return (msg == WM_XBUTTONDOWN || msg == WM_XBUTTONUP);
5ac45f98 3374
5ac45f98 3375 case WM_MOUSEMOVE:
f93bd8e4
EZ
3376 /* Ignore mouse movements as long as the menu is active. These
3377 movements are processed by the window manager anyway, and
3378 it's wrong to handle them as if they happened on the
3379 underlying frame. */
3380 f = x_window_to_frame (dpyinfo, hwnd);
3381 if (f && f->output_data.w32->menubar_active)
3382 return 0;
3383
9eb16b62
JR
3384 /* If the mouse has just moved into the frame, start tracking
3385 it, so we will be notified when it leaves the frame. Mouse
3386 tracking only works under W98 and NT4 and later. On earlier
3387 versions, there is no way of telling when the mouse leaves the
3388 frame, so we just have to put up with help-echo and mouse
3389 highlighting remaining while the frame is not active. */
3390 if (track_mouse_event_fn && !track_mouse_window)
3391 {
3392 TRACKMOUSEEVENT tme;
3393 tme.cbSize = sizeof (tme);
3394 tme.dwFlags = TME_LEAVE;
3395 tme.hwndTrack = hwnd;
3396
3397 track_mouse_event_fn (&tme);
3398 track_mouse_window = hwnd;
3399 }
3400 case WM_VSCROLL:
2ba49441 3401 if (w32_mouse_move_interval <= 0
84fb1139
KH
3402 || (msg == WM_MOUSEMOVE && button_state == 0))
3403 {
fbd6baed 3404 wmsg.dwModifiers = w32_get_modifiers ();
84fb1139
KH
3405 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3406 return 0;
3407 }
7d0393cf 3408
84fb1139
KH
3409 /* Hang onto mouse move and scroll messages for a bit, to avoid
3410 sending such events to Emacs faster than it can process them.
3411 If we get more events before the timer from the first message
3412 expires, we just replace the first message. */
3413
3414 if (saved_mouse_move_msg.msg.hwnd == 0)
3415 mouse_move_timer =
adcc3809 3416 SetTimer (hwnd, MOUSE_MOVE_ID,
2ba49441 3417 w32_mouse_move_interval, NULL);
84fb1139
KH
3418
3419 /* Hold onto message for now. */
3420 saved_mouse_move_msg.msg.hwnd = hwnd;
3421 saved_mouse_move_msg.msg.message = msg;
3422 saved_mouse_move_msg.msg.wParam = wParam;
3423 saved_mouse_move_msg.msg.lParam = lParam;
3424 saved_mouse_move_msg.msg.time = GetMessageTime ();
fbd6baed 3425 saved_mouse_move_msg.dwModifiers = w32_get_modifiers ();
7d0393cf 3426
84fb1139
KH
3427 return 0;
3428
1edf84e7 3429 case WM_MOUSEWHEEL:
fd142562 3430 case WM_DROPFILES:
1edf84e7
GV
3431 wmsg.dwModifiers = w32_get_modifiers ();
3432 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
f0c947b5 3433 signal_user_input ();
1edf84e7
GV
3434 return 0;
3435
0b151762
JR
3436 case WM_APPCOMMAND:
3437 if (w32_pass_multimedia_buttons_to_system)
3438 goto dflt;
3439 /* Otherwise, pass to lisp, the same way we do with mousehwheel. */
fd142562 3440 case WM_MOUSEHWHEEL:
cb9e33d4
RS
3441 wmsg.dwModifiers = w32_get_modifiers ();
3442 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
f0c947b5 3443 signal_user_input ();
fd142562
JR
3444 /* Non-zero must be returned when WM_MOUSEHWHEEL messages are
3445 handled, to prevent the system trying to handle it by faking
3446 scroll bar events. */
3447 return 1;
cb9e33d4 3448
84fb1139
KH
3449 case WM_TIMER:
3450 /* Flush out saved messages if necessary. */
3451 if (wParam == mouse_button_timer)
5ac45f98 3452 {
84fb1139
KH
3453 if (saved_mouse_button_msg.msg.hwnd)
3454 {
3455 post_msg (&saved_mouse_button_msg);
f0c947b5 3456 signal_user_input ();
84fb1139
KH
3457 saved_mouse_button_msg.msg.hwnd = 0;
3458 }
3459 KillTimer (hwnd, mouse_button_timer);
3460 mouse_button_timer = 0;
3461 }
3462 else if (wParam == mouse_move_timer)
3463 {
3464 if (saved_mouse_move_msg.msg.hwnd)
3465 {
3466 post_msg (&saved_mouse_move_msg);
3467 saved_mouse_move_msg.msg.hwnd = 0;
3468 }
3469 KillTimer (hwnd, mouse_move_timer);
3470 mouse_move_timer = 0;
5ac45f98 3471 }
48094ace
JR
3472 else if (wParam == menu_free_timer)
3473 {
3474 KillTimer (hwnd, menu_free_timer);
3475 menu_free_timer = 0;
27605fa7 3476 f = x_window_to_frame (dpyinfo, hwnd);
5d22ded9 3477 /* If a popup menu is active, don't wipe its strings. */
58e55497 3478 if (menubar_in_use
5d22ded9 3479 && current_popup_menu == NULL)
48094ace
JR
3480 {
3481 /* Free memory used by owner-drawn and help-echo strings. */
3482 w32_free_menu_strings (hwnd);
3483 f->output_data.w32->menubar_active = 0;
58e55497 3484 menubar_in_use = 0;
48094ace
JR
3485 }
3486 }
d148e14d
JR
3487 else if (wParam == hourglass_timer)
3488 {
3489 KillTimer (hwnd, hourglass_timer);
3490 hourglass_timer = 0;
1391cd54 3491 w32_show_hourglass (x_window_to_frame (dpyinfo, hwnd));
d148e14d 3492 }
5ac45f98 3493 return 0;
7d0393cf 3494
84fb1139
KH
3495 case WM_NCACTIVATE:
3496 /* Windows doesn't send us focus messages when putting up and
e9e23e23 3497 taking down a system popup dialog as for Ctrl-Alt-Del on Windows 95.
84fb1139
KH
3498 The only indication we get that something happened is receiving
3499 this message afterwards. So this is a good time to reset our
3500 keyboard modifiers' state. */
3501 reset_modifiers ();
3502 goto dflt;
da36a4d6 3503
1edf84e7 3504 case WM_INITMENU:
487163ac
AI
3505 button_state = 0;
3506 ReleaseCapture ();
1edf84e7
GV
3507 /* We must ensure menu bar is fully constructed and up to date
3508 before allowing user interaction with it. To achieve this
3509 we send this message to the lisp thread and wait for a
3510 reply (whose value is not actually needed) to indicate that
3511 the menu bar is now ready for use, so we can now return.
3512
3513 To remain responsive in the meantime, we enter a nested message
3514 loop that can process all other messages.
3515
3516 However, we skip all this if the message results from calling
3517 TrackPopupMenu - in fact, we must NOT attempt to send the lisp
3518 thread a message because it is blocked on us at this point. We
3519 set menubar_active before calling TrackPopupMenu to indicate
3520 this (there is no possibility of confusion with real menubar
3521 being active). */
3522
3523 f = x_window_to_frame (dpyinfo, hwnd);
3524 if (f
3525 && (f->output_data.w32->menubar_active
3526 /* We can receive this message even in the absence of a
3527 menubar (ie. when the system menu is activated) - in this
3528 case we do NOT want to forward the message, otherwise it
3529 will cause the menubar to suddenly appear when the user
3530 had requested it to be turned off! */
3531 || f->output_data.w32->menubar_widget == NULL))
3532 return 0;
3533
3534 {
3535 deferred_msg msg_buf;
3536
3537 /* Detect if message has already been deferred; in this case
3538 we cannot return any sensible value to ignore this. */
3539 if (find_deferred_msg (hwnd, msg) != NULL)
3540 abort ();
3541
58e55497 3542 menubar_in_use = 1;
90816b86 3543
1edf84e7
GV
3544 return send_deferred_msg (&msg_buf, hwnd, msg, wParam, lParam);
3545 }
3546
3547 case WM_EXITMENULOOP:
3548 f = x_window_to_frame (dpyinfo, hwnd);
3549
5d22ded9
JR
3550 /* If a menu is still active, check again after a short delay,
3551 since Windows often (always?) sends the WM_EXITMENULOOP
3552 before the corresponding WM_COMMAND message.
3553 Don't do this if a popup menu is active, since it is only
3554 menubar menus that require cleaning up in this way.
3555 */
58e55497 3556 if (f && menubar_in_use && current_popup_menu == NULL)
48094ace 3557 menu_free_timer = SetTimer (hwnd, MENU_FREE_ID, MENU_FREE_DELAY, NULL);
d148e14d
JR
3558
3559 /* If hourglass cursor should be displayed, display it now. */
3560 if (f && f->output_data.w32->hourglass_p)
3561 SetCursor (f->output_data.w32->hourglass_cursor);
3562
1edf84e7
GV
3563 goto dflt;
3564
126f2e35 3565 case WM_MENUSELECT:
4e3a1c61
JR
3566 /* Direct handling of help_echo in menus. Should be safe now
3567 that we generate the help_echo by placing a help event in the
3568 keyboard buffer. */
ca56d953 3569 {
ca56d953
JR
3570 HMENU menu = (HMENU) lParam;
3571 UINT menu_item = (UINT) LOWORD (wParam);
3572 UINT flags = (UINT) HIWORD (wParam);
3573
4e3a1c61 3574 w32_menu_display_help (hwnd, menu, menu_item, flags);
ca56d953 3575 }
126f2e35
JR
3576 return 0;
3577
87996783
GV
3578 case WM_MEASUREITEM:
3579 f = x_window_to_frame (dpyinfo, hwnd);
3580 if (f)
3581 {
3582 MEASUREITEMSTRUCT * pMis = (MEASUREITEMSTRUCT *) lParam;
3583
3584 if (pMis->CtlType == ODT_MENU)
3585 {
3586 /* Work out dimensions for popup menu titles. */
3587 char * title = (char *) pMis->itemData;
3588 HDC hdc = GetDC (hwnd);
3589 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
3590 LOGFONT menu_logfont;
3591 HFONT old_font;
3592 SIZE size;
3593
3594 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
3595 menu_logfont.lfWeight = FW_BOLD;
3596 menu_font = CreateFontIndirect (&menu_logfont);
3597 old_font = SelectObject (hdc, menu_font);
3598
dfff8a69
JR
3599 pMis->itemHeight = GetSystemMetrics (SM_CYMENUSIZE);
3600 if (title)
3601 {
b4005349
JR
3602 if (unicode_append_menu)
3603 GetTextExtentPoint32W (hdc, (WCHAR *) title,
3604 wcslen ((WCHAR *) title),
3605 &size);
3606 else
3607 GetTextExtentPoint32 (hdc, title, strlen (title), &size);
3608
dfff8a69
JR
3609 pMis->itemWidth = size.cx;
3610 if (pMis->itemHeight < size.cy)
3611 pMis->itemHeight = size.cy;
3612 }
3613 else
3614 pMis->itemWidth = 0;
87996783
GV
3615
3616 SelectObject (hdc, old_font);
3617 DeleteObject (menu_font);
3618 ReleaseDC (hwnd, hdc);
3619 return TRUE;
3620 }
3621 }
3622 return 0;
3623
3624 case WM_DRAWITEM:
3625 f = x_window_to_frame (dpyinfo, hwnd);
3626 if (f)
3627 {
3628 DRAWITEMSTRUCT * pDis = (DRAWITEMSTRUCT *) lParam;
3629
3630 if (pDis->CtlType == ODT_MENU)
3631 {
3632 /* Draw popup menu title. */
3633 char * title = (char *) pDis->itemData;
212da13b
JR
3634 if (title)
3635 {
3636 HDC hdc = pDis->hDC;
3637 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
3638 LOGFONT menu_logfont;
3639 HFONT old_font;
3640
3641 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
3642 menu_logfont.lfWeight = FW_BOLD;
3643 menu_font = CreateFontIndirect (&menu_logfont);
3644 old_font = SelectObject (hdc, menu_font);
3645
b4005349
JR
3646 /* Always draw title as if not selected. */
3647 if (unicode_append_menu)
3648 ExtTextOutW (hdc,
3649 pDis->rcItem.left
3650 + GetSystemMetrics (SM_CXMENUCHECK),
3651 pDis->rcItem.top,
3652 ETO_OPAQUE, &pDis->rcItem,
3653 (WCHAR *) title,
3654 wcslen ((WCHAR *) title), NULL);
3655 else
3656 ExtTextOut (hdc,
3657 pDis->rcItem.left
3658 + GetSystemMetrics (SM_CXMENUCHECK),
3659 pDis->rcItem.top,
3660 ETO_OPAQUE, &pDis->rcItem,
3661 title, strlen (title), NULL);
212da13b
JR
3662
3663 SelectObject (hdc, old_font);
3664 DeleteObject (menu_font);
3665 }
87996783
GV
3666 return TRUE;
3667 }
3668 }
3669 return 0;
3670
1edf84e7
GV
3671#if 0
3672 /* Still not right - can't distinguish between clicks in the
3673 client area of the frame from clicks forwarded from the scroll
3674 bars - may have to hook WM_NCHITTEST to remember the mouse
3675 position and then check if it is in the client area ourselves. */
3676 case WM_MOUSEACTIVATE:
3677 /* Discard the mouse click that activates a frame, allowing the
3678 user to click anywhere without changing point (or worse!).
3679 Don't eat mouse clicks on scrollbars though!! */
3680 if (LOWORD (lParam) == HTCLIENT )
3681 return MA_ACTIVATEANDEAT;
3682 goto dflt;
3683#endif
3684
9eb16b62
JR
3685 case WM_MOUSELEAVE:
3686 /* No longer tracking mouse. */
3687 track_mouse_window = NULL;
3688
1edf84e7 3689 case WM_ACTIVATEAPP:
ccc2d29c 3690 case WM_ACTIVATE:
1edf84e7
GV
3691 case WM_WINDOWPOSCHANGED:
3692 case WM_SHOWWINDOW:
3693 /* Inform lisp thread that a frame might have just been obscured
3694 or exposed, so should recheck visibility of all frames. */
3695 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3696 goto dflt;
3697
da36a4d6 3698 case WM_SETFOCUS:
adcc3809
GV
3699 dpyinfo->faked_key = 0;
3700 reset_modifiers ();
ccc2d29c
GV
3701 register_hot_keys (hwnd);
3702 goto command;
8681157a 3703 case WM_KILLFOCUS:
ccc2d29c 3704 unregister_hot_keys (hwnd);
487163ac
AI
3705 button_state = 0;
3706 ReleaseCapture ();
65906840
JR
3707 /* Relinquish the system caret. */
3708 if (w32_system_caret_hwnd)
3709 {
93f2ca61 3710 w32_visible_system_caret_hwnd = NULL;
d285988b
JR
3711 w32_system_caret_hwnd = NULL;
3712 DestroyCaret ();
65906840 3713 }
48094ace
JR
3714 goto command;
3715 case WM_COMMAND:
58e55497 3716 menubar_in_use = 0;
48094ace
JR
3717 f = x_window_to_frame (dpyinfo, hwnd);
3718 if (f && HIWORD (wParam) == 0)
3719 {
48094ace
JR
3720 if (menu_free_timer)
3721 {
3722 KillTimer (hwnd, menu_free_timer);
7d0393cf 3723 menu_free_timer = 0;
48094ace
JR
3724 }
3725 }
ee78dc32
GV
3726 case WM_MOVE:
3727 case WM_SIZE:
ccc2d29c 3728 command:
fbd6baed 3729 wmsg.dwModifiers = w32_get_modifiers ();
ee78dc32
GV
3730 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3731 goto dflt;
8847d890 3732
d5781bb6
JR
3733 case WM_DESTROY:
3734 CoUninitialize ();
3735 return 0;
3736
8847d890 3737 case WM_CLOSE:
fbd6baed 3738 wmsg.dwModifiers = w32_get_modifiers ();
8847d890
RS
3739 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3740 return 0;
3741
ee78dc32 3742 case WM_WINDOWPOSCHANGING:
bfd6edcc
JR
3743 /* Don't restrict the sizing of tip frames. */
3744 if (hwnd == tip_window)
3745 return 0;
ee78dc32
GV
3746 {
3747 WINDOWPLACEMENT wp;
3748 LPWINDOWPOS lppos = (WINDOWPOS *) lParam;
1edf84e7
GV
3749
3750 wp.length = sizeof (WINDOWPLACEMENT);
ee78dc32 3751 GetWindowPlacement (hwnd, &wp);
7d0393cf 3752
1edf84e7 3753 if (wp.showCmd != SW_SHOWMINIMIZED && (lppos->flags & SWP_NOSIZE) == 0)
ee78dc32
GV
3754 {
3755 RECT rect;
3756 int wdiff;
3757 int hdiff;
1edf84e7
GV
3758 DWORD font_width;
3759 DWORD line_height;
3760 DWORD internal_border;
3761 DWORD scrollbar_extra;
ee78dc32 3762 RECT wr;
7d0393cf 3763
74084731 3764 wp.length = sizeof (wp);
ee78dc32 3765 GetWindowRect (hwnd, &wr);
7d0393cf 3766
3c190163 3767 enter_crit ();
7d0393cf 3768
1edf84e7
GV
3769 font_width = GetWindowLong (hwnd, WND_FONTWIDTH_INDEX);
3770 line_height = GetWindowLong (hwnd, WND_LINEHEIGHT_INDEX);
3771 internal_border = GetWindowLong (hwnd, WND_BORDER_INDEX);
3772 scrollbar_extra = GetWindowLong (hwnd, WND_SCROLLBAR_INDEX);
7d0393cf 3773
3c190163 3774 leave_crit ();
7d0393cf 3775
ee78dc32 3776 memset (&rect, 0, sizeof (rect));
7d0393cf 3777 AdjustWindowRect (&rect, GetWindowLong (hwnd, GWL_STYLE),
ee78dc32
GV
3778 GetMenu (hwnd) != NULL);
3779
1edf84e7
GV
3780 /* Force width and height of client area to be exact
3781 multiples of the character cell dimensions. */
3782 wdiff = (lppos->cx - (rect.right - rect.left)
3783 - 2 * internal_border - scrollbar_extra)
3784 % font_width;
3785 hdiff = (lppos->cy - (rect.bottom - rect.top)
3786 - 2 * internal_border)
3787 % line_height;
7d0393cf 3788
ee78dc32
GV
3789 if (wdiff || hdiff)
3790 {
7d0393cf
JB
3791 /* For right/bottom sizing we can just fix the sizes.
3792 However for top/left sizing we will need to fix the X
ee78dc32 3793 and Y positions as well. */
7d0393cf 3794
8a4c4c7f
JB
3795 int cx_mintrack = GetSystemMetrics (SM_CXMINTRACK);
3796 int cy_mintrack = GetSystemMetrics (SM_CYMINTRACK);
3797
3798 lppos->cx = max (lppos->cx - wdiff, cx_mintrack);
3799 lppos->cy = max (lppos->cy - hdiff, cy_mintrack);
7d0393cf
JB
3800
3801 if (wp.showCmd != SW_SHOWMAXIMIZED
1edf84e7 3802 && (lppos->flags & SWP_NOMOVE) == 0)
ee78dc32
GV
3803 {
3804 if (lppos->x != wr.left || lppos->y != wr.top)
3805 {
3806 lppos->x += wdiff;
3807 lppos->y += hdiff;
3808 }
3809 else
3810 {
3811 lppos->flags |= SWP_NOMOVE;
3812 }
3813 }
7d0393cf 3814
1edf84e7 3815 return 0;
ee78dc32
GV
3816 }
3817 }
3818 }
7d0393cf 3819
ee78dc32 3820 goto dflt;
1edf84e7 3821
b1f918f8 3822 case WM_GETMINMAXINFO:
bf853fee
AI
3823 /* Hack to allow resizing the Emacs frame above the screen size.
3824 Note that Windows 9x limits coordinates to 16-bits. */
3825 ((LPMINMAXINFO) lParam)->ptMaxTrackSize.x = 32767;
3826 ((LPMINMAXINFO) lParam)->ptMaxTrackSize.y = 32767;
b1f918f8
GV
3827 return 0;
3828
c9b2104d
JR
3829 case WM_SETCURSOR:
3830 if (LOWORD (lParam) == HTCLIENT)
d148e14d
JR
3831 {
3832 f = x_window_to_frame (dpyinfo, hwnd);
3833 if (f->output_data.w32->hourglass_p && !menubar_in_use
3834 && !current_popup_menu)
3835 SetCursor (f->output_data.w32->hourglass_cursor);
3836 else
3837 SetCursor (f->output_data.w32->current_cursor);
3838 return 0;
3839 }
c9b2104d 3840 goto dflt;
c922a224 3841
c9b2104d
JR
3842 case WM_EMACS_SETCURSOR:
3843 {
3844 Cursor cursor = (Cursor) wParam;
d148e14d
JR
3845 f = x_window_to_frame (dpyinfo, hwnd);
3846 if (f && cursor)
3847 {
3848 f->output_data.w32->current_cursor = cursor;
3849 if (!f->output_data.w32->hourglass_p)
3850 SetCursor (cursor);
3851 }
c9b2104d
JR
3852 return 0;
3853 }
c922a224 3854
1edf84e7
GV
3855 case WM_EMACS_CREATESCROLLBAR:
3856 return (LRESULT) w32_createscrollbar ((struct frame *) wParam,
3857 (struct scroll_bar *) lParam);
3858
5ac45f98 3859 case WM_EMACS_SHOWWINDOW:
1edf84e7
GV
3860 return ShowWindow ((HWND) wParam, (WPARAM) lParam);
3861
dfdb4047 3862 case WM_EMACS_SETFOREGROUND:
ce6059da
AI
3863 {
3864 HWND foreground_window;
3865 DWORD foreground_thread, retval;
3866
3867 /* On NT 5.0, and apparently Windows 98, it is necessary to
3868 attach to the thread that currently has focus in order to
3869 pull the focus away from it. */
3870 foreground_window = GetForegroundWindow ();
3871 foreground_thread = GetWindowThreadProcessId (foreground_window, NULL);
3872 if (!foreground_window
3873 || foreground_thread == GetCurrentThreadId ()
3874 || !AttachThreadInput (GetCurrentThreadId (),
3875 foreground_thread, TRUE))
3876 foreground_thread = 0;
3877
3878 retval = SetForegroundWindow ((HWND) wParam);
3879
3880 /* Detach from the previous foreground thread. */
3881 if (foreground_thread)
3882 AttachThreadInput (GetCurrentThreadId (),
3883 foreground_thread, FALSE);
3884
3885 return retval;
3886 }
dfdb4047 3887
5ac45f98
GV
3888 case WM_EMACS_SETWINDOWPOS:
3889 {
1edf84e7
GV
3890 WINDOWPOS * pos = (WINDOWPOS *) wParam;
3891 return SetWindowPos (hwnd, pos->hwndInsertAfter,
5ac45f98
GV
3892 pos->x, pos->y, pos->cx, pos->cy, pos->flags);
3893 }
1edf84e7 3894
ee78dc32 3895 case WM_EMACS_DESTROYWINDOW:
cb9e33d4 3896 DragAcceptFiles ((HWND) wParam, FALSE);
1edf84e7
GV
3897 return DestroyWindow ((HWND) wParam);
3898
93f2ca61
JR
3899 case WM_EMACS_HIDE_CARET:
3900 return HideCaret (hwnd);
3901
3902 case WM_EMACS_SHOW_CARET:
3903 return ShowCaret (hwnd);
3904
65906840
JR
3905 case WM_EMACS_DESTROY_CARET:
3906 w32_system_caret_hwnd = NULL;
93f2ca61 3907 w32_visible_system_caret_hwnd = NULL;
65906840
JR
3908 return DestroyCaret ();
3909
3910 case WM_EMACS_TRACK_CARET:
3911 /* If there is currently no system caret, create one. */
3912 if (w32_system_caret_hwnd == NULL)
3913 {
93f2ca61
JR
3914 /* Use the default caret width, and avoid changing it
3915 unneccesarily, as it confuses screen reader software. */
65906840 3916 w32_system_caret_hwnd = hwnd;
93f2ca61 3917 CreateCaret (hwnd, NULL, 0,
65906840
JR
3918 w32_system_caret_height);
3919 }
7d0393cf 3920
93f2ca61
JR
3921 if (!SetCaretPos (w32_system_caret_x, w32_system_caret_y))
3922 return 0;
3923 /* Ensure visible caret gets turned on when requested. */
3924 else if (w32_use_visible_system_caret
3925 && w32_visible_system_caret_hwnd != hwnd)
3926 {
3927 w32_visible_system_caret_hwnd = hwnd;
3928 return ShowCaret (hwnd);
3929 }
3930 /* Ensure visible caret gets turned off when requested. */
3931 else if (!w32_use_visible_system_caret
3932 && w32_visible_system_caret_hwnd)
3933 {
3934 w32_visible_system_caret_hwnd = NULL;
3935 return HideCaret (hwnd);
3936 }
3937 else
3938 return 1;
65906840 3939
1edf84e7
GV
3940 case WM_EMACS_TRACKPOPUPMENU:
3941 {
3942 UINT flags;
3943 POINT *pos;
3944 int retval;
3945 pos = (POINT *)lParam;
3946 flags = TPM_CENTERALIGN;
3947 if (button_state & LMOUSE)
3948 flags |= TPM_LEFTBUTTON;
3949 else if (button_state & RMOUSE)
3950 flags |= TPM_RIGHTBUTTON;
7d0393cf 3951
87996783
GV
3952 /* Remember we did a SetCapture on the initial mouse down event,
3953 so for safety, we make sure the capture is cancelled now. */
3954 ReleaseCapture ();
490822ff 3955 button_state = 0;
87996783 3956
1edf84e7
GV
3957 /* Use menubar_active to indicate that WM_INITMENU is from
3958 TrackPopupMenu below, and should be ignored. */
3959 f = x_window_to_frame (dpyinfo, hwnd);
3960 if (f)
3961 f->output_data.w32->menubar_active = 1;
7d0393cf
JB
3962
3963 if (TrackPopupMenu ((HMENU)wParam, flags, pos->x, pos->y,
1edf84e7
GV
3964 0, hwnd, NULL))
3965 {
3966 MSG amsg;
3967 /* Eat any mouse messages during popupmenu */
3968 while (PeekMessage (&amsg, hwnd, WM_MOUSEFIRST, WM_MOUSELAST,
3969 PM_REMOVE));
3970 /* Get the menu selection, if any */
3971 if (PeekMessage (&amsg, hwnd, WM_COMMAND, WM_COMMAND, PM_REMOVE))
3972 {
3973 retval = LOWORD (amsg.wParam);
3974 }
3975 else
3976 {
3977 retval = 0;
3978 }
1edf84e7
GV
3979 }
3980 else
3981 {
3982 retval = -1;
3983 }
3984
3985 return retval;
3986 }
3987
ee78dc32 3988 default:
93fbe8b7
GV
3989 /* Check for messages registered at runtime. */
3990 if (msg == msh_mousewheel)
3991 {
3992 wmsg.dwModifiers = w32_get_modifiers ();
3993 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
f0c947b5 3994 signal_user_input ();
93fbe8b7
GV
3995 return 0;
3996 }
7d0393cf 3997
ee78dc32
GV
3998 dflt:
3999 return DefWindowProc (hwnd, msg, wParam, lParam);
4000 }
7d0393cf 4001
1edf84e7
GV
4002
4003 /* The most common default return code for handled messages is 0. */
4004 return 0;
ee78dc32
GV
4005}
4006
0962822d 4007static void
ee78dc32
GV
4008my_create_window (f)
4009 struct frame * f;
4010{
4011 MSG msg;
4012
1edf84e7
GV
4013 if (!PostThreadMessage (dwWindowsThreadId, WM_EMACS_CREATEWINDOW, (WPARAM)f, 0))
4014 abort ();
ee78dc32
GV
4015 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
4016}
4017
ca56d953
JR
4018
4019/* Create a tooltip window. Unlike my_create_window, we do not do this
4020 indirectly via the Window thread, as we do not need to process Window
4021 messages for the tooltip. Creating tooltips indirectly also creates
4022 deadlocks when tooltips are created for menu items. */
0962822d 4023static void
ca56d953
JR
4024my_create_tip_window (f)
4025 struct frame *f;
4026{
bfd6edcc 4027 RECT rect;
ca56d953 4028
bfd6edcc 4029 rect.left = rect.top = 0;
be786000
KS
4030 rect.right = FRAME_PIXEL_WIDTH (f);
4031 rect.bottom = FRAME_PIXEL_HEIGHT (f);
bfd6edcc
JR
4032
4033 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
4034 FRAME_EXTERNAL_MENU_BAR (f));
4035
4036 tip_window = FRAME_W32_WINDOW (f)
ca56d953
JR
4037 = CreateWindow (EMACS_CLASS,
4038 f->namebuf,
4039 f->output_data.w32->dwStyle,
be786000
KS
4040 f->left_pos,
4041 f->top_pos,
bfd6edcc
JR
4042 rect.right - rect.left,
4043 rect.bottom - rect.top,
ca56d953
JR
4044 FRAME_W32_WINDOW (SELECTED_FRAME ()), /* owner */
4045 NULL,
4046 hinst,
4047 NULL);
4048
bfd6edcc 4049 if (tip_window)
ca56d953 4050 {
be786000
KS
4051 SetWindowLong (tip_window, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
4052 SetWindowLong (tip_window, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
4053 SetWindowLong (tip_window, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
bfd6edcc
JR
4054 SetWindowLong (tip_window, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
4055
4056 /* Tip frames have no scrollbars. */
4057 SetWindowLong (tip_window, WND_SCROLLBAR_INDEX, 0);
ca56d953
JR
4058
4059 /* Do this to discard the default setting specified by our parent. */
bfd6edcc 4060 ShowWindow (tip_window, SW_HIDE);
ca56d953
JR
4061 }
4062}
4063
4064
fbd6baed 4065/* Create and set up the w32 window for frame F. */
ee78dc32
GV
4066
4067static void
fbd6baed 4068w32_window (f, window_prompting, minibuffer_only)
ee78dc32
GV
4069 struct frame *f;
4070 long window_prompting;
4071 int minibuffer_only;
4072{
4073 BLOCK_INPUT;
4074
4075 /* Use the resource name as the top-level window name
4076 for looking up resources. Make a non-Lisp copy
4077 for the window manager, so GC relocation won't bother it.
4078
4079 Elsewhere we specify the window name for the window manager. */
7d0393cf 4080
ee78dc32 4081 {
d5db4077 4082 char *str = (char *) SDATA (Vx_resource_name);
ee78dc32
GV
4083 f->namebuf = (char *) xmalloc (strlen (str) + 1);
4084 strcpy (f->namebuf, str);
4085 }
4086
4087 my_create_window (f);
4088
4089 validate_x_resource_name ();
4090
4091 /* x_set_name normally ignores requests to set the name if the
4092 requested name is the same as the current name. This is the one
4093 place where that assumption isn't correct; f->name is set, but
4094 the server hasn't been told. */
4095 {
4096 Lisp_Object name;
4097 int explicit = f->explicit_name;
4098
4099 f->explicit_name = 0;
4100 name = f->name;
4101 f->name = Qnil;
4102 x_set_name (f, name, explicit);
4103 }
4104
4105 UNBLOCK_INPUT;
4106
4107 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
4108 initialize_frame_menubar (f);
4109
fbd6baed 4110 if (FRAME_W32_WINDOW (f) == 0)
ee78dc32
GV
4111 error ("Unable to create window");
4112}
4113
4114/* Handle the icon stuff for this window. Perhaps later we might
4115 want an x_set_icon_position which can be called interactively as
4116 well. */
4117
4118static void
4119x_icon (f, parms)
4120 struct frame *f;
4121 Lisp_Object parms;
4122{
4123 Lisp_Object icon_x, icon_y;
3faa984f 4124 struct w32_display_info *dpyinfo = &one_w32_display_info;
ee78dc32 4125
e9e23e23 4126 /* Set the position of the icon. Note that Windows 95 groups all
ee78dc32 4127 icons in the tray. */
3faa984f
JR
4128 icon_x = x_get_arg (dpyinfo, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
4129 icon_y = x_get_arg (dpyinfo, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
ee78dc32
GV
4130 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
4131 {
b7826503
PJ
4132 CHECK_NUMBER (icon_x);
4133 CHECK_NUMBER (icon_y);
ee78dc32
GV
4134 }
4135 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
4136 error ("Both left and top icon corners of icon must be specified");
4137
4138 BLOCK_INPUT;
4139
4140 if (! EQ (icon_x, Qunbound))
4141 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
4142
1edf84e7
GV
4143#if 0 /* TODO */
4144 /* Start up iconic or window? */
4145 x_wm_set_window_state
3faa984f 4146 (f, (EQ (x_get_arg (dpyinfo, parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL), Qicon)
1edf84e7
GV
4147 ? IconicState
4148 : NormalState));
4149
d5db4077 4150 x_text_icon (f, (char *) SDATA ((!NILP (f->icon_name)
1edf84e7 4151 ? f->icon_name
d5db4077 4152 : f->name)));
1edf84e7
GV
4153#endif
4154
ee78dc32
GV
4155 UNBLOCK_INPUT;
4156}
4157
6fc2811b
JR
4158
4159static void
4160x_make_gc (f)
4161 struct frame *f;
4162{
4163 XGCValues gc_values;
4164
4165 BLOCK_INPUT;
4166
4167 /* Create the GC's of this frame.
4168 Note that many default values are used. */
4169
4170 /* Normal video */
be786000 4171 gc_values.font = FRAME_FONT (f);
6fc2811b
JR
4172
4173 /* Cursor has cursor-color background, background-color foreground. */
4174 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
4175 gc_values.background = f->output_data.w32->cursor_pixel;
4176 f->output_data.w32->cursor_gc
4177 = XCreateGC (NULL, FRAME_W32_WINDOW (f),
4178 (GCFont | GCForeground | GCBackground),
4179 &gc_values);
4180
4181 /* Reliefs. */
4182 f->output_data.w32->white_relief.gc = 0;
4183 f->output_data.w32->black_relief.gc = 0;
4184
4185 UNBLOCK_INPUT;
4186}
4187
4188
937e601e
AI
4189/* Handler for signals raised during x_create_frame and
4190 x_create_top_frame. FRAME is the frame which is partially
4191 constructed. */
4192
4193static Lisp_Object
4194unwind_create_frame (frame)
4195 Lisp_Object frame;
4196{
4197 struct frame *f = XFRAME (frame);
4198
4199 /* If frame is ``official'', nothing to do. */
4200 if (!CONSP (Vframe_list) || !EQ (XCAR (Vframe_list), frame))
4201 {
4202#ifdef GLYPH_DEBUG
4203 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4204#endif
7d0393cf 4205
937e601e
AI
4206 x_free_frame_resources (f);
4207
a9b555d3 4208#if GLYPH_DEBUG
937e601e
AI
4209 /* Check that reference counts are indeed correct. */
4210 xassert (dpyinfo->reference_count == dpyinfo_refcount);
4211 xassert (dpyinfo->image_cache->refcount == image_cache_refcount);
a9b555d3 4212#endif
c844a81a 4213 return Qt;
937e601e 4214 }
7d0393cf 4215
937e601e
AI
4216 return Qnil;
4217}
4218
a1fe5c00
JR
4219static void
4220x_default_font_parameter (f, parms)
4221 struct frame *f;
4222 Lisp_Object parms;
4223{
4224 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
770e2e6e
SM
4225 Lisp_Object font_param = x_get_arg (dpyinfo, parms, Qfont, NULL, NULL,
4226 RES_TYPE_STRING);
4227 Lisp_Object font;
4228 if (EQ (font_param, Qunbound))
4229 font_param = Qnil;
4230 font = !NILP (font_param) ? font_param
4231 : x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
a1fe5c00
JR
4232
4233 if (!STRINGP (font))
4234 {
4235 int i;
4236 static char *names[]
82523155 4237 = { "Courier New-10",
a1fe5c00
JR
4238 "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1",
4239 "-*-Fixedsys-normal-r-*-*-12-*-*-*-c-*-iso8859-1",
4240 "Fixedsys",
4241 NULL };
4242
4243 for (i = 0; names[i]; i++)
4244 {
4245 font = font_open_by_name (f, names[i]);
4246 if (! NILP (font))
4247 break;
4248 }
4249 if (NILP (font))
4250 error ("No suitable font was found");
4251 }
770e2e6e 4252 else if (!NILP (font_param))
27129af9
SM
4253 {
4254 /* Remember the explicit font parameter, so we can re-apply it after
4255 we've applied the `default' face settings. */
770e2e6e
SM
4256 x_set_frame_parameters (f, Fcons (Fcons (Qfont_param, font_param), Qnil));
4257 }
a1fe5c00
JR
4258 x_default_parameter (f, parms, Qfont, font, "font", "Font", RES_TYPE_STRING);
4259}
937e601e 4260
ee78dc32
GV
4261DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
4262 1, 1, 0,
74e1aeec 4263 doc: /* Make a new window, which is called a \"frame\" in Emacs terms.
36458ebd 4264Return an Emacs frame object.
52deb19f 4265PARAMETERS is an alist of frame parameters.
74e1aeec
JR
4266If the parameters specify that the frame should not have a minibuffer,
4267and do not specify a specific minibuffer window to use,
4268then `default-minibuffer-frame' must be a frame whose minibuffer can
4269be shared by the new frame.
4270
4271This function is an internal primitive--use `make-frame' instead. */)
52deb19f
JB
4272 (parameters)
4273 Lisp_Object parameters;
ee78dc32
GV
4274{
4275 struct frame *f;
4276 Lisp_Object frame, tem;
4277 Lisp_Object name;
4278 int minibuffer_only = 0;
4279 long window_prompting = 0;
4280 int width, height;
331379bf 4281 int count = SPECPDL_INDEX ();
1edf84e7 4282 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
ee78dc32 4283 Lisp_Object display;
6fc2811b 4284 struct w32_display_info *dpyinfo = NULL;
ee78dc32
GV
4285 Lisp_Object parent;
4286 struct kboard *kb;
4287
1da8a031
MR
4288 /* Make copy of frame parameters because the original is in pure
4289 storage now. */
4290 parameters = Fcopy_alist (parameters);
4291
ee78dc32
GV
4292 /* Use this general default value to start with
4293 until we know if this frame has a specified name. */
4294 Vx_resource_name = Vinvocation_name;
4295
3faa984f
JR
4296 display = x_get_arg (dpyinfo, parameters, Qterminal, 0, 0, RES_TYPE_NUMBER);
4297 if (EQ (display, Qunbound))
4298 display = x_get_arg (dpyinfo, parameters, Qdisplay, 0, 0, RES_TYPE_STRING);
ee78dc32
GV
4299 if (EQ (display, Qunbound))
4300 display = Qnil;
4301 dpyinfo = check_x_display_info (display);
80ca7302 4302 kb = dpyinfo->terminal->kboard;
ee78dc32 4303
3faa984f
JR
4304 if (!dpyinfo->terminal->name)
4305 error ("Terminal is not live, can't create new frames on it");
4306
4307 name = x_get_arg (dpyinfo, parameters, Qname, "name", "Name", RES_TYPE_STRING);
ee78dc32
GV
4308 if (!STRINGP (name)
4309 && ! EQ (name, Qunbound)
4310 && ! NILP (name))
4311 error ("Invalid frame name--not a string or nil");
4312
4313 if (STRINGP (name))
4314 Vx_resource_name = name;
4315
4316 /* See if parent window is specified. */
3faa984f 4317 parent = x_get_arg (dpyinfo, parameters, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
ee78dc32
GV
4318 if (EQ (parent, Qunbound))
4319 parent = Qnil;
4320 if (! NILP (parent))
b7826503 4321 CHECK_NUMBER (parent);
ee78dc32 4322
1edf84e7
GV
4323 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
4324 /* No need to protect DISPLAY because that's not used after passing
4325 it to make_frame_without_minibuffer. */
4326 frame = Qnil;
52deb19f 4327 GCPRO4 (parameters, parent, name, frame);
3faa984f 4328 tem = x_get_arg (dpyinfo, parameters, Qminibuffer, "minibuffer", "Minibuffer",
1660f34a 4329 RES_TYPE_SYMBOL);
ee78dc32
GV
4330 if (EQ (tem, Qnone) || NILP (tem))
4331 f = make_frame_without_minibuffer (Qnil, kb, display);
4332 else if (EQ (tem, Qonly))
4333 {
4334 f = make_minibuffer_frame ();
4335 minibuffer_only = 1;
4336 }
4337 else if (WINDOWP (tem))
4338 f = make_frame_without_minibuffer (tem, kb, display);
4339 else
4340 f = make_frame (1);
4341
1edf84e7
GV
4342 XSETFRAME (frame, f);
4343
ee78dc32
GV
4344 /* Note that Windows does support scroll bars. */
4345 FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;
6d906347 4346
5ac45f98 4347 /* By default, make scrollbars the system standard width. */
be786000 4348 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = GetSystemMetrics (SM_CXVSCROLL);
ee78dc32 4349
2dc8b986
JR
4350 f->terminal = dpyinfo->terminal;
4351 f->terminal->reference_count++;
4352
fbd6baed 4353 f->output_method = output_w32;
6fc2811b
JR
4354 f->output_data.w32 =
4355 (struct w32_output *) xmalloc (sizeof (struct w32_output));
fbd6baed 4356 bzero (f->output_data.w32, sizeof (struct w32_output));
4587b026
GV
4357 FRAME_FONTSET (f) = -1;
4358
1edf84e7 4359 f->icon_name
3faa984f
JR
4360 = x_get_arg (dpyinfo, parameters, Qicon_name, "iconName", "Title",
4361 RES_TYPE_STRING);
1edf84e7
GV
4362 if (! STRINGP (f->icon_name))
4363 f->icon_name = Qnil;
4364
fbd6baed 4365/* FRAME_W32_DISPLAY_INFO (f) = dpyinfo; */
3faa984f
JR
4366
4367 /* With FRAME_X_DISPLAY_INFO set up, this unwind-protect is safe. */
4368 record_unwind_protect (unwind_create_frame, frame);
4369#if GLYPH_DEBUG
4370 image_cache_refcount = FRAME_IMAGE_CACHE (f)->refcount;
4371 dpyinfo_refcount = dpyinfo->reference_count;
4372#endif /* GLYPH_DEBUG */
ee78dc32
GV
4373
4374 /* Specify the parent under which to make this window. */
4375
4376 if (!NILP (parent))
4377 {
1660f34a 4378 f->output_data.w32->parent_desc = (Window) XFASTINT (parent);
fbd6baed 4379 f->output_data.w32->explicit_parent = 1;
ee78dc32
GV
4380 }
4381 else
4382 {
fbd6baed
GV
4383 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
4384 f->output_data.w32->explicit_parent = 0;
ee78dc32
GV
4385 }
4386
ee78dc32
GV
4387 /* Set the name; the functions to which we pass f expect the name to
4388 be set. */
4389 if (EQ (name, Qunbound) || NILP (name))
4390 {
fbd6baed 4391 f->name = build_string (dpyinfo->w32_id_name);
ee78dc32
GV
4392 f->explicit_name = 0;
4393 }
4394 else
4395 {
4396 f->name = name;
4397 f->explicit_name = 1;
4398 /* use the frame's title when getting resources for this frame. */
4399 specbind (Qx_resource_name, name);
4400 }
4401
82523155
JR
4402 f->resx = dpyinfo->resx;
4403 f->resy = dpyinfo->resy;
4404
1cc06b86
KH
4405 if (uniscribe_available)
4406 register_font_driver (&uniscribe_font_driver, f);
4407 register_font_driver (&w32font_driver, f);
a1fe5c00 4408
1cc06b86
KH
4409 x_default_parameter (f, parameters, Qfont_backend, Qnil,
4410 "fontBackend", "FontBackend", RES_TYPE_STRING);
ee78dc32
GV
4411 /* Extract the window parameters from the supplied values
4412 that are needed to determine window geometry. */
1cc06b86 4413 x_default_font_parameter (f, parameters);
52deb19f 4414 x_default_parameter (f, parameters, Qborder_width, make_number (2),
1660f34a 4415 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
3faa984f 4416
601a9cf1 4417 /* We recognize either internalBorderWidth or internalBorder
3faa984f 4418 (which is what xterm calls it). */
52deb19f 4419 if (NILP (Fassq (Qinternal_border_width, parameters)))
ee78dc32
GV
4420 {
4421 Lisp_Object value;
4422
3faa984f 4423 value = x_get_arg (dpyinfo, parameters, Qinternal_border_width,
52deb19f 4424 "internalBorder", "InternalBorder", RES_TYPE_NUMBER);
ee78dc32 4425 if (! EQ (value, Qunbound))
52deb19f
JB
4426 parameters = Fcons (Fcons (Qinternal_border_width, value),
4427 parameters);
ee78dc32 4428 }
1edf84e7 4429 /* Default internalBorderWidth to 0 on Windows to match other programs. */
52deb19f 4430 x_default_parameter (f, parameters, Qinternal_border_width, make_number (0),
1660f34a 4431 "internalBorderWidth", "InternalBorder", RES_TYPE_NUMBER);
52deb19f 4432 x_default_parameter (f, parameters, Qvertical_scroll_bars, Qright,
1660f34a 4433 "verticalScrollBars", "ScrollBars", RES_TYPE_SYMBOL);
ee78dc32
GV
4434
4435 /* Also do the stuff which must be set before the window exists. */
52deb19f 4436 x_default_parameter (f, parameters, Qforeground_color, build_string ("black"),
6fc2811b 4437 "foreground", "Foreground", RES_TYPE_STRING);
52deb19f 4438 x_default_parameter (f, parameters, Qbackground_color, build_string ("white"),
6fc2811b 4439 "background", "Background", RES_TYPE_STRING);
52deb19f 4440 x_default_parameter (f, parameters, Qmouse_color, build_string ("black"),
6fc2811b 4441 "pointerColor", "Foreground", RES_TYPE_STRING);
52deb19f 4442 x_default_parameter (f, parameters, Qcursor_color, build_string ("black"),
6fc2811b 4443 "cursorColor", "Foreground", RES_TYPE_STRING);
52deb19f 4444 x_default_parameter (f, parameters, Qborder_color, build_string ("black"),
6fc2811b 4445 "borderColor", "BorderColor", RES_TYPE_STRING);
52deb19f 4446 x_default_parameter (f, parameters, Qscreen_gamma, Qnil,
6fc2811b 4447 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
52deb19f 4448 x_default_parameter (f, parameters, Qline_spacing, Qnil,
dfff8a69 4449 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
52deb19f 4450 x_default_parameter (f, parameters, Qleft_fringe, Qnil,
41c1bdd9 4451 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
52deb19f 4452 x_default_parameter (f, parameters, Qright_fringe, Qnil,
41c1bdd9 4453 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
6fc2811b 4454
ee78dc32 4455
6fc2811b
JR
4456 /* Init faces before x_default_parameter is called for scroll-bar
4457 parameters because that function calls x_set_scroll_bar_width,
4458 which calls change_frame_size, which calls Fset_window_buffer,
4459 which runs hooks, which call Fvertical_motion. At the end, we
4460 end up in init_iterator with a null face cache, which should not
4461 happen. */
4462 init_frame_faces (f);
7d0393cf 4463
52deb19f 4464 x_default_parameter (f, parameters, Qmenu_bar_lines, make_number (1),
6fc2811b 4465 "menuBar", "MenuBar", RES_TYPE_NUMBER);
52deb19f 4466 x_default_parameter (f, parameters, Qtool_bar_lines, make_number (1),
6fc2811b 4467 "toolBar", "ToolBar", RES_TYPE_NUMBER);
919f1e88 4468
52deb19f 4469 x_default_parameter (f, parameters, Qbuffer_predicate, Qnil,
6fc2811b 4470 "bufferPredicate", "BufferPredicate", RES_TYPE_SYMBOL);
52deb19f 4471 x_default_parameter (f, parameters, Qtitle, Qnil,
6fc2811b 4472 "title", "Title", RES_TYPE_STRING);
52deb19f 4473 x_default_parameter (f, parameters, Qfullscreen, Qnil,
f7b9d4d1 4474 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
ee78dc32 4475
fbd6baed
GV
4476 f->output_data.w32->dwStyle = WS_OVERLAPPEDWINDOW;
4477 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
3cf3436e 4478
c9b2104d
JR
4479 f->output_data.w32->text_cursor = w32_load_cursor (IDC_IBEAM);
4480 f->output_data.w32->nontext_cursor = w32_load_cursor (IDC_ARROW);
4481 f->output_data.w32->modeline_cursor = w32_load_cursor (IDC_ARROW);
7d63e5e3 4482 f->output_data.w32->hand_cursor = w32_load_cursor (IDC_HAND);
c9b2104d
JR
4483 f->output_data.w32->hourglass_cursor = w32_load_cursor (IDC_WAIT);
4484 f->output_data.w32->horizontal_drag_cursor = w32_load_cursor (IDC_SIZEWE);
c9b2104d 4485
d148e14d
JR
4486 f->output_data.w32->current_cursor = f->output_data.w32->nontext_cursor;
4487
52deb19f 4488 window_prompting = x_figure_window_size (f, parameters, 1);
ee78dc32 4489
3faa984f 4490 tem = x_get_arg (dpyinfo, parameters, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
6fc2811b
JR
4491 f->no_split = minibuffer_only || EQ (tem, Qt);
4492
fbd6baed 4493 w32_window (f, window_prompting, minibuffer_only);
52deb19f 4494 x_icon (f, parameters);
6fc2811b
JR
4495
4496 x_make_gc (f);
4497
4498 /* Now consider the frame official. */
4499 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
4500 Vframe_list = Fcons (frame, Vframe_list);
ee78dc32
GV
4501
4502 /* We need to do this after creating the window, so that the
4503 icon-creation functions can say whose icon they're describing. */
52deb19f 4504 x_default_parameter (f, parameters, Qicon_type, Qnil,
6fc2811b 4505 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
ee78dc32 4506
52deb19f 4507 x_default_parameter (f, parameters, Qauto_raise, Qnil,
6fc2811b 4508 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
52deb19f 4509 x_default_parameter (f, parameters, Qauto_lower, Qnil,
6fc2811b 4510 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
52deb19f 4511 x_default_parameter (f, parameters, Qcursor_type, Qbox,
6fc2811b 4512 "cursorType", "CursorType", RES_TYPE_SYMBOL);
52deb19f 4513 x_default_parameter (f, parameters, Qscroll_bar_width, Qnil,
6fc2811b 4514 "scrollBarWidth", "ScrollBarWidth", RES_TYPE_NUMBER);
8b61a891
JR
4515 x_default_parameter (f, parameters, Qalpha, Qnil,
4516 "alpha", "Alpha", RES_TYPE_NUMBER);
ee78dc32 4517
be786000 4518 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
ee78dc32 4519 Change will not be effected unless different from the current
be786000
KS
4520 FRAME_LINES (f). */
4521 width = FRAME_COLS (f);
4522 height = FRAME_LINES (f);
dc220243 4523
be786000
KS
4524 FRAME_LINES (f) = 0;
4525 SET_FRAME_COLS (f, 0);
6fc2811b
JR
4526 change_frame_size (f, height, width, 1, 0, 0);
4527
6fc2811b
JR
4528 /* Tell the server what size and position, etc, we want, and how
4529 badly we want them. This should be done after we have the menu
4530 bar so that its size can be taken into account. */
ee78dc32
GV
4531 BLOCK_INPUT;
4532 x_wm_set_size_hint (f, window_prompting, 0);
4533 UNBLOCK_INPUT;
4534
6fc2811b
JR
4535 /* Make the window appear on the frame and enable display, unless
4536 the caller says not to. However, with explicit parent, Emacs
4537 cannot control visibility, so don't try. */
fbd6baed 4538 if (! f->output_data.w32->explicit_parent)
ee78dc32
GV
4539 {
4540 Lisp_Object visibility;
4541
3faa984f 4542 visibility = x_get_arg (dpyinfo, parameters, Qvisibility, 0, 0, RES_TYPE_SYMBOL);
ee78dc32
GV
4543 if (EQ (visibility, Qunbound))
4544 visibility = Qt;
4545
4546 if (EQ (visibility, Qicon))
4547 x_iconify_frame (f);
4548 else if (! NILP (visibility))
4549 x_make_frame_visible (f);
4550 else
4551 /* Must have been Qnil. */
4552 ;
4553 }
55d5acfa
DN
4554
4555 /* Initialize `default-minibuffer-frame' in case this is the first
4556 frame on this terminal. */
4557 if (FRAME_HAS_MINIBUF_P (f)
4558 && (!FRAMEP (kb->Vdefault_minibuffer_frame)
4559 || !FRAME_LIVE_P (XFRAME (kb->Vdefault_minibuffer_frame))))
4560 kb->Vdefault_minibuffer_frame = frame;
4561
4562 /* All remaining specified parameters, which have not been "used"
4563 by x_get_arg and friends, now go in the misc. alist of the frame. */
99784d63 4564 for (tem = parameters; CONSP (tem); tem = XCDR (tem))
55d5acfa
DN
4565 if (CONSP (XCAR (tem)) && !NILP (XCAR (XCAR (tem))))
4566 f->param_alist = Fcons (XCAR (tem), f->param_alist);
4567
6fc2811b 4568 UNGCPRO;
7d0393cf 4569
9e57df62
GM
4570 /* Make sure windows on this frame appear in calls to next-window
4571 and similar functions. */
4572 Vwindow_list = Qnil;
7d0393cf 4573
ee78dc32
GV
4574 return unbind_to (count, frame);
4575}
4576
4577/* FRAME is used only to get a handle on the X display. We don't pass the
4578 display info directly because we're called from frame.c, which doesn't
4579 know about that structure. */
4580Lisp_Object
4581x_get_focus_frame (frame)
4582 struct frame *frame;
4583{
fbd6baed 4584 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (frame);
ee78dc32 4585 Lisp_Object xfocus;
fbd6baed 4586 if (! dpyinfo->w32_focus_frame)
ee78dc32
GV
4587 return Qnil;
4588
fbd6baed 4589 XSETFRAME (xfocus, dpyinfo->w32_focus_frame);
ee78dc32
GV
4590 return xfocus;
4591}
1edf84e7 4592
334a1195 4593DEFUN ("x-focus-frame", Fx_focus_frame, Sx_focus_frame, 1, 1, 0,
74e1aeec 4594 doc: /* Give FRAME input focus, raising to foreground if necessary. */)
1edf84e7
GV
4595 (frame)
4596 Lisp_Object frame;
4597{
4598 x_focus_on_frame (check_x_frame (frame));
4599 return Qnil;
4600}
4601
ee78dc32 4602\f
6fc2811b 4603DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
74e1aeec 4604 doc: /* Internal function called by `color-defined-p', which see. */)
6fc2811b
JR
4605 (color, frame)
4606 Lisp_Object color, frame;
4607{
4608 XColor foo;
4609 FRAME_PTR f = check_x_frame (frame);
ee78dc32 4610
b7826503 4611 CHECK_STRING (color);
ee78dc32 4612
d5db4077 4613 if (w32_defined_color (f, SDATA (color), &foo, 0))
6fc2811b
JR
4614 return Qt;
4615 else
4616 return Qnil;
4617}
ee78dc32 4618
2d764c78 4619DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
74e1aeec 4620 doc: /* Internal function called by `color-values', which see. */)
ee78dc32
GV
4621 (color, frame)
4622 Lisp_Object color, frame;
4623{
6fc2811b 4624 XColor foo;
ee78dc32
GV
4625 FRAME_PTR f = check_x_frame (frame);
4626
b7826503 4627 CHECK_STRING (color);
ee78dc32 4628
d5db4077 4629 if (w32_defined_color (f, SDATA (color), &foo, 0))
a508663b
KS
4630 return list3 (make_number ((GetRValue (foo.pixel) << 8)
4631 | GetRValue (foo.pixel)),
4632 make_number ((GetGValue (foo.pixel) << 8)
4633 | GetGValue (foo.pixel)),
4634 make_number ((GetBValue (foo.pixel) << 8)
4635 | GetBValue (foo.pixel)));
ee78dc32
GV
4636 else
4637 return Qnil;
4638}
4639
2d764c78 4640DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
74e1aeec 4641 doc: /* Internal function called by `display-color-p', which see. */)
ee78dc32
GV
4642 (display)
4643 Lisp_Object display;
4644{
fbd6baed 4645 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
4646
4647 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 2)
4648 return Qnil;
4649
4650 return Qt;
4651}
4652
74e1aeec
JR
4653DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p,
4654 Sx_display_grayscale_p, 0, 1, 0,
52deb19f 4655 doc: /* Return t if DISPLAY supports shades of gray.
74e1aeec
JR
4656Note that color displays do support shades of gray.
4657The optional argument DISPLAY specifies which display to ask about.
4658DISPLAY should be either a frame or a display name (a string).
4659If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
4660 (display)
4661 Lisp_Object display;
4662{
fbd6baed 4663 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
4664
4665 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 1)
4666 return Qnil;
4667
4668 return Qt;
4669}
4670
74e1aeec
JR
4671DEFUN ("x-display-pixel-width", Fx_display_pixel_width,
4672 Sx_display_pixel_width, 0, 1, 0,
36458ebd 4673 doc: /* Return the width in pixels of DISPLAY.
74e1aeec
JR
4674The optional argument DISPLAY specifies which display to ask about.
4675DISPLAY should be either a frame or a display name (a string).
4676If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
4677 (display)
4678 Lisp_Object display;
4679{
fbd6baed 4680 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32 4681
05eb7cdc 4682 return make_number (x_display_pixel_width (dpyinfo));
ee78dc32
GV
4683}
4684
4685DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
74e1aeec 4686 Sx_display_pixel_height, 0, 1, 0,
36458ebd 4687 doc: /* Return the height in pixels of DISPLAY.
74e1aeec
JR
4688The optional argument DISPLAY specifies which display to ask about.
4689DISPLAY should be either a frame or a display name (a string).
4690If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
4691 (display)
4692 Lisp_Object display;
4693{
fbd6baed 4694 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32 4695
05eb7cdc 4696 return make_number (x_display_pixel_height (dpyinfo));
ee78dc32
GV
4697}
4698
4699DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
74e1aeec 4700 0, 1, 0,
36458ebd 4701 doc: /* Return the number of bitplanes of DISPLAY.
74e1aeec
JR
4702The optional argument DISPLAY specifies which display to ask about.
4703DISPLAY should be either a frame or a display name (a string).
4704If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
4705 (display)
4706 Lisp_Object display;
4707{
fbd6baed 4708 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
4709
4710 return make_number (dpyinfo->n_planes * dpyinfo->n_cbits);
4711}
4712
4713DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
74e1aeec 4714 0, 1, 0,
36458ebd 4715 doc: /* Return the number of color cells of DISPLAY.
74e1aeec
JR
4716The optional argument DISPLAY specifies which display to ask about.
4717DISPLAY should be either a frame or a display name (a string).
4718If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
4719 (display)
4720 Lisp_Object display;
4721{
fbd6baed 4722 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
4723 HDC hdc;
4724 int cap;
4725
5ac45f98
GV
4726 hdc = GetDC (dpyinfo->root_window);
4727 if (dpyinfo->has_palette)
52deb19f 4728 cap = GetDeviceCaps (hdc, SIZEPALETTE);
5ac45f98 4729 else
52deb19f 4730 cap = GetDeviceCaps (hdc, NUMCOLORS);
abf8c61b 4731
007776bc
JB
4732 /* We force 24+ bit depths to 24-bit, both to prevent an overflow
4733 and because probably is more meaningful on Windows anyway */
abf8c61b 4734 if (cap < 0)
74084731 4735 cap = 1 << min (dpyinfo->n_planes * dpyinfo->n_cbits, 24);
7d0393cf 4736
ee78dc32 4737 ReleaseDC (dpyinfo->root_window, hdc);
7d0393cf 4738
ee78dc32
GV
4739 return make_number (cap);
4740}
4741
4742DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
4743 Sx_server_max_request_size,
74e1aeec 4744 0, 1, 0,
36458ebd 4745 doc: /* Return the maximum request size of the server of DISPLAY.
74e1aeec
JR
4746The optional argument DISPLAY specifies which display to ask about.
4747DISPLAY should be either a frame or a display name (a string).
4748If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
4749 (display)
4750 Lisp_Object display;
4751{
fbd6baed 4752 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
4753
4754 return make_number (1);
4755}
4756
4757DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
36458ebd 4758 doc: /* Return the "vendor ID" string of the W32 system (Microsoft).
74e1aeec
JR
4759The optional argument DISPLAY specifies which display to ask about.
4760DISPLAY should be either a frame or a display name (a string).
4761If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
4762 (display)
4763 Lisp_Object display;
4764{
dfff8a69 4765 return build_string ("Microsoft Corp.");
ee78dc32
GV
4766}
4767
4768DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
36458ebd 4769 doc: /* Return the version numbers of the server of DISPLAY.
74e1aeec 4770The value is a list of three integers: the major and minor
02b39a28
JB
4771version numbers of the X Protocol in use, and the distributor-specific
4772release number. See also the function `x-server-vendor'.
74e1aeec
JR
4773
4774The optional argument DISPLAY specifies which display to ask about.
4775DISPLAY should be either a frame or a display name (a string).
4776If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
4777 (display)
4778 Lisp_Object display;
4779{
fbd6baed 4780 return Fcons (make_number (w32_major_version),
58e0f0e4
AI
4781 Fcons (make_number (w32_minor_version),
4782 Fcons (make_number (w32_build_number), Qnil)));
ee78dc32
GV
4783}
4784
4785DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
36458ebd 4786 doc: /* Return the number of screens on the server of DISPLAY.
74e1aeec
JR
4787The optional argument DISPLAY specifies which display to ask about.
4788DISPLAY should be either a frame or a display name (a string).
4789If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
4790 (display)
4791 Lisp_Object display;
4792{
ee78dc32
GV
4793 return make_number (1);
4794}
4795
74e1aeec
JR
4796DEFUN ("x-display-mm-height", Fx_display_mm_height,
4797 Sx_display_mm_height, 0, 1, 0,
36458ebd 4798 doc: /* Return the height in millimeters of DISPLAY.
74e1aeec
JR
4799The optional argument DISPLAY specifies which display to ask about.
4800DISPLAY should be either a frame or a display name (a string).
4801If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
4802 (display)
4803 Lisp_Object display;
4804{
fbd6baed 4805 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
4806 HDC hdc;
4807 int cap;
4808
5ac45f98 4809 hdc = GetDC (dpyinfo->root_window);
7d0393cf 4810
ee78dc32 4811 cap = GetDeviceCaps (hdc, VERTSIZE);
7d0393cf 4812
ee78dc32 4813 ReleaseDC (dpyinfo->root_window, hdc);
7d0393cf 4814
ee78dc32
GV
4815 return make_number (cap);
4816}
4817
4818DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
36458ebd 4819 doc: /* Return the width in millimeters of DISPLAY.
74e1aeec
JR
4820The optional argument DISPLAY specifies which display to ask about.
4821DISPLAY should be either a frame or a display name (a string).
4822If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
4823 (display)
4824 Lisp_Object display;
4825{
fbd6baed 4826 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
4827
4828 HDC hdc;
4829 int cap;
4830
5ac45f98 4831 hdc = GetDC (dpyinfo->root_window);
7d0393cf 4832
ee78dc32 4833 cap = GetDeviceCaps (hdc, HORZSIZE);
7d0393cf 4834
ee78dc32 4835 ReleaseDC (dpyinfo->root_window, hdc);
7d0393cf 4836
ee78dc32
GV
4837 return make_number (cap);
4838}
4839
4840DEFUN ("x-display-backing-store", Fx_display_backing_store,
74e1aeec 4841 Sx_display_backing_store, 0, 1, 0,
36458ebd 4842 doc: /* Return an indication of whether DISPLAY does backing store.
74e1aeec
JR
4843The value may be `always', `when-mapped', or `not-useful'.
4844The optional argument DISPLAY specifies which display to ask about.
4845DISPLAY should be either a frame or a display name (a string).
4846If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
4847 (display)
4848 Lisp_Object display;
4849{
4850 return intern ("not-useful");
4851}
4852
4853DEFUN ("x-display-visual-class", Fx_display_visual_class,
74e1aeec 4854 Sx_display_visual_class, 0, 1, 0,
36458ebd 4855 doc: /* Return the visual class of DISPLAY.
74e1aeec
JR
4856The value is one of the symbols `static-gray', `gray-scale',
4857`static-color', `pseudo-color', `true-color', or `direct-color'.
4858
4859The optional argument DISPLAY specifies which display to ask about.
4860DISPLAY should be either a frame or a display name (a string).
4861If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
4862 (display)
4863 Lisp_Object display;
4864{
fbd6baed 4865 struct w32_display_info *dpyinfo = check_x_display_info (display);
abf8c61b 4866 Lisp_Object result = Qnil;
ee78dc32 4867
abf8c61b
AI
4868 if (dpyinfo->has_palette)
4869 result = intern ("pseudo-color");
4870 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 1)
4871 result = intern ("static-grey");
4872 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 4)
4873 result = intern ("static-color");
4874 else if (dpyinfo->n_planes * dpyinfo->n_cbits > 8)
4875 result = intern ("true-color");
ee78dc32 4876
abf8c61b 4877 return result;
ee78dc32
GV
4878}
4879
4880DEFUN ("x-display-save-under", Fx_display_save_under,
74e1aeec 4881 Sx_display_save_under, 0, 1, 0,
02b39a28 4882 doc: /* Return t if DISPLAY supports the save-under feature.
74e1aeec
JR
4883The optional argument DISPLAY specifies which display to ask about.
4884DISPLAY should be either a frame or a display name (a string).
4885If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
4886 (display)
4887 Lisp_Object display;
4888{
6fc2811b
JR
4889 return Qnil;
4890}
4891\f
4892int
4893x_pixel_width (f)
4894 register struct frame *f;
4895{
be786000 4896 return FRAME_PIXEL_WIDTH (f);
6fc2811b
JR
4897}
4898
4899int
4900x_pixel_height (f)
4901 register struct frame *f;
4902{
be786000 4903 return FRAME_PIXEL_HEIGHT (f);
6fc2811b
JR
4904}
4905
4906int
4907x_char_width (f)
4908 register struct frame *f;
4909{
be786000 4910 return FRAME_COLUMN_WIDTH (f);
6fc2811b
JR
4911}
4912
4913int
4914x_char_height (f)
4915 register struct frame *f;
4916{
be786000 4917 return FRAME_LINE_HEIGHT (f);
6fc2811b
JR
4918}
4919
4920int
4921x_screen_planes (f)
4922 register struct frame *f;
4923{
4924 return FRAME_W32_DISPLAY_INFO (f)->n_planes;
4925}
4926\f
4927/* Return the display structure for the display named NAME.
4928 Open a new connection if necessary. */
4929
4930struct w32_display_info *
4931x_display_info_for_name (name)
4932 Lisp_Object name;
4933{
4934 Lisp_Object names;
4935 struct w32_display_info *dpyinfo;
4936
b7826503 4937 CHECK_STRING (name);
6fc2811b
JR
4938
4939 for (dpyinfo = &one_w32_display_info, names = w32_display_name_list;
4940 dpyinfo;
4941 dpyinfo = dpyinfo->next, names = XCDR (names))
4942 {
4943 Lisp_Object tem;
4944 tem = Fstring_equal (XCAR (XCAR (names)), name);
4945 if (!NILP (tem))
4946 return dpyinfo;
4947 }
4948
4949 /* Use this general default value to start with. */
4950 Vx_resource_name = Vinvocation_name;
4951
4952 validate_x_resource_name ();
4953
4954 dpyinfo = w32_term_init (name, (unsigned char *)0,
d5db4077 4955 (char *) SDATA (Vx_resource_name));
6fc2811b
JR
4956
4957 if (dpyinfo == 0)
d5db4077 4958 error ("Cannot connect to server %s", SDATA (name));
6fc2811b
JR
4959
4960 w32_in_use = 1;
d5272e33 4961 XSETFASTINT (Vwindow_system_version, w32_major_version);
6fc2811b
JR
4962
4963 return dpyinfo;
4964}
4965
4966DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
74e1aeec
JR
4967 1, 3, 0, doc: /* Open a connection to a server.
4968DISPLAY is the name of the display to connect to.
4969Optional second arg XRM-STRING is a string of resources in xrdb format.
4970If the optional third arg MUST-SUCCEED is non-nil,
4971terminate Emacs if we can't open the connection. */)
6fc2811b
JR
4972 (display, xrm_string, must_succeed)
4973 Lisp_Object display, xrm_string, must_succeed;
4974{
4975 unsigned char *xrm_option;
4976 struct w32_display_info *dpyinfo;
4977
74e1aeec
JR
4978 /* If initialization has already been done, return now to avoid
4979 overwriting critical parts of one_w32_display_info. */
4980 if (w32_in_use)
4981 return Qnil;
4982
b7826503 4983 CHECK_STRING (display);
6fc2811b 4984 if (! NILP (xrm_string))
b7826503 4985 CHECK_STRING (xrm_string);
6fc2811b 4986
2dc8b986 4987#if 0
6fc2811b
JR
4988 if (! EQ (Vwindow_system, intern ("w32")))
4989 error ("Not using Microsoft Windows");
2dc8b986 4990#endif
6fc2811b
JR
4991
4992 /* Allow color mapping to be defined externally; first look in user's
4993 HOME directory, then in Emacs etc dir for a file called rgb.txt. */
4994 {
4995 Lisp_Object color_file;
4996 struct gcpro gcpro1;
4997
74084731 4998 color_file = build_string ("~/rgb.txt");
6fc2811b
JR
4999
5000 GCPRO1 (color_file);
5001
5002 if (NILP (Ffile_readable_p (color_file)))
5003 color_file =
5004 Fexpand_file_name (build_string ("rgb.txt"),
5005 Fsymbol_value (intern ("data-directory")));
5006
7ded3383 5007 Vw32_color_map = Fx_load_color_file (color_file);
6fc2811b
JR
5008
5009 UNGCPRO;
5010 }
5011 if (NILP (Vw32_color_map))
5012 Vw32_color_map = Fw32_default_color_map ();
5013
5a8a15ec
JR
5014 /* Merge in system logical colors. */
5015 add_system_logical_colors_to_map (&Vw32_color_map);
5016
6fc2811b 5017 if (! NILP (xrm_string))
d5db4077 5018 xrm_option = (unsigned char *) SDATA (xrm_string);
6fc2811b
JR
5019 else
5020 xrm_option = (unsigned char *) 0;
5021
5022 /* Use this general default value to start with. */
5023 /* First remove .exe suffix from invocation-name - it looks ugly. */
5024 {
5025 char basename[ MAX_PATH ], *str;
5026
d5db4077 5027 strcpy (basename, SDATA (Vinvocation_name));
6fc2811b
JR
5028 str = strrchr (basename, '.');
5029 if (str) *str = 0;
5030 Vinvocation_name = build_string (basename);
5031 }
5032 Vx_resource_name = Vinvocation_name;
5033
5034 validate_x_resource_name ();
5035
5036 /* This is what opens the connection and sets x_current_display.
5037 This also initializes many symbols, such as those used for input. */
5038 dpyinfo = w32_term_init (display, xrm_option,
d5db4077 5039 (char *) SDATA (Vx_resource_name));
6fc2811b
JR
5040
5041 if (dpyinfo == 0)
5042 {
5043 if (!NILP (must_succeed))
5044 fatal ("Cannot connect to server %s.\n",
d5db4077 5045 SDATA (display));
6fc2811b 5046 else
d5db4077 5047 error ("Cannot connect to server %s", SDATA (display));
6fc2811b
JR
5048 }
5049
5050 w32_in_use = 1;
5051
d5272e33 5052 XSETFASTINT (Vwindow_system_version, w32_major_version);
6fc2811b
JR
5053 return Qnil;
5054}
5055
5056DEFUN ("x-close-connection", Fx_close_connection,
5057 Sx_close_connection, 1, 1, 0,
74e1aeec
JR
5058 doc: /* Close the connection to DISPLAY's server.
5059For DISPLAY, specify either a frame or a display name (a string).
5060If DISPLAY is nil, that stands for the selected frame's display. */)
6fc2811b
JR
5061 (display)
5062 Lisp_Object display;
5063{
5064 struct w32_display_info *dpyinfo = check_x_display_info (display);
5065 int i;
5066
5067 if (dpyinfo->reference_count > 0)
5068 error ("Display still has frames on it");
5069
5070 BLOCK_INPUT;
6fc2811b
JR
5071 x_destroy_all_bitmaps (dpyinfo);
5072
5073 x_delete_display (dpyinfo);
5074 UNBLOCK_INPUT;
5075
5076 return Qnil;
5077}
5078
5079DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
74e1aeec 5080 doc: /* Return the list of display names that Emacs has connections to. */)
6fc2811b
JR
5081 ()
5082{
5083 Lisp_Object tail, result;
5084
5085 result = Qnil;
99784d63 5086 for (tail = w32_display_name_list; CONSP (tail); tail = XCDR (tail))
6fc2811b
JR
5087 result = Fcons (XCAR (XCAR (tail)), result);
5088
5089 return result;
5090}
5091
5092DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
0a332240
PJ
5093 doc: /* This is a noop on W32 systems. */)
5094 (on, display)
5095 Lisp_Object display, on;
6fc2811b 5096{
6fc2811b
JR
5097 return Qnil;
5098}
5099
6b61353c 5100
6fc2811b 5101\f
6fc2811b 5102/***********************************************************************
6b61353c 5103 Window properties
6fc2811b
JR
5104 ***********************************************************************/
5105
6b61353c
KH
5106DEFUN ("x-change-window-property", Fx_change_window_property,
5107 Sx_change_window_property, 2, 6, 0,
5108 doc: /* Change window property PROP to VALUE on the X window of FRAME.
5109VALUE may be a string or a list of conses, numbers and/or strings.
5110If an element in the list is a string, it is converted to
5111an Atom and the value of the Atom is used. If an element is a cons,
5112it is converted to a 32 bit number where the car is the 16 top bits and the
5113cdr is the lower 16 bits.
5114FRAME nil or omitted means use the selected frame.
5115If TYPE is given and non-nil, it is the name of the type of VALUE.
5116If TYPE is not given or nil, the type is STRING.
5117FORMAT gives the size in bits of each element if VALUE is a list.
5118It must be one of 8, 16 or 32.
5119If VALUE is a string or FORMAT is nil or not given, FORMAT defaults to 8.
5120If OUTER_P is non-nil, the property is changed for the outer X window of
5121FRAME. Default is to change on the edit X window.
5122
5123Value is VALUE. */)
5124 (prop, value, frame, type, format, outer_p)
5125 Lisp_Object prop, value, frame, type, format, outer_p;
5126{
5127#if 0 /* TODO : port window properties to W32 */
5128 struct frame *f = check_x_frame (frame);
5129 Atom prop_atom;
6fc2811b 5130
6b61353c
KH
5131 CHECK_STRING (prop);
5132 CHECK_STRING (value);
6fc2811b 5133
6b61353c
KH
5134 BLOCK_INPUT;
5135 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
5136 XChangeProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
5137 prop_atom, XA_STRING, 8, PropModeReplace,
5138 SDATA (value), SCHARS (value));
6fc2811b 5139
6b61353c
KH
5140 /* Make sure the property is set when we return. */
5141 XFlush (FRAME_W32_DISPLAY (f));
5142 UNBLOCK_INPUT;
6fc2811b 5143
6b61353c 5144#endif /* TODO */
6fc2811b 5145
6b61353c
KH
5146 return value;
5147}
dfff8a69 5148
6fc2811b 5149
6b61353c
KH
5150DEFUN ("x-delete-window-property", Fx_delete_window_property,
5151 Sx_delete_window_property, 1, 2, 0,
5152 doc: /* Remove window property PROP from X window of FRAME.
5153FRAME nil or omitted means use the selected frame. Value is PROP. */)
5154 (prop, frame)
5155 Lisp_Object prop, frame;
6fc2811b 5156{
6b61353c 5157#if 0 /* TODO : port window properties to W32 */
6fc2811b 5158
6b61353c
KH
5159 struct frame *f = check_x_frame (frame);
5160 Atom prop_atom;
6fc2811b 5161
6b61353c
KH
5162 CHECK_STRING (prop);
5163 BLOCK_INPUT;
5164 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
5165 XDeleteProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), prop_atom);
6fc2811b 5166
6b61353c
KH
5167 /* Make sure the property is removed when we return. */
5168 XFlush (FRAME_W32_DISPLAY (f));
5169 UNBLOCK_INPUT;
5170#endif /* TODO */
6fc2811b 5171
6b61353c 5172 return prop;
6fc2811b
JR
5173}
5174
5175
6b61353c
KH
5176DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
5177 1, 2, 0,
5178 doc: /* Value is the value of window property PROP on FRAME.
5179If FRAME is nil or omitted, use the selected frame. Value is nil
5180if FRAME hasn't a property with name PROP or if PROP has no string
5181value. */)
5182 (prop, frame)
5183 Lisp_Object prop, frame;
6fc2811b 5184{
6b61353c
KH
5185#if 0 /* TODO : port window properties to W32 */
5186
5187 struct frame *f = check_x_frame (frame);
5188 Atom prop_atom;
5189 int rc;
5190 Lisp_Object prop_value = Qnil;
5191 char *tmp_data = NULL;
5192 Atom actual_type;
5193 int actual_format;
5194 unsigned long actual_size, bytes_remaining;
7d0393cf 5195
6b61353c
KH
5196 CHECK_STRING (prop);
5197 BLOCK_INPUT;
5198 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
5199 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
5200 prop_atom, 0, 0, False, XA_STRING,
5201 &actual_type, &actual_format, &actual_size,
5202 &bytes_remaining, (unsigned char **) &tmp_data);
5203 if (rc == Success)
6fc2811b 5204 {
6b61353c 5205 int size = bytes_remaining;
3cf3436e 5206
6b61353c
KH
5207 XFree (tmp_data);
5208 tmp_data = NULL;
3cf3436e 5209
6b61353c
KH
5210 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
5211 prop_atom, 0, bytes_remaining,
5212 False, XA_STRING,
5213 &actual_type, &actual_format,
5214 &actual_size, &bytes_remaining,
5215 (unsigned char **) &tmp_data);
5216 if (rc == Success)
5217 prop_value = make_string (tmp_data, size);
6fc2811b 5218
6b61353c
KH
5219 XFree (tmp_data);
5220 }
6fc2811b 5221
6b61353c 5222 UNBLOCK_INPUT;
6fc2811b 5223
6b61353c 5224 return prop_value;
6fc2811b 5225
6b61353c
KH
5226#endif /* TODO */
5227 return Qnil;
6fc2811b
JR
5228}
5229
5230
5231\f
5232/***********************************************************************
6b61353c 5233 Busy cursor
6fc2811b
JR
5234 ***********************************************************************/
5235
1391cd54
JR
5236/* Default number of seconds to wait before displaying an hourglass
5237 cursor. Duplicated from xdisp.c, but cannot use the version there
5238 due to lack of atimers on w32. */
5239#define DEFAULT_HOURGLASS_DELAY 1
5240extern Lisp_Object Vhourglass_delay;
5241
d148e14d 5242/* Return non-zero if houglass timer has been started or hourglass is shown. */
1885ab29
AR
5243/* PENDING: if W32 can use atimers (atimer.[hc]) then the common impl in
5244 xdisp.c could be used. */
6fc2811b 5245
d148e14d
JR
5246int
5247hourglass_started ()
5248{
5249 return hourglass_shown_p || hourglass_timer;
5250}
6fc2811b 5251
6b61353c 5252/* Cancel a currently active hourglass timer, and start a new one. */
6fc2811b 5253
6b61353c
KH
5254void
5255start_hourglass ()
6fc2811b 5256{
d148e14d
JR
5257 DWORD delay;
5258 int secs, msecs = 0;
5259 struct frame * f = SELECTED_FRAME ();
6fc2811b 5260
30076589
JR
5261 /* No cursors on non GUI frames. */
5262 if (!FRAME_W32_P (f))
5263 return;
5264
6b61353c 5265 cancel_hourglass ();
6fc2811b 5266
6b61353c
KH
5267 if (INTEGERP (Vhourglass_delay)
5268 && XINT (Vhourglass_delay) > 0)
5269 secs = XFASTINT (Vhourglass_delay);
5270 else if (FLOATP (Vhourglass_delay)
5271 && XFLOAT_DATA (Vhourglass_delay) > 0)
6fc2811b 5272 {
6b61353c
KH
5273 Lisp_Object tem;
5274 tem = Ftruncate (Vhourglass_delay, Qnil);
5275 secs = XFASTINT (tem);
d148e14d 5276 msecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000;
6b61353c
KH
5277 }
5278 else
5279 secs = DEFAULT_HOURGLASS_DELAY;
7d0393cf 5280
d148e14d
JR
5281 delay = secs * 1000 + msecs;
5282 hourglass_hwnd = FRAME_W32_WINDOW (f);
5283 hourglass_timer = SetTimer (hourglass_hwnd, HOURGLASS_ID, delay, NULL);
f79e6790
JR
5284}
5285
5286
0af913d7
GM
5287/* Cancel the hourglass cursor timer if active, hide an hourglass
5288 cursor if shown. */
f79e6790
JR
5289
5290void
0af913d7 5291cancel_hourglass ()
f79e6790 5292{
d148e14d 5293 if (hourglass_timer)
dfff8a69 5294 {
d148e14d
JR
5295 KillTimer (hourglass_hwnd, hourglass_timer);
5296 hourglass_timer = 0;
dfff8a69 5297 }
7d0393cf 5298
0af913d7 5299 if (hourglass_shown_p)
1391cd54 5300 w32_hide_hourglass ();
f79e6790
JR
5301}
5302
5303
d148e14d 5304/* Timer function of hourglass_timer.
f79e6790 5305
d148e14d
JR
5306 Display an hourglass cursor. Set the hourglass_p flag in display info
5307 to indicate that an hourglass cursor is shown. */
f79e6790
JR
5308
5309static void
1391cd54 5310w32_show_hourglass (f)
d148e14d 5311 struct frame *f;
6fc2811b 5312{
0af913d7 5313 if (!hourglass_shown_p)
6fc2811b 5314 {
d148e14d
JR
5315 f->output_data.w32->hourglass_p = 1;
5316 if (!menubar_in_use && !current_popup_menu)
5317 SetCursor (f->output_data.w32->hourglass_cursor);
0af913d7 5318 hourglass_shown_p = 1;
f79e6790 5319 }
6fc2811b
JR
5320}
5321
5322
0af913d7 5323/* Hide the hourglass cursor on all frames, if it is currently shown. */
6fc2811b 5324
f79e6790 5325static void
1391cd54 5326w32_hide_hourglass ()
f79e6790 5327{
0af913d7 5328 if (hourglass_shown_p)
6fc2811b 5329 {
d148e14d
JR
5330 struct frame *f = x_window_to_frame (&one_w32_display_info,
5331 hourglass_hwnd);
89e09428
JR
5332 if (f)
5333 f->output_data.w32->hourglass_p = 0;
5334 else
5335 /* If frame was deleted, restore to selected frame's cursor. */
5336 f = SELECTED_FRAME ();
5337
5338 if (FRAME_W32_P (f))
5339 SetCursor (f->output_data.w32->current_cursor);
5340 else
5341 /* No cursors on non GUI frames - restore to stock arrow cursor. */
5342 SetCursor (w32_load_cursor (IDC_ARROW));
6fc2811b 5343
0af913d7 5344 hourglass_shown_p = 0;
f79e6790 5345 }
6fc2811b
JR
5346}
5347
5348
5349\f
5350/***********************************************************************
5351 Tool tips
5352 ***********************************************************************/
5353
5354static Lisp_Object x_create_tip_frame P_ ((struct w32_display_info *,
3cf3436e
JR
5355 Lisp_Object, Lisp_Object));
5356static void compute_tip_xy P_ ((struct frame *, Lisp_Object, Lisp_Object,
5357 Lisp_Object, int, int, int *, int *));
7d0393cf 5358
3cf3436e 5359/* The frame of a currently visible tooltip. */
6fc2811b 5360
937e601e 5361Lisp_Object tip_frame;
6fc2811b
JR
5362
5363/* If non-nil, a timer started that hides the last tooltip when it
5364 fires. */
5365
5366Lisp_Object tip_timer;
5367Window tip_window;
5368
3cf3436e
JR
5369/* If non-nil, a vector of 3 elements containing the last args
5370 with which x-show-tip was called. See there. */
5371
5372Lisp_Object last_show_tip_args;
5373
5374/* Maximum size for tooltips; a cons (COLUMNS . ROWS). */
5375
5376Lisp_Object Vx_max_tooltip_size;
5377
5378
937e601e
AI
5379static Lisp_Object
5380unwind_create_tip_frame (frame)
5381 Lisp_Object frame;
5382{
c844a81a
GM
5383 Lisp_Object deleted;
5384
5385 deleted = unwind_create_frame (frame);
5386 if (EQ (deleted, Qt))
5387 {
5388 tip_window = NULL;
5389 tip_frame = Qnil;
5390 }
7d0393cf 5391
c844a81a 5392 return deleted;
937e601e
AI
5393}
5394
5395
6fc2811b 5396/* Create a frame for a tooltip on the display described by DPYINFO.
3cf3436e
JR
5397 PARMS is a list of frame parameters. TEXT is the string to
5398 display in the tip frame. Value is the frame.
937e601e
AI
5399
5400 Note that functions called here, esp. x_default_parameter can
5401 signal errors, for instance when a specified color name is
5402 undefined. We have to make sure that we're in a consistent state
5403 when this happens. */
6fc2811b
JR
5404
5405static Lisp_Object
3cf3436e 5406x_create_tip_frame (dpyinfo, parms, text)
6fc2811b 5407 struct w32_display_info *dpyinfo;
3cf3436e 5408 Lisp_Object parms, text;
6fc2811b 5409{
6fc2811b
JR
5410 struct frame *f;
5411 Lisp_Object frame, tem;
5412 Lisp_Object name;
5413 long window_prompting = 0;
5414 int width, height;
331379bf 5415 int count = SPECPDL_INDEX ();
6fc2811b
JR
5416 struct gcpro gcpro1, gcpro2, gcpro3;
5417 struct kboard *kb;
3cf3436e
JR
5418 int face_change_count_before = face_change_count;
5419 Lisp_Object buffer;
5420 struct buffer *old_buffer;
6fc2811b 5421
ca56d953 5422 check_w32 ();
6fc2811b
JR
5423
5424 /* Use this general default value to start with until we know if
5425 this frame has a specified name. */
5426 Vx_resource_name = Vinvocation_name;
5427
fd142562 5428 kb = dpyinfo->terminal->kboard;
6fc2811b
JR
5429
5430 /* Get the name of the frame to use for resource lookup. */
3faa984f 5431 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
6fc2811b
JR
5432 if (!STRINGP (name)
5433 && !EQ (name, Qunbound)
5434 && !NILP (name))
5435 error ("Invalid frame name--not a string or nil");
5436 Vx_resource_name = name;
5437
5438 frame = Qnil;
5439 GCPRO3 (parms, name, frame);
9eb16b62
JR
5440 /* Make a frame without minibuffer nor mode-line. */
5441 f = make_frame (0);
5442 f->wants_modeline = 0;
6fc2811b 5443 XSETFRAME (frame, f);
3cf3436e
JR
5444
5445 buffer = Fget_buffer_create (build_string (" *tip*"));
be786000 5446 Fset_window_buffer (FRAME_ROOT_WINDOW (f), buffer, Qnil);
3cf3436e
JR
5447 old_buffer = current_buffer;
5448 set_buffer_internal_1 (XBUFFER (buffer));
5449 current_buffer->truncate_lines = Qnil;
5c2a995d
KH
5450 specbind (Qinhibit_read_only, Qt);
5451 specbind (Qinhibit_modification_hooks, Qt);
3cf3436e
JR
5452 Ferase_buffer ();
5453 Finsert (1, &text);
5454 set_buffer_internal_1 (old_buffer);
7d0393cf 5455
6fc2811b 5456 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
937e601e 5457 record_unwind_protect (unwind_create_tip_frame, frame);
6fc2811b 5458
3cf3436e
JR
5459 /* By setting the output method, we're essentially saying that
5460 the frame is live, as per FRAME_LIVE_P. If we get a signal
5461 from this point on, x_destroy_window might screw up reference
5462 counts etc. */
ebbb61be
JR
5463 f->terminal = dpyinfo->terminal;
5464 f->terminal->reference_count++;
d88c567c 5465 f->output_method = output_w32;
6fc2811b
JR
5466 f->output_data.w32 =
5467 (struct w32_output *) xmalloc (sizeof (struct w32_output));
5468 bzero (f->output_data.w32, sizeof (struct w32_output));
ca56d953
JR
5469
5470 FRAME_FONTSET (f) = -1;
6fc2811b
JR
5471 f->icon_name = Qnil;
5472
5f004711 5473#if GLYPH_DEBUG
354884c4 5474 image_cache_refcount = FRAME_IMAGE_CACHE (f)->refcount;
937e601e
AI
5475 dpyinfo_refcount = dpyinfo->reference_count;
5476#endif /* GLYPH_DEBUG */
6fc2811b 5477 FRAME_KBOARD (f) = kb;
6fc2811b
JR
5478 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
5479 f->output_data.w32->explicit_parent = 0;
5480
5481 /* Set the name; the functions to which we pass f expect the name to
5482 be set. */
5483 if (EQ (name, Qunbound) || NILP (name))
5484 {
ca56d953 5485 f->name = build_string (dpyinfo->w32_id_name);
6fc2811b
JR
5486 f->explicit_name = 0;
5487 }
5488 else
5489 {
5490 f->name = name;
5491 f->explicit_name = 1;
5492 /* use the frame's title when getting resources for this frame. */
5493 specbind (Qx_resource_name, name);
5494 }
5495
82523155
JR
5496 f->resx = dpyinfo->resx;
5497 f->resy = dpyinfo->resy;
5498
fdb55376
JR
5499 if (uniscribe_available)
5500 register_font_driver (&uniscribe_font_driver, f);
1cc06b86 5501 register_font_driver (&w32font_driver, f);
a1fe5c00 5502
1cc06b86
KH
5503 x_default_parameter (f, parms, Qfont_backend, Qnil,
5504 "fontBackend", "FontBackend", RES_TYPE_STRING);
a1fe5c00 5505
6fc2811b
JR
5506 /* Extract the window parameters from the supplied values
5507 that are needed to determine window geometry. */
1cc06b86 5508 x_default_font_parameter (f, parms);
6fc2811b
JR
5509
5510 x_default_parameter (f, parms, Qborder_width, make_number (2),
5511 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
6fc2811b
JR
5512 /* This defaults to 2 in order to match xterm. We recognize either
5513 internalBorderWidth or internalBorder (which is what xterm calls
5514 it). */
5515 if (NILP (Fassq (Qinternal_border_width, parms)))
5516 {
5517 Lisp_Object value;
5518
3faa984f 5519 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
6fc2811b
JR
5520 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
5521 if (! EQ (value, Qunbound))
5522 parms = Fcons (Fcons (Qinternal_border_width, value),
5523 parms);
5524 }
bfd6edcc 5525 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
6fc2811b
JR
5526 "internalBorderWidth", "internalBorderWidth",
5527 RES_TYPE_NUMBER);
5528
5529 /* Also do the stuff which must be set before the window exists. */
5530 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
5531 "foreground", "Foreground", RES_TYPE_STRING);
5532 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
5533 "background", "Background", RES_TYPE_STRING);
5534 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
5535 "pointerColor", "Foreground", RES_TYPE_STRING);
5536 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
5537 "cursorColor", "Foreground", RES_TYPE_STRING);
5538 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
5539 "borderColor", "BorderColor", RES_TYPE_STRING);
5540
5541 /* Init faces before x_default_parameter is called for scroll-bar
5542 parameters because that function calls x_set_scroll_bar_width,
5543 which calls change_frame_size, which calls Fset_window_buffer,
5544 which runs hooks, which call Fvertical_motion. At the end, we
5545 end up in init_iterator with a null face cache, which should not
5546 happen. */
5547 init_frame_faces (f);
ca56d953
JR
5548
5549 f->output_data.w32->dwStyle = WS_BORDER | WS_POPUP | WS_DISABLED;
6fc2811b 5550 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
9eb16b62 5551
6d906347 5552 window_prompting = x_figure_window_size (f, parms, 0);
6fc2811b 5553
9eb16b62 5554 /* No fringes on tip frame. */
be786000
KS
5555 f->fringe_cols = 0;
5556 f->left_fringe_width = 0;
5557 f->right_fringe_width = 0;
9eb16b62 5558
ca56d953
JR
5559 BLOCK_INPUT;
5560 my_create_tip_window (f);
5561 UNBLOCK_INPUT;
6fc2811b
JR
5562
5563 x_make_gc (f);
5564
5565 x_default_parameter (f, parms, Qauto_raise, Qnil,
5566 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5567 x_default_parameter (f, parms, Qauto_lower, Qnil,
5568 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5569 x_default_parameter (f, parms, Qcursor_type, Qbox,
5570 "cursorType", "CursorType", RES_TYPE_SYMBOL);
5571
be786000 5572 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
6fc2811b 5573 Change will not be effected unless different from the current
be786000
KS
5574 FRAME_LINES (f). */
5575 width = FRAME_COLS (f);
5576 height = FRAME_LINES (f);
5577 FRAME_LINES (f) = 0;
5578 SET_FRAME_COLS (f, 0);
6fc2811b
JR
5579 change_frame_size (f, height, width, 1, 0, 0);
5580
cd1d850f
JPW
5581 /* Add `tooltip' frame parameter's default value. */
5582 if (NILP (Fframe_parameter (frame, intern ("tooltip"))))
5583 Fmodify_frame_parameters (frame, Fcons (Fcons (intern ("tooltip"), Qt),
5584 Qnil));
7d0393cf 5585
3cf3436e
JR
5586 /* Set up faces after all frame parameters are known. This call
5587 also merges in face attributes specified for new frames.
5588
5589 Frame parameters may be changed if .Xdefaults contains
5590 specifications for the default font. For example, if there is an
5591 `Emacs.default.attributeBackground: pink', the `background-color'
5592 attribute of the frame get's set, which let's the internal border
5593 of the tooltip frame appear in pink. Prevent this. */
5594 {
5595 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
601a9cf1
JB
5596 Lisp_Object fg = Fframe_parameter (frame, Qforeground_color);
5597 Lisp_Object colors = Qnil;
3cf3436e
JR
5598
5599 /* Set tip_frame here, so that */
5600 tip_frame = frame;
45cebfd9 5601 call2 (Qface_set_after_frame_default, frame, Qnil);
7d0393cf 5602
3cf3436e 5603 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
601a9cf1
JB
5604 colors = Fcons (Fcons (Qbackground_color, bg), colors);
5605 if (!EQ (fg, Fframe_parameter (frame, Qforeground_color)))
5606 colors = Fcons (Fcons (Qforeground_color, fg), colors);
5607
5608 if (!NILP (colors))
5609 Fmodify_frame_parameters (frame, colors);
3cf3436e 5610 }
7d0393cf 5611
6fc2811b
JR
5612 f->no_split = 1;
5613
5614 UNGCPRO;
5615
5616 /* It is now ok to make the frame official even if we get an error
5617 below. And the frame needs to be on Vframe_list or making it
5618 visible won't work. */
5619 Vframe_list = Fcons (frame, Vframe_list);
5620
5621 /* Now that the frame is official, it counts as a reference to
5622 its display. */
5623 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
ee78dc32 5624
3cf3436e
JR
5625 /* Setting attributes of faces of the tooltip frame from resources
5626 and similar will increment face_change_count, which leads to the
5627 clearing of all current matrices. Since this isn't necessary
5628 here, avoid it by resetting face_change_count to the value it
5629 had before we created the tip frame. */
5630 face_change_count = face_change_count_before;
5631
5632 /* Discard the unwind_protect. */
6fc2811b 5633 return unbind_to (count, frame);
ee78dc32
GV
5634}
5635
3cf3436e
JR
5636
5637/* Compute where to display tip frame F. PARMS is the list of frame
5638 parameters for F. DX and DY are specified offsets from the current
5639 location of the mouse. WIDTH and HEIGHT are the width and height
5640 of the tooltip. Return coordinates relative to the root window of
5641 the display in *ROOT_X, and *ROOT_Y. */
5642
5643static void
5644compute_tip_xy (f, parms, dx, dy, width, height, root_x, root_y)
5645 struct frame *f;
5646 Lisp_Object parms, dx, dy;
5647 int width, height;
5648 int *root_x, *root_y;
5649{
3cf3436e 5650 Lisp_Object left, top;
64f0809d 5651 int min_x, min_y, max_x, max_y;
7d0393cf 5652
3cf3436e
JR
5653 /* User-specified position? */
5654 left = Fcdr (Fassq (Qleft, parms));
5655 top = Fcdr (Fassq (Qtop, parms));
7d0393cf 5656
3cf3436e
JR
5657 /* Move the tooltip window where the mouse pointer is. Resize and
5658 show it. */
ca56d953 5659 if (!INTEGERP (left) || !INTEGERP (top))
3cf3436e 5660 {
ca56d953
JR
5661 POINT pt;
5662
64f0809d
JR
5663 /* Default min and max values. */
5664 min_x = 0;
5665 min_y = 0;
05eb7cdc
JB
5666 max_x = x_display_pixel_width (FRAME_W32_DISPLAY_INFO (f));
5667 max_y = x_display_pixel_height (FRAME_W32_DISPLAY_INFO (f));
64f0809d 5668
3cf3436e 5669 BLOCK_INPUT;
ca56d953
JR
5670 GetCursorPos (&pt);
5671 *root_x = pt.x;
5672 *root_y = pt.y;
3cf3436e 5673 UNBLOCK_INPUT;
64f0809d
JR
5674
5675 /* If multiple monitor support is available, constrain the tip onto
5676 the current monitor. This improves the above by allowing negative
5677 co-ordinates if monitor positions are such that they are valid, and
5678 snaps a tooltip onto a single monitor if we are close to the edge
5679 where it would otherwise flow onto the other monitor (or into
5680 nothingness if there is a gap in the overlap). */
5681 if (monitor_from_point_fn && get_monitor_info_fn)
5682 {
5683 struct MONITOR_INFO info;
5684 HMONITOR monitor
5685 = monitor_from_point_fn (pt, MONITOR_DEFAULT_TO_NEAREST);
5686 info.cbSize = sizeof (info);
5687
5688 if (get_monitor_info_fn (monitor, &info))
5689 {
5690 min_x = info.rcWork.left;
5691 min_y = info.rcWork.top;
5692 max_x = info.rcWork.right;
5693 max_y = info.rcWork.bottom;
5694 }
5695 }
3cf3436e
JR
5696 }
5697
5698 if (INTEGERP (top))
5699 *root_y = XINT (top);
64f0809d
JR
5700 else if (*root_y + XINT (dy) <= min_y)
5701 *root_y = min_y; /* Can happen for negative dy */
5702 else if (*root_y + XINT (dy) + height <= max_y)
7e8410d1 5703 /* It fits below the pointer */
3cf3436e 5704 *root_y += XINT (dy);
64f0809d 5705 else if (height + XINT (dy) + min_y <= *root_y)
7e8410d1
JD
5706 /* It fits above the pointer. */
5707 *root_y -= height + XINT (dy);
5708 else
5709 /* Put it on the top. */
64f0809d 5710 *root_y = min_y;
3cf3436e
JR
5711
5712 if (INTEGERP (left))
5713 *root_x = XINT (left);
64f0809d 5714 else if (*root_x + XINT (dx) <= min_x)
bf63eb69 5715 *root_x = 0; /* Can happen for negative dx */
64f0809d 5716 else if (*root_x + XINT (dx) + width <= max_x)
72e4adef
JR
5717 /* It fits to the right of the pointer. */
5718 *root_x += XINT (dx);
64f0809d 5719 else if (width + XINT (dx) + min_x <= *root_x)
72e4adef 5720 /* It fits to the left of the pointer. */
3cf3436e
JR
5721 *root_x -= width + XINT (dx);
5722 else
72e4adef 5723 /* Put it left justified on the screen -- it ought to fit that way. */
64f0809d 5724 *root_x = min_x;
3cf3436e
JR
5725}
5726
5727
71eab8d1 5728DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
74e1aeec
JR
5729 doc: /* Show STRING in a \"tooltip\" window on frame FRAME.
5730A tooltip window is a small window displaying a string.
5731
87c891c7
EZ
5732This is an internal function; Lisp code should call `tooltip-show'.
5733
74e1aeec
JR
5734FRAME nil or omitted means use the selected frame.
5735
5736PARMS is an optional list of frame parameters which can be
5737used to change the tooltip's appearance.
5738
ca56d953
JR
5739Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
5740means use the default timeout of 5 seconds.
74e1aeec 5741
52deb19f 5742If the list of frame parameters PARMS contains a `left' parameter,
74e1aeec
JR
5743the tooltip is displayed at that x-position. Otherwise it is
5744displayed at the mouse position, with offset DX added (default is 5 if
5745DX isn't specified). Likewise for the y-position; if a `top' frame
5746parameter is specified, it determines the y-position of the tooltip
5747window, otherwise it is displayed at the mouse position, with offset
5748DY added (default is -10).
5749
5750A tooltip's maximum size is specified by `x-max-tooltip-size'.
5751Text larger than the specified size is clipped. */)
71eab8d1
AI
5752 (string, frame, parms, timeout, dx, dy)
5753 Lisp_Object string, frame, parms, timeout, dx, dy;
ee78dc32 5754{
6fc2811b
JR
5755 struct frame *f;
5756 struct window *w;
3cf3436e 5757 int root_x, root_y;
6fc2811b
JR
5758 struct buffer *old_buffer;
5759 struct text_pos pos;
5760 int i, width, height;
6fc2811b
JR
5761 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5762 int old_windows_or_buffers_changed = windows_or_buffers_changed;
331379bf 5763 int count = SPECPDL_INDEX ();
7d0393cf 5764
6fc2811b 5765 specbind (Qinhibit_redisplay, Qt);
ee78dc32 5766
dfff8a69 5767 GCPRO4 (string, parms, frame, timeout);
ee78dc32 5768
b7826503 5769 CHECK_STRING (string);
6fc2811b
JR
5770 f = check_x_frame (frame);
5771 if (NILP (timeout))
5772 timeout = make_number (5);
5773 else
b7826503 5774 CHECK_NATNUM (timeout);
ee78dc32 5775
71eab8d1
AI
5776 if (NILP (dx))
5777 dx = make_number (5);
5778 else
b7826503 5779 CHECK_NUMBER (dx);
7d0393cf 5780
71eab8d1 5781 if (NILP (dy))
dc220243 5782 dy = make_number (-10);
71eab8d1 5783 else
b7826503 5784 CHECK_NUMBER (dy);
71eab8d1 5785
dc220243
JR
5786 if (NILP (last_show_tip_args))
5787 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
5788
5789 if (!NILP (tip_frame))
5790 {
5791 Lisp_Object last_string = AREF (last_show_tip_args, 0);
5792 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
5793 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
5794
5795 if (EQ (frame, last_frame)
5796 && !NILP (Fequal (last_string, string))
5797 && !NILP (Fequal (last_parms, parms)))
5798 {
5799 struct frame *f = XFRAME (tip_frame);
7d0393cf 5800
dc220243
JR
5801 /* Only DX and DY have changed. */
5802 if (!NILP (tip_timer))
5803 {
5804 Lisp_Object timer = tip_timer;
5805 tip_timer = Qnil;
5806 call1 (Qcancel_timer, timer);
5807 }
5808
5809 BLOCK_INPUT;
be786000
KS
5810 compute_tip_xy (f, parms, dx, dy, FRAME_PIXEL_WIDTH (f),
5811 FRAME_PIXEL_HEIGHT (f), &root_x, &root_y);
d65a9cdc
JR
5812
5813 /* Put tooltip in topmost group and in position. */
ca56d953
JR
5814 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
5815 root_x, root_y, 0, 0,
5816 SWP_NOSIZE | SWP_NOACTIVATE);
d65a9cdc
JR
5817
5818 /* Ensure tooltip is on top of other topmost windows (eg menus). */
5819 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
5820 0, 0, 0, 0,
5821 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
5822
dc220243
JR
5823 UNBLOCK_INPUT;
5824 goto start_timer;
5825 }
5826 }
5827
6fc2811b
JR
5828 /* Hide a previous tip, if any. */
5829 Fx_hide_tip ();
ee78dc32 5830
dc220243
JR
5831 ASET (last_show_tip_args, 0, string);
5832 ASET (last_show_tip_args, 1, frame);
5833 ASET (last_show_tip_args, 2, parms);
5834
6fc2811b
JR
5835 /* Add default values to frame parameters. */
5836 if (NILP (Fassq (Qname, parms)))
5837 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
5838 if (NILP (Fassq (Qinternal_border_width, parms)))
5839 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
5840 if (NILP (Fassq (Qborder_width, parms)))
5841 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
5842 if (NILP (Fassq (Qborder_color, parms)))
5843 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
5844 if (NILP (Fassq (Qbackground_color, parms)))
5845 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
5846 parms);
5847
0e3fcdef
JR
5848 /* Block input until the tip has been fully drawn, to avoid crashes
5849 when drawing tips in menus. */
5850 BLOCK_INPUT;
5851
6fc2811b
JR
5852 /* Create a frame for the tooltip, and record it in the global
5853 variable tip_frame. */
ca56d953 5854 frame = x_create_tip_frame (FRAME_W32_DISPLAY_INFO (f), parms, string);
937e601e 5855 f = XFRAME (frame);
6fc2811b 5856
3cf3436e 5857 /* Set up the frame's root window. */
6fc2811b 5858 w = XWINDOW (FRAME_ROOT_WINDOW (f));
be786000 5859 w->left_col = w->top_line = make_number (0);
3cf3436e
JR
5860
5861 if (CONSP (Vx_max_tooltip_size)
5862 && INTEGERP (XCAR (Vx_max_tooltip_size))
5863 && XINT (XCAR (Vx_max_tooltip_size)) > 0
5864 && INTEGERP (XCDR (Vx_max_tooltip_size))
5865 && XINT (XCDR (Vx_max_tooltip_size)) > 0)
5866 {
be786000
KS
5867 w->total_cols = XCAR (Vx_max_tooltip_size);
5868 w->total_lines = XCDR (Vx_max_tooltip_size);
3cf3436e
JR
5869 }
5870 else
5871 {
be786000
KS
5872 w->total_cols = make_number (80);
5873 w->total_lines = make_number (40);
3cf3436e 5874 }
7d0393cf 5875
be786000 5876 FRAME_TOTAL_COLS (f) = XINT (w->total_cols);
6fc2811b
JR
5877 adjust_glyphs (f);
5878 w->pseudo_window_p = 1;
5879
5880 /* Display the tooltip text in a temporary buffer. */
6fc2811b 5881 old_buffer = current_buffer;
3cf3436e
JR
5882 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->buffer));
5883 current_buffer->truncate_lines = Qnil;
6fc2811b
JR
5884 clear_glyph_matrix (w->desired_matrix);
5885 clear_glyph_matrix (w->current_matrix);
5886 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
29e95254 5887 try_window (FRAME_ROOT_WINDOW (f), pos, 0);
6fc2811b
JR
5888
5889 /* Compute width and height of the tooltip. */
5890 width = height = 0;
5891 for (i = 0; i < w->desired_matrix->nrows; ++i)
ee78dc32 5892 {
6fc2811b
JR
5893 struct glyph_row *row = &w->desired_matrix->rows[i];
5894 struct glyph *last;
5895 int row_width;
5896
5897 /* Stop at the first empty row at the end. */
5898 if (!row->enabled_p || !row->displays_text_p)
5899 break;
5900
5901 /* Let the row go over the full width of the frame. */
5902 row->full_width_p = 1;
5903
4e3a1c61
JR
5904#ifdef TODO /* Investigate why some fonts need more width than is
5905 calculated for some tooltips. */
6fc2811b
JR
5906 /* There's a glyph at the end of rows that is use to place
5907 the cursor there. Don't include the width of this glyph. */
5908 if (row->used[TEXT_AREA])
5909 {
5910 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5911 row_width = row->pixel_width - last->pixel_width;
5912 }
5913 else
4e3a1c61 5914#endif
6fc2811b 5915 row_width = row->pixel_width;
7d0393cf 5916
ca56d953 5917 /* TODO: find why tips do not draw along baseline as instructed. */
bfd6edcc 5918 height += row->height;
6fc2811b 5919 width = max (width, row_width);
ee78dc32
GV
5920 }
5921
6fc2811b
JR
5922 /* Add the frame's internal border to the width and height the X
5923 window should have. */
5924 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5925 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
ee78dc32 5926
6fc2811b
JR
5927 /* Move the tooltip window where the mouse pointer is. Resize and
5928 show it. */
3cf3436e 5929 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
71eab8d1 5930
bfd6edcc
JR
5931 {
5932 /* Adjust Window size to take border into account. */
5933 RECT rect;
5934 rect.left = rect.top = 0;
5935 rect.right = width;
5936 rect.bottom = height;
5937 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
5938 FRAME_EXTERNAL_MENU_BAR (f));
5939
1d79e521
EZ
5940 /* Position and size tooltip, and put it in the topmost group.
5941 The add-on of 3 to the 5th argument is a kludge: without it,
5942 some fonts cause the last character of the tip to be truncated,
5943 for some obscure reason. */
bfd6edcc 5944 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
1d79e521 5945 root_x, root_y, rect.right - rect.left + 3,
bfd6edcc
JR
5946 rect.bottom - rect.top, SWP_NOACTIVATE);
5947
d65a9cdc
JR
5948 /* Ensure tooltip is on top of other topmost windows (eg menus). */
5949 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
5950 0, 0, 0, 0,
5951 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
5952
bfd6edcc
JR
5953 /* Let redisplay know that we have made the frame visible already. */
5954 f->async_visible = 1;
5955
5956 ShowWindow (FRAME_W32_WINDOW (f), SW_SHOWNOACTIVATE);
5957 }
ee78dc32 5958
6fc2811b
JR
5959 /* Draw into the window. */
5960 w->must_be_updated_p = 1;
5961 update_single_window (w, 1);
ee78dc32 5962
0e3fcdef
JR
5963 UNBLOCK_INPUT;
5964
6fc2811b
JR
5965 /* Restore original current buffer. */
5966 set_buffer_internal_1 (old_buffer);
5967 windows_or_buffers_changed = old_windows_or_buffers_changed;
ee78dc32 5968
dc220243 5969 start_timer:
6fc2811b
JR
5970 /* Let the tip disappear after timeout seconds. */
5971 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
5972 intern ("x-hide-tip"));
ee78dc32 5973
dfff8a69 5974 UNGCPRO;
6fc2811b 5975 return unbind_to (count, Qnil);
ee78dc32
GV
5976}
5977
ee78dc32 5978
6fc2811b 5979DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
74e1aeec
JR
5980 doc: /* Hide the current tooltip window, if there is any.
5981Value is t if tooltip was open, nil otherwise. */)
6fc2811b
JR
5982 ()
5983{
937e601e
AI
5984 int count;
5985 Lisp_Object deleted, frame, timer;
5986 struct gcpro gcpro1, gcpro2;
5987
5988 /* Return quickly if nothing to do. */
5989 if (NILP (tip_timer) && NILP (tip_frame))
5990 return Qnil;
7d0393cf 5991
937e601e
AI
5992 frame = tip_frame;
5993 timer = tip_timer;
5994 GCPRO2 (frame, timer);
5995 tip_frame = tip_timer = deleted = Qnil;
7d0393cf 5996
331379bf 5997 count = SPECPDL_INDEX ();
6fc2811b 5998 specbind (Qinhibit_redisplay, Qt);
937e601e 5999 specbind (Qinhibit_quit, Qt);
7d0393cf 6000
937e601e 6001 if (!NILP (timer))
dc220243 6002 call1 (Qcancel_timer, timer);
ee78dc32 6003
937e601e 6004 if (FRAMEP (frame))
6fc2811b 6005 {
56f2de10 6006 delete_frame (frame, Qnil);
937e601e 6007 deleted = Qt;
6fc2811b 6008 }
1edf84e7 6009
937e601e
AI
6010 UNGCPRO;
6011 return unbind_to (count, deleted);
6fc2811b 6012}
5ac45f98 6013
5ac45f98 6014
6fc2811b
JR
6015\f
6016/***********************************************************************
6017 File selection dialog
6018 ***********************************************************************/
6fc2811b
JR
6019extern Lisp_Object Qfile_name_history;
6020
fffa137c 6021/* Callback for altering the behavior of the Open File dialog.
1030b26b
JR
6022 Makes the Filename text field contain "Current Directory" and be
6023 read-only when "Directories" is selected in the filter. This
6024 allows us to work around the fact that the standard Open File
6025 dialog does not support directories. */
6026UINT CALLBACK
6027file_dialog_callback (hwnd, msg, wParam, lParam)
6028 HWND hwnd;
6029 UINT msg;
6030 WPARAM wParam;
6031 LPARAM lParam;
6032{
6033 if (msg == WM_NOTIFY)
6034 {
6035 OFNOTIFY * notify = (OFNOTIFY *)lParam;
6036 /* Detect when the Filter dropdown is changed. */
ef544dc8
JR
6037 if (notify->hdr.code == CDN_TYPECHANGE
6038 || notify->hdr.code == CDN_INITDONE)
1030b26b
JR
6039 {
6040 HWND dialog = GetParent (hwnd);
6041 HWND edit_control = GetDlgItem (dialog, FILE_NAME_TEXT_FIELD);
6042
6043 /* Directories is in index 2. */
6044 if (notify->lpOFN->nFilterIndex == 2)
6045 {
6046 CommDlg_OpenSave_SetControlText (dialog, FILE_NAME_TEXT_FIELD,
6047 "Current Directory");
6048 EnableWindow (edit_control, FALSE);
6049 }
6050 else
6051 {
ef544dc8
JR
6052 /* Don't override default filename on init done. */
6053 if (notify->hdr.code == CDN_TYPECHANGE)
6054 CommDlg_OpenSave_SetControlText (dialog,
6055 FILE_NAME_TEXT_FIELD, "");
1030b26b
JR
6056 EnableWindow (edit_control, TRUE);
6057 }
6058 }
6059 }
6060 return 0;
6061}
6062
7f2b4738
JR
6063/* Since we compile with _WIN32_WINNT set to 0x0400 (for NT4 compatibility)
6064 we end up with the old file dialogs. Define a big enough struct for the
6065 new dialog to trick GetOpenFileName into giving us the new dialogs on
6066 Windows 2000 and XP. */
6067typedef struct
6068{
6069 OPENFILENAME real_details;
6070 void * pReserved;
6071 DWORD dwReserved;
6072 DWORD FlagsEx;
6073} NEWOPENFILENAME;
6074
21517c3d 6075
f9d64bb3 6076DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
74e1aeec
JR
6077 doc: /* Read file name, prompting with PROMPT in directory DIR.
6078Use a file selection dialog.
6079Select DEFAULT-FILENAME in the dialog's file selection box, if
f9d64bb3
JD
6080specified. Ensure that file exists if MUSTMATCH is non-nil.
6081If ONLY-DIR-P is non-nil, the user can only select directories. */)
6082 (prompt, dir, default_filename, mustmatch, only_dir_p)
6083 Lisp_Object prompt, dir, default_filename, mustmatch, only_dir_p;
6fc2811b
JR
6084{
6085 struct frame *f = SELECTED_FRAME ();
6086 Lisp_Object file = Qnil;
aed13378 6087 int count = SPECPDL_INDEX ();
f9d64bb3 6088 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
6fc2811b
JR
6089 char filename[MAX_PATH + 1];
6090 char init_dir[MAX_PATH + 1];
ef544dc8 6091 int default_filter_index = 1; /* 1: All Files, 2: Directories only */
6fc2811b 6092
f9d64bb3 6093 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
b7826503
PJ
6094 CHECK_STRING (prompt);
6095 CHECK_STRING (dir);
6fc2811b
JR
6096
6097 /* Create the dialog with PROMPT as title, using DIR as initial
6098 directory and using "*" as pattern. */
6099 dir = Fexpand_file_name (dir, Qnil);
dda741ec 6100 strncpy (init_dir, SDATA (ENCODE_FILE (dir)), MAX_PATH);
6fc2811b
JR
6101 init_dir[MAX_PATH] = '\0';
6102 unixtodos_filename (init_dir);
6103
6104 if (STRINGP (default_filename))
6105 {
6106 char *file_name_only;
dda741ec 6107 char *full_path_name = SDATA (ENCODE_FILE (default_filename));
5ac45f98 6108
6fc2811b 6109 unixtodos_filename (full_path_name);
5ac45f98 6110
6fc2811b
JR
6111 file_name_only = strrchr (full_path_name, '\\');
6112 if (!file_name_only)
6113 file_name_only = full_path_name;
6114 else
ef544dc8 6115 file_name_only++;
ee78dc32 6116
6fc2811b
JR
6117 strncpy (filename, file_name_only, MAX_PATH);
6118 filename[MAX_PATH] = '\0';
6119 }
ee78dc32 6120 else
6fc2811b 6121 filename[0] = '\0';
ee78dc32 6122
1030b26b 6123 {
7f2b4738 6124 NEWOPENFILENAME new_file_details;
ba6f3859 6125 BOOL file_opened = FALSE;
7f2b4738 6126 OPENFILENAME * file_details = &new_file_details.real_details;
21517c3d 6127
1030b26b
JR
6128 /* Prevent redisplay. */
6129 specbind (Qinhibit_redisplay, Qt);
6130 BLOCK_INPUT;
ee78dc32 6131
7f2b4738
JR
6132 bzero (&new_file_details, sizeof (new_file_details));
6133 /* Apparently NT4 crashes if you give it an unexpected size.
6134 I'm not sure about Windows 9x, so play it safe. */
6135 if (w32_major_version > 4 && w32_major_version < 95)
843d2458 6136 file_details->lStructSize = sizeof (NEWOPENFILENAME);
7f2b4738 6137 else
843d2458 6138 file_details->lStructSize = sizeof (OPENFILENAME);
7f2b4738
JR
6139
6140 file_details->hwndOwner = FRAME_W32_WINDOW (f);
1030b26b
JR
6141 /* Undocumented Bug in Common File Dialog:
6142 If a filter is not specified, shell links are not resolved. */
7f2b4738
JR
6143 file_details->lpstrFilter = "All Files (*.*)\0*.*\0Directories\0*|*\0\0";
6144 file_details->lpstrFile = filename;
6145 file_details->nMaxFile = sizeof (filename);
6146 file_details->lpstrInitialDir = init_dir;
6147 file_details->lpstrTitle = SDATA (prompt);
ef544dc8 6148
f9d64bb3 6149 if (! NILP (only_dir_p))
ef544dc8
JR
6150 default_filter_index = 2;
6151
7f2b4738 6152 file_details->nFilterIndex = default_filter_index;
ef544dc8 6153
7f2b4738 6154 file_details->Flags = (OFN_HIDEREADONLY | OFN_NOCHANGEDIR
1030b26b
JR
6155 | OFN_EXPLORER | OFN_ENABLEHOOK);
6156 if (!NILP (mustmatch))
7f2b4738
JR
6157 {
6158 /* Require that the path to the parent directory exists. */
6159 file_details->Flags |= OFN_PATHMUSTEXIST;
6160 /* If we are looking for a file, require that it exists. */
6161 if (NILP (only_dir_p))
6162 file_details->Flags |= OFN_FILEMUSTEXIST;
6163 }
1030b26b 6164
7f2b4738 6165 file_details->lpfnHook = (LPOFNHOOKPROC) file_dialog_callback;
1030b26b 6166
7f2b4738 6167 file_opened = GetOpenFileName (file_details);
1030b26b 6168
ba6f3859
JR
6169 UNBLOCK_INPUT;
6170
6171 if (file_opened)
1030b26b
JR
6172 {
6173 dostounix_filename (filename);
7f2b4738
JR
6174
6175 if (file_details->nFilterIndex == 2)
1030b26b 6176 {
ef544dc8 6177 /* "Directories" selected - strip dummy file name. */
1030b26b
JR
6178 char * last = strrchr (filename, '/');
6179 *last = '\0';
6180 }
6fc2811b 6181
74084731 6182 file = DECODE_FILE (build_string (filename));
1030b26b
JR
6183 }
6184 /* User cancelled the dialog without making a selection. */
6185 else if (!CommDlgExtendedError ())
6186 file = Qnil;
6187 /* An error occurred, fallback on reading from the mini-buffer. */
6188 else
6189 file = Fcompleting_read (prompt, intern ("read-file-name-internal"),
6190 dir, mustmatch, dir, Qfile_name_history,
6191 default_filename, Qnil);
6192
1030b26b
JR
6193 file = unbind_to (count, file);
6194 }
ee78dc32 6195
6fc2811b 6196 UNGCPRO;
1edf84e7 6197
6fc2811b
JR
6198 /* Make "Cancel" equivalent to C-g. */
6199 if (NILP (file))
6200 Fsignal (Qquit, Qnil);
ee78dc32 6201
dfff8a69 6202 return unbind_to (count, file);
6fc2811b 6203}
ee78dc32 6204
ee78dc32 6205
6cf29fe8
JR
6206/* Moving files to the system recycle bin.
6207 Used by `move-file-to-trash' instead of the default moving to ~/.Trash */
6208DEFUN ("system-move-file-to-trash", Fsystem_move_file_to_trash,
6209 Ssystem_move_file_to_trash, 1, 1, 0,
6210 doc: /* Move file or directory named FILENAME to the recycle bin. */)
6211 (filename)
6212 Lisp_Object filename;
6213{
6214 Lisp_Object handler;
6215 Lisp_Object encoded_file;
6216 Lisp_Object operation;
6217
6218 operation = Qdelete_file;
6219 if (!NILP (Ffile_directory_p (filename))
6220 && NILP (Ffile_symlink_p (filename)))
6221 {
e9a0aef8 6222 operation = intern ("delete-directory");
6cf29fe8
JR
6223 filename = Fdirectory_file_name (filename);
6224 }
6225 filename = Fexpand_file_name (filename, Qnil);
6226
6227 handler = Ffind_file_name_handler (filename, operation);
6228 if (!NILP (handler))
6229 return call2 (handler, operation, filename);
6230
6231 encoded_file = ENCODE_FILE (filename);
6232
6233 {
6234 const char * path;
6235 SHFILEOPSTRUCT file_op;
6236 char tmp_path[MAX_PATH + 1];
6237
6238 path = map_w32_filename (SDATA (encoded_file), NULL);
6239
6240 /* On Windows, write permission is required to delete/move files. */
6241 _chmod (path, 0666);
6242
6243 bzero (tmp_path, sizeof (tmp_path));
6244 strcpy (tmp_path, path);
6245
6246 bzero (&file_op, sizeof (file_op));
6247 file_op.hwnd = HWND_DESKTOP;
6248 file_op.wFunc = FO_DELETE;
6249 file_op.pFrom = tmp_path;
6250 file_op.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_ALLOWUNDO
6251 | FOF_NOERRORUI | FOF_NO_CONNECTED_ELEMENTS;
6252 file_op.fAnyOperationsAborted = FALSE;
6253
6254 if (SHFileOperation (&file_op) != 0)
6255 report_file_error ("Removing old name", list1 (filename));
6256 }
6257 return Qnil;
6258}
6259
6fc2811b 6260\f
6fc2811b
JR
6261/***********************************************************************
6262 w32 specialized functions
6263 ***********************************************************************/
ee78dc32 6264
74e1aeec
JR
6265DEFUN ("w32-send-sys-command", Fw32_send_sys_command,
6266 Sw32_send_sys_command, 1, 2, 0,
6267 doc: /* Send frame a Windows WM_SYSCOMMAND message of type COMMAND.
52deb19f 6268Some useful values for COMMAND are #xf030 to maximize frame (#xf020
d84b082d
JR
6269to minimize), #xf120 to restore frame to original size, and #xf100
6270to activate the menubar for keyboard access. #xf140 activates the
74e1aeec
JR
6271screen saver if defined.
6272
6273If optional parameter FRAME is not specified, use selected frame. */)
1edf84e7
GV
6274 (command, frame)
6275 Lisp_Object command, frame;
6276{
1edf84e7
GV
6277 FRAME_PTR f = check_x_frame (frame);
6278
b7826503 6279 CHECK_NUMBER (command);
1edf84e7 6280
ce6059da 6281 PostMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, XINT (command), 0);
1edf84e7
GV
6282
6283 return Qnil;
6284}
6285
55dcfc15 6286DEFUN ("w32-shell-execute", Fw32_shell_execute, Sw32_shell_execute, 2, 4, 0,
74e1aeec
JR
6287 doc: /* Get Windows to perform OPERATION on DOCUMENT.
6288This is a wrapper around the ShellExecute system function, which
6289invokes the application registered to handle OPERATION for DOCUMENT.
74e1aeec 6290
1dccd454
EZ
6291OPERATION is either nil or a string that names a supported operation.
6292What operations can be used depends on the particular DOCUMENT and its
6293handler application, but typically it is one of the following common
6294operations:
6295
6296 \"open\" - open DOCUMENT, which could be a file, a directory, or an
6297 executable program. If it is an application, that
6298 application is launched in the current buffer's default
6299 directory. Otherwise, the application associated with
6300 DOCUMENT is launched in the buffer's default directory.
6301 \"print\" - print DOCUMENT, which must be a file
6302 \"explore\" - start the Windows Explorer on DOCUMENT
6303 \"edit\" - launch an editor and open DOCUMENT for editing; which
6304 editor is launched depends on the association for the
6305 specified DOCUMENT
6306 \"find\" - initiate search starting from DOCUMENT which must specify
6307 a directory
6308 nil - invoke the default OPERATION, or \"open\" if default is
6309 not defined or unavailable
6310
6311DOCUMENT is typically the name of a document file or a URL, but can
6312also be a program executable to run, or a directory to open in the
6313Windows Explorer.
6314
02b39a28
JB
6315If DOCUMENT is a program executable, the optional third arg PARAMETERS
6316can be a string containing command line parameters that will be passed
6317to the program; otherwise, PARAMETERS should be nil or unspecified.
1dccd454 6318
02b39a28 6319Optional fourth argument SHOW-FLAG can be used to control how the
1dccd454 6320application will be displayed when it is invoked. If SHOW-FLAG is nil
02b39a28 6321or unspecified, the application is displayed normally, otherwise it is
1dccd454 6322an integer representing a ShowWindow flag:
74e1aeec
JR
6323
6324 0 - start hidden
6325 1 - start normally
6326 3 - start maximized
6327 6 - start minimized */)
55dcfc15
AI
6328 (operation, document, parameters, show_flag)
6329 Lisp_Object operation, document, parameters, show_flag;
6330{
6331 Lisp_Object current_dir;
6332
b7826503 6333 CHECK_STRING (document);
55dcfc15 6334
3bc143eb 6335 /* Encode filename, current directory and parameters. */
55dcfc15
AI
6336 current_dir = ENCODE_FILE (current_buffer->directory);
6337 document = ENCODE_FILE (document);
3bc143eb
JR
6338 if (STRINGP (parameters))
6339 parameters = ENCODE_SYSTEM (parameters);
6340
55dcfc15 6341 if ((int) ShellExecute (NULL,
6fc2811b 6342 (STRINGP (operation) ?
d5db4077
KR
6343 SDATA (operation) : NULL),
6344 SDATA (document),
55dcfc15 6345 (STRINGP (parameters) ?
d5db4077
KR
6346 SDATA (parameters) : NULL),
6347 SDATA (current_dir),
55dcfc15
AI
6348 (INTEGERP (show_flag) ?
6349 XINT (show_flag) : SW_SHOWDEFAULT))
6350 > 32)
6351 return Qt;
90d97e64 6352 error ("ShellExecute failed: %s", w32_strerror (0));
55dcfc15
AI
6353}
6354
ccc2d29c
GV
6355/* Lookup virtual keycode from string representing the name of a
6356 non-ascii keystroke into the corresponding virtual key, using
6357 lispy_function_keys. */
6358static int
6359lookup_vk_code (char *key)
6360{
6361 int i;
6362
6363 for (i = 0; i < 256; i++)
bf254037 6364 if (lispy_function_keys[i]
ccc2d29c
GV
6365 && strcmp (lispy_function_keys[i], key) == 0)
6366 return i;
6367
6368 return -1;
6369}
6370
6371/* Convert a one-element vector style key sequence to a hot key
6372 definition. */
2ba49441 6373static Lisp_Object
ccc2d29c
GV
6374w32_parse_hot_key (key)
6375 Lisp_Object key;
6376{
6377 /* Copied from Fdefine_key and store_in_keymap. */
6378 register Lisp_Object c;
6379 int vk_code;
6380 int lisp_modifiers;
6381 int w32_modifiers;
6382 struct gcpro gcpro1;
6383
b7826503 6384 CHECK_VECTOR (key);
ccc2d29c
GV
6385
6386 if (XFASTINT (Flength (key)) != 1)
6387 return Qnil;
6388
6389 GCPRO1 (key);
6390
6391 c = Faref (key, make_number (0));
6392
6393 if (CONSP (c) && lucid_event_type_list_p (c))
6394 c = Fevent_convert_list (c);
6395
6396 UNGCPRO;
6397
6398 if (! INTEGERP (c) && ! SYMBOLP (c))
6399 error ("Key definition is invalid");
6400
6401 /* Work out the base key and the modifiers. */
6402 if (SYMBOLP (c))
6403 {
6404 c = parse_modifiers (c);
2ba49441 6405 lisp_modifiers = XINT (Fcar (Fcdr (c)));
ccc2d29c
GV
6406 c = Fcar (c);
6407 if (!SYMBOLP (c))
6408 abort ();
d5db4077 6409 vk_code = lookup_vk_code (SDATA (SYMBOL_NAME (c)));
ccc2d29c
GV
6410 }
6411 else if (INTEGERP (c))
6412 {
6413 lisp_modifiers = XINT (c) & ~CHARACTERBITS;
6414 /* Many ascii characters are their own virtual key code. */
6415 vk_code = XINT (c) & CHARACTERBITS;
6416 }
6417
6418 if (vk_code < 0 || vk_code > 255)
6419 return Qnil;
6420
6421 if ((lisp_modifiers & meta_modifier) != 0
6422 && !NILP (Vw32_alt_is_meta))
6423 lisp_modifiers |= alt_modifier;
6424
71eab8d1
AI
6425 /* Supply defs missing from mingw32. */
6426#ifndef MOD_ALT
6427#define MOD_ALT 0x0001
6428#define MOD_CONTROL 0x0002
6429#define MOD_SHIFT 0x0004
6430#define MOD_WIN 0x0008
6431#endif
6432
ccc2d29c
GV
6433 /* Convert lisp modifiers to Windows hot-key form. */
6434 w32_modifiers = (lisp_modifiers & hyper_modifier) ? MOD_WIN : 0;
6435 w32_modifiers |= (lisp_modifiers & alt_modifier) ? MOD_ALT : 0;
6436 w32_modifiers |= (lisp_modifiers & ctrl_modifier) ? MOD_CONTROL : 0;
6437 w32_modifiers |= (lisp_modifiers & shift_modifier) ? MOD_SHIFT : 0;
6438
6439 return HOTKEY (vk_code, w32_modifiers);
6440}
6441
74e1aeec
JR
6442DEFUN ("w32-register-hot-key", Fw32_register_hot_key,
6443 Sw32_register_hot_key, 1, 1, 0,
6444 doc: /* Register KEY as a hot-key combination.
6445Certain key combinations like Alt-Tab are reserved for system use on
6446Windows, and therefore are normally intercepted by the system. However,
6447most of these key combinations can be received by registering them as
6448hot-keys, overriding their special meaning.
6449
6450KEY must be a one element key definition in vector form that would be
6451acceptable to `define-key' (e.g. [A-tab] for Alt-Tab). The meta
6452modifier is interpreted as Alt if `w32-alt-is-meta' is t, and hyper
6453is always interpreted as the Windows modifier keys.
6454
6455The return value is the hotkey-id if registered, otherwise nil. */)
ccc2d29c
GV
6456 (key)
6457 Lisp_Object key;
6458{
6459 key = w32_parse_hot_key (key);
6460
fb053a1f 6461 if (!NILP (key) && NILP (Fmemq (key, w32_grabbed_keys)))
ccc2d29c
GV
6462 {
6463 /* Reuse an empty slot if possible. */
6464 Lisp_Object item = Fmemq (Qnil, w32_grabbed_keys);
6465
6466 /* Safe to add new key to list, even if we have focus. */
6467 if (NILP (item))
6468 w32_grabbed_keys = Fcons (key, w32_grabbed_keys);
6469 else
f3fbd155 6470 XSETCAR (item, key);
ccc2d29c
GV
6471
6472 /* Notify input thread about new hot-key definition, so that it
6473 takes effect without needing to switch focus. */
2ba49441
JR
6474#ifdef USE_LISP_UNION_TYPE
6475 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
6476 (WPARAM) key.i, 0);
6477#else
ccc2d29c
GV
6478 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
6479 (WPARAM) key, 0);
2ba49441 6480#endif
ccc2d29c
GV
6481 }
6482
6483 return key;
6484}
6485
74e1aeec
JR
6486DEFUN ("w32-unregister-hot-key", Fw32_unregister_hot_key,
6487 Sw32_unregister_hot_key, 1, 1, 0,
52deb19f 6488 doc: /* Unregister KEY as a hot-key combination. */)
ccc2d29c
GV
6489 (key)
6490 Lisp_Object key;
6491{
6492 Lisp_Object item;
6493
6494 if (!INTEGERP (key))
6495 key = w32_parse_hot_key (key);
6496
6497 item = Fmemq (key, w32_grabbed_keys);
6498
6499 if (!NILP (item))
6500 {
6501 /* Notify input thread about hot-key definition being removed, so
6502 that it takes effect without needing focus switch. */
2ba49441
JR
6503#ifdef USE_LISP_UNION_TYPE
6504 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
6505 (WPARAM) XINT (XCAR (item)), (LPARAM) item.i))
6506#else
ccc2d29c
GV
6507 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
6508 (WPARAM) XINT (XCAR (item)), (LPARAM) item))
2ba49441 6509#endif
ccc2d29c
GV
6510 {
6511 MSG msg;
6512 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
6513 }
6514 return Qt;
6515 }
6516 return Qnil;
6517}
6518
74e1aeec
JR
6519DEFUN ("w32-registered-hot-keys", Fw32_registered_hot_keys,
6520 Sw32_registered_hot_keys, 0, 0, 0,
6521 doc: /* Return list of registered hot-key IDs. */)
ccc2d29c
GV
6522 ()
6523{
74084731 6524 return Fdelq (Qnil, Fcopy_sequence (w32_grabbed_keys));
ccc2d29c
GV
6525}
6526
74e1aeec
JR
6527DEFUN ("w32-reconstruct-hot-key", Fw32_reconstruct_hot_key,
6528 Sw32_reconstruct_hot_key, 1, 1, 0,
52deb19f
JB
6529 doc: /* Convert hot-key ID to a lisp key combination.
6530usage: (w32-reconstruct-hot-key ID) */)
ccc2d29c
GV
6531 (hotkeyid)
6532 Lisp_Object hotkeyid;
6533{
6534 int vk_code, w32_modifiers;
6535 Lisp_Object key;
6536
b7826503 6537 CHECK_NUMBER (hotkeyid);
ccc2d29c
GV
6538
6539 vk_code = HOTKEY_VK_CODE (hotkeyid);
6540 w32_modifiers = HOTKEY_MODIFIERS (hotkeyid);
6541
bf254037 6542 if (vk_code < 256 && lispy_function_keys[vk_code])
ccc2d29c
GV
6543 key = intern (lispy_function_keys[vk_code]);
6544 else
6545 key = make_number (vk_code);
6546
6547 key = Fcons (key, Qnil);
6548 if (w32_modifiers & MOD_SHIFT)
3ef68e6b 6549 key = Fcons (Qshift, key);
ccc2d29c 6550 if (w32_modifiers & MOD_CONTROL)
3ef68e6b 6551 key = Fcons (Qctrl, key);
ccc2d29c 6552 if (w32_modifiers & MOD_ALT)
3ef68e6b 6553 key = Fcons (NILP (Vw32_alt_is_meta) ? Qalt : Qmeta, key);
ccc2d29c 6554 if (w32_modifiers & MOD_WIN)
3ef68e6b 6555 key = Fcons (Qhyper, key);
ccc2d29c
GV
6556
6557 return key;
6558}
adcc3809 6559
74e1aeec
JR
6560DEFUN ("w32-toggle-lock-key", Fw32_toggle_lock_key,
6561 Sw32_toggle_lock_key, 1, 2, 0,
6562 doc: /* Toggle the state of the lock key KEY.
6563KEY can be `capslock', `kp-numlock', or `scroll'.
6564If the optional parameter NEW-STATE is a number, then the state of KEY
6565is set to off if the low bit of NEW-STATE is zero, otherwise on. */)
adcc3809
GV
6566 (key, new_state)
6567 Lisp_Object key, new_state;
6568{
6569 int vk_code;
adcc3809
GV
6570
6571 if (EQ (key, intern ("capslock")))
6572 vk_code = VK_CAPITAL;
6573 else if (EQ (key, intern ("kp-numlock")))
6574 vk_code = VK_NUMLOCK;
6575 else if (EQ (key, intern ("scroll")))
6576 vk_code = VK_SCROLL;
6577 else
6578 return Qnil;
6579
6580 if (!dwWindowsThreadId)
6581 return make_number (w32_console_toggle_lock_key (vk_code, new_state));
6582
2ba49441
JR
6583#ifdef USE_LISP_UNION_TYPE
6584 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
6585 (WPARAM) vk_code, (LPARAM) new_state.i))
6586#else
adcc3809
GV
6587 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
6588 (WPARAM) vk_code, (LPARAM) new_state))
2ba49441 6589#endif
adcc3809
GV
6590 {
6591 MSG msg;
6592 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
6593 return make_number (msg.wParam);
6594 }
6595 return Qnil;
6596}
a01763cb
EZ
6597
6598DEFUN ("w32-window-exists-p", Fw32_window_exists_p, Sw32_window_exists_p,
6599 2, 2, 0,
6600 doc: /* Return non-nil if a window exists with the specified CLASS and NAME.
6601
6602This is a direct interface to the Windows API FindWindow function. */)
6603 (class, name)
6604Lisp_Object class, name;
6605{
6606 HWND hnd;
6607
6608 if (!NILP (class))
6609 CHECK_STRING (class);
6610 if (!NILP (name))
6611 CHECK_STRING (name);
6612
6613 hnd = FindWindow (STRINGP (class) ? ((LPCTSTR) SDATA (class)) : NULL,
6614 STRINGP (name) ? ((LPCTSTR) SDATA (name)) : NULL);
6615 if (!hnd)
6616 return Qnil;
6617 return Qt;
6618}
6619
2c2279c6
JR
6620DEFUN ("w32-battery-status", Fw32_battery_status, Sw32_battery_status, 0, 0, 0,
6621 doc: /* Get power status information from Windows system.
6622
6623The following %-sequences are provided:
6624%L AC line status (verbose)
6625%B Battery status (verbose)
6626%b Battery status, empty means high, `-' means low,
6627 `!' means critical, and `+' means charging
6628%p Battery load percentage
6629%s Remaining time (to charge or discharge) in seconds
6630%m Remaining time (to charge or discharge) in minutes
6631%h Remaining time (to charge or discharge) in hours
6632%t Remaining time (to charge or discharge) in the form `h:min' */)
6633 ()
6634{
6635 Lisp_Object status = Qnil;
6636
6637 SYSTEM_POWER_STATUS system_status;
6638 if (GetSystemPowerStatus (&system_status))
6639 {
6640 Lisp_Object line_status, battery_status, battery_status_symbol;
6641 Lisp_Object load_percentage, seconds, minutes, hours, remain;
6642 Lisp_Object sequences[8];
6643
6644 long seconds_left = (long) system_status.BatteryLifeTime;
6645
6646 if (system_status.ACLineStatus == 0)
6647 line_status = build_string ("off-line");
6648 else if (system_status.ACLineStatus == 1)
6649 line_status = build_string ("on-line");
6650 else
6651 line_status = build_string ("N/A");
6652
6653 if (system_status.BatteryFlag & 128)
6654 {
6655 battery_status = build_string ("N/A");
fff4e459 6656 battery_status_symbol = empty_unibyte_string;
2c2279c6
JR
6657 }
6658 else if (system_status.BatteryFlag & 8)
6659 {
6660 battery_status = build_string ("charging");
6661 battery_status_symbol = build_string ("+");
6662 if (system_status.BatteryFullLifeTime != -1L)
6663 seconds_left = system_status.BatteryFullLifeTime - seconds_left;
6664 }
6665 else if (system_status.BatteryFlag & 4)
6666 {
6667 battery_status = build_string ("critical");
6668 battery_status_symbol = build_string ("!");
6669 }
6670 else if (system_status.BatteryFlag & 2)
6671 {
6672 battery_status = build_string ("low");
6673 battery_status_symbol = build_string ("-");
6674 }
6675 else if (system_status.BatteryFlag & 1)
6676 {
6677 battery_status = build_string ("high");
fff4e459 6678 battery_status_symbol = empty_unibyte_string;
2c2279c6
JR
6679 }
6680 else
6681 {
6682 battery_status = build_string ("medium");
fff4e459 6683 battery_status_symbol = empty_unibyte_string;
2c2279c6
JR
6684 }
6685
6686 if (system_status.BatteryLifePercent > 100)
6687 load_percentage = build_string ("N/A");
6688 else
6689 {
6690 char buffer[16];
6691 _snprintf (buffer, 16, "%d", system_status.BatteryLifePercent);
6692 load_percentage = build_string (buffer);
6693 }
6694
6695 if (seconds_left < 0)
6696 seconds = minutes = hours = remain = build_string ("N/A");
6697 else
6698 {
6699 long m;
6700 float h;
6701 char buffer[16];
6702 _snprintf (buffer, 16, "%ld", seconds_left);
6703 seconds = build_string (buffer);
6704
6705 m = seconds_left / 60;
e9184ccb 6706 _snprintf (buffer, 16, "%ld", m);
2c2279c6
JR
6707 minutes = build_string (buffer);
6708
6709 h = seconds_left / 3600.0;
6710 _snprintf (buffer, 16, "%3.1f", h);
6711 hours = build_string (buffer);
6712
e9184ccb 6713 _snprintf (buffer, 16, "%ld:%02ld", m / 60, m % 60);
2c2279c6
JR
6714 remain = build_string (buffer);
6715 }
6716 sequences[0] = Fcons (make_number ('L'), line_status);
6717 sequences[1] = Fcons (make_number ('B'), battery_status);
6718 sequences[2] = Fcons (make_number ('b'), battery_status_symbol);
6719 sequences[3] = Fcons (make_number ('p'), load_percentage);
6720 sequences[4] = Fcons (make_number ('s'), seconds);
6721 sequences[5] = Fcons (make_number ('m'), minutes);
6722 sequences[6] = Fcons (make_number ('h'), hours);
6723 sequences[7] = Fcons (make_number ('t'), remain);
6724
6725 status = Flist (8, sequences);
6726 }
6727 return status;
6728}
a01763cb 6729
ee78dc32 6730\f
2254bcde 6731DEFUN ("file-system-info", Ffile_system_info, Sfile_system_info, 1, 1, 0,
74e1aeec
JR
6732 doc: /* Return storage information about the file system FILENAME is on.
6733Value is a list of floats (TOTAL FREE AVAIL), where TOTAL is the total
6734storage of the file system, FREE is the free storage, and AVAIL is the
6735storage available to a non-superuser. All 3 numbers are in bytes.
6736If the underlying system call fails, value is nil. */)
2254bcde
AI
6737 (filename)
6738 Lisp_Object filename;
6739{
6740 Lisp_Object encoded, value;
6741
b7826503 6742 CHECK_STRING (filename);
2254bcde
AI
6743 filename = Fexpand_file_name (filename, Qnil);
6744 encoded = ENCODE_FILE (filename);
6745
6746 value = Qnil;
6747
6748 /* Determining the required information on Windows turns out, sadly,
6749 to be more involved than one would hope. The original Win32 api
6750 call for this will return bogus information on some systems, but we
6751 must dynamically probe for the replacement api, since that was
6752 added rather late on. */
6753 {
6754 HMODULE hKernel = GetModuleHandle ("kernel32");
6755 BOOL (*pfn_GetDiskFreeSpaceEx)
6756 (char *, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER)
6757 = (void *) GetProcAddress (hKernel, "GetDiskFreeSpaceEx");
6758
6759 /* On Windows, we may need to specify the root directory of the
6760 volume holding FILENAME. */
6761 char rootname[MAX_PATH];
d5db4077 6762 char *name = SDATA (encoded);
2254bcde
AI
6763
6764 /* find the root name of the volume if given */
6765 if (isalpha (name[0]) && name[1] == ':')
6766 {
6767 rootname[0] = name[0];
6768 rootname[1] = name[1];
6769 rootname[2] = '\\';
6770 rootname[3] = 0;
6771 }
6772 else if (IS_DIRECTORY_SEP (name[0]) && IS_DIRECTORY_SEP (name[1]))
6773 {
6774 char *str = rootname;
6775 int slashes = 4;
6776 do
6777 {
6778 if (IS_DIRECTORY_SEP (*name) && --slashes == 0)
6779 break;
6780 *str++ = *name++;
6781 }
6782 while ( *name );
6783
6784 *str++ = '\\';
6785 *str = 0;
6786 }
6787
6788 if (pfn_GetDiskFreeSpaceEx)
6789 {
ac849ba4
JR
6790 /* Unsigned large integers cannot be cast to double, so
6791 use signed ones instead. */
2254bcde
AI
6792 LARGE_INTEGER availbytes;
6793 LARGE_INTEGER freebytes;
6794 LARGE_INTEGER totalbytes;
6795
74084731
JB
6796 if (pfn_GetDiskFreeSpaceEx (rootname,
6797 (ULARGE_INTEGER *)&availbytes,
6798 (ULARGE_INTEGER *)&totalbytes,
6799 (ULARGE_INTEGER *)&freebytes))
2254bcde
AI
6800 value = list3 (make_float ((double) totalbytes.QuadPart),
6801 make_float ((double) freebytes.QuadPart),
6802 make_float ((double) availbytes.QuadPart));
6803 }
6804 else
6805 {
6806 DWORD sectors_per_cluster;
6807 DWORD bytes_per_sector;
6808 DWORD free_clusters;
6809 DWORD total_clusters;
6810
74084731
JB
6811 if (GetDiskFreeSpace (rootname,
6812 &sectors_per_cluster,
6813 &bytes_per_sector,
6814 &free_clusters,
6815 &total_clusters))
2254bcde
AI
6816 value = list3 (make_float ((double) total_clusters
6817 * sectors_per_cluster * bytes_per_sector),
6818 make_float ((double) free_clusters
6819 * sectors_per_cluster * bytes_per_sector),
6820 make_float ((double) free_clusters
6821 * sectors_per_cluster * bytes_per_sector));
6822 }
6823 }
6824
6825 return value;
6826}
6827\f
6b61353c
KH
6828DEFUN ("default-printer-name", Fdefault_printer_name, Sdefault_printer_name,
6829 0, 0, 0, doc: /* Return the name of Windows default printer device. */)
6830 ()
6831{
6832 static char pname_buf[256];
6833 int err;
6834 HANDLE hPrn;
6835 PRINTER_INFO_2 *ppi2 = NULL;
6836 DWORD dwNeeded = 0, dwReturned = 0;
6837
6838 /* Retrieve the default string from Win.ini (the registry).
6839 * String will be in form "printername,drivername,portname".
6840 * This is the most portable way to get the default printer. */
6841 if (GetProfileString ("windows", "device", ",,", pname_buf, sizeof (pname_buf)) <= 0)
6842 return Qnil;
6843 /* printername precedes first "," character */
6844 strtok (pname_buf, ",");
6845 /* We want to know more than the printer name */
6846 if (!OpenPrinter (pname_buf, &hPrn, NULL))
6847 return Qnil;
6848 GetPrinter (hPrn, 2, NULL, 0, &dwNeeded);
6849 if (dwNeeded == 0)
6850 {
6851 ClosePrinter (hPrn);
6852 return Qnil;
6853 }
6854 /* Allocate memory for the PRINTER_INFO_2 struct */
6855 ppi2 = (PRINTER_INFO_2 *) xmalloc (dwNeeded);
6856 if (!ppi2)
6857 {
6858 ClosePrinter (hPrn);
6859 return Qnil;
6860 }
74084731 6861 /* Call GetPrinter again with big enouth memory block */
6b61353c
KH
6862 err = GetPrinter (hPrn, 2, (LPBYTE)ppi2, dwNeeded, &dwReturned);
6863 ClosePrinter (hPrn);
6864 if (!err)
6865 {
74084731 6866 xfree (ppi2);
6b61353c
KH
6867 return Qnil;
6868 }
6869
6870 if (ppi2)
6871 {
6872 if (ppi2->Attributes & PRINTER_ATTRIBUTE_SHARED && ppi2->pServerName)
6873 {
6874 /* a remote printer */
6875 if (*ppi2->pServerName == '\\')
74084731 6876 _snprintf (pname_buf, sizeof (pname_buf), "%s\\%s", ppi2->pServerName,
02b39a28 6877 ppi2->pShareName);
6b61353c 6878 else
74084731 6879 _snprintf (pname_buf, sizeof (pname_buf), "\\\\%s\\%s", ppi2->pServerName,
02b39a28 6880 ppi2->pShareName);
6b61353c
KH
6881 pname_buf[sizeof (pname_buf) - 1] = '\0';
6882 }
6883 else
6884 {
6885 /* a local printer */
74084731 6886 strncpy (pname_buf, ppi2->pPortName, sizeof (pname_buf));
6b61353c
KH
6887 pname_buf[sizeof (pname_buf) - 1] = '\0';
6888 /* `pPortName' can include several ports, delimited by ','.
6889 * we only use the first one. */
74084731 6890 strtok (pname_buf, ",");
6b61353c 6891 }
74084731 6892 xfree (ppi2);
6b61353c
KH
6893 }
6894
6895 return build_string (pname_buf);
6896}
6897\f
0e3fcdef
JR
6898/***********************************************************************
6899 Initialization
6900 ***********************************************************************/
6901
52deb19f 6902/* Keep this list in the same order as frame_parms in frame.c.
6d906347
KS
6903 Use 0 for unsupported frame parameters. */
6904
6905frame_parm_handler w32_frame_parm_handlers[] =
6906{
6907 x_set_autoraise,
6908 x_set_autolower,
6909 x_set_background_color,
6910 x_set_border_color,
6911 x_set_border_width,
6912 x_set_cursor_color,
6913 x_set_cursor_type,
6914 x_set_font,
6915 x_set_foreground_color,
6916 x_set_icon_name,
6917 x_set_icon_type,
6918 x_set_internal_border_width,
6919 x_set_menu_bar_lines,
6920 x_set_mouse_color,
6921 x_explicitly_set_name,
6922 x_set_scroll_bar_width,
6923 x_set_title,
6924 x_set_unsplittable,
6925 x_set_vertical_scroll_bars,
6926 x_set_visibility,
6927 x_set_tool_bar_lines,
6928 0, /* x_set_scroll_bar_foreground, */
6929 0, /* x_set_scroll_bar_background, */
6930 x_set_screen_gamma,
6931 x_set_line_spacing,
6932 x_set_fringe_width,
6933 x_set_fringe_width,
6934 0, /* x_set_wait_for_wm, */
6935 x_set_fullscreen,
a2979e8e 6936 x_set_font_backend,
cad9ef74
JD
6937 x_set_alpha,
6938 0, /* x_set_sticky */
6d906347
KS
6939};
6940
0e3fcdef 6941void
fbd6baed 6942syms_of_w32fns ()
ee78dc32 6943{
afc390dc
JR
6944 globals_of_w32fns ();
6945 /* This is zero if not using MS-Windows. */
1edf84e7 6946 w32_in_use = 0;
9eb16b62
JR
6947 track_mouse_window = NULL;
6948
d285988b
JR
6949 w32_visible_system_caret_hwnd = NULL;
6950
4f5b288c
JR
6951 DEFSYM (Qnone, "none");
6952 DEFSYM (Qsuppress_icon, "suppress-icon");
6953 DEFSYM (Qundefined_color, "undefined-color");
6954 DEFSYM (Qcancel_timer, "cancel-timer");
6955 DEFSYM (Qhyper, "hyper");
6956 DEFSYM (Qsuper, "super");
6957 DEFSYM (Qmeta, "meta");
6958 DEFSYM (Qalt, "alt");
6959 DEFSYM (Qctrl, "ctrl");
6960 DEFSYM (Qcontrol, "control");
6961 DEFSYM (Qshift, "shift");
27129af9 6962 DEFSYM (Qfont_param, "font-parameter");
f7b9d4d1 6963 /* This is the end of symbol initialization. */
adcc3809 6964
6fc2811b
JR
6965 /* Text property `display' should be nonsticky by default. */
6966 Vtext_property_default_nonsticky
6967 = Fcons (Fcons (Qdisplay, Qt), Vtext_property_default_nonsticky);
6968
6969
ee78dc32
GV
6970 Fput (Qundefined_color, Qerror_conditions,
6971 Fcons (Qundefined_color, Fcons (Qerror, Qnil)));
6972 Fput (Qundefined_color, Qerror_message,
6973 build_string ("Undefined color"));
6974
ccc2d29c
GV
6975 staticpro (&w32_grabbed_keys);
6976 w32_grabbed_keys = Qnil;
6977
fbd6baed 6978 DEFVAR_LISP ("w32-color-map", &Vw32_color_map,
52deb19f 6979 doc: /* An array of color name mappings for Windows. */);
fbd6baed 6980 Vw32_color_map = Qnil;
ee78dc32 6981
fbd6baed 6982 DEFVAR_LISP ("w32-pass-alt-to-system", &Vw32_pass_alt_to_system,
1133f8e7
EZ
6983 doc: /* Non-nil if Alt key presses are passed on to Windows.
6984When non-nil, for example, Alt pressed and released and then space will
6985open the System menu. When nil, Emacs processes the Alt key events, and
6986then silently swallows them. */);
fbd6baed 6987 Vw32_pass_alt_to_system = Qnil;
da36a4d6 6988
fbd6baed 6989 DEFVAR_LISP ("w32-alt-is-meta", &Vw32_alt_is_meta,
1133f8e7
EZ
6990 doc: /* Non-nil if the Alt key is to be considered the same as the META key.
6991When nil, Emacs will translate the Alt key to the ALT modifier, not to META. */);
fbd6baed 6992 Vw32_alt_is_meta = Qt;
8c205c63 6993
2ba49441
JR
6994 DEFVAR_INT ("w32-quit-key", &w32_quit_key,
6995 doc: /* If non-zero, the virtual key code for an alternative quit key. */);
6996 w32_quit_key = 0;
7d081355 6997
7d0393cf 6998 DEFVAR_LISP ("w32-pass-lwindow-to-system",
ccc2d29c 6999 &Vw32_pass_lwindow_to_system,
1133f8e7
EZ
7000 doc: /* If non-nil, the left \"Windows\" key is passed on to Windows.
7001
7002When non-nil, the Start menu is opened by tapping the key.
7003If you set this to nil, the left \"Windows\" key is processed by Emacs
7004according to the value of `w32-lwindow-modifier', which see.
7005
7006Note that some combinations of the left \"Windows\" key with other keys are
7007caught by Windows at low level, and so binding them in Emacs will have no
7008effect. For example, <lwindow>-r always pops up the Windows Run dialog,
7009<lwindow>-<Pause> pops up the "System Properties" dialog, etc. However, see
7010the doc string of `w32-phantom-key-code'. */);
ccc2d29c
GV
7011 Vw32_pass_lwindow_to_system = Qt;
7012
7d0393cf 7013 DEFVAR_LISP ("w32-pass-rwindow-to-system",
ccc2d29c 7014 &Vw32_pass_rwindow_to_system,
1133f8e7
EZ
7015 doc: /* If non-nil, the right \"Windows\" key is passed on to Windows.
7016
7017When non-nil, the Start menu is opened by tapping the key.
7018If you set this to nil, the right \"Windows\" key is processed by Emacs
7019according to the value of `w32-rwindow-modifier', which see.
7020
7021Note that some combinations of the right \"Windows\" key with other keys are
7022caught by Windows at low level, and so binding them in Emacs will have no
7023effect. For example, <rwindow>-r always pops up the Windows Run dialog,
7024<rwindow>-<Pause> pops up the "System Properties" dialog, etc. However, see
7025the doc string of `w32-phantom-key-code'. */);
ccc2d29c
GV
7026 Vw32_pass_rwindow_to_system = Qt;
7027
2ba49441 7028 DEFVAR_LISP ("w32-phantom-key-code",
adcc3809 7029 &Vw32_phantom_key_code,
2ba49441 7030 doc: /* Virtual key code used to generate \"phantom\" key presses.
74e1aeec
JR
7031Value is a number between 0 and 255.
7032
7033Phantom key presses are generated in order to stop the system from
7034acting on \"Windows\" key events when `w32-pass-lwindow-to-system' or
7035`w32-pass-rwindow-to-system' is nil. */);
ce6059da
AI
7036 /* Although 255 is technically not a valid key code, it works and
7037 means that this hack won't interfere with any real key code. */
2ba49441 7038 XSETINT (Vw32_phantom_key_code, 255);
adcc3809 7039
7d0393cf 7040 DEFVAR_LISP ("w32-enable-num-lock",
ccc2d29c 7041 &Vw32_enable_num_lock,
1133f8e7
EZ
7042 doc: /* If non-nil, the Num Lock key acts normally.
7043Set to nil to handle Num Lock as the `kp-numlock' key. */);
ccc2d29c
GV
7044 Vw32_enable_num_lock = Qt;
7045
7d0393cf 7046 DEFVAR_LISP ("w32-enable-caps-lock",
ccc2d29c 7047 &Vw32_enable_caps_lock,
1133f8e7
EZ
7048 doc: /* If non-nil, the Caps Lock key acts normally.
7049Set to nil to handle Caps Lock as the `capslock' key. */);
ccc2d29c
GV
7050 Vw32_enable_caps_lock = Qt;
7051
7052 DEFVAR_LISP ("w32-scroll-lock-modifier",
7053 &Vw32_scroll_lock_modifier,
1133f8e7 7054 doc: /* Modifier to use for the Scroll Lock ON state.
74e1aeec 7055The value can be hyper, super, meta, alt, control or shift for the
1133f8e7
EZ
7056respective modifier, or nil to handle Scroll Lock as the `scroll' key.
7057Any other value will cause the Scroll Lock key to be ignored. */);
ccc2d29c
GV
7058 Vw32_scroll_lock_modifier = Qt;
7059
7060 DEFVAR_LISP ("w32-lwindow-modifier",
7061 &Vw32_lwindow_modifier,
74e1aeec
JR
7062 doc: /* Modifier to use for the left \"Windows\" key.
7063The value can be hyper, super, meta, alt, control or shift for the
1133f8e7 7064respective modifier, or nil to appear as the `lwindow' key.
74e1aeec 7065Any other value will cause the key to be ignored. */);
ccc2d29c
GV
7066 Vw32_lwindow_modifier = Qnil;
7067
7068 DEFVAR_LISP ("w32-rwindow-modifier",
7069 &Vw32_rwindow_modifier,
74e1aeec
JR
7070 doc: /* Modifier to use for the right \"Windows\" key.
7071The value can be hyper, super, meta, alt, control or shift for the
1133f8e7 7072respective modifier, or nil to appear as the `rwindow' key.
74e1aeec 7073Any other value will cause the key to be ignored. */);
ccc2d29c
GV
7074 Vw32_rwindow_modifier = Qnil;
7075
7076 DEFVAR_LISP ("w32-apps-modifier",
7077 &Vw32_apps_modifier,
74e1aeec
JR
7078 doc: /* Modifier to use for the \"Apps\" key.
7079The value can be hyper, super, meta, alt, control or shift for the
1133f8e7 7080respective modifier, or nil to appear as the `apps' key.
74e1aeec 7081Any other value will cause the key to be ignored. */);
ccc2d29c 7082 Vw32_apps_modifier = Qnil;
da36a4d6 7083
d84b082d 7084 DEFVAR_BOOL ("w32-enable-synthesized-fonts", &w32_enable_synthesized_fonts,
74e1aeec 7085 doc: /* Non-nil enables selection of artificially italicized and bold fonts. */);
d84b082d 7086 w32_enable_synthesized_fonts = 0;
5ac45f98 7087
fbd6baed 7088 DEFVAR_LISP ("w32-enable-palette", &Vw32_enable_palette,
74e1aeec 7089 doc: /* Non-nil enables Windows palette management to map colors exactly. */);
fbd6baed 7090 Vw32_enable_palette = Qt;
5ac45f98 7091
fbd6baed 7092 DEFVAR_INT ("w32-mouse-button-tolerance",
2ba49441 7093 &w32_mouse_button_tolerance,
74e1aeec
JR
7094 doc: /* Analogue of double click interval for faking middle mouse events.
7095The value is the minimum time in milliseconds that must elapse between
1133f8e7 7096left and right button down events before they are considered distinct events.
74e1aeec
JR
7097If both mouse buttons are depressed within this interval, a middle mouse
7098button down event is generated instead. */);
2ba49441 7099 w32_mouse_button_tolerance = GetDoubleClickTime () / 2;
5ac45f98 7100
fbd6baed 7101 DEFVAR_INT ("w32-mouse-move-interval",
2ba49441 7102 &w32_mouse_move_interval,
74e1aeec
JR
7103 doc: /* Minimum interval between mouse move events.
7104The value is the minimum time in milliseconds that must elapse between
7105successive mouse move (or scroll bar drag) events before they are
7106reported as lisp events. */);
2ba49441 7107 w32_mouse_move_interval = 0;
84fb1139 7108
74214547
JR
7109 DEFVAR_BOOL ("w32-pass-extra-mouse-buttons-to-system",
7110 &w32_pass_extra_mouse_buttons_to_system,
1133f8e7 7111 doc: /* If non-nil, the fourth and fifth mouse buttons are passed to Windows.
74214547
JR
7112Recent versions of Windows support mice with up to five buttons.
7113Since most applications don't support these extra buttons, most mouse
7114drivers will allow you to map them to functions at the system level.
7115If this variable is non-nil, Emacs will pass them on, allowing the
7116system to handle them. */);
7117 w32_pass_extra_mouse_buttons_to_system = 0;
7118
0b151762
JR
7119 DEFVAR_BOOL ("w32-pass-multimedia-buttons-to-system",
7120 &w32_pass_multimedia_buttons_to_system,
7121 doc: /* If non-nil, media buttons are passed to Windows.
7122Some modern keyboards contain buttons for controlling media players, web
74084731 7123browsers and other applications. Generally these buttons are handled on a
0b151762
JR
7124system wide basis, but by setting this to nil they are made available
7125to Emacs for binding. Depending on your keyboard, additional keys that
7126may be available are:
7127
7128browser-back, browser-forward, browser-refresh, browser-stop,
7129browser-search, browser-favorites, browser-home,
7130mail, mail-reply, mail-forward, mail-send,
7131app-1, app-2,
7132help, find, new, open, close, save, print, undo, redo, copy, cut, paste,
7133spell-check, correction-list, toggle-dictate-command,
7134media-next, media-previous, media-stop, media-play-pause, media-select,
7135media-play, media-pause, media-record, media-fast-forward, media-rewind,
7136media-channel-up, media-channel-down,
7137volume-mute, volume-up, volume-down,
7138mic-volume-mute, mic-volume-down, mic-volume-up, mic-toggle,
74084731 7139bass-down, bass-boost, bass-up, treble-down, treble-up */);
0b151762
JR
7140 w32_pass_multimedia_buttons_to_system = 1;
7141
5f004711 7142#if 0 /* TODO: Mouse cursor customization. */
ee78dc32 7143 DEFVAR_LISP ("x-pointer-shape", &Vx_pointer_shape,
74e1aeec
JR
7144 doc: /* The shape of the pointer when over text.
7145Changing the value does not affect existing frames
7146unless you set the mouse color. */);
ee78dc32
GV
7147 Vx_pointer_shape = Qnil;
7148
ee78dc32
GV
7149 Vx_nontext_pointer_shape = Qnil;
7150
7151 Vx_mode_pointer_shape = Qnil;
7152
0af913d7 7153 DEFVAR_LISP ("x-hourglass-pointer-shape", &Vx_hourglass_pointer_shape,
74e1aeec
JR
7154 doc: /* The shape of the pointer when Emacs is busy.
7155This variable takes effect when you create a new frame
7156or when you set the mouse color. */);
0af913d7 7157 Vx_hourglass_pointer_shape = Qnil;
6fc2811b 7158
6fc2811b 7159 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
74084731 7160 &Vx_sensitive_text_pointer_shape,
74e1aeec
JR
7161 doc: /* The shape of the pointer when over mouse-sensitive text.
7162This variable takes effect when you create a new frame
7163or when you set the mouse color. */);
ee78dc32
GV
7164 Vx_sensitive_text_pointer_shape = Qnil;
7165
4694d762 7166 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
74084731 7167 &Vx_window_horizontal_drag_shape,
74e1aeec
JR
7168 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
7169This variable takes effect when you create a new frame
7170or when you set the mouse color. */);
4694d762 7171 Vx_window_horizontal_drag_shape = Qnil;
5f004711 7172#endif
4694d762 7173
ee78dc32 7174 DEFVAR_LISP ("x-cursor-fore-pixel", &Vx_cursor_fore_pixel,
74e1aeec 7175 doc: /* A string indicating the foreground color of the cursor box. */);
ee78dc32
GV
7176 Vx_cursor_fore_pixel = Qnil;
7177
3cf3436e 7178 DEFVAR_LISP ("x-max-tooltip-size", &Vx_max_tooltip_size,
b3700ae7
JR
7179 doc: /* Maximum size for tooltips.
7180Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
3cf3436e 7181 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
7d0393cf 7182
ee78dc32 7183 DEFVAR_LISP ("x-no-window-manager", &Vx_no_window_manager,
74e1aeec
JR
7184 doc: /* Non-nil if no window manager is in use.
7185Emacs doesn't try to figure this out; this is always nil
7186unless you set it to something else. */);
ee78dc32
GV
7187 /* We don't have any way to find this out, so set it to nil
7188 and maybe the user would like to set it to t. */
7189 Vx_no_window_manager = Qnil;
7190
4587b026
GV
7191 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
7192 &Vx_pixel_size_width_font_regexp,
74e1aeec
JR
7193 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
7194
7195Since Emacs gets width of a font matching with this regexp from
7196PIXEL_SIZE field of the name, font finding mechanism gets faster for
7197such a font. This is especially effective for such large fonts as
7198Chinese, Japanese, and Korean. */);
4587b026
GV
7199 Vx_pixel_size_width_font_regexp = Qnil;
7200
33d52f9c
GV
7201 DEFVAR_LISP ("w32-bdf-filename-alist",
7202 &Vw32_bdf_filename_alist,
74e1aeec 7203 doc: /* List of bdf fonts and their corresponding filenames. */);
33d52f9c
GV
7204 Vw32_bdf_filename_alist = Qnil;
7205
1075afa9
GV
7206 DEFVAR_BOOL ("w32-strict-fontnames",
7207 &w32_strict_fontnames,
74e1aeec
JR
7208 doc: /* Non-nil means only use fonts that are exact matches for those requested.
7209Default is nil, which allows old fontnames that are not XLFD compliant,
7210and allows third-party CJK display to work by specifying false charset
7211fields to trick Emacs into translating to Big5, SJIS etc.
7212Setting this to t will prevent wrong fonts being selected when
7213fontsets are automatically created. */);
1075afa9
GV
7214 w32_strict_fontnames = 0;
7215
c0611964
AI
7216 DEFVAR_BOOL ("w32-strict-painting",
7217 &w32_strict_painting,
74e1aeec 7218 doc: /* Non-nil means use strict rules for repainting frames.
a8ab3e96 7219Set this to nil to get the old behavior for repainting; this should
74e1aeec 7220only be necessary if the default setting causes problems. */);
c0611964
AI
7221 w32_strict_painting = 1;
7222
767b1ff0 7223#if 0 /* TODO: Port to W32 */
6fc2811b
JR
7224 defsubr (&Sx_change_window_property);
7225 defsubr (&Sx_delete_window_property);
7226 defsubr (&Sx_window_property);
7227#endif
2d764c78 7228 defsubr (&Sxw_display_color_p);
ee78dc32 7229 defsubr (&Sx_display_grayscale_p);
2d764c78
EZ
7230 defsubr (&Sxw_color_defined_p);
7231 defsubr (&Sxw_color_values);
ee78dc32
GV
7232 defsubr (&Sx_server_max_request_size);
7233 defsubr (&Sx_server_vendor);
7234 defsubr (&Sx_server_version);
7235 defsubr (&Sx_display_pixel_width);
7236 defsubr (&Sx_display_pixel_height);
7237 defsubr (&Sx_display_mm_width);
7238 defsubr (&Sx_display_mm_height);
7239 defsubr (&Sx_display_screens);
7240 defsubr (&Sx_display_planes);
7241 defsubr (&Sx_display_color_cells);
7242 defsubr (&Sx_display_visual_class);
7243 defsubr (&Sx_display_backing_store);
7244 defsubr (&Sx_display_save_under);
ee78dc32 7245 defsubr (&Sx_create_frame);
ee78dc32
GV
7246 defsubr (&Sx_open_connection);
7247 defsubr (&Sx_close_connection);
7248 defsubr (&Sx_display_list);
7249 defsubr (&Sx_synchronize);
334a1195 7250 defsubr (&Sx_focus_frame);
ee78dc32 7251
fbd6baed 7252 /* W32 specific functions */
ee78dc32 7253
fbd6baed
GV
7254 defsubr (&Sw32_define_rgb_color);
7255 defsubr (&Sw32_default_color_map);
1edf84e7 7256 defsubr (&Sw32_send_sys_command);
55dcfc15 7257 defsubr (&Sw32_shell_execute);
ccc2d29c
GV
7258 defsubr (&Sw32_register_hot_key);
7259 defsubr (&Sw32_unregister_hot_key);
7260 defsubr (&Sw32_registered_hot_keys);
7261 defsubr (&Sw32_reconstruct_hot_key);
adcc3809 7262 defsubr (&Sw32_toggle_lock_key);
a01763cb 7263 defsubr (&Sw32_window_exists_p);
2c2279c6 7264 defsubr (&Sw32_battery_status);
4587b026 7265
2254bcde 7266 defsubr (&Sfile_system_info);
6b61353c 7267 defsubr (&Sdefault_printer_name);
2254bcde 7268
4587b026 7269 check_window_system_func = check_w32;
6fc2811b 7270
463f5630 7271
d148e14d
JR
7272 hourglass_timer = 0;
7273 hourglass_hwnd = NULL;
1885ab29 7274
6fc2811b
JR
7275 defsubr (&Sx_show_tip);
7276 defsubr (&Sx_hide_tip);
6fc2811b 7277 tip_timer = Qnil;
57fa2774
JR
7278 staticpro (&tip_timer);
7279 tip_frame = Qnil;
7280 staticpro (&tip_frame);
6fc2811b 7281
ca56d953
JR
7282 last_show_tip_args = Qnil;
7283 staticpro (&last_show_tip_args);
7284
6fc2811b 7285 defsubr (&Sx_file_dialog);
6cf29fe8 7286 defsubr (&Ssystem_move_file_to_trash);
6fc2811b
JR
7287}
7288
c922a224 7289
9785d95b
BK
7290/*
7291 globals_of_w32fns is used to initialize those global variables that
7292 must always be initialized on startup even when the global variable
7293 initialized is non zero (see the function main in emacs.c).
7294 globals_of_w32fns is called from syms_of_w32fns when the global
7295 variable initialized is 0 and directly from main when initialized
7296 is non zero.
7297 */
02b39a28
JB
7298void
7299globals_of_w32fns ()
9785d95b
BK
7300{
7301 HMODULE user32_lib = GetModuleHandle ("user32.dll");
ccc0fdaa
JR
7302 /*
7303 TrackMouseEvent not available in all versions of Windows, so must load
7304 it dynamically. Do it once, here, instead of every time it is used.
9785d95b 7305 */
ccc0fdaa
JR
7306 track_mouse_event_fn = (TrackMouseEvent_Proc)
7307 GetProcAddress (user32_lib, "TrackMouseEvent");
7308 /* ditto for GetClipboardSequenceNumber. */
7309 clipboard_sequence_fn = (ClipboardSequence_Proc)
7310 GetProcAddress (user32_lib, "GetClipboardSequenceNumber");
64f0809d
JR
7311
7312 monitor_from_point_fn = (MonitorFromPoint_Proc)
7313 GetProcAddress (user32_lib, "MonitorFromPoint");
7314 get_monitor_info_fn = (GetMonitorInfo_Proc)
7315 GetProcAddress (user32_lib, "GetMonitorInfoA");
7316
820eff5a
JR
7317 {
7318 HMODULE imm32_lib = GetModuleHandle ("imm32.dll");
7319 get_composition_string_fn = (ImmGetCompositionString_Proc)
7320 GetProcAddress (imm32_lib, "ImmGetCompositionStringW");
7321 get_ime_context_fn = (ImmGetContext_Proc)
7322 GetProcAddress (imm32_lib, "ImmGetContext");
c902b920
JR
7323 release_ime_context_fn = (ImmReleaseContext_Proc)
7324 GetProcAddress (imm32_lib, "ImmReleaseContext");
7325 set_ime_composition_window_fn = (ImmSetCompositionWindow_Proc)
7326 GetProcAddress (imm32_lib, "ImmSetCompositionWindow");
820eff5a 7327 }
4bf91535 7328 DEFVAR_INT ("w32-ansi-code-page",
2ba49441 7329 &w32_ansi_code_page,
4bf91535 7330 doc: /* The ANSI code page used by the system. */);
2ba49441 7331 w32_ansi_code_page = GetACP ();
60860eb3
JR
7332
7333 /* MessageBox does not work without this when linked to comctl32.dll 6.0. */
7334 InitCommonControls ();
cbfedb1c 7335
cbfedb1c 7336 syms_of_w32uniscribe ();
9785d95b 7337}
6fc2811b 7338
ee78dc32
GV
7339#undef abort
7340
7d0393cf 7341void
74084731 7342w32_abort ()
ee78dc32 7343{
5ac45f98
GV
7344 int button;
7345 button = MessageBox (NULL,
7346 "A fatal error has occurred!\n\n"
c52e1638 7347 "Would you like to attach a debugger?\n\n"
cb91d111
EZ
7348 "Select YES to debug, NO to abort Emacs"
7349#if __GNUC__
7350 "\n\n(type \"gdb -p <emacs-PID>\" and\n"
7351 "\"continue\" inside GDB before clicking YES.)"
7352#endif
7353 , "Emacs Abort Dialog",
5ac45f98 7354 MB_ICONEXCLAMATION | MB_TASKMODAL
c52e1638 7355 | MB_SETFOREGROUND | MB_YESNO);
5ac45f98
GV
7356 switch (button)
7357 {
c52e1638 7358 case IDYES:
5ac45f98 7359 DebugBreak ();
c52e1638
EZ
7360 exit (2); /* tell the compiler we will never return */
7361 case IDNO:
5ac45f98
GV
7362 default:
7363 abort ();
7364 break;
7365 }
ee78dc32 7366}
d573caac 7367
83c75055
GV
7368/* For convenience when debugging. */
7369int
74084731 7370w32_last_error ()
83c75055
GV
7371{
7372 return GetLastError ();
7373}
6b61353c
KH
7374
7375/* arch-tag: 707589ab-b9be-4638-8cdd-74629cc9b446
7376 (do not change this comment) */