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