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