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