1 /* Graphical user interface functions for the Microsoft Windows API.
3 Copyright (C) 1989, 1992-2016 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 /* Added by Kevin Gallo */
23 /* Override API version to get the latest functionality. */
25 #define _WIN32_WINNT 0x0600
43 #include "blockinput.h"
46 #include "w32common.h"
51 #endif /* WINDOWSNT */
73 #ifndef FOF_NO_CONNECTED_ELEMENTS
74 #define FOF_NO_CONNECTED_ELEMENTS 0x2000
77 void syms_of_w32fns (void);
78 void globals_of_w32fns (void);
80 extern void free_frame_menubar (struct frame
*);
81 extern int w32_console_toggle_lock_key (int, Lisp_Object
);
82 extern void w32_menu_display_help (HWND
, HMENU
, UINT
, UINT
);
83 extern void w32_free_menu_strings (HWND
);
84 extern const char *map_w32_filename (const char *, const char **);
85 extern char * w32_strerror (int error_no
);
88 #define IDC_HAND MAKEINTRESOURCE(32649)
91 /* Prefix for system colors. */
92 #define SYSTEM_COLOR_PREFIX "System"
93 #define SYSTEM_COLOR_PREFIX_LEN (sizeof (SYSTEM_COLOR_PREFIX) - 1)
95 /* State variables for emulating a three button mouse. */
100 static int button_state
= 0;
101 static W32Msg saved_mouse_button_msg
;
102 static unsigned mouse_button_timer
= 0; /* non-zero when timer is active */
103 static W32Msg saved_mouse_move_msg
;
104 static unsigned mouse_move_timer
= 0;
106 /* Window that is tracking the mouse. */
107 static HWND track_mouse_window
;
109 /* Multi-monitor API definitions that are not pulled from the headers
110 since we are compiling for NT 4. */
111 #ifndef MONITOR_DEFAULT_TO_NEAREST
112 #define MONITOR_DEFAULT_TO_NEAREST 2
114 #ifndef MONITORINFOF_PRIMARY
115 #define MONITORINFOF_PRIMARY 1
117 #ifndef SM_XVIRTUALSCREEN
118 #define SM_XVIRTUALSCREEN 76
120 #ifndef SM_YVIRTUALSCREEN
121 #define SM_YVIRTUALSCREEN 77
123 /* MinGW headers define MONITORINFO unconditionally, but MSVC ones don't.
124 To avoid a compile error on one or the other, redefine with a new name. */
133 #if _WIN32_WINDOWS >= 0x0410
134 #define C_CHILDREN_TITLEBAR CCHILDREN_TITLEBAR
135 typedef TITLEBARINFO TITLEBAR_INFO
;
137 #define C_CHILDREN_TITLEBAR 5
142 DWORD rgstate
[C_CHILDREN_TITLEBAR
+1];
143 } TITLEBAR_INFO
, *PTITLEBAR_INFO
;
146 #ifndef CCHDEVICENAME
147 #define CCHDEVICENAME 32
149 struct MONITOR_INFO_EX
155 char szDevice
[CCHDEVICENAME
];
158 /* Reportedly, MSVC does not have this in its headers. */
159 #if defined (_MSC_VER) && _WIN32_WINNT < 0x0500
160 DECLARE_HANDLE(HMONITOR
);
163 typedef BOOL (WINAPI
* TrackMouseEvent_Proc
)
164 (IN OUT LPTRACKMOUSEEVENT lpEventTrack
);
165 typedef LONG (WINAPI
* ImmGetCompositionString_Proc
)
166 (IN HIMC context
, IN DWORD index
, OUT LPVOID buffer
, IN DWORD bufLen
);
167 typedef HIMC (WINAPI
* ImmGetContext_Proc
) (IN HWND window
);
168 typedef BOOL (WINAPI
* ImmReleaseContext_Proc
) (IN HWND wnd
, IN HIMC context
);
169 typedef BOOL (WINAPI
* ImmSetCompositionWindow_Proc
) (IN HIMC context
,
170 IN COMPOSITIONFORM
*form
);
171 typedef HMONITOR (WINAPI
* MonitorFromPoint_Proc
) (IN POINT pt
, IN DWORD flags
);
172 typedef BOOL (WINAPI
* GetMonitorInfo_Proc
)
173 (IN HMONITOR monitor
, OUT
struct MONITOR_INFO
* info
);
174 typedef HMONITOR (WINAPI
* MonitorFromWindow_Proc
)
175 (IN HWND hwnd
, IN DWORD dwFlags
);
176 typedef BOOL
CALLBACK (* MonitorEnum_Proc
)
177 (IN HMONITOR monitor
, IN HDC hdc
, IN RECT
*rcMonitor
, IN LPARAM dwData
);
178 typedef BOOL (WINAPI
* EnumDisplayMonitors_Proc
)
179 (IN HDC hdc
, IN RECT
*rcClip
, IN MonitorEnum_Proc fnEnum
, IN LPARAM dwData
);
180 typedef BOOL (WINAPI
* GetTitleBarInfo_Proc
)
181 (IN HWND hwnd
, OUT TITLEBAR_INFO
* info
);
183 TrackMouseEvent_Proc track_mouse_event_fn
= NULL
;
184 ImmGetCompositionString_Proc get_composition_string_fn
= NULL
;
185 ImmGetContext_Proc get_ime_context_fn
= NULL
;
186 ImmReleaseContext_Proc release_ime_context_fn
= NULL
;
187 ImmSetCompositionWindow_Proc set_ime_composition_window_fn
= NULL
;
188 MonitorFromPoint_Proc monitor_from_point_fn
= NULL
;
189 GetMonitorInfo_Proc get_monitor_info_fn
= NULL
;
190 MonitorFromWindow_Proc monitor_from_window_fn
= NULL
;
191 EnumDisplayMonitors_Proc enum_display_monitors_fn
= NULL
;
192 GetTitleBarInfo_Proc get_title_bar_info_fn
= NULL
;
195 #define unicode_append_menu AppendMenuW
196 #else /* !NTGUI_UNICODE */
197 extern AppendMenuW_Proc unicode_append_menu
;
198 #endif /* NTGUI_UNICODE */
200 /* Flag to selectively ignore WM_IME_CHAR messages. */
201 static int ignore_ime_char
= 0;
203 /* W95 mousewheel handler */
204 unsigned int msh_mousewheel
= 0;
207 #define MOUSE_BUTTON_ID 1
208 #define MOUSE_MOVE_ID 2
209 #define MENU_FREE_ID 3
210 /* The delay (milliseconds) before a menu is freed after WM_EXITMENULOOP
212 #define MENU_FREE_DELAY 1000
213 static unsigned menu_free_timer
= 0;
216 static ptrdiff_t image_cache_refcount
;
217 static int dpyinfo_refcount
;
220 static HWND w32_visible_system_caret_hwnd
;
222 static int w32_unicode_gui
;
225 extern HMENU current_popup_menu
;
226 int menubar_in_use
= 0;
228 /* From w32uniscribe.c */
229 extern void syms_of_w32uniscribe (void);
230 extern int uniscribe_available
;
233 /* From w32inevt.c */
234 extern int faked_key
;
235 #endif /* WINDOWSNT */
237 /* This gives us the page size and the size of the allocation unit on NT. */
238 SYSTEM_INFO sysinfo_cache
;
240 /* This gives us version, build, and platform identification. */
241 OSVERSIONINFO osinfo_cache
;
243 DWORD_PTR syspage_mask
= 0;
245 /* The major and minor versions of NT. */
246 int w32_major_version
;
247 int w32_minor_version
;
248 int w32_build_number
;
250 /* Distinguish between Windows NT and Windows 95. */
254 HINSTANCE hinst
= NULL
;
257 static unsigned int sound_type
= 0xFFFFFFFF;
258 #define MB_EMACS_SILENT (0xFFFFFFFF - 1)
260 /* Special virtual key code for indicating "any" key. */
263 #ifndef WM_WTSSESSION_CHANGE
264 /* 32-bit MinGW does not define these constants. */
265 # define WM_WTSSESSION_CHANGE 0x02B1
266 # define WTS_SESSION_LOCK 0x7
269 /* Keyboard hook state data. */
272 int hook_count
; /* counter, if several windows are created */
273 HHOOK hook
; /* hook handle */
274 HWND console
; /* console window handle */
276 int lwindown
; /* Left Windows key currently pressed (and hooked) */
277 int rwindown
; /* Right Windows key currently pressed (and hooked) */
278 int winsdown
; /* Number of handled keys currently pressed */
279 int send_win_up
; /* Pass through the keyup for this Windows key press? */
280 int suppress_lone
; /* Suppress simulated Windows keydown-keyup for this press? */
281 int winseen
; /* Windows keys seen during this press? */
283 char alt_hooked
[256]; /* hook Alt+[this key]? */
284 char lwin_hooked
[256]; /* hook left Win+[this key]? */
285 char rwin_hooked
[256]; /* hook right Win+[this key]? */
287 typedef HWND (WINAPI
*GetConsoleWindow_Proc
) (void);
289 /* stdin, from w32console.c */
290 extern HANDLE keyboard_handle
;
292 /* Let the user specify a display with a frame.
293 nil stands for the selected frame--or, if that is not a w32 frame,
294 the first display on the list. */
296 struct w32_display_info
*
297 check_x_display_info (Lisp_Object object
)
301 struct frame
*sf
= XFRAME (selected_frame
);
303 if (FRAME_W32_P (sf
) && FRAME_LIVE_P (sf
))
304 return FRAME_DISPLAY_INFO (sf
);
306 return &one_w32_display_info
;
308 else if (TERMINALP (object
))
310 struct terminal
*t
= decode_live_terminal (object
);
312 if (t
->type
!= output_w32
)
313 error ("Terminal %d is not a W32 display", t
->id
);
315 return t
->display_info
.w32
;
317 else if (STRINGP (object
))
318 return x_display_info_for_name (object
);
323 CHECK_LIVE_FRAME (object
);
325 if (! FRAME_W32_P (f
))
326 error ("Non-W32 frame used");
327 return FRAME_DISPLAY_INFO (f
);
331 /* Return the Emacs frame-object corresponding to an w32 window.
332 It could be the frame's main window or an icon window. */
335 x_window_to_frame (struct w32_display_info
*dpyinfo
, HWND wdesc
)
337 Lisp_Object tail
, frame
;
340 FOR_EACH_FRAME (tail
, frame
)
343 if (!FRAME_W32_P (f
) || FRAME_DISPLAY_INFO (f
) != dpyinfo
)
346 if (FRAME_W32_WINDOW (f
) == wdesc
)
353 static Lisp_Object
unwind_create_frame (Lisp_Object
);
354 static void unwind_create_tip_frame (Lisp_Object
);
355 static void my_create_window (struct frame
*);
356 static void my_create_tip_window (struct frame
*);
358 /* TODO: Native Input Method support; see x_create_im. */
359 void x_set_foreground_color (struct frame
*, Lisp_Object
, Lisp_Object
);
360 void x_set_background_color (struct frame
*, Lisp_Object
, Lisp_Object
);
361 void x_set_mouse_color (struct frame
*, Lisp_Object
, Lisp_Object
);
362 void x_set_cursor_color (struct frame
*, Lisp_Object
, Lisp_Object
);
363 void x_set_border_color (struct frame
*, Lisp_Object
, Lisp_Object
);
364 void x_set_cursor_type (struct frame
*, Lisp_Object
, Lisp_Object
);
365 void x_set_icon_type (struct frame
*, Lisp_Object
, Lisp_Object
);
366 void x_set_icon_name (struct frame
*, Lisp_Object
, Lisp_Object
);
367 void x_explicitly_set_name (struct frame
*, Lisp_Object
, Lisp_Object
);
368 void x_set_menu_bar_lines (struct frame
*, Lisp_Object
, Lisp_Object
);
369 void x_set_title (struct frame
*, Lisp_Object
, Lisp_Object
);
370 void x_set_tool_bar_lines (struct frame
*, Lisp_Object
, Lisp_Object
);
371 void x_set_internal_border_width (struct frame
*f
, Lisp_Object
, Lisp_Object
);
374 /* Store the screen positions of frame F into XPTR and YPTR.
375 These are the positions of the containing window manager window,
376 not Emacs's own window. */
379 x_real_positions (struct frame
*f
, int *xptr
, int *yptr
)
384 /* Get the bounds of the WM window. */
385 GetWindowRect (FRAME_W32_WINDOW (f
), &rect
);
390 /* Convert (0, 0) in the client area to screen co-ordinates. */
391 ClientToScreen (FRAME_W32_WINDOW (f
), &pt
);
397 /* Returns the window rectangle appropriate for the given fullscreen mode.
398 The normal rect parameter was the window's rectangle prior to entering
399 fullscreen mode. If multiple monitor support is available, the nearest
400 monitor to the window is chosen. */
403 w32_fullscreen_rect (HWND hwnd
, int fsmode
, RECT normal
, RECT
*rect
)
405 struct MONITOR_INFO mi
= { sizeof(mi
) };
406 if (monitor_from_window_fn
&& get_monitor_info_fn
)
409 monitor_from_window_fn (hwnd
, MONITOR_DEFAULT_TO_NEAREST
);
410 get_monitor_info_fn (monitor
, &mi
);
414 mi
.rcMonitor
.left
= 0;
415 mi
.rcMonitor
.top
= 0;
416 mi
.rcMonitor
.right
= GetSystemMetrics (SM_CXSCREEN
);
417 mi
.rcMonitor
.bottom
= GetSystemMetrics (SM_CYSCREEN
);
420 mi
.rcWork
.right
= GetSystemMetrics (SM_CXMAXIMIZED
);
421 mi
.rcWork
.bottom
= GetSystemMetrics (SM_CYMAXIMIZED
);
426 case FULLSCREEN_BOTH
:
427 rect
->left
= mi
.rcMonitor
.left
;
428 rect
->top
= mi
.rcMonitor
.top
;
429 rect
->right
= mi
.rcMonitor
.right
;
430 rect
->bottom
= mi
.rcMonitor
.bottom
;
432 case FULLSCREEN_WIDTH
:
433 rect
->left
= mi
.rcWork
.left
;
434 rect
->top
= normal
.top
;
435 rect
->right
= mi
.rcWork
.right
;
436 rect
->bottom
= normal
.bottom
;
438 case FULLSCREEN_HEIGHT
:
439 rect
->left
= normal
.left
;
440 rect
->top
= mi
.rcWork
.top
;
441 rect
->right
= normal
.right
;
442 rect
->bottom
= mi
.rcWork
.bottom
;
452 DEFUN ("w32-define-rgb-color", Fw32_define_rgb_color
,
453 Sw32_define_rgb_color
, 4, 4, 0,
454 doc
: /* Convert RGB numbers to a Windows color reference and associate with NAME.
455 This adds or updates a named color to `w32-color-map', making it
456 available for use. The original entry's RGB ref is returned, or nil
457 if the entry is new. */)
458 (Lisp_Object red
, Lisp_Object green
, Lisp_Object blue
, Lisp_Object name
)
461 Lisp_Object oldrgb
= Qnil
;
465 CHECK_NUMBER (green
);
469 XSETINT (rgb
, RGB (XUINT (red
), XUINT (green
), XUINT (blue
)));
473 /* replace existing entry in w32-color-map or add new entry. */
474 entry
= Fassoc (name
, Vw32_color_map
);
477 entry
= Fcons (name
, rgb
);
478 Vw32_color_map
= Fcons (entry
, Vw32_color_map
);
482 oldrgb
= Fcdr (entry
);
483 Fsetcdr (entry
, rgb
);
491 /* The default colors for the w32 color map */
492 typedef struct colormap_t
498 colormap_t w32_color_map
[] =
500 {"snow" , PALETTERGB (255,250,250)},
501 {"ghost white" , PALETTERGB (248,248,255)},
502 {"GhostWhite" , PALETTERGB (248,248,255)},
503 {"white smoke" , PALETTERGB (245,245,245)},
504 {"WhiteSmoke" , PALETTERGB (245,245,245)},
505 {"gainsboro" , PALETTERGB (220,220,220)},
506 {"floral white" , PALETTERGB (255,250,240)},
507 {"FloralWhite" , PALETTERGB (255,250,240)},
508 {"old lace" , PALETTERGB (253,245,230)},
509 {"OldLace" , PALETTERGB (253,245,230)},
510 {"linen" , PALETTERGB (250,240,230)},
511 {"antique white" , PALETTERGB (250,235,215)},
512 {"AntiqueWhite" , PALETTERGB (250,235,215)},
513 {"papaya whip" , PALETTERGB (255,239,213)},
514 {"PapayaWhip" , PALETTERGB (255,239,213)},
515 {"blanched almond" , PALETTERGB (255,235,205)},
516 {"BlanchedAlmond" , PALETTERGB (255,235,205)},
517 {"bisque" , PALETTERGB (255,228,196)},
518 {"peach puff" , PALETTERGB (255,218,185)},
519 {"PeachPuff" , PALETTERGB (255,218,185)},
520 {"navajo white" , PALETTERGB (255,222,173)},
521 {"NavajoWhite" , PALETTERGB (255,222,173)},
522 {"moccasin" , PALETTERGB (255,228,181)},
523 {"cornsilk" , PALETTERGB (255,248,220)},
524 {"ivory" , PALETTERGB (255,255,240)},
525 {"lemon chiffon" , PALETTERGB (255,250,205)},
526 {"LemonChiffon" , PALETTERGB (255,250,205)},
527 {"seashell" , PALETTERGB (255,245,238)},
528 {"honeydew" , PALETTERGB (240,255,240)},
529 {"mint cream" , PALETTERGB (245,255,250)},
530 {"MintCream" , PALETTERGB (245,255,250)},
531 {"azure" , PALETTERGB (240,255,255)},
532 {"alice blue" , PALETTERGB (240,248,255)},
533 {"AliceBlue" , PALETTERGB (240,248,255)},
534 {"lavender" , PALETTERGB (230,230,250)},
535 {"lavender blush" , PALETTERGB (255,240,245)},
536 {"LavenderBlush" , PALETTERGB (255,240,245)},
537 {"misty rose" , PALETTERGB (255,228,225)},
538 {"MistyRose" , PALETTERGB (255,228,225)},
539 {"white" , PALETTERGB (255,255,255)},
540 {"black" , PALETTERGB ( 0, 0, 0)},
541 {"dark slate gray" , PALETTERGB ( 47, 79, 79)},
542 {"DarkSlateGray" , PALETTERGB ( 47, 79, 79)},
543 {"dark slate grey" , PALETTERGB ( 47, 79, 79)},
544 {"DarkSlateGrey" , PALETTERGB ( 47, 79, 79)},
545 {"dim gray" , PALETTERGB (105,105,105)},
546 {"DimGray" , PALETTERGB (105,105,105)},
547 {"dim grey" , PALETTERGB (105,105,105)},
548 {"DimGrey" , PALETTERGB (105,105,105)},
549 {"slate gray" , PALETTERGB (112,128,144)},
550 {"SlateGray" , PALETTERGB (112,128,144)},
551 {"slate grey" , PALETTERGB (112,128,144)},
552 {"SlateGrey" , PALETTERGB (112,128,144)},
553 {"light slate gray" , PALETTERGB (119,136,153)},
554 {"LightSlateGray" , PALETTERGB (119,136,153)},
555 {"light slate grey" , PALETTERGB (119,136,153)},
556 {"LightSlateGrey" , PALETTERGB (119,136,153)},
557 {"gray" , PALETTERGB (190,190,190)},
558 {"grey" , PALETTERGB (190,190,190)},
559 {"light grey" , PALETTERGB (211,211,211)},
560 {"LightGrey" , PALETTERGB (211,211,211)},
561 {"light gray" , PALETTERGB (211,211,211)},
562 {"LightGray" , PALETTERGB (211,211,211)},
563 {"midnight blue" , PALETTERGB ( 25, 25,112)},
564 {"MidnightBlue" , PALETTERGB ( 25, 25,112)},
565 {"navy" , PALETTERGB ( 0, 0,128)},
566 {"navy blue" , PALETTERGB ( 0, 0,128)},
567 {"NavyBlue" , PALETTERGB ( 0, 0,128)},
568 {"cornflower blue" , PALETTERGB (100,149,237)},
569 {"CornflowerBlue" , PALETTERGB (100,149,237)},
570 {"dark slate blue" , PALETTERGB ( 72, 61,139)},
571 {"DarkSlateBlue" , PALETTERGB ( 72, 61,139)},
572 {"slate blue" , PALETTERGB (106, 90,205)},
573 {"SlateBlue" , PALETTERGB (106, 90,205)},
574 {"medium slate blue" , PALETTERGB (123,104,238)},
575 {"MediumSlateBlue" , PALETTERGB (123,104,238)},
576 {"light slate blue" , PALETTERGB (132,112,255)},
577 {"LightSlateBlue" , PALETTERGB (132,112,255)},
578 {"medium blue" , PALETTERGB ( 0, 0,205)},
579 {"MediumBlue" , PALETTERGB ( 0, 0,205)},
580 {"royal blue" , PALETTERGB ( 65,105,225)},
581 {"RoyalBlue" , PALETTERGB ( 65,105,225)},
582 {"blue" , PALETTERGB ( 0, 0,255)},
583 {"dodger blue" , PALETTERGB ( 30,144,255)},
584 {"DodgerBlue" , PALETTERGB ( 30,144,255)},
585 {"deep sky blue" , PALETTERGB ( 0,191,255)},
586 {"DeepSkyBlue" , PALETTERGB ( 0,191,255)},
587 {"sky blue" , PALETTERGB (135,206,235)},
588 {"SkyBlue" , PALETTERGB (135,206,235)},
589 {"light sky blue" , PALETTERGB (135,206,250)},
590 {"LightSkyBlue" , PALETTERGB (135,206,250)},
591 {"steel blue" , PALETTERGB ( 70,130,180)},
592 {"SteelBlue" , PALETTERGB ( 70,130,180)},
593 {"light steel blue" , PALETTERGB (176,196,222)},
594 {"LightSteelBlue" , PALETTERGB (176,196,222)},
595 {"light blue" , PALETTERGB (173,216,230)},
596 {"LightBlue" , PALETTERGB (173,216,230)},
597 {"powder blue" , PALETTERGB (176,224,230)},
598 {"PowderBlue" , PALETTERGB (176,224,230)},
599 {"pale turquoise" , PALETTERGB (175,238,238)},
600 {"PaleTurquoise" , PALETTERGB (175,238,238)},
601 {"dark turquoise" , PALETTERGB ( 0,206,209)},
602 {"DarkTurquoise" , PALETTERGB ( 0,206,209)},
603 {"medium turquoise" , PALETTERGB ( 72,209,204)},
604 {"MediumTurquoise" , PALETTERGB ( 72,209,204)},
605 {"turquoise" , PALETTERGB ( 64,224,208)},
606 {"cyan" , PALETTERGB ( 0,255,255)},
607 {"light cyan" , PALETTERGB (224,255,255)},
608 {"LightCyan" , PALETTERGB (224,255,255)},
609 {"cadet blue" , PALETTERGB ( 95,158,160)},
610 {"CadetBlue" , PALETTERGB ( 95,158,160)},
611 {"medium aquamarine" , PALETTERGB (102,205,170)},
612 {"MediumAquamarine" , PALETTERGB (102,205,170)},
613 {"aquamarine" , PALETTERGB (127,255,212)},
614 {"dark green" , PALETTERGB ( 0,100, 0)},
615 {"DarkGreen" , PALETTERGB ( 0,100, 0)},
616 {"dark olive green" , PALETTERGB ( 85,107, 47)},
617 {"DarkOliveGreen" , PALETTERGB ( 85,107, 47)},
618 {"dark sea green" , PALETTERGB (143,188,143)},
619 {"DarkSeaGreen" , PALETTERGB (143,188,143)},
620 {"sea green" , PALETTERGB ( 46,139, 87)},
621 {"SeaGreen" , PALETTERGB ( 46,139, 87)},
622 {"medium sea green" , PALETTERGB ( 60,179,113)},
623 {"MediumSeaGreen" , PALETTERGB ( 60,179,113)},
624 {"light sea green" , PALETTERGB ( 32,178,170)},
625 {"LightSeaGreen" , PALETTERGB ( 32,178,170)},
626 {"pale green" , PALETTERGB (152,251,152)},
627 {"PaleGreen" , PALETTERGB (152,251,152)},
628 {"spring green" , PALETTERGB ( 0,255,127)},
629 {"SpringGreen" , PALETTERGB ( 0,255,127)},
630 {"lawn green" , PALETTERGB (124,252, 0)},
631 {"LawnGreen" , PALETTERGB (124,252, 0)},
632 {"green" , PALETTERGB ( 0,255, 0)},
633 {"chartreuse" , PALETTERGB (127,255, 0)},
634 {"medium spring green" , PALETTERGB ( 0,250,154)},
635 {"MediumSpringGreen" , PALETTERGB ( 0,250,154)},
636 {"green yellow" , PALETTERGB (173,255, 47)},
637 {"GreenYellow" , PALETTERGB (173,255, 47)},
638 {"lime green" , PALETTERGB ( 50,205, 50)},
639 {"LimeGreen" , PALETTERGB ( 50,205, 50)},
640 {"yellow green" , PALETTERGB (154,205, 50)},
641 {"YellowGreen" , PALETTERGB (154,205, 50)},
642 {"forest green" , PALETTERGB ( 34,139, 34)},
643 {"ForestGreen" , PALETTERGB ( 34,139, 34)},
644 {"olive drab" , PALETTERGB (107,142, 35)},
645 {"OliveDrab" , PALETTERGB (107,142, 35)},
646 {"dark khaki" , PALETTERGB (189,183,107)},
647 {"DarkKhaki" , PALETTERGB (189,183,107)},
648 {"khaki" , PALETTERGB (240,230,140)},
649 {"pale goldenrod" , PALETTERGB (238,232,170)},
650 {"PaleGoldenrod" , PALETTERGB (238,232,170)},
651 {"light goldenrod yellow" , PALETTERGB (250,250,210)},
652 {"LightGoldenrodYellow" , PALETTERGB (250,250,210)},
653 {"light yellow" , PALETTERGB (255,255,224)},
654 {"LightYellow" , PALETTERGB (255,255,224)},
655 {"yellow" , PALETTERGB (255,255, 0)},
656 {"gold" , PALETTERGB (255,215, 0)},
657 {"light goldenrod" , PALETTERGB (238,221,130)},
658 {"LightGoldenrod" , PALETTERGB (238,221,130)},
659 {"goldenrod" , PALETTERGB (218,165, 32)},
660 {"dark goldenrod" , PALETTERGB (184,134, 11)},
661 {"DarkGoldenrod" , PALETTERGB (184,134, 11)},
662 {"rosy brown" , PALETTERGB (188,143,143)},
663 {"RosyBrown" , PALETTERGB (188,143,143)},
664 {"indian red" , PALETTERGB (205, 92, 92)},
665 {"IndianRed" , PALETTERGB (205, 92, 92)},
666 {"saddle brown" , PALETTERGB (139, 69, 19)},
667 {"SaddleBrown" , PALETTERGB (139, 69, 19)},
668 {"sienna" , PALETTERGB (160, 82, 45)},
669 {"peru" , PALETTERGB (205,133, 63)},
670 {"burlywood" , PALETTERGB (222,184,135)},
671 {"beige" , PALETTERGB (245,245,220)},
672 {"wheat" , PALETTERGB (245,222,179)},
673 {"sandy brown" , PALETTERGB (244,164, 96)},
674 {"SandyBrown" , PALETTERGB (244,164, 96)},
675 {"tan" , PALETTERGB (210,180,140)},
676 {"chocolate" , PALETTERGB (210,105, 30)},
677 {"firebrick" , PALETTERGB (178,34, 34)},
678 {"brown" , PALETTERGB (165,42, 42)},
679 {"dark salmon" , PALETTERGB (233,150,122)},
680 {"DarkSalmon" , PALETTERGB (233,150,122)},
681 {"salmon" , PALETTERGB (250,128,114)},
682 {"light salmon" , PALETTERGB (255,160,122)},
683 {"LightSalmon" , PALETTERGB (255,160,122)},
684 {"orange" , PALETTERGB (255,165, 0)},
685 {"dark orange" , PALETTERGB (255,140, 0)},
686 {"DarkOrange" , PALETTERGB (255,140, 0)},
687 {"coral" , PALETTERGB (255,127, 80)},
688 {"light coral" , PALETTERGB (240,128,128)},
689 {"LightCoral" , PALETTERGB (240,128,128)},
690 {"tomato" , PALETTERGB (255, 99, 71)},
691 {"orange red" , PALETTERGB (255, 69, 0)},
692 {"OrangeRed" , PALETTERGB (255, 69, 0)},
693 {"red" , PALETTERGB (255, 0, 0)},
694 {"hot pink" , PALETTERGB (255,105,180)},
695 {"HotPink" , PALETTERGB (255,105,180)},
696 {"deep pink" , PALETTERGB (255, 20,147)},
697 {"DeepPink" , PALETTERGB (255, 20,147)},
698 {"pink" , PALETTERGB (255,192,203)},
699 {"light pink" , PALETTERGB (255,182,193)},
700 {"LightPink" , PALETTERGB (255,182,193)},
701 {"pale violet red" , PALETTERGB (219,112,147)},
702 {"PaleVioletRed" , PALETTERGB (219,112,147)},
703 {"maroon" , PALETTERGB (176, 48, 96)},
704 {"medium violet red" , PALETTERGB (199, 21,133)},
705 {"MediumVioletRed" , PALETTERGB (199, 21,133)},
706 {"violet red" , PALETTERGB (208, 32,144)},
707 {"VioletRed" , PALETTERGB (208, 32,144)},
708 {"magenta" , PALETTERGB (255, 0,255)},
709 {"violet" , PALETTERGB (238,130,238)},
710 {"plum" , PALETTERGB (221,160,221)},
711 {"orchid" , PALETTERGB (218,112,214)},
712 {"medium orchid" , PALETTERGB (186, 85,211)},
713 {"MediumOrchid" , PALETTERGB (186, 85,211)},
714 {"dark orchid" , PALETTERGB (153, 50,204)},
715 {"DarkOrchid" , PALETTERGB (153, 50,204)},
716 {"dark violet" , PALETTERGB (148, 0,211)},
717 {"DarkViolet" , PALETTERGB (148, 0,211)},
718 {"blue violet" , PALETTERGB (138, 43,226)},
719 {"BlueViolet" , PALETTERGB (138, 43,226)},
720 {"purple" , PALETTERGB (160, 32,240)},
721 {"medium purple" , PALETTERGB (147,112,219)},
722 {"MediumPurple" , PALETTERGB (147,112,219)},
723 {"thistle" , PALETTERGB (216,191,216)},
724 {"gray0" , PALETTERGB ( 0, 0, 0)},
725 {"grey0" , PALETTERGB ( 0, 0, 0)},
726 {"dark grey" , PALETTERGB (169,169,169)},
727 {"DarkGrey" , PALETTERGB (169,169,169)},
728 {"dark gray" , PALETTERGB (169,169,169)},
729 {"DarkGray" , PALETTERGB (169,169,169)},
730 {"dark blue" , PALETTERGB ( 0, 0,139)},
731 {"DarkBlue" , PALETTERGB ( 0, 0,139)},
732 {"dark cyan" , PALETTERGB ( 0,139,139)},
733 {"DarkCyan" , PALETTERGB ( 0,139,139)},
734 {"dark magenta" , PALETTERGB (139, 0,139)},
735 {"DarkMagenta" , PALETTERGB (139, 0,139)},
736 {"dark red" , PALETTERGB (139, 0, 0)},
737 {"DarkRed" , PALETTERGB (139, 0, 0)},
738 {"light green" , PALETTERGB (144,238,144)},
739 {"LightGreen" , PALETTERGB (144,238,144)},
743 w32_default_color_map (void)
746 colormap_t
*pc
= w32_color_map
;
753 for (i
= 0; i
< ARRAYELTS (w32_color_map
); pc
++, i
++)
754 cmap
= Fcons (Fcons (build_string (pc
->name
),
755 make_number (pc
->colorref
)),
763 DEFUN ("w32-default-color-map", Fw32_default_color_map
, Sw32_default_color_map
,
764 0, 0, 0, doc
: /* Return the default color map. */)
767 return w32_default_color_map ();
771 w32_color_map_lookup (const char *colorname
)
773 Lisp_Object tail
, ret
= Qnil
;
777 for (tail
= Vw32_color_map
; CONSP (tail
); tail
= XCDR (tail
))
779 register Lisp_Object elt
, tem
;
782 if (!CONSP (elt
)) continue;
786 if (lstrcmpi (SSDATA (tem
), colorname
) == 0)
802 add_system_logical_colors_to_map (Lisp_Object
*system_colors
)
806 /* Other registry operations are done with input blocked. */
809 /* Look for "Control Panel/Colors" under User and Machine registry
811 if (RegOpenKeyEx (HKEY_CURRENT_USER
, "Control Panel\\Colors", 0,
812 KEY_READ
, &colors_key
) == ERROR_SUCCESS
813 || RegOpenKeyEx (HKEY_LOCAL_MACHINE
, "Control Panel\\Colors", 0,
814 KEY_READ
, &colors_key
) == ERROR_SUCCESS
)
817 char color_buffer
[64];
818 char full_name_buffer
[MAX_PATH
+ SYSTEM_COLOR_PREFIX_LEN
];
820 DWORD name_size
, color_size
;
821 char *name_buffer
= full_name_buffer
+ SYSTEM_COLOR_PREFIX_LEN
;
823 name_size
= sizeof (full_name_buffer
) - SYSTEM_COLOR_PREFIX_LEN
;
824 color_size
= sizeof (color_buffer
);
826 strcpy (full_name_buffer
, SYSTEM_COLOR_PREFIX
);
828 while (RegEnumValueA (colors_key
, index
, name_buffer
, &name_size
,
829 NULL
, NULL
, (LPBYTE
)color_buffer
, &color_size
)
833 if (sscanf (color_buffer
, " %u %u %u", &r
, &g
, &b
) == 3)
834 *system_colors
= Fcons (Fcons (build_string (full_name_buffer
),
835 make_number (RGB (r
, g
, b
))),
838 name_size
= sizeof (full_name_buffer
) - SYSTEM_COLOR_PREFIX_LEN
;
839 color_size
= sizeof (color_buffer
);
842 RegCloseKey (colors_key
);
850 x_to_w32_color (const char * colorname
)
852 register Lisp_Object ret
= Qnil
;
856 if (colorname
[0] == '#')
858 /* Could be an old-style RGB Device specification. */
859 int size
= strlen (colorname
+ 1);
860 char *color
= alloca (size
+ 1);
862 strcpy (color
, colorname
+ 1);
863 if (size
== 3 || size
== 6 || size
== 9 || size
== 12)
871 for (i
= 0; i
< 3; i
++)
877 /* The check for 'x' in the following conditional takes into
878 account the fact that strtol allows a "0x" in front of
879 our numbers, and we don't. */
880 if (!isxdigit (color
[0]) || color
[1] == 'x')
884 value
= strtoul (color
, &end
, 16);
886 if (errno
== ERANGE
|| end
- color
!= size
)
891 value
= value
* 0x10;
902 colorval
|= (value
<< pos
);
907 XSETINT (ret
, colorval
);
914 else if (strnicmp (colorname
, "rgb:", 4) == 0)
922 color
= colorname
+ 4;
923 for (i
= 0; i
< 3; i
++)
928 /* The check for 'x' in the following conditional takes into
929 account the fact that strtol allows a "0x" in front of
930 our numbers, and we don't. */
931 if (!isxdigit (color
[0]) || color
[1] == 'x')
933 value
= strtoul (color
, &end
, 16);
939 value
= value
* 0x10 + value
;
952 if (value
== ULONG_MAX
)
954 colorval
|= (value
<< pos
);
961 XSETINT (ret
, colorval
);
969 else if (strnicmp (colorname
, "rgbi:", 5) == 0)
971 /* This is an RGB Intensity specification. */
978 color
= colorname
+ 5;
979 for (i
= 0; i
< 3; i
++)
985 value
= strtod (color
, &end
);
988 if (value
< 0.0 || value
> 1.0)
990 val
= (UINT
)(0x100 * value
);
991 /* We used 0x100 instead of 0xFF to give a continuous
992 range between 0.0 and 1.0 inclusive. The next statement
993 fixes the 1.0 case. */
996 colorval
|= (val
<< pos
);
1003 XSETINT (ret
, colorval
);
1011 /* I am not going to attempt to handle any of the CIE color schemes
1012 or TekHVC, since I don't know the algorithms for conversion to
1015 /* If we fail to lookup the color name in w32_color_map, then check the
1016 colorname to see if it can be crudely approximated: If the X color
1017 ends in a number (e.g., "darkseagreen2"), strip the number and
1018 return the result of looking up the base color name. */
1019 ret
= w32_color_map_lookup (colorname
);
1022 int len
= strlen (colorname
);
1024 if (isdigit (colorname
[len
- 1]))
1026 char *ptr
, *approx
= alloca (len
+ 1);
1028 strcpy (approx
, colorname
);
1029 ptr
= &approx
[len
- 1];
1030 while (ptr
> approx
&& isdigit (*ptr
))
1033 ret
= w32_color_map_lookup (approx
);
1042 w32_regenerate_palette (struct frame
*f
)
1044 struct w32_palette_entry
* list
;
1045 LOGPALETTE
* log_palette
;
1046 HPALETTE new_palette
;
1049 /* don't bother trying to create palette if not supported */
1050 if (! FRAME_DISPLAY_INFO (f
)->has_palette
)
1053 log_palette
= (LOGPALETTE
*)
1054 alloca (sizeof (LOGPALETTE
) +
1055 FRAME_DISPLAY_INFO (f
)->num_colors
* sizeof (PALETTEENTRY
));
1056 log_palette
->palVersion
= 0x300;
1057 log_palette
->palNumEntries
= FRAME_DISPLAY_INFO (f
)->num_colors
;
1059 list
= FRAME_DISPLAY_INFO (f
)->color_list
;
1061 i
< FRAME_DISPLAY_INFO (f
)->num_colors
;
1062 i
++, list
= list
->next
)
1063 log_palette
->palPalEntry
[i
] = list
->entry
;
1065 new_palette
= CreatePalette (log_palette
);
1069 if (FRAME_DISPLAY_INFO (f
)->palette
)
1070 DeleteObject (FRAME_DISPLAY_INFO (f
)->palette
);
1071 FRAME_DISPLAY_INFO (f
)->palette
= new_palette
;
1073 /* Realize display palette and garbage all frames. */
1074 release_frame_dc (f
, get_frame_dc (f
));
1079 #define W32_COLOR(pe) RGB (pe.peRed, pe.peGreen, pe.peBlue)
1080 #define SET_W32_COLOR(pe, color) \
1083 pe.peRed = GetRValue (color); \
1084 pe.peGreen = GetGValue (color); \
1085 pe.peBlue = GetBValue (color); \
1090 /* Keep these around in case we ever want to track color usage. */
1092 w32_map_color (struct frame
*f
, COLORREF color
)
1094 struct w32_palette_entry
* list
= FRAME_DISPLAY_INFO (f
)->color_list
;
1096 if (NILP (Vw32_enable_palette
))
1099 /* check if color is already mapped */
1102 if (W32_COLOR (list
->entry
) == color
)
1110 /* not already mapped, so add to list and recreate Windows palette */
1111 list
= xmalloc (sizeof (struct w32_palette_entry
));
1112 SET_W32_COLOR (list
->entry
, color
);
1114 list
->next
= FRAME_DISPLAY_INFO (f
)->color_list
;
1115 FRAME_DISPLAY_INFO (f
)->color_list
= list
;
1116 FRAME_DISPLAY_INFO (f
)->num_colors
++;
1118 /* set flag that palette must be regenerated */
1119 FRAME_DISPLAY_INFO (f
)->regen_palette
= TRUE
;
1123 w32_unmap_color (struct frame
*f
, COLORREF color
)
1125 struct w32_palette_entry
* list
= FRAME_DISPLAY_INFO (f
)->color_list
;
1126 struct w32_palette_entry
**prev
= &FRAME_DISPLAY_INFO (f
)->color_list
;
1128 if (NILP (Vw32_enable_palette
))
1131 /* check if color is already mapped */
1134 if (W32_COLOR (list
->entry
) == color
)
1136 if (--list
->refcount
== 0)
1140 FRAME_DISPLAY_INFO (f
)->num_colors
--;
1150 /* set flag that palette must be regenerated */
1151 FRAME_DISPLAY_INFO (f
)->regen_palette
= TRUE
;
1156 /* Gamma-correct COLOR on frame F. */
1159 gamma_correct (struct frame
*f
, COLORREF
*color
)
1163 *color
= PALETTERGB (
1164 pow (GetRValue (*color
) / 255.0, f
->gamma
) * 255.0 + 0.5,
1165 pow (GetGValue (*color
) / 255.0, f
->gamma
) * 255.0 + 0.5,
1166 pow (GetBValue (*color
) / 255.0, f
->gamma
) * 255.0 + 0.5);
1171 /* Decide if color named COLOR is valid for the display associated with
1172 the selected frame; if so, return the rgb values in COLOR_DEF.
1173 If ALLOC is nonzero, allocate a new colormap cell. */
1176 w32_defined_color (struct frame
*f
, const char *color
, XColor
*color_def
,
1179 register Lisp_Object tem
;
1180 COLORREF w32_color_ref
;
1182 tem
= x_to_w32_color (color
);
1188 /* Apply gamma correction. */
1189 w32_color_ref
= XUINT (tem
);
1190 gamma_correct (f
, &w32_color_ref
);
1191 XSETINT (tem
, w32_color_ref
);
1194 /* Map this color to the palette if it is enabled. */
1195 if (!NILP (Vw32_enable_palette
))
1197 struct w32_palette_entry
* entry
=
1198 one_w32_display_info
.color_list
;
1199 struct w32_palette_entry
** prev
=
1200 &one_w32_display_info
.color_list
;
1202 /* check if color is already mapped */
1205 if (W32_COLOR (entry
->entry
) == XUINT (tem
))
1207 prev
= &entry
->next
;
1208 entry
= entry
->next
;
1211 if (entry
== NULL
&& alloc_p
)
1213 /* not already mapped, so add to list */
1214 entry
= xmalloc (sizeof (struct w32_palette_entry
));
1215 SET_W32_COLOR (entry
->entry
, XUINT (tem
));
1218 one_w32_display_info
.num_colors
++;
1220 /* set flag that palette must be regenerated */
1221 one_w32_display_info
.regen_palette
= TRUE
;
1224 /* Ensure COLORREF value is snapped to nearest color in (default)
1225 palette by simulating the PALETTERGB macro. This works whether
1226 or not the display device has a palette. */
1227 w32_color_ref
= XUINT (tem
) | 0x2000000;
1229 color_def
->pixel
= w32_color_ref
;
1230 color_def
->red
= GetRValue (w32_color_ref
) * 256;
1231 color_def
->green
= GetGValue (w32_color_ref
) * 256;
1232 color_def
->blue
= GetBValue (w32_color_ref
) * 256;
1242 /* Given a string ARG naming a color, compute a pixel value from it
1243 suitable for screen F.
1244 If F is not a color screen, return DEF (default) regardless of what
1248 x_decode_color (struct frame
*f
, Lisp_Object arg
, int def
)
1254 if (strcmp (SSDATA (arg
), "black") == 0)
1255 return BLACK_PIX_DEFAULT (f
);
1256 else if (strcmp (SSDATA (arg
), "white") == 0)
1257 return WHITE_PIX_DEFAULT (f
);
1259 if ((FRAME_DISPLAY_INFO (f
)->n_planes
* FRAME_DISPLAY_INFO (f
)->n_cbits
) == 1)
1262 /* w32_defined_color is responsible for coping with failures
1263 by looking for a near-miss. */
1264 if (w32_defined_color (f
, SSDATA (arg
), &cdef
, true))
1267 /* defined_color failed; return an ultimate default. */
1273 /* Functions called only from `x_set_frame_param'
1274 to set individual parameters.
1276 If FRAME_W32_WINDOW (f) is 0,
1277 the frame is being created and its window does not exist yet.
1278 In that case, just record the parameter's new value
1279 in the standard place; do not attempt to change the window. */
1282 x_set_foreground_color (struct frame
*f
, Lisp_Object arg
, Lisp_Object oldval
)
1284 struct w32_output
*x
= f
->output_data
.w32
;
1285 PIX_TYPE fg
, old_fg
;
1287 fg
= x_decode_color (f
, arg
, BLACK_PIX_DEFAULT (f
));
1288 old_fg
= FRAME_FOREGROUND_PIXEL (f
);
1289 FRAME_FOREGROUND_PIXEL (f
) = fg
;
1291 if (FRAME_W32_WINDOW (f
) != 0)
1293 if (x
->cursor_pixel
== old_fg
)
1295 x
->cursor_pixel
= fg
;
1296 x
->cursor_gc
->background
= fg
;
1299 update_face_from_frame_parameter (f
, Qforeground_color
, arg
);
1300 if (FRAME_VISIBLE_P (f
))
1306 x_set_background_color (struct frame
*f
, Lisp_Object arg
, Lisp_Object oldval
)
1308 FRAME_BACKGROUND_PIXEL (f
)
1309 = x_decode_color (f
, arg
, WHITE_PIX_DEFAULT (f
));
1311 if (FRAME_W32_WINDOW (f
) != 0)
1313 SetWindowLong (FRAME_W32_WINDOW (f
), WND_BACKGROUND_INDEX
,
1314 FRAME_BACKGROUND_PIXEL (f
));
1316 update_face_from_frame_parameter (f
, Qbackground_color
, arg
);
1318 if (FRAME_VISIBLE_P (f
))
1324 x_set_mouse_color (struct frame
*f
, Lisp_Object arg
, Lisp_Object oldval
)
1327 Cursor cursor
, nontext_cursor
, mode_cursor
, hand_cursor
;
1332 if (!EQ (Qnil
, arg
))
1333 f
->output_data
.w32
->mouse_pixel
1334 = x_decode_color (f
, arg
, BLACK_PIX_DEFAULT (f
));
1335 mask_color
= FRAME_BACKGROUND_PIXEL (f
);
1337 /* Don't let pointers be invisible. */
1338 if (mask_color
== f
->output_data
.w32
->mouse_pixel
1339 && mask_color
== FRAME_BACKGROUND_PIXEL (f
))
1340 f
->output_data
.w32
->mouse_pixel
= FRAME_FOREGROUND_PIXEL (f
);
1342 #if 0 /* TODO : Mouse cursor customization. */
1345 /* It's not okay to crash if the user selects a screwy cursor. */
1346 count
= x_catch_errors (FRAME_W32_DISPLAY (f
));
1348 if (!EQ (Qnil
, Vx_pointer_shape
))
1350 CHECK_NUMBER (Vx_pointer_shape
);
1351 cursor
= XCreateFontCursor (FRAME_W32_DISPLAY (f
), XINT (Vx_pointer_shape
));
1354 cursor
= XCreateFontCursor (FRAME_W32_DISPLAY (f
), XC_xterm
);
1355 x_check_errors (FRAME_W32_DISPLAY (f
), "bad text pointer cursor: %s");
1357 if (!EQ (Qnil
, Vx_nontext_pointer_shape
))
1359 CHECK_NUMBER (Vx_nontext_pointer_shape
);
1360 nontext_cursor
= XCreateFontCursor (FRAME_W32_DISPLAY (f
),
1361 XINT (Vx_nontext_pointer_shape
));
1364 nontext_cursor
= XCreateFontCursor (FRAME_W32_DISPLAY (f
), XC_left_ptr
);
1365 x_check_errors (FRAME_W32_DISPLAY (f
), "bad nontext pointer cursor: %s");
1367 if (!EQ (Qnil
, Vx_hourglass_pointer_shape
))
1369 CHECK_NUMBER (Vx_hourglass_pointer_shape
);
1370 hourglass_cursor
= XCreateFontCursor (FRAME_W32_DISPLAY (f
),
1371 XINT (Vx_hourglass_pointer_shape
));
1374 hourglass_cursor
= XCreateFontCursor (FRAME_W32_DISPLAY (f
), XC_watch
);
1375 x_check_errors (FRAME_W32_DISPLAY (f
), "bad busy pointer cursor: %s");
1377 x_check_errors (FRAME_W32_DISPLAY (f
), "bad nontext pointer cursor: %s");
1378 if (!EQ (Qnil
, Vx_mode_pointer_shape
))
1380 CHECK_NUMBER (Vx_mode_pointer_shape
);
1381 mode_cursor
= XCreateFontCursor (FRAME_W32_DISPLAY (f
),
1382 XINT (Vx_mode_pointer_shape
));
1385 mode_cursor
= XCreateFontCursor (FRAME_W32_DISPLAY (f
), XC_xterm
);
1386 x_check_errors (FRAME_W32_DISPLAY (f
), "bad modeline pointer cursor: %s");
1388 if (!EQ (Qnil
, Vx_sensitive_text_pointer_shape
))
1390 CHECK_NUMBER (Vx_sensitive_text_pointer_shape
);
1392 = XCreateFontCursor (FRAME_W32_DISPLAY (f
),
1393 XINT (Vx_sensitive_text_pointer_shape
));
1396 hand_cursor
= XCreateFontCursor (FRAME_W32_DISPLAY (f
), XC_crosshair
);
1398 if (!NILP (Vx_window_horizontal_drag_shape
))
1400 CHECK_NUMBER (Vx_window_horizontal_drag_shape
);
1401 horizontal_drag_cursor
1402 = XCreateFontCursor (FRAME_W32_DISPLAY (f
),
1403 XINT (Vx_window_horizontal_drag_shape
));
1406 horizontal_drag_cursor
1407 = XCreateFontCursor (FRAME_W32_DISPLAY (f
), XC_sb_h_double_arrow
);
1409 if (!NILP (Vx_window_vertical_drag_shape
))
1411 CHECK_NUMBER (Vx_window_vertical_drag_shape
);
1412 vertical_drag_cursor
1413 = XCreateFontCursor (FRAME_W32_DISPLAY (f
),
1414 XINT (Vx_window_vertical_drag_shape
));
1417 vertical_drag_cursor
1418 = XCreateFontCursor (FRAME_W32_DISPLAY (f
), XC_sb_v_double_arrow
);
1420 /* Check and report errors with the above calls. */
1421 x_check_errors (FRAME_W32_DISPLAY (f
), "can't set cursor shape: %s");
1422 x_uncatch_errors (FRAME_W32_DISPLAY (f
), count
);
1425 XColor fore_color
, back_color
;
1427 fore_color
.pixel
= f
->output_data
.w32
->mouse_pixel
;
1428 back_color
.pixel
= mask_color
;
1429 XQueryColor (FRAME_W32_DISPLAY (f
),
1430 DefaultColormap (FRAME_W32_DISPLAY (f
),
1431 DefaultScreen (FRAME_W32_DISPLAY (f
))),
1433 XQueryColor (FRAME_W32_DISPLAY (f
),
1434 DefaultColormap (FRAME_W32_DISPLAY (f
),
1435 DefaultScreen (FRAME_W32_DISPLAY (f
))),
1437 XRecolorCursor (FRAME_W32_DISPLAY (f
), cursor
,
1438 &fore_color
, &back_color
);
1439 XRecolorCursor (FRAME_W32_DISPLAY (f
), nontext_cursor
,
1440 &fore_color
, &back_color
);
1441 XRecolorCursor (FRAME_W32_DISPLAY (f
), mode_cursor
,
1442 &fore_color
, &back_color
);
1443 XRecolorCursor (FRAME_W32_DISPLAY (f
), hand_cursor
,
1444 &fore_color
, &back_color
);
1445 XRecolorCursor (FRAME_W32_DISPLAY (f
), hourglass_cursor
,
1446 &fore_color
, &back_color
);
1449 if (FRAME_W32_WINDOW (f
) != 0)
1450 XDefineCursor (FRAME_W32_DISPLAY (f
), FRAME_W32_WINDOW (f
), cursor
);
1452 if (cursor
!= f
->output_data
.w32
->text_cursor
&& f
->output_data
.w32
->text_cursor
!= 0)
1453 XFreeCursor (FRAME_W32_DISPLAY (f
), f
->output_data
.w32
->text_cursor
);
1454 f
->output_data
.w32
->text_cursor
= cursor
;
1456 if (nontext_cursor
!= f
->output_data
.w32
->nontext_cursor
1457 && f
->output_data
.w32
->nontext_cursor
!= 0)
1458 XFreeCursor (FRAME_W32_DISPLAY (f
), f
->output_data
.w32
->nontext_cursor
);
1459 f
->output_data
.w32
->nontext_cursor
= nontext_cursor
;
1461 if (hourglass_cursor
!= f
->output_data
.w32
->hourglass_cursor
1462 && f
->output_data
.w32
->hourglass_cursor
!= 0)
1463 XFreeCursor (FRAME_W32_DISPLAY (f
), f
->output_data
.w32
->hourglass_cursor
);
1464 f
->output_data
.w32
->hourglass_cursor
= hourglass_cursor
;
1466 if (mode_cursor
!= f
->output_data
.w32
->modeline_cursor
1467 && f
->output_data
.w32
->modeline_cursor
!= 0)
1468 XFreeCursor (FRAME_W32_DISPLAY (f
), f
->output_data
.w32
->modeline_cursor
);
1469 f
->output_data
.w32
->modeline_cursor
= mode_cursor
;
1471 if (hand_cursor
!= f
->output_data
.w32
->hand_cursor
1472 && f
->output_data
.w32
->hand_cursor
!= 0)
1473 XFreeCursor (FRAME_W32_DISPLAY (f
), f
->output_data
.w32
->hand_cursor
);
1474 f
->output_data
.w32
->hand_cursor
= hand_cursor
;
1476 XFlush (FRAME_W32_DISPLAY (f
));
1479 update_face_from_frame_parameter (f
, Qmouse_color
, arg
);
1484 x_set_cursor_color (struct frame
*f
, Lisp_Object arg
, Lisp_Object oldval
)
1486 unsigned long fore_pixel
, pixel
;
1488 if (!NILP (Vx_cursor_fore_pixel
))
1489 fore_pixel
= x_decode_color (f
, Vx_cursor_fore_pixel
,
1490 WHITE_PIX_DEFAULT (f
));
1492 fore_pixel
= FRAME_BACKGROUND_PIXEL (f
);
1494 pixel
= x_decode_color (f
, arg
, BLACK_PIX_DEFAULT (f
));
1496 /* Make sure that the cursor color differs from the background color. */
1497 if (pixel
== FRAME_BACKGROUND_PIXEL (f
))
1499 pixel
= f
->output_data
.w32
->mouse_pixel
;
1500 if (pixel
== fore_pixel
)
1501 fore_pixel
= FRAME_BACKGROUND_PIXEL (f
);
1504 f
->output_data
.w32
->cursor_foreground_pixel
= fore_pixel
;
1505 f
->output_data
.w32
->cursor_pixel
= pixel
;
1507 if (FRAME_W32_WINDOW (f
) != 0)
1510 /* Update frame's cursor_gc. */
1511 f
->output_data
.w32
->cursor_gc
->foreground
= fore_pixel
;
1512 f
->output_data
.w32
->cursor_gc
->background
= pixel
;
1516 if (FRAME_VISIBLE_P (f
))
1518 x_update_cursor (f
, 0);
1519 x_update_cursor (f
, 1);
1523 update_face_from_frame_parameter (f
, Qcursor_color
, arg
);
1526 /* Set the border-color of frame F to pixel value PIX.
1527 Note that this does not fully take effect if done before
1531 x_set_border_pixel (struct frame
*f
, int pix
)
1534 f
->output_data
.w32
->border_pixel
= pix
;
1536 if (FRAME_W32_WINDOW (f
) != 0 && f
->border_width
> 0)
1538 if (FRAME_VISIBLE_P (f
))
1543 /* Set the border-color of frame F to value described by ARG.
1544 ARG can be a string naming a color.
1545 The border-color is used for the border that is drawn by the server.
1546 Note that this does not fully take effect if done before
1547 F has a window; it must be redone when the window is created. */
1550 x_set_border_color (struct frame
*f
, Lisp_Object arg
, Lisp_Object oldval
)
1555 pix
= x_decode_color (f
, arg
, BLACK_PIX_DEFAULT (f
));
1556 x_set_border_pixel (f
, pix
);
1557 update_face_from_frame_parameter (f
, Qborder_color
, arg
);
1562 x_set_cursor_type (struct frame
*f
, Lisp_Object arg
, Lisp_Object oldval
)
1564 set_frame_cursor_types (f
, arg
);
1568 x_set_icon_type (struct frame
*f
, Lisp_Object arg
, Lisp_Object oldval
)
1572 if (NILP (arg
) && NILP (oldval
))
1575 if (STRINGP (arg
) && STRINGP (oldval
)
1576 && EQ (Fstring_equal (oldval
, arg
), Qt
))
1579 if (SYMBOLP (arg
) && SYMBOLP (oldval
) && EQ (arg
, oldval
))
1584 result
= x_bitmap_icon (f
, arg
);
1588 error ("No icon window available");
1595 x_set_icon_name (struct frame
*f
, Lisp_Object arg
, Lisp_Object oldval
)
1599 if (STRINGP (oldval
) && EQ (Fstring_equal (oldval
, arg
), Qt
))
1602 else if (!NILP (arg
) || NILP (oldval
))
1605 fset_icon_name (f
, arg
);
1608 if (f
->output_data
.w32
->icon_bitmap
!= 0)
1613 result
= x_text_icon (f
,
1614 SSDATA ((!NILP (f
->icon_name
)
1623 error ("No icon window available");
1626 /* If the window was unmapped (and its icon was mapped),
1627 the new icon is not mapped, so map the window in its stead. */
1628 if (FRAME_VISIBLE_P (f
))
1630 #ifdef USE_X_TOOLKIT
1631 XtPopup (f
->output_data
.w32
->widget
, XtGrabNone
);
1633 XMapWindow (FRAME_W32_DISPLAY (f
), FRAME_W32_WINDOW (f
));
1636 XFlush (FRAME_W32_DISPLAY (f
));
1642 x_clear_under_internal_border (struct frame
*f
)
1644 int border
= FRAME_INTERNAL_BORDER_WIDTH (f
);
1646 /* Clear border if it's larger than before. */
1649 HDC hdc
= get_frame_dc (f
);
1650 int width
= FRAME_PIXEL_WIDTH (f
);
1651 int height
= FRAME_PIXEL_HEIGHT (f
);
1654 w32_clear_area (f
, hdc
, 0, FRAME_TOP_MARGIN_HEIGHT (f
), width
, border
);
1655 w32_clear_area (f
, hdc
, 0, 0, border
, height
);
1656 w32_clear_area (f
, hdc
, width
- border
, 0, border
, height
);
1657 w32_clear_area (f
, hdc
, 0, height
- border
, width
, border
);
1658 release_frame_dc (f
, hdc
);
1665 x_set_internal_border_width (struct frame
*f
, Lisp_Object arg
, Lisp_Object oldval
)
1669 CHECK_TYPE_RANGED_INTEGER (int, arg
);
1670 border
= max (XINT (arg
), 0);
1672 if (border
!= FRAME_INTERNAL_BORDER_WIDTH (f
))
1674 FRAME_INTERNAL_BORDER_WIDTH (f
) = border
;
1676 if (FRAME_X_WINDOW (f
) != 0)
1678 adjust_frame_size (f
, -1, -1, 3, false, Qinternal_border_width
);
1680 if (FRAME_VISIBLE_P (f
))
1681 x_clear_under_internal_border (f
);
1688 x_set_menu_bar_lines (struct frame
*f
, Lisp_Object value
, Lisp_Object oldval
)
1692 /* Right now, menu bars don't work properly in minibuf-only frames;
1693 most of the commands try to apply themselves to the minibuffer
1694 frame itself, and get an error because you can't switch buffers
1695 in or split the minibuffer window. */
1696 if (FRAME_MINIBUF_ONLY_P (f
))
1699 if (INTEGERP (value
))
1700 nlines
= XINT (value
);
1704 FRAME_MENU_BAR_LINES (f
) = 0;
1705 FRAME_MENU_BAR_HEIGHT (f
) = 0;
1707 FRAME_EXTERNAL_MENU_BAR (f
) = 1;
1710 if (FRAME_EXTERNAL_MENU_BAR (f
) == 1)
1711 free_frame_menubar (f
);
1712 FRAME_EXTERNAL_MENU_BAR (f
) = 0;
1714 /* Adjust the frame size so that the client (text) dimensions
1715 remain the same. This depends on FRAME_EXTERNAL_MENU_BAR being
1716 set correctly. Note that we resize twice: The first time upon
1717 a request from the window manager who wants to keep the height
1718 of the outer rectangle (including decorations) unchanged, and a
1719 second time because we want to keep the height of the inner
1720 rectangle (without the decorations unchanged). */
1721 adjust_frame_size (f
, -1, -1, 2, true, Qmenu_bar_lines
);
1723 /* Not sure whether this is needed. */
1724 x_clear_under_internal_border (f
);
1729 /* Set the number of lines used for the tool bar of frame F to VALUE.
1730 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL is
1731 the old number of tool bar lines (and is unused). This function may
1732 change the height of all windows on frame F to match the new tool bar
1733 height. By design, the frame's height doesn't change (but maybe it
1734 should if we don't get enough space otherwise). */
1737 x_set_tool_bar_lines (struct frame
*f
, Lisp_Object value
, Lisp_Object oldval
)
1741 /* Treat tool bars like menu bars. */
1742 if (FRAME_MINIBUF_ONLY_P (f
))
1745 /* Use VALUE only if an integer >= 0. */
1746 if (INTEGERP (value
) && XINT (value
) >= 0)
1747 nlines
= XFASTINT (value
);
1751 x_change_tool_bar_height (f
, nlines
* FRAME_LINE_HEIGHT (f
));
1755 /* Set the pixel height of the tool bar of frame F to HEIGHT. */
1757 x_change_tool_bar_height (struct frame
*f
, int height
)
1759 int unit
= FRAME_LINE_HEIGHT (f
);
1760 int old_height
= FRAME_TOOL_BAR_HEIGHT (f
);
1761 int lines
= (height
+ unit
- 1) / unit
;
1762 Lisp_Object fullscreen
;
1764 /* Make sure we redisplay all windows in this frame. */
1765 windows_or_buffers_changed
= 23;
1767 /* Recalculate tool bar and frame text sizes. */
1768 FRAME_TOOL_BAR_HEIGHT (f
) = height
;
1769 FRAME_TOOL_BAR_LINES (f
) = lines
;
1770 /* Store `tool-bar-lines' and `height' frame parameters. */
1771 store_frame_param (f
, Qtool_bar_lines
, make_number (lines
));
1772 store_frame_param (f
, Qheight
, make_number (FRAME_LINES (f
)));
1774 if (FRAME_W32_WINDOW (f
) && FRAME_TOOL_BAR_HEIGHT (f
) == 0)
1777 clear_current_matrices (f
);
1780 if ((height
< old_height
) && WINDOWP (f
->tool_bar_window
))
1781 clear_glyph_matrix (XWINDOW (f
->tool_bar_window
)->current_matrix
);
1783 /* Recalculate toolbar height. */
1784 f
->n_tool_bar_rows
= 0;
1786 && (!f
->after_make_frame
1787 || NILP (frame_inhibit_implied_resize
)
1788 || (CONSP (frame_inhibit_implied_resize
)
1789 && NILP (Fmemq (Qtool_bar_lines
, frame_inhibit_implied_resize
)))))
1790 f
->tool_bar_redisplayed
= f
->tool_bar_resized
= false;
1792 adjust_frame_size (f
, -1, -1,
1793 ((!f
->tool_bar_resized
1794 && (NILP (fullscreen
=
1795 get_frame_param (f
, Qfullscreen
))
1796 || EQ (fullscreen
, Qfullwidth
))) ? 1
1797 : (old_height
== 0 || height
== 0) ? 2
1799 false, Qtool_bar_lines
);
1801 f
->tool_bar_resized
= f
->tool_bar_redisplayed
;
1803 /* adjust_frame_size might not have done anything, garbage frame
1805 adjust_frame_glyphs (f
);
1806 SET_FRAME_GARBAGED (f
);
1807 if (FRAME_X_WINDOW (f
))
1808 x_clear_under_internal_border (f
);
1812 w32_set_title_bar_text (struct frame
*f
, Lisp_Object name
)
1814 if (FRAME_W32_WINDOW (f
))
1818 GUI_FN (SetWindowText
) (FRAME_W32_WINDOW (f
),
1819 GUI_SDATA (GUI_ENCODE_SYSTEM (name
)));
1821 /* The frame's title many times shows the name of the file
1822 visited in the selected window's buffer, so it makes sense to
1823 support non-ASCII characters outside of the current system
1824 codepage in the title. */
1825 if (w32_unicode_filenames
)
1827 Lisp_Object encoded_title
= ENCODE_UTF_8 (name
);
1829 int tlen
= pMultiByteToWideChar (CP_UTF8
, 0, SSDATA (encoded_title
),
1834 /* Windows truncates the title text beyond what fits on
1835 a single line, so we can limit the length to some
1836 reasonably large value, and use alloca. */
1839 title_w
= alloca ((tlen
+ 1) * sizeof (wchar_t));
1840 pMultiByteToWideChar (CP_UTF8
, 0, SSDATA (encoded_title
), -1,
1842 title_w
[tlen
] = L
'\0';
1843 SetWindowTextW (FRAME_W32_WINDOW (f
), title_w
);
1845 else /* Conversion to UTF-16 failed, so we punt. */
1846 SetWindowTextA (FRAME_W32_WINDOW (f
),
1847 SSDATA (ENCODE_SYSTEM (name
)));
1850 SetWindowTextA (FRAME_W32_WINDOW (f
), SSDATA (ENCODE_SYSTEM (name
)));
1856 /* Change the name of frame F to NAME. If NAME is nil, set F's name to
1859 If EXPLICIT is non-zero, that indicates that lisp code is setting the
1860 name; if NAME is a string, set F's name to NAME and set
1861 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1863 If EXPLICIT is zero, that indicates that Emacs redisplay code is
1864 suggesting a new name, which lisp code should override; if
1865 F->explicit_name is set, ignore the new name; otherwise, set it. */
1868 x_set_name (struct frame
*f
, Lisp_Object name
, bool explicit)
1870 /* Make sure that requests from lisp code override requests from
1871 Emacs redisplay code. */
1874 /* If we're switching from explicit to implicit, we had better
1875 update the mode lines and thereby update the title. */
1876 if (f
->explicit_name
&& NILP (name
))
1877 update_mode_lines
= 25;
1879 f
->explicit_name
= ! NILP (name
);
1881 else if (f
->explicit_name
)
1884 /* If NAME is nil, set the name to the w32_id_name. */
1887 /* Check for no change needed in this very common case
1888 before we do any consing. */
1889 if (!strcmp (FRAME_DISPLAY_INFO (f
)->w32_id_name
,
1892 name
= build_string (FRAME_DISPLAY_INFO (f
)->w32_id_name
);
1895 CHECK_STRING (name
);
1897 /* Don't change the name if it's already NAME. */
1898 if (! NILP (Fstring_equal (name
, f
->name
)))
1901 fset_name (f
, name
);
1903 /* For setting the frame title, the title parameter should override
1904 the name parameter. */
1905 if (! NILP (f
->title
))
1908 w32_set_title_bar_text (f
, name
);
1911 /* This function should be called when the user's lisp code has
1912 specified a name for the frame; the name will override any set by the
1915 x_explicitly_set_name (struct frame
*f
, Lisp_Object arg
, Lisp_Object oldval
)
1917 x_set_name (f
, arg
, true);
1920 /* This function should be called by Emacs redisplay code to set the
1921 name; names set this way will never override names set by the user's
1924 x_implicitly_set_name (struct frame
*f
, Lisp_Object arg
, Lisp_Object oldval
)
1926 x_set_name (f
, arg
, false);
1929 /* Change the title of frame F to NAME.
1930 If NAME is nil, use the frame name as the title. */
1933 x_set_title (struct frame
*f
, Lisp_Object name
, Lisp_Object old_name
)
1935 /* Don't change the title if it's already NAME. */
1936 if (EQ (name
, f
->title
))
1939 update_mode_lines
= 26;
1941 fset_title (f
, name
);
1946 w32_set_title_bar_text (f
, name
);
1950 x_set_scroll_bar_default_width (struct frame
*f
)
1952 int unit
= FRAME_COLUMN_WIDTH (f
);
1954 FRAME_CONFIG_SCROLL_BAR_WIDTH (f
) = GetSystemMetrics (SM_CXVSCROLL
);
1955 FRAME_CONFIG_SCROLL_BAR_COLS (f
)
1956 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f
) + unit
- 1) / unit
;
1961 x_set_scroll_bar_default_height (struct frame
*f
)
1963 int unit
= FRAME_LINE_HEIGHT (f
);
1965 FRAME_CONFIG_SCROLL_BAR_HEIGHT (f
) = GetSystemMetrics (SM_CXHSCROLL
);
1966 FRAME_CONFIG_SCROLL_BAR_LINES (f
)
1967 = (FRAME_CONFIG_SCROLL_BAR_HEIGHT (f
) + unit
- 1) / unit
;
1970 /* Subroutines for creating a frame. */
1973 w32_load_cursor (LPCTSTR name
)
1975 /* Try first to load cursor from application resource. */
1976 Cursor cursor
= LoadImage ((HINSTANCE
) GetModuleHandle (NULL
),
1977 name
, IMAGE_CURSOR
, 0, 0,
1978 LR_DEFAULTCOLOR
| LR_DEFAULTSIZE
| LR_SHARED
);
1981 /* Then try to load a shared predefined cursor. */
1982 cursor
= LoadImage (NULL
, name
, IMAGE_CURSOR
, 0, 0,
1983 LR_DEFAULTCOLOR
| LR_DEFAULTSIZE
| LR_SHARED
);
1988 static LRESULT CALLBACK
w32_wnd_proc (HWND
, UINT
, WPARAM
, LPARAM
);
1990 #define INIT_WINDOW_CLASS(WC) \
1991 (WC).style = CS_HREDRAW | CS_VREDRAW; \
1992 (WC).lpfnWndProc = (WNDPROC) w32_wnd_proc; \
1993 (WC).cbClsExtra = 0; \
1994 (WC).cbWndExtra = WND_EXTRA_BYTES; \
1995 (WC).hInstance = hinst; \
1996 (WC).hIcon = LoadIcon (hinst, EMACS_CLASS); \
1997 (WC).hCursor = w32_load_cursor (IDC_ARROW); \
1998 (WC).hbrBackground = NULL; \
1999 (WC).lpszMenuName = NULL; \
2002 w32_init_class (HINSTANCE hinst
)
2004 if (w32_unicode_gui
)
2007 INIT_WINDOW_CLASS(uwc
);
2008 uwc
.lpszClassName
= L
"Emacs";
2010 return RegisterClassW (&uwc
);
2015 INIT_WINDOW_CLASS(wc
);
2016 wc
.lpszClassName
= EMACS_CLASS
;
2018 return RegisterClassA (&wc
);
2023 w32_createvscrollbar (struct frame
*f
, struct scroll_bar
* bar
)
2025 return CreateWindow ("SCROLLBAR", "", SBS_VERT
| WS_CHILD
| WS_VISIBLE
,
2026 /* Position and size of scroll bar. */
2027 bar
->left
, bar
->top
, bar
->width
, bar
->height
,
2028 FRAME_W32_WINDOW (f
), NULL
, hinst
, NULL
);
2032 w32_createhscrollbar (struct frame
*f
, struct scroll_bar
* bar
)
2034 return CreateWindow ("SCROLLBAR", "", SBS_HORZ
| WS_CHILD
| WS_VISIBLE
,
2035 /* Position and size of scroll bar. */
2036 bar
->left
, bar
->top
, bar
->width
, bar
->height
,
2037 FRAME_W32_WINDOW (f
), NULL
, hinst
, NULL
);
2041 w32_createwindow (struct frame
*f
, int *coords
)
2048 rect
.left
= rect
.top
= 0;
2049 rect
.right
= FRAME_PIXEL_WIDTH (f
);
2050 rect
.bottom
= FRAME_PIXEL_HEIGHT (f
);
2052 AdjustWindowRect (&rect
, f
->output_data
.w32
->dwStyle
,
2053 FRAME_EXTERNAL_MENU_BAR (f
));
2055 /* Do first time app init */
2057 w32_init_class (hinst
);
2059 if (f
->size_hint_flags
& USPosition
|| f
->size_hint_flags
& PPosition
)
2070 FRAME_W32_WINDOW (f
) = hwnd
2071 = CreateWindow (EMACS_CLASS
,
2073 f
->output_data
.w32
->dwStyle
| WS_CLIPCHILDREN
,
2075 rect
.right
- rect
.left
, rect
.bottom
- rect
.top
,
2083 SetWindowLong (hwnd
, WND_FONTWIDTH_INDEX
, FRAME_COLUMN_WIDTH (f
));
2084 SetWindowLong (hwnd
, WND_LINEHEIGHT_INDEX
, FRAME_LINE_HEIGHT (f
));
2085 SetWindowLong (hwnd
, WND_BORDER_INDEX
, FRAME_INTERNAL_BORDER_WIDTH (f
));
2086 SetWindowLong (hwnd
, WND_VSCROLLBAR_INDEX
, FRAME_SCROLL_BAR_AREA_WIDTH (f
));
2087 SetWindowLong (hwnd
, WND_HSCROLLBAR_INDEX
, FRAME_SCROLL_BAR_AREA_HEIGHT (f
));
2088 SetWindowLong (hwnd
, WND_BACKGROUND_INDEX
, FRAME_BACKGROUND_PIXEL (f
));
2090 /* Enable drag-n-drop. */
2091 DragAcceptFiles (hwnd
, TRUE
);
2093 /* Do this to discard the default setting specified by our parent. */
2094 ShowWindow (hwnd
, SW_HIDE
);
2096 /* Update frame positions. */
2097 GetWindowRect (hwnd
, &rect
);
2098 f
->left_pos
= rect
.left
;
2099 f
->top_pos
= rect
.top
;
2104 my_post_msg (W32Msg
* wmsg
, HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
2106 wmsg
->msg
.hwnd
= hwnd
;
2107 wmsg
->msg
.message
= msg
;
2108 wmsg
->msg
.wParam
= wParam
;
2109 wmsg
->msg
.lParam
= lParam
;
2110 wmsg
->msg
.time
= GetMessageTime ();
2116 /* The Windows keyboard hook callback. */
2117 static LRESULT CALLBACK
2118 funhook (int code
, WPARAM w
, LPARAM l
)
2121 HWND focus
= GetFocus ();
2123 KBDLLHOOKSTRUCT
const *hs
= (KBDLLHOOKSTRUCT
*)l
;
2125 if (code
< 0 || (hs
->flags
& LLKHF_INJECTED
))
2126 return CallNextHookEx (0, code
, w
, l
);
2128 /* The keyboard hook sees keyboard input on all processes (except
2129 elevated ones, when Emacs itself is not elevated). As such,
2130 care must be taken to only filter out keyboard input when Emacs
2131 itself is on the foreground.
2133 GetFocus returns a non-NULL window if another application is active,
2134 and always for a console Emacs process. For a console Emacs, determine
2135 focus by checking if the current foreground window is the process's
2137 if (focus
== NULL
&& kbdhook
.console
!= NULL
)
2139 if (GetForegroundWindow () == kbdhook
.console
)
2141 focus
= kbdhook
.console
;
2146 /* First, check hooks for the left and right Windows keys. */
2147 if (hs
->vkCode
== VK_LWIN
|| hs
->vkCode
== VK_RWIN
)
2149 if (focus
!= NULL
&& (w
== WM_KEYDOWN
|| w
== WM_SYSKEYDOWN
))
2151 /* The key is being pressed in an Emacs window. */
2152 if (hs
->vkCode
== VK_LWIN
&& !kbdhook
.lwindown
)
2154 kbdhook
.lwindown
= 1;
2155 kbdhook
.winseen
= 1;
2158 else if (hs
->vkCode
== VK_RWIN
&& !kbdhook
.rwindown
)
2160 kbdhook
.rwindown
= 1;
2161 kbdhook
.winseen
= 1;
2164 /* Returning 1 here drops the keypress without further processing.
2165 If the keypress was allowed to go through, the normal Windows
2166 hotkeys would take over. */
2169 else if (kbdhook
.winsdown
> 0 && (w
== WM_KEYUP
|| w
== WM_SYSKEYUP
))
2171 /* A key that has been captured earlier is being released now. */
2172 if (hs
->vkCode
== VK_LWIN
&& kbdhook
.lwindown
)
2174 kbdhook
.lwindown
= 0;
2177 else if (hs
->vkCode
== VK_RWIN
&& kbdhook
.rwindown
)
2179 kbdhook
.rwindown
= 0;
2182 if (kbdhook
.winsdown
== 0 && kbdhook
.winseen
)
2184 if (!kbdhook
.suppress_lone
)
2186 /* The Windows key was pressed, then released,
2187 without any other key pressed simultaneously.
2188 Normally, this opens the Start menu, but the user
2189 can prevent this by setting the
2190 w32-pass-[lr]window-to-system variable to
2192 if (hs
->vkCode
== VK_LWIN
&& !NILP (Vw32_pass_lwindow_to_system
) ||
2193 hs
->vkCode
== VK_RWIN
&& !NILP (Vw32_pass_rwindow_to_system
))
2195 /* Not prevented - Simulate the keypress to the system. */
2196 memset (inputs
, 0, sizeof (inputs
));
2197 inputs
[0].type
= INPUT_KEYBOARD
;
2198 inputs
[0].ki
.wVk
= hs
->vkCode
;
2199 inputs
[0].ki
.wScan
= hs
->vkCode
;
2200 inputs
[0].ki
.dwFlags
= KEYEVENTF_EXTENDEDKEY
;
2201 inputs
[0].ki
.time
= 0;
2202 inputs
[1].type
= INPUT_KEYBOARD
;
2203 inputs
[1].ki
.wVk
= hs
->vkCode
;
2204 inputs
[1].ki
.wScan
= hs
->vkCode
;
2205 inputs
[1].ki
.dwFlags
2206 = KEYEVENTF_EXTENDEDKEY
| KEYEVENTF_KEYUP
;
2207 inputs
[1].ki
.time
= 0;
2208 SendInput (2, inputs
, sizeof (INPUT
));
2210 else if (focus
!= NULL
)
2212 /* When not passed to system, must simulate privately to Emacs. */
2213 PostMessage (focus
, WM_SYSKEYDOWN
, hs
->vkCode
, 0);
2214 PostMessage (focus
, WM_SYSKEYUP
, hs
->vkCode
, 0);
2218 if (kbdhook
.winsdown
== 0)
2220 /* No Windows keys pressed anymore - clear the state flags. */
2221 kbdhook
.suppress_lone
= 0;
2222 kbdhook
.winseen
= 0;
2224 if (!kbdhook
.send_win_up
)
2226 /* Swallow this release message, as not to confuse
2227 applications who did not get to see the original
2228 WM_KEYDOWN message either. */
2231 kbdhook
.send_win_up
= 0;
2234 else if (kbdhook
.winsdown
> 0)
2236 /* Some other key was pressed while a captured Win key is down.
2237 This is either an Emacs registered hotkey combination, or a
2239 if (kbdhook
.lwindown
&& kbdhook
.lwin_hooked
[hs
->vkCode
] ||
2240 kbdhook
.rwindown
&& kbdhook
.rwin_hooked
[hs
->vkCode
])
2242 /* Hooked Win-x combination, do not pass the keypress to Windows. */
2243 kbdhook
.suppress_lone
= 1;
2245 else if (!kbdhook
.suppress_lone
)
2247 /* Unhooked S-x combination; simulate the combination now
2248 (will be seen by the system). */
2249 memset (inputs
, 0, sizeof (inputs
));
2250 inputs
[0].type
= INPUT_KEYBOARD
;
2251 inputs
[0].ki
.wVk
= kbdhook
.lwindown
? VK_LWIN
: VK_RWIN
;
2252 inputs
[0].ki
.wScan
= kbdhook
.lwindown
? VK_LWIN
: VK_RWIN
;
2253 inputs
[0].ki
.dwFlags
= KEYEVENTF_EXTENDEDKEY
;
2254 inputs
[0].ki
.time
= 0;
2255 inputs
[1].type
= INPUT_KEYBOARD
;
2256 inputs
[1].ki
.wVk
= hs
->vkCode
;
2257 inputs
[1].ki
.wScan
= hs
->scanCode
;
2258 inputs
[1].ki
.dwFlags
=
2259 (hs
->flags
& LLKHF_EXTENDED
) ? KEYEVENTF_EXTENDEDKEY
: 0;
2260 inputs
[1].ki
.time
= 0;
2261 SendInput (2, inputs
, sizeof (INPUT
));
2262 /* Stop processing of this Win sequence here; the
2263 corresponding keyup messages will come through the normal
2264 channel when the keys are released. */
2265 kbdhook
.suppress_lone
= 1;
2266 kbdhook
.send_win_up
= 1;
2267 /* Swallow the original keypress (as we want the Win key
2268 down message simulated above to precede this real message). */
2273 /* Next, handle the registered Alt-* combinations. */
2274 if ((w
== WM_SYSKEYDOWN
|| w
== WM_KEYDOWN
)
2275 && kbdhook
.alt_hooked
[hs
->vkCode
]
2277 && (GetAsyncKeyState (VK_MENU
) & 0x8000))
2279 /* Prevent the system from getting this Alt-* key - suppress the
2280 message and post as a normal keypress to Emacs. */
2285 rec
.EventType
= KEY_EVENT
;
2286 rec
.Event
.KeyEvent
.bKeyDown
= TRUE
;
2287 rec
.Event
.KeyEvent
.wVirtualKeyCode
= hs
->vkCode
;
2288 rec
.Event
.KeyEvent
.wVirtualScanCode
= hs
->scanCode
;
2289 rec
.Event
.KeyEvent
.uChar
.UnicodeChar
= 0;
2290 rec
.Event
.KeyEvent
.dwControlKeyState
=
2291 ((GetAsyncKeyState (VK_LMENU
) & 0x8000) ? LEFT_ALT_PRESSED
: 0)
2292 | ((GetAsyncKeyState (VK_RMENU
) & 0x8000) ? RIGHT_ALT_PRESSED
: 0)
2293 | ((GetAsyncKeyState (VK_LCONTROL
) & 0x8000) ? LEFT_CTRL_PRESSED
: 0)
2294 | ((GetAsyncKeyState (VK_RCONTROL
) & 0x8000) ? RIGHT_CTRL_PRESSED
: 0)
2295 | ((GetAsyncKeyState (VK_SHIFT
) & 0x8000) ? SHIFT_PRESSED
: 0)
2296 | ((hs
->flags
& LLKHF_EXTENDED
) ? ENHANCED_KEY
: 0);
2297 if (w32_console_unicode_input
)
2298 WriteConsoleInputW (keyboard_handle
, &rec
, 1, &n
);
2300 WriteConsoleInputA (keyboard_handle
, &rec
, 1, &n
);
2303 PostMessage (focus
, w
, hs
->vkCode
, 1 | (1<<29));
2307 /* The normal case - pass the message through. */
2308 return CallNextHookEx (0, code
, w
, l
);
2311 /* Set up the hook; can be called several times, with matching
2312 remove_w32_kbdhook calls. */
2314 setup_w32_kbdhook (void)
2316 kbdhook
.hook_count
++;
2318 /* Hooking is only available on NT architecture systems, as
2319 indicated by the w32_kbdhook_active variable. */
2320 if (kbdhook
.hook_count
== 1 && w32_kbdhook_active
)
2322 /* Get the handle of the Emacs console window. As the
2323 GetConsoleWindow function is only available on Win2000+, a
2324 hackish workaround described in Microsoft KB article 124103
2325 (https://support.microsoft.com/en-us/kb/124103) is used for
2327 GetConsoleWindow_Proc get_console
= (GetConsoleWindow_Proc
)
2328 GetProcAddress (GetModuleHandle ("kernel32.dll"), "GetConsoleWindow");
2330 if (get_console
!= NULL
)
2331 kbdhook
.console
= get_console ();
2335 wchar_t *oldTitle
= malloc (1024 * sizeof(wchar_t));
2336 wchar_t newTitle
[64];
2339 CoCreateGuid (&guid
);
2340 StringFromGUID2 (&guid
, newTitle
, 64);
2341 if (newTitle
!= NULL
)
2343 GetConsoleTitleW (oldTitle
, 1024);
2344 SetConsoleTitleW (newTitle
);
2345 for (i
= 0; i
< 25; i
++)
2348 kbdhook
.console
= FindWindowW (NULL
, newTitle
);
2349 if (kbdhook
.console
!= NULL
)
2352 SetConsoleTitleW (oldTitle
);
2358 kbdhook
.hook
= SetWindowsHookEx (WH_KEYBOARD_LL
, funhook
,
2359 GetModuleHandle (NULL
), 0);
2363 /* Remove the hook. */
2365 remove_w32_kbdhook (void)
2367 kbdhook
.hook_count
--;
2368 if (kbdhook
.hook_count
== 0 && w32_kbdhook_active
)
2370 UnhookWindowsHookEx (kbdhook
.hook
);
2371 kbdhook
.hook
= NULL
;
2374 #endif /* WINDOWSNT */
2376 /* Mark a specific key combination as hooked, preventing it to be
2377 handled by the system. */
2379 hook_w32_key (int hook
, int modifier
, int vkey
)
2386 tbl
= kbdhook
.alt_hooked
;
2389 tbl
= kbdhook
.lwin_hooked
;
2392 tbl
= kbdhook
.rwin_hooked
;
2396 if (tbl
!= NULL
&& vkey
>= 0 && vkey
<= 255)
2398 /* VK_ANY hooks all keys for this modifier */
2400 memset (tbl
, (char)hook
, 256);
2402 tbl
[vkey
] = (char)hook
;
2403 /* Alt-<modifier>s should go through */
2404 kbdhook
.alt_hooked
[VK_MENU
] = 0;
2405 kbdhook
.alt_hooked
[VK_LMENU
] = 0;
2406 kbdhook
.alt_hooked
[VK_RMENU
] = 0;
2407 kbdhook
.alt_hooked
[VK_CONTROL
] = 0;
2408 kbdhook
.alt_hooked
[VK_LCONTROL
] = 0;
2409 kbdhook
.alt_hooked
[VK_RCONTROL
] = 0;
2410 kbdhook
.alt_hooked
[VK_SHIFT
] = 0;
2411 kbdhook
.alt_hooked
[VK_LSHIFT
] = 0;
2412 kbdhook
.alt_hooked
[VK_RSHIFT
] = 0;
2416 /* Check the current Win key pressed state. */
2418 check_w32_winkey_state (int vkey
)
2420 /* The hook code handles grabbing of the Windows keys and Alt-* key
2421 combinations reserved by the system. Handling Alt is a bit
2422 easier, as Windows intends Alt-* shortcuts for application use in
2423 Windows; hotkeys such as Alt-tab and Alt-escape are special
2424 cases. Win-* hotkeys, on the other hand, are primarily meant for
2427 As a result, when we want Emacs to be able to grab the Win-*
2428 keys, we must swallow all Win key presses in a low-level keyboard
2429 hook. Unfortunately, this means that the Emacs window procedure
2430 (and console input handler) never see the keypresses either.
2431 Thus, to check the modifier states properly, Emacs code must use
2432 the check_w32_winkey_state function that uses the flags directly
2433 updated by the hook callback. */
2437 return kbdhook
.lwindown
;
2439 return kbdhook
.rwindown
;
2444 /* Reset the keyboard hook state. Locking the workstation with Win-L
2445 leaves the Win key(s) "down" from the hook's point of view - the
2446 keyup event is never seen. Thus, this function must be called when
2447 the system is locked. */
2449 reset_w32_kbdhook_state (void)
2451 kbdhook
.lwindown
= 0;
2452 kbdhook
.rwindown
= 0;
2453 kbdhook
.winsdown
= 0;
2454 kbdhook
.send_win_up
= 0;
2455 kbdhook
.suppress_lone
= 0;
2456 kbdhook
.winseen
= 0;
2459 /* GetKeyState and MapVirtualKey on Windows 95 do not actually distinguish
2460 between left and right keys as advertised. We test for this
2461 support dynamically, and set a flag when the support is absent. If
2462 absent, we keep track of the left and right control and alt keys
2463 ourselves. This is particularly necessary on keyboards that rely
2464 upon the AltGr key, which is represented as having the left control
2465 and right alt keys pressed. For these keyboards, we need to know
2466 when the left alt key has been pressed in addition to the AltGr key
2467 so that we can properly support M-AltGr-key sequences (such as M-@
2468 on Swedish keyboards). */
2470 #define EMACS_LCONTROL 0
2471 #define EMACS_RCONTROL 1
2472 #define EMACS_LMENU 2
2473 #define EMACS_RMENU 3
2475 static int modifiers
[4];
2476 static int modifiers_recorded
;
2477 static int modifier_key_support_tested
;
2480 test_modifier_support (unsigned int wparam
)
2484 if (wparam
!= VK_CONTROL
&& wparam
!= VK_MENU
)
2486 if (wparam
== VK_CONTROL
)
2496 if (!(GetKeyState (l
) & 0x8000) && !(GetKeyState (r
) & 0x8000))
2497 modifiers_recorded
= 1;
2499 modifiers_recorded
= 0;
2500 modifier_key_support_tested
= 1;
2504 record_keydown (unsigned int wparam
, unsigned int lparam
)
2508 if (!modifier_key_support_tested
)
2509 test_modifier_support (wparam
);
2511 if ((wparam
!= VK_CONTROL
&& wparam
!= VK_MENU
) || !modifiers_recorded
)
2514 if (wparam
== VK_CONTROL
)
2515 i
= (lparam
& 0x1000000) ? EMACS_RCONTROL
: EMACS_LCONTROL
;
2517 i
= (lparam
& 0x1000000) ? EMACS_RMENU
: EMACS_LMENU
;
2523 record_keyup (unsigned int wparam
, unsigned int lparam
)
2527 if ((wparam
!= VK_CONTROL
&& wparam
!= VK_MENU
) || !modifiers_recorded
)
2530 if (wparam
== VK_CONTROL
)
2531 i
= (lparam
& 0x1000000) ? EMACS_RCONTROL
: EMACS_LCONTROL
;
2533 i
= (lparam
& 0x1000000) ? EMACS_RMENU
: EMACS_LMENU
;
2538 /* Emacs can lose focus while a modifier key has been pressed. When
2539 it regains focus, be conservative and clear all modifiers since
2540 we cannot reconstruct the left and right modifier state. */
2542 reset_modifiers (void)
2546 if (GetFocus () == NULL
)
2547 /* Emacs doesn't have keyboard focus. Do nothing. */
2550 ctrl
= GetAsyncKeyState (VK_CONTROL
);
2551 alt
= GetAsyncKeyState (VK_MENU
);
2553 if (!(ctrl
& 0x08000))
2554 /* Clear any recorded control modifier state. */
2555 modifiers
[EMACS_RCONTROL
] = modifiers
[EMACS_LCONTROL
] = 0;
2557 if (!(alt
& 0x08000))
2558 /* Clear any recorded alt modifier state. */
2559 modifiers
[EMACS_RMENU
] = modifiers
[EMACS_LMENU
] = 0;
2561 /* Update the state of all modifier keys, because modifiers used in
2562 hot-key combinations can get stuck on if Emacs loses focus as a
2563 result of a hot-key being pressed. */
2567 #define CURRENT_STATE(key) ((GetAsyncKeyState (key) & 0x8000) >> 8)
2569 memset (keystate
, 0, sizeof (keystate
));
2570 GetKeyboardState (keystate
);
2571 keystate
[VK_SHIFT
] = CURRENT_STATE (VK_SHIFT
);
2572 keystate
[VK_CONTROL
] = CURRENT_STATE (VK_CONTROL
);
2573 keystate
[VK_LCONTROL
] = CURRENT_STATE (VK_LCONTROL
);
2574 keystate
[VK_RCONTROL
] = CURRENT_STATE (VK_RCONTROL
);
2575 keystate
[VK_MENU
] = CURRENT_STATE (VK_MENU
);
2576 keystate
[VK_LMENU
] = CURRENT_STATE (VK_LMENU
);
2577 keystate
[VK_RMENU
] = CURRENT_STATE (VK_RMENU
);
2578 keystate
[VK_LWIN
] = CURRENT_STATE (VK_LWIN
);
2579 keystate
[VK_RWIN
] = CURRENT_STATE (VK_RWIN
);
2580 keystate
[VK_APPS
] = CURRENT_STATE (VK_APPS
);
2581 SetKeyboardState (keystate
);
2585 /* Synchronize modifier state with what is reported with the current
2586 keystroke. Even if we cannot distinguish between left and right
2587 modifier keys, we know that, if no modifiers are set, then neither
2588 the left or right modifier should be set. */
2590 sync_modifiers (void)
2592 if (!modifiers_recorded
)
2595 if (!(GetKeyState (VK_CONTROL
) & 0x8000))
2596 modifiers
[EMACS_RCONTROL
] = modifiers
[EMACS_LCONTROL
] = 0;
2598 if (!(GetKeyState (VK_MENU
) & 0x8000))
2599 modifiers
[EMACS_RMENU
] = modifiers
[EMACS_LMENU
] = 0;
2603 modifier_set (int vkey
)
2605 /* Warning: The fact that VK_NUMLOCK is not treated as the other 2
2606 toggle keys is not an omission! If you want to add it, you will
2607 have to make changes in the default sub-case of the WM_KEYDOWN
2608 switch, because if the NUMLOCK modifier is set, the code there
2609 will directly convert any key that looks like an ASCII letter,
2610 and also downcase those that look like upper-case ASCII. */
2611 if (vkey
== VK_CAPITAL
)
2613 if (NILP (Vw32_enable_caps_lock
))
2616 return (GetKeyState (vkey
) & 0x1);
2618 if (vkey
== VK_SCROLL
)
2620 if (NILP (Vw32_scroll_lock_modifier
)
2621 /* w32-scroll-lock-modifier can be any non-nil value that is
2622 not one of the modifiers, in which case it shall be ignored. */
2623 || !( EQ (Vw32_scroll_lock_modifier
, Qhyper
)
2624 || EQ (Vw32_scroll_lock_modifier
, Qsuper
)
2625 || EQ (Vw32_scroll_lock_modifier
, Qmeta
)
2626 || EQ (Vw32_scroll_lock_modifier
, Qalt
)
2627 || EQ (Vw32_scroll_lock_modifier
, Qcontrol
)
2628 || EQ (Vw32_scroll_lock_modifier
, Qshift
)))
2631 return (GetKeyState (vkey
) & 0x1);
2633 if (w32_kbdhook_active
&& (vkey
== VK_LWIN
|| vkey
== VK_RWIN
))
2634 return check_w32_winkey_state (vkey
);
2636 if (!modifiers_recorded
)
2637 return (GetKeyState (vkey
) & 0x8000);
2642 return modifiers
[EMACS_LCONTROL
];
2644 return modifiers
[EMACS_RCONTROL
];
2646 return modifiers
[EMACS_LMENU
];
2648 return modifiers
[EMACS_RMENU
];
2650 return (GetKeyState (vkey
) & 0x8000);
2653 /* Convert between the modifier bits W32 uses and the modifier bits
2657 w32_key_to_modifier (int key
)
2659 Lisp_Object key_mapping
;
2664 key_mapping
= Vw32_lwindow_modifier
;
2667 key_mapping
= Vw32_rwindow_modifier
;
2670 key_mapping
= Vw32_apps_modifier
;
2673 key_mapping
= Vw32_scroll_lock_modifier
;
2679 /* NB. This code runs in the input thread, asynchronously to the lisp
2680 thread, so we must be careful to ensure access to lisp data is
2681 thread-safe. The following code is safe because the modifier
2682 variable values are updated atomically from lisp and symbols are
2683 not relocated by GC. Also, we don't have to worry about seeing GC
2685 if (EQ (key_mapping
, Qhyper
))
2686 return hyper_modifier
;
2687 if (EQ (key_mapping
, Qsuper
))
2688 return super_modifier
;
2689 if (EQ (key_mapping
, Qmeta
))
2690 return meta_modifier
;
2691 if (EQ (key_mapping
, Qalt
))
2692 return alt_modifier
;
2693 if (EQ (key_mapping
, Qctrl
))
2694 return ctrl_modifier
;
2695 if (EQ (key_mapping
, Qcontrol
)) /* synonym for ctrl */
2696 return ctrl_modifier
;
2697 if (EQ (key_mapping
, Qshift
))
2698 return shift_modifier
;
2700 /* Don't generate any modifier if not explicitly requested. */
2705 w32_get_modifiers (void)
2707 return ((modifier_set (VK_SHIFT
) ? shift_modifier
: 0) |
2708 (modifier_set (VK_CONTROL
) ? ctrl_modifier
: 0) |
2709 (modifier_set (VK_LWIN
) ? w32_key_to_modifier (VK_LWIN
) : 0) |
2710 (modifier_set (VK_RWIN
) ? w32_key_to_modifier (VK_RWIN
) : 0) |
2711 (modifier_set (VK_APPS
) ? w32_key_to_modifier (VK_APPS
) : 0) |
2712 (modifier_set (VK_SCROLL
) ? w32_key_to_modifier (VK_SCROLL
) : 0) |
2713 (modifier_set (VK_MENU
) ?
2714 ((NILP (Vw32_alt_is_meta
)) ? alt_modifier
: meta_modifier
) : 0));
2717 /* We map the VK_* modifiers into console modifier constants
2718 so that we can use the same routines to handle both console
2719 and window input. */
2722 construct_console_modifiers (void)
2727 mods
|= (modifier_set (VK_SHIFT
)) ? SHIFT_PRESSED
: 0;
2728 mods
|= (modifier_set (VK_CAPITAL
)) ? CAPSLOCK_ON
: 0;
2729 mods
|= (modifier_set (VK_SCROLL
)) ? SCROLLLOCK_ON
: 0;
2730 mods
|= (modifier_set (VK_NUMLOCK
)) ? NUMLOCK_ON
: 0;
2731 mods
|= (modifier_set (VK_LCONTROL
)) ? LEFT_CTRL_PRESSED
: 0;
2732 mods
|= (modifier_set (VK_RCONTROL
)) ? RIGHT_CTRL_PRESSED
: 0;
2733 mods
|= (modifier_set (VK_LMENU
)) ? LEFT_ALT_PRESSED
: 0;
2734 mods
|= (modifier_set (VK_RMENU
)) ? RIGHT_ALT_PRESSED
: 0;
2735 mods
|= (modifier_set (VK_LWIN
)) ? LEFT_WIN_PRESSED
: 0;
2736 mods
|= (modifier_set (VK_RWIN
)) ? RIGHT_WIN_PRESSED
: 0;
2737 mods
|= (modifier_set (VK_APPS
)) ? APPS_PRESSED
: 0;
2743 w32_get_key_modifiers (unsigned int wparam
, unsigned int lparam
)
2747 /* Convert to emacs modifiers. */
2748 mods
= w32_kbd_mods_to_emacs (construct_console_modifiers (), wparam
);
2754 map_keypad_keys (unsigned int virt_key
, unsigned int extended
)
2756 if (virt_key
< VK_CLEAR
|| virt_key
> VK_DELETE
)
2759 if (virt_key
== VK_RETURN
)
2760 return (extended
? VK_NUMPAD_ENTER
: VK_RETURN
);
2762 if (virt_key
>= VK_PRIOR
&& virt_key
<= VK_DOWN
)
2763 return (!extended
? (VK_NUMPAD_PRIOR
+ (virt_key
- VK_PRIOR
)) : virt_key
);
2765 if (virt_key
== VK_INSERT
|| virt_key
== VK_DELETE
)
2766 return (!extended
? (VK_NUMPAD_INSERT
+ (virt_key
- VK_INSERT
)) : virt_key
);
2768 if (virt_key
== VK_CLEAR
)
2769 return (!extended
? VK_NUMPAD_CLEAR
: virt_key
);
2774 /* List of special key combinations which w32 would normally capture,
2775 but Emacs should grab instead. Not directly visible to lisp, to
2776 simplify synchronization. Each item is an integer encoding a virtual
2777 key code and modifier combination to capture.
2778 Note: This code is not used if keyboard hooks are active
2779 (Windows 2000 and later). */
2780 static Lisp_Object w32_grabbed_keys
;
2782 #define HOTKEY(vk, mods) make_number (((vk) & 255) | ((mods) << 8))
2783 #define HOTKEY_ID(k) (XFASTINT (k) & 0xbfff)
2784 #define HOTKEY_VK_CODE(k) (XFASTINT (k) & 255)
2785 #define HOTKEY_MODIFIERS(k) (XFASTINT (k) >> 8)
2787 #define RAW_HOTKEY_ID(k) ((k) & 0xbfff)
2788 #define RAW_HOTKEY_VK_CODE(k) ((k) & 255)
2789 #define RAW_HOTKEY_MODIFIERS(k) ((k) >> 8)
2791 /* Register hot-keys for reserved key combinations when Emacs has
2792 keyboard focus, since this is the only way Emacs can receive key
2793 combinations like Alt-Tab which are used by the system. */
2796 register_hot_keys (HWND hwnd
)
2798 Lisp_Object keylist
;
2800 /* Use CONSP, since we are called asynchronously. */
2801 for (keylist
= w32_grabbed_keys
; CONSP (keylist
); keylist
= XCDR (keylist
))
2803 Lisp_Object key
= XCAR (keylist
);
2805 /* Deleted entries get set to nil. */
2806 if (!INTEGERP (key
))
2809 RegisterHotKey (hwnd
, HOTKEY_ID (key
),
2810 HOTKEY_MODIFIERS (key
), HOTKEY_VK_CODE (key
));
2815 unregister_hot_keys (HWND hwnd
)
2817 Lisp_Object keylist
;
2819 for (keylist
= w32_grabbed_keys
; CONSP (keylist
); keylist
= XCDR (keylist
))
2821 Lisp_Object key
= XCAR (keylist
);
2823 if (!INTEGERP (key
))
2826 UnregisterHotKey (hwnd
, HOTKEY_ID (key
));
2832 w32_name_of_message (UINT msg
)
2835 static char buf
[64];
2836 static const struct {
2840 #define M(msg) { msg, # msg }
2848 M (WM_EMACS_CREATEWINDOW
),
2850 M (WM_EMACS_CREATEVSCROLLBAR
),
2851 M (WM_EMACS_CREATEHSCROLLBAR
),
2852 M (WM_EMACS_SHOWWINDOW
),
2853 M (WM_EMACS_SETWINDOWPOS
),
2854 M (WM_EMACS_DESTROYWINDOW
),
2855 M (WM_EMACS_TRACKPOPUPMENU
),
2856 M (WM_EMACS_SETFOCUS
),
2857 M (WM_EMACS_SETFOREGROUND
),
2858 M (WM_EMACS_SETLOCALE
),
2859 M (WM_EMACS_SETKEYBOARDLAYOUT
),
2860 M (WM_EMACS_REGISTER_HOT_KEY
),
2861 M (WM_EMACS_UNREGISTER_HOT_KEY
),
2862 M (WM_EMACS_TOGGLE_LOCK_KEY
),
2863 M (WM_EMACS_TRACK_CARET
),
2864 M (WM_EMACS_DESTROY_CARET
),
2865 M (WM_EMACS_SHOW_CARET
),
2866 M (WM_EMACS_HIDE_CARET
),
2867 M (WM_EMACS_SETCURSOR
),
2868 M (WM_EMACS_SHOWCURSOR
),
2875 for (i
= 0; msgnames
[i
].name
; ++i
)
2876 if (msgnames
[i
].msg
== msg
)
2877 return msgnames
[i
].name
;
2879 sprintf (buf
, "message 0x%04x", (unsigned)msg
);
2882 #endif /* EMACSDEBUG */
2884 /* Here's an overview of how Emacs input works in GUI sessions on
2885 MS-Windows. (For description of non-GUI input, see the commentary
2886 before w32_console_read_socket in w32inevt.c.)
2888 System messages are read and processed by w32_msg_pump below. This
2889 function runs in a separate thread. It handles a small number of
2890 custom WM_EMACS_* messages (posted by the main thread, look for
2891 PostMessage calls), and dispatches the rest to w32_wnd_proc, which
2892 is the main window procedure for the entire Emacs application.
2894 w32_wnd_proc also runs in the same separate input thread. It
2895 handles some messages, mostly those that need GDI calls, by itself.
2896 For the others, it calls my_post_msg, which inserts the messages
2897 into the input queue serviced by w32_read_socket.
2899 w32_read_socket runs in the main (a.k.a. "Lisp") thread, and is
2900 called synchronously from keyboard.c when it is known or suspected
2901 that some input is available. w32_read_socket either handles
2902 messages immediately, or converts them into Emacs input events and
2903 stuffs them into kbd_buffer, where kbd_buffer_get_event can get at
2904 them and process them when read_char and its callers require
2907 Under Cygwin with the W32 toolkit, the use of /dev/windows with
2908 select(2) takes the place of w32_read_socket.
2912 /* Main message dispatch loop. */
2915 w32_msg_pump (deferred_msg
* msg_buf
)
2921 msh_mousewheel
= RegisterWindowMessage (MSH_MOUSEWHEEL
);
2923 while ((w32_unicode_gui
? GetMessageW
: GetMessageA
) (&msg
, NULL
, 0, 0))
2926 /* DebPrint (("w32_msg_pump: %s time:%u\n", */
2927 /* w32_name_of_message (msg.message), msg.time)); */
2929 if (msg
.hwnd
== NULL
)
2931 switch (msg
.message
)
2934 /* Produced by complete_deferred_msg; just ignore. */
2936 case WM_EMACS_CREATEWINDOW
:
2937 /* Initialize COM for this window. Even though we don't use it,
2938 some third party shell extensions can cause it to be used in
2939 system dialogs, which causes a crash if it is not initialized.
2940 This is a known bug in Windows, which was fixed long ago, but
2941 the patch for XP is not publicly available until XP SP3,
2942 and older versions will never be patched. */
2943 CoInitialize (NULL
);
2944 w32_createwindow ((struct frame
*) msg
.wParam
,
2945 (int *) msg
.lParam
);
2946 if (!PostThreadMessage (dwMainThreadId
, WM_EMACS_DONE
, 0, 0))
2949 case WM_EMACS_SETLOCALE
:
2950 SetThreadLocale (msg
.wParam
);
2951 /* Reply is not expected. */
2953 case WM_EMACS_SETKEYBOARDLAYOUT
:
2954 result
= (WPARAM
) ActivateKeyboardLayout ((HKL
) msg
.wParam
, 0);
2955 if (!PostThreadMessage (dwMainThreadId
, WM_EMACS_DONE
,
2959 case WM_EMACS_REGISTER_HOT_KEY
:
2960 focus_window
= GetFocus ();
2961 if (focus_window
!= NULL
)
2962 RegisterHotKey (focus_window
,
2963 RAW_HOTKEY_ID (msg
.wParam
),
2964 RAW_HOTKEY_MODIFIERS (msg
.wParam
),
2965 RAW_HOTKEY_VK_CODE (msg
.wParam
));
2966 /* Reply is not expected. */
2968 case WM_EMACS_UNREGISTER_HOT_KEY
:
2969 focus_window
= GetFocus ();
2970 if (focus_window
!= NULL
)
2971 UnregisterHotKey (focus_window
, RAW_HOTKEY_ID (msg
.wParam
));
2972 /* Mark item as erased. NB: this code must be
2973 thread-safe. The next line is okay because the cons
2974 cell is never made into garbage and is not relocated by
2976 XSETCAR (make_lisp_ptr ((void *)msg
.lParam
, Lisp_Cons
), Qnil
);
2977 if (!PostThreadMessage (dwMainThreadId
, WM_EMACS_DONE
, 0, 0))
2980 case WM_EMACS_TOGGLE_LOCK_KEY
:
2982 int vk_code
= (int) msg
.wParam
;
2983 int cur_state
= (GetKeyState (vk_code
) & 1);
2984 int new_state
= msg
.lParam
;
2987 || ((new_state
& 1) != cur_state
))
2989 one_w32_display_info
.faked_key
= vk_code
;
2991 keybd_event ((BYTE
) vk_code
,
2992 (BYTE
) MapVirtualKey (vk_code
, 0),
2993 KEYEVENTF_EXTENDEDKEY
| KEYEVENTF_KEYUP
, 0);
2994 keybd_event ((BYTE
) vk_code
,
2995 (BYTE
) MapVirtualKey (vk_code
, 0),
2996 KEYEVENTF_EXTENDEDKEY
| 0, 0);
2997 keybd_event ((BYTE
) vk_code
,
2998 (BYTE
) MapVirtualKey (vk_code
, 0),
2999 KEYEVENTF_EXTENDEDKEY
| KEYEVENTF_KEYUP
, 0);
3000 cur_state
= !cur_state
;
3002 if (!PostThreadMessage (dwMainThreadId
, WM_EMACS_DONE
,
3008 /* Broadcast messages make it here, so you need to be looking
3009 for something in particular for this to be useful. */
3011 DebPrint (("msg %x not expected by w32_msg_pump\n", msg
.message
));
3017 if (w32_unicode_gui
)
3018 DispatchMessageW (&msg
);
3020 DispatchMessageA (&msg
);
3023 /* Exit nested loop when our deferred message has completed. */
3024 if (msg_buf
->completed
)
3029 deferred_msg
* deferred_msg_head
;
3031 static deferred_msg
*
3032 find_deferred_msg (HWND hwnd
, UINT msg
)
3034 deferred_msg
* item
;
3036 /* Don't actually need synchronization for read access, since
3037 modification of single pointer is always atomic. */
3038 /* enter_crit (); */
3040 for (item
= deferred_msg_head
; item
!= NULL
; item
= item
->next
)
3041 if (item
->w32msg
.msg
.hwnd
== hwnd
3042 && item
->w32msg
.msg
.message
== msg
)
3045 /* leave_crit (); */
3051 send_deferred_msg (deferred_msg
* msg_buf
,
3057 /* Only input thread can send deferred messages. */
3058 if (GetCurrentThreadId () != dwWindowsThreadId
)
3061 /* It is an error to send a message that is already deferred. */
3062 if (find_deferred_msg (hwnd
, msg
) != NULL
)
3065 /* Enforced synchronization is not needed because this is the only
3066 function that alters deferred_msg_head, and the following critical
3067 section is guaranteed to only be serially reentered (since only the
3068 input thread can call us). */
3070 /* enter_crit (); */
3072 msg_buf
->completed
= 0;
3073 msg_buf
->next
= deferred_msg_head
;
3074 deferred_msg_head
= msg_buf
;
3075 my_post_msg (&msg_buf
->w32msg
, hwnd
, msg
, wParam
, lParam
);
3077 /* leave_crit (); */
3079 /* Start a new nested message loop to process other messages until
3080 this one is completed. */
3081 w32_msg_pump (msg_buf
);
3083 deferred_msg_head
= msg_buf
->next
;
3085 return msg_buf
->result
;
3089 complete_deferred_msg (HWND hwnd
, UINT msg
, LRESULT result
)
3091 deferred_msg
* msg_buf
= find_deferred_msg (hwnd
, msg
);
3093 if (msg_buf
== NULL
)
3094 /* Message may have been canceled, so don't abort. */
3097 msg_buf
->result
= result
;
3098 msg_buf
->completed
= 1;
3100 /* Ensure input thread is woken so it notices the completion. */
3101 PostThreadMessage (dwWindowsThreadId
, WM_NULL
, 0, 0);
3105 cancel_all_deferred_msgs (void)
3107 deferred_msg
* item
;
3109 /* Don't actually need synchronization for read access, since
3110 modification of single pointer is always atomic. */
3111 /* enter_crit (); */
3113 for (item
= deferred_msg_head
; item
!= NULL
; item
= item
->next
)
3116 item
->completed
= 1;
3119 /* leave_crit (); */
3121 /* Ensure input thread is woken so it notices the completion. */
3122 PostThreadMessage (dwWindowsThreadId
, WM_NULL
, 0, 0);
3126 w32_msg_worker (void *arg
)
3129 deferred_msg dummy_buf
;
3131 /* Ensure our message queue is created */
3133 PeekMessage (&msg
, NULL
, 0, 0, PM_NOREMOVE
);
3135 if (!PostThreadMessage (dwMainThreadId
, WM_EMACS_DONE
, 0, 0))
3138 memset (&dummy_buf
, 0, sizeof (dummy_buf
));
3139 dummy_buf
.w32msg
.msg
.hwnd
= NULL
;
3140 dummy_buf
.w32msg
.msg
.message
= WM_NULL
;
3142 /* This is the initial message loop which should only exit when the
3143 application quits. */
3144 w32_msg_pump (&dummy_buf
);
3150 signal_user_input (void)
3152 /* Interrupt any lisp that wants to be interrupted by input. */
3153 if (!NILP (Vthrow_on_input
))
3155 Vquit_flag
= Vthrow_on_input
;
3156 /* Doing a QUIT from this thread is a bad idea, since this
3157 unwinds the stack of the Lisp thread, and the Windows runtime
3158 rightfully barfs. Disabled. */
3160 /* If we're inside a function that wants immediate quits,
3162 if (immediate_quit
&& NILP (Vinhibit_quit
))
3173 post_character_message (HWND hwnd
, UINT msg
,
3174 WPARAM wParam
, LPARAM lParam
,
3179 wmsg
.dwModifiers
= modifiers
;
3181 /* Detect quit_char and set quit-flag directly. Note that we
3182 still need to post a message to ensure the main thread will be
3183 woken up if blocked in sys_select, but we do NOT want to post
3184 the quit_char message itself (because it will usually be as if
3185 the user had typed quit_char twice). Instead, we post a dummy
3186 message that has no particular effect. */
3189 if (isalpha (c
) && wmsg
.dwModifiers
== ctrl_modifier
)
3190 c
= make_ctrl_char (c
) & 0377;
3192 || (wmsg
.dwModifiers
== 0
3193 && w32_quit_key
&& wParam
== w32_quit_key
))
3197 /* The choice of message is somewhat arbitrary, as long as
3198 the main thread handler just ignores it. */
3201 /* Interrupt any blocking system calls. */
3204 /* As a safety precaution, forcibly complete any deferred
3205 messages. This is a kludge, but I don't see any particularly
3206 clean way to handle the situation where a deferred message is
3207 "dropped" in the lisp thread, and will thus never be
3208 completed, eg. by the user trying to activate the menubar
3209 when the lisp thread is busy, and then typing C-g when the
3210 menubar doesn't open promptly (with the result that the
3211 menubar never responds at all because the deferred
3212 WM_INITMENU message is never completed). Another problem
3213 situation is when the lisp thread calls SendMessage (to send
3214 a window manager command) when a message has been deferred;
3215 the lisp thread gets blocked indefinitely waiting for the
3216 deferred message to be completed, which itself is waiting for
3217 the lisp thread to respond.
3219 Note that we don't want to block the input thread waiting for
3220 a response from the lisp thread (although that would at least
3221 solve the deadlock problem above), because we want to be able
3222 to receive C-g to interrupt the lisp thread. */
3223 cancel_all_deferred_msgs ();
3226 signal_user_input ();
3229 my_post_msg (&wmsg
, hwnd
, msg
, wParam
, lParam
);
3233 get_wm_chars (HWND aWnd
, int *buf
, int buflen
, int ignore_ctrl
, int ctrl
,
3234 int *ctrl_cnt
, int *is_dead
, int vk
, int exp
)
3237 /* If doubled is at the end, ignore it. */
3238 int i
= buflen
, doubled
= 0, code_unit
;
3244 eassert (w32_unicode_gui
);
3246 /* Should be called only when w32_unicode_gui: */
3247 && PeekMessageW (&msg
, aWnd
, WM_KEYFIRST
, WM_KEYLAST
,
3248 PM_NOREMOVE
| PM_NOYIELD
)
3249 && (msg
.message
== WM_CHAR
|| msg
.message
== WM_SYSCHAR
3250 || msg
.message
== WM_DEADCHAR
|| msg
.message
== WM_SYSDEADCHAR
3251 || msg
.message
== WM_UNICHAR
))
3253 /* We extract character payload, but in this call we handle only the
3254 characters which come BEFORE the next keyup/keydown message. */
3257 GetMessageW (&msg
, aWnd
, msg
.message
, msg
.message
);
3258 dead
= (msg
.message
== WM_DEADCHAR
|| msg
.message
== WM_SYSDEADCHAR
);
3260 *is_dead
= (dead
? msg
.wParam
: -1);
3263 code_unit
= msg
.wParam
;
3266 /* Had surrogate. */
3267 if (msg
.message
== WM_UNICHAR
3268 || code_unit
< 0xDC00 || code_unit
> 0xDFFF)
3269 { /* Mismatched first surrogate.
3270 Pass both code units as if they were two characters. */
3273 return i
; /* Drop the 2nd char if at the end of the buffer. */
3275 else /* see https://en.wikipedia.org/wiki/UTF-16 */
3276 code_unit
= (doubled
<< 10) + code_unit
- 0x35FDC00;
3279 else if (code_unit
>= 0xD800 && code_unit
<= 0xDBFF)
3281 /* Handle mismatched 2nd surrogate the same as a normal character. */
3282 doubled
= code_unit
;
3286 /* The only "fake" characters delivered by ToUnicode() or
3287 TranslateMessage() are:
3288 0x01 .. 0x1a for Ctrl-letter, Enter, Tab, Ctrl-Break, Esc, Backspace
3289 0x00 and 0x1b .. 0x1f for Control- []\@^_
3290 0x7f for Control-BackSpace
3291 0x20 for Control-Space */
3293 && (code_unit
< 0x20 || code_unit
== 0x7f
3294 || (code_unit
== 0x20 && ctrl
)))
3296 /* Non-character payload in a WM_CHAR
3297 (Ctrl-something pressed, see above). Ignore, and report. */
3302 /* Traditionally, Emacs would ignore the character payload of VK_NUMPAD*
3303 keys, and would treat them later via `function-key-map'. In addition
3304 to usual 102-key NUMPAD keys, this map also treats `kp-'-variants of
3305 space, tab, enter, separator, equal. TAB and EQUAL, apparently,
3306 cannot be generated on Win-GUI branch. ENTER is already handled
3307 by the code above. According to `lispy_function_keys', kp_space is
3308 generated by not-extended VK_CLEAR. (kp-tab != VK_OEM_NEC_EQUAL!).
3310 We do similarly for backward-compatibility, but ignore only the
3311 characters restorable later by `function-key-map'. */
3312 if (code_unit
< 0x7f
3313 && ((vk
>= VK_NUMPAD0
&& vk
<= VK_DIVIDE
)
3314 || (exp
&& ((vk
>= VK_PRIOR
&& vk
<= VK_DOWN
) ||
3315 vk
== VK_INSERT
|| vk
== VK_DELETE
|| vk
== VK_CLEAR
)))
3316 && strchr ("0123456789/*-+.,", code_unit
))
3325 # define FPRINTF_WM_CHARS(ARG) fprintf ARG
3327 # define FPRINTF_WM_CHARS(ARG) (void)0
3330 /* This is a heuristic only. This is supposed to track the state of the
3331 finite automaton in the language environment of Windows.
3333 However, separate windows (if with the same different language
3334 environments!) should have different values. Moreover, switching to a
3335 non-Emacs window with the same language environment, and using (dead)keys
3336 there would change the value stored in the kernel, but not this value. */
3337 /* A layout may emit deadkey=0. It looks like this would reset the state
3338 of the kernel's finite automaton (equivalent to emiting 0-length string,
3339 which is otherwise impossible in the dead-key map of a layout).
3340 Be ready to treat the case when this delivers WM_(SYS)DEADCHAR. */
3341 static int after_deadkey
= -1;
3344 deliver_wm_chars (int do_translate
, HWND hwnd
, UINT msg
, UINT wParam
,
3345 UINT lParam
, int legacy_alt_meta
)
3347 /* An "old style" keyboard description may assign up to 125 UTF-16 code
3348 points to a keypress.
3349 (However, the "old style" TranslateMessage() would deliver at most 16 of
3350 them.) Be on a safe side, and prepare to treat many more. */
3351 int ctrl_cnt
, buf
[1024], count
, is_dead
, after_dead
= (after_deadkey
> 0);
3353 /* Since the keypress processing logic of Windows has a lot of state, it
3354 is important to call TranslateMessage() for every keyup/keydown, AND
3355 do it exactly once. (The actual change of state is done by
3356 ToUnicode[Ex](), which is called by TranslateMessage(). So one can
3357 call ToUnicode[Ex]() instead.)
3359 The "usual" message pump calls TranslateMessage() for EVERY event.
3360 Emacs calls TranslateMessage() very selectively (is it needed for doing
3361 some tricky stuff with Win95??? With newer Windows, selectiveness is,
3362 most probably, not needed -- and harms a lot).
3364 So, with the usual message pump, the following call to TranslateMessage()
3365 is not needed (and is going to be VERY harmful). With Emacs' message
3366 pump, the call is needed. */
3369 MSG windows_msg
= { hwnd
, msg
, wParam
, lParam
, 0, {0,0} };
3371 windows_msg
.time
= GetMessageTime ();
3372 TranslateMessage (&windows_msg
);
3374 count
= get_wm_chars (hwnd
, buf
, sizeof (buf
)/sizeof (*buf
), 1,
3375 /* The message may have been synthesized by
3376 who knows what; be conservative. */
3377 modifier_set (VK_LCONTROL
)
3378 || modifier_set (VK_RCONTROL
)
3379 || modifier_set (VK_CONTROL
),
3380 &ctrl_cnt
, &is_dead
, wParam
,
3381 (lParam
& 0x1000000L
) != 0);
3385 DWORD console_modifiers
= construct_console_modifiers ();
3386 int *b
= buf
, strip_ExtraMods
= 1, hairy
= 0;
3387 char *type_CtrlAlt
= NULL
;
3389 /* XXXX In fact, there may be another case when we need to do the same:
3390 What happens if the string defined in the LIGATURES has length
3391 0? Probably, we will get count==0, but the state of the finite
3392 automaton would reset to 0??? */
3395 /* wParam is checked when converting CapsLock to Shift; this is a clone
3396 of w32_get_key_modifiers (). */
3397 wmsg
.dwModifiers
= w32_kbd_mods_to_emacs (console_modifiers
, wParam
);
3399 /* What follows is just heuristics; the correct treatement requires
3400 non-destructive ToUnicode():
3401 http://search.cpan.org/~ilyaz/UI-KeyboardLayout/lib/UI/KeyboardLayout.pm#Can_an_application_on_Windows_accept_keyboard_events?_Part_IV:_application-specific_modifiers
3403 What one needs to find is:
3404 * which of the present modifiers AFFECT the resulting char(s)
3405 (so should be stripped, since their EFFECT is "already
3406 taken into account" in the string in buf), and
3407 * which modifiers are not affecting buf, so should be reported to
3408 the application for further treatment.
3410 Example: assume that we know:
3411 (A) lCtrl+rCtrl+rAlt modifiers with VK_A key produce a Latin "f"
3412 ("may be logical" in JCUKEN-flavored Russian keyboard flavors);
3413 (B) removing any of lCtrl, rCtrl, rAlt changes the produced char;
3414 (C) Win-modifier is not affecting the produced character
3415 (this is the common case: happens with all "standard" layouts).
3417 Suppose the user presses Win+lCtrl+rCtrl+rAlt modifiers with VK_A.
3418 What is the intent of the user? We need to guess the intent to decide
3419 which event to deliver to the application.
3421 This looks like a reasonable logic: since Win- modifier doesn't affect
3422 the output string, the user was pressing Win for SOME OTHER purpose.
3423 So the user wanted to generate Win-SOMETHING event. Now, what is
3424 something? If one takes the mantra that "character payload is more
3425 important than the combination of keypresses which resulted in this
3426 payload", then one should ignore lCtrl+rCtrl+rAlt, ignore VK_A, and
3427 assume that the user wanted to generate Win-f.
3429 Unfortunately, without non-destructive ToUnicode(), checking (B),(C)
3430 is out of question. So we use heuristics (hopefully, covering
3431 99.9999% of cases). */
3433 /* Another thing to watch for is a possibility to use AltGr-* and
3434 Ctrl-Alt-* with different semantic.
3436 Background: the layout defining the KLLF_ALTGR bit are treated
3437 specially by the kernel: when VK_RMENU (=rightAlt, =AltGr) is pressed
3438 (released), a press (release) of VK_LCONTROL is emulated (unless Ctrl
3439 is already down). As a result, any press/release of AltGr is seen
3440 by applications as a press/release of lCtrl AND rAlt. This is
3441 applicable, in particular, to ToUnicode[Ex](). (Keyrepeat is covered
3444 NOTE: it IS possible to see bare rAlt even with KLLF_ALTGR; but this
3445 requires a good finger coordination: doing (physically)
3446 Down-lCtrl Down-rAlt Up-lCtrl Down-a
3447 (doing quick enough, so that key repeat of rAlt [which would
3448 generate new "fake" Down-lCtrl events] does not happens before 'a'
3449 is down) results in no "fake" events, so the application will see
3450 only rAlt down when 'a' is pressed. (However, fake Up-lCtrl WILL
3451 be generated when rAlt goes UP.)
3453 In fact, note also that KLLF_ALTGR does not prohibit construction of
3454 rCtrl-rAlt (just press them in this order!).
3456 Moreover: "traditional" layouts do not define distinct modifier-masks
3457 for VK_LMENU and VK_RMENU (same for VK_L/RCONTROL). Instead, they
3458 rely on the KLLF_ALTGR bit to make the behavior of VK_LMENU and
3459 VK_RMENU distinct. As a corollary, for such layouts, the produced
3460 character is the same for AltGr-* (=rAlt-*) and Ctrl-Alt-* (in any
3461 combination of handedness). For description of masks, see
3463 http://search.cpan.org/~ilyaz/UI-KeyboardLayout/lib/UI/KeyboardLayout.pm#Keyboard_input_on_Windows,_Part_I:_what_is_the_kernel_doing?
3465 By default, Emacs was using these coincidences via the following
3466 heuristics: it was treating:
3467 (*) keypresses with lCtrl-rAlt modifiers as if they are carrying
3468 ONLY the character payload (no matter what the actual keyboard
3469 was defining: if lCtrl-lAlt-b was delivering U+05df=beta, then
3470 Emacs saw [beta]; if lCtrl-lAlt-b was undefined in the layout,
3471 the keypress was completely ignored), and
3472 (*) keypresses with the other combinations of handedness of Ctrl-Alt
3473 modifiers (e.g., lCtrl-lAlt) as if they NEVER carry a character
3474 payload (so they were reported "raw": if lCtrl-lAlt-b was
3475 delivering beta, then Emacs saw event [C-A-b], and not [beta]).
3476 This worked good for "traditional" layouts: users could type both
3477 AltGr-x and Ctrl-Alt-x, and one was a character, another a bindable
3480 However, for layouts which deliver different characters for AltGr-x
3481 and lCtrl-lAlt-x, this scheme makes the latter character unaccessible
3482 in Emacs. While it is easy to access functionality of [C-M-x] in
3483 Emacs by other means (for example, by the `controlify' prefix, or
3484 using lCtrl-rCtrl-x, or rCtrl-rAlt-x [in this order]), missing
3485 characters cannot be reconstructed without a tedious manual work. */
3487 /* These two cases are often going to be distinguishable, since at most
3488 one of these character is defined with KBDCTRL | KBDMENU modifier
3489 bitmap. (This heuristic breaks if both lCtrl-lAlt- AND lCtrl-rAlt-
3490 are translated to modifier bitmaps distinct from KBDCTRL | KBDMENU,
3491 or in the cases when lCtrl-lAlt-* and lCtrl-rAlt-* are generally
3492 different, but lCtrl-lAlt-x and lCtrl-rAlt-x happen to deliver the
3495 So we have 2 chunks of info:
3496 (A) is it lCtrl-rAlt-, or lCtrl-lAlt, or some other combination?
3497 (B) is the delivered character defined with KBDCTRL | KBDMENU bits?
3498 Basing on (A) and (B), we should decide whether to ignore the
3499 delivered character. (Before, Emacs was completely ignoring (B), and
3500 was treating the 3-state of (A) as a bit.) This means that we have 6
3501 bits of customization.
3503 Additionally, a presence of two Ctrl down may be AltGr-rCtrl-. */
3505 /* Strip all non-Shift modifiers if:
3506 - more than one UTF-16 code point delivered (can't call VkKeyScanW ())
3507 - or the character is a result of combining with a prefix key. */
3508 if (!after_dead
&& count
== 1 && *b
< 0x10000)
3510 if (console_modifiers
& (RIGHT_ALT_PRESSED
| LEFT_ALT_PRESSED
)
3511 && console_modifiers
& (RIGHT_CTRL_PRESSED
| LEFT_CTRL_PRESSED
))
3513 type_CtrlAlt
= "bB"; /* generic bindable Ctrl-Alt- modifiers */
3514 if ((console_modifiers
& (LEFT_CTRL_PRESSED
| RIGHT_CTRL_PRESSED
))
3515 == (LEFT_CTRL_PRESSED
| RIGHT_CTRL_PRESSED
))
3517 e.g. AltGr-rCtrl on some layouts (in this order!) */
3518 type_CtrlAlt
= "dD";
3519 else if ((console_modifiers
3520 & (LEFT_CTRL_PRESSED
| LEFT_ALT_PRESSED
))
3521 == (LEFT_CTRL_PRESSED
| LEFT_ALT_PRESSED
))
3522 type_CtrlAlt
= "lL"; /* Ctrl-Alt- modifiers on the left */
3523 else if (!NILP (Vw32_recognize_altgr
)
3524 && ((console_modifiers
3525 & (RIGHT_ALT_PRESSED
| LEFT_CTRL_PRESSED
)))
3526 == (RIGHT_ALT_PRESSED
| LEFT_CTRL_PRESSED
))
3527 type_CtrlAlt
= "gG"; /* modifiers as in AltGr */
3529 else if (wmsg
.dwModifiers
& (alt_modifier
| meta_modifier
)
3530 || ((console_modifiers
3531 & (LEFT_WIN_PRESSED
| RIGHT_WIN_PRESSED
3532 | APPS_PRESSED
| SCROLLLOCK_ON
))))
3534 /* Pure Alt (or combination of Alt, Win, APPS, scrolllock. */
3535 type_CtrlAlt
= "aA";
3539 /* Out of bound bitmap: */
3540 SHORT r
= VkKeyScanW (*b
), bitmap
= 0x1FF;
3542 FPRINTF_WM_CHARS((stderr
, "VkKeyScanW %#06x %#04x\n", (int)r
,
3544 if ((r
& 0xFF) == wParam
)
3545 bitmap
= r
>>8; /* *b is reachable via simple interface */
3546 if (*type_CtrlAlt
== 'a') /* Simple Alt seen */
3548 if ((bitmap
& ~1) == 0) /* 1: KBDSHIFT */
3550 /* In "traditional" layouts, Alt without Ctrl does not
3551 change the delivered character. This detects this
3552 situation; it is safe to report this as Alt-something
3553 -- as opposed to delivering the reported character
3554 without modifiers. */
3556 && *b
> 0x7f && ('A' <= wParam
&& wParam
<= 'Z'))
3557 /* For backward-compatibility with older Emacsen, let
3558 this be processed by another branch below (which
3559 would convert it to Alt-Latin char via wParam). */
3565 /* Check whether the delivered character(s) is accessible via
3566 KBDCTRL | KBDALT ( | KBDSHIFT ) modifier mask (which is 7). */
3567 else if ((bitmap
& ~1) != 6)
3569 /* The character is not accessible via plain Ctrl-Alt(-Shift)
3570 (which is, probably, same as AltGr) modifiers.
3571 Either it was after a prefix key, or is combined with
3572 modifier keys which we don't see, or there is an asymmetry
3573 between left-hand and right-hand modifiers, or other hairy
3577 /* The best solution is to delegate these tough (but rarely
3578 needed) choices to the user. Temporarily (???), it is
3579 implemented as C macros.
3581 Essentially, there are 3 things to do: return 0 (handle to the
3582 legacy processing code [ignoring the character payload]; keep
3583 some modifiers (so that they will be processed by the binding
3584 system [on top of the character payload]; strip modifiers [so
3585 that `self-insert' is going to be triggered with the character
3588 The default below should cover 99.9999% of cases:
3589 (a) strip Alt- in the hairy case only;
3590 (stripping = not ignoring)
3591 (l) for lAlt-lCtrl, ignore the char in simple cases only;
3592 (g) for what looks like AltGr, ignore the modifiers;
3593 (d) for what looks like lCtrl-rCtrl-Alt (probably
3594 AltGr-rCtrl), ignore the character in simple cases only;
3595 (b) for other cases of Ctrl-Alt, ignore the character in
3598 Essentially, in all hairy cases, and in looks-like-AltGr case,
3599 we keep the character, ignoring the modifiers. In all the
3600 other cases, we ignore the delivered character. */
3601 #define S_TYPES_TO_IGNORE_CHARACTER_PAYLOAD "aldb"
3602 #define S_TYPES_TO_REPORT_CHARACTER_PAYLOAD_WITH_MODIFIERS ""
3603 if (strchr (S_TYPES_TO_IGNORE_CHARACTER_PAYLOAD
,
3604 type_CtrlAlt
[hairy
]))
3606 /* If in neither list, report all the modifiers we see COMBINED
3607 WITH the reported character. */
3608 if (strchr (S_TYPES_TO_REPORT_CHARACTER_PAYLOAD_WITH_MODIFIERS
,
3609 type_CtrlAlt
[hairy
]))
3610 strip_ExtraMods
= 0;
3613 if (strip_ExtraMods
)
3614 wmsg
.dwModifiers
= wmsg
.dwModifiers
& shift_modifier
;
3616 signal_user_input ();
3619 FPRINTF_WM_CHARS((stderr
, "unichar %#06x\n", *b
));
3620 my_post_msg (&wmsg
, hwnd
, WM_UNICHAR
, *b
++, lParam
);
3622 if (!ctrl_cnt
) /* Process ALSO as ctrl */
3625 FPRINTF_WM_CHARS((stderr
, "extra ctrl char\n"));
3628 else if (is_dead
>= 0)
3630 FPRINTF_WM_CHARS((stderr
, "dead %#06x\n", is_dead
));
3631 after_deadkey
= is_dead
;
3637 /* Main window procedure */
3639 static LRESULT CALLBACK
3640 w32_wnd_proc (HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
3643 struct w32_display_info
*dpyinfo
= &one_w32_display_info
;
3645 int windows_translate
;
3648 /* Note that it is okay to call x_window_to_frame, even though we are
3649 not running in the main lisp thread, because frame deletion
3650 requires the lisp thread to synchronize with this thread. Thus, if
3651 a frame struct is returned, it can be used without concern that the
3652 lisp thread might make it disappear while we are using it.
3654 NB. Walking the frame list in this thread is safe (as long as
3655 writes of Lisp_Object slots are atomic, which they are on Windows).
3656 Although delete-frame can destructively modify the frame list while
3657 we are walking it, a garbage collection cannot occur until after
3658 delete-frame has synchronized with this thread.
3660 It is also safe to use functions that make GDI calls, such as
3661 w32_clear_rect, because these functions must obtain a DC handle
3662 from the frame struct using get_frame_dc which is thread-aware. */
3667 f
= x_window_to_frame (dpyinfo
, hwnd
);
3670 HDC hdc
= get_frame_dc (f
);
3671 GetUpdateRect (hwnd
, &wmsg
.rect
, FALSE
);
3672 w32_clear_rect (f
, hdc
, &wmsg
.rect
);
3673 release_frame_dc (f
, hdc
);
3675 #if defined (W32_DEBUG_DISPLAY)
3676 DebPrint (("WM_ERASEBKGND (frame %p): erasing %d,%d-%d,%d\n",
3678 wmsg
.rect
.left
, wmsg
.rect
.top
,
3679 wmsg
.rect
.right
, wmsg
.rect
.bottom
));
3680 #endif /* W32_DEBUG_DISPLAY */
3683 case WM_PALETTECHANGED
:
3684 /* ignore our own changes */
3685 if ((HWND
)wParam
!= hwnd
)
3687 f
= x_window_to_frame (dpyinfo
, hwnd
);
3689 /* get_frame_dc will realize our palette and force all
3690 frames to be redrawn if needed. */
3691 release_frame_dc (f
, get_frame_dc (f
));
3696 PAINTSTRUCT paintStruct
;
3698 memset (&update_rect
, 0, sizeof (update_rect
));
3700 f
= x_window_to_frame (dpyinfo
, hwnd
);
3703 DebPrint (("WM_PAINT received for unknown window %p\n", hwnd
));
3707 /* MSDN Docs say not to call BeginPaint if GetUpdateRect
3708 fails. Apparently this can happen under some
3710 if (GetUpdateRect (hwnd
, &update_rect
, FALSE
) || !w32_strict_painting
)
3713 BeginPaint (hwnd
, &paintStruct
);
3715 /* The rectangles returned by GetUpdateRect and BeginPaint
3716 do not always match. Play it safe by assuming both areas
3718 UnionRect (&(wmsg
.rect
), &update_rect
, &(paintStruct
.rcPaint
));
3720 #if defined (W32_DEBUG_DISPLAY)
3721 DebPrint (("WM_PAINT (frame %p): painting %d,%d-%d,%d\n",
3723 wmsg
.rect
.left
, wmsg
.rect
.top
,
3724 wmsg
.rect
.right
, wmsg
.rect
.bottom
));
3725 DebPrint ((" [update region is %d,%d-%d,%d]\n",
3726 update_rect
.left
, update_rect
.top
,
3727 update_rect
.right
, update_rect
.bottom
));
3729 EndPaint (hwnd
, &paintStruct
);
3732 /* Change the message type to prevent Windows from
3733 combining WM_PAINT messages in the Lisp thread's queue,
3734 since Windows assumes that each message queue is
3735 dedicated to one frame and does not bother checking
3736 that hwnd matches before combining them. */
3737 my_post_msg (&wmsg
, hwnd
, WM_EMACS_PAINT
, wParam
, lParam
);
3742 /* If GetUpdateRect returns 0 (meaning there is no update
3743 region), assume the whole window needs to be repainted. */
3744 GetClientRect (hwnd
, &wmsg
.rect
);
3745 my_post_msg (&wmsg
, hwnd
, msg
, wParam
, lParam
);
3749 case WM_INPUTLANGCHANGE
:
3750 /* Inform lisp thread of keyboard layout changes. */
3751 my_post_msg (&wmsg
, hwnd
, msg
, wParam
, lParam
);
3753 /* The state of the finite automaton is separate per every input
3754 language environment (so it does not change when one switches
3755 to a different window with the same environment). Moreover,
3756 the experiments show that the state is not remembered when
3757 one switches back to the pre-previous environment. */
3760 /* XXXX??? What follows is a COMPLETE misunderstanding of Windows! */
3762 /* Clear dead keys in the keyboard state; for simplicity only
3763 preserve modifier key states. */
3768 GetKeyboardState (keystate
);
3769 for (i
= 0; i
< 256; i
++)
3786 SetKeyboardState (keystate
);
3791 /* Synchronize hot keys with normal input. */
3792 PostMessage (hwnd
, WM_KEYDOWN
, HIWORD (lParam
), 0);
3797 record_keyup (wParam
, lParam
);
3802 /* Ignore keystrokes we fake ourself; see below. */
3803 if (dpyinfo
->faked_key
== wParam
)
3805 dpyinfo
->faked_key
= 0;
3806 /* Make sure TranslateMessage sees them though (as long as
3807 they don't produce WM_CHAR messages). This ensures that
3808 indicator lights are toggled promptly on Windows 9x, for
3810 if (wParam
< 256 && lispy_function_keys
[wParam
])
3812 windows_translate
= 1;
3818 /* Synchronize modifiers with current keystroke. */
3820 record_keydown (wParam
, lParam
);
3821 if (w32_use_fallback_wm_chars_method
)
3822 wParam
= map_keypad_keys (wParam
, (lParam
& 0x1000000L
) != 0);
3824 windows_translate
= 0;
3829 if (!w32_kbdhook_active
&& NILP (Vw32_pass_lwindow_to_system
))
3831 /* Prevent system from acting on keyup (which opens the
3832 Start menu if no other key was pressed) by simulating a
3833 press of Space which we will ignore. */
3834 if (GetAsyncKeyState (wParam
) & 1)
3836 if (NUMBERP (Vw32_phantom_key_code
))
3837 key
= XUINT (Vw32_phantom_key_code
) & 255;
3840 dpyinfo
->faked_key
= key
;
3841 keybd_event (key
, (BYTE
) MapVirtualKey (key
, 0), 0, 0);
3844 if (!NILP (Vw32_lwindow_modifier
))
3848 if (!w32_kbdhook_active
&& NILP (Vw32_pass_rwindow_to_system
))
3850 if (GetAsyncKeyState (wParam
) & 1)
3852 if (NUMBERP (Vw32_phantom_key_code
))
3853 key
= XUINT (Vw32_phantom_key_code
) & 255;
3856 dpyinfo
->faked_key
= key
;
3857 keybd_event (key
, (BYTE
) MapVirtualKey (key
, 0), 0, 0);
3860 if (!NILP (Vw32_rwindow_modifier
))
3864 if (!NILP (Vw32_apps_modifier
))
3868 if (NILP (Vw32_pass_alt_to_system
))
3869 /* Prevent DefWindowProc from activating the menu bar if an
3870 Alt key is pressed and released by itself. */
3872 windows_translate
= 1;
3875 /* Decide whether to treat as modifier or function key. */
3876 if (NILP (Vw32_enable_caps_lock
))
3877 goto disable_lock_key
;
3878 windows_translate
= 1;
3881 /* Decide whether to treat as modifier or function key. */
3882 if (NILP (Vw32_enable_num_lock
))
3883 goto disable_lock_key
;
3884 windows_translate
= 1;
3887 /* Decide whether to treat as modifier or function key. */
3888 if (NILP (Vw32_scroll_lock_modifier
))
3889 goto disable_lock_key
;
3890 windows_translate
= 1;
3893 /* Ensure the appropriate lock key state (and indicator light)
3894 remains in the same state. We do this by faking another
3895 press of the relevant key. Apparently, this really is the
3896 only way to toggle the state of the indicator lights. */
3897 dpyinfo
->faked_key
= wParam
;
3898 keybd_event ((BYTE
) wParam
, (BYTE
) MapVirtualKey (wParam
, 0),
3899 KEYEVENTF_EXTENDEDKEY
| KEYEVENTF_KEYUP
, 0);
3900 keybd_event ((BYTE
) wParam
, (BYTE
) MapVirtualKey (wParam
, 0),
3901 KEYEVENTF_EXTENDEDKEY
| 0, 0);
3902 keybd_event ((BYTE
) wParam
, (BYTE
) MapVirtualKey (wParam
, 0),
3903 KEYEVENTF_EXTENDEDKEY
| KEYEVENTF_KEYUP
, 0);
3904 /* Ensure indicator lights are updated promptly on Windows 9x
3905 (TranslateMessage apparently does this), after forwarding
3907 post_character_message (hwnd
, msg
, wParam
, lParam
,
3908 w32_get_key_modifiers (wParam
, lParam
));
3909 windows_translate
= 1;
3913 case VK_PROCESSKEY
: /* Generated by IME. */
3914 windows_translate
= 1;
3917 /* Windows maps Ctrl-Pause (aka Ctrl-Break) into VK_CANCEL,
3918 which is confusing for purposes of key binding; convert
3919 VK_CANCEL events into VK_PAUSE events. */
3923 /* Windows maps Ctrl-NumLock into VK_PAUSE, which is confusing
3924 for purposes of key binding; convert these back into
3925 VK_NUMLOCK events, at least when we want to see NumLock key
3926 presses. (Note that there is never any possibility that
3927 VK_PAUSE with Ctrl really is C-Pause as per above.) */
3928 if (NILP (Vw32_enable_num_lock
) && modifier_set (VK_CONTROL
))
3929 wParam
= VK_NUMLOCK
;
3932 if (w32_unicode_gui
&& !w32_use_fallback_wm_chars_method
)
3934 /* If this event generates characters or deadkeys, do
3935 not interpret it as a "raw combination of modifiers
3936 and keysym". Hide deadkeys, and use the generated
3937 character(s) instead of the keysym. (Backward
3938 compatibility: exceptions for numpad keys generating
3939 0-9 . , / * - +, and for extra-Alt combined with a
3942 Try to not report modifiers which have effect on
3943 which character or deadkey is generated.
3945 Example (contrived): if rightAlt-? generates f (on a
3946 Cyrillic keyboard layout), and Ctrl, leftAlt do not
3947 affect the generated character, one wants to report
3948 Ctrl-leftAlt-f if the user presses
3949 Ctrl-leftAlt-rightAlt-?. */
3952 /* Some of WM_CHAR may be fed to us directly, some are
3953 results of TranslateMessage(). Using 0 as the first
3954 argument (in a separate call) might help us
3955 distinguish these two cases.
3957 However, the keypress feeders would most probably
3958 expect the "standard" message pump, when
3959 TranslateMessage() is called on EVERY KeyDown/KeyUp
3960 event. So they may feed us Down-Ctrl Down-FAKE
3961 Char-o and expect us to recognize it as Ctrl-o.
3962 Using 0 as the first argument would interfere with
3964 deliver_wm_chars (0, hwnd
, msg
, wParam
, lParam
, 1);
3966 /* Processing the generated WM_CHAR messages *WHILE* we
3967 handle KEYDOWN/UP event is the best choice, since
3968 without any fuss, we know all 3 of: scancode, virtual
3969 keycode, and expansion. (Additionally, one knows
3970 boundaries of expansion of different keypresses.) */
3971 res
= deliver_wm_chars (1, hwnd
, msg
, wParam
, lParam
, 1);
3972 windows_translate
= -(res
!= 0);
3973 if (res
> 0) /* Bound to character(s) or a deadkey */
3975 /* deliver_wm_chars may make some branches after this vestigal. */
3977 wParam
= map_keypad_keys (wParam
, (lParam
& 0x1000000L
) != 0);
3978 /* If not defined as a function key, change it to a WM_CHAR message. */
3979 if (wParam
> 255 || !lispy_function_keys
[wParam
])
3981 DWORD modifiers
= construct_console_modifiers ();
3983 if (!NILP (Vw32_recognize_altgr
)
3984 && modifier_set (VK_LCONTROL
) && modifier_set (VK_RMENU
))
3986 /* Always let TranslateMessage handle AltGr key chords;
3987 for some reason, ToAscii doesn't always process AltGr
3988 chords correctly. */
3989 windows_translate
= 1;
3991 else if ((modifiers
& (~SHIFT_PRESSED
& ~CAPSLOCK_ON
)) != 0)
3993 /* Handle key chords including any modifiers other
3994 than shift directly, in order to preserve as much
3995 modifier information as possible. */
3996 if ('A' <= wParam
&& wParam
<= 'Z')
3998 /* Don't translate modified alphabetic keystrokes,
3999 so the user doesn't need to constantly switch
4000 layout to type control or meta keystrokes when
4001 the normal layout translates alphabetic
4002 characters to non-ascii characters. */
4003 if (!modifier_set (VK_SHIFT
))
4004 wParam
+= ('a' - 'A');
4009 /* Try to handle other keystrokes by determining the
4010 base character (ie. translating the base key plus
4013 KEY_EVENT_RECORD key
;
4015 key
.bKeyDown
= TRUE
;
4016 key
.wRepeatCount
= 1;
4017 key
.wVirtualKeyCode
= wParam
;
4018 key
.wVirtualScanCode
= (lParam
& 0xFF0000) >> 16;
4019 key
.uChar
.AsciiChar
= 0;
4020 key
.dwControlKeyState
= modifiers
;
4022 add
= w32_kbd_patch_key (&key
, w32_keyboard_codepage
);
4023 /* 0 means an unrecognized keycode, negative means
4024 dead key. Ignore both. */
4027 /* Forward asciified character sequence. */
4028 post_character_message
4030 (unsigned char) key
.uChar
.AsciiChar
, lParam
,
4031 w32_get_key_modifiers (wParam
, lParam
));
4032 w32_kbd_patch_key (&key
, w32_keyboard_codepage
);
4039 /* Let TranslateMessage handle everything else. */
4040 windows_translate
= 1;
4045 if (windows_translate
== -1)
4048 if (windows_translate
)
4050 MSG windows_msg
= { hwnd
, msg
, wParam
, lParam
, 0, {0,0} };
4051 windows_msg
.time
= GetMessageTime ();
4052 TranslateMessage (&windows_msg
);
4064 wmsg
.dwModifiers
= w32_get_key_modifiers (wParam
, lParam
);
4065 signal_user_input ();
4066 my_post_msg (&wmsg
, hwnd
, WM_UNICHAR
, wParam
, lParam
);
4070 post_character_message (hwnd
, msg
, wParam
, lParam
,
4071 w32_get_key_modifiers (wParam
, lParam
));
4075 /* WM_UNICHAR looks promising from the docs, but the exact
4076 circumstances in which TranslateMessage sends it is one of those
4077 Microsoft secret API things that EU and US courts are supposed
4078 to have put a stop to already. Spy++ shows it being sent to Notepad
4079 and other MS apps, but never to Emacs.
4081 Some third party IMEs send it in accordance with the official
4082 documentation though, so handle it here.
4084 UNICODE_NOCHAR is used to test for support for this message.
4085 TRUE indicates that the message is supported. */
4086 if (wParam
== UNICODE_NOCHAR
)
4091 wmsg
.dwModifiers
= w32_get_key_modifiers (wParam
, lParam
);
4092 signal_user_input ();
4093 my_post_msg (&wmsg
, hwnd
, msg
, wParam
, lParam
);
4098 /* If we can't get the IME result as Unicode, use default processing,
4099 which will at least allow characters decodable in the system locale
4101 if (!get_composition_string_fn
)
4104 else if (!ignore_ime_char
)
4109 HIMC context
= get_ime_context_fn (hwnd
);
4110 wmsg
.dwModifiers
= w32_get_key_modifiers (wParam
, lParam
);
4111 /* Get buffer size. */
4112 size
= get_composition_string_fn (context
, GCS_RESULTSTR
, NULL
, 0);
4113 buffer
= alloca (size
);
4114 size
= get_composition_string_fn (context
, GCS_RESULTSTR
,
4116 release_ime_context_fn (hwnd
, context
);
4118 signal_user_input ();
4119 for (i
= 0; i
< size
/ sizeof (wchar_t); i
++)
4121 my_post_msg (&wmsg
, hwnd
, WM_UNICHAR
, (WPARAM
) buffer
[i
],
4124 /* Ignore the messages for the rest of the
4125 characters in the string that was output above. */
4126 ignore_ime_char
= (size
/ sizeof (wchar_t)) - 1;
4133 case WM_IME_STARTCOMPOSITION
:
4134 if (!set_ime_composition_window_fn
)
4138 COMPOSITIONFORM form
;
4142 /* Implementation note: The code below does something that
4143 one shouldn't do: it accesses the window object from a
4144 separate thread, while the main (a.k.a. "Lisp") thread
4145 runs and can legitimately delete and even GC it. That is
4146 why we are extra careful not to futz with a window that
4147 is different from the one recorded when the system caret
4148 coordinates were last modified. That is also why we are
4149 careful not to move the IME window if the window
4150 described by W was deleted, as indicated by its buffer
4151 field being reset to nil. */
4152 f
= x_window_to_frame (dpyinfo
, hwnd
);
4153 if (!(f
&& FRAME_LIVE_P (f
)))
4155 w
= XWINDOW (FRAME_SELECTED_WINDOW (f
));
4156 /* Punt if someone changed the frame's selected window
4158 if (w
!= w32_system_caret_window
)
4161 form
.dwStyle
= CFS_RECT
;
4162 form
.ptCurrentPos
.x
= w32_system_caret_x
;
4163 form
.ptCurrentPos
.y
= w32_system_caret_y
;
4165 form
.rcArea
.left
= WINDOW_TEXT_TO_FRAME_PIXEL_X (w
, 0);
4166 form
.rcArea
.top
= (WINDOW_TOP_EDGE_Y (w
)
4167 + w32_system_caret_hdr_height
);
4168 form
.rcArea
.right
= (WINDOW_BOX_RIGHT_EDGE_X (w
)
4169 - WINDOW_RIGHT_MARGIN_WIDTH (w
)
4170 - WINDOW_RIGHT_FRINGE_WIDTH (w
));
4171 form
.rcArea
.bottom
= (WINDOW_BOTTOM_EDGE_Y (w
)
4172 - WINDOW_BOTTOM_DIVIDER_WIDTH (w
)
4173 - w32_system_caret_mode_height
);
4175 /* Punt if the window was deleted behind our back. */
4176 if (!BUFFERP (w
->contents
))
4179 context
= get_ime_context_fn (hwnd
);
4184 set_ime_composition_window_fn (context
, &form
);
4185 release_ime_context_fn (hwnd
, context
);
4187 /* We should "goto dflt" here to pass WM_IME_STARTCOMPOSITION to
4188 DefWindowProc, so that the composition window will actually
4189 be displayed. But doing so causes trouble with displaying
4190 dialog boxes, such as the file selection dialog or font
4191 selection dialog. So something else is needed to fix the
4192 former without breaking the latter. See bug#11732. */
4195 case WM_IME_ENDCOMPOSITION
:
4196 ignore_ime_char
= 0;
4199 /* Simulate middle mouse button events when left and right buttons
4200 are used together, but only if user has two button mouse. */
4201 case WM_LBUTTONDOWN
:
4202 case WM_RBUTTONDOWN
:
4203 if (w32_num_mouse_buttons
> 2)
4204 goto handle_plain_button
;
4207 int this = (msg
== WM_LBUTTONDOWN
) ? LMOUSE
: RMOUSE
;
4208 int other
= (msg
== WM_LBUTTONDOWN
) ? RMOUSE
: LMOUSE
;
4210 if (button_state
& this)
4213 if (button_state
== 0)
4216 button_state
|= this;
4218 if (button_state
& other
)
4220 if (mouse_button_timer
)
4222 KillTimer (hwnd
, mouse_button_timer
);
4223 mouse_button_timer
= 0;
4225 /* Generate middle mouse event instead. */
4226 msg
= WM_MBUTTONDOWN
;
4227 button_state
|= MMOUSE
;
4229 else if (button_state
& MMOUSE
)
4231 /* Ignore button event if we've already generated a
4232 middle mouse down event. This happens if the
4233 user releases and press one of the two buttons
4234 after we've faked a middle mouse event. */
4239 /* Flush out saved message. */
4240 post_msg (&saved_mouse_button_msg
);
4242 wmsg
.dwModifiers
= w32_get_modifiers ();
4243 my_post_msg (&wmsg
, hwnd
, msg
, wParam
, lParam
);
4244 signal_user_input ();
4246 /* Clear message buffer. */
4247 saved_mouse_button_msg
.msg
.hwnd
= 0;
4251 /* Hold onto message for now. */
4252 mouse_button_timer
=
4253 SetTimer (hwnd
, MOUSE_BUTTON_ID
,
4254 w32_mouse_button_tolerance
, NULL
);
4255 saved_mouse_button_msg
.msg
.hwnd
= hwnd
;
4256 saved_mouse_button_msg
.msg
.message
= msg
;
4257 saved_mouse_button_msg
.msg
.wParam
= wParam
;
4258 saved_mouse_button_msg
.msg
.lParam
= lParam
;
4259 saved_mouse_button_msg
.msg
.time
= GetMessageTime ();
4260 saved_mouse_button_msg
.dwModifiers
= w32_get_modifiers ();
4267 if (w32_num_mouse_buttons
> 2)
4268 goto handle_plain_button
;
4271 int this = (msg
== WM_LBUTTONUP
) ? LMOUSE
: RMOUSE
;
4272 int other
= (msg
== WM_LBUTTONUP
) ? RMOUSE
: LMOUSE
;
4274 if ((button_state
& this) == 0)
4277 button_state
&= ~this;
4279 if (button_state
& MMOUSE
)
4281 /* Only generate event when second button is released. */
4282 if ((button_state
& other
) == 0)
4285 button_state
&= ~MMOUSE
;
4287 if (button_state
) emacs_abort ();
4294 /* Flush out saved message if necessary. */
4295 if (saved_mouse_button_msg
.msg
.hwnd
)
4297 post_msg (&saved_mouse_button_msg
);
4300 wmsg
.dwModifiers
= w32_get_modifiers ();
4301 my_post_msg (&wmsg
, hwnd
, msg
, wParam
, lParam
);
4302 signal_user_input ();
4304 /* Always clear message buffer and cancel timer. */
4305 saved_mouse_button_msg
.msg
.hwnd
= 0;
4306 KillTimer (hwnd
, mouse_button_timer
);
4307 mouse_button_timer
= 0;
4309 if (button_state
== 0)
4314 case WM_XBUTTONDOWN
:
4316 if (w32_pass_extra_mouse_buttons_to_system
)
4318 /* else fall through and process them. */
4319 case WM_MBUTTONDOWN
:
4321 handle_plain_button
:
4326 /* Ignore middle and extra buttons as long as the menu is active. */
4327 f
= x_window_to_frame (dpyinfo
, hwnd
);
4328 if (f
&& f
->output_data
.w32
->menubar_active
)
4331 if (parse_button (msg
, HIWORD (wParam
), &button
, &up
))
4333 if (up
) ReleaseCapture ();
4334 else SetCapture (hwnd
);
4335 button
= (button
== 0) ? LMOUSE
:
4336 ((button
== 1) ? MMOUSE
: RMOUSE
);
4338 button_state
&= ~button
;
4340 button_state
|= button
;
4344 wmsg
.dwModifiers
= w32_get_modifiers ();
4345 my_post_msg (&wmsg
, hwnd
, msg
, wParam
, lParam
);
4346 signal_user_input ();
4348 /* Need to return true for XBUTTON messages, false for others,
4349 to indicate that we processed the message. */
4350 return (msg
== WM_XBUTTONDOWN
|| msg
== WM_XBUTTONUP
);
4353 f
= x_window_to_frame (dpyinfo
, hwnd
);
4356 /* Ignore mouse movements as long as the menu is active.
4357 These movements are processed by the window manager
4358 anyway, and it's wrong to handle them as if they happened
4359 on the underlying frame. */
4360 if (f
->output_data
.w32
->menubar_active
)
4363 /* If the mouse moved, and the mouse pointer is invisible,
4364 make it visible again. We do this here so as to be able
4365 to show the mouse pointer even when the main
4366 (a.k.a. "Lisp") thread is busy doing something. */
4367 static int last_x
, last_y
;
4368 int x
= GET_X_LPARAM (lParam
);
4369 int y
= GET_Y_LPARAM (lParam
);
4371 if (f
->pointer_invisible
4372 && (x
!= last_x
|| y
!= last_y
))
4373 f
->pointer_invisible
= false;
4379 /* If the mouse has just moved into the frame, start tracking
4380 it, so we will be notified when it leaves the frame. Mouse
4381 tracking only works under W98 and NT4 and later. On earlier
4382 versions, there is no way of telling when the mouse leaves the
4383 frame, so we just have to put up with help-echo and mouse
4384 highlighting remaining while the frame is not active. */
4385 if (track_mouse_event_fn
&& !track_mouse_window
4386 /* If the menu bar is active, turning on tracking of mouse
4387 movement events might send these events to the tooltip
4388 frame, if the user happens to move the mouse pointer over
4389 the tooltip. But since we don't process events for
4390 tooltip frames, this causes Windows to present a
4391 hourglass cursor, which is ugly and unexpected. So don't
4392 enable tracking mouse events in this case; they will be
4393 restarted when the menu pops down. (Confusingly, the
4394 menubar_active member of f->output_data.w32, tested
4395 above, is only set when a menu was popped up _not_ from
4396 the frame's menu bar, but via x-popup-menu.) */
4399 TRACKMOUSEEVENT tme
;
4400 tme
.cbSize
= sizeof (tme
);
4401 tme
.dwFlags
= TME_LEAVE
;
4402 tme
.hwndTrack
= hwnd
;
4403 tme
.dwHoverTime
= HOVER_DEFAULT
;
4405 track_mouse_event_fn (&tme
);
4406 track_mouse_window
= hwnd
;
4410 if (w32_mouse_move_interval
<= 0
4411 || (msg
== WM_MOUSEMOVE
&& button_state
== 0))
4413 wmsg
.dwModifiers
= w32_get_modifiers ();
4414 my_post_msg (&wmsg
, hwnd
, msg
, wParam
, lParam
);
4418 /* Hang onto mouse move and scroll messages for a bit, to avoid
4419 sending such events to Emacs faster than it can process them.
4420 If we get more events before the timer from the first message
4421 expires, we just replace the first message. */
4423 if (saved_mouse_move_msg
.msg
.hwnd
== 0)
4425 SetTimer (hwnd
, MOUSE_MOVE_ID
,
4426 w32_mouse_move_interval
, NULL
);
4428 /* Hold onto message for now. */
4429 saved_mouse_move_msg
.msg
.hwnd
= hwnd
;
4430 saved_mouse_move_msg
.msg
.message
= msg
;
4431 saved_mouse_move_msg
.msg
.wParam
= wParam
;
4432 saved_mouse_move_msg
.msg
.lParam
= lParam
;
4433 saved_mouse_move_msg
.msg
.time
= GetMessageTime ();
4434 saved_mouse_move_msg
.dwModifiers
= w32_get_modifiers ();
4440 wmsg
.dwModifiers
= w32_get_modifiers ();
4441 my_post_msg (&wmsg
, hwnd
, msg
, wParam
, lParam
);
4442 signal_user_input ();
4446 if (w32_pass_multimedia_buttons_to_system
)
4448 /* Otherwise, pass to lisp, the same way we do with mousehwheel. */
4449 case WM_MOUSEHWHEEL
:
4450 wmsg
.dwModifiers
= w32_get_modifiers ();
4451 my_post_msg (&wmsg
, hwnd
, msg
, wParam
, lParam
);
4452 signal_user_input ();
4453 /* Non-zero must be returned when WM_MOUSEHWHEEL messages are
4454 handled, to prevent the system trying to handle it by faking
4455 scroll bar events. */
4459 /* Flush out saved messages if necessary. */
4460 if (wParam
== mouse_button_timer
)
4462 if (saved_mouse_button_msg
.msg
.hwnd
)
4464 post_msg (&saved_mouse_button_msg
);
4465 signal_user_input ();
4466 saved_mouse_button_msg
.msg
.hwnd
= 0;
4468 KillTimer (hwnd
, mouse_button_timer
);
4469 mouse_button_timer
= 0;
4471 else if (wParam
== mouse_move_timer
)
4473 if (saved_mouse_move_msg
.msg
.hwnd
)
4475 post_msg (&saved_mouse_move_msg
);
4476 saved_mouse_move_msg
.msg
.hwnd
= 0;
4478 KillTimer (hwnd
, mouse_move_timer
);
4479 mouse_move_timer
= 0;
4481 else if (wParam
== menu_free_timer
)
4483 KillTimer (hwnd
, menu_free_timer
);
4484 menu_free_timer
= 0;
4485 f
= x_window_to_frame (dpyinfo
, hwnd
);
4486 /* If a popup menu is active, don't wipe its strings. */
4488 && current_popup_menu
== NULL
)
4490 /* Free memory used by owner-drawn and help-echo strings. */
4491 w32_free_menu_strings (hwnd
);
4493 f
->output_data
.w32
->menubar_active
= 0;
4500 /* Windows doesn't send us focus messages when putting up and
4501 taking down a system popup dialog as for Ctrl-Alt-Del on Windows 95.
4502 The only indication we get that something happened is receiving
4503 this message afterwards. So this is a good time to reset our
4504 keyboard modifiers' state. */
4511 /* We must ensure menu bar is fully constructed and up to date
4512 before allowing user interaction with it. To achieve this
4513 we send this message to the lisp thread and wait for a
4514 reply (whose value is not actually needed) to indicate that
4515 the menu bar is now ready for use, so we can now return.
4517 To remain responsive in the meantime, we enter a nested message
4518 loop that can process all other messages.
4520 However, we skip all this if the message results from calling
4521 TrackPopupMenu - in fact, we must NOT attempt to send the lisp
4522 thread a message because it is blocked on us at this point. We
4523 set menubar_active before calling TrackPopupMenu to indicate
4524 this (there is no possibility of confusion with real menubar
4527 f
= x_window_to_frame (dpyinfo
, hwnd
);
4529 && (f
->output_data
.w32
->menubar_active
4530 /* We can receive this message even in the absence of a
4531 menubar (ie. when the system menu is activated) - in this
4532 case we do NOT want to forward the message, otherwise it
4533 will cause the menubar to suddenly appear when the user
4534 had requested it to be turned off! */
4535 || f
->output_data
.w32
->menubar_widget
== NULL
))
4539 deferred_msg msg_buf
;
4541 /* Detect if message has already been deferred; in this case
4542 we cannot return any sensible value to ignore this. */
4543 if (find_deferred_msg (hwnd
, msg
) != NULL
)
4548 return send_deferred_msg (&msg_buf
, hwnd
, msg
, wParam
, lParam
);
4551 case WM_EXITMENULOOP
:
4552 f
= x_window_to_frame (dpyinfo
, hwnd
);
4554 /* If a menu is still active, check again after a short delay,
4555 since Windows often (always?) sends the WM_EXITMENULOOP
4556 before the corresponding WM_COMMAND message.
4557 Don't do this if a popup menu is active, since it is only
4558 menubar menus that require cleaning up in this way.
4560 if (f
&& menubar_in_use
&& current_popup_menu
== NULL
)
4561 menu_free_timer
= SetTimer (hwnd
, MENU_FREE_ID
, MENU_FREE_DELAY
, NULL
);
4563 /* If hourglass cursor should be displayed, display it now. */
4564 if (f
&& f
->output_data
.w32
->hourglass_p
)
4565 SetCursor (f
->output_data
.w32
->hourglass_cursor
);
4570 /* Direct handling of help_echo in menus. Should be safe now
4571 that we generate the help_echo by placing a help event in the
4574 HMENU menu
= (HMENU
) lParam
;
4575 UINT menu_item
= (UINT
) LOWORD (wParam
);
4576 UINT flags
= (UINT
) HIWORD (wParam
);
4578 w32_menu_display_help (hwnd
, menu
, menu_item
, flags
);
4582 case WM_MEASUREITEM
:
4583 f
= x_window_to_frame (dpyinfo
, hwnd
);
4586 MEASUREITEMSTRUCT
* pMis
= (MEASUREITEMSTRUCT
*) lParam
;
4588 if (pMis
->CtlType
== ODT_MENU
)
4590 /* Work out dimensions for popup menu titles. */
4591 char * title
= (char *) pMis
->itemData
;
4592 HDC hdc
= GetDC (hwnd
);
4593 HFONT menu_font
= GetCurrentObject (hdc
, OBJ_FONT
);
4594 LOGFONT menu_logfont
;
4598 GetObject (menu_font
, sizeof (menu_logfont
), &menu_logfont
);
4599 menu_logfont
.lfWeight
= FW_BOLD
;
4600 menu_font
= CreateFontIndirect (&menu_logfont
);
4601 old_font
= SelectObject (hdc
, menu_font
);
4603 pMis
->itemHeight
= GetSystemMetrics (SM_CYMENUSIZE
);
4606 if (unicode_append_menu
)
4607 GetTextExtentPoint32W (hdc
, (WCHAR
*) title
,
4608 wcslen ((WCHAR
*) title
),
4611 GetTextExtentPoint32 (hdc
, title
, strlen (title
), &size
);
4613 pMis
->itemWidth
= size
.cx
;
4614 if (pMis
->itemHeight
< size
.cy
)
4615 pMis
->itemHeight
= size
.cy
;
4618 pMis
->itemWidth
= 0;
4620 SelectObject (hdc
, old_font
);
4621 DeleteObject (menu_font
);
4622 ReleaseDC (hwnd
, hdc
);
4629 f
= x_window_to_frame (dpyinfo
, hwnd
);
4632 DRAWITEMSTRUCT
* pDis
= (DRAWITEMSTRUCT
*) lParam
;
4634 if (pDis
->CtlType
== ODT_MENU
)
4636 /* Draw popup menu title. */
4637 char * title
= (char *) pDis
->itemData
;
4640 HDC hdc
= pDis
->hDC
;
4641 HFONT menu_font
= GetCurrentObject (hdc
, OBJ_FONT
);
4642 LOGFONT menu_logfont
;
4645 GetObject (menu_font
, sizeof (menu_logfont
), &menu_logfont
);
4646 menu_logfont
.lfWeight
= FW_BOLD
;
4647 menu_font
= CreateFontIndirect (&menu_logfont
);
4648 old_font
= SelectObject (hdc
, menu_font
);
4650 /* Always draw title as if not selected. */
4651 if (unicode_append_menu
)
4654 + GetSystemMetrics (SM_CXMENUCHECK
),
4656 ETO_OPAQUE
, &pDis
->rcItem
,
4658 wcslen ((WCHAR
*) title
), NULL
);
4662 + GetSystemMetrics (SM_CXMENUCHECK
),
4664 ETO_OPAQUE
, &pDis
->rcItem
,
4665 title
, strlen (title
), NULL
);
4667 SelectObject (hdc
, old_font
);
4668 DeleteObject (menu_font
);
4676 /* Still not right - can't distinguish between clicks in the
4677 client area of the frame from clicks forwarded from the scroll
4678 bars - may have to hook WM_NCHITTEST to remember the mouse
4679 position and then check if it is in the client area ourselves. */
4680 case WM_MOUSEACTIVATE
:
4681 /* Discard the mouse click that activates a frame, allowing the
4682 user to click anywhere without changing point (or worse!).
4683 Don't eat mouse clicks on scrollbars though!! */
4684 if (LOWORD (lParam
) == HTCLIENT
)
4685 return MA_ACTIVATEANDEAT
;
4690 /* No longer tracking mouse. */
4691 track_mouse_window
= NULL
;
4693 case WM_ACTIVATEAPP
:
4695 case WM_WINDOWPOSCHANGED
:
4697 /* Inform lisp thread that a frame might have just been obscured
4698 or exposed, so should recheck visibility of all frames. */
4699 my_post_msg (&wmsg
, hwnd
, msg
, wParam
, lParam
);
4703 dpyinfo
->faked_key
= 0;
4705 if (!w32_kbdhook_active
)
4706 register_hot_keys (hwnd
);
4709 if (!w32_kbdhook_active
)
4710 unregister_hot_keys (hwnd
);
4713 /* Relinquish the system caret. */
4714 if (w32_system_caret_hwnd
)
4716 w32_visible_system_caret_hwnd
= NULL
;
4717 w32_system_caret_hwnd
= NULL
;
4723 f
= x_window_to_frame (dpyinfo
, hwnd
);
4724 if (f
&& HIWORD (wParam
) == 0)
4726 if (menu_free_timer
)
4728 KillTimer (hwnd
, menu_free_timer
);
4729 menu_free_timer
= 0;
4735 wmsg
.dwModifiers
= w32_get_modifiers ();
4736 my_post_msg (&wmsg
, hwnd
, msg
, wParam
, lParam
);
4741 setup_w32_kbdhook ();
4747 remove_w32_kbdhook ();
4752 case WM_WTSSESSION_CHANGE
:
4753 if (wParam
== WTS_SESSION_LOCK
)
4754 reset_w32_kbdhook_state ();
4758 wmsg
.dwModifiers
= w32_get_modifiers ();
4759 my_post_msg (&wmsg
, hwnd
, msg
, wParam
, lParam
);
4762 case WM_WINDOWPOSCHANGING
:
4763 /* Don't restrict the sizing of any kind of frames. If the window
4764 manager doesn't, there's no reason to do it ourselves. */
4767 case WM_GETMINMAXINFO
:
4768 /* Hack to allow resizing the Emacs frame above the screen size.
4769 Note that Windows 9x limits coordinates to 16-bits. */
4770 ((LPMINMAXINFO
) lParam
)->ptMaxTrackSize
.x
= 32767;
4771 ((LPMINMAXINFO
) lParam
)->ptMaxTrackSize
.y
= 32767;
4775 if (LOWORD (lParam
) == HTCLIENT
)
4777 f
= x_window_to_frame (dpyinfo
, hwnd
);
4780 if (f
->output_data
.w32
->hourglass_p
4781 && !menubar_in_use
&& !current_popup_menu
)
4782 SetCursor (f
->output_data
.w32
->hourglass_cursor
);
4783 else if (f
->pointer_invisible
)
4786 SetCursor (f
->output_data
.w32
->current_cursor
);
4793 case WM_EMACS_SETCURSOR
:
4795 Cursor cursor
= (Cursor
) wParam
;
4796 f
= x_window_to_frame (dpyinfo
, hwnd
);
4799 f
->output_data
.w32
->current_cursor
= cursor
;
4800 /* Don't change the cursor while menu-bar menu is in use. */
4801 if (!f
->output_data
.w32
->menubar_active
4802 && !f
->output_data
.w32
->hourglass_p
)
4804 if (f
->pointer_invisible
)
4813 case WM_EMACS_SHOWCURSOR
:
4815 ShowCursor ((BOOL
) wParam
);
4820 case WM_EMACS_CREATEVSCROLLBAR
:
4821 return (LRESULT
) w32_createvscrollbar ((struct frame
*) wParam
,
4822 (struct scroll_bar
*) lParam
);
4824 case WM_EMACS_CREATEHSCROLLBAR
:
4825 return (LRESULT
) w32_createhscrollbar ((struct frame
*) wParam
,
4826 (struct scroll_bar
*) lParam
);
4828 case WM_EMACS_SHOWWINDOW
:
4829 return ShowWindow ((HWND
) wParam
, (WPARAM
) lParam
);
4831 case WM_EMACS_BRINGTOTOP
:
4832 case WM_EMACS_SETFOREGROUND
:
4834 HWND foreground_window
;
4835 DWORD foreground_thread
, retval
;
4837 /* On NT 5.0, and apparently Windows 98, it is necessary to
4838 attach to the thread that currently has focus in order to
4839 pull the focus away from it. */
4840 foreground_window
= GetForegroundWindow ();
4841 foreground_thread
= GetWindowThreadProcessId (foreground_window
, NULL
);
4842 if (!foreground_window
4843 || foreground_thread
== GetCurrentThreadId ()
4844 || !AttachThreadInput (GetCurrentThreadId (),
4845 foreground_thread
, TRUE
))
4846 foreground_thread
= 0;
4848 retval
= SetForegroundWindow ((HWND
) wParam
);
4849 if (msg
== WM_EMACS_BRINGTOTOP
)
4850 retval
= BringWindowToTop ((HWND
) wParam
);
4852 /* Detach from the previous foreground thread. */
4853 if (foreground_thread
)
4854 AttachThreadInput (GetCurrentThreadId (),
4855 foreground_thread
, FALSE
);
4860 case WM_EMACS_SETWINDOWPOS
:
4862 WINDOWPOS
* pos
= (WINDOWPOS
*) wParam
;
4863 return SetWindowPos (hwnd
, pos
->hwndInsertAfter
,
4864 pos
->x
, pos
->y
, pos
->cx
, pos
->cy
, pos
->flags
);
4867 case WM_EMACS_DESTROYWINDOW
:
4868 DragAcceptFiles ((HWND
) wParam
, FALSE
);
4869 return DestroyWindow ((HWND
) wParam
);
4871 case WM_EMACS_HIDE_CARET
:
4872 return HideCaret (hwnd
);
4874 case WM_EMACS_SHOW_CARET
:
4875 return ShowCaret (hwnd
);
4877 case WM_EMACS_DESTROY_CARET
:
4878 w32_system_caret_hwnd
= NULL
;
4879 w32_visible_system_caret_hwnd
= NULL
;
4880 return DestroyCaret ();
4882 case WM_EMACS_TRACK_CARET
:
4883 /* If there is currently no system caret, create one. */
4884 if (w32_system_caret_hwnd
== NULL
)
4886 /* Use the default caret width, and avoid changing it
4887 unnecessarily, as it confuses screen reader software. */
4888 w32_system_caret_hwnd
= hwnd
;
4889 CreateCaret (hwnd
, NULL
, 0,
4890 w32_system_caret_height
);
4893 if (!SetCaretPos (w32_system_caret_x
, w32_system_caret_y
))
4895 /* Ensure visible caret gets turned on when requested. */
4896 else if (w32_use_visible_system_caret
4897 && w32_visible_system_caret_hwnd
!= hwnd
)
4899 w32_visible_system_caret_hwnd
= hwnd
;
4900 return ShowCaret (hwnd
);
4902 /* Ensure visible caret gets turned off when requested. */
4903 else if (!w32_use_visible_system_caret
4904 && w32_visible_system_caret_hwnd
)
4906 w32_visible_system_caret_hwnd
= NULL
;
4907 return HideCaret (hwnd
);
4912 case WM_EMACS_TRACKPOPUPMENU
:
4917 pos
= (POINT
*)lParam
;
4918 flags
= TPM_CENTERALIGN
;
4919 if (button_state
& LMOUSE
)
4920 flags
|= TPM_LEFTBUTTON
;
4921 else if (button_state
& RMOUSE
)
4922 flags
|= TPM_RIGHTBUTTON
;
4924 /* Remember we did a SetCapture on the initial mouse down event,
4925 so for safety, we make sure the capture is canceled now. */
4929 /* Use menubar_active to indicate that WM_INITMENU is from
4930 TrackPopupMenu below, and should be ignored. */
4931 f
= x_window_to_frame (dpyinfo
, hwnd
);
4933 f
->output_data
.w32
->menubar_active
= 1;
4935 if (TrackPopupMenu ((HMENU
)wParam
, flags
, pos
->x
, pos
->y
,
4939 /* Eat any mouse messages during popupmenu */
4940 while (PeekMessage (&amsg
, hwnd
, WM_MOUSEFIRST
, WM_MOUSELAST
,
4942 /* Get the menu selection, if any */
4943 if (PeekMessage (&amsg
, hwnd
, WM_COMMAND
, WM_COMMAND
, PM_REMOVE
))
4945 retval
= LOWORD (amsg
.wParam
);
4959 case WM_EMACS_FILENOTIFY
:
4960 my_post_msg (&wmsg
, hwnd
, msg
, wParam
, lParam
);
4964 /* Check for messages registered at runtime. */
4965 if (msg
== msh_mousewheel
)
4967 wmsg
.dwModifiers
= w32_get_modifiers ();
4968 my_post_msg (&wmsg
, hwnd
, msg
, wParam
, lParam
);
4969 signal_user_input ();
4974 return (w32_unicode_gui
? DefWindowProcW
: DefWindowProcA
) (hwnd
, msg
, wParam
, lParam
);
4977 /* The most common default return code for handled messages is 0. */
4982 my_create_window (struct frame
* f
)
4985 static int coords
[2];
4986 Lisp_Object left
, top
;
4987 struct w32_display_info
*dpyinfo
= &one_w32_display_info
;
4989 /* When called with RES_TYPE_NUMBER, x_get_arg will return zero for
4990 anything that is not a number and is not Qunbound. */
4991 left
= x_get_arg (dpyinfo
, Qnil
, Qleft
, "left", "Left", RES_TYPE_NUMBER
);
4992 top
= x_get_arg (dpyinfo
, Qnil
, Qtop
, "top", "Top", RES_TYPE_NUMBER
);
4993 if (EQ (left
, Qunbound
))
4994 coords
[0] = CW_USEDEFAULT
;
4996 coords
[0] = XINT (left
);
4997 if (EQ (top
, Qunbound
))
4998 coords
[1] = CW_USEDEFAULT
;
5000 coords
[1] = XINT (top
);
5002 if (!PostThreadMessage (dwWindowsThreadId
, WM_EMACS_CREATEWINDOW
,
5003 (WPARAM
)f
, (LPARAM
)coords
))
5005 GetMessage (&msg
, NULL
, WM_EMACS_DONE
, WM_EMACS_DONE
);
5009 /* Create a tooltip window. Unlike my_create_window, we do not do this
5010 indirectly via the Window thread, as we do not need to process Window
5011 messages for the tooltip. Creating tooltips indirectly also creates
5012 deadlocks when tooltips are created for menu items. */
5014 my_create_tip_window (struct frame
*f
)
5018 rect
.left
= rect
.top
= 0;
5019 rect
.right
= FRAME_PIXEL_WIDTH (f
);
5020 rect
.bottom
= FRAME_PIXEL_HEIGHT (f
);
5022 AdjustWindowRect (&rect
, f
->output_data
.w32
->dwStyle
, false);
5024 tip_window
= FRAME_W32_WINDOW (f
)
5025 = CreateWindow (EMACS_CLASS
,
5027 f
->output_data
.w32
->dwStyle
,
5030 rect
.right
- rect
.left
,
5031 rect
.bottom
- rect
.top
,
5032 FRAME_W32_WINDOW (SELECTED_FRAME ()), /* owner */
5039 SetWindowLong (tip_window
, WND_FONTWIDTH_INDEX
, FRAME_COLUMN_WIDTH (f
));
5040 SetWindowLong (tip_window
, WND_LINEHEIGHT_INDEX
, FRAME_LINE_HEIGHT (f
));
5041 SetWindowLong (tip_window
, WND_BORDER_INDEX
, FRAME_INTERNAL_BORDER_WIDTH (f
));
5042 SetWindowLong (tip_window
, WND_BACKGROUND_INDEX
, FRAME_BACKGROUND_PIXEL (f
));
5044 /* Tip frames have no scrollbars. */
5045 SetWindowLong (tip_window
, WND_VSCROLLBAR_INDEX
, 0);
5046 SetWindowLong (tip_window
, WND_HSCROLLBAR_INDEX
, 0);
5048 /* Do this to discard the default setting specified by our parent. */
5049 ShowWindow (tip_window
, SW_HIDE
);
5054 /* Create and set up the w32 window for frame F. */
5057 w32_window (struct frame
*f
, long window_prompting
, bool minibuffer_only
)
5061 /* Use the resource name as the top-level window name
5062 for looking up resources. Make a non-Lisp copy
5063 for the window manager, so GC relocation won't bother it.
5065 Elsewhere we specify the window name for the window manager. */
5066 f
->namebuf
= xlispstrdup (Vx_resource_name
);
5068 my_create_window (f
);
5070 validate_x_resource_name ();
5072 /* x_set_name normally ignores requests to set the name if the
5073 requested name is the same as the current name. This is the one
5074 place where that assumption isn't correct; f->name is set, but
5075 the server hasn't been told. */
5078 int explicit = f
->explicit_name
;
5080 f
->explicit_name
= 0;
5082 fset_name (f
, Qnil
);
5083 x_set_name (f
, name
, explicit);
5088 if (!minibuffer_only
&& FRAME_EXTERNAL_MENU_BAR (f
))
5089 initialize_frame_menubar (f
);
5091 if (FRAME_W32_WINDOW (f
) == 0)
5092 error ("Unable to create window");
5095 /* Handle the icon stuff for this window. Perhaps later we might
5096 want an x_set_icon_position which can be called interactively as
5100 x_icon (struct frame
*f
, Lisp_Object parms
)
5102 Lisp_Object icon_x
, icon_y
;
5103 struct w32_display_info
*dpyinfo
= &one_w32_display_info
;
5105 /* Set the position of the icon. Note that Windows 95 groups all
5106 icons in the tray. */
5107 icon_x
= x_get_arg (dpyinfo
, parms
, Qicon_left
, 0, 0, RES_TYPE_NUMBER
);
5108 icon_y
= x_get_arg (dpyinfo
, parms
, Qicon_top
, 0, 0, RES_TYPE_NUMBER
);
5109 if (!EQ (icon_x
, Qunbound
) && !EQ (icon_y
, Qunbound
))
5111 CHECK_NUMBER (icon_x
);
5112 CHECK_NUMBER (icon_y
);
5114 else if (!EQ (icon_x
, Qunbound
) || !EQ (icon_y
, Qunbound
))
5115 error ("Both left and top icon corners of icon must be specified");
5120 /* Start up iconic or window? */
5121 x_wm_set_window_state
5122 (f
, (EQ (x_get_arg (dpyinfo
, parms
, Qvisibility
, 0, 0, RES_TYPE_SYMBOL
), Qicon
)
5126 x_text_icon (f
, SSDATA ((!NILP (f
->icon_name
)
5136 x_make_gc (struct frame
*f
)
5138 XGCValues gc_values
;
5142 /* Create the GC's of this frame.
5143 Note that many default values are used. */
5146 gc_values
.font
= FRAME_FONT (f
);
5148 /* Cursor has cursor-color background, background-color foreground. */
5149 gc_values
.foreground
= FRAME_BACKGROUND_PIXEL (f
);
5150 gc_values
.background
= f
->output_data
.w32
->cursor_pixel
;
5151 f
->output_data
.w32
->cursor_gc
5152 = XCreateGC (NULL
, FRAME_W32_WINDOW (f
),
5153 (GCFont
| GCForeground
| GCBackground
),
5157 f
->output_data
.w32
->white_relief
.gc
= 0;
5158 f
->output_data
.w32
->black_relief
.gc
= 0;
5164 /* Handler for signals raised during x_create_frame and
5165 x_create_tip_frame. FRAME is the frame which is partially
5169 unwind_create_frame (Lisp_Object frame
)
5171 struct frame
*f
= XFRAME (frame
);
5173 /* If frame is ``official'', nothing to do. */
5174 if (NILP (Fmemq (frame
, Vframe_list
)))
5177 struct w32_display_info
*dpyinfo
= FRAME_DISPLAY_INFO (f
);
5179 /* If the frame's image cache refcount is still the same as our
5180 private shadow variable, it means we are unwinding a frame
5181 for which we didn't yet call init_frame_faces, where the
5182 refcount is incremented. Therefore, we increment it here, so
5183 that free_frame_faces, called in x_free_frame_resources
5184 below, will not mistakenly decrement the counter that was not
5185 incremented yet to account for this new frame. */
5186 if (FRAME_IMAGE_CACHE (f
) != NULL
5187 && FRAME_IMAGE_CACHE (f
)->refcount
== image_cache_refcount
)
5188 FRAME_IMAGE_CACHE (f
)->refcount
++;
5191 x_free_frame_resources (f
);
5195 /* Check that reference counts are indeed correct. */
5196 eassert (dpyinfo
->reference_count
== dpyinfo_refcount
);
5197 eassert ((dpyinfo
->terminal
->image_cache
== NULL
5198 && image_cache_refcount
== 0)
5199 || (dpyinfo
->terminal
->image_cache
!= NULL
5200 && dpyinfo
->terminal
->image_cache
->refcount
== image_cache_refcount
));
5209 do_unwind_create_frame (Lisp_Object frame
)
5211 unwind_create_frame (frame
);
5215 x_default_font_parameter (struct frame
*f
, Lisp_Object parms
)
5217 struct w32_display_info
*dpyinfo
= FRAME_DISPLAY_INFO (f
);
5218 Lisp_Object font_param
= x_get_arg (dpyinfo
, parms
, Qfont
, NULL
, NULL
,
5221 if (EQ (font_param
, Qunbound
))
5223 font
= !NILP (font_param
) ? font_param
5224 : x_get_arg (dpyinfo
, parms
, Qfont
, "font", "Font", RES_TYPE_STRING
);
5226 if (!STRINGP (font
))
5229 static char *names
[]
5230 = { "Courier New-10",
5231 "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1",
5232 "-*-Fixedsys-normal-r-*-*-12-*-*-*-c-*-iso8859-1",
5236 for (i
= 0; names
[i
]; i
++)
5238 font
= font_open_by_name (f
, build_unibyte_string (names
[i
]));
5243 error ("No suitable font was found");
5245 else if (!NILP (font_param
))
5247 /* Remember the explicit font parameter, so we can re-apply it after
5248 we've applied the `default' face settings. */
5249 x_set_frame_parameters (f
, Fcons (Fcons (Qfont_param
, font_param
), Qnil
));
5251 x_default_parameter (f
, parms
, Qfont
, font
, "font", "Font", RES_TYPE_STRING
);
5254 DEFUN ("x-create-frame", Fx_create_frame
, Sx_create_frame
,
5256 doc
: /* Make a new window, which is called a \"frame\" in Emacs terms.
5257 Return an Emacs frame object.
5258 PARAMETERS is an alist of frame parameters.
5259 If the parameters specify that the frame should not have a minibuffer,
5260 and do not specify a specific minibuffer window to use,
5261 then `default-minibuffer-frame' must be a frame whose minibuffer can
5262 be shared by the new frame.
5264 This function is an internal primitive--use `make-frame' instead. */)
5265 (Lisp_Object parameters
)
5268 Lisp_Object frame
, tem
;
5270 bool minibuffer_only
= false;
5271 long window_prompting
= 0;
5272 ptrdiff_t count
= SPECPDL_INDEX ();
5273 Lisp_Object display
;
5274 struct w32_display_info
*dpyinfo
= NULL
;
5277 int x_width
= 0, x_height
= 0;
5279 if (!FRAME_W32_P (SELECTED_FRAME ())
5280 && !FRAME_INITIAL_P (SELECTED_FRAME ()))
5281 error ("Cannot create a GUI frame in a -nw session");
5283 /* Make copy of frame parameters because the original is in pure
5285 parameters
= Fcopy_alist (parameters
);
5287 /* Use this general default value to start with
5288 until we know if this frame has a specified name. */
5289 Vx_resource_name
= Vinvocation_name
;
5291 display
= x_get_arg (dpyinfo
, parameters
, Qterminal
, 0, 0, RES_TYPE_NUMBER
);
5292 if (EQ (display
, Qunbound
))
5293 display
= x_get_arg (dpyinfo
, parameters
, Qdisplay
, 0, 0, RES_TYPE_STRING
);
5294 if (EQ (display
, Qunbound
))
5296 dpyinfo
= check_x_display_info (display
);
5297 kb
= dpyinfo
->terminal
->kboard
;
5299 if (!dpyinfo
->terminal
->name
)
5300 error ("Terminal is not live, can't create new frames on it");
5302 name
= x_get_arg (dpyinfo
, parameters
, Qname
, "name", "Name", RES_TYPE_STRING
);
5304 && ! EQ (name
, Qunbound
)
5306 error ("Invalid frame name--not a string or nil");
5309 Vx_resource_name
= name
;
5311 /* See if parent window is specified. */
5312 parent
= x_get_arg (dpyinfo
, parameters
, Qparent_id
, NULL
, NULL
, RES_TYPE_NUMBER
);
5313 if (EQ (parent
, Qunbound
))
5315 if (! NILP (parent
))
5316 CHECK_NUMBER (parent
);
5318 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
5319 /* No need to protect DISPLAY because that's not used after passing
5320 it to make_frame_without_minibuffer. */
5322 tem
= x_get_arg (dpyinfo
, parameters
, Qminibuffer
, "minibuffer", "Minibuffer",
5324 if (EQ (tem
, Qnone
) || NILP (tem
))
5325 f
= make_frame_without_minibuffer (Qnil
, kb
, display
);
5326 else if (EQ (tem
, Qonly
))
5328 f
= make_minibuffer_frame ();
5329 minibuffer_only
= true;
5331 else if (WINDOWP (tem
))
5332 f
= make_frame_without_minibuffer (tem
, kb
, display
);
5334 f
= make_frame (true);
5336 XSETFRAME (frame
, f
);
5338 /* By default, make scrollbars the system standard width and height. */
5339 FRAME_CONFIG_SCROLL_BAR_WIDTH (f
) = GetSystemMetrics (SM_CXVSCROLL
);
5340 FRAME_CONFIG_SCROLL_BAR_HEIGHT (f
) = GetSystemMetrics (SM_CXHSCROLL
);
5342 f
->terminal
= dpyinfo
->terminal
;
5344 f
->output_method
= output_w32
;
5345 f
->output_data
.w32
= xzalloc (sizeof (struct w32_output
));
5346 FRAME_FONTSET (f
) = -1;
5349 (f
, x_get_arg (dpyinfo
, parameters
, Qicon_name
, "iconName", "Title",
5351 if (! STRINGP (f
->icon_name
))
5352 fset_icon_name (f
, Qnil
);
5354 /* FRAME_DISPLAY_INFO (f) = dpyinfo; */
5356 /* With FRAME_DISPLAY_INFO set up, this unwind-protect is safe. */
5357 record_unwind_protect (do_unwind_create_frame
, frame
);
5360 image_cache_refcount
=
5361 FRAME_IMAGE_CACHE (f
) ? FRAME_IMAGE_CACHE (f
)->refcount
: 0;
5362 dpyinfo_refcount
= dpyinfo
->reference_count
;
5363 #endif /* GLYPH_DEBUG */
5365 /* Specify the parent under which to make this window. */
5368 /* Cast to UINT_PTR shuts up compiler warnings about cast to
5369 pointer from integer of different size. */
5370 f
->output_data
.w32
->parent_desc
= (Window
) (UINT_PTR
) XFASTINT (parent
);
5371 f
->output_data
.w32
->explicit_parent
= true;
5375 f
->output_data
.w32
->parent_desc
= FRAME_DISPLAY_INFO (f
)->root_window
;
5376 f
->output_data
.w32
->explicit_parent
= false;
5379 /* Set the name; the functions to which we pass f expect the name to
5381 if (EQ (name
, Qunbound
) || NILP (name
))
5383 fset_name (f
, build_string (dpyinfo
->w32_id_name
));
5384 f
->explicit_name
= false;
5388 fset_name (f
, name
);
5389 f
->explicit_name
= true;
5390 /* Use the frame's title when getting resources for this frame. */
5391 specbind (Qx_resource_name
, name
);
5394 if (uniscribe_available
)
5395 register_font_driver (&uniscribe_font_driver
, f
);
5396 register_font_driver (&w32font_driver
, f
);
5398 x_default_parameter (f
, parameters
, Qfont_backend
, Qnil
,
5399 "fontBackend", "FontBackend", RES_TYPE_STRING
);
5401 /* Extract the window parameters from the supplied values
5402 that are needed to determine window geometry. */
5403 x_default_font_parameter (f
, parameters
);
5405 x_default_parameter (f
, parameters
, Qborder_width
, make_number (2),
5406 "borderWidth", "BorderWidth", RES_TYPE_NUMBER
);
5408 /* We recognize either internalBorderWidth or internalBorder
5409 (which is what xterm calls it). */
5410 if (NILP (Fassq (Qinternal_border_width
, parameters
)))
5414 value
= x_get_arg (dpyinfo
, parameters
, Qinternal_border_width
,
5415 "internalBorder", "InternalBorder", RES_TYPE_NUMBER
);
5416 if (! EQ (value
, Qunbound
))
5417 parameters
= Fcons (Fcons (Qinternal_border_width
, value
),
5420 /* Default internalBorderWidth to 0 on Windows to match other programs. */
5421 x_default_parameter (f
, parameters
, Qinternal_border_width
, make_number (0),
5422 "internalBorderWidth", "InternalBorder", RES_TYPE_NUMBER
);
5423 x_default_parameter (f
, parameters
, Qright_divider_width
, make_number (0),
5424 NULL
, NULL
, RES_TYPE_NUMBER
);
5425 x_default_parameter (f
, parameters
, Qbottom_divider_width
, make_number (0),
5426 NULL
, NULL
, RES_TYPE_NUMBER
);
5427 x_default_parameter (f
, parameters
, Qvertical_scroll_bars
, Qright
,
5428 "verticalScrollBars", "ScrollBars", RES_TYPE_SYMBOL
);
5429 x_default_parameter (f
, parameters
, Qhorizontal_scroll_bars
, Qnil
,
5430 "horizontalScrollBars", "ScrollBars", RES_TYPE_SYMBOL
);
5432 /* Also do the stuff which must be set before the window exists. */
5433 x_default_parameter (f
, parameters
, Qforeground_color
, build_string ("black"),
5434 "foreground", "Foreground", RES_TYPE_STRING
);
5435 x_default_parameter (f
, parameters
, Qbackground_color
, build_string ("white"),
5436 "background", "Background", RES_TYPE_STRING
);
5437 x_default_parameter (f
, parameters
, Qmouse_color
, build_string ("black"),
5438 "pointerColor", "Foreground", RES_TYPE_STRING
);
5439 x_default_parameter (f
, parameters
, Qborder_color
, build_string ("black"),
5440 "borderColor", "BorderColor", RES_TYPE_STRING
);
5441 x_default_parameter (f
, parameters
, Qscreen_gamma
, Qnil
,
5442 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT
);
5443 x_default_parameter (f
, parameters
, Qline_spacing
, Qnil
,
5444 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER
);
5445 x_default_parameter (f
, parameters
, Qleft_fringe
, Qnil
,
5446 "leftFringe", "LeftFringe", RES_TYPE_NUMBER
);
5447 x_default_parameter (f
, parameters
, Qright_fringe
, Qnil
,
5448 "rightFringe", "RightFringe", RES_TYPE_NUMBER
);
5449 /* Process alpha here (Bug#16619). */
5450 x_default_parameter (f
, parameters
, Qalpha
, Qnil
,
5451 "alpha", "Alpha", RES_TYPE_NUMBER
);
5453 /* Init faces first since we need the frame's column width/line
5454 height in various occasions. */
5455 init_frame_faces (f
);
5457 /* The following call of change_frame_size is needed since otherwise
5458 x_set_tool_bar_lines will already work with the character sizes
5459 installed by init_frame_faces while the frame's pixel size is
5460 still calculated from a character size of 1 and we subsequently
5461 hit the (height >= 0) assertion in window_box_height.
5463 The non-pixelwise code apparently worked around this because it
5464 had one frame line vs one toolbar line which left us with a zero
5465 root window height which was obviously wrong as well ... */
5466 adjust_frame_size (f
, FRAME_COLS (f
) * FRAME_COLUMN_WIDTH (f
),
5467 FRAME_LINES (f
) * FRAME_LINE_HEIGHT (f
), 5, true,
5470 /* The X resources controlling the menu-bar and tool-bar are
5471 processed specially at startup, and reflected in the mode
5472 variables; ignore them here. */
5473 x_default_parameter (f
, parameters
, Qmenu_bar_lines
,
5474 NILP (Vmenu_bar_mode
)
5475 ? make_number (0) : make_number (1),
5476 NULL
, NULL
, RES_TYPE_NUMBER
);
5477 x_default_parameter (f
, parameters
, Qtool_bar_lines
,
5478 NILP (Vtool_bar_mode
)
5479 ? make_number (0) : make_number (1),
5480 NULL
, NULL
, RES_TYPE_NUMBER
);
5482 x_default_parameter (f
, parameters
, Qbuffer_predicate
, Qnil
,
5483 "bufferPredicate", "BufferPredicate", RES_TYPE_SYMBOL
);
5484 x_default_parameter (f
, parameters
, Qtitle
, Qnil
,
5485 "title", "Title", RES_TYPE_STRING
);
5487 f
->output_data
.w32
->dwStyle
= WS_OVERLAPPEDWINDOW
;
5488 f
->output_data
.w32
->parent_desc
= FRAME_DISPLAY_INFO (f
)->root_window
;
5490 f
->output_data
.w32
->text_cursor
= w32_load_cursor (IDC_IBEAM
);
5491 f
->output_data
.w32
->nontext_cursor
= w32_load_cursor (IDC_ARROW
);
5492 f
->output_data
.w32
->modeline_cursor
= w32_load_cursor (IDC_ARROW
);
5493 f
->output_data
.w32
->hand_cursor
= w32_load_cursor (IDC_HAND
);
5494 f
->output_data
.w32
->hourglass_cursor
= w32_load_cursor (IDC_WAIT
);
5495 f
->output_data
.w32
->horizontal_drag_cursor
= w32_load_cursor (IDC_SIZEWE
);
5496 f
->output_data
.w32
->vertical_drag_cursor
= w32_load_cursor (IDC_SIZENS
);
5498 f
->output_data
.w32
->current_cursor
= f
->output_data
.w32
->nontext_cursor
;
5500 window_prompting
= x_figure_window_size (f
, parameters
, true, &x_width
, &x_height
);
5502 tem
= x_get_arg (dpyinfo
, parameters
, Qunsplittable
, 0, 0, RES_TYPE_BOOLEAN
);
5503 f
->no_split
= minibuffer_only
|| EQ (tem
, Qt
);
5505 w32_window (f
, window_prompting
, minibuffer_only
);
5506 x_icon (f
, parameters
);
5510 /* Now consider the frame official. */
5511 f
->terminal
->reference_count
++;
5512 FRAME_DISPLAY_INFO (f
)->reference_count
++;
5513 Vframe_list
= Fcons (frame
, Vframe_list
);
5515 /* We need to do this after creating the window, so that the
5516 icon-creation functions can say whose icon they're describing. */
5517 x_default_parameter (f
, parameters
, Qicon_type
, Qnil
,
5518 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL
);
5520 x_default_parameter (f
, parameters
, Qauto_raise
, Qnil
,
5521 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN
);
5522 x_default_parameter (f
, parameters
, Qauto_lower
, Qnil
,
5523 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN
);
5524 x_default_parameter (f
, parameters
, Qcursor_type
, Qbox
,
5525 "cursorType", "CursorType", RES_TYPE_SYMBOL
);
5526 x_default_parameter (f
, parameters
, Qscroll_bar_width
, Qnil
,
5527 "scrollBarWidth", "ScrollBarWidth", RES_TYPE_NUMBER
);
5528 x_default_parameter (f
, parameters
, Qscroll_bar_height
, Qnil
,
5529 "scrollBarHeight", "ScrollBarHeight", RES_TYPE_NUMBER
);
5531 /* Allow x_set_window_size, now. */
5532 f
->can_x_set_window_size
= true;
5535 SET_FRAME_WIDTH (f
, x_width
);
5537 SET_FRAME_HEIGHT (f
, x_height
);
5539 /* Tell the server what size and position, etc, we want, and how
5540 badly we want them. This should be done after we have the menu
5541 bar so that its size can be taken into account. */
5543 x_wm_set_size_hint (f
, window_prompting
, false);
5546 adjust_frame_size (f
, FRAME_TEXT_WIDTH (f
), FRAME_TEXT_HEIGHT (f
), 0, true,
5549 /* Process fullscreen parameter here in the hope that normalizing a
5550 fullheight/fullwidth frame will produce the size set by the last
5551 adjust_frame_size call. */
5552 x_default_parameter (f
, parameters
, Qfullscreen
, Qnil
,
5553 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL
);
5555 /* Make the window appear on the frame and enable display, unless
5556 the caller says not to. However, with explicit parent, Emacs
5557 cannot control visibility, so don't try. */
5558 if (! f
->output_data
.w32
->explicit_parent
)
5560 Lisp_Object visibility
;
5562 visibility
= x_get_arg (dpyinfo
, parameters
, Qvisibility
, 0, 0, RES_TYPE_SYMBOL
);
5563 if (EQ (visibility
, Qunbound
))
5566 if (EQ (visibility
, Qicon
))
5567 x_iconify_frame (f
);
5568 else if (! NILP (visibility
))
5569 x_make_frame_visible (f
);
5571 /* Must have been Qnil. */
5575 /* Initialize `default-minibuffer-frame' in case this is the first
5576 frame on this terminal. */
5577 if (FRAME_HAS_MINIBUF_P (f
)
5578 && (!FRAMEP (KVAR (kb
, Vdefault_minibuffer_frame
))
5579 || !FRAME_LIVE_P (XFRAME (KVAR (kb
, Vdefault_minibuffer_frame
)))))
5580 kset_default_minibuffer_frame (kb
, frame
);
5582 /* All remaining specified parameters, which have not been "used"
5583 by x_get_arg and friends, now go in the misc. alist of the frame. */
5584 for (tem
= parameters
; CONSP (tem
); tem
= XCDR (tem
))
5585 if (CONSP (XCAR (tem
)) && !NILP (XCAR (XCAR (tem
))))
5586 fset_param_alist (f
, Fcons (XCAR (tem
), f
->param_alist
));
5588 /* Make sure windows on this frame appear in calls to next-window
5589 and similar functions. */
5590 Vwindow_list
= Qnil
;
5592 return unbind_to (count
, frame
);
5595 /* FRAME is used only to get a handle on the X display. We don't pass the
5596 display info directly because we're called from frame.c, which doesn't
5597 know about that structure. */
5599 x_get_focus_frame (struct frame
*frame
)
5601 struct w32_display_info
*dpyinfo
= FRAME_DISPLAY_INFO (frame
);
5603 if (! dpyinfo
->w32_focus_frame
)
5606 XSETFRAME (xfocus
, dpyinfo
->w32_focus_frame
);
5610 DEFUN ("xw-color-defined-p", Fxw_color_defined_p
, Sxw_color_defined_p
, 1, 2, 0,
5611 doc
: /* Internal function called by `color-defined-p', which see.
5612 (Note that the Nextstep version of this function ignores FRAME.) */)
5613 (Lisp_Object color
, Lisp_Object frame
)
5616 struct frame
*f
= decode_window_system_frame (frame
);
5618 CHECK_STRING (color
);
5620 if (w32_defined_color (f
, SSDATA (color
), &foo
, false))
5626 DEFUN ("xw-color-values", Fxw_color_values
, Sxw_color_values
, 1, 2, 0,
5627 doc
: /* Internal function called by `color-values', which see. */)
5628 (Lisp_Object color
, Lisp_Object frame
)
5631 struct frame
*f
= decode_window_system_frame (frame
);
5633 CHECK_STRING (color
);
5635 if (w32_defined_color (f
, SSDATA (color
), &foo
, false))
5636 return list3i ((GetRValue (foo
.pixel
) << 8) | GetRValue (foo
.pixel
),
5637 (GetGValue (foo
.pixel
) << 8) | GetGValue (foo
.pixel
),
5638 (GetBValue (foo
.pixel
) << 8) | GetBValue (foo
.pixel
));
5643 DEFUN ("xw-display-color-p", Fxw_display_color_p
, Sxw_display_color_p
, 0, 1, 0,
5644 doc
: /* Internal function called by `display-color-p', which see. */)
5645 (Lisp_Object display
)
5647 struct w32_display_info
*dpyinfo
= check_x_display_info (display
);
5649 if ((dpyinfo
->n_planes
* dpyinfo
->n_cbits
) <= 2)
5655 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p
,
5656 Sx_display_grayscale_p
, 0, 1, 0,
5657 doc
: /* Return t if DISPLAY supports shades of gray.
5658 Note that color displays do support shades of gray.
5659 The optional argument DISPLAY specifies which display to ask about.
5660 DISPLAY should be either a frame or a display name (a string).
5661 If omitted or nil, that stands for the selected frame's display. */)
5662 (Lisp_Object display
)
5664 struct w32_display_info
*dpyinfo
= check_x_display_info (display
);
5666 if ((dpyinfo
->n_planes
* dpyinfo
->n_cbits
) <= 1)
5672 DEFUN ("x-display-pixel-width", Fx_display_pixel_width
,
5673 Sx_display_pixel_width
, 0, 1, 0,
5674 doc
: /* Return the width in pixels of DISPLAY.
5675 The optional argument DISPLAY specifies which display to ask about.
5676 DISPLAY should be either a frame or a display name (a string).
5677 If omitted or nil, that stands for the selected frame's display.
5679 On \"multi-monitor\" setups this refers to the pixel width for all
5680 physical monitors associated with DISPLAY. To get information for
5681 each physical monitor, use `display-monitor-attributes-list'. */)
5682 (Lisp_Object display
)
5684 struct w32_display_info
*dpyinfo
= check_x_display_info (display
);
5686 return make_number (x_display_pixel_width (dpyinfo
));
5689 DEFUN ("x-display-pixel-height", Fx_display_pixel_height
,
5690 Sx_display_pixel_height
, 0, 1, 0,
5691 doc
: /* Return the height in pixels of DISPLAY.
5692 The optional argument DISPLAY specifies which display to ask about.
5693 DISPLAY should be either a frame or a display name (a string).
5694 If omitted or nil, that stands for the selected frame's display.
5696 On \"multi-monitor\" setups this refers to the pixel height for all
5697 physical monitors associated with DISPLAY. To get information for
5698 each physical monitor, use `display-monitor-attributes-list'. */)
5699 (Lisp_Object display
)
5701 struct w32_display_info
*dpyinfo
= check_x_display_info (display
);
5703 return make_number (x_display_pixel_height (dpyinfo
));
5706 DEFUN ("x-display-planes", Fx_display_planes
, Sx_display_planes
,
5708 doc
: /* Return the number of bitplanes of DISPLAY.
5709 The optional argument DISPLAY specifies which display to ask about.
5710 DISPLAY should be either a frame or a display name (a string).
5711 If omitted or nil, that stands for the selected frame's display. */)
5712 (Lisp_Object display
)
5714 struct w32_display_info
*dpyinfo
= check_x_display_info (display
);
5716 return make_number (dpyinfo
->n_planes
* dpyinfo
->n_cbits
);
5719 DEFUN ("x-display-color-cells", Fx_display_color_cells
, Sx_display_color_cells
,
5721 doc
: /* Return the number of color cells of DISPLAY.
5722 The optional argument DISPLAY specifies which display to ask about.
5723 DISPLAY should be either a frame or a display name (a string).
5724 If omitted or nil, that stands for the selected frame's display. */)
5725 (Lisp_Object display
)
5727 struct w32_display_info
*dpyinfo
= check_x_display_info (display
);
5730 /* Don't use NCOLORS: it returns incorrect results under remote
5731 * desktop. We force 24+ bit depths to 24-bit, both to prevent an
5732 * overflow and because probably is more meaningful on Windows
5735 cap
= 1 << min (dpyinfo
->n_planes
* dpyinfo
->n_cbits
, 24);
5736 return make_number (cap
);
5739 DEFUN ("x-server-max-request-size", Fx_server_max_request_size
,
5740 Sx_server_max_request_size
,
5742 doc
: /* Return the maximum request size of the server of DISPLAY.
5743 The optional argument DISPLAY specifies which display to ask about.
5744 DISPLAY should be either a frame or a display name (a string).
5745 If omitted or nil, that stands for the selected frame's display. */)
5746 (Lisp_Object display
)
5748 return make_number (1);
5751 DEFUN ("x-server-vendor", Fx_server_vendor
, Sx_server_vendor
, 0, 1, 0,
5752 doc
: /* Return the "vendor ID" string of the GUI software on TERMINAL.
5754 (Labeling every distributor as a "vendor" embodies the false assumption
5755 that operating systems cannot be developed and distributed noncommercially.)
5757 For GNU and Unix systems, this queries the X server software; for
5758 MS-Windows, this queries the OS.
5760 The optional argument TERMINAL specifies which display to ask about.
5761 TERMINAL should be a terminal object, a frame or a display name (a string).
5762 If omitted or nil, that stands for the selected frame's display. */)
5763 (Lisp_Object terminal
)
5765 return build_string ("Microsoft Corp.");
5768 DEFUN ("x-server-version", Fx_server_version
, Sx_server_version
, 0, 1, 0,
5769 doc
: /* Return the version numbers of the GUI software on TERMINAL.
5770 The value is a list of three integers specifying the version of the GUI
5773 For GNU and Unix system, the first 2 numbers are the version of the X
5774 Protocol used on TERMINAL and the 3rd number is the distributor-specific
5775 release number. For MS-Windows, the 3 numbers report the version and
5776 the build number of the OS.
5778 See also the function `x-server-vendor'.
5780 The optional argument TERMINAL specifies which display to ask about.
5781 TERMINAL should be a terminal object, a frame or a display name (a string).
5782 If omitted or nil, that stands for the selected frame's display. */)
5783 (Lisp_Object terminal
)
5785 return list3i (w32_major_version
, w32_minor_version
, w32_build_number
);
5788 DEFUN ("x-display-screens", Fx_display_screens
, Sx_display_screens
, 0, 1, 0,
5789 doc
: /* Return the number of screens on the server of DISPLAY.
5790 The optional argument DISPLAY specifies which display to ask about.
5791 DISPLAY should be either a frame or a display name (a string).
5792 If omitted or nil, that stands for the selected frame's display. */)
5793 (Lisp_Object display
)
5795 return make_number (1);
5798 DEFUN ("x-display-mm-height", Fx_display_mm_height
,
5799 Sx_display_mm_height
, 0, 1, 0,
5800 doc
: /* Return the height in millimeters of DISPLAY.
5801 The optional argument DISPLAY specifies which display to ask about.
5802 DISPLAY should be either a frame or a display name (a string).
5803 If omitted or nil, that stands for the selected frame's display.
5805 On \"multi-monitor\" setups this refers to the height in millimeters for
5806 all physical monitors associated with DISPLAY. To get information
5807 for each physical monitor, use `display-monitor-attributes-list'. */)
5808 (Lisp_Object display
)
5810 struct w32_display_info
*dpyinfo
= check_x_display_info (display
);
5812 double mm_per_pixel
;
5815 mm_per_pixel
= ((double) GetDeviceCaps (hdc
, VERTSIZE
)
5816 / GetDeviceCaps (hdc
, VERTRES
));
5817 ReleaseDC (NULL
, hdc
);
5819 return make_number (x_display_pixel_height (dpyinfo
) * mm_per_pixel
+ 0.5);
5822 DEFUN ("x-display-mm-width", Fx_display_mm_width
, Sx_display_mm_width
, 0, 1, 0,
5823 doc
: /* Return the width in millimeters of DISPLAY.
5824 The optional argument DISPLAY specifies which display to ask about.
5825 DISPLAY should be either a frame or a display name (a string).
5826 If omitted or nil, that stands for the selected frame's display.
5828 On \"multi-monitor\" setups this refers to the width in millimeters for
5829 all physical monitors associated with TERMINAL. To get information
5830 for each physical monitor, use `display-monitor-attributes-list'. */)
5831 (Lisp_Object display
)
5833 struct w32_display_info
*dpyinfo
= check_x_display_info (display
);
5835 double mm_per_pixel
;
5838 mm_per_pixel
= ((double) GetDeviceCaps (hdc
, HORZSIZE
)
5839 / GetDeviceCaps (hdc
, HORZRES
));
5840 ReleaseDC (NULL
, hdc
);
5842 return make_number (x_display_pixel_width (dpyinfo
) * mm_per_pixel
+ 0.5);
5845 DEFUN ("x-display-backing-store", Fx_display_backing_store
,
5846 Sx_display_backing_store
, 0, 1, 0,
5847 doc
: /* Return an indication of whether DISPLAY does backing store.
5848 The value may be `always', `when-mapped', or `not-useful'.
5849 The optional argument DISPLAY specifies which display to ask about.
5850 DISPLAY should be either a frame or a display name (a string).
5851 If omitted or nil, that stands for the selected frame's display. */)
5852 (Lisp_Object display
)
5854 return intern ("not-useful");
5857 DEFUN ("x-display-visual-class", Fx_display_visual_class
,
5858 Sx_display_visual_class
, 0, 1, 0,
5859 doc
: /* Return the visual class of DISPLAY.
5860 The value is one of the symbols `static-gray', `gray-scale',
5861 `static-color', `pseudo-color', `true-color', or `direct-color'.
5863 The optional argument DISPLAY specifies which display to ask about.
5864 DISPLAY should be either a frame or a display name (a string).
5865 If omitted or nil, that stands for the selected frame's display. */)
5866 (Lisp_Object display
)
5868 struct w32_display_info
*dpyinfo
= check_x_display_info (display
);
5869 Lisp_Object result
= Qnil
;
5871 if (dpyinfo
->has_palette
)
5872 result
= intern ("pseudo-color");
5873 else if (dpyinfo
->n_planes
* dpyinfo
->n_cbits
== 1)
5874 result
= intern ("static-grey");
5875 else if (dpyinfo
->n_planes
* dpyinfo
->n_cbits
== 4)
5876 result
= intern ("static-color");
5877 else if (dpyinfo
->n_planes
* dpyinfo
->n_cbits
> 8)
5878 result
= intern ("true-color");
5883 DEFUN ("x-display-save-under", Fx_display_save_under
,
5884 Sx_display_save_under
, 0, 1, 0,
5885 doc
: /* Return t if DISPLAY supports the save-under feature.
5886 The optional argument DISPLAY specifies which display to ask about.
5887 DISPLAY should be either a frame or a display name (a string).
5888 If omitted or nil, that stands for the selected frame's display. */)
5889 (Lisp_Object display
)
5894 static BOOL CALLBACK ALIGN_STACK
5895 w32_monitor_enum (HMONITOR monitor
, HDC hdc
, RECT
*rcMonitor
, LPARAM dwData
)
5897 Lisp_Object
*monitor_list
= (Lisp_Object
*) dwData
;
5899 *monitor_list
= Fcons (make_save_ptr (monitor
), *monitor_list
);
5905 w32_display_monitor_attributes_list (void)
5907 Lisp_Object attributes_list
= Qnil
, primary_monitor_attributes
= Qnil
;
5908 Lisp_Object monitor_list
= Qnil
, monitor_frames
, rest
, frame
;
5912 if (!(enum_display_monitors_fn
&& get_monitor_info_fn
5913 && monitor_from_window_fn
))
5916 if (!enum_display_monitors_fn (NULL
, NULL
, w32_monitor_enum
,
5917 (LPARAM
) &monitor_list
)
5918 || NILP (monitor_list
))
5922 for (rest
= monitor_list
; CONSP (rest
); rest
= XCDR (rest
))
5925 monitors
= xmalloc (n_monitors
* sizeof (*monitors
));
5926 for (i
= 0; i
< n_monitors
; i
++)
5928 monitors
[i
] = XSAVE_POINTER (XCAR (monitor_list
), 0);
5929 monitor_list
= XCDR (monitor_list
);
5932 monitor_frames
= Fmake_vector (make_number (n_monitors
), Qnil
);
5933 FOR_EACH_FRAME (rest
, frame
)
5935 struct frame
*f
= XFRAME (frame
);
5937 if (FRAME_W32_P (f
) && !EQ (frame
, tip_frame
))
5940 monitor_from_window_fn (FRAME_W32_WINDOW (f
),
5941 MONITOR_DEFAULT_TO_NEAREST
);
5943 for (i
= 0; i
< n_monitors
; i
++)
5944 if (monitors
[i
] == monitor
)
5948 ASET (monitor_frames
, i
, Fcons (frame
, AREF (monitor_frames
, i
)));
5952 for (i
= 0; i
< n_monitors
; i
++)
5954 Lisp_Object geometry
, workarea
, name
, attributes
= Qnil
;
5956 int width_mm
, height_mm
;
5957 struct MONITOR_INFO_EX mi
;
5959 mi
.cbSize
= sizeof (mi
);
5960 if (!get_monitor_info_fn (monitors
[i
], (struct MONITOR_INFO
*) &mi
))
5963 hdc
= CreateDCA ("DISPLAY", mi
.szDevice
, NULL
, NULL
);
5966 width_mm
= GetDeviceCaps (hdc
, HORZSIZE
);
5967 height_mm
= GetDeviceCaps (hdc
, VERTSIZE
);
5970 attributes
= Fcons (Fcons (Qframes
, AREF (monitor_frames
, i
)),
5973 name
= DECODE_SYSTEM (build_unibyte_string (mi
.szDevice
));
5975 attributes
= Fcons (Fcons (Qname
, name
), attributes
);
5977 attributes
= Fcons (Fcons (Qmm_size
, list2i (width_mm
, height_mm
)),
5980 workarea
= list4i (mi
.rcWork
.left
, mi
.rcWork
.top
,
5981 mi
.rcWork
.right
- mi
.rcWork
.left
,
5982 mi
.rcWork
.bottom
- mi
.rcWork
.top
);
5983 attributes
= Fcons (Fcons (Qworkarea
, workarea
), attributes
);
5985 geometry
= list4i (mi
.rcMonitor
.left
, mi
.rcMonitor
.top
,
5986 mi
.rcMonitor
.right
- mi
.rcMonitor
.left
,
5987 mi
.rcMonitor
.bottom
- mi
.rcMonitor
.top
);
5988 attributes
= Fcons (Fcons (Qgeometry
, geometry
), attributes
);
5990 if (mi
.dwFlags
& MONITORINFOF_PRIMARY
)
5991 primary_monitor_attributes
= attributes
;
5993 attributes_list
= Fcons (attributes
, attributes_list
);
5996 if (!NILP (primary_monitor_attributes
))
5997 attributes_list
= Fcons (primary_monitor_attributes
, attributes_list
);
6001 return attributes_list
;
6005 w32_display_monitor_attributes_list_fallback (struct w32_display_info
*dpyinfo
)
6007 Lisp_Object geometry
, workarea
, frames
, rest
, frame
, attributes
= Qnil
;
6009 double mm_per_pixel
;
6010 int pixel_width
, pixel_height
, width_mm
, height_mm
;
6013 /* Fallback: treat (possibly) multiple physical monitors as if they
6014 formed a single monitor as a whole. This should provide a
6015 consistent result at least on single monitor environments. */
6016 attributes
= Fcons (Fcons (Qname
, build_string ("combined screen")),
6020 FOR_EACH_FRAME (rest
, frame
)
6022 struct frame
*f
= XFRAME (frame
);
6024 if (FRAME_W32_P (f
) && !EQ (frame
, tip_frame
))
6025 frames
= Fcons (frame
, frames
);
6027 attributes
= Fcons (Fcons (Qframes
, frames
), attributes
);
6029 pixel_width
= x_display_pixel_width (dpyinfo
);
6030 pixel_height
= x_display_pixel_height (dpyinfo
);
6033 mm_per_pixel
= ((double) GetDeviceCaps (hdc
, HORZSIZE
)
6034 / GetDeviceCaps (hdc
, HORZRES
));
6035 width_mm
= pixel_width
* mm_per_pixel
+ 0.5;
6036 mm_per_pixel
= ((double) GetDeviceCaps (hdc
, VERTSIZE
)
6037 / GetDeviceCaps (hdc
, VERTRES
));
6038 height_mm
= pixel_height
* mm_per_pixel
+ 0.5;
6039 ReleaseDC (NULL
, hdc
);
6040 attributes
= Fcons (Fcons (Qmm_size
, list2i (width_mm
, height_mm
)),
6043 /* GetSystemMetrics below may return 0 for Windows 95 or NT 4.0, but
6045 geometry
= list4i (GetSystemMetrics (SM_XVIRTUALSCREEN
),
6046 GetSystemMetrics (SM_YVIRTUALSCREEN
),
6047 pixel_width
, pixel_height
);
6048 if (SystemParametersInfo (SPI_GETWORKAREA
, 0, &workarea_rect
, 0))
6049 workarea
= list4i (workarea_rect
.left
, workarea_rect
.top
,
6050 workarea_rect
.right
- workarea_rect
.left
,
6051 workarea_rect
.bottom
- workarea_rect
.top
);
6053 workarea
= geometry
;
6054 attributes
= Fcons (Fcons (Qworkarea
, workarea
), attributes
);
6056 attributes
= Fcons (Fcons (Qgeometry
, geometry
), attributes
);
6058 return list1 (attributes
);
6061 DEFUN ("w32-display-monitor-attributes-list", Fw32_display_monitor_attributes_list
,
6062 Sw32_display_monitor_attributes_list
,
6064 doc
: /* Return a list of physical monitor attributes on the W32 display DISPLAY.
6066 The optional argument DISPLAY specifies which display to ask about.
6067 DISPLAY should be either a frame or a display name (a string).
6068 If omitted or nil, that stands for the selected frame's display.
6070 Internal use only, use `display-monitor-attributes-list' instead. */)
6071 (Lisp_Object display
)
6073 struct w32_display_info
*dpyinfo
= check_x_display_info (display
);
6074 Lisp_Object attributes_list
;
6077 attributes_list
= w32_display_monitor_attributes_list ();
6078 if (NILP (attributes_list
))
6079 attributes_list
= w32_display_monitor_attributes_list_fallback (dpyinfo
);
6082 return attributes_list
;
6085 DEFUN ("set-message-beep", Fset_message_beep
, Sset_message_beep
, 1, 1, 0,
6086 doc
: /* Set the sound generated when the bell is rung.
6087 SOUND is `asterisk', `exclamation', `hand', `question', `ok', or `silent'
6088 to use the corresponding system sound for the bell. The `silent' sound
6089 prevents Emacs from making any sound at all.
6090 SOUND is nil to use the normal beep. */)
6093 CHECK_SYMBOL (sound
);
6096 sound_type
= 0xFFFFFFFF;
6097 else if (EQ (sound
, intern ("asterisk")))
6098 sound_type
= MB_ICONASTERISK
;
6099 else if (EQ (sound
, intern ("exclamation")))
6100 sound_type
= MB_ICONEXCLAMATION
;
6101 else if (EQ (sound
, intern ("hand")))
6102 sound_type
= MB_ICONHAND
;
6103 else if (EQ (sound
, intern ("question")))
6104 sound_type
= MB_ICONQUESTION
;
6105 else if (EQ (sound
, intern ("ok")))
6107 else if (EQ (sound
, intern ("silent")))
6108 sound_type
= MB_EMACS_SILENT
;
6110 sound_type
= 0xFFFFFFFF;
6116 x_screen_planes (register struct frame
*f
)
6118 return FRAME_DISPLAY_INFO (f
)->n_planes
;
6121 /* Return the display structure for the display named NAME.
6122 Open a new connection if necessary. */
6124 struct w32_display_info
*
6125 x_display_info_for_name (Lisp_Object name
)
6127 struct w32_display_info
*dpyinfo
;
6129 CHECK_STRING (name
);
6131 for (dpyinfo
= &one_w32_display_info
; dpyinfo
; dpyinfo
= dpyinfo
->next
)
6132 if (!NILP (Fstring_equal (XCAR (dpyinfo
->name_list_element
), name
)))
6135 /* Use this general default value to start with. */
6136 Vx_resource_name
= Vinvocation_name
;
6138 validate_x_resource_name ();
6140 dpyinfo
= w32_term_init (name
, NULL
, SSDATA (Vx_resource_name
));
6143 error ("Cannot connect to server %s", SDATA (name
));
6145 XSETFASTINT (Vwindow_system_version
, w32_major_version
);
6150 DEFUN ("x-open-connection", Fx_open_connection
, Sx_open_connection
,
6151 1, 3, 0, doc
: /* Open a connection to a display server.
6152 DISPLAY is the name of the display to connect to.
6153 Optional second arg XRM-STRING is a string of resources in xrdb format.
6154 If the optional third arg MUST-SUCCEED is non-nil,
6155 terminate Emacs if we can't open the connection.
6156 (In the Nextstep version, the last two arguments are currently ignored.) */)
6157 (Lisp_Object display
, Lisp_Object xrm_string
, Lisp_Object must_succeed
)
6160 struct w32_display_info
*dpyinfo
;
6162 CHECK_STRING (display
);
6164 /* Signal an error in order to encourage correct use from callers.
6165 * If we ever support multiple window systems in the same Emacs,
6166 * we'll need callers to be precise about what window system they
6169 if (strcmp (SSDATA (display
), "w32") != 0)
6170 error ("The name of the display in this Emacs must be \"w32\"");
6172 /* If initialization has already been done, return now to avoid
6173 overwriting critical parts of one_w32_display_info. */
6174 if (window_system_available (NULL
))
6177 if (! NILP (xrm_string
))
6178 CHECK_STRING (xrm_string
);
6180 /* Allow color mapping to be defined externally; first look in user's
6181 HOME directory, then in Emacs etc dir for a file called rgb.txt. */
6183 Lisp_Object color_file
;
6185 color_file
= build_string ("~/rgb.txt");
6187 if (NILP (Ffile_readable_p (color_file
)))
6189 Fexpand_file_name (build_string ("rgb.txt"),
6190 Fsymbol_value (intern ("data-directory")));
6192 Vw32_color_map
= Fx_load_color_file (color_file
);
6194 if (NILP (Vw32_color_map
))
6195 Vw32_color_map
= w32_default_color_map ();
6197 /* Merge in system logical colors. */
6198 add_system_logical_colors_to_map (&Vw32_color_map
);
6200 if (! NILP (xrm_string
))
6201 xrm_option
= SSDATA (xrm_string
);
6205 /* Use this general default value to start with. */
6206 /* First remove .exe suffix from invocation-name - it looks ugly. */
6208 char basename
[ MAX_PATH
], *str
;
6210 lispstpcpy (basename
, Vinvocation_name
);
6211 str
= strrchr (basename
, '.');
6213 Vinvocation_name
= build_string (basename
);
6215 Vx_resource_name
= Vinvocation_name
;
6217 validate_x_resource_name ();
6219 /* This is what opens the connection and sets x_current_display.
6220 This also initializes many symbols, such as those used for input. */
6221 dpyinfo
= w32_term_init (display
, xrm_option
, SSDATA (Vx_resource_name
));
6225 if (!NILP (must_succeed
))
6226 fatal ("Cannot connect to server %s.\n",
6229 error ("Cannot connect to server %s", SDATA (display
));
6232 XSETFASTINT (Vwindow_system_version
, w32_major_version
);
6236 DEFUN ("x-close-connection", Fx_close_connection
,
6237 Sx_close_connection
, 1, 1, 0,
6238 doc
: /* Close the connection to DISPLAY's server.
6239 For DISPLAY, specify either a frame or a display name (a string).
6240 If DISPLAY is nil, that stands for the selected frame's display. */)
6241 (Lisp_Object display
)
6243 struct w32_display_info
*dpyinfo
= check_x_display_info (display
);
6245 if (dpyinfo
->reference_count
> 0)
6246 error ("Display still has frames on it");
6249 x_destroy_all_bitmaps (dpyinfo
);
6251 x_delete_display (dpyinfo
);
6257 DEFUN ("x-display-list", Fx_display_list
, Sx_display_list
, 0, 0, 0,
6258 doc
: /* Return the list of display names that Emacs has connections to. */)
6261 Lisp_Object result
= Qnil
;
6262 struct w32_display_info
*wdi
;
6264 for (wdi
= x_display_list
; wdi
; wdi
= wdi
->next
)
6265 result
= Fcons (XCAR (wdi
->name_list_element
), result
);
6270 DEFUN ("x-synchronize", Fx_synchronize
, Sx_synchronize
, 1, 2, 0,
6271 doc
: /* If ON is non-nil, report X errors as soon as the erring request is made.
6272 This function only has an effect on X Windows. With MS Windows, it is
6273 defined but does nothing.
6275 If ON is nil, allow buffering of requests.
6276 Turning on synchronization prohibits the Xlib routines from buffering
6277 requests and seriously degrades performance, but makes debugging much
6279 The optional second argument TERMINAL specifies which display to act on.
6280 TERMINAL should be a terminal object, a frame or a display name (a string).
6281 If TERMINAL is omitted or nil, that stands for the selected frame's display. */)
6282 (Lisp_Object on
, Lisp_Object display
)
6289 /***********************************************************************
6291 ***********************************************************************/
6293 #if 0 /* TODO : port window properties to W32 */
6295 DEFUN ("x-change-window-property", Fx_change_window_property
,
6296 Sx_change_window_property
, 2, 6, 0,
6297 doc
: /* Change window property PROP to VALUE on the X window of FRAME.
6298 PROP must be a string. VALUE may be a string or a list of conses,
6299 numbers and/or strings. If an element in the list is a string, it is
6300 converted to an atom and the value of the Atom is used. If an element
6301 is a cons, it is converted to a 32 bit number where the car is the 16
6302 top bits and the cdr is the lower 16 bits.
6304 FRAME nil or omitted means use the selected frame.
6305 If TYPE is given and non-nil, it is the name of the type of VALUE.
6306 If TYPE is not given or nil, the type is STRING.
6307 FORMAT gives the size in bits of each element if VALUE is a list.
6308 It must be one of 8, 16 or 32.
6309 If VALUE is a string or FORMAT is nil or not given, FORMAT defaults to 8.
6310 If OUTER-P is non-nil, the property is changed for the outer X window of
6311 FRAME. Default is to change on the edit X window. */)
6312 (Lisp_Object prop
, Lisp_Object value
, Lisp_Object frame
,
6313 Lisp_Object type
, Lisp_Object format
, Lisp_Object outer_p
)
6315 struct frame
*f
= decode_window_system_frame (frame
);
6318 CHECK_STRING (prop
);
6319 CHECK_STRING (value
);
6322 prop_atom
= XInternAtom (FRAME_W32_DISPLAY (f
), SDATA (prop
), False
);
6323 XChangeProperty (FRAME_W32_DISPLAY (f
), FRAME_W32_WINDOW (f
),
6324 prop_atom
, XA_STRING
, 8, PropModeReplace
,
6325 SDATA (value
), SCHARS (value
));
6327 /* Make sure the property is set when we return. */
6328 XFlush (FRAME_W32_DISPLAY (f
));
6335 DEFUN ("x-delete-window-property", Fx_delete_window_property
,
6336 Sx_delete_window_property
, 1, 2, 0,
6337 doc
: /* Remove window property PROP from X window of FRAME.
6338 FRAME nil or omitted means use the selected frame. Value is PROP. */)
6339 (Lisp_Object prop
, Lisp_Object frame
)
6341 struct frame
*f
= decode_window_system_frame (frame
);
6344 CHECK_STRING (prop
);
6346 prop_atom
= XInternAtom (FRAME_W32_DISPLAY (f
), SDATA (prop
), False
);
6347 XDeleteProperty (FRAME_W32_DISPLAY (f
), FRAME_W32_WINDOW (f
), prop_atom
);
6349 /* Make sure the property is removed when we return. */
6350 XFlush (FRAME_W32_DISPLAY (f
));
6357 DEFUN ("x-window-property", Fx_window_property
, Sx_window_property
,
6359 doc
: /* Value is the value of window property PROP on FRAME.
6360 If FRAME is nil or omitted, use the selected frame.
6362 On X Windows, the following optional arguments are also accepted:
6363 If TYPE is nil or omitted, get the property as a string.
6364 Otherwise TYPE is the name of the atom that denotes the type expected.
6365 If SOURCE is non-nil, get the property on that window instead of from
6366 FRAME. The number 0 denotes the root window.
6367 If DELETE-P is non-nil, delete the property after retrieving it.
6368 If VECTOR-RET-P is non-nil, don't return a string but a vector of values.
6370 On MS Windows, this function accepts but ignores those optional arguments.
6372 Value is nil if FRAME hasn't a property with name PROP or if PROP has
6373 no value of TYPE (always string in the MS Windows case). */)
6374 (Lisp_Object prop
, Lisp_Object frame
, Lisp_Object type
,
6375 Lisp_Object source
, Lisp_Object delete_p
, Lisp_Object vector_ret_p
)
6377 struct frame
*f
= decode_window_system_frame (frame
);
6380 Lisp_Object prop_value
= Qnil
;
6381 char *tmp_data
= NULL
;
6384 unsigned long actual_size
, bytes_remaining
;
6386 CHECK_STRING (prop
);
6388 prop_atom
= XInternAtom (FRAME_W32_DISPLAY (f
), SDATA (prop
), False
);
6389 rc
= XGetWindowProperty (FRAME_W32_DISPLAY (f
), FRAME_W32_WINDOW (f
),
6390 prop_atom
, 0, 0, False
, XA_STRING
,
6391 &actual_type
, &actual_format
, &actual_size
,
6392 &bytes_remaining
, (unsigned char **) &tmp_data
);
6395 int size
= bytes_remaining
;
6400 rc
= XGetWindowProperty (FRAME_W32_DISPLAY (f
), FRAME_W32_WINDOW (f
),
6401 prop_atom
, 0, bytes_remaining
,
6403 &actual_type
, &actual_format
,
6404 &actual_size
, &bytes_remaining
,
6405 (unsigned char **) &tmp_data
);
6407 prop_value
= make_string (tmp_data
, size
);
6421 /***********************************************************************
6423 ***********************************************************************/
6425 static void compute_tip_xy (struct frame
*, Lisp_Object
, Lisp_Object
,
6426 Lisp_Object
, int, int, int *, int *);
6428 /* The frame of a currently visible tooltip. */
6430 Lisp_Object tip_frame
;
6432 /* If non-nil, a timer started that hides the last tooltip when it
6435 Lisp_Object tip_timer
;
6438 /* If non-nil, a vector of 3 elements containing the last args
6439 with which x-show-tip was called. See there. */
6441 Lisp_Object last_show_tip_args
;
6445 unwind_create_tip_frame (Lisp_Object frame
)
6447 Lisp_Object deleted
;
6449 deleted
= unwind_create_frame (frame
);
6450 if (EQ (deleted
, Qt
))
6458 /* Create a frame for a tooltip on the display described by DPYINFO.
6459 PARMS is a list of frame parameters. Value is the frame.
6461 Note that functions called here, esp. x_default_parameter can
6462 signal errors, for instance when a specified color name is
6463 undefined. We have to make sure that we're in a consistent state
6464 when this happens. */
6467 x_create_tip_frame (struct w32_display_info
*dpyinfo
, Lisp_Object parms
)
6473 ptrdiff_t count
= SPECPDL_INDEX ();
6475 bool face_change_before
= face_change
;
6476 int x_width
= 0, x_height
= 0;
6478 /* Use this general default value to start with until we know if
6479 this frame has a specified name. */
6480 Vx_resource_name
= Vinvocation_name
;
6482 kb
= dpyinfo
->terminal
->kboard
;
6484 /* The calls to x_get_arg remove elements from PARMS, so copy it to
6485 avoid destructive changes behind our caller's back. */
6486 parms
= Fcopy_alist (parms
);
6488 /* Get the name of the frame to use for resource lookup. */
6489 name
= x_get_arg (dpyinfo
, parms
, Qname
, "name", "Name", RES_TYPE_STRING
);
6491 && !EQ (name
, Qunbound
)
6493 error ("Invalid frame name--not a string or nil");
6494 Vx_resource_name
= name
;
6497 /* Make a frame without minibuffer nor mode-line. */
6498 f
= make_frame (false);
6499 f
->wants_modeline
= false;
6500 XSETFRAME (frame
, f
);
6502 record_unwind_protect (unwind_create_tip_frame
, frame
);
6504 /* By setting the output method, we're essentially saying that
6505 the frame is live, as per FRAME_LIVE_P. If we get a signal
6506 from this point on, x_destroy_window might screw up reference
6508 f
->terminal
= dpyinfo
->terminal
;
6509 f
->output_method
= output_w32
;
6510 f
->output_data
.w32
= xzalloc (sizeof (struct w32_output
));
6512 FRAME_FONTSET (f
) = -1;
6513 fset_icon_name (f
, Qnil
);
6516 image_cache_refcount
=
6517 FRAME_IMAGE_CACHE (f
) ? FRAME_IMAGE_CACHE (f
)->refcount
: 0;
6518 dpyinfo_refcount
= dpyinfo
->reference_count
;
6519 #endif /* GLYPH_DEBUG */
6520 FRAME_KBOARD (f
) = kb
;
6521 f
->output_data
.w32
->parent_desc
= FRAME_DISPLAY_INFO (f
)->root_window
;
6522 f
->output_data
.w32
->explicit_parent
= false;
6524 /* Set the name; the functions to which we pass f expect the name to
6526 if (EQ (name
, Qunbound
) || NILP (name
))
6528 fset_name (f
, build_string (dpyinfo
->w32_id_name
));
6529 f
->explicit_name
= false;
6533 fset_name (f
, name
);
6534 f
->explicit_name
= true;
6535 /* Use the frame's title when getting resources for this frame. */
6536 specbind (Qx_resource_name
, name
);
6539 if (uniscribe_available
)
6540 register_font_driver (&uniscribe_font_driver
, f
);
6541 register_font_driver (&w32font_driver
, f
);
6543 x_default_parameter (f
, parms
, Qfont_backend
, Qnil
,
6544 "fontBackend", "FontBackend", RES_TYPE_STRING
);
6546 /* Extract the window parameters from the supplied values
6547 that are needed to determine window geometry. */
6548 x_default_font_parameter (f
, parms
);
6550 x_default_parameter (f
, parms
, Qborder_width
, make_number (2),
6551 "borderWidth", "BorderWidth", RES_TYPE_NUMBER
);
6552 /* This defaults to 2 in order to match xterm. We recognize either
6553 internalBorderWidth or internalBorder (which is what xterm calls
6555 if (NILP (Fassq (Qinternal_border_width
, parms
)))
6559 value
= x_get_arg (dpyinfo
, parms
, Qinternal_border_width
,
6560 "internalBorder", "internalBorder", RES_TYPE_NUMBER
);
6561 if (! EQ (value
, Qunbound
))
6562 parms
= Fcons (Fcons (Qinternal_border_width
, value
),
6566 x_default_parameter (f
, parms
, Qinternal_border_width
, make_number (1),
6567 "internalBorderWidth", "internalBorderWidth",
6569 /* Also do the stuff which must be set before the window exists. */
6570 x_default_parameter (f
, parms
, Qforeground_color
, build_string ("black"),
6571 "foreground", "Foreground", RES_TYPE_STRING
);
6572 x_default_parameter (f
, parms
, Qbackground_color
, build_string ("white"),
6573 "background", "Background", RES_TYPE_STRING
);
6574 x_default_parameter (f
, parms
, Qmouse_color
, build_string ("black"),
6575 "pointerColor", "Foreground", RES_TYPE_STRING
);
6576 x_default_parameter (f
, parms
, Qcursor_color
, build_string ("black"),
6577 "cursorColor", "Foreground", RES_TYPE_STRING
);
6578 x_default_parameter (f
, parms
, Qborder_color
, build_string ("black"),
6579 "borderColor", "BorderColor", RES_TYPE_STRING
);
6581 /* Init faces before x_default_parameter is called for the
6582 scroll-bar-width parameter because otherwise we end up in
6583 init_iterator with a null face cache, which should not happen. */
6584 init_frame_faces (f
);
6586 f
->output_data
.w32
->dwStyle
= WS_BORDER
| WS_POPUP
| WS_DISABLED
;
6587 f
->output_data
.w32
->parent_desc
= FRAME_DISPLAY_INFO (f
)->root_window
;
6589 x_figure_window_size (f
, parms
, true, &x_width
, &x_height
);
6591 /* No fringes on tip frame. */
6593 f
->left_fringe_width
= 0;
6594 f
->right_fringe_width
= 0;
6595 /* No dividers on tip frame. */
6596 f
->right_divider_width
= 0;
6597 f
->bottom_divider_width
= 0;
6600 my_create_tip_window (f
);
6605 x_default_parameter (f
, parms
, Qauto_raise
, Qnil
,
6606 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN
);
6607 x_default_parameter (f
, parms
, Qauto_lower
, Qnil
,
6608 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN
);
6609 x_default_parameter (f
, parms
, Qcursor_type
, Qbox
,
6610 "cursorType", "CursorType", RES_TYPE_SYMBOL
);
6611 /* Process alpha here (Bug#17344). */
6612 x_default_parameter (f
, parms
, Qalpha
, Qnil
,
6613 "alpha", "Alpha", RES_TYPE_NUMBER
);
6615 /* Dimensions, especially FRAME_LINES (f), must be done via
6616 change_frame_size. Change will not be effected unless different
6617 from the current FRAME_LINES (f). */
6618 width
= FRAME_COLS (f
);
6619 height
= FRAME_LINES (f
);
6620 SET_FRAME_COLS (f
, 0);
6621 SET_FRAME_LINES (f
, 0);
6622 adjust_frame_size (f
, width
* FRAME_COLUMN_WIDTH (f
),
6623 height
* FRAME_LINE_HEIGHT (f
), 0, true, Qtip_frame
);
6624 /* Add `tooltip' frame parameter's default value. */
6625 if (NILP (Fframe_parameter (frame
, Qtooltip
)))
6626 Fmodify_frame_parameters (frame
, Fcons (Fcons (Qtooltip
, Qt
), Qnil
));
6628 /* Set up faces after all frame parameters are known. This call
6629 also merges in face attributes specified for new frames.
6631 Frame parameters may be changed if .Xdefaults contains
6632 specifications for the default font. For example, if there is an
6633 `Emacs.default.attributeBackground: pink', the `background-color'
6634 attribute of the frame get's set, which let's the internal border
6635 of the tooltip frame appear in pink. Prevent this. */
6637 Lisp_Object bg
= Fframe_parameter (frame
, Qbackground_color
);
6638 Lisp_Object fg
= Fframe_parameter (frame
, Qforeground_color
);
6639 Lisp_Object colors
= Qnil
;
6641 call2 (Qface_set_after_frame_default
, frame
, Qnil
);
6643 if (!EQ (bg
, Fframe_parameter (frame
, Qbackground_color
)))
6644 colors
= Fcons (Fcons (Qbackground_color
, bg
), colors
);
6645 if (!EQ (fg
, Fframe_parameter (frame
, Qforeground_color
)))
6646 colors
= Fcons (Fcons (Qforeground_color
, fg
), colors
);
6649 Fmodify_frame_parameters (frame
, colors
);
6654 /* Now that the frame is official, it counts as a reference to
6656 FRAME_DISPLAY_INFO (f
)->reference_count
++;
6657 f
->terminal
->reference_count
++;
6659 /* It is now ok to make the frame official even if we get an error
6660 below. And the frame needs to be on Vframe_list or making it
6661 visible won't work. */
6662 Vframe_list
= Fcons (frame
, Vframe_list
);
6663 f
->can_x_set_window_size
= true;
6665 /* Setting attributes of faces of the tooltip frame from resources
6666 and similar will set face_change, which leads to the
6667 clearing of all current matrices. Since this isn't necessary
6668 here, avoid it by resetting face_change to the value it
6669 had before we created the tip frame. */
6670 face_change
= face_change_before
;
6672 /* Discard the unwind_protect. */
6673 return unbind_to (count
, frame
);
6677 /* Compute where to display tip frame F. PARMS is the list of frame
6678 parameters for F. DX and DY are specified offsets from the current
6679 location of the mouse. WIDTH and HEIGHT are the width and height
6680 of the tooltip. Return coordinates relative to the root window of
6681 the display in *ROOT_X and *ROOT_Y. */
6684 compute_tip_xy (struct frame
*f
,
6685 Lisp_Object parms
, Lisp_Object dx
, Lisp_Object dy
,
6686 int width
, int height
, int *root_x
, int *root_y
)
6688 Lisp_Object left
, top
, right
, bottom
;
6689 int min_x
, min_y
, max_x
, max_y
;
6691 /* User-specified position? */
6692 left
= Fcdr (Fassq (Qleft
, parms
));
6693 top
= Fcdr (Fassq (Qtop
, parms
));
6694 right
= Fcdr (Fassq (Qright
, parms
));
6695 bottom
= Fcdr (Fassq (Qbottom
, parms
));
6697 /* Move the tooltip window where the mouse pointer is. Resize and
6699 if ((!INTEGERP (left
) && !INTEGERP (right
))
6700 || (!INTEGERP (top
) && !INTEGERP (bottom
)))
6704 /* Default min and max values. */
6707 max_x
= x_display_pixel_width (FRAME_DISPLAY_INFO (f
));
6708 max_y
= x_display_pixel_height (FRAME_DISPLAY_INFO (f
));
6716 /* If multiple monitor support is available, constrain the tip onto
6717 the current monitor. This improves the above by allowing negative
6718 co-ordinates if monitor positions are such that they are valid, and
6719 snaps a tooltip onto a single monitor if we are close to the edge
6720 where it would otherwise flow onto the other monitor (or into
6721 nothingness if there is a gap in the overlap). */
6722 if (monitor_from_point_fn
&& get_monitor_info_fn
)
6724 struct MONITOR_INFO info
;
6726 = monitor_from_point_fn (pt
, MONITOR_DEFAULT_TO_NEAREST
);
6727 info
.cbSize
= sizeof (info
);
6729 if (get_monitor_info_fn (monitor
, &info
))
6731 min_x
= info
.rcWork
.left
;
6732 min_y
= info
.rcWork
.top
;
6733 max_x
= info
.rcWork
.right
;
6734 max_y
= info
.rcWork
.bottom
;
6740 *root_y
= XINT (top
);
6741 else if (INTEGERP (bottom
))
6742 *root_y
= XINT (bottom
) - height
;
6743 else if (*root_y
+ XINT (dy
) <= min_y
)
6744 *root_y
= min_y
; /* Can happen for negative dy */
6745 else if (*root_y
+ XINT (dy
) + height
<= max_y
)
6746 /* It fits below the pointer */
6747 *root_y
+= XINT (dy
);
6748 else if (height
+ XINT (dy
) + min_y
<= *root_y
)
6749 /* It fits above the pointer. */
6750 *root_y
-= height
+ XINT (dy
);
6752 /* Put it on the top. */
6755 if (INTEGERP (left
))
6756 *root_x
= XINT (left
);
6757 else if (INTEGERP (right
))
6758 *root_x
= XINT (right
) - width
;
6759 else if (*root_x
+ XINT (dx
) <= min_x
)
6760 *root_x
= 0; /* Can happen for negative dx */
6761 else if (*root_x
+ XINT (dx
) + width
<= max_x
)
6762 /* It fits to the right of the pointer. */
6763 *root_x
+= XINT (dx
);
6764 else if (width
+ XINT (dx
) + min_x
<= *root_x
)
6765 /* It fits to the left of the pointer. */
6766 *root_x
-= width
+ XINT (dx
);
6768 /* Put it left justified on the screen -- it ought to fit that way. */
6772 /* Hide tooltip. Delete its frame if DELETE is true. */
6774 x_hide_tip (bool delete)
6776 if (!NILP (tip_timer
))
6778 call1 (Qcancel_timer
, tip_timer
);
6782 if (NILP (tip_frame
)
6783 || (!delete && FRAMEP (tip_frame
)
6784 && !FRAME_VISIBLE_P (XFRAME (tip_frame
))))
6789 Lisp_Object was_open
= Qnil
;
6791 count
= SPECPDL_INDEX ();
6792 specbind (Qinhibit_redisplay
, Qt
);
6793 specbind (Qinhibit_quit
, Qt
);
6795 if (FRAMEP (tip_frame
))
6799 delete_frame (tip_frame
, Qnil
);
6803 x_make_frame_invisible (XFRAME (tip_frame
));
6810 return unbind_to (count
, was_open
);
6815 DEFUN ("x-show-tip", Fx_show_tip
, Sx_show_tip
, 1, 6, 0,
6816 doc
: /* Show STRING in a \"tooltip\" window on frame FRAME.
6817 A tooltip window is a small window displaying a string.
6819 This is an internal function; Lisp code should call `tooltip-show'.
6821 FRAME nil or omitted means use the selected frame.
6823 PARMS is an optional list of frame parameters which can be
6824 used to change the tooltip's appearance.
6826 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
6827 means use the default timeout of 5 seconds.
6829 If the list of frame parameters PARMS contains a `left' parameter,
6830 display the tooltip at that x-position. If the list of frame parameters
6831 PARMS contains no `left' but a `right' parameter, display the tooltip
6832 right-adjusted at that x-position. Otherwise display it at the
6833 x-position of the mouse, with offset DX added (default is 5 if DX isn't
6836 Likewise for the y-position: If a `top' frame parameter is specified, it
6837 determines the position of the upper edge of the tooltip window. If a
6838 `bottom' parameter but no `top' frame parameter is specified, it
6839 determines the position of the lower edge of the tooltip window.
6840 Otherwise display the tooltip window at the y-position of the mouse,
6841 with offset DY added (default is -10).
6843 A tooltip's maximum size is specified by `x-max-tooltip-size'.
6844 Text larger than the specified size is clipped. */)
6845 (Lisp_Object string
, Lisp_Object frame
, Lisp_Object parms
, Lisp_Object timeout
, Lisp_Object dx
, Lisp_Object dy
)
6847 struct frame
*f
, *tip_f
;
6850 struct buffer
*old_buffer
;
6851 struct text_pos pos
;
6852 int i
, width
, height
;
6853 int old_windows_or_buffers_changed
= windows_or_buffers_changed
;
6854 ptrdiff_t count
= SPECPDL_INDEX ();
6856 Lisp_Object window
, size
;
6858 specbind (Qinhibit_redisplay
, Qt
);
6860 CHECK_STRING (string
);
6861 f
= decode_window_system_frame (frame
);
6863 timeout
= make_number (5);
6865 CHECK_NATNUM (timeout
);
6868 dx
= make_number (5);
6873 dy
= make_number (-10);
6877 if (NILP (last_show_tip_args
))
6878 last_show_tip_args
= Fmake_vector (make_number (3), Qnil
);
6880 if (FRAMEP (tip_frame
) && FRAME_LIVE_P (XFRAME (tip_frame
)))
6882 Lisp_Object last_string
= AREF (last_show_tip_args
, 0);
6883 Lisp_Object last_frame
= AREF (last_show_tip_args
, 1);
6884 Lisp_Object last_parms
= AREF (last_show_tip_args
, 2);
6886 if (FRAME_VISIBLE_P (XFRAME (tip_frame
))
6887 && EQ (frame
, last_frame
)
6888 && !NILP (Fequal_including_properties (last_string
, string
))
6889 && !NILP (Fequal (last_parms
, parms
)))
6891 /* Only DX and DY have changed. */
6892 tip_f
= XFRAME (tip_frame
);
6893 if (!NILP (tip_timer
))
6895 Lisp_Object timer
= tip_timer
;
6898 call1 (Qcancel_timer
, timer
);
6902 compute_tip_xy (tip_f
, parms
, dx
, dy
, FRAME_PIXEL_WIDTH (tip_f
),
6903 FRAME_PIXEL_HEIGHT (tip_f
), &root_x
, &root_y
);
6905 /* Put tooltip in topmost group and in position. */
6906 SetWindowPos (FRAME_W32_WINDOW (tip_f
), HWND_TOPMOST
,
6907 root_x
, root_y
, 0, 0,
6908 SWP_NOSIZE
| SWP_NOACTIVATE
| SWP_NOOWNERZORDER
);
6910 /* Ensure tooltip is on top of other topmost windows (eg menus). */
6911 SetWindowPos (FRAME_W32_WINDOW (tip_f
), HWND_TOP
,
6913 SWP_NOMOVE
| SWP_NOSIZE
6914 | SWP_NOACTIVATE
| SWP_NOOWNERZORDER
);
6916 /* Let redisplay know that we have made the frame visible already. */
6917 SET_FRAME_VISIBLE (tip_f
, 1);
6918 ShowWindow (FRAME_W32_WINDOW (tip_f
), SW_SHOWNOACTIVATE
);
6923 else if (tooltip_reuse_hidden_frame
&& EQ (frame
, last_frame
))
6925 bool delete = false;
6926 Lisp_Object tail
, elt
, parm
, last
;
6928 /* Check if every parameter in PARMS has the same value in
6929 last_parms. This may destruct last_parms which, however,
6930 will be recreated below. */
6931 for (tail
= parms
; CONSP (tail
); tail
= XCDR (tail
))
6935 /* The left, top, right and bottom parameters are handled
6936 by compute_tip_xy so they can be ignored here. */
6937 if (!EQ (parm
, Qleft
) && !EQ (parm
, Qtop
)
6938 && !EQ (parm
, Qright
) && !EQ (parm
, Qbottom
))
6940 last
= Fassq (parm
, last_parms
);
6941 if (NILP (Fequal (Fcdr (elt
), Fcdr (last
))))
6943 /* We lost, delete the old tooltip. */
6948 last_parms
= call2 (Qassq_delete_all
, parm
, last_parms
);
6951 last_parms
= call2 (Qassq_delete_all
, parm
, last_parms
);
6954 /* Now check if there's a parameter left in last_parms with a
6956 for (tail
= last_parms
; CONSP (tail
); tail
= XCDR (tail
))
6960 if (!EQ (parm
, Qleft
) && !EQ (parm
, Qtop
) && !EQ (parm
, Qright
)
6961 && !EQ (parm
, Qbottom
) && !NILP (Fcdr (elt
)))
6963 /* We lost, delete the old tooltip. */
6969 x_hide_tip (delete);
6977 ASET (last_show_tip_args
, 0, string
);
6978 ASET (last_show_tip_args
, 1, frame
);
6979 ASET (last_show_tip_args
, 2, parms
);
6981 /* Block input until the tip has been fully drawn, to avoid crashes
6982 when drawing tips in menus. */
6985 if (!FRAMEP (tip_frame
) || !FRAME_LIVE_P (XFRAME (tip_frame
)))
6987 /* Add default values to frame parameters. */
6988 if (NILP (Fassq (Qname
, parms
)))
6989 parms
= Fcons (Fcons (Qname
, build_string ("tooltip")), parms
);
6990 if (NILP (Fassq (Qinternal_border_width
, parms
)))
6991 parms
= Fcons (Fcons (Qinternal_border_width
, make_number (3)), parms
);
6992 if (NILP (Fassq (Qborder_width
, parms
)))
6993 parms
= Fcons (Fcons (Qborder_width
, make_number (1)), parms
);
6994 if (NILP (Fassq (Qborder_color
, parms
)))
6995 parms
= Fcons (Fcons (Qborder_color
, build_string ("lightyellow")), parms
);
6996 if (NILP (Fassq (Qbackground_color
, parms
)))
6997 parms
= Fcons (Fcons (Qbackground_color
, build_string ("lightyellow")),
7000 /* Create a frame for the tooltip, and record it in the global
7001 variable tip_frame. */
7002 if (NILP (tip_frame
= x_create_tip_frame (FRAME_DISPLAY_INFO (f
), parms
)))
7004 /* Creating the tip frame failed. */
7006 return unbind_to (count
, Qnil
);
7010 tip_f
= XFRAME (tip_frame
);
7011 window
= FRAME_ROOT_WINDOW (tip_f
);
7012 AUTO_STRING (tip
, " *tip*");
7013 set_window_buffer (window
, Fget_buffer_create (tip
), false, false);
7014 w
= XWINDOW (window
);
7015 w
->pseudo_window_p
= true;
7017 /* Set up the frame's root window. Note: The following code does not
7018 try to size the window or its frame correctly. Its only purpose is
7019 to make the subsequent text size calculations work. The right
7020 sizes should get installed when the toolkit gets back to us. */
7026 if (CONSP (Vx_max_tooltip_size
)
7027 && RANGED_INTEGERP (1, XCAR (Vx_max_tooltip_size
), INT_MAX
)
7028 && RANGED_INTEGERP (1, XCDR (Vx_max_tooltip_size
), INT_MAX
))
7030 w
->total_cols
= XFASTINT (XCAR (Vx_max_tooltip_size
));
7031 w
->total_lines
= XFASTINT (XCDR (Vx_max_tooltip_size
));
7036 w
->total_lines
= 40;
7039 w
->pixel_width
= w
->total_cols
* FRAME_COLUMN_WIDTH (tip_f
);
7040 w
->pixel_height
= w
->total_lines
* FRAME_LINE_HEIGHT (tip_f
);
7041 FRAME_TOTAL_COLS (tip_f
) = WINDOW_TOTAL_COLS (w
);
7042 adjust_frame_glyphs (tip_f
);
7044 /* Insert STRING into the root window's buffer and fit the frame to
7046 count_1
= SPECPDL_INDEX ();
7047 old_buffer
= current_buffer
;
7048 set_buffer_internal_1 (XBUFFER (w
->contents
));
7049 bset_truncate_lines (current_buffer
, Qnil
);
7050 specbind (Qinhibit_read_only
, Qt
);
7051 specbind (Qinhibit_modification_hooks
, Qt
);
7052 specbind (Qinhibit_point_motion_hooks
, Qt
);
7054 Finsert (1, &string
);
7055 clear_glyph_matrix (w
->desired_matrix
);
7056 clear_glyph_matrix (w
->current_matrix
);
7057 SET_TEXT_POS (pos
, BEGV
, BEGV_BYTE
);
7058 try_window (window
, pos
, TRY_WINDOW_IGNORE_FONTS_CHANGE
);
7059 /* Calculate size of tooltip window. */
7060 size
= Fwindow_text_pixel_size (window
, Qnil
, Qnil
, Qnil
,
7061 make_number (w
->pixel_height
), Qnil
);
7062 /* Add the frame's internal border to calculated size. */
7063 width
= XINT (Fcar (size
)) + 2 * FRAME_INTERNAL_BORDER_WIDTH (tip_f
);
7064 height
= XINT (Fcdr (size
)) + 2 * FRAME_INTERNAL_BORDER_WIDTH (tip_f
);
7065 /* Calculate position of tooltip frame. */
7066 compute_tip_xy (tip_f
, parms
, dx
, dy
, width
, height
, &root_x
, &root_y
);
7068 /* Show tooltip frame. */
7071 int pad
= (NUMBERP (Vw32_tooltip_extra_pixels
)
7072 ? max (0, XINT (Vw32_tooltip_extra_pixels
))
7073 : FRAME_COLUMN_WIDTH (tip_f
));
7075 rect
.left
= rect
.top
= 0;
7077 rect
.bottom
= height
;
7078 AdjustWindowRect (&rect
, tip_f
->output_data
.w32
->dwStyle
,
7079 FRAME_EXTERNAL_MENU_BAR (tip_f
));
7081 /* Position and size tooltip and put it in the topmost group. */
7082 SetWindowPos (FRAME_W32_WINDOW (tip_f
), HWND_TOPMOST
,
7084 rect
.right
- rect
.left
+ pad
,
7085 rect
.bottom
- rect
.top
, SWP_NOACTIVATE
| SWP_NOOWNERZORDER
);
7087 /* Ensure tooltip is on top of other topmost windows (eg menus). */
7088 SetWindowPos (FRAME_W32_WINDOW (tip_f
), HWND_TOP
,
7090 SWP_NOMOVE
| SWP_NOSIZE
7091 | SWP_NOACTIVATE
| SWP_NOOWNERZORDER
);
7093 /* Let redisplay know that we have made the frame visible already. */
7094 SET_FRAME_VISIBLE (tip_f
, 1);
7096 ShowWindow (FRAME_W32_WINDOW (tip_f
), SW_SHOWNOACTIVATE
);
7099 w
->must_be_updated_p
= true;
7100 update_single_window (w
);
7101 set_buffer_internal_1 (old_buffer
);
7102 unbind_to (count_1
, Qnil
);
7104 windows_or_buffers_changed
= old_windows_or_buffers_changed
;
7107 /* Let the tip disappear after timeout seconds. */
7108 tip_timer
= call3 (intern ("run-at-time"), timeout
, Qnil
,
7109 intern ("x-hide-tip"));
7111 return unbind_to (count
, Qnil
);
7115 DEFUN ("x-hide-tip", Fx_hide_tip
, Sx_hide_tip
, 0, 0, 0,
7116 doc
: /* Hide the current tooltip window, if there is any.
7117 Value is t if tooltip was open, nil otherwise. */)
7120 return x_hide_tip (!tooltip_reuse_hidden_frame
);
7123 /***********************************************************************
7124 File selection dialog
7125 ***********************************************************************/
7127 #define FILE_NAME_TEXT_FIELD edt1
7128 #define FILE_NAME_COMBO_BOX cmb13
7129 #define FILE_NAME_LIST lst1
7131 /* Callback for altering the behavior of the Open File dialog.
7132 Makes the Filename text field contain "Current Directory" and be
7133 read-only when "Directories" is selected in the filter. This
7134 allows us to work around the fact that the standard Open File
7135 dialog does not support directories. */
7136 static UINT_PTR CALLBACK
7137 file_dialog_callback (HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
7139 if (msg
== WM_NOTIFY
)
7141 OFNOTIFYW
* notify_w
= (OFNOTIFYW
*)lParam
;
7142 OFNOTIFYA
* notify_a
= (OFNOTIFYA
*)lParam
;
7143 int dropdown_changed
;
7145 #ifdef NTGUI_UNICODE
7146 const int use_unicode
= 1;
7147 #else /* !NTGUI_UNICODE */
7148 int use_unicode
= w32_unicode_filenames
;
7149 #endif /* NTGUI_UNICODE */
7151 /* Detect when the Filter dropdown is changed. */
7154 notify_w
->hdr
.code
== CDN_TYPECHANGE
7155 || notify_w
->hdr
.code
== CDN_INITDONE
;
7158 notify_a
->hdr
.code
== CDN_TYPECHANGE
7159 || notify_a
->hdr
.code
== CDN_INITDONE
;
7160 if (dropdown_changed
)
7162 HWND dialog
= GetParent (hwnd
);
7163 HWND edit_control
= GetDlgItem (dialog
, FILE_NAME_TEXT_FIELD
);
7164 HWND list
= GetDlgItem (dialog
, FILE_NAME_LIST
);
7167 /* At least on Windows 7, the above attempt to get the window handle
7168 to the File Name Text Field fails. The following code does the
7169 job though. Note that this code is based on my examination of the
7170 window hierarchy using Microsoft Spy++. bk */
7171 if (edit_control
== NULL
)
7173 HWND tmp
= GetDlgItem (dialog
, FILE_NAME_COMBO_BOX
);
7176 tmp
= GetWindow (tmp
, GW_CHILD
);
7178 edit_control
= GetWindow (tmp
, GW_CHILD
);
7182 /* Directories is in index 2. */
7185 dir_index
= notify_w
->lpOFN
->nFilterIndex
;
7186 hdr_code
= notify_w
->hdr
.code
;
7190 dir_index
= notify_a
->lpOFN
->nFilterIndex
;
7191 hdr_code
= notify_a
->hdr
.code
;
7196 SendMessageW (dialog
, CDM_SETCONTROLTEXT
, FILE_NAME_TEXT_FIELD
,
7197 (LPARAM
)L
"Current Directory");
7199 SendMessageA (dialog
, CDM_SETCONTROLTEXT
, FILE_NAME_TEXT_FIELD
,
7200 (LPARAM
)"Current Directory");
7201 EnableWindow (edit_control
, FALSE
);
7202 /* Note that at least on Windows 7, the above call to EnableWindow
7203 disables the window that would ordinarily have focus. If we
7204 do not set focus to some other window here, focus will land in
7205 no man's land and the user will be unable to tab through the
7206 dialog box (pressing tab will only result in a beep).
7207 Avoid that problem by setting focus to the list here. */
7208 if (hdr_code
== CDN_INITDONE
)
7213 /* Don't override default filename on init done. */
7214 if (hdr_code
== CDN_TYPECHANGE
)
7217 SendMessageW (dialog
, CDM_SETCONTROLTEXT
,
7218 FILE_NAME_TEXT_FIELD
, (LPARAM
)L
"");
7220 SendMessageA (dialog
, CDM_SETCONTROLTEXT
,
7221 FILE_NAME_TEXT_FIELD
, (LPARAM
)"");
7223 EnableWindow (edit_control
, TRUE
);
7230 DEFUN ("x-file-dialog", Fx_file_dialog
, Sx_file_dialog
, 2, 5, 0,
7231 doc
: /* Read file name, prompting with PROMPT in directory DIR.
7232 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
7233 selection box, if specified. If MUSTMATCH is non-nil, the returned file
7234 or directory must exist.
7236 This function is only defined on NS, MS Windows, and X Windows with the
7237 Motif or Gtk toolkits. With the Motif toolkit, ONLY-DIR-P is ignored.
7238 Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories.
7239 On Windows 7 and later, the file selection dialog "remembers" the last
7240 directory where the user selected a file, and will open that directory
7241 instead of DIR on subsequent invocations of this function with the same
7242 value of DIR as in previous invocations; this is standard Windows behavior. */)
7243 (Lisp_Object prompt
, Lisp_Object dir
, Lisp_Object default_filename
, Lisp_Object mustmatch
, Lisp_Object only_dir_p
)
7245 /* Filter index: 1: All Files, 2: Directories only */
7246 static const wchar_t filter_w
[] = L
"All Files (*.*)\0*.*\0Directories\0*|*\0";
7247 static const char filter_a
[] = "All Files (*.*)\0*.*\0Directories\0*|*\0";
7249 Lisp_Object filename
= default_filename
;
7250 struct frame
*f
= SELECTED_FRAME ();
7251 BOOL file_opened
= FALSE
;
7252 Lisp_Object orig_dir
= dir
;
7253 Lisp_Object orig_prompt
= prompt
;
7255 /* If we compile with _WIN32_WINNT set to 0x0400 (for NT4
7256 compatibility) we end up with the old file dialogs. Define a big
7257 enough struct for the new dialog to trick GetOpenFileName into
7258 giving us the new dialogs on newer versions of Windows. */
7260 OPENFILENAMEW details
;
7261 #if _WIN32_WINNT < 0x500 /* < win2k */
7265 #endif /* < win2k */
7266 } new_file_details_w
;
7268 #ifdef NTGUI_UNICODE
7269 wchar_t filename_buf_w
[32*1024 + 1]; // NT kernel maximum
7270 OPENFILENAMEW
* file_details_w
= &new_file_details_w
.details
;
7271 const int use_unicode
= 1;
7272 #else /* not NTGUI_UNICODE */
7274 OPENFILENAMEA details
;
7275 #if _WIN32_WINNT < 0x500 /* < win2k */
7279 #endif /* < win2k */
7280 } new_file_details_a
;
7281 wchar_t filename_buf_w
[MAX_PATH
+ 1], dir_w
[MAX_PATH
];
7282 char filename_buf_a
[MAX_PATH
+ 1], dir_a
[MAX_PATH
];
7283 OPENFILENAMEW
* file_details_w
= &new_file_details_w
.details
;
7284 OPENFILENAMEA
* file_details_a
= &new_file_details_a
.details
;
7285 int use_unicode
= w32_unicode_filenames
;
7289 char fname_ret
[MAX_UTF8_PATH
];
7290 #endif /* NTGUI_UNICODE */
7293 /* Note: under NTGUI_UNICODE, we do _NOT_ use ENCODE_FILE: the
7294 system file encoding expected by the platform APIs (e.g. Cygwin's
7295 POSIX implementation) may not be the same as the encoding expected
7296 by the Windows "ANSI" APIs! */
7298 CHECK_STRING (prompt
);
7301 dir
= Fexpand_file_name (dir
, Qnil
);
7303 if (STRINGP (filename
))
7304 filename
= Ffile_name_nondirectory (filename
);
7306 filename
= empty_unibyte_string
;
7309 dir
= Fcygwin_convert_file_name_to_windows (dir
, Qt
);
7310 if (SCHARS (filename
) > 0)
7311 filename
= Fcygwin_convert_file_name_to_windows (filename
, Qnil
);
7315 CHECK_STRING (filename
);
7317 /* The code in file_dialog_callback that attempts to set the text
7318 of the file name edit window when handling the CDN_INITDONE
7319 WM_NOTIFY message does not work. Setting filename to "Current
7320 Directory" in the only_dir_p case here does work however. */
7321 if (SCHARS (filename
) == 0 && ! NILP (only_dir_p
))
7322 filename
= build_string ("Current Directory");
7324 /* Convert the values we've computed so far to system form. */
7325 #ifdef NTGUI_UNICODE
7326 to_unicode (prompt
, &prompt
);
7327 to_unicode (dir
, &dir
);
7328 to_unicode (filename
, &filename
);
7329 if (SBYTES (filename
) + 1 > sizeof (filename_buf_w
))
7330 report_file_error ("filename too long", default_filename
);
7332 memcpy (filename_buf_w
, SDATA (filename
), SBYTES (filename
) + 1);
7333 #else /* !NTGUI_UNICODE */
7334 prompt
= ENCODE_FILE (prompt
);
7335 dir
= ENCODE_FILE (dir
);
7336 filename
= ENCODE_FILE (filename
);
7338 /* We modify these in-place, so make copies for safety. */
7339 dir
= Fcopy_sequence (dir
);
7340 unixtodos_filename (SSDATA (dir
));
7341 filename
= Fcopy_sequence (filename
);
7342 unixtodos_filename (SSDATA (filename
));
7343 if (SBYTES (filename
) >= MAX_UTF8_PATH
)
7344 report_file_error ("filename too long", default_filename
);
7345 if (w32_unicode_filenames
)
7347 filename_to_utf16 (SSDATA (dir
), dir_w
);
7348 if (filename_to_utf16 (SSDATA (filename
), filename_buf_w
) != 0)
7350 /* filename_to_utf16 sets errno to ENOENT when the file
7351 name is too long or cannot be converted to UTF-16. */
7352 if (errno
== ENOENT
&& filename_buf_w
[MAX_PATH
- 1] != 0)
7353 report_file_error ("filename too long", default_filename
);
7355 len
= pMultiByteToWideChar (CP_UTF8
, multiByteToWideCharFlags
,
7356 SSDATA (prompt
), -1, NULL
, 0);
7359 prompt_w
= alloca (len
* sizeof (wchar_t));
7360 pMultiByteToWideChar (CP_UTF8
, multiByteToWideCharFlags
,
7361 SSDATA (prompt
), -1, prompt_w
, len
);
7365 filename_to_ansi (SSDATA (dir
), dir_a
);
7366 if (filename_to_ansi (SSDATA (filename
), filename_buf_a
) != '\0')
7368 /* filename_to_ansi sets errno to ENOENT when the file
7369 name is too long or cannot be converted to UTF-16. */
7370 if (errno
== ENOENT
&& filename_buf_a
[MAX_PATH
- 1] != 0)
7371 report_file_error ("filename too long", default_filename
);
7373 len
= pMultiByteToWideChar (CP_UTF8
, multiByteToWideCharFlags
,
7374 SSDATA (prompt
), -1, NULL
, 0);
7377 prompt_w
= alloca (len
* sizeof (wchar_t));
7378 pMultiByteToWideChar (CP_UTF8
, multiByteToWideCharFlags
,
7379 SSDATA (prompt
), -1, prompt_w
, len
);
7380 len
= pWideCharToMultiByte (CP_ACP
, 0, prompt_w
, -1, NULL
, 0, NULL
, NULL
);
7383 prompt_a
= alloca (len
);
7384 pWideCharToMultiByte (CP_ACP
, 0, prompt_w
, -1, prompt_a
, len
, NULL
, NULL
);
7386 #endif /* NTGUI_UNICODE */
7388 /* Fill in the structure for the call to GetOpenFileName below.
7389 For NTGUI_UNICODE builds (which run only on NT), we just use
7390 the actual size of the structure. For non-NTGUI_UNICODE
7391 builds, we tell the OS we're using an old version of the
7392 structure if the OS isn't new enough to support the newer
7396 memset (&new_file_details_w
, 0, sizeof (new_file_details_w
));
7397 if (w32_major_version
> 4 && w32_major_version
< 95)
7398 file_details_w
->lStructSize
= sizeof (new_file_details_w
);
7400 file_details_w
->lStructSize
= sizeof (*file_details_w
);
7401 /* Set up the inout parameter for the selected file name. */
7402 file_details_w
->lpstrFile
= filename_buf_w
;
7403 file_details_w
->nMaxFile
=
7404 sizeof (filename_buf_w
) / sizeof (*filename_buf_w
);
7405 file_details_w
->hwndOwner
= FRAME_W32_WINDOW (f
);
7406 /* Undocumented Bug in Common File Dialog:
7407 If a filter is not specified, shell links are not resolved. */
7408 file_details_w
->lpstrFilter
= filter_w
;
7409 #ifdef NTGUI_UNICODE
7410 file_details_w
->lpstrInitialDir
= (wchar_t*) SDATA (dir
);
7411 file_details_w
->lpstrTitle
= (guichar_t
*) SDATA (prompt
);
7413 file_details_w
->lpstrInitialDir
= dir_w
;
7414 file_details_w
->lpstrTitle
= prompt_w
;
7416 file_details_w
->nFilterIndex
= NILP (only_dir_p
) ? 1 : 2;
7417 file_details_w
->Flags
= (OFN_HIDEREADONLY
| OFN_NOCHANGEDIR
7418 | OFN_EXPLORER
| OFN_ENABLEHOOK
);
7419 if (!NILP (mustmatch
))
7421 /* Require that the path to the parent directory exists. */
7422 file_details_w
->Flags
|= OFN_PATHMUSTEXIST
;
7423 /* If we are looking for a file, require that it exists. */
7424 if (NILP (only_dir_p
))
7425 file_details_w
->Flags
|= OFN_FILEMUSTEXIST
;
7428 #ifndef NTGUI_UNICODE
7431 memset (&new_file_details_a
, 0, sizeof (new_file_details_a
));
7432 if (w32_major_version
> 4 && w32_major_version
< 95)
7433 file_details_a
->lStructSize
= sizeof (new_file_details_a
);
7435 file_details_a
->lStructSize
= sizeof (*file_details_a
);
7436 file_details_a
->lpstrFile
= filename_buf_a
;
7437 file_details_a
->nMaxFile
=
7438 sizeof (filename_buf_a
) / sizeof (*filename_buf_a
);
7439 file_details_a
->hwndOwner
= FRAME_W32_WINDOW (f
);
7440 file_details_a
->lpstrFilter
= filter_a
;
7441 file_details_a
->lpstrInitialDir
= dir_a
;
7442 file_details_a
->lpstrTitle
= prompt_a
;
7443 file_details_a
->nFilterIndex
= NILP (only_dir_p
) ? 1 : 2;
7444 file_details_a
->Flags
= (OFN_HIDEREADONLY
| OFN_NOCHANGEDIR
7445 | OFN_EXPLORER
| OFN_ENABLEHOOK
);
7446 if (!NILP (mustmatch
))
7448 /* Require that the path to the parent directory exists. */
7449 file_details_a
->Flags
|= OFN_PATHMUSTEXIST
;
7450 /* If we are looking for a file, require that it exists. */
7451 if (NILP (only_dir_p
))
7452 file_details_a
->Flags
|= OFN_FILEMUSTEXIST
;
7455 #endif /* !NTGUI_UNICODE */
7458 int count
= SPECPDL_INDEX ();
7459 /* Prevent redisplay. */
7460 specbind (Qinhibit_redisplay
, Qt
);
7464 file_details_w
->lpfnHook
= file_dialog_callback
;
7466 file_opened
= GetOpenFileNameW (file_details_w
);
7468 #ifndef NTGUI_UNICODE
7471 file_details_a
->lpfnHook
= file_dialog_callback
;
7473 file_opened
= GetOpenFileNameA (file_details_a
);
7475 #endif /* !NTGUI_UNICODE */
7477 unbind_to (count
, Qnil
);
7482 /* Get an Emacs string from the value Windows gave us. */
7483 #ifdef NTGUI_UNICODE
7484 filename
= from_unicode_buffer (filename_buf_w
);
7485 #else /* !NTGUI_UNICODE */
7487 filename_from_utf16 (filename_buf_w
, fname_ret
);
7489 filename_from_ansi (filename_buf_a
, fname_ret
);
7490 dostounix_filename (fname_ret
);
7491 filename
= DECODE_FILE (build_unibyte_string (fname_ret
));
7492 #endif /* NTGUI_UNICODE */
7495 filename
= Fcygwin_convert_file_name_from_windows (filename
, Qt
);
7498 /* Strip the dummy filename off the end of the string if we
7499 added it to select a directory. */
7500 if ((use_unicode
&& file_details_w
->nFilterIndex
== 2)
7501 #ifndef NTGUI_UNICODE
7502 || (!use_unicode
&& file_details_a
->nFilterIndex
== 2)
7505 filename
= Ffile_name_directory (filename
);
7507 /* User canceled the dialog without making a selection. */
7508 else if (!CommDlgExtendedError ())
7510 /* An error occurred, fallback on reading from the mini-buffer. */
7512 filename
= Fcompleting_read (
7514 intern ("read-file-name-internal"),
7523 /* Make "Cancel" equivalent to C-g. */
7524 if (NILP (filename
))
7525 Fsignal (Qquit
, Qnil
);
7532 /* Moving files to the system recycle bin.
7533 Used by `move-file-to-trash' instead of the default moving to ~/.Trash */
7534 DEFUN ("system-move-file-to-trash", Fsystem_move_file_to_trash
,
7535 Ssystem_move_file_to_trash
, 1, 1, 0,
7536 doc
: /* Move file or directory named FILENAME to the recycle bin. */)
7537 (Lisp_Object filename
)
7539 Lisp_Object handler
;
7540 Lisp_Object encoded_file
;
7541 Lisp_Object operation
;
7543 operation
= Qdelete_file
;
7544 if (!NILP (Ffile_directory_p (filename
))
7545 && NILP (Ffile_symlink_p (filename
)))
7547 operation
= intern ("delete-directory");
7548 filename
= Fdirectory_file_name (filename
);
7551 /* Must have fully qualified file names for moving files to Recycle
7553 filename
= Fexpand_file_name (filename
, Qnil
);
7555 handler
= Ffind_file_name_handler (filename
, operation
);
7556 if (!NILP (handler
))
7557 return call2 (handler
, operation
, filename
);
7563 encoded_file
= ENCODE_FILE (filename
);
7565 path
= map_w32_filename (SSDATA (encoded_file
), NULL
);
7567 /* The Unicode version of SHFileOperation is not supported on
7569 if (w32_unicode_filenames
&& os_subtype
!= OS_9X
)
7571 SHFILEOPSTRUCTW file_op_w
;
7572 /* We need one more element beyond MAX_PATH because this is
7573 a list of file names, with the last element double-null
7575 wchar_t tmp_path_w
[MAX_PATH
+ 1];
7577 memset (tmp_path_w
, 0, sizeof (tmp_path_w
));
7578 filename_to_utf16 (path
, tmp_path_w
);
7580 /* On Windows, write permission is required to delete/move files. */
7581 _wchmod (tmp_path_w
, 0666);
7583 memset (&file_op_w
, 0, sizeof (file_op_w
));
7584 file_op_w
.hwnd
= HWND_DESKTOP
;
7585 file_op_w
.wFunc
= FO_DELETE
;
7586 file_op_w
.pFrom
= tmp_path_w
;
7587 file_op_w
.fFlags
= FOF_SILENT
| FOF_NOCONFIRMATION
| FOF_ALLOWUNDO
7588 | FOF_NOERRORUI
| FOF_NO_CONNECTED_ELEMENTS
;
7589 file_op_w
.fAnyOperationsAborted
= FALSE
;
7591 result
= SHFileOperationW (&file_op_w
);
7595 SHFILEOPSTRUCTA file_op_a
;
7596 char tmp_path_a
[MAX_PATH
+ 1];
7598 memset (tmp_path_a
, 0, sizeof (tmp_path_a
));
7599 filename_to_ansi (path
, tmp_path_a
);
7601 /* If a file cannot be represented in ANSI codepage, don't
7602 let them inadvertently delete other files because some
7603 characters are interpreted as a wildcards. */
7604 if (_mbspbrk ((unsigned char *)tmp_path_a
,
7605 (const unsigned char *)"?*"))
7606 result
= ERROR_FILE_NOT_FOUND
;
7609 _chmod (tmp_path_a
, 0666);
7611 memset (&file_op_a
, 0, sizeof (file_op_a
));
7612 file_op_a
.hwnd
= HWND_DESKTOP
;
7613 file_op_a
.wFunc
= FO_DELETE
;
7614 file_op_a
.pFrom
= tmp_path_a
;
7615 file_op_a
.fFlags
= FOF_SILENT
| FOF_NOCONFIRMATION
| FOF_ALLOWUNDO
7616 | FOF_NOERRORUI
| FOF_NO_CONNECTED_ELEMENTS
;
7617 file_op_a
.fAnyOperationsAborted
= FALSE
;
7619 result
= SHFileOperationA (&file_op_a
);
7623 report_file_error ("Removing old name", list1 (filename
));
7628 #endif /* WINDOWSNT */
7631 /***********************************************************************
7632 w32 specialized functions
7633 ***********************************************************************/
7635 DEFUN ("w32-send-sys-command", Fw32_send_sys_command
,
7636 Sw32_send_sys_command
, 1, 2, 0,
7637 doc
: /* Send frame a Windows WM_SYSCOMMAND message of type COMMAND.
7638 Some useful values for COMMAND are #xf030 to maximize frame (#xf020
7639 to minimize), #xf120 to restore frame to original size, and #xf100
7640 to activate the menubar for keyboard access. #xf140 activates the
7641 screen saver if defined.
7643 If optional parameter FRAME is not specified, use selected frame. */)
7644 (Lisp_Object command
, Lisp_Object frame
)
7646 struct frame
*f
= decode_window_system_frame (frame
);
7648 CHECK_NUMBER (command
);
7650 if (FRAME_W32_P (f
))
7651 PostMessage (FRAME_W32_WINDOW (f
), WM_SYSCOMMAND
, XINT (command
), 0);
7656 DEFUN ("w32-shell-execute", Fw32_shell_execute
, Sw32_shell_execute
, 2, 4, 0,
7657 doc
: /* Get Windows to perform OPERATION on DOCUMENT.
7658 This is a wrapper around the ShellExecute system function, which
7659 invokes the application registered to handle OPERATION for DOCUMENT.
7661 OPERATION is either nil or a string that names a supported operation.
7662 What operations can be used depends on the particular DOCUMENT and its
7663 handler application, but typically it is one of the following common
7666 \"open\" - open DOCUMENT, which could be a file, a directory, or an
7667 executable program (application). If it is an application,
7668 that application is launched in the current buffer's default
7669 directory. Otherwise, the application associated with
7670 DOCUMENT is launched in the buffer's default directory.
7671 \"opennew\" - like \"open\", but instruct the application to open
7672 DOCUMENT in a new window.
7673 \"openas\" - open the \"Open With\" dialog for DOCUMENT.
7674 \"print\" - print DOCUMENT, which must be a file.
7675 \"printto\" - print DOCUMENT, which must be a file, to a specified printer.
7676 The printer should be provided in PARAMETERS, see below.
7677 \"explore\" - start the Windows Explorer on DOCUMENT.
7678 \"edit\" - launch an editor and open DOCUMENT for editing; which
7679 editor is launched depends on the association for the
7681 \"find\" - initiate search starting from DOCUMENT, which must specify
7683 \"delete\" - move DOCUMENT, a file or a directory, to Recycle Bin.
7684 \"copy\" - copy DOCUMENT, which must be a file or a directory, into
7686 \"cut\" - move DOCUMENT, a file or a directory, into the clipboard.
7687 \"paste\" - paste the file whose name is in the clipboard into DOCUMENT,
7688 which must be a directory.
7690 - create a shortcut in DOCUMENT (which must be a directory)
7691 the file or directory whose name is in the clipboard.
7692 \"runas\" - run DOCUMENT, which must be an excutable file, with
7693 elevated privileges (a.k.a. \"as Administrator\").
7695 - open the property sheet dialog for DOCUMENT.
7696 nil - invoke the default OPERATION, or \"open\" if default is
7697 not defined or unavailable.
7699 DOCUMENT is typically the name of a document file or a URL, but can
7700 also be an executable program to run, or a directory to open in the
7701 Windows Explorer. If it is a file or a directory, it must be a local
7702 one; this function does not support remote file names.
7704 If DOCUMENT is an executable program, the optional third arg PARAMETERS
7705 can be a string containing command line parameters, separated by blanks,
7706 that will be passed to the program. Some values of OPERATION also require
7707 parameters (e.g., \"printto\" requires the printer address). Otherwise,
7708 PARAMETERS should be nil or unspecified. Note that double quote characters
7709 in PARAMETERS must each be enclosed in 2 additional quotes, as in \"\"\".
7711 Optional fourth argument SHOW-FLAG can be used to control how the
7712 application will be displayed when it is invoked. If SHOW-FLAG is nil
7713 or unspecified, the application is displayed as if SHOW-FLAG of 10 was
7714 specified, otherwise it is an integer between 0 and 11 representing
7718 1 - start as normal-size window
7719 3 - start in a maximized window
7720 6 - start in a minimized window
7721 10 - start as the application itself specifies; this is the default. */)
7722 (Lisp_Object operation
, Lisp_Object document
, Lisp_Object parameters
, Lisp_Object show_flag
)
7725 Lisp_Object current_dir
= BVAR (current_buffer
, directory
);;
7726 wchar_t *doc_w
= NULL
, *params_w
= NULL
, *ops_w
= NULL
;
7730 int use_unicode
= w32_unicode_filenames
;
7731 char *doc_a
= NULL
, *params_a
= NULL
, *ops_a
= NULL
;
7732 Lisp_Object absdoc
, handler
;
7736 CHECK_STRING (document
);
7739 current_dir
= Fcygwin_convert_file_name_to_windows (current_dir
, Qt
);
7740 document
= Fcygwin_convert_file_name_to_windows (document
, Qt
);
7742 /* Encode filename, current directory and parameters. */
7743 current_dir
= GUI_ENCODE_FILE (current_dir
);
7744 document
= GUI_ENCODE_FILE (document
);
7745 doc_w
= GUI_SDATA (document
);
7746 if (STRINGP (parameters
))
7748 parameters
= GUI_ENCODE_SYSTEM (parameters
);
7749 params_w
= GUI_SDATA (parameters
);
7751 if (STRINGP (operation
))
7753 operation
= GUI_ENCODE_SYSTEM (operation
);
7754 ops_w
= GUI_SDATA (operation
);
7756 result
= (intptr_t) ShellExecuteW (NULL
, ops_w
, doc_w
, params_w
,
7757 GUI_SDATA (current_dir
),
7758 (INTEGERP (show_flag
)
7759 ? XINT (show_flag
) : SW_SHOWDEFAULT
));
7766 case SE_ERR_ACCESSDENIED
:
7767 errstr
= w32_strerror (ERROR_ACCESS_DENIED
);
7769 case SE_ERR_ASSOCINCOMPLETE
:
7770 case SE_ERR_NOASSOC
:
7771 errstr
= w32_strerror (ERROR_NO_ASSOCIATION
);
7773 case SE_ERR_DDEBUSY
:
7774 case SE_ERR_DDEFAIL
:
7775 errstr
= w32_strerror (ERROR_DDE_FAIL
);
7777 case SE_ERR_DDETIMEOUT
:
7778 errstr
= w32_strerror (ERROR_TIMEOUT
);
7780 case SE_ERR_DLLNOTFOUND
:
7781 errstr
= w32_strerror (ERROR_DLL_NOT_FOUND
);
7784 errstr
= w32_strerror (ERROR_FILE_NOT_FOUND
);
7787 errstr
= w32_strerror (ERROR_NOT_ENOUGH_MEMORY
);
7790 errstr
= w32_strerror (ERROR_PATH_NOT_FOUND
);
7793 errstr
= w32_strerror (ERROR_SHARING_VIOLATION
);
7796 errstr
= w32_strerror (0);
7802 const char file_url_str
[] = "file:///";
7803 const int file_url_len
= sizeof (file_url_str
) - 1;
7806 if (strncmp (SSDATA (document
), file_url_str
, file_url_len
) == 0)
7808 /* Passing "file:///" URLs to ShellExecute causes shlwapi.dll to
7809 start a thread in some rare system configurations, for
7810 unknown reasons. That thread is started in the context of
7811 the Emacs process, but out of control of our code, and seems
7812 to never exit afterwards. Each such thread reserves 8MB of
7813 stack space (because that's the value recorded in the Emacs
7814 executable at link time: Emacs needs a large stack). So a
7815 large enough number of invocations of w32-shell-execute can
7816 potentially cause the Emacs process to run out of available
7817 address space, which is nasty. To work around this, we
7818 convert such URLs to local file names, which seems to prevent
7819 those threads from starting. See bug #20220. */
7820 char *p
= SSDATA (document
) + file_url_len
;
7822 if (c_isalpha (*p
) && p
[1] == ':' && IS_DIRECTORY_SEP (p
[2]))
7823 document
= Fsubstring_no_properties (document
,
7824 make_number (file_url_len
), Qnil
);
7826 /* We have a situation here. If DOCUMENT is a relative file name,
7827 but its name includes leading directories, i.e. it lives not in
7828 CURRENT_DIR, but in its subdirectory, then ShellExecute below
7829 will fail to find it. So we need to make the file name is
7830 absolute. But DOCUMENT does not have to be a file, it can be a
7831 URL, for example. So we make it absolute only if it is an
7832 existing file; if it is a file that does not exist, tough. */
7833 absdoc
= Fexpand_file_name (document
, Qnil
);
7834 /* Don't call file handlers for file-exists-p, since they might
7835 attempt to access the file, which could fail or produce undesired
7836 consequences, see bug#16558 for an example. */
7837 handler
= Ffind_file_name_handler (absdoc
, Qfile_exists_p
);
7840 Lisp_Object absdoc_encoded
= ENCODE_FILE (absdoc
);
7842 if (faccessat (AT_FDCWD
, SSDATA (absdoc_encoded
), F_OK
, AT_EACCESS
) == 0)
7844 /* ShellExecute fails if DOCUMENT is a UNC with forward
7845 slashes (expand-file-name above converts all backslashes
7846 to forward slashes). Now that we know DOCUMENT is a
7847 file, we can mirror all forward slashes into backslashes. */
7848 unixtodos_filename (SSDATA (absdoc_encoded
));
7849 document
= absdoc_encoded
;
7852 document
= ENCODE_FILE (document
);
7855 document
= ENCODE_FILE (document
);
7857 current_dir
= ENCODE_FILE (current_dir
);
7858 /* Cannot use filename_to_utf16/ansi with DOCUMENT, since it could
7859 be a URL that is not limited to MAX_PATH chararcters. */
7860 doclen
= pMultiByteToWideChar (CP_UTF8
, multiByteToWideCharFlags
,
7861 SSDATA (document
), -1, NULL
, 0);
7862 doc_w
= xmalloc (doclen
* sizeof (wchar_t));
7863 pMultiByteToWideChar (CP_UTF8
, multiByteToWideCharFlags
,
7864 SSDATA (document
), -1, doc_w
, doclen
);
7867 wchar_t current_dir_w
[MAX_PATH
];
7868 SHELLEXECUTEINFOW shexinfo_w
;
7870 /* Encode the current directory and parameters, and convert
7871 operation to UTF-16. */
7872 filename_to_utf16 (SSDATA (current_dir
), current_dir_w
);
7873 if (STRINGP (parameters
))
7877 parameters
= ENCODE_SYSTEM (parameters
);
7878 len
= pMultiByteToWideChar (CP_ACP
, multiByteToWideCharFlags
,
7879 SSDATA (parameters
), -1, NULL
, 0);
7882 params_w
= alloca (len
* sizeof (wchar_t));
7883 pMultiByteToWideChar (CP_ACP
, multiByteToWideCharFlags
,
7884 SSDATA (parameters
), -1, params_w
, len
);
7885 params_w
[len
- 1] = 0;
7887 if (STRINGP (operation
))
7889 /* Assume OPERATION is pure ASCII. */
7890 const char *s
= SSDATA (operation
);
7892 int len
= SBYTES (operation
) + 1;
7896 d
= ops_w
= alloca (len
* sizeof (wchar_t));
7897 while (d
< ops_w
+ len
- 1)
7902 /* Using ShellExecuteEx and setting the SEE_MASK_INVOKEIDLIST
7903 flag succeeds with more OPERATIONs (a.k.a. "verbs"), as it is
7904 able to invoke verbs from shortcut menu extensions, not just
7905 static verbs listed in the Registry. */
7906 memset (&shexinfo_w
, 0, sizeof (shexinfo_w
));
7907 shexinfo_w
.cbSize
= sizeof (shexinfo_w
);
7909 SEE_MASK_INVOKEIDLIST
| SEE_MASK_FLAG_DDEWAIT
| SEE_MASK_FLAG_NO_UI
;
7910 shexinfo_w
.hwnd
= NULL
;
7911 shexinfo_w
.lpVerb
= ops_w
;
7912 shexinfo_w
.lpFile
= doc_w
;
7913 shexinfo_w
.lpParameters
= params_w
;
7914 shexinfo_w
.lpDirectory
= current_dir_w
;
7916 (INTEGERP (show_flag
) ? XINT (show_flag
) : SW_SHOWDEFAULT
);
7917 success
= ShellExecuteExW (&shexinfo_w
);
7922 char current_dir_a
[MAX_PATH
];
7923 SHELLEXECUTEINFOA shexinfo_a
;
7924 int codepage
= codepage_for_filenames (NULL
);
7925 int ldoc_a
= pWideCharToMultiByte (codepage
, 0, doc_w
, -1, NULL
, 0,
7928 doc_a
= xmalloc (ldoc_a
);
7929 pWideCharToMultiByte (codepage
, 0, doc_w
, -1, doc_a
, ldoc_a
, NULL
, NULL
);
7930 filename_to_ansi (SSDATA (current_dir
), current_dir_a
);
7931 if (STRINGP (parameters
))
7933 parameters
= ENCODE_SYSTEM (parameters
);
7934 params_a
= SSDATA (parameters
);
7936 if (STRINGP (operation
))
7938 /* Assume OPERATION is pure ASCII. */
7939 ops_a
= SSDATA (operation
);
7941 memset (&shexinfo_a
, 0, sizeof (shexinfo_a
));
7942 shexinfo_a
.cbSize
= sizeof (shexinfo_a
);
7944 SEE_MASK_INVOKEIDLIST
| SEE_MASK_FLAG_DDEWAIT
| SEE_MASK_FLAG_NO_UI
;
7945 shexinfo_a
.hwnd
= NULL
;
7946 shexinfo_a
.lpVerb
= ops_a
;
7947 shexinfo_a
.lpFile
= doc_a
;
7948 shexinfo_a
.lpParameters
= params_a
;
7949 shexinfo_a
.lpDirectory
= current_dir_a
;
7951 (INTEGERP (show_flag
) ? XINT (show_flag
) : SW_SHOWDEFAULT
);
7952 success
= ShellExecuteExA (&shexinfo_a
);
7960 errstr
= w32_strerror (0);
7962 #endif /* !CYGWIN */
7964 /* The error string might be encoded in the locale's encoding. */
7965 if (!NILP (Vlocale_coding_system
))
7967 Lisp_Object decoded
=
7968 code_convert_string_norecord (build_unibyte_string (errstr
),
7969 Vlocale_coding_system
, 0);
7970 errstr
= SSDATA (decoded
);
7972 error ("ShellExecute failed: %s", errstr
);
7975 /* Lookup virtual keycode from string representing the name of a
7976 non-ascii keystroke into the corresponding virtual key, using
7977 lispy_function_keys. */
7979 lookup_vk_code (char *key
)
7983 for (i
= 0; i
< 256; i
++)
7984 if (lispy_function_keys
[i
]
7985 && strcmp (lispy_function_keys
[i
], key
) == 0)
7988 if (w32_kbdhook_active
)
7990 /* Alphanumerics map to themselves. */
7993 if (key
[0] >= 'A' && key
[0] <= 'Z' ||
7994 key
[0] >= '0' && key
[0] <= '9')
7996 if (key
[0] >= 'a' && key
[0] <= 'z')
7997 return toupper(key
[0]);
8004 /* Convert a one-element vector style key sequence to a hot key
8007 w32_parse_and_hook_hot_key (Lisp_Object key
, int hook
)
8009 /* Copied from Fdefine_key and store_in_keymap. */
8010 register Lisp_Object c
;
8014 Lisp_Object res
= Qnil
;
8019 if (ASIZE (key
) != 1)
8024 if (CONSP (c
) && lucid_event_type_list_p (c
))
8025 c
= Fevent_convert_list (c
);
8027 if (! INTEGERP (c
) && ! SYMBOLP (c
))
8028 error ("Key definition is invalid");
8030 /* Work out the base key and the modifiers. */
8033 c
= parse_modifiers (c
);
8034 lisp_modifiers
= XINT (Fcar (Fcdr (c
)));
8038 vkname
= SSDATA (SYMBOL_NAME (c
));
8039 /* [s-], [M-], [h-]: Register all keys for this modifier */
8040 if (w32_kbdhook_active
&& vkname
[0] == 0)
8043 vk_code
= lookup_vk_code (vkname
);
8045 else if (INTEGERP (c
))
8047 lisp_modifiers
= XINT (c
) & ~CHARACTERBITS
;
8048 /* Many ascii characters are their own virtual key code. */
8049 vk_code
= XINT (c
) & CHARACTERBITS
;
8052 if (vk_code
< 0 || vk_code
> 255)
8055 if ((lisp_modifiers
& meta_modifier
) != 0
8056 && !NILP (Vw32_alt_is_meta
))
8057 lisp_modifiers
|= alt_modifier
;
8059 /* Supply defs missing from mingw32. */
8061 #define MOD_ALT 0x0001
8062 #define MOD_CONTROL 0x0002
8063 #define MOD_SHIFT 0x0004
8064 #define MOD_WIN 0x0008
8067 if (w32_kbdhook_active
)
8069 /* Register Alt-x combinations. */
8070 if (lisp_modifiers
& alt_modifier
)
8072 hook_w32_key (hook
, VK_MENU
, vk_code
);
8075 /* Register Win-x combinations based on modifier mappings. */
8076 if (((lisp_modifiers
& hyper_modifier
)
8077 && EQ (Vw32_lwindow_modifier
, Qhyper
))
8078 || ((lisp_modifiers
& super_modifier
)
8079 && EQ (Vw32_lwindow_modifier
, Qsuper
)))
8081 hook_w32_key (hook
, VK_LWIN
, vk_code
);
8084 if (((lisp_modifiers
& hyper_modifier
)
8085 && EQ (Vw32_rwindow_modifier
, Qhyper
))
8086 || ((lisp_modifiers
& super_modifier
)
8087 && EQ (Vw32_rwindow_modifier
, Qsuper
)))
8089 hook_w32_key (hook
, VK_RWIN
, vk_code
);
8096 /* Convert lisp modifiers to Windows hot-key form. */
8097 w32_modifiers
= (lisp_modifiers
& hyper_modifier
) ? MOD_WIN
: 0;
8098 w32_modifiers
|= (lisp_modifiers
& alt_modifier
) ? MOD_ALT
: 0;
8099 w32_modifiers
|= (lisp_modifiers
& ctrl_modifier
) ? MOD_CONTROL
: 0;
8100 w32_modifiers
|= (lisp_modifiers
& shift_modifier
) ? MOD_SHIFT
: 0;
8102 return HOTKEY (vk_code
, w32_modifiers
);
8106 DEFUN ("w32-register-hot-key", Fw32_register_hot_key
,
8107 Sw32_register_hot_key
, 1, 1, 0,
8108 doc
: /* Register KEY as a hot-key combination.
8109 Certain key combinations like Alt-Tab and Win-R are reserved for
8110 system use on Windows, and therefore are normally intercepted by the
8111 system. These key combinations can be received by registering them
8112 as hot-keys, except for Win-L which always locks the computer.
8114 On Windows 98 and ME, KEY must be a one element key definition in
8115 vector form that would be acceptable to `define-key' (e.g. [A-tab] for
8116 Alt-Tab). The meta modifier is interpreted as Alt if
8117 `w32-alt-is-meta' is t, and hyper is always interpreted as the Windows
8118 modifier keys. The return value is the hotkey-id if registered, otherwise nil.
8120 On Windows versions since NT, KEY can also be specified as [M-], [s-] or
8121 [h-] to indicate that all combinations of that key should be processed
8122 by Emacs instead of the operating system. The super and hyper
8123 modifiers are interpreted according to the current values of
8124 `w32-lwindow-modifier' and `w32-rwindow-modifier'. For instance,
8125 setting `w32-lwindow-modifier' to `super' and then calling
8126 `(register-hot-key [s-])' grabs all combinations of the left Windows
8127 key to Emacs, but leaves the right Windows key free for the operating
8128 system keyboard shortcuts. The return value is t if the call affected
8129 any key combinations, otherwise nil. */)
8132 key
= w32_parse_and_hook_hot_key (key
, 1);
8134 if (!w32_kbdhook_active
8135 && !NILP (key
) && NILP (Fmemq (key
, w32_grabbed_keys
)))
8137 /* Reuse an empty slot if possible. */
8138 Lisp_Object item
= Fmemq (Qnil
, w32_grabbed_keys
);
8140 /* Safe to add new key to list, even if we have focus. */
8142 w32_grabbed_keys
= Fcons (key
, w32_grabbed_keys
);
8144 XSETCAR (item
, key
);
8146 /* Notify input thread about new hot-key definition, so that it
8147 takes effect without needing to switch focus. */
8148 PostThreadMessage (dwWindowsThreadId
, WM_EMACS_REGISTER_HOT_KEY
,
8149 (WPARAM
) XINT (key
), 0);
8155 DEFUN ("w32-unregister-hot-key", Fw32_unregister_hot_key
,
8156 Sw32_unregister_hot_key
, 1, 1, 0,
8157 doc
: /* Unregister KEY as a hot-key combination. */)
8162 if (!INTEGERP (key
))
8163 key
= w32_parse_and_hook_hot_key (key
, 0);
8165 if (w32_kbdhook_active
)
8168 item
= Fmemq (key
, w32_grabbed_keys
);
8174 eassert (CONSP (item
));
8175 /* Pass the tail of the list as a pointer to a Lisp_Cons cell,
8176 so that it works in a --with-wide-int build as well. */
8177 lparam
= (LPARAM
) XUNTAG (item
, Lisp_Cons
);
8179 /* Notify input thread about hot-key definition being removed, so
8180 that it takes effect without needing focus switch. */
8181 if (PostThreadMessage (dwWindowsThreadId
, WM_EMACS_UNREGISTER_HOT_KEY
,
8182 (WPARAM
) XINT (XCAR (item
)), lparam
))
8185 GetMessage (&msg
, NULL
, WM_EMACS_DONE
, WM_EMACS_DONE
);
8192 DEFUN ("w32-registered-hot-keys", Fw32_registered_hot_keys
,
8193 Sw32_registered_hot_keys
, 0, 0, 0,
8194 doc
: /* Return list of registered hot-key IDs. */)
8197 return Fdelq (Qnil
, Fcopy_sequence (w32_grabbed_keys
));
8200 DEFUN ("w32-reconstruct-hot-key", Fw32_reconstruct_hot_key
,
8201 Sw32_reconstruct_hot_key
, 1, 1, 0,
8202 doc
: /* Convert hot-key ID to a lisp key combination.
8203 usage: (w32-reconstruct-hot-key ID) */)
8204 (Lisp_Object hotkeyid
)
8206 int vk_code
, w32_modifiers
;
8209 CHECK_NUMBER (hotkeyid
);
8211 vk_code
= HOTKEY_VK_CODE (hotkeyid
);
8212 w32_modifiers
= HOTKEY_MODIFIERS (hotkeyid
);
8214 if (vk_code
< 256 && lispy_function_keys
[vk_code
])
8215 key
= intern (lispy_function_keys
[vk_code
]);
8217 key
= make_number (vk_code
);
8219 key
= Fcons (key
, Qnil
);
8220 if (w32_modifiers
& MOD_SHIFT
)
8221 key
= Fcons (Qshift
, key
);
8222 if (w32_modifiers
& MOD_CONTROL
)
8223 key
= Fcons (Qctrl
, key
);
8224 if (w32_modifiers
& MOD_ALT
)
8225 key
= Fcons (NILP (Vw32_alt_is_meta
) ? Qalt
: Qmeta
, key
);
8226 if (w32_modifiers
& MOD_WIN
)
8227 key
= Fcons (Qhyper
, key
);
8232 DEFUN ("w32-toggle-lock-key", Fw32_toggle_lock_key
,
8233 Sw32_toggle_lock_key
, 1, 2, 0,
8234 doc
: /* Toggle the state of the lock key KEY.
8235 KEY can be `capslock', `kp-numlock', or `scroll'.
8236 If the optional parameter NEW-STATE is a number, then the state of KEY
8237 is set to off if the low bit of NEW-STATE is zero, otherwise on.
8238 If NEW-STATE is omitted or nil, the function toggles the state,
8240 Value is the new state of the key, or nil if the function failed
8241 to change the state. */)
8242 (Lisp_Object key
, Lisp_Object new_state
)
8247 if (EQ (key
, intern ("capslock")))
8248 vk_code
= VK_CAPITAL
;
8249 else if (EQ (key
, intern ("kp-numlock")))
8250 vk_code
= VK_NUMLOCK
;
8251 else if (EQ (key
, intern ("scroll")))
8252 vk_code
= VK_SCROLL
;
8256 if (!dwWindowsThreadId
)
8257 return make_number (w32_console_toggle_lock_key (vk_code
, new_state
));
8259 if (NILP (new_state
))
8262 lparam
= (XUINT (new_state
)) & 1;
8263 if (PostThreadMessage (dwWindowsThreadId
, WM_EMACS_TOGGLE_LOCK_KEY
,
8264 (WPARAM
) vk_code
, lparam
))
8267 GetMessage (&msg
, NULL
, WM_EMACS_DONE
, WM_EMACS_DONE
);
8268 return make_number (msg
.wParam
);
8273 DEFUN ("w32-window-exists-p", Fw32_window_exists_p
, Sw32_window_exists_p
,
8275 doc
: /* Return non-nil if a window exists with the specified CLASS and NAME.
8277 This is a direct interface to the Windows API FindWindow function. */)
8278 (Lisp_Object
class, Lisp_Object name
)
8283 CHECK_STRING (class);
8285 CHECK_STRING (name
);
8287 hnd
= FindWindow (STRINGP (class) ? ((LPCTSTR
) SDATA (class)) : NULL
,
8288 STRINGP (name
) ? ((LPCTSTR
) SDATA (name
)) : NULL
);
8294 DEFUN ("w32-frame-geometry", Fw32_frame_geometry
, Sw32_frame_geometry
, 0, 1, 0,
8295 doc
: /* Return geometric attributes of FRAME.
8296 FRAME must be a live frame and defaults to the selected one. The return
8297 value is an association list of the attributes listed below. All height
8298 and width values are in pixels.
8300 `outer-position' is a cons of the outer left and top edges of FRAME
8301 relative to the origin - the position (0, 0) - of FRAME's display.
8303 `outer-size' is a cons of the outer width and height of FRAME. The
8304 outer size includes the title bar and the external borders as well as
8305 any menu and/or tool bar of frame.
8307 `external-border-size' is a cons of the horizontal and vertical width of
8308 FRAME's external borders as supplied by the window manager.
8310 `title-bar-size' is a cons of the width and height of the title bar of
8311 FRAME as supplied by the window manager. If both of them are zero,
8312 FRAME has no title bar. If only the width is zero, Emacs was not
8313 able to retrieve the width information.
8315 `menu-bar-external', if non-nil, means the menu bar is external (never
8316 included in the inner edges of FRAME).
8318 `menu-bar-size' is a cons of the width and height of the menu bar of
8321 `tool-bar-external', if non-nil, means the tool bar is external (never
8322 included in the inner edges of FRAME).
8324 `tool-bar-position' tells on which side the tool bar on FRAME is and can
8325 be one of `left', `top', `right' or `bottom'. If this is nil, FRAME
8328 `tool-bar-size' is a cons of the width and height of the tool bar of
8331 `internal-border-width' is the width of the internal border of
8335 struct frame
*f
= decode_live_frame (frame
);
8337 MENUBARINFO menu_bar
;
8339 int left
, top
, right
, bottom
;
8340 unsigned int external_border_width
, external_border_height
;
8341 int title_bar_width
= 0, title_bar_height
= 0;
8342 int single_menu_bar_height
, wrapped_menu_bar_height
, menu_bar_height
;
8343 int tool_bar_height
= FRAME_TOOL_BAR_HEIGHT (f
);
8344 int internal_border_width
= FRAME_INTERNAL_BORDER_WIDTH (f
);
8346 if (FRAME_INITIAL_P (f
) || !FRAME_W32_P (f
))
8350 /* Outer rectangle and borders. */
8351 window
.cbSize
= sizeof (window
);
8352 GetWindowInfo (FRAME_W32_WINDOW (f
), &window
);
8353 external_border_width
= window
.cxWindowBorders
;
8354 external_border_height
= window
.cyWindowBorders
;
8356 if (get_title_bar_info_fn
)
8358 TITLEBAR_INFO title_bar
;
8360 title_bar
.cbSize
= sizeof (title_bar
);
8361 title_bar
.rcTitleBar
.left
= title_bar
.rcTitleBar
.right
= 0;
8362 title_bar
.rcTitleBar
.top
= title_bar
.rcTitleBar
.bottom
= 0;
8363 for (int i
= 0; i
< 6; i
++)
8364 title_bar
.rgstate
[i
] = 0;
8365 if (get_title_bar_info_fn (FRAME_W32_WINDOW (f
), &title_bar
)
8366 && !(title_bar
.rgstate
[0] & 0x00008001))
8369 = title_bar
.rcTitleBar
.right
- title_bar
.rcTitleBar
.left
;
8371 = title_bar
.rcTitleBar
.bottom
- title_bar
.rcTitleBar
.top
;
8374 else if ((window
.dwStyle
& WS_CAPTION
) == WS_CAPTION
)
8375 title_bar_height
= GetSystemMetrics (SM_CYCAPTION
);
8377 menu_bar
.cbSize
= sizeof (menu_bar
);
8378 menu_bar
.rcBar
.right
= menu_bar
.rcBar
.left
= 0;
8379 menu_bar
.rcBar
.top
= menu_bar
.rcBar
.bottom
= 0;
8380 GetMenuBarInfo (FRAME_W32_WINDOW (f
), 0xFFFFFFFD, 0, &menu_bar
);
8381 single_menu_bar_height
= GetSystemMetrics (SM_CYMENU
);
8382 wrapped_menu_bar_height
= GetSystemMetrics (SM_CYMENUSIZE
);
8385 left
= window
.rcWindow
.left
;
8386 top
= window
.rcWindow
.top
;
8387 right
= window
.rcWindow
.right
;
8388 bottom
= window
.rcWindow
.bottom
;
8391 menu_bar_height
= menu_bar
.rcBar
.bottom
- menu_bar
.rcBar
.top
;
8392 /* Fix menu bar height reported by GetMenuBarInfo. */
8393 if (menu_bar_height
> single_menu_bar_height
)
8394 /* A wrapped menu bar. */
8395 menu_bar_height
+= single_menu_bar_height
- wrapped_menu_bar_height
;
8396 else if (menu_bar_height
> 0)
8397 /* A single line menu bar. */
8398 menu_bar_height
= single_menu_bar_height
;
8400 return listn (CONSTYPE_HEAP
, 10,
8401 Fcons (Qouter_position
,
8402 Fcons (make_number (left
), make_number (top
))),
8404 Fcons (make_number (right
- left
),
8405 make_number (bottom
- top
))),
8406 Fcons (Qexternal_border_size
,
8407 Fcons (make_number (external_border_width
),
8408 make_number (external_border_height
))),
8409 Fcons (Qtitle_bar_size
,
8410 Fcons (make_number (title_bar_width
),
8411 make_number (title_bar_height
))),
8412 Fcons (Qmenu_bar_external
, Qt
),
8413 Fcons (Qmenu_bar_size
,
8415 (menu_bar
.rcBar
.right
- menu_bar
.rcBar
.left
),
8416 make_number (menu_bar_height
))),
8417 Fcons (Qtool_bar_external
, Qnil
),
8418 Fcons (Qtool_bar_position
, tool_bar_height
? Qtop
: Qnil
),
8419 Fcons (Qtool_bar_size
,
8422 ? right
- left
- 2 * internal_border_width
8424 make_number (tool_bar_height
))),
8425 Fcons (Qinternal_border_width
,
8426 make_number (internal_border_width
)));
8429 DEFUN ("w32-frame-edges", Fw32_frame_edges
, Sw32_frame_edges
, 0, 2, 0,
8430 doc
: /* Return edge coordinates of FRAME.
8431 FRAME must be a live frame and defaults to the selected one. The return
8432 value is a list of the form (LEFT, TOP, RIGHT, BOTTOM). All values are
8433 in pixels relative to the origin - the position (0, 0) - of FRAME's
8436 If optional argument TYPE is the symbol `outer-edges', return the outer
8437 edges of FRAME. The outer edges comprise the decorations of the window
8438 manager (like the title bar or external borders) as well as any external
8439 menu or tool bar of FRAME. If optional argument TYPE is the symbol
8440 `native-edges' or nil, return the native edges of FRAME. The native
8441 edges exclude the decorations of the window manager and any external
8442 menu or tool bar of FRAME. If TYPE is the symbol `inner-edges', return
8443 the inner edges of FRAME. These edges exclude title bar, any borders,
8444 menu bar or tool bar of FRAME. */)
8445 (Lisp_Object frame
, Lisp_Object type
)
8447 struct frame
*f
= decode_live_frame (frame
);
8449 if (FRAME_INITIAL_P (f
) || !FRAME_W32_P (f
))
8452 if (EQ (type
, Qouter_edges
))
8457 /* Outer frame rectangle, including outer borders and title bar. */
8458 GetWindowRect (FRAME_W32_WINDOW (f
), &rectangle
);
8461 return list4 (make_number (rectangle
.left
),
8462 make_number (rectangle
.top
),
8463 make_number (rectangle
.right
),
8464 make_number (rectangle
.bottom
));
8470 int left
, top
, right
, bottom
;
8473 /* Inner frame rectangle, excluding borders and title bar. */
8474 GetClientRect (FRAME_W32_WINDOW (f
), &rectangle
);
8475 /* Get top-left corner of native rectangle in screen
8479 ClientToScreen (FRAME_W32_WINDOW (f
), &pt
);
8484 right
= left
+ rectangle
.right
;
8485 bottom
= top
+ rectangle
.bottom
;
8487 if (EQ (type
, Qinner_edges
))
8489 int internal_border_width
= FRAME_INTERNAL_BORDER_WIDTH (f
);
8491 return list4 (make_number (left
+ internal_border_width
),
8493 + FRAME_TOOL_BAR_HEIGHT (f
)
8494 + internal_border_width
),
8495 make_number (right
- internal_border_width
),
8496 make_number (bottom
- internal_border_width
));
8499 return list4 (make_number (left
), make_number (top
),
8500 make_number (right
), make_number (bottom
));
8504 DEFUN ("w32-mouse-absolute-pixel-position", Fw32_mouse_absolute_pixel_position
,
8505 Sw32_mouse_absolute_pixel_position
, 0, 0, 0,
8506 doc
: /* Return absolute position of mouse cursor in pixels.
8507 The position is returned as a cons cell (X . Y) of the coordinates of
8508 the mouse cursor position in pixels relative to a position (0, 0) of the
8509 selected frame's display. */)
8518 return Fcons (make_number (pt
.x
), make_number (pt
.y
));
8521 DEFUN ("w32-set-mouse-absolute-pixel-position", Fw32_set_mouse_absolute_pixel_position
,
8522 Sw32_set_mouse_absolute_pixel_position
, 2, 2, 0,
8523 doc
: /* Move mouse pointer to absolute pixel position (X, Y).
8524 The coordinates X and Y are interpreted in pixels relative to a position
8525 (0, 0) of the selected frame's display. */)
8526 (Lisp_Object x
, Lisp_Object y
)
8531 CHECK_TYPE_RANGED_INTEGER (int, x
);
8532 CHECK_TYPE_RANGED_INTEGER (int, y
);
8535 /* When "mouse trails" are in effect, moving the mouse cursor
8536 sometimes leaves behind an annoying "ghost" of the pointer.
8537 Avoid that by momentarily switching off mouse trails. */
8538 if (os_subtype
== OS_NT
8539 && w32_major_version
+ w32_minor_version
>= 6)
8540 ret
= SystemParametersInfo (SPI_GETMOUSETRAILS
, 0, &trail_num
, 0);
8541 SetCursorPos (XINT (x
), XINT (y
));
8543 SystemParametersInfo (SPI_SETMOUSETRAILS
, trail_num
, NULL
, 0);
8549 DEFUN ("w32-battery-status", Fw32_battery_status
, Sw32_battery_status
, 0, 0, 0,
8550 doc
: /* Get power status information from Windows system.
8552 The following %-sequences are provided:
8553 %L AC line status (verbose)
8554 %B Battery status (verbose)
8555 %b Battery status, empty means high, `-' means low,
8556 `!' means critical, and `+' means charging
8557 %p Battery load percentage
8558 %s Remaining time (to charge or discharge) in seconds
8559 %m Remaining time (to charge or discharge) in minutes
8560 %h Remaining time (to charge or discharge) in hours
8561 %t Remaining time (to charge or discharge) in the form `h:min' */)
8564 Lisp_Object status
= Qnil
;
8566 SYSTEM_POWER_STATUS system_status
;
8567 if (GetSystemPowerStatus (&system_status
))
8569 Lisp_Object line_status
, battery_status
, battery_status_symbol
;
8570 Lisp_Object load_percentage
, seconds
, minutes
, hours
, remain
;
8572 long seconds_left
= (long) system_status
.BatteryLifeTime
;
8574 if (system_status
.ACLineStatus
== 0)
8575 line_status
= build_string ("off-line");
8576 else if (system_status
.ACLineStatus
== 1)
8577 line_status
= build_string ("on-line");
8579 line_status
= build_string ("N/A");
8581 if (system_status
.BatteryFlag
& 128)
8583 battery_status
= build_string ("N/A");
8584 battery_status_symbol
= empty_unibyte_string
;
8586 else if (system_status
.BatteryFlag
& 8)
8588 battery_status
= build_string ("charging");
8589 battery_status_symbol
= build_string ("+");
8590 if (system_status
.BatteryFullLifeTime
!= -1L)
8591 seconds_left
= system_status
.BatteryFullLifeTime
- seconds_left
;
8593 else if (system_status
.BatteryFlag
& 4)
8595 battery_status
= build_string ("critical");
8596 battery_status_symbol
= build_string ("!");
8598 else if (system_status
.BatteryFlag
& 2)
8600 battery_status
= build_string ("low");
8601 battery_status_symbol
= build_string ("-");
8603 else if (system_status
.BatteryFlag
& 1)
8605 battery_status
= build_string ("high");
8606 battery_status_symbol
= empty_unibyte_string
;
8610 battery_status
= build_string ("medium");
8611 battery_status_symbol
= empty_unibyte_string
;
8614 if (system_status
.BatteryLifePercent
> 100)
8615 load_percentage
= build_string ("N/A");
8619 snprintf (buffer
, 16, "%d", system_status
.BatteryLifePercent
);
8620 load_percentage
= build_string (buffer
);
8623 if (seconds_left
< 0)
8624 seconds
= minutes
= hours
= remain
= build_string ("N/A");
8630 snprintf (buffer
, 16, "%ld", seconds_left
);
8631 seconds
= build_string (buffer
);
8633 m
= seconds_left
/ 60;
8634 snprintf (buffer
, 16, "%ld", m
);
8635 minutes
= build_string (buffer
);
8637 h
= seconds_left
/ 3600.0;
8638 snprintf (buffer
, 16, "%3.1f", h
);
8639 hours
= build_string (buffer
);
8641 snprintf (buffer
, 16, "%ld:%02ld", m
/ 60, m
% 60);
8642 remain
= build_string (buffer
);
8645 status
= listn (CONSTYPE_HEAP
, 8,
8646 Fcons (make_number ('L'), line_status
),
8647 Fcons (make_number ('B'), battery_status
),
8648 Fcons (make_number ('b'), battery_status_symbol
),
8649 Fcons (make_number ('p'), load_percentage
),
8650 Fcons (make_number ('s'), seconds
),
8651 Fcons (make_number ('m'), minutes
),
8652 Fcons (make_number ('h'), hours
),
8653 Fcons (make_number ('t'), remain
));
8660 typedef BOOL (WINAPI
*GetDiskFreeSpaceExW_Proc
)
8661 (LPCWSTR
, PULARGE_INTEGER
, PULARGE_INTEGER
, PULARGE_INTEGER
);
8662 typedef BOOL (WINAPI
*GetDiskFreeSpaceExA_Proc
)
8663 (LPCSTR
, PULARGE_INTEGER
, PULARGE_INTEGER
, PULARGE_INTEGER
);
8665 DEFUN ("file-system-info", Ffile_system_info
, Sfile_system_info
, 1, 1, 0,
8666 doc
: /* Return storage information about the file system FILENAME is on.
8667 Value is a list of floats (TOTAL FREE AVAIL), where TOTAL is the total
8668 storage of the file system, FREE is the free storage, and AVAIL is the
8669 storage available to a non-superuser. All 3 numbers are in bytes.
8670 If the underlying system call fails, value is nil. */)
8671 (Lisp_Object filename
)
8673 Lisp_Object encoded
, value
;
8675 CHECK_STRING (filename
);
8676 filename
= Fexpand_file_name (filename
, Qnil
);
8677 encoded
= ENCODE_FILE (filename
);
8681 /* Determining the required information on Windows turns out, sadly,
8682 to be more involved than one would hope. The original Windows API
8683 call for this will return bogus information on some systems, but we
8684 must dynamically probe for the replacement api, since that was
8685 added rather late on. */
8687 HMODULE hKernel
= GetModuleHandle ("kernel32");
8688 GetDiskFreeSpaceExW_Proc pfn_GetDiskFreeSpaceExW
=
8689 (GetDiskFreeSpaceExW_Proc
) GetProcAddress (hKernel
, "GetDiskFreeSpaceExW");
8690 GetDiskFreeSpaceExA_Proc pfn_GetDiskFreeSpaceExA
=
8691 (GetDiskFreeSpaceExA_Proc
) GetProcAddress (hKernel
, "GetDiskFreeSpaceExA");
8692 bool have_pfn_GetDiskFreeSpaceEx
=
8693 ((w32_unicode_filenames
&& pfn_GetDiskFreeSpaceExW
)
8694 || (!w32_unicode_filenames
&& pfn_GetDiskFreeSpaceExA
));
8696 /* On Windows, we may need to specify the root directory of the
8697 volume holding FILENAME. */
8698 char rootname
[MAX_UTF8_PATH
];
8699 wchar_t rootname_w
[MAX_PATH
];
8700 char rootname_a
[MAX_PATH
];
8701 char *name
= SSDATA (encoded
);
8704 /* find the root name of the volume if given */
8705 if (isalpha (name
[0]) && name
[1] == ':')
8707 rootname
[0] = name
[0];
8708 rootname
[1] = name
[1];
8712 else if (IS_DIRECTORY_SEP (name
[0]) && IS_DIRECTORY_SEP (name
[1]))
8714 char *str
= rootname
;
8718 if (IS_DIRECTORY_SEP (*name
) && --slashes
== 0)
8728 if (w32_unicode_filenames
)
8729 filename_to_utf16 (rootname
, rootname_w
);
8731 filename_to_ansi (rootname
, rootname_a
);
8733 if (have_pfn_GetDiskFreeSpaceEx
)
8735 /* Unsigned large integers cannot be cast to double, so
8736 use signed ones instead. */
8737 LARGE_INTEGER availbytes
;
8738 LARGE_INTEGER freebytes
;
8739 LARGE_INTEGER totalbytes
;
8741 if (w32_unicode_filenames
)
8742 result
= pfn_GetDiskFreeSpaceExW (rootname_w
,
8743 (ULARGE_INTEGER
*)&availbytes
,
8744 (ULARGE_INTEGER
*)&totalbytes
,
8745 (ULARGE_INTEGER
*)&freebytes
);
8747 result
= pfn_GetDiskFreeSpaceExA (rootname_a
,
8748 (ULARGE_INTEGER
*)&availbytes
,
8749 (ULARGE_INTEGER
*)&totalbytes
,
8750 (ULARGE_INTEGER
*)&freebytes
);
8752 value
= list3 (make_float ((double) totalbytes
.QuadPart
),
8753 make_float ((double) freebytes
.QuadPart
),
8754 make_float ((double) availbytes
.QuadPart
));
8758 DWORD sectors_per_cluster
;
8759 DWORD bytes_per_sector
;
8760 DWORD free_clusters
;
8761 DWORD total_clusters
;
8763 if (w32_unicode_filenames
)
8764 result
= GetDiskFreeSpaceW (rootname_w
,
8765 §ors_per_cluster
,
8770 result
= GetDiskFreeSpaceA (rootname_a
,
8771 §ors_per_cluster
,
8776 value
= list3 (make_float ((double) total_clusters
8777 * sectors_per_cluster
* bytes_per_sector
),
8778 make_float ((double) free_clusters
8779 * sectors_per_cluster
* bytes_per_sector
),
8780 make_float ((double) free_clusters
8781 * sectors_per_cluster
* bytes_per_sector
));
8787 #endif /* WINDOWSNT */
8791 DEFUN ("default-printer-name", Fdefault_printer_name
, Sdefault_printer_name
,
8792 0, 0, 0, doc
: /* Return the name of Windows default printer device. */)
8795 static char pname_buf
[256];
8798 PRINTER_INFO_2W
*ppi2w
= NULL
;
8799 PRINTER_INFO_2A
*ppi2a
= NULL
;
8800 DWORD dwNeeded
= 0, dwReturned
= 0;
8801 char server_name
[MAX_UTF8_PATH
], share_name
[MAX_UTF8_PATH
];
8802 char port_name
[MAX_UTF8_PATH
];
8804 /* Retrieve the default string from Win.ini (the registry).
8805 * String will be in form "printername,drivername,portname".
8806 * This is the most portable way to get the default printer. */
8807 if (GetProfileString ("windows", "device", ",,", pname_buf
, sizeof (pname_buf
)) <= 0)
8809 /* printername precedes first "," character */
8810 strtok (pname_buf
, ",");
8811 /* We want to know more than the printer name */
8812 if (!OpenPrinter (pname_buf
, &hPrn
, NULL
))
8814 /* GetPrinterW is not supported by unicows.dll. */
8815 if (w32_unicode_filenames
&& os_subtype
!= OS_9X
)
8816 GetPrinterW (hPrn
, 2, NULL
, 0, &dwNeeded
);
8818 GetPrinterA (hPrn
, 2, NULL
, 0, &dwNeeded
);
8821 ClosePrinter (hPrn
);
8824 /* Call GetPrinter again with big enough memory block. */
8825 if (w32_unicode_filenames
&& os_subtype
!= OS_9X
)
8827 /* Allocate memory for the PRINTER_INFO_2 struct. */
8828 ppi2w
= xmalloc (dwNeeded
);
8829 err
= GetPrinterW (hPrn
, 2, (LPBYTE
)ppi2w
, dwNeeded
, &dwReturned
);
8830 ClosePrinter (hPrn
);
8837 if ((ppi2w
->Attributes
& PRINTER_ATTRIBUTE_SHARED
)
8838 && ppi2w
->pServerName
)
8840 filename_from_utf16 (ppi2w
->pServerName
, server_name
);
8841 filename_from_utf16 (ppi2w
->pShareName
, share_name
);
8845 server_name
[0] = '\0';
8846 filename_from_utf16 (ppi2w
->pPortName
, port_name
);
8851 ppi2a
= xmalloc (dwNeeded
);
8852 err
= GetPrinterA (hPrn
, 2, (LPBYTE
)ppi2a
, dwNeeded
, &dwReturned
);
8853 ClosePrinter (hPrn
);
8860 if ((ppi2a
->Attributes
& PRINTER_ATTRIBUTE_SHARED
)
8861 && ppi2a
->pServerName
)
8863 filename_from_ansi (ppi2a
->pServerName
, server_name
);
8864 filename_from_ansi (ppi2a
->pShareName
, share_name
);
8868 server_name
[0] = '\0';
8869 filename_from_ansi (ppi2a
->pPortName
, port_name
);
8875 /* a remote printer */
8876 if (server_name
[0] == '\\')
8877 snprintf (pname_buf
, sizeof (pname_buf
), "%s\\%s", server_name
,
8880 snprintf (pname_buf
, sizeof (pname_buf
), "\\\\%s\\%s", server_name
,
8882 pname_buf
[sizeof (pname_buf
) - 1] = '\0';
8886 /* a local printer */
8887 strncpy (pname_buf
, port_name
, sizeof (pname_buf
));
8888 pname_buf
[sizeof (pname_buf
) - 1] = '\0';
8889 /* `pPortName' can include several ports, delimited by ','.
8890 * we only use the first one. */
8891 strtok (pname_buf
, ",");
8894 return DECODE_FILE (build_unibyte_string (pname_buf
));
8896 #endif /* WINDOWSNT */
8899 /* Equivalent of strerror for W32 error codes. */
8901 w32_strerror (int error_no
)
8903 static char buf
[500];
8907 error_no
= GetLastError ();
8909 ret
= FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
|
8910 FORMAT_MESSAGE_IGNORE_INSERTS
,
8913 0, /* choose most suitable language */
8914 buf
, sizeof (buf
), NULL
);
8916 while (ret
> 0 && (buf
[ret
- 1] == '\n' ||
8917 buf
[ret
- 1] == '\r' ))
8921 sprintf (buf
, "w32 error %u", error_no
);
8926 /* For convenience when debugging. (You cannot call GetLastError
8927 directly from GDB: it will crash, because it uses the __stdcall
8928 calling convention, not the _cdecl convention assumed by GDB.) */
8930 w32_last_error (void)
8932 return GetLastError ();
8935 /* Cache information describing the NT system for later use. */
8937 cache_system_info (void)
8950 /* Cache the module handle of Emacs itself. */
8951 hinst
= GetModuleHandle (NULL
);
8953 /* Cache the version of the operating system. */
8954 version
.data
= GetVersion ();
8955 w32_major_version
= version
.info
.major
;
8956 w32_minor_version
= version
.info
.minor
;
8958 if (version
.info
.platform
& 0x8000)
8963 /* Cache page size, allocation unit, processor type, etc. */
8964 GetSystemInfo (&sysinfo_cache
);
8965 syspage_mask
= (DWORD_PTR
)sysinfo_cache
.dwPageSize
- 1;
8967 /* Cache os info. */
8968 osinfo_cache
.dwOSVersionInfoSize
= sizeof (OSVERSIONINFO
);
8969 GetVersionEx (&osinfo_cache
);
8971 w32_build_number
= osinfo_cache
.dwBuildNumber
;
8972 if (os_subtype
== OS_9X
)
8973 w32_build_number
&= 0xffff;
8975 w32_num_mouse_buttons
= GetSystemMetrics (SM_CMOUSEBUTTONS
);
8980 _DebPrint (const char *fmt
, ...)
8985 va_start (args
, fmt
);
8986 vsprintf (buf
, fmt
, args
);
8989 fprintf (stderr
, "%s", buf
);
8991 OutputDebugString (buf
);
8996 w32_console_toggle_lock_key (int vk_code
, Lisp_Object new_state
)
8998 int cur_state
= (GetKeyState (vk_code
) & 1);
9000 if (NILP (new_state
)
9001 || (NUMBERP (new_state
)
9002 && ((XUINT (new_state
)) & 1) != cur_state
))
9005 faked_key
= vk_code
;
9006 #endif /* WINDOWSNT */
9008 keybd_event ((BYTE
) vk_code
,
9009 (BYTE
) MapVirtualKey (vk_code
, 0),
9010 KEYEVENTF_EXTENDEDKEY
| KEYEVENTF_KEYUP
, 0);
9011 keybd_event ((BYTE
) vk_code
,
9012 (BYTE
) MapVirtualKey (vk_code
, 0),
9013 KEYEVENTF_EXTENDEDKEY
| 0, 0);
9014 keybd_event ((BYTE
) vk_code
,
9015 (BYTE
) MapVirtualKey (vk_code
, 0),
9016 KEYEVENTF_EXTENDEDKEY
| KEYEVENTF_KEYUP
, 0);
9017 cur_state
= !cur_state
;
9023 /* Translate console modifiers to emacs modifiers.
9024 German keyboard support (Kai Morgan Zeise 2/18/95). */
9026 w32_kbd_mods_to_emacs (DWORD mods
, WORD key
)
9030 /* If we recognize right-alt and left-ctrl as AltGr, and it has been
9031 pressed, first remove those modifiers. */
9032 if (!NILP (Vw32_recognize_altgr
)
9033 && (mods
& (RIGHT_ALT_PRESSED
| LEFT_CTRL_PRESSED
))
9034 == (RIGHT_ALT_PRESSED
| LEFT_CTRL_PRESSED
))
9035 mods
&= ~ (RIGHT_ALT_PRESSED
| LEFT_CTRL_PRESSED
);
9037 if (mods
& (RIGHT_ALT_PRESSED
| LEFT_ALT_PRESSED
))
9038 retval
= ((NILP (Vw32_alt_is_meta
)) ? alt_modifier
: meta_modifier
);
9040 if (mods
& (RIGHT_CTRL_PRESSED
| LEFT_CTRL_PRESSED
))
9042 retval
|= ctrl_modifier
;
9043 if ((mods
& (RIGHT_CTRL_PRESSED
| LEFT_CTRL_PRESSED
))
9044 == (RIGHT_CTRL_PRESSED
| LEFT_CTRL_PRESSED
))
9045 retval
|= meta_modifier
;
9048 if (mods
& LEFT_WIN_PRESSED
)
9049 retval
|= w32_key_to_modifier (VK_LWIN
);
9050 if (mods
& RIGHT_WIN_PRESSED
)
9051 retval
|= w32_key_to_modifier (VK_RWIN
);
9052 if (mods
& APPS_PRESSED
)
9053 retval
|= w32_key_to_modifier (VK_APPS
);
9054 if (mods
& SCROLLLOCK_ON
)
9055 retval
|= w32_key_to_modifier (VK_SCROLL
);
9057 /* Just in case someone wanted the original behavior, make it
9058 optional by setting w32-capslock-is-shiftlock to t. */
9059 if (NILP (Vw32_capslock_is_shiftlock
)
9060 /* Keys that should _not_ be affected by CapsLock. */
9061 && ( (key
== VK_BACK
)
9063 || (key
== VK_CLEAR
)
9064 || (key
== VK_RETURN
)
9065 || (key
== VK_ESCAPE
)
9066 || ((key
>= VK_SPACE
) && (key
<= VK_HELP
))
9067 || ((key
>= VK_NUMPAD0
) && (key
<= VK_F24
))
9068 || ((key
>= VK_NUMPAD_CLEAR
) && (key
<= VK_NUMPAD_DELETE
))
9071 /* Only consider shift state. */
9072 if ((mods
& SHIFT_PRESSED
) != 0)
9073 retval
|= shift_modifier
;
9077 /* Ignore CapsLock state if not enabled. */
9078 if (NILP (Vw32_enable_caps_lock
))
9079 mods
&= ~CAPSLOCK_ON
;
9080 if ((mods
& (SHIFT_PRESSED
| CAPSLOCK_ON
)) != 0)
9081 retval
|= shift_modifier
;
9087 /* The return code indicates key code size. cpID is the codepage to
9088 use for translation to Unicode; -1 means use the current console
9091 w32_kbd_patch_key (KEY_EVENT_RECORD
*event
, int cpId
)
9093 unsigned int key_code
= event
->wVirtualKeyCode
;
9094 unsigned int mods
= event
->dwControlKeyState
;
9096 static BYTE ansi_code
[4];
9097 static int isdead
= 0;
9101 event
->uChar
.AsciiChar
= ansi_code
[2];
9105 if (event
->uChar
.AsciiChar
!= 0)
9108 memset (keystate
, 0, sizeof (keystate
));
9109 keystate
[key_code
] = 0x80;
9110 if (mods
& SHIFT_PRESSED
)
9111 keystate
[VK_SHIFT
] = 0x80;
9112 if (mods
& CAPSLOCK_ON
)
9113 keystate
[VK_CAPITAL
] = 1;
9114 /* If we recognize right-alt and left-ctrl as AltGr, set the key
9115 states accordingly before invoking ToAscii. */
9116 if (!NILP (Vw32_recognize_altgr
)
9117 && (mods
& LEFT_CTRL_PRESSED
) && (mods
& RIGHT_ALT_PRESSED
))
9119 keystate
[VK_CONTROL
] = 0x80;
9120 keystate
[VK_LCONTROL
] = 0x80;
9121 keystate
[VK_MENU
] = 0x80;
9122 keystate
[VK_RMENU
] = 0x80;
9126 /* Because of an OS bug, ToAscii corrupts the stack when called to
9127 convert a dead key in console mode on NT4. Unfortunately, trying
9128 to check for dead keys using MapVirtualKey doesn't work either -
9129 these functions apparently use internal information about keyboard
9130 layout which doesn't get properly updated in console programs when
9131 changing layout (though apparently it gets partly updated,
9132 otherwise ToAscii wouldn't crash). */
9133 if (is_dead_key (event
->wVirtualKeyCode
))
9137 /* On NT, call ToUnicode instead and then convert to the current
9138 console input codepage. */
9139 if (os_subtype
== OS_NT
)
9143 isdead
= ToUnicode (event
->wVirtualKeyCode
, event
->wVirtualScanCode
,
9144 keystate
, buf
, 128, 0);
9147 /* When we are called from the GUI message processing code,
9148 we are passed the current keyboard codepage, a positive
9149 number, to use below. */
9151 cpId
= GetConsoleCP ();
9153 event
->uChar
.UnicodeChar
= buf
[isdead
- 1];
9154 isdead
= WideCharToMultiByte (cpId
, 0, buf
, isdead
,
9155 (LPSTR
)ansi_code
, 4, NULL
, NULL
);
9162 isdead
= ToAscii (event
->wVirtualKeyCode
, event
->wVirtualScanCode
,
9163 keystate
, (LPWORD
) ansi_code
, 0);
9168 event
->uChar
.AsciiChar
= ansi_code
[0];
9174 w32_sys_ring_bell (struct frame
*f
)
9176 if (sound_type
== 0xFFFFFFFF)
9180 else if (sound_type
== MB_EMACS_SILENT
)
9185 MessageBeep (sound_type
);
9188 DEFUN ("w32--menu-bar-in-use", Fw32__menu_bar_in_use
, Sw32__menu_bar_in_use
,
9190 doc
: /* Return non-nil when a menu-bar menu is being used.
9191 Internal use only. */)
9194 return menubar_in_use
? Qt
: Qnil
;
9197 #if defined WINDOWSNT && !defined HAVE_DBUS
9199 /***********************************************************************
9201 ***********************************************************************/
9202 /* A private struct declaration to avoid compile-time limits. */
9203 typedef struct MY_NOTIFYICONDATAW
{
9208 UINT uCallbackMessage
;
9214 _ANONYMOUS_UNION
union {
9218 WCHAR szInfoTitle
[64];
9222 } MY_NOTIFYICONDATAW
;
9224 #define MYNOTIFYICONDATAW_V1_SIZE offsetof (MY_NOTIFYICONDATAW, szTip[64])
9225 #define MYNOTIFYICONDATAW_V2_SIZE offsetof (MY_NOTIFYICONDATAW, guidItem)
9226 #define MYNOTIFYICONDATAW_V3_SIZE offsetof (MY_NOTIFYICONDATAW, hBalloonIcon)
9228 # define NIF_INFO 0x00000010
9231 # define NIIF_NONE 0x00000000
9234 # define NIIF_INFO 0x00000001
9236 #ifndef NIIF_WARNING
9237 # define NIIF_WARNING 0x00000002
9240 # define NIIF_ERROR 0x00000003
9244 #define EMACS_TRAY_NOTIFICATION_ID 42 /* arbitrary */
9245 #define EMACS_NOTIFICATION_MSG (WM_APP + 1)
9254 /* Report the version of a DLL given by its name. The return value is
9255 constructed using MAKEDLLVERULL. */
9257 get_dll_version (const char *dll_name
)
9259 ULONGLONG version
= 0;
9260 HINSTANCE hdll
= LoadLibrary (dll_name
);
9264 DLLGETVERSIONPROC pDllGetVersion
9265 = (DLLGETVERSIONPROC
) GetProcAddress (hdll
, "DllGetVersion");
9272 memset (&dvi
, 0, sizeof(dvi
));
9273 dvi
.cbSize
= sizeof(dvi
);
9274 result
= pDllGetVersion (&dvi
);
9275 if (SUCCEEDED (result
))
9276 version
= MAKEDLLVERULL (dvi
.dwMajorVersion
, dvi
.dwMinorVersion
,
9285 /* Return the number of bytes in UTF-8 encoded string STR that
9286 corresponds to at most LIM characters. If STR ends before LIM
9287 characters, return the number of bytes in STR including the
9288 terminating null byte. */
9290 utf8_mbslen_lim (const char *str
, int lim
)
9292 const char *p
= str
;
9293 int mblen
= 0, nchars
= 0;
9295 while (*p
&& nchars
< lim
)
9297 int nbytes
= CHAR_BYTES (*p
);
9304 if (!*p
&& nchars
< lim
)
9310 /* Low-level subroutine to show tray notifications. All strings are
9311 supposed to be unibyte UTF-8 encoded by the caller. */
9313 add_tray_notification (struct frame
*f
, const char *icon
, const char *tip
,
9314 enum NI_Severity severity
, unsigned timeout
,
9315 const char *title
, const char *msg
)
9317 EMACS_INT retval
= EMACS_TRAY_NOTIFICATION_ID
;
9319 if (FRAME_W32_P (f
))
9321 MY_NOTIFYICONDATAW nidw
;
9322 ULONGLONG shell_dll_version
= get_dll_version ("Shell32.dll");
9323 wchar_t tipw
[128], msgw
[256], titlew
[64];
9326 memset (&nidw
, 0, sizeof(nidw
));
9328 /* MSDN says the full struct is supported since Vista, whose
9329 Shell32.dll version is said to be 6.0.6. But DllGetVersion
9330 cannot report the 3rd field value, it reports "build number"
9331 instead, which is something else. So we use the Windows 7's
9332 version 6.1 as cutoff, and Vista loses. (Actually, the loss
9333 is not a real one, since we don't expose the hBalloonIcon
9334 member of the struct to Lisp.) */
9335 if (shell_dll_version
>= MAKEDLLVERULL (6, 1, 0, 0)) /* >= Windows 7 */
9336 nidw
.cbSize
= sizeof (nidw
);
9337 else if (shell_dll_version
>= MAKEDLLVERULL (6, 0, 0, 0)) /* XP */
9338 nidw
.cbSize
= MYNOTIFYICONDATAW_V3_SIZE
;
9339 else if (shell_dll_version
>= MAKEDLLVERULL (5, 0, 0, 0)) /* W2K */
9340 nidw
.cbSize
= MYNOTIFYICONDATAW_V2_SIZE
;
9342 nidw
.cbSize
= MYNOTIFYICONDATAW_V1_SIZE
; /* < W2K */
9343 nidw
.hWnd
= FRAME_W32_WINDOW (f
);
9344 nidw
.uID
= EMACS_TRAY_NOTIFICATION_ID
;
9345 nidw
.uFlags
= NIF_MESSAGE
| NIF_ICON
| NIF_TIP
| NIF_INFO
;
9346 nidw
.uCallbackMessage
= EMACS_NOTIFICATION_MSG
;
9348 nidw
.hIcon
= LoadIcon (hinst
, EMACS_CLASS
);
9351 if (w32_unicode_filenames
)
9353 wchar_t icon_w
[MAX_PATH
];
9355 if (filename_to_utf16 (icon
, icon_w
) != 0)
9360 nidw
.hIcon
= LoadImageW (NULL
, icon_w
, IMAGE_ICON
, 0, 0,
9361 LR_DEFAULTSIZE
| LR_LOADFROMFILE
);
9365 char icon_a
[MAX_PATH
];
9367 if (filename_to_ansi (icon
, icon_a
) != 0)
9372 nidw
.hIcon
= LoadImageA (NULL
, icon_a
, IMAGE_ICON
, 0, 0,
9373 LR_DEFAULTSIZE
| LR_LOADFROMFILE
);
9378 switch (GetLastError ())
9380 case ERROR_FILE_NOT_FOUND
:
9390 /* Windows 9X and NT4 support only 64 characters in the Tip,
9391 later versions support up to 128. */
9392 if (nidw
.cbSize
== MYNOTIFYICONDATAW_V1_SIZE
)
9394 tiplen
= pMultiByteToWideChar (CP_UTF8
, multiByteToWideCharFlags
,
9395 tip
, utf8_mbslen_lim (tip
, 63),
9402 tiplen
= pMultiByteToWideChar (CP_UTF8
, multiByteToWideCharFlags
,
9403 tip
, utf8_mbslen_lim (tip
, 127),
9414 wcscpy (nidw
.szTip
, tipw
);
9416 /* The rest of the structure is only supported since Windows 2000. */
9417 if (nidw
.cbSize
> MYNOTIFYICONDATAW_V1_SIZE
)
9421 slen
= pMultiByteToWideChar (CP_UTF8
, multiByteToWideCharFlags
,
9422 msg
, utf8_mbslen_lim (msg
, 255),
9432 wcscpy (nidw
.szInfo
, msgw
);
9433 nidw
.uTimeout
= timeout
;
9434 slen
= pMultiByteToWideChar (CP_UTF8
, multiByteToWideCharFlags
,
9435 title
, utf8_mbslen_lim (title
, 63),
9445 wcscpy (nidw
.szInfoTitle
, titlew
);
9450 nidw
.dwInfoFlags
= NIIF_NONE
;
9454 nidw
.dwInfoFlags
= NIIF_INFO
;
9457 nidw
.dwInfoFlags
= NIIF_WARNING
;
9460 nidw
.dwInfoFlags
= NIIF_ERROR
;
9465 if (!Shell_NotifyIconW (NIM_ADD
, (PNOTIFYICONDATAW
)&nidw
))
9467 /* GetLastError returns meaningless results when
9468 Shell_NotifyIcon fails. */
9469 DebPrint (("Shell_NotifyIcon ADD failed (err=%d)\n",
9475 if (*icon
&& !DestroyIcon (nidw
.hIcon
))
9476 DebPrint (("DestroyIcon failed (err=%d)\n", GetLastError ()));
9481 /* Low-level subroutine to remove a tray notification. Note: we only
9482 pass the minimum data about the notification: its ID and the handle
9483 of the window to which it sends messages. MSDN doesn't say this is
9484 enough, but it works in practice. This allows us to avoid keeping
9485 the notification data around after we show the notification. */
9487 delete_tray_notification (struct frame
*f
, int id
)
9489 if (FRAME_W32_P (f
))
9491 MY_NOTIFYICONDATAW nidw
;
9493 memset (&nidw
, 0, sizeof(nidw
));
9494 nidw
.hWnd
= FRAME_W32_WINDOW (f
);
9497 if (!Shell_NotifyIconW (NIM_DELETE
, (PNOTIFYICONDATAW
)&nidw
))
9499 /* GetLastError returns meaningless results when
9500 Shell_NotifyIcon fails. */
9501 DebPrint (("Shell_NotifyIcon DELETE failed\n"));
9509 DEFUN ("w32-notification-notify",
9510 Fw32_notification_notify
, Sw32_notification_notify
,
9512 doc
: /* Display an MS-Windows tray notification as specified by PARAMS.
9514 Value is the integer unique ID of the notification that can be used
9515 to remove the notification using `w32-notification-close', which see.
9516 If the function fails, the return value is nil.
9518 Tray notifications, a.k.a. \"taskbar messages\", are messages that
9519 inform the user about events unrelated to the current user activity,
9520 such as a significant system event, by briefly displaying informative
9521 text in a balloon from an icon in the notification area of the taskbar.
9523 Parameters in PARAMS are specified as keyword/value pairs. All the
9524 parameters are optional, but if no parameters are specified, the
9525 function will do nothing and return nil.
9527 The following parameters are supported:
9529 :icon ICON -- Display ICON in the system tray. If ICON is a string,
9530 it should specify a file name from which to load the
9531 icon; the specified file should be a .ico Windows icon
9532 file. If ICON is not a string, or if this parameter
9533 is not specified, the standard Emacs icon will be used.
9535 :tip TIP -- Use TIP as the tooltip for the notification. If TIP
9536 is a string, this is the text of a tooltip that will
9537 be shown when the mouse pointer hovers over the tray
9538 icon added by the notification. If TIP is not a
9539 string, or if this parameter is not specified, the
9540 default tooltip text is \"Emacs notification\". The
9541 tooltip text can be up to 127 characters long (63
9542 on Windows versions before W2K). Longer strings
9545 :level LEVEL -- Notification severity level, one of `info',
9546 `warning', or `error'. If given, the value
9547 determines the icon displayed to the left of the
9548 notification title, but only if the `:title'
9549 parameter (see below) is also specified and is a
9552 :title TITLE -- The title of the notification. If TITLE is a string,
9553 it is displayed in a larger font immediately above
9554 the body text. The title text can be up to 63
9555 characters long; longer text will be truncated.
9557 :body BODY -- The body of the notification. If BODY is a string,
9558 it specifies the text of the notification message.
9559 Use embedded newlines to control how the text is
9560 broken into lines. The body text can be up to 255
9561 characters long, and will be truncated if it's longer.
9563 Note that versions of Windows before W2K support only `:icon' and `:tip'.
9564 You can pass the other parameters, but they will be ignored on those
9567 There can be at most one active notification at any given time. An
9568 active notification must be removed by calling `w32-notification-close'
9569 before a new one can be shown.
9571 usage: (w32-notification-notify &rest PARAMS) */)
9572 (ptrdiff_t nargs
, Lisp_Object
*args
)
9574 struct frame
*f
= SELECTED_FRAME ();
9575 Lisp_Object arg_plist
, lres
;
9577 char *icon
, *tip
, *title
, *msg
;
9578 enum NI_Severity severity
;
9584 arg_plist
= Flist (nargs
, args
);
9587 lres
= Fplist_get (arg_plist
, QCicon
);
9589 icon
= SSDATA (ENCODE_FILE (Fexpand_file_name (lres
, Qnil
)));
9594 lres
= Fplist_get (arg_plist
, QCtip
);
9596 tip
= SSDATA (code_convert_string_norecord (lres
, Qutf_8
, 1));
9598 tip
= "Emacs notification";
9601 lres
= Fplist_get (arg_plist
, QClevel
);
9604 else if (EQ (lres
, Qinfo
))
9606 else if (EQ (lres
, Qwarning
))
9608 else if (EQ (lres
, Qerror
))
9614 lres
= Fplist_get (arg_plist
, QCtitle
);
9616 title
= SSDATA (code_convert_string_norecord (lres
, Qutf_8
, 1));
9620 /* Notification body text. */
9621 lres
= Fplist_get (arg_plist
, QCbody
);
9623 msg
= SSDATA (code_convert_string_norecord (lres
, Qutf_8
, 1));
9628 retval
= add_tray_notification (f
, icon
, tip
, severity
, timeout
, title
, msg
);
9629 return (retval
< 0 ? Qnil
: make_number (retval
));
9632 DEFUN ("w32-notification-close",
9633 Fw32_notification_close
, Sw32_notification_close
,
9635 doc
: /* Remove the MS-Windows tray notification specified by its ID. */)
9638 struct frame
*f
= SELECTED_FRAME ();
9641 delete_tray_notification (f
, XINT (id
));
9646 #endif /* WINDOWSNT && !HAVE_DBUS */
9649 /***********************************************************************
9651 ***********************************************************************/
9653 /* Keep this list in the same order as frame_parms in frame.c.
9654 Use 0 for unsupported frame parameters. */
9656 frame_parm_handler w32_frame_parm_handlers
[] =
9660 x_set_background_color
,
9666 x_set_foreground_color
,
9669 x_set_internal_border_width
,
9670 x_set_right_divider_width
,
9671 x_set_bottom_divider_width
,
9672 x_set_menu_bar_lines
,
9674 x_explicitly_set_name
,
9675 x_set_scroll_bar_width
,
9676 x_set_scroll_bar_height
,
9679 x_set_vertical_scroll_bars
,
9680 x_set_horizontal_scroll_bars
,
9682 x_set_tool_bar_lines
,
9683 0, /* x_set_scroll_bar_foreground, */
9684 0, /* x_set_scroll_bar_background, */
9689 0, /* x_set_wait_for_wm, */
9693 0, /* x_set_sticky */
9694 0, /* x_set_tool_bar_position */
9698 syms_of_w32fns (void)
9700 globals_of_w32fns ();
9701 track_mouse_window
= NULL
;
9703 w32_visible_system_caret_hwnd
= NULL
;
9705 DEFSYM (Qundefined_color
, "undefined-color");
9706 DEFSYM (Qcancel_timer
, "cancel-timer");
9707 DEFSYM (Qhyper
, "hyper");
9708 DEFSYM (Qsuper
, "super");
9709 DEFSYM (Qmeta
, "meta");
9710 DEFSYM (Qalt
, "alt");
9711 DEFSYM (Qctrl
, "ctrl");
9712 DEFSYM (Qcontrol
, "control");
9713 DEFSYM (Qshift
, "shift");
9714 DEFSYM (Qfont_param
, "font-parameter");
9715 DEFSYM (Qgeometry
, "geometry");
9716 DEFSYM (Qworkarea
, "workarea");
9717 DEFSYM (Qmm_size
, "mm-size");
9718 DEFSYM (Qframes
, "frames");
9719 DEFSYM (Qtip_frame
, "tip-frame");
9720 DEFSYM (Qassq_delete_all
, "assq-delete-all");
9721 DEFSYM (Qunicode_sip
, "unicode-sip");
9722 #if defined WINDOWSNT && !defined HAVE_DBUS
9723 DEFSYM (QCicon
, ":icon");
9724 DEFSYM (QCtip
, ":tip");
9725 DEFSYM (QClevel
, ":level");
9726 DEFSYM (Qinfo
, "info");
9727 DEFSYM (Qwarning
, "warning");
9728 DEFSYM (QCtitle
, ":title");
9729 DEFSYM (QCbody
, ":body");
9732 /* Symbols used elsewhere, but only in MS-Windows-specific code. */
9733 DEFSYM (Qgnutls_dll
, "gnutls");
9734 DEFSYM (Qlibxml2_dll
, "libxml2");
9735 DEFSYM (Qserif
, "serif");
9736 DEFSYM (Qzlib_dll
, "zlib");
9738 Fput (Qundefined_color
, Qerror_conditions
,
9739 listn (CONSTYPE_PURE
, 2, Qundefined_color
, Qerror
));
9740 Fput (Qundefined_color
, Qerror_message
,
9741 build_pure_c_string ("Undefined color"));
9743 staticpro (&w32_grabbed_keys
);
9744 w32_grabbed_keys
= Qnil
;
9746 DEFVAR_LISP ("w32-color-map", Vw32_color_map
,
9747 doc
: /* An array of color name mappings for Windows. */);
9748 Vw32_color_map
= Qnil
;
9750 DEFVAR_LISP ("w32-pass-alt-to-system", Vw32_pass_alt_to_system
,
9751 doc
: /* Non-nil if Alt key presses are passed on to Windows.
9752 When non-nil, for example, Alt pressed and released and then space will
9753 open the System menu. When nil, Emacs processes the Alt key events, and
9754 then silently swallows them. */);
9755 Vw32_pass_alt_to_system
= Qnil
;
9757 DEFVAR_LISP ("w32-alt-is-meta", Vw32_alt_is_meta
,
9758 doc
: /* Non-nil if the Alt key is to be considered the same as the META key.
9759 When nil, Emacs will translate the Alt key to the ALT modifier, not to META. */);
9760 Vw32_alt_is_meta
= Qt
;
9762 DEFVAR_INT ("w32-quit-key", w32_quit_key
,
9763 doc
: /* If non-zero, the virtual key code for an alternative quit key. */);
9766 DEFVAR_LISP ("w32-pass-lwindow-to-system",
9767 Vw32_pass_lwindow_to_system
,
9768 doc
: /* If non-nil, the left \"Windows\" key is passed on to Windows.
9770 When non-nil, the Start menu is opened by tapping the key.
9771 If you set this to nil, the left \"Windows\" key is processed by Emacs
9772 according to the value of `w32-lwindow-modifier', which see.
9774 Note that some combinations of the left \"Windows\" key with other
9775 keys are caught by Windows at low level. For example, <lwindow>-r
9776 pops up the Windows Run dialog, <lwindow>-<Pause> pops up the "System
9777 Properties" dialog, etc. On Windows 10, no \"Windows\" key
9778 combinations are normally handed to applications. To enable Emacs to
9779 process \"Windows\" key combinations, use the function
9780 `w32-register-hot-key`.
9782 For Windows 98/ME, see the doc string of `w32-phantom-key-code'. */);
9783 Vw32_pass_lwindow_to_system
= Qt
;
9785 DEFVAR_LISP ("w32-pass-rwindow-to-system",
9786 Vw32_pass_rwindow_to_system
,
9787 doc
: /* If non-nil, the right \"Windows\" key is passed on to Windows.
9789 When non-nil, the Start menu is opened by tapping the key.
9790 If you set this to nil, the right \"Windows\" key is processed by Emacs
9791 according to the value of `w32-rwindow-modifier', which see.
9793 Note that some combinations of the right \"Windows\" key with other
9794 keys are caught by Windows at low level. For example, <rwindow>-r
9795 pops up the Windows Run dialog, <rwindow>-<Pause> pops up the "System
9796 Properties" dialog, etc. On Windows 10, no \"Windows\" key
9797 combinations are normally handed to applications. To enable Emacs to
9798 process \"Windows\" key combinations, use the function
9799 `w32-register-hot-key`.
9801 For Windows 98/ME, see the doc string of `w32-phantom-key-code'. */);
9802 Vw32_pass_rwindow_to_system
= Qt
;
9804 DEFVAR_LISP ("w32-phantom-key-code",
9805 Vw32_phantom_key_code
,
9806 doc
: /* Virtual key code used to generate \"phantom\" key presses.
9807 Value is a number between 0 and 255.
9809 Phantom key presses are generated in order to stop the system from
9810 acting on \"Windows\" key events when `w32-pass-lwindow-to-system' or
9811 `w32-pass-rwindow-to-system' is nil.
9813 This variable is only used on Windows 98 and ME. For other Windows
9814 versions, see the documentation of the `w32-register-hot-key`
9816 /* Although 255 is technically not a valid key code, it works and
9817 means that this hack won't interfere with any real key code. */
9818 XSETINT (Vw32_phantom_key_code
, 255);
9820 DEFVAR_LISP ("w32-enable-num-lock",
9821 Vw32_enable_num_lock
,
9822 doc
: /* If non-nil, the Num Lock key acts normally.
9823 Set to nil to handle Num Lock as the `kp-numlock' key. */);
9824 Vw32_enable_num_lock
= Qt
;
9826 DEFVAR_LISP ("w32-enable-caps-lock",
9827 Vw32_enable_caps_lock
,
9828 doc
: /* If non-nil, the Caps Lock key acts normally.
9829 Set to nil to handle Caps Lock as the `capslock' key. */);
9830 Vw32_enable_caps_lock
= Qt
;
9832 DEFVAR_LISP ("w32-scroll-lock-modifier",
9833 Vw32_scroll_lock_modifier
,
9834 doc
: /* Modifier to use for the Scroll Lock ON state.
9835 The value can be hyper, super, meta, alt, control or shift for the
9836 respective modifier, or nil to handle Scroll Lock as the `scroll' key.
9837 Any other value will cause the Scroll Lock key to be ignored. */);
9838 Vw32_scroll_lock_modifier
= Qnil
;
9840 DEFVAR_LISP ("w32-lwindow-modifier",
9841 Vw32_lwindow_modifier
,
9842 doc
: /* Modifier to use for the left \"Windows\" key.
9843 The value can be hyper, super, meta, alt, control or shift for the
9844 respective modifier, or nil to appear as the `lwindow' key.
9845 Any other value will cause the key to be ignored.
9847 Also see the documentation of the `w32-register-hot-key` function. */);
9848 Vw32_lwindow_modifier
= Qnil
;
9850 DEFVAR_LISP ("w32-rwindow-modifier",
9851 Vw32_rwindow_modifier
,
9852 doc
: /* Modifier to use for the right \"Windows\" key.
9853 The value can be hyper, super, meta, alt, control or shift for the
9854 respective modifier, or nil to appear as the `rwindow' key.
9855 Any other value will cause the key to be ignored.
9857 Also see the documentation of the `w32-register-hot-key` function. */);
9858 Vw32_rwindow_modifier
= Qnil
;
9860 DEFVAR_LISP ("w32-apps-modifier",
9862 doc
: /* Modifier to use for the \"Apps\" key.
9863 The value can be hyper, super, meta, alt, control or shift for the
9864 respective modifier, or nil to appear as the `apps' key.
9865 Any other value will cause the key to be ignored. */);
9866 Vw32_apps_modifier
= Qnil
;
9868 DEFVAR_BOOL ("w32-enable-synthesized-fonts", w32_enable_synthesized_fonts
,
9869 doc
: /* Non-nil enables selection of artificially italicized and bold fonts. */);
9870 w32_enable_synthesized_fonts
= 0;
9872 DEFVAR_LISP ("w32-enable-palette", Vw32_enable_palette
,
9873 doc
: /* Non-nil enables Windows palette management to map colors exactly. */);
9874 Vw32_enable_palette
= Qt
;
9876 DEFVAR_INT ("w32-mouse-button-tolerance",
9877 w32_mouse_button_tolerance
,
9878 doc
: /* Analogue of double click interval for faking middle mouse events.
9879 The value is the minimum time in milliseconds that must elapse between
9880 left and right button down events before they are considered distinct events.
9881 If both mouse buttons are depressed within this interval, a middle mouse
9882 button down event is generated instead. */);
9883 w32_mouse_button_tolerance
= GetDoubleClickTime () / 2;
9885 DEFVAR_INT ("w32-mouse-move-interval",
9886 w32_mouse_move_interval
,
9887 doc
: /* Minimum interval between mouse move events.
9888 The value is the minimum time in milliseconds that must elapse between
9889 successive mouse move (or scroll bar drag) events before they are
9890 reported as lisp events. */);
9891 w32_mouse_move_interval
= 0;
9893 DEFVAR_BOOL ("w32-pass-extra-mouse-buttons-to-system",
9894 w32_pass_extra_mouse_buttons_to_system
,
9895 doc
: /* If non-nil, the fourth and fifth mouse buttons are passed to Windows.
9896 Recent versions of Windows support mice with up to five buttons.
9897 Since most applications don't support these extra buttons, most mouse
9898 drivers will allow you to map them to functions at the system level.
9899 If this variable is non-nil, Emacs will pass them on, allowing the
9900 system to handle them. */);
9901 w32_pass_extra_mouse_buttons_to_system
= 0;
9903 DEFVAR_BOOL ("w32-pass-multimedia-buttons-to-system",
9904 w32_pass_multimedia_buttons_to_system
,
9905 doc
: /* If non-nil, media buttons are passed to Windows.
9906 Some modern keyboards contain buttons for controlling media players, web
9907 browsers and other applications. Generally these buttons are handled on a
9908 system wide basis, but by setting this to nil they are made available
9909 to Emacs for binding. Depending on your keyboard, additional keys that
9910 may be available are:
9912 browser-back, browser-forward, browser-refresh, browser-stop,
9913 browser-search, browser-favorites, browser-home,
9914 mail, mail-reply, mail-forward, mail-send,
9916 help, find, new, open, close, save, print, undo, redo, copy, cut, paste,
9917 spell-check, correction-list, toggle-dictate-command,
9918 media-next, media-previous, media-stop, media-play-pause, media-select,
9919 media-play, media-pause, media-record, media-fast-forward, media-rewind,
9920 media-channel-up, media-channel-down,
9921 volume-mute, volume-up, volume-down,
9922 mic-volume-mute, mic-volume-down, mic-volume-up, mic-toggle,
9923 bass-down, bass-boost, bass-up, treble-down, treble-up */);
9924 w32_pass_multimedia_buttons_to_system
= 1;
9926 #if 0 /* TODO: Mouse cursor customization. */
9927 DEFVAR_LISP ("x-pointer-shape", Vx_pointer_shape
,
9928 doc
: /* The shape of the pointer when over text.
9929 Changing the value does not affect existing frames
9930 unless you set the mouse color. */);
9931 Vx_pointer_shape
= Qnil
;
9933 Vx_nontext_pointer_shape
= Qnil
;
9935 Vx_mode_pointer_shape
= Qnil
;
9937 DEFVAR_LISP ("x-hourglass-pointer-shape", Vx_hourglass_pointer_shape
,
9938 doc
: /* The shape of the pointer when Emacs is busy.
9939 This variable takes effect when you create a new frame
9940 or when you set the mouse color. */);
9941 Vx_hourglass_pointer_shape
= Qnil
;
9943 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
9944 Vx_sensitive_text_pointer_shape
,
9945 doc
: /* The shape of the pointer when over mouse-sensitive text.
9946 This variable takes effect when you create a new frame
9947 or when you set the mouse color. */);
9948 Vx_sensitive_text_pointer_shape
= Qnil
;
9950 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
9951 Vx_window_horizontal_drag_shape
,
9952 doc
: /* Pointer shape to use for indicating a window can be dragged horizontally.
9953 This variable takes effect when you create a new frame
9954 or when you set the mouse color. */);
9955 Vx_window_horizontal_drag_shape
= Qnil
;
9957 DEFVAR_LISP ("x-window-vertical-drag-cursor",
9958 Vx_window_vertical_drag_shape
,
9959 doc
: /* Pointer shape to use for indicating a window can be dragged vertically.
9960 This variable takes effect when you create a new frame
9961 or when you set the mouse color. */);
9962 Vx_window_vertical_drag_shape
= Qnil
;
9965 DEFVAR_LISP ("x-cursor-fore-pixel", Vx_cursor_fore_pixel
,
9966 doc
: /* A string indicating the foreground color of the cursor box. */);
9967 Vx_cursor_fore_pixel
= Qnil
;
9969 DEFVAR_LISP ("x-max-tooltip-size", Vx_max_tooltip_size
,
9970 doc
: /* Maximum size for tooltips.
9971 Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
9972 Vx_max_tooltip_size
= Fcons (make_number (80), make_number (40));
9974 DEFVAR_LISP ("x-no-window-manager", Vx_no_window_manager
,
9975 doc
: /* Non-nil if no window manager is in use.
9976 Emacs doesn't try to figure this out; this is always nil
9977 unless you set it to something else. */);
9978 /* We don't have any way to find this out, so set it to nil
9979 and maybe the user would like to set it to t. */
9980 Vx_no_window_manager
= Qnil
;
9982 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
9983 Vx_pixel_size_width_font_regexp
,
9984 doc
: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
9986 Since Emacs gets width of a font matching with this regexp from
9987 PIXEL_SIZE field of the name, font finding mechanism gets faster for
9988 such a font. This is especially effective for such large fonts as
9989 Chinese, Japanese, and Korean. */);
9990 Vx_pixel_size_width_font_regexp
= Qnil
;
9992 DEFVAR_LISP ("w32-bdf-filename-alist",
9993 Vw32_bdf_filename_alist
,
9994 doc
: /* List of bdf fonts and their corresponding filenames. */);
9995 Vw32_bdf_filename_alist
= Qnil
;
9997 DEFVAR_BOOL ("w32-strict-fontnames",
9998 w32_strict_fontnames
,
9999 doc
: /* Non-nil means only use fonts that are exact matches for those requested.
10000 Default is nil, which allows old fontnames that are not XLFD compliant,
10001 and allows third-party CJK display to work by specifying false charset
10002 fields to trick Emacs into translating to Big5, SJIS etc.
10003 Setting this to t will prevent wrong fonts being selected when
10004 fontsets are automatically created. */);
10005 w32_strict_fontnames
= 0;
10007 DEFVAR_BOOL ("w32-strict-painting",
10008 w32_strict_painting
,
10009 doc
: /* Non-nil means use strict rules for repainting frames.
10010 Set this to nil to get the old behavior for repainting; this should
10011 only be necessary if the default setting causes problems. */);
10012 w32_strict_painting
= 1;
10014 DEFVAR_BOOL ("w32-use-fallback-wm-chars-method",
10015 w32_use_fallback_wm_chars_method
,
10016 doc
: /* Non-nil means use old method of processing character keys.
10017 This is intended only for debugging of the new processing method.
10020 This variable has effect only on NT family of systems, not on Windows 9X. */);
10021 w32_use_fallback_wm_chars_method
= 0;
10023 DEFVAR_BOOL ("w32-disable-new-uniscribe-apis",
10024 w32_disable_new_uniscribe_apis
,
10025 doc
: /* Non-nil means don't use new Uniscribe APIs.
10026 The new APIs are used to access OTF features supported by fonts.
10027 This is intended only for debugging of the new Uniscribe-related code.
10030 This variable has effect only on Windows Vista and later. */);
10031 w32_disable_new_uniscribe_apis
= 0;
10033 DEFVAR_LISP ("w32-tooltip-extra-pixels",
10034 Vw32_tooltip_extra_pixels
,
10035 doc
: /* Number of pixels added after tooltip text.
10036 On Windows some fonts may cause the last character of a tooltip be
10037 truncated or wrapped around to the next line. Adding some extra space
10038 at the end of the toooltip works around this problem.
10040 This variable specifies the number of pixels that shall be added. The
10041 default value t means to add the width of one canonical character of the
10043 Vw32_tooltip_extra_pixels
= Qt
;
10045 #if 0 /* TODO: Port to W32 */
10046 defsubr (&Sx_change_window_property
);
10047 defsubr (&Sx_delete_window_property
);
10048 defsubr (&Sx_window_property
);
10050 defsubr (&Sxw_display_color_p
);
10051 defsubr (&Sx_display_grayscale_p
);
10052 defsubr (&Sxw_color_defined_p
);
10053 defsubr (&Sxw_color_values
);
10054 defsubr (&Sx_server_max_request_size
);
10055 defsubr (&Sx_server_vendor
);
10056 defsubr (&Sx_server_version
);
10057 defsubr (&Sx_display_pixel_width
);
10058 defsubr (&Sx_display_pixel_height
);
10059 defsubr (&Sx_display_mm_width
);
10060 defsubr (&Sx_display_mm_height
);
10061 defsubr (&Sx_display_screens
);
10062 defsubr (&Sx_display_planes
);
10063 defsubr (&Sx_display_color_cells
);
10064 defsubr (&Sx_display_visual_class
);
10065 defsubr (&Sx_display_backing_store
);
10066 defsubr (&Sx_display_save_under
);
10067 defsubr (&Sx_create_frame
);
10068 defsubr (&Sx_open_connection
);
10069 defsubr (&Sx_close_connection
);
10070 defsubr (&Sx_display_list
);
10071 defsubr (&Sw32_frame_geometry
);
10072 defsubr (&Sw32_frame_edges
);
10073 defsubr (&Sw32_mouse_absolute_pixel_position
);
10074 defsubr (&Sw32_set_mouse_absolute_pixel_position
);
10075 defsubr (&Sx_synchronize
);
10077 /* W32 specific functions */
10079 defsubr (&Sw32_define_rgb_color
);
10080 defsubr (&Sw32_default_color_map
);
10081 defsubr (&Sw32_display_monitor_attributes_list
);
10082 defsubr (&Sw32_send_sys_command
);
10083 defsubr (&Sw32_shell_execute
);
10084 defsubr (&Sw32_register_hot_key
);
10085 defsubr (&Sw32_unregister_hot_key
);
10086 defsubr (&Sw32_registered_hot_keys
);
10087 defsubr (&Sw32_reconstruct_hot_key
);
10088 defsubr (&Sw32_toggle_lock_key
);
10089 defsubr (&Sw32_window_exists_p
);
10090 defsubr (&Sw32_battery_status
);
10091 defsubr (&Sw32__menu_bar_in_use
);
10092 #if defined WINDOWSNT && !defined HAVE_DBUS
10093 defsubr (&Sw32_notification_notify
);
10094 defsubr (&Sw32_notification_close
);
10098 defsubr (&Sfile_system_info
);
10099 defsubr (&Sdefault_printer_name
);
10102 defsubr (&Sset_message_beep
);
10103 defsubr (&Sx_show_tip
);
10104 defsubr (&Sx_hide_tip
);
10106 staticpro (&tip_timer
);
10108 staticpro (&tip_frame
);
10110 last_show_tip_args
= Qnil
;
10111 staticpro (&last_show_tip_args
);
10113 defsubr (&Sx_file_dialog
);
10115 defsubr (&Ssystem_move_file_to_trash
);
10121 /* Crashing and reporting backtrace. */
10124 static LONG CALLBACK
my_exception_handler (EXCEPTION_POINTERS
*);
10125 static LPTOP_LEVEL_EXCEPTION_FILTER prev_exception_handler
;
10127 static DWORD except_code
;
10128 static PVOID except_addr
;
10132 /* Stack overflow recovery. */
10134 /* MinGW headers don't declare this (should be in malloc.h). Also,
10135 the function is not present pre-W2K, so make the call through
10136 a function pointer. */
10137 typedef int (__cdecl
*_resetstkoflw_proc
) (void);
10138 static _resetstkoflw_proc resetstkoflw
;
10140 /* Re-establish the guard page at stack limit. This is needed because
10141 when a stack overflow is detected, Windows removes the guard bit
10142 from the guard page, so if we don't re-establish that protection,
10143 the next stack overflow will cause a crash. */
10145 w32_reset_stack_overflow_guard (void)
10147 if (resetstkoflw
== NULL
)
10149 (_resetstkoflw_proc
)GetProcAddress (GetModuleHandle ("msvcrt.dll"),
10151 /* We ignore the return value. If _resetstkoflw fails, the next
10152 stack overflow will crash the program. */
10153 if (resetstkoflw
!= NULL
)
10154 (void)resetstkoflw ();
10158 stack_overflow_handler (void)
10160 /* Hard GC error may lead to stack overflow caused by
10161 too nested calls to mark_object. No way to survive. */
10162 if (gc_in_progress
)
10163 terminate_due_to_signal (SIGSEGV
, 40);
10165 /* See ms-w32.h: MinGW64's longjmp crashes if invoked in this context. */
10166 __builtin_longjmp (return_to_command_loop
, 1);
10168 sys_longjmp (return_to_command_loop
, 1);
10172 /* This handler records the exception code and the address where it
10173 was triggered so that this info could be included in the backtrace.
10174 Without that, the backtrace in some cases has no information
10175 whatsoever about the offending code, and looks as if the top-level
10176 exception handler in the MinGW startup code was the one that
10177 crashed. We also recover from stack overflow, by calling our stack
10178 overflow handler that jumps back to top level. */
10179 static LONG CALLBACK
10180 my_exception_handler (EXCEPTION_POINTERS
* exception_data
)
10182 except_code
= exception_data
->ExceptionRecord
->ExceptionCode
;
10183 except_addr
= exception_data
->ExceptionRecord
->ExceptionAddress
;
10185 /* If this is a stack overflow exception, attempt to recover. */
10186 if (exception_data
->ExceptionRecord
->ExceptionCode
== EXCEPTION_STACK_OVERFLOW
10187 && exception_data
->ExceptionRecord
->NumberParameters
== 2
10188 /* We can only longjmp to top level from the main thread. */
10189 && GetCurrentThreadId () == dwMainThreadId
)
10191 /* Call stack_overflow_handler (). */
10193 exception_data
->ContextRecord
->Rip
= (DWORD_PTR
) &stack_overflow_handler
;
10195 exception_data
->ContextRecord
->Eip
= (DWORD_PTR
) &stack_overflow_handler
;
10197 /* Zero this out, so the stale address of the stack overflow
10198 exception we handled is not displayed in some future
10199 unrelated crash. */
10201 return EXCEPTION_CONTINUE_EXECUTION
;
10204 if (prev_exception_handler
)
10205 return prev_exception_handler (exception_data
);
10206 return EXCEPTION_EXECUTE_HANDLER
;
10210 typedef USHORT (WINAPI
* CaptureStackBackTrace_proc
) (ULONG
, ULONG
, PVOID
*,
10213 #define BACKTRACE_LIMIT_MAX 62
10216 w32_backtrace (void **buffer
, int limit
)
10218 static CaptureStackBackTrace_proc s_pfn_CaptureStackBackTrace
= NULL
;
10219 HMODULE hm_kernel32
= NULL
;
10221 if (!s_pfn_CaptureStackBackTrace
)
10223 hm_kernel32
= LoadLibrary ("Kernel32.dll");
10224 s_pfn_CaptureStackBackTrace
=
10225 (CaptureStackBackTrace_proc
) GetProcAddress (hm_kernel32
,
10226 "RtlCaptureStackBackTrace");
10228 if (s_pfn_CaptureStackBackTrace
)
10229 return s_pfn_CaptureStackBackTrace (0, min (BACKTRACE_LIMIT_MAX
, limit
),
10238 button
= MessageBox (NULL
,
10239 "A fatal error has occurred!\n\n"
10240 "Would you like to attach a debugger?\n\n"
10242 "YES -- to debug Emacs, or\n"
10243 "NO -- to abort Emacs and produce a backtrace\n"
10244 " (emacs_backtrace.txt in current directory)."
10246 "\n\n(type \"gdb -p <emacs-PID>\" and\n"
10247 "\"continue\" inside GDB before clicking YES.)"
10249 , "Emacs Abort Dialog",
10250 MB_ICONEXCLAMATION
| MB_TASKMODAL
10251 | MB_SETFOREGROUND
| MB_YESNO
);
10256 exit (2); /* tell the compiler we will never return */
10260 void *stack
[BACKTRACE_LIMIT_MAX
+ 1];
10261 int i
= w32_backtrace (stack
, BACKTRACE_LIMIT_MAX
+ 1);
10265 int errfile_fd
= -1;
10267 char buf
[sizeof ("\r\nException at this address:\r\n\r\n")
10268 /* The type below should really be 'void *', but
10269 INT_BUFSIZE_BOUND cannot handle that without
10270 triggering compiler warnings (under certain
10271 pedantic warning switches), it wants an
10273 + 2 * INT_BUFSIZE_BOUND (intptr_t)];
10277 HANDLE errout
= GetStdHandle (STD_ERROR_HANDLE
);
10278 int stderr_fd
= -1;
10280 if (errout
&& errout
!= INVALID_HANDLE_VALUE
)
10281 stderr_fd
= _open_osfhandle ((intptr_t)errout
, O_APPEND
| O_BINARY
);
10284 /* We use %p, not 0x%p, as %p produces a leading "0x" on XP,
10285 but not on Windows 7. addr2line doesn't mind a missing
10286 "0x", but will be confused by an extra one. */
10288 sprintf (buf
, "\r\nException 0x%lx at this address:\r\n%p\r\n",
10289 except_code
, except_addr
);
10290 if (stderr_fd
>= 0)
10293 write (stderr_fd
, buf
, strlen (buf
));
10294 write (stderr_fd
, "\r\nBacktrace:\r\n", 14);
10299 errfile_fd
= _open ("emacs_backtrace.txt", O_RDWR
| O_CREAT
| O_BINARY
, S_IREAD
| S_IWRITE
);
10300 if (errfile_fd
>= 0)
10302 lseek (errfile_fd
, 0L, SEEK_END
);
10304 write (errfile_fd
, buf
, strlen (buf
));
10305 write (errfile_fd
, "\r\nBacktrace:\r\n", 14);
10308 for (j
= 0; j
< i
; j
++)
10310 /* stack[] gives the return addresses, whereas we want
10311 the address of the call, so decrease each address
10312 by approximate size of 1 CALL instruction. */
10313 sprintf (buf
, "%p\r\n", (char *)stack
[j
] - sizeof(void *));
10314 if (stderr_fd
>= 0)
10315 write (stderr_fd
, buf
, strlen (buf
));
10316 if (errfile_fd
>= 0)
10317 write (errfile_fd
, buf
, strlen (buf
));
10319 if (i
== BACKTRACE_LIMIT_MAX
)
10321 if (stderr_fd
>= 0)
10322 write (stderr_fd
, "...\r\n", 5);
10323 if (errfile_fd
>= 0)
10324 write (errfile_fd
, "...\r\n", 5);
10326 if (errfile_fd
>= 0)
10327 close (errfile_fd
);
10337 /* Initialization. */
10340 globals_of_w32fns is used to initialize those global variables that
10341 must always be initialized on startup even when the global variable
10342 initialized is non zero (see the function main in emacs.c).
10343 globals_of_w32fns is called from syms_of_w32fns when the global
10344 variable initialized is 0 and directly from main when initialized
10348 globals_of_w32fns (void)
10350 HMODULE user32_lib
= GetModuleHandle ("user32.dll");
10352 TrackMouseEvent not available in all versions of Windows, so must load
10353 it dynamically. Do it once, here, instead of every time it is used.
10355 track_mouse_event_fn
= (TrackMouseEvent_Proc
)
10356 GetProcAddress (user32_lib
, "TrackMouseEvent");
10358 monitor_from_point_fn
= (MonitorFromPoint_Proc
)
10359 GetProcAddress (user32_lib
, "MonitorFromPoint");
10360 get_monitor_info_fn
= (GetMonitorInfo_Proc
)
10361 GetProcAddress (user32_lib
, "GetMonitorInfoA");
10362 monitor_from_window_fn
= (MonitorFromWindow_Proc
)
10363 GetProcAddress (user32_lib
, "MonitorFromWindow");
10364 enum_display_monitors_fn
= (EnumDisplayMonitors_Proc
)
10365 GetProcAddress (user32_lib
, "EnumDisplayMonitors");
10366 get_title_bar_info_fn
= (GetTitleBarInfo_Proc
)
10367 GetProcAddress (user32_lib
, "GetTitleBarInfo");
10370 HMODULE imm32_lib
= GetModuleHandle ("imm32.dll");
10371 get_composition_string_fn
= (ImmGetCompositionString_Proc
)
10372 GetProcAddress (imm32_lib
, "ImmGetCompositionStringW");
10373 get_ime_context_fn
= (ImmGetContext_Proc
)
10374 GetProcAddress (imm32_lib
, "ImmGetContext");
10375 release_ime_context_fn
= (ImmReleaseContext_Proc
)
10376 GetProcAddress (imm32_lib
, "ImmReleaseContext");
10377 set_ime_composition_window_fn
= (ImmSetCompositionWindow_Proc
)
10378 GetProcAddress (imm32_lib
, "ImmSetCompositionWindow");
10384 prev_exception_handler
= SetUnhandledExceptionFilter (my_exception_handler
);
10385 resetstkoflw
= NULL
;
10388 DEFVAR_INT ("w32-ansi-code-page",
10389 w32_ansi_code_page
,
10390 doc
: /* The ANSI code page used by the system. */);
10391 w32_ansi_code_page
= GetACP ();
10393 if (os_subtype
== OS_NT
)
10394 w32_unicode_gui
= 1;
10396 w32_unicode_gui
= 0;
10398 after_deadkey
= -1;
10400 /* MessageBox does not work without this when linked to comctl32.dll 6.0. */
10401 InitCommonControls ();
10403 syms_of_w32uniscribe ();
10406 #ifdef NTGUI_UNICODE
10409 ntgui_encode_system (Lisp_Object str
)
10411 Lisp_Object encoded
;
10412 to_unicode (str
, &encoded
);
10416 #endif /* NTGUI_UNICODE */