Implement C-c m in report-emacs-bug (insert to mailer) for OSX.
[emacs.git] / src / w32fns.c
blobb09bb0b5b5fb170d8081ac3ae5a019fed4c8f070
1 /* Graphical user interface functions for the Microsoft W32 API.
3 Copyright (C) 1989, 1992-2011 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>
29 #include <setjmp.h>
31 #include "lisp.h"
32 #include "w32term.h"
33 #include "frame.h"
34 #include "window.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 "character.h"
42 #include "charset.h"
43 #include "coding.h"
44 #include "ccl.h"
45 #include "fontset.h"
46 #include "systime.h"
47 #include "termhooks.h"
48 #include "w32heap.h"
49 #include "w32.h"
51 #include "bitmaps/gray.xbm"
53 #include <commctrl.h>
54 #include <commdlg.h>
55 #include <shellapi.h>
56 #include <ctype.h>
57 #include <winspool.h>
58 #include <objbase.h>
60 #include <dlgs.h>
61 #include <imm.h>
62 #define FILE_NAME_TEXT_FIELD edt1
64 #include "font.h"
65 #include "w32font.h"
67 #ifndef FOF_NO_CONNECTED_ELEMENTS
68 #define FOF_NO_CONNECTED_ELEMENTS 0x2000
69 #endif
71 void syms_of_w32fns (void);
72 void globals_of_w32fns (void);
74 extern void free_frame_menubar (struct frame *);
75 extern double atof (const char *);
76 extern int w32_console_toggle_lock_key (int, Lisp_Object);
77 extern void w32_menu_display_help (HWND, HMENU, UINT, UINT);
78 extern void w32_free_menu_strings (HWND);
79 extern const char *map_w32_filename (const char *, const char **);
81 extern int quit_char;
83 extern const char *const lispy_function_keys[];
85 /* If non-zero, a w32 timer that, when it expires, displays an
86 hourglass cursor on all frames. */
87 static unsigned hourglass_timer = 0;
88 static HWND hourglass_hwnd = NULL;
90 #ifndef IDC_HAND
91 #define IDC_HAND MAKEINTRESOURCE(32649)
92 #endif
94 /* Nonzero if using Windows. */
96 static int w32_in_use;
98 Lisp_Object Qnone;
99 Lisp_Object Qsuppress_icon;
100 Lisp_Object Qundefined_color;
101 Lisp_Object Qcancel_timer;
102 Lisp_Object Qfont_param;
103 Lisp_Object Qhyper;
104 Lisp_Object Qsuper;
105 Lisp_Object Qmeta;
106 Lisp_Object Qalt;
107 Lisp_Object Qctrl;
108 Lisp_Object Qcontrol;
109 Lisp_Object Qshift;
112 /* Prefix for system colors. */
113 #define SYSTEM_COLOR_PREFIX "System"
114 #define SYSTEM_COLOR_PREFIX_LEN (sizeof (SYSTEM_COLOR_PREFIX) - 1)
116 /* State variables for emulating a three button mouse. */
117 #define LMOUSE 1
118 #define MMOUSE 2
119 #define RMOUSE 4
121 static int button_state = 0;
122 static W32Msg saved_mouse_button_msg;
123 static unsigned mouse_button_timer = 0; /* non-zero when timer is active */
124 static W32Msg saved_mouse_move_msg;
125 static unsigned mouse_move_timer = 0;
127 /* Window that is tracking the mouse. */
128 static HWND track_mouse_window;
130 /* Multi-monitor API definitions that are not pulled from the headers
131 since we are compiling for NT 4. */
132 #ifndef MONITOR_DEFAULT_TO_NEAREST
133 #define MONITOR_DEFAULT_TO_NEAREST 2
134 #endif
135 /* MinGW headers define MONITORINFO unconditionally, but MSVC ones don't.
136 To avoid a compile error on one or the other, redefine with a new name. */
137 struct MONITOR_INFO
139 DWORD cbSize;
140 RECT rcMonitor;
141 RECT rcWork;
142 DWORD dwFlags;
145 /* Reportedly, VS 6 does not have this in its headers. */
146 #if defined (_MSC_VER) && _MSC_VER < 1300
147 DECLARE_HANDLE(HMONITOR);
148 #endif
150 typedef BOOL (WINAPI * TrackMouseEvent_Proc)
151 (IN OUT LPTRACKMOUSEEVENT lpEventTrack);
152 typedef LONG (WINAPI * ImmGetCompositionString_Proc)
153 (IN HIMC context, IN DWORD index, OUT LPVOID buffer, IN DWORD bufLen);
154 typedef HIMC (WINAPI * ImmGetContext_Proc) (IN HWND window);
155 typedef HWND (WINAPI * ImmReleaseContext_Proc) (IN HWND wnd, IN HIMC context);
156 typedef HWND (WINAPI * ImmSetCompositionWindow_Proc) (IN HIMC context,
157 IN COMPOSITIONFORM *form);
158 typedef HMONITOR (WINAPI * MonitorFromPoint_Proc) (IN POINT pt, IN DWORD flags);
159 typedef BOOL (WINAPI * GetMonitorInfo_Proc)
160 (IN HMONITOR monitor, OUT struct MONITOR_INFO* info);
162 TrackMouseEvent_Proc track_mouse_event_fn = NULL;
163 ImmGetCompositionString_Proc get_composition_string_fn = NULL;
164 ImmGetContext_Proc get_ime_context_fn = NULL;
165 ImmReleaseContext_Proc release_ime_context_fn = NULL;
166 ImmSetCompositionWindow_Proc set_ime_composition_window_fn = NULL;
167 MonitorFromPoint_Proc monitor_from_point_fn = NULL;
168 GetMonitorInfo_Proc get_monitor_info_fn = NULL;
170 extern AppendMenuW_Proc unicode_append_menu;
172 /* Flag to selectively ignore WM_IME_CHAR messages. */
173 static int ignore_ime_char = 0;
175 /* W95 mousewheel handler */
176 unsigned int msh_mousewheel = 0;
178 /* Timers */
179 #define MOUSE_BUTTON_ID 1
180 #define MOUSE_MOVE_ID 2
181 #define MENU_FREE_ID 3
182 #define HOURGLASS_ID 4
183 /* The delay (milliseconds) before a menu is freed after WM_EXITMENULOOP
184 is received. */
185 #define MENU_FREE_DELAY 1000
186 static unsigned menu_free_timer = 0;
188 extern Lisp_Object Qtooltip;
190 #ifdef GLYPH_DEBUG
191 int image_cache_refcount, dpyinfo_refcount;
192 #endif
195 extern HWND w32_system_caret_hwnd;
197 extern int w32_system_caret_height;
198 extern int w32_system_caret_x;
199 extern int w32_system_caret_y;
200 static HWND w32_visible_system_caret_hwnd;
202 /* From w32menu.c */
203 extern HMENU current_popup_menu;
204 static int menubar_in_use = 0;
206 /* From w32uniscribe.c */
207 extern void syms_of_w32uniscribe (void);
208 extern int uniscribe_available;
210 /* Function prototypes for hourglass support. */
211 static void w32_show_hourglass (struct frame *);
212 static void w32_hide_hourglass (void);
216 /* Error if we are not connected to MS-Windows. */
217 void
218 check_w32 (void)
220 if (! w32_in_use)
221 error ("MS-Windows not in use or not initialized");
224 /* Nonzero if we can use mouse menus.
225 You should not call this unless HAVE_MENUS is defined. */
228 have_menus_p (void)
230 return w32_in_use;
233 /* Extract a frame as a FRAME_PTR, defaulting to the selected frame
234 and checking validity for W32. */
236 FRAME_PTR
237 check_x_frame (Lisp_Object frame)
239 FRAME_PTR f;
241 if (NILP (frame))
242 frame = selected_frame;
243 CHECK_LIVE_FRAME (frame);
244 f = XFRAME (frame);
245 if (! FRAME_W32_P (f))
246 error ("Non-W32 frame used");
247 return f;
250 /* Let the user specify a display with a frame.
251 nil stands for the selected frame--or, if that is not a w32 frame,
252 the first display on the list. */
254 struct w32_display_info *
255 check_x_display_info (Lisp_Object frame)
257 if (NILP (frame))
259 struct frame *sf = XFRAME (selected_frame);
261 if (FRAME_W32_P (sf) && FRAME_LIVE_P (sf))
262 return FRAME_W32_DISPLAY_INFO (sf);
263 else
264 return &one_w32_display_info;
266 else if (STRINGP (frame))
267 return x_display_info_for_name (frame);
268 else
270 FRAME_PTR f;
272 CHECK_LIVE_FRAME (frame);
273 f = XFRAME (frame);
274 if (! FRAME_W32_P (f))
275 error ("Non-W32 frame used");
276 return FRAME_W32_DISPLAY_INFO (f);
280 /* Return the Emacs frame-object corresponding to an w32 window.
281 It could be the frame's main window or an icon window. */
283 /* This function can be called during GC, so use GC_xxx type test macros. */
285 struct frame *
286 x_window_to_frame (struct w32_display_info *dpyinfo, HWND wdesc)
288 Lisp_Object tail, frame;
289 struct frame *f;
291 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
293 frame = XCAR (tail);
294 if (!FRAMEP (frame))
295 continue;
296 f = XFRAME (frame);
297 if (!FRAME_W32_P (f) || FRAME_W32_DISPLAY_INFO (f) != dpyinfo)
298 continue;
300 if (FRAME_W32_WINDOW (f) == wdesc)
301 return f;
303 return 0;
307 static Lisp_Object unwind_create_frame (Lisp_Object);
308 static Lisp_Object unwind_create_tip_frame (Lisp_Object);
309 static void my_create_window (struct frame *);
310 static void my_create_tip_window (struct frame *);
312 /* TODO: Native Input Method support; see x_create_im. */
313 void x_set_foreground_color (struct frame *, Lisp_Object, Lisp_Object);
314 void x_set_background_color (struct frame *, Lisp_Object, Lisp_Object);
315 void x_set_mouse_color (struct frame *, Lisp_Object, Lisp_Object);
316 void x_set_cursor_color (struct frame *, Lisp_Object, Lisp_Object);
317 void x_set_border_color (struct frame *, Lisp_Object, Lisp_Object);
318 void x_set_cursor_type (struct frame *, Lisp_Object, Lisp_Object);
319 void x_set_icon_type (struct frame *, Lisp_Object, Lisp_Object);
320 void x_set_icon_name (struct frame *, Lisp_Object, Lisp_Object);
321 void x_explicitly_set_name (struct frame *, Lisp_Object, Lisp_Object);
322 void x_set_menu_bar_lines (struct frame *, Lisp_Object, Lisp_Object);
323 void x_set_title (struct frame *, Lisp_Object, Lisp_Object);
324 void x_set_tool_bar_lines (struct frame *, Lisp_Object, Lisp_Object);
329 /* Store the screen positions of frame F into XPTR and YPTR.
330 These are the positions of the containing window manager window,
331 not Emacs's own window. */
333 void
334 x_real_positions (FRAME_PTR f, int *xptr, int *yptr)
336 POINT pt;
337 RECT rect;
339 /* Get the bounds of the WM window. */
340 GetWindowRect (FRAME_W32_WINDOW (f), &rect);
342 pt.x = 0;
343 pt.y = 0;
345 /* Convert (0, 0) in the client area to screen co-ordinates. */
346 ClientToScreen (FRAME_W32_WINDOW (f), &pt);
348 /* Remember x_pixels_diff and y_pixels_diff. */
349 f->x_pixels_diff = pt.x - rect.left;
350 f->y_pixels_diff = pt.y - rect.top;
352 *xptr = rect.left;
353 *yptr = rect.top;
358 DEFUN ("w32-define-rgb-color", Fw32_define_rgb_color,
359 Sw32_define_rgb_color, 4, 4, 0,
360 doc: /* Convert RGB numbers to a Windows color reference and associate with NAME.
361 This adds or updates a named color to `w32-color-map', making it
362 available for use. The original entry's RGB ref is returned, or nil
363 if the entry is new. */)
364 (Lisp_Object red, Lisp_Object green, Lisp_Object blue, Lisp_Object name)
366 Lisp_Object rgb;
367 Lisp_Object oldrgb = Qnil;
368 Lisp_Object entry;
370 CHECK_NUMBER (red);
371 CHECK_NUMBER (green);
372 CHECK_NUMBER (blue);
373 CHECK_STRING (name);
375 XSETINT (rgb, RGB (XUINT (red), XUINT (green), XUINT (blue)));
377 BLOCK_INPUT;
379 /* replace existing entry in w32-color-map or add new entry. */
380 entry = Fassoc (name, Vw32_color_map);
381 if (NILP (entry))
383 entry = Fcons (name, rgb);
384 Vw32_color_map = Fcons (entry, Vw32_color_map);
386 else
388 oldrgb = Fcdr (entry);
389 Fsetcdr (entry, rgb);
392 UNBLOCK_INPUT;
394 return (oldrgb);
397 /* The default colors for the w32 color map */
398 typedef struct colormap_t
400 char *name;
401 COLORREF colorref;
402 } colormap_t;
404 colormap_t w32_color_map[] =
406 {"snow" , PALETTERGB (255,250,250)},
407 {"ghost white" , PALETTERGB (248,248,255)},
408 {"GhostWhite" , PALETTERGB (248,248,255)},
409 {"white smoke" , PALETTERGB (245,245,245)},
410 {"WhiteSmoke" , PALETTERGB (245,245,245)},
411 {"gainsboro" , PALETTERGB (220,220,220)},
412 {"floral white" , PALETTERGB (255,250,240)},
413 {"FloralWhite" , PALETTERGB (255,250,240)},
414 {"old lace" , PALETTERGB (253,245,230)},
415 {"OldLace" , PALETTERGB (253,245,230)},
416 {"linen" , PALETTERGB (250,240,230)},
417 {"antique white" , PALETTERGB (250,235,215)},
418 {"AntiqueWhite" , PALETTERGB (250,235,215)},
419 {"papaya whip" , PALETTERGB (255,239,213)},
420 {"PapayaWhip" , PALETTERGB (255,239,213)},
421 {"blanched almond" , PALETTERGB (255,235,205)},
422 {"BlanchedAlmond" , PALETTERGB (255,235,205)},
423 {"bisque" , PALETTERGB (255,228,196)},
424 {"peach puff" , PALETTERGB (255,218,185)},
425 {"PeachPuff" , PALETTERGB (255,218,185)},
426 {"navajo white" , PALETTERGB (255,222,173)},
427 {"NavajoWhite" , PALETTERGB (255,222,173)},
428 {"moccasin" , PALETTERGB (255,228,181)},
429 {"cornsilk" , PALETTERGB (255,248,220)},
430 {"ivory" , PALETTERGB (255,255,240)},
431 {"lemon chiffon" , PALETTERGB (255,250,205)},
432 {"LemonChiffon" , PALETTERGB (255,250,205)},
433 {"seashell" , PALETTERGB (255,245,238)},
434 {"honeydew" , PALETTERGB (240,255,240)},
435 {"mint cream" , PALETTERGB (245,255,250)},
436 {"MintCream" , PALETTERGB (245,255,250)},
437 {"azure" , PALETTERGB (240,255,255)},
438 {"alice blue" , PALETTERGB (240,248,255)},
439 {"AliceBlue" , PALETTERGB (240,248,255)},
440 {"lavender" , PALETTERGB (230,230,250)},
441 {"lavender blush" , PALETTERGB (255,240,245)},
442 {"LavenderBlush" , PALETTERGB (255,240,245)},
443 {"misty rose" , PALETTERGB (255,228,225)},
444 {"MistyRose" , PALETTERGB (255,228,225)},
445 {"white" , PALETTERGB (255,255,255)},
446 {"black" , PALETTERGB ( 0, 0, 0)},
447 {"dark slate gray" , PALETTERGB ( 47, 79, 79)},
448 {"DarkSlateGray" , PALETTERGB ( 47, 79, 79)},
449 {"dark slate grey" , PALETTERGB ( 47, 79, 79)},
450 {"DarkSlateGrey" , PALETTERGB ( 47, 79, 79)},
451 {"dim gray" , PALETTERGB (105,105,105)},
452 {"DimGray" , PALETTERGB (105,105,105)},
453 {"dim grey" , PALETTERGB (105,105,105)},
454 {"DimGrey" , PALETTERGB (105,105,105)},
455 {"slate gray" , PALETTERGB (112,128,144)},
456 {"SlateGray" , PALETTERGB (112,128,144)},
457 {"slate grey" , PALETTERGB (112,128,144)},
458 {"SlateGrey" , PALETTERGB (112,128,144)},
459 {"light slate gray" , PALETTERGB (119,136,153)},
460 {"LightSlateGray" , PALETTERGB (119,136,153)},
461 {"light slate grey" , PALETTERGB (119,136,153)},
462 {"LightSlateGrey" , PALETTERGB (119,136,153)},
463 {"gray" , PALETTERGB (190,190,190)},
464 {"grey" , PALETTERGB (190,190,190)},
465 {"light grey" , PALETTERGB (211,211,211)},
466 {"LightGrey" , PALETTERGB (211,211,211)},
467 {"light gray" , PALETTERGB (211,211,211)},
468 {"LightGray" , PALETTERGB (211,211,211)},
469 {"midnight blue" , PALETTERGB ( 25, 25,112)},
470 {"MidnightBlue" , PALETTERGB ( 25, 25,112)},
471 {"navy" , PALETTERGB ( 0, 0,128)},
472 {"navy blue" , PALETTERGB ( 0, 0,128)},
473 {"NavyBlue" , PALETTERGB ( 0, 0,128)},
474 {"cornflower blue" , PALETTERGB (100,149,237)},
475 {"CornflowerBlue" , PALETTERGB (100,149,237)},
476 {"dark slate blue" , PALETTERGB ( 72, 61,139)},
477 {"DarkSlateBlue" , PALETTERGB ( 72, 61,139)},
478 {"slate blue" , PALETTERGB (106, 90,205)},
479 {"SlateBlue" , PALETTERGB (106, 90,205)},
480 {"medium slate blue" , PALETTERGB (123,104,238)},
481 {"MediumSlateBlue" , PALETTERGB (123,104,238)},
482 {"light slate blue" , PALETTERGB (132,112,255)},
483 {"LightSlateBlue" , PALETTERGB (132,112,255)},
484 {"medium blue" , PALETTERGB ( 0, 0,205)},
485 {"MediumBlue" , PALETTERGB ( 0, 0,205)},
486 {"royal blue" , PALETTERGB ( 65,105,225)},
487 {"RoyalBlue" , PALETTERGB ( 65,105,225)},
488 {"blue" , PALETTERGB ( 0, 0,255)},
489 {"dodger blue" , PALETTERGB ( 30,144,255)},
490 {"DodgerBlue" , PALETTERGB ( 30,144,255)},
491 {"deep sky blue" , PALETTERGB ( 0,191,255)},
492 {"DeepSkyBlue" , PALETTERGB ( 0,191,255)},
493 {"sky blue" , PALETTERGB (135,206,235)},
494 {"SkyBlue" , PALETTERGB (135,206,235)},
495 {"light sky blue" , PALETTERGB (135,206,250)},
496 {"LightSkyBlue" , PALETTERGB (135,206,250)},
497 {"steel blue" , PALETTERGB ( 70,130,180)},
498 {"SteelBlue" , PALETTERGB ( 70,130,180)},
499 {"light steel blue" , PALETTERGB (176,196,222)},
500 {"LightSteelBlue" , PALETTERGB (176,196,222)},
501 {"light blue" , PALETTERGB (173,216,230)},
502 {"LightBlue" , PALETTERGB (173,216,230)},
503 {"powder blue" , PALETTERGB (176,224,230)},
504 {"PowderBlue" , PALETTERGB (176,224,230)},
505 {"pale turquoise" , PALETTERGB (175,238,238)},
506 {"PaleTurquoise" , PALETTERGB (175,238,238)},
507 {"dark turquoise" , PALETTERGB ( 0,206,209)},
508 {"DarkTurquoise" , PALETTERGB ( 0,206,209)},
509 {"medium turquoise" , PALETTERGB ( 72,209,204)},
510 {"MediumTurquoise" , PALETTERGB ( 72,209,204)},
511 {"turquoise" , PALETTERGB ( 64,224,208)},
512 {"cyan" , PALETTERGB ( 0,255,255)},
513 {"light cyan" , PALETTERGB (224,255,255)},
514 {"LightCyan" , PALETTERGB (224,255,255)},
515 {"cadet blue" , PALETTERGB ( 95,158,160)},
516 {"CadetBlue" , PALETTERGB ( 95,158,160)},
517 {"medium aquamarine" , PALETTERGB (102,205,170)},
518 {"MediumAquamarine" , PALETTERGB (102,205,170)},
519 {"aquamarine" , PALETTERGB (127,255,212)},
520 {"dark green" , PALETTERGB ( 0,100, 0)},
521 {"DarkGreen" , PALETTERGB ( 0,100, 0)},
522 {"dark olive green" , PALETTERGB ( 85,107, 47)},
523 {"DarkOliveGreen" , PALETTERGB ( 85,107, 47)},
524 {"dark sea green" , PALETTERGB (143,188,143)},
525 {"DarkSeaGreen" , PALETTERGB (143,188,143)},
526 {"sea green" , PALETTERGB ( 46,139, 87)},
527 {"SeaGreen" , PALETTERGB ( 46,139, 87)},
528 {"medium sea green" , PALETTERGB ( 60,179,113)},
529 {"MediumSeaGreen" , PALETTERGB ( 60,179,113)},
530 {"light sea green" , PALETTERGB ( 32,178,170)},
531 {"LightSeaGreen" , PALETTERGB ( 32,178,170)},
532 {"pale green" , PALETTERGB (152,251,152)},
533 {"PaleGreen" , PALETTERGB (152,251,152)},
534 {"spring green" , PALETTERGB ( 0,255,127)},
535 {"SpringGreen" , PALETTERGB ( 0,255,127)},
536 {"lawn green" , PALETTERGB (124,252, 0)},
537 {"LawnGreen" , PALETTERGB (124,252, 0)},
538 {"green" , PALETTERGB ( 0,255, 0)},
539 {"chartreuse" , PALETTERGB (127,255, 0)},
540 {"medium spring green" , PALETTERGB ( 0,250,154)},
541 {"MediumSpringGreen" , PALETTERGB ( 0,250,154)},
542 {"green yellow" , PALETTERGB (173,255, 47)},
543 {"GreenYellow" , PALETTERGB (173,255, 47)},
544 {"lime green" , PALETTERGB ( 50,205, 50)},
545 {"LimeGreen" , PALETTERGB ( 50,205, 50)},
546 {"yellow green" , PALETTERGB (154,205, 50)},
547 {"YellowGreen" , PALETTERGB (154,205, 50)},
548 {"forest green" , PALETTERGB ( 34,139, 34)},
549 {"ForestGreen" , PALETTERGB ( 34,139, 34)},
550 {"olive drab" , PALETTERGB (107,142, 35)},
551 {"OliveDrab" , PALETTERGB (107,142, 35)},
552 {"dark khaki" , PALETTERGB (189,183,107)},
553 {"DarkKhaki" , PALETTERGB (189,183,107)},
554 {"khaki" , PALETTERGB (240,230,140)},
555 {"pale goldenrod" , PALETTERGB (238,232,170)},
556 {"PaleGoldenrod" , PALETTERGB (238,232,170)},
557 {"light goldenrod yellow" , PALETTERGB (250,250,210)},
558 {"LightGoldenrodYellow" , PALETTERGB (250,250,210)},
559 {"light yellow" , PALETTERGB (255,255,224)},
560 {"LightYellow" , PALETTERGB (255,255,224)},
561 {"yellow" , PALETTERGB (255,255, 0)},
562 {"gold" , PALETTERGB (255,215, 0)},
563 {"light goldenrod" , PALETTERGB (238,221,130)},
564 {"LightGoldenrod" , PALETTERGB (238,221,130)},
565 {"goldenrod" , PALETTERGB (218,165, 32)},
566 {"dark goldenrod" , PALETTERGB (184,134, 11)},
567 {"DarkGoldenrod" , PALETTERGB (184,134, 11)},
568 {"rosy brown" , PALETTERGB (188,143,143)},
569 {"RosyBrown" , PALETTERGB (188,143,143)},
570 {"indian red" , PALETTERGB (205, 92, 92)},
571 {"IndianRed" , PALETTERGB (205, 92, 92)},
572 {"saddle brown" , PALETTERGB (139, 69, 19)},
573 {"SaddleBrown" , PALETTERGB (139, 69, 19)},
574 {"sienna" , PALETTERGB (160, 82, 45)},
575 {"peru" , PALETTERGB (205,133, 63)},
576 {"burlywood" , PALETTERGB (222,184,135)},
577 {"beige" , PALETTERGB (245,245,220)},
578 {"wheat" , PALETTERGB (245,222,179)},
579 {"sandy brown" , PALETTERGB (244,164, 96)},
580 {"SandyBrown" , PALETTERGB (244,164, 96)},
581 {"tan" , PALETTERGB (210,180,140)},
582 {"chocolate" , PALETTERGB (210,105, 30)},
583 {"firebrick" , PALETTERGB (178,34, 34)},
584 {"brown" , PALETTERGB (165,42, 42)},
585 {"dark salmon" , PALETTERGB (233,150,122)},
586 {"DarkSalmon" , PALETTERGB (233,150,122)},
587 {"salmon" , PALETTERGB (250,128,114)},
588 {"light salmon" , PALETTERGB (255,160,122)},
589 {"LightSalmon" , PALETTERGB (255,160,122)},
590 {"orange" , PALETTERGB (255,165, 0)},
591 {"dark orange" , PALETTERGB (255,140, 0)},
592 {"DarkOrange" , PALETTERGB (255,140, 0)},
593 {"coral" , PALETTERGB (255,127, 80)},
594 {"light coral" , PALETTERGB (240,128,128)},
595 {"LightCoral" , PALETTERGB (240,128,128)},
596 {"tomato" , PALETTERGB (255, 99, 71)},
597 {"orange red" , PALETTERGB (255, 69, 0)},
598 {"OrangeRed" , PALETTERGB (255, 69, 0)},
599 {"red" , PALETTERGB (255, 0, 0)},
600 {"hot pink" , PALETTERGB (255,105,180)},
601 {"HotPink" , PALETTERGB (255,105,180)},
602 {"deep pink" , PALETTERGB (255, 20,147)},
603 {"DeepPink" , PALETTERGB (255, 20,147)},
604 {"pink" , PALETTERGB (255,192,203)},
605 {"light pink" , PALETTERGB (255,182,193)},
606 {"LightPink" , PALETTERGB (255,182,193)},
607 {"pale violet red" , PALETTERGB (219,112,147)},
608 {"PaleVioletRed" , PALETTERGB (219,112,147)},
609 {"maroon" , PALETTERGB (176, 48, 96)},
610 {"medium violet red" , PALETTERGB (199, 21,133)},
611 {"MediumVioletRed" , PALETTERGB (199, 21,133)},
612 {"violet red" , PALETTERGB (208, 32,144)},
613 {"VioletRed" , PALETTERGB (208, 32,144)},
614 {"magenta" , PALETTERGB (255, 0,255)},
615 {"violet" , PALETTERGB (238,130,238)},
616 {"plum" , PALETTERGB (221,160,221)},
617 {"orchid" , PALETTERGB (218,112,214)},
618 {"medium orchid" , PALETTERGB (186, 85,211)},
619 {"MediumOrchid" , PALETTERGB (186, 85,211)},
620 {"dark orchid" , PALETTERGB (153, 50,204)},
621 {"DarkOrchid" , PALETTERGB (153, 50,204)},
622 {"dark violet" , PALETTERGB (148, 0,211)},
623 {"DarkViolet" , PALETTERGB (148, 0,211)},
624 {"blue violet" , PALETTERGB (138, 43,226)},
625 {"BlueViolet" , PALETTERGB (138, 43,226)},
626 {"purple" , PALETTERGB (160, 32,240)},
627 {"medium purple" , PALETTERGB (147,112,219)},
628 {"MediumPurple" , PALETTERGB (147,112,219)},
629 {"thistle" , PALETTERGB (216,191,216)},
630 {"gray0" , PALETTERGB ( 0, 0, 0)},
631 {"grey0" , PALETTERGB ( 0, 0, 0)},
632 {"dark grey" , PALETTERGB (169,169,169)},
633 {"DarkGrey" , PALETTERGB (169,169,169)},
634 {"dark gray" , PALETTERGB (169,169,169)},
635 {"DarkGray" , PALETTERGB (169,169,169)},
636 {"dark blue" , PALETTERGB ( 0, 0,139)},
637 {"DarkBlue" , PALETTERGB ( 0, 0,139)},
638 {"dark cyan" , PALETTERGB ( 0,139,139)},
639 {"DarkCyan" , PALETTERGB ( 0,139,139)},
640 {"dark magenta" , PALETTERGB (139, 0,139)},
641 {"DarkMagenta" , PALETTERGB (139, 0,139)},
642 {"dark red" , PALETTERGB (139, 0, 0)},
643 {"DarkRed" , PALETTERGB (139, 0, 0)},
644 {"light green" , PALETTERGB (144,238,144)},
645 {"LightGreen" , PALETTERGB (144,238,144)},
648 DEFUN ("w32-default-color-map", Fw32_default_color_map, Sw32_default_color_map,
649 0, 0, 0, doc: /* Return the default color map. */)
650 (void)
652 int i;
653 colormap_t *pc = w32_color_map;
654 Lisp_Object cmap;
656 BLOCK_INPUT;
658 cmap = Qnil;
660 for (i = 0; i < sizeof (w32_color_map) / sizeof (w32_color_map[0]);
661 pc++, i++)
662 cmap = Fcons (Fcons (build_string (pc->name),
663 make_number (pc->colorref)),
664 cmap);
666 UNBLOCK_INPUT;
668 return (cmap);
671 static Lisp_Object
672 w32_color_map_lookup (char *colorname)
674 Lisp_Object tail, ret = Qnil;
676 BLOCK_INPUT;
678 for (tail = Vw32_color_map; CONSP (tail); tail = XCDR (tail))
680 register Lisp_Object elt, tem;
682 elt = XCAR (tail);
683 if (!CONSP (elt)) continue;
685 tem = Fcar (elt);
687 if (lstrcmpi (SDATA (tem), colorname) == 0)
689 ret = Fcdr (elt);
690 break;
693 QUIT;
697 UNBLOCK_INPUT;
699 return ret;
703 static void
704 add_system_logical_colors_to_map (Lisp_Object *system_colors)
706 HKEY colors_key;
708 /* Other registry operations are done with input blocked. */
709 BLOCK_INPUT;
711 /* Look for "Control Panel/Colors" under User and Machine registry
712 settings. */
713 if (RegOpenKeyEx (HKEY_CURRENT_USER, "Control Panel\\Colors", 0,
714 KEY_READ, &colors_key) == ERROR_SUCCESS
715 || RegOpenKeyEx (HKEY_LOCAL_MACHINE, "Control Panel\\Colors", 0,
716 KEY_READ, &colors_key) == ERROR_SUCCESS)
718 /* List all keys. */
719 char color_buffer[64];
720 char full_name_buffer[MAX_PATH + SYSTEM_COLOR_PREFIX_LEN];
721 int index = 0;
722 DWORD name_size, color_size;
723 char *name_buffer = full_name_buffer + SYSTEM_COLOR_PREFIX_LEN;
725 name_size = sizeof (full_name_buffer) - SYSTEM_COLOR_PREFIX_LEN;
726 color_size = sizeof (color_buffer);
728 strcpy (full_name_buffer, SYSTEM_COLOR_PREFIX);
730 while (RegEnumValueA (colors_key, index, name_buffer, &name_size,
731 NULL, NULL, color_buffer, &color_size)
732 == ERROR_SUCCESS)
734 int r, g, b;
735 if (sscanf (color_buffer, " %u %u %u", &r, &g, &b) == 3)
736 *system_colors = Fcons (Fcons (build_string (full_name_buffer),
737 make_number (RGB (r, g, b))),
738 *system_colors);
740 name_size = sizeof (full_name_buffer) - SYSTEM_COLOR_PREFIX_LEN;
741 color_size = sizeof (color_buffer);
742 index++;
744 RegCloseKey (colors_key);
747 UNBLOCK_INPUT;
751 static Lisp_Object
752 x_to_w32_color (char * colorname)
754 register Lisp_Object ret = Qnil;
756 BLOCK_INPUT;
758 if (colorname[0] == '#')
760 /* Could be an old-style RGB Device specification. */
761 char *color;
762 int size;
763 color = colorname + 1;
765 size = strlen (color);
766 if (size == 3 || size == 6 || size == 9 || size == 12)
768 UINT colorval;
769 int i, pos;
770 pos = 0;
771 size /= 3;
772 colorval = 0;
774 for (i = 0; i < 3; i++)
776 char *end;
777 char t;
778 unsigned long value;
780 /* The check for 'x' in the following conditional takes into
781 account the fact that strtol allows a "0x" in front of
782 our numbers, and we don't. */
783 if (!isxdigit (color[0]) || color[1] == 'x')
784 break;
785 t = color[size];
786 color[size] = '\0';
787 value = strtoul (color, &end, 16);
788 color[size] = t;
789 if (errno == ERANGE || end - color != size)
790 break;
791 switch (size)
793 case 1:
794 value = value * 0x10;
795 break;
796 case 2:
797 break;
798 case 3:
799 value /= 0x10;
800 break;
801 case 4:
802 value /= 0x100;
803 break;
805 colorval |= (value << pos);
806 pos += 0x8;
807 if (i == 2)
809 UNBLOCK_INPUT;
810 XSETINT (ret, colorval);
811 return ret;
813 color = end;
817 else if (strnicmp (colorname, "rgb:", 4) == 0)
819 char *color;
820 UINT colorval;
821 int i, pos;
822 pos = 0;
824 colorval = 0;
825 color = colorname + 4;
826 for (i = 0; i < 3; i++)
828 char *end;
829 unsigned long value;
831 /* The check for 'x' in the following conditional takes into
832 account the fact that strtol allows a "0x" in front of
833 our numbers, and we don't. */
834 if (!isxdigit (color[0]) || color[1] == 'x')
835 break;
836 value = strtoul (color, &end, 16);
837 if (errno == ERANGE)
838 break;
839 switch (end - color)
841 case 1:
842 value = value * 0x10 + value;
843 break;
844 case 2:
845 break;
846 case 3:
847 value /= 0x10;
848 break;
849 case 4:
850 value /= 0x100;
851 break;
852 default:
853 value = ULONG_MAX;
855 if (value == ULONG_MAX)
856 break;
857 colorval |= (value << pos);
858 pos += 0x8;
859 if (i == 2)
861 if (*end != '\0')
862 break;
863 UNBLOCK_INPUT;
864 XSETINT (ret, colorval);
865 return ret;
867 if (*end != '/')
868 break;
869 color = end + 1;
872 else if (strnicmp (colorname, "rgbi:", 5) == 0)
874 /* This is an RGB Intensity specification. */
875 char *color;
876 UINT colorval;
877 int i, pos;
878 pos = 0;
880 colorval = 0;
881 color = colorname + 5;
882 for (i = 0; i < 3; i++)
884 char *end;
885 double value;
886 UINT val;
888 value = strtod (color, &end);
889 if (errno == ERANGE)
890 break;
891 if (value < 0.0 || value > 1.0)
892 break;
893 val = (UINT)(0x100 * value);
894 /* We used 0x100 instead of 0xFF to give a continuous
895 range between 0.0 and 1.0 inclusive. The next statement
896 fixes the 1.0 case. */
897 if (val == 0x100)
898 val = 0xFF;
899 colorval |= (val << pos);
900 pos += 0x8;
901 if (i == 2)
903 if (*end != '\0')
904 break;
905 UNBLOCK_INPUT;
906 XSETINT (ret, colorval);
907 return ret;
909 if (*end != '/')
910 break;
911 color = end + 1;
914 /* I am not going to attempt to handle any of the CIE color schemes
915 or TekHVC, since I don't know the algorithms for conversion to
916 RGB. */
918 /* If we fail to lookup the color name in w32_color_map, then check the
919 colorname to see if it can be crudely approximated: If the X color
920 ends in a number (e.g., "darkseagreen2"), strip the number and
921 return the result of looking up the base color name. */
922 ret = w32_color_map_lookup (colorname);
923 if (NILP (ret))
925 int len = strlen (colorname);
927 if (isdigit (colorname[len - 1]))
929 char *ptr, *approx = alloca (len + 1);
931 strcpy (approx, colorname);
932 ptr = &approx[len - 1];
933 while (ptr > approx && isdigit (*ptr))
934 *ptr-- = '\0';
936 ret = w32_color_map_lookup (approx);
940 UNBLOCK_INPUT;
941 return ret;
944 void
945 w32_regenerate_palette (FRAME_PTR f)
947 struct w32_palette_entry * list;
948 LOGPALETTE * log_palette;
949 HPALETTE new_palette;
950 int i;
952 /* don't bother trying to create palette if not supported */
953 if (! FRAME_W32_DISPLAY_INFO (f)->has_palette)
954 return;
956 log_palette = (LOGPALETTE *)
957 alloca (sizeof (LOGPALETTE) +
958 FRAME_W32_DISPLAY_INFO (f)->num_colors * sizeof (PALETTEENTRY));
959 log_palette->palVersion = 0x300;
960 log_palette->palNumEntries = FRAME_W32_DISPLAY_INFO (f)->num_colors;
962 list = FRAME_W32_DISPLAY_INFO (f)->color_list;
963 for (i = 0;
964 i < FRAME_W32_DISPLAY_INFO (f)->num_colors;
965 i++, list = list->next)
966 log_palette->palPalEntry[i] = list->entry;
968 new_palette = CreatePalette (log_palette);
970 enter_crit ();
972 if (FRAME_W32_DISPLAY_INFO (f)->palette)
973 DeleteObject (FRAME_W32_DISPLAY_INFO (f)->palette);
974 FRAME_W32_DISPLAY_INFO (f)->palette = new_palette;
976 /* Realize display palette and garbage all frames. */
977 release_frame_dc (f, get_frame_dc (f));
979 leave_crit ();
982 #define W32_COLOR(pe) RGB (pe.peRed, pe.peGreen, pe.peBlue)
983 #define SET_W32_COLOR(pe, color) \
984 do \
986 pe.peRed = GetRValue (color); \
987 pe.peGreen = GetGValue (color); \
988 pe.peBlue = GetBValue (color); \
989 pe.peFlags = 0; \
990 } while (0)
992 #if 0
993 /* Keep these around in case we ever want to track color usage. */
994 void
995 w32_map_color (FRAME_PTR f, COLORREF color)
997 struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
999 if (NILP (Vw32_enable_palette))
1000 return;
1002 /* check if color is already mapped */
1003 while (list)
1005 if (W32_COLOR (list->entry) == color)
1007 ++list->refcount;
1008 return;
1010 list = list->next;
1013 /* not already mapped, so add to list and recreate Windows palette */
1014 list = (struct w32_palette_entry *)
1015 xmalloc (sizeof (struct w32_palette_entry));
1016 SET_W32_COLOR (list->entry, color);
1017 list->refcount = 1;
1018 list->next = FRAME_W32_DISPLAY_INFO (f)->color_list;
1019 FRAME_W32_DISPLAY_INFO (f)->color_list = list;
1020 FRAME_W32_DISPLAY_INFO (f)->num_colors++;
1022 /* set flag that palette must be regenerated */
1023 FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
1026 void
1027 w32_unmap_color (FRAME_PTR f, COLORREF color)
1029 struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
1030 struct w32_palette_entry **prev = &FRAME_W32_DISPLAY_INFO (f)->color_list;
1032 if (NILP (Vw32_enable_palette))
1033 return;
1035 /* check if color is already mapped */
1036 while (list)
1038 if (W32_COLOR (list->entry) == color)
1040 if (--list->refcount == 0)
1042 *prev = list->next;
1043 xfree (list);
1044 FRAME_W32_DISPLAY_INFO (f)->num_colors--;
1045 break;
1047 else
1048 return;
1050 prev = &list->next;
1051 list = list->next;
1054 /* set flag that palette must be regenerated */
1055 FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
1057 #endif
1060 /* Gamma-correct COLOR on frame F. */
1062 void
1063 gamma_correct (struct frame *f, COLORREF *color)
1065 if (f->gamma)
1067 *color = PALETTERGB (
1068 pow (GetRValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
1069 pow (GetGValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
1070 pow (GetBValue (*color) / 255.0, f->gamma) * 255.0 + 0.5);
1075 /* Decide if color named COLOR is valid for the display associated with
1076 the selected frame; if so, return the rgb values in COLOR_DEF.
1077 If ALLOC is nonzero, allocate a new colormap cell. */
1080 w32_defined_color (FRAME_PTR f, char *color, XColor *color_def, int alloc)
1082 register Lisp_Object tem;
1083 COLORREF w32_color_ref;
1085 tem = x_to_w32_color (color);
1087 if (!NILP (tem))
1089 if (f)
1091 /* Apply gamma correction. */
1092 w32_color_ref = XUINT (tem);
1093 gamma_correct (f, &w32_color_ref);
1094 XSETINT (tem, w32_color_ref);
1097 /* Map this color to the palette if it is enabled. */
1098 if (!NILP (Vw32_enable_palette))
1100 struct w32_palette_entry * entry =
1101 one_w32_display_info.color_list;
1102 struct w32_palette_entry ** prev =
1103 &one_w32_display_info.color_list;
1105 /* check if color is already mapped */
1106 while (entry)
1108 if (W32_COLOR (entry->entry) == XUINT (tem))
1109 break;
1110 prev = &entry->next;
1111 entry = entry->next;
1114 if (entry == NULL && alloc)
1116 /* not already mapped, so add to list */
1117 entry = (struct w32_palette_entry *)
1118 xmalloc (sizeof (struct w32_palette_entry));
1119 SET_W32_COLOR (entry->entry, XUINT (tem));
1120 entry->next = NULL;
1121 *prev = entry;
1122 one_w32_display_info.num_colors++;
1124 /* set flag that palette must be regenerated */
1125 one_w32_display_info.regen_palette = TRUE;
1128 /* Ensure COLORREF value is snapped to nearest color in (default)
1129 palette by simulating the PALETTERGB macro. This works whether
1130 or not the display device has a palette. */
1131 w32_color_ref = XUINT (tem) | 0x2000000;
1133 color_def->pixel = w32_color_ref;
1134 color_def->red = GetRValue (w32_color_ref) * 256;
1135 color_def->green = GetGValue (w32_color_ref) * 256;
1136 color_def->blue = GetBValue (w32_color_ref) * 256;
1138 return 1;
1140 else
1142 return 0;
1146 /* Given a string ARG naming a color, compute a pixel value from it
1147 suitable for screen F.
1148 If F is not a color screen, return DEF (default) regardless of what
1149 ARG says. */
1152 x_decode_color (FRAME_PTR f, Lisp_Object arg, int def)
1154 XColor cdef;
1156 CHECK_STRING (arg);
1158 if (strcmp (SDATA (arg), "black") == 0)
1159 return BLACK_PIX_DEFAULT (f);
1160 else if (strcmp (SDATA (arg), "white") == 0)
1161 return WHITE_PIX_DEFAULT (f);
1163 if ((FRAME_W32_DISPLAY_INFO (f)->n_planes * FRAME_W32_DISPLAY_INFO (f)->n_cbits) == 1)
1164 return def;
1166 /* w32_defined_color is responsible for coping with failures
1167 by looking for a near-miss. */
1168 if (w32_defined_color (f, SDATA (arg), &cdef, 1))
1169 return cdef.pixel;
1171 /* defined_color failed; return an ultimate default. */
1172 return def;
1177 /* Functions called only from `x_set_frame_param'
1178 to set individual parameters.
1180 If FRAME_W32_WINDOW (f) is 0,
1181 the frame is being created and its window does not exist yet.
1182 In that case, just record the parameter's new value
1183 in the standard place; do not attempt to change the window. */
1185 void
1186 x_set_foreground_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1188 struct w32_output *x = f->output_data.w32;
1189 PIX_TYPE fg, old_fg;
1191 fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1192 old_fg = FRAME_FOREGROUND_PIXEL (f);
1193 FRAME_FOREGROUND_PIXEL (f) = fg;
1195 if (FRAME_W32_WINDOW (f) != 0)
1197 if (x->cursor_pixel == old_fg)
1199 x->cursor_pixel = fg;
1200 x->cursor_gc->background = fg;
1203 update_face_from_frame_parameter (f, Qforeground_color, arg);
1204 if (FRAME_VISIBLE_P (f))
1205 redraw_frame (f);
1209 void
1210 x_set_background_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1212 FRAME_BACKGROUND_PIXEL (f)
1213 = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
1215 if (FRAME_W32_WINDOW (f) != 0)
1217 SetWindowLong (FRAME_W32_WINDOW (f), WND_BACKGROUND_INDEX,
1218 FRAME_BACKGROUND_PIXEL (f));
1220 update_face_from_frame_parameter (f, Qbackground_color, arg);
1222 if (FRAME_VISIBLE_P (f))
1223 redraw_frame (f);
1227 void
1228 x_set_mouse_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1230 Cursor cursor, nontext_cursor, mode_cursor, hand_cursor;
1231 int count;
1232 int mask_color;
1234 if (!EQ (Qnil, arg))
1235 f->output_data.w32->mouse_pixel
1236 = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1237 mask_color = FRAME_BACKGROUND_PIXEL (f);
1239 /* Don't let pointers be invisible. */
1240 if (mask_color == f->output_data.w32->mouse_pixel
1241 && mask_color == FRAME_BACKGROUND_PIXEL (f))
1242 f->output_data.w32->mouse_pixel = FRAME_FOREGROUND_PIXEL (f);
1244 #if 0 /* TODO : Mouse cursor customization. */
1245 BLOCK_INPUT;
1247 /* It's not okay to crash if the user selects a screwy cursor. */
1248 count = x_catch_errors (FRAME_W32_DISPLAY (f));
1250 if (!EQ (Qnil, Vx_pointer_shape))
1252 CHECK_NUMBER (Vx_pointer_shape);
1253 cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XINT (Vx_pointer_shape));
1255 else
1256 cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
1257 x_check_errors (FRAME_W32_DISPLAY (f), "bad text pointer cursor: %s");
1259 if (!EQ (Qnil, Vx_nontext_pointer_shape))
1261 CHECK_NUMBER (Vx_nontext_pointer_shape);
1262 nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1263 XINT (Vx_nontext_pointer_shape));
1265 else
1266 nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_left_ptr);
1267 x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
1269 if (!EQ (Qnil, Vx_hourglass_pointer_shape))
1271 CHECK_NUMBER (Vx_hourglass_pointer_shape);
1272 hourglass_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1273 XINT (Vx_hourglass_pointer_shape));
1275 else
1276 hourglass_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_watch);
1277 x_check_errors (FRAME_W32_DISPLAY (f), "bad busy pointer cursor: %s");
1279 x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
1280 if (!EQ (Qnil, Vx_mode_pointer_shape))
1282 CHECK_NUMBER (Vx_mode_pointer_shape);
1283 mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1284 XINT (Vx_mode_pointer_shape));
1286 else
1287 mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
1288 x_check_errors (FRAME_W32_DISPLAY (f), "bad modeline pointer cursor: %s");
1290 if (!EQ (Qnil, Vx_sensitive_text_pointer_shape))
1292 CHECK_NUMBER (Vx_sensitive_text_pointer_shape);
1293 hand_cursor
1294 = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1295 XINT (Vx_sensitive_text_pointer_shape));
1297 else
1298 hand_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_crosshair);
1300 if (!NILP (Vx_window_horizontal_drag_shape))
1302 CHECK_NUMBER (Vx_window_horizontal_drag_shape);
1303 horizontal_drag_cursor
1304 = XCreateFontCursor (FRAME_X_DISPLAY (f),
1305 XINT (Vx_window_horizontal_drag_shape));
1307 else
1308 horizontal_drag_cursor
1309 = XCreateFontCursor (FRAME_X_DISPLAY (f), XC_sb_h_double_arrow);
1311 /* Check and report errors with the above calls. */
1312 x_check_errors (FRAME_W32_DISPLAY (f), "can't set cursor shape: %s");
1313 x_uncatch_errors (FRAME_W32_DISPLAY (f), count);
1316 XColor fore_color, back_color;
1318 fore_color.pixel = f->output_data.w32->mouse_pixel;
1319 back_color.pixel = mask_color;
1320 XQueryColor (FRAME_W32_DISPLAY (f),
1321 DefaultColormap (FRAME_W32_DISPLAY (f),
1322 DefaultScreen (FRAME_W32_DISPLAY (f))),
1323 &fore_color);
1324 XQueryColor (FRAME_W32_DISPLAY (f),
1325 DefaultColormap (FRAME_W32_DISPLAY (f),
1326 DefaultScreen (FRAME_W32_DISPLAY (f))),
1327 &back_color);
1328 XRecolorCursor (FRAME_W32_DISPLAY (f), cursor,
1329 &fore_color, &back_color);
1330 XRecolorCursor (FRAME_W32_DISPLAY (f), nontext_cursor,
1331 &fore_color, &back_color);
1332 XRecolorCursor (FRAME_W32_DISPLAY (f), mode_cursor,
1333 &fore_color, &back_color);
1334 XRecolorCursor (FRAME_W32_DISPLAY (f), hand_cursor,
1335 &fore_color, &back_color);
1336 XRecolorCursor (FRAME_W32_DISPLAY (f), hourglass_cursor,
1337 &fore_color, &back_color);
1340 if (FRAME_W32_WINDOW (f) != 0)
1341 XDefineCursor (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), cursor);
1343 if (cursor != f->output_data.w32->text_cursor && f->output_data.w32->text_cursor != 0)
1344 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->text_cursor);
1345 f->output_data.w32->text_cursor = cursor;
1347 if (nontext_cursor != f->output_data.w32->nontext_cursor
1348 && f->output_data.w32->nontext_cursor != 0)
1349 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->nontext_cursor);
1350 f->output_data.w32->nontext_cursor = nontext_cursor;
1352 if (hourglass_cursor != f->output_data.w32->hourglass_cursor
1353 && f->output_data.w32->hourglass_cursor != 0)
1354 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->hourglass_cursor);
1355 f->output_data.w32->hourglass_cursor = hourglass_cursor;
1357 if (mode_cursor != f->output_data.w32->modeline_cursor
1358 && f->output_data.w32->modeline_cursor != 0)
1359 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->modeline_cursor);
1360 f->output_data.w32->modeline_cursor = mode_cursor;
1362 if (hand_cursor != f->output_data.w32->hand_cursor
1363 && f->output_data.w32->hand_cursor != 0)
1364 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->hand_cursor);
1365 f->output_data.w32->hand_cursor = hand_cursor;
1367 XFlush (FRAME_W32_DISPLAY (f));
1368 UNBLOCK_INPUT;
1370 update_face_from_frame_parameter (f, Qmouse_color, arg);
1371 #endif /* TODO */
1374 void
1375 x_set_cursor_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1377 unsigned long fore_pixel, pixel;
1379 if (!NILP (Vx_cursor_fore_pixel))
1380 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
1381 WHITE_PIX_DEFAULT (f));
1382 else
1383 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
1385 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1387 /* Make sure that the cursor color differs from the background color. */
1388 if (pixel == FRAME_BACKGROUND_PIXEL (f))
1390 pixel = f->output_data.w32->mouse_pixel;
1391 if (pixel == fore_pixel)
1392 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
1395 f->output_data.w32->cursor_foreground_pixel = fore_pixel;
1396 f->output_data.w32->cursor_pixel = pixel;
1398 if (FRAME_W32_WINDOW (f) != 0)
1400 BLOCK_INPUT;
1401 /* Update frame's cursor_gc. */
1402 f->output_data.w32->cursor_gc->foreground = fore_pixel;
1403 f->output_data.w32->cursor_gc->background = pixel;
1405 UNBLOCK_INPUT;
1407 if (FRAME_VISIBLE_P (f))
1409 x_update_cursor (f, 0);
1410 x_update_cursor (f, 1);
1414 update_face_from_frame_parameter (f, Qcursor_color, arg);
1417 /* Set the border-color of frame F to pixel value PIX.
1418 Note that this does not fully take effect if done before
1419 F has a window. */
1421 void
1422 x_set_border_pixel (struct frame *f, int pix)
1425 f->output_data.w32->border_pixel = pix;
1427 if (FRAME_W32_WINDOW (f) != 0 && f->border_width > 0)
1429 if (FRAME_VISIBLE_P (f))
1430 redraw_frame (f);
1434 /* Set the border-color of frame F to value described by ARG.
1435 ARG can be a string naming a color.
1436 The border-color is used for the border that is drawn by the server.
1437 Note that this does not fully take effect if done before
1438 F has a window; it must be redone when the window is created. */
1440 void
1441 x_set_border_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1443 int pix;
1445 CHECK_STRING (arg);
1446 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1447 x_set_border_pixel (f, pix);
1448 update_face_from_frame_parameter (f, Qborder_color, arg);
1452 void
1453 x_set_cursor_type (FRAME_PTR f, Lisp_Object arg, Lisp_Object oldval)
1455 set_frame_cursor_types (f, arg);
1457 /* Make sure the cursor gets redrawn. */
1458 cursor_type_changed = 1;
1461 void
1462 x_set_icon_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1464 int result;
1466 if (NILP (arg) && NILP (oldval))
1467 return;
1469 if (STRINGP (arg) && STRINGP (oldval)
1470 && EQ (Fstring_equal (oldval, arg), Qt))
1471 return;
1473 if (SYMBOLP (arg) && SYMBOLP (oldval) && EQ (arg, oldval))
1474 return;
1476 BLOCK_INPUT;
1478 result = x_bitmap_icon (f, arg);
1479 if (result)
1481 UNBLOCK_INPUT;
1482 error ("No icon window available");
1485 UNBLOCK_INPUT;
1488 void
1489 x_set_icon_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1491 if (STRINGP (arg))
1493 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1494 return;
1496 else if (!NILP (arg) || NILP (oldval))
1497 return;
1499 f->icon_name = arg;
1501 #if 0
1502 if (f->output_data.w32->icon_bitmap != 0)
1503 return;
1505 BLOCK_INPUT;
1507 result = x_text_icon (f,
1508 SSDATA ((!NILP (f->icon_name)
1509 ? f->icon_name
1510 : !NILP (f->title)
1511 ? f->title
1512 : f->name)));
1514 if (result)
1516 UNBLOCK_INPUT;
1517 error ("No icon window available");
1520 /* If the window was unmapped (and its icon was mapped),
1521 the new icon is not mapped, so map the window in its stead. */
1522 if (FRAME_VISIBLE_P (f))
1524 #ifdef USE_X_TOOLKIT
1525 XtPopup (f->output_data.w32->widget, XtGrabNone);
1526 #endif
1527 XMapWindow (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f));
1530 XFlush (FRAME_W32_DISPLAY (f));
1531 UNBLOCK_INPUT;
1532 #endif
1536 void
1537 x_set_menu_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1539 int nlines;
1540 int olines = FRAME_MENU_BAR_LINES (f);
1542 /* Right now, menu bars don't work properly in minibuf-only frames;
1543 most of the commands try to apply themselves to the minibuffer
1544 frame itself, and get an error because you can't switch buffers
1545 in or split the minibuffer window. */
1546 if (FRAME_MINIBUF_ONLY_P (f))
1547 return;
1549 if (INTEGERP (value))
1550 nlines = XINT (value);
1551 else
1552 nlines = 0;
1554 FRAME_MENU_BAR_LINES (f) = 0;
1555 if (nlines)
1556 FRAME_EXTERNAL_MENU_BAR (f) = 1;
1557 else
1559 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
1560 free_frame_menubar (f);
1561 FRAME_EXTERNAL_MENU_BAR (f) = 0;
1563 /* Adjust the frame size so that the client (text) dimensions
1564 remain the same. This depends on FRAME_EXTERNAL_MENU_BAR being
1565 set correctly. */
1566 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
1567 do_pending_window_change (0);
1569 adjust_glyphs (f);
1573 /* Set the number of lines used for the tool bar of frame F to VALUE.
1574 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
1575 is the old number of tool bar lines. This function changes the
1576 height of all windows on frame F to match the new tool bar height.
1577 The frame's height doesn't change. */
1579 void
1580 x_set_tool_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1582 int delta, nlines, root_height;
1583 Lisp_Object root_window;
1585 /* Treat tool bars like menu bars. */
1586 if (FRAME_MINIBUF_ONLY_P (f))
1587 return;
1589 /* Use VALUE only if an integer >= 0. */
1590 if (INTEGERP (value) && XINT (value) >= 0)
1591 nlines = XFASTINT (value);
1592 else
1593 nlines = 0;
1595 /* Make sure we redisplay all windows in this frame. */
1596 ++windows_or_buffers_changed;
1598 delta = nlines - FRAME_TOOL_BAR_LINES (f);
1600 /* Don't resize the tool-bar to more than we have room for. */
1601 root_window = FRAME_ROOT_WINDOW (f);
1602 root_height = WINDOW_TOTAL_LINES (XWINDOW (root_window));
1603 if (root_height - delta < 1)
1605 delta = root_height - 1;
1606 nlines = FRAME_TOOL_BAR_LINES (f) + delta;
1609 FRAME_TOOL_BAR_LINES (f) = nlines;
1610 change_window_heights (root_window, delta);
1611 adjust_glyphs (f);
1613 /* We also have to make sure that the internal border at the top of
1614 the frame, below the menu bar or tool bar, is redrawn when the
1615 tool bar disappears. This is so because the internal border is
1616 below the tool bar if one is displayed, but is below the menu bar
1617 if there isn't a tool bar. The tool bar draws into the area
1618 below the menu bar. */
1619 if (FRAME_W32_WINDOW (f) && FRAME_TOOL_BAR_LINES (f) == 0)
1621 clear_frame (f);
1622 clear_current_matrices (f);
1625 /* If the tool bar gets smaller, the internal border below it
1626 has to be cleared. It was formerly part of the display
1627 of the larger tool bar, and updating windows won't clear it. */
1628 if (delta < 0)
1630 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
1631 int width = FRAME_PIXEL_WIDTH (f);
1632 int y = nlines * FRAME_LINE_HEIGHT (f);
1634 BLOCK_INPUT;
1636 HDC hdc = get_frame_dc (f);
1637 w32_clear_area (f, hdc, 0, y, width, height);
1638 release_frame_dc (f, hdc);
1640 UNBLOCK_INPUT;
1642 if (WINDOWP (f->tool_bar_window))
1643 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
1648 /* Change the name of frame F to NAME. If NAME is nil, set F's name to
1649 w32_id_name.
1651 If EXPLICIT is non-zero, that indicates that lisp code is setting the
1652 name; if NAME is a string, set F's name to NAME and set
1653 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1655 If EXPLICIT is zero, that indicates that Emacs redisplay code is
1656 suggesting a new name, which lisp code should override; if
1657 F->explicit_name is set, ignore the new name; otherwise, set it. */
1659 void
1660 x_set_name (struct frame *f, Lisp_Object name, int explicit)
1662 /* Make sure that requests from lisp code override requests from
1663 Emacs redisplay code. */
1664 if (explicit)
1666 /* If we're switching from explicit to implicit, we had better
1667 update the mode lines and thereby update the title. */
1668 if (f->explicit_name && NILP (name))
1669 update_mode_lines = 1;
1671 f->explicit_name = ! NILP (name);
1673 else if (f->explicit_name)
1674 return;
1676 /* If NAME is nil, set the name to the w32_id_name. */
1677 if (NILP (name))
1679 /* Check for no change needed in this very common case
1680 before we do any consing. */
1681 if (!strcmp (FRAME_W32_DISPLAY_INFO (f)->w32_id_name,
1682 SDATA (f->name)))
1683 return;
1684 name = build_string (FRAME_W32_DISPLAY_INFO (f)->w32_id_name);
1686 else
1687 CHECK_STRING (name);
1689 /* Don't change the name if it's already NAME. */
1690 if (! NILP (Fstring_equal (name, f->name)))
1691 return;
1693 f->name = name;
1695 /* For setting the frame title, the title parameter should override
1696 the name parameter. */
1697 if (! NILP (f->title))
1698 name = f->title;
1700 if (FRAME_W32_WINDOW (f))
1702 if (STRING_MULTIBYTE (name))
1703 name = ENCODE_SYSTEM (name);
1705 BLOCK_INPUT;
1706 SetWindowText (FRAME_W32_WINDOW (f), SDATA (name));
1707 UNBLOCK_INPUT;
1711 /* This function should be called when the user's lisp code has
1712 specified a name for the frame; the name will override any set by the
1713 redisplay code. */
1714 void
1715 x_explicitly_set_name (FRAME_PTR f, Lisp_Object arg, Lisp_Object oldval)
1717 x_set_name (f, arg, 1);
1720 /* This function should be called by Emacs redisplay code to set the
1721 name; names set this way will never override names set by the user's
1722 lisp code. */
1723 void
1724 x_implicitly_set_name (FRAME_PTR f, Lisp_Object arg, Lisp_Object oldval)
1726 x_set_name (f, arg, 0);
1729 /* Change the title of frame F to NAME.
1730 If NAME is nil, use the frame name as the title. */
1732 void
1733 x_set_title (struct frame *f, Lisp_Object name, Lisp_Object old_name)
1735 /* Don't change the title if it's already NAME. */
1736 if (EQ (name, f->title))
1737 return;
1739 update_mode_lines = 1;
1741 f->title = name;
1743 if (NILP (name))
1744 name = f->name;
1746 if (FRAME_W32_WINDOW (f))
1748 if (STRING_MULTIBYTE (name))
1749 name = ENCODE_SYSTEM (name);
1751 BLOCK_INPUT;
1752 SetWindowText (FRAME_W32_WINDOW (f), SDATA (name));
1753 UNBLOCK_INPUT;
1757 void
1758 x_set_scroll_bar_default_width (struct frame *f)
1760 int wid = FRAME_COLUMN_WIDTH (f);
1762 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = GetSystemMetrics (SM_CXVSCROLL);
1763 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) +
1764 wid - 1) / wid;
1768 /* Subroutines for creating a frame. */
1770 Cursor
1771 w32_load_cursor (LPCTSTR name)
1773 /* Try first to load cursor from application resource. */
1774 Cursor cursor = LoadImage ((HINSTANCE) GetModuleHandle (NULL),
1775 name, IMAGE_CURSOR, 0, 0,
1776 LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_SHARED);
1777 if (!cursor)
1779 /* Then try to load a shared predefined cursor. */
1780 cursor = LoadImage (NULL, name, IMAGE_CURSOR, 0, 0,
1781 LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_SHARED);
1783 return cursor;
1786 static LRESULT CALLBACK w32_wnd_proc (HWND, UINT, WPARAM, LPARAM);
1788 static BOOL
1789 w32_init_class (HINSTANCE hinst)
1791 WNDCLASS wc;
1793 wc.style = CS_HREDRAW | CS_VREDRAW;
1794 wc.lpfnWndProc = (WNDPROC) w32_wnd_proc;
1795 wc.cbClsExtra = 0;
1796 wc.cbWndExtra = WND_EXTRA_BYTES;
1797 wc.hInstance = hinst;
1798 wc.hIcon = LoadIcon (hinst, EMACS_CLASS);
1799 wc.hCursor = w32_load_cursor (IDC_ARROW);
1800 wc.hbrBackground = NULL; /* GetStockObject (WHITE_BRUSH); */
1801 wc.lpszMenuName = NULL;
1802 wc.lpszClassName = EMACS_CLASS;
1804 return (RegisterClass (&wc));
1807 static HWND
1808 w32_createscrollbar (struct frame *f, struct scroll_bar * bar)
1810 return (CreateWindow ("SCROLLBAR", "", SBS_VERT | WS_CHILD | WS_VISIBLE,
1811 /* Position and size of scroll bar. */
1812 XINT (bar->left) + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
1813 XINT (bar->top),
1814 XINT (bar->width) - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
1815 XINT (bar->height),
1816 FRAME_W32_WINDOW (f),
1817 NULL,
1818 hinst,
1819 NULL));
1822 static void
1823 w32_createwindow (struct frame *f)
1825 HWND hwnd;
1826 RECT rect;
1827 Lisp_Object top = Qunbound;
1828 Lisp_Object left = Qunbound;
1829 struct w32_display_info *dpyinfo = &one_w32_display_info;
1831 rect.left = rect.top = 0;
1832 rect.right = FRAME_PIXEL_WIDTH (f);
1833 rect.bottom = FRAME_PIXEL_HEIGHT (f);
1835 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
1836 FRAME_EXTERNAL_MENU_BAR (f));
1838 /* Do first time app init */
1840 if (!hprevinst)
1842 w32_init_class (hinst);
1845 if (f->size_hint_flags & USPosition || f->size_hint_flags & PPosition)
1847 XSETINT (left, f->left_pos);
1848 XSETINT (top, f->top_pos);
1850 else if (EQ (left, Qunbound) && EQ (top, Qunbound))
1852 /* When called with RES_TYPE_NUMBER, w32_get_arg will return zero
1853 for anything that is not a number and is not Qunbound. */
1854 left = x_get_arg (dpyinfo, Qnil, Qleft, "left", "Left", RES_TYPE_NUMBER);
1855 top = x_get_arg (dpyinfo, Qnil, Qtop, "top", "Top", RES_TYPE_NUMBER);
1858 FRAME_W32_WINDOW (f) = hwnd
1859 = CreateWindow (EMACS_CLASS,
1860 f->namebuf,
1861 f->output_data.w32->dwStyle | WS_CLIPCHILDREN,
1862 EQ (left, Qunbound) ? CW_USEDEFAULT : XINT (left),
1863 EQ (top, Qunbound) ? CW_USEDEFAULT : XINT (top),
1864 rect.right - rect.left,
1865 rect.bottom - rect.top,
1866 NULL,
1867 NULL,
1868 hinst,
1869 NULL);
1871 if (hwnd)
1873 SetWindowLong (hwnd, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
1874 SetWindowLong (hwnd, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
1875 SetWindowLong (hwnd, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
1876 SetWindowLong (hwnd, WND_SCROLLBAR_INDEX, f->scroll_bar_actual_width);
1877 SetWindowLong (hwnd, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
1879 /* Enable drag-n-drop. */
1880 DragAcceptFiles (hwnd, TRUE);
1882 /* Do this to discard the default setting specified by our parent. */
1883 ShowWindow (hwnd, SW_HIDE);
1885 /* Update frame positions. */
1886 GetWindowRect (hwnd, &rect);
1887 f->left_pos = rect.left;
1888 f->top_pos = rect.top;
1892 static void
1893 my_post_msg (W32Msg * wmsg, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
1895 wmsg->msg.hwnd = hwnd;
1896 wmsg->msg.message = msg;
1897 wmsg->msg.wParam = wParam;
1898 wmsg->msg.lParam = lParam;
1899 wmsg->msg.time = GetMessageTime ();
1901 post_msg (wmsg);
1904 /* GetKeyState and MapVirtualKey on Windows 95 do not actually distinguish
1905 between left and right keys as advertised. We test for this
1906 support dynamically, and set a flag when the support is absent. If
1907 absent, we keep track of the left and right control and alt keys
1908 ourselves. This is particularly necessary on keyboards that rely
1909 upon the AltGr key, which is represented as having the left control
1910 and right alt keys pressed. For these keyboards, we need to know
1911 when the left alt key has been pressed in addition to the AltGr key
1912 so that we can properly support M-AltGr-key sequences (such as M-@
1913 on Swedish keyboards). */
1915 #define EMACS_LCONTROL 0
1916 #define EMACS_RCONTROL 1
1917 #define EMACS_LMENU 2
1918 #define EMACS_RMENU 3
1920 static int modifiers[4];
1921 static int modifiers_recorded;
1922 static int modifier_key_support_tested;
1924 static void
1925 test_modifier_support (unsigned int wparam)
1927 unsigned int l, r;
1929 if (wparam != VK_CONTROL && wparam != VK_MENU)
1930 return;
1931 if (wparam == VK_CONTROL)
1933 l = VK_LCONTROL;
1934 r = VK_RCONTROL;
1936 else
1938 l = VK_LMENU;
1939 r = VK_RMENU;
1941 if (!(GetKeyState (l) & 0x8000) && !(GetKeyState (r) & 0x8000))
1942 modifiers_recorded = 1;
1943 else
1944 modifiers_recorded = 0;
1945 modifier_key_support_tested = 1;
1948 static void
1949 record_keydown (unsigned int wparam, unsigned int lparam)
1951 int i;
1953 if (!modifier_key_support_tested)
1954 test_modifier_support (wparam);
1956 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
1957 return;
1959 if (wparam == VK_CONTROL)
1960 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
1961 else
1962 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
1964 modifiers[i] = 1;
1967 static void
1968 record_keyup (unsigned int wparam, unsigned int lparam)
1970 int i;
1972 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
1973 return;
1975 if (wparam == VK_CONTROL)
1976 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
1977 else
1978 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
1980 modifiers[i] = 0;
1983 /* Emacs can lose focus while a modifier key has been pressed. When
1984 it regains focus, be conservative and clear all modifiers since
1985 we cannot reconstruct the left and right modifier state. */
1986 static void
1987 reset_modifiers (void)
1989 SHORT ctrl, alt;
1991 if (GetFocus () == NULL)
1992 /* Emacs doesn't have keyboard focus. Do nothing. */
1993 return;
1995 ctrl = GetAsyncKeyState (VK_CONTROL);
1996 alt = GetAsyncKeyState (VK_MENU);
1998 if (!(ctrl & 0x08000))
1999 /* Clear any recorded control modifier state. */
2000 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
2002 if (!(alt & 0x08000))
2003 /* Clear any recorded alt modifier state. */
2004 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
2006 /* Update the state of all modifier keys, because modifiers used in
2007 hot-key combinations can get stuck on if Emacs loses focus as a
2008 result of a hot-key being pressed. */
2010 BYTE keystate[256];
2012 #define CURRENT_STATE(key) ((GetAsyncKeyState (key) & 0x8000) >> 8)
2014 GetKeyboardState (keystate);
2015 keystate[VK_SHIFT] = CURRENT_STATE (VK_SHIFT);
2016 keystate[VK_CONTROL] = CURRENT_STATE (VK_CONTROL);
2017 keystate[VK_LCONTROL] = CURRENT_STATE (VK_LCONTROL);
2018 keystate[VK_RCONTROL] = CURRENT_STATE (VK_RCONTROL);
2019 keystate[VK_MENU] = CURRENT_STATE (VK_MENU);
2020 keystate[VK_LMENU] = CURRENT_STATE (VK_LMENU);
2021 keystate[VK_RMENU] = CURRENT_STATE (VK_RMENU);
2022 keystate[VK_LWIN] = CURRENT_STATE (VK_LWIN);
2023 keystate[VK_RWIN] = CURRENT_STATE (VK_RWIN);
2024 keystate[VK_APPS] = CURRENT_STATE (VK_APPS);
2025 SetKeyboardState (keystate);
2029 /* Synchronize modifier state with what is reported with the current
2030 keystroke. Even if we cannot distinguish between left and right
2031 modifier keys, we know that, if no modifiers are set, then neither
2032 the left or right modifier should be set. */
2033 static void
2034 sync_modifiers (void)
2036 if (!modifiers_recorded)
2037 return;
2039 if (!(GetKeyState (VK_CONTROL) & 0x8000))
2040 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
2042 if (!(GetKeyState (VK_MENU) & 0x8000))
2043 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
2046 static int
2047 modifier_set (int vkey)
2049 if (vkey == VK_CAPITAL || vkey == VK_SCROLL)
2050 return (GetKeyState (vkey) & 0x1);
2051 if (!modifiers_recorded)
2052 return (GetKeyState (vkey) & 0x8000);
2054 switch (vkey)
2056 case VK_LCONTROL:
2057 return modifiers[EMACS_LCONTROL];
2058 case VK_RCONTROL:
2059 return modifiers[EMACS_RCONTROL];
2060 case VK_LMENU:
2061 return modifiers[EMACS_LMENU];
2062 case VK_RMENU:
2063 return modifiers[EMACS_RMENU];
2065 return (GetKeyState (vkey) & 0x8000);
2068 /* Convert between the modifier bits W32 uses and the modifier bits
2069 Emacs uses. */
2071 unsigned int
2072 w32_key_to_modifier (int key)
2074 Lisp_Object key_mapping;
2076 switch (key)
2078 case VK_LWIN:
2079 key_mapping = Vw32_lwindow_modifier;
2080 break;
2081 case VK_RWIN:
2082 key_mapping = Vw32_rwindow_modifier;
2083 break;
2084 case VK_APPS:
2085 key_mapping = Vw32_apps_modifier;
2086 break;
2087 case VK_SCROLL:
2088 key_mapping = Vw32_scroll_lock_modifier;
2089 break;
2090 default:
2091 key_mapping = Qnil;
2094 /* NB. This code runs in the input thread, asychronously to the lisp
2095 thread, so we must be careful to ensure access to lisp data is
2096 thread-safe. The following code is safe because the modifier
2097 variable values are updated atomically from lisp and symbols are
2098 not relocated by GC. Also, we don't have to worry about seeing GC
2099 markbits here. */
2100 if (EQ (key_mapping, Qhyper))
2101 return hyper_modifier;
2102 if (EQ (key_mapping, Qsuper))
2103 return super_modifier;
2104 if (EQ (key_mapping, Qmeta))
2105 return meta_modifier;
2106 if (EQ (key_mapping, Qalt))
2107 return alt_modifier;
2108 if (EQ (key_mapping, Qctrl))
2109 return ctrl_modifier;
2110 if (EQ (key_mapping, Qcontrol)) /* synonym for ctrl */
2111 return ctrl_modifier;
2112 if (EQ (key_mapping, Qshift))
2113 return shift_modifier;
2115 /* Don't generate any modifier if not explicitly requested. */
2116 return 0;
2119 static unsigned int
2120 w32_get_modifiers (void)
2122 return ((modifier_set (VK_SHIFT) ? shift_modifier : 0) |
2123 (modifier_set (VK_CONTROL) ? ctrl_modifier : 0) |
2124 (modifier_set (VK_LWIN) ? w32_key_to_modifier (VK_LWIN) : 0) |
2125 (modifier_set (VK_RWIN) ? w32_key_to_modifier (VK_RWIN) : 0) |
2126 (modifier_set (VK_APPS) ? w32_key_to_modifier (VK_APPS) : 0) |
2127 (modifier_set (VK_SCROLL) ? w32_key_to_modifier (VK_SCROLL) : 0) |
2128 (modifier_set (VK_MENU) ?
2129 ((NILP (Vw32_alt_is_meta)) ? alt_modifier : meta_modifier) : 0));
2132 /* We map the VK_* modifiers into console modifier constants
2133 so that we can use the same routines to handle both console
2134 and window input. */
2136 static int
2137 construct_console_modifiers (void)
2139 int mods;
2141 mods = 0;
2142 mods |= (modifier_set (VK_SHIFT)) ? SHIFT_PRESSED : 0;
2143 mods |= (modifier_set (VK_CAPITAL)) ? CAPSLOCK_ON : 0;
2144 mods |= (modifier_set (VK_SCROLL)) ? SCROLLLOCK_ON : 0;
2145 mods |= (modifier_set (VK_NUMLOCK)) ? NUMLOCK_ON : 0;
2146 mods |= (modifier_set (VK_LCONTROL)) ? LEFT_CTRL_PRESSED : 0;
2147 mods |= (modifier_set (VK_RCONTROL)) ? RIGHT_CTRL_PRESSED : 0;
2148 mods |= (modifier_set (VK_LMENU)) ? LEFT_ALT_PRESSED : 0;
2149 mods |= (modifier_set (VK_RMENU)) ? RIGHT_ALT_PRESSED : 0;
2150 mods |= (modifier_set (VK_LWIN)) ? LEFT_WIN_PRESSED : 0;
2151 mods |= (modifier_set (VK_RWIN)) ? RIGHT_WIN_PRESSED : 0;
2152 mods |= (modifier_set (VK_APPS)) ? APPS_PRESSED : 0;
2154 return mods;
2157 static int
2158 w32_get_key_modifiers (unsigned int wparam, unsigned int lparam)
2160 int mods;
2162 /* Convert to emacs modifiers. */
2163 mods = w32_kbd_mods_to_emacs (construct_console_modifiers (), wparam);
2165 return mods;
2168 unsigned int
2169 map_keypad_keys (unsigned int virt_key, unsigned int extended)
2171 if (virt_key < VK_CLEAR || virt_key > VK_DELETE)
2172 return virt_key;
2174 if (virt_key == VK_RETURN)
2175 return (extended ? VK_NUMPAD_ENTER : VK_RETURN);
2177 if (virt_key >= VK_PRIOR && virt_key <= VK_DOWN)
2178 return (!extended ? (VK_NUMPAD_PRIOR + (virt_key - VK_PRIOR)) : virt_key);
2180 if (virt_key == VK_INSERT || virt_key == VK_DELETE)
2181 return (!extended ? (VK_NUMPAD_INSERT + (virt_key - VK_INSERT)) : virt_key);
2183 if (virt_key == VK_CLEAR)
2184 return (!extended ? VK_NUMPAD_CLEAR : virt_key);
2186 return virt_key;
2189 /* List of special key combinations which w32 would normally capture,
2190 but Emacs should grab instead. Not directly visible to lisp, to
2191 simplify synchronization. Each item is an integer encoding a virtual
2192 key code and modifier combination to capture. */
2193 static Lisp_Object w32_grabbed_keys;
2195 #define HOTKEY(vk, mods) make_number (((vk) & 255) | ((mods) << 8))
2196 #define HOTKEY_ID(k) (XFASTINT (k) & 0xbfff)
2197 #define HOTKEY_VK_CODE(k) (XFASTINT (k) & 255)
2198 #define HOTKEY_MODIFIERS(k) (XFASTINT (k) >> 8)
2200 #define RAW_HOTKEY_ID(k) ((k) & 0xbfff)
2201 #define RAW_HOTKEY_VK_CODE(k) ((k) & 255)
2202 #define RAW_HOTKEY_MODIFIERS(k) ((k) >> 8)
2204 /* Register hot-keys for reserved key combinations when Emacs has
2205 keyboard focus, since this is the only way Emacs can receive key
2206 combinations like Alt-Tab which are used by the system. */
2208 static void
2209 register_hot_keys (HWND hwnd)
2211 Lisp_Object keylist;
2213 /* Use CONSP, since we are called asynchronously. */
2214 for (keylist = w32_grabbed_keys; CONSP (keylist); keylist = XCDR (keylist))
2216 Lisp_Object key = XCAR (keylist);
2218 /* Deleted entries get set to nil. */
2219 if (!INTEGERP (key))
2220 continue;
2222 RegisterHotKey (hwnd, HOTKEY_ID (key),
2223 HOTKEY_MODIFIERS (key), HOTKEY_VK_CODE (key));
2227 static void
2228 unregister_hot_keys (HWND hwnd)
2230 Lisp_Object keylist;
2232 for (keylist = w32_grabbed_keys; CONSP (keylist); keylist = XCDR (keylist))
2234 Lisp_Object key = XCAR (keylist);
2236 if (!INTEGERP (key))
2237 continue;
2239 UnregisterHotKey (hwnd, HOTKEY_ID (key));
2243 /* Main message dispatch loop. */
2245 static void
2246 w32_msg_pump (deferred_msg * msg_buf)
2248 MSG msg;
2249 int result;
2250 HWND focus_window;
2252 msh_mousewheel = RegisterWindowMessage (MSH_MOUSEWHEEL);
2254 while (GetMessage (&msg, NULL, 0, 0))
2256 if (msg.hwnd == NULL)
2258 switch (msg.message)
2260 case WM_NULL:
2261 /* Produced by complete_deferred_msg; just ignore. */
2262 break;
2263 case WM_EMACS_CREATEWINDOW:
2264 /* Initialize COM for this window. Even though we don't use it,
2265 some third party shell extensions can cause it to be used in
2266 system dialogs, which causes a crash if it is not initialized.
2267 This is a known bug in Windows, which was fixed long ago, but
2268 the patch for XP is not publically available until XP SP3,
2269 and older versions will never be patched. */
2270 CoInitialize (NULL);
2271 w32_createwindow ((struct frame *) msg.wParam);
2272 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2273 abort ();
2274 break;
2275 case WM_EMACS_SETLOCALE:
2276 SetThreadLocale (msg.wParam);
2277 /* Reply is not expected. */
2278 break;
2279 case WM_EMACS_SETKEYBOARDLAYOUT:
2280 result = (int) ActivateKeyboardLayout ((HKL) msg.wParam, 0);
2281 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
2282 result, 0))
2283 abort ();
2284 break;
2285 case WM_EMACS_REGISTER_HOT_KEY:
2286 focus_window = GetFocus ();
2287 if (focus_window != NULL)
2288 RegisterHotKey (focus_window,
2289 RAW_HOTKEY_ID (msg.wParam),
2290 RAW_HOTKEY_MODIFIERS (msg.wParam),
2291 RAW_HOTKEY_VK_CODE (msg.wParam));
2292 /* Reply is not expected. */
2293 break;
2294 case WM_EMACS_UNREGISTER_HOT_KEY:
2295 focus_window = GetFocus ();
2296 if (focus_window != NULL)
2297 UnregisterHotKey (focus_window, RAW_HOTKEY_ID (msg.wParam));
2298 /* Mark item as erased. NB: this code must be
2299 thread-safe. The next line is okay because the cons
2300 cell is never made into garbage and is not relocated by
2301 GC. */
2302 XSETCAR ((Lisp_Object) ((EMACS_INT) msg.lParam), Qnil);
2303 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2304 abort ();
2305 break;
2306 case WM_EMACS_TOGGLE_LOCK_KEY:
2308 int vk_code = (int) msg.wParam;
2309 int cur_state = (GetKeyState (vk_code) & 1);
2310 Lisp_Object new_state = (Lisp_Object) ((EMACS_INT) msg.lParam);
2312 /* NB: This code must be thread-safe. It is safe to
2313 call NILP because symbols are not relocated by GC,
2314 and pointer here is not touched by GC (so the markbit
2315 can't be set). Numbers are safe because they are
2316 immediate values. */
2317 if (NILP (new_state)
2318 || (NUMBERP (new_state)
2319 && ((XUINT (new_state)) & 1) != cur_state))
2321 one_w32_display_info.faked_key = vk_code;
2323 keybd_event ((BYTE) vk_code,
2324 (BYTE) MapVirtualKey (vk_code, 0),
2325 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2326 keybd_event ((BYTE) vk_code,
2327 (BYTE) MapVirtualKey (vk_code, 0),
2328 KEYEVENTF_EXTENDEDKEY | 0, 0);
2329 keybd_event ((BYTE) vk_code,
2330 (BYTE) MapVirtualKey (vk_code, 0),
2331 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2332 cur_state = !cur_state;
2334 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
2335 cur_state, 0))
2336 abort ();
2338 break;
2339 #ifdef MSG_DEBUG
2340 /* Broadcast messages make it here, so you need to be looking
2341 for something in particular for this to be useful. */
2342 default:
2343 DebPrint (("msg %x not expected by w32_msg_pump\n", msg.message));
2344 #endif
2347 else
2349 DispatchMessage (&msg);
2352 /* Exit nested loop when our deferred message has completed. */
2353 if (msg_buf->completed)
2354 break;
2358 deferred_msg * deferred_msg_head;
2360 static deferred_msg *
2361 find_deferred_msg (HWND hwnd, UINT msg)
2363 deferred_msg * item;
2365 /* Don't actually need synchronization for read access, since
2366 modification of single pointer is always atomic. */
2367 /* enter_crit (); */
2369 for (item = deferred_msg_head; item != NULL; item = item->next)
2370 if (item->w32msg.msg.hwnd == hwnd
2371 && item->w32msg.msg.message == msg)
2372 break;
2374 /* leave_crit (); */
2376 return item;
2379 static LRESULT
2380 send_deferred_msg (deferred_msg * msg_buf,
2381 HWND hwnd,
2382 UINT msg,
2383 WPARAM wParam,
2384 LPARAM lParam)
2386 /* Only input thread can send deferred messages. */
2387 if (GetCurrentThreadId () != dwWindowsThreadId)
2388 abort ();
2390 /* It is an error to send a message that is already deferred. */
2391 if (find_deferred_msg (hwnd, msg) != NULL)
2392 abort ();
2394 /* Enforced synchronization is not needed because this is the only
2395 function that alters deferred_msg_head, and the following critical
2396 section is guaranteed to only be serially reentered (since only the
2397 input thread can call us). */
2399 /* enter_crit (); */
2401 msg_buf->completed = 0;
2402 msg_buf->next = deferred_msg_head;
2403 deferred_msg_head = msg_buf;
2404 my_post_msg (&msg_buf->w32msg, hwnd, msg, wParam, lParam);
2406 /* leave_crit (); */
2408 /* Start a new nested message loop to process other messages until
2409 this one is completed. */
2410 w32_msg_pump (msg_buf);
2412 deferred_msg_head = msg_buf->next;
2414 return msg_buf->result;
2417 void
2418 complete_deferred_msg (HWND hwnd, UINT msg, LRESULT result)
2420 deferred_msg * msg_buf = find_deferred_msg (hwnd, msg);
2422 if (msg_buf == NULL)
2423 /* Message may have been cancelled, so don't abort. */
2424 return;
2426 msg_buf->result = result;
2427 msg_buf->completed = 1;
2429 /* Ensure input thread is woken so it notices the completion. */
2430 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
2433 static void
2434 cancel_all_deferred_msgs (void)
2436 deferred_msg * item;
2438 /* Don't actually need synchronization for read access, since
2439 modification of single pointer is always atomic. */
2440 /* enter_crit (); */
2442 for (item = deferred_msg_head; item != NULL; item = item->next)
2444 item->result = 0;
2445 item->completed = 1;
2448 /* leave_crit (); */
2450 /* Ensure input thread is woken so it notices the completion. */
2451 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
2454 DWORD WINAPI
2455 w32_msg_worker (void *arg)
2457 MSG msg;
2458 deferred_msg dummy_buf;
2460 /* Ensure our message queue is created */
2462 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
2464 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2465 abort ();
2467 memset (&dummy_buf, 0, sizeof (dummy_buf));
2468 dummy_buf.w32msg.msg.hwnd = NULL;
2469 dummy_buf.w32msg.msg.message = WM_NULL;
2471 /* This is the initial message loop which should only exit when the
2472 application quits. */
2473 w32_msg_pump (&dummy_buf);
2475 return 0;
2478 static void
2479 signal_user_input (void)
2481 /* Interrupt any lisp that wants to be interrupted by input. */
2482 if (!NILP (Vthrow_on_input))
2484 Vquit_flag = Vthrow_on_input;
2485 /* If we're inside a function that wants immediate quits,
2486 do it now. */
2487 if (immediate_quit && NILP (Vinhibit_quit))
2489 immediate_quit = 0;
2490 QUIT;
2496 static void
2497 post_character_message (HWND hwnd, UINT msg,
2498 WPARAM wParam, LPARAM lParam,
2499 DWORD modifiers)
2501 W32Msg wmsg;
2503 wmsg.dwModifiers = modifiers;
2505 /* Detect quit_char and set quit-flag directly. Note that we
2506 still need to post a message to ensure the main thread will be
2507 woken up if blocked in sys_select, but we do NOT want to post
2508 the quit_char message itself (because it will usually be as if
2509 the user had typed quit_char twice). Instead, we post a dummy
2510 message that has no particular effect. */
2512 int c = wParam;
2513 if (isalpha (c) && wmsg.dwModifiers == ctrl_modifier)
2514 c = make_ctrl_char (c) & 0377;
2515 if (c == quit_char
2516 || (wmsg.dwModifiers == 0 &&
2517 w32_quit_key && wParam == w32_quit_key))
2519 Vquit_flag = Qt;
2521 /* The choice of message is somewhat arbitrary, as long as
2522 the main thread handler just ignores it. */
2523 msg = WM_NULL;
2525 /* Interrupt any blocking system calls. */
2526 signal_quit ();
2528 /* As a safety precaution, forcibly complete any deferred
2529 messages. This is a kludge, but I don't see any particularly
2530 clean way to handle the situation where a deferred message is
2531 "dropped" in the lisp thread, and will thus never be
2532 completed, eg. by the user trying to activate the menubar
2533 when the lisp thread is busy, and then typing C-g when the
2534 menubar doesn't open promptly (with the result that the
2535 menubar never responds at all because the deferred
2536 WM_INITMENU message is never completed). Another problem
2537 situation is when the lisp thread calls SendMessage (to send
2538 a window manager command) when a message has been deferred;
2539 the lisp thread gets blocked indefinitely waiting for the
2540 deferred message to be completed, which itself is waiting for
2541 the lisp thread to respond.
2543 Note that we don't want to block the input thread waiting for
2544 a reponse from the lisp thread (although that would at least
2545 solve the deadlock problem above), because we want to be able
2546 to receive C-g to interrupt the lisp thread. */
2547 cancel_all_deferred_msgs ();
2549 else
2550 signal_user_input ();
2553 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2556 /* Main window procedure */
2558 static LRESULT CALLBACK
2559 w32_wnd_proc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
2561 struct frame *f;
2562 struct w32_display_info *dpyinfo = &one_w32_display_info;
2563 W32Msg wmsg;
2564 int windows_translate;
2565 int key;
2567 /* Note that it is okay to call x_window_to_frame, even though we are
2568 not running in the main lisp thread, because frame deletion
2569 requires the lisp thread to synchronize with this thread. Thus, if
2570 a frame struct is returned, it can be used without concern that the
2571 lisp thread might make it disappear while we are using it.
2573 NB. Walking the frame list in this thread is safe (as long as
2574 writes of Lisp_Object slots are atomic, which they are on Windows).
2575 Although delete-frame can destructively modify the frame list while
2576 we are walking it, a garbage collection cannot occur until after
2577 delete-frame has synchronized with this thread.
2579 It is also safe to use functions that make GDI calls, such as
2580 w32_clear_rect, because these functions must obtain a DC handle
2581 from the frame struct using get_frame_dc which is thread-aware. */
2583 switch (msg)
2585 case WM_ERASEBKGND:
2586 f = x_window_to_frame (dpyinfo, hwnd);
2587 if (f)
2589 HDC hdc = get_frame_dc (f);
2590 GetUpdateRect (hwnd, &wmsg.rect, FALSE);
2591 w32_clear_rect (f, hdc, &wmsg.rect);
2592 release_frame_dc (f, hdc);
2594 #if defined (W32_DEBUG_DISPLAY)
2595 DebPrint (("WM_ERASEBKGND (frame %p): erasing %d,%d-%d,%d\n",
2597 wmsg.rect.left, wmsg.rect.top,
2598 wmsg.rect.right, wmsg.rect.bottom));
2599 #endif /* W32_DEBUG_DISPLAY */
2601 return 1;
2602 case WM_PALETTECHANGED:
2603 /* ignore our own changes */
2604 if ((HWND)wParam != hwnd)
2606 f = x_window_to_frame (dpyinfo, hwnd);
2607 if (f)
2608 /* get_frame_dc will realize our palette and force all
2609 frames to be redrawn if needed. */
2610 release_frame_dc (f, get_frame_dc (f));
2612 return 0;
2613 case WM_PAINT:
2615 PAINTSTRUCT paintStruct;
2616 RECT update_rect;
2617 memset (&update_rect, 0, sizeof (update_rect));
2619 f = x_window_to_frame (dpyinfo, hwnd);
2620 if (f == 0)
2622 DebPrint (("WM_PAINT received for unknown window %p\n", hwnd));
2623 return 0;
2626 /* MSDN Docs say not to call BeginPaint if GetUpdateRect
2627 fails. Apparently this can happen under some
2628 circumstances. */
2629 if (GetUpdateRect (hwnd, &update_rect, FALSE) || !w32_strict_painting)
2631 enter_crit ();
2632 BeginPaint (hwnd, &paintStruct);
2634 /* The rectangles returned by GetUpdateRect and BeginPaint
2635 do not always match. Play it safe by assuming both areas
2636 are invalid. */
2637 UnionRect (&(wmsg.rect), &update_rect, &(paintStruct.rcPaint));
2639 #if defined (W32_DEBUG_DISPLAY)
2640 DebPrint (("WM_PAINT (frame %p): painting %d,%d-%d,%d\n",
2642 wmsg.rect.left, wmsg.rect.top,
2643 wmsg.rect.right, wmsg.rect.bottom));
2644 DebPrint ((" [update region is %d,%d-%d,%d]\n",
2645 update_rect.left, update_rect.top,
2646 update_rect.right, update_rect.bottom));
2647 #endif
2648 EndPaint (hwnd, &paintStruct);
2649 leave_crit ();
2651 /* Change the message type to prevent Windows from
2652 combining WM_PAINT messages in the Lisp thread's queue,
2653 since Windows assumes that each message queue is
2654 dedicated to one frame and does not bother checking
2655 that hwnd matches before combining them. */
2656 my_post_msg (&wmsg, hwnd, WM_EMACS_PAINT, wParam, lParam);
2658 return 0;
2661 /* If GetUpdateRect returns 0 (meaning there is no update
2662 region), assume the whole window needs to be repainted. */
2663 GetClientRect (hwnd, &wmsg.rect);
2664 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2665 return 0;
2668 case WM_INPUTLANGCHANGE:
2669 /* Inform lisp thread of keyboard layout changes. */
2670 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2672 /* Clear dead keys in the keyboard state; for simplicity only
2673 preserve modifier key states. */
2675 int i;
2676 BYTE keystate[256];
2678 GetKeyboardState (keystate);
2679 for (i = 0; i < 256; i++)
2680 if (1
2681 && i != VK_SHIFT
2682 && i != VK_LSHIFT
2683 && i != VK_RSHIFT
2684 && i != VK_CAPITAL
2685 && i != VK_NUMLOCK
2686 && i != VK_SCROLL
2687 && i != VK_CONTROL
2688 && i != VK_LCONTROL
2689 && i != VK_RCONTROL
2690 && i != VK_MENU
2691 && i != VK_LMENU
2692 && i != VK_RMENU
2693 && i != VK_LWIN
2694 && i != VK_RWIN)
2695 keystate[i] = 0;
2696 SetKeyboardState (keystate);
2698 goto dflt;
2700 case WM_HOTKEY:
2701 /* Synchronize hot keys with normal input. */
2702 PostMessage (hwnd, WM_KEYDOWN, HIWORD (lParam), 0);
2703 return (0);
2705 case WM_KEYUP:
2706 case WM_SYSKEYUP:
2707 record_keyup (wParam, lParam);
2708 goto dflt;
2710 case WM_KEYDOWN:
2711 case WM_SYSKEYDOWN:
2712 /* Ignore keystrokes we fake ourself; see below. */
2713 if (dpyinfo->faked_key == wParam)
2715 dpyinfo->faked_key = 0;
2716 /* Make sure TranslateMessage sees them though (as long as
2717 they don't produce WM_CHAR messages). This ensures that
2718 indicator lights are toggled promptly on Windows 9x, for
2719 example. */
2720 if (wParam < 256 && lispy_function_keys[wParam])
2722 windows_translate = 1;
2723 goto translate;
2725 return 0;
2728 /* Synchronize modifiers with current keystroke. */
2729 sync_modifiers ();
2730 record_keydown (wParam, lParam);
2731 wParam = map_keypad_keys (wParam, (lParam & 0x1000000L) != 0);
2733 windows_translate = 0;
2735 switch (wParam)
2737 case VK_LWIN:
2738 if (NILP (Vw32_pass_lwindow_to_system))
2740 /* Prevent system from acting on keyup (which opens the
2741 Start menu if no other key was pressed) by simulating a
2742 press of Space which we will ignore. */
2743 if (GetAsyncKeyState (wParam) & 1)
2745 if (NUMBERP (Vw32_phantom_key_code))
2746 key = XUINT (Vw32_phantom_key_code) & 255;
2747 else
2748 key = VK_SPACE;
2749 dpyinfo->faked_key = key;
2750 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
2753 if (!NILP (Vw32_lwindow_modifier))
2754 return 0;
2755 break;
2756 case VK_RWIN:
2757 if (NILP (Vw32_pass_rwindow_to_system))
2759 if (GetAsyncKeyState (wParam) & 1)
2761 if (NUMBERP (Vw32_phantom_key_code))
2762 key = XUINT (Vw32_phantom_key_code) & 255;
2763 else
2764 key = VK_SPACE;
2765 dpyinfo->faked_key = key;
2766 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
2769 if (!NILP (Vw32_rwindow_modifier))
2770 return 0;
2771 break;
2772 case VK_APPS:
2773 if (!NILP (Vw32_apps_modifier))
2774 return 0;
2775 break;
2776 case VK_MENU:
2777 if (NILP (Vw32_pass_alt_to_system))
2778 /* Prevent DefWindowProc from activating the menu bar if an
2779 Alt key is pressed and released by itself. */
2780 return 0;
2781 windows_translate = 1;
2782 break;
2783 case VK_CAPITAL:
2784 /* Decide whether to treat as modifier or function key. */
2785 if (NILP (Vw32_enable_caps_lock))
2786 goto disable_lock_key;
2787 windows_translate = 1;
2788 break;
2789 case VK_NUMLOCK:
2790 /* Decide whether to treat as modifier or function key. */
2791 if (NILP (Vw32_enable_num_lock))
2792 goto disable_lock_key;
2793 windows_translate = 1;
2794 break;
2795 case VK_SCROLL:
2796 /* Decide whether to treat as modifier or function key. */
2797 if (NILP (Vw32_scroll_lock_modifier))
2798 goto disable_lock_key;
2799 windows_translate = 1;
2800 break;
2801 disable_lock_key:
2802 /* Ensure the appropriate lock key state (and indicator light)
2803 remains in the same state. We do this by faking another
2804 press of the relevant key. Apparently, this really is the
2805 only way to toggle the state of the indicator lights. */
2806 dpyinfo->faked_key = wParam;
2807 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
2808 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2809 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
2810 KEYEVENTF_EXTENDEDKEY | 0, 0);
2811 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
2812 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2813 /* Ensure indicator lights are updated promptly on Windows 9x
2814 (TranslateMessage apparently does this), after forwarding
2815 input event. */
2816 post_character_message (hwnd, msg, wParam, lParam,
2817 w32_get_key_modifiers (wParam, lParam));
2818 windows_translate = 1;
2819 break;
2820 case VK_CONTROL:
2821 case VK_SHIFT:
2822 case VK_PROCESSKEY: /* Generated by IME. */
2823 windows_translate = 1;
2824 break;
2825 case VK_CANCEL:
2826 /* Windows maps Ctrl-Pause (aka Ctrl-Break) into VK_CANCEL,
2827 which is confusing for purposes of key binding; convert
2828 VK_CANCEL events into VK_PAUSE events. */
2829 wParam = VK_PAUSE;
2830 break;
2831 case VK_PAUSE:
2832 /* Windows maps Ctrl-NumLock into VK_PAUSE, which is confusing
2833 for purposes of key binding; convert these back into
2834 VK_NUMLOCK events, at least when we want to see NumLock key
2835 presses. (Note that there is never any possibility that
2836 VK_PAUSE with Ctrl really is C-Pause as per above.) */
2837 if (NILP (Vw32_enable_num_lock) && modifier_set (VK_CONTROL))
2838 wParam = VK_NUMLOCK;
2839 break;
2840 default:
2841 /* If not defined as a function key, change it to a WM_CHAR message. */
2842 if (wParam > 255 || !lispy_function_keys[wParam])
2844 DWORD modifiers = construct_console_modifiers ();
2846 if (!NILP (Vw32_recognize_altgr)
2847 && modifier_set (VK_LCONTROL) && modifier_set (VK_RMENU))
2849 /* Always let TranslateMessage handle AltGr key chords;
2850 for some reason, ToAscii doesn't always process AltGr
2851 chords correctly. */
2852 windows_translate = 1;
2854 else if ((modifiers & (~SHIFT_PRESSED & ~CAPSLOCK_ON)) != 0)
2856 /* Handle key chords including any modifiers other
2857 than shift directly, in order to preserve as much
2858 modifier information as possible. */
2859 if ('A' <= wParam && wParam <= 'Z')
2861 /* Don't translate modified alphabetic keystrokes,
2862 so the user doesn't need to constantly switch
2863 layout to type control or meta keystrokes when
2864 the normal layout translates alphabetic
2865 characters to non-ascii characters. */
2866 if (!modifier_set (VK_SHIFT))
2867 wParam += ('a' - 'A');
2868 msg = WM_CHAR;
2870 else
2872 /* Try to handle other keystrokes by determining the
2873 base character (ie. translating the base key plus
2874 shift modifier). */
2875 int add;
2876 int isdead = 0;
2877 KEY_EVENT_RECORD key;
2879 key.bKeyDown = TRUE;
2880 key.wRepeatCount = 1;
2881 key.wVirtualKeyCode = wParam;
2882 key.wVirtualScanCode = (lParam & 0xFF0000) >> 16;
2883 key.uChar.AsciiChar = 0;
2884 key.dwControlKeyState = modifiers;
2886 add = w32_kbd_patch_key (&key);
2887 /* 0 means an unrecognised keycode, negative means
2888 dead key. Ignore both. */
2889 while (--add >= 0)
2891 /* Forward asciified character sequence. */
2892 post_character_message
2893 (hwnd, WM_CHAR,
2894 (unsigned char) key.uChar.AsciiChar, lParam,
2895 w32_get_key_modifiers (wParam, lParam));
2896 w32_kbd_patch_key (&key);
2898 return 0;
2901 else
2903 /* Let TranslateMessage handle everything else. */
2904 windows_translate = 1;
2909 translate:
2910 if (windows_translate)
2912 MSG windows_msg = { hwnd, msg, wParam, lParam, 0, {0,0} };
2913 windows_msg.time = GetMessageTime ();
2914 TranslateMessage (&windows_msg);
2915 goto dflt;
2918 /* Fall through */
2920 case WM_SYSCHAR:
2921 case WM_CHAR:
2922 post_character_message (hwnd, msg, wParam, lParam,
2923 w32_get_key_modifiers (wParam, lParam));
2924 break;
2926 case WM_UNICHAR:
2927 /* WM_UNICHAR looks promising from the docs, but the exact
2928 circumstances in which TranslateMessage sends it is one of those
2929 Microsoft secret API things that EU and US courts are supposed
2930 to have put a stop to already. Spy++ shows it being sent to Notepad
2931 and other MS apps, but never to Emacs.
2933 Some third party IMEs send it in accordance with the official
2934 documentation though, so handle it here.
2936 UNICODE_NOCHAR is used to test for support for this message.
2937 TRUE indicates that the message is supported. */
2938 if (wParam == UNICODE_NOCHAR)
2939 return TRUE;
2942 W32Msg wmsg;
2943 wmsg.dwModifiers = w32_get_key_modifiers (wParam, lParam);
2944 signal_user_input ();
2945 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2947 break;
2949 case WM_IME_CHAR:
2950 /* If we can't get the IME result as unicode, use default processing,
2951 which will at least allow characters decodable in the system locale
2952 get through. */
2953 if (!get_composition_string_fn)
2954 goto dflt;
2956 else if (!ignore_ime_char)
2958 wchar_t * buffer;
2959 int size, i;
2960 W32Msg wmsg;
2961 HIMC context = get_ime_context_fn (hwnd);
2962 wmsg.dwModifiers = w32_get_key_modifiers (wParam, lParam);
2963 /* Get buffer size. */
2964 size = get_composition_string_fn (context, GCS_RESULTSTR, buffer, 0);
2965 buffer = alloca (size);
2966 size = get_composition_string_fn (context, GCS_RESULTSTR,
2967 buffer, size);
2968 release_ime_context_fn (hwnd, context);
2970 signal_user_input ();
2971 for (i = 0; i < size / sizeof (wchar_t); i++)
2973 my_post_msg (&wmsg, hwnd, WM_UNICHAR, (WPARAM) buffer[i],
2974 lParam);
2976 /* Ignore the messages for the rest of the
2977 characters in the string that was output above. */
2978 ignore_ime_char = (size / sizeof (wchar_t)) - 1;
2980 else
2981 ignore_ime_char--;
2983 break;
2985 case WM_IME_STARTCOMPOSITION:
2986 if (!set_ime_composition_window_fn)
2987 goto dflt;
2988 else
2990 COMPOSITIONFORM form;
2991 HIMC context;
2992 struct window *w;
2994 f = x_window_to_frame (dpyinfo, hwnd);
2995 w = XWINDOW (FRAME_SELECTED_WINDOW (f));
2997 form.dwStyle = CFS_RECT;
2998 form.ptCurrentPos.x = w32_system_caret_x;
2999 form.ptCurrentPos.y = w32_system_caret_y;
3001 form.rcArea.left = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, 0);
3002 form.rcArea.top = (WINDOW_TOP_EDGE_Y (w)
3003 + WINDOW_HEADER_LINE_HEIGHT (w));
3004 form.rcArea.right = (WINDOW_BOX_RIGHT_EDGE_X (w)
3005 - WINDOW_RIGHT_MARGIN_WIDTH (w)
3006 - WINDOW_RIGHT_FRINGE_WIDTH (w));
3007 form.rcArea.bottom = (WINDOW_BOTTOM_EDGE_Y (w)
3008 - WINDOW_MODE_LINE_HEIGHT (w));
3010 context = get_ime_context_fn (hwnd);
3012 if (!context)
3013 break;
3015 set_ime_composition_window_fn (context, &form);
3016 release_ime_context_fn (hwnd, context);
3018 break;
3020 case WM_IME_ENDCOMPOSITION:
3021 ignore_ime_char = 0;
3022 goto dflt;
3024 /* Simulate middle mouse button events when left and right buttons
3025 are used together, but only if user has two button mouse. */
3026 case WM_LBUTTONDOWN:
3027 case WM_RBUTTONDOWN:
3028 if (w32_num_mouse_buttons > 2)
3029 goto handle_plain_button;
3032 int this = (msg == WM_LBUTTONDOWN) ? LMOUSE : RMOUSE;
3033 int other = (msg == WM_LBUTTONDOWN) ? RMOUSE : LMOUSE;
3035 if (button_state & this)
3036 return 0;
3038 if (button_state == 0)
3039 SetCapture (hwnd);
3041 button_state |= this;
3043 if (button_state & other)
3045 if (mouse_button_timer)
3047 KillTimer (hwnd, mouse_button_timer);
3048 mouse_button_timer = 0;
3050 /* Generate middle mouse event instead. */
3051 msg = WM_MBUTTONDOWN;
3052 button_state |= MMOUSE;
3054 else if (button_state & MMOUSE)
3056 /* Ignore button event if we've already generated a
3057 middle mouse down event. This happens if the
3058 user releases and press one of the two buttons
3059 after we've faked a middle mouse event. */
3060 return 0;
3062 else
3064 /* Flush out saved message. */
3065 post_msg (&saved_mouse_button_msg);
3067 wmsg.dwModifiers = w32_get_modifiers ();
3068 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3069 signal_user_input ();
3071 /* Clear message buffer. */
3072 saved_mouse_button_msg.msg.hwnd = 0;
3074 else
3076 /* Hold onto message for now. */
3077 mouse_button_timer =
3078 SetTimer (hwnd, MOUSE_BUTTON_ID,
3079 w32_mouse_button_tolerance, NULL);
3080 saved_mouse_button_msg.msg.hwnd = hwnd;
3081 saved_mouse_button_msg.msg.message = msg;
3082 saved_mouse_button_msg.msg.wParam = wParam;
3083 saved_mouse_button_msg.msg.lParam = lParam;
3084 saved_mouse_button_msg.msg.time = GetMessageTime ();
3085 saved_mouse_button_msg.dwModifiers = w32_get_modifiers ();
3088 return 0;
3090 case WM_LBUTTONUP:
3091 case WM_RBUTTONUP:
3092 if (w32_num_mouse_buttons > 2)
3093 goto handle_plain_button;
3096 int this = (msg == WM_LBUTTONUP) ? LMOUSE : RMOUSE;
3097 int other = (msg == WM_LBUTTONUP) ? RMOUSE : LMOUSE;
3099 if ((button_state & this) == 0)
3100 return 0;
3102 button_state &= ~this;
3104 if (button_state & MMOUSE)
3106 /* Only generate event when second button is released. */
3107 if ((button_state & other) == 0)
3109 msg = WM_MBUTTONUP;
3110 button_state &= ~MMOUSE;
3112 if (button_state) abort ();
3114 else
3115 return 0;
3117 else
3119 /* Flush out saved message if necessary. */
3120 if (saved_mouse_button_msg.msg.hwnd)
3122 post_msg (&saved_mouse_button_msg);
3125 wmsg.dwModifiers = w32_get_modifiers ();
3126 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3127 signal_user_input ();
3129 /* Always clear message buffer and cancel timer. */
3130 saved_mouse_button_msg.msg.hwnd = 0;
3131 KillTimer (hwnd, mouse_button_timer);
3132 mouse_button_timer = 0;
3134 if (button_state == 0)
3135 ReleaseCapture ();
3137 return 0;
3139 case WM_XBUTTONDOWN:
3140 case WM_XBUTTONUP:
3141 if (w32_pass_extra_mouse_buttons_to_system)
3142 goto dflt;
3143 /* else fall through and process them. */
3144 case WM_MBUTTONDOWN:
3145 case WM_MBUTTONUP:
3146 handle_plain_button:
3148 BOOL up;
3149 int button;
3151 /* Ignore middle and extra buttons as long as the menu is active. */
3152 f = x_window_to_frame (dpyinfo, hwnd);
3153 if (f && f->output_data.w32->menubar_active)
3154 return 0;
3156 if (parse_button (msg, HIWORD (wParam), &button, &up))
3158 if (up) ReleaseCapture ();
3159 else SetCapture (hwnd);
3160 button = (button == 0) ? LMOUSE :
3161 ((button == 1) ? MMOUSE : RMOUSE);
3162 if (up)
3163 button_state &= ~button;
3164 else
3165 button_state |= button;
3169 wmsg.dwModifiers = w32_get_modifiers ();
3170 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3171 signal_user_input ();
3173 /* Need to return true for XBUTTON messages, false for others,
3174 to indicate that we processed the message. */
3175 return (msg == WM_XBUTTONDOWN || msg == WM_XBUTTONUP);
3177 case WM_MOUSEMOVE:
3178 /* Ignore mouse movements as long as the menu is active. These
3179 movements are processed by the window manager anyway, and
3180 it's wrong to handle them as if they happened on the
3181 underlying frame. */
3182 f = x_window_to_frame (dpyinfo, hwnd);
3183 if (f && f->output_data.w32->menubar_active)
3184 return 0;
3186 /* If the mouse has just moved into the frame, start tracking
3187 it, so we will be notified when it leaves the frame. Mouse
3188 tracking only works under W98 and NT4 and later. On earlier
3189 versions, there is no way of telling when the mouse leaves the
3190 frame, so we just have to put up with help-echo and mouse
3191 highlighting remaining while the frame is not active. */
3192 if (track_mouse_event_fn && !track_mouse_window)
3194 TRACKMOUSEEVENT tme;
3195 tme.cbSize = sizeof (tme);
3196 tme.dwFlags = TME_LEAVE;
3197 tme.hwndTrack = hwnd;
3199 track_mouse_event_fn (&tme);
3200 track_mouse_window = hwnd;
3202 case WM_VSCROLL:
3203 if (w32_mouse_move_interval <= 0
3204 || (msg == WM_MOUSEMOVE && button_state == 0))
3206 wmsg.dwModifiers = w32_get_modifiers ();
3207 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3208 return 0;
3211 /* Hang onto mouse move and scroll messages for a bit, to avoid
3212 sending such events to Emacs faster than it can process them.
3213 If we get more events before the timer from the first message
3214 expires, we just replace the first message. */
3216 if (saved_mouse_move_msg.msg.hwnd == 0)
3217 mouse_move_timer =
3218 SetTimer (hwnd, MOUSE_MOVE_ID,
3219 w32_mouse_move_interval, NULL);
3221 /* Hold onto message for now. */
3222 saved_mouse_move_msg.msg.hwnd = hwnd;
3223 saved_mouse_move_msg.msg.message = msg;
3224 saved_mouse_move_msg.msg.wParam = wParam;
3225 saved_mouse_move_msg.msg.lParam = lParam;
3226 saved_mouse_move_msg.msg.time = GetMessageTime ();
3227 saved_mouse_move_msg.dwModifiers = w32_get_modifiers ();
3229 return 0;
3231 case WM_MOUSEWHEEL:
3232 case WM_DROPFILES:
3233 wmsg.dwModifiers = w32_get_modifiers ();
3234 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3235 signal_user_input ();
3236 return 0;
3238 case WM_APPCOMMAND:
3239 if (w32_pass_multimedia_buttons_to_system)
3240 goto dflt;
3241 /* Otherwise, pass to lisp, the same way we do with mousehwheel. */
3242 case WM_MOUSEHWHEEL:
3243 wmsg.dwModifiers = w32_get_modifiers ();
3244 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3245 signal_user_input ();
3246 /* Non-zero must be returned when WM_MOUSEHWHEEL messages are
3247 handled, to prevent the system trying to handle it by faking
3248 scroll bar events. */
3249 return 1;
3251 case WM_TIMER:
3252 /* Flush out saved messages if necessary. */
3253 if (wParam == mouse_button_timer)
3255 if (saved_mouse_button_msg.msg.hwnd)
3257 post_msg (&saved_mouse_button_msg);
3258 signal_user_input ();
3259 saved_mouse_button_msg.msg.hwnd = 0;
3261 KillTimer (hwnd, mouse_button_timer);
3262 mouse_button_timer = 0;
3264 else if (wParam == mouse_move_timer)
3266 if (saved_mouse_move_msg.msg.hwnd)
3268 post_msg (&saved_mouse_move_msg);
3269 saved_mouse_move_msg.msg.hwnd = 0;
3271 KillTimer (hwnd, mouse_move_timer);
3272 mouse_move_timer = 0;
3274 else if (wParam == menu_free_timer)
3276 KillTimer (hwnd, menu_free_timer);
3277 menu_free_timer = 0;
3278 f = x_window_to_frame (dpyinfo, hwnd);
3279 /* If a popup menu is active, don't wipe its strings. */
3280 if (menubar_in_use
3281 && current_popup_menu == NULL)
3283 /* Free memory used by owner-drawn and help-echo strings. */
3284 w32_free_menu_strings (hwnd);
3285 f->output_data.w32->menubar_active = 0;
3286 menubar_in_use = 0;
3289 else if (wParam == hourglass_timer)
3291 KillTimer (hwnd, hourglass_timer);
3292 hourglass_timer = 0;
3293 w32_show_hourglass (x_window_to_frame (dpyinfo, hwnd));
3295 return 0;
3297 case WM_NCACTIVATE:
3298 /* Windows doesn't send us focus messages when putting up and
3299 taking down a system popup dialog as for Ctrl-Alt-Del on Windows 95.
3300 The only indication we get that something happened is receiving
3301 this message afterwards. So this is a good time to reset our
3302 keyboard modifiers' state. */
3303 reset_modifiers ();
3304 goto dflt;
3306 case WM_INITMENU:
3307 button_state = 0;
3308 ReleaseCapture ();
3309 /* We must ensure menu bar is fully constructed and up to date
3310 before allowing user interaction with it. To achieve this
3311 we send this message to the lisp thread and wait for a
3312 reply (whose value is not actually needed) to indicate that
3313 the menu bar is now ready for use, so we can now return.
3315 To remain responsive in the meantime, we enter a nested message
3316 loop that can process all other messages.
3318 However, we skip all this if the message results from calling
3319 TrackPopupMenu - in fact, we must NOT attempt to send the lisp
3320 thread a message because it is blocked on us at this point. We
3321 set menubar_active before calling TrackPopupMenu to indicate
3322 this (there is no possibility of confusion with real menubar
3323 being active). */
3325 f = x_window_to_frame (dpyinfo, hwnd);
3326 if (f
3327 && (f->output_data.w32->menubar_active
3328 /* We can receive this message even in the absence of a
3329 menubar (ie. when the system menu is activated) - in this
3330 case we do NOT want to forward the message, otherwise it
3331 will cause the menubar to suddenly appear when the user
3332 had requested it to be turned off! */
3333 || f->output_data.w32->menubar_widget == NULL))
3334 return 0;
3337 deferred_msg msg_buf;
3339 /* Detect if message has already been deferred; in this case
3340 we cannot return any sensible value to ignore this. */
3341 if (find_deferred_msg (hwnd, msg) != NULL)
3342 abort ();
3344 menubar_in_use = 1;
3346 return send_deferred_msg (&msg_buf, hwnd, msg, wParam, lParam);
3349 case WM_EXITMENULOOP:
3350 f = x_window_to_frame (dpyinfo, hwnd);
3352 /* If a menu is still active, check again after a short delay,
3353 since Windows often (always?) sends the WM_EXITMENULOOP
3354 before the corresponding WM_COMMAND message.
3355 Don't do this if a popup menu is active, since it is only
3356 menubar menus that require cleaning up in this way.
3358 if (f && menubar_in_use && current_popup_menu == NULL)
3359 menu_free_timer = SetTimer (hwnd, MENU_FREE_ID, MENU_FREE_DELAY, NULL);
3361 /* If hourglass cursor should be displayed, display it now. */
3362 if (f && f->output_data.w32->hourglass_p)
3363 SetCursor (f->output_data.w32->hourglass_cursor);
3365 goto dflt;
3367 case WM_MENUSELECT:
3368 /* Direct handling of help_echo in menus. Should be safe now
3369 that we generate the help_echo by placing a help event in the
3370 keyboard buffer. */
3372 HMENU menu = (HMENU) lParam;
3373 UINT menu_item = (UINT) LOWORD (wParam);
3374 UINT flags = (UINT) HIWORD (wParam);
3376 w32_menu_display_help (hwnd, menu, menu_item, flags);
3378 return 0;
3380 case WM_MEASUREITEM:
3381 f = x_window_to_frame (dpyinfo, hwnd);
3382 if (f)
3384 MEASUREITEMSTRUCT * pMis = (MEASUREITEMSTRUCT *) lParam;
3386 if (pMis->CtlType == ODT_MENU)
3388 /* Work out dimensions for popup menu titles. */
3389 char * title = (char *) pMis->itemData;
3390 HDC hdc = GetDC (hwnd);
3391 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
3392 LOGFONT menu_logfont;
3393 HFONT old_font;
3394 SIZE size;
3396 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
3397 menu_logfont.lfWeight = FW_BOLD;
3398 menu_font = CreateFontIndirect (&menu_logfont);
3399 old_font = SelectObject (hdc, menu_font);
3401 pMis->itemHeight = GetSystemMetrics (SM_CYMENUSIZE);
3402 if (title)
3404 if (unicode_append_menu)
3405 GetTextExtentPoint32W (hdc, (WCHAR *) title,
3406 wcslen ((WCHAR *) title),
3407 &size);
3408 else
3409 GetTextExtentPoint32 (hdc, title, strlen (title), &size);
3411 pMis->itemWidth = size.cx;
3412 if (pMis->itemHeight < size.cy)
3413 pMis->itemHeight = size.cy;
3415 else
3416 pMis->itemWidth = 0;
3418 SelectObject (hdc, old_font);
3419 DeleteObject (menu_font);
3420 ReleaseDC (hwnd, hdc);
3421 return TRUE;
3424 return 0;
3426 case WM_DRAWITEM:
3427 f = x_window_to_frame (dpyinfo, hwnd);
3428 if (f)
3430 DRAWITEMSTRUCT * pDis = (DRAWITEMSTRUCT *) lParam;
3432 if (pDis->CtlType == ODT_MENU)
3434 /* Draw popup menu title. */
3435 char * title = (char *) pDis->itemData;
3436 if (title)
3438 HDC hdc = pDis->hDC;
3439 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
3440 LOGFONT menu_logfont;
3441 HFONT old_font;
3443 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
3444 menu_logfont.lfWeight = FW_BOLD;
3445 menu_font = CreateFontIndirect (&menu_logfont);
3446 old_font = SelectObject (hdc, menu_font);
3448 /* Always draw title as if not selected. */
3449 if (unicode_append_menu)
3450 ExtTextOutW (hdc,
3451 pDis->rcItem.left
3452 + GetSystemMetrics (SM_CXMENUCHECK),
3453 pDis->rcItem.top,
3454 ETO_OPAQUE, &pDis->rcItem,
3455 (WCHAR *) title,
3456 wcslen ((WCHAR *) title), NULL);
3457 else
3458 ExtTextOut (hdc,
3459 pDis->rcItem.left
3460 + GetSystemMetrics (SM_CXMENUCHECK),
3461 pDis->rcItem.top,
3462 ETO_OPAQUE, &pDis->rcItem,
3463 title, strlen (title), NULL);
3465 SelectObject (hdc, old_font);
3466 DeleteObject (menu_font);
3468 return TRUE;
3471 return 0;
3473 #if 0
3474 /* Still not right - can't distinguish between clicks in the
3475 client area of the frame from clicks forwarded from the scroll
3476 bars - may have to hook WM_NCHITTEST to remember the mouse
3477 position and then check if it is in the client area ourselves. */
3478 case WM_MOUSEACTIVATE:
3479 /* Discard the mouse click that activates a frame, allowing the
3480 user to click anywhere without changing point (or worse!).
3481 Don't eat mouse clicks on scrollbars though!! */
3482 if (LOWORD (lParam) == HTCLIENT )
3483 return MA_ACTIVATEANDEAT;
3484 goto dflt;
3485 #endif
3487 case WM_MOUSELEAVE:
3488 /* No longer tracking mouse. */
3489 track_mouse_window = NULL;
3491 case WM_ACTIVATEAPP:
3492 case WM_ACTIVATE:
3493 case WM_WINDOWPOSCHANGED:
3494 case WM_SHOWWINDOW:
3495 /* Inform lisp thread that a frame might have just been obscured
3496 or exposed, so should recheck visibility of all frames. */
3497 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3498 goto dflt;
3500 case WM_SETFOCUS:
3501 dpyinfo->faked_key = 0;
3502 reset_modifiers ();
3503 register_hot_keys (hwnd);
3504 goto command;
3505 case WM_KILLFOCUS:
3506 unregister_hot_keys (hwnd);
3507 button_state = 0;
3508 ReleaseCapture ();
3509 /* Relinquish the system caret. */
3510 if (w32_system_caret_hwnd)
3512 w32_visible_system_caret_hwnd = NULL;
3513 w32_system_caret_hwnd = NULL;
3514 DestroyCaret ();
3516 goto command;
3517 case WM_COMMAND:
3518 menubar_in_use = 0;
3519 f = x_window_to_frame (dpyinfo, hwnd);
3520 if (f && HIWORD (wParam) == 0)
3522 if (menu_free_timer)
3524 KillTimer (hwnd, menu_free_timer);
3525 menu_free_timer = 0;
3528 case WM_MOVE:
3529 case WM_SIZE:
3530 command:
3531 wmsg.dwModifiers = w32_get_modifiers ();
3532 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3533 goto dflt;
3535 case WM_DESTROY:
3536 CoUninitialize ();
3537 return 0;
3539 case WM_CLOSE:
3540 wmsg.dwModifiers = w32_get_modifiers ();
3541 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3542 return 0;
3544 case WM_WINDOWPOSCHANGING:
3545 /* Don't restrict the sizing of tip frames. */
3546 if (hwnd == tip_window)
3547 return 0;
3549 WINDOWPLACEMENT wp;
3550 LPWINDOWPOS lppos = (WINDOWPOS *) lParam;
3552 wp.length = sizeof (WINDOWPLACEMENT);
3553 GetWindowPlacement (hwnd, &wp);
3555 if (wp.showCmd != SW_SHOWMINIMIZED && (lppos->flags & SWP_NOSIZE) == 0)
3557 RECT rect;
3558 int wdiff;
3559 int hdiff;
3560 DWORD font_width;
3561 DWORD line_height;
3562 DWORD internal_border;
3563 DWORD scrollbar_extra;
3564 RECT wr;
3566 wp.length = sizeof (wp);
3567 GetWindowRect (hwnd, &wr);
3569 enter_crit ();
3571 font_width = GetWindowLong (hwnd, WND_FONTWIDTH_INDEX);
3572 line_height = GetWindowLong (hwnd, WND_LINEHEIGHT_INDEX);
3573 internal_border = GetWindowLong (hwnd, WND_BORDER_INDEX);
3574 scrollbar_extra = GetWindowLong (hwnd, WND_SCROLLBAR_INDEX);
3576 leave_crit ();
3578 memset (&rect, 0, sizeof (rect));
3579 AdjustWindowRect (&rect, GetWindowLong (hwnd, GWL_STYLE),
3580 GetMenu (hwnd) != NULL);
3582 /* Force width and height of client area to be exact
3583 multiples of the character cell dimensions. */
3584 wdiff = (lppos->cx - (rect.right - rect.left)
3585 - 2 * internal_border - scrollbar_extra)
3586 % font_width;
3587 hdiff = (lppos->cy - (rect.bottom - rect.top)
3588 - 2 * internal_border)
3589 % line_height;
3591 if (wdiff || hdiff)
3593 /* For right/bottom sizing we can just fix the sizes.
3594 However for top/left sizing we will need to fix the X
3595 and Y positions as well. */
3597 int cx_mintrack = GetSystemMetrics (SM_CXMINTRACK);
3598 int cy_mintrack = GetSystemMetrics (SM_CYMINTRACK);
3600 lppos->cx = max (lppos->cx - wdiff, cx_mintrack);
3601 lppos->cy = max (lppos->cy - hdiff, cy_mintrack);
3603 if (wp.showCmd != SW_SHOWMAXIMIZED
3604 && (lppos->flags & SWP_NOMOVE) == 0)
3606 if (lppos->x != wr.left || lppos->y != wr.top)
3608 lppos->x += wdiff;
3609 lppos->y += hdiff;
3611 else
3613 lppos->flags |= SWP_NOMOVE;
3617 return 0;
3622 goto dflt;
3624 case WM_GETMINMAXINFO:
3625 /* Hack to allow resizing the Emacs frame above the screen size.
3626 Note that Windows 9x limits coordinates to 16-bits. */
3627 ((LPMINMAXINFO) lParam)->ptMaxTrackSize.x = 32767;
3628 ((LPMINMAXINFO) lParam)->ptMaxTrackSize.y = 32767;
3629 return 0;
3631 case WM_SETCURSOR:
3632 if (LOWORD (lParam) == HTCLIENT)
3634 f = x_window_to_frame (dpyinfo, hwnd);
3635 if (f->output_data.w32->hourglass_p && !menubar_in_use
3636 && !current_popup_menu)
3637 SetCursor (f->output_data.w32->hourglass_cursor);
3638 else
3639 SetCursor (f->output_data.w32->current_cursor);
3640 return 0;
3642 goto dflt;
3644 case WM_EMACS_SETCURSOR:
3646 Cursor cursor = (Cursor) wParam;
3647 f = x_window_to_frame (dpyinfo, hwnd);
3648 if (f && cursor)
3650 f->output_data.w32->current_cursor = cursor;
3651 if (!f->output_data.w32->hourglass_p)
3652 SetCursor (cursor);
3654 return 0;
3657 case WM_EMACS_CREATESCROLLBAR:
3658 return (LRESULT) w32_createscrollbar ((struct frame *) wParam,
3659 (struct scroll_bar *) lParam);
3661 case WM_EMACS_SHOWWINDOW:
3662 return ShowWindow ((HWND) wParam, (WPARAM) lParam);
3664 case WM_EMACS_SETFOREGROUND:
3666 HWND foreground_window;
3667 DWORD foreground_thread, retval;
3669 /* On NT 5.0, and apparently Windows 98, it is necessary to
3670 attach to the thread that currently has focus in order to
3671 pull the focus away from it. */
3672 foreground_window = GetForegroundWindow ();
3673 foreground_thread = GetWindowThreadProcessId (foreground_window, NULL);
3674 if (!foreground_window
3675 || foreground_thread == GetCurrentThreadId ()
3676 || !AttachThreadInput (GetCurrentThreadId (),
3677 foreground_thread, TRUE))
3678 foreground_thread = 0;
3680 retval = SetForegroundWindow ((HWND) wParam);
3682 /* Detach from the previous foreground thread. */
3683 if (foreground_thread)
3684 AttachThreadInput (GetCurrentThreadId (),
3685 foreground_thread, FALSE);
3687 return retval;
3690 case WM_EMACS_SETWINDOWPOS:
3692 WINDOWPOS * pos = (WINDOWPOS *) wParam;
3693 return SetWindowPos (hwnd, pos->hwndInsertAfter,
3694 pos->x, pos->y, pos->cx, pos->cy, pos->flags);
3697 case WM_EMACS_DESTROYWINDOW:
3698 DragAcceptFiles ((HWND) wParam, FALSE);
3699 return DestroyWindow ((HWND) wParam);
3701 case WM_EMACS_HIDE_CARET:
3702 return HideCaret (hwnd);
3704 case WM_EMACS_SHOW_CARET:
3705 return ShowCaret (hwnd);
3707 case WM_EMACS_DESTROY_CARET:
3708 w32_system_caret_hwnd = NULL;
3709 w32_visible_system_caret_hwnd = NULL;
3710 return DestroyCaret ();
3712 case WM_EMACS_TRACK_CARET:
3713 /* If there is currently no system caret, create one. */
3714 if (w32_system_caret_hwnd == NULL)
3716 /* Use the default caret width, and avoid changing it
3717 unneccesarily, as it confuses screen reader software. */
3718 w32_system_caret_hwnd = hwnd;
3719 CreateCaret (hwnd, NULL, 0,
3720 w32_system_caret_height);
3723 if (!SetCaretPos (w32_system_caret_x, w32_system_caret_y))
3724 return 0;
3725 /* Ensure visible caret gets turned on when requested. */
3726 else if (w32_use_visible_system_caret
3727 && w32_visible_system_caret_hwnd != hwnd)
3729 w32_visible_system_caret_hwnd = hwnd;
3730 return ShowCaret (hwnd);
3732 /* Ensure visible caret gets turned off when requested. */
3733 else if (!w32_use_visible_system_caret
3734 && w32_visible_system_caret_hwnd)
3736 w32_visible_system_caret_hwnd = NULL;
3737 return HideCaret (hwnd);
3739 else
3740 return 1;
3742 case WM_EMACS_TRACKPOPUPMENU:
3744 UINT flags;
3745 POINT *pos;
3746 int retval;
3747 pos = (POINT *)lParam;
3748 flags = TPM_CENTERALIGN;
3749 if (button_state & LMOUSE)
3750 flags |= TPM_LEFTBUTTON;
3751 else if (button_state & RMOUSE)
3752 flags |= TPM_RIGHTBUTTON;
3754 /* Remember we did a SetCapture on the initial mouse down event,
3755 so for safety, we make sure the capture is cancelled now. */
3756 ReleaseCapture ();
3757 button_state = 0;
3759 /* Use menubar_active to indicate that WM_INITMENU is from
3760 TrackPopupMenu below, and should be ignored. */
3761 f = x_window_to_frame (dpyinfo, hwnd);
3762 if (f)
3763 f->output_data.w32->menubar_active = 1;
3765 if (TrackPopupMenu ((HMENU)wParam, flags, pos->x, pos->y,
3766 0, hwnd, NULL))
3768 MSG amsg;
3769 /* Eat any mouse messages during popupmenu */
3770 while (PeekMessage (&amsg, hwnd, WM_MOUSEFIRST, WM_MOUSELAST,
3771 PM_REMOVE));
3772 /* Get the menu selection, if any */
3773 if (PeekMessage (&amsg, hwnd, WM_COMMAND, WM_COMMAND, PM_REMOVE))
3775 retval = LOWORD (amsg.wParam);
3777 else
3779 retval = 0;
3782 else
3784 retval = -1;
3787 return retval;
3790 default:
3791 /* Check for messages registered at runtime. */
3792 if (msg == msh_mousewheel)
3794 wmsg.dwModifiers = w32_get_modifiers ();
3795 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3796 signal_user_input ();
3797 return 0;
3800 dflt:
3801 return DefWindowProc (hwnd, msg, wParam, lParam);
3804 /* The most common default return code for handled messages is 0. */
3805 return 0;
3808 static void
3809 my_create_window (struct frame * f)
3811 MSG msg;
3813 if (!PostThreadMessage (dwWindowsThreadId, WM_EMACS_CREATEWINDOW, (WPARAM)f, 0))
3814 abort ();
3815 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
3819 /* Create a tooltip window. Unlike my_create_window, we do not do this
3820 indirectly via the Window thread, as we do not need to process Window
3821 messages for the tooltip. Creating tooltips indirectly also creates
3822 deadlocks when tooltips are created for menu items. */
3823 static void
3824 my_create_tip_window (struct frame *f)
3826 RECT rect;
3828 rect.left = rect.top = 0;
3829 rect.right = FRAME_PIXEL_WIDTH (f);
3830 rect.bottom = FRAME_PIXEL_HEIGHT (f);
3832 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
3833 FRAME_EXTERNAL_MENU_BAR (f));
3835 tip_window = FRAME_W32_WINDOW (f)
3836 = CreateWindow (EMACS_CLASS,
3837 f->namebuf,
3838 f->output_data.w32->dwStyle,
3839 f->left_pos,
3840 f->top_pos,
3841 rect.right - rect.left,
3842 rect.bottom - rect.top,
3843 FRAME_W32_WINDOW (SELECTED_FRAME ()), /* owner */
3844 NULL,
3845 hinst,
3846 NULL);
3848 if (tip_window)
3850 SetWindowLong (tip_window, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
3851 SetWindowLong (tip_window, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
3852 SetWindowLong (tip_window, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
3853 SetWindowLong (tip_window, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
3855 /* Tip frames have no scrollbars. */
3856 SetWindowLong (tip_window, WND_SCROLLBAR_INDEX, 0);
3858 /* Do this to discard the default setting specified by our parent. */
3859 ShowWindow (tip_window, SW_HIDE);
3864 /* Create and set up the w32 window for frame F. */
3866 static void
3867 w32_window (struct frame *f, long window_prompting, int minibuffer_only)
3869 BLOCK_INPUT;
3871 /* Use the resource name as the top-level window name
3872 for looking up resources. Make a non-Lisp copy
3873 for the window manager, so GC relocation won't bother it.
3875 Elsewhere we specify the window name for the window manager. */
3878 char *str = SSDATA (Vx_resource_name);
3879 f->namebuf = (char *) xmalloc (strlen (str) + 1);
3880 strcpy (f->namebuf, str);
3883 my_create_window (f);
3885 validate_x_resource_name ();
3887 /* x_set_name normally ignores requests to set the name if the
3888 requested name is the same as the current name. This is the one
3889 place where that assumption isn't correct; f->name is set, but
3890 the server hasn't been told. */
3892 Lisp_Object name;
3893 int explicit = f->explicit_name;
3895 f->explicit_name = 0;
3896 name = f->name;
3897 f->name = Qnil;
3898 x_set_name (f, name, explicit);
3901 UNBLOCK_INPUT;
3903 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
3904 initialize_frame_menubar (f);
3906 if (FRAME_W32_WINDOW (f) == 0)
3907 error ("Unable to create window");
3910 /* Handle the icon stuff for this window. Perhaps later we might
3911 want an x_set_icon_position which can be called interactively as
3912 well. */
3914 static void
3915 x_icon (struct frame *f, Lisp_Object parms)
3917 Lisp_Object icon_x, icon_y;
3918 struct w32_display_info *dpyinfo = &one_w32_display_info;
3920 /* Set the position of the icon. Note that Windows 95 groups all
3921 icons in the tray. */
3922 icon_x = x_get_arg (dpyinfo, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
3923 icon_y = x_get_arg (dpyinfo, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
3924 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
3926 CHECK_NUMBER (icon_x);
3927 CHECK_NUMBER (icon_y);
3929 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
3930 error ("Both left and top icon corners of icon must be specified");
3932 BLOCK_INPUT;
3934 if (! EQ (icon_x, Qunbound))
3935 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
3937 #if 0 /* TODO */
3938 /* Start up iconic or window? */
3939 x_wm_set_window_state
3940 (f, (EQ (x_get_arg (dpyinfo, parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL), Qicon)
3941 ? IconicState
3942 : NormalState));
3944 x_text_icon (f, SSDATA ((!NILP (f->icon_name)
3945 ? f->icon_name
3946 : f->name)));
3947 #endif
3949 UNBLOCK_INPUT;
3953 static void
3954 x_make_gc (struct frame *f)
3956 XGCValues gc_values;
3958 BLOCK_INPUT;
3960 /* Create the GC's of this frame.
3961 Note that many default values are used. */
3963 /* Normal video */
3964 gc_values.font = FRAME_FONT (f);
3966 /* Cursor has cursor-color background, background-color foreground. */
3967 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
3968 gc_values.background = f->output_data.w32->cursor_pixel;
3969 f->output_data.w32->cursor_gc
3970 = XCreateGC (NULL, FRAME_W32_WINDOW (f),
3971 (GCFont | GCForeground | GCBackground),
3972 &gc_values);
3974 /* Reliefs. */
3975 f->output_data.w32->white_relief.gc = 0;
3976 f->output_data.w32->black_relief.gc = 0;
3978 UNBLOCK_INPUT;
3982 /* Handler for signals raised during x_create_frame and
3983 x_create_top_frame. FRAME is the frame which is partially
3984 constructed. */
3986 static Lisp_Object
3987 unwind_create_frame (Lisp_Object frame)
3989 struct frame *f = XFRAME (frame);
3991 /* If frame is ``official'', nothing to do. */
3992 if (!CONSP (Vframe_list) || !EQ (XCAR (Vframe_list), frame))
3994 #ifdef GLYPH_DEBUG
3995 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
3996 #endif
3998 x_free_frame_resources (f);
4000 #if GLYPH_DEBUG
4001 /* Check that reference counts are indeed correct. */
4002 xassert (dpyinfo->reference_count == dpyinfo_refcount);
4003 xassert (dpyinfo->image_cache->refcount == image_cache_refcount);
4004 #endif
4005 return Qt;
4008 return Qnil;
4011 static void
4012 x_default_font_parameter (struct frame *f, Lisp_Object parms)
4014 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4015 Lisp_Object font_param = x_get_arg (dpyinfo, parms, Qfont, NULL, NULL,
4016 RES_TYPE_STRING);
4017 Lisp_Object font;
4018 if (EQ (font_param, Qunbound))
4019 font_param = Qnil;
4020 font = !NILP (font_param) ? font_param
4021 : x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
4023 if (!STRINGP (font))
4025 int i;
4026 static char *names[]
4027 = { "Courier New-10",
4028 "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1",
4029 "-*-Fixedsys-normal-r-*-*-12-*-*-*-c-*-iso8859-1",
4030 "Fixedsys",
4031 NULL };
4033 for (i = 0; names[i]; i++)
4035 font = font_open_by_name (f, names[i]);
4036 if (! NILP (font))
4037 break;
4039 if (NILP (font))
4040 error ("No suitable font was found");
4042 else if (!NILP (font_param))
4044 /* Remember the explicit font parameter, so we can re-apply it after
4045 we've applied the `default' face settings. */
4046 x_set_frame_parameters (f, Fcons (Fcons (Qfont_param, font_param), Qnil));
4048 x_default_parameter (f, parms, Qfont, font, "font", "Font", RES_TYPE_STRING);
4051 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
4052 1, 1, 0,
4053 doc: /* Make a new window, which is called a \"frame\" in Emacs terms.
4054 Return an Emacs frame object.
4055 PARAMETERS is an alist of frame parameters.
4056 If the parameters specify that the frame should not have a minibuffer,
4057 and do not specify a specific minibuffer window to use,
4058 then `default-minibuffer-frame' must be a frame whose minibuffer can
4059 be shared by the new frame.
4061 This function is an internal primitive--use `make-frame' instead. */)
4062 (Lisp_Object parameters)
4064 struct frame *f;
4065 Lisp_Object frame, tem;
4066 Lisp_Object name;
4067 int minibuffer_only = 0;
4068 long window_prompting = 0;
4069 int width, height;
4070 int count = SPECPDL_INDEX ();
4071 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4072 Lisp_Object display;
4073 struct w32_display_info *dpyinfo = NULL;
4074 Lisp_Object parent;
4075 struct kboard *kb;
4077 /* Make copy of frame parameters because the original is in pure
4078 storage now. */
4079 parameters = Fcopy_alist (parameters);
4081 /* Use this general default value to start with
4082 until we know if this frame has a specified name. */
4083 Vx_resource_name = Vinvocation_name;
4085 display = x_get_arg (dpyinfo, parameters, Qterminal, 0, 0, RES_TYPE_NUMBER);
4086 if (EQ (display, Qunbound))
4087 display = x_get_arg (dpyinfo, parameters, Qdisplay, 0, 0, RES_TYPE_STRING);
4088 if (EQ (display, Qunbound))
4089 display = Qnil;
4090 dpyinfo = check_x_display_info (display);
4091 kb = dpyinfo->terminal->kboard;
4093 if (!dpyinfo->terminal->name)
4094 error ("Terminal is not live, can't create new frames on it");
4096 name = x_get_arg (dpyinfo, parameters, Qname, "name", "Name", RES_TYPE_STRING);
4097 if (!STRINGP (name)
4098 && ! EQ (name, Qunbound)
4099 && ! NILP (name))
4100 error ("Invalid frame name--not a string or nil");
4102 if (STRINGP (name))
4103 Vx_resource_name = name;
4105 /* See if parent window is specified. */
4106 parent = x_get_arg (dpyinfo, parameters, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
4107 if (EQ (parent, Qunbound))
4108 parent = Qnil;
4109 if (! NILP (parent))
4110 CHECK_NUMBER (parent);
4112 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
4113 /* No need to protect DISPLAY because that's not used after passing
4114 it to make_frame_without_minibuffer. */
4115 frame = Qnil;
4116 GCPRO4 (parameters, parent, name, frame);
4117 tem = x_get_arg (dpyinfo, parameters, Qminibuffer, "minibuffer", "Minibuffer",
4118 RES_TYPE_SYMBOL);
4119 if (EQ (tem, Qnone) || NILP (tem))
4120 f = make_frame_without_minibuffer (Qnil, kb, display);
4121 else if (EQ (tem, Qonly))
4123 f = make_minibuffer_frame ();
4124 minibuffer_only = 1;
4126 else if (WINDOWP (tem))
4127 f = make_frame_without_minibuffer (tem, kb, display);
4128 else
4129 f = make_frame (1);
4131 XSETFRAME (frame, f);
4133 /* Note that Windows does support scroll bars. */
4134 FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;
4136 /* By default, make scrollbars the system standard width. */
4137 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = GetSystemMetrics (SM_CXVSCROLL);
4139 f->terminal = dpyinfo->terminal;
4140 f->terminal->reference_count++;
4142 f->output_method = output_w32;
4143 f->output_data.w32 =
4144 (struct w32_output *) xmalloc (sizeof (struct w32_output));
4145 memset (f->output_data.w32, 0, sizeof (struct w32_output));
4146 FRAME_FONTSET (f) = -1;
4148 f->icon_name
4149 = x_get_arg (dpyinfo, parameters, Qicon_name, "iconName", "Title",
4150 RES_TYPE_STRING);
4151 if (! STRINGP (f->icon_name))
4152 f->icon_name = Qnil;
4154 /* FRAME_W32_DISPLAY_INFO (f) = dpyinfo; */
4156 /* With FRAME_X_DISPLAY_INFO set up, this unwind-protect is safe. */
4157 record_unwind_protect (unwind_create_frame, frame);
4158 #if GLYPH_DEBUG
4159 image_cache_refcount = FRAME_IMAGE_CACHE (f)->refcount;
4160 dpyinfo_refcount = dpyinfo->reference_count;
4161 #endif /* GLYPH_DEBUG */
4163 /* Specify the parent under which to make this window. */
4165 if (!NILP (parent))
4167 f->output_data.w32->parent_desc = (Window) XFASTINT (parent);
4168 f->output_data.w32->explicit_parent = 1;
4170 else
4172 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
4173 f->output_data.w32->explicit_parent = 0;
4176 /* Set the name; the functions to which we pass f expect the name to
4177 be set. */
4178 if (EQ (name, Qunbound) || NILP (name))
4180 f->name = build_string (dpyinfo->w32_id_name);
4181 f->explicit_name = 0;
4183 else
4185 f->name = name;
4186 f->explicit_name = 1;
4187 /* use the frame's title when getting resources for this frame. */
4188 specbind (Qx_resource_name, name);
4191 f->resx = dpyinfo->resx;
4192 f->resy = dpyinfo->resy;
4194 if (uniscribe_available)
4195 register_font_driver (&uniscribe_font_driver, f);
4196 register_font_driver (&w32font_driver, f);
4198 x_default_parameter (f, parameters, Qfont_backend, Qnil,
4199 "fontBackend", "FontBackend", RES_TYPE_STRING);
4200 /* Extract the window parameters from the supplied values
4201 that are needed to determine window geometry. */
4202 x_default_font_parameter (f, parameters);
4203 x_default_parameter (f, parameters, Qborder_width, make_number (2),
4204 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
4206 /* We recognize either internalBorderWidth or internalBorder
4207 (which is what xterm calls it). */
4208 if (NILP (Fassq (Qinternal_border_width, parameters)))
4210 Lisp_Object value;
4212 value = x_get_arg (dpyinfo, parameters, Qinternal_border_width,
4213 "internalBorder", "InternalBorder", RES_TYPE_NUMBER);
4214 if (! EQ (value, Qunbound))
4215 parameters = Fcons (Fcons (Qinternal_border_width, value),
4216 parameters);
4218 /* Default internalBorderWidth to 0 on Windows to match other programs. */
4219 x_default_parameter (f, parameters, Qinternal_border_width, make_number (0),
4220 "internalBorderWidth", "InternalBorder", RES_TYPE_NUMBER);
4221 x_default_parameter (f, parameters, Qvertical_scroll_bars, Qright,
4222 "verticalScrollBars", "ScrollBars", RES_TYPE_SYMBOL);
4224 /* Also do the stuff which must be set before the window exists. */
4225 x_default_parameter (f, parameters, Qforeground_color, build_string ("black"),
4226 "foreground", "Foreground", RES_TYPE_STRING);
4227 x_default_parameter (f, parameters, Qbackground_color, build_string ("white"),
4228 "background", "Background", RES_TYPE_STRING);
4229 x_default_parameter (f, parameters, Qmouse_color, build_string ("black"),
4230 "pointerColor", "Foreground", RES_TYPE_STRING);
4231 x_default_parameter (f, parameters, Qborder_color, build_string ("black"),
4232 "borderColor", "BorderColor", RES_TYPE_STRING);
4233 x_default_parameter (f, parameters, Qscreen_gamma, Qnil,
4234 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
4235 x_default_parameter (f, parameters, Qline_spacing, Qnil,
4236 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
4237 x_default_parameter (f, parameters, Qleft_fringe, Qnil,
4238 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
4239 x_default_parameter (f, parameters, Qright_fringe, Qnil,
4240 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
4242 /* Init faces before x_default_parameter is called for scroll-bar
4243 parameters because that function calls x_set_scroll_bar_width,
4244 which calls change_frame_size, which calls Fset_window_buffer,
4245 which runs hooks, which call Fvertical_motion. At the end, we
4246 end up in init_iterator with a null face cache, which should not
4247 happen. */
4248 init_frame_faces (f);
4250 /* The X resources controlling the menu-bar and tool-bar are
4251 processed specially at startup, and reflected in the mode
4252 variables; ignore them here. */
4253 x_default_parameter (f, parameters, Qmenu_bar_lines,
4254 NILP (Vmenu_bar_mode)
4255 ? make_number (0) : make_number (1),
4256 NULL, NULL, RES_TYPE_NUMBER);
4257 x_default_parameter (f, parameters, Qtool_bar_lines,
4258 NILP (Vtool_bar_mode)
4259 ? make_number (0) : make_number (1),
4260 NULL, NULL, RES_TYPE_NUMBER);
4262 x_default_parameter (f, parameters, Qbuffer_predicate, Qnil,
4263 "bufferPredicate", "BufferPredicate", RES_TYPE_SYMBOL);
4264 x_default_parameter (f, parameters, Qtitle, Qnil,
4265 "title", "Title", RES_TYPE_STRING);
4266 x_default_parameter (f, parameters, Qfullscreen, Qnil,
4267 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
4269 f->output_data.w32->dwStyle = WS_OVERLAPPEDWINDOW;
4270 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
4272 f->output_data.w32->text_cursor = w32_load_cursor (IDC_IBEAM);
4273 f->output_data.w32->nontext_cursor = w32_load_cursor (IDC_ARROW);
4274 f->output_data.w32->modeline_cursor = w32_load_cursor (IDC_ARROW);
4275 f->output_data.w32->hand_cursor = w32_load_cursor (IDC_HAND);
4276 f->output_data.w32->hourglass_cursor = w32_load_cursor (IDC_WAIT);
4277 f->output_data.w32->horizontal_drag_cursor = w32_load_cursor (IDC_SIZEWE);
4279 f->output_data.w32->current_cursor = f->output_data.w32->nontext_cursor;
4281 window_prompting = x_figure_window_size (f, parameters, 1);
4283 tem = x_get_arg (dpyinfo, parameters, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
4284 f->no_split = minibuffer_only || EQ (tem, Qt);
4286 w32_window (f, window_prompting, minibuffer_only);
4287 x_icon (f, parameters);
4289 x_make_gc (f);
4291 /* Now consider the frame official. */
4292 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
4293 Vframe_list = Fcons (frame, Vframe_list);
4295 /* We need to do this after creating the window, so that the
4296 icon-creation functions can say whose icon they're describing. */
4297 x_default_parameter (f, parameters, Qicon_type, Qnil,
4298 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
4300 x_default_parameter (f, parameters, Qauto_raise, Qnil,
4301 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4302 x_default_parameter (f, parameters, Qauto_lower, Qnil,
4303 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4304 x_default_parameter (f, parameters, Qcursor_type, Qbox,
4305 "cursorType", "CursorType", RES_TYPE_SYMBOL);
4306 x_default_parameter (f, parameters, Qscroll_bar_width, Qnil,
4307 "scrollBarWidth", "ScrollBarWidth", RES_TYPE_NUMBER);
4308 x_default_parameter (f, parameters, Qalpha, Qnil,
4309 "alpha", "Alpha", RES_TYPE_NUMBER);
4311 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
4312 Change will not be effected unless different from the current
4313 FRAME_LINES (f). */
4314 width = FRAME_COLS (f);
4315 height = FRAME_LINES (f);
4317 FRAME_LINES (f) = 0;
4318 SET_FRAME_COLS (f, 0);
4319 change_frame_size (f, height, width, 1, 0, 0);
4321 /* Tell the server what size and position, etc, we want, and how
4322 badly we want them. This should be done after we have the menu
4323 bar so that its size can be taken into account. */
4324 BLOCK_INPUT;
4325 x_wm_set_size_hint (f, window_prompting, 0);
4326 UNBLOCK_INPUT;
4328 /* Make the window appear on the frame and enable display, unless
4329 the caller says not to. However, with explicit parent, Emacs
4330 cannot control visibility, so don't try. */
4331 if (! f->output_data.w32->explicit_parent)
4333 Lisp_Object visibility;
4335 visibility = x_get_arg (dpyinfo, parameters, Qvisibility, 0, 0, RES_TYPE_SYMBOL);
4336 if (EQ (visibility, Qunbound))
4337 visibility = Qt;
4339 if (EQ (visibility, Qicon))
4340 x_iconify_frame (f);
4341 else if (! NILP (visibility))
4342 x_make_frame_visible (f);
4343 else
4344 /* Must have been Qnil. */
4348 /* Initialize `default-minibuffer-frame' in case this is the first
4349 frame on this terminal. */
4350 if (FRAME_HAS_MINIBUF_P (f)
4351 && (!FRAMEP (kb->Vdefault_minibuffer_frame)
4352 || !FRAME_LIVE_P (XFRAME (kb->Vdefault_minibuffer_frame))))
4353 kb->Vdefault_minibuffer_frame = frame;
4355 /* All remaining specified parameters, which have not been "used"
4356 by x_get_arg and friends, now go in the misc. alist of the frame. */
4357 for (tem = parameters; CONSP (tem); tem = XCDR (tem))
4358 if (CONSP (XCAR (tem)) && !NILP (XCAR (XCAR (tem))))
4359 f->param_alist = Fcons (XCAR (tem), f->param_alist);
4361 UNGCPRO;
4363 /* Make sure windows on this frame appear in calls to next-window
4364 and similar functions. */
4365 Vwindow_list = Qnil;
4367 return unbind_to (count, frame);
4370 /* FRAME is used only to get a handle on the X display. We don't pass the
4371 display info directly because we're called from frame.c, which doesn't
4372 know about that structure. */
4373 Lisp_Object
4374 x_get_focus_frame (struct frame *frame)
4376 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (frame);
4377 Lisp_Object xfocus;
4378 if (! dpyinfo->w32_focus_frame)
4379 return Qnil;
4381 XSETFRAME (xfocus, dpyinfo->w32_focus_frame);
4382 return xfocus;
4385 DEFUN ("x-focus-frame", Fx_focus_frame, Sx_focus_frame, 1, 1, 0,
4386 doc: /* Give FRAME input focus, raising to foreground if necessary. */)
4387 (Lisp_Object frame)
4389 x_focus_on_frame (check_x_frame (frame));
4390 return Qnil;
4394 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
4395 doc: /* Internal function called by `color-defined-p', which see.
4396 \(Note that the Nextstep version of this function ignores FRAME.) */)
4397 (Lisp_Object color, Lisp_Object frame)
4399 XColor foo;
4400 FRAME_PTR f = check_x_frame (frame);
4402 CHECK_STRING (color);
4404 if (w32_defined_color (f, SDATA (color), &foo, 0))
4405 return Qt;
4406 else
4407 return Qnil;
4410 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
4411 doc: /* Internal function called by `color-values', which see. */)
4412 (Lisp_Object color, Lisp_Object frame)
4414 XColor foo;
4415 FRAME_PTR f = check_x_frame (frame);
4417 CHECK_STRING (color);
4419 if (w32_defined_color (f, SDATA (color), &foo, 0))
4420 return list3 (make_number ((GetRValue (foo.pixel) << 8)
4421 | GetRValue (foo.pixel)),
4422 make_number ((GetGValue (foo.pixel) << 8)
4423 | GetGValue (foo.pixel)),
4424 make_number ((GetBValue (foo.pixel) << 8)
4425 | GetBValue (foo.pixel)));
4426 else
4427 return Qnil;
4430 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
4431 doc: /* Internal function called by `display-color-p', which see. */)
4432 (Lisp_Object display)
4434 struct w32_display_info *dpyinfo = check_x_display_info (display);
4436 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 2)
4437 return Qnil;
4439 return Qt;
4442 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p,
4443 Sx_display_grayscale_p, 0, 1, 0,
4444 doc: /* Return t if DISPLAY supports shades of gray.
4445 Note that color displays do support shades of gray.
4446 The optional argument DISPLAY specifies which display to ask about.
4447 DISPLAY should be either a frame or a display name (a string).
4448 If omitted or nil, that stands for the selected frame's display. */)
4449 (Lisp_Object display)
4451 struct w32_display_info *dpyinfo = check_x_display_info (display);
4453 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 1)
4454 return Qnil;
4456 return Qt;
4459 DEFUN ("x-display-pixel-width", Fx_display_pixel_width,
4460 Sx_display_pixel_width, 0, 1, 0,
4461 doc: /* Return the width in pixels of DISPLAY.
4462 The optional argument DISPLAY specifies which display to ask about.
4463 DISPLAY should be either a frame or a display name (a string).
4464 If omitted or nil, that stands for the selected frame's display. */)
4465 (Lisp_Object display)
4467 struct w32_display_info *dpyinfo = check_x_display_info (display);
4469 return make_number (x_display_pixel_width (dpyinfo));
4472 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
4473 Sx_display_pixel_height, 0, 1, 0,
4474 doc: /* Return the height in pixels of DISPLAY.
4475 The optional argument DISPLAY specifies which display to ask about.
4476 DISPLAY should be either a frame or a display name (a string).
4477 If omitted or nil, that stands for the selected frame's display. */)
4478 (Lisp_Object display)
4480 struct w32_display_info *dpyinfo = check_x_display_info (display);
4482 return make_number (x_display_pixel_height (dpyinfo));
4485 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
4486 0, 1, 0,
4487 doc: /* Return the number of bitplanes of DISPLAY.
4488 The optional argument DISPLAY specifies which display to ask about.
4489 DISPLAY should be either a frame or a display name (a string).
4490 If omitted or nil, that stands for the selected frame's display. */)
4491 (Lisp_Object display)
4493 struct w32_display_info *dpyinfo = check_x_display_info (display);
4495 return make_number (dpyinfo->n_planes * dpyinfo->n_cbits);
4498 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
4499 0, 1, 0,
4500 doc: /* Return the number of color cells of DISPLAY.
4501 The optional argument DISPLAY specifies which display to ask about.
4502 DISPLAY should be either a frame or a display name (a string).
4503 If omitted or nil, that stands for the selected frame's display. */)
4504 (Lisp_Object display)
4506 struct w32_display_info *dpyinfo = check_x_display_info (display);
4507 HDC hdc;
4508 int cap;
4510 hdc = GetDC (dpyinfo->root_window);
4511 if (dpyinfo->has_palette)
4512 cap = GetDeviceCaps (hdc, SIZEPALETTE);
4513 else
4514 cap = GetDeviceCaps (hdc, NUMCOLORS);
4516 /* We force 24+ bit depths to 24-bit, both to prevent an overflow
4517 and because probably is more meaningful on Windows anyway */
4518 if (cap < 0)
4519 cap = 1 << min (dpyinfo->n_planes * dpyinfo->n_cbits, 24);
4521 ReleaseDC (dpyinfo->root_window, hdc);
4523 return make_number (cap);
4526 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
4527 Sx_server_max_request_size,
4528 0, 1, 0,
4529 doc: /* Return the maximum request size of the server of DISPLAY.
4530 The optional argument DISPLAY specifies which display to ask about.
4531 DISPLAY should be either a frame or a display name (a string).
4532 If omitted or nil, that stands for the selected frame's display. */)
4533 (Lisp_Object display)
4535 struct w32_display_info *dpyinfo = check_x_display_info (display);
4537 return make_number (1);
4540 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
4541 doc: /* Return the "vendor ID" string of the W32 system (Microsoft).
4542 The optional argument DISPLAY specifies which display to ask about.
4543 DISPLAY should be either a frame or a display name (a string).
4544 If omitted or nil, that stands for the selected frame's display. */)
4545 (Lisp_Object display)
4547 return build_string ("Microsoft Corp.");
4550 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
4551 doc: /* Return the version numbers of the server of DISPLAY.
4552 The value is a list of three integers: the major and minor
4553 version numbers of the X Protocol in use, and the distributor-specific
4554 release number. See also the function `x-server-vendor'.
4556 The optional argument DISPLAY specifies which display to ask about.
4557 DISPLAY should be either a frame or a display name (a string).
4558 If omitted or nil, that stands for the selected frame's display. */)
4559 (Lisp_Object display)
4561 return Fcons (make_number (w32_major_version),
4562 Fcons (make_number (w32_minor_version),
4563 Fcons (make_number (w32_build_number), Qnil)));
4566 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
4567 doc: /* Return the number of screens on the server of DISPLAY.
4568 The optional argument DISPLAY specifies which display to ask about.
4569 DISPLAY should be either a frame or a display name (a string).
4570 If omitted or nil, that stands for the selected frame's display. */)
4571 (Lisp_Object display)
4573 return make_number (1);
4576 DEFUN ("x-display-mm-height", Fx_display_mm_height,
4577 Sx_display_mm_height, 0, 1, 0,
4578 doc: /* Return the height in millimeters of DISPLAY.
4579 The optional argument DISPLAY specifies which display to ask about.
4580 DISPLAY should be either a frame or a display name (a string).
4581 If omitted or nil, that stands for the selected frame's display. */)
4582 (Lisp_Object display)
4584 struct w32_display_info *dpyinfo = check_x_display_info (display);
4585 HDC hdc;
4586 int cap;
4588 hdc = GetDC (dpyinfo->root_window);
4590 cap = GetDeviceCaps (hdc, VERTSIZE);
4592 ReleaseDC (dpyinfo->root_window, hdc);
4594 return make_number (cap);
4597 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
4598 doc: /* Return the width in millimeters of DISPLAY.
4599 The optional argument DISPLAY specifies which display to ask about.
4600 DISPLAY should be either a frame or a display name (a string).
4601 If omitted or nil, that stands for the selected frame's display. */)
4602 (Lisp_Object display)
4604 struct w32_display_info *dpyinfo = check_x_display_info (display);
4606 HDC hdc;
4607 int cap;
4609 hdc = GetDC (dpyinfo->root_window);
4611 cap = GetDeviceCaps (hdc, HORZSIZE);
4613 ReleaseDC (dpyinfo->root_window, hdc);
4615 return make_number (cap);
4618 DEFUN ("x-display-backing-store", Fx_display_backing_store,
4619 Sx_display_backing_store, 0, 1, 0,
4620 doc: /* Return an indication of whether DISPLAY does backing store.
4621 The value may be `always', `when-mapped', or `not-useful'.
4622 The optional argument DISPLAY specifies which display to ask about.
4623 DISPLAY should be either a frame or a display name (a string).
4624 If omitted or nil, that stands for the selected frame's display. */)
4625 (Lisp_Object display)
4627 return intern ("not-useful");
4630 DEFUN ("x-display-visual-class", Fx_display_visual_class,
4631 Sx_display_visual_class, 0, 1, 0,
4632 doc: /* Return the visual class of DISPLAY.
4633 The value is one of the symbols `static-gray', `gray-scale',
4634 `static-color', `pseudo-color', `true-color', or `direct-color'.
4636 The optional argument DISPLAY specifies which display to ask about.
4637 DISPLAY should be either a frame or a display name (a string).
4638 If omitted or nil, that stands for the selected frame's display. */)
4639 (Lisp_Object display)
4641 struct w32_display_info *dpyinfo = check_x_display_info (display);
4642 Lisp_Object result = Qnil;
4644 if (dpyinfo->has_palette)
4645 result = intern ("pseudo-color");
4646 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 1)
4647 result = intern ("static-grey");
4648 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 4)
4649 result = intern ("static-color");
4650 else if (dpyinfo->n_planes * dpyinfo->n_cbits > 8)
4651 result = intern ("true-color");
4653 return result;
4656 DEFUN ("x-display-save-under", Fx_display_save_under,
4657 Sx_display_save_under, 0, 1, 0,
4658 doc: /* Return t if DISPLAY supports the save-under feature.
4659 The optional argument DISPLAY specifies which display to ask about.
4660 DISPLAY should be either a frame or a display name (a string).
4661 If omitted or nil, that stands for the selected frame's display. */)
4662 (Lisp_Object display)
4664 return Qnil;
4668 x_pixel_width (register struct frame *f)
4670 return FRAME_PIXEL_WIDTH (f);
4674 x_pixel_height (register struct frame *f)
4676 return FRAME_PIXEL_HEIGHT (f);
4680 x_char_width (register struct frame *f)
4682 return FRAME_COLUMN_WIDTH (f);
4686 x_char_height (register struct frame *f)
4688 return FRAME_LINE_HEIGHT (f);
4692 x_screen_planes (register struct frame *f)
4694 return FRAME_W32_DISPLAY_INFO (f)->n_planes;
4697 /* Return the display structure for the display named NAME.
4698 Open a new connection if necessary. */
4700 struct w32_display_info *
4701 x_display_info_for_name (Lisp_Object name)
4703 Lisp_Object names;
4704 struct w32_display_info *dpyinfo;
4706 CHECK_STRING (name);
4708 for (dpyinfo = &one_w32_display_info, names = w32_display_name_list;
4709 dpyinfo;
4710 dpyinfo = dpyinfo->next, names = XCDR (names))
4712 Lisp_Object tem;
4713 tem = Fstring_equal (XCAR (XCAR (names)), name);
4714 if (!NILP (tem))
4715 return dpyinfo;
4718 /* Use this general default value to start with. */
4719 Vx_resource_name = Vinvocation_name;
4721 validate_x_resource_name ();
4723 dpyinfo = w32_term_init (name, (unsigned char *)0,
4724 SSDATA (Vx_resource_name));
4726 if (dpyinfo == 0)
4727 error ("Cannot connect to server %s", SDATA (name));
4729 w32_in_use = 1;
4730 XSETFASTINT (Vwindow_system_version, w32_major_version);
4732 return dpyinfo;
4735 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
4736 1, 3, 0, doc: /* Open a connection to a display server.
4737 DISPLAY is the name of the display to connect to.
4738 Optional second arg XRM-STRING is a string of resources in xrdb format.
4739 If the optional third arg MUST-SUCCEED is non-nil,
4740 terminate Emacs if we can't open the connection.
4741 \(In the Nextstep version, the last two arguments are currently ignored.) */)
4742 (Lisp_Object display, Lisp_Object xrm_string, Lisp_Object must_succeed)
4744 unsigned char *xrm_option;
4745 struct w32_display_info *dpyinfo;
4747 /* If initialization has already been done, return now to avoid
4748 overwriting critical parts of one_w32_display_info. */
4749 if (w32_in_use)
4750 return Qnil;
4752 CHECK_STRING (display);
4753 if (! NILP (xrm_string))
4754 CHECK_STRING (xrm_string);
4756 #if 0
4757 if (! EQ (Vwindow_system, intern ("w32")))
4758 error ("Not using Microsoft Windows");
4759 #endif
4761 /* Allow color mapping to be defined externally; first look in user's
4762 HOME directory, then in Emacs etc dir for a file called rgb.txt. */
4764 Lisp_Object color_file;
4765 struct gcpro gcpro1;
4767 color_file = build_string ("~/rgb.txt");
4769 GCPRO1 (color_file);
4771 if (NILP (Ffile_readable_p (color_file)))
4772 color_file =
4773 Fexpand_file_name (build_string ("rgb.txt"),
4774 Fsymbol_value (intern ("data-directory")));
4776 Vw32_color_map = Fx_load_color_file (color_file);
4778 UNGCPRO;
4780 if (NILP (Vw32_color_map))
4781 Vw32_color_map = Fw32_default_color_map ();
4783 /* Merge in system logical colors. */
4784 add_system_logical_colors_to_map (&Vw32_color_map);
4786 if (! NILP (xrm_string))
4787 xrm_option = SDATA (xrm_string);
4788 else
4789 xrm_option = (unsigned char *) 0;
4791 /* Use this general default value to start with. */
4792 /* First remove .exe suffix from invocation-name - it looks ugly. */
4794 char basename[ MAX_PATH ], *str;
4796 strcpy (basename, SDATA (Vinvocation_name));
4797 str = strrchr (basename, '.');
4798 if (str) *str = 0;
4799 Vinvocation_name = build_string (basename);
4801 Vx_resource_name = Vinvocation_name;
4803 validate_x_resource_name ();
4805 /* This is what opens the connection and sets x_current_display.
4806 This also initializes many symbols, such as those used for input. */
4807 dpyinfo = w32_term_init (display, xrm_option,
4808 SSDATA (Vx_resource_name));
4810 if (dpyinfo == 0)
4812 if (!NILP (must_succeed))
4813 fatal ("Cannot connect to server %s.\n",
4814 SDATA (display));
4815 else
4816 error ("Cannot connect to server %s", SDATA (display));
4819 w32_in_use = 1;
4821 XSETFASTINT (Vwindow_system_version, w32_major_version);
4822 return Qnil;
4825 DEFUN ("x-close-connection", Fx_close_connection,
4826 Sx_close_connection, 1, 1, 0,
4827 doc: /* Close the connection to DISPLAY's server.
4828 For DISPLAY, specify either a frame or a display name (a string).
4829 If DISPLAY is nil, that stands for the selected frame's display. */)
4830 (Lisp_Object display)
4832 struct w32_display_info *dpyinfo = check_x_display_info (display);
4833 int i;
4835 if (dpyinfo->reference_count > 0)
4836 error ("Display still has frames on it");
4838 BLOCK_INPUT;
4839 x_destroy_all_bitmaps (dpyinfo);
4841 x_delete_display (dpyinfo);
4842 UNBLOCK_INPUT;
4844 return Qnil;
4847 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
4848 doc: /* Return the list of display names that Emacs has connections to. */)
4849 (void)
4851 Lisp_Object tail, result;
4853 result = Qnil;
4854 for (tail = w32_display_name_list; CONSP (tail); tail = XCDR (tail))
4855 result = Fcons (XCAR (XCAR (tail)), result);
4857 return result;
4860 DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
4861 doc: /* If ON is non-nil, report X errors as soon as the erring request is made.
4862 This function only has an effect on X Windows. With MS Windows, it is
4863 defined but does nothing.
4865 If ON is nil, allow buffering of requests.
4866 Turning on synchronization prohibits the Xlib routines from buffering
4867 requests and seriously degrades performance, but makes debugging much
4868 easier.
4869 The optional second argument TERMINAL specifies which display to act on.
4870 TERMINAL should be a terminal object, a frame or a display name (a string).
4871 If TERMINAL is omitted or nil, that stands for the selected frame's display. */)
4872 (Lisp_Object on, Lisp_Object display)
4874 return Qnil;
4879 /***********************************************************************
4880 Window properties
4881 ***********************************************************************/
4883 DEFUN ("x-change-window-property", Fx_change_window_property,
4884 Sx_change_window_property, 2, 6, 0,
4885 doc: /* Change window property PROP to VALUE on the X window of FRAME.
4886 PROP must be a string. VALUE may be a string or a list of conses,
4887 numbers and/or strings. If an element in the list is a string, it is
4888 converted to an atom and the value of the Atom is used. If an element
4889 is a cons, it is converted to a 32 bit number where the car is the 16
4890 top bits and the cdr is the lower 16 bits.
4892 FRAME nil or omitted means use the selected frame.
4893 If TYPE is given and non-nil, it is the name of the type of VALUE.
4894 If TYPE is not given or nil, the type is STRING.
4895 FORMAT gives the size in bits of each element if VALUE is a list.
4896 It must be one of 8, 16 or 32.
4897 If VALUE is a string or FORMAT is nil or not given, FORMAT defaults to 8.
4898 If OUTER_P is non-nil, the property is changed for the outer X window of
4899 FRAME. Default is to change on the edit X window. */)
4900 (Lisp_Object prop, Lisp_Object value, Lisp_Object frame, Lisp_Object type, Lisp_Object format, Lisp_Object outer_p)
4902 #if 0 /* TODO : port window properties to W32 */
4903 struct frame *f = check_x_frame (frame);
4904 Atom prop_atom;
4906 CHECK_STRING (prop);
4907 CHECK_STRING (value);
4909 BLOCK_INPUT;
4910 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
4911 XChangeProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
4912 prop_atom, XA_STRING, 8, PropModeReplace,
4913 SDATA (value), SCHARS (value));
4915 /* Make sure the property is set when we return. */
4916 XFlush (FRAME_W32_DISPLAY (f));
4917 UNBLOCK_INPUT;
4919 #endif /* TODO */
4921 return value;
4925 DEFUN ("x-delete-window-property", Fx_delete_window_property,
4926 Sx_delete_window_property, 1, 2, 0,
4927 doc: /* Remove window property PROP from X window of FRAME.
4928 FRAME nil or omitted means use the selected frame. Value is PROP. */)
4929 (Lisp_Object prop, Lisp_Object frame)
4931 #if 0 /* TODO : port window properties to W32 */
4933 struct frame *f = check_x_frame (frame);
4934 Atom prop_atom;
4936 CHECK_STRING (prop);
4937 BLOCK_INPUT;
4938 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
4939 XDeleteProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), prop_atom);
4941 /* Make sure the property is removed when we return. */
4942 XFlush (FRAME_W32_DISPLAY (f));
4943 UNBLOCK_INPUT;
4944 #endif /* TODO */
4946 return prop;
4950 DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
4951 1, 2, 0,
4952 doc: /* Value is the value of window property PROP on FRAME.
4953 If FRAME is nil or omitted, use the selected frame.
4955 On MS Windows, this function only accepts the PROP and FRAME arguments.
4957 On X Windows, the following optional arguments are also accepted:
4958 If TYPE is nil or omitted, get the property as a string.
4959 Otherwise TYPE is the name of the atom that denotes the type expected.
4960 If SOURCE is non-nil, get the property on that window instead of from
4961 FRAME. The number 0 denotes the root window.
4962 If DELETE_P is non-nil, delete the property after retreiving it.
4963 If VECTOR_RET_P is non-nil, don't return a string but a vector of values.
4965 Value is nil if FRAME hasn't a property with name PROP or if PROP has
4966 no value of TYPE (always string in the MS Windows case). */)
4967 (Lisp_Object prop, Lisp_Object frame)
4969 #if 0 /* TODO : port window properties to W32 */
4971 struct frame *f = check_x_frame (frame);
4972 Atom prop_atom;
4973 int rc;
4974 Lisp_Object prop_value = Qnil;
4975 char *tmp_data = NULL;
4976 Atom actual_type;
4977 int actual_format;
4978 unsigned long actual_size, bytes_remaining;
4980 CHECK_STRING (prop);
4981 BLOCK_INPUT;
4982 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
4983 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
4984 prop_atom, 0, 0, False, XA_STRING,
4985 &actual_type, &actual_format, &actual_size,
4986 &bytes_remaining, (unsigned char **) &tmp_data);
4987 if (rc == Success)
4989 int size = bytes_remaining;
4991 XFree (tmp_data);
4992 tmp_data = NULL;
4994 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
4995 prop_atom, 0, bytes_remaining,
4996 False, XA_STRING,
4997 &actual_type, &actual_format,
4998 &actual_size, &bytes_remaining,
4999 (unsigned char **) &tmp_data);
5000 if (rc == Success)
5001 prop_value = make_string (tmp_data, size);
5003 XFree (tmp_data);
5006 UNBLOCK_INPUT;
5008 return prop_value;
5010 #endif /* TODO */
5011 return Qnil;
5016 /***********************************************************************
5017 Busy cursor
5018 ***********************************************************************/
5020 /* Default number of seconds to wait before displaying an hourglass
5021 cursor. Duplicated from xdisp.c, but cannot use the version there
5022 due to lack of atimers on w32. */
5023 #define DEFAULT_HOURGLASS_DELAY 1
5024 /* Return non-zero if houglass timer has been started or hourglass is shown. */
5025 /* PENDING: if W32 can use atimers (atimer.[hc]) then the common impl in
5026 xdisp.c could be used. */
5029 hourglass_started (void)
5031 return hourglass_shown_p || hourglass_timer;
5034 /* Cancel a currently active hourglass timer, and start a new one. */
5036 void
5037 start_hourglass (void)
5039 DWORD delay;
5040 int secs, msecs = 0;
5041 struct frame * f = SELECTED_FRAME ();
5043 /* No cursors on non GUI frames. */
5044 if (!FRAME_W32_P (f))
5045 return;
5047 cancel_hourglass ();
5049 if (INTEGERP (Vhourglass_delay)
5050 && XINT (Vhourglass_delay) > 0)
5051 secs = XFASTINT (Vhourglass_delay);
5052 else if (FLOATP (Vhourglass_delay)
5053 && XFLOAT_DATA (Vhourglass_delay) > 0)
5055 Lisp_Object tem;
5056 tem = Ftruncate (Vhourglass_delay, Qnil);
5057 secs = XFASTINT (tem);
5058 msecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000;
5060 else
5061 secs = DEFAULT_HOURGLASS_DELAY;
5063 delay = secs * 1000 + msecs;
5064 hourglass_hwnd = FRAME_W32_WINDOW (f);
5065 hourglass_timer = SetTimer (hourglass_hwnd, HOURGLASS_ID, delay, NULL);
5069 /* Cancel the hourglass cursor timer if active, hide an hourglass
5070 cursor if shown. */
5072 void
5073 cancel_hourglass (void)
5075 if (hourglass_timer)
5077 KillTimer (hourglass_hwnd, hourglass_timer);
5078 hourglass_timer = 0;
5081 if (hourglass_shown_p)
5082 w32_hide_hourglass ();
5086 /* Timer function of hourglass_timer.
5088 Display an hourglass cursor. Set the hourglass_p flag in display info
5089 to indicate that an hourglass cursor is shown. */
5091 static void
5092 w32_show_hourglass (struct frame *f)
5094 if (!hourglass_shown_p)
5096 f->output_data.w32->hourglass_p = 1;
5097 if (!menubar_in_use && !current_popup_menu)
5098 SetCursor (f->output_data.w32->hourglass_cursor);
5099 hourglass_shown_p = 1;
5104 /* Hide the hourglass cursor on all frames, if it is currently shown. */
5106 static void
5107 w32_hide_hourglass (void)
5109 if (hourglass_shown_p)
5111 struct frame *f = x_window_to_frame (&one_w32_display_info,
5112 hourglass_hwnd);
5113 if (f)
5114 f->output_data.w32->hourglass_p = 0;
5115 else
5116 /* If frame was deleted, restore to selected frame's cursor. */
5117 f = SELECTED_FRAME ();
5119 if (FRAME_W32_P (f))
5120 SetCursor (f->output_data.w32->current_cursor);
5121 else
5122 /* No cursors on non GUI frames - restore to stock arrow cursor. */
5123 SetCursor (w32_load_cursor (IDC_ARROW));
5125 hourglass_shown_p = 0;
5131 /***********************************************************************
5132 Tool tips
5133 ***********************************************************************/
5135 static Lisp_Object x_create_tip_frame (struct w32_display_info *,
5136 Lisp_Object, Lisp_Object);
5137 static void compute_tip_xy (struct frame *, Lisp_Object, Lisp_Object,
5138 Lisp_Object, int, int, int *, int *);
5140 /* The frame of a currently visible tooltip. */
5142 Lisp_Object tip_frame;
5144 /* If non-nil, a timer started that hides the last tooltip when it
5145 fires. */
5147 Lisp_Object tip_timer;
5148 Window tip_window;
5150 /* If non-nil, a vector of 3 elements containing the last args
5151 with which x-show-tip was called. See there. */
5153 Lisp_Object last_show_tip_args;
5156 static Lisp_Object
5157 unwind_create_tip_frame (Lisp_Object frame)
5159 Lisp_Object deleted;
5161 deleted = unwind_create_frame (frame);
5162 if (EQ (deleted, Qt))
5164 tip_window = NULL;
5165 tip_frame = Qnil;
5168 return deleted;
5172 /* Create a frame for a tooltip on the display described by DPYINFO.
5173 PARMS is a list of frame parameters. TEXT is the string to
5174 display in the tip frame. Value is the frame.
5176 Note that functions called here, esp. x_default_parameter can
5177 signal errors, for instance when a specified color name is
5178 undefined. We have to make sure that we're in a consistent state
5179 when this happens. */
5181 static Lisp_Object
5182 x_create_tip_frame (struct w32_display_info *dpyinfo,
5183 Lisp_Object parms, Lisp_Object text)
5185 struct frame *f;
5186 Lisp_Object frame, tem;
5187 Lisp_Object name;
5188 long window_prompting = 0;
5189 int width, height;
5190 int count = SPECPDL_INDEX ();
5191 struct gcpro gcpro1, gcpro2, gcpro3;
5192 struct kboard *kb;
5193 int face_change_count_before = face_change_count;
5194 Lisp_Object buffer;
5195 struct buffer *old_buffer;
5197 check_w32 ();
5199 /* Use this general default value to start with until we know if
5200 this frame has a specified name. */
5201 Vx_resource_name = Vinvocation_name;
5203 kb = dpyinfo->terminal->kboard;
5205 /* The calls to x_get_arg remove elements from PARMS, so copy it to
5206 avoid destructive changes behind our caller's back. */
5207 parms = Fcopy_alist (parms);
5209 /* Get the name of the frame to use for resource lookup. */
5210 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
5211 if (!STRINGP (name)
5212 && !EQ (name, Qunbound)
5213 && !NILP (name))
5214 error ("Invalid frame name--not a string or nil");
5215 Vx_resource_name = name;
5217 frame = Qnil;
5218 GCPRO3 (parms, name, frame);
5219 /* Make a frame without minibuffer nor mode-line. */
5220 f = make_frame (0);
5221 f->wants_modeline = 0;
5222 XSETFRAME (frame, f);
5224 buffer = Fget_buffer_create (build_string (" *tip*"));
5225 Fset_window_buffer (FRAME_ROOT_WINDOW (f), buffer, Qnil);
5226 old_buffer = current_buffer;
5227 set_buffer_internal_1 (XBUFFER (buffer));
5228 current_buffer->truncate_lines = Qnil;
5229 specbind (Qinhibit_read_only, Qt);
5230 specbind (Qinhibit_modification_hooks, Qt);
5231 Ferase_buffer ();
5232 Finsert (1, &text);
5233 set_buffer_internal_1 (old_buffer);
5235 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
5236 record_unwind_protect (unwind_create_tip_frame, frame);
5238 /* By setting the output method, we're essentially saying that
5239 the frame is live, as per FRAME_LIVE_P. If we get a signal
5240 from this point on, x_destroy_window might screw up reference
5241 counts etc. */
5242 f->terminal = dpyinfo->terminal;
5243 f->terminal->reference_count++;
5244 f->output_method = output_w32;
5245 f->output_data.w32 =
5246 (struct w32_output *) xmalloc (sizeof (struct w32_output));
5247 memset (f->output_data.w32, 0, sizeof (struct w32_output));
5249 FRAME_FONTSET (f) = -1;
5250 f->icon_name = Qnil;
5252 #if GLYPH_DEBUG
5253 image_cache_refcount = FRAME_IMAGE_CACHE (f)->refcount;
5254 dpyinfo_refcount = dpyinfo->reference_count;
5255 #endif /* GLYPH_DEBUG */
5256 FRAME_KBOARD (f) = kb;
5257 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
5258 f->output_data.w32->explicit_parent = 0;
5260 /* Set the name; the functions to which we pass f expect the name to
5261 be set. */
5262 if (EQ (name, Qunbound) || NILP (name))
5264 f->name = build_string (dpyinfo->w32_id_name);
5265 f->explicit_name = 0;
5267 else
5269 f->name = name;
5270 f->explicit_name = 1;
5271 /* use the frame's title when getting resources for this frame. */
5272 specbind (Qx_resource_name, name);
5275 f->resx = dpyinfo->resx;
5276 f->resy = dpyinfo->resy;
5278 if (uniscribe_available)
5279 register_font_driver (&uniscribe_font_driver, f);
5280 register_font_driver (&w32font_driver, f);
5282 x_default_parameter (f, parms, Qfont_backend, Qnil,
5283 "fontBackend", "FontBackend", RES_TYPE_STRING);
5285 /* Extract the window parameters from the supplied values
5286 that are needed to determine window geometry. */
5287 x_default_font_parameter (f, parms);
5289 x_default_parameter (f, parms, Qborder_width, make_number (2),
5290 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
5291 /* This defaults to 2 in order to match xterm. We recognize either
5292 internalBorderWidth or internalBorder (which is what xterm calls
5293 it). */
5294 if (NILP (Fassq (Qinternal_border_width, parms)))
5296 Lisp_Object value;
5298 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
5299 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
5300 if (! EQ (value, Qunbound))
5301 parms = Fcons (Fcons (Qinternal_border_width, value),
5302 parms);
5304 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
5305 "internalBorderWidth", "internalBorderWidth",
5306 RES_TYPE_NUMBER);
5308 /* Also do the stuff which must be set before the window exists. */
5309 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
5310 "foreground", "Foreground", RES_TYPE_STRING);
5311 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
5312 "background", "Background", RES_TYPE_STRING);
5313 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
5314 "pointerColor", "Foreground", RES_TYPE_STRING);
5315 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
5316 "cursorColor", "Foreground", RES_TYPE_STRING);
5317 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
5318 "borderColor", "BorderColor", RES_TYPE_STRING);
5320 /* Init faces before x_default_parameter is called for scroll-bar
5321 parameters because that function calls x_set_scroll_bar_width,
5322 which calls change_frame_size, which calls Fset_window_buffer,
5323 which runs hooks, which call Fvertical_motion. At the end, we
5324 end up in init_iterator with a null face cache, which should not
5325 happen. */
5326 init_frame_faces (f);
5328 f->output_data.w32->dwStyle = WS_BORDER | WS_POPUP | WS_DISABLED;
5329 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
5331 window_prompting = x_figure_window_size (f, parms, 0);
5333 /* No fringes on tip frame. */
5334 f->fringe_cols = 0;
5335 f->left_fringe_width = 0;
5336 f->right_fringe_width = 0;
5338 BLOCK_INPUT;
5339 my_create_tip_window (f);
5340 UNBLOCK_INPUT;
5342 x_make_gc (f);
5344 x_default_parameter (f, parms, Qauto_raise, Qnil,
5345 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5346 x_default_parameter (f, parms, Qauto_lower, Qnil,
5347 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5348 x_default_parameter (f, parms, Qcursor_type, Qbox,
5349 "cursorType", "CursorType", RES_TYPE_SYMBOL);
5351 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
5352 Change will not be effected unless different from the current
5353 FRAME_LINES (f). */
5354 width = FRAME_COLS (f);
5355 height = FRAME_LINES (f);
5356 FRAME_LINES (f) = 0;
5357 SET_FRAME_COLS (f, 0);
5358 change_frame_size (f, height, width, 1, 0, 0);
5360 /* Add `tooltip' frame parameter's default value. */
5361 if (NILP (Fframe_parameter (frame, Qtooltip)))
5362 Fmodify_frame_parameters (frame, Fcons (Fcons (Qtooltip, Qt), Qnil));
5364 /* Set up faces after all frame parameters are known. This call
5365 also merges in face attributes specified for new frames.
5367 Frame parameters may be changed if .Xdefaults contains
5368 specifications for the default font. For example, if there is an
5369 `Emacs.default.attributeBackground: pink', the `background-color'
5370 attribute of the frame get's set, which let's the internal border
5371 of the tooltip frame appear in pink. Prevent this. */
5373 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
5374 Lisp_Object fg = Fframe_parameter (frame, Qforeground_color);
5375 Lisp_Object colors = Qnil;
5377 /* Set tip_frame here, so that */
5378 tip_frame = frame;
5379 call2 (Qface_set_after_frame_default, frame, Qnil);
5381 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
5382 colors = Fcons (Fcons (Qbackground_color, bg), colors);
5383 if (!EQ (fg, Fframe_parameter (frame, Qforeground_color)))
5384 colors = Fcons (Fcons (Qforeground_color, fg), colors);
5386 if (!NILP (colors))
5387 Fmodify_frame_parameters (frame, colors);
5390 f->no_split = 1;
5392 UNGCPRO;
5394 /* It is now ok to make the frame official even if we get an error
5395 below. And the frame needs to be on Vframe_list or making it
5396 visible won't work. */
5397 Vframe_list = Fcons (frame, Vframe_list);
5399 /* Now that the frame is official, it counts as a reference to
5400 its display. */
5401 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
5403 /* Setting attributes of faces of the tooltip frame from resources
5404 and similar will increment face_change_count, which leads to the
5405 clearing of all current matrices. Since this isn't necessary
5406 here, avoid it by resetting face_change_count to the value it
5407 had before we created the tip frame. */
5408 face_change_count = face_change_count_before;
5410 /* Discard the unwind_protect. */
5411 return unbind_to (count, frame);
5415 /* Compute where to display tip frame F. PARMS is the list of frame
5416 parameters for F. DX and DY are specified offsets from the current
5417 location of the mouse. WIDTH and HEIGHT are the width and height
5418 of the tooltip. Return coordinates relative to the root window of
5419 the display in *ROOT_X, and *ROOT_Y. */
5421 static void
5422 compute_tip_xy (struct frame *f,
5423 Lisp_Object parms, Lisp_Object dx, Lisp_Object dy,
5424 int width, int height, int *root_x, int *root_y)
5426 Lisp_Object left, top;
5427 int min_x, min_y, max_x, max_y;
5429 /* User-specified position? */
5430 left = Fcdr (Fassq (Qleft, parms));
5431 top = Fcdr (Fassq (Qtop, parms));
5433 /* Move the tooltip window where the mouse pointer is. Resize and
5434 show it. */
5435 if (!INTEGERP (left) || !INTEGERP (top))
5437 POINT pt;
5439 /* Default min and max values. */
5440 min_x = 0;
5441 min_y = 0;
5442 max_x = x_display_pixel_width (FRAME_W32_DISPLAY_INFO (f));
5443 max_y = x_display_pixel_height (FRAME_W32_DISPLAY_INFO (f));
5445 BLOCK_INPUT;
5446 GetCursorPos (&pt);
5447 *root_x = pt.x;
5448 *root_y = pt.y;
5449 UNBLOCK_INPUT;
5451 /* If multiple monitor support is available, constrain the tip onto
5452 the current monitor. This improves the above by allowing negative
5453 co-ordinates if monitor positions are such that they are valid, and
5454 snaps a tooltip onto a single monitor if we are close to the edge
5455 where it would otherwise flow onto the other monitor (or into
5456 nothingness if there is a gap in the overlap). */
5457 if (monitor_from_point_fn && get_monitor_info_fn)
5459 struct MONITOR_INFO info;
5460 HMONITOR monitor
5461 = monitor_from_point_fn (pt, MONITOR_DEFAULT_TO_NEAREST);
5462 info.cbSize = sizeof (info);
5464 if (get_monitor_info_fn (monitor, &info))
5466 min_x = info.rcWork.left;
5467 min_y = info.rcWork.top;
5468 max_x = info.rcWork.right;
5469 max_y = info.rcWork.bottom;
5474 if (INTEGERP (top))
5475 *root_y = XINT (top);
5476 else if (*root_y + XINT (dy) <= min_y)
5477 *root_y = min_y; /* Can happen for negative dy */
5478 else if (*root_y + XINT (dy) + height <= max_y)
5479 /* It fits below the pointer */
5480 *root_y += XINT (dy);
5481 else if (height + XINT (dy) + min_y <= *root_y)
5482 /* It fits above the pointer. */
5483 *root_y -= height + XINT (dy);
5484 else
5485 /* Put it on the top. */
5486 *root_y = min_y;
5488 if (INTEGERP (left))
5489 *root_x = XINT (left);
5490 else if (*root_x + XINT (dx) <= min_x)
5491 *root_x = 0; /* Can happen for negative dx */
5492 else if (*root_x + XINT (dx) + width <= max_x)
5493 /* It fits to the right of the pointer. */
5494 *root_x += XINT (dx);
5495 else if (width + XINT (dx) + min_x <= *root_x)
5496 /* It fits to the left of the pointer. */
5497 *root_x -= width + XINT (dx);
5498 else
5499 /* Put it left justified on the screen -- it ought to fit that way. */
5500 *root_x = min_x;
5504 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
5505 doc: /* Show STRING in a \"tooltip\" window on frame FRAME.
5506 A tooltip window is a small window displaying a string.
5508 This is an internal function; Lisp code should call `tooltip-show'.
5510 FRAME nil or omitted means use the selected frame.
5512 PARMS is an optional list of frame parameters which can be
5513 used to change the tooltip's appearance.
5515 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
5516 means use the default timeout of 5 seconds.
5518 If the list of frame parameters PARMS contains a `left' parameter,
5519 the tooltip is displayed at that x-position. Otherwise it is
5520 displayed at the mouse position, with offset DX added (default is 5 if
5521 DX isn't specified). Likewise for the y-position; if a `top' frame
5522 parameter is specified, it determines the y-position of the tooltip
5523 window, otherwise it is displayed at the mouse position, with offset
5524 DY added (default is -10).
5526 A tooltip's maximum size is specified by `x-max-tooltip-size'.
5527 Text larger than the specified size is clipped. */)
5528 (Lisp_Object string, Lisp_Object frame, Lisp_Object parms, Lisp_Object timeout, Lisp_Object dx, Lisp_Object dy)
5530 struct frame *f;
5531 struct window *w;
5532 int root_x, root_y;
5533 struct buffer *old_buffer;
5534 struct text_pos pos;
5535 int i, width, height, seen_reversed_p;
5536 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5537 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5538 int count = SPECPDL_INDEX ();
5540 specbind (Qinhibit_redisplay, Qt);
5542 GCPRO4 (string, parms, frame, timeout);
5544 CHECK_STRING (string);
5545 f = check_x_frame (frame);
5546 if (NILP (timeout))
5547 timeout = make_number (5);
5548 else
5549 CHECK_NATNUM (timeout);
5551 if (NILP (dx))
5552 dx = make_number (5);
5553 else
5554 CHECK_NUMBER (dx);
5556 if (NILP (dy))
5557 dy = make_number (-10);
5558 else
5559 CHECK_NUMBER (dy);
5561 if (NILP (last_show_tip_args))
5562 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
5564 if (!NILP (tip_frame))
5566 Lisp_Object last_string = AREF (last_show_tip_args, 0);
5567 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
5568 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
5570 if (EQ (frame, last_frame)
5571 && !NILP (Fequal (last_string, string))
5572 && !NILP (Fequal (last_parms, parms)))
5574 struct frame *f = XFRAME (tip_frame);
5576 /* Only DX and DY have changed. */
5577 if (!NILP (tip_timer))
5579 Lisp_Object timer = tip_timer;
5580 tip_timer = Qnil;
5581 call1 (Qcancel_timer, timer);
5584 BLOCK_INPUT;
5585 compute_tip_xy (f, parms, dx, dy, FRAME_PIXEL_WIDTH (f),
5586 FRAME_PIXEL_HEIGHT (f), &root_x, &root_y);
5588 /* Put tooltip in topmost group and in position. */
5589 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
5590 root_x, root_y, 0, 0,
5591 SWP_NOSIZE | SWP_NOACTIVATE);
5593 /* Ensure tooltip is on top of other topmost windows (eg menus). */
5594 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
5595 0, 0, 0, 0,
5596 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
5598 UNBLOCK_INPUT;
5599 goto start_timer;
5603 /* Hide a previous tip, if any. */
5604 Fx_hide_tip ();
5606 ASET (last_show_tip_args, 0, string);
5607 ASET (last_show_tip_args, 1, frame);
5608 ASET (last_show_tip_args, 2, parms);
5610 /* Add default values to frame parameters. */
5611 if (NILP (Fassq (Qname, parms)))
5612 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
5613 if (NILP (Fassq (Qinternal_border_width, parms)))
5614 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
5615 if (NILP (Fassq (Qborder_width, parms)))
5616 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
5617 if (NILP (Fassq (Qborder_color, parms)))
5618 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
5619 if (NILP (Fassq (Qbackground_color, parms)))
5620 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
5621 parms);
5623 /* Block input until the tip has been fully drawn, to avoid crashes
5624 when drawing tips in menus. */
5625 BLOCK_INPUT;
5627 /* Create a frame for the tooltip, and record it in the global
5628 variable tip_frame. */
5629 frame = x_create_tip_frame (FRAME_W32_DISPLAY_INFO (f), parms, string);
5630 f = XFRAME (frame);
5632 /* Set up the frame's root window. */
5633 w = XWINDOW (FRAME_ROOT_WINDOW (f));
5634 w->left_col = w->top_line = make_number (0);
5636 if (CONSP (Vx_max_tooltip_size)
5637 && INTEGERP (XCAR (Vx_max_tooltip_size))
5638 && XINT (XCAR (Vx_max_tooltip_size)) > 0
5639 && INTEGERP (XCDR (Vx_max_tooltip_size))
5640 && XINT (XCDR (Vx_max_tooltip_size)) > 0)
5642 w->total_cols = XCAR (Vx_max_tooltip_size);
5643 w->total_lines = XCDR (Vx_max_tooltip_size);
5645 else
5647 w->total_cols = make_number (80);
5648 w->total_lines = make_number (40);
5651 FRAME_TOTAL_COLS (f) = XINT (w->total_cols);
5652 adjust_glyphs (f);
5653 w->pseudo_window_p = 1;
5655 /* Display the tooltip text in a temporary buffer. */
5656 old_buffer = current_buffer;
5657 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->buffer));
5658 current_buffer->truncate_lines = Qnil;
5659 clear_glyph_matrix (w->desired_matrix);
5660 clear_glyph_matrix (w->current_matrix);
5661 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
5662 try_window (FRAME_ROOT_WINDOW (f), pos, TRY_WINDOW_IGNORE_FONTS_CHANGE);
5664 /* Compute width and height of the tooltip. */
5665 width = height = seen_reversed_p = 0;
5666 for (i = 0; i < w->desired_matrix->nrows; ++i)
5668 struct glyph_row *row = &w->desired_matrix->rows[i];
5669 struct glyph *last;
5670 int row_width;
5672 /* Stop at the first empty row at the end. */
5673 if (!row->enabled_p || !row->displays_text_p)
5674 break;
5676 /* Let the row go over the full width of the frame. */
5677 row->full_width_p = 1;
5679 row_width = row->pixel_width;
5680 if (row->used[TEXT_AREA])
5682 if (!row->reversed_p)
5684 /* There's a glyph at the end of rows that is used to
5685 place the cursor there. Don't include the width of
5686 this glyph. */
5687 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5688 if (INTEGERP (last->object))
5689 row_width -= last->pixel_width;
5691 else
5693 /* There could be a stretch glyph at the beginning of R2L
5694 rows that is produced by extend_face_to_end_of_line.
5695 Don't count that glyph. */
5696 struct glyph *g = row->glyphs[TEXT_AREA];
5698 if (g->type == STRETCH_GLYPH && INTEGERP (g->object))
5700 row_width -= g->pixel_width;
5701 seen_reversed_p = 1;
5706 height += row->height;
5707 width = max (width, row_width);
5710 /* If we've seen partial-length R2L rows, we need to re-adjust the
5711 tool-tip frame width and redisplay it again, to avoid over-wide
5712 tips due to the stretch glyph that extends R2L lines to full
5713 width of the frame. */
5714 if (seen_reversed_p)
5716 /* w->total_cols and FRAME_TOTAL_COLS want the width in columns,
5717 not in pixels. */
5718 width /= WINDOW_FRAME_COLUMN_WIDTH (w);
5719 w->total_cols = make_number (width);
5720 FRAME_TOTAL_COLS (f) = width;
5721 adjust_glyphs (f);
5722 w->pseudo_window_p = 1;
5723 clear_glyph_matrix (w->desired_matrix);
5724 clear_glyph_matrix (w->current_matrix);
5725 try_window (FRAME_ROOT_WINDOW (f), pos, TRY_WINDOW_IGNORE_FONTS_CHANGE);
5726 width = height = 0;
5727 /* Recompute width and height of the tooltip. */
5728 for (i = 0; i < w->desired_matrix->nrows; ++i)
5730 struct glyph_row *row = &w->desired_matrix->rows[i];
5731 struct glyph *last;
5732 int row_width;
5734 if (!row->enabled_p || !row->displays_text_p)
5735 break;
5736 row->full_width_p = 1;
5737 row_width = row->pixel_width;
5738 if (row->used[TEXT_AREA] && !row->reversed_p)
5740 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5741 if (INTEGERP (last->object))
5742 row_width -= last->pixel_width;
5745 height += row->height;
5746 width = max (width, row_width);
5750 /* Round up the height to an integral multiple of FRAME_LINE_HEIGHT. */
5751 if (height % FRAME_LINE_HEIGHT (f) != 0)
5752 height += FRAME_LINE_HEIGHT (f) - height % FRAME_LINE_HEIGHT (f);
5753 /* Add the frame's internal border to the width and height the w32
5754 window should have. */
5755 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5756 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5758 /* Move the tooltip window where the mouse pointer is. Resize and
5759 show it. */
5760 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5763 /* Adjust Window size to take border into account. */
5764 RECT rect;
5765 rect.left = rect.top = 0;
5766 rect.right = width;
5767 rect.bottom = height;
5768 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
5769 FRAME_EXTERNAL_MENU_BAR (f));
5771 /* Position and size tooltip, and put it in the topmost group.
5772 The add-on of FRAME_COLUMN_WIDTH to the 5th argument is a
5773 peculiarity of w32 display: without it, some fonts cause the
5774 last character of the tip to be truncated or wrapped around to
5775 the next line. */
5776 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
5777 root_x, root_y,
5778 rect.right - rect.left + FRAME_COLUMN_WIDTH (f),
5779 rect.bottom - rect.top, SWP_NOACTIVATE);
5781 /* Ensure tooltip is on top of other topmost windows (eg menus). */
5782 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
5783 0, 0, 0, 0,
5784 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
5786 /* Let redisplay know that we have made the frame visible already. */
5787 f->async_visible = 1;
5789 ShowWindow (FRAME_W32_WINDOW (f), SW_SHOWNOACTIVATE);
5792 /* Draw into the window. */
5793 w->must_be_updated_p = 1;
5794 update_single_window (w, 1);
5796 UNBLOCK_INPUT;
5798 /* Restore original current buffer. */
5799 set_buffer_internal_1 (old_buffer);
5800 windows_or_buffers_changed = old_windows_or_buffers_changed;
5802 start_timer:
5803 /* Let the tip disappear after timeout seconds. */
5804 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
5805 intern ("x-hide-tip"));
5807 UNGCPRO;
5808 return unbind_to (count, Qnil);
5812 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
5813 doc: /* Hide the current tooltip window, if there is any.
5814 Value is t if tooltip was open, nil otherwise. */)
5815 (void)
5817 int count;
5818 Lisp_Object deleted, frame, timer;
5819 struct gcpro gcpro1, gcpro2;
5821 /* Return quickly if nothing to do. */
5822 if (NILP (tip_timer) && NILP (tip_frame))
5823 return Qnil;
5825 frame = tip_frame;
5826 timer = tip_timer;
5827 GCPRO2 (frame, timer);
5828 tip_frame = tip_timer = deleted = Qnil;
5830 count = SPECPDL_INDEX ();
5831 specbind (Qinhibit_redisplay, Qt);
5832 specbind (Qinhibit_quit, Qt);
5834 if (!NILP (timer))
5835 call1 (Qcancel_timer, timer);
5837 if (FRAMEP (frame))
5839 delete_frame (frame, Qnil);
5840 deleted = Qt;
5843 UNGCPRO;
5844 return unbind_to (count, deleted);
5849 /***********************************************************************
5850 File selection dialog
5851 ***********************************************************************/
5852 extern Lisp_Object Qfile_name_history;
5854 /* Callback for altering the behavior of the Open File dialog.
5855 Makes the Filename text field contain "Current Directory" and be
5856 read-only when "Directories" is selected in the filter. This
5857 allows us to work around the fact that the standard Open File
5858 dialog does not support directories. */
5859 static UINT CALLBACK
5860 file_dialog_callback (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
5862 if (msg == WM_NOTIFY)
5864 OFNOTIFY * notify = (OFNOTIFY *)lParam;
5865 /* Detect when the Filter dropdown is changed. */
5866 if (notify->hdr.code == CDN_TYPECHANGE
5867 || notify->hdr.code == CDN_INITDONE)
5869 HWND dialog = GetParent (hwnd);
5870 HWND edit_control = GetDlgItem (dialog, FILE_NAME_TEXT_FIELD);
5872 /* Directories is in index 2. */
5873 if (notify->lpOFN->nFilterIndex == 2)
5875 CommDlg_OpenSave_SetControlText (dialog, FILE_NAME_TEXT_FIELD,
5876 "Current Directory");
5877 EnableWindow (edit_control, FALSE);
5879 else
5881 /* Don't override default filename on init done. */
5882 if (notify->hdr.code == CDN_TYPECHANGE)
5883 CommDlg_OpenSave_SetControlText (dialog,
5884 FILE_NAME_TEXT_FIELD, "");
5885 EnableWindow (edit_control, TRUE);
5889 return 0;
5892 /* Since we compile with _WIN32_WINNT set to 0x0400 (for NT4 compatibility)
5893 we end up with the old file dialogs. Define a big enough struct for the
5894 new dialog to trick GetOpenFileName into giving us the new dialogs on
5895 Windows 2000 and XP. */
5896 typedef struct
5898 OPENFILENAME real_details;
5899 void * pReserved;
5900 DWORD dwReserved;
5901 DWORD FlagsEx;
5902 } NEWOPENFILENAME;
5905 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5906 doc: /* Read file name, prompting with PROMPT in directory DIR.
5907 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5908 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5909 or directory must exist.
5911 This function is only defined on MS Windows, and X Windows with the
5912 Motif or Gtk toolkits. With the Motif toolkit, ONLY-DIR-P is ignored.
5913 Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories. */)
5914 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename, Lisp_Object mustmatch, Lisp_Object only_dir_p)
5916 struct frame *f = SELECTED_FRAME ();
5917 Lisp_Object file = Qnil;
5918 int count = SPECPDL_INDEX ();
5919 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5920 char filename[MAX_PATH + 1];
5921 char init_dir[MAX_PATH + 1];
5922 int default_filter_index = 1; /* 1: All Files, 2: Directories only */
5924 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5925 CHECK_STRING (prompt);
5926 CHECK_STRING (dir);
5928 /* Create the dialog with PROMPT as title, using DIR as initial
5929 directory and using "*" as pattern. */
5930 dir = Fexpand_file_name (dir, Qnil);
5931 strncpy (init_dir, SDATA (ENCODE_FILE (dir)), MAX_PATH);
5932 init_dir[MAX_PATH] = '\0';
5933 unixtodos_filename (init_dir);
5935 if (STRINGP (default_filename))
5937 char *file_name_only;
5938 char *full_path_name = SDATA (ENCODE_FILE (default_filename));
5940 unixtodos_filename (full_path_name);
5942 file_name_only = strrchr (full_path_name, '\\');
5943 if (!file_name_only)
5944 file_name_only = full_path_name;
5945 else
5946 file_name_only++;
5948 strncpy (filename, file_name_only, MAX_PATH);
5949 filename[MAX_PATH] = '\0';
5951 else
5952 filename[0] = '\0';
5955 NEWOPENFILENAME new_file_details;
5956 BOOL file_opened = FALSE;
5957 OPENFILENAME * file_details = &new_file_details.real_details;
5959 /* Prevent redisplay. */
5960 specbind (Qinhibit_redisplay, Qt);
5961 BLOCK_INPUT;
5963 memset (&new_file_details, 0, sizeof (new_file_details));
5964 /* Apparently NT4 crashes if you give it an unexpected size.
5965 I'm not sure about Windows 9x, so play it safe. */
5966 if (w32_major_version > 4 && w32_major_version < 95)
5967 file_details->lStructSize = sizeof (NEWOPENFILENAME);
5968 else
5969 file_details->lStructSize = sizeof (OPENFILENAME);
5971 file_details->hwndOwner = FRAME_W32_WINDOW (f);
5972 /* Undocumented Bug in Common File Dialog:
5973 If a filter is not specified, shell links are not resolved. */
5974 file_details->lpstrFilter = "All Files (*.*)\0*.*\0Directories\0*|*\0\0";
5975 file_details->lpstrFile = filename;
5976 file_details->nMaxFile = sizeof (filename);
5977 file_details->lpstrInitialDir = init_dir;
5978 file_details->lpstrTitle = SDATA (prompt);
5980 if (! NILP (only_dir_p))
5981 default_filter_index = 2;
5983 file_details->nFilterIndex = default_filter_index;
5985 file_details->Flags = (OFN_HIDEREADONLY | OFN_NOCHANGEDIR
5986 | OFN_EXPLORER | OFN_ENABLEHOOK);
5987 if (!NILP (mustmatch))
5989 /* Require that the path to the parent directory exists. */
5990 file_details->Flags |= OFN_PATHMUSTEXIST;
5991 /* If we are looking for a file, require that it exists. */
5992 if (NILP (only_dir_p))
5993 file_details->Flags |= OFN_FILEMUSTEXIST;
5996 file_details->lpfnHook = (LPOFNHOOKPROC) file_dialog_callback;
5998 file_opened = GetOpenFileName (file_details);
6000 UNBLOCK_INPUT;
6002 if (file_opened)
6004 dostounix_filename (filename);
6006 if (file_details->nFilterIndex == 2)
6008 /* "Directories" selected - strip dummy file name. */
6009 char * last = strrchr (filename, '/');
6010 *last = '\0';
6013 file = DECODE_FILE (build_string (filename));
6015 /* User cancelled the dialog without making a selection. */
6016 else if (!CommDlgExtendedError ())
6017 file = Qnil;
6018 /* An error occurred, fallback on reading from the mini-buffer. */
6019 else
6020 file = Fcompleting_read (prompt, intern ("read-file-name-internal"),
6021 dir, mustmatch, dir, Qfile_name_history,
6022 default_filename, Qnil);
6024 file = unbind_to (count, file);
6027 UNGCPRO;
6029 /* Make "Cancel" equivalent to C-g. */
6030 if (NILP (file))
6031 Fsignal (Qquit, Qnil);
6033 return unbind_to (count, file);
6037 /* Moving files to the system recycle bin.
6038 Used by `move-file-to-trash' instead of the default moving to ~/.Trash */
6039 DEFUN ("system-move-file-to-trash", Fsystem_move_file_to_trash,
6040 Ssystem_move_file_to_trash, 1, 1, 0,
6041 doc: /* Move file or directory named FILENAME to the recycle bin. */)
6042 (Lisp_Object filename)
6044 Lisp_Object handler;
6045 Lisp_Object encoded_file;
6046 Lisp_Object operation;
6048 operation = Qdelete_file;
6049 if (!NILP (Ffile_directory_p (filename))
6050 && NILP (Ffile_symlink_p (filename)))
6052 operation = intern ("delete-directory");
6053 filename = Fdirectory_file_name (filename);
6055 filename = Fexpand_file_name (filename, Qnil);
6057 handler = Ffind_file_name_handler (filename, operation);
6058 if (!NILP (handler))
6059 return call2 (handler, operation, filename);
6061 encoded_file = ENCODE_FILE (filename);
6064 const char * path;
6065 SHFILEOPSTRUCT file_op;
6066 char tmp_path[MAX_PATH + 1];
6068 path = map_w32_filename (SDATA (encoded_file), NULL);
6070 /* On Windows, write permission is required to delete/move files. */
6071 _chmod (path, 0666);
6073 memset (tmp_path, 0, sizeof (tmp_path));
6074 strcpy (tmp_path, path);
6076 memset (&file_op, 0, sizeof (file_op));
6077 file_op.hwnd = HWND_DESKTOP;
6078 file_op.wFunc = FO_DELETE;
6079 file_op.pFrom = tmp_path;
6080 file_op.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_ALLOWUNDO
6081 | FOF_NOERRORUI | FOF_NO_CONNECTED_ELEMENTS;
6082 file_op.fAnyOperationsAborted = FALSE;
6084 if (SHFileOperation (&file_op) != 0)
6085 report_file_error ("Removing old name", list1 (filename));
6087 return Qnil;
6091 /***********************************************************************
6092 w32 specialized functions
6093 ***********************************************************************/
6095 DEFUN ("w32-send-sys-command", Fw32_send_sys_command,
6096 Sw32_send_sys_command, 1, 2, 0,
6097 doc: /* Send frame a Windows WM_SYSCOMMAND message of type COMMAND.
6098 Some useful values for COMMAND are #xf030 to maximize frame (#xf020
6099 to minimize), #xf120 to restore frame to original size, and #xf100
6100 to activate the menubar for keyboard access. #xf140 activates the
6101 screen saver if defined.
6103 If optional parameter FRAME is not specified, use selected frame. */)
6104 (Lisp_Object command, Lisp_Object frame)
6106 FRAME_PTR f = check_x_frame (frame);
6108 CHECK_NUMBER (command);
6110 PostMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, XINT (command), 0);
6112 return Qnil;
6115 DEFUN ("w32-shell-execute", Fw32_shell_execute, Sw32_shell_execute, 2, 4, 0,
6116 doc: /* Get Windows to perform OPERATION on DOCUMENT.
6117 This is a wrapper around the ShellExecute system function, which
6118 invokes the application registered to handle OPERATION for DOCUMENT.
6120 OPERATION is either nil or a string that names a supported operation.
6121 What operations can be used depends on the particular DOCUMENT and its
6122 handler application, but typically it is one of the following common
6123 operations:
6125 \"open\" - open DOCUMENT, which could be a file, a directory, or an
6126 executable program. If it is an application, that
6127 application is launched in the current buffer's default
6128 directory. Otherwise, the application associated with
6129 DOCUMENT is launched in the buffer's default directory.
6130 \"print\" - print DOCUMENT, which must be a file
6131 \"explore\" - start the Windows Explorer on DOCUMENT
6132 \"edit\" - launch an editor and open DOCUMENT for editing; which
6133 editor is launched depends on the association for the
6134 specified DOCUMENT
6135 \"find\" - initiate search starting from DOCUMENT which must specify
6136 a directory
6137 nil - invoke the default OPERATION, or \"open\" if default is
6138 not defined or unavailable
6140 DOCUMENT is typically the name of a document file or a URL, but can
6141 also be a program executable to run, or a directory to open in the
6142 Windows Explorer.
6144 If DOCUMENT is a program executable, the optional third arg PARAMETERS
6145 can be a string containing command line parameters that will be passed
6146 to the program; otherwise, PARAMETERS should be nil or unspecified.
6148 Optional fourth argument SHOW-FLAG can be used to control how the
6149 application will be displayed when it is invoked. If SHOW-FLAG is nil
6150 or unspecified, the application is displayed normally, otherwise it is
6151 an integer representing a ShowWindow flag:
6153 0 - start hidden
6154 1 - start normally
6155 3 - start maximized
6156 6 - start minimized */)
6157 (Lisp_Object operation, Lisp_Object document, Lisp_Object parameters, Lisp_Object show_flag)
6159 Lisp_Object current_dir;
6160 char *errstr;
6162 CHECK_STRING (document);
6164 /* Encode filename, current directory and parameters. */
6165 current_dir = ENCODE_FILE (current_buffer->directory);
6166 document = ENCODE_FILE (document);
6167 if (STRINGP (parameters))
6168 parameters = ENCODE_SYSTEM (parameters);
6170 if ((int) ShellExecute (NULL,
6171 (STRINGP (operation) ?
6172 SDATA (operation) : NULL),
6173 SDATA (document),
6174 (STRINGP (parameters) ?
6175 SDATA (parameters) : NULL),
6176 SDATA (current_dir),
6177 (INTEGERP (show_flag) ?
6178 XINT (show_flag) : SW_SHOWDEFAULT))
6179 > 32)
6180 return Qt;
6181 errstr = w32_strerror (0);
6182 /* The error string might be encoded in the locale's encoding. */
6183 if (!NILP (Vlocale_coding_system))
6185 Lisp_Object decoded =
6186 code_convert_string_norecord (make_unibyte_string (errstr,
6187 strlen (errstr)),
6188 Vlocale_coding_system, 0);
6189 errstr = SSDATA (decoded);
6191 error ("ShellExecute failed: %s", errstr);
6194 /* Lookup virtual keycode from string representing the name of a
6195 non-ascii keystroke into the corresponding virtual key, using
6196 lispy_function_keys. */
6197 static int
6198 lookup_vk_code (char *key)
6200 int i;
6202 for (i = 0; i < 256; i++)
6203 if (lispy_function_keys[i]
6204 && strcmp (lispy_function_keys[i], key) == 0)
6205 return i;
6207 return -1;
6210 /* Convert a one-element vector style key sequence to a hot key
6211 definition. */
6212 static Lisp_Object
6213 w32_parse_hot_key (Lisp_Object key)
6215 /* Copied from Fdefine_key and store_in_keymap. */
6216 register Lisp_Object c;
6217 int vk_code;
6218 int lisp_modifiers;
6219 int w32_modifiers;
6220 struct gcpro gcpro1;
6222 CHECK_VECTOR (key);
6224 if (XFASTINT (Flength (key)) != 1)
6225 return Qnil;
6227 GCPRO1 (key);
6229 c = Faref (key, make_number (0));
6231 if (CONSP (c) && lucid_event_type_list_p (c))
6232 c = Fevent_convert_list (c);
6234 UNGCPRO;
6236 if (! INTEGERP (c) && ! SYMBOLP (c))
6237 error ("Key definition is invalid");
6239 /* Work out the base key and the modifiers. */
6240 if (SYMBOLP (c))
6242 c = parse_modifiers (c);
6243 lisp_modifiers = XINT (Fcar (Fcdr (c)));
6244 c = Fcar (c);
6245 if (!SYMBOLP (c))
6246 abort ();
6247 vk_code = lookup_vk_code (SDATA (SYMBOL_NAME (c)));
6249 else if (INTEGERP (c))
6251 lisp_modifiers = XINT (c) & ~CHARACTERBITS;
6252 /* Many ascii characters are their own virtual key code. */
6253 vk_code = XINT (c) & CHARACTERBITS;
6256 if (vk_code < 0 || vk_code > 255)
6257 return Qnil;
6259 if ((lisp_modifiers & meta_modifier) != 0
6260 && !NILP (Vw32_alt_is_meta))
6261 lisp_modifiers |= alt_modifier;
6263 /* Supply defs missing from mingw32. */
6264 #ifndef MOD_ALT
6265 #define MOD_ALT 0x0001
6266 #define MOD_CONTROL 0x0002
6267 #define MOD_SHIFT 0x0004
6268 #define MOD_WIN 0x0008
6269 #endif
6271 /* Convert lisp modifiers to Windows hot-key form. */
6272 w32_modifiers = (lisp_modifiers & hyper_modifier) ? MOD_WIN : 0;
6273 w32_modifiers |= (lisp_modifiers & alt_modifier) ? MOD_ALT : 0;
6274 w32_modifiers |= (lisp_modifiers & ctrl_modifier) ? MOD_CONTROL : 0;
6275 w32_modifiers |= (lisp_modifiers & shift_modifier) ? MOD_SHIFT : 0;
6277 return HOTKEY (vk_code, w32_modifiers);
6280 DEFUN ("w32-register-hot-key", Fw32_register_hot_key,
6281 Sw32_register_hot_key, 1, 1, 0,
6282 doc: /* Register KEY as a hot-key combination.
6283 Certain key combinations like Alt-Tab are reserved for system use on
6284 Windows, and therefore are normally intercepted by the system. However,
6285 most of these key combinations can be received by registering them as
6286 hot-keys, overriding their special meaning.
6288 KEY must be a one element key definition in vector form that would be
6289 acceptable to `define-key' (e.g. [A-tab] for Alt-Tab). The meta
6290 modifier is interpreted as Alt if `w32-alt-is-meta' is t, and hyper
6291 is always interpreted as the Windows modifier keys.
6293 The return value is the hotkey-id if registered, otherwise nil. */)
6294 (Lisp_Object key)
6296 key = w32_parse_hot_key (key);
6298 if (!NILP (key) && NILP (Fmemq (key, w32_grabbed_keys)))
6300 /* Reuse an empty slot if possible. */
6301 Lisp_Object item = Fmemq (Qnil, w32_grabbed_keys);
6303 /* Safe to add new key to list, even if we have focus. */
6304 if (NILP (item))
6305 w32_grabbed_keys = Fcons (key, w32_grabbed_keys);
6306 else
6307 XSETCAR (item, key);
6309 /* Notify input thread about new hot-key definition, so that it
6310 takes effect without needing to switch focus. */
6311 #ifdef USE_LISP_UNION_TYPE
6312 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
6313 (WPARAM) key.i, 0);
6314 #else
6315 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
6316 (WPARAM) key, 0);
6317 #endif
6320 return key;
6323 DEFUN ("w32-unregister-hot-key", Fw32_unregister_hot_key,
6324 Sw32_unregister_hot_key, 1, 1, 0,
6325 doc: /* Unregister KEY as a hot-key combination. */)
6326 (Lisp_Object key)
6328 Lisp_Object item;
6330 if (!INTEGERP (key))
6331 key = w32_parse_hot_key (key);
6333 item = Fmemq (key, w32_grabbed_keys);
6335 if (!NILP (item))
6337 /* Notify input thread about hot-key definition being removed, so
6338 that it takes effect without needing focus switch. */
6339 #ifdef USE_LISP_UNION_TYPE
6340 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
6341 (WPARAM) XINT (XCAR (item)), (LPARAM) item.i))
6342 #else
6343 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
6344 (WPARAM) XINT (XCAR (item)), (LPARAM) item))
6345 #endif
6347 MSG msg;
6348 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
6350 return Qt;
6352 return Qnil;
6355 DEFUN ("w32-registered-hot-keys", Fw32_registered_hot_keys,
6356 Sw32_registered_hot_keys, 0, 0, 0,
6357 doc: /* Return list of registered hot-key IDs. */)
6358 (void)
6360 return Fdelq (Qnil, Fcopy_sequence (w32_grabbed_keys));
6363 DEFUN ("w32-reconstruct-hot-key", Fw32_reconstruct_hot_key,
6364 Sw32_reconstruct_hot_key, 1, 1, 0,
6365 doc: /* Convert hot-key ID to a lisp key combination.
6366 usage: (w32-reconstruct-hot-key ID) */)
6367 (Lisp_Object hotkeyid)
6369 int vk_code, w32_modifiers;
6370 Lisp_Object key;
6372 CHECK_NUMBER (hotkeyid);
6374 vk_code = HOTKEY_VK_CODE (hotkeyid);
6375 w32_modifiers = HOTKEY_MODIFIERS (hotkeyid);
6377 if (vk_code < 256 && lispy_function_keys[vk_code])
6378 key = intern (lispy_function_keys[vk_code]);
6379 else
6380 key = make_number (vk_code);
6382 key = Fcons (key, Qnil);
6383 if (w32_modifiers & MOD_SHIFT)
6384 key = Fcons (Qshift, key);
6385 if (w32_modifiers & MOD_CONTROL)
6386 key = Fcons (Qctrl, key);
6387 if (w32_modifiers & MOD_ALT)
6388 key = Fcons (NILP (Vw32_alt_is_meta) ? Qalt : Qmeta, key);
6389 if (w32_modifiers & MOD_WIN)
6390 key = Fcons (Qhyper, key);
6392 return key;
6395 DEFUN ("w32-toggle-lock-key", Fw32_toggle_lock_key,
6396 Sw32_toggle_lock_key, 1, 2, 0,
6397 doc: /* Toggle the state of the lock key KEY.
6398 KEY can be `capslock', `kp-numlock', or `scroll'.
6399 If the optional parameter NEW-STATE is a number, then the state of KEY
6400 is set to off if the low bit of NEW-STATE is zero, otherwise on. */)
6401 (Lisp_Object key, Lisp_Object new_state)
6403 int vk_code;
6405 if (EQ (key, intern ("capslock")))
6406 vk_code = VK_CAPITAL;
6407 else if (EQ (key, intern ("kp-numlock")))
6408 vk_code = VK_NUMLOCK;
6409 else if (EQ (key, intern ("scroll")))
6410 vk_code = VK_SCROLL;
6411 else
6412 return Qnil;
6414 if (!dwWindowsThreadId)
6415 return make_number (w32_console_toggle_lock_key (vk_code, new_state));
6417 #ifdef USE_LISP_UNION_TYPE
6418 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
6419 (WPARAM) vk_code, (LPARAM) new_state.i))
6420 #else
6421 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
6422 (WPARAM) vk_code, (LPARAM) new_state))
6423 #endif
6425 MSG msg;
6426 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
6427 return make_number (msg.wParam);
6429 return Qnil;
6432 DEFUN ("w32-window-exists-p", Fw32_window_exists_p, Sw32_window_exists_p,
6433 2, 2, 0,
6434 doc: /* Return non-nil if a window exists with the specified CLASS and NAME.
6436 This is a direct interface to the Windows API FindWindow function. */)
6437 (Lisp_Object class, Lisp_Object name)
6439 HWND hnd;
6441 if (!NILP (class))
6442 CHECK_STRING (class);
6443 if (!NILP (name))
6444 CHECK_STRING (name);
6446 hnd = FindWindow (STRINGP (class) ? ((LPCTSTR) SDATA (class)) : NULL,
6447 STRINGP (name) ? ((LPCTSTR) SDATA (name)) : NULL);
6448 if (!hnd)
6449 return Qnil;
6450 return Qt;
6453 DEFUN ("w32-battery-status", Fw32_battery_status, Sw32_battery_status, 0, 0, 0,
6454 doc: /* Get power status information from Windows system.
6456 The following %-sequences are provided:
6457 %L AC line status (verbose)
6458 %B Battery status (verbose)
6459 %b Battery status, empty means high, `-' means low,
6460 `!' means critical, and `+' means charging
6461 %p Battery load percentage
6462 %s Remaining time (to charge or discharge) in seconds
6463 %m Remaining time (to charge or discharge) in minutes
6464 %h Remaining time (to charge or discharge) in hours
6465 %t Remaining time (to charge or discharge) in the form `h:min' */)
6466 (void)
6468 Lisp_Object status = Qnil;
6470 SYSTEM_POWER_STATUS system_status;
6471 if (GetSystemPowerStatus (&system_status))
6473 Lisp_Object line_status, battery_status, battery_status_symbol;
6474 Lisp_Object load_percentage, seconds, minutes, hours, remain;
6475 Lisp_Object sequences[8];
6477 long seconds_left = (long) system_status.BatteryLifeTime;
6479 if (system_status.ACLineStatus == 0)
6480 line_status = build_string ("off-line");
6481 else if (system_status.ACLineStatus == 1)
6482 line_status = build_string ("on-line");
6483 else
6484 line_status = build_string ("N/A");
6486 if (system_status.BatteryFlag & 128)
6488 battery_status = build_string ("N/A");
6489 battery_status_symbol = empty_unibyte_string;
6491 else if (system_status.BatteryFlag & 8)
6493 battery_status = build_string ("charging");
6494 battery_status_symbol = build_string ("+");
6495 if (system_status.BatteryFullLifeTime != -1L)
6496 seconds_left = system_status.BatteryFullLifeTime - seconds_left;
6498 else if (system_status.BatteryFlag & 4)
6500 battery_status = build_string ("critical");
6501 battery_status_symbol = build_string ("!");
6503 else if (system_status.BatteryFlag & 2)
6505 battery_status = build_string ("low");
6506 battery_status_symbol = build_string ("-");
6508 else if (system_status.BatteryFlag & 1)
6510 battery_status = build_string ("high");
6511 battery_status_symbol = empty_unibyte_string;
6513 else
6515 battery_status = build_string ("medium");
6516 battery_status_symbol = empty_unibyte_string;
6519 if (system_status.BatteryLifePercent > 100)
6520 load_percentage = build_string ("N/A");
6521 else
6523 char buffer[16];
6524 _snprintf (buffer, 16, "%d", system_status.BatteryLifePercent);
6525 load_percentage = build_string (buffer);
6528 if (seconds_left < 0)
6529 seconds = minutes = hours = remain = build_string ("N/A");
6530 else
6532 long m;
6533 float h;
6534 char buffer[16];
6535 _snprintf (buffer, 16, "%ld", seconds_left);
6536 seconds = build_string (buffer);
6538 m = seconds_left / 60;
6539 _snprintf (buffer, 16, "%ld", m);
6540 minutes = build_string (buffer);
6542 h = seconds_left / 3600.0;
6543 _snprintf (buffer, 16, "%3.1f", h);
6544 hours = build_string (buffer);
6546 _snprintf (buffer, 16, "%ld:%02ld", m / 60, m % 60);
6547 remain = build_string (buffer);
6549 sequences[0] = Fcons (make_number ('L'), line_status);
6550 sequences[1] = Fcons (make_number ('B'), battery_status);
6551 sequences[2] = Fcons (make_number ('b'), battery_status_symbol);
6552 sequences[3] = Fcons (make_number ('p'), load_percentage);
6553 sequences[4] = Fcons (make_number ('s'), seconds);
6554 sequences[5] = Fcons (make_number ('m'), minutes);
6555 sequences[6] = Fcons (make_number ('h'), hours);
6556 sequences[7] = Fcons (make_number ('t'), remain);
6558 status = Flist (8, sequences);
6560 return status;
6564 DEFUN ("file-system-info", Ffile_system_info, Sfile_system_info, 1, 1, 0,
6565 doc: /* Return storage information about the file system FILENAME is on.
6566 Value is a list of floats (TOTAL FREE AVAIL), where TOTAL is the total
6567 storage of the file system, FREE is the free storage, and AVAIL is the
6568 storage available to a non-superuser. All 3 numbers are in bytes.
6569 If the underlying system call fails, value is nil. */)
6570 (Lisp_Object filename)
6572 Lisp_Object encoded, value;
6574 CHECK_STRING (filename);
6575 filename = Fexpand_file_name (filename, Qnil);
6576 encoded = ENCODE_FILE (filename);
6578 value = Qnil;
6580 /* Determining the required information on Windows turns out, sadly,
6581 to be more involved than one would hope. The original Win32 api
6582 call for this will return bogus information on some systems, but we
6583 must dynamically probe for the replacement api, since that was
6584 added rather late on. */
6586 HMODULE hKernel = GetModuleHandle ("kernel32");
6587 BOOL (*pfn_GetDiskFreeSpaceEx)
6588 (char *, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER)
6589 = (void *) GetProcAddress (hKernel, "GetDiskFreeSpaceEx");
6591 /* On Windows, we may need to specify the root directory of the
6592 volume holding FILENAME. */
6593 char rootname[MAX_PATH];
6594 char *name = SDATA (encoded);
6596 /* find the root name of the volume if given */
6597 if (isalpha (name[0]) && name[1] == ':')
6599 rootname[0] = name[0];
6600 rootname[1] = name[1];
6601 rootname[2] = '\\';
6602 rootname[3] = 0;
6604 else if (IS_DIRECTORY_SEP (name[0]) && IS_DIRECTORY_SEP (name[1]))
6606 char *str = rootname;
6607 int slashes = 4;
6610 if (IS_DIRECTORY_SEP (*name) && --slashes == 0)
6611 break;
6612 *str++ = *name++;
6614 while ( *name );
6616 *str++ = '\\';
6617 *str = 0;
6620 if (pfn_GetDiskFreeSpaceEx)
6622 /* Unsigned large integers cannot be cast to double, so
6623 use signed ones instead. */
6624 LARGE_INTEGER availbytes;
6625 LARGE_INTEGER freebytes;
6626 LARGE_INTEGER totalbytes;
6628 if (pfn_GetDiskFreeSpaceEx (rootname,
6629 (ULARGE_INTEGER *)&availbytes,
6630 (ULARGE_INTEGER *)&totalbytes,
6631 (ULARGE_INTEGER *)&freebytes))
6632 value = list3 (make_float ((double) totalbytes.QuadPart),
6633 make_float ((double) freebytes.QuadPart),
6634 make_float ((double) availbytes.QuadPart));
6636 else
6638 DWORD sectors_per_cluster;
6639 DWORD bytes_per_sector;
6640 DWORD free_clusters;
6641 DWORD total_clusters;
6643 if (GetDiskFreeSpace (rootname,
6644 &sectors_per_cluster,
6645 &bytes_per_sector,
6646 &free_clusters,
6647 &total_clusters))
6648 value = list3 (make_float ((double) total_clusters
6649 * sectors_per_cluster * bytes_per_sector),
6650 make_float ((double) free_clusters
6651 * sectors_per_cluster * bytes_per_sector),
6652 make_float ((double) free_clusters
6653 * sectors_per_cluster * bytes_per_sector));
6657 return value;
6660 DEFUN ("default-printer-name", Fdefault_printer_name, Sdefault_printer_name,
6661 0, 0, 0, doc: /* Return the name of Windows default printer device. */)
6662 (void)
6664 static char pname_buf[256];
6665 int err;
6666 HANDLE hPrn;
6667 PRINTER_INFO_2 *ppi2 = NULL;
6668 DWORD dwNeeded = 0, dwReturned = 0;
6670 /* Retrieve the default string from Win.ini (the registry).
6671 * String will be in form "printername,drivername,portname".
6672 * This is the most portable way to get the default printer. */
6673 if (GetProfileString ("windows", "device", ",,", pname_buf, sizeof (pname_buf)) <= 0)
6674 return Qnil;
6675 /* printername precedes first "," character */
6676 strtok (pname_buf, ",");
6677 /* We want to know more than the printer name */
6678 if (!OpenPrinter (pname_buf, &hPrn, NULL))
6679 return Qnil;
6680 GetPrinter (hPrn, 2, NULL, 0, &dwNeeded);
6681 if (dwNeeded == 0)
6683 ClosePrinter (hPrn);
6684 return Qnil;
6686 /* Allocate memory for the PRINTER_INFO_2 struct */
6687 ppi2 = (PRINTER_INFO_2 *) xmalloc (dwNeeded);
6688 if (!ppi2)
6690 ClosePrinter (hPrn);
6691 return Qnil;
6693 /* Call GetPrinter again with big enouth memory block */
6694 err = GetPrinter (hPrn, 2, (LPBYTE)ppi2, dwNeeded, &dwReturned);
6695 ClosePrinter (hPrn);
6696 if (!err)
6698 xfree (ppi2);
6699 return Qnil;
6702 if (ppi2)
6704 if (ppi2->Attributes & PRINTER_ATTRIBUTE_SHARED && ppi2->pServerName)
6706 /* a remote printer */
6707 if (*ppi2->pServerName == '\\')
6708 _snprintf (pname_buf, sizeof (pname_buf), "%s\\%s", ppi2->pServerName,
6709 ppi2->pShareName);
6710 else
6711 _snprintf (pname_buf, sizeof (pname_buf), "\\\\%s\\%s", ppi2->pServerName,
6712 ppi2->pShareName);
6713 pname_buf[sizeof (pname_buf) - 1] = '\0';
6715 else
6717 /* a local printer */
6718 strncpy (pname_buf, ppi2->pPortName, sizeof (pname_buf));
6719 pname_buf[sizeof (pname_buf) - 1] = '\0';
6720 /* `pPortName' can include several ports, delimited by ','.
6721 * we only use the first one. */
6722 strtok (pname_buf, ",");
6724 xfree (ppi2);
6727 return build_string (pname_buf);
6730 /***********************************************************************
6731 Initialization
6732 ***********************************************************************/
6734 /* Keep this list in the same order as frame_parms in frame.c.
6735 Use 0 for unsupported frame parameters. */
6737 frame_parm_handler w32_frame_parm_handlers[] =
6739 x_set_autoraise,
6740 x_set_autolower,
6741 x_set_background_color,
6742 x_set_border_color,
6743 x_set_border_width,
6744 x_set_cursor_color,
6745 x_set_cursor_type,
6746 x_set_font,
6747 x_set_foreground_color,
6748 x_set_icon_name,
6749 x_set_icon_type,
6750 x_set_internal_border_width,
6751 x_set_menu_bar_lines,
6752 x_set_mouse_color,
6753 x_explicitly_set_name,
6754 x_set_scroll_bar_width,
6755 x_set_title,
6756 x_set_unsplittable,
6757 x_set_vertical_scroll_bars,
6758 x_set_visibility,
6759 x_set_tool_bar_lines,
6760 0, /* x_set_scroll_bar_foreground, */
6761 0, /* x_set_scroll_bar_background, */
6762 x_set_screen_gamma,
6763 x_set_line_spacing,
6764 x_set_fringe_width,
6765 x_set_fringe_width,
6766 0, /* x_set_wait_for_wm, */
6767 x_set_fullscreen,
6768 x_set_font_backend,
6769 x_set_alpha,
6770 0, /* x_set_sticky */
6771 0, /* x_set_tool_bar_position */
6774 void
6775 syms_of_w32fns (void)
6777 globals_of_w32fns ();
6778 /* This is zero if not using MS-Windows. */
6779 w32_in_use = 0;
6780 track_mouse_window = NULL;
6782 w32_visible_system_caret_hwnd = NULL;
6784 DEFSYM (Qnone, "none");
6785 DEFSYM (Qsuppress_icon, "suppress-icon");
6786 DEFSYM (Qundefined_color, "undefined-color");
6787 DEFSYM (Qcancel_timer, "cancel-timer");
6788 DEFSYM (Qhyper, "hyper");
6789 DEFSYM (Qsuper, "super");
6790 DEFSYM (Qmeta, "meta");
6791 DEFSYM (Qalt, "alt");
6792 DEFSYM (Qctrl, "ctrl");
6793 DEFSYM (Qcontrol, "control");
6794 DEFSYM (Qshift, "shift");
6795 DEFSYM (Qfont_param, "font-parameter");
6796 /* This is the end of symbol initialization. */
6798 /* Text property `display' should be nonsticky by default. */
6799 Vtext_property_default_nonsticky
6800 = Fcons (Fcons (Qdisplay, Qt), Vtext_property_default_nonsticky);
6803 Fput (Qundefined_color, Qerror_conditions,
6804 pure_cons (Qundefined_color, pure_cons (Qerror, Qnil)));
6805 Fput (Qundefined_color, Qerror_message,
6806 make_pure_c_string ("Undefined color"));
6808 staticpro (&w32_grabbed_keys);
6809 w32_grabbed_keys = Qnil;
6811 DEFVAR_LISP ("w32-color-map", Vw32_color_map,
6812 doc: /* An array of color name mappings for Windows. */);
6813 Vw32_color_map = Qnil;
6815 DEFVAR_LISP ("w32-pass-alt-to-system", Vw32_pass_alt_to_system,
6816 doc: /* Non-nil if Alt key presses are passed on to Windows.
6817 When non-nil, for example, Alt pressed and released and then space will
6818 open the System menu. When nil, Emacs processes the Alt key events, and
6819 then silently swallows them. */);
6820 Vw32_pass_alt_to_system = Qnil;
6822 DEFVAR_LISP ("w32-alt-is-meta", Vw32_alt_is_meta,
6823 doc: /* Non-nil if the Alt key is to be considered the same as the META key.
6824 When nil, Emacs will translate the Alt key to the ALT modifier, not to META. */);
6825 Vw32_alt_is_meta = Qt;
6827 DEFVAR_INT ("w32-quit-key", w32_quit_key,
6828 doc: /* If non-zero, the virtual key code for an alternative quit key. */);
6829 w32_quit_key = 0;
6831 DEFVAR_LISP ("w32-pass-lwindow-to-system",
6832 Vw32_pass_lwindow_to_system,
6833 doc: /* If non-nil, the left \"Windows\" key is passed on to Windows.
6835 When non-nil, the Start menu is opened by tapping the key.
6836 If you set this to nil, the left \"Windows\" key is processed by Emacs
6837 according to the value of `w32-lwindow-modifier', which see.
6839 Note that some combinations of the left \"Windows\" key with other keys are
6840 caught by Windows at low level, and so binding them in Emacs will have no
6841 effect. For example, <lwindow>-r always pops up the Windows Run dialog,
6842 <lwindow>-<Pause> pops up the "System Properties" dialog, etc. However, see
6843 the doc string of `w32-phantom-key-code'. */);
6844 Vw32_pass_lwindow_to_system = Qt;
6846 DEFVAR_LISP ("w32-pass-rwindow-to-system",
6847 Vw32_pass_rwindow_to_system,
6848 doc: /* If non-nil, the right \"Windows\" key is passed on to Windows.
6850 When non-nil, the Start menu is opened by tapping the key.
6851 If you set this to nil, the right \"Windows\" key is processed by Emacs
6852 according to the value of `w32-rwindow-modifier', which see.
6854 Note that some combinations of the right \"Windows\" key with other keys are
6855 caught by Windows at low level, and so binding them in Emacs will have no
6856 effect. For example, <rwindow>-r always pops up the Windows Run dialog,
6857 <rwindow>-<Pause> pops up the "System Properties" dialog, etc. However, see
6858 the doc string of `w32-phantom-key-code'. */);
6859 Vw32_pass_rwindow_to_system = Qt;
6861 DEFVAR_LISP ("w32-phantom-key-code",
6862 Vw32_phantom_key_code,
6863 doc: /* Virtual key code used to generate \"phantom\" key presses.
6864 Value is a number between 0 and 255.
6866 Phantom key presses are generated in order to stop the system from
6867 acting on \"Windows\" key events when `w32-pass-lwindow-to-system' or
6868 `w32-pass-rwindow-to-system' is nil. */);
6869 /* Although 255 is technically not a valid key code, it works and
6870 means that this hack won't interfere with any real key code. */
6871 XSETINT (Vw32_phantom_key_code, 255);
6873 DEFVAR_LISP ("w32-enable-num-lock",
6874 Vw32_enable_num_lock,
6875 doc: /* If non-nil, the Num Lock key acts normally.
6876 Set to nil to handle Num Lock as the `kp-numlock' key. */);
6877 Vw32_enable_num_lock = Qt;
6879 DEFVAR_LISP ("w32-enable-caps-lock",
6880 Vw32_enable_caps_lock,
6881 doc: /* If non-nil, the Caps Lock key acts normally.
6882 Set to nil to handle Caps Lock as the `capslock' key. */);
6883 Vw32_enable_caps_lock = Qt;
6885 DEFVAR_LISP ("w32-scroll-lock-modifier",
6886 Vw32_scroll_lock_modifier,
6887 doc: /* Modifier to use for the Scroll Lock ON state.
6888 The value can be hyper, super, meta, alt, control or shift for the
6889 respective modifier, or nil to handle Scroll Lock as the `scroll' key.
6890 Any other value will cause the Scroll Lock key to be ignored. */);
6891 Vw32_scroll_lock_modifier = Qnil;
6893 DEFVAR_LISP ("w32-lwindow-modifier",
6894 Vw32_lwindow_modifier,
6895 doc: /* Modifier to use for the left \"Windows\" key.
6896 The value can be hyper, super, meta, alt, control or shift for the
6897 respective modifier, or nil to appear as the `lwindow' key.
6898 Any other value will cause the key to be ignored. */);
6899 Vw32_lwindow_modifier = Qnil;
6901 DEFVAR_LISP ("w32-rwindow-modifier",
6902 Vw32_rwindow_modifier,
6903 doc: /* Modifier to use for the right \"Windows\" key.
6904 The value can be hyper, super, meta, alt, control or shift for the
6905 respective modifier, or nil to appear as the `rwindow' key.
6906 Any other value will cause the key to be ignored. */);
6907 Vw32_rwindow_modifier = Qnil;
6909 DEFVAR_LISP ("w32-apps-modifier",
6910 Vw32_apps_modifier,
6911 doc: /* Modifier to use for the \"Apps\" key.
6912 The value can be hyper, super, meta, alt, control or shift for the
6913 respective modifier, or nil to appear as the `apps' key.
6914 Any other value will cause the key to be ignored. */);
6915 Vw32_apps_modifier = Qnil;
6917 DEFVAR_BOOL ("w32-enable-synthesized-fonts", w32_enable_synthesized_fonts,
6918 doc: /* Non-nil enables selection of artificially italicized and bold fonts. */);
6919 w32_enable_synthesized_fonts = 0;
6921 DEFVAR_LISP ("w32-enable-palette", Vw32_enable_palette,
6922 doc: /* Non-nil enables Windows palette management to map colors exactly. */);
6923 Vw32_enable_palette = Qt;
6925 DEFVAR_INT ("w32-mouse-button-tolerance",
6926 w32_mouse_button_tolerance,
6927 doc: /* Analogue of double click interval for faking middle mouse events.
6928 The value is the minimum time in milliseconds that must elapse between
6929 left and right button down events before they are considered distinct events.
6930 If both mouse buttons are depressed within this interval, a middle mouse
6931 button down event is generated instead. */);
6932 w32_mouse_button_tolerance = GetDoubleClickTime () / 2;
6934 DEFVAR_INT ("w32-mouse-move-interval",
6935 w32_mouse_move_interval,
6936 doc: /* Minimum interval between mouse move events.
6937 The value is the minimum time in milliseconds that must elapse between
6938 successive mouse move (or scroll bar drag) events before they are
6939 reported as lisp events. */);
6940 w32_mouse_move_interval = 0;
6942 DEFVAR_BOOL ("w32-pass-extra-mouse-buttons-to-system",
6943 w32_pass_extra_mouse_buttons_to_system,
6944 doc: /* If non-nil, the fourth and fifth mouse buttons are passed to Windows.
6945 Recent versions of Windows support mice with up to five buttons.
6946 Since most applications don't support these extra buttons, most mouse
6947 drivers will allow you to map them to functions at the system level.
6948 If this variable is non-nil, Emacs will pass them on, allowing the
6949 system to handle them. */);
6950 w32_pass_extra_mouse_buttons_to_system = 0;
6952 DEFVAR_BOOL ("w32-pass-multimedia-buttons-to-system",
6953 w32_pass_multimedia_buttons_to_system,
6954 doc: /* If non-nil, media buttons are passed to Windows.
6955 Some modern keyboards contain buttons for controlling media players, web
6956 browsers and other applications. Generally these buttons are handled on a
6957 system wide basis, but by setting this to nil they are made available
6958 to Emacs for binding. Depending on your keyboard, additional keys that
6959 may be available are:
6961 browser-back, browser-forward, browser-refresh, browser-stop,
6962 browser-search, browser-favorites, browser-home,
6963 mail, mail-reply, mail-forward, mail-send,
6964 app-1, app-2,
6965 help, find, new, open, close, save, print, undo, redo, copy, cut, paste,
6966 spell-check, correction-list, toggle-dictate-command,
6967 media-next, media-previous, media-stop, media-play-pause, media-select,
6968 media-play, media-pause, media-record, media-fast-forward, media-rewind,
6969 media-channel-up, media-channel-down,
6970 volume-mute, volume-up, volume-down,
6971 mic-volume-mute, mic-volume-down, mic-volume-up, mic-toggle,
6972 bass-down, bass-boost, bass-up, treble-down, treble-up */);
6973 w32_pass_multimedia_buttons_to_system = 1;
6975 #if 0 /* TODO: Mouse cursor customization. */
6976 DEFVAR_LISP ("x-pointer-shape", Vx_pointer_shape,
6977 doc: /* The shape of the pointer when over text.
6978 Changing the value does not affect existing frames
6979 unless you set the mouse color. */);
6980 Vx_pointer_shape = Qnil;
6982 Vx_nontext_pointer_shape = Qnil;
6984 Vx_mode_pointer_shape = Qnil;
6986 DEFVAR_LISP ("x-hourglass-pointer-shape", Vx_hourglass_pointer_shape,
6987 doc: /* The shape of the pointer when Emacs is busy.
6988 This variable takes effect when you create a new frame
6989 or when you set the mouse color. */);
6990 Vx_hourglass_pointer_shape = Qnil;
6992 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
6993 Vx_sensitive_text_pointer_shape,
6994 doc: /* The shape of the pointer when over mouse-sensitive text.
6995 This variable takes effect when you create a new frame
6996 or when you set the mouse color. */);
6997 Vx_sensitive_text_pointer_shape = Qnil;
6999 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
7000 Vx_window_horizontal_drag_shape,
7001 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
7002 This variable takes effect when you create a new frame
7003 or when you set the mouse color. */);
7004 Vx_window_horizontal_drag_shape = Qnil;
7005 #endif
7007 DEFVAR_LISP ("x-cursor-fore-pixel", Vx_cursor_fore_pixel,
7008 doc: /* A string indicating the foreground color of the cursor box. */);
7009 Vx_cursor_fore_pixel = Qnil;
7011 DEFVAR_LISP ("x-max-tooltip-size", Vx_max_tooltip_size,
7012 doc: /* Maximum size for tooltips.
7013 Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
7014 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
7016 DEFVAR_LISP ("x-no-window-manager", Vx_no_window_manager,
7017 doc: /* Non-nil if no window manager is in use.
7018 Emacs doesn't try to figure this out; this is always nil
7019 unless you set it to something else. */);
7020 /* We don't have any way to find this out, so set it to nil
7021 and maybe the user would like to set it to t. */
7022 Vx_no_window_manager = Qnil;
7024 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
7025 Vx_pixel_size_width_font_regexp,
7026 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
7028 Since Emacs gets width of a font matching with this regexp from
7029 PIXEL_SIZE field of the name, font finding mechanism gets faster for
7030 such a font. This is especially effective for such large fonts as
7031 Chinese, Japanese, and Korean. */);
7032 Vx_pixel_size_width_font_regexp = Qnil;
7034 DEFVAR_LISP ("w32-bdf-filename-alist",
7035 Vw32_bdf_filename_alist,
7036 doc: /* List of bdf fonts and their corresponding filenames. */);
7037 Vw32_bdf_filename_alist = Qnil;
7039 DEFVAR_BOOL ("w32-strict-fontnames",
7040 w32_strict_fontnames,
7041 doc: /* Non-nil means only use fonts that are exact matches for those requested.
7042 Default is nil, which allows old fontnames that are not XLFD compliant,
7043 and allows third-party CJK display to work by specifying false charset
7044 fields to trick Emacs into translating to Big5, SJIS etc.
7045 Setting this to t will prevent wrong fonts being selected when
7046 fontsets are automatically created. */);
7047 w32_strict_fontnames = 0;
7049 DEFVAR_BOOL ("w32-strict-painting",
7050 w32_strict_painting,
7051 doc: /* Non-nil means use strict rules for repainting frames.
7052 Set this to nil to get the old behavior for repainting; this should
7053 only be necessary if the default setting causes problems. */);
7054 w32_strict_painting = 1;
7056 #if 0 /* TODO: Port to W32 */
7057 defsubr (&Sx_change_window_property);
7058 defsubr (&Sx_delete_window_property);
7059 defsubr (&Sx_window_property);
7060 #endif
7061 defsubr (&Sxw_display_color_p);
7062 defsubr (&Sx_display_grayscale_p);
7063 defsubr (&Sxw_color_defined_p);
7064 defsubr (&Sxw_color_values);
7065 defsubr (&Sx_server_max_request_size);
7066 defsubr (&Sx_server_vendor);
7067 defsubr (&Sx_server_version);
7068 defsubr (&Sx_display_pixel_width);
7069 defsubr (&Sx_display_pixel_height);
7070 defsubr (&Sx_display_mm_width);
7071 defsubr (&Sx_display_mm_height);
7072 defsubr (&Sx_display_screens);
7073 defsubr (&Sx_display_planes);
7074 defsubr (&Sx_display_color_cells);
7075 defsubr (&Sx_display_visual_class);
7076 defsubr (&Sx_display_backing_store);
7077 defsubr (&Sx_display_save_under);
7078 defsubr (&Sx_create_frame);
7079 defsubr (&Sx_open_connection);
7080 defsubr (&Sx_close_connection);
7081 defsubr (&Sx_display_list);
7082 defsubr (&Sx_synchronize);
7083 defsubr (&Sx_focus_frame);
7085 /* W32 specific functions */
7087 defsubr (&Sw32_define_rgb_color);
7088 defsubr (&Sw32_default_color_map);
7089 defsubr (&Sw32_send_sys_command);
7090 defsubr (&Sw32_shell_execute);
7091 defsubr (&Sw32_register_hot_key);
7092 defsubr (&Sw32_unregister_hot_key);
7093 defsubr (&Sw32_registered_hot_keys);
7094 defsubr (&Sw32_reconstruct_hot_key);
7095 defsubr (&Sw32_toggle_lock_key);
7096 defsubr (&Sw32_window_exists_p);
7097 defsubr (&Sw32_battery_status);
7099 defsubr (&Sfile_system_info);
7100 defsubr (&Sdefault_printer_name);
7102 check_window_system_func = check_w32;
7105 hourglass_timer = 0;
7106 hourglass_hwnd = NULL;
7108 defsubr (&Sx_show_tip);
7109 defsubr (&Sx_hide_tip);
7110 tip_timer = Qnil;
7111 staticpro (&tip_timer);
7112 tip_frame = Qnil;
7113 staticpro (&tip_frame);
7115 last_show_tip_args = Qnil;
7116 staticpro (&last_show_tip_args);
7118 defsubr (&Sx_file_dialog);
7119 defsubr (&Ssystem_move_file_to_trash);
7124 globals_of_w32fns is used to initialize those global variables that
7125 must always be initialized on startup even when the global variable
7126 initialized is non zero (see the function main in emacs.c).
7127 globals_of_w32fns is called from syms_of_w32fns when the global
7128 variable initialized is 0 and directly from main when initialized
7129 is non zero.
7131 void
7132 globals_of_w32fns (void)
7134 HMODULE user32_lib = GetModuleHandle ("user32.dll");
7136 TrackMouseEvent not available in all versions of Windows, so must load
7137 it dynamically. Do it once, here, instead of every time it is used.
7139 track_mouse_event_fn = (TrackMouseEvent_Proc)
7140 GetProcAddress (user32_lib, "TrackMouseEvent");
7142 monitor_from_point_fn = (MonitorFromPoint_Proc)
7143 GetProcAddress (user32_lib, "MonitorFromPoint");
7144 get_monitor_info_fn = (GetMonitorInfo_Proc)
7145 GetProcAddress (user32_lib, "GetMonitorInfoA");
7148 HMODULE imm32_lib = GetModuleHandle ("imm32.dll");
7149 get_composition_string_fn = (ImmGetCompositionString_Proc)
7150 GetProcAddress (imm32_lib, "ImmGetCompositionStringW");
7151 get_ime_context_fn = (ImmGetContext_Proc)
7152 GetProcAddress (imm32_lib, "ImmGetContext");
7153 release_ime_context_fn = (ImmReleaseContext_Proc)
7154 GetProcAddress (imm32_lib, "ImmReleaseContext");
7155 set_ime_composition_window_fn = (ImmSetCompositionWindow_Proc)
7156 GetProcAddress (imm32_lib, "ImmSetCompositionWindow");
7158 DEFVAR_INT ("w32-ansi-code-page",
7159 w32_ansi_code_page,
7160 doc: /* The ANSI code page used by the system. */);
7161 w32_ansi_code_page = GetACP ();
7163 /* MessageBox does not work without this when linked to comctl32.dll 6.0. */
7164 InitCommonControls ();
7166 syms_of_w32uniscribe ();
7169 #undef abort
7171 void
7172 w32_abort (void)
7174 int button;
7175 button = MessageBox (NULL,
7176 "A fatal error has occurred!\n\n"
7177 "Would you like to attach a debugger?\n\n"
7178 "Select YES to debug, NO to abort Emacs"
7179 #if __GNUC__
7180 "\n\n(type \"gdb -p <emacs-PID>\" and\n"
7181 "\"continue\" inside GDB before clicking YES.)"
7182 #endif
7183 , "Emacs Abort Dialog",
7184 MB_ICONEXCLAMATION | MB_TASKMODAL
7185 | MB_SETFOREGROUND | MB_YESNO);
7186 switch (button)
7188 case IDYES:
7189 DebugBreak ();
7190 exit (2); /* tell the compiler we will never return */
7191 case IDNO:
7192 default:
7193 abort ();
7194 break;
7198 /* For convenience when debugging. */
7200 w32_last_error (void)
7202 return GetLastError ();