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