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