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