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