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