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