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