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