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