* vc-dispatcher (vc-finish-logentry): Don't mess with the windows/frames.
[emacs.git] / src / w32fns.c
blob795e7208569109102e4b58746a207c8c2ade5cb3
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, 2009, 2010
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>
30 #include <setjmp.h>
32 #include "lisp.h"
33 #include "w32term.h"
34 #include "frame.h"
35 #include "window.h"
36 #include "buffer.h"
37 #include "intervals.h"
38 #include "dispextern.h"
39 #include "keyboard.h"
40 #include "blockinput.h"
41 #include "epaths.h"
42 #include "character.h"
43 #include "charset.h"
44 #include "coding.h"
45 #include "ccl.h"
46 #include "fontset.h"
47 #include "systime.h"
48 #include "termhooks.h"
49 #include "w32heap.h"
51 #include "bitmaps/gray.xbm"
53 #include <commctrl.h>
54 #include <commdlg.h>
55 #include <shellapi.h>
56 #include <ctype.h>
57 #include <winspool.h>
58 #include <objbase.h>
60 #include <dlgs.h>
61 #include <imm.h>
62 #define FILE_NAME_TEXT_FIELD edt1
64 #include "font.h"
65 #include "w32font.h"
67 #ifndef FOF_NO_CONNECTED_ELEMENTS
68 #define FOF_NO_CONNECTED_ELEMENTS 0x2000
69 #endif
71 void syms_of_w32fns ();
72 void globals_of_w32fns ();
74 extern void free_frame_menubar ();
75 extern double atof ();
76 extern int w32_console_toggle_lock_key P_ ((int, Lisp_Object));
77 extern void w32_menu_display_help P_ ((HWND, HMENU, UINT, UINT));
78 extern void w32_free_menu_strings P_ ((HWND));
79 extern const char *map_w32_filename P_ ((const char *, const char **));
81 extern int quit_char;
83 extern char *lispy_function_keys[];
85 /* The colormap for converting color names to RGB values */
86 Lisp_Object Vw32_color_map;
88 /* Non nil if alt key presses are passed on to Windows. */
89 Lisp_Object Vw32_pass_alt_to_system;
91 /* Non nil if alt key is translated to meta_modifier, nil if it is translated
92 to alt_modifier. */
93 Lisp_Object Vw32_alt_is_meta;
95 /* If non-zero, the windows virtual key code for an alternative quit key. */
96 int w32_quit_key;
98 /* Non nil if left window key events are passed on to Windows (this only
99 affects whether "tapping" the key opens the Start menu). */
100 Lisp_Object Vw32_pass_lwindow_to_system;
102 /* Non nil if right window key events are passed on to Windows (this
103 only affects whether "tapping" the key opens the Start menu). */
104 Lisp_Object Vw32_pass_rwindow_to_system;
106 /* Virtual key code used to generate "phantom" key presses in order
107 to stop system from acting on Windows key events. */
108 Lisp_Object Vw32_phantom_key_code;
110 /* Modifier associated with the left "Windows" key, or nil to act as a
111 normal key. */
112 Lisp_Object Vw32_lwindow_modifier;
114 /* Modifier associated with the right "Windows" key, or nil to act as a
115 normal key. */
116 Lisp_Object Vw32_rwindow_modifier;
118 /* Modifier associated with the "Apps" key, or nil to act as a normal
119 key. */
120 Lisp_Object Vw32_apps_modifier;
122 /* Value is nil if Num Lock acts as a function key. */
123 Lisp_Object Vw32_enable_num_lock;
125 /* Value is nil if Caps Lock acts as a function key. */
126 Lisp_Object Vw32_enable_caps_lock;
128 /* Modifier associated with Scroll Lock, or nil to act as a normal key. */
129 Lisp_Object Vw32_scroll_lock_modifier;
131 /* Switch to control whether we inhibit requests for synthesized bold
132 and italic versions of fonts. */
133 int w32_enable_synthesized_fonts;
135 /* Enable palette management. */
136 Lisp_Object Vw32_enable_palette;
138 /* Control how close left/right button down events must be to
139 be converted to a middle button down event. */
140 int w32_mouse_button_tolerance;
142 /* Minimum interval between mouse movement (and scroll bar drag)
143 events that are passed on to the event loop. */
144 int w32_mouse_move_interval;
146 /* Flag to indicate if XBUTTON events should be passed on to Windows. */
147 static int w32_pass_extra_mouse_buttons_to_system;
149 /* Flag to indicate if media keys should be passed on to Windows. */
150 static int w32_pass_multimedia_buttons_to_system;
152 /* Non nil if no window manager is in use. */
153 Lisp_Object Vx_no_window_manager;
155 /* If non-zero, a w32 timer that, when it expires, displays an
156 hourglass cursor on all frames. */
157 static unsigned hourglass_timer = 0;
158 static HWND hourglass_hwnd = NULL;
160 #if 0 /* TODO: Mouse cursor customization. */
161 /* The background and shape of the mouse pointer, and shape when not
162 over text or in the modeline. */
163 Lisp_Object Vx_pointer_shape, Vx_nontext_pointer_shape, Vx_mode_pointer_shape;
164 Lisp_Object Vx_hourglass_pointer_shape, Vx_window_horizontal_drag_shape;
166 /* The shape when over mouse-sensitive text. */
168 Lisp_Object Vx_sensitive_text_pointer_shape;
169 #endif
171 #ifndef IDC_HAND
172 #define IDC_HAND MAKEINTRESOURCE(32649)
173 #endif
175 /* Color of chars displayed in cursor box. */
176 Lisp_Object Vx_cursor_fore_pixel;
178 /* Nonzero if using Windows. */
180 static int w32_in_use;
182 /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'. */
184 Lisp_Object Vx_pixel_size_width_font_regexp;
186 /* Alist of bdf fonts and the files that define them. */
187 Lisp_Object Vw32_bdf_filename_alist;
189 /* A flag to control whether fonts are matched strictly or not. */
190 static int w32_strict_fontnames;
192 /* A flag to control whether we should only repaint if GetUpdateRect
193 indicates there is an update region. */
194 static int w32_strict_painting;
196 Lisp_Object Qnone;
197 Lisp_Object Qsuppress_icon;
198 Lisp_Object Qundefined_color;
199 Lisp_Object Qcancel_timer;
200 Lisp_Object Qfont_param;
201 Lisp_Object Qhyper;
202 Lisp_Object Qsuper;
203 Lisp_Object Qmeta;
204 Lisp_Object Qalt;
205 Lisp_Object Qctrl;
206 Lisp_Object Qcontrol;
207 Lisp_Object Qshift;
210 /* The ANSI codepage. */
211 int w32_ansi_code_page;
213 /* Prefix for system colors. */
214 #define SYSTEM_COLOR_PREFIX "System"
215 #define SYSTEM_COLOR_PREFIX_LEN (sizeof (SYSTEM_COLOR_PREFIX) - 1)
217 /* State variables for emulating a three button mouse. */
218 #define LMOUSE 1
219 #define MMOUSE 2
220 #define RMOUSE 4
222 static int button_state = 0;
223 static W32Msg saved_mouse_button_msg;
224 static unsigned mouse_button_timer = 0; /* non-zero when timer is active */
225 static W32Msg saved_mouse_move_msg;
226 static unsigned mouse_move_timer = 0;
228 /* Window that is tracking the mouse. */
229 static HWND track_mouse_window;
231 /* Multi-monitor API definitions that are not pulled from the headers
232 since we are compiling for NT 4. */
233 #ifndef MONITOR_DEFAULT_TO_NEAREST
234 #define MONITOR_DEFAULT_TO_NEAREST 2
235 #endif
236 /* MinGW headers define MONITORINFO unconditionally, but MSVC ones don't.
237 To avoid a compile error on one or the other, redefine with a new name. */
238 struct MONITOR_INFO
240 DWORD cbSize;
241 RECT rcMonitor;
242 RECT rcWork;
243 DWORD dwFlags;
246 /* Reportedly, VS 6 does not have this in its headers. */
247 #if defined(_MSC_VER) && _MSC_VER < 1300
248 DECLARE_HANDLE(HMONITOR);
249 #endif
251 typedef BOOL (WINAPI * TrackMouseEvent_Proc)
252 (IN OUT LPTRACKMOUSEEVENT lpEventTrack);
253 typedef LONG (WINAPI * ImmGetCompositionString_Proc)
254 (IN HIMC context, IN DWORD index, OUT LPVOID buffer, IN DWORD bufLen);
255 typedef HIMC (WINAPI * ImmGetContext_Proc) (IN HWND window);
256 typedef HWND (WINAPI * ImmReleaseContext_Proc) (IN HWND wnd, IN HIMC context);
257 typedef HWND (WINAPI * ImmSetCompositionWindow_Proc) (IN HIMC context,
258 IN COMPOSITIONFORM *form);
259 typedef HMONITOR (WINAPI * MonitorFromPoint_Proc) (IN POINT pt, IN DWORD flags);
260 typedef BOOL (WINAPI * GetMonitorInfo_Proc)
261 (IN HMONITOR monitor, OUT struct MONITOR_INFO* info);
263 TrackMouseEvent_Proc track_mouse_event_fn = NULL;
264 ClipboardSequence_Proc clipboard_sequence_fn = NULL;
265 ImmGetCompositionString_Proc get_composition_string_fn = NULL;
266 ImmGetContext_Proc get_ime_context_fn = NULL;
267 ImmReleaseContext_Proc release_ime_context_fn = NULL;
268 ImmSetCompositionWindow_Proc set_ime_composition_window_fn = NULL;
269 MonitorFromPoint_Proc monitor_from_point_fn = NULL;
270 GetMonitorInfo_Proc get_monitor_info_fn = NULL;
272 extern AppendMenuW_Proc unicode_append_menu;
274 /* Flag to selectively ignore WM_IME_CHAR messages. */
275 static int ignore_ime_char = 0;
277 /* W95 mousewheel handler */
278 unsigned int msh_mousewheel = 0;
280 /* Timers */
281 #define MOUSE_BUTTON_ID 1
282 #define MOUSE_MOVE_ID 2
283 #define MENU_FREE_ID 3
284 #define HOURGLASS_ID 4
285 /* The delay (milliseconds) before a menu is freed after WM_EXITMENULOOP
286 is received. */
287 #define MENU_FREE_DELAY 1000
288 static unsigned menu_free_timer = 0;
290 /* The below are defined in frame.c. */
292 extern Lisp_Object Vwindow_system_version;
294 #ifdef GLYPH_DEBUG
295 int image_cache_refcount, dpyinfo_refcount;
296 #endif
299 /* From w32term.c. */
300 extern int w32_num_mouse_buttons;
301 extern Lisp_Object Vw32_recognize_altgr;
303 extern HWND w32_system_caret_hwnd;
305 extern int w32_system_caret_height;
306 extern int w32_system_caret_x;
307 extern int w32_system_caret_y;
308 extern int w32_use_visible_system_caret;
310 static HWND w32_visible_system_caret_hwnd;
312 /* From w32menu.c */
313 extern HMENU current_popup_menu;
314 static int menubar_in_use = 0;
316 /* From w32uniscribe.c */
317 extern void syms_of_w32uniscribe ();
318 extern int uniscribe_available;
320 /* Function prototypes for hourglass support. */
321 static void w32_show_hourglass P_ ((struct frame *));
322 static void w32_hide_hourglass P_ ((void));
326 /* Error if we are not connected to MS-Windows. */
327 void
328 check_w32 ()
330 if (! w32_in_use)
331 error ("MS-Windows not in use or not initialized");
334 /* Nonzero if we can use mouse menus.
335 You should not call this unless HAVE_MENUS is defined. */
338 have_menus_p ()
340 return w32_in_use;
343 /* Extract a frame as a FRAME_PTR, defaulting to the selected frame
344 and checking validity for W32. */
346 FRAME_PTR
347 check_x_frame (frame)
348 Lisp_Object frame;
350 FRAME_PTR f;
352 if (NILP (frame))
353 frame = selected_frame;
354 CHECK_LIVE_FRAME (frame);
355 f = XFRAME (frame);
356 if (! FRAME_W32_P (f))
357 error ("Non-W32 frame used");
358 return f;
361 /* Let the user specify a display with a frame.
362 nil stands for the selected frame--or, if that is not a w32 frame,
363 the first display on the list. */
365 struct w32_display_info *
366 check_x_display_info (frame)
367 Lisp_Object frame;
369 if (NILP (frame))
371 struct frame *sf = XFRAME (selected_frame);
373 if (FRAME_W32_P (sf) && FRAME_LIVE_P (sf))
374 return FRAME_W32_DISPLAY_INFO (sf);
375 else
376 return &one_w32_display_info;
378 else if (STRINGP (frame))
379 return x_display_info_for_name (frame);
380 else
382 FRAME_PTR f;
384 CHECK_LIVE_FRAME (frame);
385 f = XFRAME (frame);
386 if (! FRAME_W32_P (f))
387 error ("Non-W32 frame used");
388 return FRAME_W32_DISPLAY_INFO (f);
392 /* Return the Emacs frame-object corresponding to an w32 window.
393 It could be the frame's main window or an icon window. */
395 /* This function can be called during GC, so use GC_xxx type test macros. */
397 struct frame *
398 x_window_to_frame (dpyinfo, wdesc)
399 struct w32_display_info *dpyinfo;
400 HWND wdesc;
402 Lisp_Object tail, frame;
403 struct frame *f;
405 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
407 frame = XCAR (tail);
408 if (!FRAMEP (frame))
409 continue;
410 f = XFRAME (frame);
411 if (!FRAME_W32_P (f) || FRAME_W32_DISPLAY_INFO (f) != dpyinfo)
412 continue;
414 if (FRAME_W32_WINDOW (f) == wdesc)
415 return f;
417 return 0;
421 static Lisp_Object unwind_create_frame P_ ((Lisp_Object));
422 static Lisp_Object unwind_create_tip_frame P_ ((Lisp_Object));
423 static void my_create_window P_ ((struct frame *));
424 static void my_create_tip_window P_ ((struct frame *));
426 /* TODO: Native Input Method support; see x_create_im. */
427 void x_set_foreground_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
428 void x_set_background_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
429 void x_set_mouse_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
430 void x_set_cursor_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
431 void x_set_border_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
432 void x_set_cursor_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
433 void x_set_icon_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
434 void x_set_icon_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
435 void x_explicitly_set_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
436 void x_set_menu_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
437 void x_set_title P_ ((struct frame *, Lisp_Object, Lisp_Object));
438 void x_set_tool_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
439 static void x_edge_detection P_ ((struct frame *, struct image *, Lisp_Object,
440 Lisp_Object));
445 /* Store the screen positions of frame F into XPTR and YPTR.
446 These are the positions of the containing window manager window,
447 not Emacs's own window. */
449 void
450 x_real_positions (f, xptr, yptr)
451 FRAME_PTR f;
452 int *xptr, *yptr;
454 POINT pt;
455 RECT rect;
457 /* Get the bounds of the WM window. */
458 GetWindowRect (FRAME_W32_WINDOW (f), &rect);
460 pt.x = 0;
461 pt.y = 0;
463 /* Convert (0, 0) in the client area to screen co-ordinates. */
464 ClientToScreen (FRAME_W32_WINDOW (f), &pt);
466 /* Remember x_pixels_diff and y_pixels_diff. */
467 f->x_pixels_diff = pt.x - rect.left;
468 f->y_pixels_diff = pt.y - rect.top;
470 *xptr = rect.left;
471 *yptr = rect.top;
476 DEFUN ("w32-define-rgb-color", Fw32_define_rgb_color,
477 Sw32_define_rgb_color, 4, 4, 0,
478 doc: /* Convert RGB numbers to a Windows color reference and associate with NAME.
479 This adds or updates a named color to `w32-color-map', making it
480 available for use. The original entry's RGB ref is returned, or nil
481 if the entry is new. */)
482 (red, green, blue, name)
483 Lisp_Object red, green, blue, name;
485 Lisp_Object rgb;
486 Lisp_Object oldrgb = Qnil;
487 Lisp_Object entry;
489 CHECK_NUMBER (red);
490 CHECK_NUMBER (green);
491 CHECK_NUMBER (blue);
492 CHECK_STRING (name);
494 XSETINT (rgb, RGB (XUINT (red), XUINT (green), XUINT (blue)));
496 BLOCK_INPUT;
498 /* replace existing entry in w32-color-map or add new entry. */
499 entry = Fassoc (name, Vw32_color_map);
500 if (NILP (entry))
502 entry = Fcons (name, rgb);
503 Vw32_color_map = Fcons (entry, Vw32_color_map);
505 else
507 oldrgb = Fcdr (entry);
508 Fsetcdr (entry, rgb);
511 UNBLOCK_INPUT;
513 return (oldrgb);
516 /* The default colors for the w32 color map */
517 typedef struct colormap_t
519 char *name;
520 COLORREF colorref;
521 } colormap_t;
523 colormap_t w32_color_map[] =
525 {"snow" , PALETTERGB (255,250,250)},
526 {"ghost white" , PALETTERGB (248,248,255)},
527 {"GhostWhite" , PALETTERGB (248,248,255)},
528 {"white smoke" , PALETTERGB (245,245,245)},
529 {"WhiteSmoke" , PALETTERGB (245,245,245)},
530 {"gainsboro" , PALETTERGB (220,220,220)},
531 {"floral white" , PALETTERGB (255,250,240)},
532 {"FloralWhite" , PALETTERGB (255,250,240)},
533 {"old lace" , PALETTERGB (253,245,230)},
534 {"OldLace" , PALETTERGB (253,245,230)},
535 {"linen" , PALETTERGB (250,240,230)},
536 {"antique white" , PALETTERGB (250,235,215)},
537 {"AntiqueWhite" , PALETTERGB (250,235,215)},
538 {"papaya whip" , PALETTERGB (255,239,213)},
539 {"PapayaWhip" , PALETTERGB (255,239,213)},
540 {"blanched almond" , PALETTERGB (255,235,205)},
541 {"BlanchedAlmond" , PALETTERGB (255,235,205)},
542 {"bisque" , PALETTERGB (255,228,196)},
543 {"peach puff" , PALETTERGB (255,218,185)},
544 {"PeachPuff" , PALETTERGB (255,218,185)},
545 {"navajo white" , PALETTERGB (255,222,173)},
546 {"NavajoWhite" , PALETTERGB (255,222,173)},
547 {"moccasin" , PALETTERGB (255,228,181)},
548 {"cornsilk" , PALETTERGB (255,248,220)},
549 {"ivory" , PALETTERGB (255,255,240)},
550 {"lemon chiffon" , PALETTERGB (255,250,205)},
551 {"LemonChiffon" , PALETTERGB (255,250,205)},
552 {"seashell" , PALETTERGB (255,245,238)},
553 {"honeydew" , PALETTERGB (240,255,240)},
554 {"mint cream" , PALETTERGB (245,255,250)},
555 {"MintCream" , PALETTERGB (245,255,250)},
556 {"azure" , PALETTERGB (240,255,255)},
557 {"alice blue" , PALETTERGB (240,248,255)},
558 {"AliceBlue" , PALETTERGB (240,248,255)},
559 {"lavender" , PALETTERGB (230,230,250)},
560 {"lavender blush" , PALETTERGB (255,240,245)},
561 {"LavenderBlush" , PALETTERGB (255,240,245)},
562 {"misty rose" , PALETTERGB (255,228,225)},
563 {"MistyRose" , PALETTERGB (255,228,225)},
564 {"white" , PALETTERGB (255,255,255)},
565 {"black" , PALETTERGB ( 0, 0, 0)},
566 {"dark slate gray" , PALETTERGB ( 47, 79, 79)},
567 {"DarkSlateGray" , PALETTERGB ( 47, 79, 79)},
568 {"dark slate grey" , PALETTERGB ( 47, 79, 79)},
569 {"DarkSlateGrey" , PALETTERGB ( 47, 79, 79)},
570 {"dim gray" , PALETTERGB (105,105,105)},
571 {"DimGray" , PALETTERGB (105,105,105)},
572 {"dim grey" , PALETTERGB (105,105,105)},
573 {"DimGrey" , PALETTERGB (105,105,105)},
574 {"slate gray" , PALETTERGB (112,128,144)},
575 {"SlateGray" , PALETTERGB (112,128,144)},
576 {"slate grey" , PALETTERGB (112,128,144)},
577 {"SlateGrey" , PALETTERGB (112,128,144)},
578 {"light slate gray" , PALETTERGB (119,136,153)},
579 {"LightSlateGray" , PALETTERGB (119,136,153)},
580 {"light slate grey" , PALETTERGB (119,136,153)},
581 {"LightSlateGrey" , PALETTERGB (119,136,153)},
582 {"gray" , PALETTERGB (190,190,190)},
583 {"grey" , PALETTERGB (190,190,190)},
584 {"light grey" , PALETTERGB (211,211,211)},
585 {"LightGrey" , PALETTERGB (211,211,211)},
586 {"light gray" , PALETTERGB (211,211,211)},
587 {"LightGray" , PALETTERGB (211,211,211)},
588 {"midnight blue" , PALETTERGB ( 25, 25,112)},
589 {"MidnightBlue" , PALETTERGB ( 25, 25,112)},
590 {"navy" , PALETTERGB ( 0, 0,128)},
591 {"navy blue" , PALETTERGB ( 0, 0,128)},
592 {"NavyBlue" , PALETTERGB ( 0, 0,128)},
593 {"cornflower blue" , PALETTERGB (100,149,237)},
594 {"CornflowerBlue" , PALETTERGB (100,149,237)},
595 {"dark slate blue" , PALETTERGB ( 72, 61,139)},
596 {"DarkSlateBlue" , PALETTERGB ( 72, 61,139)},
597 {"slate blue" , PALETTERGB (106, 90,205)},
598 {"SlateBlue" , PALETTERGB (106, 90,205)},
599 {"medium slate blue" , PALETTERGB (123,104,238)},
600 {"MediumSlateBlue" , PALETTERGB (123,104,238)},
601 {"light slate blue" , PALETTERGB (132,112,255)},
602 {"LightSlateBlue" , PALETTERGB (132,112,255)},
603 {"medium blue" , PALETTERGB ( 0, 0,205)},
604 {"MediumBlue" , PALETTERGB ( 0, 0,205)},
605 {"royal blue" , PALETTERGB ( 65,105,225)},
606 {"RoyalBlue" , PALETTERGB ( 65,105,225)},
607 {"blue" , PALETTERGB ( 0, 0,255)},
608 {"dodger blue" , PALETTERGB ( 30,144,255)},
609 {"DodgerBlue" , PALETTERGB ( 30,144,255)},
610 {"deep sky blue" , PALETTERGB ( 0,191,255)},
611 {"DeepSkyBlue" , PALETTERGB ( 0,191,255)},
612 {"sky blue" , PALETTERGB (135,206,235)},
613 {"SkyBlue" , PALETTERGB (135,206,235)},
614 {"light sky blue" , PALETTERGB (135,206,250)},
615 {"LightSkyBlue" , PALETTERGB (135,206,250)},
616 {"steel blue" , PALETTERGB ( 70,130,180)},
617 {"SteelBlue" , PALETTERGB ( 70,130,180)},
618 {"light steel blue" , PALETTERGB (176,196,222)},
619 {"LightSteelBlue" , PALETTERGB (176,196,222)},
620 {"light blue" , PALETTERGB (173,216,230)},
621 {"LightBlue" , PALETTERGB (173,216,230)},
622 {"powder blue" , PALETTERGB (176,224,230)},
623 {"PowderBlue" , PALETTERGB (176,224,230)},
624 {"pale turquoise" , PALETTERGB (175,238,238)},
625 {"PaleTurquoise" , PALETTERGB (175,238,238)},
626 {"dark turquoise" , PALETTERGB ( 0,206,209)},
627 {"DarkTurquoise" , PALETTERGB ( 0,206,209)},
628 {"medium turquoise" , PALETTERGB ( 72,209,204)},
629 {"MediumTurquoise" , PALETTERGB ( 72,209,204)},
630 {"turquoise" , PALETTERGB ( 64,224,208)},
631 {"cyan" , PALETTERGB ( 0,255,255)},
632 {"light cyan" , PALETTERGB (224,255,255)},
633 {"LightCyan" , PALETTERGB (224,255,255)},
634 {"cadet blue" , PALETTERGB ( 95,158,160)},
635 {"CadetBlue" , PALETTERGB ( 95,158,160)},
636 {"medium aquamarine" , PALETTERGB (102,205,170)},
637 {"MediumAquamarine" , PALETTERGB (102,205,170)},
638 {"aquamarine" , PALETTERGB (127,255,212)},
639 {"dark green" , PALETTERGB ( 0,100, 0)},
640 {"DarkGreen" , PALETTERGB ( 0,100, 0)},
641 {"dark olive green" , PALETTERGB ( 85,107, 47)},
642 {"DarkOliveGreen" , PALETTERGB ( 85,107, 47)},
643 {"dark sea green" , PALETTERGB (143,188,143)},
644 {"DarkSeaGreen" , PALETTERGB (143,188,143)},
645 {"sea green" , PALETTERGB ( 46,139, 87)},
646 {"SeaGreen" , PALETTERGB ( 46,139, 87)},
647 {"medium sea green" , PALETTERGB ( 60,179,113)},
648 {"MediumSeaGreen" , PALETTERGB ( 60,179,113)},
649 {"light sea green" , PALETTERGB ( 32,178,170)},
650 {"LightSeaGreen" , PALETTERGB ( 32,178,170)},
651 {"pale green" , PALETTERGB (152,251,152)},
652 {"PaleGreen" , PALETTERGB (152,251,152)},
653 {"spring green" , PALETTERGB ( 0,255,127)},
654 {"SpringGreen" , PALETTERGB ( 0,255,127)},
655 {"lawn green" , PALETTERGB (124,252, 0)},
656 {"LawnGreen" , PALETTERGB (124,252, 0)},
657 {"green" , PALETTERGB ( 0,255, 0)},
658 {"chartreuse" , PALETTERGB (127,255, 0)},
659 {"medium spring green" , PALETTERGB ( 0,250,154)},
660 {"MediumSpringGreen" , PALETTERGB ( 0,250,154)},
661 {"green yellow" , PALETTERGB (173,255, 47)},
662 {"GreenYellow" , PALETTERGB (173,255, 47)},
663 {"lime green" , PALETTERGB ( 50,205, 50)},
664 {"LimeGreen" , PALETTERGB ( 50,205, 50)},
665 {"yellow green" , PALETTERGB (154,205, 50)},
666 {"YellowGreen" , PALETTERGB (154,205, 50)},
667 {"forest green" , PALETTERGB ( 34,139, 34)},
668 {"ForestGreen" , PALETTERGB ( 34,139, 34)},
669 {"olive drab" , PALETTERGB (107,142, 35)},
670 {"OliveDrab" , PALETTERGB (107,142, 35)},
671 {"dark khaki" , PALETTERGB (189,183,107)},
672 {"DarkKhaki" , PALETTERGB (189,183,107)},
673 {"khaki" , PALETTERGB (240,230,140)},
674 {"pale goldenrod" , PALETTERGB (238,232,170)},
675 {"PaleGoldenrod" , PALETTERGB (238,232,170)},
676 {"light goldenrod yellow" , PALETTERGB (250,250,210)},
677 {"LightGoldenrodYellow" , PALETTERGB (250,250,210)},
678 {"light yellow" , PALETTERGB (255,255,224)},
679 {"LightYellow" , PALETTERGB (255,255,224)},
680 {"yellow" , PALETTERGB (255,255, 0)},
681 {"gold" , PALETTERGB (255,215, 0)},
682 {"light goldenrod" , PALETTERGB (238,221,130)},
683 {"LightGoldenrod" , PALETTERGB (238,221,130)},
684 {"goldenrod" , PALETTERGB (218,165, 32)},
685 {"dark goldenrod" , PALETTERGB (184,134, 11)},
686 {"DarkGoldenrod" , PALETTERGB (184,134, 11)},
687 {"rosy brown" , PALETTERGB (188,143,143)},
688 {"RosyBrown" , PALETTERGB (188,143,143)},
689 {"indian red" , PALETTERGB (205, 92, 92)},
690 {"IndianRed" , PALETTERGB (205, 92, 92)},
691 {"saddle brown" , PALETTERGB (139, 69, 19)},
692 {"SaddleBrown" , PALETTERGB (139, 69, 19)},
693 {"sienna" , PALETTERGB (160, 82, 45)},
694 {"peru" , PALETTERGB (205,133, 63)},
695 {"burlywood" , PALETTERGB (222,184,135)},
696 {"beige" , PALETTERGB (245,245,220)},
697 {"wheat" , PALETTERGB (245,222,179)},
698 {"sandy brown" , PALETTERGB (244,164, 96)},
699 {"SandyBrown" , PALETTERGB (244,164, 96)},
700 {"tan" , PALETTERGB (210,180,140)},
701 {"chocolate" , PALETTERGB (210,105, 30)},
702 {"firebrick" , PALETTERGB (178,34, 34)},
703 {"brown" , PALETTERGB (165,42, 42)},
704 {"dark salmon" , PALETTERGB (233,150,122)},
705 {"DarkSalmon" , PALETTERGB (233,150,122)},
706 {"salmon" , PALETTERGB (250,128,114)},
707 {"light salmon" , PALETTERGB (255,160,122)},
708 {"LightSalmon" , PALETTERGB (255,160,122)},
709 {"orange" , PALETTERGB (255,165, 0)},
710 {"dark orange" , PALETTERGB (255,140, 0)},
711 {"DarkOrange" , PALETTERGB (255,140, 0)},
712 {"coral" , PALETTERGB (255,127, 80)},
713 {"light coral" , PALETTERGB (240,128,128)},
714 {"LightCoral" , PALETTERGB (240,128,128)},
715 {"tomato" , PALETTERGB (255, 99, 71)},
716 {"orange red" , PALETTERGB (255, 69, 0)},
717 {"OrangeRed" , PALETTERGB (255, 69, 0)},
718 {"red" , PALETTERGB (255, 0, 0)},
719 {"hot pink" , PALETTERGB (255,105,180)},
720 {"HotPink" , PALETTERGB (255,105,180)},
721 {"deep pink" , PALETTERGB (255, 20,147)},
722 {"DeepPink" , PALETTERGB (255, 20,147)},
723 {"pink" , PALETTERGB (255,192,203)},
724 {"light pink" , PALETTERGB (255,182,193)},
725 {"LightPink" , PALETTERGB (255,182,193)},
726 {"pale violet red" , PALETTERGB (219,112,147)},
727 {"PaleVioletRed" , PALETTERGB (219,112,147)},
728 {"maroon" , PALETTERGB (176, 48, 96)},
729 {"medium violet red" , PALETTERGB (199, 21,133)},
730 {"MediumVioletRed" , PALETTERGB (199, 21,133)},
731 {"violet red" , PALETTERGB (208, 32,144)},
732 {"VioletRed" , PALETTERGB (208, 32,144)},
733 {"magenta" , PALETTERGB (255, 0,255)},
734 {"violet" , PALETTERGB (238,130,238)},
735 {"plum" , PALETTERGB (221,160,221)},
736 {"orchid" , PALETTERGB (218,112,214)},
737 {"medium orchid" , PALETTERGB (186, 85,211)},
738 {"MediumOrchid" , PALETTERGB (186, 85,211)},
739 {"dark orchid" , PALETTERGB (153, 50,204)},
740 {"DarkOrchid" , PALETTERGB (153, 50,204)},
741 {"dark violet" , PALETTERGB (148, 0,211)},
742 {"DarkViolet" , PALETTERGB (148, 0,211)},
743 {"blue violet" , PALETTERGB (138, 43,226)},
744 {"BlueViolet" , PALETTERGB (138, 43,226)},
745 {"purple" , PALETTERGB (160, 32,240)},
746 {"medium purple" , PALETTERGB (147,112,219)},
747 {"MediumPurple" , PALETTERGB (147,112,219)},
748 {"thistle" , PALETTERGB (216,191,216)},
749 {"gray0" , PALETTERGB ( 0, 0, 0)},
750 {"grey0" , PALETTERGB ( 0, 0, 0)},
751 {"dark grey" , PALETTERGB (169,169,169)},
752 {"DarkGrey" , PALETTERGB (169,169,169)},
753 {"dark gray" , PALETTERGB (169,169,169)},
754 {"DarkGray" , PALETTERGB (169,169,169)},
755 {"dark blue" , PALETTERGB ( 0, 0,139)},
756 {"DarkBlue" , PALETTERGB ( 0, 0,139)},
757 {"dark cyan" , PALETTERGB ( 0,139,139)},
758 {"DarkCyan" , PALETTERGB ( 0,139,139)},
759 {"dark magenta" , PALETTERGB (139, 0,139)},
760 {"DarkMagenta" , PALETTERGB (139, 0,139)},
761 {"dark red" , PALETTERGB (139, 0, 0)},
762 {"DarkRed" , PALETTERGB (139, 0, 0)},
763 {"light green" , PALETTERGB (144,238,144)},
764 {"LightGreen" , PALETTERGB (144,238,144)},
767 DEFUN ("w32-default-color-map", Fw32_default_color_map, Sw32_default_color_map,
768 0, 0, 0, doc: /* Return the default color map. */)
771 int i;
772 colormap_t *pc = w32_color_map;
773 Lisp_Object cmap;
775 BLOCK_INPUT;
777 cmap = Qnil;
779 for (i = 0; i < sizeof (w32_color_map) / sizeof (w32_color_map[0]);
780 pc++, i++)
781 cmap = Fcons (Fcons (build_string (pc->name),
782 make_number (pc->colorref)),
783 cmap);
785 UNBLOCK_INPUT;
787 return (cmap);
790 static Lisp_Object
791 w32_to_x_color (rgb)
792 Lisp_Object rgb;
794 Lisp_Object color;
796 CHECK_NUMBER (rgb);
798 BLOCK_INPUT;
800 color = Frassq (rgb, Vw32_color_map);
802 UNBLOCK_INPUT;
804 if (!NILP (color))
805 return (Fcar (color));
806 else
807 return Qnil;
810 static Lisp_Object
811 w32_color_map_lookup (colorname)
812 char *colorname;
814 Lisp_Object tail, ret = Qnil;
816 BLOCK_INPUT;
818 for (tail = Vw32_color_map; CONSP (tail); tail = XCDR (tail))
820 register Lisp_Object elt, tem;
822 elt = XCAR (tail);
823 if (!CONSP (elt)) continue;
825 tem = Fcar (elt);
827 if (lstrcmpi (SDATA (tem), colorname) == 0)
829 ret = Fcdr (elt);
830 break;
833 QUIT;
837 UNBLOCK_INPUT;
839 return ret;
843 static void
844 add_system_logical_colors_to_map (system_colors)
845 Lisp_Object *system_colors;
847 HKEY colors_key;
849 /* Other registry operations are done with input blocked. */
850 BLOCK_INPUT;
852 /* Look for "Control Panel/Colors" under User and Machine registry
853 settings. */
854 if (RegOpenKeyEx (HKEY_CURRENT_USER, "Control Panel\\Colors", 0,
855 KEY_READ, &colors_key) == ERROR_SUCCESS
856 || RegOpenKeyEx (HKEY_LOCAL_MACHINE, "Control Panel\\Colors", 0,
857 KEY_READ, &colors_key) == ERROR_SUCCESS)
859 /* List all keys. */
860 char color_buffer[64];
861 char full_name_buffer[MAX_PATH + SYSTEM_COLOR_PREFIX_LEN];
862 int index = 0;
863 DWORD name_size, color_size;
864 char *name_buffer = full_name_buffer + SYSTEM_COLOR_PREFIX_LEN;
866 name_size = sizeof (full_name_buffer) - SYSTEM_COLOR_PREFIX_LEN;
867 color_size = sizeof (color_buffer);
869 strcpy (full_name_buffer, SYSTEM_COLOR_PREFIX);
871 while (RegEnumValueA (colors_key, index, name_buffer, &name_size,
872 NULL, NULL, color_buffer, &color_size)
873 == ERROR_SUCCESS)
875 int r, g, b;
876 if (sscanf (color_buffer, " %u %u %u", &r, &g, &b) == 3)
877 *system_colors = Fcons (Fcons (build_string (full_name_buffer),
878 make_number (RGB (r, g, b))),
879 *system_colors);
881 name_size = sizeof (full_name_buffer) - SYSTEM_COLOR_PREFIX_LEN;
882 color_size = sizeof (color_buffer);
883 index++;
885 RegCloseKey (colors_key);
888 UNBLOCK_INPUT;
892 static Lisp_Object
893 x_to_w32_color (colorname)
894 char * colorname;
896 register Lisp_Object ret = Qnil;
898 BLOCK_INPUT;
900 if (colorname[0] == '#')
902 /* Could be an old-style RGB Device specification. */
903 char *color;
904 int size;
905 color = colorname + 1;
907 size = strlen (color);
908 if (size == 3 || size == 6 || size == 9 || size == 12)
910 UINT colorval;
911 int i, pos;
912 pos = 0;
913 size /= 3;
914 colorval = 0;
916 for (i = 0; i < 3; i++)
918 char *end;
919 char t;
920 unsigned long value;
922 /* The check for 'x' in the following conditional takes into
923 account the fact that strtol allows a "0x" in front of
924 our numbers, and we don't. */
925 if (!isxdigit (color[0]) || color[1] == 'x')
926 break;
927 t = color[size];
928 color[size] = '\0';
929 value = strtoul (color, &end, 16);
930 color[size] = t;
931 if (errno == ERANGE || end - color != size)
932 break;
933 switch (size)
935 case 1:
936 value = value * 0x10;
937 break;
938 case 2:
939 break;
940 case 3:
941 value /= 0x10;
942 break;
943 case 4:
944 value /= 0x100;
945 break;
947 colorval |= (value << pos);
948 pos += 0x8;
949 if (i == 2)
951 UNBLOCK_INPUT;
952 XSETINT (ret, colorval);
953 return ret;
955 color = end;
959 else if (strnicmp (colorname, "rgb:", 4) == 0)
961 char *color;
962 UINT colorval;
963 int i, pos;
964 pos = 0;
966 colorval = 0;
967 color = colorname + 4;
968 for (i = 0; i < 3; i++)
970 char *end;
971 unsigned long value;
973 /* The check for 'x' in the following conditional takes into
974 account the fact that strtol allows a "0x" in front of
975 our numbers, and we don't. */
976 if (!isxdigit (color[0]) || color[1] == 'x')
977 break;
978 value = strtoul (color, &end, 16);
979 if (errno == ERANGE)
980 break;
981 switch (end - color)
983 case 1:
984 value = value * 0x10 + value;
985 break;
986 case 2:
987 break;
988 case 3:
989 value /= 0x10;
990 break;
991 case 4:
992 value /= 0x100;
993 break;
994 default:
995 value = ULONG_MAX;
997 if (value == ULONG_MAX)
998 break;
999 colorval |= (value << pos);
1000 pos += 0x8;
1001 if (i == 2)
1003 if (*end != '\0')
1004 break;
1005 UNBLOCK_INPUT;
1006 XSETINT (ret, colorval);
1007 return ret;
1009 if (*end != '/')
1010 break;
1011 color = end + 1;
1014 else if (strnicmp (colorname, "rgbi:", 5) == 0)
1016 /* This is an RGB Intensity specification. */
1017 char *color;
1018 UINT colorval;
1019 int i, pos;
1020 pos = 0;
1022 colorval = 0;
1023 color = colorname + 5;
1024 for (i = 0; i < 3; i++)
1026 char *end;
1027 double value;
1028 UINT val;
1030 value = strtod (color, &end);
1031 if (errno == ERANGE)
1032 break;
1033 if (value < 0.0 || value > 1.0)
1034 break;
1035 val = (UINT)(0x100 * value);
1036 /* We used 0x100 instead of 0xFF to give a continuous
1037 range between 0.0 and 1.0 inclusive. The next statement
1038 fixes the 1.0 case. */
1039 if (val == 0x100)
1040 val = 0xFF;
1041 colorval |= (val << pos);
1042 pos += 0x8;
1043 if (i == 2)
1045 if (*end != '\0')
1046 break;
1047 UNBLOCK_INPUT;
1048 XSETINT (ret, colorval);
1049 return ret;
1051 if (*end != '/')
1052 break;
1053 color = end + 1;
1056 /* I am not going to attempt to handle any of the CIE color schemes
1057 or TekHVC, since I don't know the algorithms for conversion to
1058 RGB. */
1060 /* If we fail to lookup the color name in w32_color_map, then check the
1061 colorname to see if it can be crudely approximated: If the X color
1062 ends in a number (e.g., "darkseagreen2"), strip the number and
1063 return the result of looking up the base color name. */
1064 ret = w32_color_map_lookup (colorname);
1065 if (NILP (ret))
1067 int len = strlen (colorname);
1069 if (isdigit (colorname[len - 1]))
1071 char *ptr, *approx = alloca (len + 1);
1073 strcpy (approx, colorname);
1074 ptr = &approx[len - 1];
1075 while (ptr > approx && isdigit (*ptr))
1076 *ptr-- = '\0';
1078 ret = w32_color_map_lookup (approx);
1082 UNBLOCK_INPUT;
1083 return ret;
1086 void
1087 w32_regenerate_palette (FRAME_PTR f)
1089 struct w32_palette_entry * list;
1090 LOGPALETTE * log_palette;
1091 HPALETTE new_palette;
1092 int i;
1094 /* don't bother trying to create palette if not supported */
1095 if (! FRAME_W32_DISPLAY_INFO (f)->has_palette)
1096 return;
1098 log_palette = (LOGPALETTE *)
1099 alloca (sizeof (LOGPALETTE) +
1100 FRAME_W32_DISPLAY_INFO (f)->num_colors * sizeof (PALETTEENTRY));
1101 log_palette->palVersion = 0x300;
1102 log_palette->palNumEntries = FRAME_W32_DISPLAY_INFO (f)->num_colors;
1104 list = FRAME_W32_DISPLAY_INFO (f)->color_list;
1105 for (i = 0;
1106 i < FRAME_W32_DISPLAY_INFO (f)->num_colors;
1107 i++, list = list->next)
1108 log_palette->palPalEntry[i] = list->entry;
1110 new_palette = CreatePalette (log_palette);
1112 enter_crit ();
1114 if (FRAME_W32_DISPLAY_INFO (f)->palette)
1115 DeleteObject (FRAME_W32_DISPLAY_INFO (f)->palette);
1116 FRAME_W32_DISPLAY_INFO (f)->palette = new_palette;
1118 /* Realize display palette and garbage all frames. */
1119 release_frame_dc (f, get_frame_dc (f));
1121 leave_crit ();
1124 #define W32_COLOR(pe) RGB (pe.peRed, pe.peGreen, pe.peBlue)
1125 #define SET_W32_COLOR(pe, color) \
1126 do \
1128 pe.peRed = GetRValue (color); \
1129 pe.peGreen = GetGValue (color); \
1130 pe.peBlue = GetBValue (color); \
1131 pe.peFlags = 0; \
1132 } while (0)
1134 #if 0
1135 /* Keep these around in case we ever want to track color usage. */
1136 void
1137 w32_map_color (FRAME_PTR f, COLORREF color)
1139 struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
1141 if (NILP (Vw32_enable_palette))
1142 return;
1144 /* check if color is already mapped */
1145 while (list)
1147 if (W32_COLOR (list->entry) == color)
1149 ++list->refcount;
1150 return;
1152 list = list->next;
1155 /* not already mapped, so add to list and recreate Windows palette */
1156 list = (struct w32_palette_entry *)
1157 xmalloc (sizeof (struct w32_palette_entry));
1158 SET_W32_COLOR (list->entry, color);
1159 list->refcount = 1;
1160 list->next = FRAME_W32_DISPLAY_INFO (f)->color_list;
1161 FRAME_W32_DISPLAY_INFO (f)->color_list = list;
1162 FRAME_W32_DISPLAY_INFO (f)->num_colors++;
1164 /* set flag that palette must be regenerated */
1165 FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
1168 void
1169 w32_unmap_color (FRAME_PTR f, COLORREF color)
1171 struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
1172 struct w32_palette_entry **prev = &FRAME_W32_DISPLAY_INFO (f)->color_list;
1174 if (NILP (Vw32_enable_palette))
1175 return;
1177 /* check if color is already mapped */
1178 while (list)
1180 if (W32_COLOR (list->entry) == color)
1182 if (--list->refcount == 0)
1184 *prev = list->next;
1185 xfree (list);
1186 FRAME_W32_DISPLAY_INFO (f)->num_colors--;
1187 break;
1189 else
1190 return;
1192 prev = &list->next;
1193 list = list->next;
1196 /* set flag that palette must be regenerated */
1197 FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
1199 #endif
1202 /* Gamma-correct COLOR on frame F. */
1204 void
1205 gamma_correct (f, color)
1206 struct frame *f;
1207 COLORREF *color;
1209 if (f->gamma)
1211 *color = PALETTERGB (
1212 pow (GetRValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
1213 pow (GetGValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
1214 pow (GetBValue (*color) / 255.0, f->gamma) * 255.0 + 0.5);
1219 /* Decide if color named COLOR is valid for the display associated with
1220 the selected frame; if so, return the rgb values in COLOR_DEF.
1221 If ALLOC is nonzero, allocate a new colormap cell. */
1224 w32_defined_color (f, color, color_def, alloc)
1225 FRAME_PTR f;
1226 char *color;
1227 XColor *color_def;
1228 int alloc;
1230 register Lisp_Object tem;
1231 COLORREF w32_color_ref;
1233 tem = x_to_w32_color (color);
1235 if (!NILP (tem))
1237 if (f)
1239 /* Apply gamma correction. */
1240 w32_color_ref = XUINT (tem);
1241 gamma_correct (f, &w32_color_ref);
1242 XSETINT (tem, w32_color_ref);
1245 /* Map this color to the palette if it is enabled. */
1246 if (!NILP (Vw32_enable_palette))
1248 struct w32_palette_entry * entry =
1249 one_w32_display_info.color_list;
1250 struct w32_palette_entry ** prev =
1251 &one_w32_display_info.color_list;
1253 /* check if color is already mapped */
1254 while (entry)
1256 if (W32_COLOR (entry->entry) == XUINT (tem))
1257 break;
1258 prev = &entry->next;
1259 entry = entry->next;
1262 if (entry == NULL && alloc)
1264 /* not already mapped, so add to list */
1265 entry = (struct w32_palette_entry *)
1266 xmalloc (sizeof (struct w32_palette_entry));
1267 SET_W32_COLOR (entry->entry, XUINT (tem));
1268 entry->next = NULL;
1269 *prev = entry;
1270 one_w32_display_info.num_colors++;
1272 /* set flag that palette must be regenerated */
1273 one_w32_display_info.regen_palette = TRUE;
1276 /* Ensure COLORREF value is snapped to nearest color in (default)
1277 palette by simulating the PALETTERGB macro. This works whether
1278 or not the display device has a palette. */
1279 w32_color_ref = XUINT (tem) | 0x2000000;
1281 color_def->pixel = w32_color_ref;
1282 color_def->red = GetRValue (w32_color_ref) * 256;
1283 color_def->green = GetGValue (w32_color_ref) * 256;
1284 color_def->blue = GetBValue (w32_color_ref) * 256;
1286 return 1;
1288 else
1290 return 0;
1294 /* Given a string ARG naming a color, compute a pixel value from it
1295 suitable for screen F.
1296 If F is not a color screen, return DEF (default) regardless of what
1297 ARG says. */
1300 x_decode_color (f, arg, def)
1301 FRAME_PTR f;
1302 Lisp_Object arg;
1303 int def;
1305 XColor cdef;
1307 CHECK_STRING (arg);
1309 if (strcmp (SDATA (arg), "black") == 0)
1310 return BLACK_PIX_DEFAULT (f);
1311 else if (strcmp (SDATA (arg), "white") == 0)
1312 return WHITE_PIX_DEFAULT (f);
1314 if ((FRAME_W32_DISPLAY_INFO (f)->n_planes * FRAME_W32_DISPLAY_INFO (f)->n_cbits) == 1)
1315 return def;
1317 /* w32_defined_color is responsible for coping with failures
1318 by looking for a near-miss. */
1319 if (w32_defined_color (f, SDATA (arg), &cdef, 1))
1320 return cdef.pixel;
1322 /* defined_color failed; return an ultimate default. */
1323 return def;
1328 /* Functions called only from `x_set_frame_param'
1329 to set individual parameters.
1331 If FRAME_W32_WINDOW (f) is 0,
1332 the frame is being created and its window does not exist yet.
1333 In that case, just record the parameter's new value
1334 in the standard place; do not attempt to change the window. */
1336 void
1337 x_set_foreground_color (f, arg, oldval)
1338 struct frame *f;
1339 Lisp_Object arg, oldval;
1341 struct w32_output *x = f->output_data.w32;
1342 PIX_TYPE fg, old_fg;
1344 fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1345 old_fg = FRAME_FOREGROUND_PIXEL (f);
1346 FRAME_FOREGROUND_PIXEL (f) = fg;
1348 if (FRAME_W32_WINDOW (f) != 0)
1350 if (x->cursor_pixel == old_fg)
1351 x->cursor_pixel = fg;
1353 update_face_from_frame_parameter (f, Qforeground_color, arg);
1354 if (FRAME_VISIBLE_P (f))
1355 redraw_frame (f);
1359 void
1360 x_set_background_color (f, arg, oldval)
1361 struct frame *f;
1362 Lisp_Object arg, oldval;
1364 FRAME_BACKGROUND_PIXEL (f)
1365 = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
1367 if (FRAME_W32_WINDOW (f) != 0)
1369 SetWindowLong (FRAME_W32_WINDOW (f), WND_BACKGROUND_INDEX,
1370 FRAME_BACKGROUND_PIXEL (f));
1372 update_face_from_frame_parameter (f, Qbackground_color, arg);
1374 if (FRAME_VISIBLE_P (f))
1375 redraw_frame (f);
1379 void
1380 x_set_mouse_color (f, arg, oldval)
1381 struct frame *f;
1382 Lisp_Object arg, oldval;
1384 Cursor cursor, nontext_cursor, mode_cursor, hand_cursor;
1385 int count;
1386 int mask_color;
1388 if (!EQ (Qnil, arg))
1389 f->output_data.w32->mouse_pixel
1390 = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1391 mask_color = FRAME_BACKGROUND_PIXEL (f);
1393 /* Don't let pointers be invisible. */
1394 if (mask_color == f->output_data.w32->mouse_pixel
1395 && mask_color == FRAME_BACKGROUND_PIXEL (f))
1396 f->output_data.w32->mouse_pixel = FRAME_FOREGROUND_PIXEL (f);
1398 #if 0 /* TODO : Mouse cursor customization. */
1399 BLOCK_INPUT;
1401 /* It's not okay to crash if the user selects a screwy cursor. */
1402 count = x_catch_errors (FRAME_W32_DISPLAY (f));
1404 if (!EQ (Qnil, Vx_pointer_shape))
1406 CHECK_NUMBER (Vx_pointer_shape);
1407 cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XINT (Vx_pointer_shape));
1409 else
1410 cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
1411 x_check_errors (FRAME_W32_DISPLAY (f), "bad text pointer cursor: %s");
1413 if (!EQ (Qnil, Vx_nontext_pointer_shape))
1415 CHECK_NUMBER (Vx_nontext_pointer_shape);
1416 nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1417 XINT (Vx_nontext_pointer_shape));
1419 else
1420 nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_left_ptr);
1421 x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
1423 if (!EQ (Qnil, Vx_hourglass_pointer_shape))
1425 CHECK_NUMBER (Vx_hourglass_pointer_shape);
1426 hourglass_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1427 XINT (Vx_hourglass_pointer_shape));
1429 else
1430 hourglass_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_watch);
1431 x_check_errors (FRAME_W32_DISPLAY (f), "bad busy pointer cursor: %s");
1433 x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
1434 if (!EQ (Qnil, Vx_mode_pointer_shape))
1436 CHECK_NUMBER (Vx_mode_pointer_shape);
1437 mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1438 XINT (Vx_mode_pointer_shape));
1440 else
1441 mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
1442 x_check_errors (FRAME_W32_DISPLAY (f), "bad modeline pointer cursor: %s");
1444 if (!EQ (Qnil, Vx_sensitive_text_pointer_shape))
1446 CHECK_NUMBER (Vx_sensitive_text_pointer_shape);
1447 hand_cursor
1448 = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1449 XINT (Vx_sensitive_text_pointer_shape));
1451 else
1452 hand_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_crosshair);
1454 if (!NILP (Vx_window_horizontal_drag_shape))
1456 CHECK_NUMBER (Vx_window_horizontal_drag_shape);
1457 horizontal_drag_cursor
1458 = XCreateFontCursor (FRAME_X_DISPLAY (f),
1459 XINT (Vx_window_horizontal_drag_shape));
1461 else
1462 horizontal_drag_cursor
1463 = XCreateFontCursor (FRAME_X_DISPLAY (f), XC_sb_h_double_arrow);
1465 /* Check and report errors with the above calls. */
1466 x_check_errors (FRAME_W32_DISPLAY (f), "can't set cursor shape: %s");
1467 x_uncatch_errors (FRAME_W32_DISPLAY (f), count);
1470 XColor fore_color, back_color;
1472 fore_color.pixel = f->output_data.w32->mouse_pixel;
1473 back_color.pixel = mask_color;
1474 XQueryColor (FRAME_W32_DISPLAY (f),
1475 DefaultColormap (FRAME_W32_DISPLAY (f),
1476 DefaultScreen (FRAME_W32_DISPLAY (f))),
1477 &fore_color);
1478 XQueryColor (FRAME_W32_DISPLAY (f),
1479 DefaultColormap (FRAME_W32_DISPLAY (f),
1480 DefaultScreen (FRAME_W32_DISPLAY (f))),
1481 &back_color);
1482 XRecolorCursor (FRAME_W32_DISPLAY (f), cursor,
1483 &fore_color, &back_color);
1484 XRecolorCursor (FRAME_W32_DISPLAY (f), nontext_cursor,
1485 &fore_color, &back_color);
1486 XRecolorCursor (FRAME_W32_DISPLAY (f), mode_cursor,
1487 &fore_color, &back_color);
1488 XRecolorCursor (FRAME_W32_DISPLAY (f), hand_cursor,
1489 &fore_color, &back_color);
1490 XRecolorCursor (FRAME_W32_DISPLAY (f), hourglass_cursor,
1491 &fore_color, &back_color);
1494 if (FRAME_W32_WINDOW (f) != 0)
1495 XDefineCursor (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), cursor);
1497 if (cursor != f->output_data.w32->text_cursor && f->output_data.w32->text_cursor != 0)
1498 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->text_cursor);
1499 f->output_data.w32->text_cursor = cursor;
1501 if (nontext_cursor != f->output_data.w32->nontext_cursor
1502 && f->output_data.w32->nontext_cursor != 0)
1503 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->nontext_cursor);
1504 f->output_data.w32->nontext_cursor = nontext_cursor;
1506 if (hourglass_cursor != f->output_data.w32->hourglass_cursor
1507 && f->output_data.w32->hourglass_cursor != 0)
1508 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->hourglass_cursor);
1509 f->output_data.w32->hourglass_cursor = hourglass_cursor;
1511 if (mode_cursor != f->output_data.w32->modeline_cursor
1512 && f->output_data.w32->modeline_cursor != 0)
1513 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->modeline_cursor);
1514 f->output_data.w32->modeline_cursor = mode_cursor;
1516 if (hand_cursor != f->output_data.w32->hand_cursor
1517 && f->output_data.w32->hand_cursor != 0)
1518 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->hand_cursor);
1519 f->output_data.w32->hand_cursor = hand_cursor;
1521 XFlush (FRAME_W32_DISPLAY (f));
1522 UNBLOCK_INPUT;
1524 update_face_from_frame_parameter (f, Qmouse_color, arg);
1525 #endif /* TODO */
1528 void
1529 x_set_cursor_color (f, arg, oldval)
1530 struct frame *f;
1531 Lisp_Object arg, oldval;
1533 unsigned long fore_pixel, pixel;
1535 if (!NILP (Vx_cursor_fore_pixel))
1536 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
1537 WHITE_PIX_DEFAULT (f));
1538 else
1539 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
1541 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1543 /* Make sure that the cursor color differs from the background color. */
1544 if (pixel == FRAME_BACKGROUND_PIXEL (f))
1546 pixel = f->output_data.w32->mouse_pixel;
1547 if (pixel == fore_pixel)
1548 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
1551 f->output_data.w32->cursor_foreground_pixel = fore_pixel;
1552 f->output_data.w32->cursor_pixel = pixel;
1554 if (FRAME_W32_WINDOW (f) != 0)
1556 BLOCK_INPUT;
1557 /* Update frame's cursor_gc. */
1558 f->output_data.w32->cursor_gc->foreground = fore_pixel;
1559 f->output_data.w32->cursor_gc->background = pixel;
1561 UNBLOCK_INPUT;
1563 if (FRAME_VISIBLE_P (f))
1565 x_update_cursor (f, 0);
1566 x_update_cursor (f, 1);
1570 update_face_from_frame_parameter (f, Qcursor_color, arg);
1573 /* Set the border-color of frame F to pixel value PIX.
1574 Note that this does not fully take effect if done before
1575 F has a window. */
1577 void
1578 x_set_border_pixel (f, pix)
1579 struct frame *f;
1580 int pix;
1583 f->output_data.w32->border_pixel = pix;
1585 if (FRAME_W32_WINDOW (f) != 0 && f->border_width > 0)
1587 if (FRAME_VISIBLE_P (f))
1588 redraw_frame (f);
1592 /* Set the border-color of frame F to value described by ARG.
1593 ARG can be a string naming a color.
1594 The border-color is used for the border that is drawn by the server.
1595 Note that this does not fully take effect if done before
1596 F has a window; it must be redone when the window is created. */
1598 void
1599 x_set_border_color (f, arg, oldval)
1600 struct frame *f;
1601 Lisp_Object arg, oldval;
1603 int pix;
1605 CHECK_STRING (arg);
1606 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1607 x_set_border_pixel (f, pix);
1608 update_face_from_frame_parameter (f, Qborder_color, arg);
1612 void
1613 x_set_cursor_type (f, arg, oldval)
1614 FRAME_PTR f;
1615 Lisp_Object arg, oldval;
1617 set_frame_cursor_types (f, arg);
1619 /* Make sure the cursor gets redrawn. */
1620 cursor_type_changed = 1;
1623 void
1624 x_set_icon_type (f, arg, oldval)
1625 struct frame *f;
1626 Lisp_Object arg, oldval;
1628 int result;
1630 if (NILP (arg) && NILP (oldval))
1631 return;
1633 if (STRINGP (arg) && STRINGP (oldval)
1634 && EQ (Fstring_equal (oldval, arg), Qt))
1635 return;
1637 if (SYMBOLP (arg) && SYMBOLP (oldval) && EQ (arg, oldval))
1638 return;
1640 BLOCK_INPUT;
1642 result = x_bitmap_icon (f, arg);
1643 if (result)
1645 UNBLOCK_INPUT;
1646 error ("No icon window available");
1649 UNBLOCK_INPUT;
1652 void
1653 x_set_icon_name (f, arg, oldval)
1654 struct frame *f;
1655 Lisp_Object arg, oldval;
1657 if (STRINGP (arg))
1659 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1660 return;
1662 else if (!NILP (arg) || NILP (oldval))
1663 return;
1665 f->icon_name = arg;
1667 #if 0
1668 if (f->output_data.w32->icon_bitmap != 0)
1669 return;
1671 BLOCK_INPUT;
1673 result = x_text_icon (f,
1674 (char *) SDATA ((!NILP (f->icon_name)
1675 ? f->icon_name
1676 : !NILP (f->title)
1677 ? f->title
1678 : f->name)));
1680 if (result)
1682 UNBLOCK_INPUT;
1683 error ("No icon window available");
1686 /* If the window was unmapped (and its icon was mapped),
1687 the new icon is not mapped, so map the window in its stead. */
1688 if (FRAME_VISIBLE_P (f))
1690 #ifdef USE_X_TOOLKIT
1691 XtPopup (f->output_data.w32->widget, XtGrabNone);
1692 #endif
1693 XMapWindow (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f));
1696 XFlush (FRAME_W32_DISPLAY (f));
1697 UNBLOCK_INPUT;
1698 #endif
1702 void
1703 x_set_menu_bar_lines (f, value, oldval)
1704 struct frame *f;
1705 Lisp_Object value, oldval;
1707 int nlines;
1708 int olines = FRAME_MENU_BAR_LINES (f);
1710 /* Right now, menu bars don't work properly in minibuf-only frames;
1711 most of the commands try to apply themselves to the minibuffer
1712 frame itself, and get an error because you can't switch buffers
1713 in or split the minibuffer window. */
1714 if (FRAME_MINIBUF_ONLY_P (f))
1715 return;
1717 if (INTEGERP (value))
1718 nlines = XINT (value);
1719 else
1720 nlines = 0;
1722 FRAME_MENU_BAR_LINES (f) = 0;
1723 if (nlines)
1724 FRAME_EXTERNAL_MENU_BAR (f) = 1;
1725 else
1727 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
1728 free_frame_menubar (f);
1729 FRAME_EXTERNAL_MENU_BAR (f) = 0;
1731 /* Adjust the frame size so that the client (text) dimensions
1732 remain the same. This depends on FRAME_EXTERNAL_MENU_BAR being
1733 set correctly. */
1734 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
1735 do_pending_window_change (0);
1737 adjust_glyphs (f);
1741 /* Set the number of lines used for the tool bar of frame F to VALUE.
1742 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
1743 is the old number of tool bar lines. This function changes the
1744 height of all windows on frame F to match the new tool bar height.
1745 The frame's height doesn't change. */
1747 void
1748 x_set_tool_bar_lines (f, value, oldval)
1749 struct frame *f;
1750 Lisp_Object value, oldval;
1752 int delta, nlines, root_height;
1753 Lisp_Object root_window;
1755 /* Treat tool bars like menu bars. */
1756 if (FRAME_MINIBUF_ONLY_P (f))
1757 return;
1759 /* Use VALUE only if an integer >= 0. */
1760 if (INTEGERP (value) && XINT (value) >= 0)
1761 nlines = XFASTINT (value);
1762 else
1763 nlines = 0;
1765 /* Make sure we redisplay all windows in this frame. */
1766 ++windows_or_buffers_changed;
1768 delta = nlines - FRAME_TOOL_BAR_LINES (f);
1770 /* Don't resize the tool-bar to more than we have room for. */
1771 root_window = FRAME_ROOT_WINDOW (f);
1772 root_height = WINDOW_TOTAL_LINES (XWINDOW (root_window));
1773 if (root_height - delta < 1)
1775 delta = root_height - 1;
1776 nlines = FRAME_TOOL_BAR_LINES (f) + delta;
1779 FRAME_TOOL_BAR_LINES (f) = nlines;
1780 change_window_heights (root_window, delta);
1781 adjust_glyphs (f);
1783 /* We also have to make sure that the internal border at the top of
1784 the frame, below the menu bar or tool bar, is redrawn when the
1785 tool bar disappears. This is so because the internal border is
1786 below the tool bar if one is displayed, but is below the menu bar
1787 if there isn't a tool bar. The tool bar draws into the area
1788 below the menu bar. */
1789 if (FRAME_W32_WINDOW (f) && FRAME_TOOL_BAR_LINES (f) == 0)
1791 clear_frame (f);
1792 clear_current_matrices (f);
1795 /* If the tool bar gets smaller, the internal border below it
1796 has to be cleared. It was formerly part of the display
1797 of the larger tool bar, and updating windows won't clear it. */
1798 if (delta < 0)
1800 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
1801 int width = FRAME_PIXEL_WIDTH (f);
1802 int y = nlines * FRAME_LINE_HEIGHT (f);
1804 BLOCK_INPUT;
1806 HDC hdc = get_frame_dc (f);
1807 w32_clear_area (f, hdc, 0, y, width, height);
1808 release_frame_dc (f, hdc);
1810 UNBLOCK_INPUT;
1812 if (WINDOWP (f->tool_bar_window))
1813 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
1818 /* Change the name of frame F to NAME. If NAME is nil, set F's name to
1819 w32_id_name.
1821 If EXPLICIT is non-zero, that indicates that lisp code is setting the
1822 name; if NAME is a string, set F's name to NAME and set
1823 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1825 If EXPLICIT is zero, that indicates that Emacs redisplay code is
1826 suggesting a new name, which lisp code should override; if
1827 F->explicit_name is set, ignore the new name; otherwise, set it. */
1829 void
1830 x_set_name (f, name, explicit)
1831 struct frame *f;
1832 Lisp_Object name;
1833 int explicit;
1835 /* Make sure that requests from lisp code override requests from
1836 Emacs redisplay code. */
1837 if (explicit)
1839 /* If we're switching from explicit to implicit, we had better
1840 update the mode lines and thereby update the title. */
1841 if (f->explicit_name && NILP (name))
1842 update_mode_lines = 1;
1844 f->explicit_name = ! NILP (name);
1846 else if (f->explicit_name)
1847 return;
1849 /* If NAME is nil, set the name to the w32_id_name. */
1850 if (NILP (name))
1852 /* Check for no change needed in this very common case
1853 before we do any consing. */
1854 if (!strcmp (FRAME_W32_DISPLAY_INFO (f)->w32_id_name,
1855 SDATA (f->name)))
1856 return;
1857 name = build_string (FRAME_W32_DISPLAY_INFO (f)->w32_id_name);
1859 else
1860 CHECK_STRING (name);
1862 /* Don't change the name if it's already NAME. */
1863 if (! NILP (Fstring_equal (name, f->name)))
1864 return;
1866 f->name = name;
1868 /* For setting the frame title, the title parameter should override
1869 the name parameter. */
1870 if (! NILP (f->title))
1871 name = f->title;
1873 if (FRAME_W32_WINDOW (f))
1875 if (STRING_MULTIBYTE (name))
1876 name = ENCODE_SYSTEM (name);
1878 BLOCK_INPUT;
1879 SetWindowText (FRAME_W32_WINDOW (f), SDATA (name));
1880 UNBLOCK_INPUT;
1884 /* This function should be called when the user's lisp code has
1885 specified a name for the frame; the name will override any set by the
1886 redisplay code. */
1887 void
1888 x_explicitly_set_name (f, arg, oldval)
1889 FRAME_PTR f;
1890 Lisp_Object arg, oldval;
1892 x_set_name (f, arg, 1);
1895 /* This function should be called by Emacs redisplay code to set the
1896 name; names set this way will never override names set by the user's
1897 lisp code. */
1898 void
1899 x_implicitly_set_name (f, arg, oldval)
1900 FRAME_PTR f;
1901 Lisp_Object arg, oldval;
1903 x_set_name (f, arg, 0);
1906 /* Change the title of frame F to NAME.
1907 If NAME is nil, use the frame name as the title. */
1909 void
1910 x_set_title (f, name, old_name)
1911 struct frame *f;
1912 Lisp_Object name, old_name;
1914 /* Don't change the title if it's already NAME. */
1915 if (EQ (name, f->title))
1916 return;
1918 update_mode_lines = 1;
1920 f->title = name;
1922 if (NILP (name))
1923 name = f->name;
1925 if (FRAME_W32_WINDOW (f))
1927 if (STRING_MULTIBYTE (name))
1928 name = ENCODE_SYSTEM (name);
1930 BLOCK_INPUT;
1931 SetWindowText (FRAME_W32_WINDOW (f), SDATA (name));
1932 UNBLOCK_INPUT;
1937 void x_set_scroll_bar_default_width (f)
1938 struct frame *f;
1940 int wid = FRAME_COLUMN_WIDTH (f);
1942 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = GetSystemMetrics (SM_CXVSCROLL);
1943 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) +
1944 wid - 1) / wid;
1948 /* Subroutines for creating a frame. */
1950 Cursor
1951 w32_load_cursor (LPCTSTR name)
1953 /* Try first to load cursor from application resource. */
1954 Cursor cursor = LoadImage ((HINSTANCE) GetModuleHandle (NULL),
1955 name, IMAGE_CURSOR, 0, 0,
1956 LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_SHARED);
1957 if (!cursor)
1959 /* Then try to load a shared predefined cursor. */
1960 cursor = LoadImage (NULL, name, IMAGE_CURSOR, 0, 0,
1961 LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_SHARED);
1963 return cursor;
1966 extern LRESULT CALLBACK w32_wnd_proc ();
1968 static BOOL
1969 w32_init_class (hinst)
1970 HINSTANCE hinst;
1972 WNDCLASS wc;
1974 wc.style = CS_HREDRAW | CS_VREDRAW;
1975 wc.lpfnWndProc = (WNDPROC) w32_wnd_proc;
1976 wc.cbClsExtra = 0;
1977 wc.cbWndExtra = WND_EXTRA_BYTES;
1978 wc.hInstance = hinst;
1979 wc.hIcon = LoadIcon (hinst, EMACS_CLASS);
1980 wc.hCursor = w32_load_cursor (IDC_ARROW);
1981 wc.hbrBackground = NULL; /* GetStockObject (WHITE_BRUSH); */
1982 wc.lpszMenuName = NULL;
1983 wc.lpszClassName = EMACS_CLASS;
1985 return (RegisterClass (&wc));
1988 static HWND
1989 w32_createscrollbar (f, bar)
1990 struct frame *f;
1991 struct scroll_bar * bar;
1993 return (CreateWindow ("SCROLLBAR", "", SBS_VERT | WS_CHILD | WS_VISIBLE,
1994 /* Position and size of scroll bar. */
1995 XINT (bar->left) + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
1996 XINT (bar->top),
1997 XINT (bar->width) - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
1998 XINT (bar->height),
1999 FRAME_W32_WINDOW (f),
2000 NULL,
2001 hinst,
2002 NULL));
2005 static void
2006 w32_createwindow (f)
2007 struct frame *f;
2009 HWND hwnd;
2010 RECT rect;
2011 Lisp_Object top = Qunbound;
2012 Lisp_Object left = Qunbound;
2013 struct w32_display_info *dpyinfo = &one_w32_display_info;
2015 rect.left = rect.top = 0;
2016 rect.right = FRAME_PIXEL_WIDTH (f);
2017 rect.bottom = FRAME_PIXEL_HEIGHT (f);
2019 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
2020 FRAME_EXTERNAL_MENU_BAR (f));
2022 /* Do first time app init */
2024 if (!hprevinst)
2026 w32_init_class (hinst);
2029 if (f->size_hint_flags & USPosition || f->size_hint_flags & PPosition)
2031 XSETINT (left, f->left_pos);
2032 XSETINT (top, f->top_pos);
2034 else if (EQ (left, Qunbound) && EQ (top, Qunbound))
2036 /* When called with RES_TYPE_NUMBER, w32_get_arg will return zero
2037 for anything that is not a number and is not Qunbound. */
2038 left = x_get_arg (dpyinfo, Qnil, Qleft, "left", "Left", RES_TYPE_NUMBER);
2039 top = x_get_arg (dpyinfo, Qnil, Qtop, "top", "Top", RES_TYPE_NUMBER);
2042 FRAME_W32_WINDOW (f) = hwnd
2043 = CreateWindow (EMACS_CLASS,
2044 f->namebuf,
2045 f->output_data.w32->dwStyle | WS_CLIPCHILDREN,
2046 EQ (left, Qunbound) ? CW_USEDEFAULT : XINT (left),
2047 EQ (top, Qunbound) ? CW_USEDEFAULT : XINT (top),
2048 rect.right - rect.left,
2049 rect.bottom - rect.top,
2050 NULL,
2051 NULL,
2052 hinst,
2053 NULL);
2055 if (hwnd)
2057 SetWindowLong (hwnd, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
2058 SetWindowLong (hwnd, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
2059 SetWindowLong (hwnd, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
2060 SetWindowLong (hwnd, WND_SCROLLBAR_INDEX, f->scroll_bar_actual_width);
2061 SetWindowLong (hwnd, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
2063 /* Enable drag-n-drop. */
2064 DragAcceptFiles (hwnd, TRUE);
2066 /* Do this to discard the default setting specified by our parent. */
2067 ShowWindow (hwnd, SW_HIDE);
2069 /* Update frame positions. */
2070 GetWindowRect (hwnd, &rect);
2071 f->left_pos = rect.left;
2072 f->top_pos = rect.top;
2076 static void
2077 my_post_msg (wmsg, hwnd, msg, wParam, lParam)
2078 W32Msg * wmsg;
2079 HWND hwnd;
2080 UINT msg;
2081 WPARAM wParam;
2082 LPARAM lParam;
2084 wmsg->msg.hwnd = hwnd;
2085 wmsg->msg.message = msg;
2086 wmsg->msg.wParam = wParam;
2087 wmsg->msg.lParam = lParam;
2088 wmsg->msg.time = GetMessageTime ();
2090 post_msg (wmsg);
2093 /* GetKeyState and MapVirtualKey on Windows 95 do not actually distinguish
2094 between left and right keys as advertised. We test for this
2095 support dynamically, and set a flag when the support is absent. If
2096 absent, we keep track of the left and right control and alt keys
2097 ourselves. This is particularly necessary on keyboards that rely
2098 upon the AltGr key, which is represented as having the left control
2099 and right alt keys pressed. For these keyboards, we need to know
2100 when the left alt key has been pressed in addition to the AltGr key
2101 so that we can properly support M-AltGr-key sequences (such as M-@
2102 on Swedish keyboards). */
2104 #define EMACS_LCONTROL 0
2105 #define EMACS_RCONTROL 1
2106 #define EMACS_LMENU 2
2107 #define EMACS_RMENU 3
2109 static int modifiers[4];
2110 static int modifiers_recorded;
2111 static int modifier_key_support_tested;
2113 static void
2114 test_modifier_support (unsigned int wparam)
2116 unsigned int l, r;
2118 if (wparam != VK_CONTROL && wparam != VK_MENU)
2119 return;
2120 if (wparam == VK_CONTROL)
2122 l = VK_LCONTROL;
2123 r = VK_RCONTROL;
2125 else
2127 l = VK_LMENU;
2128 r = VK_RMENU;
2130 if (!(GetKeyState (l) & 0x8000) && !(GetKeyState (r) & 0x8000))
2131 modifiers_recorded = 1;
2132 else
2133 modifiers_recorded = 0;
2134 modifier_key_support_tested = 1;
2137 static void
2138 record_keydown (unsigned int wparam, unsigned int lparam)
2140 int i;
2142 if (!modifier_key_support_tested)
2143 test_modifier_support (wparam);
2145 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
2146 return;
2148 if (wparam == VK_CONTROL)
2149 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
2150 else
2151 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
2153 modifiers[i] = 1;
2156 static void
2157 record_keyup (unsigned int wparam, unsigned int lparam)
2159 int i;
2161 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
2162 return;
2164 if (wparam == VK_CONTROL)
2165 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
2166 else
2167 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
2169 modifiers[i] = 0;
2172 /* Emacs can lose focus while a modifier key has been pressed. When
2173 it regains focus, be conservative and clear all modifiers since
2174 we cannot reconstruct the left and right modifier state. */
2175 static void
2176 reset_modifiers ()
2178 SHORT ctrl, alt;
2180 if (GetFocus () == NULL)
2181 /* Emacs doesn't have keyboard focus. Do nothing. */
2182 return;
2184 ctrl = GetAsyncKeyState (VK_CONTROL);
2185 alt = GetAsyncKeyState (VK_MENU);
2187 if (!(ctrl & 0x08000))
2188 /* Clear any recorded control modifier state. */
2189 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
2191 if (!(alt & 0x08000))
2192 /* Clear any recorded alt modifier state. */
2193 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
2195 /* Update the state of all modifier keys, because modifiers used in
2196 hot-key combinations can get stuck on if Emacs loses focus as a
2197 result of a hot-key being pressed. */
2199 BYTE keystate[256];
2201 #define CURRENT_STATE(key) ((GetAsyncKeyState (key) & 0x8000) >> 8)
2203 GetKeyboardState (keystate);
2204 keystate[VK_SHIFT] = CURRENT_STATE (VK_SHIFT);
2205 keystate[VK_CONTROL] = CURRENT_STATE (VK_CONTROL);
2206 keystate[VK_LCONTROL] = CURRENT_STATE (VK_LCONTROL);
2207 keystate[VK_RCONTROL] = CURRENT_STATE (VK_RCONTROL);
2208 keystate[VK_MENU] = CURRENT_STATE (VK_MENU);
2209 keystate[VK_LMENU] = CURRENT_STATE (VK_LMENU);
2210 keystate[VK_RMENU] = CURRENT_STATE (VK_RMENU);
2211 keystate[VK_LWIN] = CURRENT_STATE (VK_LWIN);
2212 keystate[VK_RWIN] = CURRENT_STATE (VK_RWIN);
2213 keystate[VK_APPS] = CURRENT_STATE (VK_APPS);
2214 SetKeyboardState (keystate);
2218 /* Synchronize modifier state with what is reported with the current
2219 keystroke. Even if we cannot distinguish between left and right
2220 modifier keys, we know that, if no modifiers are set, then neither
2221 the left or right modifier should be set. */
2222 static void
2223 sync_modifiers ()
2225 if (!modifiers_recorded)
2226 return;
2228 if (!(GetKeyState (VK_CONTROL) & 0x8000))
2229 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
2231 if (!(GetKeyState (VK_MENU) & 0x8000))
2232 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
2235 static int
2236 modifier_set (int vkey)
2238 if (vkey == VK_CAPITAL || vkey == VK_SCROLL)
2239 return (GetKeyState (vkey) & 0x1);
2240 if (!modifiers_recorded)
2241 return (GetKeyState (vkey) & 0x8000);
2243 switch (vkey)
2245 case VK_LCONTROL:
2246 return modifiers[EMACS_LCONTROL];
2247 case VK_RCONTROL:
2248 return modifiers[EMACS_RCONTROL];
2249 case VK_LMENU:
2250 return modifiers[EMACS_LMENU];
2251 case VK_RMENU:
2252 return modifiers[EMACS_RMENU];
2254 return (GetKeyState (vkey) & 0x8000);
2257 /* Convert between the modifier bits W32 uses and the modifier bits
2258 Emacs uses. */
2260 unsigned int
2261 w32_key_to_modifier (int key)
2263 Lisp_Object key_mapping;
2265 switch (key)
2267 case VK_LWIN:
2268 key_mapping = Vw32_lwindow_modifier;
2269 break;
2270 case VK_RWIN:
2271 key_mapping = Vw32_rwindow_modifier;
2272 break;
2273 case VK_APPS:
2274 key_mapping = Vw32_apps_modifier;
2275 break;
2276 case VK_SCROLL:
2277 key_mapping = Vw32_scroll_lock_modifier;
2278 break;
2279 default:
2280 key_mapping = Qnil;
2283 /* NB. This code runs in the input thread, asychronously to the lisp
2284 thread, so we must be careful to ensure access to lisp data is
2285 thread-safe. The following code is safe because the modifier
2286 variable values are updated atomically from lisp and symbols are
2287 not relocated by GC. Also, we don't have to worry about seeing GC
2288 markbits here. */
2289 if (EQ (key_mapping, Qhyper))
2290 return hyper_modifier;
2291 if (EQ (key_mapping, Qsuper))
2292 return super_modifier;
2293 if (EQ (key_mapping, Qmeta))
2294 return meta_modifier;
2295 if (EQ (key_mapping, Qalt))
2296 return alt_modifier;
2297 if (EQ (key_mapping, Qctrl))
2298 return ctrl_modifier;
2299 if (EQ (key_mapping, Qcontrol)) /* synonym for ctrl */
2300 return ctrl_modifier;
2301 if (EQ (key_mapping, Qshift))
2302 return shift_modifier;
2304 /* Don't generate any modifier if not explicitly requested. */
2305 return 0;
2308 static unsigned int
2309 w32_get_modifiers ()
2311 return ((modifier_set (VK_SHIFT) ? shift_modifier : 0) |
2312 (modifier_set (VK_CONTROL) ? ctrl_modifier : 0) |
2313 (modifier_set (VK_LWIN) ? w32_key_to_modifier (VK_LWIN) : 0) |
2314 (modifier_set (VK_RWIN) ? w32_key_to_modifier (VK_RWIN) : 0) |
2315 (modifier_set (VK_APPS) ? w32_key_to_modifier (VK_APPS) : 0) |
2316 (modifier_set (VK_SCROLL) ? w32_key_to_modifier (VK_SCROLL) : 0) |
2317 (modifier_set (VK_MENU) ?
2318 ((NILP (Vw32_alt_is_meta)) ? alt_modifier : meta_modifier) : 0));
2321 /* We map the VK_* modifiers into console modifier constants
2322 so that we can use the same routines to handle both console
2323 and window input. */
2325 static int
2326 construct_console_modifiers ()
2328 int mods;
2330 mods = 0;
2331 mods |= (modifier_set (VK_SHIFT)) ? SHIFT_PRESSED : 0;
2332 mods |= (modifier_set (VK_CAPITAL)) ? CAPSLOCK_ON : 0;
2333 mods |= (modifier_set (VK_SCROLL)) ? SCROLLLOCK_ON : 0;
2334 mods |= (modifier_set (VK_NUMLOCK)) ? NUMLOCK_ON : 0;
2335 mods |= (modifier_set (VK_LCONTROL)) ? LEFT_CTRL_PRESSED : 0;
2336 mods |= (modifier_set (VK_RCONTROL)) ? RIGHT_CTRL_PRESSED : 0;
2337 mods |= (modifier_set (VK_LMENU)) ? LEFT_ALT_PRESSED : 0;
2338 mods |= (modifier_set (VK_RMENU)) ? RIGHT_ALT_PRESSED : 0;
2339 mods |= (modifier_set (VK_LWIN)) ? LEFT_WIN_PRESSED : 0;
2340 mods |= (modifier_set (VK_RWIN)) ? RIGHT_WIN_PRESSED : 0;
2341 mods |= (modifier_set (VK_APPS)) ? APPS_PRESSED : 0;
2343 return mods;
2346 static int
2347 w32_get_key_modifiers (unsigned int wparam, unsigned int lparam)
2349 int mods;
2351 /* Convert to emacs modifiers. */
2352 mods = w32_kbd_mods_to_emacs (construct_console_modifiers (), wparam);
2354 return mods;
2357 unsigned int
2358 map_keypad_keys (unsigned int virt_key, unsigned int extended)
2360 if (virt_key < VK_CLEAR || virt_key > VK_DELETE)
2361 return virt_key;
2363 if (virt_key == VK_RETURN)
2364 return (extended ? VK_NUMPAD_ENTER : VK_RETURN);
2366 if (virt_key >= VK_PRIOR && virt_key <= VK_DOWN)
2367 return (!extended ? (VK_NUMPAD_PRIOR + (virt_key - VK_PRIOR)) : virt_key);
2369 if (virt_key == VK_INSERT || virt_key == VK_DELETE)
2370 return (!extended ? (VK_NUMPAD_INSERT + (virt_key - VK_INSERT)) : virt_key);
2372 if (virt_key == VK_CLEAR)
2373 return (!extended ? VK_NUMPAD_CLEAR : virt_key);
2375 return virt_key;
2378 /* List of special key combinations which w32 would normally capture,
2379 but Emacs should grab instead. Not directly visible to lisp, to
2380 simplify synchronization. Each item is an integer encoding a virtual
2381 key code and modifier combination to capture. */
2382 static Lisp_Object w32_grabbed_keys;
2384 #define HOTKEY(vk, mods) make_number (((vk) & 255) | ((mods) << 8))
2385 #define HOTKEY_ID(k) (XFASTINT (k) & 0xbfff)
2386 #define HOTKEY_VK_CODE(k) (XFASTINT (k) & 255)
2387 #define HOTKEY_MODIFIERS(k) (XFASTINT (k) >> 8)
2389 #define RAW_HOTKEY_ID(k) ((k) & 0xbfff)
2390 #define RAW_HOTKEY_VK_CODE(k) ((k) & 255)
2391 #define RAW_HOTKEY_MODIFIERS(k) ((k) >> 8)
2393 /* Register hot-keys for reserved key combinations when Emacs has
2394 keyboard focus, since this is the only way Emacs can receive key
2395 combinations like Alt-Tab which are used by the system. */
2397 static void
2398 register_hot_keys (hwnd)
2399 HWND hwnd;
2401 Lisp_Object keylist;
2403 /* Use CONSP, since we are called asynchronously. */
2404 for (keylist = w32_grabbed_keys; CONSP (keylist); keylist = XCDR (keylist))
2406 Lisp_Object key = XCAR (keylist);
2408 /* Deleted entries get set to nil. */
2409 if (!INTEGERP (key))
2410 continue;
2412 RegisterHotKey (hwnd, HOTKEY_ID (key),
2413 HOTKEY_MODIFIERS (key), HOTKEY_VK_CODE (key));
2417 static void
2418 unregister_hot_keys (hwnd)
2419 HWND hwnd;
2421 Lisp_Object keylist;
2423 for (keylist = w32_grabbed_keys; CONSP (keylist); keylist = XCDR (keylist))
2425 Lisp_Object key = XCAR (keylist);
2427 if (!INTEGERP (key))
2428 continue;
2430 UnregisterHotKey (hwnd, HOTKEY_ID (key));
2434 /* Main message dispatch loop. */
2436 static void
2437 w32_msg_pump (deferred_msg * msg_buf)
2439 MSG msg;
2440 int result;
2441 HWND focus_window;
2443 msh_mousewheel = RegisterWindowMessage (MSH_MOUSEWHEEL);
2445 while (GetMessage (&msg, NULL, 0, 0))
2447 if (msg.hwnd == NULL)
2449 switch (msg.message)
2451 case WM_NULL:
2452 /* Produced by complete_deferred_msg; just ignore. */
2453 break;
2454 case WM_EMACS_CREATEWINDOW:
2455 /* Initialize COM for this window. Even though we don't use it,
2456 some third party shell extensions can cause it to be used in
2457 system dialogs, which causes a crash if it is not initialized.
2458 This is a known bug in Windows, which was fixed long ago, but
2459 the patch for XP is not publically available until XP SP3,
2460 and older versions will never be patched. */
2461 CoInitialize (NULL);
2462 w32_createwindow ((struct frame *) msg.wParam);
2463 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2464 abort ();
2465 break;
2466 case WM_EMACS_SETLOCALE:
2467 SetThreadLocale (msg.wParam);
2468 /* Reply is not expected. */
2469 break;
2470 case WM_EMACS_SETKEYBOARDLAYOUT:
2471 result = (int) ActivateKeyboardLayout ((HKL) msg.wParam, 0);
2472 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
2473 result, 0))
2474 abort ();
2475 break;
2476 case WM_EMACS_REGISTER_HOT_KEY:
2477 focus_window = GetFocus ();
2478 if (focus_window != NULL)
2479 RegisterHotKey (focus_window,
2480 RAW_HOTKEY_ID (msg.wParam),
2481 RAW_HOTKEY_MODIFIERS (msg.wParam),
2482 RAW_HOTKEY_VK_CODE (msg.wParam));
2483 /* Reply is not expected. */
2484 break;
2485 case WM_EMACS_UNREGISTER_HOT_KEY:
2486 focus_window = GetFocus ();
2487 if (focus_window != NULL)
2488 UnregisterHotKey (focus_window, RAW_HOTKEY_ID (msg.wParam));
2489 /* Mark item as erased. NB: this code must be
2490 thread-safe. The next line is okay because the cons
2491 cell is never made into garbage and is not relocated by
2492 GC. */
2493 XSETCAR ((Lisp_Object) ((EMACS_INT) msg.lParam), Qnil);
2494 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2495 abort ();
2496 break;
2497 case WM_EMACS_TOGGLE_LOCK_KEY:
2499 int vk_code = (int) msg.wParam;
2500 int cur_state = (GetKeyState (vk_code) & 1);
2501 Lisp_Object new_state = (Lisp_Object) ((EMACS_INT) msg.lParam);
2503 /* NB: This code must be thread-safe. It is safe to
2504 call NILP because symbols are not relocated by GC,
2505 and pointer here is not touched by GC (so the markbit
2506 can't be set). Numbers are safe because they are
2507 immediate values. */
2508 if (NILP (new_state)
2509 || (NUMBERP (new_state)
2510 && ((XUINT (new_state)) & 1) != cur_state))
2512 one_w32_display_info.faked_key = vk_code;
2514 keybd_event ((BYTE) vk_code,
2515 (BYTE) MapVirtualKey (vk_code, 0),
2516 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2517 keybd_event ((BYTE) vk_code,
2518 (BYTE) MapVirtualKey (vk_code, 0),
2519 KEYEVENTF_EXTENDEDKEY | 0, 0);
2520 keybd_event ((BYTE) vk_code,
2521 (BYTE) MapVirtualKey (vk_code, 0),
2522 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2523 cur_state = !cur_state;
2525 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
2526 cur_state, 0))
2527 abort ();
2529 break;
2530 #ifdef MSG_DEBUG
2531 /* Broadcast messages make it here, so you need to be looking
2532 for something in particular for this to be useful. */
2533 default:
2534 DebPrint (("msg %x not expected by w32_msg_pump\n", msg.message));
2535 #endif
2538 else
2540 DispatchMessage (&msg);
2543 /* Exit nested loop when our deferred message has completed. */
2544 if (msg_buf->completed)
2545 break;
2549 deferred_msg * deferred_msg_head;
2551 static deferred_msg *
2552 find_deferred_msg (HWND hwnd, UINT msg)
2554 deferred_msg * item;
2556 /* Don't actually need synchronization for read access, since
2557 modification of single pointer is always atomic. */
2558 /* enter_crit (); */
2560 for (item = deferred_msg_head; item != NULL; item = item->next)
2561 if (item->w32msg.msg.hwnd == hwnd
2562 && item->w32msg.msg.message == msg)
2563 break;
2565 /* leave_crit (); */
2567 return item;
2570 static LRESULT
2571 send_deferred_msg (deferred_msg * msg_buf,
2572 HWND hwnd,
2573 UINT msg,
2574 WPARAM wParam,
2575 LPARAM lParam)
2577 /* Only input thread can send deferred messages. */
2578 if (GetCurrentThreadId () != dwWindowsThreadId)
2579 abort ();
2581 /* It is an error to send a message that is already deferred. */
2582 if (find_deferred_msg (hwnd, msg) != NULL)
2583 abort ();
2585 /* Enforced synchronization is not needed because this is the only
2586 function that alters deferred_msg_head, and the following critical
2587 section is guaranteed to only be serially reentered (since only the
2588 input thread can call us). */
2590 /* enter_crit (); */
2592 msg_buf->completed = 0;
2593 msg_buf->next = deferred_msg_head;
2594 deferred_msg_head = msg_buf;
2595 my_post_msg (&msg_buf->w32msg, hwnd, msg, wParam, lParam);
2597 /* leave_crit (); */
2599 /* Start a new nested message loop to process other messages until
2600 this one is completed. */
2601 w32_msg_pump (msg_buf);
2603 deferred_msg_head = msg_buf->next;
2605 return msg_buf->result;
2608 void
2609 complete_deferred_msg (HWND hwnd, UINT msg, LRESULT result)
2611 deferred_msg * msg_buf = find_deferred_msg (hwnd, msg);
2613 if (msg_buf == NULL)
2614 /* Message may have been cancelled, so don't abort. */
2615 return;
2617 msg_buf->result = result;
2618 msg_buf->completed = 1;
2620 /* Ensure input thread is woken so it notices the completion. */
2621 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
2624 static void
2625 cancel_all_deferred_msgs ()
2627 deferred_msg * item;
2629 /* Don't actually need synchronization for read access, since
2630 modification of single pointer is always atomic. */
2631 /* enter_crit (); */
2633 for (item = deferred_msg_head; item != NULL; item = item->next)
2635 item->result = 0;
2636 item->completed = 1;
2639 /* leave_crit (); */
2641 /* Ensure input thread is woken so it notices the completion. */
2642 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
2645 DWORD WINAPI
2646 w32_msg_worker (void *arg)
2648 MSG msg;
2649 deferred_msg dummy_buf;
2651 /* Ensure our message queue is created */
2653 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
2655 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2656 abort ();
2658 memset (&dummy_buf, 0, sizeof (dummy_buf));
2659 dummy_buf.w32msg.msg.hwnd = NULL;
2660 dummy_buf.w32msg.msg.message = WM_NULL;
2662 /* This is the initial message loop which should only exit when the
2663 application quits. */
2664 w32_msg_pump (&dummy_buf);
2666 return 0;
2669 static void
2670 signal_user_input ()
2672 /* Interrupt any lisp that wants to be interrupted by input. */
2673 if (!NILP (Vthrow_on_input))
2675 Vquit_flag = Vthrow_on_input;
2676 /* If we're inside a function that wants immediate quits,
2677 do it now. */
2678 if (immediate_quit && NILP (Vinhibit_quit))
2680 immediate_quit = 0;
2681 QUIT;
2687 static void
2688 post_character_message (hwnd, msg, wParam, lParam, modifiers)
2689 HWND hwnd;
2690 UINT msg;
2691 WPARAM wParam;
2692 LPARAM lParam;
2693 DWORD modifiers;
2696 W32Msg wmsg;
2698 wmsg.dwModifiers = modifiers;
2700 /* Detect quit_char and set quit-flag directly. Note that we
2701 still need to post a message to ensure the main thread will be
2702 woken up if blocked in sys_select, but we do NOT want to post
2703 the quit_char message itself (because it will usually be as if
2704 the user had typed quit_char twice). Instead, we post a dummy
2705 message that has no particular effect. */
2707 int c = wParam;
2708 if (isalpha (c) && wmsg.dwModifiers == ctrl_modifier)
2709 c = make_ctrl_char (c) & 0377;
2710 if (c == quit_char
2711 || (wmsg.dwModifiers == 0 &&
2712 w32_quit_key && wParam == w32_quit_key))
2714 Vquit_flag = Qt;
2716 /* The choice of message is somewhat arbitrary, as long as
2717 the main thread handler just ignores it. */
2718 msg = WM_NULL;
2720 /* Interrupt any blocking system calls. */
2721 signal_quit ();
2723 /* As a safety precaution, forcibly complete any deferred
2724 messages. This is a kludge, but I don't see any particularly
2725 clean way to handle the situation where a deferred message is
2726 "dropped" in the lisp thread, and will thus never be
2727 completed, eg. by the user trying to activate the menubar
2728 when the lisp thread is busy, and then typing C-g when the
2729 menubar doesn't open promptly (with the result that the
2730 menubar never responds at all because the deferred
2731 WM_INITMENU message is never completed). Another problem
2732 situation is when the lisp thread calls SendMessage (to send
2733 a window manager command) when a message has been deferred;
2734 the lisp thread gets blocked indefinitely waiting for the
2735 deferred message to be completed, which itself is waiting for
2736 the lisp thread to respond.
2738 Note that we don't want to block the input thread waiting for
2739 a reponse from the lisp thread (although that would at least
2740 solve the deadlock problem above), because we want to be able
2741 to receive C-g to interrupt the lisp thread. */
2742 cancel_all_deferred_msgs ();
2744 else
2745 signal_user_input ();
2748 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2751 /* Main window procedure */
2753 LRESULT CALLBACK
2754 w32_wnd_proc (hwnd, msg, wParam, lParam)
2755 HWND hwnd;
2756 UINT msg;
2757 WPARAM wParam;
2758 LPARAM lParam;
2760 struct frame *f;
2761 struct w32_display_info *dpyinfo = &one_w32_display_info;
2762 W32Msg wmsg;
2763 int windows_translate;
2764 int key;
2766 /* Note that it is okay to call x_window_to_frame, even though we are
2767 not running in the main lisp thread, because frame deletion
2768 requires the lisp thread to synchronize with this thread. Thus, if
2769 a frame struct is returned, it can be used without concern that the
2770 lisp thread might make it disappear while we are using it.
2772 NB. Walking the frame list in this thread is safe (as long as
2773 writes of Lisp_Object slots are atomic, which they are on Windows).
2774 Although delete-frame can destructively modify the frame list while
2775 we are walking it, a garbage collection cannot occur until after
2776 delete-frame has synchronized with this thread.
2778 It is also safe to use functions that make GDI calls, such as
2779 w32_clear_rect, because these functions must obtain a DC handle
2780 from the frame struct using get_frame_dc which is thread-aware. */
2782 switch (msg)
2784 case WM_ERASEBKGND:
2785 f = x_window_to_frame (dpyinfo, hwnd);
2786 if (f)
2788 HDC hdc = get_frame_dc (f);
2789 GetUpdateRect (hwnd, &wmsg.rect, FALSE);
2790 w32_clear_rect (f, hdc, &wmsg.rect);
2791 release_frame_dc (f, hdc);
2793 #if defined (W32_DEBUG_DISPLAY)
2794 DebPrint (("WM_ERASEBKGND (frame %p): erasing %d,%d-%d,%d\n",
2796 wmsg.rect.left, wmsg.rect.top,
2797 wmsg.rect.right, wmsg.rect.bottom));
2798 #endif /* W32_DEBUG_DISPLAY */
2800 return 1;
2801 case WM_PALETTECHANGED:
2802 /* ignore our own changes */
2803 if ((HWND)wParam != hwnd)
2805 f = x_window_to_frame (dpyinfo, hwnd);
2806 if (f)
2807 /* get_frame_dc will realize our palette and force all
2808 frames to be redrawn if needed. */
2809 release_frame_dc (f, get_frame_dc (f));
2811 return 0;
2812 case WM_PAINT:
2814 PAINTSTRUCT paintStruct;
2815 RECT update_rect;
2816 bzero (&update_rect, sizeof (update_rect));
2818 f = x_window_to_frame (dpyinfo, hwnd);
2819 if (f == 0)
2821 DebPrint (("WM_PAINT received for unknown window %p\n", hwnd));
2822 return 0;
2825 /* MSDN Docs say not to call BeginPaint if GetUpdateRect
2826 fails. Apparently this can happen under some
2827 circumstances. */
2828 if (GetUpdateRect (hwnd, &update_rect, FALSE) || !w32_strict_painting)
2830 enter_crit ();
2831 BeginPaint (hwnd, &paintStruct);
2833 /* The rectangles returned by GetUpdateRect and BeginPaint
2834 do not always match. Play it safe by assuming both areas
2835 are invalid. */
2836 UnionRect (&(wmsg.rect), &update_rect, &(paintStruct.rcPaint));
2838 #if defined (W32_DEBUG_DISPLAY)
2839 DebPrint (("WM_PAINT (frame %p): painting %d,%d-%d,%d\n",
2841 wmsg.rect.left, wmsg.rect.top,
2842 wmsg.rect.right, wmsg.rect.bottom));
2843 DebPrint ((" [update region is %d,%d-%d,%d]\n",
2844 update_rect.left, update_rect.top,
2845 update_rect.right, update_rect.bottom));
2846 #endif
2847 EndPaint (hwnd, &paintStruct);
2848 leave_crit ();
2850 /* Change the message type to prevent Windows from
2851 combining WM_PAINT messages in the Lisp thread's queue,
2852 since Windows assumes that each message queue is
2853 dedicated to one frame and does not bother checking
2854 that hwnd matches before combining them. */
2855 my_post_msg (&wmsg, hwnd, WM_EMACS_PAINT, wParam, lParam);
2857 return 0;
2860 /* If GetUpdateRect returns 0 (meaning there is no update
2861 region), assume the whole window needs to be repainted. */
2862 GetClientRect (hwnd, &wmsg.rect);
2863 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2864 return 0;
2867 case WM_INPUTLANGCHANGE:
2868 /* Inform lisp thread of keyboard layout changes. */
2869 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2871 /* Clear dead keys in the keyboard state; for simplicity only
2872 preserve modifier key states. */
2874 int i;
2875 BYTE keystate[256];
2877 GetKeyboardState (keystate);
2878 for (i = 0; i < 256; i++)
2879 if (1
2880 && i != VK_SHIFT
2881 && i != VK_LSHIFT
2882 && i != VK_RSHIFT
2883 && i != VK_CAPITAL
2884 && i != VK_NUMLOCK
2885 && i != VK_SCROLL
2886 && i != VK_CONTROL
2887 && i != VK_LCONTROL
2888 && i != VK_RCONTROL
2889 && i != VK_MENU
2890 && i != VK_LMENU
2891 && i != VK_RMENU
2892 && i != VK_LWIN
2893 && i != VK_RWIN)
2894 keystate[i] = 0;
2895 SetKeyboardState (keystate);
2897 goto dflt;
2899 case WM_HOTKEY:
2900 /* Synchronize hot keys with normal input. */
2901 PostMessage (hwnd, WM_KEYDOWN, HIWORD (lParam), 0);
2902 return (0);
2904 case WM_KEYUP:
2905 case WM_SYSKEYUP:
2906 record_keyup (wParam, lParam);
2907 goto dflt;
2909 case WM_KEYDOWN:
2910 case WM_SYSKEYDOWN:
2911 /* Ignore keystrokes we fake ourself; see below. */
2912 if (dpyinfo->faked_key == wParam)
2914 dpyinfo->faked_key = 0;
2915 /* Make sure TranslateMessage sees them though (as long as
2916 they don't produce WM_CHAR messages). This ensures that
2917 indicator lights are toggled promptly on Windows 9x, for
2918 example. */
2919 if (wParam < 256 && lispy_function_keys[wParam])
2921 windows_translate = 1;
2922 goto translate;
2924 return 0;
2927 /* Synchronize modifiers with current keystroke. */
2928 sync_modifiers ();
2929 record_keydown (wParam, lParam);
2930 wParam = map_keypad_keys (wParam, (lParam & 0x1000000L) != 0);
2932 windows_translate = 0;
2934 switch (wParam)
2936 case VK_LWIN:
2937 if (NILP (Vw32_pass_lwindow_to_system))
2939 /* Prevent system from acting on keyup (which opens the
2940 Start menu if no other key was pressed) by simulating a
2941 press of Space which we will ignore. */
2942 if (GetAsyncKeyState (wParam) & 1)
2944 if (NUMBERP (Vw32_phantom_key_code))
2945 key = XUINT (Vw32_phantom_key_code) & 255;
2946 else
2947 key = VK_SPACE;
2948 dpyinfo->faked_key = key;
2949 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
2952 if (!NILP (Vw32_lwindow_modifier))
2953 return 0;
2954 break;
2955 case VK_RWIN:
2956 if (NILP (Vw32_pass_rwindow_to_system))
2958 if (GetAsyncKeyState (wParam) & 1)
2960 if (NUMBERP (Vw32_phantom_key_code))
2961 key = XUINT (Vw32_phantom_key_code) & 255;
2962 else
2963 key = VK_SPACE;
2964 dpyinfo->faked_key = key;
2965 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
2968 if (!NILP (Vw32_rwindow_modifier))
2969 return 0;
2970 break;
2971 case VK_APPS:
2972 if (!NILP (Vw32_apps_modifier))
2973 return 0;
2974 break;
2975 case VK_MENU:
2976 if (NILP (Vw32_pass_alt_to_system))
2977 /* Prevent DefWindowProc from activating the menu bar if an
2978 Alt key is pressed and released by itself. */
2979 return 0;
2980 windows_translate = 1;
2981 break;
2982 case VK_CAPITAL:
2983 /* Decide whether to treat as modifier or function key. */
2984 if (NILP (Vw32_enable_caps_lock))
2985 goto disable_lock_key;
2986 windows_translate = 1;
2987 break;
2988 case VK_NUMLOCK:
2989 /* Decide whether to treat as modifier or function key. */
2990 if (NILP (Vw32_enable_num_lock))
2991 goto disable_lock_key;
2992 windows_translate = 1;
2993 break;
2994 case VK_SCROLL:
2995 /* Decide whether to treat as modifier or function key. */
2996 if (NILP (Vw32_scroll_lock_modifier))
2997 goto disable_lock_key;
2998 windows_translate = 1;
2999 break;
3000 disable_lock_key:
3001 /* Ensure the appropriate lock key state (and indicator light)
3002 remains in the same state. We do this by faking another
3003 press of the relevant key. Apparently, this really is the
3004 only way to toggle the state of the indicator lights. */
3005 dpyinfo->faked_key = wParam;
3006 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
3007 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
3008 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
3009 KEYEVENTF_EXTENDEDKEY | 0, 0);
3010 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
3011 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
3012 /* Ensure indicator lights are updated promptly on Windows 9x
3013 (TranslateMessage apparently does this), after forwarding
3014 input event. */
3015 post_character_message (hwnd, msg, wParam, lParam,
3016 w32_get_key_modifiers (wParam, lParam));
3017 windows_translate = 1;
3018 break;
3019 case VK_CONTROL:
3020 case VK_SHIFT:
3021 case VK_PROCESSKEY: /* Generated by IME. */
3022 windows_translate = 1;
3023 break;
3024 case VK_CANCEL:
3025 /* Windows maps Ctrl-Pause (aka Ctrl-Break) into VK_CANCEL,
3026 which is confusing for purposes of key binding; convert
3027 VK_CANCEL events into VK_PAUSE events. */
3028 wParam = VK_PAUSE;
3029 break;
3030 case VK_PAUSE:
3031 /* Windows maps Ctrl-NumLock into VK_PAUSE, which is confusing
3032 for purposes of key binding; convert these back into
3033 VK_NUMLOCK events, at least when we want to see NumLock key
3034 presses. (Note that there is never any possibility that
3035 VK_PAUSE with Ctrl really is C-Pause as per above.) */
3036 if (NILP (Vw32_enable_num_lock) && modifier_set (VK_CONTROL))
3037 wParam = VK_NUMLOCK;
3038 break;
3039 default:
3040 /* If not defined as a function key, change it to a WM_CHAR message. */
3041 if (wParam > 255 || !lispy_function_keys[wParam])
3043 DWORD modifiers = construct_console_modifiers ();
3045 if (!NILP (Vw32_recognize_altgr)
3046 && modifier_set (VK_LCONTROL) && modifier_set (VK_RMENU))
3048 /* Always let TranslateMessage handle AltGr key chords;
3049 for some reason, ToAscii doesn't always process AltGr
3050 chords correctly. */
3051 windows_translate = 1;
3053 else if ((modifiers & (~SHIFT_PRESSED & ~CAPSLOCK_ON)) != 0)
3055 /* Handle key chords including any modifiers other
3056 than shift directly, in order to preserve as much
3057 modifier information as possible. */
3058 if ('A' <= wParam && wParam <= 'Z')
3060 /* Don't translate modified alphabetic keystrokes,
3061 so the user doesn't need to constantly switch
3062 layout to type control or meta keystrokes when
3063 the normal layout translates alphabetic
3064 characters to non-ascii characters. */
3065 if (!modifier_set (VK_SHIFT))
3066 wParam += ('a' - 'A');
3067 msg = WM_CHAR;
3069 else
3071 /* Try to handle other keystrokes by determining the
3072 base character (ie. translating the base key plus
3073 shift modifier). */
3074 int add;
3075 int isdead = 0;
3076 KEY_EVENT_RECORD key;
3078 key.bKeyDown = TRUE;
3079 key.wRepeatCount = 1;
3080 key.wVirtualKeyCode = wParam;
3081 key.wVirtualScanCode = (lParam & 0xFF0000) >> 16;
3082 key.uChar.AsciiChar = 0;
3083 key.dwControlKeyState = modifiers;
3085 add = w32_kbd_patch_key (&key);
3086 /* 0 means an unrecognised keycode, negative means
3087 dead key. Ignore both. */
3088 while (--add >= 0)
3090 /* Forward asciified character sequence. */
3091 post_character_message
3092 (hwnd, WM_CHAR,
3093 (unsigned char) key.uChar.AsciiChar, lParam,
3094 w32_get_key_modifiers (wParam, lParam));
3095 w32_kbd_patch_key (&key);
3097 return 0;
3100 else
3102 /* Let TranslateMessage handle everything else. */
3103 windows_translate = 1;
3108 translate:
3109 if (windows_translate)
3111 MSG windows_msg = { hwnd, msg, wParam, lParam, 0, {0,0} };
3112 windows_msg.time = GetMessageTime ();
3113 TranslateMessage (&windows_msg);
3114 goto dflt;
3117 /* Fall through */
3119 case WM_SYSCHAR:
3120 case WM_CHAR:
3121 post_character_message (hwnd, msg, wParam, lParam,
3122 w32_get_key_modifiers (wParam, lParam));
3123 break;
3125 case WM_UNICHAR:
3126 /* WM_UNICHAR looks promising from the docs, but the exact
3127 circumstances in which TranslateMessage sends it is one of those
3128 Microsoft secret API things that EU and US courts are supposed
3129 to have put a stop to already. Spy++ shows it being sent to Notepad
3130 and other MS apps, but never to Emacs.
3132 Some third party IMEs send it in accordance with the official
3133 documentation though, so handle it here.
3135 UNICODE_NOCHAR is used to test for support for this message.
3136 TRUE indicates that the message is supported. */
3137 if (wParam == UNICODE_NOCHAR)
3138 return TRUE;
3141 W32Msg wmsg;
3142 wmsg.dwModifiers = w32_get_key_modifiers (wParam, lParam);
3143 signal_user_input ();
3144 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3146 break;
3148 case WM_IME_CHAR:
3149 /* If we can't get the IME result as unicode, use default processing,
3150 which will at least allow characters decodable in the system locale
3151 get through. */
3152 if (!get_composition_string_fn)
3153 goto dflt;
3155 else if (!ignore_ime_char)
3157 wchar_t * buffer;
3158 int size, i;
3159 W32Msg wmsg;
3160 HIMC context = get_ime_context_fn (hwnd);
3161 wmsg.dwModifiers = w32_get_key_modifiers (wParam, lParam);
3162 /* Get buffer size. */
3163 size = get_composition_string_fn (context, GCS_RESULTSTR, buffer, 0);
3164 buffer = alloca(size);
3165 size = get_composition_string_fn (context, GCS_RESULTSTR,
3166 buffer, size);
3167 release_ime_context_fn (hwnd, context);
3169 signal_user_input ();
3170 for (i = 0; i < size / sizeof (wchar_t); i++)
3172 my_post_msg (&wmsg, hwnd, WM_UNICHAR, (WPARAM) buffer[i],
3173 lParam);
3175 /* Ignore the messages for the rest of the
3176 characters in the string that was output above. */
3177 ignore_ime_char = (size / sizeof (wchar_t)) - 1;
3179 else
3180 ignore_ime_char--;
3182 break;
3184 case WM_IME_STARTCOMPOSITION:
3185 if (!set_ime_composition_window_fn)
3186 goto dflt;
3187 else
3189 COMPOSITIONFORM form;
3190 HIMC context;
3191 struct window *w;
3193 if (!context)
3194 break;
3196 f = x_window_to_frame (dpyinfo, hwnd);
3197 w = XWINDOW (FRAME_SELECTED_WINDOW (f));
3199 form.dwStyle = CFS_RECT;
3200 form.ptCurrentPos.x = w32_system_caret_x;
3201 form.ptCurrentPos.y = w32_system_caret_y;
3203 form.rcArea.left = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, 0);
3204 form.rcArea.top = (WINDOW_TOP_EDGE_Y (w)
3205 + WINDOW_HEADER_LINE_HEIGHT (w));
3206 form.rcArea.right = (WINDOW_BOX_RIGHT_EDGE_X (w)
3207 - WINDOW_RIGHT_MARGIN_WIDTH (w)
3208 - WINDOW_RIGHT_FRINGE_WIDTH (w));
3209 form.rcArea.bottom = (WINDOW_BOTTOM_EDGE_Y (w)
3210 - WINDOW_MODE_LINE_HEIGHT (w));
3212 context = get_ime_context_fn (hwnd);
3213 set_ime_composition_window_fn (context, &form);
3214 release_ime_context_fn (hwnd, context);
3216 break;
3218 case WM_IME_ENDCOMPOSITION:
3219 ignore_ime_char = 0;
3220 goto dflt;
3222 /* Simulate middle mouse button events when left and right buttons
3223 are used together, but only if user has two button mouse. */
3224 case WM_LBUTTONDOWN:
3225 case WM_RBUTTONDOWN:
3226 if (w32_num_mouse_buttons > 2)
3227 goto handle_plain_button;
3230 int this = (msg == WM_LBUTTONDOWN) ? LMOUSE : RMOUSE;
3231 int other = (msg == WM_LBUTTONDOWN) ? RMOUSE : LMOUSE;
3233 if (button_state & this)
3234 return 0;
3236 if (button_state == 0)
3237 SetCapture (hwnd);
3239 button_state |= this;
3241 if (button_state & other)
3243 if (mouse_button_timer)
3245 KillTimer (hwnd, mouse_button_timer);
3246 mouse_button_timer = 0;
3248 /* Generate middle mouse event instead. */
3249 msg = WM_MBUTTONDOWN;
3250 button_state |= MMOUSE;
3252 else if (button_state & MMOUSE)
3254 /* Ignore button event if we've already generated a
3255 middle mouse down event. This happens if the
3256 user releases and press one of the two buttons
3257 after we've faked a middle mouse event. */
3258 return 0;
3260 else
3262 /* Flush out saved message. */
3263 post_msg (&saved_mouse_button_msg);
3265 wmsg.dwModifiers = w32_get_modifiers ();
3266 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3267 signal_user_input ();
3269 /* Clear message buffer. */
3270 saved_mouse_button_msg.msg.hwnd = 0;
3272 else
3274 /* Hold onto message for now. */
3275 mouse_button_timer =
3276 SetTimer (hwnd, MOUSE_BUTTON_ID,
3277 w32_mouse_button_tolerance, NULL);
3278 saved_mouse_button_msg.msg.hwnd = hwnd;
3279 saved_mouse_button_msg.msg.message = msg;
3280 saved_mouse_button_msg.msg.wParam = wParam;
3281 saved_mouse_button_msg.msg.lParam = lParam;
3282 saved_mouse_button_msg.msg.time = GetMessageTime ();
3283 saved_mouse_button_msg.dwModifiers = w32_get_modifiers ();
3286 return 0;
3288 case WM_LBUTTONUP:
3289 case WM_RBUTTONUP:
3290 if (w32_num_mouse_buttons > 2)
3291 goto handle_plain_button;
3294 int this = (msg == WM_LBUTTONUP) ? LMOUSE : RMOUSE;
3295 int other = (msg == WM_LBUTTONUP) ? RMOUSE : LMOUSE;
3297 if ((button_state & this) == 0)
3298 return 0;
3300 button_state &= ~this;
3302 if (button_state & MMOUSE)
3304 /* Only generate event when second button is released. */
3305 if ((button_state & other) == 0)
3307 msg = WM_MBUTTONUP;
3308 button_state &= ~MMOUSE;
3310 if (button_state) abort ();
3312 else
3313 return 0;
3315 else
3317 /* Flush out saved message if necessary. */
3318 if (saved_mouse_button_msg.msg.hwnd)
3320 post_msg (&saved_mouse_button_msg);
3323 wmsg.dwModifiers = w32_get_modifiers ();
3324 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3325 signal_user_input ();
3327 /* Always clear message buffer and cancel timer. */
3328 saved_mouse_button_msg.msg.hwnd = 0;
3329 KillTimer (hwnd, mouse_button_timer);
3330 mouse_button_timer = 0;
3332 if (button_state == 0)
3333 ReleaseCapture ();
3335 return 0;
3337 case WM_XBUTTONDOWN:
3338 case WM_XBUTTONUP:
3339 if (w32_pass_extra_mouse_buttons_to_system)
3340 goto dflt;
3341 /* else fall through and process them. */
3342 case WM_MBUTTONDOWN:
3343 case WM_MBUTTONUP:
3344 handle_plain_button:
3346 BOOL up;
3347 int button;
3349 /* Ignore middle and extra buttons as long as the menu is active. */
3350 f = x_window_to_frame (dpyinfo, hwnd);
3351 if (f && f->output_data.w32->menubar_active)
3352 return 0;
3354 if (parse_button (msg, HIWORD (wParam), &button, &up))
3356 if (up) ReleaseCapture ();
3357 else SetCapture (hwnd);
3358 button = (button == 0) ? LMOUSE :
3359 ((button == 1) ? MMOUSE : RMOUSE);
3360 if (up)
3361 button_state &= ~button;
3362 else
3363 button_state |= button;
3367 wmsg.dwModifiers = w32_get_modifiers ();
3368 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3369 signal_user_input ();
3371 /* Need to return true for XBUTTON messages, false for others,
3372 to indicate that we processed the message. */
3373 return (msg == WM_XBUTTONDOWN || msg == WM_XBUTTONUP);
3375 case WM_MOUSEMOVE:
3376 /* Ignore mouse movements as long as the menu is active. These
3377 movements are processed by the window manager anyway, and
3378 it's wrong to handle them as if they happened on the
3379 underlying frame. */
3380 f = x_window_to_frame (dpyinfo, hwnd);
3381 if (f && f->output_data.w32->menubar_active)
3382 return 0;
3384 /* If the mouse has just moved into the frame, start tracking
3385 it, so we will be notified when it leaves the frame. Mouse
3386 tracking only works under W98 and NT4 and later. On earlier
3387 versions, there is no way of telling when the mouse leaves the
3388 frame, so we just have to put up with help-echo and mouse
3389 highlighting remaining while the frame is not active. */
3390 if (track_mouse_event_fn && !track_mouse_window)
3392 TRACKMOUSEEVENT tme;
3393 tme.cbSize = sizeof (tme);
3394 tme.dwFlags = TME_LEAVE;
3395 tme.hwndTrack = hwnd;
3397 track_mouse_event_fn (&tme);
3398 track_mouse_window = hwnd;
3400 case WM_VSCROLL:
3401 if (w32_mouse_move_interval <= 0
3402 || (msg == WM_MOUSEMOVE && button_state == 0))
3404 wmsg.dwModifiers = w32_get_modifiers ();
3405 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3406 return 0;
3409 /* Hang onto mouse move and scroll messages for a bit, to avoid
3410 sending such events to Emacs faster than it can process them.
3411 If we get more events before the timer from the first message
3412 expires, we just replace the first message. */
3414 if (saved_mouse_move_msg.msg.hwnd == 0)
3415 mouse_move_timer =
3416 SetTimer (hwnd, MOUSE_MOVE_ID,
3417 w32_mouse_move_interval, NULL);
3419 /* Hold onto message for now. */
3420 saved_mouse_move_msg.msg.hwnd = hwnd;
3421 saved_mouse_move_msg.msg.message = msg;
3422 saved_mouse_move_msg.msg.wParam = wParam;
3423 saved_mouse_move_msg.msg.lParam = lParam;
3424 saved_mouse_move_msg.msg.time = GetMessageTime ();
3425 saved_mouse_move_msg.dwModifiers = w32_get_modifiers ();
3427 return 0;
3429 case WM_MOUSEWHEEL:
3430 case WM_DROPFILES:
3431 wmsg.dwModifiers = w32_get_modifiers ();
3432 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3433 signal_user_input ();
3434 return 0;
3436 case WM_APPCOMMAND:
3437 if (w32_pass_multimedia_buttons_to_system)
3438 goto dflt;
3439 /* Otherwise, pass to lisp, the same way we do with mousehwheel. */
3440 case WM_MOUSEHWHEEL:
3441 wmsg.dwModifiers = w32_get_modifiers ();
3442 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3443 signal_user_input ();
3444 /* Non-zero must be returned when WM_MOUSEHWHEEL messages are
3445 handled, to prevent the system trying to handle it by faking
3446 scroll bar events. */
3447 return 1;
3449 case WM_TIMER:
3450 /* Flush out saved messages if necessary. */
3451 if (wParam == mouse_button_timer)
3453 if (saved_mouse_button_msg.msg.hwnd)
3455 post_msg (&saved_mouse_button_msg);
3456 signal_user_input ();
3457 saved_mouse_button_msg.msg.hwnd = 0;
3459 KillTimer (hwnd, mouse_button_timer);
3460 mouse_button_timer = 0;
3462 else if (wParam == mouse_move_timer)
3464 if (saved_mouse_move_msg.msg.hwnd)
3466 post_msg (&saved_mouse_move_msg);
3467 saved_mouse_move_msg.msg.hwnd = 0;
3469 KillTimer (hwnd, mouse_move_timer);
3470 mouse_move_timer = 0;
3472 else if (wParam == menu_free_timer)
3474 KillTimer (hwnd, menu_free_timer);
3475 menu_free_timer = 0;
3476 f = x_window_to_frame (dpyinfo, hwnd);
3477 /* If a popup menu is active, don't wipe its strings. */
3478 if (menubar_in_use
3479 && current_popup_menu == NULL)
3481 /* Free memory used by owner-drawn and help-echo strings. */
3482 w32_free_menu_strings (hwnd);
3483 f->output_data.w32->menubar_active = 0;
3484 menubar_in_use = 0;
3487 else if (wParam == hourglass_timer)
3489 KillTimer (hwnd, hourglass_timer);
3490 hourglass_timer = 0;
3491 w32_show_hourglass (x_window_to_frame (dpyinfo, hwnd));
3493 return 0;
3495 case WM_NCACTIVATE:
3496 /* Windows doesn't send us focus messages when putting up and
3497 taking down a system popup dialog as for Ctrl-Alt-Del on Windows 95.
3498 The only indication we get that something happened is receiving
3499 this message afterwards. So this is a good time to reset our
3500 keyboard modifiers' state. */
3501 reset_modifiers ();
3502 goto dflt;
3504 case WM_INITMENU:
3505 button_state = 0;
3506 ReleaseCapture ();
3507 /* We must ensure menu bar is fully constructed and up to date
3508 before allowing user interaction with it. To achieve this
3509 we send this message to the lisp thread and wait for a
3510 reply (whose value is not actually needed) to indicate that
3511 the menu bar is now ready for use, so we can now return.
3513 To remain responsive in the meantime, we enter a nested message
3514 loop that can process all other messages.
3516 However, we skip all this if the message results from calling
3517 TrackPopupMenu - in fact, we must NOT attempt to send the lisp
3518 thread a message because it is blocked on us at this point. We
3519 set menubar_active before calling TrackPopupMenu to indicate
3520 this (there is no possibility of confusion with real menubar
3521 being active). */
3523 f = x_window_to_frame (dpyinfo, hwnd);
3524 if (f
3525 && (f->output_data.w32->menubar_active
3526 /* We can receive this message even in the absence of a
3527 menubar (ie. when the system menu is activated) - in this
3528 case we do NOT want to forward the message, otherwise it
3529 will cause the menubar to suddenly appear when the user
3530 had requested it to be turned off! */
3531 || f->output_data.w32->menubar_widget == NULL))
3532 return 0;
3535 deferred_msg msg_buf;
3537 /* Detect if message has already been deferred; in this case
3538 we cannot return any sensible value to ignore this. */
3539 if (find_deferred_msg (hwnd, msg) != NULL)
3540 abort ();
3542 menubar_in_use = 1;
3544 return send_deferred_msg (&msg_buf, hwnd, msg, wParam, lParam);
3547 case WM_EXITMENULOOP:
3548 f = x_window_to_frame (dpyinfo, hwnd);
3550 /* If a menu is still active, check again after a short delay,
3551 since Windows often (always?) sends the WM_EXITMENULOOP
3552 before the corresponding WM_COMMAND message.
3553 Don't do this if a popup menu is active, since it is only
3554 menubar menus that require cleaning up in this way.
3556 if (f && menubar_in_use && current_popup_menu == NULL)
3557 menu_free_timer = SetTimer (hwnd, MENU_FREE_ID, MENU_FREE_DELAY, NULL);
3559 /* If hourglass cursor should be displayed, display it now. */
3560 if (f && f->output_data.w32->hourglass_p)
3561 SetCursor (f->output_data.w32->hourglass_cursor);
3563 goto dflt;
3565 case WM_MENUSELECT:
3566 /* Direct handling of help_echo in menus. Should be safe now
3567 that we generate the help_echo by placing a help event in the
3568 keyboard buffer. */
3570 HMENU menu = (HMENU) lParam;
3571 UINT menu_item = (UINT) LOWORD (wParam);
3572 UINT flags = (UINT) HIWORD (wParam);
3574 w32_menu_display_help (hwnd, menu, menu_item, flags);
3576 return 0;
3578 case WM_MEASUREITEM:
3579 f = x_window_to_frame (dpyinfo, hwnd);
3580 if (f)
3582 MEASUREITEMSTRUCT * pMis = (MEASUREITEMSTRUCT *) lParam;
3584 if (pMis->CtlType == ODT_MENU)
3586 /* Work out dimensions for popup menu titles. */
3587 char * title = (char *) pMis->itemData;
3588 HDC hdc = GetDC (hwnd);
3589 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
3590 LOGFONT menu_logfont;
3591 HFONT old_font;
3592 SIZE size;
3594 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
3595 menu_logfont.lfWeight = FW_BOLD;
3596 menu_font = CreateFontIndirect (&menu_logfont);
3597 old_font = SelectObject (hdc, menu_font);
3599 pMis->itemHeight = GetSystemMetrics (SM_CYMENUSIZE);
3600 if (title)
3602 if (unicode_append_menu)
3603 GetTextExtentPoint32W (hdc, (WCHAR *) title,
3604 wcslen ((WCHAR *) title),
3605 &size);
3606 else
3607 GetTextExtentPoint32 (hdc, title, strlen (title), &size);
3609 pMis->itemWidth = size.cx;
3610 if (pMis->itemHeight < size.cy)
3611 pMis->itemHeight = size.cy;
3613 else
3614 pMis->itemWidth = 0;
3616 SelectObject (hdc, old_font);
3617 DeleteObject (menu_font);
3618 ReleaseDC (hwnd, hdc);
3619 return TRUE;
3622 return 0;
3624 case WM_DRAWITEM:
3625 f = x_window_to_frame (dpyinfo, hwnd);
3626 if (f)
3628 DRAWITEMSTRUCT * pDis = (DRAWITEMSTRUCT *) lParam;
3630 if (pDis->CtlType == ODT_MENU)
3632 /* Draw popup menu title. */
3633 char * title = (char *) pDis->itemData;
3634 if (title)
3636 HDC hdc = pDis->hDC;
3637 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
3638 LOGFONT menu_logfont;
3639 HFONT old_font;
3641 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
3642 menu_logfont.lfWeight = FW_BOLD;
3643 menu_font = CreateFontIndirect (&menu_logfont);
3644 old_font = SelectObject (hdc, menu_font);
3646 /* Always draw title as if not selected. */
3647 if (unicode_append_menu)
3648 ExtTextOutW (hdc,
3649 pDis->rcItem.left
3650 + GetSystemMetrics (SM_CXMENUCHECK),
3651 pDis->rcItem.top,
3652 ETO_OPAQUE, &pDis->rcItem,
3653 (WCHAR *) title,
3654 wcslen ((WCHAR *) title), NULL);
3655 else
3656 ExtTextOut (hdc,
3657 pDis->rcItem.left
3658 + GetSystemMetrics (SM_CXMENUCHECK),
3659 pDis->rcItem.top,
3660 ETO_OPAQUE, &pDis->rcItem,
3661 title, strlen (title), NULL);
3663 SelectObject (hdc, old_font);
3664 DeleteObject (menu_font);
3666 return TRUE;
3669 return 0;
3671 #if 0
3672 /* Still not right - can't distinguish between clicks in the
3673 client area of the frame from clicks forwarded from the scroll
3674 bars - may have to hook WM_NCHITTEST to remember the mouse
3675 position and then check if it is in the client area ourselves. */
3676 case WM_MOUSEACTIVATE:
3677 /* Discard the mouse click that activates a frame, allowing the
3678 user to click anywhere without changing point (or worse!).
3679 Don't eat mouse clicks on scrollbars though!! */
3680 if (LOWORD (lParam) == HTCLIENT )
3681 return MA_ACTIVATEANDEAT;
3682 goto dflt;
3683 #endif
3685 case WM_MOUSELEAVE:
3686 /* No longer tracking mouse. */
3687 track_mouse_window = NULL;
3689 case WM_ACTIVATEAPP:
3690 case WM_ACTIVATE:
3691 case WM_WINDOWPOSCHANGED:
3692 case WM_SHOWWINDOW:
3693 /* Inform lisp thread that a frame might have just been obscured
3694 or exposed, so should recheck visibility of all frames. */
3695 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3696 goto dflt;
3698 case WM_SETFOCUS:
3699 dpyinfo->faked_key = 0;
3700 reset_modifiers ();
3701 register_hot_keys (hwnd);
3702 goto command;
3703 case WM_KILLFOCUS:
3704 unregister_hot_keys (hwnd);
3705 button_state = 0;
3706 ReleaseCapture ();
3707 /* Relinquish the system caret. */
3708 if (w32_system_caret_hwnd)
3710 w32_visible_system_caret_hwnd = NULL;
3711 w32_system_caret_hwnd = NULL;
3712 DestroyCaret ();
3714 goto command;
3715 case WM_COMMAND:
3716 menubar_in_use = 0;
3717 f = x_window_to_frame (dpyinfo, hwnd);
3718 if (f && HIWORD (wParam) == 0)
3720 if (menu_free_timer)
3722 KillTimer (hwnd, menu_free_timer);
3723 menu_free_timer = 0;
3726 case WM_MOVE:
3727 case WM_SIZE:
3728 command:
3729 wmsg.dwModifiers = w32_get_modifiers ();
3730 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3731 goto dflt;
3733 case WM_DESTROY:
3734 CoUninitialize ();
3735 return 0;
3737 case WM_CLOSE:
3738 wmsg.dwModifiers = w32_get_modifiers ();
3739 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3740 return 0;
3742 case WM_WINDOWPOSCHANGING:
3743 /* Don't restrict the sizing of tip frames. */
3744 if (hwnd == tip_window)
3745 return 0;
3747 WINDOWPLACEMENT wp;
3748 LPWINDOWPOS lppos = (WINDOWPOS *) lParam;
3750 wp.length = sizeof (WINDOWPLACEMENT);
3751 GetWindowPlacement (hwnd, &wp);
3753 if (wp.showCmd != SW_SHOWMINIMIZED && (lppos->flags & SWP_NOSIZE) == 0)
3755 RECT rect;
3756 int wdiff;
3757 int hdiff;
3758 DWORD font_width;
3759 DWORD line_height;
3760 DWORD internal_border;
3761 DWORD scrollbar_extra;
3762 RECT wr;
3764 wp.length = sizeof (wp);
3765 GetWindowRect (hwnd, &wr);
3767 enter_crit ();
3769 font_width = GetWindowLong (hwnd, WND_FONTWIDTH_INDEX);
3770 line_height = GetWindowLong (hwnd, WND_LINEHEIGHT_INDEX);
3771 internal_border = GetWindowLong (hwnd, WND_BORDER_INDEX);
3772 scrollbar_extra = GetWindowLong (hwnd, WND_SCROLLBAR_INDEX);
3774 leave_crit ();
3776 memset (&rect, 0, sizeof (rect));
3777 AdjustWindowRect (&rect, GetWindowLong (hwnd, GWL_STYLE),
3778 GetMenu (hwnd) != NULL);
3780 /* Force width and height of client area to be exact
3781 multiples of the character cell dimensions. */
3782 wdiff = (lppos->cx - (rect.right - rect.left)
3783 - 2 * internal_border - scrollbar_extra)
3784 % font_width;
3785 hdiff = (lppos->cy - (rect.bottom - rect.top)
3786 - 2 * internal_border)
3787 % line_height;
3789 if (wdiff || hdiff)
3791 /* For right/bottom sizing we can just fix the sizes.
3792 However for top/left sizing we will need to fix the X
3793 and Y positions as well. */
3795 int cx_mintrack = GetSystemMetrics (SM_CXMINTRACK);
3796 int cy_mintrack = GetSystemMetrics (SM_CYMINTRACK);
3798 lppos->cx = max (lppos->cx - wdiff, cx_mintrack);
3799 lppos->cy = max (lppos->cy - hdiff, cy_mintrack);
3801 if (wp.showCmd != SW_SHOWMAXIMIZED
3802 && (lppos->flags & SWP_NOMOVE) == 0)
3804 if (lppos->x != wr.left || lppos->y != wr.top)
3806 lppos->x += wdiff;
3807 lppos->y += hdiff;
3809 else
3811 lppos->flags |= SWP_NOMOVE;
3815 return 0;
3820 goto dflt;
3822 case WM_GETMINMAXINFO:
3823 /* Hack to allow resizing the Emacs frame above the screen size.
3824 Note that Windows 9x limits coordinates to 16-bits. */
3825 ((LPMINMAXINFO) lParam)->ptMaxTrackSize.x = 32767;
3826 ((LPMINMAXINFO) lParam)->ptMaxTrackSize.y = 32767;
3827 return 0;
3829 case WM_SETCURSOR:
3830 if (LOWORD (lParam) == HTCLIENT)
3832 f = x_window_to_frame (dpyinfo, hwnd);
3833 if (f->output_data.w32->hourglass_p && !menubar_in_use
3834 && !current_popup_menu)
3835 SetCursor (f->output_data.w32->hourglass_cursor);
3836 else
3837 SetCursor (f->output_data.w32->current_cursor);
3838 return 0;
3840 goto dflt;
3842 case WM_EMACS_SETCURSOR:
3844 Cursor cursor = (Cursor) wParam;
3845 f = x_window_to_frame (dpyinfo, hwnd);
3846 if (f && cursor)
3848 f->output_data.w32->current_cursor = cursor;
3849 if (!f->output_data.w32->hourglass_p)
3850 SetCursor (cursor);
3852 return 0;
3855 case WM_EMACS_CREATESCROLLBAR:
3856 return (LRESULT) w32_createscrollbar ((struct frame *) wParam,
3857 (struct scroll_bar *) lParam);
3859 case WM_EMACS_SHOWWINDOW:
3860 return ShowWindow ((HWND) wParam, (WPARAM) lParam);
3862 case WM_EMACS_SETFOREGROUND:
3864 HWND foreground_window;
3865 DWORD foreground_thread, retval;
3867 /* On NT 5.0, and apparently Windows 98, it is necessary to
3868 attach to the thread that currently has focus in order to
3869 pull the focus away from it. */
3870 foreground_window = GetForegroundWindow ();
3871 foreground_thread = GetWindowThreadProcessId (foreground_window, NULL);
3872 if (!foreground_window
3873 || foreground_thread == GetCurrentThreadId ()
3874 || !AttachThreadInput (GetCurrentThreadId (),
3875 foreground_thread, TRUE))
3876 foreground_thread = 0;
3878 retval = SetForegroundWindow ((HWND) wParam);
3880 /* Detach from the previous foreground thread. */
3881 if (foreground_thread)
3882 AttachThreadInput (GetCurrentThreadId (),
3883 foreground_thread, FALSE);
3885 return retval;
3888 case WM_EMACS_SETWINDOWPOS:
3890 WINDOWPOS * pos = (WINDOWPOS *) wParam;
3891 return SetWindowPos (hwnd, pos->hwndInsertAfter,
3892 pos->x, pos->y, pos->cx, pos->cy, pos->flags);
3895 case WM_EMACS_DESTROYWINDOW:
3896 DragAcceptFiles ((HWND) wParam, FALSE);
3897 return DestroyWindow ((HWND) wParam);
3899 case WM_EMACS_HIDE_CARET:
3900 return HideCaret (hwnd);
3902 case WM_EMACS_SHOW_CARET:
3903 return ShowCaret (hwnd);
3905 case WM_EMACS_DESTROY_CARET:
3906 w32_system_caret_hwnd = NULL;
3907 w32_visible_system_caret_hwnd = NULL;
3908 return DestroyCaret ();
3910 case WM_EMACS_TRACK_CARET:
3911 /* If there is currently no system caret, create one. */
3912 if (w32_system_caret_hwnd == NULL)
3914 /* Use the default caret width, and avoid changing it
3915 unneccesarily, as it confuses screen reader software. */
3916 w32_system_caret_hwnd = hwnd;
3917 CreateCaret (hwnd, NULL, 0,
3918 w32_system_caret_height);
3921 if (!SetCaretPos (w32_system_caret_x, w32_system_caret_y))
3922 return 0;
3923 /* Ensure visible caret gets turned on when requested. */
3924 else if (w32_use_visible_system_caret
3925 && w32_visible_system_caret_hwnd != hwnd)
3927 w32_visible_system_caret_hwnd = hwnd;
3928 return ShowCaret (hwnd);
3930 /* Ensure visible caret gets turned off when requested. */
3931 else if (!w32_use_visible_system_caret
3932 && w32_visible_system_caret_hwnd)
3934 w32_visible_system_caret_hwnd = NULL;
3935 return HideCaret (hwnd);
3937 else
3938 return 1;
3940 case WM_EMACS_TRACKPOPUPMENU:
3942 UINT flags;
3943 POINT *pos;
3944 int retval;
3945 pos = (POINT *)lParam;
3946 flags = TPM_CENTERALIGN;
3947 if (button_state & LMOUSE)
3948 flags |= TPM_LEFTBUTTON;
3949 else if (button_state & RMOUSE)
3950 flags |= TPM_RIGHTBUTTON;
3952 /* Remember we did a SetCapture on the initial mouse down event,
3953 so for safety, we make sure the capture is cancelled now. */
3954 ReleaseCapture ();
3955 button_state = 0;
3957 /* Use menubar_active to indicate that WM_INITMENU is from
3958 TrackPopupMenu below, and should be ignored. */
3959 f = x_window_to_frame (dpyinfo, hwnd);
3960 if (f)
3961 f->output_data.w32->menubar_active = 1;
3963 if (TrackPopupMenu ((HMENU)wParam, flags, pos->x, pos->y,
3964 0, hwnd, NULL))
3966 MSG amsg;
3967 /* Eat any mouse messages during popupmenu */
3968 while (PeekMessage (&amsg, hwnd, WM_MOUSEFIRST, WM_MOUSELAST,
3969 PM_REMOVE));
3970 /* Get the menu selection, if any */
3971 if (PeekMessage (&amsg, hwnd, WM_COMMAND, WM_COMMAND, PM_REMOVE))
3973 retval = LOWORD (amsg.wParam);
3975 else
3977 retval = 0;
3980 else
3982 retval = -1;
3985 return retval;
3988 default:
3989 /* Check for messages registered at runtime. */
3990 if (msg == msh_mousewheel)
3992 wmsg.dwModifiers = w32_get_modifiers ();
3993 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3994 signal_user_input ();
3995 return 0;
3998 dflt:
3999 return DefWindowProc (hwnd, msg, wParam, lParam);
4003 /* The most common default return code for handled messages is 0. */
4004 return 0;
4007 static void
4008 my_create_window (f)
4009 struct frame * f;
4011 MSG msg;
4013 if (!PostThreadMessage (dwWindowsThreadId, WM_EMACS_CREATEWINDOW, (WPARAM)f, 0))
4014 abort ();
4015 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
4019 /* Create a tooltip window. Unlike my_create_window, we do not do this
4020 indirectly via the Window thread, as we do not need to process Window
4021 messages for the tooltip. Creating tooltips indirectly also creates
4022 deadlocks when tooltips are created for menu items. */
4023 static void
4024 my_create_tip_window (f)
4025 struct frame *f;
4027 RECT rect;
4029 rect.left = rect.top = 0;
4030 rect.right = FRAME_PIXEL_WIDTH (f);
4031 rect.bottom = FRAME_PIXEL_HEIGHT (f);
4033 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
4034 FRAME_EXTERNAL_MENU_BAR (f));
4036 tip_window = FRAME_W32_WINDOW (f)
4037 = CreateWindow (EMACS_CLASS,
4038 f->namebuf,
4039 f->output_data.w32->dwStyle,
4040 f->left_pos,
4041 f->top_pos,
4042 rect.right - rect.left,
4043 rect.bottom - rect.top,
4044 FRAME_W32_WINDOW (SELECTED_FRAME ()), /* owner */
4045 NULL,
4046 hinst,
4047 NULL);
4049 if (tip_window)
4051 SetWindowLong (tip_window, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
4052 SetWindowLong (tip_window, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
4053 SetWindowLong (tip_window, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
4054 SetWindowLong (tip_window, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
4056 /* Tip frames have no scrollbars. */
4057 SetWindowLong (tip_window, WND_SCROLLBAR_INDEX, 0);
4059 /* Do this to discard the default setting specified by our parent. */
4060 ShowWindow (tip_window, SW_HIDE);
4065 /* Create and set up the w32 window for frame F. */
4067 static void
4068 w32_window (f, window_prompting, minibuffer_only)
4069 struct frame *f;
4070 long window_prompting;
4071 int minibuffer_only;
4073 BLOCK_INPUT;
4075 /* Use the resource name as the top-level window name
4076 for looking up resources. Make a non-Lisp copy
4077 for the window manager, so GC relocation won't bother it.
4079 Elsewhere we specify the window name for the window manager. */
4082 char *str = (char *) SDATA (Vx_resource_name);
4083 f->namebuf = (char *) xmalloc (strlen (str) + 1);
4084 strcpy (f->namebuf, str);
4087 my_create_window (f);
4089 validate_x_resource_name ();
4091 /* x_set_name normally ignores requests to set the name if the
4092 requested name is the same as the current name. This is the one
4093 place where that assumption isn't correct; f->name is set, but
4094 the server hasn't been told. */
4096 Lisp_Object name;
4097 int explicit = f->explicit_name;
4099 f->explicit_name = 0;
4100 name = f->name;
4101 f->name = Qnil;
4102 x_set_name (f, name, explicit);
4105 UNBLOCK_INPUT;
4107 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
4108 initialize_frame_menubar (f);
4110 if (FRAME_W32_WINDOW (f) == 0)
4111 error ("Unable to create window");
4114 /* Handle the icon stuff for this window. Perhaps later we might
4115 want an x_set_icon_position which can be called interactively as
4116 well. */
4118 static void
4119 x_icon (f, parms)
4120 struct frame *f;
4121 Lisp_Object parms;
4123 Lisp_Object icon_x, icon_y;
4124 struct w32_display_info *dpyinfo = &one_w32_display_info;
4126 /* Set the position of the icon. Note that Windows 95 groups all
4127 icons in the tray. */
4128 icon_x = x_get_arg (dpyinfo, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
4129 icon_y = x_get_arg (dpyinfo, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
4130 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
4132 CHECK_NUMBER (icon_x);
4133 CHECK_NUMBER (icon_y);
4135 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
4136 error ("Both left and top icon corners of icon must be specified");
4138 BLOCK_INPUT;
4140 if (! EQ (icon_x, Qunbound))
4141 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
4143 #if 0 /* TODO */
4144 /* Start up iconic or window? */
4145 x_wm_set_window_state
4146 (f, (EQ (x_get_arg (dpyinfo, parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL), Qicon)
4147 ? IconicState
4148 : NormalState));
4150 x_text_icon (f, (char *) SDATA ((!NILP (f->icon_name)
4151 ? f->icon_name
4152 : f->name)));
4153 #endif
4155 UNBLOCK_INPUT;
4159 static void
4160 x_make_gc (f)
4161 struct frame *f;
4163 XGCValues gc_values;
4165 BLOCK_INPUT;
4167 /* Create the GC's of this frame.
4168 Note that many default values are used. */
4170 /* Normal video */
4171 gc_values.font = FRAME_FONT (f);
4173 /* Cursor has cursor-color background, background-color foreground. */
4174 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
4175 gc_values.background = f->output_data.w32->cursor_pixel;
4176 f->output_data.w32->cursor_gc
4177 = XCreateGC (NULL, FRAME_W32_WINDOW (f),
4178 (GCFont | GCForeground | GCBackground),
4179 &gc_values);
4181 /* Reliefs. */
4182 f->output_data.w32->white_relief.gc = 0;
4183 f->output_data.w32->black_relief.gc = 0;
4185 UNBLOCK_INPUT;
4189 /* Handler for signals raised during x_create_frame and
4190 x_create_top_frame. FRAME is the frame which is partially
4191 constructed. */
4193 static Lisp_Object
4194 unwind_create_frame (frame)
4195 Lisp_Object frame;
4197 struct frame *f = XFRAME (frame);
4199 /* If frame is ``official'', nothing to do. */
4200 if (!CONSP (Vframe_list) || !EQ (XCAR (Vframe_list), frame))
4202 #ifdef GLYPH_DEBUG
4203 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4204 #endif
4206 x_free_frame_resources (f);
4208 #if GLYPH_DEBUG
4209 /* Check that reference counts are indeed correct. */
4210 xassert (dpyinfo->reference_count == dpyinfo_refcount);
4211 xassert (dpyinfo->image_cache->refcount == image_cache_refcount);
4212 #endif
4213 return Qt;
4216 return Qnil;
4219 static void
4220 x_default_font_parameter (f, parms)
4221 struct frame *f;
4222 Lisp_Object parms;
4224 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4225 Lisp_Object font_param = x_get_arg (dpyinfo, parms, Qfont, NULL, NULL,
4226 RES_TYPE_STRING);
4227 Lisp_Object font;
4228 if (EQ (font_param, Qunbound))
4229 font_param = Qnil;
4230 font = !NILP (font_param) ? font_param
4231 : x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
4233 if (!STRINGP (font))
4235 int i;
4236 static char *names[]
4237 = { "Courier New-10",
4238 "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1",
4239 "-*-Fixedsys-normal-r-*-*-12-*-*-*-c-*-iso8859-1",
4240 "Fixedsys",
4241 NULL };
4243 for (i = 0; names[i]; i++)
4245 font = font_open_by_name (f, names[i]);
4246 if (! NILP (font))
4247 break;
4249 if (NILP (font))
4250 error ("No suitable font was found");
4252 else if (!NILP (font_param))
4254 /* Remember the explicit font parameter, so we can re-apply it after
4255 we've applied the `default' face settings. */
4256 x_set_frame_parameters (f, Fcons (Fcons (Qfont_param, font_param), Qnil));
4258 x_default_parameter (f, parms, Qfont, font, "font", "Font", RES_TYPE_STRING);
4261 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
4262 1, 1, 0,
4263 doc: /* Make a new window, which is called a \"frame\" in Emacs terms.
4264 Return an Emacs frame object.
4265 PARAMETERS is an alist of frame parameters.
4266 If the parameters specify that the frame should not have a minibuffer,
4267 and do not specify a specific minibuffer window to use,
4268 then `default-minibuffer-frame' must be a frame whose minibuffer can
4269 be shared by the new frame.
4271 This function is an internal primitive--use `make-frame' instead. */)
4272 (parameters)
4273 Lisp_Object parameters;
4275 struct frame *f;
4276 Lisp_Object frame, tem;
4277 Lisp_Object name;
4278 int minibuffer_only = 0;
4279 long window_prompting = 0;
4280 int width, height;
4281 int count = SPECPDL_INDEX ();
4282 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4283 Lisp_Object display;
4284 struct w32_display_info *dpyinfo = NULL;
4285 Lisp_Object parent;
4286 struct kboard *kb;
4288 /* Make copy of frame parameters because the original is in pure
4289 storage now. */
4290 parameters = Fcopy_alist (parameters);
4292 /* Use this general default value to start with
4293 until we know if this frame has a specified name. */
4294 Vx_resource_name = Vinvocation_name;
4296 display = x_get_arg (dpyinfo, parameters, Qterminal, 0, 0, RES_TYPE_NUMBER);
4297 if (EQ (display, Qunbound))
4298 display = x_get_arg (dpyinfo, parameters, Qdisplay, 0, 0, RES_TYPE_STRING);
4299 if (EQ (display, Qunbound))
4300 display = Qnil;
4301 dpyinfo = check_x_display_info (display);
4302 kb = dpyinfo->terminal->kboard;
4304 if (!dpyinfo->terminal->name)
4305 error ("Terminal is not live, can't create new frames on it");
4307 name = x_get_arg (dpyinfo, parameters, Qname, "name", "Name", RES_TYPE_STRING);
4308 if (!STRINGP (name)
4309 && ! EQ (name, Qunbound)
4310 && ! NILP (name))
4311 error ("Invalid frame name--not a string or nil");
4313 if (STRINGP (name))
4314 Vx_resource_name = name;
4316 /* See if parent window is specified. */
4317 parent = x_get_arg (dpyinfo, parameters, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
4318 if (EQ (parent, Qunbound))
4319 parent = Qnil;
4320 if (! NILP (parent))
4321 CHECK_NUMBER (parent);
4323 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
4324 /* No need to protect DISPLAY because that's not used after passing
4325 it to make_frame_without_minibuffer. */
4326 frame = Qnil;
4327 GCPRO4 (parameters, parent, name, frame);
4328 tem = x_get_arg (dpyinfo, parameters, Qminibuffer, "minibuffer", "Minibuffer",
4329 RES_TYPE_SYMBOL);
4330 if (EQ (tem, Qnone) || NILP (tem))
4331 f = make_frame_without_minibuffer (Qnil, kb, display);
4332 else if (EQ (tem, Qonly))
4334 f = make_minibuffer_frame ();
4335 minibuffer_only = 1;
4337 else if (WINDOWP (tem))
4338 f = make_frame_without_minibuffer (tem, kb, display);
4339 else
4340 f = make_frame (1);
4342 XSETFRAME (frame, f);
4344 /* Note that Windows does support scroll bars. */
4345 FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;
4347 /* By default, make scrollbars the system standard width. */
4348 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = GetSystemMetrics (SM_CXVSCROLL);
4350 f->terminal = dpyinfo->terminal;
4351 f->terminal->reference_count++;
4353 f->output_method = output_w32;
4354 f->output_data.w32 =
4355 (struct w32_output *) xmalloc (sizeof (struct w32_output));
4356 bzero (f->output_data.w32, sizeof (struct w32_output));
4357 FRAME_FONTSET (f) = -1;
4359 f->icon_name
4360 = x_get_arg (dpyinfo, parameters, Qicon_name, "iconName", "Title",
4361 RES_TYPE_STRING);
4362 if (! STRINGP (f->icon_name))
4363 f->icon_name = Qnil;
4365 /* FRAME_W32_DISPLAY_INFO (f) = dpyinfo; */
4367 /* With FRAME_X_DISPLAY_INFO set up, this unwind-protect is safe. */
4368 record_unwind_protect (unwind_create_frame, frame);
4369 #if GLYPH_DEBUG
4370 image_cache_refcount = FRAME_IMAGE_CACHE (f)->refcount;
4371 dpyinfo_refcount = dpyinfo->reference_count;
4372 #endif /* GLYPH_DEBUG */
4374 /* Specify the parent under which to make this window. */
4376 if (!NILP (parent))
4378 f->output_data.w32->parent_desc = (Window) XFASTINT (parent);
4379 f->output_data.w32->explicit_parent = 1;
4381 else
4383 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
4384 f->output_data.w32->explicit_parent = 0;
4387 /* Set the name; the functions to which we pass f expect the name to
4388 be set. */
4389 if (EQ (name, Qunbound) || NILP (name))
4391 f->name = build_string (dpyinfo->w32_id_name);
4392 f->explicit_name = 0;
4394 else
4396 f->name = name;
4397 f->explicit_name = 1;
4398 /* use the frame's title when getting resources for this frame. */
4399 specbind (Qx_resource_name, name);
4402 f->resx = dpyinfo->resx;
4403 f->resy = dpyinfo->resy;
4405 if (uniscribe_available)
4406 register_font_driver (&uniscribe_font_driver, f);
4407 register_font_driver (&w32font_driver, f);
4409 x_default_parameter (f, parameters, Qfont_backend, Qnil,
4410 "fontBackend", "FontBackend", RES_TYPE_STRING);
4411 /* Extract the window parameters from the supplied values
4412 that are needed to determine window geometry. */
4413 x_default_font_parameter (f, parameters);
4414 x_default_parameter (f, parameters, Qborder_width, make_number (2),
4415 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
4417 /* We recognize either internalBorderWidth or internalBorder
4418 (which is what xterm calls it). */
4419 if (NILP (Fassq (Qinternal_border_width, parameters)))
4421 Lisp_Object value;
4423 value = x_get_arg (dpyinfo, parameters, Qinternal_border_width,
4424 "internalBorder", "InternalBorder", RES_TYPE_NUMBER);
4425 if (! EQ (value, Qunbound))
4426 parameters = Fcons (Fcons (Qinternal_border_width, value),
4427 parameters);
4429 /* Default internalBorderWidth to 0 on Windows to match other programs. */
4430 x_default_parameter (f, parameters, Qinternal_border_width, make_number (0),
4431 "internalBorderWidth", "InternalBorder", RES_TYPE_NUMBER);
4432 x_default_parameter (f, parameters, Qvertical_scroll_bars, Qright,
4433 "verticalScrollBars", "ScrollBars", RES_TYPE_SYMBOL);
4435 /* Also do the stuff which must be set before the window exists. */
4436 x_default_parameter (f, parameters, Qforeground_color, build_string ("black"),
4437 "foreground", "Foreground", RES_TYPE_STRING);
4438 x_default_parameter (f, parameters, Qbackground_color, build_string ("white"),
4439 "background", "Background", RES_TYPE_STRING);
4440 x_default_parameter (f, parameters, Qmouse_color, build_string ("black"),
4441 "pointerColor", "Foreground", RES_TYPE_STRING);
4442 x_default_parameter (f, parameters, Qcursor_color, build_string ("black"),
4443 "cursorColor", "Foreground", RES_TYPE_STRING);
4444 x_default_parameter (f, parameters, Qborder_color, build_string ("black"),
4445 "borderColor", "BorderColor", RES_TYPE_STRING);
4446 x_default_parameter (f, parameters, Qscreen_gamma, Qnil,
4447 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
4448 x_default_parameter (f, parameters, Qline_spacing, Qnil,
4449 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
4450 x_default_parameter (f, parameters, Qleft_fringe, Qnil,
4451 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
4452 x_default_parameter (f, parameters, Qright_fringe, Qnil,
4453 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
4456 /* Init faces before x_default_parameter is called for scroll-bar
4457 parameters because that function calls x_set_scroll_bar_width,
4458 which calls change_frame_size, which calls Fset_window_buffer,
4459 which runs hooks, which call Fvertical_motion. At the end, we
4460 end up in init_iterator with a null face cache, which should not
4461 happen. */
4462 init_frame_faces (f);
4464 x_default_parameter (f, parameters, Qmenu_bar_lines, make_number (1),
4465 "menuBar", "MenuBar", RES_TYPE_NUMBER);
4466 x_default_parameter (f, parameters, Qtool_bar_lines, make_number (1),
4467 "toolBar", "ToolBar", RES_TYPE_NUMBER);
4469 x_default_parameter (f, parameters, Qbuffer_predicate, Qnil,
4470 "bufferPredicate", "BufferPredicate", RES_TYPE_SYMBOL);
4471 x_default_parameter (f, parameters, Qtitle, Qnil,
4472 "title", "Title", RES_TYPE_STRING);
4473 x_default_parameter (f, parameters, Qfullscreen, Qnil,
4474 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
4476 f->output_data.w32->dwStyle = WS_OVERLAPPEDWINDOW;
4477 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
4479 f->output_data.w32->text_cursor = w32_load_cursor (IDC_IBEAM);
4480 f->output_data.w32->nontext_cursor = w32_load_cursor (IDC_ARROW);
4481 f->output_data.w32->modeline_cursor = w32_load_cursor (IDC_ARROW);
4482 f->output_data.w32->hand_cursor = w32_load_cursor (IDC_HAND);
4483 f->output_data.w32->hourglass_cursor = w32_load_cursor (IDC_WAIT);
4484 f->output_data.w32->horizontal_drag_cursor = w32_load_cursor (IDC_SIZEWE);
4486 f->output_data.w32->current_cursor = f->output_data.w32->nontext_cursor;
4488 window_prompting = x_figure_window_size (f, parameters, 1);
4490 tem = x_get_arg (dpyinfo, parameters, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
4491 f->no_split = minibuffer_only || EQ (tem, Qt);
4493 w32_window (f, window_prompting, minibuffer_only);
4494 x_icon (f, parameters);
4496 x_make_gc (f);
4498 /* Now consider the frame official. */
4499 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
4500 Vframe_list = Fcons (frame, Vframe_list);
4502 /* We need to do this after creating the window, so that the
4503 icon-creation functions can say whose icon they're describing. */
4504 x_default_parameter (f, parameters, Qicon_type, Qnil,
4505 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
4507 x_default_parameter (f, parameters, Qauto_raise, Qnil,
4508 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4509 x_default_parameter (f, parameters, Qauto_lower, Qnil,
4510 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4511 x_default_parameter (f, parameters, Qcursor_type, Qbox,
4512 "cursorType", "CursorType", RES_TYPE_SYMBOL);
4513 x_default_parameter (f, parameters, Qscroll_bar_width, Qnil,
4514 "scrollBarWidth", "ScrollBarWidth", RES_TYPE_NUMBER);
4515 x_default_parameter (f, parameters, Qalpha, Qnil,
4516 "alpha", "Alpha", RES_TYPE_NUMBER);
4518 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
4519 Change will not be effected unless different from the current
4520 FRAME_LINES (f). */
4521 width = FRAME_COLS (f);
4522 height = FRAME_LINES (f);
4524 FRAME_LINES (f) = 0;
4525 SET_FRAME_COLS (f, 0);
4526 change_frame_size (f, height, width, 1, 0, 0);
4528 /* Tell the server what size and position, etc, we want, and how
4529 badly we want them. This should be done after we have the menu
4530 bar so that its size can be taken into account. */
4531 BLOCK_INPUT;
4532 x_wm_set_size_hint (f, window_prompting, 0);
4533 UNBLOCK_INPUT;
4535 /* Make the window appear on the frame and enable display, unless
4536 the caller says not to. However, with explicit parent, Emacs
4537 cannot control visibility, so don't try. */
4538 if (! f->output_data.w32->explicit_parent)
4540 Lisp_Object visibility;
4542 visibility = x_get_arg (dpyinfo, parameters, Qvisibility, 0, 0, RES_TYPE_SYMBOL);
4543 if (EQ (visibility, Qunbound))
4544 visibility = Qt;
4546 if (EQ (visibility, Qicon))
4547 x_iconify_frame (f);
4548 else if (! NILP (visibility))
4549 x_make_frame_visible (f);
4550 else
4551 /* Must have been Qnil. */
4555 /* Initialize `default-minibuffer-frame' in case this is the first
4556 frame on this terminal. */
4557 if (FRAME_HAS_MINIBUF_P (f)
4558 && (!FRAMEP (kb->Vdefault_minibuffer_frame)
4559 || !FRAME_LIVE_P (XFRAME (kb->Vdefault_minibuffer_frame))))
4560 kb->Vdefault_minibuffer_frame = frame;
4562 /* All remaining specified parameters, which have not been "used"
4563 by x_get_arg and friends, now go in the misc. alist of the frame. */
4564 for (tem = parameters; CONSP (tem); tem = XCDR (tem))
4565 if (CONSP (XCAR (tem)) && !NILP (XCAR (XCAR (tem))))
4566 f->param_alist = Fcons (XCAR (tem), f->param_alist);
4568 UNGCPRO;
4570 /* Make sure windows on this frame appear in calls to next-window
4571 and similar functions. */
4572 Vwindow_list = Qnil;
4574 return unbind_to (count, frame);
4577 /* FRAME is used only to get a handle on the X display. We don't pass the
4578 display info directly because we're called from frame.c, which doesn't
4579 know about that structure. */
4580 Lisp_Object
4581 x_get_focus_frame (frame)
4582 struct frame *frame;
4584 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (frame);
4585 Lisp_Object xfocus;
4586 if (! dpyinfo->w32_focus_frame)
4587 return Qnil;
4589 XSETFRAME (xfocus, dpyinfo->w32_focus_frame);
4590 return xfocus;
4593 DEFUN ("x-focus-frame", Fx_focus_frame, Sx_focus_frame, 1, 1, 0,
4594 doc: /* Give FRAME input focus, raising to foreground if necessary. */)
4595 (frame)
4596 Lisp_Object frame;
4598 x_focus_on_frame (check_x_frame (frame));
4599 return Qnil;
4603 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
4604 doc: /* Internal function called by `color-defined-p', which see. */)
4605 (color, frame)
4606 Lisp_Object color, frame;
4608 XColor foo;
4609 FRAME_PTR f = check_x_frame (frame);
4611 CHECK_STRING (color);
4613 if (w32_defined_color (f, SDATA (color), &foo, 0))
4614 return Qt;
4615 else
4616 return Qnil;
4619 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
4620 doc: /* Internal function called by `color-values', which see. */)
4621 (color, frame)
4622 Lisp_Object color, frame;
4624 XColor foo;
4625 FRAME_PTR f = check_x_frame (frame);
4627 CHECK_STRING (color);
4629 if (w32_defined_color (f, SDATA (color), &foo, 0))
4630 return list3 (make_number ((GetRValue (foo.pixel) << 8)
4631 | GetRValue (foo.pixel)),
4632 make_number ((GetGValue (foo.pixel) << 8)
4633 | GetGValue (foo.pixel)),
4634 make_number ((GetBValue (foo.pixel) << 8)
4635 | GetBValue (foo.pixel)));
4636 else
4637 return Qnil;
4640 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
4641 doc: /* Internal function called by `display-color-p', which see. */)
4642 (display)
4643 Lisp_Object display;
4645 struct w32_display_info *dpyinfo = check_x_display_info (display);
4647 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 2)
4648 return Qnil;
4650 return Qt;
4653 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p,
4654 Sx_display_grayscale_p, 0, 1, 0,
4655 doc: /* Return t if DISPLAY supports shades of gray.
4656 Note that color displays do support shades of gray.
4657 The optional argument DISPLAY specifies which display to ask about.
4658 DISPLAY should be either a frame or a display name (a string).
4659 If omitted or nil, that stands for the selected frame's display. */)
4660 (display)
4661 Lisp_Object display;
4663 struct w32_display_info *dpyinfo = check_x_display_info (display);
4665 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 1)
4666 return Qnil;
4668 return Qt;
4671 DEFUN ("x-display-pixel-width", Fx_display_pixel_width,
4672 Sx_display_pixel_width, 0, 1, 0,
4673 doc: /* Return the width in pixels of DISPLAY.
4674 The optional argument DISPLAY specifies which display to ask about.
4675 DISPLAY should be either a frame or a display name (a string).
4676 If omitted or nil, that stands for the selected frame's display. */)
4677 (display)
4678 Lisp_Object display;
4680 struct w32_display_info *dpyinfo = check_x_display_info (display);
4682 return make_number (x_display_pixel_width (dpyinfo));
4685 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
4686 Sx_display_pixel_height, 0, 1, 0,
4687 doc: /* Return the height in pixels of DISPLAY.
4688 The optional argument DISPLAY specifies which display to ask about.
4689 DISPLAY should be either a frame or a display name (a string).
4690 If omitted or nil, that stands for the selected frame's display. */)
4691 (display)
4692 Lisp_Object display;
4694 struct w32_display_info *dpyinfo = check_x_display_info (display);
4696 return make_number (x_display_pixel_height (dpyinfo));
4699 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
4700 0, 1, 0,
4701 doc: /* Return the number of bitplanes of DISPLAY.
4702 The optional argument DISPLAY specifies which display to ask about.
4703 DISPLAY should be either a frame or a display name (a string).
4704 If omitted or nil, that stands for the selected frame's display. */)
4705 (display)
4706 Lisp_Object display;
4708 struct w32_display_info *dpyinfo = check_x_display_info (display);
4710 return make_number (dpyinfo->n_planes * dpyinfo->n_cbits);
4713 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
4714 0, 1, 0,
4715 doc: /* Return the number of color cells of DISPLAY.
4716 The optional argument DISPLAY specifies which display to ask about.
4717 DISPLAY should be either a frame or a display name (a string).
4718 If omitted or nil, that stands for the selected frame's display. */)
4719 (display)
4720 Lisp_Object display;
4722 struct w32_display_info *dpyinfo = check_x_display_info (display);
4723 HDC hdc;
4724 int cap;
4726 hdc = GetDC (dpyinfo->root_window);
4727 if (dpyinfo->has_palette)
4728 cap = GetDeviceCaps (hdc, SIZEPALETTE);
4729 else
4730 cap = GetDeviceCaps (hdc, NUMCOLORS);
4732 /* We force 24+ bit depths to 24-bit, both to prevent an overflow
4733 and because probably is more meaningful on Windows anyway */
4734 if (cap < 0)
4735 cap = 1 << min (dpyinfo->n_planes * dpyinfo->n_cbits, 24);
4737 ReleaseDC (dpyinfo->root_window, hdc);
4739 return make_number (cap);
4742 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
4743 Sx_server_max_request_size,
4744 0, 1, 0,
4745 doc: /* Return the maximum request size of the server of DISPLAY.
4746 The optional argument DISPLAY specifies which display to ask about.
4747 DISPLAY should be either a frame or a display name (a string).
4748 If omitted or nil, that stands for the selected frame's display. */)
4749 (display)
4750 Lisp_Object display;
4752 struct w32_display_info *dpyinfo = check_x_display_info (display);
4754 return make_number (1);
4757 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
4758 doc: /* Return the "vendor ID" string of the W32 system (Microsoft).
4759 The optional argument DISPLAY specifies which display to ask about.
4760 DISPLAY should be either a frame or a display name (a string).
4761 If omitted or nil, that stands for the selected frame's display. */)
4762 (display)
4763 Lisp_Object display;
4765 return build_string ("Microsoft Corp.");
4768 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
4769 doc: /* Return the version numbers of the server of DISPLAY.
4770 The value is a list of three integers: the major and minor
4771 version numbers of the X Protocol in use, and the distributor-specific
4772 release number. See also the function `x-server-vendor'.
4774 The optional argument DISPLAY specifies which display to ask about.
4775 DISPLAY should be either a frame or a display name (a string).
4776 If omitted or nil, that stands for the selected frame's display. */)
4777 (display)
4778 Lisp_Object display;
4780 return Fcons (make_number (w32_major_version),
4781 Fcons (make_number (w32_minor_version),
4782 Fcons (make_number (w32_build_number), Qnil)));
4785 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
4786 doc: /* Return the number of screens on the server of DISPLAY.
4787 The optional argument DISPLAY specifies which display to ask about.
4788 DISPLAY should be either a frame or a display name (a string).
4789 If omitted or nil, that stands for the selected frame's display. */)
4790 (display)
4791 Lisp_Object display;
4793 return make_number (1);
4796 DEFUN ("x-display-mm-height", Fx_display_mm_height,
4797 Sx_display_mm_height, 0, 1, 0,
4798 doc: /* Return the height in millimeters of DISPLAY.
4799 The optional argument DISPLAY specifies which display to ask about.
4800 DISPLAY should be either a frame or a display name (a string).
4801 If omitted or nil, that stands for the selected frame's display. */)
4802 (display)
4803 Lisp_Object display;
4805 struct w32_display_info *dpyinfo = check_x_display_info (display);
4806 HDC hdc;
4807 int cap;
4809 hdc = GetDC (dpyinfo->root_window);
4811 cap = GetDeviceCaps (hdc, VERTSIZE);
4813 ReleaseDC (dpyinfo->root_window, hdc);
4815 return make_number (cap);
4818 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
4819 doc: /* Return the width in millimeters of DISPLAY.
4820 The optional argument DISPLAY specifies which display to ask about.
4821 DISPLAY should be either a frame or a display name (a string).
4822 If omitted or nil, that stands for the selected frame's display. */)
4823 (display)
4824 Lisp_Object display;
4826 struct w32_display_info *dpyinfo = check_x_display_info (display);
4828 HDC hdc;
4829 int cap;
4831 hdc = GetDC (dpyinfo->root_window);
4833 cap = GetDeviceCaps (hdc, HORZSIZE);
4835 ReleaseDC (dpyinfo->root_window, hdc);
4837 return make_number (cap);
4840 DEFUN ("x-display-backing-store", Fx_display_backing_store,
4841 Sx_display_backing_store, 0, 1, 0,
4842 doc: /* Return an indication of whether DISPLAY does backing store.
4843 The value may be `always', `when-mapped', or `not-useful'.
4844 The optional argument DISPLAY specifies which display to ask about.
4845 DISPLAY should be either a frame or a display name (a string).
4846 If omitted or nil, that stands for the selected frame's display. */)
4847 (display)
4848 Lisp_Object display;
4850 return intern ("not-useful");
4853 DEFUN ("x-display-visual-class", Fx_display_visual_class,
4854 Sx_display_visual_class, 0, 1, 0,
4855 doc: /* Return the visual class of DISPLAY.
4856 The value is one of the symbols `static-gray', `gray-scale',
4857 `static-color', `pseudo-color', `true-color', or `direct-color'.
4859 The optional argument DISPLAY specifies which display to ask about.
4860 DISPLAY should be either a frame or a display name (a string).
4861 If omitted or nil, that stands for the selected frame's display. */)
4862 (display)
4863 Lisp_Object display;
4865 struct w32_display_info *dpyinfo = check_x_display_info (display);
4866 Lisp_Object result = Qnil;
4868 if (dpyinfo->has_palette)
4869 result = intern ("pseudo-color");
4870 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 1)
4871 result = intern ("static-grey");
4872 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 4)
4873 result = intern ("static-color");
4874 else if (dpyinfo->n_planes * dpyinfo->n_cbits > 8)
4875 result = intern ("true-color");
4877 return result;
4880 DEFUN ("x-display-save-under", Fx_display_save_under,
4881 Sx_display_save_under, 0, 1, 0,
4882 doc: /* Return t if DISPLAY supports the save-under feature.
4883 The optional argument DISPLAY specifies which display to ask about.
4884 DISPLAY should be either a frame or a display name (a string).
4885 If omitted or nil, that stands for the selected frame's display. */)
4886 (display)
4887 Lisp_Object display;
4889 return Qnil;
4893 x_pixel_width (f)
4894 register struct frame *f;
4896 return FRAME_PIXEL_WIDTH (f);
4900 x_pixel_height (f)
4901 register struct frame *f;
4903 return FRAME_PIXEL_HEIGHT (f);
4907 x_char_width (f)
4908 register struct frame *f;
4910 return FRAME_COLUMN_WIDTH (f);
4914 x_char_height (f)
4915 register struct frame *f;
4917 return FRAME_LINE_HEIGHT (f);
4921 x_screen_planes (f)
4922 register struct frame *f;
4924 return FRAME_W32_DISPLAY_INFO (f)->n_planes;
4927 /* Return the display structure for the display named NAME.
4928 Open a new connection if necessary. */
4930 struct w32_display_info *
4931 x_display_info_for_name (name)
4932 Lisp_Object name;
4934 Lisp_Object names;
4935 struct w32_display_info *dpyinfo;
4937 CHECK_STRING (name);
4939 for (dpyinfo = &one_w32_display_info, names = w32_display_name_list;
4940 dpyinfo;
4941 dpyinfo = dpyinfo->next, names = XCDR (names))
4943 Lisp_Object tem;
4944 tem = Fstring_equal (XCAR (XCAR (names)), name);
4945 if (!NILP (tem))
4946 return dpyinfo;
4949 /* Use this general default value to start with. */
4950 Vx_resource_name = Vinvocation_name;
4952 validate_x_resource_name ();
4954 dpyinfo = w32_term_init (name, (unsigned char *)0,
4955 (char *) SDATA (Vx_resource_name));
4957 if (dpyinfo == 0)
4958 error ("Cannot connect to server %s", SDATA (name));
4960 w32_in_use = 1;
4961 XSETFASTINT (Vwindow_system_version, w32_major_version);
4963 return dpyinfo;
4966 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
4967 1, 3, 0, doc: /* Open a connection to a server.
4968 DISPLAY is the name of the display to connect to.
4969 Optional second arg XRM-STRING is a string of resources in xrdb format.
4970 If the optional third arg MUST-SUCCEED is non-nil,
4971 terminate Emacs if we can't open the connection. */)
4972 (display, xrm_string, must_succeed)
4973 Lisp_Object display, xrm_string, must_succeed;
4975 unsigned char *xrm_option;
4976 struct w32_display_info *dpyinfo;
4978 /* If initialization has already been done, return now to avoid
4979 overwriting critical parts of one_w32_display_info. */
4980 if (w32_in_use)
4981 return Qnil;
4983 CHECK_STRING (display);
4984 if (! NILP (xrm_string))
4985 CHECK_STRING (xrm_string);
4987 #if 0
4988 if (! EQ (Vwindow_system, intern ("w32")))
4989 error ("Not using Microsoft Windows");
4990 #endif
4992 /* Allow color mapping to be defined externally; first look in user's
4993 HOME directory, then in Emacs etc dir for a file called rgb.txt. */
4995 Lisp_Object color_file;
4996 struct gcpro gcpro1;
4998 color_file = build_string ("~/rgb.txt");
5000 GCPRO1 (color_file);
5002 if (NILP (Ffile_readable_p (color_file)))
5003 color_file =
5004 Fexpand_file_name (build_string ("rgb.txt"),
5005 Fsymbol_value (intern ("data-directory")));
5007 Vw32_color_map = Fx_load_color_file (color_file);
5009 UNGCPRO;
5011 if (NILP (Vw32_color_map))
5012 Vw32_color_map = Fw32_default_color_map ();
5014 /* Merge in system logical colors. */
5015 add_system_logical_colors_to_map (&Vw32_color_map);
5017 if (! NILP (xrm_string))
5018 xrm_option = (unsigned char *) SDATA (xrm_string);
5019 else
5020 xrm_option = (unsigned char *) 0;
5022 /* Use this general default value to start with. */
5023 /* First remove .exe suffix from invocation-name - it looks ugly. */
5025 char basename[ MAX_PATH ], *str;
5027 strcpy (basename, SDATA (Vinvocation_name));
5028 str = strrchr (basename, '.');
5029 if (str) *str = 0;
5030 Vinvocation_name = build_string (basename);
5032 Vx_resource_name = Vinvocation_name;
5034 validate_x_resource_name ();
5036 /* This is what opens the connection and sets x_current_display.
5037 This also initializes many symbols, such as those used for input. */
5038 dpyinfo = w32_term_init (display, xrm_option,
5039 (char *) SDATA (Vx_resource_name));
5041 if (dpyinfo == 0)
5043 if (!NILP (must_succeed))
5044 fatal ("Cannot connect to server %s.\n",
5045 SDATA (display));
5046 else
5047 error ("Cannot connect to server %s", SDATA (display));
5050 w32_in_use = 1;
5052 XSETFASTINT (Vwindow_system_version, w32_major_version);
5053 return Qnil;
5056 DEFUN ("x-close-connection", Fx_close_connection,
5057 Sx_close_connection, 1, 1, 0,
5058 doc: /* Close the connection to DISPLAY's server.
5059 For DISPLAY, specify either a frame or a display name (a string).
5060 If DISPLAY is nil, that stands for the selected frame's display. */)
5061 (display)
5062 Lisp_Object display;
5064 struct w32_display_info *dpyinfo = check_x_display_info (display);
5065 int i;
5067 if (dpyinfo->reference_count > 0)
5068 error ("Display still has frames on it");
5070 BLOCK_INPUT;
5071 x_destroy_all_bitmaps (dpyinfo);
5073 x_delete_display (dpyinfo);
5074 UNBLOCK_INPUT;
5076 return Qnil;
5079 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
5080 doc: /* Return the list of display names that Emacs has connections to. */)
5083 Lisp_Object tail, result;
5085 result = Qnil;
5086 for (tail = w32_display_name_list; CONSP (tail); tail = XCDR (tail))
5087 result = Fcons (XCAR (XCAR (tail)), result);
5089 return result;
5092 DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
5093 doc: /* This is a noop on W32 systems. */)
5094 (on, display)
5095 Lisp_Object display, on;
5097 return Qnil;
5102 /***********************************************************************
5103 Window properties
5104 ***********************************************************************/
5106 DEFUN ("x-change-window-property", Fx_change_window_property,
5107 Sx_change_window_property, 2, 6, 0,
5108 doc: /* Change window property PROP to VALUE on the X window of FRAME.
5109 VALUE may be a string or a list of conses, numbers and/or strings.
5110 If an element in the list is a string, it is converted to
5111 an Atom and the value of the Atom is used. If an element is a cons,
5112 it is converted to a 32 bit number where the car is the 16 top bits and the
5113 cdr is the lower 16 bits.
5114 FRAME nil or omitted means use the selected frame.
5115 If TYPE is given and non-nil, it is the name of the type of VALUE.
5116 If TYPE is not given or nil, the type is STRING.
5117 FORMAT gives the size in bits of each element if VALUE is a list.
5118 It must be one of 8, 16 or 32.
5119 If VALUE is a string or FORMAT is nil or not given, FORMAT defaults to 8.
5120 If OUTER_P is non-nil, the property is changed for the outer X window of
5121 FRAME. Default is to change on the edit X window.
5123 Value is VALUE. */)
5124 (prop, value, frame, type, format, outer_p)
5125 Lisp_Object prop, value, frame, type, format, outer_p;
5127 #if 0 /* TODO : port window properties to W32 */
5128 struct frame *f = check_x_frame (frame);
5129 Atom prop_atom;
5131 CHECK_STRING (prop);
5132 CHECK_STRING (value);
5134 BLOCK_INPUT;
5135 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
5136 XChangeProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
5137 prop_atom, XA_STRING, 8, PropModeReplace,
5138 SDATA (value), SCHARS (value));
5140 /* Make sure the property is set when we return. */
5141 XFlush (FRAME_W32_DISPLAY (f));
5142 UNBLOCK_INPUT;
5144 #endif /* TODO */
5146 return value;
5150 DEFUN ("x-delete-window-property", Fx_delete_window_property,
5151 Sx_delete_window_property, 1, 2, 0,
5152 doc: /* Remove window property PROP from X window of FRAME.
5153 FRAME nil or omitted means use the selected frame. Value is PROP. */)
5154 (prop, frame)
5155 Lisp_Object prop, frame;
5157 #if 0 /* TODO : port window properties to W32 */
5159 struct frame *f = check_x_frame (frame);
5160 Atom prop_atom;
5162 CHECK_STRING (prop);
5163 BLOCK_INPUT;
5164 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
5165 XDeleteProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), prop_atom);
5167 /* Make sure the property is removed when we return. */
5168 XFlush (FRAME_W32_DISPLAY (f));
5169 UNBLOCK_INPUT;
5170 #endif /* TODO */
5172 return prop;
5176 DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
5177 1, 2, 0,
5178 doc: /* Value is the value of window property PROP on FRAME.
5179 If FRAME is nil or omitted, use the selected frame. Value is nil
5180 if FRAME hasn't a property with name PROP or if PROP has no string
5181 value. */)
5182 (prop, frame)
5183 Lisp_Object prop, frame;
5185 #if 0 /* TODO : port window properties to W32 */
5187 struct frame *f = check_x_frame (frame);
5188 Atom prop_atom;
5189 int rc;
5190 Lisp_Object prop_value = Qnil;
5191 char *tmp_data = NULL;
5192 Atom actual_type;
5193 int actual_format;
5194 unsigned long actual_size, bytes_remaining;
5196 CHECK_STRING (prop);
5197 BLOCK_INPUT;
5198 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
5199 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
5200 prop_atom, 0, 0, False, XA_STRING,
5201 &actual_type, &actual_format, &actual_size,
5202 &bytes_remaining, (unsigned char **) &tmp_data);
5203 if (rc == Success)
5205 int size = bytes_remaining;
5207 XFree (tmp_data);
5208 tmp_data = NULL;
5210 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
5211 prop_atom, 0, bytes_remaining,
5212 False, XA_STRING,
5213 &actual_type, &actual_format,
5214 &actual_size, &bytes_remaining,
5215 (unsigned char **) &tmp_data);
5216 if (rc == Success)
5217 prop_value = make_string (tmp_data, size);
5219 XFree (tmp_data);
5222 UNBLOCK_INPUT;
5224 return prop_value;
5226 #endif /* TODO */
5227 return Qnil;
5232 /***********************************************************************
5233 Busy cursor
5234 ***********************************************************************/
5236 /* Default number of seconds to wait before displaying an hourglass
5237 cursor. Duplicated from xdisp.c, but cannot use the version there
5238 due to lack of atimers on w32. */
5239 #define DEFAULT_HOURGLASS_DELAY 1
5240 extern Lisp_Object Vhourglass_delay;
5242 /* Return non-zero if houglass timer has been started or hourglass is shown. */
5243 /* PENDING: if W32 can use atimers (atimer.[hc]) then the common impl in
5244 xdisp.c could be used. */
5247 hourglass_started ()
5249 return hourglass_shown_p || hourglass_timer;
5252 /* Cancel a currently active hourglass timer, and start a new one. */
5254 void
5255 start_hourglass ()
5257 DWORD delay;
5258 int secs, msecs = 0;
5259 struct frame * f = SELECTED_FRAME ();
5261 /* No cursors on non GUI frames. */
5262 if (!FRAME_W32_P (f))
5263 return;
5265 cancel_hourglass ();
5267 if (INTEGERP (Vhourglass_delay)
5268 && XINT (Vhourglass_delay) > 0)
5269 secs = XFASTINT (Vhourglass_delay);
5270 else if (FLOATP (Vhourglass_delay)
5271 && XFLOAT_DATA (Vhourglass_delay) > 0)
5273 Lisp_Object tem;
5274 tem = Ftruncate (Vhourglass_delay, Qnil);
5275 secs = XFASTINT (tem);
5276 msecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000;
5278 else
5279 secs = DEFAULT_HOURGLASS_DELAY;
5281 delay = secs * 1000 + msecs;
5282 hourglass_hwnd = FRAME_W32_WINDOW (f);
5283 hourglass_timer = SetTimer (hourglass_hwnd, HOURGLASS_ID, delay, NULL);
5287 /* Cancel the hourglass cursor timer if active, hide an hourglass
5288 cursor if shown. */
5290 void
5291 cancel_hourglass ()
5293 if (hourglass_timer)
5295 KillTimer (hourglass_hwnd, hourglass_timer);
5296 hourglass_timer = 0;
5299 if (hourglass_shown_p)
5300 w32_hide_hourglass ();
5304 /* Timer function of hourglass_timer.
5306 Display an hourglass cursor. Set the hourglass_p flag in display info
5307 to indicate that an hourglass cursor is shown. */
5309 static void
5310 w32_show_hourglass (f)
5311 struct frame *f;
5313 if (!hourglass_shown_p)
5315 f->output_data.w32->hourglass_p = 1;
5316 if (!menubar_in_use && !current_popup_menu)
5317 SetCursor (f->output_data.w32->hourglass_cursor);
5318 hourglass_shown_p = 1;
5323 /* Hide the hourglass cursor on all frames, if it is currently shown. */
5325 static void
5326 w32_hide_hourglass ()
5328 if (hourglass_shown_p)
5330 struct frame *f = x_window_to_frame (&one_w32_display_info,
5331 hourglass_hwnd);
5332 if (f)
5333 f->output_data.w32->hourglass_p = 0;
5334 else
5335 /* If frame was deleted, restore to selected frame's cursor. */
5336 f = SELECTED_FRAME ();
5338 if (FRAME_W32_P (f))
5339 SetCursor (f->output_data.w32->current_cursor);
5340 else
5341 /* No cursors on non GUI frames - restore to stock arrow cursor. */
5342 SetCursor (w32_load_cursor (IDC_ARROW));
5344 hourglass_shown_p = 0;
5350 /***********************************************************************
5351 Tool tips
5352 ***********************************************************************/
5354 static Lisp_Object x_create_tip_frame P_ ((struct w32_display_info *,
5355 Lisp_Object, Lisp_Object));
5356 static void compute_tip_xy P_ ((struct frame *, Lisp_Object, Lisp_Object,
5357 Lisp_Object, int, int, int *, int *));
5359 /* The frame of a currently visible tooltip. */
5361 Lisp_Object tip_frame;
5363 /* If non-nil, a timer started that hides the last tooltip when it
5364 fires. */
5366 Lisp_Object tip_timer;
5367 Window tip_window;
5369 /* If non-nil, a vector of 3 elements containing the last args
5370 with which x-show-tip was called. See there. */
5372 Lisp_Object last_show_tip_args;
5374 /* Maximum size for tooltips; a cons (COLUMNS . ROWS). */
5376 Lisp_Object Vx_max_tooltip_size;
5379 static Lisp_Object
5380 unwind_create_tip_frame (frame)
5381 Lisp_Object frame;
5383 Lisp_Object deleted;
5385 deleted = unwind_create_frame (frame);
5386 if (EQ (deleted, Qt))
5388 tip_window = NULL;
5389 tip_frame = Qnil;
5392 return deleted;
5396 /* Create a frame for a tooltip on the display described by DPYINFO.
5397 PARMS is a list of frame parameters. TEXT is the string to
5398 display in the tip frame. Value is the frame.
5400 Note that functions called here, esp. x_default_parameter can
5401 signal errors, for instance when a specified color name is
5402 undefined. We have to make sure that we're in a consistent state
5403 when this happens. */
5405 static Lisp_Object
5406 x_create_tip_frame (dpyinfo, parms, text)
5407 struct w32_display_info *dpyinfo;
5408 Lisp_Object parms, text;
5410 struct frame *f;
5411 Lisp_Object frame, tem;
5412 Lisp_Object name;
5413 long window_prompting = 0;
5414 int width, height;
5415 int count = SPECPDL_INDEX ();
5416 struct gcpro gcpro1, gcpro2, gcpro3;
5417 struct kboard *kb;
5418 int face_change_count_before = face_change_count;
5419 Lisp_Object buffer;
5420 struct buffer *old_buffer;
5422 check_w32 ();
5424 /* Use this general default value to start with until we know if
5425 this frame has a specified name. */
5426 Vx_resource_name = Vinvocation_name;
5428 kb = dpyinfo->terminal->kboard;
5430 /* The calls to x_get_arg remove elements from PARMS, so copy it to
5431 avoid destructive changes behind our caller's back. */
5432 parms = Fcopy_alist (parms);
5434 /* Get the name of the frame to use for resource lookup. */
5435 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
5436 if (!STRINGP (name)
5437 && !EQ (name, Qunbound)
5438 && !NILP (name))
5439 error ("Invalid frame name--not a string or nil");
5440 Vx_resource_name = name;
5442 frame = Qnil;
5443 GCPRO3 (parms, name, frame);
5444 /* Make a frame without minibuffer nor mode-line. */
5445 f = make_frame (0);
5446 f->wants_modeline = 0;
5447 XSETFRAME (frame, f);
5449 buffer = Fget_buffer_create (build_string (" *tip*"));
5450 Fset_window_buffer (FRAME_ROOT_WINDOW (f), buffer, Qnil);
5451 old_buffer = current_buffer;
5452 set_buffer_internal_1 (XBUFFER (buffer));
5453 current_buffer->truncate_lines = Qnil;
5454 specbind (Qinhibit_read_only, Qt);
5455 specbind (Qinhibit_modification_hooks, Qt);
5456 Ferase_buffer ();
5457 Finsert (1, &text);
5458 set_buffer_internal_1 (old_buffer);
5460 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
5461 record_unwind_protect (unwind_create_tip_frame, frame);
5463 /* By setting the output method, we're essentially saying that
5464 the frame is live, as per FRAME_LIVE_P. If we get a signal
5465 from this point on, x_destroy_window might screw up reference
5466 counts etc. */
5467 f->terminal = dpyinfo->terminal;
5468 f->terminal->reference_count++;
5469 f->output_method = output_w32;
5470 f->output_data.w32 =
5471 (struct w32_output *) xmalloc (sizeof (struct w32_output));
5472 bzero (f->output_data.w32, sizeof (struct w32_output));
5474 FRAME_FONTSET (f) = -1;
5475 f->icon_name = Qnil;
5477 #if GLYPH_DEBUG
5478 image_cache_refcount = FRAME_IMAGE_CACHE (f)->refcount;
5479 dpyinfo_refcount = dpyinfo->reference_count;
5480 #endif /* GLYPH_DEBUG */
5481 FRAME_KBOARD (f) = kb;
5482 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
5483 f->output_data.w32->explicit_parent = 0;
5485 /* Set the name; the functions to which we pass f expect the name to
5486 be set. */
5487 if (EQ (name, Qunbound) || NILP (name))
5489 f->name = build_string (dpyinfo->w32_id_name);
5490 f->explicit_name = 0;
5492 else
5494 f->name = name;
5495 f->explicit_name = 1;
5496 /* use the frame's title when getting resources for this frame. */
5497 specbind (Qx_resource_name, name);
5500 f->resx = dpyinfo->resx;
5501 f->resy = dpyinfo->resy;
5503 if (uniscribe_available)
5504 register_font_driver (&uniscribe_font_driver, f);
5505 register_font_driver (&w32font_driver, f);
5507 x_default_parameter (f, parms, Qfont_backend, Qnil,
5508 "fontBackend", "FontBackend", RES_TYPE_STRING);
5510 /* Extract the window parameters from the supplied values
5511 that are needed to determine window geometry. */
5512 x_default_font_parameter (f, parms);
5514 x_default_parameter (f, parms, Qborder_width, make_number (2),
5515 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
5516 /* This defaults to 2 in order to match xterm. We recognize either
5517 internalBorderWidth or internalBorder (which is what xterm calls
5518 it). */
5519 if (NILP (Fassq (Qinternal_border_width, parms)))
5521 Lisp_Object value;
5523 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
5524 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
5525 if (! EQ (value, Qunbound))
5526 parms = Fcons (Fcons (Qinternal_border_width, value),
5527 parms);
5529 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
5530 "internalBorderWidth", "internalBorderWidth",
5531 RES_TYPE_NUMBER);
5533 /* Also do the stuff which must be set before the window exists. */
5534 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
5535 "foreground", "Foreground", RES_TYPE_STRING);
5536 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
5537 "background", "Background", RES_TYPE_STRING);
5538 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
5539 "pointerColor", "Foreground", RES_TYPE_STRING);
5540 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
5541 "cursorColor", "Foreground", RES_TYPE_STRING);
5542 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
5543 "borderColor", "BorderColor", RES_TYPE_STRING);
5545 /* Init faces before x_default_parameter is called for scroll-bar
5546 parameters because that function calls x_set_scroll_bar_width,
5547 which calls change_frame_size, which calls Fset_window_buffer,
5548 which runs hooks, which call Fvertical_motion. At the end, we
5549 end up in init_iterator with a null face cache, which should not
5550 happen. */
5551 init_frame_faces (f);
5553 f->output_data.w32->dwStyle = WS_BORDER | WS_POPUP | WS_DISABLED;
5554 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
5556 window_prompting = x_figure_window_size (f, parms, 0);
5558 /* No fringes on tip frame. */
5559 f->fringe_cols = 0;
5560 f->left_fringe_width = 0;
5561 f->right_fringe_width = 0;
5563 BLOCK_INPUT;
5564 my_create_tip_window (f);
5565 UNBLOCK_INPUT;
5567 x_make_gc (f);
5569 x_default_parameter (f, parms, Qauto_raise, Qnil,
5570 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5571 x_default_parameter (f, parms, Qauto_lower, Qnil,
5572 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5573 x_default_parameter (f, parms, Qcursor_type, Qbox,
5574 "cursorType", "CursorType", RES_TYPE_SYMBOL);
5576 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
5577 Change will not be effected unless different from the current
5578 FRAME_LINES (f). */
5579 width = FRAME_COLS (f);
5580 height = FRAME_LINES (f);
5581 FRAME_LINES (f) = 0;
5582 SET_FRAME_COLS (f, 0);
5583 change_frame_size (f, height, width, 1, 0, 0);
5585 /* Add `tooltip' frame parameter's default value. */
5586 if (NILP (Fframe_parameter (frame, intern ("tooltip"))))
5587 Fmodify_frame_parameters (frame, Fcons (Fcons (intern ("tooltip"), Qt),
5588 Qnil));
5590 /* Set up faces after all frame parameters are known. This call
5591 also merges in face attributes specified for new frames.
5593 Frame parameters may be changed if .Xdefaults contains
5594 specifications for the default font. For example, if there is an
5595 `Emacs.default.attributeBackground: pink', the `background-color'
5596 attribute of the frame get's set, which let's the internal border
5597 of the tooltip frame appear in pink. Prevent this. */
5599 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
5600 Lisp_Object fg = Fframe_parameter (frame, Qforeground_color);
5601 Lisp_Object colors = Qnil;
5603 /* Set tip_frame here, so that */
5604 tip_frame = frame;
5605 call2 (Qface_set_after_frame_default, frame, Qnil);
5607 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
5608 colors = Fcons (Fcons (Qbackground_color, bg), colors);
5609 if (!EQ (fg, Fframe_parameter (frame, Qforeground_color)))
5610 colors = Fcons (Fcons (Qforeground_color, fg), colors);
5612 if (!NILP (colors))
5613 Fmodify_frame_parameters (frame, colors);
5616 f->no_split = 1;
5618 UNGCPRO;
5620 /* It is now ok to make the frame official even if we get an error
5621 below. And the frame needs to be on Vframe_list or making it
5622 visible won't work. */
5623 Vframe_list = Fcons (frame, Vframe_list);
5625 /* Now that the frame is official, it counts as a reference to
5626 its display. */
5627 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
5629 /* Setting attributes of faces of the tooltip frame from resources
5630 and similar will increment face_change_count, which leads to the
5631 clearing of all current matrices. Since this isn't necessary
5632 here, avoid it by resetting face_change_count to the value it
5633 had before we created the tip frame. */
5634 face_change_count = face_change_count_before;
5636 /* Discard the unwind_protect. */
5637 return unbind_to (count, frame);
5641 /* Compute where to display tip frame F. PARMS is the list of frame
5642 parameters for F. DX and DY are specified offsets from the current
5643 location of the mouse. WIDTH and HEIGHT are the width and height
5644 of the tooltip. Return coordinates relative to the root window of
5645 the display in *ROOT_X, and *ROOT_Y. */
5647 static void
5648 compute_tip_xy (f, parms, dx, dy, width, height, root_x, root_y)
5649 struct frame *f;
5650 Lisp_Object parms, dx, dy;
5651 int width, height;
5652 int *root_x, *root_y;
5654 Lisp_Object left, top;
5655 int min_x, min_y, max_x, max_y;
5657 /* User-specified position? */
5658 left = Fcdr (Fassq (Qleft, parms));
5659 top = Fcdr (Fassq (Qtop, parms));
5661 /* Move the tooltip window where the mouse pointer is. Resize and
5662 show it. */
5663 if (!INTEGERP (left) || !INTEGERP (top))
5665 POINT pt;
5667 /* Default min and max values. */
5668 min_x = 0;
5669 min_y = 0;
5670 max_x = x_display_pixel_width (FRAME_W32_DISPLAY_INFO (f));
5671 max_y = x_display_pixel_height (FRAME_W32_DISPLAY_INFO (f));
5673 BLOCK_INPUT;
5674 GetCursorPos (&pt);
5675 *root_x = pt.x;
5676 *root_y = pt.y;
5677 UNBLOCK_INPUT;
5679 /* If multiple monitor support is available, constrain the tip onto
5680 the current monitor. This improves the above by allowing negative
5681 co-ordinates if monitor positions are such that they are valid, and
5682 snaps a tooltip onto a single monitor if we are close to the edge
5683 where it would otherwise flow onto the other monitor (or into
5684 nothingness if there is a gap in the overlap). */
5685 if (monitor_from_point_fn && get_monitor_info_fn)
5687 struct MONITOR_INFO info;
5688 HMONITOR monitor
5689 = monitor_from_point_fn (pt, MONITOR_DEFAULT_TO_NEAREST);
5690 info.cbSize = sizeof (info);
5692 if (get_monitor_info_fn (monitor, &info))
5694 min_x = info.rcWork.left;
5695 min_y = info.rcWork.top;
5696 max_x = info.rcWork.right;
5697 max_y = info.rcWork.bottom;
5702 if (INTEGERP (top))
5703 *root_y = XINT (top);
5704 else if (*root_y + XINT (dy) <= min_y)
5705 *root_y = min_y; /* Can happen for negative dy */
5706 else if (*root_y + XINT (dy) + height <= max_y)
5707 /* It fits below the pointer */
5708 *root_y += XINT (dy);
5709 else if (height + XINT (dy) + min_y <= *root_y)
5710 /* It fits above the pointer. */
5711 *root_y -= height + XINT (dy);
5712 else
5713 /* Put it on the top. */
5714 *root_y = min_y;
5716 if (INTEGERP (left))
5717 *root_x = XINT (left);
5718 else if (*root_x + XINT (dx) <= min_x)
5719 *root_x = 0; /* Can happen for negative dx */
5720 else if (*root_x + XINT (dx) + width <= max_x)
5721 /* It fits to the right of the pointer. */
5722 *root_x += XINT (dx);
5723 else if (width + XINT (dx) + min_x <= *root_x)
5724 /* It fits to the left of the pointer. */
5725 *root_x -= width + XINT (dx);
5726 else
5727 /* Put it left justified on the screen -- it ought to fit that way. */
5728 *root_x = min_x;
5732 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
5733 doc: /* Show STRING in a \"tooltip\" window on frame FRAME.
5734 A tooltip window is a small window displaying a string.
5736 This is an internal function; Lisp code should call `tooltip-show'.
5738 FRAME nil or omitted means use the selected frame.
5740 PARMS is an optional list of frame parameters which can be
5741 used to change the tooltip's appearance.
5743 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
5744 means use the default timeout of 5 seconds.
5746 If the list of frame parameters PARMS contains a `left' parameter,
5747 the tooltip is displayed at that x-position. Otherwise it is
5748 displayed at the mouse position, with offset DX added (default is 5 if
5749 DX isn't specified). Likewise for the y-position; if a `top' frame
5750 parameter is specified, it determines the y-position of the tooltip
5751 window, otherwise it is displayed at the mouse position, with offset
5752 DY added (default is -10).
5754 A tooltip's maximum size is specified by `x-max-tooltip-size'.
5755 Text larger than the specified size is clipped. */)
5756 (string, frame, parms, timeout, dx, dy)
5757 Lisp_Object string, frame, parms, timeout, dx, dy;
5759 struct frame *f;
5760 struct window *w;
5761 int root_x, root_y;
5762 struct buffer *old_buffer;
5763 struct text_pos pos;
5764 int i, width, height;
5765 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5766 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5767 int count = SPECPDL_INDEX ();
5769 specbind (Qinhibit_redisplay, Qt);
5771 GCPRO4 (string, parms, frame, timeout);
5773 CHECK_STRING (string);
5774 f = check_x_frame (frame);
5775 if (NILP (timeout))
5776 timeout = make_number (5);
5777 else
5778 CHECK_NATNUM (timeout);
5780 if (NILP (dx))
5781 dx = make_number (5);
5782 else
5783 CHECK_NUMBER (dx);
5785 if (NILP (dy))
5786 dy = make_number (-10);
5787 else
5788 CHECK_NUMBER (dy);
5790 if (NILP (last_show_tip_args))
5791 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
5793 if (!NILP (tip_frame))
5795 Lisp_Object last_string = AREF (last_show_tip_args, 0);
5796 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
5797 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
5799 if (EQ (frame, last_frame)
5800 && !NILP (Fequal (last_string, string))
5801 && !NILP (Fequal (last_parms, parms)))
5803 struct frame *f = XFRAME (tip_frame);
5805 /* Only DX and DY have changed. */
5806 if (!NILP (tip_timer))
5808 Lisp_Object timer = tip_timer;
5809 tip_timer = Qnil;
5810 call1 (Qcancel_timer, timer);
5813 BLOCK_INPUT;
5814 compute_tip_xy (f, parms, dx, dy, FRAME_PIXEL_WIDTH (f),
5815 FRAME_PIXEL_HEIGHT (f), &root_x, &root_y);
5817 /* Put tooltip in topmost group and in position. */
5818 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
5819 root_x, root_y, 0, 0,
5820 SWP_NOSIZE | SWP_NOACTIVATE);
5822 /* Ensure tooltip is on top of other topmost windows (eg menus). */
5823 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
5824 0, 0, 0, 0,
5825 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
5827 UNBLOCK_INPUT;
5828 goto start_timer;
5832 /* Hide a previous tip, if any. */
5833 Fx_hide_tip ();
5835 ASET (last_show_tip_args, 0, string);
5836 ASET (last_show_tip_args, 1, frame);
5837 ASET (last_show_tip_args, 2, parms);
5839 /* Add default values to frame parameters. */
5840 if (NILP (Fassq (Qname, parms)))
5841 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
5842 if (NILP (Fassq (Qinternal_border_width, parms)))
5843 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
5844 if (NILP (Fassq (Qborder_width, parms)))
5845 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
5846 if (NILP (Fassq (Qborder_color, parms)))
5847 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
5848 if (NILP (Fassq (Qbackground_color, parms)))
5849 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
5850 parms);
5852 /* Block input until the tip has been fully drawn, to avoid crashes
5853 when drawing tips in menus. */
5854 BLOCK_INPUT;
5856 /* Create a frame for the tooltip, and record it in the global
5857 variable tip_frame. */
5858 frame = x_create_tip_frame (FRAME_W32_DISPLAY_INFO (f), parms, string);
5859 f = XFRAME (frame);
5861 /* Set up the frame's root window. */
5862 w = XWINDOW (FRAME_ROOT_WINDOW (f));
5863 w->left_col = w->top_line = make_number (0);
5865 if (CONSP (Vx_max_tooltip_size)
5866 && INTEGERP (XCAR (Vx_max_tooltip_size))
5867 && XINT (XCAR (Vx_max_tooltip_size)) > 0
5868 && INTEGERP (XCDR (Vx_max_tooltip_size))
5869 && XINT (XCDR (Vx_max_tooltip_size)) > 0)
5871 w->total_cols = XCAR (Vx_max_tooltip_size);
5872 w->total_lines = XCDR (Vx_max_tooltip_size);
5874 else
5876 w->total_cols = make_number (80);
5877 w->total_lines = make_number (40);
5880 FRAME_TOTAL_COLS (f) = XINT (w->total_cols);
5881 adjust_glyphs (f);
5882 w->pseudo_window_p = 1;
5884 /* Display the tooltip text in a temporary buffer. */
5885 old_buffer = current_buffer;
5886 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->buffer));
5887 current_buffer->truncate_lines = Qnil;
5888 clear_glyph_matrix (w->desired_matrix);
5889 clear_glyph_matrix (w->current_matrix);
5890 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
5891 try_window (FRAME_ROOT_WINDOW (f), pos, 0);
5893 /* Compute width and height of the tooltip. */
5894 width = height = 0;
5895 for (i = 0; i < w->desired_matrix->nrows; ++i)
5897 struct glyph_row *row = &w->desired_matrix->rows[i];
5898 struct glyph *last;
5899 int row_width;
5901 /* Stop at the first empty row at the end. */
5902 if (!row->enabled_p || !row->displays_text_p)
5903 break;
5905 /* Let the row go over the full width of the frame. */
5906 row->full_width_p = 1;
5908 #ifdef TODO /* Investigate why some fonts need more width than is
5909 calculated for some tooltips. */
5910 /* There's a glyph at the end of rows that is use to place
5911 the cursor there. Don't include the width of this glyph. */
5912 if (row->used[TEXT_AREA])
5914 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5915 row_width = row->pixel_width - last->pixel_width;
5917 else
5918 #endif
5919 row_width = row->pixel_width;
5921 /* TODO: find why tips do not draw along baseline as instructed. */
5922 height += row->height;
5923 width = max (width, row_width);
5926 /* Add the frame's internal border to the width and height the X
5927 window should have. */
5928 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5929 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5931 /* Move the tooltip window where the mouse pointer is. Resize and
5932 show it. */
5933 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5936 /* Adjust Window size to take border into account. */
5937 RECT rect;
5938 rect.left = rect.top = 0;
5939 rect.right = width;
5940 rect.bottom = height;
5941 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
5942 FRAME_EXTERNAL_MENU_BAR (f));
5944 /* Position and size tooltip, and put it in the topmost group.
5945 The add-on of 3 to the 5th argument is a kludge: without it,
5946 some fonts cause the last character of the tip to be truncated,
5947 for some obscure reason. */
5948 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
5949 root_x, root_y, rect.right - rect.left + 3,
5950 rect.bottom - rect.top, SWP_NOACTIVATE);
5952 /* Ensure tooltip is on top of other topmost windows (eg menus). */
5953 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
5954 0, 0, 0, 0,
5955 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
5957 /* Let redisplay know that we have made the frame visible already. */
5958 f->async_visible = 1;
5960 ShowWindow (FRAME_W32_WINDOW (f), SW_SHOWNOACTIVATE);
5963 /* Draw into the window. */
5964 w->must_be_updated_p = 1;
5965 update_single_window (w, 1);
5967 UNBLOCK_INPUT;
5969 /* Restore original current buffer. */
5970 set_buffer_internal_1 (old_buffer);
5971 windows_or_buffers_changed = old_windows_or_buffers_changed;
5973 start_timer:
5974 /* Let the tip disappear after timeout seconds. */
5975 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
5976 intern ("x-hide-tip"));
5978 UNGCPRO;
5979 return unbind_to (count, Qnil);
5983 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
5984 doc: /* Hide the current tooltip window, if there is any.
5985 Value is t if tooltip was open, nil otherwise. */)
5988 int count;
5989 Lisp_Object deleted, frame, timer;
5990 struct gcpro gcpro1, gcpro2;
5992 /* Return quickly if nothing to do. */
5993 if (NILP (tip_timer) && NILP (tip_frame))
5994 return Qnil;
5996 frame = tip_frame;
5997 timer = tip_timer;
5998 GCPRO2 (frame, timer);
5999 tip_frame = tip_timer = deleted = Qnil;
6001 count = SPECPDL_INDEX ();
6002 specbind (Qinhibit_redisplay, Qt);
6003 specbind (Qinhibit_quit, Qt);
6005 if (!NILP (timer))
6006 call1 (Qcancel_timer, timer);
6008 if (FRAMEP (frame))
6010 delete_frame (frame, Qnil);
6011 deleted = Qt;
6014 UNGCPRO;
6015 return unbind_to (count, deleted);
6020 /***********************************************************************
6021 File selection dialog
6022 ***********************************************************************/
6023 extern Lisp_Object Qfile_name_history;
6025 /* Callback for altering the behavior of the Open File dialog.
6026 Makes the Filename text field contain "Current Directory" and be
6027 read-only when "Directories" is selected in the filter. This
6028 allows us to work around the fact that the standard Open File
6029 dialog does not support directories. */
6030 UINT CALLBACK
6031 file_dialog_callback (hwnd, msg, wParam, lParam)
6032 HWND hwnd;
6033 UINT msg;
6034 WPARAM wParam;
6035 LPARAM lParam;
6037 if (msg == WM_NOTIFY)
6039 OFNOTIFY * notify = (OFNOTIFY *)lParam;
6040 /* Detect when the Filter dropdown is changed. */
6041 if (notify->hdr.code == CDN_TYPECHANGE
6042 || notify->hdr.code == CDN_INITDONE)
6044 HWND dialog = GetParent (hwnd);
6045 HWND edit_control = GetDlgItem (dialog, FILE_NAME_TEXT_FIELD);
6047 /* Directories is in index 2. */
6048 if (notify->lpOFN->nFilterIndex == 2)
6050 CommDlg_OpenSave_SetControlText (dialog, FILE_NAME_TEXT_FIELD,
6051 "Current Directory");
6052 EnableWindow (edit_control, FALSE);
6054 else
6056 /* Don't override default filename on init done. */
6057 if (notify->hdr.code == CDN_TYPECHANGE)
6058 CommDlg_OpenSave_SetControlText (dialog,
6059 FILE_NAME_TEXT_FIELD, "");
6060 EnableWindow (edit_control, TRUE);
6064 return 0;
6067 /* Since we compile with _WIN32_WINNT set to 0x0400 (for NT4 compatibility)
6068 we end up with the old file dialogs. Define a big enough struct for the
6069 new dialog to trick GetOpenFileName into giving us the new dialogs on
6070 Windows 2000 and XP. */
6071 typedef struct
6073 OPENFILENAME real_details;
6074 void * pReserved;
6075 DWORD dwReserved;
6076 DWORD FlagsEx;
6077 } NEWOPENFILENAME;
6080 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
6081 doc: /* Read file name, prompting with PROMPT in directory DIR.
6082 Use a file selection dialog.
6083 Select DEFAULT-FILENAME in the dialog's file selection box, if
6084 specified. Ensure that file exists if MUSTMATCH is non-nil.
6085 If ONLY-DIR-P is non-nil, the user can only select directories. */)
6086 (prompt, dir, default_filename, mustmatch, only_dir_p)
6087 Lisp_Object prompt, dir, default_filename, mustmatch, only_dir_p;
6089 struct frame *f = SELECTED_FRAME ();
6090 Lisp_Object file = Qnil;
6091 int count = SPECPDL_INDEX ();
6092 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
6093 char filename[MAX_PATH + 1];
6094 char init_dir[MAX_PATH + 1];
6095 int default_filter_index = 1; /* 1: All Files, 2: Directories only */
6097 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
6098 CHECK_STRING (prompt);
6099 CHECK_STRING (dir);
6101 /* Create the dialog with PROMPT as title, using DIR as initial
6102 directory and using "*" as pattern. */
6103 dir = Fexpand_file_name (dir, Qnil);
6104 strncpy (init_dir, SDATA (ENCODE_FILE (dir)), MAX_PATH);
6105 init_dir[MAX_PATH] = '\0';
6106 unixtodos_filename (init_dir);
6108 if (STRINGP (default_filename))
6110 char *file_name_only;
6111 char *full_path_name = SDATA (ENCODE_FILE (default_filename));
6113 unixtodos_filename (full_path_name);
6115 file_name_only = strrchr (full_path_name, '\\');
6116 if (!file_name_only)
6117 file_name_only = full_path_name;
6118 else
6119 file_name_only++;
6121 strncpy (filename, file_name_only, MAX_PATH);
6122 filename[MAX_PATH] = '\0';
6124 else
6125 filename[0] = '\0';
6128 NEWOPENFILENAME new_file_details;
6129 BOOL file_opened = FALSE;
6130 OPENFILENAME * file_details = &new_file_details.real_details;
6132 /* Prevent redisplay. */
6133 specbind (Qinhibit_redisplay, Qt);
6134 BLOCK_INPUT;
6136 bzero (&new_file_details, sizeof (new_file_details));
6137 /* Apparently NT4 crashes if you give it an unexpected size.
6138 I'm not sure about Windows 9x, so play it safe. */
6139 if (w32_major_version > 4 && w32_major_version < 95)
6140 file_details->lStructSize = sizeof (NEWOPENFILENAME);
6141 else
6142 file_details->lStructSize = sizeof (OPENFILENAME);
6144 file_details->hwndOwner = FRAME_W32_WINDOW (f);
6145 /* Undocumented Bug in Common File Dialog:
6146 If a filter is not specified, shell links are not resolved. */
6147 file_details->lpstrFilter = "All Files (*.*)\0*.*\0Directories\0*|*\0\0";
6148 file_details->lpstrFile = filename;
6149 file_details->nMaxFile = sizeof (filename);
6150 file_details->lpstrInitialDir = init_dir;
6151 file_details->lpstrTitle = SDATA (prompt);
6153 if (! NILP (only_dir_p))
6154 default_filter_index = 2;
6156 file_details->nFilterIndex = default_filter_index;
6158 file_details->Flags = (OFN_HIDEREADONLY | OFN_NOCHANGEDIR
6159 | OFN_EXPLORER | OFN_ENABLEHOOK);
6160 if (!NILP (mustmatch))
6162 /* Require that the path to the parent directory exists. */
6163 file_details->Flags |= OFN_PATHMUSTEXIST;
6164 /* If we are looking for a file, require that it exists. */
6165 if (NILP (only_dir_p))
6166 file_details->Flags |= OFN_FILEMUSTEXIST;
6169 file_details->lpfnHook = (LPOFNHOOKPROC) file_dialog_callback;
6171 file_opened = GetOpenFileName (file_details);
6173 UNBLOCK_INPUT;
6175 if (file_opened)
6177 dostounix_filename (filename);
6179 if (file_details->nFilterIndex == 2)
6181 /* "Directories" selected - strip dummy file name. */
6182 char * last = strrchr (filename, '/');
6183 *last = '\0';
6186 file = DECODE_FILE (build_string (filename));
6188 /* User cancelled the dialog without making a selection. */
6189 else if (!CommDlgExtendedError ())
6190 file = Qnil;
6191 /* An error occurred, fallback on reading from the mini-buffer. */
6192 else
6193 file = Fcompleting_read (prompt, intern ("read-file-name-internal"),
6194 dir, mustmatch, dir, Qfile_name_history,
6195 default_filename, Qnil);
6197 file = unbind_to (count, file);
6200 UNGCPRO;
6202 /* Make "Cancel" equivalent to C-g. */
6203 if (NILP (file))
6204 Fsignal (Qquit, Qnil);
6206 return unbind_to (count, file);
6210 /* Moving files to the system recycle bin.
6211 Used by `move-file-to-trash' instead of the default moving to ~/.Trash */
6212 DEFUN ("system-move-file-to-trash", Fsystem_move_file_to_trash,
6213 Ssystem_move_file_to_trash, 1, 1, 0,
6214 doc: /* Move file or directory named FILENAME to the recycle bin. */)
6215 (filename)
6216 Lisp_Object filename;
6218 Lisp_Object handler;
6219 Lisp_Object encoded_file;
6220 Lisp_Object operation;
6222 operation = Qdelete_file;
6223 if (!NILP (Ffile_directory_p (filename))
6224 && NILP (Ffile_symlink_p (filename)))
6226 operation = intern ("delete-directory");
6227 filename = Fdirectory_file_name (filename);
6229 filename = Fexpand_file_name (filename, Qnil);
6231 handler = Ffind_file_name_handler (filename, operation);
6232 if (!NILP (handler))
6233 return call2 (handler, operation, filename);
6235 encoded_file = ENCODE_FILE (filename);
6238 const char * path;
6239 SHFILEOPSTRUCT file_op;
6240 char tmp_path[MAX_PATH + 1];
6242 path = map_w32_filename (SDATA (encoded_file), NULL);
6244 /* On Windows, write permission is required to delete/move files. */
6245 _chmod (path, 0666);
6247 bzero (tmp_path, sizeof (tmp_path));
6248 strcpy (tmp_path, path);
6250 bzero (&file_op, sizeof (file_op));
6251 file_op.hwnd = HWND_DESKTOP;
6252 file_op.wFunc = FO_DELETE;
6253 file_op.pFrom = tmp_path;
6254 file_op.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_ALLOWUNDO
6255 | FOF_NOERRORUI | FOF_NO_CONNECTED_ELEMENTS;
6256 file_op.fAnyOperationsAborted = FALSE;
6258 if (SHFileOperation (&file_op) != 0)
6259 report_file_error ("Removing old name", list1 (filename));
6261 return Qnil;
6265 /***********************************************************************
6266 w32 specialized functions
6267 ***********************************************************************/
6269 DEFUN ("w32-send-sys-command", Fw32_send_sys_command,
6270 Sw32_send_sys_command, 1, 2, 0,
6271 doc: /* Send frame a Windows WM_SYSCOMMAND message of type COMMAND.
6272 Some useful values for COMMAND are #xf030 to maximize frame (#xf020
6273 to minimize), #xf120 to restore frame to original size, and #xf100
6274 to activate the menubar for keyboard access. #xf140 activates the
6275 screen saver if defined.
6277 If optional parameter FRAME is not specified, use selected frame. */)
6278 (command, frame)
6279 Lisp_Object command, frame;
6281 FRAME_PTR f = check_x_frame (frame);
6283 CHECK_NUMBER (command);
6285 PostMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, XINT (command), 0);
6287 return Qnil;
6290 DEFUN ("w32-shell-execute", Fw32_shell_execute, Sw32_shell_execute, 2, 4, 0,
6291 doc: /* Get Windows to perform OPERATION on DOCUMENT.
6292 This is a wrapper around the ShellExecute system function, which
6293 invokes the application registered to handle OPERATION for DOCUMENT.
6295 OPERATION is either nil or a string that names a supported operation.
6296 What operations can be used depends on the particular DOCUMENT and its
6297 handler application, but typically it is one of the following common
6298 operations:
6300 \"open\" - open DOCUMENT, which could be a file, a directory, or an
6301 executable program. If it is an application, that
6302 application is launched in the current buffer's default
6303 directory. Otherwise, the application associated with
6304 DOCUMENT is launched in the buffer's default directory.
6305 \"print\" - print DOCUMENT, which must be a file
6306 \"explore\" - start the Windows Explorer on DOCUMENT
6307 \"edit\" - launch an editor and open DOCUMENT for editing; which
6308 editor is launched depends on the association for the
6309 specified DOCUMENT
6310 \"find\" - initiate search starting from DOCUMENT which must specify
6311 a directory
6312 nil - invoke the default OPERATION, or \"open\" if default is
6313 not defined or unavailable
6315 DOCUMENT is typically the name of a document file or a URL, but can
6316 also be a program executable to run, or a directory to open in the
6317 Windows Explorer.
6319 If DOCUMENT is a program executable, the optional third arg PARAMETERS
6320 can be a string containing command line parameters that will be passed
6321 to the program; otherwise, PARAMETERS should be nil or unspecified.
6323 Optional fourth argument SHOW-FLAG can be used to control how the
6324 application will be displayed when it is invoked. If SHOW-FLAG is nil
6325 or unspecified, the application is displayed normally, otherwise it is
6326 an integer representing a ShowWindow flag:
6328 0 - start hidden
6329 1 - start normally
6330 3 - start maximized
6331 6 - start minimized */)
6332 (operation, document, parameters, show_flag)
6333 Lisp_Object operation, document, parameters, show_flag;
6335 Lisp_Object current_dir;
6337 CHECK_STRING (document);
6339 /* Encode filename, current directory and parameters. */
6340 current_dir = ENCODE_FILE (current_buffer->directory);
6341 document = ENCODE_FILE (document);
6342 if (STRINGP (parameters))
6343 parameters = ENCODE_SYSTEM (parameters);
6345 if ((int) ShellExecute (NULL,
6346 (STRINGP (operation) ?
6347 SDATA (operation) : NULL),
6348 SDATA (document),
6349 (STRINGP (parameters) ?
6350 SDATA (parameters) : NULL),
6351 SDATA (current_dir),
6352 (INTEGERP (show_flag) ?
6353 XINT (show_flag) : SW_SHOWDEFAULT))
6354 > 32)
6355 return Qt;
6356 error ("ShellExecute failed: %s", w32_strerror (0));
6359 /* Lookup virtual keycode from string representing the name of a
6360 non-ascii keystroke into the corresponding virtual key, using
6361 lispy_function_keys. */
6362 static int
6363 lookup_vk_code (char *key)
6365 int i;
6367 for (i = 0; i < 256; i++)
6368 if (lispy_function_keys[i]
6369 && strcmp (lispy_function_keys[i], key) == 0)
6370 return i;
6372 return -1;
6375 /* Convert a one-element vector style key sequence to a hot key
6376 definition. */
6377 static Lisp_Object
6378 w32_parse_hot_key (key)
6379 Lisp_Object key;
6381 /* Copied from Fdefine_key and store_in_keymap. */
6382 register Lisp_Object c;
6383 int vk_code;
6384 int lisp_modifiers;
6385 int w32_modifiers;
6386 struct gcpro gcpro1;
6388 CHECK_VECTOR (key);
6390 if (XFASTINT (Flength (key)) != 1)
6391 return Qnil;
6393 GCPRO1 (key);
6395 c = Faref (key, make_number (0));
6397 if (CONSP (c) && lucid_event_type_list_p (c))
6398 c = Fevent_convert_list (c);
6400 UNGCPRO;
6402 if (! INTEGERP (c) && ! SYMBOLP (c))
6403 error ("Key definition is invalid");
6405 /* Work out the base key and the modifiers. */
6406 if (SYMBOLP (c))
6408 c = parse_modifiers (c);
6409 lisp_modifiers = XINT (Fcar (Fcdr (c)));
6410 c = Fcar (c);
6411 if (!SYMBOLP (c))
6412 abort ();
6413 vk_code = lookup_vk_code (SDATA (SYMBOL_NAME (c)));
6415 else if (INTEGERP (c))
6417 lisp_modifiers = XINT (c) & ~CHARACTERBITS;
6418 /* Many ascii characters are their own virtual key code. */
6419 vk_code = XINT (c) & CHARACTERBITS;
6422 if (vk_code < 0 || vk_code > 255)
6423 return Qnil;
6425 if ((lisp_modifiers & meta_modifier) != 0
6426 && !NILP (Vw32_alt_is_meta))
6427 lisp_modifiers |= alt_modifier;
6429 /* Supply defs missing from mingw32. */
6430 #ifndef MOD_ALT
6431 #define MOD_ALT 0x0001
6432 #define MOD_CONTROL 0x0002
6433 #define MOD_SHIFT 0x0004
6434 #define MOD_WIN 0x0008
6435 #endif
6437 /* Convert lisp modifiers to Windows hot-key form. */
6438 w32_modifiers = (lisp_modifiers & hyper_modifier) ? MOD_WIN : 0;
6439 w32_modifiers |= (lisp_modifiers & alt_modifier) ? MOD_ALT : 0;
6440 w32_modifiers |= (lisp_modifiers & ctrl_modifier) ? MOD_CONTROL : 0;
6441 w32_modifiers |= (lisp_modifiers & shift_modifier) ? MOD_SHIFT : 0;
6443 return HOTKEY (vk_code, w32_modifiers);
6446 DEFUN ("w32-register-hot-key", Fw32_register_hot_key,
6447 Sw32_register_hot_key, 1, 1, 0,
6448 doc: /* Register KEY as a hot-key combination.
6449 Certain key combinations like Alt-Tab are reserved for system use on
6450 Windows, and therefore are normally intercepted by the system. However,
6451 most of these key combinations can be received by registering them as
6452 hot-keys, overriding their special meaning.
6454 KEY must be a one element key definition in vector form that would be
6455 acceptable to `define-key' (e.g. [A-tab] for Alt-Tab). The meta
6456 modifier is interpreted as Alt if `w32-alt-is-meta' is t, and hyper
6457 is always interpreted as the Windows modifier keys.
6459 The return value is the hotkey-id if registered, otherwise nil. */)
6460 (key)
6461 Lisp_Object key;
6463 key = w32_parse_hot_key (key);
6465 if (!NILP (key) && NILP (Fmemq (key, w32_grabbed_keys)))
6467 /* Reuse an empty slot if possible. */
6468 Lisp_Object item = Fmemq (Qnil, w32_grabbed_keys);
6470 /* Safe to add new key to list, even if we have focus. */
6471 if (NILP (item))
6472 w32_grabbed_keys = Fcons (key, w32_grabbed_keys);
6473 else
6474 XSETCAR (item, key);
6476 /* Notify input thread about new hot-key definition, so that it
6477 takes effect without needing to switch focus. */
6478 #ifdef USE_LISP_UNION_TYPE
6479 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
6480 (WPARAM) key.i, 0);
6481 #else
6482 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
6483 (WPARAM) key, 0);
6484 #endif
6487 return key;
6490 DEFUN ("w32-unregister-hot-key", Fw32_unregister_hot_key,
6491 Sw32_unregister_hot_key, 1, 1, 0,
6492 doc: /* Unregister KEY as a hot-key combination. */)
6493 (key)
6494 Lisp_Object key;
6496 Lisp_Object item;
6498 if (!INTEGERP (key))
6499 key = w32_parse_hot_key (key);
6501 item = Fmemq (key, w32_grabbed_keys);
6503 if (!NILP (item))
6505 /* Notify input thread about hot-key definition being removed, so
6506 that it takes effect without needing focus switch. */
6507 #ifdef USE_LISP_UNION_TYPE
6508 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
6509 (WPARAM) XINT (XCAR (item)), (LPARAM) item.i))
6510 #else
6511 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
6512 (WPARAM) XINT (XCAR (item)), (LPARAM) item))
6513 #endif
6515 MSG msg;
6516 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
6518 return Qt;
6520 return Qnil;
6523 DEFUN ("w32-registered-hot-keys", Fw32_registered_hot_keys,
6524 Sw32_registered_hot_keys, 0, 0, 0,
6525 doc: /* Return list of registered hot-key IDs. */)
6528 return Fdelq (Qnil, Fcopy_sequence (w32_grabbed_keys));
6531 DEFUN ("w32-reconstruct-hot-key", Fw32_reconstruct_hot_key,
6532 Sw32_reconstruct_hot_key, 1, 1, 0,
6533 doc: /* Convert hot-key ID to a lisp key combination.
6534 usage: (w32-reconstruct-hot-key ID) */)
6535 (hotkeyid)
6536 Lisp_Object hotkeyid;
6538 int vk_code, w32_modifiers;
6539 Lisp_Object key;
6541 CHECK_NUMBER (hotkeyid);
6543 vk_code = HOTKEY_VK_CODE (hotkeyid);
6544 w32_modifiers = HOTKEY_MODIFIERS (hotkeyid);
6546 if (vk_code < 256 && lispy_function_keys[vk_code])
6547 key = intern (lispy_function_keys[vk_code]);
6548 else
6549 key = make_number (vk_code);
6551 key = Fcons (key, Qnil);
6552 if (w32_modifiers & MOD_SHIFT)
6553 key = Fcons (Qshift, key);
6554 if (w32_modifiers & MOD_CONTROL)
6555 key = Fcons (Qctrl, key);
6556 if (w32_modifiers & MOD_ALT)
6557 key = Fcons (NILP (Vw32_alt_is_meta) ? Qalt : Qmeta, key);
6558 if (w32_modifiers & MOD_WIN)
6559 key = Fcons (Qhyper, key);
6561 return key;
6564 DEFUN ("w32-toggle-lock-key", Fw32_toggle_lock_key,
6565 Sw32_toggle_lock_key, 1, 2, 0,
6566 doc: /* Toggle the state of the lock key KEY.
6567 KEY can be `capslock', `kp-numlock', or `scroll'.
6568 If the optional parameter NEW-STATE is a number, then the state of KEY
6569 is set to off if the low bit of NEW-STATE is zero, otherwise on. */)
6570 (key, new_state)
6571 Lisp_Object key, new_state;
6573 int vk_code;
6575 if (EQ (key, intern ("capslock")))
6576 vk_code = VK_CAPITAL;
6577 else if (EQ (key, intern ("kp-numlock")))
6578 vk_code = VK_NUMLOCK;
6579 else if (EQ (key, intern ("scroll")))
6580 vk_code = VK_SCROLL;
6581 else
6582 return Qnil;
6584 if (!dwWindowsThreadId)
6585 return make_number (w32_console_toggle_lock_key (vk_code, new_state));
6587 #ifdef USE_LISP_UNION_TYPE
6588 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
6589 (WPARAM) vk_code, (LPARAM) new_state.i))
6590 #else
6591 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
6592 (WPARAM) vk_code, (LPARAM) new_state))
6593 #endif
6595 MSG msg;
6596 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
6597 return make_number (msg.wParam);
6599 return Qnil;
6602 DEFUN ("w32-window-exists-p", Fw32_window_exists_p, Sw32_window_exists_p,
6603 2, 2, 0,
6604 doc: /* Return non-nil if a window exists with the specified CLASS and NAME.
6606 This is a direct interface to the Windows API FindWindow function. */)
6607 (class, name)
6608 Lisp_Object class, name;
6610 HWND hnd;
6612 if (!NILP (class))
6613 CHECK_STRING (class);
6614 if (!NILP (name))
6615 CHECK_STRING (name);
6617 hnd = FindWindow (STRINGP (class) ? ((LPCTSTR) SDATA (class)) : NULL,
6618 STRINGP (name) ? ((LPCTSTR) SDATA (name)) : NULL);
6619 if (!hnd)
6620 return Qnil;
6621 return Qt;
6624 DEFUN ("w32-battery-status", Fw32_battery_status, Sw32_battery_status, 0, 0, 0,
6625 doc: /* Get power status information from Windows system.
6627 The following %-sequences are provided:
6628 %L AC line status (verbose)
6629 %B Battery status (verbose)
6630 %b Battery status, empty means high, `-' means low,
6631 `!' means critical, and `+' means charging
6632 %p Battery load percentage
6633 %s Remaining time (to charge or discharge) in seconds
6634 %m Remaining time (to charge or discharge) in minutes
6635 %h Remaining time (to charge or discharge) in hours
6636 %t Remaining time (to charge or discharge) in the form `h:min' */)
6639 Lisp_Object status = Qnil;
6641 SYSTEM_POWER_STATUS system_status;
6642 if (GetSystemPowerStatus (&system_status))
6644 Lisp_Object line_status, battery_status, battery_status_symbol;
6645 Lisp_Object load_percentage, seconds, minutes, hours, remain;
6646 Lisp_Object sequences[8];
6648 long seconds_left = (long) system_status.BatteryLifeTime;
6650 if (system_status.ACLineStatus == 0)
6651 line_status = build_string ("off-line");
6652 else if (system_status.ACLineStatus == 1)
6653 line_status = build_string ("on-line");
6654 else
6655 line_status = build_string ("N/A");
6657 if (system_status.BatteryFlag & 128)
6659 battery_status = build_string ("N/A");
6660 battery_status_symbol = empty_unibyte_string;
6662 else if (system_status.BatteryFlag & 8)
6664 battery_status = build_string ("charging");
6665 battery_status_symbol = build_string ("+");
6666 if (system_status.BatteryFullLifeTime != -1L)
6667 seconds_left = system_status.BatteryFullLifeTime - seconds_left;
6669 else if (system_status.BatteryFlag & 4)
6671 battery_status = build_string ("critical");
6672 battery_status_symbol = build_string ("!");
6674 else if (system_status.BatteryFlag & 2)
6676 battery_status = build_string ("low");
6677 battery_status_symbol = build_string ("-");
6679 else if (system_status.BatteryFlag & 1)
6681 battery_status = build_string ("high");
6682 battery_status_symbol = empty_unibyte_string;
6684 else
6686 battery_status = build_string ("medium");
6687 battery_status_symbol = empty_unibyte_string;
6690 if (system_status.BatteryLifePercent > 100)
6691 load_percentage = build_string ("N/A");
6692 else
6694 char buffer[16];
6695 _snprintf (buffer, 16, "%d", system_status.BatteryLifePercent);
6696 load_percentage = build_string (buffer);
6699 if (seconds_left < 0)
6700 seconds = minutes = hours = remain = build_string ("N/A");
6701 else
6703 long m;
6704 float h;
6705 char buffer[16];
6706 _snprintf (buffer, 16, "%ld", seconds_left);
6707 seconds = build_string (buffer);
6709 m = seconds_left / 60;
6710 _snprintf (buffer, 16, "%ld", m);
6711 minutes = build_string (buffer);
6713 h = seconds_left / 3600.0;
6714 _snprintf (buffer, 16, "%3.1f", h);
6715 hours = build_string (buffer);
6717 _snprintf (buffer, 16, "%ld:%02ld", m / 60, m % 60);
6718 remain = build_string (buffer);
6720 sequences[0] = Fcons (make_number ('L'), line_status);
6721 sequences[1] = Fcons (make_number ('B'), battery_status);
6722 sequences[2] = Fcons (make_number ('b'), battery_status_symbol);
6723 sequences[3] = Fcons (make_number ('p'), load_percentage);
6724 sequences[4] = Fcons (make_number ('s'), seconds);
6725 sequences[5] = Fcons (make_number ('m'), minutes);
6726 sequences[6] = Fcons (make_number ('h'), hours);
6727 sequences[7] = Fcons (make_number ('t'), remain);
6729 status = Flist (8, sequences);
6731 return status;
6735 DEFUN ("file-system-info", Ffile_system_info, Sfile_system_info, 1, 1, 0,
6736 doc: /* Return storage information about the file system FILENAME is on.
6737 Value is a list of floats (TOTAL FREE AVAIL), where TOTAL is the total
6738 storage of the file system, FREE is the free storage, and AVAIL is the
6739 storage available to a non-superuser. All 3 numbers are in bytes.
6740 If the underlying system call fails, value is nil. */)
6741 (filename)
6742 Lisp_Object filename;
6744 Lisp_Object encoded, value;
6746 CHECK_STRING (filename);
6747 filename = Fexpand_file_name (filename, Qnil);
6748 encoded = ENCODE_FILE (filename);
6750 value = Qnil;
6752 /* Determining the required information on Windows turns out, sadly,
6753 to be more involved than one would hope. The original Win32 api
6754 call for this will return bogus information on some systems, but we
6755 must dynamically probe for the replacement api, since that was
6756 added rather late on. */
6758 HMODULE hKernel = GetModuleHandle ("kernel32");
6759 BOOL (*pfn_GetDiskFreeSpaceEx)
6760 (char *, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER)
6761 = (void *) GetProcAddress (hKernel, "GetDiskFreeSpaceEx");
6763 /* On Windows, we may need to specify the root directory of the
6764 volume holding FILENAME. */
6765 char rootname[MAX_PATH];
6766 char *name = SDATA (encoded);
6768 /* find the root name of the volume if given */
6769 if (isalpha (name[0]) && name[1] == ':')
6771 rootname[0] = name[0];
6772 rootname[1] = name[1];
6773 rootname[2] = '\\';
6774 rootname[3] = 0;
6776 else if (IS_DIRECTORY_SEP (name[0]) && IS_DIRECTORY_SEP (name[1]))
6778 char *str = rootname;
6779 int slashes = 4;
6782 if (IS_DIRECTORY_SEP (*name) && --slashes == 0)
6783 break;
6784 *str++ = *name++;
6786 while ( *name );
6788 *str++ = '\\';
6789 *str = 0;
6792 if (pfn_GetDiskFreeSpaceEx)
6794 /* Unsigned large integers cannot be cast to double, so
6795 use signed ones instead. */
6796 LARGE_INTEGER availbytes;
6797 LARGE_INTEGER freebytes;
6798 LARGE_INTEGER totalbytes;
6800 if (pfn_GetDiskFreeSpaceEx (rootname,
6801 (ULARGE_INTEGER *)&availbytes,
6802 (ULARGE_INTEGER *)&totalbytes,
6803 (ULARGE_INTEGER *)&freebytes))
6804 value = list3 (make_float ((double) totalbytes.QuadPart),
6805 make_float ((double) freebytes.QuadPart),
6806 make_float ((double) availbytes.QuadPart));
6808 else
6810 DWORD sectors_per_cluster;
6811 DWORD bytes_per_sector;
6812 DWORD free_clusters;
6813 DWORD total_clusters;
6815 if (GetDiskFreeSpace (rootname,
6816 &sectors_per_cluster,
6817 &bytes_per_sector,
6818 &free_clusters,
6819 &total_clusters))
6820 value = list3 (make_float ((double) total_clusters
6821 * sectors_per_cluster * bytes_per_sector),
6822 make_float ((double) free_clusters
6823 * sectors_per_cluster * bytes_per_sector),
6824 make_float ((double) free_clusters
6825 * sectors_per_cluster * bytes_per_sector));
6829 return value;
6832 DEFUN ("default-printer-name", Fdefault_printer_name, Sdefault_printer_name,
6833 0, 0, 0, doc: /* Return the name of Windows default printer device. */)
6836 static char pname_buf[256];
6837 int err;
6838 HANDLE hPrn;
6839 PRINTER_INFO_2 *ppi2 = NULL;
6840 DWORD dwNeeded = 0, dwReturned = 0;
6842 /* Retrieve the default string from Win.ini (the registry).
6843 * String will be in form "printername,drivername,portname".
6844 * This is the most portable way to get the default printer. */
6845 if (GetProfileString ("windows", "device", ",,", pname_buf, sizeof (pname_buf)) <= 0)
6846 return Qnil;
6847 /* printername precedes first "," character */
6848 strtok (pname_buf, ",");
6849 /* We want to know more than the printer name */
6850 if (!OpenPrinter (pname_buf, &hPrn, NULL))
6851 return Qnil;
6852 GetPrinter (hPrn, 2, NULL, 0, &dwNeeded);
6853 if (dwNeeded == 0)
6855 ClosePrinter (hPrn);
6856 return Qnil;
6858 /* Allocate memory for the PRINTER_INFO_2 struct */
6859 ppi2 = (PRINTER_INFO_2 *) xmalloc (dwNeeded);
6860 if (!ppi2)
6862 ClosePrinter (hPrn);
6863 return Qnil;
6865 /* Call GetPrinter again with big enouth memory block */
6866 err = GetPrinter (hPrn, 2, (LPBYTE)ppi2, dwNeeded, &dwReturned);
6867 ClosePrinter (hPrn);
6868 if (!err)
6870 xfree (ppi2);
6871 return Qnil;
6874 if (ppi2)
6876 if (ppi2->Attributes & PRINTER_ATTRIBUTE_SHARED && ppi2->pServerName)
6878 /* a remote printer */
6879 if (*ppi2->pServerName == '\\')
6880 _snprintf (pname_buf, sizeof (pname_buf), "%s\\%s", ppi2->pServerName,
6881 ppi2->pShareName);
6882 else
6883 _snprintf (pname_buf, sizeof (pname_buf), "\\\\%s\\%s", ppi2->pServerName,
6884 ppi2->pShareName);
6885 pname_buf[sizeof (pname_buf) - 1] = '\0';
6887 else
6889 /* a local printer */
6890 strncpy (pname_buf, ppi2->pPortName, sizeof (pname_buf));
6891 pname_buf[sizeof (pname_buf) - 1] = '\0';
6892 /* `pPortName' can include several ports, delimited by ','.
6893 * we only use the first one. */
6894 strtok (pname_buf, ",");
6896 xfree (ppi2);
6899 return build_string (pname_buf);
6902 /***********************************************************************
6903 Initialization
6904 ***********************************************************************/
6906 /* Keep this list in the same order as frame_parms in frame.c.
6907 Use 0 for unsupported frame parameters. */
6909 frame_parm_handler w32_frame_parm_handlers[] =
6911 x_set_autoraise,
6912 x_set_autolower,
6913 x_set_background_color,
6914 x_set_border_color,
6915 x_set_border_width,
6916 x_set_cursor_color,
6917 x_set_cursor_type,
6918 x_set_font,
6919 x_set_foreground_color,
6920 x_set_icon_name,
6921 x_set_icon_type,
6922 x_set_internal_border_width,
6923 x_set_menu_bar_lines,
6924 x_set_mouse_color,
6925 x_explicitly_set_name,
6926 x_set_scroll_bar_width,
6927 x_set_title,
6928 x_set_unsplittable,
6929 x_set_vertical_scroll_bars,
6930 x_set_visibility,
6931 x_set_tool_bar_lines,
6932 0, /* x_set_scroll_bar_foreground, */
6933 0, /* x_set_scroll_bar_background, */
6934 x_set_screen_gamma,
6935 x_set_line_spacing,
6936 x_set_fringe_width,
6937 x_set_fringe_width,
6938 0, /* x_set_wait_for_wm, */
6939 x_set_fullscreen,
6940 x_set_font_backend,
6941 x_set_alpha,
6942 0, /* x_set_sticky */
6945 void
6946 syms_of_w32fns ()
6948 globals_of_w32fns ();
6949 /* This is zero if not using MS-Windows. */
6950 w32_in_use = 0;
6951 track_mouse_window = NULL;
6953 w32_visible_system_caret_hwnd = NULL;
6955 DEFSYM (Qnone, "none");
6956 DEFSYM (Qsuppress_icon, "suppress-icon");
6957 DEFSYM (Qundefined_color, "undefined-color");
6958 DEFSYM (Qcancel_timer, "cancel-timer");
6959 DEFSYM (Qhyper, "hyper");
6960 DEFSYM (Qsuper, "super");
6961 DEFSYM (Qmeta, "meta");
6962 DEFSYM (Qalt, "alt");
6963 DEFSYM (Qctrl, "ctrl");
6964 DEFSYM (Qcontrol, "control");
6965 DEFSYM (Qshift, "shift");
6966 DEFSYM (Qfont_param, "font-parameter");
6967 /* This is the end of symbol initialization. */
6969 /* Text property `display' should be nonsticky by default. */
6970 Vtext_property_default_nonsticky
6971 = Fcons (Fcons (Qdisplay, Qt), Vtext_property_default_nonsticky);
6974 Fput (Qundefined_color, Qerror_conditions,
6975 pure_cons (Qundefined_color, pure_cons (Qerror, Qnil)));
6976 Fput (Qundefined_color, Qerror_message,
6977 make_pure_c_string ("Undefined color"));
6979 staticpro (&w32_grabbed_keys);
6980 w32_grabbed_keys = Qnil;
6982 DEFVAR_LISP ("w32-color-map", &Vw32_color_map,
6983 doc: /* An array of color name mappings for Windows. */);
6984 Vw32_color_map = Qnil;
6986 DEFVAR_LISP ("w32-pass-alt-to-system", &Vw32_pass_alt_to_system,
6987 doc: /* Non-nil if Alt key presses are passed on to Windows.
6988 When non-nil, for example, Alt pressed and released and then space will
6989 open the System menu. When nil, Emacs processes the Alt key events, and
6990 then silently swallows them. */);
6991 Vw32_pass_alt_to_system = Qnil;
6993 DEFVAR_LISP ("w32-alt-is-meta", &Vw32_alt_is_meta,
6994 doc: /* Non-nil if the Alt key is to be considered the same as the META key.
6995 When nil, Emacs will translate the Alt key to the ALT modifier, not to META. */);
6996 Vw32_alt_is_meta = Qt;
6998 DEFVAR_INT ("w32-quit-key", &w32_quit_key,
6999 doc: /* If non-zero, the virtual key code for an alternative quit key. */);
7000 w32_quit_key = 0;
7002 DEFVAR_LISP ("w32-pass-lwindow-to-system",
7003 &Vw32_pass_lwindow_to_system,
7004 doc: /* If non-nil, the left \"Windows\" key is passed on to Windows.
7006 When non-nil, the Start menu is opened by tapping the key.
7007 If you set this to nil, the left \"Windows\" key is processed by Emacs
7008 according to the value of `w32-lwindow-modifier', which see.
7010 Note that some combinations of the left \"Windows\" key with other keys are
7011 caught by Windows at low level, and so binding them in Emacs will have no
7012 effect. For example, <lwindow>-r always pops up the Windows Run dialog,
7013 <lwindow>-<Pause> pops up the "System Properties" dialog, etc. However, see
7014 the doc string of `w32-phantom-key-code'. */);
7015 Vw32_pass_lwindow_to_system = Qt;
7017 DEFVAR_LISP ("w32-pass-rwindow-to-system",
7018 &Vw32_pass_rwindow_to_system,
7019 doc: /* If non-nil, the right \"Windows\" key is passed on to Windows.
7021 When non-nil, the Start menu is opened by tapping the key.
7022 If you set this to nil, the right \"Windows\" key is processed by Emacs
7023 according to the value of `w32-rwindow-modifier', which see.
7025 Note that some combinations of the right \"Windows\" key with other keys are
7026 caught by Windows at low level, and so binding them in Emacs will have no
7027 effect. For example, <rwindow>-r always pops up the Windows Run dialog,
7028 <rwindow>-<Pause> pops up the "System Properties" dialog, etc. However, see
7029 the doc string of `w32-phantom-key-code'. */);
7030 Vw32_pass_rwindow_to_system = Qt;
7032 DEFVAR_LISP ("w32-phantom-key-code",
7033 &Vw32_phantom_key_code,
7034 doc: /* Virtual key code used to generate \"phantom\" key presses.
7035 Value is a number between 0 and 255.
7037 Phantom key presses are generated in order to stop the system from
7038 acting on \"Windows\" key events when `w32-pass-lwindow-to-system' or
7039 `w32-pass-rwindow-to-system' is nil. */);
7040 /* Although 255 is technically not a valid key code, it works and
7041 means that this hack won't interfere with any real key code. */
7042 XSETINT (Vw32_phantom_key_code, 255);
7044 DEFVAR_LISP ("w32-enable-num-lock",
7045 &Vw32_enable_num_lock,
7046 doc: /* If non-nil, the Num Lock key acts normally.
7047 Set to nil to handle Num Lock as the `kp-numlock' key. */);
7048 Vw32_enable_num_lock = Qt;
7050 DEFVAR_LISP ("w32-enable-caps-lock",
7051 &Vw32_enable_caps_lock,
7052 doc: /* If non-nil, the Caps Lock key acts normally.
7053 Set to nil to handle Caps Lock as the `capslock' key. */);
7054 Vw32_enable_caps_lock = Qt;
7056 DEFVAR_LISP ("w32-scroll-lock-modifier",
7057 &Vw32_scroll_lock_modifier,
7058 doc: /* Modifier to use for the Scroll Lock ON state.
7059 The value can be hyper, super, meta, alt, control or shift for the
7060 respective modifier, or nil to handle Scroll Lock as the `scroll' key.
7061 Any other value will cause the Scroll Lock key to be ignored. */);
7062 Vw32_scroll_lock_modifier = Qnil;
7064 DEFVAR_LISP ("w32-lwindow-modifier",
7065 &Vw32_lwindow_modifier,
7066 doc: /* Modifier to use for the left \"Windows\" key.
7067 The value can be hyper, super, meta, alt, control or shift for the
7068 respective modifier, or nil to appear as the `lwindow' key.
7069 Any other value will cause the key to be ignored. */);
7070 Vw32_lwindow_modifier = Qnil;
7072 DEFVAR_LISP ("w32-rwindow-modifier",
7073 &Vw32_rwindow_modifier,
7074 doc: /* Modifier to use for the right \"Windows\" key.
7075 The value can be hyper, super, meta, alt, control or shift for the
7076 respective modifier, or nil to appear as the `rwindow' key.
7077 Any other value will cause the key to be ignored. */);
7078 Vw32_rwindow_modifier = Qnil;
7080 DEFVAR_LISP ("w32-apps-modifier",
7081 &Vw32_apps_modifier,
7082 doc: /* Modifier to use for the \"Apps\" key.
7083 The value can be hyper, super, meta, alt, control or shift for the
7084 respective modifier, or nil to appear as the `apps' key.
7085 Any other value will cause the key to be ignored. */);
7086 Vw32_apps_modifier = Qnil;
7088 DEFVAR_BOOL ("w32-enable-synthesized-fonts", &w32_enable_synthesized_fonts,
7089 doc: /* Non-nil enables selection of artificially italicized and bold fonts. */);
7090 w32_enable_synthesized_fonts = 0;
7092 DEFVAR_LISP ("w32-enable-palette", &Vw32_enable_palette,
7093 doc: /* Non-nil enables Windows palette management to map colors exactly. */);
7094 Vw32_enable_palette = Qt;
7096 DEFVAR_INT ("w32-mouse-button-tolerance",
7097 &w32_mouse_button_tolerance,
7098 doc: /* Analogue of double click interval for faking middle mouse events.
7099 The value is the minimum time in milliseconds that must elapse between
7100 left and right button down events before they are considered distinct events.
7101 If both mouse buttons are depressed within this interval, a middle mouse
7102 button down event is generated instead. */);
7103 w32_mouse_button_tolerance = GetDoubleClickTime () / 2;
7105 DEFVAR_INT ("w32-mouse-move-interval",
7106 &w32_mouse_move_interval,
7107 doc: /* Minimum interval between mouse move events.
7108 The value is the minimum time in milliseconds that must elapse between
7109 successive mouse move (or scroll bar drag) events before they are
7110 reported as lisp events. */);
7111 w32_mouse_move_interval = 0;
7113 DEFVAR_BOOL ("w32-pass-extra-mouse-buttons-to-system",
7114 &w32_pass_extra_mouse_buttons_to_system,
7115 doc: /* If non-nil, the fourth and fifth mouse buttons are passed to Windows.
7116 Recent versions of Windows support mice with up to five buttons.
7117 Since most applications don't support these extra buttons, most mouse
7118 drivers will allow you to map them to functions at the system level.
7119 If this variable is non-nil, Emacs will pass them on, allowing the
7120 system to handle them. */);
7121 w32_pass_extra_mouse_buttons_to_system = 0;
7123 DEFVAR_BOOL ("w32-pass-multimedia-buttons-to-system",
7124 &w32_pass_multimedia_buttons_to_system,
7125 doc: /* If non-nil, media buttons are passed to Windows.
7126 Some modern keyboards contain buttons for controlling media players, web
7127 browsers and other applications. Generally these buttons are handled on a
7128 system wide basis, but by setting this to nil they are made available
7129 to Emacs for binding. Depending on your keyboard, additional keys that
7130 may be available are:
7132 browser-back, browser-forward, browser-refresh, browser-stop,
7133 browser-search, browser-favorites, browser-home,
7134 mail, mail-reply, mail-forward, mail-send,
7135 app-1, app-2,
7136 help, find, new, open, close, save, print, undo, redo, copy, cut, paste,
7137 spell-check, correction-list, toggle-dictate-command,
7138 media-next, media-previous, media-stop, media-play-pause, media-select,
7139 media-play, media-pause, media-record, media-fast-forward, media-rewind,
7140 media-channel-up, media-channel-down,
7141 volume-mute, volume-up, volume-down,
7142 mic-volume-mute, mic-volume-down, mic-volume-up, mic-toggle,
7143 bass-down, bass-boost, bass-up, treble-down, treble-up */);
7144 w32_pass_multimedia_buttons_to_system = 1;
7146 #if 0 /* TODO: Mouse cursor customization. */
7147 DEFVAR_LISP ("x-pointer-shape", &Vx_pointer_shape,
7148 doc: /* The shape of the pointer when over text.
7149 Changing the value does not affect existing frames
7150 unless you set the mouse color. */);
7151 Vx_pointer_shape = Qnil;
7153 Vx_nontext_pointer_shape = Qnil;
7155 Vx_mode_pointer_shape = Qnil;
7157 DEFVAR_LISP ("x-hourglass-pointer-shape", &Vx_hourglass_pointer_shape,
7158 doc: /* The shape of the pointer when Emacs is busy.
7159 This variable takes effect when you create a new frame
7160 or when you set the mouse color. */);
7161 Vx_hourglass_pointer_shape = Qnil;
7163 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
7164 &Vx_sensitive_text_pointer_shape,
7165 doc: /* The shape of the pointer when over mouse-sensitive text.
7166 This variable takes effect when you create a new frame
7167 or when you set the mouse color. */);
7168 Vx_sensitive_text_pointer_shape = Qnil;
7170 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
7171 &Vx_window_horizontal_drag_shape,
7172 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
7173 This variable takes effect when you create a new frame
7174 or when you set the mouse color. */);
7175 Vx_window_horizontal_drag_shape = Qnil;
7176 #endif
7178 DEFVAR_LISP ("x-cursor-fore-pixel", &Vx_cursor_fore_pixel,
7179 doc: /* A string indicating the foreground color of the cursor box. */);
7180 Vx_cursor_fore_pixel = Qnil;
7182 DEFVAR_LISP ("x-max-tooltip-size", &Vx_max_tooltip_size,
7183 doc: /* Maximum size for tooltips.
7184 Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
7185 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
7187 DEFVAR_LISP ("x-no-window-manager", &Vx_no_window_manager,
7188 doc: /* Non-nil if no window manager is in use.
7189 Emacs doesn't try to figure this out; this is always nil
7190 unless you set it to something else. */);
7191 /* We don't have any way to find this out, so set it to nil
7192 and maybe the user would like to set it to t. */
7193 Vx_no_window_manager = Qnil;
7195 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
7196 &Vx_pixel_size_width_font_regexp,
7197 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
7199 Since Emacs gets width of a font matching with this regexp from
7200 PIXEL_SIZE field of the name, font finding mechanism gets faster for
7201 such a font. This is especially effective for such large fonts as
7202 Chinese, Japanese, and Korean. */);
7203 Vx_pixel_size_width_font_regexp = Qnil;
7205 DEFVAR_LISP ("w32-bdf-filename-alist",
7206 &Vw32_bdf_filename_alist,
7207 doc: /* List of bdf fonts and their corresponding filenames. */);
7208 Vw32_bdf_filename_alist = Qnil;
7210 DEFVAR_BOOL ("w32-strict-fontnames",
7211 &w32_strict_fontnames,
7212 doc: /* Non-nil means only use fonts that are exact matches for those requested.
7213 Default is nil, which allows old fontnames that are not XLFD compliant,
7214 and allows third-party CJK display to work by specifying false charset
7215 fields to trick Emacs into translating to Big5, SJIS etc.
7216 Setting this to t will prevent wrong fonts being selected when
7217 fontsets are automatically created. */);
7218 w32_strict_fontnames = 0;
7220 DEFVAR_BOOL ("w32-strict-painting",
7221 &w32_strict_painting,
7222 doc: /* Non-nil means use strict rules for repainting frames.
7223 Set this to nil to get the old behavior for repainting; this should
7224 only be necessary if the default setting causes problems. */);
7225 w32_strict_painting = 1;
7227 #if 0 /* TODO: Port to W32 */
7228 defsubr (&Sx_change_window_property);
7229 defsubr (&Sx_delete_window_property);
7230 defsubr (&Sx_window_property);
7231 #endif
7232 defsubr (&Sxw_display_color_p);
7233 defsubr (&Sx_display_grayscale_p);
7234 defsubr (&Sxw_color_defined_p);
7235 defsubr (&Sxw_color_values);
7236 defsubr (&Sx_server_max_request_size);
7237 defsubr (&Sx_server_vendor);
7238 defsubr (&Sx_server_version);
7239 defsubr (&Sx_display_pixel_width);
7240 defsubr (&Sx_display_pixel_height);
7241 defsubr (&Sx_display_mm_width);
7242 defsubr (&Sx_display_mm_height);
7243 defsubr (&Sx_display_screens);
7244 defsubr (&Sx_display_planes);
7245 defsubr (&Sx_display_color_cells);
7246 defsubr (&Sx_display_visual_class);
7247 defsubr (&Sx_display_backing_store);
7248 defsubr (&Sx_display_save_under);
7249 defsubr (&Sx_create_frame);
7250 defsubr (&Sx_open_connection);
7251 defsubr (&Sx_close_connection);
7252 defsubr (&Sx_display_list);
7253 defsubr (&Sx_synchronize);
7254 defsubr (&Sx_focus_frame);
7256 /* W32 specific functions */
7258 defsubr (&Sw32_define_rgb_color);
7259 defsubr (&Sw32_default_color_map);
7260 defsubr (&Sw32_send_sys_command);
7261 defsubr (&Sw32_shell_execute);
7262 defsubr (&Sw32_register_hot_key);
7263 defsubr (&Sw32_unregister_hot_key);
7264 defsubr (&Sw32_registered_hot_keys);
7265 defsubr (&Sw32_reconstruct_hot_key);
7266 defsubr (&Sw32_toggle_lock_key);
7267 defsubr (&Sw32_window_exists_p);
7268 defsubr (&Sw32_battery_status);
7270 defsubr (&Sfile_system_info);
7271 defsubr (&Sdefault_printer_name);
7273 check_window_system_func = check_w32;
7276 hourglass_timer = 0;
7277 hourglass_hwnd = NULL;
7279 defsubr (&Sx_show_tip);
7280 defsubr (&Sx_hide_tip);
7281 tip_timer = Qnil;
7282 staticpro (&tip_timer);
7283 tip_frame = Qnil;
7284 staticpro (&tip_frame);
7286 last_show_tip_args = Qnil;
7287 staticpro (&last_show_tip_args);
7289 defsubr (&Sx_file_dialog);
7290 defsubr (&Ssystem_move_file_to_trash);
7295 globals_of_w32fns is used to initialize those global variables that
7296 must always be initialized on startup even when the global variable
7297 initialized is non zero (see the function main in emacs.c).
7298 globals_of_w32fns is called from syms_of_w32fns when the global
7299 variable initialized is 0 and directly from main when initialized
7300 is non zero.
7302 void
7303 globals_of_w32fns ()
7305 HMODULE user32_lib = GetModuleHandle ("user32.dll");
7307 TrackMouseEvent not available in all versions of Windows, so must load
7308 it dynamically. Do it once, here, instead of every time it is used.
7310 track_mouse_event_fn = (TrackMouseEvent_Proc)
7311 GetProcAddress (user32_lib, "TrackMouseEvent");
7312 /* ditto for GetClipboardSequenceNumber. */
7313 clipboard_sequence_fn = (ClipboardSequence_Proc)
7314 GetProcAddress (user32_lib, "GetClipboardSequenceNumber");
7316 monitor_from_point_fn = (MonitorFromPoint_Proc)
7317 GetProcAddress (user32_lib, "MonitorFromPoint");
7318 get_monitor_info_fn = (GetMonitorInfo_Proc)
7319 GetProcAddress (user32_lib, "GetMonitorInfoA");
7322 HMODULE imm32_lib = GetModuleHandle ("imm32.dll");
7323 get_composition_string_fn = (ImmGetCompositionString_Proc)
7324 GetProcAddress (imm32_lib, "ImmGetCompositionStringW");
7325 get_ime_context_fn = (ImmGetContext_Proc)
7326 GetProcAddress (imm32_lib, "ImmGetContext");
7327 release_ime_context_fn = (ImmReleaseContext_Proc)
7328 GetProcAddress (imm32_lib, "ImmReleaseContext");
7329 set_ime_composition_window_fn = (ImmSetCompositionWindow_Proc)
7330 GetProcAddress (imm32_lib, "ImmSetCompositionWindow");
7332 DEFVAR_INT ("w32-ansi-code-page",
7333 &w32_ansi_code_page,
7334 doc: /* The ANSI code page used by the system. */);
7335 w32_ansi_code_page = GetACP ();
7337 /* MessageBox does not work without this when linked to comctl32.dll 6.0. */
7338 InitCommonControls ();
7340 syms_of_w32uniscribe ();
7343 #undef abort
7345 void
7346 w32_abort ()
7348 int button;
7349 button = MessageBox (NULL,
7350 "A fatal error has occurred!\n\n"
7351 "Would you like to attach a debugger?\n\n"
7352 "Select YES to debug, NO to abort Emacs"
7353 #if __GNUC__
7354 "\n\n(type \"gdb -p <emacs-PID>\" and\n"
7355 "\"continue\" inside GDB before clicking YES.)"
7356 #endif
7357 , "Emacs Abort Dialog",
7358 MB_ICONEXCLAMATION | MB_TASKMODAL
7359 | MB_SETFOREGROUND | MB_YESNO);
7360 switch (button)
7362 case IDYES:
7363 DebugBreak ();
7364 exit (2); /* tell the compiler we will never return */
7365 case IDNO:
7366 default:
7367 abort ();
7368 break;
7372 /* For convenience when debugging. */
7374 w32_last_error ()
7376 return GetLastError ();
7379 /* arch-tag: 707589ab-b9be-4638-8cdd-74629cc9b446
7380 (do not change this comment) */