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