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