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