(redisplay_internal): Make sure Elisp code always sees
[emacs.git] / src / w32fns.c
blobaeb4183393b4f3dfe858626f41d9353a82e66208
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.
5 This file is part of GNU Emacs.
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.
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.
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. */
22 /* Added by Kevin Gallo */
24 #include <config.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <limits.h>
29 #include <errno.h>
31 #include "lisp.h"
32 #include "charset.h"
33 #include "dispextern.h"
34 #include "w32term.h"
35 #include "keyboard.h"
36 #include "frame.h"
37 #include "window.h"
38 #include "buffer.h"
39 #include "fontset.h"
40 #include "intervals.h"
41 #include "blockinput.h"
42 #include "epaths.h"
43 #include "w32heap.h"
44 #include "termhooks.h"
45 #include "coding.h"
46 #include "ccl.h"
47 #include "systime.h"
49 #include "bitmaps/gray.xbm"
51 #include <commdlg.h>
52 #include <shellapi.h>
53 #include <ctype.h>
54 #include <winspool.h>
55 #include <objbase.h>
57 #include <dlgs.h>
58 #define FILE_NAME_TEXT_FIELD edt1
60 void syms_of_w32fns ();
61 void globals_of_w32fns ();
63 extern void free_frame_menubar ();
64 extern double atof ();
65 extern int w32_console_toggle_lock_key P_ ((int, Lisp_Object));
66 extern void w32_menu_display_help P_ ((HWND, HMENU, UINT, UINT));
67 extern void w32_free_menu_strings P_ ((HWND));
68 extern XCharStruct *w32_per_char_metric P_ ((XFontStruct *, wchar_t *, int));
70 extern int quit_char;
72 extern char *lispy_function_keys[];
74 /* The gray bitmap `bitmaps/gray'. This is done because w32term.c uses
75 it, and including `bitmaps/gray' more than once is a problem when
76 config.h defines `static' as an empty replacement string. */
78 int gray_bitmap_width = gray_width;
79 int gray_bitmap_height = gray_height;
80 unsigned char *gray_bitmap_bits = gray_bits;
82 /* The colormap for converting color names to RGB values */
83 Lisp_Object Vw32_color_map;
85 /* Non nil if alt key presses are passed on to Windows. */
86 Lisp_Object Vw32_pass_alt_to_system;
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;
92 /* If non-zero, the windows virtual key code for an alternative quit key. */
93 int w32_quit_key;
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;
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;
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;
107 /* Modifier associated with the left "Windows" key, or nil to act as a
108 normal key. */
109 Lisp_Object Vw32_lwindow_modifier;
111 /* Modifier associated with the right "Windows" key, or nil to act as a
112 normal key. */
113 Lisp_Object Vw32_rwindow_modifier;
115 /* Modifier associated with the "Apps" key, or nil to act as a normal
116 key. */
117 Lisp_Object Vw32_apps_modifier;
119 /* Value is nil if Num Lock acts as a function key. */
120 Lisp_Object Vw32_enable_num_lock;
122 /* Value is nil if Caps Lock acts as a function key. */
123 Lisp_Object Vw32_enable_caps_lock;
125 /* Modifier associated with Scroll Lock, or nil to act as a normal key. */
126 Lisp_Object Vw32_scroll_lock_modifier;
128 /* Switch to control whether we inhibit requests for synthesized bold
129 and italic versions of fonts. */
130 int w32_enable_synthesized_fonts;
132 /* Enable palette management. */
133 Lisp_Object Vw32_enable_palette;
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;
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;
143 /* Flag to indicate if XBUTTON events should be passed on to Windows. */
144 int w32_pass_extra_mouse_buttons_to_system;
146 /* Non nil if no window manager is in use. */
147 Lisp_Object Vx_no_window_manager;
149 /* Non-zero means we're allowed to display a hourglass pointer. */
151 int display_hourglass_p;
153 /* The background and shape of the mouse pointer, and shape when not
154 over text or in the modeline. */
156 Lisp_Object Vx_pointer_shape, Vx_nontext_pointer_shape, Vx_mode_pointer_shape;
157 Lisp_Object Vx_hourglass_pointer_shape, Vx_window_horizontal_drag_shape;
159 /* The shape when over mouse-sensitive text. */
161 Lisp_Object Vx_sensitive_text_pointer_shape;
163 #ifndef IDC_HAND
164 #define IDC_HAND MAKEINTRESOURCE(32649)
165 #endif
167 /* Color of chars displayed in cursor box. */
169 Lisp_Object Vx_cursor_fore_pixel;
171 /* Nonzero if using Windows. */
173 static int w32_in_use;
175 /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'. */
177 Lisp_Object Vx_pixel_size_width_font_regexp;
179 /* Alist of bdf fonts and the files that define them. */
180 Lisp_Object Vw32_bdf_filename_alist;
182 /* A flag to control whether fonts are matched strictly or not. */
183 int w32_strict_fontnames;
185 /* A flag to control whether we should only repaint if GetUpdateRect
186 indicates there is an update region. */
187 int w32_strict_painting;
189 /* Associative list linking character set strings to Windows codepages. */
190 Lisp_Object Vw32_charset_info_alist;
192 /* VIETNAMESE_CHARSET is not defined in some versions of MSVC. */
193 #ifndef VIETNAMESE_CHARSET
194 #define VIETNAMESE_CHARSET 163
195 #endif
197 Lisp_Object Qnone;
198 Lisp_Object Qsuppress_icon;
199 Lisp_Object Qundefined_color;
200 Lisp_Object Qcancel_timer;
201 Lisp_Object Qhyper;
202 Lisp_Object Qsuper;
203 Lisp_Object Qmeta;
204 Lisp_Object Qalt;
205 Lisp_Object Qctrl;
206 Lisp_Object Qcontrol;
207 Lisp_Object Qshift;
209 Lisp_Object Qw32_charset_ansi;
210 Lisp_Object Qw32_charset_default;
211 Lisp_Object Qw32_charset_symbol;
212 Lisp_Object Qw32_charset_shiftjis;
213 Lisp_Object Qw32_charset_hangeul;
214 Lisp_Object Qw32_charset_gb2312;
215 Lisp_Object Qw32_charset_chinesebig5;
216 Lisp_Object Qw32_charset_oem;
218 #ifndef JOHAB_CHARSET
219 #define JOHAB_CHARSET 130
220 #endif
221 #ifdef JOHAB_CHARSET
222 Lisp_Object Qw32_charset_easteurope;
223 Lisp_Object Qw32_charset_turkish;
224 Lisp_Object Qw32_charset_baltic;
225 Lisp_Object Qw32_charset_russian;
226 Lisp_Object Qw32_charset_arabic;
227 Lisp_Object Qw32_charset_greek;
228 Lisp_Object Qw32_charset_hebrew;
229 Lisp_Object Qw32_charset_vietnamese;
230 Lisp_Object Qw32_charset_thai;
231 Lisp_Object Qw32_charset_johab;
232 Lisp_Object Qw32_charset_mac;
233 #endif
235 #ifdef UNICODE_CHARSET
236 Lisp_Object Qw32_charset_unicode;
237 #endif
239 /* The ANSI codepage. */
240 int w32_ansi_code_page;
242 /* Prefix for system colors. */
243 #define SYSTEM_COLOR_PREFIX "System"
244 #define SYSTEM_COLOR_PREFIX_LEN (sizeof (SYSTEM_COLOR_PREFIX) - 1)
246 /* State variables for emulating a three button mouse. */
247 #define LMOUSE 1
248 #define MMOUSE 2
249 #define RMOUSE 4
251 static int button_state = 0;
252 static W32Msg saved_mouse_button_msg;
253 static unsigned mouse_button_timer = 0; /* non-zero when timer is active */
254 static W32Msg saved_mouse_move_msg;
255 static unsigned mouse_move_timer = 0;
257 /* Window that is tracking the mouse. */
258 static HWND track_mouse_window;
260 typedef BOOL (WINAPI * TrackMouseEvent_Proc)
261 (IN OUT LPTRACKMOUSEEVENT lpEventTrack);
263 TrackMouseEvent_Proc track_mouse_event_fn = NULL;
264 ClipboardSequence_Proc clipboard_sequence_fn = NULL;
265 extern AppendMenuW_Proc unicode_append_menu;
267 /* W95 mousewheel handler */
268 unsigned int msh_mousewheel = 0;
270 /* Timers */
271 #define MOUSE_BUTTON_ID 1
272 #define MOUSE_MOVE_ID 2
273 #define MENU_FREE_ID 3
274 /* The delay (milliseconds) before a menu is freed after WM_EXITMENULOOP
275 is received. */
276 #define MENU_FREE_DELAY 1000
277 static unsigned menu_free_timer = 0;
279 /* The below are defined in frame.c. */
281 extern Lisp_Object Vwindow_system_version;
283 #ifdef GLYPH_DEBUG
284 int image_cache_refcount, dpyinfo_refcount;
285 #endif
288 /* From w32term.c. */
289 extern int w32_num_mouse_buttons;
290 extern Lisp_Object Vw32_recognize_altgr;
292 extern HWND w32_system_caret_hwnd;
294 extern int w32_system_caret_height;
295 extern int w32_system_caret_x;
296 extern int w32_system_caret_y;
297 extern int w32_use_visible_system_caret;
299 static HWND w32_visible_system_caret_hwnd;
301 /* From w32menu.c */
302 extern HMENU current_popup_menu;
303 static int menubar_in_use = 0;
306 /* Error if we are not connected to MS-Windows. */
307 void
308 check_w32 ()
310 if (! w32_in_use)
311 error ("MS-Windows not in use or not initialized");
314 /* Nonzero if we can use mouse menus.
315 You should not call this unless HAVE_MENUS is defined. */
318 have_menus_p ()
320 return w32_in_use;
323 /* Extract a frame as a FRAME_PTR, defaulting to the selected frame
324 and checking validity for W32. */
326 FRAME_PTR
327 check_x_frame (frame)
328 Lisp_Object frame;
330 FRAME_PTR f;
332 if (NILP (frame))
333 frame = selected_frame;
334 CHECK_LIVE_FRAME (frame);
335 f = XFRAME (frame);
336 if (! FRAME_W32_P (f))
337 error ("Non-W32 frame used");
338 return f;
341 /* Let the user specify a display with a frame.
342 nil stands for the selected frame--or, if that is not a w32 frame,
343 the first display on the list. */
345 struct w32_display_info *
346 check_x_display_info (frame)
347 Lisp_Object frame;
349 if (NILP (frame))
351 struct frame *sf = XFRAME (selected_frame);
353 if (FRAME_W32_P (sf) && FRAME_LIVE_P (sf))
354 return FRAME_W32_DISPLAY_INFO (sf);
355 else
356 return &one_w32_display_info;
358 else if (STRINGP (frame))
359 return x_display_info_for_name (frame);
360 else
362 FRAME_PTR f;
364 CHECK_LIVE_FRAME (frame);
365 f = XFRAME (frame);
366 if (! FRAME_W32_P (f))
367 error ("Non-W32 frame used");
368 return FRAME_W32_DISPLAY_INFO (f);
372 /* Return the Emacs frame-object corresponding to an w32 window.
373 It could be the frame's main window or an icon window. */
375 /* This function can be called during GC, so use GC_xxx type test macros. */
377 struct frame *
378 x_window_to_frame (dpyinfo, wdesc)
379 struct w32_display_info *dpyinfo;
380 HWND wdesc;
382 Lisp_Object tail, frame;
383 struct frame *f;
385 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
387 frame = XCAR (tail);
388 if (!GC_FRAMEP (frame))
389 continue;
390 f = XFRAME (frame);
391 if (!FRAME_W32_P (f) || FRAME_W32_DISPLAY_INFO (f) != dpyinfo)
392 continue;
393 if (f->output_data.w32->hourglass_window == wdesc)
394 return f;
396 if (FRAME_W32_WINDOW (f) == wdesc)
397 return f;
399 return 0;
403 static Lisp_Object unwind_create_frame P_ ((Lisp_Object));
404 static Lisp_Object unwind_create_tip_frame P_ ((Lisp_Object));
405 static void my_create_window P_ ((struct frame *));
406 static void my_create_tip_window P_ ((struct frame *));
408 /* TODO: Native Input Method support; see x_create_im. */
409 void x_set_foreground_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
410 void x_set_background_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
411 void x_set_mouse_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
412 void x_set_cursor_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
413 void x_set_border_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
414 void x_set_cursor_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
415 void x_set_icon_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
416 void x_set_icon_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
417 void x_explicitly_set_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
418 void x_set_menu_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
419 void x_set_title P_ ((struct frame *, Lisp_Object, Lisp_Object));
420 void x_set_tool_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
421 static void x_edge_detection P_ ((struct frame *, struct image *, Lisp_Object,
422 Lisp_Object));
427 /* Store the screen positions of frame F into XPTR and YPTR.
428 These are the positions of the containing window manager window,
429 not Emacs's own window. */
431 void
432 x_real_positions (f, xptr, yptr)
433 FRAME_PTR f;
434 int *xptr, *yptr;
436 POINT pt;
437 RECT rect;
439 /* Get the bounds of the WM window. */
440 GetWindowRect (FRAME_W32_WINDOW (f), &rect);
442 pt.x = 0;
443 pt.y = 0;
445 /* Convert (0, 0) in the client area to screen co-ordinates. */
446 ClientToScreen (FRAME_W32_WINDOW (f), &pt);
448 /* Remember x_pixels_diff and y_pixels_diff. */
449 f->x_pixels_diff = pt.x - rect.left;
450 f->y_pixels_diff = pt.y - rect.top;
452 *xptr = rect.left;
453 *yptr = rect.top;
458 DEFUN ("w32-define-rgb-color", Fw32_define_rgb_color,
459 Sw32_define_rgb_color, 4, 4, 0,
460 doc: /* Convert RGB numbers to a windows color reference and associate with NAME.
461 This adds or updates a named color to w32-color-map, making it
462 available for use. The original entry's RGB ref is returned, or nil
463 if the entry is new. */)
464 (red, green, blue, name)
465 Lisp_Object red, green, blue, name;
467 Lisp_Object rgb;
468 Lisp_Object oldrgb = Qnil;
469 Lisp_Object entry;
471 CHECK_NUMBER (red);
472 CHECK_NUMBER (green);
473 CHECK_NUMBER (blue);
474 CHECK_STRING (name);
476 XSETINT (rgb, RGB(XUINT (red), XUINT (green), XUINT (blue)));
478 BLOCK_INPUT;
480 /* replace existing entry in w32-color-map or add new entry. */
481 entry = Fassoc (name, Vw32_color_map);
482 if (NILP (entry))
484 entry = Fcons (name, rgb);
485 Vw32_color_map = Fcons (entry, Vw32_color_map);
487 else
489 oldrgb = Fcdr (entry);
490 Fsetcdr (entry, rgb);
493 UNBLOCK_INPUT;
495 return (oldrgb);
498 DEFUN ("w32-load-color-file", Fw32_load_color_file,
499 Sw32_load_color_file, 1, 1, 0,
500 doc: /* Create an alist of color entries from an external file.
501 Assign this value to w32-color-map to replace the existing color map.
503 The file should define one named RGB color per line like so:
504 R G B name
505 where R,G,B are numbers between 0 and 255 and name is an arbitrary string. */)
506 (filename)
507 Lisp_Object filename;
509 FILE *fp;
510 Lisp_Object cmap = Qnil;
511 Lisp_Object abspath;
513 CHECK_STRING (filename);
514 abspath = Fexpand_file_name (filename, Qnil);
516 fp = fopen (SDATA (filename), "rt");
517 if (fp)
519 char buf[512];
520 int red, green, blue;
521 int num;
523 BLOCK_INPUT;
525 while (fgets (buf, sizeof (buf), fp) != NULL) {
526 if (sscanf (buf, "%u %u %u %n", &red, &green, &blue, &num) == 3)
528 char *name = buf + num;
529 num = strlen (name) - 1;
530 if (name[num] == '\n')
531 name[num] = 0;
532 cmap = Fcons (Fcons (build_string (name),
533 make_number (RGB (red, green, blue))),
534 cmap);
537 fclose (fp);
539 UNBLOCK_INPUT;
542 return cmap;
545 /* The default colors for the w32 color map */
546 typedef struct colormap_t
548 char *name;
549 COLORREF colorref;
550 } colormap_t;
552 colormap_t w32_color_map[] =
554 {"snow" , PALETTERGB (255,250,250)},
555 {"ghost white" , PALETTERGB (248,248,255)},
556 {"GhostWhite" , PALETTERGB (248,248,255)},
557 {"white smoke" , PALETTERGB (245,245,245)},
558 {"WhiteSmoke" , PALETTERGB (245,245,245)},
559 {"gainsboro" , PALETTERGB (220,220,220)},
560 {"floral white" , PALETTERGB (255,250,240)},
561 {"FloralWhite" , PALETTERGB (255,250,240)},
562 {"old lace" , PALETTERGB (253,245,230)},
563 {"OldLace" , PALETTERGB (253,245,230)},
564 {"linen" , PALETTERGB (250,240,230)},
565 {"antique white" , PALETTERGB (250,235,215)},
566 {"AntiqueWhite" , PALETTERGB (250,235,215)},
567 {"papaya whip" , PALETTERGB (255,239,213)},
568 {"PapayaWhip" , PALETTERGB (255,239,213)},
569 {"blanched almond" , PALETTERGB (255,235,205)},
570 {"BlanchedAlmond" , PALETTERGB (255,235,205)},
571 {"bisque" , PALETTERGB (255,228,196)},
572 {"peach puff" , PALETTERGB (255,218,185)},
573 {"PeachPuff" , PALETTERGB (255,218,185)},
574 {"navajo white" , PALETTERGB (255,222,173)},
575 {"NavajoWhite" , PALETTERGB (255,222,173)},
576 {"moccasin" , PALETTERGB (255,228,181)},
577 {"cornsilk" , PALETTERGB (255,248,220)},
578 {"ivory" , PALETTERGB (255,255,240)},
579 {"lemon chiffon" , PALETTERGB (255,250,205)},
580 {"LemonChiffon" , PALETTERGB (255,250,205)},
581 {"seashell" , PALETTERGB (255,245,238)},
582 {"honeydew" , PALETTERGB (240,255,240)},
583 {"mint cream" , PALETTERGB (245,255,250)},
584 {"MintCream" , PALETTERGB (245,255,250)},
585 {"azure" , PALETTERGB (240,255,255)},
586 {"alice blue" , PALETTERGB (240,248,255)},
587 {"AliceBlue" , PALETTERGB (240,248,255)},
588 {"lavender" , PALETTERGB (230,230,250)},
589 {"lavender blush" , PALETTERGB (255,240,245)},
590 {"LavenderBlush" , PALETTERGB (255,240,245)},
591 {"misty rose" , PALETTERGB (255,228,225)},
592 {"MistyRose" , PALETTERGB (255,228,225)},
593 {"white" , PALETTERGB (255,255,255)},
594 {"black" , PALETTERGB ( 0, 0, 0)},
595 {"dark slate gray" , PALETTERGB ( 47, 79, 79)},
596 {"DarkSlateGray" , PALETTERGB ( 47, 79, 79)},
597 {"dark slate grey" , PALETTERGB ( 47, 79, 79)},
598 {"DarkSlateGrey" , PALETTERGB ( 47, 79, 79)},
599 {"dim gray" , PALETTERGB (105,105,105)},
600 {"DimGray" , PALETTERGB (105,105,105)},
601 {"dim grey" , PALETTERGB (105,105,105)},
602 {"DimGrey" , PALETTERGB (105,105,105)},
603 {"slate gray" , PALETTERGB (112,128,144)},
604 {"SlateGray" , PALETTERGB (112,128,144)},
605 {"slate grey" , PALETTERGB (112,128,144)},
606 {"SlateGrey" , PALETTERGB (112,128,144)},
607 {"light slate gray" , PALETTERGB (119,136,153)},
608 {"LightSlateGray" , PALETTERGB (119,136,153)},
609 {"light slate grey" , PALETTERGB (119,136,153)},
610 {"LightSlateGrey" , PALETTERGB (119,136,153)},
611 {"gray" , PALETTERGB (190,190,190)},
612 {"grey" , PALETTERGB (190,190,190)},
613 {"light grey" , PALETTERGB (211,211,211)},
614 {"LightGrey" , PALETTERGB (211,211,211)},
615 {"light gray" , PALETTERGB (211,211,211)},
616 {"LightGray" , PALETTERGB (211,211,211)},
617 {"midnight blue" , PALETTERGB ( 25, 25,112)},
618 {"MidnightBlue" , PALETTERGB ( 25, 25,112)},
619 {"navy" , PALETTERGB ( 0, 0,128)},
620 {"navy blue" , PALETTERGB ( 0, 0,128)},
621 {"NavyBlue" , PALETTERGB ( 0, 0,128)},
622 {"cornflower blue" , PALETTERGB (100,149,237)},
623 {"CornflowerBlue" , PALETTERGB (100,149,237)},
624 {"dark slate blue" , PALETTERGB ( 72, 61,139)},
625 {"DarkSlateBlue" , PALETTERGB ( 72, 61,139)},
626 {"slate blue" , PALETTERGB (106, 90,205)},
627 {"SlateBlue" , PALETTERGB (106, 90,205)},
628 {"medium slate blue" , PALETTERGB (123,104,238)},
629 {"MediumSlateBlue" , PALETTERGB (123,104,238)},
630 {"light slate blue" , PALETTERGB (132,112,255)},
631 {"LightSlateBlue" , PALETTERGB (132,112,255)},
632 {"medium blue" , PALETTERGB ( 0, 0,205)},
633 {"MediumBlue" , PALETTERGB ( 0, 0,205)},
634 {"royal blue" , PALETTERGB ( 65,105,225)},
635 {"RoyalBlue" , PALETTERGB ( 65,105,225)},
636 {"blue" , PALETTERGB ( 0, 0,255)},
637 {"dodger blue" , PALETTERGB ( 30,144,255)},
638 {"DodgerBlue" , PALETTERGB ( 30,144,255)},
639 {"deep sky blue" , PALETTERGB ( 0,191,255)},
640 {"DeepSkyBlue" , PALETTERGB ( 0,191,255)},
641 {"sky blue" , PALETTERGB (135,206,235)},
642 {"SkyBlue" , PALETTERGB (135,206,235)},
643 {"light sky blue" , PALETTERGB (135,206,250)},
644 {"LightSkyBlue" , PALETTERGB (135,206,250)},
645 {"steel blue" , PALETTERGB ( 70,130,180)},
646 {"SteelBlue" , PALETTERGB ( 70,130,180)},
647 {"light steel blue" , PALETTERGB (176,196,222)},
648 {"LightSteelBlue" , PALETTERGB (176,196,222)},
649 {"light blue" , PALETTERGB (173,216,230)},
650 {"LightBlue" , PALETTERGB (173,216,230)},
651 {"powder blue" , PALETTERGB (176,224,230)},
652 {"PowderBlue" , PALETTERGB (176,224,230)},
653 {"pale turquoise" , PALETTERGB (175,238,238)},
654 {"PaleTurquoise" , PALETTERGB (175,238,238)},
655 {"dark turquoise" , PALETTERGB ( 0,206,209)},
656 {"DarkTurquoise" , PALETTERGB ( 0,206,209)},
657 {"medium turquoise" , PALETTERGB ( 72,209,204)},
658 {"MediumTurquoise" , PALETTERGB ( 72,209,204)},
659 {"turquoise" , PALETTERGB ( 64,224,208)},
660 {"cyan" , PALETTERGB ( 0,255,255)},
661 {"light cyan" , PALETTERGB (224,255,255)},
662 {"LightCyan" , PALETTERGB (224,255,255)},
663 {"cadet blue" , PALETTERGB ( 95,158,160)},
664 {"CadetBlue" , PALETTERGB ( 95,158,160)},
665 {"medium aquamarine" , PALETTERGB (102,205,170)},
666 {"MediumAquamarine" , PALETTERGB (102,205,170)},
667 {"aquamarine" , PALETTERGB (127,255,212)},
668 {"dark green" , PALETTERGB ( 0,100, 0)},
669 {"DarkGreen" , PALETTERGB ( 0,100, 0)},
670 {"dark olive green" , PALETTERGB ( 85,107, 47)},
671 {"DarkOliveGreen" , PALETTERGB ( 85,107, 47)},
672 {"dark sea green" , PALETTERGB (143,188,143)},
673 {"DarkSeaGreen" , PALETTERGB (143,188,143)},
674 {"sea green" , PALETTERGB ( 46,139, 87)},
675 {"SeaGreen" , PALETTERGB ( 46,139, 87)},
676 {"medium sea green" , PALETTERGB ( 60,179,113)},
677 {"MediumSeaGreen" , PALETTERGB ( 60,179,113)},
678 {"light sea green" , PALETTERGB ( 32,178,170)},
679 {"LightSeaGreen" , PALETTERGB ( 32,178,170)},
680 {"pale green" , PALETTERGB (152,251,152)},
681 {"PaleGreen" , PALETTERGB (152,251,152)},
682 {"spring green" , PALETTERGB ( 0,255,127)},
683 {"SpringGreen" , PALETTERGB ( 0,255,127)},
684 {"lawn green" , PALETTERGB (124,252, 0)},
685 {"LawnGreen" , PALETTERGB (124,252, 0)},
686 {"green" , PALETTERGB ( 0,255, 0)},
687 {"chartreuse" , PALETTERGB (127,255, 0)},
688 {"medium spring green" , PALETTERGB ( 0,250,154)},
689 {"MediumSpringGreen" , PALETTERGB ( 0,250,154)},
690 {"green yellow" , PALETTERGB (173,255, 47)},
691 {"GreenYellow" , PALETTERGB (173,255, 47)},
692 {"lime green" , PALETTERGB ( 50,205, 50)},
693 {"LimeGreen" , PALETTERGB ( 50,205, 50)},
694 {"yellow green" , PALETTERGB (154,205, 50)},
695 {"YellowGreen" , PALETTERGB (154,205, 50)},
696 {"forest green" , PALETTERGB ( 34,139, 34)},
697 {"ForestGreen" , PALETTERGB ( 34,139, 34)},
698 {"olive drab" , PALETTERGB (107,142, 35)},
699 {"OliveDrab" , PALETTERGB (107,142, 35)},
700 {"dark khaki" , PALETTERGB (189,183,107)},
701 {"DarkKhaki" , PALETTERGB (189,183,107)},
702 {"khaki" , PALETTERGB (240,230,140)},
703 {"pale goldenrod" , PALETTERGB (238,232,170)},
704 {"PaleGoldenrod" , PALETTERGB (238,232,170)},
705 {"light goldenrod yellow" , PALETTERGB (250,250,210)},
706 {"LightGoldenrodYellow" , PALETTERGB (250,250,210)},
707 {"light yellow" , PALETTERGB (255,255,224)},
708 {"LightYellow" , PALETTERGB (255,255,224)},
709 {"yellow" , PALETTERGB (255,255, 0)},
710 {"gold" , PALETTERGB (255,215, 0)},
711 {"light goldenrod" , PALETTERGB (238,221,130)},
712 {"LightGoldenrod" , PALETTERGB (238,221,130)},
713 {"goldenrod" , PALETTERGB (218,165, 32)},
714 {"dark goldenrod" , PALETTERGB (184,134, 11)},
715 {"DarkGoldenrod" , PALETTERGB (184,134, 11)},
716 {"rosy brown" , PALETTERGB (188,143,143)},
717 {"RosyBrown" , PALETTERGB (188,143,143)},
718 {"indian red" , PALETTERGB (205, 92, 92)},
719 {"IndianRed" , PALETTERGB (205, 92, 92)},
720 {"saddle brown" , PALETTERGB (139, 69, 19)},
721 {"SaddleBrown" , PALETTERGB (139, 69, 19)},
722 {"sienna" , PALETTERGB (160, 82, 45)},
723 {"peru" , PALETTERGB (205,133, 63)},
724 {"burlywood" , PALETTERGB (222,184,135)},
725 {"beige" , PALETTERGB (245,245,220)},
726 {"wheat" , PALETTERGB (245,222,179)},
727 {"sandy brown" , PALETTERGB (244,164, 96)},
728 {"SandyBrown" , PALETTERGB (244,164, 96)},
729 {"tan" , PALETTERGB (210,180,140)},
730 {"chocolate" , PALETTERGB (210,105, 30)},
731 {"firebrick" , PALETTERGB (178,34, 34)},
732 {"brown" , PALETTERGB (165,42, 42)},
733 {"dark salmon" , PALETTERGB (233,150,122)},
734 {"DarkSalmon" , PALETTERGB (233,150,122)},
735 {"salmon" , PALETTERGB (250,128,114)},
736 {"light salmon" , PALETTERGB (255,160,122)},
737 {"LightSalmon" , PALETTERGB (255,160,122)},
738 {"orange" , PALETTERGB (255,165, 0)},
739 {"dark orange" , PALETTERGB (255,140, 0)},
740 {"DarkOrange" , PALETTERGB (255,140, 0)},
741 {"coral" , PALETTERGB (255,127, 80)},
742 {"light coral" , PALETTERGB (240,128,128)},
743 {"LightCoral" , PALETTERGB (240,128,128)},
744 {"tomato" , PALETTERGB (255, 99, 71)},
745 {"orange red" , PALETTERGB (255, 69, 0)},
746 {"OrangeRed" , PALETTERGB (255, 69, 0)},
747 {"red" , PALETTERGB (255, 0, 0)},
748 {"hot pink" , PALETTERGB (255,105,180)},
749 {"HotPink" , PALETTERGB (255,105,180)},
750 {"deep pink" , PALETTERGB (255, 20,147)},
751 {"DeepPink" , PALETTERGB (255, 20,147)},
752 {"pink" , PALETTERGB (255,192,203)},
753 {"light pink" , PALETTERGB (255,182,193)},
754 {"LightPink" , PALETTERGB (255,182,193)},
755 {"pale violet red" , PALETTERGB (219,112,147)},
756 {"PaleVioletRed" , PALETTERGB (219,112,147)},
757 {"maroon" , PALETTERGB (176, 48, 96)},
758 {"medium violet red" , PALETTERGB (199, 21,133)},
759 {"MediumVioletRed" , PALETTERGB (199, 21,133)},
760 {"violet red" , PALETTERGB (208, 32,144)},
761 {"VioletRed" , PALETTERGB (208, 32,144)},
762 {"magenta" , PALETTERGB (255, 0,255)},
763 {"violet" , PALETTERGB (238,130,238)},
764 {"plum" , PALETTERGB (221,160,221)},
765 {"orchid" , PALETTERGB (218,112,214)},
766 {"medium orchid" , PALETTERGB (186, 85,211)},
767 {"MediumOrchid" , PALETTERGB (186, 85,211)},
768 {"dark orchid" , PALETTERGB (153, 50,204)},
769 {"DarkOrchid" , PALETTERGB (153, 50,204)},
770 {"dark violet" , PALETTERGB (148, 0,211)},
771 {"DarkViolet" , PALETTERGB (148, 0,211)},
772 {"blue violet" , PALETTERGB (138, 43,226)},
773 {"BlueViolet" , PALETTERGB (138, 43,226)},
774 {"purple" , PALETTERGB (160, 32,240)},
775 {"medium purple" , PALETTERGB (147,112,219)},
776 {"MediumPurple" , PALETTERGB (147,112,219)},
777 {"thistle" , PALETTERGB (216,191,216)},
778 {"gray0" , PALETTERGB ( 0, 0, 0)},
779 {"grey0" , PALETTERGB ( 0, 0, 0)},
780 {"dark grey" , PALETTERGB (169,169,169)},
781 {"DarkGrey" , PALETTERGB (169,169,169)},
782 {"dark gray" , PALETTERGB (169,169,169)},
783 {"DarkGray" , PALETTERGB (169,169,169)},
784 {"dark blue" , PALETTERGB ( 0, 0,139)},
785 {"DarkBlue" , PALETTERGB ( 0, 0,139)},
786 {"dark cyan" , PALETTERGB ( 0,139,139)},
787 {"DarkCyan" , PALETTERGB ( 0,139,139)},
788 {"dark magenta" , PALETTERGB (139, 0,139)},
789 {"DarkMagenta" , PALETTERGB (139, 0,139)},
790 {"dark red" , PALETTERGB (139, 0, 0)},
791 {"DarkRed" , PALETTERGB (139, 0, 0)},
792 {"light green" , PALETTERGB (144,238,144)},
793 {"LightGreen" , PALETTERGB (144,238,144)},
796 DEFUN ("w32-default-color-map", Fw32_default_color_map, Sw32_default_color_map,
797 0, 0, 0, doc: /* Return the default color map. */)
800 int i;
801 colormap_t *pc = w32_color_map;
802 Lisp_Object cmap;
804 BLOCK_INPUT;
806 cmap = Qnil;
808 for (i = 0; i < sizeof (w32_color_map) / sizeof (w32_color_map[0]);
809 pc++, i++)
810 cmap = Fcons (Fcons (build_string (pc->name),
811 make_number (pc->colorref)),
812 cmap);
814 UNBLOCK_INPUT;
816 return (cmap);
819 Lisp_Object
820 w32_to_x_color (rgb)
821 Lisp_Object rgb;
823 Lisp_Object color;
825 CHECK_NUMBER (rgb);
827 BLOCK_INPUT;
829 color = Frassq (rgb, Vw32_color_map);
831 UNBLOCK_INPUT;
833 if (!NILP (color))
834 return (Fcar (color));
835 else
836 return Qnil;
839 static Lisp_Object
840 w32_color_map_lookup (colorname)
841 char *colorname;
843 Lisp_Object tail, ret = Qnil;
845 BLOCK_INPUT;
847 for (tail = Vw32_color_map; !NILP (tail); tail = Fcdr (tail))
849 register Lisp_Object elt, tem;
851 elt = Fcar (tail);
852 if (!CONSP (elt)) continue;
854 tem = Fcar (elt);
856 if (lstrcmpi (SDATA (tem), colorname) == 0)
858 ret = Fcdr (elt);
859 break;
862 QUIT;
866 UNBLOCK_INPUT;
868 return ret;
872 static void
873 add_system_logical_colors_to_map (system_colors)
874 Lisp_Object *system_colors;
876 HKEY colors_key;
878 /* Other registry operations are done with input blocked. */
879 BLOCK_INPUT;
881 /* Look for "Control Panel/Colors" under User and Machine registry
882 settings. */
883 if (RegOpenKeyEx (HKEY_CURRENT_USER, "Control Panel\\Colors", 0,
884 KEY_READ, &colors_key) == ERROR_SUCCESS
885 || RegOpenKeyEx (HKEY_LOCAL_MACHINE, "Control Panel\\Colors", 0,
886 KEY_READ, &colors_key) == ERROR_SUCCESS)
888 /* List all keys. */
889 char color_buffer[64];
890 char full_name_buffer[MAX_PATH + SYSTEM_COLOR_PREFIX_LEN];
891 int index = 0;
892 DWORD name_size, color_size;
893 char *name_buffer = full_name_buffer + SYSTEM_COLOR_PREFIX_LEN;
895 name_size = sizeof (full_name_buffer) - SYSTEM_COLOR_PREFIX_LEN;
896 color_size = sizeof (color_buffer);
898 strcpy (full_name_buffer, SYSTEM_COLOR_PREFIX);
900 while (RegEnumValueA (colors_key, index, name_buffer, &name_size,
901 NULL, NULL, color_buffer, &color_size)
902 == ERROR_SUCCESS)
904 int r, g, b;
905 if (sscanf (color_buffer, " %u %u %u", &r, &g, &b) == 3)
906 *system_colors = Fcons (Fcons (build_string (full_name_buffer),
907 make_number (RGB (r, g, b))),
908 *system_colors);
910 name_size = sizeof (full_name_buffer) - SYSTEM_COLOR_PREFIX_LEN;
911 color_size = sizeof (color_buffer);
912 index++;
914 RegCloseKey (colors_key);
917 UNBLOCK_INPUT;
921 static Lisp_Object
922 x_to_w32_color (colorname)
923 char * colorname;
925 register Lisp_Object ret = Qnil;
927 BLOCK_INPUT;
929 if (colorname[0] == '#')
931 /* Could be an old-style RGB Device specification. */
932 char *color;
933 int size;
934 color = colorname + 1;
936 size = strlen(color);
937 if (size == 3 || size == 6 || size == 9 || size == 12)
939 UINT colorval;
940 int i, pos;
941 pos = 0;
942 size /= 3;
943 colorval = 0;
945 for (i = 0; i < 3; i++)
947 char *end;
948 char t;
949 unsigned long value;
951 /* The check for 'x' in the following conditional takes into
952 account the fact that strtol allows a "0x" in front of
953 our numbers, and we don't. */
954 if (!isxdigit(color[0]) || color[1] == 'x')
955 break;
956 t = color[size];
957 color[size] = '\0';
958 value = strtoul(color, &end, 16);
959 color[size] = t;
960 if (errno == ERANGE || end - color != size)
961 break;
962 switch (size)
964 case 1:
965 value = value * 0x10;
966 break;
967 case 2:
968 break;
969 case 3:
970 value /= 0x10;
971 break;
972 case 4:
973 value /= 0x100;
974 break;
976 colorval |= (value << pos);
977 pos += 0x8;
978 if (i == 2)
980 UNBLOCK_INPUT;
981 XSETINT (ret, colorval);
982 return ret;
984 color = end;
988 else if (strnicmp(colorname, "rgb:", 4) == 0)
990 char *color;
991 UINT colorval;
992 int i, pos;
993 pos = 0;
995 colorval = 0;
996 color = colorname + 4;
997 for (i = 0; i < 3; i++)
999 char *end;
1000 unsigned long value;
1002 /* The check for 'x' in the following conditional takes into
1003 account the fact that strtol allows a "0x" in front of
1004 our numbers, and we don't. */
1005 if (!isxdigit(color[0]) || color[1] == 'x')
1006 break;
1007 value = strtoul(color, &end, 16);
1008 if (errno == ERANGE)
1009 break;
1010 switch (end - color)
1012 case 1:
1013 value = value * 0x10 + value;
1014 break;
1015 case 2:
1016 break;
1017 case 3:
1018 value /= 0x10;
1019 break;
1020 case 4:
1021 value /= 0x100;
1022 break;
1023 default:
1024 value = ULONG_MAX;
1026 if (value == ULONG_MAX)
1027 break;
1028 colorval |= (value << pos);
1029 pos += 0x8;
1030 if (i == 2)
1032 if (*end != '\0')
1033 break;
1034 UNBLOCK_INPUT;
1035 XSETINT (ret, colorval);
1036 return ret;
1038 if (*end != '/')
1039 break;
1040 color = end + 1;
1043 else if (strnicmp(colorname, "rgbi:", 5) == 0)
1045 /* This is an RGB Intensity specification. */
1046 char *color;
1047 UINT colorval;
1048 int i, pos;
1049 pos = 0;
1051 colorval = 0;
1052 color = colorname + 5;
1053 for (i = 0; i < 3; i++)
1055 char *end;
1056 double value;
1057 UINT val;
1059 value = strtod(color, &end);
1060 if (errno == ERANGE)
1061 break;
1062 if (value < 0.0 || value > 1.0)
1063 break;
1064 val = (UINT)(0x100 * value);
1065 /* We used 0x100 instead of 0xFF to give a continuous
1066 range between 0.0 and 1.0 inclusive. The next statement
1067 fixes the 1.0 case. */
1068 if (val == 0x100)
1069 val = 0xFF;
1070 colorval |= (val << pos);
1071 pos += 0x8;
1072 if (i == 2)
1074 if (*end != '\0')
1075 break;
1076 UNBLOCK_INPUT;
1077 XSETINT (ret, colorval);
1078 return ret;
1080 if (*end != '/')
1081 break;
1082 color = end + 1;
1085 /* I am not going to attempt to handle any of the CIE color schemes
1086 or TekHVC, since I don't know the algorithms for conversion to
1087 RGB. */
1089 /* If we fail to lookup the color name in w32_color_map, then check the
1090 colorname to see if it can be crudely approximated: If the X color
1091 ends in a number (e.g., "darkseagreen2"), strip the number and
1092 return the result of looking up the base color name. */
1093 ret = w32_color_map_lookup (colorname);
1094 if (NILP (ret))
1096 int len = strlen (colorname);
1098 if (isdigit (colorname[len - 1]))
1100 char *ptr, *approx = alloca (len + 1);
1102 strcpy (approx, colorname);
1103 ptr = &approx[len - 1];
1104 while (ptr > approx && isdigit (*ptr))
1105 *ptr-- = '\0';
1107 ret = w32_color_map_lookup (approx);
1111 UNBLOCK_INPUT;
1112 return ret;
1115 void
1116 w32_regenerate_palette (FRAME_PTR f)
1118 struct w32_palette_entry * list;
1119 LOGPALETTE * log_palette;
1120 HPALETTE new_palette;
1121 int i;
1123 /* don't bother trying to create palette if not supported */
1124 if (! FRAME_W32_DISPLAY_INFO (f)->has_palette)
1125 return;
1127 log_palette = (LOGPALETTE *)
1128 alloca (sizeof (LOGPALETTE) +
1129 FRAME_W32_DISPLAY_INFO (f)->num_colors * sizeof (PALETTEENTRY));
1130 log_palette->palVersion = 0x300;
1131 log_palette->palNumEntries = FRAME_W32_DISPLAY_INFO (f)->num_colors;
1133 list = FRAME_W32_DISPLAY_INFO (f)->color_list;
1134 for (i = 0;
1135 i < FRAME_W32_DISPLAY_INFO (f)->num_colors;
1136 i++, list = list->next)
1137 log_palette->palPalEntry[i] = list->entry;
1139 new_palette = CreatePalette (log_palette);
1141 enter_crit ();
1143 if (FRAME_W32_DISPLAY_INFO (f)->palette)
1144 DeleteObject (FRAME_W32_DISPLAY_INFO (f)->palette);
1145 FRAME_W32_DISPLAY_INFO (f)->palette = new_palette;
1147 /* Realize display palette and garbage all frames. */
1148 release_frame_dc (f, get_frame_dc (f));
1150 leave_crit ();
1153 #define W32_COLOR(pe) RGB (pe.peRed, pe.peGreen, pe.peBlue)
1154 #define SET_W32_COLOR(pe, color) \
1155 do \
1157 pe.peRed = GetRValue (color); \
1158 pe.peGreen = GetGValue (color); \
1159 pe.peBlue = GetBValue (color); \
1160 pe.peFlags = 0; \
1161 } while (0)
1163 #if 0
1164 /* Keep these around in case we ever want to track color usage. */
1165 void
1166 w32_map_color (FRAME_PTR f, COLORREF color)
1168 struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
1170 if (NILP (Vw32_enable_palette))
1171 return;
1173 /* check if color is already mapped */
1174 while (list)
1176 if (W32_COLOR (list->entry) == color)
1178 ++list->refcount;
1179 return;
1181 list = list->next;
1184 /* not already mapped, so add to list and recreate Windows palette */
1185 list = (struct w32_palette_entry *)
1186 xmalloc (sizeof (struct w32_palette_entry));
1187 SET_W32_COLOR (list->entry, color);
1188 list->refcount = 1;
1189 list->next = FRAME_W32_DISPLAY_INFO (f)->color_list;
1190 FRAME_W32_DISPLAY_INFO (f)->color_list = list;
1191 FRAME_W32_DISPLAY_INFO (f)->num_colors++;
1193 /* set flag that palette must be regenerated */
1194 FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
1197 void
1198 w32_unmap_color (FRAME_PTR f, COLORREF color)
1200 struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
1201 struct w32_palette_entry **prev = &FRAME_W32_DISPLAY_INFO (f)->color_list;
1203 if (NILP (Vw32_enable_palette))
1204 return;
1206 /* check if color is already mapped */
1207 while (list)
1209 if (W32_COLOR (list->entry) == color)
1211 if (--list->refcount == 0)
1213 *prev = list->next;
1214 xfree (list);
1215 FRAME_W32_DISPLAY_INFO (f)->num_colors--;
1216 break;
1218 else
1219 return;
1221 prev = &list->next;
1222 list = list->next;
1225 /* set flag that palette must be regenerated */
1226 FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
1228 #endif
1231 /* Gamma-correct COLOR on frame F. */
1233 void
1234 gamma_correct (f, color)
1235 struct frame *f;
1236 COLORREF *color;
1238 if (f->gamma)
1240 *color = PALETTERGB (
1241 pow (GetRValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
1242 pow (GetGValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
1243 pow (GetBValue (*color) / 255.0, f->gamma) * 255.0 + 0.5);
1248 /* Decide if color named COLOR is valid for the display associated with
1249 the selected frame; if so, return the rgb values in COLOR_DEF.
1250 If ALLOC is nonzero, allocate a new colormap cell. */
1253 w32_defined_color (f, color, color_def, alloc)
1254 FRAME_PTR f;
1255 char *color;
1256 XColor *color_def;
1257 int alloc;
1259 register Lisp_Object tem;
1260 COLORREF w32_color_ref;
1262 tem = x_to_w32_color (color);
1264 if (!NILP (tem))
1266 if (f)
1268 /* Apply gamma correction. */
1269 w32_color_ref = XUINT (tem);
1270 gamma_correct (f, &w32_color_ref);
1271 XSETINT (tem, w32_color_ref);
1274 /* Map this color to the palette if it is enabled. */
1275 if (!NILP (Vw32_enable_palette))
1277 struct w32_palette_entry * entry =
1278 one_w32_display_info.color_list;
1279 struct w32_palette_entry ** prev =
1280 &one_w32_display_info.color_list;
1282 /* check if color is already mapped */
1283 while (entry)
1285 if (W32_COLOR (entry->entry) == XUINT (tem))
1286 break;
1287 prev = &entry->next;
1288 entry = entry->next;
1291 if (entry == NULL && alloc)
1293 /* not already mapped, so add to list */
1294 entry = (struct w32_palette_entry *)
1295 xmalloc (sizeof (struct w32_palette_entry));
1296 SET_W32_COLOR (entry->entry, XUINT (tem));
1297 entry->next = NULL;
1298 *prev = entry;
1299 one_w32_display_info.num_colors++;
1301 /* set flag that palette must be regenerated */
1302 one_w32_display_info.regen_palette = TRUE;
1305 /* Ensure COLORREF value is snapped to nearest color in (default)
1306 palette by simulating the PALETTERGB macro. This works whether
1307 or not the display device has a palette. */
1308 w32_color_ref = XUINT (tem) | 0x2000000;
1310 color_def->pixel = w32_color_ref;
1311 color_def->red = GetRValue (w32_color_ref) * 256;
1312 color_def->green = GetGValue (w32_color_ref) * 256;
1313 color_def->blue = GetBValue (w32_color_ref) * 256;
1315 return 1;
1317 else
1319 return 0;
1323 /* Given a string ARG naming a color, compute a pixel value from it
1324 suitable for screen F.
1325 If F is not a color screen, return DEF (default) regardless of what
1326 ARG says. */
1329 x_decode_color (f, arg, def)
1330 FRAME_PTR f;
1331 Lisp_Object arg;
1332 int def;
1334 XColor cdef;
1336 CHECK_STRING (arg);
1338 if (strcmp (SDATA (arg), "black") == 0)
1339 return BLACK_PIX_DEFAULT (f);
1340 else if (strcmp (SDATA (arg), "white") == 0)
1341 return WHITE_PIX_DEFAULT (f);
1343 if ((FRAME_W32_DISPLAY_INFO (f)->n_planes * FRAME_W32_DISPLAY_INFO (f)->n_cbits) == 1)
1344 return def;
1346 /* w32_defined_color is responsible for coping with failures
1347 by looking for a near-miss. */
1348 if (w32_defined_color (f, SDATA (arg), &cdef, 1))
1349 return cdef.pixel;
1351 /* defined_color failed; return an ultimate default. */
1352 return def;
1357 /* Functions called only from `x_set_frame_param'
1358 to set individual parameters.
1360 If FRAME_W32_WINDOW (f) is 0,
1361 the frame is being created and its window does not exist yet.
1362 In that case, just record the parameter's new value
1363 in the standard place; do not attempt to change the window. */
1365 void
1366 x_set_foreground_color (f, arg, oldval)
1367 struct frame *f;
1368 Lisp_Object arg, oldval;
1370 struct w32_output *x = f->output_data.w32;
1371 PIX_TYPE fg, old_fg;
1373 fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1374 old_fg = FRAME_FOREGROUND_PIXEL (f);
1375 FRAME_FOREGROUND_PIXEL (f) = fg;
1377 if (FRAME_W32_WINDOW (f) != 0)
1379 if (x->cursor_pixel == old_fg)
1380 x->cursor_pixel = fg;
1382 update_face_from_frame_parameter (f, Qforeground_color, arg);
1383 if (FRAME_VISIBLE_P (f))
1384 redraw_frame (f);
1388 void
1389 x_set_background_color (f, arg, oldval)
1390 struct frame *f;
1391 Lisp_Object arg, oldval;
1393 FRAME_BACKGROUND_PIXEL (f)
1394 = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
1396 if (FRAME_W32_WINDOW (f) != 0)
1398 SetWindowLong (FRAME_W32_WINDOW (f), WND_BACKGROUND_INDEX,
1399 FRAME_BACKGROUND_PIXEL (f));
1401 update_face_from_frame_parameter (f, Qbackground_color, arg);
1403 if (FRAME_VISIBLE_P (f))
1404 redraw_frame (f);
1408 void
1409 x_set_mouse_color (f, arg, oldval)
1410 struct frame *f;
1411 Lisp_Object arg, oldval;
1413 Cursor cursor, nontext_cursor, mode_cursor, hand_cursor;
1414 int count;
1415 int mask_color;
1417 if (!EQ (Qnil, arg))
1418 f->output_data.w32->mouse_pixel
1419 = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1420 mask_color = FRAME_BACKGROUND_PIXEL (f);
1422 /* Don't let pointers be invisible. */
1423 if (mask_color == f->output_data.w32->mouse_pixel
1424 && mask_color == FRAME_BACKGROUND_PIXEL (f))
1425 f->output_data.w32->mouse_pixel = FRAME_FOREGROUND_PIXEL (f);
1427 #if 0 /* TODO : cursor changes */
1428 BLOCK_INPUT;
1430 /* It's not okay to crash if the user selects a screwy cursor. */
1431 count = x_catch_errors (FRAME_W32_DISPLAY (f));
1433 if (!EQ (Qnil, Vx_pointer_shape))
1435 CHECK_NUMBER (Vx_pointer_shape);
1436 cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XINT (Vx_pointer_shape));
1438 else
1439 cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
1440 x_check_errors (FRAME_W32_DISPLAY (f), "bad text pointer cursor: %s");
1442 if (!EQ (Qnil, Vx_nontext_pointer_shape))
1444 CHECK_NUMBER (Vx_nontext_pointer_shape);
1445 nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1446 XINT (Vx_nontext_pointer_shape));
1448 else
1449 nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_left_ptr);
1450 x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
1452 if (!EQ (Qnil, Vx_hourglass_pointer_shape))
1454 CHECK_NUMBER (Vx_hourglass_pointer_shape);
1455 hourglass_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1456 XINT (Vx_hourglass_pointer_shape));
1458 else
1459 hourglass_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_watch);
1460 x_check_errors (FRAME_W32_DISPLAY (f), "bad busy pointer cursor: %s");
1462 x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
1463 if (!EQ (Qnil, Vx_mode_pointer_shape))
1465 CHECK_NUMBER (Vx_mode_pointer_shape);
1466 mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1467 XINT (Vx_mode_pointer_shape));
1469 else
1470 mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
1471 x_check_errors (FRAME_W32_DISPLAY (f), "bad modeline pointer cursor: %s");
1473 if (!EQ (Qnil, Vx_sensitive_text_pointer_shape))
1475 CHECK_NUMBER (Vx_sensitive_text_pointer_shape);
1476 hand_cursor
1477 = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1478 XINT (Vx_sensitive_text_pointer_shape));
1480 else
1481 hand_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_crosshair);
1483 if (!NILP (Vx_window_horizontal_drag_shape))
1485 CHECK_NUMBER (Vx_window_horizontal_drag_shape);
1486 horizontal_drag_cursor
1487 = XCreateFontCursor (FRAME_X_DISPLAY (f),
1488 XINT (Vx_window_horizontal_drag_shape));
1490 else
1491 horizontal_drag_cursor
1492 = XCreateFontCursor (FRAME_X_DISPLAY (f), XC_sb_h_double_arrow);
1494 /* Check and report errors with the above calls. */
1495 x_check_errors (FRAME_W32_DISPLAY (f), "can't set cursor shape: %s");
1496 x_uncatch_errors (FRAME_W32_DISPLAY (f), count);
1499 XColor fore_color, back_color;
1501 fore_color.pixel = f->output_data.w32->mouse_pixel;
1502 back_color.pixel = mask_color;
1503 XQueryColor (FRAME_W32_DISPLAY (f),
1504 DefaultColormap (FRAME_W32_DISPLAY (f),
1505 DefaultScreen (FRAME_W32_DISPLAY (f))),
1506 &fore_color);
1507 XQueryColor (FRAME_W32_DISPLAY (f),
1508 DefaultColormap (FRAME_W32_DISPLAY (f),
1509 DefaultScreen (FRAME_W32_DISPLAY (f))),
1510 &back_color);
1511 XRecolorCursor (FRAME_W32_DISPLAY (f), cursor,
1512 &fore_color, &back_color);
1513 XRecolorCursor (FRAME_W32_DISPLAY (f), nontext_cursor,
1514 &fore_color, &back_color);
1515 XRecolorCursor (FRAME_W32_DISPLAY (f), mode_cursor,
1516 &fore_color, &back_color);
1517 XRecolorCursor (FRAME_W32_DISPLAY (f), hand_cursor,
1518 &fore_color, &back_color);
1519 XRecolorCursor (FRAME_W32_DISPLAY (f), hourglass_cursor,
1520 &fore_color, &back_color);
1523 if (FRAME_W32_WINDOW (f) != 0)
1524 XDefineCursor (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), cursor);
1526 if (cursor != f->output_data.w32->text_cursor && f->output_data.w32->text_cursor != 0)
1527 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->text_cursor);
1528 f->output_data.w32->text_cursor = cursor;
1530 if (nontext_cursor != f->output_data.w32->nontext_cursor
1531 && f->output_data.w32->nontext_cursor != 0)
1532 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->nontext_cursor);
1533 f->output_data.w32->nontext_cursor = nontext_cursor;
1535 if (hourglass_cursor != f->output_data.w32->hourglass_cursor
1536 && f->output_data.w32->hourglass_cursor != 0)
1537 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->hourglass_cursor);
1538 f->output_data.w32->hourglass_cursor = hourglass_cursor;
1540 if (mode_cursor != f->output_data.w32->modeline_cursor
1541 && f->output_data.w32->modeline_cursor != 0)
1542 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->modeline_cursor);
1543 f->output_data.w32->modeline_cursor = mode_cursor;
1545 if (hand_cursor != f->output_data.w32->hand_cursor
1546 && f->output_data.w32->hand_cursor != 0)
1547 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->hand_cursor);
1548 f->output_data.w32->hand_cursor = hand_cursor;
1550 XFlush (FRAME_W32_DISPLAY (f));
1551 UNBLOCK_INPUT;
1553 update_face_from_frame_parameter (f, Qmouse_color, arg);
1554 #endif /* TODO */
1557 /* Defined in w32term.c. */
1558 void
1559 x_set_cursor_color (f, arg, oldval)
1560 struct frame *f;
1561 Lisp_Object arg, oldval;
1563 unsigned long fore_pixel, pixel;
1565 if (!NILP (Vx_cursor_fore_pixel))
1566 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
1567 WHITE_PIX_DEFAULT (f));
1568 else
1569 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
1571 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1573 /* Make sure that the cursor color differs from the background color. */
1574 if (pixel == FRAME_BACKGROUND_PIXEL (f))
1576 pixel = f->output_data.w32->mouse_pixel;
1577 if (pixel == fore_pixel)
1578 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
1581 f->output_data.w32->cursor_foreground_pixel = fore_pixel;
1582 f->output_data.w32->cursor_pixel = pixel;
1584 if (FRAME_W32_WINDOW (f) != 0)
1586 BLOCK_INPUT;
1587 /* Update frame's cursor_gc. */
1588 f->output_data.w32->cursor_gc->foreground = fore_pixel;
1589 f->output_data.w32->cursor_gc->background = pixel;
1591 UNBLOCK_INPUT;
1593 if (FRAME_VISIBLE_P (f))
1595 x_update_cursor (f, 0);
1596 x_update_cursor (f, 1);
1600 update_face_from_frame_parameter (f, Qcursor_color, arg);
1603 /* Set the border-color of frame F to pixel value PIX.
1604 Note that this does not fully take effect if done before
1605 F has a window. */
1607 void
1608 x_set_border_pixel (f, pix)
1609 struct frame *f;
1610 int pix;
1613 f->output_data.w32->border_pixel = pix;
1615 if (FRAME_W32_WINDOW (f) != 0 && f->border_width > 0)
1617 if (FRAME_VISIBLE_P (f))
1618 redraw_frame (f);
1622 /* Set the border-color of frame F to value described by ARG.
1623 ARG can be a string naming a color.
1624 The border-color is used for the border that is drawn by the server.
1625 Note that this does not fully take effect if done before
1626 F has a window; it must be redone when the window is created. */
1628 void
1629 x_set_border_color (f, arg, oldval)
1630 struct frame *f;
1631 Lisp_Object arg, oldval;
1633 int pix;
1635 CHECK_STRING (arg);
1636 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1637 x_set_border_pixel (f, pix);
1638 update_face_from_frame_parameter (f, Qborder_color, arg);
1642 void
1643 x_set_cursor_type (f, arg, oldval)
1644 FRAME_PTR f;
1645 Lisp_Object arg, oldval;
1647 set_frame_cursor_types (f, arg);
1649 /* Make sure the cursor gets redrawn. */
1650 cursor_type_changed = 1;
1653 void
1654 x_set_icon_type (f, arg, oldval)
1655 struct frame *f;
1656 Lisp_Object arg, oldval;
1658 int result;
1660 if (NILP (arg) && NILP (oldval))
1661 return;
1663 if (STRINGP (arg) && STRINGP (oldval)
1664 && EQ (Fstring_equal (oldval, arg), Qt))
1665 return;
1667 if (SYMBOLP (arg) && SYMBOLP (oldval) && EQ (arg, oldval))
1668 return;
1670 BLOCK_INPUT;
1672 result = x_bitmap_icon (f, arg);
1673 if (result)
1675 UNBLOCK_INPUT;
1676 error ("No icon window available");
1679 UNBLOCK_INPUT;
1682 void
1683 x_set_icon_name (f, arg, oldval)
1684 struct frame *f;
1685 Lisp_Object arg, oldval;
1687 if (STRINGP (arg))
1689 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1690 return;
1692 else if (!NILP (arg) || NILP (oldval))
1693 return;
1695 f->icon_name = arg;
1697 #if 0
1698 if (f->output_data.w32->icon_bitmap != 0)
1699 return;
1701 BLOCK_INPUT;
1703 result = x_text_icon (f,
1704 (char *) SDATA ((!NILP (f->icon_name)
1705 ? f->icon_name
1706 : !NILP (f->title)
1707 ? f->title
1708 : f->name)));
1710 if (result)
1712 UNBLOCK_INPUT;
1713 error ("No icon window available");
1716 /* If the window was unmapped (and its icon was mapped),
1717 the new icon is not mapped, so map the window in its stead. */
1718 if (FRAME_VISIBLE_P (f))
1720 #ifdef USE_X_TOOLKIT
1721 XtPopup (f->output_data.w32->widget, XtGrabNone);
1722 #endif
1723 XMapWindow (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f));
1726 XFlush (FRAME_W32_DISPLAY (f));
1727 UNBLOCK_INPUT;
1728 #endif
1732 void
1733 x_set_menu_bar_lines (f, value, oldval)
1734 struct frame *f;
1735 Lisp_Object value, oldval;
1737 int nlines;
1738 int olines = FRAME_MENU_BAR_LINES (f);
1740 /* Right now, menu bars don't work properly in minibuf-only frames;
1741 most of the commands try to apply themselves to the minibuffer
1742 frame itself, and get an error because you can't switch buffers
1743 in or split the minibuffer window. */
1744 if (FRAME_MINIBUF_ONLY_P (f))
1745 return;
1747 if (INTEGERP (value))
1748 nlines = XINT (value);
1749 else
1750 nlines = 0;
1752 FRAME_MENU_BAR_LINES (f) = 0;
1753 if (nlines)
1754 FRAME_EXTERNAL_MENU_BAR (f) = 1;
1755 else
1757 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
1758 free_frame_menubar (f);
1759 FRAME_EXTERNAL_MENU_BAR (f) = 0;
1761 /* Adjust the frame size so that the client (text) dimensions
1762 remain the same. This depends on FRAME_EXTERNAL_MENU_BAR being
1763 set correctly. */
1764 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
1765 do_pending_window_change (0);
1767 adjust_glyphs (f);
1771 /* Set the number of lines used for the tool bar of frame F to VALUE.
1772 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
1773 is the old number of tool bar lines. This function changes the
1774 height of all windows on frame F to match the new tool bar height.
1775 The frame's height doesn't change. */
1777 void
1778 x_set_tool_bar_lines (f, value, oldval)
1779 struct frame *f;
1780 Lisp_Object value, oldval;
1782 int delta, nlines, root_height;
1783 Lisp_Object root_window;
1785 /* Treat tool bars like menu bars. */
1786 if (FRAME_MINIBUF_ONLY_P (f))
1787 return;
1789 /* Use VALUE only if an integer >= 0. */
1790 if (INTEGERP (value) && XINT (value) >= 0)
1791 nlines = XFASTINT (value);
1792 else
1793 nlines = 0;
1795 /* Make sure we redisplay all windows in this frame. */
1796 ++windows_or_buffers_changed;
1798 delta = nlines - FRAME_TOOL_BAR_LINES (f);
1800 /* Don't resize the tool-bar to more than we have room for. */
1801 root_window = FRAME_ROOT_WINDOW (f);
1802 root_height = WINDOW_TOTAL_LINES (XWINDOW (root_window));
1803 if (root_height - delta < 1)
1805 delta = root_height - 1;
1806 nlines = FRAME_TOOL_BAR_LINES (f) + delta;
1809 FRAME_TOOL_BAR_LINES (f) = nlines;
1810 change_window_heights (root_window, delta);
1811 adjust_glyphs (f);
1813 /* We also have to make sure that the internal border at the top of
1814 the frame, below the menu bar or tool bar, is redrawn when the
1815 tool bar disappears. This is so because the internal border is
1816 below the tool bar if one is displayed, but is below the menu bar
1817 if there isn't a tool bar. The tool bar draws into the area
1818 below the menu bar. */
1819 if (FRAME_W32_WINDOW (f) && FRAME_TOOL_BAR_LINES (f) == 0)
1821 clear_frame (f);
1822 clear_current_matrices (f);
1825 /* If the tool bar gets smaller, the internal border below it
1826 has to be cleared. It was formerly part of the display
1827 of the larger tool bar, and updating windows won't clear it. */
1828 if (delta < 0)
1830 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
1831 int width = FRAME_PIXEL_WIDTH (f);
1832 int y = nlines * FRAME_LINE_HEIGHT (f);
1834 BLOCK_INPUT;
1836 HDC hdc = get_frame_dc (f);
1837 w32_clear_area (f, hdc, 0, y, width, height);
1838 release_frame_dc (f, hdc);
1840 UNBLOCK_INPUT;
1842 if (WINDOWP (f->tool_bar_window))
1843 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
1848 /* Change the name of frame F to NAME. If NAME is nil, set F's name to
1849 w32_id_name.
1851 If EXPLICIT is non-zero, that indicates that lisp code is setting the
1852 name; if NAME is a string, set F's name to NAME and set
1853 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1855 If EXPLICIT is zero, that indicates that Emacs redisplay code is
1856 suggesting a new name, which lisp code should override; if
1857 F->explicit_name is set, ignore the new name; otherwise, set it. */
1859 void
1860 x_set_name (f, name, explicit)
1861 struct frame *f;
1862 Lisp_Object name;
1863 int explicit;
1865 /* Make sure that requests from lisp code override requests from
1866 Emacs redisplay code. */
1867 if (explicit)
1869 /* If we're switching from explicit to implicit, we had better
1870 update the mode lines and thereby update the title. */
1871 if (f->explicit_name && NILP (name))
1872 update_mode_lines = 1;
1874 f->explicit_name = ! NILP (name);
1876 else if (f->explicit_name)
1877 return;
1879 /* If NAME is nil, set the name to the w32_id_name. */
1880 if (NILP (name))
1882 /* Check for no change needed in this very common case
1883 before we do any consing. */
1884 if (!strcmp (FRAME_W32_DISPLAY_INFO (f)->w32_id_name,
1885 SDATA (f->name)))
1886 return;
1887 name = build_string (FRAME_W32_DISPLAY_INFO (f)->w32_id_name);
1889 else
1890 CHECK_STRING (name);
1892 /* Don't change the name if it's already NAME. */
1893 if (! NILP (Fstring_equal (name, f->name)))
1894 return;
1896 f->name = name;
1898 /* For setting the frame title, the title parameter should override
1899 the name parameter. */
1900 if (! NILP (f->title))
1901 name = f->title;
1903 if (FRAME_W32_WINDOW (f))
1905 if (STRING_MULTIBYTE (name))
1906 name = ENCODE_SYSTEM (name);
1908 BLOCK_INPUT;
1909 SetWindowText(FRAME_W32_WINDOW (f), SDATA (name));
1910 UNBLOCK_INPUT;
1914 /* This function should be called when the user's lisp code has
1915 specified a name for the frame; the name will override any set by the
1916 redisplay code. */
1917 void
1918 x_explicitly_set_name (f, arg, oldval)
1919 FRAME_PTR f;
1920 Lisp_Object arg, oldval;
1922 x_set_name (f, arg, 1);
1925 /* This function should be called by Emacs redisplay code to set the
1926 name; names set this way will never override names set by the user's
1927 lisp code. */
1928 void
1929 x_implicitly_set_name (f, arg, oldval)
1930 FRAME_PTR f;
1931 Lisp_Object arg, oldval;
1933 x_set_name (f, arg, 0);
1936 /* Change the title of frame F to NAME.
1937 If NAME is nil, use the frame name as the title.
1939 If EXPLICIT is non-zero, that indicates that lisp code is setting the
1940 name; if NAME is a string, set F's name to NAME and set
1941 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1943 If EXPLICIT is zero, that indicates that Emacs redisplay code is
1944 suggesting a new name, which lisp code should override; if
1945 F->explicit_name is set, ignore the new name; otherwise, set it. */
1947 void
1948 x_set_title (f, name, old_name)
1949 struct frame *f;
1950 Lisp_Object name, old_name;
1952 /* Don't change the title if it's already NAME. */
1953 if (EQ (name, f->title))
1954 return;
1956 update_mode_lines = 1;
1958 f->title = name;
1960 if (NILP (name))
1961 name = f->name;
1963 if (FRAME_W32_WINDOW (f))
1965 if (STRING_MULTIBYTE (name))
1966 name = ENCODE_SYSTEM (name);
1968 BLOCK_INPUT;
1969 SetWindowText(FRAME_W32_WINDOW (f), SDATA (name));
1970 UNBLOCK_INPUT;
1975 void x_set_scroll_bar_default_width (f)
1976 struct frame *f;
1978 int wid = FRAME_COLUMN_WIDTH (f);
1980 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = GetSystemMetrics (SM_CXVSCROLL);
1981 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) +
1982 wid - 1) / wid;
1986 /* Subroutines of creating a frame. */
1989 /* Return the value of parameter PARAM.
1991 First search ALIST, then Vdefault_frame_alist, then the X defaults
1992 database, using ATTRIBUTE as the attribute name and CLASS as its class.
1994 Convert the resource to the type specified by desired_type.
1996 If no default is specified, return Qunbound. If you call
1997 w32_get_arg, make sure you deal with Qunbound in a reasonable way,
1998 and don't let it get stored in any Lisp-visible variables! */
2000 static Lisp_Object
2001 w32_get_arg (alist, param, attribute, class, type)
2002 Lisp_Object alist, param;
2003 char *attribute;
2004 char *class;
2005 enum resource_types type;
2007 return x_get_arg (check_x_display_info (Qnil),
2008 alist, param, attribute, class, type);
2012 Cursor
2013 w32_load_cursor (LPCTSTR name)
2015 /* Try first to load cursor from application resource. */
2016 Cursor cursor = LoadImage ((HINSTANCE) GetModuleHandle(NULL),
2017 name, IMAGE_CURSOR, 0, 0,
2018 LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_SHARED);
2019 if (!cursor)
2021 /* Then try to load a shared predefined cursor. */
2022 cursor = LoadImage (NULL, name, IMAGE_CURSOR, 0, 0,
2023 LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_SHARED);
2025 return cursor;
2028 extern LRESULT CALLBACK w32_wnd_proc ();
2030 BOOL
2031 w32_init_class (hinst)
2032 HINSTANCE hinst;
2034 WNDCLASS wc;
2036 wc.style = CS_HREDRAW | CS_VREDRAW;
2037 wc.lpfnWndProc = (WNDPROC) w32_wnd_proc;
2038 wc.cbClsExtra = 0;
2039 wc.cbWndExtra = WND_EXTRA_BYTES;
2040 wc.hInstance = hinst;
2041 wc.hIcon = LoadIcon (hinst, EMACS_CLASS);
2042 wc.hCursor = w32_load_cursor (IDC_ARROW);
2043 wc.hbrBackground = NULL; /* GetStockObject (WHITE_BRUSH); */
2044 wc.lpszMenuName = NULL;
2045 wc.lpszClassName = EMACS_CLASS;
2047 return (RegisterClass (&wc));
2050 HWND
2051 w32_createscrollbar (f, bar)
2052 struct frame *f;
2053 struct scroll_bar * bar;
2055 return (CreateWindow ("SCROLLBAR", "", SBS_VERT | WS_CHILD | WS_VISIBLE,
2056 /* Position and size of scroll bar. */
2057 XINT(bar->left) + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
2058 XINT(bar->top),
2059 XINT(bar->width) - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
2060 XINT(bar->height),
2061 FRAME_W32_WINDOW (f),
2062 NULL,
2063 hinst,
2064 NULL));
2067 void
2068 w32_createwindow (f)
2069 struct frame *f;
2071 HWND hwnd;
2072 RECT rect;
2073 Lisp_Object top = Qunbound;
2074 Lisp_Object left = Qunbound;
2076 rect.left = rect.top = 0;
2077 rect.right = FRAME_PIXEL_WIDTH (f);
2078 rect.bottom = FRAME_PIXEL_HEIGHT (f);
2080 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
2081 FRAME_EXTERNAL_MENU_BAR (f));
2083 /* Do first time app init */
2085 if (!hprevinst)
2087 w32_init_class (hinst);
2090 if (f->size_hint_flags & USPosition || f->size_hint_flags & PPosition)
2092 XSETINT (left, f->left_pos);
2093 XSETINT (top, f->top_pos);
2095 else if (EQ (left, Qunbound) && EQ (top, Qunbound))
2097 /* When called with RES_TYPE_NUMBER, w32_get_arg will return zero
2098 for anything that is not a number and is not Qunbound. */
2099 left = w32_get_arg (Qnil, Qleft, "left", "Left", RES_TYPE_NUMBER);
2100 top = w32_get_arg (Qnil, Qtop, "top", "Top", RES_TYPE_NUMBER);
2103 FRAME_W32_WINDOW (f) = hwnd
2104 = CreateWindow (EMACS_CLASS,
2105 f->namebuf,
2106 f->output_data.w32->dwStyle | WS_CLIPCHILDREN,
2107 EQ (left, Qunbound) ? CW_USEDEFAULT : XINT (left),
2108 EQ (top, Qunbound) ? CW_USEDEFAULT : XINT (top),
2109 rect.right - rect.left,
2110 rect.bottom - rect.top,
2111 NULL,
2112 NULL,
2113 hinst,
2114 NULL);
2116 if (hwnd)
2118 SetWindowLong (hwnd, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
2119 SetWindowLong (hwnd, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
2120 SetWindowLong (hwnd, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
2121 SetWindowLong (hwnd, WND_SCROLLBAR_INDEX, f->scroll_bar_actual_width);
2122 SetWindowLong (hwnd, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
2124 /* Enable drag-n-drop. */
2125 DragAcceptFiles (hwnd, TRUE);
2127 /* Do this to discard the default setting specified by our parent. */
2128 ShowWindow (hwnd, SW_HIDE);
2130 /* Update frame positions. */
2131 GetWindowRect (hwnd, &rect);
2132 f->left_pos = rect.left;
2133 f->top_pos = rect.top;
2137 void
2138 my_post_msg (wmsg, hwnd, msg, wParam, lParam)
2139 W32Msg * wmsg;
2140 HWND hwnd;
2141 UINT msg;
2142 WPARAM wParam;
2143 LPARAM lParam;
2145 wmsg->msg.hwnd = hwnd;
2146 wmsg->msg.message = msg;
2147 wmsg->msg.wParam = wParam;
2148 wmsg->msg.lParam = lParam;
2149 wmsg->msg.time = GetMessageTime ();
2151 post_msg (wmsg);
2154 /* GetKeyState and MapVirtualKey on Windows 95 do not actually distinguish
2155 between left and right keys as advertised. We test for this
2156 support dynamically, and set a flag when the support is absent. If
2157 absent, we keep track of the left and right control and alt keys
2158 ourselves. This is particularly necessary on keyboards that rely
2159 upon the AltGr key, which is represented as having the left control
2160 and right alt keys pressed. For these keyboards, we need to know
2161 when the left alt key has been pressed in addition to the AltGr key
2162 so that we can properly support M-AltGr-key sequences (such as M-@
2163 on Swedish keyboards). */
2165 #define EMACS_LCONTROL 0
2166 #define EMACS_RCONTROL 1
2167 #define EMACS_LMENU 2
2168 #define EMACS_RMENU 3
2170 static int modifiers[4];
2171 static int modifiers_recorded;
2172 static int modifier_key_support_tested;
2174 static void
2175 test_modifier_support (unsigned int wparam)
2177 unsigned int l, r;
2179 if (wparam != VK_CONTROL && wparam != VK_MENU)
2180 return;
2181 if (wparam == VK_CONTROL)
2183 l = VK_LCONTROL;
2184 r = VK_RCONTROL;
2186 else
2188 l = VK_LMENU;
2189 r = VK_RMENU;
2191 if (!(GetKeyState (l) & 0x8000) && !(GetKeyState (r) & 0x8000))
2192 modifiers_recorded = 1;
2193 else
2194 modifiers_recorded = 0;
2195 modifier_key_support_tested = 1;
2198 static void
2199 record_keydown (unsigned int wparam, unsigned int lparam)
2201 int i;
2203 if (!modifier_key_support_tested)
2204 test_modifier_support (wparam);
2206 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
2207 return;
2209 if (wparam == VK_CONTROL)
2210 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
2211 else
2212 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
2214 modifiers[i] = 1;
2217 static void
2218 record_keyup (unsigned int wparam, unsigned int lparam)
2220 int i;
2222 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
2223 return;
2225 if (wparam == VK_CONTROL)
2226 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
2227 else
2228 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
2230 modifiers[i] = 0;
2233 /* Emacs can lose focus while a modifier key has been pressed. When
2234 it regains focus, be conservative and clear all modifiers since
2235 we cannot reconstruct the left and right modifier state. */
2236 static void
2237 reset_modifiers ()
2239 SHORT ctrl, alt;
2241 if (GetFocus () == NULL)
2242 /* Emacs doesn't have keyboard focus. Do nothing. */
2243 return;
2245 ctrl = GetAsyncKeyState (VK_CONTROL);
2246 alt = GetAsyncKeyState (VK_MENU);
2248 if (!(ctrl & 0x08000))
2249 /* Clear any recorded control modifier state. */
2250 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
2252 if (!(alt & 0x08000))
2253 /* Clear any recorded alt modifier state. */
2254 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
2256 /* Update the state of all modifier keys, because modifiers used in
2257 hot-key combinations can get stuck on if Emacs loses focus as a
2258 result of a hot-key being pressed. */
2260 BYTE keystate[256];
2262 #define CURRENT_STATE(key) ((GetAsyncKeyState (key) & 0x8000) >> 8)
2264 GetKeyboardState (keystate);
2265 keystate[VK_SHIFT] = CURRENT_STATE (VK_SHIFT);
2266 keystate[VK_CONTROL] = CURRENT_STATE (VK_CONTROL);
2267 keystate[VK_LCONTROL] = CURRENT_STATE (VK_LCONTROL);
2268 keystate[VK_RCONTROL] = CURRENT_STATE (VK_RCONTROL);
2269 keystate[VK_MENU] = CURRENT_STATE (VK_MENU);
2270 keystate[VK_LMENU] = CURRENT_STATE (VK_LMENU);
2271 keystate[VK_RMENU] = CURRENT_STATE (VK_RMENU);
2272 keystate[VK_LWIN] = CURRENT_STATE (VK_LWIN);
2273 keystate[VK_RWIN] = CURRENT_STATE (VK_RWIN);
2274 keystate[VK_APPS] = CURRENT_STATE (VK_APPS);
2275 SetKeyboardState (keystate);
2279 /* Synchronize modifier state with what is reported with the current
2280 keystroke. Even if we cannot distinguish between left and right
2281 modifier keys, we know that, if no modifiers are set, then neither
2282 the left or right modifier should be set. */
2283 static void
2284 sync_modifiers ()
2286 if (!modifiers_recorded)
2287 return;
2289 if (!(GetKeyState (VK_CONTROL) & 0x8000))
2290 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
2292 if (!(GetKeyState (VK_MENU) & 0x8000))
2293 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
2296 static int
2297 modifier_set (int vkey)
2299 if (vkey == VK_CAPITAL || vkey == VK_SCROLL)
2300 return (GetKeyState (vkey) & 0x1);
2301 if (!modifiers_recorded)
2302 return (GetKeyState (vkey) & 0x8000);
2304 switch (vkey)
2306 case VK_LCONTROL:
2307 return modifiers[EMACS_LCONTROL];
2308 case VK_RCONTROL:
2309 return modifiers[EMACS_RCONTROL];
2310 case VK_LMENU:
2311 return modifiers[EMACS_LMENU];
2312 case VK_RMENU:
2313 return modifiers[EMACS_RMENU];
2315 return (GetKeyState (vkey) & 0x8000);
2318 /* Convert between the modifier bits W32 uses and the modifier bits
2319 Emacs uses. */
2321 unsigned int
2322 w32_key_to_modifier (int key)
2324 Lisp_Object key_mapping;
2326 switch (key)
2328 case VK_LWIN:
2329 key_mapping = Vw32_lwindow_modifier;
2330 break;
2331 case VK_RWIN:
2332 key_mapping = Vw32_rwindow_modifier;
2333 break;
2334 case VK_APPS:
2335 key_mapping = Vw32_apps_modifier;
2336 break;
2337 case VK_SCROLL:
2338 key_mapping = Vw32_scroll_lock_modifier;
2339 break;
2340 default:
2341 key_mapping = Qnil;
2344 /* NB. This code runs in the input thread, asychronously to the lisp
2345 thread, so we must be careful to ensure access to lisp data is
2346 thread-safe. The following code is safe because the modifier
2347 variable values are updated atomically from lisp and symbols are
2348 not relocated by GC. Also, we don't have to worry about seeing GC
2349 markbits here. */
2350 if (EQ (key_mapping, Qhyper))
2351 return hyper_modifier;
2352 if (EQ (key_mapping, Qsuper))
2353 return super_modifier;
2354 if (EQ (key_mapping, Qmeta))
2355 return meta_modifier;
2356 if (EQ (key_mapping, Qalt))
2357 return alt_modifier;
2358 if (EQ (key_mapping, Qctrl))
2359 return ctrl_modifier;
2360 if (EQ (key_mapping, Qcontrol)) /* synonym for ctrl */
2361 return ctrl_modifier;
2362 if (EQ (key_mapping, Qshift))
2363 return shift_modifier;
2365 /* Don't generate any modifier if not explicitly requested. */
2366 return 0;
2369 unsigned int
2370 w32_get_modifiers ()
2372 return ((modifier_set (VK_SHIFT) ? shift_modifier : 0) |
2373 (modifier_set (VK_CONTROL) ? ctrl_modifier : 0) |
2374 (modifier_set (VK_LWIN) ? w32_key_to_modifier (VK_LWIN) : 0) |
2375 (modifier_set (VK_RWIN) ? w32_key_to_modifier (VK_RWIN) : 0) |
2376 (modifier_set (VK_APPS) ? w32_key_to_modifier (VK_APPS) : 0) |
2377 (modifier_set (VK_SCROLL) ? w32_key_to_modifier (VK_SCROLL) : 0) |
2378 (modifier_set (VK_MENU) ?
2379 ((NILP (Vw32_alt_is_meta)) ? alt_modifier : meta_modifier) : 0));
2382 /* We map the VK_* modifiers into console modifier constants
2383 so that we can use the same routines to handle both console
2384 and window input. */
2386 static int
2387 construct_console_modifiers ()
2389 int mods;
2391 mods = 0;
2392 mods |= (modifier_set (VK_SHIFT)) ? SHIFT_PRESSED : 0;
2393 mods |= (modifier_set (VK_CAPITAL)) ? CAPSLOCK_ON : 0;
2394 mods |= (modifier_set (VK_SCROLL)) ? SCROLLLOCK_ON : 0;
2395 mods |= (modifier_set (VK_NUMLOCK)) ? NUMLOCK_ON : 0;
2396 mods |= (modifier_set (VK_LCONTROL)) ? LEFT_CTRL_PRESSED : 0;
2397 mods |= (modifier_set (VK_RCONTROL)) ? RIGHT_CTRL_PRESSED : 0;
2398 mods |= (modifier_set (VK_LMENU)) ? LEFT_ALT_PRESSED : 0;
2399 mods |= (modifier_set (VK_RMENU)) ? RIGHT_ALT_PRESSED : 0;
2400 mods |= (modifier_set (VK_LWIN)) ? LEFT_WIN_PRESSED : 0;
2401 mods |= (modifier_set (VK_RWIN)) ? RIGHT_WIN_PRESSED : 0;
2402 mods |= (modifier_set (VK_APPS)) ? APPS_PRESSED : 0;
2404 return mods;
2407 static int
2408 w32_get_key_modifiers (unsigned int wparam, unsigned int lparam)
2410 int mods;
2412 /* Convert to emacs modifiers. */
2413 mods = w32_kbd_mods_to_emacs (construct_console_modifiers (), wparam);
2415 return mods;
2418 unsigned int
2419 map_keypad_keys (unsigned int virt_key, unsigned int extended)
2421 if (virt_key < VK_CLEAR || virt_key > VK_DELETE)
2422 return virt_key;
2424 if (virt_key == VK_RETURN)
2425 return (extended ? VK_NUMPAD_ENTER : VK_RETURN);
2427 if (virt_key >= VK_PRIOR && virt_key <= VK_DOWN)
2428 return (!extended ? (VK_NUMPAD_PRIOR + (virt_key - VK_PRIOR)) : virt_key);
2430 if (virt_key == VK_INSERT || virt_key == VK_DELETE)
2431 return (!extended ? (VK_NUMPAD_INSERT + (virt_key - VK_INSERT)) : virt_key);
2433 if (virt_key == VK_CLEAR)
2434 return (!extended ? VK_NUMPAD_CLEAR : virt_key);
2436 return virt_key;
2439 /* List of special key combinations which w32 would normally capture,
2440 but emacs should grab instead. Not directly visible to lisp, to
2441 simplify synchronization. Each item is an integer encoding a virtual
2442 key code and modifier combination to capture. */
2443 Lisp_Object w32_grabbed_keys;
2445 #define HOTKEY(vk,mods) make_number (((vk) & 255) | ((mods) << 8))
2446 #define HOTKEY_ID(k) (XFASTINT (k) & 0xbfff)
2447 #define HOTKEY_VK_CODE(k) (XFASTINT (k) & 255)
2448 #define HOTKEY_MODIFIERS(k) (XFASTINT (k) >> 8)
2450 #define RAW_HOTKEY_ID(k) ((k) & 0xbfff)
2451 #define RAW_HOTKEY_VK_CODE(k) ((k) & 255)
2452 #define RAW_HOTKEY_MODIFIERS(k) ((k) >> 8)
2454 /* Register hot-keys for reserved key combinations when Emacs has
2455 keyboard focus, since this is the only way Emacs can receive key
2456 combinations like Alt-Tab which are used by the system. */
2458 static void
2459 register_hot_keys (hwnd)
2460 HWND hwnd;
2462 Lisp_Object keylist;
2464 /* Use GC_CONSP, since we are called asynchronously. */
2465 for (keylist = w32_grabbed_keys; GC_CONSP (keylist); keylist = XCDR (keylist))
2467 Lisp_Object key = XCAR (keylist);
2469 /* Deleted entries get set to nil. */
2470 if (!INTEGERP (key))
2471 continue;
2473 RegisterHotKey (hwnd, HOTKEY_ID (key),
2474 HOTKEY_MODIFIERS (key), HOTKEY_VK_CODE (key));
2478 static void
2479 unregister_hot_keys (hwnd)
2480 HWND hwnd;
2482 Lisp_Object keylist;
2484 /* Use GC_CONSP, since we are called asynchronously. */
2485 for (keylist = w32_grabbed_keys; GC_CONSP (keylist); keylist = XCDR (keylist))
2487 Lisp_Object key = XCAR (keylist);
2489 if (!INTEGERP (key))
2490 continue;
2492 UnregisterHotKey (hwnd, HOTKEY_ID (key));
2496 /* Main message dispatch loop. */
2498 static void
2499 w32_msg_pump (deferred_msg * msg_buf)
2501 MSG msg;
2502 int result;
2503 HWND focus_window;
2505 msh_mousewheel = RegisterWindowMessage (MSH_MOUSEWHEEL);
2507 while (GetMessage (&msg, NULL, 0, 0))
2509 if (msg.hwnd == NULL)
2511 switch (msg.message)
2513 case WM_NULL:
2514 /* Produced by complete_deferred_msg; just ignore. */
2515 break;
2516 case WM_EMACS_CREATEWINDOW:
2517 /* Initialize COM for this window. Even though we don't use it,
2518 some third party shell extensions can cause it to be used in
2519 system dialogs, which causes a crash if it is not initialized.
2520 This is a known bug in Windows, which was fixed long ago, but
2521 the patch for XP is not publically available until XP SP3,
2522 and older versions will never be patched. */
2523 CoInitialize (NULL);
2524 w32_createwindow ((struct frame *) msg.wParam);
2525 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2526 abort ();
2527 break;
2528 case WM_EMACS_SETLOCALE:
2529 SetThreadLocale (msg.wParam);
2530 /* Reply is not expected. */
2531 break;
2532 case WM_EMACS_SETKEYBOARDLAYOUT:
2533 result = (int) ActivateKeyboardLayout ((HKL) msg.wParam, 0);
2534 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
2535 result, 0))
2536 abort ();
2537 break;
2538 case WM_EMACS_REGISTER_HOT_KEY:
2539 focus_window = GetFocus ();
2540 if (focus_window != NULL)
2541 RegisterHotKey (focus_window,
2542 RAW_HOTKEY_ID (msg.wParam),
2543 RAW_HOTKEY_MODIFIERS (msg.wParam),
2544 RAW_HOTKEY_VK_CODE (msg.wParam));
2545 /* Reply is not expected. */
2546 break;
2547 case WM_EMACS_UNREGISTER_HOT_KEY:
2548 focus_window = GetFocus ();
2549 if (focus_window != NULL)
2550 UnregisterHotKey (focus_window, RAW_HOTKEY_ID (msg.wParam));
2551 /* Mark item as erased. NB: this code must be
2552 thread-safe. The next line is okay because the cons
2553 cell is never made into garbage and is not relocated by
2554 GC. */
2555 XSETCAR ((Lisp_Object) ((EMACS_INT) msg.lParam), Qnil);
2556 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2557 abort ();
2558 break;
2559 case WM_EMACS_TOGGLE_LOCK_KEY:
2561 int vk_code = (int) msg.wParam;
2562 int cur_state = (GetKeyState (vk_code) & 1);
2563 Lisp_Object new_state = (Lisp_Object) ((EMACS_INT) msg.lParam);
2565 /* NB: This code must be thread-safe. It is safe to
2566 call NILP because symbols are not relocated by GC,
2567 and pointer here is not touched by GC (so the markbit
2568 can't be set). Numbers are safe because they are
2569 immediate values. */
2570 if (NILP (new_state)
2571 || (NUMBERP (new_state)
2572 && ((XUINT (new_state)) & 1) != cur_state))
2574 one_w32_display_info.faked_key = vk_code;
2576 keybd_event ((BYTE) vk_code,
2577 (BYTE) MapVirtualKey (vk_code, 0),
2578 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2579 keybd_event ((BYTE) vk_code,
2580 (BYTE) MapVirtualKey (vk_code, 0),
2581 KEYEVENTF_EXTENDEDKEY | 0, 0);
2582 keybd_event ((BYTE) vk_code,
2583 (BYTE) MapVirtualKey (vk_code, 0),
2584 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2585 cur_state = !cur_state;
2587 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
2588 cur_state, 0))
2589 abort ();
2591 break;
2592 default:
2593 DebPrint (("msg %x not expected by w32_msg_pump\n", msg.message));
2596 else
2598 DispatchMessage (&msg);
2601 /* Exit nested loop when our deferred message has completed. */
2602 if (msg_buf->completed)
2603 break;
2607 deferred_msg * deferred_msg_head;
2609 static deferred_msg *
2610 find_deferred_msg (HWND hwnd, UINT msg)
2612 deferred_msg * item;
2614 /* Don't actually need synchronization for read access, since
2615 modification of single pointer is always atomic. */
2616 /* enter_crit (); */
2618 for (item = deferred_msg_head; item != NULL; item = item->next)
2619 if (item->w32msg.msg.hwnd == hwnd
2620 && item->w32msg.msg.message == msg)
2621 break;
2623 /* leave_crit (); */
2625 return item;
2628 static LRESULT
2629 send_deferred_msg (deferred_msg * msg_buf,
2630 HWND hwnd,
2631 UINT msg,
2632 WPARAM wParam,
2633 LPARAM lParam)
2635 /* Only input thread can send deferred messages. */
2636 if (GetCurrentThreadId () != dwWindowsThreadId)
2637 abort ();
2639 /* It is an error to send a message that is already deferred. */
2640 if (find_deferred_msg (hwnd, msg) != NULL)
2641 abort ();
2643 /* Enforced synchronization is not needed because this is the only
2644 function that alters deferred_msg_head, and the following critical
2645 section is guaranteed to only be serially reentered (since only the
2646 input thread can call us). */
2648 /* enter_crit (); */
2650 msg_buf->completed = 0;
2651 msg_buf->next = deferred_msg_head;
2652 deferred_msg_head = msg_buf;
2653 my_post_msg (&msg_buf->w32msg, hwnd, msg, wParam, lParam);
2655 /* leave_crit (); */
2657 /* Start a new nested message loop to process other messages until
2658 this one is completed. */
2659 w32_msg_pump (msg_buf);
2661 deferred_msg_head = msg_buf->next;
2663 return msg_buf->result;
2666 void
2667 complete_deferred_msg (HWND hwnd, UINT msg, LRESULT result)
2669 deferred_msg * msg_buf = find_deferred_msg (hwnd, msg);
2671 if (msg_buf == NULL)
2672 /* Message may have been cancelled, so don't abort(). */
2673 return;
2675 msg_buf->result = result;
2676 msg_buf->completed = 1;
2678 /* Ensure input thread is woken so it notices the completion. */
2679 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
2682 void
2683 cancel_all_deferred_msgs ()
2685 deferred_msg * item;
2687 /* Don't actually need synchronization for read access, since
2688 modification of single pointer is always atomic. */
2689 /* enter_crit (); */
2691 for (item = deferred_msg_head; item != NULL; item = item->next)
2693 item->result = 0;
2694 item->completed = 1;
2697 /* leave_crit (); */
2699 /* Ensure input thread is woken so it notices the completion. */
2700 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
2703 DWORD WINAPI
2704 w32_msg_worker (void *arg)
2706 MSG msg;
2707 deferred_msg dummy_buf;
2709 /* Ensure our message queue is created */
2711 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
2713 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2714 abort ();
2716 memset (&dummy_buf, 0, sizeof (dummy_buf));
2717 dummy_buf.w32msg.msg.hwnd = NULL;
2718 dummy_buf.w32msg.msg.message = WM_NULL;
2720 /* This is the inital message loop which should only exit when the
2721 application quits. */
2722 w32_msg_pump (&dummy_buf);
2724 return 0;
2727 static void
2728 signal_user_input ()
2730 /* Interrupt any lisp that wants to be interrupted by input. */
2731 if (!NILP (Vthrow_on_input))
2733 Vquit_flag = Vthrow_on_input;
2734 /* If we're inside a function that wants immediate quits,
2735 do it now. */
2736 if (immediate_quit && NILP (Vinhibit_quit))
2738 immediate_quit = 0;
2739 QUIT;
2745 static void
2746 post_character_message (hwnd, msg, wParam, lParam, modifiers)
2747 HWND hwnd;
2748 UINT msg;
2749 WPARAM wParam;
2750 LPARAM lParam;
2751 DWORD modifiers;
2754 W32Msg wmsg;
2756 wmsg.dwModifiers = modifiers;
2758 /* Detect quit_char and set quit-flag directly. Note that we
2759 still need to post a message to ensure the main thread will be
2760 woken up if blocked in sys_select(), but we do NOT want to post
2761 the quit_char message itself (because it will usually be as if
2762 the user had typed quit_char twice). Instead, we post a dummy
2763 message that has no particular effect. */
2765 int c = wParam;
2766 if (isalpha (c) && wmsg.dwModifiers == ctrl_modifier)
2767 c = make_ctrl_char (c) & 0377;
2768 if (c == quit_char
2769 || (wmsg.dwModifiers == 0 &&
2770 w32_quit_key && wParam == w32_quit_key))
2772 Vquit_flag = Qt;
2774 /* The choice of message is somewhat arbitrary, as long as
2775 the main thread handler just ignores it. */
2776 msg = WM_NULL;
2778 /* Interrupt any blocking system calls. */
2779 signal_quit ();
2781 /* As a safety precaution, forcibly complete any deferred
2782 messages. This is a kludge, but I don't see any particularly
2783 clean way to handle the situation where a deferred message is
2784 "dropped" in the lisp thread, and will thus never be
2785 completed, eg. by the user trying to activate the menubar
2786 when the lisp thread is busy, and then typing C-g when the
2787 menubar doesn't open promptly (with the result that the
2788 menubar never responds at all because the deferred
2789 WM_INITMENU message is never completed). Another problem
2790 situation is when the lisp thread calls SendMessage (to send
2791 a window manager command) when a message has been deferred;
2792 the lisp thread gets blocked indefinitely waiting for the
2793 deferred message to be completed, which itself is waiting for
2794 the lisp thread to respond.
2796 Note that we don't want to block the input thread waiting for
2797 a reponse from the lisp thread (although that would at least
2798 solve the deadlock problem above), because we want to be able
2799 to receive C-g to interrupt the lisp thread. */
2800 cancel_all_deferred_msgs ();
2802 else
2803 signal_user_input ();
2806 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2809 /* Main window procedure */
2811 LRESULT CALLBACK
2812 w32_wnd_proc (hwnd, msg, wParam, lParam)
2813 HWND hwnd;
2814 UINT msg;
2815 WPARAM wParam;
2816 LPARAM lParam;
2818 struct frame *f;
2819 struct w32_display_info *dpyinfo = &one_w32_display_info;
2820 W32Msg wmsg;
2821 int windows_translate;
2822 int key;
2824 /* Note that it is okay to call x_window_to_frame, even though we are
2825 not running in the main lisp thread, because frame deletion
2826 requires the lisp thread to synchronize with this thread. Thus, if
2827 a frame struct is returned, it can be used without concern that the
2828 lisp thread might make it disappear while we are using it.
2830 NB. Walking the frame list in this thread is safe (as long as
2831 writes of Lisp_Object slots are atomic, which they are on Windows).
2832 Although delete-frame can destructively modify the frame list while
2833 we are walking it, a garbage collection cannot occur until after
2834 delete-frame has synchronized with this thread.
2836 It is also safe to use functions that make GDI calls, such as
2837 w32_clear_rect, because these functions must obtain a DC handle
2838 from the frame struct using get_frame_dc which is thread-aware. */
2840 switch (msg)
2842 case WM_ERASEBKGND:
2843 f = x_window_to_frame (dpyinfo, hwnd);
2844 if (f)
2846 HDC hdc = get_frame_dc (f);
2847 GetUpdateRect (hwnd, &wmsg.rect, FALSE);
2848 w32_clear_rect (f, hdc, &wmsg.rect);
2849 release_frame_dc (f, hdc);
2851 #if defined (W32_DEBUG_DISPLAY)
2852 DebPrint (("WM_ERASEBKGND (frame %p): erasing %d,%d-%d,%d\n",
2854 wmsg.rect.left, wmsg.rect.top,
2855 wmsg.rect.right, wmsg.rect.bottom));
2856 #endif /* W32_DEBUG_DISPLAY */
2858 return 1;
2859 case WM_PALETTECHANGED:
2860 /* ignore our own changes */
2861 if ((HWND)wParam != hwnd)
2863 f = x_window_to_frame (dpyinfo, hwnd);
2864 if (f)
2865 /* get_frame_dc will realize our palette and force all
2866 frames to be redrawn if needed. */
2867 release_frame_dc (f, get_frame_dc (f));
2869 return 0;
2870 case WM_PAINT:
2872 PAINTSTRUCT paintStruct;
2873 RECT update_rect;
2874 bzero (&update_rect, sizeof (update_rect));
2876 f = x_window_to_frame (dpyinfo, hwnd);
2877 if (f == 0)
2879 DebPrint (("WM_PAINT received for unknown window %p\n", hwnd));
2880 return 0;
2883 /* MSDN Docs say not to call BeginPaint if GetUpdateRect
2884 fails. Apparently this can happen under some
2885 circumstances. */
2886 if (GetUpdateRect (hwnd, &update_rect, FALSE) || !w32_strict_painting)
2888 enter_crit ();
2889 BeginPaint (hwnd, &paintStruct);
2891 /* The rectangles returned by GetUpdateRect and BeginPaint
2892 do not always match. Play it safe by assuming both areas
2893 are invalid. */
2894 UnionRect (&(wmsg.rect), &update_rect, &(paintStruct.rcPaint));
2896 #if defined (W32_DEBUG_DISPLAY)
2897 DebPrint (("WM_PAINT (frame %p): painting %d,%d-%d,%d\n",
2899 wmsg.rect.left, wmsg.rect.top,
2900 wmsg.rect.right, wmsg.rect.bottom));
2901 DebPrint ((" [update region is %d,%d-%d,%d]\n",
2902 update_rect.left, update_rect.top,
2903 update_rect.right, update_rect.bottom));
2904 #endif
2905 EndPaint (hwnd, &paintStruct);
2906 leave_crit ();
2908 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2910 return 0;
2913 /* If GetUpdateRect returns 0 (meaning there is no update
2914 region), assume the whole window needs to be repainted. */
2915 GetClientRect(hwnd, &wmsg.rect);
2916 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2917 return 0;
2920 case WM_INPUTLANGCHANGE:
2921 /* Inform lisp thread of keyboard layout changes. */
2922 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2924 /* Clear dead keys in the keyboard state; for simplicity only
2925 preserve modifier key states. */
2927 int i;
2928 BYTE keystate[256];
2930 GetKeyboardState (keystate);
2931 for (i = 0; i < 256; i++)
2932 if (1
2933 && i != VK_SHIFT
2934 && i != VK_LSHIFT
2935 && i != VK_RSHIFT
2936 && i != VK_CAPITAL
2937 && i != VK_NUMLOCK
2938 && i != VK_SCROLL
2939 && i != VK_CONTROL
2940 && i != VK_LCONTROL
2941 && i != VK_RCONTROL
2942 && i != VK_MENU
2943 && i != VK_LMENU
2944 && i != VK_RMENU
2945 && i != VK_LWIN
2946 && i != VK_RWIN)
2947 keystate[i] = 0;
2948 SetKeyboardState (keystate);
2950 goto dflt;
2952 case WM_HOTKEY:
2953 /* Synchronize hot keys with normal input. */
2954 PostMessage (hwnd, WM_KEYDOWN, HIWORD (lParam), 0);
2955 return (0);
2957 case WM_KEYUP:
2958 case WM_SYSKEYUP:
2959 record_keyup (wParam, lParam);
2960 goto dflt;
2962 case WM_KEYDOWN:
2963 case WM_SYSKEYDOWN:
2964 /* Ignore keystrokes we fake ourself; see below. */
2965 if (dpyinfo->faked_key == wParam)
2967 dpyinfo->faked_key = 0;
2968 /* Make sure TranslateMessage sees them though (as long as
2969 they don't produce WM_CHAR messages). This ensures that
2970 indicator lights are toggled promptly on Windows 9x, for
2971 example. */
2972 if (lispy_function_keys[wParam] != 0)
2974 windows_translate = 1;
2975 goto translate;
2977 return 0;
2980 /* Synchronize modifiers with current keystroke. */
2981 sync_modifiers ();
2982 record_keydown (wParam, lParam);
2983 wParam = map_keypad_keys (wParam, (lParam & 0x1000000L) != 0);
2985 windows_translate = 0;
2987 switch (wParam)
2989 case VK_LWIN:
2990 if (NILP (Vw32_pass_lwindow_to_system))
2992 /* Prevent system from acting on keyup (which opens the
2993 Start menu if no other key was pressed) by simulating a
2994 press of Space which we will ignore. */
2995 if (GetAsyncKeyState (wParam) & 1)
2997 if (NUMBERP (Vw32_phantom_key_code))
2998 key = XUINT (Vw32_phantom_key_code) & 255;
2999 else
3000 key = VK_SPACE;
3001 dpyinfo->faked_key = key;
3002 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
3005 if (!NILP (Vw32_lwindow_modifier))
3006 return 0;
3007 break;
3008 case VK_RWIN:
3009 if (NILP (Vw32_pass_rwindow_to_system))
3011 if (GetAsyncKeyState (wParam) & 1)
3013 if (NUMBERP (Vw32_phantom_key_code))
3014 key = XUINT (Vw32_phantom_key_code) & 255;
3015 else
3016 key = VK_SPACE;
3017 dpyinfo->faked_key = key;
3018 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
3021 if (!NILP (Vw32_rwindow_modifier))
3022 return 0;
3023 break;
3024 case VK_APPS:
3025 if (!NILP (Vw32_apps_modifier))
3026 return 0;
3027 break;
3028 case VK_MENU:
3029 if (NILP (Vw32_pass_alt_to_system))
3030 /* Prevent DefWindowProc from activating the menu bar if an
3031 Alt key is pressed and released by itself. */
3032 return 0;
3033 windows_translate = 1;
3034 break;
3035 case VK_CAPITAL:
3036 /* Decide whether to treat as modifier or function key. */
3037 if (NILP (Vw32_enable_caps_lock))
3038 goto disable_lock_key;
3039 windows_translate = 1;
3040 break;
3041 case VK_NUMLOCK:
3042 /* Decide whether to treat as modifier or function key. */
3043 if (NILP (Vw32_enable_num_lock))
3044 goto disable_lock_key;
3045 windows_translate = 1;
3046 break;
3047 case VK_SCROLL:
3048 /* Decide whether to treat as modifier or function key. */
3049 if (NILP (Vw32_scroll_lock_modifier))
3050 goto disable_lock_key;
3051 windows_translate = 1;
3052 break;
3053 disable_lock_key:
3054 /* Ensure the appropriate lock key state (and indicator light)
3055 remains in the same state. We do this by faking another
3056 press of the relevant key. Apparently, this really is the
3057 only way to toggle the state of the indicator lights. */
3058 dpyinfo->faked_key = wParam;
3059 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
3060 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
3061 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
3062 KEYEVENTF_EXTENDEDKEY | 0, 0);
3063 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
3064 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
3065 /* Ensure indicator lights are updated promptly on Windows 9x
3066 (TranslateMessage apparently does this), after forwarding
3067 input event. */
3068 post_character_message (hwnd, msg, wParam, lParam,
3069 w32_get_key_modifiers (wParam, lParam));
3070 windows_translate = 1;
3071 break;
3072 case VK_CONTROL:
3073 case VK_SHIFT:
3074 case VK_PROCESSKEY: /* Generated by IME. */
3075 windows_translate = 1;
3076 break;
3077 case VK_CANCEL:
3078 /* Windows maps Ctrl-Pause (aka Ctrl-Break) into VK_CANCEL,
3079 which is confusing for purposes of key binding; convert
3080 VK_CANCEL events into VK_PAUSE events. */
3081 wParam = VK_PAUSE;
3082 break;
3083 case VK_PAUSE:
3084 /* Windows maps Ctrl-NumLock into VK_PAUSE, which is confusing
3085 for purposes of key binding; convert these back into
3086 VK_NUMLOCK events, at least when we want to see NumLock key
3087 presses. (Note that there is never any possibility that
3088 VK_PAUSE with Ctrl really is C-Pause as per above.) */
3089 if (NILP (Vw32_enable_num_lock) && modifier_set (VK_CONTROL))
3090 wParam = VK_NUMLOCK;
3091 break;
3092 default:
3093 /* If not defined as a function key, change it to a WM_CHAR message. */
3094 if (lispy_function_keys[wParam] == 0)
3096 DWORD modifiers = construct_console_modifiers ();
3098 if (!NILP (Vw32_recognize_altgr)
3099 && modifier_set (VK_LCONTROL) && modifier_set (VK_RMENU))
3101 /* Always let TranslateMessage handle AltGr key chords;
3102 for some reason, ToAscii doesn't always process AltGr
3103 chords correctly. */
3104 windows_translate = 1;
3106 else if ((modifiers & (~SHIFT_PRESSED & ~CAPSLOCK_ON)) != 0)
3108 /* Handle key chords including any modifiers other
3109 than shift directly, in order to preserve as much
3110 modifier information as possible. */
3111 if ('A' <= wParam && wParam <= 'Z')
3113 /* Don't translate modified alphabetic keystrokes,
3114 so the user doesn't need to constantly switch
3115 layout to type control or meta keystrokes when
3116 the normal layout translates alphabetic
3117 characters to non-ascii characters. */
3118 if (!modifier_set (VK_SHIFT))
3119 wParam += ('a' - 'A');
3120 msg = WM_CHAR;
3122 else
3124 /* Try to handle other keystrokes by determining the
3125 base character (ie. translating the base key plus
3126 shift modifier). */
3127 int add;
3128 int isdead = 0;
3129 KEY_EVENT_RECORD key;
3131 key.bKeyDown = TRUE;
3132 key.wRepeatCount = 1;
3133 key.wVirtualKeyCode = wParam;
3134 key.wVirtualScanCode = (lParam & 0xFF0000) >> 16;
3135 key.uChar.AsciiChar = 0;
3136 key.dwControlKeyState = modifiers;
3138 add = w32_kbd_patch_key (&key);
3139 /* 0 means an unrecognised keycode, negative means
3140 dead key. Ignore both. */
3141 while (--add >= 0)
3143 /* Forward asciified character sequence. */
3144 post_character_message
3145 (hwnd, WM_CHAR, key.uChar.AsciiChar, lParam,
3146 w32_get_key_modifiers (wParam, lParam));
3147 w32_kbd_patch_key (&key);
3149 return 0;
3152 else
3154 /* Let TranslateMessage handle everything else. */
3155 windows_translate = 1;
3160 translate:
3161 if (windows_translate)
3163 MSG windows_msg = { hwnd, msg, wParam, lParam, 0, {0,0} };
3165 windows_msg.time = GetMessageTime ();
3166 TranslateMessage (&windows_msg);
3167 goto dflt;
3170 /* Fall through */
3172 case WM_SYSCHAR:
3173 case WM_CHAR:
3174 post_character_message (hwnd, msg, wParam, lParam,
3175 w32_get_key_modifiers (wParam, lParam));
3176 break;
3178 /* Simulate middle mouse button events when left and right buttons
3179 are used together, but only if user has two button mouse. */
3180 case WM_LBUTTONDOWN:
3181 case WM_RBUTTONDOWN:
3182 if (w32_num_mouse_buttons > 2)
3183 goto handle_plain_button;
3186 int this = (msg == WM_LBUTTONDOWN) ? LMOUSE : RMOUSE;
3187 int other = (msg == WM_LBUTTONDOWN) ? RMOUSE : LMOUSE;
3189 if (button_state & this)
3190 return 0;
3192 if (button_state == 0)
3193 SetCapture (hwnd);
3195 button_state |= this;
3197 if (button_state & other)
3199 if (mouse_button_timer)
3201 KillTimer (hwnd, mouse_button_timer);
3202 mouse_button_timer = 0;
3204 /* Generate middle mouse event instead. */
3205 msg = WM_MBUTTONDOWN;
3206 button_state |= MMOUSE;
3208 else if (button_state & MMOUSE)
3210 /* Ignore button event if we've already generated a
3211 middle mouse down event. This happens if the
3212 user releases and press one of the two buttons
3213 after we've faked a middle mouse event. */
3214 return 0;
3216 else
3218 /* Flush out saved message. */
3219 post_msg (&saved_mouse_button_msg);
3221 wmsg.dwModifiers = w32_get_modifiers ();
3222 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3223 signal_user_input ();
3225 /* Clear message buffer. */
3226 saved_mouse_button_msg.msg.hwnd = 0;
3228 else
3230 /* Hold onto message for now. */
3231 mouse_button_timer =
3232 SetTimer (hwnd, MOUSE_BUTTON_ID,
3233 w32_mouse_button_tolerance, NULL);
3234 saved_mouse_button_msg.msg.hwnd = hwnd;
3235 saved_mouse_button_msg.msg.message = msg;
3236 saved_mouse_button_msg.msg.wParam = wParam;
3237 saved_mouse_button_msg.msg.lParam = lParam;
3238 saved_mouse_button_msg.msg.time = GetMessageTime ();
3239 saved_mouse_button_msg.dwModifiers = w32_get_modifiers ();
3242 return 0;
3244 case WM_LBUTTONUP:
3245 case WM_RBUTTONUP:
3246 if (w32_num_mouse_buttons > 2)
3247 goto handle_plain_button;
3250 int this = (msg == WM_LBUTTONUP) ? LMOUSE : RMOUSE;
3251 int other = (msg == WM_LBUTTONUP) ? RMOUSE : LMOUSE;
3253 if ((button_state & this) == 0)
3254 return 0;
3256 button_state &= ~this;
3258 if (button_state & MMOUSE)
3260 /* Only generate event when second button is released. */
3261 if ((button_state & other) == 0)
3263 msg = WM_MBUTTONUP;
3264 button_state &= ~MMOUSE;
3266 if (button_state) abort ();
3268 else
3269 return 0;
3271 else
3273 /* Flush out saved message if necessary. */
3274 if (saved_mouse_button_msg.msg.hwnd)
3276 post_msg (&saved_mouse_button_msg);
3279 wmsg.dwModifiers = w32_get_modifiers ();
3280 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3281 signal_user_input ();
3283 /* Always clear message buffer and cancel timer. */
3284 saved_mouse_button_msg.msg.hwnd = 0;
3285 KillTimer (hwnd, mouse_button_timer);
3286 mouse_button_timer = 0;
3288 if (button_state == 0)
3289 ReleaseCapture ();
3291 return 0;
3293 case WM_XBUTTONDOWN:
3294 case WM_XBUTTONUP:
3295 if (w32_pass_extra_mouse_buttons_to_system)
3296 goto dflt;
3297 /* else fall through and process them. */
3298 case WM_MBUTTONDOWN:
3299 case WM_MBUTTONUP:
3300 handle_plain_button:
3302 BOOL up;
3303 int button;
3305 /* Ignore middle and extra buttons as long as the menu is active. */
3306 f = x_window_to_frame (dpyinfo, hwnd);
3307 if (f && f->output_data.w32->menubar_active)
3308 return 0;
3310 if (parse_button (msg, HIWORD (wParam), &button, &up))
3312 if (up) ReleaseCapture ();
3313 else SetCapture (hwnd);
3314 button = (button == 0) ? LMOUSE :
3315 ((button == 1) ? MMOUSE : RMOUSE);
3316 if (up)
3317 button_state &= ~button;
3318 else
3319 button_state |= button;
3323 wmsg.dwModifiers = w32_get_modifiers ();
3324 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3325 signal_user_input ();
3327 /* Need to return true for XBUTTON messages, false for others,
3328 to indicate that we processed the message. */
3329 return (msg == WM_XBUTTONDOWN || msg == WM_XBUTTONUP);
3331 case WM_MOUSEMOVE:
3332 /* Ignore mouse movements as long as the menu is active. These
3333 movements are processed by the window manager anyway, and
3334 it's wrong to handle them as if they happened on the
3335 underlying frame. */
3336 f = x_window_to_frame (dpyinfo, hwnd);
3337 if (f && f->output_data.w32->menubar_active)
3338 return 0;
3340 /* If the mouse has just moved into the frame, start tracking
3341 it, so we will be notified when it leaves the frame. Mouse
3342 tracking only works under W98 and NT4 and later. On earlier
3343 versions, there is no way of telling when the mouse leaves the
3344 frame, so we just have to put up with help-echo and mouse
3345 highlighting remaining while the frame is not active. */
3346 if (track_mouse_event_fn && !track_mouse_window)
3348 TRACKMOUSEEVENT tme;
3349 tme.cbSize = sizeof (tme);
3350 tme.dwFlags = TME_LEAVE;
3351 tme.hwndTrack = hwnd;
3353 track_mouse_event_fn (&tme);
3354 track_mouse_window = hwnd;
3356 case WM_VSCROLL:
3357 if (w32_mouse_move_interval <= 0
3358 || (msg == WM_MOUSEMOVE && button_state == 0))
3360 wmsg.dwModifiers = w32_get_modifiers ();
3361 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3362 return 0;
3365 /* Hang onto mouse move and scroll messages for a bit, to avoid
3366 sending such events to Emacs faster than it can process them.
3367 If we get more events before the timer from the first message
3368 expires, we just replace the first message. */
3370 if (saved_mouse_move_msg.msg.hwnd == 0)
3371 mouse_move_timer =
3372 SetTimer (hwnd, MOUSE_MOVE_ID,
3373 w32_mouse_move_interval, NULL);
3375 /* Hold onto message for now. */
3376 saved_mouse_move_msg.msg.hwnd = hwnd;
3377 saved_mouse_move_msg.msg.message = msg;
3378 saved_mouse_move_msg.msg.wParam = wParam;
3379 saved_mouse_move_msg.msg.lParam = lParam;
3380 saved_mouse_move_msg.msg.time = GetMessageTime ();
3381 saved_mouse_move_msg.dwModifiers = w32_get_modifiers ();
3383 return 0;
3385 case WM_MOUSEWHEEL:
3386 case WM_DROPFILES:
3387 wmsg.dwModifiers = w32_get_modifiers ();
3388 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3389 signal_user_input ();
3390 return 0;
3392 case WM_MOUSEHWHEEL:
3393 wmsg.dwModifiers = w32_get_modifiers ();
3394 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3395 signal_user_input ();
3396 /* Non-zero must be returned when WM_MOUSEHWHEEL messages are
3397 handled, to prevent the system trying to handle it by faking
3398 scroll bar events. */
3399 return 1;
3401 case WM_TIMER:
3402 /* Flush out saved messages if necessary. */
3403 if (wParam == mouse_button_timer)
3405 if (saved_mouse_button_msg.msg.hwnd)
3407 post_msg (&saved_mouse_button_msg);
3408 signal_user_input ();
3409 saved_mouse_button_msg.msg.hwnd = 0;
3411 KillTimer (hwnd, mouse_button_timer);
3412 mouse_button_timer = 0;
3414 else if (wParam == mouse_move_timer)
3416 if (saved_mouse_move_msg.msg.hwnd)
3418 post_msg (&saved_mouse_move_msg);
3419 saved_mouse_move_msg.msg.hwnd = 0;
3421 KillTimer (hwnd, mouse_move_timer);
3422 mouse_move_timer = 0;
3424 else if (wParam == menu_free_timer)
3426 KillTimer (hwnd, menu_free_timer);
3427 menu_free_timer = 0;
3428 f = x_window_to_frame (dpyinfo, hwnd);
3429 /* If a popup menu is active, don't wipe its strings. */
3430 if (menubar_in_use
3431 && current_popup_menu == NULL)
3433 /* Free memory used by owner-drawn and help-echo strings. */
3434 w32_free_menu_strings (hwnd);
3435 f->output_data.w32->menubar_active = 0;
3436 menubar_in_use = 0;
3439 return 0;
3441 case WM_NCACTIVATE:
3442 /* Windows doesn't send us focus messages when putting up and
3443 taking down a system popup dialog as for Ctrl-Alt-Del on Windows 95.
3444 The only indication we get that something happened is receiving
3445 this message afterwards. So this is a good time to reset our
3446 keyboard modifiers' state. */
3447 reset_modifiers ();
3448 goto dflt;
3450 case WM_INITMENU:
3451 button_state = 0;
3452 ReleaseCapture ();
3453 /* We must ensure menu bar is fully constructed and up to date
3454 before allowing user interaction with it. To achieve this
3455 we send this message to the lisp thread and wait for a
3456 reply (whose value is not actually needed) to indicate that
3457 the menu bar is now ready for use, so we can now return.
3459 To remain responsive in the meantime, we enter a nested message
3460 loop that can process all other messages.
3462 However, we skip all this if the message results from calling
3463 TrackPopupMenu - in fact, we must NOT attempt to send the lisp
3464 thread a message because it is blocked on us at this point. We
3465 set menubar_active before calling TrackPopupMenu to indicate
3466 this (there is no possibility of confusion with real menubar
3467 being active). */
3469 f = x_window_to_frame (dpyinfo, hwnd);
3470 if (f
3471 && (f->output_data.w32->menubar_active
3472 /* We can receive this message even in the absence of a
3473 menubar (ie. when the system menu is activated) - in this
3474 case we do NOT want to forward the message, otherwise it
3475 will cause the menubar to suddenly appear when the user
3476 had requested it to be turned off! */
3477 || f->output_data.w32->menubar_widget == NULL))
3478 return 0;
3481 deferred_msg msg_buf;
3483 /* Detect if message has already been deferred; in this case
3484 we cannot return any sensible value to ignore this. */
3485 if (find_deferred_msg (hwnd, msg) != NULL)
3486 abort ();
3488 menubar_in_use = 1;
3490 return send_deferred_msg (&msg_buf, hwnd, msg, wParam, lParam);
3493 case WM_EXITMENULOOP:
3494 f = x_window_to_frame (dpyinfo, hwnd);
3496 /* If a menu is still active, check again after a short delay,
3497 since Windows often (always?) sends the WM_EXITMENULOOP
3498 before the corresponding WM_COMMAND message.
3499 Don't do this if a popup menu is active, since it is only
3500 menubar menus that require cleaning up in this way.
3502 if (f && menubar_in_use && current_popup_menu == NULL)
3503 menu_free_timer = SetTimer (hwnd, MENU_FREE_ID, MENU_FREE_DELAY, NULL);
3504 goto dflt;
3506 case WM_MENUSELECT:
3507 /* Direct handling of help_echo in menus. Should be safe now
3508 that we generate the help_echo by placing a help event in the
3509 keyboard buffer. */
3511 HMENU menu = (HMENU) lParam;
3512 UINT menu_item = (UINT) LOWORD (wParam);
3513 UINT flags = (UINT) HIWORD (wParam);
3515 w32_menu_display_help (hwnd, menu, menu_item, flags);
3517 return 0;
3519 case WM_MEASUREITEM:
3520 f = x_window_to_frame (dpyinfo, hwnd);
3521 if (f)
3523 MEASUREITEMSTRUCT * pMis = (MEASUREITEMSTRUCT *) lParam;
3525 if (pMis->CtlType == ODT_MENU)
3527 /* Work out dimensions for popup menu titles. */
3528 char * title = (char *) pMis->itemData;
3529 HDC hdc = GetDC (hwnd);
3530 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
3531 LOGFONT menu_logfont;
3532 HFONT old_font;
3533 SIZE size;
3535 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
3536 menu_logfont.lfWeight = FW_BOLD;
3537 menu_font = CreateFontIndirect (&menu_logfont);
3538 old_font = SelectObject (hdc, menu_font);
3540 pMis->itemHeight = GetSystemMetrics (SM_CYMENUSIZE);
3541 if (title)
3543 if (unicode_append_menu)
3544 GetTextExtentPoint32W (hdc, (WCHAR *) title,
3545 wcslen ((WCHAR *) title),
3546 &size);
3547 else
3548 GetTextExtentPoint32 (hdc, title, strlen (title), &size);
3550 pMis->itemWidth = size.cx;
3551 if (pMis->itemHeight < size.cy)
3552 pMis->itemHeight = size.cy;
3554 else
3555 pMis->itemWidth = 0;
3557 SelectObject (hdc, old_font);
3558 DeleteObject (menu_font);
3559 ReleaseDC (hwnd, hdc);
3560 return TRUE;
3563 return 0;
3565 case WM_DRAWITEM:
3566 f = x_window_to_frame (dpyinfo, hwnd);
3567 if (f)
3569 DRAWITEMSTRUCT * pDis = (DRAWITEMSTRUCT *) lParam;
3571 if (pDis->CtlType == ODT_MENU)
3573 /* Draw popup menu title. */
3574 char * title = (char *) pDis->itemData;
3575 if (title)
3577 HDC hdc = pDis->hDC;
3578 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
3579 LOGFONT menu_logfont;
3580 HFONT old_font;
3582 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
3583 menu_logfont.lfWeight = FW_BOLD;
3584 menu_font = CreateFontIndirect (&menu_logfont);
3585 old_font = SelectObject (hdc, menu_font);
3587 /* Always draw title as if not selected. */
3588 if (unicode_append_menu)
3589 ExtTextOutW (hdc,
3590 pDis->rcItem.left
3591 + GetSystemMetrics (SM_CXMENUCHECK),
3592 pDis->rcItem.top,
3593 ETO_OPAQUE, &pDis->rcItem,
3594 (WCHAR *) title,
3595 wcslen ((WCHAR *) title), NULL);
3596 else
3597 ExtTextOut (hdc,
3598 pDis->rcItem.left
3599 + GetSystemMetrics (SM_CXMENUCHECK),
3600 pDis->rcItem.top,
3601 ETO_OPAQUE, &pDis->rcItem,
3602 title, strlen (title), NULL);
3604 SelectObject (hdc, old_font);
3605 DeleteObject (menu_font);
3607 return TRUE;
3610 return 0;
3612 #if 0
3613 /* Still not right - can't distinguish between clicks in the
3614 client area of the frame from clicks forwarded from the scroll
3615 bars - may have to hook WM_NCHITTEST to remember the mouse
3616 position and then check if it is in the client area ourselves. */
3617 case WM_MOUSEACTIVATE:
3618 /* Discard the mouse click that activates a frame, allowing the
3619 user to click anywhere without changing point (or worse!).
3620 Don't eat mouse clicks on scrollbars though!! */
3621 if (LOWORD (lParam) == HTCLIENT )
3622 return MA_ACTIVATEANDEAT;
3623 goto dflt;
3624 #endif
3626 case WM_MOUSELEAVE:
3627 /* No longer tracking mouse. */
3628 track_mouse_window = NULL;
3630 case WM_ACTIVATEAPP:
3631 case WM_ACTIVATE:
3632 case WM_WINDOWPOSCHANGED:
3633 case WM_SHOWWINDOW:
3634 /* Inform lisp thread that a frame might have just been obscured
3635 or exposed, so should recheck visibility of all frames. */
3636 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3637 goto dflt;
3639 case WM_SETFOCUS:
3640 dpyinfo->faked_key = 0;
3641 reset_modifiers ();
3642 register_hot_keys (hwnd);
3643 goto command;
3644 case WM_KILLFOCUS:
3645 unregister_hot_keys (hwnd);
3646 button_state = 0;
3647 ReleaseCapture ();
3648 /* Relinquish the system caret. */
3649 if (w32_system_caret_hwnd)
3651 w32_visible_system_caret_hwnd = NULL;
3652 w32_system_caret_hwnd = NULL;
3653 DestroyCaret ();
3655 goto command;
3656 case WM_COMMAND:
3657 menubar_in_use = 0;
3658 f = x_window_to_frame (dpyinfo, hwnd);
3659 if (f && HIWORD (wParam) == 0)
3661 if (menu_free_timer)
3663 KillTimer (hwnd, menu_free_timer);
3664 menu_free_timer = 0;
3667 case WM_MOVE:
3668 case WM_SIZE:
3669 command:
3670 wmsg.dwModifiers = w32_get_modifiers ();
3671 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3672 goto dflt;
3674 case WM_DESTROY:
3675 CoUninitialize ();
3676 return 0;
3678 case WM_CLOSE:
3679 wmsg.dwModifiers = w32_get_modifiers ();
3680 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3681 return 0;
3683 case WM_WINDOWPOSCHANGING:
3684 /* Don't restrict the sizing of tip frames. */
3685 if (hwnd == tip_window)
3686 return 0;
3688 WINDOWPLACEMENT wp;
3689 LPWINDOWPOS lppos = (WINDOWPOS *) lParam;
3691 wp.length = sizeof (WINDOWPLACEMENT);
3692 GetWindowPlacement (hwnd, &wp);
3694 if (wp.showCmd != SW_SHOWMINIMIZED && (lppos->flags & SWP_NOSIZE) == 0)
3696 RECT rect;
3697 int wdiff;
3698 int hdiff;
3699 DWORD font_width;
3700 DWORD line_height;
3701 DWORD internal_border;
3702 DWORD scrollbar_extra;
3703 RECT wr;
3705 wp.length = sizeof(wp);
3706 GetWindowRect (hwnd, &wr);
3708 enter_crit ();
3710 font_width = GetWindowLong (hwnd, WND_FONTWIDTH_INDEX);
3711 line_height = GetWindowLong (hwnd, WND_LINEHEIGHT_INDEX);
3712 internal_border = GetWindowLong (hwnd, WND_BORDER_INDEX);
3713 scrollbar_extra = GetWindowLong (hwnd, WND_SCROLLBAR_INDEX);
3715 leave_crit ();
3717 memset (&rect, 0, sizeof (rect));
3718 AdjustWindowRect (&rect, GetWindowLong (hwnd, GWL_STYLE),
3719 GetMenu (hwnd) != NULL);
3721 /* Force width and height of client area to be exact
3722 multiples of the character cell dimensions. */
3723 wdiff = (lppos->cx - (rect.right - rect.left)
3724 - 2 * internal_border - scrollbar_extra)
3725 % font_width;
3726 hdiff = (lppos->cy - (rect.bottom - rect.top)
3727 - 2 * internal_border)
3728 % line_height;
3730 if (wdiff || hdiff)
3732 /* For right/bottom sizing we can just fix the sizes.
3733 However for top/left sizing we will need to fix the X
3734 and Y positions as well. */
3736 int cx_mintrack = GetSystemMetrics (SM_CXMINTRACK);
3737 int cy_mintrack = GetSystemMetrics (SM_CYMINTRACK);
3739 lppos->cx = max (lppos->cx - wdiff, cx_mintrack);
3740 lppos->cy = max (lppos->cy - hdiff, cy_mintrack);
3742 if (wp.showCmd != SW_SHOWMAXIMIZED
3743 && (lppos->flags & SWP_NOMOVE) == 0)
3745 if (lppos->x != wr.left || lppos->y != wr.top)
3747 lppos->x += wdiff;
3748 lppos->y += hdiff;
3750 else
3752 lppos->flags |= SWP_NOMOVE;
3756 return 0;
3761 goto dflt;
3763 case WM_GETMINMAXINFO:
3764 /* Hack to allow resizing the Emacs frame above the screen size.
3765 Note that Windows 9x limits coordinates to 16-bits. */
3766 ((LPMINMAXINFO) lParam)->ptMaxTrackSize.x = 32767;
3767 ((LPMINMAXINFO) lParam)->ptMaxTrackSize.y = 32767;
3768 return 0;
3770 case WM_SETCURSOR:
3771 if (LOWORD (lParam) == HTCLIENT)
3772 return 0;
3774 goto dflt;
3776 case WM_EMACS_SETCURSOR:
3778 Cursor cursor = (Cursor) wParam;
3779 if (cursor)
3780 SetCursor (cursor);
3781 return 0;
3784 case WM_EMACS_CREATESCROLLBAR:
3785 return (LRESULT) w32_createscrollbar ((struct frame *) wParam,
3786 (struct scroll_bar *) lParam);
3788 case WM_EMACS_SHOWWINDOW:
3789 return ShowWindow ((HWND) wParam, (WPARAM) lParam);
3791 case WM_EMACS_SETFOREGROUND:
3793 HWND foreground_window;
3794 DWORD foreground_thread, retval;
3796 /* On NT 5.0, and apparently Windows 98, it is necessary to
3797 attach to the thread that currently has focus in order to
3798 pull the focus away from it. */
3799 foreground_window = GetForegroundWindow ();
3800 foreground_thread = GetWindowThreadProcessId (foreground_window, NULL);
3801 if (!foreground_window
3802 || foreground_thread == GetCurrentThreadId ()
3803 || !AttachThreadInput (GetCurrentThreadId (),
3804 foreground_thread, TRUE))
3805 foreground_thread = 0;
3807 retval = SetForegroundWindow ((HWND) wParam);
3809 /* Detach from the previous foreground thread. */
3810 if (foreground_thread)
3811 AttachThreadInput (GetCurrentThreadId (),
3812 foreground_thread, FALSE);
3814 return retval;
3817 case WM_EMACS_SETWINDOWPOS:
3819 WINDOWPOS * pos = (WINDOWPOS *) wParam;
3820 return SetWindowPos (hwnd, pos->hwndInsertAfter,
3821 pos->x, pos->y, pos->cx, pos->cy, pos->flags);
3824 case WM_EMACS_DESTROYWINDOW:
3825 DragAcceptFiles ((HWND) wParam, FALSE);
3826 return DestroyWindow ((HWND) wParam);
3828 case WM_EMACS_HIDE_CARET:
3829 return HideCaret (hwnd);
3831 case WM_EMACS_SHOW_CARET:
3832 return ShowCaret (hwnd);
3834 case WM_EMACS_DESTROY_CARET:
3835 w32_system_caret_hwnd = NULL;
3836 w32_visible_system_caret_hwnd = NULL;
3837 return DestroyCaret ();
3839 case WM_EMACS_TRACK_CARET:
3840 /* If there is currently no system caret, create one. */
3841 if (w32_system_caret_hwnd == NULL)
3843 /* Use the default caret width, and avoid changing it
3844 unneccesarily, as it confuses screen reader software. */
3845 w32_system_caret_hwnd = hwnd;
3846 CreateCaret (hwnd, NULL, 0,
3847 w32_system_caret_height);
3850 if (!SetCaretPos (w32_system_caret_x, w32_system_caret_y))
3851 return 0;
3852 /* Ensure visible caret gets turned on when requested. */
3853 else if (w32_use_visible_system_caret
3854 && w32_visible_system_caret_hwnd != hwnd)
3856 w32_visible_system_caret_hwnd = hwnd;
3857 return ShowCaret (hwnd);
3859 /* Ensure visible caret gets turned off when requested. */
3860 else if (!w32_use_visible_system_caret
3861 && w32_visible_system_caret_hwnd)
3863 w32_visible_system_caret_hwnd = NULL;
3864 return HideCaret (hwnd);
3866 else
3867 return 1;
3869 case WM_EMACS_TRACKPOPUPMENU:
3871 UINT flags;
3872 POINT *pos;
3873 int retval;
3874 pos = (POINT *)lParam;
3875 flags = TPM_CENTERALIGN;
3876 if (button_state & LMOUSE)
3877 flags |= TPM_LEFTBUTTON;
3878 else if (button_state & RMOUSE)
3879 flags |= TPM_RIGHTBUTTON;
3881 /* Remember we did a SetCapture on the initial mouse down event,
3882 so for safety, we make sure the capture is cancelled now. */
3883 ReleaseCapture ();
3884 button_state = 0;
3886 /* Use menubar_active to indicate that WM_INITMENU is from
3887 TrackPopupMenu below, and should be ignored. */
3888 f = x_window_to_frame (dpyinfo, hwnd);
3889 if (f)
3890 f->output_data.w32->menubar_active = 1;
3892 if (TrackPopupMenu ((HMENU)wParam, flags, pos->x, pos->y,
3893 0, hwnd, NULL))
3895 MSG amsg;
3896 /* Eat any mouse messages during popupmenu */
3897 while (PeekMessage (&amsg, hwnd, WM_MOUSEFIRST, WM_MOUSELAST,
3898 PM_REMOVE));
3899 /* Get the menu selection, if any */
3900 if (PeekMessage (&amsg, hwnd, WM_COMMAND, WM_COMMAND, PM_REMOVE))
3902 retval = LOWORD (amsg.wParam);
3904 else
3906 retval = 0;
3909 else
3911 retval = -1;
3914 return retval;
3917 default:
3918 /* Check for messages registered at runtime. */
3919 if (msg == msh_mousewheel)
3921 wmsg.dwModifiers = w32_get_modifiers ();
3922 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3923 signal_user_input ();
3924 return 0;
3927 dflt:
3928 return DefWindowProc (hwnd, msg, wParam, lParam);
3932 /* The most common default return code for handled messages is 0. */
3933 return 0;
3936 static void
3937 my_create_window (f)
3938 struct frame * f;
3940 MSG msg;
3942 if (!PostThreadMessage (dwWindowsThreadId, WM_EMACS_CREATEWINDOW, (WPARAM)f, 0))
3943 abort ();
3944 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
3948 /* Create a tooltip window. Unlike my_create_window, we do not do this
3949 indirectly via the Window thread, as we do not need to process Window
3950 messages for the tooltip. Creating tooltips indirectly also creates
3951 deadlocks when tooltips are created for menu items. */
3952 static void
3953 my_create_tip_window (f)
3954 struct frame *f;
3956 RECT rect;
3958 rect.left = rect.top = 0;
3959 rect.right = FRAME_PIXEL_WIDTH (f);
3960 rect.bottom = FRAME_PIXEL_HEIGHT (f);
3962 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
3963 FRAME_EXTERNAL_MENU_BAR (f));
3965 tip_window = FRAME_W32_WINDOW (f)
3966 = CreateWindow (EMACS_CLASS,
3967 f->namebuf,
3968 f->output_data.w32->dwStyle,
3969 f->left_pos,
3970 f->top_pos,
3971 rect.right - rect.left,
3972 rect.bottom - rect.top,
3973 FRAME_W32_WINDOW (SELECTED_FRAME ()), /* owner */
3974 NULL,
3975 hinst,
3976 NULL);
3978 if (tip_window)
3980 SetWindowLong (tip_window, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
3981 SetWindowLong (tip_window, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
3982 SetWindowLong (tip_window, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
3983 SetWindowLong (tip_window, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
3985 /* Tip frames have no scrollbars. */
3986 SetWindowLong (tip_window, WND_SCROLLBAR_INDEX, 0);
3988 /* Do this to discard the default setting specified by our parent. */
3989 ShowWindow (tip_window, SW_HIDE);
3994 /* Create and set up the w32 window for frame F. */
3996 static void
3997 w32_window (f, window_prompting, minibuffer_only)
3998 struct frame *f;
3999 long window_prompting;
4000 int minibuffer_only;
4002 BLOCK_INPUT;
4004 /* Use the resource name as the top-level window name
4005 for looking up resources. Make a non-Lisp copy
4006 for the window manager, so GC relocation won't bother it.
4008 Elsewhere we specify the window name for the window manager. */
4011 char *str = (char *) SDATA (Vx_resource_name);
4012 f->namebuf = (char *) xmalloc (strlen (str) + 1);
4013 strcpy (f->namebuf, str);
4016 my_create_window (f);
4018 validate_x_resource_name ();
4020 /* x_set_name normally ignores requests to set the name if the
4021 requested name is the same as the current name. This is the one
4022 place where that assumption isn't correct; f->name is set, but
4023 the server hasn't been told. */
4025 Lisp_Object name;
4026 int explicit = f->explicit_name;
4028 f->explicit_name = 0;
4029 name = f->name;
4030 f->name = Qnil;
4031 x_set_name (f, name, explicit);
4034 UNBLOCK_INPUT;
4036 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
4037 initialize_frame_menubar (f);
4039 if (FRAME_W32_WINDOW (f) == 0)
4040 error ("Unable to create window");
4043 /* Handle the icon stuff for this window. Perhaps later we might
4044 want an x_set_icon_position which can be called interactively as
4045 well. */
4047 static void
4048 x_icon (f, parms)
4049 struct frame *f;
4050 Lisp_Object parms;
4052 Lisp_Object icon_x, icon_y;
4054 /* Set the position of the icon. Note that Windows 95 groups all
4055 icons in the tray. */
4056 icon_x = w32_get_arg (parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
4057 icon_y = w32_get_arg (parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
4058 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
4060 CHECK_NUMBER (icon_x);
4061 CHECK_NUMBER (icon_y);
4063 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
4064 error ("Both left and top icon corners of icon must be specified");
4066 BLOCK_INPUT;
4068 if (! EQ (icon_x, Qunbound))
4069 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
4071 #if 0 /* TODO */
4072 /* Start up iconic or window? */
4073 x_wm_set_window_state
4074 (f, (EQ (w32_get_arg (parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL), Qicon)
4075 ? IconicState
4076 : NormalState));
4078 x_text_icon (f, (char *) SDATA ((!NILP (f->icon_name)
4079 ? f->icon_name
4080 : f->name)));
4081 #endif
4083 UNBLOCK_INPUT;
4087 static void
4088 x_make_gc (f)
4089 struct frame *f;
4091 XGCValues gc_values;
4093 BLOCK_INPUT;
4095 /* Create the GC's of this frame.
4096 Note that many default values are used. */
4098 /* Normal video */
4099 gc_values.font = FRAME_FONT (f);
4101 /* Cursor has cursor-color background, background-color foreground. */
4102 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
4103 gc_values.background = f->output_data.w32->cursor_pixel;
4104 f->output_data.w32->cursor_gc
4105 = XCreateGC (NULL, FRAME_W32_WINDOW (f),
4106 (GCFont | GCForeground | GCBackground),
4107 &gc_values);
4109 /* Reliefs. */
4110 f->output_data.w32->white_relief.gc = 0;
4111 f->output_data.w32->black_relief.gc = 0;
4113 UNBLOCK_INPUT;
4117 /* Handler for signals raised during x_create_frame and
4118 x_create_top_frame. FRAME is the frame which is partially
4119 constructed. */
4121 static Lisp_Object
4122 unwind_create_frame (frame)
4123 Lisp_Object frame;
4125 struct frame *f = XFRAME (frame);
4127 /* If frame is ``official'', nothing to do. */
4128 if (!CONSP (Vframe_list) || !EQ (XCAR (Vframe_list), frame))
4130 #ifdef GLYPH_DEBUG
4131 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4132 #endif
4134 x_free_frame_resources (f);
4136 /* Check that reference counts are indeed correct. */
4137 xassert (dpyinfo->reference_count == dpyinfo_refcount);
4138 xassert (dpyinfo->image_cache->refcount == image_cache_refcount);
4140 return Qt;
4143 return Qnil;
4147 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
4148 1, 1, 0,
4149 doc: /* Make a new window, which is called a \"frame\" in Emacs terms.
4150 Returns an Emacs frame object.
4151 PARAMETERS is an alist of frame parameters.
4152 If the parameters specify that the frame should not have a minibuffer,
4153 and do not specify a specific minibuffer window to use,
4154 then `default-minibuffer-frame' must be a frame whose minibuffer can
4155 be shared by the new frame.
4157 This function is an internal primitive--use `make-frame' instead. */)
4158 (parameters)
4159 Lisp_Object parameters;
4161 struct frame *f;
4162 Lisp_Object frame, tem;
4163 Lisp_Object name;
4164 int minibuffer_only = 0;
4165 long window_prompting = 0;
4166 int width, height;
4167 int count = SPECPDL_INDEX ();
4168 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4169 Lisp_Object display;
4170 struct w32_display_info *dpyinfo = NULL;
4171 Lisp_Object parent;
4172 struct kboard *kb;
4174 check_w32 ();
4176 /* Use this general default value to start with
4177 until we know if this frame has a specified name. */
4178 Vx_resource_name = Vinvocation_name;
4180 display = w32_get_arg (parameters, Qdisplay, 0, 0, RES_TYPE_STRING);
4181 if (EQ (display, Qunbound))
4182 display = Qnil;
4183 dpyinfo = check_x_display_info (display);
4184 #ifdef MULTI_KBOARD
4185 kb = dpyinfo->terminal->kboard;
4186 #else
4187 kb = &the_only_kboard;
4188 #endif
4190 name = w32_get_arg (parameters, Qname, "name", "Name", RES_TYPE_STRING);
4191 if (!STRINGP (name)
4192 && ! EQ (name, Qunbound)
4193 && ! NILP (name))
4194 error ("Invalid frame name--not a string or nil");
4196 if (STRINGP (name))
4197 Vx_resource_name = name;
4199 /* See if parent window is specified. */
4200 parent = w32_get_arg (parameters, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
4201 if (EQ (parent, Qunbound))
4202 parent = Qnil;
4203 if (! NILP (parent))
4204 CHECK_NUMBER (parent);
4206 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
4207 /* No need to protect DISPLAY because that's not used after passing
4208 it to make_frame_without_minibuffer. */
4209 frame = Qnil;
4210 GCPRO4 (parameters, parent, name, frame);
4211 tem = w32_get_arg (parameters, Qminibuffer, "minibuffer", "Minibuffer",
4212 RES_TYPE_SYMBOL);
4213 if (EQ (tem, Qnone) || NILP (tem))
4214 f = make_frame_without_minibuffer (Qnil, kb, display);
4215 else if (EQ (tem, Qonly))
4217 f = make_minibuffer_frame ();
4218 minibuffer_only = 1;
4220 else if (WINDOWP (tem))
4221 f = make_frame_without_minibuffer (tem, kb, display);
4222 else
4223 f = make_frame (1);
4225 XSETFRAME (frame, f);
4227 /* Note that Windows does support scroll bars. */
4228 FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;
4230 /* By default, make scrollbars the system standard width. */
4231 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = GetSystemMetrics (SM_CXVSCROLL);
4233 f->terminal = dpyinfo->terminal;
4234 f->terminal->reference_count++;
4236 f->output_method = output_w32;
4237 f->output_data.w32 =
4238 (struct w32_output *) xmalloc (sizeof (struct w32_output));
4239 bzero (f->output_data.w32, sizeof (struct w32_output));
4240 FRAME_FONTSET (f) = -1;
4241 record_unwind_protect (unwind_create_frame, frame);
4243 f->icon_name
4244 = w32_get_arg (parameters, Qicon_name, "iconName", "Title", RES_TYPE_STRING);
4245 if (! STRINGP (f->icon_name))
4246 f->icon_name = Qnil;
4248 /* FRAME_W32_DISPLAY_INFO (f) = dpyinfo; */
4249 #ifdef MULTI_KBOARD
4250 FRAME_KBOARD (f) = kb;
4251 #endif
4253 /* Specify the parent under which to make this window. */
4255 if (!NILP (parent))
4257 f->output_data.w32->parent_desc = (Window) XFASTINT (parent);
4258 f->output_data.w32->explicit_parent = 1;
4260 else
4262 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
4263 f->output_data.w32->explicit_parent = 0;
4266 /* Set the name; the functions to which we pass f expect the name to
4267 be set. */
4268 if (EQ (name, Qunbound) || NILP (name))
4270 f->name = build_string (dpyinfo->w32_id_name);
4271 f->explicit_name = 0;
4273 else
4275 f->name = name;
4276 f->explicit_name = 1;
4277 /* use the frame's title when getting resources for this frame. */
4278 specbind (Qx_resource_name, name);
4281 /* Extract the window parameters from the supplied values
4282 that are needed to determine window geometry. */
4284 Lisp_Object font;
4286 font = w32_get_arg (parameters, Qfont, "font", "Font", RES_TYPE_STRING);
4288 BLOCK_INPUT;
4289 /* First, try whatever font the caller has specified. */
4290 if (STRINGP (font))
4292 tem = Fquery_fontset (font, Qnil);
4293 if (STRINGP (tem))
4294 font = x_new_fontset (f, SDATA (tem));
4295 else
4296 font = x_new_font (f, SDATA (font));
4298 /* Try out a font which we hope has bold and italic variations. */
4299 if (!STRINGP (font))
4300 font = x_new_font (f, "-*-Courier New-normal-r-*-*-*-100-*-*-c-*-iso8859-1");
4301 if (! STRINGP (font))
4302 font = x_new_font (f, "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1");
4303 /* If those didn't work, look for something which will at least work. */
4304 if (! STRINGP (font))
4305 font = x_new_font (f, "-*-Fixedsys-normal-r-*-*-12-*-*-*-c-*-iso8859-1");
4306 UNBLOCK_INPUT;
4307 if (! STRINGP (font))
4308 font = build_string ("Fixedsys");
4310 x_default_parameter (f, parameters, Qfont, font,
4311 "font", "Font", RES_TYPE_STRING);
4314 x_default_parameter (f, parameters, Qborder_width, make_number (2),
4315 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
4316 /* This defaults to 2 in order to match xterm. We recognize either
4317 internalBorderWidth or internalBorder (which is what xterm calls
4318 it). */
4319 if (NILP (Fassq (Qinternal_border_width, parameters)))
4321 Lisp_Object value;
4323 value = w32_get_arg (parameters, Qinternal_border_width,
4324 "internalBorder", "InternalBorder", RES_TYPE_NUMBER);
4325 if (! EQ (value, Qunbound))
4326 parameters = Fcons (Fcons (Qinternal_border_width, value),
4327 parameters);
4329 /* Default internalBorderWidth to 0 on Windows to match other programs. */
4330 x_default_parameter (f, parameters, Qinternal_border_width, make_number (0),
4331 "internalBorderWidth", "InternalBorder", RES_TYPE_NUMBER);
4332 x_default_parameter (f, parameters, Qvertical_scroll_bars, Qright,
4333 "verticalScrollBars", "ScrollBars", RES_TYPE_SYMBOL);
4335 /* Also do the stuff which must be set before the window exists. */
4336 x_default_parameter (f, parameters, Qforeground_color, build_string ("black"),
4337 "foreground", "Foreground", RES_TYPE_STRING);
4338 x_default_parameter (f, parameters, Qbackground_color, build_string ("white"),
4339 "background", "Background", RES_TYPE_STRING);
4340 x_default_parameter (f, parameters, Qmouse_color, build_string ("black"),
4341 "pointerColor", "Foreground", RES_TYPE_STRING);
4342 x_default_parameter (f, parameters, Qcursor_color, build_string ("black"),
4343 "cursorColor", "Foreground", RES_TYPE_STRING);
4344 x_default_parameter (f, parameters, Qborder_color, build_string ("black"),
4345 "borderColor", "BorderColor", RES_TYPE_STRING);
4346 x_default_parameter (f, parameters, Qscreen_gamma, Qnil,
4347 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
4348 x_default_parameter (f, parameters, Qline_spacing, Qnil,
4349 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
4350 x_default_parameter (f, parameters, Qleft_fringe, Qnil,
4351 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
4352 x_default_parameter (f, parameters, Qright_fringe, Qnil,
4353 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
4356 /* Init faces before x_default_parameter is called for scroll-bar
4357 parameters because that function calls x_set_scroll_bar_width,
4358 which calls change_frame_size, which calls Fset_window_buffer,
4359 which runs hooks, which call Fvertical_motion. At the end, we
4360 end up in init_iterator with a null face cache, which should not
4361 happen. */
4362 init_frame_faces (f);
4364 x_default_parameter (f, parameters, Qmenu_bar_lines, make_number (1),
4365 "menuBar", "MenuBar", RES_TYPE_NUMBER);
4366 x_default_parameter (f, parameters, Qtool_bar_lines, make_number (1),
4367 "toolBar", "ToolBar", RES_TYPE_NUMBER);
4369 x_default_parameter (f, parameters, Qbuffer_predicate, Qnil,
4370 "bufferPredicate", "BufferPredicate", RES_TYPE_SYMBOL);
4371 x_default_parameter (f, parameters, Qtitle, Qnil,
4372 "title", "Title", RES_TYPE_STRING);
4373 x_default_parameter (f, parameters, Qfullscreen, Qnil,
4374 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
4376 f->output_data.w32->dwStyle = WS_OVERLAPPEDWINDOW;
4377 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
4379 f->output_data.w32->text_cursor = w32_load_cursor (IDC_IBEAM);
4380 f->output_data.w32->nontext_cursor = w32_load_cursor (IDC_ARROW);
4381 f->output_data.w32->modeline_cursor = w32_load_cursor (IDC_ARROW);
4382 f->output_data.w32->hand_cursor = w32_load_cursor (IDC_HAND);
4383 f->output_data.w32->hourglass_cursor = w32_load_cursor (IDC_WAIT);
4384 f->output_data.w32->horizontal_drag_cursor = w32_load_cursor (IDC_SIZEWE);
4386 window_prompting = x_figure_window_size (f, parameters, 1);
4388 tem = w32_get_arg (parameters, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
4389 f->no_split = minibuffer_only || EQ (tem, Qt);
4391 w32_window (f, window_prompting, minibuffer_only);
4392 x_icon (f, parameters);
4394 x_make_gc (f);
4396 /* Now consider the frame official. */
4397 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
4398 Vframe_list = Fcons (frame, Vframe_list);
4400 /* We need to do this after creating the window, so that the
4401 icon-creation functions can say whose icon they're describing. */
4402 x_default_parameter (f, parameters, Qicon_type, Qnil,
4403 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
4405 x_default_parameter (f, parameters, Qauto_raise, Qnil,
4406 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4407 x_default_parameter (f, parameters, Qauto_lower, Qnil,
4408 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4409 x_default_parameter (f, parameters, Qcursor_type, Qbox,
4410 "cursorType", "CursorType", RES_TYPE_SYMBOL);
4411 x_default_parameter (f, parameters, Qscroll_bar_width, Qnil,
4412 "scrollBarWidth", "ScrollBarWidth", RES_TYPE_NUMBER);
4414 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
4415 Change will not be effected unless different from the current
4416 FRAME_LINES (f). */
4417 width = FRAME_COLS (f);
4418 height = FRAME_LINES (f);
4420 FRAME_LINES (f) = 0;
4421 SET_FRAME_COLS (f, 0);
4422 change_frame_size (f, height, width, 1, 0, 0);
4424 /* Tell the server what size and position, etc, we want, and how
4425 badly we want them. This should be done after we have the menu
4426 bar so that its size can be taken into account. */
4427 BLOCK_INPUT;
4428 x_wm_set_size_hint (f, window_prompting, 0);
4429 UNBLOCK_INPUT;
4431 /* Make the window appear on the frame and enable display, unless
4432 the caller says not to. However, with explicit parent, Emacs
4433 cannot control visibility, so don't try. */
4434 if (! f->output_data.w32->explicit_parent)
4436 Lisp_Object visibility;
4438 visibility = w32_get_arg (parameters, Qvisibility, 0, 0, RES_TYPE_SYMBOL);
4439 if (EQ (visibility, Qunbound))
4440 visibility = Qt;
4442 if (EQ (visibility, Qicon))
4443 x_iconify_frame (f);
4444 else if (! NILP (visibility))
4445 x_make_frame_visible (f);
4446 else
4447 /* Must have been Qnil. */
4451 /* Initialize `default-minibuffer-frame' in case this is the first
4452 frame on this terminal. */
4453 if (FRAME_HAS_MINIBUF_P (f)
4454 && (!FRAMEP (kb->Vdefault_minibuffer_frame)
4455 || !FRAME_LIVE_P (XFRAME (kb->Vdefault_minibuffer_frame))))
4456 kb->Vdefault_minibuffer_frame = frame;
4458 /* All remaining specified parameters, which have not been "used"
4459 by x_get_arg and friends, now go in the misc. alist of the frame. */
4460 for (tem = parameters; !NILP (tem); tem = XCDR (tem))
4461 if (CONSP (XCAR (tem)) && !NILP (XCAR (XCAR (tem))))
4462 f->param_alist = Fcons (XCAR (tem), f->param_alist);
4464 store_frame_param (f, Qwindow_system, Qw32);
4466 UNGCPRO;
4468 /* Make sure windows on this frame appear in calls to next-window
4469 and similar functions. */
4470 Vwindow_list = Qnil;
4472 return unbind_to (count, frame);
4475 /* FRAME is used only to get a handle on the X display. We don't pass the
4476 display info directly because we're called from frame.c, which doesn't
4477 know about that structure. */
4478 Lisp_Object
4479 x_get_focus_frame (frame)
4480 struct frame *frame;
4482 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (frame);
4483 Lisp_Object xfocus;
4484 if (! dpyinfo->w32_focus_frame)
4485 return Qnil;
4487 XSETFRAME (xfocus, dpyinfo->w32_focus_frame);
4488 return xfocus;
4491 DEFUN ("w32-focus-frame", Fw32_focus_frame, Sw32_focus_frame, 1, 1, 0,
4492 doc: /* Give FRAME input focus, raising to foreground if necessary. */)
4493 (frame)
4494 Lisp_Object frame;
4496 x_focus_on_frame (check_x_frame (frame));
4497 return Qnil;
4501 /* Return the charset portion of a font name. */
4502 char * xlfd_charset_of_font (char * fontname)
4504 char *charset, *encoding;
4506 encoding = strrchr(fontname, '-');
4507 if (!encoding || encoding == fontname)
4508 return NULL;
4510 for (charset = encoding - 1; charset >= fontname; charset--)
4511 if (*charset == '-')
4512 break;
4514 if (charset == fontname || strcmp(charset, "-*-*") == 0)
4515 return NULL;
4517 return charset + 1;
4520 struct font_info *w32_load_bdf_font (struct frame *f, char *fontname,
4521 int size, char* filename);
4522 static Lisp_Object w32_list_bdf_fonts (Lisp_Object pattern, int max_names);
4523 static BOOL w32_to_x_font (LOGFONT * lplf, char * lpxstr, int len,
4524 char * charset);
4525 static BOOL x_to_w32_font (char *lpxstr, LOGFONT *lplogfont);
4527 static struct font_info *
4528 w32_load_system_font (f,fontname,size)
4529 struct frame *f;
4530 char * fontname;
4531 int size;
4533 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4534 Lisp_Object font_names;
4536 /* Get a list of all the fonts that match this name. Once we
4537 have a list of matching fonts, we compare them against the fonts
4538 we already have loaded by comparing names. */
4539 font_names = w32_list_fonts (f, build_string (fontname), size, 100);
4541 if (!NILP (font_names))
4543 Lisp_Object tail;
4544 int i;
4546 /* First check if any are already loaded, as that is cheaper
4547 than loading another one. */
4548 for (i = 0; i < dpyinfo->n_fonts; i++)
4549 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
4550 if (dpyinfo->font_table[i].name
4551 && (!strcmp (dpyinfo->font_table[i].name,
4552 SDATA (XCAR (tail)))
4553 || !strcmp (dpyinfo->font_table[i].full_name,
4554 SDATA (XCAR (tail)))))
4555 return (dpyinfo->font_table + i);
4557 fontname = (char *) SDATA (XCAR (font_names));
4559 else if (w32_strict_fontnames)
4561 /* If EnumFontFamiliesEx was available, we got a full list of
4562 fonts back so stop now to avoid the possibility of loading a
4563 random font. If we had to fall back to EnumFontFamilies, the
4564 list is incomplete, so continue whether the font we want was
4565 listed or not. */
4566 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
4567 FARPROC enum_font_families_ex
4568 = GetProcAddress (gdi32, "EnumFontFamiliesExA");
4569 if (enum_font_families_ex)
4570 return NULL;
4573 /* Load the font and add it to the table. */
4575 char *full_name, *encoding, *charset;
4576 XFontStruct *font;
4577 struct font_info *fontp;
4578 LOGFONT lf;
4579 BOOL ok;
4580 int codepage;
4581 int i;
4583 if (!fontname || !x_to_w32_font (fontname, &lf))
4584 return (NULL);
4586 if (!*lf.lfFaceName)
4587 /* If no name was specified for the font, we get a random font
4588 from CreateFontIndirect - this is not particularly
4589 desirable, especially since CreateFontIndirect does not
4590 fill out the missing name in lf, so we never know what we
4591 ended up with. */
4592 return NULL;
4594 lf.lfQuality = DEFAULT_QUALITY;
4596 font = (XFontStruct *) xmalloc (sizeof (XFontStruct));
4597 bzero (font, sizeof (*font));
4599 /* Set bdf to NULL to indicate that this is a Windows font. */
4600 font->bdf = NULL;
4602 BLOCK_INPUT;
4604 font->hfont = CreateFontIndirect (&lf);
4606 if (font->hfont == NULL)
4608 ok = FALSE;
4610 else
4612 HDC hdc;
4613 HANDLE oldobj;
4615 codepage = w32_codepage_for_font (fontname);
4617 hdc = GetDC (dpyinfo->root_window);
4618 oldobj = SelectObject (hdc, font->hfont);
4620 ok = GetTextMetrics (hdc, &font->tm);
4621 if (codepage == CP_UNICODE)
4622 font->double_byte_p = 1;
4623 else
4625 /* Unfortunately, some fonts (eg. MingLiU, a big5 ttf font)
4626 don't report themselves as double byte fonts, when
4627 patently they are. So instead of trusting
4628 GetFontLanguageInfo, we check the properties of the
4629 codepage directly, since that is ultimately what we are
4630 working from anyway. */
4631 /* font->double_byte_p = GetFontLanguageInfo(hdc) & GCP_DBCS; */
4632 CPINFO cpi = {0};
4633 GetCPInfo (codepage, &cpi);
4634 font->double_byte_p = cpi.MaxCharSize > 1;
4637 SelectObject (hdc, oldobj);
4638 ReleaseDC (dpyinfo->root_window, hdc);
4639 /* Fill out details in lf according to the font that was
4640 actually loaded. */
4641 lf.lfHeight = font->tm.tmInternalLeading - font->tm.tmHeight;
4642 lf.lfWidth = font->tm.tmMaxCharWidth;
4643 lf.lfWeight = font->tm.tmWeight;
4644 lf.lfItalic = font->tm.tmItalic;
4645 lf.lfCharSet = font->tm.tmCharSet;
4646 lf.lfPitchAndFamily = ((font->tm.tmPitchAndFamily & TMPF_FIXED_PITCH)
4647 ? VARIABLE_PITCH : FIXED_PITCH);
4648 lf.lfOutPrecision = ((font->tm.tmPitchAndFamily & TMPF_VECTOR)
4649 ? OUT_STROKE_PRECIS : OUT_STRING_PRECIS);
4651 w32_cache_char_metrics (font);
4654 UNBLOCK_INPUT;
4656 if (!ok)
4658 w32_unload_font (dpyinfo, font);
4659 return (NULL);
4662 /* Find a free slot in the font table. */
4663 for (i = 0; i < dpyinfo->n_fonts; ++i)
4664 if (dpyinfo->font_table[i].name == NULL)
4665 break;
4667 /* If no free slot found, maybe enlarge the font table. */
4668 if (i == dpyinfo->n_fonts
4669 && dpyinfo->n_fonts == dpyinfo->font_table_size)
4671 int sz;
4672 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
4673 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
4674 dpyinfo->font_table
4675 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
4678 fontp = dpyinfo->font_table + i;
4679 if (i == dpyinfo->n_fonts)
4680 ++dpyinfo->n_fonts;
4682 /* Now fill in the slots of *FONTP. */
4683 BLOCK_INPUT;
4684 bzero (fontp, sizeof (*fontp));
4685 fontp->font = font;
4686 fontp->font_idx = i;
4687 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
4688 bcopy (fontname, fontp->name, strlen (fontname) + 1);
4690 if (lf.lfPitchAndFamily == FIXED_PITCH)
4692 /* Fixed width font. */
4693 fontp->average_width = fontp->space_width = FONT_WIDTH (font);
4695 else
4697 wchar_t space = 32;
4698 XCharStruct* pcm;
4699 pcm = w32_per_char_metric (font, &space, ANSI_FONT);
4700 if (pcm)
4701 fontp->space_width = pcm->width;
4702 else
4703 fontp->space_width = FONT_WIDTH (font);
4705 fontp->average_width = font->tm.tmAveCharWidth;
4708 charset = xlfd_charset_of_font (fontname);
4710 /* Cache the W32 codepage for a font. This makes w32_encode_char
4711 (called for every glyph during redisplay) much faster. */
4712 fontp->codepage = codepage;
4714 /* Work out the font's full name. */
4715 full_name = (char *)xmalloc (100);
4716 if (full_name && w32_to_x_font (&lf, full_name, 100, charset))
4717 fontp->full_name = full_name;
4718 else
4720 /* If all else fails - just use the name we used to load it. */
4721 xfree (full_name);
4722 fontp->full_name = fontp->name;
4725 fontp->size = FONT_WIDTH (font);
4726 fontp->height = FONT_HEIGHT (font);
4728 /* The slot `encoding' specifies how to map a character
4729 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
4730 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
4731 (0:0x20..0x7F, 1:0xA0..0xFF,
4732 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
4733 2:0xA020..0xFF7F). For the moment, we don't know which charset
4734 uses this font. So, we set information in fontp->encoding[1]
4735 which is never used by any charset. If mapping can't be
4736 decided, set FONT_ENCODING_NOT_DECIDED. */
4738 /* SJIS fonts need to be set to type 4, all others seem to work as
4739 type FONT_ENCODING_NOT_DECIDED. */
4740 encoding = strrchr (fontp->name, '-');
4741 if (encoding && strnicmp (encoding+1, "sjis", 4) == 0)
4742 fontp->encoding[1] = 4;
4743 else
4744 fontp->encoding[1] = FONT_ENCODING_NOT_DECIDED;
4746 /* The following three values are set to 0 under W32, which is
4747 what they get set to if XGetFontProperty fails under X. */
4748 fontp->baseline_offset = 0;
4749 fontp->relative_compose = 0;
4750 fontp->default_ascent = 0;
4752 /* Set global flag fonts_changed_p to non-zero if the font loaded
4753 has a character with a smaller width than any other character
4754 before, or if the font loaded has a smaller height than any
4755 other font loaded before. If this happens, it will make a
4756 glyph matrix reallocation necessary. */
4757 fonts_changed_p |= x_compute_min_glyph_bounds (f);
4758 UNBLOCK_INPUT;
4759 return fontp;
4763 /* Load font named FONTNAME of size SIZE for frame F, and return a
4764 pointer to the structure font_info while allocating it dynamically.
4765 If loading fails, return NULL. */
4766 struct font_info *
4767 w32_load_font (f,fontname,size)
4768 struct frame *f;
4769 char * fontname;
4770 int size;
4772 Lisp_Object bdf_fonts;
4773 struct font_info *retval = NULL;
4774 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4776 bdf_fonts = w32_list_bdf_fonts (build_string (fontname), 1);
4778 while (!retval && CONSP (bdf_fonts))
4780 char *bdf_name, *bdf_file;
4781 Lisp_Object bdf_pair;
4782 int i;
4784 bdf_name = SDATA (XCAR (bdf_fonts));
4785 bdf_pair = Fassoc (XCAR (bdf_fonts), Vw32_bdf_filename_alist);
4786 bdf_file = SDATA (XCDR (bdf_pair));
4788 // If the font is already loaded, do not load it again.
4789 for (i = 0; i < dpyinfo->n_fonts; i++)
4791 if ((dpyinfo->font_table[i].name
4792 && !strcmp (dpyinfo->font_table[i].name, bdf_name))
4793 || (dpyinfo->font_table[i].full_name
4794 && !strcmp (dpyinfo->font_table[i].full_name, bdf_name)))
4795 return dpyinfo->font_table + i;
4798 retval = w32_load_bdf_font (f, bdf_name, size, bdf_file);
4800 bdf_fonts = XCDR (bdf_fonts);
4803 if (retval)
4804 return retval;
4806 return w32_load_system_font(f, fontname, size);
4810 void
4811 w32_unload_font (dpyinfo, font)
4812 struct w32_display_info *dpyinfo;
4813 XFontStruct * font;
4815 if (font)
4817 if (font->per_char) xfree (font->per_char);
4818 if (font->bdf) w32_free_bdf_font (font->bdf);
4820 if (font->hfont) DeleteObject(font->hfont);
4821 xfree (font);
4825 /* The font conversion stuff between x and w32 */
4827 /* X font string is as follows (from faces.el)
4828 * (let ((- "[-?]")
4829 * (foundry "[^-]+")
4830 * (family "[^-]+")
4831 * (weight "\\(bold\\|demibold\\|medium\\)") ; 1
4832 * (weight\? "\\([^-]*\\)") ; 1
4833 * (slant "\\([ior]\\)") ; 2
4834 * (slant\? "\\([^-]?\\)") ; 2
4835 * (swidth "\\([^-]*\\)") ; 3
4836 * (adstyle "[^-]*") ; 4
4837 * (pixelsize "[0-9]+")
4838 * (pointsize "[0-9][0-9]+")
4839 * (resx "[0-9][0-9]+")
4840 * (resy "[0-9][0-9]+")
4841 * (spacing "[cmp?*]")
4842 * (avgwidth "[0-9]+")
4843 * (registry "[^-]+")
4844 * (encoding "[^-]+")
4848 static LONG
4849 x_to_w32_weight (lpw)
4850 char * lpw;
4852 if (!lpw) return (FW_DONTCARE);
4854 if (stricmp (lpw,"heavy") == 0) return FW_HEAVY;
4855 else if (stricmp (lpw,"extrabold") == 0) return FW_EXTRABOLD;
4856 else if (stricmp (lpw,"bold") == 0) return FW_BOLD;
4857 else if (stricmp (lpw,"demibold") == 0) return FW_SEMIBOLD;
4858 else if (stricmp (lpw,"semibold") == 0) return FW_SEMIBOLD;
4859 else if (stricmp (lpw,"medium") == 0) return FW_MEDIUM;
4860 else if (stricmp (lpw,"normal") == 0) return FW_NORMAL;
4861 else if (stricmp (lpw,"light") == 0) return FW_LIGHT;
4862 else if (stricmp (lpw,"extralight") == 0) return FW_EXTRALIGHT;
4863 else if (stricmp (lpw,"thin") == 0) return FW_THIN;
4864 else
4865 return FW_DONTCARE;
4869 static char *
4870 w32_to_x_weight (fnweight)
4871 int fnweight;
4873 if (fnweight >= FW_HEAVY) return "heavy";
4874 if (fnweight >= FW_EXTRABOLD) return "extrabold";
4875 if (fnweight >= FW_BOLD) return "bold";
4876 if (fnweight >= FW_SEMIBOLD) return "demibold";
4877 if (fnweight >= FW_MEDIUM) return "medium";
4878 if (fnweight >= FW_NORMAL) return "normal";
4879 if (fnweight >= FW_LIGHT) return "light";
4880 if (fnweight >= FW_EXTRALIGHT) return "extralight";
4881 if (fnweight >= FW_THIN) return "thin";
4882 else
4883 return "*";
4886 static LONG
4887 x_to_w32_charset (lpcs)
4888 char * lpcs;
4890 Lisp_Object this_entry, w32_charset;
4891 char *charset;
4892 int len = strlen (lpcs);
4894 /* Support "*-#nnn" format for unknown charsets. */
4895 if (strncmp (lpcs, "*-#", 3) == 0)
4896 return atoi (lpcs + 3);
4898 /* Handle wildcards by ignoring them; eg. treat "big5*-*" as "big5". */
4899 charset = alloca (len + 1);
4900 strcpy (charset, lpcs);
4901 lpcs = strchr (charset, '*');
4902 if (lpcs)
4903 *lpcs = 0;
4905 /* Look through w32-charset-info-alist for the character set.
4906 Format of each entry is
4907 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
4909 this_entry = Fassoc (build_string(charset), Vw32_charset_info_alist);
4911 if (NILP(this_entry))
4913 /* At startup, we want iso8859-1 fonts to come up properly. */
4914 if (stricmp(charset, "iso8859-1") == 0)
4915 return ANSI_CHARSET;
4916 else
4917 return DEFAULT_CHARSET;
4920 w32_charset = Fcar (Fcdr (this_entry));
4922 /* Translate Lisp symbol to number. */
4923 if (EQ (w32_charset, Qw32_charset_ansi))
4924 return ANSI_CHARSET;
4925 if (EQ (w32_charset, Qw32_charset_symbol))
4926 return SYMBOL_CHARSET;
4927 if (EQ (w32_charset, Qw32_charset_shiftjis))
4928 return SHIFTJIS_CHARSET;
4929 if (EQ (w32_charset, Qw32_charset_hangeul))
4930 return HANGEUL_CHARSET;
4931 if (EQ (w32_charset, Qw32_charset_chinesebig5))
4932 return CHINESEBIG5_CHARSET;
4933 if (EQ (w32_charset, Qw32_charset_gb2312))
4934 return GB2312_CHARSET;
4935 if (EQ (w32_charset, Qw32_charset_oem))
4936 return OEM_CHARSET;
4937 #ifdef JOHAB_CHARSET
4938 if (EQ (w32_charset, Qw32_charset_johab))
4939 return JOHAB_CHARSET;
4940 if (EQ (w32_charset, Qw32_charset_easteurope))
4941 return EASTEUROPE_CHARSET;
4942 if (EQ (w32_charset, Qw32_charset_turkish))
4943 return TURKISH_CHARSET;
4944 if (EQ (w32_charset, Qw32_charset_baltic))
4945 return BALTIC_CHARSET;
4946 if (EQ (w32_charset, Qw32_charset_russian))
4947 return RUSSIAN_CHARSET;
4948 if (EQ (w32_charset, Qw32_charset_arabic))
4949 return ARABIC_CHARSET;
4950 if (EQ (w32_charset, Qw32_charset_greek))
4951 return GREEK_CHARSET;
4952 if (EQ (w32_charset, Qw32_charset_hebrew))
4953 return HEBREW_CHARSET;
4954 if (EQ (w32_charset, Qw32_charset_vietnamese))
4955 return VIETNAMESE_CHARSET;
4956 if (EQ (w32_charset, Qw32_charset_thai))
4957 return THAI_CHARSET;
4958 if (EQ (w32_charset, Qw32_charset_mac))
4959 return MAC_CHARSET;
4960 #endif /* JOHAB_CHARSET */
4961 #ifdef UNICODE_CHARSET
4962 if (EQ (w32_charset, Qw32_charset_unicode))
4963 return UNICODE_CHARSET;
4964 #endif
4966 return DEFAULT_CHARSET;
4970 static char *
4971 w32_to_x_charset (fncharset)
4972 int fncharset;
4974 static char buf[32];
4975 Lisp_Object charset_type;
4977 switch (fncharset)
4979 case ANSI_CHARSET:
4980 /* Handle startup case of w32-charset-info-alist not
4981 being set up yet. */
4982 if (NILP(Vw32_charset_info_alist))
4983 return "iso8859-1";
4984 charset_type = Qw32_charset_ansi;
4985 break;
4986 case DEFAULT_CHARSET:
4987 charset_type = Qw32_charset_default;
4988 break;
4989 case SYMBOL_CHARSET:
4990 charset_type = Qw32_charset_symbol;
4991 break;
4992 case SHIFTJIS_CHARSET:
4993 charset_type = Qw32_charset_shiftjis;
4994 break;
4995 case HANGEUL_CHARSET:
4996 charset_type = Qw32_charset_hangeul;
4997 break;
4998 case GB2312_CHARSET:
4999 charset_type = Qw32_charset_gb2312;
5000 break;
5001 case CHINESEBIG5_CHARSET:
5002 charset_type = Qw32_charset_chinesebig5;
5003 break;
5004 case OEM_CHARSET:
5005 charset_type = Qw32_charset_oem;
5006 break;
5008 /* More recent versions of Windows (95 and NT4.0) define more
5009 character sets. */
5010 #ifdef EASTEUROPE_CHARSET
5011 case EASTEUROPE_CHARSET:
5012 charset_type = Qw32_charset_easteurope;
5013 break;
5014 case TURKISH_CHARSET:
5015 charset_type = Qw32_charset_turkish;
5016 break;
5017 case BALTIC_CHARSET:
5018 charset_type = Qw32_charset_baltic;
5019 break;
5020 case RUSSIAN_CHARSET:
5021 charset_type = Qw32_charset_russian;
5022 break;
5023 case ARABIC_CHARSET:
5024 charset_type = Qw32_charset_arabic;
5025 break;
5026 case GREEK_CHARSET:
5027 charset_type = Qw32_charset_greek;
5028 break;
5029 case HEBREW_CHARSET:
5030 charset_type = Qw32_charset_hebrew;
5031 break;
5032 case VIETNAMESE_CHARSET:
5033 charset_type = Qw32_charset_vietnamese;
5034 break;
5035 case THAI_CHARSET:
5036 charset_type = Qw32_charset_thai;
5037 break;
5038 case MAC_CHARSET:
5039 charset_type = Qw32_charset_mac;
5040 break;
5041 case JOHAB_CHARSET:
5042 charset_type = Qw32_charset_johab;
5043 break;
5044 #endif
5046 #ifdef UNICODE_CHARSET
5047 case UNICODE_CHARSET:
5048 charset_type = Qw32_charset_unicode;
5049 break;
5050 #endif
5051 default:
5052 /* Encode numerical value of unknown charset. */
5053 sprintf (buf, "*-#%u", fncharset);
5054 return buf;
5058 Lisp_Object rest;
5059 char * best_match = NULL;
5061 /* Look through w32-charset-info-alist for the character set.
5062 Prefer ISO codepages, and prefer lower numbers in the ISO
5063 range. Only return charsets for codepages which are installed.
5065 Format of each entry is
5066 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
5068 for (rest = Vw32_charset_info_alist; CONSP (rest); rest = XCDR (rest))
5070 char * x_charset;
5071 Lisp_Object w32_charset;
5072 Lisp_Object codepage;
5074 Lisp_Object this_entry = XCAR (rest);
5076 /* Skip invalid entries in alist. */
5077 if (!CONSP (this_entry) || !STRINGP (XCAR (this_entry))
5078 || !CONSP (XCDR (this_entry))
5079 || !SYMBOLP (XCAR (XCDR (this_entry))))
5080 continue;
5082 x_charset = SDATA (XCAR (this_entry));
5083 w32_charset = XCAR (XCDR (this_entry));
5084 codepage = XCDR (XCDR (this_entry));
5086 /* Look for Same charset and a valid codepage (or non-int
5087 which means ignore). */
5088 if (EQ (w32_charset, charset_type)
5089 && (!INTEGERP (codepage) || XINT (codepage) == CP_DEFAULT
5090 || IsValidCodePage (XINT (codepage))))
5092 /* If we don't have a match already, then this is the
5093 best. */
5094 if (!best_match)
5095 best_match = x_charset;
5096 /* If this is an ISO codepage, and the best so far isn't,
5097 then this is better. */
5098 else if (strnicmp (best_match, "iso", 3) != 0
5099 && strnicmp (x_charset, "iso", 3) == 0)
5100 best_match = x_charset;
5101 /* If both are ISO8859 codepages, choose the one with the
5102 lowest number in the encoding field. */
5103 else if (strnicmp (best_match, "iso8859-", 8) == 0
5104 && strnicmp (x_charset, "iso8859-", 8) == 0)
5106 int best_enc = atoi (best_match + 8);
5107 int this_enc = atoi (x_charset + 8);
5108 if (this_enc > 0 && this_enc < best_enc)
5109 best_match = x_charset;
5114 /* If no match, encode the numeric value. */
5115 if (!best_match)
5117 sprintf (buf, "*-#%u", fncharset);
5118 return buf;
5121 strncpy(buf, best_match, 31);
5122 buf[31] = '\0';
5123 return buf;
5128 /* Return all the X charsets that map to a font. */
5129 static Lisp_Object
5130 w32_to_all_x_charsets (fncharset)
5131 int fncharset;
5133 static char buf[32];
5134 Lisp_Object charset_type;
5135 Lisp_Object retval = Qnil;
5137 switch (fncharset)
5139 case ANSI_CHARSET:
5140 /* Handle startup case of w32-charset-info-alist not
5141 being set up yet. */
5142 if (NILP(Vw32_charset_info_alist))
5143 return Fcons (build_string ("iso8859-1"), Qnil);
5145 charset_type = Qw32_charset_ansi;
5146 break;
5147 case DEFAULT_CHARSET:
5148 charset_type = Qw32_charset_default;
5149 break;
5150 case SYMBOL_CHARSET:
5151 charset_type = Qw32_charset_symbol;
5152 break;
5153 case SHIFTJIS_CHARSET:
5154 charset_type = Qw32_charset_shiftjis;
5155 break;
5156 case HANGEUL_CHARSET:
5157 charset_type = Qw32_charset_hangeul;
5158 break;
5159 case GB2312_CHARSET:
5160 charset_type = Qw32_charset_gb2312;
5161 break;
5162 case CHINESEBIG5_CHARSET:
5163 charset_type = Qw32_charset_chinesebig5;
5164 break;
5165 case OEM_CHARSET:
5166 charset_type = Qw32_charset_oem;
5167 break;
5169 /* More recent versions of Windows (95 and NT4.0) define more
5170 character sets. */
5171 #ifdef EASTEUROPE_CHARSET
5172 case EASTEUROPE_CHARSET:
5173 charset_type = Qw32_charset_easteurope;
5174 break;
5175 case TURKISH_CHARSET:
5176 charset_type = Qw32_charset_turkish;
5177 break;
5178 case BALTIC_CHARSET:
5179 charset_type = Qw32_charset_baltic;
5180 break;
5181 case RUSSIAN_CHARSET:
5182 charset_type = Qw32_charset_russian;
5183 break;
5184 case ARABIC_CHARSET:
5185 charset_type = Qw32_charset_arabic;
5186 break;
5187 case GREEK_CHARSET:
5188 charset_type = Qw32_charset_greek;
5189 break;
5190 case HEBREW_CHARSET:
5191 charset_type = Qw32_charset_hebrew;
5192 break;
5193 case VIETNAMESE_CHARSET:
5194 charset_type = Qw32_charset_vietnamese;
5195 break;
5196 case THAI_CHARSET:
5197 charset_type = Qw32_charset_thai;
5198 break;
5199 case MAC_CHARSET:
5200 charset_type = Qw32_charset_mac;
5201 break;
5202 case JOHAB_CHARSET:
5203 charset_type = Qw32_charset_johab;
5204 break;
5205 #endif
5207 #ifdef UNICODE_CHARSET
5208 case UNICODE_CHARSET:
5209 charset_type = Qw32_charset_unicode;
5210 break;
5211 #endif
5212 default:
5213 /* Encode numerical value of unknown charset. */
5214 sprintf (buf, "*-#%u", fncharset);
5215 return Fcons (build_string (buf), Qnil);
5219 Lisp_Object rest;
5220 /* Look through w32-charset-info-alist for the character set.
5221 Only return charsets for codepages which are installed.
5223 Format of each entry in Vw32_charset_info_alist is
5224 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
5226 for (rest = Vw32_charset_info_alist; CONSP (rest); rest = XCDR (rest))
5228 Lisp_Object x_charset;
5229 Lisp_Object w32_charset;
5230 Lisp_Object codepage;
5232 Lisp_Object this_entry = XCAR (rest);
5234 /* Skip invalid entries in alist. */
5235 if (!CONSP (this_entry) || !STRINGP (XCAR (this_entry))
5236 || !CONSP (XCDR (this_entry))
5237 || !SYMBOLP (XCAR (XCDR (this_entry))))
5238 continue;
5240 x_charset = XCAR (this_entry);
5241 w32_charset = XCAR (XCDR (this_entry));
5242 codepage = XCDR (XCDR (this_entry));
5244 /* Look for Same charset and a valid codepage (or non-int
5245 which means ignore). */
5246 if (EQ (w32_charset, charset_type)
5247 && (!INTEGERP (codepage) || XINT (codepage) == CP_DEFAULT
5248 || IsValidCodePage (XINT (codepage))))
5250 retval = Fcons (x_charset, retval);
5254 /* If no match, encode the numeric value. */
5255 if (NILP (retval))
5257 sprintf (buf, "*-#%u", fncharset);
5258 return Fcons (build_string (buf), Qnil);
5261 return retval;
5265 /* Get the Windows codepage corresponding to the specified font. The
5266 charset info in the font name is used to look up
5267 w32-charset-to-codepage-alist. */
5269 w32_codepage_for_font (char *fontname)
5271 Lisp_Object codepage, entry;
5272 char *charset_str, *charset, *end;
5274 if (NILP (Vw32_charset_info_alist))
5275 return CP_DEFAULT;
5277 /* Extract charset part of font string. */
5278 charset = xlfd_charset_of_font (fontname);
5280 if (!charset)
5281 return CP_UNKNOWN;
5283 charset_str = (char *) alloca (strlen (charset) + 1);
5284 strcpy (charset_str, charset);
5286 #if 0
5287 /* Remove leading "*-". */
5288 if (strncmp ("*-", charset_str, 2) == 0)
5289 charset = charset_str + 2;
5290 else
5291 #endif
5292 charset = charset_str;
5294 /* Stop match at wildcard (including preceding '-'). */
5295 if (end = strchr (charset, '*'))
5297 if (end > charset && *(end-1) == '-')
5298 end--;
5299 *end = '\0';
5302 entry = Fassoc (build_string(charset), Vw32_charset_info_alist);
5303 if (NILP (entry))
5304 return CP_UNKNOWN;
5306 codepage = Fcdr (Fcdr (entry));
5308 if (NILP (codepage))
5309 return CP_8BIT;
5310 else if (XFASTINT (codepage) == XFASTINT (Qt))
5311 return CP_UNICODE;
5312 else if (INTEGERP (codepage))
5313 return XINT (codepage);
5314 else
5315 return CP_UNKNOWN;
5319 static BOOL
5320 w32_to_x_font (lplogfont, lpxstr, len, specific_charset)
5321 LOGFONT * lplogfont;
5322 char * lpxstr;
5323 int len;
5324 char * specific_charset;
5326 char* fonttype;
5327 char *fontname;
5328 char height_pixels[8];
5329 char height_dpi[8];
5330 char width_pixels[8];
5331 char *fontname_dash;
5332 int display_resy = (int) one_w32_display_info.resy;
5333 int display_resx = (int) one_w32_display_info.resx;
5334 int bufsz;
5335 struct coding_system coding;
5337 if (!lpxstr) abort ();
5339 if (!lplogfont)
5340 return FALSE;
5342 if (lplogfont->lfOutPrecision == OUT_STRING_PRECIS)
5343 fonttype = "raster";
5344 else if (lplogfont->lfOutPrecision == OUT_STROKE_PRECIS)
5345 fonttype = "outline";
5346 else
5347 fonttype = "unknown";
5349 setup_coding_system (Fcheck_coding_system (Vlocale_coding_system),
5350 &coding);
5351 coding.src_multibyte = 0;
5352 coding.dst_multibyte = 1;
5353 coding.mode |= CODING_MODE_LAST_BLOCK;
5354 /* We explicitely disable composition handling because selection
5355 data should not contain any composition sequence. */
5356 coding.composing = COMPOSITION_DISABLED;
5357 bufsz = decoding_buffer_size (&coding, LF_FACESIZE);
5359 fontname = alloca(sizeof(*fontname) * bufsz);
5360 decode_coding (&coding, lplogfont->lfFaceName, fontname,
5361 strlen(lplogfont->lfFaceName), bufsz - 1);
5362 *(fontname + coding.produced) = '\0';
5364 /* Replace dashes with underscores so the dashes are not
5365 misinterpreted. */
5366 fontname_dash = fontname;
5367 while (fontname_dash = strchr (fontname_dash, '-'))
5368 *fontname_dash = '_';
5370 if (lplogfont->lfHeight)
5372 sprintf (height_pixels, "%u", abs (lplogfont->lfHeight));
5373 sprintf (height_dpi, "%u",
5374 abs (lplogfont->lfHeight) * 720 / display_resy);
5376 else
5378 strcpy (height_pixels, "*");
5379 strcpy (height_dpi, "*");
5382 #if 0 /* Never put the width in the xfld. It fails on fonts with
5383 double-width characters. */
5384 if (lplogfont->lfWidth)
5385 sprintf (width_pixels, "%u", lplogfont->lfWidth * 10);
5386 else
5387 #endif
5388 strcpy (width_pixels, "*");
5390 _snprintf (lpxstr, len - 1,
5391 "-%s-%s-%s-%c-normal-normal-%s-%s-%d-%d-%c-%s-%s",
5392 fonttype, /* foundry */
5393 fontname, /* family */
5394 w32_to_x_weight (lplogfont->lfWeight), /* weight */
5395 lplogfont->lfItalic?'i':'r', /* slant */
5396 /* setwidth name */
5397 /* add style name */
5398 height_pixels, /* pixel size */
5399 height_dpi, /* point size */
5400 display_resx, /* resx */
5401 display_resy, /* resy */
5402 ((lplogfont->lfPitchAndFamily & 0x3) == VARIABLE_PITCH)
5403 ? 'p' : 'c', /* spacing */
5404 width_pixels, /* avg width */
5405 specific_charset ? specific_charset
5406 : w32_to_x_charset (lplogfont->lfCharSet)
5407 /* charset registry and encoding */
5410 lpxstr[len - 1] = 0; /* just to be sure */
5411 return (TRUE);
5414 static BOOL
5415 x_to_w32_font (lpxstr, lplogfont)
5416 char * lpxstr;
5417 LOGFONT * lplogfont;
5419 struct coding_system coding;
5421 if (!lplogfont) return (FALSE);
5423 memset (lplogfont, 0, sizeof (*lplogfont));
5425 /* Set default value for each field. */
5426 #if 1
5427 lplogfont->lfOutPrecision = OUT_DEFAULT_PRECIS;
5428 lplogfont->lfClipPrecision = CLIP_DEFAULT_PRECIS;
5429 lplogfont->lfQuality = DEFAULT_QUALITY;
5430 #else
5431 /* go for maximum quality */
5432 lplogfont->lfOutPrecision = OUT_STROKE_PRECIS;
5433 lplogfont->lfClipPrecision = CLIP_STROKE_PRECIS;
5434 lplogfont->lfQuality = PROOF_QUALITY;
5435 #endif
5437 lplogfont->lfCharSet = DEFAULT_CHARSET;
5438 lplogfont->lfWeight = FW_DONTCARE;
5439 lplogfont->lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
5441 if (!lpxstr)
5442 return FALSE;
5444 /* Provide a simple escape mechanism for specifying Windows font names
5445 * directly -- if font spec does not beginning with '-', assume this
5446 * format:
5447 * "<font name>[:height in pixels[:width in pixels[:weight]]]"
5450 if (*lpxstr == '-')
5452 int fields, tem;
5453 char name[50], weight[20], slant, pitch, pixels[10], height[10],
5454 width[10], resy[10], remainder[50];
5455 char * encoding;
5456 int dpi = (int) one_w32_display_info.resy;
5458 fields = sscanf (lpxstr,
5459 "-%*[^-]-%49[^-]-%19[^-]-%c-%*[^-]-%*[^-]-%9[^-]-%9[^-]-%*[^-]-%9[^-]-%c-%9[^-]-%49s",
5460 name, weight, &slant, pixels, height, resy, &pitch, width, remainder);
5461 if (fields == EOF)
5462 return (FALSE);
5464 /* In the general case when wildcards cover more than one field,
5465 we don't know which field is which, so don't fill any in.
5466 However, we need to cope with this particular form, which is
5467 generated by font_list_1 (invoked by try_font_list):
5468 "-raster-6x10-*-gb2312*-*"
5469 and make sure to correctly parse the charset field. */
5470 if (fields == 3)
5472 fields = sscanf (lpxstr,
5473 "-%*[^-]-%49[^-]-*-%49s",
5474 name, remainder);
5476 else if (fields < 9)
5478 fields = 0;
5479 remainder[0] = 0;
5482 if (fields > 0 && name[0] != '*')
5484 int bufsize;
5485 unsigned char *buf;
5487 setup_coding_system
5488 (Fcheck_coding_system (Vlocale_coding_system), &coding);
5489 coding.src_multibyte = 1;
5490 coding.dst_multibyte = 0;
5491 /* Need to set COMPOSITION_DISABLED, otherwise Emacs crashes in
5492 encode_coding_iso2022 trying to dereference a null pointer. */
5493 coding.composing = COMPOSITION_DISABLED;
5494 if (coding.type == coding_type_iso2022)
5495 coding.flags |= CODING_FLAG_ISO_SAFE;
5496 bufsize = encoding_buffer_size (&coding, strlen (name));
5497 buf = (unsigned char *) alloca (bufsize);
5498 coding.mode |= CODING_MODE_LAST_BLOCK;
5499 encode_coding (&coding, name, buf, strlen (name), bufsize);
5500 if (coding.produced >= LF_FACESIZE)
5501 coding.produced = LF_FACESIZE - 1;
5502 buf[coding.produced] = 0;
5503 strcpy (lplogfont->lfFaceName, buf);
5505 else
5507 lplogfont->lfFaceName[0] = '\0';
5510 fields--;
5512 lplogfont->lfWeight = x_to_w32_weight ((fields > 0 ? weight : ""));
5514 fields--;
5516 lplogfont->lfItalic = (fields > 0 && slant == 'i');
5518 fields--;
5520 if (fields > 0 && pixels[0] != '*')
5521 lplogfont->lfHeight = atoi (pixels);
5523 fields--;
5524 fields--;
5525 if (fields > 0 && resy[0] != '*')
5527 tem = atoi (resy);
5528 if (tem > 0) dpi = tem;
5531 if (fields > -1 && lplogfont->lfHeight == 0 && height[0] != '*')
5532 lplogfont->lfHeight = atoi (height) * dpi / 720;
5534 if (fields > 0)
5535 lplogfont->lfPitchAndFamily =
5536 (fields > 0 && pitch == 'p') ? VARIABLE_PITCH : FIXED_PITCH;
5538 fields--;
5540 if (fields > 0 && width[0] != '*')
5541 lplogfont->lfWidth = atoi (width) / 10;
5543 fields--;
5545 /* Strip the trailing '-' if present. (it shouldn't be, as it
5546 fails the test against xlfd-tight-regexp in fontset.el). */
5548 int len = strlen (remainder);
5549 if (len > 0 && remainder[len-1] == '-')
5550 remainder[len-1] = 0;
5552 encoding = remainder;
5553 #if 0
5554 if (strncmp (encoding, "*-", 2) == 0)
5555 encoding += 2;
5556 #endif
5557 lplogfont->lfCharSet = x_to_w32_charset (encoding);
5559 else
5561 int fields;
5562 char name[100], height[10], width[10], weight[20];
5564 fields = sscanf (lpxstr,
5565 "%99[^:]:%9[^:]:%9[^:]:%19s",
5566 name, height, width, weight);
5568 if (fields == EOF) return (FALSE);
5570 if (fields > 0)
5572 strncpy (lplogfont->lfFaceName,name, LF_FACESIZE);
5573 lplogfont->lfFaceName[LF_FACESIZE-1] = 0;
5575 else
5577 lplogfont->lfFaceName[0] = 0;
5580 fields--;
5582 if (fields > 0)
5583 lplogfont->lfHeight = atoi (height);
5585 fields--;
5587 if (fields > 0)
5588 lplogfont->lfWidth = atoi (width);
5590 fields--;
5592 lplogfont->lfWeight = x_to_w32_weight ((fields > 0 ? weight : ""));
5595 /* This makes TrueType fonts work better. */
5596 lplogfont->lfHeight = - abs (lplogfont->lfHeight);
5598 return (TRUE);
5601 /* Strip the pixel height and point height from the given xlfd, and
5602 return the pixel height. If no pixel height is specified, calculate
5603 one from the point height, or if that isn't defined either, return
5604 0 (which usually signifies a scalable font).
5606 static int
5607 xlfd_strip_height (char *fontname)
5609 int pixel_height, field_number;
5610 char *read_from, *write_to;
5612 xassert (fontname);
5614 pixel_height = field_number = 0;
5615 write_to = NULL;
5617 /* Look for height fields. */
5618 for (read_from = fontname; *read_from; read_from++)
5620 if (*read_from == '-')
5622 field_number++;
5623 if (field_number == 7) /* Pixel height. */
5625 read_from++;
5626 write_to = read_from;
5628 /* Find end of field. */
5629 for (;*read_from && *read_from != '-'; read_from++)
5632 /* Split the fontname at end of field. */
5633 if (*read_from)
5635 *read_from = '\0';
5636 read_from++;
5638 pixel_height = atoi (write_to);
5639 /* Blank out field. */
5640 if (read_from > write_to)
5642 *write_to = '-';
5643 write_to++;
5645 /* If the pixel height field is at the end (partial xlfd),
5646 return now. */
5647 else
5648 return pixel_height;
5650 /* If we got a pixel height, the point height can be
5651 ignored. Just blank it out and break now. */
5652 if (pixel_height)
5654 /* Find end of point size field. */
5655 for (; *read_from && *read_from != '-'; read_from++)
5658 if (*read_from)
5659 read_from++;
5661 /* Blank out the point size field. */
5662 if (read_from > write_to)
5664 *write_to = '-';
5665 write_to++;
5667 else
5668 return pixel_height;
5670 break;
5672 /* If the point height is already blank, break now. */
5673 if (*read_from == '-')
5675 read_from++;
5676 break;
5679 else if (field_number == 8)
5681 /* If we didn't get a pixel height, try to get the point
5682 height and convert that. */
5683 int point_size;
5684 char *point_size_start = read_from++;
5686 /* Find end of field. */
5687 for (; *read_from && *read_from != '-'; read_from++)
5690 if (*read_from)
5692 *read_from = '\0';
5693 read_from++;
5696 point_size = atoi (point_size_start);
5698 /* Convert to pixel height. */
5699 pixel_height = point_size
5700 * one_w32_display_info.height_in / 720;
5702 /* Blank out this field and break. */
5703 *write_to = '-';
5704 write_to++;
5705 break;
5710 /* Shift the rest of the font spec into place. */
5711 if (write_to && read_from > write_to)
5713 for (; *read_from; read_from++, write_to++)
5714 *write_to = *read_from;
5715 *write_to = '\0';
5718 return pixel_height;
5721 /* Assume parameter 1 is fully qualified, no wildcards. */
5722 static BOOL
5723 w32_font_match (fontname, pattern)
5724 char * fontname;
5725 char * pattern;
5727 char *ptr;
5728 char *font_name_copy;
5729 char *regex = alloca (strlen (pattern) * 2 + 3);
5731 font_name_copy = alloca (strlen (fontname) + 1);
5732 strcpy (font_name_copy, fontname);
5734 ptr = regex;
5735 *ptr++ = '^';
5737 /* Turn pattern into a regexp and do a regexp match. */
5738 for (; *pattern; pattern++)
5740 if (*pattern == '?')
5741 *ptr++ = '.';
5742 else if (*pattern == '*')
5744 *ptr++ = '.';
5745 *ptr++ = '*';
5747 else
5748 *ptr++ = *pattern;
5750 *ptr = '$';
5751 *(ptr + 1) = '\0';
5753 /* Strip out font heights and compare them seperately, since
5754 rounding error can cause mismatches. This also allows a
5755 comparison between a font that declares only a pixel height and a
5756 pattern that declares the point height.
5759 int font_height, pattern_height;
5761 font_height = xlfd_strip_height (font_name_copy);
5762 pattern_height = xlfd_strip_height (regex);
5764 /* Compare now, and don't bother doing expensive regexp matching
5765 if the heights differ. */
5766 if (font_height && pattern_height && (font_height != pattern_height))
5767 return FALSE;
5770 return (fast_string_match_ignore_case (build_string (regex),
5771 build_string(font_name_copy)) >= 0);
5774 /* Callback functions, and a structure holding info they need, for
5775 listing system fonts on W32. We need one set of functions to do the
5776 job properly, but these don't work on NT 3.51 and earlier, so we
5777 have a second set which don't handle character sets properly to
5778 fall back on.
5780 In both cases, there are two passes made. The first pass gets one
5781 font from each family, the second pass lists all the fonts from
5782 each family. */
5784 typedef struct enumfont_t
5786 HDC hdc;
5787 int numFonts;
5788 LOGFONT logfont;
5789 XFontStruct *size_ref;
5790 Lisp_Object pattern;
5791 Lisp_Object list;
5792 } enumfont_t;
5795 static void
5796 enum_font_maybe_add_to_list (enumfont_t *, LOGFONT *, char *, Lisp_Object);
5799 static int CALLBACK
5800 enum_font_cb2 (lplf, lptm, FontType, lpef)
5801 ENUMLOGFONT * lplf;
5802 NEWTEXTMETRIC * lptm;
5803 int FontType;
5804 enumfont_t * lpef;
5806 /* Ignore struck out and underlined versions of fonts. */
5807 if (lplf->elfLogFont.lfStrikeOut || lplf->elfLogFont.lfUnderline)
5808 return 1;
5810 /* Only return fonts with names starting with @ if they were
5811 explicitly specified, since Microsoft uses an initial @ to
5812 denote fonts for vertical writing, without providing a more
5813 convenient way of identifying them. */
5814 if (lplf->elfLogFont.lfFaceName[0] == '@'
5815 && lpef->logfont.lfFaceName[0] != '@')
5816 return 1;
5818 /* Check that the character set matches if it was specified */
5819 if (lpef->logfont.lfCharSet != DEFAULT_CHARSET &&
5820 lplf->elfLogFont.lfCharSet != lpef->logfont.lfCharSet)
5821 return 1;
5823 if (FontType == RASTER_FONTTYPE)
5825 /* DBCS raster fonts have problems displaying, so skip them. */
5826 int charset = lplf->elfLogFont.lfCharSet;
5827 if (charset == SHIFTJIS_CHARSET
5828 || charset == HANGEUL_CHARSET
5829 || charset == CHINESEBIG5_CHARSET
5830 || charset == GB2312_CHARSET
5831 #ifdef JOHAB_CHARSET
5832 || charset == JOHAB_CHARSET
5833 #endif
5835 return 1;
5839 char buf[100];
5840 Lisp_Object width = Qnil;
5841 Lisp_Object charset_list = Qnil;
5842 char *charset = NULL;
5844 /* Truetype fonts do not report their true metrics until loaded */
5845 if (FontType != RASTER_FONTTYPE)
5847 if (!NILP (lpef->pattern))
5849 /* Scalable fonts are as big as you want them to be. */
5850 lplf->elfLogFont.lfHeight = lpef->logfont.lfHeight;
5851 lplf->elfLogFont.lfWidth = lpef->logfont.lfWidth;
5852 width = make_number (lpef->logfont.lfWidth);
5854 else
5856 lplf->elfLogFont.lfHeight = 0;
5857 lplf->elfLogFont.lfWidth = 0;
5861 /* Make sure the height used here is the same as everywhere
5862 else (ie character height, not cell height). */
5863 if (lplf->elfLogFont.lfHeight > 0)
5865 /* lptm can be trusted for RASTER fonts, but not scalable ones. */
5866 if (FontType == RASTER_FONTTYPE)
5867 lplf->elfLogFont.lfHeight = lptm->tmInternalLeading - lptm->tmHeight;
5868 else
5869 lplf->elfLogFont.lfHeight = -lplf->elfLogFont.lfHeight;
5872 if (!NILP (lpef->pattern))
5874 charset = xlfd_charset_of_font (SDATA (lpef->pattern));
5876 /* We already checked charsets above, but DEFAULT_CHARSET
5877 slipped through. So only allow exact matches for DEFAULT_CHARSET. */
5878 if (charset
5879 && strncmp (charset, "*-*", 3) != 0
5880 && lpef->logfont.lfCharSet == DEFAULT_CHARSET
5881 && strcmp (charset, w32_to_x_charset (DEFAULT_CHARSET)) != 0)
5882 return 1;
5885 if (charset)
5886 charset_list = Fcons (build_string (charset), Qnil);
5887 else
5888 charset_list = w32_to_all_x_charsets (lplf->elfLogFont.lfCharSet);
5890 /* Loop through the charsets. */
5891 for ( ; CONSP (charset_list); charset_list = Fcdr (charset_list))
5893 Lisp_Object this_charset = Fcar (charset_list);
5894 charset = SDATA (this_charset);
5896 /* List bold and italic variations if w32-enable-synthesized-fonts
5897 is non-nil and this is a plain font. */
5898 if (w32_enable_synthesized_fonts
5899 && lplf->elfLogFont.lfWeight == FW_NORMAL
5900 && lplf->elfLogFont.lfItalic == FALSE)
5902 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5903 charset, width);
5904 /* bold. */
5905 lplf->elfLogFont.lfWeight = FW_BOLD;
5906 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5907 charset, width);
5908 /* bold italic. */
5909 lplf->elfLogFont.lfItalic = TRUE;
5910 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5911 charset, width);
5912 /* italic. */
5913 lplf->elfLogFont.lfWeight = FW_NORMAL;
5914 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5915 charset, width);
5917 else
5918 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5919 charset, width);
5923 return 1;
5926 static void
5927 enum_font_maybe_add_to_list (lpef, logfont, match_charset, width)
5928 enumfont_t * lpef;
5929 LOGFONT * logfont;
5930 char * match_charset;
5931 Lisp_Object width;
5933 char buf[100];
5935 if (!w32_to_x_font (logfont, buf, 100, match_charset))
5936 return;
5938 if (NILP (lpef->pattern)
5939 || w32_font_match (buf, SDATA (lpef->pattern)))
5941 /* Check if we already listed this font. This may happen if
5942 w32_enable_synthesized_fonts is non-nil, and there are real
5943 bold and italic versions of the font. */
5944 Lisp_Object font_name = build_string (buf);
5945 if (NILP (Fmember (font_name, lpef->list)))
5947 Lisp_Object entry = Fcons (font_name, width);
5948 lpef->list = Fcons (entry, lpef->list);
5949 lpef->numFonts++;
5955 static int CALLBACK
5956 enum_font_cb1 (lplf, lptm, FontType, lpef)
5957 ENUMLOGFONT * lplf;
5958 NEWTEXTMETRIC * lptm;
5959 int FontType;
5960 enumfont_t * lpef;
5962 return EnumFontFamilies (lpef->hdc,
5963 lplf->elfLogFont.lfFaceName,
5964 (FONTENUMPROC) enum_font_cb2,
5965 (LPARAM) lpef);
5969 static int CALLBACK
5970 enum_fontex_cb2 (lplf, lptm, font_type, lpef)
5971 ENUMLOGFONTEX * lplf;
5972 NEWTEXTMETRICEX * lptm;
5973 int font_type;
5974 enumfont_t * lpef;
5976 /* We are not interested in the extra info we get back from the 'Ex
5977 version - only the fact that we get character set variations
5978 enumerated seperately. */
5979 return enum_font_cb2 ((ENUMLOGFONT *) lplf, (NEWTEXTMETRIC *) lptm,
5980 font_type, lpef);
5983 static int CALLBACK
5984 enum_fontex_cb1 (lplf, lptm, font_type, lpef)
5985 ENUMLOGFONTEX * lplf;
5986 NEWTEXTMETRICEX * lptm;
5987 int font_type;
5988 enumfont_t * lpef;
5990 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
5991 FARPROC enum_font_families_ex
5992 = GetProcAddress ( gdi32, "EnumFontFamiliesExA");
5993 /* We don't really expect EnumFontFamiliesEx to disappear once we
5994 get here, so don't bother handling it gracefully. */
5995 if (enum_font_families_ex == NULL)
5996 error ("gdi32.dll has disappeared!");
5997 return enum_font_families_ex (lpef->hdc,
5998 &lplf->elfLogFont,
5999 (FONTENUMPROC) enum_fontex_cb2,
6000 (LPARAM) lpef, 0);
6003 /* Interface to fontset handler. (adapted from mw32font.c in Meadow
6004 and xterm.c in Emacs 20.3) */
6006 static Lisp_Object w32_list_bdf_fonts (Lisp_Object pattern, int max_names)
6008 char *fontname, *ptnstr;
6009 Lisp_Object list, tem, newlist = Qnil;
6010 int n_fonts = 0;
6012 list = Vw32_bdf_filename_alist;
6013 ptnstr = SDATA (pattern);
6015 for ( ; CONSP (list); list = XCDR (list))
6017 tem = XCAR (list);
6018 if (CONSP (tem))
6019 fontname = SDATA (XCAR (tem));
6020 else if (STRINGP (tem))
6021 fontname = SDATA (tem);
6022 else
6023 continue;
6025 if (w32_font_match (fontname, ptnstr))
6027 newlist = Fcons (XCAR (tem), newlist);
6028 n_fonts++;
6029 if (max_names >= 0 && n_fonts >= max_names)
6030 break;
6034 return newlist;
6038 /* Return a list of names of available fonts matching PATTERN on frame
6039 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
6040 to be listed. Frame F NULL means we have not yet created any
6041 frame, which means we can't get proper size info, as we don't have
6042 a device context to use for GetTextMetrics.
6043 MAXNAMES sets a limit on how many fonts to match. If MAXNAMES is
6044 negative, then all matching fonts are returned. */
6046 Lisp_Object
6047 w32_list_fonts (f, pattern, size, maxnames)
6048 struct frame *f;
6049 Lisp_Object pattern;
6050 int size;
6051 int maxnames;
6053 Lisp_Object patterns, key = Qnil, tem, tpat;
6054 Lisp_Object list = Qnil, newlist = Qnil, second_best = Qnil;
6055 struct w32_display_info *dpyinfo = &one_w32_display_info;
6056 int n_fonts = 0;
6058 patterns = Fassoc (pattern, Valternate_fontname_alist);
6059 if (NILP (patterns))
6060 patterns = Fcons (pattern, Qnil);
6062 for (; CONSP (patterns); patterns = XCDR (patterns))
6064 enumfont_t ef;
6065 int codepage;
6067 tpat = XCAR (patterns);
6069 if (!STRINGP (tpat))
6070 continue;
6072 /* Avoid expensive EnumFontFamilies functions if we are not
6073 going to be able to output one of these anyway. */
6074 codepage = w32_codepage_for_font (SDATA (tpat));
6075 if (codepage != CP_8BIT && codepage != CP_UNICODE
6076 && codepage != CP_DEFAULT && codepage != CP_UNKNOWN
6077 && !IsValidCodePage(codepage))
6078 continue;
6080 /* See if we cached the result for this particular query.
6081 The cache is an alist of the form:
6082 ((PATTERN (FONTNAME . WIDTH) ...) ...)
6084 if (tem = XCDR (dpyinfo->name_list_element),
6085 !NILP (list = Fassoc (tpat, tem)))
6087 list = Fcdr_safe (list);
6088 /* We have a cached list. Don't have to get the list again. */
6089 goto label_cached;
6092 BLOCK_INPUT;
6093 /* At first, put PATTERN in the cache. */
6094 ef.pattern = tpat;
6095 ef.list = Qnil;
6096 ef.numFonts = 0;
6098 /* Use EnumFontFamiliesEx where it is available, as it knows
6099 about character sets. Fall back to EnumFontFamilies for
6100 older versions of NT that don't support the 'Ex function. */
6101 x_to_w32_font (SDATA (tpat), &ef.logfont);
6103 LOGFONT font_match_pattern;
6104 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
6105 FARPROC enum_font_families_ex
6106 = GetProcAddress ( gdi32, "EnumFontFamiliesExA");
6108 /* We do our own pattern matching so we can handle wildcards. */
6109 font_match_pattern.lfFaceName[0] = 0;
6110 font_match_pattern.lfPitchAndFamily = 0;
6111 /* We can use the charset, because if it is a wildcard it will
6112 be DEFAULT_CHARSET anyway. */
6113 font_match_pattern.lfCharSet = ef.logfont.lfCharSet;
6115 ef.hdc = GetDC (dpyinfo->root_window);
6117 if (enum_font_families_ex)
6118 enum_font_families_ex (ef.hdc,
6119 &font_match_pattern,
6120 (FONTENUMPROC) enum_fontex_cb1,
6121 (LPARAM) &ef, 0);
6122 else
6123 EnumFontFamilies (ef.hdc, NULL, (FONTENUMPROC) enum_font_cb1,
6124 (LPARAM)&ef);
6126 ReleaseDC (dpyinfo->root_window, ef.hdc);
6129 UNBLOCK_INPUT;
6130 list = ef.list;
6132 /* Make a list of the fonts we got back.
6133 Store that in the font cache for the display. */
6134 XSETCDR (dpyinfo->name_list_element,
6135 Fcons (Fcons (tpat, list),
6136 XCDR (dpyinfo->name_list_element)));
6138 label_cached:
6139 if (NILP (list)) continue; /* Try the remaining alternatives. */
6141 newlist = second_best = Qnil;
6143 /* Make a list of the fonts that have the right width. */
6144 for (; CONSP (list); list = XCDR (list))
6146 int found_size;
6147 tem = XCAR (list);
6149 if (!CONSP (tem))
6150 continue;
6151 if (NILP (XCAR (tem)))
6152 continue;
6153 if (!size)
6155 newlist = Fcons (XCAR (tem), newlist);
6156 n_fonts++;
6157 if (maxnames >= 0 && n_fonts >= maxnames)
6158 break;
6159 else
6160 continue;
6162 if (!INTEGERP (XCDR (tem)))
6164 /* Since we don't yet know the size of the font, we must
6165 load it and try GetTextMetrics. */
6166 W32FontStruct thisinfo;
6167 LOGFONT lf;
6168 HDC hdc;
6169 HANDLE oldobj;
6171 if (!x_to_w32_font (SDATA (XCAR (tem)), &lf))
6172 continue;
6174 BLOCK_INPUT;
6175 thisinfo.bdf = NULL;
6176 thisinfo.hfont = CreateFontIndirect (&lf);
6177 if (thisinfo.hfont == NULL)
6178 continue;
6180 hdc = GetDC (dpyinfo->root_window);
6181 oldobj = SelectObject (hdc, thisinfo.hfont);
6182 if (GetTextMetrics (hdc, &thisinfo.tm))
6183 XSETCDR (tem, make_number (FONT_WIDTH (&thisinfo)));
6184 else
6185 XSETCDR (tem, make_number (0));
6186 SelectObject (hdc, oldobj);
6187 ReleaseDC (dpyinfo->root_window, hdc);
6188 DeleteObject(thisinfo.hfont);
6189 UNBLOCK_INPUT;
6191 found_size = XINT (XCDR (tem));
6192 if (found_size == size)
6194 newlist = Fcons (XCAR (tem), newlist);
6195 n_fonts++;
6196 if (maxnames >= 0 && n_fonts >= maxnames)
6197 break;
6199 /* keep track of the closest matching size in case
6200 no exact match is found. */
6201 else if (found_size > 0)
6203 if (NILP (second_best))
6204 second_best = tem;
6206 else if (found_size < size)
6208 if (XINT (XCDR (second_best)) > size
6209 || XINT (XCDR (second_best)) < found_size)
6210 second_best = tem;
6212 else
6214 if (XINT (XCDR (second_best)) > size
6215 && XINT (XCDR (second_best)) >
6216 found_size)
6217 second_best = tem;
6222 if (!NILP (newlist))
6223 break;
6224 else if (!NILP (second_best))
6226 newlist = Fcons (XCAR (second_best), Qnil);
6227 break;
6231 /* Include any bdf fonts. */
6232 if (n_fonts < maxnames || maxnames < 0)
6234 Lisp_Object combined[2];
6235 combined[0] = w32_list_bdf_fonts (pattern, maxnames - n_fonts);
6236 combined[1] = newlist;
6237 newlist = Fnconc(2, combined);
6240 return newlist;
6244 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
6245 struct font_info *
6246 w32_get_font_info (f, font_idx)
6247 FRAME_PTR f;
6248 int font_idx;
6250 return (FRAME_W32_FONT_TABLE (f) + font_idx);
6254 struct font_info*
6255 w32_query_font (struct frame *f, char *fontname)
6257 int i;
6258 struct font_info *pfi;
6260 pfi = FRAME_W32_FONT_TABLE (f);
6262 for (i = 0; i < one_w32_display_info.n_fonts ;i++, pfi++)
6264 if (stricmp(pfi->name, fontname) == 0) return pfi;
6267 return NULL;
6270 /* Find a CCL program for a font specified by FONTP, and set the member
6271 `encoder' of the structure. */
6273 void
6274 w32_find_ccl_program (fontp)
6275 struct font_info *fontp;
6277 Lisp_Object list, elt;
6279 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
6281 elt = XCAR (list);
6282 if (CONSP (elt)
6283 && STRINGP (XCAR (elt))
6284 && (fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
6285 >= 0))
6286 break;
6288 if (! NILP (list))
6290 struct ccl_program *ccl
6291 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
6293 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
6294 xfree (ccl);
6295 else
6296 fontp->font_encoder = ccl;
6300 /* directory-files from dired.c. */
6301 Lisp_Object Fdirectory_files P_((Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object));
6304 /* Find BDF files in a specified directory. (use GCPRO when calling,
6305 as this calls lisp to get a directory listing). */
6306 static Lisp_Object
6307 w32_find_bdf_fonts_in_dir (Lisp_Object directory)
6309 Lisp_Object filelist, list = Qnil;
6310 char fontname[100];
6312 if (!STRINGP(directory))
6313 return Qnil;
6315 filelist = Fdirectory_files (directory, Qt,
6316 build_string (".*\\.[bB][dD][fF]"), Qt);
6318 for ( ; CONSP(filelist); filelist = XCDR (filelist))
6320 Lisp_Object filename = XCAR (filelist);
6321 if (w32_BDF_to_x_font (SDATA (filename), fontname, 100))
6322 store_in_alist (&list, build_string (fontname), filename);
6324 return list;
6327 DEFUN ("w32-find-bdf-fonts", Fw32_find_bdf_fonts, Sw32_find_bdf_fonts,
6328 1, 1, 0,
6329 doc: /* Return a list of BDF fonts in DIRECTORY.
6330 The list is suitable for appending to `w32-bdf-filename-alist'.
6331 Fonts which do not contain an xlfd description will not be included
6332 in the list. DIRECTORY may be a list of directories. */)
6333 (directory)
6334 Lisp_Object directory;
6336 Lisp_Object list = Qnil;
6337 struct gcpro gcpro1, gcpro2;
6339 if (!CONSP (directory))
6340 return w32_find_bdf_fonts_in_dir (directory);
6342 for ( ; CONSP (directory); directory = XCDR (directory))
6344 Lisp_Object pair[2];
6345 pair[0] = list;
6346 pair[1] = Qnil;
6347 GCPRO2 (directory, list);
6348 pair[1] = w32_find_bdf_fonts_in_dir( XCAR (directory) );
6349 list = Fnconc( 2, pair );
6350 UNGCPRO;
6352 return list;
6356 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
6357 doc: /* Internal function called by `color-defined-p', which see. */)
6358 (color, frame)
6359 Lisp_Object color, frame;
6361 XColor foo;
6362 FRAME_PTR f = check_x_frame (frame);
6364 CHECK_STRING (color);
6366 if (w32_defined_color (f, SDATA (color), &foo, 0))
6367 return Qt;
6368 else
6369 return Qnil;
6372 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
6373 doc: /* Internal function called by `color-values', which see. */)
6374 (color, frame)
6375 Lisp_Object color, frame;
6377 XColor foo;
6378 FRAME_PTR f = check_x_frame (frame);
6380 CHECK_STRING (color);
6382 if (w32_defined_color (f, SDATA (color), &foo, 0))
6383 return list3 (make_number ((GetRValue (foo.pixel) << 8)
6384 | GetRValue (foo.pixel)),
6385 make_number ((GetGValue (foo.pixel) << 8)
6386 | GetGValue (foo.pixel)),
6387 make_number ((GetBValue (foo.pixel) << 8)
6388 | GetBValue (foo.pixel)));
6389 else
6390 return Qnil;
6393 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
6394 doc: /* Internal function called by `display-color-p', which see. */)
6395 (display)
6396 Lisp_Object display;
6398 struct w32_display_info *dpyinfo = check_x_display_info (display);
6400 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 2)
6401 return Qnil;
6403 return Qt;
6406 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p,
6407 Sx_display_grayscale_p, 0, 1, 0,
6408 doc: /* Return t if DISPLAY supports shades of gray.
6409 Note that color displays do support shades of gray.
6410 The optional argument DISPLAY specifies which display to ask about.
6411 DISPLAY should be either a frame or a display name (a string).
6412 If omitted or nil, that stands for the selected frame's display. */)
6413 (display)
6414 Lisp_Object display;
6416 struct w32_display_info *dpyinfo = check_x_display_info (display);
6418 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 1)
6419 return Qnil;
6421 return Qt;
6424 DEFUN ("x-display-pixel-width", Fx_display_pixel_width,
6425 Sx_display_pixel_width, 0, 1, 0,
6426 doc: /* Returns the width in pixels of DISPLAY.
6427 The optional argument DISPLAY specifies which display to ask about.
6428 DISPLAY should be either a frame or a display name (a string).
6429 If omitted or nil, that stands for the selected frame's display. */)
6430 (display)
6431 Lisp_Object display;
6433 struct w32_display_info *dpyinfo = check_x_display_info (display);
6435 return make_number (dpyinfo->width);
6438 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
6439 Sx_display_pixel_height, 0, 1, 0,
6440 doc: /* Returns the height in pixels of DISPLAY.
6441 The optional argument DISPLAY specifies which display to ask about.
6442 DISPLAY should be either a frame or a display name (a string).
6443 If omitted or nil, that stands for the selected frame's display. */)
6444 (display)
6445 Lisp_Object display;
6447 struct w32_display_info *dpyinfo = check_x_display_info (display);
6449 return make_number (dpyinfo->height);
6452 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
6453 0, 1, 0,
6454 doc: /* Returns the number of bitplanes of DISPLAY.
6455 The optional argument DISPLAY specifies which display to ask about.
6456 DISPLAY should be either a frame or a display name (a string).
6457 If omitted or nil, that stands for the selected frame's display. */)
6458 (display)
6459 Lisp_Object display;
6461 struct w32_display_info *dpyinfo = check_x_display_info (display);
6463 return make_number (dpyinfo->n_planes * dpyinfo->n_cbits);
6466 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
6467 0, 1, 0,
6468 doc: /* Returns the number of color cells of DISPLAY.
6469 The optional argument DISPLAY specifies which display to ask about.
6470 DISPLAY should be either a frame or a display name (a string).
6471 If omitted or nil, that stands for the selected frame's display. */)
6472 (display)
6473 Lisp_Object display;
6475 struct w32_display_info *dpyinfo = check_x_display_info (display);
6476 HDC hdc;
6477 int cap;
6479 hdc = GetDC (dpyinfo->root_window);
6480 if (dpyinfo->has_palette)
6481 cap = GetDeviceCaps (hdc, SIZEPALETTE);
6482 else
6483 cap = GetDeviceCaps (hdc, NUMCOLORS);
6485 /* We force 24+ bit depths to 24-bit, both to prevent an overflow
6486 and because probably is more meaningful on Windows anyway */
6487 if (cap < 0)
6488 cap = 1 << min(dpyinfo->n_planes * dpyinfo->n_cbits, 24);
6490 ReleaseDC (dpyinfo->root_window, hdc);
6492 return make_number (cap);
6495 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
6496 Sx_server_max_request_size,
6497 0, 1, 0,
6498 doc: /* Returns the maximum request size of the server of DISPLAY.
6499 The optional argument DISPLAY specifies which display to ask about.
6500 DISPLAY should be either a frame or a display name (a string).
6501 If omitted or nil, that stands for the selected frame's display. */)
6502 (display)
6503 Lisp_Object display;
6505 struct w32_display_info *dpyinfo = check_x_display_info (display);
6507 return make_number (1);
6510 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
6511 doc: /* Returns the "vendor ID" string of the W32 system (Microsoft).
6512 The optional argument DISPLAY specifies which display to ask about.
6513 DISPLAY should be either a frame or a display name (a string).
6514 If omitted or nil, that stands for the selected frame's display. */)
6515 (display)
6516 Lisp_Object display;
6518 return build_string ("Microsoft Corp.");
6521 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
6522 doc: /* Returns the version numbers of the server of DISPLAY.
6523 The value is a list of three integers: the major and minor
6524 version numbers of the X Protocol in use, and the distributor-specific release
6525 number. See also the function `x-server-vendor'.
6527 The optional argument DISPLAY specifies which display to ask about.
6528 DISPLAY should be either a frame or a display name (a string).
6529 If omitted or nil, that stands for the selected frame's display. */)
6530 (display)
6531 Lisp_Object display;
6533 return Fcons (make_number (w32_major_version),
6534 Fcons (make_number (w32_minor_version),
6535 Fcons (make_number (w32_build_number), Qnil)));
6538 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
6539 doc: /* Returns the number of screens on the server of DISPLAY.
6540 The optional argument DISPLAY specifies which display to ask about.
6541 DISPLAY should be either a frame or a display name (a string).
6542 If omitted or nil, that stands for the selected frame's display. */)
6543 (display)
6544 Lisp_Object display;
6546 return make_number (1);
6549 DEFUN ("x-display-mm-height", Fx_display_mm_height,
6550 Sx_display_mm_height, 0, 1, 0,
6551 doc: /* Returns the height in millimeters of DISPLAY.
6552 The optional argument DISPLAY specifies which display to ask about.
6553 DISPLAY should be either a frame or a display name (a string).
6554 If omitted or nil, that stands for the selected frame's display. */)
6555 (display)
6556 Lisp_Object display;
6558 struct w32_display_info *dpyinfo = check_x_display_info (display);
6559 HDC hdc;
6560 int cap;
6562 hdc = GetDC (dpyinfo->root_window);
6564 cap = GetDeviceCaps (hdc, VERTSIZE);
6566 ReleaseDC (dpyinfo->root_window, hdc);
6568 return make_number (cap);
6571 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
6572 doc: /* Returns the width in millimeters of DISPLAY.
6573 The optional argument DISPLAY specifies which display to ask about.
6574 DISPLAY should be either a frame or a display name (a string).
6575 If omitted or nil, that stands for the selected frame's display. */)
6576 (display)
6577 Lisp_Object display;
6579 struct w32_display_info *dpyinfo = check_x_display_info (display);
6581 HDC hdc;
6582 int cap;
6584 hdc = GetDC (dpyinfo->root_window);
6586 cap = GetDeviceCaps (hdc, HORZSIZE);
6588 ReleaseDC (dpyinfo->root_window, hdc);
6590 return make_number (cap);
6593 DEFUN ("x-display-backing-store", Fx_display_backing_store,
6594 Sx_display_backing_store, 0, 1, 0,
6595 doc: /* Returns an indication of whether DISPLAY does backing store.
6596 The value may be `always', `when-mapped', or `not-useful'.
6597 The optional argument DISPLAY specifies which display to ask about.
6598 DISPLAY should be either a frame or a display name (a string).
6599 If omitted or nil, that stands for the selected frame's display. */)
6600 (display)
6601 Lisp_Object display;
6603 return intern ("not-useful");
6606 DEFUN ("x-display-visual-class", Fx_display_visual_class,
6607 Sx_display_visual_class, 0, 1, 0,
6608 doc: /* Returns the visual class of DISPLAY.
6609 The value is one of the symbols `static-gray', `gray-scale',
6610 `static-color', `pseudo-color', `true-color', or `direct-color'.
6612 The optional argument DISPLAY specifies which display to ask about.
6613 DISPLAY should be either a frame or a display name (a string).
6614 If omitted or nil, that stands for the selected frame's display. */)
6615 (display)
6616 Lisp_Object display;
6618 struct w32_display_info *dpyinfo = check_x_display_info (display);
6619 Lisp_Object result = Qnil;
6621 if (dpyinfo->has_palette)
6622 result = intern ("pseudo-color");
6623 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 1)
6624 result = intern ("static-grey");
6625 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 4)
6626 result = intern ("static-color");
6627 else if (dpyinfo->n_planes * dpyinfo->n_cbits > 8)
6628 result = intern ("true-color");
6630 return result;
6633 DEFUN ("x-display-save-under", Fx_display_save_under,
6634 Sx_display_save_under, 0, 1, 0,
6635 doc: /* Returns t if DISPLAY supports the save-under feature.
6636 The optional argument DISPLAY specifies which display to ask about.
6637 DISPLAY should be either a frame or a display name (a string).
6638 If omitted or nil, that stands for the selected frame's display. */)
6639 (display)
6640 Lisp_Object display;
6642 return Qnil;
6646 x_pixel_width (f)
6647 register struct frame *f;
6649 return FRAME_PIXEL_WIDTH (f);
6653 x_pixel_height (f)
6654 register struct frame *f;
6656 return FRAME_PIXEL_HEIGHT (f);
6660 x_char_width (f)
6661 register struct frame *f;
6663 return FRAME_COLUMN_WIDTH (f);
6667 x_char_height (f)
6668 register struct frame *f;
6670 return FRAME_LINE_HEIGHT (f);
6674 x_screen_planes (f)
6675 register struct frame *f;
6677 return FRAME_W32_DISPLAY_INFO (f)->n_planes;
6680 /* Return the display structure for the display named NAME.
6681 Open a new connection if necessary. */
6683 struct w32_display_info *
6684 x_display_info_for_name (name)
6685 Lisp_Object name;
6687 Lisp_Object names;
6688 struct w32_display_info *dpyinfo;
6690 CHECK_STRING (name);
6692 for (dpyinfo = &one_w32_display_info, names = w32_display_name_list;
6693 dpyinfo;
6694 dpyinfo = dpyinfo->next, names = XCDR (names))
6696 Lisp_Object tem;
6697 tem = Fstring_equal (XCAR (XCAR (names)), name);
6698 if (!NILP (tem))
6699 return dpyinfo;
6702 /* Use this general default value to start with. */
6703 Vx_resource_name = Vinvocation_name;
6705 validate_x_resource_name ();
6707 dpyinfo = w32_term_init (name, (unsigned char *)0,
6708 (char *) SDATA (Vx_resource_name));
6710 if (dpyinfo == 0)
6711 error ("Cannot connect to server %s", SDATA (name));
6713 w32_in_use = 1;
6714 XSETFASTINT (Vwindow_system_version, 3);
6716 return dpyinfo;
6719 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
6720 1, 3, 0, doc: /* Open a connection to a server.
6721 DISPLAY is the name of the display to connect to.
6722 Optional second arg XRM-STRING is a string of resources in xrdb format.
6723 If the optional third arg MUST-SUCCEED is non-nil,
6724 terminate Emacs if we can't open the connection. */)
6725 (display, xrm_string, must_succeed)
6726 Lisp_Object display, xrm_string, must_succeed;
6728 unsigned char *xrm_option;
6729 struct w32_display_info *dpyinfo;
6731 /* If initialization has already been done, return now to avoid
6732 overwriting critical parts of one_w32_display_info. */
6733 if (w32_in_use)
6734 return Qnil;
6736 CHECK_STRING (display);
6737 if (! NILP (xrm_string))
6738 CHECK_STRING (xrm_string);
6740 #if 0
6741 if (! EQ (Vwindow_system, intern ("w32")))
6742 error ("Not using Microsoft Windows");
6743 #endif
6745 /* Allow color mapping to be defined externally; first look in user's
6746 HOME directory, then in Emacs etc dir for a file called rgb.txt. */
6748 Lisp_Object color_file;
6749 struct gcpro gcpro1;
6751 color_file = build_string("~/rgb.txt");
6753 GCPRO1 (color_file);
6755 if (NILP (Ffile_readable_p (color_file)))
6756 color_file =
6757 Fexpand_file_name (build_string ("rgb.txt"),
6758 Fsymbol_value (intern ("data-directory")));
6760 Vw32_color_map = Fw32_load_color_file (color_file);
6762 UNGCPRO;
6764 if (NILP (Vw32_color_map))
6765 Vw32_color_map = Fw32_default_color_map ();
6767 /* Merge in system logical colors. */
6768 add_system_logical_colors_to_map (&Vw32_color_map);
6770 if (! NILP (xrm_string))
6771 xrm_option = (unsigned char *) SDATA (xrm_string);
6772 else
6773 xrm_option = (unsigned char *) 0;
6775 /* Use this general default value to start with. */
6776 /* First remove .exe suffix from invocation-name - it looks ugly. */
6778 char basename[ MAX_PATH ], *str;
6780 strcpy (basename, SDATA (Vinvocation_name));
6781 str = strrchr (basename, '.');
6782 if (str) *str = 0;
6783 Vinvocation_name = build_string (basename);
6785 Vx_resource_name = Vinvocation_name;
6787 validate_x_resource_name ();
6789 /* This is what opens the connection and sets x_current_display.
6790 This also initializes many symbols, such as those used for input. */
6791 dpyinfo = w32_term_init (display, xrm_option,
6792 (char *) SDATA (Vx_resource_name));
6794 if (dpyinfo == 0)
6796 if (!NILP (must_succeed))
6797 fatal ("Cannot connect to server %s.\n",
6798 SDATA (display));
6799 else
6800 error ("Cannot connect to server %s", SDATA (display));
6803 w32_in_use = 1;
6805 XSETFASTINT (Vwindow_system_version, 3);
6806 return Qnil;
6809 DEFUN ("x-close-connection", Fx_close_connection,
6810 Sx_close_connection, 1, 1, 0,
6811 doc: /* Close the connection to DISPLAY's server.
6812 For DISPLAY, specify either a frame or a display name (a string).
6813 If DISPLAY is nil, that stands for the selected frame's display. */)
6814 (display)
6815 Lisp_Object display;
6817 struct w32_display_info *dpyinfo = check_x_display_info (display);
6818 int i;
6820 if (dpyinfo->reference_count > 0)
6821 error ("Display still has frames on it");
6823 BLOCK_INPUT;
6824 /* Free the fonts in the font table. */
6825 for (i = 0; i < dpyinfo->n_fonts; i++)
6826 if (dpyinfo->font_table[i].name)
6828 if (dpyinfo->font_table[i].name != dpyinfo->font_table[i].full_name)
6829 xfree (dpyinfo->font_table[i].full_name);
6830 xfree (dpyinfo->font_table[i].name);
6831 w32_unload_font (dpyinfo, dpyinfo->font_table[i].font);
6833 x_destroy_all_bitmaps (dpyinfo);
6835 x_delete_display (dpyinfo);
6836 UNBLOCK_INPUT;
6838 return Qnil;
6841 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
6842 doc: /* Return the list of display names that Emacs has connections to. */)
6845 Lisp_Object tail, result;
6847 result = Qnil;
6848 for (tail = w32_display_name_list; ! NILP (tail); tail = XCDR (tail))
6849 result = Fcons (XCAR (XCAR (tail)), result);
6851 return result;
6854 DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
6855 doc: /* This is a noop on W32 systems. */)
6856 (on, display)
6857 Lisp_Object display, on;
6859 return Qnil;
6864 /***********************************************************************
6865 Window properties
6866 ***********************************************************************/
6868 DEFUN ("x-change-window-property", Fx_change_window_property,
6869 Sx_change_window_property, 2, 6, 0,
6870 doc: /* Change window property PROP to VALUE on the X window of FRAME.
6871 VALUE may be a string or a list of conses, numbers and/or strings.
6872 If an element in the list is a string, it is converted to
6873 an Atom and the value of the Atom is used. If an element is a cons,
6874 it is converted to a 32 bit number where the car is the 16 top bits and the
6875 cdr is the lower 16 bits.
6876 FRAME nil or omitted means use the selected frame.
6877 If TYPE is given and non-nil, it is the name of the type of VALUE.
6878 If TYPE is not given or nil, the type is STRING.
6879 FORMAT gives the size in bits of each element if VALUE is a list.
6880 It must be one of 8, 16 or 32.
6881 If VALUE is a string or FORMAT is nil or not given, FORMAT defaults to 8.
6882 If OUTER_P is non-nil, the property is changed for the outer X window of
6883 FRAME. Default is to change on the edit X window.
6885 Value is VALUE. */)
6886 (prop, value, frame, type, format, outer_p)
6887 Lisp_Object prop, value, frame, type, format, outer_p;
6889 #if 0 /* TODO : port window properties to W32 */
6890 struct frame *f = check_x_frame (frame);
6891 Atom prop_atom;
6893 CHECK_STRING (prop);
6894 CHECK_STRING (value);
6896 BLOCK_INPUT;
6897 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
6898 XChangeProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
6899 prop_atom, XA_STRING, 8, PropModeReplace,
6900 SDATA (value), SCHARS (value));
6902 /* Make sure the property is set when we return. */
6903 XFlush (FRAME_W32_DISPLAY (f));
6904 UNBLOCK_INPUT;
6906 #endif /* TODO */
6908 return value;
6912 DEFUN ("x-delete-window-property", Fx_delete_window_property,
6913 Sx_delete_window_property, 1, 2, 0,
6914 doc: /* Remove window property PROP from X window of FRAME.
6915 FRAME nil or omitted means use the selected frame. Value is PROP. */)
6916 (prop, frame)
6917 Lisp_Object prop, frame;
6919 #if 0 /* TODO : port window properties to W32 */
6921 struct frame *f = check_x_frame (frame);
6922 Atom prop_atom;
6924 CHECK_STRING (prop);
6925 BLOCK_INPUT;
6926 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
6927 XDeleteProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), prop_atom);
6929 /* Make sure the property is removed when we return. */
6930 XFlush (FRAME_W32_DISPLAY (f));
6931 UNBLOCK_INPUT;
6932 #endif /* TODO */
6934 return prop;
6938 DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
6939 1, 2, 0,
6940 doc: /* Value is the value of window property PROP on FRAME.
6941 If FRAME is nil or omitted, use the selected frame. Value is nil
6942 if FRAME hasn't a property with name PROP or if PROP has no string
6943 value. */)
6944 (prop, frame)
6945 Lisp_Object prop, frame;
6947 #if 0 /* TODO : port window properties to W32 */
6949 struct frame *f = check_x_frame (frame);
6950 Atom prop_atom;
6951 int rc;
6952 Lisp_Object prop_value = Qnil;
6953 char *tmp_data = NULL;
6954 Atom actual_type;
6955 int actual_format;
6956 unsigned long actual_size, bytes_remaining;
6958 CHECK_STRING (prop);
6959 BLOCK_INPUT;
6960 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
6961 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
6962 prop_atom, 0, 0, False, XA_STRING,
6963 &actual_type, &actual_format, &actual_size,
6964 &bytes_remaining, (unsigned char **) &tmp_data);
6965 if (rc == Success)
6967 int size = bytes_remaining;
6969 XFree (tmp_data);
6970 tmp_data = NULL;
6972 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
6973 prop_atom, 0, bytes_remaining,
6974 False, XA_STRING,
6975 &actual_type, &actual_format,
6976 &actual_size, &bytes_remaining,
6977 (unsigned char **) &tmp_data);
6978 if (rc == Success)
6979 prop_value = make_string (tmp_data, size);
6981 XFree (tmp_data);
6984 UNBLOCK_INPUT;
6986 return prop_value;
6988 #endif /* TODO */
6989 return Qnil;
6994 /***********************************************************************
6995 Busy cursor
6996 ***********************************************************************/
6998 /* If non-null, an asynchronous timer that, when it expires, displays
6999 an hourglass cursor on all frames. */
7001 static struct atimer *hourglass_atimer;
7003 /* Non-zero means an hourglass cursor is currently shown. */
7005 static int hourglass_shown_p;
7007 /* Number of seconds to wait before displaying an hourglass cursor. */
7009 static Lisp_Object Vhourglass_delay;
7011 /* Default number of seconds to wait before displaying an hourglass
7012 cursor. */
7014 #define DEFAULT_HOURGLASS_DELAY 1
7016 /* Function prototypes. */
7018 static void show_hourglass P_ ((struct atimer *));
7019 static void hide_hourglass P_ ((void));
7022 /* Cancel a currently active hourglass timer, and start a new one. */
7024 void
7025 start_hourglass ()
7027 #if 0 /* TODO: cursor shape changes. */
7028 EMACS_TIME delay;
7029 int secs, usecs = 0;
7031 cancel_hourglass ();
7033 if (INTEGERP (Vhourglass_delay)
7034 && XINT (Vhourglass_delay) > 0)
7035 secs = XFASTINT (Vhourglass_delay);
7036 else if (FLOATP (Vhourglass_delay)
7037 && XFLOAT_DATA (Vhourglass_delay) > 0)
7039 Lisp_Object tem;
7040 tem = Ftruncate (Vhourglass_delay, Qnil);
7041 secs = XFASTINT (tem);
7042 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
7044 else
7045 secs = DEFAULT_HOURGLASS_DELAY;
7047 EMACS_SET_SECS_USECS (delay, secs, usecs);
7048 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
7049 show_hourglass, NULL);
7050 #endif
7054 /* Cancel the hourglass cursor timer if active, hide an hourglass
7055 cursor if shown. */
7057 void
7058 cancel_hourglass ()
7060 if (hourglass_atimer)
7062 cancel_atimer (hourglass_atimer);
7063 hourglass_atimer = NULL;
7066 if (hourglass_shown_p)
7067 hide_hourglass ();
7071 /* Timer function of hourglass_atimer. TIMER is equal to
7072 hourglass_atimer.
7074 Display an hourglass cursor on all frames by mapping the frames'
7075 hourglass_window. Set the hourglass_p flag in the frames'
7076 output_data.x structure to indicate that an hourglass cursor is
7077 shown on the frames. */
7079 static void
7080 show_hourglass (timer)
7081 struct atimer *timer;
7083 #if 0 /* TODO: cursor shape changes. */
7084 /* The timer implementation will cancel this timer automatically
7085 after this function has run. Set hourglass_atimer to null
7086 so that we know the timer doesn't have to be canceled. */
7087 hourglass_atimer = NULL;
7089 if (!hourglass_shown_p)
7091 Lisp_Object rest, frame;
7093 BLOCK_INPUT;
7095 FOR_EACH_FRAME (rest, frame)
7096 if (FRAME_W32_P (XFRAME (frame)))
7098 struct frame *f = XFRAME (frame);
7100 f->output_data.w32->hourglass_p = 1;
7102 if (!f->output_data.w32->hourglass_window)
7104 unsigned long mask = CWCursor;
7105 XSetWindowAttributes attrs;
7107 attrs.cursor = f->output_data.w32->hourglass_cursor;
7109 f->output_data.w32->hourglass_window
7110 = XCreateWindow (FRAME_X_DISPLAY (f),
7111 FRAME_OUTER_WINDOW (f),
7112 0, 0, 32000, 32000, 0, 0,
7113 InputOnly,
7114 CopyFromParent,
7115 mask, &attrs);
7118 XMapRaised (FRAME_X_DISPLAY (f),
7119 f->output_data.w32->hourglass_window);
7120 XFlush (FRAME_X_DISPLAY (f));
7123 hourglass_shown_p = 1;
7124 UNBLOCK_INPUT;
7126 #endif
7130 /* Hide the hourglass cursor on all frames, if it is currently shown. */
7132 static void
7133 hide_hourglass ()
7135 #if 0 /* TODO: cursor shape changes. */
7136 if (hourglass_shown_p)
7138 Lisp_Object rest, frame;
7140 BLOCK_INPUT;
7141 FOR_EACH_FRAME (rest, frame)
7143 struct frame *f = XFRAME (frame);
7145 if (FRAME_W32_P (f)
7146 /* Watch out for newly created frames. */
7147 && f->output_data.x->hourglass_window)
7149 XUnmapWindow (FRAME_X_DISPLAY (f),
7150 f->output_data.x->hourglass_window);
7151 /* Sync here because XTread_socket looks at the
7152 hourglass_p flag that is reset to zero below. */
7153 XSync (FRAME_X_DISPLAY (f), False);
7154 f->output_data.x->hourglass_p = 0;
7158 hourglass_shown_p = 0;
7159 UNBLOCK_INPUT;
7161 #endif
7166 /***********************************************************************
7167 Tool tips
7168 ***********************************************************************/
7170 static Lisp_Object x_create_tip_frame P_ ((struct w32_display_info *,
7171 Lisp_Object, Lisp_Object));
7172 static void compute_tip_xy P_ ((struct frame *, Lisp_Object, Lisp_Object,
7173 Lisp_Object, int, int, int *, int *));
7175 /* The frame of a currently visible tooltip. */
7177 Lisp_Object tip_frame;
7179 /* If non-nil, a timer started that hides the last tooltip when it
7180 fires. */
7182 Lisp_Object tip_timer;
7183 Window tip_window;
7185 /* If non-nil, a vector of 3 elements containing the last args
7186 with which x-show-tip was called. See there. */
7188 Lisp_Object last_show_tip_args;
7190 /* Maximum size for tooltips; a cons (COLUMNS . ROWS). */
7192 Lisp_Object Vx_max_tooltip_size;
7195 static Lisp_Object
7196 unwind_create_tip_frame (frame)
7197 Lisp_Object frame;
7199 Lisp_Object deleted;
7201 deleted = unwind_create_frame (frame);
7202 if (EQ (deleted, Qt))
7204 tip_window = NULL;
7205 tip_frame = Qnil;
7208 return deleted;
7212 /* Create a frame for a tooltip on the display described by DPYINFO.
7213 PARMS is a list of frame parameters. TEXT is the string to
7214 display in the tip frame. Value is the frame.
7216 Note that functions called here, esp. x_default_parameter can
7217 signal errors, for instance when a specified color name is
7218 undefined. We have to make sure that we're in a consistent state
7219 when this happens. */
7221 static Lisp_Object
7222 x_create_tip_frame (dpyinfo, parms, text)
7223 struct w32_display_info *dpyinfo;
7224 Lisp_Object parms, text;
7226 struct frame *f;
7227 Lisp_Object frame, tem;
7228 Lisp_Object name;
7229 long window_prompting = 0;
7230 int width, height;
7231 int count = SPECPDL_INDEX ();
7232 struct gcpro gcpro1, gcpro2, gcpro3;
7233 struct kboard *kb;
7234 int face_change_count_before = face_change_count;
7235 Lisp_Object buffer;
7236 struct buffer *old_buffer;
7238 check_w32 ();
7240 /* Use this general default value to start with until we know if
7241 this frame has a specified name. */
7242 Vx_resource_name = Vinvocation_name;
7244 #ifdef MULTI_KBOARD
7245 kb = dpyinfo->terminal->kboard;
7246 #else
7247 kb = &the_only_kboard;
7248 #endif
7250 /* Get the name of the frame to use for resource lookup. */
7251 name = w32_get_arg (parms, Qname, "name", "Name", RES_TYPE_STRING);
7252 if (!STRINGP (name)
7253 && !EQ (name, Qunbound)
7254 && !NILP (name))
7255 error ("Invalid frame name--not a string or nil");
7256 Vx_resource_name = name;
7258 frame = Qnil;
7259 GCPRO3 (parms, name, frame);
7260 /* Make a frame without minibuffer nor mode-line. */
7261 f = make_frame (0);
7262 f->wants_modeline = 0;
7263 XSETFRAME (frame, f);
7265 buffer = Fget_buffer_create (build_string (" *tip*"));
7266 Fset_window_buffer (FRAME_ROOT_WINDOW (f), buffer, Qnil);
7267 old_buffer = current_buffer;
7268 set_buffer_internal_1 (XBUFFER (buffer));
7269 current_buffer->truncate_lines = Qnil;
7270 specbind (Qinhibit_read_only, Qt);
7271 specbind (Qinhibit_modification_hooks, Qt);
7272 Ferase_buffer ();
7273 Finsert (1, &text);
7274 set_buffer_internal_1 (old_buffer);
7276 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
7277 record_unwind_protect (unwind_create_tip_frame, frame);
7279 /* By setting the output method, we're essentially saying that
7280 the frame is live, as per FRAME_LIVE_P. If we get a signal
7281 from this point on, x_destroy_window might screw up reference
7282 counts etc. */
7283 f->terminal = dpyinfo->terminal;
7284 f->terminal->reference_count++;
7285 f->output_method = output_w32;
7286 f->output_data.w32 =
7287 (struct w32_output *) xmalloc (sizeof (struct w32_output));
7288 bzero (f->output_data.w32, sizeof (struct w32_output));
7290 FRAME_FONTSET (f) = -1;
7291 f->icon_name = Qnil;
7293 #if 0 /* GLYPH_DEBUG TODO: image support. */
7294 image_cache_refcount = FRAME_X_IMAGE_CACHE (f)->refcount;
7295 dpyinfo_refcount = dpyinfo->reference_count;
7296 #endif /* GLYPH_DEBUG */
7297 #ifdef MULTI_KBOARD
7298 FRAME_KBOARD (f) = kb;
7299 #endif
7300 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
7301 f->output_data.w32->explicit_parent = 0;
7303 /* Set the name; the functions to which we pass f expect the name to
7304 be set. */
7305 if (EQ (name, Qunbound) || NILP (name))
7307 f->name = build_string (dpyinfo->w32_id_name);
7308 f->explicit_name = 0;
7310 else
7312 f->name = name;
7313 f->explicit_name = 1;
7314 /* use the frame's title when getting resources for this frame. */
7315 specbind (Qx_resource_name, name);
7318 /* Extract the window parameters from the supplied values
7319 that are needed to determine window geometry. */
7321 Lisp_Object font;
7323 font = w32_get_arg (parms, Qfont, "font", "Font", RES_TYPE_STRING);
7325 BLOCK_INPUT;
7326 /* First, try whatever font the caller has specified. */
7327 if (STRINGP (font))
7329 tem = Fquery_fontset (font, Qnil);
7330 if (STRINGP (tem))
7331 font = x_new_fontset (f, SDATA (tem));
7332 else
7333 font = x_new_font (f, SDATA (font));
7336 /* Try out a font which we hope has bold and italic variations. */
7337 if (!STRINGP (font))
7338 font = x_new_font (f, "-*-Courier New-normal-r-*-*-*-100-*-*-c-*-iso8859-1");
7339 if (! STRINGP (font))
7340 font = x_new_font (f, "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1");
7341 /* If those didn't work, look for something which will at least work. */
7342 if (! STRINGP (font))
7343 font = x_new_font (f, "-*-Fixedsys-normal-r-*-*-12-*-*-*-c-*-iso8859-1");
7344 UNBLOCK_INPUT;
7345 if (! STRINGP (font))
7346 font = build_string ("Fixedsys");
7348 x_default_parameter (f, parms, Qfont, font,
7349 "font", "Font", RES_TYPE_STRING);
7352 x_default_parameter (f, parms, Qborder_width, make_number (2),
7353 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
7354 /* This defaults to 2 in order to match xterm. We recognize either
7355 internalBorderWidth or internalBorder (which is what xterm calls
7356 it). */
7357 if (NILP (Fassq (Qinternal_border_width, parms)))
7359 Lisp_Object value;
7361 value = w32_get_arg (parms, Qinternal_border_width,
7362 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
7363 if (! EQ (value, Qunbound))
7364 parms = Fcons (Fcons (Qinternal_border_width, value),
7365 parms);
7367 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
7368 "internalBorderWidth", "internalBorderWidth",
7369 RES_TYPE_NUMBER);
7371 /* Also do the stuff which must be set before the window exists. */
7372 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
7373 "foreground", "Foreground", RES_TYPE_STRING);
7374 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
7375 "background", "Background", RES_TYPE_STRING);
7376 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
7377 "pointerColor", "Foreground", RES_TYPE_STRING);
7378 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
7379 "cursorColor", "Foreground", RES_TYPE_STRING);
7380 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
7381 "borderColor", "BorderColor", RES_TYPE_STRING);
7383 /* Init faces before x_default_parameter is called for scroll-bar
7384 parameters because that function calls x_set_scroll_bar_width,
7385 which calls change_frame_size, which calls Fset_window_buffer,
7386 which runs hooks, which call Fvertical_motion. At the end, we
7387 end up in init_iterator with a null face cache, which should not
7388 happen. */
7389 init_frame_faces (f);
7391 f->output_data.w32->dwStyle = WS_BORDER | WS_POPUP | WS_DISABLED;
7392 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
7394 window_prompting = x_figure_window_size (f, parms, 0);
7396 /* No fringes on tip frame. */
7397 f->fringe_cols = 0;
7398 f->left_fringe_width = 0;
7399 f->right_fringe_width = 0;
7401 BLOCK_INPUT;
7402 my_create_tip_window (f);
7403 UNBLOCK_INPUT;
7405 x_make_gc (f);
7407 x_default_parameter (f, parms, Qauto_raise, Qnil,
7408 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
7409 x_default_parameter (f, parms, Qauto_lower, Qnil,
7410 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
7411 x_default_parameter (f, parms, Qcursor_type, Qbox,
7412 "cursorType", "CursorType", RES_TYPE_SYMBOL);
7414 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
7415 Change will not be effected unless different from the current
7416 FRAME_LINES (f). */
7417 width = FRAME_COLS (f);
7418 height = FRAME_LINES (f);
7419 FRAME_LINES (f) = 0;
7420 SET_FRAME_COLS (f, 0);
7421 change_frame_size (f, height, width, 1, 0, 0);
7423 /* Add `tooltip' frame parameter's default value. */
7424 if (NILP (Fframe_parameter (frame, intern ("tooltip"))))
7425 Fmodify_frame_parameters (frame, Fcons (Fcons (intern ("tooltip"), Qt),
7426 Qnil));
7428 /* Set up faces after all frame parameters are known. This call
7429 also merges in face attributes specified for new frames.
7431 Frame parameters may be changed if .Xdefaults contains
7432 specifications for the default font. For example, if there is an
7433 `Emacs.default.attributeBackground: pink', the `background-color'
7434 attribute of the frame get's set, which let's the internal border
7435 of the tooltip frame appear in pink. Prevent this. */
7437 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
7439 /* Set tip_frame here, so that */
7440 tip_frame = frame;
7441 call1 (Qface_set_after_frame_default, frame);
7443 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
7444 Fmodify_frame_parameters (frame, Fcons (Fcons (Qbackground_color, bg),
7445 Qnil));
7448 Fmodify_frame_parameters (frame, Fcons (Fcons (Qwindow_system, Qw32), Qnil));
7450 f->no_split = 1;
7452 UNGCPRO;
7454 /* It is now ok to make the frame official even if we get an error
7455 below. And the frame needs to be on Vframe_list or making it
7456 visible won't work. */
7457 Vframe_list = Fcons (frame, Vframe_list);
7459 /* Now that the frame is official, it counts as a reference to
7460 its display. */
7461 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
7463 /* Setting attributes of faces of the tooltip frame from resources
7464 and similar will increment face_change_count, which leads to the
7465 clearing of all current matrices. Since this isn't necessary
7466 here, avoid it by resetting face_change_count to the value it
7467 had before we created the tip frame. */
7468 face_change_count = face_change_count_before;
7470 /* Discard the unwind_protect. */
7471 return unbind_to (count, frame);
7475 /* Compute where to display tip frame F. PARMS is the list of frame
7476 parameters for F. DX and DY are specified offsets from the current
7477 location of the mouse. WIDTH and HEIGHT are the width and height
7478 of the tooltip. Return coordinates relative to the root window of
7479 the display in *ROOT_X, and *ROOT_Y. */
7481 static void
7482 compute_tip_xy (f, parms, dx, dy, width, height, root_x, root_y)
7483 struct frame *f;
7484 Lisp_Object parms, dx, dy;
7485 int width, height;
7486 int *root_x, *root_y;
7488 Lisp_Object left, top;
7490 /* User-specified position? */
7491 left = Fcdr (Fassq (Qleft, parms));
7492 top = Fcdr (Fassq (Qtop, parms));
7494 /* Move the tooltip window where the mouse pointer is. Resize and
7495 show it. */
7496 if (!INTEGERP (left) || !INTEGERP (top))
7498 POINT pt;
7500 BLOCK_INPUT;
7501 GetCursorPos (&pt);
7502 *root_x = pt.x;
7503 *root_y = pt.y;
7504 UNBLOCK_INPUT;
7507 if (INTEGERP (top))
7508 *root_y = XINT (top);
7509 else if (*root_y + XINT (dy) <= 0)
7510 *root_y = 0; /* Can happen for negative dy */
7511 else if (*root_y + XINT (dy) + height <= FRAME_W32_DISPLAY_INFO (f)->height)
7512 /* It fits below the pointer */
7513 *root_y += XINT (dy);
7514 else if (height + XINT (dy) <= *root_y)
7515 /* It fits above the pointer. */
7516 *root_y -= height + XINT (dy);
7517 else
7518 /* Put it on the top. */
7519 *root_y = 0;
7521 if (INTEGERP (left))
7522 *root_x = XINT (left);
7523 else if (*root_x + XINT (dx) <= 0)
7524 *root_x = 0; /* Can happen for negative dx */
7525 else if (*root_x + XINT (dx) + width <= FRAME_W32_DISPLAY_INFO (f)->width)
7526 /* It fits to the right of the pointer. */
7527 *root_x += XINT (dx);
7528 else if (width + XINT (dx) <= *root_x)
7529 /* It fits to the left of the pointer. */
7530 *root_x -= width + XINT (dx);
7531 else
7532 /* Put it left justified on the screen -- it ought to fit that way. */
7533 *root_x = 0;
7537 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
7538 doc: /* Show STRING in a \"tooltip\" window on frame FRAME.
7539 A tooltip window is a small window displaying a string.
7541 This is an internal function; Lisp code should call `tooltip-show'.
7543 FRAME nil or omitted means use the selected frame.
7545 PARMS is an optional list of frame parameters which can be
7546 used to change the tooltip's appearance.
7548 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
7549 means use the default timeout of 5 seconds.
7551 If the list of frame parameters PARMS contains a `left' parameter,
7552 the tooltip is displayed at that x-position. Otherwise it is
7553 displayed at the mouse position, with offset DX added (default is 5 if
7554 DX isn't specified). Likewise for the y-position; if a `top' frame
7555 parameter is specified, it determines the y-position of the tooltip
7556 window, otherwise it is displayed at the mouse position, with offset
7557 DY added (default is -10).
7559 A tooltip's maximum size is specified by `x-max-tooltip-size'.
7560 Text larger than the specified size is clipped. */)
7561 (string, frame, parms, timeout, dx, dy)
7562 Lisp_Object string, frame, parms, timeout, dx, dy;
7564 struct frame *f;
7565 struct window *w;
7566 int root_x, root_y;
7567 struct buffer *old_buffer;
7568 struct text_pos pos;
7569 int i, width, height;
7570 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7571 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7572 int count = SPECPDL_INDEX ();
7574 specbind (Qinhibit_redisplay, Qt);
7576 GCPRO4 (string, parms, frame, timeout);
7578 CHECK_STRING (string);
7579 f = check_x_frame (frame);
7580 if (NILP (timeout))
7581 timeout = make_number (5);
7582 else
7583 CHECK_NATNUM (timeout);
7585 if (NILP (dx))
7586 dx = make_number (5);
7587 else
7588 CHECK_NUMBER (dx);
7590 if (NILP (dy))
7591 dy = make_number (-10);
7592 else
7593 CHECK_NUMBER (dy);
7595 if (NILP (last_show_tip_args))
7596 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
7598 if (!NILP (tip_frame))
7600 Lisp_Object last_string = AREF (last_show_tip_args, 0);
7601 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
7602 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
7604 if (EQ (frame, last_frame)
7605 && !NILP (Fequal (last_string, string))
7606 && !NILP (Fequal (last_parms, parms)))
7608 struct frame *f = XFRAME (tip_frame);
7610 /* Only DX and DY have changed. */
7611 if (!NILP (tip_timer))
7613 Lisp_Object timer = tip_timer;
7614 tip_timer = Qnil;
7615 call1 (Qcancel_timer, timer);
7618 BLOCK_INPUT;
7619 compute_tip_xy (f, parms, dx, dy, FRAME_PIXEL_WIDTH (f),
7620 FRAME_PIXEL_HEIGHT (f), &root_x, &root_y);
7622 /* Put tooltip in topmost group and in position. */
7623 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
7624 root_x, root_y, 0, 0,
7625 SWP_NOSIZE | SWP_NOACTIVATE);
7627 /* Ensure tooltip is on top of other topmost windows (eg menus). */
7628 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
7629 0, 0, 0, 0,
7630 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
7632 UNBLOCK_INPUT;
7633 goto start_timer;
7637 /* Hide a previous tip, if any. */
7638 Fx_hide_tip ();
7640 ASET (last_show_tip_args, 0, string);
7641 ASET (last_show_tip_args, 1, frame);
7642 ASET (last_show_tip_args, 2, parms);
7644 /* Add default values to frame parameters. */
7645 if (NILP (Fassq (Qname, parms)))
7646 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
7647 if (NILP (Fassq (Qinternal_border_width, parms)))
7648 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
7649 if (NILP (Fassq (Qborder_width, parms)))
7650 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
7651 if (NILP (Fassq (Qborder_color, parms)))
7652 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
7653 if (NILP (Fassq (Qbackground_color, parms)))
7654 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
7655 parms);
7657 /* Block input until the tip has been fully drawn, to avoid crashes
7658 when drawing tips in menus. */
7659 BLOCK_INPUT;
7661 /* Create a frame for the tooltip, and record it in the global
7662 variable tip_frame. */
7663 frame = x_create_tip_frame (FRAME_W32_DISPLAY_INFO (f), parms, string);
7664 f = XFRAME (frame);
7666 /* Set up the frame's root window. */
7667 w = XWINDOW (FRAME_ROOT_WINDOW (f));
7668 w->left_col = w->top_line = make_number (0);
7670 if (CONSP (Vx_max_tooltip_size)
7671 && INTEGERP (XCAR (Vx_max_tooltip_size))
7672 && XINT (XCAR (Vx_max_tooltip_size)) > 0
7673 && INTEGERP (XCDR (Vx_max_tooltip_size))
7674 && XINT (XCDR (Vx_max_tooltip_size)) > 0)
7676 w->total_cols = XCAR (Vx_max_tooltip_size);
7677 w->total_lines = XCDR (Vx_max_tooltip_size);
7679 else
7681 w->total_cols = make_number (80);
7682 w->total_lines = make_number (40);
7685 FRAME_TOTAL_COLS (f) = XINT (w->total_cols);
7686 adjust_glyphs (f);
7687 w->pseudo_window_p = 1;
7689 /* Display the tooltip text in a temporary buffer. */
7690 old_buffer = current_buffer;
7691 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->buffer));
7692 current_buffer->truncate_lines = Qnil;
7693 clear_glyph_matrix (w->desired_matrix);
7694 clear_glyph_matrix (w->current_matrix);
7695 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
7696 try_window (FRAME_ROOT_WINDOW (f), pos, 0);
7698 /* Compute width and height of the tooltip. */
7699 width = height = 0;
7700 for (i = 0; i < w->desired_matrix->nrows; ++i)
7702 struct glyph_row *row = &w->desired_matrix->rows[i];
7703 struct glyph *last;
7704 int row_width;
7706 /* Stop at the first empty row at the end. */
7707 if (!row->enabled_p || !row->displays_text_p)
7708 break;
7710 /* Let the row go over the full width of the frame. */
7711 row->full_width_p = 1;
7713 #ifdef TODO /* Investigate why some fonts need more width than is
7714 calculated for some tooltips. */
7715 /* There's a glyph at the end of rows that is use to place
7716 the cursor there. Don't include the width of this glyph. */
7717 if (row->used[TEXT_AREA])
7719 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
7720 row_width = row->pixel_width - last->pixel_width;
7722 else
7723 #endif
7724 row_width = row->pixel_width;
7726 /* TODO: find why tips do not draw along baseline as instructed. */
7727 height += row->height;
7728 width = max (width, row_width);
7731 /* Add the frame's internal border to the width and height the X
7732 window should have. */
7733 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
7734 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
7736 /* Move the tooltip window where the mouse pointer is. Resize and
7737 show it. */
7738 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
7741 /* Adjust Window size to take border into account. */
7742 RECT rect;
7743 rect.left = rect.top = 0;
7744 rect.right = width;
7745 rect.bottom = height;
7746 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
7747 FRAME_EXTERNAL_MENU_BAR (f));
7749 /* Position and size tooltip, and put it in the topmost group.
7750 The add-on of 3 to the 5th argument is a kludge: without it,
7751 some fonts cause the last character of the tip to be truncated,
7752 for some obscure reason. */
7753 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
7754 root_x, root_y, rect.right - rect.left + 3,
7755 rect.bottom - rect.top, SWP_NOACTIVATE);
7757 /* Ensure tooltip is on top of other topmost windows (eg menus). */
7758 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
7759 0, 0, 0, 0,
7760 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
7762 /* Let redisplay know that we have made the frame visible already. */
7763 f->async_visible = 1;
7765 ShowWindow (FRAME_W32_WINDOW (f), SW_SHOWNOACTIVATE);
7768 /* Draw into the window. */
7769 w->must_be_updated_p = 1;
7770 update_single_window (w, 1);
7772 UNBLOCK_INPUT;
7774 /* Restore original current buffer. */
7775 set_buffer_internal_1 (old_buffer);
7776 windows_or_buffers_changed = old_windows_or_buffers_changed;
7778 start_timer:
7779 /* Let the tip disappear after timeout seconds. */
7780 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
7781 intern ("x-hide-tip"));
7783 UNGCPRO;
7784 return unbind_to (count, Qnil);
7788 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
7789 doc: /* Hide the current tooltip window, if there is any.
7790 Value is t if tooltip was open, nil otherwise. */)
7793 int count;
7794 Lisp_Object deleted, frame, timer;
7795 struct gcpro gcpro1, gcpro2;
7797 /* Return quickly if nothing to do. */
7798 if (NILP (tip_timer) && NILP (tip_frame))
7799 return Qnil;
7801 frame = tip_frame;
7802 timer = tip_timer;
7803 GCPRO2 (frame, timer);
7804 tip_frame = tip_timer = deleted = Qnil;
7806 count = SPECPDL_INDEX ();
7807 specbind (Qinhibit_redisplay, Qt);
7808 specbind (Qinhibit_quit, Qt);
7810 if (!NILP (timer))
7811 call1 (Qcancel_timer, timer);
7813 if (FRAMEP (frame))
7815 Fdelete_frame (frame, Qnil);
7816 deleted = Qt;
7819 UNGCPRO;
7820 return unbind_to (count, deleted);
7825 /***********************************************************************
7826 File selection dialog
7827 ***********************************************************************/
7828 extern Lisp_Object Qfile_name_history;
7830 /* Callback for altering the behaviour of the Open File dialog.
7831 Makes the Filename text field contain "Current Directory" and be
7832 read-only when "Directories" is selected in the filter. This
7833 allows us to work around the fact that the standard Open File
7834 dialog does not support directories. */
7835 UINT CALLBACK
7836 file_dialog_callback (hwnd, msg, wParam, lParam)
7837 HWND hwnd;
7838 UINT msg;
7839 WPARAM wParam;
7840 LPARAM lParam;
7842 if (msg == WM_NOTIFY)
7844 OFNOTIFY * notify = (OFNOTIFY *)lParam;
7845 /* Detect when the Filter dropdown is changed. */
7846 if (notify->hdr.code == CDN_TYPECHANGE
7847 || notify->hdr.code == CDN_INITDONE)
7849 HWND dialog = GetParent (hwnd);
7850 HWND edit_control = GetDlgItem (dialog, FILE_NAME_TEXT_FIELD);
7852 /* Directories is in index 2. */
7853 if (notify->lpOFN->nFilterIndex == 2)
7855 CommDlg_OpenSave_SetControlText (dialog, FILE_NAME_TEXT_FIELD,
7856 "Current Directory");
7857 EnableWindow (edit_control, FALSE);
7859 else
7861 /* Don't override default filename on init done. */
7862 if (notify->hdr.code == CDN_TYPECHANGE)
7863 CommDlg_OpenSave_SetControlText (dialog,
7864 FILE_NAME_TEXT_FIELD, "");
7865 EnableWindow (edit_control, TRUE);
7869 return 0;
7872 /* Since we compile with _WIN32_WINNT set to 0x0400 (for NT4 compatibility)
7873 we end up with the old file dialogs. Define a big enough struct for the
7874 new dialog to trick GetOpenFileName into giving us the new dialogs on
7875 Windows 2000 and XP. */
7876 typedef struct
7878 OPENFILENAME real_details;
7879 void * pReserved;
7880 DWORD dwReserved;
7881 DWORD FlagsEx;
7882 } NEWOPENFILENAME;
7885 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
7886 doc: /* Read file name, prompting with PROMPT in directory DIR.
7887 Use a file selection dialog.
7888 Select DEFAULT-FILENAME in the dialog's file selection box, if
7889 specified. Ensure that file exists if MUSTMATCH is non-nil.
7890 If ONLY-DIR-P is non-nil, the user can only select directories. */)
7891 (prompt, dir, default_filename, mustmatch, only_dir_p)
7892 Lisp_Object prompt, dir, default_filename, mustmatch, only_dir_p;
7894 struct frame *f = SELECTED_FRAME ();
7895 Lisp_Object file = Qnil;
7896 int count = SPECPDL_INDEX ();
7897 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
7898 char filename[MAX_PATH + 1];
7899 char init_dir[MAX_PATH + 1];
7900 int default_filter_index = 1; /* 1: All Files, 2: Directories only */
7902 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
7903 CHECK_STRING (prompt);
7904 CHECK_STRING (dir);
7906 /* Create the dialog with PROMPT as title, using DIR as initial
7907 directory and using "*" as pattern. */
7908 dir = Fexpand_file_name (dir, Qnil);
7909 strncpy (init_dir, SDATA (ENCODE_FILE (dir)), MAX_PATH);
7910 init_dir[MAX_PATH] = '\0';
7911 unixtodos_filename (init_dir);
7913 if (STRINGP (default_filename))
7915 char *file_name_only;
7916 char *full_path_name = SDATA (ENCODE_FILE (default_filename));
7918 unixtodos_filename (full_path_name);
7920 file_name_only = strrchr (full_path_name, '\\');
7921 if (!file_name_only)
7922 file_name_only = full_path_name;
7923 else
7924 file_name_only++;
7926 strncpy (filename, file_name_only, MAX_PATH);
7927 filename[MAX_PATH] = '\0';
7929 else
7930 filename[0] = '\0';
7933 NEWOPENFILENAME new_file_details;
7934 BOOL file_opened = FALSE;
7935 OPENFILENAME * file_details = &new_file_details.real_details;
7937 /* Prevent redisplay. */
7938 specbind (Qinhibit_redisplay, Qt);
7939 BLOCK_INPUT;
7941 bzero (&new_file_details, sizeof (new_file_details));
7942 /* Apparently NT4 crashes if you give it an unexpected size.
7943 I'm not sure about Windows 9x, so play it safe. */
7944 if (w32_major_version > 4 && w32_major_version < 95)
7945 file_details->lStructSize = sizeof (NEWOPENFILENAME);
7946 else
7947 file_details->lStructSize = sizeof (OPENFILENAME);
7949 file_details->hwndOwner = FRAME_W32_WINDOW (f);
7950 /* Undocumented Bug in Common File Dialog:
7951 If a filter is not specified, shell links are not resolved. */
7952 file_details->lpstrFilter = "All Files (*.*)\0*.*\0Directories\0*|*\0\0";
7953 file_details->lpstrFile = filename;
7954 file_details->nMaxFile = sizeof (filename);
7955 file_details->lpstrInitialDir = init_dir;
7956 file_details->lpstrTitle = SDATA (prompt);
7958 if (! NILP (only_dir_p))
7959 default_filter_index = 2;
7961 file_details->nFilterIndex = default_filter_index;
7963 file_details->Flags = (OFN_HIDEREADONLY | OFN_NOCHANGEDIR
7964 | OFN_EXPLORER | OFN_ENABLEHOOK);
7965 if (!NILP (mustmatch))
7967 /* Require that the path to the parent directory exists. */
7968 file_details->Flags |= OFN_PATHMUSTEXIST;
7969 /* If we are looking for a file, require that it exists. */
7970 if (NILP (only_dir_p))
7971 file_details->Flags |= OFN_FILEMUSTEXIST;
7974 file_details->lpfnHook = (LPOFNHOOKPROC) file_dialog_callback;
7976 file_opened = GetOpenFileName (file_details);
7978 UNBLOCK_INPUT;
7980 if (file_opened)
7982 dostounix_filename (filename);
7984 if (file_details->nFilterIndex == 2)
7986 /* "Directories" selected - strip dummy file name. */
7987 char * last = strrchr (filename, '/');
7988 *last = '\0';
7991 file = DECODE_FILE(build_string (filename));
7993 /* User cancelled the dialog without making a selection. */
7994 else if (!CommDlgExtendedError ())
7995 file = Qnil;
7996 /* An error occurred, fallback on reading from the mini-buffer. */
7997 else
7998 file = Fcompleting_read (prompt, intern ("read-file-name-internal"),
7999 dir, mustmatch, dir, Qfile_name_history,
8000 default_filename, Qnil);
8002 file = unbind_to (count, file);
8005 UNGCPRO;
8007 /* Make "Cancel" equivalent to C-g. */
8008 if (NILP (file))
8009 Fsignal (Qquit, Qnil);
8011 return unbind_to (count, file);
8016 /***********************************************************************
8017 w32 specialized functions
8018 ***********************************************************************/
8020 DEFUN ("w32-select-font", Fw32_select_font, Sw32_select_font, 0, 2, 0,
8021 doc: /* Select a font for the named FRAME using the W32 font dialog.
8022 Returns an X-style font string corresponding to the selection.
8024 If FRAME is omitted or nil, it defaults to the selected frame.
8025 If INCLUDE-PROPORTIONAL is non-nil, include proportional fonts
8026 in the font selection dialog. */)
8027 (frame, include_proportional)
8028 Lisp_Object frame, include_proportional;
8030 FRAME_PTR f = check_x_frame (frame);
8031 CHOOSEFONT cf;
8032 LOGFONT lf;
8033 TEXTMETRIC tm;
8034 HDC hdc;
8035 HANDLE oldobj;
8036 char buf[100];
8038 bzero (&cf, sizeof (cf));
8039 bzero (&lf, sizeof (lf));
8041 cf.lStructSize = sizeof (cf);
8042 cf.hwndOwner = FRAME_W32_WINDOW (f);
8043 cf.Flags = CF_FORCEFONTEXIST | CF_SCREENFONTS | CF_NOVERTFONTS;
8045 /* Unless include_proportional is non-nil, limit the selection to
8046 monospaced fonts. */
8047 if (NILP (include_proportional))
8048 cf.Flags |= CF_FIXEDPITCHONLY;
8050 cf.lpLogFont = &lf;
8052 /* Initialize as much of the font details as we can from the current
8053 default font. */
8054 hdc = GetDC (FRAME_W32_WINDOW (f));
8055 oldobj = SelectObject (hdc, FRAME_FONT (f)->hfont);
8056 GetTextFace (hdc, LF_FACESIZE, lf.lfFaceName);
8057 if (GetTextMetrics (hdc, &tm))
8059 lf.lfHeight = tm.tmInternalLeading - tm.tmHeight;
8060 lf.lfWeight = tm.tmWeight;
8061 lf.lfItalic = tm.tmItalic;
8062 lf.lfUnderline = tm.tmUnderlined;
8063 lf.lfStrikeOut = tm.tmStruckOut;
8064 lf.lfCharSet = tm.tmCharSet;
8065 cf.Flags |= CF_INITTOLOGFONTSTRUCT;
8067 SelectObject (hdc, oldobj);
8068 ReleaseDC (FRAME_W32_WINDOW (f), hdc);
8070 if (!ChooseFont (&cf) || !w32_to_x_font (&lf, buf, 100, NULL))
8071 return Qnil;
8073 return build_string (buf);
8076 DEFUN ("w32-send-sys-command", Fw32_send_sys_command,
8077 Sw32_send_sys_command, 1, 2, 0,
8078 doc: /* Send frame a Windows WM_SYSCOMMAND message of type COMMAND.
8079 Some useful values for COMMAND are #xf030 to maximize frame (#xf020
8080 to minimize), #xf120 to restore frame to original size, and #xf100
8081 to activate the menubar for keyboard access. #xf140 activates the
8082 screen saver if defined.
8084 If optional parameter FRAME is not specified, use selected frame. */)
8085 (command, frame)
8086 Lisp_Object command, frame;
8088 FRAME_PTR f = check_x_frame (frame);
8090 CHECK_NUMBER (command);
8092 PostMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, XINT (command), 0);
8094 return Qnil;
8097 DEFUN ("w32-shell-execute", Fw32_shell_execute, Sw32_shell_execute, 2, 4, 0,
8098 doc: /* Get Windows to perform OPERATION on DOCUMENT.
8099 This is a wrapper around the ShellExecute system function, which
8100 invokes the application registered to handle OPERATION for DOCUMENT.
8102 OPERATION is either nil or a string that names a supported operation.
8103 What operations can be used depends on the particular DOCUMENT and its
8104 handler application, but typically it is one of the following common
8105 operations:
8107 \"open\" - open DOCUMENT, which could be a file, a directory, or an
8108 executable program. If it is an application, that
8109 application is launched in the current buffer's default
8110 directory. Otherwise, the application associated with
8111 DOCUMENT is launched in the buffer's default directory.
8112 \"print\" - print DOCUMENT, which must be a file
8113 \"explore\" - start the Windows Explorer on DOCUMENT
8114 \"edit\" - launch an editor and open DOCUMENT for editing; which
8115 editor is launched depends on the association for the
8116 specified DOCUMENT
8117 \"find\" - initiate search starting from DOCUMENT which must specify
8118 a directory
8119 nil - invoke the default OPERATION, or \"open\" if default is
8120 not defined or unavailable
8122 DOCUMENT is typically the name of a document file or a URL, but can
8123 also be a program executable to run, or a directory to open in the
8124 Windows Explorer.
8126 If DOCUMENT is a program executable, the optional arg PARAMETERS can
8127 be a string containing command line parameters that will be passed to
8128 the program; otherwise, PARAMETERS should be nil or unspecified.
8130 Second optional argument SHOW-FLAG can be used to control how the
8131 application will be displayed when it is invoked. If SHOW-FLAG is nil
8132 or unspceified, the application is displayed normally, otherwise it is
8133 an integer representing a ShowWindow flag:
8135 0 - start hidden
8136 1 - start normally
8137 3 - start maximized
8138 6 - start minimized */)
8139 (operation, document, parameters, show_flag)
8140 Lisp_Object operation, document, parameters, show_flag;
8142 Lisp_Object current_dir;
8144 CHECK_STRING (document);
8146 /* Encode filename and current directory. */
8147 current_dir = ENCODE_FILE (current_buffer->directory);
8148 document = ENCODE_FILE (document);
8149 if ((int) ShellExecute (NULL,
8150 (STRINGP (operation) ?
8151 SDATA (operation) : NULL),
8152 SDATA (document),
8153 (STRINGP (parameters) ?
8154 SDATA (parameters) : NULL),
8155 SDATA (current_dir),
8156 (INTEGERP (show_flag) ?
8157 XINT (show_flag) : SW_SHOWDEFAULT))
8158 > 32)
8159 return Qt;
8160 error ("ShellExecute failed: %s", w32_strerror (0));
8163 /* Lookup virtual keycode from string representing the name of a
8164 non-ascii keystroke into the corresponding virtual key, using
8165 lispy_function_keys. */
8166 static int
8167 lookup_vk_code (char *key)
8169 int i;
8171 for (i = 0; i < 256; i++)
8172 if (lispy_function_keys[i] != 0
8173 && strcmp (lispy_function_keys[i], key) == 0)
8174 return i;
8176 return -1;
8179 /* Convert a one-element vector style key sequence to a hot key
8180 definition. */
8181 static Lisp_Object
8182 w32_parse_hot_key (key)
8183 Lisp_Object key;
8185 /* Copied from Fdefine_key and store_in_keymap. */
8186 register Lisp_Object c;
8187 int vk_code;
8188 int lisp_modifiers;
8189 int w32_modifiers;
8190 struct gcpro gcpro1;
8192 CHECK_VECTOR (key);
8194 if (XFASTINT (Flength (key)) != 1)
8195 return Qnil;
8197 GCPRO1 (key);
8199 c = Faref (key, make_number (0));
8201 if (CONSP (c) && lucid_event_type_list_p (c))
8202 c = Fevent_convert_list (c);
8204 UNGCPRO;
8206 if (! INTEGERP (c) && ! SYMBOLP (c))
8207 error ("Key definition is invalid");
8209 /* Work out the base key and the modifiers. */
8210 if (SYMBOLP (c))
8212 c = parse_modifiers (c);
8213 lisp_modifiers = XINT (Fcar (Fcdr (c)));
8214 c = Fcar (c);
8215 if (!SYMBOLP (c))
8216 abort ();
8217 vk_code = lookup_vk_code (SDATA (SYMBOL_NAME (c)));
8219 else if (INTEGERP (c))
8221 lisp_modifiers = XINT (c) & ~CHARACTERBITS;
8222 /* Many ascii characters are their own virtual key code. */
8223 vk_code = XINT (c) & CHARACTERBITS;
8226 if (vk_code < 0 || vk_code > 255)
8227 return Qnil;
8229 if ((lisp_modifiers & meta_modifier) != 0
8230 && !NILP (Vw32_alt_is_meta))
8231 lisp_modifiers |= alt_modifier;
8233 /* Supply defs missing from mingw32. */
8234 #ifndef MOD_ALT
8235 #define MOD_ALT 0x0001
8236 #define MOD_CONTROL 0x0002
8237 #define MOD_SHIFT 0x0004
8238 #define MOD_WIN 0x0008
8239 #endif
8241 /* Convert lisp modifiers to Windows hot-key form. */
8242 w32_modifiers = (lisp_modifiers & hyper_modifier) ? MOD_WIN : 0;
8243 w32_modifiers |= (lisp_modifiers & alt_modifier) ? MOD_ALT : 0;
8244 w32_modifiers |= (lisp_modifiers & ctrl_modifier) ? MOD_CONTROL : 0;
8245 w32_modifiers |= (lisp_modifiers & shift_modifier) ? MOD_SHIFT : 0;
8247 return HOTKEY (vk_code, w32_modifiers);
8250 DEFUN ("w32-register-hot-key", Fw32_register_hot_key,
8251 Sw32_register_hot_key, 1, 1, 0,
8252 doc: /* Register KEY as a hot-key combination.
8253 Certain key combinations like Alt-Tab are reserved for system use on
8254 Windows, and therefore are normally intercepted by the system. However,
8255 most of these key combinations can be received by registering them as
8256 hot-keys, overriding their special meaning.
8258 KEY must be a one element key definition in vector form that would be
8259 acceptable to `define-key' (e.g. [A-tab] for Alt-Tab). The meta
8260 modifier is interpreted as Alt if `w32-alt-is-meta' is t, and hyper
8261 is always interpreted as the Windows modifier keys.
8263 The return value is the hotkey-id if registered, otherwise nil. */)
8264 (key)
8265 Lisp_Object key;
8267 key = w32_parse_hot_key (key);
8269 if (NILP (Fmemq (key, w32_grabbed_keys)))
8271 /* Reuse an empty slot if possible. */
8272 Lisp_Object item = Fmemq (Qnil, w32_grabbed_keys);
8274 /* Safe to add new key to list, even if we have focus. */
8275 if (NILP (item))
8276 w32_grabbed_keys = Fcons (key, w32_grabbed_keys);
8277 else
8278 XSETCAR (item, key);
8280 /* Notify input thread about new hot-key definition, so that it
8281 takes effect without needing to switch focus. */
8282 #ifdef USE_LISP_UNION_TYPE
8283 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
8284 (WPARAM) key.i, 0);
8285 #else
8286 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
8287 (WPARAM) key, 0);
8288 #endif
8291 return key;
8294 DEFUN ("w32-unregister-hot-key", Fw32_unregister_hot_key,
8295 Sw32_unregister_hot_key, 1, 1, 0,
8296 doc: /* Unregister KEY as a hot-key combination. */)
8297 (key)
8298 Lisp_Object key;
8300 Lisp_Object item;
8302 if (!INTEGERP (key))
8303 key = w32_parse_hot_key (key);
8305 item = Fmemq (key, w32_grabbed_keys);
8307 if (!NILP (item))
8309 /* Notify input thread about hot-key definition being removed, so
8310 that it takes effect without needing focus switch. */
8311 #ifdef USE_LISP_UNION_TYPE
8312 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
8313 (WPARAM) XINT (XCAR (item)), (LPARAM) item.i))
8314 #else
8315 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
8316 (WPARAM) XINT (XCAR (item)), (LPARAM) item))
8318 #endif
8320 MSG msg;
8321 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
8323 return Qt;
8325 return Qnil;
8328 DEFUN ("w32-registered-hot-keys", Fw32_registered_hot_keys,
8329 Sw32_registered_hot_keys, 0, 0, 0,
8330 doc: /* Return list of registered hot-key IDs. */)
8333 return Fcopy_sequence (w32_grabbed_keys);
8336 DEFUN ("w32-reconstruct-hot-key", Fw32_reconstruct_hot_key,
8337 Sw32_reconstruct_hot_key, 1, 1, 0,
8338 doc: /* Convert hot-key ID to a lisp key combination.
8339 usage: (w32-reconstruct-hot-key ID) */)
8340 (hotkeyid)
8341 Lisp_Object hotkeyid;
8343 int vk_code, w32_modifiers;
8344 Lisp_Object key;
8346 CHECK_NUMBER (hotkeyid);
8348 vk_code = HOTKEY_VK_CODE (hotkeyid);
8349 w32_modifiers = HOTKEY_MODIFIERS (hotkeyid);
8351 if (lispy_function_keys[vk_code])
8352 key = intern (lispy_function_keys[vk_code]);
8353 else
8354 key = make_number (vk_code);
8356 key = Fcons (key, Qnil);
8357 if (w32_modifiers & MOD_SHIFT)
8358 key = Fcons (Qshift, key);
8359 if (w32_modifiers & MOD_CONTROL)
8360 key = Fcons (Qctrl, key);
8361 if (w32_modifiers & MOD_ALT)
8362 key = Fcons (NILP (Vw32_alt_is_meta) ? Qalt : Qmeta, key);
8363 if (w32_modifiers & MOD_WIN)
8364 key = Fcons (Qhyper, key);
8366 return key;
8369 DEFUN ("w32-toggle-lock-key", Fw32_toggle_lock_key,
8370 Sw32_toggle_lock_key, 1, 2, 0,
8371 doc: /* Toggle the state of the lock key KEY.
8372 KEY can be `capslock', `kp-numlock', or `scroll'.
8373 If the optional parameter NEW-STATE is a number, then the state of KEY
8374 is set to off if the low bit of NEW-STATE is zero, otherwise on. */)
8375 (key, new_state)
8376 Lisp_Object key, new_state;
8378 int vk_code;
8380 if (EQ (key, intern ("capslock")))
8381 vk_code = VK_CAPITAL;
8382 else if (EQ (key, intern ("kp-numlock")))
8383 vk_code = VK_NUMLOCK;
8384 else if (EQ (key, intern ("scroll")))
8385 vk_code = VK_SCROLL;
8386 else
8387 return Qnil;
8389 if (!dwWindowsThreadId)
8390 return make_number (w32_console_toggle_lock_key (vk_code, new_state));
8392 #ifdef USE_LISP_UNION_TYPE
8393 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
8394 (WPARAM) vk_code, (LPARAM) new_state.i))
8395 #else
8396 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
8397 (WPARAM) vk_code, (LPARAM) new_state))
8398 #endif
8400 MSG msg;
8401 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
8402 return make_number (msg.wParam);
8404 return Qnil;
8407 DEFUN ("w32-window-exists-p", Fw32_window_exists_p, Sw32_window_exists_p,
8408 2, 2, 0,
8409 doc: /* Return non-nil if a window exists with the specified CLASS and NAME.
8411 This is a direct interface to the Windows API FindWindow function. */)
8412 (class, name)
8413 Lisp_Object class, name;
8415 HWND hnd;
8417 if (!NILP (class))
8418 CHECK_STRING (class);
8419 if (!NILP (name))
8420 CHECK_STRING (name);
8422 hnd = FindWindow (STRINGP (class) ? ((LPCTSTR) SDATA (class)) : NULL,
8423 STRINGP (name) ? ((LPCTSTR) SDATA (name)) : NULL);
8424 if (!hnd)
8425 return Qnil;
8426 return Qt;
8431 DEFUN ("file-system-info", Ffile_system_info, Sfile_system_info, 1, 1, 0,
8432 doc: /* Return storage information about the file system FILENAME is on.
8433 Value is a list of floats (TOTAL FREE AVAIL), where TOTAL is the total
8434 storage of the file system, FREE is the free storage, and AVAIL is the
8435 storage available to a non-superuser. All 3 numbers are in bytes.
8436 If the underlying system call fails, value is nil. */)
8437 (filename)
8438 Lisp_Object filename;
8440 Lisp_Object encoded, value;
8442 CHECK_STRING (filename);
8443 filename = Fexpand_file_name (filename, Qnil);
8444 encoded = ENCODE_FILE (filename);
8446 value = Qnil;
8448 /* Determining the required information on Windows turns out, sadly,
8449 to be more involved than one would hope. The original Win32 api
8450 call for this will return bogus information on some systems, but we
8451 must dynamically probe for the replacement api, since that was
8452 added rather late on. */
8454 HMODULE hKernel = GetModuleHandle ("kernel32");
8455 BOOL (*pfn_GetDiskFreeSpaceEx)
8456 (char *, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER)
8457 = (void *) GetProcAddress (hKernel, "GetDiskFreeSpaceEx");
8459 /* On Windows, we may need to specify the root directory of the
8460 volume holding FILENAME. */
8461 char rootname[MAX_PATH];
8462 char *name = SDATA (encoded);
8464 /* find the root name of the volume if given */
8465 if (isalpha (name[0]) && name[1] == ':')
8467 rootname[0] = name[0];
8468 rootname[1] = name[1];
8469 rootname[2] = '\\';
8470 rootname[3] = 0;
8472 else if (IS_DIRECTORY_SEP (name[0]) && IS_DIRECTORY_SEP (name[1]))
8474 char *str = rootname;
8475 int slashes = 4;
8478 if (IS_DIRECTORY_SEP (*name) && --slashes == 0)
8479 break;
8480 *str++ = *name++;
8482 while ( *name );
8484 *str++ = '\\';
8485 *str = 0;
8488 if (pfn_GetDiskFreeSpaceEx)
8490 /* Unsigned large integers cannot be cast to double, so
8491 use signed ones instead. */
8492 LARGE_INTEGER availbytes;
8493 LARGE_INTEGER freebytes;
8494 LARGE_INTEGER totalbytes;
8496 if (pfn_GetDiskFreeSpaceEx(rootname,
8497 (ULARGE_INTEGER *)&availbytes,
8498 (ULARGE_INTEGER *)&totalbytes,
8499 (ULARGE_INTEGER *)&freebytes))
8500 value = list3 (make_float ((double) totalbytes.QuadPart),
8501 make_float ((double) freebytes.QuadPart),
8502 make_float ((double) availbytes.QuadPart));
8504 else
8506 DWORD sectors_per_cluster;
8507 DWORD bytes_per_sector;
8508 DWORD free_clusters;
8509 DWORD total_clusters;
8511 if (GetDiskFreeSpace(rootname,
8512 &sectors_per_cluster,
8513 &bytes_per_sector,
8514 &free_clusters,
8515 &total_clusters))
8516 value = list3 (make_float ((double) total_clusters
8517 * sectors_per_cluster * bytes_per_sector),
8518 make_float ((double) free_clusters
8519 * sectors_per_cluster * bytes_per_sector),
8520 make_float ((double) free_clusters
8521 * sectors_per_cluster * bytes_per_sector));
8525 return value;
8528 DEFUN ("default-printer-name", Fdefault_printer_name, Sdefault_printer_name,
8529 0, 0, 0, doc: /* Return the name of Windows default printer device. */)
8532 static char pname_buf[256];
8533 int err;
8534 HANDLE hPrn;
8535 PRINTER_INFO_2 *ppi2 = NULL;
8536 DWORD dwNeeded = 0, dwReturned = 0;
8538 /* Retrieve the default string from Win.ini (the registry).
8539 * String will be in form "printername,drivername,portname".
8540 * This is the most portable way to get the default printer. */
8541 if (GetProfileString ("windows", "device", ",,", pname_buf, sizeof (pname_buf)) <= 0)
8542 return Qnil;
8543 /* printername precedes first "," character */
8544 strtok (pname_buf, ",");
8545 /* We want to know more than the printer name */
8546 if (!OpenPrinter (pname_buf, &hPrn, NULL))
8547 return Qnil;
8548 GetPrinter (hPrn, 2, NULL, 0, &dwNeeded);
8549 if (dwNeeded == 0)
8551 ClosePrinter (hPrn);
8552 return Qnil;
8554 /* Allocate memory for the PRINTER_INFO_2 struct */
8555 ppi2 = (PRINTER_INFO_2 *) xmalloc (dwNeeded);
8556 if (!ppi2)
8558 ClosePrinter (hPrn);
8559 return Qnil;
8561 /* Call GetPrinter() again with big enouth memory block */
8562 err = GetPrinter (hPrn, 2, (LPBYTE)ppi2, dwNeeded, &dwReturned);
8563 ClosePrinter (hPrn);
8564 if (!err)
8566 xfree(ppi2);
8567 return Qnil;
8570 if (ppi2)
8572 if (ppi2->Attributes & PRINTER_ATTRIBUTE_SHARED && ppi2->pServerName)
8574 /* a remote printer */
8575 if (*ppi2->pServerName == '\\')
8576 _snprintf(pname_buf, sizeof (pname_buf), "%s\\%s", ppi2->pServerName,
8577 ppi2->pShareName);
8578 else
8579 _snprintf(pname_buf, sizeof (pname_buf), "\\\\%s\\%s", ppi2->pServerName,
8580 ppi2->pShareName);
8581 pname_buf[sizeof (pname_buf) - 1] = '\0';
8583 else
8585 /* a local printer */
8586 strncpy(pname_buf, ppi2->pPortName, sizeof (pname_buf));
8587 pname_buf[sizeof (pname_buf) - 1] = '\0';
8588 /* `pPortName' can include several ports, delimited by ','.
8589 * we only use the first one. */
8590 strtok(pname_buf, ",");
8592 xfree(ppi2);
8595 return build_string (pname_buf);
8598 /***********************************************************************
8599 Initialization
8600 ***********************************************************************/
8602 /* Keep this list in the same order as frame_parms in frame.c.
8603 Use 0 for unsupported frame parameters. */
8605 frame_parm_handler w32_frame_parm_handlers[] =
8607 x_set_autoraise,
8608 x_set_autolower,
8609 x_set_background_color,
8610 x_set_border_color,
8611 x_set_border_width,
8612 x_set_cursor_color,
8613 x_set_cursor_type,
8614 x_set_font,
8615 x_set_foreground_color,
8616 x_set_icon_name,
8617 x_set_icon_type,
8618 x_set_internal_border_width,
8619 x_set_menu_bar_lines,
8620 x_set_mouse_color,
8621 x_explicitly_set_name,
8622 x_set_scroll_bar_width,
8623 x_set_title,
8624 x_set_unsplittable,
8625 x_set_vertical_scroll_bars,
8626 x_set_visibility,
8627 x_set_tool_bar_lines,
8628 0, /* x_set_scroll_bar_foreground, */
8629 0, /* x_set_scroll_bar_background, */
8630 x_set_screen_gamma,
8631 x_set_line_spacing,
8632 x_set_fringe_width,
8633 x_set_fringe_width,
8634 0, /* x_set_wait_for_wm, */
8635 x_set_fullscreen,
8638 void
8639 syms_of_w32fns ()
8641 globals_of_w32fns ();
8642 /* This is zero if not using MS-Windows. */
8643 w32_in_use = 0;
8644 track_mouse_window = NULL;
8646 w32_visible_system_caret_hwnd = NULL;
8648 Qnone = intern ("none");
8649 staticpro (&Qnone);
8650 Qsuppress_icon = intern ("suppress-icon");
8651 staticpro (&Qsuppress_icon);
8652 Qundefined_color = intern ("undefined-color");
8653 staticpro (&Qundefined_color);
8654 Qcancel_timer = intern ("cancel-timer");
8655 staticpro (&Qcancel_timer);
8657 Qhyper = intern ("hyper");
8658 staticpro (&Qhyper);
8659 Qsuper = intern ("super");
8660 staticpro (&Qsuper);
8661 Qmeta = intern ("meta");
8662 staticpro (&Qmeta);
8663 Qalt = intern ("alt");
8664 staticpro (&Qalt);
8665 Qctrl = intern ("ctrl");
8666 staticpro (&Qctrl);
8667 Qcontrol = intern ("control");
8668 staticpro (&Qcontrol);
8669 Qshift = intern ("shift");
8670 staticpro (&Qshift);
8671 /* This is the end of symbol initialization. */
8673 /* Text property `display' should be nonsticky by default. */
8674 Vtext_property_default_nonsticky
8675 = Fcons (Fcons (Qdisplay, Qt), Vtext_property_default_nonsticky);
8678 Fput (Qundefined_color, Qerror_conditions,
8679 Fcons (Qundefined_color, Fcons (Qerror, Qnil)));
8680 Fput (Qundefined_color, Qerror_message,
8681 build_string ("Undefined color"));
8683 staticpro (&w32_grabbed_keys);
8684 w32_grabbed_keys = Qnil;
8686 DEFVAR_LISP ("w32-color-map", &Vw32_color_map,
8687 doc: /* An array of color name mappings for Windows. */);
8688 Vw32_color_map = Qnil;
8690 DEFVAR_LISP ("w32-pass-alt-to-system", &Vw32_pass_alt_to_system,
8691 doc: /* Non-nil if Alt key presses are passed on to Windows.
8692 When non-nil, for example, Alt pressed and released and then space will
8693 open the System menu. When nil, Emacs processes the Alt key events, and
8694 then silently swallows them. */);
8695 Vw32_pass_alt_to_system = Qnil;
8697 DEFVAR_LISP ("w32-alt-is-meta", &Vw32_alt_is_meta,
8698 doc: /* Non-nil if the Alt key is to be considered the same as the META key.
8699 When nil, Emacs will translate the Alt key to the ALT modifier, not to META. */);
8700 Vw32_alt_is_meta = Qt;
8702 DEFVAR_INT ("w32-quit-key", &w32_quit_key,
8703 doc: /* If non-zero, the virtual key code for an alternative quit key. */);
8704 w32_quit_key = 0;
8706 DEFVAR_LISP ("w32-pass-lwindow-to-system",
8707 &Vw32_pass_lwindow_to_system,
8708 doc: /* If non-nil, the left \"Windows\" key is passed on to Windows.
8710 When non-nil, the Start menu is opened by tapping the key.
8711 If you set this to nil, the left \"Windows\" key is processed by Emacs
8712 according to the value of `w32-lwindow-modifier', which see.
8714 Note that some combinations of the left \"Windows\" key with other keys are
8715 caught by Windows at low level, and so binding them in Emacs will have no
8716 effect. For example, <lwindow>-r always pops up the Windows Run dialog,
8717 <lwindow>-<Pause> pops up the "System Properties" dialog, etc. However, see
8718 the doc string of `w32-phantom-key-code'. */);
8719 Vw32_pass_lwindow_to_system = Qt;
8721 DEFVAR_LISP ("w32-pass-rwindow-to-system",
8722 &Vw32_pass_rwindow_to_system,
8723 doc: /* If non-nil, the right \"Windows\" key is passed on to Windows.
8725 When non-nil, the Start menu is opened by tapping the key.
8726 If you set this to nil, the right \"Windows\" key is processed by Emacs
8727 according to the value of `w32-rwindow-modifier', which see.
8729 Note that some combinations of the right \"Windows\" key with other keys are
8730 caught by Windows at low level, and so binding them in Emacs will have no
8731 effect. For example, <rwindow>-r always pops up the Windows Run dialog,
8732 <rwindow>-<Pause> pops up the "System Properties" dialog, etc. However, see
8733 the doc string of `w32-phantom-key-code'. */);
8734 Vw32_pass_rwindow_to_system = Qt;
8736 DEFVAR_LISP ("w32-phantom-key-code",
8737 &Vw32_phantom_key_code,
8738 doc: /* Virtual key code used to generate \"phantom\" key presses.
8739 Value is a number between 0 and 255.
8741 Phantom key presses are generated in order to stop the system from
8742 acting on \"Windows\" key events when `w32-pass-lwindow-to-system' or
8743 `w32-pass-rwindow-to-system' is nil. */);
8744 /* Although 255 is technically not a valid key code, it works and
8745 means that this hack won't interfere with any real key code. */
8746 XSETINT (Vw32_phantom_key_code, 255);
8748 DEFVAR_LISP ("w32-enable-num-lock",
8749 &Vw32_enable_num_lock,
8750 doc: /* If non-nil, the Num Lock key acts normally.
8751 Set to nil to handle Num Lock as the `kp-numlock' key. */);
8752 Vw32_enable_num_lock = Qt;
8754 DEFVAR_LISP ("w32-enable-caps-lock",
8755 &Vw32_enable_caps_lock,
8756 doc: /* If non-nil, the Caps Lock key acts normally.
8757 Set to nil to handle Caps Lock as the `capslock' key. */);
8758 Vw32_enable_caps_lock = Qt;
8760 DEFVAR_LISP ("w32-scroll-lock-modifier",
8761 &Vw32_scroll_lock_modifier,
8762 doc: /* Modifier to use for the Scroll Lock ON state.
8763 The value can be hyper, super, meta, alt, control or shift for the
8764 respective modifier, or nil to handle Scroll Lock as the `scroll' key.
8765 Any other value will cause the Scroll Lock key to be ignored. */);
8766 Vw32_scroll_lock_modifier = Qt;
8768 DEFVAR_LISP ("w32-lwindow-modifier",
8769 &Vw32_lwindow_modifier,
8770 doc: /* Modifier to use for the left \"Windows\" key.
8771 The value can be hyper, super, meta, alt, control or shift for the
8772 respective modifier, or nil to appear as the `lwindow' key.
8773 Any other value will cause the key to be ignored. */);
8774 Vw32_lwindow_modifier = Qnil;
8776 DEFVAR_LISP ("w32-rwindow-modifier",
8777 &Vw32_rwindow_modifier,
8778 doc: /* Modifier to use for the right \"Windows\" key.
8779 The value can be hyper, super, meta, alt, control or shift for the
8780 respective modifier, or nil to appear as the `rwindow' key.
8781 Any other value will cause the key to be ignored. */);
8782 Vw32_rwindow_modifier = Qnil;
8784 DEFVAR_LISP ("w32-apps-modifier",
8785 &Vw32_apps_modifier,
8786 doc: /* Modifier to use for the \"Apps\" key.
8787 The value can be hyper, super, meta, alt, control or shift for the
8788 respective modifier, or nil to appear as the `apps' key.
8789 Any other value will cause the key to be ignored. */);
8790 Vw32_apps_modifier = Qnil;
8792 DEFVAR_BOOL ("w32-enable-synthesized-fonts", &w32_enable_synthesized_fonts,
8793 doc: /* Non-nil enables selection of artificially italicized and bold fonts. */);
8794 w32_enable_synthesized_fonts = 0;
8796 DEFVAR_LISP ("w32-enable-palette", &Vw32_enable_palette,
8797 doc: /* Non-nil enables Windows palette management to map colors exactly. */);
8798 Vw32_enable_palette = Qt;
8800 DEFVAR_INT ("w32-mouse-button-tolerance",
8801 &w32_mouse_button_tolerance,
8802 doc: /* Analogue of double click interval for faking middle mouse events.
8803 The value is the minimum time in milliseconds that must elapse between
8804 left and right button down events before they are considered distinct events.
8805 If both mouse buttons are depressed within this interval, a middle mouse
8806 button down event is generated instead. */);
8807 w32_mouse_button_tolerance = GetDoubleClickTime () / 2;
8809 DEFVAR_INT ("w32-mouse-move-interval",
8810 &w32_mouse_move_interval,
8811 doc: /* Minimum interval between mouse move events.
8812 The value is the minimum time in milliseconds that must elapse between
8813 successive mouse move (or scroll bar drag) events before they are
8814 reported as lisp events. */);
8815 w32_mouse_move_interval = 0;
8817 DEFVAR_BOOL ("w32-pass-extra-mouse-buttons-to-system",
8818 &w32_pass_extra_mouse_buttons_to_system,
8819 doc: /* If non-nil, the fourth and fifth mouse buttons are passed to Windows.
8820 Recent versions of Windows support mice with up to five buttons.
8821 Since most applications don't support these extra buttons, most mouse
8822 drivers will allow you to map them to functions at the system level.
8823 If this variable is non-nil, Emacs will pass them on, allowing the
8824 system to handle them. */);
8825 w32_pass_extra_mouse_buttons_to_system = 0;
8827 DEFVAR_LISP ("x-pointer-shape", &Vx_pointer_shape,
8828 doc: /* The shape of the pointer when over text.
8829 Changing the value does not affect existing frames
8830 unless you set the mouse color. */);
8831 Vx_pointer_shape = Qnil;
8833 Vx_nontext_pointer_shape = Qnil;
8835 Vx_mode_pointer_shape = Qnil;
8837 DEFVAR_LISP ("x-hourglass-pointer-shape", &Vx_hourglass_pointer_shape,
8838 doc: /* The shape of the pointer when Emacs is busy.
8839 This variable takes effect when you create a new frame
8840 or when you set the mouse color. */);
8841 Vx_hourglass_pointer_shape = Qnil;
8843 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
8844 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
8845 display_hourglass_p = 1;
8847 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
8848 doc: /* *Seconds to wait before displaying an hourglass pointer.
8849 Value must be an integer or float. */);
8850 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
8852 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
8853 &Vx_sensitive_text_pointer_shape,
8854 doc: /* The shape of the pointer when over mouse-sensitive text.
8855 This variable takes effect when you create a new frame
8856 or when you set the mouse color. */);
8857 Vx_sensitive_text_pointer_shape = Qnil;
8859 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
8860 &Vx_window_horizontal_drag_shape,
8861 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
8862 This variable takes effect when you create a new frame
8863 or when you set the mouse color. */);
8864 Vx_window_horizontal_drag_shape = Qnil;
8866 DEFVAR_LISP ("x-cursor-fore-pixel", &Vx_cursor_fore_pixel,
8867 doc: /* A string indicating the foreground color of the cursor box. */);
8868 Vx_cursor_fore_pixel = Qnil;
8870 DEFVAR_LISP ("x-max-tooltip-size", &Vx_max_tooltip_size,
8871 doc: /* Maximum size for tooltips.
8872 Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
8873 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
8875 DEFVAR_LISP ("x-no-window-manager", &Vx_no_window_manager,
8876 doc: /* Non-nil if no window manager is in use.
8877 Emacs doesn't try to figure this out; this is always nil
8878 unless you set it to something else. */);
8879 /* We don't have any way to find this out, so set it to nil
8880 and maybe the user would like to set it to t. */
8881 Vx_no_window_manager = Qnil;
8883 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
8884 &Vx_pixel_size_width_font_regexp,
8885 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
8887 Since Emacs gets width of a font matching with this regexp from
8888 PIXEL_SIZE field of the name, font finding mechanism gets faster for
8889 such a font. This is especially effective for such large fonts as
8890 Chinese, Japanese, and Korean. */);
8891 Vx_pixel_size_width_font_regexp = Qnil;
8893 DEFVAR_LISP ("w32-bdf-filename-alist",
8894 &Vw32_bdf_filename_alist,
8895 doc: /* List of bdf fonts and their corresponding filenames. */);
8896 Vw32_bdf_filename_alist = Qnil;
8898 DEFVAR_BOOL ("w32-strict-fontnames",
8899 &w32_strict_fontnames,
8900 doc: /* Non-nil means only use fonts that are exact matches for those requested.
8901 Default is nil, which allows old fontnames that are not XLFD compliant,
8902 and allows third-party CJK display to work by specifying false charset
8903 fields to trick Emacs into translating to Big5, SJIS etc.
8904 Setting this to t will prevent wrong fonts being selected when
8905 fontsets are automatically created. */);
8906 w32_strict_fontnames = 0;
8908 DEFVAR_BOOL ("w32-strict-painting",
8909 &w32_strict_painting,
8910 doc: /* Non-nil means use strict rules for repainting frames.
8911 Set this to nil to get the old behavior for repainting; this should
8912 only be necessary if the default setting causes problems. */);
8913 w32_strict_painting = 1;
8915 DEFVAR_LISP ("w32-charset-info-alist",
8916 &Vw32_charset_info_alist,
8917 doc: /* Alist linking Emacs character sets to Windows fonts and codepages.
8918 Each entry should be of the form:
8920 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE))
8922 where CHARSET_NAME is a string used in font names to identify the charset,
8923 WINDOWS_CHARSET is a symbol that can be one of:
8924 w32-charset-ansi, w32-charset-default, w32-charset-symbol,
8925 w32-charset-shiftjis, w32-charset-hangeul, w32-charset-gb2312,
8926 w32-charset-chinesebig5,
8927 w32-charset-johab, w32-charset-hebrew,
8928 w32-charset-arabic, w32-charset-greek, w32-charset-turkish,
8929 w32-charset-vietnamese, w32-charset-thai, w32-charset-easteurope,
8930 w32-charset-russian, w32-charset-mac, w32-charset-baltic,
8931 w32-charset-unicode,
8932 or w32-charset-oem.
8933 CODEPAGE should be an integer specifying the codepage that should be used
8934 to display the character set, t to do no translation and output as Unicode,
8935 or nil to do no translation and output as 8 bit (or multibyte on far-east
8936 versions of Windows) characters. */);
8937 Vw32_charset_info_alist = Qnil;
8939 staticpro (&Qw32_charset_ansi);
8940 Qw32_charset_ansi = intern ("w32-charset-ansi");
8941 staticpro (&Qw32_charset_symbol);
8942 Qw32_charset_default = intern ("w32-charset-default");
8943 staticpro (&Qw32_charset_default);
8944 Qw32_charset_symbol = intern ("w32-charset-symbol");
8945 staticpro (&Qw32_charset_shiftjis);
8946 Qw32_charset_shiftjis = intern ("w32-charset-shiftjis");
8947 staticpro (&Qw32_charset_hangeul);
8948 Qw32_charset_hangeul = intern ("w32-charset-hangeul");
8949 staticpro (&Qw32_charset_chinesebig5);
8950 Qw32_charset_chinesebig5 = intern ("w32-charset-chinesebig5");
8951 staticpro (&Qw32_charset_gb2312);
8952 Qw32_charset_gb2312 = intern ("w32-charset-gb2312");
8953 staticpro (&Qw32_charset_oem);
8954 Qw32_charset_oem = intern ("w32-charset-oem");
8956 #ifdef JOHAB_CHARSET
8958 static int w32_extra_charsets_defined = 1;
8959 DEFVAR_BOOL ("w32-extra-charsets-defined", &w32_extra_charsets_defined,
8960 doc: /* Internal variable. */);
8962 staticpro (&Qw32_charset_johab);
8963 Qw32_charset_johab = intern ("w32-charset-johab");
8964 staticpro (&Qw32_charset_easteurope);
8965 Qw32_charset_easteurope = intern ("w32-charset-easteurope");
8966 staticpro (&Qw32_charset_turkish);
8967 Qw32_charset_turkish = intern ("w32-charset-turkish");
8968 staticpro (&Qw32_charset_baltic);
8969 Qw32_charset_baltic = intern ("w32-charset-baltic");
8970 staticpro (&Qw32_charset_russian);
8971 Qw32_charset_russian = intern ("w32-charset-russian");
8972 staticpro (&Qw32_charset_arabic);
8973 Qw32_charset_arabic = intern ("w32-charset-arabic");
8974 staticpro (&Qw32_charset_greek);
8975 Qw32_charset_greek = intern ("w32-charset-greek");
8976 staticpro (&Qw32_charset_hebrew);
8977 Qw32_charset_hebrew = intern ("w32-charset-hebrew");
8978 staticpro (&Qw32_charset_vietnamese);
8979 Qw32_charset_vietnamese = intern ("w32-charset-vietnamese");
8980 staticpro (&Qw32_charset_thai);
8981 Qw32_charset_thai = intern ("w32-charset-thai");
8982 staticpro (&Qw32_charset_mac);
8983 Qw32_charset_mac = intern ("w32-charset-mac");
8985 #endif
8987 #ifdef UNICODE_CHARSET
8989 static int w32_unicode_charset_defined = 1;
8990 DEFVAR_BOOL ("w32-unicode-charset-defined",
8991 &w32_unicode_charset_defined,
8992 doc: /* Internal variable. */);
8994 staticpro (&Qw32_charset_unicode);
8995 Qw32_charset_unicode = intern ("w32-charset-unicode");
8997 #endif
8999 #if 0 /* TODO: Port to W32 */
9000 defsubr (&Sx_change_window_property);
9001 defsubr (&Sx_delete_window_property);
9002 defsubr (&Sx_window_property);
9003 #endif
9004 defsubr (&Sxw_display_color_p);
9005 defsubr (&Sx_display_grayscale_p);
9006 defsubr (&Sxw_color_defined_p);
9007 defsubr (&Sxw_color_values);
9008 defsubr (&Sx_server_max_request_size);
9009 defsubr (&Sx_server_vendor);
9010 defsubr (&Sx_server_version);
9011 defsubr (&Sx_display_pixel_width);
9012 defsubr (&Sx_display_pixel_height);
9013 defsubr (&Sx_display_mm_width);
9014 defsubr (&Sx_display_mm_height);
9015 defsubr (&Sx_display_screens);
9016 defsubr (&Sx_display_planes);
9017 defsubr (&Sx_display_color_cells);
9018 defsubr (&Sx_display_visual_class);
9019 defsubr (&Sx_display_backing_store);
9020 defsubr (&Sx_display_save_under);
9021 defsubr (&Sx_create_frame);
9022 defsubr (&Sx_open_connection);
9023 defsubr (&Sx_close_connection);
9024 defsubr (&Sx_display_list);
9025 defsubr (&Sx_synchronize);
9027 /* W32 specific functions */
9029 defsubr (&Sw32_focus_frame);
9030 defsubr (&Sw32_select_font);
9031 defsubr (&Sw32_define_rgb_color);
9032 defsubr (&Sw32_default_color_map);
9033 defsubr (&Sw32_load_color_file);
9034 defsubr (&Sw32_send_sys_command);
9035 defsubr (&Sw32_shell_execute);
9036 defsubr (&Sw32_register_hot_key);
9037 defsubr (&Sw32_unregister_hot_key);
9038 defsubr (&Sw32_registered_hot_keys);
9039 defsubr (&Sw32_reconstruct_hot_key);
9040 defsubr (&Sw32_toggle_lock_key);
9041 defsubr (&Sw32_window_exists_p);
9042 defsubr (&Sw32_find_bdf_fonts);
9044 defsubr (&Sfile_system_info);
9045 defsubr (&Sdefault_printer_name);
9047 /* Setting callback functions for fontset handler. */
9048 get_font_info_func = w32_get_font_info;
9050 #if 0 /* This function pointer doesn't seem to be used anywhere.
9051 And the pointer assigned has the wrong type, anyway. */
9052 list_fonts_func = w32_list_fonts;
9053 #endif
9055 load_font_func = w32_load_font;
9056 find_ccl_program_func = w32_find_ccl_program;
9057 query_font_func = w32_query_font;
9058 set_frame_fontset_func = x_set_font;
9059 check_window_system_func = check_w32;
9062 hourglass_atimer = NULL;
9063 hourglass_shown_p = 0;
9064 defsubr (&Sx_show_tip);
9065 defsubr (&Sx_hide_tip);
9066 tip_timer = Qnil;
9067 staticpro (&tip_timer);
9068 tip_frame = Qnil;
9069 staticpro (&tip_frame);
9071 last_show_tip_args = Qnil;
9072 staticpro (&last_show_tip_args);
9074 defsubr (&Sx_file_dialog);
9079 globals_of_w32fns is used to initialize those global variables that
9080 must always be initialized on startup even when the global variable
9081 initialized is non zero (see the function main in emacs.c).
9082 globals_of_w32fns is called from syms_of_w32fns when the global
9083 variable initialized is 0 and directly from main when initialized
9084 is non zero.
9086 void globals_of_w32fns ()
9088 HMODULE user32_lib = GetModuleHandle ("user32.dll");
9090 TrackMouseEvent not available in all versions of Windows, so must load
9091 it dynamically. Do it once, here, instead of every time it is used.
9093 track_mouse_event_fn = (TrackMouseEvent_Proc)
9094 GetProcAddress (user32_lib, "TrackMouseEvent");
9095 /* ditto for GetClipboardSequenceNumber. */
9096 clipboard_sequence_fn = (ClipboardSequence_Proc)
9097 GetProcAddress (user32_lib, "GetClipboardSequenceNumber");
9099 DEFVAR_INT ("w32-ansi-code-page",
9100 &w32_ansi_code_page,
9101 doc: /* The ANSI code page used by the system. */);
9102 w32_ansi_code_page = GetACP ();
9105 #undef abort
9107 void w32_abort (void) NO_RETURN;
9109 void
9110 w32_abort()
9112 int button;
9113 button = MessageBox (NULL,
9114 "A fatal error has occurred!\n\n"
9115 "Would you like to attach a debugger?\n\n"
9116 "Select YES to debug, NO to abort Emacs"
9117 #if __GNUC__
9118 "\n\n(type \"gdb -p <emacs-PID>\" and\n"
9119 "\"continue\" inside GDB before clicking YES.)"
9120 #endif
9121 , "Emacs Abort Dialog",
9122 MB_ICONEXCLAMATION | MB_TASKMODAL
9123 | MB_SETFOREGROUND | MB_YESNO);
9124 switch (button)
9126 case IDYES:
9127 DebugBreak ();
9128 exit (2); /* tell the compiler we will never return */
9129 case IDNO:
9130 default:
9131 abort ();
9132 break;
9136 /* For convenience when debugging. */
9138 w32_last_error()
9140 return GetLastError ();
9143 /* arch-tag: 707589ab-b9be-4638-8cdd-74629cc9b446
9144 (do not change this comment) */