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