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