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