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