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