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