(comment-search-forward, comment-search-backward): Fix typos.
[emacs.git] / src / w32fns.c
blobe7ead136cedf327d81c6f30b9e5c4dcd6482770d
1 /* Graphical user interface functions for the Microsoft W32 API.
2 Copyright (C) 1989, 1992, 93, 94, 95, 96, 97, 98, 99, 2000, 01, 04
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* Added by Kevin Gallo */
24 #include <config.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <limits.h>
29 #include <errno.h>
31 #include "lisp.h"
32 #include "charset.h"
33 #include "dispextern.h"
34 #include "w32term.h"
35 #include "keyboard.h"
36 #include "frame.h"
37 #include "window.h"
38 #include "buffer.h"
39 #include "fontset.h"
40 #include "intervals.h"
41 #include "blockinput.h"
42 #include "epaths.h"
43 #include "w32heap.h"
44 #include "termhooks.h"
45 #include "coding.h"
46 #include "ccl.h"
47 #include "systime.h"
49 #include "bitmaps/gray.xbm"
51 #include <commdlg.h>
52 #include <shellapi.h>
53 #include <ctype.h>
54 #include <winspool.h>
56 #include <dlgs.h>
57 #define FILE_NAME_TEXT_FIELD edt1
59 void syms_of_w32fns ();
60 void globals_of_w32fns ();
62 extern void free_frame_menubar ();
63 extern double atof ();
64 extern int w32_console_toggle_lock_key P_ ((int, Lisp_Object));
65 extern void w32_menu_display_help P_ ((HWND, HMENU, UINT, UINT));
66 extern void w32_free_menu_strings P_ ((HWND));
68 extern int quit_char;
70 extern char *lispy_function_keys[];
72 /* The gray bitmap `bitmaps/gray'. This is done because w32term.c uses
73 it, and including `bitmaps/gray' more than once is a problem when
74 config.h defines `static' as an empty replacement string. */
76 int gray_bitmap_width = gray_width;
77 int gray_bitmap_height = gray_height;
78 unsigned char *gray_bitmap_bits = gray_bits;
80 /* The colormap for converting color names to RGB values */
81 Lisp_Object Vw32_color_map;
83 /* Non nil if alt key presses are passed on to Windows. */
84 Lisp_Object Vw32_pass_alt_to_system;
86 /* Non nil if alt key is translated to meta_modifier, nil if it is translated
87 to alt_modifier. */
88 Lisp_Object Vw32_alt_is_meta;
90 /* If non-zero, the windows virtual key code for an alternative quit key. */
91 int w32_quit_key;
93 /* Non nil if left window key events are passed on to Windows (this only
94 affects whether "tapping" the key opens the Start menu). */
95 Lisp_Object Vw32_pass_lwindow_to_system;
97 /* Non nil if right window key events are passed on to Windows (this
98 only affects whether "tapping" the key opens the Start menu). */
99 Lisp_Object Vw32_pass_rwindow_to_system;
101 /* Virtual key code used to generate "phantom" key presses in order
102 to stop system from acting on Windows key events. */
103 Lisp_Object Vw32_phantom_key_code;
105 /* Modifier associated with the left "Windows" key, or nil to act as a
106 normal key. */
107 Lisp_Object Vw32_lwindow_modifier;
109 /* Modifier associated with the right "Windows" key, or nil to act as a
110 normal key. */
111 Lisp_Object Vw32_rwindow_modifier;
113 /* Modifier associated with the "Apps" key, or nil to act as a normal
114 key. */
115 Lisp_Object Vw32_apps_modifier;
117 /* Value is nil if Num Lock acts as a function key. */
118 Lisp_Object Vw32_enable_num_lock;
120 /* Value is nil if Caps Lock acts as a function key. */
121 Lisp_Object Vw32_enable_caps_lock;
123 /* Modifier associated with Scroll Lock, or nil to act as a normal key. */
124 Lisp_Object Vw32_scroll_lock_modifier;
126 /* Switch to control whether we inhibit requests for synthesized bold
127 and italic versions of fonts. */
128 int w32_enable_synthesized_fonts;
130 /* Enable palette management. */
131 Lisp_Object Vw32_enable_palette;
133 /* Control how close left/right button down events must be to
134 be converted to a middle button down event. */
135 int w32_mouse_button_tolerance;
137 /* Minimum interval between mouse movement (and scroll bar drag)
138 events that are passed on to the event loop. */
139 int w32_mouse_move_interval;
141 /* Flag to indicate if XBUTTON events should be passed on to Windows. */
142 int w32_pass_extra_mouse_buttons_to_system;
144 /* Non nil if no window manager is in use. */
145 Lisp_Object Vx_no_window_manager;
147 /* Non-zero means we're allowed to display a hourglass pointer. */
149 int display_hourglass_p;
151 /* The background and shape of the mouse pointer, and shape when not
152 over text or in the modeline. */
154 Lisp_Object Vx_pointer_shape, Vx_nontext_pointer_shape, Vx_mode_pointer_shape;
155 Lisp_Object Vx_hourglass_pointer_shape, Vx_window_horizontal_drag_shape, Vx_hand_shape;
157 /* The shape when over mouse-sensitive text. */
159 Lisp_Object Vx_sensitive_text_pointer_shape;
161 #ifndef IDC_HAND
162 #define IDC_HAND MAKEINTRESOURCE(32649)
163 #endif
165 /* Color of chars displayed in cursor box. */
167 Lisp_Object Vx_cursor_fore_pixel;
169 /* Nonzero if using Windows. */
171 static int w32_in_use;
173 /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'. */
175 Lisp_Object Vx_pixel_size_width_font_regexp;
177 /* Alist of bdf fonts and the files that define them. */
178 Lisp_Object Vw32_bdf_filename_alist;
180 /* A flag to control whether fonts are matched strictly or not. */
181 int w32_strict_fontnames;
183 /* A flag to control whether we should only repaint if GetUpdateRect
184 indicates there is an update region. */
185 int w32_strict_painting;
187 /* Associative list linking character set strings to Windows codepages. */
188 Lisp_Object Vw32_charset_info_alist;
190 /* VIETNAMESE_CHARSET is not defined in some versions of MSVC. */
191 #ifndef VIETNAMESE_CHARSET
192 #define VIETNAMESE_CHARSET 163
193 #endif
195 Lisp_Object Qnone;
196 Lisp_Object Qsuppress_icon;
197 Lisp_Object Qundefined_color;
198 Lisp_Object Qcancel_timer;
199 Lisp_Object Qhyper;
200 Lisp_Object Qsuper;
201 Lisp_Object Qmeta;
202 Lisp_Object Qalt;
203 Lisp_Object Qctrl;
204 Lisp_Object Qcontrol;
205 Lisp_Object Qshift;
207 Lisp_Object Qw32_charset_ansi;
208 Lisp_Object Qw32_charset_default;
209 Lisp_Object Qw32_charset_symbol;
210 Lisp_Object Qw32_charset_shiftjis;
211 Lisp_Object Qw32_charset_hangeul;
212 Lisp_Object Qw32_charset_gb2312;
213 Lisp_Object Qw32_charset_chinesebig5;
214 Lisp_Object Qw32_charset_oem;
216 #ifndef JOHAB_CHARSET
217 #define JOHAB_CHARSET 130
218 #endif
219 #ifdef JOHAB_CHARSET
220 Lisp_Object Qw32_charset_easteurope;
221 Lisp_Object Qw32_charset_turkish;
222 Lisp_Object Qw32_charset_baltic;
223 Lisp_Object Qw32_charset_russian;
224 Lisp_Object Qw32_charset_arabic;
225 Lisp_Object Qw32_charset_greek;
226 Lisp_Object Qw32_charset_hebrew;
227 Lisp_Object Qw32_charset_vietnamese;
228 Lisp_Object Qw32_charset_thai;
229 Lisp_Object Qw32_charset_johab;
230 Lisp_Object Qw32_charset_mac;
231 #endif
233 #ifdef UNICODE_CHARSET
234 Lisp_Object Qw32_charset_unicode;
235 #endif
237 /* The ANSI codepage. */
238 int w32_ansi_code_page;
240 /* Prefix for system colors. */
241 #define SYSTEM_COLOR_PREFIX "System"
242 #define SYSTEM_COLOR_PREFIX_LEN (sizeof (SYSTEM_COLOR_PREFIX) - 1)
244 /* State variables for emulating a three button mouse. */
245 #define LMOUSE 1
246 #define MMOUSE 2
247 #define RMOUSE 4
249 static int button_state = 0;
250 static W32Msg saved_mouse_button_msg;
251 static unsigned mouse_button_timer = 0; /* non-zero when timer is active */
252 static W32Msg saved_mouse_move_msg;
253 static unsigned mouse_move_timer = 0;
255 /* Window that is tracking the mouse. */
256 static HWND track_mouse_window;
258 typedef BOOL (WINAPI * TrackMouseEvent_Proc)
259 (IN OUT LPTRACKMOUSEEVENT lpEventTrack);
261 TrackMouseEvent_Proc track_mouse_event_fn = NULL;
262 ClipboardSequence_Proc clipboard_sequence_fn = NULL;
264 /* W95 mousewheel handler */
265 unsigned int msh_mousewheel = 0;
267 /* Timers */
268 #define MOUSE_BUTTON_ID 1
269 #define MOUSE_MOVE_ID 2
270 #define MENU_FREE_ID 3
271 /* The delay (milliseconds) before a menu is freed after WM_EXITMENULOOP
272 is received. */
273 #define MENU_FREE_DELAY 1000
274 static unsigned menu_free_timer = 0;
276 /* The below are defined in frame.c. */
278 extern Lisp_Object Vwindow_system_version;
280 #ifdef GLYPH_DEBUG
281 int image_cache_refcount, dpyinfo_refcount;
282 #endif
285 /* From w32term.c. */
286 extern int w32_num_mouse_buttons;
287 extern Lisp_Object Vw32_recognize_altgr;
289 extern HWND w32_system_caret_hwnd;
291 extern int w32_system_caret_height;
292 extern int w32_system_caret_x;
293 extern int w32_system_caret_y;
294 extern int w32_use_visible_system_caret;
296 static HWND w32_visible_system_caret_hwnd;
299 /* Error if we are not connected to MS-Windows. */
300 void
301 check_w32 ()
303 if (! w32_in_use)
304 error ("MS-Windows not in use or not initialized");
307 /* Nonzero if we can use mouse menus.
308 You should not call this unless HAVE_MENUS is defined. */
311 have_menus_p ()
313 return w32_in_use;
316 /* Extract a frame as a FRAME_PTR, defaulting to the selected frame
317 and checking validity for W32. */
319 FRAME_PTR
320 check_x_frame (frame)
321 Lisp_Object frame;
323 FRAME_PTR f;
325 if (NILP (frame))
326 frame = selected_frame;
327 CHECK_LIVE_FRAME (frame);
328 f = XFRAME (frame);
329 if (! FRAME_W32_P (f))
330 error ("non-w32 frame used");
331 return f;
334 /* Let the user specify a display with a frame.
335 nil stands for the selected frame--or, if that is not a w32 frame,
336 the first display on the list. */
338 struct w32_display_info *
339 check_x_display_info (frame)
340 Lisp_Object frame;
342 if (NILP (frame))
344 struct frame *sf = XFRAME (selected_frame);
346 if (FRAME_W32_P (sf) && FRAME_LIVE_P (sf))
347 return FRAME_W32_DISPLAY_INFO (sf);
348 else
349 return &one_w32_display_info;
351 else if (STRINGP (frame))
352 return x_display_info_for_name (frame);
353 else
355 FRAME_PTR f;
357 CHECK_LIVE_FRAME (frame);
358 f = XFRAME (frame);
359 if (! FRAME_W32_P (f))
360 error ("non-w32 frame used");
361 return FRAME_W32_DISPLAY_INFO (f);
365 /* Return the Emacs frame-object corresponding to an w32 window.
366 It could be the frame's main window or an icon window. */
368 /* This function can be called during GC, so use GC_xxx type test macros. */
370 struct frame *
371 x_window_to_frame (dpyinfo, wdesc)
372 struct w32_display_info *dpyinfo;
373 HWND wdesc;
375 Lisp_Object tail, frame;
376 struct frame *f;
378 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
380 frame = XCAR (tail);
381 if (!GC_FRAMEP (frame))
382 continue;
383 f = XFRAME (frame);
384 if (!FRAME_W32_P (f) || FRAME_W32_DISPLAY_INFO (f) != dpyinfo)
385 continue;
386 if (f->output_data.w32->hourglass_window == wdesc)
387 return f;
389 if (FRAME_W32_WINDOW (f) == wdesc)
390 return f;
392 return 0;
396 BOOL my_show_window P_ ((struct frame *, HWND, int));
397 void my_set_window_pos P_ ((HWND, HWND, int, int, int, int, UINT));
398 static Lisp_Object unwind_create_frame P_ ((Lisp_Object));
399 static Lisp_Object unwind_create_tip_frame P_ ((Lisp_Object));
401 /* TODO: Native Input Method support; see x_create_im. */
402 void x_set_foreground_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
403 void x_set_background_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
404 void x_set_mouse_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
405 void x_set_cursor_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
406 void x_set_border_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
407 void x_set_cursor_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
408 void x_set_icon_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
409 void x_set_icon_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
410 void x_explicitly_set_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
411 void x_set_menu_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
412 void x_set_title P_ ((struct frame *, Lisp_Object, Lisp_Object));
413 void x_set_tool_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
414 static void x_edge_detection P_ ((struct frame *, struct image *, Lisp_Object,
415 Lisp_Object));
420 /* Store the screen positions of frame F into XPTR and YPTR.
421 These are the positions of the containing window manager window,
422 not Emacs's own window. */
424 void
425 x_real_positions (f, xptr, yptr)
426 FRAME_PTR f;
427 int *xptr, *yptr;
429 POINT pt;
430 RECT rect;
432 GetClientRect(FRAME_W32_WINDOW(f), &rect);
433 AdjustWindowRect(&rect, f->output_data.w32->dwStyle, FRAME_EXTERNAL_MENU_BAR(f));
435 pt.x = rect.left;
436 pt.y = rect.top;
438 ClientToScreen (FRAME_W32_WINDOW(f), &pt);
440 /* Remember x_pixels_diff and y_pixels_diff. */
441 f->x_pixels_diff = pt.x - rect.left;
442 f->y_pixels_diff = pt.y - rect.top;
444 *xptr = pt.x;
445 *yptr = pt.y;
450 DEFUN ("w32-define-rgb-color", Fw32_define_rgb_color,
451 Sw32_define_rgb_color, 4, 4, 0,
452 doc: /* Convert RGB numbers to a windows color reference and associate with NAME.
453 This adds or updates a named color to w32-color-map, making it
454 available for use. The original entry's RGB ref is returned, or nil
455 if the entry is new. */)
456 (red, green, blue, name)
457 Lisp_Object red, green, blue, name;
459 Lisp_Object rgb;
460 Lisp_Object oldrgb = Qnil;
461 Lisp_Object entry;
463 CHECK_NUMBER (red);
464 CHECK_NUMBER (green);
465 CHECK_NUMBER (blue);
466 CHECK_STRING (name);
468 XSETINT (rgb, RGB(XUINT (red), XUINT (green), XUINT (blue)));
470 BLOCK_INPUT;
472 /* replace existing entry in w32-color-map or add new entry. */
473 entry = Fassoc (name, Vw32_color_map);
474 if (NILP (entry))
476 entry = Fcons (name, rgb);
477 Vw32_color_map = Fcons (entry, Vw32_color_map);
479 else
481 oldrgb = Fcdr (entry);
482 Fsetcdr (entry, rgb);
485 UNBLOCK_INPUT;
487 return (oldrgb);
490 DEFUN ("w32-load-color-file", Fw32_load_color_file,
491 Sw32_load_color_file, 1, 1, 0,
492 doc: /* Create an alist of color entries from an external file.
493 Assign this value to w32-color-map to replace the existing color map.
495 The file should define one named RGB color per line like so:
496 R G B name
497 where R,G,B are numbers between 0 and 255 and name is an arbitrary string. */)
498 (filename)
499 Lisp_Object filename;
501 FILE *fp;
502 Lisp_Object cmap = Qnil;
503 Lisp_Object abspath;
505 CHECK_STRING (filename);
506 abspath = Fexpand_file_name (filename, Qnil);
508 fp = fopen (SDATA (filename), "rt");
509 if (fp)
511 char buf[512];
512 int red, green, blue;
513 int num;
515 BLOCK_INPUT;
517 while (fgets (buf, sizeof (buf), fp) != NULL) {
518 if (sscanf (buf, "%u %u %u %n", &red, &green, &blue, &num) == 3)
520 char *name = buf + num;
521 num = strlen (name) - 1;
522 if (name[num] == '\n')
523 name[num] = 0;
524 cmap = Fcons (Fcons (build_string (name),
525 make_number (RGB (red, green, blue))),
526 cmap);
529 fclose (fp);
531 UNBLOCK_INPUT;
534 return cmap;
537 /* The default colors for the w32 color map */
538 typedef struct colormap_t
540 char *name;
541 COLORREF colorref;
542 } colormap_t;
544 colormap_t w32_color_map[] =
546 {"snow" , PALETTERGB (255,250,250)},
547 {"ghost white" , PALETTERGB (248,248,255)},
548 {"GhostWhite" , PALETTERGB (248,248,255)},
549 {"white smoke" , PALETTERGB (245,245,245)},
550 {"WhiteSmoke" , PALETTERGB (245,245,245)},
551 {"gainsboro" , PALETTERGB (220,220,220)},
552 {"floral white" , PALETTERGB (255,250,240)},
553 {"FloralWhite" , PALETTERGB (255,250,240)},
554 {"old lace" , PALETTERGB (253,245,230)},
555 {"OldLace" , PALETTERGB (253,245,230)},
556 {"linen" , PALETTERGB (250,240,230)},
557 {"antique white" , PALETTERGB (250,235,215)},
558 {"AntiqueWhite" , PALETTERGB (250,235,215)},
559 {"papaya whip" , PALETTERGB (255,239,213)},
560 {"PapayaWhip" , PALETTERGB (255,239,213)},
561 {"blanched almond" , PALETTERGB (255,235,205)},
562 {"BlanchedAlmond" , PALETTERGB (255,235,205)},
563 {"bisque" , PALETTERGB (255,228,196)},
564 {"peach puff" , PALETTERGB (255,218,185)},
565 {"PeachPuff" , PALETTERGB (255,218,185)},
566 {"navajo white" , PALETTERGB (255,222,173)},
567 {"NavajoWhite" , PALETTERGB (255,222,173)},
568 {"moccasin" , PALETTERGB (255,228,181)},
569 {"cornsilk" , PALETTERGB (255,248,220)},
570 {"ivory" , PALETTERGB (255,255,240)},
571 {"lemon chiffon" , PALETTERGB (255,250,205)},
572 {"LemonChiffon" , PALETTERGB (255,250,205)},
573 {"seashell" , PALETTERGB (255,245,238)},
574 {"honeydew" , PALETTERGB (240,255,240)},
575 {"mint cream" , PALETTERGB (245,255,250)},
576 {"MintCream" , PALETTERGB (245,255,250)},
577 {"azure" , PALETTERGB (240,255,255)},
578 {"alice blue" , PALETTERGB (240,248,255)},
579 {"AliceBlue" , PALETTERGB (240,248,255)},
580 {"lavender" , PALETTERGB (230,230,250)},
581 {"lavender blush" , PALETTERGB (255,240,245)},
582 {"LavenderBlush" , PALETTERGB (255,240,245)},
583 {"misty rose" , PALETTERGB (255,228,225)},
584 {"MistyRose" , PALETTERGB (255,228,225)},
585 {"white" , PALETTERGB (255,255,255)},
586 {"black" , PALETTERGB ( 0, 0, 0)},
587 {"dark slate gray" , PALETTERGB ( 47, 79, 79)},
588 {"DarkSlateGray" , PALETTERGB ( 47, 79, 79)},
589 {"dark slate grey" , PALETTERGB ( 47, 79, 79)},
590 {"DarkSlateGrey" , PALETTERGB ( 47, 79, 79)},
591 {"dim gray" , PALETTERGB (105,105,105)},
592 {"DimGray" , PALETTERGB (105,105,105)},
593 {"dim grey" , PALETTERGB (105,105,105)},
594 {"DimGrey" , PALETTERGB (105,105,105)},
595 {"slate gray" , PALETTERGB (112,128,144)},
596 {"SlateGray" , PALETTERGB (112,128,144)},
597 {"slate grey" , PALETTERGB (112,128,144)},
598 {"SlateGrey" , PALETTERGB (112,128,144)},
599 {"light slate gray" , PALETTERGB (119,136,153)},
600 {"LightSlateGray" , PALETTERGB (119,136,153)},
601 {"light slate grey" , PALETTERGB (119,136,153)},
602 {"LightSlateGrey" , PALETTERGB (119,136,153)},
603 {"gray" , PALETTERGB (190,190,190)},
604 {"grey" , PALETTERGB (190,190,190)},
605 {"light grey" , PALETTERGB (211,211,211)},
606 {"LightGrey" , PALETTERGB (211,211,211)},
607 {"light gray" , PALETTERGB (211,211,211)},
608 {"LightGray" , PALETTERGB (211,211,211)},
609 {"midnight blue" , PALETTERGB ( 25, 25,112)},
610 {"MidnightBlue" , PALETTERGB ( 25, 25,112)},
611 {"navy" , PALETTERGB ( 0, 0,128)},
612 {"navy blue" , PALETTERGB ( 0, 0,128)},
613 {"NavyBlue" , PALETTERGB ( 0, 0,128)},
614 {"cornflower blue" , PALETTERGB (100,149,237)},
615 {"CornflowerBlue" , PALETTERGB (100,149,237)},
616 {"dark slate blue" , PALETTERGB ( 72, 61,139)},
617 {"DarkSlateBlue" , PALETTERGB ( 72, 61,139)},
618 {"slate blue" , PALETTERGB (106, 90,205)},
619 {"SlateBlue" , PALETTERGB (106, 90,205)},
620 {"medium slate blue" , PALETTERGB (123,104,238)},
621 {"MediumSlateBlue" , PALETTERGB (123,104,238)},
622 {"light slate blue" , PALETTERGB (132,112,255)},
623 {"LightSlateBlue" , PALETTERGB (132,112,255)},
624 {"medium blue" , PALETTERGB ( 0, 0,205)},
625 {"MediumBlue" , PALETTERGB ( 0, 0,205)},
626 {"royal blue" , PALETTERGB ( 65,105,225)},
627 {"RoyalBlue" , PALETTERGB ( 65,105,225)},
628 {"blue" , PALETTERGB ( 0, 0,255)},
629 {"dodger blue" , PALETTERGB ( 30,144,255)},
630 {"DodgerBlue" , PALETTERGB ( 30,144,255)},
631 {"deep sky blue" , PALETTERGB ( 0,191,255)},
632 {"DeepSkyBlue" , PALETTERGB ( 0,191,255)},
633 {"sky blue" , PALETTERGB (135,206,235)},
634 {"SkyBlue" , PALETTERGB (135,206,235)},
635 {"light sky blue" , PALETTERGB (135,206,250)},
636 {"LightSkyBlue" , PALETTERGB (135,206,250)},
637 {"steel blue" , PALETTERGB ( 70,130,180)},
638 {"SteelBlue" , PALETTERGB ( 70,130,180)},
639 {"light steel blue" , PALETTERGB (176,196,222)},
640 {"LightSteelBlue" , PALETTERGB (176,196,222)},
641 {"light blue" , PALETTERGB (173,216,230)},
642 {"LightBlue" , PALETTERGB (173,216,230)},
643 {"powder blue" , PALETTERGB (176,224,230)},
644 {"PowderBlue" , PALETTERGB (176,224,230)},
645 {"pale turquoise" , PALETTERGB (175,238,238)},
646 {"PaleTurquoise" , PALETTERGB (175,238,238)},
647 {"dark turquoise" , PALETTERGB ( 0,206,209)},
648 {"DarkTurquoise" , PALETTERGB ( 0,206,209)},
649 {"medium turquoise" , PALETTERGB ( 72,209,204)},
650 {"MediumTurquoise" , PALETTERGB ( 72,209,204)},
651 {"turquoise" , PALETTERGB ( 64,224,208)},
652 {"cyan" , PALETTERGB ( 0,255,255)},
653 {"light cyan" , PALETTERGB (224,255,255)},
654 {"LightCyan" , PALETTERGB (224,255,255)},
655 {"cadet blue" , PALETTERGB ( 95,158,160)},
656 {"CadetBlue" , PALETTERGB ( 95,158,160)},
657 {"medium aquamarine" , PALETTERGB (102,205,170)},
658 {"MediumAquamarine" , PALETTERGB (102,205,170)},
659 {"aquamarine" , PALETTERGB (127,255,212)},
660 {"dark green" , PALETTERGB ( 0,100, 0)},
661 {"DarkGreen" , PALETTERGB ( 0,100, 0)},
662 {"dark olive green" , PALETTERGB ( 85,107, 47)},
663 {"DarkOliveGreen" , PALETTERGB ( 85,107, 47)},
664 {"dark sea green" , PALETTERGB (143,188,143)},
665 {"DarkSeaGreen" , PALETTERGB (143,188,143)},
666 {"sea green" , PALETTERGB ( 46,139, 87)},
667 {"SeaGreen" , PALETTERGB ( 46,139, 87)},
668 {"medium sea green" , PALETTERGB ( 60,179,113)},
669 {"MediumSeaGreen" , PALETTERGB ( 60,179,113)},
670 {"light sea green" , PALETTERGB ( 32,178,170)},
671 {"LightSeaGreen" , PALETTERGB ( 32,178,170)},
672 {"pale green" , PALETTERGB (152,251,152)},
673 {"PaleGreen" , PALETTERGB (152,251,152)},
674 {"spring green" , PALETTERGB ( 0,255,127)},
675 {"SpringGreen" , PALETTERGB ( 0,255,127)},
676 {"lawn green" , PALETTERGB (124,252, 0)},
677 {"LawnGreen" , PALETTERGB (124,252, 0)},
678 {"green" , PALETTERGB ( 0,255, 0)},
679 {"chartreuse" , PALETTERGB (127,255, 0)},
680 {"medium spring green" , PALETTERGB ( 0,250,154)},
681 {"MediumSpringGreen" , PALETTERGB ( 0,250,154)},
682 {"green yellow" , PALETTERGB (173,255, 47)},
683 {"GreenYellow" , PALETTERGB (173,255, 47)},
684 {"lime green" , PALETTERGB ( 50,205, 50)},
685 {"LimeGreen" , PALETTERGB ( 50,205, 50)},
686 {"yellow green" , PALETTERGB (154,205, 50)},
687 {"YellowGreen" , PALETTERGB (154,205, 50)},
688 {"forest green" , PALETTERGB ( 34,139, 34)},
689 {"ForestGreen" , PALETTERGB ( 34,139, 34)},
690 {"olive drab" , PALETTERGB (107,142, 35)},
691 {"OliveDrab" , PALETTERGB (107,142, 35)},
692 {"dark khaki" , PALETTERGB (189,183,107)},
693 {"DarkKhaki" , PALETTERGB (189,183,107)},
694 {"khaki" , PALETTERGB (240,230,140)},
695 {"pale goldenrod" , PALETTERGB (238,232,170)},
696 {"PaleGoldenrod" , PALETTERGB (238,232,170)},
697 {"light goldenrod yellow" , PALETTERGB (250,250,210)},
698 {"LightGoldenrodYellow" , PALETTERGB (250,250,210)},
699 {"light yellow" , PALETTERGB (255,255,224)},
700 {"LightYellow" , PALETTERGB (255,255,224)},
701 {"yellow" , PALETTERGB (255,255, 0)},
702 {"gold" , PALETTERGB (255,215, 0)},
703 {"light goldenrod" , PALETTERGB (238,221,130)},
704 {"LightGoldenrod" , PALETTERGB (238,221,130)},
705 {"goldenrod" , PALETTERGB (218,165, 32)},
706 {"dark goldenrod" , PALETTERGB (184,134, 11)},
707 {"DarkGoldenrod" , PALETTERGB (184,134, 11)},
708 {"rosy brown" , PALETTERGB (188,143,143)},
709 {"RosyBrown" , PALETTERGB (188,143,143)},
710 {"indian red" , PALETTERGB (205, 92, 92)},
711 {"IndianRed" , PALETTERGB (205, 92, 92)},
712 {"saddle brown" , PALETTERGB (139, 69, 19)},
713 {"SaddleBrown" , PALETTERGB (139, 69, 19)},
714 {"sienna" , PALETTERGB (160, 82, 45)},
715 {"peru" , PALETTERGB (205,133, 63)},
716 {"burlywood" , PALETTERGB (222,184,135)},
717 {"beige" , PALETTERGB (245,245,220)},
718 {"wheat" , PALETTERGB (245,222,179)},
719 {"sandy brown" , PALETTERGB (244,164, 96)},
720 {"SandyBrown" , PALETTERGB (244,164, 96)},
721 {"tan" , PALETTERGB (210,180,140)},
722 {"chocolate" , PALETTERGB (210,105, 30)},
723 {"firebrick" , PALETTERGB (178,34, 34)},
724 {"brown" , PALETTERGB (165,42, 42)},
725 {"dark salmon" , PALETTERGB (233,150,122)},
726 {"DarkSalmon" , PALETTERGB (233,150,122)},
727 {"salmon" , PALETTERGB (250,128,114)},
728 {"light salmon" , PALETTERGB (255,160,122)},
729 {"LightSalmon" , PALETTERGB (255,160,122)},
730 {"orange" , PALETTERGB (255,165, 0)},
731 {"dark orange" , PALETTERGB (255,140, 0)},
732 {"DarkOrange" , PALETTERGB (255,140, 0)},
733 {"coral" , PALETTERGB (255,127, 80)},
734 {"light coral" , PALETTERGB (240,128,128)},
735 {"LightCoral" , PALETTERGB (240,128,128)},
736 {"tomato" , PALETTERGB (255, 99, 71)},
737 {"orange red" , PALETTERGB (255, 69, 0)},
738 {"OrangeRed" , PALETTERGB (255, 69, 0)},
739 {"red" , PALETTERGB (255, 0, 0)},
740 {"hot pink" , PALETTERGB (255,105,180)},
741 {"HotPink" , PALETTERGB (255,105,180)},
742 {"deep pink" , PALETTERGB (255, 20,147)},
743 {"DeepPink" , PALETTERGB (255, 20,147)},
744 {"pink" , PALETTERGB (255,192,203)},
745 {"light pink" , PALETTERGB (255,182,193)},
746 {"LightPink" , PALETTERGB (255,182,193)},
747 {"pale violet red" , PALETTERGB (219,112,147)},
748 {"PaleVioletRed" , PALETTERGB (219,112,147)},
749 {"maroon" , PALETTERGB (176, 48, 96)},
750 {"medium violet red" , PALETTERGB (199, 21,133)},
751 {"MediumVioletRed" , PALETTERGB (199, 21,133)},
752 {"violet red" , PALETTERGB (208, 32,144)},
753 {"VioletRed" , PALETTERGB (208, 32,144)},
754 {"magenta" , PALETTERGB (255, 0,255)},
755 {"violet" , PALETTERGB (238,130,238)},
756 {"plum" , PALETTERGB (221,160,221)},
757 {"orchid" , PALETTERGB (218,112,214)},
758 {"medium orchid" , PALETTERGB (186, 85,211)},
759 {"MediumOrchid" , PALETTERGB (186, 85,211)},
760 {"dark orchid" , PALETTERGB (153, 50,204)},
761 {"DarkOrchid" , PALETTERGB (153, 50,204)},
762 {"dark violet" , PALETTERGB (148, 0,211)},
763 {"DarkViolet" , PALETTERGB (148, 0,211)},
764 {"blue violet" , PALETTERGB (138, 43,226)},
765 {"BlueViolet" , PALETTERGB (138, 43,226)},
766 {"purple" , PALETTERGB (160, 32,240)},
767 {"medium purple" , PALETTERGB (147,112,219)},
768 {"MediumPurple" , PALETTERGB (147,112,219)},
769 {"thistle" , PALETTERGB (216,191,216)},
770 {"gray0" , PALETTERGB ( 0, 0, 0)},
771 {"grey0" , PALETTERGB ( 0, 0, 0)},
772 {"dark grey" , PALETTERGB (169,169,169)},
773 {"DarkGrey" , PALETTERGB (169,169,169)},
774 {"dark gray" , PALETTERGB (169,169,169)},
775 {"DarkGray" , PALETTERGB (169,169,169)},
776 {"dark blue" , PALETTERGB ( 0, 0,139)},
777 {"DarkBlue" , PALETTERGB ( 0, 0,139)},
778 {"dark cyan" , PALETTERGB ( 0,139,139)},
779 {"DarkCyan" , PALETTERGB ( 0,139,139)},
780 {"dark magenta" , PALETTERGB (139, 0,139)},
781 {"DarkMagenta" , PALETTERGB (139, 0,139)},
782 {"dark red" , PALETTERGB (139, 0, 0)},
783 {"DarkRed" , PALETTERGB (139, 0, 0)},
784 {"light green" , PALETTERGB (144,238,144)},
785 {"LightGreen" , PALETTERGB (144,238,144)},
788 DEFUN ("w32-default-color-map", Fw32_default_color_map, Sw32_default_color_map,
789 0, 0, 0, doc: /* Return the default color map. */)
792 int i;
793 colormap_t *pc = w32_color_map;
794 Lisp_Object cmap;
796 BLOCK_INPUT;
798 cmap = Qnil;
800 for (i = 0; i < sizeof (w32_color_map) / sizeof (w32_color_map[0]);
801 pc++, i++)
802 cmap = Fcons (Fcons (build_string (pc->name),
803 make_number (pc->colorref)),
804 cmap);
806 UNBLOCK_INPUT;
808 return (cmap);
811 Lisp_Object
812 w32_to_x_color (rgb)
813 Lisp_Object rgb;
815 Lisp_Object color;
817 CHECK_NUMBER (rgb);
819 BLOCK_INPUT;
821 color = Frassq (rgb, Vw32_color_map);
823 UNBLOCK_INPUT;
825 if (!NILP (color))
826 return (Fcar (color));
827 else
828 return Qnil;
831 static Lisp_Object
832 w32_color_map_lookup (colorname)
833 char *colorname;
835 Lisp_Object tail, ret = Qnil;
837 BLOCK_INPUT;
839 for (tail = Vw32_color_map; !NILP (tail); tail = Fcdr (tail))
841 register Lisp_Object elt, tem;
843 elt = Fcar (tail);
844 if (!CONSP (elt)) continue;
846 tem = Fcar (elt);
848 if (lstrcmpi (SDATA (tem), colorname) == 0)
850 ret = Fcdr (elt);
851 break;
854 QUIT;
858 UNBLOCK_INPUT;
860 return ret;
864 static void
865 add_system_logical_colors_to_map (system_colors)
866 Lisp_Object *system_colors;
868 HKEY colors_key;
870 /* Other registry operations are done with input blocked. */
871 BLOCK_INPUT;
873 /* Look for "Control Panel/Colors" under User and Machine registry
874 settings. */
875 if (RegOpenKeyEx (HKEY_CURRENT_USER, "Control Panel\\Colors", 0,
876 KEY_READ, &colors_key) == ERROR_SUCCESS
877 || RegOpenKeyEx (HKEY_LOCAL_MACHINE, "Control Panel\\Colors", 0,
878 KEY_READ, &colors_key) == ERROR_SUCCESS)
880 /* List all keys. */
881 char color_buffer[64];
882 char full_name_buffer[MAX_PATH + SYSTEM_COLOR_PREFIX_LEN];
883 int index = 0;
884 DWORD name_size, color_size;
885 char *name_buffer = full_name_buffer + SYSTEM_COLOR_PREFIX_LEN;
887 name_size = sizeof (full_name_buffer) - SYSTEM_COLOR_PREFIX_LEN;
888 color_size = sizeof (color_buffer);
890 strcpy (full_name_buffer, SYSTEM_COLOR_PREFIX);
892 while (RegEnumValueA (colors_key, index, name_buffer, &name_size,
893 NULL, NULL, color_buffer, &color_size)
894 == ERROR_SUCCESS)
896 int r, g, b;
897 if (sscanf (color_buffer, " %u %u %u", &r, &g, &b) == 3)
898 *system_colors = Fcons (Fcons (build_string (full_name_buffer),
899 make_number (RGB (r, g, b))),
900 *system_colors);
902 name_size = sizeof (full_name_buffer) - SYSTEM_COLOR_PREFIX_LEN;
903 color_size = sizeof (color_buffer);
904 index++;
906 RegCloseKey (colors_key);
909 UNBLOCK_INPUT;
913 static Lisp_Object
914 x_to_w32_color (colorname)
915 char * colorname;
917 register Lisp_Object ret = Qnil;
919 BLOCK_INPUT;
921 if (colorname[0] == '#')
923 /* Could be an old-style RGB Device specification. */
924 char *color;
925 int size;
926 color = colorname + 1;
928 size = strlen(color);
929 if (size == 3 || size == 6 || size == 9 || size == 12)
931 UINT colorval;
932 int i, pos;
933 pos = 0;
934 size /= 3;
935 colorval = 0;
937 for (i = 0; i < 3; i++)
939 char *end;
940 char t;
941 unsigned long value;
943 /* The check for 'x' in the following conditional takes into
944 account the fact that strtol allows a "0x" in front of
945 our numbers, and we don't. */
946 if (!isxdigit(color[0]) || color[1] == 'x')
947 break;
948 t = color[size];
949 color[size] = '\0';
950 value = strtoul(color, &end, 16);
951 color[size] = t;
952 if (errno == ERANGE || end - color != size)
953 break;
954 switch (size)
956 case 1:
957 value = value * 0x10;
958 break;
959 case 2:
960 break;
961 case 3:
962 value /= 0x10;
963 break;
964 case 4:
965 value /= 0x100;
966 break;
968 colorval |= (value << pos);
969 pos += 0x8;
970 if (i == 2)
972 UNBLOCK_INPUT;
973 XSETINT (ret, colorval);
974 return ret;
976 color = end;
980 else if (strnicmp(colorname, "rgb:", 4) == 0)
982 char *color;
983 UINT colorval;
984 int i, pos;
985 pos = 0;
987 colorval = 0;
988 color = colorname + 4;
989 for (i = 0; i < 3; i++)
991 char *end;
992 unsigned long value;
994 /* The check for 'x' in the following conditional takes into
995 account the fact that strtol allows a "0x" in front of
996 our numbers, and we don't. */
997 if (!isxdigit(color[0]) || color[1] == 'x')
998 break;
999 value = strtoul(color, &end, 16);
1000 if (errno == ERANGE)
1001 break;
1002 switch (end - color)
1004 case 1:
1005 value = value * 0x10 + value;
1006 break;
1007 case 2:
1008 break;
1009 case 3:
1010 value /= 0x10;
1011 break;
1012 case 4:
1013 value /= 0x100;
1014 break;
1015 default:
1016 value = ULONG_MAX;
1018 if (value == ULONG_MAX)
1019 break;
1020 colorval |= (value << pos);
1021 pos += 0x8;
1022 if (i == 2)
1024 if (*end != '\0')
1025 break;
1026 UNBLOCK_INPUT;
1027 XSETINT (ret, colorval);
1028 return ret;
1030 if (*end != '/')
1031 break;
1032 color = end + 1;
1035 else if (strnicmp(colorname, "rgbi:", 5) == 0)
1037 /* This is an RGB Intensity specification. */
1038 char *color;
1039 UINT colorval;
1040 int i, pos;
1041 pos = 0;
1043 colorval = 0;
1044 color = colorname + 5;
1045 for (i = 0; i < 3; i++)
1047 char *end;
1048 double value;
1049 UINT val;
1051 value = strtod(color, &end);
1052 if (errno == ERANGE)
1053 break;
1054 if (value < 0.0 || value > 1.0)
1055 break;
1056 val = (UINT)(0x100 * value);
1057 /* We used 0x100 instead of 0xFF to give a continuous
1058 range between 0.0 and 1.0 inclusive. The next statement
1059 fixes the 1.0 case. */
1060 if (val == 0x100)
1061 val = 0xFF;
1062 colorval |= (val << pos);
1063 pos += 0x8;
1064 if (i == 2)
1066 if (*end != '\0')
1067 break;
1068 UNBLOCK_INPUT;
1069 XSETINT (ret, colorval);
1070 return ret;
1072 if (*end != '/')
1073 break;
1074 color = end + 1;
1077 /* I am not going to attempt to handle any of the CIE color schemes
1078 or TekHVC, since I don't know the algorithms for conversion to
1079 RGB. */
1081 /* If we fail to lookup the color name in w32_color_map, then check the
1082 colorname to see if it can be crudely approximated: If the X color
1083 ends in a number (e.g., "darkseagreen2"), strip the number and
1084 return the result of looking up the base color name. */
1085 ret = w32_color_map_lookup (colorname);
1086 if (NILP (ret))
1088 int len = strlen (colorname);
1090 if (isdigit (colorname[len - 1]))
1092 char *ptr, *approx = alloca (len + 1);
1094 strcpy (approx, colorname);
1095 ptr = &approx[len - 1];
1096 while (ptr > approx && isdigit (*ptr))
1097 *ptr-- = '\0';
1099 ret = w32_color_map_lookup (approx);
1103 UNBLOCK_INPUT;
1104 return ret;
1107 void
1108 w32_regenerate_palette (FRAME_PTR f)
1110 struct w32_palette_entry * list;
1111 LOGPALETTE * log_palette;
1112 HPALETTE new_palette;
1113 int i;
1115 /* don't bother trying to create palette if not supported */
1116 if (! FRAME_W32_DISPLAY_INFO (f)->has_palette)
1117 return;
1119 log_palette = (LOGPALETTE *)
1120 alloca (sizeof (LOGPALETTE) +
1121 FRAME_W32_DISPLAY_INFO (f)->num_colors * sizeof (PALETTEENTRY));
1122 log_palette->palVersion = 0x300;
1123 log_palette->palNumEntries = FRAME_W32_DISPLAY_INFO (f)->num_colors;
1125 list = FRAME_W32_DISPLAY_INFO (f)->color_list;
1126 for (i = 0;
1127 i < FRAME_W32_DISPLAY_INFO (f)->num_colors;
1128 i++, list = list->next)
1129 log_palette->palPalEntry[i] = list->entry;
1131 new_palette = CreatePalette (log_palette);
1133 enter_crit ();
1135 if (FRAME_W32_DISPLAY_INFO (f)->palette)
1136 DeleteObject (FRAME_W32_DISPLAY_INFO (f)->palette);
1137 FRAME_W32_DISPLAY_INFO (f)->palette = new_palette;
1139 /* Realize display palette and garbage all frames. */
1140 release_frame_dc (f, get_frame_dc (f));
1142 leave_crit ();
1145 #define W32_COLOR(pe) RGB (pe.peRed, pe.peGreen, pe.peBlue)
1146 #define SET_W32_COLOR(pe, color) \
1147 do \
1149 pe.peRed = GetRValue (color); \
1150 pe.peGreen = GetGValue (color); \
1151 pe.peBlue = GetBValue (color); \
1152 pe.peFlags = 0; \
1153 } while (0)
1155 #if 0
1156 /* Keep these around in case we ever want to track color usage. */
1157 void
1158 w32_map_color (FRAME_PTR f, COLORREF color)
1160 struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
1162 if (NILP (Vw32_enable_palette))
1163 return;
1165 /* check if color is already mapped */
1166 while (list)
1168 if (W32_COLOR (list->entry) == color)
1170 ++list->refcount;
1171 return;
1173 list = list->next;
1176 /* not already mapped, so add to list and recreate Windows palette */
1177 list = (struct w32_palette_entry *)
1178 xmalloc (sizeof (struct w32_palette_entry));
1179 SET_W32_COLOR (list->entry, color);
1180 list->refcount = 1;
1181 list->next = FRAME_W32_DISPLAY_INFO (f)->color_list;
1182 FRAME_W32_DISPLAY_INFO (f)->color_list = list;
1183 FRAME_W32_DISPLAY_INFO (f)->num_colors++;
1185 /* set flag that palette must be regenerated */
1186 FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
1189 void
1190 w32_unmap_color (FRAME_PTR f, COLORREF color)
1192 struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
1193 struct w32_palette_entry **prev = &FRAME_W32_DISPLAY_INFO (f)->color_list;
1195 if (NILP (Vw32_enable_palette))
1196 return;
1198 /* check if color is already mapped */
1199 while (list)
1201 if (W32_COLOR (list->entry) == color)
1203 if (--list->refcount == 0)
1205 *prev = list->next;
1206 xfree (list);
1207 FRAME_W32_DISPLAY_INFO (f)->num_colors--;
1208 break;
1210 else
1211 return;
1213 prev = &list->next;
1214 list = list->next;
1217 /* set flag that palette must be regenerated */
1218 FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
1220 #endif
1223 /* Gamma-correct COLOR on frame F. */
1225 void
1226 gamma_correct (f, color)
1227 struct frame *f;
1228 COLORREF *color;
1230 if (f->gamma)
1232 *color = PALETTERGB (
1233 pow (GetRValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
1234 pow (GetGValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
1235 pow (GetBValue (*color) / 255.0, f->gamma) * 255.0 + 0.5);
1240 /* Decide if color named COLOR is valid for the display associated with
1241 the selected frame; if so, return the rgb values in COLOR_DEF.
1242 If ALLOC is nonzero, allocate a new colormap cell. */
1245 w32_defined_color (f, color, color_def, alloc)
1246 FRAME_PTR f;
1247 char *color;
1248 XColor *color_def;
1249 int alloc;
1251 register Lisp_Object tem;
1252 COLORREF w32_color_ref;
1254 tem = x_to_w32_color (color);
1256 if (!NILP (tem))
1258 if (f)
1260 /* Apply gamma correction. */
1261 w32_color_ref = XUINT (tem);
1262 gamma_correct (f, &w32_color_ref);
1263 XSETINT (tem, w32_color_ref);
1266 /* Map this color to the palette if it is enabled. */
1267 if (!NILP (Vw32_enable_palette))
1269 struct w32_palette_entry * entry =
1270 one_w32_display_info.color_list;
1271 struct w32_palette_entry ** prev =
1272 &one_w32_display_info.color_list;
1274 /* check if color is already mapped */
1275 while (entry)
1277 if (W32_COLOR (entry->entry) == XUINT (tem))
1278 break;
1279 prev = &entry->next;
1280 entry = entry->next;
1283 if (entry == NULL && alloc)
1285 /* not already mapped, so add to list */
1286 entry = (struct w32_palette_entry *)
1287 xmalloc (sizeof (struct w32_palette_entry));
1288 SET_W32_COLOR (entry->entry, XUINT (tem));
1289 entry->next = NULL;
1290 *prev = entry;
1291 one_w32_display_info.num_colors++;
1293 /* set flag that palette must be regenerated */
1294 one_w32_display_info.regen_palette = TRUE;
1297 /* Ensure COLORREF value is snapped to nearest color in (default)
1298 palette by simulating the PALETTERGB macro. This works whether
1299 or not the display device has a palette. */
1300 w32_color_ref = XUINT (tem) | 0x2000000;
1302 color_def->pixel = w32_color_ref;
1303 color_def->red = GetRValue (w32_color_ref) * 256;
1304 color_def->green = GetGValue (w32_color_ref) * 256;
1305 color_def->blue = GetBValue (w32_color_ref) * 256;
1307 return 1;
1309 else
1311 return 0;
1315 /* Given a string ARG naming a color, compute a pixel value from it
1316 suitable for screen F.
1317 If F is not a color screen, return DEF (default) regardless of what
1318 ARG says. */
1321 x_decode_color (f, arg, def)
1322 FRAME_PTR f;
1323 Lisp_Object arg;
1324 int def;
1326 XColor cdef;
1328 CHECK_STRING (arg);
1330 if (strcmp (SDATA (arg), "black") == 0)
1331 return BLACK_PIX_DEFAULT (f);
1332 else if (strcmp (SDATA (arg), "white") == 0)
1333 return WHITE_PIX_DEFAULT (f);
1335 if ((FRAME_W32_DISPLAY_INFO (f)->n_planes * FRAME_W32_DISPLAY_INFO (f)->n_cbits) == 1)
1336 return def;
1338 /* w32_defined_color is responsible for coping with failures
1339 by looking for a near-miss. */
1340 if (w32_defined_color (f, SDATA (arg), &cdef, 1))
1341 return cdef.pixel;
1343 /* defined_color failed; return an ultimate default. */
1344 return def;
1349 /* Functions called only from `x_set_frame_param'
1350 to set individual parameters.
1352 If FRAME_W32_WINDOW (f) is 0,
1353 the frame is being created and its window does not exist yet.
1354 In that case, just record the parameter's new value
1355 in the standard place; do not attempt to change the window. */
1357 void
1358 x_set_foreground_color (f, arg, oldval)
1359 struct frame *f;
1360 Lisp_Object arg, oldval;
1362 struct w32_output *x = f->output_data.w32;
1363 PIX_TYPE fg, old_fg;
1365 fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1366 old_fg = FRAME_FOREGROUND_PIXEL (f);
1367 FRAME_FOREGROUND_PIXEL (f) = fg;
1369 if (FRAME_W32_WINDOW (f) != 0)
1371 if (x->cursor_pixel == old_fg)
1372 x->cursor_pixel = fg;
1374 update_face_from_frame_parameter (f, Qforeground_color, arg);
1375 if (FRAME_VISIBLE_P (f))
1376 redraw_frame (f);
1380 void
1381 x_set_background_color (f, arg, oldval)
1382 struct frame *f;
1383 Lisp_Object arg, oldval;
1385 FRAME_BACKGROUND_PIXEL (f)
1386 = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
1388 if (FRAME_W32_WINDOW (f) != 0)
1390 SetWindowLong (FRAME_W32_WINDOW (f), WND_BACKGROUND_INDEX,
1391 FRAME_BACKGROUND_PIXEL (f));
1393 update_face_from_frame_parameter (f, Qbackground_color, arg);
1395 if (FRAME_VISIBLE_P (f))
1396 redraw_frame (f);
1400 void
1401 x_set_mouse_color (f, arg, oldval)
1402 struct frame *f;
1403 Lisp_Object arg, oldval;
1405 Cursor cursor, nontext_cursor, mode_cursor, hand_cursor;
1406 int count;
1407 int mask_color;
1409 if (!EQ (Qnil, arg))
1410 f->output_data.w32->mouse_pixel
1411 = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1412 mask_color = FRAME_BACKGROUND_PIXEL (f);
1414 /* Don't let pointers be invisible. */
1415 if (mask_color == f->output_data.w32->mouse_pixel
1416 && mask_color == FRAME_BACKGROUND_PIXEL (f))
1417 f->output_data.w32->mouse_pixel = FRAME_FOREGROUND_PIXEL (f);
1419 #if 0 /* TODO : cursor changes */
1420 BLOCK_INPUT;
1422 /* It's not okay to crash if the user selects a screwy cursor. */
1423 count = x_catch_errors (FRAME_W32_DISPLAY (f));
1425 if (!EQ (Qnil, Vx_pointer_shape))
1427 CHECK_NUMBER (Vx_pointer_shape);
1428 cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XINT (Vx_pointer_shape));
1430 else
1431 cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
1432 x_check_errors (FRAME_W32_DISPLAY (f), "bad text pointer cursor: %s");
1434 if (!EQ (Qnil, Vx_nontext_pointer_shape))
1436 CHECK_NUMBER (Vx_nontext_pointer_shape);
1437 nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1438 XINT (Vx_nontext_pointer_shape));
1440 else
1441 nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_left_ptr);
1442 x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
1444 if (!EQ (Qnil, Vx_hourglass_pointer_shape))
1446 CHECK_NUMBER (Vx_hourglass_pointer_shape);
1447 hourglass_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1448 XINT (Vx_hourglass_pointer_shape));
1450 else
1451 hourglass_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_watch);
1452 x_check_errors (FRAME_W32_DISPLAY (f), "bad busy pointer cursor: %s");
1454 x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
1455 if (!EQ (Qnil, Vx_mode_pointer_shape))
1457 CHECK_NUMBER (Vx_mode_pointer_shape);
1458 mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1459 XINT (Vx_mode_pointer_shape));
1461 else
1462 mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
1463 x_check_errors (FRAME_W32_DISPLAY (f), "bad modeline pointer cursor: %s");
1465 if (!EQ (Qnil, Vx_sensitive_text_pointer_shape))
1467 CHECK_NUMBER (Vx_sensitive_text_pointer_shape);
1468 hand_cursor
1469 = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1470 XINT (Vx_sensitive_text_pointer_shape));
1472 else
1473 hand_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_crosshair);
1475 if (!NILP (Vx_window_horizontal_drag_shape))
1477 CHECK_NUMBER (Vx_window_horizontal_drag_shape);
1478 horizontal_drag_cursor
1479 = XCreateFontCursor (FRAME_X_DISPLAY (f),
1480 XINT (Vx_window_horizontal_drag_shape));
1482 else
1483 horizontal_drag_cursor
1484 = XCreateFontCursor (FRAME_X_DISPLAY (f), XC_sb_h_double_arrow);
1486 /* Check and report errors with the above calls. */
1487 x_check_errors (FRAME_W32_DISPLAY (f), "can't set cursor shape: %s");
1488 x_uncatch_errors (FRAME_W32_DISPLAY (f), count);
1491 XColor fore_color, back_color;
1493 fore_color.pixel = f->output_data.w32->mouse_pixel;
1494 back_color.pixel = mask_color;
1495 XQueryColor (FRAME_W32_DISPLAY (f),
1496 DefaultColormap (FRAME_W32_DISPLAY (f),
1497 DefaultScreen (FRAME_W32_DISPLAY (f))),
1498 &fore_color);
1499 XQueryColor (FRAME_W32_DISPLAY (f),
1500 DefaultColormap (FRAME_W32_DISPLAY (f),
1501 DefaultScreen (FRAME_W32_DISPLAY (f))),
1502 &back_color);
1503 XRecolorCursor (FRAME_W32_DISPLAY (f), cursor,
1504 &fore_color, &back_color);
1505 XRecolorCursor (FRAME_W32_DISPLAY (f), nontext_cursor,
1506 &fore_color, &back_color);
1507 XRecolorCursor (FRAME_W32_DISPLAY (f), mode_cursor,
1508 &fore_color, &back_color);
1509 XRecolorCursor (FRAME_W32_DISPLAY (f), hand_cursor,
1510 &fore_color, &back_color);
1511 XRecolorCursor (FRAME_W32_DISPLAY (f), hourglass_cursor,
1512 &fore_color, &back_color);
1515 if (FRAME_W32_WINDOW (f) != 0)
1516 XDefineCursor (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), cursor);
1518 if (cursor != f->output_data.w32->text_cursor && f->output_data.w32->text_cursor != 0)
1519 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->text_cursor);
1520 f->output_data.w32->text_cursor = cursor;
1522 if (nontext_cursor != f->output_data.w32->nontext_cursor
1523 && f->output_data.w32->nontext_cursor != 0)
1524 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->nontext_cursor);
1525 f->output_data.w32->nontext_cursor = nontext_cursor;
1527 if (hourglass_cursor != f->output_data.w32->hourglass_cursor
1528 && f->output_data.w32->hourglass_cursor != 0)
1529 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->hourglass_cursor);
1530 f->output_data.w32->hourglass_cursor = hourglass_cursor;
1532 if (mode_cursor != f->output_data.w32->modeline_cursor
1533 && f->output_data.w32->modeline_cursor != 0)
1534 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->modeline_cursor);
1535 f->output_data.w32->modeline_cursor = mode_cursor;
1537 if (hand_cursor != f->output_data.w32->hand_cursor
1538 && f->output_data.w32->hand_cursor != 0)
1539 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->hand_cursor);
1540 f->output_data.w32->hand_cursor = hand_cursor;
1542 XFlush (FRAME_W32_DISPLAY (f));
1543 UNBLOCK_INPUT;
1545 update_face_from_frame_parameter (f, Qmouse_color, arg);
1546 #endif /* TODO */
1549 /* Defined in w32term.c. */
1550 void
1551 x_set_cursor_color (f, arg, oldval)
1552 struct frame *f;
1553 Lisp_Object arg, oldval;
1555 unsigned long fore_pixel, pixel;
1557 if (!NILP (Vx_cursor_fore_pixel))
1558 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
1559 WHITE_PIX_DEFAULT (f));
1560 else
1561 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
1563 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1565 /* Make sure that the cursor color differs from the background color. */
1566 if (pixel == FRAME_BACKGROUND_PIXEL (f))
1568 pixel = f->output_data.w32->mouse_pixel;
1569 if (pixel == fore_pixel)
1570 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
1573 f->output_data.w32->cursor_foreground_pixel = fore_pixel;
1574 f->output_data.w32->cursor_pixel = pixel;
1576 if (FRAME_W32_WINDOW (f) != 0)
1578 BLOCK_INPUT;
1579 /* Update frame's cursor_gc. */
1580 f->output_data.w32->cursor_gc->foreground = fore_pixel;
1581 f->output_data.w32->cursor_gc->background = pixel;
1583 UNBLOCK_INPUT;
1585 if (FRAME_VISIBLE_P (f))
1587 x_update_cursor (f, 0);
1588 x_update_cursor (f, 1);
1592 update_face_from_frame_parameter (f, Qcursor_color, arg);
1595 /* Set the border-color of frame F to pixel value PIX.
1596 Note that this does not fully take effect if done before
1597 F has a window. */
1599 void
1600 x_set_border_pixel (f, pix)
1601 struct frame *f;
1602 int pix;
1605 f->output_data.w32->border_pixel = pix;
1607 if (FRAME_W32_WINDOW (f) != 0 && f->border_width > 0)
1609 if (FRAME_VISIBLE_P (f))
1610 redraw_frame (f);
1614 /* Set the border-color of frame F to value described by ARG.
1615 ARG can be a string naming a color.
1616 The border-color is used for the border that is drawn by the server.
1617 Note that this does not fully take effect if done before
1618 F has a window; it must be redone when the window is created. */
1620 void
1621 x_set_border_color (f, arg, oldval)
1622 struct frame *f;
1623 Lisp_Object arg, oldval;
1625 int pix;
1627 CHECK_STRING (arg);
1628 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1629 x_set_border_pixel (f, pix);
1630 update_face_from_frame_parameter (f, Qborder_color, arg);
1634 void
1635 x_set_cursor_type (f, arg, oldval)
1636 FRAME_PTR f;
1637 Lisp_Object arg, oldval;
1639 set_frame_cursor_types (f, arg);
1641 /* Make sure the cursor gets redrawn. */
1642 cursor_type_changed = 1;
1645 void
1646 x_set_icon_type (f, arg, oldval)
1647 struct frame *f;
1648 Lisp_Object arg, oldval;
1650 int result;
1652 if (NILP (arg) && NILP (oldval))
1653 return;
1655 if (STRINGP (arg) && STRINGP (oldval)
1656 && EQ (Fstring_equal (oldval, arg), Qt))
1657 return;
1659 if (SYMBOLP (arg) && SYMBOLP (oldval) && EQ (arg, oldval))
1660 return;
1662 BLOCK_INPUT;
1664 result = x_bitmap_icon (f, arg);
1665 if (result)
1667 UNBLOCK_INPUT;
1668 error ("No icon window available");
1671 UNBLOCK_INPUT;
1674 void
1675 x_set_icon_name (f, arg, oldval)
1676 struct frame *f;
1677 Lisp_Object arg, oldval;
1679 if (STRINGP (arg))
1681 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1682 return;
1684 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
1685 return;
1687 f->icon_name = arg;
1689 #if 0
1690 if (f->output_data.w32->icon_bitmap != 0)
1691 return;
1693 BLOCK_INPUT;
1695 result = x_text_icon (f,
1696 (char *) SDATA ((!NILP (f->icon_name)
1697 ? f->icon_name
1698 : !NILP (f->title)
1699 ? f->title
1700 : f->name)));
1702 if (result)
1704 UNBLOCK_INPUT;
1705 error ("No icon window available");
1708 /* If the window was unmapped (and its icon was mapped),
1709 the new icon is not mapped, so map the window in its stead. */
1710 if (FRAME_VISIBLE_P (f))
1712 #ifdef USE_X_TOOLKIT
1713 XtPopup (f->output_data.w32->widget, XtGrabNone);
1714 #endif
1715 XMapWindow (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f));
1718 XFlush (FRAME_W32_DISPLAY (f));
1719 UNBLOCK_INPUT;
1720 #endif
1724 void
1725 x_set_menu_bar_lines (f, value, oldval)
1726 struct frame *f;
1727 Lisp_Object value, oldval;
1729 int nlines;
1730 int olines = FRAME_MENU_BAR_LINES (f);
1732 /* Right now, menu bars don't work properly in minibuf-only frames;
1733 most of the commands try to apply themselves to the minibuffer
1734 frame itself, and get an error because you can't switch buffers
1735 in or split the minibuffer window. */
1736 if (FRAME_MINIBUF_ONLY_P (f))
1737 return;
1739 if (INTEGERP (value))
1740 nlines = XINT (value);
1741 else
1742 nlines = 0;
1744 FRAME_MENU_BAR_LINES (f) = 0;
1745 if (nlines)
1746 FRAME_EXTERNAL_MENU_BAR (f) = 1;
1747 else
1749 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
1750 free_frame_menubar (f);
1751 FRAME_EXTERNAL_MENU_BAR (f) = 0;
1753 /* Adjust the frame size so that the client (text) dimensions
1754 remain the same. This depends on FRAME_EXTERNAL_MENU_BAR being
1755 set correctly. */
1756 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
1757 do_pending_window_change (0);
1759 adjust_glyphs (f);
1763 /* Set the number of lines used for the tool bar of frame F to VALUE.
1764 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
1765 is the old number of tool bar lines. This function changes the
1766 height of all windows on frame F to match the new tool bar height.
1767 The frame's height doesn't change. */
1769 void
1770 x_set_tool_bar_lines (f, value, oldval)
1771 struct frame *f;
1772 Lisp_Object value, oldval;
1774 int delta, nlines, root_height;
1775 Lisp_Object root_window;
1777 /* Treat tool bars like menu bars. */
1778 if (FRAME_MINIBUF_ONLY_P (f))
1779 return;
1781 /* Use VALUE only if an integer >= 0. */
1782 if (INTEGERP (value) && XINT (value) >= 0)
1783 nlines = XFASTINT (value);
1784 else
1785 nlines = 0;
1787 /* Make sure we redisplay all windows in this frame. */
1788 ++windows_or_buffers_changed;
1790 delta = nlines - FRAME_TOOL_BAR_LINES (f);
1792 /* Don't resize the tool-bar to more than we have room for. */
1793 root_window = FRAME_ROOT_WINDOW (f);
1794 root_height = WINDOW_TOTAL_LINES (XWINDOW (root_window));
1795 if (root_height - delta < 1)
1797 delta = root_height - 1;
1798 nlines = FRAME_TOOL_BAR_LINES (f) + delta;
1801 FRAME_TOOL_BAR_LINES (f) = nlines;
1802 change_window_heights (root_window, delta);
1803 adjust_glyphs (f);
1805 /* We also have to make sure that the internal border at the top of
1806 the frame, below the menu bar or tool bar, is redrawn when the
1807 tool bar disappears. This is so because the internal border is
1808 below the tool bar if one is displayed, but is below the menu bar
1809 if there isn't a tool bar. The tool bar draws into the area
1810 below the menu bar. */
1811 if (FRAME_W32_WINDOW (f) && FRAME_TOOL_BAR_LINES (f) == 0)
1813 updating_frame = f;
1814 clear_frame ();
1815 clear_current_matrices (f);
1816 updating_frame = NULL;
1819 /* If the tool bar gets smaller, the internal border below it
1820 has to be cleared. It was formerly part of the display
1821 of the larger tool bar, and updating windows won't clear it. */
1822 if (delta < 0)
1824 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
1825 int width = FRAME_PIXEL_WIDTH (f);
1826 int y = nlines * FRAME_LINE_HEIGHT (f);
1828 BLOCK_INPUT;
1830 HDC hdc = get_frame_dc (f);
1831 w32_clear_area (f, hdc, 0, y, width, height);
1832 release_frame_dc (f, hdc);
1834 UNBLOCK_INPUT;
1836 if (WINDOWP (f->tool_bar_window))
1837 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
1842 /* Change the name of frame F to NAME. If NAME is nil, set F's name to
1843 w32_id_name.
1845 If EXPLICIT is non-zero, that indicates that lisp code is setting the
1846 name; if NAME is a string, set F's name to NAME and set
1847 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1849 If EXPLICIT is zero, that indicates that Emacs redisplay code is
1850 suggesting a new name, which lisp code should override; if
1851 F->explicit_name is set, ignore the new name; otherwise, set it. */
1853 void
1854 x_set_name (f, name, explicit)
1855 struct frame *f;
1856 Lisp_Object name;
1857 int explicit;
1859 /* Make sure that requests from lisp code override requests from
1860 Emacs redisplay code. */
1861 if (explicit)
1863 /* If we're switching from explicit to implicit, we had better
1864 update the mode lines and thereby update the title. */
1865 if (f->explicit_name && NILP (name))
1866 update_mode_lines = 1;
1868 f->explicit_name = ! NILP (name);
1870 else if (f->explicit_name)
1871 return;
1873 /* If NAME is nil, set the name to the w32_id_name. */
1874 if (NILP (name))
1876 /* Check for no change needed in this very common case
1877 before we do any consing. */
1878 if (!strcmp (FRAME_W32_DISPLAY_INFO (f)->w32_id_name,
1879 SDATA (f->name)))
1880 return;
1881 name = build_string (FRAME_W32_DISPLAY_INFO (f)->w32_id_name);
1883 else
1884 CHECK_STRING (name);
1886 /* Don't change the name if it's already NAME. */
1887 if (! NILP (Fstring_equal (name, f->name)))
1888 return;
1890 f->name = name;
1892 /* For setting the frame title, the title parameter should override
1893 the name parameter. */
1894 if (! NILP (f->title))
1895 name = f->title;
1897 if (FRAME_W32_WINDOW (f))
1899 if (STRING_MULTIBYTE (name))
1900 name = ENCODE_SYSTEM (name);
1902 BLOCK_INPUT;
1903 SetWindowText(FRAME_W32_WINDOW (f), SDATA (name));
1904 UNBLOCK_INPUT;
1908 /* This function should be called when the user's lisp code has
1909 specified a name for the frame; the name will override any set by the
1910 redisplay code. */
1911 void
1912 x_explicitly_set_name (f, arg, oldval)
1913 FRAME_PTR f;
1914 Lisp_Object arg, oldval;
1916 x_set_name (f, arg, 1);
1919 /* This function should be called by Emacs redisplay code to set the
1920 name; names set this way will never override names set by the user's
1921 lisp code. */
1922 void
1923 x_implicitly_set_name (f, arg, oldval)
1924 FRAME_PTR f;
1925 Lisp_Object arg, oldval;
1927 x_set_name (f, arg, 0);
1930 /* Change the title of frame F to NAME.
1931 If NAME is nil, use the frame name as the title.
1933 If EXPLICIT is non-zero, that indicates that lisp code is setting the
1934 name; if NAME is a string, set F's name to NAME and set
1935 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1937 If EXPLICIT is zero, that indicates that Emacs redisplay code is
1938 suggesting a new name, which lisp code should override; if
1939 F->explicit_name is set, ignore the new name; otherwise, set it. */
1941 void
1942 x_set_title (f, name, old_name)
1943 struct frame *f;
1944 Lisp_Object name, old_name;
1946 /* Don't change the title if it's already NAME. */
1947 if (EQ (name, f->title))
1948 return;
1950 update_mode_lines = 1;
1952 f->title = name;
1954 if (NILP (name))
1955 name = f->name;
1957 if (FRAME_W32_WINDOW (f))
1959 if (STRING_MULTIBYTE (name))
1960 name = ENCODE_SYSTEM (name);
1962 BLOCK_INPUT;
1963 SetWindowText(FRAME_W32_WINDOW (f), SDATA (name));
1964 UNBLOCK_INPUT;
1969 void x_set_scroll_bar_default_width (f)
1970 struct frame *f;
1972 int wid = FRAME_COLUMN_WIDTH (f);
1974 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = GetSystemMetrics (SM_CXVSCROLL);
1975 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) +
1976 wid - 1) / wid;
1980 /* Subroutines of creating a frame. */
1983 /* Return the value of parameter PARAM.
1985 First search ALIST, then Vdefault_frame_alist, then the X defaults
1986 database, using ATTRIBUTE as the attribute name and CLASS as its class.
1988 Convert the resource to the type specified by desired_type.
1990 If no default is specified, return Qunbound. If you call
1991 w32_get_arg, make sure you deal with Qunbound in a reasonable way,
1992 and don't let it get stored in any Lisp-visible variables! */
1994 static Lisp_Object
1995 w32_get_arg (alist, param, attribute, class, type)
1996 Lisp_Object alist, param;
1997 char *attribute;
1998 char *class;
1999 enum resource_types type;
2001 return x_get_arg (check_x_display_info (Qnil),
2002 alist, param, attribute, class, type);
2006 Cursor
2007 w32_load_cursor (LPCTSTR name)
2009 /* Try first to load cursor from application resource. */
2010 Cursor cursor = LoadImage ((HINSTANCE) GetModuleHandle(NULL),
2011 name, IMAGE_CURSOR, 0, 0,
2012 LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_SHARED);
2013 if (!cursor)
2015 /* Then try to load a shared predefined cursor. */
2016 cursor = LoadImage (NULL, name, IMAGE_CURSOR, 0, 0,
2017 LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_SHARED);
2019 return cursor;
2022 extern LRESULT CALLBACK w32_wnd_proc ();
2024 BOOL
2025 w32_init_class (hinst)
2026 HINSTANCE hinst;
2028 WNDCLASS wc;
2030 wc.style = CS_HREDRAW | CS_VREDRAW;
2031 wc.lpfnWndProc = (WNDPROC) w32_wnd_proc;
2032 wc.cbClsExtra = 0;
2033 wc.cbWndExtra = WND_EXTRA_BYTES;
2034 wc.hInstance = hinst;
2035 wc.hIcon = LoadIcon (hinst, EMACS_CLASS);
2036 wc.hCursor = w32_load_cursor (IDC_ARROW);
2037 wc.hbrBackground = NULL; /* GetStockObject (WHITE_BRUSH); */
2038 wc.lpszMenuName = NULL;
2039 wc.lpszClassName = EMACS_CLASS;
2041 return (RegisterClass (&wc));
2044 HWND
2045 w32_createscrollbar (f, bar)
2046 struct frame *f;
2047 struct scroll_bar * bar;
2049 return (CreateWindow ("SCROLLBAR", "", SBS_VERT | WS_CHILD | WS_VISIBLE,
2050 /* Position and size of scroll bar. */
2051 XINT(bar->left) + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
2052 XINT(bar->top),
2053 XINT(bar->width) - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
2054 XINT(bar->height),
2055 FRAME_W32_WINDOW (f),
2056 NULL,
2057 hinst,
2058 NULL));
2061 void
2062 w32_createwindow (f)
2063 struct frame *f;
2065 HWND hwnd;
2066 RECT rect;
2068 rect.left = rect.top = 0;
2069 rect.right = FRAME_PIXEL_WIDTH (f);
2070 rect.bottom = FRAME_PIXEL_HEIGHT (f);
2072 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
2073 FRAME_EXTERNAL_MENU_BAR (f));
2075 /* Do first time app init */
2077 if (!hprevinst)
2079 w32_init_class (hinst);
2082 FRAME_W32_WINDOW (f) = hwnd
2083 = CreateWindow (EMACS_CLASS,
2084 f->namebuf,
2085 f->output_data.w32->dwStyle | WS_CLIPCHILDREN,
2086 f->left_pos,
2087 f->top_pos,
2088 rect.right - rect.left,
2089 rect.bottom - rect.top,
2090 NULL,
2091 NULL,
2092 hinst,
2093 NULL);
2095 if (hwnd)
2097 SetWindowLong (hwnd, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
2098 SetWindowLong (hwnd, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
2099 SetWindowLong (hwnd, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
2100 SetWindowLong (hwnd, WND_SCROLLBAR_INDEX, f->scroll_bar_actual_width);
2101 SetWindowLong (hwnd, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
2103 /* Enable drag-n-drop. */
2104 DragAcceptFiles (hwnd, TRUE);
2106 /* Do this to discard the default setting specified by our parent. */
2107 ShowWindow (hwnd, SW_HIDE);
2111 void
2112 my_post_msg (wmsg, hwnd, msg, wParam, lParam)
2113 W32Msg * wmsg;
2114 HWND hwnd;
2115 UINT msg;
2116 WPARAM wParam;
2117 LPARAM lParam;
2119 wmsg->msg.hwnd = hwnd;
2120 wmsg->msg.message = msg;
2121 wmsg->msg.wParam = wParam;
2122 wmsg->msg.lParam = lParam;
2123 wmsg->msg.time = GetMessageTime ();
2125 post_msg (wmsg);
2128 /* GetKeyState and MapVirtualKey on Windows 95 do not actually distinguish
2129 between left and right keys as advertised. We test for this
2130 support dynamically, and set a flag when the support is absent. If
2131 absent, we keep track of the left and right control and alt keys
2132 ourselves. This is particularly necessary on keyboards that rely
2133 upon the AltGr key, which is represented as having the left control
2134 and right alt keys pressed. For these keyboards, we need to know
2135 when the left alt key has been pressed in addition to the AltGr key
2136 so that we can properly support M-AltGr-key sequences (such as M-@
2137 on Swedish keyboards). */
2139 #define EMACS_LCONTROL 0
2140 #define EMACS_RCONTROL 1
2141 #define EMACS_LMENU 2
2142 #define EMACS_RMENU 3
2144 static int modifiers[4];
2145 static int modifiers_recorded;
2146 static int modifier_key_support_tested;
2148 static void
2149 test_modifier_support (unsigned int wparam)
2151 unsigned int l, r;
2153 if (wparam != VK_CONTROL && wparam != VK_MENU)
2154 return;
2155 if (wparam == VK_CONTROL)
2157 l = VK_LCONTROL;
2158 r = VK_RCONTROL;
2160 else
2162 l = VK_LMENU;
2163 r = VK_RMENU;
2165 if (!(GetKeyState (l) & 0x8000) && !(GetKeyState (r) & 0x8000))
2166 modifiers_recorded = 1;
2167 else
2168 modifiers_recorded = 0;
2169 modifier_key_support_tested = 1;
2172 static void
2173 record_keydown (unsigned int wparam, unsigned int lparam)
2175 int i;
2177 if (!modifier_key_support_tested)
2178 test_modifier_support (wparam);
2180 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
2181 return;
2183 if (wparam == VK_CONTROL)
2184 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
2185 else
2186 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
2188 modifiers[i] = 1;
2191 static void
2192 record_keyup (unsigned int wparam, unsigned int lparam)
2194 int i;
2196 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
2197 return;
2199 if (wparam == VK_CONTROL)
2200 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
2201 else
2202 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
2204 modifiers[i] = 0;
2207 /* Emacs can lose focus while a modifier key has been pressed. When
2208 it regains focus, be conservative and clear all modifiers since
2209 we cannot reconstruct the left and right modifier state. */
2210 static void
2211 reset_modifiers ()
2213 SHORT ctrl, alt;
2215 if (GetFocus () == NULL)
2216 /* Emacs doesn't have keyboard focus. Do nothing. */
2217 return;
2219 ctrl = GetAsyncKeyState (VK_CONTROL);
2220 alt = GetAsyncKeyState (VK_MENU);
2222 if (!(ctrl & 0x08000))
2223 /* Clear any recorded control modifier state. */
2224 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
2226 if (!(alt & 0x08000))
2227 /* Clear any recorded alt modifier state. */
2228 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
2230 /* Update the state of all modifier keys, because modifiers used in
2231 hot-key combinations can get stuck on if Emacs loses focus as a
2232 result of a hot-key being pressed. */
2234 BYTE keystate[256];
2236 #define CURRENT_STATE(key) ((GetAsyncKeyState (key) & 0x8000) >> 8)
2238 GetKeyboardState (keystate);
2239 keystate[VK_SHIFT] = CURRENT_STATE (VK_SHIFT);
2240 keystate[VK_CONTROL] = CURRENT_STATE (VK_CONTROL);
2241 keystate[VK_LCONTROL] = CURRENT_STATE (VK_LCONTROL);
2242 keystate[VK_RCONTROL] = CURRENT_STATE (VK_RCONTROL);
2243 keystate[VK_MENU] = CURRENT_STATE (VK_MENU);
2244 keystate[VK_LMENU] = CURRENT_STATE (VK_LMENU);
2245 keystate[VK_RMENU] = CURRENT_STATE (VK_RMENU);
2246 keystate[VK_LWIN] = CURRENT_STATE (VK_LWIN);
2247 keystate[VK_RWIN] = CURRENT_STATE (VK_RWIN);
2248 keystate[VK_APPS] = CURRENT_STATE (VK_APPS);
2249 SetKeyboardState (keystate);
2253 /* Synchronize modifier state with what is reported with the current
2254 keystroke. Even if we cannot distinguish between left and right
2255 modifier keys, we know that, if no modifiers are set, then neither
2256 the left or right modifier should be set. */
2257 static void
2258 sync_modifiers ()
2260 if (!modifiers_recorded)
2261 return;
2263 if (!(GetKeyState (VK_CONTROL) & 0x8000))
2264 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
2266 if (!(GetKeyState (VK_MENU) & 0x8000))
2267 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
2270 static int
2271 modifier_set (int vkey)
2273 if (vkey == VK_CAPITAL || vkey == VK_SCROLL)
2274 return (GetKeyState (vkey) & 0x1);
2275 if (!modifiers_recorded)
2276 return (GetKeyState (vkey) & 0x8000);
2278 switch (vkey)
2280 case VK_LCONTROL:
2281 return modifiers[EMACS_LCONTROL];
2282 case VK_RCONTROL:
2283 return modifiers[EMACS_RCONTROL];
2284 case VK_LMENU:
2285 return modifiers[EMACS_LMENU];
2286 case VK_RMENU:
2287 return modifiers[EMACS_RMENU];
2289 return (GetKeyState (vkey) & 0x8000);
2292 /* Convert between the modifier bits W32 uses and the modifier bits
2293 Emacs uses. */
2295 unsigned int
2296 w32_key_to_modifier (int key)
2298 Lisp_Object key_mapping;
2300 switch (key)
2302 case VK_LWIN:
2303 key_mapping = Vw32_lwindow_modifier;
2304 break;
2305 case VK_RWIN:
2306 key_mapping = Vw32_rwindow_modifier;
2307 break;
2308 case VK_APPS:
2309 key_mapping = Vw32_apps_modifier;
2310 break;
2311 case VK_SCROLL:
2312 key_mapping = Vw32_scroll_lock_modifier;
2313 break;
2314 default:
2315 key_mapping = Qnil;
2318 /* NB. This code runs in the input thread, asychronously to the lisp
2319 thread, so we must be careful to ensure access to lisp data is
2320 thread-safe. The following code is safe because the modifier
2321 variable values are updated atomically from lisp and symbols are
2322 not relocated by GC. Also, we don't have to worry about seeing GC
2323 markbits here. */
2324 if (EQ (key_mapping, Qhyper))
2325 return hyper_modifier;
2326 if (EQ (key_mapping, Qsuper))
2327 return super_modifier;
2328 if (EQ (key_mapping, Qmeta))
2329 return meta_modifier;
2330 if (EQ (key_mapping, Qalt))
2331 return alt_modifier;
2332 if (EQ (key_mapping, Qctrl))
2333 return ctrl_modifier;
2334 if (EQ (key_mapping, Qcontrol)) /* synonym for ctrl */
2335 return ctrl_modifier;
2336 if (EQ (key_mapping, Qshift))
2337 return shift_modifier;
2339 /* Don't generate any modifier if not explicitly requested. */
2340 return 0;
2343 unsigned int
2344 w32_get_modifiers ()
2346 return ((modifier_set (VK_SHIFT) ? shift_modifier : 0) |
2347 (modifier_set (VK_CONTROL) ? ctrl_modifier : 0) |
2348 (modifier_set (VK_LWIN) ? w32_key_to_modifier (VK_LWIN) : 0) |
2349 (modifier_set (VK_RWIN) ? w32_key_to_modifier (VK_RWIN) : 0) |
2350 (modifier_set (VK_APPS) ? w32_key_to_modifier (VK_APPS) : 0) |
2351 (modifier_set (VK_SCROLL) ? w32_key_to_modifier (VK_SCROLL) : 0) |
2352 (modifier_set (VK_MENU) ?
2353 ((NILP (Vw32_alt_is_meta)) ? alt_modifier : meta_modifier) : 0));
2356 /* We map the VK_* modifiers into console modifier constants
2357 so that we can use the same routines to handle both console
2358 and window input. */
2360 static int
2361 construct_console_modifiers ()
2363 int mods;
2365 mods = 0;
2366 mods |= (modifier_set (VK_SHIFT)) ? SHIFT_PRESSED : 0;
2367 mods |= (modifier_set (VK_CAPITAL)) ? CAPSLOCK_ON : 0;
2368 mods |= (modifier_set (VK_SCROLL)) ? SCROLLLOCK_ON : 0;
2369 mods |= (modifier_set (VK_NUMLOCK)) ? NUMLOCK_ON : 0;
2370 mods |= (modifier_set (VK_LCONTROL)) ? LEFT_CTRL_PRESSED : 0;
2371 mods |= (modifier_set (VK_RCONTROL)) ? RIGHT_CTRL_PRESSED : 0;
2372 mods |= (modifier_set (VK_LMENU)) ? LEFT_ALT_PRESSED : 0;
2373 mods |= (modifier_set (VK_RMENU)) ? RIGHT_ALT_PRESSED : 0;
2374 mods |= (modifier_set (VK_LWIN)) ? LEFT_WIN_PRESSED : 0;
2375 mods |= (modifier_set (VK_RWIN)) ? RIGHT_WIN_PRESSED : 0;
2376 mods |= (modifier_set (VK_APPS)) ? APPS_PRESSED : 0;
2378 return mods;
2381 static int
2382 w32_get_key_modifiers (unsigned int wparam, unsigned int lparam)
2384 int mods;
2386 /* Convert to emacs modifiers. */
2387 mods = w32_kbd_mods_to_emacs (construct_console_modifiers (), wparam);
2389 return mods;
2392 unsigned int
2393 map_keypad_keys (unsigned int virt_key, unsigned int extended)
2395 if (virt_key < VK_CLEAR || virt_key > VK_DELETE)
2396 return virt_key;
2398 if (virt_key == VK_RETURN)
2399 return (extended ? VK_NUMPAD_ENTER : VK_RETURN);
2401 if (virt_key >= VK_PRIOR && virt_key <= VK_DOWN)
2402 return (!extended ? (VK_NUMPAD_PRIOR + (virt_key - VK_PRIOR)) : virt_key);
2404 if (virt_key == VK_INSERT || virt_key == VK_DELETE)
2405 return (!extended ? (VK_NUMPAD_INSERT + (virt_key - VK_INSERT)) : virt_key);
2407 if (virt_key == VK_CLEAR)
2408 return (!extended ? VK_NUMPAD_CLEAR : virt_key);
2410 return virt_key;
2413 /* List of special key combinations which w32 would normally capture,
2414 but emacs should grab instead. Not directly visible to lisp, to
2415 simplify synchronization. Each item is an integer encoding a virtual
2416 key code and modifier combination to capture. */
2417 Lisp_Object w32_grabbed_keys;
2419 #define HOTKEY(vk,mods) make_number (((vk) & 255) | ((mods) << 8))
2420 #define HOTKEY_ID(k) (XFASTINT (k) & 0xbfff)
2421 #define HOTKEY_VK_CODE(k) (XFASTINT (k) & 255)
2422 #define HOTKEY_MODIFIERS(k) (XFASTINT (k) >> 8)
2424 #define RAW_HOTKEY_ID(k) ((k) & 0xbfff)
2425 #define RAW_HOTKEY_VK_CODE(k) ((k) & 255)
2426 #define RAW_HOTKEY_MODIFIERS(k) ((k) >> 8)
2428 /* Register hot-keys for reserved key combinations when Emacs has
2429 keyboard focus, since this is the only way Emacs can receive key
2430 combinations like Alt-Tab which are used by the system. */
2432 static void
2433 register_hot_keys (hwnd)
2434 HWND hwnd;
2436 Lisp_Object keylist;
2438 /* Use GC_CONSP, since we are called asynchronously. */
2439 for (keylist = w32_grabbed_keys; GC_CONSP (keylist); keylist = XCDR (keylist))
2441 Lisp_Object key = XCAR (keylist);
2443 /* Deleted entries get set to nil. */
2444 if (!INTEGERP (key))
2445 continue;
2447 RegisterHotKey (hwnd, HOTKEY_ID (key),
2448 HOTKEY_MODIFIERS (key), HOTKEY_VK_CODE (key));
2452 static void
2453 unregister_hot_keys (hwnd)
2454 HWND hwnd;
2456 Lisp_Object keylist;
2458 /* Use GC_CONSP, since we are called asynchronously. */
2459 for (keylist = w32_grabbed_keys; GC_CONSP (keylist); keylist = XCDR (keylist))
2461 Lisp_Object key = XCAR (keylist);
2463 if (!INTEGERP (key))
2464 continue;
2466 UnregisterHotKey (hwnd, HOTKEY_ID (key));
2470 /* Main message dispatch loop. */
2472 static void
2473 w32_msg_pump (deferred_msg * msg_buf)
2475 MSG msg;
2476 int result;
2477 HWND focus_window;
2479 msh_mousewheel = RegisterWindowMessage (MSH_MOUSEWHEEL);
2481 while (GetMessage (&msg, NULL, 0, 0))
2483 if (msg.hwnd == NULL)
2485 switch (msg.message)
2487 case WM_NULL:
2488 /* Produced by complete_deferred_msg; just ignore. */
2489 break;
2490 case WM_EMACS_CREATEWINDOW:
2491 w32_createwindow ((struct frame *) msg.wParam);
2492 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2493 abort ();
2494 break;
2495 case WM_EMACS_SETLOCALE:
2496 SetThreadLocale (msg.wParam);
2497 /* Reply is not expected. */
2498 break;
2499 case WM_EMACS_SETKEYBOARDLAYOUT:
2500 result = (int) ActivateKeyboardLayout ((HKL) msg.wParam, 0);
2501 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
2502 result, 0))
2503 abort ();
2504 break;
2505 case WM_EMACS_REGISTER_HOT_KEY:
2506 focus_window = GetFocus ();
2507 if (focus_window != NULL)
2508 RegisterHotKey (focus_window,
2509 RAW_HOTKEY_ID (msg.wParam),
2510 RAW_HOTKEY_MODIFIERS (msg.wParam),
2511 RAW_HOTKEY_VK_CODE (msg.wParam));
2512 /* Reply is not expected. */
2513 break;
2514 case WM_EMACS_UNREGISTER_HOT_KEY:
2515 focus_window = GetFocus ();
2516 if (focus_window != NULL)
2517 UnregisterHotKey (focus_window, RAW_HOTKEY_ID (msg.wParam));
2518 /* Mark item as erased. NB: this code must be
2519 thread-safe. The next line is okay because the cons
2520 cell is never made into garbage and is not relocated by
2521 GC. */
2522 XSETCAR ((Lisp_Object) ((EMACS_INT) msg.lParam), Qnil);
2523 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2524 abort ();
2525 break;
2526 case WM_EMACS_TOGGLE_LOCK_KEY:
2528 int vk_code = (int) msg.wParam;
2529 int cur_state = (GetKeyState (vk_code) & 1);
2530 Lisp_Object new_state = (Lisp_Object) ((EMACS_INT) msg.lParam);
2532 /* NB: This code must be thread-safe. It is safe to
2533 call NILP because symbols are not relocated by GC,
2534 and pointer here is not touched by GC (so the markbit
2535 can't be set). Numbers are safe because they are
2536 immediate values. */
2537 if (NILP (new_state)
2538 || (NUMBERP (new_state)
2539 && ((XUINT (new_state)) & 1) != cur_state))
2541 one_w32_display_info.faked_key = vk_code;
2543 keybd_event ((BYTE) vk_code,
2544 (BYTE) MapVirtualKey (vk_code, 0),
2545 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2546 keybd_event ((BYTE) vk_code,
2547 (BYTE) MapVirtualKey (vk_code, 0),
2548 KEYEVENTF_EXTENDEDKEY | 0, 0);
2549 keybd_event ((BYTE) vk_code,
2550 (BYTE) MapVirtualKey (vk_code, 0),
2551 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2552 cur_state = !cur_state;
2554 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
2555 cur_state, 0))
2556 abort ();
2558 break;
2559 default:
2560 DebPrint (("msg %x not expected by w32_msg_pump\n", msg.message));
2563 else
2565 DispatchMessage (&msg);
2568 /* Exit nested loop when our deferred message has completed. */
2569 if (msg_buf->completed)
2570 break;
2574 deferred_msg * deferred_msg_head;
2576 static deferred_msg *
2577 find_deferred_msg (HWND hwnd, UINT msg)
2579 deferred_msg * item;
2581 /* Don't actually need synchronization for read access, since
2582 modification of single pointer is always atomic. */
2583 /* enter_crit (); */
2585 for (item = deferred_msg_head; item != NULL; item = item->next)
2586 if (item->w32msg.msg.hwnd == hwnd
2587 && item->w32msg.msg.message == msg)
2588 break;
2590 /* leave_crit (); */
2592 return item;
2595 static LRESULT
2596 send_deferred_msg (deferred_msg * msg_buf,
2597 HWND hwnd,
2598 UINT msg,
2599 WPARAM wParam,
2600 LPARAM lParam)
2602 /* Only input thread can send deferred messages. */
2603 if (GetCurrentThreadId () != dwWindowsThreadId)
2604 abort ();
2606 /* It is an error to send a message that is already deferred. */
2607 if (find_deferred_msg (hwnd, msg) != NULL)
2608 abort ();
2610 /* Enforced synchronization is not needed because this is the only
2611 function that alters deferred_msg_head, and the following critical
2612 section is guaranteed to only be serially reentered (since only the
2613 input thread can call us). */
2615 /* enter_crit (); */
2617 msg_buf->completed = 0;
2618 msg_buf->next = deferred_msg_head;
2619 deferred_msg_head = msg_buf;
2620 my_post_msg (&msg_buf->w32msg, hwnd, msg, wParam, lParam);
2622 /* leave_crit (); */
2624 /* Start a new nested message loop to process other messages until
2625 this one is completed. */
2626 w32_msg_pump (msg_buf);
2628 deferred_msg_head = msg_buf->next;
2630 return msg_buf->result;
2633 void
2634 complete_deferred_msg (HWND hwnd, UINT msg, LRESULT result)
2636 deferred_msg * msg_buf = find_deferred_msg (hwnd, msg);
2638 if (msg_buf == NULL)
2639 /* Message may have been cancelled, so don't abort(). */
2640 return;
2642 msg_buf->result = result;
2643 msg_buf->completed = 1;
2645 /* Ensure input thread is woken so it notices the completion. */
2646 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
2649 void
2650 cancel_all_deferred_msgs ()
2652 deferred_msg * item;
2654 /* Don't actually need synchronization for read access, since
2655 modification of single pointer is always atomic. */
2656 /* enter_crit (); */
2658 for (item = deferred_msg_head; item != NULL; item = item->next)
2660 item->result = 0;
2661 item->completed = 1;
2664 /* leave_crit (); */
2666 /* Ensure input thread is woken so it notices the completion. */
2667 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
2670 DWORD
2671 w32_msg_worker (dw)
2672 DWORD dw;
2674 MSG msg;
2675 deferred_msg dummy_buf;
2677 /* Ensure our message queue is created */
2679 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
2681 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2682 abort ();
2684 memset (&dummy_buf, 0, sizeof (dummy_buf));
2685 dummy_buf.w32msg.msg.hwnd = NULL;
2686 dummy_buf.w32msg.msg.message = WM_NULL;
2688 /* This is the inital message loop which should only exit when the
2689 application quits. */
2690 w32_msg_pump (&dummy_buf);
2692 return 0;
2695 static void
2696 post_character_message (hwnd, msg, wParam, lParam, modifiers)
2697 HWND hwnd;
2698 UINT msg;
2699 WPARAM wParam;
2700 LPARAM lParam;
2701 DWORD modifiers;
2704 W32Msg wmsg;
2706 wmsg.dwModifiers = modifiers;
2708 /* Detect quit_char and set quit-flag directly. Note that we
2709 still need to post a message to ensure the main thread will be
2710 woken up if blocked in sys_select(), but we do NOT want to post
2711 the quit_char message itself (because it will usually be as if
2712 the user had typed quit_char twice). Instead, we post a dummy
2713 message that has no particular effect. */
2715 int c = wParam;
2716 if (isalpha (c) && wmsg.dwModifiers == ctrl_modifier)
2717 c = make_ctrl_char (c) & 0377;
2718 if (c == quit_char
2719 || (wmsg.dwModifiers == 0 &&
2720 w32_quit_key && wParam == w32_quit_key))
2722 Vquit_flag = Qt;
2724 /* The choice of message is somewhat arbitrary, as long as
2725 the main thread handler just ignores it. */
2726 msg = WM_NULL;
2728 /* Interrupt any blocking system calls. */
2729 signal_quit ();
2731 /* As a safety precaution, forcibly complete any deferred
2732 messages. This is a kludge, but I don't see any particularly
2733 clean way to handle the situation where a deferred message is
2734 "dropped" in the lisp thread, and will thus never be
2735 completed, eg. by the user trying to activate the menubar
2736 when the lisp thread is busy, and then typing C-g when the
2737 menubar doesn't open promptly (with the result that the
2738 menubar never responds at all because the deferred
2739 WM_INITMENU message is never completed). Another problem
2740 situation is when the lisp thread calls SendMessage (to send
2741 a window manager command) when a message has been deferred;
2742 the lisp thread gets blocked indefinitely waiting for the
2743 deferred message to be completed, which itself is waiting for
2744 the lisp thread to respond.
2746 Note that we don't want to block the input thread waiting for
2747 a reponse from the lisp thread (although that would at least
2748 solve the deadlock problem above), because we want to be able
2749 to receive C-g to interrupt the lisp thread. */
2750 cancel_all_deferred_msgs ();
2754 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2757 /* Main window procedure */
2759 LRESULT CALLBACK
2760 w32_wnd_proc (hwnd, msg, wParam, lParam)
2761 HWND hwnd;
2762 UINT msg;
2763 WPARAM wParam;
2764 LPARAM lParam;
2766 struct frame *f;
2767 struct w32_display_info *dpyinfo = &one_w32_display_info;
2768 W32Msg wmsg;
2769 int windows_translate;
2770 int key;
2772 /* Note that it is okay to call x_window_to_frame, even though we are
2773 not running in the main lisp thread, because frame deletion
2774 requires the lisp thread to synchronize with this thread. Thus, if
2775 a frame struct is returned, it can be used without concern that the
2776 lisp thread might make it disappear while we are using it.
2778 NB. Walking the frame list in this thread is safe (as long as
2779 writes of Lisp_Object slots are atomic, which they are on Windows).
2780 Although delete-frame can destructively modify the frame list while
2781 we are walking it, a garbage collection cannot occur until after
2782 delete-frame has synchronized with this thread.
2784 It is also safe to use functions that make GDI calls, such as
2785 w32_clear_rect, because these functions must obtain a DC handle
2786 from the frame struct using get_frame_dc which is thread-aware. */
2788 switch (msg)
2790 case WM_ERASEBKGND:
2791 f = x_window_to_frame (dpyinfo, hwnd);
2792 if (f)
2794 HDC hdc = get_frame_dc (f);
2795 GetUpdateRect (hwnd, &wmsg.rect, FALSE);
2796 w32_clear_rect (f, hdc, &wmsg.rect);
2797 release_frame_dc (f, hdc);
2799 #if defined (W32_DEBUG_DISPLAY)
2800 DebPrint (("WM_ERASEBKGND (frame %p): erasing %d,%d-%d,%d\n",
2802 wmsg.rect.left, wmsg.rect.top,
2803 wmsg.rect.right, wmsg.rect.bottom));
2804 #endif /* W32_DEBUG_DISPLAY */
2806 return 1;
2807 case WM_PALETTECHANGED:
2808 /* ignore our own changes */
2809 if ((HWND)wParam != hwnd)
2811 f = x_window_to_frame (dpyinfo, hwnd);
2812 if (f)
2813 /* get_frame_dc will realize our palette and force all
2814 frames to be redrawn if needed. */
2815 release_frame_dc (f, get_frame_dc (f));
2817 return 0;
2818 case WM_PAINT:
2820 PAINTSTRUCT paintStruct;
2821 RECT update_rect;
2822 bzero (&update_rect, sizeof (update_rect));
2824 f = x_window_to_frame (dpyinfo, hwnd);
2825 if (f == 0)
2827 DebPrint (("WM_PAINT received for unknown window %p\n", hwnd));
2828 return 0;
2831 /* MSDN Docs say not to call BeginPaint if GetUpdateRect
2832 fails. Apparently this can happen under some
2833 circumstances. */
2834 if (GetUpdateRect (hwnd, &update_rect, FALSE) || !w32_strict_painting)
2836 enter_crit ();
2837 BeginPaint (hwnd, &paintStruct);
2839 /* The rectangles returned by GetUpdateRect and BeginPaint
2840 do not always match. Play it safe by assuming both areas
2841 are invalid. */
2842 UnionRect (&(wmsg.rect), &update_rect, &(paintStruct.rcPaint));
2844 #if defined (W32_DEBUG_DISPLAY)
2845 DebPrint (("WM_PAINT (frame %p): painting %d,%d-%d,%d\n",
2847 wmsg.rect.left, wmsg.rect.top,
2848 wmsg.rect.right, wmsg.rect.bottom));
2849 DebPrint ((" [update region is %d,%d-%d,%d]\n",
2850 update_rect.left, update_rect.top,
2851 update_rect.right, update_rect.bottom));
2852 #endif
2853 EndPaint (hwnd, &paintStruct);
2854 leave_crit ();
2856 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2858 return 0;
2861 /* If GetUpdateRect returns 0 (meaning there is no update
2862 region), assume the whole window needs to be repainted. */
2863 GetClientRect(hwnd, &wmsg.rect);
2864 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2865 return 0;
2868 case WM_INPUTLANGCHANGE:
2869 /* Inform lisp thread of keyboard layout changes. */
2870 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2872 /* Clear dead keys in the keyboard state; for simplicity only
2873 preserve modifier key states. */
2875 int i;
2876 BYTE keystate[256];
2878 GetKeyboardState (keystate);
2879 for (i = 0; i < 256; i++)
2880 if (1
2881 && i != VK_SHIFT
2882 && i != VK_LSHIFT
2883 && i != VK_RSHIFT
2884 && i != VK_CAPITAL
2885 && i != VK_NUMLOCK
2886 && i != VK_SCROLL
2887 && i != VK_CONTROL
2888 && i != VK_LCONTROL
2889 && i != VK_RCONTROL
2890 && i != VK_MENU
2891 && i != VK_LMENU
2892 && i != VK_RMENU
2893 && i != VK_LWIN
2894 && i != VK_RWIN)
2895 keystate[i] = 0;
2896 SetKeyboardState (keystate);
2898 goto dflt;
2900 case WM_HOTKEY:
2901 /* Synchronize hot keys with normal input. */
2902 PostMessage (hwnd, WM_KEYDOWN, HIWORD (lParam), 0);
2903 return (0);
2905 case WM_KEYUP:
2906 case WM_SYSKEYUP:
2907 record_keyup (wParam, lParam);
2908 goto dflt;
2910 case WM_KEYDOWN:
2911 case WM_SYSKEYDOWN:
2912 /* Ignore keystrokes we fake ourself; see below. */
2913 if (dpyinfo->faked_key == wParam)
2915 dpyinfo->faked_key = 0;
2916 /* Make sure TranslateMessage sees them though (as long as
2917 they don't produce WM_CHAR messages). This ensures that
2918 indicator lights are toggled promptly on Windows 9x, for
2919 example. */
2920 if (lispy_function_keys[wParam] != 0)
2922 windows_translate = 1;
2923 goto translate;
2925 return 0;
2928 /* Synchronize modifiers with current keystroke. */
2929 sync_modifiers ();
2930 record_keydown (wParam, lParam);
2931 wParam = map_keypad_keys (wParam, (lParam & 0x1000000L) != 0);
2933 windows_translate = 0;
2935 switch (wParam)
2937 case VK_LWIN:
2938 if (NILP (Vw32_pass_lwindow_to_system))
2940 /* Prevent system from acting on keyup (which opens the
2941 Start menu if no other key was pressed) by simulating a
2942 press of Space which we will ignore. */
2943 if (GetAsyncKeyState (wParam) & 1)
2945 if (NUMBERP (Vw32_phantom_key_code))
2946 key = XUINT (Vw32_phantom_key_code) & 255;
2947 else
2948 key = VK_SPACE;
2949 dpyinfo->faked_key = key;
2950 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
2953 if (!NILP (Vw32_lwindow_modifier))
2954 return 0;
2955 break;
2956 case VK_RWIN:
2957 if (NILP (Vw32_pass_rwindow_to_system))
2959 if (GetAsyncKeyState (wParam) & 1)
2961 if (NUMBERP (Vw32_phantom_key_code))
2962 key = XUINT (Vw32_phantom_key_code) & 255;
2963 else
2964 key = VK_SPACE;
2965 dpyinfo->faked_key = key;
2966 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
2969 if (!NILP (Vw32_rwindow_modifier))
2970 return 0;
2971 break;
2972 case VK_APPS:
2973 if (!NILP (Vw32_apps_modifier))
2974 return 0;
2975 break;
2976 case VK_MENU:
2977 if (NILP (Vw32_pass_alt_to_system))
2978 /* Prevent DefWindowProc from activating the menu bar if an
2979 Alt key is pressed and released by itself. */
2980 return 0;
2981 windows_translate = 1;
2982 break;
2983 case VK_CAPITAL:
2984 /* Decide whether to treat as modifier or function key. */
2985 if (NILP (Vw32_enable_caps_lock))
2986 goto disable_lock_key;
2987 windows_translate = 1;
2988 break;
2989 case VK_NUMLOCK:
2990 /* Decide whether to treat as modifier or function key. */
2991 if (NILP (Vw32_enable_num_lock))
2992 goto disable_lock_key;
2993 windows_translate = 1;
2994 break;
2995 case VK_SCROLL:
2996 /* Decide whether to treat as modifier or function key. */
2997 if (NILP (Vw32_scroll_lock_modifier))
2998 goto disable_lock_key;
2999 windows_translate = 1;
3000 break;
3001 disable_lock_key:
3002 /* Ensure the appropriate lock key state (and indicator light)
3003 remains in the same state. We do this by faking another
3004 press of the relevant key. Apparently, this really is the
3005 only way to toggle the state of the indicator lights. */
3006 dpyinfo->faked_key = wParam;
3007 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
3008 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
3009 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
3010 KEYEVENTF_EXTENDEDKEY | 0, 0);
3011 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
3012 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
3013 /* Ensure indicator lights are updated promptly on Windows 9x
3014 (TranslateMessage apparently does this), after forwarding
3015 input event. */
3016 post_character_message (hwnd, msg, wParam, lParam,
3017 w32_get_key_modifiers (wParam, lParam));
3018 windows_translate = 1;
3019 break;
3020 case VK_CONTROL:
3021 case VK_SHIFT:
3022 case VK_PROCESSKEY: /* Generated by IME. */
3023 windows_translate = 1;
3024 break;
3025 case VK_CANCEL:
3026 /* Windows maps Ctrl-Pause (aka Ctrl-Break) into VK_CANCEL,
3027 which is confusing for purposes of key binding; convert
3028 VK_CANCEL events into VK_PAUSE events. */
3029 wParam = VK_PAUSE;
3030 break;
3031 case VK_PAUSE:
3032 /* Windows maps Ctrl-NumLock into VK_PAUSE, which is confusing
3033 for purposes of key binding; convert these back into
3034 VK_NUMLOCK events, at least when we want to see NumLock key
3035 presses. (Note that there is never any possibility that
3036 VK_PAUSE with Ctrl really is C-Pause as per above.) */
3037 if (NILP (Vw32_enable_num_lock) && modifier_set (VK_CONTROL))
3038 wParam = VK_NUMLOCK;
3039 break;
3040 default:
3041 /* If not defined as a function key, change it to a WM_CHAR message. */
3042 if (lispy_function_keys[wParam] == 0)
3044 DWORD modifiers = construct_console_modifiers ();
3046 if (!NILP (Vw32_recognize_altgr)
3047 && modifier_set (VK_LCONTROL) && modifier_set (VK_RMENU))
3049 /* Always let TranslateMessage handle AltGr key chords;
3050 for some reason, ToAscii doesn't always process AltGr
3051 chords correctly. */
3052 windows_translate = 1;
3054 else if ((modifiers & (~SHIFT_PRESSED & ~CAPSLOCK_ON)) != 0)
3056 /* Handle key chords including any modifiers other
3057 than shift directly, in order to preserve as much
3058 modifier information as possible. */
3059 if ('A' <= wParam && wParam <= 'Z')
3061 /* Don't translate modified alphabetic keystrokes,
3062 so the user doesn't need to constantly switch
3063 layout to type control or meta keystrokes when
3064 the normal layout translates alphabetic
3065 characters to non-ascii characters. */
3066 if (!modifier_set (VK_SHIFT))
3067 wParam += ('a' - 'A');
3068 msg = WM_CHAR;
3070 else
3072 /* Try to handle other keystrokes by determining the
3073 base character (ie. translating the base key plus
3074 shift modifier). */
3075 int add;
3076 int isdead = 0;
3077 KEY_EVENT_RECORD key;
3079 key.bKeyDown = TRUE;
3080 key.wRepeatCount = 1;
3081 key.wVirtualKeyCode = wParam;
3082 key.wVirtualScanCode = (lParam & 0xFF0000) >> 16;
3083 key.uChar.AsciiChar = 0;
3084 key.dwControlKeyState = modifiers;
3086 add = w32_kbd_patch_key (&key);
3087 /* 0 means an unrecognised keycode, negative means
3088 dead key. Ignore both. */
3089 while (--add >= 0)
3091 /* Forward asciified character sequence. */
3092 post_character_message
3093 (hwnd, WM_CHAR, key.uChar.AsciiChar, lParam,
3094 w32_get_key_modifiers (wParam, lParam));
3095 w32_kbd_patch_key (&key);
3097 return 0;
3100 else
3102 /* Let TranslateMessage handle everything else. */
3103 windows_translate = 1;
3108 translate:
3109 if (windows_translate)
3111 MSG windows_msg = { hwnd, msg, wParam, lParam, 0, {0,0} };
3113 windows_msg.time = GetMessageTime ();
3114 TranslateMessage (&windows_msg);
3115 goto dflt;
3118 /* Fall through */
3120 case WM_SYSCHAR:
3121 case WM_CHAR:
3122 post_character_message (hwnd, msg, wParam, lParam,
3123 w32_get_key_modifiers (wParam, lParam));
3124 break;
3126 /* Simulate middle mouse button events when left and right buttons
3127 are used together, but only if user has two button mouse. */
3128 case WM_LBUTTONDOWN:
3129 case WM_RBUTTONDOWN:
3130 if (w32_num_mouse_buttons > 2)
3131 goto handle_plain_button;
3134 int this = (msg == WM_LBUTTONDOWN) ? LMOUSE : RMOUSE;
3135 int other = (msg == WM_LBUTTONDOWN) ? RMOUSE : LMOUSE;
3137 if (button_state & this)
3138 return 0;
3140 if (button_state == 0)
3141 SetCapture (hwnd);
3143 button_state |= this;
3145 if (button_state & other)
3147 if (mouse_button_timer)
3149 KillTimer (hwnd, mouse_button_timer);
3150 mouse_button_timer = 0;
3152 /* Generate middle mouse event instead. */
3153 msg = WM_MBUTTONDOWN;
3154 button_state |= MMOUSE;
3156 else if (button_state & MMOUSE)
3158 /* Ignore button event if we've already generated a
3159 middle mouse down event. This happens if the
3160 user releases and press one of the two buttons
3161 after we've faked a middle mouse event. */
3162 return 0;
3164 else
3166 /* Flush out saved message. */
3167 post_msg (&saved_mouse_button_msg);
3169 wmsg.dwModifiers = w32_get_modifiers ();
3170 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3172 /* Clear message buffer. */
3173 saved_mouse_button_msg.msg.hwnd = 0;
3175 else
3177 /* Hold onto message for now. */
3178 mouse_button_timer =
3179 SetTimer (hwnd, MOUSE_BUTTON_ID,
3180 w32_mouse_button_tolerance, NULL);
3181 saved_mouse_button_msg.msg.hwnd = hwnd;
3182 saved_mouse_button_msg.msg.message = msg;
3183 saved_mouse_button_msg.msg.wParam = wParam;
3184 saved_mouse_button_msg.msg.lParam = lParam;
3185 saved_mouse_button_msg.msg.time = GetMessageTime ();
3186 saved_mouse_button_msg.dwModifiers = w32_get_modifiers ();
3189 return 0;
3191 case WM_LBUTTONUP:
3192 case WM_RBUTTONUP:
3193 if (w32_num_mouse_buttons > 2)
3194 goto handle_plain_button;
3197 int this = (msg == WM_LBUTTONUP) ? LMOUSE : RMOUSE;
3198 int other = (msg == WM_LBUTTONUP) ? RMOUSE : LMOUSE;
3200 if ((button_state & this) == 0)
3201 return 0;
3203 button_state &= ~this;
3205 if (button_state & MMOUSE)
3207 /* Only generate event when second button is released. */
3208 if ((button_state & other) == 0)
3210 msg = WM_MBUTTONUP;
3211 button_state &= ~MMOUSE;
3213 if (button_state) abort ();
3215 else
3216 return 0;
3218 else
3220 /* Flush out saved message if necessary. */
3221 if (saved_mouse_button_msg.msg.hwnd)
3223 post_msg (&saved_mouse_button_msg);
3226 wmsg.dwModifiers = w32_get_modifiers ();
3227 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3229 /* Always clear message buffer and cancel timer. */
3230 saved_mouse_button_msg.msg.hwnd = 0;
3231 KillTimer (hwnd, mouse_button_timer);
3232 mouse_button_timer = 0;
3234 if (button_state == 0)
3235 ReleaseCapture ();
3237 return 0;
3239 case WM_XBUTTONDOWN:
3240 case WM_XBUTTONUP:
3241 if (w32_pass_extra_mouse_buttons_to_system)
3242 goto dflt;
3243 /* else fall through and process them. */
3244 case WM_MBUTTONDOWN:
3245 case WM_MBUTTONUP:
3246 handle_plain_button:
3248 BOOL up;
3249 int button;
3251 if (parse_button (msg, HIWORD (wParam), &button, &up))
3253 if (up) ReleaseCapture ();
3254 else SetCapture (hwnd);
3255 button = (button == 0) ? LMOUSE :
3256 ((button == 1) ? MMOUSE : RMOUSE);
3257 if (up)
3258 button_state &= ~button;
3259 else
3260 button_state |= button;
3264 wmsg.dwModifiers = w32_get_modifiers ();
3265 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3267 /* Need to return true for XBUTTON messages, false for others,
3268 to indicate that we processed the message. */
3269 return (msg == WM_XBUTTONDOWN || msg == WM_XBUTTONUP);
3271 case WM_MOUSEMOVE:
3272 /* If the mouse has just moved into the frame, start tracking
3273 it, so we will be notified when it leaves the frame. Mouse
3274 tracking only works under W98 and NT4 and later. On earlier
3275 versions, there is no way of telling when the mouse leaves the
3276 frame, so we just have to put up with help-echo and mouse
3277 highlighting remaining while the frame is not active. */
3278 if (track_mouse_event_fn && !track_mouse_window)
3280 TRACKMOUSEEVENT tme;
3281 tme.cbSize = sizeof (tme);
3282 tme.dwFlags = TME_LEAVE;
3283 tme.hwndTrack = hwnd;
3285 track_mouse_event_fn (&tme);
3286 track_mouse_window = hwnd;
3288 case WM_VSCROLL:
3289 if (w32_mouse_move_interval <= 0
3290 || (msg == WM_MOUSEMOVE && button_state == 0))
3292 wmsg.dwModifiers = w32_get_modifiers ();
3293 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3294 return 0;
3297 /* Hang onto mouse move and scroll messages for a bit, to avoid
3298 sending such events to Emacs faster than it can process them.
3299 If we get more events before the timer from the first message
3300 expires, we just replace the first message. */
3302 if (saved_mouse_move_msg.msg.hwnd == 0)
3303 mouse_move_timer =
3304 SetTimer (hwnd, MOUSE_MOVE_ID,
3305 w32_mouse_move_interval, NULL);
3307 /* Hold onto message for now. */
3308 saved_mouse_move_msg.msg.hwnd = hwnd;
3309 saved_mouse_move_msg.msg.message = msg;
3310 saved_mouse_move_msg.msg.wParam = wParam;
3311 saved_mouse_move_msg.msg.lParam = lParam;
3312 saved_mouse_move_msg.msg.time = GetMessageTime ();
3313 saved_mouse_move_msg.dwModifiers = w32_get_modifiers ();
3315 return 0;
3317 case WM_MOUSEWHEEL:
3318 wmsg.dwModifiers = w32_get_modifiers ();
3319 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3320 return 0;
3322 case WM_DROPFILES:
3323 wmsg.dwModifiers = w32_get_modifiers ();
3324 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3325 return 0;
3327 case WM_TIMER:
3328 /* Flush out saved messages if necessary. */
3329 if (wParam == mouse_button_timer)
3331 if (saved_mouse_button_msg.msg.hwnd)
3333 post_msg (&saved_mouse_button_msg);
3334 saved_mouse_button_msg.msg.hwnd = 0;
3336 KillTimer (hwnd, mouse_button_timer);
3337 mouse_button_timer = 0;
3339 else if (wParam == mouse_move_timer)
3341 if (saved_mouse_move_msg.msg.hwnd)
3343 post_msg (&saved_mouse_move_msg);
3344 saved_mouse_move_msg.msg.hwnd = 0;
3346 KillTimer (hwnd, mouse_move_timer);
3347 mouse_move_timer = 0;
3349 else if (wParam == menu_free_timer)
3351 KillTimer (hwnd, menu_free_timer);
3352 menu_free_timer = 0;
3353 f = x_window_to_frame (dpyinfo, hwnd);
3354 if (!f->output_data.w32->menu_command_in_progress)
3356 /* Free memory used by owner-drawn and help-echo strings. */
3357 w32_free_menu_strings (hwnd);
3358 f->output_data.w32->menubar_active = 0;
3361 return 0;
3363 case WM_NCACTIVATE:
3364 /* Windows doesn't send us focus messages when putting up and
3365 taking down a system popup dialog as for Ctrl-Alt-Del on Windows 95.
3366 The only indication we get that something happened is receiving
3367 this message afterwards. So this is a good time to reset our
3368 keyboard modifiers' state. */
3369 reset_modifiers ();
3370 goto dflt;
3372 case WM_INITMENU:
3373 button_state = 0;
3374 ReleaseCapture ();
3375 /* We must ensure menu bar is fully constructed and up to date
3376 before allowing user interaction with it. To achieve this
3377 we send this message to the lisp thread and wait for a
3378 reply (whose value is not actually needed) to indicate that
3379 the menu bar is now ready for use, so we can now return.
3381 To remain responsive in the meantime, we enter a nested message
3382 loop that can process all other messages.
3384 However, we skip all this if the message results from calling
3385 TrackPopupMenu - in fact, we must NOT attempt to send the lisp
3386 thread a message because it is blocked on us at this point. We
3387 set menubar_active before calling TrackPopupMenu to indicate
3388 this (there is no possibility of confusion with real menubar
3389 being active). */
3391 f = x_window_to_frame (dpyinfo, hwnd);
3392 if (f
3393 && (f->output_data.w32->menubar_active
3394 /* We can receive this message even in the absence of a
3395 menubar (ie. when the system menu is activated) - in this
3396 case we do NOT want to forward the message, otherwise it
3397 will cause the menubar to suddenly appear when the user
3398 had requested it to be turned off! */
3399 || f->output_data.w32->menubar_widget == NULL))
3400 return 0;
3403 deferred_msg msg_buf;
3405 /* Detect if message has already been deferred; in this case
3406 we cannot return any sensible value to ignore this. */
3407 if (find_deferred_msg (hwnd, msg) != NULL)
3408 abort ();
3410 return send_deferred_msg (&msg_buf, hwnd, msg, wParam, lParam);
3413 case WM_EXITMENULOOP:
3414 f = x_window_to_frame (dpyinfo, hwnd);
3416 /* If a menu command is not already in progress, check again
3417 after a short delay, since Windows often (always?) sends the
3418 WM_EXITMENULOOP before the corresponding WM_COMMAND message. */
3419 if (f && !f->output_data.w32->menu_command_in_progress)
3420 menu_free_timer = SetTimer (hwnd, MENU_FREE_ID, MENU_FREE_DELAY, NULL);
3421 goto dflt;
3423 case WM_MENUSELECT:
3424 /* Direct handling of help_echo in menus. Should be safe now
3425 that we generate the help_echo by placing a help event in the
3426 keyboard buffer. */
3428 HMENU menu = (HMENU) lParam;
3429 UINT menu_item = (UINT) LOWORD (wParam);
3430 UINT flags = (UINT) HIWORD (wParam);
3432 w32_menu_display_help (hwnd, menu, menu_item, flags);
3434 return 0;
3436 case WM_MEASUREITEM:
3437 f = x_window_to_frame (dpyinfo, hwnd);
3438 if (f)
3440 MEASUREITEMSTRUCT * pMis = (MEASUREITEMSTRUCT *) lParam;
3442 if (pMis->CtlType == ODT_MENU)
3444 /* Work out dimensions for popup menu titles. */
3445 char * title = (char *) pMis->itemData;
3446 HDC hdc = GetDC (hwnd);
3447 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
3448 LOGFONT menu_logfont;
3449 HFONT old_font;
3450 SIZE size;
3452 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
3453 menu_logfont.lfWeight = FW_BOLD;
3454 menu_font = CreateFontIndirect (&menu_logfont);
3455 old_font = SelectObject (hdc, menu_font);
3457 pMis->itemHeight = GetSystemMetrics (SM_CYMENUSIZE);
3458 if (title)
3460 GetTextExtentPoint32 (hdc, title, strlen (title), &size);
3461 pMis->itemWidth = size.cx;
3462 if (pMis->itemHeight < size.cy)
3463 pMis->itemHeight = size.cy;
3465 else
3466 pMis->itemWidth = 0;
3468 SelectObject (hdc, old_font);
3469 DeleteObject (menu_font);
3470 ReleaseDC (hwnd, hdc);
3471 return TRUE;
3474 return 0;
3476 case WM_DRAWITEM:
3477 f = x_window_to_frame (dpyinfo, hwnd);
3478 if (f)
3480 DRAWITEMSTRUCT * pDis = (DRAWITEMSTRUCT *) lParam;
3482 if (pDis->CtlType == ODT_MENU)
3484 /* Draw popup menu title. */
3485 char * title = (char *) pDis->itemData;
3486 if (title)
3488 HDC hdc = pDis->hDC;
3489 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
3490 LOGFONT menu_logfont;
3491 HFONT old_font;
3493 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
3494 menu_logfont.lfWeight = FW_BOLD;
3495 menu_font = CreateFontIndirect (&menu_logfont);
3496 old_font = SelectObject (hdc, menu_font);
3498 /* Always draw title as if not selected. */
3499 ExtTextOut (hdc,
3500 pDis->rcItem.left
3501 + GetSystemMetrics (SM_CXMENUCHECK),
3502 pDis->rcItem.top,
3503 ETO_OPAQUE, &pDis->rcItem,
3504 title, strlen (title), NULL);
3506 SelectObject (hdc, old_font);
3507 DeleteObject (menu_font);
3509 return TRUE;
3512 return 0;
3514 #if 0
3515 /* Still not right - can't distinguish between clicks in the
3516 client area of the frame from clicks forwarded from the scroll
3517 bars - may have to hook WM_NCHITTEST to remember the mouse
3518 position and then check if it is in the client area ourselves. */
3519 case WM_MOUSEACTIVATE:
3520 /* Discard the mouse click that activates a frame, allowing the
3521 user to click anywhere without changing point (or worse!).
3522 Don't eat mouse clicks on scrollbars though!! */
3523 if (LOWORD (lParam) == HTCLIENT )
3524 return MA_ACTIVATEANDEAT;
3525 goto dflt;
3526 #endif
3528 case WM_MOUSELEAVE:
3529 /* No longer tracking mouse. */
3530 track_mouse_window = NULL;
3532 case WM_ACTIVATEAPP:
3533 case WM_ACTIVATE:
3534 case WM_WINDOWPOSCHANGED:
3535 case WM_SHOWWINDOW:
3536 /* Inform lisp thread that a frame might have just been obscured
3537 or exposed, so should recheck visibility of all frames. */
3538 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3539 goto dflt;
3541 case WM_SETFOCUS:
3542 dpyinfo->faked_key = 0;
3543 reset_modifiers ();
3544 register_hot_keys (hwnd);
3545 goto command;
3546 case WM_KILLFOCUS:
3547 unregister_hot_keys (hwnd);
3548 button_state = 0;
3549 ReleaseCapture ();
3550 /* Relinquish the system caret. */
3551 if (w32_system_caret_hwnd)
3553 w32_visible_system_caret_hwnd = NULL;
3554 w32_system_caret_hwnd = NULL;
3555 DestroyCaret ();
3557 goto command;
3558 case WM_COMMAND:
3559 f = x_window_to_frame (dpyinfo, hwnd);
3560 if (f && HIWORD (wParam) == 0)
3562 f->output_data.w32->menu_command_in_progress = 1;
3563 if (menu_free_timer)
3565 KillTimer (hwnd, menu_free_timer);
3566 menu_free_timer = 0;
3569 case WM_MOVE:
3570 case WM_SIZE:
3571 command:
3572 wmsg.dwModifiers = w32_get_modifiers ();
3573 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3574 goto dflt;
3576 case WM_CLOSE:
3577 wmsg.dwModifiers = w32_get_modifiers ();
3578 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3579 return 0;
3581 case WM_WINDOWPOSCHANGING:
3582 /* Don't restrict the sizing of tip frames. */
3583 if (hwnd == tip_window)
3584 return 0;
3586 WINDOWPLACEMENT wp;
3587 LPWINDOWPOS lppos = (WINDOWPOS *) lParam;
3589 wp.length = sizeof (WINDOWPLACEMENT);
3590 GetWindowPlacement (hwnd, &wp);
3592 if (wp.showCmd != SW_SHOWMINIMIZED && (lppos->flags & SWP_NOSIZE) == 0)
3594 RECT rect;
3595 int wdiff;
3596 int hdiff;
3597 DWORD font_width;
3598 DWORD line_height;
3599 DWORD internal_border;
3600 DWORD scrollbar_extra;
3601 RECT wr;
3603 wp.length = sizeof(wp);
3604 GetWindowRect (hwnd, &wr);
3606 enter_crit ();
3608 font_width = GetWindowLong (hwnd, WND_FONTWIDTH_INDEX);
3609 line_height = GetWindowLong (hwnd, WND_LINEHEIGHT_INDEX);
3610 internal_border = GetWindowLong (hwnd, WND_BORDER_INDEX);
3611 scrollbar_extra = GetWindowLong (hwnd, WND_SCROLLBAR_INDEX);
3613 leave_crit ();
3615 memset (&rect, 0, sizeof (rect));
3616 AdjustWindowRect (&rect, GetWindowLong (hwnd, GWL_STYLE),
3617 GetMenu (hwnd) != NULL);
3619 /* Force width and height of client area to be exact
3620 multiples of the character cell dimensions. */
3621 wdiff = (lppos->cx - (rect.right - rect.left)
3622 - 2 * internal_border - scrollbar_extra)
3623 % font_width;
3624 hdiff = (lppos->cy - (rect.bottom - rect.top)
3625 - 2 * internal_border)
3626 % line_height;
3628 if (wdiff || hdiff)
3630 /* For right/bottom sizing we can just fix the sizes.
3631 However for top/left sizing we will need to fix the X
3632 and Y positions as well. */
3634 lppos->cx -= wdiff;
3635 lppos->cy -= hdiff;
3637 if (wp.showCmd != SW_SHOWMAXIMIZED
3638 && (lppos->flags & SWP_NOMOVE) == 0)
3640 if (lppos->x != wr.left || lppos->y != wr.top)
3642 lppos->x += wdiff;
3643 lppos->y += hdiff;
3645 else
3647 lppos->flags |= SWP_NOMOVE;
3651 return 0;
3656 goto dflt;
3658 case WM_GETMINMAXINFO:
3659 /* Hack to correct bug that allows Emacs frames to be resized
3660 below the Minimum Tracking Size. */
3661 ((LPMINMAXINFO) lParam)->ptMinTrackSize.y++;
3662 /* Hack to allow resizing the Emacs frame above the screen size.
3663 Note that Windows 9x limits coordinates to 16-bits. */
3664 ((LPMINMAXINFO) lParam)->ptMaxTrackSize.x = 32767;
3665 ((LPMINMAXINFO) lParam)->ptMaxTrackSize.y = 32767;
3666 return 0;
3668 case WM_SETCURSOR:
3669 if (LOWORD (lParam) == HTCLIENT)
3670 return 0;
3672 goto dflt;
3674 case WM_EMACS_SETCURSOR:
3676 Cursor cursor = (Cursor) wParam;
3677 if (cursor)
3678 SetCursor (cursor);
3679 return 0;
3682 case WM_EMACS_CREATESCROLLBAR:
3683 return (LRESULT) w32_createscrollbar ((struct frame *) wParam,
3684 (struct scroll_bar *) lParam);
3686 case WM_EMACS_SHOWWINDOW:
3687 return ShowWindow ((HWND) wParam, (WPARAM) lParam);
3689 case WM_EMACS_SETFOREGROUND:
3691 HWND foreground_window;
3692 DWORD foreground_thread, retval;
3694 /* On NT 5.0, and apparently Windows 98, it is necessary to
3695 attach to the thread that currently has focus in order to
3696 pull the focus away from it. */
3697 foreground_window = GetForegroundWindow ();
3698 foreground_thread = GetWindowThreadProcessId (foreground_window, NULL);
3699 if (!foreground_window
3700 || foreground_thread == GetCurrentThreadId ()
3701 || !AttachThreadInput (GetCurrentThreadId (),
3702 foreground_thread, TRUE))
3703 foreground_thread = 0;
3705 retval = SetForegroundWindow ((HWND) wParam);
3707 /* Detach from the previous foreground thread. */
3708 if (foreground_thread)
3709 AttachThreadInput (GetCurrentThreadId (),
3710 foreground_thread, FALSE);
3712 return retval;
3715 case WM_EMACS_SETWINDOWPOS:
3717 WINDOWPOS * pos = (WINDOWPOS *) wParam;
3718 return SetWindowPos (hwnd, pos->hwndInsertAfter,
3719 pos->x, pos->y, pos->cx, pos->cy, pos->flags);
3722 case WM_EMACS_DESTROYWINDOW:
3723 DragAcceptFiles ((HWND) wParam, FALSE);
3724 return DestroyWindow ((HWND) wParam);
3726 case WM_EMACS_HIDE_CARET:
3727 return HideCaret (hwnd);
3729 case WM_EMACS_SHOW_CARET:
3730 return ShowCaret (hwnd);
3732 case WM_EMACS_DESTROY_CARET:
3733 w32_system_caret_hwnd = NULL;
3734 w32_visible_system_caret_hwnd = NULL;
3735 return DestroyCaret ();
3737 case WM_EMACS_TRACK_CARET:
3738 /* If there is currently no system caret, create one. */
3739 if (w32_system_caret_hwnd == NULL)
3741 /* Use the default caret width, and avoid changing it
3742 unneccesarily, as it confuses screen reader software. */
3743 w32_system_caret_hwnd = hwnd;
3744 CreateCaret (hwnd, NULL, 0,
3745 w32_system_caret_height);
3748 if (!SetCaretPos (w32_system_caret_x, w32_system_caret_y))
3749 return 0;
3750 /* Ensure visible caret gets turned on when requested. */
3751 else if (w32_use_visible_system_caret
3752 && w32_visible_system_caret_hwnd != hwnd)
3754 w32_visible_system_caret_hwnd = hwnd;
3755 return ShowCaret (hwnd);
3757 /* Ensure visible caret gets turned off when requested. */
3758 else if (!w32_use_visible_system_caret
3759 && w32_visible_system_caret_hwnd)
3761 w32_visible_system_caret_hwnd = NULL;
3762 return HideCaret (hwnd);
3764 else
3765 return 1;
3767 case WM_EMACS_TRACKPOPUPMENU:
3769 UINT flags;
3770 POINT *pos;
3771 int retval;
3772 pos = (POINT *)lParam;
3773 flags = TPM_CENTERALIGN;
3774 if (button_state & LMOUSE)
3775 flags |= TPM_LEFTBUTTON;
3776 else if (button_state & RMOUSE)
3777 flags |= TPM_RIGHTBUTTON;
3779 /* Remember we did a SetCapture on the initial mouse down event,
3780 so for safety, we make sure the capture is cancelled now. */
3781 ReleaseCapture ();
3782 button_state = 0;
3784 /* Use menubar_active to indicate that WM_INITMENU is from
3785 TrackPopupMenu below, and should be ignored. */
3786 f = x_window_to_frame (dpyinfo, hwnd);
3787 if (f)
3788 f->output_data.w32->menubar_active = 1;
3790 if (TrackPopupMenu ((HMENU)wParam, flags, pos->x, pos->y,
3791 0, hwnd, NULL))
3793 MSG amsg;
3794 /* Eat any mouse messages during popupmenu */
3795 while (PeekMessage (&amsg, hwnd, WM_MOUSEFIRST, WM_MOUSELAST,
3796 PM_REMOVE));
3797 /* Get the menu selection, if any */
3798 if (PeekMessage (&amsg, hwnd, WM_COMMAND, WM_COMMAND, PM_REMOVE))
3800 retval = LOWORD (amsg.wParam);
3802 else
3804 retval = 0;
3807 else
3809 retval = -1;
3812 return retval;
3815 default:
3816 /* Check for messages registered at runtime. */
3817 if (msg == msh_mousewheel)
3819 wmsg.dwModifiers = w32_get_modifiers ();
3820 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3821 return 0;
3824 dflt:
3825 return DefWindowProc (hwnd, msg, wParam, lParam);
3829 /* The most common default return code for handled messages is 0. */
3830 return 0;
3833 void
3834 my_create_window (f)
3835 struct frame * f;
3837 MSG msg;
3839 if (!PostThreadMessage (dwWindowsThreadId, WM_EMACS_CREATEWINDOW, (WPARAM)f, 0))
3840 abort ();
3841 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
3845 /* Create a tooltip window. Unlike my_create_window, we do not do this
3846 indirectly via the Window thread, as we do not need to process Window
3847 messages for the tooltip. Creating tooltips indirectly also creates
3848 deadlocks when tooltips are created for menu items. */
3849 void
3850 my_create_tip_window (f)
3851 struct frame *f;
3853 RECT rect;
3855 rect.left = rect.top = 0;
3856 rect.right = FRAME_PIXEL_WIDTH (f);
3857 rect.bottom = FRAME_PIXEL_HEIGHT (f);
3859 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
3860 FRAME_EXTERNAL_MENU_BAR (f));
3862 tip_window = FRAME_W32_WINDOW (f)
3863 = CreateWindow (EMACS_CLASS,
3864 f->namebuf,
3865 f->output_data.w32->dwStyle,
3866 f->left_pos,
3867 f->top_pos,
3868 rect.right - rect.left,
3869 rect.bottom - rect.top,
3870 FRAME_W32_WINDOW (SELECTED_FRAME ()), /* owner */
3871 NULL,
3872 hinst,
3873 NULL);
3875 if (tip_window)
3877 SetWindowLong (tip_window, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
3878 SetWindowLong (tip_window, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
3879 SetWindowLong (tip_window, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
3880 SetWindowLong (tip_window, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
3882 /* Tip frames have no scrollbars. */
3883 SetWindowLong (tip_window, WND_SCROLLBAR_INDEX, 0);
3885 /* Do this to discard the default setting specified by our parent. */
3886 ShowWindow (tip_window, SW_HIDE);
3891 /* Create and set up the w32 window for frame F. */
3893 static void
3894 w32_window (f, window_prompting, minibuffer_only)
3895 struct frame *f;
3896 long window_prompting;
3897 int minibuffer_only;
3899 BLOCK_INPUT;
3901 /* Use the resource name as the top-level window name
3902 for looking up resources. Make a non-Lisp copy
3903 for the window manager, so GC relocation won't bother it.
3905 Elsewhere we specify the window name for the window manager. */
3908 char *str = (char *) SDATA (Vx_resource_name);
3909 f->namebuf = (char *) xmalloc (strlen (str) + 1);
3910 strcpy (f->namebuf, str);
3913 my_create_window (f);
3915 validate_x_resource_name ();
3917 /* x_set_name normally ignores requests to set the name if the
3918 requested name is the same as the current name. This is the one
3919 place where that assumption isn't correct; f->name is set, but
3920 the server hasn't been told. */
3922 Lisp_Object name;
3923 int explicit = f->explicit_name;
3925 f->explicit_name = 0;
3926 name = f->name;
3927 f->name = Qnil;
3928 x_set_name (f, name, explicit);
3931 UNBLOCK_INPUT;
3933 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
3934 initialize_frame_menubar (f);
3936 if (FRAME_W32_WINDOW (f) == 0)
3937 error ("Unable to create window");
3940 /* Handle the icon stuff for this window. Perhaps later we might
3941 want an x_set_icon_position which can be called interactively as
3942 well. */
3944 static void
3945 x_icon (f, parms)
3946 struct frame *f;
3947 Lisp_Object parms;
3949 Lisp_Object icon_x, icon_y;
3951 /* Set the position of the icon. Note that Windows 95 groups all
3952 icons in the tray. */
3953 icon_x = w32_get_arg (parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
3954 icon_y = w32_get_arg (parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
3955 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
3957 CHECK_NUMBER (icon_x);
3958 CHECK_NUMBER (icon_y);
3960 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
3961 error ("Both left and top icon corners of icon must be specified");
3963 BLOCK_INPUT;
3965 if (! EQ (icon_x, Qunbound))
3966 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
3968 #if 0 /* TODO */
3969 /* Start up iconic or window? */
3970 x_wm_set_window_state
3971 (f, (EQ (w32_get_arg (parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL), Qicon)
3972 ? IconicState
3973 : NormalState));
3975 x_text_icon (f, (char *) SDATA ((!NILP (f->icon_name)
3976 ? f->icon_name
3977 : f->name)));
3978 #endif
3980 UNBLOCK_INPUT;
3984 static void
3985 x_make_gc (f)
3986 struct frame *f;
3988 XGCValues gc_values;
3990 BLOCK_INPUT;
3992 /* Create the GC's of this frame.
3993 Note that many default values are used. */
3995 /* Normal video */
3996 gc_values.font = FRAME_FONT (f);
3998 /* Cursor has cursor-color background, background-color foreground. */
3999 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
4000 gc_values.background = f->output_data.w32->cursor_pixel;
4001 f->output_data.w32->cursor_gc
4002 = XCreateGC (NULL, FRAME_W32_WINDOW (f),
4003 (GCFont | GCForeground | GCBackground),
4004 &gc_values);
4006 /* Reliefs. */
4007 f->output_data.w32->white_relief.gc = 0;
4008 f->output_data.w32->black_relief.gc = 0;
4010 UNBLOCK_INPUT;
4014 /* Handler for signals raised during x_create_frame and
4015 x_create_top_frame. FRAME is the frame which is partially
4016 constructed. */
4018 static Lisp_Object
4019 unwind_create_frame (frame)
4020 Lisp_Object frame;
4022 struct frame *f = XFRAME (frame);
4024 /* If frame is ``official'', nothing to do. */
4025 if (!CONSP (Vframe_list) || !EQ (XCAR (Vframe_list), frame))
4027 #ifdef GLYPH_DEBUG
4028 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4029 #endif
4031 x_free_frame_resources (f);
4033 /* Check that reference counts are indeed correct. */
4034 xassert (dpyinfo->reference_count == dpyinfo_refcount);
4035 xassert (dpyinfo->image_cache->refcount == image_cache_refcount);
4037 return Qt;
4040 return Qnil;
4044 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
4045 1, 1, 0,
4046 doc: /* Make a new window, which is called a \"frame\" in Emacs terms.
4047 Returns an Emacs frame object.
4048 PARAMETERS is an alist of frame parameters.
4049 If the parameters specify that the frame should not have a minibuffer,
4050 and do not specify a specific minibuffer window to use,
4051 then `default-minibuffer-frame' must be a frame whose minibuffer can
4052 be shared by the new frame.
4054 This function is an internal primitive--use `make-frame' instead. */)
4055 (parameters)
4056 Lisp_Object parameters;
4058 struct frame *f;
4059 Lisp_Object frame, tem;
4060 Lisp_Object name;
4061 int minibuffer_only = 0;
4062 long window_prompting = 0;
4063 int width, height;
4064 int count = SPECPDL_INDEX ();
4065 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4066 Lisp_Object display;
4067 struct w32_display_info *dpyinfo = NULL;
4068 Lisp_Object parent;
4069 struct kboard *kb;
4071 check_w32 ();
4073 /* Use this general default value to start with
4074 until we know if this frame has a specified name. */
4075 Vx_resource_name = Vinvocation_name;
4077 display = w32_get_arg (parameters, Qdisplay, 0, 0, RES_TYPE_STRING);
4078 if (EQ (display, Qunbound))
4079 display = Qnil;
4080 dpyinfo = check_x_display_info (display);
4081 #ifdef MULTI_KBOARD
4082 kb = dpyinfo->kboard;
4083 #else
4084 kb = &the_only_kboard;
4085 #endif
4087 name = w32_get_arg (parameters, Qname, "name", "Name", RES_TYPE_STRING);
4088 if (!STRINGP (name)
4089 && ! EQ (name, Qunbound)
4090 && ! NILP (name))
4091 error ("Invalid frame name--not a string or nil");
4093 if (STRINGP (name))
4094 Vx_resource_name = name;
4096 /* See if parent window is specified. */
4097 parent = w32_get_arg (parameters, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
4098 if (EQ (parent, Qunbound))
4099 parent = Qnil;
4100 if (! NILP (parent))
4101 CHECK_NUMBER (parent);
4103 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
4104 /* No need to protect DISPLAY because that's not used after passing
4105 it to make_frame_without_minibuffer. */
4106 frame = Qnil;
4107 GCPRO4 (parameters, parent, name, frame);
4108 tem = w32_get_arg (parameters, Qminibuffer, "minibuffer", "Minibuffer",
4109 RES_TYPE_SYMBOL);
4110 if (EQ (tem, Qnone) || NILP (tem))
4111 f = make_frame_without_minibuffer (Qnil, kb, display);
4112 else if (EQ (tem, Qonly))
4114 f = make_minibuffer_frame ();
4115 minibuffer_only = 1;
4117 else if (WINDOWP (tem))
4118 f = make_frame_without_minibuffer (tem, kb, display);
4119 else
4120 f = make_frame (1);
4122 XSETFRAME (frame, f);
4124 /* Note that Windows does support scroll bars. */
4125 FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;
4127 /* By default, make scrollbars the system standard width. */
4128 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = GetSystemMetrics (SM_CXVSCROLL);
4130 f->output_method = output_w32;
4131 f->output_data.w32 =
4132 (struct w32_output *) xmalloc (sizeof (struct w32_output));
4133 bzero (f->output_data.w32, sizeof (struct w32_output));
4134 FRAME_FONTSET (f) = -1;
4135 record_unwind_protect (unwind_create_frame, frame);
4137 f->icon_name
4138 = w32_get_arg (parameters, Qicon_name, "iconName", "Title", RES_TYPE_STRING);
4139 if (! STRINGP (f->icon_name))
4140 f->icon_name = Qnil;
4142 /* FRAME_W32_DISPLAY_INFO (f) = dpyinfo; */
4143 #ifdef MULTI_KBOARD
4144 FRAME_KBOARD (f) = kb;
4145 #endif
4147 /* Specify the parent under which to make this window. */
4149 if (!NILP (parent))
4151 f->output_data.w32->parent_desc = (Window) XFASTINT (parent);
4152 f->output_data.w32->explicit_parent = 1;
4154 else
4156 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
4157 f->output_data.w32->explicit_parent = 0;
4160 /* Set the name; the functions to which we pass f expect the name to
4161 be set. */
4162 if (EQ (name, Qunbound) || NILP (name))
4164 f->name = build_string (dpyinfo->w32_id_name);
4165 f->explicit_name = 0;
4167 else
4169 f->name = name;
4170 f->explicit_name = 1;
4171 /* use the frame's title when getting resources for this frame. */
4172 specbind (Qx_resource_name, name);
4175 /* Extract the window parameters from the supplied values
4176 that are needed to determine window geometry. */
4178 Lisp_Object font;
4180 font = w32_get_arg (parameters, Qfont, "font", "Font", RES_TYPE_STRING);
4182 BLOCK_INPUT;
4183 /* First, try whatever font the caller has specified. */
4184 if (STRINGP (font))
4186 tem = Fquery_fontset (font, Qnil);
4187 if (STRINGP (tem))
4188 font = x_new_fontset (f, SDATA (tem));
4189 else
4190 font = x_new_font (f, SDATA (font));
4192 /* Try out a font which we hope has bold and italic variations. */
4193 if (!STRINGP (font))
4194 font = x_new_font (f, "-*-Courier New-normal-r-*-*-*-100-*-*-c-*-iso8859-1");
4195 if (! STRINGP (font))
4196 font = x_new_font (f, "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1");
4197 /* If those didn't work, look for something which will at least work. */
4198 if (! STRINGP (font))
4199 font = x_new_font (f, "-*-Fixedsys-normal-r-*-*-12-*-*-*-c-*-iso8859-1");
4200 UNBLOCK_INPUT;
4201 if (! STRINGP (font))
4202 font = build_string ("Fixedsys");
4204 x_default_parameter (f, parameters, Qfont, font,
4205 "font", "Font", RES_TYPE_STRING);
4208 x_default_parameter (f, parameters, Qborder_width, make_number (2),
4209 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
4210 /* This defaults to 2 in order to match xterm. We recognize either
4211 internalBorderWidth or internalBorder (which is what xterm calls
4212 it). */
4213 if (NILP (Fassq (Qinternal_border_width, parameters)))
4215 Lisp_Object value;
4217 value = w32_get_arg (parameters, Qinternal_border_width,
4218 "internalBorder", "InternalBorder", RES_TYPE_NUMBER);
4219 if (! EQ (value, Qunbound))
4220 parameters = Fcons (Fcons (Qinternal_border_width, value),
4221 parameters);
4223 /* Default internalBorderWidth to 0 on Windows to match other programs. */
4224 x_default_parameter (f, parameters, Qinternal_border_width, make_number (0),
4225 "internalBorderWidth", "InternalBorder", RES_TYPE_NUMBER);
4226 x_default_parameter (f, parameters, Qvertical_scroll_bars, Qright,
4227 "verticalScrollBars", "ScrollBars", RES_TYPE_SYMBOL);
4229 /* Also do the stuff which must be set before the window exists. */
4230 x_default_parameter (f, parameters, Qforeground_color, build_string ("black"),
4231 "foreground", "Foreground", RES_TYPE_STRING);
4232 x_default_parameter (f, parameters, Qbackground_color, build_string ("white"),
4233 "background", "Background", RES_TYPE_STRING);
4234 x_default_parameter (f, parameters, Qmouse_color, build_string ("black"),
4235 "pointerColor", "Foreground", RES_TYPE_STRING);
4236 x_default_parameter (f, parameters, Qcursor_color, build_string ("black"),
4237 "cursorColor", "Foreground", RES_TYPE_STRING);
4238 x_default_parameter (f, parameters, Qborder_color, build_string ("black"),
4239 "borderColor", "BorderColor", RES_TYPE_STRING);
4240 x_default_parameter (f, parameters, Qscreen_gamma, Qnil,
4241 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
4242 x_default_parameter (f, parameters, Qline_spacing, Qnil,
4243 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
4244 x_default_parameter (f, parameters, Qleft_fringe, Qnil,
4245 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
4246 x_default_parameter (f, parameters, Qright_fringe, Qnil,
4247 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
4250 /* Init faces before x_default_parameter is called for scroll-bar
4251 parameters because that function calls x_set_scroll_bar_width,
4252 which calls change_frame_size, which calls Fset_window_buffer,
4253 which runs hooks, which call Fvertical_motion. At the end, we
4254 end up in init_iterator with a null face cache, which should not
4255 happen. */
4256 init_frame_faces (f);
4258 x_default_parameter (f, parameters, Qmenu_bar_lines, make_number (1),
4259 "menuBar", "MenuBar", RES_TYPE_NUMBER);
4260 x_default_parameter (f, parameters, Qtool_bar_lines, make_number (1),
4261 "toolBar", "ToolBar", RES_TYPE_NUMBER);
4263 x_default_parameter (f, parameters, Qbuffer_predicate, Qnil,
4264 "bufferPredicate", "BufferPredicate", RES_TYPE_SYMBOL);
4265 x_default_parameter (f, parameters, Qtitle, Qnil,
4266 "title", "Title", RES_TYPE_STRING);
4267 x_default_parameter (f, parameters, Qfullscreen, Qnil,
4268 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
4270 f->output_data.w32->dwStyle = WS_OVERLAPPEDWINDOW;
4271 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
4273 f->output_data.w32->text_cursor = w32_load_cursor (IDC_IBEAM);
4274 f->output_data.w32->nontext_cursor = w32_load_cursor (IDC_ARROW);
4275 f->output_data.w32->modeline_cursor = w32_load_cursor (IDC_ARROW);
4276 f->output_data.w32->hand_cursor = w32_load_cursor (IDC_HAND);
4277 f->output_data.w32->hourglass_cursor = w32_load_cursor (IDC_WAIT);
4278 f->output_data.w32->horizontal_drag_cursor = w32_load_cursor (IDC_SIZEWE);
4280 window_prompting = x_figure_window_size (f, parameters, 1);
4282 tem = w32_get_arg (parameters, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
4283 f->no_split = minibuffer_only || EQ (tem, Qt);
4285 w32_window (f, window_prompting, minibuffer_only);
4286 x_icon (f, parameters);
4288 x_make_gc (f);
4290 /* Now consider the frame official. */
4291 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
4292 Vframe_list = Fcons (frame, Vframe_list);
4294 /* We need to do this after creating the window, so that the
4295 icon-creation functions can say whose icon they're describing. */
4296 x_default_parameter (f, parameters, Qicon_type, Qnil,
4297 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
4299 x_default_parameter (f, parameters, Qauto_raise, Qnil,
4300 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4301 x_default_parameter (f, parameters, Qauto_lower, Qnil,
4302 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4303 x_default_parameter (f, parameters, Qcursor_type, Qbox,
4304 "cursorType", "CursorType", RES_TYPE_SYMBOL);
4305 x_default_parameter (f, parameters, Qscroll_bar_width, Qnil,
4306 "scrollBarWidth", "ScrollBarWidth", RES_TYPE_NUMBER);
4308 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
4309 Change will not be effected unless different from the current
4310 FRAME_LINES (f). */
4311 width = FRAME_COLS (f);
4312 height = FRAME_LINES (f);
4314 FRAME_LINES (f) = 0;
4315 SET_FRAME_COLS (f, 0);
4316 change_frame_size (f, height, width, 1, 0, 0);
4318 /* Tell the server what size and position, etc, we want, and how
4319 badly we want them. This should be done after we have the menu
4320 bar so that its size can be taken into account. */
4321 BLOCK_INPUT;
4322 x_wm_set_size_hint (f, window_prompting, 0);
4323 UNBLOCK_INPUT;
4325 /* Avoid a bug that causes the new frame to never become visible if
4326 an echo area message is displayed during the following call1. */
4327 specbind(Qredisplay_dont_pause, Qt);
4329 /* Set up faces after all frame parameters are known. This call
4330 also merges in face attributes specified for new frames. If we
4331 don't do this, the `menu' face for instance won't have the right
4332 colors, and the menu bar won't appear in the specified colors for
4333 new frames. */
4334 call1 (Qface_set_after_frame_default, frame);
4336 /* Make the window appear on the frame and enable display, unless
4337 the caller says not to. However, with explicit parent, Emacs
4338 cannot control visibility, so don't try. */
4339 if (! f->output_data.w32->explicit_parent)
4341 Lisp_Object visibility;
4343 visibility = w32_get_arg (parameters, Qvisibility, 0, 0, RES_TYPE_SYMBOL);
4344 if (EQ (visibility, Qunbound))
4345 visibility = Qt;
4347 if (EQ (visibility, Qicon))
4348 x_iconify_frame (f);
4349 else if (! NILP (visibility))
4350 x_make_frame_visible (f);
4351 else
4352 /* Must have been Qnil. */
4355 UNGCPRO;
4357 /* Make sure windows on this frame appear in calls to next-window
4358 and similar functions. */
4359 Vwindow_list = Qnil;
4361 return unbind_to (count, frame);
4364 /* FRAME is used only to get a handle on the X display. We don't pass the
4365 display info directly because we're called from frame.c, which doesn't
4366 know about that structure. */
4367 Lisp_Object
4368 x_get_focus_frame (frame)
4369 struct frame *frame;
4371 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (frame);
4372 Lisp_Object xfocus;
4373 if (! dpyinfo->w32_focus_frame)
4374 return Qnil;
4376 XSETFRAME (xfocus, dpyinfo->w32_focus_frame);
4377 return xfocus;
4380 DEFUN ("w32-focus-frame", Fw32_focus_frame, Sw32_focus_frame, 1, 1, 0,
4381 doc: /* Give FRAME input focus, raising to foreground if necessary. */)
4382 (frame)
4383 Lisp_Object frame;
4385 x_focus_on_frame (check_x_frame (frame));
4386 return Qnil;
4390 /* Return the charset portion of a font name. */
4391 char * xlfd_charset_of_font (char * fontname)
4393 char *charset, *encoding;
4395 encoding = strrchr(fontname, '-');
4396 if (!encoding || encoding == fontname)
4397 return NULL;
4399 for (charset = encoding - 1; charset >= fontname; charset--)
4400 if (*charset == '-')
4401 break;
4403 if (charset == fontname || strcmp(charset, "-*-*") == 0)
4404 return NULL;
4406 return charset + 1;
4409 struct font_info *w32_load_bdf_font (struct frame *f, char *fontname,
4410 int size, char* filename);
4411 static Lisp_Object w32_list_bdf_fonts (Lisp_Object pattern, int max_names);
4412 static BOOL w32_to_x_font (LOGFONT * lplf, char * lpxstr, int len,
4413 char * charset);
4414 static BOOL x_to_w32_font (char *lpxstr, LOGFONT *lplogfont);
4416 static struct font_info *
4417 w32_load_system_font (f,fontname,size)
4418 struct frame *f;
4419 char * fontname;
4420 int size;
4422 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4423 Lisp_Object font_names;
4425 /* Get a list of all the fonts that match this name. Once we
4426 have a list of matching fonts, we compare them against the fonts
4427 we already have loaded by comparing names. */
4428 font_names = w32_list_fonts (f, build_string (fontname), size, 100);
4430 if (!NILP (font_names))
4432 Lisp_Object tail;
4433 int i;
4435 /* First check if any are already loaded, as that is cheaper
4436 than loading another one. */
4437 for (i = 0; i < dpyinfo->n_fonts; i++)
4438 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
4439 if (dpyinfo->font_table[i].name
4440 && (!strcmp (dpyinfo->font_table[i].name,
4441 SDATA (XCAR (tail)))
4442 || !strcmp (dpyinfo->font_table[i].full_name,
4443 SDATA (XCAR (tail)))))
4444 return (dpyinfo->font_table + i);
4446 fontname = (char *) SDATA (XCAR (font_names));
4448 else if (w32_strict_fontnames)
4450 /* If EnumFontFamiliesEx was available, we got a full list of
4451 fonts back so stop now to avoid the possibility of loading a
4452 random font. If we had to fall back to EnumFontFamilies, the
4453 list is incomplete, so continue whether the font we want was
4454 listed or not. */
4455 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
4456 FARPROC enum_font_families_ex
4457 = GetProcAddress (gdi32, "EnumFontFamiliesExA");
4458 if (enum_font_families_ex)
4459 return NULL;
4462 /* Load the font and add it to the table. */
4464 char *full_name, *encoding, *charset;
4465 XFontStruct *font;
4466 struct font_info *fontp;
4467 LOGFONT lf;
4468 BOOL ok;
4469 int codepage;
4470 int i;
4472 if (!fontname || !x_to_w32_font (fontname, &lf))
4473 return (NULL);
4475 if (!*lf.lfFaceName)
4476 /* If no name was specified for the font, we get a random font
4477 from CreateFontIndirect - this is not particularly
4478 desirable, especially since CreateFontIndirect does not
4479 fill out the missing name in lf, so we never know what we
4480 ended up with. */
4481 return NULL;
4483 lf.lfQuality = DEFAULT_QUALITY;
4485 font = (XFontStruct *) xmalloc (sizeof (XFontStruct));
4486 bzero (font, sizeof (*font));
4488 /* Set bdf to NULL to indicate that this is a Windows font. */
4489 font->bdf = NULL;
4491 BLOCK_INPUT;
4493 font->hfont = CreateFontIndirect (&lf);
4495 if (font->hfont == NULL)
4497 ok = FALSE;
4499 else
4501 HDC hdc;
4502 HANDLE oldobj;
4504 codepage = w32_codepage_for_font (fontname);
4506 hdc = GetDC (dpyinfo->root_window);
4507 oldobj = SelectObject (hdc, font->hfont);
4509 ok = GetTextMetrics (hdc, &font->tm);
4510 if (codepage == CP_UNICODE)
4511 font->double_byte_p = 1;
4512 else
4514 /* Unfortunately, some fonts (eg. MingLiU, a big5 ttf font)
4515 don't report themselves as double byte fonts, when
4516 patently they are. So instead of trusting
4517 GetFontLanguageInfo, we check the properties of the
4518 codepage directly, since that is ultimately what we are
4519 working from anyway. */
4520 /* font->double_byte_p = GetFontLanguageInfo(hdc) & GCP_DBCS; */
4521 CPINFO cpi = {0};
4522 GetCPInfo (codepage, &cpi);
4523 font->double_byte_p = cpi.MaxCharSize > 1;
4526 SelectObject (hdc, oldobj);
4527 ReleaseDC (dpyinfo->root_window, hdc);
4528 /* Fill out details in lf according to the font that was
4529 actually loaded. */
4530 lf.lfHeight = font->tm.tmInternalLeading - font->tm.tmHeight;
4531 lf.lfWidth = font->tm.tmAveCharWidth;
4532 lf.lfWeight = font->tm.tmWeight;
4533 lf.lfItalic = font->tm.tmItalic;
4534 lf.lfCharSet = font->tm.tmCharSet;
4535 lf.lfPitchAndFamily = ((font->tm.tmPitchAndFamily & TMPF_FIXED_PITCH)
4536 ? VARIABLE_PITCH : FIXED_PITCH);
4537 lf.lfOutPrecision = ((font->tm.tmPitchAndFamily & TMPF_VECTOR)
4538 ? OUT_STROKE_PRECIS : OUT_STRING_PRECIS);
4540 w32_cache_char_metrics (font);
4543 UNBLOCK_INPUT;
4545 if (!ok)
4547 w32_unload_font (dpyinfo, font);
4548 return (NULL);
4551 /* Find a free slot in the font table. */
4552 for (i = 0; i < dpyinfo->n_fonts; ++i)
4553 if (dpyinfo->font_table[i].name == NULL)
4554 break;
4556 /* If no free slot found, maybe enlarge the font table. */
4557 if (i == dpyinfo->n_fonts
4558 && dpyinfo->n_fonts == dpyinfo->font_table_size)
4560 int sz;
4561 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
4562 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
4563 dpyinfo->font_table
4564 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
4567 fontp = dpyinfo->font_table + i;
4568 if (i == dpyinfo->n_fonts)
4569 ++dpyinfo->n_fonts;
4571 /* Now fill in the slots of *FONTP. */
4572 BLOCK_INPUT;
4573 bzero (fontp, sizeof (*fontp));
4574 fontp->font = font;
4575 fontp->font_idx = i;
4576 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
4577 bcopy (fontname, fontp->name, strlen (fontname) + 1);
4579 charset = xlfd_charset_of_font (fontname);
4581 /* Cache the W32 codepage for a font. This makes w32_encode_char
4582 (called for every glyph during redisplay) much faster. */
4583 fontp->codepage = codepage;
4585 /* Work out the font's full name. */
4586 full_name = (char *)xmalloc (100);
4587 if (full_name && w32_to_x_font (&lf, full_name, 100, charset))
4588 fontp->full_name = full_name;
4589 else
4591 /* If all else fails - just use the name we used to load it. */
4592 xfree (full_name);
4593 fontp->full_name = fontp->name;
4596 fontp->size = FONT_WIDTH (font);
4597 fontp->height = FONT_HEIGHT (font);
4599 /* The slot `encoding' specifies how to map a character
4600 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
4601 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
4602 (0:0x20..0x7F, 1:0xA0..0xFF,
4603 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
4604 2:0xA020..0xFF7F). For the moment, we don't know which charset
4605 uses this font. So, we set information in fontp->encoding[1]
4606 which is never used by any charset. If mapping can't be
4607 decided, set FONT_ENCODING_NOT_DECIDED. */
4609 /* SJIS fonts need to be set to type 4, all others seem to work as
4610 type FONT_ENCODING_NOT_DECIDED. */
4611 encoding = strrchr (fontp->name, '-');
4612 if (encoding && strnicmp (encoding+1, "sjis", 4) == 0)
4613 fontp->encoding[1] = 4;
4614 else
4615 fontp->encoding[1] = FONT_ENCODING_NOT_DECIDED;
4617 /* The following three values are set to 0 under W32, which is
4618 what they get set to if XGetFontProperty fails under X. */
4619 fontp->baseline_offset = 0;
4620 fontp->relative_compose = 0;
4621 fontp->default_ascent = 0;
4623 /* Set global flag fonts_changed_p to non-zero if the font loaded
4624 has a character with a smaller width than any other character
4625 before, or if the font loaded has a smaller height than any
4626 other font loaded before. If this happens, it will make a
4627 glyph matrix reallocation necessary. */
4628 fonts_changed_p |= x_compute_min_glyph_bounds (f);
4629 UNBLOCK_INPUT;
4630 return fontp;
4634 /* Load font named FONTNAME of size SIZE for frame F, and return a
4635 pointer to the structure font_info while allocating it dynamically.
4636 If loading fails, return NULL. */
4637 struct font_info *
4638 w32_load_font (f,fontname,size)
4639 struct frame *f;
4640 char * fontname;
4641 int size;
4643 Lisp_Object bdf_fonts;
4644 struct font_info *retval = NULL;
4646 bdf_fonts = w32_list_bdf_fonts (build_string (fontname), 1);
4648 while (!retval && CONSP (bdf_fonts))
4650 char *bdf_name, *bdf_file;
4651 Lisp_Object bdf_pair;
4653 bdf_name = SDATA (XCAR (bdf_fonts));
4654 bdf_pair = Fassoc (XCAR (bdf_fonts), Vw32_bdf_filename_alist);
4655 bdf_file = SDATA (XCDR (bdf_pair));
4657 retval = w32_load_bdf_font (f, bdf_name, size, bdf_file);
4659 bdf_fonts = XCDR (bdf_fonts);
4662 if (retval)
4663 return retval;
4665 return w32_load_system_font(f, fontname, size);
4669 void
4670 w32_unload_font (dpyinfo, font)
4671 struct w32_display_info *dpyinfo;
4672 XFontStruct * font;
4674 if (font)
4676 if (font->per_char) xfree (font->per_char);
4677 if (font->bdf) w32_free_bdf_font (font->bdf);
4679 if (font->hfont) DeleteObject(font->hfont);
4680 xfree (font);
4684 /* The font conversion stuff between x and w32 */
4686 /* X font string is as follows (from faces.el)
4687 * (let ((- "[-?]")
4688 * (foundry "[^-]+")
4689 * (family "[^-]+")
4690 * (weight "\\(bold\\|demibold\\|medium\\)") ; 1
4691 * (weight\? "\\([^-]*\\)") ; 1
4692 * (slant "\\([ior]\\)") ; 2
4693 * (slant\? "\\([^-]?\\)") ; 2
4694 * (swidth "\\([^-]*\\)") ; 3
4695 * (adstyle "[^-]*") ; 4
4696 * (pixelsize "[0-9]+")
4697 * (pointsize "[0-9][0-9]+")
4698 * (resx "[0-9][0-9]+")
4699 * (resy "[0-9][0-9]+")
4700 * (spacing "[cmp?*]")
4701 * (avgwidth "[0-9]+")
4702 * (registry "[^-]+")
4703 * (encoding "[^-]+")
4707 static LONG
4708 x_to_w32_weight (lpw)
4709 char * lpw;
4711 if (!lpw) return (FW_DONTCARE);
4713 if (stricmp (lpw,"heavy") == 0) return FW_HEAVY;
4714 else if (stricmp (lpw,"extrabold") == 0) return FW_EXTRABOLD;
4715 else if (stricmp (lpw,"bold") == 0) return FW_BOLD;
4716 else if (stricmp (lpw,"demibold") == 0) return FW_SEMIBOLD;
4717 else if (stricmp (lpw,"semibold") == 0) return FW_SEMIBOLD;
4718 else if (stricmp (lpw,"medium") == 0) return FW_MEDIUM;
4719 else if (stricmp (lpw,"normal") == 0) return FW_NORMAL;
4720 else if (stricmp (lpw,"light") == 0) return FW_LIGHT;
4721 else if (stricmp (lpw,"extralight") == 0) return FW_EXTRALIGHT;
4722 else if (stricmp (lpw,"thin") == 0) return FW_THIN;
4723 else
4724 return FW_DONTCARE;
4728 static char *
4729 w32_to_x_weight (fnweight)
4730 int fnweight;
4732 if (fnweight >= FW_HEAVY) return "heavy";
4733 if (fnweight >= FW_EXTRABOLD) return "extrabold";
4734 if (fnweight >= FW_BOLD) return "bold";
4735 if (fnweight >= FW_SEMIBOLD) return "demibold";
4736 if (fnweight >= FW_MEDIUM) return "medium";
4737 if (fnweight >= FW_NORMAL) return "normal";
4738 if (fnweight >= FW_LIGHT) return "light";
4739 if (fnweight >= FW_EXTRALIGHT) return "extralight";
4740 if (fnweight >= FW_THIN) return "thin";
4741 else
4742 return "*";
4745 static LONG
4746 x_to_w32_charset (lpcs)
4747 char * lpcs;
4749 Lisp_Object this_entry, w32_charset;
4750 char *charset;
4751 int len = strlen (lpcs);
4753 /* Support "*-#nnn" format for unknown charsets. */
4754 if (strncmp (lpcs, "*-#", 3) == 0)
4755 return atoi (lpcs + 3);
4757 /* Handle wildcards by ignoring them; eg. treat "big5*-*" as "big5". */
4758 charset = alloca (len + 1);
4759 strcpy (charset, lpcs);
4760 lpcs = strchr (charset, '*');
4761 if (lpcs)
4762 *lpcs = 0;
4764 /* Look through w32-charset-info-alist for the character set.
4765 Format of each entry is
4766 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
4768 this_entry = Fassoc (build_string(charset), Vw32_charset_info_alist);
4770 if (NILP(this_entry))
4772 /* At startup, we want iso8859-1 fonts to come up properly. */
4773 if (stricmp(charset, "iso8859-1") == 0)
4774 return ANSI_CHARSET;
4775 else
4776 return DEFAULT_CHARSET;
4779 w32_charset = Fcar (Fcdr (this_entry));
4781 /* Translate Lisp symbol to number. */
4782 if (EQ (w32_charset, Qw32_charset_ansi))
4783 return ANSI_CHARSET;
4784 if (EQ (w32_charset, Qw32_charset_symbol))
4785 return SYMBOL_CHARSET;
4786 if (EQ (w32_charset, Qw32_charset_shiftjis))
4787 return SHIFTJIS_CHARSET;
4788 if (EQ (w32_charset, Qw32_charset_hangeul))
4789 return HANGEUL_CHARSET;
4790 if (EQ (w32_charset, Qw32_charset_chinesebig5))
4791 return CHINESEBIG5_CHARSET;
4792 if (EQ (w32_charset, Qw32_charset_gb2312))
4793 return GB2312_CHARSET;
4794 if (EQ (w32_charset, Qw32_charset_oem))
4795 return OEM_CHARSET;
4796 #ifdef JOHAB_CHARSET
4797 if (EQ (w32_charset, Qw32_charset_johab))
4798 return JOHAB_CHARSET;
4799 if (EQ (w32_charset, Qw32_charset_easteurope))
4800 return EASTEUROPE_CHARSET;
4801 if (EQ (w32_charset, Qw32_charset_turkish))
4802 return TURKISH_CHARSET;
4803 if (EQ (w32_charset, Qw32_charset_baltic))
4804 return BALTIC_CHARSET;
4805 if (EQ (w32_charset, Qw32_charset_russian))
4806 return RUSSIAN_CHARSET;
4807 if (EQ (w32_charset, Qw32_charset_arabic))
4808 return ARABIC_CHARSET;
4809 if (EQ (w32_charset, Qw32_charset_greek))
4810 return GREEK_CHARSET;
4811 if (EQ (w32_charset, Qw32_charset_hebrew))
4812 return HEBREW_CHARSET;
4813 if (EQ (w32_charset, Qw32_charset_vietnamese))
4814 return VIETNAMESE_CHARSET;
4815 if (EQ (w32_charset, Qw32_charset_thai))
4816 return THAI_CHARSET;
4817 if (EQ (w32_charset, Qw32_charset_mac))
4818 return MAC_CHARSET;
4819 #endif /* JOHAB_CHARSET */
4820 #ifdef UNICODE_CHARSET
4821 if (EQ (w32_charset, Qw32_charset_unicode))
4822 return UNICODE_CHARSET;
4823 #endif
4825 return DEFAULT_CHARSET;
4829 static char *
4830 w32_to_x_charset (fncharset)
4831 int fncharset;
4833 static char buf[32];
4834 Lisp_Object charset_type;
4836 switch (fncharset)
4838 case ANSI_CHARSET:
4839 /* Handle startup case of w32-charset-info-alist not
4840 being set up yet. */
4841 if (NILP(Vw32_charset_info_alist))
4842 return "iso8859-1";
4843 charset_type = Qw32_charset_ansi;
4844 break;
4845 case DEFAULT_CHARSET:
4846 charset_type = Qw32_charset_default;
4847 break;
4848 case SYMBOL_CHARSET:
4849 charset_type = Qw32_charset_symbol;
4850 break;
4851 case SHIFTJIS_CHARSET:
4852 charset_type = Qw32_charset_shiftjis;
4853 break;
4854 case HANGEUL_CHARSET:
4855 charset_type = Qw32_charset_hangeul;
4856 break;
4857 case GB2312_CHARSET:
4858 charset_type = Qw32_charset_gb2312;
4859 break;
4860 case CHINESEBIG5_CHARSET:
4861 charset_type = Qw32_charset_chinesebig5;
4862 break;
4863 case OEM_CHARSET:
4864 charset_type = Qw32_charset_oem;
4865 break;
4867 /* More recent versions of Windows (95 and NT4.0) define more
4868 character sets. */
4869 #ifdef EASTEUROPE_CHARSET
4870 case EASTEUROPE_CHARSET:
4871 charset_type = Qw32_charset_easteurope;
4872 break;
4873 case TURKISH_CHARSET:
4874 charset_type = Qw32_charset_turkish;
4875 break;
4876 case BALTIC_CHARSET:
4877 charset_type = Qw32_charset_baltic;
4878 break;
4879 case RUSSIAN_CHARSET:
4880 charset_type = Qw32_charset_russian;
4881 break;
4882 case ARABIC_CHARSET:
4883 charset_type = Qw32_charset_arabic;
4884 break;
4885 case GREEK_CHARSET:
4886 charset_type = Qw32_charset_greek;
4887 break;
4888 case HEBREW_CHARSET:
4889 charset_type = Qw32_charset_hebrew;
4890 break;
4891 case VIETNAMESE_CHARSET:
4892 charset_type = Qw32_charset_vietnamese;
4893 break;
4894 case THAI_CHARSET:
4895 charset_type = Qw32_charset_thai;
4896 break;
4897 case MAC_CHARSET:
4898 charset_type = Qw32_charset_mac;
4899 break;
4900 case JOHAB_CHARSET:
4901 charset_type = Qw32_charset_johab;
4902 break;
4903 #endif
4905 #ifdef UNICODE_CHARSET
4906 case UNICODE_CHARSET:
4907 charset_type = Qw32_charset_unicode;
4908 break;
4909 #endif
4910 default:
4911 /* Encode numerical value of unknown charset. */
4912 sprintf (buf, "*-#%u", fncharset);
4913 return buf;
4917 Lisp_Object rest;
4918 char * best_match = NULL;
4920 /* Look through w32-charset-info-alist for the character set.
4921 Prefer ISO codepages, and prefer lower numbers in the ISO
4922 range. Only return charsets for codepages which are installed.
4924 Format of each entry is
4925 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
4927 for (rest = Vw32_charset_info_alist; CONSP (rest); rest = XCDR (rest))
4929 char * x_charset;
4930 Lisp_Object w32_charset;
4931 Lisp_Object codepage;
4933 Lisp_Object this_entry = XCAR (rest);
4935 /* Skip invalid entries in alist. */
4936 if (!CONSP (this_entry) || !STRINGP (XCAR (this_entry))
4937 || !CONSP (XCDR (this_entry))
4938 || !SYMBOLP (XCAR (XCDR (this_entry))))
4939 continue;
4941 x_charset = SDATA (XCAR (this_entry));
4942 w32_charset = XCAR (XCDR (this_entry));
4943 codepage = XCDR (XCDR (this_entry));
4945 /* Look for Same charset and a valid codepage (or non-int
4946 which means ignore). */
4947 if (EQ (w32_charset, charset_type)
4948 && (!INTEGERP (codepage) || XINT (codepage) == CP_DEFAULT
4949 || IsValidCodePage (XINT (codepage))))
4951 /* If we don't have a match already, then this is the
4952 best. */
4953 if (!best_match)
4954 best_match = x_charset;
4955 /* If this is an ISO codepage, and the best so far isn't,
4956 then this is better. */
4957 else if (strnicmp (best_match, "iso", 3) != 0
4958 && strnicmp (x_charset, "iso", 3) == 0)
4959 best_match = x_charset;
4960 /* If both are ISO8859 codepages, choose the one with the
4961 lowest number in the encoding field. */
4962 else if (strnicmp (best_match, "iso8859-", 8) == 0
4963 && strnicmp (x_charset, "iso8859-", 8) == 0)
4965 int best_enc = atoi (best_match + 8);
4966 int this_enc = atoi (x_charset + 8);
4967 if (this_enc > 0 && this_enc < best_enc)
4968 best_match = x_charset;
4973 /* If no match, encode the numeric value. */
4974 if (!best_match)
4976 sprintf (buf, "*-#%u", fncharset);
4977 return buf;
4980 strncpy(buf, best_match, 31);
4981 buf[31] = '\0';
4982 return buf;
4987 /* Return all the X charsets that map to a font. */
4988 static Lisp_Object
4989 w32_to_all_x_charsets (fncharset)
4990 int fncharset;
4992 static char buf[32];
4993 Lisp_Object charset_type;
4994 Lisp_Object retval = Qnil;
4996 switch (fncharset)
4998 case ANSI_CHARSET:
4999 /* Handle startup case of w32-charset-info-alist not
5000 being set up yet. */
5001 if (NILP(Vw32_charset_info_alist))
5002 return Fcons (build_string ("iso8859-1"), Qnil);
5004 charset_type = Qw32_charset_ansi;
5005 break;
5006 case DEFAULT_CHARSET:
5007 charset_type = Qw32_charset_default;
5008 break;
5009 case SYMBOL_CHARSET:
5010 charset_type = Qw32_charset_symbol;
5011 break;
5012 case SHIFTJIS_CHARSET:
5013 charset_type = Qw32_charset_shiftjis;
5014 break;
5015 case HANGEUL_CHARSET:
5016 charset_type = Qw32_charset_hangeul;
5017 break;
5018 case GB2312_CHARSET:
5019 charset_type = Qw32_charset_gb2312;
5020 break;
5021 case CHINESEBIG5_CHARSET:
5022 charset_type = Qw32_charset_chinesebig5;
5023 break;
5024 case OEM_CHARSET:
5025 charset_type = Qw32_charset_oem;
5026 break;
5028 /* More recent versions of Windows (95 and NT4.0) define more
5029 character sets. */
5030 #ifdef EASTEUROPE_CHARSET
5031 case EASTEUROPE_CHARSET:
5032 charset_type = Qw32_charset_easteurope;
5033 break;
5034 case TURKISH_CHARSET:
5035 charset_type = Qw32_charset_turkish;
5036 break;
5037 case BALTIC_CHARSET:
5038 charset_type = Qw32_charset_baltic;
5039 break;
5040 case RUSSIAN_CHARSET:
5041 charset_type = Qw32_charset_russian;
5042 break;
5043 case ARABIC_CHARSET:
5044 charset_type = Qw32_charset_arabic;
5045 break;
5046 case GREEK_CHARSET:
5047 charset_type = Qw32_charset_greek;
5048 break;
5049 case HEBREW_CHARSET:
5050 charset_type = Qw32_charset_hebrew;
5051 break;
5052 case VIETNAMESE_CHARSET:
5053 charset_type = Qw32_charset_vietnamese;
5054 break;
5055 case THAI_CHARSET:
5056 charset_type = Qw32_charset_thai;
5057 break;
5058 case MAC_CHARSET:
5059 charset_type = Qw32_charset_mac;
5060 break;
5061 case JOHAB_CHARSET:
5062 charset_type = Qw32_charset_johab;
5063 break;
5064 #endif
5066 #ifdef UNICODE_CHARSET
5067 case UNICODE_CHARSET:
5068 charset_type = Qw32_charset_unicode;
5069 break;
5070 #endif
5071 default:
5072 /* Encode numerical value of unknown charset. */
5073 sprintf (buf, "*-#%u", fncharset);
5074 return Fcons (build_string (buf), Qnil);
5078 Lisp_Object rest;
5079 /* Look through w32-charset-info-alist for the character set.
5080 Only return charsets for codepages which are installed.
5082 Format of each entry in Vw32_charset_info_alist is
5083 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
5085 for (rest = Vw32_charset_info_alist; CONSP (rest); rest = XCDR (rest))
5087 Lisp_Object x_charset;
5088 Lisp_Object w32_charset;
5089 Lisp_Object codepage;
5091 Lisp_Object this_entry = XCAR (rest);
5093 /* Skip invalid entries in alist. */
5094 if (!CONSP (this_entry) || !STRINGP (XCAR (this_entry))
5095 || !CONSP (XCDR (this_entry))
5096 || !SYMBOLP (XCAR (XCDR (this_entry))))
5097 continue;
5099 x_charset = XCAR (this_entry);
5100 w32_charset = XCAR (XCDR (this_entry));
5101 codepage = XCDR (XCDR (this_entry));
5103 /* Look for Same charset and a valid codepage (or non-int
5104 which means ignore). */
5105 if (EQ (w32_charset, charset_type)
5106 && (!INTEGERP (codepage) || XINT (codepage) == CP_DEFAULT
5107 || IsValidCodePage (XINT (codepage))))
5109 retval = Fcons (x_charset, retval);
5113 /* If no match, encode the numeric value. */
5114 if (NILP (retval))
5116 sprintf (buf, "*-#%u", fncharset);
5117 return Fcons (build_string (buf), Qnil);
5120 return retval;
5124 /* Get the Windows codepage corresponding to the specified font. The
5125 charset info in the font name is used to look up
5126 w32-charset-to-codepage-alist. */
5128 w32_codepage_for_font (char *fontname)
5130 Lisp_Object codepage, entry;
5131 char *charset_str, *charset, *end;
5133 if (NILP (Vw32_charset_info_alist))
5134 return CP_DEFAULT;
5136 /* Extract charset part of font string. */
5137 charset = xlfd_charset_of_font (fontname);
5139 if (!charset)
5140 return CP_UNKNOWN;
5142 charset_str = (char *) alloca (strlen (charset) + 1);
5143 strcpy (charset_str, charset);
5145 #if 0
5146 /* Remove leading "*-". */
5147 if (strncmp ("*-", charset_str, 2) == 0)
5148 charset = charset_str + 2;
5149 else
5150 #endif
5151 charset = charset_str;
5153 /* Stop match at wildcard (including preceding '-'). */
5154 if (end = strchr (charset, '*'))
5156 if (end > charset && *(end-1) == '-')
5157 end--;
5158 *end = '\0';
5161 entry = Fassoc (build_string(charset), Vw32_charset_info_alist);
5162 if (NILP (entry))
5163 return CP_UNKNOWN;
5165 codepage = Fcdr (Fcdr (entry));
5167 if (NILP (codepage))
5168 return CP_8BIT;
5169 else if (XFASTINT (codepage) == XFASTINT (Qt))
5170 return CP_UNICODE;
5171 else if (INTEGERP (codepage))
5172 return XINT (codepage);
5173 else
5174 return CP_UNKNOWN;
5178 static BOOL
5179 w32_to_x_font (lplogfont, lpxstr, len, specific_charset)
5180 LOGFONT * lplogfont;
5181 char * lpxstr;
5182 int len;
5183 char * specific_charset;
5185 char* fonttype;
5186 char *fontname;
5187 char height_pixels[8];
5188 char height_dpi[8];
5189 char width_pixels[8];
5190 char *fontname_dash;
5191 int display_resy = (int) one_w32_display_info.resy;
5192 int display_resx = (int) one_w32_display_info.resx;
5193 int bufsz;
5194 struct coding_system coding;
5196 if (!lpxstr) abort ();
5198 if (!lplogfont)
5199 return FALSE;
5201 if (lplogfont->lfOutPrecision == OUT_STRING_PRECIS)
5202 fonttype = "raster";
5203 else if (lplogfont->lfOutPrecision == OUT_STROKE_PRECIS)
5204 fonttype = "outline";
5205 else
5206 fonttype = "unknown";
5208 setup_coding_system (Fcheck_coding_system (Vlocale_coding_system),
5209 &coding);
5210 coding.src_multibyte = 0;
5211 coding.dst_multibyte = 1;
5212 coding.mode |= CODING_MODE_LAST_BLOCK;
5213 /* We explicitely disable composition handling because selection
5214 data should not contain any composition sequence. */
5215 coding.composing = COMPOSITION_DISABLED;
5216 bufsz = decoding_buffer_size (&coding, LF_FACESIZE);
5218 fontname = alloca(sizeof(*fontname) * bufsz);
5219 decode_coding (&coding, lplogfont->lfFaceName, fontname,
5220 strlen(lplogfont->lfFaceName), bufsz - 1);
5221 *(fontname + coding.produced) = '\0';
5223 /* Replace dashes with underscores so the dashes are not
5224 misinterpreted. */
5225 fontname_dash = fontname;
5226 while (fontname_dash = strchr (fontname_dash, '-'))
5227 *fontname_dash = '_';
5229 if (lplogfont->lfHeight)
5231 sprintf (height_pixels, "%u", abs (lplogfont->lfHeight));
5232 sprintf (height_dpi, "%u",
5233 abs (lplogfont->lfHeight) * 720 / display_resy);
5235 else
5237 strcpy (height_pixels, "*");
5238 strcpy (height_dpi, "*");
5240 if (lplogfont->lfWidth)
5241 sprintf (width_pixels, "%u", lplogfont->lfWidth * 10);
5242 else
5243 strcpy (width_pixels, "*");
5245 _snprintf (lpxstr, len - 1,
5246 "-%s-%s-%s-%c-normal-normal-%s-%s-%d-%d-%c-%s-%s",
5247 fonttype, /* foundry */
5248 fontname, /* family */
5249 w32_to_x_weight (lplogfont->lfWeight), /* weight */
5250 lplogfont->lfItalic?'i':'r', /* slant */
5251 /* setwidth name */
5252 /* add style name */
5253 height_pixels, /* pixel size */
5254 height_dpi, /* point size */
5255 display_resx, /* resx */
5256 display_resy, /* resy */
5257 ((lplogfont->lfPitchAndFamily & 0x3) == VARIABLE_PITCH)
5258 ? 'p' : 'c', /* spacing */
5259 width_pixels, /* avg width */
5260 specific_charset ? specific_charset
5261 : w32_to_x_charset (lplogfont->lfCharSet)
5262 /* charset registry and encoding */
5265 lpxstr[len - 1] = 0; /* just to be sure */
5266 return (TRUE);
5269 static BOOL
5270 x_to_w32_font (lpxstr, lplogfont)
5271 char * lpxstr;
5272 LOGFONT * lplogfont;
5274 struct coding_system coding;
5276 if (!lplogfont) return (FALSE);
5278 memset (lplogfont, 0, sizeof (*lplogfont));
5280 /* Set default value for each field. */
5281 #if 1
5282 lplogfont->lfOutPrecision = OUT_DEFAULT_PRECIS;
5283 lplogfont->lfClipPrecision = CLIP_DEFAULT_PRECIS;
5284 lplogfont->lfQuality = DEFAULT_QUALITY;
5285 #else
5286 /* go for maximum quality */
5287 lplogfont->lfOutPrecision = OUT_STROKE_PRECIS;
5288 lplogfont->lfClipPrecision = CLIP_STROKE_PRECIS;
5289 lplogfont->lfQuality = PROOF_QUALITY;
5290 #endif
5292 lplogfont->lfCharSet = DEFAULT_CHARSET;
5293 lplogfont->lfWeight = FW_DONTCARE;
5294 lplogfont->lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
5296 if (!lpxstr)
5297 return FALSE;
5299 /* Provide a simple escape mechanism for specifying Windows font names
5300 * directly -- if font spec does not beginning with '-', assume this
5301 * format:
5302 * "<font name>[:height in pixels[:width in pixels[:weight]]]"
5305 if (*lpxstr == '-')
5307 int fields, tem;
5308 char name[50], weight[20], slant, pitch, pixels[10], height[10],
5309 width[10], resy[10], remainder[50];
5310 char * encoding;
5311 int dpi = (int) one_w32_display_info.resy;
5313 fields = sscanf (lpxstr,
5314 "-%*[^-]-%49[^-]-%19[^-]-%c-%*[^-]-%*[^-]-%9[^-]-%9[^-]-%*[^-]-%9[^-]-%c-%9[^-]-%49s",
5315 name, weight, &slant, pixels, height, resy, &pitch, width, remainder);
5316 if (fields == EOF)
5317 return (FALSE);
5319 /* In the general case when wildcards cover more than one field,
5320 we don't know which field is which, so don't fill any in.
5321 However, we need to cope with this particular form, which is
5322 generated by font_list_1 (invoked by try_font_list):
5323 "-raster-6x10-*-gb2312*-*"
5324 and make sure to correctly parse the charset field. */
5325 if (fields == 3)
5327 fields = sscanf (lpxstr,
5328 "-%*[^-]-%49[^-]-*-%49s",
5329 name, remainder);
5331 else if (fields < 9)
5333 fields = 0;
5334 remainder[0] = 0;
5337 if (fields > 0 && name[0] != '*')
5339 int bufsize;
5340 unsigned char *buf;
5342 setup_coding_system
5343 (Fcheck_coding_system (Vlocale_coding_system), &coding);
5344 coding.src_multibyte = 1;
5345 coding.dst_multibyte = 1;
5346 /* Need to set COMPOSITION_DISABLED, otherwise Emacs crashes in
5347 encode_coding_iso2022 trying to dereference a null pointer. */
5348 coding.composing = COMPOSITION_DISABLED;
5349 if (coding.type == coding_type_iso2022)
5350 coding.flags |= CODING_FLAG_ISO_SAFE;
5351 bufsize = encoding_buffer_size (&coding, strlen (name));
5352 buf = (unsigned char *) alloca (bufsize);
5353 coding.mode |= CODING_MODE_LAST_BLOCK;
5354 encode_coding (&coding, name, buf, strlen (name), bufsize);
5355 if (coding.produced >= LF_FACESIZE)
5356 coding.produced = LF_FACESIZE - 1;
5357 buf[coding.produced] = 0;
5358 strcpy (lplogfont->lfFaceName, buf);
5360 else
5362 lplogfont->lfFaceName[0] = '\0';
5365 fields--;
5367 lplogfont->lfWeight = x_to_w32_weight ((fields > 0 ? weight : ""));
5369 fields--;
5371 lplogfont->lfItalic = (fields > 0 && slant == 'i');
5373 fields--;
5375 if (fields > 0 && pixels[0] != '*')
5376 lplogfont->lfHeight = atoi (pixels);
5378 fields--;
5379 fields--;
5380 if (fields > 0 && resy[0] != '*')
5382 tem = atoi (resy);
5383 if (tem > 0) dpi = tem;
5386 if (fields > -1 && lplogfont->lfHeight == 0 && height[0] != '*')
5387 lplogfont->lfHeight = atoi (height) * dpi / 720;
5389 if (fields > 0)
5390 lplogfont->lfPitchAndFamily =
5391 (fields > 0 && pitch == 'p') ? VARIABLE_PITCH : FIXED_PITCH;
5393 fields--;
5395 if (fields > 0 && width[0] != '*')
5396 lplogfont->lfWidth = atoi (width) / 10;
5398 fields--;
5400 /* Strip the trailing '-' if present. (it shouldn't be, as it
5401 fails the test against xlfd-tight-regexp in fontset.el). */
5403 int len = strlen (remainder);
5404 if (len > 0 && remainder[len-1] == '-')
5405 remainder[len-1] = 0;
5407 encoding = remainder;
5408 #if 0
5409 if (strncmp (encoding, "*-", 2) == 0)
5410 encoding += 2;
5411 #endif
5412 lplogfont->lfCharSet = x_to_w32_charset (encoding);
5414 else
5416 int fields;
5417 char name[100], height[10], width[10], weight[20];
5419 fields = sscanf (lpxstr,
5420 "%99[^:]:%9[^:]:%9[^:]:%19s",
5421 name, height, width, weight);
5423 if (fields == EOF) return (FALSE);
5425 if (fields > 0)
5427 strncpy (lplogfont->lfFaceName,name, LF_FACESIZE);
5428 lplogfont->lfFaceName[LF_FACESIZE-1] = 0;
5430 else
5432 lplogfont->lfFaceName[0] = 0;
5435 fields--;
5437 if (fields > 0)
5438 lplogfont->lfHeight = atoi (height);
5440 fields--;
5442 if (fields > 0)
5443 lplogfont->lfWidth = atoi (width);
5445 fields--;
5447 lplogfont->lfWeight = x_to_w32_weight ((fields > 0 ? weight : ""));
5450 /* This makes TrueType fonts work better. */
5451 lplogfont->lfHeight = - abs (lplogfont->lfHeight);
5453 return (TRUE);
5456 /* Strip the pixel height and point height from the given xlfd, and
5457 return the pixel height. If no pixel height is specified, calculate
5458 one from the point height, or if that isn't defined either, return
5459 0 (which usually signifies a scalable font).
5461 static int
5462 xlfd_strip_height (char *fontname)
5464 int pixel_height, field_number;
5465 char *read_from, *write_to;
5467 xassert (fontname);
5469 pixel_height = field_number = 0;
5470 write_to = NULL;
5472 /* Look for height fields. */
5473 for (read_from = fontname; *read_from; read_from++)
5475 if (*read_from == '-')
5477 field_number++;
5478 if (field_number == 7) /* Pixel height. */
5480 read_from++;
5481 write_to = read_from;
5483 /* Find end of field. */
5484 for (;*read_from && *read_from != '-'; read_from++)
5487 /* Split the fontname at end of field. */
5488 if (*read_from)
5490 *read_from = '\0';
5491 read_from++;
5493 pixel_height = atoi (write_to);
5494 /* Blank out field. */
5495 if (read_from > write_to)
5497 *write_to = '-';
5498 write_to++;
5500 /* If the pixel height field is at the end (partial xlfd),
5501 return now. */
5502 else
5503 return pixel_height;
5505 /* If we got a pixel height, the point height can be
5506 ignored. Just blank it out and break now. */
5507 if (pixel_height)
5509 /* Find end of point size field. */
5510 for (; *read_from && *read_from != '-'; read_from++)
5513 if (*read_from)
5514 read_from++;
5516 /* Blank out the point size field. */
5517 if (read_from > write_to)
5519 *write_to = '-';
5520 write_to++;
5522 else
5523 return pixel_height;
5525 break;
5527 /* If the point height is already blank, break now. */
5528 if (*read_from == '-')
5530 read_from++;
5531 break;
5534 else if (field_number == 8)
5536 /* If we didn't get a pixel height, try to get the point
5537 height and convert that. */
5538 int point_size;
5539 char *point_size_start = read_from++;
5541 /* Find end of field. */
5542 for (; *read_from && *read_from != '-'; read_from++)
5545 if (*read_from)
5547 *read_from = '\0';
5548 read_from++;
5551 point_size = atoi (point_size_start);
5553 /* Convert to pixel height. */
5554 pixel_height = point_size
5555 * one_w32_display_info.height_in / 720;
5557 /* Blank out this field and break. */
5558 *write_to = '-';
5559 write_to++;
5560 break;
5565 /* Shift the rest of the font spec into place. */
5566 if (write_to && read_from > write_to)
5568 for (; *read_from; read_from++, write_to++)
5569 *write_to = *read_from;
5570 *write_to = '\0';
5573 return pixel_height;
5576 /* Assume parameter 1 is fully qualified, no wildcards. */
5577 static BOOL
5578 w32_font_match (fontname, pattern)
5579 char * fontname;
5580 char * pattern;
5582 char *regex = alloca (strlen (pattern) * 2 + 3);
5583 char *font_name_copy = alloca (strlen (fontname) + 1);
5584 char *ptr;
5586 /* Copy fontname so we can modify it during comparison. */
5587 strcpy (font_name_copy, fontname);
5589 ptr = regex;
5590 *ptr++ = '^';
5592 /* Turn pattern into a regexp and do a regexp match. */
5593 for (; *pattern; pattern++)
5595 if (*pattern == '?')
5596 *ptr++ = '.';
5597 else if (*pattern == '*')
5599 *ptr++ = '.';
5600 *ptr++ = '*';
5602 else
5603 *ptr++ = *pattern;
5605 *ptr = '$';
5606 *(ptr + 1) = '\0';
5608 /* Strip out font heights and compare them seperately, since
5609 rounding error can cause mismatches. This also allows a
5610 comparison between a font that declares only a pixel height and a
5611 pattern that declares the point height.
5614 int font_height, pattern_height;
5616 font_height = xlfd_strip_height (font_name_copy);
5617 pattern_height = xlfd_strip_height (regex);
5619 /* Compare now, and don't bother doing expensive regexp matching
5620 if the heights differ. */
5621 if (font_height && pattern_height && (font_height != pattern_height))
5622 return FALSE;
5625 return (fast_c_string_match_ignore_case (build_string (regex),
5626 font_name_copy) >= 0);
5629 /* Callback functions, and a structure holding info they need, for
5630 listing system fonts on W32. We need one set of functions to do the
5631 job properly, but these don't work on NT 3.51 and earlier, so we
5632 have a second set which don't handle character sets properly to
5633 fall back on.
5635 In both cases, there are two passes made. The first pass gets one
5636 font from each family, the second pass lists all the fonts from
5637 each family. */
5639 typedef struct enumfont_t
5641 HDC hdc;
5642 int numFonts;
5643 LOGFONT logfont;
5644 XFontStruct *size_ref;
5645 Lisp_Object pattern;
5646 Lisp_Object list;
5647 } enumfont_t;
5650 static void
5651 enum_font_maybe_add_to_list (enumfont_t *, LOGFONT *, char *, Lisp_Object);
5654 static int CALLBACK
5655 enum_font_cb2 (lplf, lptm, FontType, lpef)
5656 ENUMLOGFONT * lplf;
5657 NEWTEXTMETRIC * lptm;
5658 int FontType;
5659 enumfont_t * lpef;
5661 /* Ignore struck out and underlined versions of fonts. */
5662 if (lplf->elfLogFont.lfStrikeOut || lplf->elfLogFont.lfUnderline)
5663 return 1;
5665 /* Only return fonts with names starting with @ if they were
5666 explicitly specified, since Microsoft uses an initial @ to
5667 denote fonts for vertical writing, without providing a more
5668 convenient way of identifying them. */
5669 if (lplf->elfLogFont.lfFaceName[0] == '@'
5670 && lpef->logfont.lfFaceName[0] != '@')
5671 return 1;
5673 /* Check that the character set matches if it was specified */
5674 if (lpef->logfont.lfCharSet != DEFAULT_CHARSET &&
5675 lplf->elfLogFont.lfCharSet != lpef->logfont.lfCharSet)
5676 return 1;
5678 if (FontType == RASTER_FONTTYPE)
5680 /* DBCS raster fonts have problems displaying, so skip them. */
5681 int charset = lplf->elfLogFont.lfCharSet;
5682 if (charset == SHIFTJIS_CHARSET
5683 || charset == HANGEUL_CHARSET
5684 || charset == CHINESEBIG5_CHARSET
5685 || charset == GB2312_CHARSET
5686 #ifdef JOHAB_CHARSET
5687 || charset == JOHAB_CHARSET
5688 #endif
5690 return 1;
5694 char buf[100];
5695 Lisp_Object width = Qnil;
5696 Lisp_Object charset_list = Qnil;
5697 char *charset = NULL;
5699 /* Truetype fonts do not report their true metrics until loaded */
5700 if (FontType != RASTER_FONTTYPE)
5702 if (!NILP (lpef->pattern))
5704 /* Scalable fonts are as big as you want them to be. */
5705 lplf->elfLogFont.lfHeight = lpef->logfont.lfHeight;
5706 lplf->elfLogFont.lfWidth = lpef->logfont.lfWidth;
5707 width = make_number (lpef->logfont.lfWidth);
5709 else
5711 lplf->elfLogFont.lfHeight = 0;
5712 lplf->elfLogFont.lfWidth = 0;
5716 /* Make sure the height used here is the same as everywhere
5717 else (ie character height, not cell height). */
5718 if (lplf->elfLogFont.lfHeight > 0)
5720 /* lptm can be trusted for RASTER fonts, but not scalable ones. */
5721 if (FontType == RASTER_FONTTYPE)
5722 lplf->elfLogFont.lfHeight = lptm->tmInternalLeading - lptm->tmHeight;
5723 else
5724 lplf->elfLogFont.lfHeight = -lplf->elfLogFont.lfHeight;
5727 if (!NILP (lpef->pattern))
5729 charset = xlfd_charset_of_font (SDATA (lpef->pattern));
5731 /* We already checked charsets above, but DEFAULT_CHARSET
5732 slipped through. So only allow exact matches for DEFAULT_CHARSET. */
5733 if (charset
5734 && strncmp (charset, "*-*", 3) != 0
5735 && lpef->logfont.lfCharSet == DEFAULT_CHARSET
5736 && strcmp (charset, w32_to_x_charset (DEFAULT_CHARSET)) != 0)
5737 return 1;
5740 if (charset)
5741 charset_list = Fcons (build_string (charset), Qnil);
5742 else
5743 charset_list = w32_to_all_x_charsets (lplf->elfLogFont.lfCharSet);
5745 /* Loop through the charsets. */
5746 for ( ; CONSP (charset_list); charset_list = Fcdr (charset_list))
5748 Lisp_Object this_charset = Fcar (charset_list);
5749 charset = SDATA (this_charset);
5751 /* List bold and italic variations if w32-enable-synthesized-fonts
5752 is non-nil and this is a plain font. */
5753 if (w32_enable_synthesized_fonts
5754 && lplf->elfLogFont.lfWeight == FW_NORMAL
5755 && lplf->elfLogFont.lfItalic == FALSE)
5757 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5758 charset, width);
5759 /* bold. */
5760 lplf->elfLogFont.lfWeight = FW_BOLD;
5761 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5762 charset, width);
5763 /* bold italic. */
5764 lplf->elfLogFont.lfItalic = TRUE;
5765 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5766 charset, width);
5767 /* italic. */
5768 lplf->elfLogFont.lfWeight = FW_NORMAL;
5769 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5770 charset, width);
5772 else
5773 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5774 charset, width);
5778 return 1;
5781 static void
5782 enum_font_maybe_add_to_list (lpef, logfont, match_charset, width)
5783 enumfont_t * lpef;
5784 LOGFONT * logfont;
5785 char * match_charset;
5786 Lisp_Object width;
5788 char buf[100];
5790 if (!w32_to_x_font (logfont, buf, 100, match_charset))
5791 return;
5793 if (NILP (lpef->pattern)
5794 || w32_font_match (buf, SDATA (lpef->pattern)))
5796 /* Check if we already listed this font. This may happen if
5797 w32_enable_synthesized_fonts is non-nil, and there are real
5798 bold and italic versions of the font. */
5799 Lisp_Object font_name = build_string (buf);
5800 if (NILP (Fmember (font_name, lpef->list)))
5802 Lisp_Object entry = Fcons (font_name, width);
5803 lpef->list = Fcons (entry, lpef->list);
5804 lpef->numFonts++;
5810 static int CALLBACK
5811 enum_font_cb1 (lplf, lptm, FontType, lpef)
5812 ENUMLOGFONT * lplf;
5813 NEWTEXTMETRIC * lptm;
5814 int FontType;
5815 enumfont_t * lpef;
5817 return EnumFontFamilies (lpef->hdc,
5818 lplf->elfLogFont.lfFaceName,
5819 (FONTENUMPROC) enum_font_cb2,
5820 (LPARAM) lpef);
5824 static int CALLBACK
5825 enum_fontex_cb2 (lplf, lptm, font_type, lpef)
5826 ENUMLOGFONTEX * lplf;
5827 NEWTEXTMETRICEX * lptm;
5828 int font_type;
5829 enumfont_t * lpef;
5831 /* We are not interested in the extra info we get back from the 'Ex
5832 version - only the fact that we get character set variations
5833 enumerated seperately. */
5834 return enum_font_cb2 ((ENUMLOGFONT *) lplf, (NEWTEXTMETRIC *) lptm,
5835 font_type, lpef);
5838 static int CALLBACK
5839 enum_fontex_cb1 (lplf, lptm, font_type, lpef)
5840 ENUMLOGFONTEX * lplf;
5841 NEWTEXTMETRICEX * lptm;
5842 int font_type;
5843 enumfont_t * lpef;
5845 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
5846 FARPROC enum_font_families_ex
5847 = GetProcAddress ( gdi32, "EnumFontFamiliesExA");
5848 /* We don't really expect EnumFontFamiliesEx to disappear once we
5849 get here, so don't bother handling it gracefully. */
5850 if (enum_font_families_ex == NULL)
5851 error ("gdi32.dll has disappeared!");
5852 return enum_font_families_ex (lpef->hdc,
5853 &lplf->elfLogFont,
5854 (FONTENUMPROC) enum_fontex_cb2,
5855 (LPARAM) lpef, 0);
5858 /* Interface to fontset handler. (adapted from mw32font.c in Meadow
5859 and xterm.c in Emacs 20.3) */
5861 static Lisp_Object w32_list_bdf_fonts (Lisp_Object pattern, int max_names)
5863 char *fontname, *ptnstr;
5864 Lisp_Object list, tem, newlist = Qnil;
5865 int n_fonts = 0;
5867 list = Vw32_bdf_filename_alist;
5868 ptnstr = SDATA (pattern);
5870 for ( ; CONSP (list); list = XCDR (list))
5872 tem = XCAR (list);
5873 if (CONSP (tem))
5874 fontname = SDATA (XCAR (tem));
5875 else if (STRINGP (tem))
5876 fontname = SDATA (tem);
5877 else
5878 continue;
5880 if (w32_font_match (fontname, ptnstr))
5882 newlist = Fcons (XCAR (tem), newlist);
5883 n_fonts++;
5884 if (max_names >= 0 && n_fonts >= max_names)
5885 break;
5889 return newlist;
5893 /* Return a list of names of available fonts matching PATTERN on frame
5894 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
5895 to be listed. Frame F NULL means we have not yet created any
5896 frame, which means we can't get proper size info, as we don't have
5897 a device context to use for GetTextMetrics.
5898 MAXNAMES sets a limit on how many fonts to match. If MAXNAMES is
5899 negative, then all matching fonts are returned. */
5901 Lisp_Object
5902 w32_list_fonts (f, pattern, size, maxnames)
5903 struct frame *f;
5904 Lisp_Object pattern;
5905 int size;
5906 int maxnames;
5908 Lisp_Object patterns, key = Qnil, tem, tpat;
5909 Lisp_Object list = Qnil, newlist = Qnil, second_best = Qnil;
5910 struct w32_display_info *dpyinfo = &one_w32_display_info;
5911 int n_fonts = 0;
5913 patterns = Fassoc (pattern, Valternate_fontname_alist);
5914 if (NILP (patterns))
5915 patterns = Fcons (pattern, Qnil);
5917 for (; CONSP (patterns); patterns = XCDR (patterns))
5919 enumfont_t ef;
5920 int codepage;
5922 tpat = XCAR (patterns);
5924 if (!STRINGP (tpat))
5925 continue;
5927 /* Avoid expensive EnumFontFamilies functions if we are not
5928 going to be able to output one of these anyway. */
5929 codepage = w32_codepage_for_font (SDATA (tpat));
5930 if (codepage != CP_8BIT && codepage != CP_UNICODE
5931 && codepage != CP_DEFAULT && codepage != CP_UNKNOWN
5932 && !IsValidCodePage(codepage))
5933 continue;
5935 /* See if we cached the result for this particular query.
5936 The cache is an alist of the form:
5937 ((PATTERN (FONTNAME . WIDTH) ...) ...)
5939 if (tem = XCDR (dpyinfo->name_list_element),
5940 !NILP (list = Fassoc (tpat, tem)))
5942 list = Fcdr_safe (list);
5943 /* We have a cached list. Don't have to get the list again. */
5944 goto label_cached;
5947 BLOCK_INPUT;
5948 /* At first, put PATTERN in the cache. */
5949 ef.pattern = tpat;
5950 ef.list = Qnil;
5951 ef.numFonts = 0;
5953 /* Use EnumFontFamiliesEx where it is available, as it knows
5954 about character sets. Fall back to EnumFontFamilies for
5955 older versions of NT that don't support the 'Ex function. */
5956 x_to_w32_font (SDATA (tpat), &ef.logfont);
5958 LOGFONT font_match_pattern;
5959 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
5960 FARPROC enum_font_families_ex
5961 = GetProcAddress ( gdi32, "EnumFontFamiliesExA");
5963 /* We do our own pattern matching so we can handle wildcards. */
5964 font_match_pattern.lfFaceName[0] = 0;
5965 font_match_pattern.lfPitchAndFamily = 0;
5966 /* We can use the charset, because if it is a wildcard it will
5967 be DEFAULT_CHARSET anyway. */
5968 font_match_pattern.lfCharSet = ef.logfont.lfCharSet;
5970 ef.hdc = GetDC (dpyinfo->root_window);
5972 if (enum_font_families_ex)
5973 enum_font_families_ex (ef.hdc,
5974 &font_match_pattern,
5975 (FONTENUMPROC) enum_fontex_cb1,
5976 (LPARAM) &ef, 0);
5977 else
5978 EnumFontFamilies (ef.hdc, NULL, (FONTENUMPROC) enum_font_cb1,
5979 (LPARAM)&ef);
5981 ReleaseDC (dpyinfo->root_window, ef.hdc);
5984 UNBLOCK_INPUT;
5985 list = ef.list;
5987 /* Make a list of the fonts we got back.
5988 Store that in the font cache for the display. */
5989 XSETCDR (dpyinfo->name_list_element,
5990 Fcons (Fcons (tpat, list),
5991 XCDR (dpyinfo->name_list_element)));
5993 label_cached:
5994 if (NILP (list)) continue; /* Try the remaining alternatives. */
5996 newlist = second_best = Qnil;
5998 /* Make a list of the fonts that have the right width. */
5999 for (; CONSP (list); list = XCDR (list))
6001 int found_size;
6002 tem = XCAR (list);
6004 if (!CONSP (tem))
6005 continue;
6006 if (NILP (XCAR (tem)))
6007 continue;
6008 if (!size)
6010 newlist = Fcons (XCAR (tem), newlist);
6011 n_fonts++;
6012 if (maxnames >= 0 && n_fonts >= maxnames)
6013 break;
6014 else
6015 continue;
6017 if (!INTEGERP (XCDR (tem)))
6019 /* Since we don't yet know the size of the font, we must
6020 load it and try GetTextMetrics. */
6021 W32FontStruct thisinfo;
6022 LOGFONT lf;
6023 HDC hdc;
6024 HANDLE oldobj;
6026 if (!x_to_w32_font (SDATA (XCAR (tem)), &lf))
6027 continue;
6029 BLOCK_INPUT;
6030 thisinfo.bdf = NULL;
6031 thisinfo.hfont = CreateFontIndirect (&lf);
6032 if (thisinfo.hfont == NULL)
6033 continue;
6035 hdc = GetDC (dpyinfo->root_window);
6036 oldobj = SelectObject (hdc, thisinfo.hfont);
6037 if (GetTextMetrics (hdc, &thisinfo.tm))
6038 XSETCDR (tem, make_number (FONT_WIDTH (&thisinfo)));
6039 else
6040 XSETCDR (tem, make_number (0));
6041 SelectObject (hdc, oldobj);
6042 ReleaseDC (dpyinfo->root_window, hdc);
6043 DeleteObject(thisinfo.hfont);
6044 UNBLOCK_INPUT;
6046 found_size = XINT (XCDR (tem));
6047 if (found_size == size)
6049 newlist = Fcons (XCAR (tem), newlist);
6050 n_fonts++;
6051 if (maxnames >= 0 && n_fonts >= maxnames)
6052 break;
6054 /* keep track of the closest matching size in case
6055 no exact match is found. */
6056 else if (found_size > 0)
6058 if (NILP (second_best))
6059 second_best = tem;
6061 else if (found_size < size)
6063 if (XINT (XCDR (second_best)) > size
6064 || XINT (XCDR (second_best)) < found_size)
6065 second_best = tem;
6067 else
6069 if (XINT (XCDR (second_best)) > size
6070 && XINT (XCDR (second_best)) >
6071 found_size)
6072 second_best = tem;
6077 if (!NILP (newlist))
6078 break;
6079 else if (!NILP (second_best))
6081 newlist = Fcons (XCAR (second_best), Qnil);
6082 break;
6086 /* Include any bdf fonts. */
6087 if (n_fonts < maxnames || maxnames < 0)
6089 Lisp_Object combined[2];
6090 combined[0] = w32_list_bdf_fonts (pattern, maxnames - n_fonts);
6091 combined[1] = newlist;
6092 newlist = Fnconc(2, combined);
6095 return newlist;
6099 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
6100 struct font_info *
6101 w32_get_font_info (f, font_idx)
6102 FRAME_PTR f;
6103 int font_idx;
6105 return (FRAME_W32_FONT_TABLE (f) + font_idx);
6109 struct font_info*
6110 w32_query_font (struct frame *f, char *fontname)
6112 int i;
6113 struct font_info *pfi;
6115 pfi = FRAME_W32_FONT_TABLE (f);
6117 for (i = 0; i < one_w32_display_info.n_fonts ;i++, pfi++)
6119 if (strcmp(pfi->name, fontname) == 0) return pfi;
6122 return NULL;
6125 /* Find a CCL program for a font specified by FONTP, and set the member
6126 `encoder' of the structure. */
6128 void
6129 w32_find_ccl_program (fontp)
6130 struct font_info *fontp;
6132 Lisp_Object list, elt;
6134 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
6136 elt = XCAR (list);
6137 if (CONSP (elt)
6138 && STRINGP (XCAR (elt))
6139 && (fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
6140 >= 0))
6141 break;
6143 if (! NILP (list))
6145 struct ccl_program *ccl
6146 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
6148 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
6149 xfree (ccl);
6150 else
6151 fontp->font_encoder = ccl;
6155 /* directory-files from dired.c. */
6156 Lisp_Object Fdirectory_files P_((Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object));
6159 /* Find BDF files in a specified directory. (use GCPRO when calling,
6160 as this calls lisp to get a directory listing). */
6161 static Lisp_Object
6162 w32_find_bdf_fonts_in_dir (Lisp_Object directory)
6164 Lisp_Object filelist, list = Qnil;
6165 char fontname[100];
6167 if (!STRINGP(directory))
6168 return Qnil;
6170 filelist = Fdirectory_files (directory, Qt,
6171 build_string (".*\\.[bB][dD][fF]"), Qt);
6173 for ( ; CONSP(filelist); filelist = XCDR (filelist))
6175 Lisp_Object filename = XCAR (filelist);
6176 if (w32_BDF_to_x_font (SDATA (filename), fontname, 100))
6177 store_in_alist (&list, build_string (fontname), filename);
6179 return list;
6182 DEFUN ("w32-find-bdf-fonts", Fw32_find_bdf_fonts, Sw32_find_bdf_fonts,
6183 1, 1, 0,
6184 doc: /* Return a list of BDF fonts in DIRECTORY.
6185 The list is suitable for appending to `w32-bdf-filename-alist'.
6186 Fonts which do not contain an xlfd description will not be included
6187 in the list. DIRECTORY may be a list of directories. */)
6188 (directory)
6189 Lisp_Object directory;
6191 Lisp_Object list = Qnil;
6192 struct gcpro gcpro1, gcpro2;
6194 if (!CONSP (directory))
6195 return w32_find_bdf_fonts_in_dir (directory);
6197 for ( ; CONSP (directory); directory = XCDR (directory))
6199 Lisp_Object pair[2];
6200 pair[0] = list;
6201 pair[1] = Qnil;
6202 GCPRO2 (directory, list);
6203 pair[1] = w32_find_bdf_fonts_in_dir( XCAR (directory) );
6204 list = Fnconc( 2, pair );
6205 UNGCPRO;
6207 return list;
6211 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
6212 doc: /* Internal function called by `color-defined-p', which see. */)
6213 (color, frame)
6214 Lisp_Object color, frame;
6216 XColor foo;
6217 FRAME_PTR f = check_x_frame (frame);
6219 CHECK_STRING (color);
6221 if (w32_defined_color (f, SDATA (color), &foo, 0))
6222 return Qt;
6223 else
6224 return Qnil;
6227 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
6228 doc: /* Internal function called by `color-values', which see. */)
6229 (color, frame)
6230 Lisp_Object color, frame;
6232 XColor foo;
6233 FRAME_PTR f = check_x_frame (frame);
6235 CHECK_STRING (color);
6237 if (w32_defined_color (f, SDATA (color), &foo, 0))
6239 Lisp_Object rgb[3];
6241 rgb[0] = make_number ((GetRValue (foo.pixel) << 8)
6242 | GetRValue (foo.pixel));
6243 rgb[1] = make_number ((GetGValue (foo.pixel) << 8)
6244 | GetGValue (foo.pixel));
6245 rgb[2] = make_number ((GetBValue (foo.pixel) << 8)
6246 | GetBValue (foo.pixel));
6247 return Flist (3, rgb);
6249 else
6250 return Qnil;
6253 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
6254 doc: /* Internal function called by `display-color-p', which see. */)
6255 (display)
6256 Lisp_Object display;
6258 struct w32_display_info *dpyinfo = check_x_display_info (display);
6260 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 2)
6261 return Qnil;
6263 return Qt;
6266 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p,
6267 Sx_display_grayscale_p, 0, 1, 0,
6268 doc: /* Return t if DISPLAY supports shades of gray.
6269 Note that color displays do support shades of gray.
6270 The optional argument DISPLAY specifies which display to ask about.
6271 DISPLAY should be either a frame or a display name (a string).
6272 If omitted or nil, that stands for the selected frame's display. */)
6273 (display)
6274 Lisp_Object display;
6276 struct w32_display_info *dpyinfo = check_x_display_info (display);
6278 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 1)
6279 return Qnil;
6281 return Qt;
6284 DEFUN ("x-display-pixel-width", Fx_display_pixel_width,
6285 Sx_display_pixel_width, 0, 1, 0,
6286 doc: /* Returns the width in pixels of DISPLAY.
6287 The optional argument DISPLAY specifies which display to ask about.
6288 DISPLAY should be either a frame or a display name (a string).
6289 If omitted or nil, that stands for the selected frame's display. */)
6290 (display)
6291 Lisp_Object display;
6293 struct w32_display_info *dpyinfo = check_x_display_info (display);
6295 return make_number (dpyinfo->width);
6298 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
6299 Sx_display_pixel_height, 0, 1, 0,
6300 doc: /* Returns the height in pixels of DISPLAY.
6301 The optional argument DISPLAY specifies which display to ask about.
6302 DISPLAY should be either a frame or a display name (a string).
6303 If omitted or nil, that stands for the selected frame's display. */)
6304 (display)
6305 Lisp_Object display;
6307 struct w32_display_info *dpyinfo = check_x_display_info (display);
6309 return make_number (dpyinfo->height);
6312 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
6313 0, 1, 0,
6314 doc: /* Returns the number of bitplanes of DISPLAY.
6315 The optional argument DISPLAY specifies which display to ask about.
6316 DISPLAY should be either a frame or a display name (a string).
6317 If omitted or nil, that stands for the selected frame's display. */)
6318 (display)
6319 Lisp_Object display;
6321 struct w32_display_info *dpyinfo = check_x_display_info (display);
6323 return make_number (dpyinfo->n_planes * dpyinfo->n_cbits);
6326 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
6327 0, 1, 0,
6328 doc: /* Returns the number of color cells of DISPLAY.
6329 The optional argument DISPLAY specifies which display to ask about.
6330 DISPLAY should be either a frame or a display name (a string).
6331 If omitted or nil, that stands for the selected frame's display. */)
6332 (display)
6333 Lisp_Object display;
6335 struct w32_display_info *dpyinfo = check_x_display_info (display);
6336 HDC hdc;
6337 int cap;
6339 hdc = GetDC (dpyinfo->root_window);
6340 if (dpyinfo->has_palette)
6341 cap = GetDeviceCaps (hdc, SIZEPALETTE);
6342 else
6343 cap = GetDeviceCaps (hdc, NUMCOLORS);
6345 /* We force 24+ bit depths to 24-bit, both to prevent an overflow
6346 and because probably is more meaningful on Windows anyway */
6347 if (cap < 0)
6348 cap = 1 << min(dpyinfo->n_planes * dpyinfo->n_cbits, 24);
6350 ReleaseDC (dpyinfo->root_window, hdc);
6352 return make_number (cap);
6355 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
6356 Sx_server_max_request_size,
6357 0, 1, 0,
6358 doc: /* Returns the maximum request size of the server of DISPLAY.
6359 The optional argument DISPLAY specifies which display to ask about.
6360 DISPLAY should be either a frame or a display name (a string).
6361 If omitted or nil, that stands for the selected frame's display. */)
6362 (display)
6363 Lisp_Object display;
6365 struct w32_display_info *dpyinfo = check_x_display_info (display);
6367 return make_number (1);
6370 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
6371 doc: /* Returns the vendor ID string of the W32 system (Microsoft).
6372 The optional argument DISPLAY specifies which display to ask about.
6373 DISPLAY should be either a frame or a display name (a string).
6374 If omitted or nil, that stands for the selected frame's display. */)
6375 (display)
6376 Lisp_Object display;
6378 return build_string ("Microsoft Corp.");
6381 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
6382 doc: /* Returns the version numbers of the server of DISPLAY.
6383 The value is a list of three integers: the major and minor
6384 version numbers, and the vendor-specific release
6385 number. See also the function `x-server-vendor'.
6387 The optional argument DISPLAY specifies which display to ask about.
6388 DISPLAY should be either a frame or a display name (a string).
6389 If omitted or nil, that stands for the selected frame's display. */)
6390 (display)
6391 Lisp_Object display;
6393 return Fcons (make_number (w32_major_version),
6394 Fcons (make_number (w32_minor_version),
6395 Fcons (make_number (w32_build_number), Qnil)));
6398 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
6399 doc: /* Returns the number of screens on the server of DISPLAY.
6400 The optional argument DISPLAY specifies which display to ask about.
6401 DISPLAY should be either a frame or a display name (a string).
6402 If omitted or nil, that stands for the selected frame's display. */)
6403 (display)
6404 Lisp_Object display;
6406 return make_number (1);
6409 DEFUN ("x-display-mm-height", Fx_display_mm_height,
6410 Sx_display_mm_height, 0, 1, 0,
6411 doc: /* Returns the height in millimeters of DISPLAY.
6412 The optional argument DISPLAY specifies which display to ask about.
6413 DISPLAY should be either a frame or a display name (a string).
6414 If omitted or nil, that stands for the selected frame's display. */)
6415 (display)
6416 Lisp_Object display;
6418 struct w32_display_info *dpyinfo = check_x_display_info (display);
6419 HDC hdc;
6420 int cap;
6422 hdc = GetDC (dpyinfo->root_window);
6424 cap = GetDeviceCaps (hdc, VERTSIZE);
6426 ReleaseDC (dpyinfo->root_window, hdc);
6428 return make_number (cap);
6431 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
6432 doc: /* Returns the width in millimeters of DISPLAY.
6433 The optional argument DISPLAY specifies which display to ask about.
6434 DISPLAY should be either a frame or a display name (a string).
6435 If omitted or nil, that stands for the selected frame's display. */)
6436 (display)
6437 Lisp_Object display;
6439 struct w32_display_info *dpyinfo = check_x_display_info (display);
6441 HDC hdc;
6442 int cap;
6444 hdc = GetDC (dpyinfo->root_window);
6446 cap = GetDeviceCaps (hdc, HORZSIZE);
6448 ReleaseDC (dpyinfo->root_window, hdc);
6450 return make_number (cap);
6453 DEFUN ("x-display-backing-store", Fx_display_backing_store,
6454 Sx_display_backing_store, 0, 1, 0,
6455 doc: /* Returns an indication of whether DISPLAY does backing store.
6456 The value may be `always', `when-mapped', or `not-useful'.
6457 The optional argument DISPLAY specifies which display to ask about.
6458 DISPLAY should be either a frame or a display name (a string).
6459 If omitted or nil, that stands for the selected frame's display. */)
6460 (display)
6461 Lisp_Object display;
6463 return intern ("not-useful");
6466 DEFUN ("x-display-visual-class", Fx_display_visual_class,
6467 Sx_display_visual_class, 0, 1, 0,
6468 doc: /* Returns the visual class of DISPLAY.
6469 The value is one of the symbols `static-gray', `gray-scale',
6470 `static-color', `pseudo-color', `true-color', or `direct-color'.
6472 The optional argument DISPLAY specifies which display to ask about.
6473 DISPLAY should be either a frame or a display name (a string).
6474 If omitted or nil, that stands for the selected frame's display. */)
6475 (display)
6476 Lisp_Object display;
6478 struct w32_display_info *dpyinfo = check_x_display_info (display);
6479 Lisp_Object result = Qnil;
6481 if (dpyinfo->has_palette)
6482 result = intern ("pseudo-color");
6483 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 1)
6484 result = intern ("static-grey");
6485 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 4)
6486 result = intern ("static-color");
6487 else if (dpyinfo->n_planes * dpyinfo->n_cbits > 8)
6488 result = intern ("true-color");
6490 return result;
6493 DEFUN ("x-display-save-under", Fx_display_save_under,
6494 Sx_display_save_under, 0, 1, 0,
6495 doc: /* Returns t if DISPLAY supports the save-under feature.
6496 The optional argument DISPLAY specifies which display to ask about.
6497 DISPLAY should be either a frame or a display name (a string).
6498 If omitted or nil, that stands for the selected frame's display. */)
6499 (display)
6500 Lisp_Object display;
6502 return Qnil;
6506 x_pixel_width (f)
6507 register struct frame *f;
6509 return FRAME_PIXEL_WIDTH (f);
6513 x_pixel_height (f)
6514 register struct frame *f;
6516 return FRAME_PIXEL_HEIGHT (f);
6520 x_char_width (f)
6521 register struct frame *f;
6523 return FRAME_COLUMN_WIDTH (f);
6527 x_char_height (f)
6528 register struct frame *f;
6530 return FRAME_LINE_HEIGHT (f);
6534 x_screen_planes (f)
6535 register struct frame *f;
6537 return FRAME_W32_DISPLAY_INFO (f)->n_planes;
6540 /* Return the display structure for the display named NAME.
6541 Open a new connection if necessary. */
6543 struct w32_display_info *
6544 x_display_info_for_name (name)
6545 Lisp_Object name;
6547 Lisp_Object names;
6548 struct w32_display_info *dpyinfo;
6550 CHECK_STRING (name);
6552 for (dpyinfo = &one_w32_display_info, names = w32_display_name_list;
6553 dpyinfo;
6554 dpyinfo = dpyinfo->next, names = XCDR (names))
6556 Lisp_Object tem;
6557 tem = Fstring_equal (XCAR (XCAR (names)), name);
6558 if (!NILP (tem))
6559 return dpyinfo;
6562 /* Use this general default value to start with. */
6563 Vx_resource_name = Vinvocation_name;
6565 validate_x_resource_name ();
6567 dpyinfo = w32_term_init (name, (unsigned char *)0,
6568 (char *) SDATA (Vx_resource_name));
6570 if (dpyinfo == 0)
6571 error ("Cannot connect to server %s", SDATA (name));
6573 w32_in_use = 1;
6574 XSETFASTINT (Vwindow_system_version, 3);
6576 return dpyinfo;
6579 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
6580 1, 3, 0, doc: /* Open a connection to a server.
6581 DISPLAY is the name of the display to connect to.
6582 Optional second arg XRM-STRING is a string of resources in xrdb format.
6583 If the optional third arg MUST-SUCCEED is non-nil,
6584 terminate Emacs if we can't open the connection. */)
6585 (display, xrm_string, must_succeed)
6586 Lisp_Object display, xrm_string, must_succeed;
6588 unsigned char *xrm_option;
6589 struct w32_display_info *dpyinfo;
6591 /* If initialization has already been done, return now to avoid
6592 overwriting critical parts of one_w32_display_info. */
6593 if (w32_in_use)
6594 return Qnil;
6596 CHECK_STRING (display);
6597 if (! NILP (xrm_string))
6598 CHECK_STRING (xrm_string);
6600 if (! EQ (Vwindow_system, intern ("w32")))
6601 error ("Not using Microsoft Windows");
6603 /* Allow color mapping to be defined externally; first look in user's
6604 HOME directory, then in Emacs etc dir for a file called rgb.txt. */
6606 Lisp_Object color_file;
6607 struct gcpro gcpro1;
6609 color_file = build_string("~/rgb.txt");
6611 GCPRO1 (color_file);
6613 if (NILP (Ffile_readable_p (color_file)))
6614 color_file =
6615 Fexpand_file_name (build_string ("rgb.txt"),
6616 Fsymbol_value (intern ("data-directory")));
6618 Vw32_color_map = Fw32_load_color_file (color_file);
6620 UNGCPRO;
6622 if (NILP (Vw32_color_map))
6623 Vw32_color_map = Fw32_default_color_map ();
6625 /* Merge in system logical colors. */
6626 add_system_logical_colors_to_map (&Vw32_color_map);
6628 if (! NILP (xrm_string))
6629 xrm_option = (unsigned char *) SDATA (xrm_string);
6630 else
6631 xrm_option = (unsigned char *) 0;
6633 /* Use this general default value to start with. */
6634 /* First remove .exe suffix from invocation-name - it looks ugly. */
6636 char basename[ MAX_PATH ], *str;
6638 strcpy (basename, SDATA (Vinvocation_name));
6639 str = strrchr (basename, '.');
6640 if (str) *str = 0;
6641 Vinvocation_name = build_string (basename);
6643 Vx_resource_name = Vinvocation_name;
6645 validate_x_resource_name ();
6647 /* This is what opens the connection and sets x_current_display.
6648 This also initializes many symbols, such as those used for input. */
6649 dpyinfo = w32_term_init (display, xrm_option,
6650 (char *) SDATA (Vx_resource_name));
6652 if (dpyinfo == 0)
6654 if (!NILP (must_succeed))
6655 fatal ("Cannot connect to server %s.\n",
6656 SDATA (display));
6657 else
6658 error ("Cannot connect to server %s", SDATA (display));
6661 w32_in_use = 1;
6663 XSETFASTINT (Vwindow_system_version, 3);
6664 return Qnil;
6667 DEFUN ("x-close-connection", Fx_close_connection,
6668 Sx_close_connection, 1, 1, 0,
6669 doc: /* Close the connection to DISPLAY's server.
6670 For DISPLAY, specify either a frame or a display name (a string).
6671 If DISPLAY is nil, that stands for the selected frame's display. */)
6672 (display)
6673 Lisp_Object display;
6675 struct w32_display_info *dpyinfo = check_x_display_info (display);
6676 int i;
6678 if (dpyinfo->reference_count > 0)
6679 error ("Display still has frames on it");
6681 BLOCK_INPUT;
6682 /* Free the fonts in the font table. */
6683 for (i = 0; i < dpyinfo->n_fonts; i++)
6684 if (dpyinfo->font_table[i].name)
6686 if (dpyinfo->font_table[i].name != dpyinfo->font_table[i].full_name)
6687 xfree (dpyinfo->font_table[i].full_name);
6688 xfree (dpyinfo->font_table[i].name);
6689 w32_unload_font (dpyinfo, dpyinfo->font_table[i].font);
6691 x_destroy_all_bitmaps (dpyinfo);
6693 x_delete_display (dpyinfo);
6694 UNBLOCK_INPUT;
6696 return Qnil;
6699 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
6700 doc: /* Return the list of display names that Emacs has connections to. */)
6703 Lisp_Object tail, result;
6705 result = Qnil;
6706 for (tail = w32_display_name_list; ! NILP (tail); tail = XCDR (tail))
6707 result = Fcons (XCAR (XCAR (tail)), result);
6709 return result;
6712 DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
6713 doc: /* This is a noop on W32 systems. */)
6714 (on, display)
6715 Lisp_Object display, on;
6717 return Qnil;
6722 /***********************************************************************
6723 Window properties
6724 ***********************************************************************/
6726 DEFUN ("x-change-window-property", Fx_change_window_property,
6727 Sx_change_window_property, 2, 6, 0,
6728 doc: /* Change window property PROP to VALUE on the X window of FRAME.
6729 VALUE may be a string or a list of conses, numbers and/or strings.
6730 If an element in the list is a string, it is converted to
6731 an Atom and the value of the Atom is used. If an element is a cons,
6732 it is converted to a 32 bit number where the car is the 16 top bits and the
6733 cdr is the lower 16 bits.
6734 FRAME nil or omitted means use the selected frame.
6735 If TYPE is given and non-nil, it is the name of the type of VALUE.
6736 If TYPE is not given or nil, the type is STRING.
6737 FORMAT gives the size in bits of each element if VALUE is a list.
6738 It must be one of 8, 16 or 32.
6739 If VALUE is a string or FORMAT is nil or not given, FORMAT defaults to 8.
6740 If OUTER_P is non-nil, the property is changed for the outer X window of
6741 FRAME. Default is to change on the edit X window.
6743 Value is VALUE. */)
6744 (prop, value, frame, type, format, outer_p)
6745 Lisp_Object prop, value, frame, type, format, outer_p;
6747 #if 0 /* TODO : port window properties to W32 */
6748 struct frame *f = check_x_frame (frame);
6749 Atom prop_atom;
6751 CHECK_STRING (prop);
6752 CHECK_STRING (value);
6754 BLOCK_INPUT;
6755 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
6756 XChangeProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
6757 prop_atom, XA_STRING, 8, PropModeReplace,
6758 SDATA (value), SCHARS (value));
6760 /* Make sure the property is set when we return. */
6761 XFlush (FRAME_W32_DISPLAY (f));
6762 UNBLOCK_INPUT;
6764 #endif /* TODO */
6766 return value;
6770 DEFUN ("x-delete-window-property", Fx_delete_window_property,
6771 Sx_delete_window_property, 1, 2, 0,
6772 doc: /* Remove window property PROP from X window of FRAME.
6773 FRAME nil or omitted means use the selected frame. Value is PROP. */)
6774 (prop, frame)
6775 Lisp_Object prop, frame;
6777 #if 0 /* TODO : port window properties to W32 */
6779 struct frame *f = check_x_frame (frame);
6780 Atom prop_atom;
6782 CHECK_STRING (prop);
6783 BLOCK_INPUT;
6784 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
6785 XDeleteProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), prop_atom);
6787 /* Make sure the property is removed when we return. */
6788 XFlush (FRAME_W32_DISPLAY (f));
6789 UNBLOCK_INPUT;
6790 #endif /* TODO */
6792 return prop;
6796 DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
6797 1, 2, 0,
6798 doc: /* Value is the value of window property PROP on FRAME.
6799 If FRAME is nil or omitted, use the selected frame. Value is nil
6800 if FRAME hasn't a property with name PROP or if PROP has no string
6801 value. */)
6802 (prop, frame)
6803 Lisp_Object prop, frame;
6805 #if 0 /* TODO : port window properties to W32 */
6807 struct frame *f = check_x_frame (frame);
6808 Atom prop_atom;
6809 int rc;
6810 Lisp_Object prop_value = Qnil;
6811 char *tmp_data = NULL;
6812 Atom actual_type;
6813 int actual_format;
6814 unsigned long actual_size, bytes_remaining;
6816 CHECK_STRING (prop);
6817 BLOCK_INPUT;
6818 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
6819 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
6820 prop_atom, 0, 0, False, XA_STRING,
6821 &actual_type, &actual_format, &actual_size,
6822 &bytes_remaining, (unsigned char **) &tmp_data);
6823 if (rc == Success)
6825 int size = bytes_remaining;
6827 XFree (tmp_data);
6828 tmp_data = NULL;
6830 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
6831 prop_atom, 0, bytes_remaining,
6832 False, XA_STRING,
6833 &actual_type, &actual_format,
6834 &actual_size, &bytes_remaining,
6835 (unsigned char **) &tmp_data);
6836 if (rc == Success)
6837 prop_value = make_string (tmp_data, size);
6839 XFree (tmp_data);
6842 UNBLOCK_INPUT;
6844 return prop_value;
6846 #endif /* TODO */
6847 return Qnil;
6852 /***********************************************************************
6853 Busy cursor
6854 ***********************************************************************/
6856 /* If non-null, an asynchronous timer that, when it expires, displays
6857 an hourglass cursor on all frames. */
6859 static struct atimer *hourglass_atimer;
6861 /* Non-zero means an hourglass cursor is currently shown. */
6863 static int hourglass_shown_p;
6865 /* Number of seconds to wait before displaying an hourglass cursor. */
6867 static Lisp_Object Vhourglass_delay;
6869 /* Default number of seconds to wait before displaying an hourglass
6870 cursor. */
6872 #define DEFAULT_HOURGLASS_DELAY 1
6874 /* Function prototypes. */
6876 static void show_hourglass P_ ((struct atimer *));
6877 static void hide_hourglass P_ ((void));
6880 /* Cancel a currently active hourglass timer, and start a new one. */
6882 void
6883 start_hourglass ()
6885 #if 0 /* TODO: cursor shape changes. */
6886 EMACS_TIME delay;
6887 int secs, usecs = 0;
6889 cancel_hourglass ();
6891 if (INTEGERP (Vhourglass_delay)
6892 && XINT (Vhourglass_delay) > 0)
6893 secs = XFASTINT (Vhourglass_delay);
6894 else if (FLOATP (Vhourglass_delay)
6895 && XFLOAT_DATA (Vhourglass_delay) > 0)
6897 Lisp_Object tem;
6898 tem = Ftruncate (Vhourglass_delay, Qnil);
6899 secs = XFASTINT (tem);
6900 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
6902 else
6903 secs = DEFAULT_HOURGLASS_DELAY;
6905 EMACS_SET_SECS_USECS (delay, secs, usecs);
6906 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
6907 show_hourglass, NULL);
6908 #endif
6912 /* Cancel the hourglass cursor timer if active, hide an hourglass
6913 cursor if shown. */
6915 void
6916 cancel_hourglass ()
6918 if (hourglass_atimer)
6920 cancel_atimer (hourglass_atimer);
6921 hourglass_atimer = NULL;
6924 if (hourglass_shown_p)
6925 hide_hourglass ();
6929 /* Timer function of hourglass_atimer. TIMER is equal to
6930 hourglass_atimer.
6932 Display an hourglass cursor on all frames by mapping the frames'
6933 hourglass_window. Set the hourglass_p flag in the frames'
6934 output_data.x structure to indicate that an hourglass cursor is
6935 shown on the frames. */
6937 static void
6938 show_hourglass (timer)
6939 struct atimer *timer;
6941 #if 0 /* TODO: cursor shape changes. */
6942 /* The timer implementation will cancel this timer automatically
6943 after this function has run. Set hourglass_atimer to null
6944 so that we know the timer doesn't have to be canceled. */
6945 hourglass_atimer = NULL;
6947 if (!hourglass_shown_p)
6949 Lisp_Object rest, frame;
6951 BLOCK_INPUT;
6953 FOR_EACH_FRAME (rest, frame)
6954 if (FRAME_W32_P (XFRAME (frame)))
6956 struct frame *f = XFRAME (frame);
6958 f->output_data.w32->hourglass_p = 1;
6960 if (!f->output_data.w32->hourglass_window)
6962 unsigned long mask = CWCursor;
6963 XSetWindowAttributes attrs;
6965 attrs.cursor = f->output_data.w32->hourglass_cursor;
6967 f->output_data.w32->hourglass_window
6968 = XCreateWindow (FRAME_X_DISPLAY (f),
6969 FRAME_OUTER_WINDOW (f),
6970 0, 0, 32000, 32000, 0, 0,
6971 InputOnly,
6972 CopyFromParent,
6973 mask, &attrs);
6976 XMapRaised (FRAME_X_DISPLAY (f),
6977 f->output_data.w32->hourglass_window);
6978 XFlush (FRAME_X_DISPLAY (f));
6981 hourglass_shown_p = 1;
6982 UNBLOCK_INPUT;
6984 #endif
6988 /* Hide the hourglass cursor on all frames, if it is currently shown. */
6990 static void
6991 hide_hourglass ()
6993 #if 0 /* TODO: cursor shape changes. */
6994 if (hourglass_shown_p)
6996 Lisp_Object rest, frame;
6998 BLOCK_INPUT;
6999 FOR_EACH_FRAME (rest, frame)
7001 struct frame *f = XFRAME (frame);
7003 if (FRAME_W32_P (f)
7004 /* Watch out for newly created frames. */
7005 && f->output_data.x->hourglass_window)
7007 XUnmapWindow (FRAME_X_DISPLAY (f),
7008 f->output_data.x->hourglass_window);
7009 /* Sync here because XTread_socket looks at the
7010 hourglass_p flag that is reset to zero below. */
7011 XSync (FRAME_X_DISPLAY (f), False);
7012 f->output_data.x->hourglass_p = 0;
7016 hourglass_shown_p = 0;
7017 UNBLOCK_INPUT;
7019 #endif
7024 /***********************************************************************
7025 Tool tips
7026 ***********************************************************************/
7028 static Lisp_Object x_create_tip_frame P_ ((struct w32_display_info *,
7029 Lisp_Object, Lisp_Object));
7030 static void compute_tip_xy P_ ((struct frame *, Lisp_Object, Lisp_Object,
7031 Lisp_Object, int, int, int *, int *));
7033 /* The frame of a currently visible tooltip. */
7035 Lisp_Object tip_frame;
7037 /* If non-nil, a timer started that hides the last tooltip when it
7038 fires. */
7040 Lisp_Object tip_timer;
7041 Window tip_window;
7043 /* If non-nil, a vector of 3 elements containing the last args
7044 with which x-show-tip was called. See there. */
7046 Lisp_Object last_show_tip_args;
7048 /* Maximum size for tooltips; a cons (COLUMNS . ROWS). */
7050 Lisp_Object Vx_max_tooltip_size;
7053 static Lisp_Object
7054 unwind_create_tip_frame (frame)
7055 Lisp_Object frame;
7057 Lisp_Object deleted;
7059 deleted = unwind_create_frame (frame);
7060 if (EQ (deleted, Qt))
7062 tip_window = NULL;
7063 tip_frame = Qnil;
7066 return deleted;
7070 /* Create a frame for a tooltip on the display described by DPYINFO.
7071 PARMS is a list of frame parameters. TEXT is the string to
7072 display in the tip frame. Value is the frame.
7074 Note that functions called here, esp. x_default_parameter can
7075 signal errors, for instance when a specified color name is
7076 undefined. We have to make sure that we're in a consistent state
7077 when this happens. */
7079 static Lisp_Object
7080 x_create_tip_frame (dpyinfo, parms, text)
7081 struct w32_display_info *dpyinfo;
7082 Lisp_Object parms, text;
7084 struct frame *f;
7085 Lisp_Object frame, tem;
7086 Lisp_Object name;
7087 long window_prompting = 0;
7088 int width, height;
7089 int count = SPECPDL_INDEX ();
7090 struct gcpro gcpro1, gcpro2, gcpro3;
7091 struct kboard *kb;
7092 int face_change_count_before = face_change_count;
7093 Lisp_Object buffer;
7094 struct buffer *old_buffer;
7096 check_w32 ();
7098 /* Use this general default value to start with until we know if
7099 this frame has a specified name. */
7100 Vx_resource_name = Vinvocation_name;
7102 #ifdef MULTI_KBOARD
7103 kb = dpyinfo->kboard;
7104 #else
7105 kb = &the_only_kboard;
7106 #endif
7108 /* Get the name of the frame to use for resource lookup. */
7109 name = w32_get_arg (parms, Qname, "name", "Name", RES_TYPE_STRING);
7110 if (!STRINGP (name)
7111 && !EQ (name, Qunbound)
7112 && !NILP (name))
7113 error ("Invalid frame name--not a string or nil");
7114 Vx_resource_name = name;
7116 frame = Qnil;
7117 GCPRO3 (parms, name, frame);
7118 /* Make a frame without minibuffer nor mode-line. */
7119 f = make_frame (0);
7120 f->wants_modeline = 0;
7121 XSETFRAME (frame, f);
7123 buffer = Fget_buffer_create (build_string (" *tip*"));
7124 Fset_window_buffer (FRAME_ROOT_WINDOW (f), buffer, Qnil);
7125 old_buffer = current_buffer;
7126 set_buffer_internal_1 (XBUFFER (buffer));
7127 current_buffer->truncate_lines = Qnil;
7128 specbind (Qinhibit_read_only, Qt);
7129 specbind (Qinhibit_modification_hooks, Qt);
7130 Ferase_buffer ();
7131 Finsert (1, &text);
7132 set_buffer_internal_1 (old_buffer);
7134 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
7135 record_unwind_protect (unwind_create_tip_frame, frame);
7137 /* By setting the output method, we're essentially saying that
7138 the frame is live, as per FRAME_LIVE_P. If we get a signal
7139 from this point on, x_destroy_window might screw up reference
7140 counts etc. */
7141 f->output_method = output_w32;
7142 f->output_data.w32 =
7143 (struct w32_output *) xmalloc (sizeof (struct w32_output));
7144 bzero (f->output_data.w32, sizeof (struct w32_output));
7146 FRAME_FONTSET (f) = -1;
7147 f->icon_name = Qnil;
7149 #if 0 /* GLYPH_DEBUG TODO: image support. */
7150 image_cache_refcount = FRAME_X_IMAGE_CACHE (f)->refcount;
7151 dpyinfo_refcount = dpyinfo->reference_count;
7152 #endif /* GLYPH_DEBUG */
7153 #ifdef MULTI_KBOARD
7154 FRAME_KBOARD (f) = kb;
7155 #endif
7156 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
7157 f->output_data.w32->explicit_parent = 0;
7159 /* Set the name; the functions to which we pass f expect the name to
7160 be set. */
7161 if (EQ (name, Qunbound) || NILP (name))
7163 f->name = build_string (dpyinfo->w32_id_name);
7164 f->explicit_name = 0;
7166 else
7168 f->name = name;
7169 f->explicit_name = 1;
7170 /* use the frame's title when getting resources for this frame. */
7171 specbind (Qx_resource_name, name);
7174 /* Extract the window parameters from the supplied values
7175 that are needed to determine window geometry. */
7177 Lisp_Object font;
7179 font = w32_get_arg (parms, Qfont, "font", "Font", RES_TYPE_STRING);
7181 BLOCK_INPUT;
7182 /* First, try whatever font the caller has specified. */
7183 if (STRINGP (font))
7185 tem = Fquery_fontset (font, Qnil);
7186 if (STRINGP (tem))
7187 font = x_new_fontset (f, SDATA (tem));
7188 else
7189 font = x_new_font (f, SDATA (font));
7192 /* Try out a font which we hope has bold and italic variations. */
7193 if (!STRINGP (font))
7194 font = x_new_font (f, "-*-Courier New-normal-r-*-*-*-100-*-*-c-*-iso8859-1");
7195 if (! STRINGP (font))
7196 font = x_new_font (f, "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1");
7197 /* If those didn't work, look for something which will at least work. */
7198 if (! STRINGP (font))
7199 font = x_new_font (f, "-*-Fixedsys-normal-r-*-*-12-*-*-*-c-*-iso8859-1");
7200 UNBLOCK_INPUT;
7201 if (! STRINGP (font))
7202 font = build_string ("Fixedsys");
7204 x_default_parameter (f, parms, Qfont, font,
7205 "font", "Font", RES_TYPE_STRING);
7208 x_default_parameter (f, parms, Qborder_width, make_number (2),
7209 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
7210 /* This defaults to 2 in order to match xterm. We recognize either
7211 internalBorderWidth or internalBorder (which is what xterm calls
7212 it). */
7213 if (NILP (Fassq (Qinternal_border_width, parms)))
7215 Lisp_Object value;
7217 value = w32_get_arg (parms, Qinternal_border_width,
7218 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
7219 if (! EQ (value, Qunbound))
7220 parms = Fcons (Fcons (Qinternal_border_width, value),
7221 parms);
7223 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
7224 "internalBorderWidth", "internalBorderWidth",
7225 RES_TYPE_NUMBER);
7227 /* Also do the stuff which must be set before the window exists. */
7228 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
7229 "foreground", "Foreground", RES_TYPE_STRING);
7230 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
7231 "background", "Background", RES_TYPE_STRING);
7232 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
7233 "pointerColor", "Foreground", RES_TYPE_STRING);
7234 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
7235 "cursorColor", "Foreground", RES_TYPE_STRING);
7236 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
7237 "borderColor", "BorderColor", RES_TYPE_STRING);
7239 /* Init faces before x_default_parameter is called for scroll-bar
7240 parameters because that function calls x_set_scroll_bar_width,
7241 which calls change_frame_size, which calls Fset_window_buffer,
7242 which runs hooks, which call Fvertical_motion. At the end, we
7243 end up in init_iterator with a null face cache, which should not
7244 happen. */
7245 init_frame_faces (f);
7247 f->output_data.w32->dwStyle = WS_BORDER | WS_POPUP | WS_DISABLED;
7248 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
7250 window_prompting = x_figure_window_size (f, parms, 0);
7252 /* No fringes on tip frame. */
7253 f->fringe_cols = 0;
7254 f->left_fringe_width = 0;
7255 f->right_fringe_width = 0;
7257 BLOCK_INPUT;
7258 my_create_tip_window (f);
7259 UNBLOCK_INPUT;
7261 x_make_gc (f);
7263 x_default_parameter (f, parms, Qauto_raise, Qnil,
7264 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
7265 x_default_parameter (f, parms, Qauto_lower, Qnil,
7266 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
7267 x_default_parameter (f, parms, Qcursor_type, Qbox,
7268 "cursorType", "CursorType", RES_TYPE_SYMBOL);
7270 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
7271 Change will not be effected unless different from the current
7272 FRAME_LINES (f). */
7273 width = FRAME_COLS (f);
7274 height = FRAME_LINES (f);
7275 FRAME_LINES (f) = 0;
7276 SET_FRAME_COLS (f, 0);
7277 change_frame_size (f, height, width, 1, 0, 0);
7279 /* Add `tooltip' frame parameter's default value. */
7280 if (NILP (Fframe_parameter (frame, intern ("tooltip"))))
7281 Fmodify_frame_parameters (frame, Fcons (Fcons (intern ("tooltip"), Qt),
7282 Qnil));
7284 /* Set up faces after all frame parameters are known. This call
7285 also merges in face attributes specified for new frames.
7287 Frame parameters may be changed if .Xdefaults contains
7288 specifications for the default font. For example, if there is an
7289 `Emacs.default.attributeBackground: pink', the `background-color'
7290 attribute of the frame get's set, which let's the internal border
7291 of the tooltip frame appear in pink. Prevent this. */
7293 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
7295 /* Set tip_frame here, so that */
7296 tip_frame = frame;
7297 call1 (Qface_set_after_frame_default, frame);
7299 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
7300 Fmodify_frame_parameters (frame, Fcons (Fcons (Qbackground_color, bg),
7301 Qnil));
7304 f->no_split = 1;
7306 UNGCPRO;
7308 /* It is now ok to make the frame official even if we get an error
7309 below. And the frame needs to be on Vframe_list or making it
7310 visible won't work. */
7311 Vframe_list = Fcons (frame, Vframe_list);
7313 /* Now that the frame is official, it counts as a reference to
7314 its display. */
7315 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
7317 /* Setting attributes of faces of the tooltip frame from resources
7318 and similar will increment face_change_count, which leads to the
7319 clearing of all current matrices. Since this isn't necessary
7320 here, avoid it by resetting face_change_count to the value it
7321 had before we created the tip frame. */
7322 face_change_count = face_change_count_before;
7324 /* Discard the unwind_protect. */
7325 return unbind_to (count, frame);
7329 /* Compute where to display tip frame F. PARMS is the list of frame
7330 parameters for F. DX and DY are specified offsets from the current
7331 location of the mouse. WIDTH and HEIGHT are the width and height
7332 of the tooltip. Return coordinates relative to the root window of
7333 the display in *ROOT_X, and *ROOT_Y. */
7335 static void
7336 compute_tip_xy (f, parms, dx, dy, width, height, root_x, root_y)
7337 struct frame *f;
7338 Lisp_Object parms, dx, dy;
7339 int width, height;
7340 int *root_x, *root_y;
7342 Lisp_Object left, top;
7344 /* User-specified position? */
7345 left = Fcdr (Fassq (Qleft, parms));
7346 top = Fcdr (Fassq (Qtop, parms));
7348 /* Move the tooltip window where the mouse pointer is. Resize and
7349 show it. */
7350 if (!INTEGERP (left) || !INTEGERP (top))
7352 POINT pt;
7354 BLOCK_INPUT;
7355 GetCursorPos (&pt);
7356 *root_x = pt.x;
7357 *root_y = pt.y;
7358 UNBLOCK_INPUT;
7361 if (INTEGERP (top))
7362 *root_y = XINT (top);
7363 else if (*root_y + XINT (dy) - height < 0)
7364 *root_y -= XINT (dy);
7365 else
7367 *root_y -= height;
7368 *root_y += XINT (dy);
7371 if (INTEGERP (left))
7372 *root_x = XINT (left);
7373 else if (*root_x + XINT (dx) + width <= FRAME_W32_DISPLAY_INFO (f)->width)
7374 /* It fits to the right of the pointer. */
7375 *root_x += XINT (dx);
7376 else if (width + XINT (dx) <= *root_x)
7377 /* It fits to the left of the pointer. */
7378 *root_x -= width + XINT (dx);
7379 else
7380 /* Put it left justified on the screen -- it ought to fit that way. */
7381 *root_x = 0;
7385 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
7386 doc: /* Show STRING in a \"tooltip\" window on frame FRAME.
7387 A tooltip window is a small window displaying a string.
7389 FRAME nil or omitted means use the selected frame.
7391 PARMS is an optional list of frame parameters which can be
7392 used to change the tooltip's appearance.
7394 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
7395 means use the default timeout of 5 seconds.
7397 If the list of frame parameters PARMS contains a `left' parameter,
7398 the tooltip is displayed at that x-position. Otherwise it is
7399 displayed at the mouse position, with offset DX added (default is 5 if
7400 DX isn't specified). Likewise for the y-position; if a `top' frame
7401 parameter is specified, it determines the y-position of the tooltip
7402 window, otherwise it is displayed at the mouse position, with offset
7403 DY added (default is -10).
7405 A tooltip's maximum size is specified by `x-max-tooltip-size'.
7406 Text larger than the specified size is clipped. */)
7407 (string, frame, parms, timeout, dx, dy)
7408 Lisp_Object string, frame, parms, timeout, dx, dy;
7410 struct frame *f;
7411 struct window *w;
7412 int root_x, root_y;
7413 struct buffer *old_buffer;
7414 struct text_pos pos;
7415 int i, width, height;
7416 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7417 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7418 int count = SPECPDL_INDEX ();
7420 specbind (Qinhibit_redisplay, Qt);
7422 GCPRO4 (string, parms, frame, timeout);
7424 CHECK_STRING (string);
7425 f = check_x_frame (frame);
7426 if (NILP (timeout))
7427 timeout = make_number (5);
7428 else
7429 CHECK_NATNUM (timeout);
7431 if (NILP (dx))
7432 dx = make_number (5);
7433 else
7434 CHECK_NUMBER (dx);
7436 if (NILP (dy))
7437 dy = make_number (-10);
7438 else
7439 CHECK_NUMBER (dy);
7441 if (NILP (last_show_tip_args))
7442 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
7444 if (!NILP (tip_frame))
7446 Lisp_Object last_string = AREF (last_show_tip_args, 0);
7447 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
7448 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
7450 if (EQ (frame, last_frame)
7451 && !NILP (Fequal (last_string, string))
7452 && !NILP (Fequal (last_parms, parms)))
7454 struct frame *f = XFRAME (tip_frame);
7456 /* Only DX and DY have changed. */
7457 if (!NILP (tip_timer))
7459 Lisp_Object timer = tip_timer;
7460 tip_timer = Qnil;
7461 call1 (Qcancel_timer, timer);
7464 BLOCK_INPUT;
7465 compute_tip_xy (f, parms, dx, dy, FRAME_PIXEL_WIDTH (f),
7466 FRAME_PIXEL_HEIGHT (f), &root_x, &root_y);
7468 /* Put tooltip in topmost group and in position. */
7469 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
7470 root_x, root_y, 0, 0,
7471 SWP_NOSIZE | SWP_NOACTIVATE);
7473 /* Ensure tooltip is on top of other topmost windows (eg menus). */
7474 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
7475 0, 0, 0, 0,
7476 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
7478 UNBLOCK_INPUT;
7479 goto start_timer;
7483 /* Hide a previous tip, if any. */
7484 Fx_hide_tip ();
7486 ASET (last_show_tip_args, 0, string);
7487 ASET (last_show_tip_args, 1, frame);
7488 ASET (last_show_tip_args, 2, parms);
7490 /* Add default values to frame parameters. */
7491 if (NILP (Fassq (Qname, parms)))
7492 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
7493 if (NILP (Fassq (Qinternal_border_width, parms)))
7494 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
7495 if (NILP (Fassq (Qborder_width, parms)))
7496 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
7497 if (NILP (Fassq (Qborder_color, parms)))
7498 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
7499 if (NILP (Fassq (Qbackground_color, parms)))
7500 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
7501 parms);
7503 /* Block input until the tip has been fully drawn, to avoid crashes
7504 when drawing tips in menus. */
7505 BLOCK_INPUT;
7507 /* Create a frame for the tooltip, and record it in the global
7508 variable tip_frame. */
7509 frame = x_create_tip_frame (FRAME_W32_DISPLAY_INFO (f), parms, string);
7510 f = XFRAME (frame);
7512 /* Set up the frame's root window. */
7513 w = XWINDOW (FRAME_ROOT_WINDOW (f));
7514 w->left_col = w->top_line = make_number (0);
7516 if (CONSP (Vx_max_tooltip_size)
7517 && INTEGERP (XCAR (Vx_max_tooltip_size))
7518 && XINT (XCAR (Vx_max_tooltip_size)) > 0
7519 && INTEGERP (XCDR (Vx_max_tooltip_size))
7520 && XINT (XCDR (Vx_max_tooltip_size)) > 0)
7522 w->total_cols = XCAR (Vx_max_tooltip_size);
7523 w->total_lines = XCDR (Vx_max_tooltip_size);
7525 else
7527 w->total_cols = make_number (80);
7528 w->total_lines = make_number (40);
7531 FRAME_TOTAL_COLS (f) = XINT (w->total_cols);
7532 adjust_glyphs (f);
7533 w->pseudo_window_p = 1;
7535 /* Display the tooltip text in a temporary buffer. */
7536 old_buffer = current_buffer;
7537 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->buffer));
7538 current_buffer->truncate_lines = Qnil;
7539 clear_glyph_matrix (w->desired_matrix);
7540 clear_glyph_matrix (w->current_matrix);
7541 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
7542 try_window (FRAME_ROOT_WINDOW (f), pos);
7544 /* Compute width and height of the tooltip. */
7545 width = height = 0;
7546 for (i = 0; i < w->desired_matrix->nrows; ++i)
7548 struct glyph_row *row = &w->desired_matrix->rows[i];
7549 struct glyph *last;
7550 int row_width;
7552 /* Stop at the first empty row at the end. */
7553 if (!row->enabled_p || !row->displays_text_p)
7554 break;
7556 /* Let the row go over the full width of the frame. */
7557 row->full_width_p = 1;
7559 #ifdef TODO /* Investigate why some fonts need more width than is
7560 calculated for some tooltips. */
7561 /* There's a glyph at the end of rows that is use to place
7562 the cursor there. Don't include the width of this glyph. */
7563 if (row->used[TEXT_AREA])
7565 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
7566 row_width = row->pixel_width - last->pixel_width;
7568 else
7569 #endif
7570 row_width = row->pixel_width;
7572 /* TODO: find why tips do not draw along baseline as instructed. */
7573 height += row->height;
7574 width = max (width, row_width);
7577 /* Add the frame's internal border to the width and height the X
7578 window should have. */
7579 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
7580 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
7582 /* Move the tooltip window where the mouse pointer is. Resize and
7583 show it. */
7584 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
7587 /* Adjust Window size to take border into account. */
7588 RECT rect;
7589 rect.left = rect.top = 0;
7590 rect.right = width;
7591 rect.bottom = height;
7592 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
7593 FRAME_EXTERNAL_MENU_BAR (f));
7595 /* Position and size tooltip, and put it in the topmost group. */
7596 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
7597 root_x, root_y, rect.right - rect.left,
7598 rect.bottom - rect.top, SWP_NOACTIVATE);
7600 /* Ensure tooltip is on top of other topmost windows (eg menus). */
7601 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
7602 0, 0, 0, 0,
7603 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
7605 /* Let redisplay know that we have made the frame visible already. */
7606 f->async_visible = 1;
7608 ShowWindow (FRAME_W32_WINDOW (f), SW_SHOWNOACTIVATE);
7611 /* Draw into the window. */
7612 w->must_be_updated_p = 1;
7613 update_single_window (w, 1);
7615 UNBLOCK_INPUT;
7617 /* Restore original current buffer. */
7618 set_buffer_internal_1 (old_buffer);
7619 windows_or_buffers_changed = old_windows_or_buffers_changed;
7621 start_timer:
7622 /* Let the tip disappear after timeout seconds. */
7623 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
7624 intern ("x-hide-tip"));
7626 UNGCPRO;
7627 return unbind_to (count, Qnil);
7631 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
7632 doc: /* Hide the current tooltip window, if there is any.
7633 Value is t if tooltip was open, nil otherwise. */)
7636 int count;
7637 Lisp_Object deleted, frame, timer;
7638 struct gcpro gcpro1, gcpro2;
7640 /* Return quickly if nothing to do. */
7641 if (NILP (tip_timer) && NILP (tip_frame))
7642 return Qnil;
7644 frame = tip_frame;
7645 timer = tip_timer;
7646 GCPRO2 (frame, timer);
7647 tip_frame = tip_timer = deleted = Qnil;
7649 count = SPECPDL_INDEX ();
7650 specbind (Qinhibit_redisplay, Qt);
7651 specbind (Qinhibit_quit, Qt);
7653 if (!NILP (timer))
7654 call1 (Qcancel_timer, timer);
7656 if (FRAMEP (frame))
7658 Fdelete_frame (frame, Qnil);
7659 deleted = Qt;
7662 UNGCPRO;
7663 return unbind_to (count, deleted);
7668 /***********************************************************************
7669 File selection dialog
7670 ***********************************************************************/
7671 extern Lisp_Object Qfile_name_history;
7673 /* Callback for altering the behaviour of the Open File dialog.
7674 Makes the Filename text field contain "Current Directory" and be
7675 read-only when "Directories" is selected in the filter. This
7676 allows us to work around the fact that the standard Open File
7677 dialog does not support directories. */
7678 UINT CALLBACK
7679 file_dialog_callback (hwnd, msg, wParam, lParam)
7680 HWND hwnd;
7681 UINT msg;
7682 WPARAM wParam;
7683 LPARAM lParam;
7685 if (msg == WM_NOTIFY)
7687 OFNOTIFY * notify = (OFNOTIFY *)lParam;
7688 /* Detect when the Filter dropdown is changed. */
7689 if (notify->hdr.code == CDN_TYPECHANGE)
7691 HWND dialog = GetParent (hwnd);
7692 HWND edit_control = GetDlgItem (dialog, FILE_NAME_TEXT_FIELD);
7694 /* Directories is in index 2. */
7695 if (notify->lpOFN->nFilterIndex == 2)
7697 CommDlg_OpenSave_SetControlText (dialog, FILE_NAME_TEXT_FIELD,
7698 "Current Directory");
7699 EnableWindow (edit_control, FALSE);
7701 else
7703 CommDlg_OpenSave_SetControlText (dialog, FILE_NAME_TEXT_FIELD,
7704 "");
7705 EnableWindow (edit_control, TRUE);
7709 return 0;
7712 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 4, 0,
7713 doc: /* Read file name, prompting with PROMPT in directory DIR.
7714 Use a file selection dialog.
7715 Select DEFAULT-FILENAME in the dialog's file selection box, if
7716 specified. Ensure that file exists if MUSTMATCH is non-nil. */)
7717 (prompt, dir, default_filename, mustmatch)
7718 Lisp_Object prompt, dir, default_filename, mustmatch;
7720 struct frame *f = SELECTED_FRAME ();
7721 Lisp_Object file = Qnil;
7722 int count = SPECPDL_INDEX ();
7723 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
7724 char filename[MAX_PATH + 1];
7725 char init_dir[MAX_PATH + 1];
7727 GCPRO5 (prompt, dir, default_filename, mustmatch, file);
7728 CHECK_STRING (prompt);
7729 CHECK_STRING (dir);
7731 /* Create the dialog with PROMPT as title, using DIR as initial
7732 directory and using "*" as pattern. */
7733 dir = Fexpand_file_name (dir, Qnil);
7734 strncpy (init_dir, SDATA (dir), MAX_PATH);
7735 init_dir[MAX_PATH] = '\0';
7736 unixtodos_filename (init_dir);
7738 if (STRINGP (default_filename))
7740 char *file_name_only;
7741 char *full_path_name = SDATA (default_filename);
7743 unixtodos_filename (full_path_name);
7745 file_name_only = strrchr (full_path_name, '\\');
7746 if (!file_name_only)
7747 file_name_only = full_path_name;
7748 else
7750 file_name_only++;
7753 strncpy (filename, file_name_only, MAX_PATH);
7754 filename[MAX_PATH] = '\0';
7756 else
7757 filename[0] = '\0';
7760 OPENFILENAME file_details;
7762 /* Prevent redisplay. */
7763 specbind (Qinhibit_redisplay, Qt);
7764 BLOCK_INPUT;
7766 bzero (&file_details, sizeof (file_details));
7767 file_details.lStructSize = sizeof (file_details);
7768 file_details.hwndOwner = FRAME_W32_WINDOW (f);
7769 /* Undocumented Bug in Common File Dialog:
7770 If a filter is not specified, shell links are not resolved. */
7771 file_details.lpstrFilter = "All Files (*.*)\0*.*\0Directories\0*|*\0\0";
7772 file_details.lpstrFile = filename;
7773 file_details.nMaxFile = sizeof (filename);
7774 file_details.lpstrInitialDir = init_dir;
7775 file_details.lpstrTitle = SDATA (prompt);
7776 file_details.Flags = (OFN_HIDEREADONLY | OFN_NOCHANGEDIR
7777 | OFN_EXPLORER | OFN_ENABLEHOOK);
7778 if (!NILP (mustmatch))
7779 file_details.Flags |= OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
7781 file_details.lpfnHook = (LPOFNHOOKPROC) file_dialog_callback;
7783 if (GetOpenFileName (&file_details))
7785 dostounix_filename (filename);
7786 if (file_details.nFilterIndex == 2)
7788 /* "Folder Only" selected - strip dummy file name. */
7789 char * last = strrchr (filename, '/');
7790 *last = '\0';
7793 file = DECODE_FILE(build_string (filename));
7795 /* User cancelled the dialog without making a selection. */
7796 else if (!CommDlgExtendedError ())
7797 file = Qnil;
7798 /* An error occurred, fallback on reading from the mini-buffer. */
7799 else
7800 file = Fcompleting_read (prompt, intern ("read-file-name-internal"),
7801 dir, mustmatch, dir, Qfile_name_history,
7802 default_filename, Qnil);
7804 UNBLOCK_INPUT;
7805 file = unbind_to (count, file);
7808 UNGCPRO;
7810 /* Make "Cancel" equivalent to C-g. */
7811 if (NILP (file))
7812 Fsignal (Qquit, Qnil);
7814 return unbind_to (count, file);
7819 /***********************************************************************
7820 w32 specialized functions
7821 ***********************************************************************/
7823 DEFUN ("w32-select-font", Fw32_select_font, Sw32_select_font, 0, 2, 0,
7824 doc: /* Select a font using the W32 font dialog.
7825 Returns an X font string corresponding to the selection. */)
7826 (frame, include_proportional)
7827 Lisp_Object frame, include_proportional;
7829 FRAME_PTR f = check_x_frame (frame);
7830 CHOOSEFONT cf;
7831 LOGFONT lf;
7832 TEXTMETRIC tm;
7833 HDC hdc;
7834 HANDLE oldobj;
7835 char buf[100];
7837 bzero (&cf, sizeof (cf));
7838 bzero (&lf, sizeof (lf));
7840 cf.lStructSize = sizeof (cf);
7841 cf.hwndOwner = FRAME_W32_WINDOW (f);
7842 cf.Flags = CF_FORCEFONTEXIST | CF_SCREENFONTS | CF_NOVERTFONTS;
7844 /* Unless include_proportional is non-nil, limit the selection to
7845 monospaced fonts. */
7846 if (NILP (include_proportional))
7847 cf.Flags |= CF_FIXEDPITCHONLY;
7849 cf.lpLogFont = &lf;
7851 /* Initialize as much of the font details as we can from the current
7852 default font. */
7853 hdc = GetDC (FRAME_W32_WINDOW (f));
7854 oldobj = SelectObject (hdc, FRAME_FONT (f)->hfont);
7855 GetTextFace (hdc, LF_FACESIZE, lf.lfFaceName);
7856 if (GetTextMetrics (hdc, &tm))
7858 lf.lfHeight = tm.tmInternalLeading - tm.tmHeight;
7859 lf.lfWeight = tm.tmWeight;
7860 lf.lfItalic = tm.tmItalic;
7861 lf.lfUnderline = tm.tmUnderlined;
7862 lf.lfStrikeOut = tm.tmStruckOut;
7863 lf.lfCharSet = tm.tmCharSet;
7864 cf.Flags |= CF_INITTOLOGFONTSTRUCT;
7866 SelectObject (hdc, oldobj);
7867 ReleaseDC (FRAME_W32_WINDOW (f), hdc);
7869 if (!ChooseFont (&cf) || !w32_to_x_font (&lf, buf, 100, NULL))
7870 return Qnil;
7872 return build_string (buf);
7875 DEFUN ("w32-send-sys-command", Fw32_send_sys_command,
7876 Sw32_send_sys_command, 1, 2, 0,
7877 doc: /* Send frame a Windows WM_SYSCOMMAND message of type COMMAND.
7878 Some useful values for COMMAND are #xf030 to maximize frame (#xf020
7879 to minimize), #xf120 to restore frame to original size, and #xf100
7880 to activate the menubar for keyboard access. #xf140 activates the
7881 screen saver if defined.
7883 If optional parameter FRAME is not specified, use selected frame. */)
7884 (command, frame)
7885 Lisp_Object command, frame;
7887 FRAME_PTR f = check_x_frame (frame);
7889 CHECK_NUMBER (command);
7891 PostMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, XINT (command), 0);
7893 return Qnil;
7896 DEFUN ("w32-shell-execute", Fw32_shell_execute, Sw32_shell_execute, 2, 4, 0,
7897 doc: /* Get Windows to perform OPERATION on DOCUMENT.
7898 This is a wrapper around the ShellExecute system function, which
7899 invokes the application registered to handle OPERATION for DOCUMENT.
7900 OPERATION is typically \"open\", \"print\" or \"explore\" (but can be
7901 nil for the default action), and DOCUMENT is typically the name of a
7902 document file or URL, but can also be a program executable to run or
7903 a directory to open in the Windows Explorer.
7905 If DOCUMENT is a program executable, PARAMETERS can be a string
7906 containing command line parameters, but otherwise should be nil.
7908 SHOW-FLAG can be used to control whether the invoked application is hidden
7909 or minimized. If SHOW-FLAG is nil, the application is displayed normally,
7910 otherwise it is an integer representing a ShowWindow flag:
7912 0 - start hidden
7913 1 - start normally
7914 3 - start maximized
7915 6 - start minimized */)
7916 (operation, document, parameters, show_flag)
7917 Lisp_Object operation, document, parameters, show_flag;
7919 Lisp_Object current_dir;
7921 CHECK_STRING (document);
7923 /* Encode filename and current directory. */
7924 current_dir = ENCODE_FILE (current_buffer->directory);
7925 document = ENCODE_FILE (document);
7926 if ((int) ShellExecute (NULL,
7927 (STRINGP (operation) ?
7928 SDATA (operation) : NULL),
7929 SDATA (document),
7930 (STRINGP (parameters) ?
7931 SDATA (parameters) : NULL),
7932 SDATA (current_dir),
7933 (INTEGERP (show_flag) ?
7934 XINT (show_flag) : SW_SHOWDEFAULT))
7935 > 32)
7936 return Qt;
7937 error ("ShellExecute failed: %s", w32_strerror (0));
7940 /* Lookup virtual keycode from string representing the name of a
7941 non-ascii keystroke into the corresponding virtual key, using
7942 lispy_function_keys. */
7943 static int
7944 lookup_vk_code (char *key)
7946 int i;
7948 for (i = 0; i < 256; i++)
7949 if (lispy_function_keys[i] != 0
7950 && strcmp (lispy_function_keys[i], key) == 0)
7951 return i;
7953 return -1;
7956 /* Convert a one-element vector style key sequence to a hot key
7957 definition. */
7958 static Lisp_Object
7959 w32_parse_hot_key (key)
7960 Lisp_Object key;
7962 /* Copied from Fdefine_key and store_in_keymap. */
7963 register Lisp_Object c;
7964 int vk_code;
7965 int lisp_modifiers;
7966 int w32_modifiers;
7967 struct gcpro gcpro1;
7969 CHECK_VECTOR (key);
7971 if (XFASTINT (Flength (key)) != 1)
7972 return Qnil;
7974 GCPRO1 (key);
7976 c = Faref (key, make_number (0));
7978 if (CONSP (c) && lucid_event_type_list_p (c))
7979 c = Fevent_convert_list (c);
7981 UNGCPRO;
7983 if (! INTEGERP (c) && ! SYMBOLP (c))
7984 error ("Key definition is invalid");
7986 /* Work out the base key and the modifiers. */
7987 if (SYMBOLP (c))
7989 c = parse_modifiers (c);
7990 lisp_modifiers = XINT (Fcar (Fcdr (c)));
7991 c = Fcar (c);
7992 if (!SYMBOLP (c))
7993 abort ();
7994 vk_code = lookup_vk_code (SDATA (SYMBOL_NAME (c)));
7996 else if (INTEGERP (c))
7998 lisp_modifiers = XINT (c) & ~CHARACTERBITS;
7999 /* Many ascii characters are their own virtual key code. */
8000 vk_code = XINT (c) & CHARACTERBITS;
8003 if (vk_code < 0 || vk_code > 255)
8004 return Qnil;
8006 if ((lisp_modifiers & meta_modifier) != 0
8007 && !NILP (Vw32_alt_is_meta))
8008 lisp_modifiers |= alt_modifier;
8010 /* Supply defs missing from mingw32. */
8011 #ifndef MOD_ALT
8012 #define MOD_ALT 0x0001
8013 #define MOD_CONTROL 0x0002
8014 #define MOD_SHIFT 0x0004
8015 #define MOD_WIN 0x0008
8016 #endif
8018 /* Convert lisp modifiers to Windows hot-key form. */
8019 w32_modifiers = (lisp_modifiers & hyper_modifier) ? MOD_WIN : 0;
8020 w32_modifiers |= (lisp_modifiers & alt_modifier) ? MOD_ALT : 0;
8021 w32_modifiers |= (lisp_modifiers & ctrl_modifier) ? MOD_CONTROL : 0;
8022 w32_modifiers |= (lisp_modifiers & shift_modifier) ? MOD_SHIFT : 0;
8024 return HOTKEY (vk_code, w32_modifiers);
8027 DEFUN ("w32-register-hot-key", Fw32_register_hot_key,
8028 Sw32_register_hot_key, 1, 1, 0,
8029 doc: /* Register KEY as a hot-key combination.
8030 Certain key combinations like Alt-Tab are reserved for system use on
8031 Windows, and therefore are normally intercepted by the system. However,
8032 most of these key combinations can be received by registering them as
8033 hot-keys, overriding their special meaning.
8035 KEY must be a one element key definition in vector form that would be
8036 acceptable to `define-key' (e.g. [A-tab] for Alt-Tab). The meta
8037 modifier is interpreted as Alt if `w32-alt-is-meta' is t, and hyper
8038 is always interpreted as the Windows modifier keys.
8040 The return value is the hotkey-id if registered, otherwise nil. */)
8041 (key)
8042 Lisp_Object key;
8044 key = w32_parse_hot_key (key);
8046 if (NILP (Fmemq (key, w32_grabbed_keys)))
8048 /* Reuse an empty slot if possible. */
8049 Lisp_Object item = Fmemq (Qnil, w32_grabbed_keys);
8051 /* Safe to add new key to list, even if we have focus. */
8052 if (NILP (item))
8053 w32_grabbed_keys = Fcons (key, w32_grabbed_keys);
8054 else
8055 XSETCAR (item, key);
8057 /* Notify input thread about new hot-key definition, so that it
8058 takes effect without needing to switch focus. */
8059 #ifdef USE_LISP_UNION_TYPE
8060 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
8061 (WPARAM) key.i, 0);
8062 #else
8063 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
8064 (WPARAM) key, 0);
8065 #endif
8068 return key;
8071 DEFUN ("w32-unregister-hot-key", Fw32_unregister_hot_key,
8072 Sw32_unregister_hot_key, 1, 1, 0,
8073 doc: /* Unregister KEY as a hot-key combination. */)
8074 (key)
8075 Lisp_Object key;
8077 Lisp_Object item;
8079 if (!INTEGERP (key))
8080 key = w32_parse_hot_key (key);
8082 item = Fmemq (key, w32_grabbed_keys);
8084 if (!NILP (item))
8086 /* Notify input thread about hot-key definition being removed, so
8087 that it takes effect without needing focus switch. */
8088 #ifdef USE_LISP_UNION_TYPE
8089 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
8090 (WPARAM) XINT (XCAR (item)), (LPARAM) item.i))
8091 #else
8092 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
8093 (WPARAM) XINT (XCAR (item)), (LPARAM) item))
8095 #endif
8097 MSG msg;
8098 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
8100 return Qt;
8102 return Qnil;
8105 DEFUN ("w32-registered-hot-keys", Fw32_registered_hot_keys,
8106 Sw32_registered_hot_keys, 0, 0, 0,
8107 doc: /* Return list of registered hot-key IDs. */)
8110 return Fcopy_sequence (w32_grabbed_keys);
8113 DEFUN ("w32-reconstruct-hot-key", Fw32_reconstruct_hot_key,
8114 Sw32_reconstruct_hot_key, 1, 1, 0,
8115 doc: /* Convert hot-key ID to a lisp key combination.
8116 usage: (w32-reconstruct-hot-key ID) */)
8117 (hotkeyid)
8118 Lisp_Object hotkeyid;
8120 int vk_code, w32_modifiers;
8121 Lisp_Object key;
8123 CHECK_NUMBER (hotkeyid);
8125 vk_code = HOTKEY_VK_CODE (hotkeyid);
8126 w32_modifiers = HOTKEY_MODIFIERS (hotkeyid);
8128 if (lispy_function_keys[vk_code])
8129 key = intern (lispy_function_keys[vk_code]);
8130 else
8131 key = make_number (vk_code);
8133 key = Fcons (key, Qnil);
8134 if (w32_modifiers & MOD_SHIFT)
8135 key = Fcons (Qshift, key);
8136 if (w32_modifiers & MOD_CONTROL)
8137 key = Fcons (Qctrl, key);
8138 if (w32_modifiers & MOD_ALT)
8139 key = Fcons (NILP (Vw32_alt_is_meta) ? Qalt : Qmeta, key);
8140 if (w32_modifiers & MOD_WIN)
8141 key = Fcons (Qhyper, key);
8143 return key;
8146 DEFUN ("w32-toggle-lock-key", Fw32_toggle_lock_key,
8147 Sw32_toggle_lock_key, 1, 2, 0,
8148 doc: /* Toggle the state of the lock key KEY.
8149 KEY can be `capslock', `kp-numlock', or `scroll'.
8150 If the optional parameter NEW-STATE is a number, then the state of KEY
8151 is set to off if the low bit of NEW-STATE is zero, otherwise on. */)
8152 (key, new_state)
8153 Lisp_Object key, new_state;
8155 int vk_code;
8157 if (EQ (key, intern ("capslock")))
8158 vk_code = VK_CAPITAL;
8159 else if (EQ (key, intern ("kp-numlock")))
8160 vk_code = VK_NUMLOCK;
8161 else if (EQ (key, intern ("scroll")))
8162 vk_code = VK_SCROLL;
8163 else
8164 return Qnil;
8166 if (!dwWindowsThreadId)
8167 return make_number (w32_console_toggle_lock_key (vk_code, new_state));
8169 #ifdef USE_LISP_UNION_TYPE
8170 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
8171 (WPARAM) vk_code, (LPARAM) new_state.i))
8172 #else
8173 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
8174 (WPARAM) vk_code, (LPARAM) new_state))
8175 #endif
8177 MSG msg;
8178 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
8179 return make_number (msg.wParam);
8181 return Qnil;
8184 DEFUN ("file-system-info", Ffile_system_info, Sfile_system_info, 1, 1, 0,
8185 doc: /* Return storage information about the file system FILENAME is on.
8186 Value is a list of floats (TOTAL FREE AVAIL), where TOTAL is the total
8187 storage of the file system, FREE is the free storage, and AVAIL is the
8188 storage available to a non-superuser. All 3 numbers are in bytes.
8189 If the underlying system call fails, value is nil. */)
8190 (filename)
8191 Lisp_Object filename;
8193 Lisp_Object encoded, value;
8195 CHECK_STRING (filename);
8196 filename = Fexpand_file_name (filename, Qnil);
8197 encoded = ENCODE_FILE (filename);
8199 value = Qnil;
8201 /* Determining the required information on Windows turns out, sadly,
8202 to be more involved than one would hope. The original Win32 api
8203 call for this will return bogus information on some systems, but we
8204 must dynamically probe for the replacement api, since that was
8205 added rather late on. */
8207 HMODULE hKernel = GetModuleHandle ("kernel32");
8208 BOOL (*pfn_GetDiskFreeSpaceEx)
8209 (char *, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER)
8210 = (void *) GetProcAddress (hKernel, "GetDiskFreeSpaceEx");
8212 /* On Windows, we may need to specify the root directory of the
8213 volume holding FILENAME. */
8214 char rootname[MAX_PATH];
8215 char *name = SDATA (encoded);
8217 /* find the root name of the volume if given */
8218 if (isalpha (name[0]) && name[1] == ':')
8220 rootname[0] = name[0];
8221 rootname[1] = name[1];
8222 rootname[2] = '\\';
8223 rootname[3] = 0;
8225 else if (IS_DIRECTORY_SEP (name[0]) && IS_DIRECTORY_SEP (name[1]))
8227 char *str = rootname;
8228 int slashes = 4;
8231 if (IS_DIRECTORY_SEP (*name) && --slashes == 0)
8232 break;
8233 *str++ = *name++;
8235 while ( *name );
8237 *str++ = '\\';
8238 *str = 0;
8241 if (pfn_GetDiskFreeSpaceEx)
8243 /* Unsigned large integers cannot be cast to double, so
8244 use signed ones instead. */
8245 LARGE_INTEGER availbytes;
8246 LARGE_INTEGER freebytes;
8247 LARGE_INTEGER totalbytes;
8249 if (pfn_GetDiskFreeSpaceEx(rootname,
8250 (ULARGE_INTEGER *)&availbytes,
8251 (ULARGE_INTEGER *)&totalbytes,
8252 (ULARGE_INTEGER *)&freebytes))
8253 value = list3 (make_float ((double) totalbytes.QuadPart),
8254 make_float ((double) freebytes.QuadPart),
8255 make_float ((double) availbytes.QuadPart));
8257 else
8259 DWORD sectors_per_cluster;
8260 DWORD bytes_per_sector;
8261 DWORD free_clusters;
8262 DWORD total_clusters;
8264 if (GetDiskFreeSpace(rootname,
8265 &sectors_per_cluster,
8266 &bytes_per_sector,
8267 &free_clusters,
8268 &total_clusters))
8269 value = list3 (make_float ((double) total_clusters
8270 * sectors_per_cluster * bytes_per_sector),
8271 make_float ((double) free_clusters
8272 * sectors_per_cluster * bytes_per_sector),
8273 make_float ((double) free_clusters
8274 * sectors_per_cluster * bytes_per_sector));
8278 return value;
8281 DEFUN ("default-printer-name", Fdefault_printer_name, Sdefault_printer_name,
8282 0, 0, 0, doc: /* Return the name of Windows default printer device. */)
8285 static char pname_buf[256];
8286 int err;
8287 HANDLE hPrn;
8288 PRINTER_INFO_2 *ppi2 = NULL;
8289 DWORD dwNeeded = 0, dwReturned = 0;
8291 /* Retrieve the default string from Win.ini (the registry).
8292 * String will be in form "printername,drivername,portname".
8293 * This is the most portable way to get the default printer. */
8294 if (GetProfileString ("windows", "device", ",,", pname_buf, sizeof (pname_buf)) <= 0)
8295 return Qnil;
8296 /* printername precedes first "," character */
8297 strtok (pname_buf, ",");
8298 /* We want to know more than the printer name */
8299 if (!OpenPrinter (pname_buf, &hPrn, NULL))
8300 return Qnil;
8301 GetPrinter (hPrn, 2, NULL, 0, &dwNeeded);
8302 if (dwNeeded == 0)
8304 ClosePrinter (hPrn);
8305 return Qnil;
8307 /* Allocate memory for the PRINTER_INFO_2 struct */
8308 ppi2 = (PRINTER_INFO_2 *) xmalloc (dwNeeded);
8309 if (!ppi2)
8311 ClosePrinter (hPrn);
8312 return Qnil;
8314 /* Call GetPrinter() again with big enouth memory block */
8315 err = GetPrinter (hPrn, 2, (LPBYTE)ppi2, dwNeeded, &dwReturned);
8316 ClosePrinter (hPrn);
8317 if (!err)
8319 xfree(ppi2);
8320 return Qnil;
8323 if (ppi2)
8325 if (ppi2->Attributes & PRINTER_ATTRIBUTE_SHARED && ppi2->pServerName)
8327 /* a remote printer */
8328 if (*ppi2->pServerName == '\\')
8329 _snprintf(pname_buf, sizeof (pname_buf), "%s\\%s", ppi2->pServerName,
8330 ppi2->pShareName);
8331 else
8332 _snprintf(pname_buf, sizeof (pname_buf), "\\\\%s\\%s", ppi2->pServerName,
8333 ppi2->pShareName);
8334 pname_buf[sizeof (pname_buf) - 1] = '\0';
8336 else
8338 /* a local printer */
8339 strncpy(pname_buf, ppi2->pPortName, sizeof (pname_buf));
8340 pname_buf[sizeof (pname_buf) - 1] = '\0';
8341 /* `pPortName' can include several ports, delimited by ','.
8342 * we only use the first one. */
8343 strtok(pname_buf, ",");
8345 xfree(ppi2);
8348 return build_string (pname_buf);
8351 /***********************************************************************
8352 Initialization
8353 ***********************************************************************/
8355 /* Keep this list in the same order as frame_parms in frame.c.
8356 Use 0 for unsupported frame parameters. */
8358 frame_parm_handler w32_frame_parm_handlers[] =
8360 x_set_autoraise,
8361 x_set_autolower,
8362 x_set_background_color,
8363 x_set_border_color,
8364 x_set_border_width,
8365 x_set_cursor_color,
8366 x_set_cursor_type,
8367 x_set_font,
8368 x_set_foreground_color,
8369 x_set_icon_name,
8370 x_set_icon_type,
8371 x_set_internal_border_width,
8372 x_set_menu_bar_lines,
8373 x_set_mouse_color,
8374 x_explicitly_set_name,
8375 x_set_scroll_bar_width,
8376 x_set_title,
8377 x_set_unsplittable,
8378 x_set_vertical_scroll_bars,
8379 x_set_visibility,
8380 x_set_tool_bar_lines,
8381 0, /* x_set_scroll_bar_foreground, */
8382 0, /* x_set_scroll_bar_background, */
8383 x_set_screen_gamma,
8384 x_set_line_spacing,
8385 x_set_fringe_width,
8386 x_set_fringe_width,
8387 0, /* x_set_wait_for_wm, */
8388 x_set_fullscreen,
8391 void
8392 syms_of_w32fns ()
8394 globals_of_w32fns ();
8395 /* This is zero if not using MS-Windows. */
8396 w32_in_use = 0;
8397 track_mouse_window = NULL;
8399 w32_visible_system_caret_hwnd = NULL;
8401 Qnone = intern ("none");
8402 staticpro (&Qnone);
8403 Qsuppress_icon = intern ("suppress-icon");
8404 staticpro (&Qsuppress_icon);
8405 Qundefined_color = intern ("undefined-color");
8406 staticpro (&Qundefined_color);
8407 Qcancel_timer = intern ("cancel-timer");
8408 staticpro (&Qcancel_timer);
8410 Qhyper = intern ("hyper");
8411 staticpro (&Qhyper);
8412 Qsuper = intern ("super");
8413 staticpro (&Qsuper);
8414 Qmeta = intern ("meta");
8415 staticpro (&Qmeta);
8416 Qalt = intern ("alt");
8417 staticpro (&Qalt);
8418 Qctrl = intern ("ctrl");
8419 staticpro (&Qctrl);
8420 Qcontrol = intern ("control");
8421 staticpro (&Qcontrol);
8422 Qshift = intern ("shift");
8423 staticpro (&Qshift);
8424 /* This is the end of symbol initialization. */
8426 /* Text property `display' should be nonsticky by default. */
8427 Vtext_property_default_nonsticky
8428 = Fcons (Fcons (Qdisplay, Qt), Vtext_property_default_nonsticky);
8431 Fput (Qundefined_color, Qerror_conditions,
8432 Fcons (Qundefined_color, Fcons (Qerror, Qnil)));
8433 Fput (Qundefined_color, Qerror_message,
8434 build_string ("Undefined color"));
8436 staticpro (&w32_grabbed_keys);
8437 w32_grabbed_keys = Qnil;
8439 DEFVAR_LISP ("w32-color-map", &Vw32_color_map,
8440 doc: /* An array of color name mappings for Windows. */);
8441 Vw32_color_map = Qnil;
8443 DEFVAR_LISP ("w32-pass-alt-to-system", &Vw32_pass_alt_to_system,
8444 doc: /* Non-nil if alt key presses are passed on to Windows.
8445 When non-nil, for example, alt pressed and released and then space will
8446 open the System menu. When nil, Emacs silently swallows alt key events. */);
8447 Vw32_pass_alt_to_system = Qnil;
8449 DEFVAR_LISP ("w32-alt-is-meta", &Vw32_alt_is_meta,
8450 doc: /* Non-nil if the alt key is to be considered the same as the meta key.
8451 When nil, Emacs will translate the alt key to the Alt modifier, and not Meta. */);
8452 Vw32_alt_is_meta = Qt;
8454 DEFVAR_INT ("w32-quit-key", &w32_quit_key,
8455 doc: /* If non-zero, the virtual key code for an alternative quit key. */);
8456 w32_quit_key = 0;
8458 DEFVAR_LISP ("w32-pass-lwindow-to-system",
8459 &Vw32_pass_lwindow_to_system,
8460 doc: /* Non-nil if the left \"Windows\" key is passed on to Windows.
8461 When non-nil, the Start menu is opened by tapping the key. */);
8462 Vw32_pass_lwindow_to_system = Qt;
8464 DEFVAR_LISP ("w32-pass-rwindow-to-system",
8465 &Vw32_pass_rwindow_to_system,
8466 doc: /* Non-nil if the right \"Windows\" key is passed on to Windows.
8467 When non-nil, the Start menu is opened by tapping the key. */);
8468 Vw32_pass_rwindow_to_system = Qt;
8470 DEFVAR_LISP ("w32-phantom-key-code",
8471 &Vw32_phantom_key_code,
8472 doc: /* Virtual key code used to generate \"phantom\" key presses.
8473 Value is a number between 0 and 255.
8475 Phantom key presses are generated in order to stop the system from
8476 acting on \"Windows\" key events when `w32-pass-lwindow-to-system' or
8477 `w32-pass-rwindow-to-system' is nil. */);
8478 /* Although 255 is technically not a valid key code, it works and
8479 means that this hack won't interfere with any real key code. */
8480 XSETINT (Vw32_phantom_key_code, 255);
8482 DEFVAR_LISP ("w32-enable-num-lock",
8483 &Vw32_enable_num_lock,
8484 doc: /* Non-nil if Num Lock should act normally.
8485 Set to nil to see Num Lock as the key `kp-numlock'. */);
8486 Vw32_enable_num_lock = Qt;
8488 DEFVAR_LISP ("w32-enable-caps-lock",
8489 &Vw32_enable_caps_lock,
8490 doc: /* Non-nil if Caps Lock should act normally.
8491 Set to nil to see Caps Lock as the key `capslock'. */);
8492 Vw32_enable_caps_lock = Qt;
8494 DEFVAR_LISP ("w32-scroll-lock-modifier",
8495 &Vw32_scroll_lock_modifier,
8496 doc: /* Modifier to use for the Scroll Lock on state.
8497 The value can be hyper, super, meta, alt, control or shift for the
8498 respective modifier, or nil to see Scroll Lock as the key `scroll'.
8499 Any other value will cause the key to be ignored. */);
8500 Vw32_scroll_lock_modifier = Qt;
8502 DEFVAR_LISP ("w32-lwindow-modifier",
8503 &Vw32_lwindow_modifier,
8504 doc: /* Modifier to use for the left \"Windows\" key.
8505 The value can be hyper, super, meta, alt, control or shift for the
8506 respective modifier, or nil to appear as the key `lwindow'.
8507 Any other value will cause the key to be ignored. */);
8508 Vw32_lwindow_modifier = Qnil;
8510 DEFVAR_LISP ("w32-rwindow-modifier",
8511 &Vw32_rwindow_modifier,
8512 doc: /* Modifier to use for the right \"Windows\" key.
8513 The value can be hyper, super, meta, alt, control or shift for the
8514 respective modifier, or nil to appear as the key `rwindow'.
8515 Any other value will cause the key to be ignored. */);
8516 Vw32_rwindow_modifier = Qnil;
8518 DEFVAR_LISP ("w32-apps-modifier",
8519 &Vw32_apps_modifier,
8520 doc: /* Modifier to use for the \"Apps\" key.
8521 The value can be hyper, super, meta, alt, control or shift for the
8522 respective modifier, or nil to appear as the key `apps'.
8523 Any other value will cause the key to be ignored. */);
8524 Vw32_apps_modifier = Qnil;
8526 DEFVAR_BOOL ("w32-enable-synthesized-fonts", &w32_enable_synthesized_fonts,
8527 doc: /* Non-nil enables selection of artificially italicized and bold fonts. */);
8528 w32_enable_synthesized_fonts = 0;
8530 DEFVAR_LISP ("w32-enable-palette", &Vw32_enable_palette,
8531 doc: /* Non-nil enables Windows palette management to map colors exactly. */);
8532 Vw32_enable_palette = Qt;
8534 DEFVAR_INT ("w32-mouse-button-tolerance",
8535 &w32_mouse_button_tolerance,
8536 doc: /* Analogue of double click interval for faking middle mouse events.
8537 The value is the minimum time in milliseconds that must elapse between
8538 left/right button down events before they are considered distinct events.
8539 If both mouse buttons are depressed within this interval, a middle mouse
8540 button down event is generated instead. */);
8541 w32_mouse_button_tolerance = GetDoubleClickTime () / 2;
8543 DEFVAR_INT ("w32-mouse-move-interval",
8544 &w32_mouse_move_interval,
8545 doc: /* Minimum interval between mouse move events.
8546 The value is the minimum time in milliseconds that must elapse between
8547 successive mouse move (or scroll bar drag) events before they are
8548 reported as lisp events. */);
8549 w32_mouse_move_interval = 0;
8551 DEFVAR_BOOL ("w32-pass-extra-mouse-buttons-to-system",
8552 &w32_pass_extra_mouse_buttons_to_system,
8553 doc: /* Non-nil if the fourth and fifth mouse buttons are passed to Windows.
8554 Recent versions of Windows support mice with up to five buttons.
8555 Since most applications don't support these extra buttons, most mouse
8556 drivers will allow you to map them to functions at the system level.
8557 If this variable is non-nil, Emacs will pass them on, allowing the
8558 system to handle them. */);
8559 w32_pass_extra_mouse_buttons_to_system = 0;
8561 DEFVAR_LISP ("x-pointer-shape", &Vx_pointer_shape,
8562 doc: /* The shape of the pointer when over text.
8563 Changing the value does not affect existing frames
8564 unless you set the mouse color. */);
8565 Vx_pointer_shape = Qnil;
8567 Vx_nontext_pointer_shape = Qnil;
8569 Vx_mode_pointer_shape = Qnil;
8571 DEFVAR_LISP ("x-hourglass-pointer-shape", &Vx_hourglass_pointer_shape,
8572 doc: /* The shape of the pointer when Emacs is busy.
8573 This variable takes effect when you create a new frame
8574 or when you set the mouse color. */);
8575 Vx_hourglass_pointer_shape = Qnil;
8577 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
8578 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
8579 display_hourglass_p = 1;
8581 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
8582 doc: /* *Seconds to wait before displaying an hourglass pointer.
8583 Value must be an integer or float. */);
8584 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
8586 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
8587 &Vx_sensitive_text_pointer_shape,
8588 doc: /* The shape of the pointer when over mouse-sensitive text.
8589 This variable takes effect when you create a new frame
8590 or when you set the mouse color. */);
8591 Vx_sensitive_text_pointer_shape = Qnil;
8593 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
8594 &Vx_window_horizontal_drag_shape,
8595 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
8596 This variable takes effect when you create a new frame
8597 or when you set the mouse color. */);
8598 Vx_window_horizontal_drag_shape = Qnil;
8600 DEFVAR_LISP ("x-cursor-fore-pixel", &Vx_cursor_fore_pixel,
8601 doc: /* A string indicating the foreground color of the cursor box. */);
8602 Vx_cursor_fore_pixel = Qnil;
8604 DEFVAR_LISP ("x-max-tooltip-size", &Vx_max_tooltip_size,
8605 doc: /* Maximum size for tooltips.
8606 Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
8607 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
8609 DEFVAR_LISP ("x-no-window-manager", &Vx_no_window_manager,
8610 doc: /* Non-nil if no window manager is in use.
8611 Emacs doesn't try to figure this out; this is always nil
8612 unless you set it to something else. */);
8613 /* We don't have any way to find this out, so set it to nil
8614 and maybe the user would like to set it to t. */
8615 Vx_no_window_manager = Qnil;
8617 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
8618 &Vx_pixel_size_width_font_regexp,
8619 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
8621 Since Emacs gets width of a font matching with this regexp from
8622 PIXEL_SIZE field of the name, font finding mechanism gets faster for
8623 such a font. This is especially effective for such large fonts as
8624 Chinese, Japanese, and Korean. */);
8625 Vx_pixel_size_width_font_regexp = Qnil;
8627 DEFVAR_LISP ("w32-bdf-filename-alist",
8628 &Vw32_bdf_filename_alist,
8629 doc: /* List of bdf fonts and their corresponding filenames. */);
8630 Vw32_bdf_filename_alist = Qnil;
8632 DEFVAR_BOOL ("w32-strict-fontnames",
8633 &w32_strict_fontnames,
8634 doc: /* Non-nil means only use fonts that are exact matches for those requested.
8635 Default is nil, which allows old fontnames that are not XLFD compliant,
8636 and allows third-party CJK display to work by specifying false charset
8637 fields to trick Emacs into translating to Big5, SJIS etc.
8638 Setting this to t will prevent wrong fonts being selected when
8639 fontsets are automatically created. */);
8640 w32_strict_fontnames = 0;
8642 DEFVAR_BOOL ("w32-strict-painting",
8643 &w32_strict_painting,
8644 doc: /* Non-nil means use strict rules for repainting frames.
8645 Set this to nil to get the old behaviour for repainting; this should
8646 only be necessary if the default setting causes problems. */);
8647 w32_strict_painting = 1;
8649 DEFVAR_LISP ("w32-charset-info-alist",
8650 &Vw32_charset_info_alist,
8651 doc: /* Alist linking Emacs character sets to Windows fonts and codepages.
8652 Each entry should be of the form:
8654 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE))
8656 where CHARSET_NAME is a string used in font names to identify the charset,
8657 WINDOWS_CHARSET is a symbol that can be one of:
8658 w32-charset-ansi, w32-charset-default, w32-charset-symbol,
8659 w32-charset-shiftjis, w32-charset-hangeul, w32-charset-gb2312,
8660 w32-charset-chinesebig5,
8661 w32-charset-johab, w32-charset-hebrew,
8662 w32-charset-arabic, w32-charset-greek, w32-charset-turkish,
8663 w32-charset-vietnamese, w32-charset-thai, w32-charset-easteurope,
8664 w32-charset-russian, w32-charset-mac, w32-charset-baltic,
8665 w32-charset-unicode,
8666 or w32-charset-oem.
8667 CODEPAGE should be an integer specifying the codepage that should be used
8668 to display the character set, t to do no translation and output as Unicode,
8669 or nil to do no translation and output as 8 bit (or multibyte on far-east
8670 versions of Windows) characters. */);
8671 Vw32_charset_info_alist = Qnil;
8673 staticpro (&Qw32_charset_ansi);
8674 Qw32_charset_ansi = intern ("w32-charset-ansi");
8675 staticpro (&Qw32_charset_symbol);
8676 Qw32_charset_symbol = intern ("w32-charset-symbol");
8677 staticpro (&Qw32_charset_shiftjis);
8678 Qw32_charset_shiftjis = intern ("w32-charset-shiftjis");
8679 staticpro (&Qw32_charset_hangeul);
8680 Qw32_charset_hangeul = intern ("w32-charset-hangeul");
8681 staticpro (&Qw32_charset_chinesebig5);
8682 Qw32_charset_chinesebig5 = intern ("w32-charset-chinesebig5");
8683 staticpro (&Qw32_charset_gb2312);
8684 Qw32_charset_gb2312 = intern ("w32-charset-gb2312");
8685 staticpro (&Qw32_charset_oem);
8686 Qw32_charset_oem = intern ("w32-charset-oem");
8688 #ifdef JOHAB_CHARSET
8690 static int w32_extra_charsets_defined = 1;
8691 DEFVAR_BOOL ("w32-extra-charsets-defined", &w32_extra_charsets_defined,
8692 doc: /* Internal variable. */);
8694 staticpro (&Qw32_charset_johab);
8695 Qw32_charset_johab = intern ("w32-charset-johab");
8696 staticpro (&Qw32_charset_easteurope);
8697 Qw32_charset_easteurope = intern ("w32-charset-easteurope");
8698 staticpro (&Qw32_charset_turkish);
8699 Qw32_charset_turkish = intern ("w32-charset-turkish");
8700 staticpro (&Qw32_charset_baltic);
8701 Qw32_charset_baltic = intern ("w32-charset-baltic");
8702 staticpro (&Qw32_charset_russian);
8703 Qw32_charset_russian = intern ("w32-charset-russian");
8704 staticpro (&Qw32_charset_arabic);
8705 Qw32_charset_arabic = intern ("w32-charset-arabic");
8706 staticpro (&Qw32_charset_greek);
8707 Qw32_charset_greek = intern ("w32-charset-greek");
8708 staticpro (&Qw32_charset_hebrew);
8709 Qw32_charset_hebrew = intern ("w32-charset-hebrew");
8710 staticpro (&Qw32_charset_vietnamese);
8711 Qw32_charset_vietnamese = intern ("w32-charset-vietnamese");
8712 staticpro (&Qw32_charset_thai);
8713 Qw32_charset_thai = intern ("w32-charset-thai");
8714 staticpro (&Qw32_charset_mac);
8715 Qw32_charset_mac = intern ("w32-charset-mac");
8717 #endif
8719 #ifdef UNICODE_CHARSET
8721 static int w32_unicode_charset_defined = 1;
8722 DEFVAR_BOOL ("w32-unicode-charset-defined",
8723 &w32_unicode_charset_defined,
8724 doc: /* Internal variable. */);
8726 staticpro (&Qw32_charset_unicode);
8727 Qw32_charset_unicode = intern ("w32-charset-unicode");
8728 #endif
8730 #if 0 /* TODO: Port to W32 */
8731 defsubr (&Sx_change_window_property);
8732 defsubr (&Sx_delete_window_property);
8733 defsubr (&Sx_window_property);
8734 #endif
8735 defsubr (&Sxw_display_color_p);
8736 defsubr (&Sx_display_grayscale_p);
8737 defsubr (&Sxw_color_defined_p);
8738 defsubr (&Sxw_color_values);
8739 defsubr (&Sx_server_max_request_size);
8740 defsubr (&Sx_server_vendor);
8741 defsubr (&Sx_server_version);
8742 defsubr (&Sx_display_pixel_width);
8743 defsubr (&Sx_display_pixel_height);
8744 defsubr (&Sx_display_mm_width);
8745 defsubr (&Sx_display_mm_height);
8746 defsubr (&Sx_display_screens);
8747 defsubr (&Sx_display_planes);
8748 defsubr (&Sx_display_color_cells);
8749 defsubr (&Sx_display_visual_class);
8750 defsubr (&Sx_display_backing_store);
8751 defsubr (&Sx_display_save_under);
8752 defsubr (&Sx_create_frame);
8753 defsubr (&Sx_open_connection);
8754 defsubr (&Sx_close_connection);
8755 defsubr (&Sx_display_list);
8756 defsubr (&Sx_synchronize);
8758 /* W32 specific functions */
8760 defsubr (&Sw32_focus_frame);
8761 defsubr (&Sw32_select_font);
8762 defsubr (&Sw32_define_rgb_color);
8763 defsubr (&Sw32_default_color_map);
8764 defsubr (&Sw32_load_color_file);
8765 defsubr (&Sw32_send_sys_command);
8766 defsubr (&Sw32_shell_execute);
8767 defsubr (&Sw32_register_hot_key);
8768 defsubr (&Sw32_unregister_hot_key);
8769 defsubr (&Sw32_registered_hot_keys);
8770 defsubr (&Sw32_reconstruct_hot_key);
8771 defsubr (&Sw32_toggle_lock_key);
8772 defsubr (&Sw32_find_bdf_fonts);
8774 defsubr (&Sfile_system_info);
8775 defsubr (&Sdefault_printer_name);
8777 /* Setting callback functions for fontset handler. */
8778 get_font_info_func = w32_get_font_info;
8780 #if 0 /* This function pointer doesn't seem to be used anywhere.
8781 And the pointer assigned has the wrong type, anyway. */
8782 list_fonts_func = w32_list_fonts;
8783 #endif
8785 load_font_func = w32_load_font;
8786 find_ccl_program_func = w32_find_ccl_program;
8787 query_font_func = w32_query_font;
8788 set_frame_fontset_func = x_set_font;
8789 check_window_system_func = check_w32;
8792 hourglass_atimer = NULL;
8793 hourglass_shown_p = 0;
8794 defsubr (&Sx_show_tip);
8795 defsubr (&Sx_hide_tip);
8796 tip_timer = Qnil;
8797 staticpro (&tip_timer);
8798 tip_frame = Qnil;
8799 staticpro (&tip_frame);
8801 last_show_tip_args = Qnil;
8802 staticpro (&last_show_tip_args);
8804 defsubr (&Sx_file_dialog);
8809 globals_of_w32fns is used to initialize those global variables that
8810 must always be initialized on startup even when the global variable
8811 initialized is non zero (see the function main in emacs.c).
8812 globals_of_w32fns is called from syms_of_w32fns when the global
8813 variable initialized is 0 and directly from main when initialized
8814 is non zero.
8816 void globals_of_w32fns ()
8818 HMODULE user32_lib = GetModuleHandle ("user32.dll");
8820 TrackMouseEvent not available in all versions of Windows, so must load
8821 it dynamically. Do it once, here, instead of every time it is used.
8823 track_mouse_event_fn = (TrackMouseEvent_Proc)
8824 GetProcAddress (user32_lib, "TrackMouseEvent");
8825 /* ditto for GetClipboardSequenceNumber. */
8826 clipboard_sequence_fn = (ClipboardSequence_Proc)
8827 GetProcAddress (user32_lib, "GetClipboardSequenceNumber");
8829 DEFVAR_INT ("w32-ansi-code-page",
8830 &w32_ansi_code_page,
8831 doc: /* The ANSI code page used by the system. */);
8832 w32_ansi_code_page = GetACP ();
8835 #undef abort
8837 void
8838 w32_abort()
8840 int button;
8841 button = MessageBox (NULL,
8842 "A fatal error has occurred!\n\n"
8843 "Select Abort to exit, Retry to debug, Ignore to continue",
8844 "Emacs Abort Dialog",
8845 MB_ICONEXCLAMATION | MB_TASKMODAL
8846 | MB_SETFOREGROUND | MB_ABORTRETRYIGNORE);
8847 switch (button)
8849 case IDRETRY:
8850 DebugBreak ();
8851 break;
8852 case IDIGNORE:
8853 break;
8854 case IDABORT:
8855 default:
8856 abort ();
8857 break;
8861 /* For convenience when debugging. */
8863 w32_last_error()
8865 return GetLastError ();
8868 /* arch-tag: 707589ab-b9be-4638-8cdd-74629cc9b446
8869 (do not change this comment) */