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