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