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