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