]> code.delx.au - gnu-emacs/blob - src/w32fns.c
(which-function): Fix documentation/comment typo.
[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 (!NILP (arg) || NILP (oldval))
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 Lisp_Object top, left;
2070
2071 rect.left = rect.top = 0;
2072 rect.right = FRAME_PIXEL_WIDTH (f);
2073 rect.bottom = FRAME_PIXEL_HEIGHT (f);
2074
2075 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
2076 FRAME_EXTERNAL_MENU_BAR (f));
2077
2078 /* Do first time app init */
2079
2080 if (!hprevinst)
2081 {
2082 w32_init_class (hinst);
2083 }
2084
2085 /* When called with RES_TYPE_NUMBER, w32_get_arg will return zero
2086 for anything that is not a number and is not Qunbound. */
2087 left = w32_get_arg (Qnil, Qleft, "left", "Left", RES_TYPE_NUMBER);
2088 top = w32_get_arg (Qnil, Qtop, "top", "Top", RES_TYPE_NUMBER);
2089
2090 FRAME_W32_WINDOW (f) = hwnd
2091 = CreateWindow (EMACS_CLASS,
2092 f->namebuf,
2093 f->output_data.w32->dwStyle | WS_CLIPCHILDREN,
2094 EQ (left, Qunbound) ? CW_USEDEFAULT : XINT (left),
2095 EQ (top, Qunbound) ? CW_USEDEFAULT : XINT (top),
2096 rect.right - rect.left,
2097 rect.bottom - rect.top,
2098 NULL,
2099 NULL,
2100 hinst,
2101 NULL);
2102
2103 if (hwnd)
2104 {
2105 SetWindowLong (hwnd, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
2106 SetWindowLong (hwnd, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
2107 SetWindowLong (hwnd, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
2108 SetWindowLong (hwnd, WND_SCROLLBAR_INDEX, f->scroll_bar_actual_width);
2109 SetWindowLong (hwnd, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
2110
2111 /* Enable drag-n-drop. */
2112 DragAcceptFiles (hwnd, TRUE);
2113
2114 /* Do this to discard the default setting specified by our parent. */
2115 ShowWindow (hwnd, SW_HIDE);
2116
2117 /* Update frame positions. */
2118 GetWindowRect (hwnd, &rect);
2119 f->left_pos = rect.left;
2120 f->top_pos = rect.top;
2121 }
2122 }
2123
2124 void
2125 my_post_msg (wmsg, hwnd, msg, wParam, lParam)
2126 W32Msg * wmsg;
2127 HWND hwnd;
2128 UINT msg;
2129 WPARAM wParam;
2130 LPARAM lParam;
2131 {
2132 wmsg->msg.hwnd = hwnd;
2133 wmsg->msg.message = msg;
2134 wmsg->msg.wParam = wParam;
2135 wmsg->msg.lParam = lParam;
2136 wmsg->msg.time = GetMessageTime ();
2137
2138 post_msg (wmsg);
2139 }
2140
2141 /* GetKeyState and MapVirtualKey on Windows 95 do not actually distinguish
2142 between left and right keys as advertised. We test for this
2143 support dynamically, and set a flag when the support is absent. If
2144 absent, we keep track of the left and right control and alt keys
2145 ourselves. This is particularly necessary on keyboards that rely
2146 upon the AltGr key, which is represented as having the left control
2147 and right alt keys pressed. For these keyboards, we need to know
2148 when the left alt key has been pressed in addition to the AltGr key
2149 so that we can properly support M-AltGr-key sequences (such as M-@
2150 on Swedish keyboards). */
2151
2152 #define EMACS_LCONTROL 0
2153 #define EMACS_RCONTROL 1
2154 #define EMACS_LMENU 2
2155 #define EMACS_RMENU 3
2156
2157 static int modifiers[4];
2158 static int modifiers_recorded;
2159 static int modifier_key_support_tested;
2160
2161 static void
2162 test_modifier_support (unsigned int wparam)
2163 {
2164 unsigned int l, r;
2165
2166 if (wparam != VK_CONTROL && wparam != VK_MENU)
2167 return;
2168 if (wparam == VK_CONTROL)
2169 {
2170 l = VK_LCONTROL;
2171 r = VK_RCONTROL;
2172 }
2173 else
2174 {
2175 l = VK_LMENU;
2176 r = VK_RMENU;
2177 }
2178 if (!(GetKeyState (l) & 0x8000) && !(GetKeyState (r) & 0x8000))
2179 modifiers_recorded = 1;
2180 else
2181 modifiers_recorded = 0;
2182 modifier_key_support_tested = 1;
2183 }
2184
2185 static void
2186 record_keydown (unsigned int wparam, unsigned int lparam)
2187 {
2188 int i;
2189
2190 if (!modifier_key_support_tested)
2191 test_modifier_support (wparam);
2192
2193 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
2194 return;
2195
2196 if (wparam == VK_CONTROL)
2197 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
2198 else
2199 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
2200
2201 modifiers[i] = 1;
2202 }
2203
2204 static void
2205 record_keyup (unsigned int wparam, unsigned int lparam)
2206 {
2207 int i;
2208
2209 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
2210 return;
2211
2212 if (wparam == VK_CONTROL)
2213 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
2214 else
2215 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
2216
2217 modifiers[i] = 0;
2218 }
2219
2220 /* Emacs can lose focus while a modifier key has been pressed. When
2221 it regains focus, be conservative and clear all modifiers since
2222 we cannot reconstruct the left and right modifier state. */
2223 static void
2224 reset_modifiers ()
2225 {
2226 SHORT ctrl, alt;
2227
2228 if (GetFocus () == NULL)
2229 /* Emacs doesn't have keyboard focus. Do nothing. */
2230 return;
2231
2232 ctrl = GetAsyncKeyState (VK_CONTROL);
2233 alt = GetAsyncKeyState (VK_MENU);
2234
2235 if (!(ctrl & 0x08000))
2236 /* Clear any recorded control modifier state. */
2237 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
2238
2239 if (!(alt & 0x08000))
2240 /* Clear any recorded alt modifier state. */
2241 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
2242
2243 /* Update the state of all modifier keys, because modifiers used in
2244 hot-key combinations can get stuck on if Emacs loses focus as a
2245 result of a hot-key being pressed. */
2246 {
2247 BYTE keystate[256];
2248
2249 #define CURRENT_STATE(key) ((GetAsyncKeyState (key) & 0x8000) >> 8)
2250
2251 GetKeyboardState (keystate);
2252 keystate[VK_SHIFT] = CURRENT_STATE (VK_SHIFT);
2253 keystate[VK_CONTROL] = CURRENT_STATE (VK_CONTROL);
2254 keystate[VK_LCONTROL] = CURRENT_STATE (VK_LCONTROL);
2255 keystate[VK_RCONTROL] = CURRENT_STATE (VK_RCONTROL);
2256 keystate[VK_MENU] = CURRENT_STATE (VK_MENU);
2257 keystate[VK_LMENU] = CURRENT_STATE (VK_LMENU);
2258 keystate[VK_RMENU] = CURRENT_STATE (VK_RMENU);
2259 keystate[VK_LWIN] = CURRENT_STATE (VK_LWIN);
2260 keystate[VK_RWIN] = CURRENT_STATE (VK_RWIN);
2261 keystate[VK_APPS] = CURRENT_STATE (VK_APPS);
2262 SetKeyboardState (keystate);
2263 }
2264 }
2265
2266 /* Synchronize modifier state with what is reported with the current
2267 keystroke. Even if we cannot distinguish between left and right
2268 modifier keys, we know that, if no modifiers are set, then neither
2269 the left or right modifier should be set. */
2270 static void
2271 sync_modifiers ()
2272 {
2273 if (!modifiers_recorded)
2274 return;
2275
2276 if (!(GetKeyState (VK_CONTROL) & 0x8000))
2277 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
2278
2279 if (!(GetKeyState (VK_MENU) & 0x8000))
2280 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
2281 }
2282
2283 static int
2284 modifier_set (int vkey)
2285 {
2286 if (vkey == VK_CAPITAL || vkey == VK_SCROLL)
2287 return (GetKeyState (vkey) & 0x1);
2288 if (!modifiers_recorded)
2289 return (GetKeyState (vkey) & 0x8000);
2290
2291 switch (vkey)
2292 {
2293 case VK_LCONTROL:
2294 return modifiers[EMACS_LCONTROL];
2295 case VK_RCONTROL:
2296 return modifiers[EMACS_RCONTROL];
2297 case VK_LMENU:
2298 return modifiers[EMACS_LMENU];
2299 case VK_RMENU:
2300 return modifiers[EMACS_RMENU];
2301 }
2302 return (GetKeyState (vkey) & 0x8000);
2303 }
2304
2305 /* Convert between the modifier bits W32 uses and the modifier bits
2306 Emacs uses. */
2307
2308 unsigned int
2309 w32_key_to_modifier (int key)
2310 {
2311 Lisp_Object key_mapping;
2312
2313 switch (key)
2314 {
2315 case VK_LWIN:
2316 key_mapping = Vw32_lwindow_modifier;
2317 break;
2318 case VK_RWIN:
2319 key_mapping = Vw32_rwindow_modifier;
2320 break;
2321 case VK_APPS:
2322 key_mapping = Vw32_apps_modifier;
2323 break;
2324 case VK_SCROLL:
2325 key_mapping = Vw32_scroll_lock_modifier;
2326 break;
2327 default:
2328 key_mapping = Qnil;
2329 }
2330
2331 /* NB. This code runs in the input thread, asychronously to the lisp
2332 thread, so we must be careful to ensure access to lisp data is
2333 thread-safe. The following code is safe because the modifier
2334 variable values are updated atomically from lisp and symbols are
2335 not relocated by GC. Also, we don't have to worry about seeing GC
2336 markbits here. */
2337 if (EQ (key_mapping, Qhyper))
2338 return hyper_modifier;
2339 if (EQ (key_mapping, Qsuper))
2340 return super_modifier;
2341 if (EQ (key_mapping, Qmeta))
2342 return meta_modifier;
2343 if (EQ (key_mapping, Qalt))
2344 return alt_modifier;
2345 if (EQ (key_mapping, Qctrl))
2346 return ctrl_modifier;
2347 if (EQ (key_mapping, Qcontrol)) /* synonym for ctrl */
2348 return ctrl_modifier;
2349 if (EQ (key_mapping, Qshift))
2350 return shift_modifier;
2351
2352 /* Don't generate any modifier if not explicitly requested. */
2353 return 0;
2354 }
2355
2356 unsigned int
2357 w32_get_modifiers ()
2358 {
2359 return ((modifier_set (VK_SHIFT) ? shift_modifier : 0) |
2360 (modifier_set (VK_CONTROL) ? ctrl_modifier : 0) |
2361 (modifier_set (VK_LWIN) ? w32_key_to_modifier (VK_LWIN) : 0) |
2362 (modifier_set (VK_RWIN) ? w32_key_to_modifier (VK_RWIN) : 0) |
2363 (modifier_set (VK_APPS) ? w32_key_to_modifier (VK_APPS) : 0) |
2364 (modifier_set (VK_SCROLL) ? w32_key_to_modifier (VK_SCROLL) : 0) |
2365 (modifier_set (VK_MENU) ?
2366 ((NILP (Vw32_alt_is_meta)) ? alt_modifier : meta_modifier) : 0));
2367 }
2368
2369 /* We map the VK_* modifiers into console modifier constants
2370 so that we can use the same routines to handle both console
2371 and window input. */
2372
2373 static int
2374 construct_console_modifiers ()
2375 {
2376 int mods;
2377
2378 mods = 0;
2379 mods |= (modifier_set (VK_SHIFT)) ? SHIFT_PRESSED : 0;
2380 mods |= (modifier_set (VK_CAPITAL)) ? CAPSLOCK_ON : 0;
2381 mods |= (modifier_set (VK_SCROLL)) ? SCROLLLOCK_ON : 0;
2382 mods |= (modifier_set (VK_NUMLOCK)) ? NUMLOCK_ON : 0;
2383 mods |= (modifier_set (VK_LCONTROL)) ? LEFT_CTRL_PRESSED : 0;
2384 mods |= (modifier_set (VK_RCONTROL)) ? RIGHT_CTRL_PRESSED : 0;
2385 mods |= (modifier_set (VK_LMENU)) ? LEFT_ALT_PRESSED : 0;
2386 mods |= (modifier_set (VK_RMENU)) ? RIGHT_ALT_PRESSED : 0;
2387 mods |= (modifier_set (VK_LWIN)) ? LEFT_WIN_PRESSED : 0;
2388 mods |= (modifier_set (VK_RWIN)) ? RIGHT_WIN_PRESSED : 0;
2389 mods |= (modifier_set (VK_APPS)) ? APPS_PRESSED : 0;
2390
2391 return mods;
2392 }
2393
2394 static int
2395 w32_get_key_modifiers (unsigned int wparam, unsigned int lparam)
2396 {
2397 int mods;
2398
2399 /* Convert to emacs modifiers. */
2400 mods = w32_kbd_mods_to_emacs (construct_console_modifiers (), wparam);
2401
2402 return mods;
2403 }
2404
2405 unsigned int
2406 map_keypad_keys (unsigned int virt_key, unsigned int extended)
2407 {
2408 if (virt_key < VK_CLEAR || virt_key > VK_DELETE)
2409 return virt_key;
2410
2411 if (virt_key == VK_RETURN)
2412 return (extended ? VK_NUMPAD_ENTER : VK_RETURN);
2413
2414 if (virt_key >= VK_PRIOR && virt_key <= VK_DOWN)
2415 return (!extended ? (VK_NUMPAD_PRIOR + (virt_key - VK_PRIOR)) : virt_key);
2416
2417 if (virt_key == VK_INSERT || virt_key == VK_DELETE)
2418 return (!extended ? (VK_NUMPAD_INSERT + (virt_key - VK_INSERT)) : virt_key);
2419
2420 if (virt_key == VK_CLEAR)
2421 return (!extended ? VK_NUMPAD_CLEAR : virt_key);
2422
2423 return virt_key;
2424 }
2425
2426 /* List of special key combinations which w32 would normally capture,
2427 but emacs should grab instead. Not directly visible to lisp, to
2428 simplify synchronization. Each item is an integer encoding a virtual
2429 key code and modifier combination to capture. */
2430 Lisp_Object w32_grabbed_keys;
2431
2432 #define HOTKEY(vk,mods) make_number (((vk) & 255) | ((mods) << 8))
2433 #define HOTKEY_ID(k) (XFASTINT (k) & 0xbfff)
2434 #define HOTKEY_VK_CODE(k) (XFASTINT (k) & 255)
2435 #define HOTKEY_MODIFIERS(k) (XFASTINT (k) >> 8)
2436
2437 #define RAW_HOTKEY_ID(k) ((k) & 0xbfff)
2438 #define RAW_HOTKEY_VK_CODE(k) ((k) & 255)
2439 #define RAW_HOTKEY_MODIFIERS(k) ((k) >> 8)
2440
2441 /* Register hot-keys for reserved key combinations when Emacs has
2442 keyboard focus, since this is the only way Emacs can receive key
2443 combinations like Alt-Tab which are used by the system. */
2444
2445 static void
2446 register_hot_keys (hwnd)
2447 HWND hwnd;
2448 {
2449 Lisp_Object keylist;
2450
2451 /* Use GC_CONSP, since we are called asynchronously. */
2452 for (keylist = w32_grabbed_keys; GC_CONSP (keylist); keylist = XCDR (keylist))
2453 {
2454 Lisp_Object key = XCAR (keylist);
2455
2456 /* Deleted entries get set to nil. */
2457 if (!INTEGERP (key))
2458 continue;
2459
2460 RegisterHotKey (hwnd, HOTKEY_ID (key),
2461 HOTKEY_MODIFIERS (key), HOTKEY_VK_CODE (key));
2462 }
2463 }
2464
2465 static void
2466 unregister_hot_keys (hwnd)
2467 HWND hwnd;
2468 {
2469 Lisp_Object keylist;
2470
2471 /* Use GC_CONSP, since we are called asynchronously. */
2472 for (keylist = w32_grabbed_keys; GC_CONSP (keylist); keylist = XCDR (keylist))
2473 {
2474 Lisp_Object key = XCAR (keylist);
2475
2476 if (!INTEGERP (key))
2477 continue;
2478
2479 UnregisterHotKey (hwnd, HOTKEY_ID (key));
2480 }
2481 }
2482
2483 /* Main message dispatch loop. */
2484
2485 static void
2486 w32_msg_pump (deferred_msg * msg_buf)
2487 {
2488 MSG msg;
2489 int result;
2490 HWND focus_window;
2491
2492 msh_mousewheel = RegisterWindowMessage (MSH_MOUSEWHEEL);
2493
2494 while (GetMessage (&msg, NULL, 0, 0))
2495 {
2496 if (msg.hwnd == NULL)
2497 {
2498 switch (msg.message)
2499 {
2500 case WM_NULL:
2501 /* Produced by complete_deferred_msg; just ignore. */
2502 break;
2503 case WM_EMACS_CREATEWINDOW:
2504 w32_createwindow ((struct frame *) msg.wParam);
2505 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2506 abort ();
2507 break;
2508 case WM_EMACS_SETLOCALE:
2509 SetThreadLocale (msg.wParam);
2510 /* Reply is not expected. */
2511 break;
2512 case WM_EMACS_SETKEYBOARDLAYOUT:
2513 result = (int) ActivateKeyboardLayout ((HKL) msg.wParam, 0);
2514 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
2515 result, 0))
2516 abort ();
2517 break;
2518 case WM_EMACS_REGISTER_HOT_KEY:
2519 focus_window = GetFocus ();
2520 if (focus_window != NULL)
2521 RegisterHotKey (focus_window,
2522 RAW_HOTKEY_ID (msg.wParam),
2523 RAW_HOTKEY_MODIFIERS (msg.wParam),
2524 RAW_HOTKEY_VK_CODE (msg.wParam));
2525 /* Reply is not expected. */
2526 break;
2527 case WM_EMACS_UNREGISTER_HOT_KEY:
2528 focus_window = GetFocus ();
2529 if (focus_window != NULL)
2530 UnregisterHotKey (focus_window, RAW_HOTKEY_ID (msg.wParam));
2531 /* Mark item as erased. NB: this code must be
2532 thread-safe. The next line is okay because the cons
2533 cell is never made into garbage and is not relocated by
2534 GC. */
2535 XSETCAR ((Lisp_Object) ((EMACS_INT) msg.lParam), Qnil);
2536 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2537 abort ();
2538 break;
2539 case WM_EMACS_TOGGLE_LOCK_KEY:
2540 {
2541 int vk_code = (int) msg.wParam;
2542 int cur_state = (GetKeyState (vk_code) & 1);
2543 Lisp_Object new_state = (Lisp_Object) ((EMACS_INT) msg.lParam);
2544
2545 /* NB: This code must be thread-safe. It is safe to
2546 call NILP because symbols are not relocated by GC,
2547 and pointer here is not touched by GC (so the markbit
2548 can't be set). Numbers are safe because they are
2549 immediate values. */
2550 if (NILP (new_state)
2551 || (NUMBERP (new_state)
2552 && ((XUINT (new_state)) & 1) != cur_state))
2553 {
2554 one_w32_display_info.faked_key = vk_code;
2555
2556 keybd_event ((BYTE) vk_code,
2557 (BYTE) MapVirtualKey (vk_code, 0),
2558 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2559 keybd_event ((BYTE) vk_code,
2560 (BYTE) MapVirtualKey (vk_code, 0),
2561 KEYEVENTF_EXTENDEDKEY | 0, 0);
2562 keybd_event ((BYTE) vk_code,
2563 (BYTE) MapVirtualKey (vk_code, 0),
2564 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2565 cur_state = !cur_state;
2566 }
2567 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
2568 cur_state, 0))
2569 abort ();
2570 }
2571 break;
2572 default:
2573 DebPrint (("msg %x not expected by w32_msg_pump\n", msg.message));
2574 }
2575 }
2576 else
2577 {
2578 DispatchMessage (&msg);
2579 }
2580
2581 /* Exit nested loop when our deferred message has completed. */
2582 if (msg_buf->completed)
2583 break;
2584 }
2585 }
2586
2587 deferred_msg * deferred_msg_head;
2588
2589 static deferred_msg *
2590 find_deferred_msg (HWND hwnd, UINT msg)
2591 {
2592 deferred_msg * item;
2593
2594 /* Don't actually need synchronization for read access, since
2595 modification of single pointer is always atomic. */
2596 /* enter_crit (); */
2597
2598 for (item = deferred_msg_head; item != NULL; item = item->next)
2599 if (item->w32msg.msg.hwnd == hwnd
2600 && item->w32msg.msg.message == msg)
2601 break;
2602
2603 /* leave_crit (); */
2604
2605 return item;
2606 }
2607
2608 static LRESULT
2609 send_deferred_msg (deferred_msg * msg_buf,
2610 HWND hwnd,
2611 UINT msg,
2612 WPARAM wParam,
2613 LPARAM lParam)
2614 {
2615 /* Only input thread can send deferred messages. */
2616 if (GetCurrentThreadId () != dwWindowsThreadId)
2617 abort ();
2618
2619 /* It is an error to send a message that is already deferred. */
2620 if (find_deferred_msg (hwnd, msg) != NULL)
2621 abort ();
2622
2623 /* Enforced synchronization is not needed because this is the only
2624 function that alters deferred_msg_head, and the following critical
2625 section is guaranteed to only be serially reentered (since only the
2626 input thread can call us). */
2627
2628 /* enter_crit (); */
2629
2630 msg_buf->completed = 0;
2631 msg_buf->next = deferred_msg_head;
2632 deferred_msg_head = msg_buf;
2633 my_post_msg (&msg_buf->w32msg, hwnd, msg, wParam, lParam);
2634
2635 /* leave_crit (); */
2636
2637 /* Start a new nested message loop to process other messages until
2638 this one is completed. */
2639 w32_msg_pump (msg_buf);
2640
2641 deferred_msg_head = msg_buf->next;
2642
2643 return msg_buf->result;
2644 }
2645
2646 void
2647 complete_deferred_msg (HWND hwnd, UINT msg, LRESULT result)
2648 {
2649 deferred_msg * msg_buf = find_deferred_msg (hwnd, msg);
2650
2651 if (msg_buf == NULL)
2652 /* Message may have been cancelled, so don't abort(). */
2653 return;
2654
2655 msg_buf->result = result;
2656 msg_buf->completed = 1;
2657
2658 /* Ensure input thread is woken so it notices the completion. */
2659 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
2660 }
2661
2662 void
2663 cancel_all_deferred_msgs ()
2664 {
2665 deferred_msg * item;
2666
2667 /* Don't actually need synchronization for read access, since
2668 modification of single pointer is always atomic. */
2669 /* enter_crit (); */
2670
2671 for (item = deferred_msg_head; item != NULL; item = item->next)
2672 {
2673 item->result = 0;
2674 item->completed = 1;
2675 }
2676
2677 /* leave_crit (); */
2678
2679 /* Ensure input thread is woken so it notices the completion. */
2680 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
2681 }
2682
2683 DWORD
2684 w32_msg_worker (dw)
2685 DWORD dw;
2686 {
2687 MSG msg;
2688 deferred_msg dummy_buf;
2689
2690 /* Ensure our message queue is created */
2691
2692 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
2693
2694 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2695 abort ();
2696
2697 memset (&dummy_buf, 0, sizeof (dummy_buf));
2698 dummy_buf.w32msg.msg.hwnd = NULL;
2699 dummy_buf.w32msg.msg.message = WM_NULL;
2700
2701 /* This is the inital message loop which should only exit when the
2702 application quits. */
2703 w32_msg_pump (&dummy_buf);
2704
2705 return 0;
2706 }
2707
2708 static void
2709 signal_user_input ()
2710 {
2711 /* Interrupt any lisp that wants to be interrupted by input. */
2712 if (!NILP (Vthrow_on_input))
2713 {
2714 Vquit_flag = Vthrow_on_input;
2715 /* If we're inside a function that wants immediate quits,
2716 do it now. */
2717 if (immediate_quit && NILP (Vinhibit_quit))
2718 {
2719 immediate_quit = 0;
2720 QUIT;
2721 }
2722 }
2723 }
2724
2725
2726 static void
2727 post_character_message (hwnd, msg, wParam, lParam, modifiers)
2728 HWND hwnd;
2729 UINT msg;
2730 WPARAM wParam;
2731 LPARAM lParam;
2732 DWORD modifiers;
2733
2734 {
2735 W32Msg wmsg;
2736
2737 wmsg.dwModifiers = modifiers;
2738
2739 /* Detect quit_char and set quit-flag directly. Note that we
2740 still need to post a message to ensure the main thread will be
2741 woken up if blocked in sys_select(), but we do NOT want to post
2742 the quit_char message itself (because it will usually be as if
2743 the user had typed quit_char twice). Instead, we post a dummy
2744 message that has no particular effect. */
2745 {
2746 int c = wParam;
2747 if (isalpha (c) && wmsg.dwModifiers == ctrl_modifier)
2748 c = make_ctrl_char (c) & 0377;
2749 if (c == quit_char
2750 || (wmsg.dwModifiers == 0 &&
2751 w32_quit_key && wParam == w32_quit_key))
2752 {
2753 Vquit_flag = Qt;
2754
2755 /* The choice of message is somewhat arbitrary, as long as
2756 the main thread handler just ignores it. */
2757 msg = WM_NULL;
2758
2759 /* Interrupt any blocking system calls. */
2760 signal_quit ();
2761
2762 /* As a safety precaution, forcibly complete any deferred
2763 messages. This is a kludge, but I don't see any particularly
2764 clean way to handle the situation where a deferred message is
2765 "dropped" in the lisp thread, and will thus never be
2766 completed, eg. by the user trying to activate the menubar
2767 when the lisp thread is busy, and then typing C-g when the
2768 menubar doesn't open promptly (with the result that the
2769 menubar never responds at all because the deferred
2770 WM_INITMENU message is never completed). Another problem
2771 situation is when the lisp thread calls SendMessage (to send
2772 a window manager command) when a message has been deferred;
2773 the lisp thread gets blocked indefinitely waiting for the
2774 deferred message to be completed, which itself is waiting for
2775 the lisp thread to respond.
2776
2777 Note that we don't want to block the input thread waiting for
2778 a reponse from the lisp thread (although that would at least
2779 solve the deadlock problem above), because we want to be able
2780 to receive C-g to interrupt the lisp thread. */
2781 cancel_all_deferred_msgs ();
2782 }
2783 else
2784 signal_user_input ();
2785 }
2786
2787 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2788 }
2789
2790 /* Main window procedure */
2791
2792 LRESULT CALLBACK
2793 w32_wnd_proc (hwnd, msg, wParam, lParam)
2794 HWND hwnd;
2795 UINT msg;
2796 WPARAM wParam;
2797 LPARAM lParam;
2798 {
2799 struct frame *f;
2800 struct w32_display_info *dpyinfo = &one_w32_display_info;
2801 W32Msg wmsg;
2802 int windows_translate;
2803 int key;
2804
2805 /* Note that it is okay to call x_window_to_frame, even though we are
2806 not running in the main lisp thread, because frame deletion
2807 requires the lisp thread to synchronize with this thread. Thus, if
2808 a frame struct is returned, it can be used without concern that the
2809 lisp thread might make it disappear while we are using it.
2810
2811 NB. Walking the frame list in this thread is safe (as long as
2812 writes of Lisp_Object slots are atomic, which they are on Windows).
2813 Although delete-frame can destructively modify the frame list while
2814 we are walking it, a garbage collection cannot occur until after
2815 delete-frame has synchronized with this thread.
2816
2817 It is also safe to use functions that make GDI calls, such as
2818 w32_clear_rect, because these functions must obtain a DC handle
2819 from the frame struct using get_frame_dc which is thread-aware. */
2820
2821 switch (msg)
2822 {
2823 case WM_ERASEBKGND:
2824 f = x_window_to_frame (dpyinfo, hwnd);
2825 if (f)
2826 {
2827 HDC hdc = get_frame_dc (f);
2828 GetUpdateRect (hwnd, &wmsg.rect, FALSE);
2829 w32_clear_rect (f, hdc, &wmsg.rect);
2830 release_frame_dc (f, hdc);
2831
2832 #if defined (W32_DEBUG_DISPLAY)
2833 DebPrint (("WM_ERASEBKGND (frame %p): erasing %d,%d-%d,%d\n",
2834 f,
2835 wmsg.rect.left, wmsg.rect.top,
2836 wmsg.rect.right, wmsg.rect.bottom));
2837 #endif /* W32_DEBUG_DISPLAY */
2838 }
2839 return 1;
2840 case WM_PALETTECHANGED:
2841 /* ignore our own changes */
2842 if ((HWND)wParam != hwnd)
2843 {
2844 f = x_window_to_frame (dpyinfo, hwnd);
2845 if (f)
2846 /* get_frame_dc will realize our palette and force all
2847 frames to be redrawn if needed. */
2848 release_frame_dc (f, get_frame_dc (f));
2849 }
2850 return 0;
2851 case WM_PAINT:
2852 {
2853 PAINTSTRUCT paintStruct;
2854 RECT update_rect;
2855 bzero (&update_rect, sizeof (update_rect));
2856
2857 f = x_window_to_frame (dpyinfo, hwnd);
2858 if (f == 0)
2859 {
2860 DebPrint (("WM_PAINT received for unknown window %p\n", hwnd));
2861 return 0;
2862 }
2863
2864 /* MSDN Docs say not to call BeginPaint if GetUpdateRect
2865 fails. Apparently this can happen under some
2866 circumstances. */
2867 if (GetUpdateRect (hwnd, &update_rect, FALSE) || !w32_strict_painting)
2868 {
2869 enter_crit ();
2870 BeginPaint (hwnd, &paintStruct);
2871
2872 /* The rectangles returned by GetUpdateRect and BeginPaint
2873 do not always match. Play it safe by assuming both areas
2874 are invalid. */
2875 UnionRect (&(wmsg.rect), &update_rect, &(paintStruct.rcPaint));
2876
2877 #if defined (W32_DEBUG_DISPLAY)
2878 DebPrint (("WM_PAINT (frame %p): painting %d,%d-%d,%d\n",
2879 f,
2880 wmsg.rect.left, wmsg.rect.top,
2881 wmsg.rect.right, wmsg.rect.bottom));
2882 DebPrint ((" [update region is %d,%d-%d,%d]\n",
2883 update_rect.left, update_rect.top,
2884 update_rect.right, update_rect.bottom));
2885 #endif
2886 EndPaint (hwnd, &paintStruct);
2887 leave_crit ();
2888
2889 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2890
2891 return 0;
2892 }
2893
2894 /* If GetUpdateRect returns 0 (meaning there is no update
2895 region), assume the whole window needs to be repainted. */
2896 GetClientRect(hwnd, &wmsg.rect);
2897 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2898 return 0;
2899 }
2900
2901 case WM_INPUTLANGCHANGE:
2902 /* Inform lisp thread of keyboard layout changes. */
2903 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2904
2905 /* Clear dead keys in the keyboard state; for simplicity only
2906 preserve modifier key states. */
2907 {
2908 int i;
2909 BYTE keystate[256];
2910
2911 GetKeyboardState (keystate);
2912 for (i = 0; i < 256; i++)
2913 if (1
2914 && i != VK_SHIFT
2915 && i != VK_LSHIFT
2916 && i != VK_RSHIFT
2917 && i != VK_CAPITAL
2918 && i != VK_NUMLOCK
2919 && i != VK_SCROLL
2920 && i != VK_CONTROL
2921 && i != VK_LCONTROL
2922 && i != VK_RCONTROL
2923 && i != VK_MENU
2924 && i != VK_LMENU
2925 && i != VK_RMENU
2926 && i != VK_LWIN
2927 && i != VK_RWIN)
2928 keystate[i] = 0;
2929 SetKeyboardState (keystate);
2930 }
2931 goto dflt;
2932
2933 case WM_HOTKEY:
2934 /* Synchronize hot keys with normal input. */
2935 PostMessage (hwnd, WM_KEYDOWN, HIWORD (lParam), 0);
2936 return (0);
2937
2938 case WM_KEYUP:
2939 case WM_SYSKEYUP:
2940 record_keyup (wParam, lParam);
2941 goto dflt;
2942
2943 case WM_KEYDOWN:
2944 case WM_SYSKEYDOWN:
2945 /* Ignore keystrokes we fake ourself; see below. */
2946 if (dpyinfo->faked_key == wParam)
2947 {
2948 dpyinfo->faked_key = 0;
2949 /* Make sure TranslateMessage sees them though (as long as
2950 they don't produce WM_CHAR messages). This ensures that
2951 indicator lights are toggled promptly on Windows 9x, for
2952 example. */
2953 if (lispy_function_keys[wParam] != 0)
2954 {
2955 windows_translate = 1;
2956 goto translate;
2957 }
2958 return 0;
2959 }
2960
2961 /* Synchronize modifiers with current keystroke. */
2962 sync_modifiers ();
2963 record_keydown (wParam, lParam);
2964 wParam = map_keypad_keys (wParam, (lParam & 0x1000000L) != 0);
2965
2966 windows_translate = 0;
2967
2968 switch (wParam)
2969 {
2970 case VK_LWIN:
2971 if (NILP (Vw32_pass_lwindow_to_system))
2972 {
2973 /* Prevent system from acting on keyup (which opens the
2974 Start menu if no other key was pressed) by simulating a
2975 press of Space which we will ignore. */
2976 if (GetAsyncKeyState (wParam) & 1)
2977 {
2978 if (NUMBERP (Vw32_phantom_key_code))
2979 key = XUINT (Vw32_phantom_key_code) & 255;
2980 else
2981 key = VK_SPACE;
2982 dpyinfo->faked_key = key;
2983 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
2984 }
2985 }
2986 if (!NILP (Vw32_lwindow_modifier))
2987 return 0;
2988 break;
2989 case VK_RWIN:
2990 if (NILP (Vw32_pass_rwindow_to_system))
2991 {
2992 if (GetAsyncKeyState (wParam) & 1)
2993 {
2994 if (NUMBERP (Vw32_phantom_key_code))
2995 key = XUINT (Vw32_phantom_key_code) & 255;
2996 else
2997 key = VK_SPACE;
2998 dpyinfo->faked_key = key;
2999 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
3000 }
3001 }
3002 if (!NILP (Vw32_rwindow_modifier))
3003 return 0;
3004 break;
3005 case VK_APPS:
3006 if (!NILP (Vw32_apps_modifier))
3007 return 0;
3008 break;
3009 case VK_MENU:
3010 if (NILP (Vw32_pass_alt_to_system))
3011 /* Prevent DefWindowProc from activating the menu bar if an
3012 Alt key is pressed and released by itself. */
3013 return 0;
3014 windows_translate = 1;
3015 break;
3016 case VK_CAPITAL:
3017 /* Decide whether to treat as modifier or function key. */
3018 if (NILP (Vw32_enable_caps_lock))
3019 goto disable_lock_key;
3020 windows_translate = 1;
3021 break;
3022 case VK_NUMLOCK:
3023 /* Decide whether to treat as modifier or function key. */
3024 if (NILP (Vw32_enable_num_lock))
3025 goto disable_lock_key;
3026 windows_translate = 1;
3027 break;
3028 case VK_SCROLL:
3029 /* Decide whether to treat as modifier or function key. */
3030 if (NILP (Vw32_scroll_lock_modifier))
3031 goto disable_lock_key;
3032 windows_translate = 1;
3033 break;
3034 disable_lock_key:
3035 /* Ensure the appropriate lock key state (and indicator light)
3036 remains in the same state. We do this by faking another
3037 press of the relevant key. Apparently, this really is the
3038 only way to toggle the state of the indicator lights. */
3039 dpyinfo->faked_key = wParam;
3040 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
3041 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
3042 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
3043 KEYEVENTF_EXTENDEDKEY | 0, 0);
3044 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
3045 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
3046 /* Ensure indicator lights are updated promptly on Windows 9x
3047 (TranslateMessage apparently does this), after forwarding
3048 input event. */
3049 post_character_message (hwnd, msg, wParam, lParam,
3050 w32_get_key_modifiers (wParam, lParam));
3051 windows_translate = 1;
3052 break;
3053 case VK_CONTROL:
3054 case VK_SHIFT:
3055 case VK_PROCESSKEY: /* Generated by IME. */
3056 windows_translate = 1;
3057 break;
3058 case VK_CANCEL:
3059 /* Windows maps Ctrl-Pause (aka Ctrl-Break) into VK_CANCEL,
3060 which is confusing for purposes of key binding; convert
3061 VK_CANCEL events into VK_PAUSE events. */
3062 wParam = VK_PAUSE;
3063 break;
3064 case VK_PAUSE:
3065 /* Windows maps Ctrl-NumLock into VK_PAUSE, which is confusing
3066 for purposes of key binding; convert these back into
3067 VK_NUMLOCK events, at least when we want to see NumLock key
3068 presses. (Note that there is never any possibility that
3069 VK_PAUSE with Ctrl really is C-Pause as per above.) */
3070 if (NILP (Vw32_enable_num_lock) && modifier_set (VK_CONTROL))
3071 wParam = VK_NUMLOCK;
3072 break;
3073 default:
3074 /* If not defined as a function key, change it to a WM_CHAR message. */
3075 if (lispy_function_keys[wParam] == 0)
3076 {
3077 DWORD modifiers = construct_console_modifiers ();
3078
3079 if (!NILP (Vw32_recognize_altgr)
3080 && modifier_set (VK_LCONTROL) && modifier_set (VK_RMENU))
3081 {
3082 /* Always let TranslateMessage handle AltGr key chords;
3083 for some reason, ToAscii doesn't always process AltGr
3084 chords correctly. */
3085 windows_translate = 1;
3086 }
3087 else if ((modifiers & (~SHIFT_PRESSED & ~CAPSLOCK_ON)) != 0)
3088 {
3089 /* Handle key chords including any modifiers other
3090 than shift directly, in order to preserve as much
3091 modifier information as possible. */
3092 if ('A' <= wParam && wParam <= 'Z')
3093 {
3094 /* Don't translate modified alphabetic keystrokes,
3095 so the user doesn't need to constantly switch
3096 layout to type control or meta keystrokes when
3097 the normal layout translates alphabetic
3098 characters to non-ascii characters. */
3099 if (!modifier_set (VK_SHIFT))
3100 wParam += ('a' - 'A');
3101 msg = WM_CHAR;
3102 }
3103 else
3104 {
3105 /* Try to handle other keystrokes by determining the
3106 base character (ie. translating the base key plus
3107 shift modifier). */
3108 int add;
3109 int isdead = 0;
3110 KEY_EVENT_RECORD key;
3111
3112 key.bKeyDown = TRUE;
3113 key.wRepeatCount = 1;
3114 key.wVirtualKeyCode = wParam;
3115 key.wVirtualScanCode = (lParam & 0xFF0000) >> 16;
3116 key.uChar.AsciiChar = 0;
3117 key.dwControlKeyState = modifiers;
3118
3119 add = w32_kbd_patch_key (&key);
3120 /* 0 means an unrecognised keycode, negative means
3121 dead key. Ignore both. */
3122 while (--add >= 0)
3123 {
3124 /* Forward asciified character sequence. */
3125 post_character_message
3126 (hwnd, WM_CHAR, key.uChar.AsciiChar, lParam,
3127 w32_get_key_modifiers (wParam, lParam));
3128 w32_kbd_patch_key (&key);
3129 }
3130 return 0;
3131 }
3132 }
3133 else
3134 {
3135 /* Let TranslateMessage handle everything else. */
3136 windows_translate = 1;
3137 }
3138 }
3139 }
3140
3141 translate:
3142 if (windows_translate)
3143 {
3144 MSG windows_msg = { hwnd, msg, wParam, lParam, 0, {0,0} };
3145
3146 windows_msg.time = GetMessageTime ();
3147 TranslateMessage (&windows_msg);
3148 goto dflt;
3149 }
3150
3151 /* Fall through */
3152
3153 case WM_SYSCHAR:
3154 case WM_CHAR:
3155 post_character_message (hwnd, msg, wParam, lParam,
3156 w32_get_key_modifiers (wParam, lParam));
3157 break;
3158
3159 /* Simulate middle mouse button events when left and right buttons
3160 are used together, but only if user has two button mouse. */
3161 case WM_LBUTTONDOWN:
3162 case WM_RBUTTONDOWN:
3163 if (w32_num_mouse_buttons > 2)
3164 goto handle_plain_button;
3165
3166 {
3167 int this = (msg == WM_LBUTTONDOWN) ? LMOUSE : RMOUSE;
3168 int other = (msg == WM_LBUTTONDOWN) ? RMOUSE : LMOUSE;
3169
3170 if (button_state & this)
3171 return 0;
3172
3173 if (button_state == 0)
3174 SetCapture (hwnd);
3175
3176 button_state |= this;
3177
3178 if (button_state & other)
3179 {
3180 if (mouse_button_timer)
3181 {
3182 KillTimer (hwnd, mouse_button_timer);
3183 mouse_button_timer = 0;
3184
3185 /* Generate middle mouse event instead. */
3186 msg = WM_MBUTTONDOWN;
3187 button_state |= MMOUSE;
3188 }
3189 else if (button_state & MMOUSE)
3190 {
3191 /* Ignore button event if we've already generated a
3192 middle mouse down event. This happens if the
3193 user releases and press one of the two buttons
3194 after we've faked a middle mouse event. */
3195 return 0;
3196 }
3197 else
3198 {
3199 /* Flush out saved message. */
3200 post_msg (&saved_mouse_button_msg);
3201 }
3202 wmsg.dwModifiers = w32_get_modifiers ();
3203 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3204 signal_user_input ();
3205
3206 /* Clear message buffer. */
3207 saved_mouse_button_msg.msg.hwnd = 0;
3208 }
3209 else
3210 {
3211 /* Hold onto message for now. */
3212 mouse_button_timer =
3213 SetTimer (hwnd, MOUSE_BUTTON_ID,
3214 w32_mouse_button_tolerance, NULL);
3215 saved_mouse_button_msg.msg.hwnd = hwnd;
3216 saved_mouse_button_msg.msg.message = msg;
3217 saved_mouse_button_msg.msg.wParam = wParam;
3218 saved_mouse_button_msg.msg.lParam = lParam;
3219 saved_mouse_button_msg.msg.time = GetMessageTime ();
3220 saved_mouse_button_msg.dwModifiers = w32_get_modifiers ();
3221 }
3222 }
3223 return 0;
3224
3225 case WM_LBUTTONUP:
3226 case WM_RBUTTONUP:
3227 if (w32_num_mouse_buttons > 2)
3228 goto handle_plain_button;
3229
3230 {
3231 int this = (msg == WM_LBUTTONUP) ? LMOUSE : RMOUSE;
3232 int other = (msg == WM_LBUTTONUP) ? RMOUSE : LMOUSE;
3233
3234 if ((button_state & this) == 0)
3235 return 0;
3236
3237 button_state &= ~this;
3238
3239 if (button_state & MMOUSE)
3240 {
3241 /* Only generate event when second button is released. */
3242 if ((button_state & other) == 0)
3243 {
3244 msg = WM_MBUTTONUP;
3245 button_state &= ~MMOUSE;
3246
3247 if (button_state) abort ();
3248 }
3249 else
3250 return 0;
3251 }
3252 else
3253 {
3254 /* Flush out saved message if necessary. */
3255 if (saved_mouse_button_msg.msg.hwnd)
3256 {
3257 post_msg (&saved_mouse_button_msg);
3258 }
3259 }
3260 wmsg.dwModifiers = w32_get_modifiers ();
3261 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3262 signal_user_input ();
3263
3264 /* Always clear message buffer and cancel timer. */
3265 saved_mouse_button_msg.msg.hwnd = 0;
3266 KillTimer (hwnd, mouse_button_timer);
3267 mouse_button_timer = 0;
3268
3269 if (button_state == 0)
3270 ReleaseCapture ();
3271 }
3272 return 0;
3273
3274 case WM_XBUTTONDOWN:
3275 case WM_XBUTTONUP:
3276 if (w32_pass_extra_mouse_buttons_to_system)
3277 goto dflt;
3278 /* else fall through and process them. */
3279 case WM_MBUTTONDOWN:
3280 case WM_MBUTTONUP:
3281 handle_plain_button:
3282 {
3283 BOOL up;
3284 int button;
3285
3286 /* Ignore middle and extra buttons as long as the menu is active. */
3287 f = x_window_to_frame (dpyinfo, hwnd);
3288 if (f && f->output_data.w32->menubar_active)
3289 return 0;
3290
3291 if (parse_button (msg, HIWORD (wParam), &button, &up))
3292 {
3293 if (up) ReleaseCapture ();
3294 else SetCapture (hwnd);
3295 button = (button == 0) ? LMOUSE :
3296 ((button == 1) ? MMOUSE : RMOUSE);
3297 if (up)
3298 button_state &= ~button;
3299 else
3300 button_state |= button;
3301 }
3302 }
3303
3304 wmsg.dwModifiers = w32_get_modifiers ();
3305 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3306 signal_user_input ();
3307
3308 /* Need to return true for XBUTTON messages, false for others,
3309 to indicate that we processed the message. */
3310 return (msg == WM_XBUTTONDOWN || msg == WM_XBUTTONUP);
3311
3312 case WM_MOUSEMOVE:
3313 /* Ignore mouse movements as long as the menu is active. These
3314 movements are processed by the window manager anyway, and
3315 it's wrong to handle them as if they happened on the
3316 underlying frame. */
3317 f = x_window_to_frame (dpyinfo, hwnd);
3318 if (f && f->output_data.w32->menubar_active)
3319 return 0;
3320
3321 /* If the mouse has just moved into the frame, start tracking
3322 it, so we will be notified when it leaves the frame. Mouse
3323 tracking only works under W98 and NT4 and later. On earlier
3324 versions, there is no way of telling when the mouse leaves the
3325 frame, so we just have to put up with help-echo and mouse
3326 highlighting remaining while the frame is not active. */
3327 if (track_mouse_event_fn && !track_mouse_window)
3328 {
3329 TRACKMOUSEEVENT tme;
3330 tme.cbSize = sizeof (tme);
3331 tme.dwFlags = TME_LEAVE;
3332 tme.hwndTrack = hwnd;
3333
3334 track_mouse_event_fn (&tme);
3335 track_mouse_window = hwnd;
3336 }
3337 case WM_VSCROLL:
3338 if (w32_mouse_move_interval <= 0
3339 || (msg == WM_MOUSEMOVE && button_state == 0))
3340 {
3341 wmsg.dwModifiers = w32_get_modifiers ();
3342 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3343 return 0;
3344 }
3345
3346 /* Hang onto mouse move and scroll messages for a bit, to avoid
3347 sending such events to Emacs faster than it can process them.
3348 If we get more events before the timer from the first message
3349 expires, we just replace the first message. */
3350
3351 if (saved_mouse_move_msg.msg.hwnd == 0)
3352 mouse_move_timer =
3353 SetTimer (hwnd, MOUSE_MOVE_ID,
3354 w32_mouse_move_interval, NULL);
3355
3356 /* Hold onto message for now. */
3357 saved_mouse_move_msg.msg.hwnd = hwnd;
3358 saved_mouse_move_msg.msg.message = msg;
3359 saved_mouse_move_msg.msg.wParam = wParam;
3360 saved_mouse_move_msg.msg.lParam = lParam;
3361 saved_mouse_move_msg.msg.time = GetMessageTime ();
3362 saved_mouse_move_msg.dwModifiers = w32_get_modifiers ();
3363
3364 return 0;
3365
3366 case WM_MOUSEWHEEL:
3367 wmsg.dwModifiers = w32_get_modifiers ();
3368 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3369 signal_user_input ();
3370 return 0;
3371
3372 case WM_DROPFILES:
3373 wmsg.dwModifiers = w32_get_modifiers ();
3374 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3375 signal_user_input ();
3376 return 0;
3377
3378 case WM_TIMER:
3379 /* Flush out saved messages if necessary. */
3380 if (wParam == mouse_button_timer)
3381 {
3382 if (saved_mouse_button_msg.msg.hwnd)
3383 {
3384 post_msg (&saved_mouse_button_msg);
3385 signal_user_input ();
3386 saved_mouse_button_msg.msg.hwnd = 0;
3387 }
3388 KillTimer (hwnd, mouse_button_timer);
3389 mouse_button_timer = 0;
3390 }
3391 else if (wParam == mouse_move_timer)
3392 {
3393 if (saved_mouse_move_msg.msg.hwnd)
3394 {
3395 post_msg (&saved_mouse_move_msg);
3396 saved_mouse_move_msg.msg.hwnd = 0;
3397 }
3398 KillTimer (hwnd, mouse_move_timer);
3399 mouse_move_timer = 0;
3400 }
3401 else if (wParam == menu_free_timer)
3402 {
3403 KillTimer (hwnd, menu_free_timer);
3404 menu_free_timer = 0;
3405 f = x_window_to_frame (dpyinfo, hwnd);
3406 if (!f->output_data.w32->menu_command_in_progress)
3407 {
3408 /* Free memory used by owner-drawn and help-echo strings. */
3409 w32_free_menu_strings (hwnd);
3410 f->output_data.w32->menubar_active = 0;
3411 }
3412 }
3413 return 0;
3414
3415 case WM_NCACTIVATE:
3416 /* Windows doesn't send us focus messages when putting up and
3417 taking down a system popup dialog as for Ctrl-Alt-Del on Windows 95.
3418 The only indication we get that something happened is receiving
3419 this message afterwards. So this is a good time to reset our
3420 keyboard modifiers' state. */
3421 reset_modifiers ();
3422 goto dflt;
3423
3424 case WM_INITMENU:
3425 button_state = 0;
3426 ReleaseCapture ();
3427 /* We must ensure menu bar is fully constructed and up to date
3428 before allowing user interaction with it. To achieve this
3429 we send this message to the lisp thread and wait for a
3430 reply (whose value is not actually needed) to indicate that
3431 the menu bar is now ready for use, so we can now return.
3432
3433 To remain responsive in the meantime, we enter a nested message
3434 loop that can process all other messages.
3435
3436 However, we skip all this if the message results from calling
3437 TrackPopupMenu - in fact, we must NOT attempt to send the lisp
3438 thread a message because it is blocked on us at this point. We
3439 set menubar_active before calling TrackPopupMenu to indicate
3440 this (there is no possibility of confusion with real menubar
3441 being active). */
3442
3443 f = x_window_to_frame (dpyinfo, hwnd);
3444 if (f
3445 && (f->output_data.w32->menubar_active
3446 /* We can receive this message even in the absence of a
3447 menubar (ie. when the system menu is activated) - in this
3448 case we do NOT want to forward the message, otherwise it
3449 will cause the menubar to suddenly appear when the user
3450 had requested it to be turned off! */
3451 || f->output_data.w32->menubar_widget == NULL))
3452 return 0;
3453
3454 {
3455 deferred_msg msg_buf;
3456
3457 /* Detect if message has already been deferred; in this case
3458 we cannot return any sensible value to ignore this. */
3459 if (find_deferred_msg (hwnd, msg) != NULL)
3460 abort ();
3461
3462 return send_deferred_msg (&msg_buf, hwnd, msg, wParam, lParam);
3463 }
3464
3465 case WM_EXITMENULOOP:
3466 f = x_window_to_frame (dpyinfo, hwnd);
3467
3468 /* If a menu command is not already in progress, check again
3469 after a short delay, since Windows often (always?) sends the
3470 WM_EXITMENULOOP before the corresponding WM_COMMAND message. */
3471 if (f && !f->output_data.w32->menu_command_in_progress)
3472 menu_free_timer = SetTimer (hwnd, MENU_FREE_ID, MENU_FREE_DELAY, NULL);
3473 goto dflt;
3474
3475 case WM_MENUSELECT:
3476 /* Direct handling of help_echo in menus. Should be safe now
3477 that we generate the help_echo by placing a help event in the
3478 keyboard buffer. */
3479 {
3480 HMENU menu = (HMENU) lParam;
3481 UINT menu_item = (UINT) LOWORD (wParam);
3482 UINT flags = (UINT) HIWORD (wParam);
3483
3484 w32_menu_display_help (hwnd, menu, menu_item, flags);
3485 }
3486 return 0;
3487
3488 case WM_MEASUREITEM:
3489 f = x_window_to_frame (dpyinfo, hwnd);
3490 if (f)
3491 {
3492 MEASUREITEMSTRUCT * pMis = (MEASUREITEMSTRUCT *) lParam;
3493
3494 if (pMis->CtlType == ODT_MENU)
3495 {
3496 /* Work out dimensions for popup menu titles. */
3497 char * title = (char *) pMis->itemData;
3498 HDC hdc = GetDC (hwnd);
3499 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
3500 LOGFONT menu_logfont;
3501 HFONT old_font;
3502 SIZE size;
3503
3504 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
3505 menu_logfont.lfWeight = FW_BOLD;
3506 menu_font = CreateFontIndirect (&menu_logfont);
3507 old_font = SelectObject (hdc, menu_font);
3508
3509 pMis->itemHeight = GetSystemMetrics (SM_CYMENUSIZE);
3510 if (title)
3511 {
3512 if (unicode_append_menu)
3513 GetTextExtentPoint32W (hdc, (WCHAR *) title,
3514 wcslen ((WCHAR *) title),
3515 &size);
3516 else
3517 GetTextExtentPoint32 (hdc, title, strlen (title), &size);
3518
3519 pMis->itemWidth = size.cx;
3520 if (pMis->itemHeight < size.cy)
3521 pMis->itemHeight = size.cy;
3522 }
3523 else
3524 pMis->itemWidth = 0;
3525
3526 SelectObject (hdc, old_font);
3527 DeleteObject (menu_font);
3528 ReleaseDC (hwnd, hdc);
3529 return TRUE;
3530 }
3531 }
3532 return 0;
3533
3534 case WM_DRAWITEM:
3535 f = x_window_to_frame (dpyinfo, hwnd);
3536 if (f)
3537 {
3538 DRAWITEMSTRUCT * pDis = (DRAWITEMSTRUCT *) lParam;
3539
3540 if (pDis->CtlType == ODT_MENU)
3541 {
3542 /* Draw popup menu title. */
3543 char * title = (char *) pDis->itemData;
3544 if (title)
3545 {
3546 HDC hdc = pDis->hDC;
3547 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
3548 LOGFONT menu_logfont;
3549 HFONT old_font;
3550
3551 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
3552 menu_logfont.lfWeight = FW_BOLD;
3553 menu_font = CreateFontIndirect (&menu_logfont);
3554 old_font = SelectObject (hdc, menu_font);
3555
3556 /* Always draw title as if not selected. */
3557 if (unicode_append_menu)
3558 ExtTextOutW (hdc,
3559 pDis->rcItem.left
3560 + GetSystemMetrics (SM_CXMENUCHECK),
3561 pDis->rcItem.top,
3562 ETO_OPAQUE, &pDis->rcItem,
3563 (WCHAR *) title,
3564 wcslen ((WCHAR *) title), NULL);
3565 else
3566 ExtTextOut (hdc,
3567 pDis->rcItem.left
3568 + GetSystemMetrics (SM_CXMENUCHECK),
3569 pDis->rcItem.top,
3570 ETO_OPAQUE, &pDis->rcItem,
3571 title, strlen (title), NULL);
3572
3573 SelectObject (hdc, old_font);
3574 DeleteObject (menu_font);
3575 }
3576 return TRUE;
3577 }
3578 }
3579 return 0;
3580
3581 #if 0
3582 /* Still not right - can't distinguish between clicks in the
3583 client area of the frame from clicks forwarded from the scroll
3584 bars - may have to hook WM_NCHITTEST to remember the mouse
3585 position and then check if it is in the client area ourselves. */
3586 case WM_MOUSEACTIVATE:
3587 /* Discard the mouse click that activates a frame, allowing the
3588 user to click anywhere without changing point (or worse!).
3589 Don't eat mouse clicks on scrollbars though!! */
3590 if (LOWORD (lParam) == HTCLIENT )
3591 return MA_ACTIVATEANDEAT;
3592 goto dflt;
3593 #endif
3594
3595 case WM_MOUSELEAVE:
3596 /* No longer tracking mouse. */
3597 track_mouse_window = NULL;
3598
3599 case WM_ACTIVATEAPP:
3600 case WM_ACTIVATE:
3601 case WM_WINDOWPOSCHANGED:
3602 case WM_SHOWWINDOW:
3603 /* Inform lisp thread that a frame might have just been obscured
3604 or exposed, so should recheck visibility of all frames. */
3605 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3606 goto dflt;
3607
3608 case WM_SETFOCUS:
3609 dpyinfo->faked_key = 0;
3610 reset_modifiers ();
3611 register_hot_keys (hwnd);
3612 goto command;
3613 case WM_KILLFOCUS:
3614 unregister_hot_keys (hwnd);
3615 button_state = 0;
3616 ReleaseCapture ();
3617 /* Relinquish the system caret. */
3618 if (w32_system_caret_hwnd)
3619 {
3620 w32_visible_system_caret_hwnd = NULL;
3621 w32_system_caret_hwnd = NULL;
3622 DestroyCaret ();
3623 }
3624 goto command;
3625 case WM_COMMAND:
3626 f = x_window_to_frame (dpyinfo, hwnd);
3627 if (f && HIWORD (wParam) == 0)
3628 {
3629 f->output_data.w32->menu_command_in_progress = 1;
3630 if (menu_free_timer)
3631 {
3632 KillTimer (hwnd, menu_free_timer);
3633 menu_free_timer = 0;
3634 }
3635 }
3636 case WM_MOVE:
3637 case WM_SIZE:
3638 command:
3639 wmsg.dwModifiers = w32_get_modifiers ();
3640 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3641 goto dflt;
3642
3643 case WM_CLOSE:
3644 wmsg.dwModifiers = w32_get_modifiers ();
3645 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3646 return 0;
3647
3648 case WM_WINDOWPOSCHANGING:
3649 /* Don't restrict the sizing of tip frames. */
3650 if (hwnd == tip_window)
3651 return 0;
3652 {
3653 WINDOWPLACEMENT wp;
3654 LPWINDOWPOS lppos = (WINDOWPOS *) lParam;
3655
3656 wp.length = sizeof (WINDOWPLACEMENT);
3657 GetWindowPlacement (hwnd, &wp);
3658
3659 if (wp.showCmd != SW_SHOWMINIMIZED && (lppos->flags & SWP_NOSIZE) == 0)
3660 {
3661 RECT rect;
3662 int wdiff;
3663 int hdiff;
3664 DWORD font_width;
3665 DWORD line_height;
3666 DWORD internal_border;
3667 DWORD scrollbar_extra;
3668 RECT wr;
3669
3670 wp.length = sizeof(wp);
3671 GetWindowRect (hwnd, &wr);
3672
3673 enter_crit ();
3674
3675 font_width = GetWindowLong (hwnd, WND_FONTWIDTH_INDEX);
3676 line_height = GetWindowLong (hwnd, WND_LINEHEIGHT_INDEX);
3677 internal_border = GetWindowLong (hwnd, WND_BORDER_INDEX);
3678 scrollbar_extra = GetWindowLong (hwnd, WND_SCROLLBAR_INDEX);
3679
3680 leave_crit ();
3681
3682 memset (&rect, 0, sizeof (rect));
3683 AdjustWindowRect (&rect, GetWindowLong (hwnd, GWL_STYLE),
3684 GetMenu (hwnd) != NULL);
3685
3686 /* Force width and height of client area to be exact
3687 multiples of the character cell dimensions. */
3688 wdiff = (lppos->cx - (rect.right - rect.left)
3689 - 2 * internal_border - scrollbar_extra)
3690 % font_width;
3691 hdiff = (lppos->cy - (rect.bottom - rect.top)
3692 - 2 * internal_border)
3693 % line_height;
3694
3695 if (wdiff || hdiff)
3696 {
3697 /* For right/bottom sizing we can just fix the sizes.
3698 However for top/left sizing we will need to fix the X
3699 and Y positions as well. */
3700
3701 lppos->cx -= wdiff;
3702 lppos->cy -= hdiff;
3703
3704 if (wp.showCmd != SW_SHOWMAXIMIZED
3705 && (lppos->flags & SWP_NOMOVE) == 0)
3706 {
3707 if (lppos->x != wr.left || lppos->y != wr.top)
3708 {
3709 lppos->x += wdiff;
3710 lppos->y += hdiff;
3711 }
3712 else
3713 {
3714 lppos->flags |= SWP_NOMOVE;
3715 }
3716 }
3717
3718 return 0;
3719 }
3720 }
3721 }
3722
3723 goto dflt;
3724
3725 case WM_GETMINMAXINFO:
3726 /* Hack to correct bug that allows Emacs frames to be resized
3727 below the Minimum Tracking Size. */
3728 ((LPMINMAXINFO) lParam)->ptMinTrackSize.y++;
3729 /* Hack to allow resizing the Emacs frame above the screen size.
3730 Note that Windows 9x limits coordinates to 16-bits. */
3731 ((LPMINMAXINFO) lParam)->ptMaxTrackSize.x = 32767;
3732 ((LPMINMAXINFO) lParam)->ptMaxTrackSize.y = 32767;
3733 return 0;
3734
3735 case WM_SETCURSOR:
3736 if (LOWORD (lParam) == HTCLIENT)
3737 return 0;
3738
3739 goto dflt;
3740
3741 case WM_EMACS_SETCURSOR:
3742 {
3743 Cursor cursor = (Cursor) wParam;
3744 if (cursor)
3745 SetCursor (cursor);
3746 return 0;
3747 }
3748
3749 case WM_EMACS_CREATESCROLLBAR:
3750 return (LRESULT) w32_createscrollbar ((struct frame *) wParam,
3751 (struct scroll_bar *) lParam);
3752
3753 case WM_EMACS_SHOWWINDOW:
3754 return ShowWindow ((HWND) wParam, (WPARAM) lParam);
3755
3756 case WM_EMACS_SETFOREGROUND:
3757 {
3758 HWND foreground_window;
3759 DWORD foreground_thread, retval;
3760
3761 /* On NT 5.0, and apparently Windows 98, it is necessary to
3762 attach to the thread that currently has focus in order to
3763 pull the focus away from it. */
3764 foreground_window = GetForegroundWindow ();
3765 foreground_thread = GetWindowThreadProcessId (foreground_window, NULL);
3766 if (!foreground_window
3767 || foreground_thread == GetCurrentThreadId ()
3768 || !AttachThreadInput (GetCurrentThreadId (),
3769 foreground_thread, TRUE))
3770 foreground_thread = 0;
3771
3772 retval = SetForegroundWindow ((HWND) wParam);
3773
3774 /* Detach from the previous foreground thread. */
3775 if (foreground_thread)
3776 AttachThreadInput (GetCurrentThreadId (),
3777 foreground_thread, FALSE);
3778
3779 return retval;
3780 }
3781
3782 case WM_EMACS_SETWINDOWPOS:
3783 {
3784 WINDOWPOS * pos = (WINDOWPOS *) wParam;
3785 return SetWindowPos (hwnd, pos->hwndInsertAfter,
3786 pos->x, pos->y, pos->cx, pos->cy, pos->flags);
3787 }
3788
3789 case WM_EMACS_DESTROYWINDOW:
3790 DragAcceptFiles ((HWND) wParam, FALSE);
3791 return DestroyWindow ((HWND) wParam);
3792
3793 case WM_EMACS_HIDE_CARET:
3794 return HideCaret (hwnd);
3795
3796 case WM_EMACS_SHOW_CARET:
3797 return ShowCaret (hwnd);
3798
3799 case WM_EMACS_DESTROY_CARET:
3800 w32_system_caret_hwnd = NULL;
3801 w32_visible_system_caret_hwnd = NULL;
3802 return DestroyCaret ();
3803
3804 case WM_EMACS_TRACK_CARET:
3805 /* If there is currently no system caret, create one. */
3806 if (w32_system_caret_hwnd == NULL)
3807 {
3808 /* Use the default caret width, and avoid changing it
3809 unneccesarily, as it confuses screen reader software. */
3810 w32_system_caret_hwnd = hwnd;
3811 CreateCaret (hwnd, NULL, 0,
3812 w32_system_caret_height);
3813 }
3814
3815 if (!SetCaretPos (w32_system_caret_x, w32_system_caret_y))
3816 return 0;
3817 /* Ensure visible caret gets turned on when requested. */
3818 else if (w32_use_visible_system_caret
3819 && w32_visible_system_caret_hwnd != hwnd)
3820 {
3821 w32_visible_system_caret_hwnd = hwnd;
3822 return ShowCaret (hwnd);
3823 }
3824 /* Ensure visible caret gets turned off when requested. */
3825 else if (!w32_use_visible_system_caret
3826 && w32_visible_system_caret_hwnd)
3827 {
3828 w32_visible_system_caret_hwnd = NULL;
3829 return HideCaret (hwnd);
3830 }
3831 else
3832 return 1;
3833
3834 case WM_EMACS_TRACKPOPUPMENU:
3835 {
3836 UINT flags;
3837 POINT *pos;
3838 int retval;
3839 pos = (POINT *)lParam;
3840 flags = TPM_CENTERALIGN;
3841 if (button_state & LMOUSE)
3842 flags |= TPM_LEFTBUTTON;
3843 else if (button_state & RMOUSE)
3844 flags |= TPM_RIGHTBUTTON;
3845
3846 /* Remember we did a SetCapture on the initial mouse down event,
3847 so for safety, we make sure the capture is cancelled now. */
3848 ReleaseCapture ();
3849 button_state = 0;
3850
3851 /* Use menubar_active to indicate that WM_INITMENU is from
3852 TrackPopupMenu below, and should be ignored. */
3853 f = x_window_to_frame (dpyinfo, hwnd);
3854 if (f)
3855 f->output_data.w32->menubar_active = 1;
3856
3857 if (TrackPopupMenu ((HMENU)wParam, flags, pos->x, pos->y,
3858 0, hwnd, NULL))
3859 {
3860 MSG amsg;
3861 /* Eat any mouse messages during popupmenu */
3862 while (PeekMessage (&amsg, hwnd, WM_MOUSEFIRST, WM_MOUSELAST,
3863 PM_REMOVE));
3864 /* Get the menu selection, if any */
3865 if (PeekMessage (&amsg, hwnd, WM_COMMAND, WM_COMMAND, PM_REMOVE))
3866 {
3867 retval = LOWORD (amsg.wParam);
3868 }
3869 else
3870 {
3871 retval = 0;
3872 }
3873 }
3874 else
3875 {
3876 retval = -1;
3877 }
3878
3879 return retval;
3880 }
3881
3882 default:
3883 /* Check for messages registered at runtime. */
3884 if (msg == msh_mousewheel)
3885 {
3886 wmsg.dwModifiers = w32_get_modifiers ();
3887 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3888 signal_user_input ();
3889 return 0;
3890 }
3891
3892 dflt:
3893 return DefWindowProc (hwnd, msg, wParam, lParam);
3894 }
3895
3896
3897 /* The most common default return code for handled messages is 0. */
3898 return 0;
3899 }
3900
3901 static void
3902 my_create_window (f)
3903 struct frame * f;
3904 {
3905 MSG msg;
3906
3907 if (!PostThreadMessage (dwWindowsThreadId, WM_EMACS_CREATEWINDOW, (WPARAM)f, 0))
3908 abort ();
3909 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
3910 }
3911
3912
3913 /* Create a tooltip window. Unlike my_create_window, we do not do this
3914 indirectly via the Window thread, as we do not need to process Window
3915 messages for the tooltip. Creating tooltips indirectly also creates
3916 deadlocks when tooltips are created for menu items. */
3917 static void
3918 my_create_tip_window (f)
3919 struct frame *f;
3920 {
3921 RECT rect;
3922
3923 rect.left = rect.top = 0;
3924 rect.right = FRAME_PIXEL_WIDTH (f);
3925 rect.bottom = FRAME_PIXEL_HEIGHT (f);
3926
3927 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
3928 FRAME_EXTERNAL_MENU_BAR (f));
3929
3930 tip_window = FRAME_W32_WINDOW (f)
3931 = CreateWindow (EMACS_CLASS,
3932 f->namebuf,
3933 f->output_data.w32->dwStyle,
3934 f->left_pos,
3935 f->top_pos,
3936 rect.right - rect.left,
3937 rect.bottom - rect.top,
3938 FRAME_W32_WINDOW (SELECTED_FRAME ()), /* owner */
3939 NULL,
3940 hinst,
3941 NULL);
3942
3943 if (tip_window)
3944 {
3945 SetWindowLong (tip_window, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
3946 SetWindowLong (tip_window, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
3947 SetWindowLong (tip_window, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
3948 SetWindowLong (tip_window, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
3949
3950 /* Tip frames have no scrollbars. */
3951 SetWindowLong (tip_window, WND_SCROLLBAR_INDEX, 0);
3952
3953 /* Do this to discard the default setting specified by our parent. */
3954 ShowWindow (tip_window, SW_HIDE);
3955 }
3956 }
3957
3958
3959 /* Create and set up the w32 window for frame F. */
3960
3961 static void
3962 w32_window (f, window_prompting, minibuffer_only)
3963 struct frame *f;
3964 long window_prompting;
3965 int minibuffer_only;
3966 {
3967 BLOCK_INPUT;
3968
3969 /* Use the resource name as the top-level window name
3970 for looking up resources. Make a non-Lisp copy
3971 for the window manager, so GC relocation won't bother it.
3972
3973 Elsewhere we specify the window name for the window manager. */
3974
3975 {
3976 char *str = (char *) SDATA (Vx_resource_name);
3977 f->namebuf = (char *) xmalloc (strlen (str) + 1);
3978 strcpy (f->namebuf, str);
3979 }
3980
3981 my_create_window (f);
3982
3983 validate_x_resource_name ();
3984
3985 /* x_set_name normally ignores requests to set the name if the
3986 requested name is the same as the current name. This is the one
3987 place where that assumption isn't correct; f->name is set, but
3988 the server hasn't been told. */
3989 {
3990 Lisp_Object name;
3991 int explicit = f->explicit_name;
3992
3993 f->explicit_name = 0;
3994 name = f->name;
3995 f->name = Qnil;
3996 x_set_name (f, name, explicit);
3997 }
3998
3999 UNBLOCK_INPUT;
4000
4001 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
4002 initialize_frame_menubar (f);
4003
4004 if (FRAME_W32_WINDOW (f) == 0)
4005 error ("Unable to create window");
4006 }
4007
4008 /* Handle the icon stuff for this window. Perhaps later we might
4009 want an x_set_icon_position which can be called interactively as
4010 well. */
4011
4012 static void
4013 x_icon (f, parms)
4014 struct frame *f;
4015 Lisp_Object parms;
4016 {
4017 Lisp_Object icon_x, icon_y;
4018
4019 /* Set the position of the icon. Note that Windows 95 groups all
4020 icons in the tray. */
4021 icon_x = w32_get_arg (parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
4022 icon_y = w32_get_arg (parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
4023 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
4024 {
4025 CHECK_NUMBER (icon_x);
4026 CHECK_NUMBER (icon_y);
4027 }
4028 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
4029 error ("Both left and top icon corners of icon must be specified");
4030
4031 BLOCK_INPUT;
4032
4033 if (! EQ (icon_x, Qunbound))
4034 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
4035
4036 #if 0 /* TODO */
4037 /* Start up iconic or window? */
4038 x_wm_set_window_state
4039 (f, (EQ (w32_get_arg (parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL), Qicon)
4040 ? IconicState
4041 : NormalState));
4042
4043 x_text_icon (f, (char *) SDATA ((!NILP (f->icon_name)
4044 ? f->icon_name
4045 : f->name)));
4046 #endif
4047
4048 UNBLOCK_INPUT;
4049 }
4050
4051
4052 static void
4053 x_make_gc (f)
4054 struct frame *f;
4055 {
4056 XGCValues gc_values;
4057
4058 BLOCK_INPUT;
4059
4060 /* Create the GC's of this frame.
4061 Note that many default values are used. */
4062
4063 /* Normal video */
4064 gc_values.font = FRAME_FONT (f);
4065
4066 /* Cursor has cursor-color background, background-color foreground. */
4067 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
4068 gc_values.background = f->output_data.w32->cursor_pixel;
4069 f->output_data.w32->cursor_gc
4070 = XCreateGC (NULL, FRAME_W32_WINDOW (f),
4071 (GCFont | GCForeground | GCBackground),
4072 &gc_values);
4073
4074 /* Reliefs. */
4075 f->output_data.w32->white_relief.gc = 0;
4076 f->output_data.w32->black_relief.gc = 0;
4077
4078 UNBLOCK_INPUT;
4079 }
4080
4081
4082 /* Handler for signals raised during x_create_frame and
4083 x_create_top_frame. FRAME is the frame which is partially
4084 constructed. */
4085
4086 static Lisp_Object
4087 unwind_create_frame (frame)
4088 Lisp_Object frame;
4089 {
4090 struct frame *f = XFRAME (frame);
4091
4092 /* If frame is ``official'', nothing to do. */
4093 if (!CONSP (Vframe_list) || !EQ (XCAR (Vframe_list), frame))
4094 {
4095 #ifdef GLYPH_DEBUG
4096 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4097 #endif
4098
4099 x_free_frame_resources (f);
4100
4101 /* Check that reference counts are indeed correct. */
4102 xassert (dpyinfo->reference_count == dpyinfo_refcount);
4103 xassert (dpyinfo->image_cache->refcount == image_cache_refcount);
4104
4105 return Qt;
4106 }
4107
4108 return Qnil;
4109 }
4110
4111
4112 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
4113 1, 1, 0,
4114 doc: /* Make a new window, which is called a \"frame\" in Emacs terms.
4115 Returns an Emacs frame object.
4116 PARAMETERS is an alist of frame parameters.
4117 If the parameters specify that the frame should not have a minibuffer,
4118 and do not specify a specific minibuffer window to use,
4119 then `default-minibuffer-frame' must be a frame whose minibuffer can
4120 be shared by the new frame.
4121
4122 This function is an internal primitive--use `make-frame' instead. */)
4123 (parameters)
4124 Lisp_Object parameters;
4125 {
4126 struct frame *f;
4127 Lisp_Object frame, tem;
4128 Lisp_Object name;
4129 int minibuffer_only = 0;
4130 long window_prompting = 0;
4131 int width, height;
4132 int count = SPECPDL_INDEX ();
4133 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4134 Lisp_Object display;
4135 struct w32_display_info *dpyinfo = NULL;
4136 Lisp_Object parent;
4137 struct kboard *kb;
4138
4139 check_w32 ();
4140
4141 /* Use this general default value to start with
4142 until we know if this frame has a specified name. */
4143 Vx_resource_name = Vinvocation_name;
4144
4145 display = w32_get_arg (parameters, Qdisplay, 0, 0, RES_TYPE_STRING);
4146 if (EQ (display, Qunbound))
4147 display = Qnil;
4148 dpyinfo = check_x_display_info (display);
4149 #ifdef MULTI_KBOARD
4150 kb = dpyinfo->kboard;
4151 #else
4152 kb = &the_only_kboard;
4153 #endif
4154
4155 name = w32_get_arg (parameters, Qname, "name", "Name", RES_TYPE_STRING);
4156 if (!STRINGP (name)
4157 && ! EQ (name, Qunbound)
4158 && ! NILP (name))
4159 error ("Invalid frame name--not a string or nil");
4160
4161 if (STRINGP (name))
4162 Vx_resource_name = name;
4163
4164 /* See if parent window is specified. */
4165 parent = w32_get_arg (parameters, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
4166 if (EQ (parent, Qunbound))
4167 parent = Qnil;
4168 if (! NILP (parent))
4169 CHECK_NUMBER (parent);
4170
4171 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
4172 /* No need to protect DISPLAY because that's not used after passing
4173 it to make_frame_without_minibuffer. */
4174 frame = Qnil;
4175 GCPRO4 (parameters, parent, name, frame);
4176 tem = w32_get_arg (parameters, Qminibuffer, "minibuffer", "Minibuffer",
4177 RES_TYPE_SYMBOL);
4178 if (EQ (tem, Qnone) || NILP (tem))
4179 f = make_frame_without_minibuffer (Qnil, kb, display);
4180 else if (EQ (tem, Qonly))
4181 {
4182 f = make_minibuffer_frame ();
4183 minibuffer_only = 1;
4184 }
4185 else if (WINDOWP (tem))
4186 f = make_frame_without_minibuffer (tem, kb, display);
4187 else
4188 f = make_frame (1);
4189
4190 XSETFRAME (frame, f);
4191
4192 /* Note that Windows does support scroll bars. */
4193 FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;
4194
4195 /* By default, make scrollbars the system standard width. */
4196 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = GetSystemMetrics (SM_CXVSCROLL);
4197
4198 f->output_method = output_w32;
4199 f->output_data.w32 =
4200 (struct w32_output *) xmalloc (sizeof (struct w32_output));
4201 bzero (f->output_data.w32, sizeof (struct w32_output));
4202 FRAME_FONTSET (f) = -1;
4203 record_unwind_protect (unwind_create_frame, frame);
4204
4205 f->icon_name
4206 = w32_get_arg (parameters, Qicon_name, "iconName", "Title", RES_TYPE_STRING);
4207 if (! STRINGP (f->icon_name))
4208 f->icon_name = Qnil;
4209
4210 /* FRAME_W32_DISPLAY_INFO (f) = dpyinfo; */
4211 #ifdef MULTI_KBOARD
4212 FRAME_KBOARD (f) = kb;
4213 #endif
4214
4215 /* Specify the parent under which to make this window. */
4216
4217 if (!NILP (parent))
4218 {
4219 f->output_data.w32->parent_desc = (Window) XFASTINT (parent);
4220 f->output_data.w32->explicit_parent = 1;
4221 }
4222 else
4223 {
4224 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
4225 f->output_data.w32->explicit_parent = 0;
4226 }
4227
4228 /* Set the name; the functions to which we pass f expect the name to
4229 be set. */
4230 if (EQ (name, Qunbound) || NILP (name))
4231 {
4232 f->name = build_string (dpyinfo->w32_id_name);
4233 f->explicit_name = 0;
4234 }
4235 else
4236 {
4237 f->name = name;
4238 f->explicit_name = 1;
4239 /* use the frame's title when getting resources for this frame. */
4240 specbind (Qx_resource_name, name);
4241 }
4242
4243 /* Extract the window parameters from the supplied values
4244 that are needed to determine window geometry. */
4245 {
4246 Lisp_Object font;
4247
4248 font = w32_get_arg (parameters, Qfont, "font", "Font", RES_TYPE_STRING);
4249
4250 BLOCK_INPUT;
4251 /* First, try whatever font the caller has specified. */
4252 if (STRINGP (font))
4253 {
4254 tem = Fquery_fontset (font, Qnil);
4255 if (STRINGP (tem))
4256 font = x_new_fontset (f, SDATA (tem));
4257 else
4258 font = x_new_font (f, SDATA (font));
4259 }
4260 /* Try out a font which we hope has bold and italic variations. */
4261 if (!STRINGP (font))
4262 font = x_new_font (f, "-*-Courier New-normal-r-*-*-*-100-*-*-c-*-iso8859-1");
4263 if (! STRINGP (font))
4264 font = x_new_font (f, "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1");
4265 /* If those didn't work, look for something which will at least work. */
4266 if (! STRINGP (font))
4267 font = x_new_font (f, "-*-Fixedsys-normal-r-*-*-12-*-*-*-c-*-iso8859-1");
4268 UNBLOCK_INPUT;
4269 if (! STRINGP (font))
4270 font = build_string ("Fixedsys");
4271
4272 x_default_parameter (f, parameters, Qfont, font,
4273 "font", "Font", RES_TYPE_STRING);
4274 }
4275
4276 x_default_parameter (f, parameters, Qborder_width, make_number (2),
4277 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
4278 /* This defaults to 2 in order to match xterm. We recognize either
4279 internalBorderWidth or internalBorder (which is what xterm calls
4280 it). */
4281 if (NILP (Fassq (Qinternal_border_width, parameters)))
4282 {
4283 Lisp_Object value;
4284
4285 value = w32_get_arg (parameters, Qinternal_border_width,
4286 "internalBorder", "InternalBorder", RES_TYPE_NUMBER);
4287 if (! EQ (value, Qunbound))
4288 parameters = Fcons (Fcons (Qinternal_border_width, value),
4289 parameters);
4290 }
4291 /* Default internalBorderWidth to 0 on Windows to match other programs. */
4292 x_default_parameter (f, parameters, Qinternal_border_width, make_number (0),
4293 "internalBorderWidth", "InternalBorder", RES_TYPE_NUMBER);
4294 x_default_parameter (f, parameters, Qvertical_scroll_bars, Qright,
4295 "verticalScrollBars", "ScrollBars", RES_TYPE_SYMBOL);
4296
4297 /* Also do the stuff which must be set before the window exists. */
4298 x_default_parameter (f, parameters, Qforeground_color, build_string ("black"),
4299 "foreground", "Foreground", RES_TYPE_STRING);
4300 x_default_parameter (f, parameters, Qbackground_color, build_string ("white"),
4301 "background", "Background", RES_TYPE_STRING);
4302 x_default_parameter (f, parameters, Qmouse_color, build_string ("black"),
4303 "pointerColor", "Foreground", RES_TYPE_STRING);
4304 x_default_parameter (f, parameters, Qcursor_color, build_string ("black"),
4305 "cursorColor", "Foreground", RES_TYPE_STRING);
4306 x_default_parameter (f, parameters, Qborder_color, build_string ("black"),
4307 "borderColor", "BorderColor", RES_TYPE_STRING);
4308 x_default_parameter (f, parameters, Qscreen_gamma, Qnil,
4309 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
4310 x_default_parameter (f, parameters, Qline_spacing, Qnil,
4311 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
4312 x_default_parameter (f, parameters, Qleft_fringe, Qnil,
4313 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
4314 x_default_parameter (f, parameters, Qright_fringe, Qnil,
4315 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
4316
4317
4318 /* Init faces before x_default_parameter is called for scroll-bar
4319 parameters because that function calls x_set_scroll_bar_width,
4320 which calls change_frame_size, which calls Fset_window_buffer,
4321 which runs hooks, which call Fvertical_motion. At the end, we
4322 end up in init_iterator with a null face cache, which should not
4323 happen. */
4324 init_frame_faces (f);
4325
4326 x_default_parameter (f, parameters, Qmenu_bar_lines, make_number (1),
4327 "menuBar", "MenuBar", RES_TYPE_NUMBER);
4328 x_default_parameter (f, parameters, Qtool_bar_lines, make_number (1),
4329 "toolBar", "ToolBar", RES_TYPE_NUMBER);
4330
4331 x_default_parameter (f, parameters, Qbuffer_predicate, Qnil,
4332 "bufferPredicate", "BufferPredicate", RES_TYPE_SYMBOL);
4333 x_default_parameter (f, parameters, Qtitle, Qnil,
4334 "title", "Title", RES_TYPE_STRING);
4335 x_default_parameter (f, parameters, Qfullscreen, Qnil,
4336 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
4337
4338 f->output_data.w32->dwStyle = WS_OVERLAPPEDWINDOW;
4339 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
4340
4341 f->output_data.w32->text_cursor = w32_load_cursor (IDC_IBEAM);
4342 f->output_data.w32->nontext_cursor = w32_load_cursor (IDC_ARROW);
4343 f->output_data.w32->modeline_cursor = w32_load_cursor (IDC_ARROW);
4344 f->output_data.w32->hand_cursor = w32_load_cursor (IDC_HAND);
4345 f->output_data.w32->hourglass_cursor = w32_load_cursor (IDC_WAIT);
4346 f->output_data.w32->horizontal_drag_cursor = w32_load_cursor (IDC_SIZEWE);
4347
4348 window_prompting = x_figure_window_size (f, parameters, 1);
4349
4350 tem = w32_get_arg (parameters, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
4351 f->no_split = minibuffer_only || EQ (tem, Qt);
4352
4353 w32_window (f, window_prompting, minibuffer_only);
4354 x_icon (f, parameters);
4355
4356 x_make_gc (f);
4357
4358 /* Now consider the frame official. */
4359 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
4360 Vframe_list = Fcons (frame, Vframe_list);
4361
4362 /* We need to do this after creating the window, so that the
4363 icon-creation functions can say whose icon they're describing. */
4364 x_default_parameter (f, parameters, Qicon_type, Qnil,
4365 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
4366
4367 x_default_parameter (f, parameters, Qauto_raise, Qnil,
4368 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4369 x_default_parameter (f, parameters, Qauto_lower, Qnil,
4370 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4371 x_default_parameter (f, parameters, Qcursor_type, Qbox,
4372 "cursorType", "CursorType", RES_TYPE_SYMBOL);
4373 x_default_parameter (f, parameters, Qscroll_bar_width, Qnil,
4374 "scrollBarWidth", "ScrollBarWidth", RES_TYPE_NUMBER);
4375
4376 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
4377 Change will not be effected unless different from the current
4378 FRAME_LINES (f). */
4379 width = FRAME_COLS (f);
4380 height = FRAME_LINES (f);
4381
4382 FRAME_LINES (f) = 0;
4383 SET_FRAME_COLS (f, 0);
4384 change_frame_size (f, height, width, 1, 0, 0);
4385
4386 /* Tell the server what size and position, etc, we want, and how
4387 badly we want them. This should be done after we have the menu
4388 bar so that its size can be taken into account. */
4389 BLOCK_INPUT;
4390 x_wm_set_size_hint (f, window_prompting, 0);
4391 UNBLOCK_INPUT;
4392
4393 /* Make the window appear on the frame and enable display, unless
4394 the caller says not to. However, with explicit parent, Emacs
4395 cannot control visibility, so don't try. */
4396 if (! f->output_data.w32->explicit_parent)
4397 {
4398 Lisp_Object visibility;
4399
4400 visibility = w32_get_arg (parameters, Qvisibility, 0, 0, RES_TYPE_SYMBOL);
4401 if (EQ (visibility, Qunbound))
4402 visibility = Qt;
4403
4404 if (EQ (visibility, Qicon))
4405 x_iconify_frame (f);
4406 else if (! NILP (visibility))
4407 x_make_frame_visible (f);
4408 else
4409 /* Must have been Qnil. */
4410 ;
4411 }
4412 UNGCPRO;
4413
4414 /* Make sure windows on this frame appear in calls to next-window
4415 and similar functions. */
4416 Vwindow_list = Qnil;
4417
4418 return unbind_to (count, frame);
4419 }
4420
4421 /* FRAME is used only to get a handle on the X display. We don't pass the
4422 display info directly because we're called from frame.c, which doesn't
4423 know about that structure. */
4424 Lisp_Object
4425 x_get_focus_frame (frame)
4426 struct frame *frame;
4427 {
4428 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (frame);
4429 Lisp_Object xfocus;
4430 if (! dpyinfo->w32_focus_frame)
4431 return Qnil;
4432
4433 XSETFRAME (xfocus, dpyinfo->w32_focus_frame);
4434 return xfocus;
4435 }
4436
4437 DEFUN ("w32-focus-frame", Fw32_focus_frame, Sw32_focus_frame, 1, 1, 0,
4438 doc: /* Give FRAME input focus, raising to foreground if necessary. */)
4439 (frame)
4440 Lisp_Object frame;
4441 {
4442 x_focus_on_frame (check_x_frame (frame));
4443 return Qnil;
4444 }
4445
4446 \f
4447 /* Return the charset portion of a font name. */
4448 char * xlfd_charset_of_font (char * fontname)
4449 {
4450 char *charset, *encoding;
4451
4452 encoding = strrchr(fontname, '-');
4453 if (!encoding || encoding == fontname)
4454 return NULL;
4455
4456 for (charset = encoding - 1; charset >= fontname; charset--)
4457 if (*charset == '-')
4458 break;
4459
4460 if (charset == fontname || strcmp(charset, "-*-*") == 0)
4461 return NULL;
4462
4463 return charset + 1;
4464 }
4465
4466 struct font_info *w32_load_bdf_font (struct frame *f, char *fontname,
4467 int size, char* filename);
4468 static Lisp_Object w32_list_bdf_fonts (Lisp_Object pattern, int max_names);
4469 static BOOL w32_to_x_font (LOGFONT * lplf, char * lpxstr, int len,
4470 char * charset);
4471 static BOOL x_to_w32_font (char *lpxstr, LOGFONT *lplogfont);
4472
4473 static struct font_info *
4474 w32_load_system_font (f,fontname,size)
4475 struct frame *f;
4476 char * fontname;
4477 int size;
4478 {
4479 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4480 Lisp_Object font_names;
4481
4482 /* Get a list of all the fonts that match this name. Once we
4483 have a list of matching fonts, we compare them against the fonts
4484 we already have loaded by comparing names. */
4485 font_names = w32_list_fonts (f, build_string (fontname), size, 100);
4486
4487 if (!NILP (font_names))
4488 {
4489 Lisp_Object tail;
4490 int i;
4491
4492 /* First check if any are already loaded, as that is cheaper
4493 than loading another one. */
4494 for (i = 0; i < dpyinfo->n_fonts; i++)
4495 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
4496 if (dpyinfo->font_table[i].name
4497 && (!strcmp (dpyinfo->font_table[i].name,
4498 SDATA (XCAR (tail)))
4499 || !strcmp (dpyinfo->font_table[i].full_name,
4500 SDATA (XCAR (tail)))))
4501 return (dpyinfo->font_table + i);
4502
4503 fontname = (char *) SDATA (XCAR (font_names));
4504 }
4505 else if (w32_strict_fontnames)
4506 {
4507 /* If EnumFontFamiliesEx was available, we got a full list of
4508 fonts back so stop now to avoid the possibility of loading a
4509 random font. If we had to fall back to EnumFontFamilies, the
4510 list is incomplete, so continue whether the font we want was
4511 listed or not. */
4512 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
4513 FARPROC enum_font_families_ex
4514 = GetProcAddress (gdi32, "EnumFontFamiliesExA");
4515 if (enum_font_families_ex)
4516 return NULL;
4517 }
4518
4519 /* Load the font and add it to the table. */
4520 {
4521 char *full_name, *encoding, *charset;
4522 XFontStruct *font;
4523 struct font_info *fontp;
4524 LOGFONT lf;
4525 BOOL ok;
4526 int codepage;
4527 int i;
4528
4529 if (!fontname || !x_to_w32_font (fontname, &lf))
4530 return (NULL);
4531
4532 if (!*lf.lfFaceName)
4533 /* If no name was specified for the font, we get a random font
4534 from CreateFontIndirect - this is not particularly
4535 desirable, especially since CreateFontIndirect does not
4536 fill out the missing name in lf, so we never know what we
4537 ended up with. */
4538 return NULL;
4539
4540 lf.lfQuality = DEFAULT_QUALITY;
4541
4542 font = (XFontStruct *) xmalloc (sizeof (XFontStruct));
4543 bzero (font, sizeof (*font));
4544
4545 /* Set bdf to NULL to indicate that this is a Windows font. */
4546 font->bdf = NULL;
4547
4548 BLOCK_INPUT;
4549
4550 font->hfont = CreateFontIndirect (&lf);
4551
4552 if (font->hfont == NULL)
4553 {
4554 ok = FALSE;
4555 }
4556 else
4557 {
4558 HDC hdc;
4559 HANDLE oldobj;
4560
4561 codepage = w32_codepage_for_font (fontname);
4562
4563 hdc = GetDC (dpyinfo->root_window);
4564 oldobj = SelectObject (hdc, font->hfont);
4565
4566 ok = GetTextMetrics (hdc, &font->tm);
4567 if (codepage == CP_UNICODE)
4568 font->double_byte_p = 1;
4569 else
4570 {
4571 /* Unfortunately, some fonts (eg. MingLiU, a big5 ttf font)
4572 don't report themselves as double byte fonts, when
4573 patently they are. So instead of trusting
4574 GetFontLanguageInfo, we check the properties of the
4575 codepage directly, since that is ultimately what we are
4576 working from anyway. */
4577 /* font->double_byte_p = GetFontLanguageInfo(hdc) & GCP_DBCS; */
4578 CPINFO cpi = {0};
4579 GetCPInfo (codepage, &cpi);
4580 font->double_byte_p = cpi.MaxCharSize > 1;
4581 }
4582
4583 SelectObject (hdc, oldobj);
4584 ReleaseDC (dpyinfo->root_window, hdc);
4585 /* Fill out details in lf according to the font that was
4586 actually loaded. */
4587 lf.lfHeight = font->tm.tmInternalLeading - font->tm.tmHeight;
4588 lf.lfWidth = font->tm.tmMaxCharWidth;
4589 lf.lfWeight = font->tm.tmWeight;
4590 lf.lfItalic = font->tm.tmItalic;
4591 lf.lfCharSet = font->tm.tmCharSet;
4592 lf.lfPitchAndFamily = ((font->tm.tmPitchAndFamily & TMPF_FIXED_PITCH)
4593 ? VARIABLE_PITCH : FIXED_PITCH);
4594 lf.lfOutPrecision = ((font->tm.tmPitchAndFamily & TMPF_VECTOR)
4595 ? OUT_STROKE_PRECIS : OUT_STRING_PRECIS);
4596
4597 w32_cache_char_metrics (font);
4598 }
4599
4600 UNBLOCK_INPUT;
4601
4602 if (!ok)
4603 {
4604 w32_unload_font (dpyinfo, font);
4605 return (NULL);
4606 }
4607
4608 /* Find a free slot in the font table. */
4609 for (i = 0; i < dpyinfo->n_fonts; ++i)
4610 if (dpyinfo->font_table[i].name == NULL)
4611 break;
4612
4613 /* If no free slot found, maybe enlarge the font table. */
4614 if (i == dpyinfo->n_fonts
4615 && dpyinfo->n_fonts == dpyinfo->font_table_size)
4616 {
4617 int sz;
4618 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
4619 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
4620 dpyinfo->font_table
4621 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
4622 }
4623
4624 fontp = dpyinfo->font_table + i;
4625 if (i == dpyinfo->n_fonts)
4626 ++dpyinfo->n_fonts;
4627
4628 /* Now fill in the slots of *FONTP. */
4629 BLOCK_INPUT;
4630 bzero (fontp, sizeof (*fontp));
4631 fontp->font = font;
4632 fontp->font_idx = i;
4633 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
4634 bcopy (fontname, fontp->name, strlen (fontname) + 1);
4635
4636 if (lf.lfPitchAndFamily == FIXED_PITCH)
4637 {
4638 /* Fixed width font. */
4639 fontp->average_width = fontp->space_width = FONT_WIDTH (font);
4640 }
4641 else
4642 {
4643 wchar_t space = 32;
4644 XCharStruct* pcm;
4645 pcm = w32_per_char_metric (font, &space, ANSI_FONT);
4646 if (pcm)
4647 fontp->space_width = pcm->width;
4648 else
4649 fontp->space_width = FONT_WIDTH (font);
4650
4651 fontp->average_width = font->tm.tmAveCharWidth;
4652 }
4653
4654 charset = xlfd_charset_of_font (fontname);
4655
4656 /* Cache the W32 codepage for a font. This makes w32_encode_char
4657 (called for every glyph during redisplay) much faster. */
4658 fontp->codepage = codepage;
4659
4660 /* Work out the font's full name. */
4661 full_name = (char *)xmalloc (100);
4662 if (full_name && w32_to_x_font (&lf, full_name, 100, charset))
4663 fontp->full_name = full_name;
4664 else
4665 {
4666 /* If all else fails - just use the name we used to load it. */
4667 xfree (full_name);
4668 fontp->full_name = fontp->name;
4669 }
4670
4671 fontp->size = FONT_WIDTH (font);
4672 fontp->height = FONT_HEIGHT (font);
4673
4674 /* The slot `encoding' specifies how to map a character
4675 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
4676 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
4677 (0:0x20..0x7F, 1:0xA0..0xFF,
4678 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
4679 2:0xA020..0xFF7F). For the moment, we don't know which charset
4680 uses this font. So, we set information in fontp->encoding[1]
4681 which is never used by any charset. If mapping can't be
4682 decided, set FONT_ENCODING_NOT_DECIDED. */
4683
4684 /* SJIS fonts need to be set to type 4, all others seem to work as
4685 type FONT_ENCODING_NOT_DECIDED. */
4686 encoding = strrchr (fontp->name, '-');
4687 if (encoding && strnicmp (encoding+1, "sjis", 4) == 0)
4688 fontp->encoding[1] = 4;
4689 else
4690 fontp->encoding[1] = FONT_ENCODING_NOT_DECIDED;
4691
4692 /* The following three values are set to 0 under W32, which is
4693 what they get set to if XGetFontProperty fails under X. */
4694 fontp->baseline_offset = 0;
4695 fontp->relative_compose = 0;
4696 fontp->default_ascent = 0;
4697
4698 /* Set global flag fonts_changed_p to non-zero if the font loaded
4699 has a character with a smaller width than any other character
4700 before, or if the font loaded has a smaller height than any
4701 other font loaded before. If this happens, it will make a
4702 glyph matrix reallocation necessary. */
4703 fonts_changed_p |= x_compute_min_glyph_bounds (f);
4704 UNBLOCK_INPUT;
4705 return fontp;
4706 }
4707 }
4708
4709 /* Load font named FONTNAME of size SIZE for frame F, and return a
4710 pointer to the structure font_info while allocating it dynamically.
4711 If loading fails, return NULL. */
4712 struct font_info *
4713 w32_load_font (f,fontname,size)
4714 struct frame *f;
4715 char * fontname;
4716 int size;
4717 {
4718 Lisp_Object bdf_fonts;
4719 struct font_info *retval = NULL;
4720 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4721
4722 bdf_fonts = w32_list_bdf_fonts (build_string (fontname), 1);
4723
4724 while (!retval && CONSP (bdf_fonts))
4725 {
4726 char *bdf_name, *bdf_file;
4727 Lisp_Object bdf_pair;
4728 int i;
4729
4730 bdf_name = SDATA (XCAR (bdf_fonts));
4731 bdf_pair = Fassoc (XCAR (bdf_fonts), Vw32_bdf_filename_alist);
4732 bdf_file = SDATA (XCDR (bdf_pair));
4733
4734 // If the font is already loaded, do not load it again.
4735 for (i = 0; i < dpyinfo->n_fonts; i++)
4736 {
4737 if ((dpyinfo->font_table[i].name
4738 && !strcmp (dpyinfo->font_table[i].name, bdf_name))
4739 || (dpyinfo->font_table[i].full_name
4740 && !strcmp (dpyinfo->font_table[i].full_name, bdf_name)))
4741 return dpyinfo->font_table + i;
4742 }
4743
4744 retval = w32_load_bdf_font (f, bdf_name, size, bdf_file);
4745
4746 bdf_fonts = XCDR (bdf_fonts);
4747 }
4748
4749 if (retval)
4750 return retval;
4751
4752 return w32_load_system_font(f, fontname, size);
4753 }
4754
4755
4756 void
4757 w32_unload_font (dpyinfo, font)
4758 struct w32_display_info *dpyinfo;
4759 XFontStruct * font;
4760 {
4761 if (font)
4762 {
4763 if (font->per_char) xfree (font->per_char);
4764 if (font->bdf) w32_free_bdf_font (font->bdf);
4765
4766 if (font->hfont) DeleteObject(font->hfont);
4767 xfree (font);
4768 }
4769 }
4770
4771 /* The font conversion stuff between x and w32 */
4772
4773 /* X font string is as follows (from faces.el)
4774 * (let ((- "[-?]")
4775 * (foundry "[^-]+")
4776 * (family "[^-]+")
4777 * (weight "\\(bold\\|demibold\\|medium\\)") ; 1
4778 * (weight\? "\\([^-]*\\)") ; 1
4779 * (slant "\\([ior]\\)") ; 2
4780 * (slant\? "\\([^-]?\\)") ; 2
4781 * (swidth "\\([^-]*\\)") ; 3
4782 * (adstyle "[^-]*") ; 4
4783 * (pixelsize "[0-9]+")
4784 * (pointsize "[0-9][0-9]+")
4785 * (resx "[0-9][0-9]+")
4786 * (resy "[0-9][0-9]+")
4787 * (spacing "[cmp?*]")
4788 * (avgwidth "[0-9]+")
4789 * (registry "[^-]+")
4790 * (encoding "[^-]+")
4791 * )
4792 */
4793
4794 static LONG
4795 x_to_w32_weight (lpw)
4796 char * lpw;
4797 {
4798 if (!lpw) return (FW_DONTCARE);
4799
4800 if (stricmp (lpw,"heavy") == 0) return FW_HEAVY;
4801 else if (stricmp (lpw,"extrabold") == 0) return FW_EXTRABOLD;
4802 else if (stricmp (lpw,"bold") == 0) return FW_BOLD;
4803 else if (stricmp (lpw,"demibold") == 0) return FW_SEMIBOLD;
4804 else if (stricmp (lpw,"semibold") == 0) return FW_SEMIBOLD;
4805 else if (stricmp (lpw,"medium") == 0) return FW_MEDIUM;
4806 else if (stricmp (lpw,"normal") == 0) return FW_NORMAL;
4807 else if (stricmp (lpw,"light") == 0) return FW_LIGHT;
4808 else if (stricmp (lpw,"extralight") == 0) return FW_EXTRALIGHT;
4809 else if (stricmp (lpw,"thin") == 0) return FW_THIN;
4810 else
4811 return FW_DONTCARE;
4812 }
4813
4814
4815 static char *
4816 w32_to_x_weight (fnweight)
4817 int fnweight;
4818 {
4819 if (fnweight >= FW_HEAVY) return "heavy";
4820 if (fnweight >= FW_EXTRABOLD) return "extrabold";
4821 if (fnweight >= FW_BOLD) return "bold";
4822 if (fnweight >= FW_SEMIBOLD) return "demibold";
4823 if (fnweight >= FW_MEDIUM) return "medium";
4824 if (fnweight >= FW_NORMAL) return "normal";
4825 if (fnweight >= FW_LIGHT) return "light";
4826 if (fnweight >= FW_EXTRALIGHT) return "extralight";
4827 if (fnweight >= FW_THIN) return "thin";
4828 else
4829 return "*";
4830 }
4831
4832 static LONG
4833 x_to_w32_charset (lpcs)
4834 char * lpcs;
4835 {
4836 Lisp_Object this_entry, w32_charset;
4837 char *charset;
4838 int len = strlen (lpcs);
4839
4840 /* Support "*-#nnn" format for unknown charsets. */
4841 if (strncmp (lpcs, "*-#", 3) == 0)
4842 return atoi (lpcs + 3);
4843
4844 /* Handle wildcards by ignoring them; eg. treat "big5*-*" as "big5". */
4845 charset = alloca (len + 1);
4846 strcpy (charset, lpcs);
4847 lpcs = strchr (charset, '*');
4848 if (lpcs)
4849 *lpcs = 0;
4850
4851 /* Look through w32-charset-info-alist for the character set.
4852 Format of each entry is
4853 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
4854 */
4855 this_entry = Fassoc (build_string(charset), Vw32_charset_info_alist);
4856
4857 if (NILP(this_entry))
4858 {
4859 /* At startup, we want iso8859-1 fonts to come up properly. */
4860 if (stricmp(charset, "iso8859-1") == 0)
4861 return ANSI_CHARSET;
4862 else
4863 return DEFAULT_CHARSET;
4864 }
4865
4866 w32_charset = Fcar (Fcdr (this_entry));
4867
4868 /* Translate Lisp symbol to number. */
4869 if (EQ (w32_charset, Qw32_charset_ansi))
4870 return ANSI_CHARSET;
4871 if (EQ (w32_charset, Qw32_charset_symbol))
4872 return SYMBOL_CHARSET;
4873 if (EQ (w32_charset, Qw32_charset_shiftjis))
4874 return SHIFTJIS_CHARSET;
4875 if (EQ (w32_charset, Qw32_charset_hangeul))
4876 return HANGEUL_CHARSET;
4877 if (EQ (w32_charset, Qw32_charset_chinesebig5))
4878 return CHINESEBIG5_CHARSET;
4879 if (EQ (w32_charset, Qw32_charset_gb2312))
4880 return GB2312_CHARSET;
4881 if (EQ (w32_charset, Qw32_charset_oem))
4882 return OEM_CHARSET;
4883 #ifdef JOHAB_CHARSET
4884 if (EQ (w32_charset, Qw32_charset_johab))
4885 return JOHAB_CHARSET;
4886 if (EQ (w32_charset, Qw32_charset_easteurope))
4887 return EASTEUROPE_CHARSET;
4888 if (EQ (w32_charset, Qw32_charset_turkish))
4889 return TURKISH_CHARSET;
4890 if (EQ (w32_charset, Qw32_charset_baltic))
4891 return BALTIC_CHARSET;
4892 if (EQ (w32_charset, Qw32_charset_russian))
4893 return RUSSIAN_CHARSET;
4894 if (EQ (w32_charset, Qw32_charset_arabic))
4895 return ARABIC_CHARSET;
4896 if (EQ (w32_charset, Qw32_charset_greek))
4897 return GREEK_CHARSET;
4898 if (EQ (w32_charset, Qw32_charset_hebrew))
4899 return HEBREW_CHARSET;
4900 if (EQ (w32_charset, Qw32_charset_vietnamese))
4901 return VIETNAMESE_CHARSET;
4902 if (EQ (w32_charset, Qw32_charset_thai))
4903 return THAI_CHARSET;
4904 if (EQ (w32_charset, Qw32_charset_mac))
4905 return MAC_CHARSET;
4906 #endif /* JOHAB_CHARSET */
4907 #ifdef UNICODE_CHARSET
4908 if (EQ (w32_charset, Qw32_charset_unicode))
4909 return UNICODE_CHARSET;
4910 #endif
4911
4912 return DEFAULT_CHARSET;
4913 }
4914
4915
4916 static char *
4917 w32_to_x_charset (fncharset)
4918 int fncharset;
4919 {
4920 static char buf[32];
4921 Lisp_Object charset_type;
4922
4923 switch (fncharset)
4924 {
4925 case ANSI_CHARSET:
4926 /* Handle startup case of w32-charset-info-alist not
4927 being set up yet. */
4928 if (NILP(Vw32_charset_info_alist))
4929 return "iso8859-1";
4930 charset_type = Qw32_charset_ansi;
4931 break;
4932 case DEFAULT_CHARSET:
4933 charset_type = Qw32_charset_default;
4934 break;
4935 case SYMBOL_CHARSET:
4936 charset_type = Qw32_charset_symbol;
4937 break;
4938 case SHIFTJIS_CHARSET:
4939 charset_type = Qw32_charset_shiftjis;
4940 break;
4941 case HANGEUL_CHARSET:
4942 charset_type = Qw32_charset_hangeul;
4943 break;
4944 case GB2312_CHARSET:
4945 charset_type = Qw32_charset_gb2312;
4946 break;
4947 case CHINESEBIG5_CHARSET:
4948 charset_type = Qw32_charset_chinesebig5;
4949 break;
4950 case OEM_CHARSET:
4951 charset_type = Qw32_charset_oem;
4952 break;
4953
4954 /* More recent versions of Windows (95 and NT4.0) define more
4955 character sets. */
4956 #ifdef EASTEUROPE_CHARSET
4957 case EASTEUROPE_CHARSET:
4958 charset_type = Qw32_charset_easteurope;
4959 break;
4960 case TURKISH_CHARSET:
4961 charset_type = Qw32_charset_turkish;
4962 break;
4963 case BALTIC_CHARSET:
4964 charset_type = Qw32_charset_baltic;
4965 break;
4966 case RUSSIAN_CHARSET:
4967 charset_type = Qw32_charset_russian;
4968 break;
4969 case ARABIC_CHARSET:
4970 charset_type = Qw32_charset_arabic;
4971 break;
4972 case GREEK_CHARSET:
4973 charset_type = Qw32_charset_greek;
4974 break;
4975 case HEBREW_CHARSET:
4976 charset_type = Qw32_charset_hebrew;
4977 break;
4978 case VIETNAMESE_CHARSET:
4979 charset_type = Qw32_charset_vietnamese;
4980 break;
4981 case THAI_CHARSET:
4982 charset_type = Qw32_charset_thai;
4983 break;
4984 case MAC_CHARSET:
4985 charset_type = Qw32_charset_mac;
4986 break;
4987 case JOHAB_CHARSET:
4988 charset_type = Qw32_charset_johab;
4989 break;
4990 #endif
4991
4992 #ifdef UNICODE_CHARSET
4993 case UNICODE_CHARSET:
4994 charset_type = Qw32_charset_unicode;
4995 break;
4996 #endif
4997 default:
4998 /* Encode numerical value of unknown charset. */
4999 sprintf (buf, "*-#%u", fncharset);
5000 return buf;
5001 }
5002
5003 {
5004 Lisp_Object rest;
5005 char * best_match = NULL;
5006
5007 /* Look through w32-charset-info-alist for the character set.
5008 Prefer ISO codepages, and prefer lower numbers in the ISO
5009 range. Only return charsets for codepages which are installed.
5010
5011 Format of each entry is
5012 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
5013 */
5014 for (rest = Vw32_charset_info_alist; CONSP (rest); rest = XCDR (rest))
5015 {
5016 char * x_charset;
5017 Lisp_Object w32_charset;
5018 Lisp_Object codepage;
5019
5020 Lisp_Object this_entry = XCAR (rest);
5021
5022 /* Skip invalid entries in alist. */
5023 if (!CONSP (this_entry) || !STRINGP (XCAR (this_entry))
5024 || !CONSP (XCDR (this_entry))
5025 || !SYMBOLP (XCAR (XCDR (this_entry))))
5026 continue;
5027
5028 x_charset = SDATA (XCAR (this_entry));
5029 w32_charset = XCAR (XCDR (this_entry));
5030 codepage = XCDR (XCDR (this_entry));
5031
5032 /* Look for Same charset and a valid codepage (or non-int
5033 which means ignore). */
5034 if (EQ (w32_charset, charset_type)
5035 && (!INTEGERP (codepage) || XINT (codepage) == CP_DEFAULT
5036 || IsValidCodePage (XINT (codepage))))
5037 {
5038 /* If we don't have a match already, then this is the
5039 best. */
5040 if (!best_match)
5041 best_match = x_charset;
5042 /* If this is an ISO codepage, and the best so far isn't,
5043 then this is better. */
5044 else if (strnicmp (best_match, "iso", 3) != 0
5045 && strnicmp (x_charset, "iso", 3) == 0)
5046 best_match = x_charset;
5047 /* If both are ISO8859 codepages, choose the one with the
5048 lowest number in the encoding field. */
5049 else if (strnicmp (best_match, "iso8859-", 8) == 0
5050 && strnicmp (x_charset, "iso8859-", 8) == 0)
5051 {
5052 int best_enc = atoi (best_match + 8);
5053 int this_enc = atoi (x_charset + 8);
5054 if (this_enc > 0 && this_enc < best_enc)
5055 best_match = x_charset;
5056 }
5057 }
5058 }
5059
5060 /* If no match, encode the numeric value. */
5061 if (!best_match)
5062 {
5063 sprintf (buf, "*-#%u", fncharset);
5064 return buf;
5065 }
5066
5067 strncpy(buf, best_match, 31);
5068 buf[31] = '\0';
5069 return buf;
5070 }
5071 }
5072
5073
5074 /* Return all the X charsets that map to a font. */
5075 static Lisp_Object
5076 w32_to_all_x_charsets (fncharset)
5077 int fncharset;
5078 {
5079 static char buf[32];
5080 Lisp_Object charset_type;
5081 Lisp_Object retval = Qnil;
5082
5083 switch (fncharset)
5084 {
5085 case ANSI_CHARSET:
5086 /* Handle startup case of w32-charset-info-alist not
5087 being set up yet. */
5088 if (NILP(Vw32_charset_info_alist))
5089 return Fcons (build_string ("iso8859-1"), Qnil);
5090
5091 charset_type = Qw32_charset_ansi;
5092 break;
5093 case DEFAULT_CHARSET:
5094 charset_type = Qw32_charset_default;
5095 break;
5096 case SYMBOL_CHARSET:
5097 charset_type = Qw32_charset_symbol;
5098 break;
5099 case SHIFTJIS_CHARSET:
5100 charset_type = Qw32_charset_shiftjis;
5101 break;
5102 case HANGEUL_CHARSET:
5103 charset_type = Qw32_charset_hangeul;
5104 break;
5105 case GB2312_CHARSET:
5106 charset_type = Qw32_charset_gb2312;
5107 break;
5108 case CHINESEBIG5_CHARSET:
5109 charset_type = Qw32_charset_chinesebig5;
5110 break;
5111 case OEM_CHARSET:
5112 charset_type = Qw32_charset_oem;
5113 break;
5114
5115 /* More recent versions of Windows (95 and NT4.0) define more
5116 character sets. */
5117 #ifdef EASTEUROPE_CHARSET
5118 case EASTEUROPE_CHARSET:
5119 charset_type = Qw32_charset_easteurope;
5120 break;
5121 case TURKISH_CHARSET:
5122 charset_type = Qw32_charset_turkish;
5123 break;
5124 case BALTIC_CHARSET:
5125 charset_type = Qw32_charset_baltic;
5126 break;
5127 case RUSSIAN_CHARSET:
5128 charset_type = Qw32_charset_russian;
5129 break;
5130 case ARABIC_CHARSET:
5131 charset_type = Qw32_charset_arabic;
5132 break;
5133 case GREEK_CHARSET:
5134 charset_type = Qw32_charset_greek;
5135 break;
5136 case HEBREW_CHARSET:
5137 charset_type = Qw32_charset_hebrew;
5138 break;
5139 case VIETNAMESE_CHARSET:
5140 charset_type = Qw32_charset_vietnamese;
5141 break;
5142 case THAI_CHARSET:
5143 charset_type = Qw32_charset_thai;
5144 break;
5145 case MAC_CHARSET:
5146 charset_type = Qw32_charset_mac;
5147 break;
5148 case JOHAB_CHARSET:
5149 charset_type = Qw32_charset_johab;
5150 break;
5151 #endif
5152
5153 #ifdef UNICODE_CHARSET
5154 case UNICODE_CHARSET:
5155 charset_type = Qw32_charset_unicode;
5156 break;
5157 #endif
5158 default:
5159 /* Encode numerical value of unknown charset. */
5160 sprintf (buf, "*-#%u", fncharset);
5161 return Fcons (build_string (buf), Qnil);
5162 }
5163
5164 {
5165 Lisp_Object rest;
5166 /* Look through w32-charset-info-alist for the character set.
5167 Only return charsets for codepages which are installed.
5168
5169 Format of each entry in Vw32_charset_info_alist is
5170 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
5171 */
5172 for (rest = Vw32_charset_info_alist; CONSP (rest); rest = XCDR (rest))
5173 {
5174 Lisp_Object x_charset;
5175 Lisp_Object w32_charset;
5176 Lisp_Object codepage;
5177
5178 Lisp_Object this_entry = XCAR (rest);
5179
5180 /* Skip invalid entries in alist. */
5181 if (!CONSP (this_entry) || !STRINGP (XCAR (this_entry))
5182 || !CONSP (XCDR (this_entry))
5183 || !SYMBOLP (XCAR (XCDR (this_entry))))
5184 continue;
5185
5186 x_charset = XCAR (this_entry);
5187 w32_charset = XCAR (XCDR (this_entry));
5188 codepage = XCDR (XCDR (this_entry));
5189
5190 /* Look for Same charset and a valid codepage (or non-int
5191 which means ignore). */
5192 if (EQ (w32_charset, charset_type)
5193 && (!INTEGERP (codepage) || XINT (codepage) == CP_DEFAULT
5194 || IsValidCodePage (XINT (codepage))))
5195 {
5196 retval = Fcons (x_charset, retval);
5197 }
5198 }
5199
5200 /* If no match, encode the numeric value. */
5201 if (NILP (retval))
5202 {
5203 sprintf (buf, "*-#%u", fncharset);
5204 return Fcons (build_string (buf), Qnil);
5205 }
5206
5207 return retval;
5208 }
5209 }
5210
5211 /* Get the Windows codepage corresponding to the specified font. The
5212 charset info in the font name is used to look up
5213 w32-charset-to-codepage-alist. */
5214 int
5215 w32_codepage_for_font (char *fontname)
5216 {
5217 Lisp_Object codepage, entry;
5218 char *charset_str, *charset, *end;
5219
5220 if (NILP (Vw32_charset_info_alist))
5221 return CP_DEFAULT;
5222
5223 /* Extract charset part of font string. */
5224 charset = xlfd_charset_of_font (fontname);
5225
5226 if (!charset)
5227 return CP_UNKNOWN;
5228
5229 charset_str = (char *) alloca (strlen (charset) + 1);
5230 strcpy (charset_str, charset);
5231
5232 #if 0
5233 /* Remove leading "*-". */
5234 if (strncmp ("*-", charset_str, 2) == 0)
5235 charset = charset_str + 2;
5236 else
5237 #endif
5238 charset = charset_str;
5239
5240 /* Stop match at wildcard (including preceding '-'). */
5241 if (end = strchr (charset, '*'))
5242 {
5243 if (end > charset && *(end-1) == '-')
5244 end--;
5245 *end = '\0';
5246 }
5247
5248 entry = Fassoc (build_string(charset), Vw32_charset_info_alist);
5249 if (NILP (entry))
5250 return CP_UNKNOWN;
5251
5252 codepage = Fcdr (Fcdr (entry));
5253
5254 if (NILP (codepage))
5255 return CP_8BIT;
5256 else if (XFASTINT (codepage) == XFASTINT (Qt))
5257 return CP_UNICODE;
5258 else if (INTEGERP (codepage))
5259 return XINT (codepage);
5260 else
5261 return CP_UNKNOWN;
5262 }
5263
5264
5265 static BOOL
5266 w32_to_x_font (lplogfont, lpxstr, len, specific_charset)
5267 LOGFONT * lplogfont;
5268 char * lpxstr;
5269 int len;
5270 char * specific_charset;
5271 {
5272 char* fonttype;
5273 char *fontname;
5274 char height_pixels[8];
5275 char height_dpi[8];
5276 char width_pixels[8];
5277 char *fontname_dash;
5278 int display_resy = (int) one_w32_display_info.resy;
5279 int display_resx = (int) one_w32_display_info.resx;
5280 int bufsz;
5281 struct coding_system coding;
5282
5283 if (!lpxstr) abort ();
5284
5285 if (!lplogfont)
5286 return FALSE;
5287
5288 if (lplogfont->lfOutPrecision == OUT_STRING_PRECIS)
5289 fonttype = "raster";
5290 else if (lplogfont->lfOutPrecision == OUT_STROKE_PRECIS)
5291 fonttype = "outline";
5292 else
5293 fonttype = "unknown";
5294
5295 setup_coding_system (Fcheck_coding_system (Vlocale_coding_system),
5296 &coding);
5297 coding.src_multibyte = 0;
5298 coding.dst_multibyte = 1;
5299 coding.mode |= CODING_MODE_LAST_BLOCK;
5300 /* We explicitely disable composition handling because selection
5301 data should not contain any composition sequence. */
5302 coding.composing = COMPOSITION_DISABLED;
5303 bufsz = decoding_buffer_size (&coding, LF_FACESIZE);
5304
5305 fontname = alloca(sizeof(*fontname) * bufsz);
5306 decode_coding (&coding, lplogfont->lfFaceName, fontname,
5307 strlen(lplogfont->lfFaceName), bufsz - 1);
5308 *(fontname + coding.produced) = '\0';
5309
5310 /* Replace dashes with underscores so the dashes are not
5311 misinterpreted. */
5312 fontname_dash = fontname;
5313 while (fontname_dash = strchr (fontname_dash, '-'))
5314 *fontname_dash = '_';
5315
5316 if (lplogfont->lfHeight)
5317 {
5318 sprintf (height_pixels, "%u", abs (lplogfont->lfHeight));
5319 sprintf (height_dpi, "%u",
5320 abs (lplogfont->lfHeight) * 720 / display_resy);
5321 }
5322 else
5323 {
5324 strcpy (height_pixels, "*");
5325 strcpy (height_dpi, "*");
5326 }
5327
5328 #if 0 /* Never put the width in the xfld. It fails on fonts with
5329 double-width characters. */
5330 if (lplogfont->lfWidth)
5331 sprintf (width_pixels, "%u", lplogfont->lfWidth * 10);
5332 else
5333 #endif
5334 strcpy (width_pixels, "*");
5335
5336 _snprintf (lpxstr, len - 1,
5337 "-%s-%s-%s-%c-normal-normal-%s-%s-%d-%d-%c-%s-%s",
5338 fonttype, /* foundry */
5339 fontname, /* family */
5340 w32_to_x_weight (lplogfont->lfWeight), /* weight */
5341 lplogfont->lfItalic?'i':'r', /* slant */
5342 /* setwidth name */
5343 /* add style name */
5344 height_pixels, /* pixel size */
5345 height_dpi, /* point size */
5346 display_resx, /* resx */
5347 display_resy, /* resy */
5348 ((lplogfont->lfPitchAndFamily & 0x3) == VARIABLE_PITCH)
5349 ? 'p' : 'c', /* spacing */
5350 width_pixels, /* avg width */
5351 specific_charset ? specific_charset
5352 : w32_to_x_charset (lplogfont->lfCharSet)
5353 /* charset registry and encoding */
5354 );
5355
5356 lpxstr[len - 1] = 0; /* just to be sure */
5357 return (TRUE);
5358 }
5359
5360 static BOOL
5361 x_to_w32_font (lpxstr, lplogfont)
5362 char * lpxstr;
5363 LOGFONT * lplogfont;
5364 {
5365 struct coding_system coding;
5366
5367 if (!lplogfont) return (FALSE);
5368
5369 memset (lplogfont, 0, sizeof (*lplogfont));
5370
5371 /* Set default value for each field. */
5372 #if 1
5373 lplogfont->lfOutPrecision = OUT_DEFAULT_PRECIS;
5374 lplogfont->lfClipPrecision = CLIP_DEFAULT_PRECIS;
5375 lplogfont->lfQuality = DEFAULT_QUALITY;
5376 #else
5377 /* go for maximum quality */
5378 lplogfont->lfOutPrecision = OUT_STROKE_PRECIS;
5379 lplogfont->lfClipPrecision = CLIP_STROKE_PRECIS;
5380 lplogfont->lfQuality = PROOF_QUALITY;
5381 #endif
5382
5383 lplogfont->lfCharSet = DEFAULT_CHARSET;
5384 lplogfont->lfWeight = FW_DONTCARE;
5385 lplogfont->lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
5386
5387 if (!lpxstr)
5388 return FALSE;
5389
5390 /* Provide a simple escape mechanism for specifying Windows font names
5391 * directly -- if font spec does not beginning with '-', assume this
5392 * format:
5393 * "<font name>[:height in pixels[:width in pixels[:weight]]]"
5394 */
5395
5396 if (*lpxstr == '-')
5397 {
5398 int fields, tem;
5399 char name[50], weight[20], slant, pitch, pixels[10], height[10],
5400 width[10], resy[10], remainder[50];
5401 char * encoding;
5402 int dpi = (int) one_w32_display_info.resy;
5403
5404 fields = sscanf (lpxstr,
5405 "-%*[^-]-%49[^-]-%19[^-]-%c-%*[^-]-%*[^-]-%9[^-]-%9[^-]-%*[^-]-%9[^-]-%c-%9[^-]-%49s",
5406 name, weight, &slant, pixels, height, resy, &pitch, width, remainder);
5407 if (fields == EOF)
5408 return (FALSE);
5409
5410 /* In the general case when wildcards cover more than one field,
5411 we don't know which field is which, so don't fill any in.
5412 However, we need to cope with this particular form, which is
5413 generated by font_list_1 (invoked by try_font_list):
5414 "-raster-6x10-*-gb2312*-*"
5415 and make sure to correctly parse the charset field. */
5416 if (fields == 3)
5417 {
5418 fields = sscanf (lpxstr,
5419 "-%*[^-]-%49[^-]-*-%49s",
5420 name, remainder);
5421 }
5422 else if (fields < 9)
5423 {
5424 fields = 0;
5425 remainder[0] = 0;
5426 }
5427
5428 if (fields > 0 && name[0] != '*')
5429 {
5430 int bufsize;
5431 unsigned char *buf;
5432
5433 setup_coding_system
5434 (Fcheck_coding_system (Vlocale_coding_system), &coding);
5435 coding.src_multibyte = 1;
5436 coding.dst_multibyte = 0;
5437 /* Need to set COMPOSITION_DISABLED, otherwise Emacs crashes in
5438 encode_coding_iso2022 trying to dereference a null pointer. */
5439 coding.composing = COMPOSITION_DISABLED;
5440 if (coding.type == coding_type_iso2022)
5441 coding.flags |= CODING_FLAG_ISO_SAFE;
5442 bufsize = encoding_buffer_size (&coding, strlen (name));
5443 buf = (unsigned char *) alloca (bufsize);
5444 coding.mode |= CODING_MODE_LAST_BLOCK;
5445 encode_coding (&coding, name, buf, strlen (name), bufsize);
5446 if (coding.produced >= LF_FACESIZE)
5447 coding.produced = LF_FACESIZE - 1;
5448 buf[coding.produced] = 0;
5449 strcpy (lplogfont->lfFaceName, buf);
5450 }
5451 else
5452 {
5453 lplogfont->lfFaceName[0] = '\0';
5454 }
5455
5456 fields--;
5457
5458 lplogfont->lfWeight = x_to_w32_weight ((fields > 0 ? weight : ""));
5459
5460 fields--;
5461
5462 lplogfont->lfItalic = (fields > 0 && slant == 'i');
5463
5464 fields--;
5465
5466 if (fields > 0 && pixels[0] != '*')
5467 lplogfont->lfHeight = atoi (pixels);
5468
5469 fields--;
5470 fields--;
5471 if (fields > 0 && resy[0] != '*')
5472 {
5473 tem = atoi (resy);
5474 if (tem > 0) dpi = tem;
5475 }
5476
5477 if (fields > -1 && lplogfont->lfHeight == 0 && height[0] != '*')
5478 lplogfont->lfHeight = atoi (height) * dpi / 720;
5479
5480 if (fields > 0)
5481 lplogfont->lfPitchAndFamily =
5482 (fields > 0 && pitch == 'p') ? VARIABLE_PITCH : FIXED_PITCH;
5483
5484 fields--;
5485
5486 if (fields > 0 && width[0] != '*')
5487 lplogfont->lfWidth = atoi (width) / 10;
5488
5489 fields--;
5490
5491 /* Strip the trailing '-' if present. (it shouldn't be, as it
5492 fails the test against xlfd-tight-regexp in fontset.el). */
5493 {
5494 int len = strlen (remainder);
5495 if (len > 0 && remainder[len-1] == '-')
5496 remainder[len-1] = 0;
5497 }
5498 encoding = remainder;
5499 #if 0
5500 if (strncmp (encoding, "*-", 2) == 0)
5501 encoding += 2;
5502 #endif
5503 lplogfont->lfCharSet = x_to_w32_charset (encoding);
5504 }
5505 else
5506 {
5507 int fields;
5508 char name[100], height[10], width[10], weight[20];
5509
5510 fields = sscanf (lpxstr,
5511 "%99[^:]:%9[^:]:%9[^:]:%19s",
5512 name, height, width, weight);
5513
5514 if (fields == EOF) return (FALSE);
5515
5516 if (fields > 0)
5517 {
5518 strncpy (lplogfont->lfFaceName,name, LF_FACESIZE);
5519 lplogfont->lfFaceName[LF_FACESIZE-1] = 0;
5520 }
5521 else
5522 {
5523 lplogfont->lfFaceName[0] = 0;
5524 }
5525
5526 fields--;
5527
5528 if (fields > 0)
5529 lplogfont->lfHeight = atoi (height);
5530
5531 fields--;
5532
5533 if (fields > 0)
5534 lplogfont->lfWidth = atoi (width);
5535
5536 fields--;
5537
5538 lplogfont->lfWeight = x_to_w32_weight ((fields > 0 ? weight : ""));
5539 }
5540
5541 /* This makes TrueType fonts work better. */
5542 lplogfont->lfHeight = - abs (lplogfont->lfHeight);
5543
5544 return (TRUE);
5545 }
5546
5547 /* Strip the pixel height and point height from the given xlfd, and
5548 return the pixel height. If no pixel height is specified, calculate
5549 one from the point height, or if that isn't defined either, return
5550 0 (which usually signifies a scalable font).
5551 */
5552 static int
5553 xlfd_strip_height (char *fontname)
5554 {
5555 int pixel_height, field_number;
5556 char *read_from, *write_to;
5557
5558 xassert (fontname);
5559
5560 pixel_height = field_number = 0;
5561 write_to = NULL;
5562
5563 /* Look for height fields. */
5564 for (read_from = fontname; *read_from; read_from++)
5565 {
5566 if (*read_from == '-')
5567 {
5568 field_number++;
5569 if (field_number == 7) /* Pixel height. */
5570 {
5571 read_from++;
5572 write_to = read_from;
5573
5574 /* Find end of field. */
5575 for (;*read_from && *read_from != '-'; read_from++)
5576 ;
5577
5578 /* Split the fontname at end of field. */
5579 if (*read_from)
5580 {
5581 *read_from = '\0';
5582 read_from++;
5583 }
5584 pixel_height = atoi (write_to);
5585 /* Blank out field. */
5586 if (read_from > write_to)
5587 {
5588 *write_to = '-';
5589 write_to++;
5590 }
5591 /* If the pixel height field is at the end (partial xlfd),
5592 return now. */
5593 else
5594 return pixel_height;
5595
5596 /* If we got a pixel height, the point height can be
5597 ignored. Just blank it out and break now. */
5598 if (pixel_height)
5599 {
5600 /* Find end of point size field. */
5601 for (; *read_from && *read_from != '-'; read_from++)
5602 ;
5603
5604 if (*read_from)
5605 read_from++;
5606
5607 /* Blank out the point size field. */
5608 if (read_from > write_to)
5609 {
5610 *write_to = '-';
5611 write_to++;
5612 }
5613 else
5614 return pixel_height;
5615
5616 break;
5617 }
5618 /* If the point height is already blank, break now. */
5619 if (*read_from == '-')
5620 {
5621 read_from++;
5622 break;
5623 }
5624 }
5625 else if (field_number == 8)
5626 {
5627 /* If we didn't get a pixel height, try to get the point
5628 height and convert that. */
5629 int point_size;
5630 char *point_size_start = read_from++;
5631
5632 /* Find end of field. */
5633 for (; *read_from && *read_from != '-'; read_from++)
5634 ;
5635
5636 if (*read_from)
5637 {
5638 *read_from = '\0';
5639 read_from++;
5640 }
5641
5642 point_size = atoi (point_size_start);
5643
5644 /* Convert to pixel height. */
5645 pixel_height = point_size
5646 * one_w32_display_info.height_in / 720;
5647
5648 /* Blank out this field and break. */
5649 *write_to = '-';
5650 write_to++;
5651 break;
5652 }
5653 }
5654 }
5655
5656 /* Shift the rest of the font spec into place. */
5657 if (write_to && read_from > write_to)
5658 {
5659 for (; *read_from; read_from++, write_to++)
5660 *write_to = *read_from;
5661 *write_to = '\0';
5662 }
5663
5664 return pixel_height;
5665 }
5666
5667 /* Assume parameter 1 is fully qualified, no wildcards. */
5668 static BOOL
5669 w32_font_match (fontname, pattern)
5670 char * fontname;
5671 char * pattern;
5672 {
5673 char *ptr;
5674 char *font_name_copy;
5675 char *regex = alloca (strlen (pattern) * 2 + 3);
5676
5677 font_name_copy = alloca (strlen (fontname) + 1);
5678 strcpy (font_name_copy, fontname);
5679
5680 ptr = regex;
5681 *ptr++ = '^';
5682
5683 /* Turn pattern into a regexp and do a regexp match. */
5684 for (; *pattern; pattern++)
5685 {
5686 if (*pattern == '?')
5687 *ptr++ = '.';
5688 else if (*pattern == '*')
5689 {
5690 *ptr++ = '.';
5691 *ptr++ = '*';
5692 }
5693 else
5694 *ptr++ = *pattern;
5695 }
5696 *ptr = '$';
5697 *(ptr + 1) = '\0';
5698
5699 /* Strip out font heights and compare them seperately, since
5700 rounding error can cause mismatches. This also allows a
5701 comparison between a font that declares only a pixel height and a
5702 pattern that declares the point height.
5703 */
5704 {
5705 int font_height, pattern_height;
5706
5707 font_height = xlfd_strip_height (font_name_copy);
5708 pattern_height = xlfd_strip_height (regex);
5709
5710 /* Compare now, and don't bother doing expensive regexp matching
5711 if the heights differ. */
5712 if (font_height && pattern_height && (font_height != pattern_height))
5713 return FALSE;
5714 }
5715
5716 return (fast_string_match_ignore_case (build_string (regex),
5717 build_string(font_name_copy)) >= 0);
5718 }
5719
5720 /* Callback functions, and a structure holding info they need, for
5721 listing system fonts on W32. We need one set of functions to do the
5722 job properly, but these don't work on NT 3.51 and earlier, so we
5723 have a second set which don't handle character sets properly to
5724 fall back on.
5725
5726 In both cases, there are two passes made. The first pass gets one
5727 font from each family, the second pass lists all the fonts from
5728 each family. */
5729
5730 typedef struct enumfont_t
5731 {
5732 HDC hdc;
5733 int numFonts;
5734 LOGFONT logfont;
5735 XFontStruct *size_ref;
5736 Lisp_Object pattern;
5737 Lisp_Object list;
5738 } enumfont_t;
5739
5740
5741 static void
5742 enum_font_maybe_add_to_list (enumfont_t *, LOGFONT *, char *, Lisp_Object);
5743
5744
5745 static int CALLBACK
5746 enum_font_cb2 (lplf, lptm, FontType, lpef)
5747 ENUMLOGFONT * lplf;
5748 NEWTEXTMETRIC * lptm;
5749 int FontType;
5750 enumfont_t * lpef;
5751 {
5752 /* Ignore struck out and underlined versions of fonts. */
5753 if (lplf->elfLogFont.lfStrikeOut || lplf->elfLogFont.lfUnderline)
5754 return 1;
5755
5756 /* Only return fonts with names starting with @ if they were
5757 explicitly specified, since Microsoft uses an initial @ to
5758 denote fonts for vertical writing, without providing a more
5759 convenient way of identifying them. */
5760 if (lplf->elfLogFont.lfFaceName[0] == '@'
5761 && lpef->logfont.lfFaceName[0] != '@')
5762 return 1;
5763
5764 /* Check that the character set matches if it was specified */
5765 if (lpef->logfont.lfCharSet != DEFAULT_CHARSET &&
5766 lplf->elfLogFont.lfCharSet != lpef->logfont.lfCharSet)
5767 return 1;
5768
5769 if (FontType == RASTER_FONTTYPE)
5770 {
5771 /* DBCS raster fonts have problems displaying, so skip them. */
5772 int charset = lplf->elfLogFont.lfCharSet;
5773 if (charset == SHIFTJIS_CHARSET
5774 || charset == HANGEUL_CHARSET
5775 || charset == CHINESEBIG5_CHARSET
5776 || charset == GB2312_CHARSET
5777 #ifdef JOHAB_CHARSET
5778 || charset == JOHAB_CHARSET
5779 #endif
5780 )
5781 return 1;
5782 }
5783
5784 {
5785 char buf[100];
5786 Lisp_Object width = Qnil;
5787 Lisp_Object charset_list = Qnil;
5788 char *charset = NULL;
5789
5790 /* Truetype fonts do not report their true metrics until loaded */
5791 if (FontType != RASTER_FONTTYPE)
5792 {
5793 if (!NILP (lpef->pattern))
5794 {
5795 /* Scalable fonts are as big as you want them to be. */
5796 lplf->elfLogFont.lfHeight = lpef->logfont.lfHeight;
5797 lplf->elfLogFont.lfWidth = lpef->logfont.lfWidth;
5798 width = make_number (lpef->logfont.lfWidth);
5799 }
5800 else
5801 {
5802 lplf->elfLogFont.lfHeight = 0;
5803 lplf->elfLogFont.lfWidth = 0;
5804 }
5805 }
5806
5807 /* Make sure the height used here is the same as everywhere
5808 else (ie character height, not cell height). */
5809 if (lplf->elfLogFont.lfHeight > 0)
5810 {
5811 /* lptm can be trusted for RASTER fonts, but not scalable ones. */
5812 if (FontType == RASTER_FONTTYPE)
5813 lplf->elfLogFont.lfHeight = lptm->tmInternalLeading - lptm->tmHeight;
5814 else
5815 lplf->elfLogFont.lfHeight = -lplf->elfLogFont.lfHeight;
5816 }
5817
5818 if (!NILP (lpef->pattern))
5819 {
5820 charset = xlfd_charset_of_font (SDATA (lpef->pattern));
5821
5822 /* We already checked charsets above, but DEFAULT_CHARSET
5823 slipped through. So only allow exact matches for DEFAULT_CHARSET. */
5824 if (charset
5825 && strncmp (charset, "*-*", 3) != 0
5826 && lpef->logfont.lfCharSet == DEFAULT_CHARSET
5827 && strcmp (charset, w32_to_x_charset (DEFAULT_CHARSET)) != 0)
5828 return 1;
5829 }
5830
5831 if (charset)
5832 charset_list = Fcons (build_string (charset), Qnil);
5833 else
5834 charset_list = w32_to_all_x_charsets (lplf->elfLogFont.lfCharSet);
5835
5836 /* Loop through the charsets. */
5837 for ( ; CONSP (charset_list); charset_list = Fcdr (charset_list))
5838 {
5839 Lisp_Object this_charset = Fcar (charset_list);
5840 charset = SDATA (this_charset);
5841
5842 /* List bold and italic variations if w32-enable-synthesized-fonts
5843 is non-nil and this is a plain font. */
5844 if (w32_enable_synthesized_fonts
5845 && lplf->elfLogFont.lfWeight == FW_NORMAL
5846 && lplf->elfLogFont.lfItalic == FALSE)
5847 {
5848 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5849 charset, width);
5850 /* bold. */
5851 lplf->elfLogFont.lfWeight = FW_BOLD;
5852 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5853 charset, width);
5854 /* bold italic. */
5855 lplf->elfLogFont.lfItalic = TRUE;
5856 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5857 charset, width);
5858 /* italic. */
5859 lplf->elfLogFont.lfWeight = FW_NORMAL;
5860 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5861 charset, width);
5862 }
5863 else
5864 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5865 charset, width);
5866 }
5867 }
5868
5869 return 1;
5870 }
5871
5872 static void
5873 enum_font_maybe_add_to_list (lpef, logfont, match_charset, width)
5874 enumfont_t * lpef;
5875 LOGFONT * logfont;
5876 char * match_charset;
5877 Lisp_Object width;
5878 {
5879 char buf[100];
5880
5881 if (!w32_to_x_font (logfont, buf, 100, match_charset))
5882 return;
5883
5884 if (NILP (lpef->pattern)
5885 || w32_font_match (buf, SDATA (lpef->pattern)))
5886 {
5887 /* Check if we already listed this font. This may happen if
5888 w32_enable_synthesized_fonts is non-nil, and there are real
5889 bold and italic versions of the font. */
5890 Lisp_Object font_name = build_string (buf);
5891 if (NILP (Fmember (font_name, lpef->list)))
5892 {
5893 Lisp_Object entry = Fcons (font_name, width);
5894 lpef->list = Fcons (entry, lpef->list);
5895 lpef->numFonts++;
5896 }
5897 }
5898 }
5899
5900
5901 static int CALLBACK
5902 enum_font_cb1 (lplf, lptm, FontType, lpef)
5903 ENUMLOGFONT * lplf;
5904 NEWTEXTMETRIC * lptm;
5905 int FontType;
5906 enumfont_t * lpef;
5907 {
5908 return EnumFontFamilies (lpef->hdc,
5909 lplf->elfLogFont.lfFaceName,
5910 (FONTENUMPROC) enum_font_cb2,
5911 (LPARAM) lpef);
5912 }
5913
5914
5915 static int CALLBACK
5916 enum_fontex_cb2 (lplf, lptm, font_type, lpef)
5917 ENUMLOGFONTEX * lplf;
5918 NEWTEXTMETRICEX * lptm;
5919 int font_type;
5920 enumfont_t * lpef;
5921 {
5922 /* We are not interested in the extra info we get back from the 'Ex
5923 version - only the fact that we get character set variations
5924 enumerated seperately. */
5925 return enum_font_cb2 ((ENUMLOGFONT *) lplf, (NEWTEXTMETRIC *) lptm,
5926 font_type, lpef);
5927 }
5928
5929 static int CALLBACK
5930 enum_fontex_cb1 (lplf, lptm, font_type, lpef)
5931 ENUMLOGFONTEX * lplf;
5932 NEWTEXTMETRICEX * lptm;
5933 int font_type;
5934 enumfont_t * lpef;
5935 {
5936 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
5937 FARPROC enum_font_families_ex
5938 = GetProcAddress ( gdi32, "EnumFontFamiliesExA");
5939 /* We don't really expect EnumFontFamiliesEx to disappear once we
5940 get here, so don't bother handling it gracefully. */
5941 if (enum_font_families_ex == NULL)
5942 error ("gdi32.dll has disappeared!");
5943 return enum_font_families_ex (lpef->hdc,
5944 &lplf->elfLogFont,
5945 (FONTENUMPROC) enum_fontex_cb2,
5946 (LPARAM) lpef, 0);
5947 }
5948
5949 /* Interface to fontset handler. (adapted from mw32font.c in Meadow
5950 and xterm.c in Emacs 20.3) */
5951
5952 static Lisp_Object w32_list_bdf_fonts (Lisp_Object pattern, int max_names)
5953 {
5954 char *fontname, *ptnstr;
5955 Lisp_Object list, tem, newlist = Qnil;
5956 int n_fonts = 0;
5957
5958 list = Vw32_bdf_filename_alist;
5959 ptnstr = SDATA (pattern);
5960
5961 for ( ; CONSP (list); list = XCDR (list))
5962 {
5963 tem = XCAR (list);
5964 if (CONSP (tem))
5965 fontname = SDATA (XCAR (tem));
5966 else if (STRINGP (tem))
5967 fontname = SDATA (tem);
5968 else
5969 continue;
5970
5971 if (w32_font_match (fontname, ptnstr))
5972 {
5973 newlist = Fcons (XCAR (tem), newlist);
5974 n_fonts++;
5975 if (max_names >= 0 && n_fonts >= max_names)
5976 break;
5977 }
5978 }
5979
5980 return newlist;
5981 }
5982
5983
5984 /* Return a list of names of available fonts matching PATTERN on frame
5985 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
5986 to be listed. Frame F NULL means we have not yet created any
5987 frame, which means we can't get proper size info, as we don't have
5988 a device context to use for GetTextMetrics.
5989 MAXNAMES sets a limit on how many fonts to match. If MAXNAMES is
5990 negative, then all matching fonts are returned. */
5991
5992 Lisp_Object
5993 w32_list_fonts (f, pattern, size, maxnames)
5994 struct frame *f;
5995 Lisp_Object pattern;
5996 int size;
5997 int maxnames;
5998 {
5999 Lisp_Object patterns, key = Qnil, tem, tpat;
6000 Lisp_Object list = Qnil, newlist = Qnil, second_best = Qnil;
6001 struct w32_display_info *dpyinfo = &one_w32_display_info;
6002 int n_fonts = 0;
6003
6004 patterns = Fassoc (pattern, Valternate_fontname_alist);
6005 if (NILP (patterns))
6006 patterns = Fcons (pattern, Qnil);
6007
6008 for (; CONSP (patterns); patterns = XCDR (patterns))
6009 {
6010 enumfont_t ef;
6011 int codepage;
6012
6013 tpat = XCAR (patterns);
6014
6015 if (!STRINGP (tpat))
6016 continue;
6017
6018 /* Avoid expensive EnumFontFamilies functions if we are not
6019 going to be able to output one of these anyway. */
6020 codepage = w32_codepage_for_font (SDATA (tpat));
6021 if (codepage != CP_8BIT && codepage != CP_UNICODE
6022 && codepage != CP_DEFAULT && codepage != CP_UNKNOWN
6023 && !IsValidCodePage(codepage))
6024 continue;
6025
6026 /* See if we cached the result for this particular query.
6027 The cache is an alist of the form:
6028 ((PATTERN (FONTNAME . WIDTH) ...) ...)
6029 */
6030 if (tem = XCDR (dpyinfo->name_list_element),
6031 !NILP (list = Fassoc (tpat, tem)))
6032 {
6033 list = Fcdr_safe (list);
6034 /* We have a cached list. Don't have to get the list again. */
6035 goto label_cached;
6036 }
6037
6038 BLOCK_INPUT;
6039 /* At first, put PATTERN in the cache. */
6040 ef.pattern = tpat;
6041 ef.list = Qnil;
6042 ef.numFonts = 0;
6043
6044 /* Use EnumFontFamiliesEx where it is available, as it knows
6045 about character sets. Fall back to EnumFontFamilies for
6046 older versions of NT that don't support the 'Ex function. */
6047 x_to_w32_font (SDATA (tpat), &ef.logfont);
6048 {
6049 LOGFONT font_match_pattern;
6050 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
6051 FARPROC enum_font_families_ex
6052 = GetProcAddress ( gdi32, "EnumFontFamiliesExA");
6053
6054 /* We do our own pattern matching so we can handle wildcards. */
6055 font_match_pattern.lfFaceName[0] = 0;
6056 font_match_pattern.lfPitchAndFamily = 0;
6057 /* We can use the charset, because if it is a wildcard it will
6058 be DEFAULT_CHARSET anyway. */
6059 font_match_pattern.lfCharSet = ef.logfont.lfCharSet;
6060
6061 ef.hdc = GetDC (dpyinfo->root_window);
6062
6063 if (enum_font_families_ex)
6064 enum_font_families_ex (ef.hdc,
6065 &font_match_pattern,
6066 (FONTENUMPROC) enum_fontex_cb1,
6067 (LPARAM) &ef, 0);
6068 else
6069 EnumFontFamilies (ef.hdc, NULL, (FONTENUMPROC) enum_font_cb1,
6070 (LPARAM)&ef);
6071
6072 ReleaseDC (dpyinfo->root_window, ef.hdc);
6073 }
6074
6075 UNBLOCK_INPUT;
6076 list = ef.list;
6077
6078 /* Make a list of the fonts we got back.
6079 Store that in the font cache for the display. */
6080 XSETCDR (dpyinfo->name_list_element,
6081 Fcons (Fcons (tpat, list),
6082 XCDR (dpyinfo->name_list_element)));
6083
6084 label_cached:
6085 if (NILP (list)) continue; /* Try the remaining alternatives. */
6086
6087 newlist = second_best = Qnil;
6088
6089 /* Make a list of the fonts that have the right width. */
6090 for (; CONSP (list); list = XCDR (list))
6091 {
6092 int found_size;
6093 tem = XCAR (list);
6094
6095 if (!CONSP (tem))
6096 continue;
6097 if (NILP (XCAR (tem)))
6098 continue;
6099 if (!size)
6100 {
6101 newlist = Fcons (XCAR (tem), newlist);
6102 n_fonts++;
6103 if (maxnames >= 0 && n_fonts >= maxnames)
6104 break;
6105 else
6106 continue;
6107 }
6108 if (!INTEGERP (XCDR (tem)))
6109 {
6110 /* Since we don't yet know the size of the font, we must
6111 load it and try GetTextMetrics. */
6112 W32FontStruct thisinfo;
6113 LOGFONT lf;
6114 HDC hdc;
6115 HANDLE oldobj;
6116
6117 if (!x_to_w32_font (SDATA (XCAR (tem)), &lf))
6118 continue;
6119
6120 BLOCK_INPUT;
6121 thisinfo.bdf = NULL;
6122 thisinfo.hfont = CreateFontIndirect (&lf);
6123 if (thisinfo.hfont == NULL)
6124 continue;
6125
6126 hdc = GetDC (dpyinfo->root_window);
6127 oldobj = SelectObject (hdc, thisinfo.hfont);
6128 if (GetTextMetrics (hdc, &thisinfo.tm))
6129 XSETCDR (tem, make_number (FONT_WIDTH (&thisinfo)));
6130 else
6131 XSETCDR (tem, make_number (0));
6132 SelectObject (hdc, oldobj);
6133 ReleaseDC (dpyinfo->root_window, hdc);
6134 DeleteObject(thisinfo.hfont);
6135 UNBLOCK_INPUT;
6136 }
6137 found_size = XINT (XCDR (tem));
6138 if (found_size == size)
6139 {
6140 newlist = Fcons (XCAR (tem), newlist);
6141 n_fonts++;
6142 if (maxnames >= 0 && n_fonts >= maxnames)
6143 break;
6144 }
6145 /* keep track of the closest matching size in case
6146 no exact match is found. */
6147 else if (found_size > 0)
6148 {
6149 if (NILP (second_best))
6150 second_best = tem;
6151
6152 else if (found_size < size)
6153 {
6154 if (XINT (XCDR (second_best)) > size
6155 || XINT (XCDR (second_best)) < found_size)
6156 second_best = tem;
6157 }
6158 else
6159 {
6160 if (XINT (XCDR (second_best)) > size
6161 && XINT (XCDR (second_best)) >
6162 found_size)
6163 second_best = tem;
6164 }
6165 }
6166 }
6167
6168 if (!NILP (newlist))
6169 break;
6170 else if (!NILP (second_best))
6171 {
6172 newlist = Fcons (XCAR (second_best), Qnil);
6173 break;
6174 }
6175 }
6176
6177 /* Include any bdf fonts. */
6178 if (n_fonts < maxnames || maxnames < 0)
6179 {
6180 Lisp_Object combined[2];
6181 combined[0] = w32_list_bdf_fonts (pattern, maxnames - n_fonts);
6182 combined[1] = newlist;
6183 newlist = Fnconc(2, combined);
6184 }
6185
6186 return newlist;
6187 }
6188
6189
6190 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
6191 struct font_info *
6192 w32_get_font_info (f, font_idx)
6193 FRAME_PTR f;
6194 int font_idx;
6195 {
6196 return (FRAME_W32_FONT_TABLE (f) + font_idx);
6197 }
6198
6199
6200 struct font_info*
6201 w32_query_font (struct frame *f, char *fontname)
6202 {
6203 int i;
6204 struct font_info *pfi;
6205
6206 pfi = FRAME_W32_FONT_TABLE (f);
6207
6208 for (i = 0; i < one_w32_display_info.n_fonts ;i++, pfi++)
6209 {
6210 if (strcmp(pfi->name, fontname) == 0) return pfi;
6211 }
6212
6213 return NULL;
6214 }
6215
6216 /* Find a CCL program for a font specified by FONTP, and set the member
6217 `encoder' of the structure. */
6218
6219 void
6220 w32_find_ccl_program (fontp)
6221 struct font_info *fontp;
6222 {
6223 Lisp_Object list, elt;
6224
6225 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
6226 {
6227 elt = XCAR (list);
6228 if (CONSP (elt)
6229 && STRINGP (XCAR (elt))
6230 && (fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
6231 >= 0))
6232 break;
6233 }
6234 if (! NILP (list))
6235 {
6236 struct ccl_program *ccl
6237 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
6238
6239 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
6240 xfree (ccl);
6241 else
6242 fontp->font_encoder = ccl;
6243 }
6244 }
6245
6246 /* directory-files from dired.c. */
6247 Lisp_Object Fdirectory_files P_((Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object));
6248
6249 \f
6250 /* Find BDF files in a specified directory. (use GCPRO when calling,
6251 as this calls lisp to get a directory listing). */
6252 static Lisp_Object
6253 w32_find_bdf_fonts_in_dir (Lisp_Object directory)
6254 {
6255 Lisp_Object filelist, list = Qnil;
6256 char fontname[100];
6257
6258 if (!STRINGP(directory))
6259 return Qnil;
6260
6261 filelist = Fdirectory_files (directory, Qt,
6262 build_string (".*\\.[bB][dD][fF]"), Qt);
6263
6264 for ( ; CONSP(filelist); filelist = XCDR (filelist))
6265 {
6266 Lisp_Object filename = XCAR (filelist);
6267 if (w32_BDF_to_x_font (SDATA (filename), fontname, 100))
6268 store_in_alist (&list, build_string (fontname), filename);
6269 }
6270 return list;
6271 }
6272
6273 DEFUN ("w32-find-bdf-fonts", Fw32_find_bdf_fonts, Sw32_find_bdf_fonts,
6274 1, 1, 0,
6275 doc: /* Return a list of BDF fonts in DIRECTORY.
6276 The list is suitable for appending to `w32-bdf-filename-alist'.
6277 Fonts which do not contain an xlfd description will not be included
6278 in the list. DIRECTORY may be a list of directories. */)
6279 (directory)
6280 Lisp_Object directory;
6281 {
6282 Lisp_Object list = Qnil;
6283 struct gcpro gcpro1, gcpro2;
6284
6285 if (!CONSP (directory))
6286 return w32_find_bdf_fonts_in_dir (directory);
6287
6288 for ( ; CONSP (directory); directory = XCDR (directory))
6289 {
6290 Lisp_Object pair[2];
6291 pair[0] = list;
6292 pair[1] = Qnil;
6293 GCPRO2 (directory, list);
6294 pair[1] = w32_find_bdf_fonts_in_dir( XCAR (directory) );
6295 list = Fnconc( 2, pair );
6296 UNGCPRO;
6297 }
6298 return list;
6299 }
6300
6301 \f
6302 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
6303 doc: /* Internal function called by `color-defined-p', which see. */)
6304 (color, frame)
6305 Lisp_Object color, frame;
6306 {
6307 XColor foo;
6308 FRAME_PTR f = check_x_frame (frame);
6309
6310 CHECK_STRING (color);
6311
6312 if (w32_defined_color (f, SDATA (color), &foo, 0))
6313 return Qt;
6314 else
6315 return Qnil;
6316 }
6317
6318 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
6319 doc: /* Internal function called by `color-values', which see. */)
6320 (color, frame)
6321 Lisp_Object color, frame;
6322 {
6323 XColor foo;
6324 FRAME_PTR f = check_x_frame (frame);
6325
6326 CHECK_STRING (color);
6327
6328 if (w32_defined_color (f, SDATA (color), &foo, 0))
6329 {
6330 Lisp_Object rgb[3];
6331
6332 rgb[0] = make_number ((GetRValue (foo.pixel) << 8)
6333 | GetRValue (foo.pixel));
6334 rgb[1] = make_number ((GetGValue (foo.pixel) << 8)
6335 | GetGValue (foo.pixel));
6336 rgb[2] = make_number ((GetBValue (foo.pixel) << 8)
6337 | GetBValue (foo.pixel));
6338 return Flist (3, rgb);
6339 }
6340 else
6341 return Qnil;
6342 }
6343
6344 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
6345 doc: /* Internal function called by `display-color-p', which see. */)
6346 (display)
6347 Lisp_Object display;
6348 {
6349 struct w32_display_info *dpyinfo = check_x_display_info (display);
6350
6351 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 2)
6352 return Qnil;
6353
6354 return Qt;
6355 }
6356
6357 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p,
6358 Sx_display_grayscale_p, 0, 1, 0,
6359 doc: /* Return t if DISPLAY supports shades of gray.
6360 Note that color displays do support shades of gray.
6361 The optional argument DISPLAY specifies which display to ask about.
6362 DISPLAY should be either a frame or a display name (a string).
6363 If omitted or nil, that stands for the selected frame's display. */)
6364 (display)
6365 Lisp_Object display;
6366 {
6367 struct w32_display_info *dpyinfo = check_x_display_info (display);
6368
6369 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 1)
6370 return Qnil;
6371
6372 return Qt;
6373 }
6374
6375 DEFUN ("x-display-pixel-width", Fx_display_pixel_width,
6376 Sx_display_pixel_width, 0, 1, 0,
6377 doc: /* Returns the width in pixels of DISPLAY.
6378 The optional argument DISPLAY specifies which display to ask about.
6379 DISPLAY should be either a frame or a display name (a string).
6380 If omitted or nil, that stands for the selected frame's display. */)
6381 (display)
6382 Lisp_Object display;
6383 {
6384 struct w32_display_info *dpyinfo = check_x_display_info (display);
6385
6386 return make_number (dpyinfo->width);
6387 }
6388
6389 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
6390 Sx_display_pixel_height, 0, 1, 0,
6391 doc: /* Returns the height in pixels of DISPLAY.
6392 The optional argument DISPLAY specifies which display to ask about.
6393 DISPLAY should be either a frame or a display name (a string).
6394 If omitted or nil, that stands for the selected frame's display. */)
6395 (display)
6396 Lisp_Object display;
6397 {
6398 struct w32_display_info *dpyinfo = check_x_display_info (display);
6399
6400 return make_number (dpyinfo->height);
6401 }
6402
6403 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
6404 0, 1, 0,
6405 doc: /* Returns the number of bitplanes of DISPLAY.
6406 The optional argument DISPLAY specifies which display to ask about.
6407 DISPLAY should be either a frame or a display name (a string).
6408 If omitted or nil, that stands for the selected frame's display. */)
6409 (display)
6410 Lisp_Object display;
6411 {
6412 struct w32_display_info *dpyinfo = check_x_display_info (display);
6413
6414 return make_number (dpyinfo->n_planes * dpyinfo->n_cbits);
6415 }
6416
6417 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
6418 0, 1, 0,
6419 doc: /* Returns the number of color cells of DISPLAY.
6420 The optional argument DISPLAY specifies which display to ask about.
6421 DISPLAY should be either a frame or a display name (a string).
6422 If omitted or nil, that stands for the selected frame's display. */)
6423 (display)
6424 Lisp_Object display;
6425 {
6426 struct w32_display_info *dpyinfo = check_x_display_info (display);
6427 HDC hdc;
6428 int cap;
6429
6430 hdc = GetDC (dpyinfo->root_window);
6431 if (dpyinfo->has_palette)
6432 cap = GetDeviceCaps (hdc, SIZEPALETTE);
6433 else
6434 cap = GetDeviceCaps (hdc, NUMCOLORS);
6435
6436 /* We force 24+ bit depths to 24-bit, both to prevent an overflow
6437 and because probably is more meaningful on Windows anyway */
6438 if (cap < 0)
6439 cap = 1 << min(dpyinfo->n_planes * dpyinfo->n_cbits, 24);
6440
6441 ReleaseDC (dpyinfo->root_window, hdc);
6442
6443 return make_number (cap);
6444 }
6445
6446 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
6447 Sx_server_max_request_size,
6448 0, 1, 0,
6449 doc: /* Returns the maximum request size of the server of DISPLAY.
6450 The optional argument DISPLAY specifies which display to ask about.
6451 DISPLAY should be either a frame or a display name (a string).
6452 If omitted or nil, that stands for the selected frame's display. */)
6453 (display)
6454 Lisp_Object display;
6455 {
6456 struct w32_display_info *dpyinfo = check_x_display_info (display);
6457
6458 return make_number (1);
6459 }
6460
6461 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
6462 doc: /* Returns the "vendor ID" string of the W32 system (Microsoft).
6463 The optional argument DISPLAY specifies which display to ask about.
6464 DISPLAY should be either a frame or a display name (a string).
6465 If omitted or nil, that stands for the selected frame's display. */)
6466 (display)
6467 Lisp_Object display;
6468 {
6469 return build_string ("Microsoft Corp.");
6470 }
6471
6472 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
6473 doc: /* Returns the version numbers of the server of DISPLAY.
6474 The value is a list of three integers: the major and minor
6475 version numbers of the X Protocol in use, and the distributor-specific release
6476 number. See also the function `x-server-vendor'.
6477
6478 The optional argument DISPLAY specifies which display to ask about.
6479 DISPLAY should be either a frame or a display name (a string).
6480 If omitted or nil, that stands for the selected frame's display. */)
6481 (display)
6482 Lisp_Object display;
6483 {
6484 return Fcons (make_number (w32_major_version),
6485 Fcons (make_number (w32_minor_version),
6486 Fcons (make_number (w32_build_number), Qnil)));
6487 }
6488
6489 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
6490 doc: /* Returns the number of screens on the server of DISPLAY.
6491 The optional argument DISPLAY specifies which display to ask about.
6492 DISPLAY should be either a frame or a display name (a string).
6493 If omitted or nil, that stands for the selected frame's display. */)
6494 (display)
6495 Lisp_Object display;
6496 {
6497 return make_number (1);
6498 }
6499
6500 DEFUN ("x-display-mm-height", Fx_display_mm_height,
6501 Sx_display_mm_height, 0, 1, 0,
6502 doc: /* Returns the height in millimeters of DISPLAY.
6503 The optional argument DISPLAY specifies which display to ask about.
6504 DISPLAY should be either a frame or a display name (a string).
6505 If omitted or nil, that stands for the selected frame's display. */)
6506 (display)
6507 Lisp_Object display;
6508 {
6509 struct w32_display_info *dpyinfo = check_x_display_info (display);
6510 HDC hdc;
6511 int cap;
6512
6513 hdc = GetDC (dpyinfo->root_window);
6514
6515 cap = GetDeviceCaps (hdc, VERTSIZE);
6516
6517 ReleaseDC (dpyinfo->root_window, hdc);
6518
6519 return make_number (cap);
6520 }
6521
6522 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
6523 doc: /* Returns the width in millimeters of DISPLAY.
6524 The optional argument DISPLAY specifies which display to ask about.
6525 DISPLAY should be either a frame or a display name (a string).
6526 If omitted or nil, that stands for the selected frame's display. */)
6527 (display)
6528 Lisp_Object display;
6529 {
6530 struct w32_display_info *dpyinfo = check_x_display_info (display);
6531
6532 HDC hdc;
6533 int cap;
6534
6535 hdc = GetDC (dpyinfo->root_window);
6536
6537 cap = GetDeviceCaps (hdc, HORZSIZE);
6538
6539 ReleaseDC (dpyinfo->root_window, hdc);
6540
6541 return make_number (cap);
6542 }
6543
6544 DEFUN ("x-display-backing-store", Fx_display_backing_store,
6545 Sx_display_backing_store, 0, 1, 0,
6546 doc: /* Returns an indication of whether DISPLAY does backing store.
6547 The value may be `always', `when-mapped', or `not-useful'.
6548 The optional argument DISPLAY specifies which display to ask about.
6549 DISPLAY should be either a frame or a display name (a string).
6550 If omitted or nil, that stands for the selected frame's display. */)
6551 (display)
6552 Lisp_Object display;
6553 {
6554 return intern ("not-useful");
6555 }
6556
6557 DEFUN ("x-display-visual-class", Fx_display_visual_class,
6558 Sx_display_visual_class, 0, 1, 0,
6559 doc: /* Returns the visual class of DISPLAY.
6560 The value is one of the symbols `static-gray', `gray-scale',
6561 `static-color', `pseudo-color', `true-color', or `direct-color'.
6562
6563 The optional argument DISPLAY specifies which display to ask about.
6564 DISPLAY should be either a frame or a display name (a string).
6565 If omitted or nil, that stands for the selected frame's display. */)
6566 (display)
6567 Lisp_Object display;
6568 {
6569 struct w32_display_info *dpyinfo = check_x_display_info (display);
6570 Lisp_Object result = Qnil;
6571
6572 if (dpyinfo->has_palette)
6573 result = intern ("pseudo-color");
6574 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 1)
6575 result = intern ("static-grey");
6576 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 4)
6577 result = intern ("static-color");
6578 else if (dpyinfo->n_planes * dpyinfo->n_cbits > 8)
6579 result = intern ("true-color");
6580
6581 return result;
6582 }
6583
6584 DEFUN ("x-display-save-under", Fx_display_save_under,
6585 Sx_display_save_under, 0, 1, 0,
6586 doc: /* Returns t if DISPLAY supports the save-under feature.
6587 The optional argument DISPLAY specifies which display to ask about.
6588 DISPLAY should be either a frame or a display name (a string).
6589 If omitted or nil, that stands for the selected frame's display. */)
6590 (display)
6591 Lisp_Object display;
6592 {
6593 return Qnil;
6594 }
6595 \f
6596 int
6597 x_pixel_width (f)
6598 register struct frame *f;
6599 {
6600 return FRAME_PIXEL_WIDTH (f);
6601 }
6602
6603 int
6604 x_pixel_height (f)
6605 register struct frame *f;
6606 {
6607 return FRAME_PIXEL_HEIGHT (f);
6608 }
6609
6610 int
6611 x_char_width (f)
6612 register struct frame *f;
6613 {
6614 return FRAME_COLUMN_WIDTH (f);
6615 }
6616
6617 int
6618 x_char_height (f)
6619 register struct frame *f;
6620 {
6621 return FRAME_LINE_HEIGHT (f);
6622 }
6623
6624 int
6625 x_screen_planes (f)
6626 register struct frame *f;
6627 {
6628 return FRAME_W32_DISPLAY_INFO (f)->n_planes;
6629 }
6630 \f
6631 /* Return the display structure for the display named NAME.
6632 Open a new connection if necessary. */
6633
6634 struct w32_display_info *
6635 x_display_info_for_name (name)
6636 Lisp_Object name;
6637 {
6638 Lisp_Object names;
6639 struct w32_display_info *dpyinfo;
6640
6641 CHECK_STRING (name);
6642
6643 for (dpyinfo = &one_w32_display_info, names = w32_display_name_list;
6644 dpyinfo;
6645 dpyinfo = dpyinfo->next, names = XCDR (names))
6646 {
6647 Lisp_Object tem;
6648 tem = Fstring_equal (XCAR (XCAR (names)), name);
6649 if (!NILP (tem))
6650 return dpyinfo;
6651 }
6652
6653 /* Use this general default value to start with. */
6654 Vx_resource_name = Vinvocation_name;
6655
6656 validate_x_resource_name ();
6657
6658 dpyinfo = w32_term_init (name, (unsigned char *)0,
6659 (char *) SDATA (Vx_resource_name));
6660
6661 if (dpyinfo == 0)
6662 error ("Cannot connect to server %s", SDATA (name));
6663
6664 w32_in_use = 1;
6665 XSETFASTINT (Vwindow_system_version, 3);
6666
6667 return dpyinfo;
6668 }
6669
6670 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
6671 1, 3, 0, doc: /* Open a connection to a server.
6672 DISPLAY is the name of the display to connect to.
6673 Optional second arg XRM-STRING is a string of resources in xrdb format.
6674 If the optional third arg MUST-SUCCEED is non-nil,
6675 terminate Emacs if we can't open the connection. */)
6676 (display, xrm_string, must_succeed)
6677 Lisp_Object display, xrm_string, must_succeed;
6678 {
6679 unsigned char *xrm_option;
6680 struct w32_display_info *dpyinfo;
6681
6682 /* If initialization has already been done, return now to avoid
6683 overwriting critical parts of one_w32_display_info. */
6684 if (w32_in_use)
6685 return Qnil;
6686
6687 CHECK_STRING (display);
6688 if (! NILP (xrm_string))
6689 CHECK_STRING (xrm_string);
6690
6691 if (! EQ (Vwindow_system, intern ("w32")))
6692 error ("Not using Microsoft Windows");
6693
6694 /* Allow color mapping to be defined externally; first look in user's
6695 HOME directory, then in Emacs etc dir for a file called rgb.txt. */
6696 {
6697 Lisp_Object color_file;
6698 struct gcpro gcpro1;
6699
6700 color_file = build_string("~/rgb.txt");
6701
6702 GCPRO1 (color_file);
6703
6704 if (NILP (Ffile_readable_p (color_file)))
6705 color_file =
6706 Fexpand_file_name (build_string ("rgb.txt"),
6707 Fsymbol_value (intern ("data-directory")));
6708
6709 Vw32_color_map = Fw32_load_color_file (color_file);
6710
6711 UNGCPRO;
6712 }
6713 if (NILP (Vw32_color_map))
6714 Vw32_color_map = Fw32_default_color_map ();
6715
6716 /* Merge in system logical colors. */
6717 add_system_logical_colors_to_map (&Vw32_color_map);
6718
6719 if (! NILP (xrm_string))
6720 xrm_option = (unsigned char *) SDATA (xrm_string);
6721 else
6722 xrm_option = (unsigned char *) 0;
6723
6724 /* Use this general default value to start with. */
6725 /* First remove .exe suffix from invocation-name - it looks ugly. */
6726 {
6727 char basename[ MAX_PATH ], *str;
6728
6729 strcpy (basename, SDATA (Vinvocation_name));
6730 str = strrchr (basename, '.');
6731 if (str) *str = 0;
6732 Vinvocation_name = build_string (basename);
6733 }
6734 Vx_resource_name = Vinvocation_name;
6735
6736 validate_x_resource_name ();
6737
6738 /* This is what opens the connection and sets x_current_display.
6739 This also initializes many symbols, such as those used for input. */
6740 dpyinfo = w32_term_init (display, xrm_option,
6741 (char *) SDATA (Vx_resource_name));
6742
6743 if (dpyinfo == 0)
6744 {
6745 if (!NILP (must_succeed))
6746 fatal ("Cannot connect to server %s.\n",
6747 SDATA (display));
6748 else
6749 error ("Cannot connect to server %s", SDATA (display));
6750 }
6751
6752 w32_in_use = 1;
6753
6754 XSETFASTINT (Vwindow_system_version, 3);
6755 return Qnil;
6756 }
6757
6758 DEFUN ("x-close-connection", Fx_close_connection,
6759 Sx_close_connection, 1, 1, 0,
6760 doc: /* Close the connection to DISPLAY's server.
6761 For DISPLAY, specify either a frame or a display name (a string).
6762 If DISPLAY is nil, that stands for the selected frame's display. */)
6763 (display)
6764 Lisp_Object display;
6765 {
6766 struct w32_display_info *dpyinfo = check_x_display_info (display);
6767 int i;
6768
6769 if (dpyinfo->reference_count > 0)
6770 error ("Display still has frames on it");
6771
6772 BLOCK_INPUT;
6773 /* Free the fonts in the font table. */
6774 for (i = 0; i < dpyinfo->n_fonts; i++)
6775 if (dpyinfo->font_table[i].name)
6776 {
6777 if (dpyinfo->font_table[i].name != dpyinfo->font_table[i].full_name)
6778 xfree (dpyinfo->font_table[i].full_name);
6779 xfree (dpyinfo->font_table[i].name);
6780 w32_unload_font (dpyinfo, dpyinfo->font_table[i].font);
6781 }
6782 x_destroy_all_bitmaps (dpyinfo);
6783
6784 x_delete_display (dpyinfo);
6785 UNBLOCK_INPUT;
6786
6787 return Qnil;
6788 }
6789
6790 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
6791 doc: /* Return the list of display names that Emacs has connections to. */)
6792 ()
6793 {
6794 Lisp_Object tail, result;
6795
6796 result = Qnil;
6797 for (tail = w32_display_name_list; ! NILP (tail); tail = XCDR (tail))
6798 result = Fcons (XCAR (XCAR (tail)), result);
6799
6800 return result;
6801 }
6802
6803 DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
6804 doc: /* This is a noop on W32 systems. */)
6805 (on, display)
6806 Lisp_Object display, on;
6807 {
6808 return Qnil;
6809 }
6810
6811
6812 \f
6813 /***********************************************************************
6814 Window properties
6815 ***********************************************************************/
6816
6817 DEFUN ("x-change-window-property", Fx_change_window_property,
6818 Sx_change_window_property, 2, 6, 0,
6819 doc: /* Change window property PROP to VALUE on the X window of FRAME.
6820 VALUE may be a string or a list of conses, numbers and/or strings.
6821 If an element in the list is a string, it is converted to
6822 an Atom and the value of the Atom is used. If an element is a cons,
6823 it is converted to a 32 bit number where the car is the 16 top bits and the
6824 cdr is the lower 16 bits.
6825 FRAME nil or omitted means use the selected frame.
6826 If TYPE is given and non-nil, it is the name of the type of VALUE.
6827 If TYPE is not given or nil, the type is STRING.
6828 FORMAT gives the size in bits of each element if VALUE is a list.
6829 It must be one of 8, 16 or 32.
6830 If VALUE is a string or FORMAT is nil or not given, FORMAT defaults to 8.
6831 If OUTER_P is non-nil, the property is changed for the outer X window of
6832 FRAME. Default is to change on the edit X window.
6833
6834 Value is VALUE. */)
6835 (prop, value, frame, type, format, outer_p)
6836 Lisp_Object prop, value, frame, type, format, outer_p;
6837 {
6838 #if 0 /* TODO : port window properties to W32 */
6839 struct frame *f = check_x_frame (frame);
6840 Atom prop_atom;
6841
6842 CHECK_STRING (prop);
6843 CHECK_STRING (value);
6844
6845 BLOCK_INPUT;
6846 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
6847 XChangeProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
6848 prop_atom, XA_STRING, 8, PropModeReplace,
6849 SDATA (value), SCHARS (value));
6850
6851 /* Make sure the property is set when we return. */
6852 XFlush (FRAME_W32_DISPLAY (f));
6853 UNBLOCK_INPUT;
6854
6855 #endif /* TODO */
6856
6857 return value;
6858 }
6859
6860
6861 DEFUN ("x-delete-window-property", Fx_delete_window_property,
6862 Sx_delete_window_property, 1, 2, 0,
6863 doc: /* Remove window property PROP from X window of FRAME.
6864 FRAME nil or omitted means use the selected frame. Value is PROP. */)
6865 (prop, frame)
6866 Lisp_Object prop, frame;
6867 {
6868 #if 0 /* TODO : port window properties to W32 */
6869
6870 struct frame *f = check_x_frame (frame);
6871 Atom prop_atom;
6872
6873 CHECK_STRING (prop);
6874 BLOCK_INPUT;
6875 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
6876 XDeleteProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), prop_atom);
6877
6878 /* Make sure the property is removed when we return. */
6879 XFlush (FRAME_W32_DISPLAY (f));
6880 UNBLOCK_INPUT;
6881 #endif /* TODO */
6882
6883 return prop;
6884 }
6885
6886
6887 DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
6888 1, 2, 0,
6889 doc: /* Value is the value of window property PROP on FRAME.
6890 If FRAME is nil or omitted, use the selected frame. Value is nil
6891 if FRAME hasn't a property with name PROP or if PROP has no string
6892 value. */)
6893 (prop, frame)
6894 Lisp_Object prop, frame;
6895 {
6896 #if 0 /* TODO : port window properties to W32 */
6897
6898 struct frame *f = check_x_frame (frame);
6899 Atom prop_atom;
6900 int rc;
6901 Lisp_Object prop_value = Qnil;
6902 char *tmp_data = NULL;
6903 Atom actual_type;
6904 int actual_format;
6905 unsigned long actual_size, bytes_remaining;
6906
6907 CHECK_STRING (prop);
6908 BLOCK_INPUT;
6909 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
6910 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
6911 prop_atom, 0, 0, False, XA_STRING,
6912 &actual_type, &actual_format, &actual_size,
6913 &bytes_remaining, (unsigned char **) &tmp_data);
6914 if (rc == Success)
6915 {
6916 int size = bytes_remaining;
6917
6918 XFree (tmp_data);
6919 tmp_data = NULL;
6920
6921 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
6922 prop_atom, 0, bytes_remaining,
6923 False, XA_STRING,
6924 &actual_type, &actual_format,
6925 &actual_size, &bytes_remaining,
6926 (unsigned char **) &tmp_data);
6927 if (rc == Success)
6928 prop_value = make_string (tmp_data, size);
6929
6930 XFree (tmp_data);
6931 }
6932
6933 UNBLOCK_INPUT;
6934
6935 return prop_value;
6936
6937 #endif /* TODO */
6938 return Qnil;
6939 }
6940
6941
6942 \f
6943 /***********************************************************************
6944 Busy cursor
6945 ***********************************************************************/
6946
6947 /* If non-null, an asynchronous timer that, when it expires, displays
6948 an hourglass cursor on all frames. */
6949
6950 static struct atimer *hourglass_atimer;
6951
6952 /* Non-zero means an hourglass cursor is currently shown. */
6953
6954 static int hourglass_shown_p;
6955
6956 /* Number of seconds to wait before displaying an hourglass cursor. */
6957
6958 static Lisp_Object Vhourglass_delay;
6959
6960 /* Default number of seconds to wait before displaying an hourglass
6961 cursor. */
6962
6963 #define DEFAULT_HOURGLASS_DELAY 1
6964
6965 /* Function prototypes. */
6966
6967 static void show_hourglass P_ ((struct atimer *));
6968 static void hide_hourglass P_ ((void));
6969
6970
6971 /* Cancel a currently active hourglass timer, and start a new one. */
6972
6973 void
6974 start_hourglass ()
6975 {
6976 #if 0 /* TODO: cursor shape changes. */
6977 EMACS_TIME delay;
6978 int secs, usecs = 0;
6979
6980 cancel_hourglass ();
6981
6982 if (INTEGERP (Vhourglass_delay)
6983 && XINT (Vhourglass_delay) > 0)
6984 secs = XFASTINT (Vhourglass_delay);
6985 else if (FLOATP (Vhourglass_delay)
6986 && XFLOAT_DATA (Vhourglass_delay) > 0)
6987 {
6988 Lisp_Object tem;
6989 tem = Ftruncate (Vhourglass_delay, Qnil);
6990 secs = XFASTINT (tem);
6991 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
6992 }
6993 else
6994 secs = DEFAULT_HOURGLASS_DELAY;
6995
6996 EMACS_SET_SECS_USECS (delay, secs, usecs);
6997 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
6998 show_hourglass, NULL);
6999 #endif
7000 }
7001
7002
7003 /* Cancel the hourglass cursor timer if active, hide an hourglass
7004 cursor if shown. */
7005
7006 void
7007 cancel_hourglass ()
7008 {
7009 if (hourglass_atimer)
7010 {
7011 cancel_atimer (hourglass_atimer);
7012 hourglass_atimer = NULL;
7013 }
7014
7015 if (hourglass_shown_p)
7016 hide_hourglass ();
7017 }
7018
7019
7020 /* Timer function of hourglass_atimer. TIMER is equal to
7021 hourglass_atimer.
7022
7023 Display an hourglass cursor on all frames by mapping the frames'
7024 hourglass_window. Set the hourglass_p flag in the frames'
7025 output_data.x structure to indicate that an hourglass cursor is
7026 shown on the frames. */
7027
7028 static void
7029 show_hourglass (timer)
7030 struct atimer *timer;
7031 {
7032 #if 0 /* TODO: cursor shape changes. */
7033 /* The timer implementation will cancel this timer automatically
7034 after this function has run. Set hourglass_atimer to null
7035 so that we know the timer doesn't have to be canceled. */
7036 hourglass_atimer = NULL;
7037
7038 if (!hourglass_shown_p)
7039 {
7040 Lisp_Object rest, frame;
7041
7042 BLOCK_INPUT;
7043
7044 FOR_EACH_FRAME (rest, frame)
7045 if (FRAME_W32_P (XFRAME (frame)))
7046 {
7047 struct frame *f = XFRAME (frame);
7048
7049 f->output_data.w32->hourglass_p = 1;
7050
7051 if (!f->output_data.w32->hourglass_window)
7052 {
7053 unsigned long mask = CWCursor;
7054 XSetWindowAttributes attrs;
7055
7056 attrs.cursor = f->output_data.w32->hourglass_cursor;
7057
7058 f->output_data.w32->hourglass_window
7059 = XCreateWindow (FRAME_X_DISPLAY (f),
7060 FRAME_OUTER_WINDOW (f),
7061 0, 0, 32000, 32000, 0, 0,
7062 InputOnly,
7063 CopyFromParent,
7064 mask, &attrs);
7065 }
7066
7067 XMapRaised (FRAME_X_DISPLAY (f),
7068 f->output_data.w32->hourglass_window);
7069 XFlush (FRAME_X_DISPLAY (f));
7070 }
7071
7072 hourglass_shown_p = 1;
7073 UNBLOCK_INPUT;
7074 }
7075 #endif
7076 }
7077
7078
7079 /* Hide the hourglass cursor on all frames, if it is currently shown. */
7080
7081 static void
7082 hide_hourglass ()
7083 {
7084 #if 0 /* TODO: cursor shape changes. */
7085 if (hourglass_shown_p)
7086 {
7087 Lisp_Object rest, frame;
7088
7089 BLOCK_INPUT;
7090 FOR_EACH_FRAME (rest, frame)
7091 {
7092 struct frame *f = XFRAME (frame);
7093
7094 if (FRAME_W32_P (f)
7095 /* Watch out for newly created frames. */
7096 && f->output_data.x->hourglass_window)
7097 {
7098 XUnmapWindow (FRAME_X_DISPLAY (f),
7099 f->output_data.x->hourglass_window);
7100 /* Sync here because XTread_socket looks at the
7101 hourglass_p flag that is reset to zero below. */
7102 XSync (FRAME_X_DISPLAY (f), False);
7103 f->output_data.x->hourglass_p = 0;
7104 }
7105 }
7106
7107 hourglass_shown_p = 0;
7108 UNBLOCK_INPUT;
7109 }
7110 #endif
7111 }
7112
7113
7114 \f
7115 /***********************************************************************
7116 Tool tips
7117 ***********************************************************************/
7118
7119 static Lisp_Object x_create_tip_frame P_ ((struct w32_display_info *,
7120 Lisp_Object, Lisp_Object));
7121 static void compute_tip_xy P_ ((struct frame *, Lisp_Object, Lisp_Object,
7122 Lisp_Object, int, int, int *, int *));
7123
7124 /* The frame of a currently visible tooltip. */
7125
7126 Lisp_Object tip_frame;
7127
7128 /* If non-nil, a timer started that hides the last tooltip when it
7129 fires. */
7130
7131 Lisp_Object tip_timer;
7132 Window tip_window;
7133
7134 /* If non-nil, a vector of 3 elements containing the last args
7135 with which x-show-tip was called. See there. */
7136
7137 Lisp_Object last_show_tip_args;
7138
7139 /* Maximum size for tooltips; a cons (COLUMNS . ROWS). */
7140
7141 Lisp_Object Vx_max_tooltip_size;
7142
7143
7144 static Lisp_Object
7145 unwind_create_tip_frame (frame)
7146 Lisp_Object frame;
7147 {
7148 Lisp_Object deleted;
7149
7150 deleted = unwind_create_frame (frame);
7151 if (EQ (deleted, Qt))
7152 {
7153 tip_window = NULL;
7154 tip_frame = Qnil;
7155 }
7156
7157 return deleted;
7158 }
7159
7160
7161 /* Create a frame for a tooltip on the display described by DPYINFO.
7162 PARMS is a list of frame parameters. TEXT is the string to
7163 display in the tip frame. Value is the frame.
7164
7165 Note that functions called here, esp. x_default_parameter can
7166 signal errors, for instance when a specified color name is
7167 undefined. We have to make sure that we're in a consistent state
7168 when this happens. */
7169
7170 static Lisp_Object
7171 x_create_tip_frame (dpyinfo, parms, text)
7172 struct w32_display_info *dpyinfo;
7173 Lisp_Object parms, text;
7174 {
7175 struct frame *f;
7176 Lisp_Object frame, tem;
7177 Lisp_Object name;
7178 long window_prompting = 0;
7179 int width, height;
7180 int count = SPECPDL_INDEX ();
7181 struct gcpro gcpro1, gcpro2, gcpro3;
7182 struct kboard *kb;
7183 int face_change_count_before = face_change_count;
7184 Lisp_Object buffer;
7185 struct buffer *old_buffer;
7186
7187 check_w32 ();
7188
7189 /* Use this general default value to start with until we know if
7190 this frame has a specified name. */
7191 Vx_resource_name = Vinvocation_name;
7192
7193 #ifdef MULTI_KBOARD
7194 kb = dpyinfo->kboard;
7195 #else
7196 kb = &the_only_kboard;
7197 #endif
7198
7199 /* Get the name of the frame to use for resource lookup. */
7200 name = w32_get_arg (parms, Qname, "name", "Name", RES_TYPE_STRING);
7201 if (!STRINGP (name)
7202 && !EQ (name, Qunbound)
7203 && !NILP (name))
7204 error ("Invalid frame name--not a string or nil");
7205 Vx_resource_name = name;
7206
7207 frame = Qnil;
7208 GCPRO3 (parms, name, frame);
7209 /* Make a frame without minibuffer nor mode-line. */
7210 f = make_frame (0);
7211 f->wants_modeline = 0;
7212 XSETFRAME (frame, f);
7213
7214 buffer = Fget_buffer_create (build_string (" *tip*"));
7215 Fset_window_buffer (FRAME_ROOT_WINDOW (f), buffer, Qnil);
7216 old_buffer = current_buffer;
7217 set_buffer_internal_1 (XBUFFER (buffer));
7218 current_buffer->truncate_lines = Qnil;
7219 specbind (Qinhibit_read_only, Qt);
7220 specbind (Qinhibit_modification_hooks, Qt);
7221 Ferase_buffer ();
7222 Finsert (1, &text);
7223 set_buffer_internal_1 (old_buffer);
7224
7225 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
7226 record_unwind_protect (unwind_create_tip_frame, frame);
7227
7228 /* By setting the output method, we're essentially saying that
7229 the frame is live, as per FRAME_LIVE_P. If we get a signal
7230 from this point on, x_destroy_window might screw up reference
7231 counts etc. */
7232 f->output_method = output_w32;
7233 f->output_data.w32 =
7234 (struct w32_output *) xmalloc (sizeof (struct w32_output));
7235 bzero (f->output_data.w32, sizeof (struct w32_output));
7236
7237 FRAME_FONTSET (f) = -1;
7238 f->icon_name = Qnil;
7239
7240 #if 0 /* GLYPH_DEBUG TODO: image support. */
7241 image_cache_refcount = FRAME_X_IMAGE_CACHE (f)->refcount;
7242 dpyinfo_refcount = dpyinfo->reference_count;
7243 #endif /* GLYPH_DEBUG */
7244 #ifdef MULTI_KBOARD
7245 FRAME_KBOARD (f) = kb;
7246 #endif
7247 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
7248 f->output_data.w32->explicit_parent = 0;
7249
7250 /* Set the name; the functions to which we pass f expect the name to
7251 be set. */
7252 if (EQ (name, Qunbound) || NILP (name))
7253 {
7254 f->name = build_string (dpyinfo->w32_id_name);
7255 f->explicit_name = 0;
7256 }
7257 else
7258 {
7259 f->name = name;
7260 f->explicit_name = 1;
7261 /* use the frame's title when getting resources for this frame. */
7262 specbind (Qx_resource_name, name);
7263 }
7264
7265 /* Extract the window parameters from the supplied values
7266 that are needed to determine window geometry. */
7267 {
7268 Lisp_Object font;
7269
7270 font = w32_get_arg (parms, Qfont, "font", "Font", RES_TYPE_STRING);
7271
7272 BLOCK_INPUT;
7273 /* First, try whatever font the caller has specified. */
7274 if (STRINGP (font))
7275 {
7276 tem = Fquery_fontset (font, Qnil);
7277 if (STRINGP (tem))
7278 font = x_new_fontset (f, SDATA (tem));
7279 else
7280 font = x_new_font (f, SDATA (font));
7281 }
7282
7283 /* Try out a font which we hope has bold and italic variations. */
7284 if (!STRINGP (font))
7285 font = x_new_font (f, "-*-Courier New-normal-r-*-*-*-100-*-*-c-*-iso8859-1");
7286 if (! STRINGP (font))
7287 font = x_new_font (f, "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1");
7288 /* If those didn't work, look for something which will at least work. */
7289 if (! STRINGP (font))
7290 font = x_new_font (f, "-*-Fixedsys-normal-r-*-*-12-*-*-*-c-*-iso8859-1");
7291 UNBLOCK_INPUT;
7292 if (! STRINGP (font))
7293 font = build_string ("Fixedsys");
7294
7295 x_default_parameter (f, parms, Qfont, font,
7296 "font", "Font", RES_TYPE_STRING);
7297 }
7298
7299 x_default_parameter (f, parms, Qborder_width, make_number (2),
7300 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
7301 /* This defaults to 2 in order to match xterm. We recognize either
7302 internalBorderWidth or internalBorder (which is what xterm calls
7303 it). */
7304 if (NILP (Fassq (Qinternal_border_width, parms)))
7305 {
7306 Lisp_Object value;
7307
7308 value = w32_get_arg (parms, Qinternal_border_width,
7309 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
7310 if (! EQ (value, Qunbound))
7311 parms = Fcons (Fcons (Qinternal_border_width, value),
7312 parms);
7313 }
7314 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
7315 "internalBorderWidth", "internalBorderWidth",
7316 RES_TYPE_NUMBER);
7317
7318 /* Also do the stuff which must be set before the window exists. */
7319 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
7320 "foreground", "Foreground", RES_TYPE_STRING);
7321 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
7322 "background", "Background", RES_TYPE_STRING);
7323 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
7324 "pointerColor", "Foreground", RES_TYPE_STRING);
7325 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
7326 "cursorColor", "Foreground", RES_TYPE_STRING);
7327 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
7328 "borderColor", "BorderColor", RES_TYPE_STRING);
7329
7330 /* Init faces before x_default_parameter is called for scroll-bar
7331 parameters because that function calls x_set_scroll_bar_width,
7332 which calls change_frame_size, which calls Fset_window_buffer,
7333 which runs hooks, which call Fvertical_motion. At the end, we
7334 end up in init_iterator with a null face cache, which should not
7335 happen. */
7336 init_frame_faces (f);
7337
7338 f->output_data.w32->dwStyle = WS_BORDER | WS_POPUP | WS_DISABLED;
7339 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
7340
7341 window_prompting = x_figure_window_size (f, parms, 0);
7342
7343 /* No fringes on tip frame. */
7344 f->fringe_cols = 0;
7345 f->left_fringe_width = 0;
7346 f->right_fringe_width = 0;
7347
7348 BLOCK_INPUT;
7349 my_create_tip_window (f);
7350 UNBLOCK_INPUT;
7351
7352 x_make_gc (f);
7353
7354 x_default_parameter (f, parms, Qauto_raise, Qnil,
7355 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
7356 x_default_parameter (f, parms, Qauto_lower, Qnil,
7357 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
7358 x_default_parameter (f, parms, Qcursor_type, Qbox,
7359 "cursorType", "CursorType", RES_TYPE_SYMBOL);
7360
7361 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
7362 Change will not be effected unless different from the current
7363 FRAME_LINES (f). */
7364 width = FRAME_COLS (f);
7365 height = FRAME_LINES (f);
7366 FRAME_LINES (f) = 0;
7367 SET_FRAME_COLS (f, 0);
7368 change_frame_size (f, height, width, 1, 0, 0);
7369
7370 /* Add `tooltip' frame parameter's default value. */
7371 if (NILP (Fframe_parameter (frame, intern ("tooltip"))))
7372 Fmodify_frame_parameters (frame, Fcons (Fcons (intern ("tooltip"), Qt),
7373 Qnil));
7374
7375 /* Set up faces after all frame parameters are known. This call
7376 also merges in face attributes specified for new frames.
7377
7378 Frame parameters may be changed if .Xdefaults contains
7379 specifications for the default font. For example, if there is an
7380 `Emacs.default.attributeBackground: pink', the `background-color'
7381 attribute of the frame get's set, which let's the internal border
7382 of the tooltip frame appear in pink. Prevent this. */
7383 {
7384 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
7385
7386 /* Set tip_frame here, so that */
7387 tip_frame = frame;
7388 call1 (Qface_set_after_frame_default, frame);
7389
7390 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
7391 Fmodify_frame_parameters (frame, Fcons (Fcons (Qbackground_color, bg),
7392 Qnil));
7393 }
7394
7395 f->no_split = 1;
7396
7397 UNGCPRO;
7398
7399 /* It is now ok to make the frame official even if we get an error
7400 below. And the frame needs to be on Vframe_list or making it
7401 visible won't work. */
7402 Vframe_list = Fcons (frame, Vframe_list);
7403
7404 /* Now that the frame is official, it counts as a reference to
7405 its display. */
7406 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
7407
7408 /* Setting attributes of faces of the tooltip frame from resources
7409 and similar will increment face_change_count, which leads to the
7410 clearing of all current matrices. Since this isn't necessary
7411 here, avoid it by resetting face_change_count to the value it
7412 had before we created the tip frame. */
7413 face_change_count = face_change_count_before;
7414
7415 /* Discard the unwind_protect. */
7416 return unbind_to (count, frame);
7417 }
7418
7419
7420 /* Compute where to display tip frame F. PARMS is the list of frame
7421 parameters for F. DX and DY are specified offsets from the current
7422 location of the mouse. WIDTH and HEIGHT are the width and height
7423 of the tooltip. Return coordinates relative to the root window of
7424 the display in *ROOT_X, and *ROOT_Y. */
7425
7426 static void
7427 compute_tip_xy (f, parms, dx, dy, width, height, root_x, root_y)
7428 struct frame *f;
7429 Lisp_Object parms, dx, dy;
7430 int width, height;
7431 int *root_x, *root_y;
7432 {
7433 Lisp_Object left, top;
7434
7435 /* User-specified position? */
7436 left = Fcdr (Fassq (Qleft, parms));
7437 top = Fcdr (Fassq (Qtop, parms));
7438
7439 /* Move the tooltip window where the mouse pointer is. Resize and
7440 show it. */
7441 if (!INTEGERP (left) || !INTEGERP (top))
7442 {
7443 POINT pt;
7444
7445 BLOCK_INPUT;
7446 GetCursorPos (&pt);
7447 *root_x = pt.x;
7448 *root_y = pt.y;
7449 UNBLOCK_INPUT;
7450 }
7451
7452 if (INTEGERP (top))
7453 *root_y = XINT (top);
7454 else if (*root_y + XINT (dy) <= 0)
7455 *root_y = 0; /* Can happen for negative dy */
7456 else if (*root_y + XINT (dy) + height <= FRAME_W32_DISPLAY_INFO (f)->height)
7457 /* It fits below the pointer */
7458 *root_y += XINT (dy);
7459 else if (height + XINT (dy) <= *root_y)
7460 /* It fits above the pointer. */
7461 *root_y -= height + XINT (dy);
7462 else
7463 /* Put it on the top. */
7464 *root_y = 0;
7465
7466 if (INTEGERP (left))
7467 *root_x = XINT (left);
7468 else if (*root_x + XINT (dx) <= 0)
7469 *root_x = 0; /* Can happen for negative dx */
7470 else if (*root_x + XINT (dx) + width <= FRAME_W32_DISPLAY_INFO (f)->width)
7471 /* It fits to the right of the pointer. */
7472 *root_x += XINT (dx);
7473 else if (width + XINT (dx) <= *root_x)
7474 /* It fits to the left of the pointer. */
7475 *root_x -= width + XINT (dx);
7476 else
7477 /* Put it left justified on the screen -- it ought to fit that way. */
7478 *root_x = 0;
7479 }
7480
7481
7482 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
7483 doc: /* Show STRING in a \"tooltip\" window on frame FRAME.
7484 A tooltip window is a small window displaying a string.
7485
7486 FRAME nil or omitted means use the selected frame.
7487
7488 PARMS is an optional list of frame parameters which can be
7489 used to change the tooltip's appearance.
7490
7491 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
7492 means use the default timeout of 5 seconds.
7493
7494 If the list of frame parameters PARMS contains a `left' parameter,
7495 the tooltip is displayed at that x-position. Otherwise it is
7496 displayed at the mouse position, with offset DX added (default is 5 if
7497 DX isn't specified). Likewise for the y-position; if a `top' frame
7498 parameter is specified, it determines the y-position of the tooltip
7499 window, otherwise it is displayed at the mouse position, with offset
7500 DY added (default is -10).
7501
7502 A tooltip's maximum size is specified by `x-max-tooltip-size'.
7503 Text larger than the specified size is clipped. */)
7504 (string, frame, parms, timeout, dx, dy)
7505 Lisp_Object string, frame, parms, timeout, dx, dy;
7506 {
7507 struct frame *f;
7508 struct window *w;
7509 int root_x, root_y;
7510 struct buffer *old_buffer;
7511 struct text_pos pos;
7512 int i, width, height;
7513 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7514 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7515 int count = SPECPDL_INDEX ();
7516
7517 specbind (Qinhibit_redisplay, Qt);
7518
7519 GCPRO4 (string, parms, frame, timeout);
7520
7521 CHECK_STRING (string);
7522 f = check_x_frame (frame);
7523 if (NILP (timeout))
7524 timeout = make_number (5);
7525 else
7526 CHECK_NATNUM (timeout);
7527
7528 if (NILP (dx))
7529 dx = make_number (5);
7530 else
7531 CHECK_NUMBER (dx);
7532
7533 if (NILP (dy))
7534 dy = make_number (-10);
7535 else
7536 CHECK_NUMBER (dy);
7537
7538 if (NILP (last_show_tip_args))
7539 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
7540
7541 if (!NILP (tip_frame))
7542 {
7543 Lisp_Object last_string = AREF (last_show_tip_args, 0);
7544 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
7545 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
7546
7547 if (EQ (frame, last_frame)
7548 && !NILP (Fequal (last_string, string))
7549 && !NILP (Fequal (last_parms, parms)))
7550 {
7551 struct frame *f = XFRAME (tip_frame);
7552
7553 /* Only DX and DY have changed. */
7554 if (!NILP (tip_timer))
7555 {
7556 Lisp_Object timer = tip_timer;
7557 tip_timer = Qnil;
7558 call1 (Qcancel_timer, timer);
7559 }
7560
7561 BLOCK_INPUT;
7562 compute_tip_xy (f, parms, dx, dy, FRAME_PIXEL_WIDTH (f),
7563 FRAME_PIXEL_HEIGHT (f), &root_x, &root_y);
7564
7565 /* Put tooltip in topmost group and in position. */
7566 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
7567 root_x, root_y, 0, 0,
7568 SWP_NOSIZE | SWP_NOACTIVATE);
7569
7570 /* Ensure tooltip is on top of other topmost windows (eg menus). */
7571 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
7572 0, 0, 0, 0,
7573 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
7574
7575 UNBLOCK_INPUT;
7576 goto start_timer;
7577 }
7578 }
7579
7580 /* Hide a previous tip, if any. */
7581 Fx_hide_tip ();
7582
7583 ASET (last_show_tip_args, 0, string);
7584 ASET (last_show_tip_args, 1, frame);
7585 ASET (last_show_tip_args, 2, parms);
7586
7587 /* Add default values to frame parameters. */
7588 if (NILP (Fassq (Qname, parms)))
7589 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
7590 if (NILP (Fassq (Qinternal_border_width, parms)))
7591 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
7592 if (NILP (Fassq (Qborder_width, parms)))
7593 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
7594 if (NILP (Fassq (Qborder_color, parms)))
7595 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
7596 if (NILP (Fassq (Qbackground_color, parms)))
7597 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
7598 parms);
7599
7600 /* Block input until the tip has been fully drawn, to avoid crashes
7601 when drawing tips in menus. */
7602 BLOCK_INPUT;
7603
7604 /* Create a frame for the tooltip, and record it in the global
7605 variable tip_frame. */
7606 frame = x_create_tip_frame (FRAME_W32_DISPLAY_INFO (f), parms, string);
7607 f = XFRAME (frame);
7608
7609 /* Set up the frame's root window. */
7610 w = XWINDOW (FRAME_ROOT_WINDOW (f));
7611 w->left_col = w->top_line = make_number (0);
7612
7613 if (CONSP (Vx_max_tooltip_size)
7614 && INTEGERP (XCAR (Vx_max_tooltip_size))
7615 && XINT (XCAR (Vx_max_tooltip_size)) > 0
7616 && INTEGERP (XCDR (Vx_max_tooltip_size))
7617 && XINT (XCDR (Vx_max_tooltip_size)) > 0)
7618 {
7619 w->total_cols = XCAR (Vx_max_tooltip_size);
7620 w->total_lines = XCDR (Vx_max_tooltip_size);
7621 }
7622 else
7623 {
7624 w->total_cols = make_number (80);
7625 w->total_lines = make_number (40);
7626 }
7627
7628 FRAME_TOTAL_COLS (f) = XINT (w->total_cols);
7629 adjust_glyphs (f);
7630 w->pseudo_window_p = 1;
7631
7632 /* Display the tooltip text in a temporary buffer. */
7633 old_buffer = current_buffer;
7634 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->buffer));
7635 current_buffer->truncate_lines = Qnil;
7636 clear_glyph_matrix (w->desired_matrix);
7637 clear_glyph_matrix (w->current_matrix);
7638 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
7639 try_window (FRAME_ROOT_WINDOW (f), pos, 0);
7640
7641 /* Compute width and height of the tooltip. */
7642 width = height = 0;
7643 for (i = 0; i < w->desired_matrix->nrows; ++i)
7644 {
7645 struct glyph_row *row = &w->desired_matrix->rows[i];
7646 struct glyph *last;
7647 int row_width;
7648
7649 /* Stop at the first empty row at the end. */
7650 if (!row->enabled_p || !row->displays_text_p)
7651 break;
7652
7653 /* Let the row go over the full width of the frame. */
7654 row->full_width_p = 1;
7655
7656 #ifdef TODO /* Investigate why some fonts need more width than is
7657 calculated for some tooltips. */
7658 /* There's a glyph at the end of rows that is use to place
7659 the cursor there. Don't include the width of this glyph. */
7660 if (row->used[TEXT_AREA])
7661 {
7662 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
7663 row_width = row->pixel_width - last->pixel_width;
7664 }
7665 else
7666 #endif
7667 row_width = row->pixel_width;
7668
7669 /* TODO: find why tips do not draw along baseline as instructed. */
7670 height += row->height;
7671 width = max (width, row_width);
7672 }
7673
7674 /* Add the frame's internal border to the width and height the X
7675 window should have. */
7676 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
7677 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
7678
7679 /* Move the tooltip window where the mouse pointer is. Resize and
7680 show it. */
7681 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
7682
7683 {
7684 /* Adjust Window size to take border into account. */
7685 RECT rect;
7686 rect.left = rect.top = 0;
7687 rect.right = width;
7688 rect.bottom = height;
7689 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
7690 FRAME_EXTERNAL_MENU_BAR (f));
7691
7692 /* Position and size tooltip, and put it in the topmost group.
7693 The add-on of 3 to the 5th argument is a kludge: without it,
7694 some fonts cause the last character of the tip to be truncated,
7695 for some obscure reason. */
7696 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
7697 root_x, root_y, rect.right - rect.left + 3,
7698 rect.bottom - rect.top, SWP_NOACTIVATE);
7699
7700 /* Ensure tooltip is on top of other topmost windows (eg menus). */
7701 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
7702 0, 0, 0, 0,
7703 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
7704
7705 /* Let redisplay know that we have made the frame visible already. */
7706 f->async_visible = 1;
7707
7708 ShowWindow (FRAME_W32_WINDOW (f), SW_SHOWNOACTIVATE);
7709 }
7710
7711 /* Draw into the window. */
7712 w->must_be_updated_p = 1;
7713 update_single_window (w, 1);
7714
7715 UNBLOCK_INPUT;
7716
7717 /* Restore original current buffer. */
7718 set_buffer_internal_1 (old_buffer);
7719 windows_or_buffers_changed = old_windows_or_buffers_changed;
7720
7721 start_timer:
7722 /* Let the tip disappear after timeout seconds. */
7723 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
7724 intern ("x-hide-tip"));
7725
7726 UNGCPRO;
7727 return unbind_to (count, Qnil);
7728 }
7729
7730
7731 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
7732 doc: /* Hide the current tooltip window, if there is any.
7733 Value is t if tooltip was open, nil otherwise. */)
7734 ()
7735 {
7736 int count;
7737 Lisp_Object deleted, frame, timer;
7738 struct gcpro gcpro1, gcpro2;
7739
7740 /* Return quickly if nothing to do. */
7741 if (NILP (tip_timer) && NILP (tip_frame))
7742 return Qnil;
7743
7744 frame = tip_frame;
7745 timer = tip_timer;
7746 GCPRO2 (frame, timer);
7747 tip_frame = tip_timer = deleted = Qnil;
7748
7749 count = SPECPDL_INDEX ();
7750 specbind (Qinhibit_redisplay, Qt);
7751 specbind (Qinhibit_quit, Qt);
7752
7753 if (!NILP (timer))
7754 call1 (Qcancel_timer, timer);
7755
7756 if (FRAMEP (frame))
7757 {
7758 Fdelete_frame (frame, Qnil);
7759 deleted = Qt;
7760 }
7761
7762 UNGCPRO;
7763 return unbind_to (count, deleted);
7764 }
7765
7766
7767 \f
7768 /***********************************************************************
7769 File selection dialog
7770 ***********************************************************************/
7771 extern Lisp_Object Qfile_name_history;
7772
7773 /* Callback for altering the behaviour of the Open File dialog.
7774 Makes the Filename text field contain "Current Directory" and be
7775 read-only when "Directories" is selected in the filter. This
7776 allows us to work around the fact that the standard Open File
7777 dialog does not support directories. */
7778 UINT CALLBACK
7779 file_dialog_callback (hwnd, msg, wParam, lParam)
7780 HWND hwnd;
7781 UINT msg;
7782 WPARAM wParam;
7783 LPARAM lParam;
7784 {
7785 if (msg == WM_NOTIFY)
7786 {
7787 OFNOTIFY * notify = (OFNOTIFY *)lParam;
7788 /* Detect when the Filter dropdown is changed. */
7789 if (notify->hdr.code == CDN_TYPECHANGE
7790 || notify->hdr.code == CDN_INITDONE)
7791 {
7792 HWND dialog = GetParent (hwnd);
7793 HWND edit_control = GetDlgItem (dialog, FILE_NAME_TEXT_FIELD);
7794
7795 /* Directories is in index 2. */
7796 if (notify->lpOFN->nFilterIndex == 2)
7797 {
7798 CommDlg_OpenSave_SetControlText (dialog, FILE_NAME_TEXT_FIELD,
7799 "Current Directory");
7800 EnableWindow (edit_control, FALSE);
7801 }
7802 else
7803 {
7804 /* Don't override default filename on init done. */
7805 if (notify->hdr.code == CDN_TYPECHANGE)
7806 CommDlg_OpenSave_SetControlText (dialog,
7807 FILE_NAME_TEXT_FIELD, "");
7808 EnableWindow (edit_control, TRUE);
7809 }
7810 }
7811 }
7812 return 0;
7813 }
7814
7815 /* Since we compile with _WIN32_WINNT set to 0x0400 (for NT4 compatibility)
7816 we end up with the old file dialogs. Define a big enough struct for the
7817 new dialog to trick GetOpenFileName into giving us the new dialogs on
7818 Windows 2000 and XP. */
7819 typedef struct
7820 {
7821 OPENFILENAME real_details;
7822 void * pReserved;
7823 DWORD dwReserved;
7824 DWORD FlagsEx;
7825 } NEWOPENFILENAME;
7826
7827
7828 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
7829 doc: /* Read file name, prompting with PROMPT in directory DIR.
7830 Use a file selection dialog.
7831 Select DEFAULT-FILENAME in the dialog's file selection box, if
7832 specified. Ensure that file exists if MUSTMATCH is non-nil.
7833 If ONLY-DIR-P is non-nil, the user can only select directories. */)
7834 (prompt, dir, default_filename, mustmatch, only_dir_p)
7835 Lisp_Object prompt, dir, default_filename, mustmatch, only_dir_p;
7836 {
7837 struct frame *f = SELECTED_FRAME ();
7838 Lisp_Object file = Qnil;
7839 int count = SPECPDL_INDEX ();
7840 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
7841 char filename[MAX_PATH + 1];
7842 char init_dir[MAX_PATH + 1];
7843 int default_filter_index = 1; /* 1: All Files, 2: Directories only */
7844
7845 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
7846 CHECK_STRING (prompt);
7847 CHECK_STRING (dir);
7848
7849 /* Create the dialog with PROMPT as title, using DIR as initial
7850 directory and using "*" as pattern. */
7851 dir = Fexpand_file_name (dir, Qnil);
7852 strncpy (init_dir, SDATA (ENCODE_FILE (dir)), MAX_PATH);
7853 init_dir[MAX_PATH] = '\0';
7854 unixtodos_filename (init_dir);
7855
7856 if (STRINGP (default_filename))
7857 {
7858 char *file_name_only;
7859 char *full_path_name = SDATA (ENCODE_FILE (default_filename));
7860
7861 unixtodos_filename (full_path_name);
7862
7863 file_name_only = strrchr (full_path_name, '\\');
7864 if (!file_name_only)
7865 file_name_only = full_path_name;
7866 else
7867 file_name_only++;
7868
7869 strncpy (filename, file_name_only, MAX_PATH);
7870 filename[MAX_PATH] = '\0';
7871 }
7872 else
7873 filename[0] = '\0';
7874
7875 {
7876 NEWOPENFILENAME new_file_details;
7877 BOOL file_opened = FALSE;
7878 OPENFILENAME * file_details = &new_file_details.real_details;
7879
7880 /* Prevent redisplay. */
7881 specbind (Qinhibit_redisplay, Qt);
7882 BLOCK_INPUT;
7883
7884 bzero (&new_file_details, sizeof (new_file_details));
7885 /* Apparently NT4 crashes if you give it an unexpected size.
7886 I'm not sure about Windows 9x, so play it safe. */
7887 if (w32_major_version > 4 && w32_major_version < 95)
7888 file_details->lStructSize = sizeof (new_file_details);
7889 else
7890 file_details->lStructSize = sizeof (file_details);
7891
7892 file_details->hwndOwner = FRAME_W32_WINDOW (f);
7893 /* Undocumented Bug in Common File Dialog:
7894 If a filter is not specified, shell links are not resolved. */
7895 file_details->lpstrFilter = "All Files (*.*)\0*.*\0Directories\0*|*\0\0";
7896 file_details->lpstrFile = filename;
7897 file_details->nMaxFile = sizeof (filename);
7898 file_details->lpstrInitialDir = init_dir;
7899 file_details->lpstrTitle = SDATA (prompt);
7900
7901 if (! NILP (only_dir_p))
7902 default_filter_index = 2;
7903
7904 file_details->nFilterIndex = default_filter_index;
7905
7906 file_details->Flags = (OFN_HIDEREADONLY | OFN_NOCHANGEDIR
7907 | OFN_EXPLORER | OFN_ENABLEHOOK);
7908 if (!NILP (mustmatch))
7909 {
7910 /* Require that the path to the parent directory exists. */
7911 file_details->Flags |= OFN_PATHMUSTEXIST;
7912 /* If we are looking for a file, require that it exists. */
7913 if (NILP (only_dir_p))
7914 file_details->Flags |= OFN_FILEMUSTEXIST;
7915 }
7916
7917 file_details->lpfnHook = (LPOFNHOOKPROC) file_dialog_callback;
7918
7919 file_opened = GetOpenFileName (file_details);
7920
7921 UNBLOCK_INPUT;
7922
7923 if (file_opened)
7924 {
7925 dostounix_filename (filename);
7926
7927 if (file_details->nFilterIndex == 2)
7928 {
7929 /* "Directories" selected - strip dummy file name. */
7930 char * last = strrchr (filename, '/');
7931 *last = '\0';
7932 }
7933
7934 file = DECODE_FILE(build_string (filename));
7935 }
7936 /* User cancelled the dialog without making a selection. */
7937 else if (!CommDlgExtendedError ())
7938 file = Qnil;
7939 /* An error occurred, fallback on reading from the mini-buffer. */
7940 else
7941 file = Fcompleting_read (prompt, intern ("read-file-name-internal"),
7942 dir, mustmatch, dir, Qfile_name_history,
7943 default_filename, Qnil);
7944
7945 file = unbind_to (count, file);
7946 }
7947
7948 UNGCPRO;
7949
7950 /* Make "Cancel" equivalent to C-g. */
7951 if (NILP (file))
7952 Fsignal (Qquit, Qnil);
7953
7954 return unbind_to (count, file);
7955 }
7956
7957
7958 \f
7959 /***********************************************************************
7960 w32 specialized functions
7961 ***********************************************************************/
7962
7963 DEFUN ("w32-select-font", Fw32_select_font, Sw32_select_font, 0, 2, 0,
7964 doc: /* Select a font for the named FRAME using the W32 font dialog.
7965 Returns an X-style font string corresponding to the selection.
7966
7967 If FRAME is omitted or nil, it defaults to the selected frame.
7968 If INCLUDE-PROPORTIONAL is non-nil, include proportional fonts
7969 in the font selection dialog. */)
7970 (frame, include_proportional)
7971 Lisp_Object frame, include_proportional;
7972 {
7973 FRAME_PTR f = check_x_frame (frame);
7974 CHOOSEFONT cf;
7975 LOGFONT lf;
7976 TEXTMETRIC tm;
7977 HDC hdc;
7978 HANDLE oldobj;
7979 char buf[100];
7980
7981 bzero (&cf, sizeof (cf));
7982 bzero (&lf, sizeof (lf));
7983
7984 cf.lStructSize = sizeof (cf);
7985 cf.hwndOwner = FRAME_W32_WINDOW (f);
7986 cf.Flags = CF_FORCEFONTEXIST | CF_SCREENFONTS | CF_NOVERTFONTS;
7987
7988 /* Unless include_proportional is non-nil, limit the selection to
7989 monospaced fonts. */
7990 if (NILP (include_proportional))
7991 cf.Flags |= CF_FIXEDPITCHONLY;
7992
7993 cf.lpLogFont = &lf;
7994
7995 /* Initialize as much of the font details as we can from the current
7996 default font. */
7997 hdc = GetDC (FRAME_W32_WINDOW (f));
7998 oldobj = SelectObject (hdc, FRAME_FONT (f)->hfont);
7999 GetTextFace (hdc, LF_FACESIZE, lf.lfFaceName);
8000 if (GetTextMetrics (hdc, &tm))
8001 {
8002 lf.lfHeight = tm.tmInternalLeading - tm.tmHeight;
8003 lf.lfWeight = tm.tmWeight;
8004 lf.lfItalic = tm.tmItalic;
8005 lf.lfUnderline = tm.tmUnderlined;
8006 lf.lfStrikeOut = tm.tmStruckOut;
8007 lf.lfCharSet = tm.tmCharSet;
8008 cf.Flags |= CF_INITTOLOGFONTSTRUCT;
8009 }
8010 SelectObject (hdc, oldobj);
8011 ReleaseDC (FRAME_W32_WINDOW (f), hdc);
8012
8013 if (!ChooseFont (&cf) || !w32_to_x_font (&lf, buf, 100, NULL))
8014 return Qnil;
8015
8016 return build_string (buf);
8017 }
8018
8019 DEFUN ("w32-send-sys-command", Fw32_send_sys_command,
8020 Sw32_send_sys_command, 1, 2, 0,
8021 doc: /* Send frame a Windows WM_SYSCOMMAND message of type COMMAND.
8022 Some useful values for COMMAND are #xf030 to maximize frame (#xf020
8023 to minimize), #xf120 to restore frame to original size, and #xf100
8024 to activate the menubar for keyboard access. #xf140 activates the
8025 screen saver if defined.
8026
8027 If optional parameter FRAME is not specified, use selected frame. */)
8028 (command, frame)
8029 Lisp_Object command, frame;
8030 {
8031 FRAME_PTR f = check_x_frame (frame);
8032
8033 CHECK_NUMBER (command);
8034
8035 PostMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, XINT (command), 0);
8036
8037 return Qnil;
8038 }
8039
8040 DEFUN ("w32-shell-execute", Fw32_shell_execute, Sw32_shell_execute, 2, 4, 0,
8041 doc: /* Get Windows to perform OPERATION on DOCUMENT.
8042 This is a wrapper around the ShellExecute system function, which
8043 invokes the application registered to handle OPERATION for DOCUMENT.
8044 OPERATION is typically \"open\", \"print\" or \"explore\" (but can be
8045 nil for the default action), and DOCUMENT is typically the name of a
8046 document file or URL, but can also be a program executable to run or
8047 a directory to open in the Windows Explorer.
8048
8049 If DOCUMENT is a program executable, PARAMETERS can be a string
8050 containing command line parameters, but otherwise should be nil.
8051
8052 SHOW-FLAG can be used to control whether the invoked application is hidden
8053 or minimized. If SHOW-FLAG is nil, the application is displayed normally,
8054 otherwise it is an integer representing a ShowWindow flag:
8055
8056 0 - start hidden
8057 1 - start normally
8058 3 - start maximized
8059 6 - start minimized */)
8060 (operation, document, parameters, show_flag)
8061 Lisp_Object operation, document, parameters, show_flag;
8062 {
8063 Lisp_Object current_dir;
8064
8065 CHECK_STRING (document);
8066
8067 /* Encode filename and current directory. */
8068 current_dir = ENCODE_FILE (current_buffer->directory);
8069 document = ENCODE_FILE (document);
8070 if ((int) ShellExecute (NULL,
8071 (STRINGP (operation) ?
8072 SDATA (operation) : NULL),
8073 SDATA (document),
8074 (STRINGP (parameters) ?
8075 SDATA (parameters) : NULL),
8076 SDATA (current_dir),
8077 (INTEGERP (show_flag) ?
8078 XINT (show_flag) : SW_SHOWDEFAULT))
8079 > 32)
8080 return Qt;
8081 error ("ShellExecute failed: %s", w32_strerror (0));
8082 }
8083
8084 /* Lookup virtual keycode from string representing the name of a
8085 non-ascii keystroke into the corresponding virtual key, using
8086 lispy_function_keys. */
8087 static int
8088 lookup_vk_code (char *key)
8089 {
8090 int i;
8091
8092 for (i = 0; i < 256; i++)
8093 if (lispy_function_keys[i] != 0
8094 && strcmp (lispy_function_keys[i], key) == 0)
8095 return i;
8096
8097 return -1;
8098 }
8099
8100 /* Convert a one-element vector style key sequence to a hot key
8101 definition. */
8102 static Lisp_Object
8103 w32_parse_hot_key (key)
8104 Lisp_Object key;
8105 {
8106 /* Copied from Fdefine_key and store_in_keymap. */
8107 register Lisp_Object c;
8108 int vk_code;
8109 int lisp_modifiers;
8110 int w32_modifiers;
8111 struct gcpro gcpro1;
8112
8113 CHECK_VECTOR (key);
8114
8115 if (XFASTINT (Flength (key)) != 1)
8116 return Qnil;
8117
8118 GCPRO1 (key);
8119
8120 c = Faref (key, make_number (0));
8121
8122 if (CONSP (c) && lucid_event_type_list_p (c))
8123 c = Fevent_convert_list (c);
8124
8125 UNGCPRO;
8126
8127 if (! INTEGERP (c) && ! SYMBOLP (c))
8128 error ("Key definition is invalid");
8129
8130 /* Work out the base key and the modifiers. */
8131 if (SYMBOLP (c))
8132 {
8133 c = parse_modifiers (c);
8134 lisp_modifiers = XINT (Fcar (Fcdr (c)));
8135 c = Fcar (c);
8136 if (!SYMBOLP (c))
8137 abort ();
8138 vk_code = lookup_vk_code (SDATA (SYMBOL_NAME (c)));
8139 }
8140 else if (INTEGERP (c))
8141 {
8142 lisp_modifiers = XINT (c) & ~CHARACTERBITS;
8143 /* Many ascii characters are their own virtual key code. */
8144 vk_code = XINT (c) & CHARACTERBITS;
8145 }
8146
8147 if (vk_code < 0 || vk_code > 255)
8148 return Qnil;
8149
8150 if ((lisp_modifiers & meta_modifier) != 0
8151 && !NILP (Vw32_alt_is_meta))
8152 lisp_modifiers |= alt_modifier;
8153
8154 /* Supply defs missing from mingw32. */
8155 #ifndef MOD_ALT
8156 #define MOD_ALT 0x0001
8157 #define MOD_CONTROL 0x0002
8158 #define MOD_SHIFT 0x0004
8159 #define MOD_WIN 0x0008
8160 #endif
8161
8162 /* Convert lisp modifiers to Windows hot-key form. */
8163 w32_modifiers = (lisp_modifiers & hyper_modifier) ? MOD_WIN : 0;
8164 w32_modifiers |= (lisp_modifiers & alt_modifier) ? MOD_ALT : 0;
8165 w32_modifiers |= (lisp_modifiers & ctrl_modifier) ? MOD_CONTROL : 0;
8166 w32_modifiers |= (lisp_modifiers & shift_modifier) ? MOD_SHIFT : 0;
8167
8168 return HOTKEY (vk_code, w32_modifiers);
8169 }
8170
8171 DEFUN ("w32-register-hot-key", Fw32_register_hot_key,
8172 Sw32_register_hot_key, 1, 1, 0,
8173 doc: /* Register KEY as a hot-key combination.
8174 Certain key combinations like Alt-Tab are reserved for system use on
8175 Windows, and therefore are normally intercepted by the system. However,
8176 most of these key combinations can be received by registering them as
8177 hot-keys, overriding their special meaning.
8178
8179 KEY must be a one element key definition in vector form that would be
8180 acceptable to `define-key' (e.g. [A-tab] for Alt-Tab). The meta
8181 modifier is interpreted as Alt if `w32-alt-is-meta' is t, and hyper
8182 is always interpreted as the Windows modifier keys.
8183
8184 The return value is the hotkey-id if registered, otherwise nil. */)
8185 (key)
8186 Lisp_Object key;
8187 {
8188 key = w32_parse_hot_key (key);
8189
8190 if (NILP (Fmemq (key, w32_grabbed_keys)))
8191 {
8192 /* Reuse an empty slot if possible. */
8193 Lisp_Object item = Fmemq (Qnil, w32_grabbed_keys);
8194
8195 /* Safe to add new key to list, even if we have focus. */
8196 if (NILP (item))
8197 w32_grabbed_keys = Fcons (key, w32_grabbed_keys);
8198 else
8199 XSETCAR (item, key);
8200
8201 /* Notify input thread about new hot-key definition, so that it
8202 takes effect without needing to switch focus. */
8203 #ifdef USE_LISP_UNION_TYPE
8204 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
8205 (WPARAM) key.i, 0);
8206 #else
8207 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
8208 (WPARAM) key, 0);
8209 #endif
8210 }
8211
8212 return key;
8213 }
8214
8215 DEFUN ("w32-unregister-hot-key", Fw32_unregister_hot_key,
8216 Sw32_unregister_hot_key, 1, 1, 0,
8217 doc: /* Unregister KEY as a hot-key combination. */)
8218 (key)
8219 Lisp_Object key;
8220 {
8221 Lisp_Object item;
8222
8223 if (!INTEGERP (key))
8224 key = w32_parse_hot_key (key);
8225
8226 item = Fmemq (key, w32_grabbed_keys);
8227
8228 if (!NILP (item))
8229 {
8230 /* Notify input thread about hot-key definition being removed, so
8231 that it takes effect without needing focus switch. */
8232 #ifdef USE_LISP_UNION_TYPE
8233 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
8234 (WPARAM) XINT (XCAR (item)), (LPARAM) item.i))
8235 #else
8236 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
8237 (WPARAM) XINT (XCAR (item)), (LPARAM) item))
8238
8239 #endif
8240 {
8241 MSG msg;
8242 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
8243 }
8244 return Qt;
8245 }
8246 return Qnil;
8247 }
8248
8249 DEFUN ("w32-registered-hot-keys", Fw32_registered_hot_keys,
8250 Sw32_registered_hot_keys, 0, 0, 0,
8251 doc: /* Return list of registered hot-key IDs. */)
8252 ()
8253 {
8254 return Fcopy_sequence (w32_grabbed_keys);
8255 }
8256
8257 DEFUN ("w32-reconstruct-hot-key", Fw32_reconstruct_hot_key,
8258 Sw32_reconstruct_hot_key, 1, 1, 0,
8259 doc: /* Convert hot-key ID to a lisp key combination.
8260 usage: (w32-reconstruct-hot-key ID) */)
8261 (hotkeyid)
8262 Lisp_Object hotkeyid;
8263 {
8264 int vk_code, w32_modifiers;
8265 Lisp_Object key;
8266
8267 CHECK_NUMBER (hotkeyid);
8268
8269 vk_code = HOTKEY_VK_CODE (hotkeyid);
8270 w32_modifiers = HOTKEY_MODIFIERS (hotkeyid);
8271
8272 if (lispy_function_keys[vk_code])
8273 key = intern (lispy_function_keys[vk_code]);
8274 else
8275 key = make_number (vk_code);
8276
8277 key = Fcons (key, Qnil);
8278 if (w32_modifiers & MOD_SHIFT)
8279 key = Fcons (Qshift, key);
8280 if (w32_modifiers & MOD_CONTROL)
8281 key = Fcons (Qctrl, key);
8282 if (w32_modifiers & MOD_ALT)
8283 key = Fcons (NILP (Vw32_alt_is_meta) ? Qalt : Qmeta, key);
8284 if (w32_modifiers & MOD_WIN)
8285 key = Fcons (Qhyper, key);
8286
8287 return key;
8288 }
8289
8290 DEFUN ("w32-toggle-lock-key", Fw32_toggle_lock_key,
8291 Sw32_toggle_lock_key, 1, 2, 0,
8292 doc: /* Toggle the state of the lock key KEY.
8293 KEY can be `capslock', `kp-numlock', or `scroll'.
8294 If the optional parameter NEW-STATE is a number, then the state of KEY
8295 is set to off if the low bit of NEW-STATE is zero, otherwise on. */)
8296 (key, new_state)
8297 Lisp_Object key, new_state;
8298 {
8299 int vk_code;
8300
8301 if (EQ (key, intern ("capslock")))
8302 vk_code = VK_CAPITAL;
8303 else if (EQ (key, intern ("kp-numlock")))
8304 vk_code = VK_NUMLOCK;
8305 else if (EQ (key, intern ("scroll")))
8306 vk_code = VK_SCROLL;
8307 else
8308 return Qnil;
8309
8310 if (!dwWindowsThreadId)
8311 return make_number (w32_console_toggle_lock_key (vk_code, new_state));
8312
8313 #ifdef USE_LISP_UNION_TYPE
8314 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
8315 (WPARAM) vk_code, (LPARAM) new_state.i))
8316 #else
8317 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
8318 (WPARAM) vk_code, (LPARAM) new_state))
8319 #endif
8320 {
8321 MSG msg;
8322 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
8323 return make_number (msg.wParam);
8324 }
8325 return Qnil;
8326 }
8327 \f
8328 DEFUN ("file-system-info", Ffile_system_info, Sfile_system_info, 1, 1, 0,
8329 doc: /* Return storage information about the file system FILENAME is on.
8330 Value is a list of floats (TOTAL FREE AVAIL), where TOTAL is the total
8331 storage of the file system, FREE is the free storage, and AVAIL is the
8332 storage available to a non-superuser. All 3 numbers are in bytes.
8333 If the underlying system call fails, value is nil. */)
8334 (filename)
8335 Lisp_Object filename;
8336 {
8337 Lisp_Object encoded, value;
8338
8339 CHECK_STRING (filename);
8340 filename = Fexpand_file_name (filename, Qnil);
8341 encoded = ENCODE_FILE (filename);
8342
8343 value = Qnil;
8344
8345 /* Determining the required information on Windows turns out, sadly,
8346 to be more involved than one would hope. The original Win32 api
8347 call for this will return bogus information on some systems, but we
8348 must dynamically probe for the replacement api, since that was
8349 added rather late on. */
8350 {
8351 HMODULE hKernel = GetModuleHandle ("kernel32");
8352 BOOL (*pfn_GetDiskFreeSpaceEx)
8353 (char *, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER)
8354 = (void *) GetProcAddress (hKernel, "GetDiskFreeSpaceEx");
8355
8356 /* On Windows, we may need to specify the root directory of the
8357 volume holding FILENAME. */
8358 char rootname[MAX_PATH];
8359 char *name = SDATA (encoded);
8360
8361 /* find the root name of the volume if given */
8362 if (isalpha (name[0]) && name[1] == ':')
8363 {
8364 rootname[0] = name[0];
8365 rootname[1] = name[1];
8366 rootname[2] = '\\';
8367 rootname[3] = 0;
8368 }
8369 else if (IS_DIRECTORY_SEP (name[0]) && IS_DIRECTORY_SEP (name[1]))
8370 {
8371 char *str = rootname;
8372 int slashes = 4;
8373 do
8374 {
8375 if (IS_DIRECTORY_SEP (*name) && --slashes == 0)
8376 break;
8377 *str++ = *name++;
8378 }
8379 while ( *name );
8380
8381 *str++ = '\\';
8382 *str = 0;
8383 }
8384
8385 if (pfn_GetDiskFreeSpaceEx)
8386 {
8387 /* Unsigned large integers cannot be cast to double, so
8388 use signed ones instead. */
8389 LARGE_INTEGER availbytes;
8390 LARGE_INTEGER freebytes;
8391 LARGE_INTEGER totalbytes;
8392
8393 if (pfn_GetDiskFreeSpaceEx(rootname,
8394 (ULARGE_INTEGER *)&availbytes,
8395 (ULARGE_INTEGER *)&totalbytes,
8396 (ULARGE_INTEGER *)&freebytes))
8397 value = list3 (make_float ((double) totalbytes.QuadPart),
8398 make_float ((double) freebytes.QuadPart),
8399 make_float ((double) availbytes.QuadPart));
8400 }
8401 else
8402 {
8403 DWORD sectors_per_cluster;
8404 DWORD bytes_per_sector;
8405 DWORD free_clusters;
8406 DWORD total_clusters;
8407
8408 if (GetDiskFreeSpace(rootname,
8409 &sectors_per_cluster,
8410 &bytes_per_sector,
8411 &free_clusters,
8412 &total_clusters))
8413 value = list3 (make_float ((double) total_clusters
8414 * sectors_per_cluster * bytes_per_sector),
8415 make_float ((double) free_clusters
8416 * sectors_per_cluster * bytes_per_sector),
8417 make_float ((double) free_clusters
8418 * sectors_per_cluster * bytes_per_sector));
8419 }
8420 }
8421
8422 return value;
8423 }
8424 \f
8425 DEFUN ("default-printer-name", Fdefault_printer_name, Sdefault_printer_name,
8426 0, 0, 0, doc: /* Return the name of Windows default printer device. */)
8427 ()
8428 {
8429 static char pname_buf[256];
8430 int err;
8431 HANDLE hPrn;
8432 PRINTER_INFO_2 *ppi2 = NULL;
8433 DWORD dwNeeded = 0, dwReturned = 0;
8434
8435 /* Retrieve the default string from Win.ini (the registry).
8436 * String will be in form "printername,drivername,portname".
8437 * This is the most portable way to get the default printer. */
8438 if (GetProfileString ("windows", "device", ",,", pname_buf, sizeof (pname_buf)) <= 0)
8439 return Qnil;
8440 /* printername precedes first "," character */
8441 strtok (pname_buf, ",");
8442 /* We want to know more than the printer name */
8443 if (!OpenPrinter (pname_buf, &hPrn, NULL))
8444 return Qnil;
8445 GetPrinter (hPrn, 2, NULL, 0, &dwNeeded);
8446 if (dwNeeded == 0)
8447 {
8448 ClosePrinter (hPrn);
8449 return Qnil;
8450 }
8451 /* Allocate memory for the PRINTER_INFO_2 struct */
8452 ppi2 = (PRINTER_INFO_2 *) xmalloc (dwNeeded);
8453 if (!ppi2)
8454 {
8455 ClosePrinter (hPrn);
8456 return Qnil;
8457 }
8458 /* Call GetPrinter() again with big enouth memory block */
8459 err = GetPrinter (hPrn, 2, (LPBYTE)ppi2, dwNeeded, &dwReturned);
8460 ClosePrinter (hPrn);
8461 if (!err)
8462 {
8463 xfree(ppi2);
8464 return Qnil;
8465 }
8466
8467 if (ppi2)
8468 {
8469 if (ppi2->Attributes & PRINTER_ATTRIBUTE_SHARED && ppi2->pServerName)
8470 {
8471 /* a remote printer */
8472 if (*ppi2->pServerName == '\\')
8473 _snprintf(pname_buf, sizeof (pname_buf), "%s\\%s", ppi2->pServerName,
8474 ppi2->pShareName);
8475 else
8476 _snprintf(pname_buf, sizeof (pname_buf), "\\\\%s\\%s", ppi2->pServerName,
8477 ppi2->pShareName);
8478 pname_buf[sizeof (pname_buf) - 1] = '\0';
8479 }
8480 else
8481 {
8482 /* a local printer */
8483 strncpy(pname_buf, ppi2->pPortName, sizeof (pname_buf));
8484 pname_buf[sizeof (pname_buf) - 1] = '\0';
8485 /* `pPortName' can include several ports, delimited by ','.
8486 * we only use the first one. */
8487 strtok(pname_buf, ",");
8488 }
8489 xfree(ppi2);
8490 }
8491
8492 return build_string (pname_buf);
8493 }
8494 \f
8495 /***********************************************************************
8496 Initialization
8497 ***********************************************************************/
8498
8499 /* Keep this list in the same order as frame_parms in frame.c.
8500 Use 0 for unsupported frame parameters. */
8501
8502 frame_parm_handler w32_frame_parm_handlers[] =
8503 {
8504 x_set_autoraise,
8505 x_set_autolower,
8506 x_set_background_color,
8507 x_set_border_color,
8508 x_set_border_width,
8509 x_set_cursor_color,
8510 x_set_cursor_type,
8511 x_set_font,
8512 x_set_foreground_color,
8513 x_set_icon_name,
8514 x_set_icon_type,
8515 x_set_internal_border_width,
8516 x_set_menu_bar_lines,
8517 x_set_mouse_color,
8518 x_explicitly_set_name,
8519 x_set_scroll_bar_width,
8520 x_set_title,
8521 x_set_unsplittable,
8522 x_set_vertical_scroll_bars,
8523 x_set_visibility,
8524 x_set_tool_bar_lines,
8525 0, /* x_set_scroll_bar_foreground, */
8526 0, /* x_set_scroll_bar_background, */
8527 x_set_screen_gamma,
8528 x_set_line_spacing,
8529 x_set_fringe_width,
8530 x_set_fringe_width,
8531 0, /* x_set_wait_for_wm, */
8532 x_set_fullscreen,
8533 };
8534
8535 void
8536 syms_of_w32fns ()
8537 {
8538 globals_of_w32fns ();
8539 /* This is zero if not using MS-Windows. */
8540 w32_in_use = 0;
8541 track_mouse_window = NULL;
8542
8543 w32_visible_system_caret_hwnd = NULL;
8544
8545 Qnone = intern ("none");
8546 staticpro (&Qnone);
8547 Qsuppress_icon = intern ("suppress-icon");
8548 staticpro (&Qsuppress_icon);
8549 Qundefined_color = intern ("undefined-color");
8550 staticpro (&Qundefined_color);
8551 Qcancel_timer = intern ("cancel-timer");
8552 staticpro (&Qcancel_timer);
8553
8554 Qhyper = intern ("hyper");
8555 staticpro (&Qhyper);
8556 Qsuper = intern ("super");
8557 staticpro (&Qsuper);
8558 Qmeta = intern ("meta");
8559 staticpro (&Qmeta);
8560 Qalt = intern ("alt");
8561 staticpro (&Qalt);
8562 Qctrl = intern ("ctrl");
8563 staticpro (&Qctrl);
8564 Qcontrol = intern ("control");
8565 staticpro (&Qcontrol);
8566 Qshift = intern ("shift");
8567 staticpro (&Qshift);
8568 /* This is the end of symbol initialization. */
8569
8570 /* Text property `display' should be nonsticky by default. */
8571 Vtext_property_default_nonsticky
8572 = Fcons (Fcons (Qdisplay, Qt), Vtext_property_default_nonsticky);
8573
8574
8575 Fput (Qundefined_color, Qerror_conditions,
8576 Fcons (Qundefined_color, Fcons (Qerror, Qnil)));
8577 Fput (Qundefined_color, Qerror_message,
8578 build_string ("Undefined color"));
8579
8580 staticpro (&w32_grabbed_keys);
8581 w32_grabbed_keys = Qnil;
8582
8583 DEFVAR_LISP ("w32-color-map", &Vw32_color_map,
8584 doc: /* An array of color name mappings for Windows. */);
8585 Vw32_color_map = Qnil;
8586
8587 DEFVAR_LISP ("w32-pass-alt-to-system", &Vw32_pass_alt_to_system,
8588 doc: /* Non-nil if alt key presses are passed on to Windows.
8589 When non-nil, for example, alt pressed and released and then space will
8590 open the System menu. When nil, Emacs silently swallows alt key events. */);
8591 Vw32_pass_alt_to_system = Qnil;
8592
8593 DEFVAR_LISP ("w32-alt-is-meta", &Vw32_alt_is_meta,
8594 doc: /* Non-nil if the alt key is to be considered the same as the meta key.
8595 When nil, Emacs will translate the alt key to the Alt modifier, and not Meta. */);
8596 Vw32_alt_is_meta = Qt;
8597
8598 DEFVAR_INT ("w32-quit-key", &w32_quit_key,
8599 doc: /* If non-zero, the virtual key code for an alternative quit key. */);
8600 w32_quit_key = 0;
8601
8602 DEFVAR_LISP ("w32-pass-lwindow-to-system",
8603 &Vw32_pass_lwindow_to_system,
8604 doc: /* Non-nil if the left \"Windows\" key is passed on to Windows.
8605 When non-nil, the Start menu is opened by tapping the key. */);
8606 Vw32_pass_lwindow_to_system = Qt;
8607
8608 DEFVAR_LISP ("w32-pass-rwindow-to-system",
8609 &Vw32_pass_rwindow_to_system,
8610 doc: /* Non-nil if the right \"Windows\" key is passed on to Windows.
8611 When non-nil, the Start menu is opened by tapping the key. */);
8612 Vw32_pass_rwindow_to_system = Qt;
8613
8614 DEFVAR_LISP ("w32-phantom-key-code",
8615 &Vw32_phantom_key_code,
8616 doc: /* Virtual key code used to generate \"phantom\" key presses.
8617 Value is a number between 0 and 255.
8618
8619 Phantom key presses are generated in order to stop the system from
8620 acting on \"Windows\" key events when `w32-pass-lwindow-to-system' or
8621 `w32-pass-rwindow-to-system' is nil. */);
8622 /* Although 255 is technically not a valid key code, it works and
8623 means that this hack won't interfere with any real key code. */
8624 XSETINT (Vw32_phantom_key_code, 255);
8625
8626 DEFVAR_LISP ("w32-enable-num-lock",
8627 &Vw32_enable_num_lock,
8628 doc: /* Non-nil if Num Lock should act normally.
8629 Set to nil to see Num Lock as the key `kp-numlock'. */);
8630 Vw32_enable_num_lock = Qt;
8631
8632 DEFVAR_LISP ("w32-enable-caps-lock",
8633 &Vw32_enable_caps_lock,
8634 doc: /* Non-nil if Caps Lock should act normally.
8635 Set to nil to see Caps Lock as the key `capslock'. */);
8636 Vw32_enable_caps_lock = Qt;
8637
8638 DEFVAR_LISP ("w32-scroll-lock-modifier",
8639 &Vw32_scroll_lock_modifier,
8640 doc: /* Modifier to use for the Scroll Lock on state.
8641 The value can be hyper, super, meta, alt, control or shift for the
8642 respective modifier, or nil to see Scroll Lock as the key `scroll'.
8643 Any other value will cause the key to be ignored. */);
8644 Vw32_scroll_lock_modifier = Qt;
8645
8646 DEFVAR_LISP ("w32-lwindow-modifier",
8647 &Vw32_lwindow_modifier,
8648 doc: /* Modifier to use for the left \"Windows\" key.
8649 The value can be hyper, super, meta, alt, control or shift for the
8650 respective modifier, or nil to appear as the key `lwindow'.
8651 Any other value will cause the key to be ignored. */);
8652 Vw32_lwindow_modifier = Qnil;
8653
8654 DEFVAR_LISP ("w32-rwindow-modifier",
8655 &Vw32_rwindow_modifier,
8656 doc: /* Modifier to use for the right \"Windows\" key.
8657 The value can be hyper, super, meta, alt, control or shift for the
8658 respective modifier, or nil to appear as the key `rwindow'.
8659 Any other value will cause the key to be ignored. */);
8660 Vw32_rwindow_modifier = Qnil;
8661
8662 DEFVAR_LISP ("w32-apps-modifier",
8663 &Vw32_apps_modifier,
8664 doc: /* Modifier to use for the \"Apps\" key.
8665 The value can be hyper, super, meta, alt, control or shift for the
8666 respective modifier, or nil to appear as the key `apps'.
8667 Any other value will cause the key to be ignored. */);
8668 Vw32_apps_modifier = Qnil;
8669
8670 DEFVAR_BOOL ("w32-enable-synthesized-fonts", &w32_enable_synthesized_fonts,
8671 doc: /* Non-nil enables selection of artificially italicized and bold fonts. */);
8672 w32_enable_synthesized_fonts = 0;
8673
8674 DEFVAR_LISP ("w32-enable-palette", &Vw32_enable_palette,
8675 doc: /* Non-nil enables Windows palette management to map colors exactly. */);
8676 Vw32_enable_palette = Qt;
8677
8678 DEFVAR_INT ("w32-mouse-button-tolerance",
8679 &w32_mouse_button_tolerance,
8680 doc: /* Analogue of double click interval for faking middle mouse events.
8681 The value is the minimum time in milliseconds that must elapse between
8682 left/right button down events before they are considered distinct events.
8683 If both mouse buttons are depressed within this interval, a middle mouse
8684 button down event is generated instead. */);
8685 w32_mouse_button_tolerance = GetDoubleClickTime () / 2;
8686
8687 DEFVAR_INT ("w32-mouse-move-interval",
8688 &w32_mouse_move_interval,
8689 doc: /* Minimum interval between mouse move events.
8690 The value is the minimum time in milliseconds that must elapse between
8691 successive mouse move (or scroll bar drag) events before they are
8692 reported as lisp events. */);
8693 w32_mouse_move_interval = 0;
8694
8695 DEFVAR_BOOL ("w32-pass-extra-mouse-buttons-to-system",
8696 &w32_pass_extra_mouse_buttons_to_system,
8697 doc: /* Non-nil if the fourth and fifth mouse buttons are passed to Windows.
8698 Recent versions of Windows support mice with up to five buttons.
8699 Since most applications don't support these extra buttons, most mouse
8700 drivers will allow you to map them to functions at the system level.
8701 If this variable is non-nil, Emacs will pass them on, allowing the
8702 system to handle them. */);
8703 w32_pass_extra_mouse_buttons_to_system = 0;
8704
8705 DEFVAR_LISP ("x-pointer-shape", &Vx_pointer_shape,
8706 doc: /* The shape of the pointer when over text.
8707 Changing the value does not affect existing frames
8708 unless you set the mouse color. */);
8709 Vx_pointer_shape = Qnil;
8710
8711 Vx_nontext_pointer_shape = Qnil;
8712
8713 Vx_mode_pointer_shape = Qnil;
8714
8715 DEFVAR_LISP ("x-hourglass-pointer-shape", &Vx_hourglass_pointer_shape,
8716 doc: /* The shape of the pointer when Emacs is busy.
8717 This variable takes effect when you create a new frame
8718 or when you set the mouse color. */);
8719 Vx_hourglass_pointer_shape = Qnil;
8720
8721 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
8722 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
8723 display_hourglass_p = 1;
8724
8725 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
8726 doc: /* *Seconds to wait before displaying an hourglass pointer.
8727 Value must be an integer or float. */);
8728 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
8729
8730 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
8731 &Vx_sensitive_text_pointer_shape,
8732 doc: /* The shape of the pointer when over mouse-sensitive text.
8733 This variable takes effect when you create a new frame
8734 or when you set the mouse color. */);
8735 Vx_sensitive_text_pointer_shape = Qnil;
8736
8737 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
8738 &Vx_window_horizontal_drag_shape,
8739 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
8740 This variable takes effect when you create a new frame
8741 or when you set the mouse color. */);
8742 Vx_window_horizontal_drag_shape = Qnil;
8743
8744 DEFVAR_LISP ("x-cursor-fore-pixel", &Vx_cursor_fore_pixel,
8745 doc: /* A string indicating the foreground color of the cursor box. */);
8746 Vx_cursor_fore_pixel = Qnil;
8747
8748 DEFVAR_LISP ("x-max-tooltip-size", &Vx_max_tooltip_size,
8749 doc: /* Maximum size for tooltips.
8750 Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
8751 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
8752
8753 DEFVAR_LISP ("x-no-window-manager", &Vx_no_window_manager,
8754 doc: /* Non-nil if no window manager is in use.
8755 Emacs doesn't try to figure this out; this is always nil
8756 unless you set it to something else. */);
8757 /* We don't have any way to find this out, so set it to nil
8758 and maybe the user would like to set it to t. */
8759 Vx_no_window_manager = Qnil;
8760
8761 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
8762 &Vx_pixel_size_width_font_regexp,
8763 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
8764
8765 Since Emacs gets width of a font matching with this regexp from
8766 PIXEL_SIZE field of the name, font finding mechanism gets faster for
8767 such a font. This is especially effective for such large fonts as
8768 Chinese, Japanese, and Korean. */);
8769 Vx_pixel_size_width_font_regexp = Qnil;
8770
8771 DEFVAR_LISP ("w32-bdf-filename-alist",
8772 &Vw32_bdf_filename_alist,
8773 doc: /* List of bdf fonts and their corresponding filenames. */);
8774 Vw32_bdf_filename_alist = Qnil;
8775
8776 DEFVAR_BOOL ("w32-strict-fontnames",
8777 &w32_strict_fontnames,
8778 doc: /* Non-nil means only use fonts that are exact matches for those requested.
8779 Default is nil, which allows old fontnames that are not XLFD compliant,
8780 and allows third-party CJK display to work by specifying false charset
8781 fields to trick Emacs into translating to Big5, SJIS etc.
8782 Setting this to t will prevent wrong fonts being selected when
8783 fontsets are automatically created. */);
8784 w32_strict_fontnames = 0;
8785
8786 DEFVAR_BOOL ("w32-strict-painting",
8787 &w32_strict_painting,
8788 doc: /* Non-nil means use strict rules for repainting frames.
8789 Set this to nil to get the old behavior for repainting; this should
8790 only be necessary if the default setting causes problems. */);
8791 w32_strict_painting = 1;
8792
8793 DEFVAR_LISP ("w32-charset-info-alist",
8794 &Vw32_charset_info_alist,
8795 doc: /* Alist linking Emacs character sets to Windows fonts and codepages.
8796 Each entry should be of the form:
8797
8798 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE))
8799
8800 where CHARSET_NAME is a string used in font names to identify the charset,
8801 WINDOWS_CHARSET is a symbol that can be one of:
8802 w32-charset-ansi, w32-charset-default, w32-charset-symbol,
8803 w32-charset-shiftjis, w32-charset-hangeul, w32-charset-gb2312,
8804 w32-charset-chinesebig5,
8805 w32-charset-johab, w32-charset-hebrew,
8806 w32-charset-arabic, w32-charset-greek, w32-charset-turkish,
8807 w32-charset-vietnamese, w32-charset-thai, w32-charset-easteurope,
8808 w32-charset-russian, w32-charset-mac, w32-charset-baltic,
8809 w32-charset-unicode,
8810 or w32-charset-oem.
8811 CODEPAGE should be an integer specifying the codepage that should be used
8812 to display the character set, t to do no translation and output as Unicode,
8813 or nil to do no translation and output as 8 bit (or multibyte on far-east
8814 versions of Windows) characters. */);
8815 Vw32_charset_info_alist = Qnil;
8816
8817 staticpro (&Qw32_charset_ansi);
8818 Qw32_charset_ansi = intern ("w32-charset-ansi");
8819 staticpro (&Qw32_charset_symbol);
8820 Qw32_charset_default = intern ("w32-charset-default");
8821 staticpro (&Qw32_charset_default);
8822 Qw32_charset_symbol = intern ("w32-charset-symbol");
8823 staticpro (&Qw32_charset_shiftjis);
8824 Qw32_charset_shiftjis = intern ("w32-charset-shiftjis");
8825 staticpro (&Qw32_charset_hangeul);
8826 Qw32_charset_hangeul = intern ("w32-charset-hangeul");
8827 staticpro (&Qw32_charset_chinesebig5);
8828 Qw32_charset_chinesebig5 = intern ("w32-charset-chinesebig5");
8829 staticpro (&Qw32_charset_gb2312);
8830 Qw32_charset_gb2312 = intern ("w32-charset-gb2312");
8831 staticpro (&Qw32_charset_oem);
8832 Qw32_charset_oem = intern ("w32-charset-oem");
8833
8834 #ifdef JOHAB_CHARSET
8835 {
8836 static int w32_extra_charsets_defined = 1;
8837 DEFVAR_BOOL ("w32-extra-charsets-defined", &w32_extra_charsets_defined,
8838 doc: /* Internal variable. */);
8839
8840 staticpro (&Qw32_charset_johab);
8841 Qw32_charset_johab = intern ("w32-charset-johab");
8842 staticpro (&Qw32_charset_easteurope);
8843 Qw32_charset_easteurope = intern ("w32-charset-easteurope");
8844 staticpro (&Qw32_charset_turkish);
8845 Qw32_charset_turkish = intern ("w32-charset-turkish");
8846 staticpro (&Qw32_charset_baltic);
8847 Qw32_charset_baltic = intern ("w32-charset-baltic");
8848 staticpro (&Qw32_charset_russian);
8849 Qw32_charset_russian = intern ("w32-charset-russian");
8850 staticpro (&Qw32_charset_arabic);
8851 Qw32_charset_arabic = intern ("w32-charset-arabic");
8852 staticpro (&Qw32_charset_greek);
8853 Qw32_charset_greek = intern ("w32-charset-greek");
8854 staticpro (&Qw32_charset_hebrew);
8855 Qw32_charset_hebrew = intern ("w32-charset-hebrew");
8856 staticpro (&Qw32_charset_vietnamese);
8857 Qw32_charset_vietnamese = intern ("w32-charset-vietnamese");
8858 staticpro (&Qw32_charset_thai);
8859 Qw32_charset_thai = intern ("w32-charset-thai");
8860 staticpro (&Qw32_charset_mac);
8861 Qw32_charset_mac = intern ("w32-charset-mac");
8862 }
8863 #endif
8864
8865 #ifdef UNICODE_CHARSET
8866 {
8867 static int w32_unicode_charset_defined = 1;
8868 DEFVAR_BOOL ("w32-unicode-charset-defined",
8869 &w32_unicode_charset_defined,
8870 doc: /* Internal variable. */);
8871
8872 staticpro (&Qw32_charset_unicode);
8873 Qw32_charset_unicode = intern ("w32-charset-unicode");
8874 #endif
8875
8876 #if 0 /* TODO: Port to W32 */
8877 defsubr (&Sx_change_window_property);
8878 defsubr (&Sx_delete_window_property);
8879 defsubr (&Sx_window_property);
8880 #endif
8881 defsubr (&Sxw_display_color_p);
8882 defsubr (&Sx_display_grayscale_p);
8883 defsubr (&Sxw_color_defined_p);
8884 defsubr (&Sxw_color_values);
8885 defsubr (&Sx_server_max_request_size);
8886 defsubr (&Sx_server_vendor);
8887 defsubr (&Sx_server_version);
8888 defsubr (&Sx_display_pixel_width);
8889 defsubr (&Sx_display_pixel_height);
8890 defsubr (&Sx_display_mm_width);
8891 defsubr (&Sx_display_mm_height);
8892 defsubr (&Sx_display_screens);
8893 defsubr (&Sx_display_planes);
8894 defsubr (&Sx_display_color_cells);
8895 defsubr (&Sx_display_visual_class);
8896 defsubr (&Sx_display_backing_store);
8897 defsubr (&Sx_display_save_under);
8898 defsubr (&Sx_create_frame);
8899 defsubr (&Sx_open_connection);
8900 defsubr (&Sx_close_connection);
8901 defsubr (&Sx_display_list);
8902 defsubr (&Sx_synchronize);
8903
8904 /* W32 specific functions */
8905
8906 defsubr (&Sw32_focus_frame);
8907 defsubr (&Sw32_select_font);
8908 defsubr (&Sw32_define_rgb_color);
8909 defsubr (&Sw32_default_color_map);
8910 defsubr (&Sw32_load_color_file);
8911 defsubr (&Sw32_send_sys_command);
8912 defsubr (&Sw32_shell_execute);
8913 defsubr (&Sw32_register_hot_key);
8914 defsubr (&Sw32_unregister_hot_key);
8915 defsubr (&Sw32_registered_hot_keys);
8916 defsubr (&Sw32_reconstruct_hot_key);
8917 defsubr (&Sw32_toggle_lock_key);
8918 defsubr (&Sw32_find_bdf_fonts);
8919
8920 defsubr (&Sfile_system_info);
8921 defsubr (&Sdefault_printer_name);
8922
8923 /* Setting callback functions for fontset handler. */
8924 get_font_info_func = w32_get_font_info;
8925
8926 #if 0 /* This function pointer doesn't seem to be used anywhere.
8927 And the pointer assigned has the wrong type, anyway. */
8928 list_fonts_func = w32_list_fonts;
8929 #endif
8930
8931 load_font_func = w32_load_font;
8932 find_ccl_program_func = w32_find_ccl_program;
8933 query_font_func = w32_query_font;
8934 set_frame_fontset_func = x_set_font;
8935 check_window_system_func = check_w32;
8936
8937
8938 hourglass_atimer = NULL;
8939 hourglass_shown_p = 0;
8940 defsubr (&Sx_show_tip);
8941 defsubr (&Sx_hide_tip);
8942 tip_timer = Qnil;
8943 staticpro (&tip_timer);
8944 tip_frame = Qnil;
8945 staticpro (&tip_frame);
8946
8947 last_show_tip_args = Qnil;
8948 staticpro (&last_show_tip_args);
8949
8950 defsubr (&Sx_file_dialog);
8951 }
8952
8953
8954 /*
8955 globals_of_w32fns is used to initialize those global variables that
8956 must always be initialized on startup even when the global variable
8957 initialized is non zero (see the function main in emacs.c).
8958 globals_of_w32fns is called from syms_of_w32fns when the global
8959 variable initialized is 0 and directly from main when initialized
8960 is non zero.
8961 */
8962 void globals_of_w32fns ()
8963 {
8964 HMODULE user32_lib = GetModuleHandle ("user32.dll");
8965 /*
8966 TrackMouseEvent not available in all versions of Windows, so must load
8967 it dynamically. Do it once, here, instead of every time it is used.
8968 */
8969 track_mouse_event_fn = (TrackMouseEvent_Proc)
8970 GetProcAddress (user32_lib, "TrackMouseEvent");
8971 /* ditto for GetClipboardSequenceNumber. */
8972 clipboard_sequence_fn = (ClipboardSequence_Proc)
8973 GetProcAddress (user32_lib, "GetClipboardSequenceNumber");
8974
8975 DEFVAR_INT ("w32-ansi-code-page",
8976 &w32_ansi_code_page,
8977 doc: /* The ANSI code page used by the system. */);
8978 w32_ansi_code_page = GetACP ();
8979 }
8980
8981 #undef abort
8982
8983 void w32_abort (void) NO_RETURN;
8984
8985 void
8986 w32_abort()
8987 {
8988 int button;
8989 button = MessageBox (NULL,
8990 "A fatal error has occurred!\n\n"
8991 "Would you like to attach a debugger?\n\n"
8992 "Select YES to debug, NO to abort Emacs"
8993 #if __GNUC__
8994 "\n\n(type \"gdb -p <emacs-PID>\" and\n"
8995 "\"continue\" inside GDB before clicking YES.)"
8996 #endif
8997 , "Emacs Abort Dialog",
8998 MB_ICONEXCLAMATION | MB_TASKMODAL
8999 | MB_SETFOREGROUND | MB_YESNO);
9000 switch (button)
9001 {
9002 case IDYES:
9003 DebugBreak ();
9004 exit (2); /* tell the compiler we will never return */
9005 case IDNO:
9006 default:
9007 abort ();
9008 break;
9009 }
9010 }
9011
9012 /* For convenience when debugging. */
9013 int
9014 w32_last_error()
9015 {
9016 return GetLastError ();
9017 }
9018
9019 /* arch-tag: 707589ab-b9be-4638-8cdd-74629cc9b446
9020 (do not change this comment) */