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