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