(bug-reference-map): Bind down-mouse-1 rather than mouse-1.
[emacs.git] / src / w32fns.c
blob9a22601dbd44b456c6d2ba8ef146c4f5416e7bb9
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, 2008
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 /* Added by Kevin Gallo */
23 #include <config.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include <limits.h>
28 #include <errno.h>
29 #include <math.h>
31 #include "lisp.h"
32 #include "w32term.h"
33 #include "frame.h"
34 #include "window.h"
35 #include "buffer.h"
36 #include "intervals.h"
37 #include "dispextern.h"
38 #include "keyboard.h"
39 #include "blockinput.h"
40 #include "epaths.h"
41 #include "character.h"
42 #include "charset.h"
43 #include "coding.h"
44 #include "ccl.h"
45 #include "fontset.h"
46 #include "systime.h"
47 #include "termhooks.h"
48 #include "w32heap.h"
50 #include "bitmaps/gray.xbm"
52 #include <commctrl.h>
53 #include <commdlg.h>
54 #include <shellapi.h>
55 #include <ctype.h>
56 #include <winspool.h>
57 #include <objbase.h>
59 #include <dlgs.h>
60 #include <imm.h>
61 #define FILE_NAME_TEXT_FIELD edt1
63 #include "font.h"
64 #include "w32font.h"
66 void syms_of_w32fns ();
67 void globals_of_w32fns ();
69 extern void free_frame_menubar ();
70 extern double atof ();
71 extern int w32_console_toggle_lock_key P_ ((int, Lisp_Object));
72 extern void w32_menu_display_help P_ ((HWND, HMENU, UINT, UINT));
73 extern void w32_free_menu_strings P_ ((HWND));
75 extern int quit_char;
77 extern char *lispy_function_keys[];
79 /* The colormap for converting color names to RGB values */
80 Lisp_Object Vw32_color_map;
82 /* Non nil if alt key presses are passed on to Windows. */
83 Lisp_Object Vw32_pass_alt_to_system;
85 /* Non nil if alt key is translated to meta_modifier, nil if it is translated
86 to alt_modifier. */
87 Lisp_Object Vw32_alt_is_meta;
89 /* If non-zero, the windows virtual key code for an alternative quit key. */
90 int w32_quit_key;
92 /* Non nil if left window key events are passed on to Windows (this only
93 affects whether "tapping" the key opens the Start menu). */
94 Lisp_Object Vw32_pass_lwindow_to_system;
96 /* Non nil if right window key events are passed on to Windows (this
97 only affects whether "tapping" the key opens the Start menu). */
98 Lisp_Object Vw32_pass_rwindow_to_system;
100 /* Virtual key code used to generate "phantom" key presses in order
101 to stop system from acting on Windows key events. */
102 Lisp_Object Vw32_phantom_key_code;
104 /* Modifier associated with the left "Windows" key, or nil to act as a
105 normal key. */
106 Lisp_Object Vw32_lwindow_modifier;
108 /* Modifier associated with the right "Windows" key, or nil to act as a
109 normal key. */
110 Lisp_Object Vw32_rwindow_modifier;
112 /* Modifier associated with the "Apps" key, or nil to act as a normal
113 key. */
114 Lisp_Object Vw32_apps_modifier;
116 /* Value is nil if Num Lock acts as a function key. */
117 Lisp_Object Vw32_enable_num_lock;
119 /* Value is nil if Caps Lock acts as a function key. */
120 Lisp_Object Vw32_enable_caps_lock;
122 /* Modifier associated with Scroll Lock, or nil to act as a normal key. */
123 Lisp_Object Vw32_scroll_lock_modifier;
125 /* Switch to control whether we inhibit requests for synthesized bold
126 and italic versions of fonts. */
127 int w32_enable_synthesized_fonts;
129 /* Enable palette management. */
130 Lisp_Object Vw32_enable_palette;
132 /* Control how close left/right button down events must be to
133 be converted to a middle button down event. */
134 int w32_mouse_button_tolerance;
136 /* Minimum interval between mouse movement (and scroll bar drag)
137 events that are passed on to the event loop. */
138 int w32_mouse_move_interval;
140 /* Flag to indicate if XBUTTON events should be passed on to Windows. */
141 static int w32_pass_extra_mouse_buttons_to_system;
143 /* Flag to indicate if media keys should be passed on to Windows. */
144 static int w32_pass_multimedia_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 /* If non-zero, a w32 timer that, when it expires, displays an
154 hourglass cursor on all frames. */
155 static unsigned hourglass_timer = 0;
156 static HWND hourglass_hwnd = NULL;
158 /* The background and shape of the mouse pointer, and shape when not
159 over text or in the modeline. */
161 Lisp_Object Vx_pointer_shape, Vx_nontext_pointer_shape, Vx_mode_pointer_shape;
162 Lisp_Object Vx_hourglass_pointer_shape, Vx_window_horizontal_drag_shape;
164 /* The shape when over mouse-sensitive text. */
166 Lisp_Object Vx_sensitive_text_pointer_shape;
168 #ifndef IDC_HAND
169 #define IDC_HAND MAKEINTRESOURCE(32649)
170 #endif
172 /* Color of chars displayed in cursor box. */
174 Lisp_Object Vx_cursor_fore_pixel;
176 /* Nonzero if using Windows. */
178 static int w32_in_use;
180 /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'. */
182 Lisp_Object Vx_pixel_size_width_font_regexp;
184 /* Alist of bdf fonts and the files that define them. */
185 Lisp_Object Vw32_bdf_filename_alist;
187 /* A flag to control whether fonts are matched strictly or not. */
188 static int w32_strict_fontnames;
190 /* A flag to control whether we should only repaint if GetUpdateRect
191 indicates there is an update region. */
192 static int w32_strict_painting;
194 /* Associative list linking character set strings to Windows codepages. */
195 static Lisp_Object Vw32_charset_info_alist;
197 /* VIETNAMESE_CHARSET is not defined in some versions of MSVC. */
198 #ifndef VIETNAMESE_CHARSET
199 #define VIETNAMESE_CHARSET 163
200 #endif
202 Lisp_Object Qnone;
203 Lisp_Object Qsuppress_icon;
204 Lisp_Object Qundefined_color;
205 Lisp_Object Qcancel_timer;
206 Lisp_Object Qfont_param;
207 Lisp_Object Qhyper;
208 Lisp_Object Qsuper;
209 Lisp_Object Qmeta;
210 Lisp_Object Qalt;
211 Lisp_Object Qctrl;
212 Lisp_Object Qcontrol;
213 Lisp_Object Qshift;
215 Lisp_Object Qw32_charset_ansi;
216 Lisp_Object Qw32_charset_default;
217 Lisp_Object Qw32_charset_symbol;
218 Lisp_Object Qw32_charset_shiftjis;
219 Lisp_Object Qw32_charset_hangeul;
220 Lisp_Object Qw32_charset_gb2312;
221 Lisp_Object Qw32_charset_chinesebig5;
222 Lisp_Object Qw32_charset_oem;
224 #ifndef JOHAB_CHARSET
225 #define JOHAB_CHARSET 130
226 #endif
227 #ifdef JOHAB_CHARSET
228 Lisp_Object Qw32_charset_easteurope;
229 Lisp_Object Qw32_charset_turkish;
230 Lisp_Object Qw32_charset_baltic;
231 Lisp_Object Qw32_charset_russian;
232 Lisp_Object Qw32_charset_arabic;
233 Lisp_Object Qw32_charset_greek;
234 Lisp_Object Qw32_charset_hebrew;
235 Lisp_Object Qw32_charset_vietnamese;
236 Lisp_Object Qw32_charset_thai;
237 Lisp_Object Qw32_charset_johab;
238 Lisp_Object Qw32_charset_mac;
239 #endif
241 #ifdef UNICODE_CHARSET
242 Lisp_Object Qw32_charset_unicode;
243 #endif
245 /* The ANSI codepage. */
246 int w32_ansi_code_page;
248 /* Prefix for system colors. */
249 #define SYSTEM_COLOR_PREFIX "System"
250 #define SYSTEM_COLOR_PREFIX_LEN (sizeof (SYSTEM_COLOR_PREFIX) - 1)
252 /* State variables for emulating a three button mouse. */
253 #define LMOUSE 1
254 #define MMOUSE 2
255 #define RMOUSE 4
257 static int button_state = 0;
258 static W32Msg saved_mouse_button_msg;
259 static unsigned mouse_button_timer = 0; /* non-zero when timer is active */
260 static W32Msg saved_mouse_move_msg;
261 static unsigned mouse_move_timer = 0;
263 /* Window that is tracking the mouse. */
264 static HWND track_mouse_window;
266 /* Multi-monitor API definitions that are not pulled from the headers
267 since we are compiling for NT 4. */
268 #ifndef MONITOR_DEFAULT_TO_NEAREST
269 #define MONITOR_DEFAULT_TO_NEAREST 2
270 #endif
271 /* MinGW headers define MONITORINFO unconditionally, but MSVC ones don't.
272 To avoid a compile error on one or the other, redefine with a new name. */
273 struct MONITOR_INFO
275 DWORD cbSize;
276 RECT rcMonitor;
277 RECT rcWork;
278 DWORD dwFlags;
281 typedef BOOL (WINAPI * TrackMouseEvent_Proc)
282 (IN OUT LPTRACKMOUSEEVENT lpEventTrack);
283 typedef LONG (WINAPI * ImmGetCompositionString_Proc)
284 (IN HIMC context, IN DWORD index, OUT LPVOID buffer, IN DWORD bufLen);
285 typedef HIMC (WINAPI * ImmGetContext_Proc) (IN HWND window);
286 typedef HMONITOR (WINAPI * MonitorFromPoint_Proc) (IN POINT pt, IN DWORD flags);
287 typedef BOOL (WINAPI * GetMonitorInfo_Proc)
288 (IN HMONITOR monitor, OUT struct MONITOR_INFO* info);
290 TrackMouseEvent_Proc track_mouse_event_fn = NULL;
291 ClipboardSequence_Proc clipboard_sequence_fn = NULL;
292 ImmGetCompositionString_Proc get_composition_string_fn = NULL;
293 ImmGetContext_Proc get_ime_context_fn = NULL;
294 MonitorFromPoint_Proc monitor_from_point_fn = NULL;
295 GetMonitorInfo_Proc get_monitor_info_fn = NULL;
297 extern AppendMenuW_Proc unicode_append_menu;
299 /* Flag to selectively ignore WM_IME_CHAR messages. */
300 static int ignore_ime_char = 0;
302 /* W95 mousewheel handler */
303 unsigned int msh_mousewheel = 0;
305 /* Timers */
306 #define MOUSE_BUTTON_ID 1
307 #define MOUSE_MOVE_ID 2
308 #define MENU_FREE_ID 3
309 #define HOURGLASS_ID 4
310 /* The delay (milliseconds) before a menu is freed after WM_EXITMENULOOP
311 is received. */
312 #define MENU_FREE_DELAY 1000
313 static unsigned menu_free_timer = 0;
315 /* The below are defined in frame.c. */
317 extern Lisp_Object Vwindow_system_version;
319 #ifdef GLYPH_DEBUG
320 int image_cache_refcount, dpyinfo_refcount;
321 #endif
324 /* From w32term.c. */
325 extern int w32_num_mouse_buttons;
326 extern Lisp_Object Vw32_recognize_altgr;
328 extern HWND w32_system_caret_hwnd;
330 extern int w32_system_caret_height;
331 extern int w32_system_caret_x;
332 extern int w32_system_caret_y;
333 extern int w32_use_visible_system_caret;
335 static HWND w32_visible_system_caret_hwnd;
337 /* From w32menu.c */
338 extern HMENU current_popup_menu;
339 static int menubar_in_use = 0;
341 /* From w32uniscribe.c */
342 extern void syms_of_w32uniscribe ();
343 extern int uniscribe_available;
345 /* Function prototypes for hourglass support. */
346 static void show_hourglass P_ ((struct frame *));
347 static void hide_hourglass P_ ((void));
351 /* Error if we are not connected to MS-Windows. */
352 void
353 check_w32 ()
355 if (! w32_in_use)
356 error ("MS-Windows not in use or not initialized");
359 /* Nonzero if we can use mouse menus.
360 You should not call this unless HAVE_MENUS is defined. */
363 have_menus_p ()
365 return w32_in_use;
368 /* Extract a frame as a FRAME_PTR, defaulting to the selected frame
369 and checking validity for W32. */
371 FRAME_PTR
372 check_x_frame (frame)
373 Lisp_Object frame;
375 FRAME_PTR f;
377 if (NILP (frame))
378 frame = selected_frame;
379 CHECK_LIVE_FRAME (frame);
380 f = XFRAME (frame);
381 if (! FRAME_W32_P (f))
382 error ("Non-W32 frame used");
383 return f;
386 /* Let the user specify a display with a frame.
387 nil stands for the selected frame--or, if that is not a w32 frame,
388 the first display on the list. */
390 struct w32_display_info *
391 check_x_display_info (frame)
392 Lisp_Object frame;
394 if (NILP (frame))
396 struct frame *sf = XFRAME (selected_frame);
398 if (FRAME_W32_P (sf) && FRAME_LIVE_P (sf))
399 return FRAME_W32_DISPLAY_INFO (sf);
400 else
401 return &one_w32_display_info;
403 else if (STRINGP (frame))
404 return x_display_info_for_name (frame);
405 else
407 FRAME_PTR f;
409 CHECK_LIVE_FRAME (frame);
410 f = XFRAME (frame);
411 if (! FRAME_W32_P (f))
412 error ("Non-W32 frame used");
413 return FRAME_W32_DISPLAY_INFO (f);
417 /* Return the Emacs frame-object corresponding to an w32 window.
418 It could be the frame's main window or an icon window. */
420 /* This function can be called during GC, so use GC_xxx type test macros. */
422 struct frame *
423 x_window_to_frame (dpyinfo, wdesc)
424 struct w32_display_info *dpyinfo;
425 HWND wdesc;
427 Lisp_Object tail, frame;
428 struct frame *f;
430 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
432 frame = XCAR (tail);
433 if (!FRAMEP (frame))
434 continue;
435 f = XFRAME (frame);
436 if (!FRAME_W32_P (f) || FRAME_W32_DISPLAY_INFO (f) != dpyinfo)
437 continue;
439 if (FRAME_W32_WINDOW (f) == wdesc)
440 return f;
442 return 0;
446 static Lisp_Object unwind_create_frame P_ ((Lisp_Object));
447 static Lisp_Object unwind_create_tip_frame P_ ((Lisp_Object));
448 static void my_create_window P_ ((struct frame *));
449 static void my_create_tip_window P_ ((struct frame *));
451 /* TODO: Native Input Method support; see x_create_im. */
452 void x_set_foreground_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
453 void x_set_background_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
454 void x_set_mouse_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
455 void x_set_cursor_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
456 void x_set_border_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
457 void x_set_cursor_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
458 void x_set_icon_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
459 void x_set_icon_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
460 void x_explicitly_set_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
461 void x_set_menu_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
462 void x_set_title P_ ((struct frame *, Lisp_Object, Lisp_Object));
463 void x_set_tool_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
464 static void x_edge_detection P_ ((struct frame *, struct image *, Lisp_Object,
465 Lisp_Object));
470 /* Store the screen positions of frame F into XPTR and YPTR.
471 These are the positions of the containing window manager window,
472 not Emacs's own window. */
474 void
475 x_real_positions (f, xptr, yptr)
476 FRAME_PTR f;
477 int *xptr, *yptr;
479 POINT pt;
480 RECT rect;
482 /* Get the bounds of the WM window. */
483 GetWindowRect (FRAME_W32_WINDOW (f), &rect);
485 pt.x = 0;
486 pt.y = 0;
488 /* Convert (0, 0) in the client area to screen co-ordinates. */
489 ClientToScreen (FRAME_W32_WINDOW (f), &pt);
491 /* Remember x_pixels_diff and y_pixels_diff. */
492 f->x_pixels_diff = pt.x - rect.left;
493 f->y_pixels_diff = pt.y - rect.top;
495 *xptr = rect.left;
496 *yptr = rect.top;
501 DEFUN ("w32-define-rgb-color", Fw32_define_rgb_color,
502 Sw32_define_rgb_color, 4, 4, 0,
503 doc: /* Convert RGB numbers to a Windows color reference and associate with NAME.
504 This adds or updates a named color to `w32-color-map', making it
505 available for use. The original entry's RGB ref is returned, or nil
506 if the entry is new. */)
507 (red, green, blue, name)
508 Lisp_Object red, green, blue, name;
510 Lisp_Object rgb;
511 Lisp_Object oldrgb = Qnil;
512 Lisp_Object entry;
514 CHECK_NUMBER (red);
515 CHECK_NUMBER (green);
516 CHECK_NUMBER (blue);
517 CHECK_STRING (name);
519 XSETINT (rgb, RGB (XUINT (red), XUINT (green), XUINT (blue)));
521 BLOCK_INPUT;
523 /* replace existing entry in w32-color-map or add new entry. */
524 entry = Fassoc (name, Vw32_color_map);
525 if (NILP (entry))
527 entry = Fcons (name, rgb);
528 Vw32_color_map = Fcons (entry, Vw32_color_map);
530 else
532 oldrgb = Fcdr (entry);
533 Fsetcdr (entry, rgb);
536 UNBLOCK_INPUT;
538 return (oldrgb);
541 DEFUN ("w32-load-color-file", Fw32_load_color_file,
542 Sw32_load_color_file, 1, 1, 0,
543 doc: /* Create an alist of color entries from an external file.
544 Assign this value to `w32-color-map' to replace the existing color map.
546 The file should define one named RGB color per line like so:
547 R G B name
548 where R,G,B are numbers between 0 and 255 and name is an arbitrary string. */)
549 (filename)
550 Lisp_Object filename;
552 FILE *fp;
553 Lisp_Object cmap = Qnil;
554 Lisp_Object abspath;
556 CHECK_STRING (filename);
557 abspath = Fexpand_file_name (filename, Qnil);
559 fp = fopen (SDATA (filename), "rt");
560 if (fp)
562 char buf[512];
563 int red, green, blue;
564 int num;
566 BLOCK_INPUT;
568 while (fgets (buf, sizeof (buf), fp) != NULL) {
569 if (sscanf (buf, "%u %u %u %n", &red, &green, &blue, &num) == 3)
571 char *name = buf + num;
572 num = strlen (name) - 1;
573 if (name[num] == '\n')
574 name[num] = 0;
575 cmap = Fcons (Fcons (build_string (name),
576 make_number (RGB (red, green, blue))),
577 cmap);
580 fclose (fp);
582 UNBLOCK_INPUT;
585 return cmap;
588 /* The default colors for the w32 color map */
589 typedef struct colormap_t
591 char *name;
592 COLORREF colorref;
593 } colormap_t;
595 colormap_t w32_color_map[] =
597 {"snow" , PALETTERGB (255,250,250)},
598 {"ghost white" , PALETTERGB (248,248,255)},
599 {"GhostWhite" , PALETTERGB (248,248,255)},
600 {"white smoke" , PALETTERGB (245,245,245)},
601 {"WhiteSmoke" , PALETTERGB (245,245,245)},
602 {"gainsboro" , PALETTERGB (220,220,220)},
603 {"floral white" , PALETTERGB (255,250,240)},
604 {"FloralWhite" , PALETTERGB (255,250,240)},
605 {"old lace" , PALETTERGB (253,245,230)},
606 {"OldLace" , PALETTERGB (253,245,230)},
607 {"linen" , PALETTERGB (250,240,230)},
608 {"antique white" , PALETTERGB (250,235,215)},
609 {"AntiqueWhite" , PALETTERGB (250,235,215)},
610 {"papaya whip" , PALETTERGB (255,239,213)},
611 {"PapayaWhip" , PALETTERGB (255,239,213)},
612 {"blanched almond" , PALETTERGB (255,235,205)},
613 {"BlanchedAlmond" , PALETTERGB (255,235,205)},
614 {"bisque" , PALETTERGB (255,228,196)},
615 {"peach puff" , PALETTERGB (255,218,185)},
616 {"PeachPuff" , PALETTERGB (255,218,185)},
617 {"navajo white" , PALETTERGB (255,222,173)},
618 {"NavajoWhite" , PALETTERGB (255,222,173)},
619 {"moccasin" , PALETTERGB (255,228,181)},
620 {"cornsilk" , PALETTERGB (255,248,220)},
621 {"ivory" , PALETTERGB (255,255,240)},
622 {"lemon chiffon" , PALETTERGB (255,250,205)},
623 {"LemonChiffon" , PALETTERGB (255,250,205)},
624 {"seashell" , PALETTERGB (255,245,238)},
625 {"honeydew" , PALETTERGB (240,255,240)},
626 {"mint cream" , PALETTERGB (245,255,250)},
627 {"MintCream" , PALETTERGB (245,255,250)},
628 {"azure" , PALETTERGB (240,255,255)},
629 {"alice blue" , PALETTERGB (240,248,255)},
630 {"AliceBlue" , PALETTERGB (240,248,255)},
631 {"lavender" , PALETTERGB (230,230,250)},
632 {"lavender blush" , PALETTERGB (255,240,245)},
633 {"LavenderBlush" , PALETTERGB (255,240,245)},
634 {"misty rose" , PALETTERGB (255,228,225)},
635 {"MistyRose" , PALETTERGB (255,228,225)},
636 {"white" , PALETTERGB (255,255,255)},
637 {"black" , PALETTERGB ( 0, 0, 0)},
638 {"dark slate gray" , PALETTERGB ( 47, 79, 79)},
639 {"DarkSlateGray" , PALETTERGB ( 47, 79, 79)},
640 {"dark slate grey" , PALETTERGB ( 47, 79, 79)},
641 {"DarkSlateGrey" , PALETTERGB ( 47, 79, 79)},
642 {"dim gray" , PALETTERGB (105,105,105)},
643 {"DimGray" , PALETTERGB (105,105,105)},
644 {"dim grey" , PALETTERGB (105,105,105)},
645 {"DimGrey" , PALETTERGB (105,105,105)},
646 {"slate gray" , PALETTERGB (112,128,144)},
647 {"SlateGray" , PALETTERGB (112,128,144)},
648 {"slate grey" , PALETTERGB (112,128,144)},
649 {"SlateGrey" , PALETTERGB (112,128,144)},
650 {"light slate gray" , PALETTERGB (119,136,153)},
651 {"LightSlateGray" , PALETTERGB (119,136,153)},
652 {"light slate grey" , PALETTERGB (119,136,153)},
653 {"LightSlateGrey" , PALETTERGB (119,136,153)},
654 {"gray" , PALETTERGB (190,190,190)},
655 {"grey" , PALETTERGB (190,190,190)},
656 {"light grey" , PALETTERGB (211,211,211)},
657 {"LightGrey" , PALETTERGB (211,211,211)},
658 {"light gray" , PALETTERGB (211,211,211)},
659 {"LightGray" , PALETTERGB (211,211,211)},
660 {"midnight blue" , PALETTERGB ( 25, 25,112)},
661 {"MidnightBlue" , PALETTERGB ( 25, 25,112)},
662 {"navy" , PALETTERGB ( 0, 0,128)},
663 {"navy blue" , PALETTERGB ( 0, 0,128)},
664 {"NavyBlue" , PALETTERGB ( 0, 0,128)},
665 {"cornflower blue" , PALETTERGB (100,149,237)},
666 {"CornflowerBlue" , PALETTERGB (100,149,237)},
667 {"dark slate blue" , PALETTERGB ( 72, 61,139)},
668 {"DarkSlateBlue" , PALETTERGB ( 72, 61,139)},
669 {"slate blue" , PALETTERGB (106, 90,205)},
670 {"SlateBlue" , PALETTERGB (106, 90,205)},
671 {"medium slate blue" , PALETTERGB (123,104,238)},
672 {"MediumSlateBlue" , PALETTERGB (123,104,238)},
673 {"light slate blue" , PALETTERGB (132,112,255)},
674 {"LightSlateBlue" , PALETTERGB (132,112,255)},
675 {"medium blue" , PALETTERGB ( 0, 0,205)},
676 {"MediumBlue" , PALETTERGB ( 0, 0,205)},
677 {"royal blue" , PALETTERGB ( 65,105,225)},
678 {"RoyalBlue" , PALETTERGB ( 65,105,225)},
679 {"blue" , PALETTERGB ( 0, 0,255)},
680 {"dodger blue" , PALETTERGB ( 30,144,255)},
681 {"DodgerBlue" , PALETTERGB ( 30,144,255)},
682 {"deep sky blue" , PALETTERGB ( 0,191,255)},
683 {"DeepSkyBlue" , PALETTERGB ( 0,191,255)},
684 {"sky blue" , PALETTERGB (135,206,235)},
685 {"SkyBlue" , PALETTERGB (135,206,235)},
686 {"light sky blue" , PALETTERGB (135,206,250)},
687 {"LightSkyBlue" , PALETTERGB (135,206,250)},
688 {"steel blue" , PALETTERGB ( 70,130,180)},
689 {"SteelBlue" , PALETTERGB ( 70,130,180)},
690 {"light steel blue" , PALETTERGB (176,196,222)},
691 {"LightSteelBlue" , PALETTERGB (176,196,222)},
692 {"light blue" , PALETTERGB (173,216,230)},
693 {"LightBlue" , PALETTERGB (173,216,230)},
694 {"powder blue" , PALETTERGB (176,224,230)},
695 {"PowderBlue" , PALETTERGB (176,224,230)},
696 {"pale turquoise" , PALETTERGB (175,238,238)},
697 {"PaleTurquoise" , PALETTERGB (175,238,238)},
698 {"dark turquoise" , PALETTERGB ( 0,206,209)},
699 {"DarkTurquoise" , PALETTERGB ( 0,206,209)},
700 {"medium turquoise" , PALETTERGB ( 72,209,204)},
701 {"MediumTurquoise" , PALETTERGB ( 72,209,204)},
702 {"turquoise" , PALETTERGB ( 64,224,208)},
703 {"cyan" , PALETTERGB ( 0,255,255)},
704 {"light cyan" , PALETTERGB (224,255,255)},
705 {"LightCyan" , PALETTERGB (224,255,255)},
706 {"cadet blue" , PALETTERGB ( 95,158,160)},
707 {"CadetBlue" , PALETTERGB ( 95,158,160)},
708 {"medium aquamarine" , PALETTERGB (102,205,170)},
709 {"MediumAquamarine" , PALETTERGB (102,205,170)},
710 {"aquamarine" , PALETTERGB (127,255,212)},
711 {"dark green" , PALETTERGB ( 0,100, 0)},
712 {"DarkGreen" , PALETTERGB ( 0,100, 0)},
713 {"dark olive green" , PALETTERGB ( 85,107, 47)},
714 {"DarkOliveGreen" , PALETTERGB ( 85,107, 47)},
715 {"dark sea green" , PALETTERGB (143,188,143)},
716 {"DarkSeaGreen" , PALETTERGB (143,188,143)},
717 {"sea green" , PALETTERGB ( 46,139, 87)},
718 {"SeaGreen" , PALETTERGB ( 46,139, 87)},
719 {"medium sea green" , PALETTERGB ( 60,179,113)},
720 {"MediumSeaGreen" , PALETTERGB ( 60,179,113)},
721 {"light sea green" , PALETTERGB ( 32,178,170)},
722 {"LightSeaGreen" , PALETTERGB ( 32,178,170)},
723 {"pale green" , PALETTERGB (152,251,152)},
724 {"PaleGreen" , PALETTERGB (152,251,152)},
725 {"spring green" , PALETTERGB ( 0,255,127)},
726 {"SpringGreen" , PALETTERGB ( 0,255,127)},
727 {"lawn green" , PALETTERGB (124,252, 0)},
728 {"LawnGreen" , PALETTERGB (124,252, 0)},
729 {"green" , PALETTERGB ( 0,255, 0)},
730 {"chartreuse" , PALETTERGB (127,255, 0)},
731 {"medium spring green" , PALETTERGB ( 0,250,154)},
732 {"MediumSpringGreen" , PALETTERGB ( 0,250,154)},
733 {"green yellow" , PALETTERGB (173,255, 47)},
734 {"GreenYellow" , PALETTERGB (173,255, 47)},
735 {"lime green" , PALETTERGB ( 50,205, 50)},
736 {"LimeGreen" , PALETTERGB ( 50,205, 50)},
737 {"yellow green" , PALETTERGB (154,205, 50)},
738 {"YellowGreen" , PALETTERGB (154,205, 50)},
739 {"forest green" , PALETTERGB ( 34,139, 34)},
740 {"ForestGreen" , PALETTERGB ( 34,139, 34)},
741 {"olive drab" , PALETTERGB (107,142, 35)},
742 {"OliveDrab" , PALETTERGB (107,142, 35)},
743 {"dark khaki" , PALETTERGB (189,183,107)},
744 {"DarkKhaki" , PALETTERGB (189,183,107)},
745 {"khaki" , PALETTERGB (240,230,140)},
746 {"pale goldenrod" , PALETTERGB (238,232,170)},
747 {"PaleGoldenrod" , PALETTERGB (238,232,170)},
748 {"light goldenrod yellow" , PALETTERGB (250,250,210)},
749 {"LightGoldenrodYellow" , PALETTERGB (250,250,210)},
750 {"light yellow" , PALETTERGB (255,255,224)},
751 {"LightYellow" , PALETTERGB (255,255,224)},
752 {"yellow" , PALETTERGB (255,255, 0)},
753 {"gold" , PALETTERGB (255,215, 0)},
754 {"light goldenrod" , PALETTERGB (238,221,130)},
755 {"LightGoldenrod" , PALETTERGB (238,221,130)},
756 {"goldenrod" , PALETTERGB (218,165, 32)},
757 {"dark goldenrod" , PALETTERGB (184,134, 11)},
758 {"DarkGoldenrod" , PALETTERGB (184,134, 11)},
759 {"rosy brown" , PALETTERGB (188,143,143)},
760 {"RosyBrown" , PALETTERGB (188,143,143)},
761 {"indian red" , PALETTERGB (205, 92, 92)},
762 {"IndianRed" , PALETTERGB (205, 92, 92)},
763 {"saddle brown" , PALETTERGB (139, 69, 19)},
764 {"SaddleBrown" , PALETTERGB (139, 69, 19)},
765 {"sienna" , PALETTERGB (160, 82, 45)},
766 {"peru" , PALETTERGB (205,133, 63)},
767 {"burlywood" , PALETTERGB (222,184,135)},
768 {"beige" , PALETTERGB (245,245,220)},
769 {"wheat" , PALETTERGB (245,222,179)},
770 {"sandy brown" , PALETTERGB (244,164, 96)},
771 {"SandyBrown" , PALETTERGB (244,164, 96)},
772 {"tan" , PALETTERGB (210,180,140)},
773 {"chocolate" , PALETTERGB (210,105, 30)},
774 {"firebrick" , PALETTERGB (178,34, 34)},
775 {"brown" , PALETTERGB (165,42, 42)},
776 {"dark salmon" , PALETTERGB (233,150,122)},
777 {"DarkSalmon" , PALETTERGB (233,150,122)},
778 {"salmon" , PALETTERGB (250,128,114)},
779 {"light salmon" , PALETTERGB (255,160,122)},
780 {"LightSalmon" , PALETTERGB (255,160,122)},
781 {"orange" , PALETTERGB (255,165, 0)},
782 {"dark orange" , PALETTERGB (255,140, 0)},
783 {"DarkOrange" , PALETTERGB (255,140, 0)},
784 {"coral" , PALETTERGB (255,127, 80)},
785 {"light coral" , PALETTERGB (240,128,128)},
786 {"LightCoral" , PALETTERGB (240,128,128)},
787 {"tomato" , PALETTERGB (255, 99, 71)},
788 {"orange red" , PALETTERGB (255, 69, 0)},
789 {"OrangeRed" , PALETTERGB (255, 69, 0)},
790 {"red" , PALETTERGB (255, 0, 0)},
791 {"hot pink" , PALETTERGB (255,105,180)},
792 {"HotPink" , PALETTERGB (255,105,180)},
793 {"deep pink" , PALETTERGB (255, 20,147)},
794 {"DeepPink" , PALETTERGB (255, 20,147)},
795 {"pink" , PALETTERGB (255,192,203)},
796 {"light pink" , PALETTERGB (255,182,193)},
797 {"LightPink" , PALETTERGB (255,182,193)},
798 {"pale violet red" , PALETTERGB (219,112,147)},
799 {"PaleVioletRed" , PALETTERGB (219,112,147)},
800 {"maroon" , PALETTERGB (176, 48, 96)},
801 {"medium violet red" , PALETTERGB (199, 21,133)},
802 {"MediumVioletRed" , PALETTERGB (199, 21,133)},
803 {"violet red" , PALETTERGB (208, 32,144)},
804 {"VioletRed" , PALETTERGB (208, 32,144)},
805 {"magenta" , PALETTERGB (255, 0,255)},
806 {"violet" , PALETTERGB (238,130,238)},
807 {"plum" , PALETTERGB (221,160,221)},
808 {"orchid" , PALETTERGB (218,112,214)},
809 {"medium orchid" , PALETTERGB (186, 85,211)},
810 {"MediumOrchid" , PALETTERGB (186, 85,211)},
811 {"dark orchid" , PALETTERGB (153, 50,204)},
812 {"DarkOrchid" , PALETTERGB (153, 50,204)},
813 {"dark violet" , PALETTERGB (148, 0,211)},
814 {"DarkViolet" , PALETTERGB (148, 0,211)},
815 {"blue violet" , PALETTERGB (138, 43,226)},
816 {"BlueViolet" , PALETTERGB (138, 43,226)},
817 {"purple" , PALETTERGB (160, 32,240)},
818 {"medium purple" , PALETTERGB (147,112,219)},
819 {"MediumPurple" , PALETTERGB (147,112,219)},
820 {"thistle" , PALETTERGB (216,191,216)},
821 {"gray0" , PALETTERGB ( 0, 0, 0)},
822 {"grey0" , PALETTERGB ( 0, 0, 0)},
823 {"dark grey" , PALETTERGB (169,169,169)},
824 {"DarkGrey" , PALETTERGB (169,169,169)},
825 {"dark gray" , PALETTERGB (169,169,169)},
826 {"DarkGray" , PALETTERGB (169,169,169)},
827 {"dark blue" , PALETTERGB ( 0, 0,139)},
828 {"DarkBlue" , PALETTERGB ( 0, 0,139)},
829 {"dark cyan" , PALETTERGB ( 0,139,139)},
830 {"DarkCyan" , PALETTERGB ( 0,139,139)},
831 {"dark magenta" , PALETTERGB (139, 0,139)},
832 {"DarkMagenta" , PALETTERGB (139, 0,139)},
833 {"dark red" , PALETTERGB (139, 0, 0)},
834 {"DarkRed" , PALETTERGB (139, 0, 0)},
835 {"light green" , PALETTERGB (144,238,144)},
836 {"LightGreen" , PALETTERGB (144,238,144)},
839 DEFUN ("w32-default-color-map", Fw32_default_color_map, Sw32_default_color_map,
840 0, 0, 0, doc: /* Return the default color map. */)
843 int i;
844 colormap_t *pc = w32_color_map;
845 Lisp_Object cmap;
847 BLOCK_INPUT;
849 cmap = Qnil;
851 for (i = 0; i < sizeof (w32_color_map) / sizeof (w32_color_map[0]);
852 pc++, i++)
853 cmap = Fcons (Fcons (build_string (pc->name),
854 make_number (pc->colorref)),
855 cmap);
857 UNBLOCK_INPUT;
859 return (cmap);
862 static Lisp_Object
863 w32_to_x_color (rgb)
864 Lisp_Object rgb;
866 Lisp_Object color;
868 CHECK_NUMBER (rgb);
870 BLOCK_INPUT;
872 color = Frassq (rgb, Vw32_color_map);
874 UNBLOCK_INPUT;
876 if (!NILP (color))
877 return (Fcar (color));
878 else
879 return Qnil;
882 static Lisp_Object
883 w32_color_map_lookup (colorname)
884 char *colorname;
886 Lisp_Object tail, ret = Qnil;
888 BLOCK_INPUT;
890 for (tail = Vw32_color_map; CONSP (tail); tail = XCDR (tail))
892 register Lisp_Object elt, tem;
894 elt = XCAR (tail);
895 if (!CONSP (elt)) continue;
897 tem = Fcar (elt);
899 if (lstrcmpi (SDATA (tem), colorname) == 0)
901 ret = Fcdr (elt);
902 break;
905 QUIT;
909 UNBLOCK_INPUT;
911 return ret;
915 static void
916 add_system_logical_colors_to_map (system_colors)
917 Lisp_Object *system_colors;
919 HKEY colors_key;
921 /* Other registry operations are done with input blocked. */
922 BLOCK_INPUT;
924 /* Look for "Control Panel/Colors" under User and Machine registry
925 settings. */
926 if (RegOpenKeyEx (HKEY_CURRENT_USER, "Control Panel\\Colors", 0,
927 KEY_READ, &colors_key) == ERROR_SUCCESS
928 || RegOpenKeyEx (HKEY_LOCAL_MACHINE, "Control Panel\\Colors", 0,
929 KEY_READ, &colors_key) == ERROR_SUCCESS)
931 /* List all keys. */
932 char color_buffer[64];
933 char full_name_buffer[MAX_PATH + SYSTEM_COLOR_PREFIX_LEN];
934 int index = 0;
935 DWORD name_size, color_size;
936 char *name_buffer = full_name_buffer + SYSTEM_COLOR_PREFIX_LEN;
938 name_size = sizeof (full_name_buffer) - SYSTEM_COLOR_PREFIX_LEN;
939 color_size = sizeof (color_buffer);
941 strcpy (full_name_buffer, SYSTEM_COLOR_PREFIX);
943 while (RegEnumValueA (colors_key, index, name_buffer, &name_size,
944 NULL, NULL, color_buffer, &color_size)
945 == ERROR_SUCCESS)
947 int r, g, b;
948 if (sscanf (color_buffer, " %u %u %u", &r, &g, &b) == 3)
949 *system_colors = Fcons (Fcons (build_string (full_name_buffer),
950 make_number (RGB (r, g, b))),
951 *system_colors);
953 name_size = sizeof (full_name_buffer) - SYSTEM_COLOR_PREFIX_LEN;
954 color_size = sizeof (color_buffer);
955 index++;
957 RegCloseKey (colors_key);
960 UNBLOCK_INPUT;
964 static Lisp_Object
965 x_to_w32_color (colorname)
966 char * colorname;
968 register Lisp_Object ret = Qnil;
970 BLOCK_INPUT;
972 if (colorname[0] == '#')
974 /* Could be an old-style RGB Device specification. */
975 char *color;
976 int size;
977 color = colorname + 1;
979 size = strlen (color);
980 if (size == 3 || size == 6 || size == 9 || size == 12)
982 UINT colorval;
983 int i, pos;
984 pos = 0;
985 size /= 3;
986 colorval = 0;
988 for (i = 0; i < 3; i++)
990 char *end;
991 char t;
992 unsigned long value;
994 /* The check for 'x' in the following conditional takes into
995 account the fact that strtol allows a "0x" in front of
996 our numbers, and we don't. */
997 if (!isxdigit (color[0]) || color[1] == 'x')
998 break;
999 t = color[size];
1000 color[size] = '\0';
1001 value = strtoul (color, &end, 16);
1002 color[size] = t;
1003 if (errno == ERANGE || end - color != size)
1004 break;
1005 switch (size)
1007 case 1:
1008 value = value * 0x10;
1009 break;
1010 case 2:
1011 break;
1012 case 3:
1013 value /= 0x10;
1014 break;
1015 case 4:
1016 value /= 0x100;
1017 break;
1019 colorval |= (value << pos);
1020 pos += 0x8;
1021 if (i == 2)
1023 UNBLOCK_INPUT;
1024 XSETINT (ret, colorval);
1025 return ret;
1027 color = end;
1031 else if (strnicmp (colorname, "rgb:", 4) == 0)
1033 char *color;
1034 UINT colorval;
1035 int i, pos;
1036 pos = 0;
1038 colorval = 0;
1039 color = colorname + 4;
1040 for (i = 0; i < 3; i++)
1042 char *end;
1043 unsigned long value;
1045 /* The check for 'x' in the following conditional takes into
1046 account the fact that strtol allows a "0x" in front of
1047 our numbers, and we don't. */
1048 if (!isxdigit (color[0]) || color[1] == 'x')
1049 break;
1050 value = strtoul (color, &end, 16);
1051 if (errno == ERANGE)
1052 break;
1053 switch (end - color)
1055 case 1:
1056 value = value * 0x10 + value;
1057 break;
1058 case 2:
1059 break;
1060 case 3:
1061 value /= 0x10;
1062 break;
1063 case 4:
1064 value /= 0x100;
1065 break;
1066 default:
1067 value = ULONG_MAX;
1069 if (value == ULONG_MAX)
1070 break;
1071 colorval |= (value << pos);
1072 pos += 0x8;
1073 if (i == 2)
1075 if (*end != '\0')
1076 break;
1077 UNBLOCK_INPUT;
1078 XSETINT (ret, colorval);
1079 return ret;
1081 if (*end != '/')
1082 break;
1083 color = end + 1;
1086 else if (strnicmp (colorname, "rgbi:", 5) == 0)
1088 /* This is an RGB Intensity specification. */
1089 char *color;
1090 UINT colorval;
1091 int i, pos;
1092 pos = 0;
1094 colorval = 0;
1095 color = colorname + 5;
1096 for (i = 0; i < 3; i++)
1098 char *end;
1099 double value;
1100 UINT val;
1102 value = strtod (color, &end);
1103 if (errno == ERANGE)
1104 break;
1105 if (value < 0.0 || value > 1.0)
1106 break;
1107 val = (UINT)(0x100 * value);
1108 /* We used 0x100 instead of 0xFF to give a continuous
1109 range between 0.0 and 1.0 inclusive. The next statement
1110 fixes the 1.0 case. */
1111 if (val == 0x100)
1112 val = 0xFF;
1113 colorval |= (val << pos);
1114 pos += 0x8;
1115 if (i == 2)
1117 if (*end != '\0')
1118 break;
1119 UNBLOCK_INPUT;
1120 XSETINT (ret, colorval);
1121 return ret;
1123 if (*end != '/')
1124 break;
1125 color = end + 1;
1128 /* I am not going to attempt to handle any of the CIE color schemes
1129 or TekHVC, since I don't know the algorithms for conversion to
1130 RGB. */
1132 /* If we fail to lookup the color name in w32_color_map, then check the
1133 colorname to see if it can be crudely approximated: If the X color
1134 ends in a number (e.g., "darkseagreen2"), strip the number and
1135 return the result of looking up the base color name. */
1136 ret = w32_color_map_lookup (colorname);
1137 if (NILP (ret))
1139 int len = strlen (colorname);
1141 if (isdigit (colorname[len - 1]))
1143 char *ptr, *approx = alloca (len + 1);
1145 strcpy (approx, colorname);
1146 ptr = &approx[len - 1];
1147 while (ptr > approx && isdigit (*ptr))
1148 *ptr-- = '\0';
1150 ret = w32_color_map_lookup (approx);
1154 UNBLOCK_INPUT;
1155 return ret;
1158 void
1159 w32_regenerate_palette (FRAME_PTR f)
1161 struct w32_palette_entry * list;
1162 LOGPALETTE * log_palette;
1163 HPALETTE new_palette;
1164 int i;
1166 /* don't bother trying to create palette if not supported */
1167 if (! FRAME_W32_DISPLAY_INFO (f)->has_palette)
1168 return;
1170 log_palette = (LOGPALETTE *)
1171 alloca (sizeof (LOGPALETTE) +
1172 FRAME_W32_DISPLAY_INFO (f)->num_colors * sizeof (PALETTEENTRY));
1173 log_palette->palVersion = 0x300;
1174 log_palette->palNumEntries = FRAME_W32_DISPLAY_INFO (f)->num_colors;
1176 list = FRAME_W32_DISPLAY_INFO (f)->color_list;
1177 for (i = 0;
1178 i < FRAME_W32_DISPLAY_INFO (f)->num_colors;
1179 i++, list = list->next)
1180 log_palette->palPalEntry[i] = list->entry;
1182 new_palette = CreatePalette (log_palette);
1184 enter_crit ();
1186 if (FRAME_W32_DISPLAY_INFO (f)->palette)
1187 DeleteObject (FRAME_W32_DISPLAY_INFO (f)->palette);
1188 FRAME_W32_DISPLAY_INFO (f)->palette = new_palette;
1190 /* Realize display palette and garbage all frames. */
1191 release_frame_dc (f, get_frame_dc (f));
1193 leave_crit ();
1196 #define W32_COLOR(pe) RGB (pe.peRed, pe.peGreen, pe.peBlue)
1197 #define SET_W32_COLOR(pe, color) \
1198 do \
1200 pe.peRed = GetRValue (color); \
1201 pe.peGreen = GetGValue (color); \
1202 pe.peBlue = GetBValue (color); \
1203 pe.peFlags = 0; \
1204 } while (0)
1206 #if 0
1207 /* Keep these around in case we ever want to track color usage. */
1208 void
1209 w32_map_color (FRAME_PTR f, COLORREF color)
1211 struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
1213 if (NILP (Vw32_enable_palette))
1214 return;
1216 /* check if color is already mapped */
1217 while (list)
1219 if (W32_COLOR (list->entry) == color)
1221 ++list->refcount;
1222 return;
1224 list = list->next;
1227 /* not already mapped, so add to list and recreate Windows palette */
1228 list = (struct w32_palette_entry *)
1229 xmalloc (sizeof (struct w32_palette_entry));
1230 SET_W32_COLOR (list->entry, color);
1231 list->refcount = 1;
1232 list->next = FRAME_W32_DISPLAY_INFO (f)->color_list;
1233 FRAME_W32_DISPLAY_INFO (f)->color_list = list;
1234 FRAME_W32_DISPLAY_INFO (f)->num_colors++;
1236 /* set flag that palette must be regenerated */
1237 FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
1240 void
1241 w32_unmap_color (FRAME_PTR f, COLORREF color)
1243 struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
1244 struct w32_palette_entry **prev = &FRAME_W32_DISPLAY_INFO (f)->color_list;
1246 if (NILP (Vw32_enable_palette))
1247 return;
1249 /* check if color is already mapped */
1250 while (list)
1252 if (W32_COLOR (list->entry) == color)
1254 if (--list->refcount == 0)
1256 *prev = list->next;
1257 xfree (list);
1258 FRAME_W32_DISPLAY_INFO (f)->num_colors--;
1259 break;
1261 else
1262 return;
1264 prev = &list->next;
1265 list = list->next;
1268 /* set flag that palette must be regenerated */
1269 FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
1271 #endif
1274 /* Gamma-correct COLOR on frame F. */
1276 void
1277 gamma_correct (f, color)
1278 struct frame *f;
1279 COLORREF *color;
1281 if (f->gamma)
1283 *color = PALETTERGB (
1284 pow (GetRValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
1285 pow (GetGValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
1286 pow (GetBValue (*color) / 255.0, f->gamma) * 255.0 + 0.5);
1291 /* Decide if color named COLOR is valid for the display associated with
1292 the selected frame; if so, return the rgb values in COLOR_DEF.
1293 If ALLOC is nonzero, allocate a new colormap cell. */
1296 w32_defined_color (f, color, color_def, alloc)
1297 FRAME_PTR f;
1298 char *color;
1299 XColor *color_def;
1300 int alloc;
1302 register Lisp_Object tem;
1303 COLORREF w32_color_ref;
1305 tem = x_to_w32_color (color);
1307 if (!NILP (tem))
1309 if (f)
1311 /* Apply gamma correction. */
1312 w32_color_ref = XUINT (tem);
1313 gamma_correct (f, &w32_color_ref);
1314 XSETINT (tem, w32_color_ref);
1317 /* Map this color to the palette if it is enabled. */
1318 if (!NILP (Vw32_enable_palette))
1320 struct w32_palette_entry * entry =
1321 one_w32_display_info.color_list;
1322 struct w32_palette_entry ** prev =
1323 &one_w32_display_info.color_list;
1325 /* check if color is already mapped */
1326 while (entry)
1328 if (W32_COLOR (entry->entry) == XUINT (tem))
1329 break;
1330 prev = &entry->next;
1331 entry = entry->next;
1334 if (entry == NULL && alloc)
1336 /* not already mapped, so add to list */
1337 entry = (struct w32_palette_entry *)
1338 xmalloc (sizeof (struct w32_palette_entry));
1339 SET_W32_COLOR (entry->entry, XUINT (tem));
1340 entry->next = NULL;
1341 *prev = entry;
1342 one_w32_display_info.num_colors++;
1344 /* set flag that palette must be regenerated */
1345 one_w32_display_info.regen_palette = TRUE;
1348 /* Ensure COLORREF value is snapped to nearest color in (default)
1349 palette by simulating the PALETTERGB macro. This works whether
1350 or not the display device has a palette. */
1351 w32_color_ref = XUINT (tem) | 0x2000000;
1353 color_def->pixel = w32_color_ref;
1354 color_def->red = GetRValue (w32_color_ref) * 256;
1355 color_def->green = GetGValue (w32_color_ref) * 256;
1356 color_def->blue = GetBValue (w32_color_ref) * 256;
1358 return 1;
1360 else
1362 return 0;
1366 /* Given a string ARG naming a color, compute a pixel value from it
1367 suitable for screen F.
1368 If F is not a color screen, return DEF (default) regardless of what
1369 ARG says. */
1372 x_decode_color (f, arg, def)
1373 FRAME_PTR f;
1374 Lisp_Object arg;
1375 int def;
1377 XColor cdef;
1379 CHECK_STRING (arg);
1381 if (strcmp (SDATA (arg), "black") == 0)
1382 return BLACK_PIX_DEFAULT (f);
1383 else if (strcmp (SDATA (arg), "white") == 0)
1384 return WHITE_PIX_DEFAULT (f);
1386 if ((FRAME_W32_DISPLAY_INFO (f)->n_planes * FRAME_W32_DISPLAY_INFO (f)->n_cbits) == 1)
1387 return def;
1389 /* w32_defined_color is responsible for coping with failures
1390 by looking for a near-miss. */
1391 if (w32_defined_color (f, SDATA (arg), &cdef, 1))
1392 return cdef.pixel;
1394 /* defined_color failed; return an ultimate default. */
1395 return def;
1400 /* Functions called only from `x_set_frame_param'
1401 to set individual parameters.
1403 If FRAME_W32_WINDOW (f) is 0,
1404 the frame is being created and its window does not exist yet.
1405 In that case, just record the parameter's new value
1406 in the standard place; do not attempt to change the window. */
1408 void
1409 x_set_foreground_color (f, arg, oldval)
1410 struct frame *f;
1411 Lisp_Object arg, oldval;
1413 struct w32_output *x = f->output_data.w32;
1414 PIX_TYPE fg, old_fg;
1416 fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1417 old_fg = FRAME_FOREGROUND_PIXEL (f);
1418 FRAME_FOREGROUND_PIXEL (f) = fg;
1420 if (FRAME_W32_WINDOW (f) != 0)
1422 if (x->cursor_pixel == old_fg)
1423 x->cursor_pixel = fg;
1425 update_face_from_frame_parameter (f, Qforeground_color, arg);
1426 if (FRAME_VISIBLE_P (f))
1427 redraw_frame (f);
1431 void
1432 x_set_background_color (f, arg, oldval)
1433 struct frame *f;
1434 Lisp_Object arg, oldval;
1436 FRAME_BACKGROUND_PIXEL (f)
1437 = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
1439 if (FRAME_W32_WINDOW (f) != 0)
1441 SetWindowLong (FRAME_W32_WINDOW (f), WND_BACKGROUND_INDEX,
1442 FRAME_BACKGROUND_PIXEL (f));
1444 update_face_from_frame_parameter (f, Qbackground_color, arg);
1446 if (FRAME_VISIBLE_P (f))
1447 redraw_frame (f);
1451 void
1452 x_set_mouse_color (f, arg, oldval)
1453 struct frame *f;
1454 Lisp_Object arg, oldval;
1456 Cursor cursor, nontext_cursor, mode_cursor, hand_cursor;
1457 int count;
1458 int mask_color;
1460 if (!EQ (Qnil, arg))
1461 f->output_data.w32->mouse_pixel
1462 = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1463 mask_color = FRAME_BACKGROUND_PIXEL (f);
1465 /* Don't let pointers be invisible. */
1466 if (mask_color == f->output_data.w32->mouse_pixel
1467 && mask_color == FRAME_BACKGROUND_PIXEL (f))
1468 f->output_data.w32->mouse_pixel = FRAME_FOREGROUND_PIXEL (f);
1470 #if 0 /* TODO : cursor changes */
1471 BLOCK_INPUT;
1473 /* It's not okay to crash if the user selects a screwy cursor. */
1474 count = x_catch_errors (FRAME_W32_DISPLAY (f));
1476 if (!EQ (Qnil, Vx_pointer_shape))
1478 CHECK_NUMBER (Vx_pointer_shape);
1479 cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XINT (Vx_pointer_shape));
1481 else
1482 cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
1483 x_check_errors (FRAME_W32_DISPLAY (f), "bad text pointer cursor: %s");
1485 if (!EQ (Qnil, Vx_nontext_pointer_shape))
1487 CHECK_NUMBER (Vx_nontext_pointer_shape);
1488 nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1489 XINT (Vx_nontext_pointer_shape));
1491 else
1492 nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_left_ptr);
1493 x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
1495 if (!EQ (Qnil, Vx_hourglass_pointer_shape))
1497 CHECK_NUMBER (Vx_hourglass_pointer_shape);
1498 hourglass_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1499 XINT (Vx_hourglass_pointer_shape));
1501 else
1502 hourglass_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_watch);
1503 x_check_errors (FRAME_W32_DISPLAY (f), "bad busy pointer cursor: %s");
1505 x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
1506 if (!EQ (Qnil, Vx_mode_pointer_shape))
1508 CHECK_NUMBER (Vx_mode_pointer_shape);
1509 mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1510 XINT (Vx_mode_pointer_shape));
1512 else
1513 mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
1514 x_check_errors (FRAME_W32_DISPLAY (f), "bad modeline pointer cursor: %s");
1516 if (!EQ (Qnil, Vx_sensitive_text_pointer_shape))
1518 CHECK_NUMBER (Vx_sensitive_text_pointer_shape);
1519 hand_cursor
1520 = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1521 XINT (Vx_sensitive_text_pointer_shape));
1523 else
1524 hand_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_crosshair);
1526 if (!NILP (Vx_window_horizontal_drag_shape))
1528 CHECK_NUMBER (Vx_window_horizontal_drag_shape);
1529 horizontal_drag_cursor
1530 = XCreateFontCursor (FRAME_X_DISPLAY (f),
1531 XINT (Vx_window_horizontal_drag_shape));
1533 else
1534 horizontal_drag_cursor
1535 = XCreateFontCursor (FRAME_X_DISPLAY (f), XC_sb_h_double_arrow);
1537 /* Check and report errors with the above calls. */
1538 x_check_errors (FRAME_W32_DISPLAY (f), "can't set cursor shape: %s");
1539 x_uncatch_errors (FRAME_W32_DISPLAY (f), count);
1542 XColor fore_color, back_color;
1544 fore_color.pixel = f->output_data.w32->mouse_pixel;
1545 back_color.pixel = mask_color;
1546 XQueryColor (FRAME_W32_DISPLAY (f),
1547 DefaultColormap (FRAME_W32_DISPLAY (f),
1548 DefaultScreen (FRAME_W32_DISPLAY (f))),
1549 &fore_color);
1550 XQueryColor (FRAME_W32_DISPLAY (f),
1551 DefaultColormap (FRAME_W32_DISPLAY (f),
1552 DefaultScreen (FRAME_W32_DISPLAY (f))),
1553 &back_color);
1554 XRecolorCursor (FRAME_W32_DISPLAY (f), cursor,
1555 &fore_color, &back_color);
1556 XRecolorCursor (FRAME_W32_DISPLAY (f), nontext_cursor,
1557 &fore_color, &back_color);
1558 XRecolorCursor (FRAME_W32_DISPLAY (f), mode_cursor,
1559 &fore_color, &back_color);
1560 XRecolorCursor (FRAME_W32_DISPLAY (f), hand_cursor,
1561 &fore_color, &back_color);
1562 XRecolorCursor (FRAME_W32_DISPLAY (f), hourglass_cursor,
1563 &fore_color, &back_color);
1566 if (FRAME_W32_WINDOW (f) != 0)
1567 XDefineCursor (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), cursor);
1569 if (cursor != f->output_data.w32->text_cursor && f->output_data.w32->text_cursor != 0)
1570 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->text_cursor);
1571 f->output_data.w32->text_cursor = cursor;
1573 if (nontext_cursor != f->output_data.w32->nontext_cursor
1574 && f->output_data.w32->nontext_cursor != 0)
1575 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->nontext_cursor);
1576 f->output_data.w32->nontext_cursor = nontext_cursor;
1578 if (hourglass_cursor != f->output_data.w32->hourglass_cursor
1579 && f->output_data.w32->hourglass_cursor != 0)
1580 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->hourglass_cursor);
1581 f->output_data.w32->hourglass_cursor = hourglass_cursor;
1583 if (mode_cursor != f->output_data.w32->modeline_cursor
1584 && f->output_data.w32->modeline_cursor != 0)
1585 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->modeline_cursor);
1586 f->output_data.w32->modeline_cursor = mode_cursor;
1588 if (hand_cursor != f->output_data.w32->hand_cursor
1589 && f->output_data.w32->hand_cursor != 0)
1590 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->hand_cursor);
1591 f->output_data.w32->hand_cursor = hand_cursor;
1593 XFlush (FRAME_W32_DISPLAY (f));
1594 UNBLOCK_INPUT;
1596 update_face_from_frame_parameter (f, Qmouse_color, arg);
1597 #endif /* TODO */
1600 void
1601 x_set_cursor_color (f, arg, oldval)
1602 struct frame *f;
1603 Lisp_Object arg, oldval;
1605 unsigned long fore_pixel, pixel;
1607 if (!NILP (Vx_cursor_fore_pixel))
1608 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
1609 WHITE_PIX_DEFAULT (f));
1610 else
1611 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
1613 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1615 /* Make sure that the cursor color differs from the background color. */
1616 if (pixel == FRAME_BACKGROUND_PIXEL (f))
1618 pixel = f->output_data.w32->mouse_pixel;
1619 if (pixel == fore_pixel)
1620 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
1623 f->output_data.w32->cursor_foreground_pixel = fore_pixel;
1624 f->output_data.w32->cursor_pixel = pixel;
1626 if (FRAME_W32_WINDOW (f) != 0)
1628 BLOCK_INPUT;
1629 /* Update frame's cursor_gc. */
1630 f->output_data.w32->cursor_gc->foreground = fore_pixel;
1631 f->output_data.w32->cursor_gc->background = pixel;
1633 UNBLOCK_INPUT;
1635 if (FRAME_VISIBLE_P (f))
1637 x_update_cursor (f, 0);
1638 x_update_cursor (f, 1);
1642 update_face_from_frame_parameter (f, Qcursor_color, arg);
1645 /* Set the border-color of frame F to pixel value PIX.
1646 Note that this does not fully take effect if done before
1647 F has a window. */
1649 void
1650 x_set_border_pixel (f, pix)
1651 struct frame *f;
1652 int pix;
1655 f->output_data.w32->border_pixel = pix;
1657 if (FRAME_W32_WINDOW (f) != 0 && f->border_width > 0)
1659 if (FRAME_VISIBLE_P (f))
1660 redraw_frame (f);
1664 /* Set the border-color of frame F to value described by ARG.
1665 ARG can be a string naming a color.
1666 The border-color is used for the border that is drawn by the server.
1667 Note that this does not fully take effect if done before
1668 F has a window; it must be redone when the window is created. */
1670 void
1671 x_set_border_color (f, arg, oldval)
1672 struct frame *f;
1673 Lisp_Object arg, oldval;
1675 int pix;
1677 CHECK_STRING (arg);
1678 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1679 x_set_border_pixel (f, pix);
1680 update_face_from_frame_parameter (f, Qborder_color, arg);
1684 void
1685 x_set_cursor_type (f, arg, oldval)
1686 FRAME_PTR f;
1687 Lisp_Object arg, oldval;
1689 set_frame_cursor_types (f, arg);
1691 /* Make sure the cursor gets redrawn. */
1692 cursor_type_changed = 1;
1695 void
1696 x_set_icon_type (f, arg, oldval)
1697 struct frame *f;
1698 Lisp_Object arg, oldval;
1700 int result;
1702 if (NILP (arg) && NILP (oldval))
1703 return;
1705 if (STRINGP (arg) && STRINGP (oldval)
1706 && EQ (Fstring_equal (oldval, arg), Qt))
1707 return;
1709 if (SYMBOLP (arg) && SYMBOLP (oldval) && EQ (arg, oldval))
1710 return;
1712 BLOCK_INPUT;
1714 result = x_bitmap_icon (f, arg);
1715 if (result)
1717 UNBLOCK_INPUT;
1718 error ("No icon window available");
1721 UNBLOCK_INPUT;
1724 void
1725 x_set_icon_name (f, arg, oldval)
1726 struct frame *f;
1727 Lisp_Object arg, oldval;
1729 if (STRINGP (arg))
1731 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1732 return;
1734 else if (!NILP (arg) || NILP (oldval))
1735 return;
1737 f->icon_name = arg;
1739 #if 0
1740 if (f->output_data.w32->icon_bitmap != 0)
1741 return;
1743 BLOCK_INPUT;
1745 result = x_text_icon (f,
1746 (char *) SDATA ((!NILP (f->icon_name)
1747 ? f->icon_name
1748 : !NILP (f->title)
1749 ? f->title
1750 : f->name)));
1752 if (result)
1754 UNBLOCK_INPUT;
1755 error ("No icon window available");
1758 /* If the window was unmapped (and its icon was mapped),
1759 the new icon is not mapped, so map the window in its stead. */
1760 if (FRAME_VISIBLE_P (f))
1762 #ifdef USE_X_TOOLKIT
1763 XtPopup (f->output_data.w32->widget, XtGrabNone);
1764 #endif
1765 XMapWindow (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f));
1768 XFlush (FRAME_W32_DISPLAY (f));
1769 UNBLOCK_INPUT;
1770 #endif
1774 void
1775 x_set_menu_bar_lines (f, value, oldval)
1776 struct frame *f;
1777 Lisp_Object value, oldval;
1779 int nlines;
1780 int olines = FRAME_MENU_BAR_LINES (f);
1782 /* Right now, menu bars don't work properly in minibuf-only frames;
1783 most of the commands try to apply themselves to the minibuffer
1784 frame itself, and get an error because you can't switch buffers
1785 in or split the minibuffer window. */
1786 if (FRAME_MINIBUF_ONLY_P (f))
1787 return;
1789 if (INTEGERP (value))
1790 nlines = XINT (value);
1791 else
1792 nlines = 0;
1794 FRAME_MENU_BAR_LINES (f) = 0;
1795 if (nlines)
1796 FRAME_EXTERNAL_MENU_BAR (f) = 1;
1797 else
1799 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
1800 free_frame_menubar (f);
1801 FRAME_EXTERNAL_MENU_BAR (f) = 0;
1803 /* Adjust the frame size so that the client (text) dimensions
1804 remain the same. This depends on FRAME_EXTERNAL_MENU_BAR being
1805 set correctly. */
1806 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
1807 do_pending_window_change (0);
1809 adjust_glyphs (f);
1813 /* Set the number of lines used for the tool bar of frame F to VALUE.
1814 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
1815 is the old number of tool bar lines. This function changes the
1816 height of all windows on frame F to match the new tool bar height.
1817 The frame's height doesn't change. */
1819 void
1820 x_set_tool_bar_lines (f, value, oldval)
1821 struct frame *f;
1822 Lisp_Object value, oldval;
1824 int delta, nlines, root_height;
1825 Lisp_Object root_window;
1827 /* Treat tool bars like menu bars. */
1828 if (FRAME_MINIBUF_ONLY_P (f))
1829 return;
1831 /* Use VALUE only if an integer >= 0. */
1832 if (INTEGERP (value) && XINT (value) >= 0)
1833 nlines = XFASTINT (value);
1834 else
1835 nlines = 0;
1837 /* Make sure we redisplay all windows in this frame. */
1838 ++windows_or_buffers_changed;
1840 delta = nlines - FRAME_TOOL_BAR_LINES (f);
1842 /* Don't resize the tool-bar to more than we have room for. */
1843 root_window = FRAME_ROOT_WINDOW (f);
1844 root_height = WINDOW_TOTAL_LINES (XWINDOW (root_window));
1845 if (root_height - delta < 1)
1847 delta = root_height - 1;
1848 nlines = FRAME_TOOL_BAR_LINES (f) + delta;
1851 FRAME_TOOL_BAR_LINES (f) = nlines;
1852 change_window_heights (root_window, delta);
1853 adjust_glyphs (f);
1855 /* We also have to make sure that the internal border at the top of
1856 the frame, below the menu bar or tool bar, is redrawn when the
1857 tool bar disappears. This is so because the internal border is
1858 below the tool bar if one is displayed, but is below the menu bar
1859 if there isn't a tool bar. The tool bar draws into the area
1860 below the menu bar. */
1861 if (FRAME_W32_WINDOW (f) && FRAME_TOOL_BAR_LINES (f) == 0)
1863 clear_frame (f);
1864 clear_current_matrices (f);
1867 /* If the tool bar gets smaller, the internal border below it
1868 has to be cleared. It was formerly part of the display
1869 of the larger tool bar, and updating windows won't clear it. */
1870 if (delta < 0)
1872 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
1873 int width = FRAME_PIXEL_WIDTH (f);
1874 int y = nlines * FRAME_LINE_HEIGHT (f);
1876 BLOCK_INPUT;
1878 HDC hdc = get_frame_dc (f);
1879 w32_clear_area (f, hdc, 0, y, width, height);
1880 release_frame_dc (f, hdc);
1882 UNBLOCK_INPUT;
1884 if (WINDOWP (f->tool_bar_window))
1885 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
1890 /* Change the name of frame F to NAME. If NAME is nil, set F's name to
1891 w32_id_name.
1893 If EXPLICIT is non-zero, that indicates that lisp code is setting the
1894 name; if NAME is a string, set F's name to NAME and set
1895 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1897 If EXPLICIT is zero, that indicates that Emacs redisplay code is
1898 suggesting a new name, which lisp code should override; if
1899 F->explicit_name is set, ignore the new name; otherwise, set it. */
1901 void
1902 x_set_name (f, name, explicit)
1903 struct frame *f;
1904 Lisp_Object name;
1905 int explicit;
1907 /* Make sure that requests from lisp code override requests from
1908 Emacs redisplay code. */
1909 if (explicit)
1911 /* If we're switching from explicit to implicit, we had better
1912 update the mode lines and thereby update the title. */
1913 if (f->explicit_name && NILP (name))
1914 update_mode_lines = 1;
1916 f->explicit_name = ! NILP (name);
1918 else if (f->explicit_name)
1919 return;
1921 /* If NAME is nil, set the name to the w32_id_name. */
1922 if (NILP (name))
1924 /* Check for no change needed in this very common case
1925 before we do any consing. */
1926 if (!strcmp (FRAME_W32_DISPLAY_INFO (f)->w32_id_name,
1927 SDATA (f->name)))
1928 return;
1929 name = build_string (FRAME_W32_DISPLAY_INFO (f)->w32_id_name);
1931 else
1932 CHECK_STRING (name);
1934 /* Don't change the name if it's already NAME. */
1935 if (! NILP (Fstring_equal (name, f->name)))
1936 return;
1938 f->name = name;
1940 /* For setting the frame title, the title parameter should override
1941 the name parameter. */
1942 if (! NILP (f->title))
1943 name = f->title;
1945 if (FRAME_W32_WINDOW (f))
1947 if (STRING_MULTIBYTE (name))
1948 name = ENCODE_SYSTEM (name);
1950 BLOCK_INPUT;
1951 SetWindowText (FRAME_W32_WINDOW (f), SDATA (name));
1952 UNBLOCK_INPUT;
1956 /* This function should be called when the user's lisp code has
1957 specified a name for the frame; the name will override any set by the
1958 redisplay code. */
1959 void
1960 x_explicitly_set_name (f, arg, oldval)
1961 FRAME_PTR f;
1962 Lisp_Object arg, oldval;
1964 x_set_name (f, arg, 1);
1967 /* This function should be called by Emacs redisplay code to set the
1968 name; names set this way will never override names set by the user's
1969 lisp code. */
1970 void
1971 x_implicitly_set_name (f, arg, oldval)
1972 FRAME_PTR f;
1973 Lisp_Object arg, oldval;
1975 x_set_name (f, arg, 0);
1978 /* Change the title of frame F to NAME.
1979 If NAME is nil, use the frame name as the title. */
1981 void
1982 x_set_title (f, name, old_name)
1983 struct frame *f;
1984 Lisp_Object name, old_name;
1986 /* Don't change the title if it's already NAME. */
1987 if (EQ (name, f->title))
1988 return;
1990 update_mode_lines = 1;
1992 f->title = name;
1994 if (NILP (name))
1995 name = f->name;
1997 if (FRAME_W32_WINDOW (f))
1999 if (STRING_MULTIBYTE (name))
2000 name = ENCODE_SYSTEM (name);
2002 BLOCK_INPUT;
2003 SetWindowText (FRAME_W32_WINDOW (f), SDATA (name));
2004 UNBLOCK_INPUT;
2009 void x_set_scroll_bar_default_width (f)
2010 struct frame *f;
2012 int wid = FRAME_COLUMN_WIDTH (f);
2014 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = GetSystemMetrics (SM_CXVSCROLL);
2015 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) +
2016 wid - 1) / wid;
2020 /* Subroutines of creating a frame. */
2023 /* Return the value of parameter PARAM.
2025 First search ALIST, then Vdefault_frame_alist, then the X defaults
2026 database, using ATTRIBUTE as the attribute name and CLASS as its class.
2028 Convert the resource to the type specified by desired_type.
2030 If no default is specified, return Qunbound. If you call
2031 w32_get_arg, make sure you deal with Qunbound in a reasonable way,
2032 and don't let it get stored in any Lisp-visible variables! */
2034 static Lisp_Object
2035 w32_get_arg (alist, param, attribute, class, type)
2036 Lisp_Object alist, param;
2037 char *attribute;
2038 char *class;
2039 enum resource_types type;
2041 return x_get_arg (check_x_display_info (Qnil),
2042 alist, param, attribute, class, type);
2046 Cursor
2047 w32_load_cursor (LPCTSTR name)
2049 /* Try first to load cursor from application resource. */
2050 Cursor cursor = LoadImage ((HINSTANCE) GetModuleHandle (NULL),
2051 name, IMAGE_CURSOR, 0, 0,
2052 LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_SHARED);
2053 if (!cursor)
2055 /* Then try to load a shared predefined cursor. */
2056 cursor = LoadImage (NULL, name, IMAGE_CURSOR, 0, 0,
2057 LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_SHARED);
2059 return cursor;
2062 extern LRESULT CALLBACK w32_wnd_proc ();
2064 static BOOL
2065 w32_init_class (hinst)
2066 HINSTANCE hinst;
2068 WNDCLASS wc;
2070 wc.style = CS_HREDRAW | CS_VREDRAW;
2071 wc.lpfnWndProc = (WNDPROC) w32_wnd_proc;
2072 wc.cbClsExtra = 0;
2073 wc.cbWndExtra = WND_EXTRA_BYTES;
2074 wc.hInstance = hinst;
2075 wc.hIcon = LoadIcon (hinst, EMACS_CLASS);
2076 wc.hCursor = w32_load_cursor (IDC_ARROW);
2077 wc.hbrBackground = NULL; /* GetStockObject (WHITE_BRUSH); */
2078 wc.lpszMenuName = NULL;
2079 wc.lpszClassName = EMACS_CLASS;
2081 return (RegisterClass (&wc));
2084 static HWND
2085 w32_createscrollbar (f, bar)
2086 struct frame *f;
2087 struct scroll_bar * bar;
2089 return (CreateWindow ("SCROLLBAR", "", SBS_VERT | WS_CHILD | WS_VISIBLE,
2090 /* Position and size of scroll bar. */
2091 XINT (bar->left) + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
2092 XINT (bar->top),
2093 XINT (bar->width) - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
2094 XINT (bar->height),
2095 FRAME_W32_WINDOW (f),
2096 NULL,
2097 hinst,
2098 NULL));
2101 static void
2102 w32_createwindow (f)
2103 struct frame *f;
2105 HWND hwnd;
2106 RECT rect;
2107 Lisp_Object top = Qunbound;
2108 Lisp_Object left = Qunbound;
2110 rect.left = rect.top = 0;
2111 rect.right = FRAME_PIXEL_WIDTH (f);
2112 rect.bottom = FRAME_PIXEL_HEIGHT (f);
2114 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
2115 FRAME_EXTERNAL_MENU_BAR (f));
2117 /* Do first time app init */
2119 if (!hprevinst)
2121 w32_init_class (hinst);
2124 if (f->size_hint_flags & USPosition || f->size_hint_flags & PPosition)
2126 XSETINT (left, f->left_pos);
2127 XSETINT (top, f->top_pos);
2129 else if (EQ (left, Qunbound) && EQ (top, Qunbound))
2131 /* When called with RES_TYPE_NUMBER, w32_get_arg will return zero
2132 for anything that is not a number and is not Qunbound. */
2133 left = w32_get_arg (Qnil, Qleft, "left", "Left", RES_TYPE_NUMBER);
2134 top = w32_get_arg (Qnil, Qtop, "top", "Top", RES_TYPE_NUMBER);
2137 FRAME_W32_WINDOW (f) = hwnd
2138 = CreateWindow (EMACS_CLASS,
2139 f->namebuf,
2140 f->output_data.w32->dwStyle | WS_CLIPCHILDREN,
2141 EQ (left, Qunbound) ? CW_USEDEFAULT : XINT (left),
2142 EQ (top, Qunbound) ? CW_USEDEFAULT : XINT (top),
2143 rect.right - rect.left,
2144 rect.bottom - rect.top,
2145 NULL,
2146 NULL,
2147 hinst,
2148 NULL);
2150 if (hwnd)
2152 SetWindowLong (hwnd, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
2153 SetWindowLong (hwnd, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
2154 SetWindowLong (hwnd, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
2155 SetWindowLong (hwnd, WND_SCROLLBAR_INDEX, f->scroll_bar_actual_width);
2156 SetWindowLong (hwnd, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
2158 /* Enable drag-n-drop. */
2159 DragAcceptFiles (hwnd, TRUE);
2161 /* Do this to discard the default setting specified by our parent. */
2162 ShowWindow (hwnd, SW_HIDE);
2164 /* Update frame positions. */
2165 GetWindowRect (hwnd, &rect);
2166 f->left_pos = rect.left;
2167 f->top_pos = rect.top;
2171 static void
2172 my_post_msg (wmsg, hwnd, msg, wParam, lParam)
2173 W32Msg * wmsg;
2174 HWND hwnd;
2175 UINT msg;
2176 WPARAM wParam;
2177 LPARAM lParam;
2179 wmsg->msg.hwnd = hwnd;
2180 wmsg->msg.message = msg;
2181 wmsg->msg.wParam = wParam;
2182 wmsg->msg.lParam = lParam;
2183 wmsg->msg.time = GetMessageTime ();
2185 post_msg (wmsg);
2188 /* GetKeyState and MapVirtualKey on Windows 95 do not actually distinguish
2189 between left and right keys as advertised. We test for this
2190 support dynamically, and set a flag when the support is absent. If
2191 absent, we keep track of the left and right control and alt keys
2192 ourselves. This is particularly necessary on keyboards that rely
2193 upon the AltGr key, which is represented as having the left control
2194 and right alt keys pressed. For these keyboards, we need to know
2195 when the left alt key has been pressed in addition to the AltGr key
2196 so that we can properly support M-AltGr-key sequences (such as M-@
2197 on Swedish keyboards). */
2199 #define EMACS_LCONTROL 0
2200 #define EMACS_RCONTROL 1
2201 #define EMACS_LMENU 2
2202 #define EMACS_RMENU 3
2204 static int modifiers[4];
2205 static int modifiers_recorded;
2206 static int modifier_key_support_tested;
2208 static void
2209 test_modifier_support (unsigned int wparam)
2211 unsigned int l, r;
2213 if (wparam != VK_CONTROL && wparam != VK_MENU)
2214 return;
2215 if (wparam == VK_CONTROL)
2217 l = VK_LCONTROL;
2218 r = VK_RCONTROL;
2220 else
2222 l = VK_LMENU;
2223 r = VK_RMENU;
2225 if (!(GetKeyState (l) & 0x8000) && !(GetKeyState (r) & 0x8000))
2226 modifiers_recorded = 1;
2227 else
2228 modifiers_recorded = 0;
2229 modifier_key_support_tested = 1;
2232 static void
2233 record_keydown (unsigned int wparam, unsigned int lparam)
2235 int i;
2237 if (!modifier_key_support_tested)
2238 test_modifier_support (wparam);
2240 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
2241 return;
2243 if (wparam == VK_CONTROL)
2244 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
2245 else
2246 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
2248 modifiers[i] = 1;
2251 static void
2252 record_keyup (unsigned int wparam, unsigned int lparam)
2254 int i;
2256 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
2257 return;
2259 if (wparam == VK_CONTROL)
2260 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
2261 else
2262 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
2264 modifiers[i] = 0;
2267 /* Emacs can lose focus while a modifier key has been pressed. When
2268 it regains focus, be conservative and clear all modifiers since
2269 we cannot reconstruct the left and right modifier state. */
2270 static void
2271 reset_modifiers ()
2273 SHORT ctrl, alt;
2275 if (GetFocus () == NULL)
2276 /* Emacs doesn't have keyboard focus. Do nothing. */
2277 return;
2279 ctrl = GetAsyncKeyState (VK_CONTROL);
2280 alt = GetAsyncKeyState (VK_MENU);
2282 if (!(ctrl & 0x08000))
2283 /* Clear any recorded control modifier state. */
2284 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
2286 if (!(alt & 0x08000))
2287 /* Clear any recorded alt modifier state. */
2288 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
2290 /* Update the state of all modifier keys, because modifiers used in
2291 hot-key combinations can get stuck on if Emacs loses focus as a
2292 result of a hot-key being pressed. */
2294 BYTE keystate[256];
2296 #define CURRENT_STATE(key) ((GetAsyncKeyState (key) & 0x8000) >> 8)
2298 GetKeyboardState (keystate);
2299 keystate[VK_SHIFT] = CURRENT_STATE (VK_SHIFT);
2300 keystate[VK_CONTROL] = CURRENT_STATE (VK_CONTROL);
2301 keystate[VK_LCONTROL] = CURRENT_STATE (VK_LCONTROL);
2302 keystate[VK_RCONTROL] = CURRENT_STATE (VK_RCONTROL);
2303 keystate[VK_MENU] = CURRENT_STATE (VK_MENU);
2304 keystate[VK_LMENU] = CURRENT_STATE (VK_LMENU);
2305 keystate[VK_RMENU] = CURRENT_STATE (VK_RMENU);
2306 keystate[VK_LWIN] = CURRENT_STATE (VK_LWIN);
2307 keystate[VK_RWIN] = CURRENT_STATE (VK_RWIN);
2308 keystate[VK_APPS] = CURRENT_STATE (VK_APPS);
2309 SetKeyboardState (keystate);
2313 /* Synchronize modifier state with what is reported with the current
2314 keystroke. Even if we cannot distinguish between left and right
2315 modifier keys, we know that, if no modifiers are set, then neither
2316 the left or right modifier should be set. */
2317 static void
2318 sync_modifiers ()
2320 if (!modifiers_recorded)
2321 return;
2323 if (!(GetKeyState (VK_CONTROL) & 0x8000))
2324 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
2326 if (!(GetKeyState (VK_MENU) & 0x8000))
2327 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
2330 static int
2331 modifier_set (int vkey)
2333 if (vkey == VK_CAPITAL || vkey == VK_SCROLL)
2334 return (GetKeyState (vkey) & 0x1);
2335 if (!modifiers_recorded)
2336 return (GetKeyState (vkey) & 0x8000);
2338 switch (vkey)
2340 case VK_LCONTROL:
2341 return modifiers[EMACS_LCONTROL];
2342 case VK_RCONTROL:
2343 return modifiers[EMACS_RCONTROL];
2344 case VK_LMENU:
2345 return modifiers[EMACS_LMENU];
2346 case VK_RMENU:
2347 return modifiers[EMACS_RMENU];
2349 return (GetKeyState (vkey) & 0x8000);
2352 /* Convert between the modifier bits W32 uses and the modifier bits
2353 Emacs uses. */
2355 unsigned int
2356 w32_key_to_modifier (int key)
2358 Lisp_Object key_mapping;
2360 switch (key)
2362 case VK_LWIN:
2363 key_mapping = Vw32_lwindow_modifier;
2364 break;
2365 case VK_RWIN:
2366 key_mapping = Vw32_rwindow_modifier;
2367 break;
2368 case VK_APPS:
2369 key_mapping = Vw32_apps_modifier;
2370 break;
2371 case VK_SCROLL:
2372 key_mapping = Vw32_scroll_lock_modifier;
2373 break;
2374 default:
2375 key_mapping = Qnil;
2378 /* NB. This code runs in the input thread, asychronously to the lisp
2379 thread, so we must be careful to ensure access to lisp data is
2380 thread-safe. The following code is safe because the modifier
2381 variable values are updated atomically from lisp and symbols are
2382 not relocated by GC. Also, we don't have to worry about seeing GC
2383 markbits here. */
2384 if (EQ (key_mapping, Qhyper))
2385 return hyper_modifier;
2386 if (EQ (key_mapping, Qsuper))
2387 return super_modifier;
2388 if (EQ (key_mapping, Qmeta))
2389 return meta_modifier;
2390 if (EQ (key_mapping, Qalt))
2391 return alt_modifier;
2392 if (EQ (key_mapping, Qctrl))
2393 return ctrl_modifier;
2394 if (EQ (key_mapping, Qcontrol)) /* synonym for ctrl */
2395 return ctrl_modifier;
2396 if (EQ (key_mapping, Qshift))
2397 return shift_modifier;
2399 /* Don't generate any modifier if not explicitly requested. */
2400 return 0;
2403 static unsigned int
2404 w32_get_modifiers ()
2406 return ((modifier_set (VK_SHIFT) ? shift_modifier : 0) |
2407 (modifier_set (VK_CONTROL) ? ctrl_modifier : 0) |
2408 (modifier_set (VK_LWIN) ? w32_key_to_modifier (VK_LWIN) : 0) |
2409 (modifier_set (VK_RWIN) ? w32_key_to_modifier (VK_RWIN) : 0) |
2410 (modifier_set (VK_APPS) ? w32_key_to_modifier (VK_APPS) : 0) |
2411 (modifier_set (VK_SCROLL) ? w32_key_to_modifier (VK_SCROLL) : 0) |
2412 (modifier_set (VK_MENU) ?
2413 ((NILP (Vw32_alt_is_meta)) ? alt_modifier : meta_modifier) : 0));
2416 /* We map the VK_* modifiers into console modifier constants
2417 so that we can use the same routines to handle both console
2418 and window input. */
2420 static int
2421 construct_console_modifiers ()
2423 int mods;
2425 mods = 0;
2426 mods |= (modifier_set (VK_SHIFT)) ? SHIFT_PRESSED : 0;
2427 mods |= (modifier_set (VK_CAPITAL)) ? CAPSLOCK_ON : 0;
2428 mods |= (modifier_set (VK_SCROLL)) ? SCROLLLOCK_ON : 0;
2429 mods |= (modifier_set (VK_NUMLOCK)) ? NUMLOCK_ON : 0;
2430 mods |= (modifier_set (VK_LCONTROL)) ? LEFT_CTRL_PRESSED : 0;
2431 mods |= (modifier_set (VK_RCONTROL)) ? RIGHT_CTRL_PRESSED : 0;
2432 mods |= (modifier_set (VK_LMENU)) ? LEFT_ALT_PRESSED : 0;
2433 mods |= (modifier_set (VK_RMENU)) ? RIGHT_ALT_PRESSED : 0;
2434 mods |= (modifier_set (VK_LWIN)) ? LEFT_WIN_PRESSED : 0;
2435 mods |= (modifier_set (VK_RWIN)) ? RIGHT_WIN_PRESSED : 0;
2436 mods |= (modifier_set (VK_APPS)) ? APPS_PRESSED : 0;
2438 return mods;
2441 static int
2442 w32_get_key_modifiers (unsigned int wparam, unsigned int lparam)
2444 int mods;
2446 /* Convert to emacs modifiers. */
2447 mods = w32_kbd_mods_to_emacs (construct_console_modifiers (), wparam);
2449 return mods;
2452 unsigned int
2453 map_keypad_keys (unsigned int virt_key, unsigned int extended)
2455 if (virt_key < VK_CLEAR || virt_key > VK_DELETE)
2456 return virt_key;
2458 if (virt_key == VK_RETURN)
2459 return (extended ? VK_NUMPAD_ENTER : VK_RETURN);
2461 if (virt_key >= VK_PRIOR && virt_key <= VK_DOWN)
2462 return (!extended ? (VK_NUMPAD_PRIOR + (virt_key - VK_PRIOR)) : virt_key);
2464 if (virt_key == VK_INSERT || virt_key == VK_DELETE)
2465 return (!extended ? (VK_NUMPAD_INSERT + (virt_key - VK_INSERT)) : virt_key);
2467 if (virt_key == VK_CLEAR)
2468 return (!extended ? VK_NUMPAD_CLEAR : virt_key);
2470 return virt_key;
2473 /* List of special key combinations which w32 would normally capture,
2474 but Emacs should grab instead. Not directly visible to lisp, to
2475 simplify synchronization. Each item is an integer encoding a virtual
2476 key code and modifier combination to capture. */
2477 static Lisp_Object w32_grabbed_keys;
2479 #define HOTKEY(vk, mods) make_number (((vk) & 255) | ((mods) << 8))
2480 #define HOTKEY_ID(k) (XFASTINT (k) & 0xbfff)
2481 #define HOTKEY_VK_CODE(k) (XFASTINT (k) & 255)
2482 #define HOTKEY_MODIFIERS(k) (XFASTINT (k) >> 8)
2484 #define RAW_HOTKEY_ID(k) ((k) & 0xbfff)
2485 #define RAW_HOTKEY_VK_CODE(k) ((k) & 255)
2486 #define RAW_HOTKEY_MODIFIERS(k) ((k) >> 8)
2488 /* Register hot-keys for reserved key combinations when Emacs has
2489 keyboard focus, since this is the only way Emacs can receive key
2490 combinations like Alt-Tab which are used by the system. */
2492 static void
2493 register_hot_keys (hwnd)
2494 HWND hwnd;
2496 Lisp_Object keylist;
2498 /* Use CONSP, since we are called asynchronously. */
2499 for (keylist = w32_grabbed_keys; CONSP (keylist); keylist = XCDR (keylist))
2501 Lisp_Object key = XCAR (keylist);
2503 /* Deleted entries get set to nil. */
2504 if (!INTEGERP (key))
2505 continue;
2507 RegisterHotKey (hwnd, HOTKEY_ID (key),
2508 HOTKEY_MODIFIERS (key), HOTKEY_VK_CODE (key));
2512 static void
2513 unregister_hot_keys (hwnd)
2514 HWND hwnd;
2516 Lisp_Object keylist;
2518 for (keylist = w32_grabbed_keys; CONSP (keylist); keylist = XCDR (keylist))
2520 Lisp_Object key = XCAR (keylist);
2522 if (!INTEGERP (key))
2523 continue;
2525 UnregisterHotKey (hwnd, HOTKEY_ID (key));
2529 /* Main message dispatch loop. */
2531 static void
2532 w32_msg_pump (deferred_msg * msg_buf)
2534 MSG msg;
2535 int result;
2536 HWND focus_window;
2538 msh_mousewheel = RegisterWindowMessage (MSH_MOUSEWHEEL);
2540 while (GetMessage (&msg, NULL, 0, 0))
2542 if (msg.hwnd == NULL)
2544 switch (msg.message)
2546 case WM_NULL:
2547 /* Produced by complete_deferred_msg; just ignore. */
2548 break;
2549 case WM_EMACS_CREATEWINDOW:
2550 /* Initialize COM for this window. Even though we don't use it,
2551 some third party shell extensions can cause it to be used in
2552 system dialogs, which causes a crash if it is not initialized.
2553 This is a known bug in Windows, which was fixed long ago, but
2554 the patch for XP is not publically available until XP SP3,
2555 and older versions will never be patched. */
2556 CoInitialize (NULL);
2557 w32_createwindow ((struct frame *) msg.wParam);
2558 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2559 abort ();
2560 break;
2561 case WM_EMACS_SETLOCALE:
2562 SetThreadLocale (msg.wParam);
2563 /* Reply is not expected. */
2564 break;
2565 case WM_EMACS_SETKEYBOARDLAYOUT:
2566 result = (int) ActivateKeyboardLayout ((HKL) msg.wParam, 0);
2567 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
2568 result, 0))
2569 abort ();
2570 break;
2571 case WM_EMACS_REGISTER_HOT_KEY:
2572 focus_window = GetFocus ();
2573 if (focus_window != NULL)
2574 RegisterHotKey (focus_window,
2575 RAW_HOTKEY_ID (msg.wParam),
2576 RAW_HOTKEY_MODIFIERS (msg.wParam),
2577 RAW_HOTKEY_VK_CODE (msg.wParam));
2578 /* Reply is not expected. */
2579 break;
2580 case WM_EMACS_UNREGISTER_HOT_KEY:
2581 focus_window = GetFocus ();
2582 if (focus_window != NULL)
2583 UnregisterHotKey (focus_window, RAW_HOTKEY_ID (msg.wParam));
2584 /* Mark item as erased. NB: this code must be
2585 thread-safe. The next line is okay because the cons
2586 cell is never made into garbage and is not relocated by
2587 GC. */
2588 XSETCAR ((Lisp_Object) ((EMACS_INT) msg.lParam), Qnil);
2589 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2590 abort ();
2591 break;
2592 case WM_EMACS_TOGGLE_LOCK_KEY:
2594 int vk_code = (int) msg.wParam;
2595 int cur_state = (GetKeyState (vk_code) & 1);
2596 Lisp_Object new_state = (Lisp_Object) ((EMACS_INT) msg.lParam);
2598 /* NB: This code must be thread-safe. It is safe to
2599 call NILP because symbols are not relocated by GC,
2600 and pointer here is not touched by GC (so the markbit
2601 can't be set). Numbers are safe because they are
2602 immediate values. */
2603 if (NILP (new_state)
2604 || (NUMBERP (new_state)
2605 && ((XUINT (new_state)) & 1) != cur_state))
2607 one_w32_display_info.faked_key = vk_code;
2609 keybd_event ((BYTE) vk_code,
2610 (BYTE) MapVirtualKey (vk_code, 0),
2611 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2612 keybd_event ((BYTE) vk_code,
2613 (BYTE) MapVirtualKey (vk_code, 0),
2614 KEYEVENTF_EXTENDEDKEY | 0, 0);
2615 keybd_event ((BYTE) vk_code,
2616 (BYTE) MapVirtualKey (vk_code, 0),
2617 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2618 cur_state = !cur_state;
2620 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
2621 cur_state, 0))
2622 abort ();
2624 break;
2625 #ifdef MSG_DEBUG
2626 /* Broadcast messages make it here, so you need to be looking
2627 for something in particular for this to be useful. */
2628 default:
2629 DebPrint (("msg %x not expected by w32_msg_pump\n", msg.message));
2630 #endif
2633 else
2635 DispatchMessage (&msg);
2638 /* Exit nested loop when our deferred message has completed. */
2639 if (msg_buf->completed)
2640 break;
2644 deferred_msg * deferred_msg_head;
2646 static deferred_msg *
2647 find_deferred_msg (HWND hwnd, UINT msg)
2649 deferred_msg * item;
2651 /* Don't actually need synchronization for read access, since
2652 modification of single pointer is always atomic. */
2653 /* enter_crit (); */
2655 for (item = deferred_msg_head; item != NULL; item = item->next)
2656 if (item->w32msg.msg.hwnd == hwnd
2657 && item->w32msg.msg.message == msg)
2658 break;
2660 /* leave_crit (); */
2662 return item;
2665 static LRESULT
2666 send_deferred_msg (deferred_msg * msg_buf,
2667 HWND hwnd,
2668 UINT msg,
2669 WPARAM wParam,
2670 LPARAM lParam)
2672 /* Only input thread can send deferred messages. */
2673 if (GetCurrentThreadId () != dwWindowsThreadId)
2674 abort ();
2676 /* It is an error to send a message that is already deferred. */
2677 if (find_deferred_msg (hwnd, msg) != NULL)
2678 abort ();
2680 /* Enforced synchronization is not needed because this is the only
2681 function that alters deferred_msg_head, and the following critical
2682 section is guaranteed to only be serially reentered (since only the
2683 input thread can call us). */
2685 /* enter_crit (); */
2687 msg_buf->completed = 0;
2688 msg_buf->next = deferred_msg_head;
2689 deferred_msg_head = msg_buf;
2690 my_post_msg (&msg_buf->w32msg, hwnd, msg, wParam, lParam);
2692 /* leave_crit (); */
2694 /* Start a new nested message loop to process other messages until
2695 this one is completed. */
2696 w32_msg_pump (msg_buf);
2698 deferred_msg_head = msg_buf->next;
2700 return msg_buf->result;
2703 void
2704 complete_deferred_msg (HWND hwnd, UINT msg, LRESULT result)
2706 deferred_msg * msg_buf = find_deferred_msg (hwnd, msg);
2708 if (msg_buf == NULL)
2709 /* Message may have been cancelled, so don't abort. */
2710 return;
2712 msg_buf->result = result;
2713 msg_buf->completed = 1;
2715 /* Ensure input thread is woken so it notices the completion. */
2716 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
2719 static void
2720 cancel_all_deferred_msgs ()
2722 deferred_msg * item;
2724 /* Don't actually need synchronization for read access, since
2725 modification of single pointer is always atomic. */
2726 /* enter_crit (); */
2728 for (item = deferred_msg_head; item != NULL; item = item->next)
2730 item->result = 0;
2731 item->completed = 1;
2734 /* leave_crit (); */
2736 /* Ensure input thread is woken so it notices the completion. */
2737 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
2740 DWORD WINAPI
2741 w32_msg_worker (void *arg)
2743 MSG msg;
2744 deferred_msg dummy_buf;
2746 /* Ensure our message queue is created */
2748 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
2750 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2751 abort ();
2753 memset (&dummy_buf, 0, sizeof (dummy_buf));
2754 dummy_buf.w32msg.msg.hwnd = NULL;
2755 dummy_buf.w32msg.msg.message = WM_NULL;
2757 /* This is the initial message loop which should only exit when the
2758 application quits. */
2759 w32_msg_pump (&dummy_buf);
2761 return 0;
2764 static void
2765 signal_user_input ()
2767 /* Interrupt any lisp that wants to be interrupted by input. */
2768 if (!NILP (Vthrow_on_input))
2770 Vquit_flag = Vthrow_on_input;
2771 /* If we're inside a function that wants immediate quits,
2772 do it now. */
2773 if (immediate_quit && NILP (Vinhibit_quit))
2775 immediate_quit = 0;
2776 QUIT;
2782 static void
2783 post_character_message (hwnd, msg, wParam, lParam, modifiers)
2784 HWND hwnd;
2785 UINT msg;
2786 WPARAM wParam;
2787 LPARAM lParam;
2788 DWORD modifiers;
2791 W32Msg wmsg;
2793 wmsg.dwModifiers = modifiers;
2795 /* Detect quit_char and set quit-flag directly. Note that we
2796 still need to post a message to ensure the main thread will be
2797 woken up if blocked in sys_select, but we do NOT want to post
2798 the quit_char message itself (because it will usually be as if
2799 the user had typed quit_char twice). Instead, we post a dummy
2800 message that has no particular effect. */
2802 int c = wParam;
2803 if (isalpha (c) && wmsg.dwModifiers == ctrl_modifier)
2804 c = make_ctrl_char (c) & 0377;
2805 if (c == quit_char
2806 || (wmsg.dwModifiers == 0 &&
2807 w32_quit_key && wParam == w32_quit_key))
2809 Vquit_flag = Qt;
2811 /* The choice of message is somewhat arbitrary, as long as
2812 the main thread handler just ignores it. */
2813 msg = WM_NULL;
2815 /* Interrupt any blocking system calls. */
2816 signal_quit ();
2818 /* As a safety precaution, forcibly complete any deferred
2819 messages. This is a kludge, but I don't see any particularly
2820 clean way to handle the situation where a deferred message is
2821 "dropped" in the lisp thread, and will thus never be
2822 completed, eg. by the user trying to activate the menubar
2823 when the lisp thread is busy, and then typing C-g when the
2824 menubar doesn't open promptly (with the result that the
2825 menubar never responds at all because the deferred
2826 WM_INITMENU message is never completed). Another problem
2827 situation is when the lisp thread calls SendMessage (to send
2828 a window manager command) when a message has been deferred;
2829 the lisp thread gets blocked indefinitely waiting for the
2830 deferred message to be completed, which itself is waiting for
2831 the lisp thread to respond.
2833 Note that we don't want to block the input thread waiting for
2834 a reponse from the lisp thread (although that would at least
2835 solve the deadlock problem above), because we want to be able
2836 to receive C-g to interrupt the lisp thread. */
2837 cancel_all_deferred_msgs ();
2839 else
2840 signal_user_input ();
2843 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2846 /* Main window procedure */
2848 LRESULT CALLBACK
2849 w32_wnd_proc (hwnd, msg, wParam, lParam)
2850 HWND hwnd;
2851 UINT msg;
2852 WPARAM wParam;
2853 LPARAM lParam;
2855 struct frame *f;
2856 struct w32_display_info *dpyinfo = &one_w32_display_info;
2857 W32Msg wmsg;
2858 int windows_translate;
2859 int key;
2861 /* Note that it is okay to call x_window_to_frame, even though we are
2862 not running in the main lisp thread, because frame deletion
2863 requires the lisp thread to synchronize with this thread. Thus, if
2864 a frame struct is returned, it can be used without concern that the
2865 lisp thread might make it disappear while we are using it.
2867 NB. Walking the frame list in this thread is safe (as long as
2868 writes of Lisp_Object slots are atomic, which they are on Windows).
2869 Although delete-frame can destructively modify the frame list while
2870 we are walking it, a garbage collection cannot occur until after
2871 delete-frame has synchronized with this thread.
2873 It is also safe to use functions that make GDI calls, such as
2874 w32_clear_rect, because these functions must obtain a DC handle
2875 from the frame struct using get_frame_dc which is thread-aware. */
2877 switch (msg)
2879 case WM_ERASEBKGND:
2880 f = x_window_to_frame (dpyinfo, hwnd);
2881 if (f)
2883 HDC hdc = get_frame_dc (f);
2884 GetUpdateRect (hwnd, &wmsg.rect, FALSE);
2885 w32_clear_rect (f, hdc, &wmsg.rect);
2886 release_frame_dc (f, hdc);
2888 #if defined (W32_DEBUG_DISPLAY)
2889 DebPrint (("WM_ERASEBKGND (frame %p): erasing %d,%d-%d,%d\n",
2891 wmsg.rect.left, wmsg.rect.top,
2892 wmsg.rect.right, wmsg.rect.bottom));
2893 #endif /* W32_DEBUG_DISPLAY */
2895 return 1;
2896 case WM_PALETTECHANGED:
2897 /* ignore our own changes */
2898 if ((HWND)wParam != hwnd)
2900 f = x_window_to_frame (dpyinfo, hwnd);
2901 if (f)
2902 /* get_frame_dc will realize our palette and force all
2903 frames to be redrawn if needed. */
2904 release_frame_dc (f, get_frame_dc (f));
2906 return 0;
2907 case WM_PAINT:
2909 PAINTSTRUCT paintStruct;
2910 RECT update_rect;
2911 bzero (&update_rect, sizeof (update_rect));
2913 f = x_window_to_frame (dpyinfo, hwnd);
2914 if (f == 0)
2916 DebPrint (("WM_PAINT received for unknown window %p\n", hwnd));
2917 return 0;
2920 /* MSDN Docs say not to call BeginPaint if GetUpdateRect
2921 fails. Apparently this can happen under some
2922 circumstances. */
2923 if (GetUpdateRect (hwnd, &update_rect, FALSE) || !w32_strict_painting)
2925 enter_crit ();
2926 BeginPaint (hwnd, &paintStruct);
2928 /* The rectangles returned by GetUpdateRect and BeginPaint
2929 do not always match. Play it safe by assuming both areas
2930 are invalid. */
2931 UnionRect (&(wmsg.rect), &update_rect, &(paintStruct.rcPaint));
2933 #if defined (W32_DEBUG_DISPLAY)
2934 DebPrint (("WM_PAINT (frame %p): painting %d,%d-%d,%d\n",
2936 wmsg.rect.left, wmsg.rect.top,
2937 wmsg.rect.right, wmsg.rect.bottom));
2938 DebPrint ((" [update region is %d,%d-%d,%d]\n",
2939 update_rect.left, update_rect.top,
2940 update_rect.right, update_rect.bottom));
2941 #endif
2942 EndPaint (hwnd, &paintStruct);
2943 leave_crit ();
2945 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2947 return 0;
2950 /* If GetUpdateRect returns 0 (meaning there is no update
2951 region), assume the whole window needs to be repainted. */
2952 GetClientRect (hwnd, &wmsg.rect);
2953 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2954 return 0;
2957 case WM_INPUTLANGCHANGE:
2958 /* Inform lisp thread of keyboard layout changes. */
2959 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2961 /* Clear dead keys in the keyboard state; for simplicity only
2962 preserve modifier key states. */
2964 int i;
2965 BYTE keystate[256];
2967 GetKeyboardState (keystate);
2968 for (i = 0; i < 256; i++)
2969 if (1
2970 && i != VK_SHIFT
2971 && i != VK_LSHIFT
2972 && i != VK_RSHIFT
2973 && i != VK_CAPITAL
2974 && i != VK_NUMLOCK
2975 && i != VK_SCROLL
2976 && i != VK_CONTROL
2977 && i != VK_LCONTROL
2978 && i != VK_RCONTROL
2979 && i != VK_MENU
2980 && i != VK_LMENU
2981 && i != VK_RMENU
2982 && i != VK_LWIN
2983 && i != VK_RWIN)
2984 keystate[i] = 0;
2985 SetKeyboardState (keystate);
2987 goto dflt;
2989 case WM_HOTKEY:
2990 /* Synchronize hot keys with normal input. */
2991 PostMessage (hwnd, WM_KEYDOWN, HIWORD (lParam), 0);
2992 return (0);
2994 case WM_KEYUP:
2995 case WM_SYSKEYUP:
2996 record_keyup (wParam, lParam);
2997 goto dflt;
2999 case WM_KEYDOWN:
3000 case WM_SYSKEYDOWN:
3001 /* Ignore keystrokes we fake ourself; see below. */
3002 if (dpyinfo->faked_key == wParam)
3004 dpyinfo->faked_key = 0;
3005 /* Make sure TranslateMessage sees them though (as long as
3006 they don't produce WM_CHAR messages). This ensures that
3007 indicator lights are toggled promptly on Windows 9x, for
3008 example. */
3009 if (wParam < 256 && lispy_function_keys[wParam])
3011 windows_translate = 1;
3012 goto translate;
3014 return 0;
3017 /* Synchronize modifiers with current keystroke. */
3018 sync_modifiers ();
3019 record_keydown (wParam, lParam);
3020 wParam = map_keypad_keys (wParam, (lParam & 0x1000000L) != 0);
3022 windows_translate = 0;
3024 switch (wParam)
3026 case VK_LWIN:
3027 if (NILP (Vw32_pass_lwindow_to_system))
3029 /* Prevent system from acting on keyup (which opens the
3030 Start menu if no other key was pressed) by simulating a
3031 press of Space which we will ignore. */
3032 if (GetAsyncKeyState (wParam) & 1)
3034 if (NUMBERP (Vw32_phantom_key_code))
3035 key = XUINT (Vw32_phantom_key_code) & 255;
3036 else
3037 key = VK_SPACE;
3038 dpyinfo->faked_key = key;
3039 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
3042 if (!NILP (Vw32_lwindow_modifier))
3043 return 0;
3044 break;
3045 case VK_RWIN:
3046 if (NILP (Vw32_pass_rwindow_to_system))
3048 if (GetAsyncKeyState (wParam) & 1)
3050 if (NUMBERP (Vw32_phantom_key_code))
3051 key = XUINT (Vw32_phantom_key_code) & 255;
3052 else
3053 key = VK_SPACE;
3054 dpyinfo->faked_key = key;
3055 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
3058 if (!NILP (Vw32_rwindow_modifier))
3059 return 0;
3060 break;
3061 case VK_APPS:
3062 if (!NILP (Vw32_apps_modifier))
3063 return 0;
3064 break;
3065 case VK_MENU:
3066 if (NILP (Vw32_pass_alt_to_system))
3067 /* Prevent DefWindowProc from activating the menu bar if an
3068 Alt key is pressed and released by itself. */
3069 return 0;
3070 windows_translate = 1;
3071 break;
3072 case VK_CAPITAL:
3073 /* Decide whether to treat as modifier or function key. */
3074 if (NILP (Vw32_enable_caps_lock))
3075 goto disable_lock_key;
3076 windows_translate = 1;
3077 break;
3078 case VK_NUMLOCK:
3079 /* Decide whether to treat as modifier or function key. */
3080 if (NILP (Vw32_enable_num_lock))
3081 goto disable_lock_key;
3082 windows_translate = 1;
3083 break;
3084 case VK_SCROLL:
3085 /* Decide whether to treat as modifier or function key. */
3086 if (NILP (Vw32_scroll_lock_modifier))
3087 goto disable_lock_key;
3088 windows_translate = 1;
3089 break;
3090 disable_lock_key:
3091 /* Ensure the appropriate lock key state (and indicator light)
3092 remains in the same state. We do this by faking another
3093 press of the relevant key. Apparently, this really is the
3094 only way to toggle the state of the indicator lights. */
3095 dpyinfo->faked_key = wParam;
3096 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
3097 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
3098 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
3099 KEYEVENTF_EXTENDEDKEY | 0, 0);
3100 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
3101 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
3102 /* Ensure indicator lights are updated promptly on Windows 9x
3103 (TranslateMessage apparently does this), after forwarding
3104 input event. */
3105 post_character_message (hwnd, msg, wParam, lParam,
3106 w32_get_key_modifiers (wParam, lParam));
3107 windows_translate = 1;
3108 break;
3109 case VK_CONTROL:
3110 case VK_SHIFT:
3111 case VK_PROCESSKEY: /* Generated by IME. */
3112 windows_translate = 1;
3113 break;
3114 case VK_CANCEL:
3115 /* Windows maps Ctrl-Pause (aka Ctrl-Break) into VK_CANCEL,
3116 which is confusing for purposes of key binding; convert
3117 VK_CANCEL events into VK_PAUSE events. */
3118 wParam = VK_PAUSE;
3119 break;
3120 case VK_PAUSE:
3121 /* Windows maps Ctrl-NumLock into VK_PAUSE, which is confusing
3122 for purposes of key binding; convert these back into
3123 VK_NUMLOCK events, at least when we want to see NumLock key
3124 presses. (Note that there is never any possibility that
3125 VK_PAUSE with Ctrl really is C-Pause as per above.) */
3126 if (NILP (Vw32_enable_num_lock) && modifier_set (VK_CONTROL))
3127 wParam = VK_NUMLOCK;
3128 break;
3129 default:
3130 /* If not defined as a function key, change it to a WM_CHAR message. */
3131 if (wParam > 255 || !lispy_function_keys[wParam])
3133 DWORD modifiers = construct_console_modifiers ();
3135 if (!NILP (Vw32_recognize_altgr)
3136 && modifier_set (VK_LCONTROL) && modifier_set (VK_RMENU))
3138 /* Always let TranslateMessage handle AltGr key chords;
3139 for some reason, ToAscii doesn't always process AltGr
3140 chords correctly. */
3141 windows_translate = 1;
3143 else if ((modifiers & (~SHIFT_PRESSED & ~CAPSLOCK_ON)) != 0)
3145 /* Handle key chords including any modifiers other
3146 than shift directly, in order to preserve as much
3147 modifier information as possible. */
3148 if ('A' <= wParam && wParam <= 'Z')
3150 /* Don't translate modified alphabetic keystrokes,
3151 so the user doesn't need to constantly switch
3152 layout to type control or meta keystrokes when
3153 the normal layout translates alphabetic
3154 characters to non-ascii characters. */
3155 if (!modifier_set (VK_SHIFT))
3156 wParam += ('a' - 'A');
3157 msg = WM_CHAR;
3159 else
3161 /* Try to handle other keystrokes by determining the
3162 base character (ie. translating the base key plus
3163 shift modifier). */
3164 int add;
3165 int isdead = 0;
3166 KEY_EVENT_RECORD key;
3168 key.bKeyDown = TRUE;
3169 key.wRepeatCount = 1;
3170 key.wVirtualKeyCode = wParam;
3171 key.wVirtualScanCode = (lParam & 0xFF0000) >> 16;
3172 key.uChar.AsciiChar = 0;
3173 key.dwControlKeyState = modifiers;
3175 add = w32_kbd_patch_key (&key);
3176 /* 0 means an unrecognised keycode, negative means
3177 dead key. Ignore both. */
3178 while (--add >= 0)
3180 /* Forward asciified character sequence. */
3181 post_character_message
3182 (hwnd, WM_CHAR,
3183 (unsigned char) key.uChar.AsciiChar, lParam,
3184 w32_get_key_modifiers (wParam, lParam));
3185 w32_kbd_patch_key (&key);
3187 return 0;
3190 else
3192 /* Let TranslateMessage handle everything else. */
3193 windows_translate = 1;
3198 translate:
3199 if (windows_translate)
3201 MSG windows_msg = { hwnd, msg, wParam, lParam, 0, {0,0} };
3202 windows_msg.time = GetMessageTime ();
3203 TranslateMessage (&windows_msg);
3204 goto dflt;
3207 /* Fall through */
3209 case WM_SYSCHAR:
3210 case WM_CHAR:
3211 post_character_message (hwnd, msg, wParam, lParam,
3212 w32_get_key_modifiers (wParam, lParam));
3213 break;
3215 case WM_UNICHAR:
3216 /* WM_UNICHAR looks promising from the docs, but the exact
3217 circumstances in which TranslateMessage sends it is one of those
3218 Microsoft secret API things that EU and US courts are supposed
3219 to have put a stop to already. Spy++ shows it being sent to Notepad
3220 and other MS apps, but never to Emacs.
3222 Some third party IMEs send it in accordance with the official
3223 documentation though, so handle it here.
3225 UNICODE_NOCHAR is used to test for support for this message.
3226 TRUE indicates that the message is supported. */
3227 if (wParam == UNICODE_NOCHAR)
3228 return TRUE;
3231 W32Msg wmsg;
3232 wmsg.dwModifiers = w32_get_key_modifiers (wParam, lParam);
3233 signal_user_input ();
3234 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3236 break;
3238 case WM_IME_CHAR:
3239 /* If we can't get the IME result as unicode, use default processing,
3240 which will at least allow characters decodable in the system locale
3241 get through. */
3242 if (!get_composition_string_fn)
3243 goto dflt;
3245 else if (!ignore_ime_char)
3247 wchar_t * buffer;
3248 int size, i;
3249 W32Msg wmsg;
3250 HIMC context = get_ime_context_fn (hwnd);
3251 wmsg.dwModifiers = w32_get_key_modifiers (wParam, lParam);
3252 /* Get buffer size. */
3253 size = get_composition_string_fn (context, GCS_RESULTSTR, buffer, 0);
3254 buffer = alloca(size);
3255 size = get_composition_string_fn (context, GCS_RESULTSTR,
3256 buffer, size);
3257 signal_user_input ();
3258 for (i = 0; i < size / sizeof (wchar_t); i++)
3260 my_post_msg (&wmsg, hwnd, WM_UNICHAR, (WPARAM) buffer[i],
3261 lParam);
3263 /* We output the whole string above, so ignore following ones
3264 until we are notified of the end of composition. */
3265 ignore_ime_char = 1;
3267 break;
3269 case WM_IME_ENDCOMPOSITION:
3270 ignore_ime_char = 0;
3271 goto dflt;
3273 /* Simulate middle mouse button events when left and right buttons
3274 are used together, but only if user has two button mouse. */
3275 case WM_LBUTTONDOWN:
3276 case WM_RBUTTONDOWN:
3277 if (w32_num_mouse_buttons > 2)
3278 goto handle_plain_button;
3281 int this = (msg == WM_LBUTTONDOWN) ? LMOUSE : RMOUSE;
3282 int other = (msg == WM_LBUTTONDOWN) ? RMOUSE : LMOUSE;
3284 if (button_state & this)
3285 return 0;
3287 if (button_state == 0)
3288 SetCapture (hwnd);
3290 button_state |= this;
3292 if (button_state & other)
3294 if (mouse_button_timer)
3296 KillTimer (hwnd, mouse_button_timer);
3297 mouse_button_timer = 0;
3299 /* Generate middle mouse event instead. */
3300 msg = WM_MBUTTONDOWN;
3301 button_state |= MMOUSE;
3303 else if (button_state & MMOUSE)
3305 /* Ignore button event if we've already generated a
3306 middle mouse down event. This happens if the
3307 user releases and press one of the two buttons
3308 after we've faked a middle mouse event. */
3309 return 0;
3311 else
3313 /* Flush out saved message. */
3314 post_msg (&saved_mouse_button_msg);
3316 wmsg.dwModifiers = w32_get_modifiers ();
3317 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3318 signal_user_input ();
3320 /* Clear message buffer. */
3321 saved_mouse_button_msg.msg.hwnd = 0;
3323 else
3325 /* Hold onto message for now. */
3326 mouse_button_timer =
3327 SetTimer (hwnd, MOUSE_BUTTON_ID,
3328 w32_mouse_button_tolerance, NULL);
3329 saved_mouse_button_msg.msg.hwnd = hwnd;
3330 saved_mouse_button_msg.msg.message = msg;
3331 saved_mouse_button_msg.msg.wParam = wParam;
3332 saved_mouse_button_msg.msg.lParam = lParam;
3333 saved_mouse_button_msg.msg.time = GetMessageTime ();
3334 saved_mouse_button_msg.dwModifiers = w32_get_modifiers ();
3337 return 0;
3339 case WM_LBUTTONUP:
3340 case WM_RBUTTONUP:
3341 if (w32_num_mouse_buttons > 2)
3342 goto handle_plain_button;
3345 int this = (msg == WM_LBUTTONUP) ? LMOUSE : RMOUSE;
3346 int other = (msg == WM_LBUTTONUP) ? RMOUSE : LMOUSE;
3348 if ((button_state & this) == 0)
3349 return 0;
3351 button_state &= ~this;
3353 if (button_state & MMOUSE)
3355 /* Only generate event when second button is released. */
3356 if ((button_state & other) == 0)
3358 msg = WM_MBUTTONUP;
3359 button_state &= ~MMOUSE;
3361 if (button_state) abort ();
3363 else
3364 return 0;
3366 else
3368 /* Flush out saved message if necessary. */
3369 if (saved_mouse_button_msg.msg.hwnd)
3371 post_msg (&saved_mouse_button_msg);
3374 wmsg.dwModifiers = w32_get_modifiers ();
3375 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3376 signal_user_input ();
3378 /* Always clear message buffer and cancel timer. */
3379 saved_mouse_button_msg.msg.hwnd = 0;
3380 KillTimer (hwnd, mouse_button_timer);
3381 mouse_button_timer = 0;
3383 if (button_state == 0)
3384 ReleaseCapture ();
3386 return 0;
3388 case WM_XBUTTONDOWN:
3389 case WM_XBUTTONUP:
3390 if (w32_pass_extra_mouse_buttons_to_system)
3391 goto dflt;
3392 /* else fall through and process them. */
3393 case WM_MBUTTONDOWN:
3394 case WM_MBUTTONUP:
3395 handle_plain_button:
3397 BOOL up;
3398 int button;
3400 /* Ignore middle and extra buttons as long as the menu is active. */
3401 f = x_window_to_frame (dpyinfo, hwnd);
3402 if (f && f->output_data.w32->menubar_active)
3403 return 0;
3405 if (parse_button (msg, HIWORD (wParam), &button, &up))
3407 if (up) ReleaseCapture ();
3408 else SetCapture (hwnd);
3409 button = (button == 0) ? LMOUSE :
3410 ((button == 1) ? MMOUSE : RMOUSE);
3411 if (up)
3412 button_state &= ~button;
3413 else
3414 button_state |= button;
3418 wmsg.dwModifiers = w32_get_modifiers ();
3419 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3420 signal_user_input ();
3422 /* Need to return true for XBUTTON messages, false for others,
3423 to indicate that we processed the message. */
3424 return (msg == WM_XBUTTONDOWN || msg == WM_XBUTTONUP);
3426 case WM_MOUSEMOVE:
3427 /* Ignore mouse movements as long as the menu is active. These
3428 movements are processed by the window manager anyway, and
3429 it's wrong to handle them as if they happened on the
3430 underlying frame. */
3431 f = x_window_to_frame (dpyinfo, hwnd);
3432 if (f && f->output_data.w32->menubar_active)
3433 return 0;
3435 /* If the mouse has just moved into the frame, start tracking
3436 it, so we will be notified when it leaves the frame. Mouse
3437 tracking only works under W98 and NT4 and later. On earlier
3438 versions, there is no way of telling when the mouse leaves the
3439 frame, so we just have to put up with help-echo and mouse
3440 highlighting remaining while the frame is not active. */
3441 if (track_mouse_event_fn && !track_mouse_window)
3443 TRACKMOUSEEVENT tme;
3444 tme.cbSize = sizeof (tme);
3445 tme.dwFlags = TME_LEAVE;
3446 tme.hwndTrack = hwnd;
3448 track_mouse_event_fn (&tme);
3449 track_mouse_window = hwnd;
3451 case WM_VSCROLL:
3452 if (w32_mouse_move_interval <= 0
3453 || (msg == WM_MOUSEMOVE && button_state == 0))
3455 wmsg.dwModifiers = w32_get_modifiers ();
3456 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3457 return 0;
3460 /* Hang onto mouse move and scroll messages for a bit, to avoid
3461 sending such events to Emacs faster than it can process them.
3462 If we get more events before the timer from the first message
3463 expires, we just replace the first message. */
3465 if (saved_mouse_move_msg.msg.hwnd == 0)
3466 mouse_move_timer =
3467 SetTimer (hwnd, MOUSE_MOVE_ID,
3468 w32_mouse_move_interval, NULL);
3470 /* Hold onto message for now. */
3471 saved_mouse_move_msg.msg.hwnd = hwnd;
3472 saved_mouse_move_msg.msg.message = msg;
3473 saved_mouse_move_msg.msg.wParam = wParam;
3474 saved_mouse_move_msg.msg.lParam = lParam;
3475 saved_mouse_move_msg.msg.time = GetMessageTime ();
3476 saved_mouse_move_msg.dwModifiers = w32_get_modifiers ();
3478 return 0;
3480 case WM_MOUSEWHEEL:
3481 case WM_DROPFILES:
3482 wmsg.dwModifiers = w32_get_modifiers ();
3483 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3484 signal_user_input ();
3485 return 0;
3487 case WM_APPCOMMAND:
3488 if (w32_pass_multimedia_buttons_to_system)
3489 goto dflt;
3490 /* Otherwise, pass to lisp, the same way we do with mousehwheel. */
3491 case WM_MOUSEHWHEEL:
3492 wmsg.dwModifiers = w32_get_modifiers ();
3493 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3494 signal_user_input ();
3495 /* Non-zero must be returned when WM_MOUSEHWHEEL messages are
3496 handled, to prevent the system trying to handle it by faking
3497 scroll bar events. */
3498 return 1;
3500 case WM_TIMER:
3501 /* Flush out saved messages if necessary. */
3502 if (wParam == mouse_button_timer)
3504 if (saved_mouse_button_msg.msg.hwnd)
3506 post_msg (&saved_mouse_button_msg);
3507 signal_user_input ();
3508 saved_mouse_button_msg.msg.hwnd = 0;
3510 KillTimer (hwnd, mouse_button_timer);
3511 mouse_button_timer = 0;
3513 else if (wParam == mouse_move_timer)
3515 if (saved_mouse_move_msg.msg.hwnd)
3517 post_msg (&saved_mouse_move_msg);
3518 saved_mouse_move_msg.msg.hwnd = 0;
3520 KillTimer (hwnd, mouse_move_timer);
3521 mouse_move_timer = 0;
3523 else if (wParam == menu_free_timer)
3525 KillTimer (hwnd, menu_free_timer);
3526 menu_free_timer = 0;
3527 f = x_window_to_frame (dpyinfo, hwnd);
3528 /* If a popup menu is active, don't wipe its strings. */
3529 if (menubar_in_use
3530 && current_popup_menu == NULL)
3532 /* Free memory used by owner-drawn and help-echo strings. */
3533 w32_free_menu_strings (hwnd);
3534 f->output_data.w32->menubar_active = 0;
3535 menubar_in_use = 0;
3538 else if (wParam == hourglass_timer)
3540 KillTimer (hwnd, hourglass_timer);
3541 hourglass_timer = 0;
3542 show_hourglass (x_window_to_frame (dpyinfo, hwnd));
3544 return 0;
3546 case WM_NCACTIVATE:
3547 /* Windows doesn't send us focus messages when putting up and
3548 taking down a system popup dialog as for Ctrl-Alt-Del on Windows 95.
3549 The only indication we get that something happened is receiving
3550 this message afterwards. So this is a good time to reset our
3551 keyboard modifiers' state. */
3552 reset_modifiers ();
3553 goto dflt;
3555 case WM_INITMENU:
3556 button_state = 0;
3557 ReleaseCapture ();
3558 /* We must ensure menu bar is fully constructed and up to date
3559 before allowing user interaction with it. To achieve this
3560 we send this message to the lisp thread and wait for a
3561 reply (whose value is not actually needed) to indicate that
3562 the menu bar is now ready for use, so we can now return.
3564 To remain responsive in the meantime, we enter a nested message
3565 loop that can process all other messages.
3567 However, we skip all this if the message results from calling
3568 TrackPopupMenu - in fact, we must NOT attempt to send the lisp
3569 thread a message because it is blocked on us at this point. We
3570 set menubar_active before calling TrackPopupMenu to indicate
3571 this (there is no possibility of confusion with real menubar
3572 being active). */
3574 f = x_window_to_frame (dpyinfo, hwnd);
3575 if (f
3576 && (f->output_data.w32->menubar_active
3577 /* We can receive this message even in the absence of a
3578 menubar (ie. when the system menu is activated) - in this
3579 case we do NOT want to forward the message, otherwise it
3580 will cause the menubar to suddenly appear when the user
3581 had requested it to be turned off! */
3582 || f->output_data.w32->menubar_widget == NULL))
3583 return 0;
3586 deferred_msg msg_buf;
3588 /* Detect if message has already been deferred; in this case
3589 we cannot return any sensible value to ignore this. */
3590 if (find_deferred_msg (hwnd, msg) != NULL)
3591 abort ();
3593 menubar_in_use = 1;
3595 return send_deferred_msg (&msg_buf, hwnd, msg, wParam, lParam);
3598 case WM_EXITMENULOOP:
3599 f = x_window_to_frame (dpyinfo, hwnd);
3601 /* If a menu is still active, check again after a short delay,
3602 since Windows often (always?) sends the WM_EXITMENULOOP
3603 before the corresponding WM_COMMAND message.
3604 Don't do this if a popup menu is active, since it is only
3605 menubar menus that require cleaning up in this way.
3607 if (f && menubar_in_use && current_popup_menu == NULL)
3608 menu_free_timer = SetTimer (hwnd, MENU_FREE_ID, MENU_FREE_DELAY, NULL);
3610 /* If hourglass cursor should be displayed, display it now. */
3611 if (f && f->output_data.w32->hourglass_p)
3612 SetCursor (f->output_data.w32->hourglass_cursor);
3614 goto dflt;
3616 case WM_MENUSELECT:
3617 /* Direct handling of help_echo in menus. Should be safe now
3618 that we generate the help_echo by placing a help event in the
3619 keyboard buffer. */
3621 HMENU menu = (HMENU) lParam;
3622 UINT menu_item = (UINT) LOWORD (wParam);
3623 UINT flags = (UINT) HIWORD (wParam);
3625 w32_menu_display_help (hwnd, menu, menu_item, flags);
3627 return 0;
3629 case WM_MEASUREITEM:
3630 f = x_window_to_frame (dpyinfo, hwnd);
3631 if (f)
3633 MEASUREITEMSTRUCT * pMis = (MEASUREITEMSTRUCT *) lParam;
3635 if (pMis->CtlType == ODT_MENU)
3637 /* Work out dimensions for popup menu titles. */
3638 char * title = (char *) pMis->itemData;
3639 HDC hdc = GetDC (hwnd);
3640 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
3641 LOGFONT menu_logfont;
3642 HFONT old_font;
3643 SIZE size;
3645 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
3646 menu_logfont.lfWeight = FW_BOLD;
3647 menu_font = CreateFontIndirect (&menu_logfont);
3648 old_font = SelectObject (hdc, menu_font);
3650 pMis->itemHeight = GetSystemMetrics (SM_CYMENUSIZE);
3651 if (title)
3653 if (unicode_append_menu)
3654 GetTextExtentPoint32W (hdc, (WCHAR *) title,
3655 wcslen ((WCHAR *) title),
3656 &size);
3657 else
3658 GetTextExtentPoint32 (hdc, title, strlen (title), &size);
3660 pMis->itemWidth = size.cx;
3661 if (pMis->itemHeight < size.cy)
3662 pMis->itemHeight = size.cy;
3664 else
3665 pMis->itemWidth = 0;
3667 SelectObject (hdc, old_font);
3668 DeleteObject (menu_font);
3669 ReleaseDC (hwnd, hdc);
3670 return TRUE;
3673 return 0;
3675 case WM_DRAWITEM:
3676 f = x_window_to_frame (dpyinfo, hwnd);
3677 if (f)
3679 DRAWITEMSTRUCT * pDis = (DRAWITEMSTRUCT *) lParam;
3681 if (pDis->CtlType == ODT_MENU)
3683 /* Draw popup menu title. */
3684 char * title = (char *) pDis->itemData;
3685 if (title)
3687 HDC hdc = pDis->hDC;
3688 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
3689 LOGFONT menu_logfont;
3690 HFONT old_font;
3692 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
3693 menu_logfont.lfWeight = FW_BOLD;
3694 menu_font = CreateFontIndirect (&menu_logfont);
3695 old_font = SelectObject (hdc, menu_font);
3697 /* Always draw title as if not selected. */
3698 if (unicode_append_menu)
3699 ExtTextOutW (hdc,
3700 pDis->rcItem.left
3701 + GetSystemMetrics (SM_CXMENUCHECK),
3702 pDis->rcItem.top,
3703 ETO_OPAQUE, &pDis->rcItem,
3704 (WCHAR *) title,
3705 wcslen ((WCHAR *) title), NULL);
3706 else
3707 ExtTextOut (hdc,
3708 pDis->rcItem.left
3709 + GetSystemMetrics (SM_CXMENUCHECK),
3710 pDis->rcItem.top,
3711 ETO_OPAQUE, &pDis->rcItem,
3712 title, strlen (title), NULL);
3714 SelectObject (hdc, old_font);
3715 DeleteObject (menu_font);
3717 return TRUE;
3720 return 0;
3722 #if 0
3723 /* Still not right - can't distinguish between clicks in the
3724 client area of the frame from clicks forwarded from the scroll
3725 bars - may have to hook WM_NCHITTEST to remember the mouse
3726 position and then check if it is in the client area ourselves. */
3727 case WM_MOUSEACTIVATE:
3728 /* Discard the mouse click that activates a frame, allowing the
3729 user to click anywhere without changing point (or worse!).
3730 Don't eat mouse clicks on scrollbars though!! */
3731 if (LOWORD (lParam) == HTCLIENT )
3732 return MA_ACTIVATEANDEAT;
3733 goto dflt;
3734 #endif
3736 case WM_MOUSELEAVE:
3737 /* No longer tracking mouse. */
3738 track_mouse_window = NULL;
3740 case WM_ACTIVATEAPP:
3741 case WM_ACTIVATE:
3742 case WM_WINDOWPOSCHANGED:
3743 case WM_SHOWWINDOW:
3744 /* Inform lisp thread that a frame might have just been obscured
3745 or exposed, so should recheck visibility of all frames. */
3746 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3747 goto dflt;
3749 case WM_SETFOCUS:
3750 dpyinfo->faked_key = 0;
3751 reset_modifiers ();
3752 register_hot_keys (hwnd);
3753 goto command;
3754 case WM_KILLFOCUS:
3755 unregister_hot_keys (hwnd);
3756 button_state = 0;
3757 ReleaseCapture ();
3758 /* Relinquish the system caret. */
3759 if (w32_system_caret_hwnd)
3761 w32_visible_system_caret_hwnd = NULL;
3762 w32_system_caret_hwnd = NULL;
3763 DestroyCaret ();
3765 goto command;
3766 case WM_COMMAND:
3767 menubar_in_use = 0;
3768 f = x_window_to_frame (dpyinfo, hwnd);
3769 if (f && HIWORD (wParam) == 0)
3771 if (menu_free_timer)
3773 KillTimer (hwnd, menu_free_timer);
3774 menu_free_timer = 0;
3777 case WM_MOVE:
3778 case WM_SIZE:
3779 command:
3780 wmsg.dwModifiers = w32_get_modifiers ();
3781 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3782 goto dflt;
3784 case WM_DESTROY:
3785 CoUninitialize ();
3786 return 0;
3788 case WM_CLOSE:
3789 wmsg.dwModifiers = w32_get_modifiers ();
3790 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3791 return 0;
3793 case WM_WINDOWPOSCHANGING:
3794 /* Don't restrict the sizing of tip frames. */
3795 if (hwnd == tip_window)
3796 return 0;
3798 WINDOWPLACEMENT wp;
3799 LPWINDOWPOS lppos = (WINDOWPOS *) lParam;
3801 wp.length = sizeof (WINDOWPLACEMENT);
3802 GetWindowPlacement (hwnd, &wp);
3804 if (wp.showCmd != SW_SHOWMINIMIZED && (lppos->flags & SWP_NOSIZE) == 0)
3806 RECT rect;
3807 int wdiff;
3808 int hdiff;
3809 DWORD font_width;
3810 DWORD line_height;
3811 DWORD internal_border;
3812 DWORD scrollbar_extra;
3813 RECT wr;
3815 wp.length = sizeof (wp);
3816 GetWindowRect (hwnd, &wr);
3818 enter_crit ();
3820 font_width = GetWindowLong (hwnd, WND_FONTWIDTH_INDEX);
3821 line_height = GetWindowLong (hwnd, WND_LINEHEIGHT_INDEX);
3822 internal_border = GetWindowLong (hwnd, WND_BORDER_INDEX);
3823 scrollbar_extra = GetWindowLong (hwnd, WND_SCROLLBAR_INDEX);
3825 leave_crit ();
3827 memset (&rect, 0, sizeof (rect));
3828 AdjustWindowRect (&rect, GetWindowLong (hwnd, GWL_STYLE),
3829 GetMenu (hwnd) != NULL);
3831 /* Force width and height of client area to be exact
3832 multiples of the character cell dimensions. */
3833 wdiff = (lppos->cx - (rect.right - rect.left)
3834 - 2 * internal_border - scrollbar_extra)
3835 % font_width;
3836 hdiff = (lppos->cy - (rect.bottom - rect.top)
3837 - 2 * internal_border)
3838 % line_height;
3840 if (wdiff || hdiff)
3842 /* For right/bottom sizing we can just fix the sizes.
3843 However for top/left sizing we will need to fix the X
3844 and Y positions as well. */
3846 int cx_mintrack = GetSystemMetrics (SM_CXMINTRACK);
3847 int cy_mintrack = GetSystemMetrics (SM_CYMINTRACK);
3849 lppos->cx = max (lppos->cx - wdiff, cx_mintrack);
3850 lppos->cy = max (lppos->cy - hdiff, cy_mintrack);
3852 if (wp.showCmd != SW_SHOWMAXIMIZED
3853 && (lppos->flags & SWP_NOMOVE) == 0)
3855 if (lppos->x != wr.left || lppos->y != wr.top)
3857 lppos->x += wdiff;
3858 lppos->y += hdiff;
3860 else
3862 lppos->flags |= SWP_NOMOVE;
3866 return 0;
3871 goto dflt;
3873 case WM_GETMINMAXINFO:
3874 /* Hack to allow resizing the Emacs frame above the screen size.
3875 Note that Windows 9x limits coordinates to 16-bits. */
3876 ((LPMINMAXINFO) lParam)->ptMaxTrackSize.x = 32767;
3877 ((LPMINMAXINFO) lParam)->ptMaxTrackSize.y = 32767;
3878 return 0;
3880 case WM_SETCURSOR:
3881 if (LOWORD (lParam) == HTCLIENT)
3883 f = x_window_to_frame (dpyinfo, hwnd);
3884 if (f->output_data.w32->hourglass_p && !menubar_in_use
3885 && !current_popup_menu)
3886 SetCursor (f->output_data.w32->hourglass_cursor);
3887 else
3888 SetCursor (f->output_data.w32->current_cursor);
3889 return 0;
3891 goto dflt;
3893 case WM_EMACS_SETCURSOR:
3895 Cursor cursor = (Cursor) wParam;
3896 f = x_window_to_frame (dpyinfo, hwnd);
3897 if (f && cursor)
3899 f->output_data.w32->current_cursor = cursor;
3900 if (!f->output_data.w32->hourglass_p)
3901 SetCursor (cursor);
3903 return 0;
3906 case WM_EMACS_CREATESCROLLBAR:
3907 return (LRESULT) w32_createscrollbar ((struct frame *) wParam,
3908 (struct scroll_bar *) lParam);
3910 case WM_EMACS_SHOWWINDOW:
3911 return ShowWindow ((HWND) wParam, (WPARAM) lParam);
3913 case WM_EMACS_SETFOREGROUND:
3915 HWND foreground_window;
3916 DWORD foreground_thread, retval;
3918 /* On NT 5.0, and apparently Windows 98, it is necessary to
3919 attach to the thread that currently has focus in order to
3920 pull the focus away from it. */
3921 foreground_window = GetForegroundWindow ();
3922 foreground_thread = GetWindowThreadProcessId (foreground_window, NULL);
3923 if (!foreground_window
3924 || foreground_thread == GetCurrentThreadId ()
3925 || !AttachThreadInput (GetCurrentThreadId (),
3926 foreground_thread, TRUE))
3927 foreground_thread = 0;
3929 retval = SetForegroundWindow ((HWND) wParam);
3931 /* Detach from the previous foreground thread. */
3932 if (foreground_thread)
3933 AttachThreadInput (GetCurrentThreadId (),
3934 foreground_thread, FALSE);
3936 return retval;
3939 case WM_EMACS_SETWINDOWPOS:
3941 WINDOWPOS * pos = (WINDOWPOS *) wParam;
3942 return SetWindowPos (hwnd, pos->hwndInsertAfter,
3943 pos->x, pos->y, pos->cx, pos->cy, pos->flags);
3946 case WM_EMACS_DESTROYWINDOW:
3947 DragAcceptFiles ((HWND) wParam, FALSE);
3948 return DestroyWindow ((HWND) wParam);
3950 case WM_EMACS_HIDE_CARET:
3951 return HideCaret (hwnd);
3953 case WM_EMACS_SHOW_CARET:
3954 return ShowCaret (hwnd);
3956 case WM_EMACS_DESTROY_CARET:
3957 w32_system_caret_hwnd = NULL;
3958 w32_visible_system_caret_hwnd = NULL;
3959 return DestroyCaret ();
3961 case WM_EMACS_TRACK_CARET:
3962 /* If there is currently no system caret, create one. */
3963 if (w32_system_caret_hwnd == NULL)
3965 /* Use the default caret width, and avoid changing it
3966 unneccesarily, as it confuses screen reader software. */
3967 w32_system_caret_hwnd = hwnd;
3968 CreateCaret (hwnd, NULL, 0,
3969 w32_system_caret_height);
3972 if (!SetCaretPos (w32_system_caret_x, w32_system_caret_y))
3973 return 0;
3974 /* Ensure visible caret gets turned on when requested. */
3975 else if (w32_use_visible_system_caret
3976 && w32_visible_system_caret_hwnd != hwnd)
3978 w32_visible_system_caret_hwnd = hwnd;
3979 return ShowCaret (hwnd);
3981 /* Ensure visible caret gets turned off when requested. */
3982 else if (!w32_use_visible_system_caret
3983 && w32_visible_system_caret_hwnd)
3985 w32_visible_system_caret_hwnd = NULL;
3986 return HideCaret (hwnd);
3988 else
3989 return 1;
3991 case WM_EMACS_TRACKPOPUPMENU:
3993 UINT flags;
3994 POINT *pos;
3995 int retval;
3996 pos = (POINT *)lParam;
3997 flags = TPM_CENTERALIGN;
3998 if (button_state & LMOUSE)
3999 flags |= TPM_LEFTBUTTON;
4000 else if (button_state & RMOUSE)
4001 flags |= TPM_RIGHTBUTTON;
4003 /* Remember we did a SetCapture on the initial mouse down event,
4004 so for safety, we make sure the capture is cancelled now. */
4005 ReleaseCapture ();
4006 button_state = 0;
4008 /* Use menubar_active to indicate that WM_INITMENU is from
4009 TrackPopupMenu below, and should be ignored. */
4010 f = x_window_to_frame (dpyinfo, hwnd);
4011 if (f)
4012 f->output_data.w32->menubar_active = 1;
4014 if (TrackPopupMenu ((HMENU)wParam, flags, pos->x, pos->y,
4015 0, hwnd, NULL))
4017 MSG amsg;
4018 /* Eat any mouse messages during popupmenu */
4019 while (PeekMessage (&amsg, hwnd, WM_MOUSEFIRST, WM_MOUSELAST,
4020 PM_REMOVE));
4021 /* Get the menu selection, if any */
4022 if (PeekMessage (&amsg, hwnd, WM_COMMAND, WM_COMMAND, PM_REMOVE))
4024 retval = LOWORD (amsg.wParam);
4026 else
4028 retval = 0;
4031 else
4033 retval = -1;
4036 return retval;
4039 default:
4040 /* Check for messages registered at runtime. */
4041 if (msg == msh_mousewheel)
4043 wmsg.dwModifiers = w32_get_modifiers ();
4044 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4045 signal_user_input ();
4046 return 0;
4049 dflt:
4050 return DefWindowProc (hwnd, msg, wParam, lParam);
4054 /* The most common default return code for handled messages is 0. */
4055 return 0;
4058 static void
4059 my_create_window (f)
4060 struct frame * f;
4062 MSG msg;
4064 if (!PostThreadMessage (dwWindowsThreadId, WM_EMACS_CREATEWINDOW, (WPARAM)f, 0))
4065 abort ();
4066 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
4070 /* Create a tooltip window. Unlike my_create_window, we do not do this
4071 indirectly via the Window thread, as we do not need to process Window
4072 messages for the tooltip. Creating tooltips indirectly also creates
4073 deadlocks when tooltips are created for menu items. */
4074 static void
4075 my_create_tip_window (f)
4076 struct frame *f;
4078 RECT rect;
4080 rect.left = rect.top = 0;
4081 rect.right = FRAME_PIXEL_WIDTH (f);
4082 rect.bottom = FRAME_PIXEL_HEIGHT (f);
4084 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
4085 FRAME_EXTERNAL_MENU_BAR (f));
4087 tip_window = FRAME_W32_WINDOW (f)
4088 = CreateWindow (EMACS_CLASS,
4089 f->namebuf,
4090 f->output_data.w32->dwStyle,
4091 f->left_pos,
4092 f->top_pos,
4093 rect.right - rect.left,
4094 rect.bottom - rect.top,
4095 FRAME_W32_WINDOW (SELECTED_FRAME ()), /* owner */
4096 NULL,
4097 hinst,
4098 NULL);
4100 if (tip_window)
4102 SetWindowLong (tip_window, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
4103 SetWindowLong (tip_window, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
4104 SetWindowLong (tip_window, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
4105 SetWindowLong (tip_window, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
4107 /* Tip frames have no scrollbars. */
4108 SetWindowLong (tip_window, WND_SCROLLBAR_INDEX, 0);
4110 /* Do this to discard the default setting specified by our parent. */
4111 ShowWindow (tip_window, SW_HIDE);
4116 /* Create and set up the w32 window for frame F. */
4118 static void
4119 w32_window (f, window_prompting, minibuffer_only)
4120 struct frame *f;
4121 long window_prompting;
4122 int minibuffer_only;
4124 BLOCK_INPUT;
4126 /* Use the resource name as the top-level window name
4127 for looking up resources. Make a non-Lisp copy
4128 for the window manager, so GC relocation won't bother it.
4130 Elsewhere we specify the window name for the window manager. */
4133 char *str = (char *) SDATA (Vx_resource_name);
4134 f->namebuf = (char *) xmalloc (strlen (str) + 1);
4135 strcpy (f->namebuf, str);
4138 my_create_window (f);
4140 validate_x_resource_name ();
4142 /* x_set_name normally ignores requests to set the name if the
4143 requested name is the same as the current name. This is the one
4144 place where that assumption isn't correct; f->name is set, but
4145 the server hasn't been told. */
4147 Lisp_Object name;
4148 int explicit = f->explicit_name;
4150 f->explicit_name = 0;
4151 name = f->name;
4152 f->name = Qnil;
4153 x_set_name (f, name, explicit);
4156 UNBLOCK_INPUT;
4158 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
4159 initialize_frame_menubar (f);
4161 if (FRAME_W32_WINDOW (f) == 0)
4162 error ("Unable to create window");
4165 /* Handle the icon stuff for this window. Perhaps later we might
4166 want an x_set_icon_position which can be called interactively as
4167 well. */
4169 static void
4170 x_icon (f, parms)
4171 struct frame *f;
4172 Lisp_Object parms;
4174 Lisp_Object icon_x, icon_y;
4176 /* Set the position of the icon. Note that Windows 95 groups all
4177 icons in the tray. */
4178 icon_x = w32_get_arg (parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
4179 icon_y = w32_get_arg (parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
4180 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
4182 CHECK_NUMBER (icon_x);
4183 CHECK_NUMBER (icon_y);
4185 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
4186 error ("Both left and top icon corners of icon must be specified");
4188 BLOCK_INPUT;
4190 if (! EQ (icon_x, Qunbound))
4191 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
4193 #if 0 /* TODO */
4194 /* Start up iconic or window? */
4195 x_wm_set_window_state
4196 (f, (EQ (w32_get_arg (parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL), Qicon)
4197 ? IconicState
4198 : NormalState));
4200 x_text_icon (f, (char *) SDATA ((!NILP (f->icon_name)
4201 ? f->icon_name
4202 : f->name)));
4203 #endif
4205 UNBLOCK_INPUT;
4209 static void
4210 x_make_gc (f)
4211 struct frame *f;
4213 XGCValues gc_values;
4215 BLOCK_INPUT;
4217 /* Create the GC's of this frame.
4218 Note that many default values are used. */
4220 /* Normal video */
4221 gc_values.font = FRAME_FONT (f);
4223 /* Cursor has cursor-color background, background-color foreground. */
4224 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
4225 gc_values.background = f->output_data.w32->cursor_pixel;
4226 f->output_data.w32->cursor_gc
4227 = XCreateGC (NULL, FRAME_W32_WINDOW (f),
4228 (GCFont | GCForeground | GCBackground),
4229 &gc_values);
4231 /* Reliefs. */
4232 f->output_data.w32->white_relief.gc = 0;
4233 f->output_data.w32->black_relief.gc = 0;
4235 UNBLOCK_INPUT;
4239 /* Handler for signals raised during x_create_frame and
4240 x_create_top_frame. FRAME is the frame which is partially
4241 constructed. */
4243 static Lisp_Object
4244 unwind_create_frame (frame)
4245 Lisp_Object frame;
4247 struct frame *f = XFRAME (frame);
4249 /* If frame is ``official'', nothing to do. */
4250 if (!CONSP (Vframe_list) || !EQ (XCAR (Vframe_list), frame))
4252 #ifdef GLYPH_DEBUG
4253 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4254 #endif
4256 x_free_frame_resources (f);
4258 /* Check that reference counts are indeed correct. */
4259 xassert (dpyinfo->reference_count == dpyinfo_refcount);
4260 xassert (dpyinfo->image_cache->refcount == image_cache_refcount);
4262 return Qt;
4265 return Qnil;
4268 static void
4269 x_default_font_parameter (f, parms)
4270 struct frame *f;
4271 Lisp_Object parms;
4273 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4274 Lisp_Object font_param = x_get_arg (dpyinfo, parms, Qfont, NULL, NULL,
4275 RES_TYPE_STRING);
4276 Lisp_Object font;
4277 if (EQ (font_param, Qunbound))
4278 font_param = Qnil;
4279 font = !NILP (font_param) ? font_param
4280 : x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
4282 if (!STRINGP (font))
4284 int i;
4285 static char *names[]
4286 = { "Courier New-10",
4287 "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1",
4288 "-*-Fixedsys-normal-r-*-*-12-*-*-*-c-*-iso8859-1",
4289 "Fixedsys",
4290 NULL };
4292 for (i = 0; names[i]; i++)
4294 font = font_open_by_name (f, names[i]);
4295 if (! NILP (font))
4296 break;
4298 if (NILP (font))
4299 error ("No suitable font was found");
4301 else if (!NILP (font_param))
4303 /* Remember the explicit font parameter, so we can re-apply it after
4304 we've applied the `default' face settings. */
4305 x_set_frame_parameters (f, Fcons (Fcons (Qfont_param, font_param), Qnil));
4307 x_default_parameter (f, parms, Qfont, font, "font", "Font", RES_TYPE_STRING);
4310 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
4311 1, 1, 0,
4312 doc: /* Make a new window, which is called a \"frame\" in Emacs terms.
4313 Return an Emacs frame object.
4314 PARAMETERS is an alist of frame parameters.
4315 If the parameters specify that the frame should not have a minibuffer,
4316 and do not specify a specific minibuffer window to use,
4317 then `default-minibuffer-frame' must be a frame whose minibuffer can
4318 be shared by the new frame.
4320 This function is an internal primitive--use `make-frame' instead. */)
4321 (parameters)
4322 Lisp_Object parameters;
4324 struct frame *f;
4325 Lisp_Object frame, tem;
4326 Lisp_Object name;
4327 int minibuffer_only = 0;
4328 long window_prompting = 0;
4329 int width, height;
4330 int count = SPECPDL_INDEX ();
4331 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4332 Lisp_Object display;
4333 struct w32_display_info *dpyinfo = NULL;
4334 Lisp_Object parent;
4335 struct kboard *kb;
4337 check_w32 ();
4339 /* Make copy of frame parameters because the original is in pure
4340 storage now. */
4341 parameters = Fcopy_alist (parameters);
4343 /* Use this general default value to start with
4344 until we know if this frame has a specified name. */
4345 Vx_resource_name = Vinvocation_name;
4347 display = w32_get_arg (parameters, Qdisplay, 0, 0, RES_TYPE_STRING);
4348 if (EQ (display, Qunbound))
4349 display = Qnil;
4350 dpyinfo = check_x_display_info (display);
4351 #ifdef MULTI_KBOARD
4352 kb = dpyinfo->terminal->kboard;
4353 #else
4354 kb = &the_only_kboard;
4355 #endif
4357 name = w32_get_arg (parameters, Qname, "name", "Name", RES_TYPE_STRING);
4358 if (!STRINGP (name)
4359 && ! EQ (name, Qunbound)
4360 && ! NILP (name))
4361 error ("Invalid frame name--not a string or nil");
4363 if (STRINGP (name))
4364 Vx_resource_name = name;
4366 /* See if parent window is specified. */
4367 parent = w32_get_arg (parameters, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
4368 if (EQ (parent, Qunbound))
4369 parent = Qnil;
4370 if (! NILP (parent))
4371 CHECK_NUMBER (parent);
4373 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
4374 /* No need to protect DISPLAY because that's not used after passing
4375 it to make_frame_without_minibuffer. */
4376 frame = Qnil;
4377 GCPRO4 (parameters, parent, name, frame);
4378 tem = w32_get_arg (parameters, Qminibuffer, "minibuffer", "Minibuffer",
4379 RES_TYPE_SYMBOL);
4380 if (EQ (tem, Qnone) || NILP (tem))
4381 f = make_frame_without_minibuffer (Qnil, kb, display);
4382 else if (EQ (tem, Qonly))
4384 f = make_minibuffer_frame ();
4385 minibuffer_only = 1;
4387 else if (WINDOWP (tem))
4388 f = make_frame_without_minibuffer (tem, kb, display);
4389 else
4390 f = make_frame (1);
4392 XSETFRAME (frame, f);
4394 /* Note that Windows does support scroll bars. */
4395 FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;
4397 /* By default, make scrollbars the system standard width. */
4398 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = GetSystemMetrics (SM_CXVSCROLL);
4400 f->terminal = dpyinfo->terminal;
4401 f->terminal->reference_count++;
4403 f->output_method = output_w32;
4404 f->output_data.w32 =
4405 (struct w32_output *) xmalloc (sizeof (struct w32_output));
4406 bzero (f->output_data.w32, sizeof (struct w32_output));
4407 FRAME_FONTSET (f) = -1;
4408 record_unwind_protect (unwind_create_frame, frame);
4410 f->icon_name
4411 = w32_get_arg (parameters, Qicon_name, "iconName", "Title", RES_TYPE_STRING);
4412 if (! STRINGP (f->icon_name))
4413 f->icon_name = Qnil;
4415 /* FRAME_W32_DISPLAY_INFO (f) = dpyinfo; */
4416 #ifdef MULTI_KBOARD
4417 FRAME_KBOARD (f) = kb;
4418 #endif
4420 /* Specify the parent under which to make this window. */
4422 if (!NILP (parent))
4424 f->output_data.w32->parent_desc = (Window) XFASTINT (parent);
4425 f->output_data.w32->explicit_parent = 1;
4427 else
4429 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
4430 f->output_data.w32->explicit_parent = 0;
4433 /* Set the name; the functions to which we pass f expect the name to
4434 be set. */
4435 if (EQ (name, Qunbound) || NILP (name))
4437 f->name = build_string (dpyinfo->w32_id_name);
4438 f->explicit_name = 0;
4440 else
4442 f->name = name;
4443 f->explicit_name = 1;
4444 /* use the frame's title when getting resources for this frame. */
4445 specbind (Qx_resource_name, name);
4448 f->resx = dpyinfo->resx;
4449 f->resy = dpyinfo->resy;
4451 if (uniscribe_available)
4452 register_font_driver (&uniscribe_font_driver, f);
4453 register_font_driver (&w32font_driver, f);
4455 x_default_parameter (f, parameters, Qfont_backend, Qnil,
4456 "fontBackend", "FontBackend", RES_TYPE_STRING);
4457 /* Extract the window parameters from the supplied values
4458 that are needed to determine window geometry. */
4459 x_default_font_parameter (f, parameters);
4460 x_default_parameter (f, parameters, Qborder_width, make_number (2),
4461 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
4462 /* This defaults to 2 in order to match xterm. We recognize either
4463 internalBorderWidth or internalBorder (which is what xterm calls
4464 it). */
4465 if (NILP (Fassq (Qinternal_border_width, parameters)))
4467 Lisp_Object value;
4469 value = w32_get_arg (parameters, Qinternal_border_width,
4470 "internalBorder", "InternalBorder", RES_TYPE_NUMBER);
4471 if (! EQ (value, Qunbound))
4472 parameters = Fcons (Fcons (Qinternal_border_width, value),
4473 parameters);
4475 /* Default internalBorderWidth to 0 on Windows to match other programs. */
4476 x_default_parameter (f, parameters, Qinternal_border_width, make_number (0),
4477 "internalBorderWidth", "InternalBorder", RES_TYPE_NUMBER);
4478 x_default_parameter (f, parameters, Qvertical_scroll_bars, Qright,
4479 "verticalScrollBars", "ScrollBars", RES_TYPE_SYMBOL);
4481 /* Also do the stuff which must be set before the window exists. */
4482 x_default_parameter (f, parameters, Qforeground_color, build_string ("black"),
4483 "foreground", "Foreground", RES_TYPE_STRING);
4484 x_default_parameter (f, parameters, Qbackground_color, build_string ("white"),
4485 "background", "Background", RES_TYPE_STRING);
4486 x_default_parameter (f, parameters, Qmouse_color, build_string ("black"),
4487 "pointerColor", "Foreground", RES_TYPE_STRING);
4488 x_default_parameter (f, parameters, Qcursor_color, build_string ("black"),
4489 "cursorColor", "Foreground", RES_TYPE_STRING);
4490 x_default_parameter (f, parameters, Qborder_color, build_string ("black"),
4491 "borderColor", "BorderColor", RES_TYPE_STRING);
4492 x_default_parameter (f, parameters, Qscreen_gamma, Qnil,
4493 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
4494 x_default_parameter (f, parameters, Qline_spacing, Qnil,
4495 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
4496 x_default_parameter (f, parameters, Qleft_fringe, Qnil,
4497 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
4498 x_default_parameter (f, parameters, Qright_fringe, Qnil,
4499 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
4502 /* Init faces before x_default_parameter is called for scroll-bar
4503 parameters because that function calls x_set_scroll_bar_width,
4504 which calls change_frame_size, which calls Fset_window_buffer,
4505 which runs hooks, which call Fvertical_motion. At the end, we
4506 end up in init_iterator with a null face cache, which should not
4507 happen. */
4508 init_frame_faces (f);
4510 x_default_parameter (f, parameters, Qmenu_bar_lines, make_number (1),
4511 "menuBar", "MenuBar", RES_TYPE_NUMBER);
4512 x_default_parameter (f, parameters, Qtool_bar_lines, make_number (1),
4513 "toolBar", "ToolBar", RES_TYPE_NUMBER);
4515 x_default_parameter (f, parameters, Qbuffer_predicate, Qnil,
4516 "bufferPredicate", "BufferPredicate", RES_TYPE_SYMBOL);
4517 x_default_parameter (f, parameters, Qtitle, Qnil,
4518 "title", "Title", RES_TYPE_STRING);
4519 x_default_parameter (f, parameters, Qfullscreen, Qnil,
4520 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
4522 f->output_data.w32->dwStyle = WS_OVERLAPPEDWINDOW;
4523 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
4525 f->output_data.w32->text_cursor = w32_load_cursor (IDC_IBEAM);
4526 f->output_data.w32->nontext_cursor = w32_load_cursor (IDC_ARROW);
4527 f->output_data.w32->modeline_cursor = w32_load_cursor (IDC_ARROW);
4528 f->output_data.w32->hand_cursor = w32_load_cursor (IDC_HAND);
4529 f->output_data.w32->hourglass_cursor = w32_load_cursor (IDC_WAIT);
4530 f->output_data.w32->horizontal_drag_cursor = w32_load_cursor (IDC_SIZEWE);
4532 f->output_data.w32->current_cursor = f->output_data.w32->nontext_cursor;
4534 window_prompting = x_figure_window_size (f, parameters, 1);
4536 tem = w32_get_arg (parameters, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
4537 f->no_split = minibuffer_only || EQ (tem, Qt);
4539 w32_window (f, window_prompting, minibuffer_only);
4540 x_icon (f, parameters);
4542 x_make_gc (f);
4544 /* Now consider the frame official. */
4545 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
4546 Vframe_list = Fcons (frame, Vframe_list);
4548 /* We need to do this after creating the window, so that the
4549 icon-creation functions can say whose icon they're describing. */
4550 x_default_parameter (f, parameters, Qicon_type, Qnil,
4551 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
4553 x_default_parameter (f, parameters, Qauto_raise, Qnil,
4554 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4555 x_default_parameter (f, parameters, Qauto_lower, Qnil,
4556 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4557 x_default_parameter (f, parameters, Qcursor_type, Qbox,
4558 "cursorType", "CursorType", RES_TYPE_SYMBOL);
4559 x_default_parameter (f, parameters, Qscroll_bar_width, Qnil,
4560 "scrollBarWidth", "ScrollBarWidth", RES_TYPE_NUMBER);
4562 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
4563 Change will not be effected unless different from the current
4564 FRAME_LINES (f). */
4565 width = FRAME_COLS (f);
4566 height = FRAME_LINES (f);
4568 FRAME_LINES (f) = 0;
4569 SET_FRAME_COLS (f, 0);
4570 change_frame_size (f, height, width, 1, 0, 0);
4572 /* Tell the server what size and position, etc, we want, and how
4573 badly we want them. This should be done after we have the menu
4574 bar so that its size can be taken into account. */
4575 BLOCK_INPUT;
4576 x_wm_set_size_hint (f, window_prompting, 0);
4577 UNBLOCK_INPUT;
4579 /* Make the window appear on the frame and enable display, unless
4580 the caller says not to. However, with explicit parent, Emacs
4581 cannot control visibility, so don't try. */
4582 if (! f->output_data.w32->explicit_parent)
4584 Lisp_Object visibility;
4586 visibility = w32_get_arg (parameters, Qvisibility, 0, 0, RES_TYPE_SYMBOL);
4587 if (EQ (visibility, Qunbound))
4588 visibility = Qt;
4590 if (EQ (visibility, Qicon))
4591 x_iconify_frame (f);
4592 else if (! NILP (visibility))
4593 x_make_frame_visible (f);
4594 else
4595 /* Must have been Qnil. */
4599 /* Initialize `default-minibuffer-frame' in case this is the first
4600 frame on this terminal. */
4601 if (FRAME_HAS_MINIBUF_P (f)
4602 && (!FRAMEP (kb->Vdefault_minibuffer_frame)
4603 || !FRAME_LIVE_P (XFRAME (kb->Vdefault_minibuffer_frame))))
4604 kb->Vdefault_minibuffer_frame = frame;
4606 /* All remaining specified parameters, which have not been "used"
4607 by x_get_arg and friends, now go in the misc. alist of the frame. */
4608 for (tem = parameters; CONSP (tem); tem = XCDR (tem))
4609 if (CONSP (XCAR (tem)) && !NILP (XCAR (XCAR (tem))))
4610 f->param_alist = Fcons (XCAR (tem), f->param_alist);
4612 UNGCPRO;
4614 /* Make sure windows on this frame appear in calls to next-window
4615 and similar functions. */
4616 Vwindow_list = Qnil;
4618 return unbind_to (count, frame);
4621 /* FRAME is used only to get a handle on the X display. We don't pass the
4622 display info directly because we're called from frame.c, which doesn't
4623 know about that structure. */
4624 Lisp_Object
4625 x_get_focus_frame (frame)
4626 struct frame *frame;
4628 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (frame);
4629 Lisp_Object xfocus;
4630 if (! dpyinfo->w32_focus_frame)
4631 return Qnil;
4633 XSETFRAME (xfocus, dpyinfo->w32_focus_frame);
4634 return xfocus;
4637 DEFUN ("x-focus-frame", Fx_focus_frame, Sx_focus_frame, 1, 1, 0,
4638 doc: /* Give FRAME input focus, raising to foreground if necessary. */)
4639 (frame)
4640 Lisp_Object frame;
4642 x_focus_on_frame (check_x_frame (frame));
4643 return Qnil;
4647 /* The font conversion stuff between x and w32 */
4649 /* X font string is as follows (from faces.el)
4650 * (let ((- "[-?]")
4651 * (foundry "[^-]+")
4652 * (family "[^-]+")
4653 * (weight "\\(bold\\|demibold\\|medium\\)") ; 1
4654 * (weight\? "\\([^-]*\\)") ; 1
4655 * (slant "\\([ior]\\)") ; 2
4656 * (slant\? "\\([^-]?\\)") ; 2
4657 * (swidth "\\([^-]*\\)") ; 3
4658 * (adstyle "[^-]*") ; 4
4659 * (pixelsize "[0-9]+")
4660 * (pointsize "[0-9][0-9]+")
4661 * (resx "[0-9][0-9]+")
4662 * (resy "[0-9][0-9]+")
4663 * (spacing "[cmp?*]")
4664 * (avgwidth "[0-9]+")
4665 * (registry "[^-]+")
4666 * (encoding "[^-]+")
4670 static LONG
4671 x_to_w32_weight (lpw)
4672 char * lpw;
4674 if (!lpw) return (FW_DONTCARE);
4676 if (xstrcasecmp (lpw, "heavy") == 0) return FW_HEAVY;
4677 else if (xstrcasecmp (lpw, "extrabold") == 0) return FW_EXTRABOLD;
4678 else if (xstrcasecmp (lpw, "bold") == 0) return FW_BOLD;
4679 else if (xstrcasecmp (lpw, "demibold") == 0) return FW_SEMIBOLD;
4680 else if (xstrcasecmp (lpw, "semibold") == 0) return FW_SEMIBOLD;
4681 else if (xstrcasecmp (lpw, "medium") == 0) return FW_MEDIUM;
4682 else if (xstrcasecmp (lpw, "normal") == 0) return FW_NORMAL;
4683 else if (xstrcasecmp (lpw, "light") == 0) return FW_LIGHT;
4684 else if (xstrcasecmp (lpw, "extralight") == 0) return FW_EXTRALIGHT;
4685 else if (xstrcasecmp (lpw, "thin") == 0) return FW_THIN;
4686 else
4687 return FW_DONTCARE;
4691 static char *
4692 w32_to_x_weight (fnweight)
4693 int fnweight;
4695 if (fnweight >= FW_HEAVY) return "heavy";
4696 if (fnweight >= FW_EXTRABOLD) return "extrabold";
4697 if (fnweight >= FW_BOLD) return "bold";
4698 if (fnweight >= FW_SEMIBOLD) return "demibold";
4699 if (fnweight >= FW_MEDIUM) return "medium";
4700 if (fnweight >= FW_NORMAL) return "normal";
4701 if (fnweight >= FW_LIGHT) return "light";
4702 if (fnweight >= FW_EXTRALIGHT) return "extralight";
4703 if (fnweight >= FW_THIN) return "thin";
4704 else
4705 return "*";
4708 LONG
4709 x_to_w32_charset (lpcs)
4710 char * lpcs;
4712 Lisp_Object this_entry, w32_charset;
4713 char *charset;
4714 int len = strlen (lpcs);
4716 /* Support "*-#nnn" format for unknown charsets. */
4717 if (strncmp (lpcs, "*-#", 3) == 0)
4718 return atoi (lpcs + 3);
4720 /* All Windows fonts qualify as unicode. */
4721 if (!strncmp (lpcs, "iso10646", 8))
4722 return DEFAULT_CHARSET;
4724 /* Handle wildcards by ignoring them; eg. treat "big5*-*" as "big5". */
4725 charset = alloca (len + 1);
4726 strcpy (charset, lpcs);
4727 lpcs = strchr (charset, '*');
4728 if (lpcs)
4729 *lpcs = '\0';
4731 /* Look through w32-charset-info-alist for the character set.
4732 Format of each entry is
4733 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
4735 this_entry = Fassoc (build_string (charset), Vw32_charset_info_alist);
4737 if (NILP (this_entry))
4739 /* At startup, we want iso8859-1 fonts to come up properly. */
4740 if (xstrcasecmp (charset, "iso8859-1") == 0)
4741 return ANSI_CHARSET;
4742 else
4743 return DEFAULT_CHARSET;
4746 w32_charset = Fcar (Fcdr (this_entry));
4748 /* Translate Lisp symbol to number. */
4749 if (EQ (w32_charset, Qw32_charset_ansi))
4750 return ANSI_CHARSET;
4751 if (EQ (w32_charset, Qw32_charset_symbol))
4752 return SYMBOL_CHARSET;
4753 if (EQ (w32_charset, Qw32_charset_shiftjis))
4754 return SHIFTJIS_CHARSET;
4755 if (EQ (w32_charset, Qw32_charset_hangeul))
4756 return HANGEUL_CHARSET;
4757 if (EQ (w32_charset, Qw32_charset_chinesebig5))
4758 return CHINESEBIG5_CHARSET;
4759 if (EQ (w32_charset, Qw32_charset_gb2312))
4760 return GB2312_CHARSET;
4761 if (EQ (w32_charset, Qw32_charset_oem))
4762 return OEM_CHARSET;
4763 #ifdef JOHAB_CHARSET
4764 if (EQ (w32_charset, Qw32_charset_johab))
4765 return JOHAB_CHARSET;
4766 if (EQ (w32_charset, Qw32_charset_easteurope))
4767 return EASTEUROPE_CHARSET;
4768 if (EQ (w32_charset, Qw32_charset_turkish))
4769 return TURKISH_CHARSET;
4770 if (EQ (w32_charset, Qw32_charset_baltic))
4771 return BALTIC_CHARSET;
4772 if (EQ (w32_charset, Qw32_charset_russian))
4773 return RUSSIAN_CHARSET;
4774 if (EQ (w32_charset, Qw32_charset_arabic))
4775 return ARABIC_CHARSET;
4776 if (EQ (w32_charset, Qw32_charset_greek))
4777 return GREEK_CHARSET;
4778 if (EQ (w32_charset, Qw32_charset_hebrew))
4779 return HEBREW_CHARSET;
4780 if (EQ (w32_charset, Qw32_charset_vietnamese))
4781 return VIETNAMESE_CHARSET;
4782 if (EQ (w32_charset, Qw32_charset_thai))
4783 return THAI_CHARSET;
4784 if (EQ (w32_charset, Qw32_charset_mac))
4785 return MAC_CHARSET;
4786 #endif /* JOHAB_CHARSET */
4787 #ifdef UNICODE_CHARSET
4788 if (EQ (w32_charset, Qw32_charset_unicode))
4789 return UNICODE_CHARSET;
4790 #endif
4792 return DEFAULT_CHARSET;
4796 char *
4797 w32_to_x_charset (fncharset, matching)
4798 int fncharset;
4799 char *matching;
4801 static char buf[32];
4802 Lisp_Object charset_type;
4803 int match_len = 0;
4805 if (matching)
4807 /* If fully specified, accept it as it is. Otherwise use a
4808 substring match. */
4809 char *wildcard = strchr (matching, '*');
4810 if (wildcard)
4811 *wildcard = '\0';
4812 else if (strchr (matching, '-'))
4813 return matching;
4815 match_len = strlen (matching);
4818 switch (fncharset)
4820 case ANSI_CHARSET:
4821 /* Handle startup case of w32-charset-info-alist not
4822 being set up yet. */
4823 if (NILP (Vw32_charset_info_alist))
4824 return "iso8859-1";
4825 charset_type = Qw32_charset_ansi;
4826 break;
4827 case DEFAULT_CHARSET:
4828 charset_type = Qw32_charset_default;
4829 break;
4830 case SYMBOL_CHARSET:
4831 charset_type = Qw32_charset_symbol;
4832 break;
4833 case SHIFTJIS_CHARSET:
4834 charset_type = Qw32_charset_shiftjis;
4835 break;
4836 case HANGEUL_CHARSET:
4837 charset_type = Qw32_charset_hangeul;
4838 break;
4839 case GB2312_CHARSET:
4840 charset_type = Qw32_charset_gb2312;
4841 break;
4842 case CHINESEBIG5_CHARSET:
4843 charset_type = Qw32_charset_chinesebig5;
4844 break;
4845 case OEM_CHARSET:
4846 charset_type = Qw32_charset_oem;
4847 break;
4849 /* More recent versions of Windows (95 and NT4.0) define more
4850 character sets. */
4851 #ifdef EASTEUROPE_CHARSET
4852 case EASTEUROPE_CHARSET:
4853 charset_type = Qw32_charset_easteurope;
4854 break;
4855 case TURKISH_CHARSET:
4856 charset_type = Qw32_charset_turkish;
4857 break;
4858 case BALTIC_CHARSET:
4859 charset_type = Qw32_charset_baltic;
4860 break;
4861 case RUSSIAN_CHARSET:
4862 charset_type = Qw32_charset_russian;
4863 break;
4864 case ARABIC_CHARSET:
4865 charset_type = Qw32_charset_arabic;
4866 break;
4867 case GREEK_CHARSET:
4868 charset_type = Qw32_charset_greek;
4869 break;
4870 case HEBREW_CHARSET:
4871 charset_type = Qw32_charset_hebrew;
4872 break;
4873 case VIETNAMESE_CHARSET:
4874 charset_type = Qw32_charset_vietnamese;
4875 break;
4876 case THAI_CHARSET:
4877 charset_type = Qw32_charset_thai;
4878 break;
4879 case MAC_CHARSET:
4880 charset_type = Qw32_charset_mac;
4881 break;
4882 case JOHAB_CHARSET:
4883 charset_type = Qw32_charset_johab;
4884 break;
4885 #endif
4887 #ifdef UNICODE_CHARSET
4888 case UNICODE_CHARSET:
4889 charset_type = Qw32_charset_unicode;
4890 break;
4891 #endif
4892 default:
4893 /* Encode numerical value of unknown charset. */
4894 sprintf (buf, "*-#%u", fncharset);
4895 return buf;
4899 Lisp_Object rest;
4900 char * best_match = NULL;
4901 int matching_found = 0;
4903 /* Look through w32-charset-info-alist for the character set.
4904 Prefer ISO codepages, and prefer lower numbers in the ISO
4905 range. Only return charsets for codepages which are installed.
4907 Format of each entry is
4908 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
4910 for (rest = Vw32_charset_info_alist; CONSP (rest); rest = XCDR (rest))
4912 char * x_charset;
4913 Lisp_Object w32_charset;
4914 Lisp_Object codepage;
4916 Lisp_Object this_entry = XCAR (rest);
4918 /* Skip invalid entries in alist. */
4919 if (!CONSP (this_entry) || !STRINGP (XCAR (this_entry))
4920 || !CONSP (XCDR (this_entry))
4921 || !SYMBOLP (XCAR (XCDR (this_entry))))
4922 continue;
4924 x_charset = SDATA (XCAR (this_entry));
4925 w32_charset = XCAR (XCDR (this_entry));
4926 codepage = XCDR (XCDR (this_entry));
4928 /* Look for Same charset and a valid codepage (or non-int
4929 which means ignore). */
4930 if (EQ (w32_charset, charset_type)
4931 && (!INTEGERP (codepage) || XINT (codepage) == CP_DEFAULT
4932 || IsValidCodePage (XINT (codepage))))
4934 /* If we don't have a match already, then this is the
4935 best. */
4936 if (!best_match)
4938 best_match = x_charset;
4939 if (matching && !strnicmp (x_charset, matching, match_len))
4940 matching_found = 1;
4942 /* If we already found a match for MATCHING, then
4943 only consider other matches. */
4944 else if (matching_found
4945 && strnicmp (x_charset, matching, match_len))
4946 continue;
4947 /* If this matches what we want, and the best so far doesn't,
4948 then this is better. */
4949 else if (!matching_found && matching
4950 && !strnicmp (x_charset, matching, match_len))
4952 best_match = x_charset;
4953 matching_found = 1;
4955 /* If this is fully specified, and the best so far isn't,
4956 then this is better. */
4957 else if ((!strchr (best_match, '-') && strchr (x_charset, '-'))
4958 /* If this is an ISO codepage, and the best so far isn't,
4959 then this is better, but only if it fully specifies the
4960 encoding. */
4961 || (strnicmp (best_match, "iso", 3) != 0
4962 && strnicmp (x_charset, "iso", 3) == 0
4963 && strchr (x_charset, '-')))
4964 best_match = x_charset;
4965 /* If both are ISO8859 codepages, choose the one with the
4966 lowest number in the encoding field. */
4967 else if (strnicmp (best_match, "iso8859-", 8) == 0
4968 && strnicmp (x_charset, "iso8859-", 8) == 0)
4970 int best_enc = atoi (best_match + 8);
4971 int this_enc = atoi (x_charset + 8);
4972 if (this_enc > 0 && this_enc < best_enc)
4973 best_match = x_charset;
4978 /* If no match, encode the numeric value. */
4979 if (!best_match)
4981 sprintf (buf, "*-#%u", fncharset);
4982 return buf;
4985 strncpy (buf, best_match, 31);
4986 /* If the charset is not fully specified, put -0 on the end. */
4987 if (!strchr (best_match, '-'))
4989 int pos = strlen (best_match);
4990 /* Charset specifiers shouldn't be very long. If it is a made
4991 up one, truncating it should not do any harm since it isn't
4992 recognized anyway. */
4993 if (pos > 29)
4994 pos = 29;
4995 strcpy (buf + pos, "-0");
4997 buf[31] = '\0';
4998 return buf;
5003 /* Return all the X charsets that map to a font. */
5004 static Lisp_Object
5005 w32_to_all_x_charsets (fncharset)
5006 int fncharset;
5008 static char buf[32];
5009 Lisp_Object charset_type;
5010 Lisp_Object retval = Qnil;
5012 switch (fncharset)
5014 case ANSI_CHARSET:
5015 /* Handle startup case of w32-charset-info-alist not
5016 being set up yet. */
5017 if (NILP (Vw32_charset_info_alist))
5018 return Fcons (build_string ("iso8859-1"), Qnil);
5020 charset_type = Qw32_charset_ansi;
5021 break;
5022 case DEFAULT_CHARSET:
5023 charset_type = Qw32_charset_default;
5024 break;
5025 case SYMBOL_CHARSET:
5026 charset_type = Qw32_charset_symbol;
5027 break;
5028 case SHIFTJIS_CHARSET:
5029 charset_type = Qw32_charset_shiftjis;
5030 break;
5031 case HANGEUL_CHARSET:
5032 charset_type = Qw32_charset_hangeul;
5033 break;
5034 case GB2312_CHARSET:
5035 charset_type = Qw32_charset_gb2312;
5036 break;
5037 case CHINESEBIG5_CHARSET:
5038 charset_type = Qw32_charset_chinesebig5;
5039 break;
5040 case OEM_CHARSET:
5041 charset_type = Qw32_charset_oem;
5042 break;
5044 /* More recent versions of Windows (95 and NT4.0) define more
5045 character sets. */
5046 #ifdef EASTEUROPE_CHARSET
5047 case EASTEUROPE_CHARSET:
5048 charset_type = Qw32_charset_easteurope;
5049 break;
5050 case TURKISH_CHARSET:
5051 charset_type = Qw32_charset_turkish;
5052 break;
5053 case BALTIC_CHARSET:
5054 charset_type = Qw32_charset_baltic;
5055 break;
5056 case RUSSIAN_CHARSET:
5057 charset_type = Qw32_charset_russian;
5058 break;
5059 case ARABIC_CHARSET:
5060 charset_type = Qw32_charset_arabic;
5061 break;
5062 case GREEK_CHARSET:
5063 charset_type = Qw32_charset_greek;
5064 break;
5065 case HEBREW_CHARSET:
5066 charset_type = Qw32_charset_hebrew;
5067 break;
5068 case VIETNAMESE_CHARSET:
5069 charset_type = Qw32_charset_vietnamese;
5070 break;
5071 case THAI_CHARSET:
5072 charset_type = Qw32_charset_thai;
5073 break;
5074 case MAC_CHARSET:
5075 charset_type = Qw32_charset_mac;
5076 break;
5077 case JOHAB_CHARSET:
5078 charset_type = Qw32_charset_johab;
5079 break;
5080 #endif
5082 #ifdef UNICODE_CHARSET
5083 case UNICODE_CHARSET:
5084 charset_type = Qw32_charset_unicode;
5085 break;
5086 #endif
5087 default:
5088 /* Encode numerical value of unknown charset. */
5089 sprintf (buf, "*-#%u", fncharset);
5090 return Fcons (build_string (buf), Qnil);
5094 Lisp_Object rest;
5095 /* Look through w32-charset-info-alist for the character set.
5096 Only return fully specified charsets for codepages which are
5097 installed.
5099 Format of each entry in Vw32_charset_info_alist is
5100 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
5102 for (rest = Vw32_charset_info_alist; CONSP (rest); rest = XCDR (rest))
5104 Lisp_Object x_charset;
5105 Lisp_Object w32_charset;
5106 Lisp_Object codepage;
5108 Lisp_Object this_entry = XCAR (rest);
5110 /* Skip invalid entries in alist. */
5111 if (!CONSP (this_entry) || !STRINGP (XCAR (this_entry))
5112 || !CONSP (XCDR (this_entry))
5113 || !SYMBOLP (XCAR (XCDR (this_entry))))
5114 continue;
5116 x_charset = XCAR (this_entry);
5117 w32_charset = XCAR (XCDR (this_entry));
5118 codepage = XCDR (XCDR (this_entry));
5120 if (!strchr (SDATA (x_charset), '-'))
5121 continue;
5123 /* Look for Same charset and a valid codepage (or non-int
5124 which means ignore). */
5125 if (EQ (w32_charset, charset_type)
5126 && (!INTEGERP (codepage) || XINT (codepage) == CP_DEFAULT
5127 || IsValidCodePage (XINT (codepage))))
5129 retval = Fcons (x_charset, retval);
5133 /* If no match, encode the numeric value. */
5134 if (NILP (retval))
5136 sprintf (buf, "*-#%u", fncharset);
5137 return Fcons (build_string (buf), Qnil);
5140 return retval;
5144 static BOOL
5145 w32_to_x_font (lplogfont, lpxstr, len, specific_charset)
5146 LOGFONT * lplogfont;
5147 char * lpxstr;
5148 int len;
5149 char * specific_charset;
5151 char* fonttype;
5152 char *fontname;
5153 char height_pixels[8];
5154 char height_dpi[8];
5155 char width_pixels[8];
5156 char *fontname_dash;
5157 int display_resy = (int) one_w32_display_info.resy;
5158 int display_resx = (int) one_w32_display_info.resx;
5159 struct coding_system coding;
5161 if (!lpxstr) abort ();
5163 if (!lplogfont)
5164 return FALSE;
5166 if (lplogfont->lfOutPrecision == OUT_STRING_PRECIS)
5167 fonttype = "raster";
5168 else if (lplogfont->lfOutPrecision == OUT_STROKE_PRECIS)
5169 fonttype = "outline";
5170 else
5171 fonttype = "unknown";
5173 setup_coding_system (Fcheck_coding_system (Vlocale_coding_system),
5174 &coding);
5175 coding.src_multibyte = 0;
5176 coding.dst_multibyte = 1;
5177 coding.mode |= CODING_MODE_LAST_BLOCK;
5178 /* We explicitely disable composition handling because selection
5179 data should not contain any composition sequence. */
5180 coding.common_flags &= ~CODING_ANNOTATION_MASK;
5182 coding.dst_bytes = LF_FACESIZE * 2;
5183 coding.destination = (unsigned char *) xmalloc (coding.dst_bytes + 1);
5184 decode_coding_c_string (&coding, lplogfont->lfFaceName,
5185 strlen(lplogfont->lfFaceName), Qnil);
5186 fontname = coding.destination;
5188 *(fontname + coding.produced) = '\0';
5190 /* Replace dashes with underscores so the dashes are not
5191 misinterpreted. */
5192 fontname_dash = fontname;
5193 while (fontname_dash = strchr (fontname_dash, '-'))
5194 *fontname_dash = '_';
5196 if (lplogfont->lfHeight)
5198 sprintf (height_pixels, "%u", eabs (lplogfont->lfHeight));
5199 sprintf (height_dpi, "%u",
5200 eabs (lplogfont->lfHeight) * 720 / display_resy);
5202 else
5204 strcpy (height_pixels, "*");
5205 strcpy (height_dpi, "*");
5208 #if 0 /* Never put the width in the xlfd. It fails on fonts with
5209 double-width characters. */
5210 if (lplogfont->lfWidth)
5211 sprintf (width_pixels, "%u", lplogfont->lfWidth * 10);
5212 else
5213 #endif
5214 strcpy (width_pixels, "*");
5216 _snprintf (lpxstr, len - 1,
5217 "-%s-%s-%s-%c-normal-normal-%s-%s-%d-%d-%c-%s-%s",
5218 fonttype, /* foundry */
5219 fontname, /* family */
5220 w32_to_x_weight (lplogfont->lfWeight), /* weight */
5221 lplogfont->lfItalic?'i':'r', /* slant */
5222 /* setwidth name */
5223 /* add style name */
5224 height_pixels, /* pixel size */
5225 height_dpi, /* point size */
5226 display_resx, /* resx */
5227 display_resy, /* resy */
5228 ((lplogfont->lfPitchAndFamily & 0x3) == VARIABLE_PITCH)
5229 ? 'p' : 'c', /* spacing */
5230 width_pixels, /* avg width */
5231 w32_to_x_charset (lplogfont->lfCharSet, specific_charset)
5232 /* charset registry and encoding */
5235 lpxstr[len - 1] = 0; /* just to be sure */
5236 return (TRUE);
5239 static BOOL
5240 x_to_w32_font (lpxstr, lplogfont)
5241 char * lpxstr;
5242 LOGFONT * lplogfont;
5244 struct coding_system coding;
5246 if (!lplogfont) return (FALSE);
5248 memset (lplogfont, 0, sizeof (*lplogfont));
5250 /* Set default value for each field. */
5251 #if 1
5252 lplogfont->lfOutPrecision = OUT_DEFAULT_PRECIS;
5253 lplogfont->lfClipPrecision = CLIP_DEFAULT_PRECIS;
5254 lplogfont->lfQuality = DEFAULT_QUALITY;
5255 #else
5256 /* go for maximum quality */
5257 lplogfont->lfOutPrecision = OUT_STROKE_PRECIS;
5258 lplogfont->lfClipPrecision = CLIP_STROKE_PRECIS;
5259 lplogfont->lfQuality = PROOF_QUALITY;
5260 #endif
5262 lplogfont->lfCharSet = DEFAULT_CHARSET;
5263 lplogfont->lfWeight = FW_DONTCARE;
5264 lplogfont->lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
5266 if (!lpxstr)
5267 return FALSE;
5269 /* Provide a simple escape mechanism for specifying Windows font names
5270 * directly -- if font spec does not beginning with '-', assume this
5271 * format:
5272 * "<font name>[:height in pixels[:width in pixels[:weight]]]"
5275 if (*lpxstr == '-')
5277 int fields, tem;
5278 char name[50], weight[20], slant, pitch, pixels[10], height[10],
5279 width[10], resy[10], remainder[50];
5280 char * encoding;
5281 int dpi = (int) one_w32_display_info.resy;
5283 fields = sscanf (lpxstr,
5284 "-%*[^-]-%49[^-]-%19[^-]-%c-%*[^-]-%*[^-]-%9[^-]-%9[^-]-%*[^-]-%9[^-]-%c-%9[^-]-%49s",
5285 name, weight, &slant, pixels, height, resy, &pitch, width, remainder);
5286 if (fields == EOF)
5287 return (FALSE);
5289 /* In the general case when wildcards cover more than one field,
5290 we don't know which field is which, so don't fill any in.
5291 However, we need to cope with this particular form, which is
5292 generated by font_list_1 (invoked by try_font_list):
5293 "-raster-6x10-*-gb2312*-*"
5294 and make sure to correctly parse the charset field. */
5295 if (fields == 3)
5297 fields = sscanf (lpxstr,
5298 "-%*[^-]-%49[^-]-*-%49s",
5299 name, remainder);
5301 else if (fields < 9)
5303 fields = 0;
5304 remainder[0] = 0;
5307 if (fields > 0 && name[0] != '*')
5309 Lisp_Object string = build_string (name);
5310 setup_coding_system
5311 (Fcheck_coding_system (Vlocale_coding_system), &coding);
5312 coding.mode |= (CODING_MODE_SAFE_ENCODING | CODING_MODE_LAST_BLOCK);
5313 /* Disable composition/charset annotation. */
5314 coding.common_flags &= ~CODING_ANNOTATION_MASK;
5315 coding.dst_bytes = SCHARS (string) * 2;
5317 coding.destination = (unsigned char *) xmalloc (coding.dst_bytes);
5318 encode_coding_object (&coding, string, 0, 0,
5319 SCHARS (string), SBYTES (string), Qnil);
5320 if (coding.produced >= LF_FACESIZE)
5321 coding.produced = LF_FACESIZE - 1;
5323 coding.destination[coding.produced] = '\0';
5325 strcpy (lplogfont->lfFaceName, coding.destination);
5326 xfree (coding.destination);
5328 else
5330 lplogfont->lfFaceName[0] = '\0';
5333 fields--;
5335 lplogfont->lfWeight = x_to_w32_weight ((fields > 0 ? weight : ""));
5337 fields--;
5339 lplogfont->lfItalic = (fields > 0 && slant == 'i');
5341 fields--;
5343 if (fields > 0 && pixels[0] != '*')
5344 lplogfont->lfHeight = atoi (pixels);
5346 fields--;
5347 fields--;
5348 if (fields > 0 && resy[0] != '*')
5350 tem = atoi (resy);
5351 if (tem > 0) dpi = tem;
5354 if (fields > -1 && lplogfont->lfHeight == 0 && height[0] != '*')
5355 lplogfont->lfHeight = atoi (height) * dpi / 720;
5357 if (fields > 0)
5359 if (pitch == 'p')
5360 lplogfont->lfPitchAndFamily = VARIABLE_PITCH | FF_DONTCARE;
5361 else if (pitch == 'c')
5362 lplogfont->lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
5365 fields--;
5367 if (fields > 0 && width[0] != '*')
5368 lplogfont->lfWidth = atoi (width) / 10;
5370 fields--;
5372 /* Strip the trailing '-' if present. (it shouldn't be, as it
5373 fails the test against xlfd-tight-regexp in fontset.el). */
5375 int len = strlen (remainder);
5376 if (len > 0 && remainder[len-1] == '-')
5377 remainder[len-1] = 0;
5379 encoding = remainder;
5380 #if 0
5381 if (strncmp (encoding, "*-", 2) == 0)
5382 encoding += 2;
5383 #endif
5384 lplogfont->lfCharSet = x_to_w32_charset (encoding);
5386 else
5388 int fields;
5389 char name[100], height[10], width[10], weight[20];
5391 fields = sscanf (lpxstr,
5392 "%99[^:]:%9[^:]:%9[^:]:%19s",
5393 name, height, width, weight);
5395 if (fields == EOF) return (FALSE);
5397 if (fields > 0)
5399 strncpy (lplogfont->lfFaceName, name, LF_FACESIZE);
5400 lplogfont->lfFaceName[LF_FACESIZE-1] = 0;
5402 else
5404 lplogfont->lfFaceName[0] = 0;
5407 fields--;
5409 if (fields > 0)
5410 lplogfont->lfHeight = atoi (height);
5412 fields--;
5414 if (fields > 0)
5415 lplogfont->lfWidth = atoi (width);
5417 fields--;
5419 lplogfont->lfWeight = x_to_w32_weight ((fields > 0 ? weight : ""));
5422 /* This makes TrueType fonts work better. */
5423 lplogfont->lfHeight = - eabs (lplogfont->lfHeight);
5425 return (TRUE);
5430 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
5431 doc: /* Internal function called by `color-defined-p', which see. */)
5432 (color, frame)
5433 Lisp_Object color, frame;
5435 XColor foo;
5436 FRAME_PTR f = check_x_frame (frame);
5438 CHECK_STRING (color);
5440 if (w32_defined_color (f, SDATA (color), &foo, 0))
5441 return Qt;
5442 else
5443 return Qnil;
5446 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
5447 doc: /* Internal function called by `color-values', which see. */)
5448 (color, frame)
5449 Lisp_Object color, frame;
5451 XColor foo;
5452 FRAME_PTR f = check_x_frame (frame);
5454 CHECK_STRING (color);
5456 if (w32_defined_color (f, SDATA (color), &foo, 0))
5457 return list3 (make_number ((GetRValue (foo.pixel) << 8)
5458 | GetRValue (foo.pixel)),
5459 make_number ((GetGValue (foo.pixel) << 8)
5460 | GetGValue (foo.pixel)),
5461 make_number ((GetBValue (foo.pixel) << 8)
5462 | GetBValue (foo.pixel)));
5463 else
5464 return Qnil;
5467 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
5468 doc: /* Internal function called by `display-color-p', which see. */)
5469 (display)
5470 Lisp_Object display;
5472 struct w32_display_info *dpyinfo = check_x_display_info (display);
5474 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 2)
5475 return Qnil;
5477 return Qt;
5480 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p,
5481 Sx_display_grayscale_p, 0, 1, 0,
5482 doc: /* Return t if DISPLAY supports shades of gray.
5483 Note that color displays do support shades of gray.
5484 The optional argument DISPLAY specifies which display to ask about.
5485 DISPLAY should be either a frame or a display name (a string).
5486 If omitted or nil, that stands for the selected frame's display. */)
5487 (display)
5488 Lisp_Object display;
5490 struct w32_display_info *dpyinfo = check_x_display_info (display);
5492 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 1)
5493 return Qnil;
5495 return Qt;
5498 DEFUN ("x-display-pixel-width", Fx_display_pixel_width,
5499 Sx_display_pixel_width, 0, 1, 0,
5500 doc: /* Return the width in pixels of DISPLAY.
5501 The optional argument DISPLAY specifies which display to ask about.
5502 DISPLAY should be either a frame or a display name (a string).
5503 If omitted or nil, that stands for the selected frame's display. */)
5504 (display)
5505 Lisp_Object display;
5507 struct w32_display_info *dpyinfo = check_x_display_info (display);
5509 return make_number (dpyinfo->width);
5512 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
5513 Sx_display_pixel_height, 0, 1, 0,
5514 doc: /* Return the height in pixels of DISPLAY.
5515 The optional argument DISPLAY specifies which display to ask about.
5516 DISPLAY should be either a frame or a display name (a string).
5517 If omitted or nil, that stands for the selected frame's display. */)
5518 (display)
5519 Lisp_Object display;
5521 struct w32_display_info *dpyinfo = check_x_display_info (display);
5523 return make_number (dpyinfo->height);
5526 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
5527 0, 1, 0,
5528 doc: /* Return the number of bitplanes of DISPLAY.
5529 The optional argument DISPLAY specifies which display to ask about.
5530 DISPLAY should be either a frame or a display name (a string).
5531 If omitted or nil, that stands for the selected frame's display. */)
5532 (display)
5533 Lisp_Object display;
5535 struct w32_display_info *dpyinfo = check_x_display_info (display);
5537 return make_number (dpyinfo->n_planes * dpyinfo->n_cbits);
5540 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
5541 0, 1, 0,
5542 doc: /* Return the number of color cells of DISPLAY.
5543 The optional argument DISPLAY specifies which display to ask about.
5544 DISPLAY should be either a frame or a display name (a string).
5545 If omitted or nil, that stands for the selected frame's display. */)
5546 (display)
5547 Lisp_Object display;
5549 struct w32_display_info *dpyinfo = check_x_display_info (display);
5550 HDC hdc;
5551 int cap;
5553 hdc = GetDC (dpyinfo->root_window);
5554 if (dpyinfo->has_palette)
5555 cap = GetDeviceCaps (hdc, SIZEPALETTE);
5556 else
5557 cap = GetDeviceCaps (hdc, NUMCOLORS);
5559 /* We force 24+ bit depths to 24-bit, both to prevent an overflow
5560 and because probably is more meaningful on Windows anyway */
5561 if (cap < 0)
5562 cap = 1 << min (dpyinfo->n_planes * dpyinfo->n_cbits, 24);
5564 ReleaseDC (dpyinfo->root_window, hdc);
5566 return make_number (cap);
5569 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
5570 Sx_server_max_request_size,
5571 0, 1, 0,
5572 doc: /* Return the maximum request size of the server of DISPLAY.
5573 The optional argument DISPLAY specifies which display to ask about.
5574 DISPLAY should be either a frame or a display name (a string).
5575 If omitted or nil, that stands for the selected frame's display. */)
5576 (display)
5577 Lisp_Object display;
5579 struct w32_display_info *dpyinfo = check_x_display_info (display);
5581 return make_number (1);
5584 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
5585 doc: /* Return the "vendor ID" string of the W32 system (Microsoft).
5586 The optional argument DISPLAY specifies which display to ask about.
5587 DISPLAY should be either a frame or a display name (a string).
5588 If omitted or nil, that stands for the selected frame's display. */)
5589 (display)
5590 Lisp_Object display;
5592 return build_string ("Microsoft Corp.");
5595 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
5596 doc: /* Return the version numbers of the server of DISPLAY.
5597 The value is a list of three integers: the major and minor
5598 version numbers of the X Protocol in use, and the distributor-specific
5599 release number. See also the function `x-server-vendor'.
5601 The optional argument DISPLAY specifies which display to ask about.
5602 DISPLAY should be either a frame or a display name (a string).
5603 If omitted or nil, that stands for the selected frame's display. */)
5604 (display)
5605 Lisp_Object display;
5607 return Fcons (make_number (w32_major_version),
5608 Fcons (make_number (w32_minor_version),
5609 Fcons (make_number (w32_build_number), Qnil)));
5612 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
5613 doc: /* Return the number of screens on the server of DISPLAY.
5614 The optional argument DISPLAY specifies which display to ask about.
5615 DISPLAY should be either a frame or a display name (a string).
5616 If omitted or nil, that stands for the selected frame's display. */)
5617 (display)
5618 Lisp_Object display;
5620 return make_number (1);
5623 DEFUN ("x-display-mm-height", Fx_display_mm_height,
5624 Sx_display_mm_height, 0, 1, 0,
5625 doc: /* Return the height in millimeters of DISPLAY.
5626 The optional argument DISPLAY specifies which display to ask about.
5627 DISPLAY should be either a frame or a display name (a string).
5628 If omitted or nil, that stands for the selected frame's display. */)
5629 (display)
5630 Lisp_Object display;
5632 struct w32_display_info *dpyinfo = check_x_display_info (display);
5633 HDC hdc;
5634 int cap;
5636 hdc = GetDC (dpyinfo->root_window);
5638 cap = GetDeviceCaps (hdc, VERTSIZE);
5640 ReleaseDC (dpyinfo->root_window, hdc);
5642 return make_number (cap);
5645 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
5646 doc: /* Return the width in millimeters of DISPLAY.
5647 The optional argument DISPLAY specifies which display to ask about.
5648 DISPLAY should be either a frame or a display name (a string).
5649 If omitted or nil, that stands for the selected frame's display. */)
5650 (display)
5651 Lisp_Object display;
5653 struct w32_display_info *dpyinfo = check_x_display_info (display);
5655 HDC hdc;
5656 int cap;
5658 hdc = GetDC (dpyinfo->root_window);
5660 cap = GetDeviceCaps (hdc, HORZSIZE);
5662 ReleaseDC (dpyinfo->root_window, hdc);
5664 return make_number (cap);
5667 DEFUN ("x-display-backing-store", Fx_display_backing_store,
5668 Sx_display_backing_store, 0, 1, 0,
5669 doc: /* Return an indication of whether DISPLAY does backing store.
5670 The value may be `always', `when-mapped', or `not-useful'.
5671 The optional argument DISPLAY specifies which display to ask about.
5672 DISPLAY should be either a frame or a display name (a string).
5673 If omitted or nil, that stands for the selected frame's display. */)
5674 (display)
5675 Lisp_Object display;
5677 return intern ("not-useful");
5680 DEFUN ("x-display-visual-class", Fx_display_visual_class,
5681 Sx_display_visual_class, 0, 1, 0,
5682 doc: /* Return the visual class of DISPLAY.
5683 The value is one of the symbols `static-gray', `gray-scale',
5684 `static-color', `pseudo-color', `true-color', or `direct-color'.
5686 The optional argument DISPLAY specifies which display to ask about.
5687 DISPLAY should be either a frame or a display name (a string).
5688 If omitted or nil, that stands for the selected frame's display. */)
5689 (display)
5690 Lisp_Object display;
5692 struct w32_display_info *dpyinfo = check_x_display_info (display);
5693 Lisp_Object result = Qnil;
5695 if (dpyinfo->has_palette)
5696 result = intern ("pseudo-color");
5697 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 1)
5698 result = intern ("static-grey");
5699 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 4)
5700 result = intern ("static-color");
5701 else if (dpyinfo->n_planes * dpyinfo->n_cbits > 8)
5702 result = intern ("true-color");
5704 return result;
5707 DEFUN ("x-display-save-under", Fx_display_save_under,
5708 Sx_display_save_under, 0, 1, 0,
5709 doc: /* Return t if DISPLAY supports the save-under feature.
5710 The optional argument DISPLAY specifies which display to ask about.
5711 DISPLAY should be either a frame or a display name (a string).
5712 If omitted or nil, that stands for the selected frame's display. */)
5713 (display)
5714 Lisp_Object display;
5716 return Qnil;
5720 x_pixel_width (f)
5721 register struct frame *f;
5723 return FRAME_PIXEL_WIDTH (f);
5727 x_pixel_height (f)
5728 register struct frame *f;
5730 return FRAME_PIXEL_HEIGHT (f);
5734 x_char_width (f)
5735 register struct frame *f;
5737 return FRAME_COLUMN_WIDTH (f);
5741 x_char_height (f)
5742 register struct frame *f;
5744 return FRAME_LINE_HEIGHT (f);
5748 x_screen_planes (f)
5749 register struct frame *f;
5751 return FRAME_W32_DISPLAY_INFO (f)->n_planes;
5754 /* Return the display structure for the display named NAME.
5755 Open a new connection if necessary. */
5757 struct w32_display_info *
5758 x_display_info_for_name (name)
5759 Lisp_Object name;
5761 Lisp_Object names;
5762 struct w32_display_info *dpyinfo;
5764 CHECK_STRING (name);
5766 for (dpyinfo = &one_w32_display_info, names = w32_display_name_list;
5767 dpyinfo;
5768 dpyinfo = dpyinfo->next, names = XCDR (names))
5770 Lisp_Object tem;
5771 tem = Fstring_equal (XCAR (XCAR (names)), name);
5772 if (!NILP (tem))
5773 return dpyinfo;
5776 /* Use this general default value to start with. */
5777 Vx_resource_name = Vinvocation_name;
5779 validate_x_resource_name ();
5781 dpyinfo = w32_term_init (name, (unsigned char *)0,
5782 (char *) SDATA (Vx_resource_name));
5784 if (dpyinfo == 0)
5785 error ("Cannot connect to server %s", SDATA (name));
5787 w32_in_use = 1;
5788 XSETFASTINT (Vwindow_system_version, 3);
5790 return dpyinfo;
5793 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
5794 1, 3, 0, doc: /* Open a connection to a server.
5795 DISPLAY is the name of the display to connect to.
5796 Optional second arg XRM-STRING is a string of resources in xrdb format.
5797 If the optional third arg MUST-SUCCEED is non-nil,
5798 terminate Emacs if we can't open the connection. */)
5799 (display, xrm_string, must_succeed)
5800 Lisp_Object display, xrm_string, must_succeed;
5802 unsigned char *xrm_option;
5803 struct w32_display_info *dpyinfo;
5805 /* If initialization has already been done, return now to avoid
5806 overwriting critical parts of one_w32_display_info. */
5807 if (w32_in_use)
5808 return Qnil;
5810 CHECK_STRING (display);
5811 if (! NILP (xrm_string))
5812 CHECK_STRING (xrm_string);
5814 #if 0
5815 if (! EQ (Vwindow_system, intern ("w32")))
5816 error ("Not using Microsoft Windows");
5817 #endif
5819 /* Allow color mapping to be defined externally; first look in user's
5820 HOME directory, then in Emacs etc dir for a file called rgb.txt. */
5822 Lisp_Object color_file;
5823 struct gcpro gcpro1;
5825 color_file = build_string ("~/rgb.txt");
5827 GCPRO1 (color_file);
5829 if (NILP (Ffile_readable_p (color_file)))
5830 color_file =
5831 Fexpand_file_name (build_string ("rgb.txt"),
5832 Fsymbol_value (intern ("data-directory")));
5834 Vw32_color_map = Fw32_load_color_file (color_file);
5836 UNGCPRO;
5838 if (NILP (Vw32_color_map))
5839 Vw32_color_map = Fw32_default_color_map ();
5841 /* Merge in system logical colors. */
5842 add_system_logical_colors_to_map (&Vw32_color_map);
5844 if (! NILP (xrm_string))
5845 xrm_option = (unsigned char *) SDATA (xrm_string);
5846 else
5847 xrm_option = (unsigned char *) 0;
5849 /* Use this general default value to start with. */
5850 /* First remove .exe suffix from invocation-name - it looks ugly. */
5852 char basename[ MAX_PATH ], *str;
5854 strcpy (basename, SDATA (Vinvocation_name));
5855 str = strrchr (basename, '.');
5856 if (str) *str = 0;
5857 Vinvocation_name = build_string (basename);
5859 Vx_resource_name = Vinvocation_name;
5861 validate_x_resource_name ();
5863 /* This is what opens the connection and sets x_current_display.
5864 This also initializes many symbols, such as those used for input. */
5865 dpyinfo = w32_term_init (display, xrm_option,
5866 (char *) SDATA (Vx_resource_name));
5868 if (dpyinfo == 0)
5870 if (!NILP (must_succeed))
5871 fatal ("Cannot connect to server %s.\n",
5872 SDATA (display));
5873 else
5874 error ("Cannot connect to server %s", SDATA (display));
5877 w32_in_use = 1;
5879 XSETFASTINT (Vwindow_system_version, 3);
5880 return Qnil;
5883 DEFUN ("x-close-connection", Fx_close_connection,
5884 Sx_close_connection, 1, 1, 0,
5885 doc: /* Close the connection to DISPLAY's server.
5886 For DISPLAY, specify either a frame or a display name (a string).
5887 If DISPLAY is nil, that stands for the selected frame's display. */)
5888 (display)
5889 Lisp_Object display;
5891 struct w32_display_info *dpyinfo = check_x_display_info (display);
5892 int i;
5894 if (dpyinfo->reference_count > 0)
5895 error ("Display still has frames on it");
5897 BLOCK_INPUT;
5898 x_destroy_all_bitmaps (dpyinfo);
5900 x_delete_display (dpyinfo);
5901 UNBLOCK_INPUT;
5903 return Qnil;
5906 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
5907 doc: /* Return the list of display names that Emacs has connections to. */)
5910 Lisp_Object tail, result;
5912 result = Qnil;
5913 for (tail = w32_display_name_list; CONSP (tail); tail = XCDR (tail))
5914 result = Fcons (XCAR (XCAR (tail)), result);
5916 return result;
5919 DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
5920 doc: /* This is a noop on W32 systems. */)
5921 (on, display)
5922 Lisp_Object display, on;
5924 return Qnil;
5929 /***********************************************************************
5930 Window properties
5931 ***********************************************************************/
5933 DEFUN ("x-change-window-property", Fx_change_window_property,
5934 Sx_change_window_property, 2, 6, 0,
5935 doc: /* Change window property PROP to VALUE on the X window of FRAME.
5936 VALUE may be a string or a list of conses, numbers and/or strings.
5937 If an element in the list is a string, it is converted to
5938 an Atom and the value of the Atom is used. If an element is a cons,
5939 it is converted to a 32 bit number where the car is the 16 top bits and the
5940 cdr is the lower 16 bits.
5941 FRAME nil or omitted means use the selected frame.
5942 If TYPE is given and non-nil, it is the name of the type of VALUE.
5943 If TYPE is not given or nil, the type is STRING.
5944 FORMAT gives the size in bits of each element if VALUE is a list.
5945 It must be one of 8, 16 or 32.
5946 If VALUE is a string or FORMAT is nil or not given, FORMAT defaults to 8.
5947 If OUTER_P is non-nil, the property is changed for the outer X window of
5948 FRAME. Default is to change on the edit X window.
5950 Value is VALUE. */)
5951 (prop, value, frame, type, format, outer_p)
5952 Lisp_Object prop, value, frame, type, format, outer_p;
5954 #if 0 /* TODO : port window properties to W32 */
5955 struct frame *f = check_x_frame (frame);
5956 Atom prop_atom;
5958 CHECK_STRING (prop);
5959 CHECK_STRING (value);
5961 BLOCK_INPUT;
5962 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
5963 XChangeProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
5964 prop_atom, XA_STRING, 8, PropModeReplace,
5965 SDATA (value), SCHARS (value));
5967 /* Make sure the property is set when we return. */
5968 XFlush (FRAME_W32_DISPLAY (f));
5969 UNBLOCK_INPUT;
5971 #endif /* TODO */
5973 return value;
5977 DEFUN ("x-delete-window-property", Fx_delete_window_property,
5978 Sx_delete_window_property, 1, 2, 0,
5979 doc: /* Remove window property PROP from X window of FRAME.
5980 FRAME nil or omitted means use the selected frame. Value is PROP. */)
5981 (prop, frame)
5982 Lisp_Object prop, frame;
5984 #if 0 /* TODO : port window properties to W32 */
5986 struct frame *f = check_x_frame (frame);
5987 Atom prop_atom;
5989 CHECK_STRING (prop);
5990 BLOCK_INPUT;
5991 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
5992 XDeleteProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), prop_atom);
5994 /* Make sure the property is removed when we return. */
5995 XFlush (FRAME_W32_DISPLAY (f));
5996 UNBLOCK_INPUT;
5997 #endif /* TODO */
5999 return prop;
6003 DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
6004 1, 2, 0,
6005 doc: /* Value is the value of window property PROP on FRAME.
6006 If FRAME is nil or omitted, use the selected frame. Value is nil
6007 if FRAME hasn't a property with name PROP or if PROP has no string
6008 value. */)
6009 (prop, frame)
6010 Lisp_Object prop, frame;
6012 #if 0 /* TODO : port window properties to W32 */
6014 struct frame *f = check_x_frame (frame);
6015 Atom prop_atom;
6016 int rc;
6017 Lisp_Object prop_value = Qnil;
6018 char *tmp_data = NULL;
6019 Atom actual_type;
6020 int actual_format;
6021 unsigned long actual_size, bytes_remaining;
6023 CHECK_STRING (prop);
6024 BLOCK_INPUT;
6025 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
6026 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
6027 prop_atom, 0, 0, False, XA_STRING,
6028 &actual_type, &actual_format, &actual_size,
6029 &bytes_remaining, (unsigned char **) &tmp_data);
6030 if (rc == Success)
6032 int size = bytes_remaining;
6034 XFree (tmp_data);
6035 tmp_data = NULL;
6037 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
6038 prop_atom, 0, bytes_remaining,
6039 False, XA_STRING,
6040 &actual_type, &actual_format,
6041 &actual_size, &bytes_remaining,
6042 (unsigned char **) &tmp_data);
6043 if (rc == Success)
6044 prop_value = make_string (tmp_data, size);
6046 XFree (tmp_data);
6049 UNBLOCK_INPUT;
6051 return prop_value;
6053 #endif /* TODO */
6054 return Qnil;
6059 /***********************************************************************
6060 Busy cursor
6061 ***********************************************************************/
6063 /* Non-zero means an hourglass cursor is currently shown. */
6065 static int hourglass_shown_p;
6067 /* Number of seconds to wait before displaying an hourglass cursor. */
6069 static Lisp_Object Vhourglass_delay;
6071 /* Default number of seconds to wait before displaying an hourglass
6072 cursor. */
6074 #define DEFAULT_HOURGLASS_DELAY 1
6076 /* Return non-zero if houglass timer has been started or hourglass is shown. */
6079 hourglass_started ()
6081 return hourglass_shown_p || hourglass_timer;
6084 /* Cancel a currently active hourglass timer, and start a new one. */
6086 void
6087 start_hourglass ()
6089 DWORD delay;
6090 int secs, msecs = 0;
6091 struct frame * f = SELECTED_FRAME ();
6093 /* No cursors on non GUI frames. */
6094 if (!FRAME_W32_P (f))
6095 return;
6097 cancel_hourglass ();
6099 if (INTEGERP (Vhourglass_delay)
6100 && XINT (Vhourglass_delay) > 0)
6101 secs = XFASTINT (Vhourglass_delay);
6102 else if (FLOATP (Vhourglass_delay)
6103 && XFLOAT_DATA (Vhourglass_delay) > 0)
6105 Lisp_Object tem;
6106 tem = Ftruncate (Vhourglass_delay, Qnil);
6107 secs = XFASTINT (tem);
6108 msecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000;
6110 else
6111 secs = DEFAULT_HOURGLASS_DELAY;
6113 delay = secs * 1000 + msecs;
6114 hourglass_hwnd = FRAME_W32_WINDOW (f);
6115 hourglass_timer = SetTimer (hourglass_hwnd, HOURGLASS_ID, delay, NULL);
6119 /* Cancel the hourglass cursor timer if active, hide an hourglass
6120 cursor if shown. */
6122 void
6123 cancel_hourglass ()
6125 if (hourglass_timer)
6127 KillTimer (hourglass_hwnd, hourglass_timer);
6128 hourglass_timer = 0;
6131 if (hourglass_shown_p)
6132 hide_hourglass ();
6136 /* Timer function of hourglass_timer.
6138 Display an hourglass cursor. Set the hourglass_p flag in display info
6139 to indicate that an hourglass cursor is shown. */
6141 static void
6142 show_hourglass (f)
6143 struct frame *f;
6145 if (!hourglass_shown_p)
6147 f->output_data.w32->hourglass_p = 1;
6148 if (!menubar_in_use && !current_popup_menu)
6149 SetCursor (f->output_data.w32->hourglass_cursor);
6150 hourglass_shown_p = 1;
6155 /* Hide the hourglass cursor on all frames, if it is currently shown. */
6157 static void
6158 hide_hourglass ()
6160 if (hourglass_shown_p)
6162 struct frame *f = x_window_to_frame (&one_w32_display_info,
6163 hourglass_hwnd);
6165 f->output_data.w32->hourglass_p = 0;
6166 SetCursor (f->output_data.w32->current_cursor);
6167 hourglass_shown_p = 0;
6173 /***********************************************************************
6174 Tool tips
6175 ***********************************************************************/
6177 static Lisp_Object x_create_tip_frame P_ ((struct w32_display_info *,
6178 Lisp_Object, Lisp_Object));
6179 static void compute_tip_xy P_ ((struct frame *, Lisp_Object, Lisp_Object,
6180 Lisp_Object, int, int, int *, int *));
6182 /* The frame of a currently visible tooltip. */
6184 Lisp_Object tip_frame;
6186 /* If non-nil, a timer started that hides the last tooltip when it
6187 fires. */
6189 Lisp_Object tip_timer;
6190 Window tip_window;
6192 /* If non-nil, a vector of 3 elements containing the last args
6193 with which x-show-tip was called. See there. */
6195 Lisp_Object last_show_tip_args;
6197 /* Maximum size for tooltips; a cons (COLUMNS . ROWS). */
6199 Lisp_Object Vx_max_tooltip_size;
6202 static Lisp_Object
6203 unwind_create_tip_frame (frame)
6204 Lisp_Object frame;
6206 Lisp_Object deleted;
6208 deleted = unwind_create_frame (frame);
6209 if (EQ (deleted, Qt))
6211 tip_window = NULL;
6212 tip_frame = Qnil;
6215 return deleted;
6219 /* Create a frame for a tooltip on the display described by DPYINFO.
6220 PARMS is a list of frame parameters. TEXT is the string to
6221 display in the tip frame. Value is the frame.
6223 Note that functions called here, esp. x_default_parameter can
6224 signal errors, for instance when a specified color name is
6225 undefined. We have to make sure that we're in a consistent state
6226 when this happens. */
6228 static Lisp_Object
6229 x_create_tip_frame (dpyinfo, parms, text)
6230 struct w32_display_info *dpyinfo;
6231 Lisp_Object parms, text;
6233 struct frame *f;
6234 Lisp_Object frame, tem;
6235 Lisp_Object name;
6236 long window_prompting = 0;
6237 int width, height;
6238 int count = SPECPDL_INDEX ();
6239 struct gcpro gcpro1, gcpro2, gcpro3;
6240 struct kboard *kb;
6241 int face_change_count_before = face_change_count;
6242 Lisp_Object buffer;
6243 struct buffer *old_buffer;
6245 check_w32 ();
6247 /* Use this general default value to start with until we know if
6248 this frame has a specified name. */
6249 Vx_resource_name = Vinvocation_name;
6251 #ifdef MULTI_KBOARD
6252 kb = dpyinfo->terminal->kboard;
6253 #else
6254 kb = &the_only_kboard;
6255 #endif
6257 /* Get the name of the frame to use for resource lookup. */
6258 name = w32_get_arg (parms, Qname, "name", "Name", RES_TYPE_STRING);
6259 if (!STRINGP (name)
6260 && !EQ (name, Qunbound)
6261 && !NILP (name))
6262 error ("Invalid frame name--not a string or nil");
6263 Vx_resource_name = name;
6265 frame = Qnil;
6266 GCPRO3 (parms, name, frame);
6267 /* Make a frame without minibuffer nor mode-line. */
6268 f = make_frame (0);
6269 f->wants_modeline = 0;
6270 XSETFRAME (frame, f);
6272 buffer = Fget_buffer_create (build_string (" *tip*"));
6273 Fset_window_buffer (FRAME_ROOT_WINDOW (f), buffer, Qnil);
6274 old_buffer = current_buffer;
6275 set_buffer_internal_1 (XBUFFER (buffer));
6276 current_buffer->truncate_lines = Qnil;
6277 specbind (Qinhibit_read_only, Qt);
6278 specbind (Qinhibit_modification_hooks, Qt);
6279 Ferase_buffer ();
6280 Finsert (1, &text);
6281 set_buffer_internal_1 (old_buffer);
6283 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
6284 record_unwind_protect (unwind_create_tip_frame, frame);
6286 /* By setting the output method, we're essentially saying that
6287 the frame is live, as per FRAME_LIVE_P. If we get a signal
6288 from this point on, x_destroy_window might screw up reference
6289 counts etc. */
6290 f->terminal = dpyinfo->terminal;
6291 f->terminal->reference_count++;
6292 f->output_method = output_w32;
6293 f->output_data.w32 =
6294 (struct w32_output *) xmalloc (sizeof (struct w32_output));
6295 bzero (f->output_data.w32, sizeof (struct w32_output));
6297 FRAME_FONTSET (f) = -1;
6298 f->icon_name = Qnil;
6300 #if 0 /* GLYPH_DEBUG TODO: image support. */
6301 image_cache_refcount = FRAME_IMAGE_CACHE (f)->refcount;
6302 dpyinfo_refcount = dpyinfo->reference_count;
6303 #endif /* GLYPH_DEBUG */
6304 #ifdef MULTI_KBOARD
6305 FRAME_KBOARD (f) = kb;
6306 #endif
6307 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
6308 f->output_data.w32->explicit_parent = 0;
6310 /* Set the name; the functions to which we pass f expect the name to
6311 be set. */
6312 if (EQ (name, Qunbound) || NILP (name))
6314 f->name = build_string (dpyinfo->w32_id_name);
6315 f->explicit_name = 0;
6317 else
6319 f->name = name;
6320 f->explicit_name = 1;
6321 /* use the frame's title when getting resources for this frame. */
6322 specbind (Qx_resource_name, name);
6325 f->resx = dpyinfo->resx;
6326 f->resy = dpyinfo->resy;
6328 /* Perhaps, we must allow frame parameter, say `font-backend',
6329 to specify which font backends to use. */
6330 register_font_driver (&w32font_driver, f);
6332 x_default_parameter (f, parms, Qfont_backend, Qnil,
6333 "fontBackend", "FontBackend", RES_TYPE_STRING);
6335 /* Extract the window parameters from the supplied values
6336 that are needed to determine window geometry. */
6337 x_default_font_parameter (f, parms);
6339 x_default_parameter (f, parms, Qborder_width, make_number (2),
6340 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
6341 /* This defaults to 2 in order to match xterm. We recognize either
6342 internalBorderWidth or internalBorder (which is what xterm calls
6343 it). */
6344 if (NILP (Fassq (Qinternal_border_width, parms)))
6346 Lisp_Object value;
6348 value = w32_get_arg (parms, Qinternal_border_width,
6349 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
6350 if (! EQ (value, Qunbound))
6351 parms = Fcons (Fcons (Qinternal_border_width, value),
6352 parms);
6354 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
6355 "internalBorderWidth", "internalBorderWidth",
6356 RES_TYPE_NUMBER);
6358 /* Also do the stuff which must be set before the window exists. */
6359 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
6360 "foreground", "Foreground", RES_TYPE_STRING);
6361 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
6362 "background", "Background", RES_TYPE_STRING);
6363 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
6364 "pointerColor", "Foreground", RES_TYPE_STRING);
6365 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
6366 "cursorColor", "Foreground", RES_TYPE_STRING);
6367 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
6368 "borderColor", "BorderColor", RES_TYPE_STRING);
6370 /* Init faces before x_default_parameter is called for scroll-bar
6371 parameters because that function calls x_set_scroll_bar_width,
6372 which calls change_frame_size, which calls Fset_window_buffer,
6373 which runs hooks, which call Fvertical_motion. At the end, we
6374 end up in init_iterator with a null face cache, which should not
6375 happen. */
6376 init_frame_faces (f);
6378 f->output_data.w32->dwStyle = WS_BORDER | WS_POPUP | WS_DISABLED;
6379 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
6381 window_prompting = x_figure_window_size (f, parms, 0);
6383 /* No fringes on tip frame. */
6384 f->fringe_cols = 0;
6385 f->left_fringe_width = 0;
6386 f->right_fringe_width = 0;
6388 BLOCK_INPUT;
6389 my_create_tip_window (f);
6390 UNBLOCK_INPUT;
6392 x_make_gc (f);
6394 x_default_parameter (f, parms, Qauto_raise, Qnil,
6395 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
6396 x_default_parameter (f, parms, Qauto_lower, Qnil,
6397 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
6398 x_default_parameter (f, parms, Qcursor_type, Qbox,
6399 "cursorType", "CursorType", RES_TYPE_SYMBOL);
6401 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
6402 Change will not be effected unless different from the current
6403 FRAME_LINES (f). */
6404 width = FRAME_COLS (f);
6405 height = FRAME_LINES (f);
6406 FRAME_LINES (f) = 0;
6407 SET_FRAME_COLS (f, 0);
6408 change_frame_size (f, height, width, 1, 0, 0);
6410 /* Add `tooltip' frame parameter's default value. */
6411 if (NILP (Fframe_parameter (frame, intern ("tooltip"))))
6412 Fmodify_frame_parameters (frame, Fcons (Fcons (intern ("tooltip"), Qt),
6413 Qnil));
6415 /* Set up faces after all frame parameters are known. This call
6416 also merges in face attributes specified for new frames.
6418 Frame parameters may be changed if .Xdefaults contains
6419 specifications for the default font. For example, if there is an
6420 `Emacs.default.attributeBackground: pink', the `background-color'
6421 attribute of the frame get's set, which let's the internal border
6422 of the tooltip frame appear in pink. Prevent this. */
6424 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
6426 /* Set tip_frame here, so that */
6427 tip_frame = frame;
6428 call1 (Qface_set_after_frame_default, frame);
6430 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
6431 Fmodify_frame_parameters (frame, Fcons (Fcons (Qbackground_color, bg),
6432 Qnil));
6435 f->no_split = 1;
6437 UNGCPRO;
6439 /* It is now ok to make the frame official even if we get an error
6440 below. And the frame needs to be on Vframe_list or making it
6441 visible won't work. */
6442 Vframe_list = Fcons (frame, Vframe_list);
6444 /* Now that the frame is official, it counts as a reference to
6445 its display. */
6446 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
6448 /* Setting attributes of faces of the tooltip frame from resources
6449 and similar will increment face_change_count, which leads to the
6450 clearing of all current matrices. Since this isn't necessary
6451 here, avoid it by resetting face_change_count to the value it
6452 had before we created the tip frame. */
6453 face_change_count = face_change_count_before;
6455 /* Discard the unwind_protect. */
6456 return unbind_to (count, frame);
6460 /* Compute where to display tip frame F. PARMS is the list of frame
6461 parameters for F. DX and DY are specified offsets from the current
6462 location of the mouse. WIDTH and HEIGHT are the width and height
6463 of the tooltip. Return coordinates relative to the root window of
6464 the display in *ROOT_X, and *ROOT_Y. */
6466 static void
6467 compute_tip_xy (f, parms, dx, dy, width, height, root_x, root_y)
6468 struct frame *f;
6469 Lisp_Object parms, dx, dy;
6470 int width, height;
6471 int *root_x, *root_y;
6473 Lisp_Object left, top;
6474 int min_x, min_y, max_x, max_y;
6476 /* User-specified position? */
6477 left = Fcdr (Fassq (Qleft, parms));
6478 top = Fcdr (Fassq (Qtop, parms));
6480 /* Move the tooltip window where the mouse pointer is. Resize and
6481 show it. */
6482 if (!INTEGERP (left) || !INTEGERP (top))
6484 POINT pt;
6486 /* Default min and max values. */
6487 min_x = 0;
6488 min_y = 0;
6489 max_x = FRAME_W32_DISPLAY_INFO (f)->width;
6490 max_y = FRAME_W32_DISPLAY_INFO (f)->height;
6492 BLOCK_INPUT;
6493 GetCursorPos (&pt);
6494 *root_x = pt.x;
6495 *root_y = pt.y;
6496 UNBLOCK_INPUT;
6498 /* If multiple monitor support is available, constrain the tip onto
6499 the current monitor. This improves the above by allowing negative
6500 co-ordinates if monitor positions are such that they are valid, and
6501 snaps a tooltip onto a single monitor if we are close to the edge
6502 where it would otherwise flow onto the other monitor (or into
6503 nothingness if there is a gap in the overlap). */
6504 if (monitor_from_point_fn && get_monitor_info_fn)
6506 struct MONITOR_INFO info;
6507 HMONITOR monitor
6508 = monitor_from_point_fn (pt, MONITOR_DEFAULT_TO_NEAREST);
6509 info.cbSize = sizeof (info);
6511 if (get_monitor_info_fn (monitor, &info))
6513 min_x = info.rcWork.left;
6514 min_y = info.rcWork.top;
6515 max_x = info.rcWork.right;
6516 max_y = info.rcWork.bottom;
6521 if (INTEGERP (top))
6522 *root_y = XINT (top);
6523 else if (*root_y + XINT (dy) <= min_y)
6524 *root_y = min_y; /* Can happen for negative dy */
6525 else if (*root_y + XINT (dy) + height <= max_y)
6526 /* It fits below the pointer */
6527 *root_y += XINT (dy);
6528 else if (height + XINT (dy) + min_y <= *root_y)
6529 /* It fits above the pointer. */
6530 *root_y -= height + XINT (dy);
6531 else
6532 /* Put it on the top. */
6533 *root_y = min_y;
6535 if (INTEGERP (left))
6536 *root_x = XINT (left);
6537 else if (*root_x + XINT (dx) <= min_x)
6538 *root_x = 0; /* Can happen for negative dx */
6539 else if (*root_x + XINT (dx) + width <= max_x)
6540 /* It fits to the right of the pointer. */
6541 *root_x += XINT (dx);
6542 else if (width + XINT (dx) + min_x <= *root_x)
6543 /* It fits to the left of the pointer. */
6544 *root_x -= width + XINT (dx);
6545 else
6546 /* Put it left justified on the screen -- it ought to fit that way. */
6547 *root_x = min_x;
6551 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
6552 doc: /* Show STRING in a \"tooltip\" window on frame FRAME.
6553 A tooltip window is a small window displaying a string.
6555 This is an internal function; Lisp code should call `tooltip-show'.
6557 FRAME nil or omitted means use the selected frame.
6559 PARMS is an optional list of frame parameters which can be
6560 used to change the tooltip's appearance.
6562 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
6563 means use the default timeout of 5 seconds.
6565 If the list of frame parameters PARMS contains a `left' parameter,
6566 the tooltip is displayed at that x-position. Otherwise it is
6567 displayed at the mouse position, with offset DX added (default is 5 if
6568 DX isn't specified). Likewise for the y-position; if a `top' frame
6569 parameter is specified, it determines the y-position of the tooltip
6570 window, otherwise it is displayed at the mouse position, with offset
6571 DY added (default is -10).
6573 A tooltip's maximum size is specified by `x-max-tooltip-size'.
6574 Text larger than the specified size is clipped. */)
6575 (string, frame, parms, timeout, dx, dy)
6576 Lisp_Object string, frame, parms, timeout, dx, dy;
6578 struct frame *f;
6579 struct window *w;
6580 int root_x, root_y;
6581 struct buffer *old_buffer;
6582 struct text_pos pos;
6583 int i, width, height;
6584 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6585 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6586 int count = SPECPDL_INDEX ();
6588 specbind (Qinhibit_redisplay, Qt);
6590 GCPRO4 (string, parms, frame, timeout);
6592 CHECK_STRING (string);
6593 f = check_x_frame (frame);
6594 if (NILP (timeout))
6595 timeout = make_number (5);
6596 else
6597 CHECK_NATNUM (timeout);
6599 if (NILP (dx))
6600 dx = make_number (5);
6601 else
6602 CHECK_NUMBER (dx);
6604 if (NILP (dy))
6605 dy = make_number (-10);
6606 else
6607 CHECK_NUMBER (dy);
6609 if (NILP (last_show_tip_args))
6610 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
6612 if (!NILP (tip_frame))
6614 Lisp_Object last_string = AREF (last_show_tip_args, 0);
6615 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
6616 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
6618 if (EQ (frame, last_frame)
6619 && !NILP (Fequal (last_string, string))
6620 && !NILP (Fequal (last_parms, parms)))
6622 struct frame *f = XFRAME (tip_frame);
6624 /* Only DX and DY have changed. */
6625 if (!NILP (tip_timer))
6627 Lisp_Object timer = tip_timer;
6628 tip_timer = Qnil;
6629 call1 (Qcancel_timer, timer);
6632 BLOCK_INPUT;
6633 compute_tip_xy (f, parms, dx, dy, FRAME_PIXEL_WIDTH (f),
6634 FRAME_PIXEL_HEIGHT (f), &root_x, &root_y);
6636 /* Put tooltip in topmost group and in position. */
6637 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
6638 root_x, root_y, 0, 0,
6639 SWP_NOSIZE | SWP_NOACTIVATE);
6641 /* Ensure tooltip is on top of other topmost windows (eg menus). */
6642 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
6643 0, 0, 0, 0,
6644 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
6646 UNBLOCK_INPUT;
6647 goto start_timer;
6651 /* Hide a previous tip, if any. */
6652 Fx_hide_tip ();
6654 ASET (last_show_tip_args, 0, string);
6655 ASET (last_show_tip_args, 1, frame);
6656 ASET (last_show_tip_args, 2, parms);
6658 /* Add default values to frame parameters. */
6659 if (NILP (Fassq (Qname, parms)))
6660 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
6661 if (NILP (Fassq (Qinternal_border_width, parms)))
6662 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
6663 if (NILP (Fassq (Qborder_width, parms)))
6664 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
6665 if (NILP (Fassq (Qborder_color, parms)))
6666 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
6667 if (NILP (Fassq (Qbackground_color, parms)))
6668 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
6669 parms);
6671 /* Block input until the tip has been fully drawn, to avoid crashes
6672 when drawing tips in menus. */
6673 BLOCK_INPUT;
6675 /* Create a frame for the tooltip, and record it in the global
6676 variable tip_frame. */
6677 frame = x_create_tip_frame (FRAME_W32_DISPLAY_INFO (f), parms, string);
6678 f = XFRAME (frame);
6680 /* Set up the frame's root window. */
6681 w = XWINDOW (FRAME_ROOT_WINDOW (f));
6682 w->left_col = w->top_line = make_number (0);
6684 if (CONSP (Vx_max_tooltip_size)
6685 && INTEGERP (XCAR (Vx_max_tooltip_size))
6686 && XINT (XCAR (Vx_max_tooltip_size)) > 0
6687 && INTEGERP (XCDR (Vx_max_tooltip_size))
6688 && XINT (XCDR (Vx_max_tooltip_size)) > 0)
6690 w->total_cols = XCAR (Vx_max_tooltip_size);
6691 w->total_lines = XCDR (Vx_max_tooltip_size);
6693 else
6695 w->total_cols = make_number (80);
6696 w->total_lines = make_number (40);
6699 FRAME_TOTAL_COLS (f) = XINT (w->total_cols);
6700 adjust_glyphs (f);
6701 w->pseudo_window_p = 1;
6703 /* Display the tooltip text in a temporary buffer. */
6704 old_buffer = current_buffer;
6705 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->buffer));
6706 current_buffer->truncate_lines = Qnil;
6707 clear_glyph_matrix (w->desired_matrix);
6708 clear_glyph_matrix (w->current_matrix);
6709 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
6710 try_window (FRAME_ROOT_WINDOW (f), pos, 0);
6712 /* Compute width and height of the tooltip. */
6713 width = height = 0;
6714 for (i = 0; i < w->desired_matrix->nrows; ++i)
6716 struct glyph_row *row = &w->desired_matrix->rows[i];
6717 struct glyph *last;
6718 int row_width;
6720 /* Stop at the first empty row at the end. */
6721 if (!row->enabled_p || !row->displays_text_p)
6722 break;
6724 /* Let the row go over the full width of the frame. */
6725 row->full_width_p = 1;
6727 #ifdef TODO /* Investigate why some fonts need more width than is
6728 calculated for some tooltips. */
6729 /* There's a glyph at the end of rows that is use to place
6730 the cursor there. Don't include the width of this glyph. */
6731 if (row->used[TEXT_AREA])
6733 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
6734 row_width = row->pixel_width - last->pixel_width;
6736 else
6737 #endif
6738 row_width = row->pixel_width;
6740 /* TODO: find why tips do not draw along baseline as instructed. */
6741 height += row->height;
6742 width = max (width, row_width);
6745 /* Add the frame's internal border to the width and height the X
6746 window should have. */
6747 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
6748 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
6750 /* Move the tooltip window where the mouse pointer is. Resize and
6751 show it. */
6752 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
6755 /* Adjust Window size to take border into account. */
6756 RECT rect;
6757 rect.left = rect.top = 0;
6758 rect.right = width;
6759 rect.bottom = height;
6760 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
6761 FRAME_EXTERNAL_MENU_BAR (f));
6763 /* Position and size tooltip, and put it in the topmost group.
6764 The add-on of 3 to the 5th argument is a kludge: without it,
6765 some fonts cause the last character of the tip to be truncated,
6766 for some obscure reason. */
6767 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
6768 root_x, root_y, rect.right - rect.left + 3,
6769 rect.bottom - rect.top, SWP_NOACTIVATE);
6771 /* Ensure tooltip is on top of other topmost windows (eg menus). */
6772 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
6773 0, 0, 0, 0,
6774 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
6776 /* Let redisplay know that we have made the frame visible already. */
6777 f->async_visible = 1;
6779 ShowWindow (FRAME_W32_WINDOW (f), SW_SHOWNOACTIVATE);
6782 /* Draw into the window. */
6783 w->must_be_updated_p = 1;
6784 update_single_window (w, 1);
6786 UNBLOCK_INPUT;
6788 /* Restore original current buffer. */
6789 set_buffer_internal_1 (old_buffer);
6790 windows_or_buffers_changed = old_windows_or_buffers_changed;
6792 start_timer:
6793 /* Let the tip disappear after timeout seconds. */
6794 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
6795 intern ("x-hide-tip"));
6797 UNGCPRO;
6798 return unbind_to (count, Qnil);
6802 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
6803 doc: /* Hide the current tooltip window, if there is any.
6804 Value is t if tooltip was open, nil otherwise. */)
6807 int count;
6808 Lisp_Object deleted, frame, timer;
6809 struct gcpro gcpro1, gcpro2;
6811 /* Return quickly if nothing to do. */
6812 if (NILP (tip_timer) && NILP (tip_frame))
6813 return Qnil;
6815 frame = tip_frame;
6816 timer = tip_timer;
6817 GCPRO2 (frame, timer);
6818 tip_frame = tip_timer = deleted = Qnil;
6820 count = SPECPDL_INDEX ();
6821 specbind (Qinhibit_redisplay, Qt);
6822 specbind (Qinhibit_quit, Qt);
6824 if (!NILP (timer))
6825 call1 (Qcancel_timer, timer);
6827 if (FRAMEP (frame))
6829 Fdelete_frame (frame, Qnil);
6830 deleted = Qt;
6833 UNGCPRO;
6834 return unbind_to (count, deleted);
6839 /***********************************************************************
6840 File selection dialog
6841 ***********************************************************************/
6842 extern Lisp_Object Qfile_name_history;
6844 /* Callback for altering the behaviour of the Open File dialog.
6845 Makes the Filename text field contain "Current Directory" and be
6846 read-only when "Directories" is selected in the filter. This
6847 allows us to work around the fact that the standard Open File
6848 dialog does not support directories. */
6849 UINT CALLBACK
6850 file_dialog_callback (hwnd, msg, wParam, lParam)
6851 HWND hwnd;
6852 UINT msg;
6853 WPARAM wParam;
6854 LPARAM lParam;
6856 if (msg == WM_NOTIFY)
6858 OFNOTIFY * notify = (OFNOTIFY *)lParam;
6859 /* Detect when the Filter dropdown is changed. */
6860 if (notify->hdr.code == CDN_TYPECHANGE
6861 || notify->hdr.code == CDN_INITDONE)
6863 HWND dialog = GetParent (hwnd);
6864 HWND edit_control = GetDlgItem (dialog, FILE_NAME_TEXT_FIELD);
6866 /* Directories is in index 2. */
6867 if (notify->lpOFN->nFilterIndex == 2)
6869 CommDlg_OpenSave_SetControlText (dialog, FILE_NAME_TEXT_FIELD,
6870 "Current Directory");
6871 EnableWindow (edit_control, FALSE);
6873 else
6875 /* Don't override default filename on init done. */
6876 if (notify->hdr.code == CDN_TYPECHANGE)
6877 CommDlg_OpenSave_SetControlText (dialog,
6878 FILE_NAME_TEXT_FIELD, "");
6879 EnableWindow (edit_control, TRUE);
6883 return 0;
6886 /* Since we compile with _WIN32_WINNT set to 0x0400 (for NT4 compatibility)
6887 we end up with the old file dialogs. Define a big enough struct for the
6888 new dialog to trick GetOpenFileName into giving us the new dialogs on
6889 Windows 2000 and XP. */
6890 typedef struct
6892 OPENFILENAME real_details;
6893 void * pReserved;
6894 DWORD dwReserved;
6895 DWORD FlagsEx;
6896 } NEWOPENFILENAME;
6899 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
6900 doc: /* Read file name, prompting with PROMPT in directory DIR.
6901 Use a file selection dialog.
6902 Select DEFAULT-FILENAME in the dialog's file selection box, if
6903 specified. Ensure that file exists if MUSTMATCH is non-nil.
6904 If ONLY-DIR-P is non-nil, the user can only select directories. */)
6905 (prompt, dir, default_filename, mustmatch, only_dir_p)
6906 Lisp_Object prompt, dir, default_filename, mustmatch, only_dir_p;
6908 struct frame *f = SELECTED_FRAME ();
6909 Lisp_Object file = Qnil;
6910 int count = SPECPDL_INDEX ();
6911 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
6912 char filename[MAX_PATH + 1];
6913 char init_dir[MAX_PATH + 1];
6914 int default_filter_index = 1; /* 1: All Files, 2: Directories only */
6916 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
6917 CHECK_STRING (prompt);
6918 CHECK_STRING (dir);
6920 /* Create the dialog with PROMPT as title, using DIR as initial
6921 directory and using "*" as pattern. */
6922 dir = Fexpand_file_name (dir, Qnil);
6923 strncpy (init_dir, SDATA (ENCODE_FILE (dir)), MAX_PATH);
6924 init_dir[MAX_PATH] = '\0';
6925 unixtodos_filename (init_dir);
6927 if (STRINGP (default_filename))
6929 char *file_name_only;
6930 char *full_path_name = SDATA (ENCODE_FILE (default_filename));
6932 unixtodos_filename (full_path_name);
6934 file_name_only = strrchr (full_path_name, '\\');
6935 if (!file_name_only)
6936 file_name_only = full_path_name;
6937 else
6938 file_name_only++;
6940 strncpy (filename, file_name_only, MAX_PATH);
6941 filename[MAX_PATH] = '\0';
6943 else
6944 filename[0] = '\0';
6947 NEWOPENFILENAME new_file_details;
6948 BOOL file_opened = FALSE;
6949 OPENFILENAME * file_details = &new_file_details.real_details;
6951 /* Prevent redisplay. */
6952 specbind (Qinhibit_redisplay, Qt);
6953 BLOCK_INPUT;
6955 bzero (&new_file_details, sizeof (new_file_details));
6956 /* Apparently NT4 crashes if you give it an unexpected size.
6957 I'm not sure about Windows 9x, so play it safe. */
6958 if (w32_major_version > 4 && w32_major_version < 95)
6959 file_details->lStructSize = sizeof (NEWOPENFILENAME);
6960 else
6961 file_details->lStructSize = sizeof (OPENFILENAME);
6963 file_details->hwndOwner = FRAME_W32_WINDOW (f);
6964 /* Undocumented Bug in Common File Dialog:
6965 If a filter is not specified, shell links are not resolved. */
6966 file_details->lpstrFilter = "All Files (*.*)\0*.*\0Directories\0*|*\0\0";
6967 file_details->lpstrFile = filename;
6968 file_details->nMaxFile = sizeof (filename);
6969 file_details->lpstrInitialDir = init_dir;
6970 file_details->lpstrTitle = SDATA (prompt);
6972 if (! NILP (only_dir_p))
6973 default_filter_index = 2;
6975 file_details->nFilterIndex = default_filter_index;
6977 file_details->Flags = (OFN_HIDEREADONLY | OFN_NOCHANGEDIR
6978 | OFN_EXPLORER | OFN_ENABLEHOOK);
6979 if (!NILP (mustmatch))
6981 /* Require that the path to the parent directory exists. */
6982 file_details->Flags |= OFN_PATHMUSTEXIST;
6983 /* If we are looking for a file, require that it exists. */
6984 if (NILP (only_dir_p))
6985 file_details->Flags |= OFN_FILEMUSTEXIST;
6988 file_details->lpfnHook = (LPOFNHOOKPROC) file_dialog_callback;
6990 file_opened = GetOpenFileName (file_details);
6992 UNBLOCK_INPUT;
6994 if (file_opened)
6996 dostounix_filename (filename);
6998 if (file_details->nFilterIndex == 2)
7000 /* "Directories" selected - strip dummy file name. */
7001 char * last = strrchr (filename, '/');
7002 *last = '\0';
7005 file = DECODE_FILE (build_string (filename));
7007 /* User cancelled the dialog without making a selection. */
7008 else if (!CommDlgExtendedError ())
7009 file = Qnil;
7010 /* An error occurred, fallback on reading from the mini-buffer. */
7011 else
7012 file = Fcompleting_read (prompt, intern ("read-file-name-internal"),
7013 dir, mustmatch, dir, Qfile_name_history,
7014 default_filename, Qnil);
7016 file = unbind_to (count, file);
7019 UNGCPRO;
7021 /* Make "Cancel" equivalent to C-g. */
7022 if (NILP (file))
7023 Fsignal (Qquit, Qnil);
7025 return unbind_to (count, file);
7030 /***********************************************************************
7031 w32 specialized functions
7032 ***********************************************************************/
7034 DEFUN ("w32-send-sys-command", Fw32_send_sys_command,
7035 Sw32_send_sys_command, 1, 2, 0,
7036 doc: /* Send frame a Windows WM_SYSCOMMAND message of type COMMAND.
7037 Some useful values for COMMAND are #xf030 to maximize frame (#xf020
7038 to minimize), #xf120 to restore frame to original size, and #xf100
7039 to activate the menubar for keyboard access. #xf140 activates the
7040 screen saver if defined.
7042 If optional parameter FRAME is not specified, use selected frame. */)
7043 (command, frame)
7044 Lisp_Object command, frame;
7046 FRAME_PTR f = check_x_frame (frame);
7048 CHECK_NUMBER (command);
7050 PostMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, XINT (command), 0);
7052 return Qnil;
7055 DEFUN ("w32-shell-execute", Fw32_shell_execute, Sw32_shell_execute, 2, 4, 0,
7056 doc: /* Get Windows to perform OPERATION on DOCUMENT.
7057 This is a wrapper around the ShellExecute system function, which
7058 invokes the application registered to handle OPERATION for DOCUMENT.
7060 OPERATION is either nil or a string that names a supported operation.
7061 What operations can be used depends on the particular DOCUMENT and its
7062 handler application, but typically it is one of the following common
7063 operations:
7065 \"open\" - open DOCUMENT, which could be a file, a directory, or an
7066 executable program. If it is an application, that
7067 application is launched in the current buffer's default
7068 directory. Otherwise, the application associated with
7069 DOCUMENT is launched in the buffer's default directory.
7070 \"print\" - print DOCUMENT, which must be a file
7071 \"explore\" - start the Windows Explorer on DOCUMENT
7072 \"edit\" - launch an editor and open DOCUMENT for editing; which
7073 editor is launched depends on the association for the
7074 specified DOCUMENT
7075 \"find\" - initiate search starting from DOCUMENT which must specify
7076 a directory
7077 nil - invoke the default OPERATION, or \"open\" if default is
7078 not defined or unavailable
7080 DOCUMENT is typically the name of a document file or a URL, but can
7081 also be a program executable to run, or a directory to open in the
7082 Windows Explorer.
7084 If DOCUMENT is a program executable, the optional third arg PARAMETERS
7085 can be a string containing command line parameters that will be passed
7086 to the program; otherwise, PARAMETERS should be nil or unspecified.
7088 Optional fourth argument SHOW-FLAG can be used to control how the
7089 application will be displayed when it is invoked. If SHOW-FLAG is nil
7090 or unspecified, the application is displayed normally, otherwise it is
7091 an integer representing a ShowWindow flag:
7093 0 - start hidden
7094 1 - start normally
7095 3 - start maximized
7096 6 - start minimized */)
7097 (operation, document, parameters, show_flag)
7098 Lisp_Object operation, document, parameters, show_flag;
7100 Lisp_Object current_dir;
7102 CHECK_STRING (document);
7104 /* Encode filename, current directory and parameters. */
7105 current_dir = ENCODE_FILE (current_buffer->directory);
7106 document = ENCODE_FILE (document);
7107 if (STRINGP (parameters))
7108 parameters = ENCODE_SYSTEM (parameters);
7110 if ((int) ShellExecute (NULL,
7111 (STRINGP (operation) ?
7112 SDATA (operation) : NULL),
7113 SDATA (document),
7114 (STRINGP (parameters) ?
7115 SDATA (parameters) : NULL),
7116 SDATA (current_dir),
7117 (INTEGERP (show_flag) ?
7118 XINT (show_flag) : SW_SHOWDEFAULT))
7119 > 32)
7120 return Qt;
7121 error ("ShellExecute failed: %s", w32_strerror (0));
7124 /* Lookup virtual keycode from string representing the name of a
7125 non-ascii keystroke into the corresponding virtual key, using
7126 lispy_function_keys. */
7127 static int
7128 lookup_vk_code (char *key)
7130 int i;
7132 for (i = 0; i < 256; i++)
7133 if (lispy_function_keys[i]
7134 && strcmp (lispy_function_keys[i], key) == 0)
7135 return i;
7137 return -1;
7140 /* Convert a one-element vector style key sequence to a hot key
7141 definition. */
7142 static Lisp_Object
7143 w32_parse_hot_key (key)
7144 Lisp_Object key;
7146 /* Copied from Fdefine_key and store_in_keymap. */
7147 register Lisp_Object c;
7148 int vk_code;
7149 int lisp_modifiers;
7150 int w32_modifiers;
7151 struct gcpro gcpro1;
7153 CHECK_VECTOR (key);
7155 if (XFASTINT (Flength (key)) != 1)
7156 return Qnil;
7158 GCPRO1 (key);
7160 c = Faref (key, make_number (0));
7162 if (CONSP (c) && lucid_event_type_list_p (c))
7163 c = Fevent_convert_list (c);
7165 UNGCPRO;
7167 if (! INTEGERP (c) && ! SYMBOLP (c))
7168 error ("Key definition is invalid");
7170 /* Work out the base key and the modifiers. */
7171 if (SYMBOLP (c))
7173 c = parse_modifiers (c);
7174 lisp_modifiers = XINT (Fcar (Fcdr (c)));
7175 c = Fcar (c);
7176 if (!SYMBOLP (c))
7177 abort ();
7178 vk_code = lookup_vk_code (SDATA (SYMBOL_NAME (c)));
7180 else if (INTEGERP (c))
7182 lisp_modifiers = XINT (c) & ~CHARACTERBITS;
7183 /* Many ascii characters are their own virtual key code. */
7184 vk_code = XINT (c) & CHARACTERBITS;
7187 if (vk_code < 0 || vk_code > 255)
7188 return Qnil;
7190 if ((lisp_modifiers & meta_modifier) != 0
7191 && !NILP (Vw32_alt_is_meta))
7192 lisp_modifiers |= alt_modifier;
7194 /* Supply defs missing from mingw32. */
7195 #ifndef MOD_ALT
7196 #define MOD_ALT 0x0001
7197 #define MOD_CONTROL 0x0002
7198 #define MOD_SHIFT 0x0004
7199 #define MOD_WIN 0x0008
7200 #endif
7202 /* Convert lisp modifiers to Windows hot-key form. */
7203 w32_modifiers = (lisp_modifiers & hyper_modifier) ? MOD_WIN : 0;
7204 w32_modifiers |= (lisp_modifiers & alt_modifier) ? MOD_ALT : 0;
7205 w32_modifiers |= (lisp_modifiers & ctrl_modifier) ? MOD_CONTROL : 0;
7206 w32_modifiers |= (lisp_modifiers & shift_modifier) ? MOD_SHIFT : 0;
7208 return HOTKEY (vk_code, w32_modifiers);
7211 DEFUN ("w32-register-hot-key", Fw32_register_hot_key,
7212 Sw32_register_hot_key, 1, 1, 0,
7213 doc: /* Register KEY as a hot-key combination.
7214 Certain key combinations like Alt-Tab are reserved for system use on
7215 Windows, and therefore are normally intercepted by the system. However,
7216 most of these key combinations can be received by registering them as
7217 hot-keys, overriding their special meaning.
7219 KEY must be a one element key definition in vector form that would be
7220 acceptable to `define-key' (e.g. [A-tab] for Alt-Tab). The meta
7221 modifier is interpreted as Alt if `w32-alt-is-meta' is t, and hyper
7222 is always interpreted as the Windows modifier keys.
7224 The return value is the hotkey-id if registered, otherwise nil. */)
7225 (key)
7226 Lisp_Object key;
7228 key = w32_parse_hot_key (key);
7230 if (!NILP (key) && NILP (Fmemq (key, w32_grabbed_keys)))
7232 /* Reuse an empty slot if possible. */
7233 Lisp_Object item = Fmemq (Qnil, w32_grabbed_keys);
7235 /* Safe to add new key to list, even if we have focus. */
7236 if (NILP (item))
7237 w32_grabbed_keys = Fcons (key, w32_grabbed_keys);
7238 else
7239 XSETCAR (item, key);
7241 /* Notify input thread about new hot-key definition, so that it
7242 takes effect without needing to switch focus. */
7243 #ifdef USE_LISP_UNION_TYPE
7244 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
7245 (WPARAM) key.i, 0);
7246 #else
7247 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
7248 (WPARAM) key, 0);
7249 #endif
7252 return key;
7255 DEFUN ("w32-unregister-hot-key", Fw32_unregister_hot_key,
7256 Sw32_unregister_hot_key, 1, 1, 0,
7257 doc: /* Unregister KEY as a hot-key combination. */)
7258 (key)
7259 Lisp_Object key;
7261 Lisp_Object item;
7263 if (!INTEGERP (key))
7264 key = w32_parse_hot_key (key);
7266 item = Fmemq (key, w32_grabbed_keys);
7268 if (!NILP (item))
7270 /* Notify input thread about hot-key definition being removed, so
7271 that it takes effect without needing focus switch. */
7272 #ifdef USE_LISP_UNION_TYPE
7273 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
7274 (WPARAM) XINT (XCAR (item)), (LPARAM) item.i))
7275 #else
7276 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
7277 (WPARAM) XINT (XCAR (item)), (LPARAM) item))
7278 #endif
7280 MSG msg;
7281 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
7283 return Qt;
7285 return Qnil;
7288 DEFUN ("w32-registered-hot-keys", Fw32_registered_hot_keys,
7289 Sw32_registered_hot_keys, 0, 0, 0,
7290 doc: /* Return list of registered hot-key IDs. */)
7293 return Fdelq (Qnil, Fcopy_sequence (w32_grabbed_keys));
7296 DEFUN ("w32-reconstruct-hot-key", Fw32_reconstruct_hot_key,
7297 Sw32_reconstruct_hot_key, 1, 1, 0,
7298 doc: /* Convert hot-key ID to a lisp key combination.
7299 usage: (w32-reconstruct-hot-key ID) */)
7300 (hotkeyid)
7301 Lisp_Object hotkeyid;
7303 int vk_code, w32_modifiers;
7304 Lisp_Object key;
7306 CHECK_NUMBER (hotkeyid);
7308 vk_code = HOTKEY_VK_CODE (hotkeyid);
7309 w32_modifiers = HOTKEY_MODIFIERS (hotkeyid);
7311 if (vk_code < 256 && lispy_function_keys[vk_code])
7312 key = intern (lispy_function_keys[vk_code]);
7313 else
7314 key = make_number (vk_code);
7316 key = Fcons (key, Qnil);
7317 if (w32_modifiers & MOD_SHIFT)
7318 key = Fcons (Qshift, key);
7319 if (w32_modifiers & MOD_CONTROL)
7320 key = Fcons (Qctrl, key);
7321 if (w32_modifiers & MOD_ALT)
7322 key = Fcons (NILP (Vw32_alt_is_meta) ? Qalt : Qmeta, key);
7323 if (w32_modifiers & MOD_WIN)
7324 key = Fcons (Qhyper, key);
7326 return key;
7329 DEFUN ("w32-toggle-lock-key", Fw32_toggle_lock_key,
7330 Sw32_toggle_lock_key, 1, 2, 0,
7331 doc: /* Toggle the state of the lock key KEY.
7332 KEY can be `capslock', `kp-numlock', or `scroll'.
7333 If the optional parameter NEW-STATE is a number, then the state of KEY
7334 is set to off if the low bit of NEW-STATE is zero, otherwise on. */)
7335 (key, new_state)
7336 Lisp_Object key, new_state;
7338 int vk_code;
7340 if (EQ (key, intern ("capslock")))
7341 vk_code = VK_CAPITAL;
7342 else if (EQ (key, intern ("kp-numlock")))
7343 vk_code = VK_NUMLOCK;
7344 else if (EQ (key, intern ("scroll")))
7345 vk_code = VK_SCROLL;
7346 else
7347 return Qnil;
7349 if (!dwWindowsThreadId)
7350 return make_number (w32_console_toggle_lock_key (vk_code, new_state));
7352 #ifdef USE_LISP_UNION_TYPE
7353 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
7354 (WPARAM) vk_code, (LPARAM) new_state.i))
7355 #else
7356 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
7357 (WPARAM) vk_code, (LPARAM) new_state))
7358 #endif
7360 MSG msg;
7361 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
7362 return make_number (msg.wParam);
7364 return Qnil;
7367 DEFUN ("w32-window-exists-p", Fw32_window_exists_p, Sw32_window_exists_p,
7368 2, 2, 0,
7369 doc: /* Return non-nil if a window exists with the specified CLASS and NAME.
7371 This is a direct interface to the Windows API FindWindow function. */)
7372 (class, name)
7373 Lisp_Object class, name;
7375 HWND hnd;
7377 if (!NILP (class))
7378 CHECK_STRING (class);
7379 if (!NILP (name))
7380 CHECK_STRING (name);
7382 hnd = FindWindow (STRINGP (class) ? ((LPCTSTR) SDATA (class)) : NULL,
7383 STRINGP (name) ? ((LPCTSTR) SDATA (name)) : NULL);
7384 if (!hnd)
7385 return Qnil;
7386 return Qt;
7389 DEFUN ("w32-battery-status", Fw32_battery_status, Sw32_battery_status, 0, 0, 0,
7390 doc: /* Get power status information from Windows system.
7392 The following %-sequences are provided:
7393 %L AC line status (verbose)
7394 %B Battery status (verbose)
7395 %b Battery status, empty means high, `-' means low,
7396 `!' means critical, and `+' means charging
7397 %p Battery load percentage
7398 %s Remaining time (to charge or discharge) in seconds
7399 %m Remaining time (to charge or discharge) in minutes
7400 %h Remaining time (to charge or discharge) in hours
7401 %t Remaining time (to charge or discharge) in the form `h:min' */)
7404 Lisp_Object status = Qnil;
7406 SYSTEM_POWER_STATUS system_status;
7407 if (GetSystemPowerStatus (&system_status))
7409 Lisp_Object line_status, battery_status, battery_status_symbol;
7410 Lisp_Object load_percentage, seconds, minutes, hours, remain;
7411 Lisp_Object sequences[8];
7413 long seconds_left = (long) system_status.BatteryLifeTime;
7415 if (system_status.ACLineStatus == 0)
7416 line_status = build_string ("off-line");
7417 else if (system_status.ACLineStatus == 1)
7418 line_status = build_string ("on-line");
7419 else
7420 line_status = build_string ("N/A");
7422 if (system_status.BatteryFlag & 128)
7424 battery_status = build_string ("N/A");
7425 battery_status_symbol = build_string ("");
7427 else if (system_status.BatteryFlag & 8)
7429 battery_status = build_string ("charging");
7430 battery_status_symbol = build_string ("+");
7431 if (system_status.BatteryFullLifeTime != -1L)
7432 seconds_left = system_status.BatteryFullLifeTime - seconds_left;
7434 else if (system_status.BatteryFlag & 4)
7436 battery_status = build_string ("critical");
7437 battery_status_symbol = build_string ("!");
7439 else if (system_status.BatteryFlag & 2)
7441 battery_status = build_string ("low");
7442 battery_status_symbol = build_string ("-");
7444 else if (system_status.BatteryFlag & 1)
7446 battery_status = build_string ("high");
7447 battery_status_symbol = build_string ("");
7449 else
7451 battery_status = build_string ("medium");
7452 battery_status_symbol = build_string ("");
7455 if (system_status.BatteryLifePercent > 100)
7456 load_percentage = build_string ("N/A");
7457 else
7459 char buffer[16];
7460 _snprintf (buffer, 16, "%d", system_status.BatteryLifePercent);
7461 load_percentage = build_string (buffer);
7464 if (seconds_left < 0)
7465 seconds = minutes = hours = remain = build_string ("N/A");
7466 else
7468 long m;
7469 float h;
7470 char buffer[16];
7471 _snprintf (buffer, 16, "%ld", seconds_left);
7472 seconds = build_string (buffer);
7474 m = seconds_left / 60;
7475 _snprintf (buffer, 16, "%ld", m);
7476 minutes = build_string (buffer);
7478 h = seconds_left / 3600.0;
7479 _snprintf (buffer, 16, "%3.1f", h);
7480 hours = build_string (buffer);
7482 _snprintf (buffer, 16, "%ld:%02ld", m / 60, m % 60);
7483 remain = build_string (buffer);
7485 sequences[0] = Fcons (make_number ('L'), line_status);
7486 sequences[1] = Fcons (make_number ('B'), battery_status);
7487 sequences[2] = Fcons (make_number ('b'), battery_status_symbol);
7488 sequences[3] = Fcons (make_number ('p'), load_percentage);
7489 sequences[4] = Fcons (make_number ('s'), seconds);
7490 sequences[5] = Fcons (make_number ('m'), minutes);
7491 sequences[6] = Fcons (make_number ('h'), hours);
7492 sequences[7] = Fcons (make_number ('t'), remain);
7494 status = Flist (8, sequences);
7496 return status;
7500 DEFUN ("file-system-info", Ffile_system_info, Sfile_system_info, 1, 1, 0,
7501 doc: /* Return storage information about the file system FILENAME is on.
7502 Value is a list of floats (TOTAL FREE AVAIL), where TOTAL is the total
7503 storage of the file system, FREE is the free storage, and AVAIL is the
7504 storage available to a non-superuser. All 3 numbers are in bytes.
7505 If the underlying system call fails, value is nil. */)
7506 (filename)
7507 Lisp_Object filename;
7509 Lisp_Object encoded, value;
7511 CHECK_STRING (filename);
7512 filename = Fexpand_file_name (filename, Qnil);
7513 encoded = ENCODE_FILE (filename);
7515 value = Qnil;
7517 /* Determining the required information on Windows turns out, sadly,
7518 to be more involved than one would hope. The original Win32 api
7519 call for this will return bogus information on some systems, but we
7520 must dynamically probe for the replacement api, since that was
7521 added rather late on. */
7523 HMODULE hKernel = GetModuleHandle ("kernel32");
7524 BOOL (*pfn_GetDiskFreeSpaceEx)
7525 (char *, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER)
7526 = (void *) GetProcAddress (hKernel, "GetDiskFreeSpaceEx");
7528 /* On Windows, we may need to specify the root directory of the
7529 volume holding FILENAME. */
7530 char rootname[MAX_PATH];
7531 char *name = SDATA (encoded);
7533 /* find the root name of the volume if given */
7534 if (isalpha (name[0]) && name[1] == ':')
7536 rootname[0] = name[0];
7537 rootname[1] = name[1];
7538 rootname[2] = '\\';
7539 rootname[3] = 0;
7541 else if (IS_DIRECTORY_SEP (name[0]) && IS_DIRECTORY_SEP (name[1]))
7543 char *str = rootname;
7544 int slashes = 4;
7547 if (IS_DIRECTORY_SEP (*name) && --slashes == 0)
7548 break;
7549 *str++ = *name++;
7551 while ( *name );
7553 *str++ = '\\';
7554 *str = 0;
7557 if (pfn_GetDiskFreeSpaceEx)
7559 /* Unsigned large integers cannot be cast to double, so
7560 use signed ones instead. */
7561 LARGE_INTEGER availbytes;
7562 LARGE_INTEGER freebytes;
7563 LARGE_INTEGER totalbytes;
7565 if (pfn_GetDiskFreeSpaceEx (rootname,
7566 (ULARGE_INTEGER *)&availbytes,
7567 (ULARGE_INTEGER *)&totalbytes,
7568 (ULARGE_INTEGER *)&freebytes))
7569 value = list3 (make_float ((double) totalbytes.QuadPart),
7570 make_float ((double) freebytes.QuadPart),
7571 make_float ((double) availbytes.QuadPart));
7573 else
7575 DWORD sectors_per_cluster;
7576 DWORD bytes_per_sector;
7577 DWORD free_clusters;
7578 DWORD total_clusters;
7580 if (GetDiskFreeSpace (rootname,
7581 &sectors_per_cluster,
7582 &bytes_per_sector,
7583 &free_clusters,
7584 &total_clusters))
7585 value = list3 (make_float ((double) total_clusters
7586 * sectors_per_cluster * bytes_per_sector),
7587 make_float ((double) free_clusters
7588 * sectors_per_cluster * bytes_per_sector),
7589 make_float ((double) free_clusters
7590 * sectors_per_cluster * bytes_per_sector));
7594 return value;
7597 DEFUN ("default-printer-name", Fdefault_printer_name, Sdefault_printer_name,
7598 0, 0, 0, doc: /* Return the name of Windows default printer device. */)
7601 static char pname_buf[256];
7602 int err;
7603 HANDLE hPrn;
7604 PRINTER_INFO_2 *ppi2 = NULL;
7605 DWORD dwNeeded = 0, dwReturned = 0;
7607 /* Retrieve the default string from Win.ini (the registry).
7608 * String will be in form "printername,drivername,portname".
7609 * This is the most portable way to get the default printer. */
7610 if (GetProfileString ("windows", "device", ",,", pname_buf, sizeof (pname_buf)) <= 0)
7611 return Qnil;
7612 /* printername precedes first "," character */
7613 strtok (pname_buf, ",");
7614 /* We want to know more than the printer name */
7615 if (!OpenPrinter (pname_buf, &hPrn, NULL))
7616 return Qnil;
7617 GetPrinter (hPrn, 2, NULL, 0, &dwNeeded);
7618 if (dwNeeded == 0)
7620 ClosePrinter (hPrn);
7621 return Qnil;
7623 /* Allocate memory for the PRINTER_INFO_2 struct */
7624 ppi2 = (PRINTER_INFO_2 *) xmalloc (dwNeeded);
7625 if (!ppi2)
7627 ClosePrinter (hPrn);
7628 return Qnil;
7630 /* Call GetPrinter again with big enouth memory block */
7631 err = GetPrinter (hPrn, 2, (LPBYTE)ppi2, dwNeeded, &dwReturned);
7632 ClosePrinter (hPrn);
7633 if (!err)
7635 xfree (ppi2);
7636 return Qnil;
7639 if (ppi2)
7641 if (ppi2->Attributes & PRINTER_ATTRIBUTE_SHARED && ppi2->pServerName)
7643 /* a remote printer */
7644 if (*ppi2->pServerName == '\\')
7645 _snprintf (pname_buf, sizeof (pname_buf), "%s\\%s", ppi2->pServerName,
7646 ppi2->pShareName);
7647 else
7648 _snprintf (pname_buf, sizeof (pname_buf), "\\\\%s\\%s", ppi2->pServerName,
7649 ppi2->pShareName);
7650 pname_buf[sizeof (pname_buf) - 1] = '\0';
7652 else
7654 /* a local printer */
7655 strncpy (pname_buf, ppi2->pPortName, sizeof (pname_buf));
7656 pname_buf[sizeof (pname_buf) - 1] = '\0';
7657 /* `pPortName' can include several ports, delimited by ','.
7658 * we only use the first one. */
7659 strtok (pname_buf, ",");
7661 xfree (ppi2);
7664 return build_string (pname_buf);
7667 /***********************************************************************
7668 Initialization
7669 ***********************************************************************/
7671 /* Keep this list in the same order as frame_parms in frame.c.
7672 Use 0 for unsupported frame parameters. */
7674 frame_parm_handler w32_frame_parm_handlers[] =
7676 x_set_autoraise,
7677 x_set_autolower,
7678 x_set_background_color,
7679 x_set_border_color,
7680 x_set_border_width,
7681 x_set_cursor_color,
7682 x_set_cursor_type,
7683 x_set_font,
7684 x_set_foreground_color,
7685 x_set_icon_name,
7686 x_set_icon_type,
7687 x_set_internal_border_width,
7688 x_set_menu_bar_lines,
7689 x_set_mouse_color,
7690 x_explicitly_set_name,
7691 x_set_scroll_bar_width,
7692 x_set_title,
7693 x_set_unsplittable,
7694 x_set_vertical_scroll_bars,
7695 x_set_visibility,
7696 x_set_tool_bar_lines,
7697 0, /* x_set_scroll_bar_foreground, */
7698 0, /* x_set_scroll_bar_background, */
7699 x_set_screen_gamma,
7700 x_set_line_spacing,
7701 x_set_fringe_width,
7702 x_set_fringe_width,
7703 0, /* x_set_wait_for_wm, */
7704 x_set_fullscreen,
7705 x_set_font_backend,
7706 0 /* x_set_alpha, */
7709 void
7710 syms_of_w32fns ()
7712 globals_of_w32fns ();
7713 /* This is zero if not using MS-Windows. */
7714 w32_in_use = 0;
7715 track_mouse_window = NULL;
7717 w32_visible_system_caret_hwnd = NULL;
7719 DEFSYM (Qnone, "none");
7720 DEFSYM (Qsuppress_icon, "suppress-icon");
7721 DEFSYM (Qundefined_color, "undefined-color");
7722 DEFSYM (Qcancel_timer, "cancel-timer");
7723 DEFSYM (Qhyper, "hyper");
7724 DEFSYM (Qsuper, "super");
7725 DEFSYM (Qmeta, "meta");
7726 DEFSYM (Qalt, "alt");
7727 DEFSYM (Qctrl, "ctrl");
7728 DEFSYM (Qcontrol, "control");
7729 DEFSYM (Qshift, "shift");
7730 DEFSYM (Qfont_param, "font-parameter");
7731 /* This is the end of symbol initialization. */
7733 /* Text property `display' should be nonsticky by default. */
7734 Vtext_property_default_nonsticky
7735 = Fcons (Fcons (Qdisplay, Qt), Vtext_property_default_nonsticky);
7738 Fput (Qundefined_color, Qerror_conditions,
7739 Fcons (Qundefined_color, Fcons (Qerror, Qnil)));
7740 Fput (Qundefined_color, Qerror_message,
7741 build_string ("Undefined color"));
7743 staticpro (&w32_grabbed_keys);
7744 w32_grabbed_keys = Qnil;
7746 DEFVAR_LISP ("w32-color-map", &Vw32_color_map,
7747 doc: /* An array of color name mappings for Windows. */);
7748 Vw32_color_map = Qnil;
7750 DEFVAR_LISP ("w32-pass-alt-to-system", &Vw32_pass_alt_to_system,
7751 doc: /* Non-nil if Alt key presses are passed on to Windows.
7752 When non-nil, for example, Alt pressed and released and then space will
7753 open the System menu. When nil, Emacs processes the Alt key events, and
7754 then silently swallows them. */);
7755 Vw32_pass_alt_to_system = Qnil;
7757 DEFVAR_LISP ("w32-alt-is-meta", &Vw32_alt_is_meta,
7758 doc: /* Non-nil if the Alt key is to be considered the same as the META key.
7759 When nil, Emacs will translate the Alt key to the ALT modifier, not to META. */);
7760 Vw32_alt_is_meta = Qt;
7762 DEFVAR_INT ("w32-quit-key", &w32_quit_key,
7763 doc: /* If non-zero, the virtual key code for an alternative quit key. */);
7764 w32_quit_key = 0;
7766 DEFVAR_LISP ("w32-pass-lwindow-to-system",
7767 &Vw32_pass_lwindow_to_system,
7768 doc: /* If non-nil, the left \"Windows\" key is passed on to Windows.
7770 When non-nil, the Start menu is opened by tapping the key.
7771 If you set this to nil, the left \"Windows\" key is processed by Emacs
7772 according to the value of `w32-lwindow-modifier', which see.
7774 Note that some combinations of the left \"Windows\" key with other keys are
7775 caught by Windows at low level, and so binding them in Emacs will have no
7776 effect. For example, <lwindow>-r always pops up the Windows Run dialog,
7777 <lwindow>-<Pause> pops up the "System Properties" dialog, etc. However, see
7778 the doc string of `w32-phantom-key-code'. */);
7779 Vw32_pass_lwindow_to_system = Qt;
7781 DEFVAR_LISP ("w32-pass-rwindow-to-system",
7782 &Vw32_pass_rwindow_to_system,
7783 doc: /* If non-nil, the right \"Windows\" key is passed on to Windows.
7785 When non-nil, the Start menu is opened by tapping the key.
7786 If you set this to nil, the right \"Windows\" key is processed by Emacs
7787 according to the value of `w32-rwindow-modifier', which see.
7789 Note that some combinations of the right \"Windows\" key with other keys are
7790 caught by Windows at low level, and so binding them in Emacs will have no
7791 effect. For example, <rwindow>-r always pops up the Windows Run dialog,
7792 <rwindow>-<Pause> pops up the "System Properties" dialog, etc. However, see
7793 the doc string of `w32-phantom-key-code'. */);
7794 Vw32_pass_rwindow_to_system = Qt;
7796 DEFVAR_LISP ("w32-phantom-key-code",
7797 &Vw32_phantom_key_code,
7798 doc: /* Virtual key code used to generate \"phantom\" key presses.
7799 Value is a number between 0 and 255.
7801 Phantom key presses are generated in order to stop the system from
7802 acting on \"Windows\" key events when `w32-pass-lwindow-to-system' or
7803 `w32-pass-rwindow-to-system' is nil. */);
7804 /* Although 255 is technically not a valid key code, it works and
7805 means that this hack won't interfere with any real key code. */
7806 XSETINT (Vw32_phantom_key_code, 255);
7808 DEFVAR_LISP ("w32-enable-num-lock",
7809 &Vw32_enable_num_lock,
7810 doc: /* If non-nil, the Num Lock key acts normally.
7811 Set to nil to handle Num Lock as the `kp-numlock' key. */);
7812 Vw32_enable_num_lock = Qt;
7814 DEFVAR_LISP ("w32-enable-caps-lock",
7815 &Vw32_enable_caps_lock,
7816 doc: /* If non-nil, the Caps Lock key acts normally.
7817 Set to nil to handle Caps Lock as the `capslock' key. */);
7818 Vw32_enable_caps_lock = Qt;
7820 DEFVAR_LISP ("w32-scroll-lock-modifier",
7821 &Vw32_scroll_lock_modifier,
7822 doc: /* Modifier to use for the Scroll Lock ON state.
7823 The value can be hyper, super, meta, alt, control or shift for the
7824 respective modifier, or nil to handle Scroll Lock as the `scroll' key.
7825 Any other value will cause the Scroll Lock key to be ignored. */);
7826 Vw32_scroll_lock_modifier = Qt;
7828 DEFVAR_LISP ("w32-lwindow-modifier",
7829 &Vw32_lwindow_modifier,
7830 doc: /* Modifier to use for the left \"Windows\" key.
7831 The value can be hyper, super, meta, alt, control or shift for the
7832 respective modifier, or nil to appear as the `lwindow' key.
7833 Any other value will cause the key to be ignored. */);
7834 Vw32_lwindow_modifier = Qnil;
7836 DEFVAR_LISP ("w32-rwindow-modifier",
7837 &Vw32_rwindow_modifier,
7838 doc: /* Modifier to use for the right \"Windows\" key.
7839 The value can be hyper, super, meta, alt, control or shift for the
7840 respective modifier, or nil to appear as the `rwindow' key.
7841 Any other value will cause the key to be ignored. */);
7842 Vw32_rwindow_modifier = Qnil;
7844 DEFVAR_LISP ("w32-apps-modifier",
7845 &Vw32_apps_modifier,
7846 doc: /* Modifier to use for the \"Apps\" key.
7847 The value can be hyper, super, meta, alt, control or shift for the
7848 respective modifier, or nil to appear as the `apps' key.
7849 Any other value will cause the key to be ignored. */);
7850 Vw32_apps_modifier = Qnil;
7852 DEFVAR_BOOL ("w32-enable-synthesized-fonts", &w32_enable_synthesized_fonts,
7853 doc: /* Non-nil enables selection of artificially italicized and bold fonts. */);
7854 w32_enable_synthesized_fonts = 0;
7856 DEFVAR_LISP ("w32-enable-palette", &Vw32_enable_palette,
7857 doc: /* Non-nil enables Windows palette management to map colors exactly. */);
7858 Vw32_enable_palette = Qt;
7860 DEFVAR_INT ("w32-mouse-button-tolerance",
7861 &w32_mouse_button_tolerance,
7862 doc: /* Analogue of double click interval for faking middle mouse events.
7863 The value is the minimum time in milliseconds that must elapse between
7864 left and right button down events before they are considered distinct events.
7865 If both mouse buttons are depressed within this interval, a middle mouse
7866 button down event is generated instead. */);
7867 w32_mouse_button_tolerance = GetDoubleClickTime () / 2;
7869 DEFVAR_INT ("w32-mouse-move-interval",
7870 &w32_mouse_move_interval,
7871 doc: /* Minimum interval between mouse move events.
7872 The value is the minimum time in milliseconds that must elapse between
7873 successive mouse move (or scroll bar drag) events before they are
7874 reported as lisp events. */);
7875 w32_mouse_move_interval = 0;
7877 DEFVAR_BOOL ("w32-pass-extra-mouse-buttons-to-system",
7878 &w32_pass_extra_mouse_buttons_to_system,
7879 doc: /* If non-nil, the fourth and fifth mouse buttons are passed to Windows.
7880 Recent versions of Windows support mice with up to five buttons.
7881 Since most applications don't support these extra buttons, most mouse
7882 drivers will allow you to map them to functions at the system level.
7883 If this variable is non-nil, Emacs will pass them on, allowing the
7884 system to handle them. */);
7885 w32_pass_extra_mouse_buttons_to_system = 0;
7887 DEFVAR_BOOL ("w32-pass-multimedia-buttons-to-system",
7888 &w32_pass_multimedia_buttons_to_system,
7889 doc: /* If non-nil, media buttons are passed to Windows.
7890 Some modern keyboards contain buttons for controlling media players, web
7891 browsers and other applications. Generally these buttons are handled on a
7892 system wide basis, but by setting this to nil they are made available
7893 to Emacs for binding. Depending on your keyboard, additional keys that
7894 may be available are:
7896 browser-back, browser-forward, browser-refresh, browser-stop,
7897 browser-search, browser-favorites, browser-home,
7898 mail, mail-reply, mail-forward, mail-send,
7899 app-1, app-2,
7900 help, find, new, open, close, save, print, undo, redo, copy, cut, paste,
7901 spell-check, correction-list, toggle-dictate-command,
7902 media-next, media-previous, media-stop, media-play-pause, media-select,
7903 media-play, media-pause, media-record, media-fast-forward, media-rewind,
7904 media-channel-up, media-channel-down,
7905 volume-mute, volume-up, volume-down,
7906 mic-volume-mute, mic-volume-down, mic-volume-up, mic-toggle,
7907 bass-down, bass-boost, bass-up, treble-down, treble-up */);
7908 w32_pass_multimedia_buttons_to_system = 1;
7910 DEFVAR_LISP ("x-pointer-shape", &Vx_pointer_shape,
7911 doc: /* The shape of the pointer when over text.
7912 Changing the value does not affect existing frames
7913 unless you set the mouse color. */);
7914 Vx_pointer_shape = Qnil;
7916 Vx_nontext_pointer_shape = Qnil;
7918 Vx_mode_pointer_shape = Qnil;
7920 DEFVAR_LISP ("x-hourglass-pointer-shape", &Vx_hourglass_pointer_shape,
7921 doc: /* The shape of the pointer when Emacs is busy.
7922 This variable takes effect when you create a new frame
7923 or when you set the mouse color. */);
7924 Vx_hourglass_pointer_shape = Qnil;
7926 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
7927 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
7928 display_hourglass_p = 1;
7930 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
7931 doc: /* *Seconds to wait before displaying an hourglass pointer.
7932 Value must be an integer or float. */);
7933 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
7935 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
7936 &Vx_sensitive_text_pointer_shape,
7937 doc: /* The shape of the pointer when over mouse-sensitive text.
7938 This variable takes effect when you create a new frame
7939 or when you set the mouse color. */);
7940 Vx_sensitive_text_pointer_shape = Qnil;
7942 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
7943 &Vx_window_horizontal_drag_shape,
7944 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
7945 This variable takes effect when you create a new frame
7946 or when you set the mouse color. */);
7947 Vx_window_horizontal_drag_shape = Qnil;
7949 DEFVAR_LISP ("x-cursor-fore-pixel", &Vx_cursor_fore_pixel,
7950 doc: /* A string indicating the foreground color of the cursor box. */);
7951 Vx_cursor_fore_pixel = Qnil;
7953 DEFVAR_LISP ("x-max-tooltip-size", &Vx_max_tooltip_size,
7954 doc: /* Maximum size for tooltips.
7955 Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
7956 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
7958 DEFVAR_LISP ("x-no-window-manager", &Vx_no_window_manager,
7959 doc: /* Non-nil if no window manager is in use.
7960 Emacs doesn't try to figure this out; this is always nil
7961 unless you set it to something else. */);
7962 /* We don't have any way to find this out, so set it to nil
7963 and maybe the user would like to set it to t. */
7964 Vx_no_window_manager = Qnil;
7966 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
7967 &Vx_pixel_size_width_font_regexp,
7968 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
7970 Since Emacs gets width of a font matching with this regexp from
7971 PIXEL_SIZE field of the name, font finding mechanism gets faster for
7972 such a font. This is especially effective for such large fonts as
7973 Chinese, Japanese, and Korean. */);
7974 Vx_pixel_size_width_font_regexp = Qnil;
7976 DEFVAR_LISP ("w32-bdf-filename-alist",
7977 &Vw32_bdf_filename_alist,
7978 doc: /* List of bdf fonts and their corresponding filenames. */);
7979 Vw32_bdf_filename_alist = Qnil;
7981 DEFVAR_BOOL ("w32-strict-fontnames",
7982 &w32_strict_fontnames,
7983 doc: /* Non-nil means only use fonts that are exact matches for those requested.
7984 Default is nil, which allows old fontnames that are not XLFD compliant,
7985 and allows third-party CJK display to work by specifying false charset
7986 fields to trick Emacs into translating to Big5, SJIS etc.
7987 Setting this to t will prevent wrong fonts being selected when
7988 fontsets are automatically created. */);
7989 w32_strict_fontnames = 0;
7991 DEFVAR_BOOL ("w32-strict-painting",
7992 &w32_strict_painting,
7993 doc: /* Non-nil means use strict rules for repainting frames.
7994 Set this to nil to get the old behavior for repainting; this should
7995 only be necessary if the default setting causes problems. */);
7996 w32_strict_painting = 1;
7998 DEFVAR_LISP ("w32-charset-info-alist",
7999 &Vw32_charset_info_alist,
8000 doc: /* Alist linking Emacs character sets to Windows fonts and codepages.
8001 Each entry should be of the form:
8003 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE))
8005 where CHARSET_NAME is a string used in font names to identify the charset,
8006 WINDOWS_CHARSET is a symbol that can be one of:
8007 w32-charset-ansi, w32-charset-default, w32-charset-symbol,
8008 w32-charset-shiftjis, w32-charset-hangeul, w32-charset-gb2312,
8009 w32-charset-chinesebig5,
8010 w32-charset-johab, w32-charset-hebrew,
8011 w32-charset-arabic, w32-charset-greek, w32-charset-turkish,
8012 w32-charset-vietnamese, w32-charset-thai, w32-charset-easteurope,
8013 w32-charset-russian, w32-charset-mac, w32-charset-baltic,
8014 w32-charset-unicode,
8015 or w32-charset-oem.
8016 CODEPAGE should be an integer specifying the codepage that should be used
8017 to display the character set, t to do no translation and output as Unicode,
8018 or nil to do no translation and output as 8 bit (or multibyte on far-east
8019 versions of Windows) characters. */);
8020 Vw32_charset_info_alist = Qnil;
8022 DEFSYM (Qw32_charset_ansi, "w32-charset-ansi");
8023 DEFSYM (Qw32_charset_symbol, "w32-charset-symbol");
8024 DEFSYM (Qw32_charset_default, "w32-charset-default");
8025 DEFSYM (Qw32_charset_shiftjis, "w32-charset-shiftjis");
8026 DEFSYM (Qw32_charset_hangeul, "w32-charset-hangeul");
8027 DEFSYM (Qw32_charset_chinesebig5, "w32-charset-chinesebig5");
8028 DEFSYM (Qw32_charset_gb2312, "w32-charset-gb2312");
8029 DEFSYM (Qw32_charset_oem, "w32-charset-oem");
8031 #ifdef JOHAB_CHARSET
8033 static int w32_extra_charsets_defined = 1;
8034 DEFVAR_BOOL ("w32-extra-charsets-defined", &w32_extra_charsets_defined,
8035 doc: /* Internal variable. */);
8037 DEFSYM (Qw32_charset_johab, "w32-charset-johab");
8038 DEFSYM (Qw32_charset_easteurope, "w32-charset-easteurope");
8039 DEFSYM (Qw32_charset_turkish, "w32-charset-turkish");
8040 DEFSYM (Qw32_charset_baltic, "w32-charset-baltic");
8041 DEFSYM (Qw32_charset_russian, "w32-charset-russian");
8042 DEFSYM (Qw32_charset_arabic, "w32-charset-arabic");
8043 DEFSYM (Qw32_charset_greek, "w32-charset-greek");
8044 DEFSYM (Qw32_charset_hebrew, "w32-charset-hebrew");
8045 DEFSYM (Qw32_charset_vietnamese, "w32-charset-vietnamese");
8046 DEFSYM (Qw32_charset_thai, "w32-charset-thai");
8047 DEFSYM (Qw32_charset_mac, "w32-charset-mac");
8049 #endif
8051 #ifdef UNICODE_CHARSET
8053 static int w32_unicode_charset_defined = 1;
8054 DEFVAR_BOOL ("w32-unicode-charset-defined",
8055 &w32_unicode_charset_defined,
8056 doc: /* Internal variable. */);
8057 DEFSYM (Qw32_charset_unicode, "w32-charset-unicode");
8059 #endif
8061 #if 0 /* TODO: Port to W32 */
8062 defsubr (&Sx_change_window_property);
8063 defsubr (&Sx_delete_window_property);
8064 defsubr (&Sx_window_property);
8065 #endif
8066 defsubr (&Sxw_display_color_p);
8067 defsubr (&Sx_display_grayscale_p);
8068 defsubr (&Sxw_color_defined_p);
8069 defsubr (&Sxw_color_values);
8070 defsubr (&Sx_server_max_request_size);
8071 defsubr (&Sx_server_vendor);
8072 defsubr (&Sx_server_version);
8073 defsubr (&Sx_display_pixel_width);
8074 defsubr (&Sx_display_pixel_height);
8075 defsubr (&Sx_display_mm_width);
8076 defsubr (&Sx_display_mm_height);
8077 defsubr (&Sx_display_screens);
8078 defsubr (&Sx_display_planes);
8079 defsubr (&Sx_display_color_cells);
8080 defsubr (&Sx_display_visual_class);
8081 defsubr (&Sx_display_backing_store);
8082 defsubr (&Sx_display_save_under);
8083 defsubr (&Sx_create_frame);
8084 defsubr (&Sx_open_connection);
8085 defsubr (&Sx_close_connection);
8086 defsubr (&Sx_display_list);
8087 defsubr (&Sx_synchronize);
8088 defsubr (&Sx_focus_frame);
8090 /* W32 specific functions */
8092 defsubr (&Sw32_define_rgb_color);
8093 defsubr (&Sw32_default_color_map);
8094 defsubr (&Sw32_load_color_file);
8095 defsubr (&Sw32_send_sys_command);
8096 defsubr (&Sw32_shell_execute);
8097 defsubr (&Sw32_register_hot_key);
8098 defsubr (&Sw32_unregister_hot_key);
8099 defsubr (&Sw32_registered_hot_keys);
8100 defsubr (&Sw32_reconstruct_hot_key);
8101 defsubr (&Sw32_toggle_lock_key);
8102 defsubr (&Sw32_window_exists_p);
8103 defsubr (&Sw32_battery_status);
8105 defsubr (&Sfile_system_info);
8106 defsubr (&Sdefault_printer_name);
8108 check_window_system_func = check_w32;
8111 hourglass_timer = 0;
8112 hourglass_hwnd = NULL;
8113 hourglass_shown_p = 0;
8114 defsubr (&Sx_show_tip);
8115 defsubr (&Sx_hide_tip);
8116 tip_timer = Qnil;
8117 staticpro (&tip_timer);
8118 tip_frame = Qnil;
8119 staticpro (&tip_frame);
8121 last_show_tip_args = Qnil;
8122 staticpro (&last_show_tip_args);
8124 defsubr (&Sx_file_dialog);
8129 globals_of_w32fns is used to initialize those global variables that
8130 must always be initialized on startup even when the global variable
8131 initialized is non zero (see the function main in emacs.c).
8132 globals_of_w32fns is called from syms_of_w32fns when the global
8133 variable initialized is 0 and directly from main when initialized
8134 is non zero.
8136 void
8137 globals_of_w32fns ()
8139 HMODULE user32_lib = GetModuleHandle ("user32.dll");
8141 TrackMouseEvent not available in all versions of Windows, so must load
8142 it dynamically. Do it once, here, instead of every time it is used.
8144 track_mouse_event_fn = (TrackMouseEvent_Proc)
8145 GetProcAddress (user32_lib, "TrackMouseEvent");
8146 /* ditto for GetClipboardSequenceNumber. */
8147 clipboard_sequence_fn = (ClipboardSequence_Proc)
8148 GetProcAddress (user32_lib, "GetClipboardSequenceNumber");
8150 monitor_from_point_fn = (MonitorFromPoint_Proc)
8151 GetProcAddress (user32_lib, "MonitorFromPoint");
8152 get_monitor_info_fn = (GetMonitorInfo_Proc)
8153 GetProcAddress (user32_lib, "GetMonitorInfoA");
8156 HMODULE imm32_lib = GetModuleHandle ("imm32.dll");
8157 get_composition_string_fn = (ImmGetCompositionString_Proc)
8158 GetProcAddress (imm32_lib, "ImmGetCompositionStringW");
8159 get_ime_context_fn = (ImmGetContext_Proc)
8160 GetProcAddress (imm32_lib, "ImmGetContext");
8162 DEFVAR_INT ("w32-ansi-code-page",
8163 &w32_ansi_code_page,
8164 doc: /* The ANSI code page used by the system. */);
8165 w32_ansi_code_page = GetACP ();
8167 /* MessageBox does not work without this when linked to comctl32.dll 6.0. */
8168 InitCommonControls ();
8170 syms_of_w32uniscribe ();
8173 #undef abort
8175 void
8176 w32_abort ()
8178 int button;
8179 button = MessageBox (NULL,
8180 "A fatal error has occurred!\n\n"
8181 "Would you like to attach a debugger?\n\n"
8182 "Select YES to debug, NO to abort Emacs"
8183 #if __GNUC__
8184 "\n\n(type \"gdb -p <emacs-PID>\" and\n"
8185 "\"continue\" inside GDB before clicking YES.)"
8186 #endif
8187 , "Emacs Abort Dialog",
8188 MB_ICONEXCLAMATION | MB_TASKMODAL
8189 | MB_SETFOREGROUND | MB_YESNO);
8190 switch (button)
8192 case IDYES:
8193 DebugBreak ();
8194 exit (2); /* tell the compiler we will never return */
8195 case IDNO:
8196 default:
8197 abort ();
8198 break;
8202 /* For convenience when debugging. */
8204 w32_last_error ()
8206 return GetLastError ();
8209 /* arch-tag: 707589ab-b9be-4638-8cdd-74629cc9b446
8210 (do not change this comment) */