* message.el (message-mail): Just pass yank-action on to message-setup.
[emacs.git] / src / w32fns.c
blob938bd463b250f9f5e80bdb84509d915aa1a841b1
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 /* Get the name of the frame to use for resource lookup. */
5431 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
5432 if (!STRINGP (name)
5433 && !EQ (name, Qunbound)
5434 && !NILP (name))
5435 error ("Invalid frame name--not a string or nil");
5436 Vx_resource_name = name;
5438 frame = Qnil;
5439 GCPRO3 (parms, name, frame);
5440 /* Make a frame without minibuffer nor mode-line. */
5441 f = make_frame (0);
5442 f->wants_modeline = 0;
5443 XSETFRAME (frame, f);
5445 buffer = Fget_buffer_create (build_string (" *tip*"));
5446 Fset_window_buffer (FRAME_ROOT_WINDOW (f), buffer, Qnil);
5447 old_buffer = current_buffer;
5448 set_buffer_internal_1 (XBUFFER (buffer));
5449 current_buffer->truncate_lines = Qnil;
5450 specbind (Qinhibit_read_only, Qt);
5451 specbind (Qinhibit_modification_hooks, Qt);
5452 Ferase_buffer ();
5453 Finsert (1, &text);
5454 set_buffer_internal_1 (old_buffer);
5456 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
5457 record_unwind_protect (unwind_create_tip_frame, frame);
5459 /* By setting the output method, we're essentially saying that
5460 the frame is live, as per FRAME_LIVE_P. If we get a signal
5461 from this point on, x_destroy_window might screw up reference
5462 counts etc. */
5463 f->terminal = dpyinfo->terminal;
5464 f->terminal->reference_count++;
5465 f->output_method = output_w32;
5466 f->output_data.w32 =
5467 (struct w32_output *) xmalloc (sizeof (struct w32_output));
5468 bzero (f->output_data.w32, sizeof (struct w32_output));
5470 FRAME_FONTSET (f) = -1;
5471 f->icon_name = Qnil;
5473 #if GLYPH_DEBUG
5474 image_cache_refcount = FRAME_IMAGE_CACHE (f)->refcount;
5475 dpyinfo_refcount = dpyinfo->reference_count;
5476 #endif /* GLYPH_DEBUG */
5477 FRAME_KBOARD (f) = kb;
5478 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
5479 f->output_data.w32->explicit_parent = 0;
5481 /* Set the name; the functions to which we pass f expect the name to
5482 be set. */
5483 if (EQ (name, Qunbound) || NILP (name))
5485 f->name = build_string (dpyinfo->w32_id_name);
5486 f->explicit_name = 0;
5488 else
5490 f->name = name;
5491 f->explicit_name = 1;
5492 /* use the frame's title when getting resources for this frame. */
5493 specbind (Qx_resource_name, name);
5496 f->resx = dpyinfo->resx;
5497 f->resy = dpyinfo->resy;
5499 if (uniscribe_available)
5500 register_font_driver (&uniscribe_font_driver, f);
5501 register_font_driver (&w32font_driver, f);
5503 x_default_parameter (f, parms, Qfont_backend, Qnil,
5504 "fontBackend", "FontBackend", RES_TYPE_STRING);
5506 /* Extract the window parameters from the supplied values
5507 that are needed to determine window geometry. */
5508 x_default_font_parameter (f, parms);
5510 x_default_parameter (f, parms, Qborder_width, make_number (2),
5511 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
5512 /* This defaults to 2 in order to match xterm. We recognize either
5513 internalBorderWidth or internalBorder (which is what xterm calls
5514 it). */
5515 if (NILP (Fassq (Qinternal_border_width, parms)))
5517 Lisp_Object value;
5519 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
5520 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
5521 if (! EQ (value, Qunbound))
5522 parms = Fcons (Fcons (Qinternal_border_width, value),
5523 parms);
5525 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
5526 "internalBorderWidth", "internalBorderWidth",
5527 RES_TYPE_NUMBER);
5529 /* Also do the stuff which must be set before the window exists. */
5530 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
5531 "foreground", "Foreground", RES_TYPE_STRING);
5532 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
5533 "background", "Background", RES_TYPE_STRING);
5534 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
5535 "pointerColor", "Foreground", RES_TYPE_STRING);
5536 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
5537 "cursorColor", "Foreground", RES_TYPE_STRING);
5538 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
5539 "borderColor", "BorderColor", RES_TYPE_STRING);
5541 /* Init faces before x_default_parameter is called for scroll-bar
5542 parameters because that function calls x_set_scroll_bar_width,
5543 which calls change_frame_size, which calls Fset_window_buffer,
5544 which runs hooks, which call Fvertical_motion. At the end, we
5545 end up in init_iterator with a null face cache, which should not
5546 happen. */
5547 init_frame_faces (f);
5549 f->output_data.w32->dwStyle = WS_BORDER | WS_POPUP | WS_DISABLED;
5550 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
5552 window_prompting = x_figure_window_size (f, parms, 0);
5554 /* No fringes on tip frame. */
5555 f->fringe_cols = 0;
5556 f->left_fringe_width = 0;
5557 f->right_fringe_width = 0;
5559 BLOCK_INPUT;
5560 my_create_tip_window (f);
5561 UNBLOCK_INPUT;
5563 x_make_gc (f);
5565 x_default_parameter (f, parms, Qauto_raise, Qnil,
5566 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5567 x_default_parameter (f, parms, Qauto_lower, Qnil,
5568 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5569 x_default_parameter (f, parms, Qcursor_type, Qbox,
5570 "cursorType", "CursorType", RES_TYPE_SYMBOL);
5572 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
5573 Change will not be effected unless different from the current
5574 FRAME_LINES (f). */
5575 width = FRAME_COLS (f);
5576 height = FRAME_LINES (f);
5577 FRAME_LINES (f) = 0;
5578 SET_FRAME_COLS (f, 0);
5579 change_frame_size (f, height, width, 1, 0, 0);
5581 /* Add `tooltip' frame parameter's default value. */
5582 if (NILP (Fframe_parameter (frame, intern ("tooltip"))))
5583 Fmodify_frame_parameters (frame, Fcons (Fcons (intern ("tooltip"), Qt),
5584 Qnil));
5586 /* Set up faces after all frame parameters are known. This call
5587 also merges in face attributes specified for new frames.
5589 Frame parameters may be changed if .Xdefaults contains
5590 specifications for the default font. For example, if there is an
5591 `Emacs.default.attributeBackground: pink', the `background-color'
5592 attribute of the frame get's set, which let's the internal border
5593 of the tooltip frame appear in pink. Prevent this. */
5595 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
5596 Lisp_Object fg = Fframe_parameter (frame, Qforeground_color);
5597 Lisp_Object colors = Qnil;
5599 /* Set tip_frame here, so that */
5600 tip_frame = frame;
5601 call2 (Qface_set_after_frame_default, frame, Qnil);
5603 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
5604 colors = Fcons (Fcons (Qbackground_color, bg), colors);
5605 if (!EQ (fg, Fframe_parameter (frame, Qforeground_color)))
5606 colors = Fcons (Fcons (Qforeground_color, fg), colors);
5608 if (!NILP (colors))
5609 Fmodify_frame_parameters (frame, colors);
5612 f->no_split = 1;
5614 UNGCPRO;
5616 /* It is now ok to make the frame official even if we get an error
5617 below. And the frame needs to be on Vframe_list or making it
5618 visible won't work. */
5619 Vframe_list = Fcons (frame, Vframe_list);
5621 /* Now that the frame is official, it counts as a reference to
5622 its display. */
5623 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
5625 /* Setting attributes of faces of the tooltip frame from resources
5626 and similar will increment face_change_count, which leads to the
5627 clearing of all current matrices. Since this isn't necessary
5628 here, avoid it by resetting face_change_count to the value it
5629 had before we created the tip frame. */
5630 face_change_count = face_change_count_before;
5632 /* Discard the unwind_protect. */
5633 return unbind_to (count, frame);
5637 /* Compute where to display tip frame F. PARMS is the list of frame
5638 parameters for F. DX and DY are specified offsets from the current
5639 location of the mouse. WIDTH and HEIGHT are the width and height
5640 of the tooltip. Return coordinates relative to the root window of
5641 the display in *ROOT_X, and *ROOT_Y. */
5643 static void
5644 compute_tip_xy (f, parms, dx, dy, width, height, root_x, root_y)
5645 struct frame *f;
5646 Lisp_Object parms, dx, dy;
5647 int width, height;
5648 int *root_x, *root_y;
5650 Lisp_Object left, top;
5651 int min_x, min_y, max_x, max_y;
5653 /* User-specified position? */
5654 left = Fcdr (Fassq (Qleft, parms));
5655 top = Fcdr (Fassq (Qtop, parms));
5657 /* Move the tooltip window where the mouse pointer is. Resize and
5658 show it. */
5659 if (!INTEGERP (left) || !INTEGERP (top))
5661 POINT pt;
5663 /* Default min and max values. */
5664 min_x = 0;
5665 min_y = 0;
5666 max_x = x_display_pixel_width (FRAME_W32_DISPLAY_INFO (f));
5667 max_y = x_display_pixel_height (FRAME_W32_DISPLAY_INFO (f));
5669 BLOCK_INPUT;
5670 GetCursorPos (&pt);
5671 *root_x = pt.x;
5672 *root_y = pt.y;
5673 UNBLOCK_INPUT;
5675 /* If multiple monitor support is available, constrain the tip onto
5676 the current monitor. This improves the above by allowing negative
5677 co-ordinates if monitor positions are such that they are valid, and
5678 snaps a tooltip onto a single monitor if we are close to the edge
5679 where it would otherwise flow onto the other monitor (or into
5680 nothingness if there is a gap in the overlap). */
5681 if (monitor_from_point_fn && get_monitor_info_fn)
5683 struct MONITOR_INFO info;
5684 HMONITOR monitor
5685 = monitor_from_point_fn (pt, MONITOR_DEFAULT_TO_NEAREST);
5686 info.cbSize = sizeof (info);
5688 if (get_monitor_info_fn (monitor, &info))
5690 min_x = info.rcWork.left;
5691 min_y = info.rcWork.top;
5692 max_x = info.rcWork.right;
5693 max_y = info.rcWork.bottom;
5698 if (INTEGERP (top))
5699 *root_y = XINT (top);
5700 else if (*root_y + XINT (dy) <= min_y)
5701 *root_y = min_y; /* Can happen for negative dy */
5702 else if (*root_y + XINT (dy) + height <= max_y)
5703 /* It fits below the pointer */
5704 *root_y += XINT (dy);
5705 else if (height + XINT (dy) + min_y <= *root_y)
5706 /* It fits above the pointer. */
5707 *root_y -= height + XINT (dy);
5708 else
5709 /* Put it on the top. */
5710 *root_y = min_y;
5712 if (INTEGERP (left))
5713 *root_x = XINT (left);
5714 else if (*root_x + XINT (dx) <= min_x)
5715 *root_x = 0; /* Can happen for negative dx */
5716 else if (*root_x + XINT (dx) + width <= max_x)
5717 /* It fits to the right of the pointer. */
5718 *root_x += XINT (dx);
5719 else if (width + XINT (dx) + min_x <= *root_x)
5720 /* It fits to the left of the pointer. */
5721 *root_x -= width + XINT (dx);
5722 else
5723 /* Put it left justified on the screen -- it ought to fit that way. */
5724 *root_x = min_x;
5728 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
5729 doc: /* Show STRING in a \"tooltip\" window on frame FRAME.
5730 A tooltip window is a small window displaying a string.
5732 This is an internal function; Lisp code should call `tooltip-show'.
5734 FRAME nil or omitted means use the selected frame.
5736 PARMS is an optional list of frame parameters which can be
5737 used to change the tooltip's appearance.
5739 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
5740 means use the default timeout of 5 seconds.
5742 If the list of frame parameters PARMS contains a `left' parameter,
5743 the tooltip is displayed at that x-position. Otherwise it is
5744 displayed at the mouse position, with offset DX added (default is 5 if
5745 DX isn't specified). Likewise for the y-position; if a `top' frame
5746 parameter is specified, it determines the y-position of the tooltip
5747 window, otherwise it is displayed at the mouse position, with offset
5748 DY added (default is -10).
5750 A tooltip's maximum size is specified by `x-max-tooltip-size'.
5751 Text larger than the specified size is clipped. */)
5752 (string, frame, parms, timeout, dx, dy)
5753 Lisp_Object string, frame, parms, timeout, dx, dy;
5755 struct frame *f;
5756 struct window *w;
5757 int root_x, root_y;
5758 struct buffer *old_buffer;
5759 struct text_pos pos;
5760 int i, width, height;
5761 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5762 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5763 int count = SPECPDL_INDEX ();
5765 specbind (Qinhibit_redisplay, Qt);
5767 GCPRO4 (string, parms, frame, timeout);
5769 CHECK_STRING (string);
5770 f = check_x_frame (frame);
5771 if (NILP (timeout))
5772 timeout = make_number (5);
5773 else
5774 CHECK_NATNUM (timeout);
5776 if (NILP (dx))
5777 dx = make_number (5);
5778 else
5779 CHECK_NUMBER (dx);
5781 if (NILP (dy))
5782 dy = make_number (-10);
5783 else
5784 CHECK_NUMBER (dy);
5786 if (NILP (last_show_tip_args))
5787 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
5789 if (!NILP (tip_frame))
5791 Lisp_Object last_string = AREF (last_show_tip_args, 0);
5792 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
5793 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
5795 if (EQ (frame, last_frame)
5796 && !NILP (Fequal (last_string, string))
5797 && !NILP (Fequal (last_parms, parms)))
5799 struct frame *f = XFRAME (tip_frame);
5801 /* Only DX and DY have changed. */
5802 if (!NILP (tip_timer))
5804 Lisp_Object timer = tip_timer;
5805 tip_timer = Qnil;
5806 call1 (Qcancel_timer, timer);
5809 BLOCK_INPUT;
5810 compute_tip_xy (f, parms, dx, dy, FRAME_PIXEL_WIDTH (f),
5811 FRAME_PIXEL_HEIGHT (f), &root_x, &root_y);
5813 /* Put tooltip in topmost group and in position. */
5814 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
5815 root_x, root_y, 0, 0,
5816 SWP_NOSIZE | SWP_NOACTIVATE);
5818 /* Ensure tooltip is on top of other topmost windows (eg menus). */
5819 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
5820 0, 0, 0, 0,
5821 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
5823 UNBLOCK_INPUT;
5824 goto start_timer;
5828 /* Hide a previous tip, if any. */
5829 Fx_hide_tip ();
5831 ASET (last_show_tip_args, 0, string);
5832 ASET (last_show_tip_args, 1, frame);
5833 ASET (last_show_tip_args, 2, parms);
5835 /* Add default values to frame parameters. */
5836 if (NILP (Fassq (Qname, parms)))
5837 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
5838 if (NILP (Fassq (Qinternal_border_width, parms)))
5839 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
5840 if (NILP (Fassq (Qborder_width, parms)))
5841 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
5842 if (NILP (Fassq (Qborder_color, parms)))
5843 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
5844 if (NILP (Fassq (Qbackground_color, parms)))
5845 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
5846 parms);
5848 /* Block input until the tip has been fully drawn, to avoid crashes
5849 when drawing tips in menus. */
5850 BLOCK_INPUT;
5852 /* Create a frame for the tooltip, and record it in the global
5853 variable tip_frame. */
5854 frame = x_create_tip_frame (FRAME_W32_DISPLAY_INFO (f), parms, string);
5855 f = XFRAME (frame);
5857 /* Set up the frame's root window. */
5858 w = XWINDOW (FRAME_ROOT_WINDOW (f));
5859 w->left_col = w->top_line = make_number (0);
5861 if (CONSP (Vx_max_tooltip_size)
5862 && INTEGERP (XCAR (Vx_max_tooltip_size))
5863 && XINT (XCAR (Vx_max_tooltip_size)) > 0
5864 && INTEGERP (XCDR (Vx_max_tooltip_size))
5865 && XINT (XCDR (Vx_max_tooltip_size)) > 0)
5867 w->total_cols = XCAR (Vx_max_tooltip_size);
5868 w->total_lines = XCDR (Vx_max_tooltip_size);
5870 else
5872 w->total_cols = make_number (80);
5873 w->total_lines = make_number (40);
5876 FRAME_TOTAL_COLS (f) = XINT (w->total_cols);
5877 adjust_glyphs (f);
5878 w->pseudo_window_p = 1;
5880 /* Display the tooltip text in a temporary buffer. */
5881 old_buffer = current_buffer;
5882 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->buffer));
5883 current_buffer->truncate_lines = Qnil;
5884 clear_glyph_matrix (w->desired_matrix);
5885 clear_glyph_matrix (w->current_matrix);
5886 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
5887 try_window (FRAME_ROOT_WINDOW (f), pos, 0);
5889 /* Compute width and height of the tooltip. */
5890 width = height = 0;
5891 for (i = 0; i < w->desired_matrix->nrows; ++i)
5893 struct glyph_row *row = &w->desired_matrix->rows[i];
5894 struct glyph *last;
5895 int row_width;
5897 /* Stop at the first empty row at the end. */
5898 if (!row->enabled_p || !row->displays_text_p)
5899 break;
5901 /* Let the row go over the full width of the frame. */
5902 row->full_width_p = 1;
5904 #ifdef TODO /* Investigate why some fonts need more width than is
5905 calculated for some tooltips. */
5906 /* There's a glyph at the end of rows that is use to place
5907 the cursor there. Don't include the width of this glyph. */
5908 if (row->used[TEXT_AREA])
5910 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5911 row_width = row->pixel_width - last->pixel_width;
5913 else
5914 #endif
5915 row_width = row->pixel_width;
5917 /* TODO: find why tips do not draw along baseline as instructed. */
5918 height += row->height;
5919 width = max (width, row_width);
5922 /* Add the frame's internal border to the width and height the X
5923 window should have. */
5924 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5925 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5927 /* Move the tooltip window where the mouse pointer is. Resize and
5928 show it. */
5929 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5932 /* Adjust Window size to take border into account. */
5933 RECT rect;
5934 rect.left = rect.top = 0;
5935 rect.right = width;
5936 rect.bottom = height;
5937 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
5938 FRAME_EXTERNAL_MENU_BAR (f));
5940 /* Position and size tooltip, and put it in the topmost group.
5941 The add-on of 3 to the 5th argument is a kludge: without it,
5942 some fonts cause the last character of the tip to be truncated,
5943 for some obscure reason. */
5944 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
5945 root_x, root_y, rect.right - rect.left + 3,
5946 rect.bottom - rect.top, SWP_NOACTIVATE);
5948 /* Ensure tooltip is on top of other topmost windows (eg menus). */
5949 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
5950 0, 0, 0, 0,
5951 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
5953 /* Let redisplay know that we have made the frame visible already. */
5954 f->async_visible = 1;
5956 ShowWindow (FRAME_W32_WINDOW (f), SW_SHOWNOACTIVATE);
5959 /* Draw into the window. */
5960 w->must_be_updated_p = 1;
5961 update_single_window (w, 1);
5963 UNBLOCK_INPUT;
5965 /* Restore original current buffer. */
5966 set_buffer_internal_1 (old_buffer);
5967 windows_or_buffers_changed = old_windows_or_buffers_changed;
5969 start_timer:
5970 /* Let the tip disappear after timeout seconds. */
5971 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
5972 intern ("x-hide-tip"));
5974 UNGCPRO;
5975 return unbind_to (count, Qnil);
5979 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
5980 doc: /* Hide the current tooltip window, if there is any.
5981 Value is t if tooltip was open, nil otherwise. */)
5984 int count;
5985 Lisp_Object deleted, frame, timer;
5986 struct gcpro gcpro1, gcpro2;
5988 /* Return quickly if nothing to do. */
5989 if (NILP (tip_timer) && NILP (tip_frame))
5990 return Qnil;
5992 frame = tip_frame;
5993 timer = tip_timer;
5994 GCPRO2 (frame, timer);
5995 tip_frame = tip_timer = deleted = Qnil;
5997 count = SPECPDL_INDEX ();
5998 specbind (Qinhibit_redisplay, Qt);
5999 specbind (Qinhibit_quit, Qt);
6001 if (!NILP (timer))
6002 call1 (Qcancel_timer, timer);
6004 if (FRAMEP (frame))
6006 delete_frame (frame, Qnil);
6007 deleted = Qt;
6010 UNGCPRO;
6011 return unbind_to (count, deleted);
6016 /***********************************************************************
6017 File selection dialog
6018 ***********************************************************************/
6019 extern Lisp_Object Qfile_name_history;
6021 /* Callback for altering the behavior of the Open File dialog.
6022 Makes the Filename text field contain "Current Directory" and be
6023 read-only when "Directories" is selected in the filter. This
6024 allows us to work around the fact that the standard Open File
6025 dialog does not support directories. */
6026 UINT CALLBACK
6027 file_dialog_callback (hwnd, msg, wParam, lParam)
6028 HWND hwnd;
6029 UINT msg;
6030 WPARAM wParam;
6031 LPARAM lParam;
6033 if (msg == WM_NOTIFY)
6035 OFNOTIFY * notify = (OFNOTIFY *)lParam;
6036 /* Detect when the Filter dropdown is changed. */
6037 if (notify->hdr.code == CDN_TYPECHANGE
6038 || notify->hdr.code == CDN_INITDONE)
6040 HWND dialog = GetParent (hwnd);
6041 HWND edit_control = GetDlgItem (dialog, FILE_NAME_TEXT_FIELD);
6043 /* Directories is in index 2. */
6044 if (notify->lpOFN->nFilterIndex == 2)
6046 CommDlg_OpenSave_SetControlText (dialog, FILE_NAME_TEXT_FIELD,
6047 "Current Directory");
6048 EnableWindow (edit_control, FALSE);
6050 else
6052 /* Don't override default filename on init done. */
6053 if (notify->hdr.code == CDN_TYPECHANGE)
6054 CommDlg_OpenSave_SetControlText (dialog,
6055 FILE_NAME_TEXT_FIELD, "");
6056 EnableWindow (edit_control, TRUE);
6060 return 0;
6063 /* Since we compile with _WIN32_WINNT set to 0x0400 (for NT4 compatibility)
6064 we end up with the old file dialogs. Define a big enough struct for the
6065 new dialog to trick GetOpenFileName into giving us the new dialogs on
6066 Windows 2000 and XP. */
6067 typedef struct
6069 OPENFILENAME real_details;
6070 void * pReserved;
6071 DWORD dwReserved;
6072 DWORD FlagsEx;
6073 } NEWOPENFILENAME;
6076 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
6077 doc: /* Read file name, prompting with PROMPT in directory DIR.
6078 Use a file selection dialog.
6079 Select DEFAULT-FILENAME in the dialog's file selection box, if
6080 specified. Ensure that file exists if MUSTMATCH is non-nil.
6081 If ONLY-DIR-P is non-nil, the user can only select directories. */)
6082 (prompt, dir, default_filename, mustmatch, only_dir_p)
6083 Lisp_Object prompt, dir, default_filename, mustmatch, only_dir_p;
6085 struct frame *f = SELECTED_FRAME ();
6086 Lisp_Object file = Qnil;
6087 int count = SPECPDL_INDEX ();
6088 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
6089 char filename[MAX_PATH + 1];
6090 char init_dir[MAX_PATH + 1];
6091 int default_filter_index = 1; /* 1: All Files, 2: Directories only */
6093 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
6094 CHECK_STRING (prompt);
6095 CHECK_STRING (dir);
6097 /* Create the dialog with PROMPT as title, using DIR as initial
6098 directory and using "*" as pattern. */
6099 dir = Fexpand_file_name (dir, Qnil);
6100 strncpy (init_dir, SDATA (ENCODE_FILE (dir)), MAX_PATH);
6101 init_dir[MAX_PATH] = '\0';
6102 unixtodos_filename (init_dir);
6104 if (STRINGP (default_filename))
6106 char *file_name_only;
6107 char *full_path_name = SDATA (ENCODE_FILE (default_filename));
6109 unixtodos_filename (full_path_name);
6111 file_name_only = strrchr (full_path_name, '\\');
6112 if (!file_name_only)
6113 file_name_only = full_path_name;
6114 else
6115 file_name_only++;
6117 strncpy (filename, file_name_only, MAX_PATH);
6118 filename[MAX_PATH] = '\0';
6120 else
6121 filename[0] = '\0';
6124 NEWOPENFILENAME new_file_details;
6125 BOOL file_opened = FALSE;
6126 OPENFILENAME * file_details = &new_file_details.real_details;
6128 /* Prevent redisplay. */
6129 specbind (Qinhibit_redisplay, Qt);
6130 BLOCK_INPUT;
6132 bzero (&new_file_details, sizeof (new_file_details));
6133 /* Apparently NT4 crashes if you give it an unexpected size.
6134 I'm not sure about Windows 9x, so play it safe. */
6135 if (w32_major_version > 4 && w32_major_version < 95)
6136 file_details->lStructSize = sizeof (NEWOPENFILENAME);
6137 else
6138 file_details->lStructSize = sizeof (OPENFILENAME);
6140 file_details->hwndOwner = FRAME_W32_WINDOW (f);
6141 /* Undocumented Bug in Common File Dialog:
6142 If a filter is not specified, shell links are not resolved. */
6143 file_details->lpstrFilter = "All Files (*.*)\0*.*\0Directories\0*|*\0\0";
6144 file_details->lpstrFile = filename;
6145 file_details->nMaxFile = sizeof (filename);
6146 file_details->lpstrInitialDir = init_dir;
6147 file_details->lpstrTitle = SDATA (prompt);
6149 if (! NILP (only_dir_p))
6150 default_filter_index = 2;
6152 file_details->nFilterIndex = default_filter_index;
6154 file_details->Flags = (OFN_HIDEREADONLY | OFN_NOCHANGEDIR
6155 | OFN_EXPLORER | OFN_ENABLEHOOK);
6156 if (!NILP (mustmatch))
6158 /* Require that the path to the parent directory exists. */
6159 file_details->Flags |= OFN_PATHMUSTEXIST;
6160 /* If we are looking for a file, require that it exists. */
6161 if (NILP (only_dir_p))
6162 file_details->Flags |= OFN_FILEMUSTEXIST;
6165 file_details->lpfnHook = (LPOFNHOOKPROC) file_dialog_callback;
6167 file_opened = GetOpenFileName (file_details);
6169 UNBLOCK_INPUT;
6171 if (file_opened)
6173 dostounix_filename (filename);
6175 if (file_details->nFilterIndex == 2)
6177 /* "Directories" selected - strip dummy file name. */
6178 char * last = strrchr (filename, '/');
6179 *last = '\0';
6182 file = DECODE_FILE (build_string (filename));
6184 /* User cancelled the dialog without making a selection. */
6185 else if (!CommDlgExtendedError ())
6186 file = Qnil;
6187 /* An error occurred, fallback on reading from the mini-buffer. */
6188 else
6189 file = Fcompleting_read (prompt, intern ("read-file-name-internal"),
6190 dir, mustmatch, dir, Qfile_name_history,
6191 default_filename, Qnil);
6193 file = unbind_to (count, file);
6196 UNGCPRO;
6198 /* Make "Cancel" equivalent to C-g. */
6199 if (NILP (file))
6200 Fsignal (Qquit, Qnil);
6202 return unbind_to (count, file);
6206 /* Moving files to the system recycle bin.
6207 Used by `move-file-to-trash' instead of the default moving to ~/.Trash */
6208 DEFUN ("system-move-file-to-trash", Fsystem_move_file_to_trash,
6209 Ssystem_move_file_to_trash, 1, 1, 0,
6210 doc: /* Move file or directory named FILENAME to the recycle bin. */)
6211 (filename)
6212 Lisp_Object filename;
6214 Lisp_Object handler;
6215 Lisp_Object encoded_file;
6216 Lisp_Object operation;
6218 operation = Qdelete_file;
6219 if (!NILP (Ffile_directory_p (filename))
6220 && NILP (Ffile_symlink_p (filename)))
6222 operation = intern ("delete-directory");
6223 filename = Fdirectory_file_name (filename);
6225 filename = Fexpand_file_name (filename, Qnil);
6227 handler = Ffind_file_name_handler (filename, operation);
6228 if (!NILP (handler))
6229 return call2 (handler, operation, filename);
6231 encoded_file = ENCODE_FILE (filename);
6234 const char * path;
6235 SHFILEOPSTRUCT file_op;
6236 char tmp_path[MAX_PATH + 1];
6238 path = map_w32_filename (SDATA (encoded_file), NULL);
6240 /* On Windows, write permission is required to delete/move files. */
6241 _chmod (path, 0666);
6243 bzero (tmp_path, sizeof (tmp_path));
6244 strcpy (tmp_path, path);
6246 bzero (&file_op, sizeof (file_op));
6247 file_op.hwnd = HWND_DESKTOP;
6248 file_op.wFunc = FO_DELETE;
6249 file_op.pFrom = tmp_path;
6250 file_op.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_ALLOWUNDO
6251 | FOF_NOERRORUI | FOF_NO_CONNECTED_ELEMENTS;
6252 file_op.fAnyOperationsAborted = FALSE;
6254 if (SHFileOperation (&file_op) != 0)
6255 report_file_error ("Removing old name", list1 (filename));
6257 return Qnil;
6261 /***********************************************************************
6262 w32 specialized functions
6263 ***********************************************************************/
6265 DEFUN ("w32-send-sys-command", Fw32_send_sys_command,
6266 Sw32_send_sys_command, 1, 2, 0,
6267 doc: /* Send frame a Windows WM_SYSCOMMAND message of type COMMAND.
6268 Some useful values for COMMAND are #xf030 to maximize frame (#xf020
6269 to minimize), #xf120 to restore frame to original size, and #xf100
6270 to activate the menubar for keyboard access. #xf140 activates the
6271 screen saver if defined.
6273 If optional parameter FRAME is not specified, use selected frame. */)
6274 (command, frame)
6275 Lisp_Object command, frame;
6277 FRAME_PTR f = check_x_frame (frame);
6279 CHECK_NUMBER (command);
6281 PostMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, XINT (command), 0);
6283 return Qnil;
6286 DEFUN ("w32-shell-execute", Fw32_shell_execute, Sw32_shell_execute, 2, 4, 0,
6287 doc: /* Get Windows to perform OPERATION on DOCUMENT.
6288 This is a wrapper around the ShellExecute system function, which
6289 invokes the application registered to handle OPERATION for DOCUMENT.
6291 OPERATION is either nil or a string that names a supported operation.
6292 What operations can be used depends on the particular DOCUMENT and its
6293 handler application, but typically it is one of the following common
6294 operations:
6296 \"open\" - open DOCUMENT, which could be a file, a directory, or an
6297 executable program. If it is an application, that
6298 application is launched in the current buffer's default
6299 directory. Otherwise, the application associated with
6300 DOCUMENT is launched in the buffer's default directory.
6301 \"print\" - print DOCUMENT, which must be a file
6302 \"explore\" - start the Windows Explorer on DOCUMENT
6303 \"edit\" - launch an editor and open DOCUMENT for editing; which
6304 editor is launched depends on the association for the
6305 specified DOCUMENT
6306 \"find\" - initiate search starting from DOCUMENT which must specify
6307 a directory
6308 nil - invoke the default OPERATION, or \"open\" if default is
6309 not defined or unavailable
6311 DOCUMENT is typically the name of a document file or a URL, but can
6312 also be a program executable to run, or a directory to open in the
6313 Windows Explorer.
6315 If DOCUMENT is a program executable, the optional third arg PARAMETERS
6316 can be a string containing command line parameters that will be passed
6317 to the program; otherwise, PARAMETERS should be nil or unspecified.
6319 Optional fourth argument SHOW-FLAG can be used to control how the
6320 application will be displayed when it is invoked. If SHOW-FLAG is nil
6321 or unspecified, the application is displayed normally, otherwise it is
6322 an integer representing a ShowWindow flag:
6324 0 - start hidden
6325 1 - start normally
6326 3 - start maximized
6327 6 - start minimized */)
6328 (operation, document, parameters, show_flag)
6329 Lisp_Object operation, document, parameters, show_flag;
6331 Lisp_Object current_dir;
6333 CHECK_STRING (document);
6335 /* Encode filename, current directory and parameters. */
6336 current_dir = ENCODE_FILE (current_buffer->directory);
6337 document = ENCODE_FILE (document);
6338 if (STRINGP (parameters))
6339 parameters = ENCODE_SYSTEM (parameters);
6341 if ((int) ShellExecute (NULL,
6342 (STRINGP (operation) ?
6343 SDATA (operation) : NULL),
6344 SDATA (document),
6345 (STRINGP (parameters) ?
6346 SDATA (parameters) : NULL),
6347 SDATA (current_dir),
6348 (INTEGERP (show_flag) ?
6349 XINT (show_flag) : SW_SHOWDEFAULT))
6350 > 32)
6351 return Qt;
6352 error ("ShellExecute failed: %s", w32_strerror (0));
6355 /* Lookup virtual keycode from string representing the name of a
6356 non-ascii keystroke into the corresponding virtual key, using
6357 lispy_function_keys. */
6358 static int
6359 lookup_vk_code (char *key)
6361 int i;
6363 for (i = 0; i < 256; i++)
6364 if (lispy_function_keys[i]
6365 && strcmp (lispy_function_keys[i], key) == 0)
6366 return i;
6368 return -1;
6371 /* Convert a one-element vector style key sequence to a hot key
6372 definition. */
6373 static Lisp_Object
6374 w32_parse_hot_key (key)
6375 Lisp_Object key;
6377 /* Copied from Fdefine_key and store_in_keymap. */
6378 register Lisp_Object c;
6379 int vk_code;
6380 int lisp_modifiers;
6381 int w32_modifiers;
6382 struct gcpro gcpro1;
6384 CHECK_VECTOR (key);
6386 if (XFASTINT (Flength (key)) != 1)
6387 return Qnil;
6389 GCPRO1 (key);
6391 c = Faref (key, make_number (0));
6393 if (CONSP (c) && lucid_event_type_list_p (c))
6394 c = Fevent_convert_list (c);
6396 UNGCPRO;
6398 if (! INTEGERP (c) && ! SYMBOLP (c))
6399 error ("Key definition is invalid");
6401 /* Work out the base key and the modifiers. */
6402 if (SYMBOLP (c))
6404 c = parse_modifiers (c);
6405 lisp_modifiers = XINT (Fcar (Fcdr (c)));
6406 c = Fcar (c);
6407 if (!SYMBOLP (c))
6408 abort ();
6409 vk_code = lookup_vk_code (SDATA (SYMBOL_NAME (c)));
6411 else if (INTEGERP (c))
6413 lisp_modifiers = XINT (c) & ~CHARACTERBITS;
6414 /* Many ascii characters are their own virtual key code. */
6415 vk_code = XINT (c) & CHARACTERBITS;
6418 if (vk_code < 0 || vk_code > 255)
6419 return Qnil;
6421 if ((lisp_modifiers & meta_modifier) != 0
6422 && !NILP (Vw32_alt_is_meta))
6423 lisp_modifiers |= alt_modifier;
6425 /* Supply defs missing from mingw32. */
6426 #ifndef MOD_ALT
6427 #define MOD_ALT 0x0001
6428 #define MOD_CONTROL 0x0002
6429 #define MOD_SHIFT 0x0004
6430 #define MOD_WIN 0x0008
6431 #endif
6433 /* Convert lisp modifiers to Windows hot-key form. */
6434 w32_modifiers = (lisp_modifiers & hyper_modifier) ? MOD_WIN : 0;
6435 w32_modifiers |= (lisp_modifiers & alt_modifier) ? MOD_ALT : 0;
6436 w32_modifiers |= (lisp_modifiers & ctrl_modifier) ? MOD_CONTROL : 0;
6437 w32_modifiers |= (lisp_modifiers & shift_modifier) ? MOD_SHIFT : 0;
6439 return HOTKEY (vk_code, w32_modifiers);
6442 DEFUN ("w32-register-hot-key", Fw32_register_hot_key,
6443 Sw32_register_hot_key, 1, 1, 0,
6444 doc: /* Register KEY as a hot-key combination.
6445 Certain key combinations like Alt-Tab are reserved for system use on
6446 Windows, and therefore are normally intercepted by the system. However,
6447 most of these key combinations can be received by registering them as
6448 hot-keys, overriding their special meaning.
6450 KEY must be a one element key definition in vector form that would be
6451 acceptable to `define-key' (e.g. [A-tab] for Alt-Tab). The meta
6452 modifier is interpreted as Alt if `w32-alt-is-meta' is t, and hyper
6453 is always interpreted as the Windows modifier keys.
6455 The return value is the hotkey-id if registered, otherwise nil. */)
6456 (key)
6457 Lisp_Object key;
6459 key = w32_parse_hot_key (key);
6461 if (!NILP (key) && NILP (Fmemq (key, w32_grabbed_keys)))
6463 /* Reuse an empty slot if possible. */
6464 Lisp_Object item = Fmemq (Qnil, w32_grabbed_keys);
6466 /* Safe to add new key to list, even if we have focus. */
6467 if (NILP (item))
6468 w32_grabbed_keys = Fcons (key, w32_grabbed_keys);
6469 else
6470 XSETCAR (item, key);
6472 /* Notify input thread about new hot-key definition, so that it
6473 takes effect without needing to switch focus. */
6474 #ifdef USE_LISP_UNION_TYPE
6475 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
6476 (WPARAM) key.i, 0);
6477 #else
6478 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
6479 (WPARAM) key, 0);
6480 #endif
6483 return key;
6486 DEFUN ("w32-unregister-hot-key", Fw32_unregister_hot_key,
6487 Sw32_unregister_hot_key, 1, 1, 0,
6488 doc: /* Unregister KEY as a hot-key combination. */)
6489 (key)
6490 Lisp_Object key;
6492 Lisp_Object item;
6494 if (!INTEGERP (key))
6495 key = w32_parse_hot_key (key);
6497 item = Fmemq (key, w32_grabbed_keys);
6499 if (!NILP (item))
6501 /* Notify input thread about hot-key definition being removed, so
6502 that it takes effect without needing focus switch. */
6503 #ifdef USE_LISP_UNION_TYPE
6504 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
6505 (WPARAM) XINT (XCAR (item)), (LPARAM) item.i))
6506 #else
6507 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
6508 (WPARAM) XINT (XCAR (item)), (LPARAM) item))
6509 #endif
6511 MSG msg;
6512 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
6514 return Qt;
6516 return Qnil;
6519 DEFUN ("w32-registered-hot-keys", Fw32_registered_hot_keys,
6520 Sw32_registered_hot_keys, 0, 0, 0,
6521 doc: /* Return list of registered hot-key IDs. */)
6524 return Fdelq (Qnil, Fcopy_sequence (w32_grabbed_keys));
6527 DEFUN ("w32-reconstruct-hot-key", Fw32_reconstruct_hot_key,
6528 Sw32_reconstruct_hot_key, 1, 1, 0,
6529 doc: /* Convert hot-key ID to a lisp key combination.
6530 usage: (w32-reconstruct-hot-key ID) */)
6531 (hotkeyid)
6532 Lisp_Object hotkeyid;
6534 int vk_code, w32_modifiers;
6535 Lisp_Object key;
6537 CHECK_NUMBER (hotkeyid);
6539 vk_code = HOTKEY_VK_CODE (hotkeyid);
6540 w32_modifiers = HOTKEY_MODIFIERS (hotkeyid);
6542 if (vk_code < 256 && lispy_function_keys[vk_code])
6543 key = intern (lispy_function_keys[vk_code]);
6544 else
6545 key = make_number (vk_code);
6547 key = Fcons (key, Qnil);
6548 if (w32_modifiers & MOD_SHIFT)
6549 key = Fcons (Qshift, key);
6550 if (w32_modifiers & MOD_CONTROL)
6551 key = Fcons (Qctrl, key);
6552 if (w32_modifiers & MOD_ALT)
6553 key = Fcons (NILP (Vw32_alt_is_meta) ? Qalt : Qmeta, key);
6554 if (w32_modifiers & MOD_WIN)
6555 key = Fcons (Qhyper, key);
6557 return key;
6560 DEFUN ("w32-toggle-lock-key", Fw32_toggle_lock_key,
6561 Sw32_toggle_lock_key, 1, 2, 0,
6562 doc: /* Toggle the state of the lock key KEY.
6563 KEY can be `capslock', `kp-numlock', or `scroll'.
6564 If the optional parameter NEW-STATE is a number, then the state of KEY
6565 is set to off if the low bit of NEW-STATE is zero, otherwise on. */)
6566 (key, new_state)
6567 Lisp_Object key, new_state;
6569 int vk_code;
6571 if (EQ (key, intern ("capslock")))
6572 vk_code = VK_CAPITAL;
6573 else if (EQ (key, intern ("kp-numlock")))
6574 vk_code = VK_NUMLOCK;
6575 else if (EQ (key, intern ("scroll")))
6576 vk_code = VK_SCROLL;
6577 else
6578 return Qnil;
6580 if (!dwWindowsThreadId)
6581 return make_number (w32_console_toggle_lock_key (vk_code, new_state));
6583 #ifdef USE_LISP_UNION_TYPE
6584 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
6585 (WPARAM) vk_code, (LPARAM) new_state.i))
6586 #else
6587 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
6588 (WPARAM) vk_code, (LPARAM) new_state))
6589 #endif
6591 MSG msg;
6592 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
6593 return make_number (msg.wParam);
6595 return Qnil;
6598 DEFUN ("w32-window-exists-p", Fw32_window_exists_p, Sw32_window_exists_p,
6599 2, 2, 0,
6600 doc: /* Return non-nil if a window exists with the specified CLASS and NAME.
6602 This is a direct interface to the Windows API FindWindow function. */)
6603 (class, name)
6604 Lisp_Object class, name;
6606 HWND hnd;
6608 if (!NILP (class))
6609 CHECK_STRING (class);
6610 if (!NILP (name))
6611 CHECK_STRING (name);
6613 hnd = FindWindow (STRINGP (class) ? ((LPCTSTR) SDATA (class)) : NULL,
6614 STRINGP (name) ? ((LPCTSTR) SDATA (name)) : NULL);
6615 if (!hnd)
6616 return Qnil;
6617 return Qt;
6620 DEFUN ("w32-battery-status", Fw32_battery_status, Sw32_battery_status, 0, 0, 0,
6621 doc: /* Get power status information from Windows system.
6623 The following %-sequences are provided:
6624 %L AC line status (verbose)
6625 %B Battery status (verbose)
6626 %b Battery status, empty means high, `-' means low,
6627 `!' means critical, and `+' means charging
6628 %p Battery load percentage
6629 %s Remaining time (to charge or discharge) in seconds
6630 %m Remaining time (to charge or discharge) in minutes
6631 %h Remaining time (to charge or discharge) in hours
6632 %t Remaining time (to charge or discharge) in the form `h:min' */)
6635 Lisp_Object status = Qnil;
6637 SYSTEM_POWER_STATUS system_status;
6638 if (GetSystemPowerStatus (&system_status))
6640 Lisp_Object line_status, battery_status, battery_status_symbol;
6641 Lisp_Object load_percentage, seconds, minutes, hours, remain;
6642 Lisp_Object sequences[8];
6644 long seconds_left = (long) system_status.BatteryLifeTime;
6646 if (system_status.ACLineStatus == 0)
6647 line_status = build_string ("off-line");
6648 else if (system_status.ACLineStatus == 1)
6649 line_status = build_string ("on-line");
6650 else
6651 line_status = build_string ("N/A");
6653 if (system_status.BatteryFlag & 128)
6655 battery_status = build_string ("N/A");
6656 battery_status_symbol = empty_unibyte_string;
6658 else if (system_status.BatteryFlag & 8)
6660 battery_status = build_string ("charging");
6661 battery_status_symbol = build_string ("+");
6662 if (system_status.BatteryFullLifeTime != -1L)
6663 seconds_left = system_status.BatteryFullLifeTime - seconds_left;
6665 else if (system_status.BatteryFlag & 4)
6667 battery_status = build_string ("critical");
6668 battery_status_symbol = build_string ("!");
6670 else if (system_status.BatteryFlag & 2)
6672 battery_status = build_string ("low");
6673 battery_status_symbol = build_string ("-");
6675 else if (system_status.BatteryFlag & 1)
6677 battery_status = build_string ("high");
6678 battery_status_symbol = empty_unibyte_string;
6680 else
6682 battery_status = build_string ("medium");
6683 battery_status_symbol = empty_unibyte_string;
6686 if (system_status.BatteryLifePercent > 100)
6687 load_percentage = build_string ("N/A");
6688 else
6690 char buffer[16];
6691 _snprintf (buffer, 16, "%d", system_status.BatteryLifePercent);
6692 load_percentage = build_string (buffer);
6695 if (seconds_left < 0)
6696 seconds = minutes = hours = remain = build_string ("N/A");
6697 else
6699 long m;
6700 float h;
6701 char buffer[16];
6702 _snprintf (buffer, 16, "%ld", seconds_left);
6703 seconds = build_string (buffer);
6705 m = seconds_left / 60;
6706 _snprintf (buffer, 16, "%ld", m);
6707 minutes = build_string (buffer);
6709 h = seconds_left / 3600.0;
6710 _snprintf (buffer, 16, "%3.1f", h);
6711 hours = build_string (buffer);
6713 _snprintf (buffer, 16, "%ld:%02ld", m / 60, m % 60);
6714 remain = build_string (buffer);
6716 sequences[0] = Fcons (make_number ('L'), line_status);
6717 sequences[1] = Fcons (make_number ('B'), battery_status);
6718 sequences[2] = Fcons (make_number ('b'), battery_status_symbol);
6719 sequences[3] = Fcons (make_number ('p'), load_percentage);
6720 sequences[4] = Fcons (make_number ('s'), seconds);
6721 sequences[5] = Fcons (make_number ('m'), minutes);
6722 sequences[6] = Fcons (make_number ('h'), hours);
6723 sequences[7] = Fcons (make_number ('t'), remain);
6725 status = Flist (8, sequences);
6727 return status;
6731 DEFUN ("file-system-info", Ffile_system_info, Sfile_system_info, 1, 1, 0,
6732 doc: /* Return storage information about the file system FILENAME is on.
6733 Value is a list of floats (TOTAL FREE AVAIL), where TOTAL is the total
6734 storage of the file system, FREE is the free storage, and AVAIL is the
6735 storage available to a non-superuser. All 3 numbers are in bytes.
6736 If the underlying system call fails, value is nil. */)
6737 (filename)
6738 Lisp_Object filename;
6740 Lisp_Object encoded, value;
6742 CHECK_STRING (filename);
6743 filename = Fexpand_file_name (filename, Qnil);
6744 encoded = ENCODE_FILE (filename);
6746 value = Qnil;
6748 /* Determining the required information on Windows turns out, sadly,
6749 to be more involved than one would hope. The original Win32 api
6750 call for this will return bogus information on some systems, but we
6751 must dynamically probe for the replacement api, since that was
6752 added rather late on. */
6754 HMODULE hKernel = GetModuleHandle ("kernel32");
6755 BOOL (*pfn_GetDiskFreeSpaceEx)
6756 (char *, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER)
6757 = (void *) GetProcAddress (hKernel, "GetDiskFreeSpaceEx");
6759 /* On Windows, we may need to specify the root directory of the
6760 volume holding FILENAME. */
6761 char rootname[MAX_PATH];
6762 char *name = SDATA (encoded);
6764 /* find the root name of the volume if given */
6765 if (isalpha (name[0]) && name[1] == ':')
6767 rootname[0] = name[0];
6768 rootname[1] = name[1];
6769 rootname[2] = '\\';
6770 rootname[3] = 0;
6772 else if (IS_DIRECTORY_SEP (name[0]) && IS_DIRECTORY_SEP (name[1]))
6774 char *str = rootname;
6775 int slashes = 4;
6778 if (IS_DIRECTORY_SEP (*name) && --slashes == 0)
6779 break;
6780 *str++ = *name++;
6782 while ( *name );
6784 *str++ = '\\';
6785 *str = 0;
6788 if (pfn_GetDiskFreeSpaceEx)
6790 /* Unsigned large integers cannot be cast to double, so
6791 use signed ones instead. */
6792 LARGE_INTEGER availbytes;
6793 LARGE_INTEGER freebytes;
6794 LARGE_INTEGER totalbytes;
6796 if (pfn_GetDiskFreeSpaceEx (rootname,
6797 (ULARGE_INTEGER *)&availbytes,
6798 (ULARGE_INTEGER *)&totalbytes,
6799 (ULARGE_INTEGER *)&freebytes))
6800 value = list3 (make_float ((double) totalbytes.QuadPart),
6801 make_float ((double) freebytes.QuadPart),
6802 make_float ((double) availbytes.QuadPart));
6804 else
6806 DWORD sectors_per_cluster;
6807 DWORD bytes_per_sector;
6808 DWORD free_clusters;
6809 DWORD total_clusters;
6811 if (GetDiskFreeSpace (rootname,
6812 &sectors_per_cluster,
6813 &bytes_per_sector,
6814 &free_clusters,
6815 &total_clusters))
6816 value = list3 (make_float ((double) total_clusters
6817 * sectors_per_cluster * bytes_per_sector),
6818 make_float ((double) free_clusters
6819 * sectors_per_cluster * bytes_per_sector),
6820 make_float ((double) free_clusters
6821 * sectors_per_cluster * bytes_per_sector));
6825 return value;
6828 DEFUN ("default-printer-name", Fdefault_printer_name, Sdefault_printer_name,
6829 0, 0, 0, doc: /* Return the name of Windows default printer device. */)
6832 static char pname_buf[256];
6833 int err;
6834 HANDLE hPrn;
6835 PRINTER_INFO_2 *ppi2 = NULL;
6836 DWORD dwNeeded = 0, dwReturned = 0;
6838 /* Retrieve the default string from Win.ini (the registry).
6839 * String will be in form "printername,drivername,portname".
6840 * This is the most portable way to get the default printer. */
6841 if (GetProfileString ("windows", "device", ",,", pname_buf, sizeof (pname_buf)) <= 0)
6842 return Qnil;
6843 /* printername precedes first "," character */
6844 strtok (pname_buf, ",");
6845 /* We want to know more than the printer name */
6846 if (!OpenPrinter (pname_buf, &hPrn, NULL))
6847 return Qnil;
6848 GetPrinter (hPrn, 2, NULL, 0, &dwNeeded);
6849 if (dwNeeded == 0)
6851 ClosePrinter (hPrn);
6852 return Qnil;
6854 /* Allocate memory for the PRINTER_INFO_2 struct */
6855 ppi2 = (PRINTER_INFO_2 *) xmalloc (dwNeeded);
6856 if (!ppi2)
6858 ClosePrinter (hPrn);
6859 return Qnil;
6861 /* Call GetPrinter again with big enouth memory block */
6862 err = GetPrinter (hPrn, 2, (LPBYTE)ppi2, dwNeeded, &dwReturned);
6863 ClosePrinter (hPrn);
6864 if (!err)
6866 xfree (ppi2);
6867 return Qnil;
6870 if (ppi2)
6872 if (ppi2->Attributes & PRINTER_ATTRIBUTE_SHARED && ppi2->pServerName)
6874 /* a remote printer */
6875 if (*ppi2->pServerName == '\\')
6876 _snprintf (pname_buf, sizeof (pname_buf), "%s\\%s", ppi2->pServerName,
6877 ppi2->pShareName);
6878 else
6879 _snprintf (pname_buf, sizeof (pname_buf), "\\\\%s\\%s", ppi2->pServerName,
6880 ppi2->pShareName);
6881 pname_buf[sizeof (pname_buf) - 1] = '\0';
6883 else
6885 /* a local printer */
6886 strncpy (pname_buf, ppi2->pPortName, sizeof (pname_buf));
6887 pname_buf[sizeof (pname_buf) - 1] = '\0';
6888 /* `pPortName' can include several ports, delimited by ','.
6889 * we only use the first one. */
6890 strtok (pname_buf, ",");
6892 xfree (ppi2);
6895 return build_string (pname_buf);
6898 /***********************************************************************
6899 Initialization
6900 ***********************************************************************/
6902 /* Keep this list in the same order as frame_parms in frame.c.
6903 Use 0 for unsupported frame parameters. */
6905 frame_parm_handler w32_frame_parm_handlers[] =
6907 x_set_autoraise,
6908 x_set_autolower,
6909 x_set_background_color,
6910 x_set_border_color,
6911 x_set_border_width,
6912 x_set_cursor_color,
6913 x_set_cursor_type,
6914 x_set_font,
6915 x_set_foreground_color,
6916 x_set_icon_name,
6917 x_set_icon_type,
6918 x_set_internal_border_width,
6919 x_set_menu_bar_lines,
6920 x_set_mouse_color,
6921 x_explicitly_set_name,
6922 x_set_scroll_bar_width,
6923 x_set_title,
6924 x_set_unsplittable,
6925 x_set_vertical_scroll_bars,
6926 x_set_visibility,
6927 x_set_tool_bar_lines,
6928 0, /* x_set_scroll_bar_foreground, */
6929 0, /* x_set_scroll_bar_background, */
6930 x_set_screen_gamma,
6931 x_set_line_spacing,
6932 x_set_fringe_width,
6933 x_set_fringe_width,
6934 0, /* x_set_wait_for_wm, */
6935 x_set_fullscreen,
6936 x_set_font_backend,
6937 x_set_alpha,
6938 0, /* x_set_sticky */
6941 void
6942 syms_of_w32fns ()
6944 globals_of_w32fns ();
6945 /* This is zero if not using MS-Windows. */
6946 w32_in_use = 0;
6947 track_mouse_window = NULL;
6949 w32_visible_system_caret_hwnd = NULL;
6951 DEFSYM (Qnone, "none");
6952 DEFSYM (Qsuppress_icon, "suppress-icon");
6953 DEFSYM (Qundefined_color, "undefined-color");
6954 DEFSYM (Qcancel_timer, "cancel-timer");
6955 DEFSYM (Qhyper, "hyper");
6956 DEFSYM (Qsuper, "super");
6957 DEFSYM (Qmeta, "meta");
6958 DEFSYM (Qalt, "alt");
6959 DEFSYM (Qctrl, "ctrl");
6960 DEFSYM (Qcontrol, "control");
6961 DEFSYM (Qshift, "shift");
6962 DEFSYM (Qfont_param, "font-parameter");
6963 /* This is the end of symbol initialization. */
6965 /* Text property `display' should be nonsticky by default. */
6966 Vtext_property_default_nonsticky
6967 = Fcons (Fcons (Qdisplay, Qt), Vtext_property_default_nonsticky);
6970 Fput (Qundefined_color, Qerror_conditions,
6971 pure_cons (Qundefined_color, pure_cons (Qerror, Qnil)));
6972 Fput (Qundefined_color, Qerror_message,
6973 make_pure_c_string ("Undefined color"));
6975 staticpro (&w32_grabbed_keys);
6976 w32_grabbed_keys = Qnil;
6978 DEFVAR_LISP ("w32-color-map", &Vw32_color_map,
6979 doc: /* An array of color name mappings for Windows. */);
6980 Vw32_color_map = Qnil;
6982 DEFVAR_LISP ("w32-pass-alt-to-system", &Vw32_pass_alt_to_system,
6983 doc: /* Non-nil if Alt key presses are passed on to Windows.
6984 When non-nil, for example, Alt pressed and released and then space will
6985 open the System menu. When nil, Emacs processes the Alt key events, and
6986 then silently swallows them. */);
6987 Vw32_pass_alt_to_system = Qnil;
6989 DEFVAR_LISP ("w32-alt-is-meta", &Vw32_alt_is_meta,
6990 doc: /* Non-nil if the Alt key is to be considered the same as the META key.
6991 When nil, Emacs will translate the Alt key to the ALT modifier, not to META. */);
6992 Vw32_alt_is_meta = Qt;
6994 DEFVAR_INT ("w32-quit-key", &w32_quit_key,
6995 doc: /* If non-zero, the virtual key code for an alternative quit key. */);
6996 w32_quit_key = 0;
6998 DEFVAR_LISP ("w32-pass-lwindow-to-system",
6999 &Vw32_pass_lwindow_to_system,
7000 doc: /* If non-nil, the left \"Windows\" key is passed on to Windows.
7002 When non-nil, the Start menu is opened by tapping the key.
7003 If you set this to nil, the left \"Windows\" key is processed by Emacs
7004 according to the value of `w32-lwindow-modifier', which see.
7006 Note that some combinations of the left \"Windows\" key with other keys are
7007 caught by Windows at low level, and so binding them in Emacs will have no
7008 effect. For example, <lwindow>-r always pops up the Windows Run dialog,
7009 <lwindow>-<Pause> pops up the "System Properties" dialog, etc. However, see
7010 the doc string of `w32-phantom-key-code'. */);
7011 Vw32_pass_lwindow_to_system = Qt;
7013 DEFVAR_LISP ("w32-pass-rwindow-to-system",
7014 &Vw32_pass_rwindow_to_system,
7015 doc: /* If non-nil, the right \"Windows\" key is passed on to Windows.
7017 When non-nil, the Start menu is opened by tapping the key.
7018 If you set this to nil, the right \"Windows\" key is processed by Emacs
7019 according to the value of `w32-rwindow-modifier', which see.
7021 Note that some combinations of the right \"Windows\" key with other keys are
7022 caught by Windows at low level, and so binding them in Emacs will have no
7023 effect. For example, <rwindow>-r always pops up the Windows Run dialog,
7024 <rwindow>-<Pause> pops up the "System Properties" dialog, etc. However, see
7025 the doc string of `w32-phantom-key-code'. */);
7026 Vw32_pass_rwindow_to_system = Qt;
7028 DEFVAR_LISP ("w32-phantom-key-code",
7029 &Vw32_phantom_key_code,
7030 doc: /* Virtual key code used to generate \"phantom\" key presses.
7031 Value is a number between 0 and 255.
7033 Phantom key presses are generated in order to stop the system from
7034 acting on \"Windows\" key events when `w32-pass-lwindow-to-system' or
7035 `w32-pass-rwindow-to-system' is nil. */);
7036 /* Although 255 is technically not a valid key code, it works and
7037 means that this hack won't interfere with any real key code. */
7038 XSETINT (Vw32_phantom_key_code, 255);
7040 DEFVAR_LISP ("w32-enable-num-lock",
7041 &Vw32_enable_num_lock,
7042 doc: /* If non-nil, the Num Lock key acts normally.
7043 Set to nil to handle Num Lock as the `kp-numlock' key. */);
7044 Vw32_enable_num_lock = Qt;
7046 DEFVAR_LISP ("w32-enable-caps-lock",
7047 &Vw32_enable_caps_lock,
7048 doc: /* If non-nil, the Caps Lock key acts normally.
7049 Set to nil to handle Caps Lock as the `capslock' key. */);
7050 Vw32_enable_caps_lock = Qt;
7052 DEFVAR_LISP ("w32-scroll-lock-modifier",
7053 &Vw32_scroll_lock_modifier,
7054 doc: /* Modifier to use for the Scroll Lock ON state.
7055 The value can be hyper, super, meta, alt, control or shift for the
7056 respective modifier, or nil to handle Scroll Lock as the `scroll' key.
7057 Any other value will cause the Scroll Lock key to be ignored. */);
7058 Vw32_scroll_lock_modifier = Qnil;
7060 DEFVAR_LISP ("w32-lwindow-modifier",
7061 &Vw32_lwindow_modifier,
7062 doc: /* Modifier to use for the left \"Windows\" key.
7063 The value can be hyper, super, meta, alt, control or shift for the
7064 respective modifier, or nil to appear as the `lwindow' key.
7065 Any other value will cause the key to be ignored. */);
7066 Vw32_lwindow_modifier = Qnil;
7068 DEFVAR_LISP ("w32-rwindow-modifier",
7069 &Vw32_rwindow_modifier,
7070 doc: /* Modifier to use for the right \"Windows\" key.
7071 The value can be hyper, super, meta, alt, control or shift for the
7072 respective modifier, or nil to appear as the `rwindow' key.
7073 Any other value will cause the key to be ignored. */);
7074 Vw32_rwindow_modifier = Qnil;
7076 DEFVAR_LISP ("w32-apps-modifier",
7077 &Vw32_apps_modifier,
7078 doc: /* Modifier to use for the \"Apps\" key.
7079 The value can be hyper, super, meta, alt, control or shift for the
7080 respective modifier, or nil to appear as the `apps' key.
7081 Any other value will cause the key to be ignored. */);
7082 Vw32_apps_modifier = Qnil;
7084 DEFVAR_BOOL ("w32-enable-synthesized-fonts", &w32_enable_synthesized_fonts,
7085 doc: /* Non-nil enables selection of artificially italicized and bold fonts. */);
7086 w32_enable_synthesized_fonts = 0;
7088 DEFVAR_LISP ("w32-enable-palette", &Vw32_enable_palette,
7089 doc: /* Non-nil enables Windows palette management to map colors exactly. */);
7090 Vw32_enable_palette = Qt;
7092 DEFVAR_INT ("w32-mouse-button-tolerance",
7093 &w32_mouse_button_tolerance,
7094 doc: /* Analogue of double click interval for faking middle mouse events.
7095 The value is the minimum time in milliseconds that must elapse between
7096 left and right button down events before they are considered distinct events.
7097 If both mouse buttons are depressed within this interval, a middle mouse
7098 button down event is generated instead. */);
7099 w32_mouse_button_tolerance = GetDoubleClickTime () / 2;
7101 DEFVAR_INT ("w32-mouse-move-interval",
7102 &w32_mouse_move_interval,
7103 doc: /* Minimum interval between mouse move events.
7104 The value is the minimum time in milliseconds that must elapse between
7105 successive mouse move (or scroll bar drag) events before they are
7106 reported as lisp events. */);
7107 w32_mouse_move_interval = 0;
7109 DEFVAR_BOOL ("w32-pass-extra-mouse-buttons-to-system",
7110 &w32_pass_extra_mouse_buttons_to_system,
7111 doc: /* If non-nil, the fourth and fifth mouse buttons are passed to Windows.
7112 Recent versions of Windows support mice with up to five buttons.
7113 Since most applications don't support these extra buttons, most mouse
7114 drivers will allow you to map them to functions at the system level.
7115 If this variable is non-nil, Emacs will pass them on, allowing the
7116 system to handle them. */);
7117 w32_pass_extra_mouse_buttons_to_system = 0;
7119 DEFVAR_BOOL ("w32-pass-multimedia-buttons-to-system",
7120 &w32_pass_multimedia_buttons_to_system,
7121 doc: /* If non-nil, media buttons are passed to Windows.
7122 Some modern keyboards contain buttons for controlling media players, web
7123 browsers and other applications. Generally these buttons are handled on a
7124 system wide basis, but by setting this to nil they are made available
7125 to Emacs for binding. Depending on your keyboard, additional keys that
7126 may be available are:
7128 browser-back, browser-forward, browser-refresh, browser-stop,
7129 browser-search, browser-favorites, browser-home,
7130 mail, mail-reply, mail-forward, mail-send,
7131 app-1, app-2,
7132 help, find, new, open, close, save, print, undo, redo, copy, cut, paste,
7133 spell-check, correction-list, toggle-dictate-command,
7134 media-next, media-previous, media-stop, media-play-pause, media-select,
7135 media-play, media-pause, media-record, media-fast-forward, media-rewind,
7136 media-channel-up, media-channel-down,
7137 volume-mute, volume-up, volume-down,
7138 mic-volume-mute, mic-volume-down, mic-volume-up, mic-toggle,
7139 bass-down, bass-boost, bass-up, treble-down, treble-up */);
7140 w32_pass_multimedia_buttons_to_system = 1;
7142 #if 0 /* TODO: Mouse cursor customization. */
7143 DEFVAR_LISP ("x-pointer-shape", &Vx_pointer_shape,
7144 doc: /* The shape of the pointer when over text.
7145 Changing the value does not affect existing frames
7146 unless you set the mouse color. */);
7147 Vx_pointer_shape = Qnil;
7149 Vx_nontext_pointer_shape = Qnil;
7151 Vx_mode_pointer_shape = Qnil;
7153 DEFVAR_LISP ("x-hourglass-pointer-shape", &Vx_hourglass_pointer_shape,
7154 doc: /* The shape of the pointer when Emacs is busy.
7155 This variable takes effect when you create a new frame
7156 or when you set the mouse color. */);
7157 Vx_hourglass_pointer_shape = Qnil;
7159 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
7160 &Vx_sensitive_text_pointer_shape,
7161 doc: /* The shape of the pointer when over mouse-sensitive text.
7162 This variable takes effect when you create a new frame
7163 or when you set the mouse color. */);
7164 Vx_sensitive_text_pointer_shape = Qnil;
7166 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
7167 &Vx_window_horizontal_drag_shape,
7168 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
7169 This variable takes effect when you create a new frame
7170 or when you set the mouse color. */);
7171 Vx_window_horizontal_drag_shape = Qnil;
7172 #endif
7174 DEFVAR_LISP ("x-cursor-fore-pixel", &Vx_cursor_fore_pixel,
7175 doc: /* A string indicating the foreground color of the cursor box. */);
7176 Vx_cursor_fore_pixel = Qnil;
7178 DEFVAR_LISP ("x-max-tooltip-size", &Vx_max_tooltip_size,
7179 doc: /* Maximum size for tooltips.
7180 Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
7181 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
7183 DEFVAR_LISP ("x-no-window-manager", &Vx_no_window_manager,
7184 doc: /* Non-nil if no window manager is in use.
7185 Emacs doesn't try to figure this out; this is always nil
7186 unless you set it to something else. */);
7187 /* We don't have any way to find this out, so set it to nil
7188 and maybe the user would like to set it to t. */
7189 Vx_no_window_manager = Qnil;
7191 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
7192 &Vx_pixel_size_width_font_regexp,
7193 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
7195 Since Emacs gets width of a font matching with this regexp from
7196 PIXEL_SIZE field of the name, font finding mechanism gets faster for
7197 such a font. This is especially effective for such large fonts as
7198 Chinese, Japanese, and Korean. */);
7199 Vx_pixel_size_width_font_regexp = Qnil;
7201 DEFVAR_LISP ("w32-bdf-filename-alist",
7202 &Vw32_bdf_filename_alist,
7203 doc: /* List of bdf fonts and their corresponding filenames. */);
7204 Vw32_bdf_filename_alist = Qnil;
7206 DEFVAR_BOOL ("w32-strict-fontnames",
7207 &w32_strict_fontnames,
7208 doc: /* Non-nil means only use fonts that are exact matches for those requested.
7209 Default is nil, which allows old fontnames that are not XLFD compliant,
7210 and allows third-party CJK display to work by specifying false charset
7211 fields to trick Emacs into translating to Big5, SJIS etc.
7212 Setting this to t will prevent wrong fonts being selected when
7213 fontsets are automatically created. */);
7214 w32_strict_fontnames = 0;
7216 DEFVAR_BOOL ("w32-strict-painting",
7217 &w32_strict_painting,
7218 doc: /* Non-nil means use strict rules for repainting frames.
7219 Set this to nil to get the old behavior for repainting; this should
7220 only be necessary if the default setting causes problems. */);
7221 w32_strict_painting = 1;
7223 #if 0 /* TODO: Port to W32 */
7224 defsubr (&Sx_change_window_property);
7225 defsubr (&Sx_delete_window_property);
7226 defsubr (&Sx_window_property);
7227 #endif
7228 defsubr (&Sxw_display_color_p);
7229 defsubr (&Sx_display_grayscale_p);
7230 defsubr (&Sxw_color_defined_p);
7231 defsubr (&Sxw_color_values);
7232 defsubr (&Sx_server_max_request_size);
7233 defsubr (&Sx_server_vendor);
7234 defsubr (&Sx_server_version);
7235 defsubr (&Sx_display_pixel_width);
7236 defsubr (&Sx_display_pixel_height);
7237 defsubr (&Sx_display_mm_width);
7238 defsubr (&Sx_display_mm_height);
7239 defsubr (&Sx_display_screens);
7240 defsubr (&Sx_display_planes);
7241 defsubr (&Sx_display_color_cells);
7242 defsubr (&Sx_display_visual_class);
7243 defsubr (&Sx_display_backing_store);
7244 defsubr (&Sx_display_save_under);
7245 defsubr (&Sx_create_frame);
7246 defsubr (&Sx_open_connection);
7247 defsubr (&Sx_close_connection);
7248 defsubr (&Sx_display_list);
7249 defsubr (&Sx_synchronize);
7250 defsubr (&Sx_focus_frame);
7252 /* W32 specific functions */
7254 defsubr (&Sw32_define_rgb_color);
7255 defsubr (&Sw32_default_color_map);
7256 defsubr (&Sw32_send_sys_command);
7257 defsubr (&Sw32_shell_execute);
7258 defsubr (&Sw32_register_hot_key);
7259 defsubr (&Sw32_unregister_hot_key);
7260 defsubr (&Sw32_registered_hot_keys);
7261 defsubr (&Sw32_reconstruct_hot_key);
7262 defsubr (&Sw32_toggle_lock_key);
7263 defsubr (&Sw32_window_exists_p);
7264 defsubr (&Sw32_battery_status);
7266 defsubr (&Sfile_system_info);
7267 defsubr (&Sdefault_printer_name);
7269 check_window_system_func = check_w32;
7272 hourglass_timer = 0;
7273 hourglass_hwnd = NULL;
7275 defsubr (&Sx_show_tip);
7276 defsubr (&Sx_hide_tip);
7277 tip_timer = Qnil;
7278 staticpro (&tip_timer);
7279 tip_frame = Qnil;
7280 staticpro (&tip_frame);
7282 last_show_tip_args = Qnil;
7283 staticpro (&last_show_tip_args);
7285 defsubr (&Sx_file_dialog);
7286 defsubr (&Ssystem_move_file_to_trash);
7291 globals_of_w32fns is used to initialize those global variables that
7292 must always be initialized on startup even when the global variable
7293 initialized is non zero (see the function main in emacs.c).
7294 globals_of_w32fns is called from syms_of_w32fns when the global
7295 variable initialized is 0 and directly from main when initialized
7296 is non zero.
7298 void
7299 globals_of_w32fns ()
7301 HMODULE user32_lib = GetModuleHandle ("user32.dll");
7303 TrackMouseEvent not available in all versions of Windows, so must load
7304 it dynamically. Do it once, here, instead of every time it is used.
7306 track_mouse_event_fn = (TrackMouseEvent_Proc)
7307 GetProcAddress (user32_lib, "TrackMouseEvent");
7308 /* ditto for GetClipboardSequenceNumber. */
7309 clipboard_sequence_fn = (ClipboardSequence_Proc)
7310 GetProcAddress (user32_lib, "GetClipboardSequenceNumber");
7312 monitor_from_point_fn = (MonitorFromPoint_Proc)
7313 GetProcAddress (user32_lib, "MonitorFromPoint");
7314 get_monitor_info_fn = (GetMonitorInfo_Proc)
7315 GetProcAddress (user32_lib, "GetMonitorInfoA");
7318 HMODULE imm32_lib = GetModuleHandle ("imm32.dll");
7319 get_composition_string_fn = (ImmGetCompositionString_Proc)
7320 GetProcAddress (imm32_lib, "ImmGetCompositionStringW");
7321 get_ime_context_fn = (ImmGetContext_Proc)
7322 GetProcAddress (imm32_lib, "ImmGetContext");
7323 release_ime_context_fn = (ImmReleaseContext_Proc)
7324 GetProcAddress (imm32_lib, "ImmReleaseContext");
7325 set_ime_composition_window_fn = (ImmSetCompositionWindow_Proc)
7326 GetProcAddress (imm32_lib, "ImmSetCompositionWindow");
7328 DEFVAR_INT ("w32-ansi-code-page",
7329 &w32_ansi_code_page,
7330 doc: /* The ANSI code page used by the system. */);
7331 w32_ansi_code_page = GetACP ();
7333 /* MessageBox does not work without this when linked to comctl32.dll 6.0. */
7334 InitCommonControls ();
7336 syms_of_w32uniscribe ();
7339 #undef abort
7341 void
7342 w32_abort ()
7344 int button;
7345 button = MessageBox (NULL,
7346 "A fatal error has occurred!\n\n"
7347 "Would you like to attach a debugger?\n\n"
7348 "Select YES to debug, NO to abort Emacs"
7349 #if __GNUC__
7350 "\n\n(type \"gdb -p <emacs-PID>\" and\n"
7351 "\"continue\" inside GDB before clicking YES.)"
7352 #endif
7353 , "Emacs Abort Dialog",
7354 MB_ICONEXCLAMATION | MB_TASKMODAL
7355 | MB_SETFOREGROUND | MB_YESNO);
7356 switch (button)
7358 case IDYES:
7359 DebugBreak ();
7360 exit (2); /* tell the compiler we will never return */
7361 case IDNO:
7362 default:
7363 abort ();
7364 break;
7368 /* For convenience when debugging. */
7370 w32_last_error ()
7372 return GetLastError ();
7375 /* arch-tag: 707589ab-b9be-4638-8cdd-74629cc9b446
7376 (do not change this comment) */