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