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