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