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