]> code.delx.au - gnu-emacs/blob - src/w32fns.c
Merge from emacs--devo--0
[gnu-emacs] / src / w32fns.c
1 /* Graphical user interface functions for the Microsoft W32 API.
2 Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
21
22 /* Added by Kevin Gallo */
23
24 #include <config.h>
25
26 #include <signal.h>
27 #include <stdio.h>
28 #include <limits.h>
29 #include <errno.h>
30 #include <math.h>
31
32 #include "lisp.h"
33 #include "w32term.h"
34 #include "frame.h"
35 #include "window.h"
36 #include "buffer.h"
37 #include "intervals.h"
38 #include "dispextern.h"
39 #include "keyboard.h"
40 #include "blockinput.h"
41 #include "epaths.h"
42 #include "character.h"
43 #include "charset.h"
44 #include "coding.h"
45 #include "ccl.h"
46 #include "fontset.h"
47 #include "systime.h"
48 #include "termhooks.h"
49 #include "w32heap.h"
50
51 #include "bitmaps/gray.xbm"
52
53 #include <commctrl.h>
54 #include <commdlg.h>
55 #include <shellapi.h>
56 #include <ctype.h>
57 #include <winspool.h>
58 #include <objbase.h>
59
60 #include <dlgs.h>
61 #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 /* Make copy of frame parameters because the original is in pure
4273 storage now. */
4274 parameters = Fcopy_alist (parameters);
4275
4276 /* Use this general default value to start with
4277 until we know if this frame has a specified name. */
4278 Vx_resource_name = Vinvocation_name;
4279
4280 display = w32_get_arg (parameters, Qdisplay, 0, 0, RES_TYPE_STRING);
4281 if (EQ (display, Qunbound))
4282 display = Qnil;
4283 dpyinfo = check_x_display_info (display);
4284 #ifdef MULTI_KBOARD
4285 kb = dpyinfo->terminal->kboard;
4286 #else
4287 kb = &the_only_kboard;
4288 #endif
4289
4290 name = w32_get_arg (parameters, Qname, "name", "Name", RES_TYPE_STRING);
4291 if (!STRINGP (name)
4292 && ! EQ (name, Qunbound)
4293 && ! NILP (name))
4294 error ("Invalid frame name--not a string or nil");
4295
4296 if (STRINGP (name))
4297 Vx_resource_name = name;
4298
4299 /* See if parent window is specified. */
4300 parent = w32_get_arg (parameters, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
4301 if (EQ (parent, Qunbound))
4302 parent = Qnil;
4303 if (! NILP (parent))
4304 CHECK_NUMBER (parent);
4305
4306 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
4307 /* No need to protect DISPLAY because that's not used after passing
4308 it to make_frame_without_minibuffer. */
4309 frame = Qnil;
4310 GCPRO4 (parameters, parent, name, frame);
4311 tem = w32_get_arg (parameters, Qminibuffer, "minibuffer", "Minibuffer",
4312 RES_TYPE_SYMBOL);
4313 if (EQ (tem, Qnone) || NILP (tem))
4314 f = make_frame_without_minibuffer (Qnil, kb, display);
4315 else if (EQ (tem, Qonly))
4316 {
4317 f = make_minibuffer_frame ();
4318 minibuffer_only = 1;
4319 }
4320 else if (WINDOWP (tem))
4321 f = make_frame_without_minibuffer (tem, kb, display);
4322 else
4323 f = make_frame (1);
4324
4325 XSETFRAME (frame, f);
4326
4327 /* Note that Windows does support scroll bars. */
4328 FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;
4329
4330 /* By default, make scrollbars the system standard width. */
4331 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = GetSystemMetrics (SM_CXVSCROLL);
4332
4333 f->terminal = dpyinfo->terminal;
4334 f->terminal->reference_count++;
4335
4336 f->output_method = output_w32;
4337 f->output_data.w32 =
4338 (struct w32_output *) xmalloc (sizeof (struct w32_output));
4339 bzero (f->output_data.w32, sizeof (struct w32_output));
4340 FRAME_FONTSET (f) = -1;
4341 record_unwind_protect (unwind_create_frame, frame);
4342
4343 f->icon_name
4344 = w32_get_arg (parameters, Qicon_name, "iconName", "Title", RES_TYPE_STRING);
4345 if (! STRINGP (f->icon_name))
4346 f->icon_name = Qnil;
4347
4348 /* FRAME_W32_DISPLAY_INFO (f) = dpyinfo; */
4349 #ifdef MULTI_KBOARD
4350 FRAME_KBOARD (f) = kb;
4351 #endif
4352
4353 /* Specify the parent under which to make this window. */
4354
4355 if (!NILP (parent))
4356 {
4357 f->output_data.w32->parent_desc = (Window) XFASTINT (parent);
4358 f->output_data.w32->explicit_parent = 1;
4359 }
4360 else
4361 {
4362 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
4363 f->output_data.w32->explicit_parent = 0;
4364 }
4365
4366 /* Set the name; the functions to which we pass f expect the name to
4367 be set. */
4368 if (EQ (name, Qunbound) || NILP (name))
4369 {
4370 f->name = build_string (dpyinfo->w32_id_name);
4371 f->explicit_name = 0;
4372 }
4373 else
4374 {
4375 f->name = name;
4376 f->explicit_name = 1;
4377 /* use the frame's title when getting resources for this frame. */
4378 specbind (Qx_resource_name, name);
4379 }
4380
4381 f->resx = dpyinfo->resx;
4382 f->resy = dpyinfo->resy;
4383
4384 #ifdef USE_FONT_BACKEND
4385 if (enable_font_backend)
4386 {
4387 /* Perhaps, we must allow frame parameter, say `font-backend',
4388 to specify which font backends to use. */
4389 register_font_driver (&w32font_driver, f);
4390
4391 x_default_parameter (f, parameters, Qfont_backend, Qnil,
4392 "fontBackend", "FontBackend", RES_TYPE_STRING);
4393 }
4394 #endif /* USE_FONT_BACKEND */
4395
4396 /* Extract the window parameters from the supplied values
4397 that are needed to determine window geometry. */
4398 #ifdef USE_FONT_BACKEND
4399 if (enable_font_backend)
4400 x_default_font_parameter (f, parameters);
4401 else
4402 #endif
4403 {
4404 Lisp_Object font;
4405
4406 font = w32_get_arg (parameters, Qfont, "font", "Font", RES_TYPE_STRING);
4407
4408 BLOCK_INPUT;
4409 /* First, try whatever font the caller has specified. */
4410 if (STRINGP (font))
4411 {
4412 tem = Fquery_fontset (font, Qnil);
4413 if (STRINGP (tem))
4414 font = x_new_fontset (f, tem);
4415 else
4416 font = x_new_font (f, SDATA (font));
4417 }
4418 /* Try out a font which we hope has bold and italic variations. */
4419 if (!STRINGP (font))
4420 font = x_new_font (f, "-*-Courier New-normal-r-*-*-*-100-*-*-c-*-iso8859-1");
4421 if (! STRINGP (font))
4422 font = x_new_font (f, "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1");
4423 /* If those didn't work, look for something which will at least work. */
4424 if (! STRINGP (font))
4425 font = x_new_font (f, "-*-Fixedsys-normal-r-*-*-12-*-*-*-c-*-iso8859-1");
4426 UNBLOCK_INPUT;
4427 if (! STRINGP (font))
4428 font = build_string ("Fixedsys");
4429
4430 x_default_parameter (f, parameters, Qfont, font,
4431 "font", "Font", RES_TYPE_STRING);
4432 }
4433
4434 x_default_parameter (f, parameters, Qborder_width, make_number (2),
4435 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
4436 /* This defaults to 2 in order to match xterm. We recognize either
4437 internalBorderWidth or internalBorder (which is what xterm calls
4438 it). */
4439 if (NILP (Fassq (Qinternal_border_width, parameters)))
4440 {
4441 Lisp_Object value;
4442
4443 value = w32_get_arg (parameters, Qinternal_border_width,
4444 "internalBorder", "InternalBorder", RES_TYPE_NUMBER);
4445 if (! EQ (value, Qunbound))
4446 parameters = Fcons (Fcons (Qinternal_border_width, value),
4447 parameters);
4448 }
4449 /* Default internalBorderWidth to 0 on Windows to match other programs. */
4450 x_default_parameter (f, parameters, Qinternal_border_width, make_number (0),
4451 "internalBorderWidth", "InternalBorder", RES_TYPE_NUMBER);
4452 x_default_parameter (f, parameters, Qvertical_scroll_bars, Qright,
4453 "verticalScrollBars", "ScrollBars", RES_TYPE_SYMBOL);
4454
4455 /* Also do the stuff which must be set before the window exists. */
4456 x_default_parameter (f, parameters, Qforeground_color, build_string ("black"),
4457 "foreground", "Foreground", RES_TYPE_STRING);
4458 x_default_parameter (f, parameters, Qbackground_color, build_string ("white"),
4459 "background", "Background", RES_TYPE_STRING);
4460 x_default_parameter (f, parameters, Qmouse_color, build_string ("black"),
4461 "pointerColor", "Foreground", RES_TYPE_STRING);
4462 x_default_parameter (f, parameters, Qcursor_color, build_string ("black"),
4463 "cursorColor", "Foreground", RES_TYPE_STRING);
4464 x_default_parameter (f, parameters, Qborder_color, build_string ("black"),
4465 "borderColor", "BorderColor", RES_TYPE_STRING);
4466 x_default_parameter (f, parameters, Qscreen_gamma, Qnil,
4467 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
4468 x_default_parameter (f, parameters, Qline_spacing, Qnil,
4469 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
4470 x_default_parameter (f, parameters, Qleft_fringe, Qnil,
4471 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
4472 x_default_parameter (f, parameters, Qright_fringe, Qnil,
4473 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
4474
4475
4476 /* Init faces before x_default_parameter is called for scroll-bar
4477 parameters because that function calls x_set_scroll_bar_width,
4478 which calls change_frame_size, which calls Fset_window_buffer,
4479 which runs hooks, which call Fvertical_motion. At the end, we
4480 end up in init_iterator with a null face cache, which should not
4481 happen. */
4482 init_frame_faces (f);
4483
4484 x_default_parameter (f, parameters, Qmenu_bar_lines, make_number (1),
4485 "menuBar", "MenuBar", RES_TYPE_NUMBER);
4486 x_default_parameter (f, parameters, Qtool_bar_lines, make_number (1),
4487 "toolBar", "ToolBar", RES_TYPE_NUMBER);
4488
4489 x_default_parameter (f, parameters, Qbuffer_predicate, Qnil,
4490 "bufferPredicate", "BufferPredicate", RES_TYPE_SYMBOL);
4491 x_default_parameter (f, parameters, Qtitle, Qnil,
4492 "title", "Title", RES_TYPE_STRING);
4493 x_default_parameter (f, parameters, Qfullscreen, Qnil,
4494 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
4495
4496 f->output_data.w32->dwStyle = WS_OVERLAPPEDWINDOW;
4497 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
4498
4499 f->output_data.w32->text_cursor = w32_load_cursor (IDC_IBEAM);
4500 f->output_data.w32->nontext_cursor = w32_load_cursor (IDC_ARROW);
4501 f->output_data.w32->modeline_cursor = w32_load_cursor (IDC_ARROW);
4502 f->output_data.w32->hand_cursor = w32_load_cursor (IDC_HAND);
4503 f->output_data.w32->hourglass_cursor = w32_load_cursor (IDC_WAIT);
4504 f->output_data.w32->horizontal_drag_cursor = w32_load_cursor (IDC_SIZEWE);
4505
4506 window_prompting = x_figure_window_size (f, parameters, 1);
4507
4508 tem = w32_get_arg (parameters, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
4509 f->no_split = minibuffer_only || EQ (tem, Qt);
4510
4511 w32_window (f, window_prompting, minibuffer_only);
4512 x_icon (f, parameters);
4513
4514 x_make_gc (f);
4515
4516 /* Now consider the frame official. */
4517 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
4518 Vframe_list = Fcons (frame, Vframe_list);
4519
4520 /* We need to do this after creating the window, so that the
4521 icon-creation functions can say whose icon they're describing. */
4522 x_default_parameter (f, parameters, Qicon_type, Qnil,
4523 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
4524
4525 x_default_parameter (f, parameters, Qauto_raise, Qnil,
4526 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4527 x_default_parameter (f, parameters, Qauto_lower, Qnil,
4528 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4529 x_default_parameter (f, parameters, Qcursor_type, Qbox,
4530 "cursorType", "CursorType", RES_TYPE_SYMBOL);
4531 x_default_parameter (f, parameters, Qscroll_bar_width, Qnil,
4532 "scrollBarWidth", "ScrollBarWidth", RES_TYPE_NUMBER);
4533
4534 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
4535 Change will not be effected unless different from the current
4536 FRAME_LINES (f). */
4537 width = FRAME_COLS (f);
4538 height = FRAME_LINES (f);
4539
4540 FRAME_LINES (f) = 0;
4541 SET_FRAME_COLS (f, 0);
4542 change_frame_size (f, height, width, 1, 0, 0);
4543
4544 /* Tell the server what size and position, etc, we want, and how
4545 badly we want them. This should be done after we have the menu
4546 bar so that its size can be taken into account. */
4547 BLOCK_INPUT;
4548 x_wm_set_size_hint (f, window_prompting, 0);
4549 UNBLOCK_INPUT;
4550
4551 /* Make the window appear on the frame and enable display, unless
4552 the caller says not to. However, with explicit parent, Emacs
4553 cannot control visibility, so don't try. */
4554 if (! f->output_data.w32->explicit_parent)
4555 {
4556 Lisp_Object visibility;
4557
4558 visibility = w32_get_arg (parameters, Qvisibility, 0, 0, RES_TYPE_SYMBOL);
4559 if (EQ (visibility, Qunbound))
4560 visibility = Qt;
4561
4562 if (EQ (visibility, Qicon))
4563 x_iconify_frame (f);
4564 else if (! NILP (visibility))
4565 x_make_frame_visible (f);
4566 else
4567 /* Must have been Qnil. */
4568 ;
4569 }
4570
4571 /* Initialize `default-minibuffer-frame' in case this is the first
4572 frame on this terminal. */
4573 if (FRAME_HAS_MINIBUF_P (f)
4574 && (!FRAMEP (kb->Vdefault_minibuffer_frame)
4575 || !FRAME_LIVE_P (XFRAME (kb->Vdefault_minibuffer_frame))))
4576 kb->Vdefault_minibuffer_frame = frame;
4577
4578 /* All remaining specified parameters, which have not been "used"
4579 by x_get_arg and friends, now go in the misc. alist of the frame. */
4580 for (tem = parameters; CONSP (tem); tem = XCDR (tem))
4581 if (CONSP (XCAR (tem)) && !NILP (XCAR (XCAR (tem))))
4582 f->param_alist = Fcons (XCAR (tem), f->param_alist);
4583
4584 UNGCPRO;
4585
4586 /* Make sure windows on this frame appear in calls to next-window
4587 and similar functions. */
4588 Vwindow_list = Qnil;
4589
4590 return unbind_to (count, frame);
4591 }
4592
4593 /* FRAME is used only to get a handle on the X display. We don't pass the
4594 display info directly because we're called from frame.c, which doesn't
4595 know about that structure. */
4596 Lisp_Object
4597 x_get_focus_frame (frame)
4598 struct frame *frame;
4599 {
4600 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (frame);
4601 Lisp_Object xfocus;
4602 if (! dpyinfo->w32_focus_frame)
4603 return Qnil;
4604
4605 XSETFRAME (xfocus, dpyinfo->w32_focus_frame);
4606 return xfocus;
4607 }
4608
4609 DEFUN ("x-focus-frame", Fx_focus_frame, Sx_focus_frame, 1, 1, 0,
4610 doc: /* Give FRAME input focus, raising to foreground if necessary. */)
4611 (frame)
4612 Lisp_Object frame;
4613 {
4614 x_focus_on_frame (check_x_frame (frame));
4615 return Qnil;
4616 }
4617
4618 \f
4619 /* Return the charset portion of a font name. */
4620 char *
4621 xlfd_charset_of_font (char * fontname)
4622 {
4623 char *charset, *encoding;
4624
4625 encoding = strrchr (fontname, '-');
4626 if (!encoding || encoding == fontname)
4627 return NULL;
4628
4629 for (charset = encoding - 1; charset >= fontname; charset--)
4630 if (*charset == '-')
4631 break;
4632
4633 if (charset == fontname || strcmp (charset, "-*-*") == 0)
4634 return NULL;
4635
4636 return charset + 1;
4637 }
4638
4639 struct font_info *w32_load_bdf_font (struct frame *f, char *fontname,
4640 int size, char* filename);
4641 static Lisp_Object w32_list_bdf_fonts (Lisp_Object pattern, int max_names);
4642 static BOOL w32_to_x_font (LOGFONT * lplf, char * lpxstr, int len,
4643 char * charset);
4644 static BOOL x_to_w32_font (char *lpxstr, LOGFONT *lplogfont);
4645
4646 static struct font_info *
4647 w32_load_system_font (f, fontname, size)
4648 struct frame *f;
4649 char * fontname;
4650 int size;
4651 {
4652 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4653 Lisp_Object font_names;
4654
4655 /* Get a list of all the fonts that match this name. Once we
4656 have a list of matching fonts, we compare them against the fonts
4657 we already have loaded by comparing names. */
4658 font_names = w32_list_fonts (f, build_string (fontname), size, 100);
4659
4660 if (!NILP (font_names))
4661 {
4662 Lisp_Object tail;
4663 int i;
4664
4665 /* First check if any are already loaded, as that is cheaper
4666 than loading another one. */
4667 for (i = 0; i < dpyinfo->n_fonts; i++)
4668 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
4669 if (dpyinfo->font_table[i].name
4670 && (!strcmp (dpyinfo->font_table[i].name,
4671 SDATA (XCAR (tail)))
4672 || !strcmp (dpyinfo->font_table[i].full_name,
4673 SDATA (XCAR (tail)))))
4674 return (dpyinfo->font_table + i);
4675
4676 fontname = (char *) SDATA (XCAR (font_names));
4677 }
4678 else if (w32_strict_fontnames)
4679 {
4680 /* If EnumFontFamiliesEx was available, we got a full list of
4681 fonts back so stop now to avoid the possibility of loading a
4682 random font. If we had to fall back to EnumFontFamilies, the
4683 list is incomplete, so continue whether the font we want was
4684 listed or not. */
4685 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
4686 FARPROC enum_font_families_ex
4687 = GetProcAddress (gdi32, "EnumFontFamiliesExA");
4688 if (enum_font_families_ex)
4689 return NULL;
4690 }
4691
4692 /* Load the font and add it to the table. */
4693 {
4694 char *full_name, *encoding, *charset;
4695 XFontStruct *font;
4696 struct font_info *fontp;
4697 LOGFONT lf;
4698 BOOL ok;
4699 int codepage;
4700 int i;
4701
4702 if (!fontname || !x_to_w32_font (fontname, &lf))
4703 return (NULL);
4704
4705 if (!*lf.lfFaceName)
4706 /* If no name was specified for the font, we get a random font
4707 from CreateFontIndirect - this is not particularly
4708 desirable, especially since CreateFontIndirect does not
4709 fill out the missing name in lf, so we never know what we
4710 ended up with. */
4711 return NULL;
4712
4713 lf.lfQuality = DEFAULT_QUALITY;
4714
4715 font = (XFontStruct *) xmalloc (sizeof (XFontStruct));
4716 bzero (font, sizeof (*font));
4717
4718 /* Set bdf to NULL to indicate that this is a Windows font. */
4719 font->bdf = NULL;
4720
4721 BLOCK_INPUT;
4722
4723 font->hfont = CreateFontIndirect (&lf);
4724
4725 if (font->hfont == NULL)
4726 {
4727 ok = FALSE;
4728 }
4729 else
4730 {
4731 HDC hdc;
4732 HANDLE oldobj;
4733
4734 codepage = w32_codepage_for_font (fontname);
4735
4736 hdc = GetDC (dpyinfo->root_window);
4737 oldobj = SelectObject (hdc, font->hfont);
4738
4739 ok = GetTextMetrics (hdc, &font->tm);
4740 if (codepage == CP_UNICODE)
4741 font->double_byte_p = 1;
4742 else
4743 {
4744 /* Unfortunately, some fonts (eg. MingLiU, a big5 ttf font)
4745 don't report themselves as double byte fonts, when
4746 patently they are. So instead of trusting
4747 GetFontLanguageInfo, we check the properties of the
4748 codepage directly, since that is ultimately what we are
4749 working from anyway. */
4750 /* font->double_byte_p = GetFontLanguageInfo (hdc) & GCP_DBCS; */
4751 CPINFO cpi = {0};
4752 GetCPInfo (codepage, &cpi);
4753 font->double_byte_p = cpi.MaxCharSize > 1;
4754 }
4755
4756 SelectObject (hdc, oldobj);
4757 ReleaseDC (dpyinfo->root_window, hdc);
4758 /* Fill out details in lf according to the font that was
4759 actually loaded. */
4760 lf.lfHeight = font->tm.tmInternalLeading - font->tm.tmHeight;
4761 lf.lfWidth = font->tm.tmMaxCharWidth;
4762 lf.lfWeight = font->tm.tmWeight;
4763 lf.lfItalic = font->tm.tmItalic;
4764 lf.lfCharSet = font->tm.tmCharSet;
4765 lf.lfPitchAndFamily = ((font->tm.tmPitchAndFamily & TMPF_FIXED_PITCH)
4766 ? VARIABLE_PITCH : FIXED_PITCH);
4767 lf.lfOutPrecision = ((font->tm.tmPitchAndFamily & TMPF_VECTOR)
4768 ? OUT_STROKE_PRECIS : OUT_STRING_PRECIS);
4769
4770 w32_cache_char_metrics (font);
4771 }
4772
4773 UNBLOCK_INPUT;
4774
4775 if (!ok)
4776 {
4777 w32_unload_font (dpyinfo, font);
4778 return (NULL);
4779 }
4780
4781 /* Find a free slot in the font table. */
4782 for (i = 0; i < dpyinfo->n_fonts; ++i)
4783 if (dpyinfo->font_table[i].name == NULL)
4784 break;
4785
4786 /* If no free slot found, maybe enlarge the font table. */
4787 if (i == dpyinfo->n_fonts
4788 && dpyinfo->n_fonts == dpyinfo->font_table_size)
4789 {
4790 int sz;
4791 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
4792 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
4793 dpyinfo->font_table
4794 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
4795 }
4796
4797 fontp = dpyinfo->font_table + i;
4798 if (i == dpyinfo->n_fonts)
4799 ++dpyinfo->n_fonts;
4800
4801 /* Now fill in the slots of *FONTP. */
4802 BLOCK_INPUT;
4803 bzero (fontp, sizeof (*fontp));
4804 fontp->font = font;
4805 fontp->font_idx = i;
4806 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
4807 bcopy (fontname, fontp->name, strlen (fontname) + 1);
4808
4809 if ((lf.lfPitchAndFamily & 0x03) == FIXED_PITCH)
4810 {
4811 /* Fixed width font. */
4812 fontp->average_width = fontp->space_width = FONT_AVG_WIDTH (font);
4813 }
4814 else
4815 {
4816 wchar_t space = 32;
4817 XCharStruct* pcm;
4818 pcm = w32_per_char_metric (font, &space, ANSI_FONT);
4819 if (pcm)
4820 fontp->space_width = pcm->width;
4821 else
4822 fontp->space_width = FONT_AVG_WIDTH (font);
4823
4824 fontp->average_width = font->tm.tmAveCharWidth;
4825 }
4826
4827 fontp->charset = -1;
4828 charset = xlfd_charset_of_font (fontname);
4829
4830 /* Cache the W32 codepage for a font. This makes w32_encode_char
4831 (called for every glyph during redisplay) much faster. */
4832 fontp->codepage = codepage;
4833
4834 /* Work out the font's full name. */
4835 full_name = (char *)xmalloc (100);
4836 if (full_name && w32_to_x_font (&lf, full_name, 100, charset))
4837 fontp->full_name = full_name;
4838 else
4839 {
4840 /* If all else fails - just use the name we used to load it. */
4841 xfree (full_name);
4842 fontp->full_name = fontp->name;
4843 }
4844
4845 fontp->size = FONT_WIDTH (font);
4846 fontp->height = FONT_HEIGHT (font);
4847
4848 /* The slot `encoding' specifies how to map a character
4849 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
4850 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
4851 (0:0x20..0x7F, 1:0xA0..0xFF,
4852 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
4853 2:0xA020..0xFF7F). For the moment, we don't know which charset
4854 uses this font. So, we set information in fontp->encoding_type
4855 which is never used by any charset. If mapping can't be
4856 decided, set FONT_ENCODING_NOT_DECIDED. */
4857
4858 /* SJIS fonts need to be set to type 4, all others seem to work as
4859 type FONT_ENCODING_NOT_DECIDED. */
4860 encoding = strrchr (fontp->name, '-');
4861 if (encoding && strnicmp (encoding+1, "sjis", 4) == 0)
4862 fontp->encoding_type = 4;
4863 else
4864 fontp->encoding_type = FONT_ENCODING_NOT_DECIDED;
4865
4866 /* The following three values are set to 0 under W32, which is
4867 what they get set to if XGetFontProperty fails under X. */
4868 fontp->baseline_offset = 0;
4869 fontp->relative_compose = 0;
4870 fontp->default_ascent = 0;
4871
4872 /* Set global flag fonts_changed_p to non-zero if the font loaded
4873 has a character with a smaller width than any other character
4874 before, or if the font loaded has a smaller height than any
4875 other font loaded before. If this happens, it will make a
4876 glyph matrix reallocation necessary. */
4877 fonts_changed_p |= x_compute_min_glyph_bounds (f);
4878 UNBLOCK_INPUT;
4879 return fontp;
4880 }
4881 }
4882
4883 /* Load font named FONTNAME of size SIZE for frame F, and return a
4884 pointer to the structure font_info while allocating it dynamically.
4885 If loading fails, return NULL. */
4886 struct font_info *
4887 w32_load_font (f, fontname, size)
4888 struct frame *f;
4889 char * fontname;
4890 int size;
4891 {
4892 Lisp_Object bdf_fonts;
4893 struct font_info *retval = NULL;
4894 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4895
4896 bdf_fonts = w32_list_bdf_fonts (build_string (fontname), 1);
4897
4898 while (!retval && CONSP (bdf_fonts))
4899 {
4900 char *bdf_name, *bdf_file;
4901 Lisp_Object bdf_pair;
4902 int i;
4903
4904 bdf_name = SDATA (XCAR (bdf_fonts));
4905 bdf_pair = Fassoc (XCAR (bdf_fonts), Vw32_bdf_filename_alist);
4906 bdf_file = SDATA (XCDR (bdf_pair));
4907
4908 // If the font is already loaded, do not load it again.
4909 for (i = 0; i < dpyinfo->n_fonts; i++)
4910 {
4911 if ((dpyinfo->font_table[i].name
4912 && !strcmp (dpyinfo->font_table[i].name, bdf_name))
4913 || (dpyinfo->font_table[i].full_name
4914 && !strcmp (dpyinfo->font_table[i].full_name, bdf_name)))
4915 return dpyinfo->font_table + i;
4916 }
4917
4918 retval = w32_load_bdf_font (f, bdf_name, size, bdf_file);
4919
4920 bdf_fonts = XCDR (bdf_fonts);
4921 }
4922
4923 if (retval)
4924 return retval;
4925
4926 return w32_load_system_font (f, fontname, size);
4927 }
4928
4929
4930 void
4931 w32_unload_font (dpyinfo, font)
4932 struct w32_display_info *dpyinfo;
4933 XFontStruct * font;
4934 {
4935 if (font)
4936 {
4937 if (font->per_char) xfree (font->per_char);
4938 if (font->bdf) w32_free_bdf_font (font->bdf);
4939
4940 if (font->hfont) DeleteObject (font->hfont);
4941 xfree (font);
4942 }
4943 }
4944
4945 /* The font conversion stuff between x and w32 */
4946
4947 /* X font string is as follows (from faces.el)
4948 * (let ((- "[-?]")
4949 * (foundry "[^-]+")
4950 * (family "[^-]+")
4951 * (weight "\\(bold\\|demibold\\|medium\\)") ; 1
4952 * (weight\? "\\([^-]*\\)") ; 1
4953 * (slant "\\([ior]\\)") ; 2
4954 * (slant\? "\\([^-]?\\)") ; 2
4955 * (swidth "\\([^-]*\\)") ; 3
4956 * (adstyle "[^-]*") ; 4
4957 * (pixelsize "[0-9]+")
4958 * (pointsize "[0-9][0-9]+")
4959 * (resx "[0-9][0-9]+")
4960 * (resy "[0-9][0-9]+")
4961 * (spacing "[cmp?*]")
4962 * (avgwidth "[0-9]+")
4963 * (registry "[^-]+")
4964 * (encoding "[^-]+")
4965 * )
4966 */
4967
4968 static LONG
4969 x_to_w32_weight (lpw)
4970 char * lpw;
4971 {
4972 if (!lpw) return (FW_DONTCARE);
4973
4974 if (stricmp (lpw, "heavy") == 0) return FW_HEAVY;
4975 else if (stricmp (lpw, "extrabold") == 0) return FW_EXTRABOLD;
4976 else if (stricmp (lpw, "bold") == 0) return FW_BOLD;
4977 else if (stricmp (lpw, "demibold") == 0) return FW_SEMIBOLD;
4978 else if (stricmp (lpw, "semibold") == 0) return FW_SEMIBOLD;
4979 else if (stricmp (lpw, "medium") == 0) return FW_MEDIUM;
4980 else if (stricmp (lpw, "normal") == 0) return FW_NORMAL;
4981 else if (stricmp (lpw, "light") == 0) return FW_LIGHT;
4982 else if (stricmp (lpw, "extralight") == 0) return FW_EXTRALIGHT;
4983 else if (stricmp (lpw, "thin") == 0) return FW_THIN;
4984 else
4985 return FW_DONTCARE;
4986 }
4987
4988
4989 static char *
4990 w32_to_x_weight (fnweight)
4991 int fnweight;
4992 {
4993 if (fnweight >= FW_HEAVY) return "heavy";
4994 if (fnweight >= FW_EXTRABOLD) return "extrabold";
4995 if (fnweight >= FW_BOLD) return "bold";
4996 if (fnweight >= FW_SEMIBOLD) return "demibold";
4997 if (fnweight >= FW_MEDIUM) return "medium";
4998 if (fnweight >= FW_NORMAL) return "normal";
4999 if (fnweight >= FW_LIGHT) return "light";
5000 if (fnweight >= FW_EXTRALIGHT) return "extralight";
5001 if (fnweight >= FW_THIN) return "thin";
5002 else
5003 return "*";
5004 }
5005
5006 LONG
5007 x_to_w32_charset (lpcs)
5008 char * lpcs;
5009 {
5010 Lisp_Object this_entry, w32_charset;
5011 char *charset;
5012 int len = strlen (lpcs);
5013
5014 /* Support "*-#nnn" format for unknown charsets. */
5015 if (strncmp (lpcs, "*-#", 3) == 0)
5016 return atoi (lpcs + 3);
5017
5018 /* All Windows fonts qualify as unicode. */
5019 if (!strncmp (lpcs, "iso10646", 8))
5020 return DEFAULT_CHARSET;
5021
5022 /* Handle wildcards by ignoring them; eg. treat "big5*-*" as "big5". */
5023 charset = alloca (len + 1);
5024 strcpy (charset, lpcs);
5025 lpcs = strchr (charset, '*');
5026 if (lpcs)
5027 *lpcs = '\0';
5028
5029 /* Look through w32-charset-info-alist for the character set.
5030 Format of each entry is
5031 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
5032 */
5033 this_entry = Fassoc (build_string (charset), Vw32_charset_info_alist);
5034
5035 if (NILP (this_entry))
5036 {
5037 /* At startup, we want iso8859-1 fonts to come up properly. */
5038 if (stricmp (charset, "iso8859-1") == 0)
5039 return ANSI_CHARSET;
5040 else
5041 return DEFAULT_CHARSET;
5042 }
5043
5044 w32_charset = Fcar (Fcdr (this_entry));
5045
5046 /* Translate Lisp symbol to number. */
5047 if (EQ (w32_charset, Qw32_charset_ansi))
5048 return ANSI_CHARSET;
5049 if (EQ (w32_charset, Qw32_charset_symbol))
5050 return SYMBOL_CHARSET;
5051 if (EQ (w32_charset, Qw32_charset_shiftjis))
5052 return SHIFTJIS_CHARSET;
5053 if (EQ (w32_charset, Qw32_charset_hangeul))
5054 return HANGEUL_CHARSET;
5055 if (EQ (w32_charset, Qw32_charset_chinesebig5))
5056 return CHINESEBIG5_CHARSET;
5057 if (EQ (w32_charset, Qw32_charset_gb2312))
5058 return GB2312_CHARSET;
5059 if (EQ (w32_charset, Qw32_charset_oem))
5060 return OEM_CHARSET;
5061 #ifdef JOHAB_CHARSET
5062 if (EQ (w32_charset, Qw32_charset_johab))
5063 return JOHAB_CHARSET;
5064 if (EQ (w32_charset, Qw32_charset_easteurope))
5065 return EASTEUROPE_CHARSET;
5066 if (EQ (w32_charset, Qw32_charset_turkish))
5067 return TURKISH_CHARSET;
5068 if (EQ (w32_charset, Qw32_charset_baltic))
5069 return BALTIC_CHARSET;
5070 if (EQ (w32_charset, Qw32_charset_russian))
5071 return RUSSIAN_CHARSET;
5072 if (EQ (w32_charset, Qw32_charset_arabic))
5073 return ARABIC_CHARSET;
5074 if (EQ (w32_charset, Qw32_charset_greek))
5075 return GREEK_CHARSET;
5076 if (EQ (w32_charset, Qw32_charset_hebrew))
5077 return HEBREW_CHARSET;
5078 if (EQ (w32_charset, Qw32_charset_vietnamese))
5079 return VIETNAMESE_CHARSET;
5080 if (EQ (w32_charset, Qw32_charset_thai))
5081 return THAI_CHARSET;
5082 if (EQ (w32_charset, Qw32_charset_mac))
5083 return MAC_CHARSET;
5084 #endif /* JOHAB_CHARSET */
5085 #ifdef UNICODE_CHARSET
5086 if (EQ (w32_charset, Qw32_charset_unicode))
5087 return UNICODE_CHARSET;
5088 #endif
5089
5090 return DEFAULT_CHARSET;
5091 }
5092
5093
5094 char *
5095 w32_to_x_charset (fncharset, matching)
5096 int fncharset;
5097 char *matching;
5098 {
5099 static char buf[32];
5100 Lisp_Object charset_type;
5101 int match_len = 0;
5102
5103 if (matching)
5104 {
5105 /* If fully specified, accept it as it is. Otherwise use a
5106 substring match. */
5107 char *wildcard = strchr (matching, '*');
5108 if (wildcard)
5109 *wildcard = '\0';
5110 else if (strchr (matching, '-'))
5111 return matching;
5112
5113 match_len = strlen (matching);
5114 }
5115
5116 switch (fncharset)
5117 {
5118 case ANSI_CHARSET:
5119 /* Handle startup case of w32-charset-info-alist not
5120 being set up yet. */
5121 if (NILP (Vw32_charset_info_alist))
5122 return "iso8859-1";
5123 charset_type = Qw32_charset_ansi;
5124 break;
5125 case DEFAULT_CHARSET:
5126 charset_type = Qw32_charset_default;
5127 break;
5128 case SYMBOL_CHARSET:
5129 charset_type = Qw32_charset_symbol;
5130 break;
5131 case SHIFTJIS_CHARSET:
5132 charset_type = Qw32_charset_shiftjis;
5133 break;
5134 case HANGEUL_CHARSET:
5135 charset_type = Qw32_charset_hangeul;
5136 break;
5137 case GB2312_CHARSET:
5138 charset_type = Qw32_charset_gb2312;
5139 break;
5140 case CHINESEBIG5_CHARSET:
5141 charset_type = Qw32_charset_chinesebig5;
5142 break;
5143 case OEM_CHARSET:
5144 charset_type = Qw32_charset_oem;
5145 break;
5146
5147 /* More recent versions of Windows (95 and NT4.0) define more
5148 character sets. */
5149 #ifdef EASTEUROPE_CHARSET
5150 case EASTEUROPE_CHARSET:
5151 charset_type = Qw32_charset_easteurope;
5152 break;
5153 case TURKISH_CHARSET:
5154 charset_type = Qw32_charset_turkish;
5155 break;
5156 case BALTIC_CHARSET:
5157 charset_type = Qw32_charset_baltic;
5158 break;
5159 case RUSSIAN_CHARSET:
5160 charset_type = Qw32_charset_russian;
5161 break;
5162 case ARABIC_CHARSET:
5163 charset_type = Qw32_charset_arabic;
5164 break;
5165 case GREEK_CHARSET:
5166 charset_type = Qw32_charset_greek;
5167 break;
5168 case HEBREW_CHARSET:
5169 charset_type = Qw32_charset_hebrew;
5170 break;
5171 case VIETNAMESE_CHARSET:
5172 charset_type = Qw32_charset_vietnamese;
5173 break;
5174 case THAI_CHARSET:
5175 charset_type = Qw32_charset_thai;
5176 break;
5177 case MAC_CHARSET:
5178 charset_type = Qw32_charset_mac;
5179 break;
5180 case JOHAB_CHARSET:
5181 charset_type = Qw32_charset_johab;
5182 break;
5183 #endif
5184
5185 #ifdef UNICODE_CHARSET
5186 case UNICODE_CHARSET:
5187 charset_type = Qw32_charset_unicode;
5188 break;
5189 #endif
5190 default:
5191 /* Encode numerical value of unknown charset. */
5192 sprintf (buf, "*-#%u", fncharset);
5193 return buf;
5194 }
5195
5196 {
5197 Lisp_Object rest;
5198 char * best_match = NULL;
5199 int matching_found = 0;
5200
5201 /* Look through w32-charset-info-alist for the character set.
5202 Prefer ISO codepages, and prefer lower numbers in the ISO
5203 range. Only return charsets for codepages which are installed.
5204
5205 Format of each entry is
5206 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
5207 */
5208 for (rest = Vw32_charset_info_alist; CONSP (rest); rest = XCDR (rest))
5209 {
5210 char * x_charset;
5211 Lisp_Object w32_charset;
5212 Lisp_Object codepage;
5213
5214 Lisp_Object this_entry = XCAR (rest);
5215
5216 /* Skip invalid entries in alist. */
5217 if (!CONSP (this_entry) || !STRINGP (XCAR (this_entry))
5218 || !CONSP (XCDR (this_entry))
5219 || !SYMBOLP (XCAR (XCDR (this_entry))))
5220 continue;
5221
5222 x_charset = SDATA (XCAR (this_entry));
5223 w32_charset = XCAR (XCDR (this_entry));
5224 codepage = XCDR (XCDR (this_entry));
5225
5226 /* Look for Same charset and a valid codepage (or non-int
5227 which means ignore). */
5228 if (EQ (w32_charset, charset_type)
5229 && (!INTEGERP (codepage) || XINT (codepage) == CP_DEFAULT
5230 || IsValidCodePage (XINT (codepage))))
5231 {
5232 /* If we don't have a match already, then this is the
5233 best. */
5234 if (!best_match)
5235 {
5236 best_match = x_charset;
5237 if (matching && !strnicmp (x_charset, matching, match_len))
5238 matching_found = 1;
5239 }
5240 /* If we already found a match for MATCHING, then
5241 only consider other matches. */
5242 else if (matching_found
5243 && strnicmp (x_charset, matching, match_len))
5244 continue;
5245 /* If this matches what we want, and the best so far doesn't,
5246 then this is better. */
5247 else if (!matching_found && matching
5248 && !strnicmp (x_charset, matching, match_len))
5249 {
5250 best_match = x_charset;
5251 matching_found = 1;
5252 }
5253 /* If this is fully specified, and the best so far isn't,
5254 then this is better. */
5255 else if ((!strchr (best_match, '-') && strchr (x_charset, '-'))
5256 /* If this is an ISO codepage, and the best so far isn't,
5257 then this is better, but only if it fully specifies the
5258 encoding. */
5259 || (strnicmp (best_match, "iso", 3) != 0
5260 && strnicmp (x_charset, "iso", 3) == 0
5261 && strchr (x_charset, '-')))
5262 best_match = x_charset;
5263 /* If both are ISO8859 codepages, choose the one with the
5264 lowest number in the encoding field. */
5265 else if (strnicmp (best_match, "iso8859-", 8) == 0
5266 && strnicmp (x_charset, "iso8859-", 8) == 0)
5267 {
5268 int best_enc = atoi (best_match + 8);
5269 int this_enc = atoi (x_charset + 8);
5270 if (this_enc > 0 && this_enc < best_enc)
5271 best_match = x_charset;
5272 }
5273 }
5274 }
5275
5276 /* If no match, encode the numeric value. */
5277 if (!best_match)
5278 {
5279 sprintf (buf, "*-#%u", fncharset);
5280 return buf;
5281 }
5282
5283 strncpy (buf, best_match, 31);
5284 /* If the charset is not fully specified, put -0 on the end. */
5285 if (!strchr (best_match, '-'))
5286 {
5287 int pos = strlen (best_match);
5288 /* Charset specifiers shouldn't be very long. If it is a made
5289 up one, truncating it should not do any harm since it isn't
5290 recognized anyway. */
5291 if (pos > 29)
5292 pos = 29;
5293 strcpy (buf + pos, "-0");
5294 }
5295 buf[31] = '\0';
5296 return buf;
5297 }
5298 }
5299
5300
5301 /* Return all the X charsets that map to a font. */
5302 static Lisp_Object
5303 w32_to_all_x_charsets (fncharset)
5304 int fncharset;
5305 {
5306 static char buf[32];
5307 Lisp_Object charset_type;
5308 Lisp_Object retval = Qnil;
5309
5310 switch (fncharset)
5311 {
5312 case ANSI_CHARSET:
5313 /* Handle startup case of w32-charset-info-alist not
5314 being set up yet. */
5315 if (NILP (Vw32_charset_info_alist))
5316 return Fcons (build_string ("iso8859-1"), Qnil);
5317
5318 charset_type = Qw32_charset_ansi;
5319 break;
5320 case DEFAULT_CHARSET:
5321 charset_type = Qw32_charset_default;
5322 break;
5323 case SYMBOL_CHARSET:
5324 charset_type = Qw32_charset_symbol;
5325 break;
5326 case SHIFTJIS_CHARSET:
5327 charset_type = Qw32_charset_shiftjis;
5328 break;
5329 case HANGEUL_CHARSET:
5330 charset_type = Qw32_charset_hangeul;
5331 break;
5332 case GB2312_CHARSET:
5333 charset_type = Qw32_charset_gb2312;
5334 break;
5335 case CHINESEBIG5_CHARSET:
5336 charset_type = Qw32_charset_chinesebig5;
5337 break;
5338 case OEM_CHARSET:
5339 charset_type = Qw32_charset_oem;
5340 break;
5341
5342 /* More recent versions of Windows (95 and NT4.0) define more
5343 character sets. */
5344 #ifdef EASTEUROPE_CHARSET
5345 case EASTEUROPE_CHARSET:
5346 charset_type = Qw32_charset_easteurope;
5347 break;
5348 case TURKISH_CHARSET:
5349 charset_type = Qw32_charset_turkish;
5350 break;
5351 case BALTIC_CHARSET:
5352 charset_type = Qw32_charset_baltic;
5353 break;
5354 case RUSSIAN_CHARSET:
5355 charset_type = Qw32_charset_russian;
5356 break;
5357 case ARABIC_CHARSET:
5358 charset_type = Qw32_charset_arabic;
5359 break;
5360 case GREEK_CHARSET:
5361 charset_type = Qw32_charset_greek;
5362 break;
5363 case HEBREW_CHARSET:
5364 charset_type = Qw32_charset_hebrew;
5365 break;
5366 case VIETNAMESE_CHARSET:
5367 charset_type = Qw32_charset_vietnamese;
5368 break;
5369 case THAI_CHARSET:
5370 charset_type = Qw32_charset_thai;
5371 break;
5372 case MAC_CHARSET:
5373 charset_type = Qw32_charset_mac;
5374 break;
5375 case JOHAB_CHARSET:
5376 charset_type = Qw32_charset_johab;
5377 break;
5378 #endif
5379
5380 #ifdef UNICODE_CHARSET
5381 case UNICODE_CHARSET:
5382 charset_type = Qw32_charset_unicode;
5383 break;
5384 #endif
5385 default:
5386 /* Encode numerical value of unknown charset. */
5387 sprintf (buf, "*-#%u", fncharset);
5388 return Fcons (build_string (buf), Qnil);
5389 }
5390
5391 {
5392 Lisp_Object rest;
5393 /* Look through w32-charset-info-alist for the character set.
5394 Only return fully specified charsets for codepages which are
5395 installed.
5396
5397 Format of each entry in Vw32_charset_info_alist is
5398 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
5399 */
5400 for (rest = Vw32_charset_info_alist; CONSP (rest); rest = XCDR (rest))
5401 {
5402 Lisp_Object x_charset;
5403 Lisp_Object w32_charset;
5404 Lisp_Object codepage;
5405
5406 Lisp_Object this_entry = XCAR (rest);
5407
5408 /* Skip invalid entries in alist. */
5409 if (!CONSP (this_entry) || !STRINGP (XCAR (this_entry))
5410 || !CONSP (XCDR (this_entry))
5411 || !SYMBOLP (XCAR (XCDR (this_entry))))
5412 continue;
5413
5414 x_charset = XCAR (this_entry);
5415 w32_charset = XCAR (XCDR (this_entry));
5416 codepage = XCDR (XCDR (this_entry));
5417
5418 if (!strchr (SDATA (x_charset), '-'))
5419 continue;
5420
5421 /* Look for Same charset and a valid codepage (or non-int
5422 which means ignore). */
5423 if (EQ (w32_charset, charset_type)
5424 && (!INTEGERP (codepage) || XINT (codepage) == CP_DEFAULT
5425 || IsValidCodePage (XINT (codepage))))
5426 {
5427 retval = Fcons (x_charset, retval);
5428 }
5429 }
5430
5431 /* If no match, encode the numeric value. */
5432 if (NILP (retval))
5433 {
5434 sprintf (buf, "*-#%u", fncharset);
5435 return Fcons (build_string (buf), Qnil);
5436 }
5437
5438 return retval;
5439 }
5440 }
5441
5442 /* Get the Windows codepage corresponding to the specified font. The
5443 charset info in the font name is used to look up
5444 w32-charset-to-codepage-alist. */
5445 int
5446 w32_codepage_for_font (char *fontname)
5447 {
5448 Lisp_Object codepage, entry;
5449 char *charset_str, *charset, *end;
5450
5451 /* Extract charset part of font string. */
5452 charset = xlfd_charset_of_font (fontname);
5453
5454 if (!charset)
5455 return CP_UNKNOWN;
5456
5457 charset_str = (char *) alloca (strlen (charset) + 1);
5458 strcpy (charset_str, charset);
5459
5460 #if 0
5461 /* Remove leading "*-". */
5462 if (strncmp ("*-", charset_str, 2) == 0)
5463 charset = charset_str + 2;
5464 else
5465 #endif
5466 charset = charset_str;
5467
5468 /* Stop match at wildcard (including preceding '-'). */
5469 if (end = strchr (charset, '*'))
5470 {
5471 if (end > charset && *(end-1) == '-')
5472 end--;
5473 *end = '\0';
5474 }
5475
5476 if (!strcmp (charset, "iso10646"))
5477 return CP_UNICODE;
5478
5479 if (NILP (Vw32_charset_info_alist))
5480 return CP_DEFAULT;
5481
5482 entry = Fassoc (build_string(charset), Vw32_charset_info_alist);
5483 if (NILP (entry))
5484 return CP_UNKNOWN;
5485
5486 codepage = Fcdr (Fcdr (entry));
5487
5488 if (NILP (codepage))
5489 return CP_8BIT;
5490 else if (XFASTINT (codepage) == XFASTINT (Qt))
5491 return CP_UNICODE;
5492 else if (INTEGERP (codepage))
5493 return XINT (codepage);
5494 else
5495 return CP_UNKNOWN;
5496 }
5497
5498
5499 static BOOL
5500 w32_to_x_font (lplogfont, lpxstr, len, specific_charset)
5501 LOGFONT * lplogfont;
5502 char * lpxstr;
5503 int len;
5504 char * specific_charset;
5505 {
5506 char* fonttype;
5507 char *fontname;
5508 char height_pixels[8];
5509 char height_dpi[8];
5510 char width_pixels[8];
5511 char *fontname_dash;
5512 int display_resy = (int) one_w32_display_info.resy;
5513 int display_resx = (int) one_w32_display_info.resx;
5514 struct coding_system coding;
5515
5516 if (!lpxstr) abort ();
5517
5518 if (!lplogfont)
5519 return FALSE;
5520
5521 if (lplogfont->lfOutPrecision == OUT_STRING_PRECIS)
5522 fonttype = "raster";
5523 else if (lplogfont->lfOutPrecision == OUT_STROKE_PRECIS)
5524 fonttype = "outline";
5525 else
5526 fonttype = "unknown";
5527
5528 setup_coding_system (Fcheck_coding_system (Vlocale_coding_system),
5529 &coding);
5530 coding.src_multibyte = 0;
5531 coding.dst_multibyte = 1;
5532 coding.mode |= CODING_MODE_LAST_BLOCK;
5533 /* We explicitely disable composition handling because selection
5534 data should not contain any composition sequence. */
5535 coding.common_flags &= ~CODING_ANNOTATION_MASK;
5536
5537 coding.dst_bytes = LF_FACESIZE * 2;
5538 coding.destination = (unsigned char *) xmalloc (coding.dst_bytes + 1);
5539 decode_coding_c_string (&coding, lplogfont->lfFaceName,
5540 strlen(lplogfont->lfFaceName), Qnil);
5541 fontname = coding.destination;
5542
5543 *(fontname + coding.produced) = '\0';
5544
5545 /* Replace dashes with underscores so the dashes are not
5546 misinterpreted. */
5547 fontname_dash = fontname;
5548 while (fontname_dash = strchr (fontname_dash, '-'))
5549 *fontname_dash = '_';
5550
5551 if (lplogfont->lfHeight)
5552 {
5553 sprintf (height_pixels, "%u", eabs (lplogfont->lfHeight));
5554 sprintf (height_dpi, "%u",
5555 eabs (lplogfont->lfHeight) * 720 / display_resy);
5556 }
5557 else
5558 {
5559 strcpy (height_pixels, "*");
5560 strcpy (height_dpi, "*");
5561 }
5562
5563 #if 0 /* Never put the width in the xfld. It fails on fonts with
5564 double-width characters. */
5565 if (lplogfont->lfWidth)
5566 sprintf (width_pixels, "%u", lplogfont->lfWidth * 10);
5567 else
5568 #endif
5569 strcpy (width_pixels, "*");
5570
5571 _snprintf (lpxstr, len - 1,
5572 "-%s-%s-%s-%c-normal-normal-%s-%s-%d-%d-%c-%s-%s",
5573 fonttype, /* foundry */
5574 fontname, /* family */
5575 w32_to_x_weight (lplogfont->lfWeight), /* weight */
5576 lplogfont->lfItalic?'i':'r', /* slant */
5577 /* setwidth name */
5578 /* add style name */
5579 height_pixels, /* pixel size */
5580 height_dpi, /* point size */
5581 display_resx, /* resx */
5582 display_resy, /* resy */
5583 ((lplogfont->lfPitchAndFamily & 0x3) == VARIABLE_PITCH)
5584 ? 'p' : 'c', /* spacing */
5585 width_pixels, /* avg width */
5586 w32_to_x_charset (lplogfont->lfCharSet, specific_charset)
5587 /* charset registry and encoding */
5588 );
5589
5590 lpxstr[len - 1] = 0; /* just to be sure */
5591 return (TRUE);
5592 }
5593
5594 static BOOL
5595 x_to_w32_font (lpxstr, lplogfont)
5596 char * lpxstr;
5597 LOGFONT * lplogfont;
5598 {
5599 struct coding_system coding;
5600
5601 if (!lplogfont) return (FALSE);
5602
5603 memset (lplogfont, 0, sizeof (*lplogfont));
5604
5605 /* Set default value for each field. */
5606 #if 1
5607 lplogfont->lfOutPrecision = OUT_DEFAULT_PRECIS;
5608 lplogfont->lfClipPrecision = CLIP_DEFAULT_PRECIS;
5609 lplogfont->lfQuality = DEFAULT_QUALITY;
5610 #else
5611 /* go for maximum quality */
5612 lplogfont->lfOutPrecision = OUT_STROKE_PRECIS;
5613 lplogfont->lfClipPrecision = CLIP_STROKE_PRECIS;
5614 lplogfont->lfQuality = PROOF_QUALITY;
5615 #endif
5616
5617 lplogfont->lfCharSet = DEFAULT_CHARSET;
5618 lplogfont->lfWeight = FW_DONTCARE;
5619 lplogfont->lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
5620
5621 if (!lpxstr)
5622 return FALSE;
5623
5624 /* Provide a simple escape mechanism for specifying Windows font names
5625 * directly -- if font spec does not beginning with '-', assume this
5626 * format:
5627 * "<font name>[:height in pixels[:width in pixels[:weight]]]"
5628 */
5629
5630 if (*lpxstr == '-')
5631 {
5632 int fields, tem;
5633 char name[50], weight[20], slant, pitch, pixels[10], height[10],
5634 width[10], resy[10], remainder[50];
5635 char * encoding;
5636 int dpi = (int) one_w32_display_info.resy;
5637
5638 fields = sscanf (lpxstr,
5639 "-%*[^-]-%49[^-]-%19[^-]-%c-%*[^-]-%*[^-]-%9[^-]-%9[^-]-%*[^-]-%9[^-]-%c-%9[^-]-%49s",
5640 name, weight, &slant, pixels, height, resy, &pitch, width, remainder);
5641 if (fields == EOF)
5642 return (FALSE);
5643
5644 /* In the general case when wildcards cover more than one field,
5645 we don't know which field is which, so don't fill any in.
5646 However, we need to cope with this particular form, which is
5647 generated by font_list_1 (invoked by try_font_list):
5648 "-raster-6x10-*-gb2312*-*"
5649 and make sure to correctly parse the charset field. */
5650 if (fields == 3)
5651 {
5652 fields = sscanf (lpxstr,
5653 "-%*[^-]-%49[^-]-*-%49s",
5654 name, remainder);
5655 }
5656 else if (fields < 9)
5657 {
5658 fields = 0;
5659 remainder[0] = 0;
5660 }
5661
5662 if (fields > 0 && name[0] != '*')
5663 {
5664 Lisp_Object string = build_string (name);
5665 setup_coding_system
5666 (Fcheck_coding_system (Vlocale_coding_system), &coding);
5667 coding.mode |= (CODING_MODE_SAFE_ENCODING | CODING_MODE_LAST_BLOCK);
5668 /* Disable composition/charset annotation. */
5669 coding.common_flags &= ~CODING_ANNOTATION_MASK;
5670 coding.dst_bytes = SCHARS (string) * 2;
5671
5672 coding.destination = (unsigned char *) xmalloc (coding.dst_bytes);
5673 encode_coding_object (&coding, string, 0, 0,
5674 SCHARS (string), SBYTES (string), Qnil);
5675 if (coding.produced >= LF_FACESIZE)
5676 coding.produced = LF_FACESIZE - 1;
5677
5678 coding.destination[coding.produced] = '\0';
5679
5680 strcpy (lplogfont->lfFaceName, coding.destination);
5681 xfree (coding.destination);
5682 }
5683 else
5684 {
5685 lplogfont->lfFaceName[0] = '\0';
5686 }
5687
5688 fields--;
5689
5690 lplogfont->lfWeight = x_to_w32_weight ((fields > 0 ? weight : ""));
5691
5692 fields--;
5693
5694 lplogfont->lfItalic = (fields > 0 && slant == 'i');
5695
5696 fields--;
5697
5698 if (fields > 0 && pixels[0] != '*')
5699 lplogfont->lfHeight = atoi (pixels);
5700
5701 fields--;
5702 fields--;
5703 if (fields > 0 && resy[0] != '*')
5704 {
5705 tem = atoi (resy);
5706 if (tem > 0) dpi = tem;
5707 }
5708
5709 if (fields > -1 && lplogfont->lfHeight == 0 && height[0] != '*')
5710 lplogfont->lfHeight = atoi (height) * dpi / 720;
5711
5712 if (fields > 0)
5713 {
5714 if (pitch == 'p')
5715 lplogfont->lfPitchAndFamily = VARIABLE_PITCH | FF_DONTCARE;
5716 else if (pitch == 'c')
5717 lplogfont->lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
5718 }
5719
5720 fields--;
5721
5722 if (fields > 0 && width[0] != '*')
5723 lplogfont->lfWidth = atoi (width) / 10;
5724
5725 fields--;
5726
5727 /* Strip the trailing '-' if present. (it shouldn't be, as it
5728 fails the test against xlfd-tight-regexp in fontset.el). */
5729 {
5730 int len = strlen (remainder);
5731 if (len > 0 && remainder[len-1] == '-')
5732 remainder[len-1] = 0;
5733 }
5734 encoding = remainder;
5735 #if 0
5736 if (strncmp (encoding, "*-", 2) == 0)
5737 encoding += 2;
5738 #endif
5739 lplogfont->lfCharSet = x_to_w32_charset (encoding);
5740 }
5741 else
5742 {
5743 int fields;
5744 char name[100], height[10], width[10], weight[20];
5745
5746 fields = sscanf (lpxstr,
5747 "%99[^:]:%9[^:]:%9[^:]:%19s",
5748 name, height, width, weight);
5749
5750 if (fields == EOF) return (FALSE);
5751
5752 if (fields > 0)
5753 {
5754 strncpy (lplogfont->lfFaceName, name, LF_FACESIZE);
5755 lplogfont->lfFaceName[LF_FACESIZE-1] = 0;
5756 }
5757 else
5758 {
5759 lplogfont->lfFaceName[0] = 0;
5760 }
5761
5762 fields--;
5763
5764 if (fields > 0)
5765 lplogfont->lfHeight = atoi (height);
5766
5767 fields--;
5768
5769 if (fields > 0)
5770 lplogfont->lfWidth = atoi (width);
5771
5772 fields--;
5773
5774 lplogfont->lfWeight = x_to_w32_weight ((fields > 0 ? weight : ""));
5775 }
5776
5777 /* This makes TrueType fonts work better. */
5778 lplogfont->lfHeight = - eabs (lplogfont->lfHeight);
5779
5780 return (TRUE);
5781 }
5782
5783 /* Strip the pixel height and point height from the given xlfd, and
5784 return the pixel height. If no pixel height is specified, calculate
5785 one from the point height, or if that isn't defined either, return
5786 0 (which usually signifies a scalable font).
5787 */
5788 static int
5789 xlfd_strip_height (char *fontname)
5790 {
5791 int pixel_height, field_number;
5792 char *read_from, *write_to;
5793
5794 xassert (fontname);
5795
5796 pixel_height = field_number = 0;
5797 write_to = NULL;
5798
5799 /* Look for height fields. */
5800 for (read_from = fontname; *read_from; read_from++)
5801 {
5802 if (*read_from == '-')
5803 {
5804 field_number++;
5805 if (field_number == 7) /* Pixel height. */
5806 {
5807 read_from++;
5808 write_to = read_from;
5809
5810 /* Find end of field. */
5811 for (;*read_from && *read_from != '-'; read_from++)
5812 ;
5813
5814 /* Split the fontname at end of field. */
5815 if (*read_from)
5816 {
5817 *read_from = '\0';
5818 read_from++;
5819 }
5820 pixel_height = atoi (write_to);
5821 /* Blank out field. */
5822 if (read_from > write_to)
5823 {
5824 *write_to = '-';
5825 write_to++;
5826 }
5827 /* If the pixel height field is at the end (partial xlfd),
5828 return now. */
5829 else
5830 return pixel_height;
5831
5832 /* If we got a pixel height, the point height can be
5833 ignored. Just blank it out and break now. */
5834 if (pixel_height)
5835 {
5836 /* Find end of point size field. */
5837 for (; *read_from && *read_from != '-'; read_from++)
5838 ;
5839
5840 if (*read_from)
5841 read_from++;
5842
5843 /* Blank out the point size field. */
5844 if (read_from > write_to)
5845 {
5846 *write_to = '-';
5847 write_to++;
5848 }
5849 else
5850 return pixel_height;
5851
5852 break;
5853 }
5854 /* If the point height is already blank, break now. */
5855 if (*read_from == '-')
5856 {
5857 read_from++;
5858 break;
5859 }
5860 }
5861 else if (field_number == 8)
5862 {
5863 /* If we didn't get a pixel height, try to get the point
5864 height and convert that. */
5865 int point_size;
5866 char *point_size_start = read_from++;
5867
5868 /* Find end of field. */
5869 for (; *read_from && *read_from != '-'; read_from++)
5870 ;
5871
5872 if (*read_from)
5873 {
5874 *read_from = '\0';
5875 read_from++;
5876 }
5877
5878 point_size = atoi (point_size_start);
5879
5880 /* Convert to pixel height. */
5881 pixel_height = point_size
5882 * one_w32_display_info.height_in / 720;
5883
5884 /* Blank out this field and break. */
5885 *write_to = '-';
5886 write_to++;
5887 break;
5888 }
5889 }
5890 }
5891
5892 /* Shift the rest of the font spec into place. */
5893 if (write_to && read_from > write_to)
5894 {
5895 for (; *read_from; read_from++, write_to++)
5896 *write_to = *read_from;
5897 *write_to = '\0';
5898 }
5899
5900 return pixel_height;
5901 }
5902
5903 /* Assume parameter 1 is fully qualified, no wildcards. */
5904 static BOOL
5905 w32_font_match (fontname, pattern)
5906 char * fontname;
5907 char * pattern;
5908 {
5909 char *ptr;
5910 char *font_name_copy;
5911 char *regex = alloca (strlen (pattern) * 2 + 3);
5912
5913 font_name_copy = alloca (strlen (fontname) + 1);
5914 strcpy (font_name_copy, fontname);
5915
5916 ptr = regex;
5917 *ptr++ = '^';
5918
5919 /* Turn pattern into a regexp and do a regexp match. */
5920 for (; *pattern; pattern++)
5921 {
5922 if (*pattern == '?')
5923 *ptr++ = '.';
5924 else if (*pattern == '*')
5925 {
5926 *ptr++ = '.';
5927 *ptr++ = '*';
5928 }
5929 else
5930 *ptr++ = *pattern;
5931 }
5932 *ptr = '$';
5933 *(ptr + 1) = '\0';
5934
5935 /* Strip out font heights and compare them seperately, since
5936 rounding error can cause mismatches. This also allows a
5937 comparison between a font that declares only a pixel height and a
5938 pattern that declares the point height.
5939 */
5940 {
5941 int font_height, pattern_height;
5942
5943 font_height = xlfd_strip_height (font_name_copy);
5944 pattern_height = xlfd_strip_height (regex);
5945
5946 /* Compare now, and don't bother doing expensive regexp matching
5947 if the heights differ. */
5948 if (font_height && pattern_height && (font_height != pattern_height))
5949 return FALSE;
5950 }
5951
5952 return (fast_string_match_ignore_case (build_string (regex),
5953 build_string (font_name_copy)) >= 0);
5954 }
5955
5956 /* Callback functions, and a structure holding info they need, for
5957 listing system fonts on W32. We need one set of functions to do the
5958 job properly, but these don't work on NT 3.51 and earlier, so we
5959 have a second set which don't handle character sets properly to
5960 fall back on.
5961
5962 In both cases, there are two passes made. The first pass gets one
5963 font from each family, the second pass lists all the fonts from
5964 each family. */
5965
5966 typedef struct enumfont_t
5967 {
5968 HDC hdc;
5969 int numFonts;
5970 LOGFONT logfont;
5971 XFontStruct *size_ref;
5972 Lisp_Object pattern;
5973 Lisp_Object list;
5974 } enumfont_t;
5975
5976
5977 static void
5978 enum_font_maybe_add_to_list (enumfont_t *, LOGFONT *, char *, Lisp_Object);
5979
5980
5981 static int CALLBACK
5982 enum_font_cb2 (lplf, lptm, FontType, lpef)
5983 ENUMLOGFONT * lplf;
5984 NEWTEXTMETRIC * lptm;
5985 int FontType;
5986 enumfont_t * lpef;
5987 {
5988 /* Ignore struck out and underlined versions of fonts. */
5989 if (lplf->elfLogFont.lfStrikeOut || lplf->elfLogFont.lfUnderline)
5990 return 1;
5991
5992 /* Only return fonts with names starting with @ if they were
5993 explicitly specified, since Microsoft uses an initial @ to
5994 denote fonts for vertical writing, without providing a more
5995 convenient way of identifying them. */
5996 if (lplf->elfLogFont.lfFaceName[0] == '@'
5997 && lpef->logfont.lfFaceName[0] != '@')
5998 return 1;
5999
6000 /* Check that the character set matches if it was specified */
6001 if (lpef->logfont.lfCharSet != DEFAULT_CHARSET &&
6002 lplf->elfLogFont.lfCharSet != lpef->logfont.lfCharSet)
6003 return 1;
6004
6005 if (FontType == RASTER_FONTTYPE)
6006 {
6007 /* DBCS raster fonts have problems displaying, so skip them. */
6008 int charset = lplf->elfLogFont.lfCharSet;
6009 if (charset == SHIFTJIS_CHARSET
6010 || charset == HANGEUL_CHARSET
6011 || charset == CHINESEBIG5_CHARSET
6012 || charset == GB2312_CHARSET
6013 #ifdef JOHAB_CHARSET
6014 || charset == JOHAB_CHARSET
6015 #endif
6016 )
6017 return 1;
6018 }
6019
6020 {
6021 char buf[100];
6022 Lisp_Object width = Qnil;
6023 Lisp_Object charset_list = Qnil;
6024 char *charset = NULL;
6025
6026 /* Truetype fonts do not report their true metrics until loaded */
6027 if (FontType != RASTER_FONTTYPE)
6028 {
6029 if (!NILP (lpef->pattern))
6030 {
6031 /* Scalable fonts are as big as you want them to be. */
6032 lplf->elfLogFont.lfHeight = lpef->logfont.lfHeight;
6033 lplf->elfLogFont.lfWidth = lpef->logfont.lfWidth;
6034 width = make_number (lpef->logfont.lfWidth);
6035 }
6036 else
6037 {
6038 lplf->elfLogFont.lfHeight = 0;
6039 lplf->elfLogFont.lfWidth = 0;
6040 }
6041 }
6042
6043 /* Make sure the height used here is the same as everywhere
6044 else (ie character height, not cell height). */
6045 if (lplf->elfLogFont.lfHeight > 0)
6046 {
6047 /* lptm can be trusted for RASTER fonts, but not scalable ones. */
6048 if (FontType == RASTER_FONTTYPE)
6049 lplf->elfLogFont.lfHeight = lptm->tmInternalLeading - lptm->tmHeight;
6050 else
6051 lplf->elfLogFont.lfHeight = -lplf->elfLogFont.lfHeight;
6052 }
6053
6054 if (!NILP (lpef->pattern))
6055 {
6056 charset = xlfd_charset_of_font (SDATA (lpef->pattern));
6057
6058 /* We already checked charsets above, but DEFAULT_CHARSET
6059 slipped through. So only allow exact matches for DEFAULT_CHARSET. */
6060 if (charset
6061 && strncmp (charset, "*-*", 3) != 0
6062 && lpef->logfont.lfCharSet == DEFAULT_CHARSET
6063 && strcmp (charset, w32_to_x_charset (DEFAULT_CHARSET, NULL)) != 0)
6064 return 1;
6065 }
6066
6067 if (charset)
6068 charset_list = Fcons (build_string (charset), Qnil);
6069 else
6070 /* Always prefer unicode. */
6071 charset_list
6072 = Fcons (build_string ("iso10646-1"),
6073 w32_to_all_x_charsets (lplf->elfLogFont.lfCharSet));
6074
6075 /* Loop through the charsets. */
6076 for ( ; CONSP (charset_list); charset_list = Fcdr (charset_list))
6077 {
6078 Lisp_Object this_charset = Fcar (charset_list);
6079 charset = SDATA (this_charset);
6080
6081 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
6082 charset, width);
6083
6084 /* List bold and italic variations if w32-enable-synthesized-fonts
6085 is non-nil and this is a plain font. */
6086 if (w32_enable_synthesized_fonts
6087 && lplf->elfLogFont.lfWeight == FW_NORMAL
6088 && lplf->elfLogFont.lfItalic == FALSE)
6089 {
6090 /* bold. */
6091 lplf->elfLogFont.lfWeight = FW_BOLD;
6092 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
6093 charset, width);
6094 /* bold italic. */
6095 lplf->elfLogFont.lfItalic = TRUE;
6096 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
6097 charset, width);
6098 /* italic. */
6099 lplf->elfLogFont.lfWeight = FW_NORMAL;
6100 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
6101 charset, width);
6102 }
6103 }
6104 }
6105
6106 return 1;
6107 }
6108
6109 static void
6110 enum_font_maybe_add_to_list (lpef, logfont, match_charset, width)
6111 enumfont_t * lpef;
6112 LOGFONT * logfont;
6113 char * match_charset;
6114 Lisp_Object width;
6115 {
6116 char buf[100];
6117
6118 if (!w32_to_x_font (logfont, buf, 100, match_charset))
6119 return;
6120
6121 if (NILP (lpef->pattern)
6122 || w32_font_match (buf, SDATA (lpef->pattern)))
6123 {
6124 /* Check if we already listed this font. This may happen if
6125 w32_enable_synthesized_fonts is non-nil, and there are real
6126 bold and italic versions of the font. */
6127 Lisp_Object font_name = build_string (buf);
6128 if (NILP (Fmember (font_name, lpef->list)))
6129 {
6130 Lisp_Object entry = Fcons (font_name, width);
6131 lpef->list = Fcons (entry, lpef->list);
6132 lpef->numFonts++;
6133 }
6134 }
6135 }
6136
6137
6138 static int CALLBACK
6139 enum_font_cb1 (lplf, lptm, FontType, lpef)
6140 ENUMLOGFONT * lplf;
6141 NEWTEXTMETRIC * lptm;
6142 int FontType;
6143 enumfont_t * lpef;
6144 {
6145 return EnumFontFamilies (lpef->hdc,
6146 lplf->elfLogFont.lfFaceName,
6147 (FONTENUMPROC) enum_font_cb2,
6148 (LPARAM) lpef);
6149 }
6150
6151
6152 static int CALLBACK
6153 enum_fontex_cb2 (lplf, lptm, font_type, lpef)
6154 ENUMLOGFONTEX * lplf;
6155 NEWTEXTMETRICEX * lptm;
6156 int font_type;
6157 enumfont_t * lpef;
6158 {
6159 /* We are not interested in the extra info we get back from the 'Ex
6160 version - only the fact that we get character set variations
6161 enumerated seperately. */
6162 return enum_font_cb2 ((ENUMLOGFONT *) lplf, (NEWTEXTMETRIC *) lptm,
6163 font_type, lpef);
6164 }
6165
6166 static int CALLBACK
6167 enum_fontex_cb1 (lplf, lptm, font_type, lpef)
6168 ENUMLOGFONTEX * lplf;
6169 NEWTEXTMETRICEX * lptm;
6170 int font_type;
6171 enumfont_t * lpef;
6172 {
6173 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
6174 FARPROC enum_font_families_ex
6175 = GetProcAddress ( gdi32, "EnumFontFamiliesExA");
6176 /* We don't really expect EnumFontFamiliesEx to disappear once we
6177 get here, so don't bother handling it gracefully. */
6178 if (enum_font_families_ex == NULL)
6179 error ("gdi32.dll has disappeared!");
6180 return enum_font_families_ex (lpef->hdc,
6181 &lplf->elfLogFont,
6182 (FONTENUMPROC) enum_fontex_cb2,
6183 (LPARAM) lpef, 0);
6184 }
6185
6186 /* Interface to fontset handler. (adapted from mw32font.c in Meadow
6187 and xterm.c in Emacs 20.3) */
6188
6189 static Lisp_Object
6190 w32_list_bdf_fonts (Lisp_Object pattern, int max_names)
6191 {
6192 char *fontname, *ptnstr;
6193 Lisp_Object list, tem, newlist = Qnil;
6194 int n_fonts = 0;
6195
6196 list = Vw32_bdf_filename_alist;
6197 ptnstr = SDATA (pattern);
6198
6199 for ( ; CONSP (list); list = XCDR (list))
6200 {
6201 tem = XCAR (list);
6202 if (CONSP (tem))
6203 fontname = SDATA (XCAR (tem));
6204 else if (STRINGP (tem))
6205 fontname = SDATA (tem);
6206 else
6207 continue;
6208
6209 if (w32_font_match (fontname, ptnstr))
6210 {
6211 newlist = Fcons (XCAR (tem), newlist);
6212 n_fonts++;
6213 if (max_names >= 0 && n_fonts >= max_names)
6214 break;
6215 }
6216 }
6217
6218 return newlist;
6219 }
6220
6221
6222 /* Return a list of names of available fonts matching PATTERN on frame
6223 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
6224 to be listed. Frame F NULL means we have not yet created any
6225 frame, which means we can't get proper size info, as we don't have
6226 a device context to use for GetTextMetrics.
6227 MAXNAMES sets a limit on how many fonts to match. If MAXNAMES is
6228 negative, then all matching fonts are returned. */
6229
6230 Lisp_Object
6231 w32_list_fonts (f, pattern, size, maxnames)
6232 struct frame *f;
6233 Lisp_Object pattern;
6234 int size;
6235 int maxnames;
6236 {
6237 Lisp_Object patterns, key = Qnil, tem, tpat;
6238 Lisp_Object list = Qnil, newlist = Qnil, second_best = Qnil;
6239 struct w32_display_info *dpyinfo = &one_w32_display_info;
6240 int n_fonts = 0;
6241
6242 patterns = Fassoc (pattern, Valternate_fontname_alist);
6243 if (NILP (patterns))
6244 patterns = Fcons (pattern, Qnil);
6245
6246 for (; CONSP (patterns); patterns = XCDR (patterns))
6247 {
6248 enumfont_t ef;
6249 int codepage;
6250
6251 tpat = XCAR (patterns);
6252
6253 if (!STRINGP (tpat))
6254 continue;
6255
6256 /* Avoid expensive EnumFontFamilies functions if we are not
6257 going to be able to output one of these anyway. */
6258 codepage = w32_codepage_for_font (SDATA (tpat));
6259 if (codepage != CP_8BIT && codepage != CP_UNICODE
6260 && codepage != CP_DEFAULT && codepage != CP_UNKNOWN
6261 && !IsValidCodePage (codepage))
6262 continue;
6263
6264 /* See if we cached the result for this particular query.
6265 The cache is an alist of the form:
6266 ((PATTERN (FONTNAME . WIDTH) ...) ...)
6267 */
6268 if (tem = XCDR (dpyinfo->name_list_element),
6269 !NILP (list = Fassoc (tpat, tem)))
6270 {
6271 list = Fcdr_safe (list);
6272 /* We have a cached list. Don't have to get the list again. */
6273 goto label_cached;
6274 }
6275
6276 BLOCK_INPUT;
6277 /* At first, put PATTERN in the cache. */
6278 ef.pattern = tpat;
6279 ef.list = Qnil;
6280 ef.numFonts = 0;
6281
6282 /* Use EnumFontFamiliesEx where it is available, as it knows
6283 about character sets. Fall back to EnumFontFamilies for
6284 older versions of NT that don't support the 'Ex function. */
6285 x_to_w32_font (SDATA (tpat), &ef.logfont);
6286 {
6287 LOGFONT font_match_pattern;
6288 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
6289 FARPROC enum_font_families_ex
6290 = GetProcAddress ( gdi32, "EnumFontFamiliesExA");
6291
6292 /* We do our own pattern matching so we can handle wildcards. */
6293 font_match_pattern.lfFaceName[0] = 0;
6294 font_match_pattern.lfPitchAndFamily = 0;
6295 /* We can use the charset, because if it is a wildcard it will
6296 be DEFAULT_CHARSET anyway. */
6297 font_match_pattern.lfCharSet = ef.logfont.lfCharSet;
6298
6299 ef.hdc = GetDC (dpyinfo->root_window);
6300
6301 if (enum_font_families_ex)
6302 enum_font_families_ex (ef.hdc,
6303 &font_match_pattern,
6304 (FONTENUMPROC) enum_fontex_cb1,
6305 (LPARAM) &ef, 0);
6306 else
6307 EnumFontFamilies (ef.hdc, NULL, (FONTENUMPROC) enum_font_cb1,
6308 (LPARAM)&ef);
6309
6310 ReleaseDC (dpyinfo->root_window, ef.hdc);
6311 }
6312
6313 UNBLOCK_INPUT;
6314 list = ef.list;
6315
6316 /* Make a list of the fonts we got back.
6317 Store that in the font cache for the display. */
6318 XSETCDR (dpyinfo->name_list_element,
6319 Fcons (Fcons (tpat, list),
6320 XCDR (dpyinfo->name_list_element)));
6321
6322 label_cached:
6323 if (NILP (list)) continue; /* Try the remaining alternatives. */
6324
6325 newlist = second_best = Qnil;
6326
6327 /* Make a list of the fonts that have the right width. */
6328 for (; CONSP (list); list = XCDR (list))
6329 {
6330 int found_size;
6331 tem = XCAR (list);
6332
6333 if (!CONSP (tem))
6334 continue;
6335 if (NILP (XCAR (tem)))
6336 continue;
6337 if (!size)
6338 {
6339 newlist = Fcons (XCAR (tem), newlist);
6340 n_fonts++;
6341 if (maxnames >= 0 && n_fonts >= maxnames)
6342 break;
6343 else
6344 continue;
6345 }
6346 if (!INTEGERP (XCDR (tem)))
6347 {
6348 /* Since we don't yet know the size of the font, we must
6349 load it and try GetTextMetrics. */
6350 W32FontStruct thisinfo;
6351 LOGFONT lf;
6352 HDC hdc;
6353 HANDLE oldobj;
6354
6355 if (!x_to_w32_font (SDATA (XCAR (tem)), &lf))
6356 continue;
6357
6358 BLOCK_INPUT;
6359 thisinfo.bdf = NULL;
6360 thisinfo.hfont = CreateFontIndirect (&lf);
6361 if (thisinfo.hfont == NULL)
6362 continue;
6363
6364 hdc = GetDC (dpyinfo->root_window);
6365 oldobj = SelectObject (hdc, thisinfo.hfont);
6366 if (GetTextMetrics (hdc, &thisinfo.tm))
6367 XSETCDR (tem, make_number (FONT_AVG_WIDTH (&thisinfo)));
6368 else
6369 XSETCDR (tem, make_number (0));
6370 SelectObject (hdc, oldobj);
6371 ReleaseDC (dpyinfo->root_window, hdc);
6372 DeleteObject (thisinfo.hfont);
6373 UNBLOCK_INPUT;
6374 }
6375 found_size = XINT (XCDR (tem));
6376 if (found_size == size)
6377 {
6378 newlist = Fcons (XCAR (tem), newlist);
6379 n_fonts++;
6380 if (maxnames >= 0 && n_fonts >= maxnames)
6381 break;
6382 }
6383 /* keep track of the closest matching size in case
6384 no exact match is found. */
6385 else if (found_size > 0)
6386 {
6387 if (NILP (second_best))
6388 second_best = tem;
6389
6390 else if (found_size < size)
6391 {
6392 if (XINT (XCDR (second_best)) > size
6393 || XINT (XCDR (second_best)) < found_size)
6394 second_best = tem;
6395 }
6396 else
6397 {
6398 if (XINT (XCDR (second_best)) > size
6399 && XINT (XCDR (second_best)) >
6400 found_size)
6401 second_best = tem;
6402 }
6403 }
6404 }
6405
6406 if (!NILP (newlist))
6407 break;
6408 else if (!NILP (second_best))
6409 {
6410 newlist = Fcons (XCAR (second_best), Qnil);
6411 break;
6412 }
6413 }
6414
6415 /* Include any bdf fonts. */
6416 if (n_fonts < maxnames || maxnames < 0)
6417 {
6418 Lisp_Object combined[2];
6419 combined[0] = w32_list_bdf_fonts (pattern, maxnames - n_fonts);
6420 combined[1] = newlist;
6421 newlist = Fnconc (2, combined);
6422 }
6423
6424 return newlist;
6425 }
6426
6427
6428 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
6429 struct font_info *
6430 w32_get_font_info (f, font_idx)
6431 FRAME_PTR f;
6432 int font_idx;
6433 {
6434 return (FRAME_W32_FONT_TABLE (f) + font_idx);
6435 }
6436
6437
6438 struct font_info*
6439 w32_query_font (struct frame *f, char *fontname)
6440 {
6441 int i;
6442 struct font_info *pfi;
6443
6444 pfi = FRAME_W32_FONT_TABLE (f);
6445
6446 for (i = 0; i < one_w32_display_info.n_fonts ;i++, pfi++)
6447 {
6448 if (stricmp (pfi->name, fontname) == 0) return pfi;
6449 }
6450
6451 return NULL;
6452 }
6453
6454 /* Find a CCL program for a font specified by FONTP, and set the member
6455 `encoder' of the structure. */
6456
6457 void
6458 w32_find_ccl_program (fontp)
6459 struct font_info *fontp;
6460 {
6461 Lisp_Object list, elt;
6462
6463 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
6464 {
6465 elt = XCAR (list);
6466 if (CONSP (elt)
6467 && STRINGP (XCAR (elt))
6468 && (fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
6469 >= 0))
6470 break;
6471 }
6472 if (! NILP (list))
6473 {
6474 struct ccl_program *ccl
6475 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
6476
6477 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
6478 xfree (ccl);
6479 else
6480 fontp->font_encoder = ccl;
6481 }
6482 }
6483
6484 /* directory-files from dired.c. */
6485 Lisp_Object Fdirectory_files P_ ((Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object));
6486
6487 \f
6488 /* Find BDF files in a specified directory. (use GCPRO when calling,
6489 as this calls lisp to get a directory listing). */
6490 static Lisp_Object
6491 w32_find_bdf_fonts_in_dir (Lisp_Object directory)
6492 {
6493 Lisp_Object filelist, list = Qnil;
6494 char fontname[100];
6495
6496 if (!STRINGP (directory))
6497 return Qnil;
6498
6499 filelist = Fdirectory_files (directory, Qt,
6500 build_string (".*\\.[bB][dD][fF]"), Qt);
6501
6502 for ( ; CONSP (filelist); filelist = XCDR (filelist))
6503 {
6504 Lisp_Object filename = XCAR (filelist);
6505 if (w32_BDF_to_x_font (SDATA (filename), fontname, 100))
6506 store_in_alist (&list, build_string (fontname), filename);
6507 }
6508 return list;
6509 }
6510
6511 DEFUN ("w32-find-bdf-fonts", Fw32_find_bdf_fonts, Sw32_find_bdf_fonts,
6512 1, 1, 0,
6513 doc: /* Return a list of BDF fonts in DIRECTORY.
6514 The list is suitable for appending to `w32-bdf-filename-alist'.
6515 Fonts which do not contain an xlfd description will not be included
6516 in the list. DIRECTORY may be a list of directories. */)
6517 (directory)
6518 Lisp_Object directory;
6519 {
6520 Lisp_Object list = Qnil;
6521 struct gcpro gcpro1, gcpro2;
6522
6523 if (!CONSP (directory))
6524 return w32_find_bdf_fonts_in_dir (directory);
6525
6526 for ( ; CONSP (directory); directory = XCDR (directory))
6527 {
6528 Lisp_Object pair[2];
6529 pair[0] = list;
6530 pair[1] = Qnil;
6531 GCPRO2 (directory, list);
6532 pair[1] = w32_find_bdf_fonts_in_dir ( XCAR (directory) );
6533 list = Fnconc ( 2, pair );
6534 UNGCPRO;
6535 }
6536 return list;
6537 }
6538
6539 \f
6540 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
6541 doc: /* Internal function called by `color-defined-p', which see. */)
6542 (color, frame)
6543 Lisp_Object color, frame;
6544 {
6545 XColor foo;
6546 FRAME_PTR f = check_x_frame (frame);
6547
6548 CHECK_STRING (color);
6549
6550 if (w32_defined_color (f, SDATA (color), &foo, 0))
6551 return Qt;
6552 else
6553 return Qnil;
6554 }
6555
6556 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
6557 doc: /* Internal function called by `color-values', which see. */)
6558 (color, frame)
6559 Lisp_Object color, frame;
6560 {
6561 XColor foo;
6562 FRAME_PTR f = check_x_frame (frame);
6563
6564 CHECK_STRING (color);
6565
6566 if (w32_defined_color (f, SDATA (color), &foo, 0))
6567 return list3 (make_number ((GetRValue (foo.pixel) << 8)
6568 | GetRValue (foo.pixel)),
6569 make_number ((GetGValue (foo.pixel) << 8)
6570 | GetGValue (foo.pixel)),
6571 make_number ((GetBValue (foo.pixel) << 8)
6572 | GetBValue (foo.pixel)));
6573 else
6574 return Qnil;
6575 }
6576
6577 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
6578 doc: /* Internal function called by `display-color-p', which see. */)
6579 (display)
6580 Lisp_Object display;
6581 {
6582 struct w32_display_info *dpyinfo = check_x_display_info (display);
6583
6584 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 2)
6585 return Qnil;
6586
6587 return Qt;
6588 }
6589
6590 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p,
6591 Sx_display_grayscale_p, 0, 1, 0,
6592 doc: /* Return t if DISPLAY supports shades of gray.
6593 Note that color displays do support shades of gray.
6594 The optional argument DISPLAY specifies which display to ask about.
6595 DISPLAY should be either a frame or a display name (a string).
6596 If omitted or nil, that stands for the selected frame's display. */)
6597 (display)
6598 Lisp_Object display;
6599 {
6600 struct w32_display_info *dpyinfo = check_x_display_info (display);
6601
6602 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 1)
6603 return Qnil;
6604
6605 return Qt;
6606 }
6607
6608 DEFUN ("x-display-pixel-width", Fx_display_pixel_width,
6609 Sx_display_pixel_width, 0, 1, 0,
6610 doc: /* Return the width in pixels of DISPLAY.
6611 The optional argument DISPLAY specifies which display to ask about.
6612 DISPLAY should be either a frame or a display name (a string).
6613 If omitted or nil, that stands for the selected frame's display. */)
6614 (display)
6615 Lisp_Object display;
6616 {
6617 struct w32_display_info *dpyinfo = check_x_display_info (display);
6618
6619 return make_number (dpyinfo->width);
6620 }
6621
6622 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
6623 Sx_display_pixel_height, 0, 1, 0,
6624 doc: /* Return the height in pixels of DISPLAY.
6625 The optional argument DISPLAY specifies which display to ask about.
6626 DISPLAY should be either a frame or a display name (a string).
6627 If omitted or nil, that stands for the selected frame's display. */)
6628 (display)
6629 Lisp_Object display;
6630 {
6631 struct w32_display_info *dpyinfo = check_x_display_info (display);
6632
6633 return make_number (dpyinfo->height);
6634 }
6635
6636 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
6637 0, 1, 0,
6638 doc: /* Return the number of bitplanes of DISPLAY.
6639 The optional argument DISPLAY specifies which display to ask about.
6640 DISPLAY should be either a frame or a display name (a string).
6641 If omitted or nil, that stands for the selected frame's display. */)
6642 (display)
6643 Lisp_Object display;
6644 {
6645 struct w32_display_info *dpyinfo = check_x_display_info (display);
6646
6647 return make_number (dpyinfo->n_planes * dpyinfo->n_cbits);
6648 }
6649
6650 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
6651 0, 1, 0,
6652 doc: /* Return the number of color cells of DISPLAY.
6653 The optional argument DISPLAY specifies which display to ask about.
6654 DISPLAY should be either a frame or a display name (a string).
6655 If omitted or nil, that stands for the selected frame's display. */)
6656 (display)
6657 Lisp_Object display;
6658 {
6659 struct w32_display_info *dpyinfo = check_x_display_info (display);
6660 HDC hdc;
6661 int cap;
6662
6663 hdc = GetDC (dpyinfo->root_window);
6664 if (dpyinfo->has_palette)
6665 cap = GetDeviceCaps (hdc, SIZEPALETTE);
6666 else
6667 cap = GetDeviceCaps (hdc, NUMCOLORS);
6668
6669 /* We force 24+ bit depths to 24-bit, both to prevent an overflow
6670 and because probably is more meaningful on Windows anyway */
6671 if (cap < 0)
6672 cap = 1 << min (dpyinfo->n_planes * dpyinfo->n_cbits, 24);
6673
6674 ReleaseDC (dpyinfo->root_window, hdc);
6675
6676 return make_number (cap);
6677 }
6678
6679 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
6680 Sx_server_max_request_size,
6681 0, 1, 0,
6682 doc: /* Return the maximum request size of the server of DISPLAY.
6683 The optional argument DISPLAY specifies which display to ask about.
6684 DISPLAY should be either a frame or a display name (a string).
6685 If omitted or nil, that stands for the selected frame's display. */)
6686 (display)
6687 Lisp_Object display;
6688 {
6689 struct w32_display_info *dpyinfo = check_x_display_info (display);
6690
6691 return make_number (1);
6692 }
6693
6694 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
6695 doc: /* Return the "vendor ID" string of the W32 system (Microsoft).
6696 The optional argument DISPLAY specifies which display to ask about.
6697 DISPLAY should be either a frame or a display name (a string).
6698 If omitted or nil, that stands for the selected frame's display. */)
6699 (display)
6700 Lisp_Object display;
6701 {
6702 return build_string ("Microsoft Corp.");
6703 }
6704
6705 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
6706 doc: /* Return the version numbers of the server of DISPLAY.
6707 The value is a list of three integers: the major and minor
6708 version numbers of the X Protocol in use, and the distributor-specific
6709 release number. See also the function `x-server-vendor'.
6710
6711 The optional argument DISPLAY specifies which display to ask about.
6712 DISPLAY should be either a frame or a display name (a string).
6713 If omitted or nil, that stands for the selected frame's display. */)
6714 (display)
6715 Lisp_Object display;
6716 {
6717 return Fcons (make_number (w32_major_version),
6718 Fcons (make_number (w32_minor_version),
6719 Fcons (make_number (w32_build_number), Qnil)));
6720 }
6721
6722 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
6723 doc: /* Return the number of screens on the server of DISPLAY.
6724 The optional argument DISPLAY specifies which display to ask about.
6725 DISPLAY should be either a frame or a display name (a string).
6726 If omitted or nil, that stands for the selected frame's display. */)
6727 (display)
6728 Lisp_Object display;
6729 {
6730 return make_number (1);
6731 }
6732
6733 DEFUN ("x-display-mm-height", Fx_display_mm_height,
6734 Sx_display_mm_height, 0, 1, 0,
6735 doc: /* Return the height in millimeters of DISPLAY.
6736 The optional argument DISPLAY specifies which display to ask about.
6737 DISPLAY should be either a frame or a display name (a string).
6738 If omitted or nil, that stands for the selected frame's display. */)
6739 (display)
6740 Lisp_Object display;
6741 {
6742 struct w32_display_info *dpyinfo = check_x_display_info (display);
6743 HDC hdc;
6744 int cap;
6745
6746 hdc = GetDC (dpyinfo->root_window);
6747
6748 cap = GetDeviceCaps (hdc, VERTSIZE);
6749
6750 ReleaseDC (dpyinfo->root_window, hdc);
6751
6752 return make_number (cap);
6753 }
6754
6755 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
6756 doc: /* Return the width in millimeters of DISPLAY.
6757 The optional argument DISPLAY specifies which display to ask about.
6758 DISPLAY should be either a frame or a display name (a string).
6759 If omitted or nil, that stands for the selected frame's display. */)
6760 (display)
6761 Lisp_Object display;
6762 {
6763 struct w32_display_info *dpyinfo = check_x_display_info (display);
6764
6765 HDC hdc;
6766 int cap;
6767
6768 hdc = GetDC (dpyinfo->root_window);
6769
6770 cap = GetDeviceCaps (hdc, HORZSIZE);
6771
6772 ReleaseDC (dpyinfo->root_window, hdc);
6773
6774 return make_number (cap);
6775 }
6776
6777 DEFUN ("x-display-backing-store", Fx_display_backing_store,
6778 Sx_display_backing_store, 0, 1, 0,
6779 doc: /* Return an indication of whether DISPLAY does backing store.
6780 The value may be `always', `when-mapped', or `not-useful'.
6781 The optional argument DISPLAY specifies which display to ask about.
6782 DISPLAY should be either a frame or a display name (a string).
6783 If omitted or nil, that stands for the selected frame's display. */)
6784 (display)
6785 Lisp_Object display;
6786 {
6787 return intern ("not-useful");
6788 }
6789
6790 DEFUN ("x-display-visual-class", Fx_display_visual_class,
6791 Sx_display_visual_class, 0, 1, 0,
6792 doc: /* Return the visual class of DISPLAY.
6793 The value is one of the symbols `static-gray', `gray-scale',
6794 `static-color', `pseudo-color', `true-color', or `direct-color'.
6795
6796 The optional argument DISPLAY specifies which display to ask about.
6797 DISPLAY should be either a frame or a display name (a string).
6798 If omitted or nil, that stands for the selected frame's display. */)
6799 (display)
6800 Lisp_Object display;
6801 {
6802 struct w32_display_info *dpyinfo = check_x_display_info (display);
6803 Lisp_Object result = Qnil;
6804
6805 if (dpyinfo->has_palette)
6806 result = intern ("pseudo-color");
6807 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 1)
6808 result = intern ("static-grey");
6809 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 4)
6810 result = intern ("static-color");
6811 else if (dpyinfo->n_planes * dpyinfo->n_cbits > 8)
6812 result = intern ("true-color");
6813
6814 return result;
6815 }
6816
6817 DEFUN ("x-display-save-under", Fx_display_save_under,
6818 Sx_display_save_under, 0, 1, 0,
6819 doc: /* Return t if DISPLAY supports the save-under feature.
6820 The optional argument DISPLAY specifies which display to ask about.
6821 DISPLAY should be either a frame or a display name (a string).
6822 If omitted or nil, that stands for the selected frame's display. */)
6823 (display)
6824 Lisp_Object display;
6825 {
6826 return Qnil;
6827 }
6828 \f
6829 int
6830 x_pixel_width (f)
6831 register struct frame *f;
6832 {
6833 return FRAME_PIXEL_WIDTH (f);
6834 }
6835
6836 int
6837 x_pixel_height (f)
6838 register struct frame *f;
6839 {
6840 return FRAME_PIXEL_HEIGHT (f);
6841 }
6842
6843 int
6844 x_char_width (f)
6845 register struct frame *f;
6846 {
6847 return FRAME_COLUMN_WIDTH (f);
6848 }
6849
6850 int
6851 x_char_height (f)
6852 register struct frame *f;
6853 {
6854 return FRAME_LINE_HEIGHT (f);
6855 }
6856
6857 int
6858 x_screen_planes (f)
6859 register struct frame *f;
6860 {
6861 return FRAME_W32_DISPLAY_INFO (f)->n_planes;
6862 }
6863 \f
6864 /* Return the display structure for the display named NAME.
6865 Open a new connection if necessary. */
6866
6867 struct w32_display_info *
6868 x_display_info_for_name (name)
6869 Lisp_Object name;
6870 {
6871 Lisp_Object names;
6872 struct w32_display_info *dpyinfo;
6873
6874 CHECK_STRING (name);
6875
6876 for (dpyinfo = &one_w32_display_info, names = w32_display_name_list;
6877 dpyinfo;
6878 dpyinfo = dpyinfo->next, names = XCDR (names))
6879 {
6880 Lisp_Object tem;
6881 tem = Fstring_equal (XCAR (XCAR (names)), name);
6882 if (!NILP (tem))
6883 return dpyinfo;
6884 }
6885
6886 /* Use this general default value to start with. */
6887 Vx_resource_name = Vinvocation_name;
6888
6889 validate_x_resource_name ();
6890
6891 dpyinfo = w32_term_init (name, (unsigned char *)0,
6892 (char *) SDATA (Vx_resource_name));
6893
6894 if (dpyinfo == 0)
6895 error ("Cannot connect to server %s", SDATA (name));
6896
6897 w32_in_use = 1;
6898 XSETFASTINT (Vwindow_system_version, 3);
6899
6900 return dpyinfo;
6901 }
6902
6903 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
6904 1, 3, 0, doc: /* Open a connection to a server.
6905 DISPLAY is the name of the display to connect to.
6906 Optional second arg XRM-STRING is a string of resources in xrdb format.
6907 If the optional third arg MUST-SUCCEED is non-nil,
6908 terminate Emacs if we can't open the connection. */)
6909 (display, xrm_string, must_succeed)
6910 Lisp_Object display, xrm_string, must_succeed;
6911 {
6912 unsigned char *xrm_option;
6913 struct w32_display_info *dpyinfo;
6914
6915 /* If initialization has already been done, return now to avoid
6916 overwriting critical parts of one_w32_display_info. */
6917 if (w32_in_use)
6918 return Qnil;
6919
6920 CHECK_STRING (display);
6921 if (! NILP (xrm_string))
6922 CHECK_STRING (xrm_string);
6923
6924 #if 0
6925 if (! EQ (Vwindow_system, intern ("w32")))
6926 error ("Not using Microsoft Windows");
6927 #endif
6928
6929 /* Allow color mapping to be defined externally; first look in user's
6930 HOME directory, then in Emacs etc dir for a file called rgb.txt. */
6931 {
6932 Lisp_Object color_file;
6933 struct gcpro gcpro1;
6934
6935 color_file = build_string ("~/rgb.txt");
6936
6937 GCPRO1 (color_file);
6938
6939 if (NILP (Ffile_readable_p (color_file)))
6940 color_file =
6941 Fexpand_file_name (build_string ("rgb.txt"),
6942 Fsymbol_value (intern ("data-directory")));
6943
6944 Vw32_color_map = Fw32_load_color_file (color_file);
6945
6946 UNGCPRO;
6947 }
6948 if (NILP (Vw32_color_map))
6949 Vw32_color_map = Fw32_default_color_map ();
6950
6951 /* Merge in system logical colors. */
6952 add_system_logical_colors_to_map (&Vw32_color_map);
6953
6954 if (! NILP (xrm_string))
6955 xrm_option = (unsigned char *) SDATA (xrm_string);
6956 else
6957 xrm_option = (unsigned char *) 0;
6958
6959 /* Use this general default value to start with. */
6960 /* First remove .exe suffix from invocation-name - it looks ugly. */
6961 {
6962 char basename[ MAX_PATH ], *str;
6963
6964 strcpy (basename, SDATA (Vinvocation_name));
6965 str = strrchr (basename, '.');
6966 if (str) *str = 0;
6967 Vinvocation_name = build_string (basename);
6968 }
6969 Vx_resource_name = Vinvocation_name;
6970
6971 validate_x_resource_name ();
6972
6973 /* This is what opens the connection and sets x_current_display.
6974 This also initializes many symbols, such as those used for input. */
6975 dpyinfo = w32_term_init (display, xrm_option,
6976 (char *) SDATA (Vx_resource_name));
6977
6978 if (dpyinfo == 0)
6979 {
6980 if (!NILP (must_succeed))
6981 fatal ("Cannot connect to server %s.\n",
6982 SDATA (display));
6983 else
6984 error ("Cannot connect to server %s", SDATA (display));
6985 }
6986
6987 w32_in_use = 1;
6988
6989 XSETFASTINT (Vwindow_system_version, 3);
6990 return Qnil;
6991 }
6992
6993 DEFUN ("x-close-connection", Fx_close_connection,
6994 Sx_close_connection, 1, 1, 0,
6995 doc: /* Close the connection to DISPLAY's server.
6996 For DISPLAY, specify either a frame or a display name (a string).
6997 If DISPLAY is nil, that stands for the selected frame's display. */)
6998 (display)
6999 Lisp_Object display;
7000 {
7001 struct w32_display_info *dpyinfo = check_x_display_info (display);
7002 int i;
7003
7004 if (dpyinfo->reference_count > 0)
7005 error ("Display still has frames on it");
7006
7007 BLOCK_INPUT;
7008 /* Free the fonts in the font table. */
7009 for (i = 0; i < dpyinfo->n_fonts; i++)
7010 if (dpyinfo->font_table[i].name)
7011 {
7012 if (dpyinfo->font_table[i].name != dpyinfo->font_table[i].full_name)
7013 xfree (dpyinfo->font_table[i].full_name);
7014 xfree (dpyinfo->font_table[i].name);
7015 w32_unload_font (dpyinfo, dpyinfo->font_table[i].font);
7016 }
7017 x_destroy_all_bitmaps (dpyinfo);
7018
7019 x_delete_display (dpyinfo);
7020 UNBLOCK_INPUT;
7021
7022 return Qnil;
7023 }
7024
7025 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
7026 doc: /* Return the list of display names that Emacs has connections to. */)
7027 ()
7028 {
7029 Lisp_Object tail, result;
7030
7031 result = Qnil;
7032 for (tail = w32_display_name_list; CONSP (tail); tail = XCDR (tail))
7033 result = Fcons (XCAR (XCAR (tail)), result);
7034
7035 return result;
7036 }
7037
7038 DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
7039 doc: /* This is a noop on W32 systems. */)
7040 (on, display)
7041 Lisp_Object display, on;
7042 {
7043 return Qnil;
7044 }
7045
7046
7047 \f
7048 /***********************************************************************
7049 Window properties
7050 ***********************************************************************/
7051
7052 DEFUN ("x-change-window-property", Fx_change_window_property,
7053 Sx_change_window_property, 2, 6, 0,
7054 doc: /* Change window property PROP to VALUE on the X window of FRAME.
7055 VALUE may be a string or a list of conses, numbers and/or strings.
7056 If an element in the list is a string, it is converted to
7057 an Atom and the value of the Atom is used. If an element is a cons,
7058 it is converted to a 32 bit number where the car is the 16 top bits and the
7059 cdr is the lower 16 bits.
7060 FRAME nil or omitted means use the selected frame.
7061 If TYPE is given and non-nil, it is the name of the type of VALUE.
7062 If TYPE is not given or nil, the type is STRING.
7063 FORMAT gives the size in bits of each element if VALUE is a list.
7064 It must be one of 8, 16 or 32.
7065 If VALUE is a string or FORMAT is nil or not given, FORMAT defaults to 8.
7066 If OUTER_P is non-nil, the property is changed for the outer X window of
7067 FRAME. Default is to change on the edit X window.
7068
7069 Value is VALUE. */)
7070 (prop, value, frame, type, format, outer_p)
7071 Lisp_Object prop, value, frame, type, format, outer_p;
7072 {
7073 #if 0 /* TODO : port window properties to W32 */
7074 struct frame *f = check_x_frame (frame);
7075 Atom prop_atom;
7076
7077 CHECK_STRING (prop);
7078 CHECK_STRING (value);
7079
7080 BLOCK_INPUT;
7081 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
7082 XChangeProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
7083 prop_atom, XA_STRING, 8, PropModeReplace,
7084 SDATA (value), SCHARS (value));
7085
7086 /* Make sure the property is set when we return. */
7087 XFlush (FRAME_W32_DISPLAY (f));
7088 UNBLOCK_INPUT;
7089
7090 #endif /* TODO */
7091
7092 return value;
7093 }
7094
7095
7096 DEFUN ("x-delete-window-property", Fx_delete_window_property,
7097 Sx_delete_window_property, 1, 2, 0,
7098 doc: /* Remove window property PROP from X window of FRAME.
7099 FRAME nil or omitted means use the selected frame. Value is PROP. */)
7100 (prop, frame)
7101 Lisp_Object prop, frame;
7102 {
7103 #if 0 /* TODO : port window properties to W32 */
7104
7105 struct frame *f = check_x_frame (frame);
7106 Atom prop_atom;
7107
7108 CHECK_STRING (prop);
7109 BLOCK_INPUT;
7110 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
7111 XDeleteProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), prop_atom);
7112
7113 /* Make sure the property is removed when we return. */
7114 XFlush (FRAME_W32_DISPLAY (f));
7115 UNBLOCK_INPUT;
7116 #endif /* TODO */
7117
7118 return prop;
7119 }
7120
7121
7122 DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
7123 1, 2, 0,
7124 doc: /* Value is the value of window property PROP on FRAME.
7125 If FRAME is nil or omitted, use the selected frame. Value is nil
7126 if FRAME hasn't a property with name PROP or if PROP has no string
7127 value. */)
7128 (prop, frame)
7129 Lisp_Object prop, frame;
7130 {
7131 #if 0 /* TODO : port window properties to W32 */
7132
7133 struct frame *f = check_x_frame (frame);
7134 Atom prop_atom;
7135 int rc;
7136 Lisp_Object prop_value = Qnil;
7137 char *tmp_data = NULL;
7138 Atom actual_type;
7139 int actual_format;
7140 unsigned long actual_size, bytes_remaining;
7141
7142 CHECK_STRING (prop);
7143 BLOCK_INPUT;
7144 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
7145 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
7146 prop_atom, 0, 0, False, XA_STRING,
7147 &actual_type, &actual_format, &actual_size,
7148 &bytes_remaining, (unsigned char **) &tmp_data);
7149 if (rc == Success)
7150 {
7151 int size = bytes_remaining;
7152
7153 XFree (tmp_data);
7154 tmp_data = NULL;
7155
7156 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
7157 prop_atom, 0, bytes_remaining,
7158 False, XA_STRING,
7159 &actual_type, &actual_format,
7160 &actual_size, &bytes_remaining,
7161 (unsigned char **) &tmp_data);
7162 if (rc == Success)
7163 prop_value = make_string (tmp_data, size);
7164
7165 XFree (tmp_data);
7166 }
7167
7168 UNBLOCK_INPUT;
7169
7170 return prop_value;
7171
7172 #endif /* TODO */
7173 return Qnil;
7174 }
7175
7176
7177 \f
7178 /***********************************************************************
7179 Busy cursor
7180 ***********************************************************************/
7181
7182 /* If non-null, an asynchronous timer that, when it expires, displays
7183 an hourglass cursor on all frames. */
7184
7185 static struct atimer *hourglass_atimer;
7186
7187 /* Non-zero means an hourglass cursor is currently shown. */
7188
7189 static int hourglass_shown_p;
7190
7191 /* Number of seconds to wait before displaying an hourglass cursor. */
7192
7193 static Lisp_Object Vhourglass_delay;
7194
7195 /* Default number of seconds to wait before displaying an hourglass
7196 cursor. */
7197
7198 #define DEFAULT_HOURGLASS_DELAY 1
7199
7200 /* Function prototypes. */
7201
7202 static void show_hourglass P_ ((struct atimer *));
7203 static void hide_hourglass P_ ((void));
7204
7205
7206 /* Cancel a currently active hourglass timer, and start a new one. */
7207
7208 void
7209 start_hourglass ()
7210 {
7211 #if 0 /* TODO: cursor shape changes. */
7212 EMACS_TIME delay;
7213 int secs, usecs = 0;
7214
7215 cancel_hourglass ();
7216
7217 if (INTEGERP (Vhourglass_delay)
7218 && XINT (Vhourglass_delay) > 0)
7219 secs = XFASTINT (Vhourglass_delay);
7220 else if (FLOATP (Vhourglass_delay)
7221 && XFLOAT_DATA (Vhourglass_delay) > 0)
7222 {
7223 Lisp_Object tem;
7224 tem = Ftruncate (Vhourglass_delay, Qnil);
7225 secs = XFASTINT (tem);
7226 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
7227 }
7228 else
7229 secs = DEFAULT_HOURGLASS_DELAY;
7230
7231 EMACS_SET_SECS_USECS (delay, secs, usecs);
7232 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
7233 show_hourglass, NULL);
7234 #endif
7235 }
7236
7237
7238 /* Cancel the hourglass cursor timer if active, hide an hourglass
7239 cursor if shown. */
7240
7241 void
7242 cancel_hourglass ()
7243 {
7244 if (hourglass_atimer)
7245 {
7246 cancel_atimer (hourglass_atimer);
7247 hourglass_atimer = NULL;
7248 }
7249
7250 if (hourglass_shown_p)
7251 hide_hourglass ();
7252 }
7253
7254
7255 /* Timer function of hourglass_atimer. TIMER is equal to
7256 hourglass_atimer.
7257
7258 Display an hourglass cursor on all frames by mapping the frames'
7259 hourglass_window. Set the hourglass_p flag in the frames'
7260 output_data.x structure to indicate that an hourglass cursor is
7261 shown on the frames. */
7262
7263 static void
7264 show_hourglass (timer)
7265 struct atimer *timer;
7266 {
7267 #if 0 /* TODO: cursor shape changes. */
7268 /* The timer implementation will cancel this timer automatically
7269 after this function has run. Set hourglass_atimer to null
7270 so that we know the timer doesn't have to be canceled. */
7271 hourglass_atimer = NULL;
7272
7273 if (!hourglass_shown_p)
7274 {
7275 Lisp_Object rest, frame;
7276
7277 BLOCK_INPUT;
7278
7279 FOR_EACH_FRAME (rest, frame)
7280 if (FRAME_W32_P (XFRAME (frame)))
7281 {
7282 struct frame *f = XFRAME (frame);
7283
7284 f->output_data.w32->hourglass_p = 1;
7285
7286 if (!f->output_data.w32->hourglass_window)
7287 {
7288 unsigned long mask = CWCursor;
7289 XSetWindowAttributes attrs;
7290
7291 attrs.cursor = f->output_data.w32->hourglass_cursor;
7292
7293 f->output_data.w32->hourglass_window
7294 = XCreateWindow (FRAME_X_DISPLAY (f),
7295 FRAME_OUTER_WINDOW (f),
7296 0, 0, 32000, 32000, 0, 0,
7297 InputOnly,
7298 CopyFromParent,
7299 mask, &attrs);
7300 }
7301
7302 XMapRaised (FRAME_X_DISPLAY (f),
7303 f->output_data.w32->hourglass_window);
7304 XFlush (FRAME_X_DISPLAY (f));
7305 }
7306
7307 hourglass_shown_p = 1;
7308 UNBLOCK_INPUT;
7309 }
7310 #endif
7311 }
7312
7313
7314 /* Hide the hourglass cursor on all frames, if it is currently shown. */
7315
7316 static void
7317 hide_hourglass ()
7318 {
7319 #if 0 /* TODO: cursor shape changes. */
7320 if (hourglass_shown_p)
7321 {
7322 Lisp_Object rest, frame;
7323
7324 BLOCK_INPUT;
7325 FOR_EACH_FRAME (rest, frame)
7326 {
7327 struct frame *f = XFRAME (frame);
7328
7329 if (FRAME_W32_P (f)
7330 /* Watch out for newly created frames. */
7331 && f->output_data.x->hourglass_window)
7332 {
7333 XUnmapWindow (FRAME_X_DISPLAY (f),
7334 f->output_data.x->hourglass_window);
7335 /* Sync here because XTread_socket looks at the
7336 hourglass_p flag that is reset to zero below. */
7337 XSync (FRAME_X_DISPLAY (f), False);
7338 f->output_data.x->hourglass_p = 0;
7339 }
7340 }
7341
7342 hourglass_shown_p = 0;
7343 UNBLOCK_INPUT;
7344 }
7345 #endif
7346 }
7347
7348
7349 \f
7350 /***********************************************************************
7351 Tool tips
7352 ***********************************************************************/
7353
7354 static Lisp_Object x_create_tip_frame P_ ((struct w32_display_info *,
7355 Lisp_Object, Lisp_Object));
7356 static void compute_tip_xy P_ ((struct frame *, Lisp_Object, Lisp_Object,
7357 Lisp_Object, int, int, int *, int *));
7358
7359 /* The frame of a currently visible tooltip. */
7360
7361 Lisp_Object tip_frame;
7362
7363 /* If non-nil, a timer started that hides the last tooltip when it
7364 fires. */
7365
7366 Lisp_Object tip_timer;
7367 Window tip_window;
7368
7369 /* If non-nil, a vector of 3 elements containing the last args
7370 with which x-show-tip was called. See there. */
7371
7372 Lisp_Object last_show_tip_args;
7373
7374 /* Maximum size for tooltips; a cons (COLUMNS . ROWS). */
7375
7376 Lisp_Object Vx_max_tooltip_size;
7377
7378
7379 static Lisp_Object
7380 unwind_create_tip_frame (frame)
7381 Lisp_Object frame;
7382 {
7383 Lisp_Object deleted;
7384
7385 deleted = unwind_create_frame (frame);
7386 if (EQ (deleted, Qt))
7387 {
7388 tip_window = NULL;
7389 tip_frame = Qnil;
7390 }
7391
7392 return deleted;
7393 }
7394
7395
7396 /* Create a frame for a tooltip on the display described by DPYINFO.
7397 PARMS is a list of frame parameters. TEXT is the string to
7398 display in the tip frame. Value is the frame.
7399
7400 Note that functions called here, esp. x_default_parameter can
7401 signal errors, for instance when a specified color name is
7402 undefined. We have to make sure that we're in a consistent state
7403 when this happens. */
7404
7405 static Lisp_Object
7406 x_create_tip_frame (dpyinfo, parms, text)
7407 struct w32_display_info *dpyinfo;
7408 Lisp_Object parms, text;
7409 {
7410 struct frame *f;
7411 Lisp_Object frame, tem;
7412 Lisp_Object name;
7413 long window_prompting = 0;
7414 int width, height;
7415 int count = SPECPDL_INDEX ();
7416 struct gcpro gcpro1, gcpro2, gcpro3;
7417 struct kboard *kb;
7418 int face_change_count_before = face_change_count;
7419 Lisp_Object buffer;
7420 struct buffer *old_buffer;
7421
7422 check_w32 ();
7423
7424 /* Use this general default value to start with until we know if
7425 this frame has a specified name. */
7426 Vx_resource_name = Vinvocation_name;
7427
7428 #ifdef MULTI_KBOARD
7429 kb = dpyinfo->terminal->kboard;
7430 #else
7431 kb = &the_only_kboard;
7432 #endif
7433
7434 /* Get the name of the frame to use for resource lookup. */
7435 name = w32_get_arg (parms, Qname, "name", "Name", RES_TYPE_STRING);
7436 if (!STRINGP (name)
7437 && !EQ (name, Qunbound)
7438 && !NILP (name))
7439 error ("Invalid frame name--not a string or nil");
7440 Vx_resource_name = name;
7441
7442 frame = Qnil;
7443 GCPRO3 (parms, name, frame);
7444 /* Make a frame without minibuffer nor mode-line. */
7445 f = make_frame (0);
7446 f->wants_modeline = 0;
7447 XSETFRAME (frame, f);
7448
7449 buffer = Fget_buffer_create (build_string (" *tip*"));
7450 Fset_window_buffer (FRAME_ROOT_WINDOW (f), buffer, Qnil);
7451 old_buffer = current_buffer;
7452 set_buffer_internal_1 (XBUFFER (buffer));
7453 current_buffer->truncate_lines = Qnil;
7454 specbind (Qinhibit_read_only, Qt);
7455 specbind (Qinhibit_modification_hooks, Qt);
7456 Ferase_buffer ();
7457 Finsert (1, &text);
7458 set_buffer_internal_1 (old_buffer);
7459
7460 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
7461 record_unwind_protect (unwind_create_tip_frame, frame);
7462
7463 /* By setting the output method, we're essentially saying that
7464 the frame is live, as per FRAME_LIVE_P. If we get a signal
7465 from this point on, x_destroy_window might screw up reference
7466 counts etc. */
7467 f->terminal = dpyinfo->terminal;
7468 f->terminal->reference_count++;
7469 f->output_method = output_w32;
7470 f->output_data.w32 =
7471 (struct w32_output *) xmalloc (sizeof (struct w32_output));
7472 bzero (f->output_data.w32, sizeof (struct w32_output));
7473
7474 FRAME_FONTSET (f) = -1;
7475 f->icon_name = Qnil;
7476
7477 #if 0 /* GLYPH_DEBUG TODO: image support. */
7478 image_cache_refcount = FRAME_X_IMAGE_CACHE (f)->refcount;
7479 dpyinfo_refcount = dpyinfo->reference_count;
7480 #endif /* GLYPH_DEBUG */
7481 #ifdef MULTI_KBOARD
7482 FRAME_KBOARD (f) = kb;
7483 #endif
7484 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
7485 f->output_data.w32->explicit_parent = 0;
7486
7487 /* Set the name; the functions to which we pass f expect the name to
7488 be set. */
7489 if (EQ (name, Qunbound) || NILP (name))
7490 {
7491 f->name = build_string (dpyinfo->w32_id_name);
7492 f->explicit_name = 0;
7493 }
7494 else
7495 {
7496 f->name = name;
7497 f->explicit_name = 1;
7498 /* use the frame's title when getting resources for this frame. */
7499 specbind (Qx_resource_name, name);
7500 }
7501
7502 f->resx = dpyinfo->resx;
7503 f->resy = dpyinfo->resy;
7504
7505 #ifdef USE_FONT_BACKEND
7506 if (enable_font_backend)
7507 {
7508 /* Perhaps, we must allow frame parameter, say `font-backend',
7509 to specify which font backends to use. */
7510 register_font_driver (&w32font_driver, f);
7511
7512 x_default_parameter (f, parms, Qfont_backend, Qnil,
7513 "fontBackend", "FontBackend", RES_TYPE_STRING);
7514 }
7515 #endif /* USE_FONT_BACKEND */
7516
7517 /* Extract the window parameters from the supplied values
7518 that are needed to determine window geometry. */
7519 #ifdef USE_FONT_BACKEND
7520 if (enable_font_backend)
7521 x_default_font_parameter (f, parms);
7522 else
7523 #endif /* USE_FONT_BACKEND */
7524 {
7525 Lisp_Object font;
7526
7527 font = w32_get_arg (parms, Qfont, "font", "Font", RES_TYPE_STRING);
7528
7529 BLOCK_INPUT;
7530 /* First, try whatever font the caller has specified. */
7531 if (STRINGP (font))
7532 {
7533 tem = Fquery_fontset (font, Qnil);
7534 if (STRINGP (tem))
7535 font = x_new_fontset (f, tem);
7536 else
7537 font = x_new_font (f, SDATA (font));
7538 }
7539
7540 /* Try out a font which we hope has bold and italic variations. */
7541 if (!STRINGP (font))
7542 font = x_new_font (f, "-*-Courier New-normal-r-*-*-*-100-*-*-c-*-iso8859-1");
7543 if (! STRINGP (font))
7544 font = x_new_font (f, "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1");
7545 /* If those didn't work, look for something which will at least work. */
7546 if (! STRINGP (font))
7547 font = x_new_font (f, "-*-Fixedsys-normal-r-*-*-12-*-*-*-c-*-iso8859-1");
7548 UNBLOCK_INPUT;
7549 if (! STRINGP (font))
7550 font = build_string ("Fixedsys");
7551
7552 x_default_parameter (f, parms, Qfont, font,
7553 "font", "Font", RES_TYPE_STRING);
7554 }
7555
7556 x_default_parameter (f, parms, Qborder_width, make_number (2),
7557 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
7558 /* This defaults to 2 in order to match xterm. We recognize either
7559 internalBorderWidth or internalBorder (which is what xterm calls
7560 it). */
7561 if (NILP (Fassq (Qinternal_border_width, parms)))
7562 {
7563 Lisp_Object value;
7564
7565 value = w32_get_arg (parms, Qinternal_border_width,
7566 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
7567 if (! EQ (value, Qunbound))
7568 parms = Fcons (Fcons (Qinternal_border_width, value),
7569 parms);
7570 }
7571 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
7572 "internalBorderWidth", "internalBorderWidth",
7573 RES_TYPE_NUMBER);
7574
7575 /* Also do the stuff which must be set before the window exists. */
7576 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
7577 "foreground", "Foreground", RES_TYPE_STRING);
7578 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
7579 "background", "Background", RES_TYPE_STRING);
7580 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
7581 "pointerColor", "Foreground", RES_TYPE_STRING);
7582 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
7583 "cursorColor", "Foreground", RES_TYPE_STRING);
7584 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
7585 "borderColor", "BorderColor", RES_TYPE_STRING);
7586
7587 /* Init faces before x_default_parameter is called for scroll-bar
7588 parameters because that function calls x_set_scroll_bar_width,
7589 which calls change_frame_size, which calls Fset_window_buffer,
7590 which runs hooks, which call Fvertical_motion. At the end, we
7591 end up in init_iterator with a null face cache, which should not
7592 happen. */
7593 init_frame_faces (f);
7594
7595 f->output_data.w32->dwStyle = WS_BORDER | WS_POPUP | WS_DISABLED;
7596 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
7597
7598 window_prompting = x_figure_window_size (f, parms, 0);
7599
7600 /* No fringes on tip frame. */
7601 f->fringe_cols = 0;
7602 f->left_fringe_width = 0;
7603 f->right_fringe_width = 0;
7604
7605 BLOCK_INPUT;
7606 my_create_tip_window (f);
7607 UNBLOCK_INPUT;
7608
7609 x_make_gc (f);
7610
7611 x_default_parameter (f, parms, Qauto_raise, Qnil,
7612 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
7613 x_default_parameter (f, parms, Qauto_lower, Qnil,
7614 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
7615 x_default_parameter (f, parms, Qcursor_type, Qbox,
7616 "cursorType", "CursorType", RES_TYPE_SYMBOL);
7617
7618 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
7619 Change will not be effected unless different from the current
7620 FRAME_LINES (f). */
7621 width = FRAME_COLS (f);
7622 height = FRAME_LINES (f);
7623 FRAME_LINES (f) = 0;
7624 SET_FRAME_COLS (f, 0);
7625 change_frame_size (f, height, width, 1, 0, 0);
7626
7627 /* Add `tooltip' frame parameter's default value. */
7628 if (NILP (Fframe_parameter (frame, intern ("tooltip"))))
7629 Fmodify_frame_parameters (frame, Fcons (Fcons (intern ("tooltip"), Qt),
7630 Qnil));
7631
7632 /* Set up faces after all frame parameters are known. This call
7633 also merges in face attributes specified for new frames.
7634
7635 Frame parameters may be changed if .Xdefaults contains
7636 specifications for the default font. For example, if there is an
7637 `Emacs.default.attributeBackground: pink', the `background-color'
7638 attribute of the frame get's set, which let's the internal border
7639 of the tooltip frame appear in pink. Prevent this. */
7640 {
7641 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
7642
7643 /* Set tip_frame here, so that */
7644 tip_frame = frame;
7645 call1 (Qface_set_after_frame_default, frame);
7646
7647 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
7648 Fmodify_frame_parameters (frame, Fcons (Fcons (Qbackground_color, bg),
7649 Qnil));
7650 }
7651
7652 f->no_split = 1;
7653
7654 UNGCPRO;
7655
7656 /* It is now ok to make the frame official even if we get an error
7657 below. And the frame needs to be on Vframe_list or making it
7658 visible won't work. */
7659 Vframe_list = Fcons (frame, Vframe_list);
7660
7661 /* Now that the frame is official, it counts as a reference to
7662 its display. */
7663 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
7664
7665 /* Setting attributes of faces of the tooltip frame from resources
7666 and similar will increment face_change_count, which leads to the
7667 clearing of all current matrices. Since this isn't necessary
7668 here, avoid it by resetting face_change_count to the value it
7669 had before we created the tip frame. */
7670 face_change_count = face_change_count_before;
7671
7672 /* Discard the unwind_protect. */
7673 return unbind_to (count, frame);
7674 }
7675
7676
7677 /* Compute where to display tip frame F. PARMS is the list of frame
7678 parameters for F. DX and DY are specified offsets from the current
7679 location of the mouse. WIDTH and HEIGHT are the width and height
7680 of the tooltip. Return coordinates relative to the root window of
7681 the display in *ROOT_X, and *ROOT_Y. */
7682
7683 static void
7684 compute_tip_xy (f, parms, dx, dy, width, height, root_x, root_y)
7685 struct frame *f;
7686 Lisp_Object parms, dx, dy;
7687 int width, height;
7688 int *root_x, *root_y;
7689 {
7690 Lisp_Object left, top;
7691
7692 /* User-specified position? */
7693 left = Fcdr (Fassq (Qleft, parms));
7694 top = Fcdr (Fassq (Qtop, parms));
7695
7696 /* Move the tooltip window where the mouse pointer is. Resize and
7697 show it. */
7698 if (!INTEGERP (left) || !INTEGERP (top))
7699 {
7700 POINT pt;
7701
7702 BLOCK_INPUT;
7703 GetCursorPos (&pt);
7704 *root_x = pt.x;
7705 *root_y = pt.y;
7706 UNBLOCK_INPUT;
7707 }
7708
7709 if (INTEGERP (top))
7710 *root_y = XINT (top);
7711 else if (*root_y + XINT (dy) <= 0)
7712 *root_y = 0; /* Can happen for negative dy */
7713 else if (*root_y + XINT (dy) + height <= FRAME_W32_DISPLAY_INFO (f)->height)
7714 /* It fits below the pointer */
7715 *root_y += XINT (dy);
7716 else if (height + XINT (dy) <= *root_y)
7717 /* It fits above the pointer. */
7718 *root_y -= height + XINT (dy);
7719 else
7720 /* Put it on the top. */
7721 *root_y = 0;
7722
7723 if (INTEGERP (left))
7724 *root_x = XINT (left);
7725 else if (*root_x + XINT (dx) <= 0)
7726 *root_x = 0; /* Can happen for negative dx */
7727 else if (*root_x + XINT (dx) + width <= FRAME_W32_DISPLAY_INFO (f)->width)
7728 /* It fits to the right of the pointer. */
7729 *root_x += XINT (dx);
7730 else if (width + XINT (dx) <= *root_x)
7731 /* It fits to the left of the pointer. */
7732 *root_x -= width + XINT (dx);
7733 else
7734 /* Put it left justified on the screen -- it ought to fit that way. */
7735 *root_x = 0;
7736 }
7737
7738
7739 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
7740 doc: /* Show STRING in a \"tooltip\" window on frame FRAME.
7741 A tooltip window is a small window displaying a string.
7742
7743 This is an internal function; Lisp code should call `tooltip-show'.
7744
7745 FRAME nil or omitted means use the selected frame.
7746
7747 PARMS is an optional list of frame parameters which can be
7748 used to change the tooltip's appearance.
7749
7750 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
7751 means use the default timeout of 5 seconds.
7752
7753 If the list of frame parameters PARMS contains a `left' parameter,
7754 the tooltip is displayed at that x-position. Otherwise it is
7755 displayed at the mouse position, with offset DX added (default is 5 if
7756 DX isn't specified). Likewise for the y-position; if a `top' frame
7757 parameter is specified, it determines the y-position of the tooltip
7758 window, otherwise it is displayed at the mouse position, with offset
7759 DY added (default is -10).
7760
7761 A tooltip's maximum size is specified by `x-max-tooltip-size'.
7762 Text larger than the specified size is clipped. */)
7763 (string, frame, parms, timeout, dx, dy)
7764 Lisp_Object string, frame, parms, timeout, dx, dy;
7765 {
7766 struct frame *f;
7767 struct window *w;
7768 int root_x, root_y;
7769 struct buffer *old_buffer;
7770 struct text_pos pos;
7771 int i, width, height;
7772 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7773 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7774 int count = SPECPDL_INDEX ();
7775
7776 specbind (Qinhibit_redisplay, Qt);
7777
7778 GCPRO4 (string, parms, frame, timeout);
7779
7780 CHECK_STRING (string);
7781 f = check_x_frame (frame);
7782 if (NILP (timeout))
7783 timeout = make_number (5);
7784 else
7785 CHECK_NATNUM (timeout);
7786
7787 if (NILP (dx))
7788 dx = make_number (5);
7789 else
7790 CHECK_NUMBER (dx);
7791
7792 if (NILP (dy))
7793 dy = make_number (-10);
7794 else
7795 CHECK_NUMBER (dy);
7796
7797 if (NILP (last_show_tip_args))
7798 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
7799
7800 if (!NILP (tip_frame))
7801 {
7802 Lisp_Object last_string = AREF (last_show_tip_args, 0);
7803 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
7804 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
7805
7806 if (EQ (frame, last_frame)
7807 && !NILP (Fequal (last_string, string))
7808 && !NILP (Fequal (last_parms, parms)))
7809 {
7810 struct frame *f = XFRAME (tip_frame);
7811
7812 /* Only DX and DY have changed. */
7813 if (!NILP (tip_timer))
7814 {
7815 Lisp_Object timer = tip_timer;
7816 tip_timer = Qnil;
7817 call1 (Qcancel_timer, timer);
7818 }
7819
7820 BLOCK_INPUT;
7821 compute_tip_xy (f, parms, dx, dy, FRAME_PIXEL_WIDTH (f),
7822 FRAME_PIXEL_HEIGHT (f), &root_x, &root_y);
7823
7824 /* Put tooltip in topmost group and in position. */
7825 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
7826 root_x, root_y, 0, 0,
7827 SWP_NOSIZE | SWP_NOACTIVATE);
7828
7829 /* Ensure tooltip is on top of other topmost windows (eg menus). */
7830 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
7831 0, 0, 0, 0,
7832 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
7833
7834 UNBLOCK_INPUT;
7835 goto start_timer;
7836 }
7837 }
7838
7839 /* Hide a previous tip, if any. */
7840 Fx_hide_tip ();
7841
7842 ASET (last_show_tip_args, 0, string);
7843 ASET (last_show_tip_args, 1, frame);
7844 ASET (last_show_tip_args, 2, parms);
7845
7846 /* Add default values to frame parameters. */
7847 if (NILP (Fassq (Qname, parms)))
7848 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
7849 if (NILP (Fassq (Qinternal_border_width, parms)))
7850 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
7851 if (NILP (Fassq (Qborder_width, parms)))
7852 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
7853 if (NILP (Fassq (Qborder_color, parms)))
7854 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
7855 if (NILP (Fassq (Qbackground_color, parms)))
7856 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
7857 parms);
7858
7859 /* Block input until the tip has been fully drawn, to avoid crashes
7860 when drawing tips in menus. */
7861 BLOCK_INPUT;
7862
7863 /* Create a frame for the tooltip, and record it in the global
7864 variable tip_frame. */
7865 frame = x_create_tip_frame (FRAME_W32_DISPLAY_INFO (f), parms, string);
7866 f = XFRAME (frame);
7867
7868 /* Set up the frame's root window. */
7869 w = XWINDOW (FRAME_ROOT_WINDOW (f));
7870 w->left_col = w->top_line = make_number (0);
7871
7872 if (CONSP (Vx_max_tooltip_size)
7873 && INTEGERP (XCAR (Vx_max_tooltip_size))
7874 && XINT (XCAR (Vx_max_tooltip_size)) > 0
7875 && INTEGERP (XCDR (Vx_max_tooltip_size))
7876 && XINT (XCDR (Vx_max_tooltip_size)) > 0)
7877 {
7878 w->total_cols = XCAR (Vx_max_tooltip_size);
7879 w->total_lines = XCDR (Vx_max_tooltip_size);
7880 }
7881 else
7882 {
7883 w->total_cols = make_number (80);
7884 w->total_lines = make_number (40);
7885 }
7886
7887 FRAME_TOTAL_COLS (f) = XINT (w->total_cols);
7888 adjust_glyphs (f);
7889 w->pseudo_window_p = 1;
7890
7891 /* Display the tooltip text in a temporary buffer. */
7892 old_buffer = current_buffer;
7893 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->buffer));
7894 current_buffer->truncate_lines = Qnil;
7895 clear_glyph_matrix (w->desired_matrix);
7896 clear_glyph_matrix (w->current_matrix);
7897 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
7898 try_window (FRAME_ROOT_WINDOW (f), pos, 0);
7899
7900 /* Compute width and height of the tooltip. */
7901 width = height = 0;
7902 for (i = 0; i < w->desired_matrix->nrows; ++i)
7903 {
7904 struct glyph_row *row = &w->desired_matrix->rows[i];
7905 struct glyph *last;
7906 int row_width;
7907
7908 /* Stop at the first empty row at the end. */
7909 if (!row->enabled_p || !row->displays_text_p)
7910 break;
7911
7912 /* Let the row go over the full width of the frame. */
7913 row->full_width_p = 1;
7914
7915 #ifdef TODO /* Investigate why some fonts need more width than is
7916 calculated for some tooltips. */
7917 /* There's a glyph at the end of rows that is use to place
7918 the cursor there. Don't include the width of this glyph. */
7919 if (row->used[TEXT_AREA])
7920 {
7921 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
7922 row_width = row->pixel_width - last->pixel_width;
7923 }
7924 else
7925 #endif
7926 row_width = row->pixel_width;
7927
7928 /* TODO: find why tips do not draw along baseline as instructed. */
7929 height += row->height;
7930 width = max (width, row_width);
7931 }
7932
7933 /* Add the frame's internal border to the width and height the X
7934 window should have. */
7935 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
7936 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
7937
7938 /* Move the tooltip window where the mouse pointer is. Resize and
7939 show it. */
7940 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
7941
7942 {
7943 /* Adjust Window size to take border into account. */
7944 RECT rect;
7945 rect.left = rect.top = 0;
7946 rect.right = width;
7947 rect.bottom = height;
7948 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
7949 FRAME_EXTERNAL_MENU_BAR (f));
7950
7951 /* Position and size tooltip, and put it in the topmost group.
7952 The add-on of 3 to the 5th argument is a kludge: without it,
7953 some fonts cause the last character of the tip to be truncated,
7954 for some obscure reason. */
7955 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
7956 root_x, root_y, rect.right - rect.left + 3,
7957 rect.bottom - rect.top, SWP_NOACTIVATE);
7958
7959 /* Ensure tooltip is on top of other topmost windows (eg menus). */
7960 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
7961 0, 0, 0, 0,
7962 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
7963
7964 /* Let redisplay know that we have made the frame visible already. */
7965 f->async_visible = 1;
7966
7967 ShowWindow (FRAME_W32_WINDOW (f), SW_SHOWNOACTIVATE);
7968 }
7969
7970 /* Draw into the window. */
7971 w->must_be_updated_p = 1;
7972 update_single_window (w, 1);
7973
7974 UNBLOCK_INPUT;
7975
7976 /* Restore original current buffer. */
7977 set_buffer_internal_1 (old_buffer);
7978 windows_or_buffers_changed = old_windows_or_buffers_changed;
7979
7980 start_timer:
7981 /* Let the tip disappear after timeout seconds. */
7982 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
7983 intern ("x-hide-tip"));
7984
7985 UNGCPRO;
7986 return unbind_to (count, Qnil);
7987 }
7988
7989
7990 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
7991 doc: /* Hide the current tooltip window, if there is any.
7992 Value is t if tooltip was open, nil otherwise. */)
7993 ()
7994 {
7995 int count;
7996 Lisp_Object deleted, frame, timer;
7997 struct gcpro gcpro1, gcpro2;
7998
7999 /* Return quickly if nothing to do. */
8000 if (NILP (tip_timer) && NILP (tip_frame))
8001 return Qnil;
8002
8003 frame = tip_frame;
8004 timer = tip_timer;
8005 GCPRO2 (frame, timer);
8006 tip_frame = tip_timer = deleted = Qnil;
8007
8008 count = SPECPDL_INDEX ();
8009 specbind (Qinhibit_redisplay, Qt);
8010 specbind (Qinhibit_quit, Qt);
8011
8012 if (!NILP (timer))
8013 call1 (Qcancel_timer, timer);
8014
8015 if (FRAMEP (frame))
8016 {
8017 Fdelete_frame (frame, Qnil);
8018 deleted = Qt;
8019 }
8020
8021 UNGCPRO;
8022 return unbind_to (count, deleted);
8023 }
8024
8025
8026 \f
8027 /***********************************************************************
8028 File selection dialog
8029 ***********************************************************************/
8030 extern Lisp_Object Qfile_name_history;
8031
8032 /* Callback for altering the behaviour of the Open File dialog.
8033 Makes the Filename text field contain "Current Directory" and be
8034 read-only when "Directories" is selected in the filter. This
8035 allows us to work around the fact that the standard Open File
8036 dialog does not support directories. */
8037 UINT CALLBACK
8038 file_dialog_callback (hwnd, msg, wParam, lParam)
8039 HWND hwnd;
8040 UINT msg;
8041 WPARAM wParam;
8042 LPARAM lParam;
8043 {
8044 if (msg == WM_NOTIFY)
8045 {
8046 OFNOTIFY * notify = (OFNOTIFY *)lParam;
8047 /* Detect when the Filter dropdown is changed. */
8048 if (notify->hdr.code == CDN_TYPECHANGE
8049 || notify->hdr.code == CDN_INITDONE)
8050 {
8051 HWND dialog = GetParent (hwnd);
8052 HWND edit_control = GetDlgItem (dialog, FILE_NAME_TEXT_FIELD);
8053
8054 /* Directories is in index 2. */
8055 if (notify->lpOFN->nFilterIndex == 2)
8056 {
8057 CommDlg_OpenSave_SetControlText (dialog, FILE_NAME_TEXT_FIELD,
8058 "Current Directory");
8059 EnableWindow (edit_control, FALSE);
8060 }
8061 else
8062 {
8063 /* Don't override default filename on init done. */
8064 if (notify->hdr.code == CDN_TYPECHANGE)
8065 CommDlg_OpenSave_SetControlText (dialog,
8066 FILE_NAME_TEXT_FIELD, "");
8067 EnableWindow (edit_control, TRUE);
8068 }
8069 }
8070 }
8071 return 0;
8072 }
8073
8074 /* Since we compile with _WIN32_WINNT set to 0x0400 (for NT4 compatibility)
8075 we end up with the old file dialogs. Define a big enough struct for the
8076 new dialog to trick GetOpenFileName into giving us the new dialogs on
8077 Windows 2000 and XP. */
8078 typedef struct
8079 {
8080 OPENFILENAME real_details;
8081 void * pReserved;
8082 DWORD dwReserved;
8083 DWORD FlagsEx;
8084 } NEWOPENFILENAME;
8085
8086
8087 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
8088 doc: /* Read file name, prompting with PROMPT in directory DIR.
8089 Use a file selection dialog.
8090 Select DEFAULT-FILENAME in the dialog's file selection box, if
8091 specified. Ensure that file exists if MUSTMATCH is non-nil.
8092 If ONLY-DIR-P is non-nil, the user can only select directories. */)
8093 (prompt, dir, default_filename, mustmatch, only_dir_p)
8094 Lisp_Object prompt, dir, default_filename, mustmatch, only_dir_p;
8095 {
8096 struct frame *f = SELECTED_FRAME ();
8097 Lisp_Object file = Qnil;
8098 int count = SPECPDL_INDEX ();
8099 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
8100 char filename[MAX_PATH + 1];
8101 char init_dir[MAX_PATH + 1];
8102 int default_filter_index = 1; /* 1: All Files, 2: Directories only */
8103
8104 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
8105 CHECK_STRING (prompt);
8106 CHECK_STRING (dir);
8107
8108 /* Create the dialog with PROMPT as title, using DIR as initial
8109 directory and using "*" as pattern. */
8110 dir = Fexpand_file_name (dir, Qnil);
8111 strncpy (init_dir, SDATA (ENCODE_FILE (dir)), MAX_PATH);
8112 init_dir[MAX_PATH] = '\0';
8113 unixtodos_filename (init_dir);
8114
8115 if (STRINGP (default_filename))
8116 {
8117 char *file_name_only;
8118 char *full_path_name = SDATA (ENCODE_FILE (default_filename));
8119
8120 unixtodos_filename (full_path_name);
8121
8122 file_name_only = strrchr (full_path_name, '\\');
8123 if (!file_name_only)
8124 file_name_only = full_path_name;
8125 else
8126 file_name_only++;
8127
8128 strncpy (filename, file_name_only, MAX_PATH);
8129 filename[MAX_PATH] = '\0';
8130 }
8131 else
8132 filename[0] = '\0';
8133
8134 {
8135 NEWOPENFILENAME new_file_details;
8136 BOOL file_opened = FALSE;
8137 OPENFILENAME * file_details = &new_file_details.real_details;
8138
8139 /* Prevent redisplay. */
8140 specbind (Qinhibit_redisplay, Qt);
8141 BLOCK_INPUT;
8142
8143 bzero (&new_file_details, sizeof (new_file_details));
8144 /* Apparently NT4 crashes if you give it an unexpected size.
8145 I'm not sure about Windows 9x, so play it safe. */
8146 if (w32_major_version > 4 && w32_major_version < 95)
8147 file_details->lStructSize = sizeof (NEWOPENFILENAME);
8148 else
8149 file_details->lStructSize = sizeof (OPENFILENAME);
8150
8151 file_details->hwndOwner = FRAME_W32_WINDOW (f);
8152 /* Undocumented Bug in Common File Dialog:
8153 If a filter is not specified, shell links are not resolved. */
8154 file_details->lpstrFilter = "All Files (*.*)\0*.*\0Directories\0*|*\0\0";
8155 file_details->lpstrFile = filename;
8156 file_details->nMaxFile = sizeof (filename);
8157 file_details->lpstrInitialDir = init_dir;
8158 file_details->lpstrTitle = SDATA (prompt);
8159
8160 if (! NILP (only_dir_p))
8161 default_filter_index = 2;
8162
8163 file_details->nFilterIndex = default_filter_index;
8164
8165 file_details->Flags = (OFN_HIDEREADONLY | OFN_NOCHANGEDIR
8166 | OFN_EXPLORER | OFN_ENABLEHOOK);
8167 if (!NILP (mustmatch))
8168 {
8169 /* Require that the path to the parent directory exists. */
8170 file_details->Flags |= OFN_PATHMUSTEXIST;
8171 /* If we are looking for a file, require that it exists. */
8172 if (NILP (only_dir_p))
8173 file_details->Flags |= OFN_FILEMUSTEXIST;
8174 }
8175
8176 file_details->lpfnHook = (LPOFNHOOKPROC) file_dialog_callback;
8177
8178 file_opened = GetOpenFileName (file_details);
8179
8180 UNBLOCK_INPUT;
8181
8182 if (file_opened)
8183 {
8184 dostounix_filename (filename);
8185
8186 if (file_details->nFilterIndex == 2)
8187 {
8188 /* "Directories" selected - strip dummy file name. */
8189 char * last = strrchr (filename, '/');
8190 *last = '\0';
8191 }
8192
8193 file = DECODE_FILE (build_string (filename));
8194 }
8195 /* User cancelled the dialog without making a selection. */
8196 else if (!CommDlgExtendedError ())
8197 file = Qnil;
8198 /* An error occurred, fallback on reading from the mini-buffer. */
8199 else
8200 file = Fcompleting_read (prompt, intern ("read-file-name-internal"),
8201 dir, mustmatch, dir, Qfile_name_history,
8202 default_filename, Qnil);
8203
8204 file = unbind_to (count, file);
8205 }
8206
8207 UNGCPRO;
8208
8209 /* Make "Cancel" equivalent to C-g. */
8210 if (NILP (file))
8211 Fsignal (Qquit, Qnil);
8212
8213 return unbind_to (count, file);
8214 }
8215
8216
8217 \f
8218 /***********************************************************************
8219 w32 specialized functions
8220 ***********************************************************************/
8221
8222 DEFUN ("w32-select-font", Fw32_select_font, Sw32_select_font, 0, 2, 0,
8223 doc: /* Select a font for the named FRAME using the W32 font dialog.
8224 Return an X-style font string corresponding to the selection.
8225
8226 If FRAME is omitted or nil, it defaults to the selected frame.
8227 If INCLUDE-PROPORTIONAL is non-nil, include proportional fonts
8228 in the font selection dialog. */)
8229 (frame, include_proportional)
8230 Lisp_Object frame, include_proportional;
8231 {
8232 FRAME_PTR f = check_x_frame (frame);
8233 CHOOSEFONT cf;
8234 LOGFONT lf;
8235 TEXTMETRIC tm;
8236 HDC hdc;
8237 HANDLE oldobj;
8238 char buf[100];
8239
8240 bzero (&cf, sizeof (cf));
8241 bzero (&lf, sizeof (lf));
8242
8243 cf.lStructSize = sizeof (cf);
8244 cf.hwndOwner = FRAME_W32_WINDOW (f);
8245 cf.Flags = CF_FORCEFONTEXIST | CF_SCREENFONTS | CF_NOVERTFONTS;
8246
8247 /* Unless include_proportional is non-nil, limit the selection to
8248 monospaced fonts. */
8249 if (NILP (include_proportional))
8250 cf.Flags |= CF_FIXEDPITCHONLY;
8251
8252 cf.lpLogFont = &lf;
8253
8254 /* Initialize as much of the font details as we can from the current
8255 default font. */
8256 hdc = GetDC (FRAME_W32_WINDOW (f));
8257 oldobj = SelectObject (hdc, FRAME_FONT (f)->hfont);
8258 GetTextFace (hdc, LF_FACESIZE, lf.lfFaceName);
8259 if (GetTextMetrics (hdc, &tm))
8260 {
8261 lf.lfHeight = tm.tmInternalLeading - tm.tmHeight;
8262 lf.lfWeight = tm.tmWeight;
8263 lf.lfItalic = tm.tmItalic;
8264 lf.lfUnderline = tm.tmUnderlined;
8265 lf.lfStrikeOut = tm.tmStruckOut;
8266 lf.lfCharSet = tm.tmCharSet;
8267 cf.Flags |= CF_INITTOLOGFONTSTRUCT;
8268 }
8269 SelectObject (hdc, oldobj);
8270 ReleaseDC (FRAME_W32_WINDOW (f), hdc);
8271
8272 if (!ChooseFont (&cf) || !w32_to_x_font (&lf, buf, 100, NULL))
8273 return Qnil;
8274
8275 return build_string (buf);
8276 }
8277
8278 DEFUN ("w32-send-sys-command", Fw32_send_sys_command,
8279 Sw32_send_sys_command, 1, 2, 0,
8280 doc: /* Send frame a Windows WM_SYSCOMMAND message of type COMMAND.
8281 Some useful values for COMMAND are #xf030 to maximize frame (#xf020
8282 to minimize), #xf120 to restore frame to original size, and #xf100
8283 to activate the menubar for keyboard access. #xf140 activates the
8284 screen saver if defined.
8285
8286 If optional parameter FRAME is not specified, use selected frame. */)
8287 (command, frame)
8288 Lisp_Object command, frame;
8289 {
8290 FRAME_PTR f = check_x_frame (frame);
8291
8292 CHECK_NUMBER (command);
8293
8294 PostMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, XINT (command), 0);
8295
8296 return Qnil;
8297 }
8298
8299 DEFUN ("w32-shell-execute", Fw32_shell_execute, Sw32_shell_execute, 2, 4, 0,
8300 doc: /* Get Windows to perform OPERATION on DOCUMENT.
8301 This is a wrapper around the ShellExecute system function, which
8302 invokes the application registered to handle OPERATION for DOCUMENT.
8303
8304 OPERATION is either nil or a string that names a supported operation.
8305 What operations can be used depends on the particular DOCUMENT and its
8306 handler application, but typically it is one of the following common
8307 operations:
8308
8309 \"open\" - open DOCUMENT, which could be a file, a directory, or an
8310 executable program. If it is an application, that
8311 application is launched in the current buffer's default
8312 directory. Otherwise, the application associated with
8313 DOCUMENT is launched in the buffer's default directory.
8314 \"print\" - print DOCUMENT, which must be a file
8315 \"explore\" - start the Windows Explorer on DOCUMENT
8316 \"edit\" - launch an editor and open DOCUMENT for editing; which
8317 editor is launched depends on the association for the
8318 specified DOCUMENT
8319 \"find\" - initiate search starting from DOCUMENT which must specify
8320 a directory
8321 nil - invoke the default OPERATION, or \"open\" if default is
8322 not defined or unavailable
8323
8324 DOCUMENT is typically the name of a document file or a URL, but can
8325 also be a program executable to run, or a directory to open in the
8326 Windows Explorer.
8327
8328 If DOCUMENT is a program executable, the optional third arg PARAMETERS
8329 can be a string containing command line parameters that will be passed
8330 to the program; otherwise, PARAMETERS should be nil or unspecified.
8331
8332 Optional fourth argument SHOW-FLAG can be used to control how the
8333 application will be displayed when it is invoked. If SHOW-FLAG is nil
8334 or unspecified, the application is displayed normally, otherwise it is
8335 an integer representing a ShowWindow flag:
8336
8337 0 - start hidden
8338 1 - start normally
8339 3 - start maximized
8340 6 - start minimized */)
8341 (operation, document, parameters, show_flag)
8342 Lisp_Object operation, document, parameters, show_flag;
8343 {
8344 Lisp_Object current_dir;
8345
8346 CHECK_STRING (document);
8347
8348 /* Encode filename and current directory. */
8349 current_dir = ENCODE_FILE (current_buffer->directory);
8350 document = ENCODE_FILE (document);
8351 if ((int) ShellExecute (NULL,
8352 (STRINGP (operation) ?
8353 SDATA (operation) : NULL),
8354 SDATA (document),
8355 (STRINGP (parameters) ?
8356 SDATA (parameters) : NULL),
8357 SDATA (current_dir),
8358 (INTEGERP (show_flag) ?
8359 XINT (show_flag) : SW_SHOWDEFAULT))
8360 > 32)
8361 return Qt;
8362 error ("ShellExecute failed: %s", w32_strerror (0));
8363 }
8364
8365 /* Lookup virtual keycode from string representing the name of a
8366 non-ascii keystroke into the corresponding virtual key, using
8367 lispy_function_keys. */
8368 static int
8369 lookup_vk_code (char *key)
8370 {
8371 int i;
8372
8373 for (i = 0; i < 256; i++)
8374 if (lispy_function_keys[i]
8375 && strcmp (lispy_function_keys[i], key) == 0)
8376 return i;
8377
8378 return -1;
8379 }
8380
8381 /* Convert a one-element vector style key sequence to a hot key
8382 definition. */
8383 static Lisp_Object
8384 w32_parse_hot_key (key)
8385 Lisp_Object key;
8386 {
8387 /* Copied from Fdefine_key and store_in_keymap. */
8388 register Lisp_Object c;
8389 int vk_code;
8390 int lisp_modifiers;
8391 int w32_modifiers;
8392 struct gcpro gcpro1;
8393
8394 CHECK_VECTOR (key);
8395
8396 if (XFASTINT (Flength (key)) != 1)
8397 return Qnil;
8398
8399 GCPRO1 (key);
8400
8401 c = Faref (key, make_number (0));
8402
8403 if (CONSP (c) && lucid_event_type_list_p (c))
8404 c = Fevent_convert_list (c);
8405
8406 UNGCPRO;
8407
8408 if (! INTEGERP (c) && ! SYMBOLP (c))
8409 error ("Key definition is invalid");
8410
8411 /* Work out the base key and the modifiers. */
8412 if (SYMBOLP (c))
8413 {
8414 c = parse_modifiers (c);
8415 lisp_modifiers = XINT (Fcar (Fcdr (c)));
8416 c = Fcar (c);
8417 if (!SYMBOLP (c))
8418 abort ();
8419 vk_code = lookup_vk_code (SDATA (SYMBOL_NAME (c)));
8420 }
8421 else if (INTEGERP (c))
8422 {
8423 lisp_modifiers = XINT (c) & ~CHARACTERBITS;
8424 /* Many ascii characters are their own virtual key code. */
8425 vk_code = XINT (c) & CHARACTERBITS;
8426 }
8427
8428 if (vk_code < 0 || vk_code > 255)
8429 return Qnil;
8430
8431 if ((lisp_modifiers & meta_modifier) != 0
8432 && !NILP (Vw32_alt_is_meta))
8433 lisp_modifiers |= alt_modifier;
8434
8435 /* Supply defs missing from mingw32. */
8436 #ifndef MOD_ALT
8437 #define MOD_ALT 0x0001
8438 #define MOD_CONTROL 0x0002
8439 #define MOD_SHIFT 0x0004
8440 #define MOD_WIN 0x0008
8441 #endif
8442
8443 /* Convert lisp modifiers to Windows hot-key form. */
8444 w32_modifiers = (lisp_modifiers & hyper_modifier) ? MOD_WIN : 0;
8445 w32_modifiers |= (lisp_modifiers & alt_modifier) ? MOD_ALT : 0;
8446 w32_modifiers |= (lisp_modifiers & ctrl_modifier) ? MOD_CONTROL : 0;
8447 w32_modifiers |= (lisp_modifiers & shift_modifier) ? MOD_SHIFT : 0;
8448
8449 return HOTKEY (vk_code, w32_modifiers);
8450 }
8451
8452 DEFUN ("w32-register-hot-key", Fw32_register_hot_key,
8453 Sw32_register_hot_key, 1, 1, 0,
8454 doc: /* Register KEY as a hot-key combination.
8455 Certain key combinations like Alt-Tab are reserved for system use on
8456 Windows, and therefore are normally intercepted by the system. However,
8457 most of these key combinations can be received by registering them as
8458 hot-keys, overriding their special meaning.
8459
8460 KEY must be a one element key definition in vector form that would be
8461 acceptable to `define-key' (e.g. [A-tab] for Alt-Tab). The meta
8462 modifier is interpreted as Alt if `w32-alt-is-meta' is t, and hyper
8463 is always interpreted as the Windows modifier keys.
8464
8465 The return value is the hotkey-id if registered, otherwise nil. */)
8466 (key)
8467 Lisp_Object key;
8468 {
8469 key = w32_parse_hot_key (key);
8470
8471 if (!NILP (key) && NILP (Fmemq (key, w32_grabbed_keys)))
8472 {
8473 /* Reuse an empty slot if possible. */
8474 Lisp_Object item = Fmemq (Qnil, w32_grabbed_keys);
8475
8476 /* Safe to add new key to list, even if we have focus. */
8477 if (NILP (item))
8478 w32_grabbed_keys = Fcons (key, w32_grabbed_keys);
8479 else
8480 XSETCAR (item, key);
8481
8482 /* Notify input thread about new hot-key definition, so that it
8483 takes effect without needing to switch focus. */
8484 #ifdef USE_LISP_UNION_TYPE
8485 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
8486 (WPARAM) key.i, 0);
8487 #else
8488 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
8489 (WPARAM) key, 0);
8490 #endif
8491 }
8492
8493 return key;
8494 }
8495
8496 DEFUN ("w32-unregister-hot-key", Fw32_unregister_hot_key,
8497 Sw32_unregister_hot_key, 1, 1, 0,
8498 doc: /* Unregister KEY as a hot-key combination. */)
8499 (key)
8500 Lisp_Object key;
8501 {
8502 Lisp_Object item;
8503
8504 if (!INTEGERP (key))
8505 key = w32_parse_hot_key (key);
8506
8507 item = Fmemq (key, w32_grabbed_keys);
8508
8509 if (!NILP (item))
8510 {
8511 /* Notify input thread about hot-key definition being removed, so
8512 that it takes effect without needing focus switch. */
8513 #ifdef USE_LISP_UNION_TYPE
8514 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
8515 (WPARAM) XINT (XCAR (item)), (LPARAM) item.i))
8516 #else
8517 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
8518 (WPARAM) XINT (XCAR (item)), (LPARAM) item))
8519 #endif
8520 {
8521 MSG msg;
8522 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
8523 }
8524 return Qt;
8525 }
8526 return Qnil;
8527 }
8528
8529 DEFUN ("w32-registered-hot-keys", Fw32_registered_hot_keys,
8530 Sw32_registered_hot_keys, 0, 0, 0,
8531 doc: /* Return list of registered hot-key IDs. */)
8532 ()
8533 {
8534 return Fdelq (Qnil, Fcopy_sequence (w32_grabbed_keys));
8535 }
8536
8537 DEFUN ("w32-reconstruct-hot-key", Fw32_reconstruct_hot_key,
8538 Sw32_reconstruct_hot_key, 1, 1, 0,
8539 doc: /* Convert hot-key ID to a lisp key combination.
8540 usage: (w32-reconstruct-hot-key ID) */)
8541 (hotkeyid)
8542 Lisp_Object hotkeyid;
8543 {
8544 int vk_code, w32_modifiers;
8545 Lisp_Object key;
8546
8547 CHECK_NUMBER (hotkeyid);
8548
8549 vk_code = HOTKEY_VK_CODE (hotkeyid);
8550 w32_modifiers = HOTKEY_MODIFIERS (hotkeyid);
8551
8552 if (vk_code < 256 && lispy_function_keys[vk_code])
8553 key = intern (lispy_function_keys[vk_code]);
8554 else
8555 key = make_number (vk_code);
8556
8557 key = Fcons (key, Qnil);
8558 if (w32_modifiers & MOD_SHIFT)
8559 key = Fcons (Qshift, key);
8560 if (w32_modifiers & MOD_CONTROL)
8561 key = Fcons (Qctrl, key);
8562 if (w32_modifiers & MOD_ALT)
8563 key = Fcons (NILP (Vw32_alt_is_meta) ? Qalt : Qmeta, key);
8564 if (w32_modifiers & MOD_WIN)
8565 key = Fcons (Qhyper, key);
8566
8567 return key;
8568 }
8569
8570 DEFUN ("w32-toggle-lock-key", Fw32_toggle_lock_key,
8571 Sw32_toggle_lock_key, 1, 2, 0,
8572 doc: /* Toggle the state of the lock key KEY.
8573 KEY can be `capslock', `kp-numlock', or `scroll'.
8574 If the optional parameter NEW-STATE is a number, then the state of KEY
8575 is set to off if the low bit of NEW-STATE is zero, otherwise on. */)
8576 (key, new_state)
8577 Lisp_Object key, new_state;
8578 {
8579 int vk_code;
8580
8581 if (EQ (key, intern ("capslock")))
8582 vk_code = VK_CAPITAL;
8583 else if (EQ (key, intern ("kp-numlock")))
8584 vk_code = VK_NUMLOCK;
8585 else if (EQ (key, intern ("scroll")))
8586 vk_code = VK_SCROLL;
8587 else
8588 return Qnil;
8589
8590 if (!dwWindowsThreadId)
8591 return make_number (w32_console_toggle_lock_key (vk_code, new_state));
8592
8593 #ifdef USE_LISP_UNION_TYPE
8594 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
8595 (WPARAM) vk_code, (LPARAM) new_state.i))
8596 #else
8597 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
8598 (WPARAM) vk_code, (LPARAM) new_state))
8599 #endif
8600 {
8601 MSG msg;
8602 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
8603 return make_number (msg.wParam);
8604 }
8605 return Qnil;
8606 }
8607
8608 DEFUN ("w32-window-exists-p", Fw32_window_exists_p, Sw32_window_exists_p,
8609 2, 2, 0,
8610 doc: /* Return non-nil if a window exists with the specified CLASS and NAME.
8611
8612 This is a direct interface to the Windows API FindWindow function. */)
8613 (class, name)
8614 Lisp_Object class, name;
8615 {
8616 HWND hnd;
8617
8618 if (!NILP (class))
8619 CHECK_STRING (class);
8620 if (!NILP (name))
8621 CHECK_STRING (name);
8622
8623 hnd = FindWindow (STRINGP (class) ? ((LPCTSTR) SDATA (class)) : NULL,
8624 STRINGP (name) ? ((LPCTSTR) SDATA (name)) : NULL);
8625 if (!hnd)
8626 return Qnil;
8627 return Qt;
8628 }
8629
8630
8631 \f
8632 DEFUN ("file-system-info", Ffile_system_info, Sfile_system_info, 1, 1, 0,
8633 doc: /* Return storage information about the file system FILENAME is on.
8634 Value is a list of floats (TOTAL FREE AVAIL), where TOTAL is the total
8635 storage of the file system, FREE is the free storage, and AVAIL is the
8636 storage available to a non-superuser. All 3 numbers are in bytes.
8637 If the underlying system call fails, value is nil. */)
8638 (filename)
8639 Lisp_Object filename;
8640 {
8641 Lisp_Object encoded, value;
8642
8643 CHECK_STRING (filename);
8644 filename = Fexpand_file_name (filename, Qnil);
8645 encoded = ENCODE_FILE (filename);
8646
8647 value = Qnil;
8648
8649 /* Determining the required information on Windows turns out, sadly,
8650 to be more involved than one would hope. The original Win32 api
8651 call for this will return bogus information on some systems, but we
8652 must dynamically probe for the replacement api, since that was
8653 added rather late on. */
8654 {
8655 HMODULE hKernel = GetModuleHandle ("kernel32");
8656 BOOL (*pfn_GetDiskFreeSpaceEx)
8657 (char *, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER)
8658 = (void *) GetProcAddress (hKernel, "GetDiskFreeSpaceEx");
8659
8660 /* On Windows, we may need to specify the root directory of the
8661 volume holding FILENAME. */
8662 char rootname[MAX_PATH];
8663 char *name = SDATA (encoded);
8664
8665 /* find the root name of the volume if given */
8666 if (isalpha (name[0]) && name[1] == ':')
8667 {
8668 rootname[0] = name[0];
8669 rootname[1] = name[1];
8670 rootname[2] = '\\';
8671 rootname[3] = 0;
8672 }
8673 else if (IS_DIRECTORY_SEP (name[0]) && IS_DIRECTORY_SEP (name[1]))
8674 {
8675 char *str = rootname;
8676 int slashes = 4;
8677 do
8678 {
8679 if (IS_DIRECTORY_SEP (*name) && --slashes == 0)
8680 break;
8681 *str++ = *name++;
8682 }
8683 while ( *name );
8684
8685 *str++ = '\\';
8686 *str = 0;
8687 }
8688
8689 if (pfn_GetDiskFreeSpaceEx)
8690 {
8691 /* Unsigned large integers cannot be cast to double, so
8692 use signed ones instead. */
8693 LARGE_INTEGER availbytes;
8694 LARGE_INTEGER freebytes;
8695 LARGE_INTEGER totalbytes;
8696
8697 if (pfn_GetDiskFreeSpaceEx (rootname,
8698 (ULARGE_INTEGER *)&availbytes,
8699 (ULARGE_INTEGER *)&totalbytes,
8700 (ULARGE_INTEGER *)&freebytes))
8701 value = list3 (make_float ((double) totalbytes.QuadPart),
8702 make_float ((double) freebytes.QuadPart),
8703 make_float ((double) availbytes.QuadPart));
8704 }
8705 else
8706 {
8707 DWORD sectors_per_cluster;
8708 DWORD bytes_per_sector;
8709 DWORD free_clusters;
8710 DWORD total_clusters;
8711
8712 if (GetDiskFreeSpace (rootname,
8713 &sectors_per_cluster,
8714 &bytes_per_sector,
8715 &free_clusters,
8716 &total_clusters))
8717 value = list3 (make_float ((double) total_clusters
8718 * sectors_per_cluster * bytes_per_sector),
8719 make_float ((double) free_clusters
8720 * sectors_per_cluster * bytes_per_sector),
8721 make_float ((double) free_clusters
8722 * sectors_per_cluster * bytes_per_sector));
8723 }
8724 }
8725
8726 return value;
8727 }
8728 \f
8729 DEFUN ("default-printer-name", Fdefault_printer_name, Sdefault_printer_name,
8730 0, 0, 0, doc: /* Return the name of Windows default printer device. */)
8731 ()
8732 {
8733 static char pname_buf[256];
8734 int err;
8735 HANDLE hPrn;
8736 PRINTER_INFO_2 *ppi2 = NULL;
8737 DWORD dwNeeded = 0, dwReturned = 0;
8738
8739 /* Retrieve the default string from Win.ini (the registry).
8740 * String will be in form "printername,drivername,portname".
8741 * This is the most portable way to get the default printer. */
8742 if (GetProfileString ("windows", "device", ",,", pname_buf, sizeof (pname_buf)) <= 0)
8743 return Qnil;
8744 /* printername precedes first "," character */
8745 strtok (pname_buf, ",");
8746 /* We want to know more than the printer name */
8747 if (!OpenPrinter (pname_buf, &hPrn, NULL))
8748 return Qnil;
8749 GetPrinter (hPrn, 2, NULL, 0, &dwNeeded);
8750 if (dwNeeded == 0)
8751 {
8752 ClosePrinter (hPrn);
8753 return Qnil;
8754 }
8755 /* Allocate memory for the PRINTER_INFO_2 struct */
8756 ppi2 = (PRINTER_INFO_2 *) xmalloc (dwNeeded);
8757 if (!ppi2)
8758 {
8759 ClosePrinter (hPrn);
8760 return Qnil;
8761 }
8762 /* Call GetPrinter again with big enouth memory block */
8763 err = GetPrinter (hPrn, 2, (LPBYTE)ppi2, dwNeeded, &dwReturned);
8764 ClosePrinter (hPrn);
8765 if (!err)
8766 {
8767 xfree (ppi2);
8768 return Qnil;
8769 }
8770
8771 if (ppi2)
8772 {
8773 if (ppi2->Attributes & PRINTER_ATTRIBUTE_SHARED && ppi2->pServerName)
8774 {
8775 /* a remote printer */
8776 if (*ppi2->pServerName == '\\')
8777 _snprintf (pname_buf, sizeof (pname_buf), "%s\\%s", ppi2->pServerName,
8778 ppi2->pShareName);
8779 else
8780 _snprintf (pname_buf, sizeof (pname_buf), "\\\\%s\\%s", ppi2->pServerName,
8781 ppi2->pShareName);
8782 pname_buf[sizeof (pname_buf) - 1] = '\0';
8783 }
8784 else
8785 {
8786 /* a local printer */
8787 strncpy (pname_buf, ppi2->pPortName, sizeof (pname_buf));
8788 pname_buf[sizeof (pname_buf) - 1] = '\0';
8789 /* `pPortName' can include several ports, delimited by ','.
8790 * we only use the first one. */
8791 strtok (pname_buf, ",");
8792 }
8793 xfree (ppi2);
8794 }
8795
8796 return build_string (pname_buf);
8797 }
8798 \f
8799 /***********************************************************************
8800 Initialization
8801 ***********************************************************************/
8802
8803 /* Keep this list in the same order as frame_parms in frame.c.
8804 Use 0 for unsupported frame parameters. */
8805
8806 frame_parm_handler w32_frame_parm_handlers[] =
8807 {
8808 x_set_autoraise,
8809 x_set_autolower,
8810 x_set_background_color,
8811 x_set_border_color,
8812 x_set_border_width,
8813 x_set_cursor_color,
8814 x_set_cursor_type,
8815 x_set_font,
8816 x_set_foreground_color,
8817 x_set_icon_name,
8818 x_set_icon_type,
8819 x_set_internal_border_width,
8820 x_set_menu_bar_lines,
8821 x_set_mouse_color,
8822 x_explicitly_set_name,
8823 x_set_scroll_bar_width,
8824 x_set_title,
8825 x_set_unsplittable,
8826 x_set_vertical_scroll_bars,
8827 x_set_visibility,
8828 x_set_tool_bar_lines,
8829 0, /* x_set_scroll_bar_foreground, */
8830 0, /* x_set_scroll_bar_background, */
8831 x_set_screen_gamma,
8832 x_set_line_spacing,
8833 x_set_fringe_width,
8834 x_set_fringe_width,
8835 0, /* x_set_wait_for_wm, */
8836 x_set_fullscreen,
8837 #ifdef USE_FONT_BACKEND
8838 x_set_font_backend
8839 #endif
8840 };
8841
8842 void
8843 syms_of_w32fns ()
8844 {
8845 globals_of_w32fns ();
8846 /* This is zero if not using MS-Windows. */
8847 w32_in_use = 0;
8848 track_mouse_window = NULL;
8849
8850 w32_visible_system_caret_hwnd = NULL;
8851
8852 DEFSYM (Qnone, "none");
8853 DEFSYM (Qsuppress_icon, "suppress-icon");
8854 DEFSYM (Qundefined_color, "undefined-color");
8855 DEFSYM (Qcancel_timer, "cancel-timer");
8856 DEFSYM (Qhyper, "hyper");
8857 DEFSYM (Qsuper, "super");
8858 DEFSYM (Qmeta, "meta");
8859 DEFSYM (Qalt, "alt");
8860 DEFSYM (Qctrl, "ctrl");
8861 DEFSYM (Qcontrol, "control");
8862 DEFSYM (Qshift, "shift");
8863 /* This is the end of symbol initialization. */
8864
8865 /* Text property `display' should be nonsticky by default. */
8866 Vtext_property_default_nonsticky
8867 = Fcons (Fcons (Qdisplay, Qt), Vtext_property_default_nonsticky);
8868
8869
8870 Fput (Qundefined_color, Qerror_conditions,
8871 Fcons (Qundefined_color, Fcons (Qerror, Qnil)));
8872 Fput (Qundefined_color, Qerror_message,
8873 build_string ("Undefined color"));
8874
8875 staticpro (&w32_grabbed_keys);
8876 w32_grabbed_keys = Qnil;
8877
8878 DEFVAR_LISP ("w32-color-map", &Vw32_color_map,
8879 doc: /* An array of color name mappings for Windows. */);
8880 Vw32_color_map = Qnil;
8881
8882 DEFVAR_LISP ("w32-pass-alt-to-system", &Vw32_pass_alt_to_system,
8883 doc: /* Non-nil if Alt key presses are passed on to Windows.
8884 When non-nil, for example, Alt pressed and released and then space will
8885 open the System menu. When nil, Emacs processes the Alt key events, and
8886 then silently swallows them. */);
8887 Vw32_pass_alt_to_system = Qnil;
8888
8889 DEFVAR_LISP ("w32-alt-is-meta", &Vw32_alt_is_meta,
8890 doc: /* Non-nil if the Alt key is to be considered the same as the META key.
8891 When nil, Emacs will translate the Alt key to the ALT modifier, not to META. */);
8892 Vw32_alt_is_meta = Qt;
8893
8894 DEFVAR_INT ("w32-quit-key", &w32_quit_key,
8895 doc: /* If non-zero, the virtual key code for an alternative quit key. */);
8896 w32_quit_key = 0;
8897
8898 DEFVAR_LISP ("w32-pass-lwindow-to-system",
8899 &Vw32_pass_lwindow_to_system,
8900 doc: /* If non-nil, the left \"Windows\" key is passed on to Windows.
8901
8902 When non-nil, the Start menu is opened by tapping the key.
8903 If you set this to nil, the left \"Windows\" key is processed by Emacs
8904 according to the value of `w32-lwindow-modifier', which see.
8905
8906 Note that some combinations of the left \"Windows\" key with other keys are
8907 caught by Windows at low level, and so binding them in Emacs will have no
8908 effect. For example, <lwindow>-r always pops up the Windows Run dialog,
8909 <lwindow>-<Pause> pops up the "System Properties" dialog, etc. However, see
8910 the doc string of `w32-phantom-key-code'. */);
8911 Vw32_pass_lwindow_to_system = Qt;
8912
8913 DEFVAR_LISP ("w32-pass-rwindow-to-system",
8914 &Vw32_pass_rwindow_to_system,
8915 doc: /* If non-nil, the right \"Windows\" key is passed on to Windows.
8916
8917 When non-nil, the Start menu is opened by tapping the key.
8918 If you set this to nil, the right \"Windows\" key is processed by Emacs
8919 according to the value of `w32-rwindow-modifier', which see.
8920
8921 Note that some combinations of the right \"Windows\" key with other keys are
8922 caught by Windows at low level, and so binding them in Emacs will have no
8923 effect. For example, <rwindow>-r always pops up the Windows Run dialog,
8924 <rwindow>-<Pause> pops up the "System Properties" dialog, etc. However, see
8925 the doc string of `w32-phantom-key-code'. */);
8926 Vw32_pass_rwindow_to_system = Qt;
8927
8928 DEFVAR_LISP ("w32-phantom-key-code",
8929 &Vw32_phantom_key_code,
8930 doc: /* Virtual key code used to generate \"phantom\" key presses.
8931 Value is a number between 0 and 255.
8932
8933 Phantom key presses are generated in order to stop the system from
8934 acting on \"Windows\" key events when `w32-pass-lwindow-to-system' or
8935 `w32-pass-rwindow-to-system' is nil. */);
8936 /* Although 255 is technically not a valid key code, it works and
8937 means that this hack won't interfere with any real key code. */
8938 XSETINT (Vw32_phantom_key_code, 255);
8939
8940 DEFVAR_LISP ("w32-enable-num-lock",
8941 &Vw32_enable_num_lock,
8942 doc: /* If non-nil, the Num Lock key acts normally.
8943 Set to nil to handle Num Lock as the `kp-numlock' key. */);
8944 Vw32_enable_num_lock = Qt;
8945
8946 DEFVAR_LISP ("w32-enable-caps-lock",
8947 &Vw32_enable_caps_lock,
8948 doc: /* If non-nil, the Caps Lock key acts normally.
8949 Set to nil to handle Caps Lock as the `capslock' key. */);
8950 Vw32_enable_caps_lock = Qt;
8951
8952 DEFVAR_LISP ("w32-scroll-lock-modifier",
8953 &Vw32_scroll_lock_modifier,
8954 doc: /* Modifier to use for the Scroll Lock ON state.
8955 The value can be hyper, super, meta, alt, control or shift for the
8956 respective modifier, or nil to handle Scroll Lock as the `scroll' key.
8957 Any other value will cause the Scroll Lock key to be ignored. */);
8958 Vw32_scroll_lock_modifier = Qt;
8959
8960 DEFVAR_LISP ("w32-lwindow-modifier",
8961 &Vw32_lwindow_modifier,
8962 doc: /* Modifier to use for the left \"Windows\" key.
8963 The value can be hyper, super, meta, alt, control or shift for the
8964 respective modifier, or nil to appear as the `lwindow' key.
8965 Any other value will cause the key to be ignored. */);
8966 Vw32_lwindow_modifier = Qnil;
8967
8968 DEFVAR_LISP ("w32-rwindow-modifier",
8969 &Vw32_rwindow_modifier,
8970 doc: /* Modifier to use for the right \"Windows\" key.
8971 The value can be hyper, super, meta, alt, control or shift for the
8972 respective modifier, or nil to appear as the `rwindow' key.
8973 Any other value will cause the key to be ignored. */);
8974 Vw32_rwindow_modifier = Qnil;
8975
8976 DEFVAR_LISP ("w32-apps-modifier",
8977 &Vw32_apps_modifier,
8978 doc: /* Modifier to use for the \"Apps\" key.
8979 The value can be hyper, super, meta, alt, control or shift for the
8980 respective modifier, or nil to appear as the `apps' key.
8981 Any other value will cause the key to be ignored. */);
8982 Vw32_apps_modifier = Qnil;
8983
8984 DEFVAR_BOOL ("w32-enable-synthesized-fonts", &w32_enable_synthesized_fonts,
8985 doc: /* Non-nil enables selection of artificially italicized and bold fonts. */);
8986 w32_enable_synthesized_fonts = 0;
8987
8988 DEFVAR_LISP ("w32-enable-palette", &Vw32_enable_palette,
8989 doc: /* Non-nil enables Windows palette management to map colors exactly. */);
8990 Vw32_enable_palette = Qt;
8991
8992 DEFVAR_INT ("w32-mouse-button-tolerance",
8993 &w32_mouse_button_tolerance,
8994 doc: /* Analogue of double click interval for faking middle mouse events.
8995 The value is the minimum time in milliseconds that must elapse between
8996 left and right button down events before they are considered distinct events.
8997 If both mouse buttons are depressed within this interval, a middle mouse
8998 button down event is generated instead. */);
8999 w32_mouse_button_tolerance = GetDoubleClickTime () / 2;
9000
9001 DEFVAR_INT ("w32-mouse-move-interval",
9002 &w32_mouse_move_interval,
9003 doc: /* Minimum interval between mouse move events.
9004 The value is the minimum time in milliseconds that must elapse between
9005 successive mouse move (or scroll bar drag) events before they are
9006 reported as lisp events. */);
9007 w32_mouse_move_interval = 0;
9008
9009 DEFVAR_BOOL ("w32-pass-extra-mouse-buttons-to-system",
9010 &w32_pass_extra_mouse_buttons_to_system,
9011 doc: /* If non-nil, the fourth and fifth mouse buttons are passed to Windows.
9012 Recent versions of Windows support mice with up to five buttons.
9013 Since most applications don't support these extra buttons, most mouse
9014 drivers will allow you to map them to functions at the system level.
9015 If this variable is non-nil, Emacs will pass them on, allowing the
9016 system to handle them. */);
9017 w32_pass_extra_mouse_buttons_to_system = 0;
9018
9019 DEFVAR_BOOL ("w32-pass-multimedia-buttons-to-system",
9020 &w32_pass_multimedia_buttons_to_system,
9021 doc: /* If non-nil, media buttons are passed to Windows.
9022 Some modern keyboards contain buttons for controlling media players, web
9023 browsers and other applications. Generally these buttons are handled on a
9024 system wide basis, but by setting this to nil they are made available
9025 to Emacs for binding. Depending on your keyboard, additional keys that
9026 may be available are:
9027
9028 browser-back, browser-forward, browser-refresh, browser-stop,
9029 browser-search, browser-favorites, browser-home,
9030 mail, mail-reply, mail-forward, mail-send,
9031 app-1, app-2,
9032 help, find, new, open, close, save, print, undo, redo, copy, cut, paste,
9033 spell-check, correction-list, toggle-dictate-command,
9034 media-next, media-previous, media-stop, media-play-pause, media-select,
9035 media-play, media-pause, media-record, media-fast-forward, media-rewind,
9036 media-channel-up, media-channel-down,
9037 volume-mute, volume-up, volume-down,
9038 mic-volume-mute, mic-volume-down, mic-volume-up, mic-toggle,
9039 bass-down, bass-boost, bass-up, treble-down, treble-up */);
9040 w32_pass_multimedia_buttons_to_system = 1;
9041
9042 DEFVAR_LISP ("x-pointer-shape", &Vx_pointer_shape,
9043 doc: /* The shape of the pointer when over text.
9044 Changing the value does not affect existing frames
9045 unless you set the mouse color. */);
9046 Vx_pointer_shape = Qnil;
9047
9048 Vx_nontext_pointer_shape = Qnil;
9049
9050 Vx_mode_pointer_shape = Qnil;
9051
9052 DEFVAR_LISP ("x-hourglass-pointer-shape", &Vx_hourglass_pointer_shape,
9053 doc: /* The shape of the pointer when Emacs is busy.
9054 This variable takes effect when you create a new frame
9055 or when you set the mouse color. */);
9056 Vx_hourglass_pointer_shape = Qnil;
9057
9058 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
9059 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
9060 display_hourglass_p = 1;
9061
9062 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
9063 doc: /* *Seconds to wait before displaying an hourglass pointer.
9064 Value must be an integer or float. */);
9065 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
9066
9067 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
9068 &Vx_sensitive_text_pointer_shape,
9069 doc: /* The shape of the pointer when over mouse-sensitive text.
9070 This variable takes effect when you create a new frame
9071 or when you set the mouse color. */);
9072 Vx_sensitive_text_pointer_shape = Qnil;
9073
9074 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
9075 &Vx_window_horizontal_drag_shape,
9076 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
9077 This variable takes effect when you create a new frame
9078 or when you set the mouse color. */);
9079 Vx_window_horizontal_drag_shape = Qnil;
9080
9081 DEFVAR_LISP ("x-cursor-fore-pixel", &Vx_cursor_fore_pixel,
9082 doc: /* A string indicating the foreground color of the cursor box. */);
9083 Vx_cursor_fore_pixel = Qnil;
9084
9085 DEFVAR_LISP ("x-max-tooltip-size", &Vx_max_tooltip_size,
9086 doc: /* Maximum size for tooltips.
9087 Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
9088 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
9089
9090 DEFVAR_LISP ("x-no-window-manager", &Vx_no_window_manager,
9091 doc: /* Non-nil if no window manager is in use.
9092 Emacs doesn't try to figure this out; this is always nil
9093 unless you set it to something else. */);
9094 /* We don't have any way to find this out, so set it to nil
9095 and maybe the user would like to set it to t. */
9096 Vx_no_window_manager = Qnil;
9097
9098 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
9099 &Vx_pixel_size_width_font_regexp,
9100 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
9101
9102 Since Emacs gets width of a font matching with this regexp from
9103 PIXEL_SIZE field of the name, font finding mechanism gets faster for
9104 such a font. This is especially effective for such large fonts as
9105 Chinese, Japanese, and Korean. */);
9106 Vx_pixel_size_width_font_regexp = Qnil;
9107
9108 DEFVAR_LISP ("w32-bdf-filename-alist",
9109 &Vw32_bdf_filename_alist,
9110 doc: /* List of bdf fonts and their corresponding filenames. */);
9111 Vw32_bdf_filename_alist = Qnil;
9112
9113 DEFVAR_BOOL ("w32-strict-fontnames",
9114 &w32_strict_fontnames,
9115 doc: /* Non-nil means only use fonts that are exact matches for those requested.
9116 Default is nil, which allows old fontnames that are not XLFD compliant,
9117 and allows third-party CJK display to work by specifying false charset
9118 fields to trick Emacs into translating to Big5, SJIS etc.
9119 Setting this to t will prevent wrong fonts being selected when
9120 fontsets are automatically created. */);
9121 w32_strict_fontnames = 0;
9122
9123 DEFVAR_BOOL ("w32-strict-painting",
9124 &w32_strict_painting,
9125 doc: /* Non-nil means use strict rules for repainting frames.
9126 Set this to nil to get the old behavior for repainting; this should
9127 only be necessary if the default setting causes problems. */);
9128 w32_strict_painting = 1;
9129
9130 DEFVAR_LISP ("w32-charset-info-alist",
9131 &Vw32_charset_info_alist,
9132 doc: /* Alist linking Emacs character sets to Windows fonts and codepages.
9133 Each entry should be of the form:
9134
9135 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE))
9136
9137 where CHARSET_NAME is a string used in font names to identify the charset,
9138 WINDOWS_CHARSET is a symbol that can be one of:
9139 w32-charset-ansi, w32-charset-default, w32-charset-symbol,
9140 w32-charset-shiftjis, w32-charset-hangeul, w32-charset-gb2312,
9141 w32-charset-chinesebig5,
9142 w32-charset-johab, w32-charset-hebrew,
9143 w32-charset-arabic, w32-charset-greek, w32-charset-turkish,
9144 w32-charset-vietnamese, w32-charset-thai, w32-charset-easteurope,
9145 w32-charset-russian, w32-charset-mac, w32-charset-baltic,
9146 w32-charset-unicode,
9147 or w32-charset-oem.
9148 CODEPAGE should be an integer specifying the codepage that should be used
9149 to display the character set, t to do no translation and output as Unicode,
9150 or nil to do no translation and output as 8 bit (or multibyte on far-east
9151 versions of Windows) characters. */);
9152 Vw32_charset_info_alist = Qnil;
9153
9154 DEFSYM (Qw32_charset_ansi, "w32-charset-ansi");
9155 DEFSYM (Qw32_charset_symbol, "w32-charset-symbol");
9156 DEFSYM (Qw32_charset_default, "w32-charset-default");
9157 DEFSYM (Qw32_charset_shiftjis, "w32-charset-shiftjis");
9158 DEFSYM (Qw32_charset_hangeul, "w32-charset-hangeul");
9159 DEFSYM (Qw32_charset_chinesebig5, "w32-charset-chinesebig5");
9160 DEFSYM (Qw32_charset_gb2312, "w32-charset-gb2312");
9161 DEFSYM (Qw32_charset_oem, "w32-charset-oem");
9162
9163 #ifdef JOHAB_CHARSET
9164 {
9165 static int w32_extra_charsets_defined = 1;
9166 DEFVAR_BOOL ("w32-extra-charsets-defined", &w32_extra_charsets_defined,
9167 doc: /* Internal variable. */);
9168
9169 DEFSYM (Qw32_charset_johab, "w32-charset-johab");
9170 DEFSYM (Qw32_charset_easteurope, "w32-charset-easteurope");
9171 DEFSYM (Qw32_charset_turkish, "w32-charset-turkish");
9172 DEFSYM (Qw32_charset_baltic, "w32-charset-baltic");
9173 DEFSYM (Qw32_charset_russian, "w32-charset-russian");
9174 DEFSYM (Qw32_charset_arabic, "w32-charset-arabic");
9175 DEFSYM (Qw32_charset_greek, "w32-charset-greek");
9176 DEFSYM (Qw32_charset_hebrew, "w32-charset-hebrew");
9177 DEFSYM (Qw32_charset_vietnamese, "w32-charset-vietnamese");
9178 DEFSYM (Qw32_charset_thai, "w32-charset-thai");
9179 DEFSYM (Qw32_charset_mac, "w32-charset-mac");
9180 }
9181 #endif
9182
9183 #ifdef UNICODE_CHARSET
9184 {
9185 static int w32_unicode_charset_defined = 1;
9186 DEFVAR_BOOL ("w32-unicode-charset-defined",
9187 &w32_unicode_charset_defined,
9188 doc: /* Internal variable. */);
9189 DEFSYM (Qw32_charset_unicode, "w32-charset-unicode");
9190 }
9191 #endif
9192
9193 #if 0 /* TODO: Port to W32 */
9194 defsubr (&Sx_change_window_property);
9195 defsubr (&Sx_delete_window_property);
9196 defsubr (&Sx_window_property);
9197 #endif
9198 defsubr (&Sxw_display_color_p);
9199 defsubr (&Sx_display_grayscale_p);
9200 defsubr (&Sxw_color_defined_p);
9201 defsubr (&Sxw_color_values);
9202 defsubr (&Sx_server_max_request_size);
9203 defsubr (&Sx_server_vendor);
9204 defsubr (&Sx_server_version);
9205 defsubr (&Sx_display_pixel_width);
9206 defsubr (&Sx_display_pixel_height);
9207 defsubr (&Sx_display_mm_width);
9208 defsubr (&Sx_display_mm_height);
9209 defsubr (&Sx_display_screens);
9210 defsubr (&Sx_display_planes);
9211 defsubr (&Sx_display_color_cells);
9212 defsubr (&Sx_display_visual_class);
9213 defsubr (&Sx_display_backing_store);
9214 defsubr (&Sx_display_save_under);
9215 defsubr (&Sx_create_frame);
9216 defsubr (&Sx_open_connection);
9217 defsubr (&Sx_close_connection);
9218 defsubr (&Sx_display_list);
9219 defsubr (&Sx_synchronize);
9220 defsubr (&Sx_focus_frame);
9221
9222 /* W32 specific functions */
9223
9224 defsubr (&Sw32_select_font);
9225 defsubr (&Sw32_define_rgb_color);
9226 defsubr (&Sw32_default_color_map);
9227 defsubr (&Sw32_load_color_file);
9228 defsubr (&Sw32_send_sys_command);
9229 defsubr (&Sw32_shell_execute);
9230 defsubr (&Sw32_register_hot_key);
9231 defsubr (&Sw32_unregister_hot_key);
9232 defsubr (&Sw32_registered_hot_keys);
9233 defsubr (&Sw32_reconstruct_hot_key);
9234 defsubr (&Sw32_toggle_lock_key);
9235 defsubr (&Sw32_window_exists_p);
9236 defsubr (&Sw32_find_bdf_fonts);
9237
9238 defsubr (&Sfile_system_info);
9239 defsubr (&Sdefault_printer_name);
9240
9241 /* Setting callback functions for fontset handler. */
9242 get_font_info_func = w32_get_font_info;
9243
9244 #if 0 /* This function pointer doesn't seem to be used anywhere.
9245 And the pointer assigned has the wrong type, anyway. */
9246 list_fonts_func = w32_list_fonts;
9247 #endif
9248
9249 load_font_func = w32_load_font;
9250 find_ccl_program_func = w32_find_ccl_program;
9251 query_font_func = w32_query_font;
9252 set_frame_fontset_func = x_set_font;
9253 get_font_repertory_func = x_get_font_repertory;
9254 check_window_system_func = check_w32;
9255
9256
9257 hourglass_atimer = NULL;
9258 hourglass_shown_p = 0;
9259 defsubr (&Sx_show_tip);
9260 defsubr (&Sx_hide_tip);
9261 tip_timer = Qnil;
9262 staticpro (&tip_timer);
9263 tip_frame = Qnil;
9264 staticpro (&tip_frame);
9265
9266 last_show_tip_args = Qnil;
9267 staticpro (&last_show_tip_args);
9268
9269 defsubr (&Sx_file_dialog);
9270 }
9271
9272
9273 /*
9274 globals_of_w32fns is used to initialize those global variables that
9275 must always be initialized on startup even when the global variable
9276 initialized is non zero (see the function main in emacs.c).
9277 globals_of_w32fns is called from syms_of_w32fns when the global
9278 variable initialized is 0 and directly from main when initialized
9279 is non zero.
9280 */
9281 void
9282 globals_of_w32fns ()
9283 {
9284 HMODULE user32_lib = GetModuleHandle ("user32.dll");
9285 /*
9286 TrackMouseEvent not available in all versions of Windows, so must load
9287 it dynamically. Do it once, here, instead of every time it is used.
9288 */
9289 track_mouse_event_fn = (TrackMouseEvent_Proc)
9290 GetProcAddress (user32_lib, "TrackMouseEvent");
9291 /* ditto for GetClipboardSequenceNumber. */
9292 clipboard_sequence_fn = (ClipboardSequence_Proc)
9293 GetProcAddress (user32_lib, "GetClipboardSequenceNumber");
9294 {
9295 HMODULE imm32_lib = GetModuleHandle ("imm32.dll");
9296 get_composition_string_fn = (ImmGetCompositionString_Proc)
9297 GetProcAddress (imm32_lib, "ImmGetCompositionStringW");
9298 get_ime_context_fn = (ImmGetContext_Proc)
9299 GetProcAddress (imm32_lib, "ImmGetContext");
9300 }
9301 DEFVAR_INT ("w32-ansi-code-page",
9302 &w32_ansi_code_page,
9303 doc: /* The ANSI code page used by the system. */);
9304 w32_ansi_code_page = GetACP ();
9305
9306 /* MessageBox does not work without this when linked to comctl32.dll 6.0. */
9307 InitCommonControls ();
9308 }
9309
9310 #undef abort
9311
9312 void
9313 w32_abort ()
9314 {
9315 int button;
9316 button = MessageBox (NULL,
9317 "A fatal error has occurred!\n\n"
9318 "Would you like to attach a debugger?\n\n"
9319 "Select YES to debug, NO to abort Emacs"
9320 #if __GNUC__
9321 "\n\n(type \"gdb -p <emacs-PID>\" and\n"
9322 "\"continue\" inside GDB before clicking YES.)"
9323 #endif
9324 , "Emacs Abort Dialog",
9325 MB_ICONEXCLAMATION | MB_TASKMODAL
9326 | MB_SETFOREGROUND | MB_YESNO);
9327 switch (button)
9328 {
9329 case IDYES:
9330 DebugBreak ();
9331 exit (2); /* tell the compiler we will never return */
9332 case IDNO:
9333 default:
9334 abort ();
9335 break;
9336 }
9337 }
9338
9339 /* For convenience when debugging. */
9340 int
9341 w32_last_error ()
9342 {
9343 return GetLastError ();
9344 }
9345
9346 /* arch-tag: 707589ab-b9be-4638-8cdd-74629cc9b446
9347 (do not change this comment) */