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