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