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