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