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