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