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 Lisp_Object ifa;
2085
2086 w32_init_class (hinst);
2087
2088 /* Handle the -geometry command line option and the geometry
2089 settings in the registry. They are decoded and put into
2090 initial-frame-alist by w32-win.el:x-handle-geometry. */
2091 ifa = Fsymbol_value (intern ("initial-frame-alist"));
2092 if (CONSP (ifa))
2093 {
2094 Lisp_Object lt = Fassq (Qleft, ifa);
2095 Lisp_Object tp = Fassq (Qtop, ifa);
2096
2097 if (!NILP (lt))
2098 {
2099 lt = XCDR (lt);
2100 if (INTEGERP (lt))
2101 left = lt;
2102 }
2103 if (!NILP (tp))
2104 {
2105 tp = XCDR (tp);
2106 if (INTEGERP (tp))
2107 top = tp;
2108 }
2109 }
2110 }
2111
2112 if (EQ (left, Qunbound) && EQ (top, Qunbound))
2113 {
2114 /* When called with RES_TYPE_NUMBER, w32_get_arg will return zero
2115 for anything that is not a number and is not Qunbound. */
2116 left = w32_get_arg (Qnil, Qleft, "left", "Left", RES_TYPE_NUMBER);
2117 top = w32_get_arg (Qnil, Qtop, "top", "Top", RES_TYPE_NUMBER);
2118 }
2119
2120 FRAME_W32_WINDOW (f) = hwnd
2121 = CreateWindow (EMACS_CLASS,
2122 f->namebuf,
2123 f->output_data.w32->dwStyle | WS_CLIPCHILDREN,
2124 EQ (left, Qunbound) ? CW_USEDEFAULT : XINT (left),
2125 EQ (top, Qunbound) ? CW_USEDEFAULT : XINT (top),
2126 rect.right - rect.left,
2127 rect.bottom - rect.top,
2128 NULL,
2129 NULL,
2130 hinst,
2131 NULL);
2132
2133 if (hwnd)
2134 {
2135 SetWindowLong (hwnd, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
2136 SetWindowLong (hwnd, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
2137 SetWindowLong (hwnd, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
2138 SetWindowLong (hwnd, WND_SCROLLBAR_INDEX, f->scroll_bar_actual_width);
2139 SetWindowLong (hwnd, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
2140
2141 /* Enable drag-n-drop. */
2142 DragAcceptFiles (hwnd, TRUE);
2143
2144 /* Do this to discard the default setting specified by our parent. */
2145 ShowWindow (hwnd, SW_HIDE);
2146
2147 /* Update frame positions. */
2148 GetWindowRect (hwnd, &rect);
2149 f->left_pos = rect.left;
2150 f->top_pos = rect.top;
2151 }
2152 }
2153
2154 void
2155 my_post_msg (wmsg, hwnd, msg, wParam, lParam)
2156 W32Msg * wmsg;
2157 HWND hwnd;
2158 UINT msg;
2159 WPARAM wParam;
2160 LPARAM lParam;
2161 {
2162 wmsg->msg.hwnd = hwnd;
2163 wmsg->msg.message = msg;
2164 wmsg->msg.wParam = wParam;
2165 wmsg->msg.lParam = lParam;
2166 wmsg->msg.time = GetMessageTime ();
2167
2168 post_msg (wmsg);
2169 }
2170
2171 /* GetKeyState and MapVirtualKey on Windows 95 do not actually distinguish
2172 between left and right keys as advertised. We test for this
2173 support dynamically, and set a flag when the support is absent. If
2174 absent, we keep track of the left and right control and alt keys
2175 ourselves. This is particularly necessary on keyboards that rely
2176 upon the AltGr key, which is represented as having the left control
2177 and right alt keys pressed. For these keyboards, we need to know
2178 when the left alt key has been pressed in addition to the AltGr key
2179 so that we can properly support M-AltGr-key sequences (such as M-@
2180 on Swedish keyboards). */
2181
2182 #define EMACS_LCONTROL 0
2183 #define EMACS_RCONTROL 1
2184 #define EMACS_LMENU 2
2185 #define EMACS_RMENU 3
2186
2187 static int modifiers[4];
2188 static int modifiers_recorded;
2189 static int modifier_key_support_tested;
2190
2191 static void
2192 test_modifier_support (unsigned int wparam)
2193 {
2194 unsigned int l, r;
2195
2196 if (wparam != VK_CONTROL && wparam != VK_MENU)
2197 return;
2198 if (wparam == VK_CONTROL)
2199 {
2200 l = VK_LCONTROL;
2201 r = VK_RCONTROL;
2202 }
2203 else
2204 {
2205 l = VK_LMENU;
2206 r = VK_RMENU;
2207 }
2208 if (!(GetKeyState (l) & 0x8000) && !(GetKeyState (r) & 0x8000))
2209 modifiers_recorded = 1;
2210 else
2211 modifiers_recorded = 0;
2212 modifier_key_support_tested = 1;
2213 }
2214
2215 static void
2216 record_keydown (unsigned int wparam, unsigned int lparam)
2217 {
2218 int i;
2219
2220 if (!modifier_key_support_tested)
2221 test_modifier_support (wparam);
2222
2223 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
2224 return;
2225
2226 if (wparam == VK_CONTROL)
2227 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
2228 else
2229 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
2230
2231 modifiers[i] = 1;
2232 }
2233
2234 static void
2235 record_keyup (unsigned int wparam, unsigned int lparam)
2236 {
2237 int i;
2238
2239 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
2240 return;
2241
2242 if (wparam == VK_CONTROL)
2243 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
2244 else
2245 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
2246
2247 modifiers[i] = 0;
2248 }
2249
2250 /* Emacs can lose focus while a modifier key has been pressed. When
2251 it regains focus, be conservative and clear all modifiers since
2252 we cannot reconstruct the left and right modifier state. */
2253 static void
2254 reset_modifiers ()
2255 {
2256 SHORT ctrl, alt;
2257
2258 if (GetFocus () == NULL)
2259 /* Emacs doesn't have keyboard focus. Do nothing. */
2260 return;
2261
2262 ctrl = GetAsyncKeyState (VK_CONTROL);
2263 alt = GetAsyncKeyState (VK_MENU);
2264
2265 if (!(ctrl & 0x08000))
2266 /* Clear any recorded control modifier state. */
2267 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
2268
2269 if (!(alt & 0x08000))
2270 /* Clear any recorded alt modifier state. */
2271 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
2272
2273 /* Update the state of all modifier keys, because modifiers used in
2274 hot-key combinations can get stuck on if Emacs loses focus as a
2275 result of a hot-key being pressed. */
2276 {
2277 BYTE keystate[256];
2278
2279 #define CURRENT_STATE(key) ((GetAsyncKeyState (key) & 0x8000) >> 8)
2280
2281 GetKeyboardState (keystate);
2282 keystate[VK_SHIFT] = CURRENT_STATE (VK_SHIFT);
2283 keystate[VK_CONTROL] = CURRENT_STATE (VK_CONTROL);
2284 keystate[VK_LCONTROL] = CURRENT_STATE (VK_LCONTROL);
2285 keystate[VK_RCONTROL] = CURRENT_STATE (VK_RCONTROL);
2286 keystate[VK_MENU] = CURRENT_STATE (VK_MENU);
2287 keystate[VK_LMENU] = CURRENT_STATE (VK_LMENU);
2288 keystate[VK_RMENU] = CURRENT_STATE (VK_RMENU);
2289 keystate[VK_LWIN] = CURRENT_STATE (VK_LWIN);
2290 keystate[VK_RWIN] = CURRENT_STATE (VK_RWIN);
2291 keystate[VK_APPS] = CURRENT_STATE (VK_APPS);
2292 SetKeyboardState (keystate);
2293 }
2294 }
2295
2296 /* Synchronize modifier state with what is reported with the current
2297 keystroke. Even if we cannot distinguish between left and right
2298 modifier keys, we know that, if no modifiers are set, then neither
2299 the left or right modifier should be set. */
2300 static void
2301 sync_modifiers ()
2302 {
2303 if (!modifiers_recorded)
2304 return;
2305
2306 if (!(GetKeyState (VK_CONTROL) & 0x8000))
2307 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
2308
2309 if (!(GetKeyState (VK_MENU) & 0x8000))
2310 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
2311 }
2312
2313 static int
2314 modifier_set (int vkey)
2315 {
2316 if (vkey == VK_CAPITAL || vkey == VK_SCROLL)
2317 return (GetKeyState (vkey) & 0x1);
2318 if (!modifiers_recorded)
2319 return (GetKeyState (vkey) & 0x8000);
2320
2321 switch (vkey)
2322 {
2323 case VK_LCONTROL:
2324 return modifiers[EMACS_LCONTROL];
2325 case VK_RCONTROL:
2326 return modifiers[EMACS_RCONTROL];
2327 case VK_LMENU:
2328 return modifiers[EMACS_LMENU];
2329 case VK_RMENU:
2330 return modifiers[EMACS_RMENU];
2331 }
2332 return (GetKeyState (vkey) & 0x8000);
2333 }
2334
2335 /* Convert between the modifier bits W32 uses and the modifier bits
2336 Emacs uses. */
2337
2338 unsigned int
2339 w32_key_to_modifier (int key)
2340 {
2341 Lisp_Object key_mapping;
2342
2343 switch (key)
2344 {
2345 case VK_LWIN:
2346 key_mapping = Vw32_lwindow_modifier;
2347 break;
2348 case VK_RWIN:
2349 key_mapping = Vw32_rwindow_modifier;
2350 break;
2351 case VK_APPS:
2352 key_mapping = Vw32_apps_modifier;
2353 break;
2354 case VK_SCROLL:
2355 key_mapping = Vw32_scroll_lock_modifier;
2356 break;
2357 default:
2358 key_mapping = Qnil;
2359 }
2360
2361 /* NB. This code runs in the input thread, asychronously to the lisp
2362 thread, so we must be careful to ensure access to lisp data is
2363 thread-safe. The following code is safe because the modifier
2364 variable values are updated atomically from lisp and symbols are
2365 not relocated by GC. Also, we don't have to worry about seeing GC
2366 markbits here. */
2367 if (EQ (key_mapping, Qhyper))
2368 return hyper_modifier;
2369 if (EQ (key_mapping, Qsuper))
2370 return super_modifier;
2371 if (EQ (key_mapping, Qmeta))
2372 return meta_modifier;
2373 if (EQ (key_mapping, Qalt))
2374 return alt_modifier;
2375 if (EQ (key_mapping, Qctrl))
2376 return ctrl_modifier;
2377 if (EQ (key_mapping, Qcontrol)) /* synonym for ctrl */
2378 return ctrl_modifier;
2379 if (EQ (key_mapping, Qshift))
2380 return shift_modifier;
2381
2382 /* Don't generate any modifier if not explicitly requested. */
2383 return 0;
2384 }
2385
2386 unsigned int
2387 w32_get_modifiers ()
2388 {
2389 return ((modifier_set (VK_SHIFT) ? shift_modifier : 0) |
2390 (modifier_set (VK_CONTROL) ? ctrl_modifier : 0) |
2391 (modifier_set (VK_LWIN) ? w32_key_to_modifier (VK_LWIN) : 0) |
2392 (modifier_set (VK_RWIN) ? w32_key_to_modifier (VK_RWIN) : 0) |
2393 (modifier_set (VK_APPS) ? w32_key_to_modifier (VK_APPS) : 0) |
2394 (modifier_set (VK_SCROLL) ? w32_key_to_modifier (VK_SCROLL) : 0) |
2395 (modifier_set (VK_MENU) ?
2396 ((NILP (Vw32_alt_is_meta)) ? alt_modifier : meta_modifier) : 0));
2397 }
2398
2399 /* We map the VK_* modifiers into console modifier constants
2400 so that we can use the same routines to handle both console
2401 and window input. */
2402
2403 static int
2404 construct_console_modifiers ()
2405 {
2406 int mods;
2407
2408 mods = 0;
2409 mods |= (modifier_set (VK_SHIFT)) ? SHIFT_PRESSED : 0;
2410 mods |= (modifier_set (VK_CAPITAL)) ? CAPSLOCK_ON : 0;
2411 mods |= (modifier_set (VK_SCROLL)) ? SCROLLLOCK_ON : 0;
2412 mods |= (modifier_set (VK_NUMLOCK)) ? NUMLOCK_ON : 0;
2413 mods |= (modifier_set (VK_LCONTROL)) ? LEFT_CTRL_PRESSED : 0;
2414 mods |= (modifier_set (VK_RCONTROL)) ? RIGHT_CTRL_PRESSED : 0;
2415 mods |= (modifier_set (VK_LMENU)) ? LEFT_ALT_PRESSED : 0;
2416 mods |= (modifier_set (VK_RMENU)) ? RIGHT_ALT_PRESSED : 0;
2417 mods |= (modifier_set (VK_LWIN)) ? LEFT_WIN_PRESSED : 0;
2418 mods |= (modifier_set (VK_RWIN)) ? RIGHT_WIN_PRESSED : 0;
2419 mods |= (modifier_set (VK_APPS)) ? APPS_PRESSED : 0;
2420
2421 return mods;
2422 }
2423
2424 static int
2425 w32_get_key_modifiers (unsigned int wparam, unsigned int lparam)
2426 {
2427 int mods;
2428
2429 /* Convert to emacs modifiers. */
2430 mods = w32_kbd_mods_to_emacs (construct_console_modifiers (), wparam);
2431
2432 return mods;
2433 }
2434
2435 unsigned int
2436 map_keypad_keys (unsigned int virt_key, unsigned int extended)
2437 {
2438 if (virt_key < VK_CLEAR || virt_key > VK_DELETE)
2439 return virt_key;
2440
2441 if (virt_key == VK_RETURN)
2442 return (extended ? VK_NUMPAD_ENTER : VK_RETURN);
2443
2444 if (virt_key >= VK_PRIOR && virt_key <= VK_DOWN)
2445 return (!extended ? (VK_NUMPAD_PRIOR + (virt_key - VK_PRIOR)) : virt_key);
2446
2447 if (virt_key == VK_INSERT || virt_key == VK_DELETE)
2448 return (!extended ? (VK_NUMPAD_INSERT + (virt_key - VK_INSERT)) : virt_key);
2449
2450 if (virt_key == VK_CLEAR)
2451 return (!extended ? VK_NUMPAD_CLEAR : virt_key);
2452
2453 return virt_key;
2454 }
2455
2456 /* List of special key combinations which w32 would normally capture,
2457 but emacs should grab instead. Not directly visible to lisp, to
2458 simplify synchronization. Each item is an integer encoding a virtual
2459 key code and modifier combination to capture. */
2460 Lisp_Object w32_grabbed_keys;
2461
2462 #define HOTKEY(vk,mods) make_number (((vk) & 255) | ((mods) << 8))
2463 #define HOTKEY_ID(k) (XFASTINT (k) & 0xbfff)
2464 #define HOTKEY_VK_CODE(k) (XFASTINT (k) & 255)
2465 #define HOTKEY_MODIFIERS(k) (XFASTINT (k) >> 8)
2466
2467 #define RAW_HOTKEY_ID(k) ((k) & 0xbfff)
2468 #define RAW_HOTKEY_VK_CODE(k) ((k) & 255)
2469 #define RAW_HOTKEY_MODIFIERS(k) ((k) >> 8)
2470
2471 /* Register hot-keys for reserved key combinations when Emacs has
2472 keyboard focus, since this is the only way Emacs can receive key
2473 combinations like Alt-Tab which are used by the system. */
2474
2475 static void
2476 register_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 /* Deleted entries get set to nil. */
2487 if (!INTEGERP (key))
2488 continue;
2489
2490 RegisterHotKey (hwnd, HOTKEY_ID (key),
2491 HOTKEY_MODIFIERS (key), HOTKEY_VK_CODE (key));
2492 }
2493 }
2494
2495 static void
2496 unregister_hot_keys (hwnd)
2497 HWND hwnd;
2498 {
2499 Lisp_Object keylist;
2500
2501 /* Use GC_CONSP, since we are called asynchronously. */
2502 for (keylist = w32_grabbed_keys; GC_CONSP (keylist); keylist = XCDR (keylist))
2503 {
2504 Lisp_Object key = XCAR (keylist);
2505
2506 if (!INTEGERP (key))
2507 continue;
2508
2509 UnregisterHotKey (hwnd, HOTKEY_ID (key));
2510 }
2511 }
2512
2513 /* Main message dispatch loop. */
2514
2515 static void
2516 w32_msg_pump (deferred_msg * msg_buf)
2517 {
2518 MSG msg;
2519 int result;
2520 HWND focus_window;
2521
2522 msh_mousewheel = RegisterWindowMessage (MSH_MOUSEWHEEL);
2523
2524 while (GetMessage (&msg, NULL, 0, 0))
2525 {
2526 if (msg.hwnd == NULL)
2527 {
2528 switch (msg.message)
2529 {
2530 case WM_NULL:
2531 /* Produced by complete_deferred_msg; just ignore. */
2532 break;
2533 case WM_EMACS_CREATEWINDOW:
2534 w32_createwindow ((struct frame *) msg.wParam);
2535 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2536 abort ();
2537 break;
2538 case WM_EMACS_SETLOCALE:
2539 SetThreadLocale (msg.wParam);
2540 /* Reply is not expected. */
2541 break;
2542 case WM_EMACS_SETKEYBOARDLAYOUT:
2543 result = (int) ActivateKeyboardLayout ((HKL) msg.wParam, 0);
2544 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
2545 result, 0))
2546 abort ();
2547 break;
2548 case WM_EMACS_REGISTER_HOT_KEY:
2549 focus_window = GetFocus ();
2550 if (focus_window != NULL)
2551 RegisterHotKey (focus_window,
2552 RAW_HOTKEY_ID (msg.wParam),
2553 RAW_HOTKEY_MODIFIERS (msg.wParam),
2554 RAW_HOTKEY_VK_CODE (msg.wParam));
2555 /* Reply is not expected. */
2556 break;
2557 case WM_EMACS_UNREGISTER_HOT_KEY:
2558 focus_window = GetFocus ();
2559 if (focus_window != NULL)
2560 UnregisterHotKey (focus_window, RAW_HOTKEY_ID (msg.wParam));
2561 /* Mark item as erased. NB: this code must be
2562 thread-safe. The next line is okay because the cons
2563 cell is never made into garbage and is not relocated by
2564 GC. */
2565 XSETCAR ((Lisp_Object) ((EMACS_INT) msg.lParam), Qnil);
2566 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2567 abort ();
2568 break;
2569 case WM_EMACS_TOGGLE_LOCK_KEY:
2570 {
2571 int vk_code = (int) msg.wParam;
2572 int cur_state = (GetKeyState (vk_code) & 1);
2573 Lisp_Object new_state = (Lisp_Object) ((EMACS_INT) msg.lParam);
2574
2575 /* NB: This code must be thread-safe. It is safe to
2576 call NILP because symbols are not relocated by GC,
2577 and pointer here is not touched by GC (so the markbit
2578 can't be set). Numbers are safe because they are
2579 immediate values. */
2580 if (NILP (new_state)
2581 || (NUMBERP (new_state)
2582 && ((XUINT (new_state)) & 1) != cur_state))
2583 {
2584 one_w32_display_info.faked_key = vk_code;
2585
2586 keybd_event ((BYTE) vk_code,
2587 (BYTE) MapVirtualKey (vk_code, 0),
2588 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2589 keybd_event ((BYTE) vk_code,
2590 (BYTE) MapVirtualKey (vk_code, 0),
2591 KEYEVENTF_EXTENDEDKEY | 0, 0);
2592 keybd_event ((BYTE) vk_code,
2593 (BYTE) MapVirtualKey (vk_code, 0),
2594 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2595 cur_state = !cur_state;
2596 }
2597 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
2598 cur_state, 0))
2599 abort ();
2600 }
2601 break;
2602 default:
2603 DebPrint (("msg %x not expected by w32_msg_pump\n", msg.message));
2604 }
2605 }
2606 else
2607 {
2608 DispatchMessage (&msg);
2609 }
2610
2611 /* Exit nested loop when our deferred message has completed. */
2612 if (msg_buf->completed)
2613 break;
2614 }
2615 }
2616
2617 deferred_msg * deferred_msg_head;
2618
2619 static deferred_msg *
2620 find_deferred_msg (HWND hwnd, UINT msg)
2621 {
2622 deferred_msg * item;
2623
2624 /* Don't actually need synchronization for read access, since
2625 modification of single pointer is always atomic. */
2626 /* enter_crit (); */
2627
2628 for (item = deferred_msg_head; item != NULL; item = item->next)
2629 if (item->w32msg.msg.hwnd == hwnd
2630 && item->w32msg.msg.message == msg)
2631 break;
2632
2633 /* leave_crit (); */
2634
2635 return item;
2636 }
2637
2638 static LRESULT
2639 send_deferred_msg (deferred_msg * msg_buf,
2640 HWND hwnd,
2641 UINT msg,
2642 WPARAM wParam,
2643 LPARAM lParam)
2644 {
2645 /* Only input thread can send deferred messages. */
2646 if (GetCurrentThreadId () != dwWindowsThreadId)
2647 abort ();
2648
2649 /* It is an error to send a message that is already deferred. */
2650 if (find_deferred_msg (hwnd, msg) != NULL)
2651 abort ();
2652
2653 /* Enforced synchronization is not needed because this is the only
2654 function that alters deferred_msg_head, and the following critical
2655 section is guaranteed to only be serially reentered (since only the
2656 input thread can call us). */
2657
2658 /* enter_crit (); */
2659
2660 msg_buf->completed = 0;
2661 msg_buf->next = deferred_msg_head;
2662 deferred_msg_head = msg_buf;
2663 my_post_msg (&msg_buf->w32msg, hwnd, msg, wParam, lParam);
2664
2665 /* leave_crit (); */
2666
2667 /* Start a new nested message loop to process other messages until
2668 this one is completed. */
2669 w32_msg_pump (msg_buf);
2670
2671 deferred_msg_head = msg_buf->next;
2672
2673 return msg_buf->result;
2674 }
2675
2676 void
2677 complete_deferred_msg (HWND hwnd, UINT msg, LRESULT result)
2678 {
2679 deferred_msg * msg_buf = find_deferred_msg (hwnd, msg);
2680
2681 if (msg_buf == NULL)
2682 /* Message may have been cancelled, so don't abort(). */
2683 return;
2684
2685 msg_buf->result = result;
2686 msg_buf->completed = 1;
2687
2688 /* Ensure input thread is woken so it notices the completion. */
2689 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
2690 }
2691
2692 void
2693 cancel_all_deferred_msgs ()
2694 {
2695 deferred_msg * item;
2696
2697 /* Don't actually need synchronization for read access, since
2698 modification of single pointer is always atomic. */
2699 /* enter_crit (); */
2700
2701 for (item = deferred_msg_head; item != NULL; item = item->next)
2702 {
2703 item->result = 0;
2704 item->completed = 1;
2705 }
2706
2707 /* leave_crit (); */
2708
2709 /* Ensure input thread is woken so it notices the completion. */
2710 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
2711 }
2712
2713 DWORD
2714 w32_msg_worker (dw)
2715 DWORD dw;
2716 {
2717 MSG msg;
2718 deferred_msg dummy_buf;
2719
2720 /* Ensure our message queue is created */
2721
2722 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
2723
2724 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2725 abort ();
2726
2727 memset (&dummy_buf, 0, sizeof (dummy_buf));
2728 dummy_buf.w32msg.msg.hwnd = NULL;
2729 dummy_buf.w32msg.msg.message = WM_NULL;
2730
2731 /* This is the inital message loop which should only exit when the
2732 application quits. */
2733 w32_msg_pump (&dummy_buf);
2734
2735 return 0;
2736 }
2737
2738 static void
2739 signal_user_input ()
2740 {
2741 /* Interrupt any lisp that wants to be interrupted by input. */
2742 if (!NILP (Vthrow_on_input))
2743 {
2744 Vquit_flag = Vthrow_on_input;
2745 /* If we're inside a function that wants immediate quits,
2746 do it now. */
2747 if (immediate_quit && NILP (Vinhibit_quit))
2748 {
2749 immediate_quit = 0;
2750 QUIT;
2751 }
2752 }
2753 }
2754
2755
2756 static void
2757 post_character_message (hwnd, msg, wParam, lParam, modifiers)
2758 HWND hwnd;
2759 UINT msg;
2760 WPARAM wParam;
2761 LPARAM lParam;
2762 DWORD modifiers;
2763
2764 {
2765 W32Msg wmsg;
2766
2767 wmsg.dwModifiers = modifiers;
2768
2769 /* Detect quit_char and set quit-flag directly. Note that we
2770 still need to post a message to ensure the main thread will be
2771 woken up if blocked in sys_select(), but we do NOT want to post
2772 the quit_char message itself (because it will usually be as if
2773 the user had typed quit_char twice). Instead, we post a dummy
2774 message that has no particular effect. */
2775 {
2776 int c = wParam;
2777 if (isalpha (c) && wmsg.dwModifiers == ctrl_modifier)
2778 c = make_ctrl_char (c) & 0377;
2779 if (c == quit_char
2780 || (wmsg.dwModifiers == 0 &&
2781 w32_quit_key && wParam == w32_quit_key))
2782 {
2783 Vquit_flag = Qt;
2784
2785 /* The choice of message is somewhat arbitrary, as long as
2786 the main thread handler just ignores it. */
2787 msg = WM_NULL;
2788
2789 /* Interrupt any blocking system calls. */
2790 signal_quit ();
2791
2792 /* As a safety precaution, forcibly complete any deferred
2793 messages. This is a kludge, but I don't see any particularly
2794 clean way to handle the situation where a deferred message is
2795 "dropped" in the lisp thread, and will thus never be
2796 completed, eg. by the user trying to activate the menubar
2797 when the lisp thread is busy, and then typing C-g when the
2798 menubar doesn't open promptly (with the result that the
2799 menubar never responds at all because the deferred
2800 WM_INITMENU message is never completed). Another problem
2801 situation is when the lisp thread calls SendMessage (to send
2802 a window manager command) when a message has been deferred;
2803 the lisp thread gets blocked indefinitely waiting for the
2804 deferred message to be completed, which itself is waiting for
2805 the lisp thread to respond.
2806
2807 Note that we don't want to block the input thread waiting for
2808 a reponse from the lisp thread (although that would at least
2809 solve the deadlock problem above), because we want to be able
2810 to receive C-g to interrupt the lisp thread. */
2811 cancel_all_deferred_msgs ();
2812 }
2813 else
2814 signal_user_input ();
2815 }
2816
2817 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2818 }
2819
2820 /* Main window procedure */
2821
2822 LRESULT CALLBACK
2823 w32_wnd_proc (hwnd, msg, wParam, lParam)
2824 HWND hwnd;
2825 UINT msg;
2826 WPARAM wParam;
2827 LPARAM lParam;
2828 {
2829 struct frame *f;
2830 struct w32_display_info *dpyinfo = &one_w32_display_info;
2831 W32Msg wmsg;
2832 int windows_translate;
2833 int key;
2834
2835 /* Note that it is okay to call x_window_to_frame, even though we are
2836 not running in the main lisp thread, because frame deletion
2837 requires the lisp thread to synchronize with this thread. Thus, if
2838 a frame struct is returned, it can be used without concern that the
2839 lisp thread might make it disappear while we are using it.
2840
2841 NB. Walking the frame list in this thread is safe (as long as
2842 writes of Lisp_Object slots are atomic, which they are on Windows).
2843 Although delete-frame can destructively modify the frame list while
2844 we are walking it, a garbage collection cannot occur until after
2845 delete-frame has synchronized with this thread.
2846
2847 It is also safe to use functions that make GDI calls, such as
2848 w32_clear_rect, because these functions must obtain a DC handle
2849 from the frame struct using get_frame_dc which is thread-aware. */
2850
2851 switch (msg)
2852 {
2853 case WM_ERASEBKGND:
2854 f = x_window_to_frame (dpyinfo, hwnd);
2855 if (f)
2856 {
2857 HDC hdc = get_frame_dc (f);
2858 GetUpdateRect (hwnd, &wmsg.rect, FALSE);
2859 w32_clear_rect (f, hdc, &wmsg.rect);
2860 release_frame_dc (f, hdc);
2861
2862 #if defined (W32_DEBUG_DISPLAY)
2863 DebPrint (("WM_ERASEBKGND (frame %p): erasing %d,%d-%d,%d\n",
2864 f,
2865 wmsg.rect.left, wmsg.rect.top,
2866 wmsg.rect.right, wmsg.rect.bottom));
2867 #endif /* W32_DEBUG_DISPLAY */
2868 }
2869 return 1;
2870 case WM_PALETTECHANGED:
2871 /* ignore our own changes */
2872 if ((HWND)wParam != hwnd)
2873 {
2874 f = x_window_to_frame (dpyinfo, hwnd);
2875 if (f)
2876 /* get_frame_dc will realize our palette and force all
2877 frames to be redrawn if needed. */
2878 release_frame_dc (f, get_frame_dc (f));
2879 }
2880 return 0;
2881 case WM_PAINT:
2882 {
2883 PAINTSTRUCT paintStruct;
2884 RECT update_rect;
2885 bzero (&update_rect, sizeof (update_rect));
2886
2887 f = x_window_to_frame (dpyinfo, hwnd);
2888 if (f == 0)
2889 {
2890 DebPrint (("WM_PAINT received for unknown window %p\n", hwnd));
2891 return 0;
2892 }
2893
2894 /* MSDN Docs say not to call BeginPaint if GetUpdateRect
2895 fails. Apparently this can happen under some
2896 circumstances. */
2897 if (GetUpdateRect (hwnd, &update_rect, FALSE) || !w32_strict_painting)
2898 {
2899 enter_crit ();
2900 BeginPaint (hwnd, &paintStruct);
2901
2902 /* The rectangles returned by GetUpdateRect and BeginPaint
2903 do not always match. Play it safe by assuming both areas
2904 are invalid. */
2905 UnionRect (&(wmsg.rect), &update_rect, &(paintStruct.rcPaint));
2906
2907 #if defined (W32_DEBUG_DISPLAY)
2908 DebPrint (("WM_PAINT (frame %p): painting %d,%d-%d,%d\n",
2909 f,
2910 wmsg.rect.left, wmsg.rect.top,
2911 wmsg.rect.right, wmsg.rect.bottom));
2912 DebPrint ((" [update region is %d,%d-%d,%d]\n",
2913 update_rect.left, update_rect.top,
2914 update_rect.right, update_rect.bottom));
2915 #endif
2916 EndPaint (hwnd, &paintStruct);
2917 leave_crit ();
2918
2919 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2920
2921 return 0;
2922 }
2923
2924 /* If GetUpdateRect returns 0 (meaning there is no update
2925 region), assume the whole window needs to be repainted. */
2926 GetClientRect(hwnd, &wmsg.rect);
2927 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2928 return 0;
2929 }
2930
2931 case WM_INPUTLANGCHANGE:
2932 /* Inform lisp thread of keyboard layout changes. */
2933 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2934
2935 /* Clear dead keys in the keyboard state; for simplicity only
2936 preserve modifier key states. */
2937 {
2938 int i;
2939 BYTE keystate[256];
2940
2941 GetKeyboardState (keystate);
2942 for (i = 0; i < 256; i++)
2943 if (1
2944 && i != VK_SHIFT
2945 && i != VK_LSHIFT
2946 && i != VK_RSHIFT
2947 && i != VK_CAPITAL
2948 && i != VK_NUMLOCK
2949 && i != VK_SCROLL
2950 && i != VK_CONTROL
2951 && i != VK_LCONTROL
2952 && i != VK_RCONTROL
2953 && i != VK_MENU
2954 && i != VK_LMENU
2955 && i != VK_RMENU
2956 && i != VK_LWIN
2957 && i != VK_RWIN)
2958 keystate[i] = 0;
2959 SetKeyboardState (keystate);
2960 }
2961 goto dflt;
2962
2963 case WM_HOTKEY:
2964 /* Synchronize hot keys with normal input. */
2965 PostMessage (hwnd, WM_KEYDOWN, HIWORD (lParam), 0);
2966 return (0);
2967
2968 case WM_KEYUP:
2969 case WM_SYSKEYUP:
2970 record_keyup (wParam, lParam);
2971 goto dflt;
2972
2973 case WM_KEYDOWN:
2974 case WM_SYSKEYDOWN:
2975 /* Ignore keystrokes we fake ourself; see below. */
2976 if (dpyinfo->faked_key == wParam)
2977 {
2978 dpyinfo->faked_key = 0;
2979 /* Make sure TranslateMessage sees them though (as long as
2980 they don't produce WM_CHAR messages). This ensures that
2981 indicator lights are toggled promptly on Windows 9x, for
2982 example. */
2983 if (lispy_function_keys[wParam] != 0)
2984 {
2985 windows_translate = 1;
2986 goto translate;
2987 }
2988 return 0;
2989 }
2990
2991 /* Synchronize modifiers with current keystroke. */
2992 sync_modifiers ();
2993 record_keydown (wParam, lParam);
2994 wParam = map_keypad_keys (wParam, (lParam & 0x1000000L) != 0);
2995
2996 windows_translate = 0;
2997
2998 switch (wParam)
2999 {
3000 case VK_LWIN:
3001 if (NILP (Vw32_pass_lwindow_to_system))
3002 {
3003 /* Prevent system from acting on keyup (which opens the
3004 Start menu if no other key was pressed) by simulating a
3005 press of Space which we will ignore. */
3006 if (GetAsyncKeyState (wParam) & 1)
3007 {
3008 if (NUMBERP (Vw32_phantom_key_code))
3009 key = XUINT (Vw32_phantom_key_code) & 255;
3010 else
3011 key = VK_SPACE;
3012 dpyinfo->faked_key = key;
3013 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
3014 }
3015 }
3016 if (!NILP (Vw32_lwindow_modifier))
3017 return 0;
3018 break;
3019 case VK_RWIN:
3020 if (NILP (Vw32_pass_rwindow_to_system))
3021 {
3022 if (GetAsyncKeyState (wParam) & 1)
3023 {
3024 if (NUMBERP (Vw32_phantom_key_code))
3025 key = XUINT (Vw32_phantom_key_code) & 255;
3026 else
3027 key = VK_SPACE;
3028 dpyinfo->faked_key = key;
3029 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
3030 }
3031 }
3032 if (!NILP (Vw32_rwindow_modifier))
3033 return 0;
3034 break;
3035 case VK_APPS:
3036 if (!NILP (Vw32_apps_modifier))
3037 return 0;
3038 break;
3039 case VK_MENU:
3040 if (NILP (Vw32_pass_alt_to_system))
3041 /* Prevent DefWindowProc from activating the menu bar if an
3042 Alt key is pressed and released by itself. */
3043 return 0;
3044 windows_translate = 1;
3045 break;
3046 case VK_CAPITAL:
3047 /* Decide whether to treat as modifier or function key. */
3048 if (NILP (Vw32_enable_caps_lock))
3049 goto disable_lock_key;
3050 windows_translate = 1;
3051 break;
3052 case VK_NUMLOCK:
3053 /* Decide whether to treat as modifier or function key. */
3054 if (NILP (Vw32_enable_num_lock))
3055 goto disable_lock_key;
3056 windows_translate = 1;
3057 break;
3058 case VK_SCROLL:
3059 /* Decide whether to treat as modifier or function key. */
3060 if (NILP (Vw32_scroll_lock_modifier))
3061 goto disable_lock_key;
3062 windows_translate = 1;
3063 break;
3064 disable_lock_key:
3065 /* Ensure the appropriate lock key state (and indicator light)
3066 remains in the same state. We do this by faking another
3067 press of the relevant key. Apparently, this really is the
3068 only way to toggle the state of the indicator lights. */
3069 dpyinfo->faked_key = wParam;
3070 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
3071 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
3072 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
3073 KEYEVENTF_EXTENDEDKEY | 0, 0);
3074 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
3075 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
3076 /* Ensure indicator lights are updated promptly on Windows 9x
3077 (TranslateMessage apparently does this), after forwarding
3078 input event. */
3079 post_character_message (hwnd, msg, wParam, lParam,
3080 w32_get_key_modifiers (wParam, lParam));
3081 windows_translate = 1;
3082 break;
3083 case VK_CONTROL:
3084 case VK_SHIFT:
3085 case VK_PROCESSKEY: /* Generated by IME. */
3086 windows_translate = 1;
3087 break;
3088 case VK_CANCEL:
3089 /* Windows maps Ctrl-Pause (aka Ctrl-Break) into VK_CANCEL,
3090 which is confusing for purposes of key binding; convert
3091 VK_CANCEL events into VK_PAUSE events. */
3092 wParam = VK_PAUSE;
3093 break;
3094 case VK_PAUSE:
3095 /* Windows maps Ctrl-NumLock into VK_PAUSE, which is confusing
3096 for purposes of key binding; convert these back into
3097 VK_NUMLOCK events, at least when we want to see NumLock key
3098 presses. (Note that there is never any possibility that
3099 VK_PAUSE with Ctrl really is C-Pause as per above.) */
3100 if (NILP (Vw32_enable_num_lock) && modifier_set (VK_CONTROL))
3101 wParam = VK_NUMLOCK;
3102 break;
3103 default:
3104 /* If not defined as a function key, change it to a WM_CHAR message. */
3105 if (lispy_function_keys[wParam] == 0)
3106 {
3107 DWORD modifiers = construct_console_modifiers ();
3108
3109 if (!NILP (Vw32_recognize_altgr)
3110 && modifier_set (VK_LCONTROL) && modifier_set (VK_RMENU))
3111 {
3112 /* Always let TranslateMessage handle AltGr key chords;
3113 for some reason, ToAscii doesn't always process AltGr
3114 chords correctly. */
3115 windows_translate = 1;
3116 }
3117 else if ((modifiers & (~SHIFT_PRESSED & ~CAPSLOCK_ON)) != 0)
3118 {
3119 /* Handle key chords including any modifiers other
3120 than shift directly, in order to preserve as much
3121 modifier information as possible. */
3122 if ('A' <= wParam && wParam <= 'Z')
3123 {
3124 /* Don't translate modified alphabetic keystrokes,
3125 so the user doesn't need to constantly switch
3126 layout to type control or meta keystrokes when
3127 the normal layout translates alphabetic
3128 characters to non-ascii characters. */
3129 if (!modifier_set (VK_SHIFT))
3130 wParam += ('a' - 'A');
3131 msg = WM_CHAR;
3132 }
3133 else
3134 {
3135 /* Try to handle other keystrokes by determining the
3136 base character (ie. translating the base key plus
3137 shift modifier). */
3138 int add;
3139 int isdead = 0;
3140 KEY_EVENT_RECORD key;
3141
3142 key.bKeyDown = TRUE;
3143 key.wRepeatCount = 1;
3144 key.wVirtualKeyCode = wParam;
3145 key.wVirtualScanCode = (lParam & 0xFF0000) >> 16;
3146 key.uChar.AsciiChar = 0;
3147 key.dwControlKeyState = modifiers;
3148
3149 add = w32_kbd_patch_key (&key);
3150 /* 0 means an unrecognised keycode, negative means
3151 dead key. Ignore both. */
3152 while (--add >= 0)
3153 {
3154 /* Forward asciified character sequence. */
3155 post_character_message
3156 (hwnd, WM_CHAR, key.uChar.AsciiChar, lParam,
3157 w32_get_key_modifiers (wParam, lParam));
3158 w32_kbd_patch_key (&key);
3159 }
3160 return 0;
3161 }
3162 }
3163 else
3164 {
3165 /* Let TranslateMessage handle everything else. */
3166 windows_translate = 1;
3167 }
3168 }
3169 }
3170
3171 translate:
3172 if (windows_translate)
3173 {
3174 MSG windows_msg = { hwnd, msg, wParam, lParam, 0, {0,0} };
3175
3176 windows_msg.time = GetMessageTime ();
3177 TranslateMessage (&windows_msg);
3178 goto dflt;
3179 }
3180
3181 /* Fall through */
3182
3183 case WM_SYSCHAR:
3184 case WM_CHAR:
3185 post_character_message (hwnd, msg, wParam, lParam,
3186 w32_get_key_modifiers (wParam, lParam));
3187 break;
3188
3189 /* Simulate middle mouse button events when left and right buttons
3190 are used together, but only if user has two button mouse. */
3191 case WM_LBUTTONDOWN:
3192 case WM_RBUTTONDOWN:
3193 if (w32_num_mouse_buttons > 2)
3194 goto handle_plain_button;
3195
3196 {
3197 int this = (msg == WM_LBUTTONDOWN) ? LMOUSE : RMOUSE;
3198 int other = (msg == WM_LBUTTONDOWN) ? RMOUSE : LMOUSE;
3199
3200 if (button_state & this)
3201 return 0;
3202
3203 if (button_state == 0)
3204 SetCapture (hwnd);
3205
3206 button_state |= this;
3207
3208 if (button_state & other)
3209 {
3210 if (mouse_button_timer)
3211 {
3212 KillTimer (hwnd, mouse_button_timer);
3213 mouse_button_timer = 0;
3214
3215 /* Generate middle mouse event instead. */
3216 msg = WM_MBUTTONDOWN;
3217 button_state |= MMOUSE;
3218 }
3219 else if (button_state & MMOUSE)
3220 {
3221 /* Ignore button event if we've already generated a
3222 middle mouse down event. This happens if the
3223 user releases and press one of the two buttons
3224 after we've faked a middle mouse event. */
3225 return 0;
3226 }
3227 else
3228 {
3229 /* Flush out saved message. */
3230 post_msg (&saved_mouse_button_msg);
3231 }
3232 wmsg.dwModifiers = w32_get_modifiers ();
3233 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3234 signal_user_input ();
3235
3236 /* Clear message buffer. */
3237 saved_mouse_button_msg.msg.hwnd = 0;
3238 }
3239 else
3240 {
3241 /* Hold onto message for now. */
3242 mouse_button_timer =
3243 SetTimer (hwnd, MOUSE_BUTTON_ID,
3244 w32_mouse_button_tolerance, NULL);
3245 saved_mouse_button_msg.msg.hwnd = hwnd;
3246 saved_mouse_button_msg.msg.message = msg;
3247 saved_mouse_button_msg.msg.wParam = wParam;
3248 saved_mouse_button_msg.msg.lParam = lParam;
3249 saved_mouse_button_msg.msg.time = GetMessageTime ();
3250 saved_mouse_button_msg.dwModifiers = w32_get_modifiers ();
3251 }
3252 }
3253 return 0;
3254
3255 case WM_LBUTTONUP:
3256 case WM_RBUTTONUP:
3257 if (w32_num_mouse_buttons > 2)
3258 goto handle_plain_button;
3259
3260 {
3261 int this = (msg == WM_LBUTTONUP) ? LMOUSE : RMOUSE;
3262 int other = (msg == WM_LBUTTONUP) ? RMOUSE : LMOUSE;
3263
3264 if ((button_state & this) == 0)
3265 return 0;
3266
3267 button_state &= ~this;
3268
3269 if (button_state & MMOUSE)
3270 {
3271 /* Only generate event when second button is released. */
3272 if ((button_state & other) == 0)
3273 {
3274 msg = WM_MBUTTONUP;
3275 button_state &= ~MMOUSE;
3276
3277 if (button_state) abort ();
3278 }
3279 else
3280 return 0;
3281 }
3282 else
3283 {
3284 /* Flush out saved message if necessary. */
3285 if (saved_mouse_button_msg.msg.hwnd)
3286 {
3287 post_msg (&saved_mouse_button_msg);
3288 }
3289 }
3290 wmsg.dwModifiers = w32_get_modifiers ();
3291 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3292 signal_user_input ();
3293
3294 /* Always clear message buffer and cancel timer. */
3295 saved_mouse_button_msg.msg.hwnd = 0;
3296 KillTimer (hwnd, mouse_button_timer);
3297 mouse_button_timer = 0;
3298
3299 if (button_state == 0)
3300 ReleaseCapture ();
3301 }
3302 return 0;
3303
3304 case WM_XBUTTONDOWN:
3305 case WM_XBUTTONUP:
3306 if (w32_pass_extra_mouse_buttons_to_system)
3307 goto dflt;
3308 /* else fall through and process them. */
3309 case WM_MBUTTONDOWN:
3310 case WM_MBUTTONUP:
3311 handle_plain_button:
3312 {
3313 BOOL up;
3314 int button;
3315
3316 /* Ignore middle and extra buttons as long as the menu is active. */
3317 f = x_window_to_frame (dpyinfo, hwnd);
3318 if (f && f->output_data.w32->menubar_active)
3319 return 0;
3320
3321 if (parse_button (msg, HIWORD (wParam), &button, &up))
3322 {
3323 if (up) ReleaseCapture ();
3324 else SetCapture (hwnd);
3325 button = (button == 0) ? LMOUSE :
3326 ((button == 1) ? MMOUSE : RMOUSE);
3327 if (up)
3328 button_state &= ~button;
3329 else
3330 button_state |= button;
3331 }
3332 }
3333
3334 wmsg.dwModifiers = w32_get_modifiers ();
3335 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3336 signal_user_input ();
3337
3338 /* Need to return true for XBUTTON messages, false for others,
3339 to indicate that we processed the message. */
3340 return (msg == WM_XBUTTONDOWN || msg == WM_XBUTTONUP);
3341
3342 case WM_MOUSEMOVE:
3343 /* Ignore mouse movements as long as the menu is active. These
3344 movements are processed by the window manager anyway, and
3345 it's wrong to handle them as if they happened on the
3346 underlying frame. */
3347 f = x_window_to_frame (dpyinfo, hwnd);
3348 if (f && f->output_data.w32->menubar_active)
3349 return 0;
3350
3351 /* If the mouse has just moved into the frame, start tracking
3352 it, so we will be notified when it leaves the frame. Mouse
3353 tracking only works under W98 and NT4 and later. On earlier
3354 versions, there is no way of telling when the mouse leaves the
3355 frame, so we just have to put up with help-echo and mouse
3356 highlighting remaining while the frame is not active. */
3357 if (track_mouse_event_fn && !track_mouse_window)
3358 {
3359 TRACKMOUSEEVENT tme;
3360 tme.cbSize = sizeof (tme);
3361 tme.dwFlags = TME_LEAVE;
3362 tme.hwndTrack = hwnd;
3363
3364 track_mouse_event_fn (&tme);
3365 track_mouse_window = hwnd;
3366 }
3367 case WM_VSCROLL:
3368 if (w32_mouse_move_interval <= 0
3369 || (msg == WM_MOUSEMOVE && button_state == 0))
3370 {
3371 wmsg.dwModifiers = w32_get_modifiers ();
3372 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3373 return 0;
3374 }
3375
3376 /* Hang onto mouse move and scroll messages for a bit, to avoid
3377 sending such events to Emacs faster than it can process them.
3378 If we get more events before the timer from the first message
3379 expires, we just replace the first message. */
3380
3381 if (saved_mouse_move_msg.msg.hwnd == 0)
3382 mouse_move_timer =
3383 SetTimer (hwnd, MOUSE_MOVE_ID,
3384 w32_mouse_move_interval, NULL);
3385
3386 /* Hold onto message for now. */
3387 saved_mouse_move_msg.msg.hwnd = hwnd;
3388 saved_mouse_move_msg.msg.message = msg;
3389 saved_mouse_move_msg.msg.wParam = wParam;
3390 saved_mouse_move_msg.msg.lParam = lParam;
3391 saved_mouse_move_msg.msg.time = GetMessageTime ();
3392 saved_mouse_move_msg.dwModifiers = w32_get_modifiers ();
3393
3394 return 0;
3395
3396 case WM_MOUSEWHEEL:
3397 wmsg.dwModifiers = w32_get_modifiers ();
3398 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3399 signal_user_input ();
3400 return 0;
3401
3402 case WM_DROPFILES:
3403 wmsg.dwModifiers = w32_get_modifiers ();
3404 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3405 signal_user_input ();
3406 return 0;
3407
3408 case WM_TIMER:
3409 /* Flush out saved messages if necessary. */
3410 if (wParam == mouse_button_timer)
3411 {
3412 if (saved_mouse_button_msg.msg.hwnd)
3413 {
3414 post_msg (&saved_mouse_button_msg);
3415 signal_user_input ();
3416 saved_mouse_button_msg.msg.hwnd = 0;
3417 }
3418 KillTimer (hwnd, mouse_button_timer);
3419 mouse_button_timer = 0;
3420 }
3421 else if (wParam == mouse_move_timer)
3422 {
3423 if (saved_mouse_move_msg.msg.hwnd)
3424 {
3425 post_msg (&saved_mouse_move_msg);
3426 saved_mouse_move_msg.msg.hwnd = 0;
3427 }
3428 KillTimer (hwnd, mouse_move_timer);
3429 mouse_move_timer = 0;
3430 }
3431 else if (wParam == menu_free_timer)
3432 {
3433 KillTimer (hwnd, menu_free_timer);
3434 menu_free_timer = 0;
3435 f = x_window_to_frame (dpyinfo, hwnd);
3436 if (!f->output_data.w32->menu_command_in_progress)
3437 {
3438 /* Free memory used by owner-drawn and help-echo strings. */
3439 w32_free_menu_strings (hwnd);
3440 f->output_data.w32->menubar_active = 0;
3441 }
3442 }
3443 return 0;
3444
3445 case WM_NCACTIVATE:
3446 /* Windows doesn't send us focus messages when putting up and
3447 taking down a system popup dialog as for Ctrl-Alt-Del on Windows 95.
3448 The only indication we get that something happened is receiving
3449 this message afterwards. So this is a good time to reset our
3450 keyboard modifiers' state. */
3451 reset_modifiers ();
3452 goto dflt;
3453
3454 case WM_INITMENU:
3455 button_state = 0;
3456 ReleaseCapture ();
3457 /* We must ensure menu bar is fully constructed and up to date
3458 before allowing user interaction with it. To achieve this
3459 we send this message to the lisp thread and wait for a
3460 reply (whose value is not actually needed) to indicate that
3461 the menu bar is now ready for use, so we can now return.
3462
3463 To remain responsive in the meantime, we enter a nested message
3464 loop that can process all other messages.
3465
3466 However, we skip all this if the message results from calling
3467 TrackPopupMenu - in fact, we must NOT attempt to send the lisp
3468 thread a message because it is blocked on us at this point. We
3469 set menubar_active before calling TrackPopupMenu to indicate
3470 this (there is no possibility of confusion with real menubar
3471 being active). */
3472
3473 f = x_window_to_frame (dpyinfo, hwnd);
3474 if (f
3475 && (f->output_data.w32->menubar_active
3476 /* We can receive this message even in the absence of a
3477 menubar (ie. when the system menu is activated) - in this
3478 case we do NOT want to forward the message, otherwise it
3479 will cause the menubar to suddenly appear when the user
3480 had requested it to be turned off! */
3481 || f->output_data.w32->menubar_widget == NULL))
3482 return 0;
3483
3484 {
3485 deferred_msg msg_buf;
3486
3487 /* Detect if message has already been deferred; in this case
3488 we cannot return any sensible value to ignore this. */
3489 if (find_deferred_msg (hwnd, msg) != NULL)
3490 abort ();
3491
3492 return send_deferred_msg (&msg_buf, hwnd, msg, wParam, lParam);
3493 }
3494
3495 case WM_EXITMENULOOP:
3496 f = x_window_to_frame (dpyinfo, hwnd);
3497
3498 /* If a menu command is not already in progress, check again
3499 after a short delay, since Windows often (always?) sends the
3500 WM_EXITMENULOOP before the corresponding WM_COMMAND message. */
3501 if (f && !f->output_data.w32->menu_command_in_progress)
3502 menu_free_timer = SetTimer (hwnd, MENU_FREE_ID, MENU_FREE_DELAY, NULL);
3503 goto dflt;
3504
3505 case WM_MENUSELECT:
3506 /* Direct handling of help_echo in menus. Should be safe now
3507 that we generate the help_echo by placing a help event in the
3508 keyboard buffer. */
3509 {
3510 HMENU menu = (HMENU) lParam;
3511 UINT menu_item = (UINT) LOWORD (wParam);
3512 UINT flags = (UINT) HIWORD (wParam);
3513
3514 w32_menu_display_help (hwnd, menu, menu_item, flags);
3515 }
3516 return 0;
3517
3518 case WM_MEASUREITEM:
3519 f = x_window_to_frame (dpyinfo, hwnd);
3520 if (f)
3521 {
3522 MEASUREITEMSTRUCT * pMis = (MEASUREITEMSTRUCT *) lParam;
3523
3524 if (pMis->CtlType == ODT_MENU)
3525 {
3526 /* Work out dimensions for popup menu titles. */
3527 char * title = (char *) pMis->itemData;
3528 HDC hdc = GetDC (hwnd);
3529 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
3530 LOGFONT menu_logfont;
3531 HFONT old_font;
3532 SIZE size;
3533
3534 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
3535 menu_logfont.lfWeight = FW_BOLD;
3536 menu_font = CreateFontIndirect (&menu_logfont);
3537 old_font = SelectObject (hdc, menu_font);
3538
3539 pMis->itemHeight = GetSystemMetrics (SM_CYMENUSIZE);
3540 if (title)
3541 {
3542 if (unicode_append_menu)
3543 GetTextExtentPoint32W (hdc, (WCHAR *) title,
3544 wcslen ((WCHAR *) title),
3545 &size);
3546 else
3547 GetTextExtentPoint32 (hdc, title, strlen (title), &size);
3548
3549 pMis->itemWidth = size.cx;
3550 if (pMis->itemHeight < size.cy)
3551 pMis->itemHeight = size.cy;
3552 }
3553 else
3554 pMis->itemWidth = 0;
3555
3556 SelectObject (hdc, old_font);
3557 DeleteObject (menu_font);
3558 ReleaseDC (hwnd, hdc);
3559 return TRUE;
3560 }
3561 }
3562 return 0;
3563
3564 case WM_DRAWITEM:
3565 f = x_window_to_frame (dpyinfo, hwnd);
3566 if (f)
3567 {
3568 DRAWITEMSTRUCT * pDis = (DRAWITEMSTRUCT *) lParam;
3569
3570 if (pDis->CtlType == ODT_MENU)
3571 {
3572 /* Draw popup menu title. */
3573 char * title = (char *) pDis->itemData;
3574 if (title)
3575 {
3576 HDC hdc = pDis->hDC;
3577 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
3578 LOGFONT menu_logfont;
3579 HFONT old_font;
3580
3581 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
3582 menu_logfont.lfWeight = FW_BOLD;
3583 menu_font = CreateFontIndirect (&menu_logfont);
3584 old_font = SelectObject (hdc, menu_font);
3585
3586 /* Always draw title as if not selected. */
3587 if (unicode_append_menu)
3588 ExtTextOutW (hdc,
3589 pDis->rcItem.left
3590 + GetSystemMetrics (SM_CXMENUCHECK),
3591 pDis->rcItem.top,
3592 ETO_OPAQUE, &pDis->rcItem,
3593 (WCHAR *) title,
3594 wcslen ((WCHAR *) title), NULL);
3595 else
3596 ExtTextOut (hdc,
3597 pDis->rcItem.left
3598 + GetSystemMetrics (SM_CXMENUCHECK),
3599 pDis->rcItem.top,
3600 ETO_OPAQUE, &pDis->rcItem,
3601 title, strlen (title), NULL);
3602
3603 SelectObject (hdc, old_font);
3604 DeleteObject (menu_font);
3605 }
3606 return TRUE;
3607 }
3608 }
3609 return 0;
3610
3611 #if 0
3612 /* Still not right - can't distinguish between clicks in the
3613 client area of the frame from clicks forwarded from the scroll
3614 bars - may have to hook WM_NCHITTEST to remember the mouse
3615 position and then check if it is in the client area ourselves. */
3616 case WM_MOUSEACTIVATE:
3617 /* Discard the mouse click that activates a frame, allowing the
3618 user to click anywhere without changing point (or worse!).
3619 Don't eat mouse clicks on scrollbars though!! */
3620 if (LOWORD (lParam) == HTCLIENT )
3621 return MA_ACTIVATEANDEAT;
3622 goto dflt;
3623 #endif
3624
3625 case WM_MOUSELEAVE:
3626 /* No longer tracking mouse. */
3627 track_mouse_window = NULL;
3628
3629 case WM_ACTIVATEAPP:
3630 case WM_ACTIVATE:
3631 case WM_WINDOWPOSCHANGED:
3632 case WM_SHOWWINDOW:
3633 /* Inform lisp thread that a frame might have just been obscured
3634 or exposed, so should recheck visibility of all frames. */
3635 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3636 goto dflt;
3637
3638 case WM_SETFOCUS:
3639 dpyinfo->faked_key = 0;
3640 reset_modifiers ();
3641 register_hot_keys (hwnd);
3642 goto command;
3643 case WM_KILLFOCUS:
3644 unregister_hot_keys (hwnd);
3645 button_state = 0;
3646 ReleaseCapture ();
3647 /* Relinquish the system caret. */
3648 if (w32_system_caret_hwnd)
3649 {
3650 w32_visible_system_caret_hwnd = NULL;
3651 w32_system_caret_hwnd = NULL;
3652 DestroyCaret ();
3653 }
3654 goto command;
3655 case WM_COMMAND:
3656 f = x_window_to_frame (dpyinfo, hwnd);
3657 if (f && HIWORD (wParam) == 0)
3658 {
3659 f->output_data.w32->menu_command_in_progress = 1;
3660 if (menu_free_timer)
3661 {
3662 KillTimer (hwnd, menu_free_timer);
3663 menu_free_timer = 0;
3664 }
3665 }
3666 case WM_MOVE:
3667 case WM_SIZE:
3668 command:
3669 wmsg.dwModifiers = w32_get_modifiers ();
3670 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3671 goto dflt;
3672
3673 case WM_CLOSE:
3674 wmsg.dwModifiers = w32_get_modifiers ();
3675 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3676 return 0;
3677
3678 case WM_WINDOWPOSCHANGING:
3679 /* Don't restrict the sizing of tip frames. */
3680 if (hwnd == tip_window)
3681 return 0;
3682 {
3683 WINDOWPLACEMENT wp;
3684 LPWINDOWPOS lppos = (WINDOWPOS *) lParam;
3685
3686 wp.length = sizeof (WINDOWPLACEMENT);
3687 GetWindowPlacement (hwnd, &wp);
3688
3689 if (wp.showCmd != SW_SHOWMINIMIZED && (lppos->flags & SWP_NOSIZE) == 0)
3690 {
3691 RECT rect;
3692 int wdiff;
3693 int hdiff;
3694 DWORD font_width;
3695 DWORD line_height;
3696 DWORD internal_border;
3697 DWORD scrollbar_extra;
3698 RECT wr;
3699
3700 wp.length = sizeof(wp);
3701 GetWindowRect (hwnd, &wr);
3702
3703 enter_crit ();
3704
3705 font_width = GetWindowLong (hwnd, WND_FONTWIDTH_INDEX);
3706 line_height = GetWindowLong (hwnd, WND_LINEHEIGHT_INDEX);
3707 internal_border = GetWindowLong (hwnd, WND_BORDER_INDEX);
3708 scrollbar_extra = GetWindowLong (hwnd, WND_SCROLLBAR_INDEX);
3709
3710 leave_crit ();
3711
3712 memset (&rect, 0, sizeof (rect));
3713 AdjustWindowRect (&rect, GetWindowLong (hwnd, GWL_STYLE),
3714 GetMenu (hwnd) != NULL);
3715
3716 /* Force width and height of client area to be exact
3717 multiples of the character cell dimensions. */
3718 wdiff = (lppos->cx - (rect.right - rect.left)
3719 - 2 * internal_border - scrollbar_extra)
3720 % font_width;
3721 hdiff = (lppos->cy - (rect.bottom - rect.top)
3722 - 2 * internal_border)
3723 % line_height;
3724
3725 if (wdiff || hdiff)
3726 {
3727 /* For right/bottom sizing we can just fix the sizes.
3728 However for top/left sizing we will need to fix the X
3729 and Y positions as well. */
3730
3731 lppos->cx -= wdiff;
3732 lppos->cy -= hdiff;
3733
3734 if (wp.showCmd != SW_SHOWMAXIMIZED
3735 && (lppos->flags & SWP_NOMOVE) == 0)
3736 {
3737 if (lppos->x != wr.left || lppos->y != wr.top)
3738 {
3739 lppos->x += wdiff;
3740 lppos->y += hdiff;
3741 }
3742 else
3743 {
3744 lppos->flags |= SWP_NOMOVE;
3745 }
3746 }
3747
3748 return 0;
3749 }
3750 }
3751 }
3752
3753 goto dflt;
3754
3755 case WM_GETMINMAXINFO:
3756 /* Hack to correct bug that allows Emacs frames to be resized
3757 below the Minimum Tracking Size. */
3758 ((LPMINMAXINFO) lParam)->ptMinTrackSize.y++;
3759 /* Hack to allow resizing the Emacs frame above the screen size.
3760 Note that Windows 9x limits coordinates to 16-bits. */
3761 ((LPMINMAXINFO) lParam)->ptMaxTrackSize.x = 32767;
3762 ((LPMINMAXINFO) lParam)->ptMaxTrackSize.y = 32767;
3763 return 0;
3764
3765 case WM_SETCURSOR:
3766 if (LOWORD (lParam) == HTCLIENT)
3767 return 0;
3768
3769 goto dflt;
3770
3771 case WM_EMACS_SETCURSOR:
3772 {
3773 Cursor cursor = (Cursor) wParam;
3774 if (cursor)
3775 SetCursor (cursor);
3776 return 0;
3777 }
3778
3779 case WM_EMACS_CREATESCROLLBAR:
3780 return (LRESULT) w32_createscrollbar ((struct frame *) wParam,
3781 (struct scroll_bar *) lParam);
3782
3783 case WM_EMACS_SHOWWINDOW:
3784 return ShowWindow ((HWND) wParam, (WPARAM) lParam);
3785
3786 case WM_EMACS_SETFOREGROUND:
3787 {
3788 HWND foreground_window;
3789 DWORD foreground_thread, retval;
3790
3791 /* On NT 5.0, and apparently Windows 98, it is necessary to
3792 attach to the thread that currently has focus in order to
3793 pull the focus away from it. */
3794 foreground_window = GetForegroundWindow ();
3795 foreground_thread = GetWindowThreadProcessId (foreground_window, NULL);
3796 if (!foreground_window
3797 || foreground_thread == GetCurrentThreadId ()
3798 || !AttachThreadInput (GetCurrentThreadId (),
3799 foreground_thread, TRUE))
3800 foreground_thread = 0;
3801
3802 retval = SetForegroundWindow ((HWND) wParam);
3803
3804 /* Detach from the previous foreground thread. */
3805 if (foreground_thread)
3806 AttachThreadInput (GetCurrentThreadId (),
3807 foreground_thread, FALSE);
3808
3809 return retval;
3810 }
3811
3812 case WM_EMACS_SETWINDOWPOS:
3813 {
3814 WINDOWPOS * pos = (WINDOWPOS *) wParam;
3815 return SetWindowPos (hwnd, pos->hwndInsertAfter,
3816 pos->x, pos->y, pos->cx, pos->cy, pos->flags);
3817 }
3818
3819 case WM_EMACS_DESTROYWINDOW:
3820 DragAcceptFiles ((HWND) wParam, FALSE);
3821 return DestroyWindow ((HWND) wParam);
3822
3823 case WM_EMACS_HIDE_CARET:
3824 return HideCaret (hwnd);
3825
3826 case WM_EMACS_SHOW_CARET:
3827 return ShowCaret (hwnd);
3828
3829 case WM_EMACS_DESTROY_CARET:
3830 w32_system_caret_hwnd = NULL;
3831 w32_visible_system_caret_hwnd = NULL;
3832 return DestroyCaret ();
3833
3834 case WM_EMACS_TRACK_CARET:
3835 /* If there is currently no system caret, create one. */
3836 if (w32_system_caret_hwnd == NULL)
3837 {
3838 /* Use the default caret width, and avoid changing it
3839 unneccesarily, as it confuses screen reader software. */
3840 w32_system_caret_hwnd = hwnd;
3841 CreateCaret (hwnd, NULL, 0,
3842 w32_system_caret_height);
3843 }
3844
3845 if (!SetCaretPos (w32_system_caret_x, w32_system_caret_y))
3846 return 0;
3847 /* Ensure visible caret gets turned on when requested. */
3848 else if (w32_use_visible_system_caret
3849 && w32_visible_system_caret_hwnd != hwnd)
3850 {
3851 w32_visible_system_caret_hwnd = hwnd;
3852 return ShowCaret (hwnd);
3853 }
3854 /* Ensure visible caret gets turned off when requested. */
3855 else if (!w32_use_visible_system_caret
3856 && w32_visible_system_caret_hwnd)
3857 {
3858 w32_visible_system_caret_hwnd = NULL;
3859 return HideCaret (hwnd);
3860 }
3861 else
3862 return 1;
3863
3864 case WM_EMACS_TRACKPOPUPMENU:
3865 {
3866 UINT flags;
3867 POINT *pos;
3868 int retval;
3869 pos = (POINT *)lParam;
3870 flags = TPM_CENTERALIGN;
3871 if (button_state & LMOUSE)
3872 flags |= TPM_LEFTBUTTON;
3873 else if (button_state & RMOUSE)
3874 flags |= TPM_RIGHTBUTTON;
3875
3876 /* Remember we did a SetCapture on the initial mouse down event,
3877 so for safety, we make sure the capture is cancelled now. */
3878 ReleaseCapture ();
3879 button_state = 0;
3880
3881 /* Use menubar_active to indicate that WM_INITMENU is from
3882 TrackPopupMenu below, and should be ignored. */
3883 f = x_window_to_frame (dpyinfo, hwnd);
3884 if (f)
3885 f->output_data.w32->menubar_active = 1;
3886
3887 if (TrackPopupMenu ((HMENU)wParam, flags, pos->x, pos->y,
3888 0, hwnd, NULL))
3889 {
3890 MSG amsg;
3891 /* Eat any mouse messages during popupmenu */
3892 while (PeekMessage (&amsg, hwnd, WM_MOUSEFIRST, WM_MOUSELAST,
3893 PM_REMOVE));
3894 /* Get the menu selection, if any */
3895 if (PeekMessage (&amsg, hwnd, WM_COMMAND, WM_COMMAND, PM_REMOVE))
3896 {
3897 retval = LOWORD (amsg.wParam);
3898 }
3899 else
3900 {
3901 retval = 0;
3902 }
3903 }
3904 else
3905 {
3906 retval = -1;
3907 }
3908
3909 return retval;
3910 }
3911
3912 default:
3913 /* Check for messages registered at runtime. */
3914 if (msg == msh_mousewheel)
3915 {
3916 wmsg.dwModifiers = w32_get_modifiers ();
3917 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3918 signal_user_input ();
3919 return 0;
3920 }
3921
3922 dflt:
3923 return DefWindowProc (hwnd, msg, wParam, lParam);
3924 }
3925
3926
3927 /* The most common default return code for handled messages is 0. */
3928 return 0;
3929 }
3930
3931 static void
3932 my_create_window (f)
3933 struct frame * f;
3934 {
3935 MSG msg;
3936
3937 if (!PostThreadMessage (dwWindowsThreadId, WM_EMACS_CREATEWINDOW, (WPARAM)f, 0))
3938 abort ();
3939 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
3940 }
3941
3942
3943 /* Create a tooltip window. Unlike my_create_window, we do not do this
3944 indirectly via the Window thread, as we do not need to process Window
3945 messages for the tooltip. Creating tooltips indirectly also creates
3946 deadlocks when tooltips are created for menu items. */
3947 static void
3948 my_create_tip_window (f)
3949 struct frame *f;
3950 {
3951 RECT rect;
3952
3953 rect.left = rect.top = 0;
3954 rect.right = FRAME_PIXEL_WIDTH (f);
3955 rect.bottom = FRAME_PIXEL_HEIGHT (f);
3956
3957 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
3958 FRAME_EXTERNAL_MENU_BAR (f));
3959
3960 tip_window = FRAME_W32_WINDOW (f)
3961 = CreateWindow (EMACS_CLASS,
3962 f->namebuf,
3963 f->output_data.w32->dwStyle,
3964 f->left_pos,
3965 f->top_pos,
3966 rect.right - rect.left,
3967 rect.bottom - rect.top,
3968 FRAME_W32_WINDOW (SELECTED_FRAME ()), /* owner */
3969 NULL,
3970 hinst,
3971 NULL);
3972
3973 if (tip_window)
3974 {
3975 SetWindowLong (tip_window, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
3976 SetWindowLong (tip_window, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
3977 SetWindowLong (tip_window, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
3978 SetWindowLong (tip_window, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
3979
3980 /* Tip frames have no scrollbars. */
3981 SetWindowLong (tip_window, WND_SCROLLBAR_INDEX, 0);
3982
3983 /* Do this to discard the default setting specified by our parent. */
3984 ShowWindow (tip_window, SW_HIDE);
3985 }
3986 }
3987
3988
3989 /* Create and set up the w32 window for frame F. */
3990
3991 static void
3992 w32_window (f, window_prompting, minibuffer_only)
3993 struct frame *f;
3994 long window_prompting;
3995 int minibuffer_only;
3996 {
3997 BLOCK_INPUT;
3998
3999 /* Use the resource name as the top-level window name
4000 for looking up resources. Make a non-Lisp copy
4001 for the window manager, so GC relocation won't bother it.
4002
4003 Elsewhere we specify the window name for the window manager. */
4004
4005 {
4006 char *str = (char *) SDATA (Vx_resource_name);
4007 f->namebuf = (char *) xmalloc (strlen (str) + 1);
4008 strcpy (f->namebuf, str);
4009 }
4010
4011 my_create_window (f);
4012
4013 validate_x_resource_name ();
4014
4015 /* x_set_name normally ignores requests to set the name if the
4016 requested name is the same as the current name. This is the one
4017 place where that assumption isn't correct; f->name is set, but
4018 the server hasn't been told. */
4019 {
4020 Lisp_Object name;
4021 int explicit = f->explicit_name;
4022
4023 f->explicit_name = 0;
4024 name = f->name;
4025 f->name = Qnil;
4026 x_set_name (f, name, explicit);
4027 }
4028
4029 UNBLOCK_INPUT;
4030
4031 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
4032 initialize_frame_menubar (f);
4033
4034 if (FRAME_W32_WINDOW (f) == 0)
4035 error ("Unable to create window");
4036 }
4037
4038 /* Handle the icon stuff for this window. Perhaps later we might
4039 want an x_set_icon_position which can be called interactively as
4040 well. */
4041
4042 static void
4043 x_icon (f, parms)
4044 struct frame *f;
4045 Lisp_Object parms;
4046 {
4047 Lisp_Object icon_x, icon_y;
4048
4049 /* Set the position of the icon. Note that Windows 95 groups all
4050 icons in the tray. */
4051 icon_x = w32_get_arg (parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
4052 icon_y = w32_get_arg (parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
4053 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
4054 {
4055 CHECK_NUMBER (icon_x);
4056 CHECK_NUMBER (icon_y);
4057 }
4058 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
4059 error ("Both left and top icon corners of icon must be specified");
4060
4061 BLOCK_INPUT;
4062
4063 if (! EQ (icon_x, Qunbound))
4064 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
4065
4066 #if 0 /* TODO */
4067 /* Start up iconic or window? */
4068 x_wm_set_window_state
4069 (f, (EQ (w32_get_arg (parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL), Qicon)
4070 ? IconicState
4071 : NormalState));
4072
4073 x_text_icon (f, (char *) SDATA ((!NILP (f->icon_name)
4074 ? f->icon_name
4075 : f->name)));
4076 #endif
4077
4078 UNBLOCK_INPUT;
4079 }
4080
4081
4082 static void
4083 x_make_gc (f)
4084 struct frame *f;
4085 {
4086 XGCValues gc_values;
4087
4088 BLOCK_INPUT;
4089
4090 /* Create the GC's of this frame.
4091 Note that many default values are used. */
4092
4093 /* Normal video */
4094 gc_values.font = FRAME_FONT (f);
4095
4096 /* Cursor has cursor-color background, background-color foreground. */
4097 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
4098 gc_values.background = f->output_data.w32->cursor_pixel;
4099 f->output_data.w32->cursor_gc
4100 = XCreateGC (NULL, FRAME_W32_WINDOW (f),
4101 (GCFont | GCForeground | GCBackground),
4102 &gc_values);
4103
4104 /* Reliefs. */
4105 f->output_data.w32->white_relief.gc = 0;
4106 f->output_data.w32->black_relief.gc = 0;
4107
4108 UNBLOCK_INPUT;
4109 }
4110
4111
4112 /* Handler for signals raised during x_create_frame and
4113 x_create_top_frame. FRAME is the frame which is partially
4114 constructed. */
4115
4116 static Lisp_Object
4117 unwind_create_frame (frame)
4118 Lisp_Object frame;
4119 {
4120 struct frame *f = XFRAME (frame);
4121
4122 /* If frame is ``official'', nothing to do. */
4123 if (!CONSP (Vframe_list) || !EQ (XCAR (Vframe_list), frame))
4124 {
4125 #ifdef GLYPH_DEBUG
4126 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4127 #endif
4128
4129 x_free_frame_resources (f);
4130
4131 /* Check that reference counts are indeed correct. */
4132 xassert (dpyinfo->reference_count == dpyinfo_refcount);
4133 xassert (dpyinfo->image_cache->refcount == image_cache_refcount);
4134
4135 return Qt;
4136 }
4137
4138 return Qnil;
4139 }
4140
4141
4142 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
4143 1, 1, 0,
4144 doc: /* Make a new window, which is called a \"frame\" in Emacs terms.
4145 Returns an Emacs frame object.
4146 PARAMETERS is an alist of frame parameters.
4147 If the parameters specify that the frame should not have a minibuffer,
4148 and do not specify a specific minibuffer window to use,
4149 then `default-minibuffer-frame' must be a frame whose minibuffer can
4150 be shared by the new frame.
4151
4152 This function is an internal primitive--use `make-frame' instead. */)
4153 (parameters)
4154 Lisp_Object parameters;
4155 {
4156 struct frame *f;
4157 Lisp_Object frame, tem;
4158 Lisp_Object name;
4159 int minibuffer_only = 0;
4160 long window_prompting = 0;
4161 int width, height;
4162 int count = SPECPDL_INDEX ();
4163 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4164 Lisp_Object display;
4165 struct w32_display_info *dpyinfo = NULL;
4166 Lisp_Object parent;
4167 struct kboard *kb;
4168
4169 check_w32 ();
4170
4171 /* Use this general default value to start with
4172 until we know if this frame has a specified name. */
4173 Vx_resource_name = Vinvocation_name;
4174
4175 display = w32_get_arg (parameters, Qdisplay, 0, 0, RES_TYPE_STRING);
4176 if (EQ (display, Qunbound))
4177 display = Qnil;
4178 dpyinfo = check_x_display_info (display);
4179 #ifdef MULTI_KBOARD
4180 kb = dpyinfo->kboard;
4181 #else
4182 kb = &the_only_kboard;
4183 #endif
4184
4185 name = w32_get_arg (parameters, Qname, "name", "Name", RES_TYPE_STRING);
4186 if (!STRINGP (name)
4187 && ! EQ (name, Qunbound)
4188 && ! NILP (name))
4189 error ("Invalid frame name--not a string or nil");
4190
4191 if (STRINGP (name))
4192 Vx_resource_name = name;
4193
4194 /* See if parent window is specified. */
4195 parent = w32_get_arg (parameters, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
4196 if (EQ (parent, Qunbound))
4197 parent = Qnil;
4198 if (! NILP (parent))
4199 CHECK_NUMBER (parent);
4200
4201 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
4202 /* No need to protect DISPLAY because that's not used after passing
4203 it to make_frame_without_minibuffer. */
4204 frame = Qnil;
4205 GCPRO4 (parameters, parent, name, frame);
4206 tem = w32_get_arg (parameters, Qminibuffer, "minibuffer", "Minibuffer",
4207 RES_TYPE_SYMBOL);
4208 if (EQ (tem, Qnone) || NILP (tem))
4209 f = make_frame_without_minibuffer (Qnil, kb, display);
4210 else if (EQ (tem, Qonly))
4211 {
4212 f = make_minibuffer_frame ();
4213 minibuffer_only = 1;
4214 }
4215 else if (WINDOWP (tem))
4216 f = make_frame_without_minibuffer (tem, kb, display);
4217 else
4218 f = make_frame (1);
4219
4220 XSETFRAME (frame, f);
4221
4222 /* Note that Windows does support scroll bars. */
4223 FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;
4224
4225 /* By default, make scrollbars the system standard width. */
4226 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = GetSystemMetrics (SM_CXVSCROLL);
4227
4228 f->output_method = output_w32;
4229 f->output_data.w32 =
4230 (struct w32_output *) xmalloc (sizeof (struct w32_output));
4231 bzero (f->output_data.w32, sizeof (struct w32_output));
4232 FRAME_FONTSET (f) = -1;
4233 record_unwind_protect (unwind_create_frame, frame);
4234
4235 f->icon_name
4236 = w32_get_arg (parameters, Qicon_name, "iconName", "Title", RES_TYPE_STRING);
4237 if (! STRINGP (f->icon_name))
4238 f->icon_name = Qnil;
4239
4240 /* FRAME_W32_DISPLAY_INFO (f) = dpyinfo; */
4241 #ifdef MULTI_KBOARD
4242 FRAME_KBOARD (f) = kb;
4243 #endif
4244
4245 /* Specify the parent under which to make this window. */
4246
4247 if (!NILP (parent))
4248 {
4249 f->output_data.w32->parent_desc = (Window) XFASTINT (parent);
4250 f->output_data.w32->explicit_parent = 1;
4251 }
4252 else
4253 {
4254 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
4255 f->output_data.w32->explicit_parent = 0;
4256 }
4257
4258 /* Set the name; the functions to which we pass f expect the name to
4259 be set. */
4260 if (EQ (name, Qunbound) || NILP (name))
4261 {
4262 f->name = build_string (dpyinfo->w32_id_name);
4263 f->explicit_name = 0;
4264 }
4265 else
4266 {
4267 f->name = name;
4268 f->explicit_name = 1;
4269 /* use the frame's title when getting resources for this frame. */
4270 specbind (Qx_resource_name, name);
4271 }
4272
4273 /* Extract the window parameters from the supplied values
4274 that are needed to determine window geometry. */
4275 {
4276 Lisp_Object font;
4277
4278 font = w32_get_arg (parameters, Qfont, "font", "Font", RES_TYPE_STRING);
4279
4280 BLOCK_INPUT;
4281 /* First, try whatever font the caller has specified. */
4282 if (STRINGP (font))
4283 {
4284 tem = Fquery_fontset (font, Qnil);
4285 if (STRINGP (tem))
4286 font = x_new_fontset (f, tem);
4287 else
4288 font = x_new_font (f, SDATA (font));
4289 }
4290 /* Try out a font which we hope has bold and italic variations. */
4291 if (!STRINGP (font))
4292 font = x_new_font (f, "-*-Courier New-normal-r-*-*-*-100-*-*-c-*-iso8859-1");
4293 if (! STRINGP (font))
4294 font = x_new_font (f, "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1");
4295 /* If those didn't work, look for something which will at least work. */
4296 if (! STRINGP (font))
4297 font = x_new_font (f, "-*-Fixedsys-normal-r-*-*-12-*-*-*-c-*-iso8859-1");
4298 UNBLOCK_INPUT;
4299 if (! STRINGP (font))
4300 font = build_string ("Fixedsys");
4301
4302 x_default_parameter (f, parameters, Qfont, font,
4303 "font", "Font", RES_TYPE_STRING);
4304 }
4305
4306 x_default_parameter (f, parameters, Qborder_width, make_number (2),
4307 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
4308 /* This defaults to 2 in order to match xterm. We recognize either
4309 internalBorderWidth or internalBorder (which is what xterm calls
4310 it). */
4311 if (NILP (Fassq (Qinternal_border_width, parameters)))
4312 {
4313 Lisp_Object value;
4314
4315 value = w32_get_arg (parameters, Qinternal_border_width,
4316 "internalBorder", "InternalBorder", RES_TYPE_NUMBER);
4317 if (! EQ (value, Qunbound))
4318 parameters = Fcons (Fcons (Qinternal_border_width, value),
4319 parameters);
4320 }
4321 /* Default internalBorderWidth to 0 on Windows to match other programs. */
4322 x_default_parameter (f, parameters, Qinternal_border_width, make_number (0),
4323 "internalBorderWidth", "InternalBorder", RES_TYPE_NUMBER);
4324 x_default_parameter (f, parameters, Qvertical_scroll_bars, Qright,
4325 "verticalScrollBars", "ScrollBars", RES_TYPE_SYMBOL);
4326
4327 /* Also do the stuff which must be set before the window exists. */
4328 x_default_parameter (f, parameters, Qforeground_color, build_string ("black"),
4329 "foreground", "Foreground", RES_TYPE_STRING);
4330 x_default_parameter (f, parameters, Qbackground_color, build_string ("white"),
4331 "background", "Background", RES_TYPE_STRING);
4332 x_default_parameter (f, parameters, Qmouse_color, build_string ("black"),
4333 "pointerColor", "Foreground", RES_TYPE_STRING);
4334 x_default_parameter (f, parameters, Qcursor_color, build_string ("black"),
4335 "cursorColor", "Foreground", RES_TYPE_STRING);
4336 x_default_parameter (f, parameters, Qborder_color, build_string ("black"),
4337 "borderColor", "BorderColor", RES_TYPE_STRING);
4338 x_default_parameter (f, parameters, Qscreen_gamma, Qnil,
4339 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
4340 x_default_parameter (f, parameters, Qline_spacing, Qnil,
4341 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
4342 x_default_parameter (f, parameters, Qleft_fringe, Qnil,
4343 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
4344 x_default_parameter (f, parameters, Qright_fringe, Qnil,
4345 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
4346
4347
4348 /* Init faces before x_default_parameter is called for scroll-bar
4349 parameters because that function calls x_set_scroll_bar_width,
4350 which calls change_frame_size, which calls Fset_window_buffer,
4351 which runs hooks, which call Fvertical_motion. At the end, we
4352 end up in init_iterator with a null face cache, which should not
4353 happen. */
4354 init_frame_faces (f);
4355
4356 x_default_parameter (f, parameters, Qmenu_bar_lines, make_number (1),
4357 "menuBar", "MenuBar", RES_TYPE_NUMBER);
4358 x_default_parameter (f, parameters, Qtool_bar_lines, make_number (1),
4359 "toolBar", "ToolBar", RES_TYPE_NUMBER);
4360
4361 x_default_parameter (f, parameters, Qbuffer_predicate, Qnil,
4362 "bufferPredicate", "BufferPredicate", RES_TYPE_SYMBOL);
4363 x_default_parameter (f, parameters, Qtitle, Qnil,
4364 "title", "Title", RES_TYPE_STRING);
4365 x_default_parameter (f, parameters, Qfullscreen, Qnil,
4366 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
4367
4368 f->output_data.w32->dwStyle = WS_OVERLAPPEDWINDOW;
4369 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
4370
4371 f->output_data.w32->text_cursor = w32_load_cursor (IDC_IBEAM);
4372 f->output_data.w32->nontext_cursor = w32_load_cursor (IDC_ARROW);
4373 f->output_data.w32->modeline_cursor = w32_load_cursor (IDC_ARROW);
4374 f->output_data.w32->hand_cursor = w32_load_cursor (IDC_HAND);
4375 f->output_data.w32->hourglass_cursor = w32_load_cursor (IDC_WAIT);
4376 f->output_data.w32->horizontal_drag_cursor = w32_load_cursor (IDC_SIZEWE);
4377
4378 window_prompting = x_figure_window_size (f, parameters, 1);
4379
4380 tem = w32_get_arg (parameters, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
4381 f->no_split = minibuffer_only || EQ (tem, Qt);
4382
4383 w32_window (f, window_prompting, minibuffer_only);
4384 x_icon (f, parameters);
4385
4386 x_make_gc (f);
4387
4388 /* Now consider the frame official. */
4389 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
4390 Vframe_list = Fcons (frame, Vframe_list);
4391
4392 /* We need to do this after creating the window, so that the
4393 icon-creation functions can say whose icon they're describing. */
4394 x_default_parameter (f, parameters, Qicon_type, Qnil,
4395 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
4396
4397 x_default_parameter (f, parameters, Qauto_raise, Qnil,
4398 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4399 x_default_parameter (f, parameters, Qauto_lower, Qnil,
4400 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4401 x_default_parameter (f, parameters, Qcursor_type, Qbox,
4402 "cursorType", "CursorType", RES_TYPE_SYMBOL);
4403 x_default_parameter (f, parameters, Qscroll_bar_width, Qnil,
4404 "scrollBarWidth", "ScrollBarWidth", RES_TYPE_NUMBER);
4405
4406 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
4407 Change will not be effected unless different from the current
4408 FRAME_LINES (f). */
4409 width = FRAME_COLS (f);
4410 height = FRAME_LINES (f);
4411
4412 FRAME_LINES (f) = 0;
4413 SET_FRAME_COLS (f, 0);
4414 change_frame_size (f, height, width, 1, 0, 0);
4415
4416 /* Tell the server what size and position, etc, we want, and how
4417 badly we want them. This should be done after we have the menu
4418 bar so that its size can be taken into account. */
4419 BLOCK_INPUT;
4420 x_wm_set_size_hint (f, window_prompting, 0);
4421 UNBLOCK_INPUT;
4422
4423 /* Make the window appear on the frame and enable display, unless
4424 the caller says not to. However, with explicit parent, Emacs
4425 cannot control visibility, so don't try. */
4426 if (! f->output_data.w32->explicit_parent)
4427 {
4428 Lisp_Object visibility;
4429
4430 visibility = w32_get_arg (parameters, Qvisibility, 0, 0, RES_TYPE_SYMBOL);
4431 if (EQ (visibility, Qunbound))
4432 visibility = Qt;
4433
4434 if (EQ (visibility, Qicon))
4435 x_iconify_frame (f);
4436 else if (! NILP (visibility))
4437 x_make_frame_visible (f);
4438 else
4439 /* Must have been Qnil. */
4440 ;
4441 }
4442 UNGCPRO;
4443
4444 /* Make sure windows on this frame appear in calls to next-window
4445 and similar functions. */
4446 Vwindow_list = Qnil;
4447
4448 return unbind_to (count, frame);
4449 }
4450
4451 /* FRAME is used only to get a handle on the X display. We don't pass the
4452 display info directly because we're called from frame.c, which doesn't
4453 know about that structure. */
4454 Lisp_Object
4455 x_get_focus_frame (frame)
4456 struct frame *frame;
4457 {
4458 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (frame);
4459 Lisp_Object xfocus;
4460 if (! dpyinfo->w32_focus_frame)
4461 return Qnil;
4462
4463 XSETFRAME (xfocus, dpyinfo->w32_focus_frame);
4464 return xfocus;
4465 }
4466
4467 DEFUN ("w32-focus-frame", Fw32_focus_frame, Sw32_focus_frame, 1, 1, 0,
4468 doc: /* Give FRAME input focus, raising to foreground if necessary. */)
4469 (frame)
4470 Lisp_Object frame;
4471 {
4472 x_focus_on_frame (check_x_frame (frame));
4473 return Qnil;
4474 }
4475
4476 \f
4477 /* Return the charset portion of a font name. */
4478 char * xlfd_charset_of_font (char * fontname)
4479 {
4480 char *charset, *encoding;
4481
4482 encoding = strrchr(fontname, '-');
4483 if (!encoding || encoding == fontname)
4484 return NULL;
4485
4486 for (charset = encoding - 1; charset >= fontname; charset--)
4487 if (*charset == '-')
4488 break;
4489
4490 if (charset == fontname || strcmp(charset, "-*-*") == 0)
4491 return NULL;
4492
4493 return charset + 1;
4494 }
4495
4496 struct font_info *w32_load_bdf_font (struct frame *f, char *fontname,
4497 int size, char* filename);
4498 static Lisp_Object w32_list_bdf_fonts (Lisp_Object pattern, int max_names);
4499 static BOOL w32_to_x_font (LOGFONT * lplf, char * lpxstr, int len,
4500 char * charset);
4501 static BOOL x_to_w32_font (char *lpxstr, LOGFONT *lplogfont);
4502
4503 static struct font_info *
4504 w32_load_system_font (f,fontname,size)
4505 struct frame *f;
4506 char * fontname;
4507 int size;
4508 {
4509 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4510 Lisp_Object font_names;
4511
4512 /* Get a list of all the fonts that match this name. Once we
4513 have a list of matching fonts, we compare them against the fonts
4514 we already have loaded by comparing names. */
4515 font_names = w32_list_fonts (f, build_string (fontname), size, 100);
4516
4517 if (!NILP (font_names))
4518 {
4519 Lisp_Object tail;
4520 int i;
4521
4522 /* First check if any are already loaded, as that is cheaper
4523 than loading another one. */
4524 for (i = 0; i < dpyinfo->n_fonts; i++)
4525 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
4526 if (dpyinfo->font_table[i].name
4527 && (!strcmp (dpyinfo->font_table[i].name,
4528 SDATA (XCAR (tail)))
4529 || !strcmp (dpyinfo->font_table[i].full_name,
4530 SDATA (XCAR (tail)))))
4531 return (dpyinfo->font_table + i);
4532
4533 fontname = (char *) SDATA (XCAR (font_names));
4534 }
4535 else if (w32_strict_fontnames)
4536 {
4537 /* If EnumFontFamiliesEx was available, we got a full list of
4538 fonts back so stop now to avoid the possibility of loading a
4539 random font. If we had to fall back to EnumFontFamilies, the
4540 list is incomplete, so continue whether the font we want was
4541 listed or not. */
4542 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
4543 FARPROC enum_font_families_ex
4544 = GetProcAddress (gdi32, "EnumFontFamiliesExA");
4545 if (enum_font_families_ex)
4546 return NULL;
4547 }
4548
4549 /* Load the font and add it to the table. */
4550 {
4551 char *full_name, *encoding, *charset;
4552 XFontStruct *font;
4553 struct font_info *fontp;
4554 LOGFONT lf;
4555 BOOL ok;
4556 int codepage;
4557 int i;
4558
4559 if (!fontname || !x_to_w32_font (fontname, &lf))
4560 return (NULL);
4561
4562 if (!*lf.lfFaceName)
4563 /* If no name was specified for the font, we get a random font
4564 from CreateFontIndirect - this is not particularly
4565 desirable, especially since CreateFontIndirect does not
4566 fill out the missing name in lf, so we never know what we
4567 ended up with. */
4568 return NULL;
4569
4570 lf.lfQuality = DEFAULT_QUALITY;
4571
4572 font = (XFontStruct *) xmalloc (sizeof (XFontStruct));
4573 bzero (font, sizeof (*font));
4574
4575 /* Set bdf to NULL to indicate that this is a Windows font. */
4576 font->bdf = NULL;
4577
4578 BLOCK_INPUT;
4579
4580 font->hfont = CreateFontIndirect (&lf);
4581
4582 if (font->hfont == NULL)
4583 {
4584 ok = FALSE;
4585 }
4586 else
4587 {
4588 HDC hdc;
4589 HANDLE oldobj;
4590
4591 codepage = w32_codepage_for_font (fontname);
4592
4593 hdc = GetDC (dpyinfo->root_window);
4594 oldobj = SelectObject (hdc, font->hfont);
4595
4596 ok = GetTextMetrics (hdc, &font->tm);
4597 if (codepage == CP_UNICODE)
4598 font->double_byte_p = 1;
4599 else
4600 {
4601 /* Unfortunately, some fonts (eg. MingLiU, a big5 ttf font)
4602 don't report themselves as double byte fonts, when
4603 patently they are. So instead of trusting
4604 GetFontLanguageInfo, we check the properties of the
4605 codepage directly, since that is ultimately what we are
4606 working from anyway. */
4607 /* font->double_byte_p = GetFontLanguageInfo(hdc) & GCP_DBCS; */
4608 CPINFO cpi = {0};
4609 GetCPInfo (codepage, &cpi);
4610 font->double_byte_p = cpi.MaxCharSize > 1;
4611 }
4612
4613 SelectObject (hdc, oldobj);
4614 ReleaseDC (dpyinfo->root_window, hdc);
4615 /* Fill out details in lf according to the font that was
4616 actually loaded. */
4617 lf.lfHeight = font->tm.tmInternalLeading - font->tm.tmHeight;
4618 lf.lfWidth = font->tm.tmMaxCharWidth;
4619 lf.lfWeight = font->tm.tmWeight;
4620 lf.lfItalic = font->tm.tmItalic;
4621 lf.lfCharSet = font->tm.tmCharSet;
4622 lf.lfPitchAndFamily = ((font->tm.tmPitchAndFamily & TMPF_FIXED_PITCH)
4623 ? VARIABLE_PITCH : FIXED_PITCH);
4624 lf.lfOutPrecision = ((font->tm.tmPitchAndFamily & TMPF_VECTOR)
4625 ? OUT_STROKE_PRECIS : OUT_STRING_PRECIS);
4626
4627 w32_cache_char_metrics (font);
4628 }
4629
4630 UNBLOCK_INPUT;
4631
4632 if (!ok)
4633 {
4634 w32_unload_font (dpyinfo, font);
4635 return (NULL);
4636 }
4637
4638 /* Find a free slot in the font table. */
4639 for (i = 0; i < dpyinfo->n_fonts; ++i)
4640 if (dpyinfo->font_table[i].name == NULL)
4641 break;
4642
4643 /* If no free slot found, maybe enlarge the font table. */
4644 if (i == dpyinfo->n_fonts
4645 && dpyinfo->n_fonts == dpyinfo->font_table_size)
4646 {
4647 int sz;
4648 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
4649 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
4650 dpyinfo->font_table
4651 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
4652 }
4653
4654 fontp = dpyinfo->font_table + i;
4655 if (i == dpyinfo->n_fonts)
4656 ++dpyinfo->n_fonts;
4657
4658 /* Now fill in the slots of *FONTP. */
4659 BLOCK_INPUT;
4660 bzero (fontp, sizeof (*fontp));
4661 fontp->font = font;
4662 fontp->font_idx = i;
4663 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
4664 bcopy (fontname, fontp->name, strlen (fontname) + 1);
4665
4666 if (lf.lfPitchAndFamily == FIXED_PITCH)
4667 {
4668 /* Fixed width font. */
4669 fontp->average_width = fontp->space_width = FONT_WIDTH (font);
4670 }
4671 else
4672 {
4673 wchar_t space = 32;
4674 XCharStruct* pcm;
4675 pcm = w32_per_char_metric (font, &space, ANSI_FONT);
4676 if (pcm)
4677 fontp->space_width = pcm->width;
4678 else
4679 fontp->space_width = FONT_WIDTH (font);
4680
4681 fontp->average_width = font->tm.tmAveCharWidth;
4682 }
4683
4684
4685 fontp->charset = -1;
4686 charset = xlfd_charset_of_font (fontname);
4687
4688 /* Cache the W32 codepage for a font. This makes w32_encode_char
4689 (called for every glyph during redisplay) much faster. */
4690 fontp->codepage = codepage;
4691
4692 /* Work out the font's full name. */
4693 full_name = (char *)xmalloc (100);
4694 if (full_name && w32_to_x_font (&lf, full_name, 100, charset))
4695 fontp->full_name = full_name;
4696 else
4697 {
4698 /* If all else fails - just use the name we used to load it. */
4699 xfree (full_name);
4700 fontp->full_name = fontp->name;
4701 }
4702
4703 fontp->size = FONT_WIDTH (font);
4704 fontp->height = FONT_HEIGHT (font);
4705
4706 /* The slot `encoding' specifies how to map a character
4707 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
4708 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
4709 (0:0x20..0x7F, 1:0xA0..0xFF,
4710 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
4711 2:0xA020..0xFF7F). For the moment, we don't know which charset
4712 uses this font. So, we set information in fontp->encoding_type
4713 which is never used by any charset. If mapping can't be
4714 decided, set FONT_ENCODING_NOT_DECIDED. */
4715
4716 /* SJIS fonts need to be set to type 4, all others seem to work as
4717 type FONT_ENCODING_NOT_DECIDED. */
4718 encoding = strrchr (fontp->name, '-');
4719 if (encoding && strnicmp (encoding+1, "sjis", 4) == 0)
4720 fontp->encoding_type = 4;
4721 else
4722 fontp->encoding_type = FONT_ENCODING_NOT_DECIDED;
4723
4724 /* The following three values are set to 0 under W32, which is
4725 what they get set to if XGetFontProperty fails under X. */
4726 fontp->baseline_offset = 0;
4727 fontp->relative_compose = 0;
4728 fontp->default_ascent = 0;
4729
4730 /* Set global flag fonts_changed_p to non-zero if the font loaded
4731 has a character with a smaller width than any other character
4732 before, or if the font loaded has a smaller height than any
4733 other font loaded before. If this happens, it will make a
4734 glyph matrix reallocation necessary. */
4735 fonts_changed_p |= x_compute_min_glyph_bounds (f);
4736 UNBLOCK_INPUT;
4737 return fontp;
4738 }
4739 }
4740
4741 /* Load font named FONTNAME of size SIZE for frame F, and return a
4742 pointer to the structure font_info while allocating it dynamically.
4743 If loading fails, return NULL. */
4744 struct font_info *
4745 w32_load_font (f,fontname,size)
4746 struct frame *f;
4747 char * fontname;
4748 int size;
4749 {
4750 Lisp_Object bdf_fonts;
4751 struct font_info *retval = NULL;
4752 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4753
4754 bdf_fonts = w32_list_bdf_fonts (build_string (fontname), 1);
4755
4756 while (!retval && CONSP (bdf_fonts))
4757 {
4758 char *bdf_name, *bdf_file;
4759 Lisp_Object bdf_pair;
4760 int i;
4761
4762 bdf_name = SDATA (XCAR (bdf_fonts));
4763 bdf_pair = Fassoc (XCAR (bdf_fonts), Vw32_bdf_filename_alist);
4764 bdf_file = SDATA (XCDR (bdf_pair));
4765
4766 // If the font is already loaded, do not load it again.
4767 for (i = 0; i < dpyinfo->n_fonts; i++)
4768 {
4769 if ((dpyinfo->font_table[i].name
4770 && !strcmp (dpyinfo->font_table[i].name, bdf_name))
4771 || (dpyinfo->font_table[i].full_name
4772 && !strcmp (dpyinfo->font_table[i].full_name, bdf_name)))
4773 return dpyinfo->font_table + i;
4774 }
4775
4776 retval = w32_load_bdf_font (f, bdf_name, size, bdf_file);
4777
4778 bdf_fonts = XCDR (bdf_fonts);
4779 }
4780
4781 if (retval)
4782 return retval;
4783
4784 return w32_load_system_font(f, fontname, size);
4785 }
4786
4787
4788 void
4789 w32_unload_font (dpyinfo, font)
4790 struct w32_display_info *dpyinfo;
4791 XFontStruct * font;
4792 {
4793 if (font)
4794 {
4795 if (font->per_char) xfree (font->per_char);
4796 if (font->bdf) w32_free_bdf_font (font->bdf);
4797
4798 if (font->hfont) DeleteObject(font->hfont);
4799 xfree (font);
4800 }
4801 }
4802
4803 /* The font conversion stuff between x and w32 */
4804
4805 /* X font string is as follows (from faces.el)
4806 * (let ((- "[-?]")
4807 * (foundry "[^-]+")
4808 * (family "[^-]+")
4809 * (weight "\\(bold\\|demibold\\|medium\\)") ; 1
4810 * (weight\? "\\([^-]*\\)") ; 1
4811 * (slant "\\([ior]\\)") ; 2
4812 * (slant\? "\\([^-]?\\)") ; 2
4813 * (swidth "\\([^-]*\\)") ; 3
4814 * (adstyle "[^-]*") ; 4
4815 * (pixelsize "[0-9]+")
4816 * (pointsize "[0-9][0-9]+")
4817 * (resx "[0-9][0-9]+")
4818 * (resy "[0-9][0-9]+")
4819 * (spacing "[cmp?*]")
4820 * (avgwidth "[0-9]+")
4821 * (registry "[^-]+")
4822 * (encoding "[^-]+")
4823 * )
4824 */
4825
4826 static LONG
4827 x_to_w32_weight (lpw)
4828 char * lpw;
4829 {
4830 if (!lpw) return (FW_DONTCARE);
4831
4832 if (stricmp (lpw,"heavy") == 0) return FW_HEAVY;
4833 else if (stricmp (lpw,"extrabold") == 0) return FW_EXTRABOLD;
4834 else if (stricmp (lpw,"bold") == 0) return FW_BOLD;
4835 else if (stricmp (lpw,"demibold") == 0) return FW_SEMIBOLD;
4836 else if (stricmp (lpw,"semibold") == 0) return FW_SEMIBOLD;
4837 else if (stricmp (lpw,"medium") == 0) return FW_MEDIUM;
4838 else if (stricmp (lpw,"normal") == 0) return FW_NORMAL;
4839 else if (stricmp (lpw,"light") == 0) return FW_LIGHT;
4840 else if (stricmp (lpw,"extralight") == 0) return FW_EXTRALIGHT;
4841 else if (stricmp (lpw,"thin") == 0) return FW_THIN;
4842 else
4843 return FW_DONTCARE;
4844 }
4845
4846
4847 static char *
4848 w32_to_x_weight (fnweight)
4849 int fnweight;
4850 {
4851 if (fnweight >= FW_HEAVY) return "heavy";
4852 if (fnweight >= FW_EXTRABOLD) return "extrabold";
4853 if (fnweight >= FW_BOLD) return "bold";
4854 if (fnweight >= FW_SEMIBOLD) return "demibold";
4855 if (fnweight >= FW_MEDIUM) return "medium";
4856 if (fnweight >= FW_NORMAL) return "normal";
4857 if (fnweight >= FW_LIGHT) return "light";
4858 if (fnweight >= FW_EXTRALIGHT) return "extralight";
4859 if (fnweight >= FW_THIN) return "thin";
4860 else
4861 return "*";
4862 }
4863
4864 static LONG
4865 x_to_w32_charset (lpcs)
4866 char * lpcs;
4867 {
4868 Lisp_Object this_entry, w32_charset;
4869 char *charset;
4870 int len = strlen (lpcs);
4871
4872 /* Support "*-#nnn" format for unknown charsets. */
4873 if (strncmp (lpcs, "*-#", 3) == 0)
4874 return atoi (lpcs + 3);
4875
4876 /* All Windows fonts qualify as unicode. */
4877 if (!strncmp (lpcs, "iso10646", 8))
4878 return DEFAULT_CHARSET;
4879
4880 /* Handle wildcards by ignoring them; eg. treat "big5*-*" as "big5". */
4881 charset = alloca (len + 1);
4882 strcpy (charset, lpcs);
4883 lpcs = strchr (charset, '*');
4884 if (lpcs)
4885 *lpcs = '\0';
4886
4887 /* Look through w32-charset-info-alist for the character set.
4888 Format of each entry is
4889 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
4890 */
4891 this_entry = Fassoc (build_string(charset), Vw32_charset_info_alist);
4892
4893 if (NILP(this_entry))
4894 {
4895 /* At startup, we want iso8859-1 fonts to come up properly. */
4896 if (stricmp(charset, "iso8859-1") == 0)
4897 return ANSI_CHARSET;
4898 else
4899 return DEFAULT_CHARSET;
4900 }
4901
4902 w32_charset = Fcar (Fcdr (this_entry));
4903
4904 /* Translate Lisp symbol to number. */
4905 if (EQ (w32_charset, Qw32_charset_ansi))
4906 return ANSI_CHARSET;
4907 if (EQ (w32_charset, Qw32_charset_symbol))
4908 return SYMBOL_CHARSET;
4909 if (EQ (w32_charset, Qw32_charset_shiftjis))
4910 return SHIFTJIS_CHARSET;
4911 if (EQ (w32_charset, Qw32_charset_hangeul))
4912 return HANGEUL_CHARSET;
4913 if (EQ (w32_charset, Qw32_charset_chinesebig5))
4914 return CHINESEBIG5_CHARSET;
4915 if (EQ (w32_charset, Qw32_charset_gb2312))
4916 return GB2312_CHARSET;
4917 if (EQ (w32_charset, Qw32_charset_oem))
4918 return OEM_CHARSET;
4919 #ifdef JOHAB_CHARSET
4920 if (EQ (w32_charset, Qw32_charset_johab))
4921 return JOHAB_CHARSET;
4922 if (EQ (w32_charset, Qw32_charset_easteurope))
4923 return EASTEUROPE_CHARSET;
4924 if (EQ (w32_charset, Qw32_charset_turkish))
4925 return TURKISH_CHARSET;
4926 if (EQ (w32_charset, Qw32_charset_baltic))
4927 return BALTIC_CHARSET;
4928 if (EQ (w32_charset, Qw32_charset_russian))
4929 return RUSSIAN_CHARSET;
4930 if (EQ (w32_charset, Qw32_charset_arabic))
4931 return ARABIC_CHARSET;
4932 if (EQ (w32_charset, Qw32_charset_greek))
4933 return GREEK_CHARSET;
4934 if (EQ (w32_charset, Qw32_charset_hebrew))
4935 return HEBREW_CHARSET;
4936 if (EQ (w32_charset, Qw32_charset_vietnamese))
4937 return VIETNAMESE_CHARSET;
4938 if (EQ (w32_charset, Qw32_charset_thai))
4939 return THAI_CHARSET;
4940 if (EQ (w32_charset, Qw32_charset_mac))
4941 return MAC_CHARSET;
4942 #endif /* JOHAB_CHARSET */
4943 #ifdef UNICODE_CHARSET
4944 if (EQ (w32_charset, Qw32_charset_unicode))
4945 return UNICODE_CHARSET;
4946 #endif
4947
4948 return DEFAULT_CHARSET;
4949 }
4950
4951
4952 static char *
4953 w32_to_x_charset (fncharset, matching)
4954 int fncharset;
4955 char *matching;
4956 {
4957 static char buf[32];
4958 Lisp_Object charset_type;
4959 int match_len = 0;
4960
4961 if (matching)
4962 {
4963 /* If fully specified, accept it as it is. Otherwise use a
4964 substring match. */
4965 char *wildcard = strchr (matching, '*');
4966 if (wildcard)
4967 *wildcard = '\0';
4968 else if (strchr (matching, '-'))
4969 return matching;
4970
4971 match_len = strlen (matching);
4972 }
4973
4974 switch (fncharset)
4975 {
4976 case ANSI_CHARSET:
4977 /* Handle startup case of w32-charset-info-alist not
4978 being set up yet. */
4979 if (NILP(Vw32_charset_info_alist))
4980 return "iso8859-1";
4981 charset_type = Qw32_charset_ansi;
4982 break;
4983 case DEFAULT_CHARSET:
4984 charset_type = Qw32_charset_default;
4985 break;
4986 case SYMBOL_CHARSET:
4987 charset_type = Qw32_charset_symbol;
4988 break;
4989 case SHIFTJIS_CHARSET:
4990 charset_type = Qw32_charset_shiftjis;
4991 break;
4992 case HANGEUL_CHARSET:
4993 charset_type = Qw32_charset_hangeul;
4994 break;
4995 case GB2312_CHARSET:
4996 charset_type = Qw32_charset_gb2312;
4997 break;
4998 case CHINESEBIG5_CHARSET:
4999 charset_type = Qw32_charset_chinesebig5;
5000 break;
5001 case OEM_CHARSET:
5002 charset_type = Qw32_charset_oem;
5003 break;
5004
5005 /* More recent versions of Windows (95 and NT4.0) define more
5006 character sets. */
5007 #ifdef EASTEUROPE_CHARSET
5008 case EASTEUROPE_CHARSET:
5009 charset_type = Qw32_charset_easteurope;
5010 break;
5011 case TURKISH_CHARSET:
5012 charset_type = Qw32_charset_turkish;
5013 break;
5014 case BALTIC_CHARSET:
5015 charset_type = Qw32_charset_baltic;
5016 break;
5017 case RUSSIAN_CHARSET:
5018 charset_type = Qw32_charset_russian;
5019 break;
5020 case ARABIC_CHARSET:
5021 charset_type = Qw32_charset_arabic;
5022 break;
5023 case GREEK_CHARSET:
5024 charset_type = Qw32_charset_greek;
5025 break;
5026 case HEBREW_CHARSET:
5027 charset_type = Qw32_charset_hebrew;
5028 break;
5029 case VIETNAMESE_CHARSET:
5030 charset_type = Qw32_charset_vietnamese;
5031 break;
5032 case THAI_CHARSET:
5033 charset_type = Qw32_charset_thai;
5034 break;
5035 case MAC_CHARSET:
5036 charset_type = Qw32_charset_mac;
5037 break;
5038 case JOHAB_CHARSET:
5039 charset_type = Qw32_charset_johab;
5040 break;
5041 #endif
5042
5043 #ifdef UNICODE_CHARSET
5044 case UNICODE_CHARSET:
5045 charset_type = Qw32_charset_unicode;
5046 break;
5047 #endif
5048 default:
5049 /* Encode numerical value of unknown charset. */
5050 sprintf (buf, "*-#%u", fncharset);
5051 return buf;
5052 }
5053
5054 {
5055 Lisp_Object rest;
5056 char * best_match = NULL;
5057 int matching_found = 0;
5058
5059 /* Look through w32-charset-info-alist for the character set.
5060 Prefer ISO codepages, and prefer lower numbers in the ISO
5061 range. Only return charsets for codepages which are installed.
5062
5063 Format of each entry is
5064 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
5065 */
5066 for (rest = Vw32_charset_info_alist; CONSP (rest); rest = XCDR (rest))
5067 {
5068 char * x_charset;
5069 Lisp_Object w32_charset;
5070 Lisp_Object codepage;
5071
5072 Lisp_Object this_entry = XCAR (rest);
5073
5074 /* Skip invalid entries in alist. */
5075 if (!CONSP (this_entry) || !STRINGP (XCAR (this_entry))
5076 || !CONSP (XCDR (this_entry))
5077 || !SYMBOLP (XCAR (XCDR (this_entry))))
5078 continue;
5079
5080 x_charset = SDATA (XCAR (this_entry));
5081 w32_charset = XCAR (XCDR (this_entry));
5082 codepage = XCDR (XCDR (this_entry));
5083
5084 /* Look for Same charset and a valid codepage (or non-int
5085 which means ignore). */
5086 if (EQ (w32_charset, charset_type)
5087 && (!INTEGERP (codepage) || XINT (codepage) == CP_DEFAULT
5088 || IsValidCodePage (XINT (codepage))))
5089 {
5090 /* If we don't have a match already, then this is the
5091 best. */
5092 if (!best_match)
5093 {
5094 best_match = x_charset;
5095 if (matching && !strnicmp (x_charset, matching, match_len))
5096 matching_found = 1;
5097 }
5098 /* If we already found a match for MATCHING, then
5099 only consider other matches. */
5100 else if (matching_found
5101 && strnicmp (x_charset, matching, match_len))
5102 continue;
5103 /* If this matches what we want, and the best so far doesn't,
5104 then this is better. */
5105 else if (!matching_found && matching
5106 && !strnicmp (x_charset, matching, match_len))
5107 {
5108 best_match = x_charset;
5109 matching_found = 1;
5110 }
5111 /* If this is fully specified, and the best so far isn't,
5112 then this is better. */
5113 else if ((!strchr (best_match, '-') && strchr (x_charset, '-'))
5114 /* If this is an ISO codepage, and the best so far isn't,
5115 then this is better, but only if it fully specifies the
5116 encoding. */
5117 || (strnicmp (best_match, "iso", 3) != 0
5118 && strnicmp (x_charset, "iso", 3) == 0
5119 && strchr (x_charset, '-')))
5120 best_match = x_charset;
5121 /* If both are ISO8859 codepages, choose the one with the
5122 lowest number in the encoding field. */
5123 else if (strnicmp (best_match, "iso8859-", 8) == 0
5124 && strnicmp (x_charset, "iso8859-", 8) == 0)
5125 {
5126 int best_enc = atoi (best_match + 8);
5127 int this_enc = atoi (x_charset + 8);
5128 if (this_enc > 0 && this_enc < best_enc)
5129 best_match = x_charset;
5130 }
5131 }
5132 }
5133
5134 /* If no match, encode the numeric value. */
5135 if (!best_match)
5136 {
5137 sprintf (buf, "*-#%u", fncharset);
5138 return buf;
5139 }
5140
5141 strncpy (buf, best_match, 31);
5142 /* If the charset is not fully specified, put -0 on the end. */
5143 if (!strchr (best_match, '-'))
5144 {
5145 int pos = strlen (best_match);
5146 /* Charset specifiers shouldn't be very long. If it is a made
5147 up one, truncating it should not do any harm since it isn't
5148 recognized anyway. */
5149 if (pos > 29)
5150 pos = 29;
5151 strcpy (buf + pos, "-0");
5152 }
5153 buf[31] = '\0';
5154 return buf;
5155 }
5156 }
5157
5158
5159 /* Return all the X charsets that map to a font. */
5160 static Lisp_Object
5161 w32_to_all_x_charsets (fncharset)
5162 int fncharset;
5163 {
5164 static char buf[32];
5165 Lisp_Object charset_type;
5166 Lisp_Object retval = Qnil;
5167
5168 switch (fncharset)
5169 {
5170 case ANSI_CHARSET:
5171 /* Handle startup case of w32-charset-info-alist not
5172 being set up yet. */
5173 if (NILP(Vw32_charset_info_alist))
5174 return Fcons (build_string ("iso8859-1"), Qnil);
5175
5176 charset_type = Qw32_charset_ansi;
5177 break;
5178 case DEFAULT_CHARSET:
5179 charset_type = Qw32_charset_default;
5180 break;
5181 case SYMBOL_CHARSET:
5182 charset_type = Qw32_charset_symbol;
5183 break;
5184 case SHIFTJIS_CHARSET:
5185 charset_type = Qw32_charset_shiftjis;
5186 break;
5187 case HANGEUL_CHARSET:
5188 charset_type = Qw32_charset_hangeul;
5189 break;
5190 case GB2312_CHARSET:
5191 charset_type = Qw32_charset_gb2312;
5192 break;
5193 case CHINESEBIG5_CHARSET:
5194 charset_type = Qw32_charset_chinesebig5;
5195 break;
5196 case OEM_CHARSET:
5197 charset_type = Qw32_charset_oem;
5198 break;
5199
5200 /* More recent versions of Windows (95 and NT4.0) define more
5201 character sets. */
5202 #ifdef EASTEUROPE_CHARSET
5203 case EASTEUROPE_CHARSET:
5204 charset_type = Qw32_charset_easteurope;
5205 break;
5206 case TURKISH_CHARSET:
5207 charset_type = Qw32_charset_turkish;
5208 break;
5209 case BALTIC_CHARSET:
5210 charset_type = Qw32_charset_baltic;
5211 break;
5212 case RUSSIAN_CHARSET:
5213 charset_type = Qw32_charset_russian;
5214 break;
5215 case ARABIC_CHARSET:
5216 charset_type = Qw32_charset_arabic;
5217 break;
5218 case GREEK_CHARSET:
5219 charset_type = Qw32_charset_greek;
5220 break;
5221 case HEBREW_CHARSET:
5222 charset_type = Qw32_charset_hebrew;
5223 break;
5224 case VIETNAMESE_CHARSET:
5225 charset_type = Qw32_charset_vietnamese;
5226 break;
5227 case THAI_CHARSET:
5228 charset_type = Qw32_charset_thai;
5229 break;
5230 case MAC_CHARSET:
5231 charset_type = Qw32_charset_mac;
5232 break;
5233 case JOHAB_CHARSET:
5234 charset_type = Qw32_charset_johab;
5235 break;
5236 #endif
5237
5238 #ifdef UNICODE_CHARSET
5239 case UNICODE_CHARSET:
5240 charset_type = Qw32_charset_unicode;
5241 break;
5242 #endif
5243 default:
5244 /* Encode numerical value of unknown charset. */
5245 sprintf (buf, "*-#%u", fncharset);
5246 return Fcons (build_string (buf), Qnil);
5247 }
5248
5249 {
5250 Lisp_Object rest;
5251 /* Look through w32-charset-info-alist for the character set.
5252 Only return fully specified charsets for codepages which are
5253 installed.
5254
5255 Format of each entry in Vw32_charset_info_alist is
5256 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
5257 */
5258 for (rest = Vw32_charset_info_alist; CONSP (rest); rest = XCDR (rest))
5259 {
5260 Lisp_Object x_charset;
5261 Lisp_Object w32_charset;
5262 Lisp_Object codepage;
5263
5264 Lisp_Object this_entry = XCAR (rest);
5265
5266 /* Skip invalid entries in alist. */
5267 if (!CONSP (this_entry) || !STRINGP (XCAR (this_entry))
5268 || !CONSP (XCDR (this_entry))
5269 || !SYMBOLP (XCAR (XCDR (this_entry))))
5270 continue;
5271
5272 x_charset = XCAR (this_entry);
5273 w32_charset = XCAR (XCDR (this_entry));
5274 codepage = XCDR (XCDR (this_entry));
5275
5276 if (!strchr (SDATA (x_charset), '-'))
5277 continue;
5278
5279 /* Look for Same charset and a valid codepage (or non-int
5280 which means ignore). */
5281 if (EQ (w32_charset, charset_type)
5282 && (!INTEGERP (codepage) || XINT (codepage) == CP_DEFAULT
5283 || IsValidCodePage (XINT (codepage))))
5284 {
5285 retval = Fcons (x_charset, retval);
5286 }
5287 }
5288
5289 /* If no match, encode the numeric value. */
5290 if (NILP (retval))
5291 {
5292 sprintf (buf, "*-#%u", fncharset);
5293 return Fcons (build_string (buf), Qnil);
5294 }
5295
5296 return retval;
5297 }
5298 }
5299
5300 /* Get the Windows codepage corresponding to the specified font. The
5301 charset info in the font name is used to look up
5302 w32-charset-to-codepage-alist. */
5303 int
5304 w32_codepage_for_font (char *fontname)
5305 {
5306 Lisp_Object codepage, entry;
5307 char *charset_str, *charset, *end;
5308
5309 /* Extract charset part of font string. */
5310 charset = xlfd_charset_of_font (fontname);
5311
5312 if (!charset)
5313 return CP_UNKNOWN;
5314
5315 charset_str = (char *) alloca (strlen (charset) + 1);
5316 strcpy (charset_str, charset);
5317
5318 #if 0
5319 /* Remove leading "*-". */
5320 if (strncmp ("*-", charset_str, 2) == 0)
5321 charset = charset_str + 2;
5322 else
5323 #endif
5324 charset = charset_str;
5325
5326 /* Stop match at wildcard (including preceding '-'). */
5327 if (end = strchr (charset, '*'))
5328 {
5329 if (end > charset && *(end-1) == '-')
5330 end--;
5331 *end = '\0';
5332 }
5333
5334 if (!strcmp (charset, "iso10646"))
5335 return CP_UNICODE;
5336
5337 if (NILP (Vw32_charset_info_alist))
5338 return CP_DEFAULT;
5339
5340 entry = Fassoc (build_string(charset), Vw32_charset_info_alist);
5341 if (NILP (entry))
5342 return CP_UNKNOWN;
5343
5344 codepage = Fcdr (Fcdr (entry));
5345
5346 if (NILP (codepage))
5347 return CP_8BIT;
5348 else if (XFASTINT (codepage) == XFASTINT (Qt))
5349 return CP_UNICODE;
5350 else if (INTEGERP (codepage))
5351 return XINT (codepage);
5352 else
5353 return CP_UNKNOWN;
5354 }
5355
5356
5357 static BOOL
5358 w32_to_x_font (lplogfont, lpxstr, len, specific_charset)
5359 LOGFONT * lplogfont;
5360 char * lpxstr;
5361 int len;
5362 char * specific_charset;
5363 {
5364 char* fonttype;
5365 char *fontname;
5366 char height_pixels[8];
5367 char height_dpi[8];
5368 char width_pixels[8];
5369 char *fontname_dash;
5370 int display_resy = (int) one_w32_display_info.resy;
5371 int display_resx = (int) one_w32_display_info.resx;
5372 struct coding_system coding;
5373
5374 if (!lpxstr) abort ();
5375
5376 if (!lplogfont)
5377 return FALSE;
5378
5379 if (lplogfont->lfOutPrecision == OUT_STRING_PRECIS)
5380 fonttype = "raster";
5381 else if (lplogfont->lfOutPrecision == OUT_STROKE_PRECIS)
5382 fonttype = "outline";
5383 else
5384 fonttype = "unknown";
5385
5386 setup_coding_system (Fcheck_coding_system (Vlocale_coding_system),
5387 &coding);
5388 coding.src_multibyte = 0;
5389 coding.dst_multibyte = 1;
5390 coding.mode |= CODING_MODE_LAST_BLOCK;
5391 /* We explicitely disable composition handling because selection
5392 data should not contain any composition sequence. */
5393 coding.common_flags &= ~CODING_ANNOTATION_MASK;
5394
5395 coding.dst_bytes = LF_FACESIZE * 2;
5396 coding.destination = (unsigned char *) xmalloc (coding.dst_bytes + 1);
5397 decode_coding_c_string (&coding, lplogfont->lfFaceName,
5398 strlen(lplogfont->lfFaceName), Qnil);
5399 fontname = coding.destination;
5400
5401 *(fontname + coding.produced) = '\0';
5402
5403 /* Replace dashes with underscores so the dashes are not
5404 misinterpreted. */
5405 fontname_dash = fontname;
5406 while (fontname_dash = strchr (fontname_dash, '-'))
5407 *fontname_dash = '_';
5408
5409 if (lplogfont->lfHeight)
5410 {
5411 sprintf (height_pixels, "%u", abs (lplogfont->lfHeight));
5412 sprintf (height_dpi, "%u",
5413 abs (lplogfont->lfHeight) * 720 / display_resy);
5414 }
5415 else
5416 {
5417 strcpy (height_pixels, "*");
5418 strcpy (height_dpi, "*");
5419 }
5420
5421 #if 0 /* Never put the width in the xfld. It fails on fonts with
5422 double-width characters. */
5423 if (lplogfont->lfWidth)
5424 sprintf (width_pixels, "%u", lplogfont->lfWidth * 10);
5425 else
5426 #endif
5427 strcpy (width_pixels, "*");
5428
5429 _snprintf (lpxstr, len - 1,
5430 "-%s-%s-%s-%c-normal-normal-%s-%s-%d-%d-%c-%s-%s",
5431 fonttype, /* foundry */
5432 fontname, /* family */
5433 w32_to_x_weight (lplogfont->lfWeight), /* weight */
5434 lplogfont->lfItalic?'i':'r', /* slant */
5435 /* setwidth name */
5436 /* add style name */
5437 height_pixels, /* pixel size */
5438 height_dpi, /* point size */
5439 display_resx, /* resx */
5440 display_resy, /* resy */
5441 ((lplogfont->lfPitchAndFamily & 0x3) == VARIABLE_PITCH)
5442 ? 'p' : 'c', /* spacing */
5443 width_pixels, /* avg width */
5444 w32_to_x_charset (lplogfont->lfCharSet, specific_charset)
5445 /* charset registry and encoding */
5446 );
5447
5448 lpxstr[len - 1] = 0; /* just to be sure */
5449 return (TRUE);
5450 }
5451
5452 static BOOL
5453 x_to_w32_font (lpxstr, lplogfont)
5454 char * lpxstr;
5455 LOGFONT * lplogfont;
5456 {
5457 struct coding_system coding;
5458
5459 if (!lplogfont) return (FALSE);
5460
5461 memset (lplogfont, 0, sizeof (*lplogfont));
5462
5463 /* Set default value for each field. */
5464 #if 1
5465 lplogfont->lfOutPrecision = OUT_DEFAULT_PRECIS;
5466 lplogfont->lfClipPrecision = CLIP_DEFAULT_PRECIS;
5467 lplogfont->lfQuality = DEFAULT_QUALITY;
5468 #else
5469 /* go for maximum quality */
5470 lplogfont->lfOutPrecision = OUT_STROKE_PRECIS;
5471 lplogfont->lfClipPrecision = CLIP_STROKE_PRECIS;
5472 lplogfont->lfQuality = PROOF_QUALITY;
5473 #endif
5474
5475 lplogfont->lfCharSet = DEFAULT_CHARSET;
5476 lplogfont->lfWeight = FW_DONTCARE;
5477 lplogfont->lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
5478
5479 if (!lpxstr)
5480 return FALSE;
5481
5482 /* Provide a simple escape mechanism for specifying Windows font names
5483 * directly -- if font spec does not beginning with '-', assume this
5484 * format:
5485 * "<font name>[:height in pixels[:width in pixels[:weight]]]"
5486 */
5487
5488 if (*lpxstr == '-')
5489 {
5490 int fields, tem;
5491 char name[50], weight[20], slant, pitch, pixels[10], height[10],
5492 width[10], resy[10], remainder[50];
5493 char * encoding;
5494 int dpi = (int) one_w32_display_info.resy;
5495
5496 fields = sscanf (lpxstr,
5497 "-%*[^-]-%49[^-]-%19[^-]-%c-%*[^-]-%*[^-]-%9[^-]-%9[^-]-%*[^-]-%9[^-]-%c-%9[^-]-%49s",
5498 name, weight, &slant, pixels, height, resy, &pitch, width, remainder);
5499 if (fields == EOF)
5500 return (FALSE);
5501
5502 /* In the general case when wildcards cover more than one field,
5503 we don't know which field is which, so don't fill any in.
5504 However, we need to cope with this particular form, which is
5505 generated by font_list_1 (invoked by try_font_list):
5506 "-raster-6x10-*-gb2312*-*"
5507 and make sure to correctly parse the charset field. */
5508 if (fields == 3)
5509 {
5510 fields = sscanf (lpxstr,
5511 "-%*[^-]-%49[^-]-*-%49s",
5512 name, remainder);
5513 }
5514 else if (fields < 9)
5515 {
5516 fields = 0;
5517 remainder[0] = 0;
5518 }
5519
5520 if (fields > 0 && name[0] != '*')
5521 {
5522 Lisp_Object string = build_string (name);
5523 setup_coding_system
5524 (Fcheck_coding_system (Vlocale_coding_system), &coding);
5525 coding.mode |= (CODING_MODE_SAFE_ENCODING | CODING_MODE_LAST_BLOCK);
5526 /* Disable composition/charset annotation. */
5527 coding.common_flags &= ~CODING_ANNOTATION_MASK;
5528 coding.dst_bytes = SCHARS (string) * 2;
5529
5530 coding.destination = (unsigned char *) xmalloc (coding.dst_bytes);
5531 encode_coding_object (&coding, string, 0, 0,
5532 SCHARS (string), SBYTES (string), Qnil);
5533 if (coding.produced >= LF_FACESIZE)
5534 coding.produced = LF_FACESIZE - 1;
5535
5536 coding.destination[coding.produced] = '\0';
5537
5538 strcpy (lplogfont->lfFaceName, coding.destination);
5539 xfree (coding.destination);
5540 }
5541 else
5542 {
5543 lplogfont->lfFaceName[0] = '\0';
5544 }
5545
5546 fields--;
5547
5548 lplogfont->lfWeight = x_to_w32_weight ((fields > 0 ? weight : ""));
5549
5550 fields--;
5551
5552 lplogfont->lfItalic = (fields > 0 && slant == 'i');
5553
5554 fields--;
5555
5556 if (fields > 0 && pixels[0] != '*')
5557 lplogfont->lfHeight = atoi (pixels);
5558
5559 fields--;
5560 fields--;
5561 if (fields > 0 && resy[0] != '*')
5562 {
5563 tem = atoi (resy);
5564 if (tem > 0) dpi = tem;
5565 }
5566
5567 if (fields > -1 && lplogfont->lfHeight == 0 && height[0] != '*')
5568 lplogfont->lfHeight = atoi (height) * dpi / 720;
5569
5570 if (fields > 0)
5571 lplogfont->lfPitchAndFamily =
5572 (fields > 0 && pitch == 'p') ? VARIABLE_PITCH : FIXED_PITCH;
5573
5574 fields--;
5575
5576 if (fields > 0 && width[0] != '*')
5577 lplogfont->lfWidth = atoi (width) / 10;
5578
5579 fields--;
5580
5581 /* Strip the trailing '-' if present. (it shouldn't be, as it
5582 fails the test against xlfd-tight-regexp in fontset.el). */
5583 {
5584 int len = strlen (remainder);
5585 if (len > 0 && remainder[len-1] == '-')
5586 remainder[len-1] = 0;
5587 }
5588 encoding = remainder;
5589 #if 0
5590 if (strncmp (encoding, "*-", 2) == 0)
5591 encoding += 2;
5592 #endif
5593 lplogfont->lfCharSet = x_to_w32_charset (encoding);
5594 }
5595 else
5596 {
5597 int fields;
5598 char name[100], height[10], width[10], weight[20];
5599
5600 fields = sscanf (lpxstr,
5601 "%99[^:]:%9[^:]:%9[^:]:%19s",
5602 name, height, width, weight);
5603
5604 if (fields == EOF) return (FALSE);
5605
5606 if (fields > 0)
5607 {
5608 strncpy (lplogfont->lfFaceName,name, LF_FACESIZE);
5609 lplogfont->lfFaceName[LF_FACESIZE-1] = 0;
5610 }
5611 else
5612 {
5613 lplogfont->lfFaceName[0] = 0;
5614 }
5615
5616 fields--;
5617
5618 if (fields > 0)
5619 lplogfont->lfHeight = atoi (height);
5620
5621 fields--;
5622
5623 if (fields > 0)
5624 lplogfont->lfWidth = atoi (width);
5625
5626 fields--;
5627
5628 lplogfont->lfWeight = x_to_w32_weight ((fields > 0 ? weight : ""));
5629 }
5630
5631 /* This makes TrueType fonts work better. */
5632 lplogfont->lfHeight = - abs (lplogfont->lfHeight);
5633
5634 return (TRUE);
5635 }
5636
5637 /* Strip the pixel height and point height from the given xlfd, and
5638 return the pixel height. If no pixel height is specified, calculate
5639 one from the point height, or if that isn't defined either, return
5640 0 (which usually signifies a scalable font).
5641 */
5642 static int
5643 xlfd_strip_height (char *fontname)
5644 {
5645 int pixel_height, field_number;
5646 char *read_from, *write_to;
5647
5648 xassert (fontname);
5649
5650 pixel_height = field_number = 0;
5651 write_to = NULL;
5652
5653 /* Look for height fields. */
5654 for (read_from = fontname; *read_from; read_from++)
5655 {
5656 if (*read_from == '-')
5657 {
5658 field_number++;
5659 if (field_number == 7) /* Pixel height. */
5660 {
5661 read_from++;
5662 write_to = read_from;
5663
5664 /* Find end of field. */
5665 for (;*read_from && *read_from != '-'; read_from++)
5666 ;
5667
5668 /* Split the fontname at end of field. */
5669 if (*read_from)
5670 {
5671 *read_from = '\0';
5672 read_from++;
5673 }
5674 pixel_height = atoi (write_to);
5675 /* Blank out field. */
5676 if (read_from > write_to)
5677 {
5678 *write_to = '-';
5679 write_to++;
5680 }
5681 /* If the pixel height field is at the end (partial xlfd),
5682 return now. */
5683 else
5684 return pixel_height;
5685
5686 /* If we got a pixel height, the point height can be
5687 ignored. Just blank it out and break now. */
5688 if (pixel_height)
5689 {
5690 /* Find end of point size field. */
5691 for (; *read_from && *read_from != '-'; read_from++)
5692 ;
5693
5694 if (*read_from)
5695 read_from++;
5696
5697 /* Blank out the point size field. */
5698 if (read_from > write_to)
5699 {
5700 *write_to = '-';
5701 write_to++;
5702 }
5703 else
5704 return pixel_height;
5705
5706 break;
5707 }
5708 /* If the point height is already blank, break now. */
5709 if (*read_from == '-')
5710 {
5711 read_from++;
5712 break;
5713 }
5714 }
5715 else if (field_number == 8)
5716 {
5717 /* If we didn't get a pixel height, try to get the point
5718 height and convert that. */
5719 int point_size;
5720 char *point_size_start = read_from++;
5721
5722 /* Find end of field. */
5723 for (; *read_from && *read_from != '-'; read_from++)
5724 ;
5725
5726 if (*read_from)
5727 {
5728 *read_from = '\0';
5729 read_from++;
5730 }
5731
5732 point_size = atoi (point_size_start);
5733
5734 /* Convert to pixel height. */
5735 pixel_height = point_size
5736 * one_w32_display_info.height_in / 720;
5737
5738 /* Blank out this field and break. */
5739 *write_to = '-';
5740 write_to++;
5741 break;
5742 }
5743 }
5744 }
5745
5746 /* Shift the rest of the font spec into place. */
5747 if (write_to && read_from > write_to)
5748 {
5749 for (; *read_from; read_from++, write_to++)
5750 *write_to = *read_from;
5751 *write_to = '\0';
5752 }
5753
5754 return pixel_height;
5755 }
5756
5757 /* Assume parameter 1 is fully qualified, no wildcards. */
5758 static BOOL
5759 w32_font_match (fontname, pattern)
5760 char * fontname;
5761 char * pattern;
5762 {
5763 char *ptr;
5764 char *font_name_copy;
5765 char *regex = alloca (strlen (pattern) * 2 + 3);
5766
5767 font_name_copy = alloca (strlen (fontname) + 1);
5768 strcpy (font_name_copy, fontname);
5769
5770 ptr = regex;
5771 *ptr++ = '^';
5772
5773 /* Turn pattern into a regexp and do a regexp match. */
5774 for (; *pattern; pattern++)
5775 {
5776 if (*pattern == '?')
5777 *ptr++ = '.';
5778 else if (*pattern == '*')
5779 {
5780 *ptr++ = '.';
5781 *ptr++ = '*';
5782 }
5783 else
5784 *ptr++ = *pattern;
5785 }
5786 *ptr = '$';
5787 *(ptr + 1) = '\0';
5788
5789 /* Strip out font heights and compare them seperately, since
5790 rounding error can cause mismatches. This also allows a
5791 comparison between a font that declares only a pixel height and a
5792 pattern that declares the point height.
5793 */
5794 {
5795 int font_height, pattern_height;
5796
5797 font_height = xlfd_strip_height (font_name_copy);
5798 pattern_height = xlfd_strip_height (regex);
5799
5800 /* Compare now, and don't bother doing expensive regexp matching
5801 if the heights differ. */
5802 if (font_height && pattern_height && (font_height != pattern_height))
5803 return FALSE;
5804 }
5805
5806 return (fast_string_match_ignore_case (build_string (regex),
5807 build_string(font_name_copy)) >= 0);
5808 }
5809
5810 /* Callback functions, and a structure holding info they need, for
5811 listing system fonts on W32. We need one set of functions to do the
5812 job properly, but these don't work on NT 3.51 and earlier, so we
5813 have a second set which don't handle character sets properly to
5814 fall back on.
5815
5816 In both cases, there are two passes made. The first pass gets one
5817 font from each family, the second pass lists all the fonts from
5818 each family. */
5819
5820 typedef struct enumfont_t
5821 {
5822 HDC hdc;
5823 int numFonts;
5824 LOGFONT logfont;
5825 XFontStruct *size_ref;
5826 Lisp_Object pattern;
5827 Lisp_Object list;
5828 } enumfont_t;
5829
5830
5831 static void
5832 enum_font_maybe_add_to_list (enumfont_t *, LOGFONT *, char *, Lisp_Object);
5833
5834
5835 static int CALLBACK
5836 enum_font_cb2 (lplf, lptm, FontType, lpef)
5837 ENUMLOGFONT * lplf;
5838 NEWTEXTMETRIC * lptm;
5839 int FontType;
5840 enumfont_t * lpef;
5841 {
5842 /* Ignore struck out and underlined versions of fonts. */
5843 if (lplf->elfLogFont.lfStrikeOut || lplf->elfLogFont.lfUnderline)
5844 return 1;
5845
5846 /* Only return fonts with names starting with @ if they were
5847 explicitly specified, since Microsoft uses an initial @ to
5848 denote fonts for vertical writing, without providing a more
5849 convenient way of identifying them. */
5850 if (lplf->elfLogFont.lfFaceName[0] == '@'
5851 && lpef->logfont.lfFaceName[0] != '@')
5852 return 1;
5853
5854 /* Check that the character set matches if it was specified */
5855 if (lpef->logfont.lfCharSet != DEFAULT_CHARSET &&
5856 lplf->elfLogFont.lfCharSet != lpef->logfont.lfCharSet)
5857 return 1;
5858
5859 if (FontType == RASTER_FONTTYPE)
5860 {
5861 /* DBCS raster fonts have problems displaying, so skip them. */
5862 int charset = lplf->elfLogFont.lfCharSet;
5863 if (charset == SHIFTJIS_CHARSET
5864 || charset == HANGEUL_CHARSET
5865 || charset == CHINESEBIG5_CHARSET
5866 || charset == GB2312_CHARSET
5867 #ifdef JOHAB_CHARSET
5868 || charset == JOHAB_CHARSET
5869 #endif
5870 )
5871 return 1;
5872 }
5873
5874 {
5875 char buf[100];
5876 Lisp_Object width = Qnil;
5877 Lisp_Object charset_list = Qnil;
5878 char *charset = NULL;
5879
5880 /* Truetype fonts do not report their true metrics until loaded */
5881 if (FontType != RASTER_FONTTYPE)
5882 {
5883 if (!NILP (lpef->pattern))
5884 {
5885 /* Scalable fonts are as big as you want them to be. */
5886 lplf->elfLogFont.lfHeight = lpef->logfont.lfHeight;
5887 lplf->elfLogFont.lfWidth = lpef->logfont.lfWidth;
5888 width = make_number (lpef->logfont.lfWidth);
5889 }
5890 else
5891 {
5892 lplf->elfLogFont.lfHeight = 0;
5893 lplf->elfLogFont.lfWidth = 0;
5894 }
5895 }
5896
5897 /* Make sure the height used here is the same as everywhere
5898 else (ie character height, not cell height). */
5899 if (lplf->elfLogFont.lfHeight > 0)
5900 {
5901 /* lptm can be trusted for RASTER fonts, but not scalable ones. */
5902 if (FontType == RASTER_FONTTYPE)
5903 lplf->elfLogFont.lfHeight = lptm->tmInternalLeading - lptm->tmHeight;
5904 else
5905 lplf->elfLogFont.lfHeight = -lplf->elfLogFont.lfHeight;
5906 }
5907
5908 if (!NILP (lpef->pattern))
5909 {
5910 charset = xlfd_charset_of_font (SDATA (lpef->pattern));
5911
5912 /* We already checked charsets above, but DEFAULT_CHARSET
5913 slipped through. So only allow exact matches for DEFAULT_CHARSET. */
5914 if (charset
5915 && strncmp (charset, "*-*", 3) != 0
5916 && lpef->logfont.lfCharSet == DEFAULT_CHARSET
5917 && strcmp (charset, w32_to_x_charset (DEFAULT_CHARSET, NULL)) != 0)
5918 return 1;
5919 }
5920
5921 if (charset)
5922 charset_list = Fcons (build_string (charset), Qnil);
5923 else
5924 /* Always prefer unicode. */
5925 charset_list
5926 = Fcons (build_string ("iso10646-1"),
5927 w32_to_all_x_charsets (lplf->elfLogFont.lfCharSet));
5928
5929 /* Loop through the charsets. */
5930 for ( ; CONSP (charset_list); charset_list = Fcdr (charset_list))
5931 {
5932 Lisp_Object this_charset = Fcar (charset_list);
5933 charset = SDATA (this_charset);
5934
5935 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5936 charset, width);
5937
5938 /* List bold and italic variations if w32-enable-synthesized-fonts
5939 is non-nil and this is a plain font. */
5940 if (w32_enable_synthesized_fonts
5941 && lplf->elfLogFont.lfWeight == FW_NORMAL
5942 && lplf->elfLogFont.lfItalic == FALSE)
5943 {
5944 /* bold. */
5945 lplf->elfLogFont.lfWeight = FW_BOLD;
5946 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5947 charset, width);
5948 /* bold italic. */
5949 lplf->elfLogFont.lfItalic = TRUE;
5950 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5951 charset, width);
5952 /* italic. */
5953 lplf->elfLogFont.lfWeight = FW_NORMAL;
5954 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5955 charset, width);
5956 }
5957 }
5958 }
5959
5960 return 1;
5961 }
5962
5963 static void
5964 enum_font_maybe_add_to_list (lpef, logfont, match_charset, width)
5965 enumfont_t * lpef;
5966 LOGFONT * logfont;
5967 char * match_charset;
5968 Lisp_Object width;
5969 {
5970 char buf[100];
5971
5972 if (!w32_to_x_font (logfont, buf, 100, match_charset))
5973 return;
5974
5975 if (NILP (lpef->pattern)
5976 || w32_font_match (buf, SDATA (lpef->pattern)))
5977 {
5978 /* Check if we already listed this font. This may happen if
5979 w32_enable_synthesized_fonts is non-nil, and there are real
5980 bold and italic versions of the font. */
5981 Lisp_Object font_name = build_string (buf);
5982 if (NILP (Fmember (font_name, lpef->list)))
5983 {
5984 Lisp_Object entry = Fcons (font_name, width);
5985 lpef->list = Fcons (entry, lpef->list);
5986 lpef->numFonts++;
5987 }
5988 }
5989 }
5990
5991
5992 static int CALLBACK
5993 enum_font_cb1 (lplf, lptm, FontType, lpef)
5994 ENUMLOGFONT * lplf;
5995 NEWTEXTMETRIC * lptm;
5996 int FontType;
5997 enumfont_t * lpef;
5998 {
5999 return EnumFontFamilies (lpef->hdc,
6000 lplf->elfLogFont.lfFaceName,
6001 (FONTENUMPROC) enum_font_cb2,
6002 (LPARAM) lpef);
6003 }
6004
6005
6006 static int CALLBACK
6007 enum_fontex_cb2 (lplf, lptm, font_type, lpef)
6008 ENUMLOGFONTEX * lplf;
6009 NEWTEXTMETRICEX * lptm;
6010 int font_type;
6011 enumfont_t * lpef;
6012 {
6013 /* We are not interested in the extra info we get back from the 'Ex
6014 version - only the fact that we get character set variations
6015 enumerated seperately. */
6016 return enum_font_cb2 ((ENUMLOGFONT *) lplf, (NEWTEXTMETRIC *) lptm,
6017 font_type, lpef);
6018 }
6019
6020 static int CALLBACK
6021 enum_fontex_cb1 (lplf, lptm, font_type, lpef)
6022 ENUMLOGFONTEX * lplf;
6023 NEWTEXTMETRICEX * lptm;
6024 int font_type;
6025 enumfont_t * lpef;
6026 {
6027 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
6028 FARPROC enum_font_families_ex
6029 = GetProcAddress ( gdi32, "EnumFontFamiliesExA");
6030 /* We don't really expect EnumFontFamiliesEx to disappear once we
6031 get here, so don't bother handling it gracefully. */
6032 if (enum_font_families_ex == NULL)
6033 error ("gdi32.dll has disappeared!");
6034 return enum_font_families_ex (lpef->hdc,
6035 &lplf->elfLogFont,
6036 (FONTENUMPROC) enum_fontex_cb2,
6037 (LPARAM) lpef, 0);
6038 }
6039
6040 /* Interface to fontset handler. (adapted from mw32font.c in Meadow
6041 and xterm.c in Emacs 20.3) */
6042
6043 static Lisp_Object w32_list_bdf_fonts (Lisp_Object pattern, int max_names)
6044 {
6045 char *fontname, *ptnstr;
6046 Lisp_Object list, tem, newlist = Qnil;
6047 int n_fonts = 0;
6048
6049 list = Vw32_bdf_filename_alist;
6050 ptnstr = SDATA (pattern);
6051
6052 for ( ; CONSP (list); list = XCDR (list))
6053 {
6054 tem = XCAR (list);
6055 if (CONSP (tem))
6056 fontname = SDATA (XCAR (tem));
6057 else if (STRINGP (tem))
6058 fontname = SDATA (tem);
6059 else
6060 continue;
6061
6062 if (w32_font_match (fontname, ptnstr))
6063 {
6064 newlist = Fcons (XCAR (tem), newlist);
6065 n_fonts++;
6066 if (max_names >= 0 && n_fonts >= max_names)
6067 break;
6068 }
6069 }
6070
6071 return newlist;
6072 }
6073
6074
6075 /* Return a list of names of available fonts matching PATTERN on frame
6076 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
6077 to be listed. Frame F NULL means we have not yet created any
6078 frame, which means we can't get proper size info, as we don't have
6079 a device context to use for GetTextMetrics.
6080 MAXNAMES sets a limit on how many fonts to match. If MAXNAMES is
6081 negative, then all matching fonts are returned. */
6082
6083 Lisp_Object
6084 w32_list_fonts (f, pattern, size, maxnames)
6085 struct frame *f;
6086 Lisp_Object pattern;
6087 int size;
6088 int maxnames;
6089 {
6090 Lisp_Object patterns, key = Qnil, tem, tpat;
6091 Lisp_Object list = Qnil, newlist = Qnil, second_best = Qnil;
6092 struct w32_display_info *dpyinfo = &one_w32_display_info;
6093 int n_fonts = 0;
6094
6095 patterns = Fassoc (pattern, Valternate_fontname_alist);
6096 if (NILP (patterns))
6097 patterns = Fcons (pattern, Qnil);
6098
6099 for (; CONSP (patterns); patterns = XCDR (patterns))
6100 {
6101 enumfont_t ef;
6102 int codepage;
6103
6104 tpat = XCAR (patterns);
6105
6106 if (!STRINGP (tpat))
6107 continue;
6108
6109 /* Avoid expensive EnumFontFamilies functions if we are not
6110 going to be able to output one of these anyway. */
6111 codepage = w32_codepage_for_font (SDATA (tpat));
6112 if (codepage != CP_8BIT && codepage != CP_UNICODE
6113 && codepage != CP_DEFAULT && codepage != CP_UNKNOWN
6114 && !IsValidCodePage(codepage))
6115 continue;
6116
6117 /* See if we cached the result for this particular query.
6118 The cache is an alist of the form:
6119 ((PATTERN (FONTNAME . WIDTH) ...) ...)
6120 */
6121 if (tem = XCDR (dpyinfo->name_list_element),
6122 !NILP (list = Fassoc (tpat, tem)))
6123 {
6124 list = Fcdr_safe (list);
6125 /* We have a cached list. Don't have to get the list again. */
6126 goto label_cached;
6127 }
6128
6129 BLOCK_INPUT;
6130 /* At first, put PATTERN in the cache. */
6131 ef.pattern = tpat;
6132 ef.list = Qnil;
6133 ef.numFonts = 0;
6134
6135 /* Use EnumFontFamiliesEx where it is available, as it knows
6136 about character sets. Fall back to EnumFontFamilies for
6137 older versions of NT that don't support the 'Ex function. */
6138 x_to_w32_font (SDATA (tpat), &ef.logfont);
6139 {
6140 LOGFONT font_match_pattern;
6141 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
6142 FARPROC enum_font_families_ex
6143 = GetProcAddress ( gdi32, "EnumFontFamiliesExA");
6144
6145 /* We do our own pattern matching so we can handle wildcards. */
6146 font_match_pattern.lfFaceName[0] = 0;
6147 font_match_pattern.lfPitchAndFamily = 0;
6148 /* We can use the charset, because if it is a wildcard it will
6149 be DEFAULT_CHARSET anyway. */
6150 font_match_pattern.lfCharSet = ef.logfont.lfCharSet;
6151
6152 ef.hdc = GetDC (dpyinfo->root_window);
6153
6154 if (enum_font_families_ex)
6155 enum_font_families_ex (ef.hdc,
6156 &font_match_pattern,
6157 (FONTENUMPROC) enum_fontex_cb1,
6158 (LPARAM) &ef, 0);
6159 else
6160 EnumFontFamilies (ef.hdc, NULL, (FONTENUMPROC) enum_font_cb1,
6161 (LPARAM)&ef);
6162
6163 ReleaseDC (dpyinfo->root_window, ef.hdc);
6164 }
6165
6166 UNBLOCK_INPUT;
6167 list = ef.list;
6168
6169 /* Make a list of the fonts we got back.
6170 Store that in the font cache for the display. */
6171 XSETCDR (dpyinfo->name_list_element,
6172 Fcons (Fcons (tpat, list),
6173 XCDR (dpyinfo->name_list_element)));
6174
6175 label_cached:
6176 if (NILP (list)) continue; /* Try the remaining alternatives. */
6177
6178 newlist = second_best = Qnil;
6179
6180 /* Make a list of the fonts that have the right width. */
6181 for (; CONSP (list); list = XCDR (list))
6182 {
6183 int found_size;
6184 tem = XCAR (list);
6185
6186 if (!CONSP (tem))
6187 continue;
6188 if (NILP (XCAR (tem)))
6189 continue;
6190 if (!size)
6191 {
6192 newlist = Fcons (XCAR (tem), newlist);
6193 n_fonts++;
6194 if (maxnames >= 0 && n_fonts >= maxnames)
6195 break;
6196 else
6197 continue;
6198 }
6199 if (!INTEGERP (XCDR (tem)))
6200 {
6201 /* Since we don't yet know the size of the font, we must
6202 load it and try GetTextMetrics. */
6203 W32FontStruct thisinfo;
6204 LOGFONT lf;
6205 HDC hdc;
6206 HANDLE oldobj;
6207
6208 if (!x_to_w32_font (SDATA (XCAR (tem)), &lf))
6209 continue;
6210
6211 BLOCK_INPUT;
6212 thisinfo.bdf = NULL;
6213 thisinfo.hfont = CreateFontIndirect (&lf);
6214 if (thisinfo.hfont == NULL)
6215 continue;
6216
6217 hdc = GetDC (dpyinfo->root_window);
6218 oldobj = SelectObject (hdc, thisinfo.hfont);
6219 if (GetTextMetrics (hdc, &thisinfo.tm))
6220 XSETCDR (tem, make_number (FONT_WIDTH (&thisinfo)));
6221 else
6222 XSETCDR (tem, make_number (0));
6223 SelectObject (hdc, oldobj);
6224 ReleaseDC (dpyinfo->root_window, hdc);
6225 DeleteObject(thisinfo.hfont);
6226 UNBLOCK_INPUT;
6227 }
6228 found_size = XINT (XCDR (tem));
6229 if (found_size == size)
6230 {
6231 newlist = Fcons (XCAR (tem), newlist);
6232 n_fonts++;
6233 if (maxnames >= 0 && n_fonts >= maxnames)
6234 break;
6235 }
6236 /* keep track of the closest matching size in case
6237 no exact match is found. */
6238 else if (found_size > 0)
6239 {
6240 if (NILP (second_best))
6241 second_best = tem;
6242
6243 else if (found_size < size)
6244 {
6245 if (XINT (XCDR (second_best)) > size
6246 || XINT (XCDR (second_best)) < found_size)
6247 second_best = tem;
6248 }
6249 else
6250 {
6251 if (XINT (XCDR (second_best)) > size
6252 && XINT (XCDR (second_best)) >
6253 found_size)
6254 second_best = tem;
6255 }
6256 }
6257 }
6258
6259 if (!NILP (newlist))
6260 break;
6261 else if (!NILP (second_best))
6262 {
6263 newlist = Fcons (XCAR (second_best), Qnil);
6264 break;
6265 }
6266 }
6267
6268 /* Include any bdf fonts. */
6269 if (n_fonts < maxnames || maxnames < 0)
6270 {
6271 Lisp_Object combined[2];
6272 combined[0] = w32_list_bdf_fonts (pattern, maxnames - n_fonts);
6273 combined[1] = newlist;
6274 newlist = Fnconc(2, combined);
6275 }
6276
6277 return newlist;
6278 }
6279
6280
6281 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
6282 struct font_info *
6283 w32_get_font_info (f, font_idx)
6284 FRAME_PTR f;
6285 int font_idx;
6286 {
6287 return (FRAME_W32_FONT_TABLE (f) + font_idx);
6288 }
6289
6290
6291 struct font_info*
6292 w32_query_font (struct frame *f, char *fontname)
6293 {
6294 int i;
6295 struct font_info *pfi;
6296
6297 pfi = FRAME_W32_FONT_TABLE (f);
6298
6299 for (i = 0; i < one_w32_display_info.n_fonts ;i++, pfi++)
6300 {
6301 if (stricmp(pfi->name, fontname) == 0) return pfi;
6302 }
6303
6304 return NULL;
6305 }
6306
6307 /* Find a CCL program for a font specified by FONTP, and set the member
6308 `encoder' of the structure. */
6309
6310 void
6311 w32_find_ccl_program (fontp)
6312 struct font_info *fontp;
6313 {
6314 Lisp_Object list, elt;
6315
6316 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
6317 {
6318 elt = XCAR (list);
6319 if (CONSP (elt)
6320 && STRINGP (XCAR (elt))
6321 && (fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
6322 >= 0))
6323 break;
6324 }
6325 if (! NILP (list))
6326 {
6327 struct ccl_program *ccl
6328 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
6329
6330 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
6331 xfree (ccl);
6332 else
6333 fontp->font_encoder = ccl;
6334 }
6335 }
6336
6337 /* directory-files from dired.c. */
6338 Lisp_Object Fdirectory_files P_((Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object));
6339
6340 \f
6341 /* Find BDF files in a specified directory. (use GCPRO when calling,
6342 as this calls lisp to get a directory listing). */
6343 static Lisp_Object
6344 w32_find_bdf_fonts_in_dir (Lisp_Object directory)
6345 {
6346 Lisp_Object filelist, list = Qnil;
6347 char fontname[100];
6348
6349 if (!STRINGP(directory))
6350 return Qnil;
6351
6352 filelist = Fdirectory_files (directory, Qt,
6353 build_string (".*\\.[bB][dD][fF]"), Qt);
6354
6355 for ( ; CONSP(filelist); filelist = XCDR (filelist))
6356 {
6357 Lisp_Object filename = XCAR (filelist);
6358 if (w32_BDF_to_x_font (SDATA (filename), fontname, 100))
6359 store_in_alist (&list, build_string (fontname), filename);
6360 }
6361 return list;
6362 }
6363
6364 DEFUN ("w32-find-bdf-fonts", Fw32_find_bdf_fonts, Sw32_find_bdf_fonts,
6365 1, 1, 0,
6366 doc: /* Return a list of BDF fonts in DIRECTORY.
6367 The list is suitable for appending to `w32-bdf-filename-alist'.
6368 Fonts which do not contain an xlfd description will not be included
6369 in the list. DIRECTORY may be a list of directories. */)
6370 (directory)
6371 Lisp_Object directory;
6372 {
6373 Lisp_Object list = Qnil;
6374 struct gcpro gcpro1, gcpro2;
6375
6376 if (!CONSP (directory))
6377 return w32_find_bdf_fonts_in_dir (directory);
6378
6379 for ( ; CONSP (directory); directory = XCDR (directory))
6380 {
6381 Lisp_Object pair[2];
6382 pair[0] = list;
6383 pair[1] = Qnil;
6384 GCPRO2 (directory, list);
6385 pair[1] = w32_find_bdf_fonts_in_dir( XCAR (directory) );
6386 list = Fnconc( 2, pair );
6387 UNGCPRO;
6388 }
6389 return list;
6390 }
6391
6392 \f
6393 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
6394 doc: /* Internal function called by `color-defined-p', which see. */)
6395 (color, frame)
6396 Lisp_Object color, frame;
6397 {
6398 XColor foo;
6399 FRAME_PTR f = check_x_frame (frame);
6400
6401 CHECK_STRING (color);
6402
6403 if (w32_defined_color (f, SDATA (color), &foo, 0))
6404 return Qt;
6405 else
6406 return Qnil;
6407 }
6408
6409 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
6410 doc: /* Internal function called by `color-values', which see. */)
6411 (color, frame)
6412 Lisp_Object color, frame;
6413 {
6414 XColor foo;
6415 FRAME_PTR f = check_x_frame (frame);
6416
6417 CHECK_STRING (color);
6418
6419 if (w32_defined_color (f, SDATA (color), &foo, 0))
6420 return list3 (make_number ((GetRValue (foo.pixel) << 8)
6421 | GetRValue (foo.pixel)),
6422 make_number ((GetGValue (foo.pixel) << 8)
6423 | GetGValue (foo.pixel)),
6424 make_number ((GetBValue (foo.pixel) << 8)
6425 | GetBValue (foo.pixel)));
6426 else
6427 return Qnil;
6428 }
6429
6430 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
6431 doc: /* Internal function called by `display-color-p', which see. */)
6432 (display)
6433 Lisp_Object display;
6434 {
6435 struct w32_display_info *dpyinfo = check_x_display_info (display);
6436
6437 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 2)
6438 return Qnil;
6439
6440 return Qt;
6441 }
6442
6443 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p,
6444 Sx_display_grayscale_p, 0, 1, 0,
6445 doc: /* Return t if DISPLAY supports shades of gray.
6446 Note that color displays do support shades of gray.
6447 The optional argument DISPLAY specifies which display to ask about.
6448 DISPLAY should be either a frame or a display name (a string).
6449 If omitted or nil, that stands for the selected frame's display. */)
6450 (display)
6451 Lisp_Object display;
6452 {
6453 struct w32_display_info *dpyinfo = check_x_display_info (display);
6454
6455 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 1)
6456 return Qnil;
6457
6458 return Qt;
6459 }
6460
6461 DEFUN ("x-display-pixel-width", Fx_display_pixel_width,
6462 Sx_display_pixel_width, 0, 1, 0,
6463 doc: /* Returns the width in pixels of DISPLAY.
6464 The optional argument DISPLAY specifies which display to ask about.
6465 DISPLAY should be either a frame or a display name (a string).
6466 If omitted or nil, that stands for the selected frame's display. */)
6467 (display)
6468 Lisp_Object display;
6469 {
6470 struct w32_display_info *dpyinfo = check_x_display_info (display);
6471
6472 return make_number (dpyinfo->width);
6473 }
6474
6475 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
6476 Sx_display_pixel_height, 0, 1, 0,
6477 doc: /* Returns the height in pixels of DISPLAY.
6478 The optional argument DISPLAY specifies which display to ask about.
6479 DISPLAY should be either a frame or a display name (a string).
6480 If omitted or nil, that stands for the selected frame's display. */)
6481 (display)
6482 Lisp_Object display;
6483 {
6484 struct w32_display_info *dpyinfo = check_x_display_info (display);
6485
6486 return make_number (dpyinfo->height);
6487 }
6488
6489 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
6490 0, 1, 0,
6491 doc: /* Returns the number of bitplanes of DISPLAY.
6492 The optional argument DISPLAY specifies which display to ask about.
6493 DISPLAY should be either a frame or a display name (a string).
6494 If omitted or nil, that stands for the selected frame's display. */)
6495 (display)
6496 Lisp_Object display;
6497 {
6498 struct w32_display_info *dpyinfo = check_x_display_info (display);
6499
6500 return make_number (dpyinfo->n_planes * dpyinfo->n_cbits);
6501 }
6502
6503 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
6504 0, 1, 0,
6505 doc: /* Returns the number of color cells of DISPLAY.
6506 The optional argument DISPLAY specifies which display to ask about.
6507 DISPLAY should be either a frame or a display name (a string).
6508 If omitted or nil, that stands for the selected frame's display. */)
6509 (display)
6510 Lisp_Object display;
6511 {
6512 struct w32_display_info *dpyinfo = check_x_display_info (display);
6513 HDC hdc;
6514 int cap;
6515
6516 hdc = GetDC (dpyinfo->root_window);
6517 if (dpyinfo->has_palette)
6518 cap = GetDeviceCaps (hdc, SIZEPALETTE);
6519 else
6520 cap = GetDeviceCaps (hdc, NUMCOLORS);
6521
6522 /* We force 24+ bit depths to 24-bit, both to prevent an overflow
6523 and because probably is more meaningful on Windows anyway */
6524 if (cap < 0)
6525 cap = 1 << min(dpyinfo->n_planes * dpyinfo->n_cbits, 24);
6526
6527 ReleaseDC (dpyinfo->root_window, hdc);
6528
6529 return make_number (cap);
6530 }
6531
6532 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
6533 Sx_server_max_request_size,
6534 0, 1, 0,
6535 doc: /* Returns the maximum request size of the server of DISPLAY.
6536 The optional argument DISPLAY specifies which display to ask about.
6537 DISPLAY should be either a frame or a display name (a string).
6538 If omitted or nil, that stands for the selected frame's display. */)
6539 (display)
6540 Lisp_Object display;
6541 {
6542 struct w32_display_info *dpyinfo = check_x_display_info (display);
6543
6544 return make_number (1);
6545 }
6546
6547 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
6548 doc: /* Returns the "vendor ID" string of the W32 system (Microsoft).
6549 The optional argument DISPLAY specifies which display to ask about.
6550 DISPLAY should be either a frame or a display name (a string).
6551 If omitted or nil, that stands for the selected frame's display. */)
6552 (display)
6553 Lisp_Object display;
6554 {
6555 return build_string ("Microsoft Corp.");
6556 }
6557
6558 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
6559 doc: /* Returns the version numbers of the server of DISPLAY.
6560 The value is a list of three integers: the major and minor
6561 version numbers of the X Protocol in use, and the distributor-specific release
6562 number. See also the function `x-server-vendor'.
6563
6564 The optional argument DISPLAY specifies which display to ask about.
6565 DISPLAY should be either a frame or a display name (a string).
6566 If omitted or nil, that stands for the selected frame's display. */)
6567 (display)
6568 Lisp_Object display;
6569 {
6570 return Fcons (make_number (w32_major_version),
6571 Fcons (make_number (w32_minor_version),
6572 Fcons (make_number (w32_build_number), Qnil)));
6573 }
6574
6575 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
6576 doc: /* Returns the number of screens on the server of DISPLAY.
6577 The optional argument DISPLAY specifies which display to ask about.
6578 DISPLAY should be either a frame or a display name (a string).
6579 If omitted or nil, that stands for the selected frame's display. */)
6580 (display)
6581 Lisp_Object display;
6582 {
6583 return make_number (1);
6584 }
6585
6586 DEFUN ("x-display-mm-height", Fx_display_mm_height,
6587 Sx_display_mm_height, 0, 1, 0,
6588 doc: /* Returns the height in millimeters of DISPLAY.
6589 The optional argument DISPLAY specifies which display to ask about.
6590 DISPLAY should be either a frame or a display name (a string).
6591 If omitted or nil, that stands for the selected frame's display. */)
6592 (display)
6593 Lisp_Object display;
6594 {
6595 struct w32_display_info *dpyinfo = check_x_display_info (display);
6596 HDC hdc;
6597 int cap;
6598
6599 hdc = GetDC (dpyinfo->root_window);
6600
6601 cap = GetDeviceCaps (hdc, VERTSIZE);
6602
6603 ReleaseDC (dpyinfo->root_window, hdc);
6604
6605 return make_number (cap);
6606 }
6607
6608 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
6609 doc: /* Returns the width in millimeters of DISPLAY.
6610 The optional argument DISPLAY specifies which display to ask about.
6611 DISPLAY should be either a frame or a display name (a string).
6612 If omitted or nil, that stands for the selected frame's display. */)
6613 (display)
6614 Lisp_Object display;
6615 {
6616 struct w32_display_info *dpyinfo = check_x_display_info (display);
6617
6618 HDC hdc;
6619 int cap;
6620
6621 hdc = GetDC (dpyinfo->root_window);
6622
6623 cap = GetDeviceCaps (hdc, HORZSIZE);
6624
6625 ReleaseDC (dpyinfo->root_window, hdc);
6626
6627 return make_number (cap);
6628 }
6629
6630 DEFUN ("x-display-backing-store", Fx_display_backing_store,
6631 Sx_display_backing_store, 0, 1, 0,
6632 doc: /* Returns an indication of whether DISPLAY does backing store.
6633 The value may be `always', `when-mapped', or `not-useful'.
6634 The optional argument DISPLAY specifies which display to ask about.
6635 DISPLAY should be either a frame or a display name (a string).
6636 If omitted or nil, that stands for the selected frame's display. */)
6637 (display)
6638 Lisp_Object display;
6639 {
6640 return intern ("not-useful");
6641 }
6642
6643 DEFUN ("x-display-visual-class", Fx_display_visual_class,
6644 Sx_display_visual_class, 0, 1, 0,
6645 doc: /* Returns the visual class of DISPLAY.
6646 The value is one of the symbols `static-gray', `gray-scale',
6647 `static-color', `pseudo-color', `true-color', or `direct-color'.
6648
6649 The optional argument DISPLAY specifies which display to ask about.
6650 DISPLAY should be either a frame or a display name (a string).
6651 If omitted or nil, that stands for the selected frame's display. */)
6652 (display)
6653 Lisp_Object display;
6654 {
6655 struct w32_display_info *dpyinfo = check_x_display_info (display);
6656 Lisp_Object result = Qnil;
6657
6658 if (dpyinfo->has_palette)
6659 result = intern ("pseudo-color");
6660 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 1)
6661 result = intern ("static-grey");
6662 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 4)
6663 result = intern ("static-color");
6664 else if (dpyinfo->n_planes * dpyinfo->n_cbits > 8)
6665 result = intern ("true-color");
6666
6667 return result;
6668 }
6669
6670 DEFUN ("x-display-save-under", Fx_display_save_under,
6671 Sx_display_save_under, 0, 1, 0,
6672 doc: /* Returns t if DISPLAY supports the save-under feature.
6673 The optional argument DISPLAY specifies which display to ask about.
6674 DISPLAY should be either a frame or a display name (a string).
6675 If omitted or nil, that stands for the selected frame's display. */)
6676 (display)
6677 Lisp_Object display;
6678 {
6679 return Qnil;
6680 }
6681 \f
6682 int
6683 x_pixel_width (f)
6684 register struct frame *f;
6685 {
6686 return FRAME_PIXEL_WIDTH (f);
6687 }
6688
6689 int
6690 x_pixel_height (f)
6691 register struct frame *f;
6692 {
6693 return FRAME_PIXEL_HEIGHT (f);
6694 }
6695
6696 int
6697 x_char_width (f)
6698 register struct frame *f;
6699 {
6700 return FRAME_COLUMN_WIDTH (f);
6701 }
6702
6703 int
6704 x_char_height (f)
6705 register struct frame *f;
6706 {
6707 return FRAME_LINE_HEIGHT (f);
6708 }
6709
6710 int
6711 x_screen_planes (f)
6712 register struct frame *f;
6713 {
6714 return FRAME_W32_DISPLAY_INFO (f)->n_planes;
6715 }
6716 \f
6717 /* Return the display structure for the display named NAME.
6718 Open a new connection if necessary. */
6719
6720 struct w32_display_info *
6721 x_display_info_for_name (name)
6722 Lisp_Object name;
6723 {
6724 Lisp_Object names;
6725 struct w32_display_info *dpyinfo;
6726
6727 CHECK_STRING (name);
6728
6729 for (dpyinfo = &one_w32_display_info, names = w32_display_name_list;
6730 dpyinfo;
6731 dpyinfo = dpyinfo->next, names = XCDR (names))
6732 {
6733 Lisp_Object tem;
6734 tem = Fstring_equal (XCAR (XCAR (names)), name);
6735 if (!NILP (tem))
6736 return dpyinfo;
6737 }
6738
6739 /* Use this general default value to start with. */
6740 Vx_resource_name = Vinvocation_name;
6741
6742 validate_x_resource_name ();
6743
6744 dpyinfo = w32_term_init (name, (unsigned char *)0,
6745 (char *) SDATA (Vx_resource_name));
6746
6747 if (dpyinfo == 0)
6748 error ("Cannot connect to server %s", SDATA (name));
6749
6750 w32_in_use = 1;
6751 XSETFASTINT (Vwindow_system_version, 3);
6752
6753 return dpyinfo;
6754 }
6755
6756 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
6757 1, 3, 0, doc: /* Open a connection to a server.
6758 DISPLAY is the name of the display to connect to.
6759 Optional second arg XRM-STRING is a string of resources in xrdb format.
6760 If the optional third arg MUST-SUCCEED is non-nil,
6761 terminate Emacs if we can't open the connection. */)
6762 (display, xrm_string, must_succeed)
6763 Lisp_Object display, xrm_string, must_succeed;
6764 {
6765 unsigned char *xrm_option;
6766 struct w32_display_info *dpyinfo;
6767
6768 /* If initialization has already been done, return now to avoid
6769 overwriting critical parts of one_w32_display_info. */
6770 if (w32_in_use)
6771 return Qnil;
6772
6773 CHECK_STRING (display);
6774 if (! NILP (xrm_string))
6775 CHECK_STRING (xrm_string);
6776
6777 if (! EQ (Vwindow_system, intern ("w32")))
6778 error ("Not using Microsoft Windows");
6779
6780 /* Allow color mapping to be defined externally; first look in user's
6781 HOME directory, then in Emacs etc dir for a file called rgb.txt. */
6782 {
6783 Lisp_Object color_file;
6784 struct gcpro gcpro1;
6785
6786 color_file = build_string("~/rgb.txt");
6787
6788 GCPRO1 (color_file);
6789
6790 if (NILP (Ffile_readable_p (color_file)))
6791 color_file =
6792 Fexpand_file_name (build_string ("rgb.txt"),
6793 Fsymbol_value (intern ("data-directory")));
6794
6795 Vw32_color_map = Fw32_load_color_file (color_file);
6796
6797 UNGCPRO;
6798 }
6799 if (NILP (Vw32_color_map))
6800 Vw32_color_map = Fw32_default_color_map ();
6801
6802 /* Merge in system logical colors. */
6803 add_system_logical_colors_to_map (&Vw32_color_map);
6804
6805 if (! NILP (xrm_string))
6806 xrm_option = (unsigned char *) SDATA (xrm_string);
6807 else
6808 xrm_option = (unsigned char *) 0;
6809
6810 /* Use this general default value to start with. */
6811 /* First remove .exe suffix from invocation-name - it looks ugly. */
6812 {
6813 char basename[ MAX_PATH ], *str;
6814
6815 strcpy (basename, SDATA (Vinvocation_name));
6816 str = strrchr (basename, '.');
6817 if (str) *str = 0;
6818 Vinvocation_name = build_string (basename);
6819 }
6820 Vx_resource_name = Vinvocation_name;
6821
6822 validate_x_resource_name ();
6823
6824 /* This is what opens the connection and sets x_current_display.
6825 This also initializes many symbols, such as those used for input. */
6826 dpyinfo = w32_term_init (display, xrm_option,
6827 (char *) SDATA (Vx_resource_name));
6828
6829 if (dpyinfo == 0)
6830 {
6831 if (!NILP (must_succeed))
6832 fatal ("Cannot connect to server %s.\n",
6833 SDATA (display));
6834 else
6835 error ("Cannot connect to server %s", SDATA (display));
6836 }
6837
6838 w32_in_use = 1;
6839
6840 XSETFASTINT (Vwindow_system_version, 3);
6841 return Qnil;
6842 }
6843
6844 DEFUN ("x-close-connection", Fx_close_connection,
6845 Sx_close_connection, 1, 1, 0,
6846 doc: /* Close the connection to DISPLAY's server.
6847 For DISPLAY, specify either a frame or a display name (a string).
6848 If DISPLAY is nil, that stands for the selected frame's display. */)
6849 (display)
6850 Lisp_Object display;
6851 {
6852 struct w32_display_info *dpyinfo = check_x_display_info (display);
6853 int i;
6854
6855 if (dpyinfo->reference_count > 0)
6856 error ("Display still has frames on it");
6857
6858 BLOCK_INPUT;
6859 /* Free the fonts in the font table. */
6860 for (i = 0; i < dpyinfo->n_fonts; i++)
6861 if (dpyinfo->font_table[i].name)
6862 {
6863 if (dpyinfo->font_table[i].name != dpyinfo->font_table[i].full_name)
6864 xfree (dpyinfo->font_table[i].full_name);
6865 xfree (dpyinfo->font_table[i].name);
6866 w32_unload_font (dpyinfo, dpyinfo->font_table[i].font);
6867 }
6868 x_destroy_all_bitmaps (dpyinfo);
6869
6870 x_delete_display (dpyinfo);
6871 UNBLOCK_INPUT;
6872
6873 return Qnil;
6874 }
6875
6876 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
6877 doc: /* Return the list of display names that Emacs has connections to. */)
6878 ()
6879 {
6880 Lisp_Object tail, result;
6881
6882 result = Qnil;
6883 for (tail = w32_display_name_list; ! NILP (tail); tail = XCDR (tail))
6884 result = Fcons (XCAR (XCAR (tail)), result);
6885
6886 return result;
6887 }
6888
6889 DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
6890 doc: /* This is a noop on W32 systems. */)
6891 (on, display)
6892 Lisp_Object display, on;
6893 {
6894 return Qnil;
6895 }
6896
6897
6898 \f
6899 /***********************************************************************
6900 Window properties
6901 ***********************************************************************/
6902
6903 DEFUN ("x-change-window-property", Fx_change_window_property,
6904 Sx_change_window_property, 2, 6, 0,
6905 doc: /* Change window property PROP to VALUE on the X window of FRAME.
6906 VALUE may be a string or a list of conses, numbers and/or strings.
6907 If an element in the list is a string, it is converted to
6908 an Atom and the value of the Atom is used. If an element is a cons,
6909 it is converted to a 32 bit number where the car is the 16 top bits and the
6910 cdr is the lower 16 bits.
6911 FRAME nil or omitted means use the selected frame.
6912 If TYPE is given and non-nil, it is the name of the type of VALUE.
6913 If TYPE is not given or nil, the type is STRING.
6914 FORMAT gives the size in bits of each element if VALUE is a list.
6915 It must be one of 8, 16 or 32.
6916 If VALUE is a string or FORMAT is nil or not given, FORMAT defaults to 8.
6917 If OUTER_P is non-nil, the property is changed for the outer X window of
6918 FRAME. Default is to change on the edit X window.
6919
6920 Value is VALUE. */)
6921 (prop, value, frame, type, format, outer_p)
6922 Lisp_Object prop, value, frame, type, format, outer_p;
6923 {
6924 #if 0 /* TODO : port window properties to W32 */
6925 struct frame *f = check_x_frame (frame);
6926 Atom prop_atom;
6927
6928 CHECK_STRING (prop);
6929 CHECK_STRING (value);
6930
6931 BLOCK_INPUT;
6932 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
6933 XChangeProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
6934 prop_atom, XA_STRING, 8, PropModeReplace,
6935 SDATA (value), SCHARS (value));
6936
6937 /* Make sure the property is set when we return. */
6938 XFlush (FRAME_W32_DISPLAY (f));
6939 UNBLOCK_INPUT;
6940
6941 #endif /* TODO */
6942
6943 return value;
6944 }
6945
6946
6947 DEFUN ("x-delete-window-property", Fx_delete_window_property,
6948 Sx_delete_window_property, 1, 2, 0,
6949 doc: /* Remove window property PROP from X window of FRAME.
6950 FRAME nil or omitted means use the selected frame. Value is PROP. */)
6951 (prop, frame)
6952 Lisp_Object prop, frame;
6953 {
6954 #if 0 /* TODO : port window properties to W32 */
6955
6956 struct frame *f = check_x_frame (frame);
6957 Atom prop_atom;
6958
6959 CHECK_STRING (prop);
6960 BLOCK_INPUT;
6961 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
6962 XDeleteProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), prop_atom);
6963
6964 /* Make sure the property is removed when we return. */
6965 XFlush (FRAME_W32_DISPLAY (f));
6966 UNBLOCK_INPUT;
6967 #endif /* TODO */
6968
6969 return prop;
6970 }
6971
6972
6973 DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
6974 1, 2, 0,
6975 doc: /* Value is the value of window property PROP on FRAME.
6976 If FRAME is nil or omitted, use the selected frame. Value is nil
6977 if FRAME hasn't a property with name PROP or if PROP has no string
6978 value. */)
6979 (prop, frame)
6980 Lisp_Object prop, frame;
6981 {
6982 #if 0 /* TODO : port window properties to W32 */
6983
6984 struct frame *f = check_x_frame (frame);
6985 Atom prop_atom;
6986 int rc;
6987 Lisp_Object prop_value = Qnil;
6988 char *tmp_data = NULL;
6989 Atom actual_type;
6990 int actual_format;
6991 unsigned long actual_size, bytes_remaining;
6992
6993 CHECK_STRING (prop);
6994 BLOCK_INPUT;
6995 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
6996 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
6997 prop_atom, 0, 0, False, XA_STRING,
6998 &actual_type, &actual_format, &actual_size,
6999 &bytes_remaining, (unsigned char **) &tmp_data);
7000 if (rc == Success)
7001 {
7002 int size = bytes_remaining;
7003
7004 XFree (tmp_data);
7005 tmp_data = NULL;
7006
7007 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
7008 prop_atom, 0, bytes_remaining,
7009 False, XA_STRING,
7010 &actual_type, &actual_format,
7011 &actual_size, &bytes_remaining,
7012 (unsigned char **) &tmp_data);
7013 if (rc == Success)
7014 prop_value = make_string (tmp_data, size);
7015
7016 XFree (tmp_data);
7017 }
7018
7019 UNBLOCK_INPUT;
7020
7021 return prop_value;
7022
7023 #endif /* TODO */
7024 return Qnil;
7025 }
7026
7027
7028 \f
7029 /***********************************************************************
7030 Busy cursor
7031 ***********************************************************************/
7032
7033 /* If non-null, an asynchronous timer that, when it expires, displays
7034 an hourglass cursor on all frames. */
7035
7036 static struct atimer *hourglass_atimer;
7037
7038 /* Non-zero means an hourglass cursor is currently shown. */
7039
7040 static int hourglass_shown_p;
7041
7042 /* Number of seconds to wait before displaying an hourglass cursor. */
7043
7044 static Lisp_Object Vhourglass_delay;
7045
7046 /* Default number of seconds to wait before displaying an hourglass
7047 cursor. */
7048
7049 #define DEFAULT_HOURGLASS_DELAY 1
7050
7051 /* Function prototypes. */
7052
7053 static void show_hourglass P_ ((struct atimer *));
7054 static void hide_hourglass P_ ((void));
7055
7056
7057 /* Cancel a currently active hourglass timer, and start a new one. */
7058
7059 void
7060 start_hourglass ()
7061 {
7062 #if 0 /* TODO: cursor shape changes. */
7063 EMACS_TIME delay;
7064 int secs, usecs = 0;
7065
7066 cancel_hourglass ();
7067
7068 if (INTEGERP (Vhourglass_delay)
7069 && XINT (Vhourglass_delay) > 0)
7070 secs = XFASTINT (Vhourglass_delay);
7071 else if (FLOATP (Vhourglass_delay)
7072 && XFLOAT_DATA (Vhourglass_delay) > 0)
7073 {
7074 Lisp_Object tem;
7075 tem = Ftruncate (Vhourglass_delay, Qnil);
7076 secs = XFASTINT (tem);
7077 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
7078 }
7079 else
7080 secs = DEFAULT_HOURGLASS_DELAY;
7081
7082 EMACS_SET_SECS_USECS (delay, secs, usecs);
7083 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
7084 show_hourglass, NULL);
7085 #endif
7086 }
7087
7088
7089 /* Cancel the hourglass cursor timer if active, hide an hourglass
7090 cursor if shown. */
7091
7092 void
7093 cancel_hourglass ()
7094 {
7095 if (hourglass_atimer)
7096 {
7097 cancel_atimer (hourglass_atimer);
7098 hourglass_atimer = NULL;
7099 }
7100
7101 if (hourglass_shown_p)
7102 hide_hourglass ();
7103 }
7104
7105
7106 /* Timer function of hourglass_atimer. TIMER is equal to
7107 hourglass_atimer.
7108
7109 Display an hourglass cursor on all frames by mapping the frames'
7110 hourglass_window. Set the hourglass_p flag in the frames'
7111 output_data.x structure to indicate that an hourglass cursor is
7112 shown on the frames. */
7113
7114 static void
7115 show_hourglass (timer)
7116 struct atimer *timer;
7117 {
7118 #if 0 /* TODO: cursor shape changes. */
7119 /* The timer implementation will cancel this timer automatically
7120 after this function has run. Set hourglass_atimer to null
7121 so that we know the timer doesn't have to be canceled. */
7122 hourglass_atimer = NULL;
7123
7124 if (!hourglass_shown_p)
7125 {
7126 Lisp_Object rest, frame;
7127
7128 BLOCK_INPUT;
7129
7130 FOR_EACH_FRAME (rest, frame)
7131 if (FRAME_W32_P (XFRAME (frame)))
7132 {
7133 struct frame *f = XFRAME (frame);
7134
7135 f->output_data.w32->hourglass_p = 1;
7136
7137 if (!f->output_data.w32->hourglass_window)
7138 {
7139 unsigned long mask = CWCursor;
7140 XSetWindowAttributes attrs;
7141
7142 attrs.cursor = f->output_data.w32->hourglass_cursor;
7143
7144 f->output_data.w32->hourglass_window
7145 = XCreateWindow (FRAME_X_DISPLAY (f),
7146 FRAME_OUTER_WINDOW (f),
7147 0, 0, 32000, 32000, 0, 0,
7148 InputOnly,
7149 CopyFromParent,
7150 mask, &attrs);
7151 }
7152
7153 XMapRaised (FRAME_X_DISPLAY (f),
7154 f->output_data.w32->hourglass_window);
7155 XFlush (FRAME_X_DISPLAY (f));
7156 }
7157
7158 hourglass_shown_p = 1;
7159 UNBLOCK_INPUT;
7160 }
7161 #endif
7162 }
7163
7164
7165 /* Hide the hourglass cursor on all frames, if it is currently shown. */
7166
7167 static void
7168 hide_hourglass ()
7169 {
7170 #if 0 /* TODO: cursor shape changes. */
7171 if (hourglass_shown_p)
7172 {
7173 Lisp_Object rest, frame;
7174
7175 BLOCK_INPUT;
7176 FOR_EACH_FRAME (rest, frame)
7177 {
7178 struct frame *f = XFRAME (frame);
7179
7180 if (FRAME_W32_P (f)
7181 /* Watch out for newly created frames. */
7182 && f->output_data.x->hourglass_window)
7183 {
7184 XUnmapWindow (FRAME_X_DISPLAY (f),
7185 f->output_data.x->hourglass_window);
7186 /* Sync here because XTread_socket looks at the
7187 hourglass_p flag that is reset to zero below. */
7188 XSync (FRAME_X_DISPLAY (f), False);
7189 f->output_data.x->hourglass_p = 0;
7190 }
7191 }
7192
7193 hourglass_shown_p = 0;
7194 UNBLOCK_INPUT;
7195 }
7196 #endif
7197 }
7198
7199
7200 \f
7201 /***********************************************************************
7202 Tool tips
7203 ***********************************************************************/
7204
7205 static Lisp_Object x_create_tip_frame P_ ((struct w32_display_info *,
7206 Lisp_Object, Lisp_Object));
7207 static void compute_tip_xy P_ ((struct frame *, Lisp_Object, Lisp_Object,
7208 Lisp_Object, int, int, int *, int *));
7209
7210 /* The frame of a currently visible tooltip. */
7211
7212 Lisp_Object tip_frame;
7213
7214 /* If non-nil, a timer started that hides the last tooltip when it
7215 fires. */
7216
7217 Lisp_Object tip_timer;
7218 Window tip_window;
7219
7220 /* If non-nil, a vector of 3 elements containing the last args
7221 with which x-show-tip was called. See there. */
7222
7223 Lisp_Object last_show_tip_args;
7224
7225 /* Maximum size for tooltips; a cons (COLUMNS . ROWS). */
7226
7227 Lisp_Object Vx_max_tooltip_size;
7228
7229
7230 static Lisp_Object
7231 unwind_create_tip_frame (frame)
7232 Lisp_Object frame;
7233 {
7234 Lisp_Object deleted;
7235
7236 deleted = unwind_create_frame (frame);
7237 if (EQ (deleted, Qt))
7238 {
7239 tip_window = NULL;
7240 tip_frame = Qnil;
7241 }
7242
7243 return deleted;
7244 }
7245
7246
7247 /* Create a frame for a tooltip on the display described by DPYINFO.
7248 PARMS is a list of frame parameters. TEXT is the string to
7249 display in the tip frame. Value is the frame.
7250
7251 Note that functions called here, esp. x_default_parameter can
7252 signal errors, for instance when a specified color name is
7253 undefined. We have to make sure that we're in a consistent state
7254 when this happens. */
7255
7256 static Lisp_Object
7257 x_create_tip_frame (dpyinfo, parms, text)
7258 struct w32_display_info *dpyinfo;
7259 Lisp_Object parms, text;
7260 {
7261 struct frame *f;
7262 Lisp_Object frame, tem;
7263 Lisp_Object name;
7264 long window_prompting = 0;
7265 int width, height;
7266 int count = SPECPDL_INDEX ();
7267 struct gcpro gcpro1, gcpro2, gcpro3;
7268 struct kboard *kb;
7269 int face_change_count_before = face_change_count;
7270 Lisp_Object buffer;
7271 struct buffer *old_buffer;
7272
7273 check_w32 ();
7274
7275 /* Use this general default value to start with until we know if
7276 this frame has a specified name. */
7277 Vx_resource_name = Vinvocation_name;
7278
7279 #ifdef MULTI_KBOARD
7280 kb = dpyinfo->kboard;
7281 #else
7282 kb = &the_only_kboard;
7283 #endif
7284
7285 /* Get the name of the frame to use for resource lookup. */
7286 name = w32_get_arg (parms, Qname, "name", "Name", RES_TYPE_STRING);
7287 if (!STRINGP (name)
7288 && !EQ (name, Qunbound)
7289 && !NILP (name))
7290 error ("Invalid frame name--not a string or nil");
7291 Vx_resource_name = name;
7292
7293 frame = Qnil;
7294 GCPRO3 (parms, name, frame);
7295 /* Make a frame without minibuffer nor mode-line. */
7296 f = make_frame (0);
7297 f->wants_modeline = 0;
7298 XSETFRAME (frame, f);
7299
7300 buffer = Fget_buffer_create (build_string (" *tip*"));
7301 Fset_window_buffer (FRAME_ROOT_WINDOW (f), buffer, Qnil);
7302 old_buffer = current_buffer;
7303 set_buffer_internal_1 (XBUFFER (buffer));
7304 current_buffer->truncate_lines = Qnil;
7305 specbind (Qinhibit_read_only, Qt);
7306 specbind (Qinhibit_modification_hooks, Qt);
7307 Ferase_buffer ();
7308 Finsert (1, &text);
7309 set_buffer_internal_1 (old_buffer);
7310
7311 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
7312 record_unwind_protect (unwind_create_tip_frame, frame);
7313
7314 /* By setting the output method, we're essentially saying that
7315 the frame is live, as per FRAME_LIVE_P. If we get a signal
7316 from this point on, x_destroy_window might screw up reference
7317 counts etc. */
7318 f->output_method = output_w32;
7319 f->output_data.w32 =
7320 (struct w32_output *) xmalloc (sizeof (struct w32_output));
7321 bzero (f->output_data.w32, sizeof (struct w32_output));
7322
7323 FRAME_FONTSET (f) = -1;
7324 f->icon_name = Qnil;
7325
7326 #if 0 /* GLYPH_DEBUG TODO: image support. */
7327 image_cache_refcount = FRAME_X_IMAGE_CACHE (f)->refcount;
7328 dpyinfo_refcount = dpyinfo->reference_count;
7329 #endif /* GLYPH_DEBUG */
7330 #ifdef MULTI_KBOARD
7331 FRAME_KBOARD (f) = kb;
7332 #endif
7333 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
7334 f->output_data.w32->explicit_parent = 0;
7335
7336 /* Set the name; the functions to which we pass f expect the name to
7337 be set. */
7338 if (EQ (name, Qunbound) || NILP (name))
7339 {
7340 f->name = build_string (dpyinfo->w32_id_name);
7341 f->explicit_name = 0;
7342 }
7343 else
7344 {
7345 f->name = name;
7346 f->explicit_name = 1;
7347 /* use the frame's title when getting resources for this frame. */
7348 specbind (Qx_resource_name, name);
7349 }
7350
7351 /* Extract the window parameters from the supplied values
7352 that are needed to determine window geometry. */
7353 {
7354 Lisp_Object font;
7355
7356 font = w32_get_arg (parms, Qfont, "font", "Font", RES_TYPE_STRING);
7357
7358 BLOCK_INPUT;
7359 /* First, try whatever font the caller has specified. */
7360 if (STRINGP (font))
7361 {
7362 tem = Fquery_fontset (font, Qnil);
7363 if (STRINGP (tem))
7364 font = x_new_fontset (f, tem);
7365 else
7366 font = x_new_font (f, SDATA (font));
7367 }
7368
7369 /* Try out a font which we hope has bold and italic variations. */
7370 if (!STRINGP (font))
7371 font = x_new_font (f, "-*-Courier New-normal-r-*-*-*-100-*-*-c-*-iso8859-1");
7372 if (! STRINGP (font))
7373 font = x_new_font (f, "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1");
7374 /* If those didn't work, look for something which will at least work. */
7375 if (! STRINGP (font))
7376 font = x_new_font (f, "-*-Fixedsys-normal-r-*-*-12-*-*-*-c-*-iso8859-1");
7377 UNBLOCK_INPUT;
7378 if (! STRINGP (font))
7379 font = build_string ("Fixedsys");
7380
7381 x_default_parameter (f, parms, Qfont, font,
7382 "font", "Font", RES_TYPE_STRING);
7383 }
7384
7385 x_default_parameter (f, parms, Qborder_width, make_number (2),
7386 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
7387 /* This defaults to 2 in order to match xterm. We recognize either
7388 internalBorderWidth or internalBorder (which is what xterm calls
7389 it). */
7390 if (NILP (Fassq (Qinternal_border_width, parms)))
7391 {
7392 Lisp_Object value;
7393
7394 value = w32_get_arg (parms, Qinternal_border_width,
7395 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
7396 if (! EQ (value, Qunbound))
7397 parms = Fcons (Fcons (Qinternal_border_width, value),
7398 parms);
7399 }
7400 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
7401 "internalBorderWidth", "internalBorderWidth",
7402 RES_TYPE_NUMBER);
7403
7404 /* Also do the stuff which must be set before the window exists. */
7405 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
7406 "foreground", "Foreground", RES_TYPE_STRING);
7407 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
7408 "background", "Background", RES_TYPE_STRING);
7409 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
7410 "pointerColor", "Foreground", RES_TYPE_STRING);
7411 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
7412 "cursorColor", "Foreground", RES_TYPE_STRING);
7413 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
7414 "borderColor", "BorderColor", RES_TYPE_STRING);
7415
7416 /* Init faces before x_default_parameter is called for scroll-bar
7417 parameters because that function calls x_set_scroll_bar_width,
7418 which calls change_frame_size, which calls Fset_window_buffer,
7419 which runs hooks, which call Fvertical_motion. At the end, we
7420 end up in init_iterator with a null face cache, which should not
7421 happen. */
7422 init_frame_faces (f);
7423
7424 f->output_data.w32->dwStyle = WS_BORDER | WS_POPUP | WS_DISABLED;
7425 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
7426
7427 window_prompting = x_figure_window_size (f, parms, 0);
7428
7429 /* No fringes on tip frame. */
7430 f->fringe_cols = 0;
7431 f->left_fringe_width = 0;
7432 f->right_fringe_width = 0;
7433
7434 BLOCK_INPUT;
7435 my_create_tip_window (f);
7436 UNBLOCK_INPUT;
7437
7438 x_make_gc (f);
7439
7440 x_default_parameter (f, parms, Qauto_raise, Qnil,
7441 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
7442 x_default_parameter (f, parms, Qauto_lower, Qnil,
7443 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
7444 x_default_parameter (f, parms, Qcursor_type, Qbox,
7445 "cursorType", "CursorType", RES_TYPE_SYMBOL);
7446
7447 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
7448 Change will not be effected unless different from the current
7449 FRAME_LINES (f). */
7450 width = FRAME_COLS (f);
7451 height = FRAME_LINES (f);
7452 FRAME_LINES (f) = 0;
7453 SET_FRAME_COLS (f, 0);
7454 change_frame_size (f, height, width, 1, 0, 0);
7455
7456 /* Add `tooltip' frame parameter's default value. */
7457 if (NILP (Fframe_parameter (frame, intern ("tooltip"))))
7458 Fmodify_frame_parameters (frame, Fcons (Fcons (intern ("tooltip"), Qt),
7459 Qnil));
7460
7461 /* Set up faces after all frame parameters are known. This call
7462 also merges in face attributes specified for new frames.
7463
7464 Frame parameters may be changed if .Xdefaults contains
7465 specifications for the default font. For example, if there is an
7466 `Emacs.default.attributeBackground: pink', the `background-color'
7467 attribute of the frame get's set, which let's the internal border
7468 of the tooltip frame appear in pink. Prevent this. */
7469 {
7470 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
7471
7472 /* Set tip_frame here, so that */
7473 tip_frame = frame;
7474 call1 (Qface_set_after_frame_default, frame);
7475
7476 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
7477 Fmodify_frame_parameters (frame, Fcons (Fcons (Qbackground_color, bg),
7478 Qnil));
7479 }
7480
7481 f->no_split = 1;
7482
7483 UNGCPRO;
7484
7485 /* It is now ok to make the frame official even if we get an error
7486 below. And the frame needs to be on Vframe_list or making it
7487 visible won't work. */
7488 Vframe_list = Fcons (frame, Vframe_list);
7489
7490 /* Now that the frame is official, it counts as a reference to
7491 its display. */
7492 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
7493
7494 /* Setting attributes of faces of the tooltip frame from resources
7495 and similar will increment face_change_count, which leads to the
7496 clearing of all current matrices. Since this isn't necessary
7497 here, avoid it by resetting face_change_count to the value it
7498 had before we created the tip frame. */
7499 face_change_count = face_change_count_before;
7500
7501 /* Discard the unwind_protect. */
7502 return unbind_to (count, frame);
7503 }
7504
7505
7506 /* Compute where to display tip frame F. PARMS is the list of frame
7507 parameters for F. DX and DY are specified offsets from the current
7508 location of the mouse. WIDTH and HEIGHT are the width and height
7509 of the tooltip. Return coordinates relative to the root window of
7510 the display in *ROOT_X, and *ROOT_Y. */
7511
7512 static void
7513 compute_tip_xy (f, parms, dx, dy, width, height, root_x, root_y)
7514 struct frame *f;
7515 Lisp_Object parms, dx, dy;
7516 int width, height;
7517 int *root_x, *root_y;
7518 {
7519 Lisp_Object left, top;
7520
7521 /* User-specified position? */
7522 left = Fcdr (Fassq (Qleft, parms));
7523 top = Fcdr (Fassq (Qtop, parms));
7524
7525 /* Move the tooltip window where the mouse pointer is. Resize and
7526 show it. */
7527 if (!INTEGERP (left) || !INTEGERP (top))
7528 {
7529 POINT pt;
7530
7531 BLOCK_INPUT;
7532 GetCursorPos (&pt);
7533 *root_x = pt.x;
7534 *root_y = pt.y;
7535 UNBLOCK_INPUT;
7536 }
7537
7538 if (INTEGERP (top))
7539 *root_y = XINT (top);
7540 else if (*root_y + XINT (dy) <= 0)
7541 *root_y = 0; /* Can happen for negative dy */
7542 else if (*root_y + XINT (dy) + height <= FRAME_W32_DISPLAY_INFO (f)->height)
7543 /* It fits below the pointer */
7544 *root_y += XINT (dy);
7545 else if (height + XINT (dy) <= *root_y)
7546 /* It fits above the pointer. */
7547 *root_y -= height + XINT (dy);
7548 else
7549 /* Put it on the top. */
7550 *root_y = 0;
7551
7552 if (INTEGERP (left))
7553 *root_x = XINT (left);
7554 else if (*root_x + XINT (dx) <= 0)
7555 *root_x = 0; /* Can happen for negative dx */
7556 else if (*root_x + XINT (dx) + width <= FRAME_W32_DISPLAY_INFO (f)->width)
7557 /* It fits to the right of the pointer. */
7558 *root_x += XINT (dx);
7559 else if (width + XINT (dx) <= *root_x)
7560 /* It fits to the left of the pointer. */
7561 *root_x -= width + XINT (dx);
7562 else
7563 /* Put it left justified on the screen -- it ought to fit that way. */
7564 *root_x = 0;
7565 }
7566
7567
7568 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
7569 doc: /* Show STRING in a \"tooltip\" window on frame FRAME.
7570 A tooltip window is a small window displaying a string.
7571
7572 FRAME nil or omitted means use the selected frame.
7573
7574 PARMS is an optional list of frame parameters which can be
7575 used to change the tooltip's appearance.
7576
7577 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
7578 means use the default timeout of 5 seconds.
7579
7580 If the list of frame parameters PARMS contains a `left' parameter,
7581 the tooltip is displayed at that x-position. Otherwise it is
7582 displayed at the mouse position, with offset DX added (default is 5 if
7583 DX isn't specified). Likewise for the y-position; if a `top' frame
7584 parameter is specified, it determines the y-position of the tooltip
7585 window, otherwise it is displayed at the mouse position, with offset
7586 DY added (default is -10).
7587
7588 A tooltip's maximum size is specified by `x-max-tooltip-size'.
7589 Text larger than the specified size is clipped. */)
7590 (string, frame, parms, timeout, dx, dy)
7591 Lisp_Object string, frame, parms, timeout, dx, dy;
7592 {
7593 struct frame *f;
7594 struct window *w;
7595 int root_x, root_y;
7596 struct buffer *old_buffer;
7597 struct text_pos pos;
7598 int i, width, height;
7599 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7600 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7601 int count = SPECPDL_INDEX ();
7602
7603 specbind (Qinhibit_redisplay, Qt);
7604
7605 GCPRO4 (string, parms, frame, timeout);
7606
7607 CHECK_STRING (string);
7608 f = check_x_frame (frame);
7609 if (NILP (timeout))
7610 timeout = make_number (5);
7611 else
7612 CHECK_NATNUM (timeout);
7613
7614 if (NILP (dx))
7615 dx = make_number (5);
7616 else
7617 CHECK_NUMBER (dx);
7618
7619 if (NILP (dy))
7620 dy = make_number (-10);
7621 else
7622 CHECK_NUMBER (dy);
7623
7624 if (NILP (last_show_tip_args))
7625 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
7626
7627 if (!NILP (tip_frame))
7628 {
7629 Lisp_Object last_string = AREF (last_show_tip_args, 0);
7630 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
7631 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
7632
7633 if (EQ (frame, last_frame)
7634 && !NILP (Fequal (last_string, string))
7635 && !NILP (Fequal (last_parms, parms)))
7636 {
7637 struct frame *f = XFRAME (tip_frame);
7638
7639 /* Only DX and DY have changed. */
7640 if (!NILP (tip_timer))
7641 {
7642 Lisp_Object timer = tip_timer;
7643 tip_timer = Qnil;
7644 call1 (Qcancel_timer, timer);
7645 }
7646
7647 BLOCK_INPUT;
7648 compute_tip_xy (f, parms, dx, dy, FRAME_PIXEL_WIDTH (f),
7649 FRAME_PIXEL_HEIGHT (f), &root_x, &root_y);
7650
7651 /* Put tooltip in topmost group and in position. */
7652 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
7653 root_x, root_y, 0, 0,
7654 SWP_NOSIZE | SWP_NOACTIVATE);
7655
7656 /* Ensure tooltip is on top of other topmost windows (eg menus). */
7657 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
7658 0, 0, 0, 0,
7659 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
7660
7661 UNBLOCK_INPUT;
7662 goto start_timer;
7663 }
7664 }
7665
7666 /* Hide a previous tip, if any. */
7667 Fx_hide_tip ();
7668
7669 ASET (last_show_tip_args, 0, string);
7670 ASET (last_show_tip_args, 1, frame);
7671 ASET (last_show_tip_args, 2, parms);
7672
7673 /* Add default values to frame parameters. */
7674 if (NILP (Fassq (Qname, parms)))
7675 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
7676 if (NILP (Fassq (Qinternal_border_width, parms)))
7677 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
7678 if (NILP (Fassq (Qborder_width, parms)))
7679 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
7680 if (NILP (Fassq (Qborder_color, parms)))
7681 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
7682 if (NILP (Fassq (Qbackground_color, parms)))
7683 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
7684 parms);
7685
7686 /* Block input until the tip has been fully drawn, to avoid crashes
7687 when drawing tips in menus. */
7688 BLOCK_INPUT;
7689
7690 /* Create a frame for the tooltip, and record it in the global
7691 variable tip_frame. */
7692 frame = x_create_tip_frame (FRAME_W32_DISPLAY_INFO (f), parms, string);
7693 f = XFRAME (frame);
7694
7695 /* Set up the frame's root window. */
7696 w = XWINDOW (FRAME_ROOT_WINDOW (f));
7697 w->left_col = w->top_line = make_number (0);
7698
7699 if (CONSP (Vx_max_tooltip_size)
7700 && INTEGERP (XCAR (Vx_max_tooltip_size))
7701 && XINT (XCAR (Vx_max_tooltip_size)) > 0
7702 && INTEGERP (XCDR (Vx_max_tooltip_size))
7703 && XINT (XCDR (Vx_max_tooltip_size)) > 0)
7704 {
7705 w->total_cols = XCAR (Vx_max_tooltip_size);
7706 w->total_lines = XCDR (Vx_max_tooltip_size);
7707 }
7708 else
7709 {
7710 w->total_cols = make_number (80);
7711 w->total_lines = make_number (40);
7712 }
7713
7714 FRAME_TOTAL_COLS (f) = XINT (w->total_cols);
7715 adjust_glyphs (f);
7716 w->pseudo_window_p = 1;
7717
7718 /* Display the tooltip text in a temporary buffer. */
7719 old_buffer = current_buffer;
7720 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->buffer));
7721 current_buffer->truncate_lines = Qnil;
7722 clear_glyph_matrix (w->desired_matrix);
7723 clear_glyph_matrix (w->current_matrix);
7724 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
7725 try_window (FRAME_ROOT_WINDOW (f), pos, 0);
7726
7727 /* Compute width and height of the tooltip. */
7728 width = height = 0;
7729 for (i = 0; i < w->desired_matrix->nrows; ++i)
7730 {
7731 struct glyph_row *row = &w->desired_matrix->rows[i];
7732 struct glyph *last;
7733 int row_width;
7734
7735 /* Stop at the first empty row at the end. */
7736 if (!row->enabled_p || !row->displays_text_p)
7737 break;
7738
7739 /* Let the row go over the full width of the frame. */
7740 row->full_width_p = 1;
7741
7742 #ifdef TODO /* Investigate why some fonts need more width than is
7743 calculated for some tooltips. */
7744 /* There's a glyph at the end of rows that is use to place
7745 the cursor there. Don't include the width of this glyph. */
7746 if (row->used[TEXT_AREA])
7747 {
7748 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
7749 row_width = row->pixel_width - last->pixel_width;
7750 }
7751 else
7752 #endif
7753 row_width = row->pixel_width;
7754
7755 /* TODO: find why tips do not draw along baseline as instructed. */
7756 height += row->height;
7757 width = max (width, row_width);
7758 }
7759
7760 /* Add the frame's internal border to the width and height the X
7761 window should have. */
7762 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
7763 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
7764
7765 /* Move the tooltip window where the mouse pointer is. Resize and
7766 show it. */
7767 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
7768
7769 {
7770 /* Adjust Window size to take border into account. */
7771 RECT rect;
7772 rect.left = rect.top = 0;
7773 rect.right = width;
7774 rect.bottom = height;
7775 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
7776 FRAME_EXTERNAL_MENU_BAR (f));
7777
7778 /* Position and size tooltip, and put it in the topmost group.
7779 The add-on of 3 to the 5th argument is a kludge: without it,
7780 some fonts cause the last character of the tip to be truncated,
7781 for some obscure reason. */
7782 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
7783 root_x, root_y, rect.right - rect.left + 3,
7784 rect.bottom - rect.top, SWP_NOACTIVATE);
7785
7786 /* Ensure tooltip is on top of other topmost windows (eg menus). */
7787 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
7788 0, 0, 0, 0,
7789 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
7790
7791 /* Let redisplay know that we have made the frame visible already. */
7792 f->async_visible = 1;
7793
7794 ShowWindow (FRAME_W32_WINDOW (f), SW_SHOWNOACTIVATE);
7795 }
7796
7797 /* Draw into the window. */
7798 w->must_be_updated_p = 1;
7799 update_single_window (w, 1);
7800
7801 UNBLOCK_INPUT;
7802
7803 /* Restore original current buffer. */
7804 set_buffer_internal_1 (old_buffer);
7805 windows_or_buffers_changed = old_windows_or_buffers_changed;
7806
7807 start_timer:
7808 /* Let the tip disappear after timeout seconds. */
7809 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
7810 intern ("x-hide-tip"));
7811
7812 UNGCPRO;
7813 return unbind_to (count, Qnil);
7814 }
7815
7816
7817 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
7818 doc: /* Hide the current tooltip window, if there is any.
7819 Value is t if tooltip was open, nil otherwise. */)
7820 ()
7821 {
7822 int count;
7823 Lisp_Object deleted, frame, timer;
7824 struct gcpro gcpro1, gcpro2;
7825
7826 /* Return quickly if nothing to do. */
7827 if (NILP (tip_timer) && NILP (tip_frame))
7828 return Qnil;
7829
7830 frame = tip_frame;
7831 timer = tip_timer;
7832 GCPRO2 (frame, timer);
7833 tip_frame = tip_timer = deleted = Qnil;
7834
7835 count = SPECPDL_INDEX ();
7836 specbind (Qinhibit_redisplay, Qt);
7837 specbind (Qinhibit_quit, Qt);
7838
7839 if (!NILP (timer))
7840 call1 (Qcancel_timer, timer);
7841
7842 if (FRAMEP (frame))
7843 {
7844 Fdelete_frame (frame, Qnil);
7845 deleted = Qt;
7846 }
7847
7848 UNGCPRO;
7849 return unbind_to (count, deleted);
7850 }
7851
7852
7853 \f
7854 /***********************************************************************
7855 File selection dialog
7856 ***********************************************************************/
7857 extern Lisp_Object Qfile_name_history;
7858
7859 /* Callback for altering the behaviour of the Open File dialog.
7860 Makes the Filename text field contain "Current Directory" and be
7861 read-only when "Directories" is selected in the filter. This
7862 allows us to work around the fact that the standard Open File
7863 dialog does not support directories. */
7864 UINT CALLBACK
7865 file_dialog_callback (hwnd, msg, wParam, lParam)
7866 HWND hwnd;
7867 UINT msg;
7868 WPARAM wParam;
7869 LPARAM lParam;
7870 {
7871 if (msg == WM_NOTIFY)
7872 {
7873 OFNOTIFY * notify = (OFNOTIFY *)lParam;
7874 /* Detect when the Filter dropdown is changed. */
7875 if (notify->hdr.code == CDN_TYPECHANGE
7876 || notify->hdr.code == CDN_INITDONE)
7877 {
7878 HWND dialog = GetParent (hwnd);
7879 HWND edit_control = GetDlgItem (dialog, FILE_NAME_TEXT_FIELD);
7880
7881 /* Directories is in index 2. */
7882 if (notify->lpOFN->nFilterIndex == 2)
7883 {
7884 CommDlg_OpenSave_SetControlText (dialog, FILE_NAME_TEXT_FIELD,
7885 "Current Directory");
7886 EnableWindow (edit_control, FALSE);
7887 }
7888 else
7889 {
7890 /* Don't override default filename on init done. */
7891 if (notify->hdr.code == CDN_TYPECHANGE)
7892 CommDlg_OpenSave_SetControlText (dialog,
7893 FILE_NAME_TEXT_FIELD, "");
7894 EnableWindow (edit_control, TRUE);
7895 }
7896 }
7897 }
7898 return 0;
7899 }
7900
7901 /* Since we compile with _WIN32_WINNT set to 0x0400 (for NT4 compatibility)
7902 we end up with the old file dialogs. Define a big enough struct for the
7903 new dialog to trick GetOpenFileName into giving us the new dialogs on
7904 Windows 2000 and XP. */
7905 typedef struct
7906 {
7907 OPENFILENAME real_details;
7908 void * pReserved;
7909 DWORD dwReserved;
7910 DWORD FlagsEx;
7911 } NEWOPENFILENAME;
7912
7913
7914 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
7915 doc: /* Read file name, prompting with PROMPT in directory DIR.
7916 Use a file selection dialog.
7917 Select DEFAULT-FILENAME in the dialog's file selection box, if
7918 specified. Ensure that file exists if MUSTMATCH is non-nil.
7919 If ONLY-DIR-P is non-nil, the user can only select directories. */)
7920 (prompt, dir, default_filename, mustmatch, only_dir_p)
7921 Lisp_Object prompt, dir, default_filename, mustmatch, only_dir_p;
7922 {
7923 struct frame *f = SELECTED_FRAME ();
7924 Lisp_Object file = Qnil;
7925 int count = SPECPDL_INDEX ();
7926 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
7927 char filename[MAX_PATH + 1];
7928 char init_dir[MAX_PATH + 1];
7929 int default_filter_index = 1; /* 1: All Files, 2: Directories only */
7930
7931 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
7932 CHECK_STRING (prompt);
7933 CHECK_STRING (dir);
7934
7935 /* Create the dialog with PROMPT as title, using DIR as initial
7936 directory and using "*" as pattern. */
7937 dir = Fexpand_file_name (dir, Qnil);
7938 strncpy (init_dir, SDATA (ENCODE_FILE (dir)), MAX_PATH);
7939 init_dir[MAX_PATH] = '\0';
7940 unixtodos_filename (init_dir);
7941
7942 if (STRINGP (default_filename))
7943 {
7944 char *file_name_only;
7945 char *full_path_name = SDATA (ENCODE_FILE (default_filename));
7946
7947 unixtodos_filename (full_path_name);
7948
7949 file_name_only = strrchr (full_path_name, '\\');
7950 if (!file_name_only)
7951 file_name_only = full_path_name;
7952 else
7953 file_name_only++;
7954
7955 strncpy (filename, file_name_only, MAX_PATH);
7956 filename[MAX_PATH] = '\0';
7957 }
7958 else
7959 filename[0] = '\0';
7960
7961 {
7962 NEWOPENFILENAME new_file_details;
7963 BOOL file_opened = FALSE;
7964 OPENFILENAME * file_details = &new_file_details.real_details;
7965
7966 /* Prevent redisplay. */
7967 specbind (Qinhibit_redisplay, Qt);
7968 BLOCK_INPUT;
7969
7970 bzero (&new_file_details, sizeof (new_file_details));
7971 /* Apparently NT4 crashes if you give it an unexpected size.
7972 I'm not sure about Windows 9x, so play it safe. */
7973 if (w32_major_version > 4 && w32_major_version < 95)
7974 file_details->lStructSize = sizeof (new_file_details);
7975 else
7976 file_details->lStructSize = sizeof (file_details);
7977
7978 file_details->hwndOwner = FRAME_W32_WINDOW (f);
7979 /* Undocumented Bug in Common File Dialog:
7980 If a filter is not specified, shell links are not resolved. */
7981 file_details->lpstrFilter = "All Files (*.*)\0*.*\0Directories\0*|*\0\0";
7982 file_details->lpstrFile = filename;
7983 file_details->nMaxFile = sizeof (filename);
7984 file_details->lpstrInitialDir = init_dir;
7985 file_details->lpstrTitle = SDATA (prompt);
7986
7987 if (! NILP (only_dir_p))
7988 default_filter_index = 2;
7989
7990 file_details->nFilterIndex = default_filter_index;
7991
7992 file_details->Flags = (OFN_HIDEREADONLY | OFN_NOCHANGEDIR
7993 | OFN_EXPLORER | OFN_ENABLEHOOK);
7994 if (!NILP (mustmatch))
7995 {
7996 /* Require that the path to the parent directory exists. */
7997 file_details->Flags |= OFN_PATHMUSTEXIST;
7998 /* If we are looking for a file, require that it exists. */
7999 if (NILP (only_dir_p))
8000 file_details->Flags |= OFN_FILEMUSTEXIST;
8001 }
8002
8003 file_details->lpfnHook = (LPOFNHOOKPROC) file_dialog_callback;
8004
8005 file_opened = GetOpenFileName (file_details);
8006
8007 UNBLOCK_INPUT;
8008
8009 if (file_opened)
8010 {
8011 dostounix_filename (filename);
8012
8013 if (file_details->nFilterIndex == 2)
8014 {
8015 /* "Directories" selected - strip dummy file name. */
8016 char * last = strrchr (filename, '/');
8017 *last = '\0';
8018 }
8019
8020 file = DECODE_FILE(build_string (filename));
8021 }
8022 /* User cancelled the dialog without making a selection. */
8023 else if (!CommDlgExtendedError ())
8024 file = Qnil;
8025 /* An error occurred, fallback on reading from the mini-buffer. */
8026 else
8027 file = Fcompleting_read (prompt, intern ("read-file-name-internal"),
8028 dir, mustmatch, dir, Qfile_name_history,
8029 default_filename, Qnil);
8030
8031 file = unbind_to (count, file);
8032 }
8033
8034 UNGCPRO;
8035
8036 /* Make "Cancel" equivalent to C-g. */
8037 if (NILP (file))
8038 Fsignal (Qquit, Qnil);
8039
8040 return unbind_to (count, file);
8041 }
8042
8043
8044 \f
8045 /***********************************************************************
8046 w32 specialized functions
8047 ***********************************************************************/
8048
8049 DEFUN ("w32-select-font", Fw32_select_font, Sw32_select_font, 0, 2, 0,
8050 doc: /* Select a font for the named FRAME using the W32 font dialog.
8051 Returns an X-style font string corresponding to the selection.
8052
8053 If FRAME is omitted or nil, it defaults to the selected frame.
8054 If INCLUDE-PROPORTIONAL is non-nil, include proportional fonts
8055 in the font selection dialog. */)
8056 (frame, include_proportional)
8057 Lisp_Object frame, include_proportional;
8058 {
8059 FRAME_PTR f = check_x_frame (frame);
8060 CHOOSEFONT cf;
8061 LOGFONT lf;
8062 TEXTMETRIC tm;
8063 HDC hdc;
8064 HANDLE oldobj;
8065 char buf[100];
8066
8067 bzero (&cf, sizeof (cf));
8068 bzero (&lf, sizeof (lf));
8069
8070 cf.lStructSize = sizeof (cf);
8071 cf.hwndOwner = FRAME_W32_WINDOW (f);
8072 cf.Flags = CF_FORCEFONTEXIST | CF_SCREENFONTS | CF_NOVERTFONTS;
8073
8074 /* Unless include_proportional is non-nil, limit the selection to
8075 monospaced fonts. */
8076 if (NILP (include_proportional))
8077 cf.Flags |= CF_FIXEDPITCHONLY;
8078
8079 cf.lpLogFont = &lf;
8080
8081 /* Initialize as much of the font details as we can from the current
8082 default font. */
8083 hdc = GetDC (FRAME_W32_WINDOW (f));
8084 oldobj = SelectObject (hdc, FRAME_FONT (f)->hfont);
8085 GetTextFace (hdc, LF_FACESIZE, lf.lfFaceName);
8086 if (GetTextMetrics (hdc, &tm))
8087 {
8088 lf.lfHeight = tm.tmInternalLeading - tm.tmHeight;
8089 lf.lfWeight = tm.tmWeight;
8090 lf.lfItalic = tm.tmItalic;
8091 lf.lfUnderline = tm.tmUnderlined;
8092 lf.lfStrikeOut = tm.tmStruckOut;
8093 lf.lfCharSet = tm.tmCharSet;
8094 cf.Flags |= CF_INITTOLOGFONTSTRUCT;
8095 }
8096 SelectObject (hdc, oldobj);
8097 ReleaseDC (FRAME_W32_WINDOW (f), hdc);
8098
8099 if (!ChooseFont (&cf) || !w32_to_x_font (&lf, buf, 100, NULL))
8100 return Qnil;
8101
8102 return build_string (buf);
8103 }
8104
8105 DEFUN ("w32-send-sys-command", Fw32_send_sys_command,
8106 Sw32_send_sys_command, 1, 2, 0,
8107 doc: /* Send frame a Windows WM_SYSCOMMAND message of type COMMAND.
8108 Some useful values for COMMAND are #xf030 to maximize frame (#xf020
8109 to minimize), #xf120 to restore frame to original size, and #xf100
8110 to activate the menubar for keyboard access. #xf140 activates the
8111 screen saver if defined.
8112
8113 If optional parameter FRAME is not specified, use selected frame. */)
8114 (command, frame)
8115 Lisp_Object command, frame;
8116 {
8117 FRAME_PTR f = check_x_frame (frame);
8118
8119 CHECK_NUMBER (command);
8120
8121 PostMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, XINT (command), 0);
8122
8123 return Qnil;
8124 }
8125
8126 DEFUN ("w32-shell-execute", Fw32_shell_execute, Sw32_shell_execute, 2, 4, 0,
8127 doc: /* Get Windows to perform OPERATION on DOCUMENT.
8128 This is a wrapper around the ShellExecute system function, which
8129 invokes the application registered to handle OPERATION for DOCUMENT.
8130 OPERATION is typically \"open\", \"print\" or \"explore\" (but can be
8131 nil for the default action), and DOCUMENT is typically the name of a
8132 document file or URL, but can also be a program executable to run or
8133 a directory to open in the Windows Explorer.
8134
8135 If DOCUMENT is a program executable, PARAMETERS can be a string
8136 containing command line parameters, but otherwise should be nil.
8137
8138 SHOW-FLAG can be used to control whether the invoked application is hidden
8139 or minimized. If SHOW-FLAG is nil, the application is displayed normally,
8140 otherwise it is an integer representing a ShowWindow flag:
8141
8142 0 - start hidden
8143 1 - start normally
8144 3 - start maximized
8145 6 - start minimized */)
8146 (operation, document, parameters, show_flag)
8147 Lisp_Object operation, document, parameters, show_flag;
8148 {
8149 Lisp_Object current_dir;
8150
8151 CHECK_STRING (document);
8152
8153 /* Encode filename and current directory. */
8154 current_dir = ENCODE_FILE (current_buffer->directory);
8155 document = ENCODE_FILE (document);
8156 if ((int) ShellExecute (NULL,
8157 (STRINGP (operation) ?
8158 SDATA (operation) : NULL),
8159 SDATA (document),
8160 (STRINGP (parameters) ?
8161 SDATA (parameters) : NULL),
8162 SDATA (current_dir),
8163 (INTEGERP (show_flag) ?
8164 XINT (show_flag) : SW_SHOWDEFAULT))
8165 > 32)
8166 return Qt;
8167 error ("ShellExecute failed: %s", w32_strerror (0));
8168 }
8169
8170 /* Lookup virtual keycode from string representing the name of a
8171 non-ascii keystroke into the corresponding virtual key, using
8172 lispy_function_keys. */
8173 static int
8174 lookup_vk_code (char *key)
8175 {
8176 int i;
8177
8178 for (i = 0; i < 256; i++)
8179 if (lispy_function_keys[i] != 0
8180 && strcmp (lispy_function_keys[i], key) == 0)
8181 return i;
8182
8183 return -1;
8184 }
8185
8186 /* Convert a one-element vector style key sequence to a hot key
8187 definition. */
8188 static Lisp_Object
8189 w32_parse_hot_key (key)
8190 Lisp_Object key;
8191 {
8192 /* Copied from Fdefine_key and store_in_keymap. */
8193 register Lisp_Object c;
8194 int vk_code;
8195 int lisp_modifiers;
8196 int w32_modifiers;
8197 struct gcpro gcpro1;
8198
8199 CHECK_VECTOR (key);
8200
8201 if (XFASTINT (Flength (key)) != 1)
8202 return Qnil;
8203
8204 GCPRO1 (key);
8205
8206 c = Faref (key, make_number (0));
8207
8208 if (CONSP (c) && lucid_event_type_list_p (c))
8209 c = Fevent_convert_list (c);
8210
8211 UNGCPRO;
8212
8213 if (! INTEGERP (c) && ! SYMBOLP (c))
8214 error ("Key definition is invalid");
8215
8216 /* Work out the base key and the modifiers. */
8217 if (SYMBOLP (c))
8218 {
8219 c = parse_modifiers (c);
8220 lisp_modifiers = XINT (Fcar (Fcdr (c)));
8221 c = Fcar (c);
8222 if (!SYMBOLP (c))
8223 abort ();
8224 vk_code = lookup_vk_code (SDATA (SYMBOL_NAME (c)));
8225 }
8226 else if (INTEGERP (c))
8227 {
8228 lisp_modifiers = XINT (c) & ~CHARACTERBITS;
8229 /* Many ascii characters are their own virtual key code. */
8230 vk_code = XINT (c) & CHARACTERBITS;
8231 }
8232
8233 if (vk_code < 0 || vk_code > 255)
8234 return Qnil;
8235
8236 if ((lisp_modifiers & meta_modifier) != 0
8237 && !NILP (Vw32_alt_is_meta))
8238 lisp_modifiers |= alt_modifier;
8239
8240 /* Supply defs missing from mingw32. */
8241 #ifndef MOD_ALT
8242 #define MOD_ALT 0x0001
8243 #define MOD_CONTROL 0x0002
8244 #define MOD_SHIFT 0x0004
8245 #define MOD_WIN 0x0008
8246 #endif
8247
8248 /* Convert lisp modifiers to Windows hot-key form. */
8249 w32_modifiers = (lisp_modifiers & hyper_modifier) ? MOD_WIN : 0;
8250 w32_modifiers |= (lisp_modifiers & alt_modifier) ? MOD_ALT : 0;
8251 w32_modifiers |= (lisp_modifiers & ctrl_modifier) ? MOD_CONTROL : 0;
8252 w32_modifiers |= (lisp_modifiers & shift_modifier) ? MOD_SHIFT : 0;
8253
8254 return HOTKEY (vk_code, w32_modifiers);
8255 }
8256
8257 DEFUN ("w32-register-hot-key", Fw32_register_hot_key,
8258 Sw32_register_hot_key, 1, 1, 0,
8259 doc: /* Register KEY as a hot-key combination.
8260 Certain key combinations like Alt-Tab are reserved for system use on
8261 Windows, and therefore are normally intercepted by the system. However,
8262 most of these key combinations can be received by registering them as
8263 hot-keys, overriding their special meaning.
8264
8265 KEY must be a one element key definition in vector form that would be
8266 acceptable to `define-key' (e.g. [A-tab] for Alt-Tab). The meta
8267 modifier is interpreted as Alt if `w32-alt-is-meta' is t, and hyper
8268 is always interpreted as the Windows modifier keys.
8269
8270 The return value is the hotkey-id if registered, otherwise nil. */)
8271 (key)
8272 Lisp_Object key;
8273 {
8274 key = w32_parse_hot_key (key);
8275
8276 if (NILP (Fmemq (key, w32_grabbed_keys)))
8277 {
8278 /* Reuse an empty slot if possible. */
8279 Lisp_Object item = Fmemq (Qnil, w32_grabbed_keys);
8280
8281 /* Safe to add new key to list, even if we have focus. */
8282 if (NILP (item))
8283 w32_grabbed_keys = Fcons (key, w32_grabbed_keys);
8284 else
8285 XSETCAR (item, key);
8286
8287 /* Notify input thread about new hot-key definition, so that it
8288 takes effect without needing to switch focus. */
8289 #ifdef USE_LISP_UNION_TYPE
8290 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
8291 (WPARAM) key.i, 0);
8292 #else
8293 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
8294 (WPARAM) key, 0);
8295 #endif
8296 }
8297
8298 return key;
8299 }
8300
8301 DEFUN ("w32-unregister-hot-key", Fw32_unregister_hot_key,
8302 Sw32_unregister_hot_key, 1, 1, 0,
8303 doc: /* Unregister KEY as a hot-key combination. */)
8304 (key)
8305 Lisp_Object key;
8306 {
8307 Lisp_Object item;
8308
8309 if (!INTEGERP (key))
8310 key = w32_parse_hot_key (key);
8311
8312 item = Fmemq (key, w32_grabbed_keys);
8313
8314 if (!NILP (item))
8315 {
8316 /* Notify input thread about hot-key definition being removed, so
8317 that it takes effect without needing focus switch. */
8318 #ifdef USE_LISP_UNION_TYPE
8319 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
8320 (WPARAM) XINT (XCAR (item)), (LPARAM) item.i))
8321 #else
8322 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
8323 (WPARAM) XINT (XCAR (item)), (LPARAM) item))
8324
8325 #endif
8326 {
8327 MSG msg;
8328 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
8329 }
8330 return Qt;
8331 }
8332 return Qnil;
8333 }
8334
8335 DEFUN ("w32-registered-hot-keys", Fw32_registered_hot_keys,
8336 Sw32_registered_hot_keys, 0, 0, 0,
8337 doc: /* Return list of registered hot-key IDs. */)
8338 ()
8339 {
8340 return Fcopy_sequence (w32_grabbed_keys);
8341 }
8342
8343 DEFUN ("w32-reconstruct-hot-key", Fw32_reconstruct_hot_key,
8344 Sw32_reconstruct_hot_key, 1, 1, 0,
8345 doc: /* Convert hot-key ID to a lisp key combination.
8346 usage: (w32-reconstruct-hot-key ID) */)
8347 (hotkeyid)
8348 Lisp_Object hotkeyid;
8349 {
8350 int vk_code, w32_modifiers;
8351 Lisp_Object key;
8352
8353 CHECK_NUMBER (hotkeyid);
8354
8355 vk_code = HOTKEY_VK_CODE (hotkeyid);
8356 w32_modifiers = HOTKEY_MODIFIERS (hotkeyid);
8357
8358 if (lispy_function_keys[vk_code])
8359 key = intern (lispy_function_keys[vk_code]);
8360 else
8361 key = make_number (vk_code);
8362
8363 key = Fcons (key, Qnil);
8364 if (w32_modifiers & MOD_SHIFT)
8365 key = Fcons (Qshift, key);
8366 if (w32_modifiers & MOD_CONTROL)
8367 key = Fcons (Qctrl, key);
8368 if (w32_modifiers & MOD_ALT)
8369 key = Fcons (NILP (Vw32_alt_is_meta) ? Qalt : Qmeta, key);
8370 if (w32_modifiers & MOD_WIN)
8371 key = Fcons (Qhyper, key);
8372
8373 return key;
8374 }
8375
8376 DEFUN ("w32-toggle-lock-key", Fw32_toggle_lock_key,
8377 Sw32_toggle_lock_key, 1, 2, 0,
8378 doc: /* Toggle the state of the lock key KEY.
8379 KEY can be `capslock', `kp-numlock', or `scroll'.
8380 If the optional parameter NEW-STATE is a number, then the state of KEY
8381 is set to off if the low bit of NEW-STATE is zero, otherwise on. */)
8382 (key, new_state)
8383 Lisp_Object key, new_state;
8384 {
8385 int vk_code;
8386
8387 if (EQ (key, intern ("capslock")))
8388 vk_code = VK_CAPITAL;
8389 else if (EQ (key, intern ("kp-numlock")))
8390 vk_code = VK_NUMLOCK;
8391 else if (EQ (key, intern ("scroll")))
8392 vk_code = VK_SCROLL;
8393 else
8394 return Qnil;
8395
8396 if (!dwWindowsThreadId)
8397 return make_number (w32_console_toggle_lock_key (vk_code, new_state));
8398
8399 #ifdef USE_LISP_UNION_TYPE
8400 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
8401 (WPARAM) vk_code, (LPARAM) new_state.i))
8402 #else
8403 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
8404 (WPARAM) vk_code, (LPARAM) new_state))
8405 #endif
8406 {
8407 MSG msg;
8408 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
8409 return make_number (msg.wParam);
8410 }
8411 return Qnil;
8412 }
8413 \f
8414 DEFUN ("file-system-info", Ffile_system_info, Sfile_system_info, 1, 1, 0,
8415 doc: /* Return storage information about the file system FILENAME is on.
8416 Value is a list of floats (TOTAL FREE AVAIL), where TOTAL is the total
8417 storage of the file system, FREE is the free storage, and AVAIL is the
8418 storage available to a non-superuser. All 3 numbers are in bytes.
8419 If the underlying system call fails, value is nil. */)
8420 (filename)
8421 Lisp_Object filename;
8422 {
8423 Lisp_Object encoded, value;
8424
8425 CHECK_STRING (filename);
8426 filename = Fexpand_file_name (filename, Qnil);
8427 encoded = ENCODE_FILE (filename);
8428
8429 value = Qnil;
8430
8431 /* Determining the required information on Windows turns out, sadly,
8432 to be more involved than one would hope. The original Win32 api
8433 call for this will return bogus information on some systems, but we
8434 must dynamically probe for the replacement api, since that was
8435 added rather late on. */
8436 {
8437 HMODULE hKernel = GetModuleHandle ("kernel32");
8438 BOOL (*pfn_GetDiskFreeSpaceEx)
8439 (char *, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER)
8440 = (void *) GetProcAddress (hKernel, "GetDiskFreeSpaceEx");
8441
8442 /* On Windows, we may need to specify the root directory of the
8443 volume holding FILENAME. */
8444 char rootname[MAX_PATH];
8445 char *name = SDATA (encoded);
8446
8447 /* find the root name of the volume if given */
8448 if (isalpha (name[0]) && name[1] == ':')
8449 {
8450 rootname[0] = name[0];
8451 rootname[1] = name[1];
8452 rootname[2] = '\\';
8453 rootname[3] = 0;
8454 }
8455 else if (IS_DIRECTORY_SEP (name[0]) && IS_DIRECTORY_SEP (name[1]))
8456 {
8457 char *str = rootname;
8458 int slashes = 4;
8459 do
8460 {
8461 if (IS_DIRECTORY_SEP (*name) && --slashes == 0)
8462 break;
8463 *str++ = *name++;
8464 }
8465 while ( *name );
8466
8467 *str++ = '\\';
8468 *str = 0;
8469 }
8470
8471 if (pfn_GetDiskFreeSpaceEx)
8472 {
8473 /* Unsigned large integers cannot be cast to double, so
8474 use signed ones instead. */
8475 LARGE_INTEGER availbytes;
8476 LARGE_INTEGER freebytes;
8477 LARGE_INTEGER totalbytes;
8478
8479 if (pfn_GetDiskFreeSpaceEx(rootname,
8480 (ULARGE_INTEGER *)&availbytes,
8481 (ULARGE_INTEGER *)&totalbytes,
8482 (ULARGE_INTEGER *)&freebytes))
8483 value = list3 (make_float ((double) totalbytes.QuadPart),
8484 make_float ((double) freebytes.QuadPart),
8485 make_float ((double) availbytes.QuadPart));
8486 }
8487 else
8488 {
8489 DWORD sectors_per_cluster;
8490 DWORD bytes_per_sector;
8491 DWORD free_clusters;
8492 DWORD total_clusters;
8493
8494 if (GetDiskFreeSpace(rootname,
8495 &sectors_per_cluster,
8496 &bytes_per_sector,
8497 &free_clusters,
8498 &total_clusters))
8499 value = list3 (make_float ((double) total_clusters
8500 * sectors_per_cluster * bytes_per_sector),
8501 make_float ((double) free_clusters
8502 * sectors_per_cluster * bytes_per_sector),
8503 make_float ((double) free_clusters
8504 * sectors_per_cluster * bytes_per_sector));
8505 }
8506 }
8507
8508 return value;
8509 }
8510 \f
8511 DEFUN ("default-printer-name", Fdefault_printer_name, Sdefault_printer_name,
8512 0, 0, 0, doc: /* Return the name of Windows default printer device. */)
8513 ()
8514 {
8515 static char pname_buf[256];
8516 int err;
8517 HANDLE hPrn;
8518 PRINTER_INFO_2 *ppi2 = NULL;
8519 DWORD dwNeeded = 0, dwReturned = 0;
8520
8521 /* Retrieve the default string from Win.ini (the registry).
8522 * String will be in form "printername,drivername,portname".
8523 * This is the most portable way to get the default printer. */
8524 if (GetProfileString ("windows", "device", ",,", pname_buf, sizeof (pname_buf)) <= 0)
8525 return Qnil;
8526 /* printername precedes first "," character */
8527 strtok (pname_buf, ",");
8528 /* We want to know more than the printer name */
8529 if (!OpenPrinter (pname_buf, &hPrn, NULL))
8530 return Qnil;
8531 GetPrinter (hPrn, 2, NULL, 0, &dwNeeded);
8532 if (dwNeeded == 0)
8533 {
8534 ClosePrinter (hPrn);
8535 return Qnil;
8536 }
8537 /* Allocate memory for the PRINTER_INFO_2 struct */
8538 ppi2 = (PRINTER_INFO_2 *) xmalloc (dwNeeded);
8539 if (!ppi2)
8540 {
8541 ClosePrinter (hPrn);
8542 return Qnil;
8543 }
8544 /* Call GetPrinter() again with big enouth memory block */
8545 err = GetPrinter (hPrn, 2, (LPBYTE)ppi2, dwNeeded, &dwReturned);
8546 ClosePrinter (hPrn);
8547 if (!err)
8548 {
8549 xfree(ppi2);
8550 return Qnil;
8551 }
8552
8553 if (ppi2)
8554 {
8555 if (ppi2->Attributes & PRINTER_ATTRIBUTE_SHARED && ppi2->pServerName)
8556 {
8557 /* a remote printer */
8558 if (*ppi2->pServerName == '\\')
8559 _snprintf(pname_buf, sizeof (pname_buf), "%s\\%s", ppi2->pServerName,
8560 ppi2->pShareName);
8561 else
8562 _snprintf(pname_buf, sizeof (pname_buf), "\\\\%s\\%s", ppi2->pServerName,
8563 ppi2->pShareName);
8564 pname_buf[sizeof (pname_buf) - 1] = '\0';
8565 }
8566 else
8567 {
8568 /* a local printer */
8569 strncpy(pname_buf, ppi2->pPortName, sizeof (pname_buf));
8570 pname_buf[sizeof (pname_buf) - 1] = '\0';
8571 /* `pPortName' can include several ports, delimited by ','.
8572 * we only use the first one. */
8573 strtok(pname_buf, ",");
8574 }
8575 xfree(ppi2);
8576 }
8577
8578 return build_string (pname_buf);
8579 }
8580 \f
8581 /***********************************************************************
8582 Initialization
8583 ***********************************************************************/
8584
8585 /* Keep this list in the same order as frame_parms in frame.c.
8586 Use 0 for unsupported frame parameters. */
8587
8588 frame_parm_handler w32_frame_parm_handlers[] =
8589 {
8590 x_set_autoraise,
8591 x_set_autolower,
8592 x_set_background_color,
8593 x_set_border_color,
8594 x_set_border_width,
8595 x_set_cursor_color,
8596 x_set_cursor_type,
8597 x_set_font,
8598 x_set_foreground_color,
8599 x_set_icon_name,
8600 x_set_icon_type,
8601 x_set_internal_border_width,
8602 x_set_menu_bar_lines,
8603 x_set_mouse_color,
8604 x_explicitly_set_name,
8605 x_set_scroll_bar_width,
8606 x_set_title,
8607 x_set_unsplittable,
8608 x_set_vertical_scroll_bars,
8609 x_set_visibility,
8610 x_set_tool_bar_lines,
8611 0, /* x_set_scroll_bar_foreground, */
8612 0, /* x_set_scroll_bar_background, */
8613 x_set_screen_gamma,
8614 x_set_line_spacing,
8615 x_set_fringe_width,
8616 x_set_fringe_width,
8617 0, /* x_set_wait_for_wm, */
8618 x_set_fullscreen,
8619 };
8620
8621 void
8622 syms_of_w32fns ()
8623 {
8624 globals_of_w32fns ();
8625 /* This is zero if not using MS-Windows. */
8626 w32_in_use = 0;
8627 track_mouse_window = NULL;
8628
8629 w32_visible_system_caret_hwnd = NULL;
8630
8631 Qnone = intern ("none");
8632 staticpro (&Qnone);
8633 Qsuppress_icon = intern ("suppress-icon");
8634 staticpro (&Qsuppress_icon);
8635 Qundefined_color = intern ("undefined-color");
8636 staticpro (&Qundefined_color);
8637 Qcancel_timer = intern ("cancel-timer");
8638 staticpro (&Qcancel_timer);
8639
8640 Qhyper = intern ("hyper");
8641 staticpro (&Qhyper);
8642 Qsuper = intern ("super");
8643 staticpro (&Qsuper);
8644 Qmeta = intern ("meta");
8645 staticpro (&Qmeta);
8646 Qalt = intern ("alt");
8647 staticpro (&Qalt);
8648 Qctrl = intern ("ctrl");
8649 staticpro (&Qctrl);
8650 Qcontrol = intern ("control");
8651 staticpro (&Qcontrol);
8652 Qshift = intern ("shift");
8653 staticpro (&Qshift);
8654 /* This is the end of symbol initialization. */
8655
8656 /* Text property `display' should be nonsticky by default. */
8657 Vtext_property_default_nonsticky
8658 = Fcons (Fcons (Qdisplay, Qt), Vtext_property_default_nonsticky);
8659
8660
8661 Fput (Qundefined_color, Qerror_conditions,
8662 Fcons (Qundefined_color, Fcons (Qerror, Qnil)));
8663 Fput (Qundefined_color, Qerror_message,
8664 build_string ("Undefined color"));
8665
8666 staticpro (&w32_grabbed_keys);
8667 w32_grabbed_keys = Qnil;
8668
8669 DEFVAR_LISP ("w32-color-map", &Vw32_color_map,
8670 doc: /* An array of color name mappings for Windows. */);
8671 Vw32_color_map = Qnil;
8672
8673 DEFVAR_LISP ("w32-pass-alt-to-system", &Vw32_pass_alt_to_system,
8674 doc: /* Non-nil if alt key presses are passed on to Windows.
8675 When non-nil, for example, alt pressed and released and then space will
8676 open the System menu. When nil, Emacs silently swallows alt key events. */);
8677 Vw32_pass_alt_to_system = Qnil;
8678
8679 DEFVAR_LISP ("w32-alt-is-meta", &Vw32_alt_is_meta,
8680 doc: /* Non-nil if the alt key is to be considered the same as the meta key.
8681 When nil, Emacs will translate the alt key to the Alt modifier, and not Meta. */);
8682 Vw32_alt_is_meta = Qt;
8683
8684 DEFVAR_INT ("w32-quit-key", &w32_quit_key,
8685 doc: /* If non-zero, the virtual key code for an alternative quit key. */);
8686 w32_quit_key = 0;
8687
8688 DEFVAR_LISP ("w32-pass-lwindow-to-system",
8689 &Vw32_pass_lwindow_to_system,
8690 doc: /* Non-nil if the left \"Windows\" key is passed on to Windows.
8691 When non-nil, the Start menu is opened by tapping the key. */);
8692 Vw32_pass_lwindow_to_system = Qt;
8693
8694 DEFVAR_LISP ("w32-pass-rwindow-to-system",
8695 &Vw32_pass_rwindow_to_system,
8696 doc: /* Non-nil if the right \"Windows\" key is passed on to Windows.
8697 When non-nil, the Start menu is opened by tapping the key. */);
8698 Vw32_pass_rwindow_to_system = Qt;
8699
8700 DEFVAR_LISP ("w32-phantom-key-code",
8701 &Vw32_phantom_key_code,
8702 doc: /* Virtual key code used to generate \"phantom\" key presses.
8703 Value is a number between 0 and 255.
8704
8705 Phantom key presses are generated in order to stop the system from
8706 acting on \"Windows\" key events when `w32-pass-lwindow-to-system' or
8707 `w32-pass-rwindow-to-system' is nil. */);
8708 /* Although 255 is technically not a valid key code, it works and
8709 means that this hack won't interfere with any real key code. */
8710 XSETINT (Vw32_phantom_key_code, 255);
8711
8712 DEFVAR_LISP ("w32-enable-num-lock",
8713 &Vw32_enable_num_lock,
8714 doc: /* Non-nil if Num Lock should act normally.
8715 Set to nil to see Num Lock as the key `kp-numlock'. */);
8716 Vw32_enable_num_lock = Qt;
8717
8718 DEFVAR_LISP ("w32-enable-caps-lock",
8719 &Vw32_enable_caps_lock,
8720 doc: /* Non-nil if Caps Lock should act normally.
8721 Set to nil to see Caps Lock as the key `capslock'. */);
8722 Vw32_enable_caps_lock = Qt;
8723
8724 DEFVAR_LISP ("w32-scroll-lock-modifier",
8725 &Vw32_scroll_lock_modifier,
8726 doc: /* Modifier to use for the Scroll Lock on state.
8727 The value can be hyper, super, meta, alt, control or shift for the
8728 respective modifier, or nil to see Scroll Lock as the key `scroll'.
8729 Any other value will cause the key to be ignored. */);
8730 Vw32_scroll_lock_modifier = Qt;
8731
8732 DEFVAR_LISP ("w32-lwindow-modifier",
8733 &Vw32_lwindow_modifier,
8734 doc: /* Modifier to use for the left \"Windows\" key.
8735 The value can be hyper, super, meta, alt, control or shift for the
8736 respective modifier, or nil to appear as the key `lwindow'.
8737 Any other value will cause the key to be ignored. */);
8738 Vw32_lwindow_modifier = Qnil;
8739
8740 DEFVAR_LISP ("w32-rwindow-modifier",
8741 &Vw32_rwindow_modifier,
8742 doc: /* Modifier to use for the right \"Windows\" key.
8743 The value can be hyper, super, meta, alt, control or shift for the
8744 respective modifier, or nil to appear as the key `rwindow'.
8745 Any other value will cause the key to be ignored. */);
8746 Vw32_rwindow_modifier = Qnil;
8747
8748 DEFVAR_LISP ("w32-apps-modifier",
8749 &Vw32_apps_modifier,
8750 doc: /* Modifier to use for the \"Apps\" key.
8751 The value can be hyper, super, meta, alt, control or shift for the
8752 respective modifier, or nil to appear as the key `apps'.
8753 Any other value will cause the key to be ignored. */);
8754 Vw32_apps_modifier = Qnil;
8755
8756 DEFVAR_BOOL ("w32-enable-synthesized-fonts", &w32_enable_synthesized_fonts,
8757 doc: /* Non-nil enables selection of artificially italicized and bold fonts. */);
8758 w32_enable_synthesized_fonts = 0;
8759
8760 DEFVAR_LISP ("w32-enable-palette", &Vw32_enable_palette,
8761 doc: /* Non-nil enables Windows palette management to map colors exactly. */);
8762 Vw32_enable_palette = Qt;
8763
8764 DEFVAR_INT ("w32-mouse-button-tolerance",
8765 &w32_mouse_button_tolerance,
8766 doc: /* Analogue of double click interval for faking middle mouse events.
8767 The value is the minimum time in milliseconds that must elapse between
8768 left/right button down events before they are considered distinct events.
8769 If both mouse buttons are depressed within this interval, a middle mouse
8770 button down event is generated instead. */);
8771 w32_mouse_button_tolerance = GetDoubleClickTime () / 2;
8772
8773 DEFVAR_INT ("w32-mouse-move-interval",
8774 &w32_mouse_move_interval,
8775 doc: /* Minimum interval between mouse move events.
8776 The value is the minimum time in milliseconds that must elapse between
8777 successive mouse move (or scroll bar drag) events before they are
8778 reported as lisp events. */);
8779 w32_mouse_move_interval = 0;
8780
8781 DEFVAR_BOOL ("w32-pass-extra-mouse-buttons-to-system",
8782 &w32_pass_extra_mouse_buttons_to_system,
8783 doc: /* Non-nil if the fourth and fifth mouse buttons are passed to Windows.
8784 Recent versions of Windows support mice with up to five buttons.
8785 Since most applications don't support these extra buttons, most mouse
8786 drivers will allow you to map them to functions at the system level.
8787 If this variable is non-nil, Emacs will pass them on, allowing the
8788 system to handle them. */);
8789 w32_pass_extra_mouse_buttons_to_system = 0;
8790
8791 DEFVAR_LISP ("x-pointer-shape", &Vx_pointer_shape,
8792 doc: /* The shape of the pointer when over text.
8793 Changing the value does not affect existing frames
8794 unless you set the mouse color. */);
8795 Vx_pointer_shape = Qnil;
8796
8797 Vx_nontext_pointer_shape = Qnil;
8798
8799 Vx_mode_pointer_shape = Qnil;
8800
8801 DEFVAR_LISP ("x-hourglass-pointer-shape", &Vx_hourglass_pointer_shape,
8802 doc: /* The shape of the pointer when Emacs is busy.
8803 This variable takes effect when you create a new frame
8804 or when you set the mouse color. */);
8805 Vx_hourglass_pointer_shape = Qnil;
8806
8807 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
8808 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
8809 display_hourglass_p = 1;
8810
8811 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
8812 doc: /* *Seconds to wait before displaying an hourglass pointer.
8813 Value must be an integer or float. */);
8814 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
8815
8816 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
8817 &Vx_sensitive_text_pointer_shape,
8818 doc: /* The shape of the pointer when over mouse-sensitive text.
8819 This variable takes effect when you create a new frame
8820 or when you set the mouse color. */);
8821 Vx_sensitive_text_pointer_shape = Qnil;
8822
8823 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
8824 &Vx_window_horizontal_drag_shape,
8825 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
8826 This variable takes effect when you create a new frame
8827 or when you set the mouse color. */);
8828 Vx_window_horizontal_drag_shape = Qnil;
8829
8830 DEFVAR_LISP ("x-cursor-fore-pixel", &Vx_cursor_fore_pixel,
8831 doc: /* A string indicating the foreground color of the cursor box. */);
8832 Vx_cursor_fore_pixel = Qnil;
8833
8834 DEFVAR_LISP ("x-max-tooltip-size", &Vx_max_tooltip_size,
8835 doc: /* Maximum size for tooltips.
8836 Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
8837 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
8838
8839 DEFVAR_LISP ("x-no-window-manager", &Vx_no_window_manager,
8840 doc: /* Non-nil if no window manager is in use.
8841 Emacs doesn't try to figure this out; this is always nil
8842 unless you set it to something else. */);
8843 /* We don't have any way to find this out, so set it to nil
8844 and maybe the user would like to set it to t. */
8845 Vx_no_window_manager = Qnil;
8846
8847 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
8848 &Vx_pixel_size_width_font_regexp,
8849 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
8850
8851 Since Emacs gets width of a font matching with this regexp from
8852 PIXEL_SIZE field of the name, font finding mechanism gets faster for
8853 such a font. This is especially effective for such large fonts as
8854 Chinese, Japanese, and Korean. */);
8855 Vx_pixel_size_width_font_regexp = Qnil;
8856
8857 DEFVAR_LISP ("w32-bdf-filename-alist",
8858 &Vw32_bdf_filename_alist,
8859 doc: /* List of bdf fonts and their corresponding filenames. */);
8860 Vw32_bdf_filename_alist = Qnil;
8861
8862 DEFVAR_BOOL ("w32-strict-fontnames",
8863 &w32_strict_fontnames,
8864 doc: /* Non-nil means only use fonts that are exact matches for those requested.
8865 Default is nil, which allows old fontnames that are not XLFD compliant,
8866 and allows third-party CJK display to work by specifying false charset
8867 fields to trick Emacs into translating to Big5, SJIS etc.
8868 Setting this to t will prevent wrong fonts being selected when
8869 fontsets are automatically created. */);
8870 w32_strict_fontnames = 0;
8871
8872 DEFVAR_BOOL ("w32-strict-painting",
8873 &w32_strict_painting,
8874 doc: /* Non-nil means use strict rules for repainting frames.
8875 Set this to nil to get the old behavior for repainting; this should
8876 only be necessary if the default setting causes problems. */);
8877 w32_strict_painting = 1;
8878
8879 DEFVAR_LISP ("w32-charset-info-alist",
8880 &Vw32_charset_info_alist,
8881 doc: /* Alist linking Emacs character sets to Windows fonts and codepages.
8882 Each entry should be of the form:
8883
8884 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE))
8885
8886 where CHARSET_NAME is a string used in font names to identify the charset,
8887 WINDOWS_CHARSET is a symbol that can be one of:
8888 w32-charset-ansi, w32-charset-default, w32-charset-symbol,
8889 w32-charset-shiftjis, w32-charset-hangeul, w32-charset-gb2312,
8890 w32-charset-chinesebig5,
8891 w32-charset-johab, w32-charset-hebrew,
8892 w32-charset-arabic, w32-charset-greek, w32-charset-turkish,
8893 w32-charset-vietnamese, w32-charset-thai, w32-charset-easteurope,
8894 w32-charset-russian, w32-charset-mac, w32-charset-baltic,
8895 w32-charset-unicode,
8896 or w32-charset-oem.
8897 CODEPAGE should be an integer specifying the codepage that should be used
8898 to display the character set, t to do no translation and output as Unicode,
8899 or nil to do no translation and output as 8 bit (or multibyte on far-east
8900 versions of Windows) characters. */);
8901 Vw32_charset_info_alist = Qnil;
8902
8903 staticpro (&Qw32_charset_ansi);
8904 Qw32_charset_ansi = intern ("w32-charset-ansi");
8905 staticpro (&Qw32_charset_symbol);
8906 Qw32_charset_default = intern ("w32-charset-default");
8907 staticpro (&Qw32_charset_default);
8908 Qw32_charset_symbol = intern ("w32-charset-symbol");
8909 staticpro (&Qw32_charset_shiftjis);
8910 Qw32_charset_shiftjis = intern ("w32-charset-shiftjis");
8911 staticpro (&Qw32_charset_hangeul);
8912 Qw32_charset_hangeul = intern ("w32-charset-hangeul");
8913 staticpro (&Qw32_charset_chinesebig5);
8914 Qw32_charset_chinesebig5 = intern ("w32-charset-chinesebig5");
8915 staticpro (&Qw32_charset_gb2312);
8916 Qw32_charset_gb2312 = intern ("w32-charset-gb2312");
8917 staticpro (&Qw32_charset_oem);
8918 Qw32_charset_oem = intern ("w32-charset-oem");
8919
8920 #ifdef JOHAB_CHARSET
8921 {
8922 static int w32_extra_charsets_defined = 1;
8923 DEFVAR_BOOL ("w32-extra-charsets-defined", &w32_extra_charsets_defined,
8924 doc: /* Internal variable. */);
8925
8926 staticpro (&Qw32_charset_johab);
8927 Qw32_charset_johab = intern ("w32-charset-johab");
8928 staticpro (&Qw32_charset_easteurope);
8929 Qw32_charset_easteurope = intern ("w32-charset-easteurope");
8930 staticpro (&Qw32_charset_turkish);
8931 Qw32_charset_turkish = intern ("w32-charset-turkish");
8932 staticpro (&Qw32_charset_baltic);
8933 Qw32_charset_baltic = intern ("w32-charset-baltic");
8934 staticpro (&Qw32_charset_russian);
8935 Qw32_charset_russian = intern ("w32-charset-russian");
8936 staticpro (&Qw32_charset_arabic);
8937 Qw32_charset_arabic = intern ("w32-charset-arabic");
8938 staticpro (&Qw32_charset_greek);
8939 Qw32_charset_greek = intern ("w32-charset-greek");
8940 staticpro (&Qw32_charset_hebrew);
8941 Qw32_charset_hebrew = intern ("w32-charset-hebrew");
8942 staticpro (&Qw32_charset_vietnamese);
8943 Qw32_charset_vietnamese = intern ("w32-charset-vietnamese");
8944 staticpro (&Qw32_charset_thai);
8945 Qw32_charset_thai = intern ("w32-charset-thai");
8946 staticpro (&Qw32_charset_mac);
8947 Qw32_charset_mac = intern ("w32-charset-mac");
8948 }
8949 #endif
8950
8951 #ifdef UNICODE_CHARSET
8952 {
8953 static int w32_unicode_charset_defined = 1;
8954 DEFVAR_BOOL ("w32-unicode-charset-defined",
8955 &w32_unicode_charset_defined,
8956 doc: /* Internal variable. */);
8957
8958 staticpro (&Qw32_charset_unicode);
8959 Qw32_charset_unicode = intern ("w32-charset-unicode");
8960 #endif
8961
8962 #if 0 /* TODO: Port to W32 */
8963 defsubr (&Sx_change_window_property);
8964 defsubr (&Sx_delete_window_property);
8965 defsubr (&Sx_window_property);
8966 #endif
8967 defsubr (&Sxw_display_color_p);
8968 defsubr (&Sx_display_grayscale_p);
8969 defsubr (&Sxw_color_defined_p);
8970 defsubr (&Sxw_color_values);
8971 defsubr (&Sx_server_max_request_size);
8972 defsubr (&Sx_server_vendor);
8973 defsubr (&Sx_server_version);
8974 defsubr (&Sx_display_pixel_width);
8975 defsubr (&Sx_display_pixel_height);
8976 defsubr (&Sx_display_mm_width);
8977 defsubr (&Sx_display_mm_height);
8978 defsubr (&Sx_display_screens);
8979 defsubr (&Sx_display_planes);
8980 defsubr (&Sx_display_color_cells);
8981 defsubr (&Sx_display_visual_class);
8982 defsubr (&Sx_display_backing_store);
8983 defsubr (&Sx_display_save_under);
8984 defsubr (&Sx_create_frame);
8985 defsubr (&Sx_open_connection);
8986 defsubr (&Sx_close_connection);
8987 defsubr (&Sx_display_list);
8988 defsubr (&Sx_synchronize);
8989
8990 /* W32 specific functions */
8991
8992 defsubr (&Sw32_focus_frame);
8993 defsubr (&Sw32_select_font);
8994 defsubr (&Sw32_define_rgb_color);
8995 defsubr (&Sw32_default_color_map);
8996 defsubr (&Sw32_load_color_file);
8997 defsubr (&Sw32_send_sys_command);
8998 defsubr (&Sw32_shell_execute);
8999 defsubr (&Sw32_register_hot_key);
9000 defsubr (&Sw32_unregister_hot_key);
9001 defsubr (&Sw32_registered_hot_keys);
9002 defsubr (&Sw32_reconstruct_hot_key);
9003 defsubr (&Sw32_toggle_lock_key);
9004 defsubr (&Sw32_find_bdf_fonts);
9005
9006 defsubr (&Sfile_system_info);
9007 defsubr (&Sdefault_printer_name);
9008
9009 /* Setting callback functions for fontset handler. */
9010 get_font_info_func = w32_get_font_info;
9011
9012 #if 0 /* This function pointer doesn't seem to be used anywhere.
9013 And the pointer assigned has the wrong type, anyway. */
9014 list_fonts_func = w32_list_fonts;
9015 #endif
9016
9017 load_font_func = w32_load_font;
9018 find_ccl_program_func = w32_find_ccl_program;
9019 query_font_func = w32_query_font;
9020 set_frame_fontset_func = x_set_font;
9021 get_font_repertory_func = x_get_font_repertory;
9022 check_window_system_func = check_w32;
9023
9024
9025 hourglass_atimer = NULL;
9026 hourglass_shown_p = 0;
9027 defsubr (&Sx_show_tip);
9028 defsubr (&Sx_hide_tip);
9029 tip_timer = Qnil;
9030 staticpro (&tip_timer);
9031 tip_frame = Qnil;
9032 staticpro (&tip_frame);
9033
9034 last_show_tip_args = Qnil;
9035 staticpro (&last_show_tip_args);
9036
9037 defsubr (&Sx_file_dialog);
9038 }
9039
9040
9041 /*
9042 globals_of_w32fns is used to initialize those global variables that
9043 must always be initialized on startup even when the global variable
9044 initialized is non zero (see the function main in emacs.c).
9045 globals_of_w32fns is called from syms_of_w32fns when the global
9046 variable initialized is 0 and directly from main when initialized
9047 is non zero.
9048 */
9049 void globals_of_w32fns ()
9050 {
9051 HMODULE user32_lib = GetModuleHandle ("user32.dll");
9052 /*
9053 TrackMouseEvent not available in all versions of Windows, so must load
9054 it dynamically. Do it once, here, instead of every time it is used.
9055 */
9056 track_mouse_event_fn = (TrackMouseEvent_Proc)
9057 GetProcAddress (user32_lib, "TrackMouseEvent");
9058 /* ditto for GetClipboardSequenceNumber. */
9059 clipboard_sequence_fn = (ClipboardSequence_Proc)
9060 GetProcAddress (user32_lib, "GetClipboardSequenceNumber");
9061
9062 DEFVAR_INT ("w32-ansi-code-page",
9063 &w32_ansi_code_page,
9064 doc: /* The ANSI code page used by the system. */);
9065 w32_ansi_code_page = GetACP ();
9066 }
9067
9068 #undef abort
9069
9070 void w32_abort (void) NO_RETURN;
9071
9072 void
9073 w32_abort()
9074 {
9075 int button;
9076 button = MessageBox (NULL,
9077 "A fatal error has occurred!\n\n"
9078 "Would you like to attach a debugger?\n\n"
9079 "Select YES to debug, NO to abort Emacs"
9080 #if __GNUC__
9081 "\n\n(type \"gdb -p <emacs-PID>\" and\n"
9082 "\"continue\" inside GDB before clicking YES.)"
9083 #endif
9084 , "Emacs Abort Dialog",
9085 MB_ICONEXCLAMATION | MB_TASKMODAL
9086 | MB_SETFOREGROUND | MB_YESNO);
9087 switch (button)
9088 {
9089 case IDYES:
9090 DebugBreak ();
9091 exit (2); /* tell the compiler we will never return */
9092 case IDNO:
9093 default:
9094 abort ();
9095 break;
9096 }
9097 }
9098
9099 /* For convenience when debugging. */
9100 int
9101 w32_last_error()
9102 {
9103 return GetLastError ();
9104 }
9105
9106 /* arch-tag: 707589ab-b9be-4638-8cdd-74629cc9b446
9107 (do not change this comment) */