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