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