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