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