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