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