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