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