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