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