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