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