* lisp/vc/ediff-util.el (ediff-diff-at-point): Don't assume point-min==1.
[emacs/old-mirror.git] / src / w32fns.c
blob808e19d0b66fb668981fcf4ad500cf0da75ecc0c
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-zero, a w32 timer that, when it expires, displays an
83 hourglass cursor on all frames. */
84 static unsigned hourglass_timer = 0;
85 static HWND hourglass_hwnd = NULL;
87 #ifndef IDC_HAND
88 #define IDC_HAND MAKEINTRESOURCE(32649)
89 #endif
91 /* Nonzero if using Windows. */
93 static int w32_in_use;
95 Lisp_Object Qsuppress_icon;
96 Lisp_Object Qundefined_color;
97 Lisp_Object Qcancel_timer;
98 Lisp_Object Qfont_param;
99 Lisp_Object Qhyper;
100 Lisp_Object Qsuper;
101 Lisp_Object Qmeta;
102 Lisp_Object Qalt;
103 Lisp_Object Qctrl;
104 Lisp_Object Qcontrol;
105 Lisp_Object Qshift;
108 /* Prefix for system colors. */
109 #define SYSTEM_COLOR_PREFIX "System"
110 #define SYSTEM_COLOR_PREFIX_LEN (sizeof (SYSTEM_COLOR_PREFIX) - 1)
112 /* State variables for emulating a three button mouse. */
113 #define LMOUSE 1
114 #define MMOUSE 2
115 #define RMOUSE 4
117 static int button_state = 0;
118 static W32Msg saved_mouse_button_msg;
119 static unsigned mouse_button_timer = 0; /* non-zero when timer is active */
120 static W32Msg saved_mouse_move_msg;
121 static unsigned mouse_move_timer = 0;
123 /* Window that is tracking the mouse. */
124 static HWND track_mouse_window;
126 /* Multi-monitor API definitions that are not pulled from the headers
127 since we are compiling for NT 4. */
128 #ifndef MONITOR_DEFAULT_TO_NEAREST
129 #define MONITOR_DEFAULT_TO_NEAREST 2
130 #endif
131 /* MinGW headers define MONITORINFO unconditionally, but MSVC ones don't.
132 To avoid a compile error on one or the other, redefine with a new name. */
133 struct MONITOR_INFO
135 DWORD cbSize;
136 RECT rcMonitor;
137 RECT rcWork;
138 DWORD dwFlags;
141 /* Reportedly, MSVC does not have this in its headers. */
142 #ifdef _MSC_VER
143 DECLARE_HANDLE(HMONITOR);
144 #endif
146 typedef BOOL (WINAPI * TrackMouseEvent_Proc)
147 (IN OUT LPTRACKMOUSEEVENT lpEventTrack);
148 typedef LONG (WINAPI * ImmGetCompositionString_Proc)
149 (IN HIMC context, IN DWORD index, OUT LPVOID buffer, IN DWORD bufLen);
150 typedef HIMC (WINAPI * ImmGetContext_Proc) (IN HWND window);
151 typedef HWND (WINAPI * ImmReleaseContext_Proc) (IN HWND wnd, IN HIMC context);
152 typedef HWND (WINAPI * ImmSetCompositionWindow_Proc) (IN HIMC context,
153 IN COMPOSITIONFORM *form);
154 typedef HMONITOR (WINAPI * MonitorFromPoint_Proc) (IN POINT pt, IN DWORD flags);
155 typedef BOOL (WINAPI * GetMonitorInfo_Proc)
156 (IN HMONITOR monitor, OUT struct MONITOR_INFO* info);
158 TrackMouseEvent_Proc track_mouse_event_fn = NULL;
159 ImmGetCompositionString_Proc get_composition_string_fn = NULL;
160 ImmGetContext_Proc get_ime_context_fn = NULL;
161 ImmReleaseContext_Proc release_ime_context_fn = NULL;
162 ImmSetCompositionWindow_Proc set_ime_composition_window_fn = NULL;
163 MonitorFromPoint_Proc monitor_from_point_fn = NULL;
164 GetMonitorInfo_Proc get_monitor_info_fn = NULL;
166 extern AppendMenuW_Proc unicode_append_menu;
168 /* Flag to selectively ignore WM_IME_CHAR messages. */
169 static int ignore_ime_char = 0;
171 /* W95 mousewheel handler */
172 unsigned int msh_mousewheel = 0;
174 /* Timers */
175 #define MOUSE_BUTTON_ID 1
176 #define MOUSE_MOVE_ID 2
177 #define MENU_FREE_ID 3
178 #define HOURGLASS_ID 4
179 /* The delay (milliseconds) before a menu is freed after WM_EXITMENULOOP
180 is received. */
181 #define MENU_FREE_DELAY 1000
182 static unsigned menu_free_timer = 0;
184 #ifdef GLYPH_DEBUG
185 static int image_cache_refcount, dpyinfo_refcount;
186 #endif
188 static HWND w32_visible_system_caret_hwnd;
190 static int w32_unicode_gui;
192 /* From w32menu.c */
193 extern HMENU current_popup_menu;
194 static int menubar_in_use = 0;
196 /* From w32uniscribe.c */
197 extern void syms_of_w32uniscribe (void);
198 extern int uniscribe_available;
200 /* Function prototypes for hourglass support. */
201 static void w32_show_hourglass (struct frame *);
202 static void w32_hide_hourglass (void);
206 /* Error if we are not connected to MS-Windows. */
207 void
208 check_w32 (void)
210 if (! w32_in_use)
211 error ("MS-Windows not in use or not initialized");
214 /* Nonzero if we can use mouse menus.
215 You should not call this unless HAVE_MENUS is defined. */
218 have_menus_p (void)
220 return w32_in_use;
223 /* Extract a frame as a FRAME_PTR, defaulting to the selected frame
224 and checking validity for W32. */
226 FRAME_PTR
227 check_x_frame (Lisp_Object frame)
229 FRAME_PTR f;
231 if (NILP (frame))
232 frame = selected_frame;
233 CHECK_LIVE_FRAME (frame);
234 f = XFRAME (frame);
235 if (! FRAME_W32_P (f))
236 error ("Non-W32 frame used");
237 return f;
240 /* Let the user specify a display with a frame.
241 nil stands for the selected frame--or, if that is not a w32 frame,
242 the first display on the list. */
244 struct w32_display_info *
245 check_x_display_info (Lisp_Object frame)
247 if (NILP (frame))
249 struct frame *sf = XFRAME (selected_frame);
251 if (FRAME_W32_P (sf) && FRAME_LIVE_P (sf))
252 return FRAME_W32_DISPLAY_INFO (sf);
253 else
254 return &one_w32_display_info;
256 else if (STRINGP (frame))
257 return x_display_info_for_name (frame);
258 else
260 FRAME_PTR f;
262 CHECK_LIVE_FRAME (frame);
263 f = XFRAME (frame);
264 if (! FRAME_W32_P (f))
265 error ("Non-W32 frame used");
266 return FRAME_W32_DISPLAY_INFO (f);
270 /* Return the Emacs frame-object corresponding to an w32 window.
271 It could be the frame's main window or an icon window. */
273 /* This function can be called during GC, so use GC_xxx type test macros. */
275 struct frame *
276 x_window_to_frame (struct w32_display_info *dpyinfo, HWND wdesc)
278 Lisp_Object tail, frame;
279 struct frame *f;
281 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
283 frame = XCAR (tail);
284 if (!FRAMEP (frame))
285 continue;
286 f = XFRAME (frame);
287 if (!FRAME_W32_P (f) || FRAME_W32_DISPLAY_INFO (f) != dpyinfo)
288 continue;
290 if (FRAME_W32_WINDOW (f) == wdesc)
291 return f;
293 return 0;
297 static Lisp_Object unwind_create_frame (Lisp_Object);
298 static Lisp_Object unwind_create_tip_frame (Lisp_Object);
299 static void my_create_window (struct frame *);
300 static void my_create_tip_window (struct frame *);
302 /* TODO: Native Input Method support; see x_create_im. */
303 void x_set_foreground_color (struct frame *, Lisp_Object, Lisp_Object);
304 void x_set_background_color (struct frame *, Lisp_Object, Lisp_Object);
305 void x_set_mouse_color (struct frame *, Lisp_Object, Lisp_Object);
306 void x_set_cursor_color (struct frame *, Lisp_Object, Lisp_Object);
307 void x_set_border_color (struct frame *, Lisp_Object, Lisp_Object);
308 void x_set_cursor_type (struct frame *, Lisp_Object, Lisp_Object);
309 void x_set_icon_type (struct frame *, Lisp_Object, Lisp_Object);
310 void x_set_icon_name (struct frame *, Lisp_Object, Lisp_Object);
311 void x_explicitly_set_name (struct frame *, Lisp_Object, Lisp_Object);
312 void x_set_menu_bar_lines (struct frame *, Lisp_Object, Lisp_Object);
313 void x_set_title (struct frame *, Lisp_Object, Lisp_Object);
314 void x_set_tool_bar_lines (struct frame *, Lisp_Object, Lisp_Object);
319 /* Store the screen positions of frame F into XPTR and YPTR.
320 These are the positions of the containing window manager window,
321 not Emacs's own window. */
323 void
324 x_real_positions (FRAME_PTR f, int *xptr, int *yptr)
326 POINT pt;
327 RECT rect;
329 /* Get the bounds of the WM window. */
330 GetWindowRect (FRAME_W32_WINDOW (f), &rect);
332 pt.x = 0;
333 pt.y = 0;
335 /* Convert (0, 0) in the client area to screen co-ordinates. */
336 ClientToScreen (FRAME_W32_WINDOW (f), &pt);
338 /* Remember x_pixels_diff and y_pixels_diff. */
339 f->x_pixels_diff = pt.x - rect.left;
340 f->y_pixels_diff = pt.y - rect.top;
342 *xptr = rect.left;
343 *yptr = rect.top;
348 DEFUN ("w32-define-rgb-color", Fw32_define_rgb_color,
349 Sw32_define_rgb_color, 4, 4, 0,
350 doc: /* Convert RGB numbers to a Windows color reference and associate with NAME.
351 This adds or updates a named color to `w32-color-map', making it
352 available for use. The original entry's RGB ref is returned, or nil
353 if the entry is new. */)
354 (Lisp_Object red, Lisp_Object green, Lisp_Object blue, Lisp_Object name)
356 Lisp_Object rgb;
357 Lisp_Object oldrgb = Qnil;
358 Lisp_Object entry;
360 CHECK_NUMBER (red);
361 CHECK_NUMBER (green);
362 CHECK_NUMBER (blue);
363 CHECK_STRING (name);
365 XSETINT (rgb, RGB (XUINT (red), XUINT (green), XUINT (blue)));
367 block_input ();
369 /* replace existing entry in w32-color-map or add new entry. */
370 entry = Fassoc (name, Vw32_color_map);
371 if (NILP (entry))
373 entry = Fcons (name, rgb);
374 Vw32_color_map = Fcons (entry, Vw32_color_map);
376 else
378 oldrgb = Fcdr (entry);
379 Fsetcdr (entry, rgb);
382 unblock_input ();
384 return (oldrgb);
387 /* The default colors for the w32 color map */
388 typedef struct colormap_t
390 char *name;
391 COLORREF colorref;
392 } colormap_t;
394 colormap_t w32_color_map[] =
396 {"snow" , PALETTERGB (255,250,250)},
397 {"ghost white" , PALETTERGB (248,248,255)},
398 {"GhostWhite" , PALETTERGB (248,248,255)},
399 {"white smoke" , PALETTERGB (245,245,245)},
400 {"WhiteSmoke" , PALETTERGB (245,245,245)},
401 {"gainsboro" , PALETTERGB (220,220,220)},
402 {"floral white" , PALETTERGB (255,250,240)},
403 {"FloralWhite" , PALETTERGB (255,250,240)},
404 {"old lace" , PALETTERGB (253,245,230)},
405 {"OldLace" , PALETTERGB (253,245,230)},
406 {"linen" , PALETTERGB (250,240,230)},
407 {"antique white" , PALETTERGB (250,235,215)},
408 {"AntiqueWhite" , PALETTERGB (250,235,215)},
409 {"papaya whip" , PALETTERGB (255,239,213)},
410 {"PapayaWhip" , PALETTERGB (255,239,213)},
411 {"blanched almond" , PALETTERGB (255,235,205)},
412 {"BlanchedAlmond" , PALETTERGB (255,235,205)},
413 {"bisque" , PALETTERGB (255,228,196)},
414 {"peach puff" , PALETTERGB (255,218,185)},
415 {"PeachPuff" , PALETTERGB (255,218,185)},
416 {"navajo white" , PALETTERGB (255,222,173)},
417 {"NavajoWhite" , PALETTERGB (255,222,173)},
418 {"moccasin" , PALETTERGB (255,228,181)},
419 {"cornsilk" , PALETTERGB (255,248,220)},
420 {"ivory" , PALETTERGB (255,255,240)},
421 {"lemon chiffon" , PALETTERGB (255,250,205)},
422 {"LemonChiffon" , PALETTERGB (255,250,205)},
423 {"seashell" , PALETTERGB (255,245,238)},
424 {"honeydew" , PALETTERGB (240,255,240)},
425 {"mint cream" , PALETTERGB (245,255,250)},
426 {"MintCream" , PALETTERGB (245,255,250)},
427 {"azure" , PALETTERGB (240,255,255)},
428 {"alice blue" , PALETTERGB (240,248,255)},
429 {"AliceBlue" , PALETTERGB (240,248,255)},
430 {"lavender" , PALETTERGB (230,230,250)},
431 {"lavender blush" , PALETTERGB (255,240,245)},
432 {"LavenderBlush" , PALETTERGB (255,240,245)},
433 {"misty rose" , PALETTERGB (255,228,225)},
434 {"MistyRose" , PALETTERGB (255,228,225)},
435 {"white" , PALETTERGB (255,255,255)},
436 {"black" , PALETTERGB ( 0, 0, 0)},
437 {"dark slate gray" , PALETTERGB ( 47, 79, 79)},
438 {"DarkSlateGray" , PALETTERGB ( 47, 79, 79)},
439 {"dark slate grey" , PALETTERGB ( 47, 79, 79)},
440 {"DarkSlateGrey" , PALETTERGB ( 47, 79, 79)},
441 {"dim gray" , PALETTERGB (105,105,105)},
442 {"DimGray" , PALETTERGB (105,105,105)},
443 {"dim grey" , PALETTERGB (105,105,105)},
444 {"DimGrey" , PALETTERGB (105,105,105)},
445 {"slate gray" , PALETTERGB (112,128,144)},
446 {"SlateGray" , PALETTERGB (112,128,144)},
447 {"slate grey" , PALETTERGB (112,128,144)},
448 {"SlateGrey" , PALETTERGB (112,128,144)},
449 {"light slate gray" , PALETTERGB (119,136,153)},
450 {"LightSlateGray" , PALETTERGB (119,136,153)},
451 {"light slate grey" , PALETTERGB (119,136,153)},
452 {"LightSlateGrey" , PALETTERGB (119,136,153)},
453 {"gray" , PALETTERGB (190,190,190)},
454 {"grey" , PALETTERGB (190,190,190)},
455 {"light grey" , PALETTERGB (211,211,211)},
456 {"LightGrey" , PALETTERGB (211,211,211)},
457 {"light gray" , PALETTERGB (211,211,211)},
458 {"LightGray" , PALETTERGB (211,211,211)},
459 {"midnight blue" , PALETTERGB ( 25, 25,112)},
460 {"MidnightBlue" , PALETTERGB ( 25, 25,112)},
461 {"navy" , PALETTERGB ( 0, 0,128)},
462 {"navy blue" , PALETTERGB ( 0, 0,128)},
463 {"NavyBlue" , PALETTERGB ( 0, 0,128)},
464 {"cornflower blue" , PALETTERGB (100,149,237)},
465 {"CornflowerBlue" , PALETTERGB (100,149,237)},
466 {"dark slate blue" , PALETTERGB ( 72, 61,139)},
467 {"DarkSlateBlue" , PALETTERGB ( 72, 61,139)},
468 {"slate blue" , PALETTERGB (106, 90,205)},
469 {"SlateBlue" , PALETTERGB (106, 90,205)},
470 {"medium slate blue" , PALETTERGB (123,104,238)},
471 {"MediumSlateBlue" , PALETTERGB (123,104,238)},
472 {"light slate blue" , PALETTERGB (132,112,255)},
473 {"LightSlateBlue" , PALETTERGB (132,112,255)},
474 {"medium blue" , PALETTERGB ( 0, 0,205)},
475 {"MediumBlue" , PALETTERGB ( 0, 0,205)},
476 {"royal blue" , PALETTERGB ( 65,105,225)},
477 {"RoyalBlue" , PALETTERGB ( 65,105,225)},
478 {"blue" , PALETTERGB ( 0, 0,255)},
479 {"dodger blue" , PALETTERGB ( 30,144,255)},
480 {"DodgerBlue" , PALETTERGB ( 30,144,255)},
481 {"deep sky blue" , PALETTERGB ( 0,191,255)},
482 {"DeepSkyBlue" , PALETTERGB ( 0,191,255)},
483 {"sky blue" , PALETTERGB (135,206,235)},
484 {"SkyBlue" , PALETTERGB (135,206,235)},
485 {"light sky blue" , PALETTERGB (135,206,250)},
486 {"LightSkyBlue" , PALETTERGB (135,206,250)},
487 {"steel blue" , PALETTERGB ( 70,130,180)},
488 {"SteelBlue" , PALETTERGB ( 70,130,180)},
489 {"light steel blue" , PALETTERGB (176,196,222)},
490 {"LightSteelBlue" , PALETTERGB (176,196,222)},
491 {"light blue" , PALETTERGB (173,216,230)},
492 {"LightBlue" , PALETTERGB (173,216,230)},
493 {"powder blue" , PALETTERGB (176,224,230)},
494 {"PowderBlue" , PALETTERGB (176,224,230)},
495 {"pale turquoise" , PALETTERGB (175,238,238)},
496 {"PaleTurquoise" , PALETTERGB (175,238,238)},
497 {"dark turquoise" , PALETTERGB ( 0,206,209)},
498 {"DarkTurquoise" , PALETTERGB ( 0,206,209)},
499 {"medium turquoise" , PALETTERGB ( 72,209,204)},
500 {"MediumTurquoise" , PALETTERGB ( 72,209,204)},
501 {"turquoise" , PALETTERGB ( 64,224,208)},
502 {"cyan" , PALETTERGB ( 0,255,255)},
503 {"light cyan" , PALETTERGB (224,255,255)},
504 {"LightCyan" , PALETTERGB (224,255,255)},
505 {"cadet blue" , PALETTERGB ( 95,158,160)},
506 {"CadetBlue" , PALETTERGB ( 95,158,160)},
507 {"medium aquamarine" , PALETTERGB (102,205,170)},
508 {"MediumAquamarine" , PALETTERGB (102,205,170)},
509 {"aquamarine" , PALETTERGB (127,255,212)},
510 {"dark green" , PALETTERGB ( 0,100, 0)},
511 {"DarkGreen" , PALETTERGB ( 0,100, 0)},
512 {"dark olive green" , PALETTERGB ( 85,107, 47)},
513 {"DarkOliveGreen" , PALETTERGB ( 85,107, 47)},
514 {"dark sea green" , PALETTERGB (143,188,143)},
515 {"DarkSeaGreen" , PALETTERGB (143,188,143)},
516 {"sea green" , PALETTERGB ( 46,139, 87)},
517 {"SeaGreen" , PALETTERGB ( 46,139, 87)},
518 {"medium sea green" , PALETTERGB ( 60,179,113)},
519 {"MediumSeaGreen" , PALETTERGB ( 60,179,113)},
520 {"light sea green" , PALETTERGB ( 32,178,170)},
521 {"LightSeaGreen" , PALETTERGB ( 32,178,170)},
522 {"pale green" , PALETTERGB (152,251,152)},
523 {"PaleGreen" , PALETTERGB (152,251,152)},
524 {"spring green" , PALETTERGB ( 0,255,127)},
525 {"SpringGreen" , PALETTERGB ( 0,255,127)},
526 {"lawn green" , PALETTERGB (124,252, 0)},
527 {"LawnGreen" , PALETTERGB (124,252, 0)},
528 {"green" , PALETTERGB ( 0,255, 0)},
529 {"chartreuse" , PALETTERGB (127,255, 0)},
530 {"medium spring green" , PALETTERGB ( 0,250,154)},
531 {"MediumSpringGreen" , PALETTERGB ( 0,250,154)},
532 {"green yellow" , PALETTERGB (173,255, 47)},
533 {"GreenYellow" , PALETTERGB (173,255, 47)},
534 {"lime green" , PALETTERGB ( 50,205, 50)},
535 {"LimeGreen" , PALETTERGB ( 50,205, 50)},
536 {"yellow green" , PALETTERGB (154,205, 50)},
537 {"YellowGreen" , PALETTERGB (154,205, 50)},
538 {"forest green" , PALETTERGB ( 34,139, 34)},
539 {"ForestGreen" , PALETTERGB ( 34,139, 34)},
540 {"olive drab" , PALETTERGB (107,142, 35)},
541 {"OliveDrab" , PALETTERGB (107,142, 35)},
542 {"dark khaki" , PALETTERGB (189,183,107)},
543 {"DarkKhaki" , PALETTERGB (189,183,107)},
544 {"khaki" , PALETTERGB (240,230,140)},
545 {"pale goldenrod" , PALETTERGB (238,232,170)},
546 {"PaleGoldenrod" , PALETTERGB (238,232,170)},
547 {"light goldenrod yellow" , PALETTERGB (250,250,210)},
548 {"LightGoldenrodYellow" , PALETTERGB (250,250,210)},
549 {"light yellow" , PALETTERGB (255,255,224)},
550 {"LightYellow" , PALETTERGB (255,255,224)},
551 {"yellow" , PALETTERGB (255,255, 0)},
552 {"gold" , PALETTERGB (255,215, 0)},
553 {"light goldenrod" , PALETTERGB (238,221,130)},
554 {"LightGoldenrod" , PALETTERGB (238,221,130)},
555 {"goldenrod" , PALETTERGB (218,165, 32)},
556 {"dark goldenrod" , PALETTERGB (184,134, 11)},
557 {"DarkGoldenrod" , PALETTERGB (184,134, 11)},
558 {"rosy brown" , PALETTERGB (188,143,143)},
559 {"RosyBrown" , PALETTERGB (188,143,143)},
560 {"indian red" , PALETTERGB (205, 92, 92)},
561 {"IndianRed" , PALETTERGB (205, 92, 92)},
562 {"saddle brown" , PALETTERGB (139, 69, 19)},
563 {"SaddleBrown" , PALETTERGB (139, 69, 19)},
564 {"sienna" , PALETTERGB (160, 82, 45)},
565 {"peru" , PALETTERGB (205,133, 63)},
566 {"burlywood" , PALETTERGB (222,184,135)},
567 {"beige" , PALETTERGB (245,245,220)},
568 {"wheat" , PALETTERGB (245,222,179)},
569 {"sandy brown" , PALETTERGB (244,164, 96)},
570 {"SandyBrown" , PALETTERGB (244,164, 96)},
571 {"tan" , PALETTERGB (210,180,140)},
572 {"chocolate" , PALETTERGB (210,105, 30)},
573 {"firebrick" , PALETTERGB (178,34, 34)},
574 {"brown" , PALETTERGB (165,42, 42)},
575 {"dark salmon" , PALETTERGB (233,150,122)},
576 {"DarkSalmon" , PALETTERGB (233,150,122)},
577 {"salmon" , PALETTERGB (250,128,114)},
578 {"light salmon" , PALETTERGB (255,160,122)},
579 {"LightSalmon" , PALETTERGB (255,160,122)},
580 {"orange" , PALETTERGB (255,165, 0)},
581 {"dark orange" , PALETTERGB (255,140, 0)},
582 {"DarkOrange" , PALETTERGB (255,140, 0)},
583 {"coral" , PALETTERGB (255,127, 80)},
584 {"light coral" , PALETTERGB (240,128,128)},
585 {"LightCoral" , PALETTERGB (240,128,128)},
586 {"tomato" , PALETTERGB (255, 99, 71)},
587 {"orange red" , PALETTERGB (255, 69, 0)},
588 {"OrangeRed" , PALETTERGB (255, 69, 0)},
589 {"red" , PALETTERGB (255, 0, 0)},
590 {"hot pink" , PALETTERGB (255,105,180)},
591 {"HotPink" , PALETTERGB (255,105,180)},
592 {"deep pink" , PALETTERGB (255, 20,147)},
593 {"DeepPink" , PALETTERGB (255, 20,147)},
594 {"pink" , PALETTERGB (255,192,203)},
595 {"light pink" , PALETTERGB (255,182,193)},
596 {"LightPink" , PALETTERGB (255,182,193)},
597 {"pale violet red" , PALETTERGB (219,112,147)},
598 {"PaleVioletRed" , PALETTERGB (219,112,147)},
599 {"maroon" , PALETTERGB (176, 48, 96)},
600 {"medium violet red" , PALETTERGB (199, 21,133)},
601 {"MediumVioletRed" , PALETTERGB (199, 21,133)},
602 {"violet red" , PALETTERGB (208, 32,144)},
603 {"VioletRed" , PALETTERGB (208, 32,144)},
604 {"magenta" , PALETTERGB (255, 0,255)},
605 {"violet" , PALETTERGB (238,130,238)},
606 {"plum" , PALETTERGB (221,160,221)},
607 {"orchid" , PALETTERGB (218,112,214)},
608 {"medium orchid" , PALETTERGB (186, 85,211)},
609 {"MediumOrchid" , PALETTERGB (186, 85,211)},
610 {"dark orchid" , PALETTERGB (153, 50,204)},
611 {"DarkOrchid" , PALETTERGB (153, 50,204)},
612 {"dark violet" , PALETTERGB (148, 0,211)},
613 {"DarkViolet" , PALETTERGB (148, 0,211)},
614 {"blue violet" , PALETTERGB (138, 43,226)},
615 {"BlueViolet" , PALETTERGB (138, 43,226)},
616 {"purple" , PALETTERGB (160, 32,240)},
617 {"medium purple" , PALETTERGB (147,112,219)},
618 {"MediumPurple" , PALETTERGB (147,112,219)},
619 {"thistle" , PALETTERGB (216,191,216)},
620 {"gray0" , PALETTERGB ( 0, 0, 0)},
621 {"grey0" , PALETTERGB ( 0, 0, 0)},
622 {"dark grey" , PALETTERGB (169,169,169)},
623 {"DarkGrey" , PALETTERGB (169,169,169)},
624 {"dark gray" , PALETTERGB (169,169,169)},
625 {"DarkGray" , PALETTERGB (169,169,169)},
626 {"dark blue" , PALETTERGB ( 0, 0,139)},
627 {"DarkBlue" , PALETTERGB ( 0, 0,139)},
628 {"dark cyan" , PALETTERGB ( 0,139,139)},
629 {"DarkCyan" , PALETTERGB ( 0,139,139)},
630 {"dark magenta" , PALETTERGB (139, 0,139)},
631 {"DarkMagenta" , PALETTERGB (139, 0,139)},
632 {"dark red" , PALETTERGB (139, 0, 0)},
633 {"DarkRed" , PALETTERGB (139, 0, 0)},
634 {"light green" , PALETTERGB (144,238,144)},
635 {"LightGreen" , PALETTERGB (144,238,144)},
638 static Lisp_Object
639 w32_default_color_map (void)
641 int i;
642 colormap_t *pc = w32_color_map;
643 Lisp_Object cmap;
645 block_input ();
647 cmap = Qnil;
649 for (i = 0; i < sizeof (w32_color_map) / sizeof (w32_color_map[0]);
650 pc++, i++)
651 cmap = Fcons (Fcons (build_string (pc->name),
652 make_number (pc->colorref)),
653 cmap);
655 unblock_input ();
657 return (cmap);
660 DEFUN ("w32-default-color-map", Fw32_default_color_map, Sw32_default_color_map,
661 0, 0, 0, doc: /* Return the default color map. */)
662 (void)
664 return w32_default_color_map ();
667 static Lisp_Object
668 w32_color_map_lookup (char *colorname)
670 Lisp_Object tail, ret = Qnil;
672 block_input ();
674 for (tail = Vw32_color_map; CONSP (tail); tail = XCDR (tail))
676 register Lisp_Object elt, tem;
678 elt = XCAR (tail);
679 if (!CONSP (elt)) continue;
681 tem = XCAR (elt);
683 if (lstrcmpi (SDATA (tem), colorname) == 0)
685 ret = Fcdr (elt);
686 break;
689 QUIT;
692 unblock_input ();
694 return ret;
698 static void
699 add_system_logical_colors_to_map (Lisp_Object *system_colors)
701 HKEY colors_key;
703 /* Other registry operations are done with input blocked. */
704 block_input ();
706 /* Look for "Control Panel/Colors" under User and Machine registry
707 settings. */
708 if (RegOpenKeyEx (HKEY_CURRENT_USER, "Control Panel\\Colors", 0,
709 KEY_READ, &colors_key) == ERROR_SUCCESS
710 || RegOpenKeyEx (HKEY_LOCAL_MACHINE, "Control Panel\\Colors", 0,
711 KEY_READ, &colors_key) == ERROR_SUCCESS)
713 /* List all keys. */
714 char color_buffer[64];
715 char full_name_buffer[MAX_PATH + SYSTEM_COLOR_PREFIX_LEN];
716 int index = 0;
717 DWORD name_size, color_size;
718 char *name_buffer = full_name_buffer + SYSTEM_COLOR_PREFIX_LEN;
720 name_size = sizeof (full_name_buffer) - SYSTEM_COLOR_PREFIX_LEN;
721 color_size = sizeof (color_buffer);
723 strcpy (full_name_buffer, SYSTEM_COLOR_PREFIX);
725 while (RegEnumValueA (colors_key, index, name_buffer, &name_size,
726 NULL, NULL, color_buffer, &color_size)
727 == ERROR_SUCCESS)
729 int r, g, b;
730 if (sscanf (color_buffer, " %u %u %u", &r, &g, &b) == 3)
731 *system_colors = Fcons (Fcons (build_string (full_name_buffer),
732 make_number (RGB (r, g, b))),
733 *system_colors);
735 name_size = sizeof (full_name_buffer) - SYSTEM_COLOR_PREFIX_LEN;
736 color_size = sizeof (color_buffer);
737 index++;
739 RegCloseKey (colors_key);
742 unblock_input ();
746 static Lisp_Object
747 x_to_w32_color (char * colorname)
749 register Lisp_Object ret = Qnil;
751 block_input ();
753 if (colorname[0] == '#')
755 /* Could be an old-style RGB Device specification. */
756 char *color;
757 int size;
758 color = colorname + 1;
760 size = strlen (color);
761 if (size == 3 || size == 6 || size == 9 || size == 12)
763 UINT colorval;
764 int i, pos;
765 pos = 0;
766 size /= 3;
767 colorval = 0;
769 for (i = 0; i < 3; i++)
771 char *end;
772 char t;
773 unsigned long value;
775 /* The check for 'x' in the following conditional takes into
776 account the fact that strtol allows a "0x" in front of
777 our numbers, and we don't. */
778 if (!isxdigit (color[0]) || color[1] == 'x')
779 break;
780 t = color[size];
781 color[size] = '\0';
782 value = strtoul (color, &end, 16);
783 color[size] = t;
784 if (errno == ERANGE || end - color != size)
785 break;
786 switch (size)
788 case 1:
789 value = value * 0x10;
790 break;
791 case 2:
792 break;
793 case 3:
794 value /= 0x10;
795 break;
796 case 4:
797 value /= 0x100;
798 break;
800 colorval |= (value << pos);
801 pos += 0x8;
802 if (i == 2)
804 unblock_input ();
805 XSETINT (ret, colorval);
806 return ret;
808 color = end;
812 else if (strnicmp (colorname, "rgb:", 4) == 0)
814 char *color;
815 UINT colorval;
816 int i, pos;
817 pos = 0;
819 colorval = 0;
820 color = colorname + 4;
821 for (i = 0; i < 3; i++)
823 char *end;
824 unsigned long value;
826 /* The check for 'x' in the following conditional takes into
827 account the fact that strtol allows a "0x" in front of
828 our numbers, and we don't. */
829 if (!isxdigit (color[0]) || color[1] == 'x')
830 break;
831 value = strtoul (color, &end, 16);
832 if (errno == ERANGE)
833 break;
834 switch (end - color)
836 case 1:
837 value = value * 0x10 + value;
838 break;
839 case 2:
840 break;
841 case 3:
842 value /= 0x10;
843 break;
844 case 4:
845 value /= 0x100;
846 break;
847 default:
848 value = ULONG_MAX;
850 if (value == ULONG_MAX)
851 break;
852 colorval |= (value << pos);
853 pos += 0x8;
854 if (i == 2)
856 if (*end != '\0')
857 break;
858 unblock_input ();
859 XSETINT (ret, colorval);
860 return ret;
862 if (*end != '/')
863 break;
864 color = end + 1;
867 else if (strnicmp (colorname, "rgbi:", 5) == 0)
869 /* This is an RGB Intensity specification. */
870 char *color;
871 UINT colorval;
872 int i, pos;
873 pos = 0;
875 colorval = 0;
876 color = colorname + 5;
877 for (i = 0; i < 3; i++)
879 char *end;
880 double value;
881 UINT val;
883 value = strtod (color, &end);
884 if (errno == ERANGE)
885 break;
886 if (value < 0.0 || value > 1.0)
887 break;
888 val = (UINT)(0x100 * value);
889 /* We used 0x100 instead of 0xFF to give a continuous
890 range between 0.0 and 1.0 inclusive. The next statement
891 fixes the 1.0 case. */
892 if (val == 0x100)
893 val = 0xFF;
894 colorval |= (val << pos);
895 pos += 0x8;
896 if (i == 2)
898 if (*end != '\0')
899 break;
900 unblock_input ();
901 XSETINT (ret, colorval);
902 return ret;
904 if (*end != '/')
905 break;
906 color = end + 1;
909 /* I am not going to attempt to handle any of the CIE color schemes
910 or TekHVC, since I don't know the algorithms for conversion to
911 RGB. */
913 /* If we fail to lookup the color name in w32_color_map, then check the
914 colorname to see if it can be crudely approximated: If the X color
915 ends in a number (e.g., "darkseagreen2"), strip the number and
916 return the result of looking up the base color name. */
917 ret = w32_color_map_lookup (colorname);
918 if (NILP (ret))
920 int len = strlen (colorname);
922 if (isdigit (colorname[len - 1]))
924 char *ptr, *approx = alloca (len + 1);
926 strcpy (approx, colorname);
927 ptr = &approx[len - 1];
928 while (ptr > approx && isdigit (*ptr))
929 *ptr-- = '\0';
931 ret = w32_color_map_lookup (approx);
935 unblock_input ();
936 return ret;
939 void
940 w32_regenerate_palette (FRAME_PTR f)
942 struct w32_palette_entry * list;
943 LOGPALETTE * log_palette;
944 HPALETTE new_palette;
945 int i;
947 /* don't bother trying to create palette if not supported */
948 if (! FRAME_W32_DISPLAY_INFO (f)->has_palette)
949 return;
951 log_palette = (LOGPALETTE *)
952 alloca (sizeof (LOGPALETTE) +
953 FRAME_W32_DISPLAY_INFO (f)->num_colors * sizeof (PALETTEENTRY));
954 log_palette->palVersion = 0x300;
955 log_palette->palNumEntries = FRAME_W32_DISPLAY_INFO (f)->num_colors;
957 list = FRAME_W32_DISPLAY_INFO (f)->color_list;
958 for (i = 0;
959 i < FRAME_W32_DISPLAY_INFO (f)->num_colors;
960 i++, list = list->next)
961 log_palette->palPalEntry[i] = list->entry;
963 new_palette = CreatePalette (log_palette);
965 enter_crit ();
967 if (FRAME_W32_DISPLAY_INFO (f)->palette)
968 DeleteObject (FRAME_W32_DISPLAY_INFO (f)->palette);
969 FRAME_W32_DISPLAY_INFO (f)->palette = new_palette;
971 /* Realize display palette and garbage all frames. */
972 release_frame_dc (f, get_frame_dc (f));
974 leave_crit ();
977 #define W32_COLOR(pe) RGB (pe.peRed, pe.peGreen, pe.peBlue)
978 #define SET_W32_COLOR(pe, color) \
979 do \
981 pe.peRed = GetRValue (color); \
982 pe.peGreen = GetGValue (color); \
983 pe.peBlue = GetBValue (color); \
984 pe.peFlags = 0; \
985 } while (0)
987 #if 0
988 /* Keep these around in case we ever want to track color usage. */
989 void
990 w32_map_color (FRAME_PTR f, COLORREF color)
992 struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
994 if (NILP (Vw32_enable_palette))
995 return;
997 /* check if color is already mapped */
998 while (list)
1000 if (W32_COLOR (list->entry) == color)
1002 ++list->refcount;
1003 return;
1005 list = list->next;
1008 /* not already mapped, so add to list and recreate Windows palette */
1009 list = xmalloc (sizeof (struct w32_palette_entry));
1010 SET_W32_COLOR (list->entry, color);
1011 list->refcount = 1;
1012 list->next = FRAME_W32_DISPLAY_INFO (f)->color_list;
1013 FRAME_W32_DISPLAY_INFO (f)->color_list = list;
1014 FRAME_W32_DISPLAY_INFO (f)->num_colors++;
1016 /* set flag that palette must be regenerated */
1017 FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
1020 void
1021 w32_unmap_color (FRAME_PTR f, COLORREF color)
1023 struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
1024 struct w32_palette_entry **prev = &FRAME_W32_DISPLAY_INFO (f)->color_list;
1026 if (NILP (Vw32_enable_palette))
1027 return;
1029 /* check if color is already mapped */
1030 while (list)
1032 if (W32_COLOR (list->entry) == color)
1034 if (--list->refcount == 0)
1036 *prev = list->next;
1037 xfree (list);
1038 FRAME_W32_DISPLAY_INFO (f)->num_colors--;
1039 break;
1041 else
1042 return;
1044 prev = &list->next;
1045 list = list->next;
1048 /* set flag that palette must be regenerated */
1049 FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
1051 #endif
1054 /* Gamma-correct COLOR on frame F. */
1056 void
1057 gamma_correct (struct frame *f, COLORREF *color)
1059 if (f->gamma)
1061 *color = PALETTERGB (
1062 pow (GetRValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
1063 pow (GetGValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
1064 pow (GetBValue (*color) / 255.0, f->gamma) * 255.0 + 0.5);
1069 /* Decide if color named COLOR is valid for the display associated with
1070 the selected frame; if so, return the rgb values in COLOR_DEF.
1071 If ALLOC is nonzero, allocate a new colormap cell. */
1074 w32_defined_color (FRAME_PTR f, char *color, XColor *color_def, int alloc)
1076 register Lisp_Object tem;
1077 COLORREF w32_color_ref;
1079 tem = x_to_w32_color (color);
1081 if (!NILP (tem))
1083 if (f)
1085 /* Apply gamma correction. */
1086 w32_color_ref = XUINT (tem);
1087 gamma_correct (f, &w32_color_ref);
1088 XSETINT (tem, w32_color_ref);
1091 /* Map this color to the palette if it is enabled. */
1092 if (!NILP (Vw32_enable_palette))
1094 struct w32_palette_entry * entry =
1095 one_w32_display_info.color_list;
1096 struct w32_palette_entry ** prev =
1097 &one_w32_display_info.color_list;
1099 /* check if color is already mapped */
1100 while (entry)
1102 if (W32_COLOR (entry->entry) == XUINT (tem))
1103 break;
1104 prev = &entry->next;
1105 entry = entry->next;
1108 if (entry == NULL && alloc)
1110 /* not already mapped, so add to list */
1111 entry = xmalloc (sizeof (struct w32_palette_entry));
1112 SET_W32_COLOR (entry->entry, XUINT (tem));
1113 entry->next = NULL;
1114 *prev = entry;
1115 one_w32_display_info.num_colors++;
1117 /* set flag that palette must be regenerated */
1118 one_w32_display_info.regen_palette = TRUE;
1121 /* Ensure COLORREF value is snapped to nearest color in (default)
1122 palette by simulating the PALETTERGB macro. This works whether
1123 or not the display device has a palette. */
1124 w32_color_ref = XUINT (tem) | 0x2000000;
1126 color_def->pixel = w32_color_ref;
1127 color_def->red = GetRValue (w32_color_ref) * 256;
1128 color_def->green = GetGValue (w32_color_ref) * 256;
1129 color_def->blue = GetBValue (w32_color_ref) * 256;
1131 return 1;
1133 else
1135 return 0;
1139 /* Given a string ARG naming a color, compute a pixel value from it
1140 suitable for screen F.
1141 If F is not a color screen, return DEF (default) regardless of what
1142 ARG says. */
1145 x_decode_color (FRAME_PTR f, Lisp_Object arg, int def)
1147 XColor cdef;
1149 CHECK_STRING (arg);
1151 if (strcmp (SDATA (arg), "black") == 0)
1152 return BLACK_PIX_DEFAULT (f);
1153 else if (strcmp (SDATA (arg), "white") == 0)
1154 return WHITE_PIX_DEFAULT (f);
1156 if ((FRAME_W32_DISPLAY_INFO (f)->n_planes * FRAME_W32_DISPLAY_INFO (f)->n_cbits) == 1)
1157 return def;
1159 /* w32_defined_color is responsible for coping with failures
1160 by looking for a near-miss. */
1161 if (w32_defined_color (f, SDATA (arg), &cdef, 1))
1162 return cdef.pixel;
1164 /* defined_color failed; return an ultimate default. */
1165 return def;
1170 /* Functions called only from `x_set_frame_param'
1171 to set individual parameters.
1173 If FRAME_W32_WINDOW (f) is 0,
1174 the frame is being created and its window does not exist yet.
1175 In that case, just record the parameter's new value
1176 in the standard place; do not attempt to change the window. */
1178 void
1179 x_set_foreground_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1181 struct w32_output *x = f->output_data.w32;
1182 PIX_TYPE fg, old_fg;
1184 fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1185 old_fg = FRAME_FOREGROUND_PIXEL (f);
1186 FRAME_FOREGROUND_PIXEL (f) = fg;
1188 if (FRAME_W32_WINDOW (f) != 0)
1190 if (x->cursor_pixel == old_fg)
1192 x->cursor_pixel = fg;
1193 x->cursor_gc->background = fg;
1196 update_face_from_frame_parameter (f, Qforeground_color, arg);
1197 if (FRAME_VISIBLE_P (f))
1198 redraw_frame (f);
1202 void
1203 x_set_background_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1205 FRAME_BACKGROUND_PIXEL (f)
1206 = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
1208 if (FRAME_W32_WINDOW (f) != 0)
1210 SetWindowLong (FRAME_W32_WINDOW (f), WND_BACKGROUND_INDEX,
1211 FRAME_BACKGROUND_PIXEL (f));
1213 update_face_from_frame_parameter (f, Qbackground_color, arg);
1215 if (FRAME_VISIBLE_P (f))
1216 redraw_frame (f);
1220 void
1221 x_set_mouse_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1223 Cursor cursor, nontext_cursor, mode_cursor, hand_cursor;
1224 int count;
1225 int mask_color;
1227 if (!EQ (Qnil, arg))
1228 f->output_data.w32->mouse_pixel
1229 = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1230 mask_color = FRAME_BACKGROUND_PIXEL (f);
1232 /* Don't let pointers be invisible. */
1233 if (mask_color == f->output_data.w32->mouse_pixel
1234 && mask_color == FRAME_BACKGROUND_PIXEL (f))
1235 f->output_data.w32->mouse_pixel = FRAME_FOREGROUND_PIXEL (f);
1237 #if 0 /* TODO : Mouse cursor customization. */
1238 block_input ();
1240 /* It's not okay to crash if the user selects a screwy cursor. */
1241 count = x_catch_errors (FRAME_W32_DISPLAY (f));
1243 if (!EQ (Qnil, Vx_pointer_shape))
1245 CHECK_NUMBER (Vx_pointer_shape);
1246 cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XINT (Vx_pointer_shape));
1248 else
1249 cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
1250 x_check_errors (FRAME_W32_DISPLAY (f), "bad text pointer cursor: %s");
1252 if (!EQ (Qnil, Vx_nontext_pointer_shape))
1254 CHECK_NUMBER (Vx_nontext_pointer_shape);
1255 nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1256 XINT (Vx_nontext_pointer_shape));
1258 else
1259 nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_left_ptr);
1260 x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
1262 if (!EQ (Qnil, Vx_hourglass_pointer_shape))
1264 CHECK_NUMBER (Vx_hourglass_pointer_shape);
1265 hourglass_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1266 XINT (Vx_hourglass_pointer_shape));
1268 else
1269 hourglass_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_watch);
1270 x_check_errors (FRAME_W32_DISPLAY (f), "bad busy pointer cursor: %s");
1272 x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
1273 if (!EQ (Qnil, Vx_mode_pointer_shape))
1275 CHECK_NUMBER (Vx_mode_pointer_shape);
1276 mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1277 XINT (Vx_mode_pointer_shape));
1279 else
1280 mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
1281 x_check_errors (FRAME_W32_DISPLAY (f), "bad modeline pointer cursor: %s");
1283 if (!EQ (Qnil, Vx_sensitive_text_pointer_shape))
1285 CHECK_NUMBER (Vx_sensitive_text_pointer_shape);
1286 hand_cursor
1287 = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1288 XINT (Vx_sensitive_text_pointer_shape));
1290 else
1291 hand_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_crosshair);
1293 if (!NILP (Vx_window_horizontal_drag_shape))
1295 CHECK_NUMBER (Vx_window_horizontal_drag_shape);
1296 horizontal_drag_cursor
1297 = XCreateFontCursor (FRAME_X_DISPLAY (f),
1298 XINT (Vx_window_horizontal_drag_shape));
1300 else
1301 horizontal_drag_cursor
1302 = XCreateFontCursor (FRAME_X_DISPLAY (f), XC_sb_h_double_arrow);
1304 /* Check and report errors with the above calls. */
1305 x_check_errors (FRAME_W32_DISPLAY (f), "can't set cursor shape: %s");
1306 x_uncatch_errors (FRAME_W32_DISPLAY (f), count);
1309 XColor fore_color, back_color;
1311 fore_color.pixel = f->output_data.w32->mouse_pixel;
1312 back_color.pixel = mask_color;
1313 XQueryColor (FRAME_W32_DISPLAY (f),
1314 DefaultColormap (FRAME_W32_DISPLAY (f),
1315 DefaultScreen (FRAME_W32_DISPLAY (f))),
1316 &fore_color);
1317 XQueryColor (FRAME_W32_DISPLAY (f),
1318 DefaultColormap (FRAME_W32_DISPLAY (f),
1319 DefaultScreen (FRAME_W32_DISPLAY (f))),
1320 &back_color);
1321 XRecolorCursor (FRAME_W32_DISPLAY (f), cursor,
1322 &fore_color, &back_color);
1323 XRecolorCursor (FRAME_W32_DISPLAY (f), nontext_cursor,
1324 &fore_color, &back_color);
1325 XRecolorCursor (FRAME_W32_DISPLAY (f), mode_cursor,
1326 &fore_color, &back_color);
1327 XRecolorCursor (FRAME_W32_DISPLAY (f), hand_cursor,
1328 &fore_color, &back_color);
1329 XRecolorCursor (FRAME_W32_DISPLAY (f), hourglass_cursor,
1330 &fore_color, &back_color);
1333 if (FRAME_W32_WINDOW (f) != 0)
1334 XDefineCursor (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), cursor);
1336 if (cursor != f->output_data.w32->text_cursor && f->output_data.w32->text_cursor != 0)
1337 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->text_cursor);
1338 f->output_data.w32->text_cursor = cursor;
1340 if (nontext_cursor != f->output_data.w32->nontext_cursor
1341 && f->output_data.w32->nontext_cursor != 0)
1342 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->nontext_cursor);
1343 f->output_data.w32->nontext_cursor = nontext_cursor;
1345 if (hourglass_cursor != f->output_data.w32->hourglass_cursor
1346 && f->output_data.w32->hourglass_cursor != 0)
1347 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->hourglass_cursor);
1348 f->output_data.w32->hourglass_cursor = hourglass_cursor;
1350 if (mode_cursor != f->output_data.w32->modeline_cursor
1351 && f->output_data.w32->modeline_cursor != 0)
1352 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->modeline_cursor);
1353 f->output_data.w32->modeline_cursor = mode_cursor;
1355 if (hand_cursor != f->output_data.w32->hand_cursor
1356 && f->output_data.w32->hand_cursor != 0)
1357 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->hand_cursor);
1358 f->output_data.w32->hand_cursor = hand_cursor;
1360 XFlush (FRAME_W32_DISPLAY (f));
1361 unblock_input ();
1363 update_face_from_frame_parameter (f, Qmouse_color, arg);
1364 #endif /* TODO */
1367 void
1368 x_set_cursor_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1370 unsigned long fore_pixel, pixel;
1372 if (!NILP (Vx_cursor_fore_pixel))
1373 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
1374 WHITE_PIX_DEFAULT (f));
1375 else
1376 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
1378 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1380 /* Make sure that the cursor color differs from the background color. */
1381 if (pixel == FRAME_BACKGROUND_PIXEL (f))
1383 pixel = f->output_data.w32->mouse_pixel;
1384 if (pixel == fore_pixel)
1385 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
1388 f->output_data.w32->cursor_foreground_pixel = fore_pixel;
1389 f->output_data.w32->cursor_pixel = pixel;
1391 if (FRAME_W32_WINDOW (f) != 0)
1393 block_input ();
1394 /* Update frame's cursor_gc. */
1395 f->output_data.w32->cursor_gc->foreground = fore_pixel;
1396 f->output_data.w32->cursor_gc->background = pixel;
1398 unblock_input ();
1400 if (FRAME_VISIBLE_P (f))
1402 x_update_cursor (f, 0);
1403 x_update_cursor (f, 1);
1407 update_face_from_frame_parameter (f, Qcursor_color, arg);
1410 /* Set the border-color of frame F to pixel value PIX.
1411 Note that this does not fully take effect if done before
1412 F has a window. */
1414 void
1415 x_set_border_pixel (struct frame *f, int pix)
1418 f->output_data.w32->border_pixel = pix;
1420 if (FRAME_W32_WINDOW (f) != 0 && f->border_width > 0)
1422 if (FRAME_VISIBLE_P (f))
1423 redraw_frame (f);
1427 /* Set the border-color of frame F to value described by ARG.
1428 ARG can be a string naming a color.
1429 The border-color is used for the border that is drawn by the server.
1430 Note that this does not fully take effect if done before
1431 F has a window; it must be redone when the window is created. */
1433 void
1434 x_set_border_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1436 int pix;
1438 CHECK_STRING (arg);
1439 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1440 x_set_border_pixel (f, pix);
1441 update_face_from_frame_parameter (f, Qborder_color, arg);
1445 void
1446 x_set_cursor_type (FRAME_PTR f, Lisp_Object arg, Lisp_Object oldval)
1448 set_frame_cursor_types (f, arg);
1450 /* Make sure the cursor gets redrawn. */
1451 cursor_type_changed = 1;
1454 void
1455 x_set_icon_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1457 int result;
1459 if (NILP (arg) && NILP (oldval))
1460 return;
1462 if (STRINGP (arg) && STRINGP (oldval)
1463 && EQ (Fstring_equal (oldval, arg), Qt))
1464 return;
1466 if (SYMBOLP (arg) && SYMBOLP (oldval) && EQ (arg, oldval))
1467 return;
1469 block_input ();
1471 result = x_bitmap_icon (f, arg);
1472 if (result)
1474 unblock_input ();
1475 error ("No icon window available");
1478 unblock_input ();
1481 void
1482 x_set_icon_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1484 if (STRINGP (arg))
1486 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1487 return;
1489 else if (!NILP (arg) || NILP (oldval))
1490 return;
1492 fset_icon_name (f, arg);
1494 #if 0
1495 if (f->output_data.w32->icon_bitmap != 0)
1496 return;
1498 block_input ();
1500 result = x_text_icon (f,
1501 SSDATA ((!NILP (f->icon_name)
1502 ? f->icon_name
1503 : !NILP (f->title)
1504 ? f->title
1505 : f->name)));
1507 if (result)
1509 unblock_input ();
1510 error ("No icon window available");
1513 /* If the window was unmapped (and its icon was mapped),
1514 the new icon is not mapped, so map the window in its stead. */
1515 if (FRAME_VISIBLE_P (f))
1517 #ifdef USE_X_TOOLKIT
1518 XtPopup (f->output_data.w32->widget, XtGrabNone);
1519 #endif
1520 XMapWindow (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f));
1523 XFlush (FRAME_W32_DISPLAY (f));
1524 unblock_input ();
1525 #endif
1529 void
1530 x_set_menu_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1532 int nlines;
1534 /* Right now, menu bars don't work properly in minibuf-only frames;
1535 most of the commands try to apply themselves to the minibuffer
1536 frame itself, and get an error because you can't switch buffers
1537 in or split the minibuffer window. */
1538 if (FRAME_MINIBUF_ONLY_P (f))
1539 return;
1541 if (INTEGERP (value))
1542 nlines = XINT (value);
1543 else
1544 nlines = 0;
1546 FRAME_MENU_BAR_LINES (f) = 0;
1547 if (nlines)
1548 FRAME_EXTERNAL_MENU_BAR (f) = 1;
1549 else
1551 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
1552 free_frame_menubar (f);
1553 FRAME_EXTERNAL_MENU_BAR (f) = 0;
1555 /* Adjust the frame size so that the client (text) dimensions
1556 remain the same. This depends on FRAME_EXTERNAL_MENU_BAR being
1557 set correctly. */
1558 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
1559 do_pending_window_change (0);
1561 adjust_glyphs (f);
1565 /* Set the number of lines used for the tool bar of frame F to VALUE.
1566 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
1567 is the old number of tool bar lines. This function changes the
1568 height of all windows on frame F to match the new tool bar height.
1569 The frame's height doesn't change. */
1571 void
1572 x_set_tool_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1574 int delta, nlines, root_height;
1575 Lisp_Object root_window;
1577 /* Treat tool bars like menu bars. */
1578 if (FRAME_MINIBUF_ONLY_P (f))
1579 return;
1581 /* Use VALUE only if an integer >= 0. */
1582 if (INTEGERP (value) && XINT (value) >= 0)
1583 nlines = XFASTINT (value);
1584 else
1585 nlines = 0;
1587 /* Make sure we redisplay all windows in this frame. */
1588 ++windows_or_buffers_changed;
1590 delta = nlines - FRAME_TOOL_BAR_LINES (f);
1592 /* Don't resize the tool-bar to more than we have room for. */
1593 root_window = FRAME_ROOT_WINDOW (f);
1594 root_height = WINDOW_TOTAL_LINES (XWINDOW (root_window));
1595 if (root_height - delta < 1)
1597 delta = root_height - 1;
1598 nlines = FRAME_TOOL_BAR_LINES (f) + delta;
1601 FRAME_TOOL_BAR_LINES (f) = nlines;
1602 resize_frame_windows (f, FRAME_LINES (f), 0);
1603 adjust_glyphs (f);
1605 /* We also have to make sure that the internal border at the top of
1606 the frame, below the menu bar or tool bar, is redrawn when the
1607 tool bar disappears. This is so because the internal border is
1608 below the tool bar if one is displayed, but is below the menu bar
1609 if there isn't a tool bar. The tool bar draws into the area
1610 below the menu bar. */
1611 if (FRAME_W32_WINDOW (f) && FRAME_TOOL_BAR_LINES (f) == 0)
1613 clear_frame (f);
1614 clear_current_matrices (f);
1617 /* If the tool bar gets smaller, the internal border below it
1618 has to be cleared. It was formerly part of the display
1619 of the larger tool bar, and updating windows won't clear it. */
1620 if (delta < 0)
1622 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
1623 int width = FRAME_PIXEL_WIDTH (f);
1624 int y = nlines * FRAME_LINE_HEIGHT (f);
1626 block_input ();
1628 HDC hdc = get_frame_dc (f);
1629 w32_clear_area (f, hdc, 0, y, width, height);
1630 release_frame_dc (f, hdc);
1632 unblock_input ();
1634 if (WINDOWP (f->tool_bar_window))
1635 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
1638 run_window_configuration_change_hook (f);
1643 /* Change the name of frame F to NAME. If NAME is nil, set F's name to
1644 w32_id_name.
1646 If EXPLICIT is non-zero, that indicates that lisp code is setting the
1647 name; if NAME is a string, set F's name to NAME and set
1648 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1650 If EXPLICIT is zero, that indicates that Emacs redisplay code is
1651 suggesting a new name, which lisp code should override; if
1652 F->explicit_name is set, ignore the new name; otherwise, set it. */
1654 void
1655 x_set_name (struct frame *f, Lisp_Object name, int explicit)
1657 /* Make sure that requests from lisp code override requests from
1658 Emacs redisplay code. */
1659 if (explicit)
1661 /* If we're switching from explicit to implicit, we had better
1662 update the mode lines and thereby update the title. */
1663 if (f->explicit_name && NILP (name))
1664 update_mode_lines = 1;
1666 f->explicit_name = ! NILP (name);
1668 else if (f->explicit_name)
1669 return;
1671 /* If NAME is nil, set the name to the w32_id_name. */
1672 if (NILP (name))
1674 /* Check for no change needed in this very common case
1675 before we do any consing. */
1676 if (!strcmp (FRAME_W32_DISPLAY_INFO (f)->w32_id_name,
1677 SDATA (f->name)))
1678 return;
1679 name = build_string (FRAME_W32_DISPLAY_INFO (f)->w32_id_name);
1681 else
1682 CHECK_STRING (name);
1684 /* Don't change the name if it's already NAME. */
1685 if (! NILP (Fstring_equal (name, f->name)))
1686 return;
1688 fset_name (f, name);
1690 /* For setting the frame title, the title parameter should override
1691 the name parameter. */
1692 if (! NILP (f->title))
1693 name = f->title;
1695 if (FRAME_W32_WINDOW (f))
1697 if (STRING_MULTIBYTE (name))
1698 name = ENCODE_SYSTEM (name);
1700 block_input ();
1701 SetWindowText (FRAME_W32_WINDOW (f), SDATA (name));
1702 unblock_input ();
1706 /* This function should be called when the user's lisp code has
1707 specified a name for the frame; the name will override any set by the
1708 redisplay code. */
1709 void
1710 x_explicitly_set_name (FRAME_PTR f, Lisp_Object arg, Lisp_Object oldval)
1712 x_set_name (f, arg, 1);
1715 /* This function should be called by Emacs redisplay code to set the
1716 name; names set this way will never override names set by the user's
1717 lisp code. */
1718 void
1719 x_implicitly_set_name (FRAME_PTR f, Lisp_Object arg, Lisp_Object oldval)
1721 x_set_name (f, arg, 0);
1724 /* Change the title of frame F to NAME.
1725 If NAME is nil, use the frame name as the title. */
1727 void
1728 x_set_title (struct frame *f, Lisp_Object name, Lisp_Object old_name)
1730 /* Don't change the title if it's already NAME. */
1731 if (EQ (name, f->title))
1732 return;
1734 update_mode_lines = 1;
1736 fset_title (f, name);
1738 if (NILP (name))
1739 name = f->name;
1741 if (FRAME_W32_WINDOW (f))
1743 if (STRING_MULTIBYTE (name))
1744 name = ENCODE_SYSTEM (name);
1746 block_input ();
1747 SetWindowText (FRAME_W32_WINDOW (f), SDATA (name));
1748 unblock_input ();
1752 void
1753 x_set_scroll_bar_default_width (struct frame *f)
1755 int wid = FRAME_COLUMN_WIDTH (f);
1757 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = GetSystemMetrics (SM_CXVSCROLL);
1758 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) +
1759 wid - 1) / wid;
1763 /* Subroutines for creating a frame. */
1765 Cursor
1766 w32_load_cursor (LPCTSTR name)
1768 /* Try first to load cursor from application resource. */
1769 Cursor cursor = LoadImage ((HINSTANCE) GetModuleHandle (NULL),
1770 name, IMAGE_CURSOR, 0, 0,
1771 LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_SHARED);
1772 if (!cursor)
1774 /* Then try to load a shared predefined cursor. */
1775 cursor = LoadImage (NULL, name, IMAGE_CURSOR, 0, 0,
1776 LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_SHARED);
1778 return cursor;
1781 static LRESULT CALLBACK w32_wnd_proc (HWND, UINT, WPARAM, LPARAM);
1783 #define INIT_WINDOW_CLASS(WC) \
1784 (WC).style = CS_HREDRAW | CS_VREDRAW; \
1785 (WC).lpfnWndProc = (WNDPROC) w32_wnd_proc; \
1786 (WC).cbClsExtra = 0; \
1787 (WC).cbWndExtra = WND_EXTRA_BYTES; \
1788 (WC).hInstance = hinst; \
1789 (WC).hIcon = LoadIcon (hinst, EMACS_CLASS); \
1790 (WC).hCursor = w32_load_cursor (IDC_ARROW); \
1791 (WC).hbrBackground = NULL; \
1792 (WC).lpszMenuName = NULL; \
1794 static BOOL
1795 w32_init_class (HINSTANCE hinst)
1798 if (w32_unicode_gui)
1800 WNDCLASSW uwc;
1801 INIT_WINDOW_CLASS(uwc);
1802 uwc.lpszClassName = L"Emacs";
1804 return RegisterClassW (&uwc);
1806 else
1808 WNDCLASS wc;
1809 INIT_WINDOW_CLASS(wc);
1810 wc.lpszClassName = EMACS_CLASS;
1812 return RegisterClassA (&wc);
1816 static HWND
1817 w32_createscrollbar (struct frame *f, struct scroll_bar * bar)
1819 return (CreateWindow ("SCROLLBAR", "", SBS_VERT | WS_CHILD | WS_VISIBLE,
1820 /* Position and size of scroll bar. */
1821 XINT (bar->left) + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
1822 XINT (bar->top),
1823 XINT (bar->width) - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
1824 XINT (bar->height),
1825 FRAME_W32_WINDOW (f),
1826 NULL,
1827 hinst,
1828 NULL));
1831 static void
1832 w32_createwindow (struct frame *f)
1834 HWND hwnd;
1835 RECT rect;
1836 Lisp_Object top = Qunbound;
1837 Lisp_Object left = Qunbound;
1838 struct w32_display_info *dpyinfo = &one_w32_display_info;
1840 rect.left = rect.top = 0;
1841 rect.right = FRAME_PIXEL_WIDTH (f);
1842 rect.bottom = FRAME_PIXEL_HEIGHT (f);
1844 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
1845 FRAME_EXTERNAL_MENU_BAR (f));
1847 /* Do first time app init */
1849 if (!hprevinst)
1851 w32_init_class (hinst);
1854 if (f->size_hint_flags & USPosition || f->size_hint_flags & PPosition)
1856 XSETINT (left, f->left_pos);
1857 XSETINT (top, f->top_pos);
1859 else if (EQ (left, Qunbound) && EQ (top, Qunbound))
1861 /* When called with RES_TYPE_NUMBER, w32_get_arg will return zero
1862 for anything that is not a number and is not Qunbound. */
1863 left = x_get_arg (dpyinfo, Qnil, Qleft, "left", "Left", RES_TYPE_NUMBER);
1864 top = x_get_arg (dpyinfo, Qnil, Qtop, "top", "Top", RES_TYPE_NUMBER);
1867 FRAME_W32_WINDOW (f) = hwnd
1868 = CreateWindow (EMACS_CLASS,
1869 f->namebuf,
1870 f->output_data.w32->dwStyle | WS_CLIPCHILDREN,
1871 EQ (left, Qunbound) ? CW_USEDEFAULT : XINT (left),
1872 EQ (top, Qunbound) ? CW_USEDEFAULT : XINT (top),
1873 rect.right - rect.left,
1874 rect.bottom - rect.top,
1875 NULL,
1876 NULL,
1877 hinst,
1878 NULL);
1880 if (hwnd)
1882 SetWindowLong (hwnd, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
1883 SetWindowLong (hwnd, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
1884 SetWindowLong (hwnd, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
1885 SetWindowLong (hwnd, WND_SCROLLBAR_INDEX, f->scroll_bar_actual_width);
1886 SetWindowLong (hwnd, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
1888 /* Enable drag-n-drop. */
1889 DragAcceptFiles (hwnd, TRUE);
1891 /* Do this to discard the default setting specified by our parent. */
1892 ShowWindow (hwnd, SW_HIDE);
1894 /* Update frame positions. */
1895 GetWindowRect (hwnd, &rect);
1896 f->left_pos = rect.left;
1897 f->top_pos = rect.top;
1901 static void
1902 my_post_msg (W32Msg * wmsg, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
1904 wmsg->msg.hwnd = hwnd;
1905 wmsg->msg.message = msg;
1906 wmsg->msg.wParam = wParam;
1907 wmsg->msg.lParam = lParam;
1908 wmsg->msg.time = GetMessageTime ();
1910 post_msg (wmsg);
1913 /* GetKeyState and MapVirtualKey on Windows 95 do not actually distinguish
1914 between left and right keys as advertised. We test for this
1915 support dynamically, and set a flag when the support is absent. If
1916 absent, we keep track of the left and right control and alt keys
1917 ourselves. This is particularly necessary on keyboards that rely
1918 upon the AltGr key, which is represented as having the left control
1919 and right alt keys pressed. For these keyboards, we need to know
1920 when the left alt key has been pressed in addition to the AltGr key
1921 so that we can properly support M-AltGr-key sequences (such as M-@
1922 on Swedish keyboards). */
1924 #define EMACS_LCONTROL 0
1925 #define EMACS_RCONTROL 1
1926 #define EMACS_LMENU 2
1927 #define EMACS_RMENU 3
1929 static int modifiers[4];
1930 static int modifiers_recorded;
1931 static int modifier_key_support_tested;
1933 static void
1934 test_modifier_support (unsigned int wparam)
1936 unsigned int l, r;
1938 if (wparam != VK_CONTROL && wparam != VK_MENU)
1939 return;
1940 if (wparam == VK_CONTROL)
1942 l = VK_LCONTROL;
1943 r = VK_RCONTROL;
1945 else
1947 l = VK_LMENU;
1948 r = VK_RMENU;
1950 if (!(GetKeyState (l) & 0x8000) && !(GetKeyState (r) & 0x8000))
1951 modifiers_recorded = 1;
1952 else
1953 modifiers_recorded = 0;
1954 modifier_key_support_tested = 1;
1957 static void
1958 record_keydown (unsigned int wparam, unsigned int lparam)
1960 int i;
1962 if (!modifier_key_support_tested)
1963 test_modifier_support (wparam);
1965 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
1966 return;
1968 if (wparam == VK_CONTROL)
1969 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
1970 else
1971 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
1973 modifiers[i] = 1;
1976 static void
1977 record_keyup (unsigned int wparam, unsigned int lparam)
1979 int i;
1981 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
1982 return;
1984 if (wparam == VK_CONTROL)
1985 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
1986 else
1987 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
1989 modifiers[i] = 0;
1992 /* Emacs can lose focus while a modifier key has been pressed. When
1993 it regains focus, be conservative and clear all modifiers since
1994 we cannot reconstruct the left and right modifier state. */
1995 static void
1996 reset_modifiers (void)
1998 SHORT ctrl, alt;
2000 if (GetFocus () == NULL)
2001 /* Emacs doesn't have keyboard focus. Do nothing. */
2002 return;
2004 ctrl = GetAsyncKeyState (VK_CONTROL);
2005 alt = GetAsyncKeyState (VK_MENU);
2007 if (!(ctrl & 0x08000))
2008 /* Clear any recorded control modifier state. */
2009 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
2011 if (!(alt & 0x08000))
2012 /* Clear any recorded alt modifier state. */
2013 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
2015 /* Update the state of all modifier keys, because modifiers used in
2016 hot-key combinations can get stuck on if Emacs loses focus as a
2017 result of a hot-key being pressed. */
2019 BYTE keystate[256];
2021 #define CURRENT_STATE(key) ((GetAsyncKeyState (key) & 0x8000) >> 8)
2023 GetKeyboardState (keystate);
2024 keystate[VK_SHIFT] = CURRENT_STATE (VK_SHIFT);
2025 keystate[VK_CONTROL] = CURRENT_STATE (VK_CONTROL);
2026 keystate[VK_LCONTROL] = CURRENT_STATE (VK_LCONTROL);
2027 keystate[VK_RCONTROL] = CURRENT_STATE (VK_RCONTROL);
2028 keystate[VK_MENU] = CURRENT_STATE (VK_MENU);
2029 keystate[VK_LMENU] = CURRENT_STATE (VK_LMENU);
2030 keystate[VK_RMENU] = CURRENT_STATE (VK_RMENU);
2031 keystate[VK_LWIN] = CURRENT_STATE (VK_LWIN);
2032 keystate[VK_RWIN] = CURRENT_STATE (VK_RWIN);
2033 keystate[VK_APPS] = CURRENT_STATE (VK_APPS);
2034 SetKeyboardState (keystate);
2038 /* Synchronize modifier state with what is reported with the current
2039 keystroke. Even if we cannot distinguish between left and right
2040 modifier keys, we know that, if no modifiers are set, then neither
2041 the left or right modifier should be set. */
2042 static void
2043 sync_modifiers (void)
2045 if (!modifiers_recorded)
2046 return;
2048 if (!(GetKeyState (VK_CONTROL) & 0x8000))
2049 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
2051 if (!(GetKeyState (VK_MENU) & 0x8000))
2052 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
2055 static int
2056 modifier_set (int vkey)
2058 if (vkey == VK_CAPITAL || vkey == VK_SCROLL)
2059 return (GetKeyState (vkey) & 0x1);
2060 if (!modifiers_recorded)
2061 return (GetKeyState (vkey) & 0x8000);
2063 switch (vkey)
2065 case VK_LCONTROL:
2066 return modifiers[EMACS_LCONTROL];
2067 case VK_RCONTROL:
2068 return modifiers[EMACS_RCONTROL];
2069 case VK_LMENU:
2070 return modifiers[EMACS_LMENU];
2071 case VK_RMENU:
2072 return modifiers[EMACS_RMENU];
2074 return (GetKeyState (vkey) & 0x8000);
2077 /* Convert between the modifier bits W32 uses and the modifier bits
2078 Emacs uses. */
2080 unsigned int
2081 w32_key_to_modifier (int key)
2083 Lisp_Object key_mapping;
2085 switch (key)
2087 case VK_LWIN:
2088 key_mapping = Vw32_lwindow_modifier;
2089 break;
2090 case VK_RWIN:
2091 key_mapping = Vw32_rwindow_modifier;
2092 break;
2093 case VK_APPS:
2094 key_mapping = Vw32_apps_modifier;
2095 break;
2096 case VK_SCROLL:
2097 key_mapping = Vw32_scroll_lock_modifier;
2098 break;
2099 default:
2100 key_mapping = Qnil;
2103 /* NB. This code runs in the input thread, asynchronously to the lisp
2104 thread, so we must be careful to ensure access to lisp data is
2105 thread-safe. The following code is safe because the modifier
2106 variable values are updated atomically from lisp and symbols are
2107 not relocated by GC. Also, we don't have to worry about seeing GC
2108 markbits here. */
2109 if (EQ (key_mapping, Qhyper))
2110 return hyper_modifier;
2111 if (EQ (key_mapping, Qsuper))
2112 return super_modifier;
2113 if (EQ (key_mapping, Qmeta))
2114 return meta_modifier;
2115 if (EQ (key_mapping, Qalt))
2116 return alt_modifier;
2117 if (EQ (key_mapping, Qctrl))
2118 return ctrl_modifier;
2119 if (EQ (key_mapping, Qcontrol)) /* synonym for ctrl */
2120 return ctrl_modifier;
2121 if (EQ (key_mapping, Qshift))
2122 return shift_modifier;
2124 /* Don't generate any modifier if not explicitly requested. */
2125 return 0;
2128 static unsigned int
2129 w32_get_modifiers (void)
2131 return ((modifier_set (VK_SHIFT) ? shift_modifier : 0) |
2132 (modifier_set (VK_CONTROL) ? ctrl_modifier : 0) |
2133 (modifier_set (VK_LWIN) ? w32_key_to_modifier (VK_LWIN) : 0) |
2134 (modifier_set (VK_RWIN) ? w32_key_to_modifier (VK_RWIN) : 0) |
2135 (modifier_set (VK_APPS) ? w32_key_to_modifier (VK_APPS) : 0) |
2136 (modifier_set (VK_SCROLL) ? w32_key_to_modifier (VK_SCROLL) : 0) |
2137 (modifier_set (VK_MENU) ?
2138 ((NILP (Vw32_alt_is_meta)) ? alt_modifier : meta_modifier) : 0));
2141 /* We map the VK_* modifiers into console modifier constants
2142 so that we can use the same routines to handle both console
2143 and window input. */
2145 static int
2146 construct_console_modifiers (void)
2148 int mods;
2150 mods = 0;
2151 mods |= (modifier_set (VK_SHIFT)) ? SHIFT_PRESSED : 0;
2152 mods |= (modifier_set (VK_CAPITAL)) ? CAPSLOCK_ON : 0;
2153 mods |= (modifier_set (VK_SCROLL)) ? SCROLLLOCK_ON : 0;
2154 mods |= (modifier_set (VK_NUMLOCK)) ? NUMLOCK_ON : 0;
2155 mods |= (modifier_set (VK_LCONTROL)) ? LEFT_CTRL_PRESSED : 0;
2156 mods |= (modifier_set (VK_RCONTROL)) ? RIGHT_CTRL_PRESSED : 0;
2157 mods |= (modifier_set (VK_LMENU)) ? LEFT_ALT_PRESSED : 0;
2158 mods |= (modifier_set (VK_RMENU)) ? RIGHT_ALT_PRESSED : 0;
2159 mods |= (modifier_set (VK_LWIN)) ? LEFT_WIN_PRESSED : 0;
2160 mods |= (modifier_set (VK_RWIN)) ? RIGHT_WIN_PRESSED : 0;
2161 mods |= (modifier_set (VK_APPS)) ? APPS_PRESSED : 0;
2163 return mods;
2166 static int
2167 w32_get_key_modifiers (unsigned int wparam, unsigned int lparam)
2169 int mods;
2171 /* Convert to emacs modifiers. */
2172 mods = w32_kbd_mods_to_emacs (construct_console_modifiers (), wparam);
2174 return mods;
2177 unsigned int
2178 map_keypad_keys (unsigned int virt_key, unsigned int extended)
2180 if (virt_key < VK_CLEAR || virt_key > VK_DELETE)
2181 return virt_key;
2183 if (virt_key == VK_RETURN)
2184 return (extended ? VK_NUMPAD_ENTER : VK_RETURN);
2186 if (virt_key >= VK_PRIOR && virt_key <= VK_DOWN)
2187 return (!extended ? (VK_NUMPAD_PRIOR + (virt_key - VK_PRIOR)) : virt_key);
2189 if (virt_key == VK_INSERT || virt_key == VK_DELETE)
2190 return (!extended ? (VK_NUMPAD_INSERT + (virt_key - VK_INSERT)) : virt_key);
2192 if (virt_key == VK_CLEAR)
2193 return (!extended ? VK_NUMPAD_CLEAR : virt_key);
2195 return virt_key;
2198 /* List of special key combinations which w32 would normally capture,
2199 but Emacs should grab instead. Not directly visible to lisp, to
2200 simplify synchronization. Each item is an integer encoding a virtual
2201 key code and modifier combination to capture. */
2202 static Lisp_Object w32_grabbed_keys;
2204 #define HOTKEY(vk, mods) make_number (((vk) & 255) | ((mods) << 8))
2205 #define HOTKEY_ID(k) (XFASTINT (k) & 0xbfff)
2206 #define HOTKEY_VK_CODE(k) (XFASTINT (k) & 255)
2207 #define HOTKEY_MODIFIERS(k) (XFASTINT (k) >> 8)
2209 #define RAW_HOTKEY_ID(k) ((k) & 0xbfff)
2210 #define RAW_HOTKEY_VK_CODE(k) ((k) & 255)
2211 #define RAW_HOTKEY_MODIFIERS(k) ((k) >> 8)
2213 /* Register hot-keys for reserved key combinations when Emacs has
2214 keyboard focus, since this is the only way Emacs can receive key
2215 combinations like Alt-Tab which are used by the system. */
2217 static void
2218 register_hot_keys (HWND hwnd)
2220 Lisp_Object keylist;
2222 /* Use CONSP, since we are called asynchronously. */
2223 for (keylist = w32_grabbed_keys; CONSP (keylist); keylist = XCDR (keylist))
2225 Lisp_Object key = XCAR (keylist);
2227 /* Deleted entries get set to nil. */
2228 if (!INTEGERP (key))
2229 continue;
2231 RegisterHotKey (hwnd, HOTKEY_ID (key),
2232 HOTKEY_MODIFIERS (key), HOTKEY_VK_CODE (key));
2236 static void
2237 unregister_hot_keys (HWND hwnd)
2239 Lisp_Object keylist;
2241 for (keylist = w32_grabbed_keys; CONSP (keylist); keylist = XCDR (keylist))
2243 Lisp_Object key = XCAR (keylist);
2245 if (!INTEGERP (key))
2246 continue;
2248 UnregisterHotKey (hwnd, HOTKEY_ID (key));
2252 /* Main message dispatch loop. */
2254 static void
2255 w32_msg_pump (deferred_msg * msg_buf)
2257 MSG msg;
2258 int result;
2259 HWND focus_window;
2261 msh_mousewheel = RegisterWindowMessage (MSH_MOUSEWHEEL);
2263 while ((w32_unicode_gui ? GetMessageW : GetMessageA) (&msg, NULL, 0, 0))
2265 if (msg.hwnd == NULL)
2267 switch (msg.message)
2269 case WM_NULL:
2270 /* Produced by complete_deferred_msg; just ignore. */
2271 break;
2272 case WM_EMACS_CREATEWINDOW:
2273 /* Initialize COM for this window. Even though we don't use it,
2274 some third party shell extensions can cause it to be used in
2275 system dialogs, which causes a crash if it is not initialized.
2276 This is a known bug in Windows, which was fixed long ago, but
2277 the patch for XP is not publicly available until XP SP3,
2278 and older versions will never be patched. */
2279 CoInitialize (NULL);
2280 w32_createwindow ((struct frame *) msg.wParam);
2281 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2282 emacs_abort ();
2283 break;
2284 case WM_EMACS_SETLOCALE:
2285 SetThreadLocale (msg.wParam);
2286 /* Reply is not expected. */
2287 break;
2288 case WM_EMACS_SETKEYBOARDLAYOUT:
2289 result = (int) ActivateKeyboardLayout ((HKL) msg.wParam, 0);
2290 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
2291 result, 0))
2292 emacs_abort ();
2293 break;
2294 case WM_EMACS_REGISTER_HOT_KEY:
2295 focus_window = GetFocus ();
2296 if (focus_window != NULL)
2297 RegisterHotKey (focus_window,
2298 RAW_HOTKEY_ID (msg.wParam),
2299 RAW_HOTKEY_MODIFIERS (msg.wParam),
2300 RAW_HOTKEY_VK_CODE (msg.wParam));
2301 /* Reply is not expected. */
2302 break;
2303 case WM_EMACS_UNREGISTER_HOT_KEY:
2304 focus_window = GetFocus ();
2305 if (focus_window != NULL)
2306 UnregisterHotKey (focus_window, RAW_HOTKEY_ID (msg.wParam));
2307 /* Mark item as erased. NB: this code must be
2308 thread-safe. The next line is okay because the cons
2309 cell is never made into garbage and is not relocated by
2310 GC. */
2311 XSETCAR ((Lisp_Object) ((EMACS_INT) msg.lParam), Qnil);
2312 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2313 emacs_abort ();
2314 break;
2315 case WM_EMACS_TOGGLE_LOCK_KEY:
2317 int vk_code = (int) msg.wParam;
2318 int cur_state = (GetKeyState (vk_code) & 1);
2319 Lisp_Object new_state = (Lisp_Object) ((EMACS_INT) msg.lParam);
2321 /* NB: This code must be thread-safe. It is safe to
2322 call NILP because symbols are not relocated by GC,
2323 and pointer here is not touched by GC (so the markbit
2324 can't be set). Numbers are safe because they are
2325 immediate values. */
2326 if (NILP (new_state)
2327 || (NUMBERP (new_state)
2328 && ((XUINT (new_state)) & 1) != cur_state))
2330 one_w32_display_info.faked_key = vk_code;
2332 keybd_event ((BYTE) vk_code,
2333 (BYTE) MapVirtualKey (vk_code, 0),
2334 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2335 keybd_event ((BYTE) vk_code,
2336 (BYTE) MapVirtualKey (vk_code, 0),
2337 KEYEVENTF_EXTENDEDKEY | 0, 0);
2338 keybd_event ((BYTE) vk_code,
2339 (BYTE) MapVirtualKey (vk_code, 0),
2340 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2341 cur_state = !cur_state;
2343 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
2344 cur_state, 0))
2345 emacs_abort ();
2347 break;
2348 #ifdef MSG_DEBUG
2349 /* Broadcast messages make it here, so you need to be looking
2350 for something in particular for this to be useful. */
2351 default:
2352 DebPrint (("msg %x not expected by w32_msg_pump\n", msg.message));
2353 #endif
2356 else
2358 if (w32_unicode_gui)
2359 DispatchMessageW (&msg);
2360 else
2361 DispatchMessageA (&msg);
2364 /* Exit nested loop when our deferred message has completed. */
2365 if (msg_buf->completed)
2366 break;
2370 deferred_msg * deferred_msg_head;
2372 static deferred_msg *
2373 find_deferred_msg (HWND hwnd, UINT msg)
2375 deferred_msg * item;
2377 /* Don't actually need synchronization for read access, since
2378 modification of single pointer is always atomic. */
2379 /* enter_crit (); */
2381 for (item = deferred_msg_head; item != NULL; item = item->next)
2382 if (item->w32msg.msg.hwnd == hwnd
2383 && item->w32msg.msg.message == msg)
2384 break;
2386 /* leave_crit (); */
2388 return item;
2391 static LRESULT
2392 send_deferred_msg (deferred_msg * msg_buf,
2393 HWND hwnd,
2394 UINT msg,
2395 WPARAM wParam,
2396 LPARAM lParam)
2398 /* Only input thread can send deferred messages. */
2399 if (GetCurrentThreadId () != dwWindowsThreadId)
2400 emacs_abort ();
2402 /* It is an error to send a message that is already deferred. */
2403 if (find_deferred_msg (hwnd, msg) != NULL)
2404 emacs_abort ();
2406 /* Enforced synchronization is not needed because this is the only
2407 function that alters deferred_msg_head, and the following critical
2408 section is guaranteed to only be serially reentered (since only the
2409 input thread can call us). */
2411 /* enter_crit (); */
2413 msg_buf->completed = 0;
2414 msg_buf->next = deferred_msg_head;
2415 deferred_msg_head = msg_buf;
2416 my_post_msg (&msg_buf->w32msg, hwnd, msg, wParam, lParam);
2418 /* leave_crit (); */
2420 /* Start a new nested message loop to process other messages until
2421 this one is completed. */
2422 w32_msg_pump (msg_buf);
2424 deferred_msg_head = msg_buf->next;
2426 return msg_buf->result;
2429 void
2430 complete_deferred_msg (HWND hwnd, UINT msg, LRESULT result)
2432 deferred_msg * msg_buf = find_deferred_msg (hwnd, msg);
2434 if (msg_buf == NULL)
2435 /* Message may have been canceled, so don't abort. */
2436 return;
2438 msg_buf->result = result;
2439 msg_buf->completed = 1;
2441 /* Ensure input thread is woken so it notices the completion. */
2442 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
2445 static void
2446 cancel_all_deferred_msgs (void)
2448 deferred_msg * item;
2450 /* Don't actually need synchronization for read access, since
2451 modification of single pointer is always atomic. */
2452 /* enter_crit (); */
2454 for (item = deferred_msg_head; item != NULL; item = item->next)
2456 item->result = 0;
2457 item->completed = 1;
2460 /* leave_crit (); */
2462 /* Ensure input thread is woken so it notices the completion. */
2463 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
2466 DWORD WINAPI
2467 w32_msg_worker (void *arg)
2469 MSG msg;
2470 deferred_msg dummy_buf;
2472 /* Ensure our message queue is created */
2474 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
2476 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2477 emacs_abort ();
2479 memset (&dummy_buf, 0, sizeof (dummy_buf));
2480 dummy_buf.w32msg.msg.hwnd = NULL;
2481 dummy_buf.w32msg.msg.message = WM_NULL;
2483 /* This is the initial message loop which should only exit when the
2484 application quits. */
2485 w32_msg_pump (&dummy_buf);
2487 return 0;
2490 static void
2491 signal_user_input (void)
2493 /* Interrupt any lisp that wants to be interrupted by input. */
2494 if (!NILP (Vthrow_on_input))
2496 Vquit_flag = Vthrow_on_input;
2497 /* Doing a QUIT from this thread is a bad idea, since this
2498 unwinds the stack of the Lisp thread, and the Windows runtime
2499 rightfully barfs. Disabled. */
2500 #if 0
2501 /* If we're inside a function that wants immediate quits,
2502 do it now. */
2503 if (immediate_quit && NILP (Vinhibit_quit))
2505 immediate_quit = 0;
2506 QUIT;
2508 #endif
2513 static void
2514 post_character_message (HWND hwnd, UINT msg,
2515 WPARAM wParam, LPARAM lParam,
2516 DWORD modifiers)
2518 W32Msg wmsg;
2520 wmsg.dwModifiers = modifiers;
2522 /* Detect quit_char and set quit-flag directly. Note that we
2523 still need to post a message to ensure the main thread will be
2524 woken up if blocked in sys_select, but we do NOT want to post
2525 the quit_char message itself (because it will usually be as if
2526 the user had typed quit_char twice). Instead, we post a dummy
2527 message that has no particular effect. */
2529 int c = wParam;
2530 if (isalpha (c) && wmsg.dwModifiers == ctrl_modifier)
2531 c = make_ctrl_char (c) & 0377;
2532 if (c == quit_char
2533 || (wmsg.dwModifiers == 0
2534 && w32_quit_key && wParam == w32_quit_key))
2536 Vquit_flag = Qt;
2538 /* The choice of message is somewhat arbitrary, as long as
2539 the main thread handler just ignores it. */
2540 msg = WM_NULL;
2542 /* Interrupt any blocking system calls. */
2543 signal_quit ();
2545 /* As a safety precaution, forcibly complete any deferred
2546 messages. This is a kludge, but I don't see any particularly
2547 clean way to handle the situation where a deferred message is
2548 "dropped" in the lisp thread, and will thus never be
2549 completed, eg. by the user trying to activate the menubar
2550 when the lisp thread is busy, and then typing C-g when the
2551 menubar doesn't open promptly (with the result that the
2552 menubar never responds at all because the deferred
2553 WM_INITMENU message is never completed). Another problem
2554 situation is when the lisp thread calls SendMessage (to send
2555 a window manager command) when a message has been deferred;
2556 the lisp thread gets blocked indefinitely waiting for the
2557 deferred message to be completed, which itself is waiting for
2558 the lisp thread to respond.
2560 Note that we don't want to block the input thread waiting for
2561 a response from the lisp thread (although that would at least
2562 solve the deadlock problem above), because we want to be able
2563 to receive C-g to interrupt the lisp thread. */
2564 cancel_all_deferred_msgs ();
2566 else
2567 signal_user_input ();
2570 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2573 /* Main window procedure */
2575 static LRESULT CALLBACK
2576 w32_wnd_proc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
2578 struct frame *f;
2579 struct w32_display_info *dpyinfo = &one_w32_display_info;
2580 W32Msg wmsg;
2581 int windows_translate;
2582 int key;
2584 /* Note that it is okay to call x_window_to_frame, even though we are
2585 not running in the main lisp thread, because frame deletion
2586 requires the lisp thread to synchronize with this thread. Thus, if
2587 a frame struct is returned, it can be used without concern that the
2588 lisp thread might make it disappear while we are using it.
2590 NB. Walking the frame list in this thread is safe (as long as
2591 writes of Lisp_Object slots are atomic, which they are on Windows).
2592 Although delete-frame can destructively modify the frame list while
2593 we are walking it, a garbage collection cannot occur until after
2594 delete-frame has synchronized with this thread.
2596 It is also safe to use functions that make GDI calls, such as
2597 w32_clear_rect, because these functions must obtain a DC handle
2598 from the frame struct using get_frame_dc which is thread-aware. */
2600 switch (msg)
2602 case WM_ERASEBKGND:
2603 f = x_window_to_frame (dpyinfo, hwnd);
2604 if (f)
2606 HDC hdc = get_frame_dc (f);
2607 GetUpdateRect (hwnd, &wmsg.rect, FALSE);
2608 w32_clear_rect (f, hdc, &wmsg.rect);
2609 release_frame_dc (f, hdc);
2611 #if defined (W32_DEBUG_DISPLAY)
2612 DebPrint (("WM_ERASEBKGND (frame %p): erasing %d,%d-%d,%d\n",
2614 wmsg.rect.left, wmsg.rect.top,
2615 wmsg.rect.right, wmsg.rect.bottom));
2616 #endif /* W32_DEBUG_DISPLAY */
2618 return 1;
2619 case WM_PALETTECHANGED:
2620 /* ignore our own changes */
2621 if ((HWND)wParam != hwnd)
2623 f = x_window_to_frame (dpyinfo, hwnd);
2624 if (f)
2625 /* get_frame_dc will realize our palette and force all
2626 frames to be redrawn if needed. */
2627 release_frame_dc (f, get_frame_dc (f));
2629 return 0;
2630 case WM_PAINT:
2632 PAINTSTRUCT paintStruct;
2633 RECT update_rect;
2634 memset (&update_rect, 0, sizeof (update_rect));
2636 f = x_window_to_frame (dpyinfo, hwnd);
2637 if (f == 0)
2639 DebPrint (("WM_PAINT received for unknown window %p\n", hwnd));
2640 return 0;
2643 /* MSDN Docs say not to call BeginPaint if GetUpdateRect
2644 fails. Apparently this can happen under some
2645 circumstances. */
2646 if (GetUpdateRect (hwnd, &update_rect, FALSE) || !w32_strict_painting)
2648 enter_crit ();
2649 BeginPaint (hwnd, &paintStruct);
2651 /* The rectangles returned by GetUpdateRect and BeginPaint
2652 do not always match. Play it safe by assuming both areas
2653 are invalid. */
2654 UnionRect (&(wmsg.rect), &update_rect, &(paintStruct.rcPaint));
2656 #if defined (W32_DEBUG_DISPLAY)
2657 DebPrint (("WM_PAINT (frame %p): painting %d,%d-%d,%d\n",
2659 wmsg.rect.left, wmsg.rect.top,
2660 wmsg.rect.right, wmsg.rect.bottom));
2661 DebPrint ((" [update region is %d,%d-%d,%d]\n",
2662 update_rect.left, update_rect.top,
2663 update_rect.right, update_rect.bottom));
2664 #endif
2665 EndPaint (hwnd, &paintStruct);
2666 leave_crit ();
2668 /* Change the message type to prevent Windows from
2669 combining WM_PAINT messages in the Lisp thread's queue,
2670 since Windows assumes that each message queue is
2671 dedicated to one frame and does not bother checking
2672 that hwnd matches before combining them. */
2673 my_post_msg (&wmsg, hwnd, WM_EMACS_PAINT, wParam, lParam);
2675 return 0;
2678 /* If GetUpdateRect returns 0 (meaning there is no update
2679 region), assume the whole window needs to be repainted. */
2680 GetClientRect (hwnd, &wmsg.rect);
2681 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2682 return 0;
2685 case WM_INPUTLANGCHANGE:
2686 /* Inform lisp thread of keyboard layout changes. */
2687 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2689 /* Clear dead keys in the keyboard state; for simplicity only
2690 preserve modifier key states. */
2692 int i;
2693 BYTE keystate[256];
2695 GetKeyboardState (keystate);
2696 for (i = 0; i < 256; i++)
2697 if (1
2698 && i != VK_SHIFT
2699 && i != VK_LSHIFT
2700 && i != VK_RSHIFT
2701 && i != VK_CAPITAL
2702 && i != VK_NUMLOCK
2703 && i != VK_SCROLL
2704 && i != VK_CONTROL
2705 && i != VK_LCONTROL
2706 && i != VK_RCONTROL
2707 && i != VK_MENU
2708 && i != VK_LMENU
2709 && i != VK_RMENU
2710 && i != VK_LWIN
2711 && i != VK_RWIN)
2712 keystate[i] = 0;
2713 SetKeyboardState (keystate);
2715 goto dflt;
2717 case WM_HOTKEY:
2718 /* Synchronize hot keys with normal input. */
2719 PostMessage (hwnd, WM_KEYDOWN, HIWORD (lParam), 0);
2720 return (0);
2722 case WM_KEYUP:
2723 case WM_SYSKEYUP:
2724 record_keyup (wParam, lParam);
2725 goto dflt;
2727 case WM_KEYDOWN:
2728 case WM_SYSKEYDOWN:
2729 /* Ignore keystrokes we fake ourself; see below. */
2730 if (dpyinfo->faked_key == wParam)
2732 dpyinfo->faked_key = 0;
2733 /* Make sure TranslateMessage sees them though (as long as
2734 they don't produce WM_CHAR messages). This ensures that
2735 indicator lights are toggled promptly on Windows 9x, for
2736 example. */
2737 if (wParam < 256 && lispy_function_keys[wParam])
2739 windows_translate = 1;
2740 goto translate;
2742 return 0;
2745 /* Synchronize modifiers with current keystroke. */
2746 sync_modifiers ();
2747 record_keydown (wParam, lParam);
2748 wParam = map_keypad_keys (wParam, (lParam & 0x1000000L) != 0);
2750 windows_translate = 0;
2752 switch (wParam)
2754 case VK_LWIN:
2755 if (NILP (Vw32_pass_lwindow_to_system))
2757 /* Prevent system from acting on keyup (which opens the
2758 Start menu if no other key was pressed) by simulating a
2759 press of Space which we will ignore. */
2760 if (GetAsyncKeyState (wParam) & 1)
2762 if (NUMBERP (Vw32_phantom_key_code))
2763 key = XUINT (Vw32_phantom_key_code) & 255;
2764 else
2765 key = VK_SPACE;
2766 dpyinfo->faked_key = key;
2767 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
2770 if (!NILP (Vw32_lwindow_modifier))
2771 return 0;
2772 break;
2773 case VK_RWIN:
2774 if (NILP (Vw32_pass_rwindow_to_system))
2776 if (GetAsyncKeyState (wParam) & 1)
2778 if (NUMBERP (Vw32_phantom_key_code))
2779 key = XUINT (Vw32_phantom_key_code) & 255;
2780 else
2781 key = VK_SPACE;
2782 dpyinfo->faked_key = key;
2783 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
2786 if (!NILP (Vw32_rwindow_modifier))
2787 return 0;
2788 break;
2789 case VK_APPS:
2790 if (!NILP (Vw32_apps_modifier))
2791 return 0;
2792 break;
2793 case VK_MENU:
2794 if (NILP (Vw32_pass_alt_to_system))
2795 /* Prevent DefWindowProc from activating the menu bar if an
2796 Alt key is pressed and released by itself. */
2797 return 0;
2798 windows_translate = 1;
2799 break;
2800 case VK_CAPITAL:
2801 /* Decide whether to treat as modifier or function key. */
2802 if (NILP (Vw32_enable_caps_lock))
2803 goto disable_lock_key;
2804 windows_translate = 1;
2805 break;
2806 case VK_NUMLOCK:
2807 /* Decide whether to treat as modifier or function key. */
2808 if (NILP (Vw32_enable_num_lock))
2809 goto disable_lock_key;
2810 windows_translate = 1;
2811 break;
2812 case VK_SCROLL:
2813 /* Decide whether to treat as modifier or function key. */
2814 if (NILP (Vw32_scroll_lock_modifier))
2815 goto disable_lock_key;
2816 windows_translate = 1;
2817 break;
2818 disable_lock_key:
2819 /* Ensure the appropriate lock key state (and indicator light)
2820 remains in the same state. We do this by faking another
2821 press of the relevant key. Apparently, this really is the
2822 only way to toggle the state of the indicator lights. */
2823 dpyinfo->faked_key = wParam;
2824 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
2825 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2826 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
2827 KEYEVENTF_EXTENDEDKEY | 0, 0);
2828 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
2829 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2830 /* Ensure indicator lights are updated promptly on Windows 9x
2831 (TranslateMessage apparently does this), after forwarding
2832 input event. */
2833 post_character_message (hwnd, msg, wParam, lParam,
2834 w32_get_key_modifiers (wParam, lParam));
2835 windows_translate = 1;
2836 break;
2837 case VK_CONTROL:
2838 case VK_SHIFT:
2839 case VK_PROCESSKEY: /* Generated by IME. */
2840 windows_translate = 1;
2841 break;
2842 case VK_CANCEL:
2843 /* Windows maps Ctrl-Pause (aka Ctrl-Break) into VK_CANCEL,
2844 which is confusing for purposes of key binding; convert
2845 VK_CANCEL events into VK_PAUSE events. */
2846 wParam = VK_PAUSE;
2847 break;
2848 case VK_PAUSE:
2849 /* Windows maps Ctrl-NumLock into VK_PAUSE, which is confusing
2850 for purposes of key binding; convert these back into
2851 VK_NUMLOCK events, at least when we want to see NumLock key
2852 presses. (Note that there is never any possibility that
2853 VK_PAUSE with Ctrl really is C-Pause as per above.) */
2854 if (NILP (Vw32_enable_num_lock) && modifier_set (VK_CONTROL))
2855 wParam = VK_NUMLOCK;
2856 break;
2857 default:
2858 /* If not defined as a function key, change it to a WM_CHAR message. */
2859 if (wParam > 255 || !lispy_function_keys[wParam])
2861 DWORD modifiers = construct_console_modifiers ();
2863 if (!NILP (Vw32_recognize_altgr)
2864 && modifier_set (VK_LCONTROL) && modifier_set (VK_RMENU))
2866 /* Always let TranslateMessage handle AltGr key chords;
2867 for some reason, ToAscii doesn't always process AltGr
2868 chords correctly. */
2869 windows_translate = 1;
2871 else if ((modifiers & (~SHIFT_PRESSED & ~CAPSLOCK_ON)) != 0)
2873 /* Handle key chords including any modifiers other
2874 than shift directly, in order to preserve as much
2875 modifier information as possible. */
2876 if ('A' <= wParam && wParam <= 'Z')
2878 /* Don't translate modified alphabetic keystrokes,
2879 so the user doesn't need to constantly switch
2880 layout to type control or meta keystrokes when
2881 the normal layout translates alphabetic
2882 characters to non-ascii characters. */
2883 if (!modifier_set (VK_SHIFT))
2884 wParam += ('a' - 'A');
2885 msg = WM_CHAR;
2887 else
2889 /* Try to handle other keystrokes by determining the
2890 base character (ie. translating the base key plus
2891 shift modifier). */
2892 int add;
2893 KEY_EVENT_RECORD key;
2895 key.bKeyDown = TRUE;
2896 key.wRepeatCount = 1;
2897 key.wVirtualKeyCode = wParam;
2898 key.wVirtualScanCode = (lParam & 0xFF0000) >> 16;
2899 key.uChar.AsciiChar = 0;
2900 key.dwControlKeyState = modifiers;
2902 add = w32_kbd_patch_key (&key, w32_keyboard_codepage);
2903 /* 0 means an unrecognized keycode, negative means
2904 dead key. Ignore both. */
2905 while (--add >= 0)
2907 /* Forward asciified character sequence. */
2908 post_character_message
2909 (hwnd, WM_CHAR,
2910 (unsigned char) key.uChar.AsciiChar, lParam,
2911 w32_get_key_modifiers (wParam, lParam));
2912 w32_kbd_patch_key (&key, w32_keyboard_codepage);
2914 return 0;
2917 else
2919 /* Let TranslateMessage handle everything else. */
2920 windows_translate = 1;
2925 translate:
2926 if (windows_translate)
2928 MSG windows_msg = { hwnd, msg, wParam, lParam, 0, {0,0} };
2929 windows_msg.time = GetMessageTime ();
2930 TranslateMessage (&windows_msg);
2931 goto dflt;
2934 /* Fall through */
2936 case WM_SYSCHAR:
2937 case WM_CHAR:
2938 if (wParam > 255 )
2940 W32Msg wmsg;
2942 wmsg.dwModifiers = w32_get_key_modifiers (wParam, lParam);
2943 signal_user_input ();
2944 my_post_msg (&wmsg, hwnd, WM_UNICHAR, wParam, lParam);
2947 else
2948 post_character_message (hwnd, msg, wParam, lParam,
2949 w32_get_key_modifiers (wParam, lParam));
2950 break;
2952 case WM_UNICHAR:
2953 /* WM_UNICHAR looks promising from the docs, but the exact
2954 circumstances in which TranslateMessage sends it is one of those
2955 Microsoft secret API things that EU and US courts are supposed
2956 to have put a stop to already. Spy++ shows it being sent to Notepad
2957 and other MS apps, but never to Emacs.
2959 Some third party IMEs send it in accordance with the official
2960 documentation though, so handle it here.
2962 UNICODE_NOCHAR is used to test for support for this message.
2963 TRUE indicates that the message is supported. */
2964 if (wParam == UNICODE_NOCHAR)
2965 return TRUE;
2968 W32Msg wmsg;
2969 wmsg.dwModifiers = w32_get_key_modifiers (wParam, lParam);
2970 signal_user_input ();
2971 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2973 break;
2975 case WM_IME_CHAR:
2976 /* If we can't get the IME result as Unicode, use default processing,
2977 which will at least allow characters decodable in the system locale
2978 get through. */
2979 if (!get_composition_string_fn)
2980 goto dflt;
2982 else if (!ignore_ime_char)
2984 wchar_t * buffer;
2985 int size, i;
2986 W32Msg wmsg;
2987 HIMC context = get_ime_context_fn (hwnd);
2988 wmsg.dwModifiers = w32_get_key_modifiers (wParam, lParam);
2989 /* Get buffer size. */
2990 size = get_composition_string_fn (context, GCS_RESULTSTR, NULL, 0);
2991 buffer = alloca (size);
2992 size = get_composition_string_fn (context, GCS_RESULTSTR,
2993 buffer, size);
2994 release_ime_context_fn (hwnd, context);
2996 signal_user_input ();
2997 for (i = 0; i < size / sizeof (wchar_t); i++)
2999 my_post_msg (&wmsg, hwnd, WM_UNICHAR, (WPARAM) buffer[i],
3000 lParam);
3002 /* Ignore the messages for the rest of the
3003 characters in the string that was output above. */
3004 ignore_ime_char = (size / sizeof (wchar_t)) - 1;
3006 else
3007 ignore_ime_char--;
3009 break;
3011 case WM_IME_STARTCOMPOSITION:
3012 if (!set_ime_composition_window_fn)
3013 goto dflt;
3014 else
3016 COMPOSITIONFORM form;
3017 HIMC context;
3018 struct window *w;
3020 f = x_window_to_frame (dpyinfo, hwnd);
3021 w = XWINDOW (FRAME_SELECTED_WINDOW (f));
3023 form.dwStyle = CFS_RECT;
3024 form.ptCurrentPos.x = w32_system_caret_x;
3025 form.ptCurrentPos.y = w32_system_caret_y;
3027 form.rcArea.left = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, 0);
3028 form.rcArea.top = (WINDOW_TOP_EDGE_Y (w)
3029 + WINDOW_HEADER_LINE_HEIGHT (w));
3030 form.rcArea.right = (WINDOW_BOX_RIGHT_EDGE_X (w)
3031 - WINDOW_RIGHT_MARGIN_WIDTH (w)
3032 - WINDOW_RIGHT_FRINGE_WIDTH (w));
3033 form.rcArea.bottom = (WINDOW_BOTTOM_EDGE_Y (w)
3034 - WINDOW_MODE_LINE_HEIGHT (w));
3036 context = get_ime_context_fn (hwnd);
3038 if (!context)
3039 break;
3041 set_ime_composition_window_fn (context, &form);
3042 release_ime_context_fn (hwnd, context);
3044 break;
3046 case WM_IME_ENDCOMPOSITION:
3047 ignore_ime_char = 0;
3048 goto dflt;
3050 /* Simulate middle mouse button events when left and right buttons
3051 are used together, but only if user has two button mouse. */
3052 case WM_LBUTTONDOWN:
3053 case WM_RBUTTONDOWN:
3054 if (w32_num_mouse_buttons > 2)
3055 goto handle_plain_button;
3058 int this = (msg == WM_LBUTTONDOWN) ? LMOUSE : RMOUSE;
3059 int other = (msg == WM_LBUTTONDOWN) ? RMOUSE : LMOUSE;
3061 if (button_state & this)
3062 return 0;
3064 if (button_state == 0)
3065 SetCapture (hwnd);
3067 button_state |= this;
3069 if (button_state & other)
3071 if (mouse_button_timer)
3073 KillTimer (hwnd, mouse_button_timer);
3074 mouse_button_timer = 0;
3076 /* Generate middle mouse event instead. */
3077 msg = WM_MBUTTONDOWN;
3078 button_state |= MMOUSE;
3080 else if (button_state & MMOUSE)
3082 /* Ignore button event if we've already generated a
3083 middle mouse down event. This happens if the
3084 user releases and press one of the two buttons
3085 after we've faked a middle mouse event. */
3086 return 0;
3088 else
3090 /* Flush out saved message. */
3091 post_msg (&saved_mouse_button_msg);
3093 wmsg.dwModifiers = w32_get_modifiers ();
3094 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3095 signal_user_input ();
3097 /* Clear message buffer. */
3098 saved_mouse_button_msg.msg.hwnd = 0;
3100 else
3102 /* Hold onto message for now. */
3103 mouse_button_timer =
3104 SetTimer (hwnd, MOUSE_BUTTON_ID,
3105 w32_mouse_button_tolerance, NULL);
3106 saved_mouse_button_msg.msg.hwnd = hwnd;
3107 saved_mouse_button_msg.msg.message = msg;
3108 saved_mouse_button_msg.msg.wParam = wParam;
3109 saved_mouse_button_msg.msg.lParam = lParam;
3110 saved_mouse_button_msg.msg.time = GetMessageTime ();
3111 saved_mouse_button_msg.dwModifiers = w32_get_modifiers ();
3114 return 0;
3116 case WM_LBUTTONUP:
3117 case WM_RBUTTONUP:
3118 if (w32_num_mouse_buttons > 2)
3119 goto handle_plain_button;
3122 int this = (msg == WM_LBUTTONUP) ? LMOUSE : RMOUSE;
3123 int other = (msg == WM_LBUTTONUP) ? RMOUSE : LMOUSE;
3125 if ((button_state & this) == 0)
3126 return 0;
3128 button_state &= ~this;
3130 if (button_state & MMOUSE)
3132 /* Only generate event when second button is released. */
3133 if ((button_state & other) == 0)
3135 msg = WM_MBUTTONUP;
3136 button_state &= ~MMOUSE;
3138 if (button_state) emacs_abort ();
3140 else
3141 return 0;
3143 else
3145 /* Flush out saved message if necessary. */
3146 if (saved_mouse_button_msg.msg.hwnd)
3148 post_msg (&saved_mouse_button_msg);
3151 wmsg.dwModifiers = w32_get_modifiers ();
3152 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3153 signal_user_input ();
3155 /* Always clear message buffer and cancel timer. */
3156 saved_mouse_button_msg.msg.hwnd = 0;
3157 KillTimer (hwnd, mouse_button_timer);
3158 mouse_button_timer = 0;
3160 if (button_state == 0)
3161 ReleaseCapture ();
3163 return 0;
3165 case WM_XBUTTONDOWN:
3166 case WM_XBUTTONUP:
3167 if (w32_pass_extra_mouse_buttons_to_system)
3168 goto dflt;
3169 /* else fall through and process them. */
3170 case WM_MBUTTONDOWN:
3171 case WM_MBUTTONUP:
3172 handle_plain_button:
3174 BOOL up;
3175 int button;
3177 /* Ignore middle and extra buttons as long as the menu is active. */
3178 f = x_window_to_frame (dpyinfo, hwnd);
3179 if (f && f->output_data.w32->menubar_active)
3180 return 0;
3182 if (parse_button (msg, HIWORD (wParam), &button, &up))
3184 if (up) ReleaseCapture ();
3185 else SetCapture (hwnd);
3186 button = (button == 0) ? LMOUSE :
3187 ((button == 1) ? MMOUSE : RMOUSE);
3188 if (up)
3189 button_state &= ~button;
3190 else
3191 button_state |= button;
3195 wmsg.dwModifiers = w32_get_modifiers ();
3196 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3197 signal_user_input ();
3199 /* Need to return true for XBUTTON messages, false for others,
3200 to indicate that we processed the message. */
3201 return (msg == WM_XBUTTONDOWN || msg == WM_XBUTTONUP);
3203 case WM_MOUSEMOVE:
3204 /* Ignore mouse movements as long as the menu is active. These
3205 movements are processed by the window manager anyway, and
3206 it's wrong to handle them as if they happened on the
3207 underlying frame. */
3208 f = x_window_to_frame (dpyinfo, hwnd);
3209 if (f && f->output_data.w32->menubar_active)
3210 return 0;
3212 /* If the mouse has just moved into the frame, start tracking
3213 it, so we will be notified when it leaves the frame. Mouse
3214 tracking only works under W98 and NT4 and later. On earlier
3215 versions, there is no way of telling when the mouse leaves the
3216 frame, so we just have to put up with help-echo and mouse
3217 highlighting remaining while the frame is not active. */
3218 if (track_mouse_event_fn && !track_mouse_window)
3220 TRACKMOUSEEVENT tme;
3221 tme.cbSize = sizeof (tme);
3222 tme.dwFlags = TME_LEAVE;
3223 tme.hwndTrack = hwnd;
3225 track_mouse_event_fn (&tme);
3226 track_mouse_window = hwnd;
3228 case WM_VSCROLL:
3229 if (w32_mouse_move_interval <= 0
3230 || (msg == WM_MOUSEMOVE && button_state == 0))
3232 wmsg.dwModifiers = w32_get_modifiers ();
3233 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3234 return 0;
3237 /* Hang onto mouse move and scroll messages for a bit, to avoid
3238 sending such events to Emacs faster than it can process them.
3239 If we get more events before the timer from the first message
3240 expires, we just replace the first message. */
3242 if (saved_mouse_move_msg.msg.hwnd == 0)
3243 mouse_move_timer =
3244 SetTimer (hwnd, MOUSE_MOVE_ID,
3245 w32_mouse_move_interval, NULL);
3247 /* Hold onto message for now. */
3248 saved_mouse_move_msg.msg.hwnd = hwnd;
3249 saved_mouse_move_msg.msg.message = msg;
3250 saved_mouse_move_msg.msg.wParam = wParam;
3251 saved_mouse_move_msg.msg.lParam = lParam;
3252 saved_mouse_move_msg.msg.time = GetMessageTime ();
3253 saved_mouse_move_msg.dwModifiers = w32_get_modifiers ();
3255 return 0;
3257 case WM_MOUSEWHEEL:
3258 case WM_DROPFILES:
3259 wmsg.dwModifiers = w32_get_modifiers ();
3260 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3261 signal_user_input ();
3262 return 0;
3264 case WM_APPCOMMAND:
3265 if (w32_pass_multimedia_buttons_to_system)
3266 goto dflt;
3267 /* Otherwise, pass to lisp, the same way we do with mousehwheel. */
3268 case WM_MOUSEHWHEEL:
3269 wmsg.dwModifiers = w32_get_modifiers ();
3270 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3271 signal_user_input ();
3272 /* Non-zero must be returned when WM_MOUSEHWHEEL messages are
3273 handled, to prevent the system trying to handle it by faking
3274 scroll bar events. */
3275 return 1;
3277 case WM_TIMER:
3278 /* Flush out saved messages if necessary. */
3279 if (wParam == mouse_button_timer)
3281 if (saved_mouse_button_msg.msg.hwnd)
3283 post_msg (&saved_mouse_button_msg);
3284 signal_user_input ();
3285 saved_mouse_button_msg.msg.hwnd = 0;
3287 KillTimer (hwnd, mouse_button_timer);
3288 mouse_button_timer = 0;
3290 else if (wParam == mouse_move_timer)
3292 if (saved_mouse_move_msg.msg.hwnd)
3294 post_msg (&saved_mouse_move_msg);
3295 saved_mouse_move_msg.msg.hwnd = 0;
3297 KillTimer (hwnd, mouse_move_timer);
3298 mouse_move_timer = 0;
3300 else if (wParam == menu_free_timer)
3302 KillTimer (hwnd, menu_free_timer);
3303 menu_free_timer = 0;
3304 f = x_window_to_frame (dpyinfo, hwnd);
3305 /* If a popup menu is active, don't wipe its strings. */
3306 if (menubar_in_use
3307 && current_popup_menu == NULL)
3309 /* Free memory used by owner-drawn and help-echo strings. */
3310 w32_free_menu_strings (hwnd);
3311 if (f)
3312 f->output_data.w32->menubar_active = 0;
3313 menubar_in_use = 0;
3316 else if (wParam == hourglass_timer)
3318 KillTimer (hwnd, hourglass_timer);
3319 hourglass_timer = 0;
3320 w32_show_hourglass (x_window_to_frame (dpyinfo, hwnd));
3322 return 0;
3324 case WM_NCACTIVATE:
3325 /* Windows doesn't send us focus messages when putting up and
3326 taking down a system popup dialog as for Ctrl-Alt-Del on Windows 95.
3327 The only indication we get that something happened is receiving
3328 this message afterwards. So this is a good time to reset our
3329 keyboard modifiers' state. */
3330 reset_modifiers ();
3331 goto dflt;
3333 case WM_INITMENU:
3334 button_state = 0;
3335 ReleaseCapture ();
3336 /* We must ensure menu bar is fully constructed and up to date
3337 before allowing user interaction with it. To achieve this
3338 we send this message to the lisp thread and wait for a
3339 reply (whose value is not actually needed) to indicate that
3340 the menu bar is now ready for use, so we can now return.
3342 To remain responsive in the meantime, we enter a nested message
3343 loop that can process all other messages.
3345 However, we skip all this if the message results from calling
3346 TrackPopupMenu - in fact, we must NOT attempt to send the lisp
3347 thread a message because it is blocked on us at this point. We
3348 set menubar_active before calling TrackPopupMenu to indicate
3349 this (there is no possibility of confusion with real menubar
3350 being active). */
3352 f = x_window_to_frame (dpyinfo, hwnd);
3353 if (f
3354 && (f->output_data.w32->menubar_active
3355 /* We can receive this message even in the absence of a
3356 menubar (ie. when the system menu is activated) - in this
3357 case we do NOT want to forward the message, otherwise it
3358 will cause the menubar to suddenly appear when the user
3359 had requested it to be turned off! */
3360 || f->output_data.w32->menubar_widget == NULL))
3361 return 0;
3364 deferred_msg msg_buf;
3366 /* Detect if message has already been deferred; in this case
3367 we cannot return any sensible value to ignore this. */
3368 if (find_deferred_msg (hwnd, msg) != NULL)
3369 emacs_abort ();
3371 menubar_in_use = 1;
3373 return send_deferred_msg (&msg_buf, hwnd, msg, wParam, lParam);
3376 case WM_EXITMENULOOP:
3377 f = x_window_to_frame (dpyinfo, hwnd);
3379 /* If a menu is still active, check again after a short delay,
3380 since Windows often (always?) sends the WM_EXITMENULOOP
3381 before the corresponding WM_COMMAND message.
3382 Don't do this if a popup menu is active, since it is only
3383 menubar menus that require cleaning up in this way.
3385 if (f && menubar_in_use && current_popup_menu == NULL)
3386 menu_free_timer = SetTimer (hwnd, MENU_FREE_ID, MENU_FREE_DELAY, NULL);
3388 /* If hourglass cursor should be displayed, display it now. */
3389 if (f && f->output_data.w32->hourglass_p)
3390 SetCursor (f->output_data.w32->hourglass_cursor);
3392 goto dflt;
3394 case WM_MENUSELECT:
3395 /* Direct handling of help_echo in menus. Should be safe now
3396 that we generate the help_echo by placing a help event in the
3397 keyboard buffer. */
3399 HMENU menu = (HMENU) lParam;
3400 UINT menu_item = (UINT) LOWORD (wParam);
3401 UINT flags = (UINT) HIWORD (wParam);
3403 w32_menu_display_help (hwnd, menu, menu_item, flags);
3405 return 0;
3407 case WM_MEASUREITEM:
3408 f = x_window_to_frame (dpyinfo, hwnd);
3409 if (f)
3411 MEASUREITEMSTRUCT * pMis = (MEASUREITEMSTRUCT *) lParam;
3413 if (pMis->CtlType == ODT_MENU)
3415 /* Work out dimensions for popup menu titles. */
3416 char * title = (char *) pMis->itemData;
3417 HDC hdc = GetDC (hwnd);
3418 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
3419 LOGFONT menu_logfont;
3420 HFONT old_font;
3421 SIZE size;
3423 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
3424 menu_logfont.lfWeight = FW_BOLD;
3425 menu_font = CreateFontIndirect (&menu_logfont);
3426 old_font = SelectObject (hdc, menu_font);
3428 pMis->itemHeight = GetSystemMetrics (SM_CYMENUSIZE);
3429 if (title)
3431 if (unicode_append_menu)
3432 GetTextExtentPoint32W (hdc, (WCHAR *) title,
3433 wcslen ((WCHAR *) title),
3434 &size);
3435 else
3436 GetTextExtentPoint32 (hdc, title, strlen (title), &size);
3438 pMis->itemWidth = size.cx;
3439 if (pMis->itemHeight < size.cy)
3440 pMis->itemHeight = size.cy;
3442 else
3443 pMis->itemWidth = 0;
3445 SelectObject (hdc, old_font);
3446 DeleteObject (menu_font);
3447 ReleaseDC (hwnd, hdc);
3448 return TRUE;
3451 return 0;
3453 case WM_DRAWITEM:
3454 f = x_window_to_frame (dpyinfo, hwnd);
3455 if (f)
3457 DRAWITEMSTRUCT * pDis = (DRAWITEMSTRUCT *) lParam;
3459 if (pDis->CtlType == ODT_MENU)
3461 /* Draw popup menu title. */
3462 char * title = (char *) pDis->itemData;
3463 if (title)
3465 HDC hdc = pDis->hDC;
3466 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
3467 LOGFONT menu_logfont;
3468 HFONT old_font;
3470 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
3471 menu_logfont.lfWeight = FW_BOLD;
3472 menu_font = CreateFontIndirect (&menu_logfont);
3473 old_font = SelectObject (hdc, menu_font);
3475 /* Always draw title as if not selected. */
3476 if (unicode_append_menu)
3477 ExtTextOutW (hdc,
3478 pDis->rcItem.left
3479 + GetSystemMetrics (SM_CXMENUCHECK),
3480 pDis->rcItem.top,
3481 ETO_OPAQUE, &pDis->rcItem,
3482 (WCHAR *) title,
3483 wcslen ((WCHAR *) title), NULL);
3484 else
3485 ExtTextOut (hdc,
3486 pDis->rcItem.left
3487 + GetSystemMetrics (SM_CXMENUCHECK),
3488 pDis->rcItem.top,
3489 ETO_OPAQUE, &pDis->rcItem,
3490 title, strlen (title), NULL);
3492 SelectObject (hdc, old_font);
3493 DeleteObject (menu_font);
3495 return TRUE;
3498 return 0;
3500 #if 0
3501 /* Still not right - can't distinguish between clicks in the
3502 client area of the frame from clicks forwarded from the scroll
3503 bars - may have to hook WM_NCHITTEST to remember the mouse
3504 position and then check if it is in the client area ourselves. */
3505 case WM_MOUSEACTIVATE:
3506 /* Discard the mouse click that activates a frame, allowing the
3507 user to click anywhere without changing point (or worse!).
3508 Don't eat mouse clicks on scrollbars though!! */
3509 if (LOWORD (lParam) == HTCLIENT )
3510 return MA_ACTIVATEANDEAT;
3511 goto dflt;
3512 #endif
3514 case WM_MOUSELEAVE:
3515 /* No longer tracking mouse. */
3516 track_mouse_window = NULL;
3518 case WM_ACTIVATEAPP:
3519 case WM_ACTIVATE:
3520 case WM_WINDOWPOSCHANGED:
3521 case WM_SHOWWINDOW:
3522 /* Inform lisp thread that a frame might have just been obscured
3523 or exposed, so should recheck visibility of all frames. */
3524 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3525 goto dflt;
3527 case WM_SETFOCUS:
3528 dpyinfo->faked_key = 0;
3529 reset_modifiers ();
3530 register_hot_keys (hwnd);
3531 goto command;
3532 case WM_KILLFOCUS:
3533 unregister_hot_keys (hwnd);
3534 button_state = 0;
3535 ReleaseCapture ();
3536 /* Relinquish the system caret. */
3537 if (w32_system_caret_hwnd)
3539 w32_visible_system_caret_hwnd = NULL;
3540 w32_system_caret_hwnd = NULL;
3541 DestroyCaret ();
3543 goto command;
3544 case WM_COMMAND:
3545 menubar_in_use = 0;
3546 f = x_window_to_frame (dpyinfo, hwnd);
3547 if (f && HIWORD (wParam) == 0)
3549 if (menu_free_timer)
3551 KillTimer (hwnd, menu_free_timer);
3552 menu_free_timer = 0;
3555 case WM_MOVE:
3556 case WM_SIZE:
3557 command:
3558 wmsg.dwModifiers = w32_get_modifiers ();
3559 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3560 goto dflt;
3562 case WM_DESTROY:
3563 CoUninitialize ();
3564 return 0;
3566 case WM_CLOSE:
3567 wmsg.dwModifiers = w32_get_modifiers ();
3568 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3569 return 0;
3571 case WM_WINDOWPOSCHANGING:
3572 /* Don't restrict the sizing of tip frames. */
3573 if (hwnd == tip_window)
3574 return 0;
3576 WINDOWPLACEMENT wp;
3577 LPWINDOWPOS lppos = (WINDOWPOS *) lParam;
3579 wp.length = sizeof (WINDOWPLACEMENT);
3580 GetWindowPlacement (hwnd, &wp);
3582 if (wp.showCmd != SW_SHOWMINIMIZED && (lppos->flags & SWP_NOSIZE) == 0)
3584 RECT rect;
3585 int wdiff;
3586 int hdiff;
3587 DWORD font_width;
3588 DWORD line_height;
3589 DWORD internal_border;
3590 DWORD scrollbar_extra;
3591 RECT wr;
3593 wp.length = sizeof (wp);
3594 GetWindowRect (hwnd, &wr);
3596 enter_crit ();
3598 font_width = GetWindowLong (hwnd, WND_FONTWIDTH_INDEX);
3599 line_height = GetWindowLong (hwnd, WND_LINEHEIGHT_INDEX);
3600 internal_border = GetWindowLong (hwnd, WND_BORDER_INDEX);
3601 scrollbar_extra = GetWindowLong (hwnd, WND_SCROLLBAR_INDEX);
3603 leave_crit ();
3605 memset (&rect, 0, sizeof (rect));
3606 AdjustWindowRect (&rect, GetWindowLong (hwnd, GWL_STYLE),
3607 GetMenu (hwnd) != NULL);
3609 /* Force width and height of client area to be exact
3610 multiples of the character cell dimensions. */
3611 wdiff = (lppos->cx - (rect.right - rect.left)
3612 - 2 * internal_border - scrollbar_extra)
3613 % font_width;
3614 hdiff = (lppos->cy - (rect.bottom - rect.top)
3615 - 2 * internal_border)
3616 % line_height;
3618 if (wdiff || hdiff)
3620 /* For right/bottom sizing we can just fix the sizes.
3621 However for top/left sizing we will need to fix the X
3622 and Y positions as well. */
3624 int cx_mintrack = GetSystemMetrics (SM_CXMINTRACK);
3625 int cy_mintrack = GetSystemMetrics (SM_CYMINTRACK);
3627 lppos->cx = max (lppos->cx - wdiff, cx_mintrack);
3628 lppos->cy = max (lppos->cy - hdiff, cy_mintrack);
3630 if (wp.showCmd != SW_SHOWMAXIMIZED
3631 && (lppos->flags & SWP_NOMOVE) == 0)
3633 if (lppos->x != wr.left || lppos->y != wr.top)
3635 lppos->x += wdiff;
3636 lppos->y += hdiff;
3638 else
3640 lppos->flags |= SWP_NOMOVE;
3644 return 0;
3649 goto dflt;
3651 case WM_GETMINMAXINFO:
3652 /* Hack to allow resizing the Emacs frame above the screen size.
3653 Note that Windows 9x limits coordinates to 16-bits. */
3654 ((LPMINMAXINFO) lParam)->ptMaxTrackSize.x = 32767;
3655 ((LPMINMAXINFO) lParam)->ptMaxTrackSize.y = 32767;
3656 return 0;
3658 case WM_SETCURSOR:
3659 if (LOWORD (lParam) == HTCLIENT)
3661 f = x_window_to_frame (dpyinfo, hwnd);
3662 if (f && f->output_data.w32->hourglass_p
3663 && !menubar_in_use && !current_popup_menu)
3664 SetCursor (f->output_data.w32->hourglass_cursor);
3665 else if (f)
3666 SetCursor (f->output_data.w32->current_cursor);
3667 return 0;
3669 goto dflt;
3671 case WM_EMACS_SETCURSOR:
3673 Cursor cursor = (Cursor) wParam;
3674 f = x_window_to_frame (dpyinfo, hwnd);
3675 if (f && cursor)
3677 f->output_data.w32->current_cursor = cursor;
3678 if (!f->output_data.w32->hourglass_p)
3679 SetCursor (cursor);
3681 return 0;
3684 case WM_EMACS_CREATESCROLLBAR:
3685 return (LRESULT) w32_createscrollbar ((struct frame *) wParam,
3686 (struct scroll_bar *) lParam);
3688 case WM_EMACS_SHOWWINDOW:
3689 return ShowWindow ((HWND) wParam, (WPARAM) lParam);
3691 case WM_EMACS_BRINGTOTOP:
3692 case WM_EMACS_SETFOREGROUND:
3694 HWND foreground_window;
3695 DWORD foreground_thread, retval;
3697 /* On NT 5.0, and apparently Windows 98, it is necessary to
3698 attach to the thread that currently has focus in order to
3699 pull the focus away from it. */
3700 foreground_window = GetForegroundWindow ();
3701 foreground_thread = GetWindowThreadProcessId (foreground_window, NULL);
3702 if (!foreground_window
3703 || foreground_thread == GetCurrentThreadId ()
3704 || !AttachThreadInput (GetCurrentThreadId (),
3705 foreground_thread, TRUE))
3706 foreground_thread = 0;
3708 retval = SetForegroundWindow ((HWND) wParam);
3709 if (msg == WM_EMACS_BRINGTOTOP)
3710 retval = BringWindowToTop ((HWND) wParam);
3712 /* Detach from the previous foreground thread. */
3713 if (foreground_thread)
3714 AttachThreadInput (GetCurrentThreadId (),
3715 foreground_thread, FALSE);
3717 return retval;
3720 case WM_EMACS_SETWINDOWPOS:
3722 WINDOWPOS * pos = (WINDOWPOS *) wParam;
3723 return SetWindowPos (hwnd, pos->hwndInsertAfter,
3724 pos->x, pos->y, pos->cx, pos->cy, pos->flags);
3727 case WM_EMACS_DESTROYWINDOW:
3728 DragAcceptFiles ((HWND) wParam, FALSE);
3729 return DestroyWindow ((HWND) wParam);
3731 case WM_EMACS_HIDE_CARET:
3732 return HideCaret (hwnd);
3734 case WM_EMACS_SHOW_CARET:
3735 return ShowCaret (hwnd);
3737 case WM_EMACS_DESTROY_CARET:
3738 w32_system_caret_hwnd = NULL;
3739 w32_visible_system_caret_hwnd = NULL;
3740 return DestroyCaret ();
3742 case WM_EMACS_TRACK_CARET:
3743 /* If there is currently no system caret, create one. */
3744 if (w32_system_caret_hwnd == NULL)
3746 /* Use the default caret width, and avoid changing it
3747 unnecessarily, as it confuses screen reader software. */
3748 w32_system_caret_hwnd = hwnd;
3749 CreateCaret (hwnd, NULL, 0,
3750 w32_system_caret_height);
3753 if (!SetCaretPos (w32_system_caret_x, w32_system_caret_y))
3754 return 0;
3755 /* Ensure visible caret gets turned on when requested. */
3756 else if (w32_use_visible_system_caret
3757 && w32_visible_system_caret_hwnd != hwnd)
3759 w32_visible_system_caret_hwnd = hwnd;
3760 return ShowCaret (hwnd);
3762 /* Ensure visible caret gets turned off when requested. */
3763 else if (!w32_use_visible_system_caret
3764 && w32_visible_system_caret_hwnd)
3766 w32_visible_system_caret_hwnd = NULL;
3767 return HideCaret (hwnd);
3769 else
3770 return 1;
3772 case WM_EMACS_TRACKPOPUPMENU:
3774 UINT flags;
3775 POINT *pos;
3776 int retval;
3777 pos = (POINT *)lParam;
3778 flags = TPM_CENTERALIGN;
3779 if (button_state & LMOUSE)
3780 flags |= TPM_LEFTBUTTON;
3781 else if (button_state & RMOUSE)
3782 flags |= TPM_RIGHTBUTTON;
3784 /* Remember we did a SetCapture on the initial mouse down event,
3785 so for safety, we make sure the capture is canceled now. */
3786 ReleaseCapture ();
3787 button_state = 0;
3789 /* Use menubar_active to indicate that WM_INITMENU is from
3790 TrackPopupMenu below, and should be ignored. */
3791 f = x_window_to_frame (dpyinfo, hwnd);
3792 if (f)
3793 f->output_data.w32->menubar_active = 1;
3795 if (TrackPopupMenu ((HMENU)wParam, flags, pos->x, pos->y,
3796 0, hwnd, NULL))
3798 MSG amsg;
3799 /* Eat any mouse messages during popupmenu */
3800 while (PeekMessage (&amsg, hwnd, WM_MOUSEFIRST, WM_MOUSELAST,
3801 PM_REMOVE));
3802 /* Get the menu selection, if any */
3803 if (PeekMessage (&amsg, hwnd, WM_COMMAND, WM_COMMAND, PM_REMOVE))
3805 retval = LOWORD (amsg.wParam);
3807 else
3809 retval = 0;
3812 else
3814 retval = -1;
3817 return retval;
3820 default:
3821 /* Check for messages registered at runtime. */
3822 if (msg == msh_mousewheel)
3824 wmsg.dwModifiers = w32_get_modifiers ();
3825 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3826 signal_user_input ();
3827 return 0;
3830 dflt:
3831 return (w32_unicode_gui ? DefWindowProcW : DefWindowProcA) (hwnd, msg, wParam, lParam);
3834 /* The most common default return code for handled messages is 0. */
3835 return 0;
3838 static void
3839 my_create_window (struct frame * f)
3841 MSG msg;
3843 if (!PostThreadMessage (dwWindowsThreadId, WM_EMACS_CREATEWINDOW, (WPARAM)f, 0))
3844 emacs_abort ();
3845 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
3849 /* Create a tooltip window. Unlike my_create_window, we do not do this
3850 indirectly via the Window thread, as we do not need to process Window
3851 messages for the tooltip. Creating tooltips indirectly also creates
3852 deadlocks when tooltips are created for menu items. */
3853 static void
3854 my_create_tip_window (struct frame *f)
3856 RECT rect;
3858 rect.left = rect.top = 0;
3859 rect.right = FRAME_PIXEL_WIDTH (f);
3860 rect.bottom = FRAME_PIXEL_HEIGHT (f);
3862 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
3863 FRAME_EXTERNAL_MENU_BAR (f));
3865 tip_window = FRAME_W32_WINDOW (f)
3866 = CreateWindow (EMACS_CLASS,
3867 f->namebuf,
3868 f->output_data.w32->dwStyle,
3869 f->left_pos,
3870 f->top_pos,
3871 rect.right - rect.left,
3872 rect.bottom - rect.top,
3873 FRAME_W32_WINDOW (SELECTED_FRAME ()), /* owner */
3874 NULL,
3875 hinst,
3876 NULL);
3878 if (tip_window)
3880 SetWindowLong (tip_window, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
3881 SetWindowLong (tip_window, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
3882 SetWindowLong (tip_window, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
3883 SetWindowLong (tip_window, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
3885 /* Tip frames have no scrollbars. */
3886 SetWindowLong (tip_window, WND_SCROLLBAR_INDEX, 0);
3888 /* Do this to discard the default setting specified by our parent. */
3889 ShowWindow (tip_window, SW_HIDE);
3894 /* Create and set up the w32 window for frame F. */
3896 static void
3897 w32_window (struct frame *f, long window_prompting, int minibuffer_only)
3899 block_input ();
3901 /* Use the resource name as the top-level window name
3902 for looking up resources. Make a non-Lisp copy
3903 for the window manager, so GC relocation won't bother it.
3905 Elsewhere we specify the window name for the window manager. */
3908 char *str = SSDATA (Vx_resource_name);
3909 f->namebuf = xmalloc (strlen (str) + 1);
3910 strcpy (f->namebuf, str);
3913 my_create_window (f);
3915 validate_x_resource_name ();
3917 /* x_set_name normally ignores requests to set the name if the
3918 requested name is the same as the current name. This is the one
3919 place where that assumption isn't correct; f->name is set, but
3920 the server hasn't been told. */
3922 Lisp_Object name;
3923 int explicit = f->explicit_name;
3925 f->explicit_name = 0;
3926 name = f->name;
3927 fset_name (f, Qnil);
3928 x_set_name (f, name, explicit);
3931 unblock_input ();
3933 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
3934 initialize_frame_menubar (f);
3936 if (FRAME_W32_WINDOW (f) == 0)
3937 error ("Unable to create window");
3940 /* Handle the icon stuff for this window. Perhaps later we might
3941 want an x_set_icon_position which can be called interactively as
3942 well. */
3944 static void
3945 x_icon (struct frame *f, Lisp_Object parms)
3947 Lisp_Object icon_x, icon_y;
3948 struct w32_display_info *dpyinfo = &one_w32_display_info;
3950 /* Set the position of the icon. Note that Windows 95 groups all
3951 icons in the tray. */
3952 icon_x = x_get_arg (dpyinfo, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
3953 icon_y = x_get_arg (dpyinfo, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
3954 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
3956 CHECK_NUMBER (icon_x);
3957 CHECK_NUMBER (icon_y);
3959 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
3960 error ("Both left and top icon corners of icon must be specified");
3962 block_input ();
3964 if (! EQ (icon_x, Qunbound))
3965 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
3967 #if 0 /* TODO */
3968 /* Start up iconic or window? */
3969 x_wm_set_window_state
3970 (f, (EQ (x_get_arg (dpyinfo, parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL), Qicon)
3971 ? IconicState
3972 : NormalState));
3974 x_text_icon (f, SSDATA ((!NILP (f->icon_name)
3975 ? f->icon_name
3976 : f->name)));
3977 #endif
3979 unblock_input ();
3983 static void
3984 x_make_gc (struct frame *f)
3986 XGCValues gc_values;
3988 block_input ();
3990 /* Create the GC's of this frame.
3991 Note that many default values are used. */
3993 /* Normal video */
3994 gc_values.font = FRAME_FONT (f);
3996 /* Cursor has cursor-color background, background-color foreground. */
3997 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
3998 gc_values.background = f->output_data.w32->cursor_pixel;
3999 f->output_data.w32->cursor_gc
4000 = XCreateGC (NULL, FRAME_W32_WINDOW (f),
4001 (GCFont | GCForeground | GCBackground),
4002 &gc_values);
4004 /* Reliefs. */
4005 f->output_data.w32->white_relief.gc = 0;
4006 f->output_data.w32->black_relief.gc = 0;
4008 unblock_input ();
4012 /* Handler for signals raised during x_create_frame and
4013 x_create_tip_frame. FRAME is the frame which is partially
4014 constructed. */
4016 static Lisp_Object
4017 unwind_create_frame (Lisp_Object frame)
4019 struct frame *f = XFRAME (frame);
4021 /* If frame is ``official'', nothing to do. */
4022 if (NILP (Fmemq (frame, Vframe_list)))
4024 #ifdef GLYPH_DEBUG
4025 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4026 #endif
4028 x_free_frame_resources (f);
4029 free_glyphs (f);
4031 #ifdef GLYPH_DEBUG
4032 /* Check that reference counts are indeed correct. */
4033 eassert (dpyinfo->reference_count == dpyinfo_refcount);
4034 eassert (dpyinfo->terminal->image_cache->refcount == image_cache_refcount);
4035 #endif
4036 return Qt;
4039 return Qnil;
4042 static void
4043 x_default_font_parameter (struct frame *f, Lisp_Object parms)
4045 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4046 Lisp_Object font_param = x_get_arg (dpyinfo, parms, Qfont, NULL, NULL,
4047 RES_TYPE_STRING);
4048 Lisp_Object font;
4049 if (EQ (font_param, Qunbound))
4050 font_param = Qnil;
4051 font = !NILP (font_param) ? font_param
4052 : x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
4054 if (!STRINGP (font))
4056 int i;
4057 static char *names[]
4058 = { "Courier New-10",
4059 "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1",
4060 "-*-Fixedsys-normal-r-*-*-12-*-*-*-c-*-iso8859-1",
4061 "Fixedsys",
4062 NULL };
4064 for (i = 0; names[i]; i++)
4066 font = font_open_by_name (f, build_unibyte_string (names[i]));
4067 if (! NILP (font))
4068 break;
4070 if (NILP (font))
4071 error ("No suitable font was found");
4073 else if (!NILP (font_param))
4075 /* Remember the explicit font parameter, so we can re-apply it after
4076 we've applied the `default' face settings. */
4077 x_set_frame_parameters (f, Fcons (Fcons (Qfont_param, font_param), Qnil));
4079 x_default_parameter (f, parms, Qfont, font, "font", "Font", RES_TYPE_STRING);
4082 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
4083 1, 1, 0,
4084 doc: /* Make a new window, which is called a \"frame\" in Emacs terms.
4085 Return an Emacs frame object.
4086 PARAMETERS is an alist of frame parameters.
4087 If the parameters specify that the frame should not have a minibuffer,
4088 and do not specify a specific minibuffer window to use,
4089 then `default-minibuffer-frame' must be a frame whose minibuffer can
4090 be shared by the new frame.
4092 This function is an internal primitive--use `make-frame' instead. */)
4093 (Lisp_Object parameters)
4095 struct frame *f;
4096 Lisp_Object frame, tem;
4097 Lisp_Object name;
4098 int minibuffer_only = 0;
4099 long window_prompting = 0;
4100 int width, height;
4101 ptrdiff_t count = SPECPDL_INDEX ();
4102 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4103 Lisp_Object display;
4104 struct w32_display_info *dpyinfo = NULL;
4105 Lisp_Object parent;
4106 struct kboard *kb;
4108 /* Make copy of frame parameters because the original is in pure
4109 storage now. */
4110 parameters = Fcopy_alist (parameters);
4112 /* Use this general default value to start with
4113 until we know if this frame has a specified name. */
4114 Vx_resource_name = Vinvocation_name;
4116 display = x_get_arg (dpyinfo, parameters, Qterminal, 0, 0, RES_TYPE_NUMBER);
4117 if (EQ (display, Qunbound))
4118 display = x_get_arg (dpyinfo, parameters, Qdisplay, 0, 0, RES_TYPE_STRING);
4119 if (EQ (display, Qunbound))
4120 display = Qnil;
4121 dpyinfo = check_x_display_info (display);
4122 kb = dpyinfo->terminal->kboard;
4124 if (!dpyinfo->terminal->name)
4125 error ("Terminal is not live, can't create new frames on it");
4127 name = x_get_arg (dpyinfo, parameters, Qname, "name", "Name", RES_TYPE_STRING);
4128 if (!STRINGP (name)
4129 && ! EQ (name, Qunbound)
4130 && ! NILP (name))
4131 error ("Invalid frame name--not a string or nil");
4133 if (STRINGP (name))
4134 Vx_resource_name = name;
4136 /* See if parent window is specified. */
4137 parent = x_get_arg (dpyinfo, parameters, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
4138 if (EQ (parent, Qunbound))
4139 parent = Qnil;
4140 if (! NILP (parent))
4141 CHECK_NUMBER (parent);
4143 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
4144 /* No need to protect DISPLAY because that's not used after passing
4145 it to make_frame_without_minibuffer. */
4146 frame = Qnil;
4147 GCPRO4 (parameters, parent, name, frame);
4148 tem = x_get_arg (dpyinfo, parameters, Qminibuffer, "minibuffer", "Minibuffer",
4149 RES_TYPE_SYMBOL);
4150 if (EQ (tem, Qnone) || NILP (tem))
4151 f = make_frame_without_minibuffer (Qnil, kb, display);
4152 else if (EQ (tem, Qonly))
4154 f = make_minibuffer_frame ();
4155 minibuffer_only = 1;
4157 else if (WINDOWP (tem))
4158 f = make_frame_without_minibuffer (tem, kb, display);
4159 else
4160 f = make_frame (1);
4162 XSETFRAME (frame, f);
4164 /* Note that Windows does support scroll bars. */
4165 FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;
4167 /* By default, make scrollbars the system standard width. */
4168 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = GetSystemMetrics (SM_CXVSCROLL);
4170 f->terminal = dpyinfo->terminal;
4172 f->output_method = output_w32;
4173 f->output_data.w32 = xzalloc (sizeof (struct w32_output));
4174 FRAME_FONTSET (f) = -1;
4176 fset_icon_name
4177 (f, x_get_arg (dpyinfo, parameters, Qicon_name, "iconName", "Title",
4178 RES_TYPE_STRING));
4179 if (! STRINGP (f->icon_name))
4180 fset_icon_name (f, Qnil);
4182 /* FRAME_W32_DISPLAY_INFO (f) = dpyinfo; */
4184 /* With FRAME_X_DISPLAY_INFO set up, this unwind-protect is safe. */
4185 record_unwind_protect (unwind_create_frame, frame);
4186 #ifdef GLYPH_DEBUG
4187 image_cache_refcount =
4188 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
4189 dpyinfo_refcount = dpyinfo->reference_count;
4190 #endif /* GLYPH_DEBUG */
4192 /* Specify the parent under which to make this window. */
4194 if (!NILP (parent))
4196 f->output_data.w32->parent_desc = (Window) XFASTINT (parent);
4197 f->output_data.w32->explicit_parent = 1;
4199 else
4201 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
4202 f->output_data.w32->explicit_parent = 0;
4205 /* Set the name; the functions to which we pass f expect the name to
4206 be set. */
4207 if (EQ (name, Qunbound) || NILP (name))
4209 fset_name (f, build_string (dpyinfo->w32_id_name));
4210 f->explicit_name = 0;
4212 else
4214 fset_name (f, name);
4215 f->explicit_name = 1;
4216 /* use the frame's title when getting resources for this frame. */
4217 specbind (Qx_resource_name, name);
4220 f->resx = dpyinfo->resx;
4221 f->resy = dpyinfo->resy;
4223 if (uniscribe_available)
4224 register_font_driver (&uniscribe_font_driver, f);
4225 register_font_driver (&w32font_driver, f);
4227 x_default_parameter (f, parameters, Qfont_backend, Qnil,
4228 "fontBackend", "FontBackend", RES_TYPE_STRING);
4229 /* Extract the window parameters from the supplied values
4230 that are needed to determine window geometry. */
4231 x_default_font_parameter (f, parameters);
4232 x_default_parameter (f, parameters, Qborder_width, make_number (2),
4233 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
4235 /* We recognize either internalBorderWidth or internalBorder
4236 (which is what xterm calls it). */
4237 if (NILP (Fassq (Qinternal_border_width, parameters)))
4239 Lisp_Object value;
4241 value = x_get_arg (dpyinfo, parameters, Qinternal_border_width,
4242 "internalBorder", "InternalBorder", RES_TYPE_NUMBER);
4243 if (! EQ (value, Qunbound))
4244 parameters = Fcons (Fcons (Qinternal_border_width, value),
4245 parameters);
4247 /* Default internalBorderWidth to 0 on Windows to match other programs. */
4248 x_default_parameter (f, parameters, Qinternal_border_width, make_number (0),
4249 "internalBorderWidth", "InternalBorder", RES_TYPE_NUMBER);
4250 x_default_parameter (f, parameters, Qvertical_scroll_bars, Qright,
4251 "verticalScrollBars", "ScrollBars", RES_TYPE_SYMBOL);
4253 /* Also do the stuff which must be set before the window exists. */
4254 x_default_parameter (f, parameters, Qforeground_color, build_string ("black"),
4255 "foreground", "Foreground", RES_TYPE_STRING);
4256 x_default_parameter (f, parameters, Qbackground_color, build_string ("white"),
4257 "background", "Background", RES_TYPE_STRING);
4258 x_default_parameter (f, parameters, Qmouse_color, build_string ("black"),
4259 "pointerColor", "Foreground", RES_TYPE_STRING);
4260 x_default_parameter (f, parameters, Qborder_color, build_string ("black"),
4261 "borderColor", "BorderColor", RES_TYPE_STRING);
4262 x_default_parameter (f, parameters, Qscreen_gamma, Qnil,
4263 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
4264 x_default_parameter (f, parameters, Qline_spacing, Qnil,
4265 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
4266 x_default_parameter (f, parameters, Qleft_fringe, Qnil,
4267 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
4268 x_default_parameter (f, parameters, Qright_fringe, Qnil,
4269 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
4271 /* Init faces before x_default_parameter is called for scroll-bar
4272 parameters because that function calls x_set_scroll_bar_width,
4273 which calls change_frame_size, which calls Fset_window_buffer,
4274 which runs hooks, which call Fvertical_motion. At the end, we
4275 end up in init_iterator with a null face cache, which should not
4276 happen. */
4277 init_frame_faces (f);
4279 /* The X resources controlling the menu-bar and tool-bar are
4280 processed specially at startup, and reflected in the mode
4281 variables; ignore them here. */
4282 x_default_parameter (f, parameters, Qmenu_bar_lines,
4283 NILP (Vmenu_bar_mode)
4284 ? make_number (0) : make_number (1),
4285 NULL, NULL, RES_TYPE_NUMBER);
4286 x_default_parameter (f, parameters, Qtool_bar_lines,
4287 NILP (Vtool_bar_mode)
4288 ? make_number (0) : make_number (1),
4289 NULL, NULL, RES_TYPE_NUMBER);
4291 x_default_parameter (f, parameters, Qbuffer_predicate, Qnil,
4292 "bufferPredicate", "BufferPredicate", RES_TYPE_SYMBOL);
4293 x_default_parameter (f, parameters, Qtitle, Qnil,
4294 "title", "Title", RES_TYPE_STRING);
4295 x_default_parameter (f, parameters, Qfullscreen, Qnil,
4296 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
4298 f->output_data.w32->dwStyle = WS_OVERLAPPEDWINDOW;
4299 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
4301 f->output_data.w32->text_cursor = w32_load_cursor (IDC_IBEAM);
4302 f->output_data.w32->nontext_cursor = w32_load_cursor (IDC_ARROW);
4303 f->output_data.w32->modeline_cursor = w32_load_cursor (IDC_ARROW);
4304 f->output_data.w32->hand_cursor = w32_load_cursor (IDC_HAND);
4305 f->output_data.w32->hourglass_cursor = w32_load_cursor (IDC_WAIT);
4306 f->output_data.w32->horizontal_drag_cursor = w32_load_cursor (IDC_SIZEWE);
4308 f->output_data.w32->current_cursor = f->output_data.w32->nontext_cursor;
4310 window_prompting = x_figure_window_size (f, parameters, 1);
4312 tem = x_get_arg (dpyinfo, parameters, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
4313 f->no_split = minibuffer_only || EQ (tem, Qt);
4315 w32_window (f, window_prompting, minibuffer_only);
4316 x_icon (f, parameters);
4318 x_make_gc (f);
4320 /* Now consider the frame official. */
4321 f->terminal->reference_count++;
4322 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
4323 Vframe_list = Fcons (frame, Vframe_list);
4325 /* We need to do this after creating the window, so that the
4326 icon-creation functions can say whose icon they're describing. */
4327 x_default_parameter (f, parameters, Qicon_type, Qnil,
4328 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
4330 x_default_parameter (f, parameters, Qauto_raise, Qnil,
4331 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4332 x_default_parameter (f, parameters, Qauto_lower, Qnil,
4333 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4334 x_default_parameter (f, parameters, Qcursor_type, Qbox,
4335 "cursorType", "CursorType", RES_TYPE_SYMBOL);
4336 x_default_parameter (f, parameters, Qscroll_bar_width, Qnil,
4337 "scrollBarWidth", "ScrollBarWidth", RES_TYPE_NUMBER);
4338 x_default_parameter (f, parameters, Qalpha, Qnil,
4339 "alpha", "Alpha", RES_TYPE_NUMBER);
4341 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
4342 Change will not be effected unless different from the current
4343 FRAME_LINES (f). */
4344 width = FRAME_COLS (f);
4345 height = FRAME_LINES (f);
4347 FRAME_LINES (f) = 0;
4348 SET_FRAME_COLS (f, 0);
4349 change_frame_size (f, height, width, 1, 0, 0);
4351 /* Tell the server what size and position, etc, we want, and how
4352 badly we want them. This should be done after we have the menu
4353 bar so that its size can be taken into account. */
4354 block_input ();
4355 x_wm_set_size_hint (f, window_prompting, 0);
4356 unblock_input ();
4358 /* Make the window appear on the frame and enable display, unless
4359 the caller says not to. However, with explicit parent, Emacs
4360 cannot control visibility, so don't try. */
4361 if (! f->output_data.w32->explicit_parent)
4363 Lisp_Object visibility;
4365 visibility = x_get_arg (dpyinfo, parameters, Qvisibility, 0, 0, RES_TYPE_SYMBOL);
4366 if (EQ (visibility, Qunbound))
4367 visibility = Qt;
4369 if (EQ (visibility, Qicon))
4370 x_iconify_frame (f);
4371 else if (! NILP (visibility))
4372 x_make_frame_visible (f);
4373 else
4374 /* Must have been Qnil. */
4378 /* Initialize `default-minibuffer-frame' in case this is the first
4379 frame on this terminal. */
4380 if (FRAME_HAS_MINIBUF_P (f)
4381 && (!FRAMEP (KVAR (kb, Vdefault_minibuffer_frame))
4382 || !FRAME_LIVE_P (XFRAME (KVAR (kb, Vdefault_minibuffer_frame)))))
4383 kset_default_minibuffer_frame (kb, frame);
4385 /* All remaining specified parameters, which have not been "used"
4386 by x_get_arg and friends, now go in the misc. alist of the frame. */
4387 for (tem = parameters; CONSP (tem); tem = XCDR (tem))
4388 if (CONSP (XCAR (tem)) && !NILP (XCAR (XCAR (tem))))
4389 fset_param_alist (f, Fcons (XCAR (tem), f->param_alist));
4391 UNGCPRO;
4393 /* Make sure windows on this frame appear in calls to next-window
4394 and similar functions. */
4395 Vwindow_list = Qnil;
4397 return unbind_to (count, frame);
4400 /* FRAME is used only to get a handle on the X display. We don't pass the
4401 display info directly because we're called from frame.c, which doesn't
4402 know about that structure. */
4403 Lisp_Object
4404 x_get_focus_frame (struct frame *frame)
4406 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (frame);
4407 Lisp_Object xfocus;
4408 if (! dpyinfo->w32_focus_frame)
4409 return Qnil;
4411 XSETFRAME (xfocus, dpyinfo->w32_focus_frame);
4412 return xfocus;
4415 DEFUN ("x-focus-frame", Fx_focus_frame, Sx_focus_frame, 1, 1, 0,
4416 doc: /* Give FRAME input focus, raising to foreground if necessary. */)
4417 (Lisp_Object frame)
4419 x_focus_on_frame (check_x_frame (frame));
4420 return Qnil;
4424 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
4425 doc: /* Internal function called by `color-defined-p', which see.
4426 \(Note that the Nextstep version of this function ignores FRAME.) */)
4427 (Lisp_Object color, Lisp_Object frame)
4429 XColor foo;
4430 FRAME_PTR f = check_x_frame (frame);
4432 CHECK_STRING (color);
4434 if (w32_defined_color (f, SDATA (color), &foo, 0))
4435 return Qt;
4436 else
4437 return Qnil;
4440 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
4441 doc: /* Internal function called by `color-values', which see. */)
4442 (Lisp_Object color, Lisp_Object frame)
4444 XColor foo;
4445 FRAME_PTR f = check_x_frame (frame);
4447 CHECK_STRING (color);
4449 if (w32_defined_color (f, SDATA (color), &foo, 0))
4450 return list3 (make_number ((GetRValue (foo.pixel) << 8)
4451 | GetRValue (foo.pixel)),
4452 make_number ((GetGValue (foo.pixel) << 8)
4453 | GetGValue (foo.pixel)),
4454 make_number ((GetBValue (foo.pixel) << 8)
4455 | GetBValue (foo.pixel)));
4456 else
4457 return Qnil;
4460 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
4461 doc: /* Internal function called by `display-color-p', which see. */)
4462 (Lisp_Object display)
4464 struct w32_display_info *dpyinfo = check_x_display_info (display);
4466 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 2)
4467 return Qnil;
4469 return Qt;
4472 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p,
4473 Sx_display_grayscale_p, 0, 1, 0,
4474 doc: /* Return t if DISPLAY supports shades of gray.
4475 Note that color displays do support shades of gray.
4476 The optional argument DISPLAY specifies which display to ask about.
4477 DISPLAY should be either a frame or a display name (a string).
4478 If omitted or nil, that stands for the selected frame's display. */)
4479 (Lisp_Object display)
4481 struct w32_display_info *dpyinfo = check_x_display_info (display);
4483 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 1)
4484 return Qnil;
4486 return Qt;
4489 DEFUN ("x-display-pixel-width", Fx_display_pixel_width,
4490 Sx_display_pixel_width, 0, 1, 0,
4491 doc: /* Return the width in pixels of DISPLAY.
4492 The optional argument DISPLAY specifies which display to ask about.
4493 DISPLAY should be either a frame or a display name (a string).
4494 If omitted or nil, that stands for the selected frame's display. */)
4495 (Lisp_Object display)
4497 struct w32_display_info *dpyinfo = check_x_display_info (display);
4499 return make_number (x_display_pixel_width (dpyinfo));
4502 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
4503 Sx_display_pixel_height, 0, 1, 0,
4504 doc: /* Return the height in pixels of DISPLAY.
4505 The optional argument DISPLAY specifies which display to ask about.
4506 DISPLAY should be either a frame or a display name (a string).
4507 If omitted or nil, that stands for the selected frame's display. */)
4508 (Lisp_Object display)
4510 struct w32_display_info *dpyinfo = check_x_display_info (display);
4512 return make_number (x_display_pixel_height (dpyinfo));
4515 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
4516 0, 1, 0,
4517 doc: /* Return the number of bitplanes of DISPLAY.
4518 The optional argument DISPLAY specifies which display to ask about.
4519 DISPLAY should be either a frame or a display name (a string).
4520 If omitted or nil, that stands for the selected frame's display. */)
4521 (Lisp_Object display)
4523 struct w32_display_info *dpyinfo = check_x_display_info (display);
4525 return make_number (dpyinfo->n_planes * dpyinfo->n_cbits);
4528 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
4529 0, 1, 0,
4530 doc: /* Return the number of color cells of DISPLAY.
4531 The optional argument DISPLAY specifies which display to ask about.
4532 DISPLAY should be either a frame or a display name (a string).
4533 If omitted or nil, that stands for the selected frame's display. */)
4534 (Lisp_Object display)
4536 struct w32_display_info *dpyinfo = check_x_display_info (display);
4537 HDC hdc;
4538 int cap;
4540 hdc = GetDC (dpyinfo->root_window);
4541 if (dpyinfo->has_palette)
4542 cap = GetDeviceCaps (hdc, SIZEPALETTE);
4543 else
4544 cap = GetDeviceCaps (hdc, NUMCOLORS);
4546 /* We force 24+ bit depths to 24-bit, both to prevent an overflow
4547 and because probably is more meaningful on Windows anyway */
4548 if (cap < 0)
4549 cap = 1 << min (dpyinfo->n_planes * dpyinfo->n_cbits, 24);
4551 ReleaseDC (dpyinfo->root_window, hdc);
4553 return make_number (cap);
4556 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
4557 Sx_server_max_request_size,
4558 0, 1, 0,
4559 doc: /* Return the maximum request size of the server of DISPLAY.
4560 The optional argument DISPLAY specifies which display to ask about.
4561 DISPLAY should be either a frame or a display name (a string).
4562 If omitted or nil, that stands for the selected frame's display. */)
4563 (Lisp_Object display)
4565 return make_number (1);
4568 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
4569 doc: /* Return the "vendor ID" string of the W32 system (Microsoft).
4570 The optional argument DISPLAY specifies which display to ask about.
4571 DISPLAY should be either a frame or a display name (a string).
4572 If omitted or nil, that stands for the selected frame's display. */)
4573 (Lisp_Object display)
4575 return build_string ("Microsoft Corp.");
4578 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
4579 doc: /* Return the version numbers of the server of DISPLAY.
4580 The value is a list of three integers: the major and minor
4581 version numbers of the X Protocol in use, and the distributor-specific
4582 release number. See also the function `x-server-vendor'.
4584 The optional argument DISPLAY specifies which display to ask about.
4585 DISPLAY should be either a frame or a display name (a string).
4586 If omitted or nil, that stands for the selected frame's display. */)
4587 (Lisp_Object display)
4589 return Fcons (make_number (w32_major_version),
4590 Fcons (make_number (w32_minor_version),
4591 Fcons (make_number (w32_build_number), Qnil)));
4594 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
4595 doc: /* Return the number of screens on the server of DISPLAY.
4596 The optional argument DISPLAY specifies which display to ask about.
4597 DISPLAY should be either a frame or a display name (a string).
4598 If omitted or nil, that stands for the selected frame's display. */)
4599 (Lisp_Object display)
4601 return make_number (1);
4604 DEFUN ("x-display-mm-height", Fx_display_mm_height,
4605 Sx_display_mm_height, 0, 1, 0,
4606 doc: /* Return the height in millimeters of DISPLAY.
4607 The optional argument DISPLAY specifies which display to ask about.
4608 DISPLAY should be either a frame or a display name (a string).
4609 If omitted or nil, that stands for the selected frame's display. */)
4610 (Lisp_Object display)
4612 struct w32_display_info *dpyinfo = check_x_display_info (display);
4613 HDC hdc;
4614 int cap;
4616 hdc = GetDC (dpyinfo->root_window);
4618 cap = GetDeviceCaps (hdc, VERTSIZE);
4620 ReleaseDC (dpyinfo->root_window, hdc);
4622 return make_number (cap);
4625 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
4626 doc: /* Return the width in millimeters of DISPLAY.
4627 The optional argument DISPLAY specifies which display to ask about.
4628 DISPLAY should be either a frame or a display name (a string).
4629 If omitted or nil, that stands for the selected frame's display. */)
4630 (Lisp_Object display)
4632 struct w32_display_info *dpyinfo = check_x_display_info (display);
4634 HDC hdc;
4635 int cap;
4637 hdc = GetDC (dpyinfo->root_window);
4639 cap = GetDeviceCaps (hdc, HORZSIZE);
4641 ReleaseDC (dpyinfo->root_window, hdc);
4643 return make_number (cap);
4646 DEFUN ("x-display-backing-store", Fx_display_backing_store,
4647 Sx_display_backing_store, 0, 1, 0,
4648 doc: /* Return an indication of whether DISPLAY does backing store.
4649 The value may be `always', `when-mapped', or `not-useful'.
4650 The optional argument DISPLAY specifies which display to ask about.
4651 DISPLAY should be either a frame or a display name (a string).
4652 If omitted or nil, that stands for the selected frame's display. */)
4653 (Lisp_Object display)
4655 return intern ("not-useful");
4658 DEFUN ("x-display-visual-class", Fx_display_visual_class,
4659 Sx_display_visual_class, 0, 1, 0,
4660 doc: /* Return the visual class of DISPLAY.
4661 The value is one of the symbols `static-gray', `gray-scale',
4662 `static-color', `pseudo-color', `true-color', or `direct-color'.
4664 The optional argument DISPLAY specifies which display to ask about.
4665 DISPLAY should be either a frame or a display name (a string).
4666 If omitted or nil, that stands for the selected frame's display. */)
4667 (Lisp_Object display)
4669 struct w32_display_info *dpyinfo = check_x_display_info (display);
4670 Lisp_Object result = Qnil;
4672 if (dpyinfo->has_palette)
4673 result = intern ("pseudo-color");
4674 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 1)
4675 result = intern ("static-grey");
4676 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 4)
4677 result = intern ("static-color");
4678 else if (dpyinfo->n_planes * dpyinfo->n_cbits > 8)
4679 result = intern ("true-color");
4681 return result;
4684 DEFUN ("x-display-save-under", Fx_display_save_under,
4685 Sx_display_save_under, 0, 1, 0,
4686 doc: /* Return t if DISPLAY supports the save-under feature.
4687 The optional argument DISPLAY specifies which display to ask about.
4688 DISPLAY should be either a frame or a display name (a string).
4689 If omitted or nil, that stands for the selected frame's display. */)
4690 (Lisp_Object display)
4692 return Qnil;
4696 x_pixel_width (register struct frame *f)
4698 return FRAME_PIXEL_WIDTH (f);
4702 x_pixel_height (register struct frame *f)
4704 return FRAME_PIXEL_HEIGHT (f);
4708 x_char_width (register struct frame *f)
4710 return FRAME_COLUMN_WIDTH (f);
4714 x_char_height (register struct frame *f)
4716 return FRAME_LINE_HEIGHT (f);
4720 x_screen_planes (register struct frame *f)
4722 return FRAME_W32_DISPLAY_INFO (f)->n_planes;
4725 /* Return the display structure for the display named NAME.
4726 Open a new connection if necessary. */
4728 struct w32_display_info *
4729 x_display_info_for_name (Lisp_Object name)
4731 Lisp_Object names;
4732 struct w32_display_info *dpyinfo;
4734 CHECK_STRING (name);
4736 for (dpyinfo = &one_w32_display_info, names = w32_display_name_list;
4737 dpyinfo && !NILP (w32_display_name_list);
4738 dpyinfo = dpyinfo->next, names = XCDR (names))
4740 Lisp_Object tem;
4741 tem = Fstring_equal (XCAR (XCAR (names)), name);
4742 if (!NILP (tem))
4743 return dpyinfo;
4746 /* Use this general default value to start with. */
4747 Vx_resource_name = Vinvocation_name;
4749 validate_x_resource_name ();
4751 dpyinfo = w32_term_init (name, (unsigned char *)0,
4752 SSDATA (Vx_resource_name));
4754 if (dpyinfo == 0)
4755 error ("Cannot connect to server %s", SDATA (name));
4757 w32_in_use = 1;
4758 XSETFASTINT (Vwindow_system_version, w32_major_version);
4760 return dpyinfo;
4763 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
4764 1, 3, 0, doc: /* Open a connection to a display server.
4765 DISPLAY is the name of the display to connect to.
4766 Optional second arg XRM-STRING is a string of resources in xrdb format.
4767 If the optional third arg MUST-SUCCEED is non-nil,
4768 terminate Emacs if we can't open the connection.
4769 \(In the Nextstep version, the last two arguments are currently ignored.) */)
4770 (Lisp_Object display, Lisp_Object xrm_string, Lisp_Object must_succeed)
4772 unsigned char *xrm_option;
4773 struct w32_display_info *dpyinfo;
4775 /* If initialization has already been done, return now to avoid
4776 overwriting critical parts of one_w32_display_info. */
4777 if (w32_in_use)
4778 return Qnil;
4780 CHECK_STRING (display);
4781 if (! NILP (xrm_string))
4782 CHECK_STRING (xrm_string);
4784 #if 0
4785 if (! EQ (Vwindow_system, intern ("w32")))
4786 error ("Not using Microsoft Windows");
4787 #endif
4789 /* Allow color mapping to be defined externally; first look in user's
4790 HOME directory, then in Emacs etc dir for a file called rgb.txt. */
4792 Lisp_Object color_file;
4793 struct gcpro gcpro1;
4795 color_file = build_string ("~/rgb.txt");
4797 GCPRO1 (color_file);
4799 if (NILP (Ffile_readable_p (color_file)))
4800 color_file =
4801 Fexpand_file_name (build_string ("rgb.txt"),
4802 Fsymbol_value (intern ("data-directory")));
4804 Vw32_color_map = Fx_load_color_file (color_file);
4806 UNGCPRO;
4808 if (NILP (Vw32_color_map))
4809 Vw32_color_map = w32_default_color_map ();
4811 /* Merge in system logical colors. */
4812 add_system_logical_colors_to_map (&Vw32_color_map);
4814 if (! NILP (xrm_string))
4815 xrm_option = SDATA (xrm_string);
4816 else
4817 xrm_option = (unsigned char *) 0;
4819 /* Use this general default value to start with. */
4820 /* First remove .exe suffix from invocation-name - it looks ugly. */
4822 char basename[ MAX_PATH ], *str;
4824 strcpy (basename, SDATA (Vinvocation_name));
4825 str = strrchr (basename, '.');
4826 if (str) *str = 0;
4827 Vinvocation_name = build_string (basename);
4829 Vx_resource_name = Vinvocation_name;
4831 validate_x_resource_name ();
4833 /* This is what opens the connection and sets x_current_display.
4834 This also initializes many symbols, such as those used for input. */
4835 dpyinfo = w32_term_init (display, xrm_option,
4836 SSDATA (Vx_resource_name));
4838 if (dpyinfo == 0)
4840 if (!NILP (must_succeed))
4841 fatal ("Cannot connect to server %s.\n",
4842 SDATA (display));
4843 else
4844 error ("Cannot connect to server %s", SDATA (display));
4847 w32_in_use = 1;
4849 XSETFASTINT (Vwindow_system_version, w32_major_version);
4850 return Qnil;
4853 DEFUN ("x-close-connection", Fx_close_connection,
4854 Sx_close_connection, 1, 1, 0,
4855 doc: /* Close the connection to DISPLAY's server.
4856 For DISPLAY, specify either a frame or a display name (a string).
4857 If DISPLAY is nil, that stands for the selected frame's display. */)
4858 (Lisp_Object display)
4860 struct w32_display_info *dpyinfo = check_x_display_info (display);
4862 if (dpyinfo->reference_count > 0)
4863 error ("Display still has frames on it");
4865 block_input ();
4866 x_destroy_all_bitmaps (dpyinfo);
4868 x_delete_display (dpyinfo);
4869 unblock_input ();
4871 return Qnil;
4874 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
4875 doc: /* Return the list of display names that Emacs has connections to. */)
4876 (void)
4878 Lisp_Object tail, result;
4880 result = Qnil;
4881 for (tail = w32_display_name_list; CONSP (tail); tail = XCDR (tail))
4882 result = Fcons (XCAR (XCAR (tail)), result);
4884 return result;
4887 DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
4888 doc: /* If ON is non-nil, report X errors as soon as the erring request is made.
4889 This function only has an effect on X Windows. With MS Windows, it is
4890 defined but does nothing.
4892 If ON is nil, allow buffering of requests.
4893 Turning on synchronization prohibits the Xlib routines from buffering
4894 requests and seriously degrades performance, but makes debugging much
4895 easier.
4896 The optional second argument TERMINAL specifies which display to act on.
4897 TERMINAL should be a terminal object, a frame or a display name (a string).
4898 If TERMINAL is omitted or nil, that stands for the selected frame's display. */)
4899 (Lisp_Object on, Lisp_Object display)
4901 return Qnil;
4906 /***********************************************************************
4907 Window properties
4908 ***********************************************************************/
4910 #if 0 /* TODO : port window properties to W32 */
4912 DEFUN ("x-change-window-property", Fx_change_window_property,
4913 Sx_change_window_property, 2, 6, 0,
4914 doc: /* Change window property PROP to VALUE on the X window of FRAME.
4915 PROP must be a string. VALUE may be a string or a list of conses,
4916 numbers and/or strings. If an element in the list is a string, it is
4917 converted to an atom and the value of the Atom is used. If an element
4918 is a cons, it is converted to a 32 bit number where the car is the 16
4919 top bits and the cdr is the lower 16 bits.
4921 FRAME nil or omitted means use the selected frame.
4922 If TYPE is given and non-nil, it is the name of the type of VALUE.
4923 If TYPE is not given or nil, the type is STRING.
4924 FORMAT gives the size in bits of each element if VALUE is a list.
4925 It must be one of 8, 16 or 32.
4926 If VALUE is a string or FORMAT is nil or not given, FORMAT defaults to 8.
4927 If OUTER-P is non-nil, the property is changed for the outer X window of
4928 FRAME. Default is to change on the edit X window. */)
4929 (Lisp_Object prop, Lisp_Object value, Lisp_Object frame,
4930 Lisp_Object type, Lisp_Object format, Lisp_Object outer_p)
4932 struct frame *f = check_x_frame (frame);
4933 Atom prop_atom;
4935 CHECK_STRING (prop);
4936 CHECK_STRING (value);
4938 block_input ();
4939 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
4940 XChangeProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
4941 prop_atom, XA_STRING, 8, PropModeReplace,
4942 SDATA (value), SCHARS (value));
4944 /* Make sure the property is set when we return. */
4945 XFlush (FRAME_W32_DISPLAY (f));
4946 unblock_input ();
4948 return value;
4952 DEFUN ("x-delete-window-property", Fx_delete_window_property,
4953 Sx_delete_window_property, 1, 2, 0,
4954 doc: /* Remove window property PROP from X window of FRAME.
4955 FRAME nil or omitted means use the selected frame. Value is PROP. */)
4956 (Lisp_Object prop, Lisp_Object frame)
4958 struct frame *f = check_x_frame (frame);
4959 Atom prop_atom;
4961 CHECK_STRING (prop);
4962 block_input ();
4963 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
4964 XDeleteProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), prop_atom);
4966 /* Make sure the property is removed when we return. */
4967 XFlush (FRAME_W32_DISPLAY (f));
4968 unblock_input ();
4970 return prop;
4974 DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
4975 1, 6, 0,
4976 doc: /* Value is the value of window property PROP on FRAME.
4977 If FRAME is nil or omitted, use the selected frame.
4979 On X Windows, the following optional arguments are also accepted:
4980 If TYPE is nil or omitted, get the property as a string.
4981 Otherwise TYPE is the name of the atom that denotes the type expected.
4982 If SOURCE is non-nil, get the property on that window instead of from
4983 FRAME. The number 0 denotes the root window.
4984 If DELETE-P is non-nil, delete the property after retrieving it.
4985 If VECTOR-RET-P is non-nil, don't return a string but a vector of values.
4987 On MS Windows, this function accepts but ignores those optional arguments.
4989 Value is nil if FRAME hasn't a property with name PROP or if PROP has
4990 no value of TYPE (always string in the MS Windows case). */)
4991 (Lisp_Object prop, Lisp_Object frame, Lisp_Object type,
4992 Lisp_Object source, Lisp_Object delete_p, Lisp_Object vector_ret_p)
4994 struct frame *f = check_x_frame (frame);
4995 Atom prop_atom;
4996 int rc;
4997 Lisp_Object prop_value = Qnil;
4998 char *tmp_data = NULL;
4999 Atom actual_type;
5000 int actual_format;
5001 unsigned long actual_size, bytes_remaining;
5003 CHECK_STRING (prop);
5004 block_input ();
5005 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
5006 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
5007 prop_atom, 0, 0, False, XA_STRING,
5008 &actual_type, &actual_format, &actual_size,
5009 &bytes_remaining, (unsigned char **) &tmp_data);
5010 if (rc == Success)
5012 int size = bytes_remaining;
5014 XFree (tmp_data);
5015 tmp_data = NULL;
5017 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
5018 prop_atom, 0, bytes_remaining,
5019 False, XA_STRING,
5020 &actual_type, &actual_format,
5021 &actual_size, &bytes_remaining,
5022 (unsigned char **) &tmp_data);
5023 if (rc == Success)
5024 prop_value = make_string (tmp_data, size);
5026 XFree (tmp_data);
5029 unblock_input ();
5031 return prop_value;
5033 return Qnil;
5036 #endif /* TODO */
5039 /***********************************************************************
5040 Busy cursor
5041 ***********************************************************************/
5043 /* Default number of seconds to wait before displaying an hourglass
5044 cursor. Duplicated from xdisp.c, but cannot use the version there
5045 due to lack of atimers on w32. */
5046 #define DEFAULT_HOURGLASS_DELAY 1
5048 /* Cancel a currently active hourglass timer, and start a new one. */
5050 void
5051 start_hourglass (void)
5053 DWORD delay;
5054 int secs, msecs = 0;
5055 struct frame * f = SELECTED_FRAME ();
5057 /* No cursors on non GUI frames. */
5058 if (!FRAME_W32_P (f))
5059 return;
5061 cancel_hourglass ();
5063 if (INTEGERP (Vhourglass_delay)
5064 && XINT (Vhourglass_delay) > 0)
5065 secs = XFASTINT (Vhourglass_delay);
5066 else if (FLOATP (Vhourglass_delay)
5067 && XFLOAT_DATA (Vhourglass_delay) > 0)
5069 Lisp_Object tem;
5070 tem = Ftruncate (Vhourglass_delay, Qnil);
5071 secs = XFASTINT (tem);
5072 msecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000;
5074 else
5075 secs = DEFAULT_HOURGLASS_DELAY;
5077 delay = secs * 1000 + msecs;
5078 hourglass_hwnd = FRAME_W32_WINDOW (f);
5079 hourglass_timer = SetTimer (hourglass_hwnd, HOURGLASS_ID, delay, NULL);
5083 /* Cancel the hourglass cursor timer if active, hide an hourglass
5084 cursor if shown. */
5086 void
5087 cancel_hourglass (void)
5089 if (hourglass_timer)
5091 KillTimer (hourglass_hwnd, hourglass_timer);
5092 hourglass_timer = 0;
5095 if (hourglass_shown_p)
5096 w32_hide_hourglass ();
5100 /* Timer function of hourglass_timer.
5102 Display an hourglass cursor. Set the hourglass_p flag in display info
5103 to indicate that an hourglass cursor is shown. */
5105 static void
5106 w32_show_hourglass (struct frame *f)
5108 if (!hourglass_shown_p)
5110 f->output_data.w32->hourglass_p = 1;
5111 if (!menubar_in_use && !current_popup_menu)
5112 SetCursor (f->output_data.w32->hourglass_cursor);
5113 hourglass_shown_p = 1;
5118 /* Hide the hourglass cursor on all frames, if it is currently shown. */
5120 static void
5121 w32_hide_hourglass (void)
5123 if (hourglass_shown_p)
5125 struct frame *f = x_window_to_frame (&one_w32_display_info,
5126 hourglass_hwnd);
5127 if (f)
5128 f->output_data.w32->hourglass_p = 0;
5129 else
5130 /* If frame was deleted, restore to selected frame's cursor. */
5131 f = SELECTED_FRAME ();
5133 if (FRAME_W32_P (f))
5134 SetCursor (f->output_data.w32->current_cursor);
5135 else
5136 /* No cursors on non GUI frames - restore to stock arrow cursor. */
5137 SetCursor (w32_load_cursor (IDC_ARROW));
5139 hourglass_shown_p = 0;
5145 /***********************************************************************
5146 Tool tips
5147 ***********************************************************************/
5149 static Lisp_Object x_create_tip_frame (struct w32_display_info *,
5150 Lisp_Object, Lisp_Object);
5151 static void compute_tip_xy (struct frame *, Lisp_Object, Lisp_Object,
5152 Lisp_Object, int, int, int *, int *);
5154 /* The frame of a currently visible tooltip. */
5156 Lisp_Object tip_frame;
5158 /* If non-nil, a timer started that hides the last tooltip when it
5159 fires. */
5161 Lisp_Object tip_timer;
5162 Window tip_window;
5164 /* If non-nil, a vector of 3 elements containing the last args
5165 with which x-show-tip was called. See there. */
5167 Lisp_Object last_show_tip_args;
5170 static Lisp_Object
5171 unwind_create_tip_frame (Lisp_Object frame)
5173 Lisp_Object deleted;
5175 deleted = unwind_create_frame (frame);
5176 if (EQ (deleted, Qt))
5178 tip_window = NULL;
5179 tip_frame = Qnil;
5182 return deleted;
5186 /* Create a frame for a tooltip on the display described by DPYINFO.
5187 PARMS is a list of frame parameters. TEXT is the string to
5188 display in the tip frame. Value is the frame.
5190 Note that functions called here, esp. x_default_parameter can
5191 signal errors, for instance when a specified color name is
5192 undefined. We have to make sure that we're in a consistent state
5193 when this happens. */
5195 static Lisp_Object
5196 x_create_tip_frame (struct w32_display_info *dpyinfo,
5197 Lisp_Object parms, Lisp_Object text)
5199 struct frame *f;
5200 Lisp_Object frame;
5201 Lisp_Object name;
5202 long window_prompting = 0;
5203 int width, height;
5204 ptrdiff_t count = SPECPDL_INDEX ();
5205 struct gcpro gcpro1, gcpro2, gcpro3;
5206 struct kboard *kb;
5207 int face_change_count_before = face_change_count;
5208 Lisp_Object buffer;
5209 struct buffer *old_buffer;
5211 check_w32 ();
5213 /* Use this general default value to start with until we know if
5214 this frame has a specified name. */
5215 Vx_resource_name = Vinvocation_name;
5217 kb = dpyinfo->terminal->kboard;
5219 /* The calls to x_get_arg remove elements from PARMS, so copy it to
5220 avoid destructive changes behind our caller's back. */
5221 parms = Fcopy_alist (parms);
5223 /* Get the name of the frame to use for resource lookup. */
5224 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
5225 if (!STRINGP (name)
5226 && !EQ (name, Qunbound)
5227 && !NILP (name))
5228 error ("Invalid frame name--not a string or nil");
5229 Vx_resource_name = name;
5231 frame = Qnil;
5232 GCPRO3 (parms, name, frame);
5233 /* Make a frame without minibuffer nor mode-line. */
5234 f = make_frame (0);
5235 f->wants_modeline = 0;
5236 XSETFRAME (frame, f);
5238 buffer = Fget_buffer_create (build_string (" *tip*"));
5239 /* Use set_window_buffer instead of Fset_window_buffer (see
5240 discussion of bug#11984, bug#12025, bug#12026). */
5241 set_window_buffer (FRAME_ROOT_WINDOW (f), buffer, 0, 0);
5242 old_buffer = current_buffer;
5243 set_buffer_internal_1 (XBUFFER (buffer));
5244 bset_truncate_lines (current_buffer, Qnil);
5245 specbind (Qinhibit_read_only, Qt);
5246 specbind (Qinhibit_modification_hooks, Qt);
5247 Ferase_buffer ();
5248 Finsert (1, &text);
5249 set_buffer_internal_1 (old_buffer);
5251 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
5252 record_unwind_protect (unwind_create_tip_frame, frame);
5254 /* By setting the output method, we're essentially saying that
5255 the frame is live, as per FRAME_LIVE_P. If we get a signal
5256 from this point on, x_destroy_window might screw up reference
5257 counts etc. */
5258 f->terminal = dpyinfo->terminal;
5259 f->output_method = output_w32;
5260 f->output_data.w32 = xzalloc (sizeof (struct w32_output));
5262 FRAME_FONTSET (f) = -1;
5263 fset_icon_name (f, Qnil);
5265 #ifdef GLYPH_DEBUG
5266 image_cache_refcount =
5267 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
5268 dpyinfo_refcount = dpyinfo->reference_count;
5269 #endif /* GLYPH_DEBUG */
5270 FRAME_KBOARD (f) = kb;
5271 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
5272 f->output_data.w32->explicit_parent = 0;
5274 /* Set the name; the functions to which we pass f expect the name to
5275 be set. */
5276 if (EQ (name, Qunbound) || NILP (name))
5278 fset_name (f, build_string (dpyinfo->w32_id_name));
5279 f->explicit_name = 0;
5281 else
5283 fset_name (f, name);
5284 f->explicit_name = 1;
5285 /* use the frame's title when getting resources for this frame. */
5286 specbind (Qx_resource_name, name);
5289 f->resx = dpyinfo->resx;
5290 f->resy = dpyinfo->resy;
5292 if (uniscribe_available)
5293 register_font_driver (&uniscribe_font_driver, f);
5294 register_font_driver (&w32font_driver, f);
5296 x_default_parameter (f, parms, Qfont_backend, Qnil,
5297 "fontBackend", "FontBackend", RES_TYPE_STRING);
5299 /* Extract the window parameters from the supplied values
5300 that are needed to determine window geometry. */
5301 x_default_font_parameter (f, parms);
5303 x_default_parameter (f, parms, Qborder_width, make_number (2),
5304 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
5305 /* This defaults to 2 in order to match xterm. We recognize either
5306 internalBorderWidth or internalBorder (which is what xterm calls
5307 it). */
5308 if (NILP (Fassq (Qinternal_border_width, parms)))
5310 Lisp_Object value;
5312 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
5313 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
5314 if (! EQ (value, Qunbound))
5315 parms = Fcons (Fcons (Qinternal_border_width, value),
5316 parms);
5318 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
5319 "internalBorderWidth", "internalBorderWidth",
5320 RES_TYPE_NUMBER);
5322 /* Also do the stuff which must be set before the window exists. */
5323 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
5324 "foreground", "Foreground", RES_TYPE_STRING);
5325 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
5326 "background", "Background", RES_TYPE_STRING);
5327 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
5328 "pointerColor", "Foreground", RES_TYPE_STRING);
5329 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
5330 "cursorColor", "Foreground", RES_TYPE_STRING);
5331 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
5332 "borderColor", "BorderColor", RES_TYPE_STRING);
5334 /* Init faces before x_default_parameter is called for scroll-bar
5335 parameters because that function calls x_set_scroll_bar_width,
5336 which calls change_frame_size, which calls Fset_window_buffer,
5337 which runs hooks, which call Fvertical_motion. At the end, we
5338 end up in init_iterator with a null face cache, which should not
5339 happen. */
5340 init_frame_faces (f);
5342 f->output_data.w32->dwStyle = WS_BORDER | WS_POPUP | WS_DISABLED;
5343 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
5345 window_prompting = x_figure_window_size (f, parms, 0);
5347 /* No fringes on tip frame. */
5348 f->fringe_cols = 0;
5349 f->left_fringe_width = 0;
5350 f->right_fringe_width = 0;
5352 block_input ();
5353 my_create_tip_window (f);
5354 unblock_input ();
5356 x_make_gc (f);
5358 x_default_parameter (f, parms, Qauto_raise, Qnil,
5359 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5360 x_default_parameter (f, parms, Qauto_lower, Qnil,
5361 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5362 x_default_parameter (f, parms, Qcursor_type, Qbox,
5363 "cursorType", "CursorType", RES_TYPE_SYMBOL);
5365 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
5366 Change will not be effected unless different from the current
5367 FRAME_LINES (f). */
5368 width = FRAME_COLS (f);
5369 height = FRAME_LINES (f);
5370 FRAME_LINES (f) = 0;
5371 SET_FRAME_COLS (f, 0);
5372 change_frame_size (f, height, width, 1, 0, 0);
5374 /* Add `tooltip' frame parameter's default value. */
5375 if (NILP (Fframe_parameter (frame, Qtooltip)))
5376 Fmodify_frame_parameters (frame, Fcons (Fcons (Qtooltip, Qt), Qnil));
5378 /* Set up faces after all frame parameters are known. This call
5379 also merges in face attributes specified for new frames.
5381 Frame parameters may be changed if .Xdefaults contains
5382 specifications for the default font. For example, if there is an
5383 `Emacs.default.attributeBackground: pink', the `background-color'
5384 attribute of the frame get's set, which let's the internal border
5385 of the tooltip frame appear in pink. Prevent this. */
5387 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
5388 Lisp_Object fg = Fframe_parameter (frame, Qforeground_color);
5389 Lisp_Object colors = Qnil;
5391 /* Set tip_frame here, so that */
5392 tip_frame = frame;
5393 call2 (Qface_set_after_frame_default, frame, Qnil);
5395 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
5396 colors = Fcons (Fcons (Qbackground_color, bg), colors);
5397 if (!EQ (fg, Fframe_parameter (frame, Qforeground_color)))
5398 colors = Fcons (Fcons (Qforeground_color, fg), colors);
5400 if (!NILP (colors))
5401 Fmodify_frame_parameters (frame, colors);
5404 f->no_split = 1;
5406 UNGCPRO;
5408 /* Now that the frame is official, it counts as a reference to
5409 its display. */
5410 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
5411 f->terminal->reference_count++;
5413 /* It is now ok to make the frame official even if we get an error
5414 below. And the frame needs to be on Vframe_list or making it
5415 visible won't work. */
5416 Vframe_list = Fcons (frame, Vframe_list);
5418 /* Setting attributes of faces of the tooltip frame from resources
5419 and similar will increment face_change_count, which leads to the
5420 clearing of all current matrices. Since this isn't necessary
5421 here, avoid it by resetting face_change_count to the value it
5422 had before we created the tip frame. */
5423 face_change_count = face_change_count_before;
5425 /* Discard the unwind_protect. */
5426 return unbind_to (count, frame);
5430 /* Compute where to display tip frame F. PARMS is the list of frame
5431 parameters for F. DX and DY are specified offsets from the current
5432 location of the mouse. WIDTH and HEIGHT are the width and height
5433 of the tooltip. Return coordinates relative to the root window of
5434 the display in *ROOT_X, and *ROOT_Y. */
5436 static void
5437 compute_tip_xy (struct frame *f,
5438 Lisp_Object parms, Lisp_Object dx, Lisp_Object dy,
5439 int width, int height, int *root_x, int *root_y)
5441 Lisp_Object left, top;
5442 int min_x, min_y, max_x, max_y;
5444 /* User-specified position? */
5445 left = Fcdr (Fassq (Qleft, parms));
5446 top = Fcdr (Fassq (Qtop, parms));
5448 /* Move the tooltip window where the mouse pointer is. Resize and
5449 show it. */
5450 if (!INTEGERP (left) || !INTEGERP (top))
5452 POINT pt;
5454 /* Default min and max values. */
5455 min_x = 0;
5456 min_y = 0;
5457 max_x = x_display_pixel_width (FRAME_W32_DISPLAY_INFO (f));
5458 max_y = x_display_pixel_height (FRAME_W32_DISPLAY_INFO (f));
5460 block_input ();
5461 GetCursorPos (&pt);
5462 *root_x = pt.x;
5463 *root_y = pt.y;
5464 unblock_input ();
5466 /* If multiple monitor support is available, constrain the tip onto
5467 the current monitor. This improves the above by allowing negative
5468 co-ordinates if monitor positions are such that they are valid, and
5469 snaps a tooltip onto a single monitor if we are close to the edge
5470 where it would otherwise flow onto the other monitor (or into
5471 nothingness if there is a gap in the overlap). */
5472 if (monitor_from_point_fn && get_monitor_info_fn)
5474 struct MONITOR_INFO info;
5475 HMONITOR monitor
5476 = monitor_from_point_fn (pt, MONITOR_DEFAULT_TO_NEAREST);
5477 info.cbSize = sizeof (info);
5479 if (get_monitor_info_fn (monitor, &info))
5481 min_x = info.rcWork.left;
5482 min_y = info.rcWork.top;
5483 max_x = info.rcWork.right;
5484 max_y = info.rcWork.bottom;
5489 if (INTEGERP (top))
5490 *root_y = XINT (top);
5491 else if (*root_y + XINT (dy) <= min_y)
5492 *root_y = min_y; /* Can happen for negative dy */
5493 else if (*root_y + XINT (dy) + height <= max_y)
5494 /* It fits below the pointer */
5495 *root_y += XINT (dy);
5496 else if (height + XINT (dy) + min_y <= *root_y)
5497 /* It fits above the pointer. */
5498 *root_y -= height + XINT (dy);
5499 else
5500 /* Put it on the top. */
5501 *root_y = min_y;
5503 if (INTEGERP (left))
5504 *root_x = XINT (left);
5505 else if (*root_x + XINT (dx) <= min_x)
5506 *root_x = 0; /* Can happen for negative dx */
5507 else if (*root_x + XINT (dx) + width <= max_x)
5508 /* It fits to the right of the pointer. */
5509 *root_x += XINT (dx);
5510 else if (width + XINT (dx) + min_x <= *root_x)
5511 /* It fits to the left of the pointer. */
5512 *root_x -= width + XINT (dx);
5513 else
5514 /* Put it left justified on the screen -- it ought to fit that way. */
5515 *root_x = min_x;
5519 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
5520 doc: /* Show STRING in a \"tooltip\" window on frame FRAME.
5521 A tooltip window is a small window displaying a string.
5523 This is an internal function; Lisp code should call `tooltip-show'.
5525 FRAME nil or omitted means use the selected frame.
5527 PARMS is an optional list of frame parameters which can be
5528 used to change the tooltip's appearance.
5530 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
5531 means use the default timeout of 5 seconds.
5533 If the list of frame parameters PARMS contains a `left' parameter,
5534 the tooltip is displayed at that x-position. Otherwise it is
5535 displayed at the mouse position, with offset DX added (default is 5 if
5536 DX isn't specified). Likewise for the y-position; if a `top' frame
5537 parameter is specified, it determines the y-position of the tooltip
5538 window, otherwise it is displayed at the mouse position, with offset
5539 DY added (default is -10).
5541 A tooltip's maximum size is specified by `x-max-tooltip-size'.
5542 Text larger than the specified size is clipped. */)
5543 (Lisp_Object string, Lisp_Object frame, Lisp_Object parms, Lisp_Object timeout, Lisp_Object dx, Lisp_Object dy)
5545 struct frame *f;
5546 struct window *w;
5547 int root_x, root_y;
5548 struct buffer *old_buffer;
5549 struct text_pos pos;
5550 int i, width, height, seen_reversed_p;
5551 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5552 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5553 ptrdiff_t count = SPECPDL_INDEX ();
5555 specbind (Qinhibit_redisplay, Qt);
5557 GCPRO4 (string, parms, frame, timeout);
5559 CHECK_STRING (string);
5560 f = check_x_frame (frame);
5561 if (NILP (timeout))
5562 timeout = make_number (5);
5563 else
5564 CHECK_NATNUM (timeout);
5566 if (NILP (dx))
5567 dx = make_number (5);
5568 else
5569 CHECK_NUMBER (dx);
5571 if (NILP (dy))
5572 dy = make_number (-10);
5573 else
5574 CHECK_NUMBER (dy);
5576 if (NILP (last_show_tip_args))
5577 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
5579 if (!NILP (tip_frame))
5581 Lisp_Object last_string = AREF (last_show_tip_args, 0);
5582 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
5583 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
5585 if (EQ (frame, last_frame)
5586 && !NILP (Fequal (last_string, string))
5587 && !NILP (Fequal (last_parms, parms)))
5589 struct frame *f = XFRAME (tip_frame);
5591 /* Only DX and DY have changed. */
5592 if (!NILP (tip_timer))
5594 Lisp_Object timer = tip_timer;
5595 tip_timer = Qnil;
5596 call1 (Qcancel_timer, timer);
5599 block_input ();
5600 compute_tip_xy (f, parms, dx, dy, FRAME_PIXEL_WIDTH (f),
5601 FRAME_PIXEL_HEIGHT (f), &root_x, &root_y);
5603 /* Put tooltip in topmost group and in position. */
5604 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
5605 root_x, root_y, 0, 0,
5606 SWP_NOSIZE | SWP_NOACTIVATE);
5608 /* Ensure tooltip is on top of other topmost windows (eg menus). */
5609 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
5610 0, 0, 0, 0,
5611 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
5613 unblock_input ();
5614 goto start_timer;
5618 /* Hide a previous tip, if any. */
5619 Fx_hide_tip ();
5621 ASET (last_show_tip_args, 0, string);
5622 ASET (last_show_tip_args, 1, frame);
5623 ASET (last_show_tip_args, 2, parms);
5625 /* Add default values to frame parameters. */
5626 if (NILP (Fassq (Qname, parms)))
5627 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
5628 if (NILP (Fassq (Qinternal_border_width, parms)))
5629 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
5630 if (NILP (Fassq (Qborder_width, parms)))
5631 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
5632 if (NILP (Fassq (Qborder_color, parms)))
5633 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
5634 if (NILP (Fassq (Qbackground_color, parms)))
5635 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
5636 parms);
5638 /* Block input until the tip has been fully drawn, to avoid crashes
5639 when drawing tips in menus. */
5640 block_input ();
5642 /* Create a frame for the tooltip, and record it in the global
5643 variable tip_frame. */
5644 frame = x_create_tip_frame (FRAME_W32_DISPLAY_INFO (f), parms, string);
5645 f = XFRAME (frame);
5647 /* Set up the frame's root window. */
5648 w = XWINDOW (FRAME_ROOT_WINDOW (f));
5649 wset_left_col (w, make_number (0));
5650 wset_top_line (w, make_number (0));
5652 if (CONSP (Vx_max_tooltip_size)
5653 && INTEGERP (XCAR (Vx_max_tooltip_size))
5654 && XINT (XCAR (Vx_max_tooltip_size)) > 0
5655 && INTEGERP (XCDR (Vx_max_tooltip_size))
5656 && XINT (XCDR (Vx_max_tooltip_size)) > 0)
5658 wset_total_cols (w, XCAR (Vx_max_tooltip_size));
5659 wset_total_lines (w, XCDR (Vx_max_tooltip_size));
5661 else
5663 wset_total_cols (w, make_number (80));
5664 wset_total_lines (w, make_number (40));
5667 FRAME_TOTAL_COLS (f) = XINT (w->total_cols);
5668 adjust_glyphs (f);
5669 w->pseudo_window_p = 1;
5671 /* Display the tooltip text in a temporary buffer. */
5672 old_buffer = current_buffer;
5673 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->buffer));
5674 bset_truncate_lines (current_buffer, Qnil);
5675 clear_glyph_matrix (w->desired_matrix);
5676 clear_glyph_matrix (w->current_matrix);
5677 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
5678 try_window (FRAME_ROOT_WINDOW (f), pos, TRY_WINDOW_IGNORE_FONTS_CHANGE);
5680 /* Compute width and height of the tooltip. */
5681 width = height = seen_reversed_p = 0;
5682 for (i = 0; i < w->desired_matrix->nrows; ++i)
5684 struct glyph_row *row = &w->desired_matrix->rows[i];
5685 struct glyph *last;
5686 int row_width;
5688 /* Stop at the first empty row at the end. */
5689 if (!row->enabled_p || !row->displays_text_p)
5690 break;
5692 /* Let the row go over the full width of the frame. */
5693 row->full_width_p = 1;
5695 row_width = row->pixel_width;
5696 if (row->used[TEXT_AREA])
5698 if (!row->reversed_p)
5700 /* There's a glyph at the end of rows that is used to
5701 place the cursor there. Don't include the width of
5702 this glyph. */
5703 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5704 if (INTEGERP (last->object))
5705 row_width -= last->pixel_width;
5707 else
5709 /* There could be a stretch glyph at the beginning of R2L
5710 rows that is produced by extend_face_to_end_of_line.
5711 Don't count that glyph. */
5712 struct glyph *g = row->glyphs[TEXT_AREA];
5714 if (g->type == STRETCH_GLYPH && INTEGERP (g->object))
5716 row_width -= g->pixel_width;
5717 seen_reversed_p = 1;
5722 height += row->height;
5723 width = max (width, row_width);
5726 /* If we've seen partial-length R2L rows, we need to re-adjust the
5727 tool-tip frame width and redisplay it again, to avoid over-wide
5728 tips due to the stretch glyph that extends R2L lines to full
5729 width of the frame. */
5730 if (seen_reversed_p)
5732 /* w->total_cols and FRAME_TOTAL_COLS want the width in columns,
5733 not in pixels. */
5734 width /= WINDOW_FRAME_COLUMN_WIDTH (w);
5735 wset_total_cols (w, make_number (width));
5736 FRAME_TOTAL_COLS (f) = width;
5737 adjust_glyphs (f);
5738 w->pseudo_window_p = 1;
5739 clear_glyph_matrix (w->desired_matrix);
5740 clear_glyph_matrix (w->current_matrix);
5741 try_window (FRAME_ROOT_WINDOW (f), pos, TRY_WINDOW_IGNORE_FONTS_CHANGE);
5742 width = height = 0;
5743 /* Recompute width and height of the tooltip. */
5744 for (i = 0; i < w->desired_matrix->nrows; ++i)
5746 struct glyph_row *row = &w->desired_matrix->rows[i];
5747 struct glyph *last;
5748 int row_width;
5750 if (!row->enabled_p || !row->displays_text_p)
5751 break;
5752 row->full_width_p = 1;
5753 row_width = row->pixel_width;
5754 if (row->used[TEXT_AREA] && !row->reversed_p)
5756 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5757 if (INTEGERP (last->object))
5758 row_width -= last->pixel_width;
5761 height += row->height;
5762 width = max (width, row_width);
5766 /* Round up the height to an integral multiple of FRAME_LINE_HEIGHT. */
5767 if (height % FRAME_LINE_HEIGHT (f) != 0)
5768 height += FRAME_LINE_HEIGHT (f) - height % FRAME_LINE_HEIGHT (f);
5769 /* Add the frame's internal border to the width and height the w32
5770 window should have. */
5771 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5772 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5774 /* Move the tooltip window where the mouse pointer is. Resize and
5775 show it. */
5776 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5779 /* Adjust Window size to take border into account. */
5780 RECT rect;
5781 rect.left = rect.top = 0;
5782 rect.right = width;
5783 rect.bottom = height;
5784 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
5785 FRAME_EXTERNAL_MENU_BAR (f));
5787 /* Position and size tooltip, and put it in the topmost group.
5788 The add-on of FRAME_COLUMN_WIDTH to the 5th argument is a
5789 peculiarity of w32 display: without it, some fonts cause the
5790 last character of the tip to be truncated or wrapped around to
5791 the next line. */
5792 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
5793 root_x, root_y,
5794 rect.right - rect.left + FRAME_COLUMN_WIDTH (f),
5795 rect.bottom - rect.top, SWP_NOACTIVATE);
5797 /* Ensure tooltip is on top of other topmost windows (eg menus). */
5798 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
5799 0, 0, 0, 0,
5800 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
5802 /* Let redisplay know that we have made the frame visible already. */
5803 f->async_visible = 1;
5805 ShowWindow (FRAME_W32_WINDOW (f), SW_SHOWNOACTIVATE);
5808 /* Draw into the window. */
5809 w->must_be_updated_p = 1;
5810 update_single_window (w, 1);
5812 unblock_input ();
5814 /* Restore original current buffer. */
5815 set_buffer_internal_1 (old_buffer);
5816 windows_or_buffers_changed = old_windows_or_buffers_changed;
5818 start_timer:
5819 /* Let the tip disappear after timeout seconds. */
5820 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
5821 intern ("x-hide-tip"));
5823 UNGCPRO;
5824 return unbind_to (count, Qnil);
5828 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
5829 doc: /* Hide the current tooltip window, if there is any.
5830 Value is t if tooltip was open, nil otherwise. */)
5831 (void)
5833 ptrdiff_t count;
5834 Lisp_Object deleted, frame, timer;
5835 struct gcpro gcpro1, gcpro2;
5837 /* Return quickly if nothing to do. */
5838 if (NILP (tip_timer) && NILP (tip_frame))
5839 return Qnil;
5841 frame = tip_frame;
5842 timer = tip_timer;
5843 GCPRO2 (frame, timer);
5844 tip_frame = tip_timer = deleted = Qnil;
5846 count = SPECPDL_INDEX ();
5847 specbind (Qinhibit_redisplay, Qt);
5848 specbind (Qinhibit_quit, Qt);
5850 if (!NILP (timer))
5851 call1 (Qcancel_timer, timer);
5853 if (FRAMEP (frame))
5855 delete_frame (frame, Qnil);
5856 deleted = Qt;
5859 UNGCPRO;
5860 return unbind_to (count, deleted);
5863 /***********************************************************************
5864 File selection dialog
5865 ***********************************************************************/
5867 /* Callback for altering the behavior of the Open File dialog.
5868 Makes the Filename text field contain "Current Directory" and be
5869 read-only when "Directories" is selected in the filter. This
5870 allows us to work around the fact that the standard Open File
5871 dialog does not support directories. */
5872 static UINT CALLBACK
5873 file_dialog_callback (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
5875 if (msg == WM_NOTIFY)
5877 OFNOTIFY * notify = (OFNOTIFY *)lParam;
5878 /* Detect when the Filter dropdown is changed. */
5879 if (notify->hdr.code == CDN_TYPECHANGE
5880 || notify->hdr.code == CDN_INITDONE)
5882 HWND dialog = GetParent (hwnd);
5883 HWND edit_control = GetDlgItem (dialog, FILE_NAME_TEXT_FIELD);
5884 HWND list = GetDlgItem (dialog, FILE_NAME_LIST);
5886 /* At least on Windows 7, the above attempt to get the window handle
5887 to the File Name Text Field fails. The following code does the
5888 job though. Note that this code is based on my examination of the
5889 window hierarchy using Microsoft Spy++. bk */
5890 if (edit_control == NULL)
5892 HWND tmp = GetDlgItem (dialog, FILE_NAME_COMBO_BOX);
5893 if (tmp)
5895 tmp = GetWindow (tmp, GW_CHILD);
5896 if (tmp)
5897 edit_control = GetWindow (tmp, GW_CHILD);
5901 /* Directories is in index 2. */
5902 if (notify->lpOFN->nFilterIndex == 2)
5904 CommDlg_OpenSave_SetControlText (dialog, FILE_NAME_TEXT_FIELD,
5905 "Current Directory");
5906 EnableWindow (edit_control, FALSE);
5907 /* Note that at least on Windows 7, the above call to EnableWindow
5908 disables the window that would ordinarily have focus. If we
5909 do not set focus to some other window here, focus will land in
5910 no man's land and the user will be unable to tab through the
5911 dialog box (pressing tab will only result in a beep).
5912 Avoid that problem by setting focus to the list here. */
5913 if (notify->hdr.code == CDN_INITDONE)
5914 SetFocus (list);
5916 else
5918 /* Don't override default filename on init done. */
5919 if (notify->hdr.code == CDN_TYPECHANGE)
5920 CommDlg_OpenSave_SetControlText (dialog,
5921 FILE_NAME_TEXT_FIELD, "");
5922 EnableWindow (edit_control, TRUE);
5926 return 0;
5929 /* Since we compile with _WIN32_WINNT set to 0x0400 (for NT4 compatibility)
5930 we end up with the old file dialogs. Define a big enough struct for the
5931 new dialog to trick GetOpenFileName into giving us the new dialogs on
5932 Windows 2000 and XP. */
5933 typedef struct
5935 OPENFILENAME real_details;
5936 void * pReserved;
5937 DWORD dwReserved;
5938 DWORD FlagsEx;
5939 } NEWOPENFILENAME;
5942 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5943 doc: /* Read file name, prompting with PROMPT in directory DIR.
5944 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5945 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5946 or directory must exist.
5948 This function is only defined on MS Windows, and X Windows with the
5949 Motif or Gtk toolkits. With the Motif toolkit, ONLY-DIR-P is ignored.
5950 Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories. */)
5951 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename, Lisp_Object mustmatch, Lisp_Object only_dir_p)
5953 struct frame *f = SELECTED_FRAME ();
5954 Lisp_Object file = Qnil;
5955 ptrdiff_t count = SPECPDL_INDEX ();
5956 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5957 char filename[MAX_PATH + 1];
5958 char init_dir[MAX_PATH + 1];
5959 int default_filter_index = 1; /* 1: All Files, 2: Directories only */
5961 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5962 CHECK_STRING (prompt);
5963 CHECK_STRING (dir);
5965 /* Create the dialog with PROMPT as title, using DIR as initial
5966 directory and using "*" as pattern. */
5967 dir = Fexpand_file_name (dir, Qnil);
5968 strncpy (init_dir, SDATA (ENCODE_FILE (dir)), MAX_PATH);
5969 init_dir[MAX_PATH] = '\0';
5970 unixtodos_filename (init_dir);
5972 if (STRINGP (default_filename))
5974 char *file_name_only;
5975 char *full_path_name = SDATA (ENCODE_FILE (default_filename));
5977 unixtodos_filename (full_path_name);
5979 file_name_only = strrchr (full_path_name, '\\');
5980 if (!file_name_only)
5981 file_name_only = full_path_name;
5982 else
5983 file_name_only++;
5985 strncpy (filename, file_name_only, MAX_PATH);
5986 filename[MAX_PATH] = '\0';
5988 else
5989 filename[0] = '\0';
5991 /* The code in file_dialog_callback that attempts to set the text
5992 of the file name edit window when handling the CDN_INITDONE
5993 WM_NOTIFY message does not work. Setting filename to "Current
5994 Directory" in the only_dir_p case here does work however. */
5995 if (filename[0] == 0 && ! NILP (only_dir_p))
5996 strcpy (filename, "Current Directory");
5999 NEWOPENFILENAME new_file_details;
6000 BOOL file_opened = FALSE;
6001 OPENFILENAME * file_details = &new_file_details.real_details;
6003 /* Prevent redisplay. */
6004 specbind (Qinhibit_redisplay, Qt);
6005 block_input ();
6007 memset (&new_file_details, 0, sizeof (new_file_details));
6008 /* Apparently NT4 crashes if you give it an unexpected size.
6009 I'm not sure about Windows 9x, so play it safe. */
6010 if (w32_major_version > 4 && w32_major_version < 95)
6011 file_details->lStructSize = sizeof (NEWOPENFILENAME);
6012 else
6013 file_details->lStructSize = sizeof (OPENFILENAME);
6015 file_details->hwndOwner = FRAME_W32_WINDOW (f);
6016 /* Undocumented Bug in Common File Dialog:
6017 If a filter is not specified, shell links are not resolved. */
6018 file_details->lpstrFilter = "All Files (*.*)\0*.*\0Directories\0*|*\0\0";
6019 file_details->lpstrFile = filename;
6020 file_details->nMaxFile = sizeof (filename);
6021 file_details->lpstrInitialDir = init_dir;
6022 file_details->lpstrTitle = SDATA (prompt);
6024 if (! NILP (only_dir_p))
6025 default_filter_index = 2;
6027 file_details->nFilterIndex = default_filter_index;
6029 file_details->Flags = (OFN_HIDEREADONLY | OFN_NOCHANGEDIR
6030 | OFN_EXPLORER | OFN_ENABLEHOOK);
6031 if (!NILP (mustmatch))
6033 /* Require that the path to the parent directory exists. */
6034 file_details->Flags |= OFN_PATHMUSTEXIST;
6035 /* If we are looking for a file, require that it exists. */
6036 if (NILP (only_dir_p))
6037 file_details->Flags |= OFN_FILEMUSTEXIST;
6040 file_details->lpfnHook = (LPOFNHOOKPROC) file_dialog_callback;
6042 file_opened = GetOpenFileName (file_details);
6044 unblock_input ();
6046 if (file_opened)
6048 dostounix_filename (filename);
6050 if (file_details->nFilterIndex == 2)
6052 /* "Directories" selected - strip dummy file name. */
6053 char * last = strrchr (filename, '/');
6054 *last = '\0';
6057 file = DECODE_FILE (build_string (filename));
6059 /* User canceled the dialog without making a selection. */
6060 else if (!CommDlgExtendedError ())
6061 file = Qnil;
6062 /* An error occurred, fallback on reading from the mini-buffer. */
6063 else
6064 file = Fcompleting_read (prompt, intern ("read-file-name-internal"),
6065 dir, mustmatch, dir, Qfile_name_history,
6066 default_filename, Qnil);
6068 file = unbind_to (count, file);
6071 UNGCPRO;
6073 /* Make "Cancel" equivalent to C-g. */
6074 if (NILP (file))
6075 Fsignal (Qquit, Qnil);
6077 return unbind_to (count, file);
6081 /* Moving files to the system recycle bin.
6082 Used by `move-file-to-trash' instead of the default moving to ~/.Trash */
6083 DEFUN ("system-move-file-to-trash", Fsystem_move_file_to_trash,
6084 Ssystem_move_file_to_trash, 1, 1, 0,
6085 doc: /* Move file or directory named FILENAME to the recycle bin. */)
6086 (Lisp_Object filename)
6088 Lisp_Object handler;
6089 Lisp_Object encoded_file;
6090 Lisp_Object operation;
6092 operation = Qdelete_file;
6093 if (!NILP (Ffile_directory_p (filename))
6094 && NILP (Ffile_symlink_p (filename)))
6096 operation = intern ("delete-directory");
6097 filename = Fdirectory_file_name (filename);
6099 filename = Fexpand_file_name (filename, Qnil);
6101 handler = Ffind_file_name_handler (filename, operation);
6102 if (!NILP (handler))
6103 return call2 (handler, operation, filename);
6105 encoded_file = ENCODE_FILE (filename);
6108 const char * path;
6109 SHFILEOPSTRUCT file_op;
6110 char tmp_path[MAX_PATH + 1];
6112 path = map_w32_filename (SDATA (encoded_file), NULL);
6114 /* On Windows, write permission is required to delete/move files. */
6115 _chmod (path, 0666);
6117 memset (tmp_path, 0, sizeof (tmp_path));
6118 strcpy (tmp_path, path);
6120 memset (&file_op, 0, sizeof (file_op));
6121 file_op.hwnd = HWND_DESKTOP;
6122 file_op.wFunc = FO_DELETE;
6123 file_op.pFrom = tmp_path;
6124 file_op.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_ALLOWUNDO
6125 | FOF_NOERRORUI | FOF_NO_CONNECTED_ELEMENTS;
6126 file_op.fAnyOperationsAborted = FALSE;
6128 if (SHFileOperation (&file_op) != 0)
6129 report_file_error ("Removing old name", list1 (filename));
6131 return Qnil;
6135 /***********************************************************************
6136 w32 specialized functions
6137 ***********************************************************************/
6139 DEFUN ("w32-send-sys-command", Fw32_send_sys_command,
6140 Sw32_send_sys_command, 1, 2, 0,
6141 doc: /* Send frame a Windows WM_SYSCOMMAND message of type COMMAND.
6142 Some useful values for COMMAND are #xf030 to maximize frame (#xf020
6143 to minimize), #xf120 to restore frame to original size, and #xf100
6144 to activate the menubar for keyboard access. #xf140 activates the
6145 screen saver if defined.
6147 If optional parameter FRAME is not specified, use selected frame. */)
6148 (Lisp_Object command, Lisp_Object frame)
6150 FRAME_PTR f = check_x_frame (frame);
6152 CHECK_NUMBER (command);
6154 PostMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, XINT (command), 0);
6156 return Qnil;
6159 DEFUN ("w32-shell-execute", Fw32_shell_execute, Sw32_shell_execute, 2, 4, 0,
6160 doc: /* Get Windows to perform OPERATION on DOCUMENT.
6161 This is a wrapper around the ShellExecute system function, which
6162 invokes the application registered to handle OPERATION for DOCUMENT.
6164 OPERATION is either nil or a string that names a supported operation.
6165 What operations can be used depends on the particular DOCUMENT and its
6166 handler application, but typically it is one of the following common
6167 operations:
6169 \"open\" - open DOCUMENT, which could be a file, a directory, or an
6170 executable program. If it is an application, that
6171 application is launched in the current buffer's default
6172 directory. Otherwise, the application associated with
6173 DOCUMENT is launched in the buffer's default directory.
6174 \"print\" - print DOCUMENT, which must be a file
6175 \"explore\" - start the Windows Explorer on DOCUMENT
6176 \"edit\" - launch an editor and open DOCUMENT for editing; which
6177 editor is launched depends on the association for the
6178 specified DOCUMENT
6179 \"find\" - initiate search starting from DOCUMENT which must specify
6180 a directory
6181 nil - invoke the default OPERATION, or \"open\" if default is
6182 not defined or unavailable
6184 DOCUMENT is typically the name of a document file or a URL, but can
6185 also be a program executable to run, or a directory to open in the
6186 Windows Explorer.
6188 If DOCUMENT is a program executable, the optional third arg PARAMETERS
6189 can be a string containing command line parameters that will be passed
6190 to the program; otherwise, PARAMETERS should be nil or unspecified.
6192 Optional fourth argument SHOW-FLAG can be used to control how the
6193 application will be displayed when it is invoked. If SHOW-FLAG is nil
6194 or unspecified, the application is displayed normally, otherwise it is
6195 an integer representing a ShowWindow flag:
6197 0 - start hidden
6198 1 - start normally
6199 3 - start maximized
6200 6 - start minimized */)
6201 (Lisp_Object operation, Lisp_Object document, Lisp_Object parameters, Lisp_Object show_flag)
6203 Lisp_Object current_dir;
6204 char *errstr;
6206 CHECK_STRING (document);
6208 /* Encode filename, current directory and parameters. */
6209 current_dir = ENCODE_FILE (BVAR (current_buffer, directory));
6210 document = ENCODE_FILE (document);
6211 if (STRINGP (parameters))
6212 parameters = ENCODE_SYSTEM (parameters);
6214 if ((int) ShellExecute (NULL,
6215 (STRINGP (operation) ?
6216 SDATA (operation) : NULL),
6217 SDATA (document),
6218 (STRINGP (parameters) ?
6219 SDATA (parameters) : NULL),
6220 SDATA (current_dir),
6221 (INTEGERP (show_flag) ?
6222 XINT (show_flag) : SW_SHOWDEFAULT))
6223 > 32)
6224 return Qt;
6225 errstr = w32_strerror (0);
6226 /* The error string might be encoded in the locale's encoding. */
6227 if (!NILP (Vlocale_coding_system))
6229 Lisp_Object decoded =
6230 code_convert_string_norecord (build_unibyte_string (errstr),
6231 Vlocale_coding_system, 0);
6232 errstr = SSDATA (decoded);
6234 error ("ShellExecute failed: %s", errstr);
6237 /* Lookup virtual keycode from string representing the name of a
6238 non-ascii keystroke into the corresponding virtual key, using
6239 lispy_function_keys. */
6240 static int
6241 lookup_vk_code (char *key)
6243 int i;
6245 for (i = 0; i < 256; i++)
6246 if (lispy_function_keys[i]
6247 && strcmp (lispy_function_keys[i], key) == 0)
6248 return i;
6250 return -1;
6253 /* Convert a one-element vector style key sequence to a hot key
6254 definition. */
6255 static Lisp_Object
6256 w32_parse_hot_key (Lisp_Object key)
6258 /* Copied from Fdefine_key and store_in_keymap. */
6259 register Lisp_Object c;
6260 int vk_code;
6261 int lisp_modifiers;
6262 int w32_modifiers;
6263 struct gcpro gcpro1;
6265 CHECK_VECTOR (key);
6267 if (XFASTINT (Flength (key)) != 1)
6268 return Qnil;
6270 GCPRO1 (key);
6272 c = Faref (key, make_number (0));
6274 if (CONSP (c) && lucid_event_type_list_p (c))
6275 c = Fevent_convert_list (c);
6277 UNGCPRO;
6279 if (! INTEGERP (c) && ! SYMBOLP (c))
6280 error ("Key definition is invalid");
6282 /* Work out the base key and the modifiers. */
6283 if (SYMBOLP (c))
6285 c = parse_modifiers (c);
6286 lisp_modifiers = XINT (Fcar (Fcdr (c)));
6287 c = Fcar (c);
6288 if (!SYMBOLP (c))
6289 emacs_abort ();
6290 vk_code = lookup_vk_code (SDATA (SYMBOL_NAME (c)));
6292 else if (INTEGERP (c))
6294 lisp_modifiers = XINT (c) & ~CHARACTERBITS;
6295 /* Many ascii characters are their own virtual key code. */
6296 vk_code = XINT (c) & CHARACTERBITS;
6299 if (vk_code < 0 || vk_code > 255)
6300 return Qnil;
6302 if ((lisp_modifiers & meta_modifier) != 0
6303 && !NILP (Vw32_alt_is_meta))
6304 lisp_modifiers |= alt_modifier;
6306 /* Supply defs missing from mingw32. */
6307 #ifndef MOD_ALT
6308 #define MOD_ALT 0x0001
6309 #define MOD_CONTROL 0x0002
6310 #define MOD_SHIFT 0x0004
6311 #define MOD_WIN 0x0008
6312 #endif
6314 /* Convert lisp modifiers to Windows hot-key form. */
6315 w32_modifiers = (lisp_modifiers & hyper_modifier) ? MOD_WIN : 0;
6316 w32_modifiers |= (lisp_modifiers & alt_modifier) ? MOD_ALT : 0;
6317 w32_modifiers |= (lisp_modifiers & ctrl_modifier) ? MOD_CONTROL : 0;
6318 w32_modifiers |= (lisp_modifiers & shift_modifier) ? MOD_SHIFT : 0;
6320 return HOTKEY (vk_code, w32_modifiers);
6323 DEFUN ("w32-register-hot-key", Fw32_register_hot_key,
6324 Sw32_register_hot_key, 1, 1, 0,
6325 doc: /* Register KEY as a hot-key combination.
6326 Certain key combinations like Alt-Tab are reserved for system use on
6327 Windows, and therefore are normally intercepted by the system. However,
6328 most of these key combinations can be received by registering them as
6329 hot-keys, overriding their special meaning.
6331 KEY must be a one element key definition in vector form that would be
6332 acceptable to `define-key' (e.g. [A-tab] for Alt-Tab). The meta
6333 modifier is interpreted as Alt if `w32-alt-is-meta' is t, and hyper
6334 is always interpreted as the Windows modifier keys.
6336 The return value is the hotkey-id if registered, otherwise nil. */)
6337 (Lisp_Object key)
6339 key = w32_parse_hot_key (key);
6341 if (!NILP (key) && NILP (Fmemq (key, w32_grabbed_keys)))
6343 /* Reuse an empty slot if possible. */
6344 Lisp_Object item = Fmemq (Qnil, w32_grabbed_keys);
6346 /* Safe to add new key to list, even if we have focus. */
6347 if (NILP (item))
6348 w32_grabbed_keys = Fcons (key, w32_grabbed_keys);
6349 else
6350 XSETCAR (item, key);
6352 /* Notify input thread about new hot-key definition, so that it
6353 takes effect without needing to switch focus. */
6354 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
6355 (WPARAM) XLI (key), 0);
6358 return key;
6361 DEFUN ("w32-unregister-hot-key", Fw32_unregister_hot_key,
6362 Sw32_unregister_hot_key, 1, 1, 0,
6363 doc: /* Unregister KEY as a hot-key combination. */)
6364 (Lisp_Object key)
6366 Lisp_Object item;
6368 if (!INTEGERP (key))
6369 key = w32_parse_hot_key (key);
6371 item = Fmemq (key, w32_grabbed_keys);
6373 if (!NILP (item))
6375 /* Notify input thread about hot-key definition being removed, so
6376 that it takes effect without needing focus switch. */
6377 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
6378 (WPARAM) XINT (XCAR (item)), (LPARAM) XLI (item)))
6380 MSG msg;
6381 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
6383 return Qt;
6385 return Qnil;
6388 DEFUN ("w32-registered-hot-keys", Fw32_registered_hot_keys,
6389 Sw32_registered_hot_keys, 0, 0, 0,
6390 doc: /* Return list of registered hot-key IDs. */)
6391 (void)
6393 return Fdelq (Qnil, Fcopy_sequence (w32_grabbed_keys));
6396 DEFUN ("w32-reconstruct-hot-key", Fw32_reconstruct_hot_key,
6397 Sw32_reconstruct_hot_key, 1, 1, 0,
6398 doc: /* Convert hot-key ID to a lisp key combination.
6399 usage: (w32-reconstruct-hot-key ID) */)
6400 (Lisp_Object hotkeyid)
6402 int vk_code, w32_modifiers;
6403 Lisp_Object key;
6405 CHECK_NUMBER (hotkeyid);
6407 vk_code = HOTKEY_VK_CODE (hotkeyid);
6408 w32_modifiers = HOTKEY_MODIFIERS (hotkeyid);
6410 if (vk_code < 256 && lispy_function_keys[vk_code])
6411 key = intern (lispy_function_keys[vk_code]);
6412 else
6413 key = make_number (vk_code);
6415 key = Fcons (key, Qnil);
6416 if (w32_modifiers & MOD_SHIFT)
6417 key = Fcons (Qshift, key);
6418 if (w32_modifiers & MOD_CONTROL)
6419 key = Fcons (Qctrl, key);
6420 if (w32_modifiers & MOD_ALT)
6421 key = Fcons (NILP (Vw32_alt_is_meta) ? Qalt : Qmeta, key);
6422 if (w32_modifiers & MOD_WIN)
6423 key = Fcons (Qhyper, key);
6425 return key;
6428 DEFUN ("w32-toggle-lock-key", Fw32_toggle_lock_key,
6429 Sw32_toggle_lock_key, 1, 2, 0,
6430 doc: /* Toggle the state of the lock key KEY.
6431 KEY can be `capslock', `kp-numlock', or `scroll'.
6432 If the optional parameter NEW-STATE is a number, then the state of KEY
6433 is set to off if the low bit of NEW-STATE is zero, otherwise on. */)
6434 (Lisp_Object key, Lisp_Object new_state)
6436 int vk_code;
6438 if (EQ (key, intern ("capslock")))
6439 vk_code = VK_CAPITAL;
6440 else if (EQ (key, intern ("kp-numlock")))
6441 vk_code = VK_NUMLOCK;
6442 else if (EQ (key, intern ("scroll")))
6443 vk_code = VK_SCROLL;
6444 else
6445 return Qnil;
6447 if (!dwWindowsThreadId)
6448 return make_number (w32_console_toggle_lock_key (vk_code, new_state));
6450 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
6451 (WPARAM) vk_code, (LPARAM) XLI (new_state)))
6453 MSG msg;
6454 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
6455 return make_number (msg.wParam);
6457 return Qnil;
6460 DEFUN ("w32-window-exists-p", Fw32_window_exists_p, Sw32_window_exists_p,
6461 2, 2, 0,
6462 doc: /* Return non-nil if a window exists with the specified CLASS and NAME.
6464 This is a direct interface to the Windows API FindWindow function. */)
6465 (Lisp_Object class, Lisp_Object name)
6467 HWND hnd;
6469 if (!NILP (class))
6470 CHECK_STRING (class);
6471 if (!NILP (name))
6472 CHECK_STRING (name);
6474 hnd = FindWindow (STRINGP (class) ? ((LPCTSTR) SDATA (class)) : NULL,
6475 STRINGP (name) ? ((LPCTSTR) SDATA (name)) : NULL);
6476 if (!hnd)
6477 return Qnil;
6478 return Qt;
6481 DEFUN ("w32-battery-status", Fw32_battery_status, Sw32_battery_status, 0, 0, 0,
6482 doc: /* Get power status information from Windows system.
6484 The following %-sequences are provided:
6485 %L AC line status (verbose)
6486 %B Battery status (verbose)
6487 %b Battery status, empty means high, `-' means low,
6488 `!' means critical, and `+' means charging
6489 %p Battery load percentage
6490 %s Remaining time (to charge or discharge) in seconds
6491 %m Remaining time (to charge or discharge) in minutes
6492 %h Remaining time (to charge or discharge) in hours
6493 %t Remaining time (to charge or discharge) in the form `h:min' */)
6494 (void)
6496 Lisp_Object status = Qnil;
6498 SYSTEM_POWER_STATUS system_status;
6499 if (GetSystemPowerStatus (&system_status))
6501 Lisp_Object line_status, battery_status, battery_status_symbol;
6502 Lisp_Object load_percentage, seconds, minutes, hours, remain;
6504 long seconds_left = (long) system_status.BatteryLifeTime;
6506 if (system_status.ACLineStatus == 0)
6507 line_status = build_string ("off-line");
6508 else if (system_status.ACLineStatus == 1)
6509 line_status = build_string ("on-line");
6510 else
6511 line_status = build_string ("N/A");
6513 if (system_status.BatteryFlag & 128)
6515 battery_status = build_string ("N/A");
6516 battery_status_symbol = empty_unibyte_string;
6518 else if (system_status.BatteryFlag & 8)
6520 battery_status = build_string ("charging");
6521 battery_status_symbol = build_string ("+");
6522 if (system_status.BatteryFullLifeTime != -1L)
6523 seconds_left = system_status.BatteryFullLifeTime - seconds_left;
6525 else if (system_status.BatteryFlag & 4)
6527 battery_status = build_string ("critical");
6528 battery_status_symbol = build_string ("!");
6530 else if (system_status.BatteryFlag & 2)
6532 battery_status = build_string ("low");
6533 battery_status_symbol = build_string ("-");
6535 else if (system_status.BatteryFlag & 1)
6537 battery_status = build_string ("high");
6538 battery_status_symbol = empty_unibyte_string;
6540 else
6542 battery_status = build_string ("medium");
6543 battery_status_symbol = empty_unibyte_string;
6546 if (system_status.BatteryLifePercent > 100)
6547 load_percentage = build_string ("N/A");
6548 else
6550 char buffer[16];
6551 _snprintf (buffer, 16, "%d", system_status.BatteryLifePercent);
6552 load_percentage = build_string (buffer);
6555 if (seconds_left < 0)
6556 seconds = minutes = hours = remain = build_string ("N/A");
6557 else
6559 long m;
6560 float h;
6561 char buffer[16];
6562 _snprintf (buffer, 16, "%ld", seconds_left);
6563 seconds = build_string (buffer);
6565 m = seconds_left / 60;
6566 _snprintf (buffer, 16, "%ld", m);
6567 minutes = build_string (buffer);
6569 h = seconds_left / 3600.0;
6570 _snprintf (buffer, 16, "%3.1f", h);
6571 hours = build_string (buffer);
6573 _snprintf (buffer, 16, "%ld:%02ld", m / 60, m % 60);
6574 remain = build_string (buffer);
6577 status = listn (CONSTYPE_HEAP, 8,
6578 Fcons (make_number ('L'), line_status),
6579 Fcons (make_number ('B'), battery_status),
6580 Fcons (make_number ('b'), battery_status_symbol),
6581 Fcons (make_number ('p'), load_percentage),
6582 Fcons (make_number ('s'), seconds),
6583 Fcons (make_number ('m'), minutes),
6584 Fcons (make_number ('h'), hours),
6585 Fcons (make_number ('t'), remain));
6587 return status;
6591 DEFUN ("file-system-info", Ffile_system_info, Sfile_system_info, 1, 1, 0,
6592 doc: /* Return storage information about the file system FILENAME is on.
6593 Value is a list of floats (TOTAL FREE AVAIL), where TOTAL is the total
6594 storage of the file system, FREE is the free storage, and AVAIL is the
6595 storage available to a non-superuser. All 3 numbers are in bytes.
6596 If the underlying system call fails, value is nil. */)
6597 (Lisp_Object filename)
6599 Lisp_Object encoded, value;
6601 CHECK_STRING (filename);
6602 filename = Fexpand_file_name (filename, Qnil);
6603 encoded = ENCODE_FILE (filename);
6605 value = Qnil;
6607 /* Determining the required information on Windows turns out, sadly,
6608 to be more involved than one would hope. The original Windows API
6609 call for this will return bogus information on some systems, but we
6610 must dynamically probe for the replacement api, since that was
6611 added rather late on. */
6613 HMODULE hKernel = GetModuleHandle ("kernel32");
6614 BOOL (*pfn_GetDiskFreeSpaceEx)
6615 (char *, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER)
6616 = (void *) GetProcAddress (hKernel, "GetDiskFreeSpaceEx");
6618 /* On Windows, we may need to specify the root directory of the
6619 volume holding FILENAME. */
6620 char rootname[MAX_PATH];
6621 char *name = SDATA (encoded);
6623 /* find the root name of the volume if given */
6624 if (isalpha (name[0]) && name[1] == ':')
6626 rootname[0] = name[0];
6627 rootname[1] = name[1];
6628 rootname[2] = '\\';
6629 rootname[3] = 0;
6631 else if (IS_DIRECTORY_SEP (name[0]) && IS_DIRECTORY_SEP (name[1]))
6633 char *str = rootname;
6634 int slashes = 4;
6637 if (IS_DIRECTORY_SEP (*name) && --slashes == 0)
6638 break;
6639 *str++ = *name++;
6641 while ( *name );
6643 *str++ = '\\';
6644 *str = 0;
6647 if (pfn_GetDiskFreeSpaceEx)
6649 /* Unsigned large integers cannot be cast to double, so
6650 use signed ones instead. */
6651 LARGE_INTEGER availbytes;
6652 LARGE_INTEGER freebytes;
6653 LARGE_INTEGER totalbytes;
6655 if (pfn_GetDiskFreeSpaceEx (rootname,
6656 (ULARGE_INTEGER *)&availbytes,
6657 (ULARGE_INTEGER *)&totalbytes,
6658 (ULARGE_INTEGER *)&freebytes))
6659 value = list3 (make_float ((double) totalbytes.QuadPart),
6660 make_float ((double) freebytes.QuadPart),
6661 make_float ((double) availbytes.QuadPart));
6663 else
6665 DWORD sectors_per_cluster;
6666 DWORD bytes_per_sector;
6667 DWORD free_clusters;
6668 DWORD total_clusters;
6670 if (GetDiskFreeSpace (rootname,
6671 &sectors_per_cluster,
6672 &bytes_per_sector,
6673 &free_clusters,
6674 &total_clusters))
6675 value = list3 (make_float ((double) total_clusters
6676 * sectors_per_cluster * bytes_per_sector),
6677 make_float ((double) free_clusters
6678 * sectors_per_cluster * bytes_per_sector),
6679 make_float ((double) free_clusters
6680 * sectors_per_cluster * bytes_per_sector));
6684 return value;
6687 DEFUN ("default-printer-name", Fdefault_printer_name, Sdefault_printer_name,
6688 0, 0, 0, doc: /* Return the name of Windows default printer device. */)
6689 (void)
6691 static char pname_buf[256];
6692 int err;
6693 HANDLE hPrn;
6694 PRINTER_INFO_2 *ppi2 = NULL;
6695 DWORD dwNeeded = 0, dwReturned = 0;
6697 /* Retrieve the default string from Win.ini (the registry).
6698 * String will be in form "printername,drivername,portname".
6699 * This is the most portable way to get the default printer. */
6700 if (GetProfileString ("windows", "device", ",,", pname_buf, sizeof (pname_buf)) <= 0)
6701 return Qnil;
6702 /* printername precedes first "," character */
6703 strtok (pname_buf, ",");
6704 /* We want to know more than the printer name */
6705 if (!OpenPrinter (pname_buf, &hPrn, NULL))
6706 return Qnil;
6707 GetPrinter (hPrn, 2, NULL, 0, &dwNeeded);
6708 if (dwNeeded == 0)
6710 ClosePrinter (hPrn);
6711 return Qnil;
6713 /* Allocate memory for the PRINTER_INFO_2 struct */
6714 ppi2 = xmalloc (dwNeeded);
6715 if (!ppi2)
6717 ClosePrinter (hPrn);
6718 return Qnil;
6720 /* Call GetPrinter again with big enough memory block. */
6721 err = GetPrinter (hPrn, 2, (LPBYTE)ppi2, dwNeeded, &dwReturned);
6722 ClosePrinter (hPrn);
6723 if (!err)
6725 xfree (ppi2);
6726 return Qnil;
6729 if (ppi2)
6731 if (ppi2->Attributes & PRINTER_ATTRIBUTE_SHARED && ppi2->pServerName)
6733 /* a remote printer */
6734 if (*ppi2->pServerName == '\\')
6735 _snprintf (pname_buf, sizeof (pname_buf), "%s\\%s", ppi2->pServerName,
6736 ppi2->pShareName);
6737 else
6738 _snprintf (pname_buf, sizeof (pname_buf), "\\\\%s\\%s", ppi2->pServerName,
6739 ppi2->pShareName);
6740 pname_buf[sizeof (pname_buf) - 1] = '\0';
6742 else
6744 /* a local printer */
6745 strncpy (pname_buf, ppi2->pPortName, sizeof (pname_buf));
6746 pname_buf[sizeof (pname_buf) - 1] = '\0';
6747 /* `pPortName' can include several ports, delimited by ','.
6748 * we only use the first one. */
6749 strtok (pname_buf, ",");
6751 xfree (ppi2);
6754 return build_string (pname_buf);
6757 /***********************************************************************
6758 Initialization
6759 ***********************************************************************/
6761 /* Keep this list in the same order as frame_parms in frame.c.
6762 Use 0 for unsupported frame parameters. */
6764 frame_parm_handler w32_frame_parm_handlers[] =
6766 x_set_autoraise,
6767 x_set_autolower,
6768 x_set_background_color,
6769 x_set_border_color,
6770 x_set_border_width,
6771 x_set_cursor_color,
6772 x_set_cursor_type,
6773 x_set_font,
6774 x_set_foreground_color,
6775 x_set_icon_name,
6776 x_set_icon_type,
6777 x_set_internal_border_width,
6778 x_set_menu_bar_lines,
6779 x_set_mouse_color,
6780 x_explicitly_set_name,
6781 x_set_scroll_bar_width,
6782 x_set_title,
6783 x_set_unsplittable,
6784 x_set_vertical_scroll_bars,
6785 x_set_visibility,
6786 x_set_tool_bar_lines,
6787 0, /* x_set_scroll_bar_foreground, */
6788 0, /* x_set_scroll_bar_background, */
6789 x_set_screen_gamma,
6790 x_set_line_spacing,
6791 x_set_fringe_width,
6792 x_set_fringe_width,
6793 0, /* x_set_wait_for_wm, */
6794 x_set_fullscreen,
6795 x_set_font_backend,
6796 x_set_alpha,
6797 0, /* x_set_sticky */
6798 0, /* x_set_tool_bar_position */
6801 void
6802 syms_of_w32fns (void)
6804 globals_of_w32fns ();
6805 /* This is zero if not using MS-Windows. */
6806 w32_in_use = 0;
6807 track_mouse_window = NULL;
6809 w32_visible_system_caret_hwnd = NULL;
6811 DEFSYM (Qsuppress_icon, "suppress-icon");
6812 DEFSYM (Qundefined_color, "undefined-color");
6813 DEFSYM (Qcancel_timer, "cancel-timer");
6814 DEFSYM (Qhyper, "hyper");
6815 DEFSYM (Qsuper, "super");
6816 DEFSYM (Qmeta, "meta");
6817 DEFSYM (Qalt, "alt");
6818 DEFSYM (Qctrl, "ctrl");
6819 DEFSYM (Qcontrol, "control");
6820 DEFSYM (Qshift, "shift");
6821 DEFSYM (Qfont_param, "font-parameter");
6822 /* This is the end of symbol initialization. */
6825 Fput (Qundefined_color, Qerror_conditions,
6826 listn (CONSTYPE_PURE, 2, Qundefined_color, Qerror));
6827 Fput (Qundefined_color, Qerror_message,
6828 build_pure_c_string ("Undefined color"));
6830 staticpro (&w32_grabbed_keys);
6831 w32_grabbed_keys = Qnil;
6833 DEFVAR_LISP ("w32-color-map", Vw32_color_map,
6834 doc: /* An array of color name mappings for Windows. */);
6835 Vw32_color_map = Qnil;
6837 DEFVAR_LISP ("w32-pass-alt-to-system", Vw32_pass_alt_to_system,
6838 doc: /* Non-nil if Alt key presses are passed on to Windows.
6839 When non-nil, for example, Alt pressed and released and then space will
6840 open the System menu. When nil, Emacs processes the Alt key events, and
6841 then silently swallows them. */);
6842 Vw32_pass_alt_to_system = Qnil;
6844 DEFVAR_LISP ("w32-alt-is-meta", Vw32_alt_is_meta,
6845 doc: /* Non-nil if the Alt key is to be considered the same as the META key.
6846 When nil, Emacs will translate the Alt key to the ALT modifier, not to META. */);
6847 Vw32_alt_is_meta = Qt;
6849 DEFVAR_INT ("w32-quit-key", w32_quit_key,
6850 doc: /* If non-zero, the virtual key code for an alternative quit key. */);
6851 w32_quit_key = 0;
6853 DEFVAR_LISP ("w32-pass-lwindow-to-system",
6854 Vw32_pass_lwindow_to_system,
6855 doc: /* If non-nil, the left \"Windows\" key is passed on to Windows.
6857 When non-nil, the Start menu is opened by tapping the key.
6858 If you set this to nil, the left \"Windows\" key is processed by Emacs
6859 according to the value of `w32-lwindow-modifier', which see.
6861 Note that some combinations of the left \"Windows\" key with other keys are
6862 caught by Windows at low level, and so binding them in Emacs will have no
6863 effect. For example, <lwindow>-r always pops up the Windows Run dialog,
6864 <lwindow>-<Pause> pops up the "System Properties" dialog, etc. However, see
6865 the doc string of `w32-phantom-key-code'. */);
6866 Vw32_pass_lwindow_to_system = Qt;
6868 DEFVAR_LISP ("w32-pass-rwindow-to-system",
6869 Vw32_pass_rwindow_to_system,
6870 doc: /* If non-nil, the right \"Windows\" key is passed on to Windows.
6872 When non-nil, the Start menu is opened by tapping the key.
6873 If you set this to nil, the right \"Windows\" key is processed by Emacs
6874 according to the value of `w32-rwindow-modifier', which see.
6876 Note that some combinations of the right \"Windows\" key with other keys are
6877 caught by Windows at low level, and so binding them in Emacs will have no
6878 effect. For example, <rwindow>-r always pops up the Windows Run dialog,
6879 <rwindow>-<Pause> pops up the "System Properties" dialog, etc. However, see
6880 the doc string of `w32-phantom-key-code'. */);
6881 Vw32_pass_rwindow_to_system = Qt;
6883 DEFVAR_LISP ("w32-phantom-key-code",
6884 Vw32_phantom_key_code,
6885 doc: /* Virtual key code used to generate \"phantom\" key presses.
6886 Value is a number between 0 and 255.
6888 Phantom key presses are generated in order to stop the system from
6889 acting on \"Windows\" key events when `w32-pass-lwindow-to-system' or
6890 `w32-pass-rwindow-to-system' is nil. */);
6891 /* Although 255 is technically not a valid key code, it works and
6892 means that this hack won't interfere with any real key code. */
6893 XSETINT (Vw32_phantom_key_code, 255);
6895 DEFVAR_LISP ("w32-enable-num-lock",
6896 Vw32_enable_num_lock,
6897 doc: /* If non-nil, the Num Lock key acts normally.
6898 Set to nil to handle Num Lock as the `kp-numlock' key. */);
6899 Vw32_enable_num_lock = Qt;
6901 DEFVAR_LISP ("w32-enable-caps-lock",
6902 Vw32_enable_caps_lock,
6903 doc: /* If non-nil, the Caps Lock key acts normally.
6904 Set to nil to handle Caps Lock as the `capslock' key. */);
6905 Vw32_enable_caps_lock = Qt;
6907 DEFVAR_LISP ("w32-scroll-lock-modifier",
6908 Vw32_scroll_lock_modifier,
6909 doc: /* Modifier to use for the Scroll Lock ON state.
6910 The value can be hyper, super, meta, alt, control or shift for the
6911 respective modifier, or nil to handle Scroll Lock as the `scroll' key.
6912 Any other value will cause the Scroll Lock key to be ignored. */);
6913 Vw32_scroll_lock_modifier = Qnil;
6915 DEFVAR_LISP ("w32-lwindow-modifier",
6916 Vw32_lwindow_modifier,
6917 doc: /* Modifier to use for the left \"Windows\" key.
6918 The value can be hyper, super, meta, alt, control or shift for the
6919 respective modifier, or nil to appear as the `lwindow' key.
6920 Any other value will cause the key to be ignored. */);
6921 Vw32_lwindow_modifier = Qnil;
6923 DEFVAR_LISP ("w32-rwindow-modifier",
6924 Vw32_rwindow_modifier,
6925 doc: /* Modifier to use for the right \"Windows\" key.
6926 The value can be hyper, super, meta, alt, control or shift for the
6927 respective modifier, or nil to appear as the `rwindow' key.
6928 Any other value will cause the key to be ignored. */);
6929 Vw32_rwindow_modifier = Qnil;
6931 DEFVAR_LISP ("w32-apps-modifier",
6932 Vw32_apps_modifier,
6933 doc: /* Modifier to use for the \"Apps\" key.
6934 The value can be hyper, super, meta, alt, control or shift for the
6935 respective modifier, or nil to appear as the `apps' key.
6936 Any other value will cause the key to be ignored. */);
6937 Vw32_apps_modifier = Qnil;
6939 DEFVAR_BOOL ("w32-enable-synthesized-fonts", w32_enable_synthesized_fonts,
6940 doc: /* Non-nil enables selection of artificially italicized and bold fonts. */);
6941 w32_enable_synthesized_fonts = 0;
6943 DEFVAR_LISP ("w32-enable-palette", Vw32_enable_palette,
6944 doc: /* Non-nil enables Windows palette management to map colors exactly. */);
6945 Vw32_enable_palette = Qt;
6947 DEFVAR_INT ("w32-mouse-button-tolerance",
6948 w32_mouse_button_tolerance,
6949 doc: /* Analogue of double click interval for faking middle mouse events.
6950 The value is the minimum time in milliseconds that must elapse between
6951 left and right button down events before they are considered distinct events.
6952 If both mouse buttons are depressed within this interval, a middle mouse
6953 button down event is generated instead. */);
6954 w32_mouse_button_tolerance = GetDoubleClickTime () / 2;
6956 DEFVAR_INT ("w32-mouse-move-interval",
6957 w32_mouse_move_interval,
6958 doc: /* Minimum interval between mouse move events.
6959 The value is the minimum time in milliseconds that must elapse between
6960 successive mouse move (or scroll bar drag) events before they are
6961 reported as lisp events. */);
6962 w32_mouse_move_interval = 0;
6964 DEFVAR_BOOL ("w32-pass-extra-mouse-buttons-to-system",
6965 w32_pass_extra_mouse_buttons_to_system,
6966 doc: /* If non-nil, the fourth and fifth mouse buttons are passed to Windows.
6967 Recent versions of Windows support mice with up to five buttons.
6968 Since most applications don't support these extra buttons, most mouse
6969 drivers will allow you to map them to functions at the system level.
6970 If this variable is non-nil, Emacs will pass them on, allowing the
6971 system to handle them. */);
6972 w32_pass_extra_mouse_buttons_to_system = 0;
6974 DEFVAR_BOOL ("w32-pass-multimedia-buttons-to-system",
6975 w32_pass_multimedia_buttons_to_system,
6976 doc: /* If non-nil, media buttons are passed to Windows.
6977 Some modern keyboards contain buttons for controlling media players, web
6978 browsers and other applications. Generally these buttons are handled on a
6979 system wide basis, but by setting this to nil they are made available
6980 to Emacs for binding. Depending on your keyboard, additional keys that
6981 may be available are:
6983 browser-back, browser-forward, browser-refresh, browser-stop,
6984 browser-search, browser-favorites, browser-home,
6985 mail, mail-reply, mail-forward, mail-send,
6986 app-1, app-2,
6987 help, find, new, open, close, save, print, undo, redo, copy, cut, paste,
6988 spell-check, correction-list, toggle-dictate-command,
6989 media-next, media-previous, media-stop, media-play-pause, media-select,
6990 media-play, media-pause, media-record, media-fast-forward, media-rewind,
6991 media-channel-up, media-channel-down,
6992 volume-mute, volume-up, volume-down,
6993 mic-volume-mute, mic-volume-down, mic-volume-up, mic-toggle,
6994 bass-down, bass-boost, bass-up, treble-down, treble-up */);
6995 w32_pass_multimedia_buttons_to_system = 1;
6997 #if 0 /* TODO: Mouse cursor customization. */
6998 DEFVAR_LISP ("x-pointer-shape", Vx_pointer_shape,
6999 doc: /* The shape of the pointer when over text.
7000 Changing the value does not affect existing frames
7001 unless you set the mouse color. */);
7002 Vx_pointer_shape = Qnil;
7004 Vx_nontext_pointer_shape = Qnil;
7006 Vx_mode_pointer_shape = Qnil;
7008 DEFVAR_LISP ("x-hourglass-pointer-shape", Vx_hourglass_pointer_shape,
7009 doc: /* The shape of the pointer when Emacs is busy.
7010 This variable takes effect when you create a new frame
7011 or when you set the mouse color. */);
7012 Vx_hourglass_pointer_shape = Qnil;
7014 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
7015 Vx_sensitive_text_pointer_shape,
7016 doc: /* The shape of the pointer when over mouse-sensitive text.
7017 This variable takes effect when you create a new frame
7018 or when you set the mouse color. */);
7019 Vx_sensitive_text_pointer_shape = Qnil;
7021 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
7022 Vx_window_horizontal_drag_shape,
7023 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
7024 This variable takes effect when you create a new frame
7025 or when you set the mouse color. */);
7026 Vx_window_horizontal_drag_shape = Qnil;
7027 #endif
7029 DEFVAR_LISP ("x-cursor-fore-pixel", Vx_cursor_fore_pixel,
7030 doc: /* A string indicating the foreground color of the cursor box. */);
7031 Vx_cursor_fore_pixel = Qnil;
7033 DEFVAR_LISP ("x-max-tooltip-size", Vx_max_tooltip_size,
7034 doc: /* Maximum size for tooltips.
7035 Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
7036 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
7038 DEFVAR_LISP ("x-no-window-manager", Vx_no_window_manager,
7039 doc: /* Non-nil if no window manager is in use.
7040 Emacs doesn't try to figure this out; this is always nil
7041 unless you set it to something else. */);
7042 /* We don't have any way to find this out, so set it to nil
7043 and maybe the user would like to set it to t. */
7044 Vx_no_window_manager = Qnil;
7046 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
7047 Vx_pixel_size_width_font_regexp,
7048 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
7050 Since Emacs gets width of a font matching with this regexp from
7051 PIXEL_SIZE field of the name, font finding mechanism gets faster for
7052 such a font. This is especially effective for such large fonts as
7053 Chinese, Japanese, and Korean. */);
7054 Vx_pixel_size_width_font_regexp = Qnil;
7056 DEFVAR_LISP ("w32-bdf-filename-alist",
7057 Vw32_bdf_filename_alist,
7058 doc: /* List of bdf fonts and their corresponding filenames. */);
7059 Vw32_bdf_filename_alist = Qnil;
7061 DEFVAR_BOOL ("w32-strict-fontnames",
7062 w32_strict_fontnames,
7063 doc: /* Non-nil means only use fonts that are exact matches for those requested.
7064 Default is nil, which allows old fontnames that are not XLFD compliant,
7065 and allows third-party CJK display to work by specifying false charset
7066 fields to trick Emacs into translating to Big5, SJIS etc.
7067 Setting this to t will prevent wrong fonts being selected when
7068 fontsets are automatically created. */);
7069 w32_strict_fontnames = 0;
7071 DEFVAR_BOOL ("w32-strict-painting",
7072 w32_strict_painting,
7073 doc: /* Non-nil means use strict rules for repainting frames.
7074 Set this to nil to get the old behavior for repainting; this should
7075 only be necessary if the default setting causes problems. */);
7076 w32_strict_painting = 1;
7078 #if 0 /* TODO: Port to W32 */
7079 defsubr (&Sx_change_window_property);
7080 defsubr (&Sx_delete_window_property);
7081 defsubr (&Sx_window_property);
7082 #endif
7083 defsubr (&Sxw_display_color_p);
7084 defsubr (&Sx_display_grayscale_p);
7085 defsubr (&Sxw_color_defined_p);
7086 defsubr (&Sxw_color_values);
7087 defsubr (&Sx_server_max_request_size);
7088 defsubr (&Sx_server_vendor);
7089 defsubr (&Sx_server_version);
7090 defsubr (&Sx_display_pixel_width);
7091 defsubr (&Sx_display_pixel_height);
7092 defsubr (&Sx_display_mm_width);
7093 defsubr (&Sx_display_mm_height);
7094 defsubr (&Sx_display_screens);
7095 defsubr (&Sx_display_planes);
7096 defsubr (&Sx_display_color_cells);
7097 defsubr (&Sx_display_visual_class);
7098 defsubr (&Sx_display_backing_store);
7099 defsubr (&Sx_display_save_under);
7100 defsubr (&Sx_create_frame);
7101 defsubr (&Sx_open_connection);
7102 defsubr (&Sx_close_connection);
7103 defsubr (&Sx_display_list);
7104 defsubr (&Sx_synchronize);
7105 defsubr (&Sx_focus_frame);
7107 /* W32 specific functions */
7109 defsubr (&Sw32_define_rgb_color);
7110 defsubr (&Sw32_default_color_map);
7111 defsubr (&Sw32_send_sys_command);
7112 defsubr (&Sw32_shell_execute);
7113 defsubr (&Sw32_register_hot_key);
7114 defsubr (&Sw32_unregister_hot_key);
7115 defsubr (&Sw32_registered_hot_keys);
7116 defsubr (&Sw32_reconstruct_hot_key);
7117 defsubr (&Sw32_toggle_lock_key);
7118 defsubr (&Sw32_window_exists_p);
7119 defsubr (&Sw32_battery_status);
7121 defsubr (&Sfile_system_info);
7122 defsubr (&Sdefault_printer_name);
7124 check_window_system_func = check_w32;
7127 hourglass_timer = 0;
7128 hourglass_hwnd = NULL;
7130 defsubr (&Sx_show_tip);
7131 defsubr (&Sx_hide_tip);
7132 tip_timer = Qnil;
7133 staticpro (&tip_timer);
7134 tip_frame = Qnil;
7135 staticpro (&tip_frame);
7137 last_show_tip_args = Qnil;
7138 staticpro (&last_show_tip_args);
7140 defsubr (&Sx_file_dialog);
7141 defsubr (&Ssystem_move_file_to_trash);
7146 globals_of_w32fns is used to initialize those global variables that
7147 must always be initialized on startup even when the global variable
7148 initialized is non zero (see the function main in emacs.c).
7149 globals_of_w32fns is called from syms_of_w32fns when the global
7150 variable initialized is 0 and directly from main when initialized
7151 is non zero.
7153 void
7154 globals_of_w32fns (void)
7156 HMODULE user32_lib = GetModuleHandle ("user32.dll");
7158 TrackMouseEvent not available in all versions of Windows, so must load
7159 it dynamically. Do it once, here, instead of every time it is used.
7161 track_mouse_event_fn = (TrackMouseEvent_Proc)
7162 GetProcAddress (user32_lib, "TrackMouseEvent");
7164 monitor_from_point_fn = (MonitorFromPoint_Proc)
7165 GetProcAddress (user32_lib, "MonitorFromPoint");
7166 get_monitor_info_fn = (GetMonitorInfo_Proc)
7167 GetProcAddress (user32_lib, "GetMonitorInfoA");
7170 HMODULE imm32_lib = GetModuleHandle ("imm32.dll");
7171 get_composition_string_fn = (ImmGetCompositionString_Proc)
7172 GetProcAddress (imm32_lib, "ImmGetCompositionStringW");
7173 get_ime_context_fn = (ImmGetContext_Proc)
7174 GetProcAddress (imm32_lib, "ImmGetContext");
7175 release_ime_context_fn = (ImmReleaseContext_Proc)
7176 GetProcAddress (imm32_lib, "ImmReleaseContext");
7177 set_ime_composition_window_fn = (ImmSetCompositionWindow_Proc)
7178 GetProcAddress (imm32_lib, "ImmSetCompositionWindow");
7180 DEFVAR_INT ("w32-ansi-code-page",
7181 w32_ansi_code_page,
7182 doc: /* The ANSI code page used by the system. */);
7183 w32_ansi_code_page = GetACP ();
7185 if (os_subtype == OS_NT)
7186 w32_unicode_gui = 1;
7187 else
7188 w32_unicode_gui = 0;
7190 /* MessageBox does not work without this when linked to comctl32.dll 6.0. */
7191 InitCommonControls ();
7193 syms_of_w32uniscribe ();
7196 void
7197 emacs_abort (void)
7199 int button;
7200 button = MessageBox (NULL,
7201 "A fatal error has occurred!\n\n"
7202 "Would you like to attach a debugger?\n\n"
7203 "Select YES to debug, NO to abort Emacs"
7204 #if __GNUC__
7205 "\n\n(type \"gdb -p <emacs-PID>\" and\n"
7206 "\"continue\" inside GDB before clicking YES.)"
7207 #endif
7208 , "Emacs Abort Dialog",
7209 MB_ICONEXCLAMATION | MB_TASKMODAL
7210 | MB_SETFOREGROUND | MB_YESNO);
7211 switch (button)
7213 case IDYES:
7214 DebugBreak ();
7215 exit (2); /* tell the compiler we will never return */
7216 case IDNO:
7217 default:
7218 abort ();
7219 break;
7223 /* For convenience when debugging. */
7225 w32_last_error (void)
7227 return GetLastError ();