Improve the explanation of version control to include concepts
[emacs.git] / src / w32fns.c
blob47ca9157623bfaae7a37a14ab8f90980577c1b8d
1 /* Graphical user interface functions for the Microsoft W32 API.
2 Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 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>
55 #include <objbase.h>
57 #include <dlgs.h>
58 #define FILE_NAME_TEXT_FIELD edt1
60 void syms_of_w32fns ();
61 void globals_of_w32fns ();
63 extern void free_frame_menubar ();
64 extern double atof ();
65 extern int w32_console_toggle_lock_key P_ ((int, Lisp_Object));
66 extern void w32_menu_display_help P_ ((HWND, HMENU, UINT, UINT));
67 extern void w32_free_menu_strings P_ ((HWND));
68 extern XCharStruct *w32_per_char_metric P_ ((XFontStruct *, wchar_t *, int));
70 extern int quit_char;
72 extern char *lispy_function_keys[];
74 /* The gray bitmap `bitmaps/gray'. This is done because w32term.c uses
75 it, and including `bitmaps/gray' more than once is a problem when
76 config.h defines `static' as an empty replacement string. */
78 int gray_bitmap_width = gray_width;
79 int gray_bitmap_height = gray_height;
80 unsigned char *gray_bitmap_bits = gray_bits;
82 /* The colormap for converting color names to RGB values */
83 Lisp_Object Vw32_color_map;
85 /* Non nil if alt key presses are passed on to Windows. */
86 Lisp_Object Vw32_pass_alt_to_system;
88 /* Non nil if alt key is translated to meta_modifier, nil if it is translated
89 to alt_modifier. */
90 Lisp_Object Vw32_alt_is_meta;
92 /* If non-zero, the windows virtual key code for an alternative quit key. */
93 int w32_quit_key;
95 /* Non nil if left window key events are passed on to Windows (this only
96 affects whether "tapping" the key opens the Start menu). */
97 Lisp_Object Vw32_pass_lwindow_to_system;
99 /* Non nil if right window key events are passed on to Windows (this
100 only affects whether "tapping" the key opens the Start menu). */
101 Lisp_Object Vw32_pass_rwindow_to_system;
103 /* Virtual key code used to generate "phantom" key presses in order
104 to stop system from acting on Windows key events. */
105 Lisp_Object Vw32_phantom_key_code;
107 /* Modifier associated with the left "Windows" key, or nil to act as a
108 normal key. */
109 Lisp_Object Vw32_lwindow_modifier;
111 /* Modifier associated with the right "Windows" key, or nil to act as a
112 normal key. */
113 Lisp_Object Vw32_rwindow_modifier;
115 /* Modifier associated with the "Apps" key, or nil to act as a normal
116 key. */
117 Lisp_Object Vw32_apps_modifier;
119 /* Value is nil if Num Lock acts as a function key. */
120 Lisp_Object Vw32_enable_num_lock;
122 /* Value is nil if Caps Lock acts as a function key. */
123 Lisp_Object Vw32_enable_caps_lock;
125 /* Modifier associated with Scroll Lock, or nil to act as a normal key. */
126 Lisp_Object Vw32_scroll_lock_modifier;
128 /* Switch to control whether we inhibit requests for synthesized bold
129 and italic versions of fonts. */
130 int w32_enable_synthesized_fonts;
132 /* Enable palette management. */
133 Lisp_Object Vw32_enable_palette;
135 /* Control how close left/right button down events must be to
136 be converted to a middle button down event. */
137 int w32_mouse_button_tolerance;
139 /* Minimum interval between mouse movement (and scroll bar drag)
140 events that are passed on to the event loop. */
141 int w32_mouse_move_interval;
143 /* Flag to indicate if XBUTTON events should be passed on to Windows. */
144 int w32_pass_extra_mouse_buttons_to_system;
146 /* Non nil if no window manager is in use. */
147 Lisp_Object Vx_no_window_manager;
149 /* Non-zero means we're allowed to display a hourglass pointer. */
151 int display_hourglass_p;
153 /* The background and shape of the mouse pointer, and shape when not
154 over text or in the modeline. */
156 Lisp_Object Vx_pointer_shape, Vx_nontext_pointer_shape, Vx_mode_pointer_shape;
157 Lisp_Object Vx_hourglass_pointer_shape, Vx_window_horizontal_drag_shape;
159 /* The shape when over mouse-sensitive text. */
161 Lisp_Object Vx_sensitive_text_pointer_shape;
163 #ifndef IDC_HAND
164 #define IDC_HAND MAKEINTRESOURCE(32649)
165 #endif
167 /* Color of chars displayed in cursor box. */
169 Lisp_Object Vx_cursor_fore_pixel;
171 /* Nonzero if using Windows. */
173 static int w32_in_use;
175 /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'. */
177 Lisp_Object Vx_pixel_size_width_font_regexp;
179 /* Alist of bdf fonts and the files that define them. */
180 Lisp_Object Vw32_bdf_filename_alist;
182 /* A flag to control whether fonts are matched strictly or not. */
183 int w32_strict_fontnames;
185 /* A flag to control whether we should only repaint if GetUpdateRect
186 indicates there is an update region. */
187 int w32_strict_painting;
189 /* Associative list linking character set strings to Windows codepages. */
190 Lisp_Object Vw32_charset_info_alist;
192 /* VIETNAMESE_CHARSET is not defined in some versions of MSVC. */
193 #ifndef VIETNAMESE_CHARSET
194 #define VIETNAMESE_CHARSET 163
195 #endif
197 Lisp_Object Qnone;
198 Lisp_Object Qsuppress_icon;
199 Lisp_Object Qundefined_color;
200 Lisp_Object Qcancel_timer;
201 Lisp_Object Qhyper;
202 Lisp_Object Qsuper;
203 Lisp_Object Qmeta;
204 Lisp_Object Qalt;
205 Lisp_Object Qctrl;
206 Lisp_Object Qcontrol;
207 Lisp_Object Qshift;
209 Lisp_Object Qw32_charset_ansi;
210 Lisp_Object Qw32_charset_default;
211 Lisp_Object Qw32_charset_symbol;
212 Lisp_Object Qw32_charset_shiftjis;
213 Lisp_Object Qw32_charset_hangeul;
214 Lisp_Object Qw32_charset_gb2312;
215 Lisp_Object Qw32_charset_chinesebig5;
216 Lisp_Object Qw32_charset_oem;
218 #ifndef JOHAB_CHARSET
219 #define JOHAB_CHARSET 130
220 #endif
221 #ifdef JOHAB_CHARSET
222 Lisp_Object Qw32_charset_easteurope;
223 Lisp_Object Qw32_charset_turkish;
224 Lisp_Object Qw32_charset_baltic;
225 Lisp_Object Qw32_charset_russian;
226 Lisp_Object Qw32_charset_arabic;
227 Lisp_Object Qw32_charset_greek;
228 Lisp_Object Qw32_charset_hebrew;
229 Lisp_Object Qw32_charset_vietnamese;
230 Lisp_Object Qw32_charset_thai;
231 Lisp_Object Qw32_charset_johab;
232 Lisp_Object Qw32_charset_mac;
233 #endif
235 #ifdef UNICODE_CHARSET
236 Lisp_Object Qw32_charset_unicode;
237 #endif
239 /* The ANSI codepage. */
240 int w32_ansi_code_page;
242 /* Prefix for system colors. */
243 #define SYSTEM_COLOR_PREFIX "System"
244 #define SYSTEM_COLOR_PREFIX_LEN (sizeof (SYSTEM_COLOR_PREFIX) - 1)
246 /* State variables for emulating a three button mouse. */
247 #define LMOUSE 1
248 #define MMOUSE 2
249 #define RMOUSE 4
251 static int button_state = 0;
252 static W32Msg saved_mouse_button_msg;
253 static unsigned mouse_button_timer = 0; /* non-zero when timer is active */
254 static W32Msg saved_mouse_move_msg;
255 static unsigned mouse_move_timer = 0;
257 /* Window that is tracking the mouse. */
258 static HWND track_mouse_window;
260 typedef BOOL (WINAPI * TrackMouseEvent_Proc)
261 (IN OUT LPTRACKMOUSEEVENT lpEventTrack);
263 TrackMouseEvent_Proc track_mouse_event_fn = NULL;
264 ClipboardSequence_Proc clipboard_sequence_fn = NULL;
265 extern AppendMenuW_Proc unicode_append_menu;
267 /* W95 mousewheel handler */
268 unsigned int msh_mousewheel = 0;
270 /* Timers */
271 #define MOUSE_BUTTON_ID 1
272 #define MOUSE_MOVE_ID 2
273 #define MENU_FREE_ID 3
274 /* The delay (milliseconds) before a menu is freed after WM_EXITMENULOOP
275 is received. */
276 #define MENU_FREE_DELAY 1000
277 static unsigned menu_free_timer = 0;
279 /* The below are defined in frame.c. */
281 extern Lisp_Object Vwindow_system_version;
283 #ifdef GLYPH_DEBUG
284 int image_cache_refcount, dpyinfo_refcount;
285 #endif
288 /* From w32term.c. */
289 extern int w32_num_mouse_buttons;
290 extern Lisp_Object Vw32_recognize_altgr;
292 extern HWND w32_system_caret_hwnd;
294 extern int w32_system_caret_height;
295 extern int w32_system_caret_x;
296 extern int w32_system_caret_y;
297 extern int w32_use_visible_system_caret;
299 static HWND w32_visible_system_caret_hwnd;
301 /* From w32menu.c */
302 extern HMENU current_popup_menu;
303 static int menubar_in_use = 0;
306 /* Error if we are not connected to MS-Windows. */
307 void
308 check_w32 ()
310 if (! w32_in_use)
311 error ("MS-Windows not in use or not initialized");
314 /* Nonzero if we can use mouse menus.
315 You should not call this unless HAVE_MENUS is defined. */
318 have_menus_p ()
320 return w32_in_use;
323 /* Extract a frame as a FRAME_PTR, defaulting to the selected frame
324 and checking validity for W32. */
326 FRAME_PTR
327 check_x_frame (frame)
328 Lisp_Object frame;
330 FRAME_PTR f;
332 if (NILP (frame))
333 frame = selected_frame;
334 CHECK_LIVE_FRAME (frame);
335 f = XFRAME (frame);
336 if (! FRAME_W32_P (f))
337 error ("Non-W32 frame used");
338 return f;
341 /* Let the user specify a display with a frame.
342 nil stands for the selected frame--or, if that is not a w32 frame,
343 the first display on the list. */
345 struct w32_display_info *
346 check_x_display_info (frame)
347 Lisp_Object frame;
349 if (NILP (frame))
351 struct frame *sf = XFRAME (selected_frame);
353 if (FRAME_W32_P (sf) && FRAME_LIVE_P (sf))
354 return FRAME_W32_DISPLAY_INFO (sf);
355 else
356 return &one_w32_display_info;
358 else if (STRINGP (frame))
359 return x_display_info_for_name (frame);
360 else
362 FRAME_PTR f;
364 CHECK_LIVE_FRAME (frame);
365 f = XFRAME (frame);
366 if (! FRAME_W32_P (f))
367 error ("Non-W32 frame used");
368 return FRAME_W32_DISPLAY_INFO (f);
372 /* Return the Emacs frame-object corresponding to an w32 window.
373 It could be the frame's main window or an icon window. */
375 /* This function can be called during GC, so use GC_xxx type test macros. */
377 struct frame *
378 x_window_to_frame (dpyinfo, wdesc)
379 struct w32_display_info *dpyinfo;
380 HWND wdesc;
382 Lisp_Object tail, frame;
383 struct frame *f;
385 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
387 frame = XCAR (tail);
388 if (!GC_FRAMEP (frame))
389 continue;
390 f = XFRAME (frame);
391 if (!FRAME_W32_P (f) || FRAME_W32_DISPLAY_INFO (f) != dpyinfo)
392 continue;
393 if (f->output_data.w32->hourglass_window == wdesc)
394 return f;
396 if (FRAME_W32_WINDOW (f) == wdesc)
397 return f;
399 return 0;
403 static Lisp_Object unwind_create_frame P_ ((Lisp_Object));
404 static Lisp_Object unwind_create_tip_frame P_ ((Lisp_Object));
405 static void my_create_window P_ ((struct frame *));
406 static void my_create_tip_window P_ ((struct frame *));
408 /* TODO: Native Input Method support; see x_create_im. */
409 void x_set_foreground_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
410 void x_set_background_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
411 void x_set_mouse_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
412 void x_set_cursor_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
413 void x_set_border_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
414 void x_set_cursor_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
415 void x_set_icon_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
416 void x_set_icon_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
417 void x_explicitly_set_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
418 void x_set_menu_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
419 void x_set_title P_ ((struct frame *, Lisp_Object, Lisp_Object));
420 void x_set_tool_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
421 static void x_edge_detection P_ ((struct frame *, struct image *, Lisp_Object,
422 Lisp_Object));
427 /* Store the screen positions of frame F into XPTR and YPTR.
428 These are the positions of the containing window manager window,
429 not Emacs's own window. */
431 void
432 x_real_positions (f, xptr, yptr)
433 FRAME_PTR f;
434 int *xptr, *yptr;
436 POINT pt;
437 RECT rect;
439 GetClientRect(FRAME_W32_WINDOW(f), &rect);
440 AdjustWindowRect(&rect, f->output_data.w32->dwStyle, FRAME_EXTERNAL_MENU_BAR(f));
442 pt.x = rect.left;
443 pt.y = rect.top;
445 ClientToScreen (FRAME_W32_WINDOW(f), &pt);
447 /* Remember x_pixels_diff and y_pixels_diff. */
448 f->x_pixels_diff = pt.x - rect.left;
449 f->y_pixels_diff = pt.y - rect.top;
451 *xptr = pt.x;
452 *yptr = pt.y;
457 DEFUN ("w32-define-rgb-color", Fw32_define_rgb_color,
458 Sw32_define_rgb_color, 4, 4, 0,
459 doc: /* Convert RGB numbers to a windows color reference and associate with NAME.
460 This adds or updates a named color to w32-color-map, making it
461 available for use. The original entry's RGB ref is returned, or nil
462 if the entry is new. */)
463 (red, green, blue, name)
464 Lisp_Object red, green, blue, name;
466 Lisp_Object rgb;
467 Lisp_Object oldrgb = Qnil;
468 Lisp_Object entry;
470 CHECK_NUMBER (red);
471 CHECK_NUMBER (green);
472 CHECK_NUMBER (blue);
473 CHECK_STRING (name);
475 XSETINT (rgb, RGB(XUINT (red), XUINT (green), XUINT (blue)));
477 BLOCK_INPUT;
479 /* replace existing entry in w32-color-map or add new entry. */
480 entry = Fassoc (name, Vw32_color_map);
481 if (NILP (entry))
483 entry = Fcons (name, rgb);
484 Vw32_color_map = Fcons (entry, Vw32_color_map);
486 else
488 oldrgb = Fcdr (entry);
489 Fsetcdr (entry, rgb);
492 UNBLOCK_INPUT;
494 return (oldrgb);
497 DEFUN ("w32-load-color-file", Fw32_load_color_file,
498 Sw32_load_color_file, 1, 1, 0,
499 doc: /* Create an alist of color entries from an external file.
500 Assign this value to w32-color-map to replace the existing color map.
502 The file should define one named RGB color per line like so:
503 R G B name
504 where R,G,B are numbers between 0 and 255 and name is an arbitrary string. */)
505 (filename)
506 Lisp_Object filename;
508 FILE *fp;
509 Lisp_Object cmap = Qnil;
510 Lisp_Object abspath;
512 CHECK_STRING (filename);
513 abspath = Fexpand_file_name (filename, Qnil);
515 fp = fopen (SDATA (filename), "rt");
516 if (fp)
518 char buf[512];
519 int red, green, blue;
520 int num;
522 BLOCK_INPUT;
524 while (fgets (buf, sizeof (buf), fp) != NULL) {
525 if (sscanf (buf, "%u %u %u %n", &red, &green, &blue, &num) == 3)
527 char *name = buf + num;
528 num = strlen (name) - 1;
529 if (name[num] == '\n')
530 name[num] = 0;
531 cmap = Fcons (Fcons (build_string (name),
532 make_number (RGB (red, green, blue))),
533 cmap);
536 fclose (fp);
538 UNBLOCK_INPUT;
541 return cmap;
544 /* The default colors for the w32 color map */
545 typedef struct colormap_t
547 char *name;
548 COLORREF colorref;
549 } colormap_t;
551 colormap_t w32_color_map[] =
553 {"snow" , PALETTERGB (255,250,250)},
554 {"ghost white" , PALETTERGB (248,248,255)},
555 {"GhostWhite" , PALETTERGB (248,248,255)},
556 {"white smoke" , PALETTERGB (245,245,245)},
557 {"WhiteSmoke" , PALETTERGB (245,245,245)},
558 {"gainsboro" , PALETTERGB (220,220,220)},
559 {"floral white" , PALETTERGB (255,250,240)},
560 {"FloralWhite" , PALETTERGB (255,250,240)},
561 {"old lace" , PALETTERGB (253,245,230)},
562 {"OldLace" , PALETTERGB (253,245,230)},
563 {"linen" , PALETTERGB (250,240,230)},
564 {"antique white" , PALETTERGB (250,235,215)},
565 {"AntiqueWhite" , PALETTERGB (250,235,215)},
566 {"papaya whip" , PALETTERGB (255,239,213)},
567 {"PapayaWhip" , PALETTERGB (255,239,213)},
568 {"blanched almond" , PALETTERGB (255,235,205)},
569 {"BlanchedAlmond" , PALETTERGB (255,235,205)},
570 {"bisque" , PALETTERGB (255,228,196)},
571 {"peach puff" , PALETTERGB (255,218,185)},
572 {"PeachPuff" , PALETTERGB (255,218,185)},
573 {"navajo white" , PALETTERGB (255,222,173)},
574 {"NavajoWhite" , PALETTERGB (255,222,173)},
575 {"moccasin" , PALETTERGB (255,228,181)},
576 {"cornsilk" , PALETTERGB (255,248,220)},
577 {"ivory" , PALETTERGB (255,255,240)},
578 {"lemon chiffon" , PALETTERGB (255,250,205)},
579 {"LemonChiffon" , PALETTERGB (255,250,205)},
580 {"seashell" , PALETTERGB (255,245,238)},
581 {"honeydew" , PALETTERGB (240,255,240)},
582 {"mint cream" , PALETTERGB (245,255,250)},
583 {"MintCream" , PALETTERGB (245,255,250)},
584 {"azure" , PALETTERGB (240,255,255)},
585 {"alice blue" , PALETTERGB (240,248,255)},
586 {"AliceBlue" , PALETTERGB (240,248,255)},
587 {"lavender" , PALETTERGB (230,230,250)},
588 {"lavender blush" , PALETTERGB (255,240,245)},
589 {"LavenderBlush" , PALETTERGB (255,240,245)},
590 {"misty rose" , PALETTERGB (255,228,225)},
591 {"MistyRose" , PALETTERGB (255,228,225)},
592 {"white" , PALETTERGB (255,255,255)},
593 {"black" , PALETTERGB ( 0, 0, 0)},
594 {"dark slate gray" , PALETTERGB ( 47, 79, 79)},
595 {"DarkSlateGray" , PALETTERGB ( 47, 79, 79)},
596 {"dark slate grey" , PALETTERGB ( 47, 79, 79)},
597 {"DarkSlateGrey" , PALETTERGB ( 47, 79, 79)},
598 {"dim gray" , PALETTERGB (105,105,105)},
599 {"DimGray" , PALETTERGB (105,105,105)},
600 {"dim grey" , PALETTERGB (105,105,105)},
601 {"DimGrey" , PALETTERGB (105,105,105)},
602 {"slate gray" , PALETTERGB (112,128,144)},
603 {"SlateGray" , PALETTERGB (112,128,144)},
604 {"slate grey" , PALETTERGB (112,128,144)},
605 {"SlateGrey" , PALETTERGB (112,128,144)},
606 {"light slate gray" , PALETTERGB (119,136,153)},
607 {"LightSlateGray" , PALETTERGB (119,136,153)},
608 {"light slate grey" , PALETTERGB (119,136,153)},
609 {"LightSlateGrey" , PALETTERGB (119,136,153)},
610 {"gray" , PALETTERGB (190,190,190)},
611 {"grey" , PALETTERGB (190,190,190)},
612 {"light grey" , PALETTERGB (211,211,211)},
613 {"LightGrey" , PALETTERGB (211,211,211)},
614 {"light gray" , PALETTERGB (211,211,211)},
615 {"LightGray" , PALETTERGB (211,211,211)},
616 {"midnight blue" , PALETTERGB ( 25, 25,112)},
617 {"MidnightBlue" , PALETTERGB ( 25, 25,112)},
618 {"navy" , PALETTERGB ( 0, 0,128)},
619 {"navy blue" , PALETTERGB ( 0, 0,128)},
620 {"NavyBlue" , PALETTERGB ( 0, 0,128)},
621 {"cornflower blue" , PALETTERGB (100,149,237)},
622 {"CornflowerBlue" , PALETTERGB (100,149,237)},
623 {"dark slate blue" , PALETTERGB ( 72, 61,139)},
624 {"DarkSlateBlue" , PALETTERGB ( 72, 61,139)},
625 {"slate blue" , PALETTERGB (106, 90,205)},
626 {"SlateBlue" , PALETTERGB (106, 90,205)},
627 {"medium slate blue" , PALETTERGB (123,104,238)},
628 {"MediumSlateBlue" , PALETTERGB (123,104,238)},
629 {"light slate blue" , PALETTERGB (132,112,255)},
630 {"LightSlateBlue" , PALETTERGB (132,112,255)},
631 {"medium blue" , PALETTERGB ( 0, 0,205)},
632 {"MediumBlue" , PALETTERGB ( 0, 0,205)},
633 {"royal blue" , PALETTERGB ( 65,105,225)},
634 {"RoyalBlue" , PALETTERGB ( 65,105,225)},
635 {"blue" , PALETTERGB ( 0, 0,255)},
636 {"dodger blue" , PALETTERGB ( 30,144,255)},
637 {"DodgerBlue" , PALETTERGB ( 30,144,255)},
638 {"deep sky blue" , PALETTERGB ( 0,191,255)},
639 {"DeepSkyBlue" , PALETTERGB ( 0,191,255)},
640 {"sky blue" , PALETTERGB (135,206,235)},
641 {"SkyBlue" , PALETTERGB (135,206,235)},
642 {"light sky blue" , PALETTERGB (135,206,250)},
643 {"LightSkyBlue" , PALETTERGB (135,206,250)},
644 {"steel blue" , PALETTERGB ( 70,130,180)},
645 {"SteelBlue" , PALETTERGB ( 70,130,180)},
646 {"light steel blue" , PALETTERGB (176,196,222)},
647 {"LightSteelBlue" , PALETTERGB (176,196,222)},
648 {"light blue" , PALETTERGB (173,216,230)},
649 {"LightBlue" , PALETTERGB (173,216,230)},
650 {"powder blue" , PALETTERGB (176,224,230)},
651 {"PowderBlue" , PALETTERGB (176,224,230)},
652 {"pale turquoise" , PALETTERGB (175,238,238)},
653 {"PaleTurquoise" , PALETTERGB (175,238,238)},
654 {"dark turquoise" , PALETTERGB ( 0,206,209)},
655 {"DarkTurquoise" , PALETTERGB ( 0,206,209)},
656 {"medium turquoise" , PALETTERGB ( 72,209,204)},
657 {"MediumTurquoise" , PALETTERGB ( 72,209,204)},
658 {"turquoise" , PALETTERGB ( 64,224,208)},
659 {"cyan" , PALETTERGB ( 0,255,255)},
660 {"light cyan" , PALETTERGB (224,255,255)},
661 {"LightCyan" , PALETTERGB (224,255,255)},
662 {"cadet blue" , PALETTERGB ( 95,158,160)},
663 {"CadetBlue" , PALETTERGB ( 95,158,160)},
664 {"medium aquamarine" , PALETTERGB (102,205,170)},
665 {"MediumAquamarine" , PALETTERGB (102,205,170)},
666 {"aquamarine" , PALETTERGB (127,255,212)},
667 {"dark green" , PALETTERGB ( 0,100, 0)},
668 {"DarkGreen" , PALETTERGB ( 0,100, 0)},
669 {"dark olive green" , PALETTERGB ( 85,107, 47)},
670 {"DarkOliveGreen" , PALETTERGB ( 85,107, 47)},
671 {"dark sea green" , PALETTERGB (143,188,143)},
672 {"DarkSeaGreen" , PALETTERGB (143,188,143)},
673 {"sea green" , PALETTERGB ( 46,139, 87)},
674 {"SeaGreen" , PALETTERGB ( 46,139, 87)},
675 {"medium sea green" , PALETTERGB ( 60,179,113)},
676 {"MediumSeaGreen" , PALETTERGB ( 60,179,113)},
677 {"light sea green" , PALETTERGB ( 32,178,170)},
678 {"LightSeaGreen" , PALETTERGB ( 32,178,170)},
679 {"pale green" , PALETTERGB (152,251,152)},
680 {"PaleGreen" , PALETTERGB (152,251,152)},
681 {"spring green" , PALETTERGB ( 0,255,127)},
682 {"SpringGreen" , PALETTERGB ( 0,255,127)},
683 {"lawn green" , PALETTERGB (124,252, 0)},
684 {"LawnGreen" , PALETTERGB (124,252, 0)},
685 {"green" , PALETTERGB ( 0,255, 0)},
686 {"chartreuse" , PALETTERGB (127,255, 0)},
687 {"medium spring green" , PALETTERGB ( 0,250,154)},
688 {"MediumSpringGreen" , PALETTERGB ( 0,250,154)},
689 {"green yellow" , PALETTERGB (173,255, 47)},
690 {"GreenYellow" , PALETTERGB (173,255, 47)},
691 {"lime green" , PALETTERGB ( 50,205, 50)},
692 {"LimeGreen" , PALETTERGB ( 50,205, 50)},
693 {"yellow green" , PALETTERGB (154,205, 50)},
694 {"YellowGreen" , PALETTERGB (154,205, 50)},
695 {"forest green" , PALETTERGB ( 34,139, 34)},
696 {"ForestGreen" , PALETTERGB ( 34,139, 34)},
697 {"olive drab" , PALETTERGB (107,142, 35)},
698 {"OliveDrab" , PALETTERGB (107,142, 35)},
699 {"dark khaki" , PALETTERGB (189,183,107)},
700 {"DarkKhaki" , PALETTERGB (189,183,107)},
701 {"khaki" , PALETTERGB (240,230,140)},
702 {"pale goldenrod" , PALETTERGB (238,232,170)},
703 {"PaleGoldenrod" , PALETTERGB (238,232,170)},
704 {"light goldenrod yellow" , PALETTERGB (250,250,210)},
705 {"LightGoldenrodYellow" , PALETTERGB (250,250,210)},
706 {"light yellow" , PALETTERGB (255,255,224)},
707 {"LightYellow" , PALETTERGB (255,255,224)},
708 {"yellow" , PALETTERGB (255,255, 0)},
709 {"gold" , PALETTERGB (255,215, 0)},
710 {"light goldenrod" , PALETTERGB (238,221,130)},
711 {"LightGoldenrod" , PALETTERGB (238,221,130)},
712 {"goldenrod" , PALETTERGB (218,165, 32)},
713 {"dark goldenrod" , PALETTERGB (184,134, 11)},
714 {"DarkGoldenrod" , PALETTERGB (184,134, 11)},
715 {"rosy brown" , PALETTERGB (188,143,143)},
716 {"RosyBrown" , PALETTERGB (188,143,143)},
717 {"indian red" , PALETTERGB (205, 92, 92)},
718 {"IndianRed" , PALETTERGB (205, 92, 92)},
719 {"saddle brown" , PALETTERGB (139, 69, 19)},
720 {"SaddleBrown" , PALETTERGB (139, 69, 19)},
721 {"sienna" , PALETTERGB (160, 82, 45)},
722 {"peru" , PALETTERGB (205,133, 63)},
723 {"burlywood" , PALETTERGB (222,184,135)},
724 {"beige" , PALETTERGB (245,245,220)},
725 {"wheat" , PALETTERGB (245,222,179)},
726 {"sandy brown" , PALETTERGB (244,164, 96)},
727 {"SandyBrown" , PALETTERGB (244,164, 96)},
728 {"tan" , PALETTERGB (210,180,140)},
729 {"chocolate" , PALETTERGB (210,105, 30)},
730 {"firebrick" , PALETTERGB (178,34, 34)},
731 {"brown" , PALETTERGB (165,42, 42)},
732 {"dark salmon" , PALETTERGB (233,150,122)},
733 {"DarkSalmon" , PALETTERGB (233,150,122)},
734 {"salmon" , PALETTERGB (250,128,114)},
735 {"light salmon" , PALETTERGB (255,160,122)},
736 {"LightSalmon" , PALETTERGB (255,160,122)},
737 {"orange" , PALETTERGB (255,165, 0)},
738 {"dark orange" , PALETTERGB (255,140, 0)},
739 {"DarkOrange" , PALETTERGB (255,140, 0)},
740 {"coral" , PALETTERGB (255,127, 80)},
741 {"light coral" , PALETTERGB (240,128,128)},
742 {"LightCoral" , PALETTERGB (240,128,128)},
743 {"tomato" , PALETTERGB (255, 99, 71)},
744 {"orange red" , PALETTERGB (255, 69, 0)},
745 {"OrangeRed" , PALETTERGB (255, 69, 0)},
746 {"red" , PALETTERGB (255, 0, 0)},
747 {"hot pink" , PALETTERGB (255,105,180)},
748 {"HotPink" , PALETTERGB (255,105,180)},
749 {"deep pink" , PALETTERGB (255, 20,147)},
750 {"DeepPink" , PALETTERGB (255, 20,147)},
751 {"pink" , PALETTERGB (255,192,203)},
752 {"light pink" , PALETTERGB (255,182,193)},
753 {"LightPink" , PALETTERGB (255,182,193)},
754 {"pale violet red" , PALETTERGB (219,112,147)},
755 {"PaleVioletRed" , PALETTERGB (219,112,147)},
756 {"maroon" , PALETTERGB (176, 48, 96)},
757 {"medium violet red" , PALETTERGB (199, 21,133)},
758 {"MediumVioletRed" , PALETTERGB (199, 21,133)},
759 {"violet red" , PALETTERGB (208, 32,144)},
760 {"VioletRed" , PALETTERGB (208, 32,144)},
761 {"magenta" , PALETTERGB (255, 0,255)},
762 {"violet" , PALETTERGB (238,130,238)},
763 {"plum" , PALETTERGB (221,160,221)},
764 {"orchid" , PALETTERGB (218,112,214)},
765 {"medium orchid" , PALETTERGB (186, 85,211)},
766 {"MediumOrchid" , PALETTERGB (186, 85,211)},
767 {"dark orchid" , PALETTERGB (153, 50,204)},
768 {"DarkOrchid" , PALETTERGB (153, 50,204)},
769 {"dark violet" , PALETTERGB (148, 0,211)},
770 {"DarkViolet" , PALETTERGB (148, 0,211)},
771 {"blue violet" , PALETTERGB (138, 43,226)},
772 {"BlueViolet" , PALETTERGB (138, 43,226)},
773 {"purple" , PALETTERGB (160, 32,240)},
774 {"medium purple" , PALETTERGB (147,112,219)},
775 {"MediumPurple" , PALETTERGB (147,112,219)},
776 {"thistle" , PALETTERGB (216,191,216)},
777 {"gray0" , PALETTERGB ( 0, 0, 0)},
778 {"grey0" , PALETTERGB ( 0, 0, 0)},
779 {"dark grey" , PALETTERGB (169,169,169)},
780 {"DarkGrey" , PALETTERGB (169,169,169)},
781 {"dark gray" , PALETTERGB (169,169,169)},
782 {"DarkGray" , PALETTERGB (169,169,169)},
783 {"dark blue" , PALETTERGB ( 0, 0,139)},
784 {"DarkBlue" , PALETTERGB ( 0, 0,139)},
785 {"dark cyan" , PALETTERGB ( 0,139,139)},
786 {"DarkCyan" , PALETTERGB ( 0,139,139)},
787 {"dark magenta" , PALETTERGB (139, 0,139)},
788 {"DarkMagenta" , PALETTERGB (139, 0,139)},
789 {"dark red" , PALETTERGB (139, 0, 0)},
790 {"DarkRed" , PALETTERGB (139, 0, 0)},
791 {"light green" , PALETTERGB (144,238,144)},
792 {"LightGreen" , PALETTERGB (144,238,144)},
795 DEFUN ("w32-default-color-map", Fw32_default_color_map, Sw32_default_color_map,
796 0, 0, 0, doc: /* Return the default color map. */)
799 int i;
800 colormap_t *pc = w32_color_map;
801 Lisp_Object cmap;
803 BLOCK_INPUT;
805 cmap = Qnil;
807 for (i = 0; i < sizeof (w32_color_map) / sizeof (w32_color_map[0]);
808 pc++, i++)
809 cmap = Fcons (Fcons (build_string (pc->name),
810 make_number (pc->colorref)),
811 cmap);
813 UNBLOCK_INPUT;
815 return (cmap);
818 Lisp_Object
819 w32_to_x_color (rgb)
820 Lisp_Object rgb;
822 Lisp_Object color;
824 CHECK_NUMBER (rgb);
826 BLOCK_INPUT;
828 color = Frassq (rgb, Vw32_color_map);
830 UNBLOCK_INPUT;
832 if (!NILP (color))
833 return (Fcar (color));
834 else
835 return Qnil;
838 static Lisp_Object
839 w32_color_map_lookup (colorname)
840 char *colorname;
842 Lisp_Object tail, ret = Qnil;
844 BLOCK_INPUT;
846 for (tail = Vw32_color_map; !NILP (tail); tail = Fcdr (tail))
848 register Lisp_Object elt, tem;
850 elt = Fcar (tail);
851 if (!CONSP (elt)) continue;
853 tem = Fcar (elt);
855 if (lstrcmpi (SDATA (tem), colorname) == 0)
857 ret = Fcdr (elt);
858 break;
861 QUIT;
865 UNBLOCK_INPUT;
867 return ret;
871 static void
872 add_system_logical_colors_to_map (system_colors)
873 Lisp_Object *system_colors;
875 HKEY colors_key;
877 /* Other registry operations are done with input blocked. */
878 BLOCK_INPUT;
880 /* Look for "Control Panel/Colors" under User and Machine registry
881 settings. */
882 if (RegOpenKeyEx (HKEY_CURRENT_USER, "Control Panel\\Colors", 0,
883 KEY_READ, &colors_key) == ERROR_SUCCESS
884 || RegOpenKeyEx (HKEY_LOCAL_MACHINE, "Control Panel\\Colors", 0,
885 KEY_READ, &colors_key) == ERROR_SUCCESS)
887 /* List all keys. */
888 char color_buffer[64];
889 char full_name_buffer[MAX_PATH + SYSTEM_COLOR_PREFIX_LEN];
890 int index = 0;
891 DWORD name_size, color_size;
892 char *name_buffer = full_name_buffer + SYSTEM_COLOR_PREFIX_LEN;
894 name_size = sizeof (full_name_buffer) - SYSTEM_COLOR_PREFIX_LEN;
895 color_size = sizeof (color_buffer);
897 strcpy (full_name_buffer, SYSTEM_COLOR_PREFIX);
899 while (RegEnumValueA (colors_key, index, name_buffer, &name_size,
900 NULL, NULL, color_buffer, &color_size)
901 == ERROR_SUCCESS)
903 int r, g, b;
904 if (sscanf (color_buffer, " %u %u %u", &r, &g, &b) == 3)
905 *system_colors = Fcons (Fcons (build_string (full_name_buffer),
906 make_number (RGB (r, g, b))),
907 *system_colors);
909 name_size = sizeof (full_name_buffer) - SYSTEM_COLOR_PREFIX_LEN;
910 color_size = sizeof (color_buffer);
911 index++;
913 RegCloseKey (colors_key);
916 UNBLOCK_INPUT;
920 static Lisp_Object
921 x_to_w32_color (colorname)
922 char * colorname;
924 register Lisp_Object ret = Qnil;
926 BLOCK_INPUT;
928 if (colorname[0] == '#')
930 /* Could be an old-style RGB Device specification. */
931 char *color;
932 int size;
933 color = colorname + 1;
935 size = strlen(color);
936 if (size == 3 || size == 6 || size == 9 || size == 12)
938 UINT colorval;
939 int i, pos;
940 pos = 0;
941 size /= 3;
942 colorval = 0;
944 for (i = 0; i < 3; i++)
946 char *end;
947 char t;
948 unsigned long value;
950 /* The check for 'x' in the following conditional takes into
951 account the fact that strtol allows a "0x" in front of
952 our numbers, and we don't. */
953 if (!isxdigit(color[0]) || color[1] == 'x')
954 break;
955 t = color[size];
956 color[size] = '\0';
957 value = strtoul(color, &end, 16);
958 color[size] = t;
959 if (errno == ERANGE || end - color != size)
960 break;
961 switch (size)
963 case 1:
964 value = value * 0x10;
965 break;
966 case 2:
967 break;
968 case 3:
969 value /= 0x10;
970 break;
971 case 4:
972 value /= 0x100;
973 break;
975 colorval |= (value << pos);
976 pos += 0x8;
977 if (i == 2)
979 UNBLOCK_INPUT;
980 XSETINT (ret, colorval);
981 return ret;
983 color = end;
987 else if (strnicmp(colorname, "rgb:", 4) == 0)
989 char *color;
990 UINT colorval;
991 int i, pos;
992 pos = 0;
994 colorval = 0;
995 color = colorname + 4;
996 for (i = 0; i < 3; i++)
998 char *end;
999 unsigned long value;
1001 /* The check for 'x' in the following conditional takes into
1002 account the fact that strtol allows a "0x" in front of
1003 our numbers, and we don't. */
1004 if (!isxdigit(color[0]) || color[1] == 'x')
1005 break;
1006 value = strtoul(color, &end, 16);
1007 if (errno == ERANGE)
1008 break;
1009 switch (end - color)
1011 case 1:
1012 value = value * 0x10 + value;
1013 break;
1014 case 2:
1015 break;
1016 case 3:
1017 value /= 0x10;
1018 break;
1019 case 4:
1020 value /= 0x100;
1021 break;
1022 default:
1023 value = ULONG_MAX;
1025 if (value == ULONG_MAX)
1026 break;
1027 colorval |= (value << pos);
1028 pos += 0x8;
1029 if (i == 2)
1031 if (*end != '\0')
1032 break;
1033 UNBLOCK_INPUT;
1034 XSETINT (ret, colorval);
1035 return ret;
1037 if (*end != '/')
1038 break;
1039 color = end + 1;
1042 else if (strnicmp(colorname, "rgbi:", 5) == 0)
1044 /* This is an RGB Intensity specification. */
1045 char *color;
1046 UINT colorval;
1047 int i, pos;
1048 pos = 0;
1050 colorval = 0;
1051 color = colorname + 5;
1052 for (i = 0; i < 3; i++)
1054 char *end;
1055 double value;
1056 UINT val;
1058 value = strtod(color, &end);
1059 if (errno == ERANGE)
1060 break;
1061 if (value < 0.0 || value > 1.0)
1062 break;
1063 val = (UINT)(0x100 * value);
1064 /* We used 0x100 instead of 0xFF to give a continuous
1065 range between 0.0 and 1.0 inclusive. The next statement
1066 fixes the 1.0 case. */
1067 if (val == 0x100)
1068 val = 0xFF;
1069 colorval |= (val << pos);
1070 pos += 0x8;
1071 if (i == 2)
1073 if (*end != '\0')
1074 break;
1075 UNBLOCK_INPUT;
1076 XSETINT (ret, colorval);
1077 return ret;
1079 if (*end != '/')
1080 break;
1081 color = end + 1;
1084 /* I am not going to attempt to handle any of the CIE color schemes
1085 or TekHVC, since I don't know the algorithms for conversion to
1086 RGB. */
1088 /* If we fail to lookup the color name in w32_color_map, then check the
1089 colorname to see if it can be crudely approximated: If the X color
1090 ends in a number (e.g., "darkseagreen2"), strip the number and
1091 return the result of looking up the base color name. */
1092 ret = w32_color_map_lookup (colorname);
1093 if (NILP (ret))
1095 int len = strlen (colorname);
1097 if (isdigit (colorname[len - 1]))
1099 char *ptr, *approx = alloca (len + 1);
1101 strcpy (approx, colorname);
1102 ptr = &approx[len - 1];
1103 while (ptr > approx && isdigit (*ptr))
1104 *ptr-- = '\0';
1106 ret = w32_color_map_lookup (approx);
1110 UNBLOCK_INPUT;
1111 return ret;
1114 void
1115 w32_regenerate_palette (FRAME_PTR f)
1117 struct w32_palette_entry * list;
1118 LOGPALETTE * log_palette;
1119 HPALETTE new_palette;
1120 int i;
1122 /* don't bother trying to create palette if not supported */
1123 if (! FRAME_W32_DISPLAY_INFO (f)->has_palette)
1124 return;
1126 log_palette = (LOGPALETTE *)
1127 alloca (sizeof (LOGPALETTE) +
1128 FRAME_W32_DISPLAY_INFO (f)->num_colors * sizeof (PALETTEENTRY));
1129 log_palette->palVersion = 0x300;
1130 log_palette->palNumEntries = FRAME_W32_DISPLAY_INFO (f)->num_colors;
1132 list = FRAME_W32_DISPLAY_INFO (f)->color_list;
1133 for (i = 0;
1134 i < FRAME_W32_DISPLAY_INFO (f)->num_colors;
1135 i++, list = list->next)
1136 log_palette->palPalEntry[i] = list->entry;
1138 new_palette = CreatePalette (log_palette);
1140 enter_crit ();
1142 if (FRAME_W32_DISPLAY_INFO (f)->palette)
1143 DeleteObject (FRAME_W32_DISPLAY_INFO (f)->palette);
1144 FRAME_W32_DISPLAY_INFO (f)->palette = new_palette;
1146 /* Realize display palette and garbage all frames. */
1147 release_frame_dc (f, get_frame_dc (f));
1149 leave_crit ();
1152 #define W32_COLOR(pe) RGB (pe.peRed, pe.peGreen, pe.peBlue)
1153 #define SET_W32_COLOR(pe, color) \
1154 do \
1156 pe.peRed = GetRValue (color); \
1157 pe.peGreen = GetGValue (color); \
1158 pe.peBlue = GetBValue (color); \
1159 pe.peFlags = 0; \
1160 } while (0)
1162 #if 0
1163 /* Keep these around in case we ever want to track color usage. */
1164 void
1165 w32_map_color (FRAME_PTR f, COLORREF color)
1167 struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
1169 if (NILP (Vw32_enable_palette))
1170 return;
1172 /* check if color is already mapped */
1173 while (list)
1175 if (W32_COLOR (list->entry) == color)
1177 ++list->refcount;
1178 return;
1180 list = list->next;
1183 /* not already mapped, so add to list and recreate Windows palette */
1184 list = (struct w32_palette_entry *)
1185 xmalloc (sizeof (struct w32_palette_entry));
1186 SET_W32_COLOR (list->entry, color);
1187 list->refcount = 1;
1188 list->next = FRAME_W32_DISPLAY_INFO (f)->color_list;
1189 FRAME_W32_DISPLAY_INFO (f)->color_list = list;
1190 FRAME_W32_DISPLAY_INFO (f)->num_colors++;
1192 /* set flag that palette must be regenerated */
1193 FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
1196 void
1197 w32_unmap_color (FRAME_PTR f, COLORREF color)
1199 struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
1200 struct w32_palette_entry **prev = &FRAME_W32_DISPLAY_INFO (f)->color_list;
1202 if (NILP (Vw32_enable_palette))
1203 return;
1205 /* check if color is already mapped */
1206 while (list)
1208 if (W32_COLOR (list->entry) == color)
1210 if (--list->refcount == 0)
1212 *prev = list->next;
1213 xfree (list);
1214 FRAME_W32_DISPLAY_INFO (f)->num_colors--;
1215 break;
1217 else
1218 return;
1220 prev = &list->next;
1221 list = list->next;
1224 /* set flag that palette must be regenerated */
1225 FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
1227 #endif
1230 /* Gamma-correct COLOR on frame F. */
1232 void
1233 gamma_correct (f, color)
1234 struct frame *f;
1235 COLORREF *color;
1237 if (f->gamma)
1239 *color = PALETTERGB (
1240 pow (GetRValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
1241 pow (GetGValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
1242 pow (GetBValue (*color) / 255.0, f->gamma) * 255.0 + 0.5);
1247 /* Decide if color named COLOR is valid for the display associated with
1248 the selected frame; if so, return the rgb values in COLOR_DEF.
1249 If ALLOC is nonzero, allocate a new colormap cell. */
1252 w32_defined_color (f, color, color_def, alloc)
1253 FRAME_PTR f;
1254 char *color;
1255 XColor *color_def;
1256 int alloc;
1258 register Lisp_Object tem;
1259 COLORREF w32_color_ref;
1261 tem = x_to_w32_color (color);
1263 if (!NILP (tem))
1265 if (f)
1267 /* Apply gamma correction. */
1268 w32_color_ref = XUINT (tem);
1269 gamma_correct (f, &w32_color_ref);
1270 XSETINT (tem, w32_color_ref);
1273 /* Map this color to the palette if it is enabled. */
1274 if (!NILP (Vw32_enable_palette))
1276 struct w32_palette_entry * entry =
1277 one_w32_display_info.color_list;
1278 struct w32_palette_entry ** prev =
1279 &one_w32_display_info.color_list;
1281 /* check if color is already mapped */
1282 while (entry)
1284 if (W32_COLOR (entry->entry) == XUINT (tem))
1285 break;
1286 prev = &entry->next;
1287 entry = entry->next;
1290 if (entry == NULL && alloc)
1292 /* not already mapped, so add to list */
1293 entry = (struct w32_palette_entry *)
1294 xmalloc (sizeof (struct w32_palette_entry));
1295 SET_W32_COLOR (entry->entry, XUINT (tem));
1296 entry->next = NULL;
1297 *prev = entry;
1298 one_w32_display_info.num_colors++;
1300 /* set flag that palette must be regenerated */
1301 one_w32_display_info.regen_palette = TRUE;
1304 /* Ensure COLORREF value is snapped to nearest color in (default)
1305 palette by simulating the PALETTERGB macro. This works whether
1306 or not the display device has a palette. */
1307 w32_color_ref = XUINT (tem) | 0x2000000;
1309 color_def->pixel = w32_color_ref;
1310 color_def->red = GetRValue (w32_color_ref) * 256;
1311 color_def->green = GetGValue (w32_color_ref) * 256;
1312 color_def->blue = GetBValue (w32_color_ref) * 256;
1314 return 1;
1316 else
1318 return 0;
1322 /* Given a string ARG naming a color, compute a pixel value from it
1323 suitable for screen F.
1324 If F is not a color screen, return DEF (default) regardless of what
1325 ARG says. */
1328 x_decode_color (f, arg, def)
1329 FRAME_PTR f;
1330 Lisp_Object arg;
1331 int def;
1333 XColor cdef;
1335 CHECK_STRING (arg);
1337 if (strcmp (SDATA (arg), "black") == 0)
1338 return BLACK_PIX_DEFAULT (f);
1339 else if (strcmp (SDATA (arg), "white") == 0)
1340 return WHITE_PIX_DEFAULT (f);
1342 if ((FRAME_W32_DISPLAY_INFO (f)->n_planes * FRAME_W32_DISPLAY_INFO (f)->n_cbits) == 1)
1343 return def;
1345 /* w32_defined_color is responsible for coping with failures
1346 by looking for a near-miss. */
1347 if (w32_defined_color (f, SDATA (arg), &cdef, 1))
1348 return cdef.pixel;
1350 /* defined_color failed; return an ultimate default. */
1351 return def;
1356 /* Functions called only from `x_set_frame_param'
1357 to set individual parameters.
1359 If FRAME_W32_WINDOW (f) is 0,
1360 the frame is being created and its window does not exist yet.
1361 In that case, just record the parameter's new value
1362 in the standard place; do not attempt to change the window. */
1364 void
1365 x_set_foreground_color (f, arg, oldval)
1366 struct frame *f;
1367 Lisp_Object arg, oldval;
1369 struct w32_output *x = f->output_data.w32;
1370 PIX_TYPE fg, old_fg;
1372 fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1373 old_fg = FRAME_FOREGROUND_PIXEL (f);
1374 FRAME_FOREGROUND_PIXEL (f) = fg;
1376 if (FRAME_W32_WINDOW (f) != 0)
1378 if (x->cursor_pixel == old_fg)
1379 x->cursor_pixel = fg;
1381 update_face_from_frame_parameter (f, Qforeground_color, arg);
1382 if (FRAME_VISIBLE_P (f))
1383 redraw_frame (f);
1387 void
1388 x_set_background_color (f, arg, oldval)
1389 struct frame *f;
1390 Lisp_Object arg, oldval;
1392 FRAME_BACKGROUND_PIXEL (f)
1393 = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
1395 if (FRAME_W32_WINDOW (f) != 0)
1397 SetWindowLong (FRAME_W32_WINDOW (f), WND_BACKGROUND_INDEX,
1398 FRAME_BACKGROUND_PIXEL (f));
1400 update_face_from_frame_parameter (f, Qbackground_color, arg);
1402 if (FRAME_VISIBLE_P (f))
1403 redraw_frame (f);
1407 void
1408 x_set_mouse_color (f, arg, oldval)
1409 struct frame *f;
1410 Lisp_Object arg, oldval;
1412 Cursor cursor, nontext_cursor, mode_cursor, hand_cursor;
1413 int count;
1414 int mask_color;
1416 if (!EQ (Qnil, arg))
1417 f->output_data.w32->mouse_pixel
1418 = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1419 mask_color = FRAME_BACKGROUND_PIXEL (f);
1421 /* Don't let pointers be invisible. */
1422 if (mask_color == f->output_data.w32->mouse_pixel
1423 && mask_color == FRAME_BACKGROUND_PIXEL (f))
1424 f->output_data.w32->mouse_pixel = FRAME_FOREGROUND_PIXEL (f);
1426 #if 0 /* TODO : cursor changes */
1427 BLOCK_INPUT;
1429 /* It's not okay to crash if the user selects a screwy cursor. */
1430 count = x_catch_errors (FRAME_W32_DISPLAY (f));
1432 if (!EQ (Qnil, Vx_pointer_shape))
1434 CHECK_NUMBER (Vx_pointer_shape);
1435 cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XINT (Vx_pointer_shape));
1437 else
1438 cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
1439 x_check_errors (FRAME_W32_DISPLAY (f), "bad text pointer cursor: %s");
1441 if (!EQ (Qnil, Vx_nontext_pointer_shape))
1443 CHECK_NUMBER (Vx_nontext_pointer_shape);
1444 nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1445 XINT (Vx_nontext_pointer_shape));
1447 else
1448 nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_left_ptr);
1449 x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
1451 if (!EQ (Qnil, Vx_hourglass_pointer_shape))
1453 CHECK_NUMBER (Vx_hourglass_pointer_shape);
1454 hourglass_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1455 XINT (Vx_hourglass_pointer_shape));
1457 else
1458 hourglass_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_watch);
1459 x_check_errors (FRAME_W32_DISPLAY (f), "bad busy pointer cursor: %s");
1461 x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
1462 if (!EQ (Qnil, Vx_mode_pointer_shape))
1464 CHECK_NUMBER (Vx_mode_pointer_shape);
1465 mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1466 XINT (Vx_mode_pointer_shape));
1468 else
1469 mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
1470 x_check_errors (FRAME_W32_DISPLAY (f), "bad modeline pointer cursor: %s");
1472 if (!EQ (Qnil, Vx_sensitive_text_pointer_shape))
1474 CHECK_NUMBER (Vx_sensitive_text_pointer_shape);
1475 hand_cursor
1476 = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1477 XINT (Vx_sensitive_text_pointer_shape));
1479 else
1480 hand_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_crosshair);
1482 if (!NILP (Vx_window_horizontal_drag_shape))
1484 CHECK_NUMBER (Vx_window_horizontal_drag_shape);
1485 horizontal_drag_cursor
1486 = XCreateFontCursor (FRAME_X_DISPLAY (f),
1487 XINT (Vx_window_horizontal_drag_shape));
1489 else
1490 horizontal_drag_cursor
1491 = XCreateFontCursor (FRAME_X_DISPLAY (f), XC_sb_h_double_arrow);
1493 /* Check and report errors with the above calls. */
1494 x_check_errors (FRAME_W32_DISPLAY (f), "can't set cursor shape: %s");
1495 x_uncatch_errors (FRAME_W32_DISPLAY (f), count);
1498 XColor fore_color, back_color;
1500 fore_color.pixel = f->output_data.w32->mouse_pixel;
1501 back_color.pixel = mask_color;
1502 XQueryColor (FRAME_W32_DISPLAY (f),
1503 DefaultColormap (FRAME_W32_DISPLAY (f),
1504 DefaultScreen (FRAME_W32_DISPLAY (f))),
1505 &fore_color);
1506 XQueryColor (FRAME_W32_DISPLAY (f),
1507 DefaultColormap (FRAME_W32_DISPLAY (f),
1508 DefaultScreen (FRAME_W32_DISPLAY (f))),
1509 &back_color);
1510 XRecolorCursor (FRAME_W32_DISPLAY (f), cursor,
1511 &fore_color, &back_color);
1512 XRecolorCursor (FRAME_W32_DISPLAY (f), nontext_cursor,
1513 &fore_color, &back_color);
1514 XRecolorCursor (FRAME_W32_DISPLAY (f), mode_cursor,
1515 &fore_color, &back_color);
1516 XRecolorCursor (FRAME_W32_DISPLAY (f), hand_cursor,
1517 &fore_color, &back_color);
1518 XRecolorCursor (FRAME_W32_DISPLAY (f), hourglass_cursor,
1519 &fore_color, &back_color);
1522 if (FRAME_W32_WINDOW (f) != 0)
1523 XDefineCursor (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), cursor);
1525 if (cursor != f->output_data.w32->text_cursor && f->output_data.w32->text_cursor != 0)
1526 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->text_cursor);
1527 f->output_data.w32->text_cursor = cursor;
1529 if (nontext_cursor != f->output_data.w32->nontext_cursor
1530 && f->output_data.w32->nontext_cursor != 0)
1531 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->nontext_cursor);
1532 f->output_data.w32->nontext_cursor = nontext_cursor;
1534 if (hourglass_cursor != f->output_data.w32->hourglass_cursor
1535 && f->output_data.w32->hourglass_cursor != 0)
1536 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->hourglass_cursor);
1537 f->output_data.w32->hourglass_cursor = hourglass_cursor;
1539 if (mode_cursor != f->output_data.w32->modeline_cursor
1540 && f->output_data.w32->modeline_cursor != 0)
1541 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->modeline_cursor);
1542 f->output_data.w32->modeline_cursor = mode_cursor;
1544 if (hand_cursor != f->output_data.w32->hand_cursor
1545 && f->output_data.w32->hand_cursor != 0)
1546 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->hand_cursor);
1547 f->output_data.w32->hand_cursor = hand_cursor;
1549 XFlush (FRAME_W32_DISPLAY (f));
1550 UNBLOCK_INPUT;
1552 update_face_from_frame_parameter (f, Qmouse_color, arg);
1553 #endif /* TODO */
1556 /* Defined in w32term.c. */
1557 void
1558 x_set_cursor_color (f, arg, oldval)
1559 struct frame *f;
1560 Lisp_Object arg, oldval;
1562 unsigned long fore_pixel, pixel;
1564 if (!NILP (Vx_cursor_fore_pixel))
1565 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
1566 WHITE_PIX_DEFAULT (f));
1567 else
1568 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
1570 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1572 /* Make sure that the cursor color differs from the background color. */
1573 if (pixel == FRAME_BACKGROUND_PIXEL (f))
1575 pixel = f->output_data.w32->mouse_pixel;
1576 if (pixel == fore_pixel)
1577 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
1580 f->output_data.w32->cursor_foreground_pixel = fore_pixel;
1581 f->output_data.w32->cursor_pixel = pixel;
1583 if (FRAME_W32_WINDOW (f) != 0)
1585 BLOCK_INPUT;
1586 /* Update frame's cursor_gc. */
1587 f->output_data.w32->cursor_gc->foreground = fore_pixel;
1588 f->output_data.w32->cursor_gc->background = pixel;
1590 UNBLOCK_INPUT;
1592 if (FRAME_VISIBLE_P (f))
1594 x_update_cursor (f, 0);
1595 x_update_cursor (f, 1);
1599 update_face_from_frame_parameter (f, Qcursor_color, arg);
1602 /* Set the border-color of frame F to pixel value PIX.
1603 Note that this does not fully take effect if done before
1604 F has a window. */
1606 void
1607 x_set_border_pixel (f, pix)
1608 struct frame *f;
1609 int pix;
1612 f->output_data.w32->border_pixel = pix;
1614 if (FRAME_W32_WINDOW (f) != 0 && f->border_width > 0)
1616 if (FRAME_VISIBLE_P (f))
1617 redraw_frame (f);
1621 /* Set the border-color of frame F to value described by ARG.
1622 ARG can be a string naming a color.
1623 The border-color is used for the border that is drawn by the server.
1624 Note that this does not fully take effect if done before
1625 F has a window; it must be redone when the window is created. */
1627 void
1628 x_set_border_color (f, arg, oldval)
1629 struct frame *f;
1630 Lisp_Object arg, oldval;
1632 int pix;
1634 CHECK_STRING (arg);
1635 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1636 x_set_border_pixel (f, pix);
1637 update_face_from_frame_parameter (f, Qborder_color, arg);
1641 void
1642 x_set_cursor_type (f, arg, oldval)
1643 FRAME_PTR f;
1644 Lisp_Object arg, oldval;
1646 set_frame_cursor_types (f, arg);
1648 /* Make sure the cursor gets redrawn. */
1649 cursor_type_changed = 1;
1652 void
1653 x_set_icon_type (f, arg, oldval)
1654 struct frame *f;
1655 Lisp_Object arg, oldval;
1657 int result;
1659 if (NILP (arg) && NILP (oldval))
1660 return;
1662 if (STRINGP (arg) && STRINGP (oldval)
1663 && EQ (Fstring_equal (oldval, arg), Qt))
1664 return;
1666 if (SYMBOLP (arg) && SYMBOLP (oldval) && EQ (arg, oldval))
1667 return;
1669 BLOCK_INPUT;
1671 result = x_bitmap_icon (f, arg);
1672 if (result)
1674 UNBLOCK_INPUT;
1675 error ("No icon window available");
1678 UNBLOCK_INPUT;
1681 void
1682 x_set_icon_name (f, arg, oldval)
1683 struct frame *f;
1684 Lisp_Object arg, oldval;
1686 if (STRINGP (arg))
1688 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1689 return;
1691 else if (!NILP (arg) || NILP (oldval))
1692 return;
1694 f->icon_name = arg;
1696 #if 0
1697 if (f->output_data.w32->icon_bitmap != 0)
1698 return;
1700 BLOCK_INPUT;
1702 result = x_text_icon (f,
1703 (char *) SDATA ((!NILP (f->icon_name)
1704 ? f->icon_name
1705 : !NILP (f->title)
1706 ? f->title
1707 : f->name)));
1709 if (result)
1711 UNBLOCK_INPUT;
1712 error ("No icon window available");
1715 /* If the window was unmapped (and its icon was mapped),
1716 the new icon is not mapped, so map the window in its stead. */
1717 if (FRAME_VISIBLE_P (f))
1719 #ifdef USE_X_TOOLKIT
1720 XtPopup (f->output_data.w32->widget, XtGrabNone);
1721 #endif
1722 XMapWindow (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f));
1725 XFlush (FRAME_W32_DISPLAY (f));
1726 UNBLOCK_INPUT;
1727 #endif
1731 void
1732 x_set_menu_bar_lines (f, value, oldval)
1733 struct frame *f;
1734 Lisp_Object value, oldval;
1736 int nlines;
1737 int olines = FRAME_MENU_BAR_LINES (f);
1739 /* Right now, menu bars don't work properly in minibuf-only frames;
1740 most of the commands try to apply themselves to the minibuffer
1741 frame itself, and get an error because you can't switch buffers
1742 in or split the minibuffer window. */
1743 if (FRAME_MINIBUF_ONLY_P (f))
1744 return;
1746 if (INTEGERP (value))
1747 nlines = XINT (value);
1748 else
1749 nlines = 0;
1751 FRAME_MENU_BAR_LINES (f) = 0;
1752 if (nlines)
1753 FRAME_EXTERNAL_MENU_BAR (f) = 1;
1754 else
1756 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
1757 free_frame_menubar (f);
1758 FRAME_EXTERNAL_MENU_BAR (f) = 0;
1760 /* Adjust the frame size so that the client (text) dimensions
1761 remain the same. This depends on FRAME_EXTERNAL_MENU_BAR being
1762 set correctly. */
1763 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
1764 do_pending_window_change (0);
1766 adjust_glyphs (f);
1770 /* Set the number of lines used for the tool bar of frame F to VALUE.
1771 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
1772 is the old number of tool bar lines. This function changes the
1773 height of all windows on frame F to match the new tool bar height.
1774 The frame's height doesn't change. */
1776 void
1777 x_set_tool_bar_lines (f, value, oldval)
1778 struct frame *f;
1779 Lisp_Object value, oldval;
1781 int delta, nlines, root_height;
1782 Lisp_Object root_window;
1784 /* Treat tool bars like menu bars. */
1785 if (FRAME_MINIBUF_ONLY_P (f))
1786 return;
1788 /* Use VALUE only if an integer >= 0. */
1789 if (INTEGERP (value) && XINT (value) >= 0)
1790 nlines = XFASTINT (value);
1791 else
1792 nlines = 0;
1794 /* Make sure we redisplay all windows in this frame. */
1795 ++windows_or_buffers_changed;
1797 delta = nlines - FRAME_TOOL_BAR_LINES (f);
1799 /* Don't resize the tool-bar to more than we have room for. */
1800 root_window = FRAME_ROOT_WINDOW (f);
1801 root_height = WINDOW_TOTAL_LINES (XWINDOW (root_window));
1802 if (root_height - delta < 1)
1804 delta = root_height - 1;
1805 nlines = FRAME_TOOL_BAR_LINES (f) + delta;
1808 FRAME_TOOL_BAR_LINES (f) = nlines;
1809 change_window_heights (root_window, delta);
1810 adjust_glyphs (f);
1812 /* We also have to make sure that the internal border at the top of
1813 the frame, below the menu bar or tool bar, is redrawn when the
1814 tool bar disappears. This is so because the internal border is
1815 below the tool bar if one is displayed, but is below the menu bar
1816 if there isn't a tool bar. The tool bar draws into the area
1817 below the menu bar. */
1818 if (FRAME_W32_WINDOW (f) && FRAME_TOOL_BAR_LINES (f) == 0)
1820 updating_frame = f;
1821 clear_frame ();
1822 clear_current_matrices (f);
1823 updating_frame = NULL;
1826 /* If the tool bar gets smaller, the internal border below it
1827 has to be cleared. It was formerly part of the display
1828 of the larger tool bar, and updating windows won't clear it. */
1829 if (delta < 0)
1831 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
1832 int width = FRAME_PIXEL_WIDTH (f);
1833 int y = nlines * FRAME_LINE_HEIGHT (f);
1835 BLOCK_INPUT;
1837 HDC hdc = get_frame_dc (f);
1838 w32_clear_area (f, hdc, 0, y, width, height);
1839 release_frame_dc (f, hdc);
1841 UNBLOCK_INPUT;
1843 if (WINDOWP (f->tool_bar_window))
1844 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
1849 /* Change the name of frame F to NAME. If NAME is nil, set F's name to
1850 w32_id_name.
1852 If EXPLICIT is non-zero, that indicates that lisp code is setting the
1853 name; if NAME is a string, set F's name to NAME and set
1854 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1856 If EXPLICIT is zero, that indicates that Emacs redisplay code is
1857 suggesting a new name, which lisp code should override; if
1858 F->explicit_name is set, ignore the new name; otherwise, set it. */
1860 void
1861 x_set_name (f, name, explicit)
1862 struct frame *f;
1863 Lisp_Object name;
1864 int explicit;
1866 /* Make sure that requests from lisp code override requests from
1867 Emacs redisplay code. */
1868 if (explicit)
1870 /* If we're switching from explicit to implicit, we had better
1871 update the mode lines and thereby update the title. */
1872 if (f->explicit_name && NILP (name))
1873 update_mode_lines = 1;
1875 f->explicit_name = ! NILP (name);
1877 else if (f->explicit_name)
1878 return;
1880 /* If NAME is nil, set the name to the w32_id_name. */
1881 if (NILP (name))
1883 /* Check for no change needed in this very common case
1884 before we do any consing. */
1885 if (!strcmp (FRAME_W32_DISPLAY_INFO (f)->w32_id_name,
1886 SDATA (f->name)))
1887 return;
1888 name = build_string (FRAME_W32_DISPLAY_INFO (f)->w32_id_name);
1890 else
1891 CHECK_STRING (name);
1893 /* Don't change the name if it's already NAME. */
1894 if (! NILP (Fstring_equal (name, f->name)))
1895 return;
1897 f->name = name;
1899 /* For setting the frame title, the title parameter should override
1900 the name parameter. */
1901 if (! NILP (f->title))
1902 name = f->title;
1904 if (FRAME_W32_WINDOW (f))
1906 if (STRING_MULTIBYTE (name))
1907 name = ENCODE_SYSTEM (name);
1909 BLOCK_INPUT;
1910 SetWindowText(FRAME_W32_WINDOW (f), SDATA (name));
1911 UNBLOCK_INPUT;
1915 /* This function should be called when the user's lisp code has
1916 specified a name for the frame; the name will override any set by the
1917 redisplay code. */
1918 void
1919 x_explicitly_set_name (f, arg, oldval)
1920 FRAME_PTR f;
1921 Lisp_Object arg, oldval;
1923 x_set_name (f, arg, 1);
1926 /* This function should be called by Emacs redisplay code to set the
1927 name; names set this way will never override names set by the user's
1928 lisp code. */
1929 void
1930 x_implicitly_set_name (f, arg, oldval)
1931 FRAME_PTR f;
1932 Lisp_Object arg, oldval;
1934 x_set_name (f, arg, 0);
1937 /* Change the title of frame F to NAME.
1938 If NAME is nil, use the frame name as the title.
1940 If EXPLICIT is non-zero, that indicates that lisp code is setting the
1941 name; if NAME is a string, set F's name to NAME and set
1942 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1944 If EXPLICIT is zero, that indicates that Emacs redisplay code is
1945 suggesting a new name, which lisp code should override; if
1946 F->explicit_name is set, ignore the new name; otherwise, set it. */
1948 void
1949 x_set_title (f, name, old_name)
1950 struct frame *f;
1951 Lisp_Object name, old_name;
1953 /* Don't change the title if it's already NAME. */
1954 if (EQ (name, f->title))
1955 return;
1957 update_mode_lines = 1;
1959 f->title = name;
1961 if (NILP (name))
1962 name = f->name;
1964 if (FRAME_W32_WINDOW (f))
1966 if (STRING_MULTIBYTE (name))
1967 name = ENCODE_SYSTEM (name);
1969 BLOCK_INPUT;
1970 SetWindowText(FRAME_W32_WINDOW (f), SDATA (name));
1971 UNBLOCK_INPUT;
1976 void x_set_scroll_bar_default_width (f)
1977 struct frame *f;
1979 int wid = FRAME_COLUMN_WIDTH (f);
1981 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = GetSystemMetrics (SM_CXVSCROLL);
1982 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) +
1983 wid - 1) / wid;
1987 /* Subroutines of creating a frame. */
1990 /* Return the value of parameter PARAM.
1992 First search ALIST, then Vdefault_frame_alist, then the X defaults
1993 database, using ATTRIBUTE as the attribute name and CLASS as its class.
1995 Convert the resource to the type specified by desired_type.
1997 If no default is specified, return Qunbound. If you call
1998 w32_get_arg, make sure you deal with Qunbound in a reasonable way,
1999 and don't let it get stored in any Lisp-visible variables! */
2001 static Lisp_Object
2002 w32_get_arg (alist, param, attribute, class, type)
2003 Lisp_Object alist, param;
2004 char *attribute;
2005 char *class;
2006 enum resource_types type;
2008 return x_get_arg (check_x_display_info (Qnil),
2009 alist, param, attribute, class, type);
2013 Cursor
2014 w32_load_cursor (LPCTSTR name)
2016 /* Try first to load cursor from application resource. */
2017 Cursor cursor = LoadImage ((HINSTANCE) GetModuleHandle(NULL),
2018 name, IMAGE_CURSOR, 0, 0,
2019 LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_SHARED);
2020 if (!cursor)
2022 /* Then try to load a shared predefined cursor. */
2023 cursor = LoadImage (NULL, name, IMAGE_CURSOR, 0, 0,
2024 LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_SHARED);
2026 return cursor;
2029 extern LRESULT CALLBACK w32_wnd_proc ();
2031 BOOL
2032 w32_init_class (hinst)
2033 HINSTANCE hinst;
2035 WNDCLASS wc;
2037 wc.style = CS_HREDRAW | CS_VREDRAW;
2038 wc.lpfnWndProc = (WNDPROC) w32_wnd_proc;
2039 wc.cbClsExtra = 0;
2040 wc.cbWndExtra = WND_EXTRA_BYTES;
2041 wc.hInstance = hinst;
2042 wc.hIcon = LoadIcon (hinst, EMACS_CLASS);
2043 wc.hCursor = w32_load_cursor (IDC_ARROW);
2044 wc.hbrBackground = NULL; /* GetStockObject (WHITE_BRUSH); */
2045 wc.lpszMenuName = NULL;
2046 wc.lpszClassName = EMACS_CLASS;
2048 return (RegisterClass (&wc));
2051 HWND
2052 w32_createscrollbar (f, bar)
2053 struct frame *f;
2054 struct scroll_bar * bar;
2056 return (CreateWindow ("SCROLLBAR", "", SBS_VERT | WS_CHILD | WS_VISIBLE,
2057 /* Position and size of scroll bar. */
2058 XINT(bar->left) + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
2059 XINT(bar->top),
2060 XINT(bar->width) - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
2061 XINT(bar->height),
2062 FRAME_W32_WINDOW (f),
2063 NULL,
2064 hinst,
2065 NULL));
2068 void
2069 w32_createwindow (f)
2070 struct frame *f;
2072 HWND hwnd;
2073 RECT rect;
2074 Lisp_Object top = Qunbound;
2075 Lisp_Object left = Qunbound;
2077 rect.left = rect.top = 0;
2078 rect.right = FRAME_PIXEL_WIDTH (f);
2079 rect.bottom = FRAME_PIXEL_HEIGHT (f);
2081 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
2082 FRAME_EXTERNAL_MENU_BAR (f));
2084 /* Do first time app init */
2086 if (!hprevinst)
2088 w32_init_class (hinst);
2091 if (f->size_hint_flags & USPosition || f->size_hint_flags & PPosition)
2093 XSETINT (left, f->left_pos);
2094 XSETINT (top, f->top_pos);
2096 else if (EQ (left, Qunbound) && EQ (top, Qunbound))
2098 /* When called with RES_TYPE_NUMBER, w32_get_arg will return zero
2099 for anything that is not a number and is not Qunbound. */
2100 left = w32_get_arg (Qnil, Qleft, "left", "Left", RES_TYPE_NUMBER);
2101 top = w32_get_arg (Qnil, Qtop, "top", "Top", RES_TYPE_NUMBER);
2104 FRAME_W32_WINDOW (f) = hwnd
2105 = CreateWindow (EMACS_CLASS,
2106 f->namebuf,
2107 f->output_data.w32->dwStyle | WS_CLIPCHILDREN,
2108 EQ (left, Qunbound) ? CW_USEDEFAULT : XINT (left),
2109 EQ (top, Qunbound) ? CW_USEDEFAULT : XINT (top),
2110 rect.right - rect.left,
2111 rect.bottom - rect.top,
2112 NULL,
2113 NULL,
2114 hinst,
2115 NULL);
2117 if (hwnd)
2119 SetWindowLong (hwnd, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
2120 SetWindowLong (hwnd, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
2121 SetWindowLong (hwnd, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
2122 SetWindowLong (hwnd, WND_SCROLLBAR_INDEX, f->scroll_bar_actual_width);
2123 SetWindowLong (hwnd, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
2125 /* Enable drag-n-drop. */
2126 DragAcceptFiles (hwnd, TRUE);
2128 /* Do this to discard the default setting specified by our parent. */
2129 ShowWindow (hwnd, SW_HIDE);
2131 /* Update frame positions. */
2132 GetWindowRect (hwnd, &rect);
2133 f->left_pos = rect.left;
2134 f->top_pos = rect.top;
2138 void
2139 my_post_msg (wmsg, hwnd, msg, wParam, lParam)
2140 W32Msg * wmsg;
2141 HWND hwnd;
2142 UINT msg;
2143 WPARAM wParam;
2144 LPARAM lParam;
2146 wmsg->msg.hwnd = hwnd;
2147 wmsg->msg.message = msg;
2148 wmsg->msg.wParam = wParam;
2149 wmsg->msg.lParam = lParam;
2150 wmsg->msg.time = GetMessageTime ();
2152 post_msg (wmsg);
2155 /* GetKeyState and MapVirtualKey on Windows 95 do not actually distinguish
2156 between left and right keys as advertised. We test for this
2157 support dynamically, and set a flag when the support is absent. If
2158 absent, we keep track of the left and right control and alt keys
2159 ourselves. This is particularly necessary on keyboards that rely
2160 upon the AltGr key, which is represented as having the left control
2161 and right alt keys pressed. For these keyboards, we need to know
2162 when the left alt key has been pressed in addition to the AltGr key
2163 so that we can properly support M-AltGr-key sequences (such as M-@
2164 on Swedish keyboards). */
2166 #define EMACS_LCONTROL 0
2167 #define EMACS_RCONTROL 1
2168 #define EMACS_LMENU 2
2169 #define EMACS_RMENU 3
2171 static int modifiers[4];
2172 static int modifiers_recorded;
2173 static int modifier_key_support_tested;
2175 static void
2176 test_modifier_support (unsigned int wparam)
2178 unsigned int l, r;
2180 if (wparam != VK_CONTROL && wparam != VK_MENU)
2181 return;
2182 if (wparam == VK_CONTROL)
2184 l = VK_LCONTROL;
2185 r = VK_RCONTROL;
2187 else
2189 l = VK_LMENU;
2190 r = VK_RMENU;
2192 if (!(GetKeyState (l) & 0x8000) && !(GetKeyState (r) & 0x8000))
2193 modifiers_recorded = 1;
2194 else
2195 modifiers_recorded = 0;
2196 modifier_key_support_tested = 1;
2199 static void
2200 record_keydown (unsigned int wparam, unsigned int lparam)
2202 int i;
2204 if (!modifier_key_support_tested)
2205 test_modifier_support (wparam);
2207 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
2208 return;
2210 if (wparam == VK_CONTROL)
2211 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
2212 else
2213 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
2215 modifiers[i] = 1;
2218 static void
2219 record_keyup (unsigned int wparam, unsigned int lparam)
2221 int i;
2223 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
2224 return;
2226 if (wparam == VK_CONTROL)
2227 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
2228 else
2229 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
2231 modifiers[i] = 0;
2234 /* Emacs can lose focus while a modifier key has been pressed. When
2235 it regains focus, be conservative and clear all modifiers since
2236 we cannot reconstruct the left and right modifier state. */
2237 static void
2238 reset_modifiers ()
2240 SHORT ctrl, alt;
2242 if (GetFocus () == NULL)
2243 /* Emacs doesn't have keyboard focus. Do nothing. */
2244 return;
2246 ctrl = GetAsyncKeyState (VK_CONTROL);
2247 alt = GetAsyncKeyState (VK_MENU);
2249 if (!(ctrl & 0x08000))
2250 /* Clear any recorded control modifier state. */
2251 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
2253 if (!(alt & 0x08000))
2254 /* Clear any recorded alt modifier state. */
2255 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
2257 /* Update the state of all modifier keys, because modifiers used in
2258 hot-key combinations can get stuck on if Emacs loses focus as a
2259 result of a hot-key being pressed. */
2261 BYTE keystate[256];
2263 #define CURRENT_STATE(key) ((GetAsyncKeyState (key) & 0x8000) >> 8)
2265 GetKeyboardState (keystate);
2266 keystate[VK_SHIFT] = CURRENT_STATE (VK_SHIFT);
2267 keystate[VK_CONTROL] = CURRENT_STATE (VK_CONTROL);
2268 keystate[VK_LCONTROL] = CURRENT_STATE (VK_LCONTROL);
2269 keystate[VK_RCONTROL] = CURRENT_STATE (VK_RCONTROL);
2270 keystate[VK_MENU] = CURRENT_STATE (VK_MENU);
2271 keystate[VK_LMENU] = CURRENT_STATE (VK_LMENU);
2272 keystate[VK_RMENU] = CURRENT_STATE (VK_RMENU);
2273 keystate[VK_LWIN] = CURRENT_STATE (VK_LWIN);
2274 keystate[VK_RWIN] = CURRENT_STATE (VK_RWIN);
2275 keystate[VK_APPS] = CURRENT_STATE (VK_APPS);
2276 SetKeyboardState (keystate);
2280 /* Synchronize modifier state with what is reported with the current
2281 keystroke. Even if we cannot distinguish between left and right
2282 modifier keys, we know that, if no modifiers are set, then neither
2283 the left or right modifier should be set. */
2284 static void
2285 sync_modifiers ()
2287 if (!modifiers_recorded)
2288 return;
2290 if (!(GetKeyState (VK_CONTROL) & 0x8000))
2291 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
2293 if (!(GetKeyState (VK_MENU) & 0x8000))
2294 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
2297 static int
2298 modifier_set (int vkey)
2300 if (vkey == VK_CAPITAL || vkey == VK_SCROLL)
2301 return (GetKeyState (vkey) & 0x1);
2302 if (!modifiers_recorded)
2303 return (GetKeyState (vkey) & 0x8000);
2305 switch (vkey)
2307 case VK_LCONTROL:
2308 return modifiers[EMACS_LCONTROL];
2309 case VK_RCONTROL:
2310 return modifiers[EMACS_RCONTROL];
2311 case VK_LMENU:
2312 return modifiers[EMACS_LMENU];
2313 case VK_RMENU:
2314 return modifiers[EMACS_RMENU];
2316 return (GetKeyState (vkey) & 0x8000);
2319 /* Convert between the modifier bits W32 uses and the modifier bits
2320 Emacs uses. */
2322 unsigned int
2323 w32_key_to_modifier (int key)
2325 Lisp_Object key_mapping;
2327 switch (key)
2329 case VK_LWIN:
2330 key_mapping = Vw32_lwindow_modifier;
2331 break;
2332 case VK_RWIN:
2333 key_mapping = Vw32_rwindow_modifier;
2334 break;
2335 case VK_APPS:
2336 key_mapping = Vw32_apps_modifier;
2337 break;
2338 case VK_SCROLL:
2339 key_mapping = Vw32_scroll_lock_modifier;
2340 break;
2341 default:
2342 key_mapping = Qnil;
2345 /* NB. This code runs in the input thread, asychronously to the lisp
2346 thread, so we must be careful to ensure access to lisp data is
2347 thread-safe. The following code is safe because the modifier
2348 variable values are updated atomically from lisp and symbols are
2349 not relocated by GC. Also, we don't have to worry about seeing GC
2350 markbits here. */
2351 if (EQ (key_mapping, Qhyper))
2352 return hyper_modifier;
2353 if (EQ (key_mapping, Qsuper))
2354 return super_modifier;
2355 if (EQ (key_mapping, Qmeta))
2356 return meta_modifier;
2357 if (EQ (key_mapping, Qalt))
2358 return alt_modifier;
2359 if (EQ (key_mapping, Qctrl))
2360 return ctrl_modifier;
2361 if (EQ (key_mapping, Qcontrol)) /* synonym for ctrl */
2362 return ctrl_modifier;
2363 if (EQ (key_mapping, Qshift))
2364 return shift_modifier;
2366 /* Don't generate any modifier if not explicitly requested. */
2367 return 0;
2370 unsigned int
2371 w32_get_modifiers ()
2373 return ((modifier_set (VK_SHIFT) ? shift_modifier : 0) |
2374 (modifier_set (VK_CONTROL) ? ctrl_modifier : 0) |
2375 (modifier_set (VK_LWIN) ? w32_key_to_modifier (VK_LWIN) : 0) |
2376 (modifier_set (VK_RWIN) ? w32_key_to_modifier (VK_RWIN) : 0) |
2377 (modifier_set (VK_APPS) ? w32_key_to_modifier (VK_APPS) : 0) |
2378 (modifier_set (VK_SCROLL) ? w32_key_to_modifier (VK_SCROLL) : 0) |
2379 (modifier_set (VK_MENU) ?
2380 ((NILP (Vw32_alt_is_meta)) ? alt_modifier : meta_modifier) : 0));
2383 /* We map the VK_* modifiers into console modifier constants
2384 so that we can use the same routines to handle both console
2385 and window input. */
2387 static int
2388 construct_console_modifiers ()
2390 int mods;
2392 mods = 0;
2393 mods |= (modifier_set (VK_SHIFT)) ? SHIFT_PRESSED : 0;
2394 mods |= (modifier_set (VK_CAPITAL)) ? CAPSLOCK_ON : 0;
2395 mods |= (modifier_set (VK_SCROLL)) ? SCROLLLOCK_ON : 0;
2396 mods |= (modifier_set (VK_NUMLOCK)) ? NUMLOCK_ON : 0;
2397 mods |= (modifier_set (VK_LCONTROL)) ? LEFT_CTRL_PRESSED : 0;
2398 mods |= (modifier_set (VK_RCONTROL)) ? RIGHT_CTRL_PRESSED : 0;
2399 mods |= (modifier_set (VK_LMENU)) ? LEFT_ALT_PRESSED : 0;
2400 mods |= (modifier_set (VK_RMENU)) ? RIGHT_ALT_PRESSED : 0;
2401 mods |= (modifier_set (VK_LWIN)) ? LEFT_WIN_PRESSED : 0;
2402 mods |= (modifier_set (VK_RWIN)) ? RIGHT_WIN_PRESSED : 0;
2403 mods |= (modifier_set (VK_APPS)) ? APPS_PRESSED : 0;
2405 return mods;
2408 static int
2409 w32_get_key_modifiers (unsigned int wparam, unsigned int lparam)
2411 int mods;
2413 /* Convert to emacs modifiers. */
2414 mods = w32_kbd_mods_to_emacs (construct_console_modifiers (), wparam);
2416 return mods;
2419 unsigned int
2420 map_keypad_keys (unsigned int virt_key, unsigned int extended)
2422 if (virt_key < VK_CLEAR || virt_key > VK_DELETE)
2423 return virt_key;
2425 if (virt_key == VK_RETURN)
2426 return (extended ? VK_NUMPAD_ENTER : VK_RETURN);
2428 if (virt_key >= VK_PRIOR && virt_key <= VK_DOWN)
2429 return (!extended ? (VK_NUMPAD_PRIOR + (virt_key - VK_PRIOR)) : virt_key);
2431 if (virt_key == VK_INSERT || virt_key == VK_DELETE)
2432 return (!extended ? (VK_NUMPAD_INSERT + (virt_key - VK_INSERT)) : virt_key);
2434 if (virt_key == VK_CLEAR)
2435 return (!extended ? VK_NUMPAD_CLEAR : virt_key);
2437 return virt_key;
2440 /* List of special key combinations which w32 would normally capture,
2441 but emacs should grab instead. Not directly visible to lisp, to
2442 simplify synchronization. Each item is an integer encoding a virtual
2443 key code and modifier combination to capture. */
2444 Lisp_Object w32_grabbed_keys;
2446 #define HOTKEY(vk,mods) make_number (((vk) & 255) | ((mods) << 8))
2447 #define HOTKEY_ID(k) (XFASTINT (k) & 0xbfff)
2448 #define HOTKEY_VK_CODE(k) (XFASTINT (k) & 255)
2449 #define HOTKEY_MODIFIERS(k) (XFASTINT (k) >> 8)
2451 #define RAW_HOTKEY_ID(k) ((k) & 0xbfff)
2452 #define RAW_HOTKEY_VK_CODE(k) ((k) & 255)
2453 #define RAW_HOTKEY_MODIFIERS(k) ((k) >> 8)
2455 /* Register hot-keys for reserved key combinations when Emacs has
2456 keyboard focus, since this is the only way Emacs can receive key
2457 combinations like Alt-Tab which are used by the system. */
2459 static void
2460 register_hot_keys (hwnd)
2461 HWND hwnd;
2463 Lisp_Object keylist;
2465 /* Use GC_CONSP, since we are called asynchronously. */
2466 for (keylist = w32_grabbed_keys; GC_CONSP (keylist); keylist = XCDR (keylist))
2468 Lisp_Object key = XCAR (keylist);
2470 /* Deleted entries get set to nil. */
2471 if (!INTEGERP (key))
2472 continue;
2474 RegisterHotKey (hwnd, HOTKEY_ID (key),
2475 HOTKEY_MODIFIERS (key), HOTKEY_VK_CODE (key));
2479 static void
2480 unregister_hot_keys (hwnd)
2481 HWND hwnd;
2483 Lisp_Object keylist;
2485 /* Use GC_CONSP, since we are called asynchronously. */
2486 for (keylist = w32_grabbed_keys; GC_CONSP (keylist); keylist = XCDR (keylist))
2488 Lisp_Object key = XCAR (keylist);
2490 if (!INTEGERP (key))
2491 continue;
2493 UnregisterHotKey (hwnd, HOTKEY_ID (key));
2497 /* Main message dispatch loop. */
2499 static void
2500 w32_msg_pump (deferred_msg * msg_buf)
2502 MSG msg;
2503 int result;
2504 HWND focus_window;
2506 msh_mousewheel = RegisterWindowMessage (MSH_MOUSEWHEEL);
2508 while (GetMessage (&msg, NULL, 0, 0))
2510 if (msg.hwnd == NULL)
2512 switch (msg.message)
2514 case WM_NULL:
2515 /* Produced by complete_deferred_msg; just ignore. */
2516 break;
2517 case WM_EMACS_CREATEWINDOW:
2518 /* Initialize COM for this window. Even though we don't use it,
2519 some third party shell extensions can cause it to be used in
2520 system dialogs, which causes a crash if it is not initialized.
2521 This is a known bug in Windows, which was fixed long ago, but
2522 the patch for XP is not publically available until XP SP3,
2523 and older versions will never be patched. */
2524 CoInitialize (NULL);
2525 w32_createwindow ((struct frame *) msg.wParam);
2526 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2527 abort ();
2528 break;
2529 case WM_EMACS_SETLOCALE:
2530 SetThreadLocale (msg.wParam);
2531 /* Reply is not expected. */
2532 break;
2533 case WM_EMACS_SETKEYBOARDLAYOUT:
2534 result = (int) ActivateKeyboardLayout ((HKL) msg.wParam, 0);
2535 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
2536 result, 0))
2537 abort ();
2538 break;
2539 case WM_EMACS_REGISTER_HOT_KEY:
2540 focus_window = GetFocus ();
2541 if (focus_window != NULL)
2542 RegisterHotKey (focus_window,
2543 RAW_HOTKEY_ID (msg.wParam),
2544 RAW_HOTKEY_MODIFIERS (msg.wParam),
2545 RAW_HOTKEY_VK_CODE (msg.wParam));
2546 /* Reply is not expected. */
2547 break;
2548 case WM_EMACS_UNREGISTER_HOT_KEY:
2549 focus_window = GetFocus ();
2550 if (focus_window != NULL)
2551 UnregisterHotKey (focus_window, RAW_HOTKEY_ID (msg.wParam));
2552 /* Mark item as erased. NB: this code must be
2553 thread-safe. The next line is okay because the cons
2554 cell is never made into garbage and is not relocated by
2555 GC. */
2556 XSETCAR ((Lisp_Object) ((EMACS_INT) msg.lParam), Qnil);
2557 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2558 abort ();
2559 break;
2560 case WM_EMACS_TOGGLE_LOCK_KEY:
2562 int vk_code = (int) msg.wParam;
2563 int cur_state = (GetKeyState (vk_code) & 1);
2564 Lisp_Object new_state = (Lisp_Object) ((EMACS_INT) msg.lParam);
2566 /* NB: This code must be thread-safe. It is safe to
2567 call NILP because symbols are not relocated by GC,
2568 and pointer here is not touched by GC (so the markbit
2569 can't be set). Numbers are safe because they are
2570 immediate values. */
2571 if (NILP (new_state)
2572 || (NUMBERP (new_state)
2573 && ((XUINT (new_state)) & 1) != cur_state))
2575 one_w32_display_info.faked_key = vk_code;
2577 keybd_event ((BYTE) vk_code,
2578 (BYTE) MapVirtualKey (vk_code, 0),
2579 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2580 keybd_event ((BYTE) vk_code,
2581 (BYTE) MapVirtualKey (vk_code, 0),
2582 KEYEVENTF_EXTENDEDKEY | 0, 0);
2583 keybd_event ((BYTE) vk_code,
2584 (BYTE) MapVirtualKey (vk_code, 0),
2585 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2586 cur_state = !cur_state;
2588 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
2589 cur_state, 0))
2590 abort ();
2592 break;
2593 default:
2594 DebPrint (("msg %x not expected by w32_msg_pump\n", msg.message));
2597 else
2599 DispatchMessage (&msg);
2602 /* Exit nested loop when our deferred message has completed. */
2603 if (msg_buf->completed)
2604 break;
2608 deferred_msg * deferred_msg_head;
2610 static deferred_msg *
2611 find_deferred_msg (HWND hwnd, UINT msg)
2613 deferred_msg * item;
2615 /* Don't actually need synchronization for read access, since
2616 modification of single pointer is always atomic. */
2617 /* enter_crit (); */
2619 for (item = deferred_msg_head; item != NULL; item = item->next)
2620 if (item->w32msg.msg.hwnd == hwnd
2621 && item->w32msg.msg.message == msg)
2622 break;
2624 /* leave_crit (); */
2626 return item;
2629 static LRESULT
2630 send_deferred_msg (deferred_msg * msg_buf,
2631 HWND hwnd,
2632 UINT msg,
2633 WPARAM wParam,
2634 LPARAM lParam)
2636 /* Only input thread can send deferred messages. */
2637 if (GetCurrentThreadId () != dwWindowsThreadId)
2638 abort ();
2640 /* It is an error to send a message that is already deferred. */
2641 if (find_deferred_msg (hwnd, msg) != NULL)
2642 abort ();
2644 /* Enforced synchronization is not needed because this is the only
2645 function that alters deferred_msg_head, and the following critical
2646 section is guaranteed to only be serially reentered (since only the
2647 input thread can call us). */
2649 /* enter_crit (); */
2651 msg_buf->completed = 0;
2652 msg_buf->next = deferred_msg_head;
2653 deferred_msg_head = msg_buf;
2654 my_post_msg (&msg_buf->w32msg, hwnd, msg, wParam, lParam);
2656 /* leave_crit (); */
2658 /* Start a new nested message loop to process other messages until
2659 this one is completed. */
2660 w32_msg_pump (msg_buf);
2662 deferred_msg_head = msg_buf->next;
2664 return msg_buf->result;
2667 void
2668 complete_deferred_msg (HWND hwnd, UINT msg, LRESULT result)
2670 deferred_msg * msg_buf = find_deferred_msg (hwnd, msg);
2672 if (msg_buf == NULL)
2673 /* Message may have been cancelled, so don't abort(). */
2674 return;
2676 msg_buf->result = result;
2677 msg_buf->completed = 1;
2679 /* Ensure input thread is woken so it notices the completion. */
2680 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
2683 void
2684 cancel_all_deferred_msgs ()
2686 deferred_msg * item;
2688 /* Don't actually need synchronization for read access, since
2689 modification of single pointer is always atomic. */
2690 /* enter_crit (); */
2692 for (item = deferred_msg_head; item != NULL; item = item->next)
2694 item->result = 0;
2695 item->completed = 1;
2698 /* leave_crit (); */
2700 /* Ensure input thread is woken so it notices the completion. */
2701 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
2704 DWORD WINAPI
2705 w32_msg_worker (void *arg)
2707 MSG msg;
2708 deferred_msg dummy_buf;
2710 /* Ensure our message queue is created */
2712 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
2714 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2715 abort ();
2717 memset (&dummy_buf, 0, sizeof (dummy_buf));
2718 dummy_buf.w32msg.msg.hwnd = NULL;
2719 dummy_buf.w32msg.msg.message = WM_NULL;
2721 /* This is the inital message loop which should only exit when the
2722 application quits. */
2723 w32_msg_pump (&dummy_buf);
2725 return 0;
2728 static void
2729 signal_user_input ()
2731 /* Interrupt any lisp that wants to be interrupted by input. */
2732 if (!NILP (Vthrow_on_input))
2734 Vquit_flag = Vthrow_on_input;
2735 /* If we're inside a function that wants immediate quits,
2736 do it now. */
2737 if (immediate_quit && NILP (Vinhibit_quit))
2739 immediate_quit = 0;
2740 QUIT;
2746 static void
2747 post_character_message (hwnd, msg, wParam, lParam, modifiers)
2748 HWND hwnd;
2749 UINT msg;
2750 WPARAM wParam;
2751 LPARAM lParam;
2752 DWORD modifiers;
2755 W32Msg wmsg;
2757 wmsg.dwModifiers = modifiers;
2759 /* Detect quit_char and set quit-flag directly. Note that we
2760 still need to post a message to ensure the main thread will be
2761 woken up if blocked in sys_select(), but we do NOT want to post
2762 the quit_char message itself (because it will usually be as if
2763 the user had typed quit_char twice). Instead, we post a dummy
2764 message that has no particular effect. */
2766 int c = wParam;
2767 if (isalpha (c) && wmsg.dwModifiers == ctrl_modifier)
2768 c = make_ctrl_char (c) & 0377;
2769 if (c == quit_char
2770 || (wmsg.dwModifiers == 0 &&
2771 w32_quit_key && wParam == w32_quit_key))
2773 Vquit_flag = Qt;
2775 /* The choice of message is somewhat arbitrary, as long as
2776 the main thread handler just ignores it. */
2777 msg = WM_NULL;
2779 /* Interrupt any blocking system calls. */
2780 signal_quit ();
2782 /* As a safety precaution, forcibly complete any deferred
2783 messages. This is a kludge, but I don't see any particularly
2784 clean way to handle the situation where a deferred message is
2785 "dropped" in the lisp thread, and will thus never be
2786 completed, eg. by the user trying to activate the menubar
2787 when the lisp thread is busy, and then typing C-g when the
2788 menubar doesn't open promptly (with the result that the
2789 menubar never responds at all because the deferred
2790 WM_INITMENU message is never completed). Another problem
2791 situation is when the lisp thread calls SendMessage (to send
2792 a window manager command) when a message has been deferred;
2793 the lisp thread gets blocked indefinitely waiting for the
2794 deferred message to be completed, which itself is waiting for
2795 the lisp thread to respond.
2797 Note that we don't want to block the input thread waiting for
2798 a reponse from the lisp thread (although that would at least
2799 solve the deadlock problem above), because we want to be able
2800 to receive C-g to interrupt the lisp thread. */
2801 cancel_all_deferred_msgs ();
2803 else
2804 signal_user_input ();
2807 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2810 /* Main window procedure */
2812 LRESULT CALLBACK
2813 w32_wnd_proc (hwnd, msg, wParam, lParam)
2814 HWND hwnd;
2815 UINT msg;
2816 WPARAM wParam;
2817 LPARAM lParam;
2819 struct frame *f;
2820 struct w32_display_info *dpyinfo = &one_w32_display_info;
2821 W32Msg wmsg;
2822 int windows_translate;
2823 int key;
2825 /* Note that it is okay to call x_window_to_frame, even though we are
2826 not running in the main lisp thread, because frame deletion
2827 requires the lisp thread to synchronize with this thread. Thus, if
2828 a frame struct is returned, it can be used without concern that the
2829 lisp thread might make it disappear while we are using it.
2831 NB. Walking the frame list in this thread is safe (as long as
2832 writes of Lisp_Object slots are atomic, which they are on Windows).
2833 Although delete-frame can destructively modify the frame list while
2834 we are walking it, a garbage collection cannot occur until after
2835 delete-frame has synchronized with this thread.
2837 It is also safe to use functions that make GDI calls, such as
2838 w32_clear_rect, because these functions must obtain a DC handle
2839 from the frame struct using get_frame_dc which is thread-aware. */
2841 switch (msg)
2843 case WM_ERASEBKGND:
2844 f = x_window_to_frame (dpyinfo, hwnd);
2845 if (f)
2847 HDC hdc = get_frame_dc (f);
2848 GetUpdateRect (hwnd, &wmsg.rect, FALSE);
2849 w32_clear_rect (f, hdc, &wmsg.rect);
2850 release_frame_dc (f, hdc);
2852 #if defined (W32_DEBUG_DISPLAY)
2853 DebPrint (("WM_ERASEBKGND (frame %p): erasing %d,%d-%d,%d\n",
2855 wmsg.rect.left, wmsg.rect.top,
2856 wmsg.rect.right, wmsg.rect.bottom));
2857 #endif /* W32_DEBUG_DISPLAY */
2859 return 1;
2860 case WM_PALETTECHANGED:
2861 /* ignore our own changes */
2862 if ((HWND)wParam != hwnd)
2864 f = x_window_to_frame (dpyinfo, hwnd);
2865 if (f)
2866 /* get_frame_dc will realize our palette and force all
2867 frames to be redrawn if needed. */
2868 release_frame_dc (f, get_frame_dc (f));
2870 return 0;
2871 case WM_PAINT:
2873 PAINTSTRUCT paintStruct;
2874 RECT update_rect;
2875 bzero (&update_rect, sizeof (update_rect));
2877 f = x_window_to_frame (dpyinfo, hwnd);
2878 if (f == 0)
2880 DebPrint (("WM_PAINT received for unknown window %p\n", hwnd));
2881 return 0;
2884 /* MSDN Docs say not to call BeginPaint if GetUpdateRect
2885 fails. Apparently this can happen under some
2886 circumstances. */
2887 if (GetUpdateRect (hwnd, &update_rect, FALSE) || !w32_strict_painting)
2889 enter_crit ();
2890 BeginPaint (hwnd, &paintStruct);
2892 /* The rectangles returned by GetUpdateRect and BeginPaint
2893 do not always match. Play it safe by assuming both areas
2894 are invalid. */
2895 UnionRect (&(wmsg.rect), &update_rect, &(paintStruct.rcPaint));
2897 #if defined (W32_DEBUG_DISPLAY)
2898 DebPrint (("WM_PAINT (frame %p): painting %d,%d-%d,%d\n",
2900 wmsg.rect.left, wmsg.rect.top,
2901 wmsg.rect.right, wmsg.rect.bottom));
2902 DebPrint ((" [update region is %d,%d-%d,%d]\n",
2903 update_rect.left, update_rect.top,
2904 update_rect.right, update_rect.bottom));
2905 #endif
2906 EndPaint (hwnd, &paintStruct);
2907 leave_crit ();
2909 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2911 return 0;
2914 /* If GetUpdateRect returns 0 (meaning there is no update
2915 region), assume the whole window needs to be repainted. */
2916 GetClientRect(hwnd, &wmsg.rect);
2917 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2918 return 0;
2921 case WM_INPUTLANGCHANGE:
2922 /* Inform lisp thread of keyboard layout changes. */
2923 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2925 /* Clear dead keys in the keyboard state; for simplicity only
2926 preserve modifier key states. */
2928 int i;
2929 BYTE keystate[256];
2931 GetKeyboardState (keystate);
2932 for (i = 0; i < 256; i++)
2933 if (1
2934 && i != VK_SHIFT
2935 && i != VK_LSHIFT
2936 && i != VK_RSHIFT
2937 && i != VK_CAPITAL
2938 && i != VK_NUMLOCK
2939 && i != VK_SCROLL
2940 && i != VK_CONTROL
2941 && i != VK_LCONTROL
2942 && i != VK_RCONTROL
2943 && i != VK_MENU
2944 && i != VK_LMENU
2945 && i != VK_RMENU
2946 && i != VK_LWIN
2947 && i != VK_RWIN)
2948 keystate[i] = 0;
2949 SetKeyboardState (keystate);
2951 goto dflt;
2953 case WM_HOTKEY:
2954 /* Synchronize hot keys with normal input. */
2955 PostMessage (hwnd, WM_KEYDOWN, HIWORD (lParam), 0);
2956 return (0);
2958 case WM_KEYUP:
2959 case WM_SYSKEYUP:
2960 record_keyup (wParam, lParam);
2961 goto dflt;
2963 case WM_KEYDOWN:
2964 case WM_SYSKEYDOWN:
2965 /* Ignore keystrokes we fake ourself; see below. */
2966 if (dpyinfo->faked_key == wParam)
2968 dpyinfo->faked_key = 0;
2969 /* Make sure TranslateMessage sees them though (as long as
2970 they don't produce WM_CHAR messages). This ensures that
2971 indicator lights are toggled promptly on Windows 9x, for
2972 example. */
2973 if (lispy_function_keys[wParam] != 0)
2975 windows_translate = 1;
2976 goto translate;
2978 return 0;
2981 /* Synchronize modifiers with current keystroke. */
2982 sync_modifiers ();
2983 record_keydown (wParam, lParam);
2984 wParam = map_keypad_keys (wParam, (lParam & 0x1000000L) != 0);
2986 windows_translate = 0;
2988 switch (wParam)
2990 case VK_LWIN:
2991 if (NILP (Vw32_pass_lwindow_to_system))
2993 /* Prevent system from acting on keyup (which opens the
2994 Start menu if no other key was pressed) by simulating a
2995 press of Space which we will ignore. */
2996 if (GetAsyncKeyState (wParam) & 1)
2998 if (NUMBERP (Vw32_phantom_key_code))
2999 key = XUINT (Vw32_phantom_key_code) & 255;
3000 else
3001 key = VK_SPACE;
3002 dpyinfo->faked_key = key;
3003 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
3006 if (!NILP (Vw32_lwindow_modifier))
3007 return 0;
3008 break;
3009 case VK_RWIN:
3010 if (NILP (Vw32_pass_rwindow_to_system))
3012 if (GetAsyncKeyState (wParam) & 1)
3014 if (NUMBERP (Vw32_phantom_key_code))
3015 key = XUINT (Vw32_phantom_key_code) & 255;
3016 else
3017 key = VK_SPACE;
3018 dpyinfo->faked_key = key;
3019 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
3022 if (!NILP (Vw32_rwindow_modifier))
3023 return 0;
3024 break;
3025 case VK_APPS:
3026 if (!NILP (Vw32_apps_modifier))
3027 return 0;
3028 break;
3029 case VK_MENU:
3030 if (NILP (Vw32_pass_alt_to_system))
3031 /* Prevent DefWindowProc from activating the menu bar if an
3032 Alt key is pressed and released by itself. */
3033 return 0;
3034 windows_translate = 1;
3035 break;
3036 case VK_CAPITAL:
3037 /* Decide whether to treat as modifier or function key. */
3038 if (NILP (Vw32_enable_caps_lock))
3039 goto disable_lock_key;
3040 windows_translate = 1;
3041 break;
3042 case VK_NUMLOCK:
3043 /* Decide whether to treat as modifier or function key. */
3044 if (NILP (Vw32_enable_num_lock))
3045 goto disable_lock_key;
3046 windows_translate = 1;
3047 break;
3048 case VK_SCROLL:
3049 /* Decide whether to treat as modifier or function key. */
3050 if (NILP (Vw32_scroll_lock_modifier))
3051 goto disable_lock_key;
3052 windows_translate = 1;
3053 break;
3054 disable_lock_key:
3055 /* Ensure the appropriate lock key state (and indicator light)
3056 remains in the same state. We do this by faking another
3057 press of the relevant key. Apparently, this really is the
3058 only way to toggle the state of the indicator lights. */
3059 dpyinfo->faked_key = wParam;
3060 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
3061 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
3062 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
3063 KEYEVENTF_EXTENDEDKEY | 0, 0);
3064 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
3065 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
3066 /* Ensure indicator lights are updated promptly on Windows 9x
3067 (TranslateMessage apparently does this), after forwarding
3068 input event. */
3069 post_character_message (hwnd, msg, wParam, lParam,
3070 w32_get_key_modifiers (wParam, lParam));
3071 windows_translate = 1;
3072 break;
3073 case VK_CONTROL:
3074 case VK_SHIFT:
3075 case VK_PROCESSKEY: /* Generated by IME. */
3076 windows_translate = 1;
3077 break;
3078 case VK_CANCEL:
3079 /* Windows maps Ctrl-Pause (aka Ctrl-Break) into VK_CANCEL,
3080 which is confusing for purposes of key binding; convert
3081 VK_CANCEL events into VK_PAUSE events. */
3082 wParam = VK_PAUSE;
3083 break;
3084 case VK_PAUSE:
3085 /* Windows maps Ctrl-NumLock into VK_PAUSE, which is confusing
3086 for purposes of key binding; convert these back into
3087 VK_NUMLOCK events, at least when we want to see NumLock key
3088 presses. (Note that there is never any possibility that
3089 VK_PAUSE with Ctrl really is C-Pause as per above.) */
3090 if (NILP (Vw32_enable_num_lock) && modifier_set (VK_CONTROL))
3091 wParam = VK_NUMLOCK;
3092 break;
3093 default:
3094 /* If not defined as a function key, change it to a WM_CHAR message. */
3095 if (lispy_function_keys[wParam] == 0)
3097 DWORD modifiers = construct_console_modifiers ();
3099 if (!NILP (Vw32_recognize_altgr)
3100 && modifier_set (VK_LCONTROL) && modifier_set (VK_RMENU))
3102 /* Always let TranslateMessage handle AltGr key chords;
3103 for some reason, ToAscii doesn't always process AltGr
3104 chords correctly. */
3105 windows_translate = 1;
3107 else if ((modifiers & (~SHIFT_PRESSED & ~CAPSLOCK_ON)) != 0)
3109 /* Handle key chords including any modifiers other
3110 than shift directly, in order to preserve as much
3111 modifier information as possible. */
3112 if ('A' <= wParam && wParam <= 'Z')
3114 /* Don't translate modified alphabetic keystrokes,
3115 so the user doesn't need to constantly switch
3116 layout to type control or meta keystrokes when
3117 the normal layout translates alphabetic
3118 characters to non-ascii characters. */
3119 if (!modifier_set (VK_SHIFT))
3120 wParam += ('a' - 'A');
3121 msg = WM_CHAR;
3123 else
3125 /* Try to handle other keystrokes by determining the
3126 base character (ie. translating the base key plus
3127 shift modifier). */
3128 int add;
3129 int isdead = 0;
3130 KEY_EVENT_RECORD key;
3132 key.bKeyDown = TRUE;
3133 key.wRepeatCount = 1;
3134 key.wVirtualKeyCode = wParam;
3135 key.wVirtualScanCode = (lParam & 0xFF0000) >> 16;
3136 key.uChar.AsciiChar = 0;
3137 key.dwControlKeyState = modifiers;
3139 add = w32_kbd_patch_key (&key);
3140 /* 0 means an unrecognised keycode, negative means
3141 dead key. Ignore both. */
3142 while (--add >= 0)
3144 /* Forward asciified character sequence. */
3145 post_character_message
3146 (hwnd, WM_CHAR, key.uChar.AsciiChar, lParam,
3147 w32_get_key_modifiers (wParam, lParam));
3148 w32_kbd_patch_key (&key);
3150 return 0;
3153 else
3155 /* Let TranslateMessage handle everything else. */
3156 windows_translate = 1;
3161 translate:
3162 if (windows_translate)
3164 MSG windows_msg = { hwnd, msg, wParam, lParam, 0, {0,0} };
3166 windows_msg.time = GetMessageTime ();
3167 TranslateMessage (&windows_msg);
3168 goto dflt;
3171 /* Fall through */
3173 case WM_SYSCHAR:
3174 case WM_CHAR:
3175 post_character_message (hwnd, msg, wParam, lParam,
3176 w32_get_key_modifiers (wParam, lParam));
3177 break;
3179 /* Simulate middle mouse button events when left and right buttons
3180 are used together, but only if user has two button mouse. */
3181 case WM_LBUTTONDOWN:
3182 case WM_RBUTTONDOWN:
3183 if (w32_num_mouse_buttons > 2)
3184 goto handle_plain_button;
3187 int this = (msg == WM_LBUTTONDOWN) ? LMOUSE : RMOUSE;
3188 int other = (msg == WM_LBUTTONDOWN) ? RMOUSE : LMOUSE;
3190 if (button_state & this)
3191 return 0;
3193 if (button_state == 0)
3194 SetCapture (hwnd);
3196 button_state |= this;
3198 if (button_state & other)
3200 if (mouse_button_timer)
3202 KillTimer (hwnd, mouse_button_timer);
3203 mouse_button_timer = 0;
3205 /* Generate middle mouse event instead. */
3206 msg = WM_MBUTTONDOWN;
3207 button_state |= MMOUSE;
3209 else if (button_state & MMOUSE)
3211 /* Ignore button event if we've already generated a
3212 middle mouse down event. This happens if the
3213 user releases and press one of the two buttons
3214 after we've faked a middle mouse event. */
3215 return 0;
3217 else
3219 /* Flush out saved message. */
3220 post_msg (&saved_mouse_button_msg);
3222 wmsg.dwModifiers = w32_get_modifiers ();
3223 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3224 signal_user_input ();
3226 /* Clear message buffer. */
3227 saved_mouse_button_msg.msg.hwnd = 0;
3229 else
3231 /* Hold onto message for now. */
3232 mouse_button_timer =
3233 SetTimer (hwnd, MOUSE_BUTTON_ID,
3234 w32_mouse_button_tolerance, NULL);
3235 saved_mouse_button_msg.msg.hwnd = hwnd;
3236 saved_mouse_button_msg.msg.message = msg;
3237 saved_mouse_button_msg.msg.wParam = wParam;
3238 saved_mouse_button_msg.msg.lParam = lParam;
3239 saved_mouse_button_msg.msg.time = GetMessageTime ();
3240 saved_mouse_button_msg.dwModifiers = w32_get_modifiers ();
3243 return 0;
3245 case WM_LBUTTONUP:
3246 case WM_RBUTTONUP:
3247 if (w32_num_mouse_buttons > 2)
3248 goto handle_plain_button;
3251 int this = (msg == WM_LBUTTONUP) ? LMOUSE : RMOUSE;
3252 int other = (msg == WM_LBUTTONUP) ? RMOUSE : LMOUSE;
3254 if ((button_state & this) == 0)
3255 return 0;
3257 button_state &= ~this;
3259 if (button_state & MMOUSE)
3261 /* Only generate event when second button is released. */
3262 if ((button_state & other) == 0)
3264 msg = WM_MBUTTONUP;
3265 button_state &= ~MMOUSE;
3267 if (button_state) abort ();
3269 else
3270 return 0;
3272 else
3274 /* Flush out saved message if necessary. */
3275 if (saved_mouse_button_msg.msg.hwnd)
3277 post_msg (&saved_mouse_button_msg);
3280 wmsg.dwModifiers = w32_get_modifiers ();
3281 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3282 signal_user_input ();
3284 /* Always clear message buffer and cancel timer. */
3285 saved_mouse_button_msg.msg.hwnd = 0;
3286 KillTimer (hwnd, mouse_button_timer);
3287 mouse_button_timer = 0;
3289 if (button_state == 0)
3290 ReleaseCapture ();
3292 return 0;
3294 case WM_XBUTTONDOWN:
3295 case WM_XBUTTONUP:
3296 if (w32_pass_extra_mouse_buttons_to_system)
3297 goto dflt;
3298 /* else fall through and process them. */
3299 case WM_MBUTTONDOWN:
3300 case WM_MBUTTONUP:
3301 handle_plain_button:
3303 BOOL up;
3304 int button;
3306 /* Ignore middle and extra buttons as long as the menu is active. */
3307 f = x_window_to_frame (dpyinfo, hwnd);
3308 if (f && f->output_data.w32->menubar_active)
3309 return 0;
3311 if (parse_button (msg, HIWORD (wParam), &button, &up))
3313 if (up) ReleaseCapture ();
3314 else SetCapture (hwnd);
3315 button = (button == 0) ? LMOUSE :
3316 ((button == 1) ? MMOUSE : RMOUSE);
3317 if (up)
3318 button_state &= ~button;
3319 else
3320 button_state |= button;
3324 wmsg.dwModifiers = w32_get_modifiers ();
3325 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3326 signal_user_input ();
3328 /* Need to return true for XBUTTON messages, false for others,
3329 to indicate that we processed the message. */
3330 return (msg == WM_XBUTTONDOWN || msg == WM_XBUTTONUP);
3332 case WM_MOUSEMOVE:
3333 /* Ignore mouse movements as long as the menu is active. These
3334 movements are processed by the window manager anyway, and
3335 it's wrong to handle them as if they happened on the
3336 underlying frame. */
3337 f = x_window_to_frame (dpyinfo, hwnd);
3338 if (f && f->output_data.w32->menubar_active)
3339 return 0;
3341 /* If the mouse has just moved into the frame, start tracking
3342 it, so we will be notified when it leaves the frame. Mouse
3343 tracking only works under W98 and NT4 and later. On earlier
3344 versions, there is no way of telling when the mouse leaves the
3345 frame, so we just have to put up with help-echo and mouse
3346 highlighting remaining while the frame is not active. */
3347 if (track_mouse_event_fn && !track_mouse_window)
3349 TRACKMOUSEEVENT tme;
3350 tme.cbSize = sizeof (tme);
3351 tme.dwFlags = TME_LEAVE;
3352 tme.hwndTrack = hwnd;
3354 track_mouse_event_fn (&tme);
3355 track_mouse_window = hwnd;
3357 case WM_VSCROLL:
3358 if (w32_mouse_move_interval <= 0
3359 || (msg == WM_MOUSEMOVE && button_state == 0))
3361 wmsg.dwModifiers = w32_get_modifiers ();
3362 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3363 return 0;
3366 /* Hang onto mouse move and scroll messages for a bit, to avoid
3367 sending such events to Emacs faster than it can process them.
3368 If we get more events before the timer from the first message
3369 expires, we just replace the first message. */
3371 if (saved_mouse_move_msg.msg.hwnd == 0)
3372 mouse_move_timer =
3373 SetTimer (hwnd, MOUSE_MOVE_ID,
3374 w32_mouse_move_interval, NULL);
3376 /* Hold onto message for now. */
3377 saved_mouse_move_msg.msg.hwnd = hwnd;
3378 saved_mouse_move_msg.msg.message = msg;
3379 saved_mouse_move_msg.msg.wParam = wParam;
3380 saved_mouse_move_msg.msg.lParam = lParam;
3381 saved_mouse_move_msg.msg.time = GetMessageTime ();
3382 saved_mouse_move_msg.dwModifiers = w32_get_modifiers ();
3384 return 0;
3386 case WM_MOUSEWHEEL:
3387 wmsg.dwModifiers = w32_get_modifiers ();
3388 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3389 signal_user_input ();
3390 return 0;
3392 case WM_DROPFILES:
3393 wmsg.dwModifiers = w32_get_modifiers ();
3394 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3395 signal_user_input ();
3396 return 0;
3398 case WM_TIMER:
3399 /* Flush out saved messages if necessary. */
3400 if (wParam == mouse_button_timer)
3402 if (saved_mouse_button_msg.msg.hwnd)
3404 post_msg (&saved_mouse_button_msg);
3405 signal_user_input ();
3406 saved_mouse_button_msg.msg.hwnd = 0;
3408 KillTimer (hwnd, mouse_button_timer);
3409 mouse_button_timer = 0;
3411 else if (wParam == mouse_move_timer)
3413 if (saved_mouse_move_msg.msg.hwnd)
3415 post_msg (&saved_mouse_move_msg);
3416 saved_mouse_move_msg.msg.hwnd = 0;
3418 KillTimer (hwnd, mouse_move_timer);
3419 mouse_move_timer = 0;
3421 else if (wParam == menu_free_timer)
3423 KillTimer (hwnd, menu_free_timer);
3424 menu_free_timer = 0;
3425 f = x_window_to_frame (dpyinfo, hwnd);
3426 /* If a popup menu is active, don't wipe its strings. */
3427 if (menubar_in_use
3428 && current_popup_menu == NULL)
3430 /* Free memory used by owner-drawn and help-echo strings. */
3431 w32_free_menu_strings (hwnd);
3432 f->output_data.w32->menubar_active = 0;
3433 menubar_in_use = 0;
3436 return 0;
3438 case WM_NCACTIVATE:
3439 /* Windows doesn't send us focus messages when putting up and
3440 taking down a system popup dialog as for Ctrl-Alt-Del on Windows 95.
3441 The only indication we get that something happened is receiving
3442 this message afterwards. So this is a good time to reset our
3443 keyboard modifiers' state. */
3444 reset_modifiers ();
3445 goto dflt;
3447 case WM_INITMENU:
3448 button_state = 0;
3449 ReleaseCapture ();
3450 /* We must ensure menu bar is fully constructed and up to date
3451 before allowing user interaction with it. To achieve this
3452 we send this message to the lisp thread and wait for a
3453 reply (whose value is not actually needed) to indicate that
3454 the menu bar is now ready for use, so we can now return.
3456 To remain responsive in the meantime, we enter a nested message
3457 loop that can process all other messages.
3459 However, we skip all this if the message results from calling
3460 TrackPopupMenu - in fact, we must NOT attempt to send the lisp
3461 thread a message because it is blocked on us at this point. We
3462 set menubar_active before calling TrackPopupMenu to indicate
3463 this (there is no possibility of confusion with real menubar
3464 being active). */
3466 f = x_window_to_frame (dpyinfo, hwnd);
3467 if (f
3468 && (f->output_data.w32->menubar_active
3469 /* We can receive this message even in the absence of a
3470 menubar (ie. when the system menu is activated) - in this
3471 case we do NOT want to forward the message, otherwise it
3472 will cause the menubar to suddenly appear when the user
3473 had requested it to be turned off! */
3474 || f->output_data.w32->menubar_widget == NULL))
3475 return 0;
3478 deferred_msg msg_buf;
3480 /* Detect if message has already been deferred; in this case
3481 we cannot return any sensible value to ignore this. */
3482 if (find_deferred_msg (hwnd, msg) != NULL)
3483 abort ();
3485 menubar_in_use = 1;
3487 return send_deferred_msg (&msg_buf, hwnd, msg, wParam, lParam);
3490 case WM_EXITMENULOOP:
3491 f = x_window_to_frame (dpyinfo, hwnd);
3493 /* If a menu is still active, check again after a short delay,
3494 since Windows often (always?) sends the WM_EXITMENULOOP
3495 before the corresponding WM_COMMAND message.
3496 Don't do this if a popup menu is active, since it is only
3497 menubar menus that require cleaning up in this way.
3499 if (f && menubar_in_use && current_popup_menu == NULL)
3500 menu_free_timer = SetTimer (hwnd, MENU_FREE_ID, MENU_FREE_DELAY, NULL);
3501 goto dflt;
3503 case WM_MENUSELECT:
3504 /* Direct handling of help_echo in menus. Should be safe now
3505 that we generate the help_echo by placing a help event in the
3506 keyboard buffer. */
3508 HMENU menu = (HMENU) lParam;
3509 UINT menu_item = (UINT) LOWORD (wParam);
3510 UINT flags = (UINT) HIWORD (wParam);
3512 w32_menu_display_help (hwnd, menu, menu_item, flags);
3514 return 0;
3516 case WM_MEASUREITEM:
3517 f = x_window_to_frame (dpyinfo, hwnd);
3518 if (f)
3520 MEASUREITEMSTRUCT * pMis = (MEASUREITEMSTRUCT *) lParam;
3522 if (pMis->CtlType == ODT_MENU)
3524 /* Work out dimensions for popup menu titles. */
3525 char * title = (char *) pMis->itemData;
3526 HDC hdc = GetDC (hwnd);
3527 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
3528 LOGFONT menu_logfont;
3529 HFONT old_font;
3530 SIZE size;
3532 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
3533 menu_logfont.lfWeight = FW_BOLD;
3534 menu_font = CreateFontIndirect (&menu_logfont);
3535 old_font = SelectObject (hdc, menu_font);
3537 pMis->itemHeight = GetSystemMetrics (SM_CYMENUSIZE);
3538 if (title)
3540 if (unicode_append_menu)
3541 GetTextExtentPoint32W (hdc, (WCHAR *) title,
3542 wcslen ((WCHAR *) title),
3543 &size);
3544 else
3545 GetTextExtentPoint32 (hdc, title, strlen (title), &size);
3547 pMis->itemWidth = size.cx;
3548 if (pMis->itemHeight < size.cy)
3549 pMis->itemHeight = size.cy;
3551 else
3552 pMis->itemWidth = 0;
3554 SelectObject (hdc, old_font);
3555 DeleteObject (menu_font);
3556 ReleaseDC (hwnd, hdc);
3557 return TRUE;
3560 return 0;
3562 case WM_DRAWITEM:
3563 f = x_window_to_frame (dpyinfo, hwnd);
3564 if (f)
3566 DRAWITEMSTRUCT * pDis = (DRAWITEMSTRUCT *) lParam;
3568 if (pDis->CtlType == ODT_MENU)
3570 /* Draw popup menu title. */
3571 char * title = (char *) pDis->itemData;
3572 if (title)
3574 HDC hdc = pDis->hDC;
3575 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
3576 LOGFONT menu_logfont;
3577 HFONT old_font;
3579 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
3580 menu_logfont.lfWeight = FW_BOLD;
3581 menu_font = CreateFontIndirect (&menu_logfont);
3582 old_font = SelectObject (hdc, menu_font);
3584 /* Always draw title as if not selected. */
3585 if (unicode_append_menu)
3586 ExtTextOutW (hdc,
3587 pDis->rcItem.left
3588 + GetSystemMetrics (SM_CXMENUCHECK),
3589 pDis->rcItem.top,
3590 ETO_OPAQUE, &pDis->rcItem,
3591 (WCHAR *) title,
3592 wcslen ((WCHAR *) title), NULL);
3593 else
3594 ExtTextOut (hdc,
3595 pDis->rcItem.left
3596 + GetSystemMetrics (SM_CXMENUCHECK),
3597 pDis->rcItem.top,
3598 ETO_OPAQUE, &pDis->rcItem,
3599 title, strlen (title), NULL);
3601 SelectObject (hdc, old_font);
3602 DeleteObject (menu_font);
3604 return TRUE;
3607 return 0;
3609 #if 0
3610 /* Still not right - can't distinguish between clicks in the
3611 client area of the frame from clicks forwarded from the scroll
3612 bars - may have to hook WM_NCHITTEST to remember the mouse
3613 position and then check if it is in the client area ourselves. */
3614 case WM_MOUSEACTIVATE:
3615 /* Discard the mouse click that activates a frame, allowing the
3616 user to click anywhere without changing point (or worse!).
3617 Don't eat mouse clicks on scrollbars though!! */
3618 if (LOWORD (lParam) == HTCLIENT )
3619 return MA_ACTIVATEANDEAT;
3620 goto dflt;
3621 #endif
3623 case WM_MOUSELEAVE:
3624 /* No longer tracking mouse. */
3625 track_mouse_window = NULL;
3627 case WM_ACTIVATEAPP:
3628 case WM_ACTIVATE:
3629 case WM_WINDOWPOSCHANGED:
3630 case WM_SHOWWINDOW:
3631 /* Inform lisp thread that a frame might have just been obscured
3632 or exposed, so should recheck visibility of all frames. */
3633 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3634 goto dflt;
3636 case WM_SETFOCUS:
3637 dpyinfo->faked_key = 0;
3638 reset_modifiers ();
3639 register_hot_keys (hwnd);
3640 goto command;
3641 case WM_KILLFOCUS:
3642 unregister_hot_keys (hwnd);
3643 button_state = 0;
3644 ReleaseCapture ();
3645 /* Relinquish the system caret. */
3646 if (w32_system_caret_hwnd)
3648 w32_visible_system_caret_hwnd = NULL;
3649 w32_system_caret_hwnd = NULL;
3650 DestroyCaret ();
3652 goto command;
3653 case WM_COMMAND:
3654 menubar_in_use = 0;
3655 f = x_window_to_frame (dpyinfo, hwnd);
3656 if (f && HIWORD (wParam) == 0)
3658 if (menu_free_timer)
3660 KillTimer (hwnd, menu_free_timer);
3661 menu_free_timer = 0;
3664 case WM_MOVE:
3665 case WM_SIZE:
3666 command:
3667 wmsg.dwModifiers = w32_get_modifiers ();
3668 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3669 goto dflt;
3671 case WM_DESTROY:
3672 CoUninitialize ();
3673 return 0;
3675 case WM_CLOSE:
3676 wmsg.dwModifiers = w32_get_modifiers ();
3677 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3678 return 0;
3680 case WM_WINDOWPOSCHANGING:
3681 /* Don't restrict the sizing of tip frames. */
3682 if (hwnd == tip_window)
3683 return 0;
3685 WINDOWPLACEMENT wp;
3686 LPWINDOWPOS lppos = (WINDOWPOS *) lParam;
3688 wp.length = sizeof (WINDOWPLACEMENT);
3689 GetWindowPlacement (hwnd, &wp);
3691 if (wp.showCmd != SW_SHOWMINIMIZED && (lppos->flags & SWP_NOSIZE) == 0)
3693 RECT rect;
3694 int wdiff;
3695 int hdiff;
3696 DWORD font_width;
3697 DWORD line_height;
3698 DWORD internal_border;
3699 DWORD scrollbar_extra;
3700 RECT wr;
3702 wp.length = sizeof(wp);
3703 GetWindowRect (hwnd, &wr);
3705 enter_crit ();
3707 font_width = GetWindowLong (hwnd, WND_FONTWIDTH_INDEX);
3708 line_height = GetWindowLong (hwnd, WND_LINEHEIGHT_INDEX);
3709 internal_border = GetWindowLong (hwnd, WND_BORDER_INDEX);
3710 scrollbar_extra = GetWindowLong (hwnd, WND_SCROLLBAR_INDEX);
3712 leave_crit ();
3714 memset (&rect, 0, sizeof (rect));
3715 AdjustWindowRect (&rect, GetWindowLong (hwnd, GWL_STYLE),
3716 GetMenu (hwnd) != NULL);
3718 /* Force width and height of client area to be exact
3719 multiples of the character cell dimensions. */
3720 wdiff = (lppos->cx - (rect.right - rect.left)
3721 - 2 * internal_border - scrollbar_extra)
3722 % font_width;
3723 hdiff = (lppos->cy - (rect.bottom - rect.top)
3724 - 2 * internal_border)
3725 % line_height;
3727 if (wdiff || hdiff)
3729 /* For right/bottom sizing we can just fix the sizes.
3730 However for top/left sizing we will need to fix the X
3731 and Y positions as well. */
3733 int cx_mintrack = GetSystemMetrics (SM_CXMINTRACK);
3734 int cy_mintrack = GetSystemMetrics (SM_CYMINTRACK);
3736 lppos->cx = max (lppos->cx - wdiff, cx_mintrack);
3737 lppos->cy = max (lppos->cy - hdiff, cy_mintrack);
3739 if (wp.showCmd != SW_SHOWMAXIMIZED
3740 && (lppos->flags & SWP_NOMOVE) == 0)
3742 if (lppos->x != wr.left || lppos->y != wr.top)
3744 lppos->x += wdiff;
3745 lppos->y += hdiff;
3747 else
3749 lppos->flags |= SWP_NOMOVE;
3753 return 0;
3758 goto dflt;
3760 case WM_GETMINMAXINFO:
3761 /* Hack to allow resizing the Emacs frame above the screen size.
3762 Note that Windows 9x limits coordinates to 16-bits. */
3763 ((LPMINMAXINFO) lParam)->ptMaxTrackSize.x = 32767;
3764 ((LPMINMAXINFO) lParam)->ptMaxTrackSize.y = 32767;
3765 return 0;
3767 case WM_SETCURSOR:
3768 if (LOWORD (lParam) == HTCLIENT)
3769 return 0;
3771 goto dflt;
3773 case WM_EMACS_SETCURSOR:
3775 Cursor cursor = (Cursor) wParam;
3776 if (cursor)
3777 SetCursor (cursor);
3778 return 0;
3781 case WM_EMACS_CREATESCROLLBAR:
3782 return (LRESULT) w32_createscrollbar ((struct frame *) wParam,
3783 (struct scroll_bar *) lParam);
3785 case WM_EMACS_SHOWWINDOW:
3786 return ShowWindow ((HWND) wParam, (WPARAM) lParam);
3788 case WM_EMACS_SETFOREGROUND:
3790 HWND foreground_window;
3791 DWORD foreground_thread, retval;
3793 /* On NT 5.0, and apparently Windows 98, it is necessary to
3794 attach to the thread that currently has focus in order to
3795 pull the focus away from it. */
3796 foreground_window = GetForegroundWindow ();
3797 foreground_thread = GetWindowThreadProcessId (foreground_window, NULL);
3798 if (!foreground_window
3799 || foreground_thread == GetCurrentThreadId ()
3800 || !AttachThreadInput (GetCurrentThreadId (),
3801 foreground_thread, TRUE))
3802 foreground_thread = 0;
3804 retval = SetForegroundWindow ((HWND) wParam);
3806 /* Detach from the previous foreground thread. */
3807 if (foreground_thread)
3808 AttachThreadInput (GetCurrentThreadId (),
3809 foreground_thread, FALSE);
3811 return retval;
3814 case WM_EMACS_SETWINDOWPOS:
3816 WINDOWPOS * pos = (WINDOWPOS *) wParam;
3817 return SetWindowPos (hwnd, pos->hwndInsertAfter,
3818 pos->x, pos->y, pos->cx, pos->cy, pos->flags);
3821 case WM_EMACS_DESTROYWINDOW:
3822 DragAcceptFiles ((HWND) wParam, FALSE);
3823 return DestroyWindow ((HWND) wParam);
3825 case WM_EMACS_HIDE_CARET:
3826 return HideCaret (hwnd);
3828 case WM_EMACS_SHOW_CARET:
3829 return ShowCaret (hwnd);
3831 case WM_EMACS_DESTROY_CARET:
3832 w32_system_caret_hwnd = NULL;
3833 w32_visible_system_caret_hwnd = NULL;
3834 return DestroyCaret ();
3836 case WM_EMACS_TRACK_CARET:
3837 /* If there is currently no system caret, create one. */
3838 if (w32_system_caret_hwnd == NULL)
3840 /* Use the default caret width, and avoid changing it
3841 unneccesarily, as it confuses screen reader software. */
3842 w32_system_caret_hwnd = hwnd;
3843 CreateCaret (hwnd, NULL, 0,
3844 w32_system_caret_height);
3847 if (!SetCaretPos (w32_system_caret_x, w32_system_caret_y))
3848 return 0;
3849 /* Ensure visible caret gets turned on when requested. */
3850 else if (w32_use_visible_system_caret
3851 && w32_visible_system_caret_hwnd != hwnd)
3853 w32_visible_system_caret_hwnd = hwnd;
3854 return ShowCaret (hwnd);
3856 /* Ensure visible caret gets turned off when requested. */
3857 else if (!w32_use_visible_system_caret
3858 && w32_visible_system_caret_hwnd)
3860 w32_visible_system_caret_hwnd = NULL;
3861 return HideCaret (hwnd);
3863 else
3864 return 1;
3866 case WM_EMACS_TRACKPOPUPMENU:
3868 UINT flags;
3869 POINT *pos;
3870 int retval;
3871 pos = (POINT *)lParam;
3872 flags = TPM_CENTERALIGN;
3873 if (button_state & LMOUSE)
3874 flags |= TPM_LEFTBUTTON;
3875 else if (button_state & RMOUSE)
3876 flags |= TPM_RIGHTBUTTON;
3878 /* Remember we did a SetCapture on the initial mouse down event,
3879 so for safety, we make sure the capture is cancelled now. */
3880 ReleaseCapture ();
3881 button_state = 0;
3883 /* Use menubar_active to indicate that WM_INITMENU is from
3884 TrackPopupMenu below, and should be ignored. */
3885 f = x_window_to_frame (dpyinfo, hwnd);
3886 if (f)
3887 f->output_data.w32->menubar_active = 1;
3889 if (TrackPopupMenu ((HMENU)wParam, flags, pos->x, pos->y,
3890 0, hwnd, NULL))
3892 MSG amsg;
3893 /* Eat any mouse messages during popupmenu */
3894 while (PeekMessage (&amsg, hwnd, WM_MOUSEFIRST, WM_MOUSELAST,
3895 PM_REMOVE));
3896 /* Get the menu selection, if any */
3897 if (PeekMessage (&amsg, hwnd, WM_COMMAND, WM_COMMAND, PM_REMOVE))
3899 retval = LOWORD (amsg.wParam);
3901 else
3903 retval = 0;
3906 else
3908 retval = -1;
3911 return retval;
3914 default:
3915 /* Check for messages registered at runtime. */
3916 if (msg == msh_mousewheel)
3918 wmsg.dwModifiers = w32_get_modifiers ();
3919 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3920 signal_user_input ();
3921 return 0;
3924 dflt:
3925 return DefWindowProc (hwnd, msg, wParam, lParam);
3929 /* The most common default return code for handled messages is 0. */
3930 return 0;
3933 static void
3934 my_create_window (f)
3935 struct frame * f;
3937 MSG msg;
3939 if (!PostThreadMessage (dwWindowsThreadId, WM_EMACS_CREATEWINDOW, (WPARAM)f, 0))
3940 abort ();
3941 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
3945 /* Create a tooltip window. Unlike my_create_window, we do not do this
3946 indirectly via the Window thread, as we do not need to process Window
3947 messages for the tooltip. Creating tooltips indirectly also creates
3948 deadlocks when tooltips are created for menu items. */
3949 static void
3950 my_create_tip_window (f)
3951 struct frame *f;
3953 RECT rect;
3955 rect.left = rect.top = 0;
3956 rect.right = FRAME_PIXEL_WIDTH (f);
3957 rect.bottom = FRAME_PIXEL_HEIGHT (f);
3959 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
3960 FRAME_EXTERNAL_MENU_BAR (f));
3962 tip_window = FRAME_W32_WINDOW (f)
3963 = CreateWindow (EMACS_CLASS,
3964 f->namebuf,
3965 f->output_data.w32->dwStyle,
3966 f->left_pos,
3967 f->top_pos,
3968 rect.right - rect.left,
3969 rect.bottom - rect.top,
3970 FRAME_W32_WINDOW (SELECTED_FRAME ()), /* owner */
3971 NULL,
3972 hinst,
3973 NULL);
3975 if (tip_window)
3977 SetWindowLong (tip_window, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
3978 SetWindowLong (tip_window, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
3979 SetWindowLong (tip_window, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
3980 SetWindowLong (tip_window, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
3982 /* Tip frames have no scrollbars. */
3983 SetWindowLong (tip_window, WND_SCROLLBAR_INDEX, 0);
3985 /* Do this to discard the default setting specified by our parent. */
3986 ShowWindow (tip_window, SW_HIDE);
3991 /* Create and set up the w32 window for frame F. */
3993 static void
3994 w32_window (f, window_prompting, minibuffer_only)
3995 struct frame *f;
3996 long window_prompting;
3997 int minibuffer_only;
3999 BLOCK_INPUT;
4001 /* Use the resource name as the top-level window name
4002 for looking up resources. Make a non-Lisp copy
4003 for the window manager, so GC relocation won't bother it.
4005 Elsewhere we specify the window name for the window manager. */
4008 char *str = (char *) SDATA (Vx_resource_name);
4009 f->namebuf = (char *) xmalloc (strlen (str) + 1);
4010 strcpy (f->namebuf, str);
4013 my_create_window (f);
4015 validate_x_resource_name ();
4017 /* x_set_name normally ignores requests to set the name if the
4018 requested name is the same as the current name. This is the one
4019 place where that assumption isn't correct; f->name is set, but
4020 the server hasn't been told. */
4022 Lisp_Object name;
4023 int explicit = f->explicit_name;
4025 f->explicit_name = 0;
4026 name = f->name;
4027 f->name = Qnil;
4028 x_set_name (f, name, explicit);
4031 UNBLOCK_INPUT;
4033 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
4034 initialize_frame_menubar (f);
4036 if (FRAME_W32_WINDOW (f) == 0)
4037 error ("Unable to create window");
4040 /* Handle the icon stuff for this window. Perhaps later we might
4041 want an x_set_icon_position which can be called interactively as
4042 well. */
4044 static void
4045 x_icon (f, parms)
4046 struct frame *f;
4047 Lisp_Object parms;
4049 Lisp_Object icon_x, icon_y;
4051 /* Set the position of the icon. Note that Windows 95 groups all
4052 icons in the tray. */
4053 icon_x = w32_get_arg (parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
4054 icon_y = w32_get_arg (parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
4055 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
4057 CHECK_NUMBER (icon_x);
4058 CHECK_NUMBER (icon_y);
4060 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
4061 error ("Both left and top icon corners of icon must be specified");
4063 BLOCK_INPUT;
4065 if (! EQ (icon_x, Qunbound))
4066 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
4068 #if 0 /* TODO */
4069 /* Start up iconic or window? */
4070 x_wm_set_window_state
4071 (f, (EQ (w32_get_arg (parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL), Qicon)
4072 ? IconicState
4073 : NormalState));
4075 x_text_icon (f, (char *) SDATA ((!NILP (f->icon_name)
4076 ? f->icon_name
4077 : f->name)));
4078 #endif
4080 UNBLOCK_INPUT;
4084 static void
4085 x_make_gc (f)
4086 struct frame *f;
4088 XGCValues gc_values;
4090 BLOCK_INPUT;
4092 /* Create the GC's of this frame.
4093 Note that many default values are used. */
4095 /* Normal video */
4096 gc_values.font = FRAME_FONT (f);
4098 /* Cursor has cursor-color background, background-color foreground. */
4099 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
4100 gc_values.background = f->output_data.w32->cursor_pixel;
4101 f->output_data.w32->cursor_gc
4102 = XCreateGC (NULL, FRAME_W32_WINDOW (f),
4103 (GCFont | GCForeground | GCBackground),
4104 &gc_values);
4106 /* Reliefs. */
4107 f->output_data.w32->white_relief.gc = 0;
4108 f->output_data.w32->black_relief.gc = 0;
4110 UNBLOCK_INPUT;
4114 /* Handler for signals raised during x_create_frame and
4115 x_create_top_frame. FRAME is the frame which is partially
4116 constructed. */
4118 static Lisp_Object
4119 unwind_create_frame (frame)
4120 Lisp_Object frame;
4122 struct frame *f = XFRAME (frame);
4124 /* If frame is ``official'', nothing to do. */
4125 if (!CONSP (Vframe_list) || !EQ (XCAR (Vframe_list), frame))
4127 #ifdef GLYPH_DEBUG
4128 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4129 #endif
4131 x_free_frame_resources (f);
4133 /* Check that reference counts are indeed correct. */
4134 xassert (dpyinfo->reference_count == dpyinfo_refcount);
4135 xassert (dpyinfo->image_cache->refcount == image_cache_refcount);
4137 return Qt;
4140 return Qnil;
4144 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
4145 1, 1, 0,
4146 doc: /* Make a new window, which is called a \"frame\" in Emacs terms.
4147 Returns an Emacs frame object.
4148 PARAMETERS is an alist of frame parameters.
4149 If the parameters specify that the frame should not have a minibuffer,
4150 and do not specify a specific minibuffer window to use,
4151 then `default-minibuffer-frame' must be a frame whose minibuffer can
4152 be shared by the new frame.
4154 This function is an internal primitive--use `make-frame' instead. */)
4155 (parameters)
4156 Lisp_Object parameters;
4158 struct frame *f;
4159 Lisp_Object frame, tem;
4160 Lisp_Object name;
4161 int minibuffer_only = 0;
4162 long window_prompting = 0;
4163 int width, height;
4164 int count = SPECPDL_INDEX ();
4165 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4166 Lisp_Object display;
4167 struct w32_display_info *dpyinfo = NULL;
4168 Lisp_Object parent;
4169 struct kboard *kb;
4171 check_w32 ();
4173 /* Use this general default value to start with
4174 until we know if this frame has a specified name. */
4175 Vx_resource_name = Vinvocation_name;
4177 display = w32_get_arg (parameters, Qdisplay, 0, 0, RES_TYPE_STRING);
4178 if (EQ (display, Qunbound))
4179 display = Qnil;
4180 dpyinfo = check_x_display_info (display);
4181 #ifdef MULTI_KBOARD
4182 kb = dpyinfo->kboard;
4183 #else
4184 kb = &the_only_kboard;
4185 #endif
4187 name = w32_get_arg (parameters, Qname, "name", "Name", RES_TYPE_STRING);
4188 if (!STRINGP (name)
4189 && ! EQ (name, Qunbound)
4190 && ! NILP (name))
4191 error ("Invalid frame name--not a string or nil");
4193 if (STRINGP (name))
4194 Vx_resource_name = name;
4196 /* See if parent window is specified. */
4197 parent = w32_get_arg (parameters, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
4198 if (EQ (parent, Qunbound))
4199 parent = Qnil;
4200 if (! NILP (parent))
4201 CHECK_NUMBER (parent);
4203 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
4204 /* No need to protect DISPLAY because that's not used after passing
4205 it to make_frame_without_minibuffer. */
4206 frame = Qnil;
4207 GCPRO4 (parameters, parent, name, frame);
4208 tem = w32_get_arg (parameters, Qminibuffer, "minibuffer", "Minibuffer",
4209 RES_TYPE_SYMBOL);
4210 if (EQ (tem, Qnone) || NILP (tem))
4211 f = make_frame_without_minibuffer (Qnil, kb, display);
4212 else if (EQ (tem, Qonly))
4214 f = make_minibuffer_frame ();
4215 minibuffer_only = 1;
4217 else if (WINDOWP (tem))
4218 f = make_frame_without_minibuffer (tem, kb, display);
4219 else
4220 f = make_frame (1);
4222 XSETFRAME (frame, f);
4224 /* Note that Windows does support scroll bars. */
4225 FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;
4227 /* By default, make scrollbars the system standard width. */
4228 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = GetSystemMetrics (SM_CXVSCROLL);
4230 f->output_method = output_w32;
4231 f->output_data.w32 =
4232 (struct w32_output *) xmalloc (sizeof (struct w32_output));
4233 bzero (f->output_data.w32, sizeof (struct w32_output));
4234 FRAME_FONTSET (f) = -1;
4235 record_unwind_protect (unwind_create_frame, frame);
4237 f->icon_name
4238 = w32_get_arg (parameters, Qicon_name, "iconName", "Title", RES_TYPE_STRING);
4239 if (! STRINGP (f->icon_name))
4240 f->icon_name = Qnil;
4242 /* FRAME_W32_DISPLAY_INFO (f) = dpyinfo; */
4243 #ifdef MULTI_KBOARD
4244 FRAME_KBOARD (f) = kb;
4245 #endif
4247 /* Specify the parent under which to make this window. */
4249 if (!NILP (parent))
4251 f->output_data.w32->parent_desc = (Window) XFASTINT (parent);
4252 f->output_data.w32->explicit_parent = 1;
4254 else
4256 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
4257 f->output_data.w32->explicit_parent = 0;
4260 /* Set the name; the functions to which we pass f expect the name to
4261 be set. */
4262 if (EQ (name, Qunbound) || NILP (name))
4264 f->name = build_string (dpyinfo->w32_id_name);
4265 f->explicit_name = 0;
4267 else
4269 f->name = name;
4270 f->explicit_name = 1;
4271 /* use the frame's title when getting resources for this frame. */
4272 specbind (Qx_resource_name, name);
4275 /* Extract the window parameters from the supplied values
4276 that are needed to determine window geometry. */
4278 Lisp_Object font;
4280 font = w32_get_arg (parameters, Qfont, "font", "Font", RES_TYPE_STRING);
4282 BLOCK_INPUT;
4283 /* First, try whatever font the caller has specified. */
4284 if (STRINGP (font))
4286 tem = Fquery_fontset (font, Qnil);
4287 if (STRINGP (tem))
4288 font = x_new_fontset (f, SDATA (tem));
4289 else
4290 font = x_new_font (f, SDATA (font));
4292 /* Try out a font which we hope has bold and italic variations. */
4293 if (!STRINGP (font))
4294 font = x_new_font (f, "-*-Courier New-normal-r-*-*-*-100-*-*-c-*-iso8859-1");
4295 if (! STRINGP (font))
4296 font = x_new_font (f, "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1");
4297 /* If those didn't work, look for something which will at least work. */
4298 if (! STRINGP (font))
4299 font = x_new_font (f, "-*-Fixedsys-normal-r-*-*-12-*-*-*-c-*-iso8859-1");
4300 UNBLOCK_INPUT;
4301 if (! STRINGP (font))
4302 font = build_string ("Fixedsys");
4304 x_default_parameter (f, parameters, Qfont, font,
4305 "font", "Font", RES_TYPE_STRING);
4308 x_default_parameter (f, parameters, Qborder_width, make_number (2),
4309 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
4310 /* This defaults to 2 in order to match xterm. We recognize either
4311 internalBorderWidth or internalBorder (which is what xterm calls
4312 it). */
4313 if (NILP (Fassq (Qinternal_border_width, parameters)))
4315 Lisp_Object value;
4317 value = w32_get_arg (parameters, Qinternal_border_width,
4318 "internalBorder", "InternalBorder", RES_TYPE_NUMBER);
4319 if (! EQ (value, Qunbound))
4320 parameters = Fcons (Fcons (Qinternal_border_width, value),
4321 parameters);
4323 /* Default internalBorderWidth to 0 on Windows to match other programs. */
4324 x_default_parameter (f, parameters, Qinternal_border_width, make_number (0),
4325 "internalBorderWidth", "InternalBorder", RES_TYPE_NUMBER);
4326 x_default_parameter (f, parameters, Qvertical_scroll_bars, Qright,
4327 "verticalScrollBars", "ScrollBars", RES_TYPE_SYMBOL);
4329 /* Also do the stuff which must be set before the window exists. */
4330 x_default_parameter (f, parameters, Qforeground_color, build_string ("black"),
4331 "foreground", "Foreground", RES_TYPE_STRING);
4332 x_default_parameter (f, parameters, Qbackground_color, build_string ("white"),
4333 "background", "Background", RES_TYPE_STRING);
4334 x_default_parameter (f, parameters, Qmouse_color, build_string ("black"),
4335 "pointerColor", "Foreground", RES_TYPE_STRING);
4336 x_default_parameter (f, parameters, Qcursor_color, build_string ("black"),
4337 "cursorColor", "Foreground", RES_TYPE_STRING);
4338 x_default_parameter (f, parameters, Qborder_color, build_string ("black"),
4339 "borderColor", "BorderColor", RES_TYPE_STRING);
4340 x_default_parameter (f, parameters, Qscreen_gamma, Qnil,
4341 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
4342 x_default_parameter (f, parameters, Qline_spacing, Qnil,
4343 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
4344 x_default_parameter (f, parameters, Qleft_fringe, Qnil,
4345 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
4346 x_default_parameter (f, parameters, Qright_fringe, Qnil,
4347 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
4350 /* Init faces before x_default_parameter is called for scroll-bar
4351 parameters because that function calls x_set_scroll_bar_width,
4352 which calls change_frame_size, which calls Fset_window_buffer,
4353 which runs hooks, which call Fvertical_motion. At the end, we
4354 end up in init_iterator with a null face cache, which should not
4355 happen. */
4356 init_frame_faces (f);
4358 x_default_parameter (f, parameters, Qmenu_bar_lines, make_number (1),
4359 "menuBar", "MenuBar", RES_TYPE_NUMBER);
4360 x_default_parameter (f, parameters, Qtool_bar_lines, make_number (1),
4361 "toolBar", "ToolBar", RES_TYPE_NUMBER);
4363 x_default_parameter (f, parameters, Qbuffer_predicate, Qnil,
4364 "bufferPredicate", "BufferPredicate", RES_TYPE_SYMBOL);
4365 x_default_parameter (f, parameters, Qtitle, Qnil,
4366 "title", "Title", RES_TYPE_STRING);
4367 x_default_parameter (f, parameters, Qfullscreen, Qnil,
4368 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
4370 f->output_data.w32->dwStyle = WS_OVERLAPPEDWINDOW;
4371 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
4373 f->output_data.w32->text_cursor = w32_load_cursor (IDC_IBEAM);
4374 f->output_data.w32->nontext_cursor = w32_load_cursor (IDC_ARROW);
4375 f->output_data.w32->modeline_cursor = w32_load_cursor (IDC_ARROW);
4376 f->output_data.w32->hand_cursor = w32_load_cursor (IDC_HAND);
4377 f->output_data.w32->hourglass_cursor = w32_load_cursor (IDC_WAIT);
4378 f->output_data.w32->horizontal_drag_cursor = w32_load_cursor (IDC_SIZEWE);
4380 window_prompting = x_figure_window_size (f, parameters, 1);
4382 tem = w32_get_arg (parameters, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
4383 f->no_split = minibuffer_only || EQ (tem, Qt);
4385 w32_window (f, window_prompting, minibuffer_only);
4386 x_icon (f, parameters);
4388 x_make_gc (f);
4390 /* Now consider the frame official. */
4391 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
4392 Vframe_list = Fcons (frame, Vframe_list);
4394 /* We need to do this after creating the window, so that the
4395 icon-creation functions can say whose icon they're describing. */
4396 x_default_parameter (f, parameters, Qicon_type, Qnil,
4397 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
4399 x_default_parameter (f, parameters, Qauto_raise, Qnil,
4400 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4401 x_default_parameter (f, parameters, Qauto_lower, Qnil,
4402 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4403 x_default_parameter (f, parameters, Qcursor_type, Qbox,
4404 "cursorType", "CursorType", RES_TYPE_SYMBOL);
4405 x_default_parameter (f, parameters, Qscroll_bar_width, Qnil,
4406 "scrollBarWidth", "ScrollBarWidth", RES_TYPE_NUMBER);
4408 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
4409 Change will not be effected unless different from the current
4410 FRAME_LINES (f). */
4411 width = FRAME_COLS (f);
4412 height = FRAME_LINES (f);
4414 FRAME_LINES (f) = 0;
4415 SET_FRAME_COLS (f, 0);
4416 change_frame_size (f, height, width, 1, 0, 0);
4418 /* Tell the server what size and position, etc, we want, and how
4419 badly we want them. This should be done after we have the menu
4420 bar so that its size can be taken into account. */
4421 BLOCK_INPUT;
4422 x_wm_set_size_hint (f, window_prompting, 0);
4423 UNBLOCK_INPUT;
4425 /* Make the window appear on the frame and enable display, unless
4426 the caller says not to. However, with explicit parent, Emacs
4427 cannot control visibility, so don't try. */
4428 if (! f->output_data.w32->explicit_parent)
4430 Lisp_Object visibility;
4432 visibility = w32_get_arg (parameters, Qvisibility, 0, 0, RES_TYPE_SYMBOL);
4433 if (EQ (visibility, Qunbound))
4434 visibility = Qt;
4436 if (EQ (visibility, Qicon))
4437 x_iconify_frame (f);
4438 else if (! NILP (visibility))
4439 x_make_frame_visible (f);
4440 else
4441 /* Must have been Qnil. */
4444 UNGCPRO;
4446 /* Make sure windows on this frame appear in calls to next-window
4447 and similar functions. */
4448 Vwindow_list = Qnil;
4450 return unbind_to (count, frame);
4453 /* FRAME is used only to get a handle on the X display. We don't pass the
4454 display info directly because we're called from frame.c, which doesn't
4455 know about that structure. */
4456 Lisp_Object
4457 x_get_focus_frame (frame)
4458 struct frame *frame;
4460 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (frame);
4461 Lisp_Object xfocus;
4462 if (! dpyinfo->w32_focus_frame)
4463 return Qnil;
4465 XSETFRAME (xfocus, dpyinfo->w32_focus_frame);
4466 return xfocus;
4469 DEFUN ("w32-focus-frame", Fw32_focus_frame, Sw32_focus_frame, 1, 1, 0,
4470 doc: /* Give FRAME input focus, raising to foreground if necessary. */)
4471 (frame)
4472 Lisp_Object frame;
4474 x_focus_on_frame (check_x_frame (frame));
4475 return Qnil;
4479 /* Return the charset portion of a font name. */
4480 char * xlfd_charset_of_font (char * fontname)
4482 char *charset, *encoding;
4484 encoding = strrchr(fontname, '-');
4485 if (!encoding || encoding == fontname)
4486 return NULL;
4488 for (charset = encoding - 1; charset >= fontname; charset--)
4489 if (*charset == '-')
4490 break;
4492 if (charset == fontname || strcmp(charset, "-*-*") == 0)
4493 return NULL;
4495 return charset + 1;
4498 struct font_info *w32_load_bdf_font (struct frame *f, char *fontname,
4499 int size, char* filename);
4500 static Lisp_Object w32_list_bdf_fonts (Lisp_Object pattern, int max_names);
4501 static BOOL w32_to_x_font (LOGFONT * lplf, char * lpxstr, int len,
4502 char * charset);
4503 static BOOL x_to_w32_font (char *lpxstr, LOGFONT *lplogfont);
4505 static struct font_info *
4506 w32_load_system_font (f,fontname,size)
4507 struct frame *f;
4508 char * fontname;
4509 int size;
4511 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4512 Lisp_Object font_names;
4514 /* Get a list of all the fonts that match this name. Once we
4515 have a list of matching fonts, we compare them against the fonts
4516 we already have loaded by comparing names. */
4517 font_names = w32_list_fonts (f, build_string (fontname), size, 100);
4519 if (!NILP (font_names))
4521 Lisp_Object tail;
4522 int i;
4524 /* First check if any are already loaded, as that is cheaper
4525 than loading another one. */
4526 for (i = 0; i < dpyinfo->n_fonts; i++)
4527 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
4528 if (dpyinfo->font_table[i].name
4529 && (!strcmp (dpyinfo->font_table[i].name,
4530 SDATA (XCAR (tail)))
4531 || !strcmp (dpyinfo->font_table[i].full_name,
4532 SDATA (XCAR (tail)))))
4533 return (dpyinfo->font_table + i);
4535 fontname = (char *) SDATA (XCAR (font_names));
4537 else if (w32_strict_fontnames)
4539 /* If EnumFontFamiliesEx was available, we got a full list of
4540 fonts back so stop now to avoid the possibility of loading a
4541 random font. If we had to fall back to EnumFontFamilies, the
4542 list is incomplete, so continue whether the font we want was
4543 listed or not. */
4544 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
4545 FARPROC enum_font_families_ex
4546 = GetProcAddress (gdi32, "EnumFontFamiliesExA");
4547 if (enum_font_families_ex)
4548 return NULL;
4551 /* Load the font and add it to the table. */
4553 char *full_name, *encoding, *charset;
4554 XFontStruct *font;
4555 struct font_info *fontp;
4556 LOGFONT lf;
4557 BOOL ok;
4558 int codepage;
4559 int i;
4561 if (!fontname || !x_to_w32_font (fontname, &lf))
4562 return (NULL);
4564 if (!*lf.lfFaceName)
4565 /* If no name was specified for the font, we get a random font
4566 from CreateFontIndirect - this is not particularly
4567 desirable, especially since CreateFontIndirect does not
4568 fill out the missing name in lf, so we never know what we
4569 ended up with. */
4570 return NULL;
4572 lf.lfQuality = DEFAULT_QUALITY;
4574 font = (XFontStruct *) xmalloc (sizeof (XFontStruct));
4575 bzero (font, sizeof (*font));
4577 /* Set bdf to NULL to indicate that this is a Windows font. */
4578 font->bdf = NULL;
4580 BLOCK_INPUT;
4582 font->hfont = CreateFontIndirect (&lf);
4584 if (font->hfont == NULL)
4586 ok = FALSE;
4588 else
4590 HDC hdc;
4591 HANDLE oldobj;
4593 codepage = w32_codepage_for_font (fontname);
4595 hdc = GetDC (dpyinfo->root_window);
4596 oldobj = SelectObject (hdc, font->hfont);
4598 ok = GetTextMetrics (hdc, &font->tm);
4599 if (codepage == CP_UNICODE)
4600 font->double_byte_p = 1;
4601 else
4603 /* Unfortunately, some fonts (eg. MingLiU, a big5 ttf font)
4604 don't report themselves as double byte fonts, when
4605 patently they are. So instead of trusting
4606 GetFontLanguageInfo, we check the properties of the
4607 codepage directly, since that is ultimately what we are
4608 working from anyway. */
4609 /* font->double_byte_p = GetFontLanguageInfo(hdc) & GCP_DBCS; */
4610 CPINFO cpi = {0};
4611 GetCPInfo (codepage, &cpi);
4612 font->double_byte_p = cpi.MaxCharSize > 1;
4615 SelectObject (hdc, oldobj);
4616 ReleaseDC (dpyinfo->root_window, hdc);
4617 /* Fill out details in lf according to the font that was
4618 actually loaded. */
4619 lf.lfHeight = font->tm.tmInternalLeading - font->tm.tmHeight;
4620 lf.lfWidth = font->tm.tmMaxCharWidth;
4621 lf.lfWeight = font->tm.tmWeight;
4622 lf.lfItalic = font->tm.tmItalic;
4623 lf.lfCharSet = font->tm.tmCharSet;
4624 lf.lfPitchAndFamily = ((font->tm.tmPitchAndFamily & TMPF_FIXED_PITCH)
4625 ? VARIABLE_PITCH : FIXED_PITCH);
4626 lf.lfOutPrecision = ((font->tm.tmPitchAndFamily & TMPF_VECTOR)
4627 ? OUT_STROKE_PRECIS : OUT_STRING_PRECIS);
4629 w32_cache_char_metrics (font);
4632 UNBLOCK_INPUT;
4634 if (!ok)
4636 w32_unload_font (dpyinfo, font);
4637 return (NULL);
4640 /* Find a free slot in the font table. */
4641 for (i = 0; i < dpyinfo->n_fonts; ++i)
4642 if (dpyinfo->font_table[i].name == NULL)
4643 break;
4645 /* If no free slot found, maybe enlarge the font table. */
4646 if (i == dpyinfo->n_fonts
4647 && dpyinfo->n_fonts == dpyinfo->font_table_size)
4649 int sz;
4650 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
4651 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
4652 dpyinfo->font_table
4653 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
4656 fontp = dpyinfo->font_table + i;
4657 if (i == dpyinfo->n_fonts)
4658 ++dpyinfo->n_fonts;
4660 /* Now fill in the slots of *FONTP. */
4661 BLOCK_INPUT;
4662 bzero (fontp, sizeof (*fontp));
4663 fontp->font = font;
4664 fontp->font_idx = i;
4665 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
4666 bcopy (fontname, fontp->name, strlen (fontname) + 1);
4668 if (lf.lfPitchAndFamily == FIXED_PITCH)
4670 /* Fixed width font. */
4671 fontp->average_width = fontp->space_width = FONT_WIDTH (font);
4673 else
4675 wchar_t space = 32;
4676 XCharStruct* pcm;
4677 pcm = w32_per_char_metric (font, &space, ANSI_FONT);
4678 if (pcm)
4679 fontp->space_width = pcm->width;
4680 else
4681 fontp->space_width = FONT_WIDTH (font);
4683 fontp->average_width = font->tm.tmAveCharWidth;
4686 charset = xlfd_charset_of_font (fontname);
4688 /* Cache the W32 codepage for a font. This makes w32_encode_char
4689 (called for every glyph during redisplay) much faster. */
4690 fontp->codepage = codepage;
4692 /* Work out the font's full name. */
4693 full_name = (char *)xmalloc (100);
4694 if (full_name && w32_to_x_font (&lf, full_name, 100, charset))
4695 fontp->full_name = full_name;
4696 else
4698 /* If all else fails - just use the name we used to load it. */
4699 xfree (full_name);
4700 fontp->full_name = fontp->name;
4703 fontp->size = FONT_WIDTH (font);
4704 fontp->height = FONT_HEIGHT (font);
4706 /* The slot `encoding' specifies how to map a character
4707 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
4708 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
4709 (0:0x20..0x7F, 1:0xA0..0xFF,
4710 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
4711 2:0xA020..0xFF7F). For the moment, we don't know which charset
4712 uses this font. So, we set information in fontp->encoding[1]
4713 which is never used by any charset. If mapping can't be
4714 decided, set FONT_ENCODING_NOT_DECIDED. */
4716 /* SJIS fonts need to be set to type 4, all others seem to work as
4717 type FONT_ENCODING_NOT_DECIDED. */
4718 encoding = strrchr (fontp->name, '-');
4719 if (encoding && strnicmp (encoding+1, "sjis", 4) == 0)
4720 fontp->encoding[1] = 4;
4721 else
4722 fontp->encoding[1] = FONT_ENCODING_NOT_DECIDED;
4724 /* The following three values are set to 0 under W32, which is
4725 what they get set to if XGetFontProperty fails under X. */
4726 fontp->baseline_offset = 0;
4727 fontp->relative_compose = 0;
4728 fontp->default_ascent = 0;
4730 /* Set global flag fonts_changed_p to non-zero if the font loaded
4731 has a character with a smaller width than any other character
4732 before, or if the font loaded has a smaller height than any
4733 other font loaded before. If this happens, it will make a
4734 glyph matrix reallocation necessary. */
4735 fonts_changed_p |= x_compute_min_glyph_bounds (f);
4736 UNBLOCK_INPUT;
4737 return fontp;
4741 /* Load font named FONTNAME of size SIZE for frame F, and return a
4742 pointer to the structure font_info while allocating it dynamically.
4743 If loading fails, return NULL. */
4744 struct font_info *
4745 w32_load_font (f,fontname,size)
4746 struct frame *f;
4747 char * fontname;
4748 int size;
4750 Lisp_Object bdf_fonts;
4751 struct font_info *retval = NULL;
4752 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4754 bdf_fonts = w32_list_bdf_fonts (build_string (fontname), 1);
4756 while (!retval && CONSP (bdf_fonts))
4758 char *bdf_name, *bdf_file;
4759 Lisp_Object bdf_pair;
4760 int i;
4762 bdf_name = SDATA (XCAR (bdf_fonts));
4763 bdf_pair = Fassoc (XCAR (bdf_fonts), Vw32_bdf_filename_alist);
4764 bdf_file = SDATA (XCDR (bdf_pair));
4766 // If the font is already loaded, do not load it again.
4767 for (i = 0; i < dpyinfo->n_fonts; i++)
4769 if ((dpyinfo->font_table[i].name
4770 && !strcmp (dpyinfo->font_table[i].name, bdf_name))
4771 || (dpyinfo->font_table[i].full_name
4772 && !strcmp (dpyinfo->font_table[i].full_name, bdf_name)))
4773 return dpyinfo->font_table + i;
4776 retval = w32_load_bdf_font (f, bdf_name, size, bdf_file);
4778 bdf_fonts = XCDR (bdf_fonts);
4781 if (retval)
4782 return retval;
4784 return w32_load_system_font(f, fontname, size);
4788 void
4789 w32_unload_font (dpyinfo, font)
4790 struct w32_display_info *dpyinfo;
4791 XFontStruct * font;
4793 if (font)
4795 if (font->per_char) xfree (font->per_char);
4796 if (font->bdf) w32_free_bdf_font (font->bdf);
4798 if (font->hfont) DeleteObject(font->hfont);
4799 xfree (font);
4803 /* The font conversion stuff between x and w32 */
4805 /* X font string is as follows (from faces.el)
4806 * (let ((- "[-?]")
4807 * (foundry "[^-]+")
4808 * (family "[^-]+")
4809 * (weight "\\(bold\\|demibold\\|medium\\)") ; 1
4810 * (weight\? "\\([^-]*\\)") ; 1
4811 * (slant "\\([ior]\\)") ; 2
4812 * (slant\? "\\([^-]?\\)") ; 2
4813 * (swidth "\\([^-]*\\)") ; 3
4814 * (adstyle "[^-]*") ; 4
4815 * (pixelsize "[0-9]+")
4816 * (pointsize "[0-9][0-9]+")
4817 * (resx "[0-9][0-9]+")
4818 * (resy "[0-9][0-9]+")
4819 * (spacing "[cmp?*]")
4820 * (avgwidth "[0-9]+")
4821 * (registry "[^-]+")
4822 * (encoding "[^-]+")
4826 static LONG
4827 x_to_w32_weight (lpw)
4828 char * lpw;
4830 if (!lpw) return (FW_DONTCARE);
4832 if (stricmp (lpw,"heavy") == 0) return FW_HEAVY;
4833 else if (stricmp (lpw,"extrabold") == 0) return FW_EXTRABOLD;
4834 else if (stricmp (lpw,"bold") == 0) return FW_BOLD;
4835 else if (stricmp (lpw,"demibold") == 0) return FW_SEMIBOLD;
4836 else if (stricmp (lpw,"semibold") == 0) return FW_SEMIBOLD;
4837 else if (stricmp (lpw,"medium") == 0) return FW_MEDIUM;
4838 else if (stricmp (lpw,"normal") == 0) return FW_NORMAL;
4839 else if (stricmp (lpw,"light") == 0) return FW_LIGHT;
4840 else if (stricmp (lpw,"extralight") == 0) return FW_EXTRALIGHT;
4841 else if (stricmp (lpw,"thin") == 0) return FW_THIN;
4842 else
4843 return FW_DONTCARE;
4847 static char *
4848 w32_to_x_weight (fnweight)
4849 int fnweight;
4851 if (fnweight >= FW_HEAVY) return "heavy";
4852 if (fnweight >= FW_EXTRABOLD) return "extrabold";
4853 if (fnweight >= FW_BOLD) return "bold";
4854 if (fnweight >= FW_SEMIBOLD) return "demibold";
4855 if (fnweight >= FW_MEDIUM) return "medium";
4856 if (fnweight >= FW_NORMAL) return "normal";
4857 if (fnweight >= FW_LIGHT) return "light";
4858 if (fnweight >= FW_EXTRALIGHT) return "extralight";
4859 if (fnweight >= FW_THIN) return "thin";
4860 else
4861 return "*";
4864 static LONG
4865 x_to_w32_charset (lpcs)
4866 char * lpcs;
4868 Lisp_Object this_entry, w32_charset;
4869 char *charset;
4870 int len = strlen (lpcs);
4872 /* Support "*-#nnn" format for unknown charsets. */
4873 if (strncmp (lpcs, "*-#", 3) == 0)
4874 return atoi (lpcs + 3);
4876 /* Handle wildcards by ignoring them; eg. treat "big5*-*" as "big5". */
4877 charset = alloca (len + 1);
4878 strcpy (charset, lpcs);
4879 lpcs = strchr (charset, '*');
4880 if (lpcs)
4881 *lpcs = 0;
4883 /* Look through w32-charset-info-alist for the character set.
4884 Format of each entry is
4885 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
4887 this_entry = Fassoc (build_string(charset), Vw32_charset_info_alist);
4889 if (NILP(this_entry))
4891 /* At startup, we want iso8859-1 fonts to come up properly. */
4892 if (stricmp(charset, "iso8859-1") == 0)
4893 return ANSI_CHARSET;
4894 else
4895 return DEFAULT_CHARSET;
4898 w32_charset = Fcar (Fcdr (this_entry));
4900 /* Translate Lisp symbol to number. */
4901 if (EQ (w32_charset, Qw32_charset_ansi))
4902 return ANSI_CHARSET;
4903 if (EQ (w32_charset, Qw32_charset_symbol))
4904 return SYMBOL_CHARSET;
4905 if (EQ (w32_charset, Qw32_charset_shiftjis))
4906 return SHIFTJIS_CHARSET;
4907 if (EQ (w32_charset, Qw32_charset_hangeul))
4908 return HANGEUL_CHARSET;
4909 if (EQ (w32_charset, Qw32_charset_chinesebig5))
4910 return CHINESEBIG5_CHARSET;
4911 if (EQ (w32_charset, Qw32_charset_gb2312))
4912 return GB2312_CHARSET;
4913 if (EQ (w32_charset, Qw32_charset_oem))
4914 return OEM_CHARSET;
4915 #ifdef JOHAB_CHARSET
4916 if (EQ (w32_charset, Qw32_charset_johab))
4917 return JOHAB_CHARSET;
4918 if (EQ (w32_charset, Qw32_charset_easteurope))
4919 return EASTEUROPE_CHARSET;
4920 if (EQ (w32_charset, Qw32_charset_turkish))
4921 return TURKISH_CHARSET;
4922 if (EQ (w32_charset, Qw32_charset_baltic))
4923 return BALTIC_CHARSET;
4924 if (EQ (w32_charset, Qw32_charset_russian))
4925 return RUSSIAN_CHARSET;
4926 if (EQ (w32_charset, Qw32_charset_arabic))
4927 return ARABIC_CHARSET;
4928 if (EQ (w32_charset, Qw32_charset_greek))
4929 return GREEK_CHARSET;
4930 if (EQ (w32_charset, Qw32_charset_hebrew))
4931 return HEBREW_CHARSET;
4932 if (EQ (w32_charset, Qw32_charset_vietnamese))
4933 return VIETNAMESE_CHARSET;
4934 if (EQ (w32_charset, Qw32_charset_thai))
4935 return THAI_CHARSET;
4936 if (EQ (w32_charset, Qw32_charset_mac))
4937 return MAC_CHARSET;
4938 #endif /* JOHAB_CHARSET */
4939 #ifdef UNICODE_CHARSET
4940 if (EQ (w32_charset, Qw32_charset_unicode))
4941 return UNICODE_CHARSET;
4942 #endif
4944 return DEFAULT_CHARSET;
4948 static char *
4949 w32_to_x_charset (fncharset)
4950 int fncharset;
4952 static char buf[32];
4953 Lisp_Object charset_type;
4955 switch (fncharset)
4957 case ANSI_CHARSET:
4958 /* Handle startup case of w32-charset-info-alist not
4959 being set up yet. */
4960 if (NILP(Vw32_charset_info_alist))
4961 return "iso8859-1";
4962 charset_type = Qw32_charset_ansi;
4963 break;
4964 case DEFAULT_CHARSET:
4965 charset_type = Qw32_charset_default;
4966 break;
4967 case SYMBOL_CHARSET:
4968 charset_type = Qw32_charset_symbol;
4969 break;
4970 case SHIFTJIS_CHARSET:
4971 charset_type = Qw32_charset_shiftjis;
4972 break;
4973 case HANGEUL_CHARSET:
4974 charset_type = Qw32_charset_hangeul;
4975 break;
4976 case GB2312_CHARSET:
4977 charset_type = Qw32_charset_gb2312;
4978 break;
4979 case CHINESEBIG5_CHARSET:
4980 charset_type = Qw32_charset_chinesebig5;
4981 break;
4982 case OEM_CHARSET:
4983 charset_type = Qw32_charset_oem;
4984 break;
4986 /* More recent versions of Windows (95 and NT4.0) define more
4987 character sets. */
4988 #ifdef EASTEUROPE_CHARSET
4989 case EASTEUROPE_CHARSET:
4990 charset_type = Qw32_charset_easteurope;
4991 break;
4992 case TURKISH_CHARSET:
4993 charset_type = Qw32_charset_turkish;
4994 break;
4995 case BALTIC_CHARSET:
4996 charset_type = Qw32_charset_baltic;
4997 break;
4998 case RUSSIAN_CHARSET:
4999 charset_type = Qw32_charset_russian;
5000 break;
5001 case ARABIC_CHARSET:
5002 charset_type = Qw32_charset_arabic;
5003 break;
5004 case GREEK_CHARSET:
5005 charset_type = Qw32_charset_greek;
5006 break;
5007 case HEBREW_CHARSET:
5008 charset_type = Qw32_charset_hebrew;
5009 break;
5010 case VIETNAMESE_CHARSET:
5011 charset_type = Qw32_charset_vietnamese;
5012 break;
5013 case THAI_CHARSET:
5014 charset_type = Qw32_charset_thai;
5015 break;
5016 case MAC_CHARSET:
5017 charset_type = Qw32_charset_mac;
5018 break;
5019 case JOHAB_CHARSET:
5020 charset_type = Qw32_charset_johab;
5021 break;
5022 #endif
5024 #ifdef UNICODE_CHARSET
5025 case UNICODE_CHARSET:
5026 charset_type = Qw32_charset_unicode;
5027 break;
5028 #endif
5029 default:
5030 /* Encode numerical value of unknown charset. */
5031 sprintf (buf, "*-#%u", fncharset);
5032 return buf;
5036 Lisp_Object rest;
5037 char * best_match = NULL;
5039 /* Look through w32-charset-info-alist for the character set.
5040 Prefer ISO codepages, and prefer lower numbers in the ISO
5041 range. Only return charsets for codepages which are installed.
5043 Format of each entry is
5044 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
5046 for (rest = Vw32_charset_info_alist; CONSP (rest); rest = XCDR (rest))
5048 char * x_charset;
5049 Lisp_Object w32_charset;
5050 Lisp_Object codepage;
5052 Lisp_Object this_entry = XCAR (rest);
5054 /* Skip invalid entries in alist. */
5055 if (!CONSP (this_entry) || !STRINGP (XCAR (this_entry))
5056 || !CONSP (XCDR (this_entry))
5057 || !SYMBOLP (XCAR (XCDR (this_entry))))
5058 continue;
5060 x_charset = SDATA (XCAR (this_entry));
5061 w32_charset = XCAR (XCDR (this_entry));
5062 codepage = XCDR (XCDR (this_entry));
5064 /* Look for Same charset and a valid codepage (or non-int
5065 which means ignore). */
5066 if (EQ (w32_charset, charset_type)
5067 && (!INTEGERP (codepage) || XINT (codepage) == CP_DEFAULT
5068 || IsValidCodePage (XINT (codepage))))
5070 /* If we don't have a match already, then this is the
5071 best. */
5072 if (!best_match)
5073 best_match = x_charset;
5074 /* If this is an ISO codepage, and the best so far isn't,
5075 then this is better. */
5076 else if (strnicmp (best_match, "iso", 3) != 0
5077 && strnicmp (x_charset, "iso", 3) == 0)
5078 best_match = x_charset;
5079 /* If both are ISO8859 codepages, choose the one with the
5080 lowest number in the encoding field. */
5081 else if (strnicmp (best_match, "iso8859-", 8) == 0
5082 && strnicmp (x_charset, "iso8859-", 8) == 0)
5084 int best_enc = atoi (best_match + 8);
5085 int this_enc = atoi (x_charset + 8);
5086 if (this_enc > 0 && this_enc < best_enc)
5087 best_match = x_charset;
5092 /* If no match, encode the numeric value. */
5093 if (!best_match)
5095 sprintf (buf, "*-#%u", fncharset);
5096 return buf;
5099 strncpy(buf, best_match, 31);
5100 buf[31] = '\0';
5101 return buf;
5106 /* Return all the X charsets that map to a font. */
5107 static Lisp_Object
5108 w32_to_all_x_charsets (fncharset)
5109 int fncharset;
5111 static char buf[32];
5112 Lisp_Object charset_type;
5113 Lisp_Object retval = Qnil;
5115 switch (fncharset)
5117 case ANSI_CHARSET:
5118 /* Handle startup case of w32-charset-info-alist not
5119 being set up yet. */
5120 if (NILP(Vw32_charset_info_alist))
5121 return Fcons (build_string ("iso8859-1"), Qnil);
5123 charset_type = Qw32_charset_ansi;
5124 break;
5125 case DEFAULT_CHARSET:
5126 charset_type = Qw32_charset_default;
5127 break;
5128 case SYMBOL_CHARSET:
5129 charset_type = Qw32_charset_symbol;
5130 break;
5131 case SHIFTJIS_CHARSET:
5132 charset_type = Qw32_charset_shiftjis;
5133 break;
5134 case HANGEUL_CHARSET:
5135 charset_type = Qw32_charset_hangeul;
5136 break;
5137 case GB2312_CHARSET:
5138 charset_type = Qw32_charset_gb2312;
5139 break;
5140 case CHINESEBIG5_CHARSET:
5141 charset_type = Qw32_charset_chinesebig5;
5142 break;
5143 case OEM_CHARSET:
5144 charset_type = Qw32_charset_oem;
5145 break;
5147 /* More recent versions of Windows (95 and NT4.0) define more
5148 character sets. */
5149 #ifdef EASTEUROPE_CHARSET
5150 case EASTEUROPE_CHARSET:
5151 charset_type = Qw32_charset_easteurope;
5152 break;
5153 case TURKISH_CHARSET:
5154 charset_type = Qw32_charset_turkish;
5155 break;
5156 case BALTIC_CHARSET:
5157 charset_type = Qw32_charset_baltic;
5158 break;
5159 case RUSSIAN_CHARSET:
5160 charset_type = Qw32_charset_russian;
5161 break;
5162 case ARABIC_CHARSET:
5163 charset_type = Qw32_charset_arabic;
5164 break;
5165 case GREEK_CHARSET:
5166 charset_type = Qw32_charset_greek;
5167 break;
5168 case HEBREW_CHARSET:
5169 charset_type = Qw32_charset_hebrew;
5170 break;
5171 case VIETNAMESE_CHARSET:
5172 charset_type = Qw32_charset_vietnamese;
5173 break;
5174 case THAI_CHARSET:
5175 charset_type = Qw32_charset_thai;
5176 break;
5177 case MAC_CHARSET:
5178 charset_type = Qw32_charset_mac;
5179 break;
5180 case JOHAB_CHARSET:
5181 charset_type = Qw32_charset_johab;
5182 break;
5183 #endif
5185 #ifdef UNICODE_CHARSET
5186 case UNICODE_CHARSET:
5187 charset_type = Qw32_charset_unicode;
5188 break;
5189 #endif
5190 default:
5191 /* Encode numerical value of unknown charset. */
5192 sprintf (buf, "*-#%u", fncharset);
5193 return Fcons (build_string (buf), Qnil);
5197 Lisp_Object rest;
5198 /* Look through w32-charset-info-alist for the character set.
5199 Only return charsets for codepages which are installed.
5201 Format of each entry in Vw32_charset_info_alist is
5202 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
5204 for (rest = Vw32_charset_info_alist; CONSP (rest); rest = XCDR (rest))
5206 Lisp_Object x_charset;
5207 Lisp_Object w32_charset;
5208 Lisp_Object codepage;
5210 Lisp_Object this_entry = XCAR (rest);
5212 /* Skip invalid entries in alist. */
5213 if (!CONSP (this_entry) || !STRINGP (XCAR (this_entry))
5214 || !CONSP (XCDR (this_entry))
5215 || !SYMBOLP (XCAR (XCDR (this_entry))))
5216 continue;
5218 x_charset = XCAR (this_entry);
5219 w32_charset = XCAR (XCDR (this_entry));
5220 codepage = XCDR (XCDR (this_entry));
5222 /* Look for Same charset and a valid codepage (or non-int
5223 which means ignore). */
5224 if (EQ (w32_charset, charset_type)
5225 && (!INTEGERP (codepage) || XINT (codepage) == CP_DEFAULT
5226 || IsValidCodePage (XINT (codepage))))
5228 retval = Fcons (x_charset, retval);
5232 /* If no match, encode the numeric value. */
5233 if (NILP (retval))
5235 sprintf (buf, "*-#%u", fncharset);
5236 return Fcons (build_string (buf), Qnil);
5239 return retval;
5243 /* Get the Windows codepage corresponding to the specified font. The
5244 charset info in the font name is used to look up
5245 w32-charset-to-codepage-alist. */
5247 w32_codepage_for_font (char *fontname)
5249 Lisp_Object codepage, entry;
5250 char *charset_str, *charset, *end;
5252 if (NILP (Vw32_charset_info_alist))
5253 return CP_DEFAULT;
5255 /* Extract charset part of font string. */
5256 charset = xlfd_charset_of_font (fontname);
5258 if (!charset)
5259 return CP_UNKNOWN;
5261 charset_str = (char *) alloca (strlen (charset) + 1);
5262 strcpy (charset_str, charset);
5264 #if 0
5265 /* Remove leading "*-". */
5266 if (strncmp ("*-", charset_str, 2) == 0)
5267 charset = charset_str + 2;
5268 else
5269 #endif
5270 charset = charset_str;
5272 /* Stop match at wildcard (including preceding '-'). */
5273 if (end = strchr (charset, '*'))
5275 if (end > charset && *(end-1) == '-')
5276 end--;
5277 *end = '\0';
5280 entry = Fassoc (build_string(charset), Vw32_charset_info_alist);
5281 if (NILP (entry))
5282 return CP_UNKNOWN;
5284 codepage = Fcdr (Fcdr (entry));
5286 if (NILP (codepage))
5287 return CP_8BIT;
5288 else if (XFASTINT (codepage) == XFASTINT (Qt))
5289 return CP_UNICODE;
5290 else if (INTEGERP (codepage))
5291 return XINT (codepage);
5292 else
5293 return CP_UNKNOWN;
5297 static BOOL
5298 w32_to_x_font (lplogfont, lpxstr, len, specific_charset)
5299 LOGFONT * lplogfont;
5300 char * lpxstr;
5301 int len;
5302 char * specific_charset;
5304 char* fonttype;
5305 char *fontname;
5306 char height_pixels[8];
5307 char height_dpi[8];
5308 char width_pixels[8];
5309 char *fontname_dash;
5310 int display_resy = (int) one_w32_display_info.resy;
5311 int display_resx = (int) one_w32_display_info.resx;
5312 int bufsz;
5313 struct coding_system coding;
5315 if (!lpxstr) abort ();
5317 if (!lplogfont)
5318 return FALSE;
5320 if (lplogfont->lfOutPrecision == OUT_STRING_PRECIS)
5321 fonttype = "raster";
5322 else if (lplogfont->lfOutPrecision == OUT_STROKE_PRECIS)
5323 fonttype = "outline";
5324 else
5325 fonttype = "unknown";
5327 setup_coding_system (Fcheck_coding_system (Vlocale_coding_system),
5328 &coding);
5329 coding.src_multibyte = 0;
5330 coding.dst_multibyte = 1;
5331 coding.mode |= CODING_MODE_LAST_BLOCK;
5332 /* We explicitely disable composition handling because selection
5333 data should not contain any composition sequence. */
5334 coding.composing = COMPOSITION_DISABLED;
5335 bufsz = decoding_buffer_size (&coding, LF_FACESIZE);
5337 fontname = alloca(sizeof(*fontname) * bufsz);
5338 decode_coding (&coding, lplogfont->lfFaceName, fontname,
5339 strlen(lplogfont->lfFaceName), bufsz - 1);
5340 *(fontname + coding.produced) = '\0';
5342 /* Replace dashes with underscores so the dashes are not
5343 misinterpreted. */
5344 fontname_dash = fontname;
5345 while (fontname_dash = strchr (fontname_dash, '-'))
5346 *fontname_dash = '_';
5348 if (lplogfont->lfHeight)
5350 sprintf (height_pixels, "%u", abs (lplogfont->lfHeight));
5351 sprintf (height_dpi, "%u",
5352 abs (lplogfont->lfHeight) * 720 / display_resy);
5354 else
5356 strcpy (height_pixels, "*");
5357 strcpy (height_dpi, "*");
5360 #if 0 /* Never put the width in the xfld. It fails on fonts with
5361 double-width characters. */
5362 if (lplogfont->lfWidth)
5363 sprintf (width_pixels, "%u", lplogfont->lfWidth * 10);
5364 else
5365 #endif
5366 strcpy (width_pixels, "*");
5368 _snprintf (lpxstr, len - 1,
5369 "-%s-%s-%s-%c-normal-normal-%s-%s-%d-%d-%c-%s-%s",
5370 fonttype, /* foundry */
5371 fontname, /* family */
5372 w32_to_x_weight (lplogfont->lfWeight), /* weight */
5373 lplogfont->lfItalic?'i':'r', /* slant */
5374 /* setwidth name */
5375 /* add style name */
5376 height_pixels, /* pixel size */
5377 height_dpi, /* point size */
5378 display_resx, /* resx */
5379 display_resy, /* resy */
5380 ((lplogfont->lfPitchAndFamily & 0x3) == VARIABLE_PITCH)
5381 ? 'p' : 'c', /* spacing */
5382 width_pixels, /* avg width */
5383 specific_charset ? specific_charset
5384 : w32_to_x_charset (lplogfont->lfCharSet)
5385 /* charset registry and encoding */
5388 lpxstr[len - 1] = 0; /* just to be sure */
5389 return (TRUE);
5392 static BOOL
5393 x_to_w32_font (lpxstr, lplogfont)
5394 char * lpxstr;
5395 LOGFONT * lplogfont;
5397 struct coding_system coding;
5399 if (!lplogfont) return (FALSE);
5401 memset (lplogfont, 0, sizeof (*lplogfont));
5403 /* Set default value for each field. */
5404 #if 1
5405 lplogfont->lfOutPrecision = OUT_DEFAULT_PRECIS;
5406 lplogfont->lfClipPrecision = CLIP_DEFAULT_PRECIS;
5407 lplogfont->lfQuality = DEFAULT_QUALITY;
5408 #else
5409 /* go for maximum quality */
5410 lplogfont->lfOutPrecision = OUT_STROKE_PRECIS;
5411 lplogfont->lfClipPrecision = CLIP_STROKE_PRECIS;
5412 lplogfont->lfQuality = PROOF_QUALITY;
5413 #endif
5415 lplogfont->lfCharSet = DEFAULT_CHARSET;
5416 lplogfont->lfWeight = FW_DONTCARE;
5417 lplogfont->lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
5419 if (!lpxstr)
5420 return FALSE;
5422 /* Provide a simple escape mechanism for specifying Windows font names
5423 * directly -- if font spec does not beginning with '-', assume this
5424 * format:
5425 * "<font name>[:height in pixels[:width in pixels[:weight]]]"
5428 if (*lpxstr == '-')
5430 int fields, tem;
5431 char name[50], weight[20], slant, pitch, pixels[10], height[10],
5432 width[10], resy[10], remainder[50];
5433 char * encoding;
5434 int dpi = (int) one_w32_display_info.resy;
5436 fields = sscanf (lpxstr,
5437 "-%*[^-]-%49[^-]-%19[^-]-%c-%*[^-]-%*[^-]-%9[^-]-%9[^-]-%*[^-]-%9[^-]-%c-%9[^-]-%49s",
5438 name, weight, &slant, pixels, height, resy, &pitch, width, remainder);
5439 if (fields == EOF)
5440 return (FALSE);
5442 /* In the general case when wildcards cover more than one field,
5443 we don't know which field is which, so don't fill any in.
5444 However, we need to cope with this particular form, which is
5445 generated by font_list_1 (invoked by try_font_list):
5446 "-raster-6x10-*-gb2312*-*"
5447 and make sure to correctly parse the charset field. */
5448 if (fields == 3)
5450 fields = sscanf (lpxstr,
5451 "-%*[^-]-%49[^-]-*-%49s",
5452 name, remainder);
5454 else if (fields < 9)
5456 fields = 0;
5457 remainder[0] = 0;
5460 if (fields > 0 && name[0] != '*')
5462 int bufsize;
5463 unsigned char *buf;
5465 setup_coding_system
5466 (Fcheck_coding_system (Vlocale_coding_system), &coding);
5467 coding.src_multibyte = 1;
5468 coding.dst_multibyte = 0;
5469 /* Need to set COMPOSITION_DISABLED, otherwise Emacs crashes in
5470 encode_coding_iso2022 trying to dereference a null pointer. */
5471 coding.composing = COMPOSITION_DISABLED;
5472 if (coding.type == coding_type_iso2022)
5473 coding.flags |= CODING_FLAG_ISO_SAFE;
5474 bufsize = encoding_buffer_size (&coding, strlen (name));
5475 buf = (unsigned char *) alloca (bufsize);
5476 coding.mode |= CODING_MODE_LAST_BLOCK;
5477 encode_coding (&coding, name, buf, strlen (name), bufsize);
5478 if (coding.produced >= LF_FACESIZE)
5479 coding.produced = LF_FACESIZE - 1;
5480 buf[coding.produced] = 0;
5481 strcpy (lplogfont->lfFaceName, buf);
5483 else
5485 lplogfont->lfFaceName[0] = '\0';
5488 fields--;
5490 lplogfont->lfWeight = x_to_w32_weight ((fields > 0 ? weight : ""));
5492 fields--;
5494 lplogfont->lfItalic = (fields > 0 && slant == 'i');
5496 fields--;
5498 if (fields > 0 && pixels[0] != '*')
5499 lplogfont->lfHeight = atoi (pixels);
5501 fields--;
5502 fields--;
5503 if (fields > 0 && resy[0] != '*')
5505 tem = atoi (resy);
5506 if (tem > 0) dpi = tem;
5509 if (fields > -1 && lplogfont->lfHeight == 0 && height[0] != '*')
5510 lplogfont->lfHeight = atoi (height) * dpi / 720;
5512 if (fields > 0)
5513 lplogfont->lfPitchAndFamily =
5514 (fields > 0 && pitch == 'p') ? VARIABLE_PITCH : FIXED_PITCH;
5516 fields--;
5518 if (fields > 0 && width[0] != '*')
5519 lplogfont->lfWidth = atoi (width) / 10;
5521 fields--;
5523 /* Strip the trailing '-' if present. (it shouldn't be, as it
5524 fails the test against xlfd-tight-regexp in fontset.el). */
5526 int len = strlen (remainder);
5527 if (len > 0 && remainder[len-1] == '-')
5528 remainder[len-1] = 0;
5530 encoding = remainder;
5531 #if 0
5532 if (strncmp (encoding, "*-", 2) == 0)
5533 encoding += 2;
5534 #endif
5535 lplogfont->lfCharSet = x_to_w32_charset (encoding);
5537 else
5539 int fields;
5540 char name[100], height[10], width[10], weight[20];
5542 fields = sscanf (lpxstr,
5543 "%99[^:]:%9[^:]:%9[^:]:%19s",
5544 name, height, width, weight);
5546 if (fields == EOF) return (FALSE);
5548 if (fields > 0)
5550 strncpy (lplogfont->lfFaceName,name, LF_FACESIZE);
5551 lplogfont->lfFaceName[LF_FACESIZE-1] = 0;
5553 else
5555 lplogfont->lfFaceName[0] = 0;
5558 fields--;
5560 if (fields > 0)
5561 lplogfont->lfHeight = atoi (height);
5563 fields--;
5565 if (fields > 0)
5566 lplogfont->lfWidth = atoi (width);
5568 fields--;
5570 lplogfont->lfWeight = x_to_w32_weight ((fields > 0 ? weight : ""));
5573 /* This makes TrueType fonts work better. */
5574 lplogfont->lfHeight = - abs (lplogfont->lfHeight);
5576 return (TRUE);
5579 /* Strip the pixel height and point height from the given xlfd, and
5580 return the pixel height. If no pixel height is specified, calculate
5581 one from the point height, or if that isn't defined either, return
5582 0 (which usually signifies a scalable font).
5584 static int
5585 xlfd_strip_height (char *fontname)
5587 int pixel_height, field_number;
5588 char *read_from, *write_to;
5590 xassert (fontname);
5592 pixel_height = field_number = 0;
5593 write_to = NULL;
5595 /* Look for height fields. */
5596 for (read_from = fontname; *read_from; read_from++)
5598 if (*read_from == '-')
5600 field_number++;
5601 if (field_number == 7) /* Pixel height. */
5603 read_from++;
5604 write_to = read_from;
5606 /* Find end of field. */
5607 for (;*read_from && *read_from != '-'; read_from++)
5610 /* Split the fontname at end of field. */
5611 if (*read_from)
5613 *read_from = '\0';
5614 read_from++;
5616 pixel_height = atoi (write_to);
5617 /* Blank out field. */
5618 if (read_from > write_to)
5620 *write_to = '-';
5621 write_to++;
5623 /* If the pixel height field is at the end (partial xlfd),
5624 return now. */
5625 else
5626 return pixel_height;
5628 /* If we got a pixel height, the point height can be
5629 ignored. Just blank it out and break now. */
5630 if (pixel_height)
5632 /* Find end of point size field. */
5633 for (; *read_from && *read_from != '-'; read_from++)
5636 if (*read_from)
5637 read_from++;
5639 /* Blank out the point size field. */
5640 if (read_from > write_to)
5642 *write_to = '-';
5643 write_to++;
5645 else
5646 return pixel_height;
5648 break;
5650 /* If the point height is already blank, break now. */
5651 if (*read_from == '-')
5653 read_from++;
5654 break;
5657 else if (field_number == 8)
5659 /* If we didn't get a pixel height, try to get the point
5660 height and convert that. */
5661 int point_size;
5662 char *point_size_start = read_from++;
5664 /* Find end of field. */
5665 for (; *read_from && *read_from != '-'; read_from++)
5668 if (*read_from)
5670 *read_from = '\0';
5671 read_from++;
5674 point_size = atoi (point_size_start);
5676 /* Convert to pixel height. */
5677 pixel_height = point_size
5678 * one_w32_display_info.height_in / 720;
5680 /* Blank out this field and break. */
5681 *write_to = '-';
5682 write_to++;
5683 break;
5688 /* Shift the rest of the font spec into place. */
5689 if (write_to && read_from > write_to)
5691 for (; *read_from; read_from++, write_to++)
5692 *write_to = *read_from;
5693 *write_to = '\0';
5696 return pixel_height;
5699 /* Assume parameter 1 is fully qualified, no wildcards. */
5700 static BOOL
5701 w32_font_match (fontname, pattern)
5702 char * fontname;
5703 char * pattern;
5705 char *ptr;
5706 char *font_name_copy;
5707 char *regex = alloca (strlen (pattern) * 2 + 3);
5709 font_name_copy = alloca (strlen (fontname) + 1);
5710 strcpy (font_name_copy, fontname);
5712 ptr = regex;
5713 *ptr++ = '^';
5715 /* Turn pattern into a regexp and do a regexp match. */
5716 for (; *pattern; pattern++)
5718 if (*pattern == '?')
5719 *ptr++ = '.';
5720 else if (*pattern == '*')
5722 *ptr++ = '.';
5723 *ptr++ = '*';
5725 else
5726 *ptr++ = *pattern;
5728 *ptr = '$';
5729 *(ptr + 1) = '\0';
5731 /* Strip out font heights and compare them seperately, since
5732 rounding error can cause mismatches. This also allows a
5733 comparison between a font that declares only a pixel height and a
5734 pattern that declares the point height.
5737 int font_height, pattern_height;
5739 font_height = xlfd_strip_height (font_name_copy);
5740 pattern_height = xlfd_strip_height (regex);
5742 /* Compare now, and don't bother doing expensive regexp matching
5743 if the heights differ. */
5744 if (font_height && pattern_height && (font_height != pattern_height))
5745 return FALSE;
5748 return (fast_string_match_ignore_case (build_string (regex),
5749 build_string(font_name_copy)) >= 0);
5752 /* Callback functions, and a structure holding info they need, for
5753 listing system fonts on W32. We need one set of functions to do the
5754 job properly, but these don't work on NT 3.51 and earlier, so we
5755 have a second set which don't handle character sets properly to
5756 fall back on.
5758 In both cases, there are two passes made. The first pass gets one
5759 font from each family, the second pass lists all the fonts from
5760 each family. */
5762 typedef struct enumfont_t
5764 HDC hdc;
5765 int numFonts;
5766 LOGFONT logfont;
5767 XFontStruct *size_ref;
5768 Lisp_Object pattern;
5769 Lisp_Object list;
5770 } enumfont_t;
5773 static void
5774 enum_font_maybe_add_to_list (enumfont_t *, LOGFONT *, char *, Lisp_Object);
5777 static int CALLBACK
5778 enum_font_cb2 (lplf, lptm, FontType, lpef)
5779 ENUMLOGFONT * lplf;
5780 NEWTEXTMETRIC * lptm;
5781 int FontType;
5782 enumfont_t * lpef;
5784 /* Ignore struck out and underlined versions of fonts. */
5785 if (lplf->elfLogFont.lfStrikeOut || lplf->elfLogFont.lfUnderline)
5786 return 1;
5788 /* Only return fonts with names starting with @ if they were
5789 explicitly specified, since Microsoft uses an initial @ to
5790 denote fonts for vertical writing, without providing a more
5791 convenient way of identifying them. */
5792 if (lplf->elfLogFont.lfFaceName[0] == '@'
5793 && lpef->logfont.lfFaceName[0] != '@')
5794 return 1;
5796 /* Check that the character set matches if it was specified */
5797 if (lpef->logfont.lfCharSet != DEFAULT_CHARSET &&
5798 lplf->elfLogFont.lfCharSet != lpef->logfont.lfCharSet)
5799 return 1;
5801 if (FontType == RASTER_FONTTYPE)
5803 /* DBCS raster fonts have problems displaying, so skip them. */
5804 int charset = lplf->elfLogFont.lfCharSet;
5805 if (charset == SHIFTJIS_CHARSET
5806 || charset == HANGEUL_CHARSET
5807 || charset == CHINESEBIG5_CHARSET
5808 || charset == GB2312_CHARSET
5809 #ifdef JOHAB_CHARSET
5810 || charset == JOHAB_CHARSET
5811 #endif
5813 return 1;
5817 char buf[100];
5818 Lisp_Object width = Qnil;
5819 Lisp_Object charset_list = Qnil;
5820 char *charset = NULL;
5822 /* Truetype fonts do not report their true metrics until loaded */
5823 if (FontType != RASTER_FONTTYPE)
5825 if (!NILP (lpef->pattern))
5827 /* Scalable fonts are as big as you want them to be. */
5828 lplf->elfLogFont.lfHeight = lpef->logfont.lfHeight;
5829 lplf->elfLogFont.lfWidth = lpef->logfont.lfWidth;
5830 width = make_number (lpef->logfont.lfWidth);
5832 else
5834 lplf->elfLogFont.lfHeight = 0;
5835 lplf->elfLogFont.lfWidth = 0;
5839 /* Make sure the height used here is the same as everywhere
5840 else (ie character height, not cell height). */
5841 if (lplf->elfLogFont.lfHeight > 0)
5843 /* lptm can be trusted for RASTER fonts, but not scalable ones. */
5844 if (FontType == RASTER_FONTTYPE)
5845 lplf->elfLogFont.lfHeight = lptm->tmInternalLeading - lptm->tmHeight;
5846 else
5847 lplf->elfLogFont.lfHeight = -lplf->elfLogFont.lfHeight;
5850 if (!NILP (lpef->pattern))
5852 charset = xlfd_charset_of_font (SDATA (lpef->pattern));
5854 /* We already checked charsets above, but DEFAULT_CHARSET
5855 slipped through. So only allow exact matches for DEFAULT_CHARSET. */
5856 if (charset
5857 && strncmp (charset, "*-*", 3) != 0
5858 && lpef->logfont.lfCharSet == DEFAULT_CHARSET
5859 && strcmp (charset, w32_to_x_charset (DEFAULT_CHARSET)) != 0)
5860 return 1;
5863 if (charset)
5864 charset_list = Fcons (build_string (charset), Qnil);
5865 else
5866 charset_list = w32_to_all_x_charsets (lplf->elfLogFont.lfCharSet);
5868 /* Loop through the charsets. */
5869 for ( ; CONSP (charset_list); charset_list = Fcdr (charset_list))
5871 Lisp_Object this_charset = Fcar (charset_list);
5872 charset = SDATA (this_charset);
5874 /* List bold and italic variations if w32-enable-synthesized-fonts
5875 is non-nil and this is a plain font. */
5876 if (w32_enable_synthesized_fonts
5877 && lplf->elfLogFont.lfWeight == FW_NORMAL
5878 && lplf->elfLogFont.lfItalic == FALSE)
5880 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5881 charset, width);
5882 /* bold. */
5883 lplf->elfLogFont.lfWeight = FW_BOLD;
5884 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5885 charset, width);
5886 /* bold italic. */
5887 lplf->elfLogFont.lfItalic = TRUE;
5888 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5889 charset, width);
5890 /* italic. */
5891 lplf->elfLogFont.lfWeight = FW_NORMAL;
5892 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5893 charset, width);
5895 else
5896 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5897 charset, width);
5901 return 1;
5904 static void
5905 enum_font_maybe_add_to_list (lpef, logfont, match_charset, width)
5906 enumfont_t * lpef;
5907 LOGFONT * logfont;
5908 char * match_charset;
5909 Lisp_Object width;
5911 char buf[100];
5913 if (!w32_to_x_font (logfont, buf, 100, match_charset))
5914 return;
5916 if (NILP (lpef->pattern)
5917 || w32_font_match (buf, SDATA (lpef->pattern)))
5919 /* Check if we already listed this font. This may happen if
5920 w32_enable_synthesized_fonts is non-nil, and there are real
5921 bold and italic versions of the font. */
5922 Lisp_Object font_name = build_string (buf);
5923 if (NILP (Fmember (font_name, lpef->list)))
5925 Lisp_Object entry = Fcons (font_name, width);
5926 lpef->list = Fcons (entry, lpef->list);
5927 lpef->numFonts++;
5933 static int CALLBACK
5934 enum_font_cb1 (lplf, lptm, FontType, lpef)
5935 ENUMLOGFONT * lplf;
5936 NEWTEXTMETRIC * lptm;
5937 int FontType;
5938 enumfont_t * lpef;
5940 return EnumFontFamilies (lpef->hdc,
5941 lplf->elfLogFont.lfFaceName,
5942 (FONTENUMPROC) enum_font_cb2,
5943 (LPARAM) lpef);
5947 static int CALLBACK
5948 enum_fontex_cb2 (lplf, lptm, font_type, lpef)
5949 ENUMLOGFONTEX * lplf;
5950 NEWTEXTMETRICEX * lptm;
5951 int font_type;
5952 enumfont_t * lpef;
5954 /* We are not interested in the extra info we get back from the 'Ex
5955 version - only the fact that we get character set variations
5956 enumerated seperately. */
5957 return enum_font_cb2 ((ENUMLOGFONT *) lplf, (NEWTEXTMETRIC *) lptm,
5958 font_type, lpef);
5961 static int CALLBACK
5962 enum_fontex_cb1 (lplf, lptm, font_type, lpef)
5963 ENUMLOGFONTEX * lplf;
5964 NEWTEXTMETRICEX * lptm;
5965 int font_type;
5966 enumfont_t * lpef;
5968 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
5969 FARPROC enum_font_families_ex
5970 = GetProcAddress ( gdi32, "EnumFontFamiliesExA");
5971 /* We don't really expect EnumFontFamiliesEx to disappear once we
5972 get here, so don't bother handling it gracefully. */
5973 if (enum_font_families_ex == NULL)
5974 error ("gdi32.dll has disappeared!");
5975 return enum_font_families_ex (lpef->hdc,
5976 &lplf->elfLogFont,
5977 (FONTENUMPROC) enum_fontex_cb2,
5978 (LPARAM) lpef, 0);
5981 /* Interface to fontset handler. (adapted from mw32font.c in Meadow
5982 and xterm.c in Emacs 20.3) */
5984 static Lisp_Object w32_list_bdf_fonts (Lisp_Object pattern, int max_names)
5986 char *fontname, *ptnstr;
5987 Lisp_Object list, tem, newlist = Qnil;
5988 int n_fonts = 0;
5990 list = Vw32_bdf_filename_alist;
5991 ptnstr = SDATA (pattern);
5993 for ( ; CONSP (list); list = XCDR (list))
5995 tem = XCAR (list);
5996 if (CONSP (tem))
5997 fontname = SDATA (XCAR (tem));
5998 else if (STRINGP (tem))
5999 fontname = SDATA (tem);
6000 else
6001 continue;
6003 if (w32_font_match (fontname, ptnstr))
6005 newlist = Fcons (XCAR (tem), newlist);
6006 n_fonts++;
6007 if (max_names >= 0 && n_fonts >= max_names)
6008 break;
6012 return newlist;
6016 /* Return a list of names of available fonts matching PATTERN on frame
6017 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
6018 to be listed. Frame F NULL means we have not yet created any
6019 frame, which means we can't get proper size info, as we don't have
6020 a device context to use for GetTextMetrics.
6021 MAXNAMES sets a limit on how many fonts to match. If MAXNAMES is
6022 negative, then all matching fonts are returned. */
6024 Lisp_Object
6025 w32_list_fonts (f, pattern, size, maxnames)
6026 struct frame *f;
6027 Lisp_Object pattern;
6028 int size;
6029 int maxnames;
6031 Lisp_Object patterns, key = Qnil, tem, tpat;
6032 Lisp_Object list = Qnil, newlist = Qnil, second_best = Qnil;
6033 struct w32_display_info *dpyinfo = &one_w32_display_info;
6034 int n_fonts = 0;
6036 patterns = Fassoc (pattern, Valternate_fontname_alist);
6037 if (NILP (patterns))
6038 patterns = Fcons (pattern, Qnil);
6040 for (; CONSP (patterns); patterns = XCDR (patterns))
6042 enumfont_t ef;
6043 int codepage;
6045 tpat = XCAR (patterns);
6047 if (!STRINGP (tpat))
6048 continue;
6050 /* Avoid expensive EnumFontFamilies functions if we are not
6051 going to be able to output one of these anyway. */
6052 codepage = w32_codepage_for_font (SDATA (tpat));
6053 if (codepage != CP_8BIT && codepage != CP_UNICODE
6054 && codepage != CP_DEFAULT && codepage != CP_UNKNOWN
6055 && !IsValidCodePage(codepage))
6056 continue;
6058 /* See if we cached the result for this particular query.
6059 The cache is an alist of the form:
6060 ((PATTERN (FONTNAME . WIDTH) ...) ...)
6062 if (tem = XCDR (dpyinfo->name_list_element),
6063 !NILP (list = Fassoc (tpat, tem)))
6065 list = Fcdr_safe (list);
6066 /* We have a cached list. Don't have to get the list again. */
6067 goto label_cached;
6070 BLOCK_INPUT;
6071 /* At first, put PATTERN in the cache. */
6072 ef.pattern = tpat;
6073 ef.list = Qnil;
6074 ef.numFonts = 0;
6076 /* Use EnumFontFamiliesEx where it is available, as it knows
6077 about character sets. Fall back to EnumFontFamilies for
6078 older versions of NT that don't support the 'Ex function. */
6079 x_to_w32_font (SDATA (tpat), &ef.logfont);
6081 LOGFONT font_match_pattern;
6082 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
6083 FARPROC enum_font_families_ex
6084 = GetProcAddress ( gdi32, "EnumFontFamiliesExA");
6086 /* We do our own pattern matching so we can handle wildcards. */
6087 font_match_pattern.lfFaceName[0] = 0;
6088 font_match_pattern.lfPitchAndFamily = 0;
6089 /* We can use the charset, because if it is a wildcard it will
6090 be DEFAULT_CHARSET anyway. */
6091 font_match_pattern.lfCharSet = ef.logfont.lfCharSet;
6093 ef.hdc = GetDC (dpyinfo->root_window);
6095 if (enum_font_families_ex)
6096 enum_font_families_ex (ef.hdc,
6097 &font_match_pattern,
6098 (FONTENUMPROC) enum_fontex_cb1,
6099 (LPARAM) &ef, 0);
6100 else
6101 EnumFontFamilies (ef.hdc, NULL, (FONTENUMPROC) enum_font_cb1,
6102 (LPARAM)&ef);
6104 ReleaseDC (dpyinfo->root_window, ef.hdc);
6107 UNBLOCK_INPUT;
6108 list = ef.list;
6110 /* Make a list of the fonts we got back.
6111 Store that in the font cache for the display. */
6112 XSETCDR (dpyinfo->name_list_element,
6113 Fcons (Fcons (tpat, list),
6114 XCDR (dpyinfo->name_list_element)));
6116 label_cached:
6117 if (NILP (list)) continue; /* Try the remaining alternatives. */
6119 newlist = second_best = Qnil;
6121 /* Make a list of the fonts that have the right width. */
6122 for (; CONSP (list); list = XCDR (list))
6124 int found_size;
6125 tem = XCAR (list);
6127 if (!CONSP (tem))
6128 continue;
6129 if (NILP (XCAR (tem)))
6130 continue;
6131 if (!size)
6133 newlist = Fcons (XCAR (tem), newlist);
6134 n_fonts++;
6135 if (maxnames >= 0 && n_fonts >= maxnames)
6136 break;
6137 else
6138 continue;
6140 if (!INTEGERP (XCDR (tem)))
6142 /* Since we don't yet know the size of the font, we must
6143 load it and try GetTextMetrics. */
6144 W32FontStruct thisinfo;
6145 LOGFONT lf;
6146 HDC hdc;
6147 HANDLE oldobj;
6149 if (!x_to_w32_font (SDATA (XCAR (tem)), &lf))
6150 continue;
6152 BLOCK_INPUT;
6153 thisinfo.bdf = NULL;
6154 thisinfo.hfont = CreateFontIndirect (&lf);
6155 if (thisinfo.hfont == NULL)
6156 continue;
6158 hdc = GetDC (dpyinfo->root_window);
6159 oldobj = SelectObject (hdc, thisinfo.hfont);
6160 if (GetTextMetrics (hdc, &thisinfo.tm))
6161 XSETCDR (tem, make_number (FONT_WIDTH (&thisinfo)));
6162 else
6163 XSETCDR (tem, make_number (0));
6164 SelectObject (hdc, oldobj);
6165 ReleaseDC (dpyinfo->root_window, hdc);
6166 DeleteObject(thisinfo.hfont);
6167 UNBLOCK_INPUT;
6169 found_size = XINT (XCDR (tem));
6170 if (found_size == size)
6172 newlist = Fcons (XCAR (tem), newlist);
6173 n_fonts++;
6174 if (maxnames >= 0 && n_fonts >= maxnames)
6175 break;
6177 /* keep track of the closest matching size in case
6178 no exact match is found. */
6179 else if (found_size > 0)
6181 if (NILP (second_best))
6182 second_best = tem;
6184 else if (found_size < size)
6186 if (XINT (XCDR (second_best)) > size
6187 || XINT (XCDR (second_best)) < found_size)
6188 second_best = tem;
6190 else
6192 if (XINT (XCDR (second_best)) > size
6193 && XINT (XCDR (second_best)) >
6194 found_size)
6195 second_best = tem;
6200 if (!NILP (newlist))
6201 break;
6202 else if (!NILP (second_best))
6204 newlist = Fcons (XCAR (second_best), Qnil);
6205 break;
6209 /* Include any bdf fonts. */
6210 if (n_fonts < maxnames || maxnames < 0)
6212 Lisp_Object combined[2];
6213 combined[0] = w32_list_bdf_fonts (pattern, maxnames - n_fonts);
6214 combined[1] = newlist;
6215 newlist = Fnconc(2, combined);
6218 return newlist;
6222 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
6223 struct font_info *
6224 w32_get_font_info (f, font_idx)
6225 FRAME_PTR f;
6226 int font_idx;
6228 return (FRAME_W32_FONT_TABLE (f) + font_idx);
6232 struct font_info*
6233 w32_query_font (struct frame *f, char *fontname)
6235 int i;
6236 struct font_info *pfi;
6238 pfi = FRAME_W32_FONT_TABLE (f);
6240 for (i = 0; i < one_w32_display_info.n_fonts ;i++, pfi++)
6242 if (stricmp(pfi->name, fontname) == 0) return pfi;
6245 return NULL;
6248 /* Find a CCL program for a font specified by FONTP, and set the member
6249 `encoder' of the structure. */
6251 void
6252 w32_find_ccl_program (fontp)
6253 struct font_info *fontp;
6255 Lisp_Object list, elt;
6257 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
6259 elt = XCAR (list);
6260 if (CONSP (elt)
6261 && STRINGP (XCAR (elt))
6262 && (fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
6263 >= 0))
6264 break;
6266 if (! NILP (list))
6268 struct ccl_program *ccl
6269 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
6271 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
6272 xfree (ccl);
6273 else
6274 fontp->font_encoder = ccl;
6278 /* directory-files from dired.c. */
6279 Lisp_Object Fdirectory_files P_((Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object));
6282 /* Find BDF files in a specified directory. (use GCPRO when calling,
6283 as this calls lisp to get a directory listing). */
6284 static Lisp_Object
6285 w32_find_bdf_fonts_in_dir (Lisp_Object directory)
6287 Lisp_Object filelist, list = Qnil;
6288 char fontname[100];
6290 if (!STRINGP(directory))
6291 return Qnil;
6293 filelist = Fdirectory_files (directory, Qt,
6294 build_string (".*\\.[bB][dD][fF]"), Qt);
6296 for ( ; CONSP(filelist); filelist = XCDR (filelist))
6298 Lisp_Object filename = XCAR (filelist);
6299 if (w32_BDF_to_x_font (SDATA (filename), fontname, 100))
6300 store_in_alist (&list, build_string (fontname), filename);
6302 return list;
6305 DEFUN ("w32-find-bdf-fonts", Fw32_find_bdf_fonts, Sw32_find_bdf_fonts,
6306 1, 1, 0,
6307 doc: /* Return a list of BDF fonts in DIRECTORY.
6308 The list is suitable for appending to `w32-bdf-filename-alist'.
6309 Fonts which do not contain an xlfd description will not be included
6310 in the list. DIRECTORY may be a list of directories. */)
6311 (directory)
6312 Lisp_Object directory;
6314 Lisp_Object list = Qnil;
6315 struct gcpro gcpro1, gcpro2;
6317 if (!CONSP (directory))
6318 return w32_find_bdf_fonts_in_dir (directory);
6320 for ( ; CONSP (directory); directory = XCDR (directory))
6322 Lisp_Object pair[2];
6323 pair[0] = list;
6324 pair[1] = Qnil;
6325 GCPRO2 (directory, list);
6326 pair[1] = w32_find_bdf_fonts_in_dir( XCAR (directory) );
6327 list = Fnconc( 2, pair );
6328 UNGCPRO;
6330 return list;
6334 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
6335 doc: /* Internal function called by `color-defined-p', which see. */)
6336 (color, frame)
6337 Lisp_Object color, frame;
6339 XColor foo;
6340 FRAME_PTR f = check_x_frame (frame);
6342 CHECK_STRING (color);
6344 if (w32_defined_color (f, SDATA (color), &foo, 0))
6345 return Qt;
6346 else
6347 return Qnil;
6350 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
6351 doc: /* Internal function called by `color-values', which see. */)
6352 (color, frame)
6353 Lisp_Object color, frame;
6355 XColor foo;
6356 FRAME_PTR f = check_x_frame (frame);
6358 CHECK_STRING (color);
6360 if (w32_defined_color (f, SDATA (color), &foo, 0))
6361 return list3 (make_number ((GetRValue (foo.pixel) << 8)
6362 | GetRValue (foo.pixel)),
6363 make_number ((GetGValue (foo.pixel) << 8)
6364 | GetGValue (foo.pixel)),
6365 make_number ((GetBValue (foo.pixel) << 8)
6366 | GetBValue (foo.pixel)));
6367 else
6368 return Qnil;
6371 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
6372 doc: /* Internal function called by `display-color-p', which see. */)
6373 (display)
6374 Lisp_Object display;
6376 struct w32_display_info *dpyinfo = check_x_display_info (display);
6378 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 2)
6379 return Qnil;
6381 return Qt;
6384 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p,
6385 Sx_display_grayscale_p, 0, 1, 0,
6386 doc: /* Return t if DISPLAY supports shades of gray.
6387 Note that color displays do support shades of gray.
6388 The optional argument DISPLAY specifies which display to ask about.
6389 DISPLAY should be either a frame or a display name (a string).
6390 If omitted or nil, that stands for the selected frame's display. */)
6391 (display)
6392 Lisp_Object display;
6394 struct w32_display_info *dpyinfo = check_x_display_info (display);
6396 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 1)
6397 return Qnil;
6399 return Qt;
6402 DEFUN ("x-display-pixel-width", Fx_display_pixel_width,
6403 Sx_display_pixel_width, 0, 1, 0,
6404 doc: /* Returns the width in pixels of DISPLAY.
6405 The optional argument DISPLAY specifies which display to ask about.
6406 DISPLAY should be either a frame or a display name (a string).
6407 If omitted or nil, that stands for the selected frame's display. */)
6408 (display)
6409 Lisp_Object display;
6411 struct w32_display_info *dpyinfo = check_x_display_info (display);
6413 return make_number (dpyinfo->width);
6416 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
6417 Sx_display_pixel_height, 0, 1, 0,
6418 doc: /* Returns the height in pixels of DISPLAY.
6419 The optional argument DISPLAY specifies which display to ask about.
6420 DISPLAY should be either a frame or a display name (a string).
6421 If omitted or nil, that stands for the selected frame's display. */)
6422 (display)
6423 Lisp_Object display;
6425 struct w32_display_info *dpyinfo = check_x_display_info (display);
6427 return make_number (dpyinfo->height);
6430 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
6431 0, 1, 0,
6432 doc: /* Returns the number of bitplanes of DISPLAY.
6433 The optional argument DISPLAY specifies which display to ask about.
6434 DISPLAY should be either a frame or a display name (a string).
6435 If omitted or nil, that stands for the selected frame's display. */)
6436 (display)
6437 Lisp_Object display;
6439 struct w32_display_info *dpyinfo = check_x_display_info (display);
6441 return make_number (dpyinfo->n_planes * dpyinfo->n_cbits);
6444 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
6445 0, 1, 0,
6446 doc: /* Returns the number of color cells of DISPLAY.
6447 The optional argument DISPLAY specifies which display to ask about.
6448 DISPLAY should be either a frame or a display name (a string).
6449 If omitted or nil, that stands for the selected frame's display. */)
6450 (display)
6451 Lisp_Object display;
6453 struct w32_display_info *dpyinfo = check_x_display_info (display);
6454 HDC hdc;
6455 int cap;
6457 hdc = GetDC (dpyinfo->root_window);
6458 if (dpyinfo->has_palette)
6459 cap = GetDeviceCaps (hdc, SIZEPALETTE);
6460 else
6461 cap = GetDeviceCaps (hdc, NUMCOLORS);
6463 /* We force 24+ bit depths to 24-bit, both to prevent an overflow
6464 and because probably is more meaningful on Windows anyway */
6465 if (cap < 0)
6466 cap = 1 << min(dpyinfo->n_planes * dpyinfo->n_cbits, 24);
6468 ReleaseDC (dpyinfo->root_window, hdc);
6470 return make_number (cap);
6473 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
6474 Sx_server_max_request_size,
6475 0, 1, 0,
6476 doc: /* Returns the maximum request size of the server of DISPLAY.
6477 The optional argument DISPLAY specifies which display to ask about.
6478 DISPLAY should be either a frame or a display name (a string).
6479 If omitted or nil, that stands for the selected frame's display. */)
6480 (display)
6481 Lisp_Object display;
6483 struct w32_display_info *dpyinfo = check_x_display_info (display);
6485 return make_number (1);
6488 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
6489 doc: /* Returns the "vendor ID" string of the W32 system (Microsoft).
6490 The optional argument DISPLAY specifies which display to ask about.
6491 DISPLAY should be either a frame or a display name (a string).
6492 If omitted or nil, that stands for the selected frame's display. */)
6493 (display)
6494 Lisp_Object display;
6496 return build_string ("Microsoft Corp.");
6499 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
6500 doc: /* Returns the version numbers of the server of DISPLAY.
6501 The value is a list of three integers: the major and minor
6502 version numbers of the X Protocol in use, and the distributor-specific release
6503 number. See also the function `x-server-vendor'.
6505 The optional argument DISPLAY specifies which display to ask about.
6506 DISPLAY should be either a frame or a display name (a string).
6507 If omitted or nil, that stands for the selected frame's display. */)
6508 (display)
6509 Lisp_Object display;
6511 return Fcons (make_number (w32_major_version),
6512 Fcons (make_number (w32_minor_version),
6513 Fcons (make_number (w32_build_number), Qnil)));
6516 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
6517 doc: /* Returns the number of screens on the server of DISPLAY.
6518 The optional argument DISPLAY specifies which display to ask about.
6519 DISPLAY should be either a frame or a display name (a string).
6520 If omitted or nil, that stands for the selected frame's display. */)
6521 (display)
6522 Lisp_Object display;
6524 return make_number (1);
6527 DEFUN ("x-display-mm-height", Fx_display_mm_height,
6528 Sx_display_mm_height, 0, 1, 0,
6529 doc: /* Returns the height in millimeters of DISPLAY.
6530 The optional argument DISPLAY specifies which display to ask about.
6531 DISPLAY should be either a frame or a display name (a string).
6532 If omitted or nil, that stands for the selected frame's display. */)
6533 (display)
6534 Lisp_Object display;
6536 struct w32_display_info *dpyinfo = check_x_display_info (display);
6537 HDC hdc;
6538 int cap;
6540 hdc = GetDC (dpyinfo->root_window);
6542 cap = GetDeviceCaps (hdc, VERTSIZE);
6544 ReleaseDC (dpyinfo->root_window, hdc);
6546 return make_number (cap);
6549 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
6550 doc: /* Returns the width in millimeters of DISPLAY.
6551 The optional argument DISPLAY specifies which display to ask about.
6552 DISPLAY should be either a frame or a display name (a string).
6553 If omitted or nil, that stands for the selected frame's display. */)
6554 (display)
6555 Lisp_Object display;
6557 struct w32_display_info *dpyinfo = check_x_display_info (display);
6559 HDC hdc;
6560 int cap;
6562 hdc = GetDC (dpyinfo->root_window);
6564 cap = GetDeviceCaps (hdc, HORZSIZE);
6566 ReleaseDC (dpyinfo->root_window, hdc);
6568 return make_number (cap);
6571 DEFUN ("x-display-backing-store", Fx_display_backing_store,
6572 Sx_display_backing_store, 0, 1, 0,
6573 doc: /* Returns an indication of whether DISPLAY does backing store.
6574 The value may be `always', `when-mapped', or `not-useful'.
6575 The optional argument DISPLAY specifies which display to ask about.
6576 DISPLAY should be either a frame or a display name (a string).
6577 If omitted or nil, that stands for the selected frame's display. */)
6578 (display)
6579 Lisp_Object display;
6581 return intern ("not-useful");
6584 DEFUN ("x-display-visual-class", Fx_display_visual_class,
6585 Sx_display_visual_class, 0, 1, 0,
6586 doc: /* Returns the visual class of DISPLAY.
6587 The value is one of the symbols `static-gray', `gray-scale',
6588 `static-color', `pseudo-color', `true-color', or `direct-color'.
6590 The optional argument DISPLAY specifies which display to ask about.
6591 DISPLAY should be either a frame or a display name (a string).
6592 If omitted or nil, that stands for the selected frame's display. */)
6593 (display)
6594 Lisp_Object display;
6596 struct w32_display_info *dpyinfo = check_x_display_info (display);
6597 Lisp_Object result = Qnil;
6599 if (dpyinfo->has_palette)
6600 result = intern ("pseudo-color");
6601 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 1)
6602 result = intern ("static-grey");
6603 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 4)
6604 result = intern ("static-color");
6605 else if (dpyinfo->n_planes * dpyinfo->n_cbits > 8)
6606 result = intern ("true-color");
6608 return result;
6611 DEFUN ("x-display-save-under", Fx_display_save_under,
6612 Sx_display_save_under, 0, 1, 0,
6613 doc: /* Returns t if DISPLAY supports the save-under feature.
6614 The optional argument DISPLAY specifies which display to ask about.
6615 DISPLAY should be either a frame or a display name (a string).
6616 If omitted or nil, that stands for the selected frame's display. */)
6617 (display)
6618 Lisp_Object display;
6620 return Qnil;
6624 x_pixel_width (f)
6625 register struct frame *f;
6627 return FRAME_PIXEL_WIDTH (f);
6631 x_pixel_height (f)
6632 register struct frame *f;
6634 return FRAME_PIXEL_HEIGHT (f);
6638 x_char_width (f)
6639 register struct frame *f;
6641 return FRAME_COLUMN_WIDTH (f);
6645 x_char_height (f)
6646 register struct frame *f;
6648 return FRAME_LINE_HEIGHT (f);
6652 x_screen_planes (f)
6653 register struct frame *f;
6655 return FRAME_W32_DISPLAY_INFO (f)->n_planes;
6658 /* Return the display structure for the display named NAME.
6659 Open a new connection if necessary. */
6661 struct w32_display_info *
6662 x_display_info_for_name (name)
6663 Lisp_Object name;
6665 Lisp_Object names;
6666 struct w32_display_info *dpyinfo;
6668 CHECK_STRING (name);
6670 for (dpyinfo = &one_w32_display_info, names = w32_display_name_list;
6671 dpyinfo;
6672 dpyinfo = dpyinfo->next, names = XCDR (names))
6674 Lisp_Object tem;
6675 tem = Fstring_equal (XCAR (XCAR (names)), name);
6676 if (!NILP (tem))
6677 return dpyinfo;
6680 /* Use this general default value to start with. */
6681 Vx_resource_name = Vinvocation_name;
6683 validate_x_resource_name ();
6685 dpyinfo = w32_term_init (name, (unsigned char *)0,
6686 (char *) SDATA (Vx_resource_name));
6688 if (dpyinfo == 0)
6689 error ("Cannot connect to server %s", SDATA (name));
6691 w32_in_use = 1;
6692 XSETFASTINT (Vwindow_system_version, 3);
6694 return dpyinfo;
6697 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
6698 1, 3, 0, doc: /* Open a connection to a server.
6699 DISPLAY is the name of the display to connect to.
6700 Optional second arg XRM-STRING is a string of resources in xrdb format.
6701 If the optional third arg MUST-SUCCEED is non-nil,
6702 terminate Emacs if we can't open the connection. */)
6703 (display, xrm_string, must_succeed)
6704 Lisp_Object display, xrm_string, must_succeed;
6706 unsigned char *xrm_option;
6707 struct w32_display_info *dpyinfo;
6709 /* If initialization has already been done, return now to avoid
6710 overwriting critical parts of one_w32_display_info. */
6711 if (w32_in_use)
6712 return Qnil;
6714 CHECK_STRING (display);
6715 if (! NILP (xrm_string))
6716 CHECK_STRING (xrm_string);
6718 if (! EQ (Vwindow_system, intern ("w32")))
6719 error ("Not using Microsoft Windows");
6721 /* Allow color mapping to be defined externally; first look in user's
6722 HOME directory, then in Emacs etc dir for a file called rgb.txt. */
6724 Lisp_Object color_file;
6725 struct gcpro gcpro1;
6727 color_file = build_string("~/rgb.txt");
6729 GCPRO1 (color_file);
6731 if (NILP (Ffile_readable_p (color_file)))
6732 color_file =
6733 Fexpand_file_name (build_string ("rgb.txt"),
6734 Fsymbol_value (intern ("data-directory")));
6736 Vw32_color_map = Fw32_load_color_file (color_file);
6738 UNGCPRO;
6740 if (NILP (Vw32_color_map))
6741 Vw32_color_map = Fw32_default_color_map ();
6743 /* Merge in system logical colors. */
6744 add_system_logical_colors_to_map (&Vw32_color_map);
6746 if (! NILP (xrm_string))
6747 xrm_option = (unsigned char *) SDATA (xrm_string);
6748 else
6749 xrm_option = (unsigned char *) 0;
6751 /* Use this general default value to start with. */
6752 /* First remove .exe suffix from invocation-name - it looks ugly. */
6754 char basename[ MAX_PATH ], *str;
6756 strcpy (basename, SDATA (Vinvocation_name));
6757 str = strrchr (basename, '.');
6758 if (str) *str = 0;
6759 Vinvocation_name = build_string (basename);
6761 Vx_resource_name = Vinvocation_name;
6763 validate_x_resource_name ();
6765 /* This is what opens the connection and sets x_current_display.
6766 This also initializes many symbols, such as those used for input. */
6767 dpyinfo = w32_term_init (display, xrm_option,
6768 (char *) SDATA (Vx_resource_name));
6770 if (dpyinfo == 0)
6772 if (!NILP (must_succeed))
6773 fatal ("Cannot connect to server %s.\n",
6774 SDATA (display));
6775 else
6776 error ("Cannot connect to server %s", SDATA (display));
6779 w32_in_use = 1;
6781 XSETFASTINT (Vwindow_system_version, 3);
6782 return Qnil;
6785 DEFUN ("x-close-connection", Fx_close_connection,
6786 Sx_close_connection, 1, 1, 0,
6787 doc: /* Close the connection to DISPLAY's server.
6788 For DISPLAY, specify either a frame or a display name (a string).
6789 If DISPLAY is nil, that stands for the selected frame's display. */)
6790 (display)
6791 Lisp_Object display;
6793 struct w32_display_info *dpyinfo = check_x_display_info (display);
6794 int i;
6796 if (dpyinfo->reference_count > 0)
6797 error ("Display still has frames on it");
6799 BLOCK_INPUT;
6800 /* Free the fonts in the font table. */
6801 for (i = 0; i < dpyinfo->n_fonts; i++)
6802 if (dpyinfo->font_table[i].name)
6804 if (dpyinfo->font_table[i].name != dpyinfo->font_table[i].full_name)
6805 xfree (dpyinfo->font_table[i].full_name);
6806 xfree (dpyinfo->font_table[i].name);
6807 w32_unload_font (dpyinfo, dpyinfo->font_table[i].font);
6809 x_destroy_all_bitmaps (dpyinfo);
6811 x_delete_display (dpyinfo);
6812 UNBLOCK_INPUT;
6814 return Qnil;
6817 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
6818 doc: /* Return the list of display names that Emacs has connections to. */)
6821 Lisp_Object tail, result;
6823 result = Qnil;
6824 for (tail = w32_display_name_list; ! NILP (tail); tail = XCDR (tail))
6825 result = Fcons (XCAR (XCAR (tail)), result);
6827 return result;
6830 DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
6831 doc: /* This is a noop on W32 systems. */)
6832 (on, display)
6833 Lisp_Object display, on;
6835 return Qnil;
6840 /***********************************************************************
6841 Window properties
6842 ***********************************************************************/
6844 DEFUN ("x-change-window-property", Fx_change_window_property,
6845 Sx_change_window_property, 2, 6, 0,
6846 doc: /* Change window property PROP to VALUE on the X window of FRAME.
6847 VALUE may be a string or a list of conses, numbers and/or strings.
6848 If an element in the list is a string, it is converted to
6849 an Atom and the value of the Atom is used. If an element is a cons,
6850 it is converted to a 32 bit number where the car is the 16 top bits and the
6851 cdr is the lower 16 bits.
6852 FRAME nil or omitted means use the selected frame.
6853 If TYPE is given and non-nil, it is the name of the type of VALUE.
6854 If TYPE is not given or nil, the type is STRING.
6855 FORMAT gives the size in bits of each element if VALUE is a list.
6856 It must be one of 8, 16 or 32.
6857 If VALUE is a string or FORMAT is nil or not given, FORMAT defaults to 8.
6858 If OUTER_P is non-nil, the property is changed for the outer X window of
6859 FRAME. Default is to change on the edit X window.
6861 Value is VALUE. */)
6862 (prop, value, frame, type, format, outer_p)
6863 Lisp_Object prop, value, frame, type, format, outer_p;
6865 #if 0 /* TODO : port window properties to W32 */
6866 struct frame *f = check_x_frame (frame);
6867 Atom prop_atom;
6869 CHECK_STRING (prop);
6870 CHECK_STRING (value);
6872 BLOCK_INPUT;
6873 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
6874 XChangeProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
6875 prop_atom, XA_STRING, 8, PropModeReplace,
6876 SDATA (value), SCHARS (value));
6878 /* Make sure the property is set when we return. */
6879 XFlush (FRAME_W32_DISPLAY (f));
6880 UNBLOCK_INPUT;
6882 #endif /* TODO */
6884 return value;
6888 DEFUN ("x-delete-window-property", Fx_delete_window_property,
6889 Sx_delete_window_property, 1, 2, 0,
6890 doc: /* Remove window property PROP from X window of FRAME.
6891 FRAME nil or omitted means use the selected frame. Value is PROP. */)
6892 (prop, frame)
6893 Lisp_Object prop, frame;
6895 #if 0 /* TODO : port window properties to W32 */
6897 struct frame *f = check_x_frame (frame);
6898 Atom prop_atom;
6900 CHECK_STRING (prop);
6901 BLOCK_INPUT;
6902 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
6903 XDeleteProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), prop_atom);
6905 /* Make sure the property is removed when we return. */
6906 XFlush (FRAME_W32_DISPLAY (f));
6907 UNBLOCK_INPUT;
6908 #endif /* TODO */
6910 return prop;
6914 DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
6915 1, 2, 0,
6916 doc: /* Value is the value of window property PROP on FRAME.
6917 If FRAME is nil or omitted, use the selected frame. Value is nil
6918 if FRAME hasn't a property with name PROP or if PROP has no string
6919 value. */)
6920 (prop, frame)
6921 Lisp_Object prop, frame;
6923 #if 0 /* TODO : port window properties to W32 */
6925 struct frame *f = check_x_frame (frame);
6926 Atom prop_atom;
6927 int rc;
6928 Lisp_Object prop_value = Qnil;
6929 char *tmp_data = NULL;
6930 Atom actual_type;
6931 int actual_format;
6932 unsigned long actual_size, bytes_remaining;
6934 CHECK_STRING (prop);
6935 BLOCK_INPUT;
6936 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
6937 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
6938 prop_atom, 0, 0, False, XA_STRING,
6939 &actual_type, &actual_format, &actual_size,
6940 &bytes_remaining, (unsigned char **) &tmp_data);
6941 if (rc == Success)
6943 int size = bytes_remaining;
6945 XFree (tmp_data);
6946 tmp_data = NULL;
6948 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
6949 prop_atom, 0, bytes_remaining,
6950 False, XA_STRING,
6951 &actual_type, &actual_format,
6952 &actual_size, &bytes_remaining,
6953 (unsigned char **) &tmp_data);
6954 if (rc == Success)
6955 prop_value = make_string (tmp_data, size);
6957 XFree (tmp_data);
6960 UNBLOCK_INPUT;
6962 return prop_value;
6964 #endif /* TODO */
6965 return Qnil;
6970 /***********************************************************************
6971 Busy cursor
6972 ***********************************************************************/
6974 /* If non-null, an asynchronous timer that, when it expires, displays
6975 an hourglass cursor on all frames. */
6977 static struct atimer *hourglass_atimer;
6979 /* Non-zero means an hourglass cursor is currently shown. */
6981 static int hourglass_shown_p;
6983 /* Number of seconds to wait before displaying an hourglass cursor. */
6985 static Lisp_Object Vhourglass_delay;
6987 /* Default number of seconds to wait before displaying an hourglass
6988 cursor. */
6990 #define DEFAULT_HOURGLASS_DELAY 1
6992 /* Function prototypes. */
6994 static void show_hourglass P_ ((struct atimer *));
6995 static void hide_hourglass P_ ((void));
6998 /* Cancel a currently active hourglass timer, and start a new one. */
7000 void
7001 start_hourglass ()
7003 #if 0 /* TODO: cursor shape changes. */
7004 EMACS_TIME delay;
7005 int secs, usecs = 0;
7007 cancel_hourglass ();
7009 if (INTEGERP (Vhourglass_delay)
7010 && XINT (Vhourglass_delay) > 0)
7011 secs = XFASTINT (Vhourglass_delay);
7012 else if (FLOATP (Vhourglass_delay)
7013 && XFLOAT_DATA (Vhourglass_delay) > 0)
7015 Lisp_Object tem;
7016 tem = Ftruncate (Vhourglass_delay, Qnil);
7017 secs = XFASTINT (tem);
7018 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
7020 else
7021 secs = DEFAULT_HOURGLASS_DELAY;
7023 EMACS_SET_SECS_USECS (delay, secs, usecs);
7024 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
7025 show_hourglass, NULL);
7026 #endif
7030 /* Cancel the hourglass cursor timer if active, hide an hourglass
7031 cursor if shown. */
7033 void
7034 cancel_hourglass ()
7036 if (hourglass_atimer)
7038 cancel_atimer (hourglass_atimer);
7039 hourglass_atimer = NULL;
7042 if (hourglass_shown_p)
7043 hide_hourglass ();
7047 /* Timer function of hourglass_atimer. TIMER is equal to
7048 hourglass_atimer.
7050 Display an hourglass cursor on all frames by mapping the frames'
7051 hourglass_window. Set the hourglass_p flag in the frames'
7052 output_data.x structure to indicate that an hourglass cursor is
7053 shown on the frames. */
7055 static void
7056 show_hourglass (timer)
7057 struct atimer *timer;
7059 #if 0 /* TODO: cursor shape changes. */
7060 /* The timer implementation will cancel this timer automatically
7061 after this function has run. Set hourglass_atimer to null
7062 so that we know the timer doesn't have to be canceled. */
7063 hourglass_atimer = NULL;
7065 if (!hourglass_shown_p)
7067 Lisp_Object rest, frame;
7069 BLOCK_INPUT;
7071 FOR_EACH_FRAME (rest, frame)
7072 if (FRAME_W32_P (XFRAME (frame)))
7074 struct frame *f = XFRAME (frame);
7076 f->output_data.w32->hourglass_p = 1;
7078 if (!f->output_data.w32->hourglass_window)
7080 unsigned long mask = CWCursor;
7081 XSetWindowAttributes attrs;
7083 attrs.cursor = f->output_data.w32->hourglass_cursor;
7085 f->output_data.w32->hourglass_window
7086 = XCreateWindow (FRAME_X_DISPLAY (f),
7087 FRAME_OUTER_WINDOW (f),
7088 0, 0, 32000, 32000, 0, 0,
7089 InputOnly,
7090 CopyFromParent,
7091 mask, &attrs);
7094 XMapRaised (FRAME_X_DISPLAY (f),
7095 f->output_data.w32->hourglass_window);
7096 XFlush (FRAME_X_DISPLAY (f));
7099 hourglass_shown_p = 1;
7100 UNBLOCK_INPUT;
7102 #endif
7106 /* Hide the hourglass cursor on all frames, if it is currently shown. */
7108 static void
7109 hide_hourglass ()
7111 #if 0 /* TODO: cursor shape changes. */
7112 if (hourglass_shown_p)
7114 Lisp_Object rest, frame;
7116 BLOCK_INPUT;
7117 FOR_EACH_FRAME (rest, frame)
7119 struct frame *f = XFRAME (frame);
7121 if (FRAME_W32_P (f)
7122 /* Watch out for newly created frames. */
7123 && f->output_data.x->hourglass_window)
7125 XUnmapWindow (FRAME_X_DISPLAY (f),
7126 f->output_data.x->hourglass_window);
7127 /* Sync here because XTread_socket looks at the
7128 hourglass_p flag that is reset to zero below. */
7129 XSync (FRAME_X_DISPLAY (f), False);
7130 f->output_data.x->hourglass_p = 0;
7134 hourglass_shown_p = 0;
7135 UNBLOCK_INPUT;
7137 #endif
7142 /***********************************************************************
7143 Tool tips
7144 ***********************************************************************/
7146 static Lisp_Object x_create_tip_frame P_ ((struct w32_display_info *,
7147 Lisp_Object, Lisp_Object));
7148 static void compute_tip_xy P_ ((struct frame *, Lisp_Object, Lisp_Object,
7149 Lisp_Object, int, int, int *, int *));
7151 /* The frame of a currently visible tooltip. */
7153 Lisp_Object tip_frame;
7155 /* If non-nil, a timer started that hides the last tooltip when it
7156 fires. */
7158 Lisp_Object tip_timer;
7159 Window tip_window;
7161 /* If non-nil, a vector of 3 elements containing the last args
7162 with which x-show-tip was called. See there. */
7164 Lisp_Object last_show_tip_args;
7166 /* Maximum size for tooltips; a cons (COLUMNS . ROWS). */
7168 Lisp_Object Vx_max_tooltip_size;
7171 static Lisp_Object
7172 unwind_create_tip_frame (frame)
7173 Lisp_Object frame;
7175 Lisp_Object deleted;
7177 deleted = unwind_create_frame (frame);
7178 if (EQ (deleted, Qt))
7180 tip_window = NULL;
7181 tip_frame = Qnil;
7184 return deleted;
7188 /* Create a frame for a tooltip on the display described by DPYINFO.
7189 PARMS is a list of frame parameters. TEXT is the string to
7190 display in the tip frame. Value is the frame.
7192 Note that functions called here, esp. x_default_parameter can
7193 signal errors, for instance when a specified color name is
7194 undefined. We have to make sure that we're in a consistent state
7195 when this happens. */
7197 static Lisp_Object
7198 x_create_tip_frame (dpyinfo, parms, text)
7199 struct w32_display_info *dpyinfo;
7200 Lisp_Object parms, text;
7202 struct frame *f;
7203 Lisp_Object frame, tem;
7204 Lisp_Object name;
7205 long window_prompting = 0;
7206 int width, height;
7207 int count = SPECPDL_INDEX ();
7208 struct gcpro gcpro1, gcpro2, gcpro3;
7209 struct kboard *kb;
7210 int face_change_count_before = face_change_count;
7211 Lisp_Object buffer;
7212 struct buffer *old_buffer;
7214 check_w32 ();
7216 /* Use this general default value to start with until we know if
7217 this frame has a specified name. */
7218 Vx_resource_name = Vinvocation_name;
7220 #ifdef MULTI_KBOARD
7221 kb = dpyinfo->kboard;
7222 #else
7223 kb = &the_only_kboard;
7224 #endif
7226 /* Get the name of the frame to use for resource lookup. */
7227 name = w32_get_arg (parms, Qname, "name", "Name", RES_TYPE_STRING);
7228 if (!STRINGP (name)
7229 && !EQ (name, Qunbound)
7230 && !NILP (name))
7231 error ("Invalid frame name--not a string or nil");
7232 Vx_resource_name = name;
7234 frame = Qnil;
7235 GCPRO3 (parms, name, frame);
7236 /* Make a frame without minibuffer nor mode-line. */
7237 f = make_frame (0);
7238 f->wants_modeline = 0;
7239 XSETFRAME (frame, f);
7241 buffer = Fget_buffer_create (build_string (" *tip*"));
7242 Fset_window_buffer (FRAME_ROOT_WINDOW (f), buffer, Qnil);
7243 old_buffer = current_buffer;
7244 set_buffer_internal_1 (XBUFFER (buffer));
7245 current_buffer->truncate_lines = Qnil;
7246 specbind (Qinhibit_read_only, Qt);
7247 specbind (Qinhibit_modification_hooks, Qt);
7248 Ferase_buffer ();
7249 Finsert (1, &text);
7250 set_buffer_internal_1 (old_buffer);
7252 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
7253 record_unwind_protect (unwind_create_tip_frame, frame);
7255 /* By setting the output method, we're essentially saying that
7256 the frame is live, as per FRAME_LIVE_P. If we get a signal
7257 from this point on, x_destroy_window might screw up reference
7258 counts etc. */
7259 f->output_method = output_w32;
7260 f->output_data.w32 =
7261 (struct w32_output *) xmalloc (sizeof (struct w32_output));
7262 bzero (f->output_data.w32, sizeof (struct w32_output));
7264 FRAME_FONTSET (f) = -1;
7265 f->icon_name = Qnil;
7267 #if 0 /* GLYPH_DEBUG TODO: image support. */
7268 image_cache_refcount = FRAME_X_IMAGE_CACHE (f)->refcount;
7269 dpyinfo_refcount = dpyinfo->reference_count;
7270 #endif /* GLYPH_DEBUG */
7271 #ifdef MULTI_KBOARD
7272 FRAME_KBOARD (f) = kb;
7273 #endif
7274 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
7275 f->output_data.w32->explicit_parent = 0;
7277 /* Set the name; the functions to which we pass f expect the name to
7278 be set. */
7279 if (EQ (name, Qunbound) || NILP (name))
7281 f->name = build_string (dpyinfo->w32_id_name);
7282 f->explicit_name = 0;
7284 else
7286 f->name = name;
7287 f->explicit_name = 1;
7288 /* use the frame's title when getting resources for this frame. */
7289 specbind (Qx_resource_name, name);
7292 /* Extract the window parameters from the supplied values
7293 that are needed to determine window geometry. */
7295 Lisp_Object font;
7297 font = w32_get_arg (parms, Qfont, "font", "Font", RES_TYPE_STRING);
7299 BLOCK_INPUT;
7300 /* First, try whatever font the caller has specified. */
7301 if (STRINGP (font))
7303 tem = Fquery_fontset (font, Qnil);
7304 if (STRINGP (tem))
7305 font = x_new_fontset (f, SDATA (tem));
7306 else
7307 font = x_new_font (f, SDATA (font));
7310 /* Try out a font which we hope has bold and italic variations. */
7311 if (!STRINGP (font))
7312 font = x_new_font (f, "-*-Courier New-normal-r-*-*-*-100-*-*-c-*-iso8859-1");
7313 if (! STRINGP (font))
7314 font = x_new_font (f, "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1");
7315 /* If those didn't work, look for something which will at least work. */
7316 if (! STRINGP (font))
7317 font = x_new_font (f, "-*-Fixedsys-normal-r-*-*-12-*-*-*-c-*-iso8859-1");
7318 UNBLOCK_INPUT;
7319 if (! STRINGP (font))
7320 font = build_string ("Fixedsys");
7322 x_default_parameter (f, parms, Qfont, font,
7323 "font", "Font", RES_TYPE_STRING);
7326 x_default_parameter (f, parms, Qborder_width, make_number (2),
7327 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
7328 /* This defaults to 2 in order to match xterm. We recognize either
7329 internalBorderWidth or internalBorder (which is what xterm calls
7330 it). */
7331 if (NILP (Fassq (Qinternal_border_width, parms)))
7333 Lisp_Object value;
7335 value = w32_get_arg (parms, Qinternal_border_width,
7336 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
7337 if (! EQ (value, Qunbound))
7338 parms = Fcons (Fcons (Qinternal_border_width, value),
7339 parms);
7341 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
7342 "internalBorderWidth", "internalBorderWidth",
7343 RES_TYPE_NUMBER);
7345 /* Also do the stuff which must be set before the window exists. */
7346 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
7347 "foreground", "Foreground", RES_TYPE_STRING);
7348 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
7349 "background", "Background", RES_TYPE_STRING);
7350 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
7351 "pointerColor", "Foreground", RES_TYPE_STRING);
7352 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
7353 "cursorColor", "Foreground", RES_TYPE_STRING);
7354 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
7355 "borderColor", "BorderColor", RES_TYPE_STRING);
7357 /* Init faces before x_default_parameter is called for scroll-bar
7358 parameters because that function calls x_set_scroll_bar_width,
7359 which calls change_frame_size, which calls Fset_window_buffer,
7360 which runs hooks, which call Fvertical_motion. At the end, we
7361 end up in init_iterator with a null face cache, which should not
7362 happen. */
7363 init_frame_faces (f);
7365 f->output_data.w32->dwStyle = WS_BORDER | WS_POPUP | WS_DISABLED;
7366 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
7368 window_prompting = x_figure_window_size (f, parms, 0);
7370 /* No fringes on tip frame. */
7371 f->fringe_cols = 0;
7372 f->left_fringe_width = 0;
7373 f->right_fringe_width = 0;
7375 BLOCK_INPUT;
7376 my_create_tip_window (f);
7377 UNBLOCK_INPUT;
7379 x_make_gc (f);
7381 x_default_parameter (f, parms, Qauto_raise, Qnil,
7382 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
7383 x_default_parameter (f, parms, Qauto_lower, Qnil,
7384 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
7385 x_default_parameter (f, parms, Qcursor_type, Qbox,
7386 "cursorType", "CursorType", RES_TYPE_SYMBOL);
7388 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
7389 Change will not be effected unless different from the current
7390 FRAME_LINES (f). */
7391 width = FRAME_COLS (f);
7392 height = FRAME_LINES (f);
7393 FRAME_LINES (f) = 0;
7394 SET_FRAME_COLS (f, 0);
7395 change_frame_size (f, height, width, 1, 0, 0);
7397 /* Add `tooltip' frame parameter's default value. */
7398 if (NILP (Fframe_parameter (frame, intern ("tooltip"))))
7399 Fmodify_frame_parameters (frame, Fcons (Fcons (intern ("tooltip"), Qt),
7400 Qnil));
7402 /* Set up faces after all frame parameters are known. This call
7403 also merges in face attributes specified for new frames.
7405 Frame parameters may be changed if .Xdefaults contains
7406 specifications for the default font. For example, if there is an
7407 `Emacs.default.attributeBackground: pink', the `background-color'
7408 attribute of the frame get's set, which let's the internal border
7409 of the tooltip frame appear in pink. Prevent this. */
7411 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
7413 /* Set tip_frame here, so that */
7414 tip_frame = frame;
7415 call1 (Qface_set_after_frame_default, frame);
7417 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
7418 Fmodify_frame_parameters (frame, Fcons (Fcons (Qbackground_color, bg),
7419 Qnil));
7422 f->no_split = 1;
7424 UNGCPRO;
7426 /* It is now ok to make the frame official even if we get an error
7427 below. And the frame needs to be on Vframe_list or making it
7428 visible won't work. */
7429 Vframe_list = Fcons (frame, Vframe_list);
7431 /* Now that the frame is official, it counts as a reference to
7432 its display. */
7433 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
7435 /* Setting attributes of faces of the tooltip frame from resources
7436 and similar will increment face_change_count, which leads to the
7437 clearing of all current matrices. Since this isn't necessary
7438 here, avoid it by resetting face_change_count to the value it
7439 had before we created the tip frame. */
7440 face_change_count = face_change_count_before;
7442 /* Discard the unwind_protect. */
7443 return unbind_to (count, frame);
7447 /* Compute where to display tip frame F. PARMS is the list of frame
7448 parameters for F. DX and DY are specified offsets from the current
7449 location of the mouse. WIDTH and HEIGHT are the width and height
7450 of the tooltip. Return coordinates relative to the root window of
7451 the display in *ROOT_X, and *ROOT_Y. */
7453 static void
7454 compute_tip_xy (f, parms, dx, dy, width, height, root_x, root_y)
7455 struct frame *f;
7456 Lisp_Object parms, dx, dy;
7457 int width, height;
7458 int *root_x, *root_y;
7460 Lisp_Object left, top;
7462 /* User-specified position? */
7463 left = Fcdr (Fassq (Qleft, parms));
7464 top = Fcdr (Fassq (Qtop, parms));
7466 /* Move the tooltip window where the mouse pointer is. Resize and
7467 show it. */
7468 if (!INTEGERP (left) || !INTEGERP (top))
7470 POINT pt;
7472 BLOCK_INPUT;
7473 GetCursorPos (&pt);
7474 *root_x = pt.x;
7475 *root_y = pt.y;
7476 UNBLOCK_INPUT;
7479 if (INTEGERP (top))
7480 *root_y = XINT (top);
7481 else if (*root_y + XINT (dy) <= 0)
7482 *root_y = 0; /* Can happen for negative dy */
7483 else if (*root_y + XINT (dy) + height <= FRAME_W32_DISPLAY_INFO (f)->height)
7484 /* It fits below the pointer */
7485 *root_y += XINT (dy);
7486 else if (height + XINT (dy) <= *root_y)
7487 /* It fits above the pointer. */
7488 *root_y -= height + XINT (dy);
7489 else
7490 /* Put it on the top. */
7491 *root_y = 0;
7493 if (INTEGERP (left))
7494 *root_x = XINT (left);
7495 else if (*root_x + XINT (dx) <= 0)
7496 *root_x = 0; /* Can happen for negative dx */
7497 else if (*root_x + XINT (dx) + width <= FRAME_W32_DISPLAY_INFO (f)->width)
7498 /* It fits to the right of the pointer. */
7499 *root_x += XINT (dx);
7500 else if (width + XINT (dx) <= *root_x)
7501 /* It fits to the left of the pointer. */
7502 *root_x -= width + XINT (dx);
7503 else
7504 /* Put it left justified on the screen -- it ought to fit that way. */
7505 *root_x = 0;
7509 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
7510 doc: /* Show STRING in a \"tooltip\" window on frame FRAME.
7511 A tooltip window is a small window displaying a string.
7513 This is an internal function; Lisp code should call `tooltip-show'.
7515 FRAME nil or omitted means use the selected frame.
7517 PARMS is an optional list of frame parameters which can be
7518 used to change the tooltip's appearance.
7520 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
7521 means use the default timeout of 5 seconds.
7523 If the list of frame parameters PARMS contains a `left' parameter,
7524 the tooltip is displayed at that x-position. Otherwise it is
7525 displayed at the mouse position, with offset DX added (default is 5 if
7526 DX isn't specified). Likewise for the y-position; if a `top' frame
7527 parameter is specified, it determines the y-position of the tooltip
7528 window, otherwise it is displayed at the mouse position, with offset
7529 DY added (default is -10).
7531 A tooltip's maximum size is specified by `x-max-tooltip-size'.
7532 Text larger than the specified size is clipped. */)
7533 (string, frame, parms, timeout, dx, dy)
7534 Lisp_Object string, frame, parms, timeout, dx, dy;
7536 struct frame *f;
7537 struct window *w;
7538 int root_x, root_y;
7539 struct buffer *old_buffer;
7540 struct text_pos pos;
7541 int i, width, height;
7542 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7543 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7544 int count = SPECPDL_INDEX ();
7546 specbind (Qinhibit_redisplay, Qt);
7548 GCPRO4 (string, parms, frame, timeout);
7550 CHECK_STRING (string);
7551 f = check_x_frame (frame);
7552 if (NILP (timeout))
7553 timeout = make_number (5);
7554 else
7555 CHECK_NATNUM (timeout);
7557 if (NILP (dx))
7558 dx = make_number (5);
7559 else
7560 CHECK_NUMBER (dx);
7562 if (NILP (dy))
7563 dy = make_number (-10);
7564 else
7565 CHECK_NUMBER (dy);
7567 if (NILP (last_show_tip_args))
7568 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
7570 if (!NILP (tip_frame))
7572 Lisp_Object last_string = AREF (last_show_tip_args, 0);
7573 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
7574 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
7576 if (EQ (frame, last_frame)
7577 && !NILP (Fequal (last_string, string))
7578 && !NILP (Fequal (last_parms, parms)))
7580 struct frame *f = XFRAME (tip_frame);
7582 /* Only DX and DY have changed. */
7583 if (!NILP (tip_timer))
7585 Lisp_Object timer = tip_timer;
7586 tip_timer = Qnil;
7587 call1 (Qcancel_timer, timer);
7590 BLOCK_INPUT;
7591 compute_tip_xy (f, parms, dx, dy, FRAME_PIXEL_WIDTH (f),
7592 FRAME_PIXEL_HEIGHT (f), &root_x, &root_y);
7594 /* Put tooltip in topmost group and in position. */
7595 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
7596 root_x, root_y, 0, 0,
7597 SWP_NOSIZE | SWP_NOACTIVATE);
7599 /* Ensure tooltip is on top of other topmost windows (eg menus). */
7600 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
7601 0, 0, 0, 0,
7602 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
7604 UNBLOCK_INPUT;
7605 goto start_timer;
7609 /* Hide a previous tip, if any. */
7610 Fx_hide_tip ();
7612 ASET (last_show_tip_args, 0, string);
7613 ASET (last_show_tip_args, 1, frame);
7614 ASET (last_show_tip_args, 2, parms);
7616 /* Add default values to frame parameters. */
7617 if (NILP (Fassq (Qname, parms)))
7618 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
7619 if (NILP (Fassq (Qinternal_border_width, parms)))
7620 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
7621 if (NILP (Fassq (Qborder_width, parms)))
7622 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
7623 if (NILP (Fassq (Qborder_color, parms)))
7624 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
7625 if (NILP (Fassq (Qbackground_color, parms)))
7626 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
7627 parms);
7629 /* Block input until the tip has been fully drawn, to avoid crashes
7630 when drawing tips in menus. */
7631 BLOCK_INPUT;
7633 /* Create a frame for the tooltip, and record it in the global
7634 variable tip_frame. */
7635 frame = x_create_tip_frame (FRAME_W32_DISPLAY_INFO (f), parms, string);
7636 f = XFRAME (frame);
7638 /* Set up the frame's root window. */
7639 w = XWINDOW (FRAME_ROOT_WINDOW (f));
7640 w->left_col = w->top_line = make_number (0);
7642 if (CONSP (Vx_max_tooltip_size)
7643 && INTEGERP (XCAR (Vx_max_tooltip_size))
7644 && XINT (XCAR (Vx_max_tooltip_size)) > 0
7645 && INTEGERP (XCDR (Vx_max_tooltip_size))
7646 && XINT (XCDR (Vx_max_tooltip_size)) > 0)
7648 w->total_cols = XCAR (Vx_max_tooltip_size);
7649 w->total_lines = XCDR (Vx_max_tooltip_size);
7651 else
7653 w->total_cols = make_number (80);
7654 w->total_lines = make_number (40);
7657 FRAME_TOTAL_COLS (f) = XINT (w->total_cols);
7658 adjust_glyphs (f);
7659 w->pseudo_window_p = 1;
7661 /* Display the tooltip text in a temporary buffer. */
7662 old_buffer = current_buffer;
7663 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->buffer));
7664 current_buffer->truncate_lines = Qnil;
7665 clear_glyph_matrix (w->desired_matrix);
7666 clear_glyph_matrix (w->current_matrix);
7667 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
7668 try_window (FRAME_ROOT_WINDOW (f), pos, 0);
7670 /* Compute width and height of the tooltip. */
7671 width = height = 0;
7672 for (i = 0; i < w->desired_matrix->nrows; ++i)
7674 struct glyph_row *row = &w->desired_matrix->rows[i];
7675 struct glyph *last;
7676 int row_width;
7678 /* Stop at the first empty row at the end. */
7679 if (!row->enabled_p || !row->displays_text_p)
7680 break;
7682 /* Let the row go over the full width of the frame. */
7683 row->full_width_p = 1;
7685 #ifdef TODO /* Investigate why some fonts need more width than is
7686 calculated for some tooltips. */
7687 /* There's a glyph at the end of rows that is use to place
7688 the cursor there. Don't include the width of this glyph. */
7689 if (row->used[TEXT_AREA])
7691 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
7692 row_width = row->pixel_width - last->pixel_width;
7694 else
7695 #endif
7696 row_width = row->pixel_width;
7698 /* TODO: find why tips do not draw along baseline as instructed. */
7699 height += row->height;
7700 width = max (width, row_width);
7703 /* Add the frame's internal border to the width and height the X
7704 window should have. */
7705 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
7706 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
7708 /* Move the tooltip window where the mouse pointer is. Resize and
7709 show it. */
7710 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
7713 /* Adjust Window size to take border into account. */
7714 RECT rect;
7715 rect.left = rect.top = 0;
7716 rect.right = width;
7717 rect.bottom = height;
7718 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
7719 FRAME_EXTERNAL_MENU_BAR (f));
7721 /* Position and size tooltip, and put it in the topmost group.
7722 The add-on of 3 to the 5th argument is a kludge: without it,
7723 some fonts cause the last character of the tip to be truncated,
7724 for some obscure reason. */
7725 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
7726 root_x, root_y, rect.right - rect.left + 3,
7727 rect.bottom - rect.top, SWP_NOACTIVATE);
7729 /* Ensure tooltip is on top of other topmost windows (eg menus). */
7730 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
7731 0, 0, 0, 0,
7732 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
7734 /* Let redisplay know that we have made the frame visible already. */
7735 f->async_visible = 1;
7737 ShowWindow (FRAME_W32_WINDOW (f), SW_SHOWNOACTIVATE);
7740 /* Draw into the window. */
7741 w->must_be_updated_p = 1;
7742 update_single_window (w, 1);
7744 UNBLOCK_INPUT;
7746 /* Restore original current buffer. */
7747 set_buffer_internal_1 (old_buffer);
7748 windows_or_buffers_changed = old_windows_or_buffers_changed;
7750 start_timer:
7751 /* Let the tip disappear after timeout seconds. */
7752 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
7753 intern ("x-hide-tip"));
7755 UNGCPRO;
7756 return unbind_to (count, Qnil);
7760 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
7761 doc: /* Hide the current tooltip window, if there is any.
7762 Value is t if tooltip was open, nil otherwise. */)
7765 int count;
7766 Lisp_Object deleted, frame, timer;
7767 struct gcpro gcpro1, gcpro2;
7769 /* Return quickly if nothing to do. */
7770 if (NILP (tip_timer) && NILP (tip_frame))
7771 return Qnil;
7773 frame = tip_frame;
7774 timer = tip_timer;
7775 GCPRO2 (frame, timer);
7776 tip_frame = tip_timer = deleted = Qnil;
7778 count = SPECPDL_INDEX ();
7779 specbind (Qinhibit_redisplay, Qt);
7780 specbind (Qinhibit_quit, Qt);
7782 if (!NILP (timer))
7783 call1 (Qcancel_timer, timer);
7785 if (FRAMEP (frame))
7787 Fdelete_frame (frame, Qnil);
7788 deleted = Qt;
7791 UNGCPRO;
7792 return unbind_to (count, deleted);
7797 /***********************************************************************
7798 File selection dialog
7799 ***********************************************************************/
7800 extern Lisp_Object Qfile_name_history;
7802 /* Callback for altering the behaviour of the Open File dialog.
7803 Makes the Filename text field contain "Current Directory" and be
7804 read-only when "Directories" is selected in the filter. This
7805 allows us to work around the fact that the standard Open File
7806 dialog does not support directories. */
7807 UINT CALLBACK
7808 file_dialog_callback (hwnd, msg, wParam, lParam)
7809 HWND hwnd;
7810 UINT msg;
7811 WPARAM wParam;
7812 LPARAM lParam;
7814 if (msg == WM_NOTIFY)
7816 OFNOTIFY * notify = (OFNOTIFY *)lParam;
7817 /* Detect when the Filter dropdown is changed. */
7818 if (notify->hdr.code == CDN_TYPECHANGE
7819 || notify->hdr.code == CDN_INITDONE)
7821 HWND dialog = GetParent (hwnd);
7822 HWND edit_control = GetDlgItem (dialog, FILE_NAME_TEXT_FIELD);
7824 /* Directories is in index 2. */
7825 if (notify->lpOFN->nFilterIndex == 2)
7827 CommDlg_OpenSave_SetControlText (dialog, FILE_NAME_TEXT_FIELD,
7828 "Current Directory");
7829 EnableWindow (edit_control, FALSE);
7831 else
7833 /* Don't override default filename on init done. */
7834 if (notify->hdr.code == CDN_TYPECHANGE)
7835 CommDlg_OpenSave_SetControlText (dialog,
7836 FILE_NAME_TEXT_FIELD, "");
7837 EnableWindow (edit_control, TRUE);
7841 return 0;
7844 /* Since we compile with _WIN32_WINNT set to 0x0400 (for NT4 compatibility)
7845 we end up with the old file dialogs. Define a big enough struct for the
7846 new dialog to trick GetOpenFileName into giving us the new dialogs on
7847 Windows 2000 and XP. */
7848 typedef struct
7850 OPENFILENAME real_details;
7851 void * pReserved;
7852 DWORD dwReserved;
7853 DWORD FlagsEx;
7854 } NEWOPENFILENAME;
7857 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
7858 doc: /* Read file name, prompting with PROMPT in directory DIR.
7859 Use a file selection dialog.
7860 Select DEFAULT-FILENAME in the dialog's file selection box, if
7861 specified. Ensure that file exists if MUSTMATCH is non-nil.
7862 If ONLY-DIR-P is non-nil, the user can only select directories. */)
7863 (prompt, dir, default_filename, mustmatch, only_dir_p)
7864 Lisp_Object prompt, dir, default_filename, mustmatch, only_dir_p;
7866 struct frame *f = SELECTED_FRAME ();
7867 Lisp_Object file = Qnil;
7868 int count = SPECPDL_INDEX ();
7869 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
7870 char filename[MAX_PATH + 1];
7871 char init_dir[MAX_PATH + 1];
7872 int default_filter_index = 1; /* 1: All Files, 2: Directories only */
7874 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
7875 CHECK_STRING (prompt);
7876 CHECK_STRING (dir);
7878 /* Create the dialog with PROMPT as title, using DIR as initial
7879 directory and using "*" as pattern. */
7880 dir = Fexpand_file_name (dir, Qnil);
7881 strncpy (init_dir, SDATA (ENCODE_FILE (dir)), MAX_PATH);
7882 init_dir[MAX_PATH] = '\0';
7883 unixtodos_filename (init_dir);
7885 if (STRINGP (default_filename))
7887 char *file_name_only;
7888 char *full_path_name = SDATA (ENCODE_FILE (default_filename));
7890 unixtodos_filename (full_path_name);
7892 file_name_only = strrchr (full_path_name, '\\');
7893 if (!file_name_only)
7894 file_name_only = full_path_name;
7895 else
7896 file_name_only++;
7898 strncpy (filename, file_name_only, MAX_PATH);
7899 filename[MAX_PATH] = '\0';
7901 else
7902 filename[0] = '\0';
7905 NEWOPENFILENAME new_file_details;
7906 BOOL file_opened = FALSE;
7907 OPENFILENAME * file_details = &new_file_details.real_details;
7909 /* Prevent redisplay. */
7910 specbind (Qinhibit_redisplay, Qt);
7911 BLOCK_INPUT;
7913 bzero (&new_file_details, sizeof (new_file_details));
7914 /* Apparently NT4 crashes if you give it an unexpected size.
7915 I'm not sure about Windows 9x, so play it safe. */
7916 if (w32_major_version > 4 && w32_major_version < 95)
7917 file_details->lStructSize = sizeof (NEWOPENFILENAME);
7918 else
7919 file_details->lStructSize = sizeof (OPENFILENAME);
7921 file_details->hwndOwner = FRAME_W32_WINDOW (f);
7922 /* Undocumented Bug in Common File Dialog:
7923 If a filter is not specified, shell links are not resolved. */
7924 file_details->lpstrFilter = "All Files (*.*)\0*.*\0Directories\0*|*\0\0";
7925 file_details->lpstrFile = filename;
7926 file_details->nMaxFile = sizeof (filename);
7927 file_details->lpstrInitialDir = init_dir;
7928 file_details->lpstrTitle = SDATA (prompt);
7930 if (! NILP (only_dir_p))
7931 default_filter_index = 2;
7933 file_details->nFilterIndex = default_filter_index;
7935 file_details->Flags = (OFN_HIDEREADONLY | OFN_NOCHANGEDIR
7936 | OFN_EXPLORER | OFN_ENABLEHOOK);
7937 if (!NILP (mustmatch))
7939 /* Require that the path to the parent directory exists. */
7940 file_details->Flags |= OFN_PATHMUSTEXIST;
7941 /* If we are looking for a file, require that it exists. */
7942 if (NILP (only_dir_p))
7943 file_details->Flags |= OFN_FILEMUSTEXIST;
7946 file_details->lpfnHook = (LPOFNHOOKPROC) file_dialog_callback;
7948 file_opened = GetOpenFileName (file_details);
7950 UNBLOCK_INPUT;
7952 if (file_opened)
7954 dostounix_filename (filename);
7956 if (file_details->nFilterIndex == 2)
7958 /* "Directories" selected - strip dummy file name. */
7959 char * last = strrchr (filename, '/');
7960 *last = '\0';
7963 file = DECODE_FILE(build_string (filename));
7965 /* User cancelled the dialog without making a selection. */
7966 else if (!CommDlgExtendedError ())
7967 file = Qnil;
7968 /* An error occurred, fallback on reading from the mini-buffer. */
7969 else
7970 file = Fcompleting_read (prompt, intern ("read-file-name-internal"),
7971 dir, mustmatch, dir, Qfile_name_history,
7972 default_filename, Qnil);
7974 file = unbind_to (count, file);
7977 UNGCPRO;
7979 /* Make "Cancel" equivalent to C-g. */
7980 if (NILP (file))
7981 Fsignal (Qquit, Qnil);
7983 return unbind_to (count, file);
7988 /***********************************************************************
7989 w32 specialized functions
7990 ***********************************************************************/
7992 DEFUN ("w32-select-font", Fw32_select_font, Sw32_select_font, 0, 2, 0,
7993 doc: /* Select a font for the named FRAME using the W32 font dialog.
7994 Returns an X-style font string corresponding to the selection.
7996 If FRAME is omitted or nil, it defaults to the selected frame.
7997 If INCLUDE-PROPORTIONAL is non-nil, include proportional fonts
7998 in the font selection dialog. */)
7999 (frame, include_proportional)
8000 Lisp_Object frame, include_proportional;
8002 FRAME_PTR f = check_x_frame (frame);
8003 CHOOSEFONT cf;
8004 LOGFONT lf;
8005 TEXTMETRIC tm;
8006 HDC hdc;
8007 HANDLE oldobj;
8008 char buf[100];
8010 bzero (&cf, sizeof (cf));
8011 bzero (&lf, sizeof (lf));
8013 cf.lStructSize = sizeof (cf);
8014 cf.hwndOwner = FRAME_W32_WINDOW (f);
8015 cf.Flags = CF_FORCEFONTEXIST | CF_SCREENFONTS | CF_NOVERTFONTS;
8017 /* Unless include_proportional is non-nil, limit the selection to
8018 monospaced fonts. */
8019 if (NILP (include_proportional))
8020 cf.Flags |= CF_FIXEDPITCHONLY;
8022 cf.lpLogFont = &lf;
8024 /* Initialize as much of the font details as we can from the current
8025 default font. */
8026 hdc = GetDC (FRAME_W32_WINDOW (f));
8027 oldobj = SelectObject (hdc, FRAME_FONT (f)->hfont);
8028 GetTextFace (hdc, LF_FACESIZE, lf.lfFaceName);
8029 if (GetTextMetrics (hdc, &tm))
8031 lf.lfHeight = tm.tmInternalLeading - tm.tmHeight;
8032 lf.lfWeight = tm.tmWeight;
8033 lf.lfItalic = tm.tmItalic;
8034 lf.lfUnderline = tm.tmUnderlined;
8035 lf.lfStrikeOut = tm.tmStruckOut;
8036 lf.lfCharSet = tm.tmCharSet;
8037 cf.Flags |= CF_INITTOLOGFONTSTRUCT;
8039 SelectObject (hdc, oldobj);
8040 ReleaseDC (FRAME_W32_WINDOW (f), hdc);
8042 if (!ChooseFont (&cf) || !w32_to_x_font (&lf, buf, 100, NULL))
8043 return Qnil;
8045 return build_string (buf);
8048 DEFUN ("w32-send-sys-command", Fw32_send_sys_command,
8049 Sw32_send_sys_command, 1, 2, 0,
8050 doc: /* Send frame a Windows WM_SYSCOMMAND message of type COMMAND.
8051 Some useful values for COMMAND are #xf030 to maximize frame (#xf020
8052 to minimize), #xf120 to restore frame to original size, and #xf100
8053 to activate the menubar for keyboard access. #xf140 activates the
8054 screen saver if defined.
8056 If optional parameter FRAME is not specified, use selected frame. */)
8057 (command, frame)
8058 Lisp_Object command, frame;
8060 FRAME_PTR f = check_x_frame (frame);
8062 CHECK_NUMBER (command);
8064 PostMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, XINT (command), 0);
8066 return Qnil;
8069 DEFUN ("w32-shell-execute", Fw32_shell_execute, Sw32_shell_execute, 2, 4, 0,
8070 doc: /* Get Windows to perform OPERATION on DOCUMENT.
8071 This is a wrapper around the ShellExecute system function, which
8072 invokes the application registered to handle OPERATION for DOCUMENT.
8074 OPERATION is either nil or a string that names a supported operation.
8075 What operations can be used depends on the particular DOCUMENT and its
8076 handler application, but typically it is one of the following common
8077 operations:
8079 \"open\" - open DOCUMENT, which could be a file, a directory, or an
8080 executable program. If it is an application, that
8081 application is launched in the current buffer's default
8082 directory. Otherwise, the application associated with
8083 DOCUMENT is launched in the buffer's default directory.
8084 \"print\" - print DOCUMENT, which must be a file
8085 \"explore\" - start the Windows Explorer on DOCUMENT
8086 \"edit\" - launch an editor and open DOCUMENT for editing; which
8087 editor is launched depends on the association for the
8088 specified DOCUMENT
8089 \"find\" - initiate search starting from DOCUMENT which must specify
8090 a directory
8091 nil - invoke the default OPERATION, or \"open\" if default is
8092 not defined or unavailable
8094 DOCUMENT is typically the name of a document file or a URL, but can
8095 also be a program executable to run, or a directory to open in the
8096 Windows Explorer.
8098 If DOCUMENT is a program executable, the optional arg PARAMETERS can
8099 be a string containing command line parameters that will be passed to
8100 the program; otherwise, PARAMETERS should be nil or unspecified.
8102 Second optional argument SHOW-FLAG can be used to control how the
8103 application will be displayed when it is invoked. If SHOW-FLAG is nil
8104 or unspceified, the application is displayed normally, otherwise it is
8105 an integer representing a ShowWindow flag:
8107 0 - start hidden
8108 1 - start normally
8109 3 - start maximized
8110 6 - start minimized */)
8111 (operation, document, parameters, show_flag)
8112 Lisp_Object operation, document, parameters, show_flag;
8114 Lisp_Object current_dir;
8116 CHECK_STRING (document);
8118 /* Encode filename and current directory. */
8119 current_dir = ENCODE_FILE (current_buffer->directory);
8120 document = ENCODE_FILE (document);
8121 if ((int) ShellExecute (NULL,
8122 (STRINGP (operation) ?
8123 SDATA (operation) : NULL),
8124 SDATA (document),
8125 (STRINGP (parameters) ?
8126 SDATA (parameters) : NULL),
8127 SDATA (current_dir),
8128 (INTEGERP (show_flag) ?
8129 XINT (show_flag) : SW_SHOWDEFAULT))
8130 > 32)
8131 return Qt;
8132 error ("ShellExecute failed: %s", w32_strerror (0));
8135 /* Lookup virtual keycode from string representing the name of a
8136 non-ascii keystroke into the corresponding virtual key, using
8137 lispy_function_keys. */
8138 static int
8139 lookup_vk_code (char *key)
8141 int i;
8143 for (i = 0; i < 256; i++)
8144 if (lispy_function_keys[i] != 0
8145 && strcmp (lispy_function_keys[i], key) == 0)
8146 return i;
8148 return -1;
8151 /* Convert a one-element vector style key sequence to a hot key
8152 definition. */
8153 static Lisp_Object
8154 w32_parse_hot_key (key)
8155 Lisp_Object key;
8157 /* Copied from Fdefine_key and store_in_keymap. */
8158 register Lisp_Object c;
8159 int vk_code;
8160 int lisp_modifiers;
8161 int w32_modifiers;
8162 struct gcpro gcpro1;
8164 CHECK_VECTOR (key);
8166 if (XFASTINT (Flength (key)) != 1)
8167 return Qnil;
8169 GCPRO1 (key);
8171 c = Faref (key, make_number (0));
8173 if (CONSP (c) && lucid_event_type_list_p (c))
8174 c = Fevent_convert_list (c);
8176 UNGCPRO;
8178 if (! INTEGERP (c) && ! SYMBOLP (c))
8179 error ("Key definition is invalid");
8181 /* Work out the base key and the modifiers. */
8182 if (SYMBOLP (c))
8184 c = parse_modifiers (c);
8185 lisp_modifiers = XINT (Fcar (Fcdr (c)));
8186 c = Fcar (c);
8187 if (!SYMBOLP (c))
8188 abort ();
8189 vk_code = lookup_vk_code (SDATA (SYMBOL_NAME (c)));
8191 else if (INTEGERP (c))
8193 lisp_modifiers = XINT (c) & ~CHARACTERBITS;
8194 /* Many ascii characters are their own virtual key code. */
8195 vk_code = XINT (c) & CHARACTERBITS;
8198 if (vk_code < 0 || vk_code > 255)
8199 return Qnil;
8201 if ((lisp_modifiers & meta_modifier) != 0
8202 && !NILP (Vw32_alt_is_meta))
8203 lisp_modifiers |= alt_modifier;
8205 /* Supply defs missing from mingw32. */
8206 #ifndef MOD_ALT
8207 #define MOD_ALT 0x0001
8208 #define MOD_CONTROL 0x0002
8209 #define MOD_SHIFT 0x0004
8210 #define MOD_WIN 0x0008
8211 #endif
8213 /* Convert lisp modifiers to Windows hot-key form. */
8214 w32_modifiers = (lisp_modifiers & hyper_modifier) ? MOD_WIN : 0;
8215 w32_modifiers |= (lisp_modifiers & alt_modifier) ? MOD_ALT : 0;
8216 w32_modifiers |= (lisp_modifiers & ctrl_modifier) ? MOD_CONTROL : 0;
8217 w32_modifiers |= (lisp_modifiers & shift_modifier) ? MOD_SHIFT : 0;
8219 return HOTKEY (vk_code, w32_modifiers);
8222 DEFUN ("w32-register-hot-key", Fw32_register_hot_key,
8223 Sw32_register_hot_key, 1, 1, 0,
8224 doc: /* Register KEY as a hot-key combination.
8225 Certain key combinations like Alt-Tab are reserved for system use on
8226 Windows, and therefore are normally intercepted by the system. However,
8227 most of these key combinations can be received by registering them as
8228 hot-keys, overriding their special meaning.
8230 KEY must be a one element key definition in vector form that would be
8231 acceptable to `define-key' (e.g. [A-tab] for Alt-Tab). The meta
8232 modifier is interpreted as Alt if `w32-alt-is-meta' is t, and hyper
8233 is always interpreted as the Windows modifier keys.
8235 The return value is the hotkey-id if registered, otherwise nil. */)
8236 (key)
8237 Lisp_Object key;
8239 key = w32_parse_hot_key (key);
8241 if (NILP (Fmemq (key, w32_grabbed_keys)))
8243 /* Reuse an empty slot if possible. */
8244 Lisp_Object item = Fmemq (Qnil, w32_grabbed_keys);
8246 /* Safe to add new key to list, even if we have focus. */
8247 if (NILP (item))
8248 w32_grabbed_keys = Fcons (key, w32_grabbed_keys);
8249 else
8250 XSETCAR (item, key);
8252 /* Notify input thread about new hot-key definition, so that it
8253 takes effect without needing to switch focus. */
8254 #ifdef USE_LISP_UNION_TYPE
8255 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
8256 (WPARAM) key.i, 0);
8257 #else
8258 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
8259 (WPARAM) key, 0);
8260 #endif
8263 return key;
8266 DEFUN ("w32-unregister-hot-key", Fw32_unregister_hot_key,
8267 Sw32_unregister_hot_key, 1, 1, 0,
8268 doc: /* Unregister KEY as a hot-key combination. */)
8269 (key)
8270 Lisp_Object key;
8272 Lisp_Object item;
8274 if (!INTEGERP (key))
8275 key = w32_parse_hot_key (key);
8277 item = Fmemq (key, w32_grabbed_keys);
8279 if (!NILP (item))
8281 /* Notify input thread about hot-key definition being removed, so
8282 that it takes effect without needing focus switch. */
8283 #ifdef USE_LISP_UNION_TYPE
8284 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
8285 (WPARAM) XINT (XCAR (item)), (LPARAM) item.i))
8286 #else
8287 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
8288 (WPARAM) XINT (XCAR (item)), (LPARAM) item))
8290 #endif
8292 MSG msg;
8293 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
8295 return Qt;
8297 return Qnil;
8300 DEFUN ("w32-registered-hot-keys", Fw32_registered_hot_keys,
8301 Sw32_registered_hot_keys, 0, 0, 0,
8302 doc: /* Return list of registered hot-key IDs. */)
8305 return Fcopy_sequence (w32_grabbed_keys);
8308 DEFUN ("w32-reconstruct-hot-key", Fw32_reconstruct_hot_key,
8309 Sw32_reconstruct_hot_key, 1, 1, 0,
8310 doc: /* Convert hot-key ID to a lisp key combination.
8311 usage: (w32-reconstruct-hot-key ID) */)
8312 (hotkeyid)
8313 Lisp_Object hotkeyid;
8315 int vk_code, w32_modifiers;
8316 Lisp_Object key;
8318 CHECK_NUMBER (hotkeyid);
8320 vk_code = HOTKEY_VK_CODE (hotkeyid);
8321 w32_modifiers = HOTKEY_MODIFIERS (hotkeyid);
8323 if (lispy_function_keys[vk_code])
8324 key = intern (lispy_function_keys[vk_code]);
8325 else
8326 key = make_number (vk_code);
8328 key = Fcons (key, Qnil);
8329 if (w32_modifiers & MOD_SHIFT)
8330 key = Fcons (Qshift, key);
8331 if (w32_modifiers & MOD_CONTROL)
8332 key = Fcons (Qctrl, key);
8333 if (w32_modifiers & MOD_ALT)
8334 key = Fcons (NILP (Vw32_alt_is_meta) ? Qalt : Qmeta, key);
8335 if (w32_modifiers & MOD_WIN)
8336 key = Fcons (Qhyper, key);
8338 return key;
8341 DEFUN ("w32-toggle-lock-key", Fw32_toggle_lock_key,
8342 Sw32_toggle_lock_key, 1, 2, 0,
8343 doc: /* Toggle the state of the lock key KEY.
8344 KEY can be `capslock', `kp-numlock', or `scroll'.
8345 If the optional parameter NEW-STATE is a number, then the state of KEY
8346 is set to off if the low bit of NEW-STATE is zero, otherwise on. */)
8347 (key, new_state)
8348 Lisp_Object key, new_state;
8350 int vk_code;
8352 if (EQ (key, intern ("capslock")))
8353 vk_code = VK_CAPITAL;
8354 else if (EQ (key, intern ("kp-numlock")))
8355 vk_code = VK_NUMLOCK;
8356 else if (EQ (key, intern ("scroll")))
8357 vk_code = VK_SCROLL;
8358 else
8359 return Qnil;
8361 if (!dwWindowsThreadId)
8362 return make_number (w32_console_toggle_lock_key (vk_code, new_state));
8364 #ifdef USE_LISP_UNION_TYPE
8365 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
8366 (WPARAM) vk_code, (LPARAM) new_state.i))
8367 #else
8368 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
8369 (WPARAM) vk_code, (LPARAM) new_state))
8370 #endif
8372 MSG msg;
8373 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
8374 return make_number (msg.wParam);
8376 return Qnil;
8379 DEFUN ("w32-window-exists-p", Fw32_window_exists_p, Sw32_window_exists_p,
8380 2, 2, 0,
8381 doc: /* Return non-nil if a window exists with the specified CLASS and NAME.
8383 This is a direct interface to the Windows API FindWindow function. */)
8384 (class, name)
8385 Lisp_Object class, name;
8387 HWND hnd;
8389 if (!NILP (class))
8390 CHECK_STRING (class);
8391 if (!NILP (name))
8392 CHECK_STRING (name);
8394 hnd = FindWindow (STRINGP (class) ? ((LPCTSTR) SDATA (class)) : NULL,
8395 STRINGP (name) ? ((LPCTSTR) SDATA (name)) : NULL);
8396 if (!hnd)
8397 return Qnil;
8398 return Qt;
8403 DEFUN ("file-system-info", Ffile_system_info, Sfile_system_info, 1, 1, 0,
8404 doc: /* Return storage information about the file system FILENAME is on.
8405 Value is a list of floats (TOTAL FREE AVAIL), where TOTAL is the total
8406 storage of the file system, FREE is the free storage, and AVAIL is the
8407 storage available to a non-superuser. All 3 numbers are in bytes.
8408 If the underlying system call fails, value is nil. */)
8409 (filename)
8410 Lisp_Object filename;
8412 Lisp_Object encoded, value;
8414 CHECK_STRING (filename);
8415 filename = Fexpand_file_name (filename, Qnil);
8416 encoded = ENCODE_FILE (filename);
8418 value = Qnil;
8420 /* Determining the required information on Windows turns out, sadly,
8421 to be more involved than one would hope. The original Win32 api
8422 call for this will return bogus information on some systems, but we
8423 must dynamically probe for the replacement api, since that was
8424 added rather late on. */
8426 HMODULE hKernel = GetModuleHandle ("kernel32");
8427 BOOL (*pfn_GetDiskFreeSpaceEx)
8428 (char *, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER)
8429 = (void *) GetProcAddress (hKernel, "GetDiskFreeSpaceEx");
8431 /* On Windows, we may need to specify the root directory of the
8432 volume holding FILENAME. */
8433 char rootname[MAX_PATH];
8434 char *name = SDATA (encoded);
8436 /* find the root name of the volume if given */
8437 if (isalpha (name[0]) && name[1] == ':')
8439 rootname[0] = name[0];
8440 rootname[1] = name[1];
8441 rootname[2] = '\\';
8442 rootname[3] = 0;
8444 else if (IS_DIRECTORY_SEP (name[0]) && IS_DIRECTORY_SEP (name[1]))
8446 char *str = rootname;
8447 int slashes = 4;
8450 if (IS_DIRECTORY_SEP (*name) && --slashes == 0)
8451 break;
8452 *str++ = *name++;
8454 while ( *name );
8456 *str++ = '\\';
8457 *str = 0;
8460 if (pfn_GetDiskFreeSpaceEx)
8462 /* Unsigned large integers cannot be cast to double, so
8463 use signed ones instead. */
8464 LARGE_INTEGER availbytes;
8465 LARGE_INTEGER freebytes;
8466 LARGE_INTEGER totalbytes;
8468 if (pfn_GetDiskFreeSpaceEx(rootname,
8469 (ULARGE_INTEGER *)&availbytes,
8470 (ULARGE_INTEGER *)&totalbytes,
8471 (ULARGE_INTEGER *)&freebytes))
8472 value = list3 (make_float ((double) totalbytes.QuadPart),
8473 make_float ((double) freebytes.QuadPart),
8474 make_float ((double) availbytes.QuadPart));
8476 else
8478 DWORD sectors_per_cluster;
8479 DWORD bytes_per_sector;
8480 DWORD free_clusters;
8481 DWORD total_clusters;
8483 if (GetDiskFreeSpace(rootname,
8484 &sectors_per_cluster,
8485 &bytes_per_sector,
8486 &free_clusters,
8487 &total_clusters))
8488 value = list3 (make_float ((double) total_clusters
8489 * sectors_per_cluster * bytes_per_sector),
8490 make_float ((double) free_clusters
8491 * sectors_per_cluster * bytes_per_sector),
8492 make_float ((double) free_clusters
8493 * sectors_per_cluster * bytes_per_sector));
8497 return value;
8500 DEFUN ("default-printer-name", Fdefault_printer_name, Sdefault_printer_name,
8501 0, 0, 0, doc: /* Return the name of Windows default printer device. */)
8504 static char pname_buf[256];
8505 int err;
8506 HANDLE hPrn;
8507 PRINTER_INFO_2 *ppi2 = NULL;
8508 DWORD dwNeeded = 0, dwReturned = 0;
8510 /* Retrieve the default string from Win.ini (the registry).
8511 * String will be in form "printername,drivername,portname".
8512 * This is the most portable way to get the default printer. */
8513 if (GetProfileString ("windows", "device", ",,", pname_buf, sizeof (pname_buf)) <= 0)
8514 return Qnil;
8515 /* printername precedes first "," character */
8516 strtok (pname_buf, ",");
8517 /* We want to know more than the printer name */
8518 if (!OpenPrinter (pname_buf, &hPrn, NULL))
8519 return Qnil;
8520 GetPrinter (hPrn, 2, NULL, 0, &dwNeeded);
8521 if (dwNeeded == 0)
8523 ClosePrinter (hPrn);
8524 return Qnil;
8526 /* Allocate memory for the PRINTER_INFO_2 struct */
8527 ppi2 = (PRINTER_INFO_2 *) xmalloc (dwNeeded);
8528 if (!ppi2)
8530 ClosePrinter (hPrn);
8531 return Qnil;
8533 /* Call GetPrinter() again with big enouth memory block */
8534 err = GetPrinter (hPrn, 2, (LPBYTE)ppi2, dwNeeded, &dwReturned);
8535 ClosePrinter (hPrn);
8536 if (!err)
8538 xfree(ppi2);
8539 return Qnil;
8542 if (ppi2)
8544 if (ppi2->Attributes & PRINTER_ATTRIBUTE_SHARED && ppi2->pServerName)
8546 /* a remote printer */
8547 if (*ppi2->pServerName == '\\')
8548 _snprintf(pname_buf, sizeof (pname_buf), "%s\\%s", ppi2->pServerName,
8549 ppi2->pShareName);
8550 else
8551 _snprintf(pname_buf, sizeof (pname_buf), "\\\\%s\\%s", ppi2->pServerName,
8552 ppi2->pShareName);
8553 pname_buf[sizeof (pname_buf) - 1] = '\0';
8555 else
8557 /* a local printer */
8558 strncpy(pname_buf, ppi2->pPortName, sizeof (pname_buf));
8559 pname_buf[sizeof (pname_buf) - 1] = '\0';
8560 /* `pPortName' can include several ports, delimited by ','.
8561 * we only use the first one. */
8562 strtok(pname_buf, ",");
8564 xfree(ppi2);
8567 return build_string (pname_buf);
8570 /***********************************************************************
8571 Initialization
8572 ***********************************************************************/
8574 /* Keep this list in the same order as frame_parms in frame.c.
8575 Use 0 for unsupported frame parameters. */
8577 frame_parm_handler w32_frame_parm_handlers[] =
8579 x_set_autoraise,
8580 x_set_autolower,
8581 x_set_background_color,
8582 x_set_border_color,
8583 x_set_border_width,
8584 x_set_cursor_color,
8585 x_set_cursor_type,
8586 x_set_font,
8587 x_set_foreground_color,
8588 x_set_icon_name,
8589 x_set_icon_type,
8590 x_set_internal_border_width,
8591 x_set_menu_bar_lines,
8592 x_set_mouse_color,
8593 x_explicitly_set_name,
8594 x_set_scroll_bar_width,
8595 x_set_title,
8596 x_set_unsplittable,
8597 x_set_vertical_scroll_bars,
8598 x_set_visibility,
8599 x_set_tool_bar_lines,
8600 0, /* x_set_scroll_bar_foreground, */
8601 0, /* x_set_scroll_bar_background, */
8602 x_set_screen_gamma,
8603 x_set_line_spacing,
8604 x_set_fringe_width,
8605 x_set_fringe_width,
8606 0, /* x_set_wait_for_wm, */
8607 x_set_fullscreen,
8610 void
8611 syms_of_w32fns ()
8613 globals_of_w32fns ();
8614 /* This is zero if not using MS-Windows. */
8615 w32_in_use = 0;
8616 track_mouse_window = NULL;
8618 w32_visible_system_caret_hwnd = NULL;
8620 Qnone = intern ("none");
8621 staticpro (&Qnone);
8622 Qsuppress_icon = intern ("suppress-icon");
8623 staticpro (&Qsuppress_icon);
8624 Qundefined_color = intern ("undefined-color");
8625 staticpro (&Qundefined_color);
8626 Qcancel_timer = intern ("cancel-timer");
8627 staticpro (&Qcancel_timer);
8629 Qhyper = intern ("hyper");
8630 staticpro (&Qhyper);
8631 Qsuper = intern ("super");
8632 staticpro (&Qsuper);
8633 Qmeta = intern ("meta");
8634 staticpro (&Qmeta);
8635 Qalt = intern ("alt");
8636 staticpro (&Qalt);
8637 Qctrl = intern ("ctrl");
8638 staticpro (&Qctrl);
8639 Qcontrol = intern ("control");
8640 staticpro (&Qcontrol);
8641 Qshift = intern ("shift");
8642 staticpro (&Qshift);
8643 /* This is the end of symbol initialization. */
8645 /* Text property `display' should be nonsticky by default. */
8646 Vtext_property_default_nonsticky
8647 = Fcons (Fcons (Qdisplay, Qt), Vtext_property_default_nonsticky);
8650 Fput (Qundefined_color, Qerror_conditions,
8651 Fcons (Qundefined_color, Fcons (Qerror, Qnil)));
8652 Fput (Qundefined_color, Qerror_message,
8653 build_string ("Undefined color"));
8655 staticpro (&w32_grabbed_keys);
8656 w32_grabbed_keys = Qnil;
8658 DEFVAR_LISP ("w32-color-map", &Vw32_color_map,
8659 doc: /* An array of color name mappings for Windows. */);
8660 Vw32_color_map = Qnil;
8662 DEFVAR_LISP ("w32-pass-alt-to-system", &Vw32_pass_alt_to_system,
8663 doc: /* Non-nil if Alt key presses are passed on to Windows.
8664 When non-nil, for example, Alt pressed and released and then space will
8665 open the System menu. When nil, Emacs processes the Alt key events, and
8666 then silently swallows them. */);
8667 Vw32_pass_alt_to_system = Qnil;
8669 DEFVAR_LISP ("w32-alt-is-meta", &Vw32_alt_is_meta,
8670 doc: /* Non-nil if the Alt key is to be considered the same as the META key.
8671 When nil, Emacs will translate the Alt key to the ALT modifier, not to META. */);
8672 Vw32_alt_is_meta = Qt;
8674 DEFVAR_INT ("w32-quit-key", &w32_quit_key,
8675 doc: /* If non-zero, the virtual key code for an alternative quit key. */);
8676 w32_quit_key = 0;
8678 DEFVAR_LISP ("w32-pass-lwindow-to-system",
8679 &Vw32_pass_lwindow_to_system,
8680 doc: /* If non-nil, the left \"Windows\" key is passed on to Windows.
8682 When non-nil, the Start menu is opened by tapping the key.
8683 If you set this to nil, the left \"Windows\" key is processed by Emacs
8684 according to the value of `w32-lwindow-modifier', which see.
8686 Note that some combinations of the left \"Windows\" key with other keys are
8687 caught by Windows at low level, and so binding them in Emacs will have no
8688 effect. For example, <lwindow>-r always pops up the Windows Run dialog,
8689 <lwindow>-<Pause> pops up the "System Properties" dialog, etc. However, see
8690 the doc string of `w32-phantom-key-code'. */);
8691 Vw32_pass_lwindow_to_system = Qt;
8693 DEFVAR_LISP ("w32-pass-rwindow-to-system",
8694 &Vw32_pass_rwindow_to_system,
8695 doc: /* If non-nil, the right \"Windows\" key is passed on to Windows.
8697 When non-nil, the Start menu is opened by tapping the key.
8698 If you set this to nil, the right \"Windows\" key is processed by Emacs
8699 according to the value of `w32-rwindow-modifier', which see.
8701 Note that some combinations of the right \"Windows\" key with other keys are
8702 caught by Windows at low level, and so binding them in Emacs will have no
8703 effect. For example, <rwindow>-r always pops up the Windows Run dialog,
8704 <rwindow>-<Pause> pops up the "System Properties" dialog, etc. However, see
8705 the doc string of `w32-phantom-key-code'. */);
8706 Vw32_pass_rwindow_to_system = Qt;
8708 DEFVAR_LISP ("w32-phantom-key-code",
8709 &Vw32_phantom_key_code,
8710 doc: /* Virtual key code used to generate \"phantom\" key presses.
8711 Value is a number between 0 and 255.
8713 Phantom key presses are generated in order to stop the system from
8714 acting on \"Windows\" key events when `w32-pass-lwindow-to-system' or
8715 `w32-pass-rwindow-to-system' is nil. */);
8716 /* Although 255 is technically not a valid key code, it works and
8717 means that this hack won't interfere with any real key code. */
8718 XSETINT (Vw32_phantom_key_code, 255);
8720 DEFVAR_LISP ("w32-enable-num-lock",
8721 &Vw32_enable_num_lock,
8722 doc: /* If non-nil, the Num Lock key acts normally.
8723 Set to nil to handle Num Lock as the `kp-numlock' key. */);
8724 Vw32_enable_num_lock = Qt;
8726 DEFVAR_LISP ("w32-enable-caps-lock",
8727 &Vw32_enable_caps_lock,
8728 doc: /* If non-nil, the Caps Lock key acts normally.
8729 Set to nil to handle Caps Lock as the `capslock' key. */);
8730 Vw32_enable_caps_lock = Qt;
8732 DEFVAR_LISP ("w32-scroll-lock-modifier",
8733 &Vw32_scroll_lock_modifier,
8734 doc: /* Modifier to use for the Scroll Lock ON state.
8735 The value can be hyper, super, meta, alt, control or shift for the
8736 respective modifier, or nil to handle Scroll Lock as the `scroll' key.
8737 Any other value will cause the Scroll Lock key to be ignored. */);
8738 Vw32_scroll_lock_modifier = Qt;
8740 DEFVAR_LISP ("w32-lwindow-modifier",
8741 &Vw32_lwindow_modifier,
8742 doc: /* Modifier to use for the left \"Windows\" key.
8743 The value can be hyper, super, meta, alt, control or shift for the
8744 respective modifier, or nil to appear as the `lwindow' key.
8745 Any other value will cause the key to be ignored. */);
8746 Vw32_lwindow_modifier = Qnil;
8748 DEFVAR_LISP ("w32-rwindow-modifier",
8749 &Vw32_rwindow_modifier,
8750 doc: /* Modifier to use for the right \"Windows\" key.
8751 The value can be hyper, super, meta, alt, control or shift for the
8752 respective modifier, or nil to appear as the `rwindow' key.
8753 Any other value will cause the key to be ignored. */);
8754 Vw32_rwindow_modifier = Qnil;
8756 DEFVAR_LISP ("w32-apps-modifier",
8757 &Vw32_apps_modifier,
8758 doc: /* Modifier to use for the \"Apps\" key.
8759 The value can be hyper, super, meta, alt, control or shift for the
8760 respective modifier, or nil to appear as the `apps' key.
8761 Any other value will cause the key to be ignored. */);
8762 Vw32_apps_modifier = Qnil;
8764 DEFVAR_BOOL ("w32-enable-synthesized-fonts", &w32_enable_synthesized_fonts,
8765 doc: /* Non-nil enables selection of artificially italicized and bold fonts. */);
8766 w32_enable_synthesized_fonts = 0;
8768 DEFVAR_LISP ("w32-enable-palette", &Vw32_enable_palette,
8769 doc: /* Non-nil enables Windows palette management to map colors exactly. */);
8770 Vw32_enable_palette = Qt;
8772 DEFVAR_INT ("w32-mouse-button-tolerance",
8773 &w32_mouse_button_tolerance,
8774 doc: /* Analogue of double click interval for faking middle mouse events.
8775 The value is the minimum time in milliseconds that must elapse between
8776 left and right button down events before they are considered distinct events.
8777 If both mouse buttons are depressed within this interval, a middle mouse
8778 button down event is generated instead. */);
8779 w32_mouse_button_tolerance = GetDoubleClickTime () / 2;
8781 DEFVAR_INT ("w32-mouse-move-interval",
8782 &w32_mouse_move_interval,
8783 doc: /* Minimum interval between mouse move events.
8784 The value is the minimum time in milliseconds that must elapse between
8785 successive mouse move (or scroll bar drag) events before they are
8786 reported as lisp events. */);
8787 w32_mouse_move_interval = 0;
8789 DEFVAR_BOOL ("w32-pass-extra-mouse-buttons-to-system",
8790 &w32_pass_extra_mouse_buttons_to_system,
8791 doc: /* If non-nil, the fourth and fifth mouse buttons are passed to Windows.
8792 Recent versions of Windows support mice with up to five buttons.
8793 Since most applications don't support these extra buttons, most mouse
8794 drivers will allow you to map them to functions at the system level.
8795 If this variable is non-nil, Emacs will pass them on, allowing the
8796 system to handle them. */);
8797 w32_pass_extra_mouse_buttons_to_system = 0;
8799 DEFVAR_LISP ("x-pointer-shape", &Vx_pointer_shape,
8800 doc: /* The shape of the pointer when over text.
8801 Changing the value does not affect existing frames
8802 unless you set the mouse color. */);
8803 Vx_pointer_shape = Qnil;
8805 Vx_nontext_pointer_shape = Qnil;
8807 Vx_mode_pointer_shape = Qnil;
8809 DEFVAR_LISP ("x-hourglass-pointer-shape", &Vx_hourglass_pointer_shape,
8810 doc: /* The shape of the pointer when Emacs is busy.
8811 This variable takes effect when you create a new frame
8812 or when you set the mouse color. */);
8813 Vx_hourglass_pointer_shape = Qnil;
8815 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
8816 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
8817 display_hourglass_p = 1;
8819 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
8820 doc: /* *Seconds to wait before displaying an hourglass pointer.
8821 Value must be an integer or float. */);
8822 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
8824 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
8825 &Vx_sensitive_text_pointer_shape,
8826 doc: /* The shape of the pointer when over mouse-sensitive text.
8827 This variable takes effect when you create a new frame
8828 or when you set the mouse color. */);
8829 Vx_sensitive_text_pointer_shape = Qnil;
8831 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
8832 &Vx_window_horizontal_drag_shape,
8833 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
8834 This variable takes effect when you create a new frame
8835 or when you set the mouse color. */);
8836 Vx_window_horizontal_drag_shape = Qnil;
8838 DEFVAR_LISP ("x-cursor-fore-pixel", &Vx_cursor_fore_pixel,
8839 doc: /* A string indicating the foreground color of the cursor box. */);
8840 Vx_cursor_fore_pixel = Qnil;
8842 DEFVAR_LISP ("x-max-tooltip-size", &Vx_max_tooltip_size,
8843 doc: /* Maximum size for tooltips.
8844 Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
8845 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
8847 DEFVAR_LISP ("x-no-window-manager", &Vx_no_window_manager,
8848 doc: /* Non-nil if no window manager is in use.
8849 Emacs doesn't try to figure this out; this is always nil
8850 unless you set it to something else. */);
8851 /* We don't have any way to find this out, so set it to nil
8852 and maybe the user would like to set it to t. */
8853 Vx_no_window_manager = Qnil;
8855 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
8856 &Vx_pixel_size_width_font_regexp,
8857 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
8859 Since Emacs gets width of a font matching with this regexp from
8860 PIXEL_SIZE field of the name, font finding mechanism gets faster for
8861 such a font. This is especially effective for such large fonts as
8862 Chinese, Japanese, and Korean. */);
8863 Vx_pixel_size_width_font_regexp = Qnil;
8865 DEFVAR_LISP ("w32-bdf-filename-alist",
8866 &Vw32_bdf_filename_alist,
8867 doc: /* List of bdf fonts and their corresponding filenames. */);
8868 Vw32_bdf_filename_alist = Qnil;
8870 DEFVAR_BOOL ("w32-strict-fontnames",
8871 &w32_strict_fontnames,
8872 doc: /* Non-nil means only use fonts that are exact matches for those requested.
8873 Default is nil, which allows old fontnames that are not XLFD compliant,
8874 and allows third-party CJK display to work by specifying false charset
8875 fields to trick Emacs into translating to Big5, SJIS etc.
8876 Setting this to t will prevent wrong fonts being selected when
8877 fontsets are automatically created. */);
8878 w32_strict_fontnames = 0;
8880 DEFVAR_BOOL ("w32-strict-painting",
8881 &w32_strict_painting,
8882 doc: /* Non-nil means use strict rules for repainting frames.
8883 Set this to nil to get the old behavior for repainting; this should
8884 only be necessary if the default setting causes problems. */);
8885 w32_strict_painting = 1;
8887 DEFVAR_LISP ("w32-charset-info-alist",
8888 &Vw32_charset_info_alist,
8889 doc: /* Alist linking Emacs character sets to Windows fonts and codepages.
8890 Each entry should be of the form:
8892 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE))
8894 where CHARSET_NAME is a string used in font names to identify the charset,
8895 WINDOWS_CHARSET is a symbol that can be one of:
8896 w32-charset-ansi, w32-charset-default, w32-charset-symbol,
8897 w32-charset-shiftjis, w32-charset-hangeul, w32-charset-gb2312,
8898 w32-charset-chinesebig5,
8899 w32-charset-johab, w32-charset-hebrew,
8900 w32-charset-arabic, w32-charset-greek, w32-charset-turkish,
8901 w32-charset-vietnamese, w32-charset-thai, w32-charset-easteurope,
8902 w32-charset-russian, w32-charset-mac, w32-charset-baltic,
8903 w32-charset-unicode,
8904 or w32-charset-oem.
8905 CODEPAGE should be an integer specifying the codepage that should be used
8906 to display the character set, t to do no translation and output as Unicode,
8907 or nil to do no translation and output as 8 bit (or multibyte on far-east
8908 versions of Windows) characters. */);
8909 Vw32_charset_info_alist = Qnil;
8911 staticpro (&Qw32_charset_ansi);
8912 Qw32_charset_ansi = intern ("w32-charset-ansi");
8913 staticpro (&Qw32_charset_symbol);
8914 Qw32_charset_default = intern ("w32-charset-default");
8915 staticpro (&Qw32_charset_default);
8916 Qw32_charset_symbol = intern ("w32-charset-symbol");
8917 staticpro (&Qw32_charset_shiftjis);
8918 Qw32_charset_shiftjis = intern ("w32-charset-shiftjis");
8919 staticpro (&Qw32_charset_hangeul);
8920 Qw32_charset_hangeul = intern ("w32-charset-hangeul");
8921 staticpro (&Qw32_charset_chinesebig5);
8922 Qw32_charset_chinesebig5 = intern ("w32-charset-chinesebig5");
8923 staticpro (&Qw32_charset_gb2312);
8924 Qw32_charset_gb2312 = intern ("w32-charset-gb2312");
8925 staticpro (&Qw32_charset_oem);
8926 Qw32_charset_oem = intern ("w32-charset-oem");
8928 #ifdef JOHAB_CHARSET
8930 static int w32_extra_charsets_defined = 1;
8931 DEFVAR_BOOL ("w32-extra-charsets-defined", &w32_extra_charsets_defined,
8932 doc: /* Internal variable. */);
8934 staticpro (&Qw32_charset_johab);
8935 Qw32_charset_johab = intern ("w32-charset-johab");
8936 staticpro (&Qw32_charset_easteurope);
8937 Qw32_charset_easteurope = intern ("w32-charset-easteurope");
8938 staticpro (&Qw32_charset_turkish);
8939 Qw32_charset_turkish = intern ("w32-charset-turkish");
8940 staticpro (&Qw32_charset_baltic);
8941 Qw32_charset_baltic = intern ("w32-charset-baltic");
8942 staticpro (&Qw32_charset_russian);
8943 Qw32_charset_russian = intern ("w32-charset-russian");
8944 staticpro (&Qw32_charset_arabic);
8945 Qw32_charset_arabic = intern ("w32-charset-arabic");
8946 staticpro (&Qw32_charset_greek);
8947 Qw32_charset_greek = intern ("w32-charset-greek");
8948 staticpro (&Qw32_charset_hebrew);
8949 Qw32_charset_hebrew = intern ("w32-charset-hebrew");
8950 staticpro (&Qw32_charset_vietnamese);
8951 Qw32_charset_vietnamese = intern ("w32-charset-vietnamese");
8952 staticpro (&Qw32_charset_thai);
8953 Qw32_charset_thai = intern ("w32-charset-thai");
8954 staticpro (&Qw32_charset_mac);
8955 Qw32_charset_mac = intern ("w32-charset-mac");
8957 #endif
8959 #ifdef UNICODE_CHARSET
8961 static int w32_unicode_charset_defined = 1;
8962 DEFVAR_BOOL ("w32-unicode-charset-defined",
8963 &w32_unicode_charset_defined,
8964 doc: /* Internal variable. */);
8966 staticpro (&Qw32_charset_unicode);
8967 Qw32_charset_unicode = intern ("w32-charset-unicode");
8969 #endif
8971 #if 0 /* TODO: Port to W32 */
8972 defsubr (&Sx_change_window_property);
8973 defsubr (&Sx_delete_window_property);
8974 defsubr (&Sx_window_property);
8975 #endif
8976 defsubr (&Sxw_display_color_p);
8977 defsubr (&Sx_display_grayscale_p);
8978 defsubr (&Sxw_color_defined_p);
8979 defsubr (&Sxw_color_values);
8980 defsubr (&Sx_server_max_request_size);
8981 defsubr (&Sx_server_vendor);
8982 defsubr (&Sx_server_version);
8983 defsubr (&Sx_display_pixel_width);
8984 defsubr (&Sx_display_pixel_height);
8985 defsubr (&Sx_display_mm_width);
8986 defsubr (&Sx_display_mm_height);
8987 defsubr (&Sx_display_screens);
8988 defsubr (&Sx_display_planes);
8989 defsubr (&Sx_display_color_cells);
8990 defsubr (&Sx_display_visual_class);
8991 defsubr (&Sx_display_backing_store);
8992 defsubr (&Sx_display_save_under);
8993 defsubr (&Sx_create_frame);
8994 defsubr (&Sx_open_connection);
8995 defsubr (&Sx_close_connection);
8996 defsubr (&Sx_display_list);
8997 defsubr (&Sx_synchronize);
8999 /* W32 specific functions */
9001 defsubr (&Sw32_focus_frame);
9002 defsubr (&Sw32_select_font);
9003 defsubr (&Sw32_define_rgb_color);
9004 defsubr (&Sw32_default_color_map);
9005 defsubr (&Sw32_load_color_file);
9006 defsubr (&Sw32_send_sys_command);
9007 defsubr (&Sw32_shell_execute);
9008 defsubr (&Sw32_register_hot_key);
9009 defsubr (&Sw32_unregister_hot_key);
9010 defsubr (&Sw32_registered_hot_keys);
9011 defsubr (&Sw32_reconstruct_hot_key);
9012 defsubr (&Sw32_toggle_lock_key);
9013 defsubr (&Sw32_window_exists_p);
9014 defsubr (&Sw32_find_bdf_fonts);
9016 defsubr (&Sfile_system_info);
9017 defsubr (&Sdefault_printer_name);
9019 /* Setting callback functions for fontset handler. */
9020 get_font_info_func = w32_get_font_info;
9022 #if 0 /* This function pointer doesn't seem to be used anywhere.
9023 And the pointer assigned has the wrong type, anyway. */
9024 list_fonts_func = w32_list_fonts;
9025 #endif
9027 load_font_func = w32_load_font;
9028 find_ccl_program_func = w32_find_ccl_program;
9029 query_font_func = w32_query_font;
9030 set_frame_fontset_func = x_set_font;
9031 check_window_system_func = check_w32;
9034 hourglass_atimer = NULL;
9035 hourglass_shown_p = 0;
9036 defsubr (&Sx_show_tip);
9037 defsubr (&Sx_hide_tip);
9038 tip_timer = Qnil;
9039 staticpro (&tip_timer);
9040 tip_frame = Qnil;
9041 staticpro (&tip_frame);
9043 last_show_tip_args = Qnil;
9044 staticpro (&last_show_tip_args);
9046 defsubr (&Sx_file_dialog);
9051 globals_of_w32fns is used to initialize those global variables that
9052 must always be initialized on startup even when the global variable
9053 initialized is non zero (see the function main in emacs.c).
9054 globals_of_w32fns is called from syms_of_w32fns when the global
9055 variable initialized is 0 and directly from main when initialized
9056 is non zero.
9058 void globals_of_w32fns ()
9060 HMODULE user32_lib = GetModuleHandle ("user32.dll");
9062 TrackMouseEvent not available in all versions of Windows, so must load
9063 it dynamically. Do it once, here, instead of every time it is used.
9065 track_mouse_event_fn = (TrackMouseEvent_Proc)
9066 GetProcAddress (user32_lib, "TrackMouseEvent");
9067 /* ditto for GetClipboardSequenceNumber. */
9068 clipboard_sequence_fn = (ClipboardSequence_Proc)
9069 GetProcAddress (user32_lib, "GetClipboardSequenceNumber");
9071 DEFVAR_INT ("w32-ansi-code-page",
9072 &w32_ansi_code_page,
9073 doc: /* The ANSI code page used by the system. */);
9074 w32_ansi_code_page = GetACP ();
9077 #undef abort
9079 void w32_abort (void) NO_RETURN;
9081 void
9082 w32_abort()
9084 int button;
9085 button = MessageBox (NULL,
9086 "A fatal error has occurred!\n\n"
9087 "Would you like to attach a debugger?\n\n"
9088 "Select YES to debug, NO to abort Emacs"
9089 #if __GNUC__
9090 "\n\n(type \"gdb -p <emacs-PID>\" and\n"
9091 "\"continue\" inside GDB before clicking YES.)"
9092 #endif
9093 , "Emacs Abort Dialog",
9094 MB_ICONEXCLAMATION | MB_TASKMODAL
9095 | MB_SETFOREGROUND | MB_YESNO);
9096 switch (button)
9098 case IDYES:
9099 DebugBreak ();
9100 exit (2); /* tell the compiler we will never return */
9101 case IDNO:
9102 default:
9103 abort ();
9104 break;
9108 /* For convenience when debugging. */
9110 w32_last_error()
9112 return GetLastError ();
9115 /* arch-tag: 707589ab-b9be-4638-8cdd-74629cc9b446
9116 (do not change this comment) */