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