Fix typo in last checkin
[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;
63e50ea6 4645 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
33d52f9c 4646
8edb0a6f 4647 bdf_fonts = w32_list_bdf_fonts (build_string (fontname), 1);
33d52f9c
GV
4648
4649 while (!retval && CONSP (bdf_fonts))
4650 {
4651 char *bdf_name, *bdf_file;
4652 Lisp_Object bdf_pair;
63e50ea6 4653 int i;
33d52f9c 4654
d5db4077 4655 bdf_name = SDATA (XCAR (bdf_fonts));
8e713be6 4656 bdf_pair = Fassoc (XCAR (bdf_fonts), Vw32_bdf_filename_alist);
d5db4077 4657 bdf_file = SDATA (XCDR (bdf_pair));
33d52f9c 4658
63e50ea6
JR
4659 // If the font is already loaded, do not load it again.
4660 for (i = 0; i < dpyinfo->n_fonts; i++)
4661 {
4662 if ((dpyinfo->font_table[i].name
4663 && !strcmp (dpyinfo->font_table[i].name, bdf_name))
4664 || (dpyinfo->font_table[i].full_name
4665 && !strcmp (dpyinfo->font_table[i].full_name, bdf_name)))
745e0c15 4666 return dpyinfo->font_table + i;
63e50ea6
JR
4667 }
4668
33d52f9c
GV
4669 retval = w32_load_bdf_font (f, bdf_name, size, bdf_file);
4670
8e713be6 4671 bdf_fonts = XCDR (bdf_fonts);
33d52f9c
GV
4672 }
4673
4674 if (retval)
4675 return retval;
4676
4677 return w32_load_system_font(f, fontname, size);
4678}
4679
4680
7d0393cf 4681void
fbd6baed
GV
4682w32_unload_font (dpyinfo, font)
4683 struct w32_display_info *dpyinfo;
ee78dc32
GV
4684 XFontStruct * font;
4685{
7d0393cf 4686 if (font)
ee78dc32 4687 {
c6be3860 4688 if (font->per_char) xfree (font->per_char);
33d52f9c
GV
4689 if (font->bdf) w32_free_bdf_font (font->bdf);
4690
3c190163 4691 if (font->hfont) DeleteObject(font->hfont);
ee78dc32
GV
4692 xfree (font);
4693 }
4694}
4695
fbd6baed 4696/* The font conversion stuff between x and w32 */
ee78dc32
GV
4697
4698/* X font string is as follows (from faces.el)
4699 * (let ((- "[-?]")
4700 * (foundry "[^-]+")
4701 * (family "[^-]+")
4702 * (weight "\\(bold\\|demibold\\|medium\\)") ; 1
4703 * (weight\? "\\([^-]*\\)") ; 1
4704 * (slant "\\([ior]\\)") ; 2
4705 * (slant\? "\\([^-]?\\)") ; 2
4706 * (swidth "\\([^-]*\\)") ; 3
4707 * (adstyle "[^-]*") ; 4
4708 * (pixelsize "[0-9]+")
4709 * (pointsize "[0-9][0-9]+")
4710 * (resx "[0-9][0-9]+")
4711 * (resy "[0-9][0-9]+")
4712 * (spacing "[cmp?*]")
4713 * (avgwidth "[0-9]+")
4714 * (registry "[^-]+")
4715 * (encoding "[^-]+")
4716 * )
ee78dc32 4717 */
ee78dc32 4718
7d0393cf 4719static LONG
fbd6baed 4720x_to_w32_weight (lpw)
ee78dc32
GV
4721 char * lpw;
4722{
4723 if (!lpw) return (FW_DONTCARE);
5ac45f98
GV
4724
4725 if (stricmp (lpw,"heavy") == 0) return FW_HEAVY;
4726 else if (stricmp (lpw,"extrabold") == 0) return FW_EXTRABOLD;
4727 else if (stricmp (lpw,"bold") == 0) return FW_BOLD;
4728 else if (stricmp (lpw,"demibold") == 0) return FW_SEMIBOLD;
1edf84e7 4729 else if (stricmp (lpw,"semibold") == 0) return FW_SEMIBOLD;
5ac45f98
GV
4730 else if (stricmp (lpw,"medium") == 0) return FW_MEDIUM;
4731 else if (stricmp (lpw,"normal") == 0) return FW_NORMAL;
4732 else if (stricmp (lpw,"light") == 0) return FW_LIGHT;
4733 else if (stricmp (lpw,"extralight") == 0) return FW_EXTRALIGHT;
4734 else if (stricmp (lpw,"thin") == 0) return FW_THIN;
ee78dc32 4735 else
5ac45f98 4736 return FW_DONTCARE;
ee78dc32
GV
4737}
4738
5ac45f98 4739
7d0393cf 4740static char *
fbd6baed 4741w32_to_x_weight (fnweight)
ee78dc32
GV
4742 int fnweight;
4743{
5ac45f98
GV
4744 if (fnweight >= FW_HEAVY) return "heavy";
4745 if (fnweight >= FW_EXTRABOLD) return "extrabold";
4746 if (fnweight >= FW_BOLD) return "bold";
03f8fb34 4747 if (fnweight >= FW_SEMIBOLD) return "demibold";
5ac45f98
GV
4748 if (fnweight >= FW_MEDIUM) return "medium";
4749 if (fnweight >= FW_NORMAL) return "normal";
4750 if (fnweight >= FW_LIGHT) return "light";
4751 if (fnweight >= FW_EXTRALIGHT) return "extralight";
4752 if (fnweight >= FW_THIN) return "thin";
4753 else
4754 return "*";
4755}
4756
8edb0a6f 4757static LONG
fbd6baed 4758x_to_w32_charset (lpcs)
5ac45f98
GV
4759 char * lpcs;
4760{
767b1ff0 4761 Lisp_Object this_entry, w32_charset;
8b77111c
AI
4762 char *charset;
4763 int len = strlen (lpcs);
4764
4765 /* Support "*-#nnn" format for unknown charsets. */
4766 if (strncmp (lpcs, "*-#", 3) == 0)
4767 return atoi (lpcs + 3);
4768
4769 /* Handle wildcards by ignoring them; eg. treat "big5*-*" as "big5". */
4770 charset = alloca (len + 1);
4771 strcpy (charset, lpcs);
4772 lpcs = strchr (charset, '*');
4773 if (lpcs)
4774 *lpcs = 0;
4587b026 4775
dfff8a69
JR
4776 /* Look through w32-charset-info-alist for the character set.
4777 Format of each entry is
4778 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
4779 */
8b77111c 4780 this_entry = Fassoc (build_string(charset), Vw32_charset_info_alist);
4587b026 4781
767b1ff0
JR
4782 if (NILP(this_entry))
4783 {
4784 /* At startup, we want iso8859-1 fonts to come up properly. */
8b77111c 4785 if (stricmp(charset, "iso8859-1") == 0)
767b1ff0
JR
4786 return ANSI_CHARSET;
4787 else
4788 return DEFAULT_CHARSET;
4789 }
4790
4791 w32_charset = Fcar (Fcdr (this_entry));
4792
d84b082d 4793 /* Translate Lisp symbol to number. */
2ba49441 4794 if (EQ (w32_charset, Qw32_charset_ansi))
767b1ff0 4795 return ANSI_CHARSET;
2ba49441 4796 if (EQ (w32_charset, Qw32_charset_symbol))
767b1ff0 4797 return SYMBOL_CHARSET;
2ba49441 4798 if (EQ (w32_charset, Qw32_charset_shiftjis))
767b1ff0 4799 return SHIFTJIS_CHARSET;
2ba49441 4800 if (EQ (w32_charset, Qw32_charset_hangeul))
767b1ff0 4801 return HANGEUL_CHARSET;
2ba49441 4802 if (EQ (w32_charset, Qw32_charset_chinesebig5))
767b1ff0 4803 return CHINESEBIG5_CHARSET;
2ba49441 4804 if (EQ (w32_charset, Qw32_charset_gb2312))
767b1ff0 4805 return GB2312_CHARSET;
2ba49441 4806 if (EQ (w32_charset, Qw32_charset_oem))
767b1ff0 4807 return OEM_CHARSET;
dfff8a69 4808#ifdef JOHAB_CHARSET
2ba49441 4809 if (EQ (w32_charset, Qw32_charset_johab))
767b1ff0 4810 return JOHAB_CHARSET;
2ba49441 4811 if (EQ (w32_charset, Qw32_charset_easteurope))
767b1ff0 4812 return EASTEUROPE_CHARSET;
2ba49441 4813 if (EQ (w32_charset, Qw32_charset_turkish))
767b1ff0 4814 return TURKISH_CHARSET;
2ba49441 4815 if (EQ (w32_charset, Qw32_charset_baltic))
767b1ff0 4816 return BALTIC_CHARSET;
2ba49441 4817 if (EQ (w32_charset, Qw32_charset_russian))
767b1ff0 4818 return RUSSIAN_CHARSET;
2ba49441 4819 if (EQ (w32_charset, Qw32_charset_arabic))
767b1ff0 4820 return ARABIC_CHARSET;
2ba49441 4821 if (EQ (w32_charset, Qw32_charset_greek))
767b1ff0 4822 return GREEK_CHARSET;
2ba49441 4823 if (EQ (w32_charset, Qw32_charset_hebrew))
767b1ff0 4824 return HEBREW_CHARSET;
2ba49441 4825 if (EQ (w32_charset, Qw32_charset_vietnamese))
767b1ff0 4826 return VIETNAMESE_CHARSET;
2ba49441 4827 if (EQ (w32_charset, Qw32_charset_thai))
767b1ff0 4828 return THAI_CHARSET;
2ba49441 4829 if (EQ (w32_charset, Qw32_charset_mac))
767b1ff0 4830 return MAC_CHARSET;
dfff8a69 4831#endif /* JOHAB_CHARSET */
5ac45f98 4832#ifdef UNICODE_CHARSET
2ba49441 4833 if (EQ (w32_charset, Qw32_charset_unicode))
767b1ff0 4834 return UNICODE_CHARSET;
5ac45f98 4835#endif
dfff8a69
JR
4836
4837 return DEFAULT_CHARSET;
5ac45f98
GV
4838}
4839
dfff8a69 4840
8edb0a6f 4841static char *
fbd6baed 4842w32_to_x_charset (fncharset)
5ac45f98
GV
4843 int fncharset;
4844{
5e905a57 4845 static char buf[32];
767b1ff0 4846 Lisp_Object charset_type;
1edf84e7 4847
5ac45f98
GV
4848 switch (fncharset)
4849 {
767b1ff0
JR
4850 case ANSI_CHARSET:
4851 /* Handle startup case of w32-charset-info-alist not
4852 being set up yet. */
4853 if (NILP(Vw32_charset_info_alist))
4854 return "iso8859-1";
4855 charset_type = Qw32_charset_ansi;
4856 break;
4857 case DEFAULT_CHARSET:
4858 charset_type = Qw32_charset_default;
4859 break;
4860 case SYMBOL_CHARSET:
4861 charset_type = Qw32_charset_symbol;
4862 break;
4863 case SHIFTJIS_CHARSET:
4864 charset_type = Qw32_charset_shiftjis;
4865 break;
4866 case HANGEUL_CHARSET:
4867 charset_type = Qw32_charset_hangeul;
4868 break;
4869 case GB2312_CHARSET:
4870 charset_type = Qw32_charset_gb2312;
4871 break;
4872 case CHINESEBIG5_CHARSET:
4873 charset_type = Qw32_charset_chinesebig5;
4874 break;
4875 case OEM_CHARSET:
4876 charset_type = Qw32_charset_oem;
4877 break;
4587b026
GV
4878
4879 /* More recent versions of Windows (95 and NT4.0) define more
4880 character sets. */
4881#ifdef EASTEUROPE_CHARSET
767b1ff0
JR
4882 case EASTEUROPE_CHARSET:
4883 charset_type = Qw32_charset_easteurope;
4884 break;
4885 case TURKISH_CHARSET:
4886 charset_type = Qw32_charset_turkish;
4887 break;
4888 case BALTIC_CHARSET:
4889 charset_type = Qw32_charset_baltic;
4890 break;
33d52f9c 4891 case RUSSIAN_CHARSET:
767b1ff0
JR
4892 charset_type = Qw32_charset_russian;
4893 break;
4894 case ARABIC_CHARSET:
4895 charset_type = Qw32_charset_arabic;
4896 break;
4897 case GREEK_CHARSET:
4898 charset_type = Qw32_charset_greek;
4899 break;
4900 case HEBREW_CHARSET:
4901 charset_type = Qw32_charset_hebrew;
4902 break;
4903 case VIETNAMESE_CHARSET:
4904 charset_type = Qw32_charset_vietnamese;
4905 break;
4906 case THAI_CHARSET:
4907 charset_type = Qw32_charset_thai;
4908 break;
4909 case MAC_CHARSET:
4910 charset_type = Qw32_charset_mac;
4911 break;
4912 case JOHAB_CHARSET:
4913 charset_type = Qw32_charset_johab;
4914 break;
4587b026
GV
4915#endif
4916
5ac45f98 4917#ifdef UNICODE_CHARSET
767b1ff0
JR
4918 case UNICODE_CHARSET:
4919 charset_type = Qw32_charset_unicode;
4920 break;
5ac45f98 4921#endif
767b1ff0
JR
4922 default:
4923 /* Encode numerical value of unknown charset. */
4924 sprintf (buf, "*-#%u", fncharset);
4925 return buf;
5ac45f98 4926 }
7d0393cf 4927
767b1ff0
JR
4928 {
4929 Lisp_Object rest;
4930 char * best_match = NULL;
4931
4932 /* Look through w32-charset-info-alist for the character set.
4933 Prefer ISO codepages, and prefer lower numbers in the ISO
4934 range. Only return charsets for codepages which are installed.
4935
4936 Format of each entry is
4937 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
4938 */
4939 for (rest = Vw32_charset_info_alist; CONSP (rest); rest = XCDR (rest))
4940 {
4941 char * x_charset;
4942 Lisp_Object w32_charset;
4943 Lisp_Object codepage;
4944
4945 Lisp_Object this_entry = XCAR (rest);
4946
4947 /* Skip invalid entries in alist. */
4948 if (!CONSP (this_entry) || !STRINGP (XCAR (this_entry))
4949 || !CONSP (XCDR (this_entry))
4950 || !SYMBOLP (XCAR (XCDR (this_entry))))
4951 continue;
4952
d5db4077 4953 x_charset = SDATA (XCAR (this_entry));
767b1ff0
JR
4954 w32_charset = XCAR (XCDR (this_entry));
4955 codepage = XCDR (XCDR (this_entry));
4956
4957 /* Look for Same charset and a valid codepage (or non-int
4958 which means ignore). */
2ba49441
JR
4959 if (EQ (w32_charset, charset_type)
4960 && (!INTEGERP (codepage) || XINT (codepage) == CP_DEFAULT
767b1ff0
JR
4961 || IsValidCodePage (XINT (codepage))))
4962 {
4963 /* If we don't have a match already, then this is the
4964 best. */
4965 if (!best_match)
4966 best_match = x_charset;
4967 /* If this is an ISO codepage, and the best so far isn't,
4968 then this is better. */
d84b082d
JR
4969 else if (strnicmp (best_match, "iso", 3) != 0
4970 && strnicmp (x_charset, "iso", 3) == 0)
767b1ff0
JR
4971 best_match = x_charset;
4972 /* If both are ISO8859 codepages, choose the one with the
4973 lowest number in the encoding field. */
d84b082d
JR
4974 else if (strnicmp (best_match, "iso8859-", 8) == 0
4975 && strnicmp (x_charset, "iso8859-", 8) == 0)
767b1ff0
JR
4976 {
4977 int best_enc = atoi (best_match + 8);
4978 int this_enc = atoi (x_charset + 8);
4979 if (this_enc > 0 && this_enc < best_enc)
4980 best_match = x_charset;
7d0393cf 4981 }
767b1ff0
JR
4982 }
4983 }
4984
4985 /* If no match, encode the numeric value. */
4986 if (!best_match)
4987 {
4988 sprintf (buf, "*-#%u", fncharset);
4989 return buf;
4990 }
4991
5e905a57
JR
4992 strncpy(buf, best_match, 31);
4993 buf[31] = '\0';
767b1ff0
JR
4994 return buf;
4995 }
ee78dc32
GV
4996}
4997
dfff8a69 4998
d84b082d
JR
4999/* Return all the X charsets that map to a font. */
5000static Lisp_Object
5001w32_to_all_x_charsets (fncharset)
5002 int fncharset;
5003{
5004 static char buf[32];
5005 Lisp_Object charset_type;
5006 Lisp_Object retval = Qnil;
5007
5008 switch (fncharset)
5009 {
5010 case ANSI_CHARSET:
5011 /* Handle startup case of w32-charset-info-alist not
5012 being set up yet. */
5013 if (NILP(Vw32_charset_info_alist))
d86c35ee
JR
5014 return Fcons (build_string ("iso8859-1"), Qnil);
5015
d84b082d
JR
5016 charset_type = Qw32_charset_ansi;
5017 break;
5018 case DEFAULT_CHARSET:
5019 charset_type = Qw32_charset_default;
5020 break;
5021 case SYMBOL_CHARSET:
5022 charset_type = Qw32_charset_symbol;
5023 break;
5024 case SHIFTJIS_CHARSET:
5025 charset_type = Qw32_charset_shiftjis;
5026 break;
5027 case HANGEUL_CHARSET:
5028 charset_type = Qw32_charset_hangeul;
5029 break;
5030 case GB2312_CHARSET:
5031 charset_type = Qw32_charset_gb2312;
5032 break;
5033 case CHINESEBIG5_CHARSET:
5034 charset_type = Qw32_charset_chinesebig5;
5035 break;
5036 case OEM_CHARSET:
5037 charset_type = Qw32_charset_oem;
5038 break;
5039
5040 /* More recent versions of Windows (95 and NT4.0) define more
5041 character sets. */
5042#ifdef EASTEUROPE_CHARSET
5043 case EASTEUROPE_CHARSET:
5044 charset_type = Qw32_charset_easteurope;
5045 break;
5046 case TURKISH_CHARSET:
5047 charset_type = Qw32_charset_turkish;
5048 break;
5049 case BALTIC_CHARSET:
5050 charset_type = Qw32_charset_baltic;
5051 break;
5052 case RUSSIAN_CHARSET:
5053 charset_type = Qw32_charset_russian;
5054 break;
5055 case ARABIC_CHARSET:
5056 charset_type = Qw32_charset_arabic;
5057 break;
5058 case GREEK_CHARSET:
5059 charset_type = Qw32_charset_greek;
5060 break;
5061 case HEBREW_CHARSET:
5062 charset_type = Qw32_charset_hebrew;
5063 break;
5064 case VIETNAMESE_CHARSET:
5065 charset_type = Qw32_charset_vietnamese;
5066 break;
5067 case THAI_CHARSET:
5068 charset_type = Qw32_charset_thai;
5069 break;
5070 case MAC_CHARSET:
5071 charset_type = Qw32_charset_mac;
5072 break;
5073 case JOHAB_CHARSET:
5074 charset_type = Qw32_charset_johab;
5075 break;
5076#endif
5077
5078#ifdef UNICODE_CHARSET
5079 case UNICODE_CHARSET:
5080 charset_type = Qw32_charset_unicode;
5081 break;
5082#endif
5083 default:
5084 /* Encode numerical value of unknown charset. */
5085 sprintf (buf, "*-#%u", fncharset);
5086 return Fcons (build_string (buf), Qnil);
5087 }
7d0393cf 5088
d84b082d
JR
5089 {
5090 Lisp_Object rest;
5091 /* Look through w32-charset-info-alist for the character set.
5092 Only return charsets for codepages which are installed.
5093
5094 Format of each entry in Vw32_charset_info_alist is
5095 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
5096 */
5097 for (rest = Vw32_charset_info_alist; CONSP (rest); rest = XCDR (rest))
5098 {
5099 Lisp_Object x_charset;
5100 Lisp_Object w32_charset;
5101 Lisp_Object codepage;
5102
5103 Lisp_Object this_entry = XCAR (rest);
5104
5105 /* Skip invalid entries in alist. */
5106 if (!CONSP (this_entry) || !STRINGP (XCAR (this_entry))
5107 || !CONSP (XCDR (this_entry))
5108 || !SYMBOLP (XCAR (XCDR (this_entry))))
5109 continue;
5110
5111 x_charset = XCAR (this_entry);
5112 w32_charset = XCAR (XCDR (this_entry));
5113 codepage = XCDR (XCDR (this_entry));
5114
5115 /* Look for Same charset and a valid codepage (or non-int
5116 which means ignore). */
2ba49441
JR
5117 if (EQ (w32_charset, charset_type)
5118 && (!INTEGERP (codepage) || XINT (codepage) == CP_DEFAULT
d84b082d
JR
5119 || IsValidCodePage (XINT (codepage))))
5120 {
5121 retval = Fcons (x_charset, retval);
5122 }
5123 }
5124
5125 /* If no match, encode the numeric value. */
5126 if (NILP (retval))
5127 {
5128 sprintf (buf, "*-#%u", fncharset);
5129 return Fcons (build_string (buf), Qnil);
5130 }
5131
5132 return retval;
5133 }
5134}
5135
dfff8a69
JR
5136/* Get the Windows codepage corresponding to the specified font. The
5137 charset info in the font name is used to look up
5138 w32-charset-to-codepage-alist. */
7d0393cf 5139int
dfff8a69
JR
5140w32_codepage_for_font (char *fontname)
5141{
767b1ff0
JR
5142 Lisp_Object codepage, entry;
5143 char *charset_str, *charset, *end;
dfff8a69 5144
767b1ff0 5145 if (NILP (Vw32_charset_info_alist))
dfff8a69
JR
5146 return CP_DEFAULT;
5147
767b1ff0
JR
5148 /* Extract charset part of font string. */
5149 charset = xlfd_charset_of_font (fontname);
5150
5151 if (!charset)
ceb12877 5152 return CP_UNKNOWN;
767b1ff0 5153
8b77111c 5154 charset_str = (char *) alloca (strlen (charset) + 1);
767b1ff0
JR
5155 strcpy (charset_str, charset);
5156
8b77111c 5157#if 0
dfff8a69
JR
5158 /* Remove leading "*-". */
5159 if (strncmp ("*-", charset_str, 2) == 0)
5160 charset = charset_str + 2;
5161 else
8b77111c 5162#endif
dfff8a69
JR
5163 charset = charset_str;
5164
5165 /* Stop match at wildcard (including preceding '-'). */
5166 if (end = strchr (charset, '*'))
5167 {
5168 if (end > charset && *(end-1) == '-')
5169 end--;
5170 *end = '\0';
5171 }
5172
767b1ff0
JR
5173 entry = Fassoc (build_string(charset), Vw32_charset_info_alist);
5174 if (NILP (entry))
ceb12877 5175 return CP_UNKNOWN;
767b1ff0
JR
5176
5177 codepage = Fcdr (Fcdr (entry));
5178
5179 if (NILP (codepage))
5180 return CP_8BIT;
5181 else if (XFASTINT (codepage) == XFASTINT (Qt))
5182 return CP_UNICODE;
5183 else if (INTEGERP (codepage))
dfff8a69
JR
5184 return XINT (codepage);
5185 else
ceb12877 5186 return CP_UNKNOWN;
dfff8a69
JR
5187}
5188
5189
7d0393cf 5190static BOOL
767b1ff0 5191w32_to_x_font (lplogfont, lpxstr, len, specific_charset)
ee78dc32
GV
5192 LOGFONT * lplogfont;
5193 char * lpxstr;
5194 int len;
767b1ff0 5195 char * specific_charset;
ee78dc32 5196{
6fc2811b 5197 char* fonttype;
f46e6225 5198 char *fontname;
3cb20f4a
RS
5199 char height_pixels[8];
5200 char height_dpi[8];
5201 char width_pixels[8];
4587b026 5202 char *fontname_dash;
ac849ba4
JR
5203 int display_resy = (int) one_w32_display_info.resy;
5204 int display_resx = (int) one_w32_display_info.resx;
f46e6225
GV
5205 int bufsz;
5206 struct coding_system coding;
3cb20f4a
RS
5207
5208 if (!lpxstr) abort ();
ee78dc32 5209
3cb20f4a
RS
5210 if (!lplogfont)
5211 return FALSE;
5212
6fc2811b
JR
5213 if (lplogfont->lfOutPrecision == OUT_STRING_PRECIS)
5214 fonttype = "raster";
5215 else if (lplogfont->lfOutPrecision == OUT_STROKE_PRECIS)
5216 fonttype = "outline";
5217 else
5218 fonttype = "unknown";
5219
1fa3a200 5220 setup_coding_system (Fcheck_coding_system (Vlocale_coding_system),
f46e6225 5221 &coding);
aab5ac44
KH
5222 coding.src_multibyte = 0;
5223 coding.dst_multibyte = 1;
f46e6225 5224 coding.mode |= CODING_MODE_LAST_BLOCK;
65413122
KH
5225 /* We explicitely disable composition handling because selection
5226 data should not contain any composition sequence. */
5227 coding.composing = COMPOSITION_DISABLED;
f46e6225
GV
5228 bufsz = decoding_buffer_size (&coding, LF_FACESIZE);
5229
5230 fontname = alloca(sizeof(*fontname) * bufsz);
5231 decode_coding (&coding, lplogfont->lfFaceName, fontname,
5232 strlen(lplogfont->lfFaceName), bufsz - 1);
5233 *(fontname + coding.produced) = '\0';
4587b026
GV
5234
5235 /* Replace dashes with underscores so the dashes are not
f46e6225 5236 misinterpreted. */
4587b026
GV
5237 fontname_dash = fontname;
5238 while (fontname_dash = strchr (fontname_dash, '-'))
5239 *fontname_dash = '_';
5240
3cb20f4a 5241 if (lplogfont->lfHeight)
ee78dc32 5242 {
3cb20f4a
RS
5243 sprintf (height_pixels, "%u", abs (lplogfont->lfHeight));
5244 sprintf (height_dpi, "%u",
33d52f9c 5245 abs (lplogfont->lfHeight) * 720 / display_resy);
5ac45f98
GV
5246 }
5247 else
ee78dc32 5248 {
3cb20f4a
RS
5249 strcpy (height_pixels, "*");
5250 strcpy (height_dpi, "*");
ee78dc32 5251 }
3cb20f4a
RS
5252 if (lplogfont->lfWidth)
5253 sprintf (width_pixels, "%u", lplogfont->lfWidth * 10);
5254 else
5255 strcpy (width_pixels, "*");
5256
5257 _snprintf (lpxstr, len - 1,
6fc2811b
JR
5258 "-%s-%s-%s-%c-normal-normal-%s-%s-%d-%d-%c-%s-%s",
5259 fonttype, /* foundry */
4587b026
GV
5260 fontname, /* family */
5261 w32_to_x_weight (lplogfont->lfWeight), /* weight */
5262 lplogfont->lfItalic?'i':'r', /* slant */
5263 /* setwidth name */
5264 /* add style name */
5265 height_pixels, /* pixel size */
5266 height_dpi, /* point size */
33d52f9c
GV
5267 display_resx, /* resx */
5268 display_resy, /* resy */
4587b026
GV
5269 ((lplogfont->lfPitchAndFamily & 0x3) == VARIABLE_PITCH)
5270 ? 'p' : 'c', /* spacing */
5271 width_pixels, /* avg width */
767b1ff0 5272 specific_charset ? specific_charset
7d0393cf 5273 : w32_to_x_charset (lplogfont->lfCharSet)
767b1ff0 5274 /* charset registry and encoding */
3cb20f4a
RS
5275 );
5276
ee78dc32
GV
5277 lpxstr[len - 1] = 0; /* just to be sure */
5278 return (TRUE);
5279}
5280
7d0393cf 5281static BOOL
fbd6baed 5282x_to_w32_font (lpxstr, lplogfont)
ee78dc32
GV
5283 char * lpxstr;
5284 LOGFONT * lplogfont;
5285{
f46e6225
GV
5286 struct coding_system coding;
5287
ee78dc32 5288 if (!lplogfont) return (FALSE);
f46e6225 5289
ee78dc32 5290 memset (lplogfont, 0, sizeof (*lplogfont));
5ac45f98 5291
1a292d24 5292 /* Set default value for each field. */
771c47d5 5293#if 1
ee78dc32
GV
5294 lplogfont->lfOutPrecision = OUT_DEFAULT_PRECIS;
5295 lplogfont->lfClipPrecision = CLIP_DEFAULT_PRECIS;
5296 lplogfont->lfQuality = DEFAULT_QUALITY;
5ac45f98
GV
5297#else
5298 /* go for maximum quality */
5299 lplogfont->lfOutPrecision = OUT_STROKE_PRECIS;
5300 lplogfont->lfClipPrecision = CLIP_STROKE_PRECIS;
5301 lplogfont->lfQuality = PROOF_QUALITY;
5302#endif
5303
1a292d24
AI
5304 lplogfont->lfCharSet = DEFAULT_CHARSET;
5305 lplogfont->lfWeight = FW_DONTCARE;
5306 lplogfont->lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
5307
5ac45f98
GV
5308 if (!lpxstr)
5309 return FALSE;
5310
5311 /* Provide a simple escape mechanism for specifying Windows font names
5312 * directly -- if font spec does not beginning with '-', assume this
5313 * format:
5314 * "<font name>[:height in pixels[:width in pixels[:weight]]]"
5315 */
7d0393cf 5316
5ac45f98
GV
5317 if (*lpxstr == '-')
5318 {
33d52f9c
GV
5319 int fields, tem;
5320 char name[50], weight[20], slant, pitch, pixels[10], height[10],
8b77111c 5321 width[10], resy[10], remainder[50];
5ac45f98 5322 char * encoding;
ac849ba4 5323 int dpi = (int) one_w32_display_info.resy;
5ac45f98
GV
5324
5325 fields = sscanf (lpxstr,
8b77111c 5326 "-%*[^-]-%49[^-]-%19[^-]-%c-%*[^-]-%*[^-]-%9[^-]-%9[^-]-%*[^-]-%9[^-]-%c-%9[^-]-%49s",
33d52f9c 5327 name, weight, &slant, pixels, height, resy, &pitch, width, remainder);
8b77111c
AI
5328 if (fields == EOF)
5329 return (FALSE);
5330
5331 /* In the general case when wildcards cover more than one field,
5332 we don't know which field is which, so don't fill any in.
5333 However, we need to cope with this particular form, which is
5334 generated by font_list_1 (invoked by try_font_list):
5335 "-raster-6x10-*-gb2312*-*"
5336 and make sure to correctly parse the charset field. */
5337 if (fields == 3)
5338 {
5339 fields = sscanf (lpxstr,
5340 "-%*[^-]-%49[^-]-*-%49s",
5341 name, remainder);
5342 }
5343 else if (fields < 9)
5344 {
5345 fields = 0;
5346 remainder[0] = 0;
5347 }
6fc2811b 5348
5ac45f98
GV
5349 if (fields > 0 && name[0] != '*')
5350 {
8ea3e054
RS
5351 int bufsize;
5352 unsigned char *buf;
5353
f46e6225 5354 setup_coding_system
1fa3a200 5355 (Fcheck_coding_system (Vlocale_coding_system), &coding);
aab5ac44
KH
5356 coding.src_multibyte = 1;
5357 coding.dst_multibyte = 1;
2883d842
JR
5358 /* Need to set COMPOSITION_DISABLED, otherwise Emacs crashes in
5359 encode_coding_iso2022 trying to dereference a null pointer. */
5360 coding.composing = COMPOSITION_DISABLED;
5361 if (coding.type == coding_type_iso2022)
5362 coding.flags |= CODING_FLAG_ISO_SAFE;
8ea3e054
RS
5363 bufsize = encoding_buffer_size (&coding, strlen (name));
5364 buf = (unsigned char *) alloca (bufsize);
f46e6225 5365 coding.mode |= CODING_MODE_LAST_BLOCK;
8ea3e054
RS
5366 encode_coding (&coding, name, buf, strlen (name), bufsize);
5367 if (coding.produced >= LF_FACESIZE)
5368 coding.produced = LF_FACESIZE - 1;
5369 buf[coding.produced] = 0;
5370 strcpy (lplogfont->lfFaceName, buf);
5ac45f98
GV
5371 }
5372 else
5373 {
6fc2811b 5374 lplogfont->lfFaceName[0] = '\0';
5ac45f98
GV
5375 }
5376
5377 fields--;
5378
fbd6baed 5379 lplogfont->lfWeight = x_to_w32_weight ((fields > 0 ? weight : ""));
5ac45f98
GV
5380
5381 fields--;
5382
c8874f14 5383 lplogfont->lfItalic = (fields > 0 && slant == 'i');
5ac45f98
GV
5384
5385 fields--;
5386
5387 if (fields > 0 && pixels[0] != '*')
5388 lplogfont->lfHeight = atoi (pixels);
5389
5390 fields--;
5ac45f98 5391 fields--;
33d52f9c
GV
5392 if (fields > 0 && resy[0] != '*')
5393 {
6fc2811b 5394 tem = atoi (resy);
33d52f9c
GV
5395 if (tem > 0) dpi = tem;
5396 }
5ac45f98 5397
33d52f9c
GV
5398 if (fields > -1 && lplogfont->lfHeight == 0 && height[0] != '*')
5399 lplogfont->lfHeight = atoi (height) * dpi / 720;
5400
5401 if (fields > 0)
5ac45f98
GV
5402 lplogfont->lfPitchAndFamily =
5403 (fields > 0 && pitch == 'p') ? VARIABLE_PITCH : FIXED_PITCH;
5404
5405 fields--;
5406
5407 if (fields > 0 && width[0] != '*')
5408 lplogfont->lfWidth = atoi (width) / 10;
5409
5410 fields--;
5411
4587b026 5412 /* Strip the trailing '-' if present. (it shouldn't be, as it
d88c567c 5413 fails the test against xlfd-tight-regexp in fontset.el). */
3c190163 5414 {
5ac45f98
GV
5415 int len = strlen (remainder);
5416 if (len > 0 && remainder[len-1] == '-')
5417 remainder[len-1] = 0;
ee78dc32 5418 }
5ac45f98 5419 encoding = remainder;
8b77111c 5420#if 0
5ac45f98
GV
5421 if (strncmp (encoding, "*-", 2) == 0)
5422 encoding += 2;
8b77111c
AI
5423#endif
5424 lplogfont->lfCharSet = x_to_w32_charset (encoding);
5ac45f98
GV
5425 }
5426 else
5427 {
5428 int fields;
5429 char name[100], height[10], width[10], weight[20];
a1a80b40 5430
5ac45f98
GV
5431 fields = sscanf (lpxstr,
5432 "%99[^:]:%9[^:]:%9[^:]:%19s",
5433 name, height, width, weight);
5434
5435 if (fields == EOF) return (FALSE);
5436
5437 if (fields > 0)
5438 {
5439 strncpy (lplogfont->lfFaceName,name, LF_FACESIZE);
5440 lplogfont->lfFaceName[LF_FACESIZE-1] = 0;
5441 }
5442 else
5443 {
5444 lplogfont->lfFaceName[0] = 0;
5445 }
5446
5447 fields--;
5448
5449 if (fields > 0)
5450 lplogfont->lfHeight = atoi (height);
5451
5452 fields--;
5453
5454 if (fields > 0)
5455 lplogfont->lfWidth = atoi (width);
5456
5457 fields--;
5458
fbd6baed 5459 lplogfont->lfWeight = x_to_w32_weight ((fields > 0 ? weight : ""));
5ac45f98
GV
5460 }
5461
5462 /* This makes TrueType fonts work better. */
5463 lplogfont->lfHeight = - abs (lplogfont->lfHeight);
6fc2811b 5464
ee78dc32
GV
5465 return (TRUE);
5466}
5467
d88c567c
JR
5468/* Strip the pixel height and point height from the given xlfd, and
5469 return the pixel height. If no pixel height is specified, calculate
5470 one from the point height, or if that isn't defined either, return
5471 0 (which usually signifies a scalable font).
5472*/
8edb0a6f
JR
5473static int
5474xlfd_strip_height (char *fontname)
d88c567c 5475{
8edb0a6f 5476 int pixel_height, field_number;
d88c567c
JR
5477 char *read_from, *write_to;
5478
5479 xassert (fontname);
5480
5481 pixel_height = field_number = 0;
5482 write_to = NULL;
5483
5484 /* Look for height fields. */
5485 for (read_from = fontname; *read_from; read_from++)
5486 {
5487 if (*read_from == '-')
5488 {
5489 field_number++;
5490 if (field_number == 7) /* Pixel height. */
5491 {
5492 read_from++;
5493 write_to = read_from;
5494
5495 /* Find end of field. */
5496 for (;*read_from && *read_from != '-'; read_from++)
5497 ;
5498
5499 /* Split the fontname at end of field. */
5500 if (*read_from)
5501 {
5502 *read_from = '\0';
5503 read_from++;
5504 }
5505 pixel_height = atoi (write_to);
5506 /* Blank out field. */
5507 if (read_from > write_to)
5508 {
5509 *write_to = '-';
5510 write_to++;
5511 }
767b1ff0 5512 /* If the pixel height field is at the end (partial xlfd),
d88c567c
JR
5513 return now. */
5514 else
5515 return pixel_height;
5516
5517 /* If we got a pixel height, the point height can be
5518 ignored. Just blank it out and break now. */
5519 if (pixel_height)
5520 {
5521 /* Find end of point size field. */
5522 for (; *read_from && *read_from != '-'; read_from++)
5523 ;
5524
5525 if (*read_from)
5526 read_from++;
5527
5528 /* Blank out the point size field. */
5529 if (read_from > write_to)
5530 {
5531 *write_to = '-';
5532 write_to++;
5533 }
5534 else
5535 return pixel_height;
5536
5537 break;
5538 }
5539 /* If the point height is already blank, break now. */
5540 if (*read_from == '-')
5541 {
5542 read_from++;
5543 break;
5544 }
5545 }
5546 else if (field_number == 8)
5547 {
5548 /* If we didn't get a pixel height, try to get the point
5549 height and convert that. */
5550 int point_size;
5551 char *point_size_start = read_from++;
5552
5553 /* Find end of field. */
5554 for (; *read_from && *read_from != '-'; read_from++)
5555 ;
5556
5557 if (*read_from)
5558 {
5559 *read_from = '\0';
5560 read_from++;
5561 }
5562
5563 point_size = atoi (point_size_start);
5564
5565 /* Convert to pixel height. */
5566 pixel_height = point_size
5567 * one_w32_display_info.height_in / 720;
5568
5569 /* Blank out this field and break. */
5570 *write_to = '-';
5571 write_to++;
5572 break;
5573 }
5574 }
5575 }
5576
5577 /* Shift the rest of the font spec into place. */
5578 if (write_to && read_from > write_to)
5579 {
5580 for (; *read_from; read_from++, write_to++)
5581 *write_to = *read_from;
5582 *write_to = '\0';
5583 }
5584
5585 return pixel_height;
5586}
5587
6fc2811b 5588/* Assume parameter 1 is fully qualified, no wildcards. */
7d0393cf 5589static BOOL
6fc2811b
JR
5590w32_font_match (fontname, pattern)
5591 char * fontname;
5592 char * pattern;
ee78dc32 5593{
e7c72122 5594 char *regex = alloca (strlen (pattern) * 2 + 3);
d88c567c 5595 char *font_name_copy = alloca (strlen (fontname) + 1);
6fc2811b 5596 char *ptr;
ee78dc32 5597
d88c567c
JR
5598 /* Copy fontname so we can modify it during comparison. */
5599 strcpy (font_name_copy, fontname);
5600
6fc2811b
JR
5601 ptr = regex;
5602 *ptr++ = '^';
ee78dc32 5603
6fc2811b
JR
5604 /* Turn pattern into a regexp and do a regexp match. */
5605 for (; *pattern; pattern++)
5606 {
5607 if (*pattern == '?')
5608 *ptr++ = '.';
5609 else if (*pattern == '*')
5610 {
5611 *ptr++ = '.';
5612 *ptr++ = '*';
5613 }
33d52f9c 5614 else
6fc2811b 5615 *ptr++ = *pattern;
ee78dc32 5616 }
6fc2811b
JR
5617 *ptr = '$';
5618 *(ptr + 1) = '\0';
5619
d88c567c
JR
5620 /* Strip out font heights and compare them seperately, since
5621 rounding error can cause mismatches. This also allows a
5622 comparison between a font that declares only a pixel height and a
5623 pattern that declares the point height.
5624 */
5625 {
5626 int font_height, pattern_height;
5627
5628 font_height = xlfd_strip_height (font_name_copy);
5629 pattern_height = xlfd_strip_height (regex);
5630
5631 /* Compare now, and don't bother doing expensive regexp matching
5632 if the heights differ. */
5633 if (font_height && pattern_height && (font_height != pattern_height))
5634 return FALSE;
5635 }
5636
6fc2811b 5637 return (fast_c_string_match_ignore_case (build_string (regex),
d88c567c 5638 font_name_copy) >= 0);
ee78dc32
GV
5639}
5640
5ca0cd71
GV
5641/* Callback functions, and a structure holding info they need, for
5642 listing system fonts on W32. We need one set of functions to do the
5643 job properly, but these don't work on NT 3.51 and earlier, so we
5644 have a second set which don't handle character sets properly to
5645 fall back on.
5646
5647 In both cases, there are two passes made. The first pass gets one
5648 font from each family, the second pass lists all the fonts from
5649 each family. */
5650
7d0393cf 5651typedef struct enumfont_t
ee78dc32
GV
5652{
5653 HDC hdc;
5654 int numFonts;
3cb20f4a 5655 LOGFONT logfont;
ee78dc32 5656 XFontStruct *size_ref;
23afac8f 5657 Lisp_Object pattern;
d84b082d 5658 Lisp_Object list;
ee78dc32
GV
5659} enumfont_t;
5660
d84b082d
JR
5661
5662static void
5663enum_font_maybe_add_to_list (enumfont_t *, LOGFONT *, char *, Lisp_Object);
5664
5665
7d0393cf 5666static int CALLBACK
ee78dc32
GV
5667enum_font_cb2 (lplf, lptm, FontType, lpef)
5668 ENUMLOGFONT * lplf;
5669 NEWTEXTMETRIC * lptm;
5670 int FontType;
5671 enumfont_t * lpef;
5672{
66895301
JR
5673 /* Ignore struck out and underlined versions of fonts. */
5674 if (lplf->elfLogFont.lfStrikeOut || lplf->elfLogFont.lfUnderline)
5675 return 1;
5676
5677 /* Only return fonts with names starting with @ if they were
5678 explicitly specified, since Microsoft uses an initial @ to
5679 denote fonts for vertical writing, without providing a more
5680 convenient way of identifying them. */
5681 if (lplf->elfLogFont.lfFaceName[0] == '@'
5682 && lpef->logfont.lfFaceName[0] != '@')
5e905a57
JR
5683 return 1;
5684
4587b026
GV
5685 /* Check that the character set matches if it was specified */
5686 if (lpef->logfont.lfCharSet != DEFAULT_CHARSET &&
5687 lplf->elfLogFont.lfCharSet != lpef->logfont.lfCharSet)
5e905a57 5688 return 1;
4587b026 5689
6358474d
JR
5690 if (FontType == RASTER_FONTTYPE)
5691 {
5692 /* DBCS raster fonts have problems displaying, so skip them. */
5693 int charset = lplf->elfLogFont.lfCharSet;
5694 if (charset == SHIFTJIS_CHARSET
5695 || charset == HANGEUL_CHARSET
5696 || charset == CHINESEBIG5_CHARSET
5697 || charset == GB2312_CHARSET
5698#ifdef JOHAB_CHARSET
5699 || charset == JOHAB_CHARSET
5700#endif
5701 )
5702 return 1;
5703 }
5704
ee78dc32
GV
5705 {
5706 char buf[100];
4587b026 5707 Lisp_Object width = Qnil;
d84b082d 5708 Lisp_Object charset_list = Qnil;
767b1ff0 5709 char *charset = NULL;
ee78dc32 5710
6fc2811b
JR
5711 /* Truetype fonts do not report their true metrics until loaded */
5712 if (FontType != RASTER_FONTTYPE)
3cb20f4a 5713 {
23afac8f 5714 if (!NILP (lpef->pattern))
6fc2811b
JR
5715 {
5716 /* Scalable fonts are as big as you want them to be. */
5717 lplf->elfLogFont.lfHeight = lpef->logfont.lfHeight;
5718 lplf->elfLogFont.lfWidth = lpef->logfont.lfWidth;
5719 width = make_number (lpef->logfont.lfWidth);
5720 }
5721 else
5722 {
5723 lplf->elfLogFont.lfHeight = 0;
5724 lplf->elfLogFont.lfWidth = 0;
5725 }
3cb20f4a 5726 }
6fc2811b 5727
f46e6225
GV
5728 /* Make sure the height used here is the same as everywhere
5729 else (ie character height, not cell height). */
6fc2811b
JR
5730 if (lplf->elfLogFont.lfHeight > 0)
5731 {
5732 /* lptm can be trusted for RASTER fonts, but not scalable ones. */
5733 if (FontType == RASTER_FONTTYPE)
5734 lplf->elfLogFont.lfHeight = lptm->tmInternalLeading - lptm->tmHeight;
5735 else
5736 lplf->elfLogFont.lfHeight = -lplf->elfLogFont.lfHeight;
5737 }
4587b026 5738
23afac8f 5739 if (!NILP (lpef->pattern))
767b1ff0 5740 {
d5db4077 5741 charset = xlfd_charset_of_font (SDATA (lpef->pattern));
767b1ff0 5742
644cefdf
JR
5743 /* We already checked charsets above, but DEFAULT_CHARSET
5744 slipped through. So only allow exact matches for DEFAULT_CHARSET. */
5745 if (charset
5746 && strncmp (charset, "*-*", 3) != 0
5747 && lpef->logfont.lfCharSet == DEFAULT_CHARSET
5748 && strcmp (charset, w32_to_x_charset (DEFAULT_CHARSET)) != 0)
5749 return 1;
767b1ff0
JR
5750 }
5751
d84b082d
JR
5752 if (charset)
5753 charset_list = Fcons (build_string (charset), Qnil);
5754 else
5755 charset_list = w32_to_all_x_charsets (lplf->elfLogFont.lfCharSet);
ee78dc32 5756
d84b082d
JR
5757 /* Loop through the charsets. */
5758 for ( ; CONSP (charset_list); charset_list = Fcdr (charset_list))
ee78dc32 5759 {
d84b082d 5760 Lisp_Object this_charset = Fcar (charset_list);
d5db4077 5761 charset = SDATA (this_charset);
d84b082d
JR
5762
5763 /* List bold and italic variations if w32-enable-synthesized-fonts
5764 is non-nil and this is a plain font. */
5765 if (w32_enable_synthesized_fonts
5766 && lplf->elfLogFont.lfWeight == FW_NORMAL
5767 && lplf->elfLogFont.lfItalic == FALSE)
5768 {
5769 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5770 charset, width);
5771 /* bold. */
5772 lplf->elfLogFont.lfWeight = FW_BOLD;
5773 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5774 charset, width);
5775 /* bold italic. */
5776 lplf->elfLogFont.lfItalic = TRUE;
5777 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5778 charset, width);
5779 /* italic. */
5780 lplf->elfLogFont.lfWeight = FW_NORMAL;
5781 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5782 charset, width);
5783 }
5784 else
5785 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5786 charset, width);
ee78dc32
GV
5787 }
5788 }
6fc2811b 5789
5e905a57 5790 return 1;
ee78dc32
GV
5791}
5792
d84b082d
JR
5793static void
5794enum_font_maybe_add_to_list (lpef, logfont, match_charset, width)
5795 enumfont_t * lpef;
5796 LOGFONT * logfont;
5797 char * match_charset;
5798 Lisp_Object width;
5799{
5800 char buf[100];
5801
5802 if (!w32_to_x_font (logfont, buf, 100, match_charset))
5803 return;
5804
23afac8f 5805 if (NILP (lpef->pattern)
d5db4077 5806 || w32_font_match (buf, SDATA (lpef->pattern)))
d84b082d
JR
5807 {
5808 /* Check if we already listed this font. This may happen if
5809 w32_enable_synthesized_fonts is non-nil, and there are real
5810 bold and italic versions of the font. */
5811 Lisp_Object font_name = build_string (buf);
5812 if (NILP (Fmember (font_name, lpef->list)))
5813 {
23afac8f
JR
5814 Lisp_Object entry = Fcons (font_name, width);
5815 lpef->list = Fcons (entry, lpef->list);
d84b082d
JR
5816 lpef->numFonts++;
5817 }
5818 }
5819}
5820
5821
7d0393cf 5822static int CALLBACK
ee78dc32
GV
5823enum_font_cb1 (lplf, lptm, FontType, lpef)
5824 ENUMLOGFONT * lplf;
5825 NEWTEXTMETRIC * lptm;
5826 int FontType;
5827 enumfont_t * lpef;
5828{
5829 return EnumFontFamilies (lpef->hdc,
5830 lplf->elfLogFont.lfFaceName,
5831 (FONTENUMPROC) enum_font_cb2,
5832 (LPARAM) lpef);
5833}
5834
5835
8edb0a6f 5836static int CALLBACK
5ca0cd71
GV
5837enum_fontex_cb2 (lplf, lptm, font_type, lpef)
5838 ENUMLOGFONTEX * lplf;
5839 NEWTEXTMETRICEX * lptm;
5840 int font_type;
5841 enumfont_t * lpef;
5842{
5843 /* We are not interested in the extra info we get back from the 'Ex
5844 version - only the fact that we get character set variations
5845 enumerated seperately. */
5846 return enum_font_cb2 ((ENUMLOGFONT *) lplf, (NEWTEXTMETRIC *) lptm,
5847 font_type, lpef);
5848}
5849
8edb0a6f 5850static int CALLBACK
5ca0cd71
GV
5851enum_fontex_cb1 (lplf, lptm, font_type, lpef)
5852 ENUMLOGFONTEX * lplf;
5853 NEWTEXTMETRICEX * lptm;
5854 int font_type;
5855 enumfont_t * lpef;
5856{
5857 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
5858 FARPROC enum_font_families_ex
5859 = GetProcAddress ( gdi32, "EnumFontFamiliesExA");
5860 /* We don't really expect EnumFontFamiliesEx to disappear once we
5861 get here, so don't bother handling it gracefully. */
5862 if (enum_font_families_ex == NULL)
5863 error ("gdi32.dll has disappeared!");
5864 return enum_font_families_ex (lpef->hdc,
5865 &lplf->elfLogFont,
5866 (FONTENUMPROC) enum_fontex_cb2,
5867 (LPARAM) lpef, 0);
5868}
5869
4587b026
GV
5870/* Interface to fontset handler. (adapted from mw32font.c in Meadow
5871 and xterm.c in Emacs 20.3) */
5872
8edb0a6f 5873static Lisp_Object w32_list_bdf_fonts (Lisp_Object pattern, int max_names)
33d52f9c
GV
5874{
5875 char *fontname, *ptnstr;
5876 Lisp_Object list, tem, newlist = Qnil;
55dcfc15 5877 int n_fonts = 0;
33d52f9c
GV
5878
5879 list = Vw32_bdf_filename_alist;
d5db4077 5880 ptnstr = SDATA (pattern);
33d52f9c 5881
8e713be6 5882 for ( ; CONSP (list); list = XCDR (list))
33d52f9c 5883 {
8e713be6 5884 tem = XCAR (list);
33d52f9c 5885 if (CONSP (tem))
d5db4077 5886 fontname = SDATA (XCAR (tem));
33d52f9c 5887 else if (STRINGP (tem))
d5db4077 5888 fontname = SDATA (tem);
33d52f9c
GV
5889 else
5890 continue;
5891
5892 if (w32_font_match (fontname, ptnstr))
5ca0cd71 5893 {
8e713be6 5894 newlist = Fcons (XCAR (tem), newlist);
5ca0cd71 5895 n_fonts++;
bd11cc09 5896 if (max_names >= 0 && n_fonts >= max_names)
5ca0cd71
GV
5897 break;
5898 }
33d52f9c
GV
5899 }
5900
5901 return newlist;
5902}
5903
5ca0cd71 5904
4587b026
GV
5905/* Return a list of names of available fonts matching PATTERN on frame
5906 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
5907 to be listed. Frame F NULL means we have not yet created any
5908 frame, which means we can't get proper size info, as we don't have
5909 a device context to use for GetTextMetrics.
bd11cc09
JR
5910 MAXNAMES sets a limit on how many fonts to match. If MAXNAMES is
5911 negative, then all matching fonts are returned. */
4587b026
GV
5912
5913Lisp_Object
dc220243
JR
5914w32_list_fonts (f, pattern, size, maxnames)
5915 struct frame *f;
5916 Lisp_Object pattern;
5917 int size;
5918 int maxnames;
4587b026 5919{
6fc2811b 5920 Lisp_Object patterns, key = Qnil, tem, tpat;
4587b026 5921 Lisp_Object list = Qnil, newlist = Qnil, second_best = Qnil;
33d52f9c 5922 struct w32_display_info *dpyinfo = &one_w32_display_info;
5ca0cd71 5923 int n_fonts = 0;
396594fe 5924
4587b026
GV
5925 patterns = Fassoc (pattern, Valternate_fontname_alist);
5926 if (NILP (patterns))
5927 patterns = Fcons (pattern, Qnil);
5928
8e713be6 5929 for (; CONSP (patterns); patterns = XCDR (patterns))
4587b026
GV
5930 {
5931 enumfont_t ef;
767b1ff0 5932 int codepage;
4587b026 5933
8e713be6 5934 tpat = XCAR (patterns);
4587b026 5935
767b1ff0
JR
5936 if (!STRINGP (tpat))
5937 continue;
5938
5939 /* Avoid expensive EnumFontFamilies functions if we are not
5940 going to be able to output one of these anyway. */
d5db4077 5941 codepage = w32_codepage_for_font (SDATA (tpat));
767b1ff0 5942 if (codepage != CP_8BIT && codepage != CP_UNICODE
ceb12877
AI
5943 && codepage != CP_DEFAULT && codepage != CP_UNKNOWN
5944 && !IsValidCodePage(codepage))
767b1ff0
JR
5945 continue;
5946
4587b026
GV
5947 /* See if we cached the result for this particular query.
5948 The cache is an alist of the form:
5949 ((PATTERN (FONTNAME . WIDTH) ...) ...)
5950 */
8e713be6 5951 if (tem = XCDR (dpyinfo->name_list_element),
33d52f9c 5952 !NILP (list = Fassoc (tpat, tem)))
4587b026
GV
5953 {
5954 list = Fcdr_safe (list);
5955 /* We have a cached list. Don't have to get the list again. */
5956 goto label_cached;
5957 }
5958
5959 BLOCK_INPUT;
5960 /* At first, put PATTERN in the cache. */
23afac8f
JR
5961 ef.pattern = tpat;
5962 ef.list = Qnil;
4587b026 5963 ef.numFonts = 0;
33d52f9c 5964
5ca0cd71
GV
5965 /* Use EnumFontFamiliesEx where it is available, as it knows
5966 about character sets. Fall back to EnumFontFamilies for
5967 older versions of NT that don't support the 'Ex function. */
d5db4077 5968 x_to_w32_font (SDATA (tpat), &ef.logfont);
4587b026 5969 {
5ca0cd71
GV
5970 LOGFONT font_match_pattern;
5971 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
5972 FARPROC enum_font_families_ex
5973 = GetProcAddress ( gdi32, "EnumFontFamiliesExA");
5974
5975 /* We do our own pattern matching so we can handle wildcards. */
5976 font_match_pattern.lfFaceName[0] = 0;
5977 font_match_pattern.lfPitchAndFamily = 0;
5978 /* We can use the charset, because if it is a wildcard it will
5979 be DEFAULT_CHARSET anyway. */
5980 font_match_pattern.lfCharSet = ef.logfont.lfCharSet;
5981
33d52f9c 5982 ef.hdc = GetDC (dpyinfo->root_window);
4587b026 5983
5ca0cd71
GV
5984 if (enum_font_families_ex)
5985 enum_font_families_ex (ef.hdc,
5986 &font_match_pattern,
5987 (FONTENUMPROC) enum_fontex_cb1,
5988 (LPARAM) &ef, 0);
5989 else
5990 EnumFontFamilies (ef.hdc, NULL, (FONTENUMPROC) enum_font_cb1,
5991 (LPARAM)&ef);
4587b026 5992
33d52f9c 5993 ReleaseDC (dpyinfo->root_window, ef.hdc);
4587b026
GV
5994 }
5995
5996 UNBLOCK_INPUT;
23afac8f 5997 list = ef.list;
4587b026
GV
5998
5999 /* Make a list of the fonts we got back.
6000 Store that in the font cache for the display. */
f3fbd155
KR
6001 XSETCDR (dpyinfo->name_list_element,
6002 Fcons (Fcons (tpat, list),
6003 XCDR (dpyinfo->name_list_element)));
4587b026
GV
6004
6005 label_cached:
6006 if (NILP (list)) continue; /* Try the remaining alternatives. */
6007
6008 newlist = second_best = Qnil;
6009
7d0393cf 6010 /* Make a list of the fonts that have the right width. */
8e713be6 6011 for (; CONSP (list); list = XCDR (list))
4587b026
GV
6012 {
6013 int found_size;
8e713be6 6014 tem = XCAR (list);
4587b026
GV
6015
6016 if (!CONSP (tem))
6017 continue;
8e713be6 6018 if (NILP (XCAR (tem)))
4587b026
GV
6019 continue;
6020 if (!size)
6021 {
8e713be6 6022 newlist = Fcons (XCAR (tem), newlist);
5ca0cd71 6023 n_fonts++;
bd11cc09 6024 if (maxnames >= 0 && n_fonts >= maxnames)
5ca0cd71
GV
6025 break;
6026 else
6027 continue;
4587b026 6028 }
8e713be6 6029 if (!INTEGERP (XCDR (tem)))
4587b026
GV
6030 {
6031 /* Since we don't yet know the size of the font, we must
6032 load it and try GetTextMetrics. */
4587b026
GV
6033 W32FontStruct thisinfo;
6034 LOGFONT lf;
6035 HDC hdc;
6036 HANDLE oldobj;
6037
d5db4077 6038 if (!x_to_w32_font (SDATA (XCAR (tem)), &lf))
4587b026
GV
6039 continue;
6040
6041 BLOCK_INPUT;
33d52f9c 6042 thisinfo.bdf = NULL;
4587b026
GV
6043 thisinfo.hfont = CreateFontIndirect (&lf);
6044 if (thisinfo.hfont == NULL)
6045 continue;
6046
6047 hdc = GetDC (dpyinfo->root_window);
6048 oldobj = SelectObject (hdc, thisinfo.hfont);
6049 if (GetTextMetrics (hdc, &thisinfo.tm))
f3fbd155 6050 XSETCDR (tem, make_number (FONT_WIDTH (&thisinfo)));
4587b026 6051 else
f3fbd155 6052 XSETCDR (tem, make_number (0));
4587b026
GV
6053 SelectObject (hdc, oldobj);
6054 ReleaseDC (dpyinfo->root_window, hdc);
6055 DeleteObject(thisinfo.hfont);
6056 UNBLOCK_INPUT;
6057 }
8e713be6 6058 found_size = XINT (XCDR (tem));
4587b026 6059 if (found_size == size)
5ca0cd71 6060 {
8e713be6 6061 newlist = Fcons (XCAR (tem), newlist);
5ca0cd71 6062 n_fonts++;
bd11cc09 6063 if (maxnames >= 0 && n_fonts >= maxnames)
5ca0cd71
GV
6064 break;
6065 }
4587b026
GV
6066 /* keep track of the closest matching size in case
6067 no exact match is found. */
6068 else if (found_size > 0)
6069 {
6070 if (NILP (second_best))
6071 second_best = tem;
7d0393cf 6072
4587b026
GV
6073 else if (found_size < size)
6074 {
8e713be6
KR
6075 if (XINT (XCDR (second_best)) > size
6076 || XINT (XCDR (second_best)) < found_size)
4587b026
GV
6077 second_best = tem;
6078 }
6079 else
6080 {
8e713be6
KR
6081 if (XINT (XCDR (second_best)) > size
6082 && XINT (XCDR (second_best)) >
4587b026
GV
6083 found_size)
6084 second_best = tem;
6085 }
6086 }
6087 }
6088
6089 if (!NILP (newlist))
6090 break;
6091 else if (!NILP (second_best))
6092 {
8e713be6 6093 newlist = Fcons (XCAR (second_best), Qnil);
4587b026
GV
6094 break;
6095 }
6096 }
6097
33d52f9c 6098 /* Include any bdf fonts. */
bd11cc09 6099 if (n_fonts < maxnames || maxnames < 0)
33d52f9c
GV
6100 {
6101 Lisp_Object combined[2];
5ca0cd71 6102 combined[0] = w32_list_bdf_fonts (pattern, maxnames - n_fonts);
33d52f9c
GV
6103 combined[1] = newlist;
6104 newlist = Fnconc(2, combined);
6105 }
6106
4587b026
GV
6107 return newlist;
6108}
6109
5ca0cd71 6110
4587b026
GV
6111/* Return a pointer to struct font_info of font FONT_IDX of frame F. */
6112struct font_info *
6113w32_get_font_info (f, font_idx)
6114 FRAME_PTR f;
6115 int font_idx;
6116{
6117 return (FRAME_W32_FONT_TABLE (f) + font_idx);
6118}
6119
6120
6121struct font_info*
6122w32_query_font (struct frame *f, char *fontname)
6123{
6124 int i;
6125 struct font_info *pfi;
6126
6127 pfi = FRAME_W32_FONT_TABLE (f);
6128
6129 for (i = 0; i < one_w32_display_info.n_fonts ;i++, pfi++)
6130 {
6131 if (strcmp(pfi->name, fontname) == 0) return pfi;
6132 }
6133
6134 return NULL;
6135}
6136
6137/* Find a CCL program for a font specified by FONTP, and set the member
6138 `encoder' of the structure. */
6139
6140void
6141w32_find_ccl_program (fontp)
6142 struct font_info *fontp;
6143{
3545439c 6144 Lisp_Object list, elt;
4587b026 6145
8e713be6 6146 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
4587b026 6147 {
8e713be6 6148 elt = XCAR (list);
4587b026 6149 if (CONSP (elt)
8e713be6
KR
6150 && STRINGP (XCAR (elt))
6151 && (fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
4587b026 6152 >= 0))
3545439c
KH
6153 break;
6154 }
6155 if (! NILP (list))
6156 {
17eedd00
KH
6157 struct ccl_program *ccl
6158 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
3545439c 6159
8e713be6 6160 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
3545439c
KH
6161 xfree (ccl);
6162 else
6163 fontp->font_encoder = ccl;
4587b026
GV
6164 }
6165}
6166
2ba49441
JR
6167/* directory-files from dired.c. */
6168Lisp_Object Fdirectory_files P_((Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object));
6169
4587b026 6170\f
8edb0a6f
JR
6171/* Find BDF files in a specified directory. (use GCPRO when calling,
6172 as this calls lisp to get a directory listing). */
6173static Lisp_Object
6174w32_find_bdf_fonts_in_dir (Lisp_Object directory)
6175{
6176 Lisp_Object filelist, list = Qnil;
6177 char fontname[100];
6178
6179 if (!STRINGP(directory))
6180 return Qnil;
6181
6182 filelist = Fdirectory_files (directory, Qt,
2ba49441 6183 build_string (".*\\.[bB][dD][fF]"), Qt);
8edb0a6f
JR
6184
6185 for ( ; CONSP(filelist); filelist = XCDR (filelist))
6186 {
6187 Lisp_Object filename = XCAR (filelist);
d5db4077 6188 if (w32_BDF_to_x_font (SDATA (filename), fontname, 100))
8edb0a6f
JR
6189 store_in_alist (&list, build_string (fontname), filename);
6190 }
6191 return list;
6192}
6193
6fc2811b
JR
6194DEFUN ("w32-find-bdf-fonts", Fw32_find_bdf_fonts, Sw32_find_bdf_fonts,
6195 1, 1, 0,
52deb19f
JB
6196 doc: /* Return a list of BDF fonts in DIRECTORY.
6197The list is suitable for appending to `w32-bdf-filename-alist'.
6198Fonts which do not contain an xlfd description will not be included
6199in the list. DIRECTORY may be a list of directories. */)
6fc2811b
JR
6200 (directory)
6201 Lisp_Object directory;
6202{
6203 Lisp_Object list = Qnil;
6204 struct gcpro gcpro1, gcpro2;
ee78dc32 6205
6fc2811b
JR
6206 if (!CONSP (directory))
6207 return w32_find_bdf_fonts_in_dir (directory);
ee78dc32 6208
6fc2811b 6209 for ( ; CONSP (directory); directory = XCDR (directory))
ee78dc32 6210 {
6fc2811b
JR
6211 Lisp_Object pair[2];
6212 pair[0] = list;
6213 pair[1] = Qnil;
6214 GCPRO2 (directory, list);
6215 pair[1] = w32_find_bdf_fonts_in_dir( XCAR (directory) );
6216 list = Fnconc( 2, pair );
6217 UNGCPRO;
6218 }
6219 return list;
6220}
ee78dc32 6221
6fc2811b
JR
6222\f
6223DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
74e1aeec 6224 doc: /* Internal function called by `color-defined-p', which see. */)
6fc2811b
JR
6225 (color, frame)
6226 Lisp_Object color, frame;
6227{
6228 XColor foo;
6229 FRAME_PTR f = check_x_frame (frame);
ee78dc32 6230
b7826503 6231 CHECK_STRING (color);
ee78dc32 6232
d5db4077 6233 if (w32_defined_color (f, SDATA (color), &foo, 0))
6fc2811b
JR
6234 return Qt;
6235 else
6236 return Qnil;
6237}
ee78dc32 6238
2d764c78 6239DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
74e1aeec 6240 doc: /* Internal function called by `color-values', which see. */)
ee78dc32
GV
6241 (color, frame)
6242 Lisp_Object color, frame;
6243{
6fc2811b 6244 XColor foo;
ee78dc32
GV
6245 FRAME_PTR f = check_x_frame (frame);
6246
b7826503 6247 CHECK_STRING (color);
ee78dc32 6248
d5db4077 6249 if (w32_defined_color (f, SDATA (color), &foo, 0))
ee78dc32
GV
6250 {
6251 Lisp_Object rgb[3];
6252
6fc2811b
JR
6253 rgb[0] = make_number ((GetRValue (foo.pixel) << 8)
6254 | GetRValue (foo.pixel));
6255 rgb[1] = make_number ((GetGValue (foo.pixel) << 8)
6256 | GetGValue (foo.pixel));
6257 rgb[2] = make_number ((GetBValue (foo.pixel) << 8)
6258 | GetBValue (foo.pixel));
ee78dc32
GV
6259 return Flist (3, rgb);
6260 }
6261 else
6262 return Qnil;
6263}
6264
2d764c78 6265DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
74e1aeec 6266 doc: /* Internal function called by `display-color-p', which see. */)
ee78dc32
GV
6267 (display)
6268 Lisp_Object display;
6269{
fbd6baed 6270 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
6271
6272 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 2)
6273 return Qnil;
6274
6275 return Qt;
6276}
6277
74e1aeec
JR
6278DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p,
6279 Sx_display_grayscale_p, 0, 1, 0,
52deb19f 6280 doc: /* Return t if DISPLAY supports shades of gray.
74e1aeec
JR
6281Note that color displays do support shades of gray.
6282The optional argument DISPLAY specifies which display to ask about.
6283DISPLAY should be either a frame or a display name (a string).
6284If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
6285 (display)
6286 Lisp_Object display;
6287{
fbd6baed 6288 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
6289
6290 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 1)
6291 return Qnil;
6292
6293 return Qt;
6294}
6295
74e1aeec
JR
6296DEFUN ("x-display-pixel-width", Fx_display_pixel_width,
6297 Sx_display_pixel_width, 0, 1, 0,
6298 doc: /* Returns the width in pixels of DISPLAY.
6299The optional argument DISPLAY specifies which display to ask about.
6300DISPLAY should be either a frame or a display name (a string).
6301If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
6302 (display)
6303 Lisp_Object display;
6304{
fbd6baed 6305 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
6306
6307 return make_number (dpyinfo->width);
6308}
6309
6310DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
74e1aeec
JR
6311 Sx_display_pixel_height, 0, 1, 0,
6312 doc: /* Returns the height in pixels of DISPLAY.
6313The optional argument DISPLAY specifies which display to ask about.
6314DISPLAY should be either a frame or a display name (a string).
6315If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
6316 (display)
6317 Lisp_Object display;
6318{
fbd6baed 6319 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
6320
6321 return make_number (dpyinfo->height);
6322}
6323
6324DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
74e1aeec
JR
6325 0, 1, 0,
6326 doc: /* Returns the number of bitplanes of DISPLAY.
6327The optional argument DISPLAY specifies which display to ask about.
6328DISPLAY should be either a frame or a display name (a string).
6329If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
6330 (display)
6331 Lisp_Object display;
6332{
fbd6baed 6333 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
6334
6335 return make_number (dpyinfo->n_planes * dpyinfo->n_cbits);
6336}
6337
6338DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
74e1aeec
JR
6339 0, 1, 0,
6340 doc: /* Returns the number of color cells of DISPLAY.
6341The optional argument DISPLAY specifies which display to ask about.
6342DISPLAY should be either a frame or a display name (a string).
6343If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
6344 (display)
6345 Lisp_Object display;
6346{
fbd6baed 6347 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
6348 HDC hdc;
6349 int cap;
6350
5ac45f98
GV
6351 hdc = GetDC (dpyinfo->root_window);
6352 if (dpyinfo->has_palette)
52deb19f 6353 cap = GetDeviceCaps (hdc, SIZEPALETTE);
5ac45f98 6354 else
52deb19f 6355 cap = GetDeviceCaps (hdc, NUMCOLORS);
abf8c61b 6356
007776bc
JB
6357 /* We force 24+ bit depths to 24-bit, both to prevent an overflow
6358 and because probably is more meaningful on Windows anyway */
abf8c61b 6359 if (cap < 0)
007776bc 6360 cap = 1 << min(dpyinfo->n_planes * dpyinfo->n_cbits, 24);
7d0393cf 6361
ee78dc32 6362 ReleaseDC (dpyinfo->root_window, hdc);
7d0393cf 6363
ee78dc32
GV
6364 return make_number (cap);
6365}
6366
6367DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
6368 Sx_server_max_request_size,
74e1aeec
JR
6369 0, 1, 0,
6370 doc: /* Returns the maximum request size of the server of DISPLAY.
6371The optional argument DISPLAY specifies which display to ask about.
6372DISPLAY should be either a frame or a display name (a string).
6373If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
6374 (display)
6375 Lisp_Object display;
6376{
fbd6baed 6377 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
6378
6379 return make_number (1);
6380}
6381
6382DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
74e1aeec
JR
6383 doc: /* Returns the vendor ID string of the W32 system (Microsoft).
6384The optional argument DISPLAY specifies which display to ask about.
6385DISPLAY should be either a frame or a display name (a string).
6386If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
6387 (display)
6388 Lisp_Object display;
6389{
dfff8a69 6390 return build_string ("Microsoft Corp.");
ee78dc32
GV
6391}
6392
6393DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
74e1aeec
JR
6394 doc: /* Returns the version numbers of the server of DISPLAY.
6395The value is a list of three integers: the major and minor
6396version numbers, and the vendor-specific release
6397number. See also the function `x-server-vendor'.
6398
6399The optional argument DISPLAY specifies which display to ask about.
6400DISPLAY should be either a frame or a display name (a string).
6401If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
6402 (display)
6403 Lisp_Object display;
6404{
fbd6baed 6405 return Fcons (make_number (w32_major_version),
58e0f0e4
AI
6406 Fcons (make_number (w32_minor_version),
6407 Fcons (make_number (w32_build_number), Qnil)));
ee78dc32
GV
6408}
6409
6410DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
74e1aeec
JR
6411 doc: /* Returns the number of screens on the server 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{
ee78dc32
GV
6418 return make_number (1);
6419}
6420
74e1aeec
JR
6421DEFUN ("x-display-mm-height", Fx_display_mm_height,
6422 Sx_display_mm_height, 0, 1, 0,
6423 doc: /* Returns the height in millimeters of DISPLAY.
6424The optional argument DISPLAY specifies which display to ask about.
6425DISPLAY should be either a frame or a display name (a string).
6426If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
6427 (display)
6428 Lisp_Object display;
6429{
fbd6baed 6430 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
6431 HDC hdc;
6432 int cap;
6433
5ac45f98 6434 hdc = GetDC (dpyinfo->root_window);
7d0393cf 6435
ee78dc32 6436 cap = GetDeviceCaps (hdc, VERTSIZE);
7d0393cf 6437
ee78dc32 6438 ReleaseDC (dpyinfo->root_window, hdc);
7d0393cf 6439
ee78dc32
GV
6440 return make_number (cap);
6441}
6442
6443DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
74e1aeec
JR
6444 doc: /* Returns the width in millimeters of DISPLAY.
6445The optional argument DISPLAY specifies which display to ask about.
6446DISPLAY should be either a frame or a display name (a string).
6447If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
6448 (display)
6449 Lisp_Object display;
6450{
fbd6baed 6451 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
6452
6453 HDC hdc;
6454 int cap;
6455
5ac45f98 6456 hdc = GetDC (dpyinfo->root_window);
7d0393cf 6457
ee78dc32 6458 cap = GetDeviceCaps (hdc, HORZSIZE);
7d0393cf 6459
ee78dc32 6460 ReleaseDC (dpyinfo->root_window, hdc);
7d0393cf 6461
ee78dc32
GV
6462 return make_number (cap);
6463}
6464
6465DEFUN ("x-display-backing-store", Fx_display_backing_store,
74e1aeec
JR
6466 Sx_display_backing_store, 0, 1, 0,
6467 doc: /* Returns an indication of whether DISPLAY does backing store.
6468The value may be `always', `when-mapped', or `not-useful'.
6469The optional argument DISPLAY specifies which display to ask about.
6470DISPLAY should be either a frame or a display name (a string).
6471If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
6472 (display)
6473 Lisp_Object display;
6474{
6475 return intern ("not-useful");
6476}
6477
6478DEFUN ("x-display-visual-class", Fx_display_visual_class,
74e1aeec
JR
6479 Sx_display_visual_class, 0, 1, 0,
6480 doc: /* Returns the visual class of DISPLAY.
6481The value is one of the symbols `static-gray', `gray-scale',
6482`static-color', `pseudo-color', `true-color', or `direct-color'.
6483
6484The optional argument DISPLAY specifies which display to ask about.
6485DISPLAY should be either a frame or a display name (a string).
6486If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
6487 (display)
6488 Lisp_Object display;
6489{
fbd6baed 6490 struct w32_display_info *dpyinfo = check_x_display_info (display);
abf8c61b 6491 Lisp_Object result = Qnil;
ee78dc32 6492
abf8c61b
AI
6493 if (dpyinfo->has_palette)
6494 result = intern ("pseudo-color");
6495 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 1)
6496 result = intern ("static-grey");
6497 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 4)
6498 result = intern ("static-color");
6499 else if (dpyinfo->n_planes * dpyinfo->n_cbits > 8)
6500 result = intern ("true-color");
ee78dc32 6501
abf8c61b 6502 return result;
ee78dc32
GV
6503}
6504
6505DEFUN ("x-display-save-under", Fx_display_save_under,
74e1aeec
JR
6506 Sx_display_save_under, 0, 1, 0,
6507 doc: /* Returns t if DISPLAY supports the save-under feature.
6508The optional argument DISPLAY specifies which display to ask about.
6509DISPLAY should be either a frame or a display name (a string).
6510If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
6511 (display)
6512 Lisp_Object display;
6513{
6fc2811b
JR
6514 return Qnil;
6515}
6516\f
6517int
6518x_pixel_width (f)
6519 register struct frame *f;
6520{
be786000 6521 return FRAME_PIXEL_WIDTH (f);
6fc2811b
JR
6522}
6523
6524int
6525x_pixel_height (f)
6526 register struct frame *f;
6527{
be786000 6528 return FRAME_PIXEL_HEIGHT (f);
6fc2811b
JR
6529}
6530
6531int
6532x_char_width (f)
6533 register struct frame *f;
6534{
be786000 6535 return FRAME_COLUMN_WIDTH (f);
6fc2811b
JR
6536}
6537
6538int
6539x_char_height (f)
6540 register struct frame *f;
6541{
be786000 6542 return FRAME_LINE_HEIGHT (f);
6fc2811b
JR
6543}
6544
6545int
6546x_screen_planes (f)
6547 register struct frame *f;
6548{
6549 return FRAME_W32_DISPLAY_INFO (f)->n_planes;
6550}
6551\f
6552/* Return the display structure for the display named NAME.
6553 Open a new connection if necessary. */
6554
6555struct w32_display_info *
6556x_display_info_for_name (name)
6557 Lisp_Object name;
6558{
6559 Lisp_Object names;
6560 struct w32_display_info *dpyinfo;
6561
b7826503 6562 CHECK_STRING (name);
6fc2811b
JR
6563
6564 for (dpyinfo = &one_w32_display_info, names = w32_display_name_list;
6565 dpyinfo;
6566 dpyinfo = dpyinfo->next, names = XCDR (names))
6567 {
6568 Lisp_Object tem;
6569 tem = Fstring_equal (XCAR (XCAR (names)), name);
6570 if (!NILP (tem))
6571 return dpyinfo;
6572 }
6573
6574 /* Use this general default value to start with. */
6575 Vx_resource_name = Vinvocation_name;
6576
6577 validate_x_resource_name ();
6578
6579 dpyinfo = w32_term_init (name, (unsigned char *)0,
d5db4077 6580 (char *) SDATA (Vx_resource_name));
6fc2811b
JR
6581
6582 if (dpyinfo == 0)
d5db4077 6583 error ("Cannot connect to server %s", SDATA (name));
6fc2811b
JR
6584
6585 w32_in_use = 1;
6586 XSETFASTINT (Vwindow_system_version, 3);
6587
6588 return dpyinfo;
6589}
6590
6591DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
74e1aeec
JR
6592 1, 3, 0, doc: /* Open a connection to a server.
6593DISPLAY is the name of the display to connect to.
6594Optional second arg XRM-STRING is a string of resources in xrdb format.
6595If the optional third arg MUST-SUCCEED is non-nil,
6596terminate Emacs if we can't open the connection. */)
6fc2811b
JR
6597 (display, xrm_string, must_succeed)
6598 Lisp_Object display, xrm_string, must_succeed;
6599{
6600 unsigned char *xrm_option;
6601 struct w32_display_info *dpyinfo;
6602
74e1aeec
JR
6603 /* If initialization has already been done, return now to avoid
6604 overwriting critical parts of one_w32_display_info. */
6605 if (w32_in_use)
6606 return Qnil;
6607
b7826503 6608 CHECK_STRING (display);
6fc2811b 6609 if (! NILP (xrm_string))
b7826503 6610 CHECK_STRING (xrm_string);
6fc2811b
JR
6611
6612 if (! EQ (Vwindow_system, intern ("w32")))
6613 error ("Not using Microsoft Windows");
6614
6615 /* Allow color mapping to be defined externally; first look in user's
6616 HOME directory, then in Emacs etc dir for a file called rgb.txt. */
6617 {
6618 Lisp_Object color_file;
6619 struct gcpro gcpro1;
6620
6621 color_file = build_string("~/rgb.txt");
6622
6623 GCPRO1 (color_file);
6624
6625 if (NILP (Ffile_readable_p (color_file)))
6626 color_file =
6627 Fexpand_file_name (build_string ("rgb.txt"),
6628 Fsymbol_value (intern ("data-directory")));
6629
6630 Vw32_color_map = Fw32_load_color_file (color_file);
6631
6632 UNGCPRO;
6633 }
6634 if (NILP (Vw32_color_map))
6635 Vw32_color_map = Fw32_default_color_map ();
6636
5a8a15ec
JR
6637 /* Merge in system logical colors. */
6638 add_system_logical_colors_to_map (&Vw32_color_map);
6639
6fc2811b 6640 if (! NILP (xrm_string))
d5db4077 6641 xrm_option = (unsigned char *) SDATA (xrm_string);
6fc2811b
JR
6642 else
6643 xrm_option = (unsigned char *) 0;
6644
6645 /* Use this general default value to start with. */
6646 /* First remove .exe suffix from invocation-name - it looks ugly. */
6647 {
6648 char basename[ MAX_PATH ], *str;
6649
d5db4077 6650 strcpy (basename, SDATA (Vinvocation_name));
6fc2811b
JR
6651 str = strrchr (basename, '.');
6652 if (str) *str = 0;
6653 Vinvocation_name = build_string (basename);
6654 }
6655 Vx_resource_name = Vinvocation_name;
6656
6657 validate_x_resource_name ();
6658
6659 /* This is what opens the connection and sets x_current_display.
6660 This also initializes many symbols, such as those used for input. */
6661 dpyinfo = w32_term_init (display, xrm_option,
d5db4077 6662 (char *) SDATA (Vx_resource_name));
6fc2811b
JR
6663
6664 if (dpyinfo == 0)
6665 {
6666 if (!NILP (must_succeed))
6667 fatal ("Cannot connect to server %s.\n",
d5db4077 6668 SDATA (display));
6fc2811b 6669 else
d5db4077 6670 error ("Cannot connect to server %s", SDATA (display));
6fc2811b
JR
6671 }
6672
6673 w32_in_use = 1;
6674
6675 XSETFASTINT (Vwindow_system_version, 3);
6676 return Qnil;
6677}
6678
6679DEFUN ("x-close-connection", Fx_close_connection,
6680 Sx_close_connection, 1, 1, 0,
74e1aeec
JR
6681 doc: /* Close the connection to DISPLAY's server.
6682For DISPLAY, specify either a frame or a display name (a string).
6683If DISPLAY is nil, that stands for the selected frame's display. */)
6fc2811b
JR
6684 (display)
6685 Lisp_Object display;
6686{
6687 struct w32_display_info *dpyinfo = check_x_display_info (display);
6688 int i;
6689
6690 if (dpyinfo->reference_count > 0)
6691 error ("Display still has frames on it");
6692
6693 BLOCK_INPUT;
6694 /* Free the fonts in the font table. */
6695 for (i = 0; i < dpyinfo->n_fonts; i++)
6696 if (dpyinfo->font_table[i].name)
6697 {
126f2e35
JR
6698 if (dpyinfo->font_table[i].name != dpyinfo->font_table[i].full_name)
6699 xfree (dpyinfo->font_table[i].full_name);
6fc2811b 6700 xfree (dpyinfo->font_table[i].name);
6fc2811b
JR
6701 w32_unload_font (dpyinfo, dpyinfo->font_table[i].font);
6702 }
6703 x_destroy_all_bitmaps (dpyinfo);
6704
6705 x_delete_display (dpyinfo);
6706 UNBLOCK_INPUT;
6707
6708 return Qnil;
6709}
6710
6711DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
74e1aeec 6712 doc: /* Return the list of display names that Emacs has connections to. */)
6fc2811b
JR
6713 ()
6714{
6715 Lisp_Object tail, result;
6716
6717 result = Qnil;
6718 for (tail = w32_display_name_list; ! NILP (tail); tail = XCDR (tail))
6719 result = Fcons (XCAR (XCAR (tail)), result);
6720
6721 return result;
6722}
6723
6724DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
0a332240
PJ
6725 doc: /* This is a noop on W32 systems. */)
6726 (on, display)
6727 Lisp_Object display, on;
6fc2811b 6728{
6fc2811b
JR
6729 return Qnil;
6730}
6731
8eaae3d4 6732
6fc2811b 6733\f
6fc2811b 6734/***********************************************************************
8eaae3d4 6735 Window properties
6fc2811b
JR
6736 ***********************************************************************/
6737
8eaae3d4
KS
6738DEFUN ("x-change-window-property", Fx_change_window_property,
6739 Sx_change_window_property, 2, 6, 0,
6740 doc: /* Change window property PROP to VALUE on the X window of FRAME.
6741VALUE may be a string or a list of conses, numbers and/or strings.
6742If an element in the list is a string, it is converted to
6743an Atom and the value of the Atom is used. If an element is a cons,
6744it is converted to a 32 bit number where the car is the 16 top bits and the
6745cdr is the lower 16 bits.
6746FRAME nil or omitted means use the selected frame.
6747If TYPE is given and non-nil, it is the name of the type of VALUE.
6748If TYPE is not given or nil, the type is STRING.
6749FORMAT gives the size in bits of each element if VALUE is a list.
6750It must be one of 8, 16 or 32.
6751If VALUE is a string or FORMAT is nil or not given, FORMAT defaults to 8.
6752If OUTER_P is non-nil, the property is changed for the outer X window of
6753FRAME. Default is to change on the edit X window.
6fc2811b 6754
8eaae3d4
KS
6755Value is VALUE. */)
6756 (prop, value, frame, type, format, outer_p)
6757 Lisp_Object prop, value, frame, type, format, outer_p;
6758{
6759#if 0 /* TODO : port window properties to W32 */
6760 struct frame *f = check_x_frame (frame);
6761 Atom prop_atom;
6fc2811b 6762
8eaae3d4
KS
6763 CHECK_STRING (prop);
6764 CHECK_STRING (value);
6fc2811b 6765
8eaae3d4
KS
6766 BLOCK_INPUT;
6767 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
6768 XChangeProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
6769 prop_atom, XA_STRING, 8, PropModeReplace,
6770 SDATA (value), SCHARS (value));
6fc2811b 6771
8eaae3d4
KS
6772 /* Make sure the property is set when we return. */
6773 XFlush (FRAME_W32_DISPLAY (f));
6774 UNBLOCK_INPUT;
6fc2811b 6775
8eaae3d4 6776#endif /* TODO */
6fc2811b 6777
8eaae3d4
KS
6778 return value;
6779}
dfff8a69 6780
6fc2811b 6781
8eaae3d4
KS
6782DEFUN ("x-delete-window-property", Fx_delete_window_property,
6783 Sx_delete_window_property, 1, 2, 0,
6784 doc: /* Remove window property PROP from X window of FRAME.
6785FRAME nil or omitted means use the selected frame. Value is PROP. */)
6786 (prop, frame)
6787 Lisp_Object prop, frame;
6fc2811b 6788{
8eaae3d4 6789#if 0 /* TODO : port window properties to W32 */
6fc2811b 6790
8eaae3d4
KS
6791 struct frame *f = check_x_frame (frame);
6792 Atom prop_atom;
6fc2811b 6793
8eaae3d4
KS
6794 CHECK_STRING (prop);
6795 BLOCK_INPUT;
6796 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
6797 XDeleteProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), prop_atom);
6fc2811b 6798
8eaae3d4
KS
6799 /* Make sure the property is removed when we return. */
6800 XFlush (FRAME_W32_DISPLAY (f));
6801 UNBLOCK_INPUT;
6802#endif /* TODO */
6fc2811b 6803
8eaae3d4 6804 return prop;
6fc2811b
JR
6805}
6806
6807
8eaae3d4
KS
6808DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
6809 1, 2, 0,
6810 doc: /* Value is the value of window property PROP on FRAME.
6811If FRAME is nil or omitted, use the selected frame. Value is nil
6812if FRAME hasn't a property with name PROP or if PROP has no string
6813value. */)
6814 (prop, frame)
6815 Lisp_Object prop, frame;
6fc2811b 6816{
8eaae3d4
KS
6817#if 0 /* TODO : port window properties to W32 */
6818
6819 struct frame *f = check_x_frame (frame);
6820 Atom prop_atom;
6821 int rc;
6822 Lisp_Object prop_value = Qnil;
6823 char *tmp_data = NULL;
6824 Atom actual_type;
6825 int actual_format;
6826 unsigned long actual_size, bytes_remaining;
7d0393cf 6827
8eaae3d4
KS
6828 CHECK_STRING (prop);
6829 BLOCK_INPUT;
6830 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
6831 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
6832 prop_atom, 0, 0, False, XA_STRING,
6833 &actual_type, &actual_format, &actual_size,
6834 &bytes_remaining, (unsigned char **) &tmp_data);
6835 if (rc == Success)
6fc2811b 6836 {
8eaae3d4 6837 int size = bytes_remaining;
3cf3436e 6838
8eaae3d4
KS
6839 XFree (tmp_data);
6840 tmp_data = NULL;
3cf3436e 6841
8eaae3d4
KS
6842 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
6843 prop_atom, 0, bytes_remaining,
6844 False, XA_STRING,
6845 &actual_type, &actual_format,
6846 &actual_size, &bytes_remaining,
6847 (unsigned char **) &tmp_data);
6848 if (rc == Success)
6849 prop_value = make_string (tmp_data, size);
6fc2811b 6850
8eaae3d4
KS
6851 XFree (tmp_data);
6852 }
6fc2811b 6853
8eaae3d4 6854 UNBLOCK_INPUT;
6fc2811b 6855
8eaae3d4 6856 return prop_value;
6fc2811b 6857
8eaae3d4
KS
6858#endif /* TODO */
6859 return Qnil;
6fc2811b
JR
6860}
6861
6862
6863\f
6864/***********************************************************************
8eaae3d4 6865 Busy cursor
6fc2811b
JR
6866 ***********************************************************************/
6867
8eaae3d4
KS
6868/* If non-null, an asynchronous timer that, when it expires, displays
6869 an hourglass cursor on all frames. */
6fc2811b 6870
8eaae3d4 6871static struct atimer *hourglass_atimer;
6fc2811b 6872
8eaae3d4 6873/* Non-zero means an hourglass cursor is currently shown. */
6fc2811b 6874
8eaae3d4 6875static int hourglass_shown_p;
6fc2811b 6876
8eaae3d4 6877/* Number of seconds to wait before displaying an hourglass cursor. */
6fc2811b 6878
8eaae3d4 6879static Lisp_Object Vhourglass_delay;
6fc2811b 6880
8eaae3d4
KS
6881/* Default number of seconds to wait before displaying an hourglass
6882 cursor. */
6fc2811b 6883
8eaae3d4 6884#define DEFAULT_HOURGLASS_DELAY 1
6fc2811b 6885
8eaae3d4 6886/* Function prototypes. */
6fc2811b 6887
8eaae3d4
KS
6888static void show_hourglass P_ ((struct atimer *));
6889static void hide_hourglass P_ ((void));
6fc2811b 6890
6fc2811b 6891
8eaae3d4
KS
6892/* Cancel a currently active hourglass timer, and start a new one. */
6893
6894void
6895start_hourglass ()
6fc2811b 6896{
8eaae3d4
KS
6897#if 0 /* TODO: cursor shape changes. */
6898 EMACS_TIME delay;
6899 int secs, usecs = 0;
6fc2811b 6900
8eaae3d4 6901 cancel_hourglass ();
f79e6790 6902
0af913d7
GM
6903 if (INTEGERP (Vhourglass_delay)
6904 && XINT (Vhourglass_delay) > 0)
6905 secs = XFASTINT (Vhourglass_delay);
6906 else if (FLOATP (Vhourglass_delay)
6907 && XFLOAT_DATA (Vhourglass_delay) > 0)
dfff8a69
JR
6908 {
6909 Lisp_Object tem;
0af913d7 6910 tem = Ftruncate (Vhourglass_delay, Qnil);
dfff8a69 6911 secs = XFASTINT (tem);
0af913d7 6912 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
dfff8a69 6913 }
f79e6790 6914 else
0af913d7 6915 secs = DEFAULT_HOURGLASS_DELAY;
7d0393cf 6916
dfff8a69 6917 EMACS_SET_SECS_USECS (delay, secs, usecs);
0af913d7
GM
6918 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
6919 show_hourglass, NULL);
f79e6790
JR
6920#endif
6921}
6922
6923
0af913d7
GM
6924/* Cancel the hourglass cursor timer if active, hide an hourglass
6925 cursor if shown. */
f79e6790
JR
6926
6927void
0af913d7 6928cancel_hourglass ()
f79e6790 6929{
0af913d7 6930 if (hourglass_atimer)
dfff8a69 6931 {
0af913d7
GM
6932 cancel_atimer (hourglass_atimer);
6933 hourglass_atimer = NULL;
dfff8a69 6934 }
7d0393cf 6935
0af913d7
GM
6936 if (hourglass_shown_p)
6937 hide_hourglass ();
f79e6790
JR
6938}
6939
6940
0af913d7
GM
6941/* Timer function of hourglass_atimer. TIMER is equal to
6942 hourglass_atimer.
f79e6790 6943
0af913d7
GM
6944 Display an hourglass cursor on all frames by mapping the frames'
6945 hourglass_window. Set the hourglass_p flag in the frames'
6946 output_data.x structure to indicate that an hourglass cursor is
6947 shown on the frames. */
f79e6790
JR
6948
6949static void
0af913d7 6950show_hourglass (timer)
f79e6790 6951 struct atimer *timer;
6fc2811b 6952{
767b1ff0 6953#if 0 /* TODO: cursor shape changes. */
f79e6790 6954 /* The timer implementation will cancel this timer automatically
0af913d7 6955 after this function has run. Set hourglass_atimer to null
f79e6790 6956 so that we know the timer doesn't have to be canceled. */
0af913d7 6957 hourglass_atimer = NULL;
f79e6790 6958
0af913d7 6959 if (!hourglass_shown_p)
6fc2811b
JR
6960 {
6961 Lisp_Object rest, frame;
7d0393cf 6962
f79e6790 6963 BLOCK_INPUT;
7d0393cf 6964
6fc2811b 6965 FOR_EACH_FRAME (rest, frame)
dc220243 6966 if (FRAME_W32_P (XFRAME (frame)))
6fc2811b
JR
6967 {
6968 struct frame *f = XFRAME (frame);
7d0393cf 6969
0af913d7 6970 f->output_data.w32->hourglass_p = 1;
7d0393cf 6971
0af913d7 6972 if (!f->output_data.w32->hourglass_window)
6fc2811b
JR
6973 {
6974 unsigned long mask = CWCursor;
6975 XSetWindowAttributes attrs;
7d0393cf 6976
0af913d7 6977 attrs.cursor = f->output_data.w32->hourglass_cursor;
7d0393cf 6978
0af913d7 6979 f->output_data.w32->hourglass_window
f79e6790 6980 = XCreateWindow (FRAME_X_DISPLAY (f),
6fc2811b
JR
6981 FRAME_OUTER_WINDOW (f),
6982 0, 0, 32000, 32000, 0, 0,
f79e6790
JR
6983 InputOnly,
6984 CopyFromParent,
6fc2811b
JR
6985 mask, &attrs);
6986 }
7d0393cf 6987
0af913d7
GM
6988 XMapRaised (FRAME_X_DISPLAY (f),
6989 f->output_data.w32->hourglass_window);
f79e6790 6990 XFlush (FRAME_X_DISPLAY (f));
6fc2811b 6991 }
6fc2811b 6992
0af913d7 6993 hourglass_shown_p = 1;
f79e6790
JR
6994 UNBLOCK_INPUT;
6995 }
6996#endif
6fc2811b
JR
6997}
6998
6999
0af913d7 7000/* Hide the hourglass cursor on all frames, if it is currently shown. */
6fc2811b 7001
f79e6790 7002static void
0af913d7 7003hide_hourglass ()
f79e6790 7004{
767b1ff0 7005#if 0 /* TODO: cursor shape changes. */
0af913d7 7006 if (hourglass_shown_p)
6fc2811b 7007 {
f79e6790
JR
7008 Lisp_Object rest, frame;
7009
7010 BLOCK_INPUT;
7011 FOR_EACH_FRAME (rest, frame)
6fc2811b 7012 {
f79e6790 7013 struct frame *f = XFRAME (frame);
7d0393cf 7014
dc220243 7015 if (FRAME_W32_P (f)
f79e6790 7016 /* Watch out for newly created frames. */
0af913d7 7017 && f->output_data.x->hourglass_window)
f79e6790 7018 {
0af913d7
GM
7019 XUnmapWindow (FRAME_X_DISPLAY (f),
7020 f->output_data.x->hourglass_window);
7021 /* Sync here because XTread_socket looks at the
7022 hourglass_p flag that is reset to zero below. */
f79e6790 7023 XSync (FRAME_X_DISPLAY (f), False);
0af913d7 7024 f->output_data.x->hourglass_p = 0;
f79e6790 7025 }
6fc2811b 7026 }
6fc2811b 7027
0af913d7 7028 hourglass_shown_p = 0;
f79e6790
JR
7029 UNBLOCK_INPUT;
7030 }
7031#endif
6fc2811b
JR
7032}
7033
7034
7035\f
7036/***********************************************************************
7037 Tool tips
7038 ***********************************************************************/
7039
7040static Lisp_Object x_create_tip_frame P_ ((struct w32_display_info *,
3cf3436e
JR
7041 Lisp_Object, Lisp_Object));
7042static void compute_tip_xy P_ ((struct frame *, Lisp_Object, Lisp_Object,
7043 Lisp_Object, int, int, int *, int *));
7d0393cf 7044
3cf3436e 7045/* The frame of a currently visible tooltip. */
6fc2811b 7046
937e601e 7047Lisp_Object tip_frame;
6fc2811b
JR
7048
7049/* If non-nil, a timer started that hides the last tooltip when it
7050 fires. */
7051
7052Lisp_Object tip_timer;
7053Window tip_window;
7054
3cf3436e
JR
7055/* If non-nil, a vector of 3 elements containing the last args
7056 with which x-show-tip was called. See there. */
7057
7058Lisp_Object last_show_tip_args;
7059
7060/* Maximum size for tooltips; a cons (COLUMNS . ROWS). */
7061
7062Lisp_Object Vx_max_tooltip_size;
7063
7064
937e601e
AI
7065static Lisp_Object
7066unwind_create_tip_frame (frame)
7067 Lisp_Object frame;
7068{
c844a81a
GM
7069 Lisp_Object deleted;
7070
7071 deleted = unwind_create_frame (frame);
7072 if (EQ (deleted, Qt))
7073 {
7074 tip_window = NULL;
7075 tip_frame = Qnil;
7076 }
7d0393cf 7077
c844a81a 7078 return deleted;
937e601e
AI
7079}
7080
7081
6fc2811b 7082/* Create a frame for a tooltip on the display described by DPYINFO.
3cf3436e
JR
7083 PARMS is a list of frame parameters. TEXT is the string to
7084 display in the tip frame. Value is the frame.
937e601e
AI
7085
7086 Note that functions called here, esp. x_default_parameter can
7087 signal errors, for instance when a specified color name is
7088 undefined. We have to make sure that we're in a consistent state
7089 when this happens. */
6fc2811b
JR
7090
7091static Lisp_Object
3cf3436e 7092x_create_tip_frame (dpyinfo, parms, text)
6fc2811b 7093 struct w32_display_info *dpyinfo;
3cf3436e 7094 Lisp_Object parms, text;
6fc2811b 7095{
6fc2811b
JR
7096 struct frame *f;
7097 Lisp_Object frame, tem;
7098 Lisp_Object name;
7099 long window_prompting = 0;
7100 int width, height;
331379bf 7101 int count = SPECPDL_INDEX ();
6fc2811b
JR
7102 struct gcpro gcpro1, gcpro2, gcpro3;
7103 struct kboard *kb;
3cf3436e
JR
7104 int face_change_count_before = face_change_count;
7105 Lisp_Object buffer;
7106 struct buffer *old_buffer;
6fc2811b 7107
ca56d953 7108 check_w32 ();
6fc2811b
JR
7109
7110 /* Use this general default value to start with until we know if
7111 this frame has a specified name. */
7112 Vx_resource_name = Vinvocation_name;
7113
7114#ifdef MULTI_KBOARD
7115 kb = dpyinfo->kboard;
7116#else
7117 kb = &the_only_kboard;
7118#endif
7119
7120 /* Get the name of the frame to use for resource lookup. */
7121 name = w32_get_arg (parms, Qname, "name", "Name", RES_TYPE_STRING);
7122 if (!STRINGP (name)
7123 && !EQ (name, Qunbound)
7124 && !NILP (name))
7125 error ("Invalid frame name--not a string or nil");
7126 Vx_resource_name = name;
7127
7128 frame = Qnil;
7129 GCPRO3 (parms, name, frame);
9eb16b62
JR
7130 /* Make a frame without minibuffer nor mode-line. */
7131 f = make_frame (0);
7132 f->wants_modeline = 0;
6fc2811b 7133 XSETFRAME (frame, f);
3cf3436e
JR
7134
7135 buffer = Fget_buffer_create (build_string (" *tip*"));
be786000 7136 Fset_window_buffer (FRAME_ROOT_WINDOW (f), buffer, Qnil);
3cf3436e
JR
7137 old_buffer = current_buffer;
7138 set_buffer_internal_1 (XBUFFER (buffer));
7139 current_buffer->truncate_lines = Qnil;
5c2a995d
KH
7140 specbind (Qinhibit_read_only, Qt);
7141 specbind (Qinhibit_modification_hooks, Qt);
3cf3436e
JR
7142 Ferase_buffer ();
7143 Finsert (1, &text);
7144 set_buffer_internal_1 (old_buffer);
7d0393cf 7145
6fc2811b 7146 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
937e601e 7147 record_unwind_protect (unwind_create_tip_frame, frame);
6fc2811b 7148
3cf3436e
JR
7149 /* By setting the output method, we're essentially saying that
7150 the frame is live, as per FRAME_LIVE_P. If we get a signal
7151 from this point on, x_destroy_window might screw up reference
7152 counts etc. */
d88c567c 7153 f->output_method = output_w32;
6fc2811b
JR
7154 f->output_data.w32 =
7155 (struct w32_output *) xmalloc (sizeof (struct w32_output));
7156 bzero (f->output_data.w32, sizeof (struct w32_output));
ca56d953
JR
7157
7158 FRAME_FONTSET (f) = -1;
6fc2811b
JR
7159 f->icon_name = Qnil;
7160
ca56d953 7161#if 0 /* GLYPH_DEBUG TODO: image support. */
937e601e
AI
7162 image_cache_refcount = FRAME_X_IMAGE_CACHE (f)->refcount;
7163 dpyinfo_refcount = dpyinfo->reference_count;
7164#endif /* GLYPH_DEBUG */
6fc2811b
JR
7165#ifdef MULTI_KBOARD
7166 FRAME_KBOARD (f) = kb;
7167#endif
7168 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
7169 f->output_data.w32->explicit_parent = 0;
7170
7171 /* Set the name; the functions to which we pass f expect the name to
7172 be set. */
7173 if (EQ (name, Qunbound) || NILP (name))
7174 {
ca56d953 7175 f->name = build_string (dpyinfo->w32_id_name);
6fc2811b
JR
7176 f->explicit_name = 0;
7177 }
7178 else
7179 {
7180 f->name = name;
7181 f->explicit_name = 1;
7182 /* use the frame's title when getting resources for this frame. */
7183 specbind (Qx_resource_name, name);
7184 }
7185
6fc2811b
JR
7186 /* Extract the window parameters from the supplied values
7187 that are needed to determine window geometry. */
7188 {
7189 Lisp_Object font;
7190
7191 font = w32_get_arg (parms, Qfont, "font", "Font", RES_TYPE_STRING);
7192
7193 BLOCK_INPUT;
7194 /* First, try whatever font the caller has specified. */
7195 if (STRINGP (font))
7196 {
7197 tem = Fquery_fontset (font, Qnil);
7198 if (STRINGP (tem))
d5db4077 7199 font = x_new_fontset (f, SDATA (tem));
6fc2811b 7200 else
d5db4077 7201 font = x_new_font (f, SDATA (font));
6fc2811b 7202 }
7d0393cf 7203
6fc2811b
JR
7204 /* Try out a font which we hope has bold and italic variations. */
7205 if (!STRINGP (font))
ca56d953 7206 font = x_new_font (f, "-*-Courier New-normal-r-*-*-*-100-*-*-c-*-iso8859-1");
6fc2811b 7207 if (! STRINGP (font))
ca56d953 7208 font = x_new_font (f, "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1");
6fc2811b
JR
7209 /* If those didn't work, look for something which will at least work. */
7210 if (! STRINGP (font))
ca56d953 7211 font = x_new_font (f, "-*-Fixedsys-normal-r-*-*-12-*-*-*-c-*-iso8859-1");
6fc2811b
JR
7212 UNBLOCK_INPUT;
7213 if (! STRINGP (font))
ca56d953 7214 font = build_string ("Fixedsys");
6fc2811b
JR
7215
7216 x_default_parameter (f, parms, Qfont, font,
7217 "font", "Font", RES_TYPE_STRING);
7218 }
7219
7220 x_default_parameter (f, parms, Qborder_width, make_number (2),
7221 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
6fc2811b
JR
7222 /* This defaults to 2 in order to match xterm. We recognize either
7223 internalBorderWidth or internalBorder (which is what xterm calls
7224 it). */
7225 if (NILP (Fassq (Qinternal_border_width, parms)))
7226 {
7227 Lisp_Object value;
7228
7229 value = w32_get_arg (parms, Qinternal_border_width,
7230 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
7231 if (! EQ (value, Qunbound))
7232 parms = Fcons (Fcons (Qinternal_border_width, value),
7233 parms);
7234 }
bfd6edcc 7235 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
6fc2811b
JR
7236 "internalBorderWidth", "internalBorderWidth",
7237 RES_TYPE_NUMBER);
7238
7239 /* Also do the stuff which must be set before the window exists. */
7240 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
7241 "foreground", "Foreground", RES_TYPE_STRING);
7242 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
7243 "background", "Background", RES_TYPE_STRING);
7244 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
7245 "pointerColor", "Foreground", RES_TYPE_STRING);
7246 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
7247 "cursorColor", "Foreground", RES_TYPE_STRING);
7248 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
7249 "borderColor", "BorderColor", RES_TYPE_STRING);
7250
7251 /* Init faces before x_default_parameter is called for scroll-bar
7252 parameters because that function calls x_set_scroll_bar_width,
7253 which calls change_frame_size, which calls Fset_window_buffer,
7254 which runs hooks, which call Fvertical_motion. At the end, we
7255 end up in init_iterator with a null face cache, which should not
7256 happen. */
7257 init_frame_faces (f);
ca56d953
JR
7258
7259 f->output_data.w32->dwStyle = WS_BORDER | WS_POPUP | WS_DISABLED;
6fc2811b 7260 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
9eb16b62 7261
6d906347 7262 window_prompting = x_figure_window_size (f, parms, 0);
6fc2811b 7263
9eb16b62 7264 /* No fringes on tip frame. */
be786000
KS
7265 f->fringe_cols = 0;
7266 f->left_fringe_width = 0;
7267 f->right_fringe_width = 0;
9eb16b62 7268
ca56d953
JR
7269 BLOCK_INPUT;
7270 my_create_tip_window (f);
7271 UNBLOCK_INPUT;
6fc2811b
JR
7272
7273 x_make_gc (f);
7274
7275 x_default_parameter (f, parms, Qauto_raise, Qnil,
7276 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
7277 x_default_parameter (f, parms, Qauto_lower, Qnil,
7278 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
7279 x_default_parameter (f, parms, Qcursor_type, Qbox,
7280 "cursorType", "CursorType", RES_TYPE_SYMBOL);
7281
be786000 7282 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
6fc2811b 7283 Change will not be effected unless different from the current
be786000
KS
7284 FRAME_LINES (f). */
7285 width = FRAME_COLS (f);
7286 height = FRAME_LINES (f);
7287 FRAME_LINES (f) = 0;
7288 SET_FRAME_COLS (f, 0);
6fc2811b
JR
7289 change_frame_size (f, height, width, 1, 0, 0);
7290
cd1d850f
JPW
7291 /* Add `tooltip' frame parameter's default value. */
7292 if (NILP (Fframe_parameter (frame, intern ("tooltip"))))
7293 Fmodify_frame_parameters (frame, Fcons (Fcons (intern ("tooltip"), Qt),
7294 Qnil));
7d0393cf 7295
3cf3436e
JR
7296 /* Set up faces after all frame parameters are known. This call
7297 also merges in face attributes specified for new frames.
7298
7299 Frame parameters may be changed if .Xdefaults contains
7300 specifications for the default font. For example, if there is an
7301 `Emacs.default.attributeBackground: pink', the `background-color'
7302 attribute of the frame get's set, which let's the internal border
7303 of the tooltip frame appear in pink. Prevent this. */
7304 {
7305 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
7306
7307 /* Set tip_frame here, so that */
7308 tip_frame = frame;
7309 call1 (Qface_set_after_frame_default, frame);
7d0393cf 7310
3cf3436e
JR
7311 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
7312 Fmodify_frame_parameters (frame, Fcons (Fcons (Qbackground_color, bg),
7313 Qnil));
7314 }
7d0393cf 7315
6fc2811b
JR
7316 f->no_split = 1;
7317
7318 UNGCPRO;
7319
7320 /* It is now ok to make the frame official even if we get an error
7321 below. And the frame needs to be on Vframe_list or making it
7322 visible won't work. */
7323 Vframe_list = Fcons (frame, Vframe_list);
7324
7325 /* Now that the frame is official, it counts as a reference to
7326 its display. */
7327 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
ee78dc32 7328
3cf3436e
JR
7329 /* Setting attributes of faces of the tooltip frame from resources
7330 and similar will increment face_change_count, which leads to the
7331 clearing of all current matrices. Since this isn't necessary
7332 here, avoid it by resetting face_change_count to the value it
7333 had before we created the tip frame. */
7334 face_change_count = face_change_count_before;
7335
7336 /* Discard the unwind_protect. */
6fc2811b 7337 return unbind_to (count, frame);
ee78dc32
GV
7338}
7339
3cf3436e
JR
7340
7341/* Compute where to display tip frame F. PARMS is the list of frame
7342 parameters for F. DX and DY are specified offsets from the current
7343 location of the mouse. WIDTH and HEIGHT are the width and height
7344 of the tooltip. Return coordinates relative to the root window of
7345 the display in *ROOT_X, and *ROOT_Y. */
7346
7347static void
7348compute_tip_xy (f, parms, dx, dy, width, height, root_x, root_y)
7349 struct frame *f;
7350 Lisp_Object parms, dx, dy;
7351 int width, height;
7352 int *root_x, *root_y;
7353{
3cf3436e 7354 Lisp_Object left, top;
7d0393cf 7355
3cf3436e
JR
7356 /* User-specified position? */
7357 left = Fcdr (Fassq (Qleft, parms));
7358 top = Fcdr (Fassq (Qtop, parms));
7d0393cf 7359
3cf3436e
JR
7360 /* Move the tooltip window where the mouse pointer is. Resize and
7361 show it. */
ca56d953 7362 if (!INTEGERP (left) || !INTEGERP (top))
3cf3436e 7363 {
ca56d953
JR
7364 POINT pt;
7365
3cf3436e 7366 BLOCK_INPUT;
ca56d953
JR
7367 GetCursorPos (&pt);
7368 *root_x = pt.x;
7369 *root_y = pt.y;
3cf3436e
JR
7370 UNBLOCK_INPUT;
7371 }
7372
7373 if (INTEGERP (top))
7374 *root_y = XINT (top);
7375 else if (*root_y + XINT (dy) - height < 0)
7376 *root_y -= XINT (dy);
7377 else
7378 {
7379 *root_y -= height;
7380 *root_y += XINT (dy);
7381 }
7382
7383 if (INTEGERP (left))
7384 *root_x = XINT (left);
72e4adef
JR
7385 else if (*root_x + XINT (dx) + width <= FRAME_W32_DISPLAY_INFO (f)->width)
7386 /* It fits to the right of the pointer. */
7387 *root_x += XINT (dx);
7388 else if (width + XINT (dx) <= *root_x)
7389 /* It fits to the left of the pointer. */
3cf3436e
JR
7390 *root_x -= width + XINT (dx);
7391 else
72e4adef
JR
7392 /* Put it left justified on the screen -- it ought to fit that way. */
7393 *root_x = 0;
3cf3436e
JR
7394}
7395
7396
71eab8d1 7397DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
74e1aeec
JR
7398 doc: /* Show STRING in a \"tooltip\" window on frame FRAME.
7399A tooltip window is a small window displaying a string.
7400
7401FRAME nil or omitted means use the selected frame.
7402
7403PARMS is an optional list of frame parameters which can be
7404used to change the tooltip's appearance.
7405
ca56d953
JR
7406Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
7407means use the default timeout of 5 seconds.
74e1aeec 7408
52deb19f 7409If the list of frame parameters PARMS contains a `left' parameter,
74e1aeec
JR
7410the tooltip is displayed at that x-position. Otherwise it is
7411displayed at the mouse position, with offset DX added (default is 5 if
7412DX isn't specified). Likewise for the y-position; if a `top' frame
7413parameter is specified, it determines the y-position of the tooltip
7414window, otherwise it is displayed at the mouse position, with offset
7415DY added (default is -10).
7416
7417A tooltip's maximum size is specified by `x-max-tooltip-size'.
7418Text larger than the specified size is clipped. */)
71eab8d1
AI
7419 (string, frame, parms, timeout, dx, dy)
7420 Lisp_Object string, frame, parms, timeout, dx, dy;
ee78dc32 7421{
6fc2811b
JR
7422 struct frame *f;
7423 struct window *w;
3cf3436e 7424 int root_x, root_y;
6fc2811b
JR
7425 struct buffer *old_buffer;
7426 struct text_pos pos;
7427 int i, width, height;
6fc2811b
JR
7428 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7429 int old_windows_or_buffers_changed = windows_or_buffers_changed;
331379bf 7430 int count = SPECPDL_INDEX ();
7d0393cf 7431
6fc2811b 7432 specbind (Qinhibit_redisplay, Qt);
ee78dc32 7433
dfff8a69 7434 GCPRO4 (string, parms, frame, timeout);
ee78dc32 7435
b7826503 7436 CHECK_STRING (string);
6fc2811b
JR
7437 f = check_x_frame (frame);
7438 if (NILP (timeout))
7439 timeout = make_number (5);
7440 else
b7826503 7441 CHECK_NATNUM (timeout);
ee78dc32 7442
71eab8d1
AI
7443 if (NILP (dx))
7444 dx = make_number (5);
7445 else
b7826503 7446 CHECK_NUMBER (dx);
7d0393cf 7447
71eab8d1 7448 if (NILP (dy))
dc220243 7449 dy = make_number (-10);
71eab8d1 7450 else
b7826503 7451 CHECK_NUMBER (dy);
71eab8d1 7452
dc220243
JR
7453 if (NILP (last_show_tip_args))
7454 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
7455
7456 if (!NILP (tip_frame))
7457 {
7458 Lisp_Object last_string = AREF (last_show_tip_args, 0);
7459 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
7460 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
7461
7462 if (EQ (frame, last_frame)
7463 && !NILP (Fequal (last_string, string))
7464 && !NILP (Fequal (last_parms, parms)))
7465 {
7466 struct frame *f = XFRAME (tip_frame);
7d0393cf 7467
dc220243
JR
7468 /* Only DX and DY have changed. */
7469 if (!NILP (tip_timer))
7470 {
7471 Lisp_Object timer = tip_timer;
7472 tip_timer = Qnil;
7473 call1 (Qcancel_timer, timer);
7474 }
7475
7476 BLOCK_INPUT;
be786000
KS
7477 compute_tip_xy (f, parms, dx, dy, FRAME_PIXEL_WIDTH (f),
7478 FRAME_PIXEL_HEIGHT (f), &root_x, &root_y);
d65a9cdc
JR
7479
7480 /* Put tooltip in topmost group and in position. */
ca56d953
JR
7481 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
7482 root_x, root_y, 0, 0,
7483 SWP_NOSIZE | SWP_NOACTIVATE);
d65a9cdc
JR
7484
7485 /* Ensure tooltip is on top of other topmost windows (eg menus). */
7486 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
7487 0, 0, 0, 0,
7488 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
7489
dc220243
JR
7490 UNBLOCK_INPUT;
7491 goto start_timer;
7492 }
7493 }
7494
6fc2811b
JR
7495 /* Hide a previous tip, if any. */
7496 Fx_hide_tip ();
ee78dc32 7497
dc220243
JR
7498 ASET (last_show_tip_args, 0, string);
7499 ASET (last_show_tip_args, 1, frame);
7500 ASET (last_show_tip_args, 2, parms);
7501
6fc2811b
JR
7502 /* Add default values to frame parameters. */
7503 if (NILP (Fassq (Qname, parms)))
7504 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
7505 if (NILP (Fassq (Qinternal_border_width, parms)))
7506 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
7507 if (NILP (Fassq (Qborder_width, parms)))
7508 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
7509 if (NILP (Fassq (Qborder_color, parms)))
7510 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
7511 if (NILP (Fassq (Qbackground_color, parms)))
7512 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
7513 parms);
7514
0e3fcdef
JR
7515 /* Block input until the tip has been fully drawn, to avoid crashes
7516 when drawing tips in menus. */
7517 BLOCK_INPUT;
7518
6fc2811b
JR
7519 /* Create a frame for the tooltip, and record it in the global
7520 variable tip_frame. */
ca56d953 7521 frame = x_create_tip_frame (FRAME_W32_DISPLAY_INFO (f), parms, string);
937e601e 7522 f = XFRAME (frame);
6fc2811b 7523
3cf3436e 7524 /* Set up the frame's root window. */
6fc2811b 7525 w = XWINDOW (FRAME_ROOT_WINDOW (f));
be786000 7526 w->left_col = w->top_line = make_number (0);
3cf3436e
JR
7527
7528 if (CONSP (Vx_max_tooltip_size)
7529 && INTEGERP (XCAR (Vx_max_tooltip_size))
7530 && XINT (XCAR (Vx_max_tooltip_size)) > 0
7531 && INTEGERP (XCDR (Vx_max_tooltip_size))
7532 && XINT (XCDR (Vx_max_tooltip_size)) > 0)
7533 {
be786000
KS
7534 w->total_cols = XCAR (Vx_max_tooltip_size);
7535 w->total_lines = XCDR (Vx_max_tooltip_size);
3cf3436e
JR
7536 }
7537 else
7538 {
be786000
KS
7539 w->total_cols = make_number (80);
7540 w->total_lines = make_number (40);
3cf3436e 7541 }
7d0393cf 7542
be786000 7543 FRAME_TOTAL_COLS (f) = XINT (w->total_cols);
6fc2811b
JR
7544 adjust_glyphs (f);
7545 w->pseudo_window_p = 1;
7546
7547 /* Display the tooltip text in a temporary buffer. */
6fc2811b 7548 old_buffer = current_buffer;
3cf3436e
JR
7549 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->buffer));
7550 current_buffer->truncate_lines = Qnil;
6fc2811b
JR
7551 clear_glyph_matrix (w->desired_matrix);
7552 clear_glyph_matrix (w->current_matrix);
7553 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
7554 try_window (FRAME_ROOT_WINDOW (f), pos);
7555
7556 /* Compute width and height of the tooltip. */
7557 width = height = 0;
7558 for (i = 0; i < w->desired_matrix->nrows; ++i)
ee78dc32 7559 {
6fc2811b
JR
7560 struct glyph_row *row = &w->desired_matrix->rows[i];
7561 struct glyph *last;
7562 int row_width;
7563
7564 /* Stop at the first empty row at the end. */
7565 if (!row->enabled_p || !row->displays_text_p)
7566 break;
7567
7568 /* Let the row go over the full width of the frame. */
7569 row->full_width_p = 1;
7570
4e3a1c61
JR
7571#ifdef TODO /* Investigate why some fonts need more width than is
7572 calculated for some tooltips. */
6fc2811b
JR
7573 /* There's a glyph at the end of rows that is use to place
7574 the cursor there. Don't include the width of this glyph. */
7575 if (row->used[TEXT_AREA])
7576 {
7577 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
7578 row_width = row->pixel_width - last->pixel_width;
7579 }
7580 else
4e3a1c61 7581#endif
6fc2811b 7582 row_width = row->pixel_width;
7d0393cf 7583
ca56d953 7584 /* TODO: find why tips do not draw along baseline as instructed. */
bfd6edcc 7585 height += row->height;
6fc2811b 7586 width = max (width, row_width);
ee78dc32
GV
7587 }
7588
6fc2811b
JR
7589 /* Add the frame's internal border to the width and height the X
7590 window should have. */
7591 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
7592 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
ee78dc32 7593
6fc2811b
JR
7594 /* Move the tooltip window where the mouse pointer is. Resize and
7595 show it. */
3cf3436e 7596 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
71eab8d1 7597
bfd6edcc
JR
7598 {
7599 /* Adjust Window size to take border into account. */
7600 RECT rect;
7601 rect.left = rect.top = 0;
7602 rect.right = width;
7603 rect.bottom = height;
7604 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
7605 FRAME_EXTERNAL_MENU_BAR (f));
7606
d65a9cdc 7607 /* Position and size tooltip, and put it in the topmost group. */
bfd6edcc
JR
7608 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
7609 root_x, root_y, rect.right - rect.left,
7610 rect.bottom - rect.top, SWP_NOACTIVATE);
7611
d65a9cdc
JR
7612 /* Ensure tooltip is on top of other topmost windows (eg menus). */
7613 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
7614 0, 0, 0, 0,
7615 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
7616
bfd6edcc
JR
7617 /* Let redisplay know that we have made the frame visible already. */
7618 f->async_visible = 1;
7619
7620 ShowWindow (FRAME_W32_WINDOW (f), SW_SHOWNOACTIVATE);
7621 }
ee78dc32 7622
6fc2811b
JR
7623 /* Draw into the window. */
7624 w->must_be_updated_p = 1;
7625 update_single_window (w, 1);
ee78dc32 7626
0e3fcdef
JR
7627 UNBLOCK_INPUT;
7628
6fc2811b
JR
7629 /* Restore original current buffer. */
7630 set_buffer_internal_1 (old_buffer);
7631 windows_or_buffers_changed = old_windows_or_buffers_changed;
ee78dc32 7632
dc220243 7633 start_timer:
6fc2811b
JR
7634 /* Let the tip disappear after timeout seconds. */
7635 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
7636 intern ("x-hide-tip"));
ee78dc32 7637
dfff8a69 7638 UNGCPRO;
6fc2811b 7639 return unbind_to (count, Qnil);
ee78dc32
GV
7640}
7641
ee78dc32 7642
6fc2811b 7643DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
74e1aeec
JR
7644 doc: /* Hide the current tooltip window, if there is any.
7645Value is t if tooltip was open, nil otherwise. */)
6fc2811b
JR
7646 ()
7647{
937e601e
AI
7648 int count;
7649 Lisp_Object deleted, frame, timer;
7650 struct gcpro gcpro1, gcpro2;
7651
7652 /* Return quickly if nothing to do. */
7653 if (NILP (tip_timer) && NILP (tip_frame))
7654 return Qnil;
7d0393cf 7655
937e601e
AI
7656 frame = tip_frame;
7657 timer = tip_timer;
7658 GCPRO2 (frame, timer);
7659 tip_frame = tip_timer = deleted = Qnil;
7d0393cf 7660
331379bf 7661 count = SPECPDL_INDEX ();
6fc2811b 7662 specbind (Qinhibit_redisplay, Qt);
937e601e 7663 specbind (Qinhibit_quit, Qt);
7d0393cf 7664
937e601e 7665 if (!NILP (timer))
dc220243 7666 call1 (Qcancel_timer, timer);
ee78dc32 7667
937e601e 7668 if (FRAMEP (frame))
6fc2811b 7669 {
937e601e
AI
7670 Fdelete_frame (frame, Qnil);
7671 deleted = Qt;
6fc2811b 7672 }
1edf84e7 7673
937e601e
AI
7674 UNGCPRO;
7675 return unbind_to (count, deleted);
6fc2811b 7676}
5ac45f98 7677
5ac45f98 7678
6fc2811b
JR
7679\f
7680/***********************************************************************
7681 File selection dialog
7682 ***********************************************************************/
6fc2811b
JR
7683extern Lisp_Object Qfile_name_history;
7684
1030b26b
JR
7685/* Callback for altering the behaviour of the Open File dialog.
7686 Makes the Filename text field contain "Current Directory" and be
7687 read-only when "Directories" is selected in the filter. This
7688 allows us to work around the fact that the standard Open File
7689 dialog does not support directories. */
7690UINT CALLBACK
7691file_dialog_callback (hwnd, msg, wParam, lParam)
7692 HWND hwnd;
7693 UINT msg;
7694 WPARAM wParam;
7695 LPARAM lParam;
7696{
7697 if (msg == WM_NOTIFY)
7698 {
7699 OFNOTIFY * notify = (OFNOTIFY *)lParam;
7700 /* Detect when the Filter dropdown is changed. */
ef544dc8
JR
7701 if (notify->hdr.code == CDN_TYPECHANGE
7702 || notify->hdr.code == CDN_INITDONE)
1030b26b
JR
7703 {
7704 HWND dialog = GetParent (hwnd);
7705 HWND edit_control = GetDlgItem (dialog, FILE_NAME_TEXT_FIELD);
7706
7707 /* Directories is in index 2. */
7708 if (notify->lpOFN->nFilterIndex == 2)
7709 {
7710 CommDlg_OpenSave_SetControlText (dialog, FILE_NAME_TEXT_FIELD,
7711 "Current Directory");
7712 EnableWindow (edit_control, FALSE);
7713 }
7714 else
7715 {
ef544dc8
JR
7716 /* Don't override default filename on init done. */
7717 if (notify->hdr.code == CDN_TYPECHANGE)
7718 CommDlg_OpenSave_SetControlText (dialog,
7719 FILE_NAME_TEXT_FIELD, "");
1030b26b
JR
7720 EnableWindow (edit_control, TRUE);
7721 }
7722 }
7723 }
7724 return 0;
7725}
7726
6fc2811b 7727DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 4, 0,
74e1aeec
JR
7728 doc: /* Read file name, prompting with PROMPT in directory DIR.
7729Use a file selection dialog.
7730Select DEFAULT-FILENAME in the dialog's file selection box, if
7731specified. Ensure that file exists if MUSTMATCH is non-nil. */)
6fc2811b
JR
7732 (prompt, dir, default_filename, mustmatch)
7733 Lisp_Object prompt, dir, default_filename, mustmatch;
7734{
7735 struct frame *f = SELECTED_FRAME ();
7736 Lisp_Object file = Qnil;
aed13378 7737 int count = SPECPDL_INDEX ();
6fc2811b
JR
7738 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
7739 char filename[MAX_PATH + 1];
7740 char init_dir[MAX_PATH + 1];
ef544dc8 7741 int default_filter_index = 1; /* 1: All Files, 2: Directories only */
6fc2811b
JR
7742
7743 GCPRO5 (prompt, dir, default_filename, mustmatch, file);
b7826503
PJ
7744 CHECK_STRING (prompt);
7745 CHECK_STRING (dir);
6fc2811b
JR
7746
7747 /* Create the dialog with PROMPT as title, using DIR as initial
7748 directory and using "*" as pattern. */
7749 dir = Fexpand_file_name (dir, Qnil);
dda741ec 7750 strncpy (init_dir, SDATA (ENCODE_FILE (dir)), MAX_PATH);
6fc2811b
JR
7751 init_dir[MAX_PATH] = '\0';
7752 unixtodos_filename (init_dir);
7753
7754 if (STRINGP (default_filename))
7755 {
7756 char *file_name_only;
dda741ec 7757 char *full_path_name = SDATA (ENCODE_FILE (default_filename));
5ac45f98 7758
6fc2811b 7759 unixtodos_filename (full_path_name);
5ac45f98 7760
6fc2811b
JR
7761 file_name_only = strrchr (full_path_name, '\\');
7762 if (!file_name_only)
7763 file_name_only = full_path_name;
7764 else
ef544dc8 7765 file_name_only++;
ee78dc32 7766
6fc2811b
JR
7767 strncpy (filename, file_name_only, MAX_PATH);
7768 filename[MAX_PATH] = '\0';
7769 }
ee78dc32 7770 else
6fc2811b 7771 filename[0] = '\0';
ee78dc32 7772
1030b26b
JR
7773 {
7774 OPENFILENAME file_details;
5ac45f98 7775
1030b26b
JR
7776 /* Prevent redisplay. */
7777 specbind (Qinhibit_redisplay, Qt);
7778 BLOCK_INPUT;
ee78dc32 7779
1030b26b
JR
7780 bzero (&file_details, sizeof (file_details));
7781 file_details.lStructSize = sizeof (file_details);
7782 file_details.hwndOwner = FRAME_W32_WINDOW (f);
7783 /* Undocumented Bug in Common File Dialog:
7784 If a filter is not specified, shell links are not resolved. */
7785 file_details.lpstrFilter = "All Files (*.*)\0*.*\0Directories\0*|*\0\0";
7786 file_details.lpstrFile = filename;
7787 file_details.nMaxFile = sizeof (filename);
7788 file_details.lpstrInitialDir = init_dir;
d5db4077 7789 file_details.lpstrTitle = SDATA (prompt);
ef544dc8
JR
7790
7791 /* If prompt starts with Dired, default to directories only. */
7792 /* A bit hacky, but there doesn't seem to be a better way to
7793 DTRT for dired. */
7794 if (strncmp (file_details.lpstrTitle, "Dired", 5) == 0)
7795 default_filter_index = 2;
7796
7797 file_details.nFilterIndex = default_filter_index;
7798
1030b26b
JR
7799 file_details.Flags = (OFN_HIDEREADONLY | OFN_NOCHANGEDIR
7800 | OFN_EXPLORER | OFN_ENABLEHOOK);
7801 if (!NILP (mustmatch))
7802 file_details.Flags |= OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
7803
7804 file_details.lpfnHook = (LPOFNHOOKPROC) file_dialog_callback;
7805
7806 if (GetOpenFileName (&file_details))
7807 {
7808 dostounix_filename (filename);
7809 if (file_details.nFilterIndex == 2)
7810 {
ef544dc8 7811 /* "Directories" selected - strip dummy file name. */
1030b26b
JR
7812 char * last = strrchr (filename, '/');
7813 *last = '\0';
7814 }
6fc2811b 7815
1030b26b
JR
7816 file = DECODE_FILE(build_string (filename));
7817 }
7818 /* User cancelled the dialog without making a selection. */
7819 else if (!CommDlgExtendedError ())
7820 file = Qnil;
7821 /* An error occurred, fallback on reading from the mini-buffer. */
7822 else
7823 file = Fcompleting_read (prompt, intern ("read-file-name-internal"),
7824 dir, mustmatch, dir, Qfile_name_history,
7825 default_filename, Qnil);
7826
7827 UNBLOCK_INPUT;
7828 file = unbind_to (count, file);
7829 }
ee78dc32 7830
6fc2811b 7831 UNGCPRO;
1edf84e7 7832
6fc2811b
JR
7833 /* Make "Cancel" equivalent to C-g. */
7834 if (NILP (file))
7835 Fsignal (Qquit, Qnil);
ee78dc32 7836
dfff8a69 7837 return unbind_to (count, file);
6fc2811b 7838}
ee78dc32 7839
ee78dc32 7840
6fc2811b 7841\f
6fc2811b
JR
7842/***********************************************************************
7843 w32 specialized functions
7844 ***********************************************************************/
ee78dc32 7845
d84b082d 7846DEFUN ("w32-select-font", Fw32_select_font, Sw32_select_font, 0, 2, 0,
74e1aeec
JR
7847 doc: /* Select a font using the W32 font dialog.
7848Returns an X font string corresponding to the selection. */)
d84b082d
JR
7849 (frame, include_proportional)
7850 Lisp_Object frame, include_proportional;
ee78dc32
GV
7851{
7852 FRAME_PTR f = check_x_frame (frame);
7853 CHOOSEFONT cf;
7854 LOGFONT lf;
f46e6225
GV
7855 TEXTMETRIC tm;
7856 HDC hdc;
7857 HANDLE oldobj;
ee78dc32
GV
7858 char buf[100];
7859
7860 bzero (&cf, sizeof (cf));
f46e6225 7861 bzero (&lf, sizeof (lf));
ee78dc32
GV
7862
7863 cf.lStructSize = sizeof (cf);
fbd6baed 7864 cf.hwndOwner = FRAME_W32_WINDOW (f);
d84b082d
JR
7865 cf.Flags = CF_FORCEFONTEXIST | CF_SCREENFONTS | CF_NOVERTFONTS;
7866
7867 /* Unless include_proportional is non-nil, limit the selection to
7868 monospaced fonts. */
7869 if (NILP (include_proportional))
7870 cf.Flags |= CF_FIXEDPITCHONLY;
7871
ee78dc32
GV
7872 cf.lpLogFont = &lf;
7873
f46e6225
GV
7874 /* Initialize as much of the font details as we can from the current
7875 default font. */
7876 hdc = GetDC (FRAME_W32_WINDOW (f));
7877 oldobj = SelectObject (hdc, FRAME_FONT (f)->hfont);
7878 GetTextFace (hdc, LF_FACESIZE, lf.lfFaceName);
7879 if (GetTextMetrics (hdc, &tm))
7880 {
7881 lf.lfHeight = tm.tmInternalLeading - tm.tmHeight;
7882 lf.lfWeight = tm.tmWeight;
7883 lf.lfItalic = tm.tmItalic;
7884 lf.lfUnderline = tm.tmUnderlined;
7885 lf.lfStrikeOut = tm.tmStruckOut;
f46e6225
GV
7886 lf.lfCharSet = tm.tmCharSet;
7887 cf.Flags |= CF_INITTOLOGFONTSTRUCT;
7888 }
7889 SelectObject (hdc, oldobj);
6fc2811b 7890 ReleaseDC (FRAME_W32_WINDOW (f), hdc);
f46e6225 7891
767b1ff0 7892 if (!ChooseFont (&cf) || !w32_to_x_font (&lf, buf, 100, NULL))
3c190163 7893 return Qnil;
ee78dc32
GV
7894
7895 return build_string (buf);
7896}
7897
74e1aeec
JR
7898DEFUN ("w32-send-sys-command", Fw32_send_sys_command,
7899 Sw32_send_sys_command, 1, 2, 0,
7900 doc: /* Send frame a Windows WM_SYSCOMMAND message of type COMMAND.
52deb19f 7901Some useful values for COMMAND are #xf030 to maximize frame (#xf020
d84b082d
JR
7902to minimize), #xf120 to restore frame to original size, and #xf100
7903to activate the menubar for keyboard access. #xf140 activates the
74e1aeec
JR
7904screen saver if defined.
7905
7906If optional parameter FRAME is not specified, use selected frame. */)
1edf84e7
GV
7907 (command, frame)
7908 Lisp_Object command, frame;
7909{
1edf84e7
GV
7910 FRAME_PTR f = check_x_frame (frame);
7911
b7826503 7912 CHECK_NUMBER (command);
1edf84e7 7913
ce6059da 7914 PostMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, XINT (command), 0);
1edf84e7
GV
7915
7916 return Qnil;
7917}
7918
55dcfc15 7919DEFUN ("w32-shell-execute", Fw32_shell_execute, Sw32_shell_execute, 2, 4, 0,
74e1aeec
JR
7920 doc: /* Get Windows to perform OPERATION on DOCUMENT.
7921This is a wrapper around the ShellExecute system function, which
7922invokes the application registered to handle OPERATION for DOCUMENT.
7923OPERATION is typically \"open\", \"print\" or \"explore\" (but can be
7924nil for the default action), and DOCUMENT is typically the name of a
7925document file or URL, but can also be a program executable to run or
7926a directory to open in the Windows Explorer.
7927
7928If DOCUMENT is a program executable, PARAMETERS can be a string
7929containing command line parameters, but otherwise should be nil.
7930
7931SHOW-FLAG can be used to control whether the invoked application is hidden
7932or minimized. If SHOW-FLAG is nil, the application is displayed normally,
7933otherwise it is an integer representing a ShowWindow flag:
7934
7935 0 - start hidden
7936 1 - start normally
7937 3 - start maximized
7938 6 - start minimized */)
55dcfc15
AI
7939 (operation, document, parameters, show_flag)
7940 Lisp_Object operation, document, parameters, show_flag;
7941{
7942 Lisp_Object current_dir;
7943
b7826503 7944 CHECK_STRING (document);
55dcfc15
AI
7945
7946 /* Encode filename and current directory. */
7947 current_dir = ENCODE_FILE (current_buffer->directory);
7948 document = ENCODE_FILE (document);
7949 if ((int) ShellExecute (NULL,
6fc2811b 7950 (STRINGP (operation) ?
d5db4077
KR
7951 SDATA (operation) : NULL),
7952 SDATA (document),
55dcfc15 7953 (STRINGP (parameters) ?
d5db4077
KR
7954 SDATA (parameters) : NULL),
7955 SDATA (current_dir),
55dcfc15
AI
7956 (INTEGERP (show_flag) ?
7957 XINT (show_flag) : SW_SHOWDEFAULT))
7958 > 32)
7959 return Qt;
90d97e64 7960 error ("ShellExecute failed: %s", w32_strerror (0));
55dcfc15
AI
7961}
7962
ccc2d29c
GV
7963/* Lookup virtual keycode from string representing the name of a
7964 non-ascii keystroke into the corresponding virtual key, using
7965 lispy_function_keys. */
7966static int
7967lookup_vk_code (char *key)
7968{
7969 int i;
7970
7971 for (i = 0; i < 256; i++)
7972 if (lispy_function_keys[i] != 0
7973 && strcmp (lispy_function_keys[i], key) == 0)
7974 return i;
7975
7976 return -1;
7977}
7978
7979/* Convert a one-element vector style key sequence to a hot key
7980 definition. */
2ba49441 7981static Lisp_Object
ccc2d29c
GV
7982w32_parse_hot_key (key)
7983 Lisp_Object key;
7984{
7985 /* Copied from Fdefine_key and store_in_keymap. */
7986 register Lisp_Object c;
7987 int vk_code;
7988 int lisp_modifiers;
7989 int w32_modifiers;
7990 struct gcpro gcpro1;
7991
b7826503 7992 CHECK_VECTOR (key);
ccc2d29c
GV
7993
7994 if (XFASTINT (Flength (key)) != 1)
7995 return Qnil;
7996
7997 GCPRO1 (key);
7998
7999 c = Faref (key, make_number (0));
8000
8001 if (CONSP (c) && lucid_event_type_list_p (c))
8002 c = Fevent_convert_list (c);
8003
8004 UNGCPRO;
8005
8006 if (! INTEGERP (c) && ! SYMBOLP (c))
8007 error ("Key definition is invalid");
8008
8009 /* Work out the base key and the modifiers. */
8010 if (SYMBOLP (c))
8011 {
8012 c = parse_modifiers (c);
2ba49441 8013 lisp_modifiers = XINT (Fcar (Fcdr (c)));
ccc2d29c
GV
8014 c = Fcar (c);
8015 if (!SYMBOLP (c))
8016 abort ();
d5db4077 8017 vk_code = lookup_vk_code (SDATA (SYMBOL_NAME (c)));
ccc2d29c
GV
8018 }
8019 else if (INTEGERP (c))
8020 {
8021 lisp_modifiers = XINT (c) & ~CHARACTERBITS;
8022 /* Many ascii characters are their own virtual key code. */
8023 vk_code = XINT (c) & CHARACTERBITS;
8024 }
8025
8026 if (vk_code < 0 || vk_code > 255)
8027 return Qnil;
8028
8029 if ((lisp_modifiers & meta_modifier) != 0
8030 && !NILP (Vw32_alt_is_meta))
8031 lisp_modifiers |= alt_modifier;
8032
71eab8d1
AI
8033 /* Supply defs missing from mingw32. */
8034#ifndef MOD_ALT
8035#define MOD_ALT 0x0001
8036#define MOD_CONTROL 0x0002
8037#define MOD_SHIFT 0x0004
8038#define MOD_WIN 0x0008
8039#endif
8040
ccc2d29c
GV
8041 /* Convert lisp modifiers to Windows hot-key form. */
8042 w32_modifiers = (lisp_modifiers & hyper_modifier) ? MOD_WIN : 0;
8043 w32_modifiers |= (lisp_modifiers & alt_modifier) ? MOD_ALT : 0;
8044 w32_modifiers |= (lisp_modifiers & ctrl_modifier) ? MOD_CONTROL : 0;
8045 w32_modifiers |= (lisp_modifiers & shift_modifier) ? MOD_SHIFT : 0;
8046
8047 return HOTKEY (vk_code, w32_modifiers);
8048}
8049
74e1aeec
JR
8050DEFUN ("w32-register-hot-key", Fw32_register_hot_key,
8051 Sw32_register_hot_key, 1, 1, 0,
8052 doc: /* Register KEY as a hot-key combination.
8053Certain key combinations like Alt-Tab are reserved for system use on
8054Windows, and therefore are normally intercepted by the system. However,
8055most of these key combinations can be received by registering them as
8056hot-keys, overriding their special meaning.
8057
8058KEY must be a one element key definition in vector form that would be
8059acceptable to `define-key' (e.g. [A-tab] for Alt-Tab). The meta
8060modifier is interpreted as Alt if `w32-alt-is-meta' is t, and hyper
8061is always interpreted as the Windows modifier keys.
8062
8063The return value is the hotkey-id if registered, otherwise nil. */)
ccc2d29c
GV
8064 (key)
8065 Lisp_Object key;
8066{
8067 key = w32_parse_hot_key (key);
8068
8069 if (NILP (Fmemq (key, w32_grabbed_keys)))
8070 {
8071 /* Reuse an empty slot if possible. */
8072 Lisp_Object item = Fmemq (Qnil, w32_grabbed_keys);
8073
8074 /* Safe to add new key to list, even if we have focus. */
8075 if (NILP (item))
8076 w32_grabbed_keys = Fcons (key, w32_grabbed_keys);
8077 else
f3fbd155 8078 XSETCAR (item, key);
ccc2d29c
GV
8079
8080 /* Notify input thread about new hot-key definition, so that it
8081 takes effect without needing to switch focus. */
2ba49441
JR
8082#ifdef USE_LISP_UNION_TYPE
8083 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
8084 (WPARAM) key.i, 0);
8085#else
ccc2d29c
GV
8086 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
8087 (WPARAM) key, 0);
2ba49441 8088#endif
ccc2d29c
GV
8089 }
8090
8091 return key;
8092}
8093
74e1aeec
JR
8094DEFUN ("w32-unregister-hot-key", Fw32_unregister_hot_key,
8095 Sw32_unregister_hot_key, 1, 1, 0,
52deb19f 8096 doc: /* Unregister KEY as a hot-key combination. */)
ccc2d29c
GV
8097 (key)
8098 Lisp_Object key;
8099{
8100 Lisp_Object item;
8101
8102 if (!INTEGERP (key))
8103 key = w32_parse_hot_key (key);
8104
8105 item = Fmemq (key, w32_grabbed_keys);
8106
8107 if (!NILP (item))
8108 {
8109 /* Notify input thread about hot-key definition being removed, so
8110 that it takes effect without needing focus switch. */
2ba49441
JR
8111#ifdef USE_LISP_UNION_TYPE
8112 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
8113 (WPARAM) XINT (XCAR (item)), (LPARAM) item.i))
8114#else
ccc2d29c 8115 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
6dd6baa5 8116 (WPARAM) XINT (XCAR (item)), (LPARAM) item))
2ba49441
JR
8117
8118#endif
ccc2d29c
GV
8119 {
8120 MSG msg;
8121 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
8122 }
8123 return Qt;
8124 }
8125 return Qnil;
8126}
8127
74e1aeec
JR
8128DEFUN ("w32-registered-hot-keys", Fw32_registered_hot_keys,
8129 Sw32_registered_hot_keys, 0, 0, 0,
8130 doc: /* Return list of registered hot-key IDs. */)
ccc2d29c
GV
8131 ()
8132{
8133 return Fcopy_sequence (w32_grabbed_keys);
8134}
8135
74e1aeec
JR
8136DEFUN ("w32-reconstruct-hot-key", Fw32_reconstruct_hot_key,
8137 Sw32_reconstruct_hot_key, 1, 1, 0,
52deb19f
JB
8138 doc: /* Convert hot-key ID to a lisp key combination.
8139usage: (w32-reconstruct-hot-key ID) */)
ccc2d29c
GV
8140 (hotkeyid)
8141 Lisp_Object hotkeyid;
8142{
8143 int vk_code, w32_modifiers;
8144 Lisp_Object key;
8145
b7826503 8146 CHECK_NUMBER (hotkeyid);
ccc2d29c
GV
8147
8148 vk_code = HOTKEY_VK_CODE (hotkeyid);
8149 w32_modifiers = HOTKEY_MODIFIERS (hotkeyid);
8150
8151 if (lispy_function_keys[vk_code])
8152 key = intern (lispy_function_keys[vk_code]);
8153 else
8154 key = make_number (vk_code);
8155
8156 key = Fcons (key, Qnil);
8157 if (w32_modifiers & MOD_SHIFT)
3ef68e6b 8158 key = Fcons (Qshift, key);
ccc2d29c 8159 if (w32_modifiers & MOD_CONTROL)
3ef68e6b 8160 key = Fcons (Qctrl, key);
ccc2d29c 8161 if (w32_modifiers & MOD_ALT)
3ef68e6b 8162 key = Fcons (NILP (Vw32_alt_is_meta) ? Qalt : Qmeta, key);
ccc2d29c 8163 if (w32_modifiers & MOD_WIN)
3ef68e6b 8164 key = Fcons (Qhyper, key);
ccc2d29c
GV
8165
8166 return key;
8167}
adcc3809 8168
74e1aeec
JR
8169DEFUN ("w32-toggle-lock-key", Fw32_toggle_lock_key,
8170 Sw32_toggle_lock_key, 1, 2, 0,
8171 doc: /* Toggle the state of the lock key KEY.
8172KEY can be `capslock', `kp-numlock', or `scroll'.
8173If the optional parameter NEW-STATE is a number, then the state of KEY
8174is set to off if the low bit of NEW-STATE is zero, otherwise on. */)
adcc3809
GV
8175 (key, new_state)
8176 Lisp_Object key, new_state;
8177{
8178 int vk_code;
adcc3809
GV
8179
8180 if (EQ (key, intern ("capslock")))
8181 vk_code = VK_CAPITAL;
8182 else if (EQ (key, intern ("kp-numlock")))
8183 vk_code = VK_NUMLOCK;
8184 else if (EQ (key, intern ("scroll")))
8185 vk_code = VK_SCROLL;
8186 else
8187 return Qnil;
8188
8189 if (!dwWindowsThreadId)
8190 return make_number (w32_console_toggle_lock_key (vk_code, new_state));
8191
2ba49441
JR
8192#ifdef USE_LISP_UNION_TYPE
8193 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
8194 (WPARAM) vk_code, (LPARAM) new_state.i))
8195#else
adcc3809 8196 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
6dd6baa5 8197 (WPARAM) vk_code, (LPARAM) new_state))
2ba49441 8198#endif
adcc3809
GV
8199 {
8200 MSG msg;
8201 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
8202 return make_number (msg.wParam);
8203 }
8204 return Qnil;
8205}
ee78dc32 8206\f
2254bcde 8207DEFUN ("file-system-info", Ffile_system_info, Sfile_system_info, 1, 1, 0,
74e1aeec
JR
8208 doc: /* Return storage information about the file system FILENAME is on.
8209Value is a list of floats (TOTAL FREE AVAIL), where TOTAL is the total
8210storage of the file system, FREE is the free storage, and AVAIL is the
8211storage available to a non-superuser. All 3 numbers are in bytes.
8212If the underlying system call fails, value is nil. */)
2254bcde
AI
8213 (filename)
8214 Lisp_Object filename;
8215{
8216 Lisp_Object encoded, value;
8217
b7826503 8218 CHECK_STRING (filename);
2254bcde
AI
8219 filename = Fexpand_file_name (filename, Qnil);
8220 encoded = ENCODE_FILE (filename);
8221
8222 value = Qnil;
8223
8224 /* Determining the required information on Windows turns out, sadly,
8225 to be more involved than one would hope. The original Win32 api
8226 call for this will return bogus information on some systems, but we
8227 must dynamically probe for the replacement api, since that was
8228 added rather late on. */
8229 {
8230 HMODULE hKernel = GetModuleHandle ("kernel32");
8231 BOOL (*pfn_GetDiskFreeSpaceEx)
8232 (char *, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER)
8233 = (void *) GetProcAddress (hKernel, "GetDiskFreeSpaceEx");
8234
8235 /* On Windows, we may need to specify the root directory of the
8236 volume holding FILENAME. */
8237 char rootname[MAX_PATH];
d5db4077 8238 char *name = SDATA (encoded);
2254bcde
AI
8239
8240 /* find the root name of the volume if given */
8241 if (isalpha (name[0]) && name[1] == ':')
8242 {
8243 rootname[0] = name[0];
8244 rootname[1] = name[1];
8245 rootname[2] = '\\';
8246 rootname[3] = 0;
8247 }
8248 else if (IS_DIRECTORY_SEP (name[0]) && IS_DIRECTORY_SEP (name[1]))
8249 {
8250 char *str = rootname;
8251 int slashes = 4;
8252 do
8253 {
8254 if (IS_DIRECTORY_SEP (*name) && --slashes == 0)
8255 break;
8256 *str++ = *name++;
8257 }
8258 while ( *name );
8259
8260 *str++ = '\\';
8261 *str = 0;
8262 }
8263
8264 if (pfn_GetDiskFreeSpaceEx)
8265 {
ac849ba4
JR
8266 /* Unsigned large integers cannot be cast to double, so
8267 use signed ones instead. */
2254bcde
AI
8268 LARGE_INTEGER availbytes;
8269 LARGE_INTEGER freebytes;
8270 LARGE_INTEGER totalbytes;
8271
8272 if (pfn_GetDiskFreeSpaceEx(rootname,
ac849ba4
JR
8273 (ULARGE_INTEGER *)&availbytes,
8274 (ULARGE_INTEGER *)&totalbytes,
8275 (ULARGE_INTEGER *)&freebytes))
2254bcde
AI
8276 value = list3 (make_float ((double) totalbytes.QuadPart),
8277 make_float ((double) freebytes.QuadPart),
8278 make_float ((double) availbytes.QuadPart));
8279 }
8280 else
8281 {
8282 DWORD sectors_per_cluster;
8283 DWORD bytes_per_sector;
8284 DWORD free_clusters;
8285 DWORD total_clusters;
8286
8287 if (GetDiskFreeSpace(rootname,
8288 &sectors_per_cluster,
8289 &bytes_per_sector,
8290 &free_clusters,
8291 &total_clusters))
8292 value = list3 (make_float ((double) total_clusters
8293 * sectors_per_cluster * bytes_per_sector),
8294 make_float ((double) free_clusters
8295 * sectors_per_cluster * bytes_per_sector),
8296 make_float ((double) free_clusters
8297 * sectors_per_cluster * bytes_per_sector));
8298 }
8299 }
8300
8301 return value;
8302}
8303\f
39a0e135
JR
8304DEFUN ("default-printer-name", Fdefault_printer_name, Sdefault_printer_name,
8305 0, 0, 0, doc: /* Return the name of Windows default printer device. */)
8306 ()
8307{
8308 static char pname_buf[256];
8309 int err;
8310 HANDLE hPrn;
8311 PRINTER_INFO_2 *ppi2 = NULL;
8312 DWORD dwNeeded = 0, dwReturned = 0;
8313
8314 /* Retrieve the default string from Win.ini (the registry).
8315 * String will be in form "printername,drivername,portname".
8316 * This is the most portable way to get the default printer. */
8317 if (GetProfileString ("windows", "device", ",,", pname_buf, sizeof (pname_buf)) <= 0)
8318 return Qnil;
8319 /* printername precedes first "," character */
8320 strtok (pname_buf, ",");
8321 /* We want to know more than the printer name */
8322 if (!OpenPrinter (pname_buf, &hPrn, NULL))
8323 return Qnil;
8324 GetPrinter (hPrn, 2, NULL, 0, &dwNeeded);
8325 if (dwNeeded == 0)
8326 {
8327 ClosePrinter (hPrn);
8328 return Qnil;
8329 }
8330 /* Allocate memory for the PRINTER_INFO_2 struct */
8331 ppi2 = (PRINTER_INFO_2 *) xmalloc (dwNeeded);
8332 if (!ppi2)
8333 {
8334 ClosePrinter (hPrn);
8335 return Qnil;
8336 }
8337 /* Call GetPrinter() again with big enouth memory block */
8338 err = GetPrinter (hPrn, 2, (LPBYTE)ppi2, dwNeeded, &dwReturned);
8339 ClosePrinter (hPrn);
8340 if (!err)
8341 {
8342 xfree(ppi2);
8343 return Qnil;
8344 }
8345
8346 if (ppi2)
8347 {
8348 if (ppi2->Attributes & PRINTER_ATTRIBUTE_SHARED && ppi2->pServerName)
8349 {
8350 /* a remote printer */
8351 if (*ppi2->pServerName == '\\')
8352 _snprintf(pname_buf, sizeof (pname_buf), "%s\\%s", ppi2->pServerName,
8353 ppi2->pShareName);
8354 else
8355 _snprintf(pname_buf, sizeof (pname_buf), "\\\\%s\\%s", ppi2->pServerName,
8356 ppi2->pShareName);
8357 pname_buf[sizeof (pname_buf) - 1] = '\0';
8358 }
8359 else
8360 {
8361 /* a local printer */
8362 strncpy(pname_buf, ppi2->pPortName, sizeof (pname_buf));
8363 pname_buf[sizeof (pname_buf) - 1] = '\0';
8364 /* `pPortName' can include several ports, delimited by ','.
8365 * we only use the first one. */
8366 strtok(pname_buf, ",");
8367 }
8368 xfree(ppi2);
8369 }
8370
8371 return build_string (pname_buf);
8372}
8373\f
0e3fcdef
JR
8374/***********************************************************************
8375 Initialization
8376 ***********************************************************************/
8377
52deb19f 8378/* Keep this list in the same order as frame_parms in frame.c.
6d906347
KS
8379 Use 0 for unsupported frame parameters. */
8380
8381frame_parm_handler w32_frame_parm_handlers[] =
8382{
8383 x_set_autoraise,
8384 x_set_autolower,
8385 x_set_background_color,
8386 x_set_border_color,
8387 x_set_border_width,
8388 x_set_cursor_color,
8389 x_set_cursor_type,
8390 x_set_font,
8391 x_set_foreground_color,
8392 x_set_icon_name,
8393 x_set_icon_type,
8394 x_set_internal_border_width,
8395 x_set_menu_bar_lines,
8396 x_set_mouse_color,
8397 x_explicitly_set_name,
8398 x_set_scroll_bar_width,
8399 x_set_title,
8400 x_set_unsplittable,
8401 x_set_vertical_scroll_bars,
8402 x_set_visibility,
8403 x_set_tool_bar_lines,
8404 0, /* x_set_scroll_bar_foreground, */
8405 0, /* x_set_scroll_bar_background, */
8406 x_set_screen_gamma,
8407 x_set_line_spacing,
8408 x_set_fringe_width,
8409 x_set_fringe_width,
8410 0, /* x_set_wait_for_wm, */
8411 x_set_fullscreen,
8412};
8413
0e3fcdef 8414void
fbd6baed 8415syms_of_w32fns ()
ee78dc32 8416{
afc390dc
JR
8417 globals_of_w32fns ();
8418 /* This is zero if not using MS-Windows. */
1edf84e7 8419 w32_in_use = 0;
9eb16b62
JR
8420 track_mouse_window = NULL;
8421
d285988b
JR
8422 w32_visible_system_caret_hwnd = NULL;
8423
ee78dc32
GV
8424 Qnone = intern ("none");
8425 staticpro (&Qnone);
ee78dc32
GV
8426 Qsuppress_icon = intern ("suppress-icon");
8427 staticpro (&Qsuppress_icon);
ee78dc32
GV
8428 Qundefined_color = intern ("undefined-color");
8429 staticpro (&Qundefined_color);
dc220243
JR
8430 Qcancel_timer = intern ("cancel-timer");
8431 staticpro (&Qcancel_timer);
ee78dc32 8432
adcc3809
GV
8433 Qhyper = intern ("hyper");
8434 staticpro (&Qhyper);
8435 Qsuper = intern ("super");
8436 staticpro (&Qsuper);
8437 Qmeta = intern ("meta");
8438 staticpro (&Qmeta);
8439 Qalt = intern ("alt");
8440 staticpro (&Qalt);
8441 Qctrl = intern ("ctrl");
8442 staticpro (&Qctrl);
8443 Qcontrol = intern ("control");
8444 staticpro (&Qcontrol);
8445 Qshift = intern ("shift");
8446 staticpro (&Qshift);
f7b9d4d1 8447 /* This is the end of symbol initialization. */
adcc3809 8448
6fc2811b
JR
8449 /* Text property `display' should be nonsticky by default. */
8450 Vtext_property_default_nonsticky
8451 = Fcons (Fcons (Qdisplay, Qt), Vtext_property_default_nonsticky);
8452
8453
ee78dc32
GV
8454 Fput (Qundefined_color, Qerror_conditions,
8455 Fcons (Qundefined_color, Fcons (Qerror, Qnil)));
8456 Fput (Qundefined_color, Qerror_message,
8457 build_string ("Undefined color"));
8458
ccc2d29c
GV
8459 staticpro (&w32_grabbed_keys);
8460 w32_grabbed_keys = Qnil;
8461
fbd6baed 8462 DEFVAR_LISP ("w32-color-map", &Vw32_color_map,
52deb19f 8463 doc: /* An array of color name mappings for Windows. */);
fbd6baed 8464 Vw32_color_map = Qnil;
ee78dc32 8465
fbd6baed 8466 DEFVAR_LISP ("w32-pass-alt-to-system", &Vw32_pass_alt_to_system,
74e1aeec
JR
8467 doc: /* Non-nil if alt key presses are passed on to Windows.
8468When non-nil, for example, alt pressed and released and then space will
8469open the System menu. When nil, Emacs silently swallows alt key events. */);
fbd6baed 8470 Vw32_pass_alt_to_system = Qnil;
da36a4d6 8471
fbd6baed 8472 DEFVAR_LISP ("w32-alt-is-meta", &Vw32_alt_is_meta,
74e1aeec
JR
8473 doc: /* Non-nil if the alt key is to be considered the same as the meta key.
8474When nil, Emacs will translate the alt key to the Alt modifier, and not Meta. */);
fbd6baed 8475 Vw32_alt_is_meta = Qt;
8c205c63 8476
2ba49441
JR
8477 DEFVAR_INT ("w32-quit-key", &w32_quit_key,
8478 doc: /* If non-zero, the virtual key code for an alternative quit key. */);
8479 w32_quit_key = 0;
7d081355 8480
7d0393cf 8481 DEFVAR_LISP ("w32-pass-lwindow-to-system",
ccc2d29c 8482 &Vw32_pass_lwindow_to_system,
74e1aeec
JR
8483 doc: /* Non-nil if the left \"Windows\" key is passed on to Windows.
8484When non-nil, the Start menu is opened by tapping the key. */);
ccc2d29c
GV
8485 Vw32_pass_lwindow_to_system = Qt;
8486
7d0393cf 8487 DEFVAR_LISP ("w32-pass-rwindow-to-system",
ccc2d29c 8488 &Vw32_pass_rwindow_to_system,
74e1aeec
JR
8489 doc: /* Non-nil if the right \"Windows\" key is passed on to Windows.
8490When non-nil, the Start menu is opened by tapping the key. */);
ccc2d29c
GV
8491 Vw32_pass_rwindow_to_system = Qt;
8492
2ba49441 8493 DEFVAR_LISP ("w32-phantom-key-code",
adcc3809 8494 &Vw32_phantom_key_code,
2ba49441 8495 doc: /* Virtual key code used to generate \"phantom\" key presses.
74e1aeec
JR
8496Value is a number between 0 and 255.
8497
8498Phantom key presses are generated in order to stop the system from
8499acting on \"Windows\" key events when `w32-pass-lwindow-to-system' or
8500`w32-pass-rwindow-to-system' is nil. */);
ce6059da
AI
8501 /* Although 255 is technically not a valid key code, it works and
8502 means that this hack won't interfere with any real key code. */
2ba49441 8503 XSETINT (Vw32_phantom_key_code, 255);
adcc3809 8504
7d0393cf 8505 DEFVAR_LISP ("w32-enable-num-lock",
ccc2d29c 8506 &Vw32_enable_num_lock,
74e1aeec
JR
8507 doc: /* Non-nil if Num Lock should act normally.
8508Set to nil to see Num Lock as the key `kp-numlock'. */);
ccc2d29c
GV
8509 Vw32_enable_num_lock = Qt;
8510
7d0393cf 8511 DEFVAR_LISP ("w32-enable-caps-lock",
ccc2d29c 8512 &Vw32_enable_caps_lock,
74e1aeec
JR
8513 doc: /* Non-nil if Caps Lock should act normally.
8514Set to nil to see Caps Lock as the key `capslock'. */);
ccc2d29c
GV
8515 Vw32_enable_caps_lock = Qt;
8516
8517 DEFVAR_LISP ("w32-scroll-lock-modifier",
8518 &Vw32_scroll_lock_modifier,
74e1aeec
JR
8519 doc: /* Modifier to use for the Scroll Lock on state.
8520The value can be hyper, super, meta, alt, control or shift for the
8521respective modifier, or nil to see Scroll Lock as the key `scroll'.
8522Any other value will cause the key to be ignored. */);
ccc2d29c
GV
8523 Vw32_scroll_lock_modifier = Qt;
8524
8525 DEFVAR_LISP ("w32-lwindow-modifier",
8526 &Vw32_lwindow_modifier,
74e1aeec
JR
8527 doc: /* Modifier to use for the left \"Windows\" key.
8528The value can be hyper, super, meta, alt, control or shift for the
8529respective modifier, or nil to appear as the key `lwindow'.
8530Any other value will cause the key to be ignored. */);
ccc2d29c
GV
8531 Vw32_lwindow_modifier = Qnil;
8532
8533 DEFVAR_LISP ("w32-rwindow-modifier",
8534 &Vw32_rwindow_modifier,
74e1aeec
JR
8535 doc: /* Modifier to use for the right \"Windows\" key.
8536The value can be hyper, super, meta, alt, control or shift for the
8537respective modifier, or nil to appear as the key `rwindow'.
8538Any other value will cause the key to be ignored. */);
ccc2d29c
GV
8539 Vw32_rwindow_modifier = Qnil;
8540
8541 DEFVAR_LISP ("w32-apps-modifier",
8542 &Vw32_apps_modifier,
74e1aeec
JR
8543 doc: /* Modifier to use for the \"Apps\" key.
8544The value can be hyper, super, meta, alt, control or shift for the
8545respective modifier, or nil to appear as the key `apps'.
8546Any other value will cause the key to be ignored. */);
ccc2d29c 8547 Vw32_apps_modifier = Qnil;
da36a4d6 8548
d84b082d 8549 DEFVAR_BOOL ("w32-enable-synthesized-fonts", &w32_enable_synthesized_fonts,
74e1aeec 8550 doc: /* Non-nil enables selection of artificially italicized and bold fonts. */);
d84b082d 8551 w32_enable_synthesized_fonts = 0;
5ac45f98 8552
fbd6baed 8553 DEFVAR_LISP ("w32-enable-palette", &Vw32_enable_palette,
74e1aeec 8554 doc: /* Non-nil enables Windows palette management to map colors exactly. */);
fbd6baed 8555 Vw32_enable_palette = Qt;
5ac45f98 8556
fbd6baed 8557 DEFVAR_INT ("w32-mouse-button-tolerance",
2ba49441 8558 &w32_mouse_button_tolerance,
74e1aeec
JR
8559 doc: /* Analogue of double click interval for faking middle mouse events.
8560The value is the minimum time in milliseconds that must elapse between
8561left/right button down events before they are considered distinct events.
8562If both mouse buttons are depressed within this interval, a middle mouse
8563button down event is generated instead. */);
2ba49441 8564 w32_mouse_button_tolerance = GetDoubleClickTime () / 2;
5ac45f98 8565
fbd6baed 8566 DEFVAR_INT ("w32-mouse-move-interval",
2ba49441 8567 &w32_mouse_move_interval,
74e1aeec
JR
8568 doc: /* Minimum interval between mouse move events.
8569The value is the minimum time in milliseconds that must elapse between
8570successive mouse move (or scroll bar drag) events before they are
8571reported as lisp events. */);
2ba49441 8572 w32_mouse_move_interval = 0;
84fb1139 8573
74214547
JR
8574 DEFVAR_BOOL ("w32-pass-extra-mouse-buttons-to-system",
8575 &w32_pass_extra_mouse_buttons_to_system,
8576 doc: /* Non-nil if the fourth and fifth mouse buttons are passed to Windows.
8577Recent versions of Windows support mice with up to five buttons.
8578Since most applications don't support these extra buttons, most mouse
8579drivers will allow you to map them to functions at the system level.
8580If this variable is non-nil, Emacs will pass them on, allowing the
8581system to handle them. */);
8582 w32_pass_extra_mouse_buttons_to_system = 0;
8583
ee78dc32 8584 DEFVAR_LISP ("x-pointer-shape", &Vx_pointer_shape,
74e1aeec
JR
8585 doc: /* The shape of the pointer when over text.
8586Changing the value does not affect existing frames
8587unless you set the mouse color. */);
ee78dc32
GV
8588 Vx_pointer_shape = Qnil;
8589
ee78dc32
GV
8590 Vx_nontext_pointer_shape = Qnil;
8591
8592 Vx_mode_pointer_shape = Qnil;
8593
0af913d7 8594 DEFVAR_LISP ("x-hourglass-pointer-shape", &Vx_hourglass_pointer_shape,
74e1aeec
JR
8595 doc: /* The shape of the pointer when Emacs is busy.
8596This variable takes effect when you create a new frame
8597or when you set the mouse color. */);
0af913d7 8598 Vx_hourglass_pointer_shape = Qnil;
6fc2811b 8599
0af913d7 8600 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
74e1aeec 8601 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
0af913d7 8602 display_hourglass_p = 1;
7d0393cf 8603
0af913d7 8604 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
74e1aeec
JR
8605 doc: /* *Seconds to wait before displaying an hourglass pointer.
8606Value must be an integer or float. */);
0af913d7 8607 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
f79e6790 8608
6fc2811b 8609 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
ee78dc32 8610 &Vx_sensitive_text_pointer_shape,
74e1aeec
JR
8611 doc: /* The shape of the pointer when over mouse-sensitive text.
8612This variable takes effect when you create a new frame
8613or when you set the mouse color. */);
ee78dc32
GV
8614 Vx_sensitive_text_pointer_shape = Qnil;
8615
4694d762
JR
8616 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
8617 &Vx_window_horizontal_drag_shape,
74e1aeec
JR
8618 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
8619This variable takes effect when you create a new frame
8620or when you set the mouse color. */);
4694d762
JR
8621 Vx_window_horizontal_drag_shape = Qnil;
8622
ee78dc32 8623 DEFVAR_LISP ("x-cursor-fore-pixel", &Vx_cursor_fore_pixel,
74e1aeec 8624 doc: /* A string indicating the foreground color of the cursor box. */);
ee78dc32
GV
8625 Vx_cursor_fore_pixel = Qnil;
8626
3cf3436e 8627 DEFVAR_LISP ("x-max-tooltip-size", &Vx_max_tooltip_size,
b3700ae7
JR
8628 doc: /* Maximum size for tooltips.
8629Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
3cf3436e 8630 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
7d0393cf 8631
ee78dc32 8632 DEFVAR_LISP ("x-no-window-manager", &Vx_no_window_manager,
74e1aeec
JR
8633 doc: /* Non-nil if no window manager is in use.
8634Emacs doesn't try to figure this out; this is always nil
8635unless you set it to something else. */);
ee78dc32
GV
8636 /* We don't have any way to find this out, so set it to nil
8637 and maybe the user would like to set it to t. */
8638 Vx_no_window_manager = Qnil;
8639
4587b026
GV
8640 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
8641 &Vx_pixel_size_width_font_regexp,
74e1aeec
JR
8642 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
8643
8644Since Emacs gets width of a font matching with this regexp from
8645PIXEL_SIZE field of the name, font finding mechanism gets faster for
8646such a font. This is especially effective for such large fonts as
8647Chinese, Japanese, and Korean. */);
4587b026
GV
8648 Vx_pixel_size_width_font_regexp = Qnil;
8649
33d52f9c
GV
8650 DEFVAR_LISP ("w32-bdf-filename-alist",
8651 &Vw32_bdf_filename_alist,
74e1aeec 8652 doc: /* List of bdf fonts and their corresponding filenames. */);
33d52f9c
GV
8653 Vw32_bdf_filename_alist = Qnil;
8654
1075afa9
GV
8655 DEFVAR_BOOL ("w32-strict-fontnames",
8656 &w32_strict_fontnames,
74e1aeec
JR
8657 doc: /* Non-nil means only use fonts that are exact matches for those requested.
8658Default is nil, which allows old fontnames that are not XLFD compliant,
8659and allows third-party CJK display to work by specifying false charset
8660fields to trick Emacs into translating to Big5, SJIS etc.
8661Setting this to t will prevent wrong fonts being selected when
8662fontsets are automatically created. */);
1075afa9
GV
8663 w32_strict_fontnames = 0;
8664
c0611964
AI
8665 DEFVAR_BOOL ("w32-strict-painting",
8666 &w32_strict_painting,
74e1aeec
JR
8667 doc: /* Non-nil means use strict rules for repainting frames.
8668Set this to nil to get the old behaviour for repainting; this should
8669only be necessary if the default setting causes problems. */);
c0611964
AI
8670 w32_strict_painting = 1;
8671
dfff8a69
JR
8672 DEFVAR_LISP ("w32-charset-info-alist",
8673 &Vw32_charset_info_alist,
b3700ae7
JR
8674 doc: /* Alist linking Emacs character sets to Windows fonts and codepages.
8675Each entry should be of the form:
74e1aeec
JR
8676
8677 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE))
8678
8679where CHARSET_NAME is a string used in font names to identify the charset,
8680WINDOWS_CHARSET is a symbol that can be one of:
8681w32-charset-ansi, w32-charset-default, w32-charset-symbol,
8682w32-charset-shiftjis, w32-charset-hangeul, w32-charset-gb2312,
8683w32-charset-chinesebig5,
74e1aeec
JR
8684w32-charset-johab, w32-charset-hebrew,
8685w32-charset-arabic, w32-charset-greek, w32-charset-turkish,
8686w32-charset-vietnamese, w32-charset-thai, w32-charset-easteurope,
8687w32-charset-russian, w32-charset-mac, w32-charset-baltic,
74e1aeec 8688w32-charset-unicode,
74e1aeec
JR
8689or w32-charset-oem.
8690CODEPAGE should be an integer specifying the codepage that should be used
8691to display the character set, t to do no translation and output as Unicode,
8692or nil to do no translation and output as 8 bit (or multibyte on far-east
8693versions of Windows) characters. */);
dfff8a69
JR
8694 Vw32_charset_info_alist = Qnil;
8695
8696 staticpro (&Qw32_charset_ansi);
8697 Qw32_charset_ansi = intern ("w32-charset-ansi");
8698 staticpro (&Qw32_charset_symbol);
8699 Qw32_charset_symbol = intern ("w32-charset-symbol");
8700 staticpro (&Qw32_charset_shiftjis);
8701 Qw32_charset_shiftjis = intern ("w32-charset-shiftjis");
767b1ff0
JR
8702 staticpro (&Qw32_charset_hangeul);
8703 Qw32_charset_hangeul = intern ("w32-charset-hangeul");
dfff8a69
JR
8704 staticpro (&Qw32_charset_chinesebig5);
8705 Qw32_charset_chinesebig5 = intern ("w32-charset-chinesebig5");
8706 staticpro (&Qw32_charset_gb2312);
8707 Qw32_charset_gb2312 = intern ("w32-charset-gb2312");
8708 staticpro (&Qw32_charset_oem);
8709 Qw32_charset_oem = intern ("w32-charset-oem");
8710
8711#ifdef JOHAB_CHARSET
8712 {
8713 static int w32_extra_charsets_defined = 1;
74e1aeec
JR
8714 DEFVAR_BOOL ("w32-extra-charsets-defined", &w32_extra_charsets_defined,
8715 doc: /* Internal variable. */);
dfff8a69
JR
8716
8717 staticpro (&Qw32_charset_johab);
8718 Qw32_charset_johab = intern ("w32-charset-johab");
8719 staticpro (&Qw32_charset_easteurope);
8720 Qw32_charset_easteurope = intern ("w32-charset-easteurope");
8721 staticpro (&Qw32_charset_turkish);
8722 Qw32_charset_turkish = intern ("w32-charset-turkish");
8723 staticpro (&Qw32_charset_baltic);
8724 Qw32_charset_baltic = intern ("w32-charset-baltic");
8725 staticpro (&Qw32_charset_russian);
8726 Qw32_charset_russian = intern ("w32-charset-russian");
8727 staticpro (&Qw32_charset_arabic);
8728 Qw32_charset_arabic = intern ("w32-charset-arabic");
8729 staticpro (&Qw32_charset_greek);
8730 Qw32_charset_greek = intern ("w32-charset-greek");
8731 staticpro (&Qw32_charset_hebrew);
8732 Qw32_charset_hebrew = intern ("w32-charset-hebrew");
767b1ff0
JR
8733 staticpro (&Qw32_charset_vietnamese);
8734 Qw32_charset_vietnamese = intern ("w32-charset-vietnamese");
dfff8a69
JR
8735 staticpro (&Qw32_charset_thai);
8736 Qw32_charset_thai = intern ("w32-charset-thai");
8737 staticpro (&Qw32_charset_mac);
8738 Qw32_charset_mac = intern ("w32-charset-mac");
8739 }
8740#endif
8741
8742#ifdef UNICODE_CHARSET
8743 {
8744 static int w32_unicode_charset_defined = 1;
8745 DEFVAR_BOOL ("w32-unicode-charset-defined",
74e1aeec
JR
8746 &w32_unicode_charset_defined,
8747 doc: /* Internal variable. */);
dfff8a69
JR
8748
8749 staticpro (&Qw32_charset_unicode);
8750 Qw32_charset_unicode = intern ("w32-charset-unicode");
8751#endif
8752
767b1ff0 8753#if 0 /* TODO: Port to W32 */
6fc2811b
JR
8754 defsubr (&Sx_change_window_property);
8755 defsubr (&Sx_delete_window_property);
8756 defsubr (&Sx_window_property);
8757#endif
2d764c78 8758 defsubr (&Sxw_display_color_p);
ee78dc32 8759 defsubr (&Sx_display_grayscale_p);
2d764c78
EZ
8760 defsubr (&Sxw_color_defined_p);
8761 defsubr (&Sxw_color_values);
ee78dc32
GV
8762 defsubr (&Sx_server_max_request_size);
8763 defsubr (&Sx_server_vendor);
8764 defsubr (&Sx_server_version);
8765 defsubr (&Sx_display_pixel_width);
8766 defsubr (&Sx_display_pixel_height);
8767 defsubr (&Sx_display_mm_width);
8768 defsubr (&Sx_display_mm_height);
8769 defsubr (&Sx_display_screens);
8770 defsubr (&Sx_display_planes);
8771 defsubr (&Sx_display_color_cells);
8772 defsubr (&Sx_display_visual_class);
8773 defsubr (&Sx_display_backing_store);
8774 defsubr (&Sx_display_save_under);
ee78dc32 8775 defsubr (&Sx_create_frame);
ee78dc32
GV
8776 defsubr (&Sx_open_connection);
8777 defsubr (&Sx_close_connection);
8778 defsubr (&Sx_display_list);
8779 defsubr (&Sx_synchronize);
8780
fbd6baed 8781 /* W32 specific functions */
ee78dc32 8782
1edf84e7 8783 defsubr (&Sw32_focus_frame);
fbd6baed
GV
8784 defsubr (&Sw32_select_font);
8785 defsubr (&Sw32_define_rgb_color);
8786 defsubr (&Sw32_default_color_map);
8787 defsubr (&Sw32_load_color_file);
1edf84e7 8788 defsubr (&Sw32_send_sys_command);
55dcfc15 8789 defsubr (&Sw32_shell_execute);
ccc2d29c
GV
8790 defsubr (&Sw32_register_hot_key);
8791 defsubr (&Sw32_unregister_hot_key);
8792 defsubr (&Sw32_registered_hot_keys);
8793 defsubr (&Sw32_reconstruct_hot_key);
adcc3809 8794 defsubr (&Sw32_toggle_lock_key);
33d52f9c 8795 defsubr (&Sw32_find_bdf_fonts);
4587b026 8796
2254bcde 8797 defsubr (&Sfile_system_info);
39a0e135 8798 defsubr (&Sdefault_printer_name);
2254bcde 8799
4587b026
GV
8800 /* Setting callback functions for fontset handler. */
8801 get_font_info_func = w32_get_font_info;
6fc2811b
JR
8802
8803#if 0 /* This function pointer doesn't seem to be used anywhere.
8804 And the pointer assigned has the wrong type, anyway. */
4587b026 8805 list_fonts_func = w32_list_fonts;
6fc2811b
JR
8806#endif
8807
4587b026
GV
8808 load_font_func = w32_load_font;
8809 find_ccl_program_func = w32_find_ccl_program;
8810 query_font_func = w32_query_font;
8811 set_frame_fontset_func = x_set_font;
8812 check_window_system_func = check_w32;
6fc2811b 8813
6fc2811b 8814
0af913d7
GM
8815 hourglass_atimer = NULL;
8816 hourglass_shown_p = 0;
6fc2811b
JR
8817 defsubr (&Sx_show_tip);
8818 defsubr (&Sx_hide_tip);
6fc2811b 8819 tip_timer = Qnil;
57fa2774
JR
8820 staticpro (&tip_timer);
8821 tip_frame = Qnil;
8822 staticpro (&tip_frame);
6fc2811b 8823
ca56d953
JR
8824 last_show_tip_args = Qnil;
8825 staticpro (&last_show_tip_args);
8826
6fc2811b
JR
8827 defsubr (&Sx_file_dialog);
8828}
8829
c922a224 8830
9785d95b
BK
8831/*
8832 globals_of_w32fns is used to initialize those global variables that
8833 must always be initialized on startup even when the global variable
8834 initialized is non zero (see the function main in emacs.c).
8835 globals_of_w32fns is called from syms_of_w32fns when the global
8836 variable initialized is 0 and directly from main when initialized
8837 is non zero.
8838 */
8839void globals_of_w32fns ()
8840{
8841 HMODULE user32_lib = GetModuleHandle ("user32.dll");
ccc0fdaa
JR
8842 /*
8843 TrackMouseEvent not available in all versions of Windows, so must load
8844 it dynamically. Do it once, here, instead of every time it is used.
9785d95b 8845 */
ccc0fdaa
JR
8846 track_mouse_event_fn = (TrackMouseEvent_Proc)
8847 GetProcAddress (user32_lib, "TrackMouseEvent");
8848 /* ditto for GetClipboardSequenceNumber. */
8849 clipboard_sequence_fn = (ClipboardSequence_Proc)
8850 GetProcAddress (user32_lib, "GetClipboardSequenceNumber");
4bf91535
JR
8851
8852 DEFVAR_INT ("w32-ansi-code-page",
2ba49441 8853 &w32_ansi_code_page,
4bf91535 8854 doc: /* The ANSI code page used by the system. */);
2ba49441 8855 w32_ansi_code_page = GetACP ();
9785d95b 8856}
6fc2811b 8857
ee78dc32
GV
8858#undef abort
8859
7d0393cf 8860void
fbd6baed 8861w32_abort()
ee78dc32 8862{
5ac45f98
GV
8863 int button;
8864 button = MessageBox (NULL,
8865 "A fatal error has occurred!\n\n"
8866 "Select Abort to exit, Retry to debug, Ignore to continue",
8867 "Emacs Abort Dialog",
8868 MB_ICONEXCLAMATION | MB_TASKMODAL
8869 | MB_SETFOREGROUND | MB_ABORTRETRYIGNORE);
8870 switch (button)
8871 {
8872 case IDRETRY:
8873 DebugBreak ();
8874 break;
8875 case IDIGNORE:
8876 break;
8877 case IDABORT:
8878 default:
8879 abort ();
8880 break;
8881 }
ee78dc32 8882}
d573caac 8883
83c75055
GV
8884/* For convenience when debugging. */
8885int
8886w32_last_error()
8887{
8888 return GetLastError ();
8889}
ab5796a9
MB
8890
8891/* arch-tag: 707589ab-b9be-4638-8cdd-74629cc9b446
8892 (do not change this comment) */