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