(url-display-percentage): Only show a message if `url-show-status' is non-nil.
[emacs.git] / src / w32fns.c
blob269e357f0a8f585bfef1373d891b71dcafd40cfa
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;
2069 Lisp_Object top = Qunbound;
2070 Lisp_Object left = Qunbound;
2072 rect.left = rect.top = 0;
2073 rect.right = FRAME_PIXEL_WIDTH (f);
2074 rect.bottom = FRAME_PIXEL_HEIGHT (f);
2076 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
2077 FRAME_EXTERNAL_MENU_BAR (f));
2079 /* Do first time app init */
2081 if (!hprevinst)
2083 w32_init_class (hinst);
2086 if (f->size_hint_flags & USPosition || f->size_hint_flags & PPosition)
2088 XSETINT (left, f->left_pos);
2089 XSETINT (top, f->top_pos);
2091 else if (EQ (left, Qunbound) && EQ (top, Qunbound))
2093 /* When called with RES_TYPE_NUMBER, w32_get_arg will return zero
2094 for anything that is not a number and is not Qunbound. */
2095 left = w32_get_arg (Qnil, Qleft, "left", "Left", RES_TYPE_NUMBER);
2096 top = w32_get_arg (Qnil, Qtop, "top", "Top", RES_TYPE_NUMBER);
2099 FRAME_W32_WINDOW (f) = hwnd
2100 = CreateWindow (EMACS_CLASS,
2101 f->namebuf,
2102 f->output_data.w32->dwStyle | WS_CLIPCHILDREN,
2103 EQ (left, Qunbound) ? CW_USEDEFAULT : XINT (left),
2104 EQ (top, Qunbound) ? CW_USEDEFAULT : XINT (top),
2105 rect.right - rect.left,
2106 rect.bottom - rect.top,
2107 NULL,
2108 NULL,
2109 hinst,
2110 NULL);
2112 if (hwnd)
2114 SetWindowLong (hwnd, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
2115 SetWindowLong (hwnd, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
2116 SetWindowLong (hwnd, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
2117 SetWindowLong (hwnd, WND_SCROLLBAR_INDEX, f->scroll_bar_actual_width);
2118 SetWindowLong (hwnd, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
2120 /* Enable drag-n-drop. */
2121 DragAcceptFiles (hwnd, TRUE);
2123 /* Do this to discard the default setting specified by our parent. */
2124 ShowWindow (hwnd, SW_HIDE);
2126 /* Update frame positions. */
2127 GetWindowRect (hwnd, &rect);
2128 f->left_pos = rect.left;
2129 f->top_pos = rect.top;
2133 void
2134 my_post_msg (wmsg, hwnd, msg, wParam, lParam)
2135 W32Msg * wmsg;
2136 HWND hwnd;
2137 UINT msg;
2138 WPARAM wParam;
2139 LPARAM lParam;
2141 wmsg->msg.hwnd = hwnd;
2142 wmsg->msg.message = msg;
2143 wmsg->msg.wParam = wParam;
2144 wmsg->msg.lParam = lParam;
2145 wmsg->msg.time = GetMessageTime ();
2147 post_msg (wmsg);
2150 /* GetKeyState and MapVirtualKey on Windows 95 do not actually distinguish
2151 between left and right keys as advertised. We test for this
2152 support dynamically, and set a flag when the support is absent. If
2153 absent, we keep track of the left and right control and alt keys
2154 ourselves. This is particularly necessary on keyboards that rely
2155 upon the AltGr key, which is represented as having the left control
2156 and right alt keys pressed. For these keyboards, we need to know
2157 when the left alt key has been pressed in addition to the AltGr key
2158 so that we can properly support M-AltGr-key sequences (such as M-@
2159 on Swedish keyboards). */
2161 #define EMACS_LCONTROL 0
2162 #define EMACS_RCONTROL 1
2163 #define EMACS_LMENU 2
2164 #define EMACS_RMENU 3
2166 static int modifiers[4];
2167 static int modifiers_recorded;
2168 static int modifier_key_support_tested;
2170 static void
2171 test_modifier_support (unsigned int wparam)
2173 unsigned int l, r;
2175 if (wparam != VK_CONTROL && wparam != VK_MENU)
2176 return;
2177 if (wparam == VK_CONTROL)
2179 l = VK_LCONTROL;
2180 r = VK_RCONTROL;
2182 else
2184 l = VK_LMENU;
2185 r = VK_RMENU;
2187 if (!(GetKeyState (l) & 0x8000) && !(GetKeyState (r) & 0x8000))
2188 modifiers_recorded = 1;
2189 else
2190 modifiers_recorded = 0;
2191 modifier_key_support_tested = 1;
2194 static void
2195 record_keydown (unsigned int wparam, unsigned int lparam)
2197 int i;
2199 if (!modifier_key_support_tested)
2200 test_modifier_support (wparam);
2202 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
2203 return;
2205 if (wparam == VK_CONTROL)
2206 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
2207 else
2208 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
2210 modifiers[i] = 1;
2213 static void
2214 record_keyup (unsigned int wparam, unsigned int lparam)
2216 int i;
2218 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
2219 return;
2221 if (wparam == VK_CONTROL)
2222 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
2223 else
2224 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
2226 modifiers[i] = 0;
2229 /* Emacs can lose focus while a modifier key has been pressed. When
2230 it regains focus, be conservative and clear all modifiers since
2231 we cannot reconstruct the left and right modifier state. */
2232 static void
2233 reset_modifiers ()
2235 SHORT ctrl, alt;
2237 if (GetFocus () == NULL)
2238 /* Emacs doesn't have keyboard focus. Do nothing. */
2239 return;
2241 ctrl = GetAsyncKeyState (VK_CONTROL);
2242 alt = GetAsyncKeyState (VK_MENU);
2244 if (!(ctrl & 0x08000))
2245 /* Clear any recorded control modifier state. */
2246 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
2248 if (!(alt & 0x08000))
2249 /* Clear any recorded alt modifier state. */
2250 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
2252 /* Update the state of all modifier keys, because modifiers used in
2253 hot-key combinations can get stuck on if Emacs loses focus as a
2254 result of a hot-key being pressed. */
2256 BYTE keystate[256];
2258 #define CURRENT_STATE(key) ((GetAsyncKeyState (key) & 0x8000) >> 8)
2260 GetKeyboardState (keystate);
2261 keystate[VK_SHIFT] = CURRENT_STATE (VK_SHIFT);
2262 keystate[VK_CONTROL] = CURRENT_STATE (VK_CONTROL);
2263 keystate[VK_LCONTROL] = CURRENT_STATE (VK_LCONTROL);
2264 keystate[VK_RCONTROL] = CURRENT_STATE (VK_RCONTROL);
2265 keystate[VK_MENU] = CURRENT_STATE (VK_MENU);
2266 keystate[VK_LMENU] = CURRENT_STATE (VK_LMENU);
2267 keystate[VK_RMENU] = CURRENT_STATE (VK_RMENU);
2268 keystate[VK_LWIN] = CURRENT_STATE (VK_LWIN);
2269 keystate[VK_RWIN] = CURRENT_STATE (VK_RWIN);
2270 keystate[VK_APPS] = CURRENT_STATE (VK_APPS);
2271 SetKeyboardState (keystate);
2275 /* Synchronize modifier state with what is reported with the current
2276 keystroke. Even if we cannot distinguish between left and right
2277 modifier keys, we know that, if no modifiers are set, then neither
2278 the left or right modifier should be set. */
2279 static void
2280 sync_modifiers ()
2282 if (!modifiers_recorded)
2283 return;
2285 if (!(GetKeyState (VK_CONTROL) & 0x8000))
2286 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
2288 if (!(GetKeyState (VK_MENU) & 0x8000))
2289 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
2292 static int
2293 modifier_set (int vkey)
2295 if (vkey == VK_CAPITAL || vkey == VK_SCROLL)
2296 return (GetKeyState (vkey) & 0x1);
2297 if (!modifiers_recorded)
2298 return (GetKeyState (vkey) & 0x8000);
2300 switch (vkey)
2302 case VK_LCONTROL:
2303 return modifiers[EMACS_LCONTROL];
2304 case VK_RCONTROL:
2305 return modifiers[EMACS_RCONTROL];
2306 case VK_LMENU:
2307 return modifiers[EMACS_LMENU];
2308 case VK_RMENU:
2309 return modifiers[EMACS_RMENU];
2311 return (GetKeyState (vkey) & 0x8000);
2314 /* Convert between the modifier bits W32 uses and the modifier bits
2315 Emacs uses. */
2317 unsigned int
2318 w32_key_to_modifier (int key)
2320 Lisp_Object key_mapping;
2322 switch (key)
2324 case VK_LWIN:
2325 key_mapping = Vw32_lwindow_modifier;
2326 break;
2327 case VK_RWIN:
2328 key_mapping = Vw32_rwindow_modifier;
2329 break;
2330 case VK_APPS:
2331 key_mapping = Vw32_apps_modifier;
2332 break;
2333 case VK_SCROLL:
2334 key_mapping = Vw32_scroll_lock_modifier;
2335 break;
2336 default:
2337 key_mapping = Qnil;
2340 /* NB. This code runs in the input thread, asychronously to the lisp
2341 thread, so we must be careful to ensure access to lisp data is
2342 thread-safe. The following code is safe because the modifier
2343 variable values are updated atomically from lisp and symbols are
2344 not relocated by GC. Also, we don't have to worry about seeing GC
2345 markbits here. */
2346 if (EQ (key_mapping, Qhyper))
2347 return hyper_modifier;
2348 if (EQ (key_mapping, Qsuper))
2349 return super_modifier;
2350 if (EQ (key_mapping, Qmeta))
2351 return meta_modifier;
2352 if (EQ (key_mapping, Qalt))
2353 return alt_modifier;
2354 if (EQ (key_mapping, Qctrl))
2355 return ctrl_modifier;
2356 if (EQ (key_mapping, Qcontrol)) /* synonym for ctrl */
2357 return ctrl_modifier;
2358 if (EQ (key_mapping, Qshift))
2359 return shift_modifier;
2361 /* Don't generate any modifier if not explicitly requested. */
2362 return 0;
2365 unsigned int
2366 w32_get_modifiers ()
2368 return ((modifier_set (VK_SHIFT) ? shift_modifier : 0) |
2369 (modifier_set (VK_CONTROL) ? ctrl_modifier : 0) |
2370 (modifier_set (VK_LWIN) ? w32_key_to_modifier (VK_LWIN) : 0) |
2371 (modifier_set (VK_RWIN) ? w32_key_to_modifier (VK_RWIN) : 0) |
2372 (modifier_set (VK_APPS) ? w32_key_to_modifier (VK_APPS) : 0) |
2373 (modifier_set (VK_SCROLL) ? w32_key_to_modifier (VK_SCROLL) : 0) |
2374 (modifier_set (VK_MENU) ?
2375 ((NILP (Vw32_alt_is_meta)) ? alt_modifier : meta_modifier) : 0));
2378 /* We map the VK_* modifiers into console modifier constants
2379 so that we can use the same routines to handle both console
2380 and window input. */
2382 static int
2383 construct_console_modifiers ()
2385 int mods;
2387 mods = 0;
2388 mods |= (modifier_set (VK_SHIFT)) ? SHIFT_PRESSED : 0;
2389 mods |= (modifier_set (VK_CAPITAL)) ? CAPSLOCK_ON : 0;
2390 mods |= (modifier_set (VK_SCROLL)) ? SCROLLLOCK_ON : 0;
2391 mods |= (modifier_set (VK_NUMLOCK)) ? NUMLOCK_ON : 0;
2392 mods |= (modifier_set (VK_LCONTROL)) ? LEFT_CTRL_PRESSED : 0;
2393 mods |= (modifier_set (VK_RCONTROL)) ? RIGHT_CTRL_PRESSED : 0;
2394 mods |= (modifier_set (VK_LMENU)) ? LEFT_ALT_PRESSED : 0;
2395 mods |= (modifier_set (VK_RMENU)) ? RIGHT_ALT_PRESSED : 0;
2396 mods |= (modifier_set (VK_LWIN)) ? LEFT_WIN_PRESSED : 0;
2397 mods |= (modifier_set (VK_RWIN)) ? RIGHT_WIN_PRESSED : 0;
2398 mods |= (modifier_set (VK_APPS)) ? APPS_PRESSED : 0;
2400 return mods;
2403 static int
2404 w32_get_key_modifiers (unsigned int wparam, unsigned int lparam)
2406 int mods;
2408 /* Convert to emacs modifiers. */
2409 mods = w32_kbd_mods_to_emacs (construct_console_modifiers (), wparam);
2411 return mods;
2414 unsigned int
2415 map_keypad_keys (unsigned int virt_key, unsigned int extended)
2417 if (virt_key < VK_CLEAR || virt_key > VK_DELETE)
2418 return virt_key;
2420 if (virt_key == VK_RETURN)
2421 return (extended ? VK_NUMPAD_ENTER : VK_RETURN);
2423 if (virt_key >= VK_PRIOR && virt_key <= VK_DOWN)
2424 return (!extended ? (VK_NUMPAD_PRIOR + (virt_key - VK_PRIOR)) : virt_key);
2426 if (virt_key == VK_INSERT || virt_key == VK_DELETE)
2427 return (!extended ? (VK_NUMPAD_INSERT + (virt_key - VK_INSERT)) : virt_key);
2429 if (virt_key == VK_CLEAR)
2430 return (!extended ? VK_NUMPAD_CLEAR : virt_key);
2432 return virt_key;
2435 /* List of special key combinations which w32 would normally capture,
2436 but emacs should grab instead. Not directly visible to lisp, to
2437 simplify synchronization. Each item is an integer encoding a virtual
2438 key code and modifier combination to capture. */
2439 Lisp_Object w32_grabbed_keys;
2441 #define HOTKEY(vk,mods) make_number (((vk) & 255) | ((mods) << 8))
2442 #define HOTKEY_ID(k) (XFASTINT (k) & 0xbfff)
2443 #define HOTKEY_VK_CODE(k) (XFASTINT (k) & 255)
2444 #define HOTKEY_MODIFIERS(k) (XFASTINT (k) >> 8)
2446 #define RAW_HOTKEY_ID(k) ((k) & 0xbfff)
2447 #define RAW_HOTKEY_VK_CODE(k) ((k) & 255)
2448 #define RAW_HOTKEY_MODIFIERS(k) ((k) >> 8)
2450 /* Register hot-keys for reserved key combinations when Emacs has
2451 keyboard focus, since this is the only way Emacs can receive key
2452 combinations like Alt-Tab which are used by the system. */
2454 static void
2455 register_hot_keys (hwnd)
2456 HWND hwnd;
2458 Lisp_Object keylist;
2460 /* Use GC_CONSP, since we are called asynchronously. */
2461 for (keylist = w32_grabbed_keys; GC_CONSP (keylist); keylist = XCDR (keylist))
2463 Lisp_Object key = XCAR (keylist);
2465 /* Deleted entries get set to nil. */
2466 if (!INTEGERP (key))
2467 continue;
2469 RegisterHotKey (hwnd, HOTKEY_ID (key),
2470 HOTKEY_MODIFIERS (key), HOTKEY_VK_CODE (key));
2474 static void
2475 unregister_hot_keys (hwnd)
2476 HWND hwnd;
2478 Lisp_Object keylist;
2480 /* Use GC_CONSP, since we are called asynchronously. */
2481 for (keylist = w32_grabbed_keys; GC_CONSP (keylist); keylist = XCDR (keylist))
2483 Lisp_Object key = XCAR (keylist);
2485 if (!INTEGERP (key))
2486 continue;
2488 UnregisterHotKey (hwnd, HOTKEY_ID (key));
2492 /* Main message dispatch loop. */
2494 static void
2495 w32_msg_pump (deferred_msg * msg_buf)
2497 MSG msg;
2498 int result;
2499 HWND focus_window;
2501 msh_mousewheel = RegisterWindowMessage (MSH_MOUSEWHEEL);
2503 while (GetMessage (&msg, NULL, 0, 0))
2505 if (msg.hwnd == NULL)
2507 switch (msg.message)
2509 case WM_NULL:
2510 /* Produced by complete_deferred_msg; just ignore. */
2511 break;
2512 case WM_EMACS_CREATEWINDOW:
2513 w32_createwindow ((struct frame *) msg.wParam);
2514 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2515 abort ();
2516 break;
2517 case WM_EMACS_SETLOCALE:
2518 SetThreadLocale (msg.wParam);
2519 /* Reply is not expected. */
2520 break;
2521 case WM_EMACS_SETKEYBOARDLAYOUT:
2522 result = (int) ActivateKeyboardLayout ((HKL) msg.wParam, 0);
2523 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
2524 result, 0))
2525 abort ();
2526 break;
2527 case WM_EMACS_REGISTER_HOT_KEY:
2528 focus_window = GetFocus ();
2529 if (focus_window != NULL)
2530 RegisterHotKey (focus_window,
2531 RAW_HOTKEY_ID (msg.wParam),
2532 RAW_HOTKEY_MODIFIERS (msg.wParam),
2533 RAW_HOTKEY_VK_CODE (msg.wParam));
2534 /* Reply is not expected. */
2535 break;
2536 case WM_EMACS_UNREGISTER_HOT_KEY:
2537 focus_window = GetFocus ();
2538 if (focus_window != NULL)
2539 UnregisterHotKey (focus_window, RAW_HOTKEY_ID (msg.wParam));
2540 /* Mark item as erased. NB: this code must be
2541 thread-safe. The next line is okay because the cons
2542 cell is never made into garbage and is not relocated by
2543 GC. */
2544 XSETCAR ((Lisp_Object) ((EMACS_INT) msg.lParam), Qnil);
2545 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2546 abort ();
2547 break;
2548 case WM_EMACS_TOGGLE_LOCK_KEY:
2550 int vk_code = (int) msg.wParam;
2551 int cur_state = (GetKeyState (vk_code) & 1);
2552 Lisp_Object new_state = (Lisp_Object) ((EMACS_INT) msg.lParam);
2554 /* NB: This code must be thread-safe. It is safe to
2555 call NILP because symbols are not relocated by GC,
2556 and pointer here is not touched by GC (so the markbit
2557 can't be set). Numbers are safe because they are
2558 immediate values. */
2559 if (NILP (new_state)
2560 || (NUMBERP (new_state)
2561 && ((XUINT (new_state)) & 1) != cur_state))
2563 one_w32_display_info.faked_key = vk_code;
2565 keybd_event ((BYTE) vk_code,
2566 (BYTE) MapVirtualKey (vk_code, 0),
2567 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2568 keybd_event ((BYTE) vk_code,
2569 (BYTE) MapVirtualKey (vk_code, 0),
2570 KEYEVENTF_EXTENDEDKEY | 0, 0);
2571 keybd_event ((BYTE) vk_code,
2572 (BYTE) MapVirtualKey (vk_code, 0),
2573 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2574 cur_state = !cur_state;
2576 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
2577 cur_state, 0))
2578 abort ();
2580 break;
2581 default:
2582 DebPrint (("msg %x not expected by w32_msg_pump\n", msg.message));
2585 else
2587 DispatchMessage (&msg);
2590 /* Exit nested loop when our deferred message has completed. */
2591 if (msg_buf->completed)
2592 break;
2596 deferred_msg * deferred_msg_head;
2598 static deferred_msg *
2599 find_deferred_msg (HWND hwnd, UINT msg)
2601 deferred_msg * item;
2603 /* Don't actually need synchronization for read access, since
2604 modification of single pointer is always atomic. */
2605 /* enter_crit (); */
2607 for (item = deferred_msg_head; item != NULL; item = item->next)
2608 if (item->w32msg.msg.hwnd == hwnd
2609 && item->w32msg.msg.message == msg)
2610 break;
2612 /* leave_crit (); */
2614 return item;
2617 static LRESULT
2618 send_deferred_msg (deferred_msg * msg_buf,
2619 HWND hwnd,
2620 UINT msg,
2621 WPARAM wParam,
2622 LPARAM lParam)
2624 /* Only input thread can send deferred messages. */
2625 if (GetCurrentThreadId () != dwWindowsThreadId)
2626 abort ();
2628 /* It is an error to send a message that is already deferred. */
2629 if (find_deferred_msg (hwnd, msg) != NULL)
2630 abort ();
2632 /* Enforced synchronization is not needed because this is the only
2633 function that alters deferred_msg_head, and the following critical
2634 section is guaranteed to only be serially reentered (since only the
2635 input thread can call us). */
2637 /* enter_crit (); */
2639 msg_buf->completed = 0;
2640 msg_buf->next = deferred_msg_head;
2641 deferred_msg_head = msg_buf;
2642 my_post_msg (&msg_buf->w32msg, hwnd, msg, wParam, lParam);
2644 /* leave_crit (); */
2646 /* Start a new nested message loop to process other messages until
2647 this one is completed. */
2648 w32_msg_pump (msg_buf);
2650 deferred_msg_head = msg_buf->next;
2652 return msg_buf->result;
2655 void
2656 complete_deferred_msg (HWND hwnd, UINT msg, LRESULT result)
2658 deferred_msg * msg_buf = find_deferred_msg (hwnd, msg);
2660 if (msg_buf == NULL)
2661 /* Message may have been cancelled, so don't abort(). */
2662 return;
2664 msg_buf->result = result;
2665 msg_buf->completed = 1;
2667 /* Ensure input thread is woken so it notices the completion. */
2668 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
2671 void
2672 cancel_all_deferred_msgs ()
2674 deferred_msg * item;
2676 /* Don't actually need synchronization for read access, since
2677 modification of single pointer is always atomic. */
2678 /* enter_crit (); */
2680 for (item = deferred_msg_head; item != NULL; item = item->next)
2682 item->result = 0;
2683 item->completed = 1;
2686 /* leave_crit (); */
2688 /* Ensure input thread is woken so it notices the completion. */
2689 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
2692 DWORD
2693 w32_msg_worker (dw)
2694 DWORD dw;
2696 MSG msg;
2697 deferred_msg dummy_buf;
2699 /* Ensure our message queue is created */
2701 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
2703 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2704 abort ();
2706 memset (&dummy_buf, 0, sizeof (dummy_buf));
2707 dummy_buf.w32msg.msg.hwnd = NULL;
2708 dummy_buf.w32msg.msg.message = WM_NULL;
2710 /* This is the inital message loop which should only exit when the
2711 application quits. */
2712 w32_msg_pump (&dummy_buf);
2714 return 0;
2717 static void
2718 signal_user_input ()
2720 /* Interrupt any lisp that wants to be interrupted by input. */
2721 if (!NILP (Vthrow_on_input))
2723 Vquit_flag = Vthrow_on_input;
2724 /* If we're inside a function that wants immediate quits,
2725 do it now. */
2726 if (immediate_quit && NILP (Vinhibit_quit))
2728 immediate_quit = 0;
2729 QUIT;
2735 static void
2736 post_character_message (hwnd, msg, wParam, lParam, modifiers)
2737 HWND hwnd;
2738 UINT msg;
2739 WPARAM wParam;
2740 LPARAM lParam;
2741 DWORD modifiers;
2744 W32Msg wmsg;
2746 wmsg.dwModifiers = modifiers;
2748 /* Detect quit_char and set quit-flag directly. Note that we
2749 still need to post a message to ensure the main thread will be
2750 woken up if blocked in sys_select(), but we do NOT want to post
2751 the quit_char message itself (because it will usually be as if
2752 the user had typed quit_char twice). Instead, we post a dummy
2753 message that has no particular effect. */
2755 int c = wParam;
2756 if (isalpha (c) && wmsg.dwModifiers == ctrl_modifier)
2757 c = make_ctrl_char (c) & 0377;
2758 if (c == quit_char
2759 || (wmsg.dwModifiers == 0 &&
2760 w32_quit_key && wParam == w32_quit_key))
2762 Vquit_flag = Qt;
2764 /* The choice of message is somewhat arbitrary, as long as
2765 the main thread handler just ignores it. */
2766 msg = WM_NULL;
2768 /* Interrupt any blocking system calls. */
2769 signal_quit ();
2771 /* As a safety precaution, forcibly complete any deferred
2772 messages. This is a kludge, but I don't see any particularly
2773 clean way to handle the situation where a deferred message is
2774 "dropped" in the lisp thread, and will thus never be
2775 completed, eg. by the user trying to activate the menubar
2776 when the lisp thread is busy, and then typing C-g when the
2777 menubar doesn't open promptly (with the result that the
2778 menubar never responds at all because the deferred
2779 WM_INITMENU message is never completed). Another problem
2780 situation is when the lisp thread calls SendMessage (to send
2781 a window manager command) when a message has been deferred;
2782 the lisp thread gets blocked indefinitely waiting for the
2783 deferred message to be completed, which itself is waiting for
2784 the lisp thread to respond.
2786 Note that we don't want to block the input thread waiting for
2787 a reponse from the lisp thread (although that would at least
2788 solve the deadlock problem above), because we want to be able
2789 to receive C-g to interrupt the lisp thread. */
2790 cancel_all_deferred_msgs ();
2792 else
2793 signal_user_input ();
2796 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2799 /* Main window procedure */
2801 LRESULT CALLBACK
2802 w32_wnd_proc (hwnd, msg, wParam, lParam)
2803 HWND hwnd;
2804 UINT msg;
2805 WPARAM wParam;
2806 LPARAM lParam;
2808 struct frame *f;
2809 struct w32_display_info *dpyinfo = &one_w32_display_info;
2810 W32Msg wmsg;
2811 int windows_translate;
2812 int key;
2814 /* Note that it is okay to call x_window_to_frame, even though we are
2815 not running in the main lisp thread, because frame deletion
2816 requires the lisp thread to synchronize with this thread. Thus, if
2817 a frame struct is returned, it can be used without concern that the
2818 lisp thread might make it disappear while we are using it.
2820 NB. Walking the frame list in this thread is safe (as long as
2821 writes of Lisp_Object slots are atomic, which they are on Windows).
2822 Although delete-frame can destructively modify the frame list while
2823 we are walking it, a garbage collection cannot occur until after
2824 delete-frame has synchronized with this thread.
2826 It is also safe to use functions that make GDI calls, such as
2827 w32_clear_rect, because these functions must obtain a DC handle
2828 from the frame struct using get_frame_dc which is thread-aware. */
2830 switch (msg)
2832 case WM_ERASEBKGND:
2833 f = x_window_to_frame (dpyinfo, hwnd);
2834 if (f)
2836 HDC hdc = get_frame_dc (f);
2837 GetUpdateRect (hwnd, &wmsg.rect, FALSE);
2838 w32_clear_rect (f, hdc, &wmsg.rect);
2839 release_frame_dc (f, hdc);
2841 #if defined (W32_DEBUG_DISPLAY)
2842 DebPrint (("WM_ERASEBKGND (frame %p): erasing %d,%d-%d,%d\n",
2844 wmsg.rect.left, wmsg.rect.top,
2845 wmsg.rect.right, wmsg.rect.bottom));
2846 #endif /* W32_DEBUG_DISPLAY */
2848 return 1;
2849 case WM_PALETTECHANGED:
2850 /* ignore our own changes */
2851 if ((HWND)wParam != hwnd)
2853 f = x_window_to_frame (dpyinfo, hwnd);
2854 if (f)
2855 /* get_frame_dc will realize our palette and force all
2856 frames to be redrawn if needed. */
2857 release_frame_dc (f, get_frame_dc (f));
2859 return 0;
2860 case WM_PAINT:
2862 PAINTSTRUCT paintStruct;
2863 RECT update_rect;
2864 bzero (&update_rect, sizeof (update_rect));
2866 f = x_window_to_frame (dpyinfo, hwnd);
2867 if (f == 0)
2869 DebPrint (("WM_PAINT received for unknown window %p\n", hwnd));
2870 return 0;
2873 /* MSDN Docs say not to call BeginPaint if GetUpdateRect
2874 fails. Apparently this can happen under some
2875 circumstances. */
2876 if (GetUpdateRect (hwnd, &update_rect, FALSE) || !w32_strict_painting)
2878 enter_crit ();
2879 BeginPaint (hwnd, &paintStruct);
2881 /* The rectangles returned by GetUpdateRect and BeginPaint
2882 do not always match. Play it safe by assuming both areas
2883 are invalid. */
2884 UnionRect (&(wmsg.rect), &update_rect, &(paintStruct.rcPaint));
2886 #if defined (W32_DEBUG_DISPLAY)
2887 DebPrint (("WM_PAINT (frame %p): painting %d,%d-%d,%d\n",
2889 wmsg.rect.left, wmsg.rect.top,
2890 wmsg.rect.right, wmsg.rect.bottom));
2891 DebPrint ((" [update region is %d,%d-%d,%d]\n",
2892 update_rect.left, update_rect.top,
2893 update_rect.right, update_rect.bottom));
2894 #endif
2895 EndPaint (hwnd, &paintStruct);
2896 leave_crit ();
2898 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2900 return 0;
2903 /* If GetUpdateRect returns 0 (meaning there is no update
2904 region), assume the whole window needs to be repainted. */
2905 GetClientRect(hwnd, &wmsg.rect);
2906 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2907 return 0;
2910 case WM_INPUTLANGCHANGE:
2911 /* Inform lisp thread of keyboard layout changes. */
2912 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2914 /* Clear dead keys in the keyboard state; for simplicity only
2915 preserve modifier key states. */
2917 int i;
2918 BYTE keystate[256];
2920 GetKeyboardState (keystate);
2921 for (i = 0; i < 256; i++)
2922 if (1
2923 && i != VK_SHIFT
2924 && i != VK_LSHIFT
2925 && i != VK_RSHIFT
2926 && i != VK_CAPITAL
2927 && i != VK_NUMLOCK
2928 && i != VK_SCROLL
2929 && i != VK_CONTROL
2930 && i != VK_LCONTROL
2931 && i != VK_RCONTROL
2932 && i != VK_MENU
2933 && i != VK_LMENU
2934 && i != VK_RMENU
2935 && i != VK_LWIN
2936 && i != VK_RWIN)
2937 keystate[i] = 0;
2938 SetKeyboardState (keystate);
2940 goto dflt;
2942 case WM_HOTKEY:
2943 /* Synchronize hot keys with normal input. */
2944 PostMessage (hwnd, WM_KEYDOWN, HIWORD (lParam), 0);
2945 return (0);
2947 case WM_KEYUP:
2948 case WM_SYSKEYUP:
2949 record_keyup (wParam, lParam);
2950 goto dflt;
2952 case WM_KEYDOWN:
2953 case WM_SYSKEYDOWN:
2954 /* Ignore keystrokes we fake ourself; see below. */
2955 if (dpyinfo->faked_key == wParam)
2957 dpyinfo->faked_key = 0;
2958 /* Make sure TranslateMessage sees them though (as long as
2959 they don't produce WM_CHAR messages). This ensures that
2960 indicator lights are toggled promptly on Windows 9x, for
2961 example. */
2962 if (lispy_function_keys[wParam] != 0)
2964 windows_translate = 1;
2965 goto translate;
2967 return 0;
2970 /* Synchronize modifiers with current keystroke. */
2971 sync_modifiers ();
2972 record_keydown (wParam, lParam);
2973 wParam = map_keypad_keys (wParam, (lParam & 0x1000000L) != 0);
2975 windows_translate = 0;
2977 switch (wParam)
2979 case VK_LWIN:
2980 if (NILP (Vw32_pass_lwindow_to_system))
2982 /* Prevent system from acting on keyup (which opens the
2983 Start menu if no other key was pressed) by simulating a
2984 press of Space which we will ignore. */
2985 if (GetAsyncKeyState (wParam) & 1)
2987 if (NUMBERP (Vw32_phantom_key_code))
2988 key = XUINT (Vw32_phantom_key_code) & 255;
2989 else
2990 key = VK_SPACE;
2991 dpyinfo->faked_key = key;
2992 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
2995 if (!NILP (Vw32_lwindow_modifier))
2996 return 0;
2997 break;
2998 case VK_RWIN:
2999 if (NILP (Vw32_pass_rwindow_to_system))
3001 if (GetAsyncKeyState (wParam) & 1)
3003 if (NUMBERP (Vw32_phantom_key_code))
3004 key = XUINT (Vw32_phantom_key_code) & 255;
3005 else
3006 key = VK_SPACE;
3007 dpyinfo->faked_key = key;
3008 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
3011 if (!NILP (Vw32_rwindow_modifier))
3012 return 0;
3013 break;
3014 case VK_APPS:
3015 if (!NILP (Vw32_apps_modifier))
3016 return 0;
3017 break;
3018 case VK_MENU:
3019 if (NILP (Vw32_pass_alt_to_system))
3020 /* Prevent DefWindowProc from activating the menu bar if an
3021 Alt key is pressed and released by itself. */
3022 return 0;
3023 windows_translate = 1;
3024 break;
3025 case VK_CAPITAL:
3026 /* Decide whether to treat as modifier or function key. */
3027 if (NILP (Vw32_enable_caps_lock))
3028 goto disable_lock_key;
3029 windows_translate = 1;
3030 break;
3031 case VK_NUMLOCK:
3032 /* Decide whether to treat as modifier or function key. */
3033 if (NILP (Vw32_enable_num_lock))
3034 goto disable_lock_key;
3035 windows_translate = 1;
3036 break;
3037 case VK_SCROLL:
3038 /* Decide whether to treat as modifier or function key. */
3039 if (NILP (Vw32_scroll_lock_modifier))
3040 goto disable_lock_key;
3041 windows_translate = 1;
3042 break;
3043 disable_lock_key:
3044 /* Ensure the appropriate lock key state (and indicator light)
3045 remains in the same state. We do this by faking another
3046 press of the relevant key. Apparently, this really is the
3047 only way to toggle the state of the indicator lights. */
3048 dpyinfo->faked_key = wParam;
3049 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
3050 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
3051 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
3052 KEYEVENTF_EXTENDEDKEY | 0, 0);
3053 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
3054 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
3055 /* Ensure indicator lights are updated promptly on Windows 9x
3056 (TranslateMessage apparently does this), after forwarding
3057 input event. */
3058 post_character_message (hwnd, msg, wParam, lParam,
3059 w32_get_key_modifiers (wParam, lParam));
3060 windows_translate = 1;
3061 break;
3062 case VK_CONTROL:
3063 case VK_SHIFT:
3064 case VK_PROCESSKEY: /* Generated by IME. */
3065 windows_translate = 1;
3066 break;
3067 case VK_CANCEL:
3068 /* Windows maps Ctrl-Pause (aka Ctrl-Break) into VK_CANCEL,
3069 which is confusing for purposes of key binding; convert
3070 VK_CANCEL events into VK_PAUSE events. */
3071 wParam = VK_PAUSE;
3072 break;
3073 case VK_PAUSE:
3074 /* Windows maps Ctrl-NumLock into VK_PAUSE, which is confusing
3075 for purposes of key binding; convert these back into
3076 VK_NUMLOCK events, at least when we want to see NumLock key
3077 presses. (Note that there is never any possibility that
3078 VK_PAUSE with Ctrl really is C-Pause as per above.) */
3079 if (NILP (Vw32_enable_num_lock) && modifier_set (VK_CONTROL))
3080 wParam = VK_NUMLOCK;
3081 break;
3082 default:
3083 /* If not defined as a function key, change it to a WM_CHAR message. */
3084 if (lispy_function_keys[wParam] == 0)
3086 DWORD modifiers = construct_console_modifiers ();
3088 if (!NILP (Vw32_recognize_altgr)
3089 && modifier_set (VK_LCONTROL) && modifier_set (VK_RMENU))
3091 /* Always let TranslateMessage handle AltGr key chords;
3092 for some reason, ToAscii doesn't always process AltGr
3093 chords correctly. */
3094 windows_translate = 1;
3096 else if ((modifiers & (~SHIFT_PRESSED & ~CAPSLOCK_ON)) != 0)
3098 /* Handle key chords including any modifiers other
3099 than shift directly, in order to preserve as much
3100 modifier information as possible. */
3101 if ('A' <= wParam && wParam <= 'Z')
3103 /* Don't translate modified alphabetic keystrokes,
3104 so the user doesn't need to constantly switch
3105 layout to type control or meta keystrokes when
3106 the normal layout translates alphabetic
3107 characters to non-ascii characters. */
3108 if (!modifier_set (VK_SHIFT))
3109 wParam += ('a' - 'A');
3110 msg = WM_CHAR;
3112 else
3114 /* Try to handle other keystrokes by determining the
3115 base character (ie. translating the base key plus
3116 shift modifier). */
3117 int add;
3118 int isdead = 0;
3119 KEY_EVENT_RECORD key;
3121 key.bKeyDown = TRUE;
3122 key.wRepeatCount = 1;
3123 key.wVirtualKeyCode = wParam;
3124 key.wVirtualScanCode = (lParam & 0xFF0000) >> 16;
3125 key.uChar.AsciiChar = 0;
3126 key.dwControlKeyState = modifiers;
3128 add = w32_kbd_patch_key (&key);
3129 /* 0 means an unrecognised keycode, negative means
3130 dead key. Ignore both. */
3131 while (--add >= 0)
3133 /* Forward asciified character sequence. */
3134 post_character_message
3135 (hwnd, WM_CHAR, key.uChar.AsciiChar, lParam,
3136 w32_get_key_modifiers (wParam, lParam));
3137 w32_kbd_patch_key (&key);
3139 return 0;
3142 else
3144 /* Let TranslateMessage handle everything else. */
3145 windows_translate = 1;
3150 translate:
3151 if (windows_translate)
3153 MSG windows_msg = { hwnd, msg, wParam, lParam, 0, {0,0} };
3155 windows_msg.time = GetMessageTime ();
3156 TranslateMessage (&windows_msg);
3157 goto dflt;
3160 /* Fall through */
3162 case WM_SYSCHAR:
3163 case WM_CHAR:
3164 post_character_message (hwnd, msg, wParam, lParam,
3165 w32_get_key_modifiers (wParam, lParam));
3166 break;
3168 /* Simulate middle mouse button events when left and right buttons
3169 are used together, but only if user has two button mouse. */
3170 case WM_LBUTTONDOWN:
3171 case WM_RBUTTONDOWN:
3172 if (w32_num_mouse_buttons > 2)
3173 goto handle_plain_button;
3176 int this = (msg == WM_LBUTTONDOWN) ? LMOUSE : RMOUSE;
3177 int other = (msg == WM_LBUTTONDOWN) ? RMOUSE : LMOUSE;
3179 if (button_state & this)
3180 return 0;
3182 if (button_state == 0)
3183 SetCapture (hwnd);
3185 button_state |= this;
3187 if (button_state & other)
3189 if (mouse_button_timer)
3191 KillTimer (hwnd, mouse_button_timer);
3192 mouse_button_timer = 0;
3194 /* Generate middle mouse event instead. */
3195 msg = WM_MBUTTONDOWN;
3196 button_state |= MMOUSE;
3198 else if (button_state & MMOUSE)
3200 /* Ignore button event if we've already generated a
3201 middle mouse down event. This happens if the
3202 user releases and press one of the two buttons
3203 after we've faked a middle mouse event. */
3204 return 0;
3206 else
3208 /* Flush out saved message. */
3209 post_msg (&saved_mouse_button_msg);
3211 wmsg.dwModifiers = w32_get_modifiers ();
3212 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3213 signal_user_input ();
3215 /* Clear message buffer. */
3216 saved_mouse_button_msg.msg.hwnd = 0;
3218 else
3220 /* Hold onto message for now. */
3221 mouse_button_timer =
3222 SetTimer (hwnd, MOUSE_BUTTON_ID,
3223 w32_mouse_button_tolerance, NULL);
3224 saved_mouse_button_msg.msg.hwnd = hwnd;
3225 saved_mouse_button_msg.msg.message = msg;
3226 saved_mouse_button_msg.msg.wParam = wParam;
3227 saved_mouse_button_msg.msg.lParam = lParam;
3228 saved_mouse_button_msg.msg.time = GetMessageTime ();
3229 saved_mouse_button_msg.dwModifiers = w32_get_modifiers ();
3232 return 0;
3234 case WM_LBUTTONUP:
3235 case WM_RBUTTONUP:
3236 if (w32_num_mouse_buttons > 2)
3237 goto handle_plain_button;
3240 int this = (msg == WM_LBUTTONUP) ? LMOUSE : RMOUSE;
3241 int other = (msg == WM_LBUTTONUP) ? RMOUSE : LMOUSE;
3243 if ((button_state & this) == 0)
3244 return 0;
3246 button_state &= ~this;
3248 if (button_state & MMOUSE)
3250 /* Only generate event when second button is released. */
3251 if ((button_state & other) == 0)
3253 msg = WM_MBUTTONUP;
3254 button_state &= ~MMOUSE;
3256 if (button_state) abort ();
3258 else
3259 return 0;
3261 else
3263 /* Flush out saved message if necessary. */
3264 if (saved_mouse_button_msg.msg.hwnd)
3266 post_msg (&saved_mouse_button_msg);
3269 wmsg.dwModifiers = w32_get_modifiers ();
3270 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3271 signal_user_input ();
3273 /* Always clear message buffer and cancel timer. */
3274 saved_mouse_button_msg.msg.hwnd = 0;
3275 KillTimer (hwnd, mouse_button_timer);
3276 mouse_button_timer = 0;
3278 if (button_state == 0)
3279 ReleaseCapture ();
3281 return 0;
3283 case WM_XBUTTONDOWN:
3284 case WM_XBUTTONUP:
3285 if (w32_pass_extra_mouse_buttons_to_system)
3286 goto dflt;
3287 /* else fall through and process them. */
3288 case WM_MBUTTONDOWN:
3289 case WM_MBUTTONUP:
3290 handle_plain_button:
3292 BOOL up;
3293 int button;
3295 /* Ignore middle and extra buttons as long as the menu is active. */
3296 f = x_window_to_frame (dpyinfo, hwnd);
3297 if (f && f->output_data.w32->menubar_active)
3298 return 0;
3300 if (parse_button (msg, HIWORD (wParam), &button, &up))
3302 if (up) ReleaseCapture ();
3303 else SetCapture (hwnd);
3304 button = (button == 0) ? LMOUSE :
3305 ((button == 1) ? MMOUSE : RMOUSE);
3306 if (up)
3307 button_state &= ~button;
3308 else
3309 button_state |= button;
3313 wmsg.dwModifiers = w32_get_modifiers ();
3314 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3315 signal_user_input ();
3317 /* Need to return true for XBUTTON messages, false for others,
3318 to indicate that we processed the message. */
3319 return (msg == WM_XBUTTONDOWN || msg == WM_XBUTTONUP);
3321 case WM_MOUSEMOVE:
3322 /* Ignore mouse movements as long as the menu is active. These
3323 movements are processed by the window manager anyway, and
3324 it's wrong to handle them as if they happened on the
3325 underlying frame. */
3326 f = x_window_to_frame (dpyinfo, hwnd);
3327 if (f && f->output_data.w32->menubar_active)
3328 return 0;
3330 /* If the mouse has just moved into the frame, start tracking
3331 it, so we will be notified when it leaves the frame. Mouse
3332 tracking only works under W98 and NT4 and later. On earlier
3333 versions, there is no way of telling when the mouse leaves the
3334 frame, so we just have to put up with help-echo and mouse
3335 highlighting remaining while the frame is not active. */
3336 if (track_mouse_event_fn && !track_mouse_window)
3338 TRACKMOUSEEVENT tme;
3339 tme.cbSize = sizeof (tme);
3340 tme.dwFlags = TME_LEAVE;
3341 tme.hwndTrack = hwnd;
3343 track_mouse_event_fn (&tme);
3344 track_mouse_window = hwnd;
3346 case WM_VSCROLL:
3347 if (w32_mouse_move_interval <= 0
3348 || (msg == WM_MOUSEMOVE && button_state == 0))
3350 wmsg.dwModifiers = w32_get_modifiers ();
3351 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3352 return 0;
3355 /* Hang onto mouse move and scroll messages for a bit, to avoid
3356 sending such events to Emacs faster than it can process them.
3357 If we get more events before the timer from the first message
3358 expires, we just replace the first message. */
3360 if (saved_mouse_move_msg.msg.hwnd == 0)
3361 mouse_move_timer =
3362 SetTimer (hwnd, MOUSE_MOVE_ID,
3363 w32_mouse_move_interval, NULL);
3365 /* Hold onto message for now. */
3366 saved_mouse_move_msg.msg.hwnd = hwnd;
3367 saved_mouse_move_msg.msg.message = msg;
3368 saved_mouse_move_msg.msg.wParam = wParam;
3369 saved_mouse_move_msg.msg.lParam = lParam;
3370 saved_mouse_move_msg.msg.time = GetMessageTime ();
3371 saved_mouse_move_msg.dwModifiers = w32_get_modifiers ();
3373 return 0;
3375 case WM_MOUSEWHEEL:
3376 wmsg.dwModifiers = w32_get_modifiers ();
3377 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3378 signal_user_input ();
3379 return 0;
3381 case WM_DROPFILES:
3382 wmsg.dwModifiers = w32_get_modifiers ();
3383 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3384 signal_user_input ();
3385 return 0;
3387 case WM_TIMER:
3388 /* Flush out saved messages if necessary. */
3389 if (wParam == mouse_button_timer)
3391 if (saved_mouse_button_msg.msg.hwnd)
3393 post_msg (&saved_mouse_button_msg);
3394 signal_user_input ();
3395 saved_mouse_button_msg.msg.hwnd = 0;
3397 KillTimer (hwnd, mouse_button_timer);
3398 mouse_button_timer = 0;
3400 else if (wParam == mouse_move_timer)
3402 if (saved_mouse_move_msg.msg.hwnd)
3404 post_msg (&saved_mouse_move_msg);
3405 saved_mouse_move_msg.msg.hwnd = 0;
3407 KillTimer (hwnd, mouse_move_timer);
3408 mouse_move_timer = 0;
3410 else if (wParam == menu_free_timer)
3412 KillTimer (hwnd, menu_free_timer);
3413 menu_free_timer = 0;
3414 f = x_window_to_frame (dpyinfo, hwnd);
3415 if (!f->output_data.w32->menu_command_in_progress)
3417 /* Free memory used by owner-drawn and help-echo strings. */
3418 w32_free_menu_strings (hwnd);
3419 f->output_data.w32->menubar_active = 0;
3422 return 0;
3424 case WM_NCACTIVATE:
3425 /* Windows doesn't send us focus messages when putting up and
3426 taking down a system popup dialog as for Ctrl-Alt-Del on Windows 95.
3427 The only indication we get that something happened is receiving
3428 this message afterwards. So this is a good time to reset our
3429 keyboard modifiers' state. */
3430 reset_modifiers ();
3431 goto dflt;
3433 case WM_INITMENU:
3434 button_state = 0;
3435 ReleaseCapture ();
3436 /* We must ensure menu bar is fully constructed and up to date
3437 before allowing user interaction with it. To achieve this
3438 we send this message to the lisp thread and wait for a
3439 reply (whose value is not actually needed) to indicate that
3440 the menu bar is now ready for use, so we can now return.
3442 To remain responsive in the meantime, we enter a nested message
3443 loop that can process all other messages.
3445 However, we skip all this if the message results from calling
3446 TrackPopupMenu - in fact, we must NOT attempt to send the lisp
3447 thread a message because it is blocked on us at this point. We
3448 set menubar_active before calling TrackPopupMenu to indicate
3449 this (there is no possibility of confusion with real menubar
3450 being active). */
3452 f = x_window_to_frame (dpyinfo, hwnd);
3453 if (f
3454 && (f->output_data.w32->menubar_active
3455 /* We can receive this message even in the absence of a
3456 menubar (ie. when the system menu is activated) - in this
3457 case we do NOT want to forward the message, otherwise it
3458 will cause the menubar to suddenly appear when the user
3459 had requested it to be turned off! */
3460 || f->output_data.w32->menubar_widget == NULL))
3461 return 0;
3464 deferred_msg msg_buf;
3466 /* Detect if message has already been deferred; in this case
3467 we cannot return any sensible value to ignore this. */
3468 if (find_deferred_msg (hwnd, msg) != NULL)
3469 abort ();
3471 return send_deferred_msg (&msg_buf, hwnd, msg, wParam, lParam);
3474 case WM_EXITMENULOOP:
3475 f = x_window_to_frame (dpyinfo, hwnd);
3477 /* If a menu command is not already in progress, check again
3478 after a short delay, since Windows often (always?) sends the
3479 WM_EXITMENULOOP before the corresponding WM_COMMAND message. */
3480 if (f && !f->output_data.w32->menu_command_in_progress)
3481 menu_free_timer = SetTimer (hwnd, MENU_FREE_ID, MENU_FREE_DELAY, NULL);
3482 goto dflt;
3484 case WM_MENUSELECT:
3485 /* Direct handling of help_echo in menus. Should be safe now
3486 that we generate the help_echo by placing a help event in the
3487 keyboard buffer. */
3489 HMENU menu = (HMENU) lParam;
3490 UINT menu_item = (UINT) LOWORD (wParam);
3491 UINT flags = (UINT) HIWORD (wParam);
3493 w32_menu_display_help (hwnd, menu, menu_item, flags);
3495 return 0;
3497 case WM_MEASUREITEM:
3498 f = x_window_to_frame (dpyinfo, hwnd);
3499 if (f)
3501 MEASUREITEMSTRUCT * pMis = (MEASUREITEMSTRUCT *) lParam;
3503 if (pMis->CtlType == ODT_MENU)
3505 /* Work out dimensions for popup menu titles. */
3506 char * title = (char *) pMis->itemData;
3507 HDC hdc = GetDC (hwnd);
3508 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
3509 LOGFONT menu_logfont;
3510 HFONT old_font;
3511 SIZE size;
3513 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
3514 menu_logfont.lfWeight = FW_BOLD;
3515 menu_font = CreateFontIndirect (&menu_logfont);
3516 old_font = SelectObject (hdc, menu_font);
3518 pMis->itemHeight = GetSystemMetrics (SM_CYMENUSIZE);
3519 if (title)
3521 if (unicode_append_menu)
3522 GetTextExtentPoint32W (hdc, (WCHAR *) title,
3523 wcslen ((WCHAR *) title),
3524 &size);
3525 else
3526 GetTextExtentPoint32 (hdc, title, strlen (title), &size);
3528 pMis->itemWidth = size.cx;
3529 if (pMis->itemHeight < size.cy)
3530 pMis->itemHeight = size.cy;
3532 else
3533 pMis->itemWidth = 0;
3535 SelectObject (hdc, old_font);
3536 DeleteObject (menu_font);
3537 ReleaseDC (hwnd, hdc);
3538 return TRUE;
3541 return 0;
3543 case WM_DRAWITEM:
3544 f = x_window_to_frame (dpyinfo, hwnd);
3545 if (f)
3547 DRAWITEMSTRUCT * pDis = (DRAWITEMSTRUCT *) lParam;
3549 if (pDis->CtlType == ODT_MENU)
3551 /* Draw popup menu title. */
3552 char * title = (char *) pDis->itemData;
3553 if (title)
3555 HDC hdc = pDis->hDC;
3556 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
3557 LOGFONT menu_logfont;
3558 HFONT old_font;
3560 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
3561 menu_logfont.lfWeight = FW_BOLD;
3562 menu_font = CreateFontIndirect (&menu_logfont);
3563 old_font = SelectObject (hdc, menu_font);
3565 /* Always draw title as if not selected. */
3566 if (unicode_append_menu)
3567 ExtTextOutW (hdc,
3568 pDis->rcItem.left
3569 + GetSystemMetrics (SM_CXMENUCHECK),
3570 pDis->rcItem.top,
3571 ETO_OPAQUE, &pDis->rcItem,
3572 (WCHAR *) title,
3573 wcslen ((WCHAR *) title), NULL);
3574 else
3575 ExtTextOut (hdc,
3576 pDis->rcItem.left
3577 + GetSystemMetrics (SM_CXMENUCHECK),
3578 pDis->rcItem.top,
3579 ETO_OPAQUE, &pDis->rcItem,
3580 title, strlen (title), NULL);
3582 SelectObject (hdc, old_font);
3583 DeleteObject (menu_font);
3585 return TRUE;
3588 return 0;
3590 #if 0
3591 /* Still not right - can't distinguish between clicks in the
3592 client area of the frame from clicks forwarded from the scroll
3593 bars - may have to hook WM_NCHITTEST to remember the mouse
3594 position and then check if it is in the client area ourselves. */
3595 case WM_MOUSEACTIVATE:
3596 /* Discard the mouse click that activates a frame, allowing the
3597 user to click anywhere without changing point (or worse!).
3598 Don't eat mouse clicks on scrollbars though!! */
3599 if (LOWORD (lParam) == HTCLIENT )
3600 return MA_ACTIVATEANDEAT;
3601 goto dflt;
3602 #endif
3604 case WM_MOUSELEAVE:
3605 /* No longer tracking mouse. */
3606 track_mouse_window = NULL;
3608 case WM_ACTIVATEAPP:
3609 case WM_ACTIVATE:
3610 case WM_WINDOWPOSCHANGED:
3611 case WM_SHOWWINDOW:
3612 /* Inform lisp thread that a frame might have just been obscured
3613 or exposed, so should recheck visibility of all frames. */
3614 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3615 goto dflt;
3617 case WM_SETFOCUS:
3618 dpyinfo->faked_key = 0;
3619 reset_modifiers ();
3620 register_hot_keys (hwnd);
3621 goto command;
3622 case WM_KILLFOCUS:
3623 unregister_hot_keys (hwnd);
3624 button_state = 0;
3625 ReleaseCapture ();
3626 /* Relinquish the system caret. */
3627 if (w32_system_caret_hwnd)
3629 w32_visible_system_caret_hwnd = NULL;
3630 w32_system_caret_hwnd = NULL;
3631 DestroyCaret ();
3633 goto command;
3634 case WM_COMMAND:
3635 f = x_window_to_frame (dpyinfo, hwnd);
3636 if (f && HIWORD (wParam) == 0)
3638 f->output_data.w32->menu_command_in_progress = 1;
3639 if (menu_free_timer)
3641 KillTimer (hwnd, menu_free_timer);
3642 menu_free_timer = 0;
3645 case WM_MOVE:
3646 case WM_SIZE:
3647 command:
3648 wmsg.dwModifiers = w32_get_modifiers ();
3649 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3650 goto dflt;
3652 case WM_CLOSE:
3653 wmsg.dwModifiers = w32_get_modifiers ();
3654 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3655 return 0;
3657 case WM_WINDOWPOSCHANGING:
3658 /* Don't restrict the sizing of tip frames. */
3659 if (hwnd == tip_window)
3660 return 0;
3662 WINDOWPLACEMENT wp;
3663 LPWINDOWPOS lppos = (WINDOWPOS *) lParam;
3665 wp.length = sizeof (WINDOWPLACEMENT);
3666 GetWindowPlacement (hwnd, &wp);
3668 if (wp.showCmd != SW_SHOWMINIMIZED && (lppos->flags & SWP_NOSIZE) == 0)
3670 RECT rect;
3671 int wdiff;
3672 int hdiff;
3673 DWORD font_width;
3674 DWORD line_height;
3675 DWORD internal_border;
3676 DWORD scrollbar_extra;
3677 RECT wr;
3679 wp.length = sizeof(wp);
3680 GetWindowRect (hwnd, &wr);
3682 enter_crit ();
3684 font_width = GetWindowLong (hwnd, WND_FONTWIDTH_INDEX);
3685 line_height = GetWindowLong (hwnd, WND_LINEHEIGHT_INDEX);
3686 internal_border = GetWindowLong (hwnd, WND_BORDER_INDEX);
3687 scrollbar_extra = GetWindowLong (hwnd, WND_SCROLLBAR_INDEX);
3689 leave_crit ();
3691 memset (&rect, 0, sizeof (rect));
3692 AdjustWindowRect (&rect, GetWindowLong (hwnd, GWL_STYLE),
3693 GetMenu (hwnd) != NULL);
3695 /* Force width and height of client area to be exact
3696 multiples of the character cell dimensions. */
3697 wdiff = (lppos->cx - (rect.right - rect.left)
3698 - 2 * internal_border - scrollbar_extra)
3699 % font_width;
3700 hdiff = (lppos->cy - (rect.bottom - rect.top)
3701 - 2 * internal_border)
3702 % line_height;
3704 if (wdiff || hdiff)
3706 /* For right/bottom sizing we can just fix the sizes.
3707 However for top/left sizing we will need to fix the X
3708 and Y positions as well. */
3710 lppos->cx -= wdiff;
3711 lppos->cy -= hdiff;
3713 if (wp.showCmd != SW_SHOWMAXIMIZED
3714 && (lppos->flags & SWP_NOMOVE) == 0)
3716 if (lppos->x != wr.left || lppos->y != wr.top)
3718 lppos->x += wdiff;
3719 lppos->y += hdiff;
3721 else
3723 lppos->flags |= SWP_NOMOVE;
3727 return 0;
3732 goto dflt;
3734 case WM_GETMINMAXINFO:
3735 /* Hack to correct bug that allows Emacs frames to be resized
3736 below the Minimum Tracking Size. */
3737 ((LPMINMAXINFO) lParam)->ptMinTrackSize.y++;
3738 /* Hack to allow resizing the Emacs frame above the screen size.
3739 Note that Windows 9x limits coordinates to 16-bits. */
3740 ((LPMINMAXINFO) lParam)->ptMaxTrackSize.x = 32767;
3741 ((LPMINMAXINFO) lParam)->ptMaxTrackSize.y = 32767;
3742 return 0;
3744 case WM_SETCURSOR:
3745 if (LOWORD (lParam) == HTCLIENT)
3746 return 0;
3748 goto dflt;
3750 case WM_EMACS_SETCURSOR:
3752 Cursor cursor = (Cursor) wParam;
3753 if (cursor)
3754 SetCursor (cursor);
3755 return 0;
3758 case WM_EMACS_CREATESCROLLBAR:
3759 return (LRESULT) w32_createscrollbar ((struct frame *) wParam,
3760 (struct scroll_bar *) lParam);
3762 case WM_EMACS_SHOWWINDOW:
3763 return ShowWindow ((HWND) wParam, (WPARAM) lParam);
3765 case WM_EMACS_SETFOREGROUND:
3767 HWND foreground_window;
3768 DWORD foreground_thread, retval;
3770 /* On NT 5.0, and apparently Windows 98, it is necessary to
3771 attach to the thread that currently has focus in order to
3772 pull the focus away from it. */
3773 foreground_window = GetForegroundWindow ();
3774 foreground_thread = GetWindowThreadProcessId (foreground_window, NULL);
3775 if (!foreground_window
3776 || foreground_thread == GetCurrentThreadId ()
3777 || !AttachThreadInput (GetCurrentThreadId (),
3778 foreground_thread, TRUE))
3779 foreground_thread = 0;
3781 retval = SetForegroundWindow ((HWND) wParam);
3783 /* Detach from the previous foreground thread. */
3784 if (foreground_thread)
3785 AttachThreadInput (GetCurrentThreadId (),
3786 foreground_thread, FALSE);
3788 return retval;
3791 case WM_EMACS_SETWINDOWPOS:
3793 WINDOWPOS * pos = (WINDOWPOS *) wParam;
3794 return SetWindowPos (hwnd, pos->hwndInsertAfter,
3795 pos->x, pos->y, pos->cx, pos->cy, pos->flags);
3798 case WM_EMACS_DESTROYWINDOW:
3799 DragAcceptFiles ((HWND) wParam, FALSE);
3800 return DestroyWindow ((HWND) wParam);
3802 case WM_EMACS_HIDE_CARET:
3803 return HideCaret (hwnd);
3805 case WM_EMACS_SHOW_CARET:
3806 return ShowCaret (hwnd);
3808 case WM_EMACS_DESTROY_CARET:
3809 w32_system_caret_hwnd = NULL;
3810 w32_visible_system_caret_hwnd = NULL;
3811 return DestroyCaret ();
3813 case WM_EMACS_TRACK_CARET:
3814 /* If there is currently no system caret, create one. */
3815 if (w32_system_caret_hwnd == NULL)
3817 /* Use the default caret width, and avoid changing it
3818 unneccesarily, as it confuses screen reader software. */
3819 w32_system_caret_hwnd = hwnd;
3820 CreateCaret (hwnd, NULL, 0,
3821 w32_system_caret_height);
3824 if (!SetCaretPos (w32_system_caret_x, w32_system_caret_y))
3825 return 0;
3826 /* Ensure visible caret gets turned on when requested. */
3827 else if (w32_use_visible_system_caret
3828 && w32_visible_system_caret_hwnd != hwnd)
3830 w32_visible_system_caret_hwnd = hwnd;
3831 return ShowCaret (hwnd);
3833 /* Ensure visible caret gets turned off when requested. */
3834 else if (!w32_use_visible_system_caret
3835 && w32_visible_system_caret_hwnd)
3837 w32_visible_system_caret_hwnd = NULL;
3838 return HideCaret (hwnd);
3840 else
3841 return 1;
3843 case WM_EMACS_TRACKPOPUPMENU:
3845 UINT flags;
3846 POINT *pos;
3847 int retval;
3848 pos = (POINT *)lParam;
3849 flags = TPM_CENTERALIGN;
3850 if (button_state & LMOUSE)
3851 flags |= TPM_LEFTBUTTON;
3852 else if (button_state & RMOUSE)
3853 flags |= TPM_RIGHTBUTTON;
3855 /* Remember we did a SetCapture on the initial mouse down event,
3856 so for safety, we make sure the capture is cancelled now. */
3857 ReleaseCapture ();
3858 button_state = 0;
3860 /* Use menubar_active to indicate that WM_INITMENU is from
3861 TrackPopupMenu below, and should be ignored. */
3862 f = x_window_to_frame (dpyinfo, hwnd);
3863 if (f)
3864 f->output_data.w32->menubar_active = 1;
3866 if (TrackPopupMenu ((HMENU)wParam, flags, pos->x, pos->y,
3867 0, hwnd, NULL))
3869 MSG amsg;
3870 /* Eat any mouse messages during popupmenu */
3871 while (PeekMessage (&amsg, hwnd, WM_MOUSEFIRST, WM_MOUSELAST,
3872 PM_REMOVE));
3873 /* Get the menu selection, if any */
3874 if (PeekMessage (&amsg, hwnd, WM_COMMAND, WM_COMMAND, PM_REMOVE))
3876 retval = LOWORD (amsg.wParam);
3878 else
3880 retval = 0;
3883 else
3885 retval = -1;
3888 return retval;
3891 default:
3892 /* Check for messages registered at runtime. */
3893 if (msg == msh_mousewheel)
3895 wmsg.dwModifiers = w32_get_modifiers ();
3896 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3897 signal_user_input ();
3898 return 0;
3901 dflt:
3902 return DefWindowProc (hwnd, msg, wParam, lParam);
3906 /* The most common default return code for handled messages is 0. */
3907 return 0;
3910 static void
3911 my_create_window (f)
3912 struct frame * f;
3914 MSG msg;
3916 if (!PostThreadMessage (dwWindowsThreadId, WM_EMACS_CREATEWINDOW, (WPARAM)f, 0))
3917 abort ();
3918 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
3922 /* Create a tooltip window. Unlike my_create_window, we do not do this
3923 indirectly via the Window thread, as we do not need to process Window
3924 messages for the tooltip. Creating tooltips indirectly also creates
3925 deadlocks when tooltips are created for menu items. */
3926 static void
3927 my_create_tip_window (f)
3928 struct frame *f;
3930 RECT rect;
3932 rect.left = rect.top = 0;
3933 rect.right = FRAME_PIXEL_WIDTH (f);
3934 rect.bottom = FRAME_PIXEL_HEIGHT (f);
3936 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
3937 FRAME_EXTERNAL_MENU_BAR (f));
3939 tip_window = FRAME_W32_WINDOW (f)
3940 = CreateWindow (EMACS_CLASS,
3941 f->namebuf,
3942 f->output_data.w32->dwStyle,
3943 f->left_pos,
3944 f->top_pos,
3945 rect.right - rect.left,
3946 rect.bottom - rect.top,
3947 FRAME_W32_WINDOW (SELECTED_FRAME ()), /* owner */
3948 NULL,
3949 hinst,
3950 NULL);
3952 if (tip_window)
3954 SetWindowLong (tip_window, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
3955 SetWindowLong (tip_window, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
3956 SetWindowLong (tip_window, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
3957 SetWindowLong (tip_window, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
3959 /* Tip frames have no scrollbars. */
3960 SetWindowLong (tip_window, WND_SCROLLBAR_INDEX, 0);
3962 /* Do this to discard the default setting specified by our parent. */
3963 ShowWindow (tip_window, SW_HIDE);
3968 /* Create and set up the w32 window for frame F. */
3970 static void
3971 w32_window (f, window_prompting, minibuffer_only)
3972 struct frame *f;
3973 long window_prompting;
3974 int minibuffer_only;
3976 BLOCK_INPUT;
3978 /* Use the resource name as the top-level window name
3979 for looking up resources. Make a non-Lisp copy
3980 for the window manager, so GC relocation won't bother it.
3982 Elsewhere we specify the window name for the window manager. */
3985 char *str = (char *) SDATA (Vx_resource_name);
3986 f->namebuf = (char *) xmalloc (strlen (str) + 1);
3987 strcpy (f->namebuf, str);
3990 my_create_window (f);
3992 validate_x_resource_name ();
3994 /* x_set_name normally ignores requests to set the name if the
3995 requested name is the same as the current name. This is the one
3996 place where that assumption isn't correct; f->name is set, but
3997 the server hasn't been told. */
3999 Lisp_Object name;
4000 int explicit = f->explicit_name;
4002 f->explicit_name = 0;
4003 name = f->name;
4004 f->name = Qnil;
4005 x_set_name (f, name, explicit);
4008 UNBLOCK_INPUT;
4010 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
4011 initialize_frame_menubar (f);
4013 if (FRAME_W32_WINDOW (f) == 0)
4014 error ("Unable to create window");
4017 /* Handle the icon stuff for this window. Perhaps later we might
4018 want an x_set_icon_position which can be called interactively as
4019 well. */
4021 static void
4022 x_icon (f, parms)
4023 struct frame *f;
4024 Lisp_Object parms;
4026 Lisp_Object icon_x, icon_y;
4028 /* Set the position of the icon. Note that Windows 95 groups all
4029 icons in the tray. */
4030 icon_x = w32_get_arg (parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
4031 icon_y = w32_get_arg (parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
4032 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
4034 CHECK_NUMBER (icon_x);
4035 CHECK_NUMBER (icon_y);
4037 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
4038 error ("Both left and top icon corners of icon must be specified");
4040 BLOCK_INPUT;
4042 if (! EQ (icon_x, Qunbound))
4043 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
4045 #if 0 /* TODO */
4046 /* Start up iconic or window? */
4047 x_wm_set_window_state
4048 (f, (EQ (w32_get_arg (parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL), Qicon)
4049 ? IconicState
4050 : NormalState));
4052 x_text_icon (f, (char *) SDATA ((!NILP (f->icon_name)
4053 ? f->icon_name
4054 : f->name)));
4055 #endif
4057 UNBLOCK_INPUT;
4061 static void
4062 x_make_gc (f)
4063 struct frame *f;
4065 XGCValues gc_values;
4067 BLOCK_INPUT;
4069 /* Create the GC's of this frame.
4070 Note that many default values are used. */
4072 /* Normal video */
4073 gc_values.font = FRAME_FONT (f);
4075 /* Cursor has cursor-color background, background-color foreground. */
4076 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
4077 gc_values.background = f->output_data.w32->cursor_pixel;
4078 f->output_data.w32->cursor_gc
4079 = XCreateGC (NULL, FRAME_W32_WINDOW (f),
4080 (GCFont | GCForeground | GCBackground),
4081 &gc_values);
4083 /* Reliefs. */
4084 f->output_data.w32->white_relief.gc = 0;
4085 f->output_data.w32->black_relief.gc = 0;
4087 UNBLOCK_INPUT;
4091 /* Handler for signals raised during x_create_frame and
4092 x_create_top_frame. FRAME is the frame which is partially
4093 constructed. */
4095 static Lisp_Object
4096 unwind_create_frame (frame)
4097 Lisp_Object frame;
4099 struct frame *f = XFRAME (frame);
4101 /* If frame is ``official'', nothing to do. */
4102 if (!CONSP (Vframe_list) || !EQ (XCAR (Vframe_list), frame))
4104 #ifdef GLYPH_DEBUG
4105 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4106 #endif
4108 x_free_frame_resources (f);
4110 /* Check that reference counts are indeed correct. */
4111 xassert (dpyinfo->reference_count == dpyinfo_refcount);
4112 xassert (dpyinfo->image_cache->refcount == image_cache_refcount);
4114 return Qt;
4117 return Qnil;
4121 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
4122 1, 1, 0,
4123 doc: /* Make a new window, which is called a \"frame\" in Emacs terms.
4124 Returns an Emacs frame object.
4125 PARAMETERS is an alist of frame parameters.
4126 If the parameters specify that the frame should not have a minibuffer,
4127 and do not specify a specific minibuffer window to use,
4128 then `default-minibuffer-frame' must be a frame whose minibuffer can
4129 be shared by the new frame.
4131 This function is an internal primitive--use `make-frame' instead. */)
4132 (parameters)
4133 Lisp_Object parameters;
4135 struct frame *f;
4136 Lisp_Object frame, tem;
4137 Lisp_Object name;
4138 int minibuffer_only = 0;
4139 long window_prompting = 0;
4140 int width, height;
4141 int count = SPECPDL_INDEX ();
4142 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4143 Lisp_Object display;
4144 struct w32_display_info *dpyinfo = NULL;
4145 Lisp_Object parent;
4146 struct kboard *kb;
4148 check_w32 ();
4150 /* Use this general default value to start with
4151 until we know if this frame has a specified name. */
4152 Vx_resource_name = Vinvocation_name;
4154 display = w32_get_arg (parameters, Qdisplay, 0, 0, RES_TYPE_STRING);
4155 if (EQ (display, Qunbound))
4156 display = Qnil;
4157 dpyinfo = check_x_display_info (display);
4158 #ifdef MULTI_KBOARD
4159 kb = dpyinfo->kboard;
4160 #else
4161 kb = &the_only_kboard;
4162 #endif
4164 name = w32_get_arg (parameters, Qname, "name", "Name", RES_TYPE_STRING);
4165 if (!STRINGP (name)
4166 && ! EQ (name, Qunbound)
4167 && ! NILP (name))
4168 error ("Invalid frame name--not a string or nil");
4170 if (STRINGP (name))
4171 Vx_resource_name = name;
4173 /* See if parent window is specified. */
4174 parent = w32_get_arg (parameters, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
4175 if (EQ (parent, Qunbound))
4176 parent = Qnil;
4177 if (! NILP (parent))
4178 CHECK_NUMBER (parent);
4180 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
4181 /* No need to protect DISPLAY because that's not used after passing
4182 it to make_frame_without_minibuffer. */
4183 frame = Qnil;
4184 GCPRO4 (parameters, parent, name, frame);
4185 tem = w32_get_arg (parameters, Qminibuffer, "minibuffer", "Minibuffer",
4186 RES_TYPE_SYMBOL);
4187 if (EQ (tem, Qnone) || NILP (tem))
4188 f = make_frame_without_minibuffer (Qnil, kb, display);
4189 else if (EQ (tem, Qonly))
4191 f = make_minibuffer_frame ();
4192 minibuffer_only = 1;
4194 else if (WINDOWP (tem))
4195 f = make_frame_without_minibuffer (tem, kb, display);
4196 else
4197 f = make_frame (1);
4199 XSETFRAME (frame, f);
4201 /* Note that Windows does support scroll bars. */
4202 FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;
4204 /* By default, make scrollbars the system standard width. */
4205 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = GetSystemMetrics (SM_CXVSCROLL);
4207 f->output_method = output_w32;
4208 f->output_data.w32 =
4209 (struct w32_output *) xmalloc (sizeof (struct w32_output));
4210 bzero (f->output_data.w32, sizeof (struct w32_output));
4211 FRAME_FONTSET (f) = -1;
4212 record_unwind_protect (unwind_create_frame, frame);
4214 f->icon_name
4215 = w32_get_arg (parameters, Qicon_name, "iconName", "Title", RES_TYPE_STRING);
4216 if (! STRINGP (f->icon_name))
4217 f->icon_name = Qnil;
4219 /* FRAME_W32_DISPLAY_INFO (f) = dpyinfo; */
4220 #ifdef MULTI_KBOARD
4221 FRAME_KBOARD (f) = kb;
4222 #endif
4224 /* Specify the parent under which to make this window. */
4226 if (!NILP (parent))
4228 f->output_data.w32->parent_desc = (Window) XFASTINT (parent);
4229 f->output_data.w32->explicit_parent = 1;
4231 else
4233 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
4234 f->output_data.w32->explicit_parent = 0;
4237 /* Set the name; the functions to which we pass f expect the name to
4238 be set. */
4239 if (EQ (name, Qunbound) || NILP (name))
4241 f->name = build_string (dpyinfo->w32_id_name);
4242 f->explicit_name = 0;
4244 else
4246 f->name = name;
4247 f->explicit_name = 1;
4248 /* use the frame's title when getting resources for this frame. */
4249 specbind (Qx_resource_name, name);
4252 /* Extract the window parameters from the supplied values
4253 that are needed to determine window geometry. */
4255 Lisp_Object font;
4257 font = w32_get_arg (parameters, Qfont, "font", "Font", RES_TYPE_STRING);
4259 BLOCK_INPUT;
4260 /* First, try whatever font the caller has specified. */
4261 if (STRINGP (font))
4263 tem = Fquery_fontset (font, Qnil);
4264 if (STRINGP (tem))
4265 font = x_new_fontset (f, SDATA (tem));
4266 else
4267 font = x_new_font (f, SDATA (font));
4269 /* Try out a font which we hope has bold and italic variations. */
4270 if (!STRINGP (font))
4271 font = x_new_font (f, "-*-Courier New-normal-r-*-*-*-100-*-*-c-*-iso8859-1");
4272 if (! STRINGP (font))
4273 font = x_new_font (f, "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1");
4274 /* If those didn't work, look for something which will at least work. */
4275 if (! STRINGP (font))
4276 font = x_new_font (f, "-*-Fixedsys-normal-r-*-*-12-*-*-*-c-*-iso8859-1");
4277 UNBLOCK_INPUT;
4278 if (! STRINGP (font))
4279 font = build_string ("Fixedsys");
4281 x_default_parameter (f, parameters, Qfont, font,
4282 "font", "Font", RES_TYPE_STRING);
4285 x_default_parameter (f, parameters, Qborder_width, make_number (2),
4286 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
4287 /* This defaults to 2 in order to match xterm. We recognize either
4288 internalBorderWidth or internalBorder (which is what xterm calls
4289 it). */
4290 if (NILP (Fassq (Qinternal_border_width, parameters)))
4292 Lisp_Object value;
4294 value = w32_get_arg (parameters, Qinternal_border_width,
4295 "internalBorder", "InternalBorder", RES_TYPE_NUMBER);
4296 if (! EQ (value, Qunbound))
4297 parameters = Fcons (Fcons (Qinternal_border_width, value),
4298 parameters);
4300 /* Default internalBorderWidth to 0 on Windows to match other programs. */
4301 x_default_parameter (f, parameters, Qinternal_border_width, make_number (0),
4302 "internalBorderWidth", "InternalBorder", RES_TYPE_NUMBER);
4303 x_default_parameter (f, parameters, Qvertical_scroll_bars, Qright,
4304 "verticalScrollBars", "ScrollBars", RES_TYPE_SYMBOL);
4306 /* Also do the stuff which must be set before the window exists. */
4307 x_default_parameter (f, parameters, Qforeground_color, build_string ("black"),
4308 "foreground", "Foreground", RES_TYPE_STRING);
4309 x_default_parameter (f, parameters, Qbackground_color, build_string ("white"),
4310 "background", "Background", RES_TYPE_STRING);
4311 x_default_parameter (f, parameters, Qmouse_color, build_string ("black"),
4312 "pointerColor", "Foreground", RES_TYPE_STRING);
4313 x_default_parameter (f, parameters, Qcursor_color, build_string ("black"),
4314 "cursorColor", "Foreground", RES_TYPE_STRING);
4315 x_default_parameter (f, parameters, Qborder_color, build_string ("black"),
4316 "borderColor", "BorderColor", RES_TYPE_STRING);
4317 x_default_parameter (f, parameters, Qscreen_gamma, Qnil,
4318 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
4319 x_default_parameter (f, parameters, Qline_spacing, Qnil,
4320 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
4321 x_default_parameter (f, parameters, Qleft_fringe, Qnil,
4322 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
4323 x_default_parameter (f, parameters, Qright_fringe, Qnil,
4324 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
4327 /* Init faces before x_default_parameter is called for scroll-bar
4328 parameters because that function calls x_set_scroll_bar_width,
4329 which calls change_frame_size, which calls Fset_window_buffer,
4330 which runs hooks, which call Fvertical_motion. At the end, we
4331 end up in init_iterator with a null face cache, which should not
4332 happen. */
4333 init_frame_faces (f);
4335 x_default_parameter (f, parameters, Qmenu_bar_lines, make_number (1),
4336 "menuBar", "MenuBar", RES_TYPE_NUMBER);
4337 x_default_parameter (f, parameters, Qtool_bar_lines, make_number (1),
4338 "toolBar", "ToolBar", RES_TYPE_NUMBER);
4340 x_default_parameter (f, parameters, Qbuffer_predicate, Qnil,
4341 "bufferPredicate", "BufferPredicate", RES_TYPE_SYMBOL);
4342 x_default_parameter (f, parameters, Qtitle, Qnil,
4343 "title", "Title", RES_TYPE_STRING);
4344 x_default_parameter (f, parameters, Qfullscreen, Qnil,
4345 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
4347 f->output_data.w32->dwStyle = WS_OVERLAPPEDWINDOW;
4348 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
4350 f->output_data.w32->text_cursor = w32_load_cursor (IDC_IBEAM);
4351 f->output_data.w32->nontext_cursor = w32_load_cursor (IDC_ARROW);
4352 f->output_data.w32->modeline_cursor = w32_load_cursor (IDC_ARROW);
4353 f->output_data.w32->hand_cursor = w32_load_cursor (IDC_HAND);
4354 f->output_data.w32->hourglass_cursor = w32_load_cursor (IDC_WAIT);
4355 f->output_data.w32->horizontal_drag_cursor = w32_load_cursor (IDC_SIZEWE);
4357 window_prompting = x_figure_window_size (f, parameters, 1);
4359 tem = w32_get_arg (parameters, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
4360 f->no_split = minibuffer_only || EQ (tem, Qt);
4362 w32_window (f, window_prompting, minibuffer_only);
4363 x_icon (f, parameters);
4365 x_make_gc (f);
4367 /* Now consider the frame official. */
4368 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
4369 Vframe_list = Fcons (frame, Vframe_list);
4371 /* We need to do this after creating the window, so that the
4372 icon-creation functions can say whose icon they're describing. */
4373 x_default_parameter (f, parameters, Qicon_type, Qnil,
4374 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
4376 x_default_parameter (f, parameters, Qauto_raise, Qnil,
4377 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4378 x_default_parameter (f, parameters, Qauto_lower, Qnil,
4379 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4380 x_default_parameter (f, parameters, Qcursor_type, Qbox,
4381 "cursorType", "CursorType", RES_TYPE_SYMBOL);
4382 x_default_parameter (f, parameters, Qscroll_bar_width, Qnil,
4383 "scrollBarWidth", "ScrollBarWidth", RES_TYPE_NUMBER);
4385 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
4386 Change will not be effected unless different from the current
4387 FRAME_LINES (f). */
4388 width = FRAME_COLS (f);
4389 height = FRAME_LINES (f);
4391 FRAME_LINES (f) = 0;
4392 SET_FRAME_COLS (f, 0);
4393 change_frame_size (f, height, width, 1, 0, 0);
4395 /* Tell the server what size and position, etc, we want, and how
4396 badly we want them. This should be done after we have the menu
4397 bar so that its size can be taken into account. */
4398 BLOCK_INPUT;
4399 x_wm_set_size_hint (f, window_prompting, 0);
4400 UNBLOCK_INPUT;
4402 /* Make the window appear on the frame and enable display, unless
4403 the caller says not to. However, with explicit parent, Emacs
4404 cannot control visibility, so don't try. */
4405 if (! f->output_data.w32->explicit_parent)
4407 Lisp_Object visibility;
4409 visibility = w32_get_arg (parameters, Qvisibility, 0, 0, RES_TYPE_SYMBOL);
4410 if (EQ (visibility, Qunbound))
4411 visibility = Qt;
4413 if (EQ (visibility, Qicon))
4414 x_iconify_frame (f);
4415 else if (! NILP (visibility))
4416 x_make_frame_visible (f);
4417 else
4418 /* Must have been Qnil. */
4421 UNGCPRO;
4423 /* Make sure windows on this frame appear in calls to next-window
4424 and similar functions. */
4425 Vwindow_list = Qnil;
4427 return unbind_to (count, frame);
4430 /* FRAME is used only to get a handle on the X display. We don't pass the
4431 display info directly because we're called from frame.c, which doesn't
4432 know about that structure. */
4433 Lisp_Object
4434 x_get_focus_frame (frame)
4435 struct frame *frame;
4437 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (frame);
4438 Lisp_Object xfocus;
4439 if (! dpyinfo->w32_focus_frame)
4440 return Qnil;
4442 XSETFRAME (xfocus, dpyinfo->w32_focus_frame);
4443 return xfocus;
4446 DEFUN ("w32-focus-frame", Fw32_focus_frame, Sw32_focus_frame, 1, 1, 0,
4447 doc: /* Give FRAME input focus, raising to foreground if necessary. */)
4448 (frame)
4449 Lisp_Object frame;
4451 x_focus_on_frame (check_x_frame (frame));
4452 return Qnil;
4456 /* Return the charset portion of a font name. */
4457 char * xlfd_charset_of_font (char * fontname)
4459 char *charset, *encoding;
4461 encoding = strrchr(fontname, '-');
4462 if (!encoding || encoding == fontname)
4463 return NULL;
4465 for (charset = encoding - 1; charset >= fontname; charset--)
4466 if (*charset == '-')
4467 break;
4469 if (charset == fontname || strcmp(charset, "-*-*") == 0)
4470 return NULL;
4472 return charset + 1;
4475 struct font_info *w32_load_bdf_font (struct frame *f, char *fontname,
4476 int size, char* filename);
4477 static Lisp_Object w32_list_bdf_fonts (Lisp_Object pattern, int max_names);
4478 static BOOL w32_to_x_font (LOGFONT * lplf, char * lpxstr, int len,
4479 char * charset);
4480 static BOOL x_to_w32_font (char *lpxstr, LOGFONT *lplogfont);
4482 static struct font_info *
4483 w32_load_system_font (f,fontname,size)
4484 struct frame *f;
4485 char * fontname;
4486 int size;
4488 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4489 Lisp_Object font_names;
4491 /* Get a list of all the fonts that match this name. Once we
4492 have a list of matching fonts, we compare them against the fonts
4493 we already have loaded by comparing names. */
4494 font_names = w32_list_fonts (f, build_string (fontname), size, 100);
4496 if (!NILP (font_names))
4498 Lisp_Object tail;
4499 int i;
4501 /* First check if any are already loaded, as that is cheaper
4502 than loading another one. */
4503 for (i = 0; i < dpyinfo->n_fonts; i++)
4504 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
4505 if (dpyinfo->font_table[i].name
4506 && (!strcmp (dpyinfo->font_table[i].name,
4507 SDATA (XCAR (tail)))
4508 || !strcmp (dpyinfo->font_table[i].full_name,
4509 SDATA (XCAR (tail)))))
4510 return (dpyinfo->font_table + i);
4512 fontname = (char *) SDATA (XCAR (font_names));
4514 else if (w32_strict_fontnames)
4516 /* If EnumFontFamiliesEx was available, we got a full list of
4517 fonts back so stop now to avoid the possibility of loading a
4518 random font. If we had to fall back to EnumFontFamilies, the
4519 list is incomplete, so continue whether the font we want was
4520 listed or not. */
4521 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
4522 FARPROC enum_font_families_ex
4523 = GetProcAddress (gdi32, "EnumFontFamiliesExA");
4524 if (enum_font_families_ex)
4525 return NULL;
4528 /* Load the font and add it to the table. */
4530 char *full_name, *encoding, *charset;
4531 XFontStruct *font;
4532 struct font_info *fontp;
4533 LOGFONT lf;
4534 BOOL ok;
4535 int codepage;
4536 int i;
4538 if (!fontname || !x_to_w32_font (fontname, &lf))
4539 return (NULL);
4541 if (!*lf.lfFaceName)
4542 /* If no name was specified for the font, we get a random font
4543 from CreateFontIndirect - this is not particularly
4544 desirable, especially since CreateFontIndirect does not
4545 fill out the missing name in lf, so we never know what we
4546 ended up with. */
4547 return NULL;
4549 lf.lfQuality = DEFAULT_QUALITY;
4551 font = (XFontStruct *) xmalloc (sizeof (XFontStruct));
4552 bzero (font, sizeof (*font));
4554 /* Set bdf to NULL to indicate that this is a Windows font. */
4555 font->bdf = NULL;
4557 BLOCK_INPUT;
4559 font->hfont = CreateFontIndirect (&lf);
4561 if (font->hfont == NULL)
4563 ok = FALSE;
4565 else
4567 HDC hdc;
4568 HANDLE oldobj;
4570 codepage = w32_codepage_for_font (fontname);
4572 hdc = GetDC (dpyinfo->root_window);
4573 oldobj = SelectObject (hdc, font->hfont);
4575 ok = GetTextMetrics (hdc, &font->tm);
4576 if (codepage == CP_UNICODE)
4577 font->double_byte_p = 1;
4578 else
4580 /* Unfortunately, some fonts (eg. MingLiU, a big5 ttf font)
4581 don't report themselves as double byte fonts, when
4582 patently they are. So instead of trusting
4583 GetFontLanguageInfo, we check the properties of the
4584 codepage directly, since that is ultimately what we are
4585 working from anyway. */
4586 /* font->double_byte_p = GetFontLanguageInfo(hdc) & GCP_DBCS; */
4587 CPINFO cpi = {0};
4588 GetCPInfo (codepage, &cpi);
4589 font->double_byte_p = cpi.MaxCharSize > 1;
4592 SelectObject (hdc, oldobj);
4593 ReleaseDC (dpyinfo->root_window, hdc);
4594 /* Fill out details in lf according to the font that was
4595 actually loaded. */
4596 lf.lfHeight = font->tm.tmInternalLeading - font->tm.tmHeight;
4597 lf.lfWidth = font->tm.tmMaxCharWidth;
4598 lf.lfWeight = font->tm.tmWeight;
4599 lf.lfItalic = font->tm.tmItalic;
4600 lf.lfCharSet = font->tm.tmCharSet;
4601 lf.lfPitchAndFamily = ((font->tm.tmPitchAndFamily & TMPF_FIXED_PITCH)
4602 ? VARIABLE_PITCH : FIXED_PITCH);
4603 lf.lfOutPrecision = ((font->tm.tmPitchAndFamily & TMPF_VECTOR)
4604 ? OUT_STROKE_PRECIS : OUT_STRING_PRECIS);
4606 w32_cache_char_metrics (font);
4609 UNBLOCK_INPUT;
4611 if (!ok)
4613 w32_unload_font (dpyinfo, font);
4614 return (NULL);
4617 /* Find a free slot in the font table. */
4618 for (i = 0; i < dpyinfo->n_fonts; ++i)
4619 if (dpyinfo->font_table[i].name == NULL)
4620 break;
4622 /* If no free slot found, maybe enlarge the font table. */
4623 if (i == dpyinfo->n_fonts
4624 && dpyinfo->n_fonts == dpyinfo->font_table_size)
4626 int sz;
4627 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
4628 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
4629 dpyinfo->font_table
4630 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
4633 fontp = dpyinfo->font_table + i;
4634 if (i == dpyinfo->n_fonts)
4635 ++dpyinfo->n_fonts;
4637 /* Now fill in the slots of *FONTP. */
4638 BLOCK_INPUT;
4639 bzero (fontp, sizeof (*fontp));
4640 fontp->font = font;
4641 fontp->font_idx = i;
4642 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
4643 bcopy (fontname, fontp->name, strlen (fontname) + 1);
4645 if (lf.lfPitchAndFamily == FIXED_PITCH)
4647 /* Fixed width font. */
4648 fontp->average_width = fontp->space_width = FONT_WIDTH (font);
4650 else
4652 wchar_t space = 32;
4653 XCharStruct* pcm;
4654 pcm = w32_per_char_metric (font, &space, ANSI_FONT);
4655 if (pcm)
4656 fontp->space_width = pcm->width;
4657 else
4658 fontp->space_width = FONT_WIDTH (font);
4660 fontp->average_width = font->tm.tmAveCharWidth;
4663 charset = xlfd_charset_of_font (fontname);
4665 /* Cache the W32 codepage for a font. This makes w32_encode_char
4666 (called for every glyph during redisplay) much faster. */
4667 fontp->codepage = codepage;
4669 /* Work out the font's full name. */
4670 full_name = (char *)xmalloc (100);
4671 if (full_name && w32_to_x_font (&lf, full_name, 100, charset))
4672 fontp->full_name = full_name;
4673 else
4675 /* If all else fails - just use the name we used to load it. */
4676 xfree (full_name);
4677 fontp->full_name = fontp->name;
4680 fontp->size = FONT_WIDTH (font);
4681 fontp->height = FONT_HEIGHT (font);
4683 /* The slot `encoding' specifies how to map a character
4684 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
4685 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
4686 (0:0x20..0x7F, 1:0xA0..0xFF,
4687 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
4688 2:0xA020..0xFF7F). For the moment, we don't know which charset
4689 uses this font. So, we set information in fontp->encoding[1]
4690 which is never used by any charset. If mapping can't be
4691 decided, set FONT_ENCODING_NOT_DECIDED. */
4693 /* SJIS fonts need to be set to type 4, all others seem to work as
4694 type FONT_ENCODING_NOT_DECIDED. */
4695 encoding = strrchr (fontp->name, '-');
4696 if (encoding && strnicmp (encoding+1, "sjis", 4) == 0)
4697 fontp->encoding[1] = 4;
4698 else
4699 fontp->encoding[1] = FONT_ENCODING_NOT_DECIDED;
4701 /* The following three values are set to 0 under W32, which is
4702 what they get set to if XGetFontProperty fails under X. */
4703 fontp->baseline_offset = 0;
4704 fontp->relative_compose = 0;
4705 fontp->default_ascent = 0;
4707 /* Set global flag fonts_changed_p to non-zero if the font loaded
4708 has a character with a smaller width than any other character
4709 before, or if the font loaded has a smaller height than any
4710 other font loaded before. If this happens, it will make a
4711 glyph matrix reallocation necessary. */
4712 fonts_changed_p |= x_compute_min_glyph_bounds (f);
4713 UNBLOCK_INPUT;
4714 return fontp;
4718 /* Load font named FONTNAME of size SIZE for frame F, and return a
4719 pointer to the structure font_info while allocating it dynamically.
4720 If loading fails, return NULL. */
4721 struct font_info *
4722 w32_load_font (f,fontname,size)
4723 struct frame *f;
4724 char * fontname;
4725 int size;
4727 Lisp_Object bdf_fonts;
4728 struct font_info *retval = NULL;
4729 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4731 bdf_fonts = w32_list_bdf_fonts (build_string (fontname), 1);
4733 while (!retval && CONSP (bdf_fonts))
4735 char *bdf_name, *bdf_file;
4736 Lisp_Object bdf_pair;
4737 int i;
4739 bdf_name = SDATA (XCAR (bdf_fonts));
4740 bdf_pair = Fassoc (XCAR (bdf_fonts), Vw32_bdf_filename_alist);
4741 bdf_file = SDATA (XCDR (bdf_pair));
4743 // If the font is already loaded, do not load it again.
4744 for (i = 0; i < dpyinfo->n_fonts; i++)
4746 if ((dpyinfo->font_table[i].name
4747 && !strcmp (dpyinfo->font_table[i].name, bdf_name))
4748 || (dpyinfo->font_table[i].full_name
4749 && !strcmp (dpyinfo->font_table[i].full_name, bdf_name)))
4750 return dpyinfo->font_table + i;
4753 retval = w32_load_bdf_font (f, bdf_name, size, bdf_file);
4755 bdf_fonts = XCDR (bdf_fonts);
4758 if (retval)
4759 return retval;
4761 return w32_load_system_font(f, fontname, size);
4765 void
4766 w32_unload_font (dpyinfo, font)
4767 struct w32_display_info *dpyinfo;
4768 XFontStruct * font;
4770 if (font)
4772 if (font->per_char) xfree (font->per_char);
4773 if (font->bdf) w32_free_bdf_font (font->bdf);
4775 if (font->hfont) DeleteObject(font->hfont);
4776 xfree (font);
4780 /* The font conversion stuff between x and w32 */
4782 /* X font string is as follows (from faces.el)
4783 * (let ((- "[-?]")
4784 * (foundry "[^-]+")
4785 * (family "[^-]+")
4786 * (weight "\\(bold\\|demibold\\|medium\\)") ; 1
4787 * (weight\? "\\([^-]*\\)") ; 1
4788 * (slant "\\([ior]\\)") ; 2
4789 * (slant\? "\\([^-]?\\)") ; 2
4790 * (swidth "\\([^-]*\\)") ; 3
4791 * (adstyle "[^-]*") ; 4
4792 * (pixelsize "[0-9]+")
4793 * (pointsize "[0-9][0-9]+")
4794 * (resx "[0-9][0-9]+")
4795 * (resy "[0-9][0-9]+")
4796 * (spacing "[cmp?*]")
4797 * (avgwidth "[0-9]+")
4798 * (registry "[^-]+")
4799 * (encoding "[^-]+")
4803 static LONG
4804 x_to_w32_weight (lpw)
4805 char * lpw;
4807 if (!lpw) return (FW_DONTCARE);
4809 if (stricmp (lpw,"heavy") == 0) return FW_HEAVY;
4810 else if (stricmp (lpw,"extrabold") == 0) return FW_EXTRABOLD;
4811 else if (stricmp (lpw,"bold") == 0) return FW_BOLD;
4812 else if (stricmp (lpw,"demibold") == 0) return FW_SEMIBOLD;
4813 else if (stricmp (lpw,"semibold") == 0) return FW_SEMIBOLD;
4814 else if (stricmp (lpw,"medium") == 0) return FW_MEDIUM;
4815 else if (stricmp (lpw,"normal") == 0) return FW_NORMAL;
4816 else if (stricmp (lpw,"light") == 0) return FW_LIGHT;
4817 else if (stricmp (lpw,"extralight") == 0) return FW_EXTRALIGHT;
4818 else if (stricmp (lpw,"thin") == 0) return FW_THIN;
4819 else
4820 return FW_DONTCARE;
4824 static char *
4825 w32_to_x_weight (fnweight)
4826 int fnweight;
4828 if (fnweight >= FW_HEAVY) return "heavy";
4829 if (fnweight >= FW_EXTRABOLD) return "extrabold";
4830 if (fnweight >= FW_BOLD) return "bold";
4831 if (fnweight >= FW_SEMIBOLD) return "demibold";
4832 if (fnweight >= FW_MEDIUM) return "medium";
4833 if (fnweight >= FW_NORMAL) return "normal";
4834 if (fnweight >= FW_LIGHT) return "light";
4835 if (fnweight >= FW_EXTRALIGHT) return "extralight";
4836 if (fnweight >= FW_THIN) return "thin";
4837 else
4838 return "*";
4841 static LONG
4842 x_to_w32_charset (lpcs)
4843 char * lpcs;
4845 Lisp_Object this_entry, w32_charset;
4846 char *charset;
4847 int len = strlen (lpcs);
4849 /* Support "*-#nnn" format for unknown charsets. */
4850 if (strncmp (lpcs, "*-#", 3) == 0)
4851 return atoi (lpcs + 3);
4853 /* Handle wildcards by ignoring them; eg. treat "big5*-*" as "big5". */
4854 charset = alloca (len + 1);
4855 strcpy (charset, lpcs);
4856 lpcs = strchr (charset, '*');
4857 if (lpcs)
4858 *lpcs = 0;
4860 /* Look through w32-charset-info-alist for the character set.
4861 Format of each entry is
4862 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
4864 this_entry = Fassoc (build_string(charset), Vw32_charset_info_alist);
4866 if (NILP(this_entry))
4868 /* At startup, we want iso8859-1 fonts to come up properly. */
4869 if (stricmp(charset, "iso8859-1") == 0)
4870 return ANSI_CHARSET;
4871 else
4872 return DEFAULT_CHARSET;
4875 w32_charset = Fcar (Fcdr (this_entry));
4877 /* Translate Lisp symbol to number. */
4878 if (EQ (w32_charset, Qw32_charset_ansi))
4879 return ANSI_CHARSET;
4880 if (EQ (w32_charset, Qw32_charset_symbol))
4881 return SYMBOL_CHARSET;
4882 if (EQ (w32_charset, Qw32_charset_shiftjis))
4883 return SHIFTJIS_CHARSET;
4884 if (EQ (w32_charset, Qw32_charset_hangeul))
4885 return HANGEUL_CHARSET;
4886 if (EQ (w32_charset, Qw32_charset_chinesebig5))
4887 return CHINESEBIG5_CHARSET;
4888 if (EQ (w32_charset, Qw32_charset_gb2312))
4889 return GB2312_CHARSET;
4890 if (EQ (w32_charset, Qw32_charset_oem))
4891 return OEM_CHARSET;
4892 #ifdef JOHAB_CHARSET
4893 if (EQ (w32_charset, Qw32_charset_johab))
4894 return JOHAB_CHARSET;
4895 if (EQ (w32_charset, Qw32_charset_easteurope))
4896 return EASTEUROPE_CHARSET;
4897 if (EQ (w32_charset, Qw32_charset_turkish))
4898 return TURKISH_CHARSET;
4899 if (EQ (w32_charset, Qw32_charset_baltic))
4900 return BALTIC_CHARSET;
4901 if (EQ (w32_charset, Qw32_charset_russian))
4902 return RUSSIAN_CHARSET;
4903 if (EQ (w32_charset, Qw32_charset_arabic))
4904 return ARABIC_CHARSET;
4905 if (EQ (w32_charset, Qw32_charset_greek))
4906 return GREEK_CHARSET;
4907 if (EQ (w32_charset, Qw32_charset_hebrew))
4908 return HEBREW_CHARSET;
4909 if (EQ (w32_charset, Qw32_charset_vietnamese))
4910 return VIETNAMESE_CHARSET;
4911 if (EQ (w32_charset, Qw32_charset_thai))
4912 return THAI_CHARSET;
4913 if (EQ (w32_charset, Qw32_charset_mac))
4914 return MAC_CHARSET;
4915 #endif /* JOHAB_CHARSET */
4916 #ifdef UNICODE_CHARSET
4917 if (EQ (w32_charset, Qw32_charset_unicode))
4918 return UNICODE_CHARSET;
4919 #endif
4921 return DEFAULT_CHARSET;
4925 static char *
4926 w32_to_x_charset (fncharset)
4927 int fncharset;
4929 static char buf[32];
4930 Lisp_Object charset_type;
4932 switch (fncharset)
4934 case ANSI_CHARSET:
4935 /* Handle startup case of w32-charset-info-alist not
4936 being set up yet. */
4937 if (NILP(Vw32_charset_info_alist))
4938 return "iso8859-1";
4939 charset_type = Qw32_charset_ansi;
4940 break;
4941 case DEFAULT_CHARSET:
4942 charset_type = Qw32_charset_default;
4943 break;
4944 case SYMBOL_CHARSET:
4945 charset_type = Qw32_charset_symbol;
4946 break;
4947 case SHIFTJIS_CHARSET:
4948 charset_type = Qw32_charset_shiftjis;
4949 break;
4950 case HANGEUL_CHARSET:
4951 charset_type = Qw32_charset_hangeul;
4952 break;
4953 case GB2312_CHARSET:
4954 charset_type = Qw32_charset_gb2312;
4955 break;
4956 case CHINESEBIG5_CHARSET:
4957 charset_type = Qw32_charset_chinesebig5;
4958 break;
4959 case OEM_CHARSET:
4960 charset_type = Qw32_charset_oem;
4961 break;
4963 /* More recent versions of Windows (95 and NT4.0) define more
4964 character sets. */
4965 #ifdef EASTEUROPE_CHARSET
4966 case EASTEUROPE_CHARSET:
4967 charset_type = Qw32_charset_easteurope;
4968 break;
4969 case TURKISH_CHARSET:
4970 charset_type = Qw32_charset_turkish;
4971 break;
4972 case BALTIC_CHARSET:
4973 charset_type = Qw32_charset_baltic;
4974 break;
4975 case RUSSIAN_CHARSET:
4976 charset_type = Qw32_charset_russian;
4977 break;
4978 case ARABIC_CHARSET:
4979 charset_type = Qw32_charset_arabic;
4980 break;
4981 case GREEK_CHARSET:
4982 charset_type = Qw32_charset_greek;
4983 break;
4984 case HEBREW_CHARSET:
4985 charset_type = Qw32_charset_hebrew;
4986 break;
4987 case VIETNAMESE_CHARSET:
4988 charset_type = Qw32_charset_vietnamese;
4989 break;
4990 case THAI_CHARSET:
4991 charset_type = Qw32_charset_thai;
4992 break;
4993 case MAC_CHARSET:
4994 charset_type = Qw32_charset_mac;
4995 break;
4996 case JOHAB_CHARSET:
4997 charset_type = Qw32_charset_johab;
4998 break;
4999 #endif
5001 #ifdef UNICODE_CHARSET
5002 case UNICODE_CHARSET:
5003 charset_type = Qw32_charset_unicode;
5004 break;
5005 #endif
5006 default:
5007 /* Encode numerical value of unknown charset. */
5008 sprintf (buf, "*-#%u", fncharset);
5009 return buf;
5013 Lisp_Object rest;
5014 char * best_match = NULL;
5016 /* Look through w32-charset-info-alist for the character set.
5017 Prefer ISO codepages, and prefer lower numbers in the ISO
5018 range. Only return charsets for codepages which are installed.
5020 Format of each entry is
5021 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
5023 for (rest = Vw32_charset_info_alist; CONSP (rest); rest = XCDR (rest))
5025 char * x_charset;
5026 Lisp_Object w32_charset;
5027 Lisp_Object codepage;
5029 Lisp_Object this_entry = XCAR (rest);
5031 /* Skip invalid entries in alist. */
5032 if (!CONSP (this_entry) || !STRINGP (XCAR (this_entry))
5033 || !CONSP (XCDR (this_entry))
5034 || !SYMBOLP (XCAR (XCDR (this_entry))))
5035 continue;
5037 x_charset = SDATA (XCAR (this_entry));
5038 w32_charset = XCAR (XCDR (this_entry));
5039 codepage = XCDR (XCDR (this_entry));
5041 /* Look for Same charset and a valid codepage (or non-int
5042 which means ignore). */
5043 if (EQ (w32_charset, charset_type)
5044 && (!INTEGERP (codepage) || XINT (codepage) == CP_DEFAULT
5045 || IsValidCodePage (XINT (codepage))))
5047 /* If we don't have a match already, then this is the
5048 best. */
5049 if (!best_match)
5050 best_match = x_charset;
5051 /* If this is an ISO codepage, and the best so far isn't,
5052 then this is better. */
5053 else if (strnicmp (best_match, "iso", 3) != 0
5054 && strnicmp (x_charset, "iso", 3) == 0)
5055 best_match = x_charset;
5056 /* If both are ISO8859 codepages, choose the one with the
5057 lowest number in the encoding field. */
5058 else if (strnicmp (best_match, "iso8859-", 8) == 0
5059 && strnicmp (x_charset, "iso8859-", 8) == 0)
5061 int best_enc = atoi (best_match + 8);
5062 int this_enc = atoi (x_charset + 8);
5063 if (this_enc > 0 && this_enc < best_enc)
5064 best_match = x_charset;
5069 /* If no match, encode the numeric value. */
5070 if (!best_match)
5072 sprintf (buf, "*-#%u", fncharset);
5073 return buf;
5076 strncpy(buf, best_match, 31);
5077 buf[31] = '\0';
5078 return buf;
5083 /* Return all the X charsets that map to a font. */
5084 static Lisp_Object
5085 w32_to_all_x_charsets (fncharset)
5086 int fncharset;
5088 static char buf[32];
5089 Lisp_Object charset_type;
5090 Lisp_Object retval = Qnil;
5092 switch (fncharset)
5094 case ANSI_CHARSET:
5095 /* Handle startup case of w32-charset-info-alist not
5096 being set up yet. */
5097 if (NILP(Vw32_charset_info_alist))
5098 return Fcons (build_string ("iso8859-1"), Qnil);
5100 charset_type = Qw32_charset_ansi;
5101 break;
5102 case DEFAULT_CHARSET:
5103 charset_type = Qw32_charset_default;
5104 break;
5105 case SYMBOL_CHARSET:
5106 charset_type = Qw32_charset_symbol;
5107 break;
5108 case SHIFTJIS_CHARSET:
5109 charset_type = Qw32_charset_shiftjis;
5110 break;
5111 case HANGEUL_CHARSET:
5112 charset_type = Qw32_charset_hangeul;
5113 break;
5114 case GB2312_CHARSET:
5115 charset_type = Qw32_charset_gb2312;
5116 break;
5117 case CHINESEBIG5_CHARSET:
5118 charset_type = Qw32_charset_chinesebig5;
5119 break;
5120 case OEM_CHARSET:
5121 charset_type = Qw32_charset_oem;
5122 break;
5124 /* More recent versions of Windows (95 and NT4.0) define more
5125 character sets. */
5126 #ifdef EASTEUROPE_CHARSET
5127 case EASTEUROPE_CHARSET:
5128 charset_type = Qw32_charset_easteurope;
5129 break;
5130 case TURKISH_CHARSET:
5131 charset_type = Qw32_charset_turkish;
5132 break;
5133 case BALTIC_CHARSET:
5134 charset_type = Qw32_charset_baltic;
5135 break;
5136 case RUSSIAN_CHARSET:
5137 charset_type = Qw32_charset_russian;
5138 break;
5139 case ARABIC_CHARSET:
5140 charset_type = Qw32_charset_arabic;
5141 break;
5142 case GREEK_CHARSET:
5143 charset_type = Qw32_charset_greek;
5144 break;
5145 case HEBREW_CHARSET:
5146 charset_type = Qw32_charset_hebrew;
5147 break;
5148 case VIETNAMESE_CHARSET:
5149 charset_type = Qw32_charset_vietnamese;
5150 break;
5151 case THAI_CHARSET:
5152 charset_type = Qw32_charset_thai;
5153 break;
5154 case MAC_CHARSET:
5155 charset_type = Qw32_charset_mac;
5156 break;
5157 case JOHAB_CHARSET:
5158 charset_type = Qw32_charset_johab;
5159 break;
5160 #endif
5162 #ifdef UNICODE_CHARSET
5163 case UNICODE_CHARSET:
5164 charset_type = Qw32_charset_unicode;
5165 break;
5166 #endif
5167 default:
5168 /* Encode numerical value of unknown charset. */
5169 sprintf (buf, "*-#%u", fncharset);
5170 return Fcons (build_string (buf), Qnil);
5174 Lisp_Object rest;
5175 /* Look through w32-charset-info-alist for the character set.
5176 Only return charsets for codepages which are installed.
5178 Format of each entry in Vw32_charset_info_alist is
5179 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
5181 for (rest = Vw32_charset_info_alist; CONSP (rest); rest = XCDR (rest))
5183 Lisp_Object x_charset;
5184 Lisp_Object w32_charset;
5185 Lisp_Object codepage;
5187 Lisp_Object this_entry = XCAR (rest);
5189 /* Skip invalid entries in alist. */
5190 if (!CONSP (this_entry) || !STRINGP (XCAR (this_entry))
5191 || !CONSP (XCDR (this_entry))
5192 || !SYMBOLP (XCAR (XCDR (this_entry))))
5193 continue;
5195 x_charset = XCAR (this_entry);
5196 w32_charset = XCAR (XCDR (this_entry));
5197 codepage = XCDR (XCDR (this_entry));
5199 /* Look for Same charset and a valid codepage (or non-int
5200 which means ignore). */
5201 if (EQ (w32_charset, charset_type)
5202 && (!INTEGERP (codepage) || XINT (codepage) == CP_DEFAULT
5203 || IsValidCodePage (XINT (codepage))))
5205 retval = Fcons (x_charset, retval);
5209 /* If no match, encode the numeric value. */
5210 if (NILP (retval))
5212 sprintf (buf, "*-#%u", fncharset);
5213 return Fcons (build_string (buf), Qnil);
5216 return retval;
5220 /* Get the Windows codepage corresponding to the specified font. The
5221 charset info in the font name is used to look up
5222 w32-charset-to-codepage-alist. */
5224 w32_codepage_for_font (char *fontname)
5226 Lisp_Object codepage, entry;
5227 char *charset_str, *charset, *end;
5229 if (NILP (Vw32_charset_info_alist))
5230 return CP_DEFAULT;
5232 /* Extract charset part of font string. */
5233 charset = xlfd_charset_of_font (fontname);
5235 if (!charset)
5236 return CP_UNKNOWN;
5238 charset_str = (char *) alloca (strlen (charset) + 1);
5239 strcpy (charset_str, charset);
5241 #if 0
5242 /* Remove leading "*-". */
5243 if (strncmp ("*-", charset_str, 2) == 0)
5244 charset = charset_str + 2;
5245 else
5246 #endif
5247 charset = charset_str;
5249 /* Stop match at wildcard (including preceding '-'). */
5250 if (end = strchr (charset, '*'))
5252 if (end > charset && *(end-1) == '-')
5253 end--;
5254 *end = '\0';
5257 entry = Fassoc (build_string(charset), Vw32_charset_info_alist);
5258 if (NILP (entry))
5259 return CP_UNKNOWN;
5261 codepage = Fcdr (Fcdr (entry));
5263 if (NILP (codepage))
5264 return CP_8BIT;
5265 else if (XFASTINT (codepage) == XFASTINT (Qt))
5266 return CP_UNICODE;
5267 else if (INTEGERP (codepage))
5268 return XINT (codepage);
5269 else
5270 return CP_UNKNOWN;
5274 static BOOL
5275 w32_to_x_font (lplogfont, lpxstr, len, specific_charset)
5276 LOGFONT * lplogfont;
5277 char * lpxstr;
5278 int len;
5279 char * specific_charset;
5281 char* fonttype;
5282 char *fontname;
5283 char height_pixels[8];
5284 char height_dpi[8];
5285 char width_pixels[8];
5286 char *fontname_dash;
5287 int display_resy = (int) one_w32_display_info.resy;
5288 int display_resx = (int) one_w32_display_info.resx;
5289 int bufsz;
5290 struct coding_system coding;
5292 if (!lpxstr) abort ();
5294 if (!lplogfont)
5295 return FALSE;
5297 if (lplogfont->lfOutPrecision == OUT_STRING_PRECIS)
5298 fonttype = "raster";
5299 else if (lplogfont->lfOutPrecision == OUT_STROKE_PRECIS)
5300 fonttype = "outline";
5301 else
5302 fonttype = "unknown";
5304 setup_coding_system (Fcheck_coding_system (Vlocale_coding_system),
5305 &coding);
5306 coding.src_multibyte = 0;
5307 coding.dst_multibyte = 1;
5308 coding.mode |= CODING_MODE_LAST_BLOCK;
5309 /* We explicitely disable composition handling because selection
5310 data should not contain any composition sequence. */
5311 coding.composing = COMPOSITION_DISABLED;
5312 bufsz = decoding_buffer_size (&coding, LF_FACESIZE);
5314 fontname = alloca(sizeof(*fontname) * bufsz);
5315 decode_coding (&coding, lplogfont->lfFaceName, fontname,
5316 strlen(lplogfont->lfFaceName), bufsz - 1);
5317 *(fontname + coding.produced) = '\0';
5319 /* Replace dashes with underscores so the dashes are not
5320 misinterpreted. */
5321 fontname_dash = fontname;
5322 while (fontname_dash = strchr (fontname_dash, '-'))
5323 *fontname_dash = '_';
5325 if (lplogfont->lfHeight)
5327 sprintf (height_pixels, "%u", abs (lplogfont->lfHeight));
5328 sprintf (height_dpi, "%u",
5329 abs (lplogfont->lfHeight) * 720 / display_resy);
5331 else
5333 strcpy (height_pixels, "*");
5334 strcpy (height_dpi, "*");
5337 #if 0 /* Never put the width in the xfld. It fails on fonts with
5338 double-width characters. */
5339 if (lplogfont->lfWidth)
5340 sprintf (width_pixels, "%u", lplogfont->lfWidth * 10);
5341 else
5342 #endif
5343 strcpy (width_pixels, "*");
5345 _snprintf (lpxstr, len - 1,
5346 "-%s-%s-%s-%c-normal-normal-%s-%s-%d-%d-%c-%s-%s",
5347 fonttype, /* foundry */
5348 fontname, /* family */
5349 w32_to_x_weight (lplogfont->lfWeight), /* weight */
5350 lplogfont->lfItalic?'i':'r', /* slant */
5351 /* setwidth name */
5352 /* add style name */
5353 height_pixels, /* pixel size */
5354 height_dpi, /* point size */
5355 display_resx, /* resx */
5356 display_resy, /* resy */
5357 ((lplogfont->lfPitchAndFamily & 0x3) == VARIABLE_PITCH)
5358 ? 'p' : 'c', /* spacing */
5359 width_pixels, /* avg width */
5360 specific_charset ? specific_charset
5361 : w32_to_x_charset (lplogfont->lfCharSet)
5362 /* charset registry and encoding */
5365 lpxstr[len - 1] = 0; /* just to be sure */
5366 return (TRUE);
5369 static BOOL
5370 x_to_w32_font (lpxstr, lplogfont)
5371 char * lpxstr;
5372 LOGFONT * lplogfont;
5374 struct coding_system coding;
5376 if (!lplogfont) return (FALSE);
5378 memset (lplogfont, 0, sizeof (*lplogfont));
5380 /* Set default value for each field. */
5381 #if 1
5382 lplogfont->lfOutPrecision = OUT_DEFAULT_PRECIS;
5383 lplogfont->lfClipPrecision = CLIP_DEFAULT_PRECIS;
5384 lplogfont->lfQuality = DEFAULT_QUALITY;
5385 #else
5386 /* go for maximum quality */
5387 lplogfont->lfOutPrecision = OUT_STROKE_PRECIS;
5388 lplogfont->lfClipPrecision = CLIP_STROKE_PRECIS;
5389 lplogfont->lfQuality = PROOF_QUALITY;
5390 #endif
5392 lplogfont->lfCharSet = DEFAULT_CHARSET;
5393 lplogfont->lfWeight = FW_DONTCARE;
5394 lplogfont->lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
5396 if (!lpxstr)
5397 return FALSE;
5399 /* Provide a simple escape mechanism for specifying Windows font names
5400 * directly -- if font spec does not beginning with '-', assume this
5401 * format:
5402 * "<font name>[:height in pixels[:width in pixels[:weight]]]"
5405 if (*lpxstr == '-')
5407 int fields, tem;
5408 char name[50], weight[20], slant, pitch, pixels[10], height[10],
5409 width[10], resy[10], remainder[50];
5410 char * encoding;
5411 int dpi = (int) one_w32_display_info.resy;
5413 fields = sscanf (lpxstr,
5414 "-%*[^-]-%49[^-]-%19[^-]-%c-%*[^-]-%*[^-]-%9[^-]-%9[^-]-%*[^-]-%9[^-]-%c-%9[^-]-%49s",
5415 name, weight, &slant, pixels, height, resy, &pitch, width, remainder);
5416 if (fields == EOF)
5417 return (FALSE);
5419 /* In the general case when wildcards cover more than one field,
5420 we don't know which field is which, so don't fill any in.
5421 However, we need to cope with this particular form, which is
5422 generated by font_list_1 (invoked by try_font_list):
5423 "-raster-6x10-*-gb2312*-*"
5424 and make sure to correctly parse the charset field. */
5425 if (fields == 3)
5427 fields = sscanf (lpxstr,
5428 "-%*[^-]-%49[^-]-*-%49s",
5429 name, remainder);
5431 else if (fields < 9)
5433 fields = 0;
5434 remainder[0] = 0;
5437 if (fields > 0 && name[0] != '*')
5439 int bufsize;
5440 unsigned char *buf;
5442 setup_coding_system
5443 (Fcheck_coding_system (Vlocale_coding_system), &coding);
5444 coding.src_multibyte = 1;
5445 coding.dst_multibyte = 0;
5446 /* Need to set COMPOSITION_DISABLED, otherwise Emacs crashes in
5447 encode_coding_iso2022 trying to dereference a null pointer. */
5448 coding.composing = COMPOSITION_DISABLED;
5449 if (coding.type == coding_type_iso2022)
5450 coding.flags |= CODING_FLAG_ISO_SAFE;
5451 bufsize = encoding_buffer_size (&coding, strlen (name));
5452 buf = (unsigned char *) alloca (bufsize);
5453 coding.mode |= CODING_MODE_LAST_BLOCK;
5454 encode_coding (&coding, name, buf, strlen (name), bufsize);
5455 if (coding.produced >= LF_FACESIZE)
5456 coding.produced = LF_FACESIZE - 1;
5457 buf[coding.produced] = 0;
5458 strcpy (lplogfont->lfFaceName, buf);
5460 else
5462 lplogfont->lfFaceName[0] = '\0';
5465 fields--;
5467 lplogfont->lfWeight = x_to_w32_weight ((fields > 0 ? weight : ""));
5469 fields--;
5471 lplogfont->lfItalic = (fields > 0 && slant == 'i');
5473 fields--;
5475 if (fields > 0 && pixels[0] != '*')
5476 lplogfont->lfHeight = atoi (pixels);
5478 fields--;
5479 fields--;
5480 if (fields > 0 && resy[0] != '*')
5482 tem = atoi (resy);
5483 if (tem > 0) dpi = tem;
5486 if (fields > -1 && lplogfont->lfHeight == 0 && height[0] != '*')
5487 lplogfont->lfHeight = atoi (height) * dpi / 720;
5489 if (fields > 0)
5490 lplogfont->lfPitchAndFamily =
5491 (fields > 0 && pitch == 'p') ? VARIABLE_PITCH : FIXED_PITCH;
5493 fields--;
5495 if (fields > 0 && width[0] != '*')
5496 lplogfont->lfWidth = atoi (width) / 10;
5498 fields--;
5500 /* Strip the trailing '-' if present. (it shouldn't be, as it
5501 fails the test against xlfd-tight-regexp in fontset.el). */
5503 int len = strlen (remainder);
5504 if (len > 0 && remainder[len-1] == '-')
5505 remainder[len-1] = 0;
5507 encoding = remainder;
5508 #if 0
5509 if (strncmp (encoding, "*-", 2) == 0)
5510 encoding += 2;
5511 #endif
5512 lplogfont->lfCharSet = x_to_w32_charset (encoding);
5514 else
5516 int fields;
5517 char name[100], height[10], width[10], weight[20];
5519 fields = sscanf (lpxstr,
5520 "%99[^:]:%9[^:]:%9[^:]:%19s",
5521 name, height, width, weight);
5523 if (fields == EOF) return (FALSE);
5525 if (fields > 0)
5527 strncpy (lplogfont->lfFaceName,name, LF_FACESIZE);
5528 lplogfont->lfFaceName[LF_FACESIZE-1] = 0;
5530 else
5532 lplogfont->lfFaceName[0] = 0;
5535 fields--;
5537 if (fields > 0)
5538 lplogfont->lfHeight = atoi (height);
5540 fields--;
5542 if (fields > 0)
5543 lplogfont->lfWidth = atoi (width);
5545 fields--;
5547 lplogfont->lfWeight = x_to_w32_weight ((fields > 0 ? weight : ""));
5550 /* This makes TrueType fonts work better. */
5551 lplogfont->lfHeight = - abs (lplogfont->lfHeight);
5553 return (TRUE);
5556 /* Strip the pixel height and point height from the given xlfd, and
5557 return the pixel height. If no pixel height is specified, calculate
5558 one from the point height, or if that isn't defined either, return
5559 0 (which usually signifies a scalable font).
5561 static int
5562 xlfd_strip_height (char *fontname)
5564 int pixel_height, field_number;
5565 char *read_from, *write_to;
5567 xassert (fontname);
5569 pixel_height = field_number = 0;
5570 write_to = NULL;
5572 /* Look for height fields. */
5573 for (read_from = fontname; *read_from; read_from++)
5575 if (*read_from == '-')
5577 field_number++;
5578 if (field_number == 7) /* Pixel height. */
5580 read_from++;
5581 write_to = read_from;
5583 /* Find end of field. */
5584 for (;*read_from && *read_from != '-'; read_from++)
5587 /* Split the fontname at end of field. */
5588 if (*read_from)
5590 *read_from = '\0';
5591 read_from++;
5593 pixel_height = atoi (write_to);
5594 /* Blank out field. */
5595 if (read_from > write_to)
5597 *write_to = '-';
5598 write_to++;
5600 /* If the pixel height field is at the end (partial xlfd),
5601 return now. */
5602 else
5603 return pixel_height;
5605 /* If we got a pixel height, the point height can be
5606 ignored. Just blank it out and break now. */
5607 if (pixel_height)
5609 /* Find end of point size field. */
5610 for (; *read_from && *read_from != '-'; read_from++)
5613 if (*read_from)
5614 read_from++;
5616 /* Blank out the point size field. */
5617 if (read_from > write_to)
5619 *write_to = '-';
5620 write_to++;
5622 else
5623 return pixel_height;
5625 break;
5627 /* If the point height is already blank, break now. */
5628 if (*read_from == '-')
5630 read_from++;
5631 break;
5634 else if (field_number == 8)
5636 /* If we didn't get a pixel height, try to get the point
5637 height and convert that. */
5638 int point_size;
5639 char *point_size_start = read_from++;
5641 /* Find end of field. */
5642 for (; *read_from && *read_from != '-'; read_from++)
5645 if (*read_from)
5647 *read_from = '\0';
5648 read_from++;
5651 point_size = atoi (point_size_start);
5653 /* Convert to pixel height. */
5654 pixel_height = point_size
5655 * one_w32_display_info.height_in / 720;
5657 /* Blank out this field and break. */
5658 *write_to = '-';
5659 write_to++;
5660 break;
5665 /* Shift the rest of the font spec into place. */
5666 if (write_to && read_from > write_to)
5668 for (; *read_from; read_from++, write_to++)
5669 *write_to = *read_from;
5670 *write_to = '\0';
5673 return pixel_height;
5676 /* Assume parameter 1 is fully qualified, no wildcards. */
5677 static BOOL
5678 w32_font_match (fontname, pattern)
5679 char * fontname;
5680 char * pattern;
5682 char *ptr;
5683 char *font_name_copy;
5684 char *regex = alloca (strlen (pattern) * 2 + 3);
5686 font_name_copy = alloca (strlen (fontname) + 1);
5687 strcpy (font_name_copy, fontname);
5689 ptr = regex;
5690 *ptr++ = '^';
5692 /* Turn pattern into a regexp and do a regexp match. */
5693 for (; *pattern; pattern++)
5695 if (*pattern == '?')
5696 *ptr++ = '.';
5697 else if (*pattern == '*')
5699 *ptr++ = '.';
5700 *ptr++ = '*';
5702 else
5703 *ptr++ = *pattern;
5705 *ptr = '$';
5706 *(ptr + 1) = '\0';
5708 /* Strip out font heights and compare them seperately, since
5709 rounding error can cause mismatches. This also allows a
5710 comparison between a font that declares only a pixel height and a
5711 pattern that declares the point height.
5714 int font_height, pattern_height;
5716 font_height = xlfd_strip_height (font_name_copy);
5717 pattern_height = xlfd_strip_height (regex);
5719 /* Compare now, and don't bother doing expensive regexp matching
5720 if the heights differ. */
5721 if (font_height && pattern_height && (font_height != pattern_height))
5722 return FALSE;
5725 return (fast_string_match_ignore_case (build_string (regex),
5726 build_string(font_name_copy)) >= 0);
5729 /* Callback functions, and a structure holding info they need, for
5730 listing system fonts on W32. We need one set of functions to do the
5731 job properly, but these don't work on NT 3.51 and earlier, so we
5732 have a second set which don't handle character sets properly to
5733 fall back on.
5735 In both cases, there are two passes made. The first pass gets one
5736 font from each family, the second pass lists all the fonts from
5737 each family. */
5739 typedef struct enumfont_t
5741 HDC hdc;
5742 int numFonts;
5743 LOGFONT logfont;
5744 XFontStruct *size_ref;
5745 Lisp_Object pattern;
5746 Lisp_Object list;
5747 } enumfont_t;
5750 static void
5751 enum_font_maybe_add_to_list (enumfont_t *, LOGFONT *, char *, Lisp_Object);
5754 static int CALLBACK
5755 enum_font_cb2 (lplf, lptm, FontType, lpef)
5756 ENUMLOGFONT * lplf;
5757 NEWTEXTMETRIC * lptm;
5758 int FontType;
5759 enumfont_t * lpef;
5761 /* Ignore struck out and underlined versions of fonts. */
5762 if (lplf->elfLogFont.lfStrikeOut || lplf->elfLogFont.lfUnderline)
5763 return 1;
5765 /* Only return fonts with names starting with @ if they were
5766 explicitly specified, since Microsoft uses an initial @ to
5767 denote fonts for vertical writing, without providing a more
5768 convenient way of identifying them. */
5769 if (lplf->elfLogFont.lfFaceName[0] == '@'
5770 && lpef->logfont.lfFaceName[0] != '@')
5771 return 1;
5773 /* Check that the character set matches if it was specified */
5774 if (lpef->logfont.lfCharSet != DEFAULT_CHARSET &&
5775 lplf->elfLogFont.lfCharSet != lpef->logfont.lfCharSet)
5776 return 1;
5778 if (FontType == RASTER_FONTTYPE)
5780 /* DBCS raster fonts have problems displaying, so skip them. */
5781 int charset = lplf->elfLogFont.lfCharSet;
5782 if (charset == SHIFTJIS_CHARSET
5783 || charset == HANGEUL_CHARSET
5784 || charset == CHINESEBIG5_CHARSET
5785 || charset == GB2312_CHARSET
5786 #ifdef JOHAB_CHARSET
5787 || charset == JOHAB_CHARSET
5788 #endif
5790 return 1;
5794 char buf[100];
5795 Lisp_Object width = Qnil;
5796 Lisp_Object charset_list = Qnil;
5797 char *charset = NULL;
5799 /* Truetype fonts do not report their true metrics until loaded */
5800 if (FontType != RASTER_FONTTYPE)
5802 if (!NILP (lpef->pattern))
5804 /* Scalable fonts are as big as you want them to be. */
5805 lplf->elfLogFont.lfHeight = lpef->logfont.lfHeight;
5806 lplf->elfLogFont.lfWidth = lpef->logfont.lfWidth;
5807 width = make_number (lpef->logfont.lfWidth);
5809 else
5811 lplf->elfLogFont.lfHeight = 0;
5812 lplf->elfLogFont.lfWidth = 0;
5816 /* Make sure the height used here is the same as everywhere
5817 else (ie character height, not cell height). */
5818 if (lplf->elfLogFont.lfHeight > 0)
5820 /* lptm can be trusted for RASTER fonts, but not scalable ones. */
5821 if (FontType == RASTER_FONTTYPE)
5822 lplf->elfLogFont.lfHeight = lptm->tmInternalLeading - lptm->tmHeight;
5823 else
5824 lplf->elfLogFont.lfHeight = -lplf->elfLogFont.lfHeight;
5827 if (!NILP (lpef->pattern))
5829 charset = xlfd_charset_of_font (SDATA (lpef->pattern));
5831 /* We already checked charsets above, but DEFAULT_CHARSET
5832 slipped through. So only allow exact matches for DEFAULT_CHARSET. */
5833 if (charset
5834 && strncmp (charset, "*-*", 3) != 0
5835 && lpef->logfont.lfCharSet == DEFAULT_CHARSET
5836 && strcmp (charset, w32_to_x_charset (DEFAULT_CHARSET)) != 0)
5837 return 1;
5840 if (charset)
5841 charset_list = Fcons (build_string (charset), Qnil);
5842 else
5843 charset_list = w32_to_all_x_charsets (lplf->elfLogFont.lfCharSet);
5845 /* Loop through the charsets. */
5846 for ( ; CONSP (charset_list); charset_list = Fcdr (charset_list))
5848 Lisp_Object this_charset = Fcar (charset_list);
5849 charset = SDATA (this_charset);
5851 /* List bold and italic variations if w32-enable-synthesized-fonts
5852 is non-nil and this is a plain font. */
5853 if (w32_enable_synthesized_fonts
5854 && lplf->elfLogFont.lfWeight == FW_NORMAL
5855 && lplf->elfLogFont.lfItalic == FALSE)
5857 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5858 charset, width);
5859 /* bold. */
5860 lplf->elfLogFont.lfWeight = FW_BOLD;
5861 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5862 charset, width);
5863 /* bold italic. */
5864 lplf->elfLogFont.lfItalic = TRUE;
5865 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5866 charset, width);
5867 /* italic. */
5868 lplf->elfLogFont.lfWeight = FW_NORMAL;
5869 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5870 charset, width);
5872 else
5873 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5874 charset, width);
5878 return 1;
5881 static void
5882 enum_font_maybe_add_to_list (lpef, logfont, match_charset, width)
5883 enumfont_t * lpef;
5884 LOGFONT * logfont;
5885 char * match_charset;
5886 Lisp_Object width;
5888 char buf[100];
5890 if (!w32_to_x_font (logfont, buf, 100, match_charset))
5891 return;
5893 if (NILP (lpef->pattern)
5894 || w32_font_match (buf, SDATA (lpef->pattern)))
5896 /* Check if we already listed this font. This may happen if
5897 w32_enable_synthesized_fonts is non-nil, and there are real
5898 bold and italic versions of the font. */
5899 Lisp_Object font_name = build_string (buf);
5900 if (NILP (Fmember (font_name, lpef->list)))
5902 Lisp_Object entry = Fcons (font_name, width);
5903 lpef->list = Fcons (entry, lpef->list);
5904 lpef->numFonts++;
5910 static int CALLBACK
5911 enum_font_cb1 (lplf, lptm, FontType, lpef)
5912 ENUMLOGFONT * lplf;
5913 NEWTEXTMETRIC * lptm;
5914 int FontType;
5915 enumfont_t * lpef;
5917 return EnumFontFamilies (lpef->hdc,
5918 lplf->elfLogFont.lfFaceName,
5919 (FONTENUMPROC) enum_font_cb2,
5920 (LPARAM) lpef);
5924 static int CALLBACK
5925 enum_fontex_cb2 (lplf, lptm, font_type, lpef)
5926 ENUMLOGFONTEX * lplf;
5927 NEWTEXTMETRICEX * lptm;
5928 int font_type;
5929 enumfont_t * lpef;
5931 /* We are not interested in the extra info we get back from the 'Ex
5932 version - only the fact that we get character set variations
5933 enumerated seperately. */
5934 return enum_font_cb2 ((ENUMLOGFONT *) lplf, (NEWTEXTMETRIC *) lptm,
5935 font_type, lpef);
5938 static int CALLBACK
5939 enum_fontex_cb1 (lplf, lptm, font_type, lpef)
5940 ENUMLOGFONTEX * lplf;
5941 NEWTEXTMETRICEX * lptm;
5942 int font_type;
5943 enumfont_t * lpef;
5945 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
5946 FARPROC enum_font_families_ex
5947 = GetProcAddress ( gdi32, "EnumFontFamiliesExA");
5948 /* We don't really expect EnumFontFamiliesEx to disappear once we
5949 get here, so don't bother handling it gracefully. */
5950 if (enum_font_families_ex == NULL)
5951 error ("gdi32.dll has disappeared!");
5952 return enum_font_families_ex (lpef->hdc,
5953 &lplf->elfLogFont,
5954 (FONTENUMPROC) enum_fontex_cb2,
5955 (LPARAM) lpef, 0);
5958 /* Interface to fontset handler. (adapted from mw32font.c in Meadow
5959 and xterm.c in Emacs 20.3) */
5961 static Lisp_Object w32_list_bdf_fonts (Lisp_Object pattern, int max_names)
5963 char *fontname, *ptnstr;
5964 Lisp_Object list, tem, newlist = Qnil;
5965 int n_fonts = 0;
5967 list = Vw32_bdf_filename_alist;
5968 ptnstr = SDATA (pattern);
5970 for ( ; CONSP (list); list = XCDR (list))
5972 tem = XCAR (list);
5973 if (CONSP (tem))
5974 fontname = SDATA (XCAR (tem));
5975 else if (STRINGP (tem))
5976 fontname = SDATA (tem);
5977 else
5978 continue;
5980 if (w32_font_match (fontname, ptnstr))
5982 newlist = Fcons (XCAR (tem), newlist);
5983 n_fonts++;
5984 if (max_names >= 0 && n_fonts >= max_names)
5985 break;
5989 return newlist;
5993 /* Return a list of names of available fonts matching PATTERN on frame
5994 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
5995 to be listed. Frame F NULL means we have not yet created any
5996 frame, which means we can't get proper size info, as we don't have
5997 a device context to use for GetTextMetrics.
5998 MAXNAMES sets a limit on how many fonts to match. If MAXNAMES is
5999 negative, then all matching fonts are returned. */
6001 Lisp_Object
6002 w32_list_fonts (f, pattern, size, maxnames)
6003 struct frame *f;
6004 Lisp_Object pattern;
6005 int size;
6006 int maxnames;
6008 Lisp_Object patterns, key = Qnil, tem, tpat;
6009 Lisp_Object list = Qnil, newlist = Qnil, second_best = Qnil;
6010 struct w32_display_info *dpyinfo = &one_w32_display_info;
6011 int n_fonts = 0;
6013 patterns = Fassoc (pattern, Valternate_fontname_alist);
6014 if (NILP (patterns))
6015 patterns = Fcons (pattern, Qnil);
6017 for (; CONSP (patterns); patterns = XCDR (patterns))
6019 enumfont_t ef;
6020 int codepage;
6022 tpat = XCAR (patterns);
6024 if (!STRINGP (tpat))
6025 continue;
6027 /* Avoid expensive EnumFontFamilies functions if we are not
6028 going to be able to output one of these anyway. */
6029 codepage = w32_codepage_for_font (SDATA (tpat));
6030 if (codepage != CP_8BIT && codepage != CP_UNICODE
6031 && codepage != CP_DEFAULT && codepage != CP_UNKNOWN
6032 && !IsValidCodePage(codepage))
6033 continue;
6035 /* See if we cached the result for this particular query.
6036 The cache is an alist of the form:
6037 ((PATTERN (FONTNAME . WIDTH) ...) ...)
6039 if (tem = XCDR (dpyinfo->name_list_element),
6040 !NILP (list = Fassoc (tpat, tem)))
6042 list = Fcdr_safe (list);
6043 /* We have a cached list. Don't have to get the list again. */
6044 goto label_cached;
6047 BLOCK_INPUT;
6048 /* At first, put PATTERN in the cache. */
6049 ef.pattern = tpat;
6050 ef.list = Qnil;
6051 ef.numFonts = 0;
6053 /* Use EnumFontFamiliesEx where it is available, as it knows
6054 about character sets. Fall back to EnumFontFamilies for
6055 older versions of NT that don't support the 'Ex function. */
6056 x_to_w32_font (SDATA (tpat), &ef.logfont);
6058 LOGFONT font_match_pattern;
6059 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
6060 FARPROC enum_font_families_ex
6061 = GetProcAddress ( gdi32, "EnumFontFamiliesExA");
6063 /* We do our own pattern matching so we can handle wildcards. */
6064 font_match_pattern.lfFaceName[0] = 0;
6065 font_match_pattern.lfPitchAndFamily = 0;
6066 /* We can use the charset, because if it is a wildcard it will
6067 be DEFAULT_CHARSET anyway. */
6068 font_match_pattern.lfCharSet = ef.logfont.lfCharSet;
6070 ef.hdc = GetDC (dpyinfo->root_window);
6072 if (enum_font_families_ex)
6073 enum_font_families_ex (ef.hdc,
6074 &font_match_pattern,
6075 (FONTENUMPROC) enum_fontex_cb1,
6076 (LPARAM) &ef, 0);
6077 else
6078 EnumFontFamilies (ef.hdc, NULL, (FONTENUMPROC) enum_font_cb1,
6079 (LPARAM)&ef);
6081 ReleaseDC (dpyinfo->root_window, ef.hdc);
6084 UNBLOCK_INPUT;
6085 list = ef.list;
6087 /* Make a list of the fonts we got back.
6088 Store that in the font cache for the display. */
6089 XSETCDR (dpyinfo->name_list_element,
6090 Fcons (Fcons (tpat, list),
6091 XCDR (dpyinfo->name_list_element)));
6093 label_cached:
6094 if (NILP (list)) continue; /* Try the remaining alternatives. */
6096 newlist = second_best = Qnil;
6098 /* Make a list of the fonts that have the right width. */
6099 for (; CONSP (list); list = XCDR (list))
6101 int found_size;
6102 tem = XCAR (list);
6104 if (!CONSP (tem))
6105 continue;
6106 if (NILP (XCAR (tem)))
6107 continue;
6108 if (!size)
6110 newlist = Fcons (XCAR (tem), newlist);
6111 n_fonts++;
6112 if (maxnames >= 0 && n_fonts >= maxnames)
6113 break;
6114 else
6115 continue;
6117 if (!INTEGERP (XCDR (tem)))
6119 /* Since we don't yet know the size of the font, we must
6120 load it and try GetTextMetrics. */
6121 W32FontStruct thisinfo;
6122 LOGFONT lf;
6123 HDC hdc;
6124 HANDLE oldobj;
6126 if (!x_to_w32_font (SDATA (XCAR (tem)), &lf))
6127 continue;
6129 BLOCK_INPUT;
6130 thisinfo.bdf = NULL;
6131 thisinfo.hfont = CreateFontIndirect (&lf);
6132 if (thisinfo.hfont == NULL)
6133 continue;
6135 hdc = GetDC (dpyinfo->root_window);
6136 oldobj = SelectObject (hdc, thisinfo.hfont);
6137 if (GetTextMetrics (hdc, &thisinfo.tm))
6138 XSETCDR (tem, make_number (FONT_WIDTH (&thisinfo)));
6139 else
6140 XSETCDR (tem, make_number (0));
6141 SelectObject (hdc, oldobj);
6142 ReleaseDC (dpyinfo->root_window, hdc);
6143 DeleteObject(thisinfo.hfont);
6144 UNBLOCK_INPUT;
6146 found_size = XINT (XCDR (tem));
6147 if (found_size == size)
6149 newlist = Fcons (XCAR (tem), newlist);
6150 n_fonts++;
6151 if (maxnames >= 0 && n_fonts >= maxnames)
6152 break;
6154 /* keep track of the closest matching size in case
6155 no exact match is found. */
6156 else if (found_size > 0)
6158 if (NILP (second_best))
6159 second_best = tem;
6161 else if (found_size < size)
6163 if (XINT (XCDR (second_best)) > size
6164 || XINT (XCDR (second_best)) < found_size)
6165 second_best = tem;
6167 else
6169 if (XINT (XCDR (second_best)) > size
6170 && XINT (XCDR (second_best)) >
6171 found_size)
6172 second_best = tem;
6177 if (!NILP (newlist))
6178 break;
6179 else if (!NILP (second_best))
6181 newlist = Fcons (XCAR (second_best), Qnil);
6182 break;
6186 /* Include any bdf fonts. */
6187 if (n_fonts < maxnames || maxnames < 0)
6189 Lisp_Object combined[2];
6190 combined[0] = w32_list_bdf_fonts (pattern, maxnames - n_fonts);
6191 combined[1] = newlist;
6192 newlist = Fnconc(2, combined);
6195 return newlist;
6199 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
6200 struct font_info *
6201 w32_get_font_info (f, font_idx)
6202 FRAME_PTR f;
6203 int font_idx;
6205 return (FRAME_W32_FONT_TABLE (f) + font_idx);
6209 struct font_info*
6210 w32_query_font (struct frame *f, char *fontname)
6212 int i;
6213 struct font_info *pfi;
6215 pfi = FRAME_W32_FONT_TABLE (f);
6217 for (i = 0; i < one_w32_display_info.n_fonts ;i++, pfi++)
6219 if (stricmp(pfi->name, fontname) == 0) return pfi;
6222 return NULL;
6225 /* Find a CCL program for a font specified by FONTP, and set the member
6226 `encoder' of the structure. */
6228 void
6229 w32_find_ccl_program (fontp)
6230 struct font_info *fontp;
6232 Lisp_Object list, elt;
6234 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
6236 elt = XCAR (list);
6237 if (CONSP (elt)
6238 && STRINGP (XCAR (elt))
6239 && (fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
6240 >= 0))
6241 break;
6243 if (! NILP (list))
6245 struct ccl_program *ccl
6246 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
6248 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
6249 xfree (ccl);
6250 else
6251 fontp->font_encoder = ccl;
6255 /* directory-files from dired.c. */
6256 Lisp_Object Fdirectory_files P_((Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object));
6259 /* Find BDF files in a specified directory. (use GCPRO when calling,
6260 as this calls lisp to get a directory listing). */
6261 static Lisp_Object
6262 w32_find_bdf_fonts_in_dir (Lisp_Object directory)
6264 Lisp_Object filelist, list = Qnil;
6265 char fontname[100];
6267 if (!STRINGP(directory))
6268 return Qnil;
6270 filelist = Fdirectory_files (directory, Qt,
6271 build_string (".*\\.[bB][dD][fF]"), Qt);
6273 for ( ; CONSP(filelist); filelist = XCDR (filelist))
6275 Lisp_Object filename = XCAR (filelist);
6276 if (w32_BDF_to_x_font (SDATA (filename), fontname, 100))
6277 store_in_alist (&list, build_string (fontname), filename);
6279 return list;
6282 DEFUN ("w32-find-bdf-fonts", Fw32_find_bdf_fonts, Sw32_find_bdf_fonts,
6283 1, 1, 0,
6284 doc: /* Return a list of BDF fonts in DIRECTORY.
6285 The list is suitable for appending to `w32-bdf-filename-alist'.
6286 Fonts which do not contain an xlfd description will not be included
6287 in the list. DIRECTORY may be a list of directories. */)
6288 (directory)
6289 Lisp_Object directory;
6291 Lisp_Object list = Qnil;
6292 struct gcpro gcpro1, gcpro2;
6294 if (!CONSP (directory))
6295 return w32_find_bdf_fonts_in_dir (directory);
6297 for ( ; CONSP (directory); directory = XCDR (directory))
6299 Lisp_Object pair[2];
6300 pair[0] = list;
6301 pair[1] = Qnil;
6302 GCPRO2 (directory, list);
6303 pair[1] = w32_find_bdf_fonts_in_dir( XCAR (directory) );
6304 list = Fnconc( 2, pair );
6305 UNGCPRO;
6307 return list;
6311 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
6312 doc: /* Internal function called by `color-defined-p', which see. */)
6313 (color, frame)
6314 Lisp_Object color, frame;
6316 XColor foo;
6317 FRAME_PTR f = check_x_frame (frame);
6319 CHECK_STRING (color);
6321 if (w32_defined_color (f, SDATA (color), &foo, 0))
6322 return Qt;
6323 else
6324 return Qnil;
6327 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
6328 doc: /* Internal function called by `color-values', which see. */)
6329 (color, frame)
6330 Lisp_Object color, frame;
6332 XColor foo;
6333 FRAME_PTR f = check_x_frame (frame);
6335 CHECK_STRING (color);
6337 if (w32_defined_color (f, SDATA (color), &foo, 0))
6338 return list3 (make_number ((GetRValue (foo.pixel) << 8)
6339 | GetRValue (foo.pixel)),
6340 make_number ((GetGValue (foo.pixel) << 8)
6341 | GetGValue (foo.pixel)),
6342 make_number ((GetBValue (foo.pixel) << 8)
6343 | GetBValue (foo.pixel)));
6344 else
6345 return Qnil;
6348 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
6349 doc: /* Internal function called by `display-color-p', which see. */)
6350 (display)
6351 Lisp_Object display;
6353 struct w32_display_info *dpyinfo = check_x_display_info (display);
6355 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 2)
6356 return Qnil;
6358 return Qt;
6361 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p,
6362 Sx_display_grayscale_p, 0, 1, 0,
6363 doc: /* Return t if DISPLAY supports shades of gray.
6364 Note that color displays do support shades of gray.
6365 The optional argument DISPLAY specifies which display to ask about.
6366 DISPLAY should be either a frame or a display name (a string).
6367 If omitted or nil, that stands for the selected frame's display. */)
6368 (display)
6369 Lisp_Object display;
6371 struct w32_display_info *dpyinfo = check_x_display_info (display);
6373 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 1)
6374 return Qnil;
6376 return Qt;
6379 DEFUN ("x-display-pixel-width", Fx_display_pixel_width,
6380 Sx_display_pixel_width, 0, 1, 0,
6381 doc: /* Returns the width in pixels of DISPLAY.
6382 The optional argument DISPLAY specifies which display to ask about.
6383 DISPLAY should be either a frame or a display name (a string).
6384 If omitted or nil, that stands for the selected frame's display. */)
6385 (display)
6386 Lisp_Object display;
6388 struct w32_display_info *dpyinfo = check_x_display_info (display);
6390 return make_number (dpyinfo->width);
6393 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
6394 Sx_display_pixel_height, 0, 1, 0,
6395 doc: /* Returns the height in pixels of DISPLAY.
6396 The optional argument DISPLAY specifies which display to ask about.
6397 DISPLAY should be either a frame or a display name (a string).
6398 If omitted or nil, that stands for the selected frame's display. */)
6399 (display)
6400 Lisp_Object display;
6402 struct w32_display_info *dpyinfo = check_x_display_info (display);
6404 return make_number (dpyinfo->height);
6407 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
6408 0, 1, 0,
6409 doc: /* Returns the number of bitplanes of DISPLAY.
6410 The optional argument DISPLAY specifies which display to ask about.
6411 DISPLAY should be either a frame or a display name (a string).
6412 If omitted or nil, that stands for the selected frame's display. */)
6413 (display)
6414 Lisp_Object display;
6416 struct w32_display_info *dpyinfo = check_x_display_info (display);
6418 return make_number (dpyinfo->n_planes * dpyinfo->n_cbits);
6421 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
6422 0, 1, 0,
6423 doc: /* Returns the number of color cells of DISPLAY.
6424 The optional argument DISPLAY specifies which display to ask about.
6425 DISPLAY should be either a frame or a display name (a string).
6426 If omitted or nil, that stands for the selected frame's display. */)
6427 (display)
6428 Lisp_Object display;
6430 struct w32_display_info *dpyinfo = check_x_display_info (display);
6431 HDC hdc;
6432 int cap;
6434 hdc = GetDC (dpyinfo->root_window);
6435 if (dpyinfo->has_palette)
6436 cap = GetDeviceCaps (hdc, SIZEPALETTE);
6437 else
6438 cap = GetDeviceCaps (hdc, NUMCOLORS);
6440 /* We force 24+ bit depths to 24-bit, both to prevent an overflow
6441 and because probably is more meaningful on Windows anyway */
6442 if (cap < 0)
6443 cap = 1 << min(dpyinfo->n_planes * dpyinfo->n_cbits, 24);
6445 ReleaseDC (dpyinfo->root_window, hdc);
6447 return make_number (cap);
6450 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
6451 Sx_server_max_request_size,
6452 0, 1, 0,
6453 doc: /* Returns the maximum request size of the server of DISPLAY.
6454 The optional argument DISPLAY specifies which display to ask about.
6455 DISPLAY should be either a frame or a display name (a string).
6456 If omitted or nil, that stands for the selected frame's display. */)
6457 (display)
6458 Lisp_Object display;
6460 struct w32_display_info *dpyinfo = check_x_display_info (display);
6462 return make_number (1);
6465 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
6466 doc: /* Returns the "vendor ID" string of the W32 system (Microsoft).
6467 The optional argument DISPLAY specifies which display to ask about.
6468 DISPLAY should be either a frame or a display name (a string).
6469 If omitted or nil, that stands for the selected frame's display. */)
6470 (display)
6471 Lisp_Object display;
6473 return build_string ("Microsoft Corp.");
6476 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
6477 doc: /* Returns the version numbers of the server of DISPLAY.
6478 The value is a list of three integers: the major and minor
6479 version numbers of the X Protocol in use, and the distributor-specific release
6480 number. See also the function `x-server-vendor'.
6482 The optional argument DISPLAY specifies which display to ask about.
6483 DISPLAY should be either a frame or a display name (a string).
6484 If omitted or nil, that stands for the selected frame's display. */)
6485 (display)
6486 Lisp_Object display;
6488 return Fcons (make_number (w32_major_version),
6489 Fcons (make_number (w32_minor_version),
6490 Fcons (make_number (w32_build_number), Qnil)));
6493 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
6494 doc: /* Returns the number of screens on the server of DISPLAY.
6495 The optional argument DISPLAY specifies which display to ask about.
6496 DISPLAY should be either a frame or a display name (a string).
6497 If omitted or nil, that stands for the selected frame's display. */)
6498 (display)
6499 Lisp_Object display;
6501 return make_number (1);
6504 DEFUN ("x-display-mm-height", Fx_display_mm_height,
6505 Sx_display_mm_height, 0, 1, 0,
6506 doc: /* Returns the height in millimeters of DISPLAY.
6507 The optional argument DISPLAY specifies which display to ask about.
6508 DISPLAY should be either a frame or a display name (a string).
6509 If omitted or nil, that stands for the selected frame's display. */)
6510 (display)
6511 Lisp_Object display;
6513 struct w32_display_info *dpyinfo = check_x_display_info (display);
6514 HDC hdc;
6515 int cap;
6517 hdc = GetDC (dpyinfo->root_window);
6519 cap = GetDeviceCaps (hdc, VERTSIZE);
6521 ReleaseDC (dpyinfo->root_window, hdc);
6523 return make_number (cap);
6526 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
6527 doc: /* Returns the width in millimeters of DISPLAY.
6528 The optional argument DISPLAY specifies which display to ask about.
6529 DISPLAY should be either a frame or a display name (a string).
6530 If omitted or nil, that stands for the selected frame's display. */)
6531 (display)
6532 Lisp_Object display;
6534 struct w32_display_info *dpyinfo = check_x_display_info (display);
6536 HDC hdc;
6537 int cap;
6539 hdc = GetDC (dpyinfo->root_window);
6541 cap = GetDeviceCaps (hdc, HORZSIZE);
6543 ReleaseDC (dpyinfo->root_window, hdc);
6545 return make_number (cap);
6548 DEFUN ("x-display-backing-store", Fx_display_backing_store,
6549 Sx_display_backing_store, 0, 1, 0,
6550 doc: /* Returns an indication of whether DISPLAY does backing store.
6551 The value may be `always', `when-mapped', or `not-useful'.
6552 The optional argument DISPLAY specifies which display to ask about.
6553 DISPLAY should be either a frame or a display name (a string).
6554 If omitted or nil, that stands for the selected frame's display. */)
6555 (display)
6556 Lisp_Object display;
6558 return intern ("not-useful");
6561 DEFUN ("x-display-visual-class", Fx_display_visual_class,
6562 Sx_display_visual_class, 0, 1, 0,
6563 doc: /* Returns the visual class of DISPLAY.
6564 The value is one of the symbols `static-gray', `gray-scale',
6565 `static-color', `pseudo-color', `true-color', or `direct-color'.
6567 The optional argument DISPLAY specifies which display to ask about.
6568 DISPLAY should be either a frame or a display name (a string).
6569 If omitted or nil, that stands for the selected frame's display. */)
6570 (display)
6571 Lisp_Object display;
6573 struct w32_display_info *dpyinfo = check_x_display_info (display);
6574 Lisp_Object result = Qnil;
6576 if (dpyinfo->has_palette)
6577 result = intern ("pseudo-color");
6578 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 1)
6579 result = intern ("static-grey");
6580 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 4)
6581 result = intern ("static-color");
6582 else if (dpyinfo->n_planes * dpyinfo->n_cbits > 8)
6583 result = intern ("true-color");
6585 return result;
6588 DEFUN ("x-display-save-under", Fx_display_save_under,
6589 Sx_display_save_under, 0, 1, 0,
6590 doc: /* Returns t if DISPLAY supports the save-under feature.
6591 The optional argument DISPLAY specifies which display to ask about.
6592 DISPLAY should be either a frame or a display name (a string).
6593 If omitted or nil, that stands for the selected frame's display. */)
6594 (display)
6595 Lisp_Object display;
6597 return Qnil;
6601 x_pixel_width (f)
6602 register struct frame *f;
6604 return FRAME_PIXEL_WIDTH (f);
6608 x_pixel_height (f)
6609 register struct frame *f;
6611 return FRAME_PIXEL_HEIGHT (f);
6615 x_char_width (f)
6616 register struct frame *f;
6618 return FRAME_COLUMN_WIDTH (f);
6622 x_char_height (f)
6623 register struct frame *f;
6625 return FRAME_LINE_HEIGHT (f);
6629 x_screen_planes (f)
6630 register struct frame *f;
6632 return FRAME_W32_DISPLAY_INFO (f)->n_planes;
6635 /* Return the display structure for the display named NAME.
6636 Open a new connection if necessary. */
6638 struct w32_display_info *
6639 x_display_info_for_name (name)
6640 Lisp_Object name;
6642 Lisp_Object names;
6643 struct w32_display_info *dpyinfo;
6645 CHECK_STRING (name);
6647 for (dpyinfo = &one_w32_display_info, names = w32_display_name_list;
6648 dpyinfo;
6649 dpyinfo = dpyinfo->next, names = XCDR (names))
6651 Lisp_Object tem;
6652 tem = Fstring_equal (XCAR (XCAR (names)), name);
6653 if (!NILP (tem))
6654 return dpyinfo;
6657 /* Use this general default value to start with. */
6658 Vx_resource_name = Vinvocation_name;
6660 validate_x_resource_name ();
6662 dpyinfo = w32_term_init (name, (unsigned char *)0,
6663 (char *) SDATA (Vx_resource_name));
6665 if (dpyinfo == 0)
6666 error ("Cannot connect to server %s", SDATA (name));
6668 w32_in_use = 1;
6669 XSETFASTINT (Vwindow_system_version, 3);
6671 return dpyinfo;
6674 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
6675 1, 3, 0, doc: /* Open a connection to a server.
6676 DISPLAY is the name of the display to connect to.
6677 Optional second arg XRM-STRING is a string of resources in xrdb format.
6678 If the optional third arg MUST-SUCCEED is non-nil,
6679 terminate Emacs if we can't open the connection. */)
6680 (display, xrm_string, must_succeed)
6681 Lisp_Object display, xrm_string, must_succeed;
6683 unsigned char *xrm_option;
6684 struct w32_display_info *dpyinfo;
6686 /* If initialization has already been done, return now to avoid
6687 overwriting critical parts of one_w32_display_info. */
6688 if (w32_in_use)
6689 return Qnil;
6691 CHECK_STRING (display);
6692 if (! NILP (xrm_string))
6693 CHECK_STRING (xrm_string);
6695 if (! EQ (Vwindow_system, intern ("w32")))
6696 error ("Not using Microsoft Windows");
6698 /* Allow color mapping to be defined externally; first look in user's
6699 HOME directory, then in Emacs etc dir for a file called rgb.txt. */
6701 Lisp_Object color_file;
6702 struct gcpro gcpro1;
6704 color_file = build_string("~/rgb.txt");
6706 GCPRO1 (color_file);
6708 if (NILP (Ffile_readable_p (color_file)))
6709 color_file =
6710 Fexpand_file_name (build_string ("rgb.txt"),
6711 Fsymbol_value (intern ("data-directory")));
6713 Vw32_color_map = Fw32_load_color_file (color_file);
6715 UNGCPRO;
6717 if (NILP (Vw32_color_map))
6718 Vw32_color_map = Fw32_default_color_map ();
6720 /* Merge in system logical colors. */
6721 add_system_logical_colors_to_map (&Vw32_color_map);
6723 if (! NILP (xrm_string))
6724 xrm_option = (unsigned char *) SDATA (xrm_string);
6725 else
6726 xrm_option = (unsigned char *) 0;
6728 /* Use this general default value to start with. */
6729 /* First remove .exe suffix from invocation-name - it looks ugly. */
6731 char basename[ MAX_PATH ], *str;
6733 strcpy (basename, SDATA (Vinvocation_name));
6734 str = strrchr (basename, '.');
6735 if (str) *str = 0;
6736 Vinvocation_name = build_string (basename);
6738 Vx_resource_name = Vinvocation_name;
6740 validate_x_resource_name ();
6742 /* This is what opens the connection and sets x_current_display.
6743 This also initializes many symbols, such as those used for input. */
6744 dpyinfo = w32_term_init (display, xrm_option,
6745 (char *) SDATA (Vx_resource_name));
6747 if (dpyinfo == 0)
6749 if (!NILP (must_succeed))
6750 fatal ("Cannot connect to server %s.\n",
6751 SDATA (display));
6752 else
6753 error ("Cannot connect to server %s", SDATA (display));
6756 w32_in_use = 1;
6758 XSETFASTINT (Vwindow_system_version, 3);
6759 return Qnil;
6762 DEFUN ("x-close-connection", Fx_close_connection,
6763 Sx_close_connection, 1, 1, 0,
6764 doc: /* Close the connection to DISPLAY's server.
6765 For DISPLAY, specify either a frame or a display name (a string).
6766 If DISPLAY is nil, that stands for the selected frame's display. */)
6767 (display)
6768 Lisp_Object display;
6770 struct w32_display_info *dpyinfo = check_x_display_info (display);
6771 int i;
6773 if (dpyinfo->reference_count > 0)
6774 error ("Display still has frames on it");
6776 BLOCK_INPUT;
6777 /* Free the fonts in the font table. */
6778 for (i = 0; i < dpyinfo->n_fonts; i++)
6779 if (dpyinfo->font_table[i].name)
6781 if (dpyinfo->font_table[i].name != dpyinfo->font_table[i].full_name)
6782 xfree (dpyinfo->font_table[i].full_name);
6783 xfree (dpyinfo->font_table[i].name);
6784 w32_unload_font (dpyinfo, dpyinfo->font_table[i].font);
6786 x_destroy_all_bitmaps (dpyinfo);
6788 x_delete_display (dpyinfo);
6789 UNBLOCK_INPUT;
6791 return Qnil;
6794 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
6795 doc: /* Return the list of display names that Emacs has connections to. */)
6798 Lisp_Object tail, result;
6800 result = Qnil;
6801 for (tail = w32_display_name_list; ! NILP (tail); tail = XCDR (tail))
6802 result = Fcons (XCAR (XCAR (tail)), result);
6804 return result;
6807 DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
6808 doc: /* This is a noop on W32 systems. */)
6809 (on, display)
6810 Lisp_Object display, on;
6812 return Qnil;
6817 /***********************************************************************
6818 Window properties
6819 ***********************************************************************/
6821 DEFUN ("x-change-window-property", Fx_change_window_property,
6822 Sx_change_window_property, 2, 6, 0,
6823 doc: /* Change window property PROP to VALUE on the X window of FRAME.
6824 VALUE may be a string or a list of conses, numbers and/or strings.
6825 If an element in the list is a string, it is converted to
6826 an Atom and the value of the Atom is used. If an element is a cons,
6827 it is converted to a 32 bit number where the car is the 16 top bits and the
6828 cdr is the lower 16 bits.
6829 FRAME nil or omitted means use the selected frame.
6830 If TYPE is given and non-nil, it is the name of the type of VALUE.
6831 If TYPE is not given or nil, the type is STRING.
6832 FORMAT gives the size in bits of each element if VALUE is a list.
6833 It must be one of 8, 16 or 32.
6834 If VALUE is a string or FORMAT is nil or not given, FORMAT defaults to 8.
6835 If OUTER_P is non-nil, the property is changed for the outer X window of
6836 FRAME. Default is to change on the edit X window.
6838 Value is VALUE. */)
6839 (prop, value, frame, type, format, outer_p)
6840 Lisp_Object prop, value, frame, type, format, outer_p;
6842 #if 0 /* TODO : port window properties to W32 */
6843 struct frame *f = check_x_frame (frame);
6844 Atom prop_atom;
6846 CHECK_STRING (prop);
6847 CHECK_STRING (value);
6849 BLOCK_INPUT;
6850 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
6851 XChangeProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
6852 prop_atom, XA_STRING, 8, PropModeReplace,
6853 SDATA (value), SCHARS (value));
6855 /* Make sure the property is set when we return. */
6856 XFlush (FRAME_W32_DISPLAY (f));
6857 UNBLOCK_INPUT;
6859 #endif /* TODO */
6861 return value;
6865 DEFUN ("x-delete-window-property", Fx_delete_window_property,
6866 Sx_delete_window_property, 1, 2, 0,
6867 doc: /* Remove window property PROP from X window of FRAME.
6868 FRAME nil or omitted means use the selected frame. Value is PROP. */)
6869 (prop, frame)
6870 Lisp_Object prop, frame;
6872 #if 0 /* TODO : port window properties to W32 */
6874 struct frame *f = check_x_frame (frame);
6875 Atom prop_atom;
6877 CHECK_STRING (prop);
6878 BLOCK_INPUT;
6879 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
6880 XDeleteProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), prop_atom);
6882 /* Make sure the property is removed when we return. */
6883 XFlush (FRAME_W32_DISPLAY (f));
6884 UNBLOCK_INPUT;
6885 #endif /* TODO */
6887 return prop;
6891 DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
6892 1, 2, 0,
6893 doc: /* Value is the value of window property PROP on FRAME.
6894 If FRAME is nil or omitted, use the selected frame. Value is nil
6895 if FRAME hasn't a property with name PROP or if PROP has no string
6896 value. */)
6897 (prop, frame)
6898 Lisp_Object prop, frame;
6900 #if 0 /* TODO : port window properties to W32 */
6902 struct frame *f = check_x_frame (frame);
6903 Atom prop_atom;
6904 int rc;
6905 Lisp_Object prop_value = Qnil;
6906 char *tmp_data = NULL;
6907 Atom actual_type;
6908 int actual_format;
6909 unsigned long actual_size, bytes_remaining;
6911 CHECK_STRING (prop);
6912 BLOCK_INPUT;
6913 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
6914 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
6915 prop_atom, 0, 0, False, XA_STRING,
6916 &actual_type, &actual_format, &actual_size,
6917 &bytes_remaining, (unsigned char **) &tmp_data);
6918 if (rc == Success)
6920 int size = bytes_remaining;
6922 XFree (tmp_data);
6923 tmp_data = NULL;
6925 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
6926 prop_atom, 0, bytes_remaining,
6927 False, XA_STRING,
6928 &actual_type, &actual_format,
6929 &actual_size, &bytes_remaining,
6930 (unsigned char **) &tmp_data);
6931 if (rc == Success)
6932 prop_value = make_string (tmp_data, size);
6934 XFree (tmp_data);
6937 UNBLOCK_INPUT;
6939 return prop_value;
6941 #endif /* TODO */
6942 return Qnil;
6947 /***********************************************************************
6948 Busy cursor
6949 ***********************************************************************/
6951 /* If non-null, an asynchronous timer that, when it expires, displays
6952 an hourglass cursor on all frames. */
6954 static struct atimer *hourglass_atimer;
6956 /* Non-zero means an hourglass cursor is currently shown. */
6958 static int hourglass_shown_p;
6960 /* Number of seconds to wait before displaying an hourglass cursor. */
6962 static Lisp_Object Vhourglass_delay;
6964 /* Default number of seconds to wait before displaying an hourglass
6965 cursor. */
6967 #define DEFAULT_HOURGLASS_DELAY 1
6969 /* Function prototypes. */
6971 static void show_hourglass P_ ((struct atimer *));
6972 static void hide_hourglass P_ ((void));
6975 /* Cancel a currently active hourglass timer, and start a new one. */
6977 void
6978 start_hourglass ()
6980 #if 0 /* TODO: cursor shape changes. */
6981 EMACS_TIME delay;
6982 int secs, usecs = 0;
6984 cancel_hourglass ();
6986 if (INTEGERP (Vhourglass_delay)
6987 && XINT (Vhourglass_delay) > 0)
6988 secs = XFASTINT (Vhourglass_delay);
6989 else if (FLOATP (Vhourglass_delay)
6990 && XFLOAT_DATA (Vhourglass_delay) > 0)
6992 Lisp_Object tem;
6993 tem = Ftruncate (Vhourglass_delay, Qnil);
6994 secs = XFASTINT (tem);
6995 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
6997 else
6998 secs = DEFAULT_HOURGLASS_DELAY;
7000 EMACS_SET_SECS_USECS (delay, secs, usecs);
7001 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
7002 show_hourglass, NULL);
7003 #endif
7007 /* Cancel the hourglass cursor timer if active, hide an hourglass
7008 cursor if shown. */
7010 void
7011 cancel_hourglass ()
7013 if (hourglass_atimer)
7015 cancel_atimer (hourglass_atimer);
7016 hourglass_atimer = NULL;
7019 if (hourglass_shown_p)
7020 hide_hourglass ();
7024 /* Timer function of hourglass_atimer. TIMER is equal to
7025 hourglass_atimer.
7027 Display an hourglass cursor on all frames by mapping the frames'
7028 hourglass_window. Set the hourglass_p flag in the frames'
7029 output_data.x structure to indicate that an hourglass cursor is
7030 shown on the frames. */
7032 static void
7033 show_hourglass (timer)
7034 struct atimer *timer;
7036 #if 0 /* TODO: cursor shape changes. */
7037 /* The timer implementation will cancel this timer automatically
7038 after this function has run. Set hourglass_atimer to null
7039 so that we know the timer doesn't have to be canceled. */
7040 hourglass_atimer = NULL;
7042 if (!hourglass_shown_p)
7044 Lisp_Object rest, frame;
7046 BLOCK_INPUT;
7048 FOR_EACH_FRAME (rest, frame)
7049 if (FRAME_W32_P (XFRAME (frame)))
7051 struct frame *f = XFRAME (frame);
7053 f->output_data.w32->hourglass_p = 1;
7055 if (!f->output_data.w32->hourglass_window)
7057 unsigned long mask = CWCursor;
7058 XSetWindowAttributes attrs;
7060 attrs.cursor = f->output_data.w32->hourglass_cursor;
7062 f->output_data.w32->hourglass_window
7063 = XCreateWindow (FRAME_X_DISPLAY (f),
7064 FRAME_OUTER_WINDOW (f),
7065 0, 0, 32000, 32000, 0, 0,
7066 InputOnly,
7067 CopyFromParent,
7068 mask, &attrs);
7071 XMapRaised (FRAME_X_DISPLAY (f),
7072 f->output_data.w32->hourglass_window);
7073 XFlush (FRAME_X_DISPLAY (f));
7076 hourglass_shown_p = 1;
7077 UNBLOCK_INPUT;
7079 #endif
7083 /* Hide the hourglass cursor on all frames, if it is currently shown. */
7085 static void
7086 hide_hourglass ()
7088 #if 0 /* TODO: cursor shape changes. */
7089 if (hourglass_shown_p)
7091 Lisp_Object rest, frame;
7093 BLOCK_INPUT;
7094 FOR_EACH_FRAME (rest, frame)
7096 struct frame *f = XFRAME (frame);
7098 if (FRAME_W32_P (f)
7099 /* Watch out for newly created frames. */
7100 && f->output_data.x->hourglass_window)
7102 XUnmapWindow (FRAME_X_DISPLAY (f),
7103 f->output_data.x->hourglass_window);
7104 /* Sync here because XTread_socket looks at the
7105 hourglass_p flag that is reset to zero below. */
7106 XSync (FRAME_X_DISPLAY (f), False);
7107 f->output_data.x->hourglass_p = 0;
7111 hourglass_shown_p = 0;
7112 UNBLOCK_INPUT;
7114 #endif
7119 /***********************************************************************
7120 Tool tips
7121 ***********************************************************************/
7123 static Lisp_Object x_create_tip_frame P_ ((struct w32_display_info *,
7124 Lisp_Object, Lisp_Object));
7125 static void compute_tip_xy P_ ((struct frame *, Lisp_Object, Lisp_Object,
7126 Lisp_Object, int, int, int *, int *));
7128 /* The frame of a currently visible tooltip. */
7130 Lisp_Object tip_frame;
7132 /* If non-nil, a timer started that hides the last tooltip when it
7133 fires. */
7135 Lisp_Object tip_timer;
7136 Window tip_window;
7138 /* If non-nil, a vector of 3 elements containing the last args
7139 with which x-show-tip was called. See there. */
7141 Lisp_Object last_show_tip_args;
7143 /* Maximum size for tooltips; a cons (COLUMNS . ROWS). */
7145 Lisp_Object Vx_max_tooltip_size;
7148 static Lisp_Object
7149 unwind_create_tip_frame (frame)
7150 Lisp_Object frame;
7152 Lisp_Object deleted;
7154 deleted = unwind_create_frame (frame);
7155 if (EQ (deleted, Qt))
7157 tip_window = NULL;
7158 tip_frame = Qnil;
7161 return deleted;
7165 /* Create a frame for a tooltip on the display described by DPYINFO.
7166 PARMS is a list of frame parameters. TEXT is the string to
7167 display in the tip frame. Value is the frame.
7169 Note that functions called here, esp. x_default_parameter can
7170 signal errors, for instance when a specified color name is
7171 undefined. We have to make sure that we're in a consistent state
7172 when this happens. */
7174 static Lisp_Object
7175 x_create_tip_frame (dpyinfo, parms, text)
7176 struct w32_display_info *dpyinfo;
7177 Lisp_Object parms, text;
7179 struct frame *f;
7180 Lisp_Object frame, tem;
7181 Lisp_Object name;
7182 long window_prompting = 0;
7183 int width, height;
7184 int count = SPECPDL_INDEX ();
7185 struct gcpro gcpro1, gcpro2, gcpro3;
7186 struct kboard *kb;
7187 int face_change_count_before = face_change_count;
7188 Lisp_Object buffer;
7189 struct buffer *old_buffer;
7191 check_w32 ();
7193 /* Use this general default value to start with until we know if
7194 this frame has a specified name. */
7195 Vx_resource_name = Vinvocation_name;
7197 #ifdef MULTI_KBOARD
7198 kb = dpyinfo->kboard;
7199 #else
7200 kb = &the_only_kboard;
7201 #endif
7203 /* Get the name of the frame to use for resource lookup. */
7204 name = w32_get_arg (parms, Qname, "name", "Name", RES_TYPE_STRING);
7205 if (!STRINGP (name)
7206 && !EQ (name, Qunbound)
7207 && !NILP (name))
7208 error ("Invalid frame name--not a string or nil");
7209 Vx_resource_name = name;
7211 frame = Qnil;
7212 GCPRO3 (parms, name, frame);
7213 /* Make a frame without minibuffer nor mode-line. */
7214 f = make_frame (0);
7215 f->wants_modeline = 0;
7216 XSETFRAME (frame, f);
7218 buffer = Fget_buffer_create (build_string (" *tip*"));
7219 Fset_window_buffer (FRAME_ROOT_WINDOW (f), buffer, Qnil);
7220 old_buffer = current_buffer;
7221 set_buffer_internal_1 (XBUFFER (buffer));
7222 current_buffer->truncate_lines = Qnil;
7223 specbind (Qinhibit_read_only, Qt);
7224 specbind (Qinhibit_modification_hooks, Qt);
7225 Ferase_buffer ();
7226 Finsert (1, &text);
7227 set_buffer_internal_1 (old_buffer);
7229 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
7230 record_unwind_protect (unwind_create_tip_frame, frame);
7232 /* By setting the output method, we're essentially saying that
7233 the frame is live, as per FRAME_LIVE_P. If we get a signal
7234 from this point on, x_destroy_window might screw up reference
7235 counts etc. */
7236 f->output_method = output_w32;
7237 f->output_data.w32 =
7238 (struct w32_output *) xmalloc (sizeof (struct w32_output));
7239 bzero (f->output_data.w32, sizeof (struct w32_output));
7241 FRAME_FONTSET (f) = -1;
7242 f->icon_name = Qnil;
7244 #if 0 /* GLYPH_DEBUG TODO: image support. */
7245 image_cache_refcount = FRAME_X_IMAGE_CACHE (f)->refcount;
7246 dpyinfo_refcount = dpyinfo->reference_count;
7247 #endif /* GLYPH_DEBUG */
7248 #ifdef MULTI_KBOARD
7249 FRAME_KBOARD (f) = kb;
7250 #endif
7251 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
7252 f->output_data.w32->explicit_parent = 0;
7254 /* Set the name; the functions to which we pass f expect the name to
7255 be set. */
7256 if (EQ (name, Qunbound) || NILP (name))
7258 f->name = build_string (dpyinfo->w32_id_name);
7259 f->explicit_name = 0;
7261 else
7263 f->name = name;
7264 f->explicit_name = 1;
7265 /* use the frame's title when getting resources for this frame. */
7266 specbind (Qx_resource_name, name);
7269 /* Extract the window parameters from the supplied values
7270 that are needed to determine window geometry. */
7272 Lisp_Object font;
7274 font = w32_get_arg (parms, Qfont, "font", "Font", RES_TYPE_STRING);
7276 BLOCK_INPUT;
7277 /* First, try whatever font the caller has specified. */
7278 if (STRINGP (font))
7280 tem = Fquery_fontset (font, Qnil);
7281 if (STRINGP (tem))
7282 font = x_new_fontset (f, SDATA (tem));
7283 else
7284 font = x_new_font (f, SDATA (font));
7287 /* Try out a font which we hope has bold and italic variations. */
7288 if (!STRINGP (font))
7289 font = x_new_font (f, "-*-Courier New-normal-r-*-*-*-100-*-*-c-*-iso8859-1");
7290 if (! STRINGP (font))
7291 font = x_new_font (f, "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1");
7292 /* If those didn't work, look for something which will at least work. */
7293 if (! STRINGP (font))
7294 font = x_new_font (f, "-*-Fixedsys-normal-r-*-*-12-*-*-*-c-*-iso8859-1");
7295 UNBLOCK_INPUT;
7296 if (! STRINGP (font))
7297 font = build_string ("Fixedsys");
7299 x_default_parameter (f, parms, Qfont, font,
7300 "font", "Font", RES_TYPE_STRING);
7303 x_default_parameter (f, parms, Qborder_width, make_number (2),
7304 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
7305 /* This defaults to 2 in order to match xterm. We recognize either
7306 internalBorderWidth or internalBorder (which is what xterm calls
7307 it). */
7308 if (NILP (Fassq (Qinternal_border_width, parms)))
7310 Lisp_Object value;
7312 value = w32_get_arg (parms, Qinternal_border_width,
7313 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
7314 if (! EQ (value, Qunbound))
7315 parms = Fcons (Fcons (Qinternal_border_width, value),
7316 parms);
7318 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
7319 "internalBorderWidth", "internalBorderWidth",
7320 RES_TYPE_NUMBER);
7322 /* Also do the stuff which must be set before the window exists. */
7323 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
7324 "foreground", "Foreground", RES_TYPE_STRING);
7325 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
7326 "background", "Background", RES_TYPE_STRING);
7327 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
7328 "pointerColor", "Foreground", RES_TYPE_STRING);
7329 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
7330 "cursorColor", "Foreground", RES_TYPE_STRING);
7331 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
7332 "borderColor", "BorderColor", RES_TYPE_STRING);
7334 /* Init faces before x_default_parameter is called for scroll-bar
7335 parameters because that function calls x_set_scroll_bar_width,
7336 which calls change_frame_size, which calls Fset_window_buffer,
7337 which runs hooks, which call Fvertical_motion. At the end, we
7338 end up in init_iterator with a null face cache, which should not
7339 happen. */
7340 init_frame_faces (f);
7342 f->output_data.w32->dwStyle = WS_BORDER | WS_POPUP | WS_DISABLED;
7343 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
7345 window_prompting = x_figure_window_size (f, parms, 0);
7347 /* No fringes on tip frame. */
7348 f->fringe_cols = 0;
7349 f->left_fringe_width = 0;
7350 f->right_fringe_width = 0;
7352 BLOCK_INPUT;
7353 my_create_tip_window (f);
7354 UNBLOCK_INPUT;
7356 x_make_gc (f);
7358 x_default_parameter (f, parms, Qauto_raise, Qnil,
7359 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
7360 x_default_parameter (f, parms, Qauto_lower, Qnil,
7361 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
7362 x_default_parameter (f, parms, Qcursor_type, Qbox,
7363 "cursorType", "CursorType", RES_TYPE_SYMBOL);
7365 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
7366 Change will not be effected unless different from the current
7367 FRAME_LINES (f). */
7368 width = FRAME_COLS (f);
7369 height = FRAME_LINES (f);
7370 FRAME_LINES (f) = 0;
7371 SET_FRAME_COLS (f, 0);
7372 change_frame_size (f, height, width, 1, 0, 0);
7374 /* Add `tooltip' frame parameter's default value. */
7375 if (NILP (Fframe_parameter (frame, intern ("tooltip"))))
7376 Fmodify_frame_parameters (frame, Fcons (Fcons (intern ("tooltip"), Qt),
7377 Qnil));
7379 /* Set up faces after all frame parameters are known. This call
7380 also merges in face attributes specified for new frames.
7382 Frame parameters may be changed if .Xdefaults contains
7383 specifications for the default font. For example, if there is an
7384 `Emacs.default.attributeBackground: pink', the `background-color'
7385 attribute of the frame get's set, which let's the internal border
7386 of the tooltip frame appear in pink. Prevent this. */
7388 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
7390 /* Set tip_frame here, so that */
7391 tip_frame = frame;
7392 call1 (Qface_set_after_frame_default, frame);
7394 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
7395 Fmodify_frame_parameters (frame, Fcons (Fcons (Qbackground_color, bg),
7396 Qnil));
7399 f->no_split = 1;
7401 UNGCPRO;
7403 /* It is now ok to make the frame official even if we get an error
7404 below. And the frame needs to be on Vframe_list or making it
7405 visible won't work. */
7406 Vframe_list = Fcons (frame, Vframe_list);
7408 /* Now that the frame is official, it counts as a reference to
7409 its display. */
7410 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
7412 /* Setting attributes of faces of the tooltip frame from resources
7413 and similar will increment face_change_count, which leads to the
7414 clearing of all current matrices. Since this isn't necessary
7415 here, avoid it by resetting face_change_count to the value it
7416 had before we created the tip frame. */
7417 face_change_count = face_change_count_before;
7419 /* Discard the unwind_protect. */
7420 return unbind_to (count, frame);
7424 /* Compute where to display tip frame F. PARMS is the list of frame
7425 parameters for F. DX and DY are specified offsets from the current
7426 location of the mouse. WIDTH and HEIGHT are the width and height
7427 of the tooltip. Return coordinates relative to the root window of
7428 the display in *ROOT_X, and *ROOT_Y. */
7430 static void
7431 compute_tip_xy (f, parms, dx, dy, width, height, root_x, root_y)
7432 struct frame *f;
7433 Lisp_Object parms, dx, dy;
7434 int width, height;
7435 int *root_x, *root_y;
7437 Lisp_Object left, top;
7439 /* User-specified position? */
7440 left = Fcdr (Fassq (Qleft, parms));
7441 top = Fcdr (Fassq (Qtop, parms));
7443 /* Move the tooltip window where the mouse pointer is. Resize and
7444 show it. */
7445 if (!INTEGERP (left) || !INTEGERP (top))
7447 POINT pt;
7449 BLOCK_INPUT;
7450 GetCursorPos (&pt);
7451 *root_x = pt.x;
7452 *root_y = pt.y;
7453 UNBLOCK_INPUT;
7456 if (INTEGERP (top))
7457 *root_y = XINT (top);
7458 else if (*root_y + XINT (dy) <= 0)
7459 *root_y = 0; /* Can happen for negative dy */
7460 else if (*root_y + XINT (dy) + height <= FRAME_W32_DISPLAY_INFO (f)->height)
7461 /* It fits below the pointer */
7462 *root_y += XINT (dy);
7463 else if (height + XINT (dy) <= *root_y)
7464 /* It fits above the pointer. */
7465 *root_y -= height + XINT (dy);
7466 else
7467 /* Put it on the top. */
7468 *root_y = 0;
7470 if (INTEGERP (left))
7471 *root_x = XINT (left);
7472 else if (*root_x + XINT (dx) <= 0)
7473 *root_x = 0; /* Can happen for negative dx */
7474 else if (*root_x + XINT (dx) + width <= FRAME_W32_DISPLAY_INFO (f)->width)
7475 /* It fits to the right of the pointer. */
7476 *root_x += XINT (dx);
7477 else if (width + XINT (dx) <= *root_x)
7478 /* It fits to the left of the pointer. */
7479 *root_x -= width + XINT (dx);
7480 else
7481 /* Put it left justified on the screen -- it ought to fit that way. */
7482 *root_x = 0;
7486 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
7487 doc: /* Show STRING in a \"tooltip\" window on frame FRAME.
7488 A tooltip window is a small window displaying a string.
7490 FRAME nil or omitted means use the selected frame.
7492 PARMS is an optional list of frame parameters which can be
7493 used to change the tooltip's appearance.
7495 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
7496 means use the default timeout of 5 seconds.
7498 If the list of frame parameters PARMS contains a `left' parameter,
7499 the tooltip is displayed at that x-position. Otherwise it is
7500 displayed at the mouse position, with offset DX added (default is 5 if
7501 DX isn't specified). Likewise for the y-position; if a `top' frame
7502 parameter is specified, it determines the y-position of the tooltip
7503 window, otherwise it is displayed at the mouse position, with offset
7504 DY added (default is -10).
7506 A tooltip's maximum size is specified by `x-max-tooltip-size'.
7507 Text larger than the specified size is clipped. */)
7508 (string, frame, parms, timeout, dx, dy)
7509 Lisp_Object string, frame, parms, timeout, dx, dy;
7511 struct frame *f;
7512 struct window *w;
7513 int root_x, root_y;
7514 struct buffer *old_buffer;
7515 struct text_pos pos;
7516 int i, width, height;
7517 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7518 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7519 int count = SPECPDL_INDEX ();
7521 specbind (Qinhibit_redisplay, Qt);
7523 GCPRO4 (string, parms, frame, timeout);
7525 CHECK_STRING (string);
7526 f = check_x_frame (frame);
7527 if (NILP (timeout))
7528 timeout = make_number (5);
7529 else
7530 CHECK_NATNUM (timeout);
7532 if (NILP (dx))
7533 dx = make_number (5);
7534 else
7535 CHECK_NUMBER (dx);
7537 if (NILP (dy))
7538 dy = make_number (-10);
7539 else
7540 CHECK_NUMBER (dy);
7542 if (NILP (last_show_tip_args))
7543 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
7545 if (!NILP (tip_frame))
7547 Lisp_Object last_string = AREF (last_show_tip_args, 0);
7548 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
7549 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
7551 if (EQ (frame, last_frame)
7552 && !NILP (Fequal (last_string, string))
7553 && !NILP (Fequal (last_parms, parms)))
7555 struct frame *f = XFRAME (tip_frame);
7557 /* Only DX and DY have changed. */
7558 if (!NILP (tip_timer))
7560 Lisp_Object timer = tip_timer;
7561 tip_timer = Qnil;
7562 call1 (Qcancel_timer, timer);
7565 BLOCK_INPUT;
7566 compute_tip_xy (f, parms, dx, dy, FRAME_PIXEL_WIDTH (f),
7567 FRAME_PIXEL_HEIGHT (f), &root_x, &root_y);
7569 /* Put tooltip in topmost group and in position. */
7570 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
7571 root_x, root_y, 0, 0,
7572 SWP_NOSIZE | SWP_NOACTIVATE);
7574 /* Ensure tooltip is on top of other topmost windows (eg menus). */
7575 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
7576 0, 0, 0, 0,
7577 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
7579 UNBLOCK_INPUT;
7580 goto start_timer;
7584 /* Hide a previous tip, if any. */
7585 Fx_hide_tip ();
7587 ASET (last_show_tip_args, 0, string);
7588 ASET (last_show_tip_args, 1, frame);
7589 ASET (last_show_tip_args, 2, parms);
7591 /* Add default values to frame parameters. */
7592 if (NILP (Fassq (Qname, parms)))
7593 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
7594 if (NILP (Fassq (Qinternal_border_width, parms)))
7595 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
7596 if (NILP (Fassq (Qborder_width, parms)))
7597 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
7598 if (NILP (Fassq (Qborder_color, parms)))
7599 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
7600 if (NILP (Fassq (Qbackground_color, parms)))
7601 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
7602 parms);
7604 /* Block input until the tip has been fully drawn, to avoid crashes
7605 when drawing tips in menus. */
7606 BLOCK_INPUT;
7608 /* Create a frame for the tooltip, and record it in the global
7609 variable tip_frame. */
7610 frame = x_create_tip_frame (FRAME_W32_DISPLAY_INFO (f), parms, string);
7611 f = XFRAME (frame);
7613 /* Set up the frame's root window. */
7614 w = XWINDOW (FRAME_ROOT_WINDOW (f));
7615 w->left_col = w->top_line = make_number (0);
7617 if (CONSP (Vx_max_tooltip_size)
7618 && INTEGERP (XCAR (Vx_max_tooltip_size))
7619 && XINT (XCAR (Vx_max_tooltip_size)) > 0
7620 && INTEGERP (XCDR (Vx_max_tooltip_size))
7621 && XINT (XCDR (Vx_max_tooltip_size)) > 0)
7623 w->total_cols = XCAR (Vx_max_tooltip_size);
7624 w->total_lines = XCDR (Vx_max_tooltip_size);
7626 else
7628 w->total_cols = make_number (80);
7629 w->total_lines = make_number (40);
7632 FRAME_TOTAL_COLS (f) = XINT (w->total_cols);
7633 adjust_glyphs (f);
7634 w->pseudo_window_p = 1;
7636 /* Display the tooltip text in a temporary buffer. */
7637 old_buffer = current_buffer;
7638 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->buffer));
7639 current_buffer->truncate_lines = Qnil;
7640 clear_glyph_matrix (w->desired_matrix);
7641 clear_glyph_matrix (w->current_matrix);
7642 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
7643 try_window (FRAME_ROOT_WINDOW (f), pos, 0);
7645 /* Compute width and height of the tooltip. */
7646 width = height = 0;
7647 for (i = 0; i < w->desired_matrix->nrows; ++i)
7649 struct glyph_row *row = &w->desired_matrix->rows[i];
7650 struct glyph *last;
7651 int row_width;
7653 /* Stop at the first empty row at the end. */
7654 if (!row->enabled_p || !row->displays_text_p)
7655 break;
7657 /* Let the row go over the full width of the frame. */
7658 row->full_width_p = 1;
7660 #ifdef TODO /* Investigate why some fonts need more width than is
7661 calculated for some tooltips. */
7662 /* There's a glyph at the end of rows that is use to place
7663 the cursor there. Don't include the width of this glyph. */
7664 if (row->used[TEXT_AREA])
7666 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
7667 row_width = row->pixel_width - last->pixel_width;
7669 else
7670 #endif
7671 row_width = row->pixel_width;
7673 /* TODO: find why tips do not draw along baseline as instructed. */
7674 height += row->height;
7675 width = max (width, row_width);
7678 /* Add the frame's internal border to the width and height the X
7679 window should have. */
7680 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
7681 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
7683 /* Move the tooltip window where the mouse pointer is. Resize and
7684 show it. */
7685 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
7688 /* Adjust Window size to take border into account. */
7689 RECT rect;
7690 rect.left = rect.top = 0;
7691 rect.right = width;
7692 rect.bottom = height;
7693 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
7694 FRAME_EXTERNAL_MENU_BAR (f));
7696 /* Position and size tooltip, and put it in the topmost group.
7697 The add-on of 3 to the 5th argument is a kludge: without it,
7698 some fonts cause the last character of the tip to be truncated,
7699 for some obscure reason. */
7700 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
7701 root_x, root_y, rect.right - rect.left + 3,
7702 rect.bottom - rect.top, SWP_NOACTIVATE);
7704 /* Ensure tooltip is on top of other topmost windows (eg menus). */
7705 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
7706 0, 0, 0, 0,
7707 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
7709 /* Let redisplay know that we have made the frame visible already. */
7710 f->async_visible = 1;
7712 ShowWindow (FRAME_W32_WINDOW (f), SW_SHOWNOACTIVATE);
7715 /* Draw into the window. */
7716 w->must_be_updated_p = 1;
7717 update_single_window (w, 1);
7719 UNBLOCK_INPUT;
7721 /* Restore original current buffer. */
7722 set_buffer_internal_1 (old_buffer);
7723 windows_or_buffers_changed = old_windows_or_buffers_changed;
7725 start_timer:
7726 /* Let the tip disappear after timeout seconds. */
7727 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
7728 intern ("x-hide-tip"));
7730 UNGCPRO;
7731 return unbind_to (count, Qnil);
7735 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
7736 doc: /* Hide the current tooltip window, if there is any.
7737 Value is t if tooltip was open, nil otherwise. */)
7740 int count;
7741 Lisp_Object deleted, frame, timer;
7742 struct gcpro gcpro1, gcpro2;
7744 /* Return quickly if nothing to do. */
7745 if (NILP (tip_timer) && NILP (tip_frame))
7746 return Qnil;
7748 frame = tip_frame;
7749 timer = tip_timer;
7750 GCPRO2 (frame, timer);
7751 tip_frame = tip_timer = deleted = Qnil;
7753 count = SPECPDL_INDEX ();
7754 specbind (Qinhibit_redisplay, Qt);
7755 specbind (Qinhibit_quit, Qt);
7757 if (!NILP (timer))
7758 call1 (Qcancel_timer, timer);
7760 if (FRAMEP (frame))
7762 Fdelete_frame (frame, Qnil);
7763 deleted = Qt;
7766 UNGCPRO;
7767 return unbind_to (count, deleted);
7772 /***********************************************************************
7773 File selection dialog
7774 ***********************************************************************/
7775 extern Lisp_Object Qfile_name_history;
7777 /* Callback for altering the behaviour of the Open File dialog.
7778 Makes the Filename text field contain "Current Directory" and be
7779 read-only when "Directories" is selected in the filter. This
7780 allows us to work around the fact that the standard Open File
7781 dialog does not support directories. */
7782 UINT CALLBACK
7783 file_dialog_callback (hwnd, msg, wParam, lParam)
7784 HWND hwnd;
7785 UINT msg;
7786 WPARAM wParam;
7787 LPARAM lParam;
7789 if (msg == WM_NOTIFY)
7791 OFNOTIFY * notify = (OFNOTIFY *)lParam;
7792 /* Detect when the Filter dropdown is changed. */
7793 if (notify->hdr.code == CDN_TYPECHANGE
7794 || notify->hdr.code == CDN_INITDONE)
7796 HWND dialog = GetParent (hwnd);
7797 HWND edit_control = GetDlgItem (dialog, FILE_NAME_TEXT_FIELD);
7799 /* Directories is in index 2. */
7800 if (notify->lpOFN->nFilterIndex == 2)
7802 CommDlg_OpenSave_SetControlText (dialog, FILE_NAME_TEXT_FIELD,
7803 "Current Directory");
7804 EnableWindow (edit_control, FALSE);
7806 else
7808 /* Don't override default filename on init done. */
7809 if (notify->hdr.code == CDN_TYPECHANGE)
7810 CommDlg_OpenSave_SetControlText (dialog,
7811 FILE_NAME_TEXT_FIELD, "");
7812 EnableWindow (edit_control, TRUE);
7816 return 0;
7819 /* Since we compile with _WIN32_WINNT set to 0x0400 (for NT4 compatibility)
7820 we end up with the old file dialogs. Define a big enough struct for the
7821 new dialog to trick GetOpenFileName into giving us the new dialogs on
7822 Windows 2000 and XP. */
7823 typedef struct
7825 OPENFILENAME real_details;
7826 void * pReserved;
7827 DWORD dwReserved;
7828 DWORD FlagsEx;
7829 } NEWOPENFILENAME;
7832 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
7833 doc: /* Read file name, prompting with PROMPT in directory DIR.
7834 Use a file selection dialog.
7835 Select DEFAULT-FILENAME in the dialog's file selection box, if
7836 specified. Ensure that file exists if MUSTMATCH is non-nil.
7837 If ONLY-DIR-P is non-nil, the user can only select directories. */)
7838 (prompt, dir, default_filename, mustmatch, only_dir_p)
7839 Lisp_Object prompt, dir, default_filename, mustmatch, only_dir_p;
7841 struct frame *f = SELECTED_FRAME ();
7842 Lisp_Object file = Qnil;
7843 int count = SPECPDL_INDEX ();
7844 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
7845 char filename[MAX_PATH + 1];
7846 char init_dir[MAX_PATH + 1];
7847 int default_filter_index = 1; /* 1: All Files, 2: Directories only */
7849 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
7850 CHECK_STRING (prompt);
7851 CHECK_STRING (dir);
7853 /* Create the dialog with PROMPT as title, using DIR as initial
7854 directory and using "*" as pattern. */
7855 dir = Fexpand_file_name (dir, Qnil);
7856 strncpy (init_dir, SDATA (ENCODE_FILE (dir)), MAX_PATH);
7857 init_dir[MAX_PATH] = '\0';
7858 unixtodos_filename (init_dir);
7860 if (STRINGP (default_filename))
7862 char *file_name_only;
7863 char *full_path_name = SDATA (ENCODE_FILE (default_filename));
7865 unixtodos_filename (full_path_name);
7867 file_name_only = strrchr (full_path_name, '\\');
7868 if (!file_name_only)
7869 file_name_only = full_path_name;
7870 else
7871 file_name_only++;
7873 strncpy (filename, file_name_only, MAX_PATH);
7874 filename[MAX_PATH] = '\0';
7876 else
7877 filename[0] = '\0';
7880 NEWOPENFILENAME new_file_details;
7881 BOOL file_opened = FALSE;
7882 OPENFILENAME * file_details = &new_file_details.real_details;
7884 /* Prevent redisplay. */
7885 specbind (Qinhibit_redisplay, Qt);
7886 BLOCK_INPUT;
7888 bzero (&new_file_details, sizeof (new_file_details));
7889 /* Apparently NT4 crashes if you give it an unexpected size.
7890 I'm not sure about Windows 9x, so play it safe. */
7891 if (w32_major_version > 4 && w32_major_version < 95)
7892 file_details->lStructSize = sizeof (new_file_details);
7893 else
7894 file_details->lStructSize = sizeof (file_details);
7896 file_details->hwndOwner = FRAME_W32_WINDOW (f);
7897 /* Undocumented Bug in Common File Dialog:
7898 If a filter is not specified, shell links are not resolved. */
7899 file_details->lpstrFilter = "All Files (*.*)\0*.*\0Directories\0*|*\0\0";
7900 file_details->lpstrFile = filename;
7901 file_details->nMaxFile = sizeof (filename);
7902 file_details->lpstrInitialDir = init_dir;
7903 file_details->lpstrTitle = SDATA (prompt);
7905 if (! NILP (only_dir_p))
7906 default_filter_index = 2;
7908 file_details->nFilterIndex = default_filter_index;
7910 file_details->Flags = (OFN_HIDEREADONLY | OFN_NOCHANGEDIR
7911 | OFN_EXPLORER | OFN_ENABLEHOOK);
7912 if (!NILP (mustmatch))
7914 /* Require that the path to the parent directory exists. */
7915 file_details->Flags |= OFN_PATHMUSTEXIST;
7916 /* If we are looking for a file, require that it exists. */
7917 if (NILP (only_dir_p))
7918 file_details->Flags |= OFN_FILEMUSTEXIST;
7921 file_details->lpfnHook = (LPOFNHOOKPROC) file_dialog_callback;
7923 file_opened = GetOpenFileName (file_details);
7925 UNBLOCK_INPUT;
7927 if (file_opened)
7929 dostounix_filename (filename);
7931 if (file_details->nFilterIndex == 2)
7933 /* "Directories" selected - strip dummy file name. */
7934 char * last = strrchr (filename, '/');
7935 *last = '\0';
7938 file = DECODE_FILE(build_string (filename));
7940 /* User cancelled the dialog without making a selection. */
7941 else if (!CommDlgExtendedError ())
7942 file = Qnil;
7943 /* An error occurred, fallback on reading from the mini-buffer. */
7944 else
7945 file = Fcompleting_read (prompt, intern ("read-file-name-internal"),
7946 dir, mustmatch, dir, Qfile_name_history,
7947 default_filename, Qnil);
7949 file = unbind_to (count, file);
7952 UNGCPRO;
7954 /* Make "Cancel" equivalent to C-g. */
7955 if (NILP (file))
7956 Fsignal (Qquit, Qnil);
7958 return unbind_to (count, file);
7963 /***********************************************************************
7964 w32 specialized functions
7965 ***********************************************************************/
7967 DEFUN ("w32-select-font", Fw32_select_font, Sw32_select_font, 0, 2, 0,
7968 doc: /* Select a font for the named FRAME using the W32 font dialog.
7969 Returns an X-style font string corresponding to the selection.
7971 If FRAME is omitted or nil, it defaults to the selected frame.
7972 If INCLUDE-PROPORTIONAL is non-nil, include proportional fonts
7973 in the font selection dialog. */)
7974 (frame, include_proportional)
7975 Lisp_Object frame, include_proportional;
7977 FRAME_PTR f = check_x_frame (frame);
7978 CHOOSEFONT cf;
7979 LOGFONT lf;
7980 TEXTMETRIC tm;
7981 HDC hdc;
7982 HANDLE oldobj;
7983 char buf[100];
7985 bzero (&cf, sizeof (cf));
7986 bzero (&lf, sizeof (lf));
7988 cf.lStructSize = sizeof (cf);
7989 cf.hwndOwner = FRAME_W32_WINDOW (f);
7990 cf.Flags = CF_FORCEFONTEXIST | CF_SCREENFONTS | CF_NOVERTFONTS;
7992 /* Unless include_proportional is non-nil, limit the selection to
7993 monospaced fonts. */
7994 if (NILP (include_proportional))
7995 cf.Flags |= CF_FIXEDPITCHONLY;
7997 cf.lpLogFont = &lf;
7999 /* Initialize as much of the font details as we can from the current
8000 default font. */
8001 hdc = GetDC (FRAME_W32_WINDOW (f));
8002 oldobj = SelectObject (hdc, FRAME_FONT (f)->hfont);
8003 GetTextFace (hdc, LF_FACESIZE, lf.lfFaceName);
8004 if (GetTextMetrics (hdc, &tm))
8006 lf.lfHeight = tm.tmInternalLeading - tm.tmHeight;
8007 lf.lfWeight = tm.tmWeight;
8008 lf.lfItalic = tm.tmItalic;
8009 lf.lfUnderline = tm.tmUnderlined;
8010 lf.lfStrikeOut = tm.tmStruckOut;
8011 lf.lfCharSet = tm.tmCharSet;
8012 cf.Flags |= CF_INITTOLOGFONTSTRUCT;
8014 SelectObject (hdc, oldobj);
8015 ReleaseDC (FRAME_W32_WINDOW (f), hdc);
8017 if (!ChooseFont (&cf) || !w32_to_x_font (&lf, buf, 100, NULL))
8018 return Qnil;
8020 return build_string (buf);
8023 DEFUN ("w32-send-sys-command", Fw32_send_sys_command,
8024 Sw32_send_sys_command, 1, 2, 0,
8025 doc: /* Send frame a Windows WM_SYSCOMMAND message of type COMMAND.
8026 Some useful values for COMMAND are #xf030 to maximize frame (#xf020
8027 to minimize), #xf120 to restore frame to original size, and #xf100
8028 to activate the menubar for keyboard access. #xf140 activates the
8029 screen saver if defined.
8031 If optional parameter FRAME is not specified, use selected frame. */)
8032 (command, frame)
8033 Lisp_Object command, frame;
8035 FRAME_PTR f = check_x_frame (frame);
8037 CHECK_NUMBER (command);
8039 PostMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, XINT (command), 0);
8041 return Qnil;
8044 DEFUN ("w32-shell-execute", Fw32_shell_execute, Sw32_shell_execute, 2, 4, 0,
8045 doc: /* Get Windows to perform OPERATION on DOCUMENT.
8046 This is a wrapper around the ShellExecute system function, which
8047 invokes the application registered to handle OPERATION for DOCUMENT.
8048 OPERATION is typically \"open\", \"print\" or \"explore\" (but can be
8049 nil for the default action), and DOCUMENT is typically the name of a
8050 document file or URL, but can also be a program executable to run or
8051 a directory to open in the Windows Explorer.
8053 If DOCUMENT is a program executable, PARAMETERS can be a string
8054 containing command line parameters, but otherwise should be nil.
8056 SHOW-FLAG can be used to control whether the invoked application is hidden
8057 or minimized. If SHOW-FLAG is nil, the application is displayed normally,
8058 otherwise it is an integer representing a ShowWindow flag:
8060 0 - start hidden
8061 1 - start normally
8062 3 - start maximized
8063 6 - start minimized */)
8064 (operation, document, parameters, show_flag)
8065 Lisp_Object operation, document, parameters, show_flag;
8067 Lisp_Object current_dir;
8069 CHECK_STRING (document);
8071 /* Encode filename and current directory. */
8072 current_dir = ENCODE_FILE (current_buffer->directory);
8073 document = ENCODE_FILE (document);
8074 if ((int) ShellExecute (NULL,
8075 (STRINGP (operation) ?
8076 SDATA (operation) : NULL),
8077 SDATA (document),
8078 (STRINGP (parameters) ?
8079 SDATA (parameters) : NULL),
8080 SDATA (current_dir),
8081 (INTEGERP (show_flag) ?
8082 XINT (show_flag) : SW_SHOWDEFAULT))
8083 > 32)
8084 return Qt;
8085 error ("ShellExecute failed: %s", w32_strerror (0));
8088 /* Lookup virtual keycode from string representing the name of a
8089 non-ascii keystroke into the corresponding virtual key, using
8090 lispy_function_keys. */
8091 static int
8092 lookup_vk_code (char *key)
8094 int i;
8096 for (i = 0; i < 256; i++)
8097 if (lispy_function_keys[i] != 0
8098 && strcmp (lispy_function_keys[i], key) == 0)
8099 return i;
8101 return -1;
8104 /* Convert a one-element vector style key sequence to a hot key
8105 definition. */
8106 static Lisp_Object
8107 w32_parse_hot_key (key)
8108 Lisp_Object key;
8110 /* Copied from Fdefine_key and store_in_keymap. */
8111 register Lisp_Object c;
8112 int vk_code;
8113 int lisp_modifiers;
8114 int w32_modifiers;
8115 struct gcpro gcpro1;
8117 CHECK_VECTOR (key);
8119 if (XFASTINT (Flength (key)) != 1)
8120 return Qnil;
8122 GCPRO1 (key);
8124 c = Faref (key, make_number (0));
8126 if (CONSP (c) && lucid_event_type_list_p (c))
8127 c = Fevent_convert_list (c);
8129 UNGCPRO;
8131 if (! INTEGERP (c) && ! SYMBOLP (c))
8132 error ("Key definition is invalid");
8134 /* Work out the base key and the modifiers. */
8135 if (SYMBOLP (c))
8137 c = parse_modifiers (c);
8138 lisp_modifiers = XINT (Fcar (Fcdr (c)));
8139 c = Fcar (c);
8140 if (!SYMBOLP (c))
8141 abort ();
8142 vk_code = lookup_vk_code (SDATA (SYMBOL_NAME (c)));
8144 else if (INTEGERP (c))
8146 lisp_modifiers = XINT (c) & ~CHARACTERBITS;
8147 /* Many ascii characters are their own virtual key code. */
8148 vk_code = XINT (c) & CHARACTERBITS;
8151 if (vk_code < 0 || vk_code > 255)
8152 return Qnil;
8154 if ((lisp_modifiers & meta_modifier) != 0
8155 && !NILP (Vw32_alt_is_meta))
8156 lisp_modifiers |= alt_modifier;
8158 /* Supply defs missing from mingw32. */
8159 #ifndef MOD_ALT
8160 #define MOD_ALT 0x0001
8161 #define MOD_CONTROL 0x0002
8162 #define MOD_SHIFT 0x0004
8163 #define MOD_WIN 0x0008
8164 #endif
8166 /* Convert lisp modifiers to Windows hot-key form. */
8167 w32_modifiers = (lisp_modifiers & hyper_modifier) ? MOD_WIN : 0;
8168 w32_modifiers |= (lisp_modifiers & alt_modifier) ? MOD_ALT : 0;
8169 w32_modifiers |= (lisp_modifiers & ctrl_modifier) ? MOD_CONTROL : 0;
8170 w32_modifiers |= (lisp_modifiers & shift_modifier) ? MOD_SHIFT : 0;
8172 return HOTKEY (vk_code, w32_modifiers);
8175 DEFUN ("w32-register-hot-key", Fw32_register_hot_key,
8176 Sw32_register_hot_key, 1, 1, 0,
8177 doc: /* Register KEY as a hot-key combination.
8178 Certain key combinations like Alt-Tab are reserved for system use on
8179 Windows, and therefore are normally intercepted by the system. However,
8180 most of these key combinations can be received by registering them as
8181 hot-keys, overriding their special meaning.
8183 KEY must be a one element key definition in vector form that would be
8184 acceptable to `define-key' (e.g. [A-tab] for Alt-Tab). The meta
8185 modifier is interpreted as Alt if `w32-alt-is-meta' is t, and hyper
8186 is always interpreted as the Windows modifier keys.
8188 The return value is the hotkey-id if registered, otherwise nil. */)
8189 (key)
8190 Lisp_Object key;
8192 key = w32_parse_hot_key (key);
8194 if (NILP (Fmemq (key, w32_grabbed_keys)))
8196 /* Reuse an empty slot if possible. */
8197 Lisp_Object item = Fmemq (Qnil, w32_grabbed_keys);
8199 /* Safe to add new key to list, even if we have focus. */
8200 if (NILP (item))
8201 w32_grabbed_keys = Fcons (key, w32_grabbed_keys);
8202 else
8203 XSETCAR (item, key);
8205 /* Notify input thread about new hot-key definition, so that it
8206 takes effect without needing to switch focus. */
8207 #ifdef USE_LISP_UNION_TYPE
8208 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
8209 (WPARAM) key.i, 0);
8210 #else
8211 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
8212 (WPARAM) key, 0);
8213 #endif
8216 return key;
8219 DEFUN ("w32-unregister-hot-key", Fw32_unregister_hot_key,
8220 Sw32_unregister_hot_key, 1, 1, 0,
8221 doc: /* Unregister KEY as a hot-key combination. */)
8222 (key)
8223 Lisp_Object key;
8225 Lisp_Object item;
8227 if (!INTEGERP (key))
8228 key = w32_parse_hot_key (key);
8230 item = Fmemq (key, w32_grabbed_keys);
8232 if (!NILP (item))
8234 /* Notify input thread about hot-key definition being removed, so
8235 that it takes effect without needing focus switch. */
8236 #ifdef USE_LISP_UNION_TYPE
8237 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
8238 (WPARAM) XINT (XCAR (item)), (LPARAM) item.i))
8239 #else
8240 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
8241 (WPARAM) XINT (XCAR (item)), (LPARAM) item))
8243 #endif
8245 MSG msg;
8246 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
8248 return Qt;
8250 return Qnil;
8253 DEFUN ("w32-registered-hot-keys", Fw32_registered_hot_keys,
8254 Sw32_registered_hot_keys, 0, 0, 0,
8255 doc: /* Return list of registered hot-key IDs. */)
8258 return Fcopy_sequence (w32_grabbed_keys);
8261 DEFUN ("w32-reconstruct-hot-key", Fw32_reconstruct_hot_key,
8262 Sw32_reconstruct_hot_key, 1, 1, 0,
8263 doc: /* Convert hot-key ID to a lisp key combination.
8264 usage: (w32-reconstruct-hot-key ID) */)
8265 (hotkeyid)
8266 Lisp_Object hotkeyid;
8268 int vk_code, w32_modifiers;
8269 Lisp_Object key;
8271 CHECK_NUMBER (hotkeyid);
8273 vk_code = HOTKEY_VK_CODE (hotkeyid);
8274 w32_modifiers = HOTKEY_MODIFIERS (hotkeyid);
8276 if (lispy_function_keys[vk_code])
8277 key = intern (lispy_function_keys[vk_code]);
8278 else
8279 key = make_number (vk_code);
8281 key = Fcons (key, Qnil);
8282 if (w32_modifiers & MOD_SHIFT)
8283 key = Fcons (Qshift, key);
8284 if (w32_modifiers & MOD_CONTROL)
8285 key = Fcons (Qctrl, key);
8286 if (w32_modifiers & MOD_ALT)
8287 key = Fcons (NILP (Vw32_alt_is_meta) ? Qalt : Qmeta, key);
8288 if (w32_modifiers & MOD_WIN)
8289 key = Fcons (Qhyper, key);
8291 return key;
8294 DEFUN ("w32-toggle-lock-key", Fw32_toggle_lock_key,
8295 Sw32_toggle_lock_key, 1, 2, 0,
8296 doc: /* Toggle the state of the lock key KEY.
8297 KEY can be `capslock', `kp-numlock', or `scroll'.
8298 If the optional parameter NEW-STATE is a number, then the state of KEY
8299 is set to off if the low bit of NEW-STATE is zero, otherwise on. */)
8300 (key, new_state)
8301 Lisp_Object key, new_state;
8303 int vk_code;
8305 if (EQ (key, intern ("capslock")))
8306 vk_code = VK_CAPITAL;
8307 else if (EQ (key, intern ("kp-numlock")))
8308 vk_code = VK_NUMLOCK;
8309 else if (EQ (key, intern ("scroll")))
8310 vk_code = VK_SCROLL;
8311 else
8312 return Qnil;
8314 if (!dwWindowsThreadId)
8315 return make_number (w32_console_toggle_lock_key (vk_code, new_state));
8317 #ifdef USE_LISP_UNION_TYPE
8318 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
8319 (WPARAM) vk_code, (LPARAM) new_state.i))
8320 #else
8321 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
8322 (WPARAM) vk_code, (LPARAM) new_state))
8323 #endif
8325 MSG msg;
8326 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
8327 return make_number (msg.wParam);
8329 return Qnil;
8332 DEFUN ("file-system-info", Ffile_system_info, Sfile_system_info, 1, 1, 0,
8333 doc: /* Return storage information about the file system FILENAME is on.
8334 Value is a list of floats (TOTAL FREE AVAIL), where TOTAL is the total
8335 storage of the file system, FREE is the free storage, and AVAIL is the
8336 storage available to a non-superuser. All 3 numbers are in bytes.
8337 If the underlying system call fails, value is nil. */)
8338 (filename)
8339 Lisp_Object filename;
8341 Lisp_Object encoded, value;
8343 CHECK_STRING (filename);
8344 filename = Fexpand_file_name (filename, Qnil);
8345 encoded = ENCODE_FILE (filename);
8347 value = Qnil;
8349 /* Determining the required information on Windows turns out, sadly,
8350 to be more involved than one would hope. The original Win32 api
8351 call for this will return bogus information on some systems, but we
8352 must dynamically probe for the replacement api, since that was
8353 added rather late on. */
8355 HMODULE hKernel = GetModuleHandle ("kernel32");
8356 BOOL (*pfn_GetDiskFreeSpaceEx)
8357 (char *, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER)
8358 = (void *) GetProcAddress (hKernel, "GetDiskFreeSpaceEx");
8360 /* On Windows, we may need to specify the root directory of the
8361 volume holding FILENAME. */
8362 char rootname[MAX_PATH];
8363 char *name = SDATA (encoded);
8365 /* find the root name of the volume if given */
8366 if (isalpha (name[0]) && name[1] == ':')
8368 rootname[0] = name[0];
8369 rootname[1] = name[1];
8370 rootname[2] = '\\';
8371 rootname[3] = 0;
8373 else if (IS_DIRECTORY_SEP (name[0]) && IS_DIRECTORY_SEP (name[1]))
8375 char *str = rootname;
8376 int slashes = 4;
8379 if (IS_DIRECTORY_SEP (*name) && --slashes == 0)
8380 break;
8381 *str++ = *name++;
8383 while ( *name );
8385 *str++ = '\\';
8386 *str = 0;
8389 if (pfn_GetDiskFreeSpaceEx)
8391 /* Unsigned large integers cannot be cast to double, so
8392 use signed ones instead. */
8393 LARGE_INTEGER availbytes;
8394 LARGE_INTEGER freebytes;
8395 LARGE_INTEGER totalbytes;
8397 if (pfn_GetDiskFreeSpaceEx(rootname,
8398 (ULARGE_INTEGER *)&availbytes,
8399 (ULARGE_INTEGER *)&totalbytes,
8400 (ULARGE_INTEGER *)&freebytes))
8401 value = list3 (make_float ((double) totalbytes.QuadPart),
8402 make_float ((double) freebytes.QuadPart),
8403 make_float ((double) availbytes.QuadPart));
8405 else
8407 DWORD sectors_per_cluster;
8408 DWORD bytes_per_sector;
8409 DWORD free_clusters;
8410 DWORD total_clusters;
8412 if (GetDiskFreeSpace(rootname,
8413 &sectors_per_cluster,
8414 &bytes_per_sector,
8415 &free_clusters,
8416 &total_clusters))
8417 value = list3 (make_float ((double) total_clusters
8418 * sectors_per_cluster * bytes_per_sector),
8419 make_float ((double) free_clusters
8420 * sectors_per_cluster * bytes_per_sector),
8421 make_float ((double) free_clusters
8422 * sectors_per_cluster * bytes_per_sector));
8426 return value;
8429 DEFUN ("default-printer-name", Fdefault_printer_name, Sdefault_printer_name,
8430 0, 0, 0, doc: /* Return the name of Windows default printer device. */)
8433 static char pname_buf[256];
8434 int err;
8435 HANDLE hPrn;
8436 PRINTER_INFO_2 *ppi2 = NULL;
8437 DWORD dwNeeded = 0, dwReturned = 0;
8439 /* Retrieve the default string from Win.ini (the registry).
8440 * String will be in form "printername,drivername,portname".
8441 * This is the most portable way to get the default printer. */
8442 if (GetProfileString ("windows", "device", ",,", pname_buf, sizeof (pname_buf)) <= 0)
8443 return Qnil;
8444 /* printername precedes first "," character */
8445 strtok (pname_buf, ",");
8446 /* We want to know more than the printer name */
8447 if (!OpenPrinter (pname_buf, &hPrn, NULL))
8448 return Qnil;
8449 GetPrinter (hPrn, 2, NULL, 0, &dwNeeded);
8450 if (dwNeeded == 0)
8452 ClosePrinter (hPrn);
8453 return Qnil;
8455 /* Allocate memory for the PRINTER_INFO_2 struct */
8456 ppi2 = (PRINTER_INFO_2 *) xmalloc (dwNeeded);
8457 if (!ppi2)
8459 ClosePrinter (hPrn);
8460 return Qnil;
8462 /* Call GetPrinter() again with big enouth memory block */
8463 err = GetPrinter (hPrn, 2, (LPBYTE)ppi2, dwNeeded, &dwReturned);
8464 ClosePrinter (hPrn);
8465 if (!err)
8467 xfree(ppi2);
8468 return Qnil;
8471 if (ppi2)
8473 if (ppi2->Attributes & PRINTER_ATTRIBUTE_SHARED && ppi2->pServerName)
8475 /* a remote printer */
8476 if (*ppi2->pServerName == '\\')
8477 _snprintf(pname_buf, sizeof (pname_buf), "%s\\%s", ppi2->pServerName,
8478 ppi2->pShareName);
8479 else
8480 _snprintf(pname_buf, sizeof (pname_buf), "\\\\%s\\%s", ppi2->pServerName,
8481 ppi2->pShareName);
8482 pname_buf[sizeof (pname_buf) - 1] = '\0';
8484 else
8486 /* a local printer */
8487 strncpy(pname_buf, ppi2->pPortName, sizeof (pname_buf));
8488 pname_buf[sizeof (pname_buf) - 1] = '\0';
8489 /* `pPortName' can include several ports, delimited by ','.
8490 * we only use the first one. */
8491 strtok(pname_buf, ",");
8493 xfree(ppi2);
8496 return build_string (pname_buf);
8499 /***********************************************************************
8500 Initialization
8501 ***********************************************************************/
8503 /* Keep this list in the same order as frame_parms in frame.c.
8504 Use 0 for unsupported frame parameters. */
8506 frame_parm_handler w32_frame_parm_handlers[] =
8508 x_set_autoraise,
8509 x_set_autolower,
8510 x_set_background_color,
8511 x_set_border_color,
8512 x_set_border_width,
8513 x_set_cursor_color,
8514 x_set_cursor_type,
8515 x_set_font,
8516 x_set_foreground_color,
8517 x_set_icon_name,
8518 x_set_icon_type,
8519 x_set_internal_border_width,
8520 x_set_menu_bar_lines,
8521 x_set_mouse_color,
8522 x_explicitly_set_name,
8523 x_set_scroll_bar_width,
8524 x_set_title,
8525 x_set_unsplittable,
8526 x_set_vertical_scroll_bars,
8527 x_set_visibility,
8528 x_set_tool_bar_lines,
8529 0, /* x_set_scroll_bar_foreground, */
8530 0, /* x_set_scroll_bar_background, */
8531 x_set_screen_gamma,
8532 x_set_line_spacing,
8533 x_set_fringe_width,
8534 x_set_fringe_width,
8535 0, /* x_set_wait_for_wm, */
8536 x_set_fullscreen,
8539 void
8540 syms_of_w32fns ()
8542 globals_of_w32fns ();
8543 /* This is zero if not using MS-Windows. */
8544 w32_in_use = 0;
8545 track_mouse_window = NULL;
8547 w32_visible_system_caret_hwnd = NULL;
8549 Qnone = intern ("none");
8550 staticpro (&Qnone);
8551 Qsuppress_icon = intern ("suppress-icon");
8552 staticpro (&Qsuppress_icon);
8553 Qundefined_color = intern ("undefined-color");
8554 staticpro (&Qundefined_color);
8555 Qcancel_timer = intern ("cancel-timer");
8556 staticpro (&Qcancel_timer);
8558 Qhyper = intern ("hyper");
8559 staticpro (&Qhyper);
8560 Qsuper = intern ("super");
8561 staticpro (&Qsuper);
8562 Qmeta = intern ("meta");
8563 staticpro (&Qmeta);
8564 Qalt = intern ("alt");
8565 staticpro (&Qalt);
8566 Qctrl = intern ("ctrl");
8567 staticpro (&Qctrl);
8568 Qcontrol = intern ("control");
8569 staticpro (&Qcontrol);
8570 Qshift = intern ("shift");
8571 staticpro (&Qshift);
8572 /* This is the end of symbol initialization. */
8574 /* Text property `display' should be nonsticky by default. */
8575 Vtext_property_default_nonsticky
8576 = Fcons (Fcons (Qdisplay, Qt), Vtext_property_default_nonsticky);
8579 Fput (Qundefined_color, Qerror_conditions,
8580 Fcons (Qundefined_color, Fcons (Qerror, Qnil)));
8581 Fput (Qundefined_color, Qerror_message,
8582 build_string ("Undefined color"));
8584 staticpro (&w32_grabbed_keys);
8585 w32_grabbed_keys = Qnil;
8587 DEFVAR_LISP ("w32-color-map", &Vw32_color_map,
8588 doc: /* An array of color name mappings for Windows. */);
8589 Vw32_color_map = Qnil;
8591 DEFVAR_LISP ("w32-pass-alt-to-system", &Vw32_pass_alt_to_system,
8592 doc: /* Non-nil if Alt key presses are passed on to Windows.
8593 When non-nil, for example, Alt pressed and released and then space will
8594 open the System menu. When nil, Emacs processes the Alt key events, and
8595 then silently swallows them. */);
8596 Vw32_pass_alt_to_system = Qnil;
8598 DEFVAR_LISP ("w32-alt-is-meta", &Vw32_alt_is_meta,
8599 doc: /* Non-nil if the Alt key is to be considered the same as the META key.
8600 When nil, Emacs will translate the Alt key to the ALT modifier, not to META. */);
8601 Vw32_alt_is_meta = Qt;
8603 DEFVAR_INT ("w32-quit-key", &w32_quit_key,
8604 doc: /* If non-zero, the virtual key code for an alternative quit key. */);
8605 w32_quit_key = 0;
8607 DEFVAR_LISP ("w32-pass-lwindow-to-system",
8608 &Vw32_pass_lwindow_to_system,
8609 doc: /* If non-nil, the left \"Windows\" key is passed on to Windows.
8611 When non-nil, the Start menu is opened by tapping the key.
8612 If you set this to nil, the left \"Windows\" key is processed by Emacs
8613 according to the value of `w32-lwindow-modifier', which see.
8615 Note that some combinations of the left \"Windows\" key with other keys are
8616 caught by Windows at low level, and so binding them in Emacs will have no
8617 effect. For example, <lwindow>-r always pops up the Windows Run dialog,
8618 <lwindow>-<Pause> pops up the "System Properties" dialog, etc. However, see
8619 the doc string of `w32-phantom-key-code'. */);
8620 Vw32_pass_lwindow_to_system = Qt;
8622 DEFVAR_LISP ("w32-pass-rwindow-to-system",
8623 &Vw32_pass_rwindow_to_system,
8624 doc: /* If non-nil, the right \"Windows\" key is passed on to Windows.
8626 When non-nil, the Start menu is opened by tapping the key.
8627 If you set this to nil, the right \"Windows\" key is processed by Emacs
8628 according to the value of `w32-rwindow-modifier', which see.
8630 Note that some combinations of the right \"Windows\" key with other keys are
8631 caught by Windows at low level, and so binding them in Emacs will have no
8632 effect. For example, <rwindow>-r always pops up the Windows Run dialog,
8633 <rwindow>-<Pause> pops up the "System Properties" dialog, etc. However, see
8634 the doc string of `w32-phantom-key-code'. */);
8635 Vw32_pass_rwindow_to_system = Qt;
8637 DEFVAR_LISP ("w32-phantom-key-code",
8638 &Vw32_phantom_key_code,
8639 doc: /* Virtual key code used to generate \"phantom\" key presses.
8640 Value is a number between 0 and 255.
8642 Phantom key presses are generated in order to stop the system from
8643 acting on \"Windows\" key events when `w32-pass-lwindow-to-system' or
8644 `w32-pass-rwindow-to-system' is nil. */);
8645 /* Although 255 is technically not a valid key code, it works and
8646 means that this hack won't interfere with any real key code. */
8647 XSETINT (Vw32_phantom_key_code, 255);
8649 DEFVAR_LISP ("w32-enable-num-lock",
8650 &Vw32_enable_num_lock,
8651 doc: /* If non-nil, the Num Lock key acts normally.
8652 Set to nil to handle Num Lock as the `kp-numlock' key. */);
8653 Vw32_enable_num_lock = Qt;
8655 DEFVAR_LISP ("w32-enable-caps-lock",
8656 &Vw32_enable_caps_lock,
8657 doc: /* If non-nil, the Caps Lock key acts normally.
8658 Set to nil to handle Caps Lock as the `capslock' key. */);
8659 Vw32_enable_caps_lock = Qt;
8661 DEFVAR_LISP ("w32-scroll-lock-modifier",
8662 &Vw32_scroll_lock_modifier,
8663 doc: /* Modifier to use for the Scroll Lock ON state.
8664 The value can be hyper, super, meta, alt, control or shift for the
8665 respective modifier, or nil to handle Scroll Lock as the `scroll' key.
8666 Any other value will cause the Scroll Lock key to be ignored. */);
8667 Vw32_scroll_lock_modifier = Qt;
8669 DEFVAR_LISP ("w32-lwindow-modifier",
8670 &Vw32_lwindow_modifier,
8671 doc: /* Modifier to use for the left \"Windows\" key.
8672 The value can be hyper, super, meta, alt, control or shift for the
8673 respective modifier, or nil to appear as the `lwindow' key.
8674 Any other value will cause the key to be ignored. */);
8675 Vw32_lwindow_modifier = Qnil;
8677 DEFVAR_LISP ("w32-rwindow-modifier",
8678 &Vw32_rwindow_modifier,
8679 doc: /* Modifier to use for the right \"Windows\" key.
8680 The value can be hyper, super, meta, alt, control or shift for the
8681 respective modifier, or nil to appear as the `rwindow' key.
8682 Any other value will cause the key to be ignored. */);
8683 Vw32_rwindow_modifier = Qnil;
8685 DEFVAR_LISP ("w32-apps-modifier",
8686 &Vw32_apps_modifier,
8687 doc: /* Modifier to use for the \"Apps\" key.
8688 The value can be hyper, super, meta, alt, control or shift for the
8689 respective modifier, or nil to appear as the `apps' key.
8690 Any other value will cause the key to be ignored. */);
8691 Vw32_apps_modifier = Qnil;
8693 DEFVAR_BOOL ("w32-enable-synthesized-fonts", &w32_enable_synthesized_fonts,
8694 doc: /* Non-nil enables selection of artificially italicized and bold fonts. */);
8695 w32_enable_synthesized_fonts = 0;
8697 DEFVAR_LISP ("w32-enable-palette", &Vw32_enable_palette,
8698 doc: /* Non-nil enables Windows palette management to map colors exactly. */);
8699 Vw32_enable_palette = Qt;
8701 DEFVAR_INT ("w32-mouse-button-tolerance",
8702 &w32_mouse_button_tolerance,
8703 doc: /* Analogue of double click interval for faking middle mouse events.
8704 The value is the minimum time in milliseconds that must elapse between
8705 left and right button down events before they are considered distinct events.
8706 If both mouse buttons are depressed within this interval, a middle mouse
8707 button down event is generated instead. */);
8708 w32_mouse_button_tolerance = GetDoubleClickTime () / 2;
8710 DEFVAR_INT ("w32-mouse-move-interval",
8711 &w32_mouse_move_interval,
8712 doc: /* Minimum interval between mouse move events.
8713 The value is the minimum time in milliseconds that must elapse between
8714 successive mouse move (or scroll bar drag) events before they are
8715 reported as lisp events. */);
8716 w32_mouse_move_interval = 0;
8718 DEFVAR_BOOL ("w32-pass-extra-mouse-buttons-to-system",
8719 &w32_pass_extra_mouse_buttons_to_system,
8720 doc: /* If non-nil, the fourth and fifth mouse buttons are passed to Windows.
8721 Recent versions of Windows support mice with up to five buttons.
8722 Since most applications don't support these extra buttons, most mouse
8723 drivers will allow you to map them to functions at the system level.
8724 If this variable is non-nil, Emacs will pass them on, allowing the
8725 system to handle them. */);
8726 w32_pass_extra_mouse_buttons_to_system = 0;
8728 DEFVAR_LISP ("x-pointer-shape", &Vx_pointer_shape,
8729 doc: /* The shape of the pointer when over text.
8730 Changing the value does not affect existing frames
8731 unless you set the mouse color. */);
8732 Vx_pointer_shape = Qnil;
8734 Vx_nontext_pointer_shape = Qnil;
8736 Vx_mode_pointer_shape = Qnil;
8738 DEFVAR_LISP ("x-hourglass-pointer-shape", &Vx_hourglass_pointer_shape,
8739 doc: /* The shape of the pointer when Emacs is busy.
8740 This variable takes effect when you create a new frame
8741 or when you set the mouse color. */);
8742 Vx_hourglass_pointer_shape = Qnil;
8744 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
8745 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
8746 display_hourglass_p = 1;
8748 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
8749 doc: /* *Seconds to wait before displaying an hourglass pointer.
8750 Value must be an integer or float. */);
8751 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
8753 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
8754 &Vx_sensitive_text_pointer_shape,
8755 doc: /* The shape of the pointer when over mouse-sensitive text.
8756 This variable takes effect when you create a new frame
8757 or when you set the mouse color. */);
8758 Vx_sensitive_text_pointer_shape = Qnil;
8760 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
8761 &Vx_window_horizontal_drag_shape,
8762 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
8763 This variable takes effect when you create a new frame
8764 or when you set the mouse color. */);
8765 Vx_window_horizontal_drag_shape = Qnil;
8767 DEFVAR_LISP ("x-cursor-fore-pixel", &Vx_cursor_fore_pixel,
8768 doc: /* A string indicating the foreground color of the cursor box. */);
8769 Vx_cursor_fore_pixel = Qnil;
8771 DEFVAR_LISP ("x-max-tooltip-size", &Vx_max_tooltip_size,
8772 doc: /* Maximum size for tooltips.
8773 Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
8774 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
8776 DEFVAR_LISP ("x-no-window-manager", &Vx_no_window_manager,
8777 doc: /* Non-nil if no window manager is in use.
8778 Emacs doesn't try to figure this out; this is always nil
8779 unless you set it to something else. */);
8780 /* We don't have any way to find this out, so set it to nil
8781 and maybe the user would like to set it to t. */
8782 Vx_no_window_manager = Qnil;
8784 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
8785 &Vx_pixel_size_width_font_regexp,
8786 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
8788 Since Emacs gets width of a font matching with this regexp from
8789 PIXEL_SIZE field of the name, font finding mechanism gets faster for
8790 such a font. This is especially effective for such large fonts as
8791 Chinese, Japanese, and Korean. */);
8792 Vx_pixel_size_width_font_regexp = Qnil;
8794 DEFVAR_LISP ("w32-bdf-filename-alist",
8795 &Vw32_bdf_filename_alist,
8796 doc: /* List of bdf fonts and their corresponding filenames. */);
8797 Vw32_bdf_filename_alist = Qnil;
8799 DEFVAR_BOOL ("w32-strict-fontnames",
8800 &w32_strict_fontnames,
8801 doc: /* Non-nil means only use fonts that are exact matches for those requested.
8802 Default is nil, which allows old fontnames that are not XLFD compliant,
8803 and allows third-party CJK display to work by specifying false charset
8804 fields to trick Emacs into translating to Big5, SJIS etc.
8805 Setting this to t will prevent wrong fonts being selected when
8806 fontsets are automatically created. */);
8807 w32_strict_fontnames = 0;
8809 DEFVAR_BOOL ("w32-strict-painting",
8810 &w32_strict_painting,
8811 doc: /* Non-nil means use strict rules for repainting frames.
8812 Set this to nil to get the old behavior for repainting; this should
8813 only be necessary if the default setting causes problems. */);
8814 w32_strict_painting = 1;
8816 DEFVAR_LISP ("w32-charset-info-alist",
8817 &Vw32_charset_info_alist,
8818 doc: /* Alist linking Emacs character sets to Windows fonts and codepages.
8819 Each entry should be of the form:
8821 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE))
8823 where CHARSET_NAME is a string used in font names to identify the charset,
8824 WINDOWS_CHARSET is a symbol that can be one of:
8825 w32-charset-ansi, w32-charset-default, w32-charset-symbol,
8826 w32-charset-shiftjis, w32-charset-hangeul, w32-charset-gb2312,
8827 w32-charset-chinesebig5,
8828 w32-charset-johab, w32-charset-hebrew,
8829 w32-charset-arabic, w32-charset-greek, w32-charset-turkish,
8830 w32-charset-vietnamese, w32-charset-thai, w32-charset-easteurope,
8831 w32-charset-russian, w32-charset-mac, w32-charset-baltic,
8832 w32-charset-unicode,
8833 or w32-charset-oem.
8834 CODEPAGE should be an integer specifying the codepage that should be used
8835 to display the character set, t to do no translation and output as Unicode,
8836 or nil to do no translation and output as 8 bit (or multibyte on far-east
8837 versions of Windows) characters. */);
8838 Vw32_charset_info_alist = Qnil;
8840 staticpro (&Qw32_charset_ansi);
8841 Qw32_charset_ansi = intern ("w32-charset-ansi");
8842 staticpro (&Qw32_charset_symbol);
8843 Qw32_charset_default = intern ("w32-charset-default");
8844 staticpro (&Qw32_charset_default);
8845 Qw32_charset_symbol = intern ("w32-charset-symbol");
8846 staticpro (&Qw32_charset_shiftjis);
8847 Qw32_charset_shiftjis = intern ("w32-charset-shiftjis");
8848 staticpro (&Qw32_charset_hangeul);
8849 Qw32_charset_hangeul = intern ("w32-charset-hangeul");
8850 staticpro (&Qw32_charset_chinesebig5);
8851 Qw32_charset_chinesebig5 = intern ("w32-charset-chinesebig5");
8852 staticpro (&Qw32_charset_gb2312);
8853 Qw32_charset_gb2312 = intern ("w32-charset-gb2312");
8854 staticpro (&Qw32_charset_oem);
8855 Qw32_charset_oem = intern ("w32-charset-oem");
8857 #ifdef JOHAB_CHARSET
8859 static int w32_extra_charsets_defined = 1;
8860 DEFVAR_BOOL ("w32-extra-charsets-defined", &w32_extra_charsets_defined,
8861 doc: /* Internal variable. */);
8863 staticpro (&Qw32_charset_johab);
8864 Qw32_charset_johab = intern ("w32-charset-johab");
8865 staticpro (&Qw32_charset_easteurope);
8866 Qw32_charset_easteurope = intern ("w32-charset-easteurope");
8867 staticpro (&Qw32_charset_turkish);
8868 Qw32_charset_turkish = intern ("w32-charset-turkish");
8869 staticpro (&Qw32_charset_baltic);
8870 Qw32_charset_baltic = intern ("w32-charset-baltic");
8871 staticpro (&Qw32_charset_russian);
8872 Qw32_charset_russian = intern ("w32-charset-russian");
8873 staticpro (&Qw32_charset_arabic);
8874 Qw32_charset_arabic = intern ("w32-charset-arabic");
8875 staticpro (&Qw32_charset_greek);
8876 Qw32_charset_greek = intern ("w32-charset-greek");
8877 staticpro (&Qw32_charset_hebrew);
8878 Qw32_charset_hebrew = intern ("w32-charset-hebrew");
8879 staticpro (&Qw32_charset_vietnamese);
8880 Qw32_charset_vietnamese = intern ("w32-charset-vietnamese");
8881 staticpro (&Qw32_charset_thai);
8882 Qw32_charset_thai = intern ("w32-charset-thai");
8883 staticpro (&Qw32_charset_mac);
8884 Qw32_charset_mac = intern ("w32-charset-mac");
8886 #endif
8888 #ifdef UNICODE_CHARSET
8890 static int w32_unicode_charset_defined = 1;
8891 DEFVAR_BOOL ("w32-unicode-charset-defined",
8892 &w32_unicode_charset_defined,
8893 doc: /* Internal variable. */);
8895 staticpro (&Qw32_charset_unicode);
8896 Qw32_charset_unicode = intern ("w32-charset-unicode");
8897 #endif
8899 #if 0 /* TODO: Port to W32 */
8900 defsubr (&Sx_change_window_property);
8901 defsubr (&Sx_delete_window_property);
8902 defsubr (&Sx_window_property);
8903 #endif
8904 defsubr (&Sxw_display_color_p);
8905 defsubr (&Sx_display_grayscale_p);
8906 defsubr (&Sxw_color_defined_p);
8907 defsubr (&Sxw_color_values);
8908 defsubr (&Sx_server_max_request_size);
8909 defsubr (&Sx_server_vendor);
8910 defsubr (&Sx_server_version);
8911 defsubr (&Sx_display_pixel_width);
8912 defsubr (&Sx_display_pixel_height);
8913 defsubr (&Sx_display_mm_width);
8914 defsubr (&Sx_display_mm_height);
8915 defsubr (&Sx_display_screens);
8916 defsubr (&Sx_display_planes);
8917 defsubr (&Sx_display_color_cells);
8918 defsubr (&Sx_display_visual_class);
8919 defsubr (&Sx_display_backing_store);
8920 defsubr (&Sx_display_save_under);
8921 defsubr (&Sx_create_frame);
8922 defsubr (&Sx_open_connection);
8923 defsubr (&Sx_close_connection);
8924 defsubr (&Sx_display_list);
8925 defsubr (&Sx_synchronize);
8927 /* W32 specific functions */
8929 defsubr (&Sw32_focus_frame);
8930 defsubr (&Sw32_select_font);
8931 defsubr (&Sw32_define_rgb_color);
8932 defsubr (&Sw32_default_color_map);
8933 defsubr (&Sw32_load_color_file);
8934 defsubr (&Sw32_send_sys_command);
8935 defsubr (&Sw32_shell_execute);
8936 defsubr (&Sw32_register_hot_key);
8937 defsubr (&Sw32_unregister_hot_key);
8938 defsubr (&Sw32_registered_hot_keys);
8939 defsubr (&Sw32_reconstruct_hot_key);
8940 defsubr (&Sw32_toggle_lock_key);
8941 defsubr (&Sw32_find_bdf_fonts);
8943 defsubr (&Sfile_system_info);
8944 defsubr (&Sdefault_printer_name);
8946 /* Setting callback functions for fontset handler. */
8947 get_font_info_func = w32_get_font_info;
8949 #if 0 /* This function pointer doesn't seem to be used anywhere.
8950 And the pointer assigned has the wrong type, anyway. */
8951 list_fonts_func = w32_list_fonts;
8952 #endif
8954 load_font_func = w32_load_font;
8955 find_ccl_program_func = w32_find_ccl_program;
8956 query_font_func = w32_query_font;
8957 set_frame_fontset_func = x_set_font;
8958 check_window_system_func = check_w32;
8961 hourglass_atimer = NULL;
8962 hourglass_shown_p = 0;
8963 defsubr (&Sx_show_tip);
8964 defsubr (&Sx_hide_tip);
8965 tip_timer = Qnil;
8966 staticpro (&tip_timer);
8967 tip_frame = Qnil;
8968 staticpro (&tip_frame);
8970 last_show_tip_args = Qnil;
8971 staticpro (&last_show_tip_args);
8973 defsubr (&Sx_file_dialog);
8978 globals_of_w32fns is used to initialize those global variables that
8979 must always be initialized on startup even when the global variable
8980 initialized is non zero (see the function main in emacs.c).
8981 globals_of_w32fns is called from syms_of_w32fns when the global
8982 variable initialized is 0 and directly from main when initialized
8983 is non zero.
8985 void globals_of_w32fns ()
8987 HMODULE user32_lib = GetModuleHandle ("user32.dll");
8989 TrackMouseEvent not available in all versions of Windows, so must load
8990 it dynamically. Do it once, here, instead of every time it is used.
8992 track_mouse_event_fn = (TrackMouseEvent_Proc)
8993 GetProcAddress (user32_lib, "TrackMouseEvent");
8994 /* ditto for GetClipboardSequenceNumber. */
8995 clipboard_sequence_fn = (ClipboardSequence_Proc)
8996 GetProcAddress (user32_lib, "GetClipboardSequenceNumber");
8998 DEFVAR_INT ("w32-ansi-code-page",
8999 &w32_ansi_code_page,
9000 doc: /* The ANSI code page used by the system. */);
9001 w32_ansi_code_page = GetACP ();
9004 #undef abort
9006 void w32_abort (void) NO_RETURN;
9008 void
9009 w32_abort()
9011 int button;
9012 button = MessageBox (NULL,
9013 "A fatal error has occurred!\n\n"
9014 "Would you like to attach a debugger?\n\n"
9015 "Select YES to debug, NO to abort Emacs"
9016 #if __GNUC__
9017 "\n\n(type \"gdb -p <emacs-PID>\" and\n"
9018 "\"continue\" inside GDB before clicking YES.)"
9019 #endif
9020 , "Emacs Abort Dialog",
9021 MB_ICONEXCLAMATION | MB_TASKMODAL
9022 | MB_SETFOREGROUND | MB_YESNO);
9023 switch (button)
9025 case IDYES:
9026 DebugBreak ();
9027 exit (2); /* tell the compiler we will never return */
9028 case IDNO:
9029 default:
9030 abort ();
9031 break;
9035 /* For convenience when debugging. */
9037 w32_last_error()
9039 return GetLastError ();
9042 /* arch-tag: 707589ab-b9be-4638-8cdd-74629cc9b446
9043 (do not change this comment) */