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