* lisp/facemenu.el (list-colors-sort): Add option "Luminance".
[emacs.git] / src / w32fns.c
blobb857e45047627923ea5f11e043012607d9b86f8f
1 /* Graphical user interface functions for the Microsoft Windows API.
3 Copyright (C) 1989, 1992-2012 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 */
22 #include <config.h>
24 #include <signal.h>
25 #include <stdio.h>
26 #include <limits.h>
27 #include <errno.h>
28 #include <math.h>
30 #include "lisp.h"
31 #include "w32term.h"
32 #include "frame.h"
33 #include "window.h"
34 #include "character.h"
35 #include "buffer.h"
36 #include "intervals.h"
37 #include "dispextern.h"
38 #include "keyboard.h"
39 #include "blockinput.h"
40 #include "epaths.h"
41 #include "charset.h"
42 #include "coding.h"
43 #include "ccl.h"
44 #include "fontset.h"
45 #include "systime.h"
46 #include "termhooks.h"
47 #include "w32heap.h"
48 #include "w32.h"
50 #include "bitmaps/gray.xbm"
52 #include <commctrl.h>
53 #include <commdlg.h>
54 #include <shellapi.h>
55 #include <ctype.h>
56 #include <winspool.h>
57 #include <objbase.h>
59 #include <dlgs.h>
60 #include <imm.h>
61 #define FILE_NAME_TEXT_FIELD edt1
62 #define FILE_NAME_COMBO_BOX cmb13
63 #define FILE_NAME_LIST lst1
65 #include "font.h"
66 #include "w32font.h"
68 #ifndef FOF_NO_CONNECTED_ELEMENTS
69 #define FOF_NO_CONNECTED_ELEMENTS 0x2000
70 #endif
72 void syms_of_w32fns (void);
73 void globals_of_w32fns (void);
75 extern void free_frame_menubar (struct frame *);
76 extern double atof (const char *);
77 extern int w32_console_toggle_lock_key (int, Lisp_Object);
78 extern void w32_menu_display_help (HWND, HMENU, UINT, UINT);
79 extern void w32_free_menu_strings (HWND);
80 extern const char *map_w32_filename (const char *, const char **);
82 /* If non-NULL, a handle to a frame where to display the hourglass cursor. */
83 static HWND hourglass_hwnd = NULL;
85 #ifndef IDC_HAND
86 #define IDC_HAND MAKEINTRESOURCE(32649)
87 #endif
89 /* Nonzero if using Windows. */
91 static int w32_in_use;
93 Lisp_Object Qsuppress_icon;
94 Lisp_Object Qundefined_color;
95 Lisp_Object Qcancel_timer;
96 Lisp_Object Qfont_param;
97 Lisp_Object Qhyper;
98 Lisp_Object Qsuper;
99 Lisp_Object Qmeta;
100 Lisp_Object Qalt;
101 Lisp_Object Qctrl;
102 Lisp_Object Qcontrol;
103 Lisp_Object Qshift;
106 /* Prefix for system colors. */
107 #define SYSTEM_COLOR_PREFIX "System"
108 #define SYSTEM_COLOR_PREFIX_LEN (sizeof (SYSTEM_COLOR_PREFIX) - 1)
110 /* State variables for emulating a three button mouse. */
111 #define LMOUSE 1
112 #define MMOUSE 2
113 #define RMOUSE 4
115 static int button_state = 0;
116 static W32Msg saved_mouse_button_msg;
117 static unsigned mouse_button_timer = 0; /* non-zero when timer is active */
118 static W32Msg saved_mouse_move_msg;
119 static unsigned mouse_move_timer = 0;
121 /* Window that is tracking the mouse. */
122 static HWND track_mouse_window;
124 /* Multi-monitor API definitions that are not pulled from the headers
125 since we are compiling for NT 4. */
126 #ifndef MONITOR_DEFAULT_TO_NEAREST
127 #define MONITOR_DEFAULT_TO_NEAREST 2
128 #endif
129 /* MinGW headers define MONITORINFO unconditionally, but MSVC ones don't.
130 To avoid a compile error on one or the other, redefine with a new name. */
131 struct MONITOR_INFO
133 DWORD cbSize;
134 RECT rcMonitor;
135 RECT rcWork;
136 DWORD dwFlags;
139 /* Reportedly, MSVC does not have this in its headers. */
140 #ifdef _MSC_VER
141 DECLARE_HANDLE(HMONITOR);
142 #endif
144 typedef BOOL (WINAPI * TrackMouseEvent_Proc)
145 (IN OUT LPTRACKMOUSEEVENT lpEventTrack);
146 typedef LONG (WINAPI * ImmGetCompositionString_Proc)
147 (IN HIMC context, IN DWORD index, OUT LPVOID buffer, IN DWORD bufLen);
148 typedef HIMC (WINAPI * ImmGetContext_Proc) (IN HWND window);
149 typedef HWND (WINAPI * ImmReleaseContext_Proc) (IN HWND wnd, IN HIMC context);
150 typedef HWND (WINAPI * ImmSetCompositionWindow_Proc) (IN HIMC context,
151 IN COMPOSITIONFORM *form);
152 typedef HMONITOR (WINAPI * MonitorFromPoint_Proc) (IN POINT pt, IN DWORD flags);
153 typedef BOOL (WINAPI * GetMonitorInfo_Proc)
154 (IN HMONITOR monitor, OUT struct MONITOR_INFO* info);
156 TrackMouseEvent_Proc track_mouse_event_fn = NULL;
157 ImmGetCompositionString_Proc get_composition_string_fn = NULL;
158 ImmGetContext_Proc get_ime_context_fn = NULL;
159 ImmReleaseContext_Proc release_ime_context_fn = NULL;
160 ImmSetCompositionWindow_Proc set_ime_composition_window_fn = NULL;
161 MonitorFromPoint_Proc monitor_from_point_fn = NULL;
162 GetMonitorInfo_Proc get_monitor_info_fn = NULL;
164 extern AppendMenuW_Proc unicode_append_menu;
166 /* Flag to selectively ignore WM_IME_CHAR messages. */
167 static int ignore_ime_char = 0;
169 /* W95 mousewheel handler */
170 unsigned int msh_mousewheel = 0;
172 /* Timers */
173 #define MOUSE_BUTTON_ID 1
174 #define MOUSE_MOVE_ID 2
175 #define MENU_FREE_ID 3
176 /* The delay (milliseconds) before a menu is freed after WM_EXITMENULOOP
177 is received. */
178 #define MENU_FREE_DELAY 1000
179 static unsigned menu_free_timer = 0;
181 #ifdef GLYPH_DEBUG
182 static int image_cache_refcount, dpyinfo_refcount;
183 #endif
185 static HWND w32_visible_system_caret_hwnd;
187 static int w32_unicode_gui;
189 /* From w32menu.c */
190 extern HMENU current_popup_menu;
191 static int menubar_in_use = 0;
193 /* From w32uniscribe.c */
194 extern void syms_of_w32uniscribe (void);
195 extern int uniscribe_available;
197 /* Function prototypes for hourglass support. */
198 static void w32_show_hourglass (struct frame *);
199 static void w32_hide_hourglass (void);
203 /* Error if we are not connected to MS-Windows. */
204 void
205 check_w32 (void)
207 if (! w32_in_use)
208 error ("MS-Windows not in use or not initialized");
211 /* Nonzero if we can use mouse menus.
212 You should not call this unless HAVE_MENUS is defined. */
215 have_menus_p (void)
217 return w32_in_use;
220 /* Extract a frame as a FRAME_PTR, defaulting to the selected frame
221 and checking validity for W32. */
223 FRAME_PTR
224 check_x_frame (Lisp_Object frame)
226 FRAME_PTR f;
228 if (NILP (frame))
229 frame = selected_frame;
230 CHECK_LIVE_FRAME (frame);
231 f = XFRAME (frame);
232 if (! FRAME_W32_P (f))
233 error ("Non-W32 frame used");
234 return f;
237 /* Let the user specify a display with a frame.
238 nil stands for the selected frame--or, if that is not a w32 frame,
239 the first display on the list. */
241 struct w32_display_info *
242 check_x_display_info (Lisp_Object frame)
244 if (NILP (frame))
246 struct frame *sf = XFRAME (selected_frame);
248 if (FRAME_W32_P (sf) && FRAME_LIVE_P (sf))
249 return FRAME_W32_DISPLAY_INFO (sf);
250 else
251 return &one_w32_display_info;
253 else if (STRINGP (frame))
254 return x_display_info_for_name (frame);
255 else
257 FRAME_PTR f;
259 CHECK_LIVE_FRAME (frame);
260 f = XFRAME (frame);
261 if (! FRAME_W32_P (f))
262 error ("Non-W32 frame used");
263 return FRAME_W32_DISPLAY_INFO (f);
267 /* Return the Emacs frame-object corresponding to an w32 window.
268 It could be the frame's main window or an icon window. */
270 /* This function can be called during GC, so use GC_xxx type test macros. */
272 struct frame *
273 x_window_to_frame (struct w32_display_info *dpyinfo, HWND wdesc)
275 Lisp_Object tail, frame;
276 struct frame *f;
278 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
280 frame = XCAR (tail);
281 if (!FRAMEP (frame))
282 continue;
283 f = XFRAME (frame);
284 if (!FRAME_W32_P (f) || FRAME_W32_DISPLAY_INFO (f) != dpyinfo)
285 continue;
287 if (FRAME_W32_WINDOW (f) == wdesc)
288 return f;
290 return 0;
294 static Lisp_Object unwind_create_frame (Lisp_Object);
295 static Lisp_Object unwind_create_tip_frame (Lisp_Object);
296 static void my_create_window (struct frame *);
297 static void my_create_tip_window (struct frame *);
299 /* TODO: Native Input Method support; see x_create_im. */
300 void x_set_foreground_color (struct frame *, Lisp_Object, Lisp_Object);
301 void x_set_background_color (struct frame *, Lisp_Object, Lisp_Object);
302 void x_set_mouse_color (struct frame *, Lisp_Object, Lisp_Object);
303 void x_set_cursor_color (struct frame *, Lisp_Object, Lisp_Object);
304 void x_set_border_color (struct frame *, Lisp_Object, Lisp_Object);
305 void x_set_cursor_type (struct frame *, Lisp_Object, Lisp_Object);
306 void x_set_icon_type (struct frame *, Lisp_Object, Lisp_Object);
307 void x_set_icon_name (struct frame *, Lisp_Object, Lisp_Object);
308 void x_explicitly_set_name (struct frame *, Lisp_Object, Lisp_Object);
309 void x_set_menu_bar_lines (struct frame *, Lisp_Object, Lisp_Object);
310 void x_set_title (struct frame *, Lisp_Object, Lisp_Object);
311 void x_set_tool_bar_lines (struct frame *, Lisp_Object, Lisp_Object);
316 /* Store the screen positions of frame F into XPTR and YPTR.
317 These are the positions of the containing window manager window,
318 not Emacs's own window. */
320 void
321 x_real_positions (FRAME_PTR f, int *xptr, int *yptr)
323 POINT pt;
324 RECT rect;
326 /* Get the bounds of the WM window. */
327 GetWindowRect (FRAME_W32_WINDOW (f), &rect);
329 pt.x = 0;
330 pt.y = 0;
332 /* Convert (0, 0) in the client area to screen co-ordinates. */
333 ClientToScreen (FRAME_W32_WINDOW (f), &pt);
335 /* Remember x_pixels_diff and y_pixels_diff. */
336 f->x_pixels_diff = pt.x - rect.left;
337 f->y_pixels_diff = pt.y - rect.top;
339 *xptr = rect.left;
340 *yptr = rect.top;
345 DEFUN ("w32-define-rgb-color", Fw32_define_rgb_color,
346 Sw32_define_rgb_color, 4, 4, 0,
347 doc: /* Convert RGB numbers to a Windows color reference and associate with NAME.
348 This adds or updates a named color to `w32-color-map', making it
349 available for use. The original entry's RGB ref is returned, or nil
350 if the entry is new. */)
351 (Lisp_Object red, Lisp_Object green, Lisp_Object blue, Lisp_Object name)
353 Lisp_Object rgb;
354 Lisp_Object oldrgb = Qnil;
355 Lisp_Object entry;
357 CHECK_NUMBER (red);
358 CHECK_NUMBER (green);
359 CHECK_NUMBER (blue);
360 CHECK_STRING (name);
362 XSETINT (rgb, RGB (XUINT (red), XUINT (green), XUINT (blue)));
364 block_input ();
366 /* replace existing entry in w32-color-map or add new entry. */
367 entry = Fassoc (name, Vw32_color_map);
368 if (NILP (entry))
370 entry = Fcons (name, rgb);
371 Vw32_color_map = Fcons (entry, Vw32_color_map);
373 else
375 oldrgb = Fcdr (entry);
376 Fsetcdr (entry, rgb);
379 unblock_input ();
381 return (oldrgb);
384 /* The default colors for the w32 color map */
385 typedef struct colormap_t
387 char *name;
388 COLORREF colorref;
389 } colormap_t;
391 colormap_t w32_color_map[] =
393 {"snow" , PALETTERGB (255,250,250)},
394 {"ghost white" , PALETTERGB (248,248,255)},
395 {"GhostWhite" , PALETTERGB (248,248,255)},
396 {"white smoke" , PALETTERGB (245,245,245)},
397 {"WhiteSmoke" , PALETTERGB (245,245,245)},
398 {"gainsboro" , PALETTERGB (220,220,220)},
399 {"floral white" , PALETTERGB (255,250,240)},
400 {"FloralWhite" , PALETTERGB (255,250,240)},
401 {"old lace" , PALETTERGB (253,245,230)},
402 {"OldLace" , PALETTERGB (253,245,230)},
403 {"linen" , PALETTERGB (250,240,230)},
404 {"antique white" , PALETTERGB (250,235,215)},
405 {"AntiqueWhite" , PALETTERGB (250,235,215)},
406 {"papaya whip" , PALETTERGB (255,239,213)},
407 {"PapayaWhip" , PALETTERGB (255,239,213)},
408 {"blanched almond" , PALETTERGB (255,235,205)},
409 {"BlanchedAlmond" , PALETTERGB (255,235,205)},
410 {"bisque" , PALETTERGB (255,228,196)},
411 {"peach puff" , PALETTERGB (255,218,185)},
412 {"PeachPuff" , PALETTERGB (255,218,185)},
413 {"navajo white" , PALETTERGB (255,222,173)},
414 {"NavajoWhite" , PALETTERGB (255,222,173)},
415 {"moccasin" , PALETTERGB (255,228,181)},
416 {"cornsilk" , PALETTERGB (255,248,220)},
417 {"ivory" , PALETTERGB (255,255,240)},
418 {"lemon chiffon" , PALETTERGB (255,250,205)},
419 {"LemonChiffon" , PALETTERGB (255,250,205)},
420 {"seashell" , PALETTERGB (255,245,238)},
421 {"honeydew" , PALETTERGB (240,255,240)},
422 {"mint cream" , PALETTERGB (245,255,250)},
423 {"MintCream" , PALETTERGB (245,255,250)},
424 {"azure" , PALETTERGB (240,255,255)},
425 {"alice blue" , PALETTERGB (240,248,255)},
426 {"AliceBlue" , PALETTERGB (240,248,255)},
427 {"lavender" , PALETTERGB (230,230,250)},
428 {"lavender blush" , PALETTERGB (255,240,245)},
429 {"LavenderBlush" , PALETTERGB (255,240,245)},
430 {"misty rose" , PALETTERGB (255,228,225)},
431 {"MistyRose" , PALETTERGB (255,228,225)},
432 {"white" , PALETTERGB (255,255,255)},
433 {"black" , PALETTERGB ( 0, 0, 0)},
434 {"dark slate gray" , PALETTERGB ( 47, 79, 79)},
435 {"DarkSlateGray" , PALETTERGB ( 47, 79, 79)},
436 {"dark slate grey" , PALETTERGB ( 47, 79, 79)},
437 {"DarkSlateGrey" , PALETTERGB ( 47, 79, 79)},
438 {"dim gray" , PALETTERGB (105,105,105)},
439 {"DimGray" , PALETTERGB (105,105,105)},
440 {"dim grey" , PALETTERGB (105,105,105)},
441 {"DimGrey" , PALETTERGB (105,105,105)},
442 {"slate gray" , PALETTERGB (112,128,144)},
443 {"SlateGray" , PALETTERGB (112,128,144)},
444 {"slate grey" , PALETTERGB (112,128,144)},
445 {"SlateGrey" , PALETTERGB (112,128,144)},
446 {"light slate gray" , PALETTERGB (119,136,153)},
447 {"LightSlateGray" , PALETTERGB (119,136,153)},
448 {"light slate grey" , PALETTERGB (119,136,153)},
449 {"LightSlateGrey" , PALETTERGB (119,136,153)},
450 {"gray" , PALETTERGB (190,190,190)},
451 {"grey" , PALETTERGB (190,190,190)},
452 {"light grey" , PALETTERGB (211,211,211)},
453 {"LightGrey" , PALETTERGB (211,211,211)},
454 {"light gray" , PALETTERGB (211,211,211)},
455 {"LightGray" , PALETTERGB (211,211,211)},
456 {"midnight blue" , PALETTERGB ( 25, 25,112)},
457 {"MidnightBlue" , PALETTERGB ( 25, 25,112)},
458 {"navy" , PALETTERGB ( 0, 0,128)},
459 {"navy blue" , PALETTERGB ( 0, 0,128)},
460 {"NavyBlue" , PALETTERGB ( 0, 0,128)},
461 {"cornflower blue" , PALETTERGB (100,149,237)},
462 {"CornflowerBlue" , PALETTERGB (100,149,237)},
463 {"dark slate blue" , PALETTERGB ( 72, 61,139)},
464 {"DarkSlateBlue" , PALETTERGB ( 72, 61,139)},
465 {"slate blue" , PALETTERGB (106, 90,205)},
466 {"SlateBlue" , PALETTERGB (106, 90,205)},
467 {"medium slate blue" , PALETTERGB (123,104,238)},
468 {"MediumSlateBlue" , PALETTERGB (123,104,238)},
469 {"light slate blue" , PALETTERGB (132,112,255)},
470 {"LightSlateBlue" , PALETTERGB (132,112,255)},
471 {"medium blue" , PALETTERGB ( 0, 0,205)},
472 {"MediumBlue" , PALETTERGB ( 0, 0,205)},
473 {"royal blue" , PALETTERGB ( 65,105,225)},
474 {"RoyalBlue" , PALETTERGB ( 65,105,225)},
475 {"blue" , PALETTERGB ( 0, 0,255)},
476 {"dodger blue" , PALETTERGB ( 30,144,255)},
477 {"DodgerBlue" , PALETTERGB ( 30,144,255)},
478 {"deep sky blue" , PALETTERGB ( 0,191,255)},
479 {"DeepSkyBlue" , PALETTERGB ( 0,191,255)},
480 {"sky blue" , PALETTERGB (135,206,235)},
481 {"SkyBlue" , PALETTERGB (135,206,235)},
482 {"light sky blue" , PALETTERGB (135,206,250)},
483 {"LightSkyBlue" , PALETTERGB (135,206,250)},
484 {"steel blue" , PALETTERGB ( 70,130,180)},
485 {"SteelBlue" , PALETTERGB ( 70,130,180)},
486 {"light steel blue" , PALETTERGB (176,196,222)},
487 {"LightSteelBlue" , PALETTERGB (176,196,222)},
488 {"light blue" , PALETTERGB (173,216,230)},
489 {"LightBlue" , PALETTERGB (173,216,230)},
490 {"powder blue" , PALETTERGB (176,224,230)},
491 {"PowderBlue" , PALETTERGB (176,224,230)},
492 {"pale turquoise" , PALETTERGB (175,238,238)},
493 {"PaleTurquoise" , PALETTERGB (175,238,238)},
494 {"dark turquoise" , PALETTERGB ( 0,206,209)},
495 {"DarkTurquoise" , PALETTERGB ( 0,206,209)},
496 {"medium turquoise" , PALETTERGB ( 72,209,204)},
497 {"MediumTurquoise" , PALETTERGB ( 72,209,204)},
498 {"turquoise" , PALETTERGB ( 64,224,208)},
499 {"cyan" , PALETTERGB ( 0,255,255)},
500 {"light cyan" , PALETTERGB (224,255,255)},
501 {"LightCyan" , PALETTERGB (224,255,255)},
502 {"cadet blue" , PALETTERGB ( 95,158,160)},
503 {"CadetBlue" , PALETTERGB ( 95,158,160)},
504 {"medium aquamarine" , PALETTERGB (102,205,170)},
505 {"MediumAquamarine" , PALETTERGB (102,205,170)},
506 {"aquamarine" , PALETTERGB (127,255,212)},
507 {"dark green" , PALETTERGB ( 0,100, 0)},
508 {"DarkGreen" , PALETTERGB ( 0,100, 0)},
509 {"dark olive green" , PALETTERGB ( 85,107, 47)},
510 {"DarkOliveGreen" , PALETTERGB ( 85,107, 47)},
511 {"dark sea green" , PALETTERGB (143,188,143)},
512 {"DarkSeaGreen" , PALETTERGB (143,188,143)},
513 {"sea green" , PALETTERGB ( 46,139, 87)},
514 {"SeaGreen" , PALETTERGB ( 46,139, 87)},
515 {"medium sea green" , PALETTERGB ( 60,179,113)},
516 {"MediumSeaGreen" , PALETTERGB ( 60,179,113)},
517 {"light sea green" , PALETTERGB ( 32,178,170)},
518 {"LightSeaGreen" , PALETTERGB ( 32,178,170)},
519 {"pale green" , PALETTERGB (152,251,152)},
520 {"PaleGreen" , PALETTERGB (152,251,152)},
521 {"spring green" , PALETTERGB ( 0,255,127)},
522 {"SpringGreen" , PALETTERGB ( 0,255,127)},
523 {"lawn green" , PALETTERGB (124,252, 0)},
524 {"LawnGreen" , PALETTERGB (124,252, 0)},
525 {"green" , PALETTERGB ( 0,255, 0)},
526 {"chartreuse" , PALETTERGB (127,255, 0)},
527 {"medium spring green" , PALETTERGB ( 0,250,154)},
528 {"MediumSpringGreen" , PALETTERGB ( 0,250,154)},
529 {"green yellow" , PALETTERGB (173,255, 47)},
530 {"GreenYellow" , PALETTERGB (173,255, 47)},
531 {"lime green" , PALETTERGB ( 50,205, 50)},
532 {"LimeGreen" , PALETTERGB ( 50,205, 50)},
533 {"yellow green" , PALETTERGB (154,205, 50)},
534 {"YellowGreen" , PALETTERGB (154,205, 50)},
535 {"forest green" , PALETTERGB ( 34,139, 34)},
536 {"ForestGreen" , PALETTERGB ( 34,139, 34)},
537 {"olive drab" , PALETTERGB (107,142, 35)},
538 {"OliveDrab" , PALETTERGB (107,142, 35)},
539 {"dark khaki" , PALETTERGB (189,183,107)},
540 {"DarkKhaki" , PALETTERGB (189,183,107)},
541 {"khaki" , PALETTERGB (240,230,140)},
542 {"pale goldenrod" , PALETTERGB (238,232,170)},
543 {"PaleGoldenrod" , PALETTERGB (238,232,170)},
544 {"light goldenrod yellow" , PALETTERGB (250,250,210)},
545 {"LightGoldenrodYellow" , PALETTERGB (250,250,210)},
546 {"light yellow" , PALETTERGB (255,255,224)},
547 {"LightYellow" , PALETTERGB (255,255,224)},
548 {"yellow" , PALETTERGB (255,255, 0)},
549 {"gold" , PALETTERGB (255,215, 0)},
550 {"light goldenrod" , PALETTERGB (238,221,130)},
551 {"LightGoldenrod" , PALETTERGB (238,221,130)},
552 {"goldenrod" , PALETTERGB (218,165, 32)},
553 {"dark goldenrod" , PALETTERGB (184,134, 11)},
554 {"DarkGoldenrod" , PALETTERGB (184,134, 11)},
555 {"rosy brown" , PALETTERGB (188,143,143)},
556 {"RosyBrown" , PALETTERGB (188,143,143)},
557 {"indian red" , PALETTERGB (205, 92, 92)},
558 {"IndianRed" , PALETTERGB (205, 92, 92)},
559 {"saddle brown" , PALETTERGB (139, 69, 19)},
560 {"SaddleBrown" , PALETTERGB (139, 69, 19)},
561 {"sienna" , PALETTERGB (160, 82, 45)},
562 {"peru" , PALETTERGB (205,133, 63)},
563 {"burlywood" , PALETTERGB (222,184,135)},
564 {"beige" , PALETTERGB (245,245,220)},
565 {"wheat" , PALETTERGB (245,222,179)},
566 {"sandy brown" , PALETTERGB (244,164, 96)},
567 {"SandyBrown" , PALETTERGB (244,164, 96)},
568 {"tan" , PALETTERGB (210,180,140)},
569 {"chocolate" , PALETTERGB (210,105, 30)},
570 {"firebrick" , PALETTERGB (178,34, 34)},
571 {"brown" , PALETTERGB (165,42, 42)},
572 {"dark salmon" , PALETTERGB (233,150,122)},
573 {"DarkSalmon" , PALETTERGB (233,150,122)},
574 {"salmon" , PALETTERGB (250,128,114)},
575 {"light salmon" , PALETTERGB (255,160,122)},
576 {"LightSalmon" , PALETTERGB (255,160,122)},
577 {"orange" , PALETTERGB (255,165, 0)},
578 {"dark orange" , PALETTERGB (255,140, 0)},
579 {"DarkOrange" , PALETTERGB (255,140, 0)},
580 {"coral" , PALETTERGB (255,127, 80)},
581 {"light coral" , PALETTERGB (240,128,128)},
582 {"LightCoral" , PALETTERGB (240,128,128)},
583 {"tomato" , PALETTERGB (255, 99, 71)},
584 {"orange red" , PALETTERGB (255, 69, 0)},
585 {"OrangeRed" , PALETTERGB (255, 69, 0)},
586 {"red" , PALETTERGB (255, 0, 0)},
587 {"hot pink" , PALETTERGB (255,105,180)},
588 {"HotPink" , PALETTERGB (255,105,180)},
589 {"deep pink" , PALETTERGB (255, 20,147)},
590 {"DeepPink" , PALETTERGB (255, 20,147)},
591 {"pink" , PALETTERGB (255,192,203)},
592 {"light pink" , PALETTERGB (255,182,193)},
593 {"LightPink" , PALETTERGB (255,182,193)},
594 {"pale violet red" , PALETTERGB (219,112,147)},
595 {"PaleVioletRed" , PALETTERGB (219,112,147)},
596 {"maroon" , PALETTERGB (176, 48, 96)},
597 {"medium violet red" , PALETTERGB (199, 21,133)},
598 {"MediumVioletRed" , PALETTERGB (199, 21,133)},
599 {"violet red" , PALETTERGB (208, 32,144)},
600 {"VioletRed" , PALETTERGB (208, 32,144)},
601 {"magenta" , PALETTERGB (255, 0,255)},
602 {"violet" , PALETTERGB (238,130,238)},
603 {"plum" , PALETTERGB (221,160,221)},
604 {"orchid" , PALETTERGB (218,112,214)},
605 {"medium orchid" , PALETTERGB (186, 85,211)},
606 {"MediumOrchid" , PALETTERGB (186, 85,211)},
607 {"dark orchid" , PALETTERGB (153, 50,204)},
608 {"DarkOrchid" , PALETTERGB (153, 50,204)},
609 {"dark violet" , PALETTERGB (148, 0,211)},
610 {"DarkViolet" , PALETTERGB (148, 0,211)},
611 {"blue violet" , PALETTERGB (138, 43,226)},
612 {"BlueViolet" , PALETTERGB (138, 43,226)},
613 {"purple" , PALETTERGB (160, 32,240)},
614 {"medium purple" , PALETTERGB (147,112,219)},
615 {"MediumPurple" , PALETTERGB (147,112,219)},
616 {"thistle" , PALETTERGB (216,191,216)},
617 {"gray0" , PALETTERGB ( 0, 0, 0)},
618 {"grey0" , PALETTERGB ( 0, 0, 0)},
619 {"dark grey" , PALETTERGB (169,169,169)},
620 {"DarkGrey" , PALETTERGB (169,169,169)},
621 {"dark gray" , PALETTERGB (169,169,169)},
622 {"DarkGray" , PALETTERGB (169,169,169)},
623 {"dark blue" , PALETTERGB ( 0, 0,139)},
624 {"DarkBlue" , PALETTERGB ( 0, 0,139)},
625 {"dark cyan" , PALETTERGB ( 0,139,139)},
626 {"DarkCyan" , PALETTERGB ( 0,139,139)},
627 {"dark magenta" , PALETTERGB (139, 0,139)},
628 {"DarkMagenta" , PALETTERGB (139, 0,139)},
629 {"dark red" , PALETTERGB (139, 0, 0)},
630 {"DarkRed" , PALETTERGB (139, 0, 0)},
631 {"light green" , PALETTERGB (144,238,144)},
632 {"LightGreen" , PALETTERGB (144,238,144)},
635 static Lisp_Object
636 w32_default_color_map (void)
638 int i;
639 colormap_t *pc = w32_color_map;
640 Lisp_Object cmap;
642 block_input ();
644 cmap = Qnil;
646 for (i = 0; i < sizeof (w32_color_map) / sizeof (w32_color_map[0]);
647 pc++, i++)
648 cmap = Fcons (Fcons (build_string (pc->name),
649 make_number (pc->colorref)),
650 cmap);
652 unblock_input ();
654 return (cmap);
657 DEFUN ("w32-default-color-map", Fw32_default_color_map, Sw32_default_color_map,
658 0, 0, 0, doc: /* Return the default color map. */)
659 (void)
661 return w32_default_color_map ();
664 static Lisp_Object
665 w32_color_map_lookup (char *colorname)
667 Lisp_Object tail, ret = Qnil;
669 block_input ();
671 for (tail = Vw32_color_map; CONSP (tail); tail = XCDR (tail))
673 register Lisp_Object elt, tem;
675 elt = XCAR (tail);
676 if (!CONSP (elt)) continue;
678 tem = XCAR (elt);
680 if (lstrcmpi (SDATA (tem), colorname) == 0)
682 ret = Fcdr (elt);
683 break;
686 QUIT;
689 unblock_input ();
691 return ret;
695 static void
696 add_system_logical_colors_to_map (Lisp_Object *system_colors)
698 HKEY colors_key;
700 /* Other registry operations are done with input blocked. */
701 block_input ();
703 /* Look for "Control Panel/Colors" under User and Machine registry
704 settings. */
705 if (RegOpenKeyEx (HKEY_CURRENT_USER, "Control Panel\\Colors", 0,
706 KEY_READ, &colors_key) == ERROR_SUCCESS
707 || RegOpenKeyEx (HKEY_LOCAL_MACHINE, "Control Panel\\Colors", 0,
708 KEY_READ, &colors_key) == ERROR_SUCCESS)
710 /* List all keys. */
711 char color_buffer[64];
712 char full_name_buffer[MAX_PATH + SYSTEM_COLOR_PREFIX_LEN];
713 int index = 0;
714 DWORD name_size, color_size;
715 char *name_buffer = full_name_buffer + SYSTEM_COLOR_PREFIX_LEN;
717 name_size = sizeof (full_name_buffer) - SYSTEM_COLOR_PREFIX_LEN;
718 color_size = sizeof (color_buffer);
720 strcpy (full_name_buffer, SYSTEM_COLOR_PREFIX);
722 while (RegEnumValueA (colors_key, index, name_buffer, &name_size,
723 NULL, NULL, color_buffer, &color_size)
724 == ERROR_SUCCESS)
726 int r, g, b;
727 if (sscanf (color_buffer, " %u %u %u", &r, &g, &b) == 3)
728 *system_colors = Fcons (Fcons (build_string (full_name_buffer),
729 make_number (RGB (r, g, b))),
730 *system_colors);
732 name_size = sizeof (full_name_buffer) - SYSTEM_COLOR_PREFIX_LEN;
733 color_size = sizeof (color_buffer);
734 index++;
736 RegCloseKey (colors_key);
739 unblock_input ();
743 static Lisp_Object
744 x_to_w32_color (char * colorname)
746 register Lisp_Object ret = Qnil;
748 block_input ();
750 if (colorname[0] == '#')
752 /* Could be an old-style RGB Device specification. */
753 char *color;
754 int size;
755 color = colorname + 1;
757 size = strlen (color);
758 if (size == 3 || size == 6 || size == 9 || size == 12)
760 UINT colorval;
761 int i, pos;
762 pos = 0;
763 size /= 3;
764 colorval = 0;
766 for (i = 0; i < 3; i++)
768 char *end;
769 char t;
770 unsigned long value;
772 /* The check for 'x' in the following conditional takes into
773 account the fact that strtol allows a "0x" in front of
774 our numbers, and we don't. */
775 if (!isxdigit (color[0]) || color[1] == 'x')
776 break;
777 t = color[size];
778 color[size] = '\0';
779 value = strtoul (color, &end, 16);
780 color[size] = t;
781 if (errno == ERANGE || end - color != size)
782 break;
783 switch (size)
785 case 1:
786 value = value * 0x10;
787 break;
788 case 2:
789 break;
790 case 3:
791 value /= 0x10;
792 break;
793 case 4:
794 value /= 0x100;
795 break;
797 colorval |= (value << pos);
798 pos += 0x8;
799 if (i == 2)
801 unblock_input ();
802 XSETINT (ret, colorval);
803 return ret;
805 color = end;
809 else if (strnicmp (colorname, "rgb:", 4) == 0)
811 char *color;
812 UINT colorval;
813 int i, pos;
814 pos = 0;
816 colorval = 0;
817 color = colorname + 4;
818 for (i = 0; i < 3; i++)
820 char *end;
821 unsigned long value;
823 /* The check for 'x' in the following conditional takes into
824 account the fact that strtol allows a "0x" in front of
825 our numbers, and we don't. */
826 if (!isxdigit (color[0]) || color[1] == 'x')
827 break;
828 value = strtoul (color, &end, 16);
829 if (errno == ERANGE)
830 break;
831 switch (end - color)
833 case 1:
834 value = value * 0x10 + value;
835 break;
836 case 2:
837 break;
838 case 3:
839 value /= 0x10;
840 break;
841 case 4:
842 value /= 0x100;
843 break;
844 default:
845 value = ULONG_MAX;
847 if (value == ULONG_MAX)
848 break;
849 colorval |= (value << pos);
850 pos += 0x8;
851 if (i == 2)
853 if (*end != '\0')
854 break;
855 unblock_input ();
856 XSETINT (ret, colorval);
857 return ret;
859 if (*end != '/')
860 break;
861 color = end + 1;
864 else if (strnicmp (colorname, "rgbi:", 5) == 0)
866 /* This is an RGB Intensity specification. */
867 char *color;
868 UINT colorval;
869 int i, pos;
870 pos = 0;
872 colorval = 0;
873 color = colorname + 5;
874 for (i = 0; i < 3; i++)
876 char *end;
877 double value;
878 UINT val;
880 value = strtod (color, &end);
881 if (errno == ERANGE)
882 break;
883 if (value < 0.0 || value > 1.0)
884 break;
885 val = (UINT)(0x100 * value);
886 /* We used 0x100 instead of 0xFF to give a continuous
887 range between 0.0 and 1.0 inclusive. The next statement
888 fixes the 1.0 case. */
889 if (val == 0x100)
890 val = 0xFF;
891 colorval |= (val << pos);
892 pos += 0x8;
893 if (i == 2)
895 if (*end != '\0')
896 break;
897 unblock_input ();
898 XSETINT (ret, colorval);
899 return ret;
901 if (*end != '/')
902 break;
903 color = end + 1;
906 /* I am not going to attempt to handle any of the CIE color schemes
907 or TekHVC, since I don't know the algorithms for conversion to
908 RGB. */
910 /* If we fail to lookup the color name in w32_color_map, then check the
911 colorname to see if it can be crudely approximated: If the X color
912 ends in a number (e.g., "darkseagreen2"), strip the number and
913 return the result of looking up the base color name. */
914 ret = w32_color_map_lookup (colorname);
915 if (NILP (ret))
917 int len = strlen (colorname);
919 if (isdigit (colorname[len - 1]))
921 char *ptr, *approx = alloca (len + 1);
923 strcpy (approx, colorname);
924 ptr = &approx[len - 1];
925 while (ptr > approx && isdigit (*ptr))
926 *ptr-- = '\0';
928 ret = w32_color_map_lookup (approx);
932 unblock_input ();
933 return ret;
936 void
937 w32_regenerate_palette (FRAME_PTR f)
939 struct w32_palette_entry * list;
940 LOGPALETTE * log_palette;
941 HPALETTE new_palette;
942 int i;
944 /* don't bother trying to create palette if not supported */
945 if (! FRAME_W32_DISPLAY_INFO (f)->has_palette)
946 return;
948 log_palette = (LOGPALETTE *)
949 alloca (sizeof (LOGPALETTE) +
950 FRAME_W32_DISPLAY_INFO (f)->num_colors * sizeof (PALETTEENTRY));
951 log_palette->palVersion = 0x300;
952 log_palette->palNumEntries = FRAME_W32_DISPLAY_INFO (f)->num_colors;
954 list = FRAME_W32_DISPLAY_INFO (f)->color_list;
955 for (i = 0;
956 i < FRAME_W32_DISPLAY_INFO (f)->num_colors;
957 i++, list = list->next)
958 log_palette->palPalEntry[i] = list->entry;
960 new_palette = CreatePalette (log_palette);
962 enter_crit ();
964 if (FRAME_W32_DISPLAY_INFO (f)->palette)
965 DeleteObject (FRAME_W32_DISPLAY_INFO (f)->palette);
966 FRAME_W32_DISPLAY_INFO (f)->palette = new_palette;
968 /* Realize display palette and garbage all frames. */
969 release_frame_dc (f, get_frame_dc (f));
971 leave_crit ();
974 #define W32_COLOR(pe) RGB (pe.peRed, pe.peGreen, pe.peBlue)
975 #define SET_W32_COLOR(pe, color) \
976 do \
978 pe.peRed = GetRValue (color); \
979 pe.peGreen = GetGValue (color); \
980 pe.peBlue = GetBValue (color); \
981 pe.peFlags = 0; \
982 } while (0)
984 #if 0
985 /* Keep these around in case we ever want to track color usage. */
986 void
987 w32_map_color (FRAME_PTR f, COLORREF color)
989 struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
991 if (NILP (Vw32_enable_palette))
992 return;
994 /* check if color is already mapped */
995 while (list)
997 if (W32_COLOR (list->entry) == color)
999 ++list->refcount;
1000 return;
1002 list = list->next;
1005 /* not already mapped, so add to list and recreate Windows palette */
1006 list = xmalloc (sizeof (struct w32_palette_entry));
1007 SET_W32_COLOR (list->entry, color);
1008 list->refcount = 1;
1009 list->next = FRAME_W32_DISPLAY_INFO (f)->color_list;
1010 FRAME_W32_DISPLAY_INFO (f)->color_list = list;
1011 FRAME_W32_DISPLAY_INFO (f)->num_colors++;
1013 /* set flag that palette must be regenerated */
1014 FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
1017 void
1018 w32_unmap_color (FRAME_PTR f, COLORREF color)
1020 struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
1021 struct w32_palette_entry **prev = &FRAME_W32_DISPLAY_INFO (f)->color_list;
1023 if (NILP (Vw32_enable_palette))
1024 return;
1026 /* check if color is already mapped */
1027 while (list)
1029 if (W32_COLOR (list->entry) == color)
1031 if (--list->refcount == 0)
1033 *prev = list->next;
1034 xfree (list);
1035 FRAME_W32_DISPLAY_INFO (f)->num_colors--;
1036 break;
1038 else
1039 return;
1041 prev = &list->next;
1042 list = list->next;
1045 /* set flag that palette must be regenerated */
1046 FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
1048 #endif
1051 /* Gamma-correct COLOR on frame F. */
1053 void
1054 gamma_correct (struct frame *f, COLORREF *color)
1056 if (f->gamma)
1058 *color = PALETTERGB (
1059 pow (GetRValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
1060 pow (GetGValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
1061 pow (GetBValue (*color) / 255.0, f->gamma) * 255.0 + 0.5);
1066 /* Decide if color named COLOR is valid for the display associated with
1067 the selected frame; if so, return the rgb values in COLOR_DEF.
1068 If ALLOC is nonzero, allocate a new colormap cell. */
1071 w32_defined_color (FRAME_PTR f, char *color, XColor *color_def, int alloc)
1073 register Lisp_Object tem;
1074 COLORREF w32_color_ref;
1076 tem = x_to_w32_color (color);
1078 if (!NILP (tem))
1080 if (f)
1082 /* Apply gamma correction. */
1083 w32_color_ref = XUINT (tem);
1084 gamma_correct (f, &w32_color_ref);
1085 XSETINT (tem, w32_color_ref);
1088 /* Map this color to the palette if it is enabled. */
1089 if (!NILP (Vw32_enable_palette))
1091 struct w32_palette_entry * entry =
1092 one_w32_display_info.color_list;
1093 struct w32_palette_entry ** prev =
1094 &one_w32_display_info.color_list;
1096 /* check if color is already mapped */
1097 while (entry)
1099 if (W32_COLOR (entry->entry) == XUINT (tem))
1100 break;
1101 prev = &entry->next;
1102 entry = entry->next;
1105 if (entry == NULL && alloc)
1107 /* not already mapped, so add to list */
1108 entry = xmalloc (sizeof (struct w32_palette_entry));
1109 SET_W32_COLOR (entry->entry, XUINT (tem));
1110 entry->next = NULL;
1111 *prev = entry;
1112 one_w32_display_info.num_colors++;
1114 /* set flag that palette must be regenerated */
1115 one_w32_display_info.regen_palette = TRUE;
1118 /* Ensure COLORREF value is snapped to nearest color in (default)
1119 palette by simulating the PALETTERGB macro. This works whether
1120 or not the display device has a palette. */
1121 w32_color_ref = XUINT (tem) | 0x2000000;
1123 color_def->pixel = w32_color_ref;
1124 color_def->red = GetRValue (w32_color_ref) * 256;
1125 color_def->green = GetGValue (w32_color_ref) * 256;
1126 color_def->blue = GetBValue (w32_color_ref) * 256;
1128 return 1;
1130 else
1132 return 0;
1136 /* Given a string ARG naming a color, compute a pixel value from it
1137 suitable for screen F.
1138 If F is not a color screen, return DEF (default) regardless of what
1139 ARG says. */
1142 x_decode_color (FRAME_PTR f, Lisp_Object arg, int def)
1144 XColor cdef;
1146 CHECK_STRING (arg);
1148 if (strcmp (SDATA (arg), "black") == 0)
1149 return BLACK_PIX_DEFAULT (f);
1150 else if (strcmp (SDATA (arg), "white") == 0)
1151 return WHITE_PIX_DEFAULT (f);
1153 if ((FRAME_W32_DISPLAY_INFO (f)->n_planes * FRAME_W32_DISPLAY_INFO (f)->n_cbits) == 1)
1154 return def;
1156 /* w32_defined_color is responsible for coping with failures
1157 by looking for a near-miss. */
1158 if (w32_defined_color (f, SDATA (arg), &cdef, 1))
1159 return cdef.pixel;
1161 /* defined_color failed; return an ultimate default. */
1162 return def;
1167 /* Functions called only from `x_set_frame_param'
1168 to set individual parameters.
1170 If FRAME_W32_WINDOW (f) is 0,
1171 the frame is being created and its window does not exist yet.
1172 In that case, just record the parameter's new value
1173 in the standard place; do not attempt to change the window. */
1175 void
1176 x_set_foreground_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1178 struct w32_output *x = f->output_data.w32;
1179 PIX_TYPE fg, old_fg;
1181 fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1182 old_fg = FRAME_FOREGROUND_PIXEL (f);
1183 FRAME_FOREGROUND_PIXEL (f) = fg;
1185 if (FRAME_W32_WINDOW (f) != 0)
1187 if (x->cursor_pixel == old_fg)
1189 x->cursor_pixel = fg;
1190 x->cursor_gc->background = fg;
1193 update_face_from_frame_parameter (f, Qforeground_color, arg);
1194 if (FRAME_VISIBLE_P (f))
1195 redraw_frame (f);
1199 void
1200 x_set_background_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1202 FRAME_BACKGROUND_PIXEL (f)
1203 = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
1205 if (FRAME_W32_WINDOW (f) != 0)
1207 SetWindowLong (FRAME_W32_WINDOW (f), WND_BACKGROUND_INDEX,
1208 FRAME_BACKGROUND_PIXEL (f));
1210 update_face_from_frame_parameter (f, Qbackground_color, arg);
1212 if (FRAME_VISIBLE_P (f))
1213 redraw_frame (f);
1217 void
1218 x_set_mouse_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1220 Cursor cursor, nontext_cursor, mode_cursor, hand_cursor;
1221 int count;
1222 int mask_color;
1224 if (!EQ (Qnil, arg))
1225 f->output_data.w32->mouse_pixel
1226 = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1227 mask_color = FRAME_BACKGROUND_PIXEL (f);
1229 /* Don't let pointers be invisible. */
1230 if (mask_color == f->output_data.w32->mouse_pixel
1231 && mask_color == FRAME_BACKGROUND_PIXEL (f))
1232 f->output_data.w32->mouse_pixel = FRAME_FOREGROUND_PIXEL (f);
1234 #if 0 /* TODO : Mouse cursor customization. */
1235 block_input ();
1237 /* It's not okay to crash if the user selects a screwy cursor. */
1238 count = x_catch_errors (FRAME_W32_DISPLAY (f));
1240 if (!EQ (Qnil, Vx_pointer_shape))
1242 CHECK_NUMBER (Vx_pointer_shape);
1243 cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XINT (Vx_pointer_shape));
1245 else
1246 cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
1247 x_check_errors (FRAME_W32_DISPLAY (f), "bad text pointer cursor: %s");
1249 if (!EQ (Qnil, Vx_nontext_pointer_shape))
1251 CHECK_NUMBER (Vx_nontext_pointer_shape);
1252 nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1253 XINT (Vx_nontext_pointer_shape));
1255 else
1256 nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_left_ptr);
1257 x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
1259 if (!EQ (Qnil, Vx_hourglass_pointer_shape))
1261 CHECK_NUMBER (Vx_hourglass_pointer_shape);
1262 hourglass_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1263 XINT (Vx_hourglass_pointer_shape));
1265 else
1266 hourglass_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_watch);
1267 x_check_errors (FRAME_W32_DISPLAY (f), "bad busy pointer cursor: %s");
1269 x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
1270 if (!EQ (Qnil, Vx_mode_pointer_shape))
1272 CHECK_NUMBER (Vx_mode_pointer_shape);
1273 mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1274 XINT (Vx_mode_pointer_shape));
1276 else
1277 mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
1278 x_check_errors (FRAME_W32_DISPLAY (f), "bad modeline pointer cursor: %s");
1280 if (!EQ (Qnil, Vx_sensitive_text_pointer_shape))
1282 CHECK_NUMBER (Vx_sensitive_text_pointer_shape);
1283 hand_cursor
1284 = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1285 XINT (Vx_sensitive_text_pointer_shape));
1287 else
1288 hand_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_crosshair);
1290 if (!NILP (Vx_window_horizontal_drag_shape))
1292 CHECK_NUMBER (Vx_window_horizontal_drag_shape);
1293 horizontal_drag_cursor
1294 = XCreateFontCursor (FRAME_X_DISPLAY (f),
1295 XINT (Vx_window_horizontal_drag_shape));
1297 else
1298 horizontal_drag_cursor
1299 = XCreateFontCursor (FRAME_X_DISPLAY (f), XC_sb_h_double_arrow);
1301 /* Check and report errors with the above calls. */
1302 x_check_errors (FRAME_W32_DISPLAY (f), "can't set cursor shape: %s");
1303 x_uncatch_errors (FRAME_W32_DISPLAY (f), count);
1306 XColor fore_color, back_color;
1308 fore_color.pixel = f->output_data.w32->mouse_pixel;
1309 back_color.pixel = mask_color;
1310 XQueryColor (FRAME_W32_DISPLAY (f),
1311 DefaultColormap (FRAME_W32_DISPLAY (f),
1312 DefaultScreen (FRAME_W32_DISPLAY (f))),
1313 &fore_color);
1314 XQueryColor (FRAME_W32_DISPLAY (f),
1315 DefaultColormap (FRAME_W32_DISPLAY (f),
1316 DefaultScreen (FRAME_W32_DISPLAY (f))),
1317 &back_color);
1318 XRecolorCursor (FRAME_W32_DISPLAY (f), cursor,
1319 &fore_color, &back_color);
1320 XRecolorCursor (FRAME_W32_DISPLAY (f), nontext_cursor,
1321 &fore_color, &back_color);
1322 XRecolorCursor (FRAME_W32_DISPLAY (f), mode_cursor,
1323 &fore_color, &back_color);
1324 XRecolorCursor (FRAME_W32_DISPLAY (f), hand_cursor,
1325 &fore_color, &back_color);
1326 XRecolorCursor (FRAME_W32_DISPLAY (f), hourglass_cursor,
1327 &fore_color, &back_color);
1330 if (FRAME_W32_WINDOW (f) != 0)
1331 XDefineCursor (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), cursor);
1333 if (cursor != f->output_data.w32->text_cursor && f->output_data.w32->text_cursor != 0)
1334 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->text_cursor);
1335 f->output_data.w32->text_cursor = cursor;
1337 if (nontext_cursor != f->output_data.w32->nontext_cursor
1338 && f->output_data.w32->nontext_cursor != 0)
1339 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->nontext_cursor);
1340 f->output_data.w32->nontext_cursor = nontext_cursor;
1342 if (hourglass_cursor != f->output_data.w32->hourglass_cursor
1343 && f->output_data.w32->hourglass_cursor != 0)
1344 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->hourglass_cursor);
1345 f->output_data.w32->hourglass_cursor = hourglass_cursor;
1347 if (mode_cursor != f->output_data.w32->modeline_cursor
1348 && f->output_data.w32->modeline_cursor != 0)
1349 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->modeline_cursor);
1350 f->output_data.w32->modeline_cursor = mode_cursor;
1352 if (hand_cursor != f->output_data.w32->hand_cursor
1353 && f->output_data.w32->hand_cursor != 0)
1354 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->hand_cursor);
1355 f->output_data.w32->hand_cursor = hand_cursor;
1357 XFlush (FRAME_W32_DISPLAY (f));
1358 unblock_input ();
1360 update_face_from_frame_parameter (f, Qmouse_color, arg);
1361 #endif /* TODO */
1364 void
1365 x_set_cursor_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1367 unsigned long fore_pixel, pixel;
1369 if (!NILP (Vx_cursor_fore_pixel))
1370 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
1371 WHITE_PIX_DEFAULT (f));
1372 else
1373 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
1375 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1377 /* Make sure that the cursor color differs from the background color. */
1378 if (pixel == FRAME_BACKGROUND_PIXEL (f))
1380 pixel = f->output_data.w32->mouse_pixel;
1381 if (pixel == fore_pixel)
1382 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
1385 f->output_data.w32->cursor_foreground_pixel = fore_pixel;
1386 f->output_data.w32->cursor_pixel = pixel;
1388 if (FRAME_W32_WINDOW (f) != 0)
1390 block_input ();
1391 /* Update frame's cursor_gc. */
1392 f->output_data.w32->cursor_gc->foreground = fore_pixel;
1393 f->output_data.w32->cursor_gc->background = pixel;
1395 unblock_input ();
1397 if (FRAME_VISIBLE_P (f))
1399 x_update_cursor (f, 0);
1400 x_update_cursor (f, 1);
1404 update_face_from_frame_parameter (f, Qcursor_color, arg);
1407 /* Set the border-color of frame F to pixel value PIX.
1408 Note that this does not fully take effect if done before
1409 F has a window. */
1411 void
1412 x_set_border_pixel (struct frame *f, int pix)
1415 f->output_data.w32->border_pixel = pix;
1417 if (FRAME_W32_WINDOW (f) != 0 && f->border_width > 0)
1419 if (FRAME_VISIBLE_P (f))
1420 redraw_frame (f);
1424 /* Set the border-color of frame F to value described by ARG.
1425 ARG can be a string naming a color.
1426 The border-color is used for the border that is drawn by the server.
1427 Note that this does not fully take effect if done before
1428 F has a window; it must be redone when the window is created. */
1430 void
1431 x_set_border_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1433 int pix;
1435 CHECK_STRING (arg);
1436 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1437 x_set_border_pixel (f, pix);
1438 update_face_from_frame_parameter (f, Qborder_color, arg);
1442 void
1443 x_set_cursor_type (FRAME_PTR f, Lisp_Object arg, Lisp_Object oldval)
1445 set_frame_cursor_types (f, arg);
1447 /* Make sure the cursor gets redrawn. */
1448 cursor_type_changed = 1;
1451 void
1452 x_set_icon_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1454 int result;
1456 if (NILP (arg) && NILP (oldval))
1457 return;
1459 if (STRINGP (arg) && STRINGP (oldval)
1460 && EQ (Fstring_equal (oldval, arg), Qt))
1461 return;
1463 if (SYMBOLP (arg) && SYMBOLP (oldval) && EQ (arg, oldval))
1464 return;
1466 block_input ();
1468 result = x_bitmap_icon (f, arg);
1469 if (result)
1471 unblock_input ();
1472 error ("No icon window available");
1475 unblock_input ();
1478 void
1479 x_set_icon_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1481 if (STRINGP (arg))
1483 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1484 return;
1486 else if (!NILP (arg) || NILP (oldval))
1487 return;
1489 fset_icon_name (f, arg);
1491 #if 0
1492 if (f->output_data.w32->icon_bitmap != 0)
1493 return;
1495 block_input ();
1497 result = x_text_icon (f,
1498 SSDATA ((!NILP (f->icon_name)
1499 ? f->icon_name
1500 : !NILP (f->title)
1501 ? f->title
1502 : f->name)));
1504 if (result)
1506 unblock_input ();
1507 error ("No icon window available");
1510 /* If the window was unmapped (and its icon was mapped),
1511 the new icon is not mapped, so map the window in its stead. */
1512 if (FRAME_VISIBLE_P (f))
1514 #ifdef USE_X_TOOLKIT
1515 XtPopup (f->output_data.w32->widget, XtGrabNone);
1516 #endif
1517 XMapWindow (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f));
1520 XFlush (FRAME_W32_DISPLAY (f));
1521 unblock_input ();
1522 #endif
1526 void
1527 x_set_menu_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1529 int nlines;
1531 /* Right now, menu bars don't work properly in minibuf-only frames;
1532 most of the commands try to apply themselves to the minibuffer
1533 frame itself, and get an error because you can't switch buffers
1534 in or split the minibuffer window. */
1535 if (FRAME_MINIBUF_ONLY_P (f))
1536 return;
1538 if (INTEGERP (value))
1539 nlines = XINT (value);
1540 else
1541 nlines = 0;
1543 FRAME_MENU_BAR_LINES (f) = 0;
1544 if (nlines)
1545 FRAME_EXTERNAL_MENU_BAR (f) = 1;
1546 else
1548 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
1549 free_frame_menubar (f);
1550 FRAME_EXTERNAL_MENU_BAR (f) = 0;
1552 /* Adjust the frame size so that the client (text) dimensions
1553 remain the same. This depends on FRAME_EXTERNAL_MENU_BAR being
1554 set correctly. */
1555 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
1556 do_pending_window_change (0);
1558 adjust_glyphs (f);
1562 /* Set the number of lines used for the tool bar of frame F to VALUE.
1563 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
1564 is the old number of tool bar lines. This function changes the
1565 height of all windows on frame F to match the new tool bar height.
1566 The frame's height doesn't change. */
1568 void
1569 x_set_tool_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1571 int delta, nlines, root_height;
1572 Lisp_Object root_window;
1574 /* Treat tool bars like menu bars. */
1575 if (FRAME_MINIBUF_ONLY_P (f))
1576 return;
1578 /* Use VALUE only if an integer >= 0. */
1579 if (INTEGERP (value) && XINT (value) >= 0)
1580 nlines = XFASTINT (value);
1581 else
1582 nlines = 0;
1584 /* Make sure we redisplay all windows in this frame. */
1585 ++windows_or_buffers_changed;
1587 delta = nlines - FRAME_TOOL_BAR_LINES (f);
1589 /* Don't resize the tool-bar to more than we have room for. */
1590 root_window = FRAME_ROOT_WINDOW (f);
1591 root_height = WINDOW_TOTAL_LINES (XWINDOW (root_window));
1592 if (root_height - delta < 1)
1594 delta = root_height - 1;
1595 nlines = FRAME_TOOL_BAR_LINES (f) + delta;
1598 FRAME_TOOL_BAR_LINES (f) = nlines;
1599 resize_frame_windows (f, FRAME_LINES (f), 0);
1600 adjust_glyphs (f);
1602 /* We also have to make sure that the internal border at the top of
1603 the frame, below the menu bar or tool bar, is redrawn when the
1604 tool bar disappears. This is so because the internal border is
1605 below the tool bar if one is displayed, but is below the menu bar
1606 if there isn't a tool bar. The tool bar draws into the area
1607 below the menu bar. */
1608 if (FRAME_W32_WINDOW (f) && FRAME_TOOL_BAR_LINES (f) == 0)
1610 clear_frame (f);
1611 clear_current_matrices (f);
1614 /* If the tool bar gets smaller, the internal border below it
1615 has to be cleared. It was formerly part of the display
1616 of the larger tool bar, and updating windows won't clear it. */
1617 if (delta < 0)
1619 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
1620 int width = FRAME_PIXEL_WIDTH (f);
1621 int y = nlines * FRAME_LINE_HEIGHT (f);
1623 block_input ();
1625 HDC hdc = get_frame_dc (f);
1626 w32_clear_area (f, hdc, 0, y, width, height);
1627 release_frame_dc (f, hdc);
1629 unblock_input ();
1631 if (WINDOWP (f->tool_bar_window))
1632 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
1635 run_window_configuration_change_hook (f);
1640 /* Change the name of frame F to NAME. If NAME is nil, set F's name to
1641 w32_id_name.
1643 If EXPLICIT is non-zero, that indicates that lisp code is setting the
1644 name; if NAME is a string, set F's name to NAME and set
1645 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1647 If EXPLICIT is zero, that indicates that Emacs redisplay code is
1648 suggesting a new name, which lisp code should override; if
1649 F->explicit_name is set, ignore the new name; otherwise, set it. */
1651 void
1652 x_set_name (struct frame *f, Lisp_Object name, int explicit)
1654 /* Make sure that requests from lisp code override requests from
1655 Emacs redisplay code. */
1656 if (explicit)
1658 /* If we're switching from explicit to implicit, we had better
1659 update the mode lines and thereby update the title. */
1660 if (f->explicit_name && NILP (name))
1661 update_mode_lines = 1;
1663 f->explicit_name = ! NILP (name);
1665 else if (f->explicit_name)
1666 return;
1668 /* If NAME is nil, set the name to the w32_id_name. */
1669 if (NILP (name))
1671 /* Check for no change needed in this very common case
1672 before we do any consing. */
1673 if (!strcmp (FRAME_W32_DISPLAY_INFO (f)->w32_id_name,
1674 SDATA (f->name)))
1675 return;
1676 name = build_string (FRAME_W32_DISPLAY_INFO (f)->w32_id_name);
1678 else
1679 CHECK_STRING (name);
1681 /* Don't change the name if it's already NAME. */
1682 if (! NILP (Fstring_equal (name, f->name)))
1683 return;
1685 fset_name (f, name);
1687 /* For setting the frame title, the title parameter should override
1688 the name parameter. */
1689 if (! NILP (f->title))
1690 name = f->title;
1692 if (FRAME_W32_WINDOW (f))
1694 if (STRING_MULTIBYTE (name))
1695 name = ENCODE_SYSTEM (name);
1697 block_input ();
1698 SetWindowText (FRAME_W32_WINDOW (f), SDATA (name));
1699 unblock_input ();
1703 /* This function should be called when the user's lisp code has
1704 specified a name for the frame; the name will override any set by the
1705 redisplay code. */
1706 void
1707 x_explicitly_set_name (FRAME_PTR f, Lisp_Object arg, Lisp_Object oldval)
1709 x_set_name (f, arg, 1);
1712 /* This function should be called by Emacs redisplay code to set the
1713 name; names set this way will never override names set by the user's
1714 lisp code. */
1715 void
1716 x_implicitly_set_name (FRAME_PTR f, Lisp_Object arg, Lisp_Object oldval)
1718 x_set_name (f, arg, 0);
1721 /* Change the title of frame F to NAME.
1722 If NAME is nil, use the frame name as the title. */
1724 void
1725 x_set_title (struct frame *f, Lisp_Object name, Lisp_Object old_name)
1727 /* Don't change the title if it's already NAME. */
1728 if (EQ (name, f->title))
1729 return;
1731 update_mode_lines = 1;
1733 fset_title (f, name);
1735 if (NILP (name))
1736 name = f->name;
1738 if (FRAME_W32_WINDOW (f))
1740 if (STRING_MULTIBYTE (name))
1741 name = ENCODE_SYSTEM (name);
1743 block_input ();
1744 SetWindowText (FRAME_W32_WINDOW (f), SDATA (name));
1745 unblock_input ();
1749 void
1750 x_set_scroll_bar_default_width (struct frame *f)
1752 int wid = FRAME_COLUMN_WIDTH (f);
1754 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = GetSystemMetrics (SM_CXVSCROLL);
1755 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) +
1756 wid - 1) / wid;
1760 /* Subroutines for creating a frame. */
1762 Cursor
1763 w32_load_cursor (LPCTSTR name)
1765 /* Try first to load cursor from application resource. */
1766 Cursor cursor = LoadImage ((HINSTANCE) GetModuleHandle (NULL),
1767 name, IMAGE_CURSOR, 0, 0,
1768 LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_SHARED);
1769 if (!cursor)
1771 /* Then try to load a shared predefined cursor. */
1772 cursor = LoadImage (NULL, name, IMAGE_CURSOR, 0, 0,
1773 LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_SHARED);
1775 return cursor;
1778 static LRESULT CALLBACK w32_wnd_proc (HWND, UINT, WPARAM, LPARAM);
1780 #define INIT_WINDOW_CLASS(WC) \
1781 (WC).style = CS_HREDRAW | CS_VREDRAW; \
1782 (WC).lpfnWndProc = (WNDPROC) w32_wnd_proc; \
1783 (WC).cbClsExtra = 0; \
1784 (WC).cbWndExtra = WND_EXTRA_BYTES; \
1785 (WC).hInstance = hinst; \
1786 (WC).hIcon = LoadIcon (hinst, EMACS_CLASS); \
1787 (WC).hCursor = w32_load_cursor (IDC_ARROW); \
1788 (WC).hbrBackground = NULL; \
1789 (WC).lpszMenuName = NULL; \
1791 static BOOL
1792 w32_init_class (HINSTANCE hinst)
1795 if (w32_unicode_gui)
1797 WNDCLASSW uwc;
1798 INIT_WINDOW_CLASS(uwc);
1799 uwc.lpszClassName = L"Emacs";
1801 return RegisterClassW (&uwc);
1803 else
1805 WNDCLASS wc;
1806 INIT_WINDOW_CLASS(wc);
1807 wc.lpszClassName = EMACS_CLASS;
1809 return RegisterClassA (&wc);
1813 static HWND
1814 w32_createscrollbar (struct frame *f, struct scroll_bar * bar)
1816 return (CreateWindow ("SCROLLBAR", "", SBS_VERT | WS_CHILD | WS_VISIBLE,
1817 /* Position and size of scroll bar. */
1818 XINT (bar->left) + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
1819 XINT (bar->top),
1820 XINT (bar->width) - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
1821 XINT (bar->height),
1822 FRAME_W32_WINDOW (f),
1823 NULL,
1824 hinst,
1825 NULL));
1828 static void
1829 w32_createwindow (struct frame *f)
1831 HWND hwnd;
1832 RECT rect;
1833 Lisp_Object top = Qunbound;
1834 Lisp_Object left = Qunbound;
1835 struct w32_display_info *dpyinfo = &one_w32_display_info;
1837 rect.left = rect.top = 0;
1838 rect.right = FRAME_PIXEL_WIDTH (f);
1839 rect.bottom = FRAME_PIXEL_HEIGHT (f);
1841 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
1842 FRAME_EXTERNAL_MENU_BAR (f));
1844 /* Do first time app init */
1846 if (!hprevinst)
1848 w32_init_class (hinst);
1851 if (f->size_hint_flags & USPosition || f->size_hint_flags & PPosition)
1853 XSETINT (left, f->left_pos);
1854 XSETINT (top, f->top_pos);
1856 else if (EQ (left, Qunbound) && EQ (top, Qunbound))
1858 /* When called with RES_TYPE_NUMBER, w32_get_arg will return zero
1859 for anything that is not a number and is not Qunbound. */
1860 left = x_get_arg (dpyinfo, Qnil, Qleft, "left", "Left", RES_TYPE_NUMBER);
1861 top = x_get_arg (dpyinfo, Qnil, Qtop, "top", "Top", RES_TYPE_NUMBER);
1864 FRAME_W32_WINDOW (f) = hwnd
1865 = CreateWindow (EMACS_CLASS,
1866 f->namebuf,
1867 f->output_data.w32->dwStyle | WS_CLIPCHILDREN,
1868 EQ (left, Qunbound) ? CW_USEDEFAULT : XINT (left),
1869 EQ (top, Qunbound) ? CW_USEDEFAULT : XINT (top),
1870 rect.right - rect.left,
1871 rect.bottom - rect.top,
1872 NULL,
1873 NULL,
1874 hinst,
1875 NULL);
1877 if (hwnd)
1879 SetWindowLong (hwnd, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
1880 SetWindowLong (hwnd, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
1881 SetWindowLong (hwnd, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
1882 SetWindowLong (hwnd, WND_SCROLLBAR_INDEX, f->scroll_bar_actual_width);
1883 SetWindowLong (hwnd, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
1885 /* Enable drag-n-drop. */
1886 DragAcceptFiles (hwnd, TRUE);
1888 /* Do this to discard the default setting specified by our parent. */
1889 ShowWindow (hwnd, SW_HIDE);
1891 /* Update frame positions. */
1892 GetWindowRect (hwnd, &rect);
1893 f->left_pos = rect.left;
1894 f->top_pos = rect.top;
1898 static void
1899 my_post_msg (W32Msg * wmsg, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
1901 wmsg->msg.hwnd = hwnd;
1902 wmsg->msg.message = msg;
1903 wmsg->msg.wParam = wParam;
1904 wmsg->msg.lParam = lParam;
1905 wmsg->msg.time = GetMessageTime ();
1907 post_msg (wmsg);
1910 /* GetKeyState and MapVirtualKey on Windows 95 do not actually distinguish
1911 between left and right keys as advertised. We test for this
1912 support dynamically, and set a flag when the support is absent. If
1913 absent, we keep track of the left and right control and alt keys
1914 ourselves. This is particularly necessary on keyboards that rely
1915 upon the AltGr key, which is represented as having the left control
1916 and right alt keys pressed. For these keyboards, we need to know
1917 when the left alt key has been pressed in addition to the AltGr key
1918 so that we can properly support M-AltGr-key sequences (such as M-@
1919 on Swedish keyboards). */
1921 #define EMACS_LCONTROL 0
1922 #define EMACS_RCONTROL 1
1923 #define EMACS_LMENU 2
1924 #define EMACS_RMENU 3
1926 static int modifiers[4];
1927 static int modifiers_recorded;
1928 static int modifier_key_support_tested;
1930 static void
1931 test_modifier_support (unsigned int wparam)
1933 unsigned int l, r;
1935 if (wparam != VK_CONTROL && wparam != VK_MENU)
1936 return;
1937 if (wparam == VK_CONTROL)
1939 l = VK_LCONTROL;
1940 r = VK_RCONTROL;
1942 else
1944 l = VK_LMENU;
1945 r = VK_RMENU;
1947 if (!(GetKeyState (l) & 0x8000) && !(GetKeyState (r) & 0x8000))
1948 modifiers_recorded = 1;
1949 else
1950 modifiers_recorded = 0;
1951 modifier_key_support_tested = 1;
1954 static void
1955 record_keydown (unsigned int wparam, unsigned int lparam)
1957 int i;
1959 if (!modifier_key_support_tested)
1960 test_modifier_support (wparam);
1962 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
1963 return;
1965 if (wparam == VK_CONTROL)
1966 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
1967 else
1968 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
1970 modifiers[i] = 1;
1973 static void
1974 record_keyup (unsigned int wparam, unsigned int lparam)
1976 int i;
1978 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
1979 return;
1981 if (wparam == VK_CONTROL)
1982 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
1983 else
1984 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
1986 modifiers[i] = 0;
1989 /* Emacs can lose focus while a modifier key has been pressed. When
1990 it regains focus, be conservative and clear all modifiers since
1991 we cannot reconstruct the left and right modifier state. */
1992 static void
1993 reset_modifiers (void)
1995 SHORT ctrl, alt;
1997 if (GetFocus () == NULL)
1998 /* Emacs doesn't have keyboard focus. Do nothing. */
1999 return;
2001 ctrl = GetAsyncKeyState (VK_CONTROL);
2002 alt = GetAsyncKeyState (VK_MENU);
2004 if (!(ctrl & 0x08000))
2005 /* Clear any recorded control modifier state. */
2006 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
2008 if (!(alt & 0x08000))
2009 /* Clear any recorded alt modifier state. */
2010 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
2012 /* Update the state of all modifier keys, because modifiers used in
2013 hot-key combinations can get stuck on if Emacs loses focus as a
2014 result of a hot-key being pressed. */
2016 BYTE keystate[256];
2018 #define CURRENT_STATE(key) ((GetAsyncKeyState (key) & 0x8000) >> 8)
2020 GetKeyboardState (keystate);
2021 keystate[VK_SHIFT] = CURRENT_STATE (VK_SHIFT);
2022 keystate[VK_CONTROL] = CURRENT_STATE (VK_CONTROL);
2023 keystate[VK_LCONTROL] = CURRENT_STATE (VK_LCONTROL);
2024 keystate[VK_RCONTROL] = CURRENT_STATE (VK_RCONTROL);
2025 keystate[VK_MENU] = CURRENT_STATE (VK_MENU);
2026 keystate[VK_LMENU] = CURRENT_STATE (VK_LMENU);
2027 keystate[VK_RMENU] = CURRENT_STATE (VK_RMENU);
2028 keystate[VK_LWIN] = CURRENT_STATE (VK_LWIN);
2029 keystate[VK_RWIN] = CURRENT_STATE (VK_RWIN);
2030 keystate[VK_APPS] = CURRENT_STATE (VK_APPS);
2031 SetKeyboardState (keystate);
2035 /* Synchronize modifier state with what is reported with the current
2036 keystroke. Even if we cannot distinguish between left and right
2037 modifier keys, we know that, if no modifiers are set, then neither
2038 the left or right modifier should be set. */
2039 static void
2040 sync_modifiers (void)
2042 if (!modifiers_recorded)
2043 return;
2045 if (!(GetKeyState (VK_CONTROL) & 0x8000))
2046 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
2048 if (!(GetKeyState (VK_MENU) & 0x8000))
2049 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
2052 static int
2053 modifier_set (int vkey)
2055 if (vkey == VK_CAPITAL || vkey == VK_SCROLL)
2056 return (GetKeyState (vkey) & 0x1);
2057 if (!modifiers_recorded)
2058 return (GetKeyState (vkey) & 0x8000);
2060 switch (vkey)
2062 case VK_LCONTROL:
2063 return modifiers[EMACS_LCONTROL];
2064 case VK_RCONTROL:
2065 return modifiers[EMACS_RCONTROL];
2066 case VK_LMENU:
2067 return modifiers[EMACS_LMENU];
2068 case VK_RMENU:
2069 return modifiers[EMACS_RMENU];
2071 return (GetKeyState (vkey) & 0x8000);
2074 /* Convert between the modifier bits W32 uses and the modifier bits
2075 Emacs uses. */
2077 unsigned int
2078 w32_key_to_modifier (int key)
2080 Lisp_Object key_mapping;
2082 switch (key)
2084 case VK_LWIN:
2085 key_mapping = Vw32_lwindow_modifier;
2086 break;
2087 case VK_RWIN:
2088 key_mapping = Vw32_rwindow_modifier;
2089 break;
2090 case VK_APPS:
2091 key_mapping = Vw32_apps_modifier;
2092 break;
2093 case VK_SCROLL:
2094 key_mapping = Vw32_scroll_lock_modifier;
2095 break;
2096 default:
2097 key_mapping = Qnil;
2100 /* NB. This code runs in the input thread, asynchronously to the lisp
2101 thread, so we must be careful to ensure access to lisp data is
2102 thread-safe. The following code is safe because the modifier
2103 variable values are updated atomically from lisp and symbols are
2104 not relocated by GC. Also, we don't have to worry about seeing GC
2105 markbits here. */
2106 if (EQ (key_mapping, Qhyper))
2107 return hyper_modifier;
2108 if (EQ (key_mapping, Qsuper))
2109 return super_modifier;
2110 if (EQ (key_mapping, Qmeta))
2111 return meta_modifier;
2112 if (EQ (key_mapping, Qalt))
2113 return alt_modifier;
2114 if (EQ (key_mapping, Qctrl))
2115 return ctrl_modifier;
2116 if (EQ (key_mapping, Qcontrol)) /* synonym for ctrl */
2117 return ctrl_modifier;
2118 if (EQ (key_mapping, Qshift))
2119 return shift_modifier;
2121 /* Don't generate any modifier if not explicitly requested. */
2122 return 0;
2125 static unsigned int
2126 w32_get_modifiers (void)
2128 return ((modifier_set (VK_SHIFT) ? shift_modifier : 0) |
2129 (modifier_set (VK_CONTROL) ? ctrl_modifier : 0) |
2130 (modifier_set (VK_LWIN) ? w32_key_to_modifier (VK_LWIN) : 0) |
2131 (modifier_set (VK_RWIN) ? w32_key_to_modifier (VK_RWIN) : 0) |
2132 (modifier_set (VK_APPS) ? w32_key_to_modifier (VK_APPS) : 0) |
2133 (modifier_set (VK_SCROLL) ? w32_key_to_modifier (VK_SCROLL) : 0) |
2134 (modifier_set (VK_MENU) ?
2135 ((NILP (Vw32_alt_is_meta)) ? alt_modifier : meta_modifier) : 0));
2138 /* We map the VK_* modifiers into console modifier constants
2139 so that we can use the same routines to handle both console
2140 and window input. */
2142 static int
2143 construct_console_modifiers (void)
2145 int mods;
2147 mods = 0;
2148 mods |= (modifier_set (VK_SHIFT)) ? SHIFT_PRESSED : 0;
2149 mods |= (modifier_set (VK_CAPITAL)) ? CAPSLOCK_ON : 0;
2150 mods |= (modifier_set (VK_SCROLL)) ? SCROLLLOCK_ON : 0;
2151 mods |= (modifier_set (VK_NUMLOCK)) ? NUMLOCK_ON : 0;
2152 mods |= (modifier_set (VK_LCONTROL)) ? LEFT_CTRL_PRESSED : 0;
2153 mods |= (modifier_set (VK_RCONTROL)) ? RIGHT_CTRL_PRESSED : 0;
2154 mods |= (modifier_set (VK_LMENU)) ? LEFT_ALT_PRESSED : 0;
2155 mods |= (modifier_set (VK_RMENU)) ? RIGHT_ALT_PRESSED : 0;
2156 mods |= (modifier_set (VK_LWIN)) ? LEFT_WIN_PRESSED : 0;
2157 mods |= (modifier_set (VK_RWIN)) ? RIGHT_WIN_PRESSED : 0;
2158 mods |= (modifier_set (VK_APPS)) ? APPS_PRESSED : 0;
2160 return mods;
2163 static int
2164 w32_get_key_modifiers (unsigned int wparam, unsigned int lparam)
2166 int mods;
2168 /* Convert to emacs modifiers. */
2169 mods = w32_kbd_mods_to_emacs (construct_console_modifiers (), wparam);
2171 return mods;
2174 unsigned int
2175 map_keypad_keys (unsigned int virt_key, unsigned int extended)
2177 if (virt_key < VK_CLEAR || virt_key > VK_DELETE)
2178 return virt_key;
2180 if (virt_key == VK_RETURN)
2181 return (extended ? VK_NUMPAD_ENTER : VK_RETURN);
2183 if (virt_key >= VK_PRIOR && virt_key <= VK_DOWN)
2184 return (!extended ? (VK_NUMPAD_PRIOR + (virt_key - VK_PRIOR)) : virt_key);
2186 if (virt_key == VK_INSERT || virt_key == VK_DELETE)
2187 return (!extended ? (VK_NUMPAD_INSERT + (virt_key - VK_INSERT)) : virt_key);
2189 if (virt_key == VK_CLEAR)
2190 return (!extended ? VK_NUMPAD_CLEAR : virt_key);
2192 return virt_key;
2195 /* List of special key combinations which w32 would normally capture,
2196 but Emacs should grab instead. Not directly visible to lisp, to
2197 simplify synchronization. Each item is an integer encoding a virtual
2198 key code and modifier combination to capture. */
2199 static Lisp_Object w32_grabbed_keys;
2201 #define HOTKEY(vk, mods) make_number (((vk) & 255) | ((mods) << 8))
2202 #define HOTKEY_ID(k) (XFASTINT (k) & 0xbfff)
2203 #define HOTKEY_VK_CODE(k) (XFASTINT (k) & 255)
2204 #define HOTKEY_MODIFIERS(k) (XFASTINT (k) >> 8)
2206 #define RAW_HOTKEY_ID(k) ((k) & 0xbfff)
2207 #define RAW_HOTKEY_VK_CODE(k) ((k) & 255)
2208 #define RAW_HOTKEY_MODIFIERS(k) ((k) >> 8)
2210 /* Register hot-keys for reserved key combinations when Emacs has
2211 keyboard focus, since this is the only way Emacs can receive key
2212 combinations like Alt-Tab which are used by the system. */
2214 static void
2215 register_hot_keys (HWND hwnd)
2217 Lisp_Object keylist;
2219 /* Use CONSP, since we are called asynchronously. */
2220 for (keylist = w32_grabbed_keys; CONSP (keylist); keylist = XCDR (keylist))
2222 Lisp_Object key = XCAR (keylist);
2224 /* Deleted entries get set to nil. */
2225 if (!INTEGERP (key))
2226 continue;
2228 RegisterHotKey (hwnd, HOTKEY_ID (key),
2229 HOTKEY_MODIFIERS (key), HOTKEY_VK_CODE (key));
2233 static void
2234 unregister_hot_keys (HWND hwnd)
2236 Lisp_Object keylist;
2238 for (keylist = w32_grabbed_keys; CONSP (keylist); keylist = XCDR (keylist))
2240 Lisp_Object key = XCAR (keylist);
2242 if (!INTEGERP (key))
2243 continue;
2245 UnregisterHotKey (hwnd, HOTKEY_ID (key));
2249 /* Main message dispatch loop. */
2251 static void
2252 w32_msg_pump (deferred_msg * msg_buf)
2254 MSG msg;
2255 int result;
2256 HWND focus_window;
2258 msh_mousewheel = RegisterWindowMessage (MSH_MOUSEWHEEL);
2260 while ((w32_unicode_gui ? GetMessageW : GetMessageA) (&msg, NULL, 0, 0))
2262 if (msg.hwnd == NULL)
2264 switch (msg.message)
2266 case WM_NULL:
2267 /* Produced by complete_deferred_msg; just ignore. */
2268 break;
2269 case WM_EMACS_CREATEWINDOW:
2270 /* Initialize COM for this window. Even though we don't use it,
2271 some third party shell extensions can cause it to be used in
2272 system dialogs, which causes a crash if it is not initialized.
2273 This is a known bug in Windows, which was fixed long ago, but
2274 the patch for XP is not publicly available until XP SP3,
2275 and older versions will never be patched. */
2276 CoInitialize (NULL);
2277 w32_createwindow ((struct frame *) msg.wParam);
2278 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2279 emacs_abort ();
2280 break;
2281 case WM_EMACS_SETLOCALE:
2282 SetThreadLocale (msg.wParam);
2283 /* Reply is not expected. */
2284 break;
2285 case WM_EMACS_SETKEYBOARDLAYOUT:
2286 result = (int) ActivateKeyboardLayout ((HKL) msg.wParam, 0);
2287 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
2288 result, 0))
2289 emacs_abort ();
2290 break;
2291 case WM_EMACS_REGISTER_HOT_KEY:
2292 focus_window = GetFocus ();
2293 if (focus_window != NULL)
2294 RegisterHotKey (focus_window,
2295 RAW_HOTKEY_ID (msg.wParam),
2296 RAW_HOTKEY_MODIFIERS (msg.wParam),
2297 RAW_HOTKEY_VK_CODE (msg.wParam));
2298 /* Reply is not expected. */
2299 break;
2300 case WM_EMACS_UNREGISTER_HOT_KEY:
2301 focus_window = GetFocus ();
2302 if (focus_window != NULL)
2303 UnregisterHotKey (focus_window, RAW_HOTKEY_ID (msg.wParam));
2304 /* Mark item as erased. NB: this code must be
2305 thread-safe. The next line is okay because the cons
2306 cell is never made into garbage and is not relocated by
2307 GC. */
2308 XSETCAR ((Lisp_Object) ((EMACS_INT) msg.lParam), Qnil);
2309 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2310 emacs_abort ();
2311 break;
2312 case WM_EMACS_TOGGLE_LOCK_KEY:
2314 int vk_code = (int) msg.wParam;
2315 int cur_state = (GetKeyState (vk_code) & 1);
2316 Lisp_Object new_state = (Lisp_Object) ((EMACS_INT) msg.lParam);
2318 /* NB: This code must be thread-safe. It is safe to
2319 call NILP because symbols are not relocated by GC,
2320 and pointer here is not touched by GC (so the markbit
2321 can't be set). Numbers are safe because they are
2322 immediate values. */
2323 if (NILP (new_state)
2324 || (NUMBERP (new_state)
2325 && ((XUINT (new_state)) & 1) != cur_state))
2327 one_w32_display_info.faked_key = vk_code;
2329 keybd_event ((BYTE) vk_code,
2330 (BYTE) MapVirtualKey (vk_code, 0),
2331 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2332 keybd_event ((BYTE) vk_code,
2333 (BYTE) MapVirtualKey (vk_code, 0),
2334 KEYEVENTF_EXTENDEDKEY | 0, 0);
2335 keybd_event ((BYTE) vk_code,
2336 (BYTE) MapVirtualKey (vk_code, 0),
2337 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2338 cur_state = !cur_state;
2340 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
2341 cur_state, 0))
2342 emacs_abort ();
2344 break;
2345 #ifdef MSG_DEBUG
2346 /* Broadcast messages make it here, so you need to be looking
2347 for something in particular for this to be useful. */
2348 default:
2349 DebPrint (("msg %x not expected by w32_msg_pump\n", msg.message));
2350 #endif
2353 else
2355 if (w32_unicode_gui)
2356 DispatchMessageW (&msg);
2357 else
2358 DispatchMessageA (&msg);
2361 /* Exit nested loop when our deferred message has completed. */
2362 if (msg_buf->completed)
2363 break;
2367 deferred_msg * deferred_msg_head;
2369 static deferred_msg *
2370 find_deferred_msg (HWND hwnd, UINT msg)
2372 deferred_msg * item;
2374 /* Don't actually need synchronization for read access, since
2375 modification of single pointer is always atomic. */
2376 /* enter_crit (); */
2378 for (item = deferred_msg_head; item != NULL; item = item->next)
2379 if (item->w32msg.msg.hwnd == hwnd
2380 && item->w32msg.msg.message == msg)
2381 break;
2383 /* leave_crit (); */
2385 return item;
2388 static LRESULT
2389 send_deferred_msg (deferred_msg * msg_buf,
2390 HWND hwnd,
2391 UINT msg,
2392 WPARAM wParam,
2393 LPARAM lParam)
2395 /* Only input thread can send deferred messages. */
2396 if (GetCurrentThreadId () != dwWindowsThreadId)
2397 emacs_abort ();
2399 /* It is an error to send a message that is already deferred. */
2400 if (find_deferred_msg (hwnd, msg) != NULL)
2401 emacs_abort ();
2403 /* Enforced synchronization is not needed because this is the only
2404 function that alters deferred_msg_head, and the following critical
2405 section is guaranteed to only be serially reentered (since only the
2406 input thread can call us). */
2408 /* enter_crit (); */
2410 msg_buf->completed = 0;
2411 msg_buf->next = deferred_msg_head;
2412 deferred_msg_head = msg_buf;
2413 my_post_msg (&msg_buf->w32msg, hwnd, msg, wParam, lParam);
2415 /* leave_crit (); */
2417 /* Start a new nested message loop to process other messages until
2418 this one is completed. */
2419 w32_msg_pump (msg_buf);
2421 deferred_msg_head = msg_buf->next;
2423 return msg_buf->result;
2426 void
2427 complete_deferred_msg (HWND hwnd, UINT msg, LRESULT result)
2429 deferred_msg * msg_buf = find_deferred_msg (hwnd, msg);
2431 if (msg_buf == NULL)
2432 /* Message may have been canceled, so don't abort. */
2433 return;
2435 msg_buf->result = result;
2436 msg_buf->completed = 1;
2438 /* Ensure input thread is woken so it notices the completion. */
2439 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
2442 static void
2443 cancel_all_deferred_msgs (void)
2445 deferred_msg * item;
2447 /* Don't actually need synchronization for read access, since
2448 modification of single pointer is always atomic. */
2449 /* enter_crit (); */
2451 for (item = deferred_msg_head; item != NULL; item = item->next)
2453 item->result = 0;
2454 item->completed = 1;
2457 /* leave_crit (); */
2459 /* Ensure input thread is woken so it notices the completion. */
2460 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
2463 DWORD WINAPI
2464 w32_msg_worker (void *arg)
2466 MSG msg;
2467 deferred_msg dummy_buf;
2469 /* Ensure our message queue is created */
2471 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
2473 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2474 emacs_abort ();
2476 memset (&dummy_buf, 0, sizeof (dummy_buf));
2477 dummy_buf.w32msg.msg.hwnd = NULL;
2478 dummy_buf.w32msg.msg.message = WM_NULL;
2480 /* This is the initial message loop which should only exit when the
2481 application quits. */
2482 w32_msg_pump (&dummy_buf);
2484 return 0;
2487 static void
2488 signal_user_input (void)
2490 /* Interrupt any lisp that wants to be interrupted by input. */
2491 if (!NILP (Vthrow_on_input))
2493 Vquit_flag = Vthrow_on_input;
2494 /* Doing a QUIT from this thread is a bad idea, since this
2495 unwinds the stack of the Lisp thread, and the Windows runtime
2496 rightfully barfs. Disabled. */
2497 #if 0
2498 /* If we're inside a function that wants immediate quits,
2499 do it now. */
2500 if (immediate_quit && NILP (Vinhibit_quit))
2502 immediate_quit = 0;
2503 QUIT;
2505 #endif
2510 static void
2511 post_character_message (HWND hwnd, UINT msg,
2512 WPARAM wParam, LPARAM lParam,
2513 DWORD modifiers)
2515 W32Msg wmsg;
2517 wmsg.dwModifiers = modifiers;
2519 /* Detect quit_char and set quit-flag directly. Note that we
2520 still need to post a message to ensure the main thread will be
2521 woken up if blocked in sys_select, but we do NOT want to post
2522 the quit_char message itself (because it will usually be as if
2523 the user had typed quit_char twice). Instead, we post a dummy
2524 message that has no particular effect. */
2526 int c = wParam;
2527 if (isalpha (c) && wmsg.dwModifiers == ctrl_modifier)
2528 c = make_ctrl_char (c) & 0377;
2529 if (c == quit_char
2530 || (wmsg.dwModifiers == 0
2531 && w32_quit_key && wParam == w32_quit_key))
2533 Vquit_flag = Qt;
2535 /* The choice of message is somewhat arbitrary, as long as
2536 the main thread handler just ignores it. */
2537 msg = WM_NULL;
2539 /* Interrupt any blocking system calls. */
2540 signal_quit ();
2542 /* As a safety precaution, forcibly complete any deferred
2543 messages. This is a kludge, but I don't see any particularly
2544 clean way to handle the situation where a deferred message is
2545 "dropped" in the lisp thread, and will thus never be
2546 completed, eg. by the user trying to activate the menubar
2547 when the lisp thread is busy, and then typing C-g when the
2548 menubar doesn't open promptly (with the result that the
2549 menubar never responds at all because the deferred
2550 WM_INITMENU message is never completed). Another problem
2551 situation is when the lisp thread calls SendMessage (to send
2552 a window manager command) when a message has been deferred;
2553 the lisp thread gets blocked indefinitely waiting for the
2554 deferred message to be completed, which itself is waiting for
2555 the lisp thread to respond.
2557 Note that we don't want to block the input thread waiting for
2558 a response from the lisp thread (although that would at least
2559 solve the deadlock problem above), because we want to be able
2560 to receive C-g to interrupt the lisp thread. */
2561 cancel_all_deferred_msgs ();
2563 else
2564 signal_user_input ();
2567 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2570 /* Main window procedure */
2572 static LRESULT CALLBACK
2573 w32_wnd_proc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
2575 struct frame *f;
2576 struct w32_display_info *dpyinfo = &one_w32_display_info;
2577 W32Msg wmsg;
2578 int windows_translate;
2579 int key;
2581 /* Note that it is okay to call x_window_to_frame, even though we are
2582 not running in the main lisp thread, because frame deletion
2583 requires the lisp thread to synchronize with this thread. Thus, if
2584 a frame struct is returned, it can be used without concern that the
2585 lisp thread might make it disappear while we are using it.
2587 NB. Walking the frame list in this thread is safe (as long as
2588 writes of Lisp_Object slots are atomic, which they are on Windows).
2589 Although delete-frame can destructively modify the frame list while
2590 we are walking it, a garbage collection cannot occur until after
2591 delete-frame has synchronized with this thread.
2593 It is also safe to use functions that make GDI calls, such as
2594 w32_clear_rect, because these functions must obtain a DC handle
2595 from the frame struct using get_frame_dc which is thread-aware. */
2597 switch (msg)
2599 case WM_ERASEBKGND:
2600 f = x_window_to_frame (dpyinfo, hwnd);
2601 if (f)
2603 HDC hdc = get_frame_dc (f);
2604 GetUpdateRect (hwnd, &wmsg.rect, FALSE);
2605 w32_clear_rect (f, hdc, &wmsg.rect);
2606 release_frame_dc (f, hdc);
2608 #if defined (W32_DEBUG_DISPLAY)
2609 DebPrint (("WM_ERASEBKGND (frame %p): erasing %d,%d-%d,%d\n",
2611 wmsg.rect.left, wmsg.rect.top,
2612 wmsg.rect.right, wmsg.rect.bottom));
2613 #endif /* W32_DEBUG_DISPLAY */
2615 return 1;
2616 case WM_PALETTECHANGED:
2617 /* ignore our own changes */
2618 if ((HWND)wParam != hwnd)
2620 f = x_window_to_frame (dpyinfo, hwnd);
2621 if (f)
2622 /* get_frame_dc will realize our palette and force all
2623 frames to be redrawn if needed. */
2624 release_frame_dc (f, get_frame_dc (f));
2626 return 0;
2627 case WM_PAINT:
2629 PAINTSTRUCT paintStruct;
2630 RECT update_rect;
2631 memset (&update_rect, 0, sizeof (update_rect));
2633 f = x_window_to_frame (dpyinfo, hwnd);
2634 if (f == 0)
2636 DebPrint (("WM_PAINT received for unknown window %p\n", hwnd));
2637 return 0;
2640 /* MSDN Docs say not to call BeginPaint if GetUpdateRect
2641 fails. Apparently this can happen under some
2642 circumstances. */
2643 if (GetUpdateRect (hwnd, &update_rect, FALSE) || !w32_strict_painting)
2645 enter_crit ();
2646 BeginPaint (hwnd, &paintStruct);
2648 /* The rectangles returned by GetUpdateRect and BeginPaint
2649 do not always match. Play it safe by assuming both areas
2650 are invalid. */
2651 UnionRect (&(wmsg.rect), &update_rect, &(paintStruct.rcPaint));
2653 #if defined (W32_DEBUG_DISPLAY)
2654 DebPrint (("WM_PAINT (frame %p): painting %d,%d-%d,%d\n",
2656 wmsg.rect.left, wmsg.rect.top,
2657 wmsg.rect.right, wmsg.rect.bottom));
2658 DebPrint ((" [update region is %d,%d-%d,%d]\n",
2659 update_rect.left, update_rect.top,
2660 update_rect.right, update_rect.bottom));
2661 #endif
2662 EndPaint (hwnd, &paintStruct);
2663 leave_crit ();
2665 /* Change the message type to prevent Windows from
2666 combining WM_PAINT messages in the Lisp thread's queue,
2667 since Windows assumes that each message queue is
2668 dedicated to one frame and does not bother checking
2669 that hwnd matches before combining them. */
2670 my_post_msg (&wmsg, hwnd, WM_EMACS_PAINT, wParam, lParam);
2672 return 0;
2675 /* If GetUpdateRect returns 0 (meaning there is no update
2676 region), assume the whole window needs to be repainted. */
2677 GetClientRect (hwnd, &wmsg.rect);
2678 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2679 return 0;
2682 case WM_INPUTLANGCHANGE:
2683 /* Inform lisp thread of keyboard layout changes. */
2684 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2686 /* Clear dead keys in the keyboard state; for simplicity only
2687 preserve modifier key states. */
2689 int i;
2690 BYTE keystate[256];
2692 GetKeyboardState (keystate);
2693 for (i = 0; i < 256; i++)
2694 if (1
2695 && i != VK_SHIFT
2696 && i != VK_LSHIFT
2697 && i != VK_RSHIFT
2698 && i != VK_CAPITAL
2699 && i != VK_NUMLOCK
2700 && i != VK_SCROLL
2701 && i != VK_CONTROL
2702 && i != VK_LCONTROL
2703 && i != VK_RCONTROL
2704 && i != VK_MENU
2705 && i != VK_LMENU
2706 && i != VK_RMENU
2707 && i != VK_LWIN
2708 && i != VK_RWIN)
2709 keystate[i] = 0;
2710 SetKeyboardState (keystate);
2712 goto dflt;
2714 case WM_HOTKEY:
2715 /* Synchronize hot keys with normal input. */
2716 PostMessage (hwnd, WM_KEYDOWN, HIWORD (lParam), 0);
2717 return (0);
2719 case WM_KEYUP:
2720 case WM_SYSKEYUP:
2721 record_keyup (wParam, lParam);
2722 goto dflt;
2724 case WM_KEYDOWN:
2725 case WM_SYSKEYDOWN:
2726 /* Ignore keystrokes we fake ourself; see below. */
2727 if (dpyinfo->faked_key == wParam)
2729 dpyinfo->faked_key = 0;
2730 /* Make sure TranslateMessage sees them though (as long as
2731 they don't produce WM_CHAR messages). This ensures that
2732 indicator lights are toggled promptly on Windows 9x, for
2733 example. */
2734 if (wParam < 256 && lispy_function_keys[wParam])
2736 windows_translate = 1;
2737 goto translate;
2739 return 0;
2742 /* Synchronize modifiers with current keystroke. */
2743 sync_modifiers ();
2744 record_keydown (wParam, lParam);
2745 wParam = map_keypad_keys (wParam, (lParam & 0x1000000L) != 0);
2747 windows_translate = 0;
2749 switch (wParam)
2751 case VK_LWIN:
2752 if (NILP (Vw32_pass_lwindow_to_system))
2754 /* Prevent system from acting on keyup (which opens the
2755 Start menu if no other key was pressed) by simulating a
2756 press of Space which we will ignore. */
2757 if (GetAsyncKeyState (wParam) & 1)
2759 if (NUMBERP (Vw32_phantom_key_code))
2760 key = XUINT (Vw32_phantom_key_code) & 255;
2761 else
2762 key = VK_SPACE;
2763 dpyinfo->faked_key = key;
2764 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
2767 if (!NILP (Vw32_lwindow_modifier))
2768 return 0;
2769 break;
2770 case VK_RWIN:
2771 if (NILP (Vw32_pass_rwindow_to_system))
2773 if (GetAsyncKeyState (wParam) & 1)
2775 if (NUMBERP (Vw32_phantom_key_code))
2776 key = XUINT (Vw32_phantom_key_code) & 255;
2777 else
2778 key = VK_SPACE;
2779 dpyinfo->faked_key = key;
2780 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
2783 if (!NILP (Vw32_rwindow_modifier))
2784 return 0;
2785 break;
2786 case VK_APPS:
2787 if (!NILP (Vw32_apps_modifier))
2788 return 0;
2789 break;
2790 case VK_MENU:
2791 if (NILP (Vw32_pass_alt_to_system))
2792 /* Prevent DefWindowProc from activating the menu bar if an
2793 Alt key is pressed and released by itself. */
2794 return 0;
2795 windows_translate = 1;
2796 break;
2797 case VK_CAPITAL:
2798 /* Decide whether to treat as modifier or function key. */
2799 if (NILP (Vw32_enable_caps_lock))
2800 goto disable_lock_key;
2801 windows_translate = 1;
2802 break;
2803 case VK_NUMLOCK:
2804 /* Decide whether to treat as modifier or function key. */
2805 if (NILP (Vw32_enable_num_lock))
2806 goto disable_lock_key;
2807 windows_translate = 1;
2808 break;
2809 case VK_SCROLL:
2810 /* Decide whether to treat as modifier or function key. */
2811 if (NILP (Vw32_scroll_lock_modifier))
2812 goto disable_lock_key;
2813 windows_translate = 1;
2814 break;
2815 disable_lock_key:
2816 /* Ensure the appropriate lock key state (and indicator light)
2817 remains in the same state. We do this by faking another
2818 press of the relevant key. Apparently, this really is the
2819 only way to toggle the state of the indicator lights. */
2820 dpyinfo->faked_key = wParam;
2821 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
2822 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2823 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
2824 KEYEVENTF_EXTENDEDKEY | 0, 0);
2825 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
2826 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2827 /* Ensure indicator lights are updated promptly on Windows 9x
2828 (TranslateMessage apparently does this), after forwarding
2829 input event. */
2830 post_character_message (hwnd, msg, wParam, lParam,
2831 w32_get_key_modifiers (wParam, lParam));
2832 windows_translate = 1;
2833 break;
2834 case VK_CONTROL:
2835 case VK_SHIFT:
2836 case VK_PROCESSKEY: /* Generated by IME. */
2837 windows_translate = 1;
2838 break;
2839 case VK_CANCEL:
2840 /* Windows maps Ctrl-Pause (aka Ctrl-Break) into VK_CANCEL,
2841 which is confusing for purposes of key binding; convert
2842 VK_CANCEL events into VK_PAUSE events. */
2843 wParam = VK_PAUSE;
2844 break;
2845 case VK_PAUSE:
2846 /* Windows maps Ctrl-NumLock into VK_PAUSE, which is confusing
2847 for purposes of key binding; convert these back into
2848 VK_NUMLOCK events, at least when we want to see NumLock key
2849 presses. (Note that there is never any possibility that
2850 VK_PAUSE with Ctrl really is C-Pause as per above.) */
2851 if (NILP (Vw32_enable_num_lock) && modifier_set (VK_CONTROL))
2852 wParam = VK_NUMLOCK;
2853 break;
2854 default:
2855 /* If not defined as a function key, change it to a WM_CHAR message. */
2856 if (wParam > 255 || !lispy_function_keys[wParam])
2858 DWORD modifiers = construct_console_modifiers ();
2860 if (!NILP (Vw32_recognize_altgr)
2861 && modifier_set (VK_LCONTROL) && modifier_set (VK_RMENU))
2863 /* Always let TranslateMessage handle AltGr key chords;
2864 for some reason, ToAscii doesn't always process AltGr
2865 chords correctly. */
2866 windows_translate = 1;
2868 else if ((modifiers & (~SHIFT_PRESSED & ~CAPSLOCK_ON)) != 0)
2870 /* Handle key chords including any modifiers other
2871 than shift directly, in order to preserve as much
2872 modifier information as possible. */
2873 if ('A' <= wParam && wParam <= 'Z')
2875 /* Don't translate modified alphabetic keystrokes,
2876 so the user doesn't need to constantly switch
2877 layout to type control or meta keystrokes when
2878 the normal layout translates alphabetic
2879 characters to non-ascii characters. */
2880 if (!modifier_set (VK_SHIFT))
2881 wParam += ('a' - 'A');
2882 msg = WM_CHAR;
2884 else
2886 /* Try to handle other keystrokes by determining the
2887 base character (ie. translating the base key plus
2888 shift modifier). */
2889 int add;
2890 KEY_EVENT_RECORD key;
2892 key.bKeyDown = TRUE;
2893 key.wRepeatCount = 1;
2894 key.wVirtualKeyCode = wParam;
2895 key.wVirtualScanCode = (lParam & 0xFF0000) >> 16;
2896 key.uChar.AsciiChar = 0;
2897 key.dwControlKeyState = modifiers;
2899 add = w32_kbd_patch_key (&key, w32_keyboard_codepage);
2900 /* 0 means an unrecognized keycode, negative means
2901 dead key. Ignore both. */
2902 while (--add >= 0)
2904 /* Forward asciified character sequence. */
2905 post_character_message
2906 (hwnd, WM_CHAR,
2907 (unsigned char) key.uChar.AsciiChar, lParam,
2908 w32_get_key_modifiers (wParam, lParam));
2909 w32_kbd_patch_key (&key, w32_keyboard_codepage);
2911 return 0;
2914 else
2916 /* Let TranslateMessage handle everything else. */
2917 windows_translate = 1;
2922 translate:
2923 if (windows_translate)
2925 MSG windows_msg = { hwnd, msg, wParam, lParam, 0, {0,0} };
2926 windows_msg.time = GetMessageTime ();
2927 TranslateMessage (&windows_msg);
2928 goto dflt;
2931 /* Fall through */
2933 case WM_SYSCHAR:
2934 case WM_CHAR:
2935 if (wParam > 255 )
2937 W32Msg wmsg;
2939 wmsg.dwModifiers = w32_get_key_modifiers (wParam, lParam);
2940 signal_user_input ();
2941 my_post_msg (&wmsg, hwnd, WM_UNICHAR, wParam, lParam);
2944 else
2945 post_character_message (hwnd, msg, wParam, lParam,
2946 w32_get_key_modifiers (wParam, lParam));
2947 break;
2949 case WM_UNICHAR:
2950 /* WM_UNICHAR looks promising from the docs, but the exact
2951 circumstances in which TranslateMessage sends it is one of those
2952 Microsoft secret API things that EU and US courts are supposed
2953 to have put a stop to already. Spy++ shows it being sent to Notepad
2954 and other MS apps, but never to Emacs.
2956 Some third party IMEs send it in accordance with the official
2957 documentation though, so handle it here.
2959 UNICODE_NOCHAR is used to test for support for this message.
2960 TRUE indicates that the message is supported. */
2961 if (wParam == UNICODE_NOCHAR)
2962 return TRUE;
2965 W32Msg wmsg;
2966 wmsg.dwModifiers = w32_get_key_modifiers (wParam, lParam);
2967 signal_user_input ();
2968 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2970 break;
2972 case WM_IME_CHAR:
2973 /* If we can't get the IME result as Unicode, use default processing,
2974 which will at least allow characters decodable in the system locale
2975 get through. */
2976 if (!get_composition_string_fn)
2977 goto dflt;
2979 else if (!ignore_ime_char)
2981 wchar_t * buffer;
2982 int size, i;
2983 W32Msg wmsg;
2984 HIMC context = get_ime_context_fn (hwnd);
2985 wmsg.dwModifiers = w32_get_key_modifiers (wParam, lParam);
2986 /* Get buffer size. */
2987 size = get_composition_string_fn (context, GCS_RESULTSTR, NULL, 0);
2988 buffer = alloca (size);
2989 size = get_composition_string_fn (context, GCS_RESULTSTR,
2990 buffer, size);
2991 release_ime_context_fn (hwnd, context);
2993 signal_user_input ();
2994 for (i = 0; i < size / sizeof (wchar_t); i++)
2996 my_post_msg (&wmsg, hwnd, WM_UNICHAR, (WPARAM) buffer[i],
2997 lParam);
2999 /* Ignore the messages for the rest of the
3000 characters in the string that was output above. */
3001 ignore_ime_char = (size / sizeof (wchar_t)) - 1;
3003 else
3004 ignore_ime_char--;
3006 break;
3008 case WM_IME_STARTCOMPOSITION:
3009 if (!set_ime_composition_window_fn)
3010 goto dflt;
3011 else
3013 COMPOSITIONFORM form;
3014 HIMC context;
3015 struct window *w;
3017 f = x_window_to_frame (dpyinfo, hwnd);
3018 w = XWINDOW (FRAME_SELECTED_WINDOW (f));
3020 form.dwStyle = CFS_RECT;
3021 form.ptCurrentPos.x = w32_system_caret_x;
3022 form.ptCurrentPos.y = w32_system_caret_y;
3024 form.rcArea.left = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, 0);
3025 form.rcArea.top = (WINDOW_TOP_EDGE_Y (w)
3026 + WINDOW_HEADER_LINE_HEIGHT (w));
3027 form.rcArea.right = (WINDOW_BOX_RIGHT_EDGE_X (w)
3028 - WINDOW_RIGHT_MARGIN_WIDTH (w)
3029 - WINDOW_RIGHT_FRINGE_WIDTH (w));
3030 form.rcArea.bottom = (WINDOW_BOTTOM_EDGE_Y (w)
3031 - WINDOW_MODE_LINE_HEIGHT (w));
3033 context = get_ime_context_fn (hwnd);
3035 if (!context)
3036 break;
3038 set_ime_composition_window_fn (context, &form);
3039 release_ime_context_fn (hwnd, context);
3041 break;
3043 case WM_IME_ENDCOMPOSITION:
3044 ignore_ime_char = 0;
3045 goto dflt;
3047 /* Simulate middle mouse button events when left and right buttons
3048 are used together, but only if user has two button mouse. */
3049 case WM_LBUTTONDOWN:
3050 case WM_RBUTTONDOWN:
3051 if (w32_num_mouse_buttons > 2)
3052 goto handle_plain_button;
3055 int this = (msg == WM_LBUTTONDOWN) ? LMOUSE : RMOUSE;
3056 int other = (msg == WM_LBUTTONDOWN) ? RMOUSE : LMOUSE;
3058 if (button_state & this)
3059 return 0;
3061 if (button_state == 0)
3062 SetCapture (hwnd);
3064 button_state |= this;
3066 if (button_state & other)
3068 if (mouse_button_timer)
3070 KillTimer (hwnd, mouse_button_timer);
3071 mouse_button_timer = 0;
3073 /* Generate middle mouse event instead. */
3074 msg = WM_MBUTTONDOWN;
3075 button_state |= MMOUSE;
3077 else if (button_state & MMOUSE)
3079 /* Ignore button event if we've already generated a
3080 middle mouse down event. This happens if the
3081 user releases and press one of the two buttons
3082 after we've faked a middle mouse event. */
3083 return 0;
3085 else
3087 /* Flush out saved message. */
3088 post_msg (&saved_mouse_button_msg);
3090 wmsg.dwModifiers = w32_get_modifiers ();
3091 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3092 signal_user_input ();
3094 /* Clear message buffer. */
3095 saved_mouse_button_msg.msg.hwnd = 0;
3097 else
3099 /* Hold onto message for now. */
3100 mouse_button_timer =
3101 SetTimer (hwnd, MOUSE_BUTTON_ID,
3102 w32_mouse_button_tolerance, NULL);
3103 saved_mouse_button_msg.msg.hwnd = hwnd;
3104 saved_mouse_button_msg.msg.message = msg;
3105 saved_mouse_button_msg.msg.wParam = wParam;
3106 saved_mouse_button_msg.msg.lParam = lParam;
3107 saved_mouse_button_msg.msg.time = GetMessageTime ();
3108 saved_mouse_button_msg.dwModifiers = w32_get_modifiers ();
3111 return 0;
3113 case WM_LBUTTONUP:
3114 case WM_RBUTTONUP:
3115 if (w32_num_mouse_buttons > 2)
3116 goto handle_plain_button;
3119 int this = (msg == WM_LBUTTONUP) ? LMOUSE : RMOUSE;
3120 int other = (msg == WM_LBUTTONUP) ? RMOUSE : LMOUSE;
3122 if ((button_state & this) == 0)
3123 return 0;
3125 button_state &= ~this;
3127 if (button_state & MMOUSE)
3129 /* Only generate event when second button is released. */
3130 if ((button_state & other) == 0)
3132 msg = WM_MBUTTONUP;
3133 button_state &= ~MMOUSE;
3135 if (button_state) emacs_abort ();
3137 else
3138 return 0;
3140 else
3142 /* Flush out saved message if necessary. */
3143 if (saved_mouse_button_msg.msg.hwnd)
3145 post_msg (&saved_mouse_button_msg);
3148 wmsg.dwModifiers = w32_get_modifiers ();
3149 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3150 signal_user_input ();
3152 /* Always clear message buffer and cancel timer. */
3153 saved_mouse_button_msg.msg.hwnd = 0;
3154 KillTimer (hwnd, mouse_button_timer);
3155 mouse_button_timer = 0;
3157 if (button_state == 0)
3158 ReleaseCapture ();
3160 return 0;
3162 case WM_XBUTTONDOWN:
3163 case WM_XBUTTONUP:
3164 if (w32_pass_extra_mouse_buttons_to_system)
3165 goto dflt;
3166 /* else fall through and process them. */
3167 case WM_MBUTTONDOWN:
3168 case WM_MBUTTONUP:
3169 handle_plain_button:
3171 BOOL up;
3172 int button;
3174 /* Ignore middle and extra buttons as long as the menu is active. */
3175 f = x_window_to_frame (dpyinfo, hwnd);
3176 if (f && f->output_data.w32->menubar_active)
3177 return 0;
3179 if (parse_button (msg, HIWORD (wParam), &button, &up))
3181 if (up) ReleaseCapture ();
3182 else SetCapture (hwnd);
3183 button = (button == 0) ? LMOUSE :
3184 ((button == 1) ? MMOUSE : RMOUSE);
3185 if (up)
3186 button_state &= ~button;
3187 else
3188 button_state |= button;
3192 wmsg.dwModifiers = w32_get_modifiers ();
3193 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3194 signal_user_input ();
3196 /* Need to return true for XBUTTON messages, false for others,
3197 to indicate that we processed the message. */
3198 return (msg == WM_XBUTTONDOWN || msg == WM_XBUTTONUP);
3200 case WM_MOUSEMOVE:
3201 /* Ignore mouse movements as long as the menu is active. These
3202 movements are processed by the window manager anyway, and
3203 it's wrong to handle them as if they happened on the
3204 underlying frame. */
3205 f = x_window_to_frame (dpyinfo, hwnd);
3206 if (f && f->output_data.w32->menubar_active)
3207 return 0;
3209 /* If the mouse has just moved into the frame, start tracking
3210 it, so we will be notified when it leaves the frame. Mouse
3211 tracking only works under W98 and NT4 and later. On earlier
3212 versions, there is no way of telling when the mouse leaves the
3213 frame, so we just have to put up with help-echo and mouse
3214 highlighting remaining while the frame is not active. */
3215 if (track_mouse_event_fn && !track_mouse_window)
3217 TRACKMOUSEEVENT tme;
3218 tme.cbSize = sizeof (tme);
3219 tme.dwFlags = TME_LEAVE;
3220 tme.hwndTrack = hwnd;
3222 track_mouse_event_fn (&tme);
3223 track_mouse_window = hwnd;
3225 case WM_VSCROLL:
3226 if (w32_mouse_move_interval <= 0
3227 || (msg == WM_MOUSEMOVE && button_state == 0))
3229 wmsg.dwModifiers = w32_get_modifiers ();
3230 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3231 return 0;
3234 /* Hang onto mouse move and scroll messages for a bit, to avoid
3235 sending such events to Emacs faster than it can process them.
3236 If we get more events before the timer from the first message
3237 expires, we just replace the first message. */
3239 if (saved_mouse_move_msg.msg.hwnd == 0)
3240 mouse_move_timer =
3241 SetTimer (hwnd, MOUSE_MOVE_ID,
3242 w32_mouse_move_interval, NULL);
3244 /* Hold onto message for now. */
3245 saved_mouse_move_msg.msg.hwnd = hwnd;
3246 saved_mouse_move_msg.msg.message = msg;
3247 saved_mouse_move_msg.msg.wParam = wParam;
3248 saved_mouse_move_msg.msg.lParam = lParam;
3249 saved_mouse_move_msg.msg.time = GetMessageTime ();
3250 saved_mouse_move_msg.dwModifiers = w32_get_modifiers ();
3252 return 0;
3254 case WM_MOUSEWHEEL:
3255 case WM_DROPFILES:
3256 wmsg.dwModifiers = w32_get_modifiers ();
3257 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3258 signal_user_input ();
3259 return 0;
3261 case WM_APPCOMMAND:
3262 if (w32_pass_multimedia_buttons_to_system)
3263 goto dflt;
3264 /* Otherwise, pass to lisp, the same way we do with mousehwheel. */
3265 case WM_MOUSEHWHEEL:
3266 wmsg.dwModifiers = w32_get_modifiers ();
3267 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3268 signal_user_input ();
3269 /* Non-zero must be returned when WM_MOUSEHWHEEL messages are
3270 handled, to prevent the system trying to handle it by faking
3271 scroll bar events. */
3272 return 1;
3274 case WM_TIMER:
3275 /* Flush out saved messages if necessary. */
3276 if (wParam == mouse_button_timer)
3278 if (saved_mouse_button_msg.msg.hwnd)
3280 post_msg (&saved_mouse_button_msg);
3281 signal_user_input ();
3282 saved_mouse_button_msg.msg.hwnd = 0;
3284 KillTimer (hwnd, mouse_button_timer);
3285 mouse_button_timer = 0;
3287 else if (wParam == mouse_move_timer)
3289 if (saved_mouse_move_msg.msg.hwnd)
3291 post_msg (&saved_mouse_move_msg);
3292 saved_mouse_move_msg.msg.hwnd = 0;
3294 KillTimer (hwnd, mouse_move_timer);
3295 mouse_move_timer = 0;
3297 else if (wParam == menu_free_timer)
3299 KillTimer (hwnd, menu_free_timer);
3300 menu_free_timer = 0;
3301 f = x_window_to_frame (dpyinfo, hwnd);
3302 /* If a popup menu is active, don't wipe its strings. */
3303 if (menubar_in_use
3304 && current_popup_menu == NULL)
3306 /* Free memory used by owner-drawn and help-echo strings. */
3307 w32_free_menu_strings (hwnd);
3308 if (f)
3309 f->output_data.w32->menubar_active = 0;
3310 menubar_in_use = 0;
3313 return 0;
3315 case WM_NCACTIVATE:
3316 /* Windows doesn't send us focus messages when putting up and
3317 taking down a system popup dialog as for Ctrl-Alt-Del on Windows 95.
3318 The only indication we get that something happened is receiving
3319 this message afterwards. So this is a good time to reset our
3320 keyboard modifiers' state. */
3321 reset_modifiers ();
3322 goto dflt;
3324 case WM_INITMENU:
3325 button_state = 0;
3326 ReleaseCapture ();
3327 /* We must ensure menu bar is fully constructed and up to date
3328 before allowing user interaction with it. To achieve this
3329 we send this message to the lisp thread and wait for a
3330 reply (whose value is not actually needed) to indicate that
3331 the menu bar is now ready for use, so we can now return.
3333 To remain responsive in the meantime, we enter a nested message
3334 loop that can process all other messages.
3336 However, we skip all this if the message results from calling
3337 TrackPopupMenu - in fact, we must NOT attempt to send the lisp
3338 thread a message because it is blocked on us at this point. We
3339 set menubar_active before calling TrackPopupMenu to indicate
3340 this (there is no possibility of confusion with real menubar
3341 being active). */
3343 f = x_window_to_frame (dpyinfo, hwnd);
3344 if (f
3345 && (f->output_data.w32->menubar_active
3346 /* We can receive this message even in the absence of a
3347 menubar (ie. when the system menu is activated) - in this
3348 case we do NOT want to forward the message, otherwise it
3349 will cause the menubar to suddenly appear when the user
3350 had requested it to be turned off! */
3351 || f->output_data.w32->menubar_widget == NULL))
3352 return 0;
3355 deferred_msg msg_buf;
3357 /* Detect if message has already been deferred; in this case
3358 we cannot return any sensible value to ignore this. */
3359 if (find_deferred_msg (hwnd, msg) != NULL)
3360 emacs_abort ();
3362 menubar_in_use = 1;
3364 return send_deferred_msg (&msg_buf, hwnd, msg, wParam, lParam);
3367 case WM_EXITMENULOOP:
3368 f = x_window_to_frame (dpyinfo, hwnd);
3370 /* If a menu is still active, check again after a short delay,
3371 since Windows often (always?) sends the WM_EXITMENULOOP
3372 before the corresponding WM_COMMAND message.
3373 Don't do this if a popup menu is active, since it is only
3374 menubar menus that require cleaning up in this way.
3376 if (f && menubar_in_use && current_popup_menu == NULL)
3377 menu_free_timer = SetTimer (hwnd, MENU_FREE_ID, MENU_FREE_DELAY, NULL);
3379 /* If hourglass cursor should be displayed, display it now. */
3380 if (f && f->output_data.w32->hourglass_p)
3381 SetCursor (f->output_data.w32->hourglass_cursor);
3383 goto dflt;
3385 case WM_MENUSELECT:
3386 /* Direct handling of help_echo in menus. Should be safe now
3387 that we generate the help_echo by placing a help event in the
3388 keyboard buffer. */
3390 HMENU menu = (HMENU) lParam;
3391 UINT menu_item = (UINT) LOWORD (wParam);
3392 UINT flags = (UINT) HIWORD (wParam);
3394 w32_menu_display_help (hwnd, menu, menu_item, flags);
3396 return 0;
3398 case WM_MEASUREITEM:
3399 f = x_window_to_frame (dpyinfo, hwnd);
3400 if (f)
3402 MEASUREITEMSTRUCT * pMis = (MEASUREITEMSTRUCT *) lParam;
3404 if (pMis->CtlType == ODT_MENU)
3406 /* Work out dimensions for popup menu titles. */
3407 char * title = (char *) pMis->itemData;
3408 HDC hdc = GetDC (hwnd);
3409 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
3410 LOGFONT menu_logfont;
3411 HFONT old_font;
3412 SIZE size;
3414 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
3415 menu_logfont.lfWeight = FW_BOLD;
3416 menu_font = CreateFontIndirect (&menu_logfont);
3417 old_font = SelectObject (hdc, menu_font);
3419 pMis->itemHeight = GetSystemMetrics (SM_CYMENUSIZE);
3420 if (title)
3422 if (unicode_append_menu)
3423 GetTextExtentPoint32W (hdc, (WCHAR *) title,
3424 wcslen ((WCHAR *) title),
3425 &size);
3426 else
3427 GetTextExtentPoint32 (hdc, title, strlen (title), &size);
3429 pMis->itemWidth = size.cx;
3430 if (pMis->itemHeight < size.cy)
3431 pMis->itemHeight = size.cy;
3433 else
3434 pMis->itemWidth = 0;
3436 SelectObject (hdc, old_font);
3437 DeleteObject (menu_font);
3438 ReleaseDC (hwnd, hdc);
3439 return TRUE;
3442 return 0;
3444 case WM_DRAWITEM:
3445 f = x_window_to_frame (dpyinfo, hwnd);
3446 if (f)
3448 DRAWITEMSTRUCT * pDis = (DRAWITEMSTRUCT *) lParam;
3450 if (pDis->CtlType == ODT_MENU)
3452 /* Draw popup menu title. */
3453 char * title = (char *) pDis->itemData;
3454 if (title)
3456 HDC hdc = pDis->hDC;
3457 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
3458 LOGFONT menu_logfont;
3459 HFONT old_font;
3461 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
3462 menu_logfont.lfWeight = FW_BOLD;
3463 menu_font = CreateFontIndirect (&menu_logfont);
3464 old_font = SelectObject (hdc, menu_font);
3466 /* Always draw title as if not selected. */
3467 if (unicode_append_menu)
3468 ExtTextOutW (hdc,
3469 pDis->rcItem.left
3470 + GetSystemMetrics (SM_CXMENUCHECK),
3471 pDis->rcItem.top,
3472 ETO_OPAQUE, &pDis->rcItem,
3473 (WCHAR *) title,
3474 wcslen ((WCHAR *) title), NULL);
3475 else
3476 ExtTextOut (hdc,
3477 pDis->rcItem.left
3478 + GetSystemMetrics (SM_CXMENUCHECK),
3479 pDis->rcItem.top,
3480 ETO_OPAQUE, &pDis->rcItem,
3481 title, strlen (title), NULL);
3483 SelectObject (hdc, old_font);
3484 DeleteObject (menu_font);
3486 return TRUE;
3489 return 0;
3491 #if 0
3492 /* Still not right - can't distinguish between clicks in the
3493 client area of the frame from clicks forwarded from the scroll
3494 bars - may have to hook WM_NCHITTEST to remember the mouse
3495 position and then check if it is in the client area ourselves. */
3496 case WM_MOUSEACTIVATE:
3497 /* Discard the mouse click that activates a frame, allowing the
3498 user to click anywhere without changing point (or worse!).
3499 Don't eat mouse clicks on scrollbars though!! */
3500 if (LOWORD (lParam) == HTCLIENT )
3501 return MA_ACTIVATEANDEAT;
3502 goto dflt;
3503 #endif
3505 case WM_MOUSELEAVE:
3506 /* No longer tracking mouse. */
3507 track_mouse_window = NULL;
3509 case WM_ACTIVATEAPP:
3510 case WM_ACTIVATE:
3511 case WM_WINDOWPOSCHANGED:
3512 case WM_SHOWWINDOW:
3513 /* Inform lisp thread that a frame might have just been obscured
3514 or exposed, so should recheck visibility of all frames. */
3515 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3516 goto dflt;
3518 case WM_SETFOCUS:
3519 dpyinfo->faked_key = 0;
3520 reset_modifiers ();
3521 register_hot_keys (hwnd);
3522 goto command;
3523 case WM_KILLFOCUS:
3524 unregister_hot_keys (hwnd);
3525 button_state = 0;
3526 ReleaseCapture ();
3527 /* Relinquish the system caret. */
3528 if (w32_system_caret_hwnd)
3530 w32_visible_system_caret_hwnd = NULL;
3531 w32_system_caret_hwnd = NULL;
3532 DestroyCaret ();
3534 goto command;
3535 case WM_COMMAND:
3536 menubar_in_use = 0;
3537 f = x_window_to_frame (dpyinfo, hwnd);
3538 if (f && HIWORD (wParam) == 0)
3540 if (menu_free_timer)
3542 KillTimer (hwnd, menu_free_timer);
3543 menu_free_timer = 0;
3546 case WM_MOVE:
3547 case WM_SIZE:
3548 command:
3549 wmsg.dwModifiers = w32_get_modifiers ();
3550 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3551 goto dflt;
3553 case WM_DESTROY:
3554 CoUninitialize ();
3555 return 0;
3557 case WM_CLOSE:
3558 wmsg.dwModifiers = w32_get_modifiers ();
3559 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3560 return 0;
3562 case WM_WINDOWPOSCHANGING:
3563 /* Don't restrict the sizing of tip frames. */
3564 if (hwnd == tip_window)
3565 return 0;
3567 WINDOWPLACEMENT wp;
3568 LPWINDOWPOS lppos = (WINDOWPOS *) lParam;
3570 wp.length = sizeof (WINDOWPLACEMENT);
3571 GetWindowPlacement (hwnd, &wp);
3573 if (wp.showCmd != SW_SHOWMINIMIZED && (lppos->flags & SWP_NOSIZE) == 0)
3575 RECT rect;
3576 int wdiff;
3577 int hdiff;
3578 DWORD font_width;
3579 DWORD line_height;
3580 DWORD internal_border;
3581 DWORD scrollbar_extra;
3582 RECT wr;
3584 wp.length = sizeof (wp);
3585 GetWindowRect (hwnd, &wr);
3587 enter_crit ();
3589 font_width = GetWindowLong (hwnd, WND_FONTWIDTH_INDEX);
3590 line_height = GetWindowLong (hwnd, WND_LINEHEIGHT_INDEX);
3591 internal_border = GetWindowLong (hwnd, WND_BORDER_INDEX);
3592 scrollbar_extra = GetWindowLong (hwnd, WND_SCROLLBAR_INDEX);
3594 leave_crit ();
3596 memset (&rect, 0, sizeof (rect));
3597 AdjustWindowRect (&rect, GetWindowLong (hwnd, GWL_STYLE),
3598 GetMenu (hwnd) != NULL);
3600 /* Force width and height of client area to be exact
3601 multiples of the character cell dimensions. */
3602 wdiff = (lppos->cx - (rect.right - rect.left)
3603 - 2 * internal_border - scrollbar_extra)
3604 % font_width;
3605 hdiff = (lppos->cy - (rect.bottom - rect.top)
3606 - 2 * internal_border)
3607 % line_height;
3609 if (wdiff || hdiff)
3611 /* For right/bottom sizing we can just fix the sizes.
3612 However for top/left sizing we will need to fix the X
3613 and Y positions as well. */
3615 int cx_mintrack = GetSystemMetrics (SM_CXMINTRACK);
3616 int cy_mintrack = GetSystemMetrics (SM_CYMINTRACK);
3618 lppos->cx = max (lppos->cx - wdiff, cx_mintrack);
3619 lppos->cy = max (lppos->cy - hdiff, cy_mintrack);
3621 if (wp.showCmd != SW_SHOWMAXIMIZED
3622 && (lppos->flags & SWP_NOMOVE) == 0)
3624 if (lppos->x != wr.left || lppos->y != wr.top)
3626 lppos->x += wdiff;
3627 lppos->y += hdiff;
3629 else
3631 lppos->flags |= SWP_NOMOVE;
3635 return 0;
3640 goto dflt;
3642 case WM_GETMINMAXINFO:
3643 /* Hack to allow resizing the Emacs frame above the screen size.
3644 Note that Windows 9x limits coordinates to 16-bits. */
3645 ((LPMINMAXINFO) lParam)->ptMaxTrackSize.x = 32767;
3646 ((LPMINMAXINFO) lParam)->ptMaxTrackSize.y = 32767;
3647 return 0;
3649 case WM_SETCURSOR:
3650 if (LOWORD (lParam) == HTCLIENT)
3652 f = x_window_to_frame (dpyinfo, hwnd);
3653 if (f && f->output_data.w32->hourglass_p
3654 && !menubar_in_use && !current_popup_menu)
3655 SetCursor (f->output_data.w32->hourglass_cursor);
3656 else if (f)
3657 SetCursor (f->output_data.w32->current_cursor);
3658 return 0;
3660 goto dflt;
3662 case WM_EMACS_SETCURSOR:
3664 Cursor cursor = (Cursor) wParam;
3665 f = x_window_to_frame (dpyinfo, hwnd);
3666 if (f && cursor)
3668 f->output_data.w32->current_cursor = cursor;
3669 if (!f->output_data.w32->hourglass_p)
3670 SetCursor (cursor);
3672 return 0;
3675 case WM_EMACS_CREATESCROLLBAR:
3676 return (LRESULT) w32_createscrollbar ((struct frame *) wParam,
3677 (struct scroll_bar *) lParam);
3679 case WM_EMACS_SHOWWINDOW:
3680 return ShowWindow ((HWND) wParam, (WPARAM) lParam);
3682 case WM_EMACS_BRINGTOTOP:
3683 case WM_EMACS_SETFOREGROUND:
3685 HWND foreground_window;
3686 DWORD foreground_thread, retval;
3688 /* On NT 5.0, and apparently Windows 98, it is necessary to
3689 attach to the thread that currently has focus in order to
3690 pull the focus away from it. */
3691 foreground_window = GetForegroundWindow ();
3692 foreground_thread = GetWindowThreadProcessId (foreground_window, NULL);
3693 if (!foreground_window
3694 || foreground_thread == GetCurrentThreadId ()
3695 || !AttachThreadInput (GetCurrentThreadId (),
3696 foreground_thread, TRUE))
3697 foreground_thread = 0;
3699 retval = SetForegroundWindow ((HWND) wParam);
3700 if (msg == WM_EMACS_BRINGTOTOP)
3701 retval = BringWindowToTop ((HWND) wParam);
3703 /* Detach from the previous foreground thread. */
3704 if (foreground_thread)
3705 AttachThreadInput (GetCurrentThreadId (),
3706 foreground_thread, FALSE);
3708 return retval;
3711 case WM_EMACS_SETWINDOWPOS:
3713 WINDOWPOS * pos = (WINDOWPOS *) wParam;
3714 return SetWindowPos (hwnd, pos->hwndInsertAfter,
3715 pos->x, pos->y, pos->cx, pos->cy, pos->flags);
3718 case WM_EMACS_DESTROYWINDOW:
3719 DragAcceptFiles ((HWND) wParam, FALSE);
3720 return DestroyWindow ((HWND) wParam);
3722 case WM_EMACS_HIDE_CARET:
3723 return HideCaret (hwnd);
3725 case WM_EMACS_SHOW_CARET:
3726 return ShowCaret (hwnd);
3728 case WM_EMACS_DESTROY_CARET:
3729 w32_system_caret_hwnd = NULL;
3730 w32_visible_system_caret_hwnd = NULL;
3731 return DestroyCaret ();
3733 case WM_EMACS_TRACK_CARET:
3734 /* If there is currently no system caret, create one. */
3735 if (w32_system_caret_hwnd == NULL)
3737 /* Use the default caret width, and avoid changing it
3738 unnecessarily, as it confuses screen reader software. */
3739 w32_system_caret_hwnd = hwnd;
3740 CreateCaret (hwnd, NULL, 0,
3741 w32_system_caret_height);
3744 if (!SetCaretPos (w32_system_caret_x, w32_system_caret_y))
3745 return 0;
3746 /* Ensure visible caret gets turned on when requested. */
3747 else if (w32_use_visible_system_caret
3748 && w32_visible_system_caret_hwnd != hwnd)
3750 w32_visible_system_caret_hwnd = hwnd;
3751 return ShowCaret (hwnd);
3753 /* Ensure visible caret gets turned off when requested. */
3754 else if (!w32_use_visible_system_caret
3755 && w32_visible_system_caret_hwnd)
3757 w32_visible_system_caret_hwnd = NULL;
3758 return HideCaret (hwnd);
3760 else
3761 return 1;
3763 case WM_EMACS_TRACKPOPUPMENU:
3765 UINT flags;
3766 POINT *pos;
3767 int retval;
3768 pos = (POINT *)lParam;
3769 flags = TPM_CENTERALIGN;
3770 if (button_state & LMOUSE)
3771 flags |= TPM_LEFTBUTTON;
3772 else if (button_state & RMOUSE)
3773 flags |= TPM_RIGHTBUTTON;
3775 /* Remember we did a SetCapture on the initial mouse down event,
3776 so for safety, we make sure the capture is canceled now. */
3777 ReleaseCapture ();
3778 button_state = 0;
3780 /* Use menubar_active to indicate that WM_INITMENU is from
3781 TrackPopupMenu below, and should be ignored. */
3782 f = x_window_to_frame (dpyinfo, hwnd);
3783 if (f)
3784 f->output_data.w32->menubar_active = 1;
3786 if (TrackPopupMenu ((HMENU)wParam, flags, pos->x, pos->y,
3787 0, hwnd, NULL))
3789 MSG amsg;
3790 /* Eat any mouse messages during popupmenu */
3791 while (PeekMessage (&amsg, hwnd, WM_MOUSEFIRST, WM_MOUSELAST,
3792 PM_REMOVE));
3793 /* Get the menu selection, if any */
3794 if (PeekMessage (&amsg, hwnd, WM_COMMAND, WM_COMMAND, PM_REMOVE))
3796 retval = LOWORD (amsg.wParam);
3798 else
3800 retval = 0;
3803 else
3805 retval = -1;
3808 return retval;
3811 default:
3812 /* Check for messages registered at runtime. */
3813 if (msg == msh_mousewheel)
3815 wmsg.dwModifiers = w32_get_modifiers ();
3816 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3817 signal_user_input ();
3818 return 0;
3821 dflt:
3822 return (w32_unicode_gui ? DefWindowProcW : DefWindowProcA) (hwnd, msg, wParam, lParam);
3825 /* The most common default return code for handled messages is 0. */
3826 return 0;
3829 static void
3830 my_create_window (struct frame * f)
3832 MSG msg;
3834 if (!PostThreadMessage (dwWindowsThreadId, WM_EMACS_CREATEWINDOW, (WPARAM)f, 0))
3835 emacs_abort ();
3836 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
3840 /* Create a tooltip window. Unlike my_create_window, we do not do this
3841 indirectly via the Window thread, as we do not need to process Window
3842 messages for the tooltip. Creating tooltips indirectly also creates
3843 deadlocks when tooltips are created for menu items. */
3844 static void
3845 my_create_tip_window (struct frame *f)
3847 RECT rect;
3849 rect.left = rect.top = 0;
3850 rect.right = FRAME_PIXEL_WIDTH (f);
3851 rect.bottom = FRAME_PIXEL_HEIGHT (f);
3853 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
3854 FRAME_EXTERNAL_MENU_BAR (f));
3856 tip_window = FRAME_W32_WINDOW (f)
3857 = CreateWindow (EMACS_CLASS,
3858 f->namebuf,
3859 f->output_data.w32->dwStyle,
3860 f->left_pos,
3861 f->top_pos,
3862 rect.right - rect.left,
3863 rect.bottom - rect.top,
3864 FRAME_W32_WINDOW (SELECTED_FRAME ()), /* owner */
3865 NULL,
3866 hinst,
3867 NULL);
3869 if (tip_window)
3871 SetWindowLong (tip_window, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
3872 SetWindowLong (tip_window, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
3873 SetWindowLong (tip_window, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
3874 SetWindowLong (tip_window, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
3876 /* Tip frames have no scrollbars. */
3877 SetWindowLong (tip_window, WND_SCROLLBAR_INDEX, 0);
3879 /* Do this to discard the default setting specified by our parent. */
3880 ShowWindow (tip_window, SW_HIDE);
3885 /* Create and set up the w32 window for frame F. */
3887 static void
3888 w32_window (struct frame *f, long window_prompting, int minibuffer_only)
3890 block_input ();
3892 /* Use the resource name as the top-level window name
3893 for looking up resources. Make a non-Lisp copy
3894 for the window manager, so GC relocation won't bother it.
3896 Elsewhere we specify the window name for the window manager. */
3899 char *str = SSDATA (Vx_resource_name);
3900 f->namebuf = xmalloc (strlen (str) + 1);
3901 strcpy (f->namebuf, str);
3904 my_create_window (f);
3906 validate_x_resource_name ();
3908 /* x_set_name normally ignores requests to set the name if the
3909 requested name is the same as the current name. This is the one
3910 place where that assumption isn't correct; f->name is set, but
3911 the server hasn't been told. */
3913 Lisp_Object name;
3914 int explicit = f->explicit_name;
3916 f->explicit_name = 0;
3917 name = f->name;
3918 fset_name (f, Qnil);
3919 x_set_name (f, name, explicit);
3922 unblock_input ();
3924 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
3925 initialize_frame_menubar (f);
3927 if (FRAME_W32_WINDOW (f) == 0)
3928 error ("Unable to create window");
3931 /* Handle the icon stuff for this window. Perhaps later we might
3932 want an x_set_icon_position which can be called interactively as
3933 well. */
3935 static void
3936 x_icon (struct frame *f, Lisp_Object parms)
3938 Lisp_Object icon_x, icon_y;
3939 struct w32_display_info *dpyinfo = &one_w32_display_info;
3941 /* Set the position of the icon. Note that Windows 95 groups all
3942 icons in the tray. */
3943 icon_x = x_get_arg (dpyinfo, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
3944 icon_y = x_get_arg (dpyinfo, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
3945 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
3947 CHECK_NUMBER (icon_x);
3948 CHECK_NUMBER (icon_y);
3950 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
3951 error ("Both left and top icon corners of icon must be specified");
3953 block_input ();
3955 if (! EQ (icon_x, Qunbound))
3956 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
3958 #if 0 /* TODO */
3959 /* Start up iconic or window? */
3960 x_wm_set_window_state
3961 (f, (EQ (x_get_arg (dpyinfo, parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL), Qicon)
3962 ? IconicState
3963 : NormalState));
3965 x_text_icon (f, SSDATA ((!NILP (f->icon_name)
3966 ? f->icon_name
3967 : f->name)));
3968 #endif
3970 unblock_input ();
3974 static void
3975 x_make_gc (struct frame *f)
3977 XGCValues gc_values;
3979 block_input ();
3981 /* Create the GC's of this frame.
3982 Note that many default values are used. */
3984 /* Normal video */
3985 gc_values.font = FRAME_FONT (f);
3987 /* Cursor has cursor-color background, background-color foreground. */
3988 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
3989 gc_values.background = f->output_data.w32->cursor_pixel;
3990 f->output_data.w32->cursor_gc
3991 = XCreateGC (NULL, FRAME_W32_WINDOW (f),
3992 (GCFont | GCForeground | GCBackground),
3993 &gc_values);
3995 /* Reliefs. */
3996 f->output_data.w32->white_relief.gc = 0;
3997 f->output_data.w32->black_relief.gc = 0;
3999 unblock_input ();
4003 /* Handler for signals raised during x_create_frame and
4004 x_create_tip_frame. FRAME is the frame which is partially
4005 constructed. */
4007 static Lisp_Object
4008 unwind_create_frame (Lisp_Object frame)
4010 struct frame *f = XFRAME (frame);
4012 /* If frame is ``official'', nothing to do. */
4013 if (NILP (Fmemq (frame, Vframe_list)))
4015 #ifdef GLYPH_DEBUG
4016 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4017 #endif
4019 x_free_frame_resources (f);
4020 free_glyphs (f);
4022 #ifdef GLYPH_DEBUG
4023 /* Check that reference counts are indeed correct. */
4024 eassert (dpyinfo->reference_count == dpyinfo_refcount);
4025 eassert (dpyinfo->terminal->image_cache->refcount == image_cache_refcount);
4026 #endif
4027 return Qt;
4030 return Qnil;
4033 static void
4034 x_default_font_parameter (struct frame *f, Lisp_Object parms)
4036 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4037 Lisp_Object font_param = x_get_arg (dpyinfo, parms, Qfont, NULL, NULL,
4038 RES_TYPE_STRING);
4039 Lisp_Object font;
4040 if (EQ (font_param, Qunbound))
4041 font_param = Qnil;
4042 font = !NILP (font_param) ? font_param
4043 : x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
4045 if (!STRINGP (font))
4047 int i;
4048 static char *names[]
4049 = { "Courier New-10",
4050 "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1",
4051 "-*-Fixedsys-normal-r-*-*-12-*-*-*-c-*-iso8859-1",
4052 "Fixedsys",
4053 NULL };
4055 for (i = 0; names[i]; i++)
4057 font = font_open_by_name (f, build_unibyte_string (names[i]));
4058 if (! NILP (font))
4059 break;
4061 if (NILP (font))
4062 error ("No suitable font was found");
4064 else if (!NILP (font_param))
4066 /* Remember the explicit font parameter, so we can re-apply it after
4067 we've applied the `default' face settings. */
4068 x_set_frame_parameters (f, Fcons (Fcons (Qfont_param, font_param), Qnil));
4070 x_default_parameter (f, parms, Qfont, font, "font", "Font", RES_TYPE_STRING);
4073 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
4074 1, 1, 0,
4075 doc: /* Make a new window, which is called a \"frame\" in Emacs terms.
4076 Return an Emacs frame object.
4077 PARAMETERS is an alist of frame parameters.
4078 If the parameters specify that the frame should not have a minibuffer,
4079 and do not specify a specific minibuffer window to use,
4080 then `default-minibuffer-frame' must be a frame whose minibuffer can
4081 be shared by the new frame.
4083 This function is an internal primitive--use `make-frame' instead. */)
4084 (Lisp_Object parameters)
4086 struct frame *f;
4087 Lisp_Object frame, tem;
4088 Lisp_Object name;
4089 int minibuffer_only = 0;
4090 long window_prompting = 0;
4091 int width, height;
4092 ptrdiff_t count = SPECPDL_INDEX ();
4093 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4094 Lisp_Object display;
4095 struct w32_display_info *dpyinfo = NULL;
4096 Lisp_Object parent;
4097 struct kboard *kb;
4099 /* Make copy of frame parameters because the original is in pure
4100 storage now. */
4101 parameters = Fcopy_alist (parameters);
4103 /* Use this general default value to start with
4104 until we know if this frame has a specified name. */
4105 Vx_resource_name = Vinvocation_name;
4107 display = x_get_arg (dpyinfo, parameters, Qterminal, 0, 0, RES_TYPE_NUMBER);
4108 if (EQ (display, Qunbound))
4109 display = x_get_arg (dpyinfo, parameters, Qdisplay, 0, 0, RES_TYPE_STRING);
4110 if (EQ (display, Qunbound))
4111 display = Qnil;
4112 dpyinfo = check_x_display_info (display);
4113 kb = dpyinfo->terminal->kboard;
4115 if (!dpyinfo->terminal->name)
4116 error ("Terminal is not live, can't create new frames on it");
4118 name = x_get_arg (dpyinfo, parameters, Qname, "name", "Name", RES_TYPE_STRING);
4119 if (!STRINGP (name)
4120 && ! EQ (name, Qunbound)
4121 && ! NILP (name))
4122 error ("Invalid frame name--not a string or nil");
4124 if (STRINGP (name))
4125 Vx_resource_name = name;
4127 /* See if parent window is specified. */
4128 parent = x_get_arg (dpyinfo, parameters, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
4129 if (EQ (parent, Qunbound))
4130 parent = Qnil;
4131 if (! NILP (parent))
4132 CHECK_NUMBER (parent);
4134 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
4135 /* No need to protect DISPLAY because that's not used after passing
4136 it to make_frame_without_minibuffer. */
4137 frame = Qnil;
4138 GCPRO4 (parameters, parent, name, frame);
4139 tem = x_get_arg (dpyinfo, parameters, Qminibuffer, "minibuffer", "Minibuffer",
4140 RES_TYPE_SYMBOL);
4141 if (EQ (tem, Qnone) || NILP (tem))
4142 f = make_frame_without_minibuffer (Qnil, kb, display);
4143 else if (EQ (tem, Qonly))
4145 f = make_minibuffer_frame ();
4146 minibuffer_only = 1;
4148 else if (WINDOWP (tem))
4149 f = make_frame_without_minibuffer (tem, kb, display);
4150 else
4151 f = make_frame (1);
4153 XSETFRAME (frame, f);
4155 /* Note that Windows does support scroll bars. */
4156 FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;
4158 /* By default, make scrollbars the system standard width. */
4159 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = GetSystemMetrics (SM_CXVSCROLL);
4161 f->terminal = dpyinfo->terminal;
4163 f->output_method = output_w32;
4164 f->output_data.w32 = xzalloc (sizeof (struct w32_output));
4165 FRAME_FONTSET (f) = -1;
4167 fset_icon_name
4168 (f, x_get_arg (dpyinfo, parameters, Qicon_name, "iconName", "Title",
4169 RES_TYPE_STRING));
4170 if (! STRINGP (f->icon_name))
4171 fset_icon_name (f, Qnil);
4173 /* FRAME_W32_DISPLAY_INFO (f) = dpyinfo; */
4175 /* With FRAME_X_DISPLAY_INFO set up, this unwind-protect is safe. */
4176 record_unwind_protect (unwind_create_frame, frame);
4177 #ifdef GLYPH_DEBUG
4178 image_cache_refcount =
4179 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
4180 dpyinfo_refcount = dpyinfo->reference_count;
4181 #endif /* GLYPH_DEBUG */
4183 /* Specify the parent under which to make this window. */
4185 if (!NILP (parent))
4187 f->output_data.w32->parent_desc = (Window) XFASTINT (parent);
4188 f->output_data.w32->explicit_parent = 1;
4190 else
4192 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
4193 f->output_data.w32->explicit_parent = 0;
4196 /* Set the name; the functions to which we pass f expect the name to
4197 be set. */
4198 if (EQ (name, Qunbound) || NILP (name))
4200 fset_name (f, build_string (dpyinfo->w32_id_name));
4201 f->explicit_name = 0;
4203 else
4205 fset_name (f, name);
4206 f->explicit_name = 1;
4207 /* use the frame's title when getting resources for this frame. */
4208 specbind (Qx_resource_name, name);
4211 f->resx = dpyinfo->resx;
4212 f->resy = dpyinfo->resy;
4214 if (uniscribe_available)
4215 register_font_driver (&uniscribe_font_driver, f);
4216 register_font_driver (&w32font_driver, f);
4218 x_default_parameter (f, parameters, Qfont_backend, Qnil,
4219 "fontBackend", "FontBackend", RES_TYPE_STRING);
4220 /* Extract the window parameters from the supplied values
4221 that are needed to determine window geometry. */
4222 x_default_font_parameter (f, parameters);
4223 x_default_parameter (f, parameters, Qborder_width, make_number (2),
4224 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
4226 /* We recognize either internalBorderWidth or internalBorder
4227 (which is what xterm calls it). */
4228 if (NILP (Fassq (Qinternal_border_width, parameters)))
4230 Lisp_Object value;
4232 value = x_get_arg (dpyinfo, parameters, Qinternal_border_width,
4233 "internalBorder", "InternalBorder", RES_TYPE_NUMBER);
4234 if (! EQ (value, Qunbound))
4235 parameters = Fcons (Fcons (Qinternal_border_width, value),
4236 parameters);
4238 /* Default internalBorderWidth to 0 on Windows to match other programs. */
4239 x_default_parameter (f, parameters, Qinternal_border_width, make_number (0),
4240 "internalBorderWidth", "InternalBorder", RES_TYPE_NUMBER);
4241 x_default_parameter (f, parameters, Qvertical_scroll_bars, Qright,
4242 "verticalScrollBars", "ScrollBars", RES_TYPE_SYMBOL);
4244 /* Also do the stuff which must be set before the window exists. */
4245 x_default_parameter (f, parameters, Qforeground_color, build_string ("black"),
4246 "foreground", "Foreground", RES_TYPE_STRING);
4247 x_default_parameter (f, parameters, Qbackground_color, build_string ("white"),
4248 "background", "Background", RES_TYPE_STRING);
4249 x_default_parameter (f, parameters, Qmouse_color, build_string ("black"),
4250 "pointerColor", "Foreground", RES_TYPE_STRING);
4251 x_default_parameter (f, parameters, Qborder_color, build_string ("black"),
4252 "borderColor", "BorderColor", RES_TYPE_STRING);
4253 x_default_parameter (f, parameters, Qscreen_gamma, Qnil,
4254 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
4255 x_default_parameter (f, parameters, Qline_spacing, Qnil,
4256 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
4257 x_default_parameter (f, parameters, Qleft_fringe, Qnil,
4258 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
4259 x_default_parameter (f, parameters, Qright_fringe, Qnil,
4260 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
4262 /* Init faces before x_default_parameter is called for scroll-bar
4263 parameters because that function calls x_set_scroll_bar_width,
4264 which calls change_frame_size, which calls Fset_window_buffer,
4265 which runs hooks, which call Fvertical_motion. At the end, we
4266 end up in init_iterator with a null face cache, which should not
4267 happen. */
4268 init_frame_faces (f);
4270 /* The X resources controlling the menu-bar and tool-bar are
4271 processed specially at startup, and reflected in the mode
4272 variables; ignore them here. */
4273 x_default_parameter (f, parameters, Qmenu_bar_lines,
4274 NILP (Vmenu_bar_mode)
4275 ? make_number (0) : make_number (1),
4276 NULL, NULL, RES_TYPE_NUMBER);
4277 x_default_parameter (f, parameters, Qtool_bar_lines,
4278 NILP (Vtool_bar_mode)
4279 ? make_number (0) : make_number (1),
4280 NULL, NULL, RES_TYPE_NUMBER);
4282 x_default_parameter (f, parameters, Qbuffer_predicate, Qnil,
4283 "bufferPredicate", "BufferPredicate", RES_TYPE_SYMBOL);
4284 x_default_parameter (f, parameters, Qtitle, Qnil,
4285 "title", "Title", RES_TYPE_STRING);
4286 x_default_parameter (f, parameters, Qfullscreen, Qnil,
4287 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
4289 f->output_data.w32->dwStyle = WS_OVERLAPPEDWINDOW;
4290 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
4292 f->output_data.w32->text_cursor = w32_load_cursor (IDC_IBEAM);
4293 f->output_data.w32->nontext_cursor = w32_load_cursor (IDC_ARROW);
4294 f->output_data.w32->modeline_cursor = w32_load_cursor (IDC_ARROW);
4295 f->output_data.w32->hand_cursor = w32_load_cursor (IDC_HAND);
4296 f->output_data.w32->hourglass_cursor = w32_load_cursor (IDC_WAIT);
4297 f->output_data.w32->horizontal_drag_cursor = w32_load_cursor (IDC_SIZEWE);
4299 f->output_data.w32->current_cursor = f->output_data.w32->nontext_cursor;
4301 window_prompting = x_figure_window_size (f, parameters, 1);
4303 tem = x_get_arg (dpyinfo, parameters, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
4304 f->no_split = minibuffer_only || EQ (tem, Qt);
4306 w32_window (f, window_prompting, minibuffer_only);
4307 x_icon (f, parameters);
4309 x_make_gc (f);
4311 /* Now consider the frame official. */
4312 f->terminal->reference_count++;
4313 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
4314 Vframe_list = Fcons (frame, Vframe_list);
4316 /* We need to do this after creating the window, so that the
4317 icon-creation functions can say whose icon they're describing. */
4318 x_default_parameter (f, parameters, Qicon_type, Qnil,
4319 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
4321 x_default_parameter (f, parameters, Qauto_raise, Qnil,
4322 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4323 x_default_parameter (f, parameters, Qauto_lower, Qnil,
4324 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4325 x_default_parameter (f, parameters, Qcursor_type, Qbox,
4326 "cursorType", "CursorType", RES_TYPE_SYMBOL);
4327 x_default_parameter (f, parameters, Qscroll_bar_width, Qnil,
4328 "scrollBarWidth", "ScrollBarWidth", RES_TYPE_NUMBER);
4329 x_default_parameter (f, parameters, Qalpha, Qnil,
4330 "alpha", "Alpha", RES_TYPE_NUMBER);
4332 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
4333 Change will not be effected unless different from the current
4334 FRAME_LINES (f). */
4335 width = FRAME_COLS (f);
4336 height = FRAME_LINES (f);
4338 FRAME_LINES (f) = 0;
4339 SET_FRAME_COLS (f, 0);
4340 change_frame_size (f, height, width, 1, 0, 0);
4342 /* Tell the server what size and position, etc, we want, and how
4343 badly we want them. This should be done after we have the menu
4344 bar so that its size can be taken into account. */
4345 block_input ();
4346 x_wm_set_size_hint (f, window_prompting, 0);
4347 unblock_input ();
4349 /* Make the window appear on the frame and enable display, unless
4350 the caller says not to. However, with explicit parent, Emacs
4351 cannot control visibility, so don't try. */
4352 if (! f->output_data.w32->explicit_parent)
4354 Lisp_Object visibility;
4356 visibility = x_get_arg (dpyinfo, parameters, Qvisibility, 0, 0, RES_TYPE_SYMBOL);
4357 if (EQ (visibility, Qunbound))
4358 visibility = Qt;
4360 if (EQ (visibility, Qicon))
4361 x_iconify_frame (f);
4362 else if (! NILP (visibility))
4363 x_make_frame_visible (f);
4364 else
4365 /* Must have been Qnil. */
4369 /* Initialize `default-minibuffer-frame' in case this is the first
4370 frame on this terminal. */
4371 if (FRAME_HAS_MINIBUF_P (f)
4372 && (!FRAMEP (KVAR (kb, Vdefault_minibuffer_frame))
4373 || !FRAME_LIVE_P (XFRAME (KVAR (kb, Vdefault_minibuffer_frame)))))
4374 kset_default_minibuffer_frame (kb, frame);
4376 /* All remaining specified parameters, which have not been "used"
4377 by x_get_arg and friends, now go in the misc. alist of the frame. */
4378 for (tem = parameters; CONSP (tem); tem = XCDR (tem))
4379 if (CONSP (XCAR (tem)) && !NILP (XCAR (XCAR (tem))))
4380 fset_param_alist (f, Fcons (XCAR (tem), f->param_alist));
4382 UNGCPRO;
4384 /* Make sure windows on this frame appear in calls to next-window
4385 and similar functions. */
4386 Vwindow_list = Qnil;
4388 return unbind_to (count, frame);
4391 /* FRAME is used only to get a handle on the X display. We don't pass the
4392 display info directly because we're called from frame.c, which doesn't
4393 know about that structure. */
4394 Lisp_Object
4395 x_get_focus_frame (struct frame *frame)
4397 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (frame);
4398 Lisp_Object xfocus;
4399 if (! dpyinfo->w32_focus_frame)
4400 return Qnil;
4402 XSETFRAME (xfocus, dpyinfo->w32_focus_frame);
4403 return xfocus;
4406 DEFUN ("x-focus-frame", Fx_focus_frame, Sx_focus_frame, 1, 1, 0,
4407 doc: /* Give FRAME input focus, raising to foreground if necessary. */)
4408 (Lisp_Object frame)
4410 x_focus_on_frame (check_x_frame (frame));
4411 return Qnil;
4415 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
4416 doc: /* Internal function called by `color-defined-p', which see.
4417 \(Note that the Nextstep version of this function ignores FRAME.) */)
4418 (Lisp_Object color, Lisp_Object frame)
4420 XColor foo;
4421 FRAME_PTR f = check_x_frame (frame);
4423 CHECK_STRING (color);
4425 if (w32_defined_color (f, SDATA (color), &foo, 0))
4426 return Qt;
4427 else
4428 return Qnil;
4431 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
4432 doc: /* Internal function called by `color-values', which see. */)
4433 (Lisp_Object color, Lisp_Object frame)
4435 XColor foo;
4436 FRAME_PTR f = check_x_frame (frame);
4438 CHECK_STRING (color);
4440 if (w32_defined_color (f, SDATA (color), &foo, 0))
4441 return list3 (make_number ((GetRValue (foo.pixel) << 8)
4442 | GetRValue (foo.pixel)),
4443 make_number ((GetGValue (foo.pixel) << 8)
4444 | GetGValue (foo.pixel)),
4445 make_number ((GetBValue (foo.pixel) << 8)
4446 | GetBValue (foo.pixel)));
4447 else
4448 return Qnil;
4451 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
4452 doc: /* Internal function called by `display-color-p', which see. */)
4453 (Lisp_Object display)
4455 struct w32_display_info *dpyinfo = check_x_display_info (display);
4457 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 2)
4458 return Qnil;
4460 return Qt;
4463 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p,
4464 Sx_display_grayscale_p, 0, 1, 0,
4465 doc: /* Return t if DISPLAY supports shades of gray.
4466 Note that color displays do support shades of gray.
4467 The optional argument DISPLAY specifies which display to ask about.
4468 DISPLAY should be either a frame or a display name (a string).
4469 If omitted or nil, that stands for the selected frame's display. */)
4470 (Lisp_Object display)
4472 struct w32_display_info *dpyinfo = check_x_display_info (display);
4474 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 1)
4475 return Qnil;
4477 return Qt;
4480 DEFUN ("x-display-pixel-width", Fx_display_pixel_width,
4481 Sx_display_pixel_width, 0, 1, 0,
4482 doc: /* Return the width in pixels of DISPLAY.
4483 The optional argument DISPLAY specifies which display to ask about.
4484 DISPLAY should be either a frame or a display name (a string).
4485 If omitted or nil, that stands for the selected frame's display. */)
4486 (Lisp_Object display)
4488 struct w32_display_info *dpyinfo = check_x_display_info (display);
4490 return make_number (x_display_pixel_width (dpyinfo));
4493 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
4494 Sx_display_pixel_height, 0, 1, 0,
4495 doc: /* Return the height in pixels of DISPLAY.
4496 The optional argument DISPLAY specifies which display to ask about.
4497 DISPLAY should be either a frame or a display name (a string).
4498 If omitted or nil, that stands for the selected frame's display. */)
4499 (Lisp_Object display)
4501 struct w32_display_info *dpyinfo = check_x_display_info (display);
4503 return make_number (x_display_pixel_height (dpyinfo));
4506 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
4507 0, 1, 0,
4508 doc: /* Return the number of bitplanes of DISPLAY.
4509 The optional argument DISPLAY specifies which display to ask about.
4510 DISPLAY should be either a frame or a display name (a string).
4511 If omitted or nil, that stands for the selected frame's display. */)
4512 (Lisp_Object display)
4514 struct w32_display_info *dpyinfo = check_x_display_info (display);
4516 return make_number (dpyinfo->n_planes * dpyinfo->n_cbits);
4519 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
4520 0, 1, 0,
4521 doc: /* Return the number of color cells of DISPLAY.
4522 The optional argument DISPLAY specifies which display to ask about.
4523 DISPLAY should be either a frame or a display name (a string).
4524 If omitted or nil, that stands for the selected frame's display. */)
4525 (Lisp_Object display)
4527 struct w32_display_info *dpyinfo = check_x_display_info (display);
4528 HDC hdc;
4529 int cap;
4531 hdc = GetDC (dpyinfo->root_window);
4532 if (dpyinfo->has_palette)
4533 cap = GetDeviceCaps (hdc, SIZEPALETTE);
4534 else
4535 cap = GetDeviceCaps (hdc, NUMCOLORS);
4537 /* We force 24+ bit depths to 24-bit, both to prevent an overflow
4538 and because probably is more meaningful on Windows anyway */
4539 if (cap < 0)
4540 cap = 1 << min (dpyinfo->n_planes * dpyinfo->n_cbits, 24);
4542 ReleaseDC (dpyinfo->root_window, hdc);
4544 return make_number (cap);
4547 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
4548 Sx_server_max_request_size,
4549 0, 1, 0,
4550 doc: /* Return the maximum request size of the server of DISPLAY.
4551 The optional argument DISPLAY specifies which display to ask about.
4552 DISPLAY should be either a frame or a display name (a string).
4553 If omitted or nil, that stands for the selected frame's display. */)
4554 (Lisp_Object display)
4556 return make_number (1);
4559 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
4560 doc: /* Return the "vendor ID" string of the W32 system (Microsoft).
4561 The optional argument DISPLAY specifies which display to ask about.
4562 DISPLAY should be either a frame or a display name (a string).
4563 If omitted or nil, that stands for the selected frame's display. */)
4564 (Lisp_Object display)
4566 return build_string ("Microsoft Corp.");
4569 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
4570 doc: /* Return the version numbers of the server of DISPLAY.
4571 The value is a list of three integers: the major and minor
4572 version numbers of the X Protocol in use, and the distributor-specific
4573 release number. See also the function `x-server-vendor'.
4575 The optional argument DISPLAY specifies which display to ask about.
4576 DISPLAY should be either a frame or a display name (a string).
4577 If omitted or nil, that stands for the selected frame's display. */)
4578 (Lisp_Object display)
4580 return Fcons (make_number (w32_major_version),
4581 Fcons (make_number (w32_minor_version),
4582 Fcons (make_number (w32_build_number), Qnil)));
4585 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
4586 doc: /* Return the number of screens on the server of DISPLAY.
4587 The optional argument DISPLAY specifies which display to ask about.
4588 DISPLAY should be either a frame or a display name (a string).
4589 If omitted or nil, that stands for the selected frame's display. */)
4590 (Lisp_Object display)
4592 return make_number (1);
4595 DEFUN ("x-display-mm-height", Fx_display_mm_height,
4596 Sx_display_mm_height, 0, 1, 0,
4597 doc: /* Return the height in millimeters of DISPLAY.
4598 The optional argument DISPLAY specifies which display to ask about.
4599 DISPLAY should be either a frame or a display name (a string).
4600 If omitted or nil, that stands for the selected frame's display. */)
4601 (Lisp_Object display)
4603 struct w32_display_info *dpyinfo = check_x_display_info (display);
4604 HDC hdc;
4605 int cap;
4607 hdc = GetDC (dpyinfo->root_window);
4609 cap = GetDeviceCaps (hdc, VERTSIZE);
4611 ReleaseDC (dpyinfo->root_window, hdc);
4613 return make_number (cap);
4616 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
4617 doc: /* Return the width in millimeters of DISPLAY.
4618 The optional argument DISPLAY specifies which display to ask about.
4619 DISPLAY should be either a frame or a display name (a string).
4620 If omitted or nil, that stands for the selected frame's display. */)
4621 (Lisp_Object display)
4623 struct w32_display_info *dpyinfo = check_x_display_info (display);
4625 HDC hdc;
4626 int cap;
4628 hdc = GetDC (dpyinfo->root_window);
4630 cap = GetDeviceCaps (hdc, HORZSIZE);
4632 ReleaseDC (dpyinfo->root_window, hdc);
4634 return make_number (cap);
4637 DEFUN ("x-display-backing-store", Fx_display_backing_store,
4638 Sx_display_backing_store, 0, 1, 0,
4639 doc: /* Return an indication of whether DISPLAY does backing store.
4640 The value may be `always', `when-mapped', or `not-useful'.
4641 The optional argument DISPLAY specifies which display to ask about.
4642 DISPLAY should be either a frame or a display name (a string).
4643 If omitted or nil, that stands for the selected frame's display. */)
4644 (Lisp_Object display)
4646 return intern ("not-useful");
4649 DEFUN ("x-display-visual-class", Fx_display_visual_class,
4650 Sx_display_visual_class, 0, 1, 0,
4651 doc: /* Return the visual class of DISPLAY.
4652 The value is one of the symbols `static-gray', `gray-scale',
4653 `static-color', `pseudo-color', `true-color', or `direct-color'.
4655 The optional argument DISPLAY specifies which display to ask about.
4656 DISPLAY should be either a frame or a display name (a string).
4657 If omitted or nil, that stands for the selected frame's display. */)
4658 (Lisp_Object display)
4660 struct w32_display_info *dpyinfo = check_x_display_info (display);
4661 Lisp_Object result = Qnil;
4663 if (dpyinfo->has_palette)
4664 result = intern ("pseudo-color");
4665 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 1)
4666 result = intern ("static-grey");
4667 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 4)
4668 result = intern ("static-color");
4669 else if (dpyinfo->n_planes * dpyinfo->n_cbits > 8)
4670 result = intern ("true-color");
4672 return result;
4675 DEFUN ("x-display-save-under", Fx_display_save_under,
4676 Sx_display_save_under, 0, 1, 0,
4677 doc: /* Return t if DISPLAY supports the save-under feature.
4678 The optional argument DISPLAY specifies which display to ask about.
4679 DISPLAY should be either a frame or a display name (a string).
4680 If omitted or nil, that stands for the selected frame's display. */)
4681 (Lisp_Object display)
4683 return Qnil;
4687 x_pixel_width (register struct frame *f)
4689 return FRAME_PIXEL_WIDTH (f);
4693 x_pixel_height (register struct frame *f)
4695 return FRAME_PIXEL_HEIGHT (f);
4699 x_char_width (register struct frame *f)
4701 return FRAME_COLUMN_WIDTH (f);
4705 x_char_height (register struct frame *f)
4707 return FRAME_LINE_HEIGHT (f);
4711 x_screen_planes (register struct frame *f)
4713 return FRAME_W32_DISPLAY_INFO (f)->n_planes;
4716 /* Return the display structure for the display named NAME.
4717 Open a new connection if necessary. */
4719 struct w32_display_info *
4720 x_display_info_for_name (Lisp_Object name)
4722 Lisp_Object names;
4723 struct w32_display_info *dpyinfo;
4725 CHECK_STRING (name);
4727 for (dpyinfo = &one_w32_display_info, names = w32_display_name_list;
4728 dpyinfo && !NILP (w32_display_name_list);
4729 dpyinfo = dpyinfo->next, names = XCDR (names))
4731 Lisp_Object tem;
4732 tem = Fstring_equal (XCAR (XCAR (names)), name);
4733 if (!NILP (tem))
4734 return dpyinfo;
4737 /* Use this general default value to start with. */
4738 Vx_resource_name = Vinvocation_name;
4740 validate_x_resource_name ();
4742 dpyinfo = w32_term_init (name, (unsigned char *)0,
4743 SSDATA (Vx_resource_name));
4745 if (dpyinfo == 0)
4746 error ("Cannot connect to server %s", SDATA (name));
4748 w32_in_use = 1;
4749 XSETFASTINT (Vwindow_system_version, w32_major_version);
4751 return dpyinfo;
4754 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
4755 1, 3, 0, doc: /* Open a connection to a display server.
4756 DISPLAY is the name of the display to connect to.
4757 Optional second arg XRM-STRING is a string of resources in xrdb format.
4758 If the optional third arg MUST-SUCCEED is non-nil,
4759 terminate Emacs if we can't open the connection.
4760 \(In the Nextstep version, the last two arguments are currently ignored.) */)
4761 (Lisp_Object display, Lisp_Object xrm_string, Lisp_Object must_succeed)
4763 unsigned char *xrm_option;
4764 struct w32_display_info *dpyinfo;
4766 /* If initialization has already been done, return now to avoid
4767 overwriting critical parts of one_w32_display_info. */
4768 if (w32_in_use)
4769 return Qnil;
4771 CHECK_STRING (display);
4772 if (! NILP (xrm_string))
4773 CHECK_STRING (xrm_string);
4775 #if 0
4776 if (! EQ (Vwindow_system, intern ("w32")))
4777 error ("Not using Microsoft Windows");
4778 #endif
4780 /* Allow color mapping to be defined externally; first look in user's
4781 HOME directory, then in Emacs etc dir for a file called rgb.txt. */
4783 Lisp_Object color_file;
4784 struct gcpro gcpro1;
4786 color_file = build_string ("~/rgb.txt");
4788 GCPRO1 (color_file);
4790 if (NILP (Ffile_readable_p (color_file)))
4791 color_file =
4792 Fexpand_file_name (build_string ("rgb.txt"),
4793 Fsymbol_value (intern ("data-directory")));
4795 Vw32_color_map = Fx_load_color_file (color_file);
4797 UNGCPRO;
4799 if (NILP (Vw32_color_map))
4800 Vw32_color_map = w32_default_color_map ();
4802 /* Merge in system logical colors. */
4803 add_system_logical_colors_to_map (&Vw32_color_map);
4805 if (! NILP (xrm_string))
4806 xrm_option = SDATA (xrm_string);
4807 else
4808 xrm_option = (unsigned char *) 0;
4810 /* Use this general default value to start with. */
4811 /* First remove .exe suffix from invocation-name - it looks ugly. */
4813 char basename[ MAX_PATH ], *str;
4815 strcpy (basename, SDATA (Vinvocation_name));
4816 str = strrchr (basename, '.');
4817 if (str) *str = 0;
4818 Vinvocation_name = build_string (basename);
4820 Vx_resource_name = Vinvocation_name;
4822 validate_x_resource_name ();
4824 /* This is what opens the connection and sets x_current_display.
4825 This also initializes many symbols, such as those used for input. */
4826 dpyinfo = w32_term_init (display, xrm_option,
4827 SSDATA (Vx_resource_name));
4829 if (dpyinfo == 0)
4831 if (!NILP (must_succeed))
4832 fatal ("Cannot connect to server %s.\n",
4833 SDATA (display));
4834 else
4835 error ("Cannot connect to server %s", SDATA (display));
4838 w32_in_use = 1;
4840 XSETFASTINT (Vwindow_system_version, w32_major_version);
4841 return Qnil;
4844 DEFUN ("x-close-connection", Fx_close_connection,
4845 Sx_close_connection, 1, 1, 0,
4846 doc: /* Close the connection to DISPLAY's server.
4847 For DISPLAY, specify either a frame or a display name (a string).
4848 If DISPLAY is nil, that stands for the selected frame's display. */)
4849 (Lisp_Object display)
4851 struct w32_display_info *dpyinfo = check_x_display_info (display);
4853 if (dpyinfo->reference_count > 0)
4854 error ("Display still has frames on it");
4856 block_input ();
4857 x_destroy_all_bitmaps (dpyinfo);
4859 x_delete_display (dpyinfo);
4860 unblock_input ();
4862 return Qnil;
4865 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
4866 doc: /* Return the list of display names that Emacs has connections to. */)
4867 (void)
4869 Lisp_Object tail, result;
4871 result = Qnil;
4872 for (tail = w32_display_name_list; CONSP (tail); tail = XCDR (tail))
4873 result = Fcons (XCAR (XCAR (tail)), result);
4875 return result;
4878 DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
4879 doc: /* If ON is non-nil, report X errors as soon as the erring request is made.
4880 This function only has an effect on X Windows. With MS Windows, it is
4881 defined but does nothing.
4883 If ON is nil, allow buffering of requests.
4884 Turning on synchronization prohibits the Xlib routines from buffering
4885 requests and seriously degrades performance, but makes debugging much
4886 easier.
4887 The optional second argument TERMINAL specifies which display to act on.
4888 TERMINAL should be a terminal object, a frame or a display name (a string).
4889 If TERMINAL is omitted or nil, that stands for the selected frame's display. */)
4890 (Lisp_Object on, Lisp_Object display)
4892 return Qnil;
4897 /***********************************************************************
4898 Window properties
4899 ***********************************************************************/
4901 #if 0 /* TODO : port window properties to W32 */
4903 DEFUN ("x-change-window-property", Fx_change_window_property,
4904 Sx_change_window_property, 2, 6, 0,
4905 doc: /* Change window property PROP to VALUE on the X window of FRAME.
4906 PROP must be a string. VALUE may be a string or a list of conses,
4907 numbers and/or strings. If an element in the list is a string, it is
4908 converted to an atom and the value of the Atom is used. If an element
4909 is a cons, it is converted to a 32 bit number where the car is the 16
4910 top bits and the cdr is the lower 16 bits.
4912 FRAME nil or omitted means use the selected frame.
4913 If TYPE is given and non-nil, it is the name of the type of VALUE.
4914 If TYPE is not given or nil, the type is STRING.
4915 FORMAT gives the size in bits of each element if VALUE is a list.
4916 It must be one of 8, 16 or 32.
4917 If VALUE is a string or FORMAT is nil or not given, FORMAT defaults to 8.
4918 If OUTER-P is non-nil, the property is changed for the outer X window of
4919 FRAME. Default is to change on the edit X window. */)
4920 (Lisp_Object prop, Lisp_Object value, Lisp_Object frame,
4921 Lisp_Object type, Lisp_Object format, Lisp_Object outer_p)
4923 struct frame *f = check_x_frame (frame);
4924 Atom prop_atom;
4926 CHECK_STRING (prop);
4927 CHECK_STRING (value);
4929 block_input ();
4930 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
4931 XChangeProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
4932 prop_atom, XA_STRING, 8, PropModeReplace,
4933 SDATA (value), SCHARS (value));
4935 /* Make sure the property is set when we return. */
4936 XFlush (FRAME_W32_DISPLAY (f));
4937 unblock_input ();
4939 return value;
4943 DEFUN ("x-delete-window-property", Fx_delete_window_property,
4944 Sx_delete_window_property, 1, 2, 0,
4945 doc: /* Remove window property PROP from X window of FRAME.
4946 FRAME nil or omitted means use the selected frame. Value is PROP. */)
4947 (Lisp_Object prop, Lisp_Object frame)
4949 struct frame *f = check_x_frame (frame);
4950 Atom prop_atom;
4952 CHECK_STRING (prop);
4953 block_input ();
4954 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
4955 XDeleteProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), prop_atom);
4957 /* Make sure the property is removed when we return. */
4958 XFlush (FRAME_W32_DISPLAY (f));
4959 unblock_input ();
4961 return prop;
4965 DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
4966 1, 6, 0,
4967 doc: /* Value is the value of window property PROP on FRAME.
4968 If FRAME is nil or omitted, use the selected frame.
4970 On X Windows, the following optional arguments are also accepted:
4971 If TYPE is nil or omitted, get the property as a string.
4972 Otherwise TYPE is the name of the atom that denotes the type expected.
4973 If SOURCE is non-nil, get the property on that window instead of from
4974 FRAME. The number 0 denotes the root window.
4975 If DELETE-P is non-nil, delete the property after retrieving it.
4976 If VECTOR-RET-P is non-nil, don't return a string but a vector of values.
4978 On MS Windows, this function accepts but ignores those optional arguments.
4980 Value is nil if FRAME hasn't a property with name PROP or if PROP has
4981 no value of TYPE (always string in the MS Windows case). */)
4982 (Lisp_Object prop, Lisp_Object frame, Lisp_Object type,
4983 Lisp_Object source, Lisp_Object delete_p, Lisp_Object vector_ret_p)
4985 struct frame *f = check_x_frame (frame);
4986 Atom prop_atom;
4987 int rc;
4988 Lisp_Object prop_value = Qnil;
4989 char *tmp_data = NULL;
4990 Atom actual_type;
4991 int actual_format;
4992 unsigned long actual_size, bytes_remaining;
4994 CHECK_STRING (prop);
4995 block_input ();
4996 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
4997 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
4998 prop_atom, 0, 0, False, XA_STRING,
4999 &actual_type, &actual_format, &actual_size,
5000 &bytes_remaining, (unsigned char **) &tmp_data);
5001 if (rc == Success)
5003 int size = bytes_remaining;
5005 XFree (tmp_data);
5006 tmp_data = NULL;
5008 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
5009 prop_atom, 0, bytes_remaining,
5010 False, XA_STRING,
5011 &actual_type, &actual_format,
5012 &actual_size, &bytes_remaining,
5013 (unsigned char **) &tmp_data);
5014 if (rc == Success)
5015 prop_value = make_string (tmp_data, size);
5017 XFree (tmp_data);
5020 unblock_input ();
5022 return prop_value;
5024 return Qnil;
5027 #endif /* TODO */
5030 /***********************************************************************
5031 Busy cursor
5032 ***********************************************************************/
5034 void
5035 w32_note_current_window (void)
5037 struct frame * f = SELECTED_FRAME ();
5039 if (!FRAME_W32_P (f))
5040 return;
5042 hourglass_hwnd = FRAME_W32_WINDOW (f);
5045 void
5046 show_hourglass (struct atimer *timer)
5048 struct frame *f;
5050 hourglass_atimer = NULL;
5052 block_input ();
5053 f = x_window_to_frame (&one_w32_display_info,
5054 hourglass_hwnd);
5056 if (f)
5057 f->output_data.w32->hourglass_p = 0;
5058 else
5059 f = SELECTED_FRAME ();
5061 if (!FRAME_W32_P (f))
5062 return;
5064 w32_show_hourglass (f);
5065 unblock_input ();
5068 void
5069 hide_hourglass (void)
5071 block_input ();
5072 w32_hide_hourglass ();
5073 unblock_input ();
5077 /* Display an hourglass cursor. Set the hourglass_p flag in display info
5078 to indicate that an hourglass cursor is shown. */
5080 static void
5081 w32_show_hourglass (struct frame *f)
5083 if (!hourglass_shown_p)
5085 f->output_data.w32->hourglass_p = 1;
5086 if (!menubar_in_use && !current_popup_menu)
5087 SetCursor (f->output_data.w32->hourglass_cursor);
5088 hourglass_shown_p = 1;
5093 /* Hide the hourglass cursor on all frames, if it is currently shown. */
5095 static void
5096 w32_hide_hourglass (void)
5098 if (hourglass_shown_p)
5100 struct frame *f = x_window_to_frame (&one_w32_display_info,
5101 hourglass_hwnd);
5102 if (f)
5103 f->output_data.w32->hourglass_p = 0;
5104 else
5105 /* If frame was deleted, restore to selected frame's cursor. */
5106 f = SELECTED_FRAME ();
5108 if (FRAME_W32_P (f))
5109 SetCursor (f->output_data.w32->current_cursor);
5110 else
5111 /* No cursors on non GUI frames - restore to stock arrow cursor. */
5112 SetCursor (w32_load_cursor (IDC_ARROW));
5114 hourglass_shown_p = 0;
5120 /***********************************************************************
5121 Tool tips
5122 ***********************************************************************/
5124 static Lisp_Object x_create_tip_frame (struct w32_display_info *,
5125 Lisp_Object, Lisp_Object);
5126 static void compute_tip_xy (struct frame *, Lisp_Object, Lisp_Object,
5127 Lisp_Object, int, int, int *, int *);
5129 /* The frame of a currently visible tooltip. */
5131 Lisp_Object tip_frame;
5133 /* If non-nil, a timer started that hides the last tooltip when it
5134 fires. */
5136 Lisp_Object tip_timer;
5137 Window tip_window;
5139 /* If non-nil, a vector of 3 elements containing the last args
5140 with which x-show-tip was called. See there. */
5142 Lisp_Object last_show_tip_args;
5145 static Lisp_Object
5146 unwind_create_tip_frame (Lisp_Object frame)
5148 Lisp_Object deleted;
5150 deleted = unwind_create_frame (frame);
5151 if (EQ (deleted, Qt))
5153 tip_window = NULL;
5154 tip_frame = Qnil;
5157 return deleted;
5161 /* Create a frame for a tooltip on the display described by DPYINFO.
5162 PARMS is a list of frame parameters. TEXT is the string to
5163 display in the tip frame. Value is the frame.
5165 Note that functions called here, esp. x_default_parameter can
5166 signal errors, for instance when a specified color name is
5167 undefined. We have to make sure that we're in a consistent state
5168 when this happens. */
5170 static Lisp_Object
5171 x_create_tip_frame (struct w32_display_info *dpyinfo,
5172 Lisp_Object parms, Lisp_Object text)
5174 struct frame *f;
5175 Lisp_Object frame;
5176 Lisp_Object name;
5177 long window_prompting = 0;
5178 int width, height;
5179 ptrdiff_t count = SPECPDL_INDEX ();
5180 struct gcpro gcpro1, gcpro2, gcpro3;
5181 struct kboard *kb;
5182 int face_change_count_before = face_change_count;
5183 Lisp_Object buffer;
5184 struct buffer *old_buffer;
5186 check_w32 ();
5188 /* Use this general default value to start with until we know if
5189 this frame has a specified name. */
5190 Vx_resource_name = Vinvocation_name;
5192 kb = dpyinfo->terminal->kboard;
5194 /* The calls to x_get_arg remove elements from PARMS, so copy it to
5195 avoid destructive changes behind our caller's back. */
5196 parms = Fcopy_alist (parms);
5198 /* Get the name of the frame to use for resource lookup. */
5199 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
5200 if (!STRINGP (name)
5201 && !EQ (name, Qunbound)
5202 && !NILP (name))
5203 error ("Invalid frame name--not a string or nil");
5204 Vx_resource_name = name;
5206 frame = Qnil;
5207 GCPRO3 (parms, name, frame);
5208 /* Make a frame without minibuffer nor mode-line. */
5209 f = make_frame (0);
5210 f->wants_modeline = 0;
5211 XSETFRAME (frame, f);
5213 buffer = Fget_buffer_create (build_string (" *tip*"));
5214 /* Use set_window_buffer instead of Fset_window_buffer (see
5215 discussion of bug#11984, bug#12025, bug#12026). */
5216 set_window_buffer (FRAME_ROOT_WINDOW (f), buffer, 0, 0);
5217 old_buffer = current_buffer;
5218 set_buffer_internal_1 (XBUFFER (buffer));
5219 bset_truncate_lines (current_buffer, Qnil);
5220 specbind (Qinhibit_read_only, Qt);
5221 specbind (Qinhibit_modification_hooks, Qt);
5222 Ferase_buffer ();
5223 Finsert (1, &text);
5224 set_buffer_internal_1 (old_buffer);
5226 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
5227 record_unwind_protect (unwind_create_tip_frame, frame);
5229 /* By setting the output method, we're essentially saying that
5230 the frame is live, as per FRAME_LIVE_P. If we get a signal
5231 from this point on, x_destroy_window might screw up reference
5232 counts etc. */
5233 f->terminal = dpyinfo->terminal;
5234 f->output_method = output_w32;
5235 f->output_data.w32 = xzalloc (sizeof (struct w32_output));
5237 FRAME_FONTSET (f) = -1;
5238 fset_icon_name (f, Qnil);
5240 #ifdef GLYPH_DEBUG
5241 image_cache_refcount =
5242 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
5243 dpyinfo_refcount = dpyinfo->reference_count;
5244 #endif /* GLYPH_DEBUG */
5245 FRAME_KBOARD (f) = kb;
5246 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
5247 f->output_data.w32->explicit_parent = 0;
5249 /* Set the name; the functions to which we pass f expect the name to
5250 be set. */
5251 if (EQ (name, Qunbound) || NILP (name))
5253 fset_name (f, build_string (dpyinfo->w32_id_name));
5254 f->explicit_name = 0;
5256 else
5258 fset_name (f, name);
5259 f->explicit_name = 1;
5260 /* use the frame's title when getting resources for this frame. */
5261 specbind (Qx_resource_name, name);
5264 f->resx = dpyinfo->resx;
5265 f->resy = dpyinfo->resy;
5267 if (uniscribe_available)
5268 register_font_driver (&uniscribe_font_driver, f);
5269 register_font_driver (&w32font_driver, f);
5271 x_default_parameter (f, parms, Qfont_backend, Qnil,
5272 "fontBackend", "FontBackend", RES_TYPE_STRING);
5274 /* Extract the window parameters from the supplied values
5275 that are needed to determine window geometry. */
5276 x_default_font_parameter (f, parms);
5278 x_default_parameter (f, parms, Qborder_width, make_number (2),
5279 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
5280 /* This defaults to 2 in order to match xterm. We recognize either
5281 internalBorderWidth or internalBorder (which is what xterm calls
5282 it). */
5283 if (NILP (Fassq (Qinternal_border_width, parms)))
5285 Lisp_Object value;
5287 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
5288 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
5289 if (! EQ (value, Qunbound))
5290 parms = Fcons (Fcons (Qinternal_border_width, value),
5291 parms);
5293 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
5294 "internalBorderWidth", "internalBorderWidth",
5295 RES_TYPE_NUMBER);
5297 /* Also do the stuff which must be set before the window exists. */
5298 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
5299 "foreground", "Foreground", RES_TYPE_STRING);
5300 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
5301 "background", "Background", RES_TYPE_STRING);
5302 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
5303 "pointerColor", "Foreground", RES_TYPE_STRING);
5304 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
5305 "cursorColor", "Foreground", RES_TYPE_STRING);
5306 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
5307 "borderColor", "BorderColor", RES_TYPE_STRING);
5309 /* Init faces before x_default_parameter is called for scroll-bar
5310 parameters because that function calls x_set_scroll_bar_width,
5311 which calls change_frame_size, which calls Fset_window_buffer,
5312 which runs hooks, which call Fvertical_motion. At the end, we
5313 end up in init_iterator with a null face cache, which should not
5314 happen. */
5315 init_frame_faces (f);
5317 f->output_data.w32->dwStyle = WS_BORDER | WS_POPUP | WS_DISABLED;
5318 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
5320 window_prompting = x_figure_window_size (f, parms, 0);
5322 /* No fringes on tip frame. */
5323 f->fringe_cols = 0;
5324 f->left_fringe_width = 0;
5325 f->right_fringe_width = 0;
5327 block_input ();
5328 my_create_tip_window (f);
5329 unblock_input ();
5331 x_make_gc (f);
5333 x_default_parameter (f, parms, Qauto_raise, Qnil,
5334 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5335 x_default_parameter (f, parms, Qauto_lower, Qnil,
5336 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5337 x_default_parameter (f, parms, Qcursor_type, Qbox,
5338 "cursorType", "CursorType", RES_TYPE_SYMBOL);
5340 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
5341 Change will not be effected unless different from the current
5342 FRAME_LINES (f). */
5343 width = FRAME_COLS (f);
5344 height = FRAME_LINES (f);
5345 FRAME_LINES (f) = 0;
5346 SET_FRAME_COLS (f, 0);
5347 change_frame_size (f, height, width, 1, 0, 0);
5349 /* Add `tooltip' frame parameter's default value. */
5350 if (NILP (Fframe_parameter (frame, Qtooltip)))
5351 Fmodify_frame_parameters (frame, Fcons (Fcons (Qtooltip, Qt), Qnil));
5353 /* Set up faces after all frame parameters are known. This call
5354 also merges in face attributes specified for new frames.
5356 Frame parameters may be changed if .Xdefaults contains
5357 specifications for the default font. For example, if there is an
5358 `Emacs.default.attributeBackground: pink', the `background-color'
5359 attribute of the frame get's set, which let's the internal border
5360 of the tooltip frame appear in pink. Prevent this. */
5362 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
5363 Lisp_Object fg = Fframe_parameter (frame, Qforeground_color);
5364 Lisp_Object colors = Qnil;
5366 /* Set tip_frame here, so that */
5367 tip_frame = frame;
5368 call2 (Qface_set_after_frame_default, frame, Qnil);
5370 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
5371 colors = Fcons (Fcons (Qbackground_color, bg), colors);
5372 if (!EQ (fg, Fframe_parameter (frame, Qforeground_color)))
5373 colors = Fcons (Fcons (Qforeground_color, fg), colors);
5375 if (!NILP (colors))
5376 Fmodify_frame_parameters (frame, colors);
5379 f->no_split = 1;
5381 UNGCPRO;
5383 /* Now that the frame is official, it counts as a reference to
5384 its display. */
5385 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
5386 f->terminal->reference_count++;
5388 /* It is now ok to make the frame official even if we get an error
5389 below. And the frame needs to be on Vframe_list or making it
5390 visible won't work. */
5391 Vframe_list = Fcons (frame, Vframe_list);
5393 /* Setting attributes of faces of the tooltip frame from resources
5394 and similar will increment face_change_count, which leads to the
5395 clearing of all current matrices. Since this isn't necessary
5396 here, avoid it by resetting face_change_count to the value it
5397 had before we created the tip frame. */
5398 face_change_count = face_change_count_before;
5400 /* Discard the unwind_protect. */
5401 return unbind_to (count, frame);
5405 /* Compute where to display tip frame F. PARMS is the list of frame
5406 parameters for F. DX and DY are specified offsets from the current
5407 location of the mouse. WIDTH and HEIGHT are the width and height
5408 of the tooltip. Return coordinates relative to the root window of
5409 the display in *ROOT_X, and *ROOT_Y. */
5411 static void
5412 compute_tip_xy (struct frame *f,
5413 Lisp_Object parms, Lisp_Object dx, Lisp_Object dy,
5414 int width, int height, int *root_x, int *root_y)
5416 Lisp_Object left, top;
5417 int min_x, min_y, max_x, max_y;
5419 /* User-specified position? */
5420 left = Fcdr (Fassq (Qleft, parms));
5421 top = Fcdr (Fassq (Qtop, parms));
5423 /* Move the tooltip window where the mouse pointer is. Resize and
5424 show it. */
5425 if (!INTEGERP (left) || !INTEGERP (top))
5427 POINT pt;
5429 /* Default min and max values. */
5430 min_x = 0;
5431 min_y = 0;
5432 max_x = x_display_pixel_width (FRAME_W32_DISPLAY_INFO (f));
5433 max_y = x_display_pixel_height (FRAME_W32_DISPLAY_INFO (f));
5435 block_input ();
5436 GetCursorPos (&pt);
5437 *root_x = pt.x;
5438 *root_y = pt.y;
5439 unblock_input ();
5441 /* If multiple monitor support is available, constrain the tip onto
5442 the current monitor. This improves the above by allowing negative
5443 co-ordinates if monitor positions are such that they are valid, and
5444 snaps a tooltip onto a single monitor if we are close to the edge
5445 where it would otherwise flow onto the other monitor (or into
5446 nothingness if there is a gap in the overlap). */
5447 if (monitor_from_point_fn && get_monitor_info_fn)
5449 struct MONITOR_INFO info;
5450 HMONITOR monitor
5451 = monitor_from_point_fn (pt, MONITOR_DEFAULT_TO_NEAREST);
5452 info.cbSize = sizeof (info);
5454 if (get_monitor_info_fn (monitor, &info))
5456 min_x = info.rcWork.left;
5457 min_y = info.rcWork.top;
5458 max_x = info.rcWork.right;
5459 max_y = info.rcWork.bottom;
5464 if (INTEGERP (top))
5465 *root_y = XINT (top);
5466 else if (*root_y + XINT (dy) <= min_y)
5467 *root_y = min_y; /* Can happen for negative dy */
5468 else if (*root_y + XINT (dy) + height <= max_y)
5469 /* It fits below the pointer */
5470 *root_y += XINT (dy);
5471 else if (height + XINT (dy) + min_y <= *root_y)
5472 /* It fits above the pointer. */
5473 *root_y -= height + XINT (dy);
5474 else
5475 /* Put it on the top. */
5476 *root_y = min_y;
5478 if (INTEGERP (left))
5479 *root_x = XINT (left);
5480 else if (*root_x + XINT (dx) <= min_x)
5481 *root_x = 0; /* Can happen for negative dx */
5482 else if (*root_x + XINT (dx) + width <= max_x)
5483 /* It fits to the right of the pointer. */
5484 *root_x += XINT (dx);
5485 else if (width + XINT (dx) + min_x <= *root_x)
5486 /* It fits to the left of the pointer. */
5487 *root_x -= width + XINT (dx);
5488 else
5489 /* Put it left justified on the screen -- it ought to fit that way. */
5490 *root_x = min_x;
5494 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
5495 doc: /* Show STRING in a \"tooltip\" window on frame FRAME.
5496 A tooltip window is a small window displaying a string.
5498 This is an internal function; Lisp code should call `tooltip-show'.
5500 FRAME nil or omitted means use the selected frame.
5502 PARMS is an optional list of frame parameters which can be
5503 used to change the tooltip's appearance.
5505 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
5506 means use the default timeout of 5 seconds.
5508 If the list of frame parameters PARMS contains a `left' parameter,
5509 the tooltip is displayed at that x-position. Otherwise it is
5510 displayed at the mouse position, with offset DX added (default is 5 if
5511 DX isn't specified). Likewise for the y-position; if a `top' frame
5512 parameter is specified, it determines the y-position of the tooltip
5513 window, otherwise it is displayed at the mouse position, with offset
5514 DY added (default is -10).
5516 A tooltip's maximum size is specified by `x-max-tooltip-size'.
5517 Text larger than the specified size is clipped. */)
5518 (Lisp_Object string, Lisp_Object frame, Lisp_Object parms, Lisp_Object timeout, Lisp_Object dx, Lisp_Object dy)
5520 struct frame *f;
5521 struct window *w;
5522 int root_x, root_y;
5523 struct buffer *old_buffer;
5524 struct text_pos pos;
5525 int i, width, height, seen_reversed_p;
5526 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5527 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5528 ptrdiff_t count = SPECPDL_INDEX ();
5530 specbind (Qinhibit_redisplay, Qt);
5532 GCPRO4 (string, parms, frame, timeout);
5534 CHECK_STRING (string);
5535 f = check_x_frame (frame);
5536 if (NILP (timeout))
5537 timeout = make_number (5);
5538 else
5539 CHECK_NATNUM (timeout);
5541 if (NILP (dx))
5542 dx = make_number (5);
5543 else
5544 CHECK_NUMBER (dx);
5546 if (NILP (dy))
5547 dy = make_number (-10);
5548 else
5549 CHECK_NUMBER (dy);
5551 if (NILP (last_show_tip_args))
5552 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
5554 if (!NILP (tip_frame))
5556 Lisp_Object last_string = AREF (last_show_tip_args, 0);
5557 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
5558 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
5560 if (EQ (frame, last_frame)
5561 && !NILP (Fequal (last_string, string))
5562 && !NILP (Fequal (last_parms, parms)))
5564 struct frame *f = XFRAME (tip_frame);
5566 /* Only DX and DY have changed. */
5567 if (!NILP (tip_timer))
5569 Lisp_Object timer = tip_timer;
5570 tip_timer = Qnil;
5571 call1 (Qcancel_timer, timer);
5574 block_input ();
5575 compute_tip_xy (f, parms, dx, dy, FRAME_PIXEL_WIDTH (f),
5576 FRAME_PIXEL_HEIGHT (f), &root_x, &root_y);
5578 /* Put tooltip in topmost group and in position. */
5579 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
5580 root_x, root_y, 0, 0,
5581 SWP_NOSIZE | SWP_NOACTIVATE);
5583 /* Ensure tooltip is on top of other topmost windows (eg menus). */
5584 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
5585 0, 0, 0, 0,
5586 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
5588 unblock_input ();
5589 goto start_timer;
5593 /* Hide a previous tip, if any. */
5594 Fx_hide_tip ();
5596 ASET (last_show_tip_args, 0, string);
5597 ASET (last_show_tip_args, 1, frame);
5598 ASET (last_show_tip_args, 2, parms);
5600 /* Add default values to frame parameters. */
5601 if (NILP (Fassq (Qname, parms)))
5602 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
5603 if (NILP (Fassq (Qinternal_border_width, parms)))
5604 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
5605 if (NILP (Fassq (Qborder_width, parms)))
5606 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
5607 if (NILP (Fassq (Qborder_color, parms)))
5608 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
5609 if (NILP (Fassq (Qbackground_color, parms)))
5610 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
5611 parms);
5613 /* Block input until the tip has been fully drawn, to avoid crashes
5614 when drawing tips in menus. */
5615 block_input ();
5617 /* Create a frame for the tooltip, and record it in the global
5618 variable tip_frame. */
5619 frame = x_create_tip_frame (FRAME_W32_DISPLAY_INFO (f), parms, string);
5620 f = XFRAME (frame);
5622 /* Set up the frame's root window. */
5623 w = XWINDOW (FRAME_ROOT_WINDOW (f));
5624 wset_left_col (w, make_number (0));
5625 wset_top_line (w, make_number (0));
5627 if (CONSP (Vx_max_tooltip_size)
5628 && INTEGERP (XCAR (Vx_max_tooltip_size))
5629 && XINT (XCAR (Vx_max_tooltip_size)) > 0
5630 && INTEGERP (XCDR (Vx_max_tooltip_size))
5631 && XINT (XCDR (Vx_max_tooltip_size)) > 0)
5633 wset_total_cols (w, XCAR (Vx_max_tooltip_size));
5634 wset_total_lines (w, XCDR (Vx_max_tooltip_size));
5636 else
5638 wset_total_cols (w, make_number (80));
5639 wset_total_lines (w, make_number (40));
5642 FRAME_TOTAL_COLS (f) = XINT (w->total_cols);
5643 adjust_glyphs (f);
5644 w->pseudo_window_p = 1;
5646 /* Display the tooltip text in a temporary buffer. */
5647 old_buffer = current_buffer;
5648 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->buffer));
5649 bset_truncate_lines (current_buffer, Qnil);
5650 clear_glyph_matrix (w->desired_matrix);
5651 clear_glyph_matrix (w->current_matrix);
5652 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
5653 try_window (FRAME_ROOT_WINDOW (f), pos, TRY_WINDOW_IGNORE_FONTS_CHANGE);
5655 /* Compute width and height of the tooltip. */
5656 width = height = seen_reversed_p = 0;
5657 for (i = 0; i < w->desired_matrix->nrows; ++i)
5659 struct glyph_row *row = &w->desired_matrix->rows[i];
5660 struct glyph *last;
5661 int row_width;
5663 /* Stop at the first empty row at the end. */
5664 if (!row->enabled_p || !row->displays_text_p)
5665 break;
5667 /* Let the row go over the full width of the frame. */
5668 row->full_width_p = 1;
5670 row_width = row->pixel_width;
5671 if (row->used[TEXT_AREA])
5673 if (!row->reversed_p)
5675 /* There's a glyph at the end of rows that is used to
5676 place the cursor there. Don't include the width of
5677 this glyph. */
5678 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5679 if (INTEGERP (last->object))
5680 row_width -= last->pixel_width;
5682 else
5684 /* There could be a stretch glyph at the beginning of R2L
5685 rows that is produced by extend_face_to_end_of_line.
5686 Don't count that glyph. */
5687 struct glyph *g = row->glyphs[TEXT_AREA];
5689 if (g->type == STRETCH_GLYPH && INTEGERP (g->object))
5691 row_width -= g->pixel_width;
5692 seen_reversed_p = 1;
5697 height += row->height;
5698 width = max (width, row_width);
5701 /* If we've seen partial-length R2L rows, we need to re-adjust the
5702 tool-tip frame width and redisplay it again, to avoid over-wide
5703 tips due to the stretch glyph that extends R2L lines to full
5704 width of the frame. */
5705 if (seen_reversed_p)
5707 /* w->total_cols and FRAME_TOTAL_COLS want the width in columns,
5708 not in pixels. */
5709 width /= WINDOW_FRAME_COLUMN_WIDTH (w);
5710 wset_total_cols (w, make_number (width));
5711 FRAME_TOTAL_COLS (f) = width;
5712 adjust_glyphs (f);
5713 w->pseudo_window_p = 1;
5714 clear_glyph_matrix (w->desired_matrix);
5715 clear_glyph_matrix (w->current_matrix);
5716 try_window (FRAME_ROOT_WINDOW (f), pos, TRY_WINDOW_IGNORE_FONTS_CHANGE);
5717 width = height = 0;
5718 /* Recompute width and height of the tooltip. */
5719 for (i = 0; i < w->desired_matrix->nrows; ++i)
5721 struct glyph_row *row = &w->desired_matrix->rows[i];
5722 struct glyph *last;
5723 int row_width;
5725 if (!row->enabled_p || !row->displays_text_p)
5726 break;
5727 row->full_width_p = 1;
5728 row_width = row->pixel_width;
5729 if (row->used[TEXT_AREA] && !row->reversed_p)
5731 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5732 if (INTEGERP (last->object))
5733 row_width -= last->pixel_width;
5736 height += row->height;
5737 width = max (width, row_width);
5741 /* Round up the height to an integral multiple of FRAME_LINE_HEIGHT. */
5742 if (height % FRAME_LINE_HEIGHT (f) != 0)
5743 height += FRAME_LINE_HEIGHT (f) - height % FRAME_LINE_HEIGHT (f);
5744 /* Add the frame's internal border to the width and height the w32
5745 window should have. */
5746 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5747 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5749 /* Move the tooltip window where the mouse pointer is. Resize and
5750 show it. */
5751 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5754 /* Adjust Window size to take border into account. */
5755 RECT rect;
5756 rect.left = rect.top = 0;
5757 rect.right = width;
5758 rect.bottom = height;
5759 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
5760 FRAME_EXTERNAL_MENU_BAR (f));
5762 /* Position and size tooltip, and put it in the topmost group.
5763 The add-on of FRAME_COLUMN_WIDTH to the 5th argument is a
5764 peculiarity of w32 display: without it, some fonts cause the
5765 last character of the tip to be truncated or wrapped around to
5766 the next line. */
5767 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
5768 root_x, root_y,
5769 rect.right - rect.left + FRAME_COLUMN_WIDTH (f),
5770 rect.bottom - rect.top, SWP_NOACTIVATE);
5772 /* Ensure tooltip is on top of other topmost windows (eg menus). */
5773 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
5774 0, 0, 0, 0,
5775 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
5777 /* Let redisplay know that we have made the frame visible already. */
5778 f->async_visible = 1;
5780 ShowWindow (FRAME_W32_WINDOW (f), SW_SHOWNOACTIVATE);
5783 /* Draw into the window. */
5784 w->must_be_updated_p = 1;
5785 update_single_window (w, 1);
5787 unblock_input ();
5789 /* Restore original current buffer. */
5790 set_buffer_internal_1 (old_buffer);
5791 windows_or_buffers_changed = old_windows_or_buffers_changed;
5793 start_timer:
5794 /* Let the tip disappear after timeout seconds. */
5795 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
5796 intern ("x-hide-tip"));
5798 UNGCPRO;
5799 return unbind_to (count, Qnil);
5803 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
5804 doc: /* Hide the current tooltip window, if there is any.
5805 Value is t if tooltip was open, nil otherwise. */)
5806 (void)
5808 ptrdiff_t count;
5809 Lisp_Object deleted, frame, timer;
5810 struct gcpro gcpro1, gcpro2;
5812 /* Return quickly if nothing to do. */
5813 if (NILP (tip_timer) && NILP (tip_frame))
5814 return Qnil;
5816 frame = tip_frame;
5817 timer = tip_timer;
5818 GCPRO2 (frame, timer);
5819 tip_frame = tip_timer = deleted = Qnil;
5821 count = SPECPDL_INDEX ();
5822 specbind (Qinhibit_redisplay, Qt);
5823 specbind (Qinhibit_quit, Qt);
5825 if (!NILP (timer))
5826 call1 (Qcancel_timer, timer);
5828 if (FRAMEP (frame))
5830 delete_frame (frame, Qnil);
5831 deleted = Qt;
5834 UNGCPRO;
5835 return unbind_to (count, deleted);
5838 /***********************************************************************
5839 File selection dialog
5840 ***********************************************************************/
5842 /* Callback for altering the behavior of the Open File dialog.
5843 Makes the Filename text field contain "Current Directory" and be
5844 read-only when "Directories" is selected in the filter. This
5845 allows us to work around the fact that the standard Open File
5846 dialog does not support directories. */
5847 static UINT CALLBACK
5848 file_dialog_callback (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
5850 if (msg == WM_NOTIFY)
5852 OFNOTIFY * notify = (OFNOTIFY *)lParam;
5853 /* Detect when the Filter dropdown is changed. */
5854 if (notify->hdr.code == CDN_TYPECHANGE
5855 || notify->hdr.code == CDN_INITDONE)
5857 HWND dialog = GetParent (hwnd);
5858 HWND edit_control = GetDlgItem (dialog, FILE_NAME_TEXT_FIELD);
5859 HWND list = GetDlgItem (dialog, FILE_NAME_LIST);
5861 /* At least on Windows 7, the above attempt to get the window handle
5862 to the File Name Text Field fails. The following code does the
5863 job though. Note that this code is based on my examination of the
5864 window hierarchy using Microsoft Spy++. bk */
5865 if (edit_control == NULL)
5867 HWND tmp = GetDlgItem (dialog, FILE_NAME_COMBO_BOX);
5868 if (tmp)
5870 tmp = GetWindow (tmp, GW_CHILD);
5871 if (tmp)
5872 edit_control = GetWindow (tmp, GW_CHILD);
5876 /* Directories is in index 2. */
5877 if (notify->lpOFN->nFilterIndex == 2)
5879 CommDlg_OpenSave_SetControlText (dialog, FILE_NAME_TEXT_FIELD,
5880 "Current Directory");
5881 EnableWindow (edit_control, FALSE);
5882 /* Note that at least on Windows 7, the above call to EnableWindow
5883 disables the window that would ordinarily have focus. If we
5884 do not set focus to some other window here, focus will land in
5885 no man's land and the user will be unable to tab through the
5886 dialog box (pressing tab will only result in a beep).
5887 Avoid that problem by setting focus to the list here. */
5888 if (notify->hdr.code == CDN_INITDONE)
5889 SetFocus (list);
5891 else
5893 /* Don't override default filename on init done. */
5894 if (notify->hdr.code == CDN_TYPECHANGE)
5895 CommDlg_OpenSave_SetControlText (dialog,
5896 FILE_NAME_TEXT_FIELD, "");
5897 EnableWindow (edit_control, TRUE);
5901 return 0;
5904 /* Since we compile with _WIN32_WINNT set to 0x0400 (for NT4 compatibility)
5905 we end up with the old file dialogs. Define a big enough struct for the
5906 new dialog to trick GetOpenFileName into giving us the new dialogs on
5907 Windows 2000 and XP. */
5908 typedef struct
5910 OPENFILENAME real_details;
5911 void * pReserved;
5912 DWORD dwReserved;
5913 DWORD FlagsEx;
5914 } NEWOPENFILENAME;
5917 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5918 doc: /* Read file name, prompting with PROMPT in directory DIR.
5919 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5920 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5921 or directory must exist.
5923 This function is only defined on NS, MS Windows, and X Windows with the
5924 Motif or Gtk toolkits. With the Motif toolkit, ONLY-DIR-P is ignored.
5925 Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories. */)
5926 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename, Lisp_Object mustmatch, Lisp_Object only_dir_p)
5928 struct frame *f = SELECTED_FRAME ();
5929 Lisp_Object file = Qnil;
5930 ptrdiff_t count = SPECPDL_INDEX ();
5931 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5932 char filename[MAX_PATH + 1];
5933 char init_dir[MAX_PATH + 1];
5934 int default_filter_index = 1; /* 1: All Files, 2: Directories only */
5936 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5937 CHECK_STRING (prompt);
5938 CHECK_STRING (dir);
5940 /* Create the dialog with PROMPT as title, using DIR as initial
5941 directory and using "*" as pattern. */
5942 dir = Fexpand_file_name (dir, Qnil);
5943 strncpy (init_dir, SDATA (ENCODE_FILE (dir)), MAX_PATH);
5944 init_dir[MAX_PATH] = '\0';
5945 unixtodos_filename (init_dir);
5947 if (STRINGP (default_filename))
5949 char *file_name_only;
5950 char *full_path_name = SDATA (ENCODE_FILE (default_filename));
5952 unixtodos_filename (full_path_name);
5954 file_name_only = strrchr (full_path_name, '\\');
5955 if (!file_name_only)
5956 file_name_only = full_path_name;
5957 else
5958 file_name_only++;
5960 strncpy (filename, file_name_only, MAX_PATH);
5961 filename[MAX_PATH] = '\0';
5963 else
5964 filename[0] = '\0';
5966 /* The code in file_dialog_callback that attempts to set the text
5967 of the file name edit window when handling the CDN_INITDONE
5968 WM_NOTIFY message does not work. Setting filename to "Current
5969 Directory" in the only_dir_p case here does work however. */
5970 if (filename[0] == 0 && ! NILP (only_dir_p))
5971 strcpy (filename, "Current Directory");
5974 NEWOPENFILENAME new_file_details;
5975 BOOL file_opened = FALSE;
5976 OPENFILENAME * file_details = &new_file_details.real_details;
5978 /* Prevent redisplay. */
5979 specbind (Qinhibit_redisplay, Qt);
5980 block_input ();
5982 memset (&new_file_details, 0, sizeof (new_file_details));
5983 /* Apparently NT4 crashes if you give it an unexpected size.
5984 I'm not sure about Windows 9x, so play it safe. */
5985 if (w32_major_version > 4 && w32_major_version < 95)
5986 file_details->lStructSize = sizeof (NEWOPENFILENAME);
5987 else
5988 file_details->lStructSize = sizeof (OPENFILENAME);
5990 file_details->hwndOwner = FRAME_W32_WINDOW (f);
5991 /* Undocumented Bug in Common File Dialog:
5992 If a filter is not specified, shell links are not resolved. */
5993 file_details->lpstrFilter = "All Files (*.*)\0*.*\0Directories\0*|*\0\0";
5994 file_details->lpstrFile = filename;
5995 file_details->nMaxFile = sizeof (filename);
5996 file_details->lpstrInitialDir = init_dir;
5997 file_details->lpstrTitle = SDATA (prompt);
5999 if (! NILP (only_dir_p))
6000 default_filter_index = 2;
6002 file_details->nFilterIndex = default_filter_index;
6004 file_details->Flags = (OFN_HIDEREADONLY | OFN_NOCHANGEDIR
6005 | OFN_EXPLORER | OFN_ENABLEHOOK);
6006 if (!NILP (mustmatch))
6008 /* Require that the path to the parent directory exists. */
6009 file_details->Flags |= OFN_PATHMUSTEXIST;
6010 /* If we are looking for a file, require that it exists. */
6011 if (NILP (only_dir_p))
6012 file_details->Flags |= OFN_FILEMUSTEXIST;
6015 file_details->lpfnHook = (LPOFNHOOKPROC) file_dialog_callback;
6017 file_opened = GetOpenFileName (file_details);
6019 unblock_input ();
6021 if (file_opened)
6023 dostounix_filename (filename);
6025 if (file_details->nFilterIndex == 2)
6027 /* "Directories" selected - strip dummy file name. */
6028 char * last = strrchr (filename, '/');
6029 *last = '\0';
6032 file = DECODE_FILE (build_string (filename));
6034 /* User canceled the dialog without making a selection. */
6035 else if (!CommDlgExtendedError ())
6036 file = Qnil;
6037 /* An error occurred, fallback on reading from the mini-buffer. */
6038 else
6039 file = Fcompleting_read (prompt, intern ("read-file-name-internal"),
6040 dir, mustmatch, dir, Qfile_name_history,
6041 default_filename, Qnil);
6043 file = unbind_to (count, file);
6046 UNGCPRO;
6048 /* Make "Cancel" equivalent to C-g. */
6049 if (NILP (file))
6050 Fsignal (Qquit, Qnil);
6052 return unbind_to (count, file);
6056 /* Moving files to the system recycle bin.
6057 Used by `move-file-to-trash' instead of the default moving to ~/.Trash */
6058 DEFUN ("system-move-file-to-trash", Fsystem_move_file_to_trash,
6059 Ssystem_move_file_to_trash, 1, 1, 0,
6060 doc: /* Move file or directory named FILENAME to the recycle bin. */)
6061 (Lisp_Object filename)
6063 Lisp_Object handler;
6064 Lisp_Object encoded_file;
6065 Lisp_Object operation;
6067 operation = Qdelete_file;
6068 if (!NILP (Ffile_directory_p (filename))
6069 && NILP (Ffile_symlink_p (filename)))
6071 operation = intern ("delete-directory");
6072 filename = Fdirectory_file_name (filename);
6074 filename = Fexpand_file_name (filename, Qnil);
6076 handler = Ffind_file_name_handler (filename, operation);
6077 if (!NILP (handler))
6078 return call2 (handler, operation, filename);
6080 encoded_file = ENCODE_FILE (filename);
6083 const char * path;
6084 SHFILEOPSTRUCT file_op;
6085 char tmp_path[MAX_PATH + 1];
6087 path = map_w32_filename (SDATA (encoded_file), NULL);
6089 /* On Windows, write permission is required to delete/move files. */
6090 _chmod (path, 0666);
6092 memset (tmp_path, 0, sizeof (tmp_path));
6093 strcpy (tmp_path, path);
6095 memset (&file_op, 0, sizeof (file_op));
6096 file_op.hwnd = HWND_DESKTOP;
6097 file_op.wFunc = FO_DELETE;
6098 file_op.pFrom = tmp_path;
6099 file_op.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_ALLOWUNDO
6100 | FOF_NOERRORUI | FOF_NO_CONNECTED_ELEMENTS;
6101 file_op.fAnyOperationsAborted = FALSE;
6103 if (SHFileOperation (&file_op) != 0)
6104 report_file_error ("Removing old name", list1 (filename));
6106 return Qnil;
6110 /***********************************************************************
6111 w32 specialized functions
6112 ***********************************************************************/
6114 DEFUN ("w32-send-sys-command", Fw32_send_sys_command,
6115 Sw32_send_sys_command, 1, 2, 0,
6116 doc: /* Send frame a Windows WM_SYSCOMMAND message of type COMMAND.
6117 Some useful values for COMMAND are #xf030 to maximize frame (#xf020
6118 to minimize), #xf120 to restore frame to original size, and #xf100
6119 to activate the menubar for keyboard access. #xf140 activates the
6120 screen saver if defined.
6122 If optional parameter FRAME is not specified, use selected frame. */)
6123 (Lisp_Object command, Lisp_Object frame)
6125 FRAME_PTR f = check_x_frame (frame);
6127 CHECK_NUMBER (command);
6129 PostMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, XINT (command), 0);
6131 return Qnil;
6134 DEFUN ("w32-shell-execute", Fw32_shell_execute, Sw32_shell_execute, 2, 4, 0,
6135 doc: /* Get Windows to perform OPERATION on DOCUMENT.
6136 This is a wrapper around the ShellExecute system function, which
6137 invokes the application registered to handle OPERATION for DOCUMENT.
6139 OPERATION is either nil or a string that names a supported operation.
6140 What operations can be used depends on the particular DOCUMENT and its
6141 handler application, but typically it is one of the following common
6142 operations:
6144 \"open\" - open DOCUMENT, which could be a file, a directory, or an
6145 executable program. If it is an application, that
6146 application is launched in the current buffer's default
6147 directory. Otherwise, the application associated with
6148 DOCUMENT is launched in the buffer's default directory.
6149 \"print\" - print DOCUMENT, which must be a file
6150 \"explore\" - start the Windows Explorer on DOCUMENT
6151 \"edit\" - launch an editor and open DOCUMENT for editing; which
6152 editor is launched depends on the association for the
6153 specified DOCUMENT
6154 \"find\" - initiate search starting from DOCUMENT which must specify
6155 a directory
6156 nil - invoke the default OPERATION, or \"open\" if default is
6157 not defined or unavailable
6159 DOCUMENT is typically the name of a document file or a URL, but can
6160 also be a program executable to run, or a directory to open in the
6161 Windows Explorer.
6163 If DOCUMENT is a program executable, the optional third arg PARAMETERS
6164 can be a string containing command line parameters that will be passed
6165 to the program; otherwise, PARAMETERS should be nil or unspecified.
6167 Optional fourth argument SHOW-FLAG can be used to control how the
6168 application will be displayed when it is invoked. If SHOW-FLAG is nil
6169 or unspecified, the application is displayed normally, otherwise it is
6170 an integer representing a ShowWindow flag:
6172 0 - start hidden
6173 1 - start normally
6174 3 - start maximized
6175 6 - start minimized */)
6176 (Lisp_Object operation, Lisp_Object document, Lisp_Object parameters, Lisp_Object show_flag)
6178 Lisp_Object current_dir;
6179 char *errstr;
6181 CHECK_STRING (document);
6183 /* Encode filename, current directory and parameters. */
6184 current_dir = ENCODE_FILE (BVAR (current_buffer, directory));
6185 document = ENCODE_FILE (document);
6186 if (STRINGP (parameters))
6187 parameters = ENCODE_SYSTEM (parameters);
6189 if ((int) ShellExecute (NULL,
6190 (STRINGP (operation) ?
6191 SDATA (operation) : NULL),
6192 SDATA (document),
6193 (STRINGP (parameters) ?
6194 SDATA (parameters) : NULL),
6195 SDATA (current_dir),
6196 (INTEGERP (show_flag) ?
6197 XINT (show_flag) : SW_SHOWDEFAULT))
6198 > 32)
6199 return Qt;
6200 errstr = w32_strerror (0);
6201 /* The error string might be encoded in the locale's encoding. */
6202 if (!NILP (Vlocale_coding_system))
6204 Lisp_Object decoded =
6205 code_convert_string_norecord (build_unibyte_string (errstr),
6206 Vlocale_coding_system, 0);
6207 errstr = SSDATA (decoded);
6209 error ("ShellExecute failed: %s", errstr);
6212 /* Lookup virtual keycode from string representing the name of a
6213 non-ascii keystroke into the corresponding virtual key, using
6214 lispy_function_keys. */
6215 static int
6216 lookup_vk_code (char *key)
6218 int i;
6220 for (i = 0; i < 256; i++)
6221 if (lispy_function_keys[i]
6222 && strcmp (lispy_function_keys[i], key) == 0)
6223 return i;
6225 return -1;
6228 /* Convert a one-element vector style key sequence to a hot key
6229 definition. */
6230 static Lisp_Object
6231 w32_parse_hot_key (Lisp_Object key)
6233 /* Copied from Fdefine_key and store_in_keymap. */
6234 register Lisp_Object c;
6235 int vk_code;
6236 int lisp_modifiers;
6237 int w32_modifiers;
6238 struct gcpro gcpro1;
6240 CHECK_VECTOR (key);
6242 if (XFASTINT (Flength (key)) != 1)
6243 return Qnil;
6245 GCPRO1 (key);
6247 c = Faref (key, make_number (0));
6249 if (CONSP (c) && lucid_event_type_list_p (c))
6250 c = Fevent_convert_list (c);
6252 UNGCPRO;
6254 if (! INTEGERP (c) && ! SYMBOLP (c))
6255 error ("Key definition is invalid");
6257 /* Work out the base key and the modifiers. */
6258 if (SYMBOLP (c))
6260 c = parse_modifiers (c);
6261 lisp_modifiers = XINT (Fcar (Fcdr (c)));
6262 c = Fcar (c);
6263 if (!SYMBOLP (c))
6264 emacs_abort ();
6265 vk_code = lookup_vk_code (SDATA (SYMBOL_NAME (c)));
6267 else if (INTEGERP (c))
6269 lisp_modifiers = XINT (c) & ~CHARACTERBITS;
6270 /* Many ascii characters are their own virtual key code. */
6271 vk_code = XINT (c) & CHARACTERBITS;
6274 if (vk_code < 0 || vk_code > 255)
6275 return Qnil;
6277 if ((lisp_modifiers & meta_modifier) != 0
6278 && !NILP (Vw32_alt_is_meta))
6279 lisp_modifiers |= alt_modifier;
6281 /* Supply defs missing from mingw32. */
6282 #ifndef MOD_ALT
6283 #define MOD_ALT 0x0001
6284 #define MOD_CONTROL 0x0002
6285 #define MOD_SHIFT 0x0004
6286 #define MOD_WIN 0x0008
6287 #endif
6289 /* Convert lisp modifiers to Windows hot-key form. */
6290 w32_modifiers = (lisp_modifiers & hyper_modifier) ? MOD_WIN : 0;
6291 w32_modifiers |= (lisp_modifiers & alt_modifier) ? MOD_ALT : 0;
6292 w32_modifiers |= (lisp_modifiers & ctrl_modifier) ? MOD_CONTROL : 0;
6293 w32_modifiers |= (lisp_modifiers & shift_modifier) ? MOD_SHIFT : 0;
6295 return HOTKEY (vk_code, w32_modifiers);
6298 DEFUN ("w32-register-hot-key", Fw32_register_hot_key,
6299 Sw32_register_hot_key, 1, 1, 0,
6300 doc: /* Register KEY as a hot-key combination.
6301 Certain key combinations like Alt-Tab are reserved for system use on
6302 Windows, and therefore are normally intercepted by the system. However,
6303 most of these key combinations can be received by registering them as
6304 hot-keys, overriding their special meaning.
6306 KEY must be a one element key definition in vector form that would be
6307 acceptable to `define-key' (e.g. [A-tab] for Alt-Tab). The meta
6308 modifier is interpreted as Alt if `w32-alt-is-meta' is t, and hyper
6309 is always interpreted as the Windows modifier keys.
6311 The return value is the hotkey-id if registered, otherwise nil. */)
6312 (Lisp_Object key)
6314 key = w32_parse_hot_key (key);
6316 if (!NILP (key) && NILP (Fmemq (key, w32_grabbed_keys)))
6318 /* Reuse an empty slot if possible. */
6319 Lisp_Object item = Fmemq (Qnil, w32_grabbed_keys);
6321 /* Safe to add new key to list, even if we have focus. */
6322 if (NILP (item))
6323 w32_grabbed_keys = Fcons (key, w32_grabbed_keys);
6324 else
6325 XSETCAR (item, key);
6327 /* Notify input thread about new hot-key definition, so that it
6328 takes effect without needing to switch focus. */
6329 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
6330 (WPARAM) XLI (key), 0);
6333 return key;
6336 DEFUN ("w32-unregister-hot-key", Fw32_unregister_hot_key,
6337 Sw32_unregister_hot_key, 1, 1, 0,
6338 doc: /* Unregister KEY as a hot-key combination. */)
6339 (Lisp_Object key)
6341 Lisp_Object item;
6343 if (!INTEGERP (key))
6344 key = w32_parse_hot_key (key);
6346 item = Fmemq (key, w32_grabbed_keys);
6348 if (!NILP (item))
6350 /* Notify input thread about hot-key definition being removed, so
6351 that it takes effect without needing focus switch. */
6352 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
6353 (WPARAM) XINT (XCAR (item)), (LPARAM) XLI (item)))
6355 MSG msg;
6356 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
6358 return Qt;
6360 return Qnil;
6363 DEFUN ("w32-registered-hot-keys", Fw32_registered_hot_keys,
6364 Sw32_registered_hot_keys, 0, 0, 0,
6365 doc: /* Return list of registered hot-key IDs. */)
6366 (void)
6368 return Fdelq (Qnil, Fcopy_sequence (w32_grabbed_keys));
6371 DEFUN ("w32-reconstruct-hot-key", Fw32_reconstruct_hot_key,
6372 Sw32_reconstruct_hot_key, 1, 1, 0,
6373 doc: /* Convert hot-key ID to a lisp key combination.
6374 usage: (w32-reconstruct-hot-key ID) */)
6375 (Lisp_Object hotkeyid)
6377 int vk_code, w32_modifiers;
6378 Lisp_Object key;
6380 CHECK_NUMBER (hotkeyid);
6382 vk_code = HOTKEY_VK_CODE (hotkeyid);
6383 w32_modifiers = HOTKEY_MODIFIERS (hotkeyid);
6385 if (vk_code < 256 && lispy_function_keys[vk_code])
6386 key = intern (lispy_function_keys[vk_code]);
6387 else
6388 key = make_number (vk_code);
6390 key = Fcons (key, Qnil);
6391 if (w32_modifiers & MOD_SHIFT)
6392 key = Fcons (Qshift, key);
6393 if (w32_modifiers & MOD_CONTROL)
6394 key = Fcons (Qctrl, key);
6395 if (w32_modifiers & MOD_ALT)
6396 key = Fcons (NILP (Vw32_alt_is_meta) ? Qalt : Qmeta, key);
6397 if (w32_modifiers & MOD_WIN)
6398 key = Fcons (Qhyper, key);
6400 return key;
6403 DEFUN ("w32-toggle-lock-key", Fw32_toggle_lock_key,
6404 Sw32_toggle_lock_key, 1, 2, 0,
6405 doc: /* Toggle the state of the lock key KEY.
6406 KEY can be `capslock', `kp-numlock', or `scroll'.
6407 If the optional parameter NEW-STATE is a number, then the state of KEY
6408 is set to off if the low bit of NEW-STATE is zero, otherwise on. */)
6409 (Lisp_Object key, Lisp_Object new_state)
6411 int vk_code;
6413 if (EQ (key, intern ("capslock")))
6414 vk_code = VK_CAPITAL;
6415 else if (EQ (key, intern ("kp-numlock")))
6416 vk_code = VK_NUMLOCK;
6417 else if (EQ (key, intern ("scroll")))
6418 vk_code = VK_SCROLL;
6419 else
6420 return Qnil;
6422 if (!dwWindowsThreadId)
6423 return make_number (w32_console_toggle_lock_key (vk_code, new_state));
6425 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
6426 (WPARAM) vk_code, (LPARAM) XLI (new_state)))
6428 MSG msg;
6429 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
6430 return make_number (msg.wParam);
6432 return Qnil;
6435 DEFUN ("w32-window-exists-p", Fw32_window_exists_p, Sw32_window_exists_p,
6436 2, 2, 0,
6437 doc: /* Return non-nil if a window exists with the specified CLASS and NAME.
6439 This is a direct interface to the Windows API FindWindow function. */)
6440 (Lisp_Object class, Lisp_Object name)
6442 HWND hnd;
6444 if (!NILP (class))
6445 CHECK_STRING (class);
6446 if (!NILP (name))
6447 CHECK_STRING (name);
6449 hnd = FindWindow (STRINGP (class) ? ((LPCTSTR) SDATA (class)) : NULL,
6450 STRINGP (name) ? ((LPCTSTR) SDATA (name)) : NULL);
6451 if (!hnd)
6452 return Qnil;
6453 return Qt;
6456 DEFUN ("w32-battery-status", Fw32_battery_status, Sw32_battery_status, 0, 0, 0,
6457 doc: /* Get power status information from Windows system.
6459 The following %-sequences are provided:
6460 %L AC line status (verbose)
6461 %B Battery status (verbose)
6462 %b Battery status, empty means high, `-' means low,
6463 `!' means critical, and `+' means charging
6464 %p Battery load percentage
6465 %s Remaining time (to charge or discharge) in seconds
6466 %m Remaining time (to charge or discharge) in minutes
6467 %h Remaining time (to charge or discharge) in hours
6468 %t Remaining time (to charge or discharge) in the form `h:min' */)
6469 (void)
6471 Lisp_Object status = Qnil;
6473 SYSTEM_POWER_STATUS system_status;
6474 if (GetSystemPowerStatus (&system_status))
6476 Lisp_Object line_status, battery_status, battery_status_symbol;
6477 Lisp_Object load_percentage, seconds, minutes, hours, remain;
6479 long seconds_left = (long) system_status.BatteryLifeTime;
6481 if (system_status.ACLineStatus == 0)
6482 line_status = build_string ("off-line");
6483 else if (system_status.ACLineStatus == 1)
6484 line_status = build_string ("on-line");
6485 else
6486 line_status = build_string ("N/A");
6488 if (system_status.BatteryFlag & 128)
6490 battery_status = build_string ("N/A");
6491 battery_status_symbol = empty_unibyte_string;
6493 else if (system_status.BatteryFlag & 8)
6495 battery_status = build_string ("charging");
6496 battery_status_symbol = build_string ("+");
6497 if (system_status.BatteryFullLifeTime != -1L)
6498 seconds_left = system_status.BatteryFullLifeTime - seconds_left;
6500 else if (system_status.BatteryFlag & 4)
6502 battery_status = build_string ("critical");
6503 battery_status_symbol = build_string ("!");
6505 else if (system_status.BatteryFlag & 2)
6507 battery_status = build_string ("low");
6508 battery_status_symbol = build_string ("-");
6510 else if (system_status.BatteryFlag & 1)
6512 battery_status = build_string ("high");
6513 battery_status_symbol = empty_unibyte_string;
6515 else
6517 battery_status = build_string ("medium");
6518 battery_status_symbol = empty_unibyte_string;
6521 if (system_status.BatteryLifePercent > 100)
6522 load_percentage = build_string ("N/A");
6523 else
6525 char buffer[16];
6526 _snprintf (buffer, 16, "%d", system_status.BatteryLifePercent);
6527 load_percentage = build_string (buffer);
6530 if (seconds_left < 0)
6531 seconds = minutes = hours = remain = build_string ("N/A");
6532 else
6534 long m;
6535 float h;
6536 char buffer[16];
6537 _snprintf (buffer, 16, "%ld", seconds_left);
6538 seconds = build_string (buffer);
6540 m = seconds_left / 60;
6541 _snprintf (buffer, 16, "%ld", m);
6542 minutes = build_string (buffer);
6544 h = seconds_left / 3600.0;
6545 _snprintf (buffer, 16, "%3.1f", h);
6546 hours = build_string (buffer);
6548 _snprintf (buffer, 16, "%ld:%02ld", m / 60, m % 60);
6549 remain = build_string (buffer);
6552 status = listn (CONSTYPE_HEAP, 8,
6553 Fcons (make_number ('L'), line_status),
6554 Fcons (make_number ('B'), battery_status),
6555 Fcons (make_number ('b'), battery_status_symbol),
6556 Fcons (make_number ('p'), load_percentage),
6557 Fcons (make_number ('s'), seconds),
6558 Fcons (make_number ('m'), minutes),
6559 Fcons (make_number ('h'), hours),
6560 Fcons (make_number ('t'), remain));
6562 return status;
6566 DEFUN ("file-system-info", Ffile_system_info, Sfile_system_info, 1, 1, 0,
6567 doc: /* Return storage information about the file system FILENAME is on.
6568 Value is a list of floats (TOTAL FREE AVAIL), where TOTAL is the total
6569 storage of the file system, FREE is the free storage, and AVAIL is the
6570 storage available to a non-superuser. All 3 numbers are in bytes.
6571 If the underlying system call fails, value is nil. */)
6572 (Lisp_Object filename)
6574 Lisp_Object encoded, value;
6576 CHECK_STRING (filename);
6577 filename = Fexpand_file_name (filename, Qnil);
6578 encoded = ENCODE_FILE (filename);
6580 value = Qnil;
6582 /* Determining the required information on Windows turns out, sadly,
6583 to be more involved than one would hope. The original Windows API
6584 call for this will return bogus information on some systems, but we
6585 must dynamically probe for the replacement api, since that was
6586 added rather late on. */
6588 HMODULE hKernel = GetModuleHandle ("kernel32");
6589 BOOL (*pfn_GetDiskFreeSpaceEx)
6590 (char *, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER)
6591 = (void *) GetProcAddress (hKernel, "GetDiskFreeSpaceEx");
6593 /* On Windows, we may need to specify the root directory of the
6594 volume holding FILENAME. */
6595 char rootname[MAX_PATH];
6596 char *name = SDATA (encoded);
6598 /* find the root name of the volume if given */
6599 if (isalpha (name[0]) && name[1] == ':')
6601 rootname[0] = name[0];
6602 rootname[1] = name[1];
6603 rootname[2] = '\\';
6604 rootname[3] = 0;
6606 else if (IS_DIRECTORY_SEP (name[0]) && IS_DIRECTORY_SEP (name[1]))
6608 char *str = rootname;
6609 int slashes = 4;
6612 if (IS_DIRECTORY_SEP (*name) && --slashes == 0)
6613 break;
6614 *str++ = *name++;
6616 while ( *name );
6618 *str++ = '\\';
6619 *str = 0;
6622 if (pfn_GetDiskFreeSpaceEx)
6624 /* Unsigned large integers cannot be cast to double, so
6625 use signed ones instead. */
6626 LARGE_INTEGER availbytes;
6627 LARGE_INTEGER freebytes;
6628 LARGE_INTEGER totalbytes;
6630 if (pfn_GetDiskFreeSpaceEx (rootname,
6631 (ULARGE_INTEGER *)&availbytes,
6632 (ULARGE_INTEGER *)&totalbytes,
6633 (ULARGE_INTEGER *)&freebytes))
6634 value = list3 (make_float ((double) totalbytes.QuadPart),
6635 make_float ((double) freebytes.QuadPart),
6636 make_float ((double) availbytes.QuadPart));
6638 else
6640 DWORD sectors_per_cluster;
6641 DWORD bytes_per_sector;
6642 DWORD free_clusters;
6643 DWORD total_clusters;
6645 if (GetDiskFreeSpace (rootname,
6646 &sectors_per_cluster,
6647 &bytes_per_sector,
6648 &free_clusters,
6649 &total_clusters))
6650 value = list3 (make_float ((double) total_clusters
6651 * sectors_per_cluster * bytes_per_sector),
6652 make_float ((double) free_clusters
6653 * sectors_per_cluster * bytes_per_sector),
6654 make_float ((double) free_clusters
6655 * sectors_per_cluster * bytes_per_sector));
6659 return value;
6662 DEFUN ("default-printer-name", Fdefault_printer_name, Sdefault_printer_name,
6663 0, 0, 0, doc: /* Return the name of Windows default printer device. */)
6664 (void)
6666 static char pname_buf[256];
6667 int err;
6668 HANDLE hPrn;
6669 PRINTER_INFO_2 *ppi2 = NULL;
6670 DWORD dwNeeded = 0, dwReturned = 0;
6672 /* Retrieve the default string from Win.ini (the registry).
6673 * String will be in form "printername,drivername,portname".
6674 * This is the most portable way to get the default printer. */
6675 if (GetProfileString ("windows", "device", ",,", pname_buf, sizeof (pname_buf)) <= 0)
6676 return Qnil;
6677 /* printername precedes first "," character */
6678 strtok (pname_buf, ",");
6679 /* We want to know more than the printer name */
6680 if (!OpenPrinter (pname_buf, &hPrn, NULL))
6681 return Qnil;
6682 GetPrinter (hPrn, 2, NULL, 0, &dwNeeded);
6683 if (dwNeeded == 0)
6685 ClosePrinter (hPrn);
6686 return Qnil;
6688 /* Allocate memory for the PRINTER_INFO_2 struct */
6689 ppi2 = xmalloc (dwNeeded);
6690 if (!ppi2)
6692 ClosePrinter (hPrn);
6693 return Qnil;
6695 /* Call GetPrinter again with big enough memory block. */
6696 err = GetPrinter (hPrn, 2, (LPBYTE)ppi2, dwNeeded, &dwReturned);
6697 ClosePrinter (hPrn);
6698 if (!err)
6700 xfree (ppi2);
6701 return Qnil;
6704 if (ppi2)
6706 if (ppi2->Attributes & PRINTER_ATTRIBUTE_SHARED && ppi2->pServerName)
6708 /* a remote printer */
6709 if (*ppi2->pServerName == '\\')
6710 _snprintf (pname_buf, sizeof (pname_buf), "%s\\%s", ppi2->pServerName,
6711 ppi2->pShareName);
6712 else
6713 _snprintf (pname_buf, sizeof (pname_buf), "\\\\%s\\%s", ppi2->pServerName,
6714 ppi2->pShareName);
6715 pname_buf[sizeof (pname_buf) - 1] = '\0';
6717 else
6719 /* a local printer */
6720 strncpy (pname_buf, ppi2->pPortName, sizeof (pname_buf));
6721 pname_buf[sizeof (pname_buf) - 1] = '\0';
6722 /* `pPortName' can include several ports, delimited by ','.
6723 * we only use the first one. */
6724 strtok (pname_buf, ",");
6726 xfree (ppi2);
6729 return build_string (pname_buf);
6732 /***********************************************************************
6733 Initialization
6734 ***********************************************************************/
6736 /* Keep this list in the same order as frame_parms in frame.c.
6737 Use 0 for unsupported frame parameters. */
6739 frame_parm_handler w32_frame_parm_handlers[] =
6741 x_set_autoraise,
6742 x_set_autolower,
6743 x_set_background_color,
6744 x_set_border_color,
6745 x_set_border_width,
6746 x_set_cursor_color,
6747 x_set_cursor_type,
6748 x_set_font,
6749 x_set_foreground_color,
6750 x_set_icon_name,
6751 x_set_icon_type,
6752 x_set_internal_border_width,
6753 x_set_menu_bar_lines,
6754 x_set_mouse_color,
6755 x_explicitly_set_name,
6756 x_set_scroll_bar_width,
6757 x_set_title,
6758 x_set_unsplittable,
6759 x_set_vertical_scroll_bars,
6760 x_set_visibility,
6761 x_set_tool_bar_lines,
6762 0, /* x_set_scroll_bar_foreground, */
6763 0, /* x_set_scroll_bar_background, */
6764 x_set_screen_gamma,
6765 x_set_line_spacing,
6766 x_set_fringe_width,
6767 x_set_fringe_width,
6768 0, /* x_set_wait_for_wm, */
6769 x_set_fullscreen,
6770 x_set_font_backend,
6771 x_set_alpha,
6772 0, /* x_set_sticky */
6773 0, /* x_set_tool_bar_position */
6776 void
6777 syms_of_w32fns (void)
6779 globals_of_w32fns ();
6780 /* This is zero if not using MS-Windows. */
6781 w32_in_use = 0;
6782 track_mouse_window = NULL;
6784 w32_visible_system_caret_hwnd = NULL;
6786 DEFSYM (Qsuppress_icon, "suppress-icon");
6787 DEFSYM (Qundefined_color, "undefined-color");
6788 DEFSYM (Qcancel_timer, "cancel-timer");
6789 DEFSYM (Qhyper, "hyper");
6790 DEFSYM (Qsuper, "super");
6791 DEFSYM (Qmeta, "meta");
6792 DEFSYM (Qalt, "alt");
6793 DEFSYM (Qctrl, "ctrl");
6794 DEFSYM (Qcontrol, "control");
6795 DEFSYM (Qshift, "shift");
6796 DEFSYM (Qfont_param, "font-parameter");
6797 /* This is the end of symbol initialization. */
6800 Fput (Qundefined_color, Qerror_conditions,
6801 listn (CONSTYPE_PURE, 2, Qundefined_color, Qerror));
6802 Fput (Qundefined_color, Qerror_message,
6803 build_pure_c_string ("Undefined color"));
6805 staticpro (&w32_grabbed_keys);
6806 w32_grabbed_keys = Qnil;
6808 DEFVAR_LISP ("w32-color-map", Vw32_color_map,
6809 doc: /* An array of color name mappings for Windows. */);
6810 Vw32_color_map = Qnil;
6812 DEFVAR_LISP ("w32-pass-alt-to-system", Vw32_pass_alt_to_system,
6813 doc: /* Non-nil if Alt key presses are passed on to Windows.
6814 When non-nil, for example, Alt pressed and released and then space will
6815 open the System menu. When nil, Emacs processes the Alt key events, and
6816 then silently swallows them. */);
6817 Vw32_pass_alt_to_system = Qnil;
6819 DEFVAR_LISP ("w32-alt-is-meta", Vw32_alt_is_meta,
6820 doc: /* Non-nil if the Alt key is to be considered the same as the META key.
6821 When nil, Emacs will translate the Alt key to the ALT modifier, not to META. */);
6822 Vw32_alt_is_meta = Qt;
6824 DEFVAR_INT ("w32-quit-key", w32_quit_key,
6825 doc: /* If non-zero, the virtual key code for an alternative quit key. */);
6826 w32_quit_key = 0;
6828 DEFVAR_LISP ("w32-pass-lwindow-to-system",
6829 Vw32_pass_lwindow_to_system,
6830 doc: /* If non-nil, the left \"Windows\" key is passed on to Windows.
6832 When non-nil, the Start menu is opened by tapping the key.
6833 If you set this to nil, the left \"Windows\" key is processed by Emacs
6834 according to the value of `w32-lwindow-modifier', which see.
6836 Note that some combinations of the left \"Windows\" key with other keys are
6837 caught by Windows at low level, and so binding them in Emacs will have no
6838 effect. For example, <lwindow>-r always pops up the Windows Run dialog,
6839 <lwindow>-<Pause> pops up the "System Properties" dialog, etc. However, see
6840 the doc string of `w32-phantom-key-code'. */);
6841 Vw32_pass_lwindow_to_system = Qt;
6843 DEFVAR_LISP ("w32-pass-rwindow-to-system",
6844 Vw32_pass_rwindow_to_system,
6845 doc: /* If non-nil, the right \"Windows\" key is passed on to Windows.
6847 When non-nil, the Start menu is opened by tapping the key.
6848 If you set this to nil, the right \"Windows\" key is processed by Emacs
6849 according to the value of `w32-rwindow-modifier', which see.
6851 Note that some combinations of the right \"Windows\" key with other keys are
6852 caught by Windows at low level, and so binding them in Emacs will have no
6853 effect. For example, <rwindow>-r always pops up the Windows Run dialog,
6854 <rwindow>-<Pause> pops up the "System Properties" dialog, etc. However, see
6855 the doc string of `w32-phantom-key-code'. */);
6856 Vw32_pass_rwindow_to_system = Qt;
6858 DEFVAR_LISP ("w32-phantom-key-code",
6859 Vw32_phantom_key_code,
6860 doc: /* Virtual key code used to generate \"phantom\" key presses.
6861 Value is a number between 0 and 255.
6863 Phantom key presses are generated in order to stop the system from
6864 acting on \"Windows\" key events when `w32-pass-lwindow-to-system' or
6865 `w32-pass-rwindow-to-system' is nil. */);
6866 /* Although 255 is technically not a valid key code, it works and
6867 means that this hack won't interfere with any real key code. */
6868 XSETINT (Vw32_phantom_key_code, 255);
6870 DEFVAR_LISP ("w32-enable-num-lock",
6871 Vw32_enable_num_lock,
6872 doc: /* If non-nil, the Num Lock key acts normally.
6873 Set to nil to handle Num Lock as the `kp-numlock' key. */);
6874 Vw32_enable_num_lock = Qt;
6876 DEFVAR_LISP ("w32-enable-caps-lock",
6877 Vw32_enable_caps_lock,
6878 doc: /* If non-nil, the Caps Lock key acts normally.
6879 Set to nil to handle Caps Lock as the `capslock' key. */);
6880 Vw32_enable_caps_lock = Qt;
6882 DEFVAR_LISP ("w32-scroll-lock-modifier",
6883 Vw32_scroll_lock_modifier,
6884 doc: /* Modifier to use for the Scroll Lock ON state.
6885 The value can be hyper, super, meta, alt, control or shift for the
6886 respective modifier, or nil to handle Scroll Lock as the `scroll' key.
6887 Any other value will cause the Scroll Lock key to be ignored. */);
6888 Vw32_scroll_lock_modifier = Qnil;
6890 DEFVAR_LISP ("w32-lwindow-modifier",
6891 Vw32_lwindow_modifier,
6892 doc: /* Modifier to use for the left \"Windows\" key.
6893 The value can be hyper, super, meta, alt, control or shift for the
6894 respective modifier, or nil to appear as the `lwindow' key.
6895 Any other value will cause the key to be ignored. */);
6896 Vw32_lwindow_modifier = Qnil;
6898 DEFVAR_LISP ("w32-rwindow-modifier",
6899 Vw32_rwindow_modifier,
6900 doc: /* Modifier to use for the right \"Windows\" key.
6901 The value can be hyper, super, meta, alt, control or shift for the
6902 respective modifier, or nil to appear as the `rwindow' key.
6903 Any other value will cause the key to be ignored. */);
6904 Vw32_rwindow_modifier = Qnil;
6906 DEFVAR_LISP ("w32-apps-modifier",
6907 Vw32_apps_modifier,
6908 doc: /* Modifier to use for the \"Apps\" key.
6909 The value can be hyper, super, meta, alt, control or shift for the
6910 respective modifier, or nil to appear as the `apps' key.
6911 Any other value will cause the key to be ignored. */);
6912 Vw32_apps_modifier = Qnil;
6914 DEFVAR_BOOL ("w32-enable-synthesized-fonts", w32_enable_synthesized_fonts,
6915 doc: /* Non-nil enables selection of artificially italicized and bold fonts. */);
6916 w32_enable_synthesized_fonts = 0;
6918 DEFVAR_LISP ("w32-enable-palette", Vw32_enable_palette,
6919 doc: /* Non-nil enables Windows palette management to map colors exactly. */);
6920 Vw32_enable_palette = Qt;
6922 DEFVAR_INT ("w32-mouse-button-tolerance",
6923 w32_mouse_button_tolerance,
6924 doc: /* Analogue of double click interval for faking middle mouse events.
6925 The value is the minimum time in milliseconds that must elapse between
6926 left and right button down events before they are considered distinct events.
6927 If both mouse buttons are depressed within this interval, a middle mouse
6928 button down event is generated instead. */);
6929 w32_mouse_button_tolerance = GetDoubleClickTime () / 2;
6931 DEFVAR_INT ("w32-mouse-move-interval",
6932 w32_mouse_move_interval,
6933 doc: /* Minimum interval between mouse move events.
6934 The value is the minimum time in milliseconds that must elapse between
6935 successive mouse move (or scroll bar drag) events before they are
6936 reported as lisp events. */);
6937 w32_mouse_move_interval = 0;
6939 DEFVAR_BOOL ("w32-pass-extra-mouse-buttons-to-system",
6940 w32_pass_extra_mouse_buttons_to_system,
6941 doc: /* If non-nil, the fourth and fifth mouse buttons are passed to Windows.
6942 Recent versions of Windows support mice with up to five buttons.
6943 Since most applications don't support these extra buttons, most mouse
6944 drivers will allow you to map them to functions at the system level.
6945 If this variable is non-nil, Emacs will pass them on, allowing the
6946 system to handle them. */);
6947 w32_pass_extra_mouse_buttons_to_system = 0;
6949 DEFVAR_BOOL ("w32-pass-multimedia-buttons-to-system",
6950 w32_pass_multimedia_buttons_to_system,
6951 doc: /* If non-nil, media buttons are passed to Windows.
6952 Some modern keyboards contain buttons for controlling media players, web
6953 browsers and other applications. Generally these buttons are handled on a
6954 system wide basis, but by setting this to nil they are made available
6955 to Emacs for binding. Depending on your keyboard, additional keys that
6956 may be available are:
6958 browser-back, browser-forward, browser-refresh, browser-stop,
6959 browser-search, browser-favorites, browser-home,
6960 mail, mail-reply, mail-forward, mail-send,
6961 app-1, app-2,
6962 help, find, new, open, close, save, print, undo, redo, copy, cut, paste,
6963 spell-check, correction-list, toggle-dictate-command,
6964 media-next, media-previous, media-stop, media-play-pause, media-select,
6965 media-play, media-pause, media-record, media-fast-forward, media-rewind,
6966 media-channel-up, media-channel-down,
6967 volume-mute, volume-up, volume-down,
6968 mic-volume-mute, mic-volume-down, mic-volume-up, mic-toggle,
6969 bass-down, bass-boost, bass-up, treble-down, treble-up */);
6970 w32_pass_multimedia_buttons_to_system = 1;
6972 #if 0 /* TODO: Mouse cursor customization. */
6973 DEFVAR_LISP ("x-pointer-shape", Vx_pointer_shape,
6974 doc: /* The shape of the pointer when over text.
6975 Changing the value does not affect existing frames
6976 unless you set the mouse color. */);
6977 Vx_pointer_shape = Qnil;
6979 Vx_nontext_pointer_shape = Qnil;
6981 Vx_mode_pointer_shape = Qnil;
6983 DEFVAR_LISP ("x-hourglass-pointer-shape", Vx_hourglass_pointer_shape,
6984 doc: /* The shape of the pointer when Emacs is busy.
6985 This variable takes effect when you create a new frame
6986 or when you set the mouse color. */);
6987 Vx_hourglass_pointer_shape = Qnil;
6989 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
6990 Vx_sensitive_text_pointer_shape,
6991 doc: /* The shape of the pointer when over mouse-sensitive text.
6992 This variable takes effect when you create a new frame
6993 or when you set the mouse color. */);
6994 Vx_sensitive_text_pointer_shape = Qnil;
6996 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
6997 Vx_window_horizontal_drag_shape,
6998 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
6999 This variable takes effect when you create a new frame
7000 or when you set the mouse color. */);
7001 Vx_window_horizontal_drag_shape = Qnil;
7002 #endif
7004 DEFVAR_LISP ("x-cursor-fore-pixel", Vx_cursor_fore_pixel,
7005 doc: /* A string indicating the foreground color of the cursor box. */);
7006 Vx_cursor_fore_pixel = Qnil;
7008 DEFVAR_LISP ("x-max-tooltip-size", Vx_max_tooltip_size,
7009 doc: /* Maximum size for tooltips.
7010 Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
7011 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
7013 DEFVAR_LISP ("x-no-window-manager", Vx_no_window_manager,
7014 doc: /* Non-nil if no window manager is in use.
7015 Emacs doesn't try to figure this out; this is always nil
7016 unless you set it to something else. */);
7017 /* We don't have any way to find this out, so set it to nil
7018 and maybe the user would like to set it to t. */
7019 Vx_no_window_manager = Qnil;
7021 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
7022 Vx_pixel_size_width_font_regexp,
7023 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
7025 Since Emacs gets width of a font matching with this regexp from
7026 PIXEL_SIZE field of the name, font finding mechanism gets faster for
7027 such a font. This is especially effective for such large fonts as
7028 Chinese, Japanese, and Korean. */);
7029 Vx_pixel_size_width_font_regexp = Qnil;
7031 DEFVAR_LISP ("w32-bdf-filename-alist",
7032 Vw32_bdf_filename_alist,
7033 doc: /* List of bdf fonts and their corresponding filenames. */);
7034 Vw32_bdf_filename_alist = Qnil;
7036 DEFVAR_BOOL ("w32-strict-fontnames",
7037 w32_strict_fontnames,
7038 doc: /* Non-nil means only use fonts that are exact matches for those requested.
7039 Default is nil, which allows old fontnames that are not XLFD compliant,
7040 and allows third-party CJK display to work by specifying false charset
7041 fields to trick Emacs into translating to Big5, SJIS etc.
7042 Setting this to t will prevent wrong fonts being selected when
7043 fontsets are automatically created. */);
7044 w32_strict_fontnames = 0;
7046 DEFVAR_BOOL ("w32-strict-painting",
7047 w32_strict_painting,
7048 doc: /* Non-nil means use strict rules for repainting frames.
7049 Set this to nil to get the old behavior for repainting; this should
7050 only be necessary if the default setting causes problems. */);
7051 w32_strict_painting = 1;
7053 #if 0 /* TODO: Port to W32 */
7054 defsubr (&Sx_change_window_property);
7055 defsubr (&Sx_delete_window_property);
7056 defsubr (&Sx_window_property);
7057 #endif
7058 defsubr (&Sxw_display_color_p);
7059 defsubr (&Sx_display_grayscale_p);
7060 defsubr (&Sxw_color_defined_p);
7061 defsubr (&Sxw_color_values);
7062 defsubr (&Sx_server_max_request_size);
7063 defsubr (&Sx_server_vendor);
7064 defsubr (&Sx_server_version);
7065 defsubr (&Sx_display_pixel_width);
7066 defsubr (&Sx_display_pixel_height);
7067 defsubr (&Sx_display_mm_width);
7068 defsubr (&Sx_display_mm_height);
7069 defsubr (&Sx_display_screens);
7070 defsubr (&Sx_display_planes);
7071 defsubr (&Sx_display_color_cells);
7072 defsubr (&Sx_display_visual_class);
7073 defsubr (&Sx_display_backing_store);
7074 defsubr (&Sx_display_save_under);
7075 defsubr (&Sx_create_frame);
7076 defsubr (&Sx_open_connection);
7077 defsubr (&Sx_close_connection);
7078 defsubr (&Sx_display_list);
7079 defsubr (&Sx_synchronize);
7080 defsubr (&Sx_focus_frame);
7082 /* W32 specific functions */
7084 defsubr (&Sw32_define_rgb_color);
7085 defsubr (&Sw32_default_color_map);
7086 defsubr (&Sw32_send_sys_command);
7087 defsubr (&Sw32_shell_execute);
7088 defsubr (&Sw32_register_hot_key);
7089 defsubr (&Sw32_unregister_hot_key);
7090 defsubr (&Sw32_registered_hot_keys);
7091 defsubr (&Sw32_reconstruct_hot_key);
7092 defsubr (&Sw32_toggle_lock_key);
7093 defsubr (&Sw32_window_exists_p);
7094 defsubr (&Sw32_battery_status);
7096 defsubr (&Sfile_system_info);
7097 defsubr (&Sdefault_printer_name);
7099 check_window_system_func = check_w32;
7101 hourglass_hwnd = NULL;
7103 defsubr (&Sx_show_tip);
7104 defsubr (&Sx_hide_tip);
7105 tip_timer = Qnil;
7106 staticpro (&tip_timer);
7107 tip_frame = Qnil;
7108 staticpro (&tip_frame);
7110 last_show_tip_args = Qnil;
7111 staticpro (&last_show_tip_args);
7113 defsubr (&Sx_file_dialog);
7114 defsubr (&Ssystem_move_file_to_trash);
7119 globals_of_w32fns is used to initialize those global variables that
7120 must always be initialized on startup even when the global variable
7121 initialized is non zero (see the function main in emacs.c).
7122 globals_of_w32fns is called from syms_of_w32fns when the global
7123 variable initialized is 0 and directly from main when initialized
7124 is non zero.
7126 void
7127 globals_of_w32fns (void)
7129 HMODULE user32_lib = GetModuleHandle ("user32.dll");
7131 TrackMouseEvent not available in all versions of Windows, so must load
7132 it dynamically. Do it once, here, instead of every time it is used.
7134 track_mouse_event_fn = (TrackMouseEvent_Proc)
7135 GetProcAddress (user32_lib, "TrackMouseEvent");
7137 monitor_from_point_fn = (MonitorFromPoint_Proc)
7138 GetProcAddress (user32_lib, "MonitorFromPoint");
7139 get_monitor_info_fn = (GetMonitorInfo_Proc)
7140 GetProcAddress (user32_lib, "GetMonitorInfoA");
7143 HMODULE imm32_lib = GetModuleHandle ("imm32.dll");
7144 get_composition_string_fn = (ImmGetCompositionString_Proc)
7145 GetProcAddress (imm32_lib, "ImmGetCompositionStringW");
7146 get_ime_context_fn = (ImmGetContext_Proc)
7147 GetProcAddress (imm32_lib, "ImmGetContext");
7148 release_ime_context_fn = (ImmReleaseContext_Proc)
7149 GetProcAddress (imm32_lib, "ImmReleaseContext");
7150 set_ime_composition_window_fn = (ImmSetCompositionWindow_Proc)
7151 GetProcAddress (imm32_lib, "ImmSetCompositionWindow");
7153 DEFVAR_INT ("w32-ansi-code-page",
7154 w32_ansi_code_page,
7155 doc: /* The ANSI code page used by the system. */);
7156 w32_ansi_code_page = GetACP ();
7158 if (os_subtype == OS_NT)
7159 w32_unicode_gui = 1;
7160 else
7161 w32_unicode_gui = 0;
7163 /* MessageBox does not work without this when linked to comctl32.dll 6.0. */
7164 InitCommonControls ();
7166 syms_of_w32uniscribe ();
7169 void
7170 emacs_abort (void)
7172 int button;
7173 button = MessageBox (NULL,
7174 "A fatal error has occurred!\n\n"
7175 "Would you like to attach a debugger?\n\n"
7176 "Select YES to debug, NO to abort Emacs"
7177 #if __GNUC__
7178 "\n\n(type \"gdb -p <emacs-PID>\" and\n"
7179 "\"continue\" inside GDB before clicking YES.)"
7180 #endif
7181 , "Emacs Abort Dialog",
7182 MB_ICONEXCLAMATION | MB_TASKMODAL
7183 | MB_SETFOREGROUND | MB_YESNO);
7184 switch (button)
7186 case IDYES:
7187 DebugBreak ();
7188 exit (2); /* tell the compiler we will never return */
7189 case IDNO:
7190 default:
7191 abort ();
7192 break;
7196 /* For convenience when debugging. */
7198 w32_last_error (void)
7200 return GetLastError ();