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