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