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