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