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