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