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