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