Fix typo in previous comment.
[bpt/emacs.git] / src / w32fns.c
1 /* Graphical user interface functions for the Microsoft W32 API.
2
3 Copyright (C) 1989, 1992-2011 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, VS 6 does not have this in its headers. */
144 #if defined (_MSC_VER) && _MSC_VER < 1300
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 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 DEFUN ("w32-default-color-map", Fw32_default_color_map, Sw32_default_color_map,
639 0, 0, 0, doc: /* Return the default color map. */)
640 (void)
641 {
642 int i;
643 colormap_t *pc = w32_color_map;
644 Lisp_Object cmap;
645
646 BLOCK_INPUT;
647
648 cmap = Qnil;
649
650 for (i = 0; i < sizeof (w32_color_map) / sizeof (w32_color_map[0]);
651 pc++, i++)
652 cmap = Fcons (Fcons (build_string (pc->name),
653 make_number (pc->colorref)),
654 cmap);
655
656 UNBLOCK_INPUT;
657
658 return (cmap);
659 }
660
661 static Lisp_Object
662 w32_color_map_lookup (char *colorname)
663 {
664 Lisp_Object tail, ret = Qnil;
665
666 BLOCK_INPUT;
667
668 for (tail = Vw32_color_map; CONSP (tail); tail = XCDR (tail))
669 {
670 register Lisp_Object elt, tem;
671
672 elt = XCAR (tail);
673 if (!CONSP (elt)) continue;
674
675 tem = Fcar (elt);
676
677 if (lstrcmpi (SDATA (tem), colorname) == 0)
678 {
679 ret = Fcdr (elt);
680 break;
681 }
682
683 QUIT;
684 }
685
686
687 UNBLOCK_INPUT;
688
689 return ret;
690 }
691
692
693 static void
694 add_system_logical_colors_to_map (Lisp_Object *system_colors)
695 {
696 HKEY colors_key;
697
698 /* Other registry operations are done with input blocked. */
699 BLOCK_INPUT;
700
701 /* Look for "Control Panel/Colors" under User and Machine registry
702 settings. */
703 if (RegOpenKeyEx (HKEY_CURRENT_USER, "Control Panel\\Colors", 0,
704 KEY_READ, &colors_key) == ERROR_SUCCESS
705 || RegOpenKeyEx (HKEY_LOCAL_MACHINE, "Control Panel\\Colors", 0,
706 KEY_READ, &colors_key) == ERROR_SUCCESS)
707 {
708 /* List all keys. */
709 char color_buffer[64];
710 char full_name_buffer[MAX_PATH + SYSTEM_COLOR_PREFIX_LEN];
711 int index = 0;
712 DWORD name_size, color_size;
713 char *name_buffer = full_name_buffer + SYSTEM_COLOR_PREFIX_LEN;
714
715 name_size = sizeof (full_name_buffer) - SYSTEM_COLOR_PREFIX_LEN;
716 color_size = sizeof (color_buffer);
717
718 strcpy (full_name_buffer, SYSTEM_COLOR_PREFIX);
719
720 while (RegEnumValueA (colors_key, index, name_buffer, &name_size,
721 NULL, NULL, color_buffer, &color_size)
722 == ERROR_SUCCESS)
723 {
724 int r, g, b;
725 if (sscanf (color_buffer, " %u %u %u", &r, &g, &b) == 3)
726 *system_colors = Fcons (Fcons (build_string (full_name_buffer),
727 make_number (RGB (r, g, b))),
728 *system_colors);
729
730 name_size = sizeof (full_name_buffer) - SYSTEM_COLOR_PREFIX_LEN;
731 color_size = sizeof (color_buffer);
732 index++;
733 }
734 RegCloseKey (colors_key);
735 }
736
737 UNBLOCK_INPUT;
738 }
739
740
741 static Lisp_Object
742 x_to_w32_color (char * colorname)
743 {
744 register Lisp_Object ret = Qnil;
745
746 BLOCK_INPUT;
747
748 if (colorname[0] == '#')
749 {
750 /* Could be an old-style RGB Device specification. */
751 char *color;
752 int size;
753 color = colorname + 1;
754
755 size = strlen (color);
756 if (size == 3 || size == 6 || size == 9 || size == 12)
757 {
758 UINT colorval;
759 int i, pos;
760 pos = 0;
761 size /= 3;
762 colorval = 0;
763
764 for (i = 0; i < 3; i++)
765 {
766 char *end;
767 char t;
768 unsigned long value;
769
770 /* The check for 'x' in the following conditional takes into
771 account the fact that strtol allows a "0x" in front of
772 our numbers, and we don't. */
773 if (!isxdigit (color[0]) || color[1] == 'x')
774 break;
775 t = color[size];
776 color[size] = '\0';
777 value = strtoul (color, &end, 16);
778 color[size] = t;
779 if (errno == ERANGE || end - color != size)
780 break;
781 switch (size)
782 {
783 case 1:
784 value = value * 0x10;
785 break;
786 case 2:
787 break;
788 case 3:
789 value /= 0x10;
790 break;
791 case 4:
792 value /= 0x100;
793 break;
794 }
795 colorval |= (value << pos);
796 pos += 0x8;
797 if (i == 2)
798 {
799 UNBLOCK_INPUT;
800 XSETINT (ret, colorval);
801 return ret;
802 }
803 color = end;
804 }
805 }
806 }
807 else if (strnicmp (colorname, "rgb:", 4) == 0)
808 {
809 char *color;
810 UINT colorval;
811 int i, pos;
812 pos = 0;
813
814 colorval = 0;
815 color = colorname + 4;
816 for (i = 0; i < 3; i++)
817 {
818 char *end;
819 unsigned long value;
820
821 /* The check for 'x' in the following conditional takes into
822 account the fact that strtol allows a "0x" in front of
823 our numbers, and we don't. */
824 if (!isxdigit (color[0]) || color[1] == 'x')
825 break;
826 value = strtoul (color, &end, 16);
827 if (errno == ERANGE)
828 break;
829 switch (end - color)
830 {
831 case 1:
832 value = value * 0x10 + value;
833 break;
834 case 2:
835 break;
836 case 3:
837 value /= 0x10;
838 break;
839 case 4:
840 value /= 0x100;
841 break;
842 default:
843 value = ULONG_MAX;
844 }
845 if (value == ULONG_MAX)
846 break;
847 colorval |= (value << pos);
848 pos += 0x8;
849 if (i == 2)
850 {
851 if (*end != '\0')
852 break;
853 UNBLOCK_INPUT;
854 XSETINT (ret, colorval);
855 return ret;
856 }
857 if (*end != '/')
858 break;
859 color = end + 1;
860 }
861 }
862 else if (strnicmp (colorname, "rgbi:", 5) == 0)
863 {
864 /* This is an RGB Intensity specification. */
865 char *color;
866 UINT colorval;
867 int i, pos;
868 pos = 0;
869
870 colorval = 0;
871 color = colorname + 5;
872 for (i = 0; i < 3; i++)
873 {
874 char *end;
875 double value;
876 UINT val;
877
878 value = strtod (color, &end);
879 if (errno == ERANGE)
880 break;
881 if (value < 0.0 || value > 1.0)
882 break;
883 val = (UINT)(0x100 * value);
884 /* We used 0x100 instead of 0xFF to give a continuous
885 range between 0.0 and 1.0 inclusive. The next statement
886 fixes the 1.0 case. */
887 if (val == 0x100)
888 val = 0xFF;
889 colorval |= (val << pos);
890 pos += 0x8;
891 if (i == 2)
892 {
893 if (*end != '\0')
894 break;
895 UNBLOCK_INPUT;
896 XSETINT (ret, colorval);
897 return ret;
898 }
899 if (*end != '/')
900 break;
901 color = end + 1;
902 }
903 }
904 /* I am not going to attempt to handle any of the CIE color schemes
905 or TekHVC, since I don't know the algorithms for conversion to
906 RGB. */
907
908 /* If we fail to lookup the color name in w32_color_map, then check the
909 colorname to see if it can be crudely approximated: If the X color
910 ends in a number (e.g., "darkseagreen2"), strip the number and
911 return the result of looking up the base color name. */
912 ret = w32_color_map_lookup (colorname);
913 if (NILP (ret))
914 {
915 int len = strlen (colorname);
916
917 if (isdigit (colorname[len - 1]))
918 {
919 char *ptr, *approx = alloca (len + 1);
920
921 strcpy (approx, colorname);
922 ptr = &approx[len - 1];
923 while (ptr > approx && isdigit (*ptr))
924 *ptr-- = '\0';
925
926 ret = w32_color_map_lookup (approx);
927 }
928 }
929
930 UNBLOCK_INPUT;
931 return ret;
932 }
933
934 void
935 w32_regenerate_palette (FRAME_PTR f)
936 {
937 struct w32_palette_entry * list;
938 LOGPALETTE * log_palette;
939 HPALETTE new_palette;
940 int i;
941
942 /* don't bother trying to create palette if not supported */
943 if (! FRAME_W32_DISPLAY_INFO (f)->has_palette)
944 return;
945
946 log_palette = (LOGPALETTE *)
947 alloca (sizeof (LOGPALETTE) +
948 FRAME_W32_DISPLAY_INFO (f)->num_colors * sizeof (PALETTEENTRY));
949 log_palette->palVersion = 0x300;
950 log_palette->palNumEntries = FRAME_W32_DISPLAY_INFO (f)->num_colors;
951
952 list = FRAME_W32_DISPLAY_INFO (f)->color_list;
953 for (i = 0;
954 i < FRAME_W32_DISPLAY_INFO (f)->num_colors;
955 i++, list = list->next)
956 log_palette->palPalEntry[i] = list->entry;
957
958 new_palette = CreatePalette (log_palette);
959
960 enter_crit ();
961
962 if (FRAME_W32_DISPLAY_INFO (f)->palette)
963 DeleteObject (FRAME_W32_DISPLAY_INFO (f)->palette);
964 FRAME_W32_DISPLAY_INFO (f)->palette = new_palette;
965
966 /* Realize display palette and garbage all frames. */
967 release_frame_dc (f, get_frame_dc (f));
968
969 leave_crit ();
970 }
971
972 #define W32_COLOR(pe) RGB (pe.peRed, pe.peGreen, pe.peBlue)
973 #define SET_W32_COLOR(pe, color) \
974 do \
975 { \
976 pe.peRed = GetRValue (color); \
977 pe.peGreen = GetGValue (color); \
978 pe.peBlue = GetBValue (color); \
979 pe.peFlags = 0; \
980 } while (0)
981
982 #if 0
983 /* Keep these around in case we ever want to track color usage. */
984 void
985 w32_map_color (FRAME_PTR f, COLORREF color)
986 {
987 struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
988
989 if (NILP (Vw32_enable_palette))
990 return;
991
992 /* check if color is already mapped */
993 while (list)
994 {
995 if (W32_COLOR (list->entry) == color)
996 {
997 ++list->refcount;
998 return;
999 }
1000 list = list->next;
1001 }
1002
1003 /* not already mapped, so add to list and recreate Windows palette */
1004 list = (struct w32_palette_entry *)
1005 xmalloc (sizeof (struct w32_palette_entry));
1006 SET_W32_COLOR (list->entry, color);
1007 list->refcount = 1;
1008 list->next = FRAME_W32_DISPLAY_INFO (f)->color_list;
1009 FRAME_W32_DISPLAY_INFO (f)->color_list = list;
1010 FRAME_W32_DISPLAY_INFO (f)->num_colors++;
1011
1012 /* set flag that palette must be regenerated */
1013 FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
1014 }
1015
1016 void
1017 w32_unmap_color (FRAME_PTR f, COLORREF color)
1018 {
1019 struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
1020 struct w32_palette_entry **prev = &FRAME_W32_DISPLAY_INFO (f)->color_list;
1021
1022 if (NILP (Vw32_enable_palette))
1023 return;
1024
1025 /* check if color is already mapped */
1026 while (list)
1027 {
1028 if (W32_COLOR (list->entry) == color)
1029 {
1030 if (--list->refcount == 0)
1031 {
1032 *prev = list->next;
1033 xfree (list);
1034 FRAME_W32_DISPLAY_INFO (f)->num_colors--;
1035 break;
1036 }
1037 else
1038 return;
1039 }
1040 prev = &list->next;
1041 list = list->next;
1042 }
1043
1044 /* set flag that palette must be regenerated */
1045 FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
1046 }
1047 #endif
1048
1049
1050 /* Gamma-correct COLOR on frame F. */
1051
1052 void
1053 gamma_correct (struct frame *f, COLORREF *color)
1054 {
1055 if (f->gamma)
1056 {
1057 *color = PALETTERGB (
1058 pow (GetRValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
1059 pow (GetGValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
1060 pow (GetBValue (*color) / 255.0, f->gamma) * 255.0 + 0.5);
1061 }
1062 }
1063
1064
1065 /* Decide if color named COLOR is valid for the display associated with
1066 the selected frame; if so, return the rgb values in COLOR_DEF.
1067 If ALLOC is nonzero, allocate a new colormap cell. */
1068
1069 int
1070 w32_defined_color (FRAME_PTR f, char *color, XColor *color_def, int alloc)
1071 {
1072 register Lisp_Object tem;
1073 COLORREF w32_color_ref;
1074
1075 tem = x_to_w32_color (color);
1076
1077 if (!NILP (tem))
1078 {
1079 if (f)
1080 {
1081 /* Apply gamma correction. */
1082 w32_color_ref = XUINT (tem);
1083 gamma_correct (f, &w32_color_ref);
1084 XSETINT (tem, w32_color_ref);
1085 }
1086
1087 /* Map this color to the palette if it is enabled. */
1088 if (!NILP (Vw32_enable_palette))
1089 {
1090 struct w32_palette_entry * entry =
1091 one_w32_display_info.color_list;
1092 struct w32_palette_entry ** prev =
1093 &one_w32_display_info.color_list;
1094
1095 /* check if color is already mapped */
1096 while (entry)
1097 {
1098 if (W32_COLOR (entry->entry) == XUINT (tem))
1099 break;
1100 prev = &entry->next;
1101 entry = entry->next;
1102 }
1103
1104 if (entry == NULL && alloc)
1105 {
1106 /* not already mapped, so add to list */
1107 entry = (struct w32_palette_entry *)
1108 xmalloc (sizeof (struct w32_palette_entry));
1109 SET_W32_COLOR (entry->entry, XUINT (tem));
1110 entry->next = NULL;
1111 *prev = entry;
1112 one_w32_display_info.num_colors++;
1113
1114 /* set flag that palette must be regenerated */
1115 one_w32_display_info.regen_palette = TRUE;
1116 }
1117 }
1118 /* Ensure COLORREF value is snapped to nearest color in (default)
1119 palette by simulating the PALETTERGB macro. This works whether
1120 or not the display device has a palette. */
1121 w32_color_ref = XUINT (tem) | 0x2000000;
1122
1123 color_def->pixel = w32_color_ref;
1124 color_def->red = GetRValue (w32_color_ref) * 256;
1125 color_def->green = GetGValue (w32_color_ref) * 256;
1126 color_def->blue = GetBValue (w32_color_ref) * 256;
1127
1128 return 1;
1129 }
1130 else
1131 {
1132 return 0;
1133 }
1134 }
1135
1136 /* Given a string ARG naming a color, compute a pixel value from it
1137 suitable for screen F.
1138 If F is not a color screen, return DEF (default) regardless of what
1139 ARG says. */
1140
1141 int
1142 x_decode_color (FRAME_PTR f, Lisp_Object arg, int def)
1143 {
1144 XColor cdef;
1145
1146 CHECK_STRING (arg);
1147
1148 if (strcmp (SDATA (arg), "black") == 0)
1149 return BLACK_PIX_DEFAULT (f);
1150 else if (strcmp (SDATA (arg), "white") == 0)
1151 return WHITE_PIX_DEFAULT (f);
1152
1153 if ((FRAME_W32_DISPLAY_INFO (f)->n_planes * FRAME_W32_DISPLAY_INFO (f)->n_cbits) == 1)
1154 return def;
1155
1156 /* w32_defined_color is responsible for coping with failures
1157 by looking for a near-miss. */
1158 if (w32_defined_color (f, SDATA (arg), &cdef, 1))
1159 return cdef.pixel;
1160
1161 /* defined_color failed; return an ultimate default. */
1162 return def;
1163 }
1164 \f
1165
1166
1167 /* Functions called only from `x_set_frame_param'
1168 to set individual parameters.
1169
1170 If FRAME_W32_WINDOW (f) is 0,
1171 the frame is being created and its window does not exist yet.
1172 In that case, just record the parameter's new value
1173 in the standard place; do not attempt to change the window. */
1174
1175 void
1176 x_set_foreground_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1177 {
1178 struct w32_output *x = f->output_data.w32;
1179 PIX_TYPE fg, old_fg;
1180
1181 fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1182 old_fg = FRAME_FOREGROUND_PIXEL (f);
1183 FRAME_FOREGROUND_PIXEL (f) = fg;
1184
1185 if (FRAME_W32_WINDOW (f) != 0)
1186 {
1187 if (x->cursor_pixel == old_fg)
1188 {
1189 x->cursor_pixel = fg;
1190 x->cursor_gc->background = fg;
1191 }
1192
1193 update_face_from_frame_parameter (f, Qforeground_color, arg);
1194 if (FRAME_VISIBLE_P (f))
1195 redraw_frame (f);
1196 }
1197 }
1198
1199 void
1200 x_set_background_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1201 {
1202 FRAME_BACKGROUND_PIXEL (f)
1203 = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
1204
1205 if (FRAME_W32_WINDOW (f) != 0)
1206 {
1207 SetWindowLong (FRAME_W32_WINDOW (f), WND_BACKGROUND_INDEX,
1208 FRAME_BACKGROUND_PIXEL (f));
1209
1210 update_face_from_frame_parameter (f, Qbackground_color, arg);
1211
1212 if (FRAME_VISIBLE_P (f))
1213 redraw_frame (f);
1214 }
1215 }
1216
1217 void
1218 x_set_mouse_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1219 {
1220 Cursor cursor, nontext_cursor, mode_cursor, hand_cursor;
1221 int count;
1222 int mask_color;
1223
1224 if (!EQ (Qnil, arg))
1225 f->output_data.w32->mouse_pixel
1226 = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1227 mask_color = FRAME_BACKGROUND_PIXEL (f);
1228
1229 /* Don't let pointers be invisible. */
1230 if (mask_color == f->output_data.w32->mouse_pixel
1231 && mask_color == FRAME_BACKGROUND_PIXEL (f))
1232 f->output_data.w32->mouse_pixel = FRAME_FOREGROUND_PIXEL (f);
1233
1234 #if 0 /* TODO : Mouse cursor customization. */
1235 BLOCK_INPUT;
1236
1237 /* It's not okay to crash if the user selects a screwy cursor. */
1238 count = x_catch_errors (FRAME_W32_DISPLAY (f));
1239
1240 if (!EQ (Qnil, Vx_pointer_shape))
1241 {
1242 CHECK_NUMBER (Vx_pointer_shape);
1243 cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XINT (Vx_pointer_shape));
1244 }
1245 else
1246 cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
1247 x_check_errors (FRAME_W32_DISPLAY (f), "bad text pointer cursor: %s");
1248
1249 if (!EQ (Qnil, Vx_nontext_pointer_shape))
1250 {
1251 CHECK_NUMBER (Vx_nontext_pointer_shape);
1252 nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1253 XINT (Vx_nontext_pointer_shape));
1254 }
1255 else
1256 nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_left_ptr);
1257 x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
1258
1259 if (!EQ (Qnil, Vx_hourglass_pointer_shape))
1260 {
1261 CHECK_NUMBER (Vx_hourglass_pointer_shape);
1262 hourglass_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1263 XINT (Vx_hourglass_pointer_shape));
1264 }
1265 else
1266 hourglass_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_watch);
1267 x_check_errors (FRAME_W32_DISPLAY (f), "bad busy pointer cursor: %s");
1268
1269 x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
1270 if (!EQ (Qnil, Vx_mode_pointer_shape))
1271 {
1272 CHECK_NUMBER (Vx_mode_pointer_shape);
1273 mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1274 XINT (Vx_mode_pointer_shape));
1275 }
1276 else
1277 mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
1278 x_check_errors (FRAME_W32_DISPLAY (f), "bad modeline pointer cursor: %s");
1279
1280 if (!EQ (Qnil, Vx_sensitive_text_pointer_shape))
1281 {
1282 CHECK_NUMBER (Vx_sensitive_text_pointer_shape);
1283 hand_cursor
1284 = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1285 XINT (Vx_sensitive_text_pointer_shape));
1286 }
1287 else
1288 hand_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_crosshair);
1289
1290 if (!NILP (Vx_window_horizontal_drag_shape))
1291 {
1292 CHECK_NUMBER (Vx_window_horizontal_drag_shape);
1293 horizontal_drag_cursor
1294 = XCreateFontCursor (FRAME_X_DISPLAY (f),
1295 XINT (Vx_window_horizontal_drag_shape));
1296 }
1297 else
1298 horizontal_drag_cursor
1299 = XCreateFontCursor (FRAME_X_DISPLAY (f), XC_sb_h_double_arrow);
1300
1301 /* Check and report errors with the above calls. */
1302 x_check_errors (FRAME_W32_DISPLAY (f), "can't set cursor shape: %s");
1303 x_uncatch_errors (FRAME_W32_DISPLAY (f), count);
1304
1305 {
1306 XColor fore_color, back_color;
1307
1308 fore_color.pixel = f->output_data.w32->mouse_pixel;
1309 back_color.pixel = mask_color;
1310 XQueryColor (FRAME_W32_DISPLAY (f),
1311 DefaultColormap (FRAME_W32_DISPLAY (f),
1312 DefaultScreen (FRAME_W32_DISPLAY (f))),
1313 &fore_color);
1314 XQueryColor (FRAME_W32_DISPLAY (f),
1315 DefaultColormap (FRAME_W32_DISPLAY (f),
1316 DefaultScreen (FRAME_W32_DISPLAY (f))),
1317 &back_color);
1318 XRecolorCursor (FRAME_W32_DISPLAY (f), cursor,
1319 &fore_color, &back_color);
1320 XRecolorCursor (FRAME_W32_DISPLAY (f), nontext_cursor,
1321 &fore_color, &back_color);
1322 XRecolorCursor (FRAME_W32_DISPLAY (f), mode_cursor,
1323 &fore_color, &back_color);
1324 XRecolorCursor (FRAME_W32_DISPLAY (f), hand_cursor,
1325 &fore_color, &back_color);
1326 XRecolorCursor (FRAME_W32_DISPLAY (f), hourglass_cursor,
1327 &fore_color, &back_color);
1328 }
1329
1330 if (FRAME_W32_WINDOW (f) != 0)
1331 XDefineCursor (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), cursor);
1332
1333 if (cursor != f->output_data.w32->text_cursor && f->output_data.w32->text_cursor != 0)
1334 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->text_cursor);
1335 f->output_data.w32->text_cursor = cursor;
1336
1337 if (nontext_cursor != f->output_data.w32->nontext_cursor
1338 && f->output_data.w32->nontext_cursor != 0)
1339 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->nontext_cursor);
1340 f->output_data.w32->nontext_cursor = nontext_cursor;
1341
1342 if (hourglass_cursor != f->output_data.w32->hourglass_cursor
1343 && f->output_data.w32->hourglass_cursor != 0)
1344 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->hourglass_cursor);
1345 f->output_data.w32->hourglass_cursor = hourglass_cursor;
1346
1347 if (mode_cursor != f->output_data.w32->modeline_cursor
1348 && f->output_data.w32->modeline_cursor != 0)
1349 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->modeline_cursor);
1350 f->output_data.w32->modeline_cursor = mode_cursor;
1351
1352 if (hand_cursor != f->output_data.w32->hand_cursor
1353 && f->output_data.w32->hand_cursor != 0)
1354 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->hand_cursor);
1355 f->output_data.w32->hand_cursor = hand_cursor;
1356
1357 XFlush (FRAME_W32_DISPLAY (f));
1358 UNBLOCK_INPUT;
1359
1360 update_face_from_frame_parameter (f, Qmouse_color, arg);
1361 #endif /* TODO */
1362 }
1363
1364 void
1365 x_set_cursor_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1366 {
1367 unsigned long fore_pixel, pixel;
1368
1369 if (!NILP (Vx_cursor_fore_pixel))
1370 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
1371 WHITE_PIX_DEFAULT (f));
1372 else
1373 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
1374
1375 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1376
1377 /* Make sure that the cursor color differs from the background color. */
1378 if (pixel == FRAME_BACKGROUND_PIXEL (f))
1379 {
1380 pixel = f->output_data.w32->mouse_pixel;
1381 if (pixel == fore_pixel)
1382 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
1383 }
1384
1385 f->output_data.w32->cursor_foreground_pixel = fore_pixel;
1386 f->output_data.w32->cursor_pixel = pixel;
1387
1388 if (FRAME_W32_WINDOW (f) != 0)
1389 {
1390 BLOCK_INPUT;
1391 /* Update frame's cursor_gc. */
1392 f->output_data.w32->cursor_gc->foreground = fore_pixel;
1393 f->output_data.w32->cursor_gc->background = pixel;
1394
1395 UNBLOCK_INPUT;
1396
1397 if (FRAME_VISIBLE_P (f))
1398 {
1399 x_update_cursor (f, 0);
1400 x_update_cursor (f, 1);
1401 }
1402 }
1403
1404 update_face_from_frame_parameter (f, Qcursor_color, arg);
1405 }
1406
1407 /* Set the border-color of frame F to pixel value PIX.
1408 Note that this does not fully take effect if done before
1409 F has a window. */
1410
1411 void
1412 x_set_border_pixel (struct frame *f, int pix)
1413 {
1414
1415 f->output_data.w32->border_pixel = pix;
1416
1417 if (FRAME_W32_WINDOW (f) != 0 && f->border_width > 0)
1418 {
1419 if (FRAME_VISIBLE_P (f))
1420 redraw_frame (f);
1421 }
1422 }
1423
1424 /* Set the border-color of frame F to value described by ARG.
1425 ARG can be a string naming a color.
1426 The border-color is used for the border that is drawn by the server.
1427 Note that this does not fully take effect if done before
1428 F has a window; it must be redone when the window is created. */
1429
1430 void
1431 x_set_border_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1432 {
1433 int pix;
1434
1435 CHECK_STRING (arg);
1436 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1437 x_set_border_pixel (f, pix);
1438 update_face_from_frame_parameter (f, Qborder_color, arg);
1439 }
1440
1441
1442 void
1443 x_set_cursor_type (FRAME_PTR f, Lisp_Object arg, Lisp_Object oldval)
1444 {
1445 set_frame_cursor_types (f, arg);
1446
1447 /* Make sure the cursor gets redrawn. */
1448 cursor_type_changed = 1;
1449 }
1450 \f
1451 void
1452 x_set_icon_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1453 {
1454 int result;
1455
1456 if (NILP (arg) && NILP (oldval))
1457 return;
1458
1459 if (STRINGP (arg) && STRINGP (oldval)
1460 && EQ (Fstring_equal (oldval, arg), Qt))
1461 return;
1462
1463 if (SYMBOLP (arg) && SYMBOLP (oldval) && EQ (arg, oldval))
1464 return;
1465
1466 BLOCK_INPUT;
1467
1468 result = x_bitmap_icon (f, arg);
1469 if (result)
1470 {
1471 UNBLOCK_INPUT;
1472 error ("No icon window available");
1473 }
1474
1475 UNBLOCK_INPUT;
1476 }
1477
1478 void
1479 x_set_icon_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1480 {
1481 if (STRINGP (arg))
1482 {
1483 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1484 return;
1485 }
1486 else if (!NILP (arg) || NILP (oldval))
1487 return;
1488
1489 f->icon_name = arg;
1490
1491 #if 0
1492 if (f->output_data.w32->icon_bitmap != 0)
1493 return;
1494
1495 BLOCK_INPUT;
1496
1497 result = x_text_icon (f,
1498 SSDATA ((!NILP (f->icon_name)
1499 ? f->icon_name
1500 : !NILP (f->title)
1501 ? f->title
1502 : f->name)));
1503
1504 if (result)
1505 {
1506 UNBLOCK_INPUT;
1507 error ("No icon window available");
1508 }
1509
1510 /* If the window was unmapped (and its icon was mapped),
1511 the new icon is not mapped, so map the window in its stead. */
1512 if (FRAME_VISIBLE_P (f))
1513 {
1514 #ifdef USE_X_TOOLKIT
1515 XtPopup (f->output_data.w32->widget, XtGrabNone);
1516 #endif
1517 XMapWindow (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f));
1518 }
1519
1520 XFlush (FRAME_W32_DISPLAY (f));
1521 UNBLOCK_INPUT;
1522 #endif
1523 }
1524
1525 \f
1526 void
1527 x_set_menu_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1528 {
1529 int nlines;
1530
1531 /* Right now, menu bars don't work properly in minibuf-only frames;
1532 most of the commands try to apply themselves to the minibuffer
1533 frame itself, and get an error because you can't switch buffers
1534 in or split the minibuffer window. */
1535 if (FRAME_MINIBUF_ONLY_P (f))
1536 return;
1537
1538 if (INTEGERP (value))
1539 nlines = XINT (value);
1540 else
1541 nlines = 0;
1542
1543 FRAME_MENU_BAR_LINES (f) = 0;
1544 if (nlines)
1545 FRAME_EXTERNAL_MENU_BAR (f) = 1;
1546 else
1547 {
1548 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
1549 free_frame_menubar (f);
1550 FRAME_EXTERNAL_MENU_BAR (f) = 0;
1551
1552 /* Adjust the frame size so that the client (text) dimensions
1553 remain the same. This depends on FRAME_EXTERNAL_MENU_BAR being
1554 set correctly. */
1555 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
1556 do_pending_window_change (0);
1557 }
1558 adjust_glyphs (f);
1559 }
1560
1561
1562 /* Set the number of lines used for the tool bar of frame F to VALUE.
1563 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
1564 is the old number of tool bar lines. This function changes the
1565 height of all windows on frame F to match the new tool bar height.
1566 The frame's height doesn't change. */
1567
1568 void
1569 x_set_tool_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1570 {
1571 int delta, nlines, root_height;
1572 Lisp_Object root_window;
1573
1574 /* Treat tool bars like menu bars. */
1575 if (FRAME_MINIBUF_ONLY_P (f))
1576 return;
1577
1578 /* Use VALUE only if an integer >= 0. */
1579 if (INTEGERP (value) && XINT (value) >= 0)
1580 nlines = XFASTINT (value);
1581 else
1582 nlines = 0;
1583
1584 /* Make sure we redisplay all windows in this frame. */
1585 ++windows_or_buffers_changed;
1586
1587 delta = nlines - FRAME_TOOL_BAR_LINES (f);
1588
1589 /* Don't resize the tool-bar to more than we have room for. */
1590 root_window = FRAME_ROOT_WINDOW (f);
1591 root_height = WINDOW_TOTAL_LINES (XWINDOW (root_window));
1592 if (root_height - delta < 1)
1593 {
1594 delta = root_height - 1;
1595 nlines = FRAME_TOOL_BAR_LINES (f) + delta;
1596 }
1597
1598 FRAME_TOOL_BAR_LINES (f) = nlines;
1599 change_window_heights (root_window, delta);
1600 adjust_glyphs (f);
1601
1602 /* We also have to make sure that the internal border at the top of
1603 the frame, below the menu bar or tool bar, is redrawn when the
1604 tool bar disappears. This is so because the internal border is
1605 below the tool bar if one is displayed, but is below the menu bar
1606 if there isn't a tool bar. The tool bar draws into the area
1607 below the menu bar. */
1608 if (FRAME_W32_WINDOW (f) && FRAME_TOOL_BAR_LINES (f) == 0)
1609 {
1610 clear_frame (f);
1611 clear_current_matrices (f);
1612 }
1613
1614 /* If the tool bar gets smaller, the internal border below it
1615 has to be cleared. It was formerly part of the display
1616 of the larger tool bar, and updating windows won't clear it. */
1617 if (delta < 0)
1618 {
1619 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
1620 int width = FRAME_PIXEL_WIDTH (f);
1621 int y = nlines * FRAME_LINE_HEIGHT (f);
1622
1623 BLOCK_INPUT;
1624 {
1625 HDC hdc = get_frame_dc (f);
1626 w32_clear_area (f, hdc, 0, y, width, height);
1627 release_frame_dc (f, hdc);
1628 }
1629 UNBLOCK_INPUT;
1630
1631 if (WINDOWP (f->tool_bar_window))
1632 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
1633 }
1634 }
1635
1636
1637 /* Change the name of frame F to NAME. If NAME is nil, set F's name to
1638 w32_id_name.
1639
1640 If EXPLICIT is non-zero, that indicates that lisp code is setting the
1641 name; if NAME is a string, set F's name to NAME and set
1642 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1643
1644 If EXPLICIT is zero, that indicates that Emacs redisplay code is
1645 suggesting a new name, which lisp code should override; if
1646 F->explicit_name is set, ignore the new name; otherwise, set it. */
1647
1648 void
1649 x_set_name (struct frame *f, Lisp_Object name, int explicit)
1650 {
1651 /* Make sure that requests from lisp code override requests from
1652 Emacs redisplay code. */
1653 if (explicit)
1654 {
1655 /* If we're switching from explicit to implicit, we had better
1656 update the mode lines and thereby update the title. */
1657 if (f->explicit_name && NILP (name))
1658 update_mode_lines = 1;
1659
1660 f->explicit_name = ! NILP (name);
1661 }
1662 else if (f->explicit_name)
1663 return;
1664
1665 /* If NAME is nil, set the name to the w32_id_name. */
1666 if (NILP (name))
1667 {
1668 /* Check for no change needed in this very common case
1669 before we do any consing. */
1670 if (!strcmp (FRAME_W32_DISPLAY_INFO (f)->w32_id_name,
1671 SDATA (f->name)))
1672 return;
1673 name = build_string (FRAME_W32_DISPLAY_INFO (f)->w32_id_name);
1674 }
1675 else
1676 CHECK_STRING (name);
1677
1678 /* Don't change the name if it's already NAME. */
1679 if (! NILP (Fstring_equal (name, f->name)))
1680 return;
1681
1682 f->name = name;
1683
1684 /* For setting the frame title, the title parameter should override
1685 the name parameter. */
1686 if (! NILP (f->title))
1687 name = f->title;
1688
1689 if (FRAME_W32_WINDOW (f))
1690 {
1691 if (STRING_MULTIBYTE (name))
1692 name = ENCODE_SYSTEM (name);
1693
1694 BLOCK_INPUT;
1695 SetWindowText (FRAME_W32_WINDOW (f), SDATA (name));
1696 UNBLOCK_INPUT;
1697 }
1698 }
1699
1700 /* This function should be called when the user's lisp code has
1701 specified a name for the frame; the name will override any set by the
1702 redisplay code. */
1703 void
1704 x_explicitly_set_name (FRAME_PTR f, Lisp_Object arg, Lisp_Object oldval)
1705 {
1706 x_set_name (f, arg, 1);
1707 }
1708
1709 /* This function should be called by Emacs redisplay code to set the
1710 name; names set this way will never override names set by the user's
1711 lisp code. */
1712 void
1713 x_implicitly_set_name (FRAME_PTR f, Lisp_Object arg, Lisp_Object oldval)
1714 {
1715 x_set_name (f, arg, 0);
1716 }
1717 \f
1718 /* Change the title of frame F to NAME.
1719 If NAME is nil, use the frame name as the title. */
1720
1721 void
1722 x_set_title (struct frame *f, Lisp_Object name, Lisp_Object old_name)
1723 {
1724 /* Don't change the title if it's already NAME. */
1725 if (EQ (name, f->title))
1726 return;
1727
1728 update_mode_lines = 1;
1729
1730 f->title = name;
1731
1732 if (NILP (name))
1733 name = f->name;
1734
1735 if (FRAME_W32_WINDOW (f))
1736 {
1737 if (STRING_MULTIBYTE (name))
1738 name = ENCODE_SYSTEM (name);
1739
1740 BLOCK_INPUT;
1741 SetWindowText (FRAME_W32_WINDOW (f), SDATA (name));
1742 UNBLOCK_INPUT;
1743 }
1744 }
1745
1746 void
1747 x_set_scroll_bar_default_width (struct frame *f)
1748 {
1749 int wid = FRAME_COLUMN_WIDTH (f);
1750
1751 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = GetSystemMetrics (SM_CXVSCROLL);
1752 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) +
1753 wid - 1) / wid;
1754 }
1755
1756 \f
1757 /* Subroutines for creating a frame. */
1758
1759 Cursor
1760 w32_load_cursor (LPCTSTR name)
1761 {
1762 /* Try first to load cursor from application resource. */
1763 Cursor cursor = LoadImage ((HINSTANCE) GetModuleHandle (NULL),
1764 name, IMAGE_CURSOR, 0, 0,
1765 LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_SHARED);
1766 if (!cursor)
1767 {
1768 /* Then try to load a shared predefined cursor. */
1769 cursor = LoadImage (NULL, name, IMAGE_CURSOR, 0, 0,
1770 LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_SHARED);
1771 }
1772 return cursor;
1773 }
1774
1775 static LRESULT CALLBACK w32_wnd_proc (HWND, UINT, WPARAM, LPARAM);
1776
1777 static BOOL
1778 w32_init_class (HINSTANCE hinst)
1779 {
1780 WNDCLASS wc;
1781
1782 wc.style = CS_HREDRAW | CS_VREDRAW;
1783 wc.lpfnWndProc = (WNDPROC) w32_wnd_proc;
1784 wc.cbClsExtra = 0;
1785 wc.cbWndExtra = WND_EXTRA_BYTES;
1786 wc.hInstance = hinst;
1787 wc.hIcon = LoadIcon (hinst, EMACS_CLASS);
1788 wc.hCursor = w32_load_cursor (IDC_ARROW);
1789 wc.hbrBackground = NULL; /* GetStockObject (WHITE_BRUSH); */
1790 wc.lpszMenuName = NULL;
1791 wc.lpszClassName = EMACS_CLASS;
1792
1793 return (RegisterClass (&wc));
1794 }
1795
1796 static HWND
1797 w32_createscrollbar (struct frame *f, struct scroll_bar * bar)
1798 {
1799 return (CreateWindow ("SCROLLBAR", "", SBS_VERT | WS_CHILD | WS_VISIBLE,
1800 /* Position and size of scroll bar. */
1801 XINT (bar->left) + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
1802 XINT (bar->top),
1803 XINT (bar->width) - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
1804 XINT (bar->height),
1805 FRAME_W32_WINDOW (f),
1806 NULL,
1807 hinst,
1808 NULL));
1809 }
1810
1811 static void
1812 w32_createwindow (struct frame *f)
1813 {
1814 HWND hwnd;
1815 RECT rect;
1816 Lisp_Object top = Qunbound;
1817 Lisp_Object left = Qunbound;
1818 struct w32_display_info *dpyinfo = &one_w32_display_info;
1819
1820 rect.left = rect.top = 0;
1821 rect.right = FRAME_PIXEL_WIDTH (f);
1822 rect.bottom = FRAME_PIXEL_HEIGHT (f);
1823
1824 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
1825 FRAME_EXTERNAL_MENU_BAR (f));
1826
1827 /* Do first time app init */
1828
1829 if (!hprevinst)
1830 {
1831 w32_init_class (hinst);
1832 }
1833
1834 if (f->size_hint_flags & USPosition || f->size_hint_flags & PPosition)
1835 {
1836 XSETINT (left, f->left_pos);
1837 XSETINT (top, f->top_pos);
1838 }
1839 else if (EQ (left, Qunbound) && EQ (top, Qunbound))
1840 {
1841 /* When called with RES_TYPE_NUMBER, w32_get_arg will return zero
1842 for anything that is not a number and is not Qunbound. */
1843 left = x_get_arg (dpyinfo, Qnil, Qleft, "left", "Left", RES_TYPE_NUMBER);
1844 top = x_get_arg (dpyinfo, Qnil, Qtop, "top", "Top", RES_TYPE_NUMBER);
1845 }
1846
1847 FRAME_W32_WINDOW (f) = hwnd
1848 = CreateWindow (EMACS_CLASS,
1849 f->namebuf,
1850 f->output_data.w32->dwStyle | WS_CLIPCHILDREN,
1851 EQ (left, Qunbound) ? CW_USEDEFAULT : XINT (left),
1852 EQ (top, Qunbound) ? CW_USEDEFAULT : XINT (top),
1853 rect.right - rect.left,
1854 rect.bottom - rect.top,
1855 NULL,
1856 NULL,
1857 hinst,
1858 NULL);
1859
1860 if (hwnd)
1861 {
1862 SetWindowLong (hwnd, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
1863 SetWindowLong (hwnd, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
1864 SetWindowLong (hwnd, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
1865 SetWindowLong (hwnd, WND_SCROLLBAR_INDEX, f->scroll_bar_actual_width);
1866 SetWindowLong (hwnd, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
1867
1868 /* Enable drag-n-drop. */
1869 DragAcceptFiles (hwnd, TRUE);
1870
1871 /* Do this to discard the default setting specified by our parent. */
1872 ShowWindow (hwnd, SW_HIDE);
1873
1874 /* Update frame positions. */
1875 GetWindowRect (hwnd, &rect);
1876 f->left_pos = rect.left;
1877 f->top_pos = rect.top;
1878 }
1879 }
1880
1881 static void
1882 my_post_msg (W32Msg * wmsg, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
1883 {
1884 wmsg->msg.hwnd = hwnd;
1885 wmsg->msg.message = msg;
1886 wmsg->msg.wParam = wParam;
1887 wmsg->msg.lParam = lParam;
1888 wmsg->msg.time = GetMessageTime ();
1889
1890 post_msg (wmsg);
1891 }
1892
1893 /* GetKeyState and MapVirtualKey on Windows 95 do not actually distinguish
1894 between left and right keys as advertised. We test for this
1895 support dynamically, and set a flag when the support is absent. If
1896 absent, we keep track of the left and right control and alt keys
1897 ourselves. This is particularly necessary on keyboards that rely
1898 upon the AltGr key, which is represented as having the left control
1899 and right alt keys pressed. For these keyboards, we need to know
1900 when the left alt key has been pressed in addition to the AltGr key
1901 so that we can properly support M-AltGr-key sequences (such as M-@
1902 on Swedish keyboards). */
1903
1904 #define EMACS_LCONTROL 0
1905 #define EMACS_RCONTROL 1
1906 #define EMACS_LMENU 2
1907 #define EMACS_RMENU 3
1908
1909 static int modifiers[4];
1910 static int modifiers_recorded;
1911 static int modifier_key_support_tested;
1912
1913 static void
1914 test_modifier_support (unsigned int wparam)
1915 {
1916 unsigned int l, r;
1917
1918 if (wparam != VK_CONTROL && wparam != VK_MENU)
1919 return;
1920 if (wparam == VK_CONTROL)
1921 {
1922 l = VK_LCONTROL;
1923 r = VK_RCONTROL;
1924 }
1925 else
1926 {
1927 l = VK_LMENU;
1928 r = VK_RMENU;
1929 }
1930 if (!(GetKeyState (l) & 0x8000) && !(GetKeyState (r) & 0x8000))
1931 modifiers_recorded = 1;
1932 else
1933 modifiers_recorded = 0;
1934 modifier_key_support_tested = 1;
1935 }
1936
1937 static void
1938 record_keydown (unsigned int wparam, unsigned int lparam)
1939 {
1940 int i;
1941
1942 if (!modifier_key_support_tested)
1943 test_modifier_support (wparam);
1944
1945 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
1946 return;
1947
1948 if (wparam == VK_CONTROL)
1949 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
1950 else
1951 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
1952
1953 modifiers[i] = 1;
1954 }
1955
1956 static void
1957 record_keyup (unsigned int wparam, unsigned int lparam)
1958 {
1959 int i;
1960
1961 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
1962 return;
1963
1964 if (wparam == VK_CONTROL)
1965 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
1966 else
1967 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
1968
1969 modifiers[i] = 0;
1970 }
1971
1972 /* Emacs can lose focus while a modifier key has been pressed. When
1973 it regains focus, be conservative and clear all modifiers since
1974 we cannot reconstruct the left and right modifier state. */
1975 static void
1976 reset_modifiers (void)
1977 {
1978 SHORT ctrl, alt;
1979
1980 if (GetFocus () == NULL)
1981 /* Emacs doesn't have keyboard focus. Do nothing. */
1982 return;
1983
1984 ctrl = GetAsyncKeyState (VK_CONTROL);
1985 alt = GetAsyncKeyState (VK_MENU);
1986
1987 if (!(ctrl & 0x08000))
1988 /* Clear any recorded control modifier state. */
1989 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
1990
1991 if (!(alt & 0x08000))
1992 /* Clear any recorded alt modifier state. */
1993 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
1994
1995 /* Update the state of all modifier keys, because modifiers used in
1996 hot-key combinations can get stuck on if Emacs loses focus as a
1997 result of a hot-key being pressed. */
1998 {
1999 BYTE keystate[256];
2000
2001 #define CURRENT_STATE(key) ((GetAsyncKeyState (key) & 0x8000) >> 8)
2002
2003 GetKeyboardState (keystate);
2004 keystate[VK_SHIFT] = CURRENT_STATE (VK_SHIFT);
2005 keystate[VK_CONTROL] = CURRENT_STATE (VK_CONTROL);
2006 keystate[VK_LCONTROL] = CURRENT_STATE (VK_LCONTROL);
2007 keystate[VK_RCONTROL] = CURRENT_STATE (VK_RCONTROL);
2008 keystate[VK_MENU] = CURRENT_STATE (VK_MENU);
2009 keystate[VK_LMENU] = CURRENT_STATE (VK_LMENU);
2010 keystate[VK_RMENU] = CURRENT_STATE (VK_RMENU);
2011 keystate[VK_LWIN] = CURRENT_STATE (VK_LWIN);
2012 keystate[VK_RWIN] = CURRENT_STATE (VK_RWIN);
2013 keystate[VK_APPS] = CURRENT_STATE (VK_APPS);
2014 SetKeyboardState (keystate);
2015 }
2016 }
2017
2018 /* Synchronize modifier state with what is reported with the current
2019 keystroke. Even if we cannot distinguish between left and right
2020 modifier keys, we know that, if no modifiers are set, then neither
2021 the left or right modifier should be set. */
2022 static void
2023 sync_modifiers (void)
2024 {
2025 if (!modifiers_recorded)
2026 return;
2027
2028 if (!(GetKeyState (VK_CONTROL) & 0x8000))
2029 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
2030
2031 if (!(GetKeyState (VK_MENU) & 0x8000))
2032 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
2033 }
2034
2035 static int
2036 modifier_set (int vkey)
2037 {
2038 if (vkey == VK_CAPITAL || vkey == VK_SCROLL)
2039 return (GetKeyState (vkey) & 0x1);
2040 if (!modifiers_recorded)
2041 return (GetKeyState (vkey) & 0x8000);
2042
2043 switch (vkey)
2044 {
2045 case VK_LCONTROL:
2046 return modifiers[EMACS_LCONTROL];
2047 case VK_RCONTROL:
2048 return modifiers[EMACS_RCONTROL];
2049 case VK_LMENU:
2050 return modifiers[EMACS_LMENU];
2051 case VK_RMENU:
2052 return modifiers[EMACS_RMENU];
2053 }
2054 return (GetKeyState (vkey) & 0x8000);
2055 }
2056
2057 /* Convert between the modifier bits W32 uses and the modifier bits
2058 Emacs uses. */
2059
2060 unsigned int
2061 w32_key_to_modifier (int key)
2062 {
2063 Lisp_Object key_mapping;
2064
2065 switch (key)
2066 {
2067 case VK_LWIN:
2068 key_mapping = Vw32_lwindow_modifier;
2069 break;
2070 case VK_RWIN:
2071 key_mapping = Vw32_rwindow_modifier;
2072 break;
2073 case VK_APPS:
2074 key_mapping = Vw32_apps_modifier;
2075 break;
2076 case VK_SCROLL:
2077 key_mapping = Vw32_scroll_lock_modifier;
2078 break;
2079 default:
2080 key_mapping = Qnil;
2081 }
2082
2083 /* NB. This code runs in the input thread, asychronously to the lisp
2084 thread, so we must be careful to ensure access to lisp data is
2085 thread-safe. The following code is safe because the modifier
2086 variable values are updated atomically from lisp and symbols are
2087 not relocated by GC. Also, we don't have to worry about seeing GC
2088 markbits here. */
2089 if (EQ (key_mapping, Qhyper))
2090 return hyper_modifier;
2091 if (EQ (key_mapping, Qsuper))
2092 return super_modifier;
2093 if (EQ (key_mapping, Qmeta))
2094 return meta_modifier;
2095 if (EQ (key_mapping, Qalt))
2096 return alt_modifier;
2097 if (EQ (key_mapping, Qctrl))
2098 return ctrl_modifier;
2099 if (EQ (key_mapping, Qcontrol)) /* synonym for ctrl */
2100 return ctrl_modifier;
2101 if (EQ (key_mapping, Qshift))
2102 return shift_modifier;
2103
2104 /* Don't generate any modifier if not explicitly requested. */
2105 return 0;
2106 }
2107
2108 static unsigned int
2109 w32_get_modifiers (void)
2110 {
2111 return ((modifier_set (VK_SHIFT) ? shift_modifier : 0) |
2112 (modifier_set (VK_CONTROL) ? ctrl_modifier : 0) |
2113 (modifier_set (VK_LWIN) ? w32_key_to_modifier (VK_LWIN) : 0) |
2114 (modifier_set (VK_RWIN) ? w32_key_to_modifier (VK_RWIN) : 0) |
2115 (modifier_set (VK_APPS) ? w32_key_to_modifier (VK_APPS) : 0) |
2116 (modifier_set (VK_SCROLL) ? w32_key_to_modifier (VK_SCROLL) : 0) |
2117 (modifier_set (VK_MENU) ?
2118 ((NILP (Vw32_alt_is_meta)) ? alt_modifier : meta_modifier) : 0));
2119 }
2120
2121 /* We map the VK_* modifiers into console modifier constants
2122 so that we can use the same routines to handle both console
2123 and window input. */
2124
2125 static int
2126 construct_console_modifiers (void)
2127 {
2128 int mods;
2129
2130 mods = 0;
2131 mods |= (modifier_set (VK_SHIFT)) ? SHIFT_PRESSED : 0;
2132 mods |= (modifier_set (VK_CAPITAL)) ? CAPSLOCK_ON : 0;
2133 mods |= (modifier_set (VK_SCROLL)) ? SCROLLLOCK_ON : 0;
2134 mods |= (modifier_set (VK_NUMLOCK)) ? NUMLOCK_ON : 0;
2135 mods |= (modifier_set (VK_LCONTROL)) ? LEFT_CTRL_PRESSED : 0;
2136 mods |= (modifier_set (VK_RCONTROL)) ? RIGHT_CTRL_PRESSED : 0;
2137 mods |= (modifier_set (VK_LMENU)) ? LEFT_ALT_PRESSED : 0;
2138 mods |= (modifier_set (VK_RMENU)) ? RIGHT_ALT_PRESSED : 0;
2139 mods |= (modifier_set (VK_LWIN)) ? LEFT_WIN_PRESSED : 0;
2140 mods |= (modifier_set (VK_RWIN)) ? RIGHT_WIN_PRESSED : 0;
2141 mods |= (modifier_set (VK_APPS)) ? APPS_PRESSED : 0;
2142
2143 return mods;
2144 }
2145
2146 static int
2147 w32_get_key_modifiers (unsigned int wparam, unsigned int lparam)
2148 {
2149 int mods;
2150
2151 /* Convert to emacs modifiers. */
2152 mods = w32_kbd_mods_to_emacs (construct_console_modifiers (), wparam);
2153
2154 return mods;
2155 }
2156
2157 unsigned int
2158 map_keypad_keys (unsigned int virt_key, unsigned int extended)
2159 {
2160 if (virt_key < VK_CLEAR || virt_key > VK_DELETE)
2161 return virt_key;
2162
2163 if (virt_key == VK_RETURN)
2164 return (extended ? VK_NUMPAD_ENTER : VK_RETURN);
2165
2166 if (virt_key >= VK_PRIOR && virt_key <= VK_DOWN)
2167 return (!extended ? (VK_NUMPAD_PRIOR + (virt_key - VK_PRIOR)) : virt_key);
2168
2169 if (virt_key == VK_INSERT || virt_key == VK_DELETE)
2170 return (!extended ? (VK_NUMPAD_INSERT + (virt_key - VK_INSERT)) : virt_key);
2171
2172 if (virt_key == VK_CLEAR)
2173 return (!extended ? VK_NUMPAD_CLEAR : virt_key);
2174
2175 return virt_key;
2176 }
2177
2178 /* List of special key combinations which w32 would normally capture,
2179 but Emacs should grab instead. Not directly visible to lisp, to
2180 simplify synchronization. Each item is an integer encoding a virtual
2181 key code and modifier combination to capture. */
2182 static Lisp_Object w32_grabbed_keys;
2183
2184 #define HOTKEY(vk, mods) make_number (((vk) & 255) | ((mods) << 8))
2185 #define HOTKEY_ID(k) (XFASTINT (k) & 0xbfff)
2186 #define HOTKEY_VK_CODE(k) (XFASTINT (k) & 255)
2187 #define HOTKEY_MODIFIERS(k) (XFASTINT (k) >> 8)
2188
2189 #define RAW_HOTKEY_ID(k) ((k) & 0xbfff)
2190 #define RAW_HOTKEY_VK_CODE(k) ((k) & 255)
2191 #define RAW_HOTKEY_MODIFIERS(k) ((k) >> 8)
2192
2193 /* Register hot-keys for reserved key combinations when Emacs has
2194 keyboard focus, since this is the only way Emacs can receive key
2195 combinations like Alt-Tab which are used by the system. */
2196
2197 static void
2198 register_hot_keys (HWND hwnd)
2199 {
2200 Lisp_Object keylist;
2201
2202 /* Use CONSP, since we are called asynchronously. */
2203 for (keylist = w32_grabbed_keys; CONSP (keylist); keylist = XCDR (keylist))
2204 {
2205 Lisp_Object key = XCAR (keylist);
2206
2207 /* Deleted entries get set to nil. */
2208 if (!INTEGERP (key))
2209 continue;
2210
2211 RegisterHotKey (hwnd, HOTKEY_ID (key),
2212 HOTKEY_MODIFIERS (key), HOTKEY_VK_CODE (key));
2213 }
2214 }
2215
2216 static void
2217 unregister_hot_keys (HWND hwnd)
2218 {
2219 Lisp_Object keylist;
2220
2221 for (keylist = w32_grabbed_keys; CONSP (keylist); keylist = XCDR (keylist))
2222 {
2223 Lisp_Object key = XCAR (keylist);
2224
2225 if (!INTEGERP (key))
2226 continue;
2227
2228 UnregisterHotKey (hwnd, HOTKEY_ID (key));
2229 }
2230 }
2231
2232 /* Main message dispatch loop. */
2233
2234 static void
2235 w32_msg_pump (deferred_msg * msg_buf)
2236 {
2237 MSG msg;
2238 int result;
2239 HWND focus_window;
2240
2241 msh_mousewheel = RegisterWindowMessage (MSH_MOUSEWHEEL);
2242
2243 while (GetMessage (&msg, NULL, 0, 0))
2244 {
2245 if (msg.hwnd == NULL)
2246 {
2247 switch (msg.message)
2248 {
2249 case WM_NULL:
2250 /* Produced by complete_deferred_msg; just ignore. */
2251 break;
2252 case WM_EMACS_CREATEWINDOW:
2253 /* Initialize COM for this window. Even though we don't use it,
2254 some third party shell extensions can cause it to be used in
2255 system dialogs, which causes a crash if it is not initialized.
2256 This is a known bug in Windows, which was fixed long ago, but
2257 the patch for XP is not publically available until XP SP3,
2258 and older versions will never be patched. */
2259 CoInitialize (NULL);
2260 w32_createwindow ((struct frame *) msg.wParam);
2261 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2262 abort ();
2263 break;
2264 case WM_EMACS_SETLOCALE:
2265 SetThreadLocale (msg.wParam);
2266 /* Reply is not expected. */
2267 break;
2268 case WM_EMACS_SETKEYBOARDLAYOUT:
2269 result = (int) ActivateKeyboardLayout ((HKL) msg.wParam, 0);
2270 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
2271 result, 0))
2272 abort ();
2273 break;
2274 case WM_EMACS_REGISTER_HOT_KEY:
2275 focus_window = GetFocus ();
2276 if (focus_window != NULL)
2277 RegisterHotKey (focus_window,
2278 RAW_HOTKEY_ID (msg.wParam),
2279 RAW_HOTKEY_MODIFIERS (msg.wParam),
2280 RAW_HOTKEY_VK_CODE (msg.wParam));
2281 /* Reply is not expected. */
2282 break;
2283 case WM_EMACS_UNREGISTER_HOT_KEY:
2284 focus_window = GetFocus ();
2285 if (focus_window != NULL)
2286 UnregisterHotKey (focus_window, RAW_HOTKEY_ID (msg.wParam));
2287 /* Mark item as erased. NB: this code must be
2288 thread-safe. The next line is okay because the cons
2289 cell is never made into garbage and is not relocated by
2290 GC. */
2291 XSETCAR ((Lisp_Object) ((EMACS_INT) msg.lParam), Qnil);
2292 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2293 abort ();
2294 break;
2295 case WM_EMACS_TOGGLE_LOCK_KEY:
2296 {
2297 int vk_code = (int) msg.wParam;
2298 int cur_state = (GetKeyState (vk_code) & 1);
2299 Lisp_Object new_state = (Lisp_Object) ((EMACS_INT) msg.lParam);
2300
2301 /* NB: This code must be thread-safe. It is safe to
2302 call NILP because symbols are not relocated by GC,
2303 and pointer here is not touched by GC (so the markbit
2304 can't be set). Numbers are safe because they are
2305 immediate values. */
2306 if (NILP (new_state)
2307 || (NUMBERP (new_state)
2308 && ((XUINT (new_state)) & 1) != cur_state))
2309 {
2310 one_w32_display_info.faked_key = vk_code;
2311
2312 keybd_event ((BYTE) vk_code,
2313 (BYTE) MapVirtualKey (vk_code, 0),
2314 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2315 keybd_event ((BYTE) vk_code,
2316 (BYTE) MapVirtualKey (vk_code, 0),
2317 KEYEVENTF_EXTENDEDKEY | 0, 0);
2318 keybd_event ((BYTE) vk_code,
2319 (BYTE) MapVirtualKey (vk_code, 0),
2320 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2321 cur_state = !cur_state;
2322 }
2323 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
2324 cur_state, 0))
2325 abort ();
2326 }
2327 break;
2328 #ifdef MSG_DEBUG
2329 /* Broadcast messages make it here, so you need to be looking
2330 for something in particular for this to be useful. */
2331 default:
2332 DebPrint (("msg %x not expected by w32_msg_pump\n", msg.message));
2333 #endif
2334 }
2335 }
2336 else
2337 {
2338 DispatchMessage (&msg);
2339 }
2340
2341 /* Exit nested loop when our deferred message has completed. */
2342 if (msg_buf->completed)
2343 break;
2344 }
2345 }
2346
2347 deferred_msg * deferred_msg_head;
2348
2349 static deferred_msg *
2350 find_deferred_msg (HWND hwnd, UINT msg)
2351 {
2352 deferred_msg * item;
2353
2354 /* Don't actually need synchronization for read access, since
2355 modification of single pointer is always atomic. */
2356 /* enter_crit (); */
2357
2358 for (item = deferred_msg_head; item != NULL; item = item->next)
2359 if (item->w32msg.msg.hwnd == hwnd
2360 && item->w32msg.msg.message == msg)
2361 break;
2362
2363 /* leave_crit (); */
2364
2365 return item;
2366 }
2367
2368 static LRESULT
2369 send_deferred_msg (deferred_msg * msg_buf,
2370 HWND hwnd,
2371 UINT msg,
2372 WPARAM wParam,
2373 LPARAM lParam)
2374 {
2375 /* Only input thread can send deferred messages. */
2376 if (GetCurrentThreadId () != dwWindowsThreadId)
2377 abort ();
2378
2379 /* It is an error to send a message that is already deferred. */
2380 if (find_deferred_msg (hwnd, msg) != NULL)
2381 abort ();
2382
2383 /* Enforced synchronization is not needed because this is the only
2384 function that alters deferred_msg_head, and the following critical
2385 section is guaranteed to only be serially reentered (since only the
2386 input thread can call us). */
2387
2388 /* enter_crit (); */
2389
2390 msg_buf->completed = 0;
2391 msg_buf->next = deferred_msg_head;
2392 deferred_msg_head = msg_buf;
2393 my_post_msg (&msg_buf->w32msg, hwnd, msg, wParam, lParam);
2394
2395 /* leave_crit (); */
2396
2397 /* Start a new nested message loop to process other messages until
2398 this one is completed. */
2399 w32_msg_pump (msg_buf);
2400
2401 deferred_msg_head = msg_buf->next;
2402
2403 return msg_buf->result;
2404 }
2405
2406 void
2407 complete_deferred_msg (HWND hwnd, UINT msg, LRESULT result)
2408 {
2409 deferred_msg * msg_buf = find_deferred_msg (hwnd, msg);
2410
2411 if (msg_buf == NULL)
2412 /* Message may have been cancelled, so don't abort. */
2413 return;
2414
2415 msg_buf->result = result;
2416 msg_buf->completed = 1;
2417
2418 /* Ensure input thread is woken so it notices the completion. */
2419 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
2420 }
2421
2422 static void
2423 cancel_all_deferred_msgs (void)
2424 {
2425 deferred_msg * item;
2426
2427 /* Don't actually need synchronization for read access, since
2428 modification of single pointer is always atomic. */
2429 /* enter_crit (); */
2430
2431 for (item = deferred_msg_head; item != NULL; item = item->next)
2432 {
2433 item->result = 0;
2434 item->completed = 1;
2435 }
2436
2437 /* leave_crit (); */
2438
2439 /* Ensure input thread is woken so it notices the completion. */
2440 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
2441 }
2442
2443 DWORD WINAPI
2444 w32_msg_worker (void *arg)
2445 {
2446 MSG msg;
2447 deferred_msg dummy_buf;
2448
2449 /* Ensure our message queue is created */
2450
2451 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
2452
2453 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2454 abort ();
2455
2456 memset (&dummy_buf, 0, sizeof (dummy_buf));
2457 dummy_buf.w32msg.msg.hwnd = NULL;
2458 dummy_buf.w32msg.msg.message = WM_NULL;
2459
2460 /* This is the initial message loop which should only exit when the
2461 application quits. */
2462 w32_msg_pump (&dummy_buf);
2463
2464 return 0;
2465 }
2466
2467 static void
2468 signal_user_input (void)
2469 {
2470 /* Interrupt any lisp that wants to be interrupted by input. */
2471 if (!NILP (Vthrow_on_input))
2472 {
2473 Vquit_flag = Vthrow_on_input;
2474 /* If we're inside a function that wants immediate quits,
2475 do it now. */
2476 if (immediate_quit && NILP (Vinhibit_quit))
2477 {
2478 immediate_quit = 0;
2479 QUIT;
2480 }
2481 }
2482 }
2483
2484
2485 static void
2486 post_character_message (HWND hwnd, UINT msg,
2487 WPARAM wParam, LPARAM lParam,
2488 DWORD modifiers)
2489 {
2490 W32Msg wmsg;
2491
2492 wmsg.dwModifiers = modifiers;
2493
2494 /* Detect quit_char and set quit-flag directly. Note that we
2495 still need to post a message to ensure the main thread will be
2496 woken up if blocked in sys_select, but we do NOT want to post
2497 the quit_char message itself (because it will usually be as if
2498 the user had typed quit_char twice). Instead, we post a dummy
2499 message that has no particular effect. */
2500 {
2501 int c = wParam;
2502 if (isalpha (c) && wmsg.dwModifiers == ctrl_modifier)
2503 c = make_ctrl_char (c) & 0377;
2504 if (c == quit_char
2505 || (wmsg.dwModifiers == 0 &&
2506 w32_quit_key && wParam == w32_quit_key))
2507 {
2508 Vquit_flag = Qt;
2509
2510 /* The choice of message is somewhat arbitrary, as long as
2511 the main thread handler just ignores it. */
2512 msg = WM_NULL;
2513
2514 /* Interrupt any blocking system calls. */
2515 signal_quit ();
2516
2517 /* As a safety precaution, forcibly complete any deferred
2518 messages. This is a kludge, but I don't see any particularly
2519 clean way to handle the situation where a deferred message is
2520 "dropped" in the lisp thread, and will thus never be
2521 completed, eg. by the user trying to activate the menubar
2522 when the lisp thread is busy, and then typing C-g when the
2523 menubar doesn't open promptly (with the result that the
2524 menubar never responds at all because the deferred
2525 WM_INITMENU message is never completed). Another problem
2526 situation is when the lisp thread calls SendMessage (to send
2527 a window manager command) when a message has been deferred;
2528 the lisp thread gets blocked indefinitely waiting for the
2529 deferred message to be completed, which itself is waiting for
2530 the lisp thread to respond.
2531
2532 Note that we don't want to block the input thread waiting for
2533 a reponse from the lisp thread (although that would at least
2534 solve the deadlock problem above), because we want to be able
2535 to receive C-g to interrupt the lisp thread. */
2536 cancel_all_deferred_msgs ();
2537 }
2538 else
2539 signal_user_input ();
2540 }
2541
2542 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2543 }
2544
2545 /* Main window procedure */
2546
2547 static LRESULT CALLBACK
2548 w32_wnd_proc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
2549 {
2550 struct frame *f;
2551 struct w32_display_info *dpyinfo = &one_w32_display_info;
2552 W32Msg wmsg;
2553 int windows_translate;
2554 int key;
2555
2556 /* Note that it is okay to call x_window_to_frame, even though we are
2557 not running in the main lisp thread, because frame deletion
2558 requires the lisp thread to synchronize with this thread. Thus, if
2559 a frame struct is returned, it can be used without concern that the
2560 lisp thread might make it disappear while we are using it.
2561
2562 NB. Walking the frame list in this thread is safe (as long as
2563 writes of Lisp_Object slots are atomic, which they are on Windows).
2564 Although delete-frame can destructively modify the frame list while
2565 we are walking it, a garbage collection cannot occur until after
2566 delete-frame has synchronized with this thread.
2567
2568 It is also safe to use functions that make GDI calls, such as
2569 w32_clear_rect, because these functions must obtain a DC handle
2570 from the frame struct using get_frame_dc which is thread-aware. */
2571
2572 switch (msg)
2573 {
2574 case WM_ERASEBKGND:
2575 f = x_window_to_frame (dpyinfo, hwnd);
2576 if (f)
2577 {
2578 HDC hdc = get_frame_dc (f);
2579 GetUpdateRect (hwnd, &wmsg.rect, FALSE);
2580 w32_clear_rect (f, hdc, &wmsg.rect);
2581 release_frame_dc (f, hdc);
2582
2583 #if defined (W32_DEBUG_DISPLAY)
2584 DebPrint (("WM_ERASEBKGND (frame %p): erasing %d,%d-%d,%d\n",
2585 f,
2586 wmsg.rect.left, wmsg.rect.top,
2587 wmsg.rect.right, wmsg.rect.bottom));
2588 #endif /* W32_DEBUG_DISPLAY */
2589 }
2590 return 1;
2591 case WM_PALETTECHANGED:
2592 /* ignore our own changes */
2593 if ((HWND)wParam != hwnd)
2594 {
2595 f = x_window_to_frame (dpyinfo, hwnd);
2596 if (f)
2597 /* get_frame_dc will realize our palette and force all
2598 frames to be redrawn if needed. */
2599 release_frame_dc (f, get_frame_dc (f));
2600 }
2601 return 0;
2602 case WM_PAINT:
2603 {
2604 PAINTSTRUCT paintStruct;
2605 RECT update_rect;
2606 memset (&update_rect, 0, sizeof (update_rect));
2607
2608 f = x_window_to_frame (dpyinfo, hwnd);
2609 if (f == 0)
2610 {
2611 DebPrint (("WM_PAINT received for unknown window %p\n", hwnd));
2612 return 0;
2613 }
2614
2615 /* MSDN Docs say not to call BeginPaint if GetUpdateRect
2616 fails. Apparently this can happen under some
2617 circumstances. */
2618 if (GetUpdateRect (hwnd, &update_rect, FALSE) || !w32_strict_painting)
2619 {
2620 enter_crit ();
2621 BeginPaint (hwnd, &paintStruct);
2622
2623 /* The rectangles returned by GetUpdateRect and BeginPaint
2624 do not always match. Play it safe by assuming both areas
2625 are invalid. */
2626 UnionRect (&(wmsg.rect), &update_rect, &(paintStruct.rcPaint));
2627
2628 #if defined (W32_DEBUG_DISPLAY)
2629 DebPrint (("WM_PAINT (frame %p): painting %d,%d-%d,%d\n",
2630 f,
2631 wmsg.rect.left, wmsg.rect.top,
2632 wmsg.rect.right, wmsg.rect.bottom));
2633 DebPrint ((" [update region is %d,%d-%d,%d]\n",
2634 update_rect.left, update_rect.top,
2635 update_rect.right, update_rect.bottom));
2636 #endif
2637 EndPaint (hwnd, &paintStruct);
2638 leave_crit ();
2639
2640 /* Change the message type to prevent Windows from
2641 combining WM_PAINT messages in the Lisp thread's queue,
2642 since Windows assumes that each message queue is
2643 dedicated to one frame and does not bother checking
2644 that hwnd matches before combining them. */
2645 my_post_msg (&wmsg, hwnd, WM_EMACS_PAINT, wParam, lParam);
2646
2647 return 0;
2648 }
2649
2650 /* If GetUpdateRect returns 0 (meaning there is no update
2651 region), assume the whole window needs to be repainted. */
2652 GetClientRect (hwnd, &wmsg.rect);
2653 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2654 return 0;
2655 }
2656
2657 case WM_INPUTLANGCHANGE:
2658 /* Inform lisp thread of keyboard layout changes. */
2659 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2660
2661 /* Clear dead keys in the keyboard state; for simplicity only
2662 preserve modifier key states. */
2663 {
2664 int i;
2665 BYTE keystate[256];
2666
2667 GetKeyboardState (keystate);
2668 for (i = 0; i < 256; i++)
2669 if (1
2670 && i != VK_SHIFT
2671 && i != VK_LSHIFT
2672 && i != VK_RSHIFT
2673 && i != VK_CAPITAL
2674 && i != VK_NUMLOCK
2675 && i != VK_SCROLL
2676 && i != VK_CONTROL
2677 && i != VK_LCONTROL
2678 && i != VK_RCONTROL
2679 && i != VK_MENU
2680 && i != VK_LMENU
2681 && i != VK_RMENU
2682 && i != VK_LWIN
2683 && i != VK_RWIN)
2684 keystate[i] = 0;
2685 SetKeyboardState (keystate);
2686 }
2687 goto dflt;
2688
2689 case WM_HOTKEY:
2690 /* Synchronize hot keys with normal input. */
2691 PostMessage (hwnd, WM_KEYDOWN, HIWORD (lParam), 0);
2692 return (0);
2693
2694 case WM_KEYUP:
2695 case WM_SYSKEYUP:
2696 record_keyup (wParam, lParam);
2697 goto dflt;
2698
2699 case WM_KEYDOWN:
2700 case WM_SYSKEYDOWN:
2701 /* Ignore keystrokes we fake ourself; see below. */
2702 if (dpyinfo->faked_key == wParam)
2703 {
2704 dpyinfo->faked_key = 0;
2705 /* Make sure TranslateMessage sees them though (as long as
2706 they don't produce WM_CHAR messages). This ensures that
2707 indicator lights are toggled promptly on Windows 9x, for
2708 example. */
2709 if (wParam < 256 && lispy_function_keys[wParam])
2710 {
2711 windows_translate = 1;
2712 goto translate;
2713 }
2714 return 0;
2715 }
2716
2717 /* Synchronize modifiers with current keystroke. */
2718 sync_modifiers ();
2719 record_keydown (wParam, lParam);
2720 wParam = map_keypad_keys (wParam, (lParam & 0x1000000L) != 0);
2721
2722 windows_translate = 0;
2723
2724 switch (wParam)
2725 {
2726 case VK_LWIN:
2727 if (NILP (Vw32_pass_lwindow_to_system))
2728 {
2729 /* Prevent system from acting on keyup (which opens the
2730 Start menu if no other key was pressed) by simulating a
2731 press of Space which we will ignore. */
2732 if (GetAsyncKeyState (wParam) & 1)
2733 {
2734 if (NUMBERP (Vw32_phantom_key_code))
2735 key = XUINT (Vw32_phantom_key_code) & 255;
2736 else
2737 key = VK_SPACE;
2738 dpyinfo->faked_key = key;
2739 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
2740 }
2741 }
2742 if (!NILP (Vw32_lwindow_modifier))
2743 return 0;
2744 break;
2745 case VK_RWIN:
2746 if (NILP (Vw32_pass_rwindow_to_system))
2747 {
2748 if (GetAsyncKeyState (wParam) & 1)
2749 {
2750 if (NUMBERP (Vw32_phantom_key_code))
2751 key = XUINT (Vw32_phantom_key_code) & 255;
2752 else
2753 key = VK_SPACE;
2754 dpyinfo->faked_key = key;
2755 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
2756 }
2757 }
2758 if (!NILP (Vw32_rwindow_modifier))
2759 return 0;
2760 break;
2761 case VK_APPS:
2762 if (!NILP (Vw32_apps_modifier))
2763 return 0;
2764 break;
2765 case VK_MENU:
2766 if (NILP (Vw32_pass_alt_to_system))
2767 /* Prevent DefWindowProc from activating the menu bar if an
2768 Alt key is pressed and released by itself. */
2769 return 0;
2770 windows_translate = 1;
2771 break;
2772 case VK_CAPITAL:
2773 /* Decide whether to treat as modifier or function key. */
2774 if (NILP (Vw32_enable_caps_lock))
2775 goto disable_lock_key;
2776 windows_translate = 1;
2777 break;
2778 case VK_NUMLOCK:
2779 /* Decide whether to treat as modifier or function key. */
2780 if (NILP (Vw32_enable_num_lock))
2781 goto disable_lock_key;
2782 windows_translate = 1;
2783 break;
2784 case VK_SCROLL:
2785 /* Decide whether to treat as modifier or function key. */
2786 if (NILP (Vw32_scroll_lock_modifier))
2787 goto disable_lock_key;
2788 windows_translate = 1;
2789 break;
2790 disable_lock_key:
2791 /* Ensure the appropriate lock key state (and indicator light)
2792 remains in the same state. We do this by faking another
2793 press of the relevant key. Apparently, this really is the
2794 only way to toggle the state of the indicator lights. */
2795 dpyinfo->faked_key = wParam;
2796 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
2797 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2798 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
2799 KEYEVENTF_EXTENDEDKEY | 0, 0);
2800 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
2801 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2802 /* Ensure indicator lights are updated promptly on Windows 9x
2803 (TranslateMessage apparently does this), after forwarding
2804 input event. */
2805 post_character_message (hwnd, msg, wParam, lParam,
2806 w32_get_key_modifiers (wParam, lParam));
2807 windows_translate = 1;
2808 break;
2809 case VK_CONTROL:
2810 case VK_SHIFT:
2811 case VK_PROCESSKEY: /* Generated by IME. */
2812 windows_translate = 1;
2813 break;
2814 case VK_CANCEL:
2815 /* Windows maps Ctrl-Pause (aka Ctrl-Break) into VK_CANCEL,
2816 which is confusing for purposes of key binding; convert
2817 VK_CANCEL events into VK_PAUSE events. */
2818 wParam = VK_PAUSE;
2819 break;
2820 case VK_PAUSE:
2821 /* Windows maps Ctrl-NumLock into VK_PAUSE, which is confusing
2822 for purposes of key binding; convert these back into
2823 VK_NUMLOCK events, at least when we want to see NumLock key
2824 presses. (Note that there is never any possibility that
2825 VK_PAUSE with Ctrl really is C-Pause as per above.) */
2826 if (NILP (Vw32_enable_num_lock) && modifier_set (VK_CONTROL))
2827 wParam = VK_NUMLOCK;
2828 break;
2829 default:
2830 /* If not defined as a function key, change it to a WM_CHAR message. */
2831 if (wParam > 255 || !lispy_function_keys[wParam])
2832 {
2833 DWORD modifiers = construct_console_modifiers ();
2834
2835 if (!NILP (Vw32_recognize_altgr)
2836 && modifier_set (VK_LCONTROL) && modifier_set (VK_RMENU))
2837 {
2838 /* Always let TranslateMessage handle AltGr key chords;
2839 for some reason, ToAscii doesn't always process AltGr
2840 chords correctly. */
2841 windows_translate = 1;
2842 }
2843 else if ((modifiers & (~SHIFT_PRESSED & ~CAPSLOCK_ON)) != 0)
2844 {
2845 /* Handle key chords including any modifiers other
2846 than shift directly, in order to preserve as much
2847 modifier information as possible. */
2848 if ('A' <= wParam && wParam <= 'Z')
2849 {
2850 /* Don't translate modified alphabetic keystrokes,
2851 so the user doesn't need to constantly switch
2852 layout to type control or meta keystrokes when
2853 the normal layout translates alphabetic
2854 characters to non-ascii characters. */
2855 if (!modifier_set (VK_SHIFT))
2856 wParam += ('a' - 'A');
2857 msg = WM_CHAR;
2858 }
2859 else
2860 {
2861 /* Try to handle other keystrokes by determining the
2862 base character (ie. translating the base key plus
2863 shift modifier). */
2864 int add;
2865 KEY_EVENT_RECORD key;
2866
2867 key.bKeyDown = TRUE;
2868 key.wRepeatCount = 1;
2869 key.wVirtualKeyCode = wParam;
2870 key.wVirtualScanCode = (lParam & 0xFF0000) >> 16;
2871 key.uChar.AsciiChar = 0;
2872 key.dwControlKeyState = modifiers;
2873
2874 add = w32_kbd_patch_key (&key);
2875 /* 0 means an unrecognised keycode, negative means
2876 dead key. Ignore both. */
2877 while (--add >= 0)
2878 {
2879 /* Forward asciified character sequence. */
2880 post_character_message
2881 (hwnd, WM_CHAR,
2882 (unsigned char) key.uChar.AsciiChar, lParam,
2883 w32_get_key_modifiers (wParam, lParam));
2884 w32_kbd_patch_key (&key);
2885 }
2886 return 0;
2887 }
2888 }
2889 else
2890 {
2891 /* Let TranslateMessage handle everything else. */
2892 windows_translate = 1;
2893 }
2894 }
2895 }
2896
2897 translate:
2898 if (windows_translate)
2899 {
2900 MSG windows_msg = { hwnd, msg, wParam, lParam, 0, {0,0} };
2901 windows_msg.time = GetMessageTime ();
2902 TranslateMessage (&windows_msg);
2903 goto dflt;
2904 }
2905
2906 /* Fall through */
2907
2908 case WM_SYSCHAR:
2909 case WM_CHAR:
2910 post_character_message (hwnd, msg, wParam, lParam,
2911 w32_get_key_modifiers (wParam, lParam));
2912 break;
2913
2914 case WM_UNICHAR:
2915 /* WM_UNICHAR looks promising from the docs, but the exact
2916 circumstances in which TranslateMessage sends it is one of those
2917 Microsoft secret API things that EU and US courts are supposed
2918 to have put a stop to already. Spy++ shows it being sent to Notepad
2919 and other MS apps, but never to Emacs.
2920
2921 Some third party IMEs send it in accordance with the official
2922 documentation though, so handle it here.
2923
2924 UNICODE_NOCHAR is used to test for support for this message.
2925 TRUE indicates that the message is supported. */
2926 if (wParam == UNICODE_NOCHAR)
2927 return TRUE;
2928
2929 {
2930 W32Msg wmsg;
2931 wmsg.dwModifiers = w32_get_key_modifiers (wParam, lParam);
2932 signal_user_input ();
2933 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2934 }
2935 break;
2936
2937 case WM_IME_CHAR:
2938 /* If we can't get the IME result as unicode, use default processing,
2939 which will at least allow characters decodable in the system locale
2940 get through. */
2941 if (!get_composition_string_fn)
2942 goto dflt;
2943
2944 else if (!ignore_ime_char)
2945 {
2946 wchar_t * buffer;
2947 int size, i;
2948 W32Msg wmsg;
2949 HIMC context = get_ime_context_fn (hwnd);
2950 wmsg.dwModifiers = w32_get_key_modifiers (wParam, lParam);
2951 /* Get buffer size. */
2952 size = get_composition_string_fn (context, GCS_RESULTSTR, NULL, 0);
2953 buffer = alloca (size);
2954 size = get_composition_string_fn (context, GCS_RESULTSTR,
2955 buffer, size);
2956 release_ime_context_fn (hwnd, context);
2957
2958 signal_user_input ();
2959 for (i = 0; i < size / sizeof (wchar_t); i++)
2960 {
2961 my_post_msg (&wmsg, hwnd, WM_UNICHAR, (WPARAM) buffer[i],
2962 lParam);
2963 }
2964 /* Ignore the messages for the rest of the
2965 characters in the string that was output above. */
2966 ignore_ime_char = (size / sizeof (wchar_t)) - 1;
2967 }
2968 else
2969 ignore_ime_char--;
2970
2971 break;
2972
2973 case WM_IME_STARTCOMPOSITION:
2974 if (!set_ime_composition_window_fn)
2975 goto dflt;
2976 else
2977 {
2978 COMPOSITIONFORM form;
2979 HIMC context;
2980 struct window *w;
2981
2982 f = x_window_to_frame (dpyinfo, hwnd);
2983 w = XWINDOW (FRAME_SELECTED_WINDOW (f));
2984
2985 form.dwStyle = CFS_RECT;
2986 form.ptCurrentPos.x = w32_system_caret_x;
2987 form.ptCurrentPos.y = w32_system_caret_y;
2988
2989 form.rcArea.left = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, 0);
2990 form.rcArea.top = (WINDOW_TOP_EDGE_Y (w)
2991 + WINDOW_HEADER_LINE_HEIGHT (w));
2992 form.rcArea.right = (WINDOW_BOX_RIGHT_EDGE_X (w)
2993 - WINDOW_RIGHT_MARGIN_WIDTH (w)
2994 - WINDOW_RIGHT_FRINGE_WIDTH (w));
2995 form.rcArea.bottom = (WINDOW_BOTTOM_EDGE_Y (w)
2996 - WINDOW_MODE_LINE_HEIGHT (w));
2997
2998 context = get_ime_context_fn (hwnd);
2999
3000 if (!context)
3001 break;
3002
3003 set_ime_composition_window_fn (context, &form);
3004 release_ime_context_fn (hwnd, context);
3005 }
3006 break;
3007
3008 case WM_IME_ENDCOMPOSITION:
3009 ignore_ime_char = 0;
3010 goto dflt;
3011
3012 /* Simulate middle mouse button events when left and right buttons
3013 are used together, but only if user has two button mouse. */
3014 case WM_LBUTTONDOWN:
3015 case WM_RBUTTONDOWN:
3016 if (w32_num_mouse_buttons > 2)
3017 goto handle_plain_button;
3018
3019 {
3020 int this = (msg == WM_LBUTTONDOWN) ? LMOUSE : RMOUSE;
3021 int other = (msg == WM_LBUTTONDOWN) ? RMOUSE : LMOUSE;
3022
3023 if (button_state & this)
3024 return 0;
3025
3026 if (button_state == 0)
3027 SetCapture (hwnd);
3028
3029 button_state |= this;
3030
3031 if (button_state & other)
3032 {
3033 if (mouse_button_timer)
3034 {
3035 KillTimer (hwnd, mouse_button_timer);
3036 mouse_button_timer = 0;
3037
3038 /* Generate middle mouse event instead. */
3039 msg = WM_MBUTTONDOWN;
3040 button_state |= MMOUSE;
3041 }
3042 else if (button_state & MMOUSE)
3043 {
3044 /* Ignore button event if we've already generated a
3045 middle mouse down event. This happens if the
3046 user releases and press one of the two buttons
3047 after we've faked a middle mouse event. */
3048 return 0;
3049 }
3050 else
3051 {
3052 /* Flush out saved message. */
3053 post_msg (&saved_mouse_button_msg);
3054 }
3055 wmsg.dwModifiers = w32_get_modifiers ();
3056 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3057 signal_user_input ();
3058
3059 /* Clear message buffer. */
3060 saved_mouse_button_msg.msg.hwnd = 0;
3061 }
3062 else
3063 {
3064 /* Hold onto message for now. */
3065 mouse_button_timer =
3066 SetTimer (hwnd, MOUSE_BUTTON_ID,
3067 w32_mouse_button_tolerance, NULL);
3068 saved_mouse_button_msg.msg.hwnd = hwnd;
3069 saved_mouse_button_msg.msg.message = msg;
3070 saved_mouse_button_msg.msg.wParam = wParam;
3071 saved_mouse_button_msg.msg.lParam = lParam;
3072 saved_mouse_button_msg.msg.time = GetMessageTime ();
3073 saved_mouse_button_msg.dwModifiers = w32_get_modifiers ();
3074 }
3075 }
3076 return 0;
3077
3078 case WM_LBUTTONUP:
3079 case WM_RBUTTONUP:
3080 if (w32_num_mouse_buttons > 2)
3081 goto handle_plain_button;
3082
3083 {
3084 int this = (msg == WM_LBUTTONUP) ? LMOUSE : RMOUSE;
3085 int other = (msg == WM_LBUTTONUP) ? RMOUSE : LMOUSE;
3086
3087 if ((button_state & this) == 0)
3088 return 0;
3089
3090 button_state &= ~this;
3091
3092 if (button_state & MMOUSE)
3093 {
3094 /* Only generate event when second button is released. */
3095 if ((button_state & other) == 0)
3096 {
3097 msg = WM_MBUTTONUP;
3098 button_state &= ~MMOUSE;
3099
3100 if (button_state) abort ();
3101 }
3102 else
3103 return 0;
3104 }
3105 else
3106 {
3107 /* Flush out saved message if necessary. */
3108 if (saved_mouse_button_msg.msg.hwnd)
3109 {
3110 post_msg (&saved_mouse_button_msg);
3111 }
3112 }
3113 wmsg.dwModifiers = w32_get_modifiers ();
3114 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3115 signal_user_input ();
3116
3117 /* Always clear message buffer and cancel timer. */
3118 saved_mouse_button_msg.msg.hwnd = 0;
3119 KillTimer (hwnd, mouse_button_timer);
3120 mouse_button_timer = 0;
3121
3122 if (button_state == 0)
3123 ReleaseCapture ();
3124 }
3125 return 0;
3126
3127 case WM_XBUTTONDOWN:
3128 case WM_XBUTTONUP:
3129 if (w32_pass_extra_mouse_buttons_to_system)
3130 goto dflt;
3131 /* else fall through and process them. */
3132 case WM_MBUTTONDOWN:
3133 case WM_MBUTTONUP:
3134 handle_plain_button:
3135 {
3136 BOOL up;
3137 int button;
3138
3139 /* Ignore middle and extra buttons as long as the menu is active. */
3140 f = x_window_to_frame (dpyinfo, hwnd);
3141 if (f && f->output_data.w32->menubar_active)
3142 return 0;
3143
3144 if (parse_button (msg, HIWORD (wParam), &button, &up))
3145 {
3146 if (up) ReleaseCapture ();
3147 else SetCapture (hwnd);
3148 button = (button == 0) ? LMOUSE :
3149 ((button == 1) ? MMOUSE : RMOUSE);
3150 if (up)
3151 button_state &= ~button;
3152 else
3153 button_state |= button;
3154 }
3155 }
3156
3157 wmsg.dwModifiers = w32_get_modifiers ();
3158 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3159 signal_user_input ();
3160
3161 /* Need to return true for XBUTTON messages, false for others,
3162 to indicate that we processed the message. */
3163 return (msg == WM_XBUTTONDOWN || msg == WM_XBUTTONUP);
3164
3165 case WM_MOUSEMOVE:
3166 /* Ignore mouse movements as long as the menu is active. These
3167 movements are processed by the window manager anyway, and
3168 it's wrong to handle them as if they happened on the
3169 underlying frame. */
3170 f = x_window_to_frame (dpyinfo, hwnd);
3171 if (f && f->output_data.w32->menubar_active)
3172 return 0;
3173
3174 /* If the mouse has just moved into the frame, start tracking
3175 it, so we will be notified when it leaves the frame. Mouse
3176 tracking only works under W98 and NT4 and later. On earlier
3177 versions, there is no way of telling when the mouse leaves the
3178 frame, so we just have to put up with help-echo and mouse
3179 highlighting remaining while the frame is not active. */
3180 if (track_mouse_event_fn && !track_mouse_window)
3181 {
3182 TRACKMOUSEEVENT tme;
3183 tme.cbSize = sizeof (tme);
3184 tme.dwFlags = TME_LEAVE;
3185 tme.hwndTrack = hwnd;
3186
3187 track_mouse_event_fn (&tme);
3188 track_mouse_window = hwnd;
3189 }
3190 case WM_VSCROLL:
3191 if (w32_mouse_move_interval <= 0
3192 || (msg == WM_MOUSEMOVE && button_state == 0))
3193 {
3194 wmsg.dwModifiers = w32_get_modifiers ();
3195 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3196 return 0;
3197 }
3198
3199 /* Hang onto mouse move and scroll messages for a bit, to avoid
3200 sending such events to Emacs faster than it can process them.
3201 If we get more events before the timer from the first message
3202 expires, we just replace the first message. */
3203
3204 if (saved_mouse_move_msg.msg.hwnd == 0)
3205 mouse_move_timer =
3206 SetTimer (hwnd, MOUSE_MOVE_ID,
3207 w32_mouse_move_interval, NULL);
3208
3209 /* Hold onto message for now. */
3210 saved_mouse_move_msg.msg.hwnd = hwnd;
3211 saved_mouse_move_msg.msg.message = msg;
3212 saved_mouse_move_msg.msg.wParam = wParam;
3213 saved_mouse_move_msg.msg.lParam = lParam;
3214 saved_mouse_move_msg.msg.time = GetMessageTime ();
3215 saved_mouse_move_msg.dwModifiers = w32_get_modifiers ();
3216
3217 return 0;
3218
3219 case WM_MOUSEWHEEL:
3220 case WM_DROPFILES:
3221 wmsg.dwModifiers = w32_get_modifiers ();
3222 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3223 signal_user_input ();
3224 return 0;
3225
3226 case WM_APPCOMMAND:
3227 if (w32_pass_multimedia_buttons_to_system)
3228 goto dflt;
3229 /* Otherwise, pass to lisp, the same way we do with mousehwheel. */
3230 case WM_MOUSEHWHEEL:
3231 wmsg.dwModifiers = w32_get_modifiers ();
3232 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3233 signal_user_input ();
3234 /* Non-zero must be returned when WM_MOUSEHWHEEL messages are
3235 handled, to prevent the system trying to handle it by faking
3236 scroll bar events. */
3237 return 1;
3238
3239 case WM_TIMER:
3240 /* Flush out saved messages if necessary. */
3241 if (wParam == mouse_button_timer)
3242 {
3243 if (saved_mouse_button_msg.msg.hwnd)
3244 {
3245 post_msg (&saved_mouse_button_msg);
3246 signal_user_input ();
3247 saved_mouse_button_msg.msg.hwnd = 0;
3248 }
3249 KillTimer (hwnd, mouse_button_timer);
3250 mouse_button_timer = 0;
3251 }
3252 else if (wParam == mouse_move_timer)
3253 {
3254 if (saved_mouse_move_msg.msg.hwnd)
3255 {
3256 post_msg (&saved_mouse_move_msg);
3257 saved_mouse_move_msg.msg.hwnd = 0;
3258 }
3259 KillTimer (hwnd, mouse_move_timer);
3260 mouse_move_timer = 0;
3261 }
3262 else if (wParam == menu_free_timer)
3263 {
3264 KillTimer (hwnd, menu_free_timer);
3265 menu_free_timer = 0;
3266 f = x_window_to_frame (dpyinfo, hwnd);
3267 /* If a popup menu is active, don't wipe its strings. */
3268 if (menubar_in_use
3269 && current_popup_menu == NULL)
3270 {
3271 /* Free memory used by owner-drawn and help-echo strings. */
3272 w32_free_menu_strings (hwnd);
3273 f->output_data.w32->menubar_active = 0;
3274 menubar_in_use = 0;
3275 }
3276 }
3277 else if (wParam == hourglass_timer)
3278 {
3279 KillTimer (hwnd, hourglass_timer);
3280 hourglass_timer = 0;
3281 w32_show_hourglass (x_window_to_frame (dpyinfo, hwnd));
3282 }
3283 return 0;
3284
3285 case WM_NCACTIVATE:
3286 /* Windows doesn't send us focus messages when putting up and
3287 taking down a system popup dialog as for Ctrl-Alt-Del on Windows 95.
3288 The only indication we get that something happened is receiving
3289 this message afterwards. So this is a good time to reset our
3290 keyboard modifiers' state. */
3291 reset_modifiers ();
3292 goto dflt;
3293
3294 case WM_INITMENU:
3295 button_state = 0;
3296 ReleaseCapture ();
3297 /* We must ensure menu bar is fully constructed and up to date
3298 before allowing user interaction with it. To achieve this
3299 we send this message to the lisp thread and wait for a
3300 reply (whose value is not actually needed) to indicate that
3301 the menu bar is now ready for use, so we can now return.
3302
3303 To remain responsive in the meantime, we enter a nested message
3304 loop that can process all other messages.
3305
3306 However, we skip all this if the message results from calling
3307 TrackPopupMenu - in fact, we must NOT attempt to send the lisp
3308 thread a message because it is blocked on us at this point. We
3309 set menubar_active before calling TrackPopupMenu to indicate
3310 this (there is no possibility of confusion with real menubar
3311 being active). */
3312
3313 f = x_window_to_frame (dpyinfo, hwnd);
3314 if (f
3315 && (f->output_data.w32->menubar_active
3316 /* We can receive this message even in the absence of a
3317 menubar (ie. when the system menu is activated) - in this
3318 case we do NOT want to forward the message, otherwise it
3319 will cause the menubar to suddenly appear when the user
3320 had requested it to be turned off! */
3321 || f->output_data.w32->menubar_widget == NULL))
3322 return 0;
3323
3324 {
3325 deferred_msg msg_buf;
3326
3327 /* Detect if message has already been deferred; in this case
3328 we cannot return any sensible value to ignore this. */
3329 if (find_deferred_msg (hwnd, msg) != NULL)
3330 abort ();
3331
3332 menubar_in_use = 1;
3333
3334 return send_deferred_msg (&msg_buf, hwnd, msg, wParam, lParam);
3335 }
3336
3337 case WM_EXITMENULOOP:
3338 f = x_window_to_frame (dpyinfo, hwnd);
3339
3340 /* If a menu is still active, check again after a short delay,
3341 since Windows often (always?) sends the WM_EXITMENULOOP
3342 before the corresponding WM_COMMAND message.
3343 Don't do this if a popup menu is active, since it is only
3344 menubar menus that require cleaning up in this way.
3345 */
3346 if (f && menubar_in_use && current_popup_menu == NULL)
3347 menu_free_timer = SetTimer (hwnd, MENU_FREE_ID, MENU_FREE_DELAY, NULL);
3348
3349 /* If hourglass cursor should be displayed, display it now. */
3350 if (f && f->output_data.w32->hourglass_p)
3351 SetCursor (f->output_data.w32->hourglass_cursor);
3352
3353 goto dflt;
3354
3355 case WM_MENUSELECT:
3356 /* Direct handling of help_echo in menus. Should be safe now
3357 that we generate the help_echo by placing a help event in the
3358 keyboard buffer. */
3359 {
3360 HMENU menu = (HMENU) lParam;
3361 UINT menu_item = (UINT) LOWORD (wParam);
3362 UINT flags = (UINT) HIWORD (wParam);
3363
3364 w32_menu_display_help (hwnd, menu, menu_item, flags);
3365 }
3366 return 0;
3367
3368 case WM_MEASUREITEM:
3369 f = x_window_to_frame (dpyinfo, hwnd);
3370 if (f)
3371 {
3372 MEASUREITEMSTRUCT * pMis = (MEASUREITEMSTRUCT *) lParam;
3373
3374 if (pMis->CtlType == ODT_MENU)
3375 {
3376 /* Work out dimensions for popup menu titles. */
3377 char * title = (char *) pMis->itemData;
3378 HDC hdc = GetDC (hwnd);
3379 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
3380 LOGFONT menu_logfont;
3381 HFONT old_font;
3382 SIZE size;
3383
3384 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
3385 menu_logfont.lfWeight = FW_BOLD;
3386 menu_font = CreateFontIndirect (&menu_logfont);
3387 old_font = SelectObject (hdc, menu_font);
3388
3389 pMis->itemHeight = GetSystemMetrics (SM_CYMENUSIZE);
3390 if (title)
3391 {
3392 if (unicode_append_menu)
3393 GetTextExtentPoint32W (hdc, (WCHAR *) title,
3394 wcslen ((WCHAR *) title),
3395 &size);
3396 else
3397 GetTextExtentPoint32 (hdc, title, strlen (title), &size);
3398
3399 pMis->itemWidth = size.cx;
3400 if (pMis->itemHeight < size.cy)
3401 pMis->itemHeight = size.cy;
3402 }
3403 else
3404 pMis->itemWidth = 0;
3405
3406 SelectObject (hdc, old_font);
3407 DeleteObject (menu_font);
3408 ReleaseDC (hwnd, hdc);
3409 return TRUE;
3410 }
3411 }
3412 return 0;
3413
3414 case WM_DRAWITEM:
3415 f = x_window_to_frame (dpyinfo, hwnd);
3416 if (f)
3417 {
3418 DRAWITEMSTRUCT * pDis = (DRAWITEMSTRUCT *) lParam;
3419
3420 if (pDis->CtlType == ODT_MENU)
3421 {
3422 /* Draw popup menu title. */
3423 char * title = (char *) pDis->itemData;
3424 if (title)
3425 {
3426 HDC hdc = pDis->hDC;
3427 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
3428 LOGFONT menu_logfont;
3429 HFONT old_font;
3430
3431 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
3432 menu_logfont.lfWeight = FW_BOLD;
3433 menu_font = CreateFontIndirect (&menu_logfont);
3434 old_font = SelectObject (hdc, menu_font);
3435
3436 /* Always draw title as if not selected. */
3437 if (unicode_append_menu)
3438 ExtTextOutW (hdc,
3439 pDis->rcItem.left
3440 + GetSystemMetrics (SM_CXMENUCHECK),
3441 pDis->rcItem.top,
3442 ETO_OPAQUE, &pDis->rcItem,
3443 (WCHAR *) title,
3444 wcslen ((WCHAR *) title), NULL);
3445 else
3446 ExtTextOut (hdc,
3447 pDis->rcItem.left
3448 + GetSystemMetrics (SM_CXMENUCHECK),
3449 pDis->rcItem.top,
3450 ETO_OPAQUE, &pDis->rcItem,
3451 title, strlen (title), NULL);
3452
3453 SelectObject (hdc, old_font);
3454 DeleteObject (menu_font);
3455 }
3456 return TRUE;
3457 }
3458 }
3459 return 0;
3460
3461 #if 0
3462 /* Still not right - can't distinguish between clicks in the
3463 client area of the frame from clicks forwarded from the scroll
3464 bars - may have to hook WM_NCHITTEST to remember the mouse
3465 position and then check if it is in the client area ourselves. */
3466 case WM_MOUSEACTIVATE:
3467 /* Discard the mouse click that activates a frame, allowing the
3468 user to click anywhere without changing point (or worse!).
3469 Don't eat mouse clicks on scrollbars though!! */
3470 if (LOWORD (lParam) == HTCLIENT )
3471 return MA_ACTIVATEANDEAT;
3472 goto dflt;
3473 #endif
3474
3475 case WM_MOUSELEAVE:
3476 /* No longer tracking mouse. */
3477 track_mouse_window = NULL;
3478
3479 case WM_ACTIVATEAPP:
3480 case WM_ACTIVATE:
3481 case WM_WINDOWPOSCHANGED:
3482 case WM_SHOWWINDOW:
3483 /* Inform lisp thread that a frame might have just been obscured
3484 or exposed, so should recheck visibility of all frames. */
3485 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3486 goto dflt;
3487
3488 case WM_SETFOCUS:
3489 dpyinfo->faked_key = 0;
3490 reset_modifiers ();
3491 register_hot_keys (hwnd);
3492 goto command;
3493 case WM_KILLFOCUS:
3494 unregister_hot_keys (hwnd);
3495 button_state = 0;
3496 ReleaseCapture ();
3497 /* Relinquish the system caret. */
3498 if (w32_system_caret_hwnd)
3499 {
3500 w32_visible_system_caret_hwnd = NULL;
3501 w32_system_caret_hwnd = NULL;
3502 DestroyCaret ();
3503 }
3504 goto command;
3505 case WM_COMMAND:
3506 menubar_in_use = 0;
3507 f = x_window_to_frame (dpyinfo, hwnd);
3508 if (f && HIWORD (wParam) == 0)
3509 {
3510 if (menu_free_timer)
3511 {
3512 KillTimer (hwnd, menu_free_timer);
3513 menu_free_timer = 0;
3514 }
3515 }
3516 case WM_MOVE:
3517 case WM_SIZE:
3518 command:
3519 wmsg.dwModifiers = w32_get_modifiers ();
3520 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3521 goto dflt;
3522
3523 case WM_DESTROY:
3524 CoUninitialize ();
3525 return 0;
3526
3527 case WM_CLOSE:
3528 wmsg.dwModifiers = w32_get_modifiers ();
3529 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3530 return 0;
3531
3532 case WM_WINDOWPOSCHANGING:
3533 /* Don't restrict the sizing of tip frames. */
3534 if (hwnd == tip_window)
3535 return 0;
3536 {
3537 WINDOWPLACEMENT wp;
3538 LPWINDOWPOS lppos = (WINDOWPOS *) lParam;
3539
3540 wp.length = sizeof (WINDOWPLACEMENT);
3541 GetWindowPlacement (hwnd, &wp);
3542
3543 if (wp.showCmd != SW_SHOWMINIMIZED && (lppos->flags & SWP_NOSIZE) == 0)
3544 {
3545 RECT rect;
3546 int wdiff;
3547 int hdiff;
3548 DWORD font_width;
3549 DWORD line_height;
3550 DWORD internal_border;
3551 DWORD scrollbar_extra;
3552 RECT wr;
3553
3554 wp.length = sizeof (wp);
3555 GetWindowRect (hwnd, &wr);
3556
3557 enter_crit ();
3558
3559 font_width = GetWindowLong (hwnd, WND_FONTWIDTH_INDEX);
3560 line_height = GetWindowLong (hwnd, WND_LINEHEIGHT_INDEX);
3561 internal_border = GetWindowLong (hwnd, WND_BORDER_INDEX);
3562 scrollbar_extra = GetWindowLong (hwnd, WND_SCROLLBAR_INDEX);
3563
3564 leave_crit ();
3565
3566 memset (&rect, 0, sizeof (rect));
3567 AdjustWindowRect (&rect, GetWindowLong (hwnd, GWL_STYLE),
3568 GetMenu (hwnd) != NULL);
3569
3570 /* Force width and height of client area to be exact
3571 multiples of the character cell dimensions. */
3572 wdiff = (lppos->cx - (rect.right - rect.left)
3573 - 2 * internal_border - scrollbar_extra)
3574 % font_width;
3575 hdiff = (lppos->cy - (rect.bottom - rect.top)
3576 - 2 * internal_border)
3577 % line_height;
3578
3579 if (wdiff || hdiff)
3580 {
3581 /* For right/bottom sizing we can just fix the sizes.
3582 However for top/left sizing we will need to fix the X
3583 and Y positions as well. */
3584
3585 int cx_mintrack = GetSystemMetrics (SM_CXMINTRACK);
3586 int cy_mintrack = GetSystemMetrics (SM_CYMINTRACK);
3587
3588 lppos->cx = max (lppos->cx - wdiff, cx_mintrack);
3589 lppos->cy = max (lppos->cy - hdiff, cy_mintrack);
3590
3591 if (wp.showCmd != SW_SHOWMAXIMIZED
3592 && (lppos->flags & SWP_NOMOVE) == 0)
3593 {
3594 if (lppos->x != wr.left || lppos->y != wr.top)
3595 {
3596 lppos->x += wdiff;
3597 lppos->y += hdiff;
3598 }
3599 else
3600 {
3601 lppos->flags |= SWP_NOMOVE;
3602 }
3603 }
3604
3605 return 0;
3606 }
3607 }
3608 }
3609
3610 goto dflt;
3611
3612 case WM_GETMINMAXINFO:
3613 /* Hack to allow resizing the Emacs frame above the screen size.
3614 Note that Windows 9x limits coordinates to 16-bits. */
3615 ((LPMINMAXINFO) lParam)->ptMaxTrackSize.x = 32767;
3616 ((LPMINMAXINFO) lParam)->ptMaxTrackSize.y = 32767;
3617 return 0;
3618
3619 case WM_SETCURSOR:
3620 if (LOWORD (lParam) == HTCLIENT)
3621 {
3622 f = x_window_to_frame (dpyinfo, hwnd);
3623 if (f->output_data.w32->hourglass_p && !menubar_in_use
3624 && !current_popup_menu)
3625 SetCursor (f->output_data.w32->hourglass_cursor);
3626 else
3627 SetCursor (f->output_data.w32->current_cursor);
3628 return 0;
3629 }
3630 goto dflt;
3631
3632 case WM_EMACS_SETCURSOR:
3633 {
3634 Cursor cursor = (Cursor) wParam;
3635 f = x_window_to_frame (dpyinfo, hwnd);
3636 if (f && cursor)
3637 {
3638 f->output_data.w32->current_cursor = cursor;
3639 if (!f->output_data.w32->hourglass_p)
3640 SetCursor (cursor);
3641 }
3642 return 0;
3643 }
3644
3645 case WM_EMACS_CREATESCROLLBAR:
3646 return (LRESULT) w32_createscrollbar ((struct frame *) wParam,
3647 (struct scroll_bar *) lParam);
3648
3649 case WM_EMACS_SHOWWINDOW:
3650 return ShowWindow ((HWND) wParam, (WPARAM) lParam);
3651
3652 case WM_EMACS_SETFOREGROUND:
3653 {
3654 HWND foreground_window;
3655 DWORD foreground_thread, retval;
3656
3657 /* On NT 5.0, and apparently Windows 98, it is necessary to
3658 attach to the thread that currently has focus in order to
3659 pull the focus away from it. */
3660 foreground_window = GetForegroundWindow ();
3661 foreground_thread = GetWindowThreadProcessId (foreground_window, NULL);
3662 if (!foreground_window
3663 || foreground_thread == GetCurrentThreadId ()
3664 || !AttachThreadInput (GetCurrentThreadId (),
3665 foreground_thread, TRUE))
3666 foreground_thread = 0;
3667
3668 retval = SetForegroundWindow ((HWND) wParam);
3669
3670 /* Detach from the previous foreground thread. */
3671 if (foreground_thread)
3672 AttachThreadInput (GetCurrentThreadId (),
3673 foreground_thread, FALSE);
3674
3675 return retval;
3676 }
3677
3678 case WM_EMACS_SETWINDOWPOS:
3679 {
3680 WINDOWPOS * pos = (WINDOWPOS *) wParam;
3681 return SetWindowPos (hwnd, pos->hwndInsertAfter,
3682 pos->x, pos->y, pos->cx, pos->cy, pos->flags);
3683 }
3684
3685 case WM_EMACS_DESTROYWINDOW:
3686 DragAcceptFiles ((HWND) wParam, FALSE);
3687 return DestroyWindow ((HWND) wParam);
3688
3689 case WM_EMACS_HIDE_CARET:
3690 return HideCaret (hwnd);
3691
3692 case WM_EMACS_SHOW_CARET:
3693 return ShowCaret (hwnd);
3694
3695 case WM_EMACS_DESTROY_CARET:
3696 w32_system_caret_hwnd = NULL;
3697 w32_visible_system_caret_hwnd = NULL;
3698 return DestroyCaret ();
3699
3700 case WM_EMACS_TRACK_CARET:
3701 /* If there is currently no system caret, create one. */
3702 if (w32_system_caret_hwnd == NULL)
3703 {
3704 /* Use the default caret width, and avoid changing it
3705 unneccesarily, as it confuses screen reader software. */
3706 w32_system_caret_hwnd = hwnd;
3707 CreateCaret (hwnd, NULL, 0,
3708 w32_system_caret_height);
3709 }
3710
3711 if (!SetCaretPos (w32_system_caret_x, w32_system_caret_y))
3712 return 0;
3713 /* Ensure visible caret gets turned on when requested. */
3714 else if (w32_use_visible_system_caret
3715 && w32_visible_system_caret_hwnd != hwnd)
3716 {
3717 w32_visible_system_caret_hwnd = hwnd;
3718 return ShowCaret (hwnd);
3719 }
3720 /* Ensure visible caret gets turned off when requested. */
3721 else if (!w32_use_visible_system_caret
3722 && w32_visible_system_caret_hwnd)
3723 {
3724 w32_visible_system_caret_hwnd = NULL;
3725 return HideCaret (hwnd);
3726 }
3727 else
3728 return 1;
3729
3730 case WM_EMACS_TRACKPOPUPMENU:
3731 {
3732 UINT flags;
3733 POINT *pos;
3734 int retval;
3735 pos = (POINT *)lParam;
3736 flags = TPM_CENTERALIGN;
3737 if (button_state & LMOUSE)
3738 flags |= TPM_LEFTBUTTON;
3739 else if (button_state & RMOUSE)
3740 flags |= TPM_RIGHTBUTTON;
3741
3742 /* Remember we did a SetCapture on the initial mouse down event,
3743 so for safety, we make sure the capture is cancelled now. */
3744 ReleaseCapture ();
3745 button_state = 0;
3746
3747 /* Use menubar_active to indicate that WM_INITMENU is from
3748 TrackPopupMenu below, and should be ignored. */
3749 f = x_window_to_frame (dpyinfo, hwnd);
3750 if (f)
3751 f->output_data.w32->menubar_active = 1;
3752
3753 if (TrackPopupMenu ((HMENU)wParam, flags, pos->x, pos->y,
3754 0, hwnd, NULL))
3755 {
3756 MSG amsg;
3757 /* Eat any mouse messages during popupmenu */
3758 while (PeekMessage (&amsg, hwnd, WM_MOUSEFIRST, WM_MOUSELAST,
3759 PM_REMOVE));
3760 /* Get the menu selection, if any */
3761 if (PeekMessage (&amsg, hwnd, WM_COMMAND, WM_COMMAND, PM_REMOVE))
3762 {
3763 retval = LOWORD (amsg.wParam);
3764 }
3765 else
3766 {
3767 retval = 0;
3768 }
3769 }
3770 else
3771 {
3772 retval = -1;
3773 }
3774
3775 return retval;
3776 }
3777
3778 default:
3779 /* Check for messages registered at runtime. */
3780 if (msg == msh_mousewheel)
3781 {
3782 wmsg.dwModifiers = w32_get_modifiers ();
3783 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3784 signal_user_input ();
3785 return 0;
3786 }
3787
3788 dflt:
3789 return DefWindowProc (hwnd, msg, wParam, lParam);
3790 }
3791
3792 /* The most common default return code for handled messages is 0. */
3793 return 0;
3794 }
3795
3796 static void
3797 my_create_window (struct frame * f)
3798 {
3799 MSG msg;
3800
3801 if (!PostThreadMessage (dwWindowsThreadId, WM_EMACS_CREATEWINDOW, (WPARAM)f, 0))
3802 abort ();
3803 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
3804 }
3805
3806
3807 /* Create a tooltip window. Unlike my_create_window, we do not do this
3808 indirectly via the Window thread, as we do not need to process Window
3809 messages for the tooltip. Creating tooltips indirectly also creates
3810 deadlocks when tooltips are created for menu items. */
3811 static void
3812 my_create_tip_window (struct frame *f)
3813 {
3814 RECT rect;
3815
3816 rect.left = rect.top = 0;
3817 rect.right = FRAME_PIXEL_WIDTH (f);
3818 rect.bottom = FRAME_PIXEL_HEIGHT (f);
3819
3820 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
3821 FRAME_EXTERNAL_MENU_BAR (f));
3822
3823 tip_window = FRAME_W32_WINDOW (f)
3824 = CreateWindow (EMACS_CLASS,
3825 f->namebuf,
3826 f->output_data.w32->dwStyle,
3827 f->left_pos,
3828 f->top_pos,
3829 rect.right - rect.left,
3830 rect.bottom - rect.top,
3831 FRAME_W32_WINDOW (SELECTED_FRAME ()), /* owner */
3832 NULL,
3833 hinst,
3834 NULL);
3835
3836 if (tip_window)
3837 {
3838 SetWindowLong (tip_window, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
3839 SetWindowLong (tip_window, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
3840 SetWindowLong (tip_window, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
3841 SetWindowLong (tip_window, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
3842
3843 /* Tip frames have no scrollbars. */
3844 SetWindowLong (tip_window, WND_SCROLLBAR_INDEX, 0);
3845
3846 /* Do this to discard the default setting specified by our parent. */
3847 ShowWindow (tip_window, SW_HIDE);
3848 }
3849 }
3850
3851
3852 /* Create and set up the w32 window for frame F. */
3853
3854 static void
3855 w32_window (struct frame *f, long window_prompting, int minibuffer_only)
3856 {
3857 BLOCK_INPUT;
3858
3859 /* Use the resource name as the top-level window name
3860 for looking up resources. Make a non-Lisp copy
3861 for the window manager, so GC relocation won't bother it.
3862
3863 Elsewhere we specify the window name for the window manager. */
3864
3865 {
3866 char *str = SSDATA (Vx_resource_name);
3867 f->namebuf = (char *) xmalloc (strlen (str) + 1);
3868 strcpy (f->namebuf, str);
3869 }
3870
3871 my_create_window (f);
3872
3873 validate_x_resource_name ();
3874
3875 /* x_set_name normally ignores requests to set the name if the
3876 requested name is the same as the current name. This is the one
3877 place where that assumption isn't correct; f->name is set, but
3878 the server hasn't been told. */
3879 {
3880 Lisp_Object name;
3881 int explicit = f->explicit_name;
3882
3883 f->explicit_name = 0;
3884 name = f->name;
3885 f->name = Qnil;
3886 x_set_name (f, name, explicit);
3887 }
3888
3889 UNBLOCK_INPUT;
3890
3891 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
3892 initialize_frame_menubar (f);
3893
3894 if (FRAME_W32_WINDOW (f) == 0)
3895 error ("Unable to create window");
3896 }
3897
3898 /* Handle the icon stuff for this window. Perhaps later we might
3899 want an x_set_icon_position which can be called interactively as
3900 well. */
3901
3902 static void
3903 x_icon (struct frame *f, Lisp_Object parms)
3904 {
3905 Lisp_Object icon_x, icon_y;
3906 struct w32_display_info *dpyinfo = &one_w32_display_info;
3907
3908 /* Set the position of the icon. Note that Windows 95 groups all
3909 icons in the tray. */
3910 icon_x = x_get_arg (dpyinfo, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
3911 icon_y = x_get_arg (dpyinfo, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
3912 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
3913 {
3914 CHECK_NUMBER (icon_x);
3915 CHECK_NUMBER (icon_y);
3916 }
3917 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
3918 error ("Both left and top icon corners of icon must be specified");
3919
3920 BLOCK_INPUT;
3921
3922 if (! EQ (icon_x, Qunbound))
3923 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
3924
3925 #if 0 /* TODO */
3926 /* Start up iconic or window? */
3927 x_wm_set_window_state
3928 (f, (EQ (x_get_arg (dpyinfo, parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL), Qicon)
3929 ? IconicState
3930 : NormalState));
3931
3932 x_text_icon (f, SSDATA ((!NILP (f->icon_name)
3933 ? f->icon_name
3934 : f->name)));
3935 #endif
3936
3937 UNBLOCK_INPUT;
3938 }
3939
3940
3941 static void
3942 x_make_gc (struct frame *f)
3943 {
3944 XGCValues gc_values;
3945
3946 BLOCK_INPUT;
3947
3948 /* Create the GC's of this frame.
3949 Note that many default values are used. */
3950
3951 /* Normal video */
3952 gc_values.font = FRAME_FONT (f);
3953
3954 /* Cursor has cursor-color background, background-color foreground. */
3955 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
3956 gc_values.background = f->output_data.w32->cursor_pixel;
3957 f->output_data.w32->cursor_gc
3958 = XCreateGC (NULL, FRAME_W32_WINDOW (f),
3959 (GCFont | GCForeground | GCBackground),
3960 &gc_values);
3961
3962 /* Reliefs. */
3963 f->output_data.w32->white_relief.gc = 0;
3964 f->output_data.w32->black_relief.gc = 0;
3965
3966 UNBLOCK_INPUT;
3967 }
3968
3969
3970 /* Handler for signals raised during x_create_frame and
3971 x_create_top_frame. FRAME is the frame which is partially
3972 constructed. */
3973
3974 static Lisp_Object
3975 unwind_create_frame (Lisp_Object frame)
3976 {
3977 struct frame *f = XFRAME (frame);
3978
3979 /* If frame is ``official'', nothing to do. */
3980 if (!CONSP (Vframe_list) || !EQ (XCAR (Vframe_list), frame))
3981 {
3982 #if GLYPH_DEBUG
3983 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
3984 #endif
3985
3986 x_free_frame_resources (f);
3987
3988 #if GLYPH_DEBUG
3989 /* Check that reference counts are indeed correct. */
3990 xassert (dpyinfo->reference_count == dpyinfo_refcount);
3991 xassert (dpyinfo->image_cache->refcount == image_cache_refcount);
3992 #endif
3993 return Qt;
3994 }
3995
3996 return Qnil;
3997 }
3998
3999 static void
4000 x_default_font_parameter (struct frame *f, Lisp_Object parms)
4001 {
4002 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4003 Lisp_Object font_param = x_get_arg (dpyinfo, parms, Qfont, NULL, NULL,
4004 RES_TYPE_STRING);
4005 Lisp_Object font;
4006 if (EQ (font_param, Qunbound))
4007 font_param = Qnil;
4008 font = !NILP (font_param) ? font_param
4009 : x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
4010
4011 if (!STRINGP (font))
4012 {
4013 int i;
4014 static char *names[]
4015 = { "Courier New-10",
4016 "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1",
4017 "-*-Fixedsys-normal-r-*-*-12-*-*-*-c-*-iso8859-1",
4018 "Fixedsys",
4019 NULL };
4020
4021 for (i = 0; names[i]; i++)
4022 {
4023 font = font_open_by_name (f, names[i]);
4024 if (! NILP (font))
4025 break;
4026 }
4027 if (NILP (font))
4028 error ("No suitable font was found");
4029 }
4030 else if (!NILP (font_param))
4031 {
4032 /* Remember the explicit font parameter, so we can re-apply it after
4033 we've applied the `default' face settings. */
4034 x_set_frame_parameters (f, Fcons (Fcons (Qfont_param, font_param), Qnil));
4035 }
4036 x_default_parameter (f, parms, Qfont, font, "font", "Font", RES_TYPE_STRING);
4037 }
4038
4039 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
4040 1, 1, 0,
4041 doc: /* Make a new window, which is called a \"frame\" in Emacs terms.
4042 Return an Emacs frame object.
4043 PARAMETERS is an alist of frame parameters.
4044 If the parameters specify that the frame should not have a minibuffer,
4045 and do not specify a specific minibuffer window to use,
4046 then `default-minibuffer-frame' must be a frame whose minibuffer can
4047 be shared by the new frame.
4048
4049 This function is an internal primitive--use `make-frame' instead. */)
4050 (Lisp_Object parameters)
4051 {
4052 struct frame *f;
4053 Lisp_Object frame, tem;
4054 Lisp_Object name;
4055 int minibuffer_only = 0;
4056 long window_prompting = 0;
4057 int width, height;
4058 int count = SPECPDL_INDEX ();
4059 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4060 Lisp_Object display;
4061 struct w32_display_info *dpyinfo = NULL;
4062 Lisp_Object parent;
4063 struct kboard *kb;
4064
4065 /* Make copy of frame parameters because the original is in pure
4066 storage now. */
4067 parameters = Fcopy_alist (parameters);
4068
4069 /* Use this general default value to start with
4070 until we know if this frame has a specified name. */
4071 Vx_resource_name = Vinvocation_name;
4072
4073 display = x_get_arg (dpyinfo, parameters, Qterminal, 0, 0, RES_TYPE_NUMBER);
4074 if (EQ (display, Qunbound))
4075 display = x_get_arg (dpyinfo, parameters, Qdisplay, 0, 0, RES_TYPE_STRING);
4076 if (EQ (display, Qunbound))
4077 display = Qnil;
4078 dpyinfo = check_x_display_info (display);
4079 kb = dpyinfo->terminal->kboard;
4080
4081 if (!dpyinfo->terminal->name)
4082 error ("Terminal is not live, can't create new frames on it");
4083
4084 name = x_get_arg (dpyinfo, parameters, Qname, "name", "Name", RES_TYPE_STRING);
4085 if (!STRINGP (name)
4086 && ! EQ (name, Qunbound)
4087 && ! NILP (name))
4088 error ("Invalid frame name--not a string or nil");
4089
4090 if (STRINGP (name))
4091 Vx_resource_name = name;
4092
4093 /* See if parent window is specified. */
4094 parent = x_get_arg (dpyinfo, parameters, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
4095 if (EQ (parent, Qunbound))
4096 parent = Qnil;
4097 if (! NILP (parent))
4098 CHECK_NUMBER (parent);
4099
4100 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
4101 /* No need to protect DISPLAY because that's not used after passing
4102 it to make_frame_without_minibuffer. */
4103 frame = Qnil;
4104 GCPRO4 (parameters, parent, name, frame);
4105 tem = x_get_arg (dpyinfo, parameters, Qminibuffer, "minibuffer", "Minibuffer",
4106 RES_TYPE_SYMBOL);
4107 if (EQ (tem, Qnone) || NILP (tem))
4108 f = make_frame_without_minibuffer (Qnil, kb, display);
4109 else if (EQ (tem, Qonly))
4110 {
4111 f = make_minibuffer_frame ();
4112 minibuffer_only = 1;
4113 }
4114 else if (WINDOWP (tem))
4115 f = make_frame_without_minibuffer (tem, kb, display);
4116 else
4117 f = make_frame (1);
4118
4119 XSETFRAME (frame, f);
4120
4121 /* Note that Windows does support scroll bars. */
4122 FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;
4123
4124 /* By default, make scrollbars the system standard width. */
4125 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = GetSystemMetrics (SM_CXVSCROLL);
4126
4127 f->terminal = dpyinfo->terminal;
4128 f->terminal->reference_count++;
4129
4130 f->output_method = output_w32;
4131 f->output_data.w32 =
4132 (struct w32_output *) xmalloc (sizeof (struct w32_output));
4133 memset (f->output_data.w32, 0, sizeof (struct w32_output));
4134 FRAME_FONTSET (f) = -1;
4135
4136 f->icon_name
4137 = x_get_arg (dpyinfo, parameters, Qicon_name, "iconName", "Title",
4138 RES_TYPE_STRING);
4139 if (! STRINGP (f->icon_name))
4140 f->icon_name = Qnil;
4141
4142 /* FRAME_W32_DISPLAY_INFO (f) = dpyinfo; */
4143
4144 /* With FRAME_X_DISPLAY_INFO set up, this unwind-protect is safe. */
4145 record_unwind_protect (unwind_create_frame, frame);
4146 #if GLYPH_DEBUG
4147 image_cache_refcount = FRAME_IMAGE_CACHE (f)->refcount;
4148 dpyinfo_refcount = dpyinfo->reference_count;
4149 #endif /* GLYPH_DEBUG */
4150
4151 /* Specify the parent under which to make this window. */
4152
4153 if (!NILP (parent))
4154 {
4155 f->output_data.w32->parent_desc = (Window) XFASTINT (parent);
4156 f->output_data.w32->explicit_parent = 1;
4157 }
4158 else
4159 {
4160 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
4161 f->output_data.w32->explicit_parent = 0;
4162 }
4163
4164 /* Set the name; the functions to which we pass f expect the name to
4165 be set. */
4166 if (EQ (name, Qunbound) || NILP (name))
4167 {
4168 f->name = build_string (dpyinfo->w32_id_name);
4169 f->explicit_name = 0;
4170 }
4171 else
4172 {
4173 f->name = name;
4174 f->explicit_name = 1;
4175 /* use the frame's title when getting resources for this frame. */
4176 specbind (Qx_resource_name, name);
4177 }
4178
4179 f->resx = dpyinfo->resx;
4180 f->resy = dpyinfo->resy;
4181
4182 if (uniscribe_available)
4183 register_font_driver (&uniscribe_font_driver, f);
4184 register_font_driver (&w32font_driver, f);
4185
4186 x_default_parameter (f, parameters, Qfont_backend, Qnil,
4187 "fontBackend", "FontBackend", RES_TYPE_STRING);
4188 /* Extract the window parameters from the supplied values
4189 that are needed to determine window geometry. */
4190 x_default_font_parameter (f, parameters);
4191 x_default_parameter (f, parameters, Qborder_width, make_number (2),
4192 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
4193
4194 /* We recognize either internalBorderWidth or internalBorder
4195 (which is what xterm calls it). */
4196 if (NILP (Fassq (Qinternal_border_width, parameters)))
4197 {
4198 Lisp_Object value;
4199
4200 value = x_get_arg (dpyinfo, parameters, Qinternal_border_width,
4201 "internalBorder", "InternalBorder", RES_TYPE_NUMBER);
4202 if (! EQ (value, Qunbound))
4203 parameters = Fcons (Fcons (Qinternal_border_width, value),
4204 parameters);
4205 }
4206 /* Default internalBorderWidth to 0 on Windows to match other programs. */
4207 x_default_parameter (f, parameters, Qinternal_border_width, make_number (0),
4208 "internalBorderWidth", "InternalBorder", RES_TYPE_NUMBER);
4209 x_default_parameter (f, parameters, Qvertical_scroll_bars, Qright,
4210 "verticalScrollBars", "ScrollBars", RES_TYPE_SYMBOL);
4211
4212 /* Also do the stuff which must be set before the window exists. */
4213 x_default_parameter (f, parameters, Qforeground_color, build_string ("black"),
4214 "foreground", "Foreground", RES_TYPE_STRING);
4215 x_default_parameter (f, parameters, Qbackground_color, build_string ("white"),
4216 "background", "Background", RES_TYPE_STRING);
4217 x_default_parameter (f, parameters, Qmouse_color, build_string ("black"),
4218 "pointerColor", "Foreground", RES_TYPE_STRING);
4219 x_default_parameter (f, parameters, Qborder_color, build_string ("black"),
4220 "borderColor", "BorderColor", RES_TYPE_STRING);
4221 x_default_parameter (f, parameters, Qscreen_gamma, Qnil,
4222 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
4223 x_default_parameter (f, parameters, Qline_spacing, Qnil,
4224 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
4225 x_default_parameter (f, parameters, Qleft_fringe, Qnil,
4226 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
4227 x_default_parameter (f, parameters, Qright_fringe, Qnil,
4228 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
4229
4230 /* Init faces before x_default_parameter is called for scroll-bar
4231 parameters because that function calls x_set_scroll_bar_width,
4232 which calls change_frame_size, which calls Fset_window_buffer,
4233 which runs hooks, which call Fvertical_motion. At the end, we
4234 end up in init_iterator with a null face cache, which should not
4235 happen. */
4236 init_frame_faces (f);
4237
4238 /* The X resources controlling the menu-bar and tool-bar are
4239 processed specially at startup, and reflected in the mode
4240 variables; ignore them here. */
4241 x_default_parameter (f, parameters, Qmenu_bar_lines,
4242 NILP (Vmenu_bar_mode)
4243 ? make_number (0) : make_number (1),
4244 NULL, NULL, RES_TYPE_NUMBER);
4245 x_default_parameter (f, parameters, Qtool_bar_lines,
4246 NILP (Vtool_bar_mode)
4247 ? make_number (0) : make_number (1),
4248 NULL, NULL, RES_TYPE_NUMBER);
4249
4250 x_default_parameter (f, parameters, Qbuffer_predicate, Qnil,
4251 "bufferPredicate", "BufferPredicate", RES_TYPE_SYMBOL);
4252 x_default_parameter (f, parameters, Qtitle, Qnil,
4253 "title", "Title", RES_TYPE_STRING);
4254 x_default_parameter (f, parameters, Qfullscreen, Qnil,
4255 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
4256
4257 f->output_data.w32->dwStyle = WS_OVERLAPPEDWINDOW;
4258 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
4259
4260 f->output_data.w32->text_cursor = w32_load_cursor (IDC_IBEAM);
4261 f->output_data.w32->nontext_cursor = w32_load_cursor (IDC_ARROW);
4262 f->output_data.w32->modeline_cursor = w32_load_cursor (IDC_ARROW);
4263 f->output_data.w32->hand_cursor = w32_load_cursor (IDC_HAND);
4264 f->output_data.w32->hourglass_cursor = w32_load_cursor (IDC_WAIT);
4265 f->output_data.w32->horizontal_drag_cursor = w32_load_cursor (IDC_SIZEWE);
4266
4267 f->output_data.w32->current_cursor = f->output_data.w32->nontext_cursor;
4268
4269 window_prompting = x_figure_window_size (f, parameters, 1);
4270
4271 tem = x_get_arg (dpyinfo, parameters, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
4272 f->no_split = minibuffer_only || EQ (tem, Qt);
4273
4274 w32_window (f, window_prompting, minibuffer_only);
4275 x_icon (f, parameters);
4276
4277 x_make_gc (f);
4278
4279 /* Now consider the frame official. */
4280 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
4281 Vframe_list = Fcons (frame, Vframe_list);
4282
4283 /* We need to do this after creating the window, so that the
4284 icon-creation functions can say whose icon they're describing. */
4285 x_default_parameter (f, parameters, Qicon_type, Qnil,
4286 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
4287
4288 x_default_parameter (f, parameters, Qauto_raise, Qnil,
4289 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4290 x_default_parameter (f, parameters, Qauto_lower, Qnil,
4291 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4292 x_default_parameter (f, parameters, Qcursor_type, Qbox,
4293 "cursorType", "CursorType", RES_TYPE_SYMBOL);
4294 x_default_parameter (f, parameters, Qscroll_bar_width, Qnil,
4295 "scrollBarWidth", "ScrollBarWidth", RES_TYPE_NUMBER);
4296 x_default_parameter (f, parameters, Qalpha, Qnil,
4297 "alpha", "Alpha", RES_TYPE_NUMBER);
4298
4299 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
4300 Change will not be effected unless different from the current
4301 FRAME_LINES (f). */
4302 width = FRAME_COLS (f);
4303 height = FRAME_LINES (f);
4304
4305 FRAME_LINES (f) = 0;
4306 SET_FRAME_COLS (f, 0);
4307 change_frame_size (f, height, width, 1, 0, 0);
4308
4309 /* Tell the server what size and position, etc, we want, and how
4310 badly we want them. This should be done after we have the menu
4311 bar so that its size can be taken into account. */
4312 BLOCK_INPUT;
4313 x_wm_set_size_hint (f, window_prompting, 0);
4314 UNBLOCK_INPUT;
4315
4316 /* Make the window appear on the frame and enable display, unless
4317 the caller says not to. However, with explicit parent, Emacs
4318 cannot control visibility, so don't try. */
4319 if (! f->output_data.w32->explicit_parent)
4320 {
4321 Lisp_Object visibility;
4322
4323 visibility = x_get_arg (dpyinfo, parameters, Qvisibility, 0, 0, RES_TYPE_SYMBOL);
4324 if (EQ (visibility, Qunbound))
4325 visibility = Qt;
4326
4327 if (EQ (visibility, Qicon))
4328 x_iconify_frame (f);
4329 else if (! NILP (visibility))
4330 x_make_frame_visible (f);
4331 else
4332 /* Must have been Qnil. */
4333 ;
4334 }
4335
4336 /* Initialize `default-minibuffer-frame' in case this is the first
4337 frame on this terminal. */
4338 if (FRAME_HAS_MINIBUF_P (f)
4339 && (!FRAMEP (KVAR (kb, Vdefault_minibuffer_frame))
4340 || !FRAME_LIVE_P (XFRAME (KVAR (kb, Vdefault_minibuffer_frame)))))
4341 KVAR (kb, Vdefault_minibuffer_frame) = frame;
4342
4343 /* All remaining specified parameters, which have not been "used"
4344 by x_get_arg and friends, now go in the misc. alist of the frame. */
4345 for (tem = parameters; CONSP (tem); tem = XCDR (tem))
4346 if (CONSP (XCAR (tem)) && !NILP (XCAR (XCAR (tem))))
4347 f->param_alist = Fcons (XCAR (tem), f->param_alist);
4348
4349 UNGCPRO;
4350
4351 /* Make sure windows on this frame appear in calls to next-window
4352 and similar functions. */
4353 Vwindow_list = Qnil;
4354
4355 return unbind_to (count, frame);
4356 }
4357
4358 /* FRAME is used only to get a handle on the X display. We don't pass the
4359 display info directly because we're called from frame.c, which doesn't
4360 know about that structure. */
4361 Lisp_Object
4362 x_get_focus_frame (struct frame *frame)
4363 {
4364 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (frame);
4365 Lisp_Object xfocus;
4366 if (! dpyinfo->w32_focus_frame)
4367 return Qnil;
4368
4369 XSETFRAME (xfocus, dpyinfo->w32_focus_frame);
4370 return xfocus;
4371 }
4372
4373 DEFUN ("x-focus-frame", Fx_focus_frame, Sx_focus_frame, 1, 1, 0,
4374 doc: /* Give FRAME input focus, raising to foreground if necessary. */)
4375 (Lisp_Object frame)
4376 {
4377 x_focus_on_frame (check_x_frame (frame));
4378 return Qnil;
4379 }
4380
4381 \f
4382 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
4383 doc: /* Internal function called by `color-defined-p', which see.
4384 \(Note that the Nextstep version of this function ignores FRAME.) */)
4385 (Lisp_Object color, Lisp_Object frame)
4386 {
4387 XColor foo;
4388 FRAME_PTR f = check_x_frame (frame);
4389
4390 CHECK_STRING (color);
4391
4392 if (w32_defined_color (f, SDATA (color), &foo, 0))
4393 return Qt;
4394 else
4395 return Qnil;
4396 }
4397
4398 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
4399 doc: /* Internal function called by `color-values', which see. */)
4400 (Lisp_Object color, Lisp_Object frame)
4401 {
4402 XColor foo;
4403 FRAME_PTR f = check_x_frame (frame);
4404
4405 CHECK_STRING (color);
4406
4407 if (w32_defined_color (f, SDATA (color), &foo, 0))
4408 return list3 (make_number ((GetRValue (foo.pixel) << 8)
4409 | GetRValue (foo.pixel)),
4410 make_number ((GetGValue (foo.pixel) << 8)
4411 | GetGValue (foo.pixel)),
4412 make_number ((GetBValue (foo.pixel) << 8)
4413 | GetBValue (foo.pixel)));
4414 else
4415 return Qnil;
4416 }
4417
4418 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
4419 doc: /* Internal function called by `display-color-p', which see. */)
4420 (Lisp_Object display)
4421 {
4422 struct w32_display_info *dpyinfo = check_x_display_info (display);
4423
4424 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 2)
4425 return Qnil;
4426
4427 return Qt;
4428 }
4429
4430 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p,
4431 Sx_display_grayscale_p, 0, 1, 0,
4432 doc: /* Return t if DISPLAY supports shades of gray.
4433 Note that color displays do support shades of gray.
4434 The optional argument DISPLAY specifies which display to ask about.
4435 DISPLAY should be either a frame or a display name (a string).
4436 If omitted or nil, that stands for the selected frame's display. */)
4437 (Lisp_Object display)
4438 {
4439 struct w32_display_info *dpyinfo = check_x_display_info (display);
4440
4441 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 1)
4442 return Qnil;
4443
4444 return Qt;
4445 }
4446
4447 DEFUN ("x-display-pixel-width", Fx_display_pixel_width,
4448 Sx_display_pixel_width, 0, 1, 0,
4449 doc: /* Return the width in pixels of DISPLAY.
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 return make_number (x_display_pixel_width (dpyinfo));
4458 }
4459
4460 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
4461 Sx_display_pixel_height, 0, 1, 0,
4462 doc: /* Return the height in pixels of DISPLAY.
4463 The optional argument DISPLAY specifies which display to ask about.
4464 DISPLAY should be either a frame or a display name (a string).
4465 If omitted or nil, that stands for the selected frame's display. */)
4466 (Lisp_Object display)
4467 {
4468 struct w32_display_info *dpyinfo = check_x_display_info (display);
4469
4470 return make_number (x_display_pixel_height (dpyinfo));
4471 }
4472
4473 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
4474 0, 1, 0,
4475 doc: /* Return the number of bitplanes of DISPLAY.
4476 The optional argument DISPLAY specifies which display to ask about.
4477 DISPLAY should be either a frame or a display name (a string).
4478 If omitted or nil, that stands for the selected frame's display. */)
4479 (Lisp_Object display)
4480 {
4481 struct w32_display_info *dpyinfo = check_x_display_info (display);
4482
4483 return make_number (dpyinfo->n_planes * dpyinfo->n_cbits);
4484 }
4485
4486 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
4487 0, 1, 0,
4488 doc: /* Return the number of color cells of DISPLAY.
4489 The optional argument DISPLAY specifies which display to ask about.
4490 DISPLAY should be either a frame or a display name (a string).
4491 If omitted or nil, that stands for the selected frame's display. */)
4492 (Lisp_Object display)
4493 {
4494 struct w32_display_info *dpyinfo = check_x_display_info (display);
4495 HDC hdc;
4496 int cap;
4497
4498 hdc = GetDC (dpyinfo->root_window);
4499 if (dpyinfo->has_palette)
4500 cap = GetDeviceCaps (hdc, SIZEPALETTE);
4501 else
4502 cap = GetDeviceCaps (hdc, NUMCOLORS);
4503
4504 /* We force 24+ bit depths to 24-bit, both to prevent an overflow
4505 and because probably is more meaningful on Windows anyway */
4506 if (cap < 0)
4507 cap = 1 << min (dpyinfo->n_planes * dpyinfo->n_cbits, 24);
4508
4509 ReleaseDC (dpyinfo->root_window, hdc);
4510
4511 return make_number (cap);
4512 }
4513
4514 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
4515 Sx_server_max_request_size,
4516 0, 1, 0,
4517 doc: /* Return the maximum request size of the server of DISPLAY.
4518 The optional argument DISPLAY specifies which display to ask about.
4519 DISPLAY should be either a frame or a display name (a string).
4520 If omitted or nil, that stands for the selected frame's display. */)
4521 (Lisp_Object display)
4522 {
4523 return make_number (1);
4524 }
4525
4526 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
4527 doc: /* Return the "vendor ID" string of the W32 system (Microsoft).
4528 The optional argument DISPLAY specifies which display to ask about.
4529 DISPLAY should be either a frame or a display name (a string).
4530 If omitted or nil, that stands for the selected frame's display. */)
4531 (Lisp_Object display)
4532 {
4533 return build_string ("Microsoft Corp.");
4534 }
4535
4536 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
4537 doc: /* Return the version numbers of the server of DISPLAY.
4538 The value is a list of three integers: the major and minor
4539 version numbers of the X Protocol in use, and the distributor-specific
4540 release number. See also the function `x-server-vendor'.
4541
4542 The optional argument DISPLAY specifies which display to ask about.
4543 DISPLAY should be either a frame or a display name (a string).
4544 If omitted or nil, that stands for the selected frame's display. */)
4545 (Lisp_Object display)
4546 {
4547 return Fcons (make_number (w32_major_version),
4548 Fcons (make_number (w32_minor_version),
4549 Fcons (make_number (w32_build_number), Qnil)));
4550 }
4551
4552 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
4553 doc: /* Return the number of screens on the server of DISPLAY.
4554 The optional argument DISPLAY specifies which display to ask about.
4555 DISPLAY should be either a frame or a display name (a string).
4556 If omitted or nil, that stands for the selected frame's display. */)
4557 (Lisp_Object display)
4558 {
4559 return make_number (1);
4560 }
4561
4562 DEFUN ("x-display-mm-height", Fx_display_mm_height,
4563 Sx_display_mm_height, 0, 1, 0,
4564 doc: /* Return the height in millimeters of DISPLAY.
4565 The optional argument DISPLAY specifies which display to ask about.
4566 DISPLAY should be either a frame or a display name (a string).
4567 If omitted or nil, that stands for the selected frame's display. */)
4568 (Lisp_Object display)
4569 {
4570 struct w32_display_info *dpyinfo = check_x_display_info (display);
4571 HDC hdc;
4572 int cap;
4573
4574 hdc = GetDC (dpyinfo->root_window);
4575
4576 cap = GetDeviceCaps (hdc, VERTSIZE);
4577
4578 ReleaseDC (dpyinfo->root_window, hdc);
4579
4580 return make_number (cap);
4581 }
4582
4583 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
4584 doc: /* Return the width in millimeters of DISPLAY.
4585 The optional argument DISPLAY specifies which display to ask about.
4586 DISPLAY should be either a frame or a display name (a string).
4587 If omitted or nil, that stands for the selected frame's display. */)
4588 (Lisp_Object display)
4589 {
4590 struct w32_display_info *dpyinfo = check_x_display_info (display);
4591
4592 HDC hdc;
4593 int cap;
4594
4595 hdc = GetDC (dpyinfo->root_window);
4596
4597 cap = GetDeviceCaps (hdc, HORZSIZE);
4598
4599 ReleaseDC (dpyinfo->root_window, hdc);
4600
4601 return make_number (cap);
4602 }
4603
4604 DEFUN ("x-display-backing-store", Fx_display_backing_store,
4605 Sx_display_backing_store, 0, 1, 0,
4606 doc: /* Return an indication of whether DISPLAY does backing store.
4607 The value may be `always', `when-mapped', or `not-useful'.
4608 The optional argument DISPLAY specifies which display to ask about.
4609 DISPLAY should be either a frame or a display name (a string).
4610 If omitted or nil, that stands for the selected frame's display. */)
4611 (Lisp_Object display)
4612 {
4613 return intern ("not-useful");
4614 }
4615
4616 DEFUN ("x-display-visual-class", Fx_display_visual_class,
4617 Sx_display_visual_class, 0, 1, 0,
4618 doc: /* Return the visual class of DISPLAY.
4619 The value is one of the symbols `static-gray', `gray-scale',
4620 `static-color', `pseudo-color', `true-color', or `direct-color'.
4621
4622 The optional argument DISPLAY specifies which display to ask about.
4623 DISPLAY should be either a frame or a display name (a string).
4624 If omitted or nil, that stands for the selected frame's display. */)
4625 (Lisp_Object display)
4626 {
4627 struct w32_display_info *dpyinfo = check_x_display_info (display);
4628 Lisp_Object result = Qnil;
4629
4630 if (dpyinfo->has_palette)
4631 result = intern ("pseudo-color");
4632 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 1)
4633 result = intern ("static-grey");
4634 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 4)
4635 result = intern ("static-color");
4636 else if (dpyinfo->n_planes * dpyinfo->n_cbits > 8)
4637 result = intern ("true-color");
4638
4639 return result;
4640 }
4641
4642 DEFUN ("x-display-save-under", Fx_display_save_under,
4643 Sx_display_save_under, 0, 1, 0,
4644 doc: /* Return t if DISPLAY supports the save-under feature.
4645 The optional argument DISPLAY specifies which display to ask about.
4646 DISPLAY should be either a frame or a display name (a string).
4647 If omitted or nil, that stands for the selected frame's display. */)
4648 (Lisp_Object display)
4649 {
4650 return Qnil;
4651 }
4652 \f
4653 int
4654 x_pixel_width (register struct frame *f)
4655 {
4656 return FRAME_PIXEL_WIDTH (f);
4657 }
4658
4659 int
4660 x_pixel_height (register struct frame *f)
4661 {
4662 return FRAME_PIXEL_HEIGHT (f);
4663 }
4664
4665 int
4666 x_char_width (register struct frame *f)
4667 {
4668 return FRAME_COLUMN_WIDTH (f);
4669 }
4670
4671 int
4672 x_char_height (register struct frame *f)
4673 {
4674 return FRAME_LINE_HEIGHT (f);
4675 }
4676
4677 int
4678 x_screen_planes (register struct frame *f)
4679 {
4680 return FRAME_W32_DISPLAY_INFO (f)->n_planes;
4681 }
4682 \f
4683 /* Return the display structure for the display named NAME.
4684 Open a new connection if necessary. */
4685
4686 struct w32_display_info *
4687 x_display_info_for_name (Lisp_Object name)
4688 {
4689 Lisp_Object names;
4690 struct w32_display_info *dpyinfo;
4691
4692 CHECK_STRING (name);
4693
4694 for (dpyinfo = &one_w32_display_info, names = w32_display_name_list;
4695 dpyinfo;
4696 dpyinfo = dpyinfo->next, names = XCDR (names))
4697 {
4698 Lisp_Object tem;
4699 tem = Fstring_equal (XCAR (XCAR (names)), name);
4700 if (!NILP (tem))
4701 return dpyinfo;
4702 }
4703
4704 /* Use this general default value to start with. */
4705 Vx_resource_name = Vinvocation_name;
4706
4707 validate_x_resource_name ();
4708
4709 dpyinfo = w32_term_init (name, (unsigned char *)0,
4710 SSDATA (Vx_resource_name));
4711
4712 if (dpyinfo == 0)
4713 error ("Cannot connect to server %s", SDATA (name));
4714
4715 w32_in_use = 1;
4716 XSETFASTINT (Vwindow_system_version, w32_major_version);
4717
4718 return dpyinfo;
4719 }
4720
4721 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
4722 1, 3, 0, doc: /* Open a connection to a display server.
4723 DISPLAY is the name of the display to connect to.
4724 Optional second arg XRM-STRING is a string of resources in xrdb format.
4725 If the optional third arg MUST-SUCCEED is non-nil,
4726 terminate Emacs if we can't open the connection.
4727 \(In the Nextstep version, the last two arguments are currently ignored.) */)
4728 (Lisp_Object display, Lisp_Object xrm_string, Lisp_Object must_succeed)
4729 {
4730 unsigned char *xrm_option;
4731 struct w32_display_info *dpyinfo;
4732
4733 /* If initialization has already been done, return now to avoid
4734 overwriting critical parts of one_w32_display_info. */
4735 if (w32_in_use)
4736 return Qnil;
4737
4738 CHECK_STRING (display);
4739 if (! NILP (xrm_string))
4740 CHECK_STRING (xrm_string);
4741
4742 #if 0
4743 if (! EQ (Vwindow_system, intern ("w32")))
4744 error ("Not using Microsoft Windows");
4745 #endif
4746
4747 /* Allow color mapping to be defined externally; first look in user's
4748 HOME directory, then in Emacs etc dir for a file called rgb.txt. */
4749 {
4750 Lisp_Object color_file;
4751 struct gcpro gcpro1;
4752
4753 color_file = build_string ("~/rgb.txt");
4754
4755 GCPRO1 (color_file);
4756
4757 if (NILP (Ffile_readable_p (color_file)))
4758 color_file =
4759 Fexpand_file_name (build_string ("rgb.txt"),
4760 Fsymbol_value (intern ("data-directory")));
4761
4762 Vw32_color_map = Fx_load_color_file (color_file);
4763
4764 UNGCPRO;
4765 }
4766 if (NILP (Vw32_color_map))
4767 Vw32_color_map = Fw32_default_color_map ();
4768
4769 /* Merge in system logical colors. */
4770 add_system_logical_colors_to_map (&Vw32_color_map);
4771
4772 if (! NILP (xrm_string))
4773 xrm_option = SDATA (xrm_string);
4774 else
4775 xrm_option = (unsigned char *) 0;
4776
4777 /* Use this general default value to start with. */
4778 /* First remove .exe suffix from invocation-name - it looks ugly. */
4779 {
4780 char basename[ MAX_PATH ], *str;
4781
4782 strcpy (basename, SDATA (Vinvocation_name));
4783 str = strrchr (basename, '.');
4784 if (str) *str = 0;
4785 Vinvocation_name = build_string (basename);
4786 }
4787 Vx_resource_name = Vinvocation_name;
4788
4789 validate_x_resource_name ();
4790
4791 /* This is what opens the connection and sets x_current_display.
4792 This also initializes many symbols, such as those used for input. */
4793 dpyinfo = w32_term_init (display, xrm_option,
4794 SSDATA (Vx_resource_name));
4795
4796 if (dpyinfo == 0)
4797 {
4798 if (!NILP (must_succeed))
4799 fatal ("Cannot connect to server %s.\n",
4800 SDATA (display));
4801 else
4802 error ("Cannot connect to server %s", SDATA (display));
4803 }
4804
4805 w32_in_use = 1;
4806
4807 XSETFASTINT (Vwindow_system_version, w32_major_version);
4808 return Qnil;
4809 }
4810
4811 DEFUN ("x-close-connection", Fx_close_connection,
4812 Sx_close_connection, 1, 1, 0,
4813 doc: /* Close the connection to DISPLAY's server.
4814 For DISPLAY, specify either a frame or a display name (a string).
4815 If DISPLAY is nil, that stands for the selected frame's display. */)
4816 (Lisp_Object display)
4817 {
4818 struct w32_display_info *dpyinfo = check_x_display_info (display);
4819
4820 if (dpyinfo->reference_count > 0)
4821 error ("Display still has frames on it");
4822
4823 BLOCK_INPUT;
4824 x_destroy_all_bitmaps (dpyinfo);
4825
4826 x_delete_display (dpyinfo);
4827 UNBLOCK_INPUT;
4828
4829 return Qnil;
4830 }
4831
4832 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
4833 doc: /* Return the list of display names that Emacs has connections to. */)
4834 (void)
4835 {
4836 Lisp_Object tail, result;
4837
4838 result = Qnil;
4839 for (tail = w32_display_name_list; CONSP (tail); tail = XCDR (tail))
4840 result = Fcons (XCAR (XCAR (tail)), result);
4841
4842 return result;
4843 }
4844
4845 DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
4846 doc: /* If ON is non-nil, report X errors as soon as the erring request is made.
4847 This function only has an effect on X Windows. With MS Windows, it is
4848 defined but does nothing.
4849
4850 If ON is nil, allow buffering of requests.
4851 Turning on synchronization prohibits the Xlib routines from buffering
4852 requests and seriously degrades performance, but makes debugging much
4853 easier.
4854 The optional second argument TERMINAL specifies which display to act on.
4855 TERMINAL should be a terminal object, a frame or a display name (a string).
4856 If TERMINAL is omitted or nil, that stands for the selected frame's display. */)
4857 (Lisp_Object on, Lisp_Object display)
4858 {
4859 return Qnil;
4860 }
4861
4862
4863 \f
4864 /***********************************************************************
4865 Window properties
4866 ***********************************************************************/
4867
4868 DEFUN ("x-change-window-property", Fx_change_window_property,
4869 Sx_change_window_property, 2, 6, 0,
4870 doc: /* Change window property PROP to VALUE on the X window of FRAME.
4871 PROP must be a string. VALUE may be a string or a list of conses,
4872 numbers and/or strings. If an element in the list is a string, it is
4873 converted to an atom and the value of the Atom is used. If an element
4874 is a cons, it is converted to a 32 bit number where the car is the 16
4875 top bits and the cdr is the lower 16 bits.
4876
4877 FRAME nil or omitted means use the selected frame.
4878 If TYPE is given and non-nil, it is the name of the type of VALUE.
4879 If TYPE is not given or nil, the type is STRING.
4880 FORMAT gives the size in bits of each element if VALUE is a list.
4881 It must be one of 8, 16 or 32.
4882 If VALUE is a string or FORMAT is nil or not given, FORMAT defaults to 8.
4883 If OUTER_P is non-nil, the property is changed for the outer X window of
4884 FRAME. Default is to change on the edit X window. */)
4885 (Lisp_Object prop, Lisp_Object value, Lisp_Object frame, Lisp_Object type, Lisp_Object format, Lisp_Object outer_p)
4886 {
4887 #if 0 /* TODO : port window properties to W32 */
4888 struct frame *f = check_x_frame (frame);
4889 Atom prop_atom;
4890
4891 CHECK_STRING (prop);
4892 CHECK_STRING (value);
4893
4894 BLOCK_INPUT;
4895 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
4896 XChangeProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
4897 prop_atom, XA_STRING, 8, PropModeReplace,
4898 SDATA (value), SCHARS (value));
4899
4900 /* Make sure the property is set when we return. */
4901 XFlush (FRAME_W32_DISPLAY (f));
4902 UNBLOCK_INPUT;
4903
4904 #endif /* TODO */
4905
4906 return value;
4907 }
4908
4909
4910 DEFUN ("x-delete-window-property", Fx_delete_window_property,
4911 Sx_delete_window_property, 1, 2, 0,
4912 doc: /* Remove window property PROP from X window of FRAME.
4913 FRAME nil or omitted means use the selected frame. Value is PROP. */)
4914 (Lisp_Object prop, Lisp_Object frame)
4915 {
4916 #if 0 /* TODO : port window properties to W32 */
4917
4918 struct frame *f = check_x_frame (frame);
4919 Atom prop_atom;
4920
4921 CHECK_STRING (prop);
4922 BLOCK_INPUT;
4923 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
4924 XDeleteProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), prop_atom);
4925
4926 /* Make sure the property is removed when we return. */
4927 XFlush (FRAME_W32_DISPLAY (f));
4928 UNBLOCK_INPUT;
4929 #endif /* TODO */
4930
4931 return prop;
4932 }
4933
4934
4935 DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
4936 1, 2, 0,
4937 doc: /* Value is the value of window property PROP on FRAME.
4938 If FRAME is nil or omitted, use the selected frame.
4939
4940 On MS Windows, this function only accepts the PROP and FRAME arguments.
4941
4942 On X Windows, the following optional arguments are also accepted:
4943 If TYPE is nil or omitted, get the property as a string.
4944 Otherwise TYPE is the name of the atom that denotes the type expected.
4945 If SOURCE is non-nil, get the property on that window instead of from
4946 FRAME. The number 0 denotes the root window.
4947 If DELETE_P is non-nil, delete the property after retreiving it.
4948 If VECTOR_RET_P is non-nil, don't return a string but a vector of values.
4949
4950 Value is nil if FRAME hasn't a property with name PROP or if PROP has
4951 no value of TYPE (always string in the MS Windows case). */)
4952 (Lisp_Object prop, Lisp_Object frame)
4953 {
4954 #if 0 /* TODO : port window properties to W32 */
4955
4956 struct frame *f = check_x_frame (frame);
4957 Atom prop_atom;
4958 int rc;
4959 Lisp_Object prop_value = Qnil;
4960 char *tmp_data = NULL;
4961 Atom actual_type;
4962 int actual_format;
4963 unsigned long actual_size, bytes_remaining;
4964
4965 CHECK_STRING (prop);
4966 BLOCK_INPUT;
4967 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
4968 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
4969 prop_atom, 0, 0, False, XA_STRING,
4970 &actual_type, &actual_format, &actual_size,
4971 &bytes_remaining, (unsigned char **) &tmp_data);
4972 if (rc == Success)
4973 {
4974 int size = bytes_remaining;
4975
4976 XFree (tmp_data);
4977 tmp_data = NULL;
4978
4979 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
4980 prop_atom, 0, bytes_remaining,
4981 False, XA_STRING,
4982 &actual_type, &actual_format,
4983 &actual_size, &bytes_remaining,
4984 (unsigned char **) &tmp_data);
4985 if (rc == Success)
4986 prop_value = make_string (tmp_data, size);
4987
4988 XFree (tmp_data);
4989 }
4990
4991 UNBLOCK_INPUT;
4992
4993 return prop_value;
4994
4995 #endif /* TODO */
4996 return Qnil;
4997 }
4998
4999
5000 \f
5001 /***********************************************************************
5002 Busy cursor
5003 ***********************************************************************/
5004
5005 /* Default number of seconds to wait before displaying an hourglass
5006 cursor. Duplicated from xdisp.c, but cannot use the version there
5007 due to lack of atimers on w32. */
5008 #define DEFAULT_HOURGLASS_DELAY 1
5009 /* Return non-zero if houglass timer has been started or hourglass is shown. */
5010 /* PENDING: if W32 can use atimers (atimer.[hc]) then the common impl in
5011 xdisp.c could be used. */
5012
5013 int
5014 hourglass_started (void)
5015 {
5016 return hourglass_shown_p || hourglass_timer;
5017 }
5018
5019 /* Cancel a currently active hourglass timer, and start a new one. */
5020
5021 void
5022 start_hourglass (void)
5023 {
5024 DWORD delay;
5025 int secs, msecs = 0;
5026 struct frame * f = SELECTED_FRAME ();
5027
5028 /* No cursors on non GUI frames. */
5029 if (!FRAME_W32_P (f))
5030 return;
5031
5032 cancel_hourglass ();
5033
5034 if (INTEGERP (Vhourglass_delay)
5035 && XINT (Vhourglass_delay) > 0)
5036 secs = XFASTINT (Vhourglass_delay);
5037 else if (FLOATP (Vhourglass_delay)
5038 && XFLOAT_DATA (Vhourglass_delay) > 0)
5039 {
5040 Lisp_Object tem;
5041 tem = Ftruncate (Vhourglass_delay, Qnil);
5042 secs = XFASTINT (tem);
5043 msecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000;
5044 }
5045 else
5046 secs = DEFAULT_HOURGLASS_DELAY;
5047
5048 delay = secs * 1000 + msecs;
5049 hourglass_hwnd = FRAME_W32_WINDOW (f);
5050 hourglass_timer = SetTimer (hourglass_hwnd, HOURGLASS_ID, delay, NULL);
5051 }
5052
5053
5054 /* Cancel the hourglass cursor timer if active, hide an hourglass
5055 cursor if shown. */
5056
5057 void
5058 cancel_hourglass (void)
5059 {
5060 if (hourglass_timer)
5061 {
5062 KillTimer (hourglass_hwnd, hourglass_timer);
5063 hourglass_timer = 0;
5064 }
5065
5066 if (hourglass_shown_p)
5067 w32_hide_hourglass ();
5068 }
5069
5070
5071 /* Timer function of hourglass_timer.
5072
5073 Display an hourglass cursor. Set the hourglass_p flag in display info
5074 to indicate that an hourglass cursor is shown. */
5075
5076 static void
5077 w32_show_hourglass (struct frame *f)
5078 {
5079 if (!hourglass_shown_p)
5080 {
5081 f->output_data.w32->hourglass_p = 1;
5082 if (!menubar_in_use && !current_popup_menu)
5083 SetCursor (f->output_data.w32->hourglass_cursor);
5084 hourglass_shown_p = 1;
5085 }
5086 }
5087
5088
5089 /* Hide the hourglass cursor on all frames, if it is currently shown. */
5090
5091 static void
5092 w32_hide_hourglass (void)
5093 {
5094 if (hourglass_shown_p)
5095 {
5096 struct frame *f = x_window_to_frame (&one_w32_display_info,
5097 hourglass_hwnd);
5098 if (f)
5099 f->output_data.w32->hourglass_p = 0;
5100 else
5101 /* If frame was deleted, restore to selected frame's cursor. */
5102 f = SELECTED_FRAME ();
5103
5104 if (FRAME_W32_P (f))
5105 SetCursor (f->output_data.w32->current_cursor);
5106 else
5107 /* No cursors on non GUI frames - restore to stock arrow cursor. */
5108 SetCursor (w32_load_cursor (IDC_ARROW));
5109
5110 hourglass_shown_p = 0;
5111 }
5112 }
5113
5114
5115 \f
5116 /***********************************************************************
5117 Tool tips
5118 ***********************************************************************/
5119
5120 static Lisp_Object x_create_tip_frame (struct w32_display_info *,
5121 Lisp_Object, Lisp_Object);
5122 static void compute_tip_xy (struct frame *, Lisp_Object, Lisp_Object,
5123 Lisp_Object, int, int, int *, int *);
5124
5125 /* The frame of a currently visible tooltip. */
5126
5127 Lisp_Object tip_frame;
5128
5129 /* If non-nil, a timer started that hides the last tooltip when it
5130 fires. */
5131
5132 Lisp_Object tip_timer;
5133 Window tip_window;
5134
5135 /* If non-nil, a vector of 3 elements containing the last args
5136 with which x-show-tip was called. See there. */
5137
5138 Lisp_Object last_show_tip_args;
5139
5140
5141 static Lisp_Object
5142 unwind_create_tip_frame (Lisp_Object frame)
5143 {
5144 Lisp_Object deleted;
5145
5146 deleted = unwind_create_frame (frame);
5147 if (EQ (deleted, Qt))
5148 {
5149 tip_window = NULL;
5150 tip_frame = Qnil;
5151 }
5152
5153 return deleted;
5154 }
5155
5156
5157 /* Create a frame for a tooltip on the display described by DPYINFO.
5158 PARMS is a list of frame parameters. TEXT is the string to
5159 display in the tip frame. Value is the frame.
5160
5161 Note that functions called here, esp. x_default_parameter can
5162 signal errors, for instance when a specified color name is
5163 undefined. We have to make sure that we're in a consistent state
5164 when this happens. */
5165
5166 static Lisp_Object
5167 x_create_tip_frame (struct w32_display_info *dpyinfo,
5168 Lisp_Object parms, Lisp_Object text)
5169 {
5170 struct frame *f;
5171 Lisp_Object frame;
5172 Lisp_Object name;
5173 long window_prompting = 0;
5174 int width, height;
5175 int count = SPECPDL_INDEX ();
5176 struct gcpro gcpro1, gcpro2, gcpro3;
5177 struct kboard *kb;
5178 int face_change_count_before = face_change_count;
5179 Lisp_Object buffer;
5180 struct buffer *old_buffer;
5181
5182 check_w32 ();
5183
5184 /* Use this general default value to start with until we know if
5185 this frame has a specified name. */
5186 Vx_resource_name = Vinvocation_name;
5187
5188 kb = dpyinfo->terminal->kboard;
5189
5190 /* The calls to x_get_arg remove elements from PARMS, so copy it to
5191 avoid destructive changes behind our caller's back. */
5192 parms = Fcopy_alist (parms);
5193
5194 /* Get the name of the frame to use for resource lookup. */
5195 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
5196 if (!STRINGP (name)
5197 && !EQ (name, Qunbound)
5198 && !NILP (name))
5199 error ("Invalid frame name--not a string or nil");
5200 Vx_resource_name = name;
5201
5202 frame = Qnil;
5203 GCPRO3 (parms, name, frame);
5204 /* Make a frame without minibuffer nor mode-line. */
5205 f = make_frame (0);
5206 f->wants_modeline = 0;
5207 XSETFRAME (frame, f);
5208
5209 buffer = Fget_buffer_create (build_string (" *tip*"));
5210 Fset_window_buffer (FRAME_ROOT_WINDOW (f), buffer, Qnil);
5211 old_buffer = current_buffer;
5212 set_buffer_internal_1 (XBUFFER (buffer));
5213 BVAR (current_buffer, truncate_lines) = Qnil;
5214 specbind (Qinhibit_read_only, Qt);
5215 specbind (Qinhibit_modification_hooks, Qt);
5216 Ferase_buffer ();
5217 Finsert (1, &text);
5218 set_buffer_internal_1 (old_buffer);
5219
5220 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
5221 record_unwind_protect (unwind_create_tip_frame, frame);
5222
5223 /* By setting the output method, we're essentially saying that
5224 the frame is live, as per FRAME_LIVE_P. If we get a signal
5225 from this point on, x_destroy_window might screw up reference
5226 counts etc. */
5227 f->terminal = dpyinfo->terminal;
5228 f->terminal->reference_count++;
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 = FRAME_IMAGE_CACHE (f)->refcount;
5239 dpyinfo_refcount = dpyinfo->reference_count;
5240 #endif /* GLYPH_DEBUG */
5241 FRAME_KBOARD (f) = kb;
5242 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
5243 f->output_data.w32->explicit_parent = 0;
5244
5245 /* Set the name; the functions to which we pass f expect the name to
5246 be set. */
5247 if (EQ (name, Qunbound) || NILP (name))
5248 {
5249 f->name = build_string (dpyinfo->w32_id_name);
5250 f->explicit_name = 0;
5251 }
5252 else
5253 {
5254 f->name = name;
5255 f->explicit_name = 1;
5256 /* use the frame's title when getting resources for this frame. */
5257 specbind (Qx_resource_name, name);
5258 }
5259
5260 f->resx = dpyinfo->resx;
5261 f->resy = dpyinfo->resy;
5262
5263 if (uniscribe_available)
5264 register_font_driver (&uniscribe_font_driver, f);
5265 register_font_driver (&w32font_driver, f);
5266
5267 x_default_parameter (f, parms, Qfont_backend, Qnil,
5268 "fontBackend", "FontBackend", RES_TYPE_STRING);
5269
5270 /* Extract the window parameters from the supplied values
5271 that are needed to determine window geometry. */
5272 x_default_font_parameter (f, parms);
5273
5274 x_default_parameter (f, parms, Qborder_width, make_number (2),
5275 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
5276 /* This defaults to 2 in order to match xterm. We recognize either
5277 internalBorderWidth or internalBorder (which is what xterm calls
5278 it). */
5279 if (NILP (Fassq (Qinternal_border_width, parms)))
5280 {
5281 Lisp_Object value;
5282
5283 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
5284 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
5285 if (! EQ (value, Qunbound))
5286 parms = Fcons (Fcons (Qinternal_border_width, value),
5287 parms);
5288 }
5289 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
5290 "internalBorderWidth", "internalBorderWidth",
5291 RES_TYPE_NUMBER);
5292
5293 /* Also do the stuff which must be set before the window exists. */
5294 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
5295 "foreground", "Foreground", RES_TYPE_STRING);
5296 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
5297 "background", "Background", RES_TYPE_STRING);
5298 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
5299 "pointerColor", "Foreground", RES_TYPE_STRING);
5300 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
5301 "cursorColor", "Foreground", RES_TYPE_STRING);
5302 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
5303 "borderColor", "BorderColor", RES_TYPE_STRING);
5304
5305 /* Init faces before x_default_parameter is called for scroll-bar
5306 parameters because that function calls x_set_scroll_bar_width,
5307 which calls change_frame_size, which calls Fset_window_buffer,
5308 which runs hooks, which call Fvertical_motion. At the end, we
5309 end up in init_iterator with a null face cache, which should not
5310 happen. */
5311 init_frame_faces (f);
5312
5313 f->output_data.w32->dwStyle = WS_BORDER | WS_POPUP | WS_DISABLED;
5314 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
5315
5316 window_prompting = x_figure_window_size (f, parms, 0);
5317
5318 /* No fringes on tip frame. */
5319 f->fringe_cols = 0;
5320 f->left_fringe_width = 0;
5321 f->right_fringe_width = 0;
5322
5323 BLOCK_INPUT;
5324 my_create_tip_window (f);
5325 UNBLOCK_INPUT;
5326
5327 x_make_gc (f);
5328
5329 x_default_parameter (f, parms, Qauto_raise, Qnil,
5330 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5331 x_default_parameter (f, parms, Qauto_lower, Qnil,
5332 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5333 x_default_parameter (f, parms, Qcursor_type, Qbox,
5334 "cursorType", "CursorType", RES_TYPE_SYMBOL);
5335
5336 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
5337 Change will not be effected unless different from the current
5338 FRAME_LINES (f). */
5339 width = FRAME_COLS (f);
5340 height = FRAME_LINES (f);
5341 FRAME_LINES (f) = 0;
5342 SET_FRAME_COLS (f, 0);
5343 change_frame_size (f, height, width, 1, 0, 0);
5344
5345 /* Add `tooltip' frame parameter's default value. */
5346 if (NILP (Fframe_parameter (frame, Qtooltip)))
5347 Fmodify_frame_parameters (frame, Fcons (Fcons (Qtooltip, Qt), Qnil));
5348
5349 /* Set up faces after all frame parameters are known. This call
5350 also merges in face attributes specified for new frames.
5351
5352 Frame parameters may be changed if .Xdefaults contains
5353 specifications for the default font. For example, if there is an
5354 `Emacs.default.attributeBackground: pink', the `background-color'
5355 attribute of the frame get's set, which let's the internal border
5356 of the tooltip frame appear in pink. Prevent this. */
5357 {
5358 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
5359 Lisp_Object fg = Fframe_parameter (frame, Qforeground_color);
5360 Lisp_Object colors = Qnil;
5361
5362 /* Set tip_frame here, so that */
5363 tip_frame = frame;
5364 call2 (Qface_set_after_frame_default, frame, Qnil);
5365
5366 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
5367 colors = Fcons (Fcons (Qbackground_color, bg), colors);
5368 if (!EQ (fg, Fframe_parameter (frame, Qforeground_color)))
5369 colors = Fcons (Fcons (Qforeground_color, fg), colors);
5370
5371 if (!NILP (colors))
5372 Fmodify_frame_parameters (frame, colors);
5373 }
5374
5375 f->no_split = 1;
5376
5377 UNGCPRO;
5378
5379 /* It is now ok to make the frame official even if we get an error
5380 below. And the frame needs to be on Vframe_list or making it
5381 visible won't work. */
5382 Vframe_list = Fcons (frame, Vframe_list);
5383
5384 /* Now that the frame is official, it counts as a reference to
5385 its display. */
5386 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
5387
5388 /* Setting attributes of faces of the tooltip frame from resources
5389 and similar will increment face_change_count, which leads to the
5390 clearing of all current matrices. Since this isn't necessary
5391 here, avoid it by resetting face_change_count to the value it
5392 had before we created the tip frame. */
5393 face_change_count = face_change_count_before;
5394
5395 /* Discard the unwind_protect. */
5396 return unbind_to (count, frame);
5397 }
5398
5399
5400 /* Compute where to display tip frame F. PARMS is the list of frame
5401 parameters for F. DX and DY are specified offsets from the current
5402 location of the mouse. WIDTH and HEIGHT are the width and height
5403 of the tooltip. Return coordinates relative to the root window of
5404 the display in *ROOT_X, and *ROOT_Y. */
5405
5406 static void
5407 compute_tip_xy (struct frame *f,
5408 Lisp_Object parms, Lisp_Object dx, Lisp_Object dy,
5409 int width, int height, int *root_x, int *root_y)
5410 {
5411 Lisp_Object left, top;
5412 int min_x, min_y, max_x, max_y;
5413
5414 /* User-specified position? */
5415 left = Fcdr (Fassq (Qleft, parms));
5416 top = Fcdr (Fassq (Qtop, parms));
5417
5418 /* Move the tooltip window where the mouse pointer is. Resize and
5419 show it. */
5420 if (!INTEGERP (left) || !INTEGERP (top))
5421 {
5422 POINT pt;
5423
5424 /* Default min and max values. */
5425 min_x = 0;
5426 min_y = 0;
5427 max_x = x_display_pixel_width (FRAME_W32_DISPLAY_INFO (f));
5428 max_y = x_display_pixel_height (FRAME_W32_DISPLAY_INFO (f));
5429
5430 BLOCK_INPUT;
5431 GetCursorPos (&pt);
5432 *root_x = pt.x;
5433 *root_y = pt.y;
5434 UNBLOCK_INPUT;
5435
5436 /* If multiple monitor support is available, constrain the tip onto
5437 the current monitor. This improves the above by allowing negative
5438 co-ordinates if monitor positions are such that they are valid, and
5439 snaps a tooltip onto a single monitor if we are close to the edge
5440 where it would otherwise flow onto the other monitor (or into
5441 nothingness if there is a gap in the overlap). */
5442 if (monitor_from_point_fn && get_monitor_info_fn)
5443 {
5444 struct MONITOR_INFO info;
5445 HMONITOR monitor
5446 = monitor_from_point_fn (pt, MONITOR_DEFAULT_TO_NEAREST);
5447 info.cbSize = sizeof (info);
5448
5449 if (get_monitor_info_fn (monitor, &info))
5450 {
5451 min_x = info.rcWork.left;
5452 min_y = info.rcWork.top;
5453 max_x = info.rcWork.right;
5454 max_y = info.rcWork.bottom;
5455 }
5456 }
5457 }
5458
5459 if (INTEGERP (top))
5460 *root_y = XINT (top);
5461 else if (*root_y + XINT (dy) <= min_y)
5462 *root_y = min_y; /* Can happen for negative dy */
5463 else if (*root_y + XINT (dy) + height <= max_y)
5464 /* It fits below the pointer */
5465 *root_y += XINT (dy);
5466 else if (height + XINT (dy) + min_y <= *root_y)
5467 /* It fits above the pointer. */
5468 *root_y -= height + XINT (dy);
5469 else
5470 /* Put it on the top. */
5471 *root_y = min_y;
5472
5473 if (INTEGERP (left))
5474 *root_x = XINT (left);
5475 else if (*root_x + XINT (dx) <= min_x)
5476 *root_x = 0; /* Can happen for negative dx */
5477 else if (*root_x + XINT (dx) + width <= max_x)
5478 /* It fits to the right of the pointer. */
5479 *root_x += XINT (dx);
5480 else if (width + XINT (dx) + min_x <= *root_x)
5481 /* It fits to the left of the pointer. */
5482 *root_x -= width + XINT (dx);
5483 else
5484 /* Put it left justified on the screen -- it ought to fit that way. */
5485 *root_x = min_x;
5486 }
5487
5488
5489 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
5490 doc: /* Show STRING in a \"tooltip\" window on frame FRAME.
5491 A tooltip window is a small window displaying a string.
5492
5493 This is an internal function; Lisp code should call `tooltip-show'.
5494
5495 FRAME nil or omitted means use the selected frame.
5496
5497 PARMS is an optional list of frame parameters which can be
5498 used to change the tooltip's appearance.
5499
5500 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
5501 means use the default timeout of 5 seconds.
5502
5503 If the list of frame parameters PARMS contains a `left' parameter,
5504 the tooltip is displayed at that x-position. Otherwise it is
5505 displayed at the mouse position, with offset DX added (default is 5 if
5506 DX isn't specified). Likewise for the y-position; if a `top' frame
5507 parameter is specified, it determines the y-position of the tooltip
5508 window, otherwise it is displayed at the mouse position, with offset
5509 DY added (default is -10).
5510
5511 A tooltip's maximum size is specified by `x-max-tooltip-size'.
5512 Text larger than the specified size is clipped. */)
5513 (Lisp_Object string, Lisp_Object frame, Lisp_Object parms, Lisp_Object timeout, Lisp_Object dx, Lisp_Object dy)
5514 {
5515 struct frame *f;
5516 struct window *w;
5517 int root_x, root_y;
5518 struct buffer *old_buffer;
5519 struct text_pos pos;
5520 int i, width, height, seen_reversed_p;
5521 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5522 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5523 int count = SPECPDL_INDEX ();
5524
5525 specbind (Qinhibit_redisplay, Qt);
5526
5527 GCPRO4 (string, parms, frame, timeout);
5528
5529 CHECK_STRING (string);
5530 f = check_x_frame (frame);
5531 if (NILP (timeout))
5532 timeout = make_number (5);
5533 else
5534 CHECK_NATNUM (timeout);
5535
5536 if (NILP (dx))
5537 dx = make_number (5);
5538 else
5539 CHECK_NUMBER (dx);
5540
5541 if (NILP (dy))
5542 dy = make_number (-10);
5543 else
5544 CHECK_NUMBER (dy);
5545
5546 if (NILP (last_show_tip_args))
5547 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
5548
5549 if (!NILP (tip_frame))
5550 {
5551 Lisp_Object last_string = AREF (last_show_tip_args, 0);
5552 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
5553 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
5554
5555 if (EQ (frame, last_frame)
5556 && !NILP (Fequal (last_string, string))
5557 && !NILP (Fequal (last_parms, parms)))
5558 {
5559 struct frame *f = XFRAME (tip_frame);
5560
5561 /* Only DX and DY have changed. */
5562 if (!NILP (tip_timer))
5563 {
5564 Lisp_Object timer = tip_timer;
5565 tip_timer = Qnil;
5566 call1 (Qcancel_timer, timer);
5567 }
5568
5569 BLOCK_INPUT;
5570 compute_tip_xy (f, parms, dx, dy, FRAME_PIXEL_WIDTH (f),
5571 FRAME_PIXEL_HEIGHT (f), &root_x, &root_y);
5572
5573 /* Put tooltip in topmost group and in position. */
5574 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
5575 root_x, root_y, 0, 0,
5576 SWP_NOSIZE | SWP_NOACTIVATE);
5577
5578 /* Ensure tooltip is on top of other topmost windows (eg menus). */
5579 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
5580 0, 0, 0, 0,
5581 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
5582
5583 UNBLOCK_INPUT;
5584 goto start_timer;
5585 }
5586 }
5587
5588 /* Hide a previous tip, if any. */
5589 Fx_hide_tip ();
5590
5591 ASET (last_show_tip_args, 0, string);
5592 ASET (last_show_tip_args, 1, frame);
5593 ASET (last_show_tip_args, 2, parms);
5594
5595 /* Add default values to frame parameters. */
5596 if (NILP (Fassq (Qname, parms)))
5597 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
5598 if (NILP (Fassq (Qinternal_border_width, parms)))
5599 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
5600 if (NILP (Fassq (Qborder_width, parms)))
5601 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
5602 if (NILP (Fassq (Qborder_color, parms)))
5603 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
5604 if (NILP (Fassq (Qbackground_color, parms)))
5605 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
5606 parms);
5607
5608 /* Block input until the tip has been fully drawn, to avoid crashes
5609 when drawing tips in menus. */
5610 BLOCK_INPUT;
5611
5612 /* Create a frame for the tooltip, and record it in the global
5613 variable tip_frame. */
5614 frame = x_create_tip_frame (FRAME_W32_DISPLAY_INFO (f), parms, string);
5615 f = XFRAME (frame);
5616
5617 /* Set up the frame's root window. */
5618 w = XWINDOW (FRAME_ROOT_WINDOW (f));
5619 w->left_col = w->top_line = make_number (0);
5620
5621 if (CONSP (Vx_max_tooltip_size)
5622 && INTEGERP (XCAR (Vx_max_tooltip_size))
5623 && XINT (XCAR (Vx_max_tooltip_size)) > 0
5624 && INTEGERP (XCDR (Vx_max_tooltip_size))
5625 && XINT (XCDR (Vx_max_tooltip_size)) > 0)
5626 {
5627 w->total_cols = XCAR (Vx_max_tooltip_size);
5628 w->total_lines = XCDR (Vx_max_tooltip_size);
5629 }
5630 else
5631 {
5632 w->total_cols = make_number (80);
5633 w->total_lines = make_number (40);
5634 }
5635
5636 FRAME_TOTAL_COLS (f) = XINT (w->total_cols);
5637 adjust_glyphs (f);
5638 w->pseudo_window_p = 1;
5639
5640 /* Display the tooltip text in a temporary buffer. */
5641 old_buffer = current_buffer;
5642 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->buffer));
5643 BVAR (current_buffer, truncate_lines) = Qnil;
5644 clear_glyph_matrix (w->desired_matrix);
5645 clear_glyph_matrix (w->current_matrix);
5646 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
5647 try_window (FRAME_ROOT_WINDOW (f), pos, TRY_WINDOW_IGNORE_FONTS_CHANGE);
5648
5649 /* Compute width and height of the tooltip. */
5650 width = height = seen_reversed_p = 0;
5651 for (i = 0; i < w->desired_matrix->nrows; ++i)
5652 {
5653 struct glyph_row *row = &w->desired_matrix->rows[i];
5654 struct glyph *last;
5655 int row_width;
5656
5657 /* Stop at the first empty row at the end. */
5658 if (!row->enabled_p || !row->displays_text_p)
5659 break;
5660
5661 /* Let the row go over the full width of the frame. */
5662 row->full_width_p = 1;
5663
5664 row_width = row->pixel_width;
5665 if (row->used[TEXT_AREA])
5666 {
5667 if (!row->reversed_p)
5668 {
5669 /* There's a glyph at the end of rows that is used to
5670 place the cursor there. Don't include the width of
5671 this glyph. */
5672 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5673 if (INTEGERP (last->object))
5674 row_width -= last->pixel_width;
5675 }
5676 else
5677 {
5678 /* There could be a stretch glyph at the beginning of R2L
5679 rows that is produced by extend_face_to_end_of_line.
5680 Don't count that glyph. */
5681 struct glyph *g = row->glyphs[TEXT_AREA];
5682
5683 if (g->type == STRETCH_GLYPH && INTEGERP (g->object))
5684 {
5685 row_width -= g->pixel_width;
5686 seen_reversed_p = 1;
5687 }
5688 }
5689 }
5690
5691 height += row->height;
5692 width = max (width, row_width);
5693 }
5694
5695 /* If we've seen partial-length R2L rows, we need to re-adjust the
5696 tool-tip frame width and redisplay it again, to avoid over-wide
5697 tips due to the stretch glyph that extends R2L lines to full
5698 width of the frame. */
5699 if (seen_reversed_p)
5700 {
5701 /* w->total_cols and FRAME_TOTAL_COLS want the width in columns,
5702 not in pixels. */
5703 width /= WINDOW_FRAME_COLUMN_WIDTH (w);
5704 w->total_cols = make_number (width);
5705 FRAME_TOTAL_COLS (f) = width;
5706 adjust_glyphs (f);
5707 w->pseudo_window_p = 1;
5708 clear_glyph_matrix (w->desired_matrix);
5709 clear_glyph_matrix (w->current_matrix);
5710 try_window (FRAME_ROOT_WINDOW (f), pos, TRY_WINDOW_IGNORE_FONTS_CHANGE);
5711 width = height = 0;
5712 /* Recompute width and height of the tooltip. */
5713 for (i = 0; i < w->desired_matrix->nrows; ++i)
5714 {
5715 struct glyph_row *row = &w->desired_matrix->rows[i];
5716 struct glyph *last;
5717 int row_width;
5718
5719 if (!row->enabled_p || !row->displays_text_p)
5720 break;
5721 row->full_width_p = 1;
5722 row_width = row->pixel_width;
5723 if (row->used[TEXT_AREA] && !row->reversed_p)
5724 {
5725 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5726 if (INTEGERP (last->object))
5727 row_width -= last->pixel_width;
5728 }
5729
5730 height += row->height;
5731 width = max (width, row_width);
5732 }
5733 }
5734
5735 /* Round up the height to an integral multiple of FRAME_LINE_HEIGHT. */
5736 if (height % FRAME_LINE_HEIGHT (f) != 0)
5737 height += FRAME_LINE_HEIGHT (f) - height % FRAME_LINE_HEIGHT (f);
5738 /* Add the frame's internal border to the width and height the w32
5739 window should have. */
5740 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5741 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5742
5743 /* Move the tooltip window where the mouse pointer is. Resize and
5744 show it. */
5745 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5746
5747 {
5748 /* Adjust Window size to take border into account. */
5749 RECT rect;
5750 rect.left = rect.top = 0;
5751 rect.right = width;
5752 rect.bottom = height;
5753 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
5754 FRAME_EXTERNAL_MENU_BAR (f));
5755
5756 /* Position and size tooltip, and put it in the topmost group.
5757 The add-on of FRAME_COLUMN_WIDTH to the 5th argument is a
5758 peculiarity of w32 display: without it, some fonts cause the
5759 last character of the tip to be truncated or wrapped around to
5760 the next line. */
5761 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
5762 root_x, root_y,
5763 rect.right - rect.left + FRAME_COLUMN_WIDTH (f),
5764 rect.bottom - rect.top, SWP_NOACTIVATE);
5765
5766 /* Ensure tooltip is on top of other topmost windows (eg menus). */
5767 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
5768 0, 0, 0, 0,
5769 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
5770
5771 /* Let redisplay know that we have made the frame visible already. */
5772 f->async_visible = 1;
5773
5774 ShowWindow (FRAME_W32_WINDOW (f), SW_SHOWNOACTIVATE);
5775 }
5776
5777 /* Draw into the window. */
5778 w->must_be_updated_p = 1;
5779 update_single_window (w, 1);
5780
5781 UNBLOCK_INPUT;
5782
5783 /* Restore original current buffer. */
5784 set_buffer_internal_1 (old_buffer);
5785 windows_or_buffers_changed = old_windows_or_buffers_changed;
5786
5787 start_timer:
5788 /* Let the tip disappear after timeout seconds. */
5789 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
5790 intern ("x-hide-tip"));
5791
5792 UNGCPRO;
5793 return unbind_to (count, Qnil);
5794 }
5795
5796
5797 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
5798 doc: /* Hide the current tooltip window, if there is any.
5799 Value is t if tooltip was open, nil otherwise. */)
5800 (void)
5801 {
5802 int count;
5803 Lisp_Object deleted, frame, timer;
5804 struct gcpro gcpro1, gcpro2;
5805
5806 /* Return quickly if nothing to do. */
5807 if (NILP (tip_timer) && NILP (tip_frame))
5808 return Qnil;
5809
5810 frame = tip_frame;
5811 timer = tip_timer;
5812 GCPRO2 (frame, timer);
5813 tip_frame = tip_timer = deleted = Qnil;
5814
5815 count = SPECPDL_INDEX ();
5816 specbind (Qinhibit_redisplay, Qt);
5817 specbind (Qinhibit_quit, Qt);
5818
5819 if (!NILP (timer))
5820 call1 (Qcancel_timer, timer);
5821
5822 if (FRAMEP (frame))
5823 {
5824 delete_frame (frame, Qnil);
5825 deleted = Qt;
5826 }
5827
5828 UNGCPRO;
5829 return unbind_to (count, deleted);
5830 }
5831
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 cancelled 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 enouth 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 /* Text property `display' should be nonsticky by default. */
6814 Vtext_property_default_nonsticky
6815 = Fcons (Fcons (Qdisplay, Qt), Vtext_property_default_nonsticky);
6816
6817
6818 Fput (Qundefined_color, Qerror_conditions,
6819 pure_cons (Qundefined_color, pure_cons (Qerror, Qnil)));
6820 Fput (Qundefined_color, Qerror_message,
6821 make_pure_c_string ("Undefined color"));
6822
6823 staticpro (&w32_grabbed_keys);
6824 w32_grabbed_keys = Qnil;
6825
6826 DEFVAR_LISP ("w32-color-map", Vw32_color_map,
6827 doc: /* An array of color name mappings for Windows. */);
6828 Vw32_color_map = Qnil;
6829
6830 DEFVAR_LISP ("w32-pass-alt-to-system", Vw32_pass_alt_to_system,
6831 doc: /* Non-nil if Alt key presses are passed on to Windows.
6832 When non-nil, for example, Alt pressed and released and then space will
6833 open the System menu. When nil, Emacs processes the Alt key events, and
6834 then silently swallows them. */);
6835 Vw32_pass_alt_to_system = Qnil;
6836
6837 DEFVAR_LISP ("w32-alt-is-meta", Vw32_alt_is_meta,
6838 doc: /* Non-nil if the Alt key is to be considered the same as the META key.
6839 When nil, Emacs will translate the Alt key to the ALT modifier, not to META. */);
6840 Vw32_alt_is_meta = Qt;
6841
6842 DEFVAR_INT ("w32-quit-key", w32_quit_key,
6843 doc: /* If non-zero, the virtual key code for an alternative quit key. */);
6844 w32_quit_key = 0;
6845
6846 DEFVAR_LISP ("w32-pass-lwindow-to-system",
6847 Vw32_pass_lwindow_to_system,
6848 doc: /* If non-nil, the left \"Windows\" key is passed on to Windows.
6849
6850 When non-nil, the Start menu is opened by tapping the key.
6851 If you set this to nil, the left \"Windows\" key is processed by Emacs
6852 according to the value of `w32-lwindow-modifier', which see.
6853
6854 Note that some combinations of the left \"Windows\" key with other keys are
6855 caught by Windows at low level, and so binding them in Emacs will have no
6856 effect. For example, <lwindow>-r always pops up the Windows Run dialog,
6857 <lwindow>-<Pause> pops up the "System Properties" dialog, etc. However, see
6858 the doc string of `w32-phantom-key-code'. */);
6859 Vw32_pass_lwindow_to_system = Qt;
6860
6861 DEFVAR_LISP ("w32-pass-rwindow-to-system",
6862 Vw32_pass_rwindow_to_system,
6863 doc: /* If non-nil, the right \"Windows\" key is passed on to Windows.
6864
6865 When non-nil, the Start menu is opened by tapping the key.
6866 If you set this to nil, the right \"Windows\" key is processed by Emacs
6867 according to the value of `w32-rwindow-modifier', which see.
6868
6869 Note that some combinations of the right \"Windows\" key with other keys are
6870 caught by Windows at low level, and so binding them in Emacs will have no
6871 effect. For example, <rwindow>-r always pops up the Windows Run dialog,
6872 <rwindow>-<Pause> pops up the "System Properties" dialog, etc. However, see
6873 the doc string of `w32-phantom-key-code'. */);
6874 Vw32_pass_rwindow_to_system = Qt;
6875
6876 DEFVAR_LISP ("w32-phantom-key-code",
6877 Vw32_phantom_key_code,
6878 doc: /* Virtual key code used to generate \"phantom\" key presses.
6879 Value is a number between 0 and 255.
6880
6881 Phantom key presses are generated in order to stop the system from
6882 acting on \"Windows\" key events when `w32-pass-lwindow-to-system' or
6883 `w32-pass-rwindow-to-system' is nil. */);
6884 /* Although 255 is technically not a valid key code, it works and
6885 means that this hack won't interfere with any real key code. */
6886 XSETINT (Vw32_phantom_key_code, 255);
6887
6888 DEFVAR_LISP ("w32-enable-num-lock",
6889 Vw32_enable_num_lock,
6890 doc: /* If non-nil, the Num Lock key acts normally.
6891 Set to nil to handle Num Lock as the `kp-numlock' key. */);
6892 Vw32_enable_num_lock = Qt;
6893
6894 DEFVAR_LISP ("w32-enable-caps-lock",
6895 Vw32_enable_caps_lock,
6896 doc: /* If non-nil, the Caps Lock key acts normally.
6897 Set to nil to handle Caps Lock as the `capslock' key. */);
6898 Vw32_enable_caps_lock = Qt;
6899
6900 DEFVAR_LISP ("w32-scroll-lock-modifier",
6901 Vw32_scroll_lock_modifier,
6902 doc: /* Modifier to use for the Scroll Lock ON state.
6903 The value can be hyper, super, meta, alt, control or shift for the
6904 respective modifier, or nil to handle Scroll Lock as the `scroll' key.
6905 Any other value will cause the Scroll Lock key to be ignored. */);
6906 Vw32_scroll_lock_modifier = Qnil;
6907
6908 DEFVAR_LISP ("w32-lwindow-modifier",
6909 Vw32_lwindow_modifier,
6910 doc: /* Modifier to use for the left \"Windows\" key.
6911 The value can be hyper, super, meta, alt, control or shift for the
6912 respective modifier, or nil to appear as the `lwindow' key.
6913 Any other value will cause the key to be ignored. */);
6914 Vw32_lwindow_modifier = Qnil;
6915
6916 DEFVAR_LISP ("w32-rwindow-modifier",
6917 Vw32_rwindow_modifier,
6918 doc: /* Modifier to use for the right \"Windows\" key.
6919 The value can be hyper, super, meta, alt, control or shift for the
6920 respective modifier, or nil to appear as the `rwindow' key.
6921 Any other value will cause the key to be ignored. */);
6922 Vw32_rwindow_modifier = Qnil;
6923
6924 DEFVAR_LISP ("w32-apps-modifier",
6925 Vw32_apps_modifier,
6926 doc: /* Modifier to use for the \"Apps\" key.
6927 The value can be hyper, super, meta, alt, control or shift for the
6928 respective modifier, or nil to appear as the `apps' key.
6929 Any other value will cause the key to be ignored. */);
6930 Vw32_apps_modifier = Qnil;
6931
6932 DEFVAR_BOOL ("w32-enable-synthesized-fonts", w32_enable_synthesized_fonts,
6933 doc: /* Non-nil enables selection of artificially italicized and bold fonts. */);
6934 w32_enable_synthesized_fonts = 0;
6935
6936 DEFVAR_LISP ("w32-enable-palette", Vw32_enable_palette,
6937 doc: /* Non-nil enables Windows palette management to map colors exactly. */);
6938 Vw32_enable_palette = Qt;
6939
6940 DEFVAR_INT ("w32-mouse-button-tolerance",
6941 w32_mouse_button_tolerance,
6942 doc: /* Analogue of double click interval for faking middle mouse events.
6943 The value is the minimum time in milliseconds that must elapse between
6944 left and right button down events before they are considered distinct events.
6945 If both mouse buttons are depressed within this interval, a middle mouse
6946 button down event is generated instead. */);
6947 w32_mouse_button_tolerance = GetDoubleClickTime () / 2;
6948
6949 DEFVAR_INT ("w32-mouse-move-interval",
6950 w32_mouse_move_interval,
6951 doc: /* Minimum interval between mouse move events.
6952 The value is the minimum time in milliseconds that must elapse between
6953 successive mouse move (or scroll bar drag) events before they are
6954 reported as lisp events. */);
6955 w32_mouse_move_interval = 0;
6956
6957 DEFVAR_BOOL ("w32-pass-extra-mouse-buttons-to-system",
6958 w32_pass_extra_mouse_buttons_to_system,
6959 doc: /* If non-nil, the fourth and fifth mouse buttons are passed to Windows.
6960 Recent versions of Windows support mice with up to five buttons.
6961 Since most applications don't support these extra buttons, most mouse
6962 drivers will allow you to map them to functions at the system level.
6963 If this variable is non-nil, Emacs will pass them on, allowing the
6964 system to handle them. */);
6965 w32_pass_extra_mouse_buttons_to_system = 0;
6966
6967 DEFVAR_BOOL ("w32-pass-multimedia-buttons-to-system",
6968 w32_pass_multimedia_buttons_to_system,
6969 doc: /* If non-nil, media buttons are passed to Windows.
6970 Some modern keyboards contain buttons for controlling media players, web
6971 browsers and other applications. Generally these buttons are handled on a
6972 system wide basis, but by setting this to nil they are made available
6973 to Emacs for binding. Depending on your keyboard, additional keys that
6974 may be available are:
6975
6976 browser-back, browser-forward, browser-refresh, browser-stop,
6977 browser-search, browser-favorites, browser-home,
6978 mail, mail-reply, mail-forward, mail-send,
6979 app-1, app-2,
6980 help, find, new, open, close, save, print, undo, redo, copy, cut, paste,
6981 spell-check, correction-list, toggle-dictate-command,
6982 media-next, media-previous, media-stop, media-play-pause, media-select,
6983 media-play, media-pause, media-record, media-fast-forward, media-rewind,
6984 media-channel-up, media-channel-down,
6985 volume-mute, volume-up, volume-down,
6986 mic-volume-mute, mic-volume-down, mic-volume-up, mic-toggle,
6987 bass-down, bass-boost, bass-up, treble-down, treble-up */);
6988 w32_pass_multimedia_buttons_to_system = 1;
6989
6990 #if 0 /* TODO: Mouse cursor customization. */
6991 DEFVAR_LISP ("x-pointer-shape", Vx_pointer_shape,
6992 doc: /* The shape of the pointer when over text.
6993 Changing the value does not affect existing frames
6994 unless you set the mouse color. */);
6995 Vx_pointer_shape = Qnil;
6996
6997 Vx_nontext_pointer_shape = Qnil;
6998
6999 Vx_mode_pointer_shape = Qnil;
7000
7001 DEFVAR_LISP ("x-hourglass-pointer-shape", Vx_hourglass_pointer_shape,
7002 doc: /* The shape of the pointer when Emacs is busy.
7003 This variable takes effect when you create a new frame
7004 or when you set the mouse color. */);
7005 Vx_hourglass_pointer_shape = Qnil;
7006
7007 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
7008 Vx_sensitive_text_pointer_shape,
7009 doc: /* The shape of the pointer when over mouse-sensitive text.
7010 This variable takes effect when you create a new frame
7011 or when you set the mouse color. */);
7012 Vx_sensitive_text_pointer_shape = Qnil;
7013
7014 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
7015 Vx_window_horizontal_drag_shape,
7016 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
7017 This variable takes effect when you create a new frame
7018 or when you set the mouse color. */);
7019 Vx_window_horizontal_drag_shape = Qnil;
7020 #endif
7021
7022 DEFVAR_LISP ("x-cursor-fore-pixel", Vx_cursor_fore_pixel,
7023 doc: /* A string indicating the foreground color of the cursor box. */);
7024 Vx_cursor_fore_pixel = Qnil;
7025
7026 DEFVAR_LISP ("x-max-tooltip-size", Vx_max_tooltip_size,
7027 doc: /* Maximum size for tooltips.
7028 Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
7029 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
7030
7031 DEFVAR_LISP ("x-no-window-manager", Vx_no_window_manager,
7032 doc: /* Non-nil if no window manager is in use.
7033 Emacs doesn't try to figure this out; this is always nil
7034 unless you set it to something else. */);
7035 /* We don't have any way to find this out, so set it to nil
7036 and maybe the user would like to set it to t. */
7037 Vx_no_window_manager = Qnil;
7038
7039 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
7040 Vx_pixel_size_width_font_regexp,
7041 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
7042
7043 Since Emacs gets width of a font matching with this regexp from
7044 PIXEL_SIZE field of the name, font finding mechanism gets faster for
7045 such a font. This is especially effective for such large fonts as
7046 Chinese, Japanese, and Korean. */);
7047 Vx_pixel_size_width_font_regexp = Qnil;
7048
7049 DEFVAR_LISP ("w32-bdf-filename-alist",
7050 Vw32_bdf_filename_alist,
7051 doc: /* List of bdf fonts and their corresponding filenames. */);
7052 Vw32_bdf_filename_alist = Qnil;
7053
7054 DEFVAR_BOOL ("w32-strict-fontnames",
7055 w32_strict_fontnames,
7056 doc: /* Non-nil means only use fonts that are exact matches for those requested.
7057 Default is nil, which allows old fontnames that are not XLFD compliant,
7058 and allows third-party CJK display to work by specifying false charset
7059 fields to trick Emacs into translating to Big5, SJIS etc.
7060 Setting this to t will prevent wrong fonts being selected when
7061 fontsets are automatically created. */);
7062 w32_strict_fontnames = 0;
7063
7064 DEFVAR_BOOL ("w32-strict-painting",
7065 w32_strict_painting,
7066 doc: /* Non-nil means use strict rules for repainting frames.
7067 Set this to nil to get the old behavior for repainting; this should
7068 only be necessary if the default setting causes problems. */);
7069 w32_strict_painting = 1;
7070
7071 #if 0 /* TODO: Port to W32 */
7072 defsubr (&Sx_change_window_property);
7073 defsubr (&Sx_delete_window_property);
7074 defsubr (&Sx_window_property);
7075 #endif
7076 defsubr (&Sxw_display_color_p);
7077 defsubr (&Sx_display_grayscale_p);
7078 defsubr (&Sxw_color_defined_p);
7079 defsubr (&Sxw_color_values);
7080 defsubr (&Sx_server_max_request_size);
7081 defsubr (&Sx_server_vendor);
7082 defsubr (&Sx_server_version);
7083 defsubr (&Sx_display_pixel_width);
7084 defsubr (&Sx_display_pixel_height);
7085 defsubr (&Sx_display_mm_width);
7086 defsubr (&Sx_display_mm_height);
7087 defsubr (&Sx_display_screens);
7088 defsubr (&Sx_display_planes);
7089 defsubr (&Sx_display_color_cells);
7090 defsubr (&Sx_display_visual_class);
7091 defsubr (&Sx_display_backing_store);
7092 defsubr (&Sx_display_save_under);
7093 defsubr (&Sx_create_frame);
7094 defsubr (&Sx_open_connection);
7095 defsubr (&Sx_close_connection);
7096 defsubr (&Sx_display_list);
7097 defsubr (&Sx_synchronize);
7098 defsubr (&Sx_focus_frame);
7099
7100 /* W32 specific functions */
7101
7102 defsubr (&Sw32_define_rgb_color);
7103 defsubr (&Sw32_default_color_map);
7104 defsubr (&Sw32_send_sys_command);
7105 defsubr (&Sw32_shell_execute);
7106 defsubr (&Sw32_register_hot_key);
7107 defsubr (&Sw32_unregister_hot_key);
7108 defsubr (&Sw32_registered_hot_keys);
7109 defsubr (&Sw32_reconstruct_hot_key);
7110 defsubr (&Sw32_toggle_lock_key);
7111 defsubr (&Sw32_window_exists_p);
7112 defsubr (&Sw32_battery_status);
7113
7114 defsubr (&Sfile_system_info);
7115 defsubr (&Sdefault_printer_name);
7116
7117 check_window_system_func = check_w32;
7118
7119
7120 hourglass_timer = 0;
7121 hourglass_hwnd = NULL;
7122
7123 defsubr (&Sx_show_tip);
7124 defsubr (&Sx_hide_tip);
7125 tip_timer = Qnil;
7126 staticpro (&tip_timer);
7127 tip_frame = Qnil;
7128 staticpro (&tip_frame);
7129
7130 last_show_tip_args = Qnil;
7131 staticpro (&last_show_tip_args);
7132
7133 defsubr (&Sx_file_dialog);
7134 defsubr (&Ssystem_move_file_to_trash);
7135 }
7136
7137
7138 /*
7139 globals_of_w32fns is used to initialize those global variables that
7140 must always be initialized on startup even when the global variable
7141 initialized is non zero (see the function main in emacs.c).
7142 globals_of_w32fns is called from syms_of_w32fns when the global
7143 variable initialized is 0 and directly from main when initialized
7144 is non zero.
7145 */
7146 void
7147 globals_of_w32fns (void)
7148 {
7149 HMODULE user32_lib = GetModuleHandle ("user32.dll");
7150 /*
7151 TrackMouseEvent not available in all versions of Windows, so must load
7152 it dynamically. Do it once, here, instead of every time it is used.
7153 */
7154 track_mouse_event_fn = (TrackMouseEvent_Proc)
7155 GetProcAddress (user32_lib, "TrackMouseEvent");
7156
7157 monitor_from_point_fn = (MonitorFromPoint_Proc)
7158 GetProcAddress (user32_lib, "MonitorFromPoint");
7159 get_monitor_info_fn = (GetMonitorInfo_Proc)
7160 GetProcAddress (user32_lib, "GetMonitorInfoA");
7161
7162 {
7163 HMODULE imm32_lib = GetModuleHandle ("imm32.dll");
7164 get_composition_string_fn = (ImmGetCompositionString_Proc)
7165 GetProcAddress (imm32_lib, "ImmGetCompositionStringW");
7166 get_ime_context_fn = (ImmGetContext_Proc)
7167 GetProcAddress (imm32_lib, "ImmGetContext");
7168 release_ime_context_fn = (ImmReleaseContext_Proc)
7169 GetProcAddress (imm32_lib, "ImmReleaseContext");
7170 set_ime_composition_window_fn = (ImmSetCompositionWindow_Proc)
7171 GetProcAddress (imm32_lib, "ImmSetCompositionWindow");
7172 }
7173 DEFVAR_INT ("w32-ansi-code-page",
7174 w32_ansi_code_page,
7175 doc: /* The ANSI code page used by the system. */);
7176 w32_ansi_code_page = GetACP ();
7177
7178 /* MessageBox does not work without this when linked to comctl32.dll 6.0. */
7179 InitCommonControls ();
7180
7181 syms_of_w32uniscribe ();
7182 }
7183
7184 #undef abort
7185
7186 void
7187 w32_abort (void)
7188 {
7189 int button;
7190 button = MessageBox (NULL,
7191 "A fatal error has occurred!\n\n"
7192 "Would you like to attach a debugger?\n\n"
7193 "Select YES to debug, NO to abort Emacs"
7194 #if __GNUC__
7195 "\n\n(type \"gdb -p <emacs-PID>\" and\n"
7196 "\"continue\" inside GDB before clicking YES.)"
7197 #endif
7198 , "Emacs Abort Dialog",
7199 MB_ICONEXCLAMATION | MB_TASKMODAL
7200 | MB_SETFOREGROUND | MB_YESNO);
7201 switch (button)
7202 {
7203 case IDYES:
7204 DebugBreak ();
7205 exit (2); /* tell the compiler we will never return */
7206 case IDNO:
7207 default:
7208 abort ();
7209 break;
7210 }
7211 }
7212
7213 /* For convenience when debugging. */
7214 int
7215 w32_last_error (void)
7216 {
7217 return GetLastError ();
7218 }