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