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