Remove mention of obsolete president.
[emacs.git] / src / w32fns.c
blobbf261a754de83bed6c2d45d34715270504c62a03
1 /* Graphical user interface functions for the Microsoft W32 API.
2 Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 /* Added by Kevin Gallo */
23 #include <config.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include <limits.h>
28 #include <errno.h>
29 #include <math.h>
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"
50 #include "bitmaps/gray.xbm"
52 #include <commctrl.h>
53 #include <commdlg.h>
54 #include <shellapi.h>
55 #include <ctype.h>
56 #include <winspool.h>
57 #include <objbase.h>
59 #include <dlgs.h>
60 #include <imm.h>
61 #define FILE_NAME_TEXT_FIELD edt1
63 #include "font.h"
64 #include "w32font.h"
66 #ifndef FOF_NO_CONNECTED_ELEMENTS
67 #define FOF_NO_CONNECTED_ELEMENTS 0x2000
68 #endif
70 void syms_of_w32fns ();
71 void globals_of_w32fns ();
73 extern void free_frame_menubar ();
74 extern double atof ();
75 extern int w32_console_toggle_lock_key P_ ((int, Lisp_Object));
76 extern void w32_menu_display_help P_ ((HWND, HMENU, UINT, UINT));
77 extern void w32_free_menu_strings P_ ((HWND));
78 extern const char *map_w32_filename P_ ((const char *, const char **));
80 extern int quit_char;
82 extern char *lispy_function_keys[];
84 /* The colormap for converting color names to RGB values */
85 Lisp_Object Vw32_color_map;
87 /* Non nil if alt key presses are passed on to Windows. */
88 Lisp_Object Vw32_pass_alt_to_system;
90 /* Non nil if alt key is translated to meta_modifier, nil if it is translated
91 to alt_modifier. */
92 Lisp_Object Vw32_alt_is_meta;
94 /* If non-zero, the windows virtual key code for an alternative quit key. */
95 int w32_quit_key;
97 /* Non nil if left window key events are passed on to Windows (this only
98 affects whether "tapping" the key opens the Start menu). */
99 Lisp_Object Vw32_pass_lwindow_to_system;
101 /* Non nil if right window key events are passed on to Windows (this
102 only affects whether "tapping" the key opens the Start menu). */
103 Lisp_Object Vw32_pass_rwindow_to_system;
105 /* Virtual key code used to generate "phantom" key presses in order
106 to stop system from acting on Windows key events. */
107 Lisp_Object Vw32_phantom_key_code;
109 /* Modifier associated with the left "Windows" key, or nil to act as a
110 normal key. */
111 Lisp_Object Vw32_lwindow_modifier;
113 /* Modifier associated with the right "Windows" key, or nil to act as a
114 normal key. */
115 Lisp_Object Vw32_rwindow_modifier;
117 /* Modifier associated with the "Apps" key, or nil to act as a normal
118 key. */
119 Lisp_Object Vw32_apps_modifier;
121 /* Value is nil if Num Lock acts as a function key. */
122 Lisp_Object Vw32_enable_num_lock;
124 /* Value is nil if Caps Lock acts as a function key. */
125 Lisp_Object Vw32_enable_caps_lock;
127 /* Modifier associated with Scroll Lock, or nil to act as a normal key. */
128 Lisp_Object Vw32_scroll_lock_modifier;
130 /* Switch to control whether we inhibit requests for synthesized bold
131 and italic versions of fonts. */
132 int w32_enable_synthesized_fonts;
134 /* Enable palette management. */
135 Lisp_Object Vw32_enable_palette;
137 /* Control how close left/right button down events must be to
138 be converted to a middle button down event. */
139 int w32_mouse_button_tolerance;
141 /* Minimum interval between mouse movement (and scroll bar drag)
142 events that are passed on to the event loop. */
143 int w32_mouse_move_interval;
145 /* Flag to indicate if XBUTTON events should be passed on to Windows. */
146 static int w32_pass_extra_mouse_buttons_to_system;
148 /* Flag to indicate if media keys should be passed on to Windows. */
149 static int w32_pass_multimedia_buttons_to_system;
151 /* Non nil if no window manager is in use. */
152 Lisp_Object Vx_no_window_manager;
154 /* If non-zero, a w32 timer that, when it expires, displays an
155 hourglass cursor on all frames. */
156 static unsigned hourglass_timer = 0;
157 static HWND hourglass_hwnd = NULL;
159 #if 0 /* TODO: Mouse cursor customization. */
160 /* The background and shape of the mouse pointer, and shape when not
161 over text or in the modeline. */
162 Lisp_Object Vx_pointer_shape, Vx_nontext_pointer_shape, Vx_mode_pointer_shape;
163 Lisp_Object Vx_hourglass_pointer_shape, Vx_window_horizontal_drag_shape;
165 /* The shape when over mouse-sensitive text. */
167 Lisp_Object Vx_sensitive_text_pointer_shape;
168 #endif
170 #ifndef IDC_HAND
171 #define IDC_HAND MAKEINTRESOURCE(32649)
172 #endif
174 /* Color of chars displayed in cursor box. */
175 Lisp_Object Vx_cursor_fore_pixel;
177 /* Nonzero if using Windows. */
179 static int w32_in_use;
181 /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'. */
183 Lisp_Object Vx_pixel_size_width_font_regexp;
185 /* Alist of bdf fonts and the files that define them. */
186 Lisp_Object Vw32_bdf_filename_alist;
188 /* A flag to control whether fonts are matched strictly or not. */
189 static int w32_strict_fontnames;
191 /* A flag to control whether we should only repaint if GetUpdateRect
192 indicates there is an update region. */
193 static int w32_strict_painting;
195 Lisp_Object Qnone;
196 Lisp_Object Qsuppress_icon;
197 Lisp_Object Qundefined_color;
198 Lisp_Object Qcancel_timer;
199 Lisp_Object Qfont_param;
200 Lisp_Object Qhyper;
201 Lisp_Object Qsuper;
202 Lisp_Object Qmeta;
203 Lisp_Object Qalt;
204 Lisp_Object Qctrl;
205 Lisp_Object Qcontrol;
206 Lisp_Object Qshift;
209 /* The ANSI codepage. */
210 int w32_ansi_code_page;
212 /* Prefix for system colors. */
213 #define SYSTEM_COLOR_PREFIX "System"
214 #define SYSTEM_COLOR_PREFIX_LEN (sizeof (SYSTEM_COLOR_PREFIX) - 1)
216 /* State variables for emulating a three button mouse. */
217 #define LMOUSE 1
218 #define MMOUSE 2
219 #define RMOUSE 4
221 static int button_state = 0;
222 static W32Msg saved_mouse_button_msg;
223 static unsigned mouse_button_timer = 0; /* non-zero when timer is active */
224 static W32Msg saved_mouse_move_msg;
225 static unsigned mouse_move_timer = 0;
227 /* Window that is tracking the mouse. */
228 static HWND track_mouse_window;
230 /* Multi-monitor API definitions that are not pulled from the headers
231 since we are compiling for NT 4. */
232 #ifndef MONITOR_DEFAULT_TO_NEAREST
233 #define MONITOR_DEFAULT_TO_NEAREST 2
234 #endif
235 /* MinGW headers define MONITORINFO unconditionally, but MSVC ones don't.
236 To avoid a compile error on one or the other, redefine with a new name. */
237 struct MONITOR_INFO
239 DWORD cbSize;
240 RECT rcMonitor;
241 RECT rcWork;
242 DWORD dwFlags;
245 /* Reportedly, VS 6 does not have this in its headers. */
246 #if defined(_MSC_VER) && _MSC_VER < 1300
247 DECLARE_HANDLE(HMONITOR);
248 #endif
250 typedef BOOL (WINAPI * TrackMouseEvent_Proc)
251 (IN OUT LPTRACKMOUSEEVENT lpEventTrack);
252 typedef LONG (WINAPI * ImmGetCompositionString_Proc)
253 (IN HIMC context, IN DWORD index, OUT LPVOID buffer, IN DWORD bufLen);
254 typedef HIMC (WINAPI * ImmGetContext_Proc) (IN HWND window);
255 typedef HMONITOR (WINAPI * MonitorFromPoint_Proc) (IN POINT pt, IN DWORD flags);
256 typedef BOOL (WINAPI * GetMonitorInfo_Proc)
257 (IN HMONITOR monitor, OUT struct MONITOR_INFO* info);
259 TrackMouseEvent_Proc track_mouse_event_fn = NULL;
260 ClipboardSequence_Proc clipboard_sequence_fn = NULL;
261 ImmGetCompositionString_Proc get_composition_string_fn = NULL;
262 ImmGetContext_Proc get_ime_context_fn = NULL;
263 MonitorFromPoint_Proc monitor_from_point_fn = NULL;
264 GetMonitorInfo_Proc get_monitor_info_fn = NULL;
266 extern AppendMenuW_Proc unicode_append_menu;
268 /* Flag to selectively ignore WM_IME_CHAR messages. */
269 static int ignore_ime_char = 0;
271 /* W95 mousewheel handler */
272 unsigned int msh_mousewheel = 0;
274 /* Timers */
275 #define MOUSE_BUTTON_ID 1
276 #define MOUSE_MOVE_ID 2
277 #define MENU_FREE_ID 3
278 #define HOURGLASS_ID 4
279 /* The delay (milliseconds) before a menu is freed after WM_EXITMENULOOP
280 is received. */
281 #define MENU_FREE_DELAY 1000
282 static unsigned menu_free_timer = 0;
284 /* The below are defined in frame.c. */
286 extern Lisp_Object Vwindow_system_version;
288 #ifdef GLYPH_DEBUG
289 int image_cache_refcount, dpyinfo_refcount;
290 #endif
293 /* From w32term.c. */
294 extern int w32_num_mouse_buttons;
295 extern Lisp_Object Vw32_recognize_altgr;
297 extern HWND w32_system_caret_hwnd;
299 extern int w32_system_caret_height;
300 extern int w32_system_caret_x;
301 extern int w32_system_caret_y;
302 extern int w32_use_visible_system_caret;
304 static HWND w32_visible_system_caret_hwnd;
306 /* From w32menu.c */
307 extern HMENU current_popup_menu;
308 static int menubar_in_use = 0;
310 /* From w32uniscribe.c */
311 extern void syms_of_w32uniscribe ();
312 extern int uniscribe_available;
314 /* Function prototypes for hourglass support. */
315 static void w32_show_hourglass P_ ((struct frame *));
316 static void w32_hide_hourglass P_ ((void));
320 /* Error if we are not connected to MS-Windows. */
321 void
322 check_w32 ()
324 if (! w32_in_use)
325 error ("MS-Windows not in use or not initialized");
328 /* Nonzero if we can use mouse menus.
329 You should not call this unless HAVE_MENUS is defined. */
332 have_menus_p ()
334 return w32_in_use;
337 /* Extract a frame as a FRAME_PTR, defaulting to the selected frame
338 and checking validity for W32. */
340 FRAME_PTR
341 check_x_frame (frame)
342 Lisp_Object frame;
344 FRAME_PTR f;
346 if (NILP (frame))
347 frame = selected_frame;
348 CHECK_LIVE_FRAME (frame);
349 f = XFRAME (frame);
350 if (! FRAME_W32_P (f))
351 error ("Non-W32 frame used");
352 return f;
355 /* Let the user specify a display with a frame.
356 nil stands for the selected frame--or, if that is not a w32 frame,
357 the first display on the list. */
359 struct w32_display_info *
360 check_x_display_info (frame)
361 Lisp_Object frame;
363 if (NILP (frame))
365 struct frame *sf = XFRAME (selected_frame);
367 if (FRAME_W32_P (sf) && FRAME_LIVE_P (sf))
368 return FRAME_W32_DISPLAY_INFO (sf);
369 else
370 return &one_w32_display_info;
372 else if (STRINGP (frame))
373 return x_display_info_for_name (frame);
374 else
376 FRAME_PTR f;
378 CHECK_LIVE_FRAME (frame);
379 f = XFRAME (frame);
380 if (! FRAME_W32_P (f))
381 error ("Non-W32 frame used");
382 return FRAME_W32_DISPLAY_INFO (f);
386 /* Return the Emacs frame-object corresponding to an w32 window.
387 It could be the frame's main window or an icon window. */
389 /* This function can be called during GC, so use GC_xxx type test macros. */
391 struct frame *
392 x_window_to_frame (dpyinfo, wdesc)
393 struct w32_display_info *dpyinfo;
394 HWND wdesc;
396 Lisp_Object tail, frame;
397 struct frame *f;
399 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
401 frame = XCAR (tail);
402 if (!FRAMEP (frame))
403 continue;
404 f = XFRAME (frame);
405 if (!FRAME_W32_P (f) || FRAME_W32_DISPLAY_INFO (f) != dpyinfo)
406 continue;
408 if (FRAME_W32_WINDOW (f) == wdesc)
409 return f;
411 return 0;
415 static Lisp_Object unwind_create_frame P_ ((Lisp_Object));
416 static Lisp_Object unwind_create_tip_frame P_ ((Lisp_Object));
417 static void my_create_window P_ ((struct frame *));
418 static void my_create_tip_window P_ ((struct frame *));
420 /* TODO: Native Input Method support; see x_create_im. */
421 void x_set_foreground_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
422 void x_set_background_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
423 void x_set_mouse_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
424 void x_set_cursor_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
425 void x_set_border_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
426 void x_set_cursor_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
427 void x_set_icon_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
428 void x_set_icon_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
429 void x_explicitly_set_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
430 void x_set_menu_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
431 void x_set_title P_ ((struct frame *, Lisp_Object, Lisp_Object));
432 void x_set_tool_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
433 static void x_edge_detection P_ ((struct frame *, struct image *, Lisp_Object,
434 Lisp_Object));
439 /* Store the screen positions of frame F into XPTR and YPTR.
440 These are the positions of the containing window manager window,
441 not Emacs's own window. */
443 void
444 x_real_positions (f, xptr, yptr)
445 FRAME_PTR f;
446 int *xptr, *yptr;
448 POINT pt;
449 RECT rect;
451 /* Get the bounds of the WM window. */
452 GetWindowRect (FRAME_W32_WINDOW (f), &rect);
454 pt.x = 0;
455 pt.y = 0;
457 /* Convert (0, 0) in the client area to screen co-ordinates. */
458 ClientToScreen (FRAME_W32_WINDOW (f), &pt);
460 /* Remember x_pixels_diff and y_pixels_diff. */
461 f->x_pixels_diff = pt.x - rect.left;
462 f->y_pixels_diff = pt.y - rect.top;
464 *xptr = rect.left;
465 *yptr = rect.top;
470 DEFUN ("w32-define-rgb-color", Fw32_define_rgb_color,
471 Sw32_define_rgb_color, 4, 4, 0,
472 doc: /* Convert RGB numbers to a Windows color reference and associate with NAME.
473 This adds or updates a named color to `w32-color-map', making it
474 available for use. The original entry's RGB ref is returned, or nil
475 if the entry is new. */)
476 (red, green, blue, name)
477 Lisp_Object red, green, blue, name;
479 Lisp_Object rgb;
480 Lisp_Object oldrgb = Qnil;
481 Lisp_Object entry;
483 CHECK_NUMBER (red);
484 CHECK_NUMBER (green);
485 CHECK_NUMBER (blue);
486 CHECK_STRING (name);
488 XSETINT (rgb, RGB (XUINT (red), XUINT (green), XUINT (blue)));
490 BLOCK_INPUT;
492 /* replace existing entry in w32-color-map or add new entry. */
493 entry = Fassoc (name, Vw32_color_map);
494 if (NILP (entry))
496 entry = Fcons (name, rgb);
497 Vw32_color_map = Fcons (entry, Vw32_color_map);
499 else
501 oldrgb = Fcdr (entry);
502 Fsetcdr (entry, rgb);
505 UNBLOCK_INPUT;
507 return (oldrgb);
510 /* The default colors for the w32 color map */
511 typedef struct colormap_t
513 char *name;
514 COLORREF colorref;
515 } colormap_t;
517 colormap_t w32_color_map[] =
519 {"snow" , PALETTERGB (255,250,250)},
520 {"ghost white" , PALETTERGB (248,248,255)},
521 {"GhostWhite" , PALETTERGB (248,248,255)},
522 {"white smoke" , PALETTERGB (245,245,245)},
523 {"WhiteSmoke" , PALETTERGB (245,245,245)},
524 {"gainsboro" , PALETTERGB (220,220,220)},
525 {"floral white" , PALETTERGB (255,250,240)},
526 {"FloralWhite" , PALETTERGB (255,250,240)},
527 {"old lace" , PALETTERGB (253,245,230)},
528 {"OldLace" , PALETTERGB (253,245,230)},
529 {"linen" , PALETTERGB (250,240,230)},
530 {"antique white" , PALETTERGB (250,235,215)},
531 {"AntiqueWhite" , PALETTERGB (250,235,215)},
532 {"papaya whip" , PALETTERGB (255,239,213)},
533 {"PapayaWhip" , PALETTERGB (255,239,213)},
534 {"blanched almond" , PALETTERGB (255,235,205)},
535 {"BlanchedAlmond" , PALETTERGB (255,235,205)},
536 {"bisque" , PALETTERGB (255,228,196)},
537 {"peach puff" , PALETTERGB (255,218,185)},
538 {"PeachPuff" , PALETTERGB (255,218,185)},
539 {"navajo white" , PALETTERGB (255,222,173)},
540 {"NavajoWhite" , PALETTERGB (255,222,173)},
541 {"moccasin" , PALETTERGB (255,228,181)},
542 {"cornsilk" , PALETTERGB (255,248,220)},
543 {"ivory" , PALETTERGB (255,255,240)},
544 {"lemon chiffon" , PALETTERGB (255,250,205)},
545 {"LemonChiffon" , PALETTERGB (255,250,205)},
546 {"seashell" , PALETTERGB (255,245,238)},
547 {"honeydew" , PALETTERGB (240,255,240)},
548 {"mint cream" , PALETTERGB (245,255,250)},
549 {"MintCream" , PALETTERGB (245,255,250)},
550 {"azure" , PALETTERGB (240,255,255)},
551 {"alice blue" , PALETTERGB (240,248,255)},
552 {"AliceBlue" , PALETTERGB (240,248,255)},
553 {"lavender" , PALETTERGB (230,230,250)},
554 {"lavender blush" , PALETTERGB (255,240,245)},
555 {"LavenderBlush" , PALETTERGB (255,240,245)},
556 {"misty rose" , PALETTERGB (255,228,225)},
557 {"MistyRose" , PALETTERGB (255,228,225)},
558 {"white" , PALETTERGB (255,255,255)},
559 {"black" , PALETTERGB ( 0, 0, 0)},
560 {"dark slate gray" , PALETTERGB ( 47, 79, 79)},
561 {"DarkSlateGray" , PALETTERGB ( 47, 79, 79)},
562 {"dark slate grey" , PALETTERGB ( 47, 79, 79)},
563 {"DarkSlateGrey" , PALETTERGB ( 47, 79, 79)},
564 {"dim gray" , PALETTERGB (105,105,105)},
565 {"DimGray" , PALETTERGB (105,105,105)},
566 {"dim grey" , PALETTERGB (105,105,105)},
567 {"DimGrey" , PALETTERGB (105,105,105)},
568 {"slate gray" , PALETTERGB (112,128,144)},
569 {"SlateGray" , PALETTERGB (112,128,144)},
570 {"slate grey" , PALETTERGB (112,128,144)},
571 {"SlateGrey" , PALETTERGB (112,128,144)},
572 {"light slate gray" , PALETTERGB (119,136,153)},
573 {"LightSlateGray" , PALETTERGB (119,136,153)},
574 {"light slate grey" , PALETTERGB (119,136,153)},
575 {"LightSlateGrey" , PALETTERGB (119,136,153)},
576 {"gray" , PALETTERGB (190,190,190)},
577 {"grey" , PALETTERGB (190,190,190)},
578 {"light grey" , PALETTERGB (211,211,211)},
579 {"LightGrey" , PALETTERGB (211,211,211)},
580 {"light gray" , PALETTERGB (211,211,211)},
581 {"LightGray" , PALETTERGB (211,211,211)},
582 {"midnight blue" , PALETTERGB ( 25, 25,112)},
583 {"MidnightBlue" , PALETTERGB ( 25, 25,112)},
584 {"navy" , PALETTERGB ( 0, 0,128)},
585 {"navy blue" , PALETTERGB ( 0, 0,128)},
586 {"NavyBlue" , PALETTERGB ( 0, 0,128)},
587 {"cornflower blue" , PALETTERGB (100,149,237)},
588 {"CornflowerBlue" , PALETTERGB (100,149,237)},
589 {"dark slate blue" , PALETTERGB ( 72, 61,139)},
590 {"DarkSlateBlue" , PALETTERGB ( 72, 61,139)},
591 {"slate blue" , PALETTERGB (106, 90,205)},
592 {"SlateBlue" , PALETTERGB (106, 90,205)},
593 {"medium slate blue" , PALETTERGB (123,104,238)},
594 {"MediumSlateBlue" , PALETTERGB (123,104,238)},
595 {"light slate blue" , PALETTERGB (132,112,255)},
596 {"LightSlateBlue" , PALETTERGB (132,112,255)},
597 {"medium blue" , PALETTERGB ( 0, 0,205)},
598 {"MediumBlue" , PALETTERGB ( 0, 0,205)},
599 {"royal blue" , PALETTERGB ( 65,105,225)},
600 {"RoyalBlue" , PALETTERGB ( 65,105,225)},
601 {"blue" , PALETTERGB ( 0, 0,255)},
602 {"dodger blue" , PALETTERGB ( 30,144,255)},
603 {"DodgerBlue" , PALETTERGB ( 30,144,255)},
604 {"deep sky blue" , PALETTERGB ( 0,191,255)},
605 {"DeepSkyBlue" , PALETTERGB ( 0,191,255)},
606 {"sky blue" , PALETTERGB (135,206,235)},
607 {"SkyBlue" , PALETTERGB (135,206,235)},
608 {"light sky blue" , PALETTERGB (135,206,250)},
609 {"LightSkyBlue" , PALETTERGB (135,206,250)},
610 {"steel blue" , PALETTERGB ( 70,130,180)},
611 {"SteelBlue" , PALETTERGB ( 70,130,180)},
612 {"light steel blue" , PALETTERGB (176,196,222)},
613 {"LightSteelBlue" , PALETTERGB (176,196,222)},
614 {"light blue" , PALETTERGB (173,216,230)},
615 {"LightBlue" , PALETTERGB (173,216,230)},
616 {"powder blue" , PALETTERGB (176,224,230)},
617 {"PowderBlue" , PALETTERGB (176,224,230)},
618 {"pale turquoise" , PALETTERGB (175,238,238)},
619 {"PaleTurquoise" , PALETTERGB (175,238,238)},
620 {"dark turquoise" , PALETTERGB ( 0,206,209)},
621 {"DarkTurquoise" , PALETTERGB ( 0,206,209)},
622 {"medium turquoise" , PALETTERGB ( 72,209,204)},
623 {"MediumTurquoise" , PALETTERGB ( 72,209,204)},
624 {"turquoise" , PALETTERGB ( 64,224,208)},
625 {"cyan" , PALETTERGB ( 0,255,255)},
626 {"light cyan" , PALETTERGB (224,255,255)},
627 {"LightCyan" , PALETTERGB (224,255,255)},
628 {"cadet blue" , PALETTERGB ( 95,158,160)},
629 {"CadetBlue" , PALETTERGB ( 95,158,160)},
630 {"medium aquamarine" , PALETTERGB (102,205,170)},
631 {"MediumAquamarine" , PALETTERGB (102,205,170)},
632 {"aquamarine" , PALETTERGB (127,255,212)},
633 {"dark green" , PALETTERGB ( 0,100, 0)},
634 {"DarkGreen" , PALETTERGB ( 0,100, 0)},
635 {"dark olive green" , PALETTERGB ( 85,107, 47)},
636 {"DarkOliveGreen" , PALETTERGB ( 85,107, 47)},
637 {"dark sea green" , PALETTERGB (143,188,143)},
638 {"DarkSeaGreen" , PALETTERGB (143,188,143)},
639 {"sea green" , PALETTERGB ( 46,139, 87)},
640 {"SeaGreen" , PALETTERGB ( 46,139, 87)},
641 {"medium sea green" , PALETTERGB ( 60,179,113)},
642 {"MediumSeaGreen" , PALETTERGB ( 60,179,113)},
643 {"light sea green" , PALETTERGB ( 32,178,170)},
644 {"LightSeaGreen" , PALETTERGB ( 32,178,170)},
645 {"pale green" , PALETTERGB (152,251,152)},
646 {"PaleGreen" , PALETTERGB (152,251,152)},
647 {"spring green" , PALETTERGB ( 0,255,127)},
648 {"SpringGreen" , PALETTERGB ( 0,255,127)},
649 {"lawn green" , PALETTERGB (124,252, 0)},
650 {"LawnGreen" , PALETTERGB (124,252, 0)},
651 {"green" , PALETTERGB ( 0,255, 0)},
652 {"chartreuse" , PALETTERGB (127,255, 0)},
653 {"medium spring green" , PALETTERGB ( 0,250,154)},
654 {"MediumSpringGreen" , PALETTERGB ( 0,250,154)},
655 {"green yellow" , PALETTERGB (173,255, 47)},
656 {"GreenYellow" , PALETTERGB (173,255, 47)},
657 {"lime green" , PALETTERGB ( 50,205, 50)},
658 {"LimeGreen" , PALETTERGB ( 50,205, 50)},
659 {"yellow green" , PALETTERGB (154,205, 50)},
660 {"YellowGreen" , PALETTERGB (154,205, 50)},
661 {"forest green" , PALETTERGB ( 34,139, 34)},
662 {"ForestGreen" , PALETTERGB ( 34,139, 34)},
663 {"olive drab" , PALETTERGB (107,142, 35)},
664 {"OliveDrab" , PALETTERGB (107,142, 35)},
665 {"dark khaki" , PALETTERGB (189,183,107)},
666 {"DarkKhaki" , PALETTERGB (189,183,107)},
667 {"khaki" , PALETTERGB (240,230,140)},
668 {"pale goldenrod" , PALETTERGB (238,232,170)},
669 {"PaleGoldenrod" , PALETTERGB (238,232,170)},
670 {"light goldenrod yellow" , PALETTERGB (250,250,210)},
671 {"LightGoldenrodYellow" , PALETTERGB (250,250,210)},
672 {"light yellow" , PALETTERGB (255,255,224)},
673 {"LightYellow" , PALETTERGB (255,255,224)},
674 {"yellow" , PALETTERGB (255,255, 0)},
675 {"gold" , PALETTERGB (255,215, 0)},
676 {"light goldenrod" , PALETTERGB (238,221,130)},
677 {"LightGoldenrod" , PALETTERGB (238,221,130)},
678 {"goldenrod" , PALETTERGB (218,165, 32)},
679 {"dark goldenrod" , PALETTERGB (184,134, 11)},
680 {"DarkGoldenrod" , PALETTERGB (184,134, 11)},
681 {"rosy brown" , PALETTERGB (188,143,143)},
682 {"RosyBrown" , PALETTERGB (188,143,143)},
683 {"indian red" , PALETTERGB (205, 92, 92)},
684 {"IndianRed" , PALETTERGB (205, 92, 92)},
685 {"saddle brown" , PALETTERGB (139, 69, 19)},
686 {"SaddleBrown" , PALETTERGB (139, 69, 19)},
687 {"sienna" , PALETTERGB (160, 82, 45)},
688 {"peru" , PALETTERGB (205,133, 63)},
689 {"burlywood" , PALETTERGB (222,184,135)},
690 {"beige" , PALETTERGB (245,245,220)},
691 {"wheat" , PALETTERGB (245,222,179)},
692 {"sandy brown" , PALETTERGB (244,164, 96)},
693 {"SandyBrown" , PALETTERGB (244,164, 96)},
694 {"tan" , PALETTERGB (210,180,140)},
695 {"chocolate" , PALETTERGB (210,105, 30)},
696 {"firebrick" , PALETTERGB (178,34, 34)},
697 {"brown" , PALETTERGB (165,42, 42)},
698 {"dark salmon" , PALETTERGB (233,150,122)},
699 {"DarkSalmon" , PALETTERGB (233,150,122)},
700 {"salmon" , PALETTERGB (250,128,114)},
701 {"light salmon" , PALETTERGB (255,160,122)},
702 {"LightSalmon" , PALETTERGB (255,160,122)},
703 {"orange" , PALETTERGB (255,165, 0)},
704 {"dark orange" , PALETTERGB (255,140, 0)},
705 {"DarkOrange" , PALETTERGB (255,140, 0)},
706 {"coral" , PALETTERGB (255,127, 80)},
707 {"light coral" , PALETTERGB (240,128,128)},
708 {"LightCoral" , PALETTERGB (240,128,128)},
709 {"tomato" , PALETTERGB (255, 99, 71)},
710 {"orange red" , PALETTERGB (255, 69, 0)},
711 {"OrangeRed" , PALETTERGB (255, 69, 0)},
712 {"red" , PALETTERGB (255, 0, 0)},
713 {"hot pink" , PALETTERGB (255,105,180)},
714 {"HotPink" , PALETTERGB (255,105,180)},
715 {"deep pink" , PALETTERGB (255, 20,147)},
716 {"DeepPink" , PALETTERGB (255, 20,147)},
717 {"pink" , PALETTERGB (255,192,203)},
718 {"light pink" , PALETTERGB (255,182,193)},
719 {"LightPink" , PALETTERGB (255,182,193)},
720 {"pale violet red" , PALETTERGB (219,112,147)},
721 {"PaleVioletRed" , PALETTERGB (219,112,147)},
722 {"maroon" , PALETTERGB (176, 48, 96)},
723 {"medium violet red" , PALETTERGB (199, 21,133)},
724 {"MediumVioletRed" , PALETTERGB (199, 21,133)},
725 {"violet red" , PALETTERGB (208, 32,144)},
726 {"VioletRed" , PALETTERGB (208, 32,144)},
727 {"magenta" , PALETTERGB (255, 0,255)},
728 {"violet" , PALETTERGB (238,130,238)},
729 {"plum" , PALETTERGB (221,160,221)},
730 {"orchid" , PALETTERGB (218,112,214)},
731 {"medium orchid" , PALETTERGB (186, 85,211)},
732 {"MediumOrchid" , PALETTERGB (186, 85,211)},
733 {"dark orchid" , PALETTERGB (153, 50,204)},
734 {"DarkOrchid" , PALETTERGB (153, 50,204)},
735 {"dark violet" , PALETTERGB (148, 0,211)},
736 {"DarkViolet" , PALETTERGB (148, 0,211)},
737 {"blue violet" , PALETTERGB (138, 43,226)},
738 {"BlueViolet" , PALETTERGB (138, 43,226)},
739 {"purple" , PALETTERGB (160, 32,240)},
740 {"medium purple" , PALETTERGB (147,112,219)},
741 {"MediumPurple" , PALETTERGB (147,112,219)},
742 {"thistle" , PALETTERGB (216,191,216)},
743 {"gray0" , PALETTERGB ( 0, 0, 0)},
744 {"grey0" , PALETTERGB ( 0, 0, 0)},
745 {"dark grey" , PALETTERGB (169,169,169)},
746 {"DarkGrey" , PALETTERGB (169,169,169)},
747 {"dark gray" , PALETTERGB (169,169,169)},
748 {"DarkGray" , PALETTERGB (169,169,169)},
749 {"dark blue" , PALETTERGB ( 0, 0,139)},
750 {"DarkBlue" , PALETTERGB ( 0, 0,139)},
751 {"dark cyan" , PALETTERGB ( 0,139,139)},
752 {"DarkCyan" , PALETTERGB ( 0,139,139)},
753 {"dark magenta" , PALETTERGB (139, 0,139)},
754 {"DarkMagenta" , PALETTERGB (139, 0,139)},
755 {"dark red" , PALETTERGB (139, 0, 0)},
756 {"DarkRed" , PALETTERGB (139, 0, 0)},
757 {"light green" , PALETTERGB (144,238,144)},
758 {"LightGreen" , PALETTERGB (144,238,144)},
761 DEFUN ("w32-default-color-map", Fw32_default_color_map, Sw32_default_color_map,
762 0, 0, 0, doc: /* Return the default color map. */)
765 int i;
766 colormap_t *pc = w32_color_map;
767 Lisp_Object cmap;
769 BLOCK_INPUT;
771 cmap = Qnil;
773 for (i = 0; i < sizeof (w32_color_map) / sizeof (w32_color_map[0]);
774 pc++, i++)
775 cmap = Fcons (Fcons (build_string (pc->name),
776 make_number (pc->colorref)),
777 cmap);
779 UNBLOCK_INPUT;
781 return (cmap);
784 static Lisp_Object
785 w32_to_x_color (rgb)
786 Lisp_Object rgb;
788 Lisp_Object color;
790 CHECK_NUMBER (rgb);
792 BLOCK_INPUT;
794 color = Frassq (rgb, Vw32_color_map);
796 UNBLOCK_INPUT;
798 if (!NILP (color))
799 return (Fcar (color));
800 else
801 return Qnil;
804 static Lisp_Object
805 w32_color_map_lookup (colorname)
806 char *colorname;
808 Lisp_Object tail, ret = Qnil;
810 BLOCK_INPUT;
812 for (tail = Vw32_color_map; CONSP (tail); tail = XCDR (tail))
814 register Lisp_Object elt, tem;
816 elt = XCAR (tail);
817 if (!CONSP (elt)) continue;
819 tem = Fcar (elt);
821 if (lstrcmpi (SDATA (tem), colorname) == 0)
823 ret = Fcdr (elt);
824 break;
827 QUIT;
831 UNBLOCK_INPUT;
833 return ret;
837 static void
838 add_system_logical_colors_to_map (system_colors)
839 Lisp_Object *system_colors;
841 HKEY colors_key;
843 /* Other registry operations are done with input blocked. */
844 BLOCK_INPUT;
846 /* Look for "Control Panel/Colors" under User and Machine registry
847 settings. */
848 if (RegOpenKeyEx (HKEY_CURRENT_USER, "Control Panel\\Colors", 0,
849 KEY_READ, &colors_key) == ERROR_SUCCESS
850 || RegOpenKeyEx (HKEY_LOCAL_MACHINE, "Control Panel\\Colors", 0,
851 KEY_READ, &colors_key) == ERROR_SUCCESS)
853 /* List all keys. */
854 char color_buffer[64];
855 char full_name_buffer[MAX_PATH + SYSTEM_COLOR_PREFIX_LEN];
856 int index = 0;
857 DWORD name_size, color_size;
858 char *name_buffer = full_name_buffer + SYSTEM_COLOR_PREFIX_LEN;
860 name_size = sizeof (full_name_buffer) - SYSTEM_COLOR_PREFIX_LEN;
861 color_size = sizeof (color_buffer);
863 strcpy (full_name_buffer, SYSTEM_COLOR_PREFIX);
865 while (RegEnumValueA (colors_key, index, name_buffer, &name_size,
866 NULL, NULL, color_buffer, &color_size)
867 == ERROR_SUCCESS)
869 int r, g, b;
870 if (sscanf (color_buffer, " %u %u %u", &r, &g, &b) == 3)
871 *system_colors = Fcons (Fcons (build_string (full_name_buffer),
872 make_number (RGB (r, g, b))),
873 *system_colors);
875 name_size = sizeof (full_name_buffer) - SYSTEM_COLOR_PREFIX_LEN;
876 color_size = sizeof (color_buffer);
877 index++;
879 RegCloseKey (colors_key);
882 UNBLOCK_INPUT;
886 static Lisp_Object
887 x_to_w32_color (colorname)
888 char * colorname;
890 register Lisp_Object ret = Qnil;
892 BLOCK_INPUT;
894 if (colorname[0] == '#')
896 /* Could be an old-style RGB Device specification. */
897 char *color;
898 int size;
899 color = colorname + 1;
901 size = strlen (color);
902 if (size == 3 || size == 6 || size == 9 || size == 12)
904 UINT colorval;
905 int i, pos;
906 pos = 0;
907 size /= 3;
908 colorval = 0;
910 for (i = 0; i < 3; i++)
912 char *end;
913 char t;
914 unsigned long value;
916 /* The check for 'x' in the following conditional takes into
917 account the fact that strtol allows a "0x" in front of
918 our numbers, and we don't. */
919 if (!isxdigit (color[0]) || color[1] == 'x')
920 break;
921 t = color[size];
922 color[size] = '\0';
923 value = strtoul (color, &end, 16);
924 color[size] = t;
925 if (errno == ERANGE || end - color != size)
926 break;
927 switch (size)
929 case 1:
930 value = value * 0x10;
931 break;
932 case 2:
933 break;
934 case 3:
935 value /= 0x10;
936 break;
937 case 4:
938 value /= 0x100;
939 break;
941 colorval |= (value << pos);
942 pos += 0x8;
943 if (i == 2)
945 UNBLOCK_INPUT;
946 XSETINT (ret, colorval);
947 return ret;
949 color = end;
953 else if (strnicmp (colorname, "rgb:", 4) == 0)
955 char *color;
956 UINT colorval;
957 int i, pos;
958 pos = 0;
960 colorval = 0;
961 color = colorname + 4;
962 for (i = 0; i < 3; i++)
964 char *end;
965 unsigned long value;
967 /* The check for 'x' in the following conditional takes into
968 account the fact that strtol allows a "0x" in front of
969 our numbers, and we don't. */
970 if (!isxdigit (color[0]) || color[1] == 'x')
971 break;
972 value = strtoul (color, &end, 16);
973 if (errno == ERANGE)
974 break;
975 switch (end - color)
977 case 1:
978 value = value * 0x10 + value;
979 break;
980 case 2:
981 break;
982 case 3:
983 value /= 0x10;
984 break;
985 case 4:
986 value /= 0x100;
987 break;
988 default:
989 value = ULONG_MAX;
991 if (value == ULONG_MAX)
992 break;
993 colorval |= (value << pos);
994 pos += 0x8;
995 if (i == 2)
997 if (*end != '\0')
998 break;
999 UNBLOCK_INPUT;
1000 XSETINT (ret, colorval);
1001 return ret;
1003 if (*end != '/')
1004 break;
1005 color = end + 1;
1008 else if (strnicmp (colorname, "rgbi:", 5) == 0)
1010 /* This is an RGB Intensity specification. */
1011 char *color;
1012 UINT colorval;
1013 int i, pos;
1014 pos = 0;
1016 colorval = 0;
1017 color = colorname + 5;
1018 for (i = 0; i < 3; i++)
1020 char *end;
1021 double value;
1022 UINT val;
1024 value = strtod (color, &end);
1025 if (errno == ERANGE)
1026 break;
1027 if (value < 0.0 || value > 1.0)
1028 break;
1029 val = (UINT)(0x100 * value);
1030 /* We used 0x100 instead of 0xFF to give a continuous
1031 range between 0.0 and 1.0 inclusive. The next statement
1032 fixes the 1.0 case. */
1033 if (val == 0x100)
1034 val = 0xFF;
1035 colorval |= (val << pos);
1036 pos += 0x8;
1037 if (i == 2)
1039 if (*end != '\0')
1040 break;
1041 UNBLOCK_INPUT;
1042 XSETINT (ret, colorval);
1043 return ret;
1045 if (*end != '/')
1046 break;
1047 color = end + 1;
1050 /* I am not going to attempt to handle any of the CIE color schemes
1051 or TekHVC, since I don't know the algorithms for conversion to
1052 RGB. */
1054 /* If we fail to lookup the color name in w32_color_map, then check the
1055 colorname to see if it can be crudely approximated: If the X color
1056 ends in a number (e.g., "darkseagreen2"), strip the number and
1057 return the result of looking up the base color name. */
1058 ret = w32_color_map_lookup (colorname);
1059 if (NILP (ret))
1061 int len = strlen (colorname);
1063 if (isdigit (colorname[len - 1]))
1065 char *ptr, *approx = alloca (len + 1);
1067 strcpy (approx, colorname);
1068 ptr = &approx[len - 1];
1069 while (ptr > approx && isdigit (*ptr))
1070 *ptr-- = '\0';
1072 ret = w32_color_map_lookup (approx);
1076 UNBLOCK_INPUT;
1077 return ret;
1080 void
1081 w32_regenerate_palette (FRAME_PTR f)
1083 struct w32_palette_entry * list;
1084 LOGPALETTE * log_palette;
1085 HPALETTE new_palette;
1086 int i;
1088 /* don't bother trying to create palette if not supported */
1089 if (! FRAME_W32_DISPLAY_INFO (f)->has_palette)
1090 return;
1092 log_palette = (LOGPALETTE *)
1093 alloca (sizeof (LOGPALETTE) +
1094 FRAME_W32_DISPLAY_INFO (f)->num_colors * sizeof (PALETTEENTRY));
1095 log_palette->palVersion = 0x300;
1096 log_palette->palNumEntries = FRAME_W32_DISPLAY_INFO (f)->num_colors;
1098 list = FRAME_W32_DISPLAY_INFO (f)->color_list;
1099 for (i = 0;
1100 i < FRAME_W32_DISPLAY_INFO (f)->num_colors;
1101 i++, list = list->next)
1102 log_palette->palPalEntry[i] = list->entry;
1104 new_palette = CreatePalette (log_palette);
1106 enter_crit ();
1108 if (FRAME_W32_DISPLAY_INFO (f)->palette)
1109 DeleteObject (FRAME_W32_DISPLAY_INFO (f)->palette);
1110 FRAME_W32_DISPLAY_INFO (f)->palette = new_palette;
1112 /* Realize display palette and garbage all frames. */
1113 release_frame_dc (f, get_frame_dc (f));
1115 leave_crit ();
1118 #define W32_COLOR(pe) RGB (pe.peRed, pe.peGreen, pe.peBlue)
1119 #define SET_W32_COLOR(pe, color) \
1120 do \
1122 pe.peRed = GetRValue (color); \
1123 pe.peGreen = GetGValue (color); \
1124 pe.peBlue = GetBValue (color); \
1125 pe.peFlags = 0; \
1126 } while (0)
1128 #if 0
1129 /* Keep these around in case we ever want to track color usage. */
1130 void
1131 w32_map_color (FRAME_PTR f, COLORREF color)
1133 struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
1135 if (NILP (Vw32_enable_palette))
1136 return;
1138 /* check if color is already mapped */
1139 while (list)
1141 if (W32_COLOR (list->entry) == color)
1143 ++list->refcount;
1144 return;
1146 list = list->next;
1149 /* not already mapped, so add to list and recreate Windows palette */
1150 list = (struct w32_palette_entry *)
1151 xmalloc (sizeof (struct w32_palette_entry));
1152 SET_W32_COLOR (list->entry, color);
1153 list->refcount = 1;
1154 list->next = FRAME_W32_DISPLAY_INFO (f)->color_list;
1155 FRAME_W32_DISPLAY_INFO (f)->color_list = list;
1156 FRAME_W32_DISPLAY_INFO (f)->num_colors++;
1158 /* set flag that palette must be regenerated */
1159 FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
1162 void
1163 w32_unmap_color (FRAME_PTR f, COLORREF color)
1165 struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
1166 struct w32_palette_entry **prev = &FRAME_W32_DISPLAY_INFO (f)->color_list;
1168 if (NILP (Vw32_enable_palette))
1169 return;
1171 /* check if color is already mapped */
1172 while (list)
1174 if (W32_COLOR (list->entry) == color)
1176 if (--list->refcount == 0)
1178 *prev = list->next;
1179 xfree (list);
1180 FRAME_W32_DISPLAY_INFO (f)->num_colors--;
1181 break;
1183 else
1184 return;
1186 prev = &list->next;
1187 list = list->next;
1190 /* set flag that palette must be regenerated */
1191 FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
1193 #endif
1196 /* Gamma-correct COLOR on frame F. */
1198 void
1199 gamma_correct (f, color)
1200 struct frame *f;
1201 COLORREF *color;
1203 if (f->gamma)
1205 *color = PALETTERGB (
1206 pow (GetRValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
1207 pow (GetGValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
1208 pow (GetBValue (*color) / 255.0, f->gamma) * 255.0 + 0.5);
1213 /* Decide if color named COLOR is valid for the display associated with
1214 the selected frame; if so, return the rgb values in COLOR_DEF.
1215 If ALLOC is nonzero, allocate a new colormap cell. */
1218 w32_defined_color (f, color, color_def, alloc)
1219 FRAME_PTR f;
1220 char *color;
1221 XColor *color_def;
1222 int alloc;
1224 register Lisp_Object tem;
1225 COLORREF w32_color_ref;
1227 tem = x_to_w32_color (color);
1229 if (!NILP (tem))
1231 if (f)
1233 /* Apply gamma correction. */
1234 w32_color_ref = XUINT (tem);
1235 gamma_correct (f, &w32_color_ref);
1236 XSETINT (tem, w32_color_ref);
1239 /* Map this color to the palette if it is enabled. */
1240 if (!NILP (Vw32_enable_palette))
1242 struct w32_palette_entry * entry =
1243 one_w32_display_info.color_list;
1244 struct w32_palette_entry ** prev =
1245 &one_w32_display_info.color_list;
1247 /* check if color is already mapped */
1248 while (entry)
1250 if (W32_COLOR (entry->entry) == XUINT (tem))
1251 break;
1252 prev = &entry->next;
1253 entry = entry->next;
1256 if (entry == NULL && alloc)
1258 /* not already mapped, so add to list */
1259 entry = (struct w32_palette_entry *)
1260 xmalloc (sizeof (struct w32_palette_entry));
1261 SET_W32_COLOR (entry->entry, XUINT (tem));
1262 entry->next = NULL;
1263 *prev = entry;
1264 one_w32_display_info.num_colors++;
1266 /* set flag that palette must be regenerated */
1267 one_w32_display_info.regen_palette = TRUE;
1270 /* Ensure COLORREF value is snapped to nearest color in (default)
1271 palette by simulating the PALETTERGB macro. This works whether
1272 or not the display device has a palette. */
1273 w32_color_ref = XUINT (tem) | 0x2000000;
1275 color_def->pixel = w32_color_ref;
1276 color_def->red = GetRValue (w32_color_ref) * 256;
1277 color_def->green = GetGValue (w32_color_ref) * 256;
1278 color_def->blue = GetBValue (w32_color_ref) * 256;
1280 return 1;
1282 else
1284 return 0;
1288 /* Given a string ARG naming a color, compute a pixel value from it
1289 suitable for screen F.
1290 If F is not a color screen, return DEF (default) regardless of what
1291 ARG says. */
1294 x_decode_color (f, arg, def)
1295 FRAME_PTR f;
1296 Lisp_Object arg;
1297 int def;
1299 XColor cdef;
1301 CHECK_STRING (arg);
1303 if (strcmp (SDATA (arg), "black") == 0)
1304 return BLACK_PIX_DEFAULT (f);
1305 else if (strcmp (SDATA (arg), "white") == 0)
1306 return WHITE_PIX_DEFAULT (f);
1308 if ((FRAME_W32_DISPLAY_INFO (f)->n_planes * FRAME_W32_DISPLAY_INFO (f)->n_cbits) == 1)
1309 return def;
1311 /* w32_defined_color is responsible for coping with failures
1312 by looking for a near-miss. */
1313 if (w32_defined_color (f, SDATA (arg), &cdef, 1))
1314 return cdef.pixel;
1316 /* defined_color failed; return an ultimate default. */
1317 return def;
1322 /* Functions called only from `x_set_frame_param'
1323 to set individual parameters.
1325 If FRAME_W32_WINDOW (f) is 0,
1326 the frame is being created and its window does not exist yet.
1327 In that case, just record the parameter's new value
1328 in the standard place; do not attempt to change the window. */
1330 void
1331 x_set_foreground_color (f, arg, oldval)
1332 struct frame *f;
1333 Lisp_Object arg, oldval;
1335 struct w32_output *x = f->output_data.w32;
1336 PIX_TYPE fg, old_fg;
1338 fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1339 old_fg = FRAME_FOREGROUND_PIXEL (f);
1340 FRAME_FOREGROUND_PIXEL (f) = fg;
1342 if (FRAME_W32_WINDOW (f) != 0)
1344 if (x->cursor_pixel == old_fg)
1345 x->cursor_pixel = fg;
1347 update_face_from_frame_parameter (f, Qforeground_color, arg);
1348 if (FRAME_VISIBLE_P (f))
1349 redraw_frame (f);
1353 void
1354 x_set_background_color (f, arg, oldval)
1355 struct frame *f;
1356 Lisp_Object arg, oldval;
1358 FRAME_BACKGROUND_PIXEL (f)
1359 = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
1361 if (FRAME_W32_WINDOW (f) != 0)
1363 SetWindowLong (FRAME_W32_WINDOW (f), WND_BACKGROUND_INDEX,
1364 FRAME_BACKGROUND_PIXEL (f));
1366 update_face_from_frame_parameter (f, Qbackground_color, arg);
1368 if (FRAME_VISIBLE_P (f))
1369 redraw_frame (f);
1373 void
1374 x_set_mouse_color (f, arg, oldval)
1375 struct frame *f;
1376 Lisp_Object arg, oldval;
1378 Cursor cursor, nontext_cursor, mode_cursor, hand_cursor;
1379 int count;
1380 int mask_color;
1382 if (!EQ (Qnil, arg))
1383 f->output_data.w32->mouse_pixel
1384 = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1385 mask_color = FRAME_BACKGROUND_PIXEL (f);
1387 /* Don't let pointers be invisible. */
1388 if (mask_color == f->output_data.w32->mouse_pixel
1389 && mask_color == FRAME_BACKGROUND_PIXEL (f))
1390 f->output_data.w32->mouse_pixel = FRAME_FOREGROUND_PIXEL (f);
1392 #if 0 /* TODO : Mouse cursor customization. */
1393 BLOCK_INPUT;
1395 /* It's not okay to crash if the user selects a screwy cursor. */
1396 count = x_catch_errors (FRAME_W32_DISPLAY (f));
1398 if (!EQ (Qnil, Vx_pointer_shape))
1400 CHECK_NUMBER (Vx_pointer_shape);
1401 cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XINT (Vx_pointer_shape));
1403 else
1404 cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
1405 x_check_errors (FRAME_W32_DISPLAY (f), "bad text pointer cursor: %s");
1407 if (!EQ (Qnil, Vx_nontext_pointer_shape))
1409 CHECK_NUMBER (Vx_nontext_pointer_shape);
1410 nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1411 XINT (Vx_nontext_pointer_shape));
1413 else
1414 nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_left_ptr);
1415 x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
1417 if (!EQ (Qnil, Vx_hourglass_pointer_shape))
1419 CHECK_NUMBER (Vx_hourglass_pointer_shape);
1420 hourglass_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1421 XINT (Vx_hourglass_pointer_shape));
1423 else
1424 hourglass_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_watch);
1425 x_check_errors (FRAME_W32_DISPLAY (f), "bad busy pointer cursor: %s");
1427 x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
1428 if (!EQ (Qnil, Vx_mode_pointer_shape))
1430 CHECK_NUMBER (Vx_mode_pointer_shape);
1431 mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1432 XINT (Vx_mode_pointer_shape));
1434 else
1435 mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
1436 x_check_errors (FRAME_W32_DISPLAY (f), "bad modeline pointer cursor: %s");
1438 if (!EQ (Qnil, Vx_sensitive_text_pointer_shape))
1440 CHECK_NUMBER (Vx_sensitive_text_pointer_shape);
1441 hand_cursor
1442 = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1443 XINT (Vx_sensitive_text_pointer_shape));
1445 else
1446 hand_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_crosshair);
1448 if (!NILP (Vx_window_horizontal_drag_shape))
1450 CHECK_NUMBER (Vx_window_horizontal_drag_shape);
1451 horizontal_drag_cursor
1452 = XCreateFontCursor (FRAME_X_DISPLAY (f),
1453 XINT (Vx_window_horizontal_drag_shape));
1455 else
1456 horizontal_drag_cursor
1457 = XCreateFontCursor (FRAME_X_DISPLAY (f), XC_sb_h_double_arrow);
1459 /* Check and report errors with the above calls. */
1460 x_check_errors (FRAME_W32_DISPLAY (f), "can't set cursor shape: %s");
1461 x_uncatch_errors (FRAME_W32_DISPLAY (f), count);
1464 XColor fore_color, back_color;
1466 fore_color.pixel = f->output_data.w32->mouse_pixel;
1467 back_color.pixel = mask_color;
1468 XQueryColor (FRAME_W32_DISPLAY (f),
1469 DefaultColormap (FRAME_W32_DISPLAY (f),
1470 DefaultScreen (FRAME_W32_DISPLAY (f))),
1471 &fore_color);
1472 XQueryColor (FRAME_W32_DISPLAY (f),
1473 DefaultColormap (FRAME_W32_DISPLAY (f),
1474 DefaultScreen (FRAME_W32_DISPLAY (f))),
1475 &back_color);
1476 XRecolorCursor (FRAME_W32_DISPLAY (f), cursor,
1477 &fore_color, &back_color);
1478 XRecolorCursor (FRAME_W32_DISPLAY (f), nontext_cursor,
1479 &fore_color, &back_color);
1480 XRecolorCursor (FRAME_W32_DISPLAY (f), mode_cursor,
1481 &fore_color, &back_color);
1482 XRecolorCursor (FRAME_W32_DISPLAY (f), hand_cursor,
1483 &fore_color, &back_color);
1484 XRecolorCursor (FRAME_W32_DISPLAY (f), hourglass_cursor,
1485 &fore_color, &back_color);
1488 if (FRAME_W32_WINDOW (f) != 0)
1489 XDefineCursor (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), cursor);
1491 if (cursor != f->output_data.w32->text_cursor && f->output_data.w32->text_cursor != 0)
1492 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->text_cursor);
1493 f->output_data.w32->text_cursor = cursor;
1495 if (nontext_cursor != f->output_data.w32->nontext_cursor
1496 && f->output_data.w32->nontext_cursor != 0)
1497 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->nontext_cursor);
1498 f->output_data.w32->nontext_cursor = nontext_cursor;
1500 if (hourglass_cursor != f->output_data.w32->hourglass_cursor
1501 && f->output_data.w32->hourglass_cursor != 0)
1502 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->hourglass_cursor);
1503 f->output_data.w32->hourglass_cursor = hourglass_cursor;
1505 if (mode_cursor != f->output_data.w32->modeline_cursor
1506 && f->output_data.w32->modeline_cursor != 0)
1507 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->modeline_cursor);
1508 f->output_data.w32->modeline_cursor = mode_cursor;
1510 if (hand_cursor != f->output_data.w32->hand_cursor
1511 && f->output_data.w32->hand_cursor != 0)
1512 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->hand_cursor);
1513 f->output_data.w32->hand_cursor = hand_cursor;
1515 XFlush (FRAME_W32_DISPLAY (f));
1516 UNBLOCK_INPUT;
1518 update_face_from_frame_parameter (f, Qmouse_color, arg);
1519 #endif /* TODO */
1522 void
1523 x_set_cursor_color (f, arg, oldval)
1524 struct frame *f;
1525 Lisp_Object arg, oldval;
1527 unsigned long fore_pixel, pixel;
1529 if (!NILP (Vx_cursor_fore_pixel))
1530 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
1531 WHITE_PIX_DEFAULT (f));
1532 else
1533 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
1535 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1537 /* Make sure that the cursor color differs from the background color. */
1538 if (pixel == FRAME_BACKGROUND_PIXEL (f))
1540 pixel = f->output_data.w32->mouse_pixel;
1541 if (pixel == fore_pixel)
1542 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
1545 f->output_data.w32->cursor_foreground_pixel = fore_pixel;
1546 f->output_data.w32->cursor_pixel = pixel;
1548 if (FRAME_W32_WINDOW (f) != 0)
1550 BLOCK_INPUT;
1551 /* Update frame's cursor_gc. */
1552 f->output_data.w32->cursor_gc->foreground = fore_pixel;
1553 f->output_data.w32->cursor_gc->background = pixel;
1555 UNBLOCK_INPUT;
1557 if (FRAME_VISIBLE_P (f))
1559 x_update_cursor (f, 0);
1560 x_update_cursor (f, 1);
1564 update_face_from_frame_parameter (f, Qcursor_color, arg);
1567 /* Set the border-color of frame F to pixel value PIX.
1568 Note that this does not fully take effect if done before
1569 F has a window. */
1571 void
1572 x_set_border_pixel (f, pix)
1573 struct frame *f;
1574 int pix;
1577 f->output_data.w32->border_pixel = pix;
1579 if (FRAME_W32_WINDOW (f) != 0 && f->border_width > 0)
1581 if (FRAME_VISIBLE_P (f))
1582 redraw_frame (f);
1586 /* Set the border-color of frame F to value described by ARG.
1587 ARG can be a string naming a color.
1588 The border-color is used for the border that is drawn by the server.
1589 Note that this does not fully take effect if done before
1590 F has a window; it must be redone when the window is created. */
1592 void
1593 x_set_border_color (f, arg, oldval)
1594 struct frame *f;
1595 Lisp_Object arg, oldval;
1597 int pix;
1599 CHECK_STRING (arg);
1600 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1601 x_set_border_pixel (f, pix);
1602 update_face_from_frame_parameter (f, Qborder_color, arg);
1606 void
1607 x_set_cursor_type (f, arg, oldval)
1608 FRAME_PTR f;
1609 Lisp_Object arg, oldval;
1611 set_frame_cursor_types (f, arg);
1613 /* Make sure the cursor gets redrawn. */
1614 cursor_type_changed = 1;
1617 void
1618 x_set_icon_type (f, arg, oldval)
1619 struct frame *f;
1620 Lisp_Object arg, oldval;
1622 int result;
1624 if (NILP (arg) && NILP (oldval))
1625 return;
1627 if (STRINGP (arg) && STRINGP (oldval)
1628 && EQ (Fstring_equal (oldval, arg), Qt))
1629 return;
1631 if (SYMBOLP (arg) && SYMBOLP (oldval) && EQ (arg, oldval))
1632 return;
1634 BLOCK_INPUT;
1636 result = x_bitmap_icon (f, arg);
1637 if (result)
1639 UNBLOCK_INPUT;
1640 error ("No icon window available");
1643 UNBLOCK_INPUT;
1646 void
1647 x_set_icon_name (f, arg, oldval)
1648 struct frame *f;
1649 Lisp_Object arg, oldval;
1651 if (STRINGP (arg))
1653 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1654 return;
1656 else if (!NILP (arg) || NILP (oldval))
1657 return;
1659 f->icon_name = arg;
1661 #if 0
1662 if (f->output_data.w32->icon_bitmap != 0)
1663 return;
1665 BLOCK_INPUT;
1667 result = x_text_icon (f,
1668 (char *) SDATA ((!NILP (f->icon_name)
1669 ? f->icon_name
1670 : !NILP (f->title)
1671 ? f->title
1672 : f->name)));
1674 if (result)
1676 UNBLOCK_INPUT;
1677 error ("No icon window available");
1680 /* If the window was unmapped (and its icon was mapped),
1681 the new icon is not mapped, so map the window in its stead. */
1682 if (FRAME_VISIBLE_P (f))
1684 #ifdef USE_X_TOOLKIT
1685 XtPopup (f->output_data.w32->widget, XtGrabNone);
1686 #endif
1687 XMapWindow (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f));
1690 XFlush (FRAME_W32_DISPLAY (f));
1691 UNBLOCK_INPUT;
1692 #endif
1696 void
1697 x_set_menu_bar_lines (f, value, oldval)
1698 struct frame *f;
1699 Lisp_Object value, oldval;
1701 int nlines;
1702 int olines = FRAME_MENU_BAR_LINES (f);
1704 /* Right now, menu bars don't work properly in minibuf-only frames;
1705 most of the commands try to apply themselves to the minibuffer
1706 frame itself, and get an error because you can't switch buffers
1707 in or split the minibuffer window. */
1708 if (FRAME_MINIBUF_ONLY_P (f))
1709 return;
1711 if (INTEGERP (value))
1712 nlines = XINT (value);
1713 else
1714 nlines = 0;
1716 FRAME_MENU_BAR_LINES (f) = 0;
1717 if (nlines)
1718 FRAME_EXTERNAL_MENU_BAR (f) = 1;
1719 else
1721 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
1722 free_frame_menubar (f);
1723 FRAME_EXTERNAL_MENU_BAR (f) = 0;
1725 /* Adjust the frame size so that the client (text) dimensions
1726 remain the same. This depends on FRAME_EXTERNAL_MENU_BAR being
1727 set correctly. */
1728 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
1729 do_pending_window_change (0);
1731 adjust_glyphs (f);
1735 /* Set the number of lines used for the tool bar of frame F to VALUE.
1736 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
1737 is the old number of tool bar lines. This function changes the
1738 height of all windows on frame F to match the new tool bar height.
1739 The frame's height doesn't change. */
1741 void
1742 x_set_tool_bar_lines (f, value, oldval)
1743 struct frame *f;
1744 Lisp_Object value, oldval;
1746 int delta, nlines, root_height;
1747 Lisp_Object root_window;
1749 /* Treat tool bars like menu bars. */
1750 if (FRAME_MINIBUF_ONLY_P (f))
1751 return;
1753 /* Use VALUE only if an integer >= 0. */
1754 if (INTEGERP (value) && XINT (value) >= 0)
1755 nlines = XFASTINT (value);
1756 else
1757 nlines = 0;
1759 /* Make sure we redisplay all windows in this frame. */
1760 ++windows_or_buffers_changed;
1762 delta = nlines - FRAME_TOOL_BAR_LINES (f);
1764 /* Don't resize the tool-bar to more than we have room for. */
1765 root_window = FRAME_ROOT_WINDOW (f);
1766 root_height = WINDOW_TOTAL_LINES (XWINDOW (root_window));
1767 if (root_height - delta < 1)
1769 delta = root_height - 1;
1770 nlines = FRAME_TOOL_BAR_LINES (f) + delta;
1773 FRAME_TOOL_BAR_LINES (f) = nlines;
1774 change_window_heights (root_window, delta);
1775 adjust_glyphs (f);
1777 /* We also have to make sure that the internal border at the top of
1778 the frame, below the menu bar or tool bar, is redrawn when the
1779 tool bar disappears. This is so because the internal border is
1780 below the tool bar if one is displayed, but is below the menu bar
1781 if there isn't a tool bar. The tool bar draws into the area
1782 below the menu bar. */
1783 if (FRAME_W32_WINDOW (f) && FRAME_TOOL_BAR_LINES (f) == 0)
1785 clear_frame (f);
1786 clear_current_matrices (f);
1789 /* If the tool bar gets smaller, the internal border below it
1790 has to be cleared. It was formerly part of the display
1791 of the larger tool bar, and updating windows won't clear it. */
1792 if (delta < 0)
1794 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
1795 int width = FRAME_PIXEL_WIDTH (f);
1796 int y = nlines * FRAME_LINE_HEIGHT (f);
1798 BLOCK_INPUT;
1800 HDC hdc = get_frame_dc (f);
1801 w32_clear_area (f, hdc, 0, y, width, height);
1802 release_frame_dc (f, hdc);
1804 UNBLOCK_INPUT;
1806 if (WINDOWP (f->tool_bar_window))
1807 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
1812 /* Change the name of frame F to NAME. If NAME is nil, set F's name to
1813 w32_id_name.
1815 If EXPLICIT is non-zero, that indicates that lisp code is setting the
1816 name; if NAME is a string, set F's name to NAME and set
1817 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1819 If EXPLICIT is zero, that indicates that Emacs redisplay code is
1820 suggesting a new name, which lisp code should override; if
1821 F->explicit_name is set, ignore the new name; otherwise, set it. */
1823 void
1824 x_set_name (f, name, explicit)
1825 struct frame *f;
1826 Lisp_Object name;
1827 int explicit;
1829 /* Make sure that requests from lisp code override requests from
1830 Emacs redisplay code. */
1831 if (explicit)
1833 /* If we're switching from explicit to implicit, we had better
1834 update the mode lines and thereby update the title. */
1835 if (f->explicit_name && NILP (name))
1836 update_mode_lines = 1;
1838 f->explicit_name = ! NILP (name);
1840 else if (f->explicit_name)
1841 return;
1843 /* If NAME is nil, set the name to the w32_id_name. */
1844 if (NILP (name))
1846 /* Check for no change needed in this very common case
1847 before we do any consing. */
1848 if (!strcmp (FRAME_W32_DISPLAY_INFO (f)->w32_id_name,
1849 SDATA (f->name)))
1850 return;
1851 name = build_string (FRAME_W32_DISPLAY_INFO (f)->w32_id_name);
1853 else
1854 CHECK_STRING (name);
1856 /* Don't change the name if it's already NAME. */
1857 if (! NILP (Fstring_equal (name, f->name)))
1858 return;
1860 f->name = name;
1862 /* For setting the frame title, the title parameter should override
1863 the name parameter. */
1864 if (! NILP (f->title))
1865 name = f->title;
1867 if (FRAME_W32_WINDOW (f))
1869 if (STRING_MULTIBYTE (name))
1870 name = ENCODE_SYSTEM (name);
1872 BLOCK_INPUT;
1873 SetWindowText (FRAME_W32_WINDOW (f), SDATA (name));
1874 UNBLOCK_INPUT;
1878 /* This function should be called when the user's lisp code has
1879 specified a name for the frame; the name will override any set by the
1880 redisplay code. */
1881 void
1882 x_explicitly_set_name (f, arg, oldval)
1883 FRAME_PTR f;
1884 Lisp_Object arg, oldval;
1886 x_set_name (f, arg, 1);
1889 /* This function should be called by Emacs redisplay code to set the
1890 name; names set this way will never override names set by the user's
1891 lisp code. */
1892 void
1893 x_implicitly_set_name (f, arg, oldval)
1894 FRAME_PTR f;
1895 Lisp_Object arg, oldval;
1897 x_set_name (f, arg, 0);
1900 /* Change the title of frame F to NAME.
1901 If NAME is nil, use the frame name as the title. */
1903 void
1904 x_set_title (f, name, old_name)
1905 struct frame *f;
1906 Lisp_Object name, old_name;
1908 /* Don't change the title if it's already NAME. */
1909 if (EQ (name, f->title))
1910 return;
1912 update_mode_lines = 1;
1914 f->title = name;
1916 if (NILP (name))
1917 name = f->name;
1919 if (FRAME_W32_WINDOW (f))
1921 if (STRING_MULTIBYTE (name))
1922 name = ENCODE_SYSTEM (name);
1924 BLOCK_INPUT;
1925 SetWindowText (FRAME_W32_WINDOW (f), SDATA (name));
1926 UNBLOCK_INPUT;
1931 void x_set_scroll_bar_default_width (f)
1932 struct frame *f;
1934 int wid = FRAME_COLUMN_WIDTH (f);
1936 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = GetSystemMetrics (SM_CXVSCROLL);
1937 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) +
1938 wid - 1) / wid;
1942 /* Subroutines for creating a frame. */
1944 Cursor
1945 w32_load_cursor (LPCTSTR name)
1947 /* Try first to load cursor from application resource. */
1948 Cursor cursor = LoadImage ((HINSTANCE) GetModuleHandle (NULL),
1949 name, IMAGE_CURSOR, 0, 0,
1950 LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_SHARED);
1951 if (!cursor)
1953 /* Then try to load a shared predefined cursor. */
1954 cursor = LoadImage (NULL, name, IMAGE_CURSOR, 0, 0,
1955 LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_SHARED);
1957 return cursor;
1960 extern LRESULT CALLBACK w32_wnd_proc ();
1962 static BOOL
1963 w32_init_class (hinst)
1964 HINSTANCE hinst;
1966 WNDCLASS wc;
1968 wc.style = CS_HREDRAW | CS_VREDRAW;
1969 wc.lpfnWndProc = (WNDPROC) w32_wnd_proc;
1970 wc.cbClsExtra = 0;
1971 wc.cbWndExtra = WND_EXTRA_BYTES;
1972 wc.hInstance = hinst;
1973 wc.hIcon = LoadIcon (hinst, EMACS_CLASS);
1974 wc.hCursor = w32_load_cursor (IDC_ARROW);
1975 wc.hbrBackground = NULL; /* GetStockObject (WHITE_BRUSH); */
1976 wc.lpszMenuName = NULL;
1977 wc.lpszClassName = EMACS_CLASS;
1979 return (RegisterClass (&wc));
1982 static HWND
1983 w32_createscrollbar (f, bar)
1984 struct frame *f;
1985 struct scroll_bar * bar;
1987 return (CreateWindow ("SCROLLBAR", "", SBS_VERT | WS_CHILD | WS_VISIBLE,
1988 /* Position and size of scroll bar. */
1989 XINT (bar->left) + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
1990 XINT (bar->top),
1991 XINT (bar->width) - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
1992 XINT (bar->height),
1993 FRAME_W32_WINDOW (f),
1994 NULL,
1995 hinst,
1996 NULL));
1999 static void
2000 w32_createwindow (f)
2001 struct frame *f;
2003 HWND hwnd;
2004 RECT rect;
2005 Lisp_Object top = Qunbound;
2006 Lisp_Object left = Qunbound;
2007 struct w32_display_info *dpyinfo = &one_w32_display_info;
2009 rect.left = rect.top = 0;
2010 rect.right = FRAME_PIXEL_WIDTH (f);
2011 rect.bottom = FRAME_PIXEL_HEIGHT (f);
2013 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
2014 FRAME_EXTERNAL_MENU_BAR (f));
2016 /* Do first time app init */
2018 if (!hprevinst)
2020 w32_init_class (hinst);
2023 if (f->size_hint_flags & USPosition || f->size_hint_flags & PPosition)
2025 XSETINT (left, f->left_pos);
2026 XSETINT (top, f->top_pos);
2028 else if (EQ (left, Qunbound) && EQ (top, Qunbound))
2030 /* When called with RES_TYPE_NUMBER, w32_get_arg will return zero
2031 for anything that is not a number and is not Qunbound. */
2032 left = x_get_arg (dpyinfo, Qnil, Qleft, "left", "Left", RES_TYPE_NUMBER);
2033 top = x_get_arg (dpyinfo, Qnil, Qtop, "top", "Top", RES_TYPE_NUMBER);
2036 FRAME_W32_WINDOW (f) = hwnd
2037 = CreateWindow (EMACS_CLASS,
2038 f->namebuf,
2039 f->output_data.w32->dwStyle | WS_CLIPCHILDREN,
2040 EQ (left, Qunbound) ? CW_USEDEFAULT : XINT (left),
2041 EQ (top, Qunbound) ? CW_USEDEFAULT : XINT (top),
2042 rect.right - rect.left,
2043 rect.bottom - rect.top,
2044 NULL,
2045 NULL,
2046 hinst,
2047 NULL);
2049 if (hwnd)
2051 SetWindowLong (hwnd, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
2052 SetWindowLong (hwnd, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
2053 SetWindowLong (hwnd, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
2054 SetWindowLong (hwnd, WND_SCROLLBAR_INDEX, f->scroll_bar_actual_width);
2055 SetWindowLong (hwnd, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
2057 /* Enable drag-n-drop. */
2058 DragAcceptFiles (hwnd, TRUE);
2060 /* Do this to discard the default setting specified by our parent. */
2061 ShowWindow (hwnd, SW_HIDE);
2063 /* Update frame positions. */
2064 GetWindowRect (hwnd, &rect);
2065 f->left_pos = rect.left;
2066 f->top_pos = rect.top;
2070 static void
2071 my_post_msg (wmsg, hwnd, msg, wParam, lParam)
2072 W32Msg * wmsg;
2073 HWND hwnd;
2074 UINT msg;
2075 WPARAM wParam;
2076 LPARAM lParam;
2078 wmsg->msg.hwnd = hwnd;
2079 wmsg->msg.message = msg;
2080 wmsg->msg.wParam = wParam;
2081 wmsg->msg.lParam = lParam;
2082 wmsg->msg.time = GetMessageTime ();
2084 post_msg (wmsg);
2087 /* GetKeyState and MapVirtualKey on Windows 95 do not actually distinguish
2088 between left and right keys as advertised. We test for this
2089 support dynamically, and set a flag when the support is absent. If
2090 absent, we keep track of the left and right control and alt keys
2091 ourselves. This is particularly necessary on keyboards that rely
2092 upon the AltGr key, which is represented as having the left control
2093 and right alt keys pressed. For these keyboards, we need to know
2094 when the left alt key has been pressed in addition to the AltGr key
2095 so that we can properly support M-AltGr-key sequences (such as M-@
2096 on Swedish keyboards). */
2098 #define EMACS_LCONTROL 0
2099 #define EMACS_RCONTROL 1
2100 #define EMACS_LMENU 2
2101 #define EMACS_RMENU 3
2103 static int modifiers[4];
2104 static int modifiers_recorded;
2105 static int modifier_key_support_tested;
2107 static void
2108 test_modifier_support (unsigned int wparam)
2110 unsigned int l, r;
2112 if (wparam != VK_CONTROL && wparam != VK_MENU)
2113 return;
2114 if (wparam == VK_CONTROL)
2116 l = VK_LCONTROL;
2117 r = VK_RCONTROL;
2119 else
2121 l = VK_LMENU;
2122 r = VK_RMENU;
2124 if (!(GetKeyState (l) & 0x8000) && !(GetKeyState (r) & 0x8000))
2125 modifiers_recorded = 1;
2126 else
2127 modifiers_recorded = 0;
2128 modifier_key_support_tested = 1;
2131 static void
2132 record_keydown (unsigned int wparam, unsigned int lparam)
2134 int i;
2136 if (!modifier_key_support_tested)
2137 test_modifier_support (wparam);
2139 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
2140 return;
2142 if (wparam == VK_CONTROL)
2143 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
2144 else
2145 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
2147 modifiers[i] = 1;
2150 static void
2151 record_keyup (unsigned int wparam, unsigned int lparam)
2153 int i;
2155 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
2156 return;
2158 if (wparam == VK_CONTROL)
2159 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
2160 else
2161 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
2163 modifiers[i] = 0;
2166 /* Emacs can lose focus while a modifier key has been pressed. When
2167 it regains focus, be conservative and clear all modifiers since
2168 we cannot reconstruct the left and right modifier state. */
2169 static void
2170 reset_modifiers ()
2172 SHORT ctrl, alt;
2174 if (GetFocus () == NULL)
2175 /* Emacs doesn't have keyboard focus. Do nothing. */
2176 return;
2178 ctrl = GetAsyncKeyState (VK_CONTROL);
2179 alt = GetAsyncKeyState (VK_MENU);
2181 if (!(ctrl & 0x08000))
2182 /* Clear any recorded control modifier state. */
2183 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
2185 if (!(alt & 0x08000))
2186 /* Clear any recorded alt modifier state. */
2187 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
2189 /* Update the state of all modifier keys, because modifiers used in
2190 hot-key combinations can get stuck on if Emacs loses focus as a
2191 result of a hot-key being pressed. */
2193 BYTE keystate[256];
2195 #define CURRENT_STATE(key) ((GetAsyncKeyState (key) & 0x8000) >> 8)
2197 GetKeyboardState (keystate);
2198 keystate[VK_SHIFT] = CURRENT_STATE (VK_SHIFT);
2199 keystate[VK_CONTROL] = CURRENT_STATE (VK_CONTROL);
2200 keystate[VK_LCONTROL] = CURRENT_STATE (VK_LCONTROL);
2201 keystate[VK_RCONTROL] = CURRENT_STATE (VK_RCONTROL);
2202 keystate[VK_MENU] = CURRENT_STATE (VK_MENU);
2203 keystate[VK_LMENU] = CURRENT_STATE (VK_LMENU);
2204 keystate[VK_RMENU] = CURRENT_STATE (VK_RMENU);
2205 keystate[VK_LWIN] = CURRENT_STATE (VK_LWIN);
2206 keystate[VK_RWIN] = CURRENT_STATE (VK_RWIN);
2207 keystate[VK_APPS] = CURRENT_STATE (VK_APPS);
2208 SetKeyboardState (keystate);
2212 /* Synchronize modifier state with what is reported with the current
2213 keystroke. Even if we cannot distinguish between left and right
2214 modifier keys, we know that, if no modifiers are set, then neither
2215 the left or right modifier should be set. */
2216 static void
2217 sync_modifiers ()
2219 if (!modifiers_recorded)
2220 return;
2222 if (!(GetKeyState (VK_CONTROL) & 0x8000))
2223 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
2225 if (!(GetKeyState (VK_MENU) & 0x8000))
2226 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
2229 static int
2230 modifier_set (int vkey)
2232 if (vkey == VK_CAPITAL || vkey == VK_SCROLL)
2233 return (GetKeyState (vkey) & 0x1);
2234 if (!modifiers_recorded)
2235 return (GetKeyState (vkey) & 0x8000);
2237 switch (vkey)
2239 case VK_LCONTROL:
2240 return modifiers[EMACS_LCONTROL];
2241 case VK_RCONTROL:
2242 return modifiers[EMACS_RCONTROL];
2243 case VK_LMENU:
2244 return modifiers[EMACS_LMENU];
2245 case VK_RMENU:
2246 return modifiers[EMACS_RMENU];
2248 return (GetKeyState (vkey) & 0x8000);
2251 /* Convert between the modifier bits W32 uses and the modifier bits
2252 Emacs uses. */
2254 unsigned int
2255 w32_key_to_modifier (int key)
2257 Lisp_Object key_mapping;
2259 switch (key)
2261 case VK_LWIN:
2262 key_mapping = Vw32_lwindow_modifier;
2263 break;
2264 case VK_RWIN:
2265 key_mapping = Vw32_rwindow_modifier;
2266 break;
2267 case VK_APPS:
2268 key_mapping = Vw32_apps_modifier;
2269 break;
2270 case VK_SCROLL:
2271 key_mapping = Vw32_scroll_lock_modifier;
2272 break;
2273 default:
2274 key_mapping = Qnil;
2277 /* NB. This code runs in the input thread, asychronously to the lisp
2278 thread, so we must be careful to ensure access to lisp data is
2279 thread-safe. The following code is safe because the modifier
2280 variable values are updated atomically from lisp and symbols are
2281 not relocated by GC. Also, we don't have to worry about seeing GC
2282 markbits here. */
2283 if (EQ (key_mapping, Qhyper))
2284 return hyper_modifier;
2285 if (EQ (key_mapping, Qsuper))
2286 return super_modifier;
2287 if (EQ (key_mapping, Qmeta))
2288 return meta_modifier;
2289 if (EQ (key_mapping, Qalt))
2290 return alt_modifier;
2291 if (EQ (key_mapping, Qctrl))
2292 return ctrl_modifier;
2293 if (EQ (key_mapping, Qcontrol)) /* synonym for ctrl */
2294 return ctrl_modifier;
2295 if (EQ (key_mapping, Qshift))
2296 return shift_modifier;
2298 /* Don't generate any modifier if not explicitly requested. */
2299 return 0;
2302 static unsigned int
2303 w32_get_modifiers ()
2305 return ((modifier_set (VK_SHIFT) ? shift_modifier : 0) |
2306 (modifier_set (VK_CONTROL) ? ctrl_modifier : 0) |
2307 (modifier_set (VK_LWIN) ? w32_key_to_modifier (VK_LWIN) : 0) |
2308 (modifier_set (VK_RWIN) ? w32_key_to_modifier (VK_RWIN) : 0) |
2309 (modifier_set (VK_APPS) ? w32_key_to_modifier (VK_APPS) : 0) |
2310 (modifier_set (VK_SCROLL) ? w32_key_to_modifier (VK_SCROLL) : 0) |
2311 (modifier_set (VK_MENU) ?
2312 ((NILP (Vw32_alt_is_meta)) ? alt_modifier : meta_modifier) : 0));
2315 /* We map the VK_* modifiers into console modifier constants
2316 so that we can use the same routines to handle both console
2317 and window input. */
2319 static int
2320 construct_console_modifiers ()
2322 int mods;
2324 mods = 0;
2325 mods |= (modifier_set (VK_SHIFT)) ? SHIFT_PRESSED : 0;
2326 mods |= (modifier_set (VK_CAPITAL)) ? CAPSLOCK_ON : 0;
2327 mods |= (modifier_set (VK_SCROLL)) ? SCROLLLOCK_ON : 0;
2328 mods |= (modifier_set (VK_NUMLOCK)) ? NUMLOCK_ON : 0;
2329 mods |= (modifier_set (VK_LCONTROL)) ? LEFT_CTRL_PRESSED : 0;
2330 mods |= (modifier_set (VK_RCONTROL)) ? RIGHT_CTRL_PRESSED : 0;
2331 mods |= (modifier_set (VK_LMENU)) ? LEFT_ALT_PRESSED : 0;
2332 mods |= (modifier_set (VK_RMENU)) ? RIGHT_ALT_PRESSED : 0;
2333 mods |= (modifier_set (VK_LWIN)) ? LEFT_WIN_PRESSED : 0;
2334 mods |= (modifier_set (VK_RWIN)) ? RIGHT_WIN_PRESSED : 0;
2335 mods |= (modifier_set (VK_APPS)) ? APPS_PRESSED : 0;
2337 return mods;
2340 static int
2341 w32_get_key_modifiers (unsigned int wparam, unsigned int lparam)
2343 int mods;
2345 /* Convert to emacs modifiers. */
2346 mods = w32_kbd_mods_to_emacs (construct_console_modifiers (), wparam);
2348 return mods;
2351 unsigned int
2352 map_keypad_keys (unsigned int virt_key, unsigned int extended)
2354 if (virt_key < VK_CLEAR || virt_key > VK_DELETE)
2355 return virt_key;
2357 if (virt_key == VK_RETURN)
2358 return (extended ? VK_NUMPAD_ENTER : VK_RETURN);
2360 if (virt_key >= VK_PRIOR && virt_key <= VK_DOWN)
2361 return (!extended ? (VK_NUMPAD_PRIOR + (virt_key - VK_PRIOR)) : virt_key);
2363 if (virt_key == VK_INSERT || virt_key == VK_DELETE)
2364 return (!extended ? (VK_NUMPAD_INSERT + (virt_key - VK_INSERT)) : virt_key);
2366 if (virt_key == VK_CLEAR)
2367 return (!extended ? VK_NUMPAD_CLEAR : virt_key);
2369 return virt_key;
2372 /* List of special key combinations which w32 would normally capture,
2373 but Emacs should grab instead. Not directly visible to lisp, to
2374 simplify synchronization. Each item is an integer encoding a virtual
2375 key code and modifier combination to capture. */
2376 static Lisp_Object w32_grabbed_keys;
2378 #define HOTKEY(vk, mods) make_number (((vk) & 255) | ((mods) << 8))
2379 #define HOTKEY_ID(k) (XFASTINT (k) & 0xbfff)
2380 #define HOTKEY_VK_CODE(k) (XFASTINT (k) & 255)
2381 #define HOTKEY_MODIFIERS(k) (XFASTINT (k) >> 8)
2383 #define RAW_HOTKEY_ID(k) ((k) & 0xbfff)
2384 #define RAW_HOTKEY_VK_CODE(k) ((k) & 255)
2385 #define RAW_HOTKEY_MODIFIERS(k) ((k) >> 8)
2387 /* Register hot-keys for reserved key combinations when Emacs has
2388 keyboard focus, since this is the only way Emacs can receive key
2389 combinations like Alt-Tab which are used by the system. */
2391 static void
2392 register_hot_keys (hwnd)
2393 HWND hwnd;
2395 Lisp_Object keylist;
2397 /* Use CONSP, since we are called asynchronously. */
2398 for (keylist = w32_grabbed_keys; CONSP (keylist); keylist = XCDR (keylist))
2400 Lisp_Object key = XCAR (keylist);
2402 /* Deleted entries get set to nil. */
2403 if (!INTEGERP (key))
2404 continue;
2406 RegisterHotKey (hwnd, HOTKEY_ID (key),
2407 HOTKEY_MODIFIERS (key), HOTKEY_VK_CODE (key));
2411 static void
2412 unregister_hot_keys (hwnd)
2413 HWND hwnd;
2415 Lisp_Object keylist;
2417 for (keylist = w32_grabbed_keys; CONSP (keylist); keylist = XCDR (keylist))
2419 Lisp_Object key = XCAR (keylist);
2421 if (!INTEGERP (key))
2422 continue;
2424 UnregisterHotKey (hwnd, HOTKEY_ID (key));
2428 /* Main message dispatch loop. */
2430 static void
2431 w32_msg_pump (deferred_msg * msg_buf)
2433 MSG msg;
2434 int result;
2435 HWND focus_window;
2437 msh_mousewheel = RegisterWindowMessage (MSH_MOUSEWHEEL);
2439 while (GetMessage (&msg, NULL, 0, 0))
2441 if (msg.hwnd == NULL)
2443 switch (msg.message)
2445 case WM_NULL:
2446 /* Produced by complete_deferred_msg; just ignore. */
2447 break;
2448 case WM_EMACS_CREATEWINDOW:
2449 /* Initialize COM for this window. Even though we don't use it,
2450 some third party shell extensions can cause it to be used in
2451 system dialogs, which causes a crash if it is not initialized.
2452 This is a known bug in Windows, which was fixed long ago, but
2453 the patch for XP is not publically available until XP SP3,
2454 and older versions will never be patched. */
2455 CoInitialize (NULL);
2456 w32_createwindow ((struct frame *) msg.wParam);
2457 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2458 abort ();
2459 break;
2460 case WM_EMACS_SETLOCALE:
2461 SetThreadLocale (msg.wParam);
2462 /* Reply is not expected. */
2463 break;
2464 case WM_EMACS_SETKEYBOARDLAYOUT:
2465 result = (int) ActivateKeyboardLayout ((HKL) msg.wParam, 0);
2466 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
2467 result, 0))
2468 abort ();
2469 break;
2470 case WM_EMACS_REGISTER_HOT_KEY:
2471 focus_window = GetFocus ();
2472 if (focus_window != NULL)
2473 RegisterHotKey (focus_window,
2474 RAW_HOTKEY_ID (msg.wParam),
2475 RAW_HOTKEY_MODIFIERS (msg.wParam),
2476 RAW_HOTKEY_VK_CODE (msg.wParam));
2477 /* Reply is not expected. */
2478 break;
2479 case WM_EMACS_UNREGISTER_HOT_KEY:
2480 focus_window = GetFocus ();
2481 if (focus_window != NULL)
2482 UnregisterHotKey (focus_window, RAW_HOTKEY_ID (msg.wParam));
2483 /* Mark item as erased. NB: this code must be
2484 thread-safe. The next line is okay because the cons
2485 cell is never made into garbage and is not relocated by
2486 GC. */
2487 XSETCAR ((Lisp_Object) ((EMACS_INT) msg.lParam), Qnil);
2488 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2489 abort ();
2490 break;
2491 case WM_EMACS_TOGGLE_LOCK_KEY:
2493 int vk_code = (int) msg.wParam;
2494 int cur_state = (GetKeyState (vk_code) & 1);
2495 Lisp_Object new_state = (Lisp_Object) ((EMACS_INT) msg.lParam);
2497 /* NB: This code must be thread-safe. It is safe to
2498 call NILP because symbols are not relocated by GC,
2499 and pointer here is not touched by GC (so the markbit
2500 can't be set). Numbers are safe because they are
2501 immediate values. */
2502 if (NILP (new_state)
2503 || (NUMBERP (new_state)
2504 && ((XUINT (new_state)) & 1) != cur_state))
2506 one_w32_display_info.faked_key = vk_code;
2508 keybd_event ((BYTE) vk_code,
2509 (BYTE) MapVirtualKey (vk_code, 0),
2510 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2511 keybd_event ((BYTE) vk_code,
2512 (BYTE) MapVirtualKey (vk_code, 0),
2513 KEYEVENTF_EXTENDEDKEY | 0, 0);
2514 keybd_event ((BYTE) vk_code,
2515 (BYTE) MapVirtualKey (vk_code, 0),
2516 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2517 cur_state = !cur_state;
2519 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
2520 cur_state, 0))
2521 abort ();
2523 break;
2524 #ifdef MSG_DEBUG
2525 /* Broadcast messages make it here, so you need to be looking
2526 for something in particular for this to be useful. */
2527 default:
2528 DebPrint (("msg %x not expected by w32_msg_pump\n", msg.message));
2529 #endif
2532 else
2534 DispatchMessage (&msg);
2537 /* Exit nested loop when our deferred message has completed. */
2538 if (msg_buf->completed)
2539 break;
2543 deferred_msg * deferred_msg_head;
2545 static deferred_msg *
2546 find_deferred_msg (HWND hwnd, UINT msg)
2548 deferred_msg * item;
2550 /* Don't actually need synchronization for read access, since
2551 modification of single pointer is always atomic. */
2552 /* enter_crit (); */
2554 for (item = deferred_msg_head; item != NULL; item = item->next)
2555 if (item->w32msg.msg.hwnd == hwnd
2556 && item->w32msg.msg.message == msg)
2557 break;
2559 /* leave_crit (); */
2561 return item;
2564 static LRESULT
2565 send_deferred_msg (deferred_msg * msg_buf,
2566 HWND hwnd,
2567 UINT msg,
2568 WPARAM wParam,
2569 LPARAM lParam)
2571 /* Only input thread can send deferred messages. */
2572 if (GetCurrentThreadId () != dwWindowsThreadId)
2573 abort ();
2575 /* It is an error to send a message that is already deferred. */
2576 if (find_deferred_msg (hwnd, msg) != NULL)
2577 abort ();
2579 /* Enforced synchronization is not needed because this is the only
2580 function that alters deferred_msg_head, and the following critical
2581 section is guaranteed to only be serially reentered (since only the
2582 input thread can call us). */
2584 /* enter_crit (); */
2586 msg_buf->completed = 0;
2587 msg_buf->next = deferred_msg_head;
2588 deferred_msg_head = msg_buf;
2589 my_post_msg (&msg_buf->w32msg, hwnd, msg, wParam, lParam);
2591 /* leave_crit (); */
2593 /* Start a new nested message loop to process other messages until
2594 this one is completed. */
2595 w32_msg_pump (msg_buf);
2597 deferred_msg_head = msg_buf->next;
2599 return msg_buf->result;
2602 void
2603 complete_deferred_msg (HWND hwnd, UINT msg, LRESULT result)
2605 deferred_msg * msg_buf = find_deferred_msg (hwnd, msg);
2607 if (msg_buf == NULL)
2608 /* Message may have been cancelled, so don't abort. */
2609 return;
2611 msg_buf->result = result;
2612 msg_buf->completed = 1;
2614 /* Ensure input thread is woken so it notices the completion. */
2615 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
2618 static void
2619 cancel_all_deferred_msgs ()
2621 deferred_msg * item;
2623 /* Don't actually need synchronization for read access, since
2624 modification of single pointer is always atomic. */
2625 /* enter_crit (); */
2627 for (item = deferred_msg_head; item != NULL; item = item->next)
2629 item->result = 0;
2630 item->completed = 1;
2633 /* leave_crit (); */
2635 /* Ensure input thread is woken so it notices the completion. */
2636 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
2639 DWORD WINAPI
2640 w32_msg_worker (void *arg)
2642 MSG msg;
2643 deferred_msg dummy_buf;
2645 /* Ensure our message queue is created */
2647 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
2649 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2650 abort ();
2652 memset (&dummy_buf, 0, sizeof (dummy_buf));
2653 dummy_buf.w32msg.msg.hwnd = NULL;
2654 dummy_buf.w32msg.msg.message = WM_NULL;
2656 /* This is the initial message loop which should only exit when the
2657 application quits. */
2658 w32_msg_pump (&dummy_buf);
2660 return 0;
2663 static void
2664 signal_user_input ()
2666 /* Interrupt any lisp that wants to be interrupted by input. */
2667 if (!NILP (Vthrow_on_input))
2669 Vquit_flag = Vthrow_on_input;
2670 /* If we're inside a function that wants immediate quits,
2671 do it now. */
2672 if (immediate_quit && NILP (Vinhibit_quit))
2674 immediate_quit = 0;
2675 QUIT;
2681 static void
2682 post_character_message (hwnd, msg, wParam, lParam, modifiers)
2683 HWND hwnd;
2684 UINT msg;
2685 WPARAM wParam;
2686 LPARAM lParam;
2687 DWORD modifiers;
2690 W32Msg wmsg;
2692 wmsg.dwModifiers = modifiers;
2694 /* Detect quit_char and set quit-flag directly. Note that we
2695 still need to post a message to ensure the main thread will be
2696 woken up if blocked in sys_select, but we do NOT want to post
2697 the quit_char message itself (because it will usually be as if
2698 the user had typed quit_char twice). Instead, we post a dummy
2699 message that has no particular effect. */
2701 int c = wParam;
2702 if (isalpha (c) && wmsg.dwModifiers == ctrl_modifier)
2703 c = make_ctrl_char (c) & 0377;
2704 if (c == quit_char
2705 || (wmsg.dwModifiers == 0 &&
2706 w32_quit_key && wParam == w32_quit_key))
2708 Vquit_flag = Qt;
2710 /* The choice of message is somewhat arbitrary, as long as
2711 the main thread handler just ignores it. */
2712 msg = WM_NULL;
2714 /* Interrupt any blocking system calls. */
2715 signal_quit ();
2717 /* As a safety precaution, forcibly complete any deferred
2718 messages. This is a kludge, but I don't see any particularly
2719 clean way to handle the situation where a deferred message is
2720 "dropped" in the lisp thread, and will thus never be
2721 completed, eg. by the user trying to activate the menubar
2722 when the lisp thread is busy, and then typing C-g when the
2723 menubar doesn't open promptly (with the result that the
2724 menubar never responds at all because the deferred
2725 WM_INITMENU message is never completed). Another problem
2726 situation is when the lisp thread calls SendMessage (to send
2727 a window manager command) when a message has been deferred;
2728 the lisp thread gets blocked indefinitely waiting for the
2729 deferred message to be completed, which itself is waiting for
2730 the lisp thread to respond.
2732 Note that we don't want to block the input thread waiting for
2733 a reponse from the lisp thread (although that would at least
2734 solve the deadlock problem above), because we want to be able
2735 to receive C-g to interrupt the lisp thread. */
2736 cancel_all_deferred_msgs ();
2738 else
2739 signal_user_input ();
2742 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2745 /* Main window procedure */
2747 LRESULT CALLBACK
2748 w32_wnd_proc (hwnd, msg, wParam, lParam)
2749 HWND hwnd;
2750 UINT msg;
2751 WPARAM wParam;
2752 LPARAM lParam;
2754 struct frame *f;
2755 struct w32_display_info *dpyinfo = &one_w32_display_info;
2756 W32Msg wmsg;
2757 int windows_translate;
2758 int key;
2760 /* Note that it is okay to call x_window_to_frame, even though we are
2761 not running in the main lisp thread, because frame deletion
2762 requires the lisp thread to synchronize with this thread. Thus, if
2763 a frame struct is returned, it can be used without concern that the
2764 lisp thread might make it disappear while we are using it.
2766 NB. Walking the frame list in this thread is safe (as long as
2767 writes of Lisp_Object slots are atomic, which they are on Windows).
2768 Although delete-frame can destructively modify the frame list while
2769 we are walking it, a garbage collection cannot occur until after
2770 delete-frame has synchronized with this thread.
2772 It is also safe to use functions that make GDI calls, such as
2773 w32_clear_rect, because these functions must obtain a DC handle
2774 from the frame struct using get_frame_dc which is thread-aware. */
2776 switch (msg)
2778 case WM_ERASEBKGND:
2779 f = x_window_to_frame (dpyinfo, hwnd);
2780 if (f)
2782 HDC hdc = get_frame_dc (f);
2783 GetUpdateRect (hwnd, &wmsg.rect, FALSE);
2784 w32_clear_rect (f, hdc, &wmsg.rect);
2785 release_frame_dc (f, hdc);
2787 #if defined (W32_DEBUG_DISPLAY)
2788 DebPrint (("WM_ERASEBKGND (frame %p): erasing %d,%d-%d,%d\n",
2790 wmsg.rect.left, wmsg.rect.top,
2791 wmsg.rect.right, wmsg.rect.bottom));
2792 #endif /* W32_DEBUG_DISPLAY */
2794 return 1;
2795 case WM_PALETTECHANGED:
2796 /* ignore our own changes */
2797 if ((HWND)wParam != hwnd)
2799 f = x_window_to_frame (dpyinfo, hwnd);
2800 if (f)
2801 /* get_frame_dc will realize our palette and force all
2802 frames to be redrawn if needed. */
2803 release_frame_dc (f, get_frame_dc (f));
2805 return 0;
2806 case WM_PAINT:
2808 PAINTSTRUCT paintStruct;
2809 RECT update_rect;
2810 bzero (&update_rect, sizeof (update_rect));
2812 f = x_window_to_frame (dpyinfo, hwnd);
2813 if (f == 0)
2815 DebPrint (("WM_PAINT received for unknown window %p\n", hwnd));
2816 return 0;
2819 /* MSDN Docs say not to call BeginPaint if GetUpdateRect
2820 fails. Apparently this can happen under some
2821 circumstances. */
2822 if (GetUpdateRect (hwnd, &update_rect, FALSE) || !w32_strict_painting)
2824 enter_crit ();
2825 BeginPaint (hwnd, &paintStruct);
2827 /* The rectangles returned by GetUpdateRect and BeginPaint
2828 do not always match. Play it safe by assuming both areas
2829 are invalid. */
2830 UnionRect (&(wmsg.rect), &update_rect, &(paintStruct.rcPaint));
2832 #if defined (W32_DEBUG_DISPLAY)
2833 DebPrint (("WM_PAINT (frame %p): painting %d,%d-%d,%d\n",
2835 wmsg.rect.left, wmsg.rect.top,
2836 wmsg.rect.right, wmsg.rect.bottom));
2837 DebPrint ((" [update region is %d,%d-%d,%d]\n",
2838 update_rect.left, update_rect.top,
2839 update_rect.right, update_rect.bottom));
2840 #endif
2841 EndPaint (hwnd, &paintStruct);
2842 leave_crit ();
2844 /* Change the message type to prevent Windows from
2845 combining WM_PAINT messages in the Lisp thread's queue,
2846 since Windows assumes that each message queue is
2847 dedicated to one frame and does not bother checking
2848 that hwnd matches before combining them. */
2849 my_post_msg (&wmsg, hwnd, WM_EMACS_PAINT, wParam, lParam);
2851 return 0;
2854 /* If GetUpdateRect returns 0 (meaning there is no update
2855 region), assume the whole window needs to be repainted. */
2856 GetClientRect (hwnd, &wmsg.rect);
2857 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2858 return 0;
2861 case WM_INPUTLANGCHANGE:
2862 /* Inform lisp thread of keyboard layout changes. */
2863 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2865 /* Clear dead keys in the keyboard state; for simplicity only
2866 preserve modifier key states. */
2868 int i;
2869 BYTE keystate[256];
2871 GetKeyboardState (keystate);
2872 for (i = 0; i < 256; i++)
2873 if (1
2874 && i != VK_SHIFT
2875 && i != VK_LSHIFT
2876 && i != VK_RSHIFT
2877 && i != VK_CAPITAL
2878 && i != VK_NUMLOCK
2879 && i != VK_SCROLL
2880 && i != VK_CONTROL
2881 && i != VK_LCONTROL
2882 && i != VK_RCONTROL
2883 && i != VK_MENU
2884 && i != VK_LMENU
2885 && i != VK_RMENU
2886 && i != VK_LWIN
2887 && i != VK_RWIN)
2888 keystate[i] = 0;
2889 SetKeyboardState (keystate);
2891 goto dflt;
2893 case WM_HOTKEY:
2894 /* Synchronize hot keys with normal input. */
2895 PostMessage (hwnd, WM_KEYDOWN, HIWORD (lParam), 0);
2896 return (0);
2898 case WM_KEYUP:
2899 case WM_SYSKEYUP:
2900 record_keyup (wParam, lParam);
2901 goto dflt;
2903 case WM_KEYDOWN:
2904 case WM_SYSKEYDOWN:
2905 /* Ignore keystrokes we fake ourself; see below. */
2906 if (dpyinfo->faked_key == wParam)
2908 dpyinfo->faked_key = 0;
2909 /* Make sure TranslateMessage sees them though (as long as
2910 they don't produce WM_CHAR messages). This ensures that
2911 indicator lights are toggled promptly on Windows 9x, for
2912 example. */
2913 if (wParam < 256 && lispy_function_keys[wParam])
2915 windows_translate = 1;
2916 goto translate;
2918 return 0;
2921 /* Synchronize modifiers with current keystroke. */
2922 sync_modifiers ();
2923 record_keydown (wParam, lParam);
2924 wParam = map_keypad_keys (wParam, (lParam & 0x1000000L) != 0);
2926 windows_translate = 0;
2928 switch (wParam)
2930 case VK_LWIN:
2931 if (NILP (Vw32_pass_lwindow_to_system))
2933 /* Prevent system from acting on keyup (which opens the
2934 Start menu if no other key was pressed) by simulating a
2935 press of Space which we will ignore. */
2936 if (GetAsyncKeyState (wParam) & 1)
2938 if (NUMBERP (Vw32_phantom_key_code))
2939 key = XUINT (Vw32_phantom_key_code) & 255;
2940 else
2941 key = VK_SPACE;
2942 dpyinfo->faked_key = key;
2943 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
2946 if (!NILP (Vw32_lwindow_modifier))
2947 return 0;
2948 break;
2949 case VK_RWIN:
2950 if (NILP (Vw32_pass_rwindow_to_system))
2952 if (GetAsyncKeyState (wParam) & 1)
2954 if (NUMBERP (Vw32_phantom_key_code))
2955 key = XUINT (Vw32_phantom_key_code) & 255;
2956 else
2957 key = VK_SPACE;
2958 dpyinfo->faked_key = key;
2959 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
2962 if (!NILP (Vw32_rwindow_modifier))
2963 return 0;
2964 break;
2965 case VK_APPS:
2966 if (!NILP (Vw32_apps_modifier))
2967 return 0;
2968 break;
2969 case VK_MENU:
2970 if (NILP (Vw32_pass_alt_to_system))
2971 /* Prevent DefWindowProc from activating the menu bar if an
2972 Alt key is pressed and released by itself. */
2973 return 0;
2974 windows_translate = 1;
2975 break;
2976 case VK_CAPITAL:
2977 /* Decide whether to treat as modifier or function key. */
2978 if (NILP (Vw32_enable_caps_lock))
2979 goto disable_lock_key;
2980 windows_translate = 1;
2981 break;
2982 case VK_NUMLOCK:
2983 /* Decide whether to treat as modifier or function key. */
2984 if (NILP (Vw32_enable_num_lock))
2985 goto disable_lock_key;
2986 windows_translate = 1;
2987 break;
2988 case VK_SCROLL:
2989 /* Decide whether to treat as modifier or function key. */
2990 if (NILP (Vw32_scroll_lock_modifier))
2991 goto disable_lock_key;
2992 windows_translate = 1;
2993 break;
2994 disable_lock_key:
2995 /* Ensure the appropriate lock key state (and indicator light)
2996 remains in the same state. We do this by faking another
2997 press of the relevant key. Apparently, this really is the
2998 only way to toggle the state of the indicator lights. */
2999 dpyinfo->faked_key = wParam;
3000 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
3001 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
3002 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
3003 KEYEVENTF_EXTENDEDKEY | 0, 0);
3004 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
3005 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
3006 /* Ensure indicator lights are updated promptly on Windows 9x
3007 (TranslateMessage apparently does this), after forwarding
3008 input event. */
3009 post_character_message (hwnd, msg, wParam, lParam,
3010 w32_get_key_modifiers (wParam, lParam));
3011 windows_translate = 1;
3012 break;
3013 case VK_CONTROL:
3014 case VK_SHIFT:
3015 case VK_PROCESSKEY: /* Generated by IME. */
3016 windows_translate = 1;
3017 break;
3018 case VK_CANCEL:
3019 /* Windows maps Ctrl-Pause (aka Ctrl-Break) into VK_CANCEL,
3020 which is confusing for purposes of key binding; convert
3021 VK_CANCEL events into VK_PAUSE events. */
3022 wParam = VK_PAUSE;
3023 break;
3024 case VK_PAUSE:
3025 /* Windows maps Ctrl-NumLock into VK_PAUSE, which is confusing
3026 for purposes of key binding; convert these back into
3027 VK_NUMLOCK events, at least when we want to see NumLock key
3028 presses. (Note that there is never any possibility that
3029 VK_PAUSE with Ctrl really is C-Pause as per above.) */
3030 if (NILP (Vw32_enable_num_lock) && modifier_set (VK_CONTROL))
3031 wParam = VK_NUMLOCK;
3032 break;
3033 default:
3034 /* If not defined as a function key, change it to a WM_CHAR message. */
3035 if (wParam > 255 || !lispy_function_keys[wParam])
3037 DWORD modifiers = construct_console_modifiers ();
3039 if (!NILP (Vw32_recognize_altgr)
3040 && modifier_set (VK_LCONTROL) && modifier_set (VK_RMENU))
3042 /* Always let TranslateMessage handle AltGr key chords;
3043 for some reason, ToAscii doesn't always process AltGr
3044 chords correctly. */
3045 windows_translate = 1;
3047 else if ((modifiers & (~SHIFT_PRESSED & ~CAPSLOCK_ON)) != 0)
3049 /* Handle key chords including any modifiers other
3050 than shift directly, in order to preserve as much
3051 modifier information as possible. */
3052 if ('A' <= wParam && wParam <= 'Z')
3054 /* Don't translate modified alphabetic keystrokes,
3055 so the user doesn't need to constantly switch
3056 layout to type control or meta keystrokes when
3057 the normal layout translates alphabetic
3058 characters to non-ascii characters. */
3059 if (!modifier_set (VK_SHIFT))
3060 wParam += ('a' - 'A');
3061 msg = WM_CHAR;
3063 else
3065 /* Try to handle other keystrokes by determining the
3066 base character (ie. translating the base key plus
3067 shift modifier). */
3068 int add;
3069 int isdead = 0;
3070 KEY_EVENT_RECORD key;
3072 key.bKeyDown = TRUE;
3073 key.wRepeatCount = 1;
3074 key.wVirtualKeyCode = wParam;
3075 key.wVirtualScanCode = (lParam & 0xFF0000) >> 16;
3076 key.uChar.AsciiChar = 0;
3077 key.dwControlKeyState = modifiers;
3079 add = w32_kbd_patch_key (&key);
3080 /* 0 means an unrecognised keycode, negative means
3081 dead key. Ignore both. */
3082 while (--add >= 0)
3084 /* Forward asciified character sequence. */
3085 post_character_message
3086 (hwnd, WM_CHAR,
3087 (unsigned char) key.uChar.AsciiChar, lParam,
3088 w32_get_key_modifiers (wParam, lParam));
3089 w32_kbd_patch_key (&key);
3091 return 0;
3094 else
3096 /* Let TranslateMessage handle everything else. */
3097 windows_translate = 1;
3102 translate:
3103 if (windows_translate)
3105 MSG windows_msg = { hwnd, msg, wParam, lParam, 0, {0,0} };
3106 windows_msg.time = GetMessageTime ();
3107 TranslateMessage (&windows_msg);
3108 goto dflt;
3111 /* Fall through */
3113 case WM_SYSCHAR:
3114 case WM_CHAR:
3115 post_character_message (hwnd, msg, wParam, lParam,
3116 w32_get_key_modifiers (wParam, lParam));
3117 break;
3119 case WM_UNICHAR:
3120 /* WM_UNICHAR looks promising from the docs, but the exact
3121 circumstances in which TranslateMessage sends it is one of those
3122 Microsoft secret API things that EU and US courts are supposed
3123 to have put a stop to already. Spy++ shows it being sent to Notepad
3124 and other MS apps, but never to Emacs.
3126 Some third party IMEs send it in accordance with the official
3127 documentation though, so handle it here.
3129 UNICODE_NOCHAR is used to test for support for this message.
3130 TRUE indicates that the message is supported. */
3131 if (wParam == UNICODE_NOCHAR)
3132 return TRUE;
3135 W32Msg wmsg;
3136 wmsg.dwModifiers = w32_get_key_modifiers (wParam, lParam);
3137 signal_user_input ();
3138 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3140 break;
3142 case WM_IME_CHAR:
3143 /* If we can't get the IME result as unicode, use default processing,
3144 which will at least allow characters decodable in the system locale
3145 get through. */
3146 if (!get_composition_string_fn)
3147 goto dflt;
3149 else if (!ignore_ime_char)
3151 wchar_t * buffer;
3152 int size, i;
3153 W32Msg wmsg;
3154 HIMC context = get_ime_context_fn (hwnd);
3155 wmsg.dwModifiers = w32_get_key_modifiers (wParam, lParam);
3156 /* Get buffer size. */
3157 size = get_composition_string_fn (context, GCS_RESULTSTR, buffer, 0);
3158 buffer = alloca(size);
3159 size = get_composition_string_fn (context, GCS_RESULTSTR,
3160 buffer, size);
3161 signal_user_input ();
3162 for (i = 0; i < size / sizeof (wchar_t); i++)
3164 my_post_msg (&wmsg, hwnd, WM_UNICHAR, (WPARAM) buffer[i],
3165 lParam);
3167 /* Ignore the messages for the rest of the
3168 characters in the string that was output above. */
3169 ignore_ime_char = (size / sizeof (wchar_t)) - 1;
3171 else
3172 ignore_ime_char--;
3174 break;
3176 case WM_IME_ENDCOMPOSITION:
3177 ignore_ime_char = 0;
3178 goto dflt;
3180 /* Simulate middle mouse button events when left and right buttons
3181 are used together, but only if user has two button mouse. */
3182 case WM_LBUTTONDOWN:
3183 case WM_RBUTTONDOWN:
3184 if (w32_num_mouse_buttons > 2)
3185 goto handle_plain_button;
3188 int this = (msg == WM_LBUTTONDOWN) ? LMOUSE : RMOUSE;
3189 int other = (msg == WM_LBUTTONDOWN) ? RMOUSE : LMOUSE;
3191 if (button_state & this)
3192 return 0;
3194 if (button_state == 0)
3195 SetCapture (hwnd);
3197 button_state |= this;
3199 if (button_state & other)
3201 if (mouse_button_timer)
3203 KillTimer (hwnd, mouse_button_timer);
3204 mouse_button_timer = 0;
3206 /* Generate middle mouse event instead. */
3207 msg = WM_MBUTTONDOWN;
3208 button_state |= MMOUSE;
3210 else if (button_state & MMOUSE)
3212 /* Ignore button event if we've already generated a
3213 middle mouse down event. This happens if the
3214 user releases and press one of the two buttons
3215 after we've faked a middle mouse event. */
3216 return 0;
3218 else
3220 /* Flush out saved message. */
3221 post_msg (&saved_mouse_button_msg);
3223 wmsg.dwModifiers = w32_get_modifiers ();
3224 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3225 signal_user_input ();
3227 /* Clear message buffer. */
3228 saved_mouse_button_msg.msg.hwnd = 0;
3230 else
3232 /* Hold onto message for now. */
3233 mouse_button_timer =
3234 SetTimer (hwnd, MOUSE_BUTTON_ID,
3235 w32_mouse_button_tolerance, NULL);
3236 saved_mouse_button_msg.msg.hwnd = hwnd;
3237 saved_mouse_button_msg.msg.message = msg;
3238 saved_mouse_button_msg.msg.wParam = wParam;
3239 saved_mouse_button_msg.msg.lParam = lParam;
3240 saved_mouse_button_msg.msg.time = GetMessageTime ();
3241 saved_mouse_button_msg.dwModifiers = w32_get_modifiers ();
3244 return 0;
3246 case WM_LBUTTONUP:
3247 case WM_RBUTTONUP:
3248 if (w32_num_mouse_buttons > 2)
3249 goto handle_plain_button;
3252 int this = (msg == WM_LBUTTONUP) ? LMOUSE : RMOUSE;
3253 int other = (msg == WM_LBUTTONUP) ? RMOUSE : LMOUSE;
3255 if ((button_state & this) == 0)
3256 return 0;
3258 button_state &= ~this;
3260 if (button_state & MMOUSE)
3262 /* Only generate event when second button is released. */
3263 if ((button_state & other) == 0)
3265 msg = WM_MBUTTONUP;
3266 button_state &= ~MMOUSE;
3268 if (button_state) abort ();
3270 else
3271 return 0;
3273 else
3275 /* Flush out saved message if necessary. */
3276 if (saved_mouse_button_msg.msg.hwnd)
3278 post_msg (&saved_mouse_button_msg);
3281 wmsg.dwModifiers = w32_get_modifiers ();
3282 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3283 signal_user_input ();
3285 /* Always clear message buffer and cancel timer. */
3286 saved_mouse_button_msg.msg.hwnd = 0;
3287 KillTimer (hwnd, mouse_button_timer);
3288 mouse_button_timer = 0;
3290 if (button_state == 0)
3291 ReleaseCapture ();
3293 return 0;
3295 case WM_XBUTTONDOWN:
3296 case WM_XBUTTONUP:
3297 if (w32_pass_extra_mouse_buttons_to_system)
3298 goto dflt;
3299 /* else fall through and process them. */
3300 case WM_MBUTTONDOWN:
3301 case WM_MBUTTONUP:
3302 handle_plain_button:
3304 BOOL up;
3305 int button;
3307 /* Ignore middle and extra buttons as long as the menu is active. */
3308 f = x_window_to_frame (dpyinfo, hwnd);
3309 if (f && f->output_data.w32->menubar_active)
3310 return 0;
3312 if (parse_button (msg, HIWORD (wParam), &button, &up))
3314 if (up) ReleaseCapture ();
3315 else SetCapture (hwnd);
3316 button = (button == 0) ? LMOUSE :
3317 ((button == 1) ? MMOUSE : RMOUSE);
3318 if (up)
3319 button_state &= ~button;
3320 else
3321 button_state |= button;
3325 wmsg.dwModifiers = w32_get_modifiers ();
3326 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3327 signal_user_input ();
3329 /* Need to return true for XBUTTON messages, false for others,
3330 to indicate that we processed the message. */
3331 return (msg == WM_XBUTTONDOWN || msg == WM_XBUTTONUP);
3333 case WM_MOUSEMOVE:
3334 /* Ignore mouse movements as long as the menu is active. These
3335 movements are processed by the window manager anyway, and
3336 it's wrong to handle them as if they happened on the
3337 underlying frame. */
3338 f = x_window_to_frame (dpyinfo, hwnd);
3339 if (f && f->output_data.w32->menubar_active)
3340 return 0;
3342 /* If the mouse has just moved into the frame, start tracking
3343 it, so we will be notified when it leaves the frame. Mouse
3344 tracking only works under W98 and NT4 and later. On earlier
3345 versions, there is no way of telling when the mouse leaves the
3346 frame, so we just have to put up with help-echo and mouse
3347 highlighting remaining while the frame is not active. */
3348 if (track_mouse_event_fn && !track_mouse_window)
3350 TRACKMOUSEEVENT tme;
3351 tme.cbSize = sizeof (tme);
3352 tme.dwFlags = TME_LEAVE;
3353 tme.hwndTrack = hwnd;
3355 track_mouse_event_fn (&tme);
3356 track_mouse_window = hwnd;
3358 case WM_VSCROLL:
3359 if (w32_mouse_move_interval <= 0
3360 || (msg == WM_MOUSEMOVE && button_state == 0))
3362 wmsg.dwModifiers = w32_get_modifiers ();
3363 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3364 return 0;
3367 /* Hang onto mouse move and scroll messages for a bit, to avoid
3368 sending such events to Emacs faster than it can process them.
3369 If we get more events before the timer from the first message
3370 expires, we just replace the first message. */
3372 if (saved_mouse_move_msg.msg.hwnd == 0)
3373 mouse_move_timer =
3374 SetTimer (hwnd, MOUSE_MOVE_ID,
3375 w32_mouse_move_interval, NULL);
3377 /* Hold onto message for now. */
3378 saved_mouse_move_msg.msg.hwnd = hwnd;
3379 saved_mouse_move_msg.msg.message = msg;
3380 saved_mouse_move_msg.msg.wParam = wParam;
3381 saved_mouse_move_msg.msg.lParam = lParam;
3382 saved_mouse_move_msg.msg.time = GetMessageTime ();
3383 saved_mouse_move_msg.dwModifiers = w32_get_modifiers ();
3385 return 0;
3387 case WM_MOUSEWHEEL:
3388 case WM_DROPFILES:
3389 wmsg.dwModifiers = w32_get_modifiers ();
3390 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3391 signal_user_input ();
3392 return 0;
3394 case WM_APPCOMMAND:
3395 if (w32_pass_multimedia_buttons_to_system)
3396 goto dflt;
3397 /* Otherwise, pass to lisp, the same way we do with mousehwheel. */
3398 case WM_MOUSEHWHEEL:
3399 wmsg.dwModifiers = w32_get_modifiers ();
3400 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3401 signal_user_input ();
3402 /* Non-zero must be returned when WM_MOUSEHWHEEL messages are
3403 handled, to prevent the system trying to handle it by faking
3404 scroll bar events. */
3405 return 1;
3407 case WM_TIMER:
3408 /* Flush out saved messages if necessary. */
3409 if (wParam == mouse_button_timer)
3411 if (saved_mouse_button_msg.msg.hwnd)
3413 post_msg (&saved_mouse_button_msg);
3414 signal_user_input ();
3415 saved_mouse_button_msg.msg.hwnd = 0;
3417 KillTimer (hwnd, mouse_button_timer);
3418 mouse_button_timer = 0;
3420 else if (wParam == mouse_move_timer)
3422 if (saved_mouse_move_msg.msg.hwnd)
3424 post_msg (&saved_mouse_move_msg);
3425 saved_mouse_move_msg.msg.hwnd = 0;
3427 KillTimer (hwnd, mouse_move_timer);
3428 mouse_move_timer = 0;
3430 else if (wParam == menu_free_timer)
3432 KillTimer (hwnd, menu_free_timer);
3433 menu_free_timer = 0;
3434 f = x_window_to_frame (dpyinfo, hwnd);
3435 /* If a popup menu is active, don't wipe its strings. */
3436 if (menubar_in_use
3437 && current_popup_menu == NULL)
3439 /* Free memory used by owner-drawn and help-echo strings. */
3440 w32_free_menu_strings (hwnd);
3441 f->output_data.w32->menubar_active = 0;
3442 menubar_in_use = 0;
3445 else if (wParam == hourglass_timer)
3447 KillTimer (hwnd, hourglass_timer);
3448 hourglass_timer = 0;
3449 w32_show_hourglass (x_window_to_frame (dpyinfo, hwnd));
3451 return 0;
3453 case WM_NCACTIVATE:
3454 /* Windows doesn't send us focus messages when putting up and
3455 taking down a system popup dialog as for Ctrl-Alt-Del on Windows 95.
3456 The only indication we get that something happened is receiving
3457 this message afterwards. So this is a good time to reset our
3458 keyboard modifiers' state. */
3459 reset_modifiers ();
3460 goto dflt;
3462 case WM_INITMENU:
3463 button_state = 0;
3464 ReleaseCapture ();
3465 /* We must ensure menu bar is fully constructed and up to date
3466 before allowing user interaction with it. To achieve this
3467 we send this message to the lisp thread and wait for a
3468 reply (whose value is not actually needed) to indicate that
3469 the menu bar is now ready for use, so we can now return.
3471 To remain responsive in the meantime, we enter a nested message
3472 loop that can process all other messages.
3474 However, we skip all this if the message results from calling
3475 TrackPopupMenu - in fact, we must NOT attempt to send the lisp
3476 thread a message because it is blocked on us at this point. We
3477 set menubar_active before calling TrackPopupMenu to indicate
3478 this (there is no possibility of confusion with real menubar
3479 being active). */
3481 f = x_window_to_frame (dpyinfo, hwnd);
3482 if (f
3483 && (f->output_data.w32->menubar_active
3484 /* We can receive this message even in the absence of a
3485 menubar (ie. when the system menu is activated) - in this
3486 case we do NOT want to forward the message, otherwise it
3487 will cause the menubar to suddenly appear when the user
3488 had requested it to be turned off! */
3489 || f->output_data.w32->menubar_widget == NULL))
3490 return 0;
3493 deferred_msg msg_buf;
3495 /* Detect if message has already been deferred; in this case
3496 we cannot return any sensible value to ignore this. */
3497 if (find_deferred_msg (hwnd, msg) != NULL)
3498 abort ();
3500 menubar_in_use = 1;
3502 return send_deferred_msg (&msg_buf, hwnd, msg, wParam, lParam);
3505 case WM_EXITMENULOOP:
3506 f = x_window_to_frame (dpyinfo, hwnd);
3508 /* If a menu is still active, check again after a short delay,
3509 since Windows often (always?) sends the WM_EXITMENULOOP
3510 before the corresponding WM_COMMAND message.
3511 Don't do this if a popup menu is active, since it is only
3512 menubar menus that require cleaning up in this way.
3514 if (f && menubar_in_use && current_popup_menu == NULL)
3515 menu_free_timer = SetTimer (hwnd, MENU_FREE_ID, MENU_FREE_DELAY, NULL);
3517 /* If hourglass cursor should be displayed, display it now. */
3518 if (f && f->output_data.w32->hourglass_p)
3519 SetCursor (f->output_data.w32->hourglass_cursor);
3521 goto dflt;
3523 case WM_MENUSELECT:
3524 /* Direct handling of help_echo in menus. Should be safe now
3525 that we generate the help_echo by placing a help event in the
3526 keyboard buffer. */
3528 HMENU menu = (HMENU) lParam;
3529 UINT menu_item = (UINT) LOWORD (wParam);
3530 UINT flags = (UINT) HIWORD (wParam);
3532 w32_menu_display_help (hwnd, menu, menu_item, flags);
3534 return 0;
3536 case WM_MEASUREITEM:
3537 f = x_window_to_frame (dpyinfo, hwnd);
3538 if (f)
3540 MEASUREITEMSTRUCT * pMis = (MEASUREITEMSTRUCT *) lParam;
3542 if (pMis->CtlType == ODT_MENU)
3544 /* Work out dimensions for popup menu titles. */
3545 char * title = (char *) pMis->itemData;
3546 HDC hdc = GetDC (hwnd);
3547 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
3548 LOGFONT menu_logfont;
3549 HFONT old_font;
3550 SIZE size;
3552 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
3553 menu_logfont.lfWeight = FW_BOLD;
3554 menu_font = CreateFontIndirect (&menu_logfont);
3555 old_font = SelectObject (hdc, menu_font);
3557 pMis->itemHeight = GetSystemMetrics (SM_CYMENUSIZE);
3558 if (title)
3560 if (unicode_append_menu)
3561 GetTextExtentPoint32W (hdc, (WCHAR *) title,
3562 wcslen ((WCHAR *) title),
3563 &size);
3564 else
3565 GetTextExtentPoint32 (hdc, title, strlen (title), &size);
3567 pMis->itemWidth = size.cx;
3568 if (pMis->itemHeight < size.cy)
3569 pMis->itemHeight = size.cy;
3571 else
3572 pMis->itemWidth = 0;
3574 SelectObject (hdc, old_font);
3575 DeleteObject (menu_font);
3576 ReleaseDC (hwnd, hdc);
3577 return TRUE;
3580 return 0;
3582 case WM_DRAWITEM:
3583 f = x_window_to_frame (dpyinfo, hwnd);
3584 if (f)
3586 DRAWITEMSTRUCT * pDis = (DRAWITEMSTRUCT *) lParam;
3588 if (pDis->CtlType == ODT_MENU)
3590 /* Draw popup menu title. */
3591 char * title = (char *) pDis->itemData;
3592 if (title)
3594 HDC hdc = pDis->hDC;
3595 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
3596 LOGFONT menu_logfont;
3597 HFONT old_font;
3599 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
3600 menu_logfont.lfWeight = FW_BOLD;
3601 menu_font = CreateFontIndirect (&menu_logfont);
3602 old_font = SelectObject (hdc, menu_font);
3604 /* Always draw title as if not selected. */
3605 if (unicode_append_menu)
3606 ExtTextOutW (hdc,
3607 pDis->rcItem.left
3608 + GetSystemMetrics (SM_CXMENUCHECK),
3609 pDis->rcItem.top,
3610 ETO_OPAQUE, &pDis->rcItem,
3611 (WCHAR *) title,
3612 wcslen ((WCHAR *) title), NULL);
3613 else
3614 ExtTextOut (hdc,
3615 pDis->rcItem.left
3616 + GetSystemMetrics (SM_CXMENUCHECK),
3617 pDis->rcItem.top,
3618 ETO_OPAQUE, &pDis->rcItem,
3619 title, strlen (title), NULL);
3621 SelectObject (hdc, old_font);
3622 DeleteObject (menu_font);
3624 return TRUE;
3627 return 0;
3629 #if 0
3630 /* Still not right - can't distinguish between clicks in the
3631 client area of the frame from clicks forwarded from the scroll
3632 bars - may have to hook WM_NCHITTEST to remember the mouse
3633 position and then check if it is in the client area ourselves. */
3634 case WM_MOUSEACTIVATE:
3635 /* Discard the mouse click that activates a frame, allowing the
3636 user to click anywhere without changing point (or worse!).
3637 Don't eat mouse clicks on scrollbars though!! */
3638 if (LOWORD (lParam) == HTCLIENT )
3639 return MA_ACTIVATEANDEAT;
3640 goto dflt;
3641 #endif
3643 case WM_MOUSELEAVE:
3644 /* No longer tracking mouse. */
3645 track_mouse_window = NULL;
3647 case WM_ACTIVATEAPP:
3648 case WM_ACTIVATE:
3649 case WM_WINDOWPOSCHANGED:
3650 case WM_SHOWWINDOW:
3651 /* Inform lisp thread that a frame might have just been obscured
3652 or exposed, so should recheck visibility of all frames. */
3653 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3654 goto dflt;
3656 case WM_SETFOCUS:
3657 dpyinfo->faked_key = 0;
3658 reset_modifiers ();
3659 register_hot_keys (hwnd);
3660 goto command;
3661 case WM_KILLFOCUS:
3662 unregister_hot_keys (hwnd);
3663 button_state = 0;
3664 ReleaseCapture ();
3665 /* Relinquish the system caret. */
3666 if (w32_system_caret_hwnd)
3668 w32_visible_system_caret_hwnd = NULL;
3669 w32_system_caret_hwnd = NULL;
3670 DestroyCaret ();
3672 goto command;
3673 case WM_COMMAND:
3674 menubar_in_use = 0;
3675 f = x_window_to_frame (dpyinfo, hwnd);
3676 if (f && HIWORD (wParam) == 0)
3678 if (menu_free_timer)
3680 KillTimer (hwnd, menu_free_timer);
3681 menu_free_timer = 0;
3684 case WM_MOVE:
3685 case WM_SIZE:
3686 command:
3687 wmsg.dwModifiers = w32_get_modifiers ();
3688 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3689 goto dflt;
3691 case WM_DESTROY:
3692 CoUninitialize ();
3693 return 0;
3695 case WM_CLOSE:
3696 wmsg.dwModifiers = w32_get_modifiers ();
3697 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3698 return 0;
3700 case WM_WINDOWPOSCHANGING:
3701 /* Don't restrict the sizing of tip frames. */
3702 if (hwnd == tip_window)
3703 return 0;
3705 WINDOWPLACEMENT wp;
3706 LPWINDOWPOS lppos = (WINDOWPOS *) lParam;
3708 wp.length = sizeof (WINDOWPLACEMENT);
3709 GetWindowPlacement (hwnd, &wp);
3711 if (wp.showCmd != SW_SHOWMINIMIZED && (lppos->flags & SWP_NOSIZE) == 0)
3713 RECT rect;
3714 int wdiff;
3715 int hdiff;
3716 DWORD font_width;
3717 DWORD line_height;
3718 DWORD internal_border;
3719 DWORD scrollbar_extra;
3720 RECT wr;
3722 wp.length = sizeof (wp);
3723 GetWindowRect (hwnd, &wr);
3725 enter_crit ();
3727 font_width = GetWindowLong (hwnd, WND_FONTWIDTH_INDEX);
3728 line_height = GetWindowLong (hwnd, WND_LINEHEIGHT_INDEX);
3729 internal_border = GetWindowLong (hwnd, WND_BORDER_INDEX);
3730 scrollbar_extra = GetWindowLong (hwnd, WND_SCROLLBAR_INDEX);
3732 leave_crit ();
3734 memset (&rect, 0, sizeof (rect));
3735 AdjustWindowRect (&rect, GetWindowLong (hwnd, GWL_STYLE),
3736 GetMenu (hwnd) != NULL);
3738 /* Force width and height of client area to be exact
3739 multiples of the character cell dimensions. */
3740 wdiff = (lppos->cx - (rect.right - rect.left)
3741 - 2 * internal_border - scrollbar_extra)
3742 % font_width;
3743 hdiff = (lppos->cy - (rect.bottom - rect.top)
3744 - 2 * internal_border)
3745 % line_height;
3747 if (wdiff || hdiff)
3749 /* For right/bottom sizing we can just fix the sizes.
3750 However for top/left sizing we will need to fix the X
3751 and Y positions as well. */
3753 int cx_mintrack = GetSystemMetrics (SM_CXMINTRACK);
3754 int cy_mintrack = GetSystemMetrics (SM_CYMINTRACK);
3756 lppos->cx = max (lppos->cx - wdiff, cx_mintrack);
3757 lppos->cy = max (lppos->cy - hdiff, cy_mintrack);
3759 if (wp.showCmd != SW_SHOWMAXIMIZED
3760 && (lppos->flags & SWP_NOMOVE) == 0)
3762 if (lppos->x != wr.left || lppos->y != wr.top)
3764 lppos->x += wdiff;
3765 lppos->y += hdiff;
3767 else
3769 lppos->flags |= SWP_NOMOVE;
3773 return 0;
3778 goto dflt;
3780 case WM_GETMINMAXINFO:
3781 /* Hack to allow resizing the Emacs frame above the screen size.
3782 Note that Windows 9x limits coordinates to 16-bits. */
3783 ((LPMINMAXINFO) lParam)->ptMaxTrackSize.x = 32767;
3784 ((LPMINMAXINFO) lParam)->ptMaxTrackSize.y = 32767;
3785 return 0;
3787 case WM_SETCURSOR:
3788 if (LOWORD (lParam) == HTCLIENT)
3790 f = x_window_to_frame (dpyinfo, hwnd);
3791 if (f->output_data.w32->hourglass_p && !menubar_in_use
3792 && !current_popup_menu)
3793 SetCursor (f->output_data.w32->hourglass_cursor);
3794 else
3795 SetCursor (f->output_data.w32->current_cursor);
3796 return 0;
3798 goto dflt;
3800 case WM_EMACS_SETCURSOR:
3802 Cursor cursor = (Cursor) wParam;
3803 f = x_window_to_frame (dpyinfo, hwnd);
3804 if (f && cursor)
3806 f->output_data.w32->current_cursor = cursor;
3807 if (!f->output_data.w32->hourglass_p)
3808 SetCursor (cursor);
3810 return 0;
3813 case WM_EMACS_CREATESCROLLBAR:
3814 return (LRESULT) w32_createscrollbar ((struct frame *) wParam,
3815 (struct scroll_bar *) lParam);
3817 case WM_EMACS_SHOWWINDOW:
3818 return ShowWindow ((HWND) wParam, (WPARAM) lParam);
3820 case WM_EMACS_SETFOREGROUND:
3822 HWND foreground_window;
3823 DWORD foreground_thread, retval;
3825 /* On NT 5.0, and apparently Windows 98, it is necessary to
3826 attach to the thread that currently has focus in order to
3827 pull the focus away from it. */
3828 foreground_window = GetForegroundWindow ();
3829 foreground_thread = GetWindowThreadProcessId (foreground_window, NULL);
3830 if (!foreground_window
3831 || foreground_thread == GetCurrentThreadId ()
3832 || !AttachThreadInput (GetCurrentThreadId (),
3833 foreground_thread, TRUE))
3834 foreground_thread = 0;
3836 retval = SetForegroundWindow ((HWND) wParam);
3838 /* Detach from the previous foreground thread. */
3839 if (foreground_thread)
3840 AttachThreadInput (GetCurrentThreadId (),
3841 foreground_thread, FALSE);
3843 return retval;
3846 case WM_EMACS_SETWINDOWPOS:
3848 WINDOWPOS * pos = (WINDOWPOS *) wParam;
3849 return SetWindowPos (hwnd, pos->hwndInsertAfter,
3850 pos->x, pos->y, pos->cx, pos->cy, pos->flags);
3853 case WM_EMACS_DESTROYWINDOW:
3854 DragAcceptFiles ((HWND) wParam, FALSE);
3855 return DestroyWindow ((HWND) wParam);
3857 case WM_EMACS_HIDE_CARET:
3858 return HideCaret (hwnd);
3860 case WM_EMACS_SHOW_CARET:
3861 return ShowCaret (hwnd);
3863 case WM_EMACS_DESTROY_CARET:
3864 w32_system_caret_hwnd = NULL;
3865 w32_visible_system_caret_hwnd = NULL;
3866 return DestroyCaret ();
3868 case WM_EMACS_TRACK_CARET:
3869 /* If there is currently no system caret, create one. */
3870 if (w32_system_caret_hwnd == NULL)
3872 /* Use the default caret width, and avoid changing it
3873 unneccesarily, as it confuses screen reader software. */
3874 w32_system_caret_hwnd = hwnd;
3875 CreateCaret (hwnd, NULL, 0,
3876 w32_system_caret_height);
3879 if (!SetCaretPos (w32_system_caret_x, w32_system_caret_y))
3880 return 0;
3881 /* Ensure visible caret gets turned on when requested. */
3882 else if (w32_use_visible_system_caret
3883 && w32_visible_system_caret_hwnd != hwnd)
3885 w32_visible_system_caret_hwnd = hwnd;
3886 return ShowCaret (hwnd);
3888 /* Ensure visible caret gets turned off when requested. */
3889 else if (!w32_use_visible_system_caret
3890 && w32_visible_system_caret_hwnd)
3892 w32_visible_system_caret_hwnd = NULL;
3893 return HideCaret (hwnd);
3895 else
3896 return 1;
3898 case WM_EMACS_TRACKPOPUPMENU:
3900 UINT flags;
3901 POINT *pos;
3902 int retval;
3903 pos = (POINT *)lParam;
3904 flags = TPM_CENTERALIGN;
3905 if (button_state & LMOUSE)
3906 flags |= TPM_LEFTBUTTON;
3907 else if (button_state & RMOUSE)
3908 flags |= TPM_RIGHTBUTTON;
3910 /* Remember we did a SetCapture on the initial mouse down event,
3911 so for safety, we make sure the capture is cancelled now. */
3912 ReleaseCapture ();
3913 button_state = 0;
3915 /* Use menubar_active to indicate that WM_INITMENU is from
3916 TrackPopupMenu below, and should be ignored. */
3917 f = x_window_to_frame (dpyinfo, hwnd);
3918 if (f)
3919 f->output_data.w32->menubar_active = 1;
3921 if (TrackPopupMenu ((HMENU)wParam, flags, pos->x, pos->y,
3922 0, hwnd, NULL))
3924 MSG amsg;
3925 /* Eat any mouse messages during popupmenu */
3926 while (PeekMessage (&amsg, hwnd, WM_MOUSEFIRST, WM_MOUSELAST,
3927 PM_REMOVE));
3928 /* Get the menu selection, if any */
3929 if (PeekMessage (&amsg, hwnd, WM_COMMAND, WM_COMMAND, PM_REMOVE))
3931 retval = LOWORD (amsg.wParam);
3933 else
3935 retval = 0;
3938 else
3940 retval = -1;
3943 return retval;
3946 default:
3947 /* Check for messages registered at runtime. */
3948 if (msg == msh_mousewheel)
3950 wmsg.dwModifiers = w32_get_modifiers ();
3951 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3952 signal_user_input ();
3953 return 0;
3956 dflt:
3957 return DefWindowProc (hwnd, msg, wParam, lParam);
3961 /* The most common default return code for handled messages is 0. */
3962 return 0;
3965 static void
3966 my_create_window (f)
3967 struct frame * f;
3969 MSG msg;
3971 if (!PostThreadMessage (dwWindowsThreadId, WM_EMACS_CREATEWINDOW, (WPARAM)f, 0))
3972 abort ();
3973 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
3977 /* Create a tooltip window. Unlike my_create_window, we do not do this
3978 indirectly via the Window thread, as we do not need to process Window
3979 messages for the tooltip. Creating tooltips indirectly also creates
3980 deadlocks when tooltips are created for menu items. */
3981 static void
3982 my_create_tip_window (f)
3983 struct frame *f;
3985 RECT rect;
3987 rect.left = rect.top = 0;
3988 rect.right = FRAME_PIXEL_WIDTH (f);
3989 rect.bottom = FRAME_PIXEL_HEIGHT (f);
3991 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
3992 FRAME_EXTERNAL_MENU_BAR (f));
3994 tip_window = FRAME_W32_WINDOW (f)
3995 = CreateWindow (EMACS_CLASS,
3996 f->namebuf,
3997 f->output_data.w32->dwStyle,
3998 f->left_pos,
3999 f->top_pos,
4000 rect.right - rect.left,
4001 rect.bottom - rect.top,
4002 FRAME_W32_WINDOW (SELECTED_FRAME ()), /* owner */
4003 NULL,
4004 hinst,
4005 NULL);
4007 if (tip_window)
4009 SetWindowLong (tip_window, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
4010 SetWindowLong (tip_window, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
4011 SetWindowLong (tip_window, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
4012 SetWindowLong (tip_window, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
4014 /* Tip frames have no scrollbars. */
4015 SetWindowLong (tip_window, WND_SCROLLBAR_INDEX, 0);
4017 /* Do this to discard the default setting specified by our parent. */
4018 ShowWindow (tip_window, SW_HIDE);
4023 /* Create and set up the w32 window for frame F. */
4025 static void
4026 w32_window (f, window_prompting, minibuffer_only)
4027 struct frame *f;
4028 long window_prompting;
4029 int minibuffer_only;
4031 BLOCK_INPUT;
4033 /* Use the resource name as the top-level window name
4034 for looking up resources. Make a non-Lisp copy
4035 for the window manager, so GC relocation won't bother it.
4037 Elsewhere we specify the window name for the window manager. */
4040 char *str = (char *) SDATA (Vx_resource_name);
4041 f->namebuf = (char *) xmalloc (strlen (str) + 1);
4042 strcpy (f->namebuf, str);
4045 my_create_window (f);
4047 validate_x_resource_name ();
4049 /* x_set_name normally ignores requests to set the name if the
4050 requested name is the same as the current name. This is the one
4051 place where that assumption isn't correct; f->name is set, but
4052 the server hasn't been told. */
4054 Lisp_Object name;
4055 int explicit = f->explicit_name;
4057 f->explicit_name = 0;
4058 name = f->name;
4059 f->name = Qnil;
4060 x_set_name (f, name, explicit);
4063 UNBLOCK_INPUT;
4065 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
4066 initialize_frame_menubar (f);
4068 if (FRAME_W32_WINDOW (f) == 0)
4069 error ("Unable to create window");
4072 /* Handle the icon stuff for this window. Perhaps later we might
4073 want an x_set_icon_position which can be called interactively as
4074 well. */
4076 static void
4077 x_icon (f, parms)
4078 struct frame *f;
4079 Lisp_Object parms;
4081 Lisp_Object icon_x, icon_y;
4082 struct w32_display_info *dpyinfo = &one_w32_display_info;
4084 /* Set the position of the icon. Note that Windows 95 groups all
4085 icons in the tray. */
4086 icon_x = x_get_arg (dpyinfo, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
4087 icon_y = x_get_arg (dpyinfo, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
4088 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
4090 CHECK_NUMBER (icon_x);
4091 CHECK_NUMBER (icon_y);
4093 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
4094 error ("Both left and top icon corners of icon must be specified");
4096 BLOCK_INPUT;
4098 if (! EQ (icon_x, Qunbound))
4099 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
4101 #if 0 /* TODO */
4102 /* Start up iconic or window? */
4103 x_wm_set_window_state
4104 (f, (EQ (x_get_arg (dpyinfo, parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL), Qicon)
4105 ? IconicState
4106 : NormalState));
4108 x_text_icon (f, (char *) SDATA ((!NILP (f->icon_name)
4109 ? f->icon_name
4110 : f->name)));
4111 #endif
4113 UNBLOCK_INPUT;
4117 static void
4118 x_make_gc (f)
4119 struct frame *f;
4121 XGCValues gc_values;
4123 BLOCK_INPUT;
4125 /* Create the GC's of this frame.
4126 Note that many default values are used. */
4128 /* Normal video */
4129 gc_values.font = FRAME_FONT (f);
4131 /* Cursor has cursor-color background, background-color foreground. */
4132 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
4133 gc_values.background = f->output_data.w32->cursor_pixel;
4134 f->output_data.w32->cursor_gc
4135 = XCreateGC (NULL, FRAME_W32_WINDOW (f),
4136 (GCFont | GCForeground | GCBackground),
4137 &gc_values);
4139 /* Reliefs. */
4140 f->output_data.w32->white_relief.gc = 0;
4141 f->output_data.w32->black_relief.gc = 0;
4143 UNBLOCK_INPUT;
4147 /* Handler for signals raised during x_create_frame and
4148 x_create_top_frame. FRAME is the frame which is partially
4149 constructed. */
4151 static Lisp_Object
4152 unwind_create_frame (frame)
4153 Lisp_Object frame;
4155 struct frame *f = XFRAME (frame);
4157 /* If frame is ``official'', nothing to do. */
4158 if (!CONSP (Vframe_list) || !EQ (XCAR (Vframe_list), frame))
4160 #ifdef GLYPH_DEBUG
4161 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4162 #endif
4164 x_free_frame_resources (f);
4166 #if GLYPH_DEBUG
4167 /* Check that reference counts are indeed correct. */
4168 xassert (dpyinfo->reference_count == dpyinfo_refcount);
4169 xassert (dpyinfo->image_cache->refcount == image_cache_refcount);
4170 #endif
4171 return Qt;
4174 return Qnil;
4177 static void
4178 x_default_font_parameter (f, parms)
4179 struct frame *f;
4180 Lisp_Object parms;
4182 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4183 Lisp_Object font_param = x_get_arg (dpyinfo, parms, Qfont, NULL, NULL,
4184 RES_TYPE_STRING);
4185 Lisp_Object font;
4186 if (EQ (font_param, Qunbound))
4187 font_param = Qnil;
4188 font = !NILP (font_param) ? font_param
4189 : x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
4191 if (!STRINGP (font))
4193 int i;
4194 static char *names[]
4195 = { "Courier New-10",
4196 "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1",
4197 "-*-Fixedsys-normal-r-*-*-12-*-*-*-c-*-iso8859-1",
4198 "Fixedsys",
4199 NULL };
4201 for (i = 0; names[i]; i++)
4203 font = font_open_by_name (f, names[i]);
4204 if (! NILP (font))
4205 break;
4207 if (NILP (font))
4208 error ("No suitable font was found");
4210 else if (!NILP (font_param))
4212 /* Remember the explicit font parameter, so we can re-apply it after
4213 we've applied the `default' face settings. */
4214 x_set_frame_parameters (f, Fcons (Fcons (Qfont_param, font_param), Qnil));
4216 x_default_parameter (f, parms, Qfont, font, "font", "Font", RES_TYPE_STRING);
4219 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
4220 1, 1, 0,
4221 doc: /* Make a new window, which is called a \"frame\" in Emacs terms.
4222 Return an Emacs frame object.
4223 PARAMETERS is an alist of frame parameters.
4224 If the parameters specify that the frame should not have a minibuffer,
4225 and do not specify a specific minibuffer window to use,
4226 then `default-minibuffer-frame' must be a frame whose minibuffer can
4227 be shared by the new frame.
4229 This function is an internal primitive--use `make-frame' instead. */)
4230 (parameters)
4231 Lisp_Object parameters;
4233 struct frame *f;
4234 Lisp_Object frame, tem;
4235 Lisp_Object name;
4236 int minibuffer_only = 0;
4237 long window_prompting = 0;
4238 int width, height;
4239 int count = SPECPDL_INDEX ();
4240 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4241 Lisp_Object display;
4242 struct w32_display_info *dpyinfo = NULL;
4243 Lisp_Object parent;
4244 struct kboard *kb;
4246 /* Make copy of frame parameters because the original is in pure
4247 storage now. */
4248 parameters = Fcopy_alist (parameters);
4250 /* Use this general default value to start with
4251 until we know if this frame has a specified name. */
4252 Vx_resource_name = Vinvocation_name;
4254 display = x_get_arg (dpyinfo, parameters, Qterminal, 0, 0, RES_TYPE_NUMBER);
4255 if (EQ (display, Qunbound))
4256 display = x_get_arg (dpyinfo, parameters, Qdisplay, 0, 0, RES_TYPE_STRING);
4257 if (EQ (display, Qunbound))
4258 display = Qnil;
4259 dpyinfo = check_x_display_info (display);
4260 kb = dpyinfo->terminal->kboard;
4262 if (!dpyinfo->terminal->name)
4263 error ("Terminal is not live, can't create new frames on it");
4265 name = x_get_arg (dpyinfo, parameters, Qname, "name", "Name", RES_TYPE_STRING);
4266 if (!STRINGP (name)
4267 && ! EQ (name, Qunbound)
4268 && ! NILP (name))
4269 error ("Invalid frame name--not a string or nil");
4271 if (STRINGP (name))
4272 Vx_resource_name = name;
4274 /* See if parent window is specified. */
4275 parent = x_get_arg (dpyinfo, parameters, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
4276 if (EQ (parent, Qunbound))
4277 parent = Qnil;
4278 if (! NILP (parent))
4279 CHECK_NUMBER (parent);
4281 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
4282 /* No need to protect DISPLAY because that's not used after passing
4283 it to make_frame_without_minibuffer. */
4284 frame = Qnil;
4285 GCPRO4 (parameters, parent, name, frame);
4286 tem = x_get_arg (dpyinfo, parameters, Qminibuffer, "minibuffer", "Minibuffer",
4287 RES_TYPE_SYMBOL);
4288 if (EQ (tem, Qnone) || NILP (tem))
4289 f = make_frame_without_minibuffer (Qnil, kb, display);
4290 else if (EQ (tem, Qonly))
4292 f = make_minibuffer_frame ();
4293 minibuffer_only = 1;
4295 else if (WINDOWP (tem))
4296 f = make_frame_without_minibuffer (tem, kb, display);
4297 else
4298 f = make_frame (1);
4300 XSETFRAME (frame, f);
4302 /* Note that Windows does support scroll bars. */
4303 FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;
4305 /* By default, make scrollbars the system standard width. */
4306 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = GetSystemMetrics (SM_CXVSCROLL);
4308 f->terminal = dpyinfo->terminal;
4309 f->terminal->reference_count++;
4311 f->output_method = output_w32;
4312 f->output_data.w32 =
4313 (struct w32_output *) xmalloc (sizeof (struct w32_output));
4314 bzero (f->output_data.w32, sizeof (struct w32_output));
4315 FRAME_FONTSET (f) = -1;
4317 f->icon_name
4318 = x_get_arg (dpyinfo, parameters, Qicon_name, "iconName", "Title",
4319 RES_TYPE_STRING);
4320 if (! STRINGP (f->icon_name))
4321 f->icon_name = Qnil;
4323 /* FRAME_W32_DISPLAY_INFO (f) = dpyinfo; */
4325 /* With FRAME_X_DISPLAY_INFO set up, this unwind-protect is safe. */
4326 record_unwind_protect (unwind_create_frame, frame);
4327 #if GLYPH_DEBUG
4328 image_cache_refcount = FRAME_IMAGE_CACHE (f)->refcount;
4329 dpyinfo_refcount = dpyinfo->reference_count;
4330 #endif /* GLYPH_DEBUG */
4332 /* Specify the parent under which to make this window. */
4334 if (!NILP (parent))
4336 f->output_data.w32->parent_desc = (Window) XFASTINT (parent);
4337 f->output_data.w32->explicit_parent = 1;
4339 else
4341 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
4342 f->output_data.w32->explicit_parent = 0;
4345 /* Set the name; the functions to which we pass f expect the name to
4346 be set. */
4347 if (EQ (name, Qunbound) || NILP (name))
4349 f->name = build_string (dpyinfo->w32_id_name);
4350 f->explicit_name = 0;
4352 else
4354 f->name = name;
4355 f->explicit_name = 1;
4356 /* use the frame's title when getting resources for this frame. */
4357 specbind (Qx_resource_name, name);
4360 f->resx = dpyinfo->resx;
4361 f->resy = dpyinfo->resy;
4363 if (uniscribe_available)
4364 register_font_driver (&uniscribe_font_driver, f);
4365 register_font_driver (&w32font_driver, f);
4367 x_default_parameter (f, parameters, Qfont_backend, Qnil,
4368 "fontBackend", "FontBackend", RES_TYPE_STRING);
4369 /* Extract the window parameters from the supplied values
4370 that are needed to determine window geometry. */
4371 x_default_font_parameter (f, parameters);
4372 x_default_parameter (f, parameters, Qborder_width, make_number (2),
4373 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
4375 /* We recognize either internalBorderWidth or internalBorder
4376 (which is what xterm calls it). */
4377 if (NILP (Fassq (Qinternal_border_width, parameters)))
4379 Lisp_Object value;
4381 value = x_get_arg (dpyinfo, parameters, Qinternal_border_width,
4382 "internalBorder", "InternalBorder", RES_TYPE_NUMBER);
4383 if (! EQ (value, Qunbound))
4384 parameters = Fcons (Fcons (Qinternal_border_width, value),
4385 parameters);
4387 /* Default internalBorderWidth to 0 on Windows to match other programs. */
4388 x_default_parameter (f, parameters, Qinternal_border_width, make_number (0),
4389 "internalBorderWidth", "InternalBorder", RES_TYPE_NUMBER);
4390 x_default_parameter (f, parameters, Qvertical_scroll_bars, Qright,
4391 "verticalScrollBars", "ScrollBars", RES_TYPE_SYMBOL);
4393 /* Also do the stuff which must be set before the window exists. */
4394 x_default_parameter (f, parameters, Qforeground_color, build_string ("black"),
4395 "foreground", "Foreground", RES_TYPE_STRING);
4396 x_default_parameter (f, parameters, Qbackground_color, build_string ("white"),
4397 "background", "Background", RES_TYPE_STRING);
4398 x_default_parameter (f, parameters, Qmouse_color, build_string ("black"),
4399 "pointerColor", "Foreground", RES_TYPE_STRING);
4400 x_default_parameter (f, parameters, Qcursor_color, build_string ("black"),
4401 "cursorColor", "Foreground", RES_TYPE_STRING);
4402 x_default_parameter (f, parameters, Qborder_color, build_string ("black"),
4403 "borderColor", "BorderColor", RES_TYPE_STRING);
4404 x_default_parameter (f, parameters, Qscreen_gamma, Qnil,
4405 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
4406 x_default_parameter (f, parameters, Qline_spacing, Qnil,
4407 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
4408 x_default_parameter (f, parameters, Qleft_fringe, Qnil,
4409 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
4410 x_default_parameter (f, parameters, Qright_fringe, Qnil,
4411 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
4414 /* Init faces before x_default_parameter is called for scroll-bar
4415 parameters because that function calls x_set_scroll_bar_width,
4416 which calls change_frame_size, which calls Fset_window_buffer,
4417 which runs hooks, which call Fvertical_motion. At the end, we
4418 end up in init_iterator with a null face cache, which should not
4419 happen. */
4420 init_frame_faces (f);
4422 x_default_parameter (f, parameters, Qmenu_bar_lines, make_number (1),
4423 "menuBar", "MenuBar", RES_TYPE_NUMBER);
4424 x_default_parameter (f, parameters, Qtool_bar_lines, make_number (1),
4425 "toolBar", "ToolBar", RES_TYPE_NUMBER);
4427 x_default_parameter (f, parameters, Qbuffer_predicate, Qnil,
4428 "bufferPredicate", "BufferPredicate", RES_TYPE_SYMBOL);
4429 x_default_parameter (f, parameters, Qtitle, Qnil,
4430 "title", "Title", RES_TYPE_STRING);
4431 x_default_parameter (f, parameters, Qfullscreen, Qnil,
4432 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
4434 f->output_data.w32->dwStyle = WS_OVERLAPPEDWINDOW;
4435 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
4437 f->output_data.w32->text_cursor = w32_load_cursor (IDC_IBEAM);
4438 f->output_data.w32->nontext_cursor = w32_load_cursor (IDC_ARROW);
4439 f->output_data.w32->modeline_cursor = w32_load_cursor (IDC_ARROW);
4440 f->output_data.w32->hand_cursor = w32_load_cursor (IDC_HAND);
4441 f->output_data.w32->hourglass_cursor = w32_load_cursor (IDC_WAIT);
4442 f->output_data.w32->horizontal_drag_cursor = w32_load_cursor (IDC_SIZEWE);
4444 f->output_data.w32->current_cursor = f->output_data.w32->nontext_cursor;
4446 window_prompting = x_figure_window_size (f, parameters, 1);
4448 tem = x_get_arg (dpyinfo, parameters, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
4449 f->no_split = minibuffer_only || EQ (tem, Qt);
4451 w32_window (f, window_prompting, minibuffer_only);
4452 x_icon (f, parameters);
4454 x_make_gc (f);
4456 /* Now consider the frame official. */
4457 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
4458 Vframe_list = Fcons (frame, Vframe_list);
4460 /* We need to do this after creating the window, so that the
4461 icon-creation functions can say whose icon they're describing. */
4462 x_default_parameter (f, parameters, Qicon_type, Qnil,
4463 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
4465 x_default_parameter (f, parameters, Qauto_raise, Qnil,
4466 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4467 x_default_parameter (f, parameters, Qauto_lower, Qnil,
4468 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4469 x_default_parameter (f, parameters, Qcursor_type, Qbox,
4470 "cursorType", "CursorType", RES_TYPE_SYMBOL);
4471 x_default_parameter (f, parameters, Qscroll_bar_width, Qnil,
4472 "scrollBarWidth", "ScrollBarWidth", RES_TYPE_NUMBER);
4473 x_default_parameter (f, parameters, Qalpha, Qnil,
4474 "alpha", "Alpha", RES_TYPE_NUMBER);
4476 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
4477 Change will not be effected unless different from the current
4478 FRAME_LINES (f). */
4479 width = FRAME_COLS (f);
4480 height = FRAME_LINES (f);
4482 FRAME_LINES (f) = 0;
4483 SET_FRAME_COLS (f, 0);
4484 change_frame_size (f, height, width, 1, 0, 0);
4486 /* Tell the server what size and position, etc, we want, and how
4487 badly we want them. This should be done after we have the menu
4488 bar so that its size can be taken into account. */
4489 BLOCK_INPUT;
4490 x_wm_set_size_hint (f, window_prompting, 0);
4491 UNBLOCK_INPUT;
4493 /* Make the window appear on the frame and enable display, unless
4494 the caller says not to. However, with explicit parent, Emacs
4495 cannot control visibility, so don't try. */
4496 if (! f->output_data.w32->explicit_parent)
4498 Lisp_Object visibility;
4500 visibility = x_get_arg (dpyinfo, parameters, Qvisibility, 0, 0, RES_TYPE_SYMBOL);
4501 if (EQ (visibility, Qunbound))
4502 visibility = Qt;
4504 if (EQ (visibility, Qicon))
4505 x_iconify_frame (f);
4506 else if (! NILP (visibility))
4507 x_make_frame_visible (f);
4508 else
4509 /* Must have been Qnil. */
4513 /* Initialize `default-minibuffer-frame' in case this is the first
4514 frame on this terminal. */
4515 if (FRAME_HAS_MINIBUF_P (f)
4516 && (!FRAMEP (kb->Vdefault_minibuffer_frame)
4517 || !FRAME_LIVE_P (XFRAME (kb->Vdefault_minibuffer_frame))))
4518 kb->Vdefault_minibuffer_frame = frame;
4520 /* All remaining specified parameters, which have not been "used"
4521 by x_get_arg and friends, now go in the misc. alist of the frame. */
4522 for (tem = parameters; CONSP (tem); tem = XCDR (tem))
4523 if (CONSP (XCAR (tem)) && !NILP (XCAR (XCAR (tem))))
4524 f->param_alist = Fcons (XCAR (tem), f->param_alist);
4526 UNGCPRO;
4528 /* Make sure windows on this frame appear in calls to next-window
4529 and similar functions. */
4530 Vwindow_list = Qnil;
4532 return unbind_to (count, frame);
4535 /* FRAME is used only to get a handle on the X display. We don't pass the
4536 display info directly because we're called from frame.c, which doesn't
4537 know about that structure. */
4538 Lisp_Object
4539 x_get_focus_frame (frame)
4540 struct frame *frame;
4542 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (frame);
4543 Lisp_Object xfocus;
4544 if (! dpyinfo->w32_focus_frame)
4545 return Qnil;
4547 XSETFRAME (xfocus, dpyinfo->w32_focus_frame);
4548 return xfocus;
4551 DEFUN ("x-focus-frame", Fx_focus_frame, Sx_focus_frame, 1, 1, 0,
4552 doc: /* Give FRAME input focus, raising to foreground if necessary. */)
4553 (frame)
4554 Lisp_Object frame;
4556 x_focus_on_frame (check_x_frame (frame));
4557 return Qnil;
4561 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
4562 doc: /* Internal function called by `color-defined-p', which see. */)
4563 (color, frame)
4564 Lisp_Object color, frame;
4566 XColor foo;
4567 FRAME_PTR f = check_x_frame (frame);
4569 CHECK_STRING (color);
4571 if (w32_defined_color (f, SDATA (color), &foo, 0))
4572 return Qt;
4573 else
4574 return Qnil;
4577 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
4578 doc: /* Internal function called by `color-values', which see. */)
4579 (color, frame)
4580 Lisp_Object color, frame;
4582 XColor foo;
4583 FRAME_PTR f = check_x_frame (frame);
4585 CHECK_STRING (color);
4587 if (w32_defined_color (f, SDATA (color), &foo, 0))
4588 return list3 (make_number ((GetRValue (foo.pixel) << 8)
4589 | GetRValue (foo.pixel)),
4590 make_number ((GetGValue (foo.pixel) << 8)
4591 | GetGValue (foo.pixel)),
4592 make_number ((GetBValue (foo.pixel) << 8)
4593 | GetBValue (foo.pixel)));
4594 else
4595 return Qnil;
4598 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
4599 doc: /* Internal function called by `display-color-p', which see. */)
4600 (display)
4601 Lisp_Object display;
4603 struct w32_display_info *dpyinfo = check_x_display_info (display);
4605 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 2)
4606 return Qnil;
4608 return Qt;
4611 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p,
4612 Sx_display_grayscale_p, 0, 1, 0,
4613 doc: /* Return t if DISPLAY supports shades of gray.
4614 Note that color displays do support shades of gray.
4615 The optional argument DISPLAY specifies which display to ask about.
4616 DISPLAY should be either a frame or a display name (a string).
4617 If omitted or nil, that stands for the selected frame's display. */)
4618 (display)
4619 Lisp_Object display;
4621 struct w32_display_info *dpyinfo = check_x_display_info (display);
4623 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 1)
4624 return Qnil;
4626 return Qt;
4629 DEFUN ("x-display-pixel-width", Fx_display_pixel_width,
4630 Sx_display_pixel_width, 0, 1, 0,
4631 doc: /* Return the width in pixels of DISPLAY.
4632 The optional argument DISPLAY specifies which display to ask about.
4633 DISPLAY should be either a frame or a display name (a string).
4634 If omitted or nil, that stands for the selected frame's display. */)
4635 (display)
4636 Lisp_Object display;
4638 struct w32_display_info *dpyinfo = check_x_display_info (display);
4640 return make_number (x_display_pixel_width (dpyinfo));
4643 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
4644 Sx_display_pixel_height, 0, 1, 0,
4645 doc: /* Return the height in pixels of DISPLAY.
4646 The optional argument DISPLAY specifies which display to ask about.
4647 DISPLAY should be either a frame or a display name (a string).
4648 If omitted or nil, that stands for the selected frame's display. */)
4649 (display)
4650 Lisp_Object display;
4652 struct w32_display_info *dpyinfo = check_x_display_info (display);
4654 return make_number (x_display_pixel_height (dpyinfo));
4657 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
4658 0, 1, 0,
4659 doc: /* Return the number of bitplanes of DISPLAY.
4660 The optional argument DISPLAY specifies which display to ask about.
4661 DISPLAY should be either a frame or a display name (a string).
4662 If omitted or nil, that stands for the selected frame's display. */)
4663 (display)
4664 Lisp_Object display;
4666 struct w32_display_info *dpyinfo = check_x_display_info (display);
4668 return make_number (dpyinfo->n_planes * dpyinfo->n_cbits);
4671 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
4672 0, 1, 0,
4673 doc: /* Return the number of color cells of DISPLAY.
4674 The optional argument DISPLAY specifies which display to ask about.
4675 DISPLAY should be either a frame or a display name (a string).
4676 If omitted or nil, that stands for the selected frame's display. */)
4677 (display)
4678 Lisp_Object display;
4680 struct w32_display_info *dpyinfo = check_x_display_info (display);
4681 HDC hdc;
4682 int cap;
4684 hdc = GetDC (dpyinfo->root_window);
4685 if (dpyinfo->has_palette)
4686 cap = GetDeviceCaps (hdc, SIZEPALETTE);
4687 else
4688 cap = GetDeviceCaps (hdc, NUMCOLORS);
4690 /* We force 24+ bit depths to 24-bit, both to prevent an overflow
4691 and because probably is more meaningful on Windows anyway */
4692 if (cap < 0)
4693 cap = 1 << min (dpyinfo->n_planes * dpyinfo->n_cbits, 24);
4695 ReleaseDC (dpyinfo->root_window, hdc);
4697 return make_number (cap);
4700 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
4701 Sx_server_max_request_size,
4702 0, 1, 0,
4703 doc: /* Return the maximum request size of the server of DISPLAY.
4704 The optional argument DISPLAY specifies which display to ask about.
4705 DISPLAY should be either a frame or a display name (a string).
4706 If omitted or nil, that stands for the selected frame's display. */)
4707 (display)
4708 Lisp_Object display;
4710 struct w32_display_info *dpyinfo = check_x_display_info (display);
4712 return make_number (1);
4715 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
4716 doc: /* Return the "vendor ID" string of the W32 system (Microsoft).
4717 The optional argument DISPLAY specifies which display to ask about.
4718 DISPLAY should be either a frame or a display name (a string).
4719 If omitted or nil, that stands for the selected frame's display. */)
4720 (display)
4721 Lisp_Object display;
4723 return build_string ("Microsoft Corp.");
4726 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
4727 doc: /* Return the version numbers of the server of DISPLAY.
4728 The value is a list of three integers: the major and minor
4729 version numbers of the X Protocol in use, and the distributor-specific
4730 release number. See also the function `x-server-vendor'.
4732 The optional argument DISPLAY specifies which display to ask about.
4733 DISPLAY should be either a frame or a display name (a string).
4734 If omitted or nil, that stands for the selected frame's display. */)
4735 (display)
4736 Lisp_Object display;
4738 return Fcons (make_number (w32_major_version),
4739 Fcons (make_number (w32_minor_version),
4740 Fcons (make_number (w32_build_number), Qnil)));
4743 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
4744 doc: /* Return the number of screens on the server of DISPLAY.
4745 The optional argument DISPLAY specifies which display to ask about.
4746 DISPLAY should be either a frame or a display name (a string).
4747 If omitted or nil, that stands for the selected frame's display. */)
4748 (display)
4749 Lisp_Object display;
4751 return make_number (1);
4754 DEFUN ("x-display-mm-height", Fx_display_mm_height,
4755 Sx_display_mm_height, 0, 1, 0,
4756 doc: /* Return the height in millimeters of DISPLAY.
4757 The optional argument DISPLAY specifies which display to ask about.
4758 DISPLAY should be either a frame or a display name (a string).
4759 If omitted or nil, that stands for the selected frame's display. */)
4760 (display)
4761 Lisp_Object display;
4763 struct w32_display_info *dpyinfo = check_x_display_info (display);
4764 HDC hdc;
4765 int cap;
4767 hdc = GetDC (dpyinfo->root_window);
4769 cap = GetDeviceCaps (hdc, VERTSIZE);
4771 ReleaseDC (dpyinfo->root_window, hdc);
4773 return make_number (cap);
4776 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
4777 doc: /* Return the width in millimeters of DISPLAY.
4778 The optional argument DISPLAY specifies which display to ask about.
4779 DISPLAY should be either a frame or a display name (a string).
4780 If omitted or nil, that stands for the selected frame's display. */)
4781 (display)
4782 Lisp_Object display;
4784 struct w32_display_info *dpyinfo = check_x_display_info (display);
4786 HDC hdc;
4787 int cap;
4789 hdc = GetDC (dpyinfo->root_window);
4791 cap = GetDeviceCaps (hdc, HORZSIZE);
4793 ReleaseDC (dpyinfo->root_window, hdc);
4795 return make_number (cap);
4798 DEFUN ("x-display-backing-store", Fx_display_backing_store,
4799 Sx_display_backing_store, 0, 1, 0,
4800 doc: /* Return an indication of whether DISPLAY does backing store.
4801 The value may be `always', `when-mapped', or `not-useful'.
4802 The optional argument DISPLAY specifies which display to ask about.
4803 DISPLAY should be either a frame or a display name (a string).
4804 If omitted or nil, that stands for the selected frame's display. */)
4805 (display)
4806 Lisp_Object display;
4808 return intern ("not-useful");
4811 DEFUN ("x-display-visual-class", Fx_display_visual_class,
4812 Sx_display_visual_class, 0, 1, 0,
4813 doc: /* Return the visual class of DISPLAY.
4814 The value is one of the symbols `static-gray', `gray-scale',
4815 `static-color', `pseudo-color', `true-color', or `direct-color'.
4817 The optional argument DISPLAY specifies which display to ask about.
4818 DISPLAY should be either a frame or a display name (a string).
4819 If omitted or nil, that stands for the selected frame's display. */)
4820 (display)
4821 Lisp_Object display;
4823 struct w32_display_info *dpyinfo = check_x_display_info (display);
4824 Lisp_Object result = Qnil;
4826 if (dpyinfo->has_palette)
4827 result = intern ("pseudo-color");
4828 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 1)
4829 result = intern ("static-grey");
4830 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 4)
4831 result = intern ("static-color");
4832 else if (dpyinfo->n_planes * dpyinfo->n_cbits > 8)
4833 result = intern ("true-color");
4835 return result;
4838 DEFUN ("x-display-save-under", Fx_display_save_under,
4839 Sx_display_save_under, 0, 1, 0,
4840 doc: /* Return t if DISPLAY supports the save-under feature.
4841 The optional argument DISPLAY specifies which display to ask about.
4842 DISPLAY should be either a frame or a display name (a string).
4843 If omitted or nil, that stands for the selected frame's display. */)
4844 (display)
4845 Lisp_Object display;
4847 return Qnil;
4851 x_pixel_width (f)
4852 register struct frame *f;
4854 return FRAME_PIXEL_WIDTH (f);
4858 x_pixel_height (f)
4859 register struct frame *f;
4861 return FRAME_PIXEL_HEIGHT (f);
4865 x_char_width (f)
4866 register struct frame *f;
4868 return FRAME_COLUMN_WIDTH (f);
4872 x_char_height (f)
4873 register struct frame *f;
4875 return FRAME_LINE_HEIGHT (f);
4879 x_screen_planes (f)
4880 register struct frame *f;
4882 return FRAME_W32_DISPLAY_INFO (f)->n_planes;
4885 /* Return the display structure for the display named NAME.
4886 Open a new connection if necessary. */
4888 struct w32_display_info *
4889 x_display_info_for_name (name)
4890 Lisp_Object name;
4892 Lisp_Object names;
4893 struct w32_display_info *dpyinfo;
4895 CHECK_STRING (name);
4897 for (dpyinfo = &one_w32_display_info, names = w32_display_name_list;
4898 dpyinfo;
4899 dpyinfo = dpyinfo->next, names = XCDR (names))
4901 Lisp_Object tem;
4902 tem = Fstring_equal (XCAR (XCAR (names)), name);
4903 if (!NILP (tem))
4904 return dpyinfo;
4907 /* Use this general default value to start with. */
4908 Vx_resource_name = Vinvocation_name;
4910 validate_x_resource_name ();
4912 dpyinfo = w32_term_init (name, (unsigned char *)0,
4913 (char *) SDATA (Vx_resource_name));
4915 if (dpyinfo == 0)
4916 error ("Cannot connect to server %s", SDATA (name));
4918 w32_in_use = 1;
4919 XSETFASTINT (Vwindow_system_version, w32_major_version);
4921 return dpyinfo;
4924 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
4925 1, 3, 0, doc: /* Open a connection to a server.
4926 DISPLAY is the name of the display to connect to.
4927 Optional second arg XRM-STRING is a string of resources in xrdb format.
4928 If the optional third arg MUST-SUCCEED is non-nil,
4929 terminate Emacs if we can't open the connection. */)
4930 (display, xrm_string, must_succeed)
4931 Lisp_Object display, xrm_string, must_succeed;
4933 unsigned char *xrm_option;
4934 struct w32_display_info *dpyinfo;
4936 /* If initialization has already been done, return now to avoid
4937 overwriting critical parts of one_w32_display_info. */
4938 if (w32_in_use)
4939 return Qnil;
4941 CHECK_STRING (display);
4942 if (! NILP (xrm_string))
4943 CHECK_STRING (xrm_string);
4945 #if 0
4946 if (! EQ (Vwindow_system, intern ("w32")))
4947 error ("Not using Microsoft Windows");
4948 #endif
4950 /* Allow color mapping to be defined externally; first look in user's
4951 HOME directory, then in Emacs etc dir for a file called rgb.txt. */
4953 Lisp_Object color_file;
4954 struct gcpro gcpro1;
4956 color_file = build_string ("~/rgb.txt");
4958 GCPRO1 (color_file);
4960 if (NILP (Ffile_readable_p (color_file)))
4961 color_file =
4962 Fexpand_file_name (build_string ("rgb.txt"),
4963 Fsymbol_value (intern ("data-directory")));
4965 Vw32_color_map = Fx_load_color_file (color_file);
4967 UNGCPRO;
4969 if (NILP (Vw32_color_map))
4970 Vw32_color_map = Fw32_default_color_map ();
4972 /* Merge in system logical colors. */
4973 add_system_logical_colors_to_map (&Vw32_color_map);
4975 if (! NILP (xrm_string))
4976 xrm_option = (unsigned char *) SDATA (xrm_string);
4977 else
4978 xrm_option = (unsigned char *) 0;
4980 /* Use this general default value to start with. */
4981 /* First remove .exe suffix from invocation-name - it looks ugly. */
4983 char basename[ MAX_PATH ], *str;
4985 strcpy (basename, SDATA (Vinvocation_name));
4986 str = strrchr (basename, '.');
4987 if (str) *str = 0;
4988 Vinvocation_name = build_string (basename);
4990 Vx_resource_name = Vinvocation_name;
4992 validate_x_resource_name ();
4994 /* This is what opens the connection and sets x_current_display.
4995 This also initializes many symbols, such as those used for input. */
4996 dpyinfo = w32_term_init (display, xrm_option,
4997 (char *) SDATA (Vx_resource_name));
4999 if (dpyinfo == 0)
5001 if (!NILP (must_succeed))
5002 fatal ("Cannot connect to server %s.\n",
5003 SDATA (display));
5004 else
5005 error ("Cannot connect to server %s", SDATA (display));
5008 w32_in_use = 1;
5010 XSETFASTINT (Vwindow_system_version, w32_major_version);
5011 return Qnil;
5014 DEFUN ("x-close-connection", Fx_close_connection,
5015 Sx_close_connection, 1, 1, 0,
5016 doc: /* Close the connection to DISPLAY's server.
5017 For DISPLAY, specify either a frame or a display name (a string).
5018 If DISPLAY is nil, that stands for the selected frame's display. */)
5019 (display)
5020 Lisp_Object display;
5022 struct w32_display_info *dpyinfo = check_x_display_info (display);
5023 int i;
5025 if (dpyinfo->reference_count > 0)
5026 error ("Display still has frames on it");
5028 BLOCK_INPUT;
5029 x_destroy_all_bitmaps (dpyinfo);
5031 x_delete_display (dpyinfo);
5032 UNBLOCK_INPUT;
5034 return Qnil;
5037 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
5038 doc: /* Return the list of display names that Emacs has connections to. */)
5041 Lisp_Object tail, result;
5043 result = Qnil;
5044 for (tail = w32_display_name_list; CONSP (tail); tail = XCDR (tail))
5045 result = Fcons (XCAR (XCAR (tail)), result);
5047 return result;
5050 DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
5051 doc: /* This is a noop on W32 systems. */)
5052 (on, display)
5053 Lisp_Object display, on;
5055 return Qnil;
5060 /***********************************************************************
5061 Window properties
5062 ***********************************************************************/
5064 DEFUN ("x-change-window-property", Fx_change_window_property,
5065 Sx_change_window_property, 2, 6, 0,
5066 doc: /* Change window property PROP to VALUE on the X window of FRAME.
5067 VALUE may be a string or a list of conses, numbers and/or strings.
5068 If an element in the list is a string, it is converted to
5069 an Atom and the value of the Atom is used. If an element is a cons,
5070 it is converted to a 32 bit number where the car is the 16 top bits and the
5071 cdr is the lower 16 bits.
5072 FRAME nil or omitted means use the selected frame.
5073 If TYPE is given and non-nil, it is the name of the type of VALUE.
5074 If TYPE is not given or nil, the type is STRING.
5075 FORMAT gives the size in bits of each element if VALUE is a list.
5076 It must be one of 8, 16 or 32.
5077 If VALUE is a string or FORMAT is nil or not given, FORMAT defaults to 8.
5078 If OUTER_P is non-nil, the property is changed for the outer X window of
5079 FRAME. Default is to change on the edit X window.
5081 Value is VALUE. */)
5082 (prop, value, frame, type, format, outer_p)
5083 Lisp_Object prop, value, frame, type, format, outer_p;
5085 #if 0 /* TODO : port window properties to W32 */
5086 struct frame *f = check_x_frame (frame);
5087 Atom prop_atom;
5089 CHECK_STRING (prop);
5090 CHECK_STRING (value);
5092 BLOCK_INPUT;
5093 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
5094 XChangeProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
5095 prop_atom, XA_STRING, 8, PropModeReplace,
5096 SDATA (value), SCHARS (value));
5098 /* Make sure the property is set when we return. */
5099 XFlush (FRAME_W32_DISPLAY (f));
5100 UNBLOCK_INPUT;
5102 #endif /* TODO */
5104 return value;
5108 DEFUN ("x-delete-window-property", Fx_delete_window_property,
5109 Sx_delete_window_property, 1, 2, 0,
5110 doc: /* Remove window property PROP from X window of FRAME.
5111 FRAME nil or omitted means use the selected frame. Value is PROP. */)
5112 (prop, frame)
5113 Lisp_Object prop, frame;
5115 #if 0 /* TODO : port window properties to W32 */
5117 struct frame *f = check_x_frame (frame);
5118 Atom prop_atom;
5120 CHECK_STRING (prop);
5121 BLOCK_INPUT;
5122 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
5123 XDeleteProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), prop_atom);
5125 /* Make sure the property is removed when we return. */
5126 XFlush (FRAME_W32_DISPLAY (f));
5127 UNBLOCK_INPUT;
5128 #endif /* TODO */
5130 return prop;
5134 DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
5135 1, 2, 0,
5136 doc: /* Value is the value of window property PROP on FRAME.
5137 If FRAME is nil or omitted, use the selected frame. Value is nil
5138 if FRAME hasn't a property with name PROP or if PROP has no string
5139 value. */)
5140 (prop, frame)
5141 Lisp_Object prop, frame;
5143 #if 0 /* TODO : port window properties to W32 */
5145 struct frame *f = check_x_frame (frame);
5146 Atom prop_atom;
5147 int rc;
5148 Lisp_Object prop_value = Qnil;
5149 char *tmp_data = NULL;
5150 Atom actual_type;
5151 int actual_format;
5152 unsigned long actual_size, bytes_remaining;
5154 CHECK_STRING (prop);
5155 BLOCK_INPUT;
5156 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
5157 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
5158 prop_atom, 0, 0, False, XA_STRING,
5159 &actual_type, &actual_format, &actual_size,
5160 &bytes_remaining, (unsigned char **) &tmp_data);
5161 if (rc == Success)
5163 int size = bytes_remaining;
5165 XFree (tmp_data);
5166 tmp_data = NULL;
5168 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
5169 prop_atom, 0, bytes_remaining,
5170 False, XA_STRING,
5171 &actual_type, &actual_format,
5172 &actual_size, &bytes_remaining,
5173 (unsigned char **) &tmp_data);
5174 if (rc == Success)
5175 prop_value = make_string (tmp_data, size);
5177 XFree (tmp_data);
5180 UNBLOCK_INPUT;
5182 return prop_value;
5184 #endif /* TODO */
5185 return Qnil;
5190 /***********************************************************************
5191 Busy cursor
5192 ***********************************************************************/
5194 /* Default number of seconds to wait before displaying an hourglass
5195 cursor. Duplicated from xdisp.c, but cannot use the version there
5196 due to lack of atimers on w32. */
5197 #define DEFAULT_HOURGLASS_DELAY 1
5198 extern Lisp_Object Vhourglass_delay;
5200 /* Return non-zero if houglass timer has been started or hourglass is shown. */
5201 /* PENDING: if W32 can use atimers (atimer.[hc]) then the common impl in
5202 xdisp.c could be used. */
5205 hourglass_started ()
5207 return hourglass_shown_p || hourglass_timer;
5210 /* Cancel a currently active hourglass timer, and start a new one. */
5212 void
5213 start_hourglass ()
5215 DWORD delay;
5216 int secs, msecs = 0;
5217 struct frame * f = SELECTED_FRAME ();
5219 /* No cursors on non GUI frames. */
5220 if (!FRAME_W32_P (f))
5221 return;
5223 cancel_hourglass ();
5225 if (INTEGERP (Vhourglass_delay)
5226 && XINT (Vhourglass_delay) > 0)
5227 secs = XFASTINT (Vhourglass_delay);
5228 else if (FLOATP (Vhourglass_delay)
5229 && XFLOAT_DATA (Vhourglass_delay) > 0)
5231 Lisp_Object tem;
5232 tem = Ftruncate (Vhourglass_delay, Qnil);
5233 secs = XFASTINT (tem);
5234 msecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000;
5236 else
5237 secs = DEFAULT_HOURGLASS_DELAY;
5239 delay = secs * 1000 + msecs;
5240 hourglass_hwnd = FRAME_W32_WINDOW (f);
5241 hourglass_timer = SetTimer (hourglass_hwnd, HOURGLASS_ID, delay, NULL);
5245 /* Cancel the hourglass cursor timer if active, hide an hourglass
5246 cursor if shown. */
5248 void
5249 cancel_hourglass ()
5251 if (hourglass_timer)
5253 KillTimer (hourglass_hwnd, hourglass_timer);
5254 hourglass_timer = 0;
5257 if (hourglass_shown_p)
5258 w32_hide_hourglass ();
5262 /* Timer function of hourglass_timer.
5264 Display an hourglass cursor. Set the hourglass_p flag in display info
5265 to indicate that an hourglass cursor is shown. */
5267 static void
5268 w32_show_hourglass (f)
5269 struct frame *f;
5271 if (!hourglass_shown_p)
5273 f->output_data.w32->hourglass_p = 1;
5274 if (!menubar_in_use && !current_popup_menu)
5275 SetCursor (f->output_data.w32->hourglass_cursor);
5276 hourglass_shown_p = 1;
5281 /* Hide the hourglass cursor on all frames, if it is currently shown. */
5283 static void
5284 w32_hide_hourglass ()
5286 if (hourglass_shown_p)
5288 struct frame *f = x_window_to_frame (&one_w32_display_info,
5289 hourglass_hwnd);
5290 if (f)
5291 f->output_data.w32->hourglass_p = 0;
5292 else
5293 /* If frame was deleted, restore to selected frame's cursor. */
5294 f = SELECTED_FRAME ();
5296 if (FRAME_W32_P (f))
5297 SetCursor (f->output_data.w32->current_cursor);
5298 else
5299 /* No cursors on non GUI frames - restore to stock arrow cursor. */
5300 SetCursor (w32_load_cursor (IDC_ARROW));
5302 hourglass_shown_p = 0;
5308 /***********************************************************************
5309 Tool tips
5310 ***********************************************************************/
5312 static Lisp_Object x_create_tip_frame P_ ((struct w32_display_info *,
5313 Lisp_Object, Lisp_Object));
5314 static void compute_tip_xy P_ ((struct frame *, Lisp_Object, Lisp_Object,
5315 Lisp_Object, int, int, int *, int *));
5317 /* The frame of a currently visible tooltip. */
5319 Lisp_Object tip_frame;
5321 /* If non-nil, a timer started that hides the last tooltip when it
5322 fires. */
5324 Lisp_Object tip_timer;
5325 Window tip_window;
5327 /* If non-nil, a vector of 3 elements containing the last args
5328 with which x-show-tip was called. See there. */
5330 Lisp_Object last_show_tip_args;
5332 /* Maximum size for tooltips; a cons (COLUMNS . ROWS). */
5334 Lisp_Object Vx_max_tooltip_size;
5337 static Lisp_Object
5338 unwind_create_tip_frame (frame)
5339 Lisp_Object frame;
5341 Lisp_Object deleted;
5343 deleted = unwind_create_frame (frame);
5344 if (EQ (deleted, Qt))
5346 tip_window = NULL;
5347 tip_frame = Qnil;
5350 return deleted;
5354 /* Create a frame for a tooltip on the display described by DPYINFO.
5355 PARMS is a list of frame parameters. TEXT is the string to
5356 display in the tip frame. Value is the frame.
5358 Note that functions called here, esp. x_default_parameter can
5359 signal errors, for instance when a specified color name is
5360 undefined. We have to make sure that we're in a consistent state
5361 when this happens. */
5363 static Lisp_Object
5364 x_create_tip_frame (dpyinfo, parms, text)
5365 struct w32_display_info *dpyinfo;
5366 Lisp_Object parms, text;
5368 struct frame *f;
5369 Lisp_Object frame, tem;
5370 Lisp_Object name;
5371 long window_prompting = 0;
5372 int width, height;
5373 int count = SPECPDL_INDEX ();
5374 struct gcpro gcpro1, gcpro2, gcpro3;
5375 struct kboard *kb;
5376 int face_change_count_before = face_change_count;
5377 Lisp_Object buffer;
5378 struct buffer *old_buffer;
5380 check_w32 ();
5382 /* Use this general default value to start with until we know if
5383 this frame has a specified name. */
5384 Vx_resource_name = Vinvocation_name;
5386 kb = dpyinfo->terminal->kboard;
5388 /* Get the name of the frame to use for resource lookup. */
5389 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
5390 if (!STRINGP (name)
5391 && !EQ (name, Qunbound)
5392 && !NILP (name))
5393 error ("Invalid frame name--not a string or nil");
5394 Vx_resource_name = name;
5396 frame = Qnil;
5397 GCPRO3 (parms, name, frame);
5398 /* Make a frame without minibuffer nor mode-line. */
5399 f = make_frame (0);
5400 f->wants_modeline = 0;
5401 XSETFRAME (frame, f);
5403 buffer = Fget_buffer_create (build_string (" *tip*"));
5404 Fset_window_buffer (FRAME_ROOT_WINDOW (f), buffer, Qnil);
5405 old_buffer = current_buffer;
5406 set_buffer_internal_1 (XBUFFER (buffer));
5407 current_buffer->truncate_lines = Qnil;
5408 specbind (Qinhibit_read_only, Qt);
5409 specbind (Qinhibit_modification_hooks, Qt);
5410 Ferase_buffer ();
5411 Finsert (1, &text);
5412 set_buffer_internal_1 (old_buffer);
5414 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
5415 record_unwind_protect (unwind_create_tip_frame, frame);
5417 /* By setting the output method, we're essentially saying that
5418 the frame is live, as per FRAME_LIVE_P. If we get a signal
5419 from this point on, x_destroy_window might screw up reference
5420 counts etc. */
5421 f->terminal = dpyinfo->terminal;
5422 f->terminal->reference_count++;
5423 f->output_method = output_w32;
5424 f->output_data.w32 =
5425 (struct w32_output *) xmalloc (sizeof (struct w32_output));
5426 bzero (f->output_data.w32, sizeof (struct w32_output));
5428 FRAME_FONTSET (f) = -1;
5429 f->icon_name = Qnil;
5431 #if GLYPH_DEBUG
5432 image_cache_refcount = FRAME_IMAGE_CACHE (f)->refcount;
5433 dpyinfo_refcount = dpyinfo->reference_count;
5434 #endif /* GLYPH_DEBUG */
5435 FRAME_KBOARD (f) = kb;
5436 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
5437 f->output_data.w32->explicit_parent = 0;
5439 /* Set the name; the functions to which we pass f expect the name to
5440 be set. */
5441 if (EQ (name, Qunbound) || NILP (name))
5443 f->name = build_string (dpyinfo->w32_id_name);
5444 f->explicit_name = 0;
5446 else
5448 f->name = name;
5449 f->explicit_name = 1;
5450 /* use the frame's title when getting resources for this frame. */
5451 specbind (Qx_resource_name, name);
5454 f->resx = dpyinfo->resx;
5455 f->resy = dpyinfo->resy;
5457 /* Perhaps, we must allow frame parameter, say `font-backend',
5458 to specify which font backends to use. */
5459 register_font_driver (&w32font_driver, f);
5461 x_default_parameter (f, parms, Qfont_backend, Qnil,
5462 "fontBackend", "FontBackend", RES_TYPE_STRING);
5464 /* Extract the window parameters from the supplied values
5465 that are needed to determine window geometry. */
5466 x_default_font_parameter (f, parms);
5468 x_default_parameter (f, parms, Qborder_width, make_number (2),
5469 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
5470 /* This defaults to 2 in order to match xterm. We recognize either
5471 internalBorderWidth or internalBorder (which is what xterm calls
5472 it). */
5473 if (NILP (Fassq (Qinternal_border_width, parms)))
5475 Lisp_Object value;
5477 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
5478 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
5479 if (! EQ (value, Qunbound))
5480 parms = Fcons (Fcons (Qinternal_border_width, value),
5481 parms);
5483 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
5484 "internalBorderWidth", "internalBorderWidth",
5485 RES_TYPE_NUMBER);
5487 /* Also do the stuff which must be set before the window exists. */
5488 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
5489 "foreground", "Foreground", RES_TYPE_STRING);
5490 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
5491 "background", "Background", RES_TYPE_STRING);
5492 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
5493 "pointerColor", "Foreground", RES_TYPE_STRING);
5494 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
5495 "cursorColor", "Foreground", RES_TYPE_STRING);
5496 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
5497 "borderColor", "BorderColor", RES_TYPE_STRING);
5499 /* Init faces before x_default_parameter is called for scroll-bar
5500 parameters because that function calls x_set_scroll_bar_width,
5501 which calls change_frame_size, which calls Fset_window_buffer,
5502 which runs hooks, which call Fvertical_motion. At the end, we
5503 end up in init_iterator with a null face cache, which should not
5504 happen. */
5505 init_frame_faces (f);
5507 f->output_data.w32->dwStyle = WS_BORDER | WS_POPUP | WS_DISABLED;
5508 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
5510 window_prompting = x_figure_window_size (f, parms, 0);
5512 /* No fringes on tip frame. */
5513 f->fringe_cols = 0;
5514 f->left_fringe_width = 0;
5515 f->right_fringe_width = 0;
5517 BLOCK_INPUT;
5518 my_create_tip_window (f);
5519 UNBLOCK_INPUT;
5521 x_make_gc (f);
5523 x_default_parameter (f, parms, Qauto_raise, Qnil,
5524 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5525 x_default_parameter (f, parms, Qauto_lower, Qnil,
5526 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5527 x_default_parameter (f, parms, Qcursor_type, Qbox,
5528 "cursorType", "CursorType", RES_TYPE_SYMBOL);
5530 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
5531 Change will not be effected unless different from the current
5532 FRAME_LINES (f). */
5533 width = FRAME_COLS (f);
5534 height = FRAME_LINES (f);
5535 FRAME_LINES (f) = 0;
5536 SET_FRAME_COLS (f, 0);
5537 change_frame_size (f, height, width, 1, 0, 0);
5539 /* Add `tooltip' frame parameter's default value. */
5540 if (NILP (Fframe_parameter (frame, intern ("tooltip"))))
5541 Fmodify_frame_parameters (frame, Fcons (Fcons (intern ("tooltip"), Qt),
5542 Qnil));
5544 /* Set up faces after all frame parameters are known. This call
5545 also merges in face attributes specified for new frames.
5547 Frame parameters may be changed if .Xdefaults contains
5548 specifications for the default font. For example, if there is an
5549 `Emacs.default.attributeBackground: pink', the `background-color'
5550 attribute of the frame get's set, which let's the internal border
5551 of the tooltip frame appear in pink. Prevent this. */
5553 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
5554 Lisp_Object fg = Fframe_parameter (frame, Qforeground_color);
5555 Lisp_Object colors = Qnil;
5557 /* Set tip_frame here, so that */
5558 tip_frame = frame;
5559 call2 (Qface_set_after_frame_default, frame, Qnil);
5561 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
5562 colors = Fcons (Fcons (Qbackground_color, bg), colors);
5563 if (!EQ (fg, Fframe_parameter (frame, Qforeground_color)))
5564 colors = Fcons (Fcons (Qforeground_color, fg), colors);
5566 if (!NILP (colors))
5567 Fmodify_frame_parameters (frame, colors);
5570 f->no_split = 1;
5572 UNGCPRO;
5574 /* It is now ok to make the frame official even if we get an error
5575 below. And the frame needs to be on Vframe_list or making it
5576 visible won't work. */
5577 Vframe_list = Fcons (frame, Vframe_list);
5579 /* Now that the frame is official, it counts as a reference to
5580 its display. */
5581 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
5583 /* Setting attributes of faces of the tooltip frame from resources
5584 and similar will increment face_change_count, which leads to the
5585 clearing of all current matrices. Since this isn't necessary
5586 here, avoid it by resetting face_change_count to the value it
5587 had before we created the tip frame. */
5588 face_change_count = face_change_count_before;
5590 /* Discard the unwind_protect. */
5591 return unbind_to (count, frame);
5595 /* Compute where to display tip frame F. PARMS is the list of frame
5596 parameters for F. DX and DY are specified offsets from the current
5597 location of the mouse. WIDTH and HEIGHT are the width and height
5598 of the tooltip. Return coordinates relative to the root window of
5599 the display in *ROOT_X, and *ROOT_Y. */
5601 static void
5602 compute_tip_xy (f, parms, dx, dy, width, height, root_x, root_y)
5603 struct frame *f;
5604 Lisp_Object parms, dx, dy;
5605 int width, height;
5606 int *root_x, *root_y;
5608 Lisp_Object left, top;
5609 int min_x, min_y, max_x, max_y;
5611 /* User-specified position? */
5612 left = Fcdr (Fassq (Qleft, parms));
5613 top = Fcdr (Fassq (Qtop, parms));
5615 /* Move the tooltip window where the mouse pointer is. Resize and
5616 show it. */
5617 if (!INTEGERP (left) || !INTEGERP (top))
5619 POINT pt;
5621 /* Default min and max values. */
5622 min_x = 0;
5623 min_y = 0;
5624 max_x = x_display_pixel_width (FRAME_W32_DISPLAY_INFO (f));
5625 max_y = x_display_pixel_height (FRAME_W32_DISPLAY_INFO (f));
5627 BLOCK_INPUT;
5628 GetCursorPos (&pt);
5629 *root_x = pt.x;
5630 *root_y = pt.y;
5631 UNBLOCK_INPUT;
5633 /* If multiple monitor support is available, constrain the tip onto
5634 the current monitor. This improves the above by allowing negative
5635 co-ordinates if monitor positions are such that they are valid, and
5636 snaps a tooltip onto a single monitor if we are close to the edge
5637 where it would otherwise flow onto the other monitor (or into
5638 nothingness if there is a gap in the overlap). */
5639 if (monitor_from_point_fn && get_monitor_info_fn)
5641 struct MONITOR_INFO info;
5642 HMONITOR monitor
5643 = monitor_from_point_fn (pt, MONITOR_DEFAULT_TO_NEAREST);
5644 info.cbSize = sizeof (info);
5646 if (get_monitor_info_fn (monitor, &info))
5648 min_x = info.rcWork.left;
5649 min_y = info.rcWork.top;
5650 max_x = info.rcWork.right;
5651 max_y = info.rcWork.bottom;
5656 if (INTEGERP (top))
5657 *root_y = XINT (top);
5658 else if (*root_y + XINT (dy) <= min_y)
5659 *root_y = min_y; /* Can happen for negative dy */
5660 else if (*root_y + XINT (dy) + height <= max_y)
5661 /* It fits below the pointer */
5662 *root_y += XINT (dy);
5663 else if (height + XINT (dy) + min_y <= *root_y)
5664 /* It fits above the pointer. */
5665 *root_y -= height + XINT (dy);
5666 else
5667 /* Put it on the top. */
5668 *root_y = min_y;
5670 if (INTEGERP (left))
5671 *root_x = XINT (left);
5672 else if (*root_x + XINT (dx) <= min_x)
5673 *root_x = 0; /* Can happen for negative dx */
5674 else if (*root_x + XINT (dx) + width <= max_x)
5675 /* It fits to the right of the pointer. */
5676 *root_x += XINT (dx);
5677 else if (width + XINT (dx) + min_x <= *root_x)
5678 /* It fits to the left of the pointer. */
5679 *root_x -= width + XINT (dx);
5680 else
5681 /* Put it left justified on the screen -- it ought to fit that way. */
5682 *root_x = min_x;
5686 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
5687 doc: /* Show STRING in a \"tooltip\" window on frame FRAME.
5688 A tooltip window is a small window displaying a string.
5690 This is an internal function; Lisp code should call `tooltip-show'.
5692 FRAME nil or omitted means use the selected frame.
5694 PARMS is an optional list of frame parameters which can be
5695 used to change the tooltip's appearance.
5697 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
5698 means use the default timeout of 5 seconds.
5700 If the list of frame parameters PARMS contains a `left' parameter,
5701 the tooltip is displayed at that x-position. Otherwise it is
5702 displayed at the mouse position, with offset DX added (default is 5 if
5703 DX isn't specified). Likewise for the y-position; if a `top' frame
5704 parameter is specified, it determines the y-position of the tooltip
5705 window, otherwise it is displayed at the mouse position, with offset
5706 DY added (default is -10).
5708 A tooltip's maximum size is specified by `x-max-tooltip-size'.
5709 Text larger than the specified size is clipped. */)
5710 (string, frame, parms, timeout, dx, dy)
5711 Lisp_Object string, frame, parms, timeout, dx, dy;
5713 struct frame *f;
5714 struct window *w;
5715 int root_x, root_y;
5716 struct buffer *old_buffer;
5717 struct text_pos pos;
5718 int i, width, height;
5719 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5720 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5721 int count = SPECPDL_INDEX ();
5723 specbind (Qinhibit_redisplay, Qt);
5725 GCPRO4 (string, parms, frame, timeout);
5727 CHECK_STRING (string);
5728 f = check_x_frame (frame);
5729 if (NILP (timeout))
5730 timeout = make_number (5);
5731 else
5732 CHECK_NATNUM (timeout);
5734 if (NILP (dx))
5735 dx = make_number (5);
5736 else
5737 CHECK_NUMBER (dx);
5739 if (NILP (dy))
5740 dy = make_number (-10);
5741 else
5742 CHECK_NUMBER (dy);
5744 if (NILP (last_show_tip_args))
5745 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
5747 if (!NILP (tip_frame))
5749 Lisp_Object last_string = AREF (last_show_tip_args, 0);
5750 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
5751 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
5753 if (EQ (frame, last_frame)
5754 && !NILP (Fequal (last_string, string))
5755 && !NILP (Fequal (last_parms, parms)))
5757 struct frame *f = XFRAME (tip_frame);
5759 /* Only DX and DY have changed. */
5760 if (!NILP (tip_timer))
5762 Lisp_Object timer = tip_timer;
5763 tip_timer = Qnil;
5764 call1 (Qcancel_timer, timer);
5767 BLOCK_INPUT;
5768 compute_tip_xy (f, parms, dx, dy, FRAME_PIXEL_WIDTH (f),
5769 FRAME_PIXEL_HEIGHT (f), &root_x, &root_y);
5771 /* Put tooltip in topmost group and in position. */
5772 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
5773 root_x, root_y, 0, 0,
5774 SWP_NOSIZE | SWP_NOACTIVATE);
5776 /* Ensure tooltip is on top of other topmost windows (eg menus). */
5777 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
5778 0, 0, 0, 0,
5779 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
5781 UNBLOCK_INPUT;
5782 goto start_timer;
5786 /* Hide a previous tip, if any. */
5787 Fx_hide_tip ();
5789 ASET (last_show_tip_args, 0, string);
5790 ASET (last_show_tip_args, 1, frame);
5791 ASET (last_show_tip_args, 2, parms);
5793 /* Add default values to frame parameters. */
5794 if (NILP (Fassq (Qname, parms)))
5795 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
5796 if (NILP (Fassq (Qinternal_border_width, parms)))
5797 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
5798 if (NILP (Fassq (Qborder_width, parms)))
5799 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
5800 if (NILP (Fassq (Qborder_color, parms)))
5801 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
5802 if (NILP (Fassq (Qbackground_color, parms)))
5803 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
5804 parms);
5806 /* Block input until the tip has been fully drawn, to avoid crashes
5807 when drawing tips in menus. */
5808 BLOCK_INPUT;
5810 /* Create a frame for the tooltip, and record it in the global
5811 variable tip_frame. */
5812 frame = x_create_tip_frame (FRAME_W32_DISPLAY_INFO (f), parms, string);
5813 f = XFRAME (frame);
5815 /* Set up the frame's root window. */
5816 w = XWINDOW (FRAME_ROOT_WINDOW (f));
5817 w->left_col = w->top_line = make_number (0);
5819 if (CONSP (Vx_max_tooltip_size)
5820 && INTEGERP (XCAR (Vx_max_tooltip_size))
5821 && XINT (XCAR (Vx_max_tooltip_size)) > 0
5822 && INTEGERP (XCDR (Vx_max_tooltip_size))
5823 && XINT (XCDR (Vx_max_tooltip_size)) > 0)
5825 w->total_cols = XCAR (Vx_max_tooltip_size);
5826 w->total_lines = XCDR (Vx_max_tooltip_size);
5828 else
5830 w->total_cols = make_number (80);
5831 w->total_lines = make_number (40);
5834 FRAME_TOTAL_COLS (f) = XINT (w->total_cols);
5835 adjust_glyphs (f);
5836 w->pseudo_window_p = 1;
5838 /* Display the tooltip text in a temporary buffer. */
5839 old_buffer = current_buffer;
5840 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->buffer));
5841 current_buffer->truncate_lines = Qnil;
5842 clear_glyph_matrix (w->desired_matrix);
5843 clear_glyph_matrix (w->current_matrix);
5844 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
5845 try_window (FRAME_ROOT_WINDOW (f), pos, 0);
5847 /* Compute width and height of the tooltip. */
5848 width = height = 0;
5849 for (i = 0; i < w->desired_matrix->nrows; ++i)
5851 struct glyph_row *row = &w->desired_matrix->rows[i];
5852 struct glyph *last;
5853 int row_width;
5855 /* Stop at the first empty row at the end. */
5856 if (!row->enabled_p || !row->displays_text_p)
5857 break;
5859 /* Let the row go over the full width of the frame. */
5860 row->full_width_p = 1;
5862 #ifdef TODO /* Investigate why some fonts need more width than is
5863 calculated for some tooltips. */
5864 /* There's a glyph at the end of rows that is use to place
5865 the cursor there. Don't include the width of this glyph. */
5866 if (row->used[TEXT_AREA])
5868 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5869 row_width = row->pixel_width - last->pixel_width;
5871 else
5872 #endif
5873 row_width = row->pixel_width;
5875 /* TODO: find why tips do not draw along baseline as instructed. */
5876 height += row->height;
5877 width = max (width, row_width);
5880 /* Add the frame's internal border to the width and height the X
5881 window should have. */
5882 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5883 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5885 /* Move the tooltip window where the mouse pointer is. Resize and
5886 show it. */
5887 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5890 /* Adjust Window size to take border into account. */
5891 RECT rect;
5892 rect.left = rect.top = 0;
5893 rect.right = width;
5894 rect.bottom = height;
5895 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
5896 FRAME_EXTERNAL_MENU_BAR (f));
5898 /* Position and size tooltip, and put it in the topmost group.
5899 The add-on of 3 to the 5th argument is a kludge: without it,
5900 some fonts cause the last character of the tip to be truncated,
5901 for some obscure reason. */
5902 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
5903 root_x, root_y, rect.right - rect.left + 3,
5904 rect.bottom - rect.top, SWP_NOACTIVATE);
5906 /* Ensure tooltip is on top of other topmost windows (eg menus). */
5907 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
5908 0, 0, 0, 0,
5909 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
5911 /* Let redisplay know that we have made the frame visible already. */
5912 f->async_visible = 1;
5914 ShowWindow (FRAME_W32_WINDOW (f), SW_SHOWNOACTIVATE);
5917 /* Draw into the window. */
5918 w->must_be_updated_p = 1;
5919 update_single_window (w, 1);
5921 UNBLOCK_INPUT;
5923 /* Restore original current buffer. */
5924 set_buffer_internal_1 (old_buffer);
5925 windows_or_buffers_changed = old_windows_or_buffers_changed;
5927 start_timer:
5928 /* Let the tip disappear after timeout seconds. */
5929 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
5930 intern ("x-hide-tip"));
5932 UNGCPRO;
5933 return unbind_to (count, Qnil);
5937 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
5938 doc: /* Hide the current tooltip window, if there is any.
5939 Value is t if tooltip was open, nil otherwise. */)
5942 int count;
5943 Lisp_Object deleted, frame, timer;
5944 struct gcpro gcpro1, gcpro2;
5946 /* Return quickly if nothing to do. */
5947 if (NILP (tip_timer) && NILP (tip_frame))
5948 return Qnil;
5950 frame = tip_frame;
5951 timer = tip_timer;
5952 GCPRO2 (frame, timer);
5953 tip_frame = tip_timer = deleted = Qnil;
5955 count = SPECPDL_INDEX ();
5956 specbind (Qinhibit_redisplay, Qt);
5957 specbind (Qinhibit_quit, Qt);
5959 if (!NILP (timer))
5960 call1 (Qcancel_timer, timer);
5962 if (FRAMEP (frame))
5964 delete_frame (frame, Qnil);
5965 deleted = Qt;
5968 UNGCPRO;
5969 return unbind_to (count, deleted);
5974 /***********************************************************************
5975 File selection dialog
5976 ***********************************************************************/
5977 extern Lisp_Object Qfile_name_history;
5979 /* Callback for altering the behavior of the Open File dialog.
5980 Makes the Filename text field contain "Current Directory" and be
5981 read-only when "Directories" is selected in the filter. This
5982 allows us to work around the fact that the standard Open File
5983 dialog does not support directories. */
5984 UINT CALLBACK
5985 file_dialog_callback (hwnd, msg, wParam, lParam)
5986 HWND hwnd;
5987 UINT msg;
5988 WPARAM wParam;
5989 LPARAM lParam;
5991 if (msg == WM_NOTIFY)
5993 OFNOTIFY * notify = (OFNOTIFY *)lParam;
5994 /* Detect when the Filter dropdown is changed. */
5995 if (notify->hdr.code == CDN_TYPECHANGE
5996 || notify->hdr.code == CDN_INITDONE)
5998 HWND dialog = GetParent (hwnd);
5999 HWND edit_control = GetDlgItem (dialog, FILE_NAME_TEXT_FIELD);
6001 /* Directories is in index 2. */
6002 if (notify->lpOFN->nFilterIndex == 2)
6004 CommDlg_OpenSave_SetControlText (dialog, FILE_NAME_TEXT_FIELD,
6005 "Current Directory");
6006 EnableWindow (edit_control, FALSE);
6008 else
6010 /* Don't override default filename on init done. */
6011 if (notify->hdr.code == CDN_TYPECHANGE)
6012 CommDlg_OpenSave_SetControlText (dialog,
6013 FILE_NAME_TEXT_FIELD, "");
6014 EnableWindow (edit_control, TRUE);
6018 return 0;
6021 /* Since we compile with _WIN32_WINNT set to 0x0400 (for NT4 compatibility)
6022 we end up with the old file dialogs. Define a big enough struct for the
6023 new dialog to trick GetOpenFileName into giving us the new dialogs on
6024 Windows 2000 and XP. */
6025 typedef struct
6027 OPENFILENAME real_details;
6028 void * pReserved;
6029 DWORD dwReserved;
6030 DWORD FlagsEx;
6031 } NEWOPENFILENAME;
6034 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
6035 doc: /* Read file name, prompting with PROMPT in directory DIR.
6036 Use a file selection dialog.
6037 Select DEFAULT-FILENAME in the dialog's file selection box, if
6038 specified. Ensure that file exists if MUSTMATCH is non-nil.
6039 If ONLY-DIR-P is non-nil, the user can only select directories. */)
6040 (prompt, dir, default_filename, mustmatch, only_dir_p)
6041 Lisp_Object prompt, dir, default_filename, mustmatch, only_dir_p;
6043 struct frame *f = SELECTED_FRAME ();
6044 Lisp_Object file = Qnil;
6045 int count = SPECPDL_INDEX ();
6046 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
6047 char filename[MAX_PATH + 1];
6048 char init_dir[MAX_PATH + 1];
6049 int default_filter_index = 1; /* 1: All Files, 2: Directories only */
6051 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
6052 CHECK_STRING (prompt);
6053 CHECK_STRING (dir);
6055 /* Create the dialog with PROMPT as title, using DIR as initial
6056 directory and using "*" as pattern. */
6057 dir = Fexpand_file_name (dir, Qnil);
6058 strncpy (init_dir, SDATA (ENCODE_FILE (dir)), MAX_PATH);
6059 init_dir[MAX_PATH] = '\0';
6060 unixtodos_filename (init_dir);
6062 if (STRINGP (default_filename))
6064 char *file_name_only;
6065 char *full_path_name = SDATA (ENCODE_FILE (default_filename));
6067 unixtodos_filename (full_path_name);
6069 file_name_only = strrchr (full_path_name, '\\');
6070 if (!file_name_only)
6071 file_name_only = full_path_name;
6072 else
6073 file_name_only++;
6075 strncpy (filename, file_name_only, MAX_PATH);
6076 filename[MAX_PATH] = '\0';
6078 else
6079 filename[0] = '\0';
6082 NEWOPENFILENAME new_file_details;
6083 BOOL file_opened = FALSE;
6084 OPENFILENAME * file_details = &new_file_details.real_details;
6086 /* Prevent redisplay. */
6087 specbind (Qinhibit_redisplay, Qt);
6088 BLOCK_INPUT;
6090 bzero (&new_file_details, sizeof (new_file_details));
6091 /* Apparently NT4 crashes if you give it an unexpected size.
6092 I'm not sure about Windows 9x, so play it safe. */
6093 if (w32_major_version > 4 && w32_major_version < 95)
6094 file_details->lStructSize = sizeof (NEWOPENFILENAME);
6095 else
6096 file_details->lStructSize = sizeof (OPENFILENAME);
6098 file_details->hwndOwner = FRAME_W32_WINDOW (f);
6099 /* Undocumented Bug in Common File Dialog:
6100 If a filter is not specified, shell links are not resolved. */
6101 file_details->lpstrFilter = "All Files (*.*)\0*.*\0Directories\0*|*\0\0";
6102 file_details->lpstrFile = filename;
6103 file_details->nMaxFile = sizeof (filename);
6104 file_details->lpstrInitialDir = init_dir;
6105 file_details->lpstrTitle = SDATA (prompt);
6107 if (! NILP (only_dir_p))
6108 default_filter_index = 2;
6110 file_details->nFilterIndex = default_filter_index;
6112 file_details->Flags = (OFN_HIDEREADONLY | OFN_NOCHANGEDIR
6113 | OFN_EXPLORER | OFN_ENABLEHOOK);
6114 if (!NILP (mustmatch))
6116 /* Require that the path to the parent directory exists. */
6117 file_details->Flags |= OFN_PATHMUSTEXIST;
6118 /* If we are looking for a file, require that it exists. */
6119 if (NILP (only_dir_p))
6120 file_details->Flags |= OFN_FILEMUSTEXIST;
6123 file_details->lpfnHook = (LPOFNHOOKPROC) file_dialog_callback;
6125 file_opened = GetOpenFileName (file_details);
6127 UNBLOCK_INPUT;
6129 if (file_opened)
6131 dostounix_filename (filename);
6133 if (file_details->nFilterIndex == 2)
6135 /* "Directories" selected - strip dummy file name. */
6136 char * last = strrchr (filename, '/');
6137 *last = '\0';
6140 file = DECODE_FILE (build_string (filename));
6142 /* User cancelled the dialog without making a selection. */
6143 else if (!CommDlgExtendedError ())
6144 file = Qnil;
6145 /* An error occurred, fallback on reading from the mini-buffer. */
6146 else
6147 file = Fcompleting_read (prompt, intern ("read-file-name-internal"),
6148 dir, mustmatch, dir, Qfile_name_history,
6149 default_filename, Qnil);
6151 file = unbind_to (count, file);
6154 UNGCPRO;
6156 /* Make "Cancel" equivalent to C-g. */
6157 if (NILP (file))
6158 Fsignal (Qquit, Qnil);
6160 return unbind_to (count, file);
6164 /* Moving files to the system recycle bin.
6165 Used by `move-file-to-trash' instead of the default moving to ~/.Trash */
6166 DEFUN ("system-move-file-to-trash", Fsystem_move_file_to_trash,
6167 Ssystem_move_file_to_trash, 1, 1, 0,
6168 doc: /* Move file or directory named FILENAME to the recycle bin. */)
6169 (filename)
6170 Lisp_Object filename;
6172 Lisp_Object handler;
6173 Lisp_Object encoded_file;
6174 Lisp_Object operation;
6176 operation = Qdelete_file;
6177 if (!NILP (Ffile_directory_p (filename))
6178 && NILP (Ffile_symlink_p (filename)))
6180 operation = Qdelete_directory;
6181 filename = Fdirectory_file_name (filename);
6183 filename = Fexpand_file_name (filename, Qnil);
6185 handler = Ffind_file_name_handler (filename, operation);
6186 if (!NILP (handler))
6187 return call2 (handler, operation, filename);
6189 encoded_file = ENCODE_FILE (filename);
6192 const char * path;
6193 SHFILEOPSTRUCT file_op;
6194 char tmp_path[MAX_PATH + 1];
6196 path = map_w32_filename (SDATA (encoded_file), NULL);
6198 /* On Windows, write permission is required to delete/move files. */
6199 _chmod (path, 0666);
6201 bzero (tmp_path, sizeof (tmp_path));
6202 strcpy (tmp_path, path);
6204 bzero (&file_op, sizeof (file_op));
6205 file_op.hwnd = HWND_DESKTOP;
6206 file_op.wFunc = FO_DELETE;
6207 file_op.pFrom = tmp_path;
6208 file_op.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_ALLOWUNDO
6209 | FOF_NOERRORUI | FOF_NO_CONNECTED_ELEMENTS;
6210 file_op.fAnyOperationsAborted = FALSE;
6212 if (SHFileOperation (&file_op) != 0)
6213 report_file_error ("Removing old name", list1 (filename));
6215 return Qnil;
6219 /***********************************************************************
6220 w32 specialized functions
6221 ***********************************************************************/
6223 DEFUN ("w32-send-sys-command", Fw32_send_sys_command,
6224 Sw32_send_sys_command, 1, 2, 0,
6225 doc: /* Send frame a Windows WM_SYSCOMMAND message of type COMMAND.
6226 Some useful values for COMMAND are #xf030 to maximize frame (#xf020
6227 to minimize), #xf120 to restore frame to original size, and #xf100
6228 to activate the menubar for keyboard access. #xf140 activates the
6229 screen saver if defined.
6231 If optional parameter FRAME is not specified, use selected frame. */)
6232 (command, frame)
6233 Lisp_Object command, frame;
6235 FRAME_PTR f = check_x_frame (frame);
6237 CHECK_NUMBER (command);
6239 PostMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, XINT (command), 0);
6241 return Qnil;
6244 DEFUN ("w32-shell-execute", Fw32_shell_execute, Sw32_shell_execute, 2, 4, 0,
6245 doc: /* Get Windows to perform OPERATION on DOCUMENT.
6246 This is a wrapper around the ShellExecute system function, which
6247 invokes the application registered to handle OPERATION for DOCUMENT.
6249 OPERATION is either nil or a string that names a supported operation.
6250 What operations can be used depends on the particular DOCUMENT and its
6251 handler application, but typically it is one of the following common
6252 operations:
6254 \"open\" - open DOCUMENT, which could be a file, a directory, or an
6255 executable program. If it is an application, that
6256 application is launched in the current buffer's default
6257 directory. Otherwise, the application associated with
6258 DOCUMENT is launched in the buffer's default directory.
6259 \"print\" - print DOCUMENT, which must be a file
6260 \"explore\" - start the Windows Explorer on DOCUMENT
6261 \"edit\" - launch an editor and open DOCUMENT for editing; which
6262 editor is launched depends on the association for the
6263 specified DOCUMENT
6264 \"find\" - initiate search starting from DOCUMENT which must specify
6265 a directory
6266 nil - invoke the default OPERATION, or \"open\" if default is
6267 not defined or unavailable
6269 DOCUMENT is typically the name of a document file or a URL, but can
6270 also be a program executable to run, or a directory to open in the
6271 Windows Explorer.
6273 If DOCUMENT is a program executable, the optional third arg PARAMETERS
6274 can be a string containing command line parameters that will be passed
6275 to the program; otherwise, PARAMETERS should be nil or unspecified.
6277 Optional fourth argument SHOW-FLAG can be used to control how the
6278 application will be displayed when it is invoked. If SHOW-FLAG is nil
6279 or unspecified, the application is displayed normally, otherwise it is
6280 an integer representing a ShowWindow flag:
6282 0 - start hidden
6283 1 - start normally
6284 3 - start maximized
6285 6 - start minimized */)
6286 (operation, document, parameters, show_flag)
6287 Lisp_Object operation, document, parameters, show_flag;
6289 Lisp_Object current_dir;
6291 CHECK_STRING (document);
6293 /* Encode filename, current directory and parameters. */
6294 current_dir = ENCODE_FILE (current_buffer->directory);
6295 document = ENCODE_FILE (document);
6296 if (STRINGP (parameters))
6297 parameters = ENCODE_SYSTEM (parameters);
6299 if ((int) ShellExecute (NULL,
6300 (STRINGP (operation) ?
6301 SDATA (operation) : NULL),
6302 SDATA (document),
6303 (STRINGP (parameters) ?
6304 SDATA (parameters) : NULL),
6305 SDATA (current_dir),
6306 (INTEGERP (show_flag) ?
6307 XINT (show_flag) : SW_SHOWDEFAULT))
6308 > 32)
6309 return Qt;
6310 error ("ShellExecute failed: %s", w32_strerror (0));
6313 /* Lookup virtual keycode from string representing the name of a
6314 non-ascii keystroke into the corresponding virtual key, using
6315 lispy_function_keys. */
6316 static int
6317 lookup_vk_code (char *key)
6319 int i;
6321 for (i = 0; i < 256; i++)
6322 if (lispy_function_keys[i]
6323 && strcmp (lispy_function_keys[i], key) == 0)
6324 return i;
6326 return -1;
6329 /* Convert a one-element vector style key sequence to a hot key
6330 definition. */
6331 static Lisp_Object
6332 w32_parse_hot_key (key)
6333 Lisp_Object key;
6335 /* Copied from Fdefine_key and store_in_keymap. */
6336 register Lisp_Object c;
6337 int vk_code;
6338 int lisp_modifiers;
6339 int w32_modifiers;
6340 struct gcpro gcpro1;
6342 CHECK_VECTOR (key);
6344 if (XFASTINT (Flength (key)) != 1)
6345 return Qnil;
6347 GCPRO1 (key);
6349 c = Faref (key, make_number (0));
6351 if (CONSP (c) && lucid_event_type_list_p (c))
6352 c = Fevent_convert_list (c);
6354 UNGCPRO;
6356 if (! INTEGERP (c) && ! SYMBOLP (c))
6357 error ("Key definition is invalid");
6359 /* Work out the base key and the modifiers. */
6360 if (SYMBOLP (c))
6362 c = parse_modifiers (c);
6363 lisp_modifiers = XINT (Fcar (Fcdr (c)));
6364 c = Fcar (c);
6365 if (!SYMBOLP (c))
6366 abort ();
6367 vk_code = lookup_vk_code (SDATA (SYMBOL_NAME (c)));
6369 else if (INTEGERP (c))
6371 lisp_modifiers = XINT (c) & ~CHARACTERBITS;
6372 /* Many ascii characters are their own virtual key code. */
6373 vk_code = XINT (c) & CHARACTERBITS;
6376 if (vk_code < 0 || vk_code > 255)
6377 return Qnil;
6379 if ((lisp_modifiers & meta_modifier) != 0
6380 && !NILP (Vw32_alt_is_meta))
6381 lisp_modifiers |= alt_modifier;
6383 /* Supply defs missing from mingw32. */
6384 #ifndef MOD_ALT
6385 #define MOD_ALT 0x0001
6386 #define MOD_CONTROL 0x0002
6387 #define MOD_SHIFT 0x0004
6388 #define MOD_WIN 0x0008
6389 #endif
6391 /* Convert lisp modifiers to Windows hot-key form. */
6392 w32_modifiers = (lisp_modifiers & hyper_modifier) ? MOD_WIN : 0;
6393 w32_modifiers |= (lisp_modifiers & alt_modifier) ? MOD_ALT : 0;
6394 w32_modifiers |= (lisp_modifiers & ctrl_modifier) ? MOD_CONTROL : 0;
6395 w32_modifiers |= (lisp_modifiers & shift_modifier) ? MOD_SHIFT : 0;
6397 return HOTKEY (vk_code, w32_modifiers);
6400 DEFUN ("w32-register-hot-key", Fw32_register_hot_key,
6401 Sw32_register_hot_key, 1, 1, 0,
6402 doc: /* Register KEY as a hot-key combination.
6403 Certain key combinations like Alt-Tab are reserved for system use on
6404 Windows, and therefore are normally intercepted by the system. However,
6405 most of these key combinations can be received by registering them as
6406 hot-keys, overriding their special meaning.
6408 KEY must be a one element key definition in vector form that would be
6409 acceptable to `define-key' (e.g. [A-tab] for Alt-Tab). The meta
6410 modifier is interpreted as Alt if `w32-alt-is-meta' is t, and hyper
6411 is always interpreted as the Windows modifier keys.
6413 The return value is the hotkey-id if registered, otherwise nil. */)
6414 (key)
6415 Lisp_Object key;
6417 key = w32_parse_hot_key (key);
6419 if (!NILP (key) && NILP (Fmemq (key, w32_grabbed_keys)))
6421 /* Reuse an empty slot if possible. */
6422 Lisp_Object item = Fmemq (Qnil, w32_grabbed_keys);
6424 /* Safe to add new key to list, even if we have focus. */
6425 if (NILP (item))
6426 w32_grabbed_keys = Fcons (key, w32_grabbed_keys);
6427 else
6428 XSETCAR (item, key);
6430 /* Notify input thread about new hot-key definition, so that it
6431 takes effect without needing to switch focus. */
6432 #ifdef USE_LISP_UNION_TYPE
6433 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
6434 (WPARAM) key.i, 0);
6435 #else
6436 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
6437 (WPARAM) key, 0);
6438 #endif
6441 return key;
6444 DEFUN ("w32-unregister-hot-key", Fw32_unregister_hot_key,
6445 Sw32_unregister_hot_key, 1, 1, 0,
6446 doc: /* Unregister KEY as a hot-key combination. */)
6447 (key)
6448 Lisp_Object key;
6450 Lisp_Object item;
6452 if (!INTEGERP (key))
6453 key = w32_parse_hot_key (key);
6455 item = Fmemq (key, w32_grabbed_keys);
6457 if (!NILP (item))
6459 /* Notify input thread about hot-key definition being removed, so
6460 that it takes effect without needing focus switch. */
6461 #ifdef USE_LISP_UNION_TYPE
6462 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
6463 (WPARAM) XINT (XCAR (item)), (LPARAM) item.i))
6464 #else
6465 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
6466 (WPARAM) XINT (XCAR (item)), (LPARAM) item))
6467 #endif
6469 MSG msg;
6470 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
6472 return Qt;
6474 return Qnil;
6477 DEFUN ("w32-registered-hot-keys", Fw32_registered_hot_keys,
6478 Sw32_registered_hot_keys, 0, 0, 0,
6479 doc: /* Return list of registered hot-key IDs. */)
6482 return Fdelq (Qnil, Fcopy_sequence (w32_grabbed_keys));
6485 DEFUN ("w32-reconstruct-hot-key", Fw32_reconstruct_hot_key,
6486 Sw32_reconstruct_hot_key, 1, 1, 0,
6487 doc: /* Convert hot-key ID to a lisp key combination.
6488 usage: (w32-reconstruct-hot-key ID) */)
6489 (hotkeyid)
6490 Lisp_Object hotkeyid;
6492 int vk_code, w32_modifiers;
6493 Lisp_Object key;
6495 CHECK_NUMBER (hotkeyid);
6497 vk_code = HOTKEY_VK_CODE (hotkeyid);
6498 w32_modifiers = HOTKEY_MODIFIERS (hotkeyid);
6500 if (vk_code < 256 && lispy_function_keys[vk_code])
6501 key = intern (lispy_function_keys[vk_code]);
6502 else
6503 key = make_number (vk_code);
6505 key = Fcons (key, Qnil);
6506 if (w32_modifiers & MOD_SHIFT)
6507 key = Fcons (Qshift, key);
6508 if (w32_modifiers & MOD_CONTROL)
6509 key = Fcons (Qctrl, key);
6510 if (w32_modifiers & MOD_ALT)
6511 key = Fcons (NILP (Vw32_alt_is_meta) ? Qalt : Qmeta, key);
6512 if (w32_modifiers & MOD_WIN)
6513 key = Fcons (Qhyper, key);
6515 return key;
6518 DEFUN ("w32-toggle-lock-key", Fw32_toggle_lock_key,
6519 Sw32_toggle_lock_key, 1, 2, 0,
6520 doc: /* Toggle the state of the lock key KEY.
6521 KEY can be `capslock', `kp-numlock', or `scroll'.
6522 If the optional parameter NEW-STATE is a number, then the state of KEY
6523 is set to off if the low bit of NEW-STATE is zero, otherwise on. */)
6524 (key, new_state)
6525 Lisp_Object key, new_state;
6527 int vk_code;
6529 if (EQ (key, intern ("capslock")))
6530 vk_code = VK_CAPITAL;
6531 else if (EQ (key, intern ("kp-numlock")))
6532 vk_code = VK_NUMLOCK;
6533 else if (EQ (key, intern ("scroll")))
6534 vk_code = VK_SCROLL;
6535 else
6536 return Qnil;
6538 if (!dwWindowsThreadId)
6539 return make_number (w32_console_toggle_lock_key (vk_code, new_state));
6541 #ifdef USE_LISP_UNION_TYPE
6542 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
6543 (WPARAM) vk_code, (LPARAM) new_state.i))
6544 #else
6545 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
6546 (WPARAM) vk_code, (LPARAM) new_state))
6547 #endif
6549 MSG msg;
6550 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
6551 return make_number (msg.wParam);
6553 return Qnil;
6556 DEFUN ("w32-window-exists-p", Fw32_window_exists_p, Sw32_window_exists_p,
6557 2, 2, 0,
6558 doc: /* Return non-nil if a window exists with the specified CLASS and NAME.
6560 This is a direct interface to the Windows API FindWindow function. */)
6561 (class, name)
6562 Lisp_Object class, name;
6564 HWND hnd;
6566 if (!NILP (class))
6567 CHECK_STRING (class);
6568 if (!NILP (name))
6569 CHECK_STRING (name);
6571 hnd = FindWindow (STRINGP (class) ? ((LPCTSTR) SDATA (class)) : NULL,
6572 STRINGP (name) ? ((LPCTSTR) SDATA (name)) : NULL);
6573 if (!hnd)
6574 return Qnil;
6575 return Qt;
6578 DEFUN ("w32-battery-status", Fw32_battery_status, Sw32_battery_status, 0, 0, 0,
6579 doc: /* Get power status information from Windows system.
6581 The following %-sequences are provided:
6582 %L AC line status (verbose)
6583 %B Battery status (verbose)
6584 %b Battery status, empty means high, `-' means low,
6585 `!' means critical, and `+' means charging
6586 %p Battery load percentage
6587 %s Remaining time (to charge or discharge) in seconds
6588 %m Remaining time (to charge or discharge) in minutes
6589 %h Remaining time (to charge or discharge) in hours
6590 %t Remaining time (to charge or discharge) in the form `h:min' */)
6593 Lisp_Object status = Qnil;
6595 SYSTEM_POWER_STATUS system_status;
6596 if (GetSystemPowerStatus (&system_status))
6598 Lisp_Object line_status, battery_status, battery_status_symbol;
6599 Lisp_Object load_percentage, seconds, minutes, hours, remain;
6600 Lisp_Object sequences[8];
6602 long seconds_left = (long) system_status.BatteryLifeTime;
6604 if (system_status.ACLineStatus == 0)
6605 line_status = build_string ("off-line");
6606 else if (system_status.ACLineStatus == 1)
6607 line_status = build_string ("on-line");
6608 else
6609 line_status = build_string ("N/A");
6611 if (system_status.BatteryFlag & 128)
6613 battery_status = build_string ("N/A");
6614 battery_status_symbol = empty_unibyte_string;
6616 else if (system_status.BatteryFlag & 8)
6618 battery_status = build_string ("charging");
6619 battery_status_symbol = build_string ("+");
6620 if (system_status.BatteryFullLifeTime != -1L)
6621 seconds_left = system_status.BatteryFullLifeTime - seconds_left;
6623 else if (system_status.BatteryFlag & 4)
6625 battery_status = build_string ("critical");
6626 battery_status_symbol = build_string ("!");
6628 else if (system_status.BatteryFlag & 2)
6630 battery_status = build_string ("low");
6631 battery_status_symbol = build_string ("-");
6633 else if (system_status.BatteryFlag & 1)
6635 battery_status = build_string ("high");
6636 battery_status_symbol = empty_unibyte_string;
6638 else
6640 battery_status = build_string ("medium");
6641 battery_status_symbol = empty_unibyte_string;
6644 if (system_status.BatteryLifePercent > 100)
6645 load_percentage = build_string ("N/A");
6646 else
6648 char buffer[16];
6649 _snprintf (buffer, 16, "%d", system_status.BatteryLifePercent);
6650 load_percentage = build_string (buffer);
6653 if (seconds_left < 0)
6654 seconds = minutes = hours = remain = build_string ("N/A");
6655 else
6657 long m;
6658 float h;
6659 char buffer[16];
6660 _snprintf (buffer, 16, "%ld", seconds_left);
6661 seconds = build_string (buffer);
6663 m = seconds_left / 60;
6664 _snprintf (buffer, 16, "%ld", m);
6665 minutes = build_string (buffer);
6667 h = seconds_left / 3600.0;
6668 _snprintf (buffer, 16, "%3.1f", h);
6669 hours = build_string (buffer);
6671 _snprintf (buffer, 16, "%ld:%02ld", m / 60, m % 60);
6672 remain = build_string (buffer);
6674 sequences[0] = Fcons (make_number ('L'), line_status);
6675 sequences[1] = Fcons (make_number ('B'), battery_status);
6676 sequences[2] = Fcons (make_number ('b'), battery_status_symbol);
6677 sequences[3] = Fcons (make_number ('p'), load_percentage);
6678 sequences[4] = Fcons (make_number ('s'), seconds);
6679 sequences[5] = Fcons (make_number ('m'), minutes);
6680 sequences[6] = Fcons (make_number ('h'), hours);
6681 sequences[7] = Fcons (make_number ('t'), remain);
6683 status = Flist (8, sequences);
6685 return status;
6689 DEFUN ("file-system-info", Ffile_system_info, Sfile_system_info, 1, 1, 0,
6690 doc: /* Return storage information about the file system FILENAME is on.
6691 Value is a list of floats (TOTAL FREE AVAIL), where TOTAL is the total
6692 storage of the file system, FREE is the free storage, and AVAIL is the
6693 storage available to a non-superuser. All 3 numbers are in bytes.
6694 If the underlying system call fails, value is nil. */)
6695 (filename)
6696 Lisp_Object filename;
6698 Lisp_Object encoded, value;
6700 CHECK_STRING (filename);
6701 filename = Fexpand_file_name (filename, Qnil);
6702 encoded = ENCODE_FILE (filename);
6704 value = Qnil;
6706 /* Determining the required information on Windows turns out, sadly,
6707 to be more involved than one would hope. The original Win32 api
6708 call for this will return bogus information on some systems, but we
6709 must dynamically probe for the replacement api, since that was
6710 added rather late on. */
6712 HMODULE hKernel = GetModuleHandle ("kernel32");
6713 BOOL (*pfn_GetDiskFreeSpaceEx)
6714 (char *, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER)
6715 = (void *) GetProcAddress (hKernel, "GetDiskFreeSpaceEx");
6717 /* On Windows, we may need to specify the root directory of the
6718 volume holding FILENAME. */
6719 char rootname[MAX_PATH];
6720 char *name = SDATA (encoded);
6722 /* find the root name of the volume if given */
6723 if (isalpha (name[0]) && name[1] == ':')
6725 rootname[0] = name[0];
6726 rootname[1] = name[1];
6727 rootname[2] = '\\';
6728 rootname[3] = 0;
6730 else if (IS_DIRECTORY_SEP (name[0]) && IS_DIRECTORY_SEP (name[1]))
6732 char *str = rootname;
6733 int slashes = 4;
6736 if (IS_DIRECTORY_SEP (*name) && --slashes == 0)
6737 break;
6738 *str++ = *name++;
6740 while ( *name );
6742 *str++ = '\\';
6743 *str = 0;
6746 if (pfn_GetDiskFreeSpaceEx)
6748 /* Unsigned large integers cannot be cast to double, so
6749 use signed ones instead. */
6750 LARGE_INTEGER availbytes;
6751 LARGE_INTEGER freebytes;
6752 LARGE_INTEGER totalbytes;
6754 if (pfn_GetDiskFreeSpaceEx (rootname,
6755 (ULARGE_INTEGER *)&availbytes,
6756 (ULARGE_INTEGER *)&totalbytes,
6757 (ULARGE_INTEGER *)&freebytes))
6758 value = list3 (make_float ((double) totalbytes.QuadPart),
6759 make_float ((double) freebytes.QuadPart),
6760 make_float ((double) availbytes.QuadPart));
6762 else
6764 DWORD sectors_per_cluster;
6765 DWORD bytes_per_sector;
6766 DWORD free_clusters;
6767 DWORD total_clusters;
6769 if (GetDiskFreeSpace (rootname,
6770 &sectors_per_cluster,
6771 &bytes_per_sector,
6772 &free_clusters,
6773 &total_clusters))
6774 value = list3 (make_float ((double) total_clusters
6775 * sectors_per_cluster * bytes_per_sector),
6776 make_float ((double) free_clusters
6777 * sectors_per_cluster * bytes_per_sector),
6778 make_float ((double) free_clusters
6779 * sectors_per_cluster * bytes_per_sector));
6783 return value;
6786 DEFUN ("default-printer-name", Fdefault_printer_name, Sdefault_printer_name,
6787 0, 0, 0, doc: /* Return the name of Windows default printer device. */)
6790 static char pname_buf[256];
6791 int err;
6792 HANDLE hPrn;
6793 PRINTER_INFO_2 *ppi2 = NULL;
6794 DWORD dwNeeded = 0, dwReturned = 0;
6796 /* Retrieve the default string from Win.ini (the registry).
6797 * String will be in form "printername,drivername,portname".
6798 * This is the most portable way to get the default printer. */
6799 if (GetProfileString ("windows", "device", ",,", pname_buf, sizeof (pname_buf)) <= 0)
6800 return Qnil;
6801 /* printername precedes first "," character */
6802 strtok (pname_buf, ",");
6803 /* We want to know more than the printer name */
6804 if (!OpenPrinter (pname_buf, &hPrn, NULL))
6805 return Qnil;
6806 GetPrinter (hPrn, 2, NULL, 0, &dwNeeded);
6807 if (dwNeeded == 0)
6809 ClosePrinter (hPrn);
6810 return Qnil;
6812 /* Allocate memory for the PRINTER_INFO_2 struct */
6813 ppi2 = (PRINTER_INFO_2 *) xmalloc (dwNeeded);
6814 if (!ppi2)
6816 ClosePrinter (hPrn);
6817 return Qnil;
6819 /* Call GetPrinter again with big enouth memory block */
6820 err = GetPrinter (hPrn, 2, (LPBYTE)ppi2, dwNeeded, &dwReturned);
6821 ClosePrinter (hPrn);
6822 if (!err)
6824 xfree (ppi2);
6825 return Qnil;
6828 if (ppi2)
6830 if (ppi2->Attributes & PRINTER_ATTRIBUTE_SHARED && ppi2->pServerName)
6832 /* a remote printer */
6833 if (*ppi2->pServerName == '\\')
6834 _snprintf (pname_buf, sizeof (pname_buf), "%s\\%s", ppi2->pServerName,
6835 ppi2->pShareName);
6836 else
6837 _snprintf (pname_buf, sizeof (pname_buf), "\\\\%s\\%s", ppi2->pServerName,
6838 ppi2->pShareName);
6839 pname_buf[sizeof (pname_buf) - 1] = '\0';
6841 else
6843 /* a local printer */
6844 strncpy (pname_buf, ppi2->pPortName, sizeof (pname_buf));
6845 pname_buf[sizeof (pname_buf) - 1] = '\0';
6846 /* `pPortName' can include several ports, delimited by ','.
6847 * we only use the first one. */
6848 strtok (pname_buf, ",");
6850 xfree (ppi2);
6853 return build_string (pname_buf);
6856 /***********************************************************************
6857 Initialization
6858 ***********************************************************************/
6860 /* Keep this list in the same order as frame_parms in frame.c.
6861 Use 0 for unsupported frame parameters. */
6863 frame_parm_handler w32_frame_parm_handlers[] =
6865 x_set_autoraise,
6866 x_set_autolower,
6867 x_set_background_color,
6868 x_set_border_color,
6869 x_set_border_width,
6870 x_set_cursor_color,
6871 x_set_cursor_type,
6872 x_set_font,
6873 x_set_foreground_color,
6874 x_set_icon_name,
6875 x_set_icon_type,
6876 x_set_internal_border_width,
6877 x_set_menu_bar_lines,
6878 x_set_mouse_color,
6879 x_explicitly_set_name,
6880 x_set_scroll_bar_width,
6881 x_set_title,
6882 x_set_unsplittable,
6883 x_set_vertical_scroll_bars,
6884 x_set_visibility,
6885 x_set_tool_bar_lines,
6886 0, /* x_set_scroll_bar_foreground, */
6887 0, /* x_set_scroll_bar_background, */
6888 x_set_screen_gamma,
6889 x_set_line_spacing,
6890 x_set_fringe_width,
6891 x_set_fringe_width,
6892 0, /* x_set_wait_for_wm, */
6893 x_set_fullscreen,
6894 x_set_font_backend,
6895 x_set_alpha
6898 void
6899 syms_of_w32fns ()
6901 globals_of_w32fns ();
6902 /* This is zero if not using MS-Windows. */
6903 w32_in_use = 0;
6904 track_mouse_window = NULL;
6906 w32_visible_system_caret_hwnd = NULL;
6908 DEFSYM (Qnone, "none");
6909 DEFSYM (Qsuppress_icon, "suppress-icon");
6910 DEFSYM (Qundefined_color, "undefined-color");
6911 DEFSYM (Qcancel_timer, "cancel-timer");
6912 DEFSYM (Qhyper, "hyper");
6913 DEFSYM (Qsuper, "super");
6914 DEFSYM (Qmeta, "meta");
6915 DEFSYM (Qalt, "alt");
6916 DEFSYM (Qctrl, "ctrl");
6917 DEFSYM (Qcontrol, "control");
6918 DEFSYM (Qshift, "shift");
6919 DEFSYM (Qfont_param, "font-parameter");
6920 /* This is the end of symbol initialization. */
6922 /* Text property `display' should be nonsticky by default. */
6923 Vtext_property_default_nonsticky
6924 = Fcons (Fcons (Qdisplay, Qt), Vtext_property_default_nonsticky);
6927 Fput (Qundefined_color, Qerror_conditions,
6928 Fcons (Qundefined_color, Fcons (Qerror, Qnil)));
6929 Fput (Qundefined_color, Qerror_message,
6930 build_string ("Undefined color"));
6932 staticpro (&w32_grabbed_keys);
6933 w32_grabbed_keys = Qnil;
6935 DEFVAR_LISP ("w32-color-map", &Vw32_color_map,
6936 doc: /* An array of color name mappings for Windows. */);
6937 Vw32_color_map = Qnil;
6939 DEFVAR_LISP ("w32-pass-alt-to-system", &Vw32_pass_alt_to_system,
6940 doc: /* Non-nil if Alt key presses are passed on to Windows.
6941 When non-nil, for example, Alt pressed and released and then space will
6942 open the System menu. When nil, Emacs processes the Alt key events, and
6943 then silently swallows them. */);
6944 Vw32_pass_alt_to_system = Qnil;
6946 DEFVAR_LISP ("w32-alt-is-meta", &Vw32_alt_is_meta,
6947 doc: /* Non-nil if the Alt key is to be considered the same as the META key.
6948 When nil, Emacs will translate the Alt key to the ALT modifier, not to META. */);
6949 Vw32_alt_is_meta = Qt;
6951 DEFVAR_INT ("w32-quit-key", &w32_quit_key,
6952 doc: /* If non-zero, the virtual key code for an alternative quit key. */);
6953 w32_quit_key = 0;
6955 DEFVAR_LISP ("w32-pass-lwindow-to-system",
6956 &Vw32_pass_lwindow_to_system,
6957 doc: /* If non-nil, the left \"Windows\" key is passed on to Windows.
6959 When non-nil, the Start menu is opened by tapping the key.
6960 If you set this to nil, the left \"Windows\" key is processed by Emacs
6961 according to the value of `w32-lwindow-modifier', which see.
6963 Note that some combinations of the left \"Windows\" key with other keys are
6964 caught by Windows at low level, and so binding them in Emacs will have no
6965 effect. For example, <lwindow>-r always pops up the Windows Run dialog,
6966 <lwindow>-<Pause> pops up the "System Properties" dialog, etc. However, see
6967 the doc string of `w32-phantom-key-code'. */);
6968 Vw32_pass_lwindow_to_system = Qt;
6970 DEFVAR_LISP ("w32-pass-rwindow-to-system",
6971 &Vw32_pass_rwindow_to_system,
6972 doc: /* If non-nil, the right \"Windows\" key is passed on to Windows.
6974 When non-nil, the Start menu is opened by tapping the key.
6975 If you set this to nil, the right \"Windows\" key is processed by Emacs
6976 according to the value of `w32-rwindow-modifier', which see.
6978 Note that some combinations of the right \"Windows\" key with other keys are
6979 caught by Windows at low level, and so binding them in Emacs will have no
6980 effect. For example, <rwindow>-r always pops up the Windows Run dialog,
6981 <rwindow>-<Pause> pops up the "System Properties" dialog, etc. However, see
6982 the doc string of `w32-phantom-key-code'. */);
6983 Vw32_pass_rwindow_to_system = Qt;
6985 DEFVAR_LISP ("w32-phantom-key-code",
6986 &Vw32_phantom_key_code,
6987 doc: /* Virtual key code used to generate \"phantom\" key presses.
6988 Value is a number between 0 and 255.
6990 Phantom key presses are generated in order to stop the system from
6991 acting on \"Windows\" key events when `w32-pass-lwindow-to-system' or
6992 `w32-pass-rwindow-to-system' is nil. */);
6993 /* Although 255 is technically not a valid key code, it works and
6994 means that this hack won't interfere with any real key code. */
6995 XSETINT (Vw32_phantom_key_code, 255);
6997 DEFVAR_LISP ("w32-enable-num-lock",
6998 &Vw32_enable_num_lock,
6999 doc: /* If non-nil, the Num Lock key acts normally.
7000 Set to nil to handle Num Lock as the `kp-numlock' key. */);
7001 Vw32_enable_num_lock = Qt;
7003 DEFVAR_LISP ("w32-enable-caps-lock",
7004 &Vw32_enable_caps_lock,
7005 doc: /* If non-nil, the Caps Lock key acts normally.
7006 Set to nil to handle Caps Lock as the `capslock' key. */);
7007 Vw32_enable_caps_lock = Qt;
7009 DEFVAR_LISP ("w32-scroll-lock-modifier",
7010 &Vw32_scroll_lock_modifier,
7011 doc: /* Modifier to use for the Scroll Lock ON state.
7012 The value can be hyper, super, meta, alt, control or shift for the
7013 respective modifier, or nil to handle Scroll Lock as the `scroll' key.
7014 Any other value will cause the Scroll Lock key to be ignored. */);
7015 Vw32_scroll_lock_modifier = Qt;
7017 DEFVAR_LISP ("w32-lwindow-modifier",
7018 &Vw32_lwindow_modifier,
7019 doc: /* Modifier to use for the left \"Windows\" key.
7020 The value can be hyper, super, meta, alt, control or shift for the
7021 respective modifier, or nil to appear as the `lwindow' key.
7022 Any other value will cause the key to be ignored. */);
7023 Vw32_lwindow_modifier = Qnil;
7025 DEFVAR_LISP ("w32-rwindow-modifier",
7026 &Vw32_rwindow_modifier,
7027 doc: /* Modifier to use for the right \"Windows\" key.
7028 The value can be hyper, super, meta, alt, control or shift for the
7029 respective modifier, or nil to appear as the `rwindow' key.
7030 Any other value will cause the key to be ignored. */);
7031 Vw32_rwindow_modifier = Qnil;
7033 DEFVAR_LISP ("w32-apps-modifier",
7034 &Vw32_apps_modifier,
7035 doc: /* Modifier to use for the \"Apps\" key.
7036 The value can be hyper, super, meta, alt, control or shift for the
7037 respective modifier, or nil to appear as the `apps' key.
7038 Any other value will cause the key to be ignored. */);
7039 Vw32_apps_modifier = Qnil;
7041 DEFVAR_BOOL ("w32-enable-synthesized-fonts", &w32_enable_synthesized_fonts,
7042 doc: /* Non-nil enables selection of artificially italicized and bold fonts. */);
7043 w32_enable_synthesized_fonts = 0;
7045 DEFVAR_LISP ("w32-enable-palette", &Vw32_enable_palette,
7046 doc: /* Non-nil enables Windows palette management to map colors exactly. */);
7047 Vw32_enable_palette = Qt;
7049 DEFVAR_INT ("w32-mouse-button-tolerance",
7050 &w32_mouse_button_tolerance,
7051 doc: /* Analogue of double click interval for faking middle mouse events.
7052 The value is the minimum time in milliseconds that must elapse between
7053 left and right button down events before they are considered distinct events.
7054 If both mouse buttons are depressed within this interval, a middle mouse
7055 button down event is generated instead. */);
7056 w32_mouse_button_tolerance = GetDoubleClickTime () / 2;
7058 DEFVAR_INT ("w32-mouse-move-interval",
7059 &w32_mouse_move_interval,
7060 doc: /* Minimum interval between mouse move events.
7061 The value is the minimum time in milliseconds that must elapse between
7062 successive mouse move (or scroll bar drag) events before they are
7063 reported as lisp events. */);
7064 w32_mouse_move_interval = 0;
7066 DEFVAR_BOOL ("w32-pass-extra-mouse-buttons-to-system",
7067 &w32_pass_extra_mouse_buttons_to_system,
7068 doc: /* If non-nil, the fourth and fifth mouse buttons are passed to Windows.
7069 Recent versions of Windows support mice with up to five buttons.
7070 Since most applications don't support these extra buttons, most mouse
7071 drivers will allow you to map them to functions at the system level.
7072 If this variable is non-nil, Emacs will pass them on, allowing the
7073 system to handle them. */);
7074 w32_pass_extra_mouse_buttons_to_system = 0;
7076 DEFVAR_BOOL ("w32-pass-multimedia-buttons-to-system",
7077 &w32_pass_multimedia_buttons_to_system,
7078 doc: /* If non-nil, media buttons are passed to Windows.
7079 Some modern keyboards contain buttons for controlling media players, web
7080 browsers and other applications. Generally these buttons are handled on a
7081 system wide basis, but by setting this to nil they are made available
7082 to Emacs for binding. Depending on your keyboard, additional keys that
7083 may be available are:
7085 browser-back, browser-forward, browser-refresh, browser-stop,
7086 browser-search, browser-favorites, browser-home,
7087 mail, mail-reply, mail-forward, mail-send,
7088 app-1, app-2,
7089 help, find, new, open, close, save, print, undo, redo, copy, cut, paste,
7090 spell-check, correction-list, toggle-dictate-command,
7091 media-next, media-previous, media-stop, media-play-pause, media-select,
7092 media-play, media-pause, media-record, media-fast-forward, media-rewind,
7093 media-channel-up, media-channel-down,
7094 volume-mute, volume-up, volume-down,
7095 mic-volume-mute, mic-volume-down, mic-volume-up, mic-toggle,
7096 bass-down, bass-boost, bass-up, treble-down, treble-up */);
7097 w32_pass_multimedia_buttons_to_system = 1;
7099 #if 0 /* TODO: Mouse cursor customization. */
7100 DEFVAR_LISP ("x-pointer-shape", &Vx_pointer_shape,
7101 doc: /* The shape of the pointer when over text.
7102 Changing the value does not affect existing frames
7103 unless you set the mouse color. */);
7104 Vx_pointer_shape = Qnil;
7106 Vx_nontext_pointer_shape = Qnil;
7108 Vx_mode_pointer_shape = Qnil;
7110 DEFVAR_LISP ("x-hourglass-pointer-shape", &Vx_hourglass_pointer_shape,
7111 doc: /* The shape of the pointer when Emacs is busy.
7112 This variable takes effect when you create a new frame
7113 or when you set the mouse color. */);
7114 Vx_hourglass_pointer_shape = Qnil;
7116 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
7117 &Vx_sensitive_text_pointer_shape,
7118 doc: /* The shape of the pointer when over mouse-sensitive text.
7119 This variable takes effect when you create a new frame
7120 or when you set the mouse color. */);
7121 Vx_sensitive_text_pointer_shape = Qnil;
7123 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
7124 &Vx_window_horizontal_drag_shape,
7125 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
7126 This variable takes effect when you create a new frame
7127 or when you set the mouse color. */);
7128 Vx_window_horizontal_drag_shape = Qnil;
7129 #endif
7131 DEFVAR_LISP ("x-cursor-fore-pixel", &Vx_cursor_fore_pixel,
7132 doc: /* A string indicating the foreground color of the cursor box. */);
7133 Vx_cursor_fore_pixel = Qnil;
7135 DEFVAR_LISP ("x-max-tooltip-size", &Vx_max_tooltip_size,
7136 doc: /* Maximum size for tooltips.
7137 Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
7138 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
7140 DEFVAR_LISP ("x-no-window-manager", &Vx_no_window_manager,
7141 doc: /* Non-nil if no window manager is in use.
7142 Emacs doesn't try to figure this out; this is always nil
7143 unless you set it to something else. */);
7144 /* We don't have any way to find this out, so set it to nil
7145 and maybe the user would like to set it to t. */
7146 Vx_no_window_manager = Qnil;
7148 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
7149 &Vx_pixel_size_width_font_regexp,
7150 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
7152 Since Emacs gets width of a font matching with this regexp from
7153 PIXEL_SIZE field of the name, font finding mechanism gets faster for
7154 such a font. This is especially effective for such large fonts as
7155 Chinese, Japanese, and Korean. */);
7156 Vx_pixel_size_width_font_regexp = Qnil;
7158 DEFVAR_LISP ("w32-bdf-filename-alist",
7159 &Vw32_bdf_filename_alist,
7160 doc: /* List of bdf fonts and their corresponding filenames. */);
7161 Vw32_bdf_filename_alist = Qnil;
7163 DEFVAR_BOOL ("w32-strict-fontnames",
7164 &w32_strict_fontnames,
7165 doc: /* Non-nil means only use fonts that are exact matches for those requested.
7166 Default is nil, which allows old fontnames that are not XLFD compliant,
7167 and allows third-party CJK display to work by specifying false charset
7168 fields to trick Emacs into translating to Big5, SJIS etc.
7169 Setting this to t will prevent wrong fonts being selected when
7170 fontsets are automatically created. */);
7171 w32_strict_fontnames = 0;
7173 DEFVAR_BOOL ("w32-strict-painting",
7174 &w32_strict_painting,
7175 doc: /* Non-nil means use strict rules for repainting frames.
7176 Set this to nil to get the old behavior for repainting; this should
7177 only be necessary if the default setting causes problems. */);
7178 w32_strict_painting = 1;
7180 #if 0 /* TODO: Port to W32 */
7181 defsubr (&Sx_change_window_property);
7182 defsubr (&Sx_delete_window_property);
7183 defsubr (&Sx_window_property);
7184 #endif
7185 defsubr (&Sxw_display_color_p);
7186 defsubr (&Sx_display_grayscale_p);
7187 defsubr (&Sxw_color_defined_p);
7188 defsubr (&Sxw_color_values);
7189 defsubr (&Sx_server_max_request_size);
7190 defsubr (&Sx_server_vendor);
7191 defsubr (&Sx_server_version);
7192 defsubr (&Sx_display_pixel_width);
7193 defsubr (&Sx_display_pixel_height);
7194 defsubr (&Sx_display_mm_width);
7195 defsubr (&Sx_display_mm_height);
7196 defsubr (&Sx_display_screens);
7197 defsubr (&Sx_display_planes);
7198 defsubr (&Sx_display_color_cells);
7199 defsubr (&Sx_display_visual_class);
7200 defsubr (&Sx_display_backing_store);
7201 defsubr (&Sx_display_save_under);
7202 defsubr (&Sx_create_frame);
7203 defsubr (&Sx_open_connection);
7204 defsubr (&Sx_close_connection);
7205 defsubr (&Sx_display_list);
7206 defsubr (&Sx_synchronize);
7207 defsubr (&Sx_focus_frame);
7209 /* W32 specific functions */
7211 defsubr (&Sw32_define_rgb_color);
7212 defsubr (&Sw32_default_color_map);
7213 defsubr (&Sw32_send_sys_command);
7214 defsubr (&Sw32_shell_execute);
7215 defsubr (&Sw32_register_hot_key);
7216 defsubr (&Sw32_unregister_hot_key);
7217 defsubr (&Sw32_registered_hot_keys);
7218 defsubr (&Sw32_reconstruct_hot_key);
7219 defsubr (&Sw32_toggle_lock_key);
7220 defsubr (&Sw32_window_exists_p);
7221 defsubr (&Sw32_battery_status);
7223 defsubr (&Sfile_system_info);
7224 defsubr (&Sdefault_printer_name);
7226 check_window_system_func = check_w32;
7229 hourglass_timer = 0;
7230 hourglass_hwnd = NULL;
7232 defsubr (&Sx_show_tip);
7233 defsubr (&Sx_hide_tip);
7234 tip_timer = Qnil;
7235 staticpro (&tip_timer);
7236 tip_frame = Qnil;
7237 staticpro (&tip_frame);
7239 last_show_tip_args = Qnil;
7240 staticpro (&last_show_tip_args);
7242 defsubr (&Sx_file_dialog);
7243 defsubr (&Ssystem_move_file_to_trash);
7248 globals_of_w32fns is used to initialize those global variables that
7249 must always be initialized on startup even when the global variable
7250 initialized is non zero (see the function main in emacs.c).
7251 globals_of_w32fns is called from syms_of_w32fns when the global
7252 variable initialized is 0 and directly from main when initialized
7253 is non zero.
7255 void
7256 globals_of_w32fns ()
7258 HMODULE user32_lib = GetModuleHandle ("user32.dll");
7260 TrackMouseEvent not available in all versions of Windows, so must load
7261 it dynamically. Do it once, here, instead of every time it is used.
7263 track_mouse_event_fn = (TrackMouseEvent_Proc)
7264 GetProcAddress (user32_lib, "TrackMouseEvent");
7265 /* ditto for GetClipboardSequenceNumber. */
7266 clipboard_sequence_fn = (ClipboardSequence_Proc)
7267 GetProcAddress (user32_lib, "GetClipboardSequenceNumber");
7269 monitor_from_point_fn = (MonitorFromPoint_Proc)
7270 GetProcAddress (user32_lib, "MonitorFromPoint");
7271 get_monitor_info_fn = (GetMonitorInfo_Proc)
7272 GetProcAddress (user32_lib, "GetMonitorInfoA");
7275 HMODULE imm32_lib = GetModuleHandle ("imm32.dll");
7276 get_composition_string_fn = (ImmGetCompositionString_Proc)
7277 GetProcAddress (imm32_lib, "ImmGetCompositionStringW");
7278 get_ime_context_fn = (ImmGetContext_Proc)
7279 GetProcAddress (imm32_lib, "ImmGetContext");
7281 DEFVAR_INT ("w32-ansi-code-page",
7282 &w32_ansi_code_page,
7283 doc: /* The ANSI code page used by the system. */);
7284 w32_ansi_code_page = GetACP ();
7286 /* MessageBox does not work without this when linked to comctl32.dll 6.0. */
7287 InitCommonControls ();
7289 syms_of_w32uniscribe ();
7292 #undef abort
7294 void
7295 w32_abort ()
7297 int button;
7298 button = MessageBox (NULL,
7299 "A fatal error has occurred!\n\n"
7300 "Would you like to attach a debugger?\n\n"
7301 "Select YES to debug, NO to abort Emacs"
7302 #if __GNUC__
7303 "\n\n(type \"gdb -p <emacs-PID>\" and\n"
7304 "\"continue\" inside GDB before clicking YES.)"
7305 #endif
7306 , "Emacs Abort Dialog",
7307 MB_ICONEXCLAMATION | MB_TASKMODAL
7308 | MB_SETFOREGROUND | MB_YESNO);
7309 switch (button)
7311 case IDYES:
7312 DebugBreak ();
7313 exit (2); /* tell the compiler we will never return */
7314 case IDNO:
7315 default:
7316 abort ();
7317 break;
7321 /* For convenience when debugging. */
7323 w32_last_error ()
7325 return GetLastError ();
7328 /* arch-tag: 707589ab-b9be-4638-8cdd-74629cc9b446
7329 (do not change this comment) */