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