(Qbalinese, Qbuginese, Qbuhid, Qcuneiform, Qcypriot)
[bpt/emacs.git] / src / w32fns.c
... / ...
CommitLineData
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
6This file is part of GNU Emacs.
7
8GNU Emacs is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 3, or (at your option)
11any later version.
12
13GNU Emacs is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GNU Emacs; see the file COPYING. If not, write to
20the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21Boston, MA 02110-1301, USA. */
22
23/* Added by Kevin Gallo */
24
25#include <config.h>
26
27#include <signal.h>
28#include <stdio.h>
29#include <limits.h>
30#include <errno.h>
31#include <math.h>
32
33#include "lisp.h"
34#include "w32term.h"
35#include "frame.h"
36#include "window.h"
37#include "buffer.h"
38#include "intervals.h"
39#include "dispextern.h"
40#include "keyboard.h"
41#include "blockinput.h"
42#include "epaths.h"
43#include "character.h"
44#include "charset.h"
45#include "coding.h"
46#include "ccl.h"
47#include "fontset.h"
48#include "systime.h"
49#include "termhooks.h"
50#include "w32heap.h"
51
52#include "bitmaps/gray.xbm"
53
54#include <commctrl.h>
55#include <commdlg.h>
56#include <shellapi.h>
57#include <ctype.h>
58#include <winspool.h>
59#include <objbase.h>
60
61#include <dlgs.h>
62#include <imm.h>
63#define FILE_NAME_TEXT_FIELD edt1
64
65#ifdef USE_FONT_BACKEND
66#include "font.h"
67#endif
68
69void syms_of_w32fns ();
70void globals_of_w32fns ();
71
72extern void free_frame_menubar ();
73extern double atof ();
74extern int w32_console_toggle_lock_key P_ ((int, Lisp_Object));
75extern void w32_menu_display_help P_ ((HWND, HMENU, UINT, UINT));
76extern void w32_free_menu_strings P_ ((HWND));
77extern XCharStruct *w32_per_char_metric P_ ((XFontStruct *, wchar_t *, int));
78
79extern int quit_char;
80
81extern char *lispy_function_keys[];
82
83/* The colormap for converting color names to RGB values */
84Lisp_Object Vw32_color_map;
85
86/* Non nil if alt key presses are passed on to Windows. */
87Lisp_Object Vw32_pass_alt_to_system;
88
89/* Non nil if alt key is translated to meta_modifier, nil if it is translated
90 to alt_modifier. */
91Lisp_Object Vw32_alt_is_meta;
92
93/* If non-zero, the windows virtual key code for an alternative quit key. */
94int w32_quit_key;
95
96/* Non nil if left window key events are passed on to Windows (this only
97 affects whether "tapping" the key opens the Start menu). */
98Lisp_Object Vw32_pass_lwindow_to_system;
99
100/* Non nil if right window key events are passed on to Windows (this
101 only affects whether "tapping" the key opens the Start menu). */
102Lisp_Object Vw32_pass_rwindow_to_system;
103
104/* Virtual key code used to generate "phantom" key presses in order
105 to stop system from acting on Windows key events. */
106Lisp_Object Vw32_phantom_key_code;
107
108/* Modifier associated with the left "Windows" key, or nil to act as a
109 normal key. */
110Lisp_Object Vw32_lwindow_modifier;
111
112/* Modifier associated with the right "Windows" key, or nil to act as a
113 normal key. */
114Lisp_Object Vw32_rwindow_modifier;
115
116/* Modifier associated with the "Apps" key, or nil to act as a normal
117 key. */
118Lisp_Object Vw32_apps_modifier;
119
120/* Value is nil if Num Lock acts as a function key. */
121Lisp_Object Vw32_enable_num_lock;
122
123/* Value is nil if Caps Lock acts as a function key. */
124Lisp_Object Vw32_enable_caps_lock;
125
126/* Modifier associated with Scroll Lock, or nil to act as a normal key. */
127Lisp_Object Vw32_scroll_lock_modifier;
128
129/* Switch to control whether we inhibit requests for synthesized bold
130 and italic versions of fonts. */
131int w32_enable_synthesized_fonts;
132
133/* Enable palette management. */
134Lisp_Object Vw32_enable_palette;
135
136/* Control how close left/right button down events must be to
137 be converted to a middle button down event. */
138int w32_mouse_button_tolerance;
139
140/* Minimum interval between mouse movement (and scroll bar drag)
141 events that are passed on to the event loop. */
142int w32_mouse_move_interval;
143
144/* Flag to indicate if XBUTTON events should be passed on to Windows. */
145static int w32_pass_extra_mouse_buttons_to_system;
146
147/* Flag to indicate if media keys should be passed on to Windows. */
148static int w32_pass_multimedia_buttons_to_system;
149
150/* Non nil if no window manager is in use. */
151Lisp_Object Vx_no_window_manager;
152
153/* Non-zero means we're allowed to display a hourglass pointer. */
154
155int display_hourglass_p;
156
157/* If non-zero, a w32 timer that, when it expires, displays an
158 hourglass cursor on all frames. */
159static unsigned hourglass_timer = 0;
160static HWND hourglass_hwnd = NULL;
161
162/* The background and shape of the mouse pointer, and shape when not
163 over text or in the modeline. */
164
165Lisp_Object Vx_pointer_shape, Vx_nontext_pointer_shape, Vx_mode_pointer_shape;
166Lisp_Object Vx_hourglass_pointer_shape, Vx_window_horizontal_drag_shape;
167
168/* The shape when over mouse-sensitive text. */
169
170Lisp_Object Vx_sensitive_text_pointer_shape;
171
172#ifndef IDC_HAND
173#define IDC_HAND MAKEINTRESOURCE(32649)
174#endif
175
176/* Color of chars displayed in cursor box. */
177
178Lisp_Object Vx_cursor_fore_pixel;
179
180/* Nonzero if using Windows. */
181
182static int w32_in_use;
183
184/* Regexp matching a font name whose width is the same as `PIXEL_SIZE'. */
185
186Lisp_Object Vx_pixel_size_width_font_regexp;
187
188/* Alist of bdf fonts and the files that define them. */
189Lisp_Object Vw32_bdf_filename_alist;
190
191/* A flag to control whether fonts are matched strictly or not. */
192static int w32_strict_fontnames;
193
194/* A flag to control whether we should only repaint if GetUpdateRect
195 indicates there is an update region. */
196static int w32_strict_painting;
197
198/* Associative list linking character set strings to Windows codepages. */
199static Lisp_Object Vw32_charset_info_alist;
200
201/* VIETNAMESE_CHARSET is not defined in some versions of MSVC. */
202#ifndef VIETNAMESE_CHARSET
203#define VIETNAMESE_CHARSET 163
204#endif
205
206Lisp_Object Qnone;
207Lisp_Object Qsuppress_icon;
208Lisp_Object Qundefined_color;
209Lisp_Object Qcancel_timer;
210Lisp_Object Qhyper;
211Lisp_Object Qsuper;
212Lisp_Object Qmeta;
213Lisp_Object Qalt;
214Lisp_Object Qctrl;
215Lisp_Object Qcontrol;
216Lisp_Object Qshift;
217
218Lisp_Object Qw32_charset_ansi;
219Lisp_Object Qw32_charset_default;
220Lisp_Object Qw32_charset_symbol;
221Lisp_Object Qw32_charset_shiftjis;
222Lisp_Object Qw32_charset_hangeul;
223Lisp_Object Qw32_charset_gb2312;
224Lisp_Object Qw32_charset_chinesebig5;
225Lisp_Object Qw32_charset_oem;
226
227#ifndef JOHAB_CHARSET
228#define JOHAB_CHARSET 130
229#endif
230#ifdef JOHAB_CHARSET
231Lisp_Object Qw32_charset_easteurope;
232Lisp_Object Qw32_charset_turkish;
233Lisp_Object Qw32_charset_baltic;
234Lisp_Object Qw32_charset_russian;
235Lisp_Object Qw32_charset_arabic;
236Lisp_Object Qw32_charset_greek;
237Lisp_Object Qw32_charset_hebrew;
238Lisp_Object Qw32_charset_vietnamese;
239Lisp_Object Qw32_charset_thai;
240Lisp_Object Qw32_charset_johab;
241Lisp_Object Qw32_charset_mac;
242#endif
243
244#ifdef UNICODE_CHARSET
245Lisp_Object Qw32_charset_unicode;
246#endif
247
248/* The ANSI codepage. */
249int w32_ansi_code_page;
250
251/* Prefix for system colors. */
252#define SYSTEM_COLOR_PREFIX "System"
253#define SYSTEM_COLOR_PREFIX_LEN (sizeof (SYSTEM_COLOR_PREFIX) - 1)
254
255/* State variables for emulating a three button mouse. */
256#define LMOUSE 1
257#define MMOUSE 2
258#define RMOUSE 4
259
260static int button_state = 0;
261static W32Msg saved_mouse_button_msg;
262static unsigned mouse_button_timer = 0; /* non-zero when timer is active */
263static W32Msg saved_mouse_move_msg;
264static unsigned mouse_move_timer = 0;
265
266/* Window that is tracking the mouse. */
267static HWND track_mouse_window;
268
269/* Multi-monitor API definitions that are not pulled from the headers
270 since we are compiling for NT 4. */
271#ifndef MONITOR_DEFAULT_TO_NEAREST
272#define MONITOR_DEFAULT_TO_NEAREST 2
273#endif
274/* MinGW headers define MONITORINFO unconditionally, but MSVC ones don't.
275 To avoid a compile error on one or the other, redefine with a new name. */
276struct MONITOR_INFO
277{
278 DWORD cbSize;
279 RECT rcMonitor;
280 RECT rcWork;
281 DWORD dwFlags;
282};
283
284typedef BOOL (WINAPI * TrackMouseEvent_Proc)
285 (IN OUT LPTRACKMOUSEEVENT lpEventTrack);
286typedef LONG (WINAPI * ImmGetCompositionString_Proc)
287 (IN HIMC context, IN DWORD index, OUT LPVOID buffer, IN DWORD bufLen);
288typedef HIMC (WINAPI * ImmGetContext_Proc) (IN HWND window);
289typedef HMONITOR (WINAPI * MonitorFromPoint_Proc) (IN POINT pt, IN DWORD flags);
290typedef BOOL (WINAPI * GetMonitorInfo_Proc)
291 (IN HMONITOR monitor, OUT struct MONITOR_INFO* info);
292
293TrackMouseEvent_Proc track_mouse_event_fn = NULL;
294ClipboardSequence_Proc clipboard_sequence_fn = NULL;
295ImmGetCompositionString_Proc get_composition_string_fn = NULL;
296ImmGetContext_Proc get_ime_context_fn = NULL;
297MonitorFromPoint_Proc monitor_from_point_fn = NULL;
298GetMonitorInfo_Proc get_monitor_info_fn = NULL;
299
300extern AppendMenuW_Proc unicode_append_menu;
301
302/* Flag to selectively ignore WM_IME_CHAR messages. */
303static int ignore_ime_char = 0;
304
305/* W95 mousewheel handler */
306unsigned int msh_mousewheel = 0;
307
308/* Timers */
309#define MOUSE_BUTTON_ID 1
310#define MOUSE_MOVE_ID 2
311#define MENU_FREE_ID 3
312#define HOURGLASS_ID 4
313/* The delay (milliseconds) before a menu is freed after WM_EXITMENULOOP
314 is received. */
315#define MENU_FREE_DELAY 1000
316static unsigned menu_free_timer = 0;
317
318/* The below are defined in frame.c. */
319
320extern Lisp_Object Vwindow_system_version;
321
322#ifdef GLYPH_DEBUG
323int image_cache_refcount, dpyinfo_refcount;
324#endif
325
326
327/* From w32term.c. */
328extern int w32_num_mouse_buttons;
329extern Lisp_Object Vw32_recognize_altgr;
330
331extern HWND w32_system_caret_hwnd;
332
333extern int w32_system_caret_height;
334extern int w32_system_caret_x;
335extern int w32_system_caret_y;
336extern int w32_use_visible_system_caret;
337
338static HWND w32_visible_system_caret_hwnd;
339
340/* From w32menu.c */
341extern HMENU current_popup_menu;
342static int menubar_in_use = 0;
343
344/* Function prototypes for hourglass support. */
345static void show_hourglass P_ ((struct frame *));
346static void hide_hourglass P_ ((void));
347
348
349\f
350/* Error if we are not connected to MS-Windows. */
351void
352check_w32 ()
353{
354 if (! w32_in_use)
355 error ("MS-Windows not in use or not initialized");
356}
357
358/* Nonzero if we can use mouse menus.
359 You should not call this unless HAVE_MENUS is defined. */
360
361int
362have_menus_p ()
363{
364 return w32_in_use;
365}
366
367/* Extract a frame as a FRAME_PTR, defaulting to the selected frame
368 and checking validity for W32. */
369
370FRAME_PTR
371check_x_frame (frame)
372 Lisp_Object frame;
373{
374 FRAME_PTR f;
375
376 if (NILP (frame))
377 frame = selected_frame;
378 CHECK_LIVE_FRAME (frame);
379 f = XFRAME (frame);
380 if (! FRAME_W32_P (f))
381 error ("Non-W32 frame used");
382 return f;
383}
384
385/* Let the user specify a display with a frame.
386 nil stands for the selected frame--or, if that is not a w32 frame,
387 the first display on the list. */
388
389struct w32_display_info *
390check_x_display_info (frame)
391 Lisp_Object frame;
392{
393 if (NILP (frame))
394 {
395 struct frame *sf = XFRAME (selected_frame);
396
397 if (FRAME_W32_P (sf) && FRAME_LIVE_P (sf))
398 return FRAME_W32_DISPLAY_INFO (sf);
399 else
400 return &one_w32_display_info;
401 }
402 else if (STRINGP (frame))
403 return x_display_info_for_name (frame);
404 else
405 {
406 FRAME_PTR f;
407
408 CHECK_LIVE_FRAME (frame);
409 f = XFRAME (frame);
410 if (! FRAME_W32_P (f))
411 error ("Non-W32 frame used");
412 return FRAME_W32_DISPLAY_INFO (f);
413 }
414}
415\f
416/* Return the Emacs frame-object corresponding to an w32 window.
417 It could be the frame's main window or an icon window. */
418
419/* This function can be called during GC, so use GC_xxx type test macros. */
420
421struct frame *
422x_window_to_frame (dpyinfo, wdesc)
423 struct w32_display_info *dpyinfo;
424 HWND wdesc;
425{
426 Lisp_Object tail, frame;
427 struct frame *f;
428
429 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
430 {
431 frame = XCAR (tail);
432 if (!FRAMEP (frame))
433 continue;
434 f = XFRAME (frame);
435 if (!FRAME_W32_P (f) || FRAME_W32_DISPLAY_INFO (f) != dpyinfo)
436 continue;
437
438 if (FRAME_W32_WINDOW (f) == wdesc)
439 return f;
440 }
441 return 0;
442}
443
444\f
445static Lisp_Object unwind_create_frame P_ ((Lisp_Object));
446static Lisp_Object unwind_create_tip_frame P_ ((Lisp_Object));
447static void my_create_window P_ ((struct frame *));
448static void my_create_tip_window P_ ((struct frame *));
449
450/* TODO: Native Input Method support; see x_create_im. */
451void x_set_foreground_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
452void x_set_background_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
453void x_set_mouse_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
454void x_set_cursor_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
455void x_set_border_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
456void x_set_cursor_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
457void x_set_icon_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
458void x_set_icon_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
459void x_explicitly_set_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
460void x_set_menu_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
461void x_set_title P_ ((struct frame *, Lisp_Object, Lisp_Object));
462void x_set_tool_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
463static void x_edge_detection P_ ((struct frame *, struct image *, Lisp_Object,
464 Lisp_Object));
465
466
467\f
468
469/* Store the screen positions of frame F into XPTR and YPTR.
470 These are the positions of the containing window manager window,
471 not Emacs's own window. */
472
473void
474x_real_positions (f, xptr, yptr)
475 FRAME_PTR f;
476 int *xptr, *yptr;
477{
478 POINT pt;
479 RECT rect;
480
481 /* Get the bounds of the WM window. */
482 GetWindowRect (FRAME_W32_WINDOW (f), &rect);
483
484 pt.x = 0;
485 pt.y = 0;
486
487 /* Convert (0, 0) in the client area to screen co-ordinates. */
488 ClientToScreen (FRAME_W32_WINDOW (f), &pt);
489
490 /* Remember x_pixels_diff and y_pixels_diff. */
491 f->x_pixels_diff = pt.x - rect.left;
492 f->y_pixels_diff = pt.y - rect.top;
493
494 *xptr = rect.left;
495 *yptr = rect.top;
496}
497
498\f
499
500DEFUN ("w32-define-rgb-color", Fw32_define_rgb_color,
501 Sw32_define_rgb_color, 4, 4, 0,
502 doc: /* Convert RGB numbers to a windows color reference and associate with NAME.
503This adds or updates a named color to `w32-color-map', making it
504available for use. The original entry's RGB ref is returned, or nil
505if the entry is new. */)
506 (red, green, blue, name)
507 Lisp_Object red, green, blue, name;
508{
509 Lisp_Object rgb;
510 Lisp_Object oldrgb = Qnil;
511 Lisp_Object entry;
512
513 CHECK_NUMBER (red);
514 CHECK_NUMBER (green);
515 CHECK_NUMBER (blue);
516 CHECK_STRING (name);
517
518 XSETINT (rgb, RGB (XUINT (red), XUINT (green), XUINT (blue)));
519
520 BLOCK_INPUT;
521
522 /* replace existing entry in w32-color-map or add new entry. */
523 entry = Fassoc (name, Vw32_color_map);
524 if (NILP (entry))
525 {
526 entry = Fcons (name, rgb);
527 Vw32_color_map = Fcons (entry, Vw32_color_map);
528 }
529 else
530 {
531 oldrgb = Fcdr (entry);
532 Fsetcdr (entry, rgb);
533 }
534
535 UNBLOCK_INPUT;
536
537 return (oldrgb);
538}
539
540DEFUN ("w32-load-color-file", Fw32_load_color_file,
541 Sw32_load_color_file, 1, 1, 0,
542 doc: /* Create an alist of color entries from an external file.
543Assign this value to `w32-color-map' to replace the existing color map.
544
545The file should define one named RGB color per line like so:
546 R G B name
547where R,G,B are numbers between 0 and 255 and name is an arbitrary string. */)
548 (filename)
549 Lisp_Object filename;
550{
551 FILE *fp;
552 Lisp_Object cmap = Qnil;
553 Lisp_Object abspath;
554
555 CHECK_STRING (filename);
556 abspath = Fexpand_file_name (filename, Qnil);
557
558 fp = fopen (SDATA (filename), "rt");
559 if (fp)
560 {
561 char buf[512];
562 int red, green, blue;
563 int num;
564
565 BLOCK_INPUT;
566
567 while (fgets (buf, sizeof (buf), fp) != NULL) {
568 if (sscanf (buf, "%u %u %u %n", &red, &green, &blue, &num) == 3)
569 {
570 char *name = buf + num;
571 num = strlen (name) - 1;
572 if (name[num] == '\n')
573 name[num] = 0;
574 cmap = Fcons (Fcons (build_string (name),
575 make_number (RGB (red, green, blue))),
576 cmap);
577 }
578 }
579 fclose (fp);
580
581 UNBLOCK_INPUT;
582 }
583
584 return cmap;
585}
586
587/* The default colors for the w32 color map */
588typedef struct colormap_t
589{
590 char *name;
591 COLORREF colorref;
592} colormap_t;
593
594colormap_t w32_color_map[] =
595{
596 {"snow" , PALETTERGB (255,250,250)},
597 {"ghost white" , PALETTERGB (248,248,255)},
598 {"GhostWhite" , PALETTERGB (248,248,255)},
599 {"white smoke" , PALETTERGB (245,245,245)},
600 {"WhiteSmoke" , PALETTERGB (245,245,245)},
601 {"gainsboro" , PALETTERGB (220,220,220)},
602 {"floral white" , PALETTERGB (255,250,240)},
603 {"FloralWhite" , PALETTERGB (255,250,240)},
604 {"old lace" , PALETTERGB (253,245,230)},
605 {"OldLace" , PALETTERGB (253,245,230)},
606 {"linen" , PALETTERGB (250,240,230)},
607 {"antique white" , PALETTERGB (250,235,215)},
608 {"AntiqueWhite" , PALETTERGB (250,235,215)},
609 {"papaya whip" , PALETTERGB (255,239,213)},
610 {"PapayaWhip" , PALETTERGB (255,239,213)},
611 {"blanched almond" , PALETTERGB (255,235,205)},
612 {"BlanchedAlmond" , PALETTERGB (255,235,205)},
613 {"bisque" , PALETTERGB (255,228,196)},
614 {"peach puff" , PALETTERGB (255,218,185)},
615 {"PeachPuff" , PALETTERGB (255,218,185)},
616 {"navajo white" , PALETTERGB (255,222,173)},
617 {"NavajoWhite" , PALETTERGB (255,222,173)},
618 {"moccasin" , PALETTERGB (255,228,181)},
619 {"cornsilk" , PALETTERGB (255,248,220)},
620 {"ivory" , PALETTERGB (255,255,240)},
621 {"lemon chiffon" , PALETTERGB (255,250,205)},
622 {"LemonChiffon" , PALETTERGB (255,250,205)},
623 {"seashell" , PALETTERGB (255,245,238)},
624 {"honeydew" , PALETTERGB (240,255,240)},
625 {"mint cream" , PALETTERGB (245,255,250)},
626 {"MintCream" , PALETTERGB (245,255,250)},
627 {"azure" , PALETTERGB (240,255,255)},
628 {"alice blue" , PALETTERGB (240,248,255)},
629 {"AliceBlue" , PALETTERGB (240,248,255)},
630 {"lavender" , PALETTERGB (230,230,250)},
631 {"lavender blush" , PALETTERGB (255,240,245)},
632 {"LavenderBlush" , PALETTERGB (255,240,245)},
633 {"misty rose" , PALETTERGB (255,228,225)},
634 {"MistyRose" , PALETTERGB (255,228,225)},
635 {"white" , PALETTERGB (255,255,255)},
636 {"black" , PALETTERGB ( 0, 0, 0)},
637 {"dark slate gray" , PALETTERGB ( 47, 79, 79)},
638 {"DarkSlateGray" , PALETTERGB ( 47, 79, 79)},
639 {"dark slate grey" , PALETTERGB ( 47, 79, 79)},
640 {"DarkSlateGrey" , PALETTERGB ( 47, 79, 79)},
641 {"dim gray" , PALETTERGB (105,105,105)},
642 {"DimGray" , PALETTERGB (105,105,105)},
643 {"dim grey" , PALETTERGB (105,105,105)},
644 {"DimGrey" , PALETTERGB (105,105,105)},
645 {"slate gray" , PALETTERGB (112,128,144)},
646 {"SlateGray" , PALETTERGB (112,128,144)},
647 {"slate grey" , PALETTERGB (112,128,144)},
648 {"SlateGrey" , PALETTERGB (112,128,144)},
649 {"light slate gray" , PALETTERGB (119,136,153)},
650 {"LightSlateGray" , PALETTERGB (119,136,153)},
651 {"light slate grey" , PALETTERGB (119,136,153)},
652 {"LightSlateGrey" , PALETTERGB (119,136,153)},
653 {"gray" , PALETTERGB (190,190,190)},
654 {"grey" , PALETTERGB (190,190,190)},
655 {"light grey" , PALETTERGB (211,211,211)},
656 {"LightGrey" , PALETTERGB (211,211,211)},
657 {"light gray" , PALETTERGB (211,211,211)},
658 {"LightGray" , PALETTERGB (211,211,211)},
659 {"midnight blue" , PALETTERGB ( 25, 25,112)},
660 {"MidnightBlue" , PALETTERGB ( 25, 25,112)},
661 {"navy" , PALETTERGB ( 0, 0,128)},
662 {"navy blue" , PALETTERGB ( 0, 0,128)},
663 {"NavyBlue" , PALETTERGB ( 0, 0,128)},
664 {"cornflower blue" , PALETTERGB (100,149,237)},
665 {"CornflowerBlue" , PALETTERGB (100,149,237)},
666 {"dark slate blue" , PALETTERGB ( 72, 61,139)},
667 {"DarkSlateBlue" , PALETTERGB ( 72, 61,139)},
668 {"slate blue" , PALETTERGB (106, 90,205)},
669 {"SlateBlue" , PALETTERGB (106, 90,205)},
670 {"medium slate blue" , PALETTERGB (123,104,238)},
671 {"MediumSlateBlue" , PALETTERGB (123,104,238)},
672 {"light slate blue" , PALETTERGB (132,112,255)},
673 {"LightSlateBlue" , PALETTERGB (132,112,255)},
674 {"medium blue" , PALETTERGB ( 0, 0,205)},
675 {"MediumBlue" , PALETTERGB ( 0, 0,205)},
676 {"royal blue" , PALETTERGB ( 65,105,225)},
677 {"RoyalBlue" , PALETTERGB ( 65,105,225)},
678 {"blue" , PALETTERGB ( 0, 0,255)},
679 {"dodger blue" , PALETTERGB ( 30,144,255)},
680 {"DodgerBlue" , PALETTERGB ( 30,144,255)},
681 {"deep sky blue" , PALETTERGB ( 0,191,255)},
682 {"DeepSkyBlue" , PALETTERGB ( 0,191,255)},
683 {"sky blue" , PALETTERGB (135,206,235)},
684 {"SkyBlue" , PALETTERGB (135,206,235)},
685 {"light sky blue" , PALETTERGB (135,206,250)},
686 {"LightSkyBlue" , PALETTERGB (135,206,250)},
687 {"steel blue" , PALETTERGB ( 70,130,180)},
688 {"SteelBlue" , PALETTERGB ( 70,130,180)},
689 {"light steel blue" , PALETTERGB (176,196,222)},
690 {"LightSteelBlue" , PALETTERGB (176,196,222)},
691 {"light blue" , PALETTERGB (173,216,230)},
692 {"LightBlue" , PALETTERGB (173,216,230)},
693 {"powder blue" , PALETTERGB (176,224,230)},
694 {"PowderBlue" , PALETTERGB (176,224,230)},
695 {"pale turquoise" , PALETTERGB (175,238,238)},
696 {"PaleTurquoise" , PALETTERGB (175,238,238)},
697 {"dark turquoise" , PALETTERGB ( 0,206,209)},
698 {"DarkTurquoise" , PALETTERGB ( 0,206,209)},
699 {"medium turquoise" , PALETTERGB ( 72,209,204)},
700 {"MediumTurquoise" , PALETTERGB ( 72,209,204)},
701 {"turquoise" , PALETTERGB ( 64,224,208)},
702 {"cyan" , PALETTERGB ( 0,255,255)},
703 {"light cyan" , PALETTERGB (224,255,255)},
704 {"LightCyan" , PALETTERGB (224,255,255)},
705 {"cadet blue" , PALETTERGB ( 95,158,160)},
706 {"CadetBlue" , PALETTERGB ( 95,158,160)},
707 {"medium aquamarine" , PALETTERGB (102,205,170)},
708 {"MediumAquamarine" , PALETTERGB (102,205,170)},
709 {"aquamarine" , PALETTERGB (127,255,212)},
710 {"dark green" , PALETTERGB ( 0,100, 0)},
711 {"DarkGreen" , PALETTERGB ( 0,100, 0)},
712 {"dark olive green" , PALETTERGB ( 85,107, 47)},
713 {"DarkOliveGreen" , PALETTERGB ( 85,107, 47)},
714 {"dark sea green" , PALETTERGB (143,188,143)},
715 {"DarkSeaGreen" , PALETTERGB (143,188,143)},
716 {"sea green" , PALETTERGB ( 46,139, 87)},
717 {"SeaGreen" , PALETTERGB ( 46,139, 87)},
718 {"medium sea green" , PALETTERGB ( 60,179,113)},
719 {"MediumSeaGreen" , PALETTERGB ( 60,179,113)},
720 {"light sea green" , PALETTERGB ( 32,178,170)},
721 {"LightSeaGreen" , PALETTERGB ( 32,178,170)},
722 {"pale green" , PALETTERGB (152,251,152)},
723 {"PaleGreen" , PALETTERGB (152,251,152)},
724 {"spring green" , PALETTERGB ( 0,255,127)},
725 {"SpringGreen" , PALETTERGB ( 0,255,127)},
726 {"lawn green" , PALETTERGB (124,252, 0)},
727 {"LawnGreen" , PALETTERGB (124,252, 0)},
728 {"green" , PALETTERGB ( 0,255, 0)},
729 {"chartreuse" , PALETTERGB (127,255, 0)},
730 {"medium spring green" , PALETTERGB ( 0,250,154)},
731 {"MediumSpringGreen" , PALETTERGB ( 0,250,154)},
732 {"green yellow" , PALETTERGB (173,255, 47)},
733 {"GreenYellow" , PALETTERGB (173,255, 47)},
734 {"lime green" , PALETTERGB ( 50,205, 50)},
735 {"LimeGreen" , PALETTERGB ( 50,205, 50)},
736 {"yellow green" , PALETTERGB (154,205, 50)},
737 {"YellowGreen" , PALETTERGB (154,205, 50)},
738 {"forest green" , PALETTERGB ( 34,139, 34)},
739 {"ForestGreen" , PALETTERGB ( 34,139, 34)},
740 {"olive drab" , PALETTERGB (107,142, 35)},
741 {"OliveDrab" , PALETTERGB (107,142, 35)},
742 {"dark khaki" , PALETTERGB (189,183,107)},
743 {"DarkKhaki" , PALETTERGB (189,183,107)},
744 {"khaki" , PALETTERGB (240,230,140)},
745 {"pale goldenrod" , PALETTERGB (238,232,170)},
746 {"PaleGoldenrod" , PALETTERGB (238,232,170)},
747 {"light goldenrod yellow" , PALETTERGB (250,250,210)},
748 {"LightGoldenrodYellow" , PALETTERGB (250,250,210)},
749 {"light yellow" , PALETTERGB (255,255,224)},
750 {"LightYellow" , PALETTERGB (255,255,224)},
751 {"yellow" , PALETTERGB (255,255, 0)},
752 {"gold" , PALETTERGB (255,215, 0)},
753 {"light goldenrod" , PALETTERGB (238,221,130)},
754 {"LightGoldenrod" , PALETTERGB (238,221,130)},
755 {"goldenrod" , PALETTERGB (218,165, 32)},
756 {"dark goldenrod" , PALETTERGB (184,134, 11)},
757 {"DarkGoldenrod" , PALETTERGB (184,134, 11)},
758 {"rosy brown" , PALETTERGB (188,143,143)},
759 {"RosyBrown" , PALETTERGB (188,143,143)},
760 {"indian red" , PALETTERGB (205, 92, 92)},
761 {"IndianRed" , PALETTERGB (205, 92, 92)},
762 {"saddle brown" , PALETTERGB (139, 69, 19)},
763 {"SaddleBrown" , PALETTERGB (139, 69, 19)},
764 {"sienna" , PALETTERGB (160, 82, 45)},
765 {"peru" , PALETTERGB (205,133, 63)},
766 {"burlywood" , PALETTERGB (222,184,135)},
767 {"beige" , PALETTERGB (245,245,220)},
768 {"wheat" , PALETTERGB (245,222,179)},
769 {"sandy brown" , PALETTERGB (244,164, 96)},
770 {"SandyBrown" , PALETTERGB (244,164, 96)},
771 {"tan" , PALETTERGB (210,180,140)},
772 {"chocolate" , PALETTERGB (210,105, 30)},
773 {"firebrick" , PALETTERGB (178,34, 34)},
774 {"brown" , PALETTERGB (165,42, 42)},
775 {"dark salmon" , PALETTERGB (233,150,122)},
776 {"DarkSalmon" , PALETTERGB (233,150,122)},
777 {"salmon" , PALETTERGB (250,128,114)},
778 {"light salmon" , PALETTERGB (255,160,122)},
779 {"LightSalmon" , PALETTERGB (255,160,122)},
780 {"orange" , PALETTERGB (255,165, 0)},
781 {"dark orange" , PALETTERGB (255,140, 0)},
782 {"DarkOrange" , PALETTERGB (255,140, 0)},
783 {"coral" , PALETTERGB (255,127, 80)},
784 {"light coral" , PALETTERGB (240,128,128)},
785 {"LightCoral" , PALETTERGB (240,128,128)},
786 {"tomato" , PALETTERGB (255, 99, 71)},
787 {"orange red" , PALETTERGB (255, 69, 0)},
788 {"OrangeRed" , PALETTERGB (255, 69, 0)},
789 {"red" , PALETTERGB (255, 0, 0)},
790 {"hot pink" , PALETTERGB (255,105,180)},
791 {"HotPink" , PALETTERGB (255,105,180)},
792 {"deep pink" , PALETTERGB (255, 20,147)},
793 {"DeepPink" , PALETTERGB (255, 20,147)},
794 {"pink" , PALETTERGB (255,192,203)},
795 {"light pink" , PALETTERGB (255,182,193)},
796 {"LightPink" , PALETTERGB (255,182,193)},
797 {"pale violet red" , PALETTERGB (219,112,147)},
798 {"PaleVioletRed" , PALETTERGB (219,112,147)},
799 {"maroon" , PALETTERGB (176, 48, 96)},
800 {"medium violet red" , PALETTERGB (199, 21,133)},
801 {"MediumVioletRed" , PALETTERGB (199, 21,133)},
802 {"violet red" , PALETTERGB (208, 32,144)},
803 {"VioletRed" , PALETTERGB (208, 32,144)},
804 {"magenta" , PALETTERGB (255, 0,255)},
805 {"violet" , PALETTERGB (238,130,238)},
806 {"plum" , PALETTERGB (221,160,221)},
807 {"orchid" , PALETTERGB (218,112,214)},
808 {"medium orchid" , PALETTERGB (186, 85,211)},
809 {"MediumOrchid" , PALETTERGB (186, 85,211)},
810 {"dark orchid" , PALETTERGB (153, 50,204)},
811 {"DarkOrchid" , PALETTERGB (153, 50,204)},
812 {"dark violet" , PALETTERGB (148, 0,211)},
813 {"DarkViolet" , PALETTERGB (148, 0,211)},
814 {"blue violet" , PALETTERGB (138, 43,226)},
815 {"BlueViolet" , PALETTERGB (138, 43,226)},
816 {"purple" , PALETTERGB (160, 32,240)},
817 {"medium purple" , PALETTERGB (147,112,219)},
818 {"MediumPurple" , PALETTERGB (147,112,219)},
819 {"thistle" , PALETTERGB (216,191,216)},
820 {"gray0" , PALETTERGB ( 0, 0, 0)},
821 {"grey0" , PALETTERGB ( 0, 0, 0)},
822 {"dark grey" , PALETTERGB (169,169,169)},
823 {"DarkGrey" , PALETTERGB (169,169,169)},
824 {"dark gray" , PALETTERGB (169,169,169)},
825 {"DarkGray" , PALETTERGB (169,169,169)},
826 {"dark blue" , PALETTERGB ( 0, 0,139)},
827 {"DarkBlue" , PALETTERGB ( 0, 0,139)},
828 {"dark cyan" , PALETTERGB ( 0,139,139)},
829 {"DarkCyan" , PALETTERGB ( 0,139,139)},
830 {"dark magenta" , PALETTERGB (139, 0,139)},
831 {"DarkMagenta" , PALETTERGB (139, 0,139)},
832 {"dark red" , PALETTERGB (139, 0, 0)},
833 {"DarkRed" , PALETTERGB (139, 0, 0)},
834 {"light green" , PALETTERGB (144,238,144)},
835 {"LightGreen" , PALETTERGB (144,238,144)},
836};
837
838DEFUN ("w32-default-color-map", Fw32_default_color_map, Sw32_default_color_map,
839 0, 0, 0, doc: /* Return the default color map. */)
840 ()
841{
842 int i;
843 colormap_t *pc = w32_color_map;
844 Lisp_Object cmap;
845
846 BLOCK_INPUT;
847
848 cmap = Qnil;
849
850 for (i = 0; i < sizeof (w32_color_map) / sizeof (w32_color_map[0]);
851 pc++, i++)
852 cmap = Fcons (Fcons (build_string (pc->name),
853 make_number (pc->colorref)),
854 cmap);
855
856 UNBLOCK_INPUT;
857
858 return (cmap);
859}
860
861static Lisp_Object
862w32_to_x_color (rgb)
863 Lisp_Object rgb;
864{
865 Lisp_Object color;
866
867 CHECK_NUMBER (rgb);
868
869 BLOCK_INPUT;
870
871 color = Frassq (rgb, Vw32_color_map);
872
873 UNBLOCK_INPUT;
874
875 if (!NILP (color))
876 return (Fcar (color));
877 else
878 return Qnil;
879}
880
881static Lisp_Object
882w32_color_map_lookup (colorname)
883 char *colorname;
884{
885 Lisp_Object tail, ret = Qnil;
886
887 BLOCK_INPUT;
888
889 for (tail = Vw32_color_map; CONSP (tail); tail = XCDR (tail))
890 {
891 register Lisp_Object elt, tem;
892
893 elt = XCAR (tail);
894 if (!CONSP (elt)) continue;
895
896 tem = Fcar (elt);
897
898 if (lstrcmpi (SDATA (tem), colorname) == 0)
899 {
900 ret = Fcdr (elt);
901 break;
902 }
903
904 QUIT;
905 }
906
907
908 UNBLOCK_INPUT;
909
910 return ret;
911}
912
913
914static void
915add_system_logical_colors_to_map (system_colors)
916 Lisp_Object *system_colors;
917{
918 HKEY colors_key;
919
920 /* Other registry operations are done with input blocked. */
921 BLOCK_INPUT;
922
923 /* Look for "Control Panel/Colors" under User and Machine registry
924 settings. */
925 if (RegOpenKeyEx (HKEY_CURRENT_USER, "Control Panel\\Colors", 0,
926 KEY_READ, &colors_key) == ERROR_SUCCESS
927 || RegOpenKeyEx (HKEY_LOCAL_MACHINE, "Control Panel\\Colors", 0,
928 KEY_READ, &colors_key) == ERROR_SUCCESS)
929 {
930 /* List all keys. */
931 char color_buffer[64];
932 char full_name_buffer[MAX_PATH + SYSTEM_COLOR_PREFIX_LEN];
933 int index = 0;
934 DWORD name_size, color_size;
935 char *name_buffer = full_name_buffer + SYSTEM_COLOR_PREFIX_LEN;
936
937 name_size = sizeof (full_name_buffer) - SYSTEM_COLOR_PREFIX_LEN;
938 color_size = sizeof (color_buffer);
939
940 strcpy (full_name_buffer, SYSTEM_COLOR_PREFIX);
941
942 while (RegEnumValueA (colors_key, index, name_buffer, &name_size,
943 NULL, NULL, color_buffer, &color_size)
944 == ERROR_SUCCESS)
945 {
946 int r, g, b;
947 if (sscanf (color_buffer, " %u %u %u", &r, &g, &b) == 3)
948 *system_colors = Fcons (Fcons (build_string (full_name_buffer),
949 make_number (RGB (r, g, b))),
950 *system_colors);
951
952 name_size = sizeof (full_name_buffer) - SYSTEM_COLOR_PREFIX_LEN;
953 color_size = sizeof (color_buffer);
954 index++;
955 }
956 RegCloseKey (colors_key);
957 }
958
959 UNBLOCK_INPUT;
960}
961
962
963static Lisp_Object
964x_to_w32_color (colorname)
965 char * colorname;
966{
967 register Lisp_Object ret = Qnil;
968
969 BLOCK_INPUT;
970
971 if (colorname[0] == '#')
972 {
973 /* Could be an old-style RGB Device specification. */
974 char *color;
975 int size;
976 color = colorname + 1;
977
978 size = strlen (color);
979 if (size == 3 || size == 6 || size == 9 || size == 12)
980 {
981 UINT colorval;
982 int i, pos;
983 pos = 0;
984 size /= 3;
985 colorval = 0;
986
987 for (i = 0; i < 3; i++)
988 {
989 char *end;
990 char t;
991 unsigned long value;
992
993 /* The check for 'x' in the following conditional takes into
994 account the fact that strtol allows a "0x" in front of
995 our numbers, and we don't. */
996 if (!isxdigit (color[0]) || color[1] == 'x')
997 break;
998 t = color[size];
999 color[size] = '\0';
1000 value = strtoul (color, &end, 16);
1001 color[size] = t;
1002 if (errno == ERANGE || end - color != size)
1003 break;
1004 switch (size)
1005 {
1006 case 1:
1007 value = value * 0x10;
1008 break;
1009 case 2:
1010 break;
1011 case 3:
1012 value /= 0x10;
1013 break;
1014 case 4:
1015 value /= 0x100;
1016 break;
1017 }
1018 colorval |= (value << pos);
1019 pos += 0x8;
1020 if (i == 2)
1021 {
1022 UNBLOCK_INPUT;
1023 XSETINT (ret, colorval);
1024 return ret;
1025 }
1026 color = end;
1027 }
1028 }
1029 }
1030 else if (strnicmp (colorname, "rgb:", 4) == 0)
1031 {
1032 char *color;
1033 UINT colorval;
1034 int i, pos;
1035 pos = 0;
1036
1037 colorval = 0;
1038 color = colorname + 4;
1039 for (i = 0; i < 3; i++)
1040 {
1041 char *end;
1042 unsigned long value;
1043
1044 /* The check for 'x' in the following conditional takes into
1045 account the fact that strtol allows a "0x" in front of
1046 our numbers, and we don't. */
1047 if (!isxdigit (color[0]) || color[1] == 'x')
1048 break;
1049 value = strtoul (color, &end, 16);
1050 if (errno == ERANGE)
1051 break;
1052 switch (end - color)
1053 {
1054 case 1:
1055 value = value * 0x10 + value;
1056 break;
1057 case 2:
1058 break;
1059 case 3:
1060 value /= 0x10;
1061 break;
1062 case 4:
1063 value /= 0x100;
1064 break;
1065 default:
1066 value = ULONG_MAX;
1067 }
1068 if (value == ULONG_MAX)
1069 break;
1070 colorval |= (value << pos);
1071 pos += 0x8;
1072 if (i == 2)
1073 {
1074 if (*end != '\0')
1075 break;
1076 UNBLOCK_INPUT;
1077 XSETINT (ret, colorval);
1078 return ret;
1079 }
1080 if (*end != '/')
1081 break;
1082 color = end + 1;
1083 }
1084 }
1085 else if (strnicmp (colorname, "rgbi:", 5) == 0)
1086 {
1087 /* This is an RGB Intensity specification. */
1088 char *color;
1089 UINT colorval;
1090 int i, pos;
1091 pos = 0;
1092
1093 colorval = 0;
1094 color = colorname + 5;
1095 for (i = 0; i < 3; i++)
1096 {
1097 char *end;
1098 double value;
1099 UINT val;
1100
1101 value = strtod (color, &end);
1102 if (errno == ERANGE)
1103 break;
1104 if (value < 0.0 || value > 1.0)
1105 break;
1106 val = (UINT)(0x100 * value);
1107 /* We used 0x100 instead of 0xFF to give a continuous
1108 range between 0.0 and 1.0 inclusive. The next statement
1109 fixes the 1.0 case. */
1110 if (val == 0x100)
1111 val = 0xFF;
1112 colorval |= (val << pos);
1113 pos += 0x8;
1114 if (i == 2)
1115 {
1116 if (*end != '\0')
1117 break;
1118 UNBLOCK_INPUT;
1119 XSETINT (ret, colorval);
1120 return ret;
1121 }
1122 if (*end != '/')
1123 break;
1124 color = end + 1;
1125 }
1126 }
1127 /* I am not going to attempt to handle any of the CIE color schemes
1128 or TekHVC, since I don't know the algorithms for conversion to
1129 RGB. */
1130
1131 /* If we fail to lookup the color name in w32_color_map, then check the
1132 colorname to see if it can be crudely approximated: If the X color
1133 ends in a number (e.g., "darkseagreen2"), strip the number and
1134 return the result of looking up the base color name. */
1135 ret = w32_color_map_lookup (colorname);
1136 if (NILP (ret))
1137 {
1138 int len = strlen (colorname);
1139
1140 if (isdigit (colorname[len - 1]))
1141 {
1142 char *ptr, *approx = alloca (len + 1);
1143
1144 strcpy (approx, colorname);
1145 ptr = &approx[len - 1];
1146 while (ptr > approx && isdigit (*ptr))
1147 *ptr-- = '\0';
1148
1149 ret = w32_color_map_lookup (approx);
1150 }
1151 }
1152
1153 UNBLOCK_INPUT;
1154 return ret;
1155}
1156
1157void
1158w32_regenerate_palette (FRAME_PTR f)
1159{
1160 struct w32_palette_entry * list;
1161 LOGPALETTE * log_palette;
1162 HPALETTE new_palette;
1163 int i;
1164
1165 /* don't bother trying to create palette if not supported */
1166 if (! FRAME_W32_DISPLAY_INFO (f)->has_palette)
1167 return;
1168
1169 log_palette = (LOGPALETTE *)
1170 alloca (sizeof (LOGPALETTE) +
1171 FRAME_W32_DISPLAY_INFO (f)->num_colors * sizeof (PALETTEENTRY));
1172 log_palette->palVersion = 0x300;
1173 log_palette->palNumEntries = FRAME_W32_DISPLAY_INFO (f)->num_colors;
1174
1175 list = FRAME_W32_DISPLAY_INFO (f)->color_list;
1176 for (i = 0;
1177 i < FRAME_W32_DISPLAY_INFO (f)->num_colors;
1178 i++, list = list->next)
1179 log_palette->palPalEntry[i] = list->entry;
1180
1181 new_palette = CreatePalette (log_palette);
1182
1183 enter_crit ();
1184
1185 if (FRAME_W32_DISPLAY_INFO (f)->palette)
1186 DeleteObject (FRAME_W32_DISPLAY_INFO (f)->palette);
1187 FRAME_W32_DISPLAY_INFO (f)->palette = new_palette;
1188
1189 /* Realize display palette and garbage all frames. */
1190 release_frame_dc (f, get_frame_dc (f));
1191
1192 leave_crit ();
1193}
1194
1195#define W32_COLOR(pe) RGB (pe.peRed, pe.peGreen, pe.peBlue)
1196#define SET_W32_COLOR(pe, color) \
1197 do \
1198 { \
1199 pe.peRed = GetRValue (color); \
1200 pe.peGreen = GetGValue (color); \
1201 pe.peBlue = GetBValue (color); \
1202 pe.peFlags = 0; \
1203 } while (0)
1204
1205#if 0
1206/* Keep these around in case we ever want to track color usage. */
1207void
1208w32_map_color (FRAME_PTR f, COLORREF color)
1209{
1210 struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
1211
1212 if (NILP (Vw32_enable_palette))
1213 return;
1214
1215 /* check if color is already mapped */
1216 while (list)
1217 {
1218 if (W32_COLOR (list->entry) == color)
1219 {
1220 ++list->refcount;
1221 return;
1222 }
1223 list = list->next;
1224 }
1225
1226 /* not already mapped, so add to list and recreate Windows palette */
1227 list = (struct w32_palette_entry *)
1228 xmalloc (sizeof (struct w32_palette_entry));
1229 SET_W32_COLOR (list->entry, color);
1230 list->refcount = 1;
1231 list->next = FRAME_W32_DISPLAY_INFO (f)->color_list;
1232 FRAME_W32_DISPLAY_INFO (f)->color_list = list;
1233 FRAME_W32_DISPLAY_INFO (f)->num_colors++;
1234
1235 /* set flag that palette must be regenerated */
1236 FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
1237}
1238
1239void
1240w32_unmap_color (FRAME_PTR f, COLORREF color)
1241{
1242 struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
1243 struct w32_palette_entry **prev = &FRAME_W32_DISPLAY_INFO (f)->color_list;
1244
1245 if (NILP (Vw32_enable_palette))
1246 return;
1247
1248 /* check if color is already mapped */
1249 while (list)
1250 {
1251 if (W32_COLOR (list->entry) == color)
1252 {
1253 if (--list->refcount == 0)
1254 {
1255 *prev = list->next;
1256 xfree (list);
1257 FRAME_W32_DISPLAY_INFO (f)->num_colors--;
1258 break;
1259 }
1260 else
1261 return;
1262 }
1263 prev = &list->next;
1264 list = list->next;
1265 }
1266
1267 /* set flag that palette must be regenerated */
1268 FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
1269}
1270#endif
1271
1272
1273/* Gamma-correct COLOR on frame F. */
1274
1275void
1276gamma_correct (f, color)
1277 struct frame *f;
1278 COLORREF *color;
1279{
1280 if (f->gamma)
1281 {
1282 *color = PALETTERGB (
1283 pow (GetRValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
1284 pow (GetGValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
1285 pow (GetBValue (*color) / 255.0, f->gamma) * 255.0 + 0.5);
1286 }
1287}
1288
1289
1290/* Decide if color named COLOR is valid for the display associated with
1291 the selected frame; if so, return the rgb values in COLOR_DEF.
1292 If ALLOC is nonzero, allocate a new colormap cell. */
1293
1294int
1295w32_defined_color (f, color, color_def, alloc)
1296 FRAME_PTR f;
1297 char *color;
1298 XColor *color_def;
1299 int alloc;
1300{
1301 register Lisp_Object tem;
1302 COLORREF w32_color_ref;
1303
1304 tem = x_to_w32_color (color);
1305
1306 if (!NILP (tem))
1307 {
1308 if (f)
1309 {
1310 /* Apply gamma correction. */
1311 w32_color_ref = XUINT (tem);
1312 gamma_correct (f, &w32_color_ref);
1313 XSETINT (tem, w32_color_ref);
1314 }
1315
1316 /* Map this color to the palette if it is enabled. */
1317 if (!NILP (Vw32_enable_palette))
1318 {
1319 struct w32_palette_entry * entry =
1320 one_w32_display_info.color_list;
1321 struct w32_palette_entry ** prev =
1322 &one_w32_display_info.color_list;
1323
1324 /* check if color is already mapped */
1325 while (entry)
1326 {
1327 if (W32_COLOR (entry->entry) == XUINT (tem))
1328 break;
1329 prev = &entry->next;
1330 entry = entry->next;
1331 }
1332
1333 if (entry == NULL && alloc)
1334 {
1335 /* not already mapped, so add to list */
1336 entry = (struct w32_palette_entry *)
1337 xmalloc (sizeof (struct w32_palette_entry));
1338 SET_W32_COLOR (entry->entry, XUINT (tem));
1339 entry->next = NULL;
1340 *prev = entry;
1341 one_w32_display_info.num_colors++;
1342
1343 /* set flag that palette must be regenerated */
1344 one_w32_display_info.regen_palette = TRUE;
1345 }
1346 }
1347 /* Ensure COLORREF value is snapped to nearest color in (default)
1348 palette by simulating the PALETTERGB macro. This works whether
1349 or not the display device has a palette. */
1350 w32_color_ref = XUINT (tem) | 0x2000000;
1351
1352 color_def->pixel = w32_color_ref;
1353 color_def->red = GetRValue (w32_color_ref) * 256;
1354 color_def->green = GetGValue (w32_color_ref) * 256;
1355 color_def->blue = GetBValue (w32_color_ref) * 256;
1356
1357 return 1;
1358 }
1359 else
1360 {
1361 return 0;
1362 }
1363}
1364
1365/* Given a string ARG naming a color, compute a pixel value from it
1366 suitable for screen F.
1367 If F is not a color screen, return DEF (default) regardless of what
1368 ARG says. */
1369
1370int
1371x_decode_color (f, arg, def)
1372 FRAME_PTR f;
1373 Lisp_Object arg;
1374 int def;
1375{
1376 XColor cdef;
1377
1378 CHECK_STRING (arg);
1379
1380 if (strcmp (SDATA (arg), "black") == 0)
1381 return BLACK_PIX_DEFAULT (f);
1382 else if (strcmp (SDATA (arg), "white") == 0)
1383 return WHITE_PIX_DEFAULT (f);
1384
1385 if ((FRAME_W32_DISPLAY_INFO (f)->n_planes * FRAME_W32_DISPLAY_INFO (f)->n_cbits) == 1)
1386 return def;
1387
1388 /* w32_defined_color is responsible for coping with failures
1389 by looking for a near-miss. */
1390 if (w32_defined_color (f, SDATA (arg), &cdef, 1))
1391 return cdef.pixel;
1392
1393 /* defined_color failed; return an ultimate default. */
1394 return def;
1395}
1396\f
1397
1398
1399/* Functions called only from `x_set_frame_param'
1400 to set individual parameters.
1401
1402 If FRAME_W32_WINDOW (f) is 0,
1403 the frame is being created and its window does not exist yet.
1404 In that case, just record the parameter's new value
1405 in the standard place; do not attempt to change the window. */
1406
1407void
1408x_set_foreground_color (f, arg, oldval)
1409 struct frame *f;
1410 Lisp_Object arg, oldval;
1411{
1412 struct w32_output *x = f->output_data.w32;
1413 PIX_TYPE fg, old_fg;
1414
1415 fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1416 old_fg = FRAME_FOREGROUND_PIXEL (f);
1417 FRAME_FOREGROUND_PIXEL (f) = fg;
1418
1419 if (FRAME_W32_WINDOW (f) != 0)
1420 {
1421 if (x->cursor_pixel == old_fg)
1422 x->cursor_pixel = fg;
1423
1424 update_face_from_frame_parameter (f, Qforeground_color, arg);
1425 if (FRAME_VISIBLE_P (f))
1426 redraw_frame (f);
1427 }
1428}
1429
1430void
1431x_set_background_color (f, arg, oldval)
1432 struct frame *f;
1433 Lisp_Object arg, oldval;
1434{
1435 FRAME_BACKGROUND_PIXEL (f)
1436 = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
1437
1438 if (FRAME_W32_WINDOW (f) != 0)
1439 {
1440 SetWindowLong (FRAME_W32_WINDOW (f), WND_BACKGROUND_INDEX,
1441 FRAME_BACKGROUND_PIXEL (f));
1442
1443 update_face_from_frame_parameter (f, Qbackground_color, arg);
1444
1445 if (FRAME_VISIBLE_P (f))
1446 redraw_frame (f);
1447 }
1448}
1449
1450void
1451x_set_mouse_color (f, arg, oldval)
1452 struct frame *f;
1453 Lisp_Object arg, oldval;
1454{
1455 Cursor cursor, nontext_cursor, mode_cursor, hand_cursor;
1456 int count;
1457 int mask_color;
1458
1459 if (!EQ (Qnil, arg))
1460 f->output_data.w32->mouse_pixel
1461 = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1462 mask_color = FRAME_BACKGROUND_PIXEL (f);
1463
1464 /* Don't let pointers be invisible. */
1465 if (mask_color == f->output_data.w32->mouse_pixel
1466 && mask_color == FRAME_BACKGROUND_PIXEL (f))
1467 f->output_data.w32->mouse_pixel = FRAME_FOREGROUND_PIXEL (f);
1468
1469#if 0 /* TODO : cursor changes */
1470 BLOCK_INPUT;
1471
1472 /* It's not okay to crash if the user selects a screwy cursor. */
1473 count = x_catch_errors (FRAME_W32_DISPLAY (f));
1474
1475 if (!EQ (Qnil, Vx_pointer_shape))
1476 {
1477 CHECK_NUMBER (Vx_pointer_shape);
1478 cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XINT (Vx_pointer_shape));
1479 }
1480 else
1481 cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
1482 x_check_errors (FRAME_W32_DISPLAY (f), "bad text pointer cursor: %s");
1483
1484 if (!EQ (Qnil, Vx_nontext_pointer_shape))
1485 {
1486 CHECK_NUMBER (Vx_nontext_pointer_shape);
1487 nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1488 XINT (Vx_nontext_pointer_shape));
1489 }
1490 else
1491 nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_left_ptr);
1492 x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
1493
1494 if (!EQ (Qnil, Vx_hourglass_pointer_shape))
1495 {
1496 CHECK_NUMBER (Vx_hourglass_pointer_shape);
1497 hourglass_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1498 XINT (Vx_hourglass_pointer_shape));
1499 }
1500 else
1501 hourglass_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_watch);
1502 x_check_errors (FRAME_W32_DISPLAY (f), "bad busy pointer cursor: %s");
1503
1504 x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
1505 if (!EQ (Qnil, Vx_mode_pointer_shape))
1506 {
1507 CHECK_NUMBER (Vx_mode_pointer_shape);
1508 mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1509 XINT (Vx_mode_pointer_shape));
1510 }
1511 else
1512 mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
1513 x_check_errors (FRAME_W32_DISPLAY (f), "bad modeline pointer cursor: %s");
1514
1515 if (!EQ (Qnil, Vx_sensitive_text_pointer_shape))
1516 {
1517 CHECK_NUMBER (Vx_sensitive_text_pointer_shape);
1518 hand_cursor
1519 = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1520 XINT (Vx_sensitive_text_pointer_shape));
1521 }
1522 else
1523 hand_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_crosshair);
1524
1525 if (!NILP (Vx_window_horizontal_drag_shape))
1526 {
1527 CHECK_NUMBER (Vx_window_horizontal_drag_shape);
1528 horizontal_drag_cursor
1529 = XCreateFontCursor (FRAME_X_DISPLAY (f),
1530 XINT (Vx_window_horizontal_drag_shape));
1531 }
1532 else
1533 horizontal_drag_cursor
1534 = XCreateFontCursor (FRAME_X_DISPLAY (f), XC_sb_h_double_arrow);
1535
1536 /* Check and report errors with the above calls. */
1537 x_check_errors (FRAME_W32_DISPLAY (f), "can't set cursor shape: %s");
1538 x_uncatch_errors (FRAME_W32_DISPLAY (f), count);
1539
1540 {
1541 XColor fore_color, back_color;
1542
1543 fore_color.pixel = f->output_data.w32->mouse_pixel;
1544 back_color.pixel = mask_color;
1545 XQueryColor (FRAME_W32_DISPLAY (f),
1546 DefaultColormap (FRAME_W32_DISPLAY (f),
1547 DefaultScreen (FRAME_W32_DISPLAY (f))),
1548 &fore_color);
1549 XQueryColor (FRAME_W32_DISPLAY (f),
1550 DefaultColormap (FRAME_W32_DISPLAY (f),
1551 DefaultScreen (FRAME_W32_DISPLAY (f))),
1552 &back_color);
1553 XRecolorCursor (FRAME_W32_DISPLAY (f), cursor,
1554 &fore_color, &back_color);
1555 XRecolorCursor (FRAME_W32_DISPLAY (f), nontext_cursor,
1556 &fore_color, &back_color);
1557 XRecolorCursor (FRAME_W32_DISPLAY (f), mode_cursor,
1558 &fore_color, &back_color);
1559 XRecolorCursor (FRAME_W32_DISPLAY (f), hand_cursor,
1560 &fore_color, &back_color);
1561 XRecolorCursor (FRAME_W32_DISPLAY (f), hourglass_cursor,
1562 &fore_color, &back_color);
1563 }
1564
1565 if (FRAME_W32_WINDOW (f) != 0)
1566 XDefineCursor (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), cursor);
1567
1568 if (cursor != f->output_data.w32->text_cursor && f->output_data.w32->text_cursor != 0)
1569 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->text_cursor);
1570 f->output_data.w32->text_cursor = cursor;
1571
1572 if (nontext_cursor != f->output_data.w32->nontext_cursor
1573 && f->output_data.w32->nontext_cursor != 0)
1574 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->nontext_cursor);
1575 f->output_data.w32->nontext_cursor = nontext_cursor;
1576
1577 if (hourglass_cursor != f->output_data.w32->hourglass_cursor
1578 && f->output_data.w32->hourglass_cursor != 0)
1579 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->hourglass_cursor);
1580 f->output_data.w32->hourglass_cursor = hourglass_cursor;
1581
1582 if (mode_cursor != f->output_data.w32->modeline_cursor
1583 && f->output_data.w32->modeline_cursor != 0)
1584 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->modeline_cursor);
1585 f->output_data.w32->modeline_cursor = mode_cursor;
1586
1587 if (hand_cursor != f->output_data.w32->hand_cursor
1588 && f->output_data.w32->hand_cursor != 0)
1589 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->hand_cursor);
1590 f->output_data.w32->hand_cursor = hand_cursor;
1591
1592 XFlush (FRAME_W32_DISPLAY (f));
1593 UNBLOCK_INPUT;
1594
1595 update_face_from_frame_parameter (f, Qmouse_color, arg);
1596#endif /* TODO */
1597}
1598
1599void
1600x_set_cursor_color (f, arg, oldval)
1601 struct frame *f;
1602 Lisp_Object arg, oldval;
1603{
1604 unsigned long fore_pixel, pixel;
1605
1606 if (!NILP (Vx_cursor_fore_pixel))
1607 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
1608 WHITE_PIX_DEFAULT (f));
1609 else
1610 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
1611
1612 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1613
1614 /* Make sure that the cursor color differs from the background color. */
1615 if (pixel == FRAME_BACKGROUND_PIXEL (f))
1616 {
1617 pixel = f->output_data.w32->mouse_pixel;
1618 if (pixel == fore_pixel)
1619 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
1620 }
1621
1622 f->output_data.w32->cursor_foreground_pixel = fore_pixel;
1623 f->output_data.w32->cursor_pixel = pixel;
1624
1625 if (FRAME_W32_WINDOW (f) != 0)
1626 {
1627 BLOCK_INPUT;
1628 /* Update frame's cursor_gc. */
1629 f->output_data.w32->cursor_gc->foreground = fore_pixel;
1630 f->output_data.w32->cursor_gc->background = pixel;
1631
1632 UNBLOCK_INPUT;
1633
1634 if (FRAME_VISIBLE_P (f))
1635 {
1636 x_update_cursor (f, 0);
1637 x_update_cursor (f, 1);
1638 }
1639 }
1640
1641 update_face_from_frame_parameter (f, Qcursor_color, arg);
1642}
1643
1644/* Set the border-color of frame F to pixel value PIX.
1645 Note that this does not fully take effect if done before
1646 F has a window. */
1647
1648void
1649x_set_border_pixel (f, pix)
1650 struct frame *f;
1651 int pix;
1652{
1653
1654 f->output_data.w32->border_pixel = pix;
1655
1656 if (FRAME_W32_WINDOW (f) != 0 && f->border_width > 0)
1657 {
1658 if (FRAME_VISIBLE_P (f))
1659 redraw_frame (f);
1660 }
1661}
1662
1663/* Set the border-color of frame F to value described by ARG.
1664 ARG can be a string naming a color.
1665 The border-color is used for the border that is drawn by the server.
1666 Note that this does not fully take effect if done before
1667 F has a window; it must be redone when the window is created. */
1668
1669void
1670x_set_border_color (f, arg, oldval)
1671 struct frame *f;
1672 Lisp_Object arg, oldval;
1673{
1674 int pix;
1675
1676 CHECK_STRING (arg);
1677 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1678 x_set_border_pixel (f, pix);
1679 update_face_from_frame_parameter (f, Qborder_color, arg);
1680}
1681
1682
1683void
1684x_set_cursor_type (f, arg, oldval)
1685 FRAME_PTR f;
1686 Lisp_Object arg, oldval;
1687{
1688 set_frame_cursor_types (f, arg);
1689
1690 /* Make sure the cursor gets redrawn. */
1691 cursor_type_changed = 1;
1692}
1693\f
1694void
1695x_set_icon_type (f, arg, oldval)
1696 struct frame *f;
1697 Lisp_Object arg, oldval;
1698{
1699 int result;
1700
1701 if (NILP (arg) && NILP (oldval))
1702 return;
1703
1704 if (STRINGP (arg) && STRINGP (oldval)
1705 && EQ (Fstring_equal (oldval, arg), Qt))
1706 return;
1707
1708 if (SYMBOLP (arg) && SYMBOLP (oldval) && EQ (arg, oldval))
1709 return;
1710
1711 BLOCK_INPUT;
1712
1713 result = x_bitmap_icon (f, arg);
1714 if (result)
1715 {
1716 UNBLOCK_INPUT;
1717 error ("No icon window available");
1718 }
1719
1720 UNBLOCK_INPUT;
1721}
1722
1723void
1724x_set_icon_name (f, arg, oldval)
1725 struct frame *f;
1726 Lisp_Object arg, oldval;
1727{
1728 if (STRINGP (arg))
1729 {
1730 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1731 return;
1732 }
1733 else if (!NILP (arg) || NILP (oldval))
1734 return;
1735
1736 f->icon_name = arg;
1737
1738#if 0
1739 if (f->output_data.w32->icon_bitmap != 0)
1740 return;
1741
1742 BLOCK_INPUT;
1743
1744 result = x_text_icon (f,
1745 (char *) SDATA ((!NILP (f->icon_name)
1746 ? f->icon_name
1747 : !NILP (f->title)
1748 ? f->title
1749 : f->name)));
1750
1751 if (result)
1752 {
1753 UNBLOCK_INPUT;
1754 error ("No icon window available");
1755 }
1756
1757 /* If the window was unmapped (and its icon was mapped),
1758 the new icon is not mapped, so map the window in its stead. */
1759 if (FRAME_VISIBLE_P (f))
1760 {
1761#ifdef USE_X_TOOLKIT
1762 XtPopup (f->output_data.w32->widget, XtGrabNone);
1763#endif
1764 XMapWindow (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f));
1765 }
1766
1767 XFlush (FRAME_W32_DISPLAY (f));
1768 UNBLOCK_INPUT;
1769#endif
1770}
1771
1772\f
1773void
1774x_set_menu_bar_lines (f, value, oldval)
1775 struct frame *f;
1776 Lisp_Object value, oldval;
1777{
1778 int nlines;
1779 int olines = FRAME_MENU_BAR_LINES (f);
1780
1781 /* Right now, menu bars don't work properly in minibuf-only frames;
1782 most of the commands try to apply themselves to the minibuffer
1783 frame itself, and get an error because you can't switch buffers
1784 in or split the minibuffer window. */
1785 if (FRAME_MINIBUF_ONLY_P (f))
1786 return;
1787
1788 if (INTEGERP (value))
1789 nlines = XINT (value);
1790 else
1791 nlines = 0;
1792
1793 FRAME_MENU_BAR_LINES (f) = 0;
1794 if (nlines)
1795 FRAME_EXTERNAL_MENU_BAR (f) = 1;
1796 else
1797 {
1798 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
1799 free_frame_menubar (f);
1800 FRAME_EXTERNAL_MENU_BAR (f) = 0;
1801
1802 /* Adjust the frame size so that the client (text) dimensions
1803 remain the same. This depends on FRAME_EXTERNAL_MENU_BAR being
1804 set correctly. */
1805 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
1806 do_pending_window_change (0);
1807 }
1808 adjust_glyphs (f);
1809}
1810
1811
1812/* Set the number of lines used for the tool bar of frame F to VALUE.
1813 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
1814 is the old number of tool bar lines. This function changes the
1815 height of all windows on frame F to match the new tool bar height.
1816 The frame's height doesn't change. */
1817
1818void
1819x_set_tool_bar_lines (f, value, oldval)
1820 struct frame *f;
1821 Lisp_Object value, oldval;
1822{
1823 int delta, nlines, root_height;
1824 Lisp_Object root_window;
1825
1826 /* Treat tool bars like menu bars. */
1827 if (FRAME_MINIBUF_ONLY_P (f))
1828 return;
1829
1830 /* Use VALUE only if an integer >= 0. */
1831 if (INTEGERP (value) && XINT (value) >= 0)
1832 nlines = XFASTINT (value);
1833 else
1834 nlines = 0;
1835
1836 /* Make sure we redisplay all windows in this frame. */
1837 ++windows_or_buffers_changed;
1838
1839 delta = nlines - FRAME_TOOL_BAR_LINES (f);
1840
1841 /* Don't resize the tool-bar to more than we have room for. */
1842 root_window = FRAME_ROOT_WINDOW (f);
1843 root_height = WINDOW_TOTAL_LINES (XWINDOW (root_window));
1844 if (root_height - delta < 1)
1845 {
1846 delta = root_height - 1;
1847 nlines = FRAME_TOOL_BAR_LINES (f) + delta;
1848 }
1849
1850 FRAME_TOOL_BAR_LINES (f) = nlines;
1851 change_window_heights (root_window, delta);
1852 adjust_glyphs (f);
1853
1854 /* We also have to make sure that the internal border at the top of
1855 the frame, below the menu bar or tool bar, is redrawn when the
1856 tool bar disappears. This is so because the internal border is
1857 below the tool bar if one is displayed, but is below the menu bar
1858 if there isn't a tool bar. The tool bar draws into the area
1859 below the menu bar. */
1860 if (FRAME_W32_WINDOW (f) && FRAME_TOOL_BAR_LINES (f) == 0)
1861 {
1862 clear_frame (f);
1863 clear_current_matrices (f);
1864 }
1865
1866 /* If the tool bar gets smaller, the internal border below it
1867 has to be cleared. It was formerly part of the display
1868 of the larger tool bar, and updating windows won't clear it. */
1869 if (delta < 0)
1870 {
1871 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
1872 int width = FRAME_PIXEL_WIDTH (f);
1873 int y = nlines * FRAME_LINE_HEIGHT (f);
1874
1875 BLOCK_INPUT;
1876 {
1877 HDC hdc = get_frame_dc (f);
1878 w32_clear_area (f, hdc, 0, y, width, height);
1879 release_frame_dc (f, hdc);
1880 }
1881 UNBLOCK_INPUT;
1882
1883 if (WINDOWP (f->tool_bar_window))
1884 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
1885 }
1886}
1887
1888
1889/* Change the name of frame F to NAME. If NAME is nil, set F's name to
1890 w32_id_name.
1891
1892 If EXPLICIT is non-zero, that indicates that lisp code is setting the
1893 name; if NAME is a string, set F's name to NAME and set
1894 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1895
1896 If EXPLICIT is zero, that indicates that Emacs redisplay code is
1897 suggesting a new name, which lisp code should override; if
1898 F->explicit_name is set, ignore the new name; otherwise, set it. */
1899
1900void
1901x_set_name (f, name, explicit)
1902 struct frame *f;
1903 Lisp_Object name;
1904 int explicit;
1905{
1906 /* Make sure that requests from lisp code override requests from
1907 Emacs redisplay code. */
1908 if (explicit)
1909 {
1910 /* If we're switching from explicit to implicit, we had better
1911 update the mode lines and thereby update the title. */
1912 if (f->explicit_name && NILP (name))
1913 update_mode_lines = 1;
1914
1915 f->explicit_name = ! NILP (name);
1916 }
1917 else if (f->explicit_name)
1918 return;
1919
1920 /* If NAME is nil, set the name to the w32_id_name. */
1921 if (NILP (name))
1922 {
1923 /* Check for no change needed in this very common case
1924 before we do any consing. */
1925 if (!strcmp (FRAME_W32_DISPLAY_INFO (f)->w32_id_name,
1926 SDATA (f->name)))
1927 return;
1928 name = build_string (FRAME_W32_DISPLAY_INFO (f)->w32_id_name);
1929 }
1930 else
1931 CHECK_STRING (name);
1932
1933 /* Don't change the name if it's already NAME. */
1934 if (! NILP (Fstring_equal (name, f->name)))
1935 return;
1936
1937 f->name = name;
1938
1939 /* For setting the frame title, the title parameter should override
1940 the name parameter. */
1941 if (! NILP (f->title))
1942 name = f->title;
1943
1944 if (FRAME_W32_WINDOW (f))
1945 {
1946 if (STRING_MULTIBYTE (name))
1947 name = ENCODE_SYSTEM (name);
1948
1949 BLOCK_INPUT;
1950 SetWindowText (FRAME_W32_WINDOW (f), SDATA (name));
1951 UNBLOCK_INPUT;
1952 }
1953}
1954
1955/* This function should be called when the user's lisp code has
1956 specified a name for the frame; the name will override any set by the
1957 redisplay code. */
1958void
1959x_explicitly_set_name (f, arg, oldval)
1960 FRAME_PTR f;
1961 Lisp_Object arg, oldval;
1962{
1963 x_set_name (f, arg, 1);
1964}
1965
1966/* This function should be called by Emacs redisplay code to set the
1967 name; names set this way will never override names set by the user's
1968 lisp code. */
1969void
1970x_implicitly_set_name (f, arg, oldval)
1971 FRAME_PTR f;
1972 Lisp_Object arg, oldval;
1973{
1974 x_set_name (f, arg, 0);
1975}
1976\f
1977/* Change the title of frame F to NAME.
1978 If NAME is nil, use the frame name as the title. */
1979
1980void
1981x_set_title (f, name, old_name)
1982 struct frame *f;
1983 Lisp_Object name, old_name;
1984{
1985 /* Don't change the title if it's already NAME. */
1986 if (EQ (name, f->title))
1987 return;
1988
1989 update_mode_lines = 1;
1990
1991 f->title = name;
1992
1993 if (NILP (name))
1994 name = f->name;
1995
1996 if (FRAME_W32_WINDOW (f))
1997 {
1998 if (STRING_MULTIBYTE (name))
1999 name = ENCODE_SYSTEM (name);
2000
2001 BLOCK_INPUT;
2002 SetWindowText (FRAME_W32_WINDOW (f), SDATA (name));
2003 UNBLOCK_INPUT;
2004 }
2005}
2006
2007
2008void x_set_scroll_bar_default_width (f)
2009 struct frame *f;
2010{
2011 int wid = FRAME_COLUMN_WIDTH (f);
2012
2013 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = GetSystemMetrics (SM_CXVSCROLL);
2014 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) +
2015 wid - 1) / wid;
2016}
2017
2018\f
2019/* Subroutines of creating a frame. */
2020
2021
2022/* Return the value of parameter PARAM.
2023
2024 First search ALIST, then Vdefault_frame_alist, then the X defaults
2025 database, using ATTRIBUTE as the attribute name and CLASS as its class.
2026
2027 Convert the resource to the type specified by desired_type.
2028
2029 If no default is specified, return Qunbound. If you call
2030 w32_get_arg, make sure you deal with Qunbound in a reasonable way,
2031 and don't let it get stored in any Lisp-visible variables! */
2032
2033static Lisp_Object
2034w32_get_arg (alist, param, attribute, class, type)
2035 Lisp_Object alist, param;
2036 char *attribute;
2037 char *class;
2038 enum resource_types type;
2039{
2040 return x_get_arg (check_x_display_info (Qnil),
2041 alist, param, attribute, class, type);
2042}
2043
2044\f
2045Cursor
2046w32_load_cursor (LPCTSTR name)
2047{
2048 /* Try first to load cursor from application resource. */
2049 Cursor cursor = LoadImage ((HINSTANCE) GetModuleHandle (NULL),
2050 name, IMAGE_CURSOR, 0, 0,
2051 LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_SHARED);
2052 if (!cursor)
2053 {
2054 /* Then try to load a shared predefined cursor. */
2055 cursor = LoadImage (NULL, name, IMAGE_CURSOR, 0, 0,
2056 LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_SHARED);
2057 }
2058 return cursor;
2059}
2060
2061extern LRESULT CALLBACK w32_wnd_proc ();
2062
2063static BOOL
2064w32_init_class (hinst)
2065 HINSTANCE hinst;
2066{
2067 WNDCLASS wc;
2068
2069 wc.style = CS_HREDRAW | CS_VREDRAW;
2070 wc.lpfnWndProc = (WNDPROC) w32_wnd_proc;
2071 wc.cbClsExtra = 0;
2072 wc.cbWndExtra = WND_EXTRA_BYTES;
2073 wc.hInstance = hinst;
2074 wc.hIcon = LoadIcon (hinst, EMACS_CLASS);
2075 wc.hCursor = w32_load_cursor (IDC_ARROW);
2076 wc.hbrBackground = NULL; /* GetStockObject (WHITE_BRUSH); */
2077 wc.lpszMenuName = NULL;
2078 wc.lpszClassName = EMACS_CLASS;
2079
2080 return (RegisterClass (&wc));
2081}
2082
2083static HWND
2084w32_createscrollbar (f, bar)
2085 struct frame *f;
2086 struct scroll_bar * bar;
2087{
2088 return (CreateWindow ("SCROLLBAR", "", SBS_VERT | WS_CHILD | WS_VISIBLE,
2089 /* Position and size of scroll bar. */
2090 XINT (bar->left) + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
2091 XINT (bar->top),
2092 XINT (bar->width) - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
2093 XINT (bar->height),
2094 FRAME_W32_WINDOW (f),
2095 NULL,
2096 hinst,
2097 NULL));
2098}
2099
2100static void
2101w32_createwindow (f)
2102 struct frame *f;
2103{
2104 HWND hwnd;
2105 RECT rect;
2106 Lisp_Object top = Qunbound;
2107 Lisp_Object left = Qunbound;
2108
2109 rect.left = rect.top = 0;
2110 rect.right = FRAME_PIXEL_WIDTH (f);
2111 rect.bottom = FRAME_PIXEL_HEIGHT (f);
2112
2113 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
2114 FRAME_EXTERNAL_MENU_BAR (f));
2115
2116 /* Do first time app init */
2117
2118 if (!hprevinst)
2119 {
2120 w32_init_class (hinst);
2121 }
2122
2123 if (f->size_hint_flags & USPosition || f->size_hint_flags & PPosition)
2124 {
2125 XSETINT (left, f->left_pos);
2126 XSETINT (top, f->top_pos);
2127 }
2128 else if (EQ (left, Qunbound) && EQ (top, Qunbound))
2129 {
2130 /* When called with RES_TYPE_NUMBER, w32_get_arg will return zero
2131 for anything that is not a number and is not Qunbound. */
2132 left = w32_get_arg (Qnil, Qleft, "left", "Left", RES_TYPE_NUMBER);
2133 top = w32_get_arg (Qnil, Qtop, "top", "Top", RES_TYPE_NUMBER);
2134 }
2135
2136 FRAME_W32_WINDOW (f) = hwnd
2137 = CreateWindow (EMACS_CLASS,
2138 f->namebuf,
2139 f->output_data.w32->dwStyle | WS_CLIPCHILDREN,
2140 EQ (left, Qunbound) ? CW_USEDEFAULT : XINT (left),
2141 EQ (top, Qunbound) ? CW_USEDEFAULT : XINT (top),
2142 rect.right - rect.left,
2143 rect.bottom - rect.top,
2144 NULL,
2145 NULL,
2146 hinst,
2147 NULL);
2148
2149 if (hwnd)
2150 {
2151 SetWindowLong (hwnd, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
2152 SetWindowLong (hwnd, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
2153 SetWindowLong (hwnd, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
2154 SetWindowLong (hwnd, WND_SCROLLBAR_INDEX, f->scroll_bar_actual_width);
2155 SetWindowLong (hwnd, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
2156
2157 /* Enable drag-n-drop. */
2158 DragAcceptFiles (hwnd, TRUE);
2159
2160 /* Do this to discard the default setting specified by our parent. */
2161 ShowWindow (hwnd, SW_HIDE);
2162
2163 /* Update frame positions. */
2164 GetWindowRect (hwnd, &rect);
2165 f->left_pos = rect.left;
2166 f->top_pos = rect.top;
2167 }
2168}
2169
2170static void
2171my_post_msg (wmsg, hwnd, msg, wParam, lParam)
2172 W32Msg * wmsg;
2173 HWND hwnd;
2174 UINT msg;
2175 WPARAM wParam;
2176 LPARAM lParam;
2177{
2178 wmsg->msg.hwnd = hwnd;
2179 wmsg->msg.message = msg;
2180 wmsg->msg.wParam = wParam;
2181 wmsg->msg.lParam = lParam;
2182 wmsg->msg.time = GetMessageTime ();
2183
2184 post_msg (wmsg);
2185}
2186
2187/* GetKeyState and MapVirtualKey on Windows 95 do not actually distinguish
2188 between left and right keys as advertised. We test for this
2189 support dynamically, and set a flag when the support is absent. If
2190 absent, we keep track of the left and right control and alt keys
2191 ourselves. This is particularly necessary on keyboards that rely
2192 upon the AltGr key, which is represented as having the left control
2193 and right alt keys pressed. For these keyboards, we need to know
2194 when the left alt key has been pressed in addition to the AltGr key
2195 so that we can properly support M-AltGr-key sequences (such as M-@
2196 on Swedish keyboards). */
2197
2198#define EMACS_LCONTROL 0
2199#define EMACS_RCONTROL 1
2200#define EMACS_LMENU 2
2201#define EMACS_RMENU 3
2202
2203static int modifiers[4];
2204static int modifiers_recorded;
2205static int modifier_key_support_tested;
2206
2207static void
2208test_modifier_support (unsigned int wparam)
2209{
2210 unsigned int l, r;
2211
2212 if (wparam != VK_CONTROL && wparam != VK_MENU)
2213 return;
2214 if (wparam == VK_CONTROL)
2215 {
2216 l = VK_LCONTROL;
2217 r = VK_RCONTROL;
2218 }
2219 else
2220 {
2221 l = VK_LMENU;
2222 r = VK_RMENU;
2223 }
2224 if (!(GetKeyState (l) & 0x8000) && !(GetKeyState (r) & 0x8000))
2225 modifiers_recorded = 1;
2226 else
2227 modifiers_recorded = 0;
2228 modifier_key_support_tested = 1;
2229}
2230
2231static void
2232record_keydown (unsigned int wparam, unsigned int lparam)
2233{
2234 int i;
2235
2236 if (!modifier_key_support_tested)
2237 test_modifier_support (wparam);
2238
2239 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
2240 return;
2241
2242 if (wparam == VK_CONTROL)
2243 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
2244 else
2245 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
2246
2247 modifiers[i] = 1;
2248}
2249
2250static void
2251record_keyup (unsigned int wparam, unsigned int lparam)
2252{
2253 int i;
2254
2255 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
2256 return;
2257
2258 if (wparam == VK_CONTROL)
2259 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
2260 else
2261 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
2262
2263 modifiers[i] = 0;
2264}
2265
2266/* Emacs can lose focus while a modifier key has been pressed. When
2267 it regains focus, be conservative and clear all modifiers since
2268 we cannot reconstruct the left and right modifier state. */
2269static void
2270reset_modifiers ()
2271{
2272 SHORT ctrl, alt;
2273
2274 if (GetFocus () == NULL)
2275 /* Emacs doesn't have keyboard focus. Do nothing. */
2276 return;
2277
2278 ctrl = GetAsyncKeyState (VK_CONTROL);
2279 alt = GetAsyncKeyState (VK_MENU);
2280
2281 if (!(ctrl & 0x08000))
2282 /* Clear any recorded control modifier state. */
2283 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
2284
2285 if (!(alt & 0x08000))
2286 /* Clear any recorded alt modifier state. */
2287 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
2288
2289 /* Update the state of all modifier keys, because modifiers used in
2290 hot-key combinations can get stuck on if Emacs loses focus as a
2291 result of a hot-key being pressed. */
2292 {
2293 BYTE keystate[256];
2294
2295#define CURRENT_STATE(key) ((GetAsyncKeyState (key) & 0x8000) >> 8)
2296
2297 GetKeyboardState (keystate);
2298 keystate[VK_SHIFT] = CURRENT_STATE (VK_SHIFT);
2299 keystate[VK_CONTROL] = CURRENT_STATE (VK_CONTROL);
2300 keystate[VK_LCONTROL] = CURRENT_STATE (VK_LCONTROL);
2301 keystate[VK_RCONTROL] = CURRENT_STATE (VK_RCONTROL);
2302 keystate[VK_MENU] = CURRENT_STATE (VK_MENU);
2303 keystate[VK_LMENU] = CURRENT_STATE (VK_LMENU);
2304 keystate[VK_RMENU] = CURRENT_STATE (VK_RMENU);
2305 keystate[VK_LWIN] = CURRENT_STATE (VK_LWIN);
2306 keystate[VK_RWIN] = CURRENT_STATE (VK_RWIN);
2307 keystate[VK_APPS] = CURRENT_STATE (VK_APPS);
2308 SetKeyboardState (keystate);
2309 }
2310}
2311
2312/* Synchronize modifier state with what is reported with the current
2313 keystroke. Even if we cannot distinguish between left and right
2314 modifier keys, we know that, if no modifiers are set, then neither
2315 the left or right modifier should be set. */
2316static void
2317sync_modifiers ()
2318{
2319 if (!modifiers_recorded)
2320 return;
2321
2322 if (!(GetKeyState (VK_CONTROL) & 0x8000))
2323 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
2324
2325 if (!(GetKeyState (VK_MENU) & 0x8000))
2326 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
2327}
2328
2329static int
2330modifier_set (int vkey)
2331{
2332 if (vkey == VK_CAPITAL || vkey == VK_SCROLL)
2333 return (GetKeyState (vkey) & 0x1);
2334 if (!modifiers_recorded)
2335 return (GetKeyState (vkey) & 0x8000);
2336
2337 switch (vkey)
2338 {
2339 case VK_LCONTROL:
2340 return modifiers[EMACS_LCONTROL];
2341 case VK_RCONTROL:
2342 return modifiers[EMACS_RCONTROL];
2343 case VK_LMENU:
2344 return modifiers[EMACS_LMENU];
2345 case VK_RMENU:
2346 return modifiers[EMACS_RMENU];
2347 }
2348 return (GetKeyState (vkey) & 0x8000);
2349}
2350
2351/* Convert between the modifier bits W32 uses and the modifier bits
2352 Emacs uses. */
2353
2354unsigned int
2355w32_key_to_modifier (int key)
2356{
2357 Lisp_Object key_mapping;
2358
2359 switch (key)
2360 {
2361 case VK_LWIN:
2362 key_mapping = Vw32_lwindow_modifier;
2363 break;
2364 case VK_RWIN:
2365 key_mapping = Vw32_rwindow_modifier;
2366 break;
2367 case VK_APPS:
2368 key_mapping = Vw32_apps_modifier;
2369 break;
2370 case VK_SCROLL:
2371 key_mapping = Vw32_scroll_lock_modifier;
2372 break;
2373 default:
2374 key_mapping = Qnil;
2375 }
2376
2377 /* NB. This code runs in the input thread, asychronously to the lisp
2378 thread, so we must be careful to ensure access to lisp data is
2379 thread-safe. The following code is safe because the modifier
2380 variable values are updated atomically from lisp and symbols are
2381 not relocated by GC. Also, we don't have to worry about seeing GC
2382 markbits here. */
2383 if (EQ (key_mapping, Qhyper))
2384 return hyper_modifier;
2385 if (EQ (key_mapping, Qsuper))
2386 return super_modifier;
2387 if (EQ (key_mapping, Qmeta))
2388 return meta_modifier;
2389 if (EQ (key_mapping, Qalt))
2390 return alt_modifier;
2391 if (EQ (key_mapping, Qctrl))
2392 return ctrl_modifier;
2393 if (EQ (key_mapping, Qcontrol)) /* synonym for ctrl */
2394 return ctrl_modifier;
2395 if (EQ (key_mapping, Qshift))
2396 return shift_modifier;
2397
2398 /* Don't generate any modifier if not explicitly requested. */
2399 return 0;
2400}
2401
2402static unsigned int
2403w32_get_modifiers ()
2404{
2405 return ((modifier_set (VK_SHIFT) ? shift_modifier : 0) |
2406 (modifier_set (VK_CONTROL) ? ctrl_modifier : 0) |
2407 (modifier_set (VK_LWIN) ? w32_key_to_modifier (VK_LWIN) : 0) |
2408 (modifier_set (VK_RWIN) ? w32_key_to_modifier (VK_RWIN) : 0) |
2409 (modifier_set (VK_APPS) ? w32_key_to_modifier (VK_APPS) : 0) |
2410 (modifier_set (VK_SCROLL) ? w32_key_to_modifier (VK_SCROLL) : 0) |
2411 (modifier_set (VK_MENU) ?
2412 ((NILP (Vw32_alt_is_meta)) ? alt_modifier : meta_modifier) : 0));
2413}
2414
2415/* We map the VK_* modifiers into console modifier constants
2416 so that we can use the same routines to handle both console
2417 and window input. */
2418
2419static int
2420construct_console_modifiers ()
2421{
2422 int mods;
2423
2424 mods = 0;
2425 mods |= (modifier_set (VK_SHIFT)) ? SHIFT_PRESSED : 0;
2426 mods |= (modifier_set (VK_CAPITAL)) ? CAPSLOCK_ON : 0;
2427 mods |= (modifier_set (VK_SCROLL)) ? SCROLLLOCK_ON : 0;
2428 mods |= (modifier_set (VK_NUMLOCK)) ? NUMLOCK_ON : 0;
2429 mods |= (modifier_set (VK_LCONTROL)) ? LEFT_CTRL_PRESSED : 0;
2430 mods |= (modifier_set (VK_RCONTROL)) ? RIGHT_CTRL_PRESSED : 0;
2431 mods |= (modifier_set (VK_LMENU)) ? LEFT_ALT_PRESSED : 0;
2432 mods |= (modifier_set (VK_RMENU)) ? RIGHT_ALT_PRESSED : 0;
2433 mods |= (modifier_set (VK_LWIN)) ? LEFT_WIN_PRESSED : 0;
2434 mods |= (modifier_set (VK_RWIN)) ? RIGHT_WIN_PRESSED : 0;
2435 mods |= (modifier_set (VK_APPS)) ? APPS_PRESSED : 0;
2436
2437 return mods;
2438}
2439
2440static int
2441w32_get_key_modifiers (unsigned int wparam, unsigned int lparam)
2442{
2443 int mods;
2444
2445 /* Convert to emacs modifiers. */
2446 mods = w32_kbd_mods_to_emacs (construct_console_modifiers (), wparam);
2447
2448 return mods;
2449}
2450
2451unsigned int
2452map_keypad_keys (unsigned int virt_key, unsigned int extended)
2453{
2454 if (virt_key < VK_CLEAR || virt_key > VK_DELETE)
2455 return virt_key;
2456
2457 if (virt_key == VK_RETURN)
2458 return (extended ? VK_NUMPAD_ENTER : VK_RETURN);
2459
2460 if (virt_key >= VK_PRIOR && virt_key <= VK_DOWN)
2461 return (!extended ? (VK_NUMPAD_PRIOR + (virt_key - VK_PRIOR)) : virt_key);
2462
2463 if (virt_key == VK_INSERT || virt_key == VK_DELETE)
2464 return (!extended ? (VK_NUMPAD_INSERT + (virt_key - VK_INSERT)) : virt_key);
2465
2466 if (virt_key == VK_CLEAR)
2467 return (!extended ? VK_NUMPAD_CLEAR : virt_key);
2468
2469 return virt_key;
2470}
2471
2472/* List of special key combinations which w32 would normally capture,
2473 but Emacs should grab instead. Not directly visible to lisp, to
2474 simplify synchronization. Each item is an integer encoding a virtual
2475 key code and modifier combination to capture. */
2476static Lisp_Object w32_grabbed_keys;
2477
2478#define HOTKEY(vk, mods) make_number (((vk) & 255) | ((mods) << 8))
2479#define HOTKEY_ID(k) (XFASTINT (k) & 0xbfff)
2480#define HOTKEY_VK_CODE(k) (XFASTINT (k) & 255)
2481#define HOTKEY_MODIFIERS(k) (XFASTINT (k) >> 8)
2482
2483#define RAW_HOTKEY_ID(k) ((k) & 0xbfff)
2484#define RAW_HOTKEY_VK_CODE(k) ((k) & 255)
2485#define RAW_HOTKEY_MODIFIERS(k) ((k) >> 8)
2486
2487/* Register hot-keys for reserved key combinations when Emacs has
2488 keyboard focus, since this is the only way Emacs can receive key
2489 combinations like Alt-Tab which are used by the system. */
2490
2491static void
2492register_hot_keys (hwnd)
2493 HWND hwnd;
2494{
2495 Lisp_Object keylist;
2496
2497 /* Use CONSP, since we are called asynchronously. */
2498 for (keylist = w32_grabbed_keys; CONSP (keylist); keylist = XCDR (keylist))
2499 {
2500 Lisp_Object key = XCAR (keylist);
2501
2502 /* Deleted entries get set to nil. */
2503 if (!INTEGERP (key))
2504 continue;
2505
2506 RegisterHotKey (hwnd, HOTKEY_ID (key),
2507 HOTKEY_MODIFIERS (key), HOTKEY_VK_CODE (key));
2508 }
2509}
2510
2511static void
2512unregister_hot_keys (hwnd)
2513 HWND hwnd;
2514{
2515 Lisp_Object keylist;
2516
2517 for (keylist = w32_grabbed_keys; CONSP (keylist); keylist = XCDR (keylist))
2518 {
2519 Lisp_Object key = XCAR (keylist);
2520
2521 if (!INTEGERP (key))
2522 continue;
2523
2524 UnregisterHotKey (hwnd, HOTKEY_ID (key));
2525 }
2526}
2527
2528/* Main message dispatch loop. */
2529
2530static void
2531w32_msg_pump (deferred_msg * msg_buf)
2532{
2533 MSG msg;
2534 int result;
2535 HWND focus_window;
2536
2537 msh_mousewheel = RegisterWindowMessage (MSH_MOUSEWHEEL);
2538
2539 while (GetMessage (&msg, NULL, 0, 0))
2540 {
2541 if (msg.hwnd == NULL)
2542 {
2543 switch (msg.message)
2544 {
2545 case WM_NULL:
2546 /* Produced by complete_deferred_msg; just ignore. */
2547 break;
2548 case WM_EMACS_CREATEWINDOW:
2549 /* Initialize COM for this window. Even though we don't use it,
2550 some third party shell extensions can cause it to be used in
2551 system dialogs, which causes a crash if it is not initialized.
2552 This is a known bug in Windows, which was fixed long ago, but
2553 the patch for XP is not publically available until XP SP3,
2554 and older versions will never be patched. */
2555 CoInitialize (NULL);
2556 w32_createwindow ((struct frame *) msg.wParam);
2557 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2558 abort ();
2559 break;
2560 case WM_EMACS_SETLOCALE:
2561 SetThreadLocale (msg.wParam);
2562 /* Reply is not expected. */
2563 break;
2564 case WM_EMACS_SETKEYBOARDLAYOUT:
2565 result = (int) ActivateKeyboardLayout ((HKL) msg.wParam, 0);
2566 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
2567 result, 0))
2568 abort ();
2569 break;
2570 case WM_EMACS_REGISTER_HOT_KEY:
2571 focus_window = GetFocus ();
2572 if (focus_window != NULL)
2573 RegisterHotKey (focus_window,
2574 RAW_HOTKEY_ID (msg.wParam),
2575 RAW_HOTKEY_MODIFIERS (msg.wParam),
2576 RAW_HOTKEY_VK_CODE (msg.wParam));
2577 /* Reply is not expected. */
2578 break;
2579 case WM_EMACS_UNREGISTER_HOT_KEY:
2580 focus_window = GetFocus ();
2581 if (focus_window != NULL)
2582 UnregisterHotKey (focus_window, RAW_HOTKEY_ID (msg.wParam));
2583 /* Mark item as erased. NB: this code must be
2584 thread-safe. The next line is okay because the cons
2585 cell is never made into garbage and is not relocated by
2586 GC. */
2587 XSETCAR ((Lisp_Object) ((EMACS_INT) msg.lParam), Qnil);
2588 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2589 abort ();
2590 break;
2591 case WM_EMACS_TOGGLE_LOCK_KEY:
2592 {
2593 int vk_code = (int) msg.wParam;
2594 int cur_state = (GetKeyState (vk_code) & 1);
2595 Lisp_Object new_state = (Lisp_Object) ((EMACS_INT) msg.lParam);
2596
2597 /* NB: This code must be thread-safe. It is safe to
2598 call NILP because symbols are not relocated by GC,
2599 and pointer here is not touched by GC (so the markbit
2600 can't be set). Numbers are safe because they are
2601 immediate values. */
2602 if (NILP (new_state)
2603 || (NUMBERP (new_state)
2604 && ((XUINT (new_state)) & 1) != cur_state))
2605 {
2606 one_w32_display_info.faked_key = vk_code;
2607
2608 keybd_event ((BYTE) vk_code,
2609 (BYTE) MapVirtualKey (vk_code, 0),
2610 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2611 keybd_event ((BYTE) vk_code,
2612 (BYTE) MapVirtualKey (vk_code, 0),
2613 KEYEVENTF_EXTENDEDKEY | 0, 0);
2614 keybd_event ((BYTE) vk_code,
2615 (BYTE) MapVirtualKey (vk_code, 0),
2616 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2617 cur_state = !cur_state;
2618 }
2619 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
2620 cur_state, 0))
2621 abort ();
2622 }
2623 break;
2624#ifdef MSG_DEBUG
2625 /* Broadcast messages make it here, so you need to be looking
2626 for something in particular for this to be useful. */
2627 default:
2628 DebPrint (("msg %x not expected by w32_msg_pump\n", msg.message));
2629#endif
2630 }
2631 }
2632 else
2633 {
2634 DispatchMessage (&msg);
2635 }
2636
2637 /* Exit nested loop when our deferred message has completed. */
2638 if (msg_buf->completed)
2639 break;
2640 }
2641}
2642
2643deferred_msg * deferred_msg_head;
2644
2645static deferred_msg *
2646find_deferred_msg (HWND hwnd, UINT msg)
2647{
2648 deferred_msg * item;
2649
2650 /* Don't actually need synchronization for read access, since
2651 modification of single pointer is always atomic. */
2652 /* enter_crit (); */
2653
2654 for (item = deferred_msg_head; item != NULL; item = item->next)
2655 if (item->w32msg.msg.hwnd == hwnd
2656 && item->w32msg.msg.message == msg)
2657 break;
2658
2659 /* leave_crit (); */
2660
2661 return item;
2662}
2663
2664static LRESULT
2665send_deferred_msg (deferred_msg * msg_buf,
2666 HWND hwnd,
2667 UINT msg,
2668 WPARAM wParam,
2669 LPARAM lParam)
2670{
2671 /* Only input thread can send deferred messages. */
2672 if (GetCurrentThreadId () != dwWindowsThreadId)
2673 abort ();
2674
2675 /* It is an error to send a message that is already deferred. */
2676 if (find_deferred_msg (hwnd, msg) != NULL)
2677 abort ();
2678
2679 /* Enforced synchronization is not needed because this is the only
2680 function that alters deferred_msg_head, and the following critical
2681 section is guaranteed to only be serially reentered (since only the
2682 input thread can call us). */
2683
2684 /* enter_crit (); */
2685
2686 msg_buf->completed = 0;
2687 msg_buf->next = deferred_msg_head;
2688 deferred_msg_head = msg_buf;
2689 my_post_msg (&msg_buf->w32msg, hwnd, msg, wParam, lParam);
2690
2691 /* leave_crit (); */
2692
2693 /* Start a new nested message loop to process other messages until
2694 this one is completed. */
2695 w32_msg_pump (msg_buf);
2696
2697 deferred_msg_head = msg_buf->next;
2698
2699 return msg_buf->result;
2700}
2701
2702void
2703complete_deferred_msg (HWND hwnd, UINT msg, LRESULT result)
2704{
2705 deferred_msg * msg_buf = find_deferred_msg (hwnd, msg);
2706
2707 if (msg_buf == NULL)
2708 /* Message may have been cancelled, so don't abort. */
2709 return;
2710
2711 msg_buf->result = result;
2712 msg_buf->completed = 1;
2713
2714 /* Ensure input thread is woken so it notices the completion. */
2715 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
2716}
2717
2718static void
2719cancel_all_deferred_msgs ()
2720{
2721 deferred_msg * item;
2722
2723 /* Don't actually need synchronization for read access, since
2724 modification of single pointer is always atomic. */
2725 /* enter_crit (); */
2726
2727 for (item = deferred_msg_head; item != NULL; item = item->next)
2728 {
2729 item->result = 0;
2730 item->completed = 1;
2731 }
2732
2733 /* leave_crit (); */
2734
2735 /* Ensure input thread is woken so it notices the completion. */
2736 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
2737}
2738
2739DWORD WINAPI
2740w32_msg_worker (void *arg)
2741{
2742 MSG msg;
2743 deferred_msg dummy_buf;
2744
2745 /* Ensure our message queue is created */
2746
2747 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
2748
2749 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2750 abort ();
2751
2752 memset (&dummy_buf, 0, sizeof (dummy_buf));
2753 dummy_buf.w32msg.msg.hwnd = NULL;
2754 dummy_buf.w32msg.msg.message = WM_NULL;
2755
2756 /* This is the inital message loop which should only exit when the
2757 application quits. */
2758 w32_msg_pump (&dummy_buf);
2759
2760 return 0;
2761}
2762
2763static void
2764signal_user_input ()
2765{
2766 /* Interrupt any lisp that wants to be interrupted by input. */
2767 if (!NILP (Vthrow_on_input))
2768 {
2769 Vquit_flag = Vthrow_on_input;
2770 /* If we're inside a function that wants immediate quits,
2771 do it now. */
2772 if (immediate_quit && NILP (Vinhibit_quit))
2773 {
2774 immediate_quit = 0;
2775 QUIT;
2776 }
2777 }
2778}
2779
2780
2781static void
2782post_character_message (hwnd, msg, wParam, lParam, modifiers)
2783 HWND hwnd;
2784 UINT msg;
2785 WPARAM wParam;
2786 LPARAM lParam;
2787 DWORD modifiers;
2788
2789{
2790 W32Msg wmsg;
2791
2792 wmsg.dwModifiers = modifiers;
2793
2794 /* Detect quit_char and set quit-flag directly. Note that we
2795 still need to post a message to ensure the main thread will be
2796 woken up if blocked in sys_select, but we do NOT want to post
2797 the quit_char message itself (because it will usually be as if
2798 the user had typed quit_char twice). Instead, we post a dummy
2799 message that has no particular effect. */
2800 {
2801 int c = wParam;
2802 if (isalpha (c) && wmsg.dwModifiers == ctrl_modifier)
2803 c = make_ctrl_char (c) & 0377;
2804 if (c == quit_char
2805 || (wmsg.dwModifiers == 0 &&
2806 w32_quit_key && wParam == w32_quit_key))
2807 {
2808 Vquit_flag = Qt;
2809
2810 /* The choice of message is somewhat arbitrary, as long as
2811 the main thread handler just ignores it. */
2812 msg = WM_NULL;
2813
2814 /* Interrupt any blocking system calls. */
2815 signal_quit ();
2816
2817 /* As a safety precaution, forcibly complete any deferred
2818 messages. This is a kludge, but I don't see any particularly
2819 clean way to handle the situation where a deferred message is
2820 "dropped" in the lisp thread, and will thus never be
2821 completed, eg. by the user trying to activate the menubar
2822 when the lisp thread is busy, and then typing C-g when the
2823 menubar doesn't open promptly (with the result that the
2824 menubar never responds at all because the deferred
2825 WM_INITMENU message is never completed). Another problem
2826 situation is when the lisp thread calls SendMessage (to send
2827 a window manager command) when a message has been deferred;
2828 the lisp thread gets blocked indefinitely waiting for the
2829 deferred message to be completed, which itself is waiting for
2830 the lisp thread to respond.
2831
2832 Note that we don't want to block the input thread waiting for
2833 a reponse from the lisp thread (although that would at least
2834 solve the deadlock problem above), because we want to be able
2835 to receive C-g to interrupt the lisp thread. */
2836 cancel_all_deferred_msgs ();
2837 }
2838 else
2839 signal_user_input ();
2840 }
2841
2842 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2843}
2844
2845/* Main window procedure */
2846
2847LRESULT CALLBACK
2848w32_wnd_proc (hwnd, msg, wParam, lParam)
2849 HWND hwnd;
2850 UINT msg;
2851 WPARAM wParam;
2852 LPARAM lParam;
2853{
2854 struct frame *f;
2855 struct w32_display_info *dpyinfo = &one_w32_display_info;
2856 W32Msg wmsg;
2857 int windows_translate;
2858 int key;
2859
2860 /* Note that it is okay to call x_window_to_frame, even though we are
2861 not running in the main lisp thread, because frame deletion
2862 requires the lisp thread to synchronize with this thread. Thus, if
2863 a frame struct is returned, it can be used without concern that the
2864 lisp thread might make it disappear while we are using it.
2865
2866 NB. Walking the frame list in this thread is safe (as long as
2867 writes of Lisp_Object slots are atomic, which they are on Windows).
2868 Although delete-frame can destructively modify the frame list while
2869 we are walking it, a garbage collection cannot occur until after
2870 delete-frame has synchronized with this thread.
2871
2872 It is also safe to use functions that make GDI calls, such as
2873 w32_clear_rect, because these functions must obtain a DC handle
2874 from the frame struct using get_frame_dc which is thread-aware. */
2875
2876 switch (msg)
2877 {
2878 case WM_ERASEBKGND:
2879 f = x_window_to_frame (dpyinfo, hwnd);
2880 if (f)
2881 {
2882 HDC hdc = get_frame_dc (f);
2883 GetUpdateRect (hwnd, &wmsg.rect, FALSE);
2884 w32_clear_rect (f, hdc, &wmsg.rect);
2885 release_frame_dc (f, hdc);
2886
2887#if defined (W32_DEBUG_DISPLAY)
2888 DebPrint (("WM_ERASEBKGND (frame %p): erasing %d,%d-%d,%d\n",
2889 f,
2890 wmsg.rect.left, wmsg.rect.top,
2891 wmsg.rect.right, wmsg.rect.bottom));
2892#endif /* W32_DEBUG_DISPLAY */
2893 }
2894 return 1;
2895 case WM_PALETTECHANGED:
2896 /* ignore our own changes */
2897 if ((HWND)wParam != hwnd)
2898 {
2899 f = x_window_to_frame (dpyinfo, hwnd);
2900 if (f)
2901 /* get_frame_dc will realize our palette and force all
2902 frames to be redrawn if needed. */
2903 release_frame_dc (f, get_frame_dc (f));
2904 }
2905 return 0;
2906 case WM_PAINT:
2907 {
2908 PAINTSTRUCT paintStruct;
2909 RECT update_rect;
2910 bzero (&update_rect, sizeof (update_rect));
2911
2912 f = x_window_to_frame (dpyinfo, hwnd);
2913 if (f == 0)
2914 {
2915 DebPrint (("WM_PAINT received for unknown window %p\n", hwnd));
2916 return 0;
2917 }
2918
2919 /* MSDN Docs say not to call BeginPaint if GetUpdateRect
2920 fails. Apparently this can happen under some
2921 circumstances. */
2922 if (GetUpdateRect (hwnd, &update_rect, FALSE) || !w32_strict_painting)
2923 {
2924 enter_crit ();
2925 BeginPaint (hwnd, &paintStruct);
2926
2927 /* The rectangles returned by GetUpdateRect and BeginPaint
2928 do not always match. Play it safe by assuming both areas
2929 are invalid. */
2930 UnionRect (&(wmsg.rect), &update_rect, &(paintStruct.rcPaint));
2931
2932#if defined (W32_DEBUG_DISPLAY)
2933 DebPrint (("WM_PAINT (frame %p): painting %d,%d-%d,%d\n",
2934 f,
2935 wmsg.rect.left, wmsg.rect.top,
2936 wmsg.rect.right, wmsg.rect.bottom));
2937 DebPrint ((" [update region is %d,%d-%d,%d]\n",
2938 update_rect.left, update_rect.top,
2939 update_rect.right, update_rect.bottom));
2940#endif
2941 EndPaint (hwnd, &paintStruct);
2942 leave_crit ();
2943
2944 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2945
2946 return 0;
2947 }
2948
2949 /* If GetUpdateRect returns 0 (meaning there is no update
2950 region), assume the whole window needs to be repainted. */
2951 GetClientRect (hwnd, &wmsg.rect);
2952 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2953 return 0;
2954 }
2955
2956 case WM_INPUTLANGCHANGE:
2957 /* Inform lisp thread of keyboard layout changes. */
2958 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2959
2960 /* Clear dead keys in the keyboard state; for simplicity only
2961 preserve modifier key states. */
2962 {
2963 int i;
2964 BYTE keystate[256];
2965
2966 GetKeyboardState (keystate);
2967 for (i = 0; i < 256; i++)
2968 if (1
2969 && i != VK_SHIFT
2970 && i != VK_LSHIFT
2971 && i != VK_RSHIFT
2972 && i != VK_CAPITAL
2973 && i != VK_NUMLOCK
2974 && i != VK_SCROLL
2975 && i != VK_CONTROL
2976 && i != VK_LCONTROL
2977 && i != VK_RCONTROL
2978 && i != VK_MENU
2979 && i != VK_LMENU
2980 && i != VK_RMENU
2981 && i != VK_LWIN
2982 && i != VK_RWIN)
2983 keystate[i] = 0;
2984 SetKeyboardState (keystate);
2985 }
2986 goto dflt;
2987
2988 case WM_HOTKEY:
2989 /* Synchronize hot keys with normal input. */
2990 PostMessage (hwnd, WM_KEYDOWN, HIWORD (lParam), 0);
2991 return (0);
2992
2993 case WM_KEYUP:
2994 case WM_SYSKEYUP:
2995 record_keyup (wParam, lParam);
2996 goto dflt;
2997
2998 case WM_KEYDOWN:
2999 case WM_SYSKEYDOWN:
3000 /* Ignore keystrokes we fake ourself; see below. */
3001 if (dpyinfo->faked_key == wParam)
3002 {
3003 dpyinfo->faked_key = 0;
3004 /* Make sure TranslateMessage sees them though (as long as
3005 they don't produce WM_CHAR messages). This ensures that
3006 indicator lights are toggled promptly on Windows 9x, for
3007 example. */
3008 if (wParam < 256 && lispy_function_keys[wParam])
3009 {
3010 windows_translate = 1;
3011 goto translate;
3012 }
3013 return 0;
3014 }
3015
3016 /* Synchronize modifiers with current keystroke. */
3017 sync_modifiers ();
3018 record_keydown (wParam, lParam);
3019 wParam = map_keypad_keys (wParam, (lParam & 0x1000000L) != 0);
3020
3021 windows_translate = 0;
3022
3023 switch (wParam)
3024 {
3025 case VK_LWIN:
3026 if (NILP (Vw32_pass_lwindow_to_system))
3027 {
3028 /* Prevent system from acting on keyup (which opens the
3029 Start menu if no other key was pressed) by simulating a
3030 press of Space which we will ignore. */
3031 if (GetAsyncKeyState (wParam) & 1)
3032 {
3033 if (NUMBERP (Vw32_phantom_key_code))
3034 key = XUINT (Vw32_phantom_key_code) & 255;
3035 else
3036 key = VK_SPACE;
3037 dpyinfo->faked_key = key;
3038 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
3039 }
3040 }
3041 if (!NILP (Vw32_lwindow_modifier))
3042 return 0;
3043 break;
3044 case VK_RWIN:
3045 if (NILP (Vw32_pass_rwindow_to_system))
3046 {
3047 if (GetAsyncKeyState (wParam) & 1)
3048 {
3049 if (NUMBERP (Vw32_phantom_key_code))
3050 key = XUINT (Vw32_phantom_key_code) & 255;
3051 else
3052 key = VK_SPACE;
3053 dpyinfo->faked_key = key;
3054 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
3055 }
3056 }
3057 if (!NILP (Vw32_rwindow_modifier))
3058 return 0;
3059 break;
3060 case VK_APPS:
3061 if (!NILP (Vw32_apps_modifier))
3062 return 0;
3063 break;
3064 case VK_MENU:
3065 if (NILP (Vw32_pass_alt_to_system))
3066 /* Prevent DefWindowProc from activating the menu bar if an
3067 Alt key is pressed and released by itself. */
3068 return 0;
3069 windows_translate = 1;
3070 break;
3071 case VK_CAPITAL:
3072 /* Decide whether to treat as modifier or function key. */
3073 if (NILP (Vw32_enable_caps_lock))
3074 goto disable_lock_key;
3075 windows_translate = 1;
3076 break;
3077 case VK_NUMLOCK:
3078 /* Decide whether to treat as modifier or function key. */
3079 if (NILP (Vw32_enable_num_lock))
3080 goto disable_lock_key;
3081 windows_translate = 1;
3082 break;
3083 case VK_SCROLL:
3084 /* Decide whether to treat as modifier or function key. */
3085 if (NILP (Vw32_scroll_lock_modifier))
3086 goto disable_lock_key;
3087 windows_translate = 1;
3088 break;
3089 disable_lock_key:
3090 /* Ensure the appropriate lock key state (and indicator light)
3091 remains in the same state. We do this by faking another
3092 press of the relevant key. Apparently, this really is the
3093 only way to toggle the state of the indicator lights. */
3094 dpyinfo->faked_key = wParam;
3095 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
3096 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
3097 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
3098 KEYEVENTF_EXTENDEDKEY | 0, 0);
3099 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
3100 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
3101 /* Ensure indicator lights are updated promptly on Windows 9x
3102 (TranslateMessage apparently does this), after forwarding
3103 input event. */
3104 post_character_message (hwnd, msg, wParam, lParam,
3105 w32_get_key_modifiers (wParam, lParam));
3106 windows_translate = 1;
3107 break;
3108 case VK_CONTROL:
3109 case VK_SHIFT:
3110 case VK_PROCESSKEY: /* Generated by IME. */
3111 windows_translate = 1;
3112 break;
3113 case VK_CANCEL:
3114 /* Windows maps Ctrl-Pause (aka Ctrl-Break) into VK_CANCEL,
3115 which is confusing for purposes of key binding; convert
3116 VK_CANCEL events into VK_PAUSE events. */
3117 wParam = VK_PAUSE;
3118 break;
3119 case VK_PAUSE:
3120 /* Windows maps Ctrl-NumLock into VK_PAUSE, which is confusing
3121 for purposes of key binding; convert these back into
3122 VK_NUMLOCK events, at least when we want to see NumLock key
3123 presses. (Note that there is never any possibility that
3124 VK_PAUSE with Ctrl really is C-Pause as per above.) */
3125 if (NILP (Vw32_enable_num_lock) && modifier_set (VK_CONTROL))
3126 wParam = VK_NUMLOCK;
3127 break;
3128 default:
3129 /* If not defined as a function key, change it to a WM_CHAR message. */
3130 if (wParam > 255 || !lispy_function_keys[wParam])
3131 {
3132 DWORD modifiers = construct_console_modifiers ();
3133
3134 if (!NILP (Vw32_recognize_altgr)
3135 && modifier_set (VK_LCONTROL) && modifier_set (VK_RMENU))
3136 {
3137 /* Always let TranslateMessage handle AltGr key chords;
3138 for some reason, ToAscii doesn't always process AltGr
3139 chords correctly. */
3140 windows_translate = 1;
3141 }
3142 else if ((modifiers & (~SHIFT_PRESSED & ~CAPSLOCK_ON)) != 0)
3143 {
3144 /* Handle key chords including any modifiers other
3145 than shift directly, in order to preserve as much
3146 modifier information as possible. */
3147 if ('A' <= wParam && wParam <= 'Z')
3148 {
3149 /* Don't translate modified alphabetic keystrokes,
3150 so the user doesn't need to constantly switch
3151 layout to type control or meta keystrokes when
3152 the normal layout translates alphabetic
3153 characters to non-ascii characters. */
3154 if (!modifier_set (VK_SHIFT))
3155 wParam += ('a' - 'A');
3156 msg = WM_CHAR;
3157 }
3158 else
3159 {
3160 /* Try to handle other keystrokes by determining the
3161 base character (ie. translating the base key plus
3162 shift modifier). */
3163 int add;
3164 int isdead = 0;
3165 KEY_EVENT_RECORD key;
3166
3167 key.bKeyDown = TRUE;
3168 key.wRepeatCount = 1;
3169 key.wVirtualKeyCode = wParam;
3170 key.wVirtualScanCode = (lParam & 0xFF0000) >> 16;
3171 key.uChar.AsciiChar = 0;
3172 key.dwControlKeyState = modifiers;
3173
3174 add = w32_kbd_patch_key (&key);
3175 /* 0 means an unrecognised keycode, negative means
3176 dead key. Ignore both. */
3177 while (--add >= 0)
3178 {
3179 /* Forward asciified character sequence. */
3180 post_character_message
3181 (hwnd, WM_CHAR,
3182 (unsigned char) key.uChar.AsciiChar, lParam,
3183 w32_get_key_modifiers (wParam, lParam));
3184 w32_kbd_patch_key (&key);
3185 }
3186 return 0;
3187 }
3188 }
3189 else
3190 {
3191 /* Let TranslateMessage handle everything else. */
3192 windows_translate = 1;
3193 }
3194 }
3195 }
3196
3197 translate:
3198 if (windows_translate)
3199 {
3200 MSG windows_msg = { hwnd, msg, wParam, lParam, 0, {0,0} };
3201 windows_msg.time = GetMessageTime ();
3202 TranslateMessage (&windows_msg);
3203 goto dflt;
3204 }
3205
3206 /* Fall through */
3207
3208 case WM_SYSCHAR:
3209 case WM_CHAR:
3210 post_character_message (hwnd, msg, wParam, lParam,
3211 w32_get_key_modifiers (wParam, lParam));
3212 break;
3213
3214 case WM_UNICHAR:
3215 /* WM_UNICHAR looks promising from the docs, but the exact
3216 circumstances in which TranslateMessage sends it is one of those
3217 Microsoft secret API things that EU and US courts are supposed
3218 to have put a stop to already. Spy++ shows it being sent to Notepad
3219 and other MS apps, but never to Emacs.
3220
3221 Some third party IMEs send it in accordance with the official
3222 documentation though, so handle it here.
3223
3224 UNICODE_NOCHAR is used to test for support for this message.
3225 TRUE indicates that the message is supported. */
3226 if (wParam == UNICODE_NOCHAR)
3227 return TRUE;
3228
3229 {
3230 W32Msg wmsg;
3231 wmsg.dwModifiers = w32_get_key_modifiers (wParam, lParam);
3232 signal_user_input ();
3233 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3234 }
3235 break;
3236
3237 case WM_IME_CHAR:
3238 /* If we can't get the IME result as unicode, use default processing,
3239 which will at least allow characters decodable in the system locale
3240 get through. */
3241 if (!get_composition_string_fn)
3242 goto dflt;
3243
3244 else if (!ignore_ime_char)
3245 {
3246 wchar_t * buffer;
3247 int size, i;
3248 W32Msg wmsg;
3249 HIMC context = get_ime_context_fn (hwnd);
3250 wmsg.dwModifiers = w32_get_key_modifiers (wParam, lParam);
3251 /* Get buffer size. */
3252 size = get_composition_string_fn (context, GCS_RESULTSTR, buffer, 0);
3253 buffer = alloca(size);
3254 size = get_composition_string_fn (context, GCS_RESULTSTR,
3255 buffer, size);
3256 signal_user_input ();
3257 for (i = 0; i < size / sizeof (wchar_t); i++)
3258 {
3259 my_post_msg (&wmsg, hwnd, WM_UNICHAR, (WPARAM) buffer[i],
3260 lParam);
3261 }
3262 /* We output the whole string above, so ignore following ones
3263 until we are notified of the end of composition. */
3264 ignore_ime_char = 1;
3265 }
3266 break;
3267
3268 case WM_IME_ENDCOMPOSITION:
3269 ignore_ime_char = 0;
3270 goto dflt;
3271
3272 /* Simulate middle mouse button events when left and right buttons
3273 are used together, but only if user has two button mouse. */
3274 case WM_LBUTTONDOWN:
3275 case WM_RBUTTONDOWN:
3276 if (w32_num_mouse_buttons > 2)
3277 goto handle_plain_button;
3278
3279 {
3280 int this = (msg == WM_LBUTTONDOWN) ? LMOUSE : RMOUSE;
3281 int other = (msg == WM_LBUTTONDOWN) ? RMOUSE : LMOUSE;
3282
3283 if (button_state & this)
3284 return 0;
3285
3286 if (button_state == 0)
3287 SetCapture (hwnd);
3288
3289 button_state |= this;
3290
3291 if (button_state & other)
3292 {
3293 if (mouse_button_timer)
3294 {
3295 KillTimer (hwnd, mouse_button_timer);
3296 mouse_button_timer = 0;
3297
3298 /* Generate middle mouse event instead. */
3299 msg = WM_MBUTTONDOWN;
3300 button_state |= MMOUSE;
3301 }
3302 else if (button_state & MMOUSE)
3303 {
3304 /* Ignore button event if we've already generated a
3305 middle mouse down event. This happens if the
3306 user releases and press one of the two buttons
3307 after we've faked a middle mouse event. */
3308 return 0;
3309 }
3310 else
3311 {
3312 /* Flush out saved message. */
3313 post_msg (&saved_mouse_button_msg);
3314 }
3315 wmsg.dwModifiers = w32_get_modifiers ();
3316 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3317 signal_user_input ();
3318
3319 /* Clear message buffer. */
3320 saved_mouse_button_msg.msg.hwnd = 0;
3321 }
3322 else
3323 {
3324 /* Hold onto message for now. */
3325 mouse_button_timer =
3326 SetTimer (hwnd, MOUSE_BUTTON_ID,
3327 w32_mouse_button_tolerance, NULL);
3328 saved_mouse_button_msg.msg.hwnd = hwnd;
3329 saved_mouse_button_msg.msg.message = msg;
3330 saved_mouse_button_msg.msg.wParam = wParam;
3331 saved_mouse_button_msg.msg.lParam = lParam;
3332 saved_mouse_button_msg.msg.time = GetMessageTime ();
3333 saved_mouse_button_msg.dwModifiers = w32_get_modifiers ();
3334 }
3335 }
3336 return 0;
3337
3338 case WM_LBUTTONUP:
3339 case WM_RBUTTONUP:
3340 if (w32_num_mouse_buttons > 2)
3341 goto handle_plain_button;
3342
3343 {
3344 int this = (msg == WM_LBUTTONUP) ? LMOUSE : RMOUSE;
3345 int other = (msg == WM_LBUTTONUP) ? RMOUSE : LMOUSE;
3346
3347 if ((button_state & this) == 0)
3348 return 0;
3349
3350 button_state &= ~this;
3351
3352 if (button_state & MMOUSE)
3353 {
3354 /* Only generate event when second button is released. */
3355 if ((button_state & other) == 0)
3356 {
3357 msg = WM_MBUTTONUP;
3358 button_state &= ~MMOUSE;
3359
3360 if (button_state) abort ();
3361 }
3362 else
3363 return 0;
3364 }
3365 else
3366 {
3367 /* Flush out saved message if necessary. */
3368 if (saved_mouse_button_msg.msg.hwnd)
3369 {
3370 post_msg (&saved_mouse_button_msg);
3371 }
3372 }
3373 wmsg.dwModifiers = w32_get_modifiers ();
3374 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3375 signal_user_input ();
3376
3377 /* Always clear message buffer and cancel timer. */
3378 saved_mouse_button_msg.msg.hwnd = 0;
3379 KillTimer (hwnd, mouse_button_timer);
3380 mouse_button_timer = 0;
3381
3382 if (button_state == 0)
3383 ReleaseCapture ();
3384 }
3385 return 0;
3386
3387 case WM_XBUTTONDOWN:
3388 case WM_XBUTTONUP:
3389 if (w32_pass_extra_mouse_buttons_to_system)
3390 goto dflt;
3391 /* else fall through and process them. */
3392 case WM_MBUTTONDOWN:
3393 case WM_MBUTTONUP:
3394 handle_plain_button:
3395 {
3396 BOOL up;
3397 int button;
3398
3399 /* Ignore middle and extra buttons as long as the menu is active. */
3400 f = x_window_to_frame (dpyinfo, hwnd);
3401 if (f && f->output_data.w32->menubar_active)
3402 return 0;
3403
3404 if (parse_button (msg, HIWORD (wParam), &button, &up))
3405 {
3406 if (up) ReleaseCapture ();
3407 else SetCapture (hwnd);
3408 button = (button == 0) ? LMOUSE :
3409 ((button == 1) ? MMOUSE : RMOUSE);
3410 if (up)
3411 button_state &= ~button;
3412 else
3413 button_state |= button;
3414 }
3415 }
3416
3417 wmsg.dwModifiers = w32_get_modifiers ();
3418 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3419 signal_user_input ();
3420
3421 /* Need to return true for XBUTTON messages, false for others,
3422 to indicate that we processed the message. */
3423 return (msg == WM_XBUTTONDOWN || msg == WM_XBUTTONUP);
3424
3425 case WM_MOUSEMOVE:
3426 /* Ignore mouse movements as long as the menu is active. These
3427 movements are processed by the window manager anyway, and
3428 it's wrong to handle them as if they happened on the
3429 underlying frame. */
3430 f = x_window_to_frame (dpyinfo, hwnd);
3431 if (f && f->output_data.w32->menubar_active)
3432 return 0;
3433
3434 /* If the mouse has just moved into the frame, start tracking
3435 it, so we will be notified when it leaves the frame. Mouse
3436 tracking only works under W98 and NT4 and later. On earlier
3437 versions, there is no way of telling when the mouse leaves the
3438 frame, so we just have to put up with help-echo and mouse
3439 highlighting remaining while the frame is not active. */
3440 if (track_mouse_event_fn && !track_mouse_window)
3441 {
3442 TRACKMOUSEEVENT tme;
3443 tme.cbSize = sizeof (tme);
3444 tme.dwFlags = TME_LEAVE;
3445 tme.hwndTrack = hwnd;
3446
3447 track_mouse_event_fn (&tme);
3448 track_mouse_window = hwnd;
3449 }
3450 case WM_VSCROLL:
3451 if (w32_mouse_move_interval <= 0
3452 || (msg == WM_MOUSEMOVE && button_state == 0))
3453 {
3454 wmsg.dwModifiers = w32_get_modifiers ();
3455 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3456 return 0;
3457 }
3458
3459 /* Hang onto mouse move and scroll messages for a bit, to avoid
3460 sending such events to Emacs faster than it can process them.
3461 If we get more events before the timer from the first message
3462 expires, we just replace the first message. */
3463
3464 if (saved_mouse_move_msg.msg.hwnd == 0)
3465 mouse_move_timer =
3466 SetTimer (hwnd, MOUSE_MOVE_ID,
3467 w32_mouse_move_interval, NULL);
3468
3469 /* Hold onto message for now. */
3470 saved_mouse_move_msg.msg.hwnd = hwnd;
3471 saved_mouse_move_msg.msg.message = msg;
3472 saved_mouse_move_msg.msg.wParam = wParam;
3473 saved_mouse_move_msg.msg.lParam = lParam;
3474 saved_mouse_move_msg.msg.time = GetMessageTime ();
3475 saved_mouse_move_msg.dwModifiers = w32_get_modifiers ();
3476
3477 return 0;
3478
3479 case WM_MOUSEWHEEL:
3480 case WM_DROPFILES:
3481 wmsg.dwModifiers = w32_get_modifiers ();
3482 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3483 signal_user_input ();
3484 return 0;
3485
3486 case WM_APPCOMMAND:
3487 if (w32_pass_multimedia_buttons_to_system)
3488 goto dflt;
3489 /* Otherwise, pass to lisp, the same way we do with mousehwheel. */
3490 case WM_MOUSEHWHEEL:
3491 wmsg.dwModifiers = w32_get_modifiers ();
3492 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3493 signal_user_input ();
3494 /* Non-zero must be returned when WM_MOUSEHWHEEL messages are
3495 handled, to prevent the system trying to handle it by faking
3496 scroll bar events. */
3497 return 1;
3498
3499 case WM_TIMER:
3500 /* Flush out saved messages if necessary. */
3501 if (wParam == mouse_button_timer)
3502 {
3503 if (saved_mouse_button_msg.msg.hwnd)
3504 {
3505 post_msg (&saved_mouse_button_msg);
3506 signal_user_input ();
3507 saved_mouse_button_msg.msg.hwnd = 0;
3508 }
3509 KillTimer (hwnd, mouse_button_timer);
3510 mouse_button_timer = 0;
3511 }
3512 else if (wParam == mouse_move_timer)
3513 {
3514 if (saved_mouse_move_msg.msg.hwnd)
3515 {
3516 post_msg (&saved_mouse_move_msg);
3517 saved_mouse_move_msg.msg.hwnd = 0;
3518 }
3519 KillTimer (hwnd, mouse_move_timer);
3520 mouse_move_timer = 0;
3521 }
3522 else if (wParam == menu_free_timer)
3523 {
3524 KillTimer (hwnd, menu_free_timer);
3525 menu_free_timer = 0;
3526 f = x_window_to_frame (dpyinfo, hwnd);
3527 /* If a popup menu is active, don't wipe its strings. */
3528 if (menubar_in_use
3529 && current_popup_menu == NULL)
3530 {
3531 /* Free memory used by owner-drawn and help-echo strings. */
3532 w32_free_menu_strings (hwnd);
3533 f->output_data.w32->menubar_active = 0;
3534 menubar_in_use = 0;
3535 }
3536 }
3537 else if (wParam == hourglass_timer)
3538 {
3539 KillTimer (hwnd, hourglass_timer);
3540 hourglass_timer = 0;
3541 show_hourglass (x_window_to_frame (dpyinfo, hwnd));
3542 }
3543 return 0;
3544
3545 case WM_NCACTIVATE:
3546 /* Windows doesn't send us focus messages when putting up and
3547 taking down a system popup dialog as for Ctrl-Alt-Del on Windows 95.
3548 The only indication we get that something happened is receiving
3549 this message afterwards. So this is a good time to reset our
3550 keyboard modifiers' state. */
3551 reset_modifiers ();
3552 goto dflt;
3553
3554 case WM_INITMENU:
3555 button_state = 0;
3556 ReleaseCapture ();
3557 /* We must ensure menu bar is fully constructed and up to date
3558 before allowing user interaction with it. To achieve this
3559 we send this message to the lisp thread and wait for a
3560 reply (whose value is not actually needed) to indicate that
3561 the menu bar is now ready for use, so we can now return.
3562
3563 To remain responsive in the meantime, we enter a nested message
3564 loop that can process all other messages.
3565
3566 However, we skip all this if the message results from calling
3567 TrackPopupMenu - in fact, we must NOT attempt to send the lisp
3568 thread a message because it is blocked on us at this point. We
3569 set menubar_active before calling TrackPopupMenu to indicate
3570 this (there is no possibility of confusion with real menubar
3571 being active). */
3572
3573 f = x_window_to_frame (dpyinfo, hwnd);
3574 if (f
3575 && (f->output_data.w32->menubar_active
3576 /* We can receive this message even in the absence of a
3577 menubar (ie. when the system menu is activated) - in this
3578 case we do NOT want to forward the message, otherwise it
3579 will cause the menubar to suddenly appear when the user
3580 had requested it to be turned off! */
3581 || f->output_data.w32->menubar_widget == NULL))
3582 return 0;
3583
3584 {
3585 deferred_msg msg_buf;
3586
3587 /* Detect if message has already been deferred; in this case
3588 we cannot return any sensible value to ignore this. */
3589 if (find_deferred_msg (hwnd, msg) != NULL)
3590 abort ();
3591
3592 menubar_in_use = 1;
3593
3594 return send_deferred_msg (&msg_buf, hwnd, msg, wParam, lParam);
3595 }
3596
3597 case WM_EXITMENULOOP:
3598 f = x_window_to_frame (dpyinfo, hwnd);
3599
3600 /* If a menu is still active, check again after a short delay,
3601 since Windows often (always?) sends the WM_EXITMENULOOP
3602 before the corresponding WM_COMMAND message.
3603 Don't do this if a popup menu is active, since it is only
3604 menubar menus that require cleaning up in this way.
3605 */
3606 if (f && menubar_in_use && current_popup_menu == NULL)
3607 menu_free_timer = SetTimer (hwnd, MENU_FREE_ID, MENU_FREE_DELAY, NULL);
3608
3609 /* If hourglass cursor should be displayed, display it now. */
3610 if (f && f->output_data.w32->hourglass_p)
3611 SetCursor (f->output_data.w32->hourglass_cursor);
3612
3613 goto dflt;
3614
3615 case WM_MENUSELECT:
3616 /* Direct handling of help_echo in menus. Should be safe now
3617 that we generate the help_echo by placing a help event in the
3618 keyboard buffer. */
3619 {
3620 HMENU menu = (HMENU) lParam;
3621 UINT menu_item = (UINT) LOWORD (wParam);
3622 UINT flags = (UINT) HIWORD (wParam);
3623
3624 w32_menu_display_help (hwnd, menu, menu_item, flags);
3625 }
3626 return 0;
3627
3628 case WM_MEASUREITEM:
3629 f = x_window_to_frame (dpyinfo, hwnd);
3630 if (f)
3631 {
3632 MEASUREITEMSTRUCT * pMis = (MEASUREITEMSTRUCT *) lParam;
3633
3634 if (pMis->CtlType == ODT_MENU)
3635 {
3636 /* Work out dimensions for popup menu titles. */
3637 char * title = (char *) pMis->itemData;
3638 HDC hdc = GetDC (hwnd);
3639 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
3640 LOGFONT menu_logfont;
3641 HFONT old_font;
3642 SIZE size;
3643
3644 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
3645 menu_logfont.lfWeight = FW_BOLD;
3646 menu_font = CreateFontIndirect (&menu_logfont);
3647 old_font = SelectObject (hdc, menu_font);
3648
3649 pMis->itemHeight = GetSystemMetrics (SM_CYMENUSIZE);
3650 if (title)
3651 {
3652 if (unicode_append_menu)
3653 GetTextExtentPoint32W (hdc, (WCHAR *) title,
3654 wcslen ((WCHAR *) title),
3655 &size);
3656 else
3657 GetTextExtentPoint32 (hdc, title, strlen (title), &size);
3658
3659 pMis->itemWidth = size.cx;
3660 if (pMis->itemHeight < size.cy)
3661 pMis->itemHeight = size.cy;
3662 }
3663 else
3664 pMis->itemWidth = 0;
3665
3666 SelectObject (hdc, old_font);
3667 DeleteObject (menu_font);
3668 ReleaseDC (hwnd, hdc);
3669 return TRUE;
3670 }
3671 }
3672 return 0;
3673
3674 case WM_DRAWITEM:
3675 f = x_window_to_frame (dpyinfo, hwnd);
3676 if (f)
3677 {
3678 DRAWITEMSTRUCT * pDis = (DRAWITEMSTRUCT *) lParam;
3679
3680 if (pDis->CtlType == ODT_MENU)
3681 {
3682 /* Draw popup menu title. */
3683 char * title = (char *) pDis->itemData;
3684 if (title)
3685 {
3686 HDC hdc = pDis->hDC;
3687 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
3688 LOGFONT menu_logfont;
3689 HFONT old_font;
3690
3691 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
3692 menu_logfont.lfWeight = FW_BOLD;
3693 menu_font = CreateFontIndirect (&menu_logfont);
3694 old_font = SelectObject (hdc, menu_font);
3695
3696 /* Always draw title as if not selected. */
3697 if (unicode_append_menu)
3698 ExtTextOutW (hdc,
3699 pDis->rcItem.left
3700 + GetSystemMetrics (SM_CXMENUCHECK),
3701 pDis->rcItem.top,
3702 ETO_OPAQUE, &pDis->rcItem,
3703 (WCHAR *) title,
3704 wcslen ((WCHAR *) title), NULL);
3705 else
3706 ExtTextOut (hdc,
3707 pDis->rcItem.left
3708 + GetSystemMetrics (SM_CXMENUCHECK),
3709 pDis->rcItem.top,
3710 ETO_OPAQUE, &pDis->rcItem,
3711 title, strlen (title), NULL);
3712
3713 SelectObject (hdc, old_font);
3714 DeleteObject (menu_font);
3715 }
3716 return TRUE;
3717 }
3718 }
3719 return 0;
3720
3721#if 0
3722 /* Still not right - can't distinguish between clicks in the
3723 client area of the frame from clicks forwarded from the scroll
3724 bars - may have to hook WM_NCHITTEST to remember the mouse
3725 position and then check if it is in the client area ourselves. */
3726 case WM_MOUSEACTIVATE:
3727 /* Discard the mouse click that activates a frame, allowing the
3728 user to click anywhere without changing point (or worse!).
3729 Don't eat mouse clicks on scrollbars though!! */
3730 if (LOWORD (lParam) == HTCLIENT )
3731 return MA_ACTIVATEANDEAT;
3732 goto dflt;
3733#endif
3734
3735 case WM_MOUSELEAVE:
3736 /* No longer tracking mouse. */
3737 track_mouse_window = NULL;
3738
3739 case WM_ACTIVATEAPP:
3740 case WM_ACTIVATE:
3741 case WM_WINDOWPOSCHANGED:
3742 case WM_SHOWWINDOW:
3743 /* Inform lisp thread that a frame might have just been obscured
3744 or exposed, so should recheck visibility of all frames. */
3745 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3746 goto dflt;
3747
3748 case WM_SETFOCUS:
3749 dpyinfo->faked_key = 0;
3750 reset_modifiers ();
3751 register_hot_keys (hwnd);
3752 goto command;
3753 case WM_KILLFOCUS:
3754 unregister_hot_keys (hwnd);
3755 button_state = 0;
3756 ReleaseCapture ();
3757 /* Relinquish the system caret. */
3758 if (w32_system_caret_hwnd)
3759 {
3760 w32_visible_system_caret_hwnd = NULL;
3761 w32_system_caret_hwnd = NULL;
3762 DestroyCaret ();
3763 }
3764 goto command;
3765 case WM_COMMAND:
3766 menubar_in_use = 0;
3767 f = x_window_to_frame (dpyinfo, hwnd);
3768 if (f && HIWORD (wParam) == 0)
3769 {
3770 if (menu_free_timer)
3771 {
3772 KillTimer (hwnd, menu_free_timer);
3773 menu_free_timer = 0;
3774 }
3775 }
3776 case WM_MOVE:
3777 case WM_SIZE:
3778 command:
3779 wmsg.dwModifiers = w32_get_modifiers ();
3780 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3781 goto dflt;
3782
3783 case WM_DESTROY:
3784 CoUninitialize ();
3785 return 0;
3786
3787 case WM_CLOSE:
3788 wmsg.dwModifiers = w32_get_modifiers ();
3789 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3790 return 0;
3791
3792 case WM_WINDOWPOSCHANGING:
3793 /* Don't restrict the sizing of tip frames. */
3794 if (hwnd == tip_window)
3795 return 0;
3796 {
3797 WINDOWPLACEMENT wp;
3798 LPWINDOWPOS lppos = (WINDOWPOS *) lParam;
3799
3800 wp.length = sizeof (WINDOWPLACEMENT);
3801 GetWindowPlacement (hwnd, &wp);
3802
3803 if (wp.showCmd != SW_SHOWMINIMIZED && (lppos->flags & SWP_NOSIZE) == 0)
3804 {
3805 RECT rect;
3806 int wdiff;
3807 int hdiff;
3808 DWORD font_width;
3809 DWORD line_height;
3810 DWORD internal_border;
3811 DWORD scrollbar_extra;
3812 RECT wr;
3813
3814 wp.length = sizeof (wp);
3815 GetWindowRect (hwnd, &wr);
3816
3817 enter_crit ();
3818
3819 font_width = GetWindowLong (hwnd, WND_FONTWIDTH_INDEX);
3820 line_height = GetWindowLong (hwnd, WND_LINEHEIGHT_INDEX);
3821 internal_border = GetWindowLong (hwnd, WND_BORDER_INDEX);
3822 scrollbar_extra = GetWindowLong (hwnd, WND_SCROLLBAR_INDEX);
3823
3824 leave_crit ();
3825
3826 memset (&rect, 0, sizeof (rect));
3827 AdjustWindowRect (&rect, GetWindowLong (hwnd, GWL_STYLE),
3828 GetMenu (hwnd) != NULL);
3829
3830 /* Force width and height of client area to be exact
3831 multiples of the character cell dimensions. */
3832 wdiff = (lppos->cx - (rect.right - rect.left)
3833 - 2 * internal_border - scrollbar_extra)
3834 % font_width;
3835 hdiff = (lppos->cy - (rect.bottom - rect.top)
3836 - 2 * internal_border)
3837 % line_height;
3838
3839 if (wdiff || hdiff)
3840 {
3841 /* For right/bottom sizing we can just fix the sizes.
3842 However for top/left sizing we will need to fix the X
3843 and Y positions as well. */
3844
3845 int cx_mintrack = GetSystemMetrics (SM_CXMINTRACK);
3846 int cy_mintrack = GetSystemMetrics (SM_CYMINTRACK);
3847
3848 lppos->cx = max (lppos->cx - wdiff, cx_mintrack);
3849 lppos->cy = max (lppos->cy - hdiff, cy_mintrack);
3850
3851 if (wp.showCmd != SW_SHOWMAXIMIZED
3852 && (lppos->flags & SWP_NOMOVE) == 0)
3853 {
3854 if (lppos->x != wr.left || lppos->y != wr.top)
3855 {
3856 lppos->x += wdiff;
3857 lppos->y += hdiff;
3858 }
3859 else
3860 {
3861 lppos->flags |= SWP_NOMOVE;
3862 }
3863 }
3864
3865 return 0;
3866 }
3867 }
3868 }
3869
3870 goto dflt;
3871
3872 case WM_GETMINMAXINFO:
3873 /* Hack to allow resizing the Emacs frame above the screen size.
3874 Note that Windows 9x limits coordinates to 16-bits. */
3875 ((LPMINMAXINFO) lParam)->ptMaxTrackSize.x = 32767;
3876 ((LPMINMAXINFO) lParam)->ptMaxTrackSize.y = 32767;
3877 return 0;
3878
3879 case WM_SETCURSOR:
3880 if (LOWORD (lParam) == HTCLIENT)
3881 {
3882 f = x_window_to_frame (dpyinfo, hwnd);
3883 if (f->output_data.w32->hourglass_p && !menubar_in_use
3884 && !current_popup_menu)
3885 SetCursor (f->output_data.w32->hourglass_cursor);
3886 else
3887 SetCursor (f->output_data.w32->current_cursor);
3888 return 0;
3889 }
3890 goto dflt;
3891
3892 case WM_EMACS_SETCURSOR:
3893 {
3894 Cursor cursor = (Cursor) wParam;
3895 f = x_window_to_frame (dpyinfo, hwnd);
3896 if (f && cursor)
3897 {
3898 f->output_data.w32->current_cursor = cursor;
3899 if (!f->output_data.w32->hourglass_p)
3900 SetCursor (cursor);
3901 }
3902 return 0;
3903 }
3904
3905 case WM_EMACS_CREATESCROLLBAR:
3906 return (LRESULT) w32_createscrollbar ((struct frame *) wParam,
3907 (struct scroll_bar *) lParam);
3908
3909 case WM_EMACS_SHOWWINDOW:
3910 return ShowWindow ((HWND) wParam, (WPARAM) lParam);
3911
3912 case WM_EMACS_SETFOREGROUND:
3913 {
3914 HWND foreground_window;
3915 DWORD foreground_thread, retval;
3916
3917 /* On NT 5.0, and apparently Windows 98, it is necessary to
3918 attach to the thread that currently has focus in order to
3919 pull the focus away from it. */
3920 foreground_window = GetForegroundWindow ();
3921 foreground_thread = GetWindowThreadProcessId (foreground_window, NULL);
3922 if (!foreground_window
3923 || foreground_thread == GetCurrentThreadId ()
3924 || !AttachThreadInput (GetCurrentThreadId (),
3925 foreground_thread, TRUE))
3926 foreground_thread = 0;
3927
3928 retval = SetForegroundWindow ((HWND) wParam);
3929
3930 /* Detach from the previous foreground thread. */
3931 if (foreground_thread)
3932 AttachThreadInput (GetCurrentThreadId (),
3933 foreground_thread, FALSE);
3934
3935 return retval;
3936 }
3937
3938 case WM_EMACS_SETWINDOWPOS:
3939 {
3940 WINDOWPOS * pos = (WINDOWPOS *) wParam;
3941 return SetWindowPos (hwnd, pos->hwndInsertAfter,
3942 pos->x, pos->y, pos->cx, pos->cy, pos->flags);
3943 }
3944
3945 case WM_EMACS_DESTROYWINDOW:
3946 DragAcceptFiles ((HWND) wParam, FALSE);
3947 return DestroyWindow ((HWND) wParam);
3948
3949 case WM_EMACS_HIDE_CARET:
3950 return HideCaret (hwnd);
3951
3952 case WM_EMACS_SHOW_CARET:
3953 return ShowCaret (hwnd);
3954
3955 case WM_EMACS_DESTROY_CARET:
3956 w32_system_caret_hwnd = NULL;
3957 w32_visible_system_caret_hwnd = NULL;
3958 return DestroyCaret ();
3959
3960 case WM_EMACS_TRACK_CARET:
3961 /* If there is currently no system caret, create one. */
3962 if (w32_system_caret_hwnd == NULL)
3963 {
3964 /* Use the default caret width, and avoid changing it
3965 unneccesarily, as it confuses screen reader software. */
3966 w32_system_caret_hwnd = hwnd;
3967 CreateCaret (hwnd, NULL, 0,
3968 w32_system_caret_height);
3969 }
3970
3971 if (!SetCaretPos (w32_system_caret_x, w32_system_caret_y))
3972 return 0;
3973 /* Ensure visible caret gets turned on when requested. */
3974 else if (w32_use_visible_system_caret
3975 && w32_visible_system_caret_hwnd != hwnd)
3976 {
3977 w32_visible_system_caret_hwnd = hwnd;
3978 return ShowCaret (hwnd);
3979 }
3980 /* Ensure visible caret gets turned off when requested. */
3981 else if (!w32_use_visible_system_caret
3982 && w32_visible_system_caret_hwnd)
3983 {
3984 w32_visible_system_caret_hwnd = NULL;
3985 return HideCaret (hwnd);
3986 }
3987 else
3988 return 1;
3989
3990 case WM_EMACS_TRACKPOPUPMENU:
3991 {
3992 UINT flags;
3993 POINT *pos;
3994 int retval;
3995 pos = (POINT *)lParam;
3996 flags = TPM_CENTERALIGN;
3997 if (button_state & LMOUSE)
3998 flags |= TPM_LEFTBUTTON;
3999 else if (button_state & RMOUSE)
4000 flags |= TPM_RIGHTBUTTON;
4001
4002 /* Remember we did a SetCapture on the initial mouse down event,
4003 so for safety, we make sure the capture is cancelled now. */
4004 ReleaseCapture ();
4005 button_state = 0;
4006
4007 /* Use menubar_active to indicate that WM_INITMENU is from
4008 TrackPopupMenu below, and should be ignored. */
4009 f = x_window_to_frame (dpyinfo, hwnd);
4010 if (f)
4011 f->output_data.w32->menubar_active = 1;
4012
4013 if (TrackPopupMenu ((HMENU)wParam, flags, pos->x, pos->y,
4014 0, hwnd, NULL))
4015 {
4016 MSG amsg;
4017 /* Eat any mouse messages during popupmenu */
4018 while (PeekMessage (&amsg, hwnd, WM_MOUSEFIRST, WM_MOUSELAST,
4019 PM_REMOVE));
4020 /* Get the menu selection, if any */
4021 if (PeekMessage (&amsg, hwnd, WM_COMMAND, WM_COMMAND, PM_REMOVE))
4022 {
4023 retval = LOWORD (amsg.wParam);
4024 }
4025 else
4026 {
4027 retval = 0;
4028 }
4029 }
4030 else
4031 {
4032 retval = -1;
4033 }
4034
4035 return retval;
4036 }
4037
4038 default:
4039 /* Check for messages registered at runtime. */
4040 if (msg == msh_mousewheel)
4041 {
4042 wmsg.dwModifiers = w32_get_modifiers ();
4043 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4044 signal_user_input ();
4045 return 0;
4046 }
4047
4048 dflt:
4049 return DefWindowProc (hwnd, msg, wParam, lParam);
4050 }
4051
4052
4053 /* The most common default return code for handled messages is 0. */
4054 return 0;
4055}
4056
4057static void
4058my_create_window (f)
4059 struct frame * f;
4060{
4061 MSG msg;
4062
4063 if (!PostThreadMessage (dwWindowsThreadId, WM_EMACS_CREATEWINDOW, (WPARAM)f, 0))
4064 abort ();
4065 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
4066}
4067
4068
4069/* Create a tooltip window. Unlike my_create_window, we do not do this
4070 indirectly via the Window thread, as we do not need to process Window
4071 messages for the tooltip. Creating tooltips indirectly also creates
4072 deadlocks when tooltips are created for menu items. */
4073static void
4074my_create_tip_window (f)
4075 struct frame *f;
4076{
4077 RECT rect;
4078
4079 rect.left = rect.top = 0;
4080 rect.right = FRAME_PIXEL_WIDTH (f);
4081 rect.bottom = FRAME_PIXEL_HEIGHT (f);
4082
4083 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
4084 FRAME_EXTERNAL_MENU_BAR (f));
4085
4086 tip_window = FRAME_W32_WINDOW (f)
4087 = CreateWindow (EMACS_CLASS,
4088 f->namebuf,
4089 f->output_data.w32->dwStyle,
4090 f->left_pos,
4091 f->top_pos,
4092 rect.right - rect.left,
4093 rect.bottom - rect.top,
4094 FRAME_W32_WINDOW (SELECTED_FRAME ()), /* owner */
4095 NULL,
4096 hinst,
4097 NULL);
4098
4099 if (tip_window)
4100 {
4101 SetWindowLong (tip_window, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
4102 SetWindowLong (tip_window, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
4103 SetWindowLong (tip_window, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
4104 SetWindowLong (tip_window, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
4105
4106 /* Tip frames have no scrollbars. */
4107 SetWindowLong (tip_window, WND_SCROLLBAR_INDEX, 0);
4108
4109 /* Do this to discard the default setting specified by our parent. */
4110 ShowWindow (tip_window, SW_HIDE);
4111 }
4112}
4113
4114
4115/* Create and set up the w32 window for frame F. */
4116
4117static void
4118w32_window (f, window_prompting, minibuffer_only)
4119 struct frame *f;
4120 long window_prompting;
4121 int minibuffer_only;
4122{
4123 BLOCK_INPUT;
4124
4125 /* Use the resource name as the top-level window name
4126 for looking up resources. Make a non-Lisp copy
4127 for the window manager, so GC relocation won't bother it.
4128
4129 Elsewhere we specify the window name for the window manager. */
4130
4131 {
4132 char *str = (char *) SDATA (Vx_resource_name);
4133 f->namebuf = (char *) xmalloc (strlen (str) + 1);
4134 strcpy (f->namebuf, str);
4135 }
4136
4137 my_create_window (f);
4138
4139 validate_x_resource_name ();
4140
4141 /* x_set_name normally ignores requests to set the name if the
4142 requested name is the same as the current name. This is the one
4143 place where that assumption isn't correct; f->name is set, but
4144 the server hasn't been told. */
4145 {
4146 Lisp_Object name;
4147 int explicit = f->explicit_name;
4148
4149 f->explicit_name = 0;
4150 name = f->name;
4151 f->name = Qnil;
4152 x_set_name (f, name, explicit);
4153 }
4154
4155 UNBLOCK_INPUT;
4156
4157 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
4158 initialize_frame_menubar (f);
4159
4160 if (FRAME_W32_WINDOW (f) == 0)
4161 error ("Unable to create window");
4162}
4163
4164/* Handle the icon stuff for this window. Perhaps later we might
4165 want an x_set_icon_position which can be called interactively as
4166 well. */
4167
4168static void
4169x_icon (f, parms)
4170 struct frame *f;
4171 Lisp_Object parms;
4172{
4173 Lisp_Object icon_x, icon_y;
4174
4175 /* Set the position of the icon. Note that Windows 95 groups all
4176 icons in the tray. */
4177 icon_x = w32_get_arg (parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
4178 icon_y = w32_get_arg (parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
4179 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
4180 {
4181 CHECK_NUMBER (icon_x);
4182 CHECK_NUMBER (icon_y);
4183 }
4184 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
4185 error ("Both left and top icon corners of icon must be specified");
4186
4187 BLOCK_INPUT;
4188
4189 if (! EQ (icon_x, Qunbound))
4190 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
4191
4192#if 0 /* TODO */
4193 /* Start up iconic or window? */
4194 x_wm_set_window_state
4195 (f, (EQ (w32_get_arg (parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL), Qicon)
4196 ? IconicState
4197 : NormalState));
4198
4199 x_text_icon (f, (char *) SDATA ((!NILP (f->icon_name)
4200 ? f->icon_name
4201 : f->name)));
4202#endif
4203
4204 UNBLOCK_INPUT;
4205}
4206
4207
4208static void
4209x_make_gc (f)
4210 struct frame *f;
4211{
4212 XGCValues gc_values;
4213
4214 BLOCK_INPUT;
4215
4216 /* Create the GC's of this frame.
4217 Note that many default values are used. */
4218
4219 /* Normal video */
4220 gc_values.font = FRAME_FONT (f);
4221
4222 /* Cursor has cursor-color background, background-color foreground. */
4223 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
4224 gc_values.background = f->output_data.w32->cursor_pixel;
4225 f->output_data.w32->cursor_gc
4226 = XCreateGC (NULL, FRAME_W32_WINDOW (f),
4227 (GCFont | GCForeground | GCBackground),
4228 &gc_values);
4229
4230 /* Reliefs. */
4231 f->output_data.w32->white_relief.gc = 0;
4232 f->output_data.w32->black_relief.gc = 0;
4233
4234 UNBLOCK_INPUT;
4235}
4236
4237
4238/* Handler for signals raised during x_create_frame and
4239 x_create_top_frame. FRAME is the frame which is partially
4240 constructed. */
4241
4242static Lisp_Object
4243unwind_create_frame (frame)
4244 Lisp_Object frame;
4245{
4246 struct frame *f = XFRAME (frame);
4247
4248 /* If frame is ``official'', nothing to do. */
4249 if (!CONSP (Vframe_list) || !EQ (XCAR (Vframe_list), frame))
4250 {
4251#ifdef GLYPH_DEBUG
4252 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4253#endif
4254
4255 x_free_frame_resources (f);
4256
4257 /* Check that reference counts are indeed correct. */
4258 xassert (dpyinfo->reference_count == dpyinfo_refcount);
4259 xassert (dpyinfo->image_cache->refcount == image_cache_refcount);
4260
4261 return Qt;
4262 }
4263
4264 return Qnil;
4265}
4266
4267#ifdef USE_FONT_BACKEND
4268static void
4269x_default_font_parameter (f, parms)
4270 struct frame *f;
4271 Lisp_Object parms;
4272{
4273 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4274 Lisp_Object font = x_get_arg (dpyinfo, parms, Qfont, "font", "Font",
4275 RES_TYPE_STRING);
4276
4277 if (!STRINGP (font))
4278 {
4279 int i;
4280 static char *names[]
4281 = { "Courier New-10",
4282 "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1",
4283 "-*-Fixedsys-normal-r-*-*-12-*-*-*-c-*-iso8859-1",
4284 "Fixedsys",
4285 NULL };
4286
4287 for (i = 0; names[i]; i++)
4288 {
4289 font = font_open_by_name (f, names[i]);
4290 if (! NILP (font))
4291 break;
4292 }
4293 if (NILP (font))
4294 error ("No suitable font was found");
4295 }
4296 x_default_parameter (f, parms, Qfont, font, "font", "Font", RES_TYPE_STRING);
4297}
4298#endif
4299
4300DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
4301 1, 1, 0,
4302 doc: /* Make a new window, which is called a \"frame\" in Emacs terms.
4303Return an Emacs frame object.
4304PARAMETERS is an alist of frame parameters.
4305If the parameters specify that the frame should not have a minibuffer,
4306and do not specify a specific minibuffer window to use,
4307then `default-minibuffer-frame' must be a frame whose minibuffer can
4308be shared by the new frame.
4309
4310This function is an internal primitive--use `make-frame' instead. */)
4311 (parameters)
4312 Lisp_Object parameters;
4313{
4314 struct frame *f;
4315 Lisp_Object frame, tem;
4316 Lisp_Object name;
4317 int minibuffer_only = 0;
4318 long window_prompting = 0;
4319 int width, height;
4320 int count = SPECPDL_INDEX ();
4321 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4322 Lisp_Object display;
4323 struct w32_display_info *dpyinfo = NULL;
4324 Lisp_Object parent;
4325 struct kboard *kb;
4326
4327 check_w32 ();
4328
4329 /* Make copy of frame parameters because the original is in pure
4330 storage now. */
4331 parameters = Fcopy_alist (parameters);
4332
4333 /* Use this general default value to start with
4334 until we know if this frame has a specified name. */
4335 Vx_resource_name = Vinvocation_name;
4336
4337 display = w32_get_arg (parameters, Qdisplay, 0, 0, RES_TYPE_STRING);
4338 if (EQ (display, Qunbound))
4339 display = Qnil;
4340 dpyinfo = check_x_display_info (display);
4341#ifdef MULTI_KBOARD
4342 kb = dpyinfo->terminal->kboard;
4343#else
4344 kb = &the_only_kboard;
4345#endif
4346
4347 name = w32_get_arg (parameters, Qname, "name", "Name", RES_TYPE_STRING);
4348 if (!STRINGP (name)
4349 && ! EQ (name, Qunbound)
4350 && ! NILP (name))
4351 error ("Invalid frame name--not a string or nil");
4352
4353 if (STRINGP (name))
4354 Vx_resource_name = name;
4355
4356 /* See if parent window is specified. */
4357 parent = w32_get_arg (parameters, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
4358 if (EQ (parent, Qunbound))
4359 parent = Qnil;
4360 if (! NILP (parent))
4361 CHECK_NUMBER (parent);
4362
4363 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
4364 /* No need to protect DISPLAY because that's not used after passing
4365 it to make_frame_without_minibuffer. */
4366 frame = Qnil;
4367 GCPRO4 (parameters, parent, name, frame);
4368 tem = w32_get_arg (parameters, Qminibuffer, "minibuffer", "Minibuffer",
4369 RES_TYPE_SYMBOL);
4370 if (EQ (tem, Qnone) || NILP (tem))
4371 f = make_frame_without_minibuffer (Qnil, kb, display);
4372 else if (EQ (tem, Qonly))
4373 {
4374 f = make_minibuffer_frame ();
4375 minibuffer_only = 1;
4376 }
4377 else if (WINDOWP (tem))
4378 f = make_frame_without_minibuffer (tem, kb, display);
4379 else
4380 f = make_frame (1);
4381
4382 XSETFRAME (frame, f);
4383
4384 /* Note that Windows does support scroll bars. */
4385 FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;
4386
4387 /* By default, make scrollbars the system standard width. */
4388 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = GetSystemMetrics (SM_CXVSCROLL);
4389
4390 f->terminal = dpyinfo->terminal;
4391 f->terminal->reference_count++;
4392
4393 f->output_method = output_w32;
4394 f->output_data.w32 =
4395 (struct w32_output *) xmalloc (sizeof (struct w32_output));
4396 bzero (f->output_data.w32, sizeof (struct w32_output));
4397 FRAME_FONTSET (f) = -1;
4398 record_unwind_protect (unwind_create_frame, frame);
4399
4400 f->icon_name
4401 = w32_get_arg (parameters, Qicon_name, "iconName", "Title", RES_TYPE_STRING);
4402 if (! STRINGP (f->icon_name))
4403 f->icon_name = Qnil;
4404
4405/* FRAME_W32_DISPLAY_INFO (f) = dpyinfo; */
4406#ifdef MULTI_KBOARD
4407 FRAME_KBOARD (f) = kb;
4408#endif
4409
4410 /* Specify the parent under which to make this window. */
4411
4412 if (!NILP (parent))
4413 {
4414 f->output_data.w32->parent_desc = (Window) XFASTINT (parent);
4415 f->output_data.w32->explicit_parent = 1;
4416 }
4417 else
4418 {
4419 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
4420 f->output_data.w32->explicit_parent = 0;
4421 }
4422
4423 /* Set the name; the functions to which we pass f expect the name to
4424 be set. */
4425 if (EQ (name, Qunbound) || NILP (name))
4426 {
4427 f->name = build_string (dpyinfo->w32_id_name);
4428 f->explicit_name = 0;
4429 }
4430 else
4431 {
4432 f->name = name;
4433 f->explicit_name = 1;
4434 /* use the frame's title when getting resources for this frame. */
4435 specbind (Qx_resource_name, name);
4436 }
4437
4438 f->resx = dpyinfo->resx;
4439 f->resy = dpyinfo->resy;
4440
4441#ifdef USE_FONT_BACKEND
4442 if (enable_font_backend)
4443 {
4444 /* Perhaps, we must allow frame parameter, say `font-backend',
4445 to specify which font backends to use. */
4446 register_font_driver (&w32font_driver, f);
4447
4448 x_default_parameter (f, parameters, Qfont_backend, Qnil,
4449 "fontBackend", "FontBackend", RES_TYPE_STRING);
4450 }
4451#endif /* USE_FONT_BACKEND */
4452
4453 /* Extract the window parameters from the supplied values
4454 that are needed to determine window geometry. */
4455#ifdef USE_FONT_BACKEND
4456 if (enable_font_backend)
4457 x_default_font_parameter (f, parameters);
4458 else
4459#endif
4460 {
4461 Lisp_Object font;
4462
4463 font = w32_get_arg (parameters, Qfont, "font", "Font", RES_TYPE_STRING);
4464
4465 BLOCK_INPUT;
4466 /* First, try whatever font the caller has specified. */
4467 if (STRINGP (font))
4468 {
4469 tem = Fquery_fontset (font, Qnil);
4470 if (STRINGP (tem))
4471 font = x_new_fontset (f, tem);
4472 else
4473 font = x_new_font (f, SDATA (font));
4474 }
4475 /* Try out a font which we hope has bold and italic variations. */
4476 if (!STRINGP (font))
4477 font = x_new_font (f, "-*-Courier New-normal-r-*-*-*-100-*-*-c-*-iso8859-1");
4478 if (! STRINGP (font))
4479 font = x_new_font (f, "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1");
4480 /* If those didn't work, look for something which will at least work. */
4481 if (! STRINGP (font))
4482 font = x_new_font (f, "-*-Fixedsys-normal-r-*-*-12-*-*-*-c-*-iso8859-1");
4483 UNBLOCK_INPUT;
4484 if (! STRINGP (font))
4485 font = build_string ("Fixedsys");
4486
4487 x_default_parameter (f, parameters, Qfont, font,
4488 "font", "Font", RES_TYPE_STRING);
4489 }
4490
4491 x_default_parameter (f, parameters, Qborder_width, make_number (2),
4492 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
4493 /* This defaults to 2 in order to match xterm. We recognize either
4494 internalBorderWidth or internalBorder (which is what xterm calls
4495 it). */
4496 if (NILP (Fassq (Qinternal_border_width, parameters)))
4497 {
4498 Lisp_Object value;
4499
4500 value = w32_get_arg (parameters, Qinternal_border_width,
4501 "internalBorder", "InternalBorder", RES_TYPE_NUMBER);
4502 if (! EQ (value, Qunbound))
4503 parameters = Fcons (Fcons (Qinternal_border_width, value),
4504 parameters);
4505 }
4506 /* Default internalBorderWidth to 0 on Windows to match other programs. */
4507 x_default_parameter (f, parameters, Qinternal_border_width, make_number (0),
4508 "internalBorderWidth", "InternalBorder", RES_TYPE_NUMBER);
4509 x_default_parameter (f, parameters, Qvertical_scroll_bars, Qright,
4510 "verticalScrollBars", "ScrollBars", RES_TYPE_SYMBOL);
4511
4512 /* Also do the stuff which must be set before the window exists. */
4513 x_default_parameter (f, parameters, Qforeground_color, build_string ("black"),
4514 "foreground", "Foreground", RES_TYPE_STRING);
4515 x_default_parameter (f, parameters, Qbackground_color, build_string ("white"),
4516 "background", "Background", RES_TYPE_STRING);
4517 x_default_parameter (f, parameters, Qmouse_color, build_string ("black"),
4518 "pointerColor", "Foreground", RES_TYPE_STRING);
4519 x_default_parameter (f, parameters, Qcursor_color, build_string ("black"),
4520 "cursorColor", "Foreground", RES_TYPE_STRING);
4521 x_default_parameter (f, parameters, Qborder_color, build_string ("black"),
4522 "borderColor", "BorderColor", RES_TYPE_STRING);
4523 x_default_parameter (f, parameters, Qscreen_gamma, Qnil,
4524 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
4525 x_default_parameter (f, parameters, Qline_spacing, Qnil,
4526 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
4527 x_default_parameter (f, parameters, Qleft_fringe, Qnil,
4528 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
4529 x_default_parameter (f, parameters, Qright_fringe, Qnil,
4530 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
4531
4532
4533 /* Init faces before x_default_parameter is called for scroll-bar
4534 parameters because that function calls x_set_scroll_bar_width,
4535 which calls change_frame_size, which calls Fset_window_buffer,
4536 which runs hooks, which call Fvertical_motion. At the end, we
4537 end up in init_iterator with a null face cache, which should not
4538 happen. */
4539 init_frame_faces (f);
4540
4541 x_default_parameter (f, parameters, Qmenu_bar_lines, make_number (1),
4542 "menuBar", "MenuBar", RES_TYPE_NUMBER);
4543 x_default_parameter (f, parameters, Qtool_bar_lines, make_number (1),
4544 "toolBar", "ToolBar", RES_TYPE_NUMBER);
4545
4546 x_default_parameter (f, parameters, Qbuffer_predicate, Qnil,
4547 "bufferPredicate", "BufferPredicate", RES_TYPE_SYMBOL);
4548 x_default_parameter (f, parameters, Qtitle, Qnil,
4549 "title", "Title", RES_TYPE_STRING);
4550 x_default_parameter (f, parameters, Qfullscreen, Qnil,
4551 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
4552
4553 f->output_data.w32->dwStyle = WS_OVERLAPPEDWINDOW;
4554 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
4555
4556 f->output_data.w32->text_cursor = w32_load_cursor (IDC_IBEAM);
4557 f->output_data.w32->nontext_cursor = w32_load_cursor (IDC_ARROW);
4558 f->output_data.w32->modeline_cursor = w32_load_cursor (IDC_ARROW);
4559 f->output_data.w32->hand_cursor = w32_load_cursor (IDC_HAND);
4560 f->output_data.w32->hourglass_cursor = w32_load_cursor (IDC_WAIT);
4561 f->output_data.w32->horizontal_drag_cursor = w32_load_cursor (IDC_SIZEWE);
4562
4563 f->output_data.w32->current_cursor = f->output_data.w32->nontext_cursor;
4564
4565 window_prompting = x_figure_window_size (f, parameters, 1);
4566
4567 tem = w32_get_arg (parameters, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
4568 f->no_split = minibuffer_only || EQ (tem, Qt);
4569
4570 w32_window (f, window_prompting, minibuffer_only);
4571 x_icon (f, parameters);
4572
4573 x_make_gc (f);
4574
4575 /* Now consider the frame official. */
4576 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
4577 Vframe_list = Fcons (frame, Vframe_list);
4578
4579 /* We need to do this after creating the window, so that the
4580 icon-creation functions can say whose icon they're describing. */
4581 x_default_parameter (f, parameters, Qicon_type, Qnil,
4582 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
4583
4584 x_default_parameter (f, parameters, Qauto_raise, Qnil,
4585 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4586 x_default_parameter (f, parameters, Qauto_lower, Qnil,
4587 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4588 x_default_parameter (f, parameters, Qcursor_type, Qbox,
4589 "cursorType", "CursorType", RES_TYPE_SYMBOL);
4590 x_default_parameter (f, parameters, Qscroll_bar_width, Qnil,
4591 "scrollBarWidth", "ScrollBarWidth", RES_TYPE_NUMBER);
4592
4593 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
4594 Change will not be effected unless different from the current
4595 FRAME_LINES (f). */
4596 width = FRAME_COLS (f);
4597 height = FRAME_LINES (f);
4598
4599 FRAME_LINES (f) = 0;
4600 SET_FRAME_COLS (f, 0);
4601 change_frame_size (f, height, width, 1, 0, 0);
4602
4603 /* Tell the server what size and position, etc, we want, and how
4604 badly we want them. This should be done after we have the menu
4605 bar so that its size can be taken into account. */
4606 BLOCK_INPUT;
4607 x_wm_set_size_hint (f, window_prompting, 0);
4608 UNBLOCK_INPUT;
4609
4610 /* Make the window appear on the frame and enable display, unless
4611 the caller says not to. However, with explicit parent, Emacs
4612 cannot control visibility, so don't try. */
4613 if (! f->output_data.w32->explicit_parent)
4614 {
4615 Lisp_Object visibility;
4616
4617 visibility = w32_get_arg (parameters, Qvisibility, 0, 0, RES_TYPE_SYMBOL);
4618 if (EQ (visibility, Qunbound))
4619 visibility = Qt;
4620
4621 if (EQ (visibility, Qicon))
4622 x_iconify_frame (f);
4623 else if (! NILP (visibility))
4624 x_make_frame_visible (f);
4625 else
4626 /* Must have been Qnil. */
4627 ;
4628 }
4629
4630 /* Initialize `default-minibuffer-frame' in case this is the first
4631 frame on this terminal. */
4632 if (FRAME_HAS_MINIBUF_P (f)
4633 && (!FRAMEP (kb->Vdefault_minibuffer_frame)
4634 || !FRAME_LIVE_P (XFRAME (kb->Vdefault_minibuffer_frame))))
4635 kb->Vdefault_minibuffer_frame = frame;
4636
4637 /* All remaining specified parameters, which have not been "used"
4638 by x_get_arg and friends, now go in the misc. alist of the frame. */
4639 for (tem = parameters; CONSP (tem); tem = XCDR (tem))
4640 if (CONSP (XCAR (tem)) && !NILP (XCAR (XCAR (tem))))
4641 f->param_alist = Fcons (XCAR (tem), f->param_alist);
4642
4643 UNGCPRO;
4644
4645 /* Make sure windows on this frame appear in calls to next-window
4646 and similar functions. */
4647 Vwindow_list = Qnil;
4648
4649 return unbind_to (count, frame);
4650}
4651
4652/* FRAME is used only to get a handle on the X display. We don't pass the
4653 display info directly because we're called from frame.c, which doesn't
4654 know about that structure. */
4655Lisp_Object
4656x_get_focus_frame (frame)
4657 struct frame *frame;
4658{
4659 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (frame);
4660 Lisp_Object xfocus;
4661 if (! dpyinfo->w32_focus_frame)
4662 return Qnil;
4663
4664 XSETFRAME (xfocus, dpyinfo->w32_focus_frame);
4665 return xfocus;
4666}
4667
4668DEFUN ("x-focus-frame", Fx_focus_frame, Sx_focus_frame, 1, 1, 0,
4669 doc: /* Give FRAME input focus, raising to foreground if necessary. */)
4670 (frame)
4671 Lisp_Object frame;
4672{
4673 x_focus_on_frame (check_x_frame (frame));
4674 return Qnil;
4675}
4676
4677\f
4678/* Return the charset portion of a font name. */
4679char *
4680xlfd_charset_of_font (char * fontname)
4681{
4682 char *charset, *encoding;
4683
4684 encoding = strrchr (fontname, '-');
4685 if (!encoding || encoding == fontname)
4686 return NULL;
4687
4688 for (charset = encoding - 1; charset >= fontname; charset--)
4689 if (*charset == '-')
4690 break;
4691
4692 if (charset == fontname || strcmp (charset, "-*-*") == 0)
4693 return NULL;
4694
4695 return charset + 1;
4696}
4697
4698struct font_info *w32_load_bdf_font (struct frame *f, char *fontname,
4699 int size, char* filename);
4700static Lisp_Object w32_list_bdf_fonts (Lisp_Object pattern, int max_names);
4701static BOOL w32_to_x_font (LOGFONT * lplf, char * lpxstr, int len,
4702 char * charset);
4703static BOOL x_to_w32_font (char *lpxstr, LOGFONT *lplogfont);
4704
4705static struct font_info *
4706w32_load_system_font (f, fontname, size)
4707 struct frame *f;
4708 char * fontname;
4709 int size;
4710{
4711 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4712 Lisp_Object font_names;
4713
4714 /* Get a list of all the fonts that match this name. Once we
4715 have a list of matching fonts, we compare them against the fonts
4716 we already have loaded by comparing names. */
4717 font_names = w32_list_fonts (f, build_string (fontname), size, 100);
4718
4719 if (!NILP (font_names))
4720 {
4721 Lisp_Object tail;
4722 int i;
4723
4724 /* First check if any are already loaded, as that is cheaper
4725 than loading another one. */
4726 for (i = 0; i < dpyinfo->n_fonts; i++)
4727 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
4728 if (dpyinfo->font_table[i].name
4729 && (!strcmp (dpyinfo->font_table[i].name,
4730 SDATA (XCAR (tail)))
4731 || !strcmp (dpyinfo->font_table[i].full_name,
4732 SDATA (XCAR (tail)))))
4733 return (dpyinfo->font_table + i);
4734
4735 fontname = (char *) SDATA (XCAR (font_names));
4736 }
4737 else if (w32_strict_fontnames)
4738 {
4739 /* If EnumFontFamiliesEx was available, we got a full list of
4740 fonts back so stop now to avoid the possibility of loading a
4741 random font. If we had to fall back to EnumFontFamilies, the
4742 list is incomplete, so continue whether the font we want was
4743 listed or not. */
4744 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
4745 FARPROC enum_font_families_ex
4746 = GetProcAddress (gdi32, "EnumFontFamiliesExA");
4747 if (enum_font_families_ex)
4748 return NULL;
4749 }
4750
4751 /* Load the font and add it to the table. */
4752 {
4753 char *full_name, *encoding, *charset;
4754 XFontStruct *font;
4755 struct font_info *fontp;
4756 LOGFONT lf;
4757 BOOL ok;
4758 int codepage;
4759 int i;
4760
4761 if (!fontname || !x_to_w32_font (fontname, &lf))
4762 return (NULL);
4763
4764 if (!*lf.lfFaceName)
4765 /* If no name was specified for the font, we get a random font
4766 from CreateFontIndirect - this is not particularly
4767 desirable, especially since CreateFontIndirect does not
4768 fill out the missing name in lf, so we never know what we
4769 ended up with. */
4770 return NULL;
4771
4772 lf.lfQuality = DEFAULT_QUALITY;
4773
4774 font = (XFontStruct *) xmalloc (sizeof (XFontStruct));
4775 bzero (font, sizeof (*font));
4776
4777 /* Set bdf to NULL to indicate that this is a Windows font. */
4778 font->bdf = NULL;
4779
4780 BLOCK_INPUT;
4781
4782 font->hfont = CreateFontIndirect (&lf);
4783
4784 if (font->hfont == NULL)
4785 {
4786 ok = FALSE;
4787 }
4788 else
4789 {
4790 HDC hdc;
4791 HANDLE oldobj;
4792
4793 codepage = w32_codepage_for_font (fontname);
4794
4795 hdc = GetDC (dpyinfo->root_window);
4796 oldobj = SelectObject (hdc, font->hfont);
4797
4798 ok = GetTextMetrics (hdc, &font->tm);
4799 if (codepage == CP_UNICODE)
4800 font->double_byte_p = 1;
4801 else
4802 {
4803 /* Unfortunately, some fonts (eg. MingLiU, a big5 ttf font)
4804 don't report themselves as double byte fonts, when
4805 patently they are. So instead of trusting
4806 GetFontLanguageInfo, we check the properties of the
4807 codepage directly, since that is ultimately what we are
4808 working from anyway. */
4809 /* font->double_byte_p = GetFontLanguageInfo (hdc) & GCP_DBCS; */
4810 CPINFO cpi = {0};
4811 GetCPInfo (codepage, &cpi);
4812 font->double_byte_p = cpi.MaxCharSize > 1;
4813 }
4814
4815 SelectObject (hdc, oldobj);
4816 ReleaseDC (dpyinfo->root_window, hdc);
4817 /* Fill out details in lf according to the font that was
4818 actually loaded. */
4819 lf.lfHeight = font->tm.tmInternalLeading - font->tm.tmHeight;
4820 lf.lfWidth = font->tm.tmMaxCharWidth;
4821 lf.lfWeight = font->tm.tmWeight;
4822 lf.lfItalic = font->tm.tmItalic;
4823 lf.lfCharSet = font->tm.tmCharSet;
4824 lf.lfPitchAndFamily = ((font->tm.tmPitchAndFamily & TMPF_FIXED_PITCH)
4825 ? VARIABLE_PITCH : FIXED_PITCH);
4826 lf.lfOutPrecision = ((font->tm.tmPitchAndFamily & TMPF_VECTOR)
4827 ? OUT_STROKE_PRECIS : OUT_STRING_PRECIS);
4828
4829 w32_cache_char_metrics (font);
4830 }
4831
4832 UNBLOCK_INPUT;
4833
4834 if (!ok)
4835 {
4836 w32_unload_font (dpyinfo, font);
4837 return (NULL);
4838 }
4839
4840 /* Find a free slot in the font table. */
4841 for (i = 0; i < dpyinfo->n_fonts; ++i)
4842 if (dpyinfo->font_table[i].name == NULL)
4843 break;
4844
4845 /* If no free slot found, maybe enlarge the font table. */
4846 if (i == dpyinfo->n_fonts
4847 && dpyinfo->n_fonts == dpyinfo->font_table_size)
4848 {
4849 int sz;
4850 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
4851 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
4852 dpyinfo->font_table
4853 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
4854 }
4855
4856 fontp = dpyinfo->font_table + i;
4857 if (i == dpyinfo->n_fonts)
4858 ++dpyinfo->n_fonts;
4859
4860 /* Now fill in the slots of *FONTP. */
4861 BLOCK_INPUT;
4862 bzero (fontp, sizeof (*fontp));
4863 fontp->font = font;
4864 fontp->font_idx = i;
4865 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
4866 bcopy (fontname, fontp->name, strlen (fontname) + 1);
4867
4868 if ((lf.lfPitchAndFamily & 0x03) == FIXED_PITCH)
4869 {
4870 /* Fixed width font. */
4871 fontp->average_width = fontp->space_width = FONT_AVG_WIDTH (font);
4872 }
4873 else
4874 {
4875 wchar_t space = 32;
4876 XCharStruct* pcm;
4877 pcm = w32_per_char_metric (font, &space, ANSI_FONT);
4878 if (pcm)
4879 fontp->space_width = pcm->width;
4880 else
4881 fontp->space_width = FONT_AVG_WIDTH (font);
4882
4883 fontp->average_width = font->tm.tmAveCharWidth;
4884 }
4885
4886 fontp->charset = -1;
4887 charset = xlfd_charset_of_font (fontname);
4888
4889 /* Cache the W32 codepage for a font. This makes w32_encode_char
4890 (called for every glyph during redisplay) much faster. */
4891 fontp->codepage = codepage;
4892
4893 /* Work out the font's full name. */
4894 full_name = (char *)xmalloc (100);
4895 if (full_name && w32_to_x_font (&lf, full_name, 100, charset))
4896 fontp->full_name = full_name;
4897 else
4898 {
4899 /* If all else fails - just use the name we used to load it. */
4900 xfree (full_name);
4901 fontp->full_name = fontp->name;
4902 }
4903
4904 fontp->size = FONT_WIDTH (font);
4905 fontp->height = FONT_HEIGHT (font);
4906
4907 /* The slot `encoding' specifies how to map a character
4908 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
4909 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
4910 (0:0x20..0x7F, 1:0xA0..0xFF,
4911 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
4912 2:0xA020..0xFF7F). For the moment, we don't know which charset
4913 uses this font. So, we set information in fontp->encoding_type
4914 which is never used by any charset. If mapping can't be
4915 decided, set FONT_ENCODING_NOT_DECIDED. */
4916
4917 /* SJIS fonts need to be set to type 4, all others seem to work as
4918 type FONT_ENCODING_NOT_DECIDED. */
4919 encoding = strrchr (fontp->name, '-');
4920 if (encoding && strnicmp (encoding+1, "sjis", 4) == 0)
4921 fontp->encoding_type = 4;
4922 else
4923 fontp->encoding_type = FONT_ENCODING_NOT_DECIDED;
4924
4925 /* The following three values are set to 0 under W32, which is
4926 what they get set to if XGetFontProperty fails under X. */
4927 fontp->baseline_offset = 0;
4928 fontp->relative_compose = 0;
4929 fontp->default_ascent = 0;
4930
4931 /* Set global flag fonts_changed_p to non-zero if the font loaded
4932 has a character with a smaller width than any other character
4933 before, or if the font loaded has a smaller height than any
4934 other font loaded before. If this happens, it will make a
4935 glyph matrix reallocation necessary. */
4936 fonts_changed_p |= x_compute_min_glyph_bounds (f);
4937 UNBLOCK_INPUT;
4938 return fontp;
4939 }
4940}
4941
4942/* Load font named FONTNAME of size SIZE for frame F, and return a
4943 pointer to the structure font_info while allocating it dynamically.
4944 If loading fails, return NULL. */
4945struct font_info *
4946w32_load_font (f, fontname, size)
4947 struct frame *f;
4948 char * fontname;
4949 int size;
4950{
4951 Lisp_Object bdf_fonts;
4952 struct font_info *retval = NULL;
4953 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4954
4955 bdf_fonts = w32_list_bdf_fonts (build_string (fontname), 1);
4956
4957 while (!retval && CONSP (bdf_fonts))
4958 {
4959 char *bdf_name, *bdf_file;
4960 Lisp_Object bdf_pair;
4961 int i;
4962
4963 bdf_name = SDATA (XCAR (bdf_fonts));
4964 bdf_pair = Fassoc (XCAR (bdf_fonts), Vw32_bdf_filename_alist);
4965 bdf_file = SDATA (XCDR (bdf_pair));
4966
4967 /* If the font is already loaded, do not load it again. */
4968 for (i = 0; i < dpyinfo->n_fonts; i++)
4969 {
4970 if ((dpyinfo->font_table[i].name
4971 && !strcmp (dpyinfo->font_table[i].name, bdf_name))
4972 || (dpyinfo->font_table[i].full_name
4973 && !strcmp (dpyinfo->font_table[i].full_name, bdf_name)))
4974 return dpyinfo->font_table + i;
4975 }
4976
4977 retval = w32_load_bdf_font (f, bdf_name, size, bdf_file);
4978
4979 bdf_fonts = XCDR (bdf_fonts);
4980 }
4981
4982 if (retval)
4983 return retval;
4984
4985 return w32_load_system_font (f, fontname, size);
4986}
4987
4988
4989void
4990w32_unload_font (dpyinfo, font)
4991 struct w32_display_info *dpyinfo;
4992 XFontStruct * font;
4993{
4994 if (font)
4995 {
4996 if (font->per_char) xfree (font->per_char);
4997 if (font->bdf) w32_free_bdf_font (font->bdf);
4998
4999 if (font->hfont) DeleteObject (font->hfont);
5000 xfree (font);
5001 }
5002}
5003
5004/* The font conversion stuff between x and w32 */
5005
5006/* X font string is as follows (from faces.el)
5007 * (let ((- "[-?]")
5008 * (foundry "[^-]+")
5009 * (family "[^-]+")
5010 * (weight "\\(bold\\|demibold\\|medium\\)") ; 1
5011 * (weight\? "\\([^-]*\\)") ; 1
5012 * (slant "\\([ior]\\)") ; 2
5013 * (slant\? "\\([^-]?\\)") ; 2
5014 * (swidth "\\([^-]*\\)") ; 3
5015 * (adstyle "[^-]*") ; 4
5016 * (pixelsize "[0-9]+")
5017 * (pointsize "[0-9][0-9]+")
5018 * (resx "[0-9][0-9]+")
5019 * (resy "[0-9][0-9]+")
5020 * (spacing "[cmp?*]")
5021 * (avgwidth "[0-9]+")
5022 * (registry "[^-]+")
5023 * (encoding "[^-]+")
5024 * )
5025 */
5026
5027static LONG
5028x_to_w32_weight (lpw)
5029 char * lpw;
5030{
5031 if (!lpw) return (FW_DONTCARE);
5032
5033 if (stricmp (lpw, "heavy") == 0) return FW_HEAVY;
5034 else if (stricmp (lpw, "extrabold") == 0) return FW_EXTRABOLD;
5035 else if (stricmp (lpw, "bold") == 0) return FW_BOLD;
5036 else if (stricmp (lpw, "demibold") == 0) return FW_SEMIBOLD;
5037 else if (stricmp (lpw, "semibold") == 0) return FW_SEMIBOLD;
5038 else if (stricmp (lpw, "medium") == 0) return FW_MEDIUM;
5039 else if (stricmp (lpw, "normal") == 0) return FW_NORMAL;
5040 else if (stricmp (lpw, "light") == 0) return FW_LIGHT;
5041 else if (stricmp (lpw, "extralight") == 0) return FW_EXTRALIGHT;
5042 else if (stricmp (lpw, "thin") == 0) return FW_THIN;
5043 else
5044 return FW_DONTCARE;
5045}
5046
5047
5048static char *
5049w32_to_x_weight (fnweight)
5050 int fnweight;
5051{
5052 if (fnweight >= FW_HEAVY) return "heavy";
5053 if (fnweight >= FW_EXTRABOLD) return "extrabold";
5054 if (fnweight >= FW_BOLD) return "bold";
5055 if (fnweight >= FW_SEMIBOLD) return "demibold";
5056 if (fnweight >= FW_MEDIUM) return "medium";
5057 if (fnweight >= FW_NORMAL) return "normal";
5058 if (fnweight >= FW_LIGHT) return "light";
5059 if (fnweight >= FW_EXTRALIGHT) return "extralight";
5060 if (fnweight >= FW_THIN) return "thin";
5061 else
5062 return "*";
5063}
5064
5065LONG
5066x_to_w32_charset (lpcs)
5067 char * lpcs;
5068{
5069 Lisp_Object this_entry, w32_charset;
5070 char *charset;
5071 int len = strlen (lpcs);
5072
5073 /* Support "*-#nnn" format for unknown charsets. */
5074 if (strncmp (lpcs, "*-#", 3) == 0)
5075 return atoi (lpcs + 3);
5076
5077 /* All Windows fonts qualify as unicode. */
5078 if (!strncmp (lpcs, "iso10646", 8))
5079 return DEFAULT_CHARSET;
5080
5081 /* Handle wildcards by ignoring them; eg. treat "big5*-*" as "big5". */
5082 charset = alloca (len + 1);
5083 strcpy (charset, lpcs);
5084 lpcs = strchr (charset, '*');
5085 if (lpcs)
5086 *lpcs = '\0';
5087
5088 /* Look through w32-charset-info-alist for the character set.
5089 Format of each entry is
5090 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
5091 */
5092 this_entry = Fassoc (build_string (charset), Vw32_charset_info_alist);
5093
5094 if (NILP (this_entry))
5095 {
5096 /* At startup, we want iso8859-1 fonts to come up properly. */
5097 if (stricmp (charset, "iso8859-1") == 0)
5098 return ANSI_CHARSET;
5099 else
5100 return DEFAULT_CHARSET;
5101 }
5102
5103 w32_charset = Fcar (Fcdr (this_entry));
5104
5105 /* Translate Lisp symbol to number. */
5106 if (EQ (w32_charset, Qw32_charset_ansi))
5107 return ANSI_CHARSET;
5108 if (EQ (w32_charset, Qw32_charset_symbol))
5109 return SYMBOL_CHARSET;
5110 if (EQ (w32_charset, Qw32_charset_shiftjis))
5111 return SHIFTJIS_CHARSET;
5112 if (EQ (w32_charset, Qw32_charset_hangeul))
5113 return HANGEUL_CHARSET;
5114 if (EQ (w32_charset, Qw32_charset_chinesebig5))
5115 return CHINESEBIG5_CHARSET;
5116 if (EQ (w32_charset, Qw32_charset_gb2312))
5117 return GB2312_CHARSET;
5118 if (EQ (w32_charset, Qw32_charset_oem))
5119 return OEM_CHARSET;
5120#ifdef JOHAB_CHARSET
5121 if (EQ (w32_charset, Qw32_charset_johab))
5122 return JOHAB_CHARSET;
5123 if (EQ (w32_charset, Qw32_charset_easteurope))
5124 return EASTEUROPE_CHARSET;
5125 if (EQ (w32_charset, Qw32_charset_turkish))
5126 return TURKISH_CHARSET;
5127 if (EQ (w32_charset, Qw32_charset_baltic))
5128 return BALTIC_CHARSET;
5129 if (EQ (w32_charset, Qw32_charset_russian))
5130 return RUSSIAN_CHARSET;
5131 if (EQ (w32_charset, Qw32_charset_arabic))
5132 return ARABIC_CHARSET;
5133 if (EQ (w32_charset, Qw32_charset_greek))
5134 return GREEK_CHARSET;
5135 if (EQ (w32_charset, Qw32_charset_hebrew))
5136 return HEBREW_CHARSET;
5137 if (EQ (w32_charset, Qw32_charset_vietnamese))
5138 return VIETNAMESE_CHARSET;
5139 if (EQ (w32_charset, Qw32_charset_thai))
5140 return THAI_CHARSET;
5141 if (EQ (w32_charset, Qw32_charset_mac))
5142 return MAC_CHARSET;
5143#endif /* JOHAB_CHARSET */
5144#ifdef UNICODE_CHARSET
5145 if (EQ (w32_charset, Qw32_charset_unicode))
5146 return UNICODE_CHARSET;
5147#endif
5148
5149 return DEFAULT_CHARSET;
5150}
5151
5152
5153char *
5154w32_to_x_charset (fncharset, matching)
5155 int fncharset;
5156 char *matching;
5157{
5158 static char buf[32];
5159 Lisp_Object charset_type;
5160 int match_len = 0;
5161
5162 if (matching)
5163 {
5164 /* If fully specified, accept it as it is. Otherwise use a
5165 substring match. */
5166 char *wildcard = strchr (matching, '*');
5167 if (wildcard)
5168 *wildcard = '\0';
5169 else if (strchr (matching, '-'))
5170 return matching;
5171
5172 match_len = strlen (matching);
5173 }
5174
5175 switch (fncharset)
5176 {
5177 case ANSI_CHARSET:
5178 /* Handle startup case of w32-charset-info-alist not
5179 being set up yet. */
5180 if (NILP (Vw32_charset_info_alist))
5181 return "iso8859-1";
5182 charset_type = Qw32_charset_ansi;
5183 break;
5184 case DEFAULT_CHARSET:
5185 charset_type = Qw32_charset_default;
5186 break;
5187 case SYMBOL_CHARSET:
5188 charset_type = Qw32_charset_symbol;
5189 break;
5190 case SHIFTJIS_CHARSET:
5191 charset_type = Qw32_charset_shiftjis;
5192 break;
5193 case HANGEUL_CHARSET:
5194 charset_type = Qw32_charset_hangeul;
5195 break;
5196 case GB2312_CHARSET:
5197 charset_type = Qw32_charset_gb2312;
5198 break;
5199 case CHINESEBIG5_CHARSET:
5200 charset_type = Qw32_charset_chinesebig5;
5201 break;
5202 case OEM_CHARSET:
5203 charset_type = Qw32_charset_oem;
5204 break;
5205
5206 /* More recent versions of Windows (95 and NT4.0) define more
5207 character sets. */
5208#ifdef EASTEUROPE_CHARSET
5209 case EASTEUROPE_CHARSET:
5210 charset_type = Qw32_charset_easteurope;
5211 break;
5212 case TURKISH_CHARSET:
5213 charset_type = Qw32_charset_turkish;
5214 break;
5215 case BALTIC_CHARSET:
5216 charset_type = Qw32_charset_baltic;
5217 break;
5218 case RUSSIAN_CHARSET:
5219 charset_type = Qw32_charset_russian;
5220 break;
5221 case ARABIC_CHARSET:
5222 charset_type = Qw32_charset_arabic;
5223 break;
5224 case GREEK_CHARSET:
5225 charset_type = Qw32_charset_greek;
5226 break;
5227 case HEBREW_CHARSET:
5228 charset_type = Qw32_charset_hebrew;
5229 break;
5230 case VIETNAMESE_CHARSET:
5231 charset_type = Qw32_charset_vietnamese;
5232 break;
5233 case THAI_CHARSET:
5234 charset_type = Qw32_charset_thai;
5235 break;
5236 case MAC_CHARSET:
5237 charset_type = Qw32_charset_mac;
5238 break;
5239 case JOHAB_CHARSET:
5240 charset_type = Qw32_charset_johab;
5241 break;
5242#endif
5243
5244#ifdef UNICODE_CHARSET
5245 case UNICODE_CHARSET:
5246 charset_type = Qw32_charset_unicode;
5247 break;
5248#endif
5249 default:
5250 /* Encode numerical value of unknown charset. */
5251 sprintf (buf, "*-#%u", fncharset);
5252 return buf;
5253 }
5254
5255 {
5256 Lisp_Object rest;
5257 char * best_match = NULL;
5258 int matching_found = 0;
5259
5260 /* Look through w32-charset-info-alist for the character set.
5261 Prefer ISO codepages, and prefer lower numbers in the ISO
5262 range. Only return charsets for codepages which are installed.
5263
5264 Format of each entry is
5265 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
5266 */
5267 for (rest = Vw32_charset_info_alist; CONSP (rest); rest = XCDR (rest))
5268 {
5269 char * x_charset;
5270 Lisp_Object w32_charset;
5271 Lisp_Object codepage;
5272
5273 Lisp_Object this_entry = XCAR (rest);
5274
5275 /* Skip invalid entries in alist. */
5276 if (!CONSP (this_entry) || !STRINGP (XCAR (this_entry))
5277 || !CONSP (XCDR (this_entry))
5278 || !SYMBOLP (XCAR (XCDR (this_entry))))
5279 continue;
5280
5281 x_charset = SDATA (XCAR (this_entry));
5282 w32_charset = XCAR (XCDR (this_entry));
5283 codepage = XCDR (XCDR (this_entry));
5284
5285 /* Look for Same charset and a valid codepage (or non-int
5286 which means ignore). */
5287 if (EQ (w32_charset, charset_type)
5288 && (!INTEGERP (codepage) || XINT (codepage) == CP_DEFAULT
5289 || IsValidCodePage (XINT (codepage))))
5290 {
5291 /* If we don't have a match already, then this is the
5292 best. */
5293 if (!best_match)
5294 {
5295 best_match = x_charset;
5296 if (matching && !strnicmp (x_charset, matching, match_len))
5297 matching_found = 1;
5298 }
5299 /* If we already found a match for MATCHING, then
5300 only consider other matches. */
5301 else if (matching_found
5302 && strnicmp (x_charset, matching, match_len))
5303 continue;
5304 /* If this matches what we want, and the best so far doesn't,
5305 then this is better. */
5306 else if (!matching_found && matching
5307 && !strnicmp (x_charset, matching, match_len))
5308 {
5309 best_match = x_charset;
5310 matching_found = 1;
5311 }
5312 /* If this is fully specified, and the best so far isn't,
5313 then this is better. */
5314 else if ((!strchr (best_match, '-') && strchr (x_charset, '-'))
5315 /* If this is an ISO codepage, and the best so far isn't,
5316 then this is better, but only if it fully specifies the
5317 encoding. */
5318 || (strnicmp (best_match, "iso", 3) != 0
5319 && strnicmp (x_charset, "iso", 3) == 0
5320 && strchr (x_charset, '-')))
5321 best_match = x_charset;
5322 /* If both are ISO8859 codepages, choose the one with the
5323 lowest number in the encoding field. */
5324 else if (strnicmp (best_match, "iso8859-", 8) == 0
5325 && strnicmp (x_charset, "iso8859-", 8) == 0)
5326 {
5327 int best_enc = atoi (best_match + 8);
5328 int this_enc = atoi (x_charset + 8);
5329 if (this_enc > 0 && this_enc < best_enc)
5330 best_match = x_charset;
5331 }
5332 }
5333 }
5334
5335 /* If no match, encode the numeric value. */
5336 if (!best_match)
5337 {
5338 sprintf (buf, "*-#%u", fncharset);
5339 return buf;
5340 }
5341
5342 strncpy (buf, best_match, 31);
5343 /* If the charset is not fully specified, put -0 on the end. */
5344 if (!strchr (best_match, '-'))
5345 {
5346 int pos = strlen (best_match);
5347 /* Charset specifiers shouldn't be very long. If it is a made
5348 up one, truncating it should not do any harm since it isn't
5349 recognized anyway. */
5350 if (pos > 29)
5351 pos = 29;
5352 strcpy (buf + pos, "-0");
5353 }
5354 buf[31] = '\0';
5355 return buf;
5356 }
5357}
5358
5359
5360/* Return all the X charsets that map to a font. */
5361static Lisp_Object
5362w32_to_all_x_charsets (fncharset)
5363 int fncharset;
5364{
5365 static char buf[32];
5366 Lisp_Object charset_type;
5367 Lisp_Object retval = Qnil;
5368
5369 switch (fncharset)
5370 {
5371 case ANSI_CHARSET:
5372 /* Handle startup case of w32-charset-info-alist not
5373 being set up yet. */
5374 if (NILP (Vw32_charset_info_alist))
5375 return Fcons (build_string ("iso8859-1"), Qnil);
5376
5377 charset_type = Qw32_charset_ansi;
5378 break;
5379 case DEFAULT_CHARSET:
5380 charset_type = Qw32_charset_default;
5381 break;
5382 case SYMBOL_CHARSET:
5383 charset_type = Qw32_charset_symbol;
5384 break;
5385 case SHIFTJIS_CHARSET:
5386 charset_type = Qw32_charset_shiftjis;
5387 break;
5388 case HANGEUL_CHARSET:
5389 charset_type = Qw32_charset_hangeul;
5390 break;
5391 case GB2312_CHARSET:
5392 charset_type = Qw32_charset_gb2312;
5393 break;
5394 case CHINESEBIG5_CHARSET:
5395 charset_type = Qw32_charset_chinesebig5;
5396 break;
5397 case OEM_CHARSET:
5398 charset_type = Qw32_charset_oem;
5399 break;
5400
5401 /* More recent versions of Windows (95 and NT4.0) define more
5402 character sets. */
5403#ifdef EASTEUROPE_CHARSET
5404 case EASTEUROPE_CHARSET:
5405 charset_type = Qw32_charset_easteurope;
5406 break;
5407 case TURKISH_CHARSET:
5408 charset_type = Qw32_charset_turkish;
5409 break;
5410 case BALTIC_CHARSET:
5411 charset_type = Qw32_charset_baltic;
5412 break;
5413 case RUSSIAN_CHARSET:
5414 charset_type = Qw32_charset_russian;
5415 break;
5416 case ARABIC_CHARSET:
5417 charset_type = Qw32_charset_arabic;
5418 break;
5419 case GREEK_CHARSET:
5420 charset_type = Qw32_charset_greek;
5421 break;
5422 case HEBREW_CHARSET:
5423 charset_type = Qw32_charset_hebrew;
5424 break;
5425 case VIETNAMESE_CHARSET:
5426 charset_type = Qw32_charset_vietnamese;
5427 break;
5428 case THAI_CHARSET:
5429 charset_type = Qw32_charset_thai;
5430 break;
5431 case MAC_CHARSET:
5432 charset_type = Qw32_charset_mac;
5433 break;
5434 case JOHAB_CHARSET:
5435 charset_type = Qw32_charset_johab;
5436 break;
5437#endif
5438
5439#ifdef UNICODE_CHARSET
5440 case UNICODE_CHARSET:
5441 charset_type = Qw32_charset_unicode;
5442 break;
5443#endif
5444 default:
5445 /* Encode numerical value of unknown charset. */
5446 sprintf (buf, "*-#%u", fncharset);
5447 return Fcons (build_string (buf), Qnil);
5448 }
5449
5450 {
5451 Lisp_Object rest;
5452 /* Look through w32-charset-info-alist for the character set.
5453 Only return fully specified charsets for codepages which are
5454 installed.
5455
5456 Format of each entry in Vw32_charset_info_alist is
5457 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
5458 */
5459 for (rest = Vw32_charset_info_alist; CONSP (rest); rest = XCDR (rest))
5460 {
5461 Lisp_Object x_charset;
5462 Lisp_Object w32_charset;
5463 Lisp_Object codepage;
5464
5465 Lisp_Object this_entry = XCAR (rest);
5466
5467 /* Skip invalid entries in alist. */
5468 if (!CONSP (this_entry) || !STRINGP (XCAR (this_entry))
5469 || !CONSP (XCDR (this_entry))
5470 || !SYMBOLP (XCAR (XCDR (this_entry))))
5471 continue;
5472
5473 x_charset = XCAR (this_entry);
5474 w32_charset = XCAR (XCDR (this_entry));
5475 codepage = XCDR (XCDR (this_entry));
5476
5477 if (!strchr (SDATA (x_charset), '-'))
5478 continue;
5479
5480 /* Look for Same charset and a valid codepage (or non-int
5481 which means ignore). */
5482 if (EQ (w32_charset, charset_type)
5483 && (!INTEGERP (codepage) || XINT (codepage) == CP_DEFAULT
5484 || IsValidCodePage (XINT (codepage))))
5485 {
5486 retval = Fcons (x_charset, retval);
5487 }
5488 }
5489
5490 /* If no match, encode the numeric value. */
5491 if (NILP (retval))
5492 {
5493 sprintf (buf, "*-#%u", fncharset);
5494 return Fcons (build_string (buf), Qnil);
5495 }
5496
5497 return retval;
5498 }
5499}
5500
5501/* Get the Windows codepage corresponding to the specified font. The
5502 charset info in the font name is used to look up
5503 w32-charset-to-codepage-alist. */
5504int
5505w32_codepage_for_font (char *fontname)
5506{
5507 Lisp_Object codepage, entry;
5508 char *charset_str, *charset, *end;
5509
5510 /* Extract charset part of font string. */
5511 charset = xlfd_charset_of_font (fontname);
5512
5513 if (!charset)
5514 return CP_UNKNOWN;
5515
5516 charset_str = (char *) alloca (strlen (charset) + 1);
5517 strcpy (charset_str, charset);
5518
5519#if 0
5520 /* Remove leading "*-". */
5521 if (strncmp ("*-", charset_str, 2) == 0)
5522 charset = charset_str + 2;
5523 else
5524#endif
5525 charset = charset_str;
5526
5527 /* Stop match at wildcard (including preceding '-'). */
5528 if (end = strchr (charset, '*'))
5529 {
5530 if (end > charset && *(end-1) == '-')
5531 end--;
5532 *end = '\0';
5533 }
5534
5535 if (!strcmp (charset, "iso10646"))
5536 return CP_UNICODE;
5537
5538 if (NILP (Vw32_charset_info_alist))
5539 return CP_DEFAULT;
5540
5541 entry = Fassoc (build_string(charset), Vw32_charset_info_alist);
5542 if (NILP (entry))
5543 return CP_UNKNOWN;
5544
5545 codepage = Fcdr (Fcdr (entry));
5546
5547 if (NILP (codepage))
5548 return CP_8BIT;
5549 else if (XFASTINT (codepage) == XFASTINT (Qt))
5550 return CP_UNICODE;
5551 else if (INTEGERP (codepage))
5552 return XINT (codepage);
5553 else
5554 return CP_UNKNOWN;
5555}
5556
5557
5558static BOOL
5559w32_to_x_font (lplogfont, lpxstr, len, specific_charset)
5560 LOGFONT * lplogfont;
5561 char * lpxstr;
5562 int len;
5563 char * specific_charset;
5564{
5565 char* fonttype;
5566 char *fontname;
5567 char height_pixels[8];
5568 char height_dpi[8];
5569 char width_pixels[8];
5570 char *fontname_dash;
5571 int display_resy = (int) one_w32_display_info.resy;
5572 int display_resx = (int) one_w32_display_info.resx;
5573 struct coding_system coding;
5574
5575 if (!lpxstr) abort ();
5576
5577 if (!lplogfont)
5578 return FALSE;
5579
5580 if (lplogfont->lfOutPrecision == OUT_STRING_PRECIS)
5581 fonttype = "raster";
5582 else if (lplogfont->lfOutPrecision == OUT_STROKE_PRECIS)
5583 fonttype = "outline";
5584 else
5585 fonttype = "unknown";
5586
5587 setup_coding_system (Fcheck_coding_system (Vlocale_coding_system),
5588 &coding);
5589 coding.src_multibyte = 0;
5590 coding.dst_multibyte = 1;
5591 coding.mode |= CODING_MODE_LAST_BLOCK;
5592 /* We explicitely disable composition handling because selection
5593 data should not contain any composition sequence. */
5594 coding.common_flags &= ~CODING_ANNOTATION_MASK;
5595
5596 coding.dst_bytes = LF_FACESIZE * 2;
5597 coding.destination = (unsigned char *) xmalloc (coding.dst_bytes + 1);
5598 decode_coding_c_string (&coding, lplogfont->lfFaceName,
5599 strlen(lplogfont->lfFaceName), Qnil);
5600 fontname = coding.destination;
5601
5602 *(fontname + coding.produced) = '\0';
5603
5604 /* Replace dashes with underscores so the dashes are not
5605 misinterpreted. */
5606 fontname_dash = fontname;
5607 while (fontname_dash = strchr (fontname_dash, '-'))
5608 *fontname_dash = '_';
5609
5610 if (lplogfont->lfHeight)
5611 {
5612 sprintf (height_pixels, "%u", eabs (lplogfont->lfHeight));
5613 sprintf (height_dpi, "%u",
5614 eabs (lplogfont->lfHeight) * 720 / display_resy);
5615 }
5616 else
5617 {
5618 strcpy (height_pixels, "*");
5619 strcpy (height_dpi, "*");
5620 }
5621
5622#if 0 /* Never put the width in the xfld. It fails on fonts with
5623 double-width characters. */
5624 if (lplogfont->lfWidth)
5625 sprintf (width_pixels, "%u", lplogfont->lfWidth * 10);
5626 else
5627#endif
5628 strcpy (width_pixels, "*");
5629
5630 _snprintf (lpxstr, len - 1,
5631 "-%s-%s-%s-%c-normal-normal-%s-%s-%d-%d-%c-%s-%s",
5632 fonttype, /* foundry */
5633 fontname, /* family */
5634 w32_to_x_weight (lplogfont->lfWeight), /* weight */
5635 lplogfont->lfItalic?'i':'r', /* slant */
5636 /* setwidth name */
5637 /* add style name */
5638 height_pixels, /* pixel size */
5639 height_dpi, /* point size */
5640 display_resx, /* resx */
5641 display_resy, /* resy */
5642 ((lplogfont->lfPitchAndFamily & 0x3) == VARIABLE_PITCH)
5643 ? 'p' : 'c', /* spacing */
5644 width_pixels, /* avg width */
5645 w32_to_x_charset (lplogfont->lfCharSet, specific_charset)
5646 /* charset registry and encoding */
5647 );
5648
5649 lpxstr[len - 1] = 0; /* just to be sure */
5650 return (TRUE);
5651}
5652
5653static BOOL
5654x_to_w32_font (lpxstr, lplogfont)
5655 char * lpxstr;
5656 LOGFONT * lplogfont;
5657{
5658 struct coding_system coding;
5659
5660 if (!lplogfont) return (FALSE);
5661
5662 memset (lplogfont, 0, sizeof (*lplogfont));
5663
5664 /* Set default value for each field. */
5665#if 1
5666 lplogfont->lfOutPrecision = OUT_DEFAULT_PRECIS;
5667 lplogfont->lfClipPrecision = CLIP_DEFAULT_PRECIS;
5668 lplogfont->lfQuality = DEFAULT_QUALITY;
5669#else
5670 /* go for maximum quality */
5671 lplogfont->lfOutPrecision = OUT_STROKE_PRECIS;
5672 lplogfont->lfClipPrecision = CLIP_STROKE_PRECIS;
5673 lplogfont->lfQuality = PROOF_QUALITY;
5674#endif
5675
5676 lplogfont->lfCharSet = DEFAULT_CHARSET;
5677 lplogfont->lfWeight = FW_DONTCARE;
5678 lplogfont->lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
5679
5680 if (!lpxstr)
5681 return FALSE;
5682
5683 /* Provide a simple escape mechanism for specifying Windows font names
5684 * directly -- if font spec does not beginning with '-', assume this
5685 * format:
5686 * "<font name>[:height in pixels[:width in pixels[:weight]]]"
5687 */
5688
5689 if (*lpxstr == '-')
5690 {
5691 int fields, tem;
5692 char name[50], weight[20], slant, pitch, pixels[10], height[10],
5693 width[10], resy[10], remainder[50];
5694 char * encoding;
5695 int dpi = (int) one_w32_display_info.resy;
5696
5697 fields = sscanf (lpxstr,
5698 "-%*[^-]-%49[^-]-%19[^-]-%c-%*[^-]-%*[^-]-%9[^-]-%9[^-]-%*[^-]-%9[^-]-%c-%9[^-]-%49s",
5699 name, weight, &slant, pixels, height, resy, &pitch, width, remainder);
5700 if (fields == EOF)
5701 return (FALSE);
5702
5703 /* In the general case when wildcards cover more than one field,
5704 we don't know which field is which, so don't fill any in.
5705 However, we need to cope with this particular form, which is
5706 generated by font_list_1 (invoked by try_font_list):
5707 "-raster-6x10-*-gb2312*-*"
5708 and make sure to correctly parse the charset field. */
5709 if (fields == 3)
5710 {
5711 fields = sscanf (lpxstr,
5712 "-%*[^-]-%49[^-]-*-%49s",
5713 name, remainder);
5714 }
5715 else if (fields < 9)
5716 {
5717 fields = 0;
5718 remainder[0] = 0;
5719 }
5720
5721 if (fields > 0 && name[0] != '*')
5722 {
5723 Lisp_Object string = build_string (name);
5724 setup_coding_system
5725 (Fcheck_coding_system (Vlocale_coding_system), &coding);
5726 coding.mode |= (CODING_MODE_SAFE_ENCODING | CODING_MODE_LAST_BLOCK);
5727 /* Disable composition/charset annotation. */
5728 coding.common_flags &= ~CODING_ANNOTATION_MASK;
5729 coding.dst_bytes = SCHARS (string) * 2;
5730
5731 coding.destination = (unsigned char *) xmalloc (coding.dst_bytes);
5732 encode_coding_object (&coding, string, 0, 0,
5733 SCHARS (string), SBYTES (string), Qnil);
5734 if (coding.produced >= LF_FACESIZE)
5735 coding.produced = LF_FACESIZE - 1;
5736
5737 coding.destination[coding.produced] = '\0';
5738
5739 strcpy (lplogfont->lfFaceName, coding.destination);
5740 xfree (coding.destination);
5741 }
5742 else
5743 {
5744 lplogfont->lfFaceName[0] = '\0';
5745 }
5746
5747 fields--;
5748
5749 lplogfont->lfWeight = x_to_w32_weight ((fields > 0 ? weight : ""));
5750
5751 fields--;
5752
5753 lplogfont->lfItalic = (fields > 0 && slant == 'i');
5754
5755 fields--;
5756
5757 if (fields > 0 && pixels[0] != '*')
5758 lplogfont->lfHeight = atoi (pixels);
5759
5760 fields--;
5761 fields--;
5762 if (fields > 0 && resy[0] != '*')
5763 {
5764 tem = atoi (resy);
5765 if (tem > 0) dpi = tem;
5766 }
5767
5768 if (fields > -1 && lplogfont->lfHeight == 0 && height[0] != '*')
5769 lplogfont->lfHeight = atoi (height) * dpi / 720;
5770
5771 if (fields > 0)
5772 {
5773 if (pitch == 'p')
5774 lplogfont->lfPitchAndFamily = VARIABLE_PITCH | FF_DONTCARE;
5775 else if (pitch == 'c')
5776 lplogfont->lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
5777 }
5778
5779 fields--;
5780
5781 if (fields > 0 && width[0] != '*')
5782 lplogfont->lfWidth = atoi (width) / 10;
5783
5784 fields--;
5785
5786 /* Strip the trailing '-' if present. (it shouldn't be, as it
5787 fails the test against xlfd-tight-regexp in fontset.el). */
5788 {
5789 int len = strlen (remainder);
5790 if (len > 0 && remainder[len-1] == '-')
5791 remainder[len-1] = 0;
5792 }
5793 encoding = remainder;
5794#if 0
5795 if (strncmp (encoding, "*-", 2) == 0)
5796 encoding += 2;
5797#endif
5798 lplogfont->lfCharSet = x_to_w32_charset (encoding);
5799 }
5800 else
5801 {
5802 int fields;
5803 char name[100], height[10], width[10], weight[20];
5804
5805 fields = sscanf (lpxstr,
5806 "%99[^:]:%9[^:]:%9[^:]:%19s",
5807 name, height, width, weight);
5808
5809 if (fields == EOF) return (FALSE);
5810
5811 if (fields > 0)
5812 {
5813 strncpy (lplogfont->lfFaceName, name, LF_FACESIZE);
5814 lplogfont->lfFaceName[LF_FACESIZE-1] = 0;
5815 }
5816 else
5817 {
5818 lplogfont->lfFaceName[0] = 0;
5819 }
5820
5821 fields--;
5822
5823 if (fields > 0)
5824 lplogfont->lfHeight = atoi (height);
5825
5826 fields--;
5827
5828 if (fields > 0)
5829 lplogfont->lfWidth = atoi (width);
5830
5831 fields--;
5832
5833 lplogfont->lfWeight = x_to_w32_weight ((fields > 0 ? weight : ""));
5834 }
5835
5836 /* This makes TrueType fonts work better. */
5837 lplogfont->lfHeight = - eabs (lplogfont->lfHeight);
5838
5839 return (TRUE);
5840}
5841
5842/* Strip the pixel height and point height from the given xlfd, and
5843 return the pixel height. If no pixel height is specified, calculate
5844 one from the point height, or if that isn't defined either, return
5845 0 (which usually signifies a scalable font).
5846*/
5847static int
5848xlfd_strip_height (char *fontname)
5849{
5850 int pixel_height, field_number;
5851 char *read_from, *write_to;
5852
5853 xassert (fontname);
5854
5855 pixel_height = field_number = 0;
5856 write_to = NULL;
5857
5858 /* Look for height fields. */
5859 for (read_from = fontname; *read_from; read_from++)
5860 {
5861 if (*read_from == '-')
5862 {
5863 field_number++;
5864 if (field_number == 7) /* Pixel height. */
5865 {
5866 read_from++;
5867 write_to = read_from;
5868
5869 /* Find end of field. */
5870 for (;*read_from && *read_from != '-'; read_from++)
5871 ;
5872
5873 /* Split the fontname at end of field. */
5874 if (*read_from)
5875 {
5876 *read_from = '\0';
5877 read_from++;
5878 }
5879 pixel_height = atoi (write_to);
5880 /* Blank out field. */
5881 if (read_from > write_to)
5882 {
5883 *write_to = '-';
5884 write_to++;
5885 }
5886 /* If the pixel height field is at the end (partial xlfd),
5887 return now. */
5888 else
5889 return pixel_height;
5890
5891 /* If we got a pixel height, the point height can be
5892 ignored. Just blank it out and break now. */
5893 if (pixel_height)
5894 {
5895 /* Find end of point size field. */
5896 for (; *read_from && *read_from != '-'; read_from++)
5897 ;
5898
5899 if (*read_from)
5900 read_from++;
5901
5902 /* Blank out the point size field. */
5903 if (read_from > write_to)
5904 {
5905 *write_to = '-';
5906 write_to++;
5907 }
5908 else
5909 return pixel_height;
5910
5911 break;
5912 }
5913 /* If the point height is already blank, break now. */
5914 if (*read_from == '-')
5915 {
5916 read_from++;
5917 break;
5918 }
5919 }
5920 else if (field_number == 8)
5921 {
5922 /* If we didn't get a pixel height, try to get the point
5923 height and convert that. */
5924 int point_size;
5925 char *point_size_start = read_from++;
5926
5927 /* Find end of field. */
5928 for (; *read_from && *read_from != '-'; read_from++)
5929 ;
5930
5931 if (*read_from)
5932 {
5933 *read_from = '\0';
5934 read_from++;
5935 }
5936
5937 point_size = atoi (point_size_start);
5938
5939 /* Convert to pixel height. */
5940 pixel_height = point_size
5941 * one_w32_display_info.height_in / 720;
5942
5943 /* Blank out this field and break. */
5944 *write_to = '-';
5945 write_to++;
5946 break;
5947 }
5948 }
5949 }
5950
5951 /* Shift the rest of the font spec into place. */
5952 if (write_to && read_from > write_to)
5953 {
5954 for (; *read_from; read_from++, write_to++)
5955 *write_to = *read_from;
5956 *write_to = '\0';
5957 }
5958
5959 return pixel_height;
5960}
5961
5962/* Assume parameter 1 is fully qualified, no wildcards. */
5963static BOOL
5964w32_font_match (fontname, pattern)
5965 char * fontname;
5966 char * pattern;
5967{
5968 char *ptr;
5969 char *font_name_copy;
5970 char *regex = alloca (strlen (pattern) * 2 + 3);
5971
5972 font_name_copy = alloca (strlen (fontname) + 1);
5973 strcpy (font_name_copy, fontname);
5974
5975 ptr = regex;
5976 *ptr++ = '^';
5977
5978 /* Turn pattern into a regexp and do a regexp match. */
5979 for (; *pattern; pattern++)
5980 {
5981 if (*pattern == '?')
5982 *ptr++ = '.';
5983 else if (*pattern == '*')
5984 {
5985 *ptr++ = '.';
5986 *ptr++ = '*';
5987 }
5988 else
5989 *ptr++ = *pattern;
5990 }
5991 *ptr = '$';
5992 *(ptr + 1) = '\0';
5993
5994 /* Strip out font heights and compare them seperately, since
5995 rounding error can cause mismatches. This also allows a
5996 comparison between a font that declares only a pixel height and a
5997 pattern that declares the point height.
5998 */
5999 {
6000 int font_height, pattern_height;
6001
6002 font_height = xlfd_strip_height (font_name_copy);
6003 pattern_height = xlfd_strip_height (regex);
6004
6005 /* Compare now, and don't bother doing expensive regexp matching
6006 if the heights differ. */
6007 if (font_height && pattern_height && (font_height != pattern_height))
6008 return FALSE;
6009 }
6010
6011 return (fast_string_match_ignore_case (build_string (regex),
6012 build_string (font_name_copy)) >= 0);
6013}
6014
6015/* Callback functions, and a structure holding info they need, for
6016 listing system fonts on W32. We need one set of functions to do the
6017 job properly, but these don't work on NT 3.51 and earlier, so we
6018 have a second set which don't handle character sets properly to
6019 fall back on.
6020
6021 In both cases, there are two passes made. The first pass gets one
6022 font from each family, the second pass lists all the fonts from
6023 each family. */
6024
6025typedef struct enumfont_t
6026{
6027 HDC hdc;
6028 int numFonts;
6029 LOGFONT logfont;
6030 XFontStruct *size_ref;
6031 Lisp_Object pattern;
6032 Lisp_Object list;
6033} enumfont_t;
6034
6035
6036static void
6037enum_font_maybe_add_to_list (enumfont_t *, LOGFONT *, char *, Lisp_Object);
6038
6039
6040static int CALLBACK
6041enum_font_cb2 (lplf, lptm, FontType, lpef)
6042 ENUMLOGFONT * lplf;
6043 NEWTEXTMETRIC * lptm;
6044 int FontType;
6045 enumfont_t * lpef;
6046{
6047 /* Ignore struck out and underlined versions of fonts. */
6048 if (lplf->elfLogFont.lfStrikeOut || lplf->elfLogFont.lfUnderline)
6049 return 1;
6050
6051 /* Only return fonts with names starting with @ if they were
6052 explicitly specified, since Microsoft uses an initial @ to
6053 denote fonts for vertical writing, without providing a more
6054 convenient way of identifying them. */
6055 if (lplf->elfLogFont.lfFaceName[0] == '@'
6056 && lpef->logfont.lfFaceName[0] != '@')
6057 return 1;
6058
6059 /* Check that the character set matches if it was specified */
6060 if (lpef->logfont.lfCharSet != DEFAULT_CHARSET &&
6061 lplf->elfLogFont.lfCharSet != lpef->logfont.lfCharSet)
6062 return 1;
6063
6064 if (FontType == RASTER_FONTTYPE)
6065 {
6066 /* DBCS raster fonts have problems displaying, so skip them. */
6067 int charset = lplf->elfLogFont.lfCharSet;
6068 if (charset == SHIFTJIS_CHARSET
6069 || charset == HANGEUL_CHARSET
6070 || charset == CHINESEBIG5_CHARSET
6071 || charset == GB2312_CHARSET
6072#ifdef JOHAB_CHARSET
6073 || charset == JOHAB_CHARSET
6074#endif
6075 )
6076 return 1;
6077 }
6078
6079 {
6080 char buf[100];
6081 Lisp_Object width = Qnil;
6082 Lisp_Object charset_list = Qnil;
6083 char *charset = NULL;
6084
6085 /* Truetype fonts do not report their true metrics until loaded */
6086 if (FontType != RASTER_FONTTYPE)
6087 {
6088 if (!NILP (lpef->pattern))
6089 {
6090 /* Scalable fonts are as big as you want them to be. */
6091 lplf->elfLogFont.lfHeight = lpef->logfont.lfHeight;
6092 lplf->elfLogFont.lfWidth = lpef->logfont.lfWidth;
6093 width = make_number (lpef->logfont.lfWidth);
6094 }
6095 else
6096 {
6097 lplf->elfLogFont.lfHeight = 0;
6098 lplf->elfLogFont.lfWidth = 0;
6099 }
6100 }
6101
6102 /* Make sure the height used here is the same as everywhere
6103 else (ie character height, not cell height). */
6104 if (lplf->elfLogFont.lfHeight > 0)
6105 {
6106 /* lptm can be trusted for RASTER fonts, but not scalable ones. */
6107 if (FontType == RASTER_FONTTYPE)
6108 lplf->elfLogFont.lfHeight = lptm->tmInternalLeading - lptm->tmHeight;
6109 else
6110 lplf->elfLogFont.lfHeight = -lplf->elfLogFont.lfHeight;
6111 }
6112
6113 if (!NILP (lpef->pattern))
6114 {
6115 charset = xlfd_charset_of_font (SDATA (lpef->pattern));
6116
6117 /* We already checked charsets above, but DEFAULT_CHARSET
6118 slipped through. So only allow exact matches for DEFAULT_CHARSET. */
6119 if (charset
6120 && strncmp (charset, "*-*", 3) != 0
6121 && lpef->logfont.lfCharSet == DEFAULT_CHARSET
6122 && strcmp (charset, w32_to_x_charset (DEFAULT_CHARSET, NULL)) != 0)
6123 return 1;
6124
6125 /* Reject raster fonts if we are looking for a unicode font. */
6126 if (charset
6127 && FontType == RASTER_FONTTYPE
6128 && strncmp (charset, "iso10646", 8) == 0)
6129 return 1;
6130 }
6131
6132 if (charset)
6133 charset_list = Fcons (build_string (charset), Qnil);
6134 else
6135 /* Always prefer unicode. */
6136 charset_list
6137 = Fcons (build_string ("iso10646-1"),
6138 w32_to_all_x_charsets (lplf->elfLogFont.lfCharSet));
6139
6140 /* Loop through the charsets. */
6141 for ( ; CONSP (charset_list); charset_list = Fcdr (charset_list))
6142 {
6143 Lisp_Object this_charset = Fcar (charset_list);
6144 charset = SDATA (this_charset);
6145
6146 /* Don't list raster fonts as unicode. */
6147 if (charset
6148 && FontType == RASTER_FONTTYPE
6149 && strncmp (charset, "iso10646", 8) == 0)
6150 continue;
6151
6152 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
6153 charset, width);
6154
6155 /* List bold and italic variations if w32-enable-synthesized-fonts
6156 is non-nil and this is a plain font. */
6157 if (w32_enable_synthesized_fonts
6158 && lplf->elfLogFont.lfWeight == FW_NORMAL
6159 && lplf->elfLogFont.lfItalic == FALSE)
6160 {
6161 /* bold. */
6162 lplf->elfLogFont.lfWeight = FW_BOLD;
6163 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
6164 charset, width);
6165 /* bold italic. */
6166 lplf->elfLogFont.lfItalic = TRUE;
6167 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
6168 charset, width);
6169 /* italic. */
6170 lplf->elfLogFont.lfWeight = FW_NORMAL;
6171 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
6172 charset, width);
6173 }
6174 }
6175 }
6176
6177 return 1;
6178}
6179
6180static void
6181enum_font_maybe_add_to_list (lpef, logfont, match_charset, width)
6182 enumfont_t * lpef;
6183 LOGFONT * logfont;
6184 char * match_charset;
6185 Lisp_Object width;
6186{
6187 char buf[100];
6188
6189 if (!w32_to_x_font (logfont, buf, 100, match_charset))
6190 return;
6191
6192 if (NILP (lpef->pattern)
6193 || w32_font_match (buf, SDATA (lpef->pattern)))
6194 {
6195 /* Check if we already listed this font. This may happen if
6196 w32_enable_synthesized_fonts is non-nil, and there are real
6197 bold and italic versions of the font. */
6198 Lisp_Object font_name = build_string (buf);
6199 if (NILP (Fmember (font_name, lpef->list)))
6200 {
6201 Lisp_Object entry = Fcons (font_name, width);
6202 lpef->list = Fcons (entry, lpef->list);
6203 lpef->numFonts++;
6204 }
6205 }
6206}
6207
6208
6209static int CALLBACK
6210enum_font_cb1 (lplf, lptm, FontType, lpef)
6211 ENUMLOGFONT * lplf;
6212 NEWTEXTMETRIC * lptm;
6213 int FontType;
6214 enumfont_t * lpef;
6215{
6216 return EnumFontFamilies (lpef->hdc,
6217 lplf->elfLogFont.lfFaceName,
6218 (FONTENUMPROC) enum_font_cb2,
6219 (LPARAM) lpef);
6220}
6221
6222
6223static int CALLBACK
6224enum_fontex_cb2 (lplf, lptm, font_type, lpef)
6225 ENUMLOGFONTEX * lplf;
6226 NEWTEXTMETRICEX * lptm;
6227 int font_type;
6228 enumfont_t * lpef;
6229{
6230 /* We are not interested in the extra info we get back from the 'Ex
6231 version - only the fact that we get character set variations
6232 enumerated seperately. */
6233 return enum_font_cb2 ((ENUMLOGFONT *) lplf, (NEWTEXTMETRIC *) lptm,
6234 font_type, lpef);
6235}
6236
6237static int CALLBACK
6238enum_fontex_cb1 (lplf, lptm, font_type, lpef)
6239 ENUMLOGFONTEX * lplf;
6240 NEWTEXTMETRICEX * lptm;
6241 int font_type;
6242 enumfont_t * lpef;
6243{
6244 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
6245 FARPROC enum_font_families_ex
6246 = GetProcAddress ( gdi32, "EnumFontFamiliesExA");
6247 /* We don't really expect EnumFontFamiliesEx to disappear once we
6248 get here, so don't bother handling it gracefully. */
6249 if (enum_font_families_ex == NULL)
6250 error ("gdi32.dll has disappeared!");
6251 return enum_font_families_ex (lpef->hdc,
6252 &lplf->elfLogFont,
6253 (FONTENUMPROC) enum_fontex_cb2,
6254 (LPARAM) lpef, 0);
6255}
6256
6257/* Interface to fontset handler. (adapted from mw32font.c in Meadow
6258 and xterm.c in Emacs 20.3) */
6259
6260static Lisp_Object
6261w32_list_bdf_fonts (Lisp_Object pattern, int max_names)
6262{
6263 char *fontname, *ptnstr;
6264 Lisp_Object list, tem, newlist = Qnil;
6265 int n_fonts = 0;
6266
6267 list = Vw32_bdf_filename_alist;
6268 ptnstr = SDATA (pattern);
6269
6270 for ( ; CONSP (list); list = XCDR (list))
6271 {
6272 tem = XCAR (list);
6273 if (CONSP (tem))
6274 fontname = SDATA (XCAR (tem));
6275 else if (STRINGP (tem))
6276 fontname = SDATA (tem);
6277 else
6278 continue;
6279
6280 if (w32_font_match (fontname, ptnstr))
6281 {
6282 newlist = Fcons (XCAR (tem), newlist);
6283 n_fonts++;
6284 if (max_names >= 0 && n_fonts >= max_names)
6285 break;
6286 }
6287 }
6288
6289 return newlist;
6290}
6291
6292
6293/* Return a list of names of available fonts matching PATTERN on frame
6294 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
6295 to be listed. Frame F NULL means we have not yet created any
6296 frame, which means we can't get proper size info, as we don't have
6297 a device context to use for GetTextMetrics.
6298 MAXNAMES sets a limit on how many fonts to match. If MAXNAMES is
6299 negative, then all matching fonts are returned. */
6300
6301Lisp_Object
6302w32_list_fonts (f, pattern, size, maxnames)
6303 struct frame *f;
6304 Lisp_Object pattern;
6305 int size;
6306 int maxnames;
6307{
6308 Lisp_Object patterns, key = Qnil, tem, tpat;
6309 Lisp_Object list = Qnil, newlist = Qnil, second_best = Qnil;
6310 struct w32_display_info *dpyinfo = &one_w32_display_info;
6311 int n_fonts = 0;
6312
6313 patterns = Fassoc (pattern, Valternate_fontname_alist);
6314 if (NILP (patterns))
6315 patterns = Fcons (pattern, Qnil);
6316
6317 for (; CONSP (patterns); patterns = XCDR (patterns))
6318 {
6319 enumfont_t ef;
6320 int codepage;
6321
6322 tpat = XCAR (patterns);
6323
6324 if (!STRINGP (tpat))
6325 continue;
6326
6327 /* Avoid expensive EnumFontFamilies functions if we are not
6328 going to be able to output one of these anyway. */
6329 codepage = w32_codepage_for_font (SDATA (tpat));
6330 if (codepage != CP_8BIT && codepage != CP_UNICODE
6331 && codepage != CP_DEFAULT && codepage != CP_UNKNOWN
6332 && !IsValidCodePage (codepage))
6333 continue;
6334
6335 /* See if we cached the result for this particular query.
6336 The cache is an alist of the form:
6337 ((PATTERN (FONTNAME . WIDTH) ...) ...)
6338 */
6339 if (tem = XCDR (dpyinfo->name_list_element),
6340 !NILP (list = Fassoc (tpat, tem)))
6341 {
6342 list = Fcdr_safe (list);
6343 /* We have a cached list. Don't have to get the list again. */
6344 goto label_cached;
6345 }
6346
6347 BLOCK_INPUT;
6348 /* At first, put PATTERN in the cache. */
6349 ef.pattern = tpat;
6350 ef.list = Qnil;
6351 ef.numFonts = 0;
6352
6353 /* Use EnumFontFamiliesEx where it is available, as it knows
6354 about character sets. Fall back to EnumFontFamilies for
6355 older versions of NT that don't support the 'Ex function. */
6356 x_to_w32_font (SDATA (tpat), &ef.logfont);
6357 {
6358 LOGFONT font_match_pattern;
6359 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
6360 FARPROC enum_font_families_ex
6361 = GetProcAddress ( gdi32, "EnumFontFamiliesExA");
6362
6363 /* We do our own pattern matching so we can handle wildcards. */
6364 font_match_pattern.lfFaceName[0] = 0;
6365 font_match_pattern.lfPitchAndFamily = 0;
6366 /* We can use the charset, because if it is a wildcard it will
6367 be DEFAULT_CHARSET anyway. */
6368 font_match_pattern.lfCharSet = ef.logfont.lfCharSet;
6369
6370 ef.hdc = GetDC (dpyinfo->root_window);
6371
6372 if (enum_font_families_ex)
6373 enum_font_families_ex (ef.hdc,
6374 &font_match_pattern,
6375 (FONTENUMPROC) enum_fontex_cb1,
6376 (LPARAM) &ef, 0);
6377 else
6378 EnumFontFamilies (ef.hdc, NULL, (FONTENUMPROC) enum_font_cb1,
6379 (LPARAM)&ef);
6380
6381 ReleaseDC (dpyinfo->root_window, ef.hdc);
6382 }
6383
6384 UNBLOCK_INPUT;
6385 list = ef.list;
6386
6387 /* Make a list of the fonts we got back.
6388 Store that in the font cache for the display. */
6389 XSETCDR (dpyinfo->name_list_element,
6390 Fcons (Fcons (tpat, list),
6391 XCDR (dpyinfo->name_list_element)));
6392
6393 label_cached:
6394 if (NILP (list)) continue; /* Try the remaining alternatives. */
6395
6396 newlist = second_best = Qnil;
6397
6398 /* Make a list of the fonts that have the right width. */
6399 for (; CONSP (list); list = XCDR (list))
6400 {
6401 int found_size;
6402 tem = XCAR (list);
6403
6404 if (!CONSP (tem))
6405 continue;
6406 if (NILP (XCAR (tem)))
6407 continue;
6408 if (!size)
6409 {
6410 newlist = Fcons (XCAR (tem), newlist);
6411 n_fonts++;
6412 if (maxnames >= 0 && n_fonts >= maxnames)
6413 break;
6414 else
6415 continue;
6416 }
6417 if (!INTEGERP (XCDR (tem)))
6418 {
6419 /* Since we don't yet know the size of the font, we must
6420 load it and try GetTextMetrics. */
6421 W32FontStruct thisinfo;
6422 LOGFONT lf;
6423 HDC hdc;
6424 HANDLE oldobj;
6425
6426 if (!x_to_w32_font (SDATA (XCAR (tem)), &lf))
6427 continue;
6428
6429 BLOCK_INPUT;
6430 thisinfo.bdf = NULL;
6431 thisinfo.hfont = CreateFontIndirect (&lf);
6432 if (thisinfo.hfont == NULL)
6433 continue;
6434
6435 hdc = GetDC (dpyinfo->root_window);
6436 oldobj = SelectObject (hdc, thisinfo.hfont);
6437 if (GetTextMetrics (hdc, &thisinfo.tm))
6438 XSETCDR (tem, make_number (FONT_AVG_WIDTH (&thisinfo)));
6439 else
6440 XSETCDR (tem, make_number (0));
6441 SelectObject (hdc, oldobj);
6442 ReleaseDC (dpyinfo->root_window, hdc);
6443 DeleteObject (thisinfo.hfont);
6444 UNBLOCK_INPUT;
6445 }
6446 found_size = XINT (XCDR (tem));
6447 if (found_size == size)
6448 {
6449 newlist = Fcons (XCAR (tem), newlist);
6450 n_fonts++;
6451 if (maxnames >= 0 && n_fonts >= maxnames)
6452 break;
6453 }
6454 /* keep track of the closest matching size in case
6455 no exact match is found. */
6456 else if (found_size > 0)
6457 {
6458 if (NILP (second_best))
6459 second_best = tem;
6460
6461 else if (found_size < size)
6462 {
6463 if (XINT (XCDR (second_best)) > size
6464 || XINT (XCDR (second_best)) < found_size)
6465 second_best = tem;
6466 }
6467 else
6468 {
6469 if (XINT (XCDR (second_best)) > size
6470 && XINT (XCDR (second_best)) >
6471 found_size)
6472 second_best = tem;
6473 }
6474 }
6475 }
6476
6477 if (!NILP (newlist))
6478 break;
6479 else if (!NILP (second_best))
6480 {
6481 newlist = Fcons (XCAR (second_best), Qnil);
6482 break;
6483 }
6484 }
6485
6486 /* Include any bdf fonts. */
6487 if (n_fonts < maxnames || maxnames < 0)
6488 {
6489 Lisp_Object combined[2];
6490 combined[0] = w32_list_bdf_fonts (pattern, maxnames - n_fonts);
6491 combined[1] = newlist;
6492 newlist = Fnconc (2, combined);
6493 }
6494
6495 return newlist;
6496}
6497
6498
6499/* Return a pointer to struct font_info of font FONT_IDX of frame F. */
6500struct font_info *
6501w32_get_font_info (f, font_idx)
6502 FRAME_PTR f;
6503 int font_idx;
6504{
6505 return (FRAME_W32_FONT_TABLE (f) + font_idx);
6506}
6507
6508
6509struct font_info*
6510w32_query_font (struct frame *f, char *fontname)
6511{
6512 int i;
6513 struct font_info *pfi;
6514
6515 pfi = FRAME_W32_FONT_TABLE (f);
6516
6517 for (i = 0; i < one_w32_display_info.n_fonts ;i++, pfi++)
6518 {
6519 if (stricmp (pfi->name, fontname) == 0) return pfi;
6520 }
6521
6522 return NULL;
6523}
6524
6525/* Find a CCL program for a font specified by FONTP, and set the member
6526 `encoder' of the structure. */
6527
6528void
6529w32_find_ccl_program (fontp)
6530 struct font_info *fontp;
6531{
6532 Lisp_Object list, elt;
6533
6534 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
6535 {
6536 elt = XCAR (list);
6537 if (CONSP (elt)
6538 && STRINGP (XCAR (elt))
6539 && (fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
6540 >= 0))
6541 break;
6542 }
6543 if (! NILP (list))
6544 {
6545 struct ccl_program *ccl
6546 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
6547
6548 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
6549 xfree (ccl);
6550 else
6551 fontp->font_encoder = ccl;
6552 }
6553}
6554
6555/* directory-files from dired.c. */
6556Lisp_Object Fdirectory_files P_ ((Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object));
6557
6558\f
6559/* Find BDF files in a specified directory. (use GCPRO when calling,
6560 as this calls lisp to get a directory listing). */
6561static Lisp_Object
6562w32_find_bdf_fonts_in_dir (Lisp_Object directory)
6563{
6564 Lisp_Object filelist, list = Qnil;
6565 char fontname[100];
6566
6567 if (!STRINGP (directory))
6568 return Qnil;
6569
6570 filelist = Fdirectory_files (directory, Qt,
6571 build_string (".*\\.[bB][dD][fF]"), Qt);
6572
6573 for ( ; CONSP (filelist); filelist = XCDR (filelist))
6574 {
6575 Lisp_Object filename = XCAR (filelist);
6576 if (w32_BDF_to_x_font (SDATA (filename), fontname, 100))
6577 store_in_alist (&list, build_string (fontname), filename);
6578 }
6579 return list;
6580}
6581
6582DEFUN ("w32-find-bdf-fonts", Fw32_find_bdf_fonts, Sw32_find_bdf_fonts,
6583 1, 1, 0,
6584 doc: /* Return a list of BDF fonts in DIRECTORY.
6585The list is suitable for appending to `w32-bdf-filename-alist'.
6586Fonts which do not contain an xlfd description will not be included
6587in the list. DIRECTORY may be a list of directories. */)
6588 (directory)
6589 Lisp_Object directory;
6590{
6591 Lisp_Object list = Qnil;
6592 struct gcpro gcpro1, gcpro2;
6593
6594 if (!CONSP (directory))
6595 return w32_find_bdf_fonts_in_dir (directory);
6596
6597 for ( ; CONSP (directory); directory = XCDR (directory))
6598 {
6599 Lisp_Object pair[2];
6600 pair[0] = list;
6601 pair[1] = Qnil;
6602 GCPRO2 (directory, list);
6603 pair[1] = w32_find_bdf_fonts_in_dir ( XCAR (directory) );
6604 list = Fnconc ( 2, pair );
6605 UNGCPRO;
6606 }
6607 return list;
6608}
6609
6610\f
6611DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
6612 doc: /* Internal function called by `color-defined-p', which see. */)
6613 (color, frame)
6614 Lisp_Object color, frame;
6615{
6616 XColor foo;
6617 FRAME_PTR f = check_x_frame (frame);
6618
6619 CHECK_STRING (color);
6620
6621 if (w32_defined_color (f, SDATA (color), &foo, 0))
6622 return Qt;
6623 else
6624 return Qnil;
6625}
6626
6627DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
6628 doc: /* Internal function called by `color-values', which see. */)
6629 (color, frame)
6630 Lisp_Object color, frame;
6631{
6632 XColor foo;
6633 FRAME_PTR f = check_x_frame (frame);
6634
6635 CHECK_STRING (color);
6636
6637 if (w32_defined_color (f, SDATA (color), &foo, 0))
6638 return list3 (make_number ((GetRValue (foo.pixel) << 8)
6639 | GetRValue (foo.pixel)),
6640 make_number ((GetGValue (foo.pixel) << 8)
6641 | GetGValue (foo.pixel)),
6642 make_number ((GetBValue (foo.pixel) << 8)
6643 | GetBValue (foo.pixel)));
6644 else
6645 return Qnil;
6646}
6647
6648DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
6649 doc: /* Internal function called by `display-color-p', which see. */)
6650 (display)
6651 Lisp_Object display;
6652{
6653 struct w32_display_info *dpyinfo = check_x_display_info (display);
6654
6655 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 2)
6656 return Qnil;
6657
6658 return Qt;
6659}
6660
6661DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p,
6662 Sx_display_grayscale_p, 0, 1, 0,
6663 doc: /* Return t if DISPLAY supports shades of gray.
6664Note that color displays do support shades of gray.
6665The optional argument DISPLAY specifies which display to ask about.
6666DISPLAY should be either a frame or a display name (a string).
6667If omitted or nil, that stands for the selected frame's display. */)
6668 (display)
6669 Lisp_Object display;
6670{
6671 struct w32_display_info *dpyinfo = check_x_display_info (display);
6672
6673 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 1)
6674 return Qnil;
6675
6676 return Qt;
6677}
6678
6679DEFUN ("x-display-pixel-width", Fx_display_pixel_width,
6680 Sx_display_pixel_width, 0, 1, 0,
6681 doc: /* Return the width in pixels of DISPLAY.
6682The optional argument DISPLAY specifies which display to ask about.
6683DISPLAY should be either a frame or a display name (a string).
6684If omitted or nil, that stands for the selected frame's display. */)
6685 (display)
6686 Lisp_Object display;
6687{
6688 struct w32_display_info *dpyinfo = check_x_display_info (display);
6689
6690 return make_number (dpyinfo->width);
6691}
6692
6693DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
6694 Sx_display_pixel_height, 0, 1, 0,
6695 doc: /* Return the height in pixels of DISPLAY.
6696The optional argument DISPLAY specifies which display to ask about.
6697DISPLAY should be either a frame or a display name (a string).
6698If omitted or nil, that stands for the selected frame's display. */)
6699 (display)
6700 Lisp_Object display;
6701{
6702 struct w32_display_info *dpyinfo = check_x_display_info (display);
6703
6704 return make_number (dpyinfo->height);
6705}
6706
6707DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
6708 0, 1, 0,
6709 doc: /* Return the number of bitplanes of DISPLAY.
6710The optional argument DISPLAY specifies which display to ask about.
6711DISPLAY should be either a frame or a display name (a string).
6712If omitted or nil, that stands for the selected frame's display. */)
6713 (display)
6714 Lisp_Object display;
6715{
6716 struct w32_display_info *dpyinfo = check_x_display_info (display);
6717
6718 return make_number (dpyinfo->n_planes * dpyinfo->n_cbits);
6719}
6720
6721DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
6722 0, 1, 0,
6723 doc: /* Return the number of color cells of DISPLAY.
6724The optional argument DISPLAY specifies which display to ask about.
6725DISPLAY should be either a frame or a display name (a string).
6726If omitted or nil, that stands for the selected frame's display. */)
6727 (display)
6728 Lisp_Object display;
6729{
6730 struct w32_display_info *dpyinfo = check_x_display_info (display);
6731 HDC hdc;
6732 int cap;
6733
6734 hdc = GetDC (dpyinfo->root_window);
6735 if (dpyinfo->has_palette)
6736 cap = GetDeviceCaps (hdc, SIZEPALETTE);
6737 else
6738 cap = GetDeviceCaps (hdc, NUMCOLORS);
6739
6740 /* We force 24+ bit depths to 24-bit, both to prevent an overflow
6741 and because probably is more meaningful on Windows anyway */
6742 if (cap < 0)
6743 cap = 1 << min (dpyinfo->n_planes * dpyinfo->n_cbits, 24);
6744
6745 ReleaseDC (dpyinfo->root_window, hdc);
6746
6747 return make_number (cap);
6748}
6749
6750DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
6751 Sx_server_max_request_size,
6752 0, 1, 0,
6753 doc: /* Return the maximum request size of the server of DISPLAY.
6754The optional argument DISPLAY specifies which display to ask about.
6755DISPLAY should be either a frame or a display name (a string).
6756If omitted or nil, that stands for the selected frame's display. */)
6757 (display)
6758 Lisp_Object display;
6759{
6760 struct w32_display_info *dpyinfo = check_x_display_info (display);
6761
6762 return make_number (1);
6763}
6764
6765DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
6766 doc: /* Return the "vendor ID" string of the W32 system (Microsoft).
6767The optional argument DISPLAY specifies which display to ask about.
6768DISPLAY should be either a frame or a display name (a string).
6769If omitted or nil, that stands for the selected frame's display. */)
6770 (display)
6771 Lisp_Object display;
6772{
6773 return build_string ("Microsoft Corp.");
6774}
6775
6776DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
6777 doc: /* Return the version numbers of the server of DISPLAY.
6778The value is a list of three integers: the major and minor
6779version numbers of the X Protocol in use, and the distributor-specific
6780release number. See also the function `x-server-vendor'.
6781
6782The optional argument DISPLAY specifies which display to ask about.
6783DISPLAY should be either a frame or a display name (a string).
6784If omitted or nil, that stands for the selected frame's display. */)
6785 (display)
6786 Lisp_Object display;
6787{
6788 return Fcons (make_number (w32_major_version),
6789 Fcons (make_number (w32_minor_version),
6790 Fcons (make_number (w32_build_number), Qnil)));
6791}
6792
6793DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
6794 doc: /* Return the number of screens on the server of DISPLAY.
6795The optional argument DISPLAY specifies which display to ask about.
6796DISPLAY should be either a frame or a display name (a string).
6797If omitted or nil, that stands for the selected frame's display. */)
6798 (display)
6799 Lisp_Object display;
6800{
6801 return make_number (1);
6802}
6803
6804DEFUN ("x-display-mm-height", Fx_display_mm_height,
6805 Sx_display_mm_height, 0, 1, 0,
6806 doc: /* Return the height in millimeters of DISPLAY.
6807The optional argument DISPLAY specifies which display to ask about.
6808DISPLAY should be either a frame or a display name (a string).
6809If omitted or nil, that stands for the selected frame's display. */)
6810 (display)
6811 Lisp_Object display;
6812{
6813 struct w32_display_info *dpyinfo = check_x_display_info (display);
6814 HDC hdc;
6815 int cap;
6816
6817 hdc = GetDC (dpyinfo->root_window);
6818
6819 cap = GetDeviceCaps (hdc, VERTSIZE);
6820
6821 ReleaseDC (dpyinfo->root_window, hdc);
6822
6823 return make_number (cap);
6824}
6825
6826DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
6827 doc: /* Return the width in millimeters of DISPLAY.
6828The optional argument DISPLAY specifies which display to ask about.
6829DISPLAY should be either a frame or a display name (a string).
6830If omitted or nil, that stands for the selected frame's display. */)
6831 (display)
6832 Lisp_Object display;
6833{
6834 struct w32_display_info *dpyinfo = check_x_display_info (display);
6835
6836 HDC hdc;
6837 int cap;
6838
6839 hdc = GetDC (dpyinfo->root_window);
6840
6841 cap = GetDeviceCaps (hdc, HORZSIZE);
6842
6843 ReleaseDC (dpyinfo->root_window, hdc);
6844
6845 return make_number (cap);
6846}
6847
6848DEFUN ("x-display-backing-store", Fx_display_backing_store,
6849 Sx_display_backing_store, 0, 1, 0,
6850 doc: /* Return an indication of whether DISPLAY does backing store.
6851The value may be `always', `when-mapped', or `not-useful'.
6852The optional argument DISPLAY specifies which display to ask about.
6853DISPLAY should be either a frame or a display name (a string).
6854If omitted or nil, that stands for the selected frame's display. */)
6855 (display)
6856 Lisp_Object display;
6857{
6858 return intern ("not-useful");
6859}
6860
6861DEFUN ("x-display-visual-class", Fx_display_visual_class,
6862 Sx_display_visual_class, 0, 1, 0,
6863 doc: /* Return the visual class of DISPLAY.
6864The value is one of the symbols `static-gray', `gray-scale',
6865`static-color', `pseudo-color', `true-color', or `direct-color'.
6866
6867The optional argument DISPLAY specifies which display to ask about.
6868DISPLAY should be either a frame or a display name (a string).
6869If omitted or nil, that stands for the selected frame's display. */)
6870 (display)
6871 Lisp_Object display;
6872{
6873 struct w32_display_info *dpyinfo = check_x_display_info (display);
6874 Lisp_Object result = Qnil;
6875
6876 if (dpyinfo->has_palette)
6877 result = intern ("pseudo-color");
6878 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 1)
6879 result = intern ("static-grey");
6880 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 4)
6881 result = intern ("static-color");
6882 else if (dpyinfo->n_planes * dpyinfo->n_cbits > 8)
6883 result = intern ("true-color");
6884
6885 return result;
6886}
6887
6888DEFUN ("x-display-save-under", Fx_display_save_under,
6889 Sx_display_save_under, 0, 1, 0,
6890 doc: /* Return t if DISPLAY supports the save-under feature.
6891The optional argument DISPLAY specifies which display to ask about.
6892DISPLAY should be either a frame or a display name (a string).
6893If omitted or nil, that stands for the selected frame's display. */)
6894 (display)
6895 Lisp_Object display;
6896{
6897 return Qnil;
6898}
6899\f
6900int
6901x_pixel_width (f)
6902 register struct frame *f;
6903{
6904 return FRAME_PIXEL_WIDTH (f);
6905}
6906
6907int
6908x_pixel_height (f)
6909 register struct frame *f;
6910{
6911 return FRAME_PIXEL_HEIGHT (f);
6912}
6913
6914int
6915x_char_width (f)
6916 register struct frame *f;
6917{
6918 return FRAME_COLUMN_WIDTH (f);
6919}
6920
6921int
6922x_char_height (f)
6923 register struct frame *f;
6924{
6925 return FRAME_LINE_HEIGHT (f);
6926}
6927
6928int
6929x_screen_planes (f)
6930 register struct frame *f;
6931{
6932 return FRAME_W32_DISPLAY_INFO (f)->n_planes;
6933}
6934\f
6935/* Return the display structure for the display named NAME.
6936 Open a new connection if necessary. */
6937
6938struct w32_display_info *
6939x_display_info_for_name (name)
6940 Lisp_Object name;
6941{
6942 Lisp_Object names;
6943 struct w32_display_info *dpyinfo;
6944
6945 CHECK_STRING (name);
6946
6947 for (dpyinfo = &one_w32_display_info, names = w32_display_name_list;
6948 dpyinfo;
6949 dpyinfo = dpyinfo->next, names = XCDR (names))
6950 {
6951 Lisp_Object tem;
6952 tem = Fstring_equal (XCAR (XCAR (names)), name);
6953 if (!NILP (tem))
6954 return dpyinfo;
6955 }
6956
6957 /* Use this general default value to start with. */
6958 Vx_resource_name = Vinvocation_name;
6959
6960 validate_x_resource_name ();
6961
6962 dpyinfo = w32_term_init (name, (unsigned char *)0,
6963 (char *) SDATA (Vx_resource_name));
6964
6965 if (dpyinfo == 0)
6966 error ("Cannot connect to server %s", SDATA (name));
6967
6968 w32_in_use = 1;
6969 XSETFASTINT (Vwindow_system_version, 3);
6970
6971 return dpyinfo;
6972}
6973
6974DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
6975 1, 3, 0, doc: /* Open a connection to a server.
6976DISPLAY is the name of the display to connect to.
6977Optional second arg XRM-STRING is a string of resources in xrdb format.
6978If the optional third arg MUST-SUCCEED is non-nil,
6979terminate Emacs if we can't open the connection. */)
6980 (display, xrm_string, must_succeed)
6981 Lisp_Object display, xrm_string, must_succeed;
6982{
6983 unsigned char *xrm_option;
6984 struct w32_display_info *dpyinfo;
6985
6986 /* If initialization has already been done, return now to avoid
6987 overwriting critical parts of one_w32_display_info. */
6988 if (w32_in_use)
6989 return Qnil;
6990
6991 CHECK_STRING (display);
6992 if (! NILP (xrm_string))
6993 CHECK_STRING (xrm_string);
6994
6995#if 0
6996 if (! EQ (Vwindow_system, intern ("w32")))
6997 error ("Not using Microsoft Windows");
6998#endif
6999
7000 /* Allow color mapping to be defined externally; first look in user's
7001 HOME directory, then in Emacs etc dir for a file called rgb.txt. */
7002 {
7003 Lisp_Object color_file;
7004 struct gcpro gcpro1;
7005
7006 color_file = build_string ("~/rgb.txt");
7007
7008 GCPRO1 (color_file);
7009
7010 if (NILP (Ffile_readable_p (color_file)))
7011 color_file =
7012 Fexpand_file_name (build_string ("rgb.txt"),
7013 Fsymbol_value (intern ("data-directory")));
7014
7015 Vw32_color_map = Fw32_load_color_file (color_file);
7016
7017 UNGCPRO;
7018 }
7019 if (NILP (Vw32_color_map))
7020 Vw32_color_map = Fw32_default_color_map ();
7021
7022 /* Merge in system logical colors. */
7023 add_system_logical_colors_to_map (&Vw32_color_map);
7024
7025 if (! NILP (xrm_string))
7026 xrm_option = (unsigned char *) SDATA (xrm_string);
7027 else
7028 xrm_option = (unsigned char *) 0;
7029
7030 /* Use this general default value to start with. */
7031 /* First remove .exe suffix from invocation-name - it looks ugly. */
7032 {
7033 char basename[ MAX_PATH ], *str;
7034
7035 strcpy (basename, SDATA (Vinvocation_name));
7036 str = strrchr (basename, '.');
7037 if (str) *str = 0;
7038 Vinvocation_name = build_string (basename);
7039 }
7040 Vx_resource_name = Vinvocation_name;
7041
7042 validate_x_resource_name ();
7043
7044 /* This is what opens the connection and sets x_current_display.
7045 This also initializes many symbols, such as those used for input. */
7046 dpyinfo = w32_term_init (display, xrm_option,
7047 (char *) SDATA (Vx_resource_name));
7048
7049 if (dpyinfo == 0)
7050 {
7051 if (!NILP (must_succeed))
7052 fatal ("Cannot connect to server %s.\n",
7053 SDATA (display));
7054 else
7055 error ("Cannot connect to server %s", SDATA (display));
7056 }
7057
7058 w32_in_use = 1;
7059
7060 XSETFASTINT (Vwindow_system_version, 3);
7061 return Qnil;
7062}
7063
7064DEFUN ("x-close-connection", Fx_close_connection,
7065 Sx_close_connection, 1, 1, 0,
7066 doc: /* Close the connection to DISPLAY's server.
7067For DISPLAY, specify either a frame or a display name (a string).
7068If DISPLAY is nil, that stands for the selected frame's display. */)
7069 (display)
7070 Lisp_Object display;
7071{
7072 struct w32_display_info *dpyinfo = check_x_display_info (display);
7073 int i;
7074
7075 if (dpyinfo->reference_count > 0)
7076 error ("Display still has frames on it");
7077
7078 BLOCK_INPUT;
7079 /* Free the fonts in the font table. */
7080 for (i = 0; i < dpyinfo->n_fonts; i++)
7081 if (dpyinfo->font_table[i].name)
7082 {
7083 if (dpyinfo->font_table[i].name != dpyinfo->font_table[i].full_name)
7084 xfree (dpyinfo->font_table[i].full_name);
7085 xfree (dpyinfo->font_table[i].name);
7086 w32_unload_font (dpyinfo, dpyinfo->font_table[i].font);
7087 }
7088 x_destroy_all_bitmaps (dpyinfo);
7089
7090 x_delete_display (dpyinfo);
7091 UNBLOCK_INPUT;
7092
7093 return Qnil;
7094}
7095
7096DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
7097 doc: /* Return the list of display names that Emacs has connections to. */)
7098 ()
7099{
7100 Lisp_Object tail, result;
7101
7102 result = Qnil;
7103 for (tail = w32_display_name_list; CONSP (tail); tail = XCDR (tail))
7104 result = Fcons (XCAR (XCAR (tail)), result);
7105
7106 return result;
7107}
7108
7109DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
7110 doc: /* This is a noop on W32 systems. */)
7111 (on, display)
7112 Lisp_Object display, on;
7113{
7114 return Qnil;
7115}
7116
7117
7118\f
7119/***********************************************************************
7120 Window properties
7121 ***********************************************************************/
7122
7123DEFUN ("x-change-window-property", Fx_change_window_property,
7124 Sx_change_window_property, 2, 6, 0,
7125 doc: /* Change window property PROP to VALUE on the X window of FRAME.
7126VALUE may be a string or a list of conses, numbers and/or strings.
7127If an element in the list is a string, it is converted to
7128an Atom and the value of the Atom is used. If an element is a cons,
7129it is converted to a 32 bit number where the car is the 16 top bits and the
7130cdr is the lower 16 bits.
7131FRAME nil or omitted means use the selected frame.
7132If TYPE is given and non-nil, it is the name of the type of VALUE.
7133If TYPE is not given or nil, the type is STRING.
7134FORMAT gives the size in bits of each element if VALUE is a list.
7135It must be one of 8, 16 or 32.
7136If VALUE is a string or FORMAT is nil or not given, FORMAT defaults to 8.
7137If OUTER_P is non-nil, the property is changed for the outer X window of
7138FRAME. Default is to change on the edit X window.
7139
7140Value is VALUE. */)
7141 (prop, value, frame, type, format, outer_p)
7142 Lisp_Object prop, value, frame, type, format, outer_p;
7143{
7144#if 0 /* TODO : port window properties to W32 */
7145 struct frame *f = check_x_frame (frame);
7146 Atom prop_atom;
7147
7148 CHECK_STRING (prop);
7149 CHECK_STRING (value);
7150
7151 BLOCK_INPUT;
7152 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
7153 XChangeProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
7154 prop_atom, XA_STRING, 8, PropModeReplace,
7155 SDATA (value), SCHARS (value));
7156
7157 /* Make sure the property is set when we return. */
7158 XFlush (FRAME_W32_DISPLAY (f));
7159 UNBLOCK_INPUT;
7160
7161#endif /* TODO */
7162
7163 return value;
7164}
7165
7166
7167DEFUN ("x-delete-window-property", Fx_delete_window_property,
7168 Sx_delete_window_property, 1, 2, 0,
7169 doc: /* Remove window property PROP from X window of FRAME.
7170FRAME nil or omitted means use the selected frame. Value is PROP. */)
7171 (prop, frame)
7172 Lisp_Object prop, frame;
7173{
7174#if 0 /* TODO : port window properties to W32 */
7175
7176 struct frame *f = check_x_frame (frame);
7177 Atom prop_atom;
7178
7179 CHECK_STRING (prop);
7180 BLOCK_INPUT;
7181 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
7182 XDeleteProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), prop_atom);
7183
7184 /* Make sure the property is removed when we return. */
7185 XFlush (FRAME_W32_DISPLAY (f));
7186 UNBLOCK_INPUT;
7187#endif /* TODO */
7188
7189 return prop;
7190}
7191
7192
7193DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
7194 1, 2, 0,
7195 doc: /* Value is the value of window property PROP on FRAME.
7196If FRAME is nil or omitted, use the selected frame. Value is nil
7197if FRAME hasn't a property with name PROP or if PROP has no string
7198value. */)
7199 (prop, frame)
7200 Lisp_Object prop, frame;
7201{
7202#if 0 /* TODO : port window properties to W32 */
7203
7204 struct frame *f = check_x_frame (frame);
7205 Atom prop_atom;
7206 int rc;
7207 Lisp_Object prop_value = Qnil;
7208 char *tmp_data = NULL;
7209 Atom actual_type;
7210 int actual_format;
7211 unsigned long actual_size, bytes_remaining;
7212
7213 CHECK_STRING (prop);
7214 BLOCK_INPUT;
7215 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
7216 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
7217 prop_atom, 0, 0, False, XA_STRING,
7218 &actual_type, &actual_format, &actual_size,
7219 &bytes_remaining, (unsigned char **) &tmp_data);
7220 if (rc == Success)
7221 {
7222 int size = bytes_remaining;
7223
7224 XFree (tmp_data);
7225 tmp_data = NULL;
7226
7227 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
7228 prop_atom, 0, bytes_remaining,
7229 False, XA_STRING,
7230 &actual_type, &actual_format,
7231 &actual_size, &bytes_remaining,
7232 (unsigned char **) &tmp_data);
7233 if (rc == Success)
7234 prop_value = make_string (tmp_data, size);
7235
7236 XFree (tmp_data);
7237 }
7238
7239 UNBLOCK_INPUT;
7240
7241 return prop_value;
7242
7243#endif /* TODO */
7244 return Qnil;
7245}
7246
7247
7248\f
7249/***********************************************************************
7250 Busy cursor
7251 ***********************************************************************/
7252
7253/* Non-zero means an hourglass cursor is currently shown. */
7254
7255static int hourglass_shown_p;
7256
7257/* Number of seconds to wait before displaying an hourglass cursor. */
7258
7259static Lisp_Object Vhourglass_delay;
7260
7261/* Default number of seconds to wait before displaying an hourglass
7262 cursor. */
7263
7264#define DEFAULT_HOURGLASS_DELAY 1
7265
7266/* Return non-zero if houglass timer has been started or hourglass is shown. */
7267
7268int
7269hourglass_started ()
7270{
7271 return hourglass_shown_p || hourglass_timer;
7272}
7273
7274/* Cancel a currently active hourglass timer, and start a new one. */
7275
7276void
7277start_hourglass ()
7278{
7279 DWORD delay;
7280 int secs, msecs = 0;
7281 struct frame * f = SELECTED_FRAME ();
7282
7283 /* No cursors on non GUI frames. */
7284 if (!FRAME_W32_P (f))
7285 return;
7286
7287 cancel_hourglass ();
7288
7289 if (INTEGERP (Vhourglass_delay)
7290 && XINT (Vhourglass_delay) > 0)
7291 secs = XFASTINT (Vhourglass_delay);
7292 else if (FLOATP (Vhourglass_delay)
7293 && XFLOAT_DATA (Vhourglass_delay) > 0)
7294 {
7295 Lisp_Object tem;
7296 tem = Ftruncate (Vhourglass_delay, Qnil);
7297 secs = XFASTINT (tem);
7298 msecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000;
7299 }
7300 else
7301 secs = DEFAULT_HOURGLASS_DELAY;
7302
7303 delay = secs * 1000 + msecs;
7304 hourglass_hwnd = FRAME_W32_WINDOW (f);
7305 hourglass_timer = SetTimer (hourglass_hwnd, HOURGLASS_ID, delay, NULL);
7306}
7307
7308
7309/* Cancel the hourglass cursor timer if active, hide an hourglass
7310 cursor if shown. */
7311
7312void
7313cancel_hourglass ()
7314{
7315 if (hourglass_timer)
7316 {
7317 KillTimer (hourglass_hwnd, hourglass_timer);
7318 hourglass_timer = 0;
7319 }
7320
7321 if (hourglass_shown_p)
7322 hide_hourglass ();
7323}
7324
7325
7326/* Timer function of hourglass_timer.
7327
7328 Display an hourglass cursor. Set the hourglass_p flag in display info
7329 to indicate that an hourglass cursor is shown. */
7330
7331static void
7332show_hourglass (f)
7333 struct frame *f;
7334{
7335 if (!hourglass_shown_p)
7336 {
7337 f->output_data.w32->hourglass_p = 1;
7338 if (!menubar_in_use && !current_popup_menu)
7339 SetCursor (f->output_data.w32->hourglass_cursor);
7340 hourglass_shown_p = 1;
7341 }
7342}
7343
7344
7345/* Hide the hourglass cursor on all frames, if it is currently shown. */
7346
7347static void
7348hide_hourglass ()
7349{
7350 if (hourglass_shown_p)
7351 {
7352 struct frame *f = x_window_to_frame (&one_w32_display_info,
7353 hourglass_hwnd);
7354
7355 f->output_data.w32->hourglass_p = 0;
7356 SetCursor (f->output_data.w32->current_cursor);
7357 hourglass_shown_p = 0;
7358 }
7359}
7360
7361
7362\f
7363/***********************************************************************
7364 Tool tips
7365 ***********************************************************************/
7366
7367static Lisp_Object x_create_tip_frame P_ ((struct w32_display_info *,
7368 Lisp_Object, Lisp_Object));
7369static void compute_tip_xy P_ ((struct frame *, Lisp_Object, Lisp_Object,
7370 Lisp_Object, int, int, int *, int *));
7371
7372/* The frame of a currently visible tooltip. */
7373
7374Lisp_Object tip_frame;
7375
7376/* If non-nil, a timer started that hides the last tooltip when it
7377 fires. */
7378
7379Lisp_Object tip_timer;
7380Window tip_window;
7381
7382/* If non-nil, a vector of 3 elements containing the last args
7383 with which x-show-tip was called. See there. */
7384
7385Lisp_Object last_show_tip_args;
7386
7387/* Maximum size for tooltips; a cons (COLUMNS . ROWS). */
7388
7389Lisp_Object Vx_max_tooltip_size;
7390
7391
7392static Lisp_Object
7393unwind_create_tip_frame (frame)
7394 Lisp_Object frame;
7395{
7396 Lisp_Object deleted;
7397
7398 deleted = unwind_create_frame (frame);
7399 if (EQ (deleted, Qt))
7400 {
7401 tip_window = NULL;
7402 tip_frame = Qnil;
7403 }
7404
7405 return deleted;
7406}
7407
7408
7409/* Create a frame for a tooltip on the display described by DPYINFO.
7410 PARMS is a list of frame parameters. TEXT is the string to
7411 display in the tip frame. Value is the frame.
7412
7413 Note that functions called here, esp. x_default_parameter can
7414 signal errors, for instance when a specified color name is
7415 undefined. We have to make sure that we're in a consistent state
7416 when this happens. */
7417
7418static Lisp_Object
7419x_create_tip_frame (dpyinfo, parms, text)
7420 struct w32_display_info *dpyinfo;
7421 Lisp_Object parms, text;
7422{
7423 struct frame *f;
7424 Lisp_Object frame, tem;
7425 Lisp_Object name;
7426 long window_prompting = 0;
7427 int width, height;
7428 int count = SPECPDL_INDEX ();
7429 struct gcpro gcpro1, gcpro2, gcpro3;
7430 struct kboard *kb;
7431 int face_change_count_before = face_change_count;
7432 Lisp_Object buffer;
7433 struct buffer *old_buffer;
7434
7435 check_w32 ();
7436
7437 /* Use this general default value to start with until we know if
7438 this frame has a specified name. */
7439 Vx_resource_name = Vinvocation_name;
7440
7441#ifdef MULTI_KBOARD
7442 kb = dpyinfo->terminal->kboard;
7443#else
7444 kb = &the_only_kboard;
7445#endif
7446
7447 /* Get the name of the frame to use for resource lookup. */
7448 name = w32_get_arg (parms, Qname, "name", "Name", RES_TYPE_STRING);
7449 if (!STRINGP (name)
7450 && !EQ (name, Qunbound)
7451 && !NILP (name))
7452 error ("Invalid frame name--not a string or nil");
7453 Vx_resource_name = name;
7454
7455 frame = Qnil;
7456 GCPRO3 (parms, name, frame);
7457 /* Make a frame without minibuffer nor mode-line. */
7458 f = make_frame (0);
7459 f->wants_modeline = 0;
7460 XSETFRAME (frame, f);
7461
7462 buffer = Fget_buffer_create (build_string (" *tip*"));
7463 Fset_window_buffer (FRAME_ROOT_WINDOW (f), buffer, Qnil);
7464 old_buffer = current_buffer;
7465 set_buffer_internal_1 (XBUFFER (buffer));
7466 current_buffer->truncate_lines = Qnil;
7467 specbind (Qinhibit_read_only, Qt);
7468 specbind (Qinhibit_modification_hooks, Qt);
7469 Ferase_buffer ();
7470 Finsert (1, &text);
7471 set_buffer_internal_1 (old_buffer);
7472
7473 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
7474 record_unwind_protect (unwind_create_tip_frame, frame);
7475
7476 /* By setting the output method, we're essentially saying that
7477 the frame is live, as per FRAME_LIVE_P. If we get a signal
7478 from this point on, x_destroy_window might screw up reference
7479 counts etc. */
7480 f->terminal = dpyinfo->terminal;
7481 f->terminal->reference_count++;
7482 f->output_method = output_w32;
7483 f->output_data.w32 =
7484 (struct w32_output *) xmalloc (sizeof (struct w32_output));
7485 bzero (f->output_data.w32, sizeof (struct w32_output));
7486
7487 FRAME_FONTSET (f) = -1;
7488 f->icon_name = Qnil;
7489
7490#if 0 /* GLYPH_DEBUG TODO: image support. */
7491 image_cache_refcount = FRAME_IMAGE_CACHE (f)->refcount;
7492 dpyinfo_refcount = dpyinfo->reference_count;
7493#endif /* GLYPH_DEBUG */
7494#ifdef MULTI_KBOARD
7495 FRAME_KBOARD (f) = kb;
7496#endif
7497 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
7498 f->output_data.w32->explicit_parent = 0;
7499
7500 /* Set the name; the functions to which we pass f expect the name to
7501 be set. */
7502 if (EQ (name, Qunbound) || NILP (name))
7503 {
7504 f->name = build_string (dpyinfo->w32_id_name);
7505 f->explicit_name = 0;
7506 }
7507 else
7508 {
7509 f->name = name;
7510 f->explicit_name = 1;
7511 /* use the frame's title when getting resources for this frame. */
7512 specbind (Qx_resource_name, name);
7513 }
7514
7515 f->resx = dpyinfo->resx;
7516 f->resy = dpyinfo->resy;
7517
7518#ifdef USE_FONT_BACKEND
7519 if (enable_font_backend)
7520 {
7521 /* Perhaps, we must allow frame parameter, say `font-backend',
7522 to specify which font backends to use. */
7523 register_font_driver (&w32font_driver, f);
7524
7525 x_default_parameter (f, parms, Qfont_backend, Qnil,
7526 "fontBackend", "FontBackend", RES_TYPE_STRING);
7527 }
7528#endif /* USE_FONT_BACKEND */
7529
7530 /* Extract the window parameters from the supplied values
7531 that are needed to determine window geometry. */
7532#ifdef USE_FONT_BACKEND
7533 if (enable_font_backend)
7534 x_default_font_parameter (f, parms);
7535 else
7536#endif /* USE_FONT_BACKEND */
7537 {
7538 Lisp_Object font;
7539
7540 font = w32_get_arg (parms, Qfont, "font", "Font", RES_TYPE_STRING);
7541
7542 BLOCK_INPUT;
7543 /* First, try whatever font the caller has specified. */
7544 if (STRINGP (font))
7545 {
7546 tem = Fquery_fontset (font, Qnil);
7547 if (STRINGP (tem))
7548 font = x_new_fontset (f, tem);
7549 else
7550 font = x_new_font (f, SDATA (font));
7551 }
7552
7553 /* Try out a font which we hope has bold and italic variations. */
7554 if (!STRINGP (font))
7555 font = x_new_font (f, "-*-Courier New-normal-r-*-*-*-100-*-*-c-*-iso8859-1");
7556 if (! STRINGP (font))
7557 font = x_new_font (f, "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1");
7558 /* If those didn't work, look for something which will at least work. */
7559 if (! STRINGP (font))
7560 font = x_new_font (f, "-*-Fixedsys-normal-r-*-*-12-*-*-*-c-*-iso8859-1");
7561 UNBLOCK_INPUT;
7562 if (! STRINGP (font))
7563 font = build_string ("Fixedsys");
7564
7565 x_default_parameter (f, parms, Qfont, font,
7566 "font", "Font", RES_TYPE_STRING);
7567 }
7568
7569 x_default_parameter (f, parms, Qborder_width, make_number (2),
7570 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
7571 /* This defaults to 2 in order to match xterm. We recognize either
7572 internalBorderWidth or internalBorder (which is what xterm calls
7573 it). */
7574 if (NILP (Fassq (Qinternal_border_width, parms)))
7575 {
7576 Lisp_Object value;
7577
7578 value = w32_get_arg (parms, Qinternal_border_width,
7579 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
7580 if (! EQ (value, Qunbound))
7581 parms = Fcons (Fcons (Qinternal_border_width, value),
7582 parms);
7583 }
7584 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
7585 "internalBorderWidth", "internalBorderWidth",
7586 RES_TYPE_NUMBER);
7587
7588 /* Also do the stuff which must be set before the window exists. */
7589 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
7590 "foreground", "Foreground", RES_TYPE_STRING);
7591 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
7592 "background", "Background", RES_TYPE_STRING);
7593 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
7594 "pointerColor", "Foreground", RES_TYPE_STRING);
7595 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
7596 "cursorColor", "Foreground", RES_TYPE_STRING);
7597 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
7598 "borderColor", "BorderColor", RES_TYPE_STRING);
7599
7600 /* Init faces before x_default_parameter is called for scroll-bar
7601 parameters because that function calls x_set_scroll_bar_width,
7602 which calls change_frame_size, which calls Fset_window_buffer,
7603 which runs hooks, which call Fvertical_motion. At the end, we
7604 end up in init_iterator with a null face cache, which should not
7605 happen. */
7606 init_frame_faces (f);
7607
7608 f->output_data.w32->dwStyle = WS_BORDER | WS_POPUP | WS_DISABLED;
7609 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
7610
7611 window_prompting = x_figure_window_size (f, parms, 0);
7612
7613 /* No fringes on tip frame. */
7614 f->fringe_cols = 0;
7615 f->left_fringe_width = 0;
7616 f->right_fringe_width = 0;
7617
7618 BLOCK_INPUT;
7619 my_create_tip_window (f);
7620 UNBLOCK_INPUT;
7621
7622 x_make_gc (f);
7623
7624 x_default_parameter (f, parms, Qauto_raise, Qnil,
7625 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
7626 x_default_parameter (f, parms, Qauto_lower, Qnil,
7627 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
7628 x_default_parameter (f, parms, Qcursor_type, Qbox,
7629 "cursorType", "CursorType", RES_TYPE_SYMBOL);
7630
7631 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
7632 Change will not be effected unless different from the current
7633 FRAME_LINES (f). */
7634 width = FRAME_COLS (f);
7635 height = FRAME_LINES (f);
7636 FRAME_LINES (f) = 0;
7637 SET_FRAME_COLS (f, 0);
7638 change_frame_size (f, height, width, 1, 0, 0);
7639
7640 /* Add `tooltip' frame parameter's default value. */
7641 if (NILP (Fframe_parameter (frame, intern ("tooltip"))))
7642 Fmodify_frame_parameters (frame, Fcons (Fcons (intern ("tooltip"), Qt),
7643 Qnil));
7644
7645 /* Set up faces after all frame parameters are known. This call
7646 also merges in face attributes specified for new frames.
7647
7648 Frame parameters may be changed if .Xdefaults contains
7649 specifications for the default font. For example, if there is an
7650 `Emacs.default.attributeBackground: pink', the `background-color'
7651 attribute of the frame get's set, which let's the internal border
7652 of the tooltip frame appear in pink. Prevent this. */
7653 {
7654 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
7655
7656 /* Set tip_frame here, so that */
7657 tip_frame = frame;
7658 call1 (Qface_set_after_frame_default, frame);
7659
7660 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
7661 Fmodify_frame_parameters (frame, Fcons (Fcons (Qbackground_color, bg),
7662 Qnil));
7663 }
7664
7665 f->no_split = 1;
7666
7667 UNGCPRO;
7668
7669 /* It is now ok to make the frame official even if we get an error
7670 below. And the frame needs to be on Vframe_list or making it
7671 visible won't work. */
7672 Vframe_list = Fcons (frame, Vframe_list);
7673
7674 /* Now that the frame is official, it counts as a reference to
7675 its display. */
7676 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
7677
7678 /* Setting attributes of faces of the tooltip frame from resources
7679 and similar will increment face_change_count, which leads to the
7680 clearing of all current matrices. Since this isn't necessary
7681 here, avoid it by resetting face_change_count to the value it
7682 had before we created the tip frame. */
7683 face_change_count = face_change_count_before;
7684
7685 /* Discard the unwind_protect. */
7686 return unbind_to (count, frame);
7687}
7688
7689
7690/* Compute where to display tip frame F. PARMS is the list of frame
7691 parameters for F. DX and DY are specified offsets from the current
7692 location of the mouse. WIDTH and HEIGHT are the width and height
7693 of the tooltip. Return coordinates relative to the root window of
7694 the display in *ROOT_X, and *ROOT_Y. */
7695
7696static void
7697compute_tip_xy (f, parms, dx, dy, width, height, root_x, root_y)
7698 struct frame *f;
7699 Lisp_Object parms, dx, dy;
7700 int width, height;
7701 int *root_x, *root_y;
7702{
7703 Lisp_Object left, top;
7704 int min_x, min_y, max_x, max_y;
7705
7706 /* User-specified position? */
7707 left = Fcdr (Fassq (Qleft, parms));
7708 top = Fcdr (Fassq (Qtop, parms));
7709
7710 /* Move the tooltip window where the mouse pointer is. Resize and
7711 show it. */
7712 if (!INTEGERP (left) || !INTEGERP (top))
7713 {
7714 POINT pt;
7715
7716 /* Default min and max values. */
7717 min_x = 0;
7718 min_y = 0;
7719 max_x = FRAME_W32_DISPLAY_INFO (f)->width;
7720 max_y = FRAME_W32_DISPLAY_INFO (f)->height;
7721
7722 BLOCK_INPUT;
7723 GetCursorPos (&pt);
7724 *root_x = pt.x;
7725 *root_y = pt.y;
7726 UNBLOCK_INPUT;
7727
7728 /* If multiple monitor support is available, constrain the tip onto
7729 the current monitor. This improves the above by allowing negative
7730 co-ordinates if monitor positions are such that they are valid, and
7731 snaps a tooltip onto a single monitor if we are close to the edge
7732 where it would otherwise flow onto the other monitor (or into
7733 nothingness if there is a gap in the overlap). */
7734 if (monitor_from_point_fn && get_monitor_info_fn)
7735 {
7736 struct MONITOR_INFO info;
7737 HMONITOR monitor
7738 = monitor_from_point_fn (pt, MONITOR_DEFAULT_TO_NEAREST);
7739 info.cbSize = sizeof (info);
7740
7741 if (get_monitor_info_fn (monitor, &info))
7742 {
7743 min_x = info.rcWork.left;
7744 min_y = info.rcWork.top;
7745 max_x = info.rcWork.right;
7746 max_y = info.rcWork.bottom;
7747 }
7748 }
7749 }
7750
7751 if (INTEGERP (top))
7752 *root_y = XINT (top);
7753 else if (*root_y + XINT (dy) <= min_y)
7754 *root_y = min_y; /* Can happen for negative dy */
7755 else if (*root_y + XINT (dy) + height <= max_y)
7756 /* It fits below the pointer */
7757 *root_y += XINT (dy);
7758 else if (height + XINT (dy) + min_y <= *root_y)
7759 /* It fits above the pointer. */
7760 *root_y -= height + XINT (dy);
7761 else
7762 /* Put it on the top. */
7763 *root_y = min_y;
7764
7765 if (INTEGERP (left))
7766 *root_x = XINT (left);
7767 else if (*root_x + XINT (dx) <= min_x)
7768 *root_x = 0; /* Can happen for negative dx */
7769 else if (*root_x + XINT (dx) + width <= max_x)
7770 /* It fits to the right of the pointer. */
7771 *root_x += XINT (dx);
7772 else if (width + XINT (dx) + min_x <= *root_x)
7773 /* It fits to the left of the pointer. */
7774 *root_x -= width + XINT (dx);
7775 else
7776 /* Put it left justified on the screen -- it ought to fit that way. */
7777 *root_x = min_x;
7778}
7779
7780
7781DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
7782 doc: /* Show STRING in a \"tooltip\" window on frame FRAME.
7783A tooltip window is a small window displaying a string.
7784
7785This is an internal function; Lisp code should call `tooltip-show'.
7786
7787FRAME nil or omitted means use the selected frame.
7788
7789PARMS is an optional list of frame parameters which can be
7790used to change the tooltip's appearance.
7791
7792Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
7793means use the default timeout of 5 seconds.
7794
7795If the list of frame parameters PARMS contains a `left' parameter,
7796the tooltip is displayed at that x-position. Otherwise it is
7797displayed at the mouse position, with offset DX added (default is 5 if
7798DX isn't specified). Likewise for the y-position; if a `top' frame
7799parameter is specified, it determines the y-position of the tooltip
7800window, otherwise it is displayed at the mouse position, with offset
7801DY added (default is -10).
7802
7803A tooltip's maximum size is specified by `x-max-tooltip-size'.
7804Text larger than the specified size is clipped. */)
7805 (string, frame, parms, timeout, dx, dy)
7806 Lisp_Object string, frame, parms, timeout, dx, dy;
7807{
7808 struct frame *f;
7809 struct window *w;
7810 int root_x, root_y;
7811 struct buffer *old_buffer;
7812 struct text_pos pos;
7813 int i, width, height;
7814 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7815 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7816 int count = SPECPDL_INDEX ();
7817
7818 specbind (Qinhibit_redisplay, Qt);
7819
7820 GCPRO4 (string, parms, frame, timeout);
7821
7822 CHECK_STRING (string);
7823 f = check_x_frame (frame);
7824 if (NILP (timeout))
7825 timeout = make_number (5);
7826 else
7827 CHECK_NATNUM (timeout);
7828
7829 if (NILP (dx))
7830 dx = make_number (5);
7831 else
7832 CHECK_NUMBER (dx);
7833
7834 if (NILP (dy))
7835 dy = make_number (-10);
7836 else
7837 CHECK_NUMBER (dy);
7838
7839 if (NILP (last_show_tip_args))
7840 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
7841
7842 if (!NILP (tip_frame))
7843 {
7844 Lisp_Object last_string = AREF (last_show_tip_args, 0);
7845 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
7846 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
7847
7848 if (EQ (frame, last_frame)
7849 && !NILP (Fequal (last_string, string))
7850 && !NILP (Fequal (last_parms, parms)))
7851 {
7852 struct frame *f = XFRAME (tip_frame);
7853
7854 /* Only DX and DY have changed. */
7855 if (!NILP (tip_timer))
7856 {
7857 Lisp_Object timer = tip_timer;
7858 tip_timer = Qnil;
7859 call1 (Qcancel_timer, timer);
7860 }
7861
7862 BLOCK_INPUT;
7863 compute_tip_xy (f, parms, dx, dy, FRAME_PIXEL_WIDTH (f),
7864 FRAME_PIXEL_HEIGHT (f), &root_x, &root_y);
7865
7866 /* Put tooltip in topmost group and in position. */
7867 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
7868 root_x, root_y, 0, 0,
7869 SWP_NOSIZE | SWP_NOACTIVATE);
7870
7871 /* Ensure tooltip is on top of other topmost windows (eg menus). */
7872 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
7873 0, 0, 0, 0,
7874 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
7875
7876 UNBLOCK_INPUT;
7877 goto start_timer;
7878 }
7879 }
7880
7881 /* Hide a previous tip, if any. */
7882 Fx_hide_tip ();
7883
7884 ASET (last_show_tip_args, 0, string);
7885 ASET (last_show_tip_args, 1, frame);
7886 ASET (last_show_tip_args, 2, parms);
7887
7888 /* Add default values to frame parameters. */
7889 if (NILP (Fassq (Qname, parms)))
7890 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
7891 if (NILP (Fassq (Qinternal_border_width, parms)))
7892 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
7893 if (NILP (Fassq (Qborder_width, parms)))
7894 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
7895 if (NILP (Fassq (Qborder_color, parms)))
7896 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
7897 if (NILP (Fassq (Qbackground_color, parms)))
7898 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
7899 parms);
7900
7901 /* Block input until the tip has been fully drawn, to avoid crashes
7902 when drawing tips in menus. */
7903 BLOCK_INPUT;
7904
7905 /* Create a frame for the tooltip, and record it in the global
7906 variable tip_frame. */
7907 frame = x_create_tip_frame (FRAME_W32_DISPLAY_INFO (f), parms, string);
7908 f = XFRAME (frame);
7909
7910 /* Set up the frame's root window. */
7911 w = XWINDOW (FRAME_ROOT_WINDOW (f));
7912 w->left_col = w->top_line = make_number (0);
7913
7914 if (CONSP (Vx_max_tooltip_size)
7915 && INTEGERP (XCAR (Vx_max_tooltip_size))
7916 && XINT (XCAR (Vx_max_tooltip_size)) > 0
7917 && INTEGERP (XCDR (Vx_max_tooltip_size))
7918 && XINT (XCDR (Vx_max_tooltip_size)) > 0)
7919 {
7920 w->total_cols = XCAR (Vx_max_tooltip_size);
7921 w->total_lines = XCDR (Vx_max_tooltip_size);
7922 }
7923 else
7924 {
7925 w->total_cols = make_number (80);
7926 w->total_lines = make_number (40);
7927 }
7928
7929 FRAME_TOTAL_COLS (f) = XINT (w->total_cols);
7930 adjust_glyphs (f);
7931 w->pseudo_window_p = 1;
7932
7933 /* Display the tooltip text in a temporary buffer. */
7934 old_buffer = current_buffer;
7935 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->buffer));
7936 current_buffer->truncate_lines = Qnil;
7937 clear_glyph_matrix (w->desired_matrix);
7938 clear_glyph_matrix (w->current_matrix);
7939 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
7940 try_window (FRAME_ROOT_WINDOW (f), pos, 0);
7941
7942 /* Compute width and height of the tooltip. */
7943 width = height = 0;
7944 for (i = 0; i < w->desired_matrix->nrows; ++i)
7945 {
7946 struct glyph_row *row = &w->desired_matrix->rows[i];
7947 struct glyph *last;
7948 int row_width;
7949
7950 /* Stop at the first empty row at the end. */
7951 if (!row->enabled_p || !row->displays_text_p)
7952 break;
7953
7954 /* Let the row go over the full width of the frame. */
7955 row->full_width_p = 1;
7956
7957#ifdef TODO /* Investigate why some fonts need more width than is
7958 calculated for some tooltips. */
7959 /* There's a glyph at the end of rows that is use to place
7960 the cursor there. Don't include the width of this glyph. */
7961 if (row->used[TEXT_AREA])
7962 {
7963 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
7964 row_width = row->pixel_width - last->pixel_width;
7965 }
7966 else
7967#endif
7968 row_width = row->pixel_width;
7969
7970 /* TODO: find why tips do not draw along baseline as instructed. */
7971 height += row->height;
7972 width = max (width, row_width);
7973 }
7974
7975 /* Add the frame's internal border to the width and height the X
7976 window should have. */
7977 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
7978 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
7979
7980 /* Move the tooltip window where the mouse pointer is. Resize and
7981 show it. */
7982 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
7983
7984 {
7985 /* Adjust Window size to take border into account. */
7986 RECT rect;
7987 rect.left = rect.top = 0;
7988 rect.right = width;
7989 rect.bottom = height;
7990 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
7991 FRAME_EXTERNAL_MENU_BAR (f));
7992
7993 /* Position and size tooltip, and put it in the topmost group.
7994 The add-on of 3 to the 5th argument is a kludge: without it,
7995 some fonts cause the last character of the tip to be truncated,
7996 for some obscure reason. */
7997 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
7998 root_x, root_y, rect.right - rect.left + 3,
7999 rect.bottom - rect.top, SWP_NOACTIVATE);
8000
8001 /* Ensure tooltip is on top of other topmost windows (eg menus). */
8002 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
8003 0, 0, 0, 0,
8004 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8005
8006 /* Let redisplay know that we have made the frame visible already. */
8007 f->async_visible = 1;
8008
8009 ShowWindow (FRAME_W32_WINDOW (f), SW_SHOWNOACTIVATE);
8010 }
8011
8012 /* Draw into the window. */
8013 w->must_be_updated_p = 1;
8014 update_single_window (w, 1);
8015
8016 UNBLOCK_INPUT;
8017
8018 /* Restore original current buffer. */
8019 set_buffer_internal_1 (old_buffer);
8020 windows_or_buffers_changed = old_windows_or_buffers_changed;
8021
8022 start_timer:
8023 /* Let the tip disappear after timeout seconds. */
8024 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
8025 intern ("x-hide-tip"));
8026
8027 UNGCPRO;
8028 return unbind_to (count, Qnil);
8029}
8030
8031
8032DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
8033 doc: /* Hide the current tooltip window, if there is any.
8034Value is t if tooltip was open, nil otherwise. */)
8035 ()
8036{
8037 int count;
8038 Lisp_Object deleted, frame, timer;
8039 struct gcpro gcpro1, gcpro2;
8040
8041 /* Return quickly if nothing to do. */
8042 if (NILP (tip_timer) && NILP (tip_frame))
8043 return Qnil;
8044
8045 frame = tip_frame;
8046 timer = tip_timer;
8047 GCPRO2 (frame, timer);
8048 tip_frame = tip_timer = deleted = Qnil;
8049
8050 count = SPECPDL_INDEX ();
8051 specbind (Qinhibit_redisplay, Qt);
8052 specbind (Qinhibit_quit, Qt);
8053
8054 if (!NILP (timer))
8055 call1 (Qcancel_timer, timer);
8056
8057 if (FRAMEP (frame))
8058 {
8059 Fdelete_frame (frame, Qnil);
8060 deleted = Qt;
8061 }
8062
8063 UNGCPRO;
8064 return unbind_to (count, deleted);
8065}
8066
8067
8068\f
8069/***********************************************************************
8070 File selection dialog
8071 ***********************************************************************/
8072extern Lisp_Object Qfile_name_history;
8073
8074/* Callback for altering the behaviour of the Open File dialog.
8075 Makes the Filename text field contain "Current Directory" and be
8076 read-only when "Directories" is selected in the filter. This
8077 allows us to work around the fact that the standard Open File
8078 dialog does not support directories. */
8079UINT CALLBACK
8080file_dialog_callback (hwnd, msg, wParam, lParam)
8081 HWND hwnd;
8082 UINT msg;
8083 WPARAM wParam;
8084 LPARAM lParam;
8085{
8086 if (msg == WM_NOTIFY)
8087 {
8088 OFNOTIFY * notify = (OFNOTIFY *)lParam;
8089 /* Detect when the Filter dropdown is changed. */
8090 if (notify->hdr.code == CDN_TYPECHANGE
8091 || notify->hdr.code == CDN_INITDONE)
8092 {
8093 HWND dialog = GetParent (hwnd);
8094 HWND edit_control = GetDlgItem (dialog, FILE_NAME_TEXT_FIELD);
8095
8096 /* Directories is in index 2. */
8097 if (notify->lpOFN->nFilterIndex == 2)
8098 {
8099 CommDlg_OpenSave_SetControlText (dialog, FILE_NAME_TEXT_FIELD,
8100 "Current Directory");
8101 EnableWindow (edit_control, FALSE);
8102 }
8103 else
8104 {
8105 /* Don't override default filename on init done. */
8106 if (notify->hdr.code == CDN_TYPECHANGE)
8107 CommDlg_OpenSave_SetControlText (dialog,
8108 FILE_NAME_TEXT_FIELD, "");
8109 EnableWindow (edit_control, TRUE);
8110 }
8111 }
8112 }
8113 return 0;
8114}
8115
8116/* Since we compile with _WIN32_WINNT set to 0x0400 (for NT4 compatibility)
8117 we end up with the old file dialogs. Define a big enough struct for the
8118 new dialog to trick GetOpenFileName into giving us the new dialogs on
8119 Windows 2000 and XP. */
8120typedef struct
8121{
8122 OPENFILENAME real_details;
8123 void * pReserved;
8124 DWORD dwReserved;
8125 DWORD FlagsEx;
8126} NEWOPENFILENAME;
8127
8128
8129DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
8130 doc: /* Read file name, prompting with PROMPT in directory DIR.
8131Use a file selection dialog.
8132Select DEFAULT-FILENAME in the dialog's file selection box, if
8133specified. Ensure that file exists if MUSTMATCH is non-nil.
8134If ONLY-DIR-P is non-nil, the user can only select directories. */)
8135 (prompt, dir, default_filename, mustmatch, only_dir_p)
8136 Lisp_Object prompt, dir, default_filename, mustmatch, only_dir_p;
8137{
8138 struct frame *f = SELECTED_FRAME ();
8139 Lisp_Object file = Qnil;
8140 int count = SPECPDL_INDEX ();
8141 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
8142 char filename[MAX_PATH + 1];
8143 char init_dir[MAX_PATH + 1];
8144 int default_filter_index = 1; /* 1: All Files, 2: Directories only */
8145
8146 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
8147 CHECK_STRING (prompt);
8148 CHECK_STRING (dir);
8149
8150 /* Create the dialog with PROMPT as title, using DIR as initial
8151 directory and using "*" as pattern. */
8152 dir = Fexpand_file_name (dir, Qnil);
8153 strncpy (init_dir, SDATA (ENCODE_FILE (dir)), MAX_PATH);
8154 init_dir[MAX_PATH] = '\0';
8155 unixtodos_filename (init_dir);
8156
8157 if (STRINGP (default_filename))
8158 {
8159 char *file_name_only;
8160 char *full_path_name = SDATA (ENCODE_FILE (default_filename));
8161
8162 unixtodos_filename (full_path_name);
8163
8164 file_name_only = strrchr (full_path_name, '\\');
8165 if (!file_name_only)
8166 file_name_only = full_path_name;
8167 else
8168 file_name_only++;
8169
8170 strncpy (filename, file_name_only, MAX_PATH);
8171 filename[MAX_PATH] = '\0';
8172 }
8173 else
8174 filename[0] = '\0';
8175
8176 {
8177 NEWOPENFILENAME new_file_details;
8178 BOOL file_opened = FALSE;
8179 OPENFILENAME * file_details = &new_file_details.real_details;
8180
8181 /* Prevent redisplay. */
8182 specbind (Qinhibit_redisplay, Qt);
8183 BLOCK_INPUT;
8184
8185 bzero (&new_file_details, sizeof (new_file_details));
8186 /* Apparently NT4 crashes if you give it an unexpected size.
8187 I'm not sure about Windows 9x, so play it safe. */
8188 if (w32_major_version > 4 && w32_major_version < 95)
8189 file_details->lStructSize = sizeof (NEWOPENFILENAME);
8190 else
8191 file_details->lStructSize = sizeof (OPENFILENAME);
8192
8193 file_details->hwndOwner = FRAME_W32_WINDOW (f);
8194 /* Undocumented Bug in Common File Dialog:
8195 If a filter is not specified, shell links are not resolved. */
8196 file_details->lpstrFilter = "All Files (*.*)\0*.*\0Directories\0*|*\0\0";
8197 file_details->lpstrFile = filename;
8198 file_details->nMaxFile = sizeof (filename);
8199 file_details->lpstrInitialDir = init_dir;
8200 file_details->lpstrTitle = SDATA (prompt);
8201
8202 if (! NILP (only_dir_p))
8203 default_filter_index = 2;
8204
8205 file_details->nFilterIndex = default_filter_index;
8206
8207 file_details->Flags = (OFN_HIDEREADONLY | OFN_NOCHANGEDIR
8208 | OFN_EXPLORER | OFN_ENABLEHOOK);
8209 if (!NILP (mustmatch))
8210 {
8211 /* Require that the path to the parent directory exists. */
8212 file_details->Flags |= OFN_PATHMUSTEXIST;
8213 /* If we are looking for a file, require that it exists. */
8214 if (NILP (only_dir_p))
8215 file_details->Flags |= OFN_FILEMUSTEXIST;
8216 }
8217
8218 file_details->lpfnHook = (LPOFNHOOKPROC) file_dialog_callback;
8219
8220 file_opened = GetOpenFileName (file_details);
8221
8222 UNBLOCK_INPUT;
8223
8224 if (file_opened)
8225 {
8226 dostounix_filename (filename);
8227
8228 if (file_details->nFilterIndex == 2)
8229 {
8230 /* "Directories" selected - strip dummy file name. */
8231 char * last = strrchr (filename, '/');
8232 *last = '\0';
8233 }
8234
8235 file = DECODE_FILE (build_string (filename));
8236 }
8237 /* User cancelled the dialog without making a selection. */
8238 else if (!CommDlgExtendedError ())
8239 file = Qnil;
8240 /* An error occurred, fallback on reading from the mini-buffer. */
8241 else
8242 file = Fcompleting_read (prompt, intern ("read-file-name-internal"),
8243 dir, mustmatch, dir, Qfile_name_history,
8244 default_filename, Qnil);
8245
8246 file = unbind_to (count, file);
8247 }
8248
8249 UNGCPRO;
8250
8251 /* Make "Cancel" equivalent to C-g. */
8252 if (NILP (file))
8253 Fsignal (Qquit, Qnil);
8254
8255 return unbind_to (count, file);
8256}
8257
8258
8259\f
8260/***********************************************************************
8261 w32 specialized functions
8262 ***********************************************************************/
8263
8264DEFUN ("w32-select-font", Fw32_select_font, Sw32_select_font, 0, 2, 0,
8265 doc: /* Select a font for the named FRAME using the W32 font dialog.
8266Return an X-style font string corresponding to the selection.
8267
8268If FRAME is omitted or nil, it defaults to the selected frame.
8269If INCLUDE-PROPORTIONAL is non-nil, include proportional fonts
8270in the font selection dialog. */)
8271 (frame, include_proportional)
8272 Lisp_Object frame, include_proportional;
8273{
8274 FRAME_PTR f = check_x_frame (frame);
8275 CHOOSEFONT cf;
8276 LOGFONT lf;
8277 TEXTMETRIC tm;
8278 HDC hdc;
8279 HANDLE oldobj;
8280 char buf[100];
8281
8282 bzero (&cf, sizeof (cf));
8283 bzero (&lf, sizeof (lf));
8284
8285 cf.lStructSize = sizeof (cf);
8286 cf.hwndOwner = FRAME_W32_WINDOW (f);
8287 cf.Flags = CF_FORCEFONTEXIST | CF_SCREENFONTS | CF_NOVERTFONTS;
8288
8289 /* Unless include_proportional is non-nil, limit the selection to
8290 monospaced fonts. */
8291 if (NILP (include_proportional))
8292 cf.Flags |= CF_FIXEDPITCHONLY;
8293
8294 cf.lpLogFont = &lf;
8295
8296 /* Initialize as much of the font details as we can from the current
8297 default font. */
8298 hdc = GetDC (FRAME_W32_WINDOW (f));
8299 oldobj = SelectObject (hdc, FRAME_FONT (f)->hfont);
8300 GetTextFace (hdc, LF_FACESIZE, lf.lfFaceName);
8301 if (GetTextMetrics (hdc, &tm))
8302 {
8303 lf.lfHeight = tm.tmInternalLeading - tm.tmHeight;
8304 lf.lfWeight = tm.tmWeight;
8305 lf.lfItalic = tm.tmItalic;
8306 lf.lfUnderline = tm.tmUnderlined;
8307 lf.lfStrikeOut = tm.tmStruckOut;
8308 lf.lfCharSet = tm.tmCharSet;
8309 cf.Flags |= CF_INITTOLOGFONTSTRUCT;
8310 }
8311 SelectObject (hdc, oldobj);
8312 ReleaseDC (FRAME_W32_WINDOW (f), hdc);
8313
8314 if (!ChooseFont (&cf) || !w32_to_x_font (&lf, buf, 100, NULL))
8315 return Qnil;
8316
8317 return build_string (buf);
8318}
8319
8320DEFUN ("w32-send-sys-command", Fw32_send_sys_command,
8321 Sw32_send_sys_command, 1, 2, 0,
8322 doc: /* Send frame a Windows WM_SYSCOMMAND message of type COMMAND.
8323Some useful values for COMMAND are #xf030 to maximize frame (#xf020
8324to minimize), #xf120 to restore frame to original size, and #xf100
8325to activate the menubar for keyboard access. #xf140 activates the
8326screen saver if defined.
8327
8328If optional parameter FRAME is not specified, use selected frame. */)
8329 (command, frame)
8330 Lisp_Object command, frame;
8331{
8332 FRAME_PTR f = check_x_frame (frame);
8333
8334 CHECK_NUMBER (command);
8335
8336 PostMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, XINT (command), 0);
8337
8338 return Qnil;
8339}
8340
8341DEFUN ("w32-shell-execute", Fw32_shell_execute, Sw32_shell_execute, 2, 4, 0,
8342 doc: /* Get Windows to perform OPERATION on DOCUMENT.
8343This is a wrapper around the ShellExecute system function, which
8344invokes the application registered to handle OPERATION for DOCUMENT.
8345
8346OPERATION is either nil or a string that names a supported operation.
8347What operations can be used depends on the particular DOCUMENT and its
8348handler application, but typically it is one of the following common
8349operations:
8350
8351 \"open\" - open DOCUMENT, which could be a file, a directory, or an
8352 executable program. If it is an application, that
8353 application is launched in the current buffer's default
8354 directory. Otherwise, the application associated with
8355 DOCUMENT is launched in the buffer's default directory.
8356 \"print\" - print DOCUMENT, which must be a file
8357 \"explore\" - start the Windows Explorer on DOCUMENT
8358 \"edit\" - launch an editor and open DOCUMENT for editing; which
8359 editor is launched depends on the association for the
8360 specified DOCUMENT
8361 \"find\" - initiate search starting from DOCUMENT which must specify
8362 a directory
8363 nil - invoke the default OPERATION, or \"open\" if default is
8364 not defined or unavailable
8365
8366DOCUMENT is typically the name of a document file or a URL, but can
8367also be a program executable to run, or a directory to open in the
8368Windows Explorer.
8369
8370If DOCUMENT is a program executable, the optional third arg PARAMETERS
8371can be a string containing command line parameters that will be passed
8372to the program; otherwise, PARAMETERS should be nil or unspecified.
8373
8374Optional fourth argument SHOW-FLAG can be used to control how the
8375application will be displayed when it is invoked. If SHOW-FLAG is nil
8376or unspecified, the application is displayed normally, otherwise it is
8377an integer representing a ShowWindow flag:
8378
8379 0 - start hidden
8380 1 - start normally
8381 3 - start maximized
8382 6 - start minimized */)
8383 (operation, document, parameters, show_flag)
8384 Lisp_Object operation, document, parameters, show_flag;
8385{
8386 Lisp_Object current_dir;
8387
8388 CHECK_STRING (document);
8389
8390 /* Encode filename, current directory and parameters. */
8391 current_dir = ENCODE_FILE (current_buffer->directory);
8392 document = ENCODE_FILE (document);
8393 if (STRINGP (parameters))
8394 parameters = ENCODE_SYSTEM (parameters);
8395
8396 if ((int) ShellExecute (NULL,
8397 (STRINGP (operation) ?
8398 SDATA (operation) : NULL),
8399 SDATA (document),
8400 (STRINGP (parameters) ?
8401 SDATA (parameters) : NULL),
8402 SDATA (current_dir),
8403 (INTEGERP (show_flag) ?
8404 XINT (show_flag) : SW_SHOWDEFAULT))
8405 > 32)
8406 return Qt;
8407 error ("ShellExecute failed: %s", w32_strerror (0));
8408}
8409
8410/* Lookup virtual keycode from string representing the name of a
8411 non-ascii keystroke into the corresponding virtual key, using
8412 lispy_function_keys. */
8413static int
8414lookup_vk_code (char *key)
8415{
8416 int i;
8417
8418 for (i = 0; i < 256; i++)
8419 if (lispy_function_keys[i]
8420 && strcmp (lispy_function_keys[i], key) == 0)
8421 return i;
8422
8423 return -1;
8424}
8425
8426/* Convert a one-element vector style key sequence to a hot key
8427 definition. */
8428static Lisp_Object
8429w32_parse_hot_key (key)
8430 Lisp_Object key;
8431{
8432 /* Copied from Fdefine_key and store_in_keymap. */
8433 register Lisp_Object c;
8434 int vk_code;
8435 int lisp_modifiers;
8436 int w32_modifiers;
8437 struct gcpro gcpro1;
8438
8439 CHECK_VECTOR (key);
8440
8441 if (XFASTINT (Flength (key)) != 1)
8442 return Qnil;
8443
8444 GCPRO1 (key);
8445
8446 c = Faref (key, make_number (0));
8447
8448 if (CONSP (c) && lucid_event_type_list_p (c))
8449 c = Fevent_convert_list (c);
8450
8451 UNGCPRO;
8452
8453 if (! INTEGERP (c) && ! SYMBOLP (c))
8454 error ("Key definition is invalid");
8455
8456 /* Work out the base key and the modifiers. */
8457 if (SYMBOLP (c))
8458 {
8459 c = parse_modifiers (c);
8460 lisp_modifiers = XINT (Fcar (Fcdr (c)));
8461 c = Fcar (c);
8462 if (!SYMBOLP (c))
8463 abort ();
8464 vk_code = lookup_vk_code (SDATA (SYMBOL_NAME (c)));
8465 }
8466 else if (INTEGERP (c))
8467 {
8468 lisp_modifiers = XINT (c) & ~CHARACTERBITS;
8469 /* Many ascii characters are their own virtual key code. */
8470 vk_code = XINT (c) & CHARACTERBITS;
8471 }
8472
8473 if (vk_code < 0 || vk_code > 255)
8474 return Qnil;
8475
8476 if ((lisp_modifiers & meta_modifier) != 0
8477 && !NILP (Vw32_alt_is_meta))
8478 lisp_modifiers |= alt_modifier;
8479
8480 /* Supply defs missing from mingw32. */
8481#ifndef MOD_ALT
8482#define MOD_ALT 0x0001
8483#define MOD_CONTROL 0x0002
8484#define MOD_SHIFT 0x0004
8485#define MOD_WIN 0x0008
8486#endif
8487
8488 /* Convert lisp modifiers to Windows hot-key form. */
8489 w32_modifiers = (lisp_modifiers & hyper_modifier) ? MOD_WIN : 0;
8490 w32_modifiers |= (lisp_modifiers & alt_modifier) ? MOD_ALT : 0;
8491 w32_modifiers |= (lisp_modifiers & ctrl_modifier) ? MOD_CONTROL : 0;
8492 w32_modifiers |= (lisp_modifiers & shift_modifier) ? MOD_SHIFT : 0;
8493
8494 return HOTKEY (vk_code, w32_modifiers);
8495}
8496
8497DEFUN ("w32-register-hot-key", Fw32_register_hot_key,
8498 Sw32_register_hot_key, 1, 1, 0,
8499 doc: /* Register KEY as a hot-key combination.
8500Certain key combinations like Alt-Tab are reserved for system use on
8501Windows, and therefore are normally intercepted by the system. However,
8502most of these key combinations can be received by registering them as
8503hot-keys, overriding their special meaning.
8504
8505KEY must be a one element key definition in vector form that would be
8506acceptable to `define-key' (e.g. [A-tab] for Alt-Tab). The meta
8507modifier is interpreted as Alt if `w32-alt-is-meta' is t, and hyper
8508is always interpreted as the Windows modifier keys.
8509
8510The return value is the hotkey-id if registered, otherwise nil. */)
8511 (key)
8512 Lisp_Object key;
8513{
8514 key = w32_parse_hot_key (key);
8515
8516 if (!NILP (key) && NILP (Fmemq (key, w32_grabbed_keys)))
8517 {
8518 /* Reuse an empty slot if possible. */
8519 Lisp_Object item = Fmemq (Qnil, w32_grabbed_keys);
8520
8521 /* Safe to add new key to list, even if we have focus. */
8522 if (NILP (item))
8523 w32_grabbed_keys = Fcons (key, w32_grabbed_keys);
8524 else
8525 XSETCAR (item, key);
8526
8527 /* Notify input thread about new hot-key definition, so that it
8528 takes effect without needing to switch focus. */
8529#ifdef USE_LISP_UNION_TYPE
8530 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
8531 (WPARAM) key.i, 0);
8532#else
8533 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
8534 (WPARAM) key, 0);
8535#endif
8536 }
8537
8538 return key;
8539}
8540
8541DEFUN ("w32-unregister-hot-key", Fw32_unregister_hot_key,
8542 Sw32_unregister_hot_key, 1, 1, 0,
8543 doc: /* Unregister KEY as a hot-key combination. */)
8544 (key)
8545 Lisp_Object key;
8546{
8547 Lisp_Object item;
8548
8549 if (!INTEGERP (key))
8550 key = w32_parse_hot_key (key);
8551
8552 item = Fmemq (key, w32_grabbed_keys);
8553
8554 if (!NILP (item))
8555 {
8556 /* Notify input thread about hot-key definition being removed, so
8557 that it takes effect without needing focus switch. */
8558#ifdef USE_LISP_UNION_TYPE
8559 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
8560 (WPARAM) XINT (XCAR (item)), (LPARAM) item.i))
8561#else
8562 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
8563 (WPARAM) XINT (XCAR (item)), (LPARAM) item))
8564#endif
8565 {
8566 MSG msg;
8567 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
8568 }
8569 return Qt;
8570 }
8571 return Qnil;
8572}
8573
8574DEFUN ("w32-registered-hot-keys", Fw32_registered_hot_keys,
8575 Sw32_registered_hot_keys, 0, 0, 0,
8576 doc: /* Return list of registered hot-key IDs. */)
8577 ()
8578{
8579 return Fdelq (Qnil, Fcopy_sequence (w32_grabbed_keys));
8580}
8581
8582DEFUN ("w32-reconstruct-hot-key", Fw32_reconstruct_hot_key,
8583 Sw32_reconstruct_hot_key, 1, 1, 0,
8584 doc: /* Convert hot-key ID to a lisp key combination.
8585usage: (w32-reconstruct-hot-key ID) */)
8586 (hotkeyid)
8587 Lisp_Object hotkeyid;
8588{
8589 int vk_code, w32_modifiers;
8590 Lisp_Object key;
8591
8592 CHECK_NUMBER (hotkeyid);
8593
8594 vk_code = HOTKEY_VK_CODE (hotkeyid);
8595 w32_modifiers = HOTKEY_MODIFIERS (hotkeyid);
8596
8597 if (vk_code < 256 && lispy_function_keys[vk_code])
8598 key = intern (lispy_function_keys[vk_code]);
8599 else
8600 key = make_number (vk_code);
8601
8602 key = Fcons (key, Qnil);
8603 if (w32_modifiers & MOD_SHIFT)
8604 key = Fcons (Qshift, key);
8605 if (w32_modifiers & MOD_CONTROL)
8606 key = Fcons (Qctrl, key);
8607 if (w32_modifiers & MOD_ALT)
8608 key = Fcons (NILP (Vw32_alt_is_meta) ? Qalt : Qmeta, key);
8609 if (w32_modifiers & MOD_WIN)
8610 key = Fcons (Qhyper, key);
8611
8612 return key;
8613}
8614
8615DEFUN ("w32-toggle-lock-key", Fw32_toggle_lock_key,
8616 Sw32_toggle_lock_key, 1, 2, 0,
8617 doc: /* Toggle the state of the lock key KEY.
8618KEY can be `capslock', `kp-numlock', or `scroll'.
8619If the optional parameter NEW-STATE is a number, then the state of KEY
8620is set to off if the low bit of NEW-STATE is zero, otherwise on. */)
8621 (key, new_state)
8622 Lisp_Object key, new_state;
8623{
8624 int vk_code;
8625
8626 if (EQ (key, intern ("capslock")))
8627 vk_code = VK_CAPITAL;
8628 else if (EQ (key, intern ("kp-numlock")))
8629 vk_code = VK_NUMLOCK;
8630 else if (EQ (key, intern ("scroll")))
8631 vk_code = VK_SCROLL;
8632 else
8633 return Qnil;
8634
8635 if (!dwWindowsThreadId)
8636 return make_number (w32_console_toggle_lock_key (vk_code, new_state));
8637
8638#ifdef USE_LISP_UNION_TYPE
8639 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
8640 (WPARAM) vk_code, (LPARAM) new_state.i))
8641#else
8642 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
8643 (WPARAM) vk_code, (LPARAM) new_state))
8644#endif
8645 {
8646 MSG msg;
8647 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
8648 return make_number (msg.wParam);
8649 }
8650 return Qnil;
8651}
8652
8653DEFUN ("w32-window-exists-p", Fw32_window_exists_p, Sw32_window_exists_p,
8654 2, 2, 0,
8655 doc: /* Return non-nil if a window exists with the specified CLASS and NAME.
8656
8657This is a direct interface to the Windows API FindWindow function. */)
8658 (class, name)
8659Lisp_Object class, name;
8660{
8661 HWND hnd;
8662
8663 if (!NILP (class))
8664 CHECK_STRING (class);
8665 if (!NILP (name))
8666 CHECK_STRING (name);
8667
8668 hnd = FindWindow (STRINGP (class) ? ((LPCTSTR) SDATA (class)) : NULL,
8669 STRINGP (name) ? ((LPCTSTR) SDATA (name)) : NULL);
8670 if (!hnd)
8671 return Qnil;
8672 return Qt;
8673}
8674
8675
8676\f
8677DEFUN ("file-system-info", Ffile_system_info, Sfile_system_info, 1, 1, 0,
8678 doc: /* Return storage information about the file system FILENAME is on.
8679Value is a list of floats (TOTAL FREE AVAIL), where TOTAL is the total
8680storage of the file system, FREE is the free storage, and AVAIL is the
8681storage available to a non-superuser. All 3 numbers are in bytes.
8682If the underlying system call fails, value is nil. */)
8683 (filename)
8684 Lisp_Object filename;
8685{
8686 Lisp_Object encoded, value;
8687
8688 CHECK_STRING (filename);
8689 filename = Fexpand_file_name (filename, Qnil);
8690 encoded = ENCODE_FILE (filename);
8691
8692 value = Qnil;
8693
8694 /* Determining the required information on Windows turns out, sadly,
8695 to be more involved than one would hope. The original Win32 api
8696 call for this will return bogus information on some systems, but we
8697 must dynamically probe for the replacement api, since that was
8698 added rather late on. */
8699 {
8700 HMODULE hKernel = GetModuleHandle ("kernel32");
8701 BOOL (*pfn_GetDiskFreeSpaceEx)
8702 (char *, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER)
8703 = (void *) GetProcAddress (hKernel, "GetDiskFreeSpaceEx");
8704
8705 /* On Windows, we may need to specify the root directory of the
8706 volume holding FILENAME. */
8707 char rootname[MAX_PATH];
8708 char *name = SDATA (encoded);
8709
8710 /* find the root name of the volume if given */
8711 if (isalpha (name[0]) && name[1] == ':')
8712 {
8713 rootname[0] = name[0];
8714 rootname[1] = name[1];
8715 rootname[2] = '\\';
8716 rootname[3] = 0;
8717 }
8718 else if (IS_DIRECTORY_SEP (name[0]) && IS_DIRECTORY_SEP (name[1]))
8719 {
8720 char *str = rootname;
8721 int slashes = 4;
8722 do
8723 {
8724 if (IS_DIRECTORY_SEP (*name) && --slashes == 0)
8725 break;
8726 *str++ = *name++;
8727 }
8728 while ( *name );
8729
8730 *str++ = '\\';
8731 *str = 0;
8732 }
8733
8734 if (pfn_GetDiskFreeSpaceEx)
8735 {
8736 /* Unsigned large integers cannot be cast to double, so
8737 use signed ones instead. */
8738 LARGE_INTEGER availbytes;
8739 LARGE_INTEGER freebytes;
8740 LARGE_INTEGER totalbytes;
8741
8742 if (pfn_GetDiskFreeSpaceEx (rootname,
8743 (ULARGE_INTEGER *)&availbytes,
8744 (ULARGE_INTEGER *)&totalbytes,
8745 (ULARGE_INTEGER *)&freebytes))
8746 value = list3 (make_float ((double) totalbytes.QuadPart),
8747 make_float ((double) freebytes.QuadPart),
8748 make_float ((double) availbytes.QuadPart));
8749 }
8750 else
8751 {
8752 DWORD sectors_per_cluster;
8753 DWORD bytes_per_sector;
8754 DWORD free_clusters;
8755 DWORD total_clusters;
8756
8757 if (GetDiskFreeSpace (rootname,
8758 &sectors_per_cluster,
8759 &bytes_per_sector,
8760 &free_clusters,
8761 &total_clusters))
8762 value = list3 (make_float ((double) total_clusters
8763 * sectors_per_cluster * bytes_per_sector),
8764 make_float ((double) free_clusters
8765 * sectors_per_cluster * bytes_per_sector),
8766 make_float ((double) free_clusters
8767 * sectors_per_cluster * bytes_per_sector));
8768 }
8769 }
8770
8771 return value;
8772}
8773\f
8774DEFUN ("default-printer-name", Fdefault_printer_name, Sdefault_printer_name,
8775 0, 0, 0, doc: /* Return the name of Windows default printer device. */)
8776 ()
8777{
8778 static char pname_buf[256];
8779 int err;
8780 HANDLE hPrn;
8781 PRINTER_INFO_2 *ppi2 = NULL;
8782 DWORD dwNeeded = 0, dwReturned = 0;
8783
8784 /* Retrieve the default string from Win.ini (the registry).
8785 * String will be in form "printername,drivername,portname".
8786 * This is the most portable way to get the default printer. */
8787 if (GetProfileString ("windows", "device", ",,", pname_buf, sizeof (pname_buf)) <= 0)
8788 return Qnil;
8789 /* printername precedes first "," character */
8790 strtok (pname_buf, ",");
8791 /* We want to know more than the printer name */
8792 if (!OpenPrinter (pname_buf, &hPrn, NULL))
8793 return Qnil;
8794 GetPrinter (hPrn, 2, NULL, 0, &dwNeeded);
8795 if (dwNeeded == 0)
8796 {
8797 ClosePrinter (hPrn);
8798 return Qnil;
8799 }
8800 /* Allocate memory for the PRINTER_INFO_2 struct */
8801 ppi2 = (PRINTER_INFO_2 *) xmalloc (dwNeeded);
8802 if (!ppi2)
8803 {
8804 ClosePrinter (hPrn);
8805 return Qnil;
8806 }
8807 /* Call GetPrinter again with big enouth memory block */
8808 err = GetPrinter (hPrn, 2, (LPBYTE)ppi2, dwNeeded, &dwReturned);
8809 ClosePrinter (hPrn);
8810 if (!err)
8811 {
8812 xfree (ppi2);
8813 return Qnil;
8814 }
8815
8816 if (ppi2)
8817 {
8818 if (ppi2->Attributes & PRINTER_ATTRIBUTE_SHARED && ppi2->pServerName)
8819 {
8820 /* a remote printer */
8821 if (*ppi2->pServerName == '\\')
8822 _snprintf (pname_buf, sizeof (pname_buf), "%s\\%s", ppi2->pServerName,
8823 ppi2->pShareName);
8824 else
8825 _snprintf (pname_buf, sizeof (pname_buf), "\\\\%s\\%s", ppi2->pServerName,
8826 ppi2->pShareName);
8827 pname_buf[sizeof (pname_buf) - 1] = '\0';
8828 }
8829 else
8830 {
8831 /* a local printer */
8832 strncpy (pname_buf, ppi2->pPortName, sizeof (pname_buf));
8833 pname_buf[sizeof (pname_buf) - 1] = '\0';
8834 /* `pPortName' can include several ports, delimited by ','.
8835 * we only use the first one. */
8836 strtok (pname_buf, ",");
8837 }
8838 xfree (ppi2);
8839 }
8840
8841 return build_string (pname_buf);
8842}
8843\f
8844/***********************************************************************
8845 Initialization
8846 ***********************************************************************/
8847
8848/* Keep this list in the same order as frame_parms in frame.c.
8849 Use 0 for unsupported frame parameters. */
8850
8851frame_parm_handler w32_frame_parm_handlers[] =
8852{
8853 x_set_autoraise,
8854 x_set_autolower,
8855 x_set_background_color,
8856 x_set_border_color,
8857 x_set_border_width,
8858 x_set_cursor_color,
8859 x_set_cursor_type,
8860 x_set_font,
8861 x_set_foreground_color,
8862 x_set_icon_name,
8863 x_set_icon_type,
8864 x_set_internal_border_width,
8865 x_set_menu_bar_lines,
8866 x_set_mouse_color,
8867 x_explicitly_set_name,
8868 x_set_scroll_bar_width,
8869 x_set_title,
8870 x_set_unsplittable,
8871 x_set_vertical_scroll_bars,
8872 x_set_visibility,
8873 x_set_tool_bar_lines,
8874 0, /* x_set_scroll_bar_foreground, */
8875 0, /* x_set_scroll_bar_background, */
8876 x_set_screen_gamma,
8877 x_set_line_spacing,
8878 x_set_fringe_width,
8879 x_set_fringe_width,
8880 0, /* x_set_wait_for_wm, */
8881 x_set_fullscreen,
8882#ifdef USE_FONT_BACKEND
8883 x_set_font_backend
8884#endif
8885};
8886
8887void
8888syms_of_w32fns ()
8889{
8890 globals_of_w32fns ();
8891 /* This is zero if not using MS-Windows. */
8892 w32_in_use = 0;
8893 track_mouse_window = NULL;
8894
8895 w32_visible_system_caret_hwnd = NULL;
8896
8897 DEFSYM (Qnone, "none");
8898 DEFSYM (Qsuppress_icon, "suppress-icon");
8899 DEFSYM (Qundefined_color, "undefined-color");
8900 DEFSYM (Qcancel_timer, "cancel-timer");
8901 DEFSYM (Qhyper, "hyper");
8902 DEFSYM (Qsuper, "super");
8903 DEFSYM (Qmeta, "meta");
8904 DEFSYM (Qalt, "alt");
8905 DEFSYM (Qctrl, "ctrl");
8906 DEFSYM (Qcontrol, "control");
8907 DEFSYM (Qshift, "shift");
8908 /* This is the end of symbol initialization. */
8909
8910 /* Text property `display' should be nonsticky by default. */
8911 Vtext_property_default_nonsticky
8912 = Fcons (Fcons (Qdisplay, Qt), Vtext_property_default_nonsticky);
8913
8914
8915 Fput (Qundefined_color, Qerror_conditions,
8916 Fcons (Qundefined_color, Fcons (Qerror, Qnil)));
8917 Fput (Qundefined_color, Qerror_message,
8918 build_string ("Undefined color"));
8919
8920 staticpro (&w32_grabbed_keys);
8921 w32_grabbed_keys = Qnil;
8922
8923 DEFVAR_LISP ("w32-color-map", &Vw32_color_map,
8924 doc: /* An array of color name mappings for Windows. */);
8925 Vw32_color_map = Qnil;
8926
8927 DEFVAR_LISP ("w32-pass-alt-to-system", &Vw32_pass_alt_to_system,
8928 doc: /* Non-nil if Alt key presses are passed on to Windows.
8929When non-nil, for example, Alt pressed and released and then space will
8930open the System menu. When nil, Emacs processes the Alt key events, and
8931then silently swallows them. */);
8932 Vw32_pass_alt_to_system = Qnil;
8933
8934 DEFVAR_LISP ("w32-alt-is-meta", &Vw32_alt_is_meta,
8935 doc: /* Non-nil if the Alt key is to be considered the same as the META key.
8936When nil, Emacs will translate the Alt key to the ALT modifier, not to META. */);
8937 Vw32_alt_is_meta = Qt;
8938
8939 DEFVAR_INT ("w32-quit-key", &w32_quit_key,
8940 doc: /* If non-zero, the virtual key code for an alternative quit key. */);
8941 w32_quit_key = 0;
8942
8943 DEFVAR_LISP ("w32-pass-lwindow-to-system",
8944 &Vw32_pass_lwindow_to_system,
8945 doc: /* If non-nil, the left \"Windows\" key is passed on to Windows.
8946
8947When non-nil, the Start menu is opened by tapping the key.
8948If you set this to nil, the left \"Windows\" key is processed by Emacs
8949according to the value of `w32-lwindow-modifier', which see.
8950
8951Note that some combinations of the left \"Windows\" key with other keys are
8952caught by Windows at low level, and so binding them in Emacs will have no
8953effect. For example, <lwindow>-r always pops up the Windows Run dialog,
8954<lwindow>-<Pause> pops up the "System Properties" dialog, etc. However, see
8955the doc string of `w32-phantom-key-code'. */);
8956 Vw32_pass_lwindow_to_system = Qt;
8957
8958 DEFVAR_LISP ("w32-pass-rwindow-to-system",
8959 &Vw32_pass_rwindow_to_system,
8960 doc: /* If non-nil, the right \"Windows\" key is passed on to Windows.
8961
8962When non-nil, the Start menu is opened by tapping the key.
8963If you set this to nil, the right \"Windows\" key is processed by Emacs
8964according to the value of `w32-rwindow-modifier', which see.
8965
8966Note that some combinations of the right \"Windows\" key with other keys are
8967caught by Windows at low level, and so binding them in Emacs will have no
8968effect. For example, <rwindow>-r always pops up the Windows Run dialog,
8969<rwindow>-<Pause> pops up the "System Properties" dialog, etc. However, see
8970the doc string of `w32-phantom-key-code'. */);
8971 Vw32_pass_rwindow_to_system = Qt;
8972
8973 DEFVAR_LISP ("w32-phantom-key-code",
8974 &Vw32_phantom_key_code,
8975 doc: /* Virtual key code used to generate \"phantom\" key presses.
8976Value is a number between 0 and 255.
8977
8978Phantom key presses are generated in order to stop the system from
8979acting on \"Windows\" key events when `w32-pass-lwindow-to-system' or
8980`w32-pass-rwindow-to-system' is nil. */);
8981 /* Although 255 is technically not a valid key code, it works and
8982 means that this hack won't interfere with any real key code. */
8983 XSETINT (Vw32_phantom_key_code, 255);
8984
8985 DEFVAR_LISP ("w32-enable-num-lock",
8986 &Vw32_enable_num_lock,
8987 doc: /* If non-nil, the Num Lock key acts normally.
8988Set to nil to handle Num Lock as the `kp-numlock' key. */);
8989 Vw32_enable_num_lock = Qt;
8990
8991 DEFVAR_LISP ("w32-enable-caps-lock",
8992 &Vw32_enable_caps_lock,
8993 doc: /* If non-nil, the Caps Lock key acts normally.
8994Set to nil to handle Caps Lock as the `capslock' key. */);
8995 Vw32_enable_caps_lock = Qt;
8996
8997 DEFVAR_LISP ("w32-scroll-lock-modifier",
8998 &Vw32_scroll_lock_modifier,
8999 doc: /* Modifier to use for the Scroll Lock ON state.
9000The value can be hyper, super, meta, alt, control or shift for the
9001respective modifier, or nil to handle Scroll Lock as the `scroll' key.
9002Any other value will cause the Scroll Lock key to be ignored. */);
9003 Vw32_scroll_lock_modifier = Qt;
9004
9005 DEFVAR_LISP ("w32-lwindow-modifier",
9006 &Vw32_lwindow_modifier,
9007 doc: /* Modifier to use for the left \"Windows\" key.
9008The value can be hyper, super, meta, alt, control or shift for the
9009respective modifier, or nil to appear as the `lwindow' key.
9010Any other value will cause the key to be ignored. */);
9011 Vw32_lwindow_modifier = Qnil;
9012
9013 DEFVAR_LISP ("w32-rwindow-modifier",
9014 &Vw32_rwindow_modifier,
9015 doc: /* Modifier to use for the right \"Windows\" key.
9016The value can be hyper, super, meta, alt, control or shift for the
9017respective modifier, or nil to appear as the `rwindow' key.
9018Any other value will cause the key to be ignored. */);
9019 Vw32_rwindow_modifier = Qnil;
9020
9021 DEFVAR_LISP ("w32-apps-modifier",
9022 &Vw32_apps_modifier,
9023 doc: /* Modifier to use for the \"Apps\" key.
9024The value can be hyper, super, meta, alt, control or shift for the
9025respective modifier, or nil to appear as the `apps' key.
9026Any other value will cause the key to be ignored. */);
9027 Vw32_apps_modifier = Qnil;
9028
9029 DEFVAR_BOOL ("w32-enable-synthesized-fonts", &w32_enable_synthesized_fonts,
9030 doc: /* Non-nil enables selection of artificially italicized and bold fonts. */);
9031 w32_enable_synthesized_fonts = 0;
9032
9033 DEFVAR_LISP ("w32-enable-palette", &Vw32_enable_palette,
9034 doc: /* Non-nil enables Windows palette management to map colors exactly. */);
9035 Vw32_enable_palette = Qt;
9036
9037 DEFVAR_INT ("w32-mouse-button-tolerance",
9038 &w32_mouse_button_tolerance,
9039 doc: /* Analogue of double click interval for faking middle mouse events.
9040The value is the minimum time in milliseconds that must elapse between
9041left and right button down events before they are considered distinct events.
9042If both mouse buttons are depressed within this interval, a middle mouse
9043button down event is generated instead. */);
9044 w32_mouse_button_tolerance = GetDoubleClickTime () / 2;
9045
9046 DEFVAR_INT ("w32-mouse-move-interval",
9047 &w32_mouse_move_interval,
9048 doc: /* Minimum interval between mouse move events.
9049The value is the minimum time in milliseconds that must elapse between
9050successive mouse move (or scroll bar drag) events before they are
9051reported as lisp events. */);
9052 w32_mouse_move_interval = 0;
9053
9054 DEFVAR_BOOL ("w32-pass-extra-mouse-buttons-to-system",
9055 &w32_pass_extra_mouse_buttons_to_system,
9056 doc: /* If non-nil, the fourth and fifth mouse buttons are passed to Windows.
9057Recent versions of Windows support mice with up to five buttons.
9058Since most applications don't support these extra buttons, most mouse
9059drivers will allow you to map them to functions at the system level.
9060If this variable is non-nil, Emacs will pass them on, allowing the
9061system to handle them. */);
9062 w32_pass_extra_mouse_buttons_to_system = 0;
9063
9064 DEFVAR_BOOL ("w32-pass-multimedia-buttons-to-system",
9065 &w32_pass_multimedia_buttons_to_system,
9066 doc: /* If non-nil, media buttons are passed to Windows.
9067Some modern keyboards contain buttons for controlling media players, web
9068browsers and other applications. Generally these buttons are handled on a
9069system wide basis, but by setting this to nil they are made available
9070to Emacs for binding. Depending on your keyboard, additional keys that
9071may be available are:
9072
9073browser-back, browser-forward, browser-refresh, browser-stop,
9074browser-search, browser-favorites, browser-home,
9075mail, mail-reply, mail-forward, mail-send,
9076app-1, app-2,
9077help, find, new, open, close, save, print, undo, redo, copy, cut, paste,
9078spell-check, correction-list, toggle-dictate-command,
9079media-next, media-previous, media-stop, media-play-pause, media-select,
9080media-play, media-pause, media-record, media-fast-forward, media-rewind,
9081media-channel-up, media-channel-down,
9082volume-mute, volume-up, volume-down,
9083mic-volume-mute, mic-volume-down, mic-volume-up, mic-toggle,
9084bass-down, bass-boost, bass-up, treble-down, treble-up */);
9085 w32_pass_multimedia_buttons_to_system = 1;
9086
9087 DEFVAR_LISP ("x-pointer-shape", &Vx_pointer_shape,
9088 doc: /* The shape of the pointer when over text.
9089Changing the value does not affect existing frames
9090unless you set the mouse color. */);
9091 Vx_pointer_shape = Qnil;
9092
9093 Vx_nontext_pointer_shape = Qnil;
9094
9095 Vx_mode_pointer_shape = Qnil;
9096
9097 DEFVAR_LISP ("x-hourglass-pointer-shape", &Vx_hourglass_pointer_shape,
9098 doc: /* The shape of the pointer when Emacs is busy.
9099This variable takes effect when you create a new frame
9100or when you set the mouse color. */);
9101 Vx_hourglass_pointer_shape = Qnil;
9102
9103 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
9104 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
9105 display_hourglass_p = 1;
9106
9107 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
9108 doc: /* *Seconds to wait before displaying an hourglass pointer.
9109Value must be an integer or float. */);
9110 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
9111
9112 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
9113 &Vx_sensitive_text_pointer_shape,
9114 doc: /* The shape of the pointer when over mouse-sensitive text.
9115This variable takes effect when you create a new frame
9116or when you set the mouse color. */);
9117 Vx_sensitive_text_pointer_shape = Qnil;
9118
9119 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
9120 &Vx_window_horizontal_drag_shape,
9121 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
9122This variable takes effect when you create a new frame
9123or when you set the mouse color. */);
9124 Vx_window_horizontal_drag_shape = Qnil;
9125
9126 DEFVAR_LISP ("x-cursor-fore-pixel", &Vx_cursor_fore_pixel,
9127 doc: /* A string indicating the foreground color of the cursor box. */);
9128 Vx_cursor_fore_pixel = Qnil;
9129
9130 DEFVAR_LISP ("x-max-tooltip-size", &Vx_max_tooltip_size,
9131 doc: /* Maximum size for tooltips.
9132Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
9133 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
9134
9135 DEFVAR_LISP ("x-no-window-manager", &Vx_no_window_manager,
9136 doc: /* Non-nil if no window manager is in use.
9137Emacs doesn't try to figure this out; this is always nil
9138unless you set it to something else. */);
9139 /* We don't have any way to find this out, so set it to nil
9140 and maybe the user would like to set it to t. */
9141 Vx_no_window_manager = Qnil;
9142
9143 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
9144 &Vx_pixel_size_width_font_regexp,
9145 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
9146
9147Since Emacs gets width of a font matching with this regexp from
9148PIXEL_SIZE field of the name, font finding mechanism gets faster for
9149such a font. This is especially effective for such large fonts as
9150Chinese, Japanese, and Korean. */);
9151 Vx_pixel_size_width_font_regexp = Qnil;
9152
9153 DEFVAR_LISP ("w32-bdf-filename-alist",
9154 &Vw32_bdf_filename_alist,
9155 doc: /* List of bdf fonts and their corresponding filenames. */);
9156 Vw32_bdf_filename_alist = Qnil;
9157
9158 DEFVAR_BOOL ("w32-strict-fontnames",
9159 &w32_strict_fontnames,
9160 doc: /* Non-nil means only use fonts that are exact matches for those requested.
9161Default is nil, which allows old fontnames that are not XLFD compliant,
9162and allows third-party CJK display to work by specifying false charset
9163fields to trick Emacs into translating to Big5, SJIS etc.
9164Setting this to t will prevent wrong fonts being selected when
9165fontsets are automatically created. */);
9166 w32_strict_fontnames = 0;
9167
9168 DEFVAR_BOOL ("w32-strict-painting",
9169 &w32_strict_painting,
9170 doc: /* Non-nil means use strict rules for repainting frames.
9171Set this to nil to get the old behavior for repainting; this should
9172only be necessary if the default setting causes problems. */);
9173 w32_strict_painting = 1;
9174
9175 DEFVAR_LISP ("w32-charset-info-alist",
9176 &Vw32_charset_info_alist,
9177 doc: /* Alist linking Emacs character sets to Windows fonts and codepages.
9178Each entry should be of the form:
9179
9180 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE))
9181
9182where CHARSET_NAME is a string used in font names to identify the charset,
9183WINDOWS_CHARSET is a symbol that can be one of:
9184w32-charset-ansi, w32-charset-default, w32-charset-symbol,
9185w32-charset-shiftjis, w32-charset-hangeul, w32-charset-gb2312,
9186w32-charset-chinesebig5,
9187w32-charset-johab, w32-charset-hebrew,
9188w32-charset-arabic, w32-charset-greek, w32-charset-turkish,
9189w32-charset-vietnamese, w32-charset-thai, w32-charset-easteurope,
9190w32-charset-russian, w32-charset-mac, w32-charset-baltic,
9191w32-charset-unicode,
9192or w32-charset-oem.
9193CODEPAGE should be an integer specifying the codepage that should be used
9194to display the character set, t to do no translation and output as Unicode,
9195or nil to do no translation and output as 8 bit (or multibyte on far-east
9196versions of Windows) characters. */);
9197 Vw32_charset_info_alist = Qnil;
9198
9199 DEFSYM (Qw32_charset_ansi, "w32-charset-ansi");
9200 DEFSYM (Qw32_charset_symbol, "w32-charset-symbol");
9201 DEFSYM (Qw32_charset_default, "w32-charset-default");
9202 DEFSYM (Qw32_charset_shiftjis, "w32-charset-shiftjis");
9203 DEFSYM (Qw32_charset_hangeul, "w32-charset-hangeul");
9204 DEFSYM (Qw32_charset_chinesebig5, "w32-charset-chinesebig5");
9205 DEFSYM (Qw32_charset_gb2312, "w32-charset-gb2312");
9206 DEFSYM (Qw32_charset_oem, "w32-charset-oem");
9207
9208#ifdef JOHAB_CHARSET
9209 {
9210 static int w32_extra_charsets_defined = 1;
9211 DEFVAR_BOOL ("w32-extra-charsets-defined", &w32_extra_charsets_defined,
9212 doc: /* Internal variable. */);
9213
9214 DEFSYM (Qw32_charset_johab, "w32-charset-johab");
9215 DEFSYM (Qw32_charset_easteurope, "w32-charset-easteurope");
9216 DEFSYM (Qw32_charset_turkish, "w32-charset-turkish");
9217 DEFSYM (Qw32_charset_baltic, "w32-charset-baltic");
9218 DEFSYM (Qw32_charset_russian, "w32-charset-russian");
9219 DEFSYM (Qw32_charset_arabic, "w32-charset-arabic");
9220 DEFSYM (Qw32_charset_greek, "w32-charset-greek");
9221 DEFSYM (Qw32_charset_hebrew, "w32-charset-hebrew");
9222 DEFSYM (Qw32_charset_vietnamese, "w32-charset-vietnamese");
9223 DEFSYM (Qw32_charset_thai, "w32-charset-thai");
9224 DEFSYM (Qw32_charset_mac, "w32-charset-mac");
9225 }
9226#endif
9227
9228#ifdef UNICODE_CHARSET
9229 {
9230 static int w32_unicode_charset_defined = 1;
9231 DEFVAR_BOOL ("w32-unicode-charset-defined",
9232 &w32_unicode_charset_defined,
9233 doc: /* Internal variable. */);
9234 DEFSYM (Qw32_charset_unicode, "w32-charset-unicode");
9235 }
9236#endif
9237
9238#if 0 /* TODO: Port to W32 */
9239 defsubr (&Sx_change_window_property);
9240 defsubr (&Sx_delete_window_property);
9241 defsubr (&Sx_window_property);
9242#endif
9243 defsubr (&Sxw_display_color_p);
9244 defsubr (&Sx_display_grayscale_p);
9245 defsubr (&Sxw_color_defined_p);
9246 defsubr (&Sxw_color_values);
9247 defsubr (&Sx_server_max_request_size);
9248 defsubr (&Sx_server_vendor);
9249 defsubr (&Sx_server_version);
9250 defsubr (&Sx_display_pixel_width);
9251 defsubr (&Sx_display_pixel_height);
9252 defsubr (&Sx_display_mm_width);
9253 defsubr (&Sx_display_mm_height);
9254 defsubr (&Sx_display_screens);
9255 defsubr (&Sx_display_planes);
9256 defsubr (&Sx_display_color_cells);
9257 defsubr (&Sx_display_visual_class);
9258 defsubr (&Sx_display_backing_store);
9259 defsubr (&Sx_display_save_under);
9260 defsubr (&Sx_create_frame);
9261 defsubr (&Sx_open_connection);
9262 defsubr (&Sx_close_connection);
9263 defsubr (&Sx_display_list);
9264 defsubr (&Sx_synchronize);
9265 defsubr (&Sx_focus_frame);
9266
9267 /* W32 specific functions */
9268
9269 defsubr (&Sw32_select_font);
9270 defsubr (&Sw32_define_rgb_color);
9271 defsubr (&Sw32_default_color_map);
9272 defsubr (&Sw32_load_color_file);
9273 defsubr (&Sw32_send_sys_command);
9274 defsubr (&Sw32_shell_execute);
9275 defsubr (&Sw32_register_hot_key);
9276 defsubr (&Sw32_unregister_hot_key);
9277 defsubr (&Sw32_registered_hot_keys);
9278 defsubr (&Sw32_reconstruct_hot_key);
9279 defsubr (&Sw32_toggle_lock_key);
9280 defsubr (&Sw32_window_exists_p);
9281 defsubr (&Sw32_find_bdf_fonts);
9282
9283 defsubr (&Sfile_system_info);
9284 defsubr (&Sdefault_printer_name);
9285
9286 /* Setting callback functions for fontset handler. */
9287 get_font_info_func = w32_get_font_info;
9288
9289#if 0 /* This function pointer doesn't seem to be used anywhere.
9290 And the pointer assigned has the wrong type, anyway. */
9291 list_fonts_func = w32_list_fonts;
9292#endif
9293
9294 load_font_func = w32_load_font;
9295 find_ccl_program_func = w32_find_ccl_program;
9296 query_font_func = w32_query_font;
9297 set_frame_fontset_func = x_set_font;
9298 get_font_repertory_func = x_get_font_repertory;
9299 check_window_system_func = check_w32;
9300
9301
9302 hourglass_timer = 0;
9303 hourglass_hwnd = NULL;
9304 hourglass_shown_p = 0;
9305 defsubr (&Sx_show_tip);
9306 defsubr (&Sx_hide_tip);
9307 tip_timer = Qnil;
9308 staticpro (&tip_timer);
9309 tip_frame = Qnil;
9310 staticpro (&tip_frame);
9311
9312 last_show_tip_args = Qnil;
9313 staticpro (&last_show_tip_args);
9314
9315 defsubr (&Sx_file_dialog);
9316}
9317
9318
9319/*
9320 globals_of_w32fns is used to initialize those global variables that
9321 must always be initialized on startup even when the global variable
9322 initialized is non zero (see the function main in emacs.c).
9323 globals_of_w32fns is called from syms_of_w32fns when the global
9324 variable initialized is 0 and directly from main when initialized
9325 is non zero.
9326 */
9327void
9328globals_of_w32fns ()
9329{
9330 HMODULE user32_lib = GetModuleHandle ("user32.dll");
9331 /*
9332 TrackMouseEvent not available in all versions of Windows, so must load
9333 it dynamically. Do it once, here, instead of every time it is used.
9334 */
9335 track_mouse_event_fn = (TrackMouseEvent_Proc)
9336 GetProcAddress (user32_lib, "TrackMouseEvent");
9337 /* ditto for GetClipboardSequenceNumber. */
9338 clipboard_sequence_fn = (ClipboardSequence_Proc)
9339 GetProcAddress (user32_lib, "GetClipboardSequenceNumber");
9340
9341 monitor_from_point_fn = (MonitorFromPoint_Proc)
9342 GetProcAddress (user32_lib, "MonitorFromPoint");
9343 get_monitor_info_fn = (GetMonitorInfo_Proc)
9344 GetProcAddress (user32_lib, "GetMonitorInfoA");
9345
9346 {
9347 HMODULE imm32_lib = GetModuleHandle ("imm32.dll");
9348 get_composition_string_fn = (ImmGetCompositionString_Proc)
9349 GetProcAddress (imm32_lib, "ImmGetCompositionStringW");
9350 get_ime_context_fn = (ImmGetContext_Proc)
9351 GetProcAddress (imm32_lib, "ImmGetContext");
9352 }
9353 DEFVAR_INT ("w32-ansi-code-page",
9354 &w32_ansi_code_page,
9355 doc: /* The ANSI code page used by the system. */);
9356 w32_ansi_code_page = GetACP ();
9357
9358 /* MessageBox does not work without this when linked to comctl32.dll 6.0. */
9359 InitCommonControls ();
9360}
9361
9362#undef abort
9363
9364void
9365w32_abort ()
9366{
9367 int button;
9368 button = MessageBox (NULL,
9369 "A fatal error has occurred!\n\n"
9370 "Would you like to attach a debugger?\n\n"
9371 "Select YES to debug, NO to abort Emacs"
9372#if __GNUC__
9373 "\n\n(type \"gdb -p <emacs-PID>\" and\n"
9374 "\"continue\" inside GDB before clicking YES.)"
9375#endif
9376 , "Emacs Abort Dialog",
9377 MB_ICONEXCLAMATION | MB_TASKMODAL
9378 | MB_SETFOREGROUND | MB_YESNO);
9379 switch (button)
9380 {
9381 case IDYES:
9382 DebugBreak ();
9383 exit (2); /* tell the compiler we will never return */
9384 case IDNO:
9385 default:
9386 abort ();
9387 break;
9388 }
9389}
9390
9391/* For convenience when debugging. */
9392int
9393w32_last_error ()
9394{
9395 return GetLastError ();
9396}
9397
9398/* arch-tag: 707589ab-b9be-4638-8cdd-74629cc9b446
9399 (do not change this comment) */