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