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