Info-index and colons
[bpt/emacs.git] / src / w32fns.c
CommitLineData
e9e23e23 1/* Graphical user interface functions for the Microsoft W32 API.
a93f4566 2 Copyright (C) 1989, 92, 93, 94, 95, 1996, 1997, 1998, 1999, 2000, 2001
6fc2811b 3 Free Software Foundation, Inc.
ee78dc32
GV
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
ee78dc32
GV
21
22/* Added by Kevin Gallo */
23
ee78dc32 24#include <config.h>
1edf84e7
GV
25
26#include <signal.h>
ee78dc32 27#include <stdio.h>
1edf84e7
GV
28#include <limits.h>
29#include <errno.h>
ee78dc32
GV
30
31#include "lisp.h"
4587b026 32#include "charset.h"
71eab8d1 33#include "dispextern.h"
ee78dc32 34#include "w32term.h"
c7501041 35#include "keyboard.h"
ee78dc32
GV
36#include "frame.h"
37#include "window.h"
38#include "buffer.h"
126f2e35 39#include "fontset.h"
6fc2811b 40#include "intervals.h"
ee78dc32 41#include "blockinput.h"
57bda87a 42#include "epaths.h"
489f9371 43#include "w32heap.h"
ee78dc32 44#include "termhooks.h"
4587b026 45#include "coding.h"
3545439c 46#include "ccl.h"
6fc2811b
JR
47#include "systime.h"
48
49#include "bitmaps/gray.xbm"
ee78dc32
GV
50
51#include <commdlg.h>
cb9e33d4 52#include <shellapi.h>
6fc2811b 53#include <ctype.h>
ee78dc32 54
1030b26b
JR
55#include <dlgs.h>
56#define FILE_NAME_TEXT_FIELD edt1
57
9785d95b
BK
58void syms_of_w32fns ();
59void globals_of_w32fns ();
60
ee78dc32 61extern void free_frame_menubar ();
9eb16b62 62extern void x_compute_fringe_widths P_ ((struct frame *, int));
6fc2811b 63extern double atof ();
9eb16b62
JR
64extern int w32_console_toggle_lock_key P_ ((int, Lisp_Object));
65extern void w32_menu_display_help P_ ((HWND, HMENU, UINT, UINT));
66extern void w32_free_menu_strings P_ ((HWND));
67
5ac45f98 68extern int quit_char;
ee78dc32 69
ccc2d29c
GV
70extern char *lispy_function_keys[];
71
6fc2811b
JR
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. */
75
76int gray_bitmap_width = gray_width;
77int gray_bitmap_height = gray_height;
78unsigned char *gray_bitmap_bits = gray_bits;
79
ee78dc32 80/* The colormap for converting color names to RGB values */
fbd6baed 81Lisp_Object Vw32_color_map;
ee78dc32 82
da36a4d6 83/* Non nil if alt key presses are passed on to Windows. */
fbd6baed 84Lisp_Object Vw32_pass_alt_to_system;
da36a4d6 85
8c205c63
RS
86/* Non nil if alt key is translated to meta_modifier, nil if it is translated
87 to alt_modifier. */
fbd6baed 88Lisp_Object Vw32_alt_is_meta;
8c205c63 89
7d081355
AI
90/* If non-zero, the windows virtual key code for an alternative quit key. */
91Lisp_Object Vw32_quit_key;
92
ccc2d29c
GV
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). */
95Lisp_Object Vw32_pass_lwindow_to_system;
96
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). */
99Lisp_Object Vw32_pass_rwindow_to_system;
100
adcc3809
GV
101/* Virtual key code used to generate "phantom" key presses in order
102 to stop system from acting on Windows key events. */
103Lisp_Object Vw32_phantom_key_code;
104
ccc2d29c
GV
105/* Modifier associated with the left "Windows" key, or nil to act as a
106 normal key. */
107Lisp_Object Vw32_lwindow_modifier;
108
109/* Modifier associated with the right "Windows" key, or nil to act as a
110 normal key. */
111Lisp_Object Vw32_rwindow_modifier;
112
113/* Modifier associated with the "Apps" key, or nil to act as a normal
114 key. */
115Lisp_Object Vw32_apps_modifier;
116
117/* Value is nil if Num Lock acts as a function key. */
118Lisp_Object Vw32_enable_num_lock;
119
120/* Value is nil if Caps Lock acts as a function key. */
121Lisp_Object Vw32_enable_caps_lock;
122
123/* Modifier associated with Scroll Lock, or nil to act as a normal key. */
124Lisp_Object Vw32_scroll_lock_modifier;
da36a4d6 125
7ce9aaca 126/* Switch to control whether we inhibit requests for synthesized bold
6fc2811b 127 and italic versions of fonts. */
d84b082d 128int w32_enable_synthesized_fonts;
5ac45f98
GV
129
130/* Enable palette management. */
fbd6baed 131Lisp_Object Vw32_enable_palette;
5ac45f98
GV
132
133/* Control how close left/right button down events must be to
134 be converted to a middle button down event. */
fbd6baed 135Lisp_Object Vw32_mouse_button_tolerance;
5ac45f98 136
84fb1139
KH
137/* Minimum interval between mouse movement (and scroll bar drag)
138 events that are passed on to the event loop. */
fbd6baed 139Lisp_Object Vw32_mouse_move_interval;
84fb1139 140
74214547
JR
141/* Flag to indicate if XBUTTON events should be passed on to Windows. */
142int w32_pass_extra_mouse_buttons_to_system;
143
ee78dc32
GV
144/* The name we're using in resource queries. */
145Lisp_Object Vx_resource_name;
146
147/* Non nil if no window manager is in use. */
148Lisp_Object Vx_no_window_manager;
149
0af913d7 150/* Non-zero means we're allowed to display a hourglass pointer. */
dfff8a69 151
0af913d7 152int display_hourglass_p;
6fc2811b 153
ee78dc32
GV
154/* The background and shape of the mouse pointer, and shape when not
155 over text or in the modeline. */
dfff8a69 156
ee78dc32 157Lisp_Object Vx_pointer_shape, Vx_nontext_pointer_shape, Vx_mode_pointer_shape;
c9b2104d 158Lisp_Object Vx_hourglass_pointer_shape, Vx_window_horizontal_drag_shape, Vx_hand_shape;
6fc2811b 159
ee78dc32 160/* The shape when over mouse-sensitive text. */
dfff8a69 161
ee78dc32
GV
162Lisp_Object Vx_sensitive_text_pointer_shape;
163
c9b2104d
JR
164#ifndef IDC_HAND
165#define IDC_HAND MAKEINTRESOURCE(32649)
166#endif
167
ee78dc32 168/* Color of chars displayed in cursor box. */
dfff8a69 169
ee78dc32
GV
170Lisp_Object Vx_cursor_fore_pixel;
171
1edf84e7 172/* Nonzero if using Windows. */
dfff8a69 173
1edf84e7
GV
174static int w32_in_use;
175
ee78dc32 176/* Search path for bitmap files. */
dfff8a69 177
ee78dc32
GV
178Lisp_Object Vx_bitmap_file_path;
179
4587b026 180/* Regexp matching a font name whose width is the same as `PIXEL_SIZE'. */
dfff8a69 181
4587b026
GV
182Lisp_Object Vx_pixel_size_width_font_regexp;
183
33d52f9c
GV
184/* Alist of bdf fonts and the files that define them. */
185Lisp_Object Vw32_bdf_filename_alist;
186
f46e6225 187/* A flag to control whether fonts are matched strictly or not. */
1075afa9
GV
188int w32_strict_fontnames;
189
c0611964
AI
190/* A flag to control whether we should only repaint if GetUpdateRect
191 indicates there is an update region. */
192int w32_strict_painting;
193
dfff8a69
JR
194/* Associative list linking character set strings to Windows codepages. */
195Lisp_Object Vw32_charset_info_alist;
196
197/* VIETNAMESE_CHARSET is not defined in some versions of MSVC. */
198#ifndef VIETNAMESE_CHARSET
199#define VIETNAMESE_CHARSET 163
200#endif
201
ee78dc32
GV
202Lisp_Object Qauto_raise;
203Lisp_Object Qauto_lower;
ee78dc32
GV
204Lisp_Object Qborder_color;
205Lisp_Object Qborder_width;
ab0bb234 206extern Lisp_Object Qbox;
ee78dc32
GV
207Lisp_Object Qcursor_color;
208Lisp_Object Qcursor_type;
ee78dc32
GV
209Lisp_Object Qgeometry;
210Lisp_Object Qicon_left;
211Lisp_Object Qicon_top;
212Lisp_Object Qicon_type;
213Lisp_Object Qicon_name;
214Lisp_Object Qinternal_border_width;
215Lisp_Object Qleft;
1026b400 216Lisp_Object Qright;
ee78dc32
GV
217Lisp_Object Qmouse_color;
218Lisp_Object Qnone;
219Lisp_Object Qparent_id;
220Lisp_Object Qscroll_bar_width;
221Lisp_Object Qsuppress_icon;
ee78dc32
GV
222Lisp_Object Qundefined_color;
223Lisp_Object Qvertical_scroll_bars;
224Lisp_Object Qvisibility;
225Lisp_Object Qwindow_id;
226Lisp_Object Qx_frame_parameter;
227Lisp_Object Qx_resource_name;
228Lisp_Object Quser_position;
229Lisp_Object Quser_size;
6fc2811b 230Lisp_Object Qscreen_gamma;
dfff8a69
JR
231Lisp_Object Qline_spacing;
232Lisp_Object Qcenter;
dc220243 233Lisp_Object Qcancel_timer;
adcc3809
GV
234Lisp_Object Qhyper;
235Lisp_Object Qsuper;
236Lisp_Object Qmeta;
237Lisp_Object Qalt;
238Lisp_Object Qctrl;
239Lisp_Object Qcontrol;
240Lisp_Object Qshift;
241
dfff8a69
JR
242Lisp_Object Qw32_charset_ansi;
243Lisp_Object Qw32_charset_default;
244Lisp_Object Qw32_charset_symbol;
245Lisp_Object Qw32_charset_shiftjis;
767b1ff0 246Lisp_Object Qw32_charset_hangeul;
dfff8a69
JR
247Lisp_Object Qw32_charset_gb2312;
248Lisp_Object Qw32_charset_chinesebig5;
249Lisp_Object Qw32_charset_oem;
250
71eab8d1
AI
251#ifndef JOHAB_CHARSET
252#define JOHAB_CHARSET 130
253#endif
dfff8a69
JR
254#ifdef JOHAB_CHARSET
255Lisp_Object Qw32_charset_easteurope;
256Lisp_Object Qw32_charset_turkish;
257Lisp_Object Qw32_charset_baltic;
258Lisp_Object Qw32_charset_russian;
259Lisp_Object Qw32_charset_arabic;
260Lisp_Object Qw32_charset_greek;
261Lisp_Object Qw32_charset_hebrew;
767b1ff0 262Lisp_Object Qw32_charset_vietnamese;
dfff8a69
JR
263Lisp_Object Qw32_charset_thai;
264Lisp_Object Qw32_charset_johab;
265Lisp_Object Qw32_charset_mac;
266#endif
267
268#ifdef UNICODE_CHARSET
269Lisp_Object Qw32_charset_unicode;
270#endif
271
f7b9d4d1
JR
272Lisp_Object Qfullscreen;
273Lisp_Object Qfullwidth;
274Lisp_Object Qfullheight;
275Lisp_Object Qfullboth;
276
6fc2811b
JR
277extern Lisp_Object Qtop;
278extern Lisp_Object Qdisplay;
6fc2811b 279
5ac45f98
GV
280/* State variables for emulating a three button mouse. */
281#define LMOUSE 1
282#define MMOUSE 2
283#define RMOUSE 4
284
285static int button_state = 0;
fbd6baed 286static W32Msg saved_mouse_button_msg;
48094ace 287static unsigned mouse_button_timer = 0; /* non-zero when timer is active */
fbd6baed 288static W32Msg saved_mouse_move_msg;
48094ace 289static unsigned mouse_move_timer = 0;
84fb1139 290
9eb16b62
JR
291/* Window that is tracking the mouse. */
292static HWND track_mouse_window;
f60ae425
BK
293
294typedef BOOL (WINAPI * TrackMouseEvent_Proc) (
295 IN OUT LPTRACKMOUSEEVENT lpEventTrack
296 );
297
298TrackMouseEvent_Proc track_mouse_event_fn=NULL;
9eb16b62 299
93fbe8b7 300/* W95 mousewheel handler */
7d0393cf 301unsigned int msh_mousewheel = 0;
93fbe8b7 302
48094ace 303/* Timers */
84fb1139
KH
304#define MOUSE_BUTTON_ID 1
305#define MOUSE_MOVE_ID 2
48094ace
JR
306#define MENU_FREE_ID 3
307/* The delay (milliseconds) before a menu is freed after WM_EXITMENULOOP
308 is received. */
309#define MENU_FREE_DELAY 1000
310static unsigned menu_free_timer = 0;
5ac45f98 311
ee78dc32 312/* The below are defined in frame.c. */
dfff8a69 313
ee78dc32 314extern Lisp_Object Qheight, Qminibuffer, Qname, Qonly, Qwidth;
1edf84e7 315extern Lisp_Object Qunsplittable, Qmenu_bar_lines, Qbuffer_predicate, Qtitle;
6fc2811b 316extern Lisp_Object Qtool_bar_lines;
ee78dc32
GV
317
318extern Lisp_Object Vwindow_system_version;
319
4b817373
RS
320Lisp_Object Qface_set_after_frame_default;
321
937e601e
AI
322#ifdef GLYPH_DEBUG
323int image_cache_refcount, dpyinfo_refcount;
324#endif
325
326
fbd6baed
GV
327/* From w32term.c. */
328extern Lisp_Object Vw32_num_mouse_buttons;
ccc2d29c 329extern Lisp_Object Vw32_recognize_altgr;
5ac45f98 330
65906840 331extern HWND w32_system_caret_hwnd;
93f2ca61 332
65906840
JR
333extern int w32_system_caret_height;
334extern int w32_system_caret_x;
335extern int w32_system_caret_y;
93f2ca61
JR
336extern int w32_use_visible_system_caret;
337
d285988b 338static HWND w32_visible_system_caret_hwnd;
65906840 339
ee78dc32 340\f
1edf84e7
GV
341/* Error if we are not connected to MS-Windows. */
342void
343check_w32 ()
344{
345 if (! w32_in_use)
346 error ("MS-Windows not in use or not initialized");
347}
348
349/* Nonzero if we can use mouse menus.
350 You should not call this unless HAVE_MENUS is defined. */
7d0393cf 351
1edf84e7
GV
352int
353have_menus_p ()
354{
355 return w32_in_use;
356}
357
ee78dc32 358/* Extract a frame as a FRAME_PTR, defaulting to the selected frame
fbd6baed 359 and checking validity for W32. */
ee78dc32
GV
360
361FRAME_PTR
362check_x_frame (frame)
363 Lisp_Object frame;
364{
365 FRAME_PTR f;
366
367 if (NILP (frame))
6fc2811b 368 frame = selected_frame;
b7826503 369 CHECK_LIVE_FRAME (frame);
6fc2811b 370 f = XFRAME (frame);
fbd6baed
GV
371 if (! FRAME_W32_P (f))
372 error ("non-w32 frame used");
ee78dc32
GV
373 return f;
374}
375
7d0393cf 376/* Let the user specify a display with a frame.
fbd6baed 377 nil stands for the selected frame--or, if that is not a w32 frame,
ee78dc32
GV
378 the first display on the list. */
379
fbd6baed 380static struct w32_display_info *
ee78dc32
GV
381check_x_display_info (frame)
382 Lisp_Object frame;
383{
384 if (NILP (frame))
385 {
6fc2811b 386 struct frame *sf = XFRAME (selected_frame);
7d0393cf 387
6fc2811b
JR
388 if (FRAME_W32_P (sf) && FRAME_LIVE_P (sf))
389 return FRAME_W32_DISPLAY_INFO (sf);
ee78dc32 390 else
fbd6baed 391 return &one_w32_display_info;
ee78dc32
GV
392 }
393 else if (STRINGP (frame))
394 return x_display_info_for_name (frame);
395 else
396 {
397 FRAME_PTR f;
398
b7826503 399 CHECK_LIVE_FRAME (frame);
ee78dc32 400 f = XFRAME (frame);
fbd6baed
GV
401 if (! FRAME_W32_P (f))
402 error ("non-w32 frame used");
403 return FRAME_W32_DISPLAY_INFO (f);
ee78dc32
GV
404 }
405}
406\f
fbd6baed 407/* Return the Emacs frame-object corresponding to an w32 window.
ee78dc32
GV
408 It could be the frame's main window or an icon window. */
409
410/* This function can be called during GC, so use GC_xxx type test macros. */
411
412struct frame *
413x_window_to_frame (dpyinfo, wdesc)
fbd6baed 414 struct w32_display_info *dpyinfo;
ee78dc32
GV
415 HWND wdesc;
416{
417 Lisp_Object tail, frame;
418 struct frame *f;
419
8e713be6 420 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
ee78dc32 421 {
8e713be6 422 frame = XCAR (tail);
ee78dc32
GV
423 if (!GC_FRAMEP (frame))
424 continue;
425 f = XFRAME (frame);
2d764c78 426 if (!FRAME_W32_P (f) || FRAME_W32_DISPLAY_INFO (f) != dpyinfo)
ee78dc32 427 continue;
0af913d7 428 if (f->output_data.w32->hourglass_window == wdesc)
f79e6790
JR
429 return f;
430
fbd6baed 431 if (FRAME_W32_WINDOW (f) == wdesc)
ee78dc32
GV
432 return f;
433 }
434 return 0;
435}
436
437\f
438
439/* Code to deal with bitmaps. Bitmaps are referenced by their bitmap
440 id, which is just an int that this section returns. Bitmaps are
441 reference counted so they can be shared among frames.
442
443 Bitmap indices are guaranteed to be > 0, so a negative number can
444 be used to indicate no bitmap.
445
446 If you use x_create_bitmap_from_data, then you must keep track of
447 the bitmaps yourself. That is, creating a bitmap from the same
448 data more than once will not be caught. */
449
450
451/* Functions to access the contents of a bitmap, given an id. */
452
453int
454x_bitmap_height (f, id)
455 FRAME_PTR f;
456 int id;
457{
fbd6baed 458 return FRAME_W32_DISPLAY_INFO (f)->bitmaps[id - 1].height;
ee78dc32
GV
459}
460
461int
462x_bitmap_width (f, id)
463 FRAME_PTR f;
464 int id;
465{
fbd6baed 466 return FRAME_W32_DISPLAY_INFO (f)->bitmaps[id - 1].width;
ee78dc32
GV
467}
468
469int
470x_bitmap_pixmap (f, id)
471 FRAME_PTR f;
472 int id;
473{
fbd6baed 474 return (int) FRAME_W32_DISPLAY_INFO (f)->bitmaps[id - 1].pixmap;
ee78dc32
GV
475}
476
477
478/* Allocate a new bitmap record. Returns index of new record. */
479
480static int
481x_allocate_bitmap_record (f)
482 FRAME_PTR f;
483{
fbd6baed 484 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
ee78dc32
GV
485 int i;
486
487 if (dpyinfo->bitmaps == NULL)
488 {
489 dpyinfo->bitmaps_size = 10;
490 dpyinfo->bitmaps
fbd6baed 491 = (struct w32_bitmap_record *) xmalloc (dpyinfo->bitmaps_size * sizeof (struct w32_bitmap_record));
ee78dc32
GV
492 dpyinfo->bitmaps_last = 1;
493 return 1;
494 }
495
496 if (dpyinfo->bitmaps_last < dpyinfo->bitmaps_size)
497 return ++dpyinfo->bitmaps_last;
498
499 for (i = 0; i < dpyinfo->bitmaps_size; ++i)
500 if (dpyinfo->bitmaps[i].refcount == 0)
501 return i + 1;
502
503 dpyinfo->bitmaps_size *= 2;
504 dpyinfo->bitmaps
fbd6baed
GV
505 = (struct w32_bitmap_record *) xrealloc (dpyinfo->bitmaps,
506 dpyinfo->bitmaps_size * sizeof (struct w32_bitmap_record));
ee78dc32
GV
507 return ++dpyinfo->bitmaps_last;
508}
509
510/* Add one reference to the reference count of the bitmap with id ID. */
511
512void
513x_reference_bitmap (f, id)
514 FRAME_PTR f;
515 int id;
516{
fbd6baed 517 ++FRAME_W32_DISPLAY_INFO (f)->bitmaps[id - 1].refcount;
ee78dc32
GV
518}
519
520/* Create a bitmap for frame F from a HEIGHT x WIDTH array of bits at BITS. */
521
522int
523x_create_bitmap_from_data (f, bits, width, height)
524 struct frame *f;
525 char *bits;
526 unsigned int width, height;
527{
fbd6baed 528 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
ee78dc32
GV
529 Pixmap bitmap;
530 int id;
531
532 bitmap = CreateBitmap (width, height,
fbd6baed
GV
533 FRAME_W32_DISPLAY_INFO (XFRAME (frame))->n_planes,
534 FRAME_W32_DISPLAY_INFO (XFRAME (frame))->n_cbits,
ee78dc32
GV
535 bits);
536
537 if (! bitmap)
538 return -1;
539
540 id = x_allocate_bitmap_record (f);
541 dpyinfo->bitmaps[id - 1].pixmap = bitmap;
542 dpyinfo->bitmaps[id - 1].file = NULL;
543 dpyinfo->bitmaps[id - 1].hinst = NULL;
544 dpyinfo->bitmaps[id - 1].refcount = 1;
545 dpyinfo->bitmaps[id - 1].depth = 1;
546 dpyinfo->bitmaps[id - 1].height = height;
547 dpyinfo->bitmaps[id - 1].width = width;
548
549 return id;
550}
551
552/* Create bitmap from file FILE for frame F. */
553
554int
555x_create_bitmap_from_file (f, file)
556 struct frame *f;
557 Lisp_Object file;
558{
559 return -1;
767b1ff0 560#if 0 /* TODO : bitmap support */
fbd6baed 561 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
ee78dc32 562 unsigned int width, height;
6fc2811b 563 HBITMAP bitmap;
ee78dc32
GV
564 int xhot, yhot, result, id;
565 Lisp_Object found;
566 int fd;
567 char *filename;
568 HINSTANCE hinst;
569
570 /* Look for an existing bitmap with the same name. */
571 for (id = 0; id < dpyinfo->bitmaps_last; ++id)
572 {
573 if (dpyinfo->bitmaps[id].refcount
574 && dpyinfo->bitmaps[id].file
d5db4077 575 && !strcmp (dpyinfo->bitmaps[id].file, (char *) SDATA (file)))
ee78dc32
GV
576 {
577 ++dpyinfo->bitmaps[id].refcount;
578 return id + 1;
579 }
580 }
581
582 /* Search bitmap-file-path for the file, if appropriate. */
de2413e9 583 fd = openp (Vx_bitmap_file_path, file, Qnil, &found, Qnil);
ee78dc32
GV
584 if (fd < 0)
585 return -1;
6fc2811b 586 emacs_close (fd);
ee78dc32 587
d5db4077 588 filename = (char *) SDATA (found);
ee78dc32
GV
589
590 hinst = LoadLibraryEx (filename, NULL, LOAD_LIBRARY_AS_DATAFILE);
591
592 if (hinst == NULL)
593 return -1;
594
7d0393cf 595
fbd6baed 596 result = XReadBitmapFile (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
ee78dc32
GV
597 filename, &width, &height, &bitmap, &xhot, &yhot);
598 if (result != BitmapSuccess)
599 return -1;
600
601 id = x_allocate_bitmap_record (f);
602 dpyinfo->bitmaps[id - 1].pixmap = bitmap;
603 dpyinfo->bitmaps[id - 1].refcount = 1;
d5db4077 604 dpyinfo->bitmaps[id - 1].file = (char *) xmalloc (SCHARS (file) + 1);
ee78dc32
GV
605 dpyinfo->bitmaps[id - 1].depth = 1;
606 dpyinfo->bitmaps[id - 1].height = height;
607 dpyinfo->bitmaps[id - 1].width = width;
d5db4077 608 strcpy (dpyinfo->bitmaps[id - 1].file, SDATA (file));
ee78dc32
GV
609
610 return id;
767b1ff0 611#endif /* TODO */
ee78dc32
GV
612}
613
614/* Remove reference to bitmap with id number ID. */
615
33d52f9c 616void
ee78dc32
GV
617x_destroy_bitmap (f, id)
618 FRAME_PTR f;
619 int id;
620{
fbd6baed 621 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
ee78dc32
GV
622
623 if (id > 0)
624 {
625 --dpyinfo->bitmaps[id - 1].refcount;
626 if (dpyinfo->bitmaps[id - 1].refcount == 0)
627 {
628 BLOCK_INPUT;
629 DeleteObject (dpyinfo->bitmaps[id - 1].pixmap);
630 if (dpyinfo->bitmaps[id - 1].file)
631 {
6fc2811b 632 xfree (dpyinfo->bitmaps[id - 1].file);
ee78dc32
GV
633 dpyinfo->bitmaps[id - 1].file = NULL;
634 }
635 UNBLOCK_INPUT;
636 }
637 }
638}
639
640/* Free all the bitmaps for the display specified by DPYINFO. */
641
642static void
643x_destroy_all_bitmaps (dpyinfo)
fbd6baed 644 struct w32_display_info *dpyinfo;
ee78dc32
GV
645{
646 int i;
647 for (i = 0; i < dpyinfo->bitmaps_last; i++)
648 if (dpyinfo->bitmaps[i].refcount > 0)
649 {
650 DeleteObject (dpyinfo->bitmaps[i].pixmap);
651 if (dpyinfo->bitmaps[i].file)
6fc2811b 652 xfree (dpyinfo->bitmaps[i].file);
ee78dc32
GV
653 }
654 dpyinfo->bitmaps_last = 0;
655}
656\f
fbd6baed 657/* Connect the frame-parameter names for W32 frames
ee78dc32
GV
658 to the ways of passing the parameter values to the window system.
659
660 The name of a parameter, as a Lisp symbol,
661 has an `x-frame-parameter' property which is an integer in Lisp
662 but can be interpreted as an `enum x_frame_parm' in C. */
663
664enum x_frame_parm
665{
666 X_PARM_FOREGROUND_COLOR,
667 X_PARM_BACKGROUND_COLOR,
668 X_PARM_MOUSE_COLOR,
669 X_PARM_CURSOR_COLOR,
670 X_PARM_BORDER_COLOR,
671 X_PARM_ICON_TYPE,
672 X_PARM_FONT,
673 X_PARM_BORDER_WIDTH,
674 X_PARM_INTERNAL_BORDER_WIDTH,
675 X_PARM_NAME,
676 X_PARM_AUTORAISE,
677 X_PARM_AUTOLOWER,
678 X_PARM_VERT_SCROLL_BAR,
679 X_PARM_VISIBILITY,
680 X_PARM_MENU_BAR_LINES
681};
682
683
684struct x_frame_parm_table
685{
686 char *name;
6fc2811b 687 void (*setter) P_ ((struct frame *, Lisp_Object, Lisp_Object));
ee78dc32
GV
688};
689
ca56d953
JR
690BOOL my_show_window P_ ((struct frame *, HWND, int));
691void my_set_window_pos P_ ((HWND, HWND, int, int, int, int, UINT));
937e601e
AI
692static Lisp_Object unwind_create_frame P_ ((Lisp_Object));
693static Lisp_Object unwind_create_tip_frame P_ ((Lisp_Object));
694static void x_change_window_heights P_ ((Lisp_Object, int));
767b1ff0 695/* TODO: Native Input Method support; see x_create_im. */
6fc2811b 696void x_set_foreground_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
dfff8a69 697static void x_set_line_spacing P_ ((struct frame *, Lisp_Object, Lisp_Object));
f7b9d4d1 698static void x_set_fullscreen P_ ((struct frame *, Lisp_Object, Lisp_Object));
6fc2811b
JR
699void x_set_background_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
700void x_set_mouse_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
701void x_set_cursor_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
702void x_set_border_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
703void x_set_cursor_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
704void x_set_icon_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
705void x_set_icon_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
706void x_set_font P_ ((struct frame *, Lisp_Object, Lisp_Object));
41c1bdd9 707static void x_set_fringe_width P_ ((struct frame *, Lisp_Object, Lisp_Object));
6fc2811b
JR
708void x_set_border_width P_ ((struct frame *, Lisp_Object, Lisp_Object));
709void x_set_internal_border_width P_ ((struct frame *, Lisp_Object,
710 Lisp_Object));
711void x_explicitly_set_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
712void x_set_autoraise P_ ((struct frame *, Lisp_Object, Lisp_Object));
713void x_set_autolower P_ ((struct frame *, Lisp_Object, Lisp_Object));
714void x_set_vertical_scroll_bars P_ ((struct frame *, Lisp_Object,
715 Lisp_Object));
716void x_set_visibility P_ ((struct frame *, Lisp_Object, Lisp_Object));
717void x_set_menu_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
718void x_set_scroll_bar_width P_ ((struct frame *, Lisp_Object, Lisp_Object));
719void x_set_title P_ ((struct frame *, Lisp_Object, Lisp_Object));
720void x_set_unsplittable P_ ((struct frame *, Lisp_Object, Lisp_Object));
721void x_set_tool_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
722static void x_set_screen_gamma P_ ((struct frame *, Lisp_Object, Lisp_Object));
3cf3436e
JR
723static void x_edge_detection P_ ((struct frame *, struct image *, Lisp_Object,
724 Lisp_Object));
ee78dc32
GV
725
726static struct x_frame_parm_table x_frame_parms[] =
727{
72e4adef
JR
728 {"auto-raise", x_set_autoraise},
729 {"auto-lower", x_set_autolower},
730 {"background-color", x_set_background_color},
731 {"border-color", x_set_border_color},
732 {"border-width", x_set_border_width},
733 {"cursor-color", x_set_cursor_color},
734 {"cursor-type", x_set_cursor_type},
735 {"font", x_set_font},
736 {"foreground-color", x_set_foreground_color},
737 {"icon-name", x_set_icon_name},
738 {"icon-type", x_set_icon_type},
739 {"internal-border-width", x_set_internal_border_width},
740 {"menu-bar-lines", x_set_menu_bar_lines},
741 {"mouse-color", x_set_mouse_color},
742 {"name", x_explicitly_set_name},
743 {"scroll-bar-width", x_set_scroll_bar_width},
744 {"title", x_set_title},
745 {"unsplittable", x_set_unsplittable},
746 {"vertical-scroll-bars", x_set_vertical_scroll_bars},
747 {"visibility", x_set_visibility},
748 {"tool-bar-lines", x_set_tool_bar_lines},
749 {"screen-gamma", x_set_screen_gamma},
750 {"line-spacing", x_set_line_spacing},
751 {"left-fringe", x_set_fringe_width},
f7b9d4d1
JR
752 {"right-fringe", x_set_fringe_width},
753 {"fullscreen", x_set_fullscreen},
ee78dc32
GV
754};
755
756/* Attach the `x-frame-parameter' properties to
fbd6baed 757 the Lisp symbol names of parameters relevant to W32. */
ee78dc32 758
dfff8a69 759void
ee78dc32
GV
760init_x_parm_symbols ()
761{
762 int i;
763
764 for (i = 0; i < sizeof (x_frame_parms) / sizeof (x_frame_parms[0]); i++)
765 Fput (intern (x_frame_parms[i].name), Qx_frame_parameter,
766 make_number (i));
767}
768\f
f7b9d4d1
JR
769/* Really try to move where we want to be in case of fullscreen. Some WMs
770 moves the window where we tell them. Some (mwm, twm) moves the outer
771 window manager window there instead.
772 Try to compensate for those WM here. */
773static void
774x_fullscreen_move (f, new_top, new_left)
775 struct frame *f;
776 int new_top;
777 int new_left;
778{
779 if (new_top != f->output_data.w32->top_pos
780 || new_left != f->output_data.w32->left_pos)
781 {
782 int move_x = new_left;
783 int move_y = new_top;
784
785 f->output_data.w32->want_fullscreen |= FULLSCREEN_MOVE_WAIT;
786 x_set_offset (f, move_x, move_y, 1);
787 }
788}
789
dfff8a69 790/* Change the parameters of frame F as specified by ALIST.
ee78dc32
GV
791 If a parameter is not specially recognized, do nothing;
792 otherwise call the `x_set_...' function for that parameter. */
793
794void
795x_set_frame_parameters (f, alist)
796 FRAME_PTR f;
797 Lisp_Object alist;
798{
799 Lisp_Object tail;
800
801 /* If both of these parameters are present, it's more efficient to
802 set them both at once. So we wait until we've looked at the
803 entire list before we set them. */
b839712d 804 int width, height;
ee78dc32
GV
805
806 /* Same here. */
807 Lisp_Object left, top;
808
809 /* Same with these. */
810 Lisp_Object icon_left, icon_top;
811
812 /* Record in these vectors all the parms specified. */
813 Lisp_Object *parms;
814 Lisp_Object *values;
a797a73d 815 int i, p;
ee78dc32
GV
816 int left_no_change = 0, top_no_change = 0;
817 int icon_left_no_change = 0, icon_top_no_change = 0;
f7b9d4d1 818 int fullscreen_is_being_set = 0;
ee78dc32 819
5878523b
RS
820 struct gcpro gcpro1, gcpro2;
821
ee78dc32
GV
822 i = 0;
823 for (tail = alist; CONSP (tail); tail = Fcdr (tail))
824 i++;
825
826 parms = (Lisp_Object *) alloca (i * sizeof (Lisp_Object));
827 values = (Lisp_Object *) alloca (i * sizeof (Lisp_Object));
828
829 /* Extract parm names and values into those vectors. */
830
831 i = 0;
832 for (tail = alist; CONSP (tail); tail = Fcdr (tail))
833 {
6fc2811b 834 Lisp_Object elt;
ee78dc32
GV
835
836 elt = Fcar (tail);
837 parms[i] = Fcar (elt);
838 values[i] = Fcdr (elt);
839 i++;
840 }
5878523b
RS
841 /* TAIL and ALIST are not used again below here. */
842 alist = tail = Qnil;
843
844 GCPRO2 (*parms, *values);
845 gcpro1.nvars = i;
846 gcpro2.nvars = i;
847
848 /* There is no need to gcpro LEFT, TOP, ICON_LEFT, or ICON_TOP,
849 because their values appear in VALUES and strings are not valid. */
b839712d 850 top = left = Qunbound;
ee78dc32
GV
851 icon_left = icon_top = Qunbound;
852
b839712d 853 /* Provide default values for HEIGHT and WIDTH. */
dfff8a69
JR
854 if (FRAME_NEW_WIDTH (f))
855 width = FRAME_NEW_WIDTH (f);
856 else
857 width = FRAME_WIDTH (f);
858
859 if (FRAME_NEW_HEIGHT (f))
860 height = FRAME_NEW_HEIGHT (f);
861 else
862 height = FRAME_HEIGHT (f);
b839712d 863
a797a73d
GV
864 /* Process foreground_color and background_color before anything else.
865 They are independent of other properties, but other properties (e.g.,
866 cursor_color) are dependent upon them. */
41c1bdd9 867 /* Process default font as well, since fringe widths depends on it. */
7d0393cf 868 for (p = 0; p < i; p++)
a797a73d
GV
869 {
870 Lisp_Object prop, val;
871
872 prop = parms[p];
873 val = values[p];
41c1bdd9
KS
874 if (EQ (prop, Qforeground_color)
875 || EQ (prop, Qbackground_color)
f7b9d4d1
JR
876 || EQ (prop, Qfont)
877 || EQ (prop, Qfullscreen))
a797a73d
GV
878 {
879 register Lisp_Object param_index, old_value;
880
a797a73d 881 old_value = get_frame_param (f, prop);
f7b9d4d1 882 fullscreen_is_being_set |= EQ (prop, Qfullscreen);
a05e2bae
JR
883
884 if (NILP (Fequal (val, old_value)))
885 {
886 store_frame_param (f, prop, val);
7d0393cf 887
a05e2bae
JR
888 param_index = Fget (prop, Qx_frame_parameter);
889 if (NATNUMP (param_index)
890 && (XFASTINT (param_index)
891 < sizeof (x_frame_parms)/sizeof (x_frame_parms[0])))
892 (*x_frame_parms[XINT (param_index)].setter)(f, val, old_value);
893 }
a797a73d
GV
894 }
895 }
896
ee78dc32
GV
897 /* Now process them in reverse of specified order. */
898 for (i--; i >= 0; i--)
899 {
900 Lisp_Object prop, val;
901
902 prop = parms[i];
903 val = values[i];
904
b839712d
RS
905 if (EQ (prop, Qwidth) && NUMBERP (val))
906 width = XFASTINT (val);
907 else if (EQ (prop, Qheight) && NUMBERP (val))
908 height = XFASTINT (val);
ee78dc32
GV
909 else if (EQ (prop, Qtop))
910 top = val;
911 else if (EQ (prop, Qleft))
912 left = val;
913 else if (EQ (prop, Qicon_top))
914 icon_top = val;
915 else if (EQ (prop, Qicon_left))
916 icon_left = val;
41c1bdd9
KS
917 else if (EQ (prop, Qforeground_color)
918 || EQ (prop, Qbackground_color)
f7b9d4d1
JR
919 || EQ (prop, Qfont)
920 || EQ (prop, Qfullscreen))
a797a73d
GV
921 /* Processed above. */
922 continue;
ee78dc32
GV
923 else
924 {
925 register Lisp_Object param_index, old_value;
926
ee78dc32 927 old_value = get_frame_param (f, prop);
a05e2bae 928
ee78dc32 929 store_frame_param (f, prop, val);
a05e2bae
JR
930
931 param_index = Fget (prop, Qx_frame_parameter);
ee78dc32
GV
932 if (NATNUMP (param_index)
933 && (XFASTINT (param_index)
934 < sizeof (x_frame_parms)/sizeof (x_frame_parms[0])))
3c190163 935 (*x_frame_parms[XINT (param_index)].setter)(f, val, old_value);
ee78dc32
GV
936 }
937 }
938
939 /* Don't die if just one of these was set. */
940 if (EQ (left, Qunbound))
941 {
942 left_no_change = 1;
fbd6baed
GV
943 if (f->output_data.w32->left_pos < 0)
944 left = Fcons (Qplus, Fcons (make_number (f->output_data.w32->left_pos), Qnil));
ee78dc32 945 else
fbd6baed 946 XSETINT (left, f->output_data.w32->left_pos);
ee78dc32
GV
947 }
948 if (EQ (top, Qunbound))
949 {
950 top_no_change = 1;
fbd6baed
GV
951 if (f->output_data.w32->top_pos < 0)
952 top = Fcons (Qplus, Fcons (make_number (f->output_data.w32->top_pos), Qnil));
ee78dc32 953 else
fbd6baed 954 XSETINT (top, f->output_data.w32->top_pos);
ee78dc32
GV
955 }
956
957 /* If one of the icon positions was not set, preserve or default it. */
958 if (EQ (icon_left, Qunbound) || ! INTEGERP (icon_left))
959 {
960 icon_left_no_change = 1;
961 icon_left = Fcdr (Fassq (Qicon_left, f->param_alist));
962 if (NILP (icon_left))
963 XSETINT (icon_left, 0);
964 }
965 if (EQ (icon_top, Qunbound) || ! INTEGERP (icon_top))
966 {
967 icon_top_no_change = 1;
968 icon_top = Fcdr (Fassq (Qicon_top, f->param_alist));
969 if (NILP (icon_top))
970 XSETINT (icon_top, 0);
971 }
972
f7b9d4d1
JR
973 if (FRAME_VISIBLE_P (f) && fullscreen_is_being_set)
974 {
975 /* If the frame is visible already and the fullscreen parameter is
976 being set, it is too late to set WM manager hints to specify
977 size and position.
978 Here we first get the width, height and position that applies to
979 fullscreen. We then move the frame to the appropriate
980 position. Resize of the frame is taken care of in the code after
981 this if-statement. */
982 int new_left, new_top;
7d0393cf 983
f7b9d4d1
JR
984 x_fullscreen_adjust (f, &width, &height, &new_top, &new_left);
985 x_fullscreen_move (f, new_top, new_left);
986 }
987
ee78dc32
GV
988 /* Don't set these parameters unless they've been explicitly
989 specified. The window might be mapped or resized while we're in
990 this function, and we don't want to override that unless the lisp
991 code has asked for it.
992
993 Don't set these parameters unless they actually differ from the
994 window's current parameters; the window may not actually exist
995 yet. */
996 {
997 Lisp_Object frame;
998
999 check_frame_size (f, &height, &width);
1000
1001 XSETFRAME (frame, f);
1002
dfff8a69
JR
1003 if (width != FRAME_WIDTH (f)
1004 || height != FRAME_HEIGHT (f)
1005 || FRAME_NEW_HEIGHT (f) || FRAME_NEW_WIDTH (f))
b839712d 1006 Fset_frame_size (frame, make_number (width), make_number (height));
ee78dc32
GV
1007
1008 if ((!NILP (left) || !NILP (top))
1009 && ! (left_no_change && top_no_change)
fbd6baed
GV
1010 && ! (NUMBERP (left) && XINT (left) == f->output_data.w32->left_pos
1011 && NUMBERP (top) && XINT (top) == f->output_data.w32->top_pos))
ee78dc32
GV
1012 {
1013 int leftpos = 0;
1014 int toppos = 0;
1015
1016 /* Record the signs. */
fbd6baed 1017 f->output_data.w32->size_hint_flags &= ~ (XNegative | YNegative);
ee78dc32 1018 if (EQ (left, Qminus))
fbd6baed 1019 f->output_data.w32->size_hint_flags |= XNegative;
ee78dc32
GV
1020 else if (INTEGERP (left))
1021 {
1022 leftpos = XINT (left);
1023 if (leftpos < 0)
fbd6baed 1024 f->output_data.w32->size_hint_flags |= XNegative;
ee78dc32 1025 }
8e713be6
KR
1026 else if (CONSP (left) && EQ (XCAR (left), Qminus)
1027 && CONSP (XCDR (left))
1028 && INTEGERP (XCAR (XCDR (left))))
ee78dc32 1029 {
8e713be6 1030 leftpos = - XINT (XCAR (XCDR (left)));
fbd6baed 1031 f->output_data.w32->size_hint_flags |= XNegative;
ee78dc32 1032 }
8e713be6
KR
1033 else if (CONSP (left) && EQ (XCAR (left), Qplus)
1034 && CONSP (XCDR (left))
1035 && INTEGERP (XCAR (XCDR (left))))
ee78dc32 1036 {
8e713be6 1037 leftpos = XINT (XCAR (XCDR (left)));
ee78dc32
GV
1038 }
1039
1040 if (EQ (top, Qminus))
fbd6baed 1041 f->output_data.w32->size_hint_flags |= YNegative;
ee78dc32
GV
1042 else if (INTEGERP (top))
1043 {
1044 toppos = XINT (top);
1045 if (toppos < 0)
fbd6baed 1046 f->output_data.w32->size_hint_flags |= YNegative;
ee78dc32 1047 }
8e713be6
KR
1048 else if (CONSP (top) && EQ (XCAR (top), Qminus)
1049 && CONSP (XCDR (top))
1050 && INTEGERP (XCAR (XCDR (top))))
ee78dc32 1051 {
8e713be6 1052 toppos = - XINT (XCAR (XCDR (top)));
fbd6baed 1053 f->output_data.w32->size_hint_flags |= YNegative;
ee78dc32 1054 }
8e713be6
KR
1055 else if (CONSP (top) && EQ (XCAR (top), Qplus)
1056 && CONSP (XCDR (top))
1057 && INTEGERP (XCAR (XCDR (top))))
ee78dc32 1058 {
8e713be6 1059 toppos = XINT (XCAR (XCDR (top)));
ee78dc32
GV
1060 }
1061
1062
1063 /* Store the numeric value of the position. */
fbd6baed
GV
1064 f->output_data.w32->top_pos = toppos;
1065 f->output_data.w32->left_pos = leftpos;
ee78dc32 1066
fbd6baed 1067 f->output_data.w32->win_gravity = NorthWestGravity;
ee78dc32
GV
1068
1069 /* Actually set that position, and convert to absolute. */
1070 x_set_offset (f, leftpos, toppos, -1);
1071 }
1072
1073 if ((!NILP (icon_left) || !NILP (icon_top))
1074 && ! (icon_left_no_change && icon_top_no_change))
1075 x_wm_set_icon_position (f, XINT (icon_left), XINT (icon_top));
1076 }
5878523b
RS
1077
1078 UNGCPRO;
ee78dc32
GV
1079}
1080
1081/* Store the screen positions of frame F into XPTR and YPTR.
1082 These are the positions of the containing window manager window,
1083 not Emacs's own window. */
1084
1085void
1086x_real_positions (f, xptr, yptr)
1087 FRAME_PTR f;
1088 int *xptr, *yptr;
1089{
1090 POINT pt;
f7b9d4d1 1091 RECT rect;
3c190163 1092
f7b9d4d1
JR
1093 GetClientRect(FRAME_W32_WINDOW(f), &rect);
1094 AdjustWindowRect(&rect, f->output_data.w32->dwStyle, FRAME_EXTERNAL_MENU_BAR(f));
1095
1096 pt.x = rect.left;
1097 pt.y = rect.top;
ee78dc32 1098
fbd6baed 1099 ClientToScreen (FRAME_W32_WINDOW(f), &pt);
ee78dc32 1100
f7b9d4d1
JR
1101 /* Remember x_pixels_diff and y_pixels_diff. */
1102 f->output_data.w32->x_pixels_diff = pt.x - rect.left;
1103 f->output_data.w32->y_pixels_diff = pt.y - rect.top;
1104
ee78dc32
GV
1105 *xptr = pt.x;
1106 *yptr = pt.y;
1107}
1108
1109/* Insert a description of internally-recorded parameters of frame X
1110 into the parameter alist *ALISTPTR that is to be given to the user.
fbd6baed 1111 Only parameters that are specific to W32
ee78dc32
GV
1112 and whose values are not correctly recorded in the frame's
1113 param_alist need to be considered here. */
1114
dfff8a69 1115void
ee78dc32
GV
1116x_report_frame_params (f, alistptr)
1117 struct frame *f;
1118 Lisp_Object *alistptr;
1119{
1120 char buf[16];
1121 Lisp_Object tem;
1122
1123 /* Represent negative positions (off the top or left screen edge)
1124 in a way that Fmodify_frame_parameters will understand correctly. */
fbd6baed
GV
1125 XSETINT (tem, f->output_data.w32->left_pos);
1126 if (f->output_data.w32->left_pos >= 0)
ee78dc32
GV
1127 store_in_alist (alistptr, Qleft, tem);
1128 else
1129 store_in_alist (alistptr, Qleft, Fcons (Qplus, Fcons (tem, Qnil)));
1130
fbd6baed
GV
1131 XSETINT (tem, f->output_data.w32->top_pos);
1132 if (f->output_data.w32->top_pos >= 0)
ee78dc32
GV
1133 store_in_alist (alistptr, Qtop, tem);
1134 else
1135 store_in_alist (alistptr, Qtop, Fcons (Qplus, Fcons (tem, Qnil)));
1136
1137 store_in_alist (alistptr, Qborder_width,
fbd6baed 1138 make_number (f->output_data.w32->border_width));
ee78dc32 1139 store_in_alist (alistptr, Qinternal_border_width,
fbd6baed 1140 make_number (f->output_data.w32->internal_border_width));
e90c3f90
KS
1141 store_in_alist (alistptr, Qleft_fringe,
1142 make_number (f->output_data.w32->left_fringe_width));
1143 store_in_alist (alistptr, Qright_fringe,
1144 make_number (f->output_data.w32->right_fringe_width));
aa17b858
EZ
1145 store_in_alist (alistptr, Qscroll_bar_width,
1146 make_number (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
1147 ? FRAME_SCROLL_BAR_PIXEL_WIDTH(f)
1148 : 0));
fbd6baed 1149 sprintf (buf, "%ld", (long) FRAME_W32_WINDOW (f));
ee78dc32
GV
1150 store_in_alist (alistptr, Qwindow_id,
1151 build_string (buf));
1152 store_in_alist (alistptr, Qicon_name, f->icon_name);
1153 FRAME_SAMPLE_VISIBILITY (f);
1154 store_in_alist (alistptr, Qvisibility,
1155 (FRAME_VISIBLE_P (f) ? Qt
1156 : FRAME_ICONIFIED_P (f) ? Qicon : Qnil));
1157 store_in_alist (alistptr, Qdisplay,
8e713be6 1158 XCAR (FRAME_W32_DISPLAY_INFO (f)->name_list_element));
ee78dc32
GV
1159}
1160\f
1161
74e1aeec
JR
1162DEFUN ("w32-define-rgb-color", Fw32_define_rgb_color,
1163 Sw32_define_rgb_color, 4, 4, 0,
1164 doc: /* Convert RGB numbers to a windows color reference and associate with NAME.
1165This adds or updates a named color to w32-color-map, making it
1166available for use. The original entry's RGB ref is returned, or nil
1167if the entry is new. */)
5ac45f98
GV
1168 (red, green, blue, name)
1169 Lisp_Object red, green, blue, name;
ee78dc32 1170{
5ac45f98
GV
1171 Lisp_Object rgb;
1172 Lisp_Object oldrgb = Qnil;
1173 Lisp_Object entry;
1174
b7826503
PJ
1175 CHECK_NUMBER (red);
1176 CHECK_NUMBER (green);
1177 CHECK_NUMBER (blue);
1178 CHECK_STRING (name);
ee78dc32 1179
5ac45f98 1180 XSET (rgb, Lisp_Int, RGB(XUINT (red), XUINT (green), XUINT (blue)));
ee78dc32 1181
5ac45f98 1182 BLOCK_INPUT;
ee78dc32 1183
fbd6baed
GV
1184 /* replace existing entry in w32-color-map or add new entry. */
1185 entry = Fassoc (name, Vw32_color_map);
5ac45f98
GV
1186 if (NILP (entry))
1187 {
1188 entry = Fcons (name, rgb);
fbd6baed 1189 Vw32_color_map = Fcons (entry, Vw32_color_map);
5ac45f98
GV
1190 }
1191 else
1192 {
1193 oldrgb = Fcdr (entry);
1194 Fsetcdr (entry, rgb);
1195 }
1196
1197 UNBLOCK_INPUT;
1198
1199 return (oldrgb);
ee78dc32
GV
1200}
1201
74e1aeec
JR
1202DEFUN ("w32-load-color-file", Fw32_load_color_file,
1203 Sw32_load_color_file, 1, 1, 0,
1204 doc: /* Create an alist of color entries from an external file.
1205Assign this value to w32-color-map to replace the existing color map.
1206
1207The file should define one named RGB color per line like so:
1208 R G B name
1209where R,G,B are numbers between 0 and 255 and name is an arbitrary string. */)
5ac45f98
GV
1210 (filename)
1211 Lisp_Object filename;
1212{
1213 FILE *fp;
1214 Lisp_Object cmap = Qnil;
1215 Lisp_Object abspath;
1216
b7826503 1217 CHECK_STRING (filename);
5ac45f98
GV
1218 abspath = Fexpand_file_name (filename, Qnil);
1219
d5db4077 1220 fp = fopen (SDATA (filename), "rt");
5ac45f98
GV
1221 if (fp)
1222 {
1223 char buf[512];
1224 int red, green, blue;
1225 int num;
1226
1227 BLOCK_INPUT;
1228
1229 while (fgets (buf, sizeof (buf), fp) != NULL) {
1230 if (sscanf (buf, "%u %u %u %n", &red, &green, &blue, &num) == 3)
1231 {
1232 char *name = buf + num;
1233 num = strlen (name) - 1;
1234 if (name[num] == '\n')
1235 name[num] = 0;
1236 cmap = Fcons (Fcons (build_string (name),
1237 make_number (RGB (red, green, blue))),
1238 cmap);
1239 }
1240 }
1241 fclose (fp);
1242
1243 UNBLOCK_INPUT;
1244 }
1245
1246 return cmap;
1247}
ee78dc32 1248
fbd6baed 1249/* The default colors for the w32 color map */
7d0393cf 1250typedef struct colormap_t
ee78dc32
GV
1251{
1252 char *name;
1253 COLORREF colorref;
1254} colormap_t;
1255
7d0393cf 1256colormap_t w32_color_map[] =
ee78dc32 1257{
1da8a614
GV
1258 {"snow" , PALETTERGB (255,250,250)},
1259 {"ghost white" , PALETTERGB (248,248,255)},
1260 {"GhostWhite" , PALETTERGB (248,248,255)},
1261 {"white smoke" , PALETTERGB (245,245,245)},
1262 {"WhiteSmoke" , PALETTERGB (245,245,245)},
1263 {"gainsboro" , PALETTERGB (220,220,220)},
1264 {"floral white" , PALETTERGB (255,250,240)},
1265 {"FloralWhite" , PALETTERGB (255,250,240)},
1266 {"old lace" , PALETTERGB (253,245,230)},
1267 {"OldLace" , PALETTERGB (253,245,230)},
1268 {"linen" , PALETTERGB (250,240,230)},
1269 {"antique white" , PALETTERGB (250,235,215)},
1270 {"AntiqueWhite" , PALETTERGB (250,235,215)},
1271 {"papaya whip" , PALETTERGB (255,239,213)},
1272 {"PapayaWhip" , PALETTERGB (255,239,213)},
1273 {"blanched almond" , PALETTERGB (255,235,205)},
1274 {"BlanchedAlmond" , PALETTERGB (255,235,205)},
1275 {"bisque" , PALETTERGB (255,228,196)},
1276 {"peach puff" , PALETTERGB (255,218,185)},
1277 {"PeachPuff" , PALETTERGB (255,218,185)},
1278 {"navajo white" , PALETTERGB (255,222,173)},
1279 {"NavajoWhite" , PALETTERGB (255,222,173)},
1280 {"moccasin" , PALETTERGB (255,228,181)},
1281 {"cornsilk" , PALETTERGB (255,248,220)},
1282 {"ivory" , PALETTERGB (255,255,240)},
1283 {"lemon chiffon" , PALETTERGB (255,250,205)},
1284 {"LemonChiffon" , PALETTERGB (255,250,205)},
1285 {"seashell" , PALETTERGB (255,245,238)},
1286 {"honeydew" , PALETTERGB (240,255,240)},
1287 {"mint cream" , PALETTERGB (245,255,250)},
1288 {"MintCream" , PALETTERGB (245,255,250)},
1289 {"azure" , PALETTERGB (240,255,255)},
1290 {"alice blue" , PALETTERGB (240,248,255)},
1291 {"AliceBlue" , PALETTERGB (240,248,255)},
1292 {"lavender" , PALETTERGB (230,230,250)},
1293 {"lavender blush" , PALETTERGB (255,240,245)},
1294 {"LavenderBlush" , PALETTERGB (255,240,245)},
1295 {"misty rose" , PALETTERGB (255,228,225)},
1296 {"MistyRose" , PALETTERGB (255,228,225)},
1297 {"white" , PALETTERGB (255,255,255)},
1298 {"black" , PALETTERGB ( 0, 0, 0)},
1299 {"dark slate gray" , PALETTERGB ( 47, 79, 79)},
1300 {"DarkSlateGray" , PALETTERGB ( 47, 79, 79)},
1301 {"dark slate grey" , PALETTERGB ( 47, 79, 79)},
1302 {"DarkSlateGrey" , PALETTERGB ( 47, 79, 79)},
1303 {"dim gray" , PALETTERGB (105,105,105)},
1304 {"DimGray" , PALETTERGB (105,105,105)},
1305 {"dim grey" , PALETTERGB (105,105,105)},
1306 {"DimGrey" , PALETTERGB (105,105,105)},
1307 {"slate gray" , PALETTERGB (112,128,144)},
1308 {"SlateGray" , PALETTERGB (112,128,144)},
1309 {"slate grey" , PALETTERGB (112,128,144)},
1310 {"SlateGrey" , PALETTERGB (112,128,144)},
1311 {"light slate gray" , PALETTERGB (119,136,153)},
1312 {"LightSlateGray" , PALETTERGB (119,136,153)},
1313 {"light slate grey" , PALETTERGB (119,136,153)},
1314 {"LightSlateGrey" , PALETTERGB (119,136,153)},
1315 {"gray" , PALETTERGB (190,190,190)},
1316 {"grey" , PALETTERGB (190,190,190)},
1317 {"light grey" , PALETTERGB (211,211,211)},
1318 {"LightGrey" , PALETTERGB (211,211,211)},
1319 {"light gray" , PALETTERGB (211,211,211)},
1320 {"LightGray" , PALETTERGB (211,211,211)},
1321 {"midnight blue" , PALETTERGB ( 25, 25,112)},
1322 {"MidnightBlue" , PALETTERGB ( 25, 25,112)},
1323 {"navy" , PALETTERGB ( 0, 0,128)},
1324 {"navy blue" , PALETTERGB ( 0, 0,128)},
1325 {"NavyBlue" , PALETTERGB ( 0, 0,128)},
1326 {"cornflower blue" , PALETTERGB (100,149,237)},
1327 {"CornflowerBlue" , PALETTERGB (100,149,237)},
1328 {"dark slate blue" , PALETTERGB ( 72, 61,139)},
1329 {"DarkSlateBlue" , PALETTERGB ( 72, 61,139)},
1330 {"slate blue" , PALETTERGB (106, 90,205)},
1331 {"SlateBlue" , PALETTERGB (106, 90,205)},
1332 {"medium slate blue" , PALETTERGB (123,104,238)},
1333 {"MediumSlateBlue" , PALETTERGB (123,104,238)},
1334 {"light slate blue" , PALETTERGB (132,112,255)},
1335 {"LightSlateBlue" , PALETTERGB (132,112,255)},
1336 {"medium blue" , PALETTERGB ( 0, 0,205)},
1337 {"MediumBlue" , PALETTERGB ( 0, 0,205)},
1338 {"royal blue" , PALETTERGB ( 65,105,225)},
1339 {"RoyalBlue" , PALETTERGB ( 65,105,225)},
1340 {"blue" , PALETTERGB ( 0, 0,255)},
1341 {"dodger blue" , PALETTERGB ( 30,144,255)},
1342 {"DodgerBlue" , PALETTERGB ( 30,144,255)},
1343 {"deep sky blue" , PALETTERGB ( 0,191,255)},
1344 {"DeepSkyBlue" , PALETTERGB ( 0,191,255)},
1345 {"sky blue" , PALETTERGB (135,206,235)},
1346 {"SkyBlue" , PALETTERGB (135,206,235)},
1347 {"light sky blue" , PALETTERGB (135,206,250)},
1348 {"LightSkyBlue" , PALETTERGB (135,206,250)},
1349 {"steel blue" , PALETTERGB ( 70,130,180)},
1350 {"SteelBlue" , PALETTERGB ( 70,130,180)},
1351 {"light steel blue" , PALETTERGB (176,196,222)},
1352 {"LightSteelBlue" , PALETTERGB (176,196,222)},
1353 {"light blue" , PALETTERGB (173,216,230)},
1354 {"LightBlue" , PALETTERGB (173,216,230)},
1355 {"powder blue" , PALETTERGB (176,224,230)},
1356 {"PowderBlue" , PALETTERGB (176,224,230)},
1357 {"pale turquoise" , PALETTERGB (175,238,238)},
1358 {"PaleTurquoise" , PALETTERGB (175,238,238)},
1359 {"dark turquoise" , PALETTERGB ( 0,206,209)},
1360 {"DarkTurquoise" , PALETTERGB ( 0,206,209)},
1361 {"medium turquoise" , PALETTERGB ( 72,209,204)},
1362 {"MediumTurquoise" , PALETTERGB ( 72,209,204)},
1363 {"turquoise" , PALETTERGB ( 64,224,208)},
1364 {"cyan" , PALETTERGB ( 0,255,255)},
1365 {"light cyan" , PALETTERGB (224,255,255)},
1366 {"LightCyan" , PALETTERGB (224,255,255)},
1367 {"cadet blue" , PALETTERGB ( 95,158,160)},
1368 {"CadetBlue" , PALETTERGB ( 95,158,160)},
1369 {"medium aquamarine" , PALETTERGB (102,205,170)},
1370 {"MediumAquamarine" , PALETTERGB (102,205,170)},
1371 {"aquamarine" , PALETTERGB (127,255,212)},
1372 {"dark green" , PALETTERGB ( 0,100, 0)},
1373 {"DarkGreen" , PALETTERGB ( 0,100, 0)},
1374 {"dark olive green" , PALETTERGB ( 85,107, 47)},
1375 {"DarkOliveGreen" , PALETTERGB ( 85,107, 47)},
1376 {"dark sea green" , PALETTERGB (143,188,143)},
1377 {"DarkSeaGreen" , PALETTERGB (143,188,143)},
1378 {"sea green" , PALETTERGB ( 46,139, 87)},
1379 {"SeaGreen" , PALETTERGB ( 46,139, 87)},
1380 {"medium sea green" , PALETTERGB ( 60,179,113)},
1381 {"MediumSeaGreen" , PALETTERGB ( 60,179,113)},
1382 {"light sea green" , PALETTERGB ( 32,178,170)},
1383 {"LightSeaGreen" , PALETTERGB ( 32,178,170)},
1384 {"pale green" , PALETTERGB (152,251,152)},
1385 {"PaleGreen" , PALETTERGB (152,251,152)},
1386 {"spring green" , PALETTERGB ( 0,255,127)},
1387 {"SpringGreen" , PALETTERGB ( 0,255,127)},
1388 {"lawn green" , PALETTERGB (124,252, 0)},
1389 {"LawnGreen" , PALETTERGB (124,252, 0)},
1390 {"green" , PALETTERGB ( 0,255, 0)},
1391 {"chartreuse" , PALETTERGB (127,255, 0)},
1392 {"medium spring green" , PALETTERGB ( 0,250,154)},
1393 {"MediumSpringGreen" , PALETTERGB ( 0,250,154)},
1394 {"green yellow" , PALETTERGB (173,255, 47)},
1395 {"GreenYellow" , PALETTERGB (173,255, 47)},
1396 {"lime green" , PALETTERGB ( 50,205, 50)},
1397 {"LimeGreen" , PALETTERGB ( 50,205, 50)},
1398 {"yellow green" , PALETTERGB (154,205, 50)},
1399 {"YellowGreen" , PALETTERGB (154,205, 50)},
1400 {"forest green" , PALETTERGB ( 34,139, 34)},
1401 {"ForestGreen" , PALETTERGB ( 34,139, 34)},
1402 {"olive drab" , PALETTERGB (107,142, 35)},
1403 {"OliveDrab" , PALETTERGB (107,142, 35)},
1404 {"dark khaki" , PALETTERGB (189,183,107)},
1405 {"DarkKhaki" , PALETTERGB (189,183,107)},
1406 {"khaki" , PALETTERGB (240,230,140)},
1407 {"pale goldenrod" , PALETTERGB (238,232,170)},
1408 {"PaleGoldenrod" , PALETTERGB (238,232,170)},
1409 {"light goldenrod yellow" , PALETTERGB (250,250,210)},
1410 {"LightGoldenrodYellow" , PALETTERGB (250,250,210)},
1411 {"light yellow" , PALETTERGB (255,255,224)},
1412 {"LightYellow" , PALETTERGB (255,255,224)},
1413 {"yellow" , PALETTERGB (255,255, 0)},
1414 {"gold" , PALETTERGB (255,215, 0)},
1415 {"light goldenrod" , PALETTERGB (238,221,130)},
1416 {"LightGoldenrod" , PALETTERGB (238,221,130)},
1417 {"goldenrod" , PALETTERGB (218,165, 32)},
1418 {"dark goldenrod" , PALETTERGB (184,134, 11)},
1419 {"DarkGoldenrod" , PALETTERGB (184,134, 11)},
1420 {"rosy brown" , PALETTERGB (188,143,143)},
1421 {"RosyBrown" , PALETTERGB (188,143,143)},
1422 {"indian red" , PALETTERGB (205, 92, 92)},
1423 {"IndianRed" , PALETTERGB (205, 92, 92)},
1424 {"saddle brown" , PALETTERGB (139, 69, 19)},
1425 {"SaddleBrown" , PALETTERGB (139, 69, 19)},
1426 {"sienna" , PALETTERGB (160, 82, 45)},
1427 {"peru" , PALETTERGB (205,133, 63)},
1428 {"burlywood" , PALETTERGB (222,184,135)},
1429 {"beige" , PALETTERGB (245,245,220)},
1430 {"wheat" , PALETTERGB (245,222,179)},
1431 {"sandy brown" , PALETTERGB (244,164, 96)},
1432 {"SandyBrown" , PALETTERGB (244,164, 96)},
1433 {"tan" , PALETTERGB (210,180,140)},
1434 {"chocolate" , PALETTERGB (210,105, 30)},
1435 {"firebrick" , PALETTERGB (178,34, 34)},
1436 {"brown" , PALETTERGB (165,42, 42)},
1437 {"dark salmon" , PALETTERGB (233,150,122)},
1438 {"DarkSalmon" , PALETTERGB (233,150,122)},
1439 {"salmon" , PALETTERGB (250,128,114)},
1440 {"light salmon" , PALETTERGB (255,160,122)},
1441 {"LightSalmon" , PALETTERGB (255,160,122)},
1442 {"orange" , PALETTERGB (255,165, 0)},
1443 {"dark orange" , PALETTERGB (255,140, 0)},
1444 {"DarkOrange" , PALETTERGB (255,140, 0)},
1445 {"coral" , PALETTERGB (255,127, 80)},
1446 {"light coral" , PALETTERGB (240,128,128)},
1447 {"LightCoral" , PALETTERGB (240,128,128)},
1448 {"tomato" , PALETTERGB (255, 99, 71)},
1449 {"orange red" , PALETTERGB (255, 69, 0)},
1450 {"OrangeRed" , PALETTERGB (255, 69, 0)},
1451 {"red" , PALETTERGB (255, 0, 0)},
1452 {"hot pink" , PALETTERGB (255,105,180)},
1453 {"HotPink" , PALETTERGB (255,105,180)},
1454 {"deep pink" , PALETTERGB (255, 20,147)},
1455 {"DeepPink" , PALETTERGB (255, 20,147)},
1456 {"pink" , PALETTERGB (255,192,203)},
1457 {"light pink" , PALETTERGB (255,182,193)},
1458 {"LightPink" , PALETTERGB (255,182,193)},
1459 {"pale violet red" , PALETTERGB (219,112,147)},
1460 {"PaleVioletRed" , PALETTERGB (219,112,147)},
1461 {"maroon" , PALETTERGB (176, 48, 96)},
1462 {"medium violet red" , PALETTERGB (199, 21,133)},
1463 {"MediumVioletRed" , PALETTERGB (199, 21,133)},
1464 {"violet red" , PALETTERGB (208, 32,144)},
1465 {"VioletRed" , PALETTERGB (208, 32,144)},
1466 {"magenta" , PALETTERGB (255, 0,255)},
1467 {"violet" , PALETTERGB (238,130,238)},
1468 {"plum" , PALETTERGB (221,160,221)},
1469 {"orchid" , PALETTERGB (218,112,214)},
1470 {"medium orchid" , PALETTERGB (186, 85,211)},
1471 {"MediumOrchid" , PALETTERGB (186, 85,211)},
1472 {"dark orchid" , PALETTERGB (153, 50,204)},
1473 {"DarkOrchid" , PALETTERGB (153, 50,204)},
1474 {"dark violet" , PALETTERGB (148, 0,211)},
1475 {"DarkViolet" , PALETTERGB (148, 0,211)},
1476 {"blue violet" , PALETTERGB (138, 43,226)},
1477 {"BlueViolet" , PALETTERGB (138, 43,226)},
1478 {"purple" , PALETTERGB (160, 32,240)},
1479 {"medium purple" , PALETTERGB (147,112,219)},
1480 {"MediumPurple" , PALETTERGB (147,112,219)},
1481 {"thistle" , PALETTERGB (216,191,216)},
1482 {"gray0" , PALETTERGB ( 0, 0, 0)},
1483 {"grey0" , PALETTERGB ( 0, 0, 0)},
1484 {"dark grey" , PALETTERGB (169,169,169)},
1485 {"DarkGrey" , PALETTERGB (169,169,169)},
1486 {"dark gray" , PALETTERGB (169,169,169)},
1487 {"DarkGray" , PALETTERGB (169,169,169)},
1488 {"dark blue" , PALETTERGB ( 0, 0,139)},
1489 {"DarkBlue" , PALETTERGB ( 0, 0,139)},
1490 {"dark cyan" , PALETTERGB ( 0,139,139)},
1491 {"DarkCyan" , PALETTERGB ( 0,139,139)},
1492 {"dark magenta" , PALETTERGB (139, 0,139)},
1493 {"DarkMagenta" , PALETTERGB (139, 0,139)},
1494 {"dark red" , PALETTERGB (139, 0, 0)},
1495 {"DarkRed" , PALETTERGB (139, 0, 0)},
1496 {"light green" , PALETTERGB (144,238,144)},
1497 {"LightGreen" , PALETTERGB (144,238,144)},
ee78dc32
GV
1498};
1499
fbd6baed 1500DEFUN ("w32-default-color-map", Fw32_default_color_map, Sw32_default_color_map,
74e1aeec 1501 0, 0, 0, doc: /* Return the default color map. */)
ee78dc32
GV
1502 ()
1503{
1504 int i;
fbd6baed 1505 colormap_t *pc = w32_color_map;
ee78dc32 1506 Lisp_Object cmap;
7d0393cf 1507
ee78dc32 1508 BLOCK_INPUT;
7d0393cf 1509
ee78dc32 1510 cmap = Qnil;
7d0393cf
JB
1511
1512 for (i = 0; i < sizeof (w32_color_map) / sizeof (w32_color_map[0]);
ee78dc32
GV
1513 pc++, i++)
1514 cmap = Fcons (Fcons (build_string (pc->name),
1515 make_number (pc->colorref)),
1516 cmap);
7d0393cf 1517
ee78dc32 1518 UNBLOCK_INPUT;
7d0393cf 1519
ee78dc32
GV
1520 return (cmap);
1521}
ee78dc32 1522
7d0393cf 1523Lisp_Object
fbd6baed 1524w32_to_x_color (rgb)
ee78dc32
GV
1525 Lisp_Object rgb;
1526{
1527 Lisp_Object color;
7d0393cf 1528
b7826503 1529 CHECK_NUMBER (rgb);
7d0393cf 1530
ee78dc32 1531 BLOCK_INPUT;
7d0393cf 1532
fbd6baed 1533 color = Frassq (rgb, Vw32_color_map);
7d0393cf 1534
ee78dc32 1535 UNBLOCK_INPUT;
7d0393cf 1536
ee78dc32
GV
1537 if (!NILP (color))
1538 return (Fcar (color));
1539 else
1540 return Qnil;
1541}
1542
5d7fed93
GV
1543COLORREF
1544w32_color_map_lookup (colorname)
1545 char *colorname;
1546{
1547 Lisp_Object tail, ret = Qnil;
1548
1549 BLOCK_INPUT;
1550
1551 for (tail = Vw32_color_map; !NILP (tail); tail = Fcdr (tail))
1552 {
1553 register Lisp_Object elt, tem;
1554
1555 elt = Fcar (tail);
1556 if (!CONSP (elt)) continue;
1557
1558 tem = Fcar (elt);
1559
d5db4077 1560 if (lstrcmpi (SDATA (tem), colorname) == 0)
5d7fed93
GV
1561 {
1562 ret = XUINT (Fcdr (elt));
1563 break;
1564 }
1565
1566 QUIT;
1567 }
1568
1569
1570 UNBLOCK_INPUT;
1571
1572 return ret;
1573}
1574
7d0393cf 1575COLORREF
fbd6baed 1576x_to_w32_color (colorname)
ee78dc32
GV
1577 char * colorname;
1578{
8edb0a6f
JR
1579 register Lisp_Object ret = Qnil;
1580
ee78dc32 1581 BLOCK_INPUT;
1edf84e7
GV
1582
1583 if (colorname[0] == '#')
1584 {
1585 /* Could be an old-style RGB Device specification. */
1586 char *color;
1587 int size;
1588 color = colorname + 1;
7d0393cf 1589
1edf84e7
GV
1590 size = strlen(color);
1591 if (size == 3 || size == 6 || size == 9 || size == 12)
1592 {
1593 UINT colorval;
1594 int i, pos;
1595 pos = 0;
1596 size /= 3;
1597 colorval = 0;
7d0393cf 1598
1edf84e7
GV
1599 for (i = 0; i < 3; i++)
1600 {
1601 char *end;
1602 char t;
1603 unsigned long value;
1604
1605 /* The check for 'x' in the following conditional takes into
1606 account the fact that strtol allows a "0x" in front of
1607 our numbers, and we don't. */
1608 if (!isxdigit(color[0]) || color[1] == 'x')
1609 break;
1610 t = color[size];
1611 color[size] = '\0';
1612 value = strtoul(color, &end, 16);
1613 color[size] = t;
1614 if (errno == ERANGE || end - color != size)
1615 break;
1616 switch (size)
1617 {
1618 case 1:
1619 value = value * 0x10;
1620 break;
1621 case 2:
1622 break;
1623 case 3:
1624 value /= 0x10;
1625 break;
1626 case 4:
1627 value /= 0x100;
1628 break;
1629 }
1630 colorval |= (value << pos);
1631 pos += 0x8;
1632 if (i == 2)
1633 {
1634 UNBLOCK_INPUT;
1635 return (colorval);
1636 }
1637 color = end;
1638 }
1639 }
1640 }
1641 else if (strnicmp(colorname, "rgb:", 4) == 0)
1642 {
1643 char *color;
1644 UINT colorval;
1645 int i, pos;
1646 pos = 0;
1647
1648 colorval = 0;
1649 color = colorname + 4;
1650 for (i = 0; i < 3; i++)
1651 {
1652 char *end;
1653 unsigned long value;
7d0393cf 1654
1edf84e7
GV
1655 /* The check for 'x' in the following conditional takes into
1656 account the fact that strtol allows a "0x" in front of
1657 our numbers, and we don't. */
1658 if (!isxdigit(color[0]) || color[1] == 'x')
1659 break;
1660 value = strtoul(color, &end, 16);
1661 if (errno == ERANGE)
1662 break;
1663 switch (end - color)
1664 {
1665 case 1:
1666 value = value * 0x10 + value;
1667 break;
1668 case 2:
1669 break;
1670 case 3:
1671 value /= 0x10;
1672 break;
1673 case 4:
1674 value /= 0x100;
1675 break;
1676 default:
1677 value = ULONG_MAX;
1678 }
1679 if (value == ULONG_MAX)
1680 break;
1681 colorval |= (value << pos);
1682 pos += 0x8;
1683 if (i == 2)
1684 {
1685 if (*end != '\0')
1686 break;
1687 UNBLOCK_INPUT;
1688 return (colorval);
1689 }
1690 if (*end != '/')
1691 break;
1692 color = end + 1;
1693 }
1694 }
1695 else if (strnicmp(colorname, "rgbi:", 5) == 0)
1696 {
1697 /* This is an RGB Intensity specification. */
1698 char *color;
1699 UINT colorval;
1700 int i, pos;
1701 pos = 0;
1702
1703 colorval = 0;
1704 color = colorname + 5;
1705 for (i = 0; i < 3; i++)
1706 {
1707 char *end;
1708 double value;
1709 UINT val;
1710
1711 value = strtod(color, &end);
1712 if (errno == ERANGE)
1713 break;
1714 if (value < 0.0 || value > 1.0)
1715 break;
1716 val = (UINT)(0x100 * value);
7d0393cf 1717 /* We used 0x100 instead of 0xFF to give a continuous
1edf84e7
GV
1718 range between 0.0 and 1.0 inclusive. The next statement
1719 fixes the 1.0 case. */
1720 if (val == 0x100)
1721 val = 0xFF;
1722 colorval |= (val << pos);
1723 pos += 0x8;
1724 if (i == 2)
1725 {
1726 if (*end != '\0')
1727 break;
1728 UNBLOCK_INPUT;
1729 return (colorval);
1730 }
1731 if (*end != '/')
1732 break;
1733 color = end + 1;
1734 }
1735 }
1736 /* I am not going to attempt to handle any of the CIE color schemes
1737 or TekHVC, since I don't know the algorithms for conversion to
1738 RGB. */
f695b4b1
GV
1739
1740 /* If we fail to lookup the color name in w32_color_map, then check the
7d0393cf 1741 colorname to see if it can be crudely approximated: If the X color
f695b4b1
GV
1742 ends in a number (e.g., "darkseagreen2"), strip the number and
1743 return the result of looking up the base color name. */
1744 ret = w32_color_map_lookup (colorname);
7d0393cf 1745 if (NILP (ret))
ee78dc32 1746 {
f695b4b1 1747 int len = strlen (colorname);
ee78dc32 1748
7d0393cf 1749 if (isdigit (colorname[len - 1]))
f695b4b1 1750 {
8b77111c 1751 char *ptr, *approx = alloca (len + 1);
ee78dc32 1752
f695b4b1
GV
1753 strcpy (approx, colorname);
1754 ptr = &approx[len - 1];
7d0393cf 1755 while (ptr > approx && isdigit (*ptr))
f695b4b1 1756 *ptr-- = '\0';
ee78dc32 1757
f695b4b1 1758 ret = w32_color_map_lookup (approx);
ee78dc32 1759 }
ee78dc32 1760 }
7d0393cf 1761
ee78dc32 1762 UNBLOCK_INPUT;
ee78dc32
GV
1763 return ret;
1764}
1765
5ac45f98
GV
1766
1767void
fbd6baed 1768w32_regenerate_palette (FRAME_PTR f)
5ac45f98 1769{
fbd6baed 1770 struct w32_palette_entry * list;
5ac45f98
GV
1771 LOGPALETTE * log_palette;
1772 HPALETTE new_palette;
1773 int i;
1774
1775 /* don't bother trying to create palette if not supported */
fbd6baed 1776 if (! FRAME_W32_DISPLAY_INFO (f)->has_palette)
5ac45f98
GV
1777 return;
1778
1779 log_palette = (LOGPALETTE *)
1780 alloca (sizeof (LOGPALETTE) +
fbd6baed 1781 FRAME_W32_DISPLAY_INFO (f)->num_colors * sizeof (PALETTEENTRY));
5ac45f98 1782 log_palette->palVersion = 0x300;
fbd6baed 1783 log_palette->palNumEntries = FRAME_W32_DISPLAY_INFO (f)->num_colors;
5ac45f98 1784
fbd6baed 1785 list = FRAME_W32_DISPLAY_INFO (f)->color_list;
5ac45f98 1786 for (i = 0;
fbd6baed 1787 i < FRAME_W32_DISPLAY_INFO (f)->num_colors;
5ac45f98
GV
1788 i++, list = list->next)
1789 log_palette->palPalEntry[i] = list->entry;
1790
1791 new_palette = CreatePalette (log_palette);
1792
1793 enter_crit ();
1794
fbd6baed
GV
1795 if (FRAME_W32_DISPLAY_INFO (f)->palette)
1796 DeleteObject (FRAME_W32_DISPLAY_INFO (f)->palette);
1797 FRAME_W32_DISPLAY_INFO (f)->palette = new_palette;
5ac45f98
GV
1798
1799 /* Realize display palette and garbage all frames. */
1800 release_frame_dc (f, get_frame_dc (f));
1801
1802 leave_crit ();
1803}
1804
fbd6baed
GV
1805#define W32_COLOR(pe) RGB (pe.peRed, pe.peGreen, pe.peBlue)
1806#define SET_W32_COLOR(pe, color) \
5ac45f98
GV
1807 do \
1808 { \
1809 pe.peRed = GetRValue (color); \
1810 pe.peGreen = GetGValue (color); \
1811 pe.peBlue = GetBValue (color); \
1812 pe.peFlags = 0; \
1813 } while (0)
1814
1815#if 0
1816/* Keep these around in case we ever want to track color usage. */
1817void
fbd6baed 1818w32_map_color (FRAME_PTR f, COLORREF color)
5ac45f98 1819{
fbd6baed 1820 struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
5ac45f98 1821
fbd6baed 1822 if (NILP (Vw32_enable_palette))
5ac45f98
GV
1823 return;
1824
1825 /* check if color is already mapped */
1826 while (list)
1827 {
fbd6baed 1828 if (W32_COLOR (list->entry) == color)
5ac45f98
GV
1829 {
1830 ++list->refcount;
1831 return;
1832 }
1833 list = list->next;
1834 }
1835
1836 /* not already mapped, so add to list and recreate Windows palette */
fbd6baed
GV
1837 list = (struct w32_palette_entry *)
1838 xmalloc (sizeof (struct w32_palette_entry));
1839 SET_W32_COLOR (list->entry, color);
5ac45f98 1840 list->refcount = 1;
fbd6baed
GV
1841 list->next = FRAME_W32_DISPLAY_INFO (f)->color_list;
1842 FRAME_W32_DISPLAY_INFO (f)->color_list = list;
1843 FRAME_W32_DISPLAY_INFO (f)->num_colors++;
5ac45f98
GV
1844
1845 /* set flag that palette must be regenerated */
fbd6baed 1846 FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
5ac45f98
GV
1847}
1848
1849void
fbd6baed 1850w32_unmap_color (FRAME_PTR f, COLORREF color)
5ac45f98 1851{
fbd6baed
GV
1852 struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
1853 struct w32_palette_entry **prev = &FRAME_W32_DISPLAY_INFO (f)->color_list;
5ac45f98 1854
fbd6baed 1855 if (NILP (Vw32_enable_palette))
5ac45f98
GV
1856 return;
1857
1858 /* check if color is already mapped */
1859 while (list)
1860 {
fbd6baed 1861 if (W32_COLOR (list->entry) == color)
5ac45f98
GV
1862 {
1863 if (--list->refcount == 0)
1864 {
1865 *prev = list->next;
1866 xfree (list);
fbd6baed 1867 FRAME_W32_DISPLAY_INFO (f)->num_colors--;
5ac45f98
GV
1868 break;
1869 }
1870 else
1871 return;
1872 }
1873 prev = &list->next;
1874 list = list->next;
1875 }
1876
1877 /* set flag that palette must be regenerated */
fbd6baed 1878 FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
5ac45f98
GV
1879}
1880#endif
1881
6fc2811b
JR
1882
1883/* Gamma-correct COLOR on frame F. */
1884
1885void
1886gamma_correct (f, color)
1887 struct frame *f;
1888 COLORREF *color;
1889{
1890 if (f->gamma)
1891 {
1892 *color = PALETTERGB (
1893 pow (GetRValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
1894 pow (GetGValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
1895 pow (GetBValue (*color) / 255.0, f->gamma) * 255.0 + 0.5);
1896 }
1897}
1898
1899
ee78dc32
GV
1900/* Decide if color named COLOR is valid for the display associated with
1901 the selected frame; if so, return the rgb values in COLOR_DEF.
1902 If ALLOC is nonzero, allocate a new colormap cell. */
1903
1904int
6fc2811b 1905w32_defined_color (f, color, color_def, alloc)
ee78dc32
GV
1906 FRAME_PTR f;
1907 char *color;
6fc2811b 1908 XColor *color_def;
ee78dc32
GV
1909 int alloc;
1910{
1911 register Lisp_Object tem;
6fc2811b 1912 COLORREF w32_color_ref;
3c190163 1913
fbd6baed 1914 tem = x_to_w32_color (color);
3c190163 1915
7d0393cf 1916 if (!NILP (tem))
ee78dc32 1917 {
d88c567c
JR
1918 if (f)
1919 {
1920 /* Apply gamma correction. */
1921 w32_color_ref = XUINT (tem);
1922 gamma_correct (f, &w32_color_ref);
1923 XSETINT (tem, w32_color_ref);
1924 }
9badad41
JR
1925
1926 /* Map this color to the palette if it is enabled. */
fbd6baed 1927 if (!NILP (Vw32_enable_palette))
5ac45f98 1928 {
fbd6baed 1929 struct w32_palette_entry * entry =
d88c567c 1930 one_w32_display_info.color_list;
fbd6baed 1931 struct w32_palette_entry ** prev =
d88c567c 1932 &one_w32_display_info.color_list;
7d0393cf 1933
5ac45f98
GV
1934 /* check if color is already mapped */
1935 while (entry)
1936 {
fbd6baed 1937 if (W32_COLOR (entry->entry) == XUINT (tem))
5ac45f98
GV
1938 break;
1939 prev = &entry->next;
1940 entry = entry->next;
1941 }
1942
1943 if (entry == NULL && alloc)
1944 {
1945 /* not already mapped, so add to list */
fbd6baed
GV
1946 entry = (struct w32_palette_entry *)
1947 xmalloc (sizeof (struct w32_palette_entry));
1948 SET_W32_COLOR (entry->entry, XUINT (tem));
5ac45f98
GV
1949 entry->next = NULL;
1950 *prev = entry;
d88c567c 1951 one_w32_display_info.num_colors++;
5ac45f98
GV
1952
1953 /* set flag that palette must be regenerated */
d88c567c 1954 one_w32_display_info.regen_palette = TRUE;
5ac45f98
GV
1955 }
1956 }
1957 /* Ensure COLORREF value is snapped to nearest color in (default)
1958 palette by simulating the PALETTERGB macro. This works whether
1959 or not the display device has a palette. */
6fc2811b
JR
1960 w32_color_ref = XUINT (tem) | 0x2000000;
1961
6fc2811b 1962 color_def->pixel = w32_color_ref;
197edd35
JR
1963 color_def->red = GetRValue (w32_color_ref) * 256;
1964 color_def->green = GetGValue (w32_color_ref) * 256;
1965 color_def->blue = GetBValue (w32_color_ref) * 256;
6fc2811b 1966
ee78dc32 1967 return 1;
5ac45f98 1968 }
7d0393cf 1969 else
3c190163
GV
1970 {
1971 return 0;
1972 }
ee78dc32
GV
1973}
1974
1975/* Given a string ARG naming a color, compute a pixel value from it
1976 suitable for screen F.
1977 If F is not a color screen, return DEF (default) regardless of what
1978 ARG says. */
1979
1980int
1981x_decode_color (f, arg, def)
1982 FRAME_PTR f;
1983 Lisp_Object arg;
1984 int def;
1985{
6fc2811b 1986 XColor cdef;
ee78dc32 1987
b7826503 1988 CHECK_STRING (arg);
ee78dc32 1989
d5db4077 1990 if (strcmp (SDATA (arg), "black") == 0)
ee78dc32 1991 return BLACK_PIX_DEFAULT (f);
d5db4077 1992 else if (strcmp (SDATA (arg), "white") == 0)
ee78dc32
GV
1993 return WHITE_PIX_DEFAULT (f);
1994
fbd6baed 1995 if ((FRAME_W32_DISPLAY_INFO (f)->n_planes * FRAME_W32_DISPLAY_INFO (f)->n_cbits) == 1)
ee78dc32
GV
1996 return def;
1997
6fc2811b 1998 /* w32_defined_color is responsible for coping with failures
ee78dc32 1999 by looking for a near-miss. */
d5db4077 2000 if (w32_defined_color (f, SDATA (arg), &cdef, 1))
6fc2811b 2001 return cdef.pixel;
ee78dc32
GV
2002
2003 /* defined_color failed; return an ultimate default. */
2004 return def;
2005}
2006\f
dfff8a69
JR
2007/* Change the `line-spacing' frame parameter of frame F. OLD_VALUE is
2008 the previous value of that parameter, NEW_VALUE is the new value. */
2009
2010static void
2011x_set_line_spacing (f, new_value, old_value)
2012 struct frame *f;
2013 Lisp_Object new_value, old_value;
2014{
2015 if (NILP (new_value))
2016 f->extra_line_spacing = 0;
2017 else if (NATNUMP (new_value))
2018 f->extra_line_spacing = XFASTINT (new_value);
2019 else
1a948b17 2020 Fsignal (Qerror, Fcons (build_string ("Invalid line-spacing"),
dfff8a69
JR
2021 Fcons (new_value, Qnil)));
2022 if (FRAME_VISIBLE_P (f))
2023 redraw_frame (f);
2024}
2025
2026
f7b9d4d1
JR
2027/* Change the `fullscreen' frame parameter of frame F. OLD_VALUE is
2028 the previous value of that parameter, NEW_VALUE is the new value. */
2029
2030static void
2031x_set_fullscreen (f, new_value, old_value)
2032 struct frame *f;
2033 Lisp_Object new_value, old_value;
2034{
2035 if (NILP (new_value))
2036 f->output_data.w32->want_fullscreen = FULLSCREEN_NONE;
2037 else if (EQ (new_value, Qfullboth))
2038 f->output_data.w32->want_fullscreen = FULLSCREEN_BOTH;
2039 else if (EQ (new_value, Qfullwidth))
2040 f->output_data.w32->want_fullscreen = FULLSCREEN_WIDTH;
2041 else if (EQ (new_value, Qfullheight))
2042 f->output_data.w32->want_fullscreen = FULLSCREEN_HEIGHT;
2043}
2044
2045
6fc2811b
JR
2046/* Change the `screen-gamma' frame parameter of frame F. OLD_VALUE is
2047 the previous value of that parameter, NEW_VALUE is the new value. */
2048
2049static void
2050x_set_screen_gamma (f, new_value, old_value)
2051 struct frame *f;
2052 Lisp_Object new_value, old_value;
2053{
2054 if (NILP (new_value))
2055 f->gamma = 0;
2056 else if (NUMBERP (new_value) && XFLOATINT (new_value) > 0)
2057 /* The value 0.4545 is the normal viewing gamma. */
2058 f->gamma = 1.0 / (0.4545 * XFLOATINT (new_value));
2059 else
1a948b17 2060 Fsignal (Qerror, Fcons (build_string ("Invalid screen-gamma"),
6fc2811b
JR
2061 Fcons (new_value, Qnil)));
2062
2063 clear_face_cache (0);
2064}
2065
2066
ee78dc32
GV
2067/* Functions called only from `x_set_frame_param'
2068 to set individual parameters.
2069
fbd6baed 2070 If FRAME_W32_WINDOW (f) is 0,
ee78dc32
GV
2071 the frame is being created and its window does not exist yet.
2072 In that case, just record the parameter's new value
2073 in the standard place; do not attempt to change the window. */
2074
2075void
2076x_set_foreground_color (f, arg, oldval)
2077 struct frame *f;
2078 Lisp_Object arg, oldval;
2079{
3cf3436e
JR
2080 struct w32_output *x = f->output_data.w32;
2081 PIX_TYPE fg, old_fg;
2082
2083 fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
2084 old_fg = FRAME_FOREGROUND_PIXEL (f);
2085 FRAME_FOREGROUND_PIXEL (f) = fg;
5ac45f98 2086
fbd6baed 2087 if (FRAME_W32_WINDOW (f) != 0)
ee78dc32 2088 {
3cf3436e
JR
2089 if (x->cursor_pixel == old_fg)
2090 x->cursor_pixel = fg;
2091
6fc2811b 2092 update_face_from_frame_parameter (f, Qforeground_color, arg);
ee78dc32
GV
2093 if (FRAME_VISIBLE_P (f))
2094 redraw_frame (f);
2095 }
2096}
2097
2098void
2099x_set_background_color (f, arg, oldval)
2100 struct frame *f;
2101 Lisp_Object arg, oldval;
2102{
6fc2811b 2103 FRAME_BACKGROUND_PIXEL (f)
ee78dc32
GV
2104 = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
2105
fbd6baed 2106 if (FRAME_W32_WINDOW (f) != 0)
ee78dc32 2107 {
6fc2811b
JR
2108 SetWindowLong (FRAME_W32_WINDOW (f), WND_BACKGROUND_INDEX,
2109 FRAME_BACKGROUND_PIXEL (f));
ee78dc32 2110
6fc2811b 2111 update_face_from_frame_parameter (f, Qbackground_color, arg);
ee78dc32
GV
2112
2113 if (FRAME_VISIBLE_P (f))
2114 redraw_frame (f);
2115 }
2116}
2117
2118void
2119x_set_mouse_color (f, arg, oldval)
2120 struct frame *f;
2121 Lisp_Object arg, oldval;
2122{
ee78dc32 2123 Cursor cursor, nontext_cursor, mode_cursor, cross_cursor;
dfc465d3 2124 int count;
ee78dc32
GV
2125 int mask_color;
2126
2127 if (!EQ (Qnil, arg))
fbd6baed 2128 f->output_data.w32->mouse_pixel
ee78dc32 2129 = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
6fc2811b
JR
2130 mask_color = FRAME_BACKGROUND_PIXEL (f);
2131
2132 /* Don't let pointers be invisible. */
fbd6baed 2133 if (mask_color == f->output_data.w32->mouse_pixel
6fc2811b
JR
2134 && mask_color == FRAME_BACKGROUND_PIXEL (f))
2135 f->output_data.w32->mouse_pixel = FRAME_FOREGROUND_PIXEL (f);
ee78dc32 2136
767b1ff0 2137#if 0 /* TODO : cursor changes */
ee78dc32
GV
2138 BLOCK_INPUT;
2139
2140 /* It's not okay to crash if the user selects a screwy cursor. */
fadca6c6 2141 count = x_catch_errors (FRAME_W32_DISPLAY (f));
ee78dc32
GV
2142
2143 if (!EQ (Qnil, Vx_pointer_shape))
2144 {
b7826503 2145 CHECK_NUMBER (Vx_pointer_shape);
fbd6baed 2146 cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XINT (Vx_pointer_shape));
ee78dc32
GV
2147 }
2148 else
fbd6baed
GV
2149 cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
2150 x_check_errors (FRAME_W32_DISPLAY (f), "bad text pointer cursor: %s");
ee78dc32
GV
2151
2152 if (!EQ (Qnil, Vx_nontext_pointer_shape))
2153 {
b7826503 2154 CHECK_NUMBER (Vx_nontext_pointer_shape);
fbd6baed 2155 nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
ee78dc32
GV
2156 XINT (Vx_nontext_pointer_shape));
2157 }
2158 else
fbd6baed
GV
2159 nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_left_ptr);
2160 x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
ee78dc32 2161
0af913d7 2162 if (!EQ (Qnil, Vx_hourglass_pointer_shape))
6fc2811b 2163 {
b7826503 2164 CHECK_NUMBER (Vx_hourglass_pointer_shape);
0af913d7
GM
2165 hourglass_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
2166 XINT (Vx_hourglass_pointer_shape));
6fc2811b
JR
2167 }
2168 else
0af913d7 2169 hourglass_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_watch);
6fc2811b 2170 x_check_errors (FRAME_W32_DISPLAY (f), "bad busy pointer cursor: %s");
7d0393cf 2171
6fc2811b 2172 x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
ee78dc32
GV
2173 if (!EQ (Qnil, Vx_mode_pointer_shape))
2174 {
b7826503 2175 CHECK_NUMBER (Vx_mode_pointer_shape);
fbd6baed 2176 mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
ee78dc32
GV
2177 XINT (Vx_mode_pointer_shape));
2178 }
2179 else
fbd6baed
GV
2180 mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
2181 x_check_errors (FRAME_W32_DISPLAY (f), "bad modeline pointer cursor: %s");
ee78dc32
GV
2182
2183 if (!EQ (Qnil, Vx_sensitive_text_pointer_shape))
2184 {
b7826503 2185 CHECK_NUMBER (Vx_sensitive_text_pointer_shape);
ee78dc32 2186 cross_cursor
fbd6baed 2187 = XCreateFontCursor (FRAME_W32_DISPLAY (f),
ee78dc32
GV
2188 XINT (Vx_sensitive_text_pointer_shape));
2189 }
2190 else
fbd6baed 2191 cross_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_crosshair);
ee78dc32 2192
4694d762
JR
2193 if (!NILP (Vx_window_horizontal_drag_shape))
2194 {
b7826503 2195 CHECK_NUMBER (Vx_window_horizontal_drag_shape);
4694d762
JR
2196 horizontal_drag_cursor
2197 = XCreateFontCursor (FRAME_X_DISPLAY (f),
2198 XINT (Vx_window_horizontal_drag_shape));
2199 }
2200 else
2201 horizontal_drag_cursor
2202 = XCreateFontCursor (FRAME_X_DISPLAY (f), XC_sb_h_double_arrow);
c9b2104d 2203 /* TODO: hand_cursor */
4694d762 2204
ee78dc32 2205 /* Check and report errors with the above calls. */
fbd6baed 2206 x_check_errors (FRAME_W32_DISPLAY (f), "can't set cursor shape: %s");
fadca6c6 2207 x_uncatch_errors (FRAME_W32_DISPLAY (f), count);
ee78dc32
GV
2208
2209 {
2210 XColor fore_color, back_color;
2211
fbd6baed 2212 fore_color.pixel = f->output_data.w32->mouse_pixel;
ee78dc32 2213 back_color.pixel = mask_color;
fbd6baed
GV
2214 XQueryColor (FRAME_W32_DISPLAY (f),
2215 DefaultColormap (FRAME_W32_DISPLAY (f),
2216 DefaultScreen (FRAME_W32_DISPLAY (f))),
ee78dc32 2217 &fore_color);
fbd6baed
GV
2218 XQueryColor (FRAME_W32_DISPLAY (f),
2219 DefaultColormap (FRAME_W32_DISPLAY (f),
2220 DefaultScreen (FRAME_W32_DISPLAY (f))),
ee78dc32 2221 &back_color);
fbd6baed 2222 XRecolorCursor (FRAME_W32_DISPLAY (f), cursor,
ee78dc32 2223 &fore_color, &back_color);
fbd6baed 2224 XRecolorCursor (FRAME_W32_DISPLAY (f), nontext_cursor,
ee78dc32 2225 &fore_color, &back_color);
fbd6baed 2226 XRecolorCursor (FRAME_W32_DISPLAY (f), mode_cursor,
ee78dc32 2227 &fore_color, &back_color);
fbd6baed 2228 XRecolorCursor (FRAME_W32_DISPLAY (f), cross_cursor,
ee78dc32 2229 &fore_color, &back_color);
0af913d7 2230 XRecolorCursor (FRAME_W32_DISPLAY (f), hourglass_cursor,
6fc2811b 2231 &fore_color, &back_color);
c9b2104d 2232 /* TODO: hand_cursor */
ee78dc32
GV
2233 }
2234
fbd6baed 2235 if (FRAME_W32_WINDOW (f) != 0)
6fc2811b 2236 XDefineCursor (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), cursor);
ee78dc32 2237
fbd6baed
GV
2238 if (cursor != f->output_data.w32->text_cursor && f->output_data.w32->text_cursor != 0)
2239 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->text_cursor);
2240 f->output_data.w32->text_cursor = cursor;
2241
2242 if (nontext_cursor != f->output_data.w32->nontext_cursor
2243 && f->output_data.w32->nontext_cursor != 0)
2244 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->nontext_cursor);
2245 f->output_data.w32->nontext_cursor = nontext_cursor;
2246
0af913d7
GM
2247 if (hourglass_cursor != f->output_data.w32->hourglass_cursor
2248 && f->output_data.w32->hourglass_cursor != 0)
2249 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->hourglass_cursor);
2250 f->output_data.w32->hourglass_cursor = hourglass_cursor;
6fc2811b 2251
fbd6baed
GV
2252 if (mode_cursor != f->output_data.w32->modeline_cursor
2253 && f->output_data.w32->modeline_cursor != 0)
2254 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->modeline_cursor);
2255 f->output_data.w32->modeline_cursor = mode_cursor;
7d0393cf 2256
fbd6baed
GV
2257 if (cross_cursor != f->output_data.w32->cross_cursor
2258 && f->output_data.w32->cross_cursor != 0)
2259 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->cross_cursor);
2260 f->output_data.w32->cross_cursor = cross_cursor;
c9b2104d 2261 /* TODO: hand_cursor */
fbd6baed
GV
2262
2263 XFlush (FRAME_W32_DISPLAY (f));
ee78dc32 2264 UNBLOCK_INPUT;
6fc2811b
JR
2265
2266 update_face_from_frame_parameter (f, Qmouse_color, arg);
767b1ff0 2267#endif /* TODO */
ee78dc32
GV
2268}
2269
70a0239a
JR
2270/* Defined in w32term.c. */
2271void x_update_cursor (struct frame *f, int on_p);
2272
ee78dc32
GV
2273void
2274x_set_cursor_color (f, arg, oldval)
2275 struct frame *f;
2276 Lisp_Object arg, oldval;
2277{
70a0239a 2278 unsigned long fore_pixel, pixel;
ee78dc32 2279
dfff8a69 2280 if (!NILP (Vx_cursor_fore_pixel))
ee78dc32 2281 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
70a0239a 2282 WHITE_PIX_DEFAULT (f));
ee78dc32 2283 else
6fc2811b 2284 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
70a0239a 2285
6759f872 2286 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
7d0393cf 2287
ee78dc32 2288 /* Make sure that the cursor color differs from the background color. */
70a0239a 2289 if (pixel == FRAME_BACKGROUND_PIXEL (f))
ee78dc32 2290 {
70a0239a
JR
2291 pixel = f->output_data.w32->mouse_pixel;
2292 if (pixel == fore_pixel)
6fc2811b 2293 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
ee78dc32 2294 }
70a0239a 2295
ac849ba4 2296 f->output_data.w32->cursor_foreground_pixel = fore_pixel;
70a0239a 2297 f->output_data.w32->cursor_pixel = pixel;
ee78dc32 2298
fbd6baed 2299 if (FRAME_W32_WINDOW (f) != 0)
ee78dc32 2300 {
0327b4cc
JR
2301 BLOCK_INPUT;
2302 /* Update frame's cursor_gc. */
2303 f->output_data.w32->cursor_gc->foreground = fore_pixel;
2304 f->output_data.w32->cursor_gc->background = pixel;
2305
2306 UNBLOCK_INPUT;
2307
ee78dc32
GV
2308 if (FRAME_VISIBLE_P (f))
2309 {
70a0239a
JR
2310 x_update_cursor (f, 0);
2311 x_update_cursor (f, 1);
ee78dc32
GV
2312 }
2313 }
6fc2811b
JR
2314
2315 update_face_from_frame_parameter (f, Qcursor_color, arg);
ee78dc32
GV
2316}
2317
33d52f9c
GV
2318/* Set the border-color of frame F to pixel value PIX.
2319 Note that this does not fully take effect if done before
7d0393cf 2320 F has a window. */
33d52f9c
GV
2321void
2322x_set_border_pixel (f, pix)
2323 struct frame *f;
2324 int pix;
2325{
2326 f->output_data.w32->border_pixel = pix;
2327
2328 if (FRAME_W32_WINDOW (f) != 0 && f->output_data.w32->border_width > 0)
2329 {
2330 if (FRAME_VISIBLE_P (f))
2331 redraw_frame (f);
2332 }
2333}
2334
ee78dc32
GV
2335/* Set the border-color of frame F to value described by ARG.
2336 ARG can be a string naming a color.
2337 The border-color is used for the border that is drawn by the server.
2338 Note that this does not fully take effect if done before
2339 F has a window; it must be redone when the window is created. */
2340
2341void
2342x_set_border_color (f, arg, oldval)
2343 struct frame *f;
2344 Lisp_Object arg, oldval;
2345{
ee78dc32
GV
2346 int pix;
2347
b7826503 2348 CHECK_STRING (arg);
ee78dc32 2349 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
ee78dc32 2350 x_set_border_pixel (f, pix);
6fc2811b 2351 update_face_from_frame_parameter (f, Qborder_color, arg);
ee78dc32
GV
2352}
2353
dfff8a69
JR
2354
2355void
2356x_set_cursor_type (f, arg, oldval)
2357 FRAME_PTR f;
2358 Lisp_Object arg, oldval;
2359{
50e363e6 2360 set_frame_cursor_types (f, arg);
ee78dc32
GV
2361
2362 /* Make sure the cursor gets redrawn. This is overkill, but how
2363 often do people change cursor types? */
2364 update_mode_lines++;
2365}
dfff8a69 2366\f
ee78dc32
GV
2367void
2368x_set_icon_type (f, arg, oldval)
2369 struct frame *f;
2370 Lisp_Object arg, oldval;
2371{
ee78dc32
GV
2372 int result;
2373
eb7576ce
GV
2374 if (NILP (arg) && NILP (oldval))
2375 return;
2376
7d0393cf 2377 if (STRINGP (arg) && STRINGP (oldval)
eb7576ce
GV
2378 && EQ (Fstring_equal (oldval, arg), Qt))
2379 return;
2380
2381 if (SYMBOLP (arg) && SYMBOLP (oldval) && EQ (arg, oldval))
ee78dc32
GV
2382 return;
2383
2384 BLOCK_INPUT;
ee78dc32 2385
eb7576ce 2386 result = x_bitmap_icon (f, arg);
ee78dc32
GV
2387 if (result)
2388 {
2389 UNBLOCK_INPUT;
2390 error ("No icon window available");
2391 }
2392
ee78dc32 2393 UNBLOCK_INPUT;
ee78dc32
GV
2394}
2395
2396/* Return non-nil if frame F wants a bitmap icon. */
2397
2398Lisp_Object
2399x_icon_type (f)
2400 FRAME_PTR f;
2401{
2402 Lisp_Object tem;
2403
2404 tem = assq_no_quit (Qicon_type, f->param_alist);
2405 if (CONSP (tem))
8e713be6 2406 return XCDR (tem);
ee78dc32
GV
2407 else
2408 return Qnil;
2409}
2410
2411void
2412x_set_icon_name (f, arg, oldval)
2413 struct frame *f;
2414 Lisp_Object arg, oldval;
2415{
ee78dc32
GV
2416 if (STRINGP (arg))
2417 {
2418 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
2419 return;
2420 }
2421 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
2422 return;
2423
2424 f->icon_name = arg;
2425
2426#if 0
fbd6baed 2427 if (f->output_data.w32->icon_bitmap != 0)
ee78dc32
GV
2428 return;
2429
2430 BLOCK_INPUT;
2431
2432 result = x_text_icon (f,
d5db4077
KR
2433 (char *) SDATA ((!NILP (f->icon_name)
2434 ? f->icon_name
2435 : !NILP (f->title)
2436 ? f->title
2437 : f->name)));
ee78dc32
GV
2438
2439 if (result)
2440 {
2441 UNBLOCK_INPUT;
2442 error ("No icon window available");
2443 }
2444
2445 /* If the window was unmapped (and its icon was mapped),
2446 the new icon is not mapped, so map the window in its stead. */
2447 if (FRAME_VISIBLE_P (f))
2448 {
2449#ifdef USE_X_TOOLKIT
fbd6baed 2450 XtPopup (f->output_data.w32->widget, XtGrabNone);
ee78dc32 2451#endif
fbd6baed 2452 XMapWindow (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f));
ee78dc32
GV
2453 }
2454
fbd6baed 2455 XFlush (FRAME_W32_DISPLAY (f));
ee78dc32
GV
2456 UNBLOCK_INPUT;
2457#endif
2458}
2459
2460extern Lisp_Object x_new_font ();
4587b026 2461extern Lisp_Object x_new_fontset();
ee78dc32
GV
2462
2463void
2464x_set_font (f, arg, oldval)
2465 struct frame *f;
2466 Lisp_Object arg, oldval;
2467{
2468 Lisp_Object result;
4587b026 2469 Lisp_Object fontset_name;
4b817373 2470 Lisp_Object frame;
3cf3436e 2471 int old_fontset = FRAME_FONTSET(f);
ee78dc32 2472
b7826503 2473 CHECK_STRING (arg);
ee78dc32 2474
4587b026
GV
2475 fontset_name = Fquery_fontset (arg, Qnil);
2476
ee78dc32 2477 BLOCK_INPUT;
4587b026 2478 result = (STRINGP (fontset_name)
d5db4077
KR
2479 ? x_new_fontset (f, SDATA (fontset_name))
2480 : x_new_font (f, SDATA (arg)));
ee78dc32 2481 UNBLOCK_INPUT;
7d0393cf 2482
ee78dc32 2483 if (EQ (result, Qnil))
d5db4077 2484 error ("Font `%s' is not defined", SDATA (arg));
ee78dc32 2485 else if (EQ (result, Qt))
dfff8a69 2486 error ("The characters of the given font have varying widths");
ee78dc32
GV
2487 else if (STRINGP (result))
2488 {
3cf3436e
JR
2489 if (STRINGP (fontset_name))
2490 {
2491 /* Fontset names are built from ASCII font names, so the
2492 names may be equal despite there was a change. */
2493 if (old_fontset == FRAME_FONTSET (f))
2494 return;
2495 }
2496 else if (!NILP (Fequal (result, oldval)))
dc220243 2497 return;
3cf3436e 2498
ee78dc32 2499 store_frame_param (f, Qfont, result);
6fc2811b 2500 recompute_basic_faces (f);
ee78dc32
GV
2501 }
2502 else
2503 abort ();
4b817373 2504
6fc2811b
JR
2505 do_pending_window_change (0);
2506
2507 /* Don't call `face-set-after-frame-default' when faces haven't been
2508 initialized yet. This is the case when called from
2509 Fx_create_frame. In that case, the X widget or window doesn't
2510 exist either, and we can end up in x_report_frame_params with a
2511 null widget which gives a segfault. */
2512 if (FRAME_FACE_CACHE (f))
2513 {
2514 XSETFRAME (frame, f);
2515 call1 (Qface_set_after_frame_default, frame);
2516 }
ee78dc32
GV
2517}
2518
41c1bdd9
KS
2519static void
2520x_set_fringe_width (f, new_value, old_value)
2521 struct frame *f;
2522 Lisp_Object new_value, old_value;
2523{
2524 x_compute_fringe_widths (f, 1);
2525}
2526
ee78dc32
GV
2527void
2528x_set_border_width (f, arg, oldval)
2529 struct frame *f;
2530 Lisp_Object arg, oldval;
2531{
b7826503 2532 CHECK_NUMBER (arg);
ee78dc32 2533
fbd6baed 2534 if (XINT (arg) == f->output_data.w32->border_width)
ee78dc32
GV
2535 return;
2536
fbd6baed 2537 if (FRAME_W32_WINDOW (f) != 0)
ee78dc32
GV
2538 error ("Cannot change the border width of a window");
2539
fbd6baed 2540 f->output_data.w32->border_width = XINT (arg);
ee78dc32
GV
2541}
2542
2543void
2544x_set_internal_border_width (f, arg, oldval)
2545 struct frame *f;
2546 Lisp_Object arg, oldval;
2547{
fbd6baed 2548 int old = f->output_data.w32->internal_border_width;
ee78dc32 2549
b7826503 2550 CHECK_NUMBER (arg);
fbd6baed
GV
2551 f->output_data.w32->internal_border_width = XINT (arg);
2552 if (f->output_data.w32->internal_border_width < 0)
2553 f->output_data.w32->internal_border_width = 0;
ee78dc32 2554
fbd6baed 2555 if (f->output_data.w32->internal_border_width == old)
ee78dc32
GV
2556 return;
2557
fbd6baed 2558 if (FRAME_W32_WINDOW (f) != 0)
ee78dc32 2559 {
ee78dc32 2560 x_set_window_size (f, 0, f->width, f->height);
ee78dc32 2561 SET_FRAME_GARBAGED (f);
6fc2811b 2562 do_pending_window_change (0);
ee78dc32 2563 }
a05e2bae
JR
2564 else
2565 SET_FRAME_GARBAGED (f);
ee78dc32
GV
2566}
2567
2568void
2569x_set_visibility (f, value, oldval)
2570 struct frame *f;
2571 Lisp_Object value, oldval;
2572{
2573 Lisp_Object frame;
2574 XSETFRAME (frame, f);
2575
2576 if (NILP (value))
2577 Fmake_frame_invisible (frame, Qt);
2578 else if (EQ (value, Qicon))
2579 Ficonify_frame (frame);
2580 else
2581 Fmake_frame_visible (frame);
2582}
2583
a1258667
JR
2584\f
2585/* Change window heights in windows rooted in WINDOW by N lines. */
2586
2587static void
2588x_change_window_heights (window, n)
2589 Lisp_Object window;
2590 int n;
2591{
2592 struct window *w = XWINDOW (window);
2593
2594 XSETFASTINT (w->top, XFASTINT (w->top) + n);
2595 XSETFASTINT (w->height, XFASTINT (w->height) - n);
2596
2597 if (INTEGERP (w->orig_top))
2598 XSETFASTINT (w->orig_top, XFASTINT (w->orig_top) + n);
2599 if (INTEGERP (w->orig_height))
2600 XSETFASTINT (w->orig_height, XFASTINT (w->orig_height) - n);
2601
2602 /* Handle just the top child in a vertical split. */
2603 if (!NILP (w->vchild))
2604 x_change_window_heights (w->vchild, n);
2605
2606 /* Adjust all children in a horizontal split. */
2607 for (window = w->hchild; !NILP (window); window = w->next)
2608 {
2609 w = XWINDOW (window);
2610 x_change_window_heights (window, n);
2611 }
2612}
2613
ee78dc32
GV
2614void
2615x_set_menu_bar_lines (f, value, oldval)
2616 struct frame *f;
2617 Lisp_Object value, oldval;
2618{
2619 int nlines;
2620 int olines = FRAME_MENU_BAR_LINES (f);
2621
2622 /* Right now, menu bars don't work properly in minibuf-only frames;
2623 most of the commands try to apply themselves to the minibuffer
6fc2811b 2624 frame itself, and get an error because you can't switch buffers
ee78dc32
GV
2625 in or split the minibuffer window. */
2626 if (FRAME_MINIBUF_ONLY_P (f))
2627 return;
2628
2629 if (INTEGERP (value))
2630 nlines = XINT (value);
2631 else
2632 nlines = 0;
2633
2634 FRAME_MENU_BAR_LINES (f) = 0;
2635 if (nlines)
2636 FRAME_EXTERNAL_MENU_BAR (f) = 1;
2637 else
2638 {
2639 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
2640 free_frame_menubar (f);
2641 FRAME_EXTERNAL_MENU_BAR (f) = 0;
1edf84e7
GV
2642
2643 /* Adjust the frame size so that the client (text) dimensions
2644 remain the same. This depends on FRAME_EXTERNAL_MENU_BAR being
2645 set correctly. */
2646 x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
6fc2811b 2647 do_pending_window_change (0);
ee78dc32 2648 }
6fc2811b
JR
2649 adjust_glyphs (f);
2650}
2651
2652
2653/* Set the number of lines used for the tool bar of frame F to VALUE.
2654 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
2655 is the old number of tool bar lines. This function changes the
2656 height of all windows on frame F to match the new tool bar height.
2657 The frame's height doesn't change. */
2658
2659void
2660x_set_tool_bar_lines (f, value, oldval)
2661 struct frame *f;
2662 Lisp_Object value, oldval;
2663{
36f8209a
JR
2664 int delta, nlines, root_height;
2665 Lisp_Object root_window;
6fc2811b 2666
dc220243
JR
2667 /* Treat tool bars like menu bars. */
2668 if (FRAME_MINIBUF_ONLY_P (f))
2669 return;
2670
6fc2811b
JR
2671 /* Use VALUE only if an integer >= 0. */
2672 if (INTEGERP (value) && XINT (value) >= 0)
2673 nlines = XFASTINT (value);
2674 else
2675 nlines = 0;
2676
2677 /* Make sure we redisplay all windows in this frame. */
2678 ++windows_or_buffers_changed;
2679
2680 delta = nlines - FRAME_TOOL_BAR_LINES (f);
36f8209a
JR
2681
2682 /* Don't resize the tool-bar to more than we have room for. */
2683 root_window = FRAME_ROOT_WINDOW (f);
2684 root_height = XINT (XWINDOW (root_window)->height);
2685 if (root_height - delta < 1)
2686 {
2687 delta = root_height - 1;
2688 nlines = FRAME_TOOL_BAR_LINES (f) + delta;
2689 }
2690
6fc2811b 2691 FRAME_TOOL_BAR_LINES (f) = nlines;
36f8209a 2692 x_change_window_heights (root_window, delta);
6fc2811b 2693 adjust_glyphs (f);
36f8209a
JR
2694
2695 /* We also have to make sure that the internal border at the top of
2696 the frame, below the menu bar or tool bar, is redrawn when the
2697 tool bar disappears. This is so because the internal border is
2698 below the tool bar if one is displayed, but is below the menu bar
2699 if there isn't a tool bar. The tool bar draws into the area
2700 below the menu bar. */
2701 if (FRAME_W32_WINDOW (f) && FRAME_TOOL_BAR_LINES (f) == 0)
2702 {
2703 updating_frame = f;
2704 clear_frame ();
2705 clear_current_matrices (f);
2706 updating_frame = NULL;
2707 }
2708
2709 /* If the tool bar gets smaller, the internal border below it
2710 has to be cleared. It was formerly part of the display
2711 of the larger tool bar, and updating windows won't clear it. */
2712 if (delta < 0)
2713 {
2714 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
2715 int width = PIXEL_WIDTH (f);
2716 int y = nlines * CANON_Y_UNIT (f);
2717
2718 BLOCK_INPUT;
2719 {
2720 HDC hdc = get_frame_dc (f);
2721 w32_clear_area (f, hdc, 0, y, width, height);
2722 release_frame_dc (f, hdc);
2723 }
2724 UNBLOCK_INPUT;
3cf3436e
JR
2725
2726 if (WINDOWP (f->tool_bar_window))
2727 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
36f8209a 2728 }
ee78dc32
GV
2729}
2730
6fc2811b 2731
ee78dc32 2732/* Change the name of frame F to NAME. If NAME is nil, set F's name to
fbd6baed 2733 w32_id_name.
ee78dc32
GV
2734
2735 If EXPLICIT is non-zero, that indicates that lisp code is setting the
2736 name; if NAME is a string, set F's name to NAME and set
2737 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
2738
2739 If EXPLICIT is zero, that indicates that Emacs redisplay code is
2740 suggesting a new name, which lisp code should override; if
2741 F->explicit_name is set, ignore the new name; otherwise, set it. */
2742
2743void
2744x_set_name (f, name, explicit)
2745 struct frame *f;
2746 Lisp_Object name;
2747 int explicit;
2748{
7d0393cf 2749 /* Make sure that requests from lisp code override requests from
ee78dc32
GV
2750 Emacs redisplay code. */
2751 if (explicit)
2752 {
2753 /* If we're switching from explicit to implicit, we had better
2754 update the mode lines and thereby update the title. */
2755 if (f->explicit_name && NILP (name))
2756 update_mode_lines = 1;
2757
2758 f->explicit_name = ! NILP (name);
2759 }
2760 else if (f->explicit_name)
2761 return;
2762
fbd6baed 2763 /* If NAME is nil, set the name to the w32_id_name. */
ee78dc32
GV
2764 if (NILP (name))
2765 {
2766 /* Check for no change needed in this very common case
2767 before we do any consing. */
fbd6baed 2768 if (!strcmp (FRAME_W32_DISPLAY_INFO (f)->w32_id_name,
d5db4077 2769 SDATA (f->name)))
ee78dc32 2770 return;
fbd6baed 2771 name = build_string (FRAME_W32_DISPLAY_INFO (f)->w32_id_name);
ee78dc32
GV
2772 }
2773 else
b7826503 2774 CHECK_STRING (name);
ee78dc32
GV
2775
2776 /* Don't change the name if it's already NAME. */
2777 if (! NILP (Fstring_equal (name, f->name)))
2778 return;
2779
1edf84e7
GV
2780 f->name = name;
2781
2782 /* For setting the frame title, the title parameter should override
2783 the name parameter. */
2784 if (! NILP (f->title))
2785 name = f->title;
2786
fbd6baed 2787 if (FRAME_W32_WINDOW (f))
ee78dc32 2788 {
6fc2811b 2789 if (STRING_MULTIBYTE (name))
dfff8a69 2790 name = ENCODE_SYSTEM (name);
6fc2811b 2791
ee78dc32 2792 BLOCK_INPUT;
d5db4077 2793 SetWindowText(FRAME_W32_WINDOW (f), SDATA (name));
ee78dc32
GV
2794 UNBLOCK_INPUT;
2795 }
ee78dc32
GV
2796}
2797
2798/* This function should be called when the user's lisp code has
2799 specified a name for the frame; the name will override any set by the
2800 redisplay code. */
2801void
2802x_explicitly_set_name (f, arg, oldval)
2803 FRAME_PTR f;
2804 Lisp_Object arg, oldval;
2805{
2806 x_set_name (f, arg, 1);
2807}
2808
2809/* This function should be called by Emacs redisplay code to set the
2810 name; names set this way will never override names set by the user's
2811 lisp code. */
2812void
2813x_implicitly_set_name (f, arg, oldval)
2814 FRAME_PTR f;
2815 Lisp_Object arg, oldval;
2816{
2817 x_set_name (f, arg, 0);
2818}
1edf84e7
GV
2819\f
2820/* Change the title of frame F to NAME.
2821 If NAME is nil, use the frame name as the title.
2822
2823 If EXPLICIT is non-zero, that indicates that lisp code is setting the
2824 name; if NAME is a string, set F's name to NAME and set
2825 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
2826
2827 If EXPLICIT is zero, that indicates that Emacs redisplay code is
2828 suggesting a new name, which lisp code should override; if
2829 F->explicit_name is set, ignore the new name; otherwise, set it. */
ee78dc32 2830
1edf84e7 2831void
6fc2811b 2832x_set_title (f, name, old_name)
1edf84e7 2833 struct frame *f;
6fc2811b 2834 Lisp_Object name, old_name;
1edf84e7
GV
2835{
2836 /* Don't change the title if it's already NAME. */
2837 if (EQ (name, f->title))
2838 return;
2839
2840 update_mode_lines = 1;
2841
2842 f->title = name;
2843
2844 if (NILP (name))
2845 name = f->name;
2846
2847 if (FRAME_W32_WINDOW (f))
2848 {
6fc2811b 2849 if (STRING_MULTIBYTE (name))
dfff8a69 2850 name = ENCODE_SYSTEM (name);
6fc2811b 2851
1edf84e7 2852 BLOCK_INPUT;
d5db4077 2853 SetWindowText(FRAME_W32_WINDOW (f), SDATA (name));
1edf84e7
GV
2854 UNBLOCK_INPUT;
2855 }
2856}
2857\f
ee78dc32
GV
2858void
2859x_set_autoraise (f, arg, oldval)
2860 struct frame *f;
2861 Lisp_Object arg, oldval;
2862{
2863 f->auto_raise = !EQ (Qnil, arg);
2864}
2865
2866void
2867x_set_autolower (f, arg, oldval)
2868 struct frame *f;
2869 Lisp_Object arg, oldval;
2870{
2871 f->auto_lower = !EQ (Qnil, arg);
2872}
2873
2874void
2875x_set_unsplittable (f, arg, oldval)
2876 struct frame *f;
2877 Lisp_Object arg, oldval;
2878{
2879 f->no_split = !NILP (arg);
2880}
2881
2882void
2883x_set_vertical_scroll_bars (f, arg, oldval)
2884 struct frame *f;
2885 Lisp_Object arg, oldval;
2886{
1026b400
RS
2887 if ((EQ (arg, Qleft) && FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
2888 || (EQ (arg, Qright) && FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f))
2889 || (NILP (arg) && FRAME_HAS_VERTICAL_SCROLL_BARS (f))
2890 || (!NILP (arg) && ! FRAME_HAS_VERTICAL_SCROLL_BARS (f)))
ee78dc32 2891 {
1026b400
RS
2892 FRAME_VERTICAL_SCROLL_BAR_TYPE (f) = NILP (arg) ?
2893 vertical_scroll_bar_none :
87996783
GV
2894 /* Put scroll bars on the right by default, as is conventional
2895 on MS-Windows. */
2896 EQ (Qleft, arg)
7d0393cf 2897 ? vertical_scroll_bar_left
87996783 2898 : vertical_scroll_bar_right;
ee78dc32
GV
2899
2900 /* We set this parameter before creating the window for the
2901 frame, so we can get the geometry right from the start.
2902 However, if the window hasn't been created yet, we shouldn't
2903 call x_set_window_size. */
fbd6baed 2904 if (FRAME_W32_WINDOW (f))
ee78dc32 2905 x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
6fc2811b 2906 do_pending_window_change (0);
ee78dc32
GV
2907 }
2908}
2909
2910void
2911x_set_scroll_bar_width (f, arg, oldval)
2912 struct frame *f;
2913 Lisp_Object arg, oldval;
2914{
6fc2811b
JR
2915 int wid = FONT_WIDTH (f->output_data.w32->font);
2916
ee78dc32
GV
2917 if (NILP (arg))
2918 {
6fc2811b
JR
2919 FRAME_SCROLL_BAR_PIXEL_WIDTH (f) = GetSystemMetrics (SM_CXVSCROLL);
2920 FRAME_SCROLL_BAR_COLS (f) = (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) +
2921 wid - 1) / wid;
2922 if (FRAME_W32_WINDOW (f))
2923 x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
2924 do_pending_window_change (0);
ee78dc32
GV
2925 }
2926 else if (INTEGERP (arg) && XINT (arg) > 0
2927 && XFASTINT (arg) != FRAME_SCROLL_BAR_PIXEL_WIDTH (f))
2928 {
ee78dc32 2929 FRAME_SCROLL_BAR_PIXEL_WIDTH (f) = XFASTINT (arg);
6fc2811b
JR
2930 FRAME_SCROLL_BAR_COLS (f) = (FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
2931 + wid-1) / wid;
fbd6baed 2932 if (FRAME_W32_WINDOW (f))
ee78dc32 2933 x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
6fc2811b 2934 do_pending_window_change (0);
ee78dc32 2935 }
6fc2811b
JR
2936 change_frame_size (f, 0, FRAME_WIDTH (f), 0, 0, 0);
2937 XWINDOW (FRAME_SELECTED_WINDOW (f))->cursor.hpos = 0;
7d0393cf 2938 XWINDOW (FRAME_SELECTED_WINDOW (f))->cursor.x = 0;
ee78dc32
GV
2939}
2940\f
7d0393cf 2941/* Subroutines of creating a frame. */
ee78dc32
GV
2942
2943/* Make sure that Vx_resource_name is set to a reasonable value.
2944 Fix it up, or set it to `emacs' if it is too hopeless. */
2945
2946static void
2947validate_x_resource_name ()
2948{
6fc2811b 2949 int len = 0;
ee78dc32
GV
2950 /* Number of valid characters in the resource name. */
2951 int good_count = 0;
2952 /* Number of invalid characters in the resource name. */
2953 int bad_count = 0;
2954 Lisp_Object new;
2955 int i;
2956
2957 if (STRINGP (Vx_resource_name))
2958 {
d5db4077 2959 unsigned char *p = SDATA (Vx_resource_name);
ee78dc32
GV
2960 int i;
2961
d5db4077 2962 len = SBYTES (Vx_resource_name);
ee78dc32
GV
2963
2964 /* Only letters, digits, - and _ are valid in resource names.
2965 Count the valid characters and count the invalid ones. */
2966 for (i = 0; i < len; i++)
2967 {
2968 int c = p[i];
2969 if (! ((c >= 'a' && c <= 'z')
2970 || (c >= 'A' && c <= 'Z')
2971 || (c >= '0' && c <= '9')
2972 || c == '-' || c == '_'))
2973 bad_count++;
2974 else
2975 good_count++;
2976 }
2977 }
2978 else
2979 /* Not a string => completely invalid. */
2980 bad_count = 5, good_count = 0;
2981
2982 /* If name is valid already, return. */
2983 if (bad_count == 0)
2984 return;
2985
2986 /* If name is entirely invalid, or nearly so, use `emacs'. */
2987 if (good_count == 0
2988 || (good_count == 1 && bad_count > 0))
2989 {
2990 Vx_resource_name = build_string ("emacs");
2991 return;
2992 }
2993
2994 /* Name is partly valid. Copy it and replace the invalid characters
2995 with underscores. */
2996
2997 Vx_resource_name = new = Fcopy_sequence (Vx_resource_name);
2998
2999 for (i = 0; i < len; i++)
3000 {
d5db4077 3001 int c = SREF (new, i);
ee78dc32
GV
3002 if (! ((c >= 'a' && c <= 'z')
3003 || (c >= 'A' && c <= 'Z')
3004 || (c >= '0' && c <= '9')
3005 || c == '-' || c == '_'))
7960d5ab 3006 SSET (new, i, '_');
ee78dc32
GV
3007 }
3008}
3009
3010
3011extern char *x_get_string_resource ();
3012
3013DEFUN ("x-get-resource", Fx_get_resource, Sx_get_resource, 2, 4, 0,
74e1aeec
JR
3014 doc: /* Return the value of ATTRIBUTE, of class CLASS, from the X defaults database.
3015This uses `INSTANCE.ATTRIBUTE' as the key and `Emacs.CLASS' as the
3016class, where INSTANCE is the name under which Emacs was invoked, or
3017the name specified by the `-name' or `-rn' command-line arguments.
3018
3019The optional arguments COMPONENT and SUBCLASS add to the key and the
3020class, respectively. You must specify both of them or neither.
3021If you specify them, the key is `INSTANCE.COMPONENT.ATTRIBUTE'
3022and the class is `Emacs.CLASS.SUBCLASS'. */)
ee78dc32
GV
3023 (attribute, class, component, subclass)
3024 Lisp_Object attribute, class, component, subclass;
3025{
3026 register char *value;
3027 char *name_key;
3028 char *class_key;
3029
b7826503
PJ
3030 CHECK_STRING (attribute);
3031 CHECK_STRING (class);
ee78dc32
GV
3032
3033 if (!NILP (component))
b7826503 3034 CHECK_STRING (component);
ee78dc32 3035 if (!NILP (subclass))
b7826503 3036 CHECK_STRING (subclass);
ee78dc32
GV
3037 if (NILP (component) != NILP (subclass))
3038 error ("x-get-resource: must specify both COMPONENT and SUBCLASS or neither");
3039
3040 validate_x_resource_name ();
3041
3042 /* Allocate space for the components, the dots which separate them,
3043 and the final '\0'. Make them big enough for the worst case. */
d5db4077 3044 name_key = (char *) alloca (SBYTES (Vx_resource_name)
ee78dc32 3045 + (STRINGP (component)
d5db4077
KR
3046 ? SBYTES (component) : 0)
3047 + SBYTES (attribute)
ee78dc32
GV
3048 + 3);
3049
3050 class_key = (char *) alloca ((sizeof (EMACS_CLASS) - 1)
d5db4077 3051 + SBYTES (class)
ee78dc32 3052 + (STRINGP (subclass)
d5db4077 3053 ? SBYTES (subclass) : 0)
ee78dc32
GV
3054 + 3);
3055
3056 /* Start with emacs.FRAMENAME for the name (the specific one)
3057 and with `Emacs' for the class key (the general one). */
d5db4077 3058 strcpy (name_key, SDATA (Vx_resource_name));
ee78dc32
GV
3059 strcpy (class_key, EMACS_CLASS);
3060
3061 strcat (class_key, ".");
d5db4077 3062 strcat (class_key, SDATA (class));
ee78dc32
GV
3063
3064 if (!NILP (component))
3065 {
3066 strcat (class_key, ".");
d5db4077 3067 strcat (class_key, SDATA (subclass));
ee78dc32
GV
3068
3069 strcat (name_key, ".");
d5db4077 3070 strcat (name_key, SDATA (component));
ee78dc32
GV
3071 }
3072
3073 strcat (name_key, ".");
d5db4077 3074 strcat (name_key, SDATA (attribute));
ee78dc32
GV
3075
3076 value = x_get_string_resource (Qnil,
3077 name_key, class_key);
3078
3079 if (value != (char *) 0)
3080 return build_string (value);
3081 else
3082 return Qnil;
3083}
3084
3085/* Used when C code wants a resource value. */
3086
3087char *
3088x_get_resource_string (attribute, class)
3089 char *attribute, *class;
3090{
ee78dc32
GV
3091 char *name_key;
3092 char *class_key;
6fc2811b 3093 struct frame *sf = SELECTED_FRAME ();
ee78dc32
GV
3094
3095 /* Allocate space for the components, the dots which separate them,
3096 and the final '\0'. */
d5db4077 3097 name_key = (char *) alloca (SBYTES (Vinvocation_name)
ee78dc32
GV
3098 + strlen (attribute) + 2);
3099 class_key = (char *) alloca ((sizeof (EMACS_CLASS) - 1)
3100 + strlen (class) + 2);
3101
3102 sprintf (name_key, "%s.%s",
d5db4077 3103 SDATA (Vinvocation_name),
ee78dc32
GV
3104 attribute);
3105 sprintf (class_key, "%s.%s", EMACS_CLASS, class);
3106
6fc2811b 3107 return x_get_string_resource (sf, name_key, class_key);
ee78dc32
GV
3108}
3109
3110/* Types we might convert a resource string into. */
3111enum resource_types
6fc2811b
JR
3112{
3113 RES_TYPE_NUMBER,
3114 RES_TYPE_FLOAT,
3115 RES_TYPE_BOOLEAN,
3116 RES_TYPE_STRING,
3117 RES_TYPE_SYMBOL
3118};
ee78dc32
GV
3119
3120/* Return the value of parameter PARAM.
3121
3122 First search ALIST, then Vdefault_frame_alist, then the X defaults
3123 database, using ATTRIBUTE as the attribute name and CLASS as its class.
3124
3125 Convert the resource to the type specified by desired_type.
3126
3127 If no default is specified, return Qunbound. If you call
6fc2811b 3128 w32_get_arg, make sure you deal with Qunbound in a reasonable way,
ee78dc32
GV
3129 and don't let it get stored in any Lisp-visible variables! */
3130
3131static Lisp_Object
6fc2811b 3132w32_get_arg (alist, param, attribute, class, type)
ee78dc32
GV
3133 Lisp_Object alist, param;
3134 char *attribute;
3135 char *class;
3136 enum resource_types type;
3137{
3138 register Lisp_Object tem;
3139
3140 tem = Fassq (param, alist);
3141 if (EQ (tem, Qnil))
3142 tem = Fassq (param, Vdefault_frame_alist);
3143 if (EQ (tem, Qnil))
3144 {
3145
3146 if (attribute)
3147 {
3148 tem = Fx_get_resource (build_string (attribute),
3149 build_string (class),
3150 Qnil, Qnil);
3151
3152 if (NILP (tem))
3153 return Qunbound;
3154
3155 switch (type)
3156 {
6fc2811b 3157 case RES_TYPE_NUMBER:
d5db4077 3158 return make_number (atoi (SDATA (tem)));
ee78dc32 3159
6fc2811b 3160 case RES_TYPE_FLOAT:
d5db4077 3161 return make_float (atof (SDATA (tem)));
6fc2811b
JR
3162
3163 case RES_TYPE_BOOLEAN:
ee78dc32 3164 tem = Fdowncase (tem);
d5db4077
KR
3165 if (!strcmp (SDATA (tem), "on")
3166 || !strcmp (SDATA (tem), "true"))
ee78dc32 3167 return Qt;
7d0393cf 3168 else
ee78dc32
GV
3169 return Qnil;
3170
6fc2811b 3171 case RES_TYPE_STRING:
ee78dc32
GV
3172 return tem;
3173
6fc2811b 3174 case RES_TYPE_SYMBOL:
ee78dc32
GV
3175 /* As a special case, we map the values `true' and `on'
3176 to Qt, and `false' and `off' to Qnil. */
3177 {
3178 Lisp_Object lower;
3179 lower = Fdowncase (tem);
d5db4077
KR
3180 if (!strcmp (SDATA (lower), "on")
3181 || !strcmp (SDATA (lower), "true"))
ee78dc32 3182 return Qt;
d5db4077
KR
3183 else if (!strcmp (SDATA (lower), "off")
3184 || !strcmp (SDATA (lower), "false"))
ee78dc32
GV
3185 return Qnil;
3186 else
3187 return Fintern (tem, Qnil);
3188 }
3189
3190 default:
3191 abort ();
3192 }
3193 }
3194 else
3195 return Qunbound;
3196 }
3197 return Fcdr (tem);
3198}
3199
3200/* Record in frame F the specified or default value according to ALIST
dfff8a69
JR
3201 of the parameter named PROP (a Lisp symbol).
3202 If no value is specified for PROP, look for an X default for XPROP
ee78dc32
GV
3203 on the frame named NAME.
3204 If that is not found either, use the value DEFLT. */
3205
3206static Lisp_Object
3207x_default_parameter (f, alist, prop, deflt, xprop, xclass, type)
3208 struct frame *f;
3209 Lisp_Object alist;
3210 Lisp_Object prop;
3211 Lisp_Object deflt;
3212 char *xprop;
3213 char *xclass;
3214 enum resource_types type;
3215{
3216 Lisp_Object tem;
3217
6fc2811b 3218 tem = w32_get_arg (alist, prop, xprop, xclass, type);
ee78dc32
GV
3219 if (EQ (tem, Qunbound))
3220 tem = deflt;
3221 x_set_frame_parameters (f, Fcons (Fcons (prop, tem), Qnil));
3222 return tem;
3223}
3224\f
3225DEFUN ("x-parse-geometry", Fx_parse_geometry, Sx_parse_geometry, 1, 1, 0,
74e1aeec
JR
3226 doc: /* Parse an X-style geometry string STRING.
3227Returns an alist of the form ((top . TOP), (left . LEFT) ... ).
3228The properties returned may include `top', `left', `height', and `width'.
3229The value of `left' or `top' may be an integer,
3230or a list (+ N) meaning N pixels relative to top/left corner,
3231or a list (- N) meaning -N pixels relative to bottom/right corner. */)
ee78dc32
GV
3232 (string)
3233 Lisp_Object string;
3234{
3235 int geometry, x, y;
3236 unsigned int width, height;
3237 Lisp_Object result;
3238
b7826503 3239 CHECK_STRING (string);
ee78dc32 3240
d5db4077 3241 geometry = XParseGeometry ((char *) SDATA (string),
ee78dc32
GV
3242 &x, &y, &width, &height);
3243
3244 result = Qnil;
3245 if (geometry & XValue)
3246 {
3247 Lisp_Object element;
3248
3249 if (x >= 0 && (geometry & XNegative))
3250 element = Fcons (Qleft, Fcons (Qminus, Fcons (make_number (-x), Qnil)));
3251 else if (x < 0 && ! (geometry & XNegative))
3252 element = Fcons (Qleft, Fcons (Qplus, Fcons (make_number (x), Qnil)));
3253 else
3254 element = Fcons (Qleft, make_number (x));
3255 result = Fcons (element, result);
3256 }
3257
3258 if (geometry & YValue)
3259 {
3260 Lisp_Object element;
3261
3262 if (y >= 0 && (geometry & YNegative))
3263 element = Fcons (Qtop, Fcons (Qminus, Fcons (make_number (-y), Qnil)));
3264 else if (y < 0 && ! (geometry & YNegative))
3265 element = Fcons (Qtop, Fcons (Qplus, Fcons (make_number (y), Qnil)));
3266 else
3267 element = Fcons (Qtop, make_number (y));
3268 result = Fcons (element, result);
3269 }
3270
3271 if (geometry & WidthValue)
3272 result = Fcons (Fcons (Qwidth, make_number (width)), result);
3273 if (geometry & HeightValue)
3274 result = Fcons (Fcons (Qheight, make_number (height)), result);
3275
3276 return result;
3277}
3278
3279/* Calculate the desired size and position of this window,
3280 and return the flags saying which aspects were specified.
3281
3282 This function does not make the coordinates positive. */
3283
3284#define DEFAULT_ROWS 40
3285#define DEFAULT_COLS 80
3286
3287static int
3288x_figure_window_size (f, parms)
3289 struct frame *f;
3290 Lisp_Object parms;
3291{
3292 register Lisp_Object tem0, tem1, tem2;
ee78dc32
GV
3293 long window_prompting = 0;
3294
3295 /* Default values if we fall through.
3296 Actually, if that happens we should get
3297 window manager prompting. */
1026b400 3298 SET_FRAME_WIDTH (f, DEFAULT_COLS);
ee78dc32
GV
3299 f->height = DEFAULT_ROWS;
3300 /* Window managers expect that if program-specified
3301 positions are not (0,0), they're intentional, not defaults. */
fbd6baed
GV
3302 f->output_data.w32->top_pos = 0;
3303 f->output_data.w32->left_pos = 0;
ee78dc32 3304
35b41202
JR
3305 /* Ensure that old new_width and new_height will not override the
3306 values set here. */
3307 FRAME_NEW_WIDTH (f) = 0;
3308 FRAME_NEW_HEIGHT (f) = 0;
3309
6fc2811b
JR
3310 tem0 = w32_get_arg (parms, Qheight, 0, 0, RES_TYPE_NUMBER);
3311 tem1 = w32_get_arg (parms, Qwidth, 0, 0, RES_TYPE_NUMBER);
3312 tem2 = w32_get_arg (parms, Quser_size, 0, 0, RES_TYPE_NUMBER);
ee78dc32
GV
3313 if (! EQ (tem0, Qunbound) || ! EQ (tem1, Qunbound))
3314 {
3315 if (!EQ (tem0, Qunbound))
3316 {
b7826503 3317 CHECK_NUMBER (tem0);
ee78dc32
GV
3318 f->height = XINT (tem0);
3319 }
3320 if (!EQ (tem1, Qunbound))
3321 {
b7826503 3322 CHECK_NUMBER (tem1);
1026b400 3323 SET_FRAME_WIDTH (f, XINT (tem1));
ee78dc32
GV
3324 }
3325 if (!NILP (tem2) && !EQ (tem2, Qunbound))
3326 window_prompting |= USSize;
3327 else
3328 window_prompting |= PSize;
3329 }
3330
fbd6baed 3331 f->output_data.w32->vertical_scroll_bar_extra
ee78dc32
GV
3332 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
3333 ? 0
3334 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
3335 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
fbd6baed 3336 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.w32->font)));
f7b9d4d1 3337
41c1bdd9 3338 x_compute_fringe_widths (f, 0);
f7b9d4d1 3339
fbd6baed
GV
3340 f->output_data.w32->pixel_width = CHAR_TO_PIXEL_WIDTH (f, f->width);
3341 f->output_data.w32->pixel_height = CHAR_TO_PIXEL_HEIGHT (f, f->height);
ee78dc32 3342
6fc2811b
JR
3343 tem0 = w32_get_arg (parms, Qtop, 0, 0, RES_TYPE_NUMBER);
3344 tem1 = w32_get_arg (parms, Qleft, 0, 0, RES_TYPE_NUMBER);
3345 tem2 = w32_get_arg (parms, Quser_position, 0, 0, RES_TYPE_NUMBER);
ee78dc32
GV
3346 if (! EQ (tem0, Qunbound) || ! EQ (tem1, Qunbound))
3347 {
3348 if (EQ (tem0, Qminus))
3349 {
fbd6baed 3350 f->output_data.w32->top_pos = 0;
ee78dc32
GV
3351 window_prompting |= YNegative;
3352 }
8e713be6
KR
3353 else if (CONSP (tem0) && EQ (XCAR (tem0), Qminus)
3354 && CONSP (XCDR (tem0))
3355 && INTEGERP (XCAR (XCDR (tem0))))
ee78dc32 3356 {
8e713be6 3357 f->output_data.w32->top_pos = - XINT (XCAR (XCDR (tem0)));
ee78dc32
GV
3358 window_prompting |= YNegative;
3359 }
8e713be6
KR
3360 else if (CONSP (tem0) && EQ (XCAR (tem0), Qplus)
3361 && CONSP (XCDR (tem0))
3362 && INTEGERP (XCAR (XCDR (tem0))))
ee78dc32 3363 {
8e713be6 3364 f->output_data.w32->top_pos = XINT (XCAR (XCDR (tem0)));
ee78dc32
GV
3365 }
3366 else if (EQ (tem0, Qunbound))
fbd6baed 3367 f->output_data.w32->top_pos = 0;
ee78dc32
GV
3368 else
3369 {
b7826503 3370 CHECK_NUMBER (tem0);
fbd6baed
GV
3371 f->output_data.w32->top_pos = XINT (tem0);
3372 if (f->output_data.w32->top_pos < 0)
ee78dc32
GV
3373 window_prompting |= YNegative;
3374 }
3375
3376 if (EQ (tem1, Qminus))
3377 {
fbd6baed 3378 f->output_data.w32->left_pos = 0;
ee78dc32
GV
3379 window_prompting |= XNegative;
3380 }
8e713be6
KR
3381 else if (CONSP (tem1) && EQ (XCAR (tem1), Qminus)
3382 && CONSP (XCDR (tem1))
3383 && INTEGERP (XCAR (XCDR (tem1))))
ee78dc32 3384 {
8e713be6 3385 f->output_data.w32->left_pos = - XINT (XCAR (XCDR (tem1)));
ee78dc32
GV
3386 window_prompting |= XNegative;
3387 }
8e713be6
KR
3388 else if (CONSP (tem1) && EQ (XCAR (tem1), Qplus)
3389 && CONSP (XCDR (tem1))
3390 && INTEGERP (XCAR (XCDR (tem1))))
ee78dc32 3391 {
8e713be6 3392 f->output_data.w32->left_pos = XINT (XCAR (XCDR (tem1)));
ee78dc32
GV
3393 }
3394 else if (EQ (tem1, Qunbound))
fbd6baed 3395 f->output_data.w32->left_pos = 0;
ee78dc32
GV
3396 else
3397 {
b7826503 3398 CHECK_NUMBER (tem1);
fbd6baed
GV
3399 f->output_data.w32->left_pos = XINT (tem1);
3400 if (f->output_data.w32->left_pos < 0)
ee78dc32
GV
3401 window_prompting |= XNegative;
3402 }
3403
3404 if (!NILP (tem2) && ! EQ (tem2, Qunbound))
3405 window_prompting |= USPosition;
3406 else
3407 window_prompting |= PPosition;
3408 }
3409
f7b9d4d1
JR
3410 if (f->output_data.w32->want_fullscreen != FULLSCREEN_NONE)
3411 {
3412 int left, top;
3413 int width, height;
3414
3415 /* It takes both for some WM:s to place it where we want */
3416 window_prompting = USPosition | PPosition;
3417 x_fullscreen_adjust (f, &width, &height, &top, &left);
3418 f->width = width;
3419 f->height = height;
3420 f->output_data.w32->pixel_width = CHAR_TO_PIXEL_WIDTH (f, f->width);
3421 f->output_data.w32->pixel_height = CHAR_TO_PIXEL_HEIGHT (f, f->height);
3422 f->output_data.w32->left_pos = left;
3423 f->output_data.w32->top_pos = top;
3424 }
3425
ee78dc32
GV
3426 return window_prompting;
3427}
3428
3429\f
c9b2104d
JR
3430Cursor
3431w32_load_cursor (LPCTSTR name)
3432{
3433 /* Try first to load cursor from application resource. */
3434 Cursor cursor = LoadImage ((HINSTANCE) GetModuleHandle(NULL),
3435 name, IMAGE_CURSOR, 0, 0,
3436 LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_SHARED);
3437 if (!cursor)
3438 {
3439 /* Then try to load a shared predefined cursor. */
3440 cursor = LoadImage (NULL, name, IMAGE_CURSOR, 0, 0,
3441 LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_SHARED);
3442 }
3443 return cursor;
3444}
ee78dc32 3445
fbd6baed 3446extern LRESULT CALLBACK w32_wnd_proc ();
ee78dc32 3447
7d0393cf 3448BOOL
fbd6baed 3449w32_init_class (hinst)
ee78dc32
GV
3450 HINSTANCE hinst;
3451{
3452 WNDCLASS wc;
3453
5ac45f98 3454 wc.style = CS_HREDRAW | CS_VREDRAW;
fbd6baed 3455 wc.lpfnWndProc = (WNDPROC) w32_wnd_proc;
ee78dc32
GV
3456 wc.cbClsExtra = 0;
3457 wc.cbWndExtra = WND_EXTRA_BYTES;
3458 wc.hInstance = hinst;
3459 wc.hIcon = LoadIcon (hinst, EMACS_CLASS);
c9b2104d 3460 wc.hCursor = w32_load_cursor (IDC_ARROW);
4587b026 3461 wc.hbrBackground = NULL; /* GetStockObject (WHITE_BRUSH); */
ee78dc32
GV
3462 wc.lpszMenuName = NULL;
3463 wc.lpszClassName = EMACS_CLASS;
3464
3465 return (RegisterClass (&wc));
3466}
3467
7d0393cf 3468HWND
fbd6baed 3469w32_createscrollbar (f, bar)
ee78dc32
GV
3470 struct frame *f;
3471 struct scroll_bar * bar;
3472{
3473 return (CreateWindow ("SCROLLBAR", "", SBS_VERT | WS_CHILD | WS_VISIBLE,
3474 /* Position and size of scroll bar. */
6fc2811b 3475 XINT(bar->left) + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7d0393cf 3476 XINT(bar->top),
6fc2811b
JR
3477 XINT(bar->width) - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
3478 XINT(bar->height),
fbd6baed 3479 FRAME_W32_WINDOW (f),
ee78dc32
GV
3480 NULL,
3481 hinst,
3482 NULL));
3483}
3484
7d0393cf 3485void
fbd6baed 3486w32_createwindow (f)
ee78dc32
GV
3487 struct frame *f;
3488{
3489 HWND hwnd;
1edf84e7
GV
3490 RECT rect;
3491
3492 rect.left = rect.top = 0;
3493 rect.right = PIXEL_WIDTH (f);
3494 rect.bottom = PIXEL_HEIGHT (f);
7d0393cf 3495
1edf84e7
GV
3496 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
3497 FRAME_EXTERNAL_MENU_BAR (f));
7d0393cf 3498
ee78dc32 3499 /* Do first time app init */
7d0393cf 3500
ee78dc32
GV
3501 if (!hprevinst)
3502 {
fbd6baed 3503 w32_init_class (hinst);
ee78dc32 3504 }
7d0393cf 3505
1edf84e7
GV
3506 FRAME_W32_WINDOW (f) = hwnd
3507 = CreateWindow (EMACS_CLASS,
3508 f->namebuf,
9ead1b60 3509 f->output_data.w32->dwStyle | WS_CLIPCHILDREN,
1edf84e7
GV
3510 f->output_data.w32->left_pos,
3511 f->output_data.w32->top_pos,
3512 rect.right - rect.left,
3513 rect.bottom - rect.top,
3514 NULL,
3515 NULL,
3516 hinst,
3517 NULL);
3518
ee78dc32
GV
3519 if (hwnd)
3520 {
1edf84e7
GV
3521 SetWindowLong (hwnd, WND_FONTWIDTH_INDEX, FONT_WIDTH (f->output_data.w32->font));
3522 SetWindowLong (hwnd, WND_LINEHEIGHT_INDEX, f->output_data.w32->line_height);
3523 SetWindowLong (hwnd, WND_BORDER_INDEX, f->output_data.w32->internal_border_width);
3524 SetWindowLong (hwnd, WND_SCROLLBAR_INDEX, f->output_data.w32->vertical_scroll_bar_extra);
6fc2811b 3525 SetWindowLong (hwnd, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
ee78dc32 3526
cb9e33d4
RS
3527 /* Enable drag-n-drop. */
3528 DragAcceptFiles (hwnd, TRUE);
7d0393cf 3529
5ac45f98
GV
3530 /* Do this to discard the default setting specified by our parent. */
3531 ShowWindow (hwnd, SW_HIDE);
3c190163 3532 }
3c190163
GV
3533}
3534
7d0393cf 3535void
ee78dc32 3536my_post_msg (wmsg, hwnd, msg, wParam, lParam)
fbd6baed 3537 W32Msg * wmsg;
ee78dc32
GV
3538 HWND hwnd;
3539 UINT msg;
3540 WPARAM wParam;
3541 LPARAM lParam;
3542{
3543 wmsg->msg.hwnd = hwnd;
3544 wmsg->msg.message = msg;
3545 wmsg->msg.wParam = wParam;
3546 wmsg->msg.lParam = lParam;
3547 wmsg->msg.time = GetMessageTime ();
3548
3549 post_msg (wmsg);
3550}
3551
e9e23e23 3552/* GetKeyState and MapVirtualKey on Windows 95 do not actually distinguish
a1a80b40
GV
3553 between left and right keys as advertised. We test for this
3554 support dynamically, and set a flag when the support is absent. If
3555 absent, we keep track of the left and right control and alt keys
3556 ourselves. This is particularly necessary on keyboards that rely
3557 upon the AltGr key, which is represented as having the left control
3558 and right alt keys pressed. For these keyboards, we need to know
3559 when the left alt key has been pressed in addition to the AltGr key
3560 so that we can properly support M-AltGr-key sequences (such as M-@
3561 on Swedish keyboards). */
3562
3563#define EMACS_LCONTROL 0
3564#define EMACS_RCONTROL 1
3565#define EMACS_LMENU 2
3566#define EMACS_RMENU 3
3567
3568static int modifiers[4];
3569static int modifiers_recorded;
3570static int modifier_key_support_tested;
3571
3572static void
3573test_modifier_support (unsigned int wparam)
3574{
3575 unsigned int l, r;
3576
3577 if (wparam != VK_CONTROL && wparam != VK_MENU)
3578 return;
3579 if (wparam == VK_CONTROL)
3580 {
3581 l = VK_LCONTROL;
3582 r = VK_RCONTROL;
3583 }
3584 else
3585 {
3586 l = VK_LMENU;
3587 r = VK_RMENU;
3588 }
3589 if (!(GetKeyState (l) & 0x8000) && !(GetKeyState (r) & 0x8000))
3590 modifiers_recorded = 1;
3591 else
3592 modifiers_recorded = 0;
3593 modifier_key_support_tested = 1;
3594}
3595
3596static void
3597record_keydown (unsigned int wparam, unsigned int lparam)
3598{
3599 int i;
3600
3601 if (!modifier_key_support_tested)
3602 test_modifier_support (wparam);
3603
3604 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
3605 return;
3606
3607 if (wparam == VK_CONTROL)
3608 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
3609 else
3610 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
3611
3612 modifiers[i] = 1;
3613}
3614
3615static void
3616record_keyup (unsigned int wparam, unsigned int lparam)
3617{
3618 int i;
3619
3620 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
3621 return;
3622
3623 if (wparam == VK_CONTROL)
3624 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
3625 else
3626 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
3627
3628 modifiers[i] = 0;
3629}
3630
da36a4d6 3631/* Emacs can lose focus while a modifier key has been pressed. When
7d0393cf 3632 it regains focus, be conservative and clear all modifiers since
da36a4d6
GV
3633 we cannot reconstruct the left and right modifier state. */
3634static void
3635reset_modifiers ()
3636{
8681157a
RS
3637 SHORT ctrl, alt;
3638
adcc3809
GV
3639 if (GetFocus () == NULL)
3640 /* Emacs doesn't have keyboard focus. Do nothing. */
da36a4d6 3641 return;
8681157a
RS
3642
3643 ctrl = GetAsyncKeyState (VK_CONTROL);
3644 alt = GetAsyncKeyState (VK_MENU);
3645
8681157a
RS
3646 if (!(ctrl & 0x08000))
3647 /* Clear any recorded control modifier state. */
3648 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
3649
3650 if (!(alt & 0x08000))
3651 /* Clear any recorded alt modifier state. */
3652 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
3653
adcc3809
GV
3654 /* Update the state of all modifier keys, because modifiers used in
3655 hot-key combinations can get stuck on if Emacs loses focus as a
3656 result of a hot-key being pressed. */
3657 {
3658 BYTE keystate[256];
3659
3660#define CURRENT_STATE(key) ((GetAsyncKeyState (key) & 0x8000) >> 8)
3661
3662 GetKeyboardState (keystate);
3663 keystate[VK_SHIFT] = CURRENT_STATE (VK_SHIFT);
3664 keystate[VK_CONTROL] = CURRENT_STATE (VK_CONTROL);
3665 keystate[VK_LCONTROL] = CURRENT_STATE (VK_LCONTROL);
3666 keystate[VK_RCONTROL] = CURRENT_STATE (VK_RCONTROL);
3667 keystate[VK_MENU] = CURRENT_STATE (VK_MENU);
3668 keystate[VK_LMENU] = CURRENT_STATE (VK_LMENU);
3669 keystate[VK_RMENU] = CURRENT_STATE (VK_RMENU);
3670 keystate[VK_LWIN] = CURRENT_STATE (VK_LWIN);
3671 keystate[VK_RWIN] = CURRENT_STATE (VK_RWIN);
3672 keystate[VK_APPS] = CURRENT_STATE (VK_APPS);
3673 SetKeyboardState (keystate);
3674 }
da36a4d6
GV
3675}
3676
7830e24b
RS
3677/* Synchronize modifier state with what is reported with the current
3678 keystroke. Even if we cannot distinguish between left and right
3679 modifier keys, we know that, if no modifiers are set, then neither
3680 the left or right modifier should be set. */
3681static void
3682sync_modifiers ()
3683{
3684 if (!modifiers_recorded)
3685 return;
3686
7d0393cf 3687 if (!(GetKeyState (VK_CONTROL) & 0x8000))
7830e24b
RS
3688 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
3689
7d0393cf 3690 if (!(GetKeyState (VK_MENU) & 0x8000))
7830e24b
RS
3691 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
3692}
3693
a1a80b40
GV
3694static int
3695modifier_set (int vkey)
3696{
ccc2d29c 3697 if (vkey == VK_CAPITAL || vkey == VK_SCROLL)
891560d6 3698 return (GetKeyState (vkey) & 0x1);
a1a80b40
GV
3699 if (!modifiers_recorded)
3700 return (GetKeyState (vkey) & 0x8000);
3701
3702 switch (vkey)
3703 {
3704 case VK_LCONTROL:
3705 return modifiers[EMACS_LCONTROL];
3706 case VK_RCONTROL:
3707 return modifiers[EMACS_RCONTROL];
3708 case VK_LMENU:
3709 return modifiers[EMACS_LMENU];
3710 case VK_RMENU:
3711 return modifiers[EMACS_RMENU];
a1a80b40
GV
3712 }
3713 return (GetKeyState (vkey) & 0x8000);
3714}
3715
ccc2d29c
GV
3716/* Convert between the modifier bits W32 uses and the modifier bits
3717 Emacs uses. */
3718
3719unsigned int
3720w32_key_to_modifier (int key)
3721{
3722 Lisp_Object key_mapping;
3723
3724 switch (key)
3725 {
3726 case VK_LWIN:
3727 key_mapping = Vw32_lwindow_modifier;
3728 break;
3729 case VK_RWIN:
3730 key_mapping = Vw32_rwindow_modifier;
3731 break;
3732 case VK_APPS:
3733 key_mapping = Vw32_apps_modifier;
3734 break;
3735 case VK_SCROLL:
3736 key_mapping = Vw32_scroll_lock_modifier;
3737 break;
3738 default:
3739 key_mapping = Qnil;
3740 }
3741
adcc3809
GV
3742 /* NB. This code runs in the input thread, asychronously to the lisp
3743 thread, so we must be careful to ensure access to lisp data is
3744 thread-safe. The following code is safe because the modifier
3745 variable values are updated atomically from lisp and symbols are
3746 not relocated by GC. Also, we don't have to worry about seeing GC
3747 markbits here. */
3748 if (EQ (key_mapping, Qhyper))
ccc2d29c 3749 return hyper_modifier;
adcc3809 3750 if (EQ (key_mapping, Qsuper))
ccc2d29c 3751 return super_modifier;
adcc3809 3752 if (EQ (key_mapping, Qmeta))
ccc2d29c 3753 return meta_modifier;
adcc3809 3754 if (EQ (key_mapping, Qalt))
ccc2d29c 3755 return alt_modifier;
adcc3809 3756 if (EQ (key_mapping, Qctrl))
ccc2d29c 3757 return ctrl_modifier;
adcc3809 3758 if (EQ (key_mapping, Qcontrol)) /* synonym for ctrl */
ccc2d29c 3759 return ctrl_modifier;
adcc3809 3760 if (EQ (key_mapping, Qshift))
ccc2d29c
GV
3761 return shift_modifier;
3762
3763 /* Don't generate any modifier if not explicitly requested. */
3764 return 0;
3765}
3766
3767unsigned int
3768w32_get_modifiers ()
3769{
3770 return ((modifier_set (VK_SHIFT) ? shift_modifier : 0) |
3771 (modifier_set (VK_CONTROL) ? ctrl_modifier : 0) |
3772 (modifier_set (VK_LWIN) ? w32_key_to_modifier (VK_LWIN) : 0) |
3773 (modifier_set (VK_RWIN) ? w32_key_to_modifier (VK_RWIN) : 0) |
3774 (modifier_set (VK_APPS) ? w32_key_to_modifier (VK_APPS) : 0) |
3775 (modifier_set (VK_SCROLL) ? w32_key_to_modifier (VK_SCROLL) : 0) |
3776 (modifier_set (VK_MENU) ?
3777 ((NILP (Vw32_alt_is_meta)) ? alt_modifier : meta_modifier) : 0));
3778}
3779
a1a80b40
GV
3780/* We map the VK_* modifiers into console modifier constants
3781 so that we can use the same routines to handle both console
3782 and window input. */
3783
3784static int
ccc2d29c 3785construct_console_modifiers ()
a1a80b40
GV
3786{
3787 int mods;
3788
a1a80b40
GV
3789 mods = 0;
3790 mods |= (modifier_set (VK_SHIFT)) ? SHIFT_PRESSED : 0;
3791 mods |= (modifier_set (VK_CAPITAL)) ? CAPSLOCK_ON : 0;
ccc2d29c
GV
3792 mods |= (modifier_set (VK_SCROLL)) ? SCROLLLOCK_ON : 0;
3793 mods |= (modifier_set (VK_NUMLOCK)) ? NUMLOCK_ON : 0;
a1a80b40
GV
3794 mods |= (modifier_set (VK_LCONTROL)) ? LEFT_CTRL_PRESSED : 0;
3795 mods |= (modifier_set (VK_RCONTROL)) ? RIGHT_CTRL_PRESSED : 0;
3796 mods |= (modifier_set (VK_LMENU)) ? LEFT_ALT_PRESSED : 0;
3797 mods |= (modifier_set (VK_RMENU)) ? RIGHT_ALT_PRESSED : 0;
ccc2d29c
GV
3798 mods |= (modifier_set (VK_LWIN)) ? LEFT_WIN_PRESSED : 0;
3799 mods |= (modifier_set (VK_RWIN)) ? RIGHT_WIN_PRESSED : 0;
3800 mods |= (modifier_set (VK_APPS)) ? APPS_PRESSED : 0;
a1a80b40
GV
3801
3802 return mods;
3803}
3804
ccc2d29c
GV
3805static int
3806w32_get_key_modifiers (unsigned int wparam, unsigned int lparam)
da36a4d6 3807{
ccc2d29c
GV
3808 int mods;
3809
3810 /* Convert to emacs modifiers. */
3811 mods = w32_kbd_mods_to_emacs (construct_console_modifiers (), wparam);
3812
3813 return mods;
3814}
da36a4d6 3815
ccc2d29c
GV
3816unsigned int
3817map_keypad_keys (unsigned int virt_key, unsigned int extended)
3818{
3819 if (virt_key < VK_CLEAR || virt_key > VK_DELETE)
3820 return virt_key;
da36a4d6 3821
ccc2d29c 3822 if (virt_key == VK_RETURN)
da36a4d6
GV
3823 return (extended ? VK_NUMPAD_ENTER : VK_RETURN);
3824
ccc2d29c
GV
3825 if (virt_key >= VK_PRIOR && virt_key <= VK_DOWN)
3826 return (!extended ? (VK_NUMPAD_PRIOR + (virt_key - VK_PRIOR)) : virt_key);
3827
3828 if (virt_key == VK_INSERT || virt_key == VK_DELETE)
3829 return (!extended ? (VK_NUMPAD_INSERT + (virt_key - VK_INSERT)) : virt_key);
3830
3831 if (virt_key == VK_CLEAR)
3832 return (!extended ? VK_NUMPAD_CLEAR : virt_key);
3833
3834 return virt_key;
3835}
3836
3837/* List of special key combinations which w32 would normally capture,
3838 but emacs should grab instead. Not directly visible to lisp, to
3839 simplify synchronization. Each item is an integer encoding a virtual
3840 key code and modifier combination to capture. */
3841Lisp_Object w32_grabbed_keys;
3842
3843#define HOTKEY(vk,mods) make_number (((vk) & 255) | ((mods) << 8))
3844#define HOTKEY_ID(k) (XFASTINT (k) & 0xbfff)
3845#define HOTKEY_VK_CODE(k) (XFASTINT (k) & 255)
3846#define HOTKEY_MODIFIERS(k) (XFASTINT (k) >> 8)
3847
3848/* Register hot-keys for reserved key combinations when Emacs has
3849 keyboard focus, since this is the only way Emacs can receive key
3850 combinations like Alt-Tab which are used by the system. */
3851
3852static void
3853register_hot_keys (hwnd)
3854 HWND hwnd;
3855{
3856 Lisp_Object keylist;
3857
3858 /* Use GC_CONSP, since we are called asynchronously. */
3859 for (keylist = w32_grabbed_keys; GC_CONSP (keylist); keylist = XCDR (keylist))
3860 {
3861 Lisp_Object key = XCAR (keylist);
3862
3863 /* Deleted entries get set to nil. */
3864 if (!INTEGERP (key))
3865 continue;
3866
3867 RegisterHotKey (hwnd, HOTKEY_ID (key),
3868 HOTKEY_MODIFIERS (key), HOTKEY_VK_CODE (key));
3869 }
3870}
3871
3872static void
3873unregister_hot_keys (hwnd)
3874 HWND hwnd;
3875{
3876 Lisp_Object keylist;
3877
3878 /* Use GC_CONSP, since we are called asynchronously. */
3879 for (keylist = w32_grabbed_keys; GC_CONSP (keylist); keylist = XCDR (keylist))
3880 {
3881 Lisp_Object key = XCAR (keylist);
3882
3883 if (!INTEGERP (key))
3884 continue;
3885
3886 UnregisterHotKey (hwnd, HOTKEY_ID (key));
3887 }
3888}
3889
5ac45f98
GV
3890/* Main message dispatch loop. */
3891
1edf84e7
GV
3892static void
3893w32_msg_pump (deferred_msg * msg_buf)
5ac45f98
GV
3894{
3895 MSG msg;
ccc2d29c
GV
3896 int result;
3897 HWND focus_window;
93fbe8b7
GV
3898
3899 msh_mousewheel = RegisterWindowMessage (MSH_MOUSEWHEEL);
7d0393cf 3900
5ac45f98
GV
3901 while (GetMessage (&msg, NULL, 0, 0))
3902 {
3903 if (msg.hwnd == NULL)
3904 {
3905 switch (msg.message)
3906 {
3ef68e6b
AI
3907 case WM_NULL:
3908 /* Produced by complete_deferred_msg; just ignore. */
3909 break;
5ac45f98 3910 case WM_EMACS_CREATEWINDOW:
fbd6baed 3911 w32_createwindow ((struct frame *) msg.wParam);
1edf84e7
GV
3912 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
3913 abort ();
5ac45f98 3914 break;
dfdb4047
GV
3915 case WM_EMACS_SETLOCALE:
3916 SetThreadLocale (msg.wParam);
3917 /* Reply is not expected. */
3918 break;
ccc2d29c
GV
3919 case WM_EMACS_SETKEYBOARDLAYOUT:
3920 result = (int) ActivateKeyboardLayout ((HKL) msg.wParam, 0);
3921 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
3922 result, 0))
3923 abort ();
3924 break;
3925 case WM_EMACS_REGISTER_HOT_KEY:
3926 focus_window = GetFocus ();
3927 if (focus_window != NULL)
3928 RegisterHotKey (focus_window,
3929 HOTKEY_ID (msg.wParam),
3930 HOTKEY_MODIFIERS (msg.wParam),
3931 HOTKEY_VK_CODE (msg.wParam));
3932 /* Reply is not expected. */
3933 break;
3934 case WM_EMACS_UNREGISTER_HOT_KEY:
3935 focus_window = GetFocus ();
3936 if (focus_window != NULL)
3937 UnregisterHotKey (focus_window, HOTKEY_ID (msg.wParam));
adcc3809
GV
3938 /* Mark item as erased. NB: this code must be
3939 thread-safe. The next line is okay because the cons
3940 cell is never made into garbage and is not relocated by
3941 GC. */
f3fbd155 3942 XSETCAR ((Lisp_Object) msg.lParam, Qnil);
ccc2d29c
GV
3943 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
3944 abort ();
3945 break;
adcc3809
GV
3946 case WM_EMACS_TOGGLE_LOCK_KEY:
3947 {
3948 int vk_code = (int) msg.wParam;
3949 int cur_state = (GetKeyState (vk_code) & 1);
3950 Lisp_Object new_state = (Lisp_Object) msg.lParam;
3951
3952 /* NB: This code must be thread-safe. It is safe to
3953 call NILP because symbols are not relocated by GC,
3954 and pointer here is not touched by GC (so the markbit
3955 can't be set). Numbers are safe because they are
3956 immediate values. */
3957 if (NILP (new_state)
3958 || (NUMBERP (new_state)
8edb0a6f 3959 && ((XUINT (new_state)) & 1) != cur_state))
adcc3809
GV
3960 {
3961 one_w32_display_info.faked_key = vk_code;
3962
3963 keybd_event ((BYTE) vk_code,
3964 (BYTE) MapVirtualKey (vk_code, 0),
3965 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
3966 keybd_event ((BYTE) vk_code,
3967 (BYTE) MapVirtualKey (vk_code, 0),
3968 KEYEVENTF_EXTENDEDKEY | 0, 0);
3969 keybd_event ((BYTE) vk_code,
3970 (BYTE) MapVirtualKey (vk_code, 0),
3971 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
3972 cur_state = !cur_state;
3973 }
3974 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
3975 cur_state, 0))
3976 abort ();
3977 }
3978 break;
1edf84e7 3979 default:
1edf84e7 3980 DebPrint (("msg %x not expected by w32_msg_pump\n", msg.message));
5ac45f98
GV
3981 }
3982 }
3983 else
3984 {
3985 DispatchMessage (&msg);
3986 }
1edf84e7
GV
3987
3988 /* Exit nested loop when our deferred message has completed. */
3989 if (msg_buf->completed)
3990 break;
5ac45f98 3991 }
1edf84e7
GV
3992}
3993
3994deferred_msg * deferred_msg_head;
3995
3996static deferred_msg *
3997find_deferred_msg (HWND hwnd, UINT msg)
3998{
3999 deferred_msg * item;
4000
4001 /* Don't actually need synchronization for read access, since
4002 modification of single pointer is always atomic. */
4003 /* enter_crit (); */
4004
4005 for (item = deferred_msg_head; item != NULL; item = item->next)
4006 if (item->w32msg.msg.hwnd == hwnd
4007 && item->w32msg.msg.message == msg)
4008 break;
4009
4010 /* leave_crit (); */
4011
4012 return item;
4013}
4014
4015static LRESULT
4016send_deferred_msg (deferred_msg * msg_buf,
4017 HWND hwnd,
4018 UINT msg,
4019 WPARAM wParam,
4020 LPARAM lParam)
4021{
4022 /* Only input thread can send deferred messages. */
4023 if (GetCurrentThreadId () != dwWindowsThreadId)
4024 abort ();
4025
4026 /* It is an error to send a message that is already deferred. */
4027 if (find_deferred_msg (hwnd, msg) != NULL)
4028 abort ();
4029
4030 /* Enforced synchronization is not needed because this is the only
4031 function that alters deferred_msg_head, and the following critical
4032 section is guaranteed to only be serially reentered (since only the
4033 input thread can call us). */
4034
4035 /* enter_crit (); */
4036
4037 msg_buf->completed = 0;
4038 msg_buf->next = deferred_msg_head;
4039 deferred_msg_head = msg_buf;
4040 my_post_msg (&msg_buf->w32msg, hwnd, msg, wParam, lParam);
4041
4042 /* leave_crit (); */
4043
4044 /* Start a new nested message loop to process other messages until
4045 this one is completed. */
4046 w32_msg_pump (msg_buf);
4047
4048 deferred_msg_head = msg_buf->next;
4049
4050 return msg_buf->result;
4051}
4052
4053void
4054complete_deferred_msg (HWND hwnd, UINT msg, LRESULT result)
4055{
4056 deferred_msg * msg_buf = find_deferred_msg (hwnd, msg);
4057
4058 if (msg_buf == NULL)
3ef68e6b
AI
4059 /* Message may have been cancelled, so don't abort(). */
4060 return;
1edf84e7
GV
4061
4062 msg_buf->result = result;
4063 msg_buf->completed = 1;
4064
4065 /* Ensure input thread is woken so it notices the completion. */
4066 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
4067}
4068
3ef68e6b
AI
4069void
4070cancel_all_deferred_msgs ()
4071{
4072 deferred_msg * item;
4073
4074 /* Don't actually need synchronization for read access, since
4075 modification of single pointer is always atomic. */
4076 /* enter_crit (); */
4077
4078 for (item = deferred_msg_head; item != NULL; item = item->next)
4079 {
4080 item->result = 0;
4081 item->completed = 1;
4082 }
4083
4084 /* leave_crit (); */
4085
4086 /* Ensure input thread is woken so it notices the completion. */
4087 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
4088}
1edf84e7 4089
7d0393cf 4090DWORD
1edf84e7
GV
4091w32_msg_worker (dw)
4092 DWORD dw;
4093{
4094 MSG msg;
4095 deferred_msg dummy_buf;
4096
4097 /* Ensure our message queue is created */
7d0393cf 4098
1edf84e7 4099 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
7d0393cf 4100
1edf84e7
GV
4101 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
4102 abort ();
4103
4104 memset (&dummy_buf, 0, sizeof (dummy_buf));
4105 dummy_buf.w32msg.msg.hwnd = NULL;
4106 dummy_buf.w32msg.msg.message = WM_NULL;
4107
4108 /* This is the inital message loop which should only exit when the
4109 application quits. */
4110 w32_msg_pump (&dummy_buf);
4111
4112 return 0;
5ac45f98
GV
4113}
4114
3ef68e6b
AI
4115static void
4116post_character_message (hwnd, msg, wParam, lParam, modifiers)
4117 HWND hwnd;
4118 UINT msg;
4119 WPARAM wParam;
4120 LPARAM lParam;
4121 DWORD modifiers;
4122
4123{
4124 W32Msg wmsg;
4125
4126 wmsg.dwModifiers = modifiers;
4127
4128 /* Detect quit_char and set quit-flag directly. Note that we
4129 still need to post a message to ensure the main thread will be
4130 woken up if blocked in sys_select(), but we do NOT want to post
4131 the quit_char message itself (because it will usually be as if
4132 the user had typed quit_char twice). Instead, we post a dummy
4133 message that has no particular effect. */
4134 {
4135 int c = wParam;
4136 if (isalpha (c) && wmsg.dwModifiers == ctrl_modifier)
4137 c = make_ctrl_char (c) & 0377;
7d081355
AI
4138 if (c == quit_char
4139 || (wmsg.dwModifiers == 0 &&
4140 XFASTINT (Vw32_quit_key) && wParam == XFASTINT (Vw32_quit_key)))
3ef68e6b
AI
4141 {
4142 Vquit_flag = Qt;
4143
4144 /* The choice of message is somewhat arbitrary, as long as
4145 the main thread handler just ignores it. */
4146 msg = WM_NULL;
4147
4148 /* Interrupt any blocking system calls. */
4149 signal_quit ();
4150
4151 /* As a safety precaution, forcibly complete any deferred
4152 messages. This is a kludge, but I don't see any particularly
4153 clean way to handle the situation where a deferred message is
4154 "dropped" in the lisp thread, and will thus never be
4155 completed, eg. by the user trying to activate the menubar
4156 when the lisp thread is busy, and then typing C-g when the
4157 menubar doesn't open promptly (with the result that the
4158 menubar never responds at all because the deferred
4159 WM_INITMENU message is never completed). Another problem
4160 situation is when the lisp thread calls SendMessage (to send
4161 a window manager command) when a message has been deferred;
4162 the lisp thread gets blocked indefinitely waiting for the
4163 deferred message to be completed, which itself is waiting for
4164 the lisp thread to respond.
4165
4166 Note that we don't want to block the input thread waiting for
4167 a reponse from the lisp thread (although that would at least
4168 solve the deadlock problem above), because we want to be able
4169 to receive C-g to interrupt the lisp thread. */
4170 cancel_all_deferred_msgs ();
4171 }
4172 }
4173
4174 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4175}
4176
ee78dc32
GV
4177/* Main window procedure */
4178
7d0393cf 4179LRESULT CALLBACK
fbd6baed 4180w32_wnd_proc (hwnd, msg, wParam, lParam)
ee78dc32
GV
4181 HWND hwnd;
4182 UINT msg;
4183 WPARAM wParam;
4184 LPARAM lParam;
4185{
4186 struct frame *f;
fbd6baed
GV
4187 struct w32_display_info *dpyinfo = &one_w32_display_info;
4188 W32Msg wmsg;
84fb1139 4189 int windows_translate;
576ba81c 4190 int key;
84fb1139 4191
a6085637
KH
4192 /* Note that it is okay to call x_window_to_frame, even though we are
4193 not running in the main lisp thread, because frame deletion
4194 requires the lisp thread to synchronize with this thread. Thus, if
4195 a frame struct is returned, it can be used without concern that the
4196 lisp thread might make it disappear while we are using it.
4197
4198 NB. Walking the frame list in this thread is safe (as long as
4199 writes of Lisp_Object slots are atomic, which they are on Windows).
4200 Although delete-frame can destructively modify the frame list while
4201 we are walking it, a garbage collection cannot occur until after
4202 delete-frame has synchronized with this thread.
4203
4204 It is also safe to use functions that make GDI calls, such as
fbd6baed 4205 w32_clear_rect, because these functions must obtain a DC handle
a6085637
KH
4206 from the frame struct using get_frame_dc which is thread-aware. */
4207
7d0393cf 4208 switch (msg)
ee78dc32
GV
4209 {
4210 case WM_ERASEBKGND:
a6085637
KH
4211 f = x_window_to_frame (dpyinfo, hwnd);
4212 if (f)
4213 {
9badad41 4214 HDC hdc = get_frame_dc (f);
a6085637 4215 GetUpdateRect (hwnd, &wmsg.rect, FALSE);
9badad41
JR
4216 w32_clear_rect (f, hdc, &wmsg.rect);
4217 release_frame_dc (f, hdc);
ce6059da
AI
4218
4219#if defined (W32_DEBUG_DISPLAY)
18f0b342
AI
4220 DebPrint (("WM_ERASEBKGND (frame %p): erasing %d,%d-%d,%d\n",
4221 f,
4222 wmsg.rect.left, wmsg.rect.top,
4223 wmsg.rect.right, wmsg.rect.bottom));
ce6059da 4224#endif /* W32_DEBUG_DISPLAY */
a6085637 4225 }
5ac45f98
GV
4226 return 1;
4227 case WM_PALETTECHANGED:
4228 /* ignore our own changes */
4229 if ((HWND)wParam != hwnd)
4230 {
a6085637
KH
4231 f = x_window_to_frame (dpyinfo, hwnd);
4232 if (f)
4233 /* get_frame_dc will realize our palette and force all
4234 frames to be redrawn if needed. */
4235 release_frame_dc (f, get_frame_dc (f));
5ac45f98
GV
4236 }
4237 return 0;
ee78dc32 4238 case WM_PAINT:
ce6059da 4239 {
55dcfc15
AI
4240 PAINTSTRUCT paintStruct;
4241 RECT update_rect;
aa35b6ad 4242 bzero (&update_rect, sizeof (update_rect));
55dcfc15 4243
18f0b342
AI
4244 f = x_window_to_frame (dpyinfo, hwnd);
4245 if (f == 0)
4246 {
4247 DebPrint (("WM_PAINT received for unknown window %p\n", hwnd));
4248 return 0;
4249 }
4250
55dcfc15
AI
4251 /* MSDN Docs say not to call BeginPaint if GetUpdateRect
4252 fails. Apparently this can happen under some
4253 circumstances. */
aa35b6ad 4254 if (GetUpdateRect (hwnd, &update_rect, FALSE) || !w32_strict_painting)
55dcfc15
AI
4255 {
4256 enter_crit ();
4257 BeginPaint (hwnd, &paintStruct);
4258
aa35b6ad
JR
4259 /* The rectangles returned by GetUpdateRect and BeginPaint
4260 do not always match. Play it safe by assuming both areas
4261 are invalid. */
4262 UnionRect (&(wmsg.rect), &update_rect, &(paintStruct.rcPaint));
55dcfc15
AI
4263
4264#if defined (W32_DEBUG_DISPLAY)
18f0b342
AI
4265 DebPrint (("WM_PAINT (frame %p): painting %d,%d-%d,%d\n",
4266 f,
4267 wmsg.rect.left, wmsg.rect.top,
4268 wmsg.rect.right, wmsg.rect.bottom));
4269 DebPrint ((" [update region is %d,%d-%d,%d]\n",
55dcfc15
AI
4270 update_rect.left, update_rect.top,
4271 update_rect.right, update_rect.bottom));
4272#endif
4273 EndPaint (hwnd, &paintStruct);
4274 leave_crit ();
4275
4276 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
7d0393cf 4277
55dcfc15
AI
4278 return 0;
4279 }
c0611964
AI
4280
4281 /* If GetUpdateRect returns 0 (meaning there is no update
4282 region), assume the whole window needs to be repainted. */
4283 GetClientRect(hwnd, &wmsg.rect);
4284 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4285 return 0;
ee78dc32 4286 }
a1a80b40 4287
ccc2d29c
GV
4288 case WM_INPUTLANGCHANGE:
4289 /* Inform lisp thread of keyboard layout changes. */
4290 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4291
4292 /* Clear dead keys in the keyboard state; for simplicity only
4293 preserve modifier key states. */
4294 {
4295 int i;
4296 BYTE keystate[256];
4297
4298 GetKeyboardState (keystate);
4299 for (i = 0; i < 256; i++)
4300 if (1
4301 && i != VK_SHIFT
4302 && i != VK_LSHIFT
4303 && i != VK_RSHIFT
4304 && i != VK_CAPITAL
4305 && i != VK_NUMLOCK
4306 && i != VK_SCROLL
4307 && i != VK_CONTROL
4308 && i != VK_LCONTROL
4309 && i != VK_RCONTROL
4310 && i != VK_MENU
4311 && i != VK_LMENU
4312 && i != VK_RMENU
4313 && i != VK_LWIN
4314 && i != VK_RWIN)
4315 keystate[i] = 0;
4316 SetKeyboardState (keystate);
4317 }
4318 goto dflt;
4319
4320 case WM_HOTKEY:
4321 /* Synchronize hot keys with normal input. */
4322 PostMessage (hwnd, WM_KEYDOWN, HIWORD (lParam), 0);
4323 return (0);
4324
a1a80b40
GV
4325 case WM_KEYUP:
4326 case WM_SYSKEYUP:
4327 record_keyup (wParam, lParam);
4328 goto dflt;
4329
ee78dc32
GV
4330 case WM_KEYDOWN:
4331 case WM_SYSKEYDOWN:
ccc2d29c
GV
4332 /* Ignore keystrokes we fake ourself; see below. */
4333 if (dpyinfo->faked_key == wParam)
4334 {
4335 dpyinfo->faked_key = 0;
576ba81c
AI
4336 /* Make sure TranslateMessage sees them though (as long as
4337 they don't produce WM_CHAR messages). This ensures that
4338 indicator lights are toggled promptly on Windows 9x, for
4339 example. */
4340 if (lispy_function_keys[wParam] != 0)
4341 {
4342 windows_translate = 1;
4343 goto translate;
4344 }
4345 return 0;
ccc2d29c
GV
4346 }
4347
7830e24b
RS
4348 /* Synchronize modifiers with current keystroke. */
4349 sync_modifiers ();
a1a80b40 4350 record_keydown (wParam, lParam);
ccc2d29c 4351 wParam = map_keypad_keys (wParam, (lParam & 0x1000000L) != 0);
84fb1139
KH
4352
4353 windows_translate = 0;
ccc2d29c
GV
4354
4355 switch (wParam)
4356 {
4357 case VK_LWIN:
4358 if (NILP (Vw32_pass_lwindow_to_system))
4359 {
4360 /* Prevent system from acting on keyup (which opens the
4361 Start menu if no other key was pressed) by simulating a
4362 press of Space which we will ignore. */
4363 if (GetAsyncKeyState (wParam) & 1)
4364 {
adcc3809 4365 if (NUMBERP (Vw32_phantom_key_code))
576ba81c 4366 key = XUINT (Vw32_phantom_key_code) & 255;
adcc3809 4367 else
576ba81c
AI
4368 key = VK_SPACE;
4369 dpyinfo->faked_key = key;
4370 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
ccc2d29c
GV
4371 }
4372 }
4373 if (!NILP (Vw32_lwindow_modifier))
4374 return 0;
4375 break;
4376 case VK_RWIN:
4377 if (NILP (Vw32_pass_rwindow_to_system))
4378 {
4379 if (GetAsyncKeyState (wParam) & 1)
4380 {
adcc3809 4381 if (NUMBERP (Vw32_phantom_key_code))
576ba81c 4382 key = XUINT (Vw32_phantom_key_code) & 255;
adcc3809 4383 else
576ba81c
AI
4384 key = VK_SPACE;
4385 dpyinfo->faked_key = key;
4386 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
ccc2d29c
GV
4387 }
4388 }
4389 if (!NILP (Vw32_rwindow_modifier))
4390 return 0;
4391 break;
576ba81c 4392 case VK_APPS:
ccc2d29c
GV
4393 if (!NILP (Vw32_apps_modifier))
4394 return 0;
4395 break;
4396 case VK_MENU:
7d0393cf 4397 if (NILP (Vw32_pass_alt_to_system))
adcc3809
GV
4398 /* Prevent DefWindowProc from activating the menu bar if an
4399 Alt key is pressed and released by itself. */
ccc2d29c 4400 return 0;
84fb1139 4401 windows_translate = 1;
ccc2d29c 4402 break;
7d0393cf 4403 case VK_CAPITAL:
ccc2d29c
GV
4404 /* Decide whether to treat as modifier or function key. */
4405 if (NILP (Vw32_enable_caps_lock))
4406 goto disable_lock_key;
adcc3809
GV
4407 windows_translate = 1;
4408 break;
ccc2d29c
GV
4409 case VK_NUMLOCK:
4410 /* Decide whether to treat as modifier or function key. */
4411 if (NILP (Vw32_enable_num_lock))
4412 goto disable_lock_key;
adcc3809
GV
4413 windows_translate = 1;
4414 break;
ccc2d29c
GV
4415 case VK_SCROLL:
4416 /* Decide whether to treat as modifier or function key. */
4417 if (NILP (Vw32_scroll_lock_modifier))
4418 goto disable_lock_key;
adcc3809
GV
4419 windows_translate = 1;
4420 break;
ccc2d29c 4421 disable_lock_key:
adcc3809
GV
4422 /* Ensure the appropriate lock key state (and indicator light)
4423 remains in the same state. We do this by faking another
4424 press of the relevant key. Apparently, this really is the
4425 only way to toggle the state of the indicator lights. */
4426 dpyinfo->faked_key = wParam;
4427 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
4428 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
4429 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
4430 KEYEVENTF_EXTENDEDKEY | 0, 0);
4431 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
4432 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
4433 /* Ensure indicator lights are updated promptly on Windows 9x
4434 (TranslateMessage apparently does this), after forwarding
4435 input event. */
4436 post_character_message (hwnd, msg, wParam, lParam,
4437 w32_get_key_modifiers (wParam, lParam));
4438 windows_translate = 1;
ccc2d29c 4439 break;
7d0393cf 4440 case VK_CONTROL:
ccc2d29c
GV
4441 case VK_SHIFT:
4442 case VK_PROCESSKEY: /* Generated by IME. */
4443 windows_translate = 1;
4444 break;
adcc3809
GV
4445 case VK_CANCEL:
4446 /* Windows maps Ctrl-Pause (aka Ctrl-Break) into VK_CANCEL,
4447 which is confusing for purposes of key binding; convert
4448 VK_CANCEL events into VK_PAUSE events. */
4449 wParam = VK_PAUSE;
4450 break;
4451 case VK_PAUSE:
4452 /* Windows maps Ctrl-NumLock into VK_PAUSE, which is confusing
4453 for purposes of key binding; convert these back into
4454 VK_NUMLOCK events, at least when we want to see NumLock key
4455 presses. (Note that there is never any possibility that
4456 VK_PAUSE with Ctrl really is C-Pause as per above.) */
4457 if (NILP (Vw32_enable_num_lock) && modifier_set (VK_CONTROL))
4458 wParam = VK_NUMLOCK;
4459 break;
ccc2d29c
GV
4460 default:
4461 /* If not defined as a function key, change it to a WM_CHAR message. */
4462 if (lispy_function_keys[wParam] == 0)
4463 {
adcc3809
GV
4464 DWORD modifiers = construct_console_modifiers ();
4465
ccc2d29c
GV
4466 if (!NILP (Vw32_recognize_altgr)
4467 && modifier_set (VK_LCONTROL) && modifier_set (VK_RMENU))
4468 {
4469 /* Always let TranslateMessage handle AltGr key chords;
4470 for some reason, ToAscii doesn't always process AltGr
4471 chords correctly. */
4472 windows_translate = 1;
4473 }
adcc3809 4474 else if ((modifiers & (~SHIFT_PRESSED & ~CAPSLOCK_ON)) != 0)
ccc2d29c 4475 {
adcc3809
GV
4476 /* Handle key chords including any modifiers other
4477 than shift directly, in order to preserve as much
4478 modifier information as possible. */
ccc2d29c
GV
4479 if ('A' <= wParam && wParam <= 'Z')
4480 {
4481 /* Don't translate modified alphabetic keystrokes,
4482 so the user doesn't need to constantly switch
4483 layout to type control or meta keystrokes when
4484 the normal layout translates alphabetic
4485 characters to non-ascii characters. */
4486 if (!modifier_set (VK_SHIFT))
4487 wParam += ('a' - 'A');
4488 msg = WM_CHAR;
4489 }
4490 else
4491 {
4492 /* Try to handle other keystrokes by determining the
4493 base character (ie. translating the base key plus
4494 shift modifier). */
4495 int add;
4496 int isdead = 0;
4497 KEY_EVENT_RECORD key;
7d0393cf 4498
ccc2d29c
GV
4499 key.bKeyDown = TRUE;
4500 key.wRepeatCount = 1;
4501 key.wVirtualKeyCode = wParam;
4502 key.wVirtualScanCode = (lParam & 0xFF0000) >> 16;
4503 key.uChar.AsciiChar = 0;
adcc3809 4504 key.dwControlKeyState = modifiers;
ccc2d29c
GV
4505
4506 add = w32_kbd_patch_key (&key);
4507 /* 0 means an unrecognised keycode, negative means
4508 dead key. Ignore both. */
4509 while (--add >= 0)
4510 {
4511 /* Forward asciified character sequence. */
4512 post_character_message
4513 (hwnd, WM_CHAR, key.uChar.AsciiChar, lParam,
4514 w32_get_key_modifiers (wParam, lParam));
4515 w32_kbd_patch_key (&key);
4516 }
4517 return 0;
4518 }
4519 }
4520 else
4521 {
4522 /* Let TranslateMessage handle everything else. */
4523 windows_translate = 1;
4524 }
4525 }
4526 }
a1a80b40 4527
adcc3809 4528 translate:
84fb1139
KH
4529 if (windows_translate)
4530 {
e9e23e23 4531 MSG windows_msg = { hwnd, msg, wParam, lParam, 0, {0,0} };
84fb1139 4532
e9e23e23
GV
4533 windows_msg.time = GetMessageTime ();
4534 TranslateMessage (&windows_msg);
84fb1139
KH
4535 goto dflt;
4536 }
4537
ee78dc32 4538 /* Fall through */
7d0393cf 4539
ee78dc32
GV
4540 case WM_SYSCHAR:
4541 case WM_CHAR:
ccc2d29c
GV
4542 post_character_message (hwnd, msg, wParam, lParam,
4543 w32_get_key_modifiers (wParam, lParam));
ee78dc32 4544 break;
da36a4d6 4545
5ac45f98
GV
4546 /* Simulate middle mouse button events when left and right buttons
4547 are used together, but only if user has two button mouse. */
ee78dc32 4548 case WM_LBUTTONDOWN:
5ac45f98 4549 case WM_RBUTTONDOWN:
7ce9aaca 4550 if (XINT (Vw32_num_mouse_buttons) > 2)
5ac45f98
GV
4551 goto handle_plain_button;
4552
4553 {
4554 int this = (msg == WM_LBUTTONDOWN) ? LMOUSE : RMOUSE;
4555 int other = (msg == WM_LBUTTONDOWN) ? RMOUSE : LMOUSE;
4556
3cb20f4a
RS
4557 if (button_state & this)
4558 return 0;
5ac45f98
GV
4559
4560 if (button_state == 0)
4561 SetCapture (hwnd);
4562
4563 button_state |= this;
4564
4565 if (button_state & other)
4566 {
84fb1139 4567 if (mouse_button_timer)
5ac45f98 4568 {
84fb1139
KH
4569 KillTimer (hwnd, mouse_button_timer);
4570 mouse_button_timer = 0;
5ac45f98
GV
4571
4572 /* Generate middle mouse event instead. */
4573 msg = WM_MBUTTONDOWN;
4574 button_state |= MMOUSE;
4575 }
4576 else if (button_state & MMOUSE)
4577 {
4578 /* Ignore button event if we've already generated a
4579 middle mouse down event. This happens if the
4580 user releases and press one of the two buttons
4581 after we've faked a middle mouse event. */
4582 return 0;
4583 }
4584 else
4585 {
4586 /* Flush out saved message. */
84fb1139 4587 post_msg (&saved_mouse_button_msg);
5ac45f98 4588 }
fbd6baed 4589 wmsg.dwModifiers = w32_get_modifiers ();
5ac45f98
GV
4590 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4591
4592 /* Clear message buffer. */
84fb1139 4593 saved_mouse_button_msg.msg.hwnd = 0;
5ac45f98
GV
4594 }
4595 else
4596 {
4597 /* Hold onto message for now. */
84fb1139 4598 mouse_button_timer =
adcc3809
GV
4599 SetTimer (hwnd, MOUSE_BUTTON_ID,
4600 XINT (Vw32_mouse_button_tolerance), NULL);
84fb1139
KH
4601 saved_mouse_button_msg.msg.hwnd = hwnd;
4602 saved_mouse_button_msg.msg.message = msg;
4603 saved_mouse_button_msg.msg.wParam = wParam;
4604 saved_mouse_button_msg.msg.lParam = lParam;
4605 saved_mouse_button_msg.msg.time = GetMessageTime ();
fbd6baed 4606 saved_mouse_button_msg.dwModifiers = w32_get_modifiers ();
5ac45f98
GV
4607 }
4608 }
4609 return 0;
4610
ee78dc32 4611 case WM_LBUTTONUP:
5ac45f98 4612 case WM_RBUTTONUP:
7ce9aaca 4613 if (XINT (Vw32_num_mouse_buttons) > 2)
5ac45f98
GV
4614 goto handle_plain_button;
4615
4616 {
4617 int this = (msg == WM_LBUTTONUP) ? LMOUSE : RMOUSE;
4618 int other = (msg == WM_LBUTTONUP) ? RMOUSE : LMOUSE;
4619
3cb20f4a
RS
4620 if ((button_state & this) == 0)
4621 return 0;
5ac45f98
GV
4622
4623 button_state &= ~this;
4624
4625 if (button_state & MMOUSE)
4626 {
4627 /* Only generate event when second button is released. */
4628 if ((button_state & other) == 0)
4629 {
4630 msg = WM_MBUTTONUP;
4631 button_state &= ~MMOUSE;
4632
4633 if (button_state) abort ();
4634 }
4635 else
4636 return 0;
4637 }
4638 else
4639 {
4640 /* Flush out saved message if necessary. */
84fb1139 4641 if (saved_mouse_button_msg.msg.hwnd)
5ac45f98 4642 {
84fb1139 4643 post_msg (&saved_mouse_button_msg);
5ac45f98
GV
4644 }
4645 }
fbd6baed 4646 wmsg.dwModifiers = w32_get_modifiers ();
5ac45f98
GV
4647 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4648
4649 /* Always clear message buffer and cancel timer. */
84fb1139
KH
4650 saved_mouse_button_msg.msg.hwnd = 0;
4651 KillTimer (hwnd, mouse_button_timer);
4652 mouse_button_timer = 0;
5ac45f98
GV
4653
4654 if (button_state == 0)
4655 ReleaseCapture ();
4656 }
4657 return 0;
4658
74214547
JR
4659 case WM_XBUTTONDOWN:
4660 case WM_XBUTTONUP:
4661 if (w32_pass_extra_mouse_buttons_to_system)
4662 goto dflt;
4663 /* else fall through and process them. */
ee78dc32
GV
4664 case WM_MBUTTONDOWN:
4665 case WM_MBUTTONUP:
5ac45f98 4666 handle_plain_button:
ee78dc32
GV
4667 {
4668 BOOL up;
1edf84e7 4669 int button;
ee78dc32 4670
74214547 4671 if (parse_button (msg, HIWORD (wParam), &button, &up))
ee78dc32
GV
4672 {
4673 if (up) ReleaseCapture ();
4674 else SetCapture (hwnd);
7d0393cf 4675 button = (button == 0) ? LMOUSE :
1edf84e7
GV
4676 ((button == 1) ? MMOUSE : RMOUSE);
4677 if (up)
4678 button_state &= ~button;
4679 else
4680 button_state |= button;
ee78dc32
GV
4681 }
4682 }
7d0393cf 4683
fbd6baed 4684 wmsg.dwModifiers = w32_get_modifiers ();
ee78dc32 4685 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
74214547
JR
4686
4687 /* Need to return true for XBUTTON messages, false for others,
4688 to indicate that we processed the message. */
4689 return (msg == WM_XBUTTONDOWN || msg == WM_XBUTTONUP);
5ac45f98 4690
5ac45f98 4691 case WM_MOUSEMOVE:
9eb16b62
JR
4692 /* If the mouse has just moved into the frame, start tracking
4693 it, so we will be notified when it leaves the frame. Mouse
4694 tracking only works under W98 and NT4 and later. On earlier
4695 versions, there is no way of telling when the mouse leaves the
4696 frame, so we just have to put up with help-echo and mouse
4697 highlighting remaining while the frame is not active. */
4698 if (track_mouse_event_fn && !track_mouse_window)
4699 {
4700 TRACKMOUSEEVENT tme;
4701 tme.cbSize = sizeof (tme);
4702 tme.dwFlags = TME_LEAVE;
4703 tme.hwndTrack = hwnd;
4704
4705 track_mouse_event_fn (&tme);
4706 track_mouse_window = hwnd;
4707 }
4708 case WM_VSCROLL:
fbd6baed 4709 if (XINT (Vw32_mouse_move_interval) <= 0
84fb1139
KH
4710 || (msg == WM_MOUSEMOVE && button_state == 0))
4711 {
fbd6baed 4712 wmsg.dwModifiers = w32_get_modifiers ();
84fb1139
KH
4713 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4714 return 0;
4715 }
7d0393cf 4716
84fb1139
KH
4717 /* Hang onto mouse move and scroll messages for a bit, to avoid
4718 sending such events to Emacs faster than it can process them.
4719 If we get more events before the timer from the first message
4720 expires, we just replace the first message. */
4721
4722 if (saved_mouse_move_msg.msg.hwnd == 0)
4723 mouse_move_timer =
adcc3809
GV
4724 SetTimer (hwnd, MOUSE_MOVE_ID,
4725 XINT (Vw32_mouse_move_interval), NULL);
84fb1139
KH
4726
4727 /* Hold onto message for now. */
4728 saved_mouse_move_msg.msg.hwnd = hwnd;
4729 saved_mouse_move_msg.msg.message = msg;
4730 saved_mouse_move_msg.msg.wParam = wParam;
4731 saved_mouse_move_msg.msg.lParam = lParam;
4732 saved_mouse_move_msg.msg.time = GetMessageTime ();
fbd6baed 4733 saved_mouse_move_msg.dwModifiers = w32_get_modifiers ();
7d0393cf 4734
84fb1139
KH
4735 return 0;
4736
1edf84e7
GV
4737 case WM_MOUSEWHEEL:
4738 wmsg.dwModifiers = w32_get_modifiers ();
4739 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4740 return 0;
4741
cb9e33d4
RS
4742 case WM_DROPFILES:
4743 wmsg.dwModifiers = w32_get_modifiers ();
4744 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4745 return 0;
4746
84fb1139
KH
4747 case WM_TIMER:
4748 /* Flush out saved messages if necessary. */
4749 if (wParam == mouse_button_timer)
5ac45f98 4750 {
84fb1139
KH
4751 if (saved_mouse_button_msg.msg.hwnd)
4752 {
4753 post_msg (&saved_mouse_button_msg);
4754 saved_mouse_button_msg.msg.hwnd = 0;
4755 }
4756 KillTimer (hwnd, mouse_button_timer);
4757 mouse_button_timer = 0;
4758 }
4759 else if (wParam == mouse_move_timer)
4760 {
4761 if (saved_mouse_move_msg.msg.hwnd)
4762 {
4763 post_msg (&saved_mouse_move_msg);
4764 saved_mouse_move_msg.msg.hwnd = 0;
4765 }
4766 KillTimer (hwnd, mouse_move_timer);
4767 mouse_move_timer = 0;
5ac45f98 4768 }
48094ace
JR
4769 else if (wParam == menu_free_timer)
4770 {
4771 KillTimer (hwnd, menu_free_timer);
4772 menu_free_timer = 0;
27605fa7 4773 f = x_window_to_frame (dpyinfo, hwnd);
48094ace
JR
4774 if (!f->output_data.w32->menu_command_in_progress)
4775 {
4776 /* Free memory used by owner-drawn and help-echo strings. */
4777 w32_free_menu_strings (hwnd);
4778 f->output_data.w32->menubar_active = 0;
4779 }
4780 }
5ac45f98 4781 return 0;
7d0393cf 4782
84fb1139
KH
4783 case WM_NCACTIVATE:
4784 /* Windows doesn't send us focus messages when putting up and
e9e23e23 4785 taking down a system popup dialog as for Ctrl-Alt-Del on Windows 95.
84fb1139
KH
4786 The only indication we get that something happened is receiving
4787 this message afterwards. So this is a good time to reset our
4788 keyboard modifiers' state. */
4789 reset_modifiers ();
4790 goto dflt;
da36a4d6 4791
1edf84e7 4792 case WM_INITMENU:
487163ac
AI
4793 button_state = 0;
4794 ReleaseCapture ();
1edf84e7
GV
4795 /* We must ensure menu bar is fully constructed and up to date
4796 before allowing user interaction with it. To achieve this
4797 we send this message to the lisp thread and wait for a
4798 reply (whose value is not actually needed) to indicate that
4799 the menu bar is now ready for use, so we can now return.
4800
4801 To remain responsive in the meantime, we enter a nested message
4802 loop that can process all other messages.
4803
4804 However, we skip all this if the message results from calling
4805 TrackPopupMenu - in fact, we must NOT attempt to send the lisp
4806 thread a message because it is blocked on us at this point. We
4807 set menubar_active before calling TrackPopupMenu to indicate
4808 this (there is no possibility of confusion with real menubar
4809 being active). */
4810
4811 f = x_window_to_frame (dpyinfo, hwnd);
4812 if (f
4813 && (f->output_data.w32->menubar_active
4814 /* We can receive this message even in the absence of a
4815 menubar (ie. when the system menu is activated) - in this
4816 case we do NOT want to forward the message, otherwise it
4817 will cause the menubar to suddenly appear when the user
4818 had requested it to be turned off! */
4819 || f->output_data.w32->menubar_widget == NULL))
4820 return 0;
4821
4822 {
4823 deferred_msg msg_buf;
4824
4825 /* Detect if message has already been deferred; in this case
4826 we cannot return any sensible value to ignore this. */
4827 if (find_deferred_msg (hwnd, msg) != NULL)
4828 abort ();
4829
4830 return send_deferred_msg (&msg_buf, hwnd, msg, wParam, lParam);
4831 }
4832
4833 case WM_EXITMENULOOP:
4834 f = x_window_to_frame (dpyinfo, hwnd);
4835
48094ace
JR
4836 /* If a menu command is not already in progress, check again
4837 after a short delay, since Windows often (always?) sends the
4838 WM_EXITMENULOOP before the corresponding WM_COMMAND message. */
4839 if (f && !f->output_data.w32->menu_command_in_progress)
4840 menu_free_timer = SetTimer (hwnd, MENU_FREE_ID, MENU_FREE_DELAY, NULL);
1edf84e7
GV
4841 goto dflt;
4842
126f2e35 4843 case WM_MENUSELECT:
4e3a1c61
JR
4844 /* Direct handling of help_echo in menus. Should be safe now
4845 that we generate the help_echo by placing a help event in the
4846 keyboard buffer. */
ca56d953 4847 {
ca56d953
JR
4848 HMENU menu = (HMENU) lParam;
4849 UINT menu_item = (UINT) LOWORD (wParam);
4850 UINT flags = (UINT) HIWORD (wParam);
4851
4e3a1c61 4852 w32_menu_display_help (hwnd, menu, menu_item, flags);
ca56d953 4853 }
126f2e35
JR
4854 return 0;
4855
87996783
GV
4856 case WM_MEASUREITEM:
4857 f = x_window_to_frame (dpyinfo, hwnd);
4858 if (f)
4859 {
4860 MEASUREITEMSTRUCT * pMis = (MEASUREITEMSTRUCT *) lParam;
4861
4862 if (pMis->CtlType == ODT_MENU)
4863 {
4864 /* Work out dimensions for popup menu titles. */
4865 char * title = (char *) pMis->itemData;
4866 HDC hdc = GetDC (hwnd);
4867 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
4868 LOGFONT menu_logfont;
4869 HFONT old_font;
4870 SIZE size;
4871
4872 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
4873 menu_logfont.lfWeight = FW_BOLD;
4874 menu_font = CreateFontIndirect (&menu_logfont);
4875 old_font = SelectObject (hdc, menu_font);
4876
dfff8a69
JR
4877 pMis->itemHeight = GetSystemMetrics (SM_CYMENUSIZE);
4878 if (title)
4879 {
4880 GetTextExtentPoint32 (hdc, title, strlen (title), &size);
4881 pMis->itemWidth = size.cx;
4882 if (pMis->itemHeight < size.cy)
4883 pMis->itemHeight = size.cy;
4884 }
4885 else
4886 pMis->itemWidth = 0;
87996783
GV
4887
4888 SelectObject (hdc, old_font);
4889 DeleteObject (menu_font);
4890 ReleaseDC (hwnd, hdc);
4891 return TRUE;
4892 }
4893 }
4894 return 0;
4895
4896 case WM_DRAWITEM:
4897 f = x_window_to_frame (dpyinfo, hwnd);
4898 if (f)
4899 {
4900 DRAWITEMSTRUCT * pDis = (DRAWITEMSTRUCT *) lParam;
4901
4902 if (pDis->CtlType == ODT_MENU)
4903 {
4904 /* Draw popup menu title. */
4905 char * title = (char *) pDis->itemData;
212da13b
JR
4906 if (title)
4907 {
4908 HDC hdc = pDis->hDC;
4909 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
4910 LOGFONT menu_logfont;
4911 HFONT old_font;
4912
4913 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
4914 menu_logfont.lfWeight = FW_BOLD;
4915 menu_font = CreateFontIndirect (&menu_logfont);
4916 old_font = SelectObject (hdc, menu_font);
4917
4918 /* Always draw title as if not selected. */
4919 ExtTextOut (hdc,
4920 pDis->rcItem.left
4921 + GetSystemMetrics (SM_CXMENUCHECK),
4922 pDis->rcItem.top,
4923 ETO_OPAQUE, &pDis->rcItem,
4924 title, strlen (title), NULL);
4925
4926 SelectObject (hdc, old_font);
4927 DeleteObject (menu_font);
4928 }
87996783
GV
4929 return TRUE;
4930 }
4931 }
4932 return 0;
4933
1edf84e7
GV
4934#if 0
4935 /* Still not right - can't distinguish between clicks in the
4936 client area of the frame from clicks forwarded from the scroll
4937 bars - may have to hook WM_NCHITTEST to remember the mouse
4938 position and then check if it is in the client area ourselves. */
4939 case WM_MOUSEACTIVATE:
4940 /* Discard the mouse click that activates a frame, allowing the
4941 user to click anywhere without changing point (or worse!).
4942 Don't eat mouse clicks on scrollbars though!! */
4943 if (LOWORD (lParam) == HTCLIENT )
4944 return MA_ACTIVATEANDEAT;
4945 goto dflt;
4946#endif
4947
9eb16b62
JR
4948 case WM_MOUSELEAVE:
4949 /* No longer tracking mouse. */
4950 track_mouse_window = NULL;
4951
1edf84e7 4952 case WM_ACTIVATEAPP:
ccc2d29c 4953 case WM_ACTIVATE:
1edf84e7
GV
4954 case WM_WINDOWPOSCHANGED:
4955 case WM_SHOWWINDOW:
4956 /* Inform lisp thread that a frame might have just been obscured
4957 or exposed, so should recheck visibility of all frames. */
4958 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4959 goto dflt;
4960
da36a4d6 4961 case WM_SETFOCUS:
adcc3809
GV
4962 dpyinfo->faked_key = 0;
4963 reset_modifiers ();
ccc2d29c
GV
4964 register_hot_keys (hwnd);
4965 goto command;
8681157a 4966 case WM_KILLFOCUS:
ccc2d29c 4967 unregister_hot_keys (hwnd);
487163ac
AI
4968 button_state = 0;
4969 ReleaseCapture ();
65906840
JR
4970 /* Relinquish the system caret. */
4971 if (w32_system_caret_hwnd)
4972 {
93f2ca61 4973 w32_visible_system_caret_hwnd = NULL;
d285988b
JR
4974 w32_system_caret_hwnd = NULL;
4975 DestroyCaret ();
65906840 4976 }
48094ace
JR
4977 goto command;
4978 case WM_COMMAND:
4979 f = x_window_to_frame (dpyinfo, hwnd);
4980 if (f && HIWORD (wParam) == 0)
4981 {
4982 f->output_data.w32->menu_command_in_progress = 1;
4983 if (menu_free_timer)
4984 {
4985 KillTimer (hwnd, menu_free_timer);
7d0393cf 4986 menu_free_timer = 0;
48094ace
JR
4987 }
4988 }
ee78dc32
GV
4989 case WM_MOVE:
4990 case WM_SIZE:
ccc2d29c 4991 command:
fbd6baed 4992 wmsg.dwModifiers = w32_get_modifiers ();
ee78dc32
GV
4993 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4994 goto dflt;
8847d890
RS
4995
4996 case WM_CLOSE:
fbd6baed 4997 wmsg.dwModifiers = w32_get_modifiers ();
8847d890
RS
4998 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4999 return 0;
5000
ee78dc32 5001 case WM_WINDOWPOSCHANGING:
bfd6edcc
JR
5002 /* Don't restrict the sizing of tip frames. */
5003 if (hwnd == tip_window)
5004 return 0;
ee78dc32
GV
5005 {
5006 WINDOWPLACEMENT wp;
5007 LPWINDOWPOS lppos = (WINDOWPOS *) lParam;
1edf84e7
GV
5008
5009 wp.length = sizeof (WINDOWPLACEMENT);
ee78dc32 5010 GetWindowPlacement (hwnd, &wp);
7d0393cf 5011
1edf84e7 5012 if (wp.showCmd != SW_SHOWMINIMIZED && (lppos->flags & SWP_NOSIZE) == 0)
ee78dc32
GV
5013 {
5014 RECT rect;
5015 int wdiff;
5016 int hdiff;
1edf84e7
GV
5017 DWORD font_width;
5018 DWORD line_height;
5019 DWORD internal_border;
5020 DWORD scrollbar_extra;
ee78dc32 5021 RECT wr;
7d0393cf 5022
5ac45f98 5023 wp.length = sizeof(wp);
ee78dc32 5024 GetWindowRect (hwnd, &wr);
7d0393cf 5025
3c190163 5026 enter_crit ();
7d0393cf 5027
1edf84e7
GV
5028 font_width = GetWindowLong (hwnd, WND_FONTWIDTH_INDEX);
5029 line_height = GetWindowLong (hwnd, WND_LINEHEIGHT_INDEX);
5030 internal_border = GetWindowLong (hwnd, WND_BORDER_INDEX);
5031 scrollbar_extra = GetWindowLong (hwnd, WND_SCROLLBAR_INDEX);
7d0393cf 5032
3c190163 5033 leave_crit ();
7d0393cf 5034
ee78dc32 5035 memset (&rect, 0, sizeof (rect));
7d0393cf 5036 AdjustWindowRect (&rect, GetWindowLong (hwnd, GWL_STYLE),
ee78dc32
GV
5037 GetMenu (hwnd) != NULL);
5038
1edf84e7
GV
5039 /* Force width and height of client area to be exact
5040 multiples of the character cell dimensions. */
5041 wdiff = (lppos->cx - (rect.right - rect.left)
5042 - 2 * internal_border - scrollbar_extra)
5043 % font_width;
5044 hdiff = (lppos->cy - (rect.bottom - rect.top)
5045 - 2 * internal_border)
5046 % line_height;
7d0393cf 5047
ee78dc32
GV
5048 if (wdiff || hdiff)
5049 {
7d0393cf
JB
5050 /* For right/bottom sizing we can just fix the sizes.
5051 However for top/left sizing we will need to fix the X
ee78dc32 5052 and Y positions as well. */
7d0393cf 5053
ee78dc32
GV
5054 lppos->cx -= wdiff;
5055 lppos->cy -= hdiff;
7d0393cf
JB
5056
5057 if (wp.showCmd != SW_SHOWMAXIMIZED
1edf84e7 5058 && (lppos->flags & SWP_NOMOVE) == 0)
ee78dc32
GV
5059 {
5060 if (lppos->x != wr.left || lppos->y != wr.top)
5061 {
5062 lppos->x += wdiff;
5063 lppos->y += hdiff;
5064 }
5065 else
5066 {
5067 lppos->flags |= SWP_NOMOVE;
5068 }
5069 }
7d0393cf 5070
1edf84e7 5071 return 0;
ee78dc32
GV
5072 }
5073 }
5074 }
7d0393cf 5075
ee78dc32 5076 goto dflt;
1edf84e7 5077
b1f918f8
GV
5078 case WM_GETMINMAXINFO:
5079 /* Hack to correct bug that allows Emacs frames to be resized
5080 below the Minimum Tracking Size. */
5081 ((LPMINMAXINFO) lParam)->ptMinTrackSize.y++;
bf853fee
AI
5082 /* Hack to allow resizing the Emacs frame above the screen size.
5083 Note that Windows 9x limits coordinates to 16-bits. */
5084 ((LPMINMAXINFO) lParam)->ptMaxTrackSize.x = 32767;
5085 ((LPMINMAXINFO) lParam)->ptMaxTrackSize.y = 32767;
b1f918f8
GV
5086 return 0;
5087
c9b2104d
JR
5088 case WM_SETCURSOR:
5089 if (LOWORD (lParam) == HTCLIENT)
5090 return 0;
5091
5092 goto dflt;
5093
5094 case WM_EMACS_SETCURSOR:
5095 {
5096 Cursor cursor = (Cursor) wParam;
5097 if (cursor)
5098 SetCursor (cursor);
5099 return 0;
5100 }
5101
1edf84e7
GV
5102 case WM_EMACS_CREATESCROLLBAR:
5103 return (LRESULT) w32_createscrollbar ((struct frame *) wParam,
5104 (struct scroll_bar *) lParam);
5105
5ac45f98 5106 case WM_EMACS_SHOWWINDOW:
1edf84e7
GV
5107 return ShowWindow ((HWND) wParam, (WPARAM) lParam);
5108
dfdb4047 5109 case WM_EMACS_SETFOREGROUND:
ce6059da
AI
5110 {
5111 HWND foreground_window;
5112 DWORD foreground_thread, retval;
5113
5114 /* On NT 5.0, and apparently Windows 98, it is necessary to
5115 attach to the thread that currently has focus in order to
5116 pull the focus away from it. */
5117 foreground_window = GetForegroundWindow ();
5118 foreground_thread = GetWindowThreadProcessId (foreground_window, NULL);
5119 if (!foreground_window
5120 || foreground_thread == GetCurrentThreadId ()
5121 || !AttachThreadInput (GetCurrentThreadId (),
5122 foreground_thread, TRUE))
5123 foreground_thread = 0;
5124
5125 retval = SetForegroundWindow ((HWND) wParam);
5126
5127 /* Detach from the previous foreground thread. */
5128 if (foreground_thread)
5129 AttachThreadInput (GetCurrentThreadId (),
5130 foreground_thread, FALSE);
5131
5132 return retval;
5133 }
dfdb4047 5134
5ac45f98
GV
5135 case WM_EMACS_SETWINDOWPOS:
5136 {
1edf84e7
GV
5137 WINDOWPOS * pos = (WINDOWPOS *) wParam;
5138 return SetWindowPos (hwnd, pos->hwndInsertAfter,
5ac45f98
GV
5139 pos->x, pos->y, pos->cx, pos->cy, pos->flags);
5140 }
1edf84e7 5141
ee78dc32 5142 case WM_EMACS_DESTROYWINDOW:
cb9e33d4 5143 DragAcceptFiles ((HWND) wParam, FALSE);
1edf84e7
GV
5144 return DestroyWindow ((HWND) wParam);
5145
93f2ca61
JR
5146 case WM_EMACS_HIDE_CARET:
5147 return HideCaret (hwnd);
5148
5149 case WM_EMACS_SHOW_CARET:
5150 return ShowCaret (hwnd);
5151
65906840
JR
5152 case WM_EMACS_DESTROY_CARET:
5153 w32_system_caret_hwnd = NULL;
93f2ca61 5154 w32_visible_system_caret_hwnd = NULL;
65906840
JR
5155 return DestroyCaret ();
5156
5157 case WM_EMACS_TRACK_CARET:
5158 /* If there is currently no system caret, create one. */
5159 if (w32_system_caret_hwnd == NULL)
5160 {
93f2ca61
JR
5161 /* Use the default caret width, and avoid changing it
5162 unneccesarily, as it confuses screen reader software. */
65906840 5163 w32_system_caret_hwnd = hwnd;
93f2ca61 5164 CreateCaret (hwnd, NULL, 0,
65906840
JR
5165 w32_system_caret_height);
5166 }
7d0393cf 5167
93f2ca61
JR
5168 if (!SetCaretPos (w32_system_caret_x, w32_system_caret_y))
5169 return 0;
5170 /* Ensure visible caret gets turned on when requested. */
5171 else if (w32_use_visible_system_caret
5172 && w32_visible_system_caret_hwnd != hwnd)
5173 {
5174 w32_visible_system_caret_hwnd = hwnd;
5175 return ShowCaret (hwnd);
5176 }
5177 /* Ensure visible caret gets turned off when requested. */
5178 else if (!w32_use_visible_system_caret
5179 && w32_visible_system_caret_hwnd)
5180 {
5181 w32_visible_system_caret_hwnd = NULL;
5182 return HideCaret (hwnd);
5183 }
5184 else
5185 return 1;
65906840 5186
1edf84e7
GV
5187 case WM_EMACS_TRACKPOPUPMENU:
5188 {
5189 UINT flags;
5190 POINT *pos;
5191 int retval;
5192 pos = (POINT *)lParam;
5193 flags = TPM_CENTERALIGN;
5194 if (button_state & LMOUSE)
5195 flags |= TPM_LEFTBUTTON;
5196 else if (button_state & RMOUSE)
5197 flags |= TPM_RIGHTBUTTON;
7d0393cf 5198
87996783
GV
5199 /* Remember we did a SetCapture on the initial mouse down event,
5200 so for safety, we make sure the capture is cancelled now. */
5201 ReleaseCapture ();
490822ff 5202 button_state = 0;
87996783 5203
1edf84e7
GV
5204 /* Use menubar_active to indicate that WM_INITMENU is from
5205 TrackPopupMenu below, and should be ignored. */
5206 f = x_window_to_frame (dpyinfo, hwnd);
5207 if (f)
5208 f->output_data.w32->menubar_active = 1;
7d0393cf
JB
5209
5210 if (TrackPopupMenu ((HMENU)wParam, flags, pos->x, pos->y,
1edf84e7
GV
5211 0, hwnd, NULL))
5212 {
5213 MSG amsg;
5214 /* Eat any mouse messages during popupmenu */
5215 while (PeekMessage (&amsg, hwnd, WM_MOUSEFIRST, WM_MOUSELAST,
5216 PM_REMOVE));
5217 /* Get the menu selection, if any */
5218 if (PeekMessage (&amsg, hwnd, WM_COMMAND, WM_COMMAND, PM_REMOVE))
5219 {
5220 retval = LOWORD (amsg.wParam);
5221 }
5222 else
5223 {
5224 retval = 0;
5225 }
1edf84e7
GV
5226 }
5227 else
5228 {
5229 retval = -1;
5230 }
5231
5232 return retval;
5233 }
5234
ee78dc32 5235 default:
93fbe8b7
GV
5236 /* Check for messages registered at runtime. */
5237 if (msg == msh_mousewheel)
5238 {
5239 wmsg.dwModifiers = w32_get_modifiers ();
5240 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
5241 return 0;
5242 }
7d0393cf 5243
ee78dc32
GV
5244 dflt:
5245 return DefWindowProc (hwnd, msg, wParam, lParam);
5246 }
7d0393cf 5247
1edf84e7
GV
5248
5249 /* The most common default return code for handled messages is 0. */
5250 return 0;
ee78dc32
GV
5251}
5252
7d0393cf 5253void
ee78dc32
GV
5254my_create_window (f)
5255 struct frame * f;
5256{
5257 MSG msg;
5258
1edf84e7
GV
5259 if (!PostThreadMessage (dwWindowsThreadId, WM_EMACS_CREATEWINDOW, (WPARAM)f, 0))
5260 abort ();
ee78dc32
GV
5261 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
5262}
5263
ca56d953
JR
5264
5265/* Create a tooltip window. Unlike my_create_window, we do not do this
5266 indirectly via the Window thread, as we do not need to process Window
5267 messages for the tooltip. Creating tooltips indirectly also creates
5268 deadlocks when tooltips are created for menu items. */
7d0393cf 5269void
ca56d953
JR
5270my_create_tip_window (f)
5271 struct frame *f;
5272{
bfd6edcc 5273 RECT rect;
ca56d953 5274
bfd6edcc
JR
5275 rect.left = rect.top = 0;
5276 rect.right = PIXEL_WIDTH (f);
5277 rect.bottom = PIXEL_HEIGHT (f);
5278
5279 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
5280 FRAME_EXTERNAL_MENU_BAR (f));
5281
5282 tip_window = FRAME_W32_WINDOW (f)
ca56d953
JR
5283 = CreateWindow (EMACS_CLASS,
5284 f->namebuf,
5285 f->output_data.w32->dwStyle,
5286 f->output_data.w32->left_pos,
5287 f->output_data.w32->top_pos,
bfd6edcc
JR
5288 rect.right - rect.left,
5289 rect.bottom - rect.top,
ca56d953
JR
5290 FRAME_W32_WINDOW (SELECTED_FRAME ()), /* owner */
5291 NULL,
5292 hinst,
5293 NULL);
5294
bfd6edcc 5295 if (tip_window)
ca56d953 5296 {
bfd6edcc
JR
5297 SetWindowLong (tip_window, WND_FONTWIDTH_INDEX, FONT_WIDTH (f->output_data.w32->font));
5298 SetWindowLong (tip_window, WND_LINEHEIGHT_INDEX, f->output_data.w32->line_height);
5299 SetWindowLong (tip_window, WND_BORDER_INDEX, f->output_data.w32->internal_border_width);
5300 SetWindowLong (tip_window, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
5301
5302 /* Tip frames have no scrollbars. */
5303 SetWindowLong (tip_window, WND_SCROLLBAR_INDEX, 0);
ca56d953
JR
5304
5305 /* Do this to discard the default setting specified by our parent. */
bfd6edcc 5306 ShowWindow (tip_window, SW_HIDE);
ca56d953
JR
5307 }
5308}
5309
5310
fbd6baed 5311/* Create and set up the w32 window for frame F. */
ee78dc32
GV
5312
5313static void
fbd6baed 5314w32_window (f, window_prompting, minibuffer_only)
ee78dc32
GV
5315 struct frame *f;
5316 long window_prompting;
5317 int minibuffer_only;
5318{
5319 BLOCK_INPUT;
5320
5321 /* Use the resource name as the top-level window name
5322 for looking up resources. Make a non-Lisp copy
5323 for the window manager, so GC relocation won't bother it.
5324
5325 Elsewhere we specify the window name for the window manager. */
7d0393cf 5326
ee78dc32 5327 {
d5db4077 5328 char *str = (char *) SDATA (Vx_resource_name);
ee78dc32
GV
5329 f->namebuf = (char *) xmalloc (strlen (str) + 1);
5330 strcpy (f->namebuf, str);
5331 }
5332
5333 my_create_window (f);
5334
5335 validate_x_resource_name ();
5336
5337 /* x_set_name normally ignores requests to set the name if the
5338 requested name is the same as the current name. This is the one
5339 place where that assumption isn't correct; f->name is set, but
5340 the server hasn't been told. */
5341 {
5342 Lisp_Object name;
5343 int explicit = f->explicit_name;
5344
5345 f->explicit_name = 0;
5346 name = f->name;
5347 f->name = Qnil;
5348 x_set_name (f, name, explicit);
5349 }
5350
5351 UNBLOCK_INPUT;
5352
5353 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
5354 initialize_frame_menubar (f);
5355
fbd6baed 5356 if (FRAME_W32_WINDOW (f) == 0)
ee78dc32
GV
5357 error ("Unable to create window");
5358}
5359
5360/* Handle the icon stuff for this window. Perhaps later we might
5361 want an x_set_icon_position which can be called interactively as
5362 well. */
5363
5364static void
5365x_icon (f, parms)
5366 struct frame *f;
5367 Lisp_Object parms;
5368{
5369 Lisp_Object icon_x, icon_y;
5370
e9e23e23 5371 /* Set the position of the icon. Note that Windows 95 groups all
ee78dc32 5372 icons in the tray. */
6fc2811b
JR
5373 icon_x = w32_get_arg (parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
5374 icon_y = w32_get_arg (parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
ee78dc32
GV
5375 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
5376 {
b7826503
PJ
5377 CHECK_NUMBER (icon_x);
5378 CHECK_NUMBER (icon_y);
ee78dc32
GV
5379 }
5380 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
5381 error ("Both left and top icon corners of icon must be specified");
5382
5383 BLOCK_INPUT;
5384
5385 if (! EQ (icon_x, Qunbound))
5386 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
5387
1edf84e7
GV
5388#if 0 /* TODO */
5389 /* Start up iconic or window? */
5390 x_wm_set_window_state
6fc2811b 5391 (f, (EQ (w32_get_arg (parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL), Qicon)
1edf84e7
GV
5392 ? IconicState
5393 : NormalState));
5394
d5db4077 5395 x_text_icon (f, (char *) SDATA ((!NILP (f->icon_name)
1edf84e7 5396 ? f->icon_name
d5db4077 5397 : f->name)));
1edf84e7
GV
5398#endif
5399
ee78dc32
GV
5400 UNBLOCK_INPUT;
5401}
5402
6fc2811b
JR
5403
5404static void
5405x_make_gc (f)
5406 struct frame *f;
5407{
5408 XGCValues gc_values;
5409
5410 BLOCK_INPUT;
5411
5412 /* Create the GC's of this frame.
5413 Note that many default values are used. */
5414
5415 /* Normal video */
5416 gc_values.font = f->output_data.w32->font;
5417
5418 /* Cursor has cursor-color background, background-color foreground. */
5419 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
5420 gc_values.background = f->output_data.w32->cursor_pixel;
5421 f->output_data.w32->cursor_gc
5422 = XCreateGC (NULL, FRAME_W32_WINDOW (f),
5423 (GCFont | GCForeground | GCBackground),
5424 &gc_values);
5425
5426 /* Reliefs. */
5427 f->output_data.w32->white_relief.gc = 0;
5428 f->output_data.w32->black_relief.gc = 0;
5429
5430 UNBLOCK_INPUT;
5431}
5432
5433
937e601e
AI
5434/* Handler for signals raised during x_create_frame and
5435 x_create_top_frame. FRAME is the frame which is partially
5436 constructed. */
5437
5438static Lisp_Object
5439unwind_create_frame (frame)
5440 Lisp_Object frame;
5441{
5442 struct frame *f = XFRAME (frame);
5443
5444 /* If frame is ``official'', nothing to do. */
5445 if (!CONSP (Vframe_list) || !EQ (XCAR (Vframe_list), frame))
5446 {
5447#ifdef GLYPH_DEBUG
5448 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
5449#endif
7d0393cf 5450
937e601e
AI
5451 x_free_frame_resources (f);
5452
5453 /* Check that reference counts are indeed correct. */
5454 xassert (dpyinfo->reference_count == dpyinfo_refcount);
5455 xassert (dpyinfo->image_cache->refcount == image_cache_refcount);
c844a81a
GM
5456
5457 return Qt;
937e601e 5458 }
7d0393cf 5459
937e601e
AI
5460 return Qnil;
5461}
5462
5463
ee78dc32
GV
5464DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
5465 1, 1, 0,
74e1aeec
JR
5466 doc: /* Make a new window, which is called a \"frame\" in Emacs terms.
5467Returns an Emacs frame object.
5468ALIST is an alist of frame parameters.
5469If the parameters specify that the frame should not have a minibuffer,
5470and do not specify a specific minibuffer window to use,
5471then `default-minibuffer-frame' must be a frame whose minibuffer can
5472be shared by the new frame.
5473
5474This function is an internal primitive--use `make-frame' instead. */)
ee78dc32
GV
5475 (parms)
5476 Lisp_Object parms;
5477{
5478 struct frame *f;
5479 Lisp_Object frame, tem;
5480 Lisp_Object name;
5481 int minibuffer_only = 0;
5482 long window_prompting = 0;
5483 int width, height;
331379bf 5484 int count = SPECPDL_INDEX ();
1edf84e7 5485 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
ee78dc32 5486 Lisp_Object display;
6fc2811b 5487 struct w32_display_info *dpyinfo = NULL;
ee78dc32
GV
5488 Lisp_Object parent;
5489 struct kboard *kb;
5490
4587b026
GV
5491 check_w32 ();
5492
ee78dc32
GV
5493 /* Use this general default value to start with
5494 until we know if this frame has a specified name. */
5495 Vx_resource_name = Vinvocation_name;
5496
6fc2811b 5497 display = w32_get_arg (parms, Qdisplay, 0, 0, RES_TYPE_STRING);
ee78dc32
GV
5498 if (EQ (display, Qunbound))
5499 display = Qnil;
5500 dpyinfo = check_x_display_info (display);
5501#ifdef MULTI_KBOARD
5502 kb = dpyinfo->kboard;
5503#else
5504 kb = &the_only_kboard;
5505#endif
5506
6fc2811b 5507 name = w32_get_arg (parms, Qname, "name", "Name", RES_TYPE_STRING);
ee78dc32
GV
5508 if (!STRINGP (name)
5509 && ! EQ (name, Qunbound)
5510 && ! NILP (name))
5511 error ("Invalid frame name--not a string or nil");
5512
5513 if (STRINGP (name))
5514 Vx_resource_name = name;
5515
5516 /* See if parent window is specified. */
6fc2811b 5517 parent = w32_get_arg (parms, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
ee78dc32
GV
5518 if (EQ (parent, Qunbound))
5519 parent = Qnil;
5520 if (! NILP (parent))
b7826503 5521 CHECK_NUMBER (parent);
ee78dc32 5522
1edf84e7
GV
5523 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
5524 /* No need to protect DISPLAY because that's not used after passing
5525 it to make_frame_without_minibuffer. */
5526 frame = Qnil;
5527 GCPRO4 (parms, parent, name, frame);
1660f34a
JR
5528 tem = w32_get_arg (parms, Qminibuffer, "minibuffer", "Minibuffer",
5529 RES_TYPE_SYMBOL);
ee78dc32
GV
5530 if (EQ (tem, Qnone) || NILP (tem))
5531 f = make_frame_without_minibuffer (Qnil, kb, display);
5532 else if (EQ (tem, Qonly))
5533 {
5534 f = make_minibuffer_frame ();
5535 minibuffer_only = 1;
5536 }
5537 else if (WINDOWP (tem))
5538 f = make_frame_without_minibuffer (tem, kb, display);
5539 else
5540 f = make_frame (1);
5541
1edf84e7
GV
5542 XSETFRAME (frame, f);
5543
ee78dc32
GV
5544 /* Note that Windows does support scroll bars. */
5545 FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;
5ac45f98
GV
5546 /* By default, make scrollbars the system standard width. */
5547 f->scroll_bar_pixel_width = GetSystemMetrics (SM_CXVSCROLL);
ee78dc32 5548
fbd6baed 5549 f->output_method = output_w32;
6fc2811b
JR
5550 f->output_data.w32 =
5551 (struct w32_output *) xmalloc (sizeof (struct w32_output));
fbd6baed 5552 bzero (f->output_data.w32, sizeof (struct w32_output));
4587b026 5553 FRAME_FONTSET (f) = -1;
937e601e 5554 record_unwind_protect (unwind_create_frame, frame);
4587b026 5555
1edf84e7 5556 f->icon_name
6fc2811b 5557 = w32_get_arg (parms, Qicon_name, "iconName", "Title", RES_TYPE_STRING);
1edf84e7
GV
5558 if (! STRINGP (f->icon_name))
5559 f->icon_name = Qnil;
5560
fbd6baed 5561/* FRAME_W32_DISPLAY_INFO (f) = dpyinfo; */
ee78dc32
GV
5562#ifdef MULTI_KBOARD
5563 FRAME_KBOARD (f) = kb;
5564#endif
5565
5566 /* Specify the parent under which to make this window. */
5567
5568 if (!NILP (parent))
5569 {
1660f34a 5570 f->output_data.w32->parent_desc = (Window) XFASTINT (parent);
fbd6baed 5571 f->output_data.w32->explicit_parent = 1;
ee78dc32
GV
5572 }
5573 else
5574 {
fbd6baed
GV
5575 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
5576 f->output_data.w32->explicit_parent = 0;
ee78dc32
GV
5577 }
5578
ee78dc32
GV
5579 /* Set the name; the functions to which we pass f expect the name to
5580 be set. */
5581 if (EQ (name, Qunbound) || NILP (name))
5582 {
fbd6baed 5583 f->name = build_string (dpyinfo->w32_id_name);
ee78dc32
GV
5584 f->explicit_name = 0;
5585 }
5586 else
5587 {
5588 f->name = name;
5589 f->explicit_name = 1;
5590 /* use the frame's title when getting resources for this frame. */
5591 specbind (Qx_resource_name, name);
5592 }
5593
5594 /* Extract the window parameters from the supplied values
5595 that are needed to determine window geometry. */
5596 {
5597 Lisp_Object font;
5598
6fc2811b
JR
5599 font = w32_get_arg (parms, Qfont, "font", "Font", RES_TYPE_STRING);
5600
ee78dc32
GV
5601 BLOCK_INPUT;
5602 /* First, try whatever font the caller has specified. */
5603 if (STRINGP (font))
4587b026
GV
5604 {
5605 tem = Fquery_fontset (font, Qnil);
5606 if (STRINGP (tem))
d5db4077 5607 font = x_new_fontset (f, SDATA (tem));
4587b026 5608 else
d5db4077 5609 font = x_new_font (f, SDATA (font));
4587b026 5610 }
ee78dc32
GV
5611 /* Try out a font which we hope has bold and italic variations. */
5612 if (!STRINGP (font))
e39649be 5613 font = x_new_font (f, "-*-Courier New-normal-r-*-*-*-100-*-*-c-*-iso8859-1");
ee78dc32 5614 if (! STRINGP (font))
6fc2811b 5615 font = x_new_font (f, "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1");
ee78dc32
GV
5616 /* If those didn't work, look for something which will at least work. */
5617 if (! STRINGP (font))
6fc2811b 5618 font = x_new_font (f, "-*-Fixedsys-normal-r-*-*-12-*-*-*-c-*-iso8859-1");
ee78dc32
GV
5619 UNBLOCK_INPUT;
5620 if (! STRINGP (font))
1edf84e7 5621 font = build_string ("Fixedsys");
ee78dc32 5622
7d0393cf 5623 x_default_parameter (f, parms, Qfont, font,
6fc2811b 5624 "font", "Font", RES_TYPE_STRING);
ee78dc32
GV
5625 }
5626
5627 x_default_parameter (f, parms, Qborder_width, make_number (2),
1660f34a 5628 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
ee78dc32
GV
5629 /* This defaults to 2 in order to match xterm. We recognize either
5630 internalBorderWidth or internalBorder (which is what xterm calls
5631 it). */
5632 if (NILP (Fassq (Qinternal_border_width, parms)))
5633 {
5634 Lisp_Object value;
5635
6fc2811b 5636 value = w32_get_arg (parms, Qinternal_border_width,
1660f34a 5637 "internalBorder", "InternalBorder", RES_TYPE_NUMBER);
ee78dc32
GV
5638 if (! EQ (value, Qunbound))
5639 parms = Fcons (Fcons (Qinternal_border_width, value),
5640 parms);
5641 }
1edf84e7 5642 /* Default internalBorderWidth to 0 on Windows to match other programs. */
ee78dc32 5643 x_default_parameter (f, parms, Qinternal_border_width, make_number (0),
1660f34a
JR
5644 "internalBorderWidth", "InternalBorder", RES_TYPE_NUMBER);
5645 x_default_parameter (f, parms, Qvertical_scroll_bars, Qright,
5646 "verticalScrollBars", "ScrollBars", RES_TYPE_SYMBOL);
ee78dc32
GV
5647
5648 /* Also do the stuff which must be set before the window exists. */
5649 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
6fc2811b 5650 "foreground", "Foreground", RES_TYPE_STRING);
ee78dc32 5651 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
6fc2811b 5652 "background", "Background", RES_TYPE_STRING);
ee78dc32 5653 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
6fc2811b 5654 "pointerColor", "Foreground", RES_TYPE_STRING);
ee78dc32 5655 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
6fc2811b 5656 "cursorColor", "Foreground", RES_TYPE_STRING);
ee78dc32 5657 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
6fc2811b
JR
5658 "borderColor", "BorderColor", RES_TYPE_STRING);
5659 x_default_parameter (f, parms, Qscreen_gamma, Qnil,
5660 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
dfff8a69
JR
5661 x_default_parameter (f, parms, Qline_spacing, Qnil,
5662 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
41c1bdd9
KS
5663 x_default_parameter (f, parms, Qleft_fringe, Qnil,
5664 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
5665 x_default_parameter (f, parms, Qright_fringe, Qnil,
5666 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
6fc2811b 5667
ee78dc32 5668
6fc2811b
JR
5669 /* Init faces before x_default_parameter is called for scroll-bar
5670 parameters because that function calls x_set_scroll_bar_width,
5671 which calls change_frame_size, which calls Fset_window_buffer,
5672 which runs hooks, which call Fvertical_motion. At the end, we
5673 end up in init_iterator with a null face cache, which should not
5674 happen. */
5675 init_frame_faces (f);
7d0393cf 5676
ee78dc32 5677 x_default_parameter (f, parms, Qmenu_bar_lines, make_number (1),
6fc2811b 5678 "menuBar", "MenuBar", RES_TYPE_NUMBER);
d3109773 5679 x_default_parameter (f, parms, Qtool_bar_lines, make_number (1),
6fc2811b 5680 "toolBar", "ToolBar", RES_TYPE_NUMBER);
919f1e88 5681
1edf84e7 5682 x_default_parameter (f, parms, Qbuffer_predicate, Qnil,
6fc2811b 5683 "bufferPredicate", "BufferPredicate", RES_TYPE_SYMBOL);
1edf84e7 5684 x_default_parameter (f, parms, Qtitle, Qnil,
6fc2811b 5685 "title", "Title", RES_TYPE_STRING);
f7b9d4d1
JR
5686 x_default_parameter (f, parms, Qfullscreen, Qnil,
5687 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
ee78dc32 5688
fbd6baed
GV
5689 f->output_data.w32->dwStyle = WS_OVERLAPPEDWINDOW;
5690 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
3cf3436e 5691
c9b2104d
JR
5692 f->output_data.w32->text_cursor = w32_load_cursor (IDC_IBEAM);
5693 f->output_data.w32->nontext_cursor = w32_load_cursor (IDC_ARROW);
5694 f->output_data.w32->modeline_cursor = w32_load_cursor (IDC_ARROW);
5695 f->output_data.w32->cross_cursor = w32_load_cursor (IDC_CROSS);
5696 f->output_data.w32->hourglass_cursor = w32_load_cursor (IDC_WAIT);
5697 f->output_data.w32->horizontal_drag_cursor = w32_load_cursor (IDC_SIZEWE);
5698 f->output_data.w32->hand_cursor = w32_load_cursor (IDC_HAND);
5699
3cf3436e
JR
5700 /* Add the tool-bar height to the initial frame height so that the
5701 user gets a text display area of the size he specified with -g or
5702 via .Xdefaults. Later changes of the tool-bar height don't
5703 change the frame size. This is done so that users can create
5704 tall Emacs frames without having to guess how tall the tool-bar
5705 will get. */
5706 if (FRAME_TOOL_BAR_LINES (f))
5707 {
5708 int margin, relief, bar_height;
7d0393cf 5709
a05e2bae 5710 relief = (tool_bar_button_relief >= 0
3cf3436e
JR
5711 ? tool_bar_button_relief
5712 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
5713
5714 if (INTEGERP (Vtool_bar_button_margin)
5715 && XINT (Vtool_bar_button_margin) > 0)
5716 margin = XFASTINT (Vtool_bar_button_margin);
5717 else if (CONSP (Vtool_bar_button_margin)
5718 && INTEGERP (XCDR (Vtool_bar_button_margin))
5719 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
5720 margin = XFASTINT (XCDR (Vtool_bar_button_margin));
5721 else
5722 margin = 0;
7d0393cf 5723
3cf3436e
JR
5724 bar_height = DEFAULT_TOOL_BAR_IMAGE_HEIGHT + 2 * margin + 2 * relief;
5725 f->height += (bar_height + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
5726 }
5727
ee78dc32
GV
5728 window_prompting = x_figure_window_size (f, parms);
5729
5730 if (window_prompting & XNegative)
5731 {
5732 if (window_prompting & YNegative)
fbd6baed 5733 f->output_data.w32->win_gravity = SouthEastGravity;
ee78dc32 5734 else
fbd6baed 5735 f->output_data.w32->win_gravity = NorthEastGravity;
ee78dc32
GV
5736 }
5737 else
5738 {
5739 if (window_prompting & YNegative)
fbd6baed 5740 f->output_data.w32->win_gravity = SouthWestGravity;
ee78dc32 5741 else
fbd6baed 5742 f->output_data.w32->win_gravity = NorthWestGravity;
ee78dc32
GV
5743 }
5744
fbd6baed 5745 f->output_data.w32->size_hint_flags = window_prompting;
ee78dc32 5746
6fc2811b
JR
5747 tem = w32_get_arg (parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
5748 f->no_split = minibuffer_only || EQ (tem, Qt);
5749
fbd6baed 5750 w32_window (f, window_prompting, minibuffer_only);
ee78dc32 5751 x_icon (f, parms);
6fc2811b
JR
5752
5753 x_make_gc (f);
5754
5755 /* Now consider the frame official. */
5756 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
5757 Vframe_list = Fcons (frame, Vframe_list);
ee78dc32
GV
5758
5759 /* We need to do this after creating the window, so that the
5760 icon-creation functions can say whose icon they're describing. */
5761 x_default_parameter (f, parms, Qicon_type, Qnil,
6fc2811b 5762 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
ee78dc32
GV
5763
5764 x_default_parameter (f, parms, Qauto_raise, Qnil,
6fc2811b 5765 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
ee78dc32 5766 x_default_parameter (f, parms, Qauto_lower, Qnil,
6fc2811b 5767 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
ee78dc32 5768 x_default_parameter (f, parms, Qcursor_type, Qbox,
6fc2811b
JR
5769 "cursorType", "CursorType", RES_TYPE_SYMBOL);
5770 x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
5771 "scrollBarWidth", "ScrollBarWidth", RES_TYPE_NUMBER);
ee78dc32
GV
5772
5773 /* Dimensions, especially f->height, must be done via change_frame_size.
5774 Change will not be effected unless different from the current
5775 f->height. */
5776 width = f->width;
5777 height = f->height;
dc220243 5778
1026b400
RS
5779 f->height = 0;
5780 SET_FRAME_WIDTH (f, 0);
6fc2811b
JR
5781 change_frame_size (f, height, width, 1, 0, 0);
5782
6fc2811b
JR
5783 /* Tell the server what size and position, etc, we want, and how
5784 badly we want them. This should be done after we have the menu
5785 bar so that its size can be taken into account. */
ee78dc32
GV
5786 BLOCK_INPUT;
5787 x_wm_set_size_hint (f, window_prompting, 0);
5788 UNBLOCK_INPUT;
5789
815d969e
JR
5790 /* Avoid a bug that causes the new frame to never become visible if
5791 an echo area message is displayed during the following call1. */
5792 specbind(Qredisplay_dont_pause, Qt);
5793
4694d762
JR
5794 /* Set up faces after all frame parameters are known. This call
5795 also merges in face attributes specified for new frames. If we
5796 don't do this, the `menu' face for instance won't have the right
5797 colors, and the menu bar won't appear in the specified colors for
5798 new frames. */
5799 call1 (Qface_set_after_frame_default, frame);
5800
6fc2811b
JR
5801 /* Make the window appear on the frame and enable display, unless
5802 the caller says not to. However, with explicit parent, Emacs
5803 cannot control visibility, so don't try. */
fbd6baed 5804 if (! f->output_data.w32->explicit_parent)
ee78dc32
GV
5805 {
5806 Lisp_Object visibility;
5807
6fc2811b 5808 visibility = w32_get_arg (parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL);
ee78dc32
GV
5809 if (EQ (visibility, Qunbound))
5810 visibility = Qt;
5811
5812 if (EQ (visibility, Qicon))
5813 x_iconify_frame (f);
5814 else if (! NILP (visibility))
5815 x_make_frame_visible (f);
5816 else
5817 /* Must have been Qnil. */
5818 ;
5819 }
6fc2811b 5820 UNGCPRO;
7d0393cf 5821
9e57df62
GM
5822 /* Make sure windows on this frame appear in calls to next-window
5823 and similar functions. */
5824 Vwindow_list = Qnil;
7d0393cf 5825
ee78dc32
GV
5826 return unbind_to (count, frame);
5827}
5828
5829/* FRAME is used only to get a handle on the X display. We don't pass the
5830 display info directly because we're called from frame.c, which doesn't
5831 know about that structure. */
5832Lisp_Object
5833x_get_focus_frame (frame)
5834 struct frame *frame;
5835{
fbd6baed 5836 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (frame);
ee78dc32 5837 Lisp_Object xfocus;
fbd6baed 5838 if (! dpyinfo->w32_focus_frame)
ee78dc32
GV
5839 return Qnil;
5840
fbd6baed 5841 XSETFRAME (xfocus, dpyinfo->w32_focus_frame);
ee78dc32
GV
5842 return xfocus;
5843}
1edf84e7
GV
5844
5845DEFUN ("w32-focus-frame", Fw32_focus_frame, Sw32_focus_frame, 1, 1, 0,
74e1aeec 5846 doc: /* Give FRAME input focus, raising to foreground if necessary. */)
1edf84e7
GV
5847 (frame)
5848 Lisp_Object frame;
5849{
5850 x_focus_on_frame (check_x_frame (frame));
5851 return Qnil;
5852}
5853
ee78dc32 5854\f
767b1ff0
JR
5855/* Return the charset portion of a font name. */
5856char * xlfd_charset_of_font (char * fontname)
5857{
5858 char *charset, *encoding;
5859
5860 encoding = strrchr(fontname, '-');
ceb12877 5861 if (!encoding || encoding == fontname)
767b1ff0
JR
5862 return NULL;
5863
478ea067
AI
5864 for (charset = encoding - 1; charset >= fontname; charset--)
5865 if (*charset == '-')
5866 break;
767b1ff0 5867
478ea067 5868 if (charset == fontname || strcmp(charset, "-*-*") == 0)
767b1ff0
JR
5869 return NULL;
5870
5871 return charset + 1;
5872}
5873
33d52f9c
GV
5874struct font_info *w32_load_bdf_font (struct frame *f, char *fontname,
5875 int size, char* filename);
8edb0a6f 5876static Lisp_Object w32_list_bdf_fonts (Lisp_Object pattern, int max_names);
c7501041
EZ
5877static BOOL w32_to_x_font (LOGFONT * lplf, char * lpxstr, int len,
5878 char * charset);
5879static BOOL x_to_w32_font (char *lpxstr, LOGFONT *lplogfont);
33d52f9c 5880
8edb0a6f 5881static struct font_info *
33d52f9c 5882w32_load_system_font (f,fontname,size)
55dcfc15
AI
5883 struct frame *f;
5884 char * fontname;
5885 int size;
ee78dc32 5886{
4587b026
GV
5887 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
5888 Lisp_Object font_names;
5889
4587b026
GV
5890 /* Get a list of all the fonts that match this name. Once we
5891 have a list of matching fonts, we compare them against the fonts
5892 we already have loaded by comparing names. */
5893 font_names = w32_list_fonts (f, build_string (fontname), size, 100);
5894
5895 if (!NILP (font_names))
3c190163 5896 {
4587b026
GV
5897 Lisp_Object tail;
5898 int i;
4587b026
GV
5899
5900 /* First check if any are already loaded, as that is cheaper
5901 than loading another one. */
5902 for (i = 0; i < dpyinfo->n_fonts; i++)
8e713be6 5903 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
6fc2811b
JR
5904 if (dpyinfo->font_table[i].name
5905 && (!strcmp (dpyinfo->font_table[i].name,
d5db4077 5906 SDATA (XCAR (tail)))
6fc2811b 5907 || !strcmp (dpyinfo->font_table[i].full_name,
d5db4077 5908 SDATA (XCAR (tail)))))
4587b026 5909 return (dpyinfo->font_table + i);
6fc2811b 5910
d5db4077 5911 fontname = (char *) SDATA (XCAR (font_names));
4587b026 5912 }
1075afa9 5913 else if (w32_strict_fontnames)
5ca0cd71
GV
5914 {
5915 /* If EnumFontFamiliesEx was available, we got a full list of
5916 fonts back so stop now to avoid the possibility of loading a
5917 random font. If we had to fall back to EnumFontFamilies, the
5918 list is incomplete, so continue whether the font we want was
5919 listed or not. */
5920 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
5921 FARPROC enum_font_families_ex
1075afa9 5922 = GetProcAddress (gdi32, "EnumFontFamiliesExA");
5ca0cd71
GV
5923 if (enum_font_families_ex)
5924 return NULL;
5925 }
4587b026
GV
5926
5927 /* Load the font and add it to the table. */
5928 {
767b1ff0 5929 char *full_name, *encoding, *charset;
4587b026
GV
5930 XFontStruct *font;
5931 struct font_info *fontp;
3c190163 5932 LOGFONT lf;
4587b026 5933 BOOL ok;
19c291d3 5934 int codepage;
6fc2811b 5935 int i;
5ac45f98 5936
4587b026 5937 if (!fontname || !x_to_w32_font (fontname, &lf))
3c190163 5938 return (NULL);
5ac45f98 5939
4587b026
GV
5940 if (!*lf.lfFaceName)
5941 /* If no name was specified for the font, we get a random font
5942 from CreateFontIndirect - this is not particularly
5943 desirable, especially since CreateFontIndirect does not
5944 fill out the missing name in lf, so we never know what we
5945 ended up with. */
5946 return NULL;
5947
c8d88d08 5948 lf.lfQuality = DEFAULT_QUALITY;
d65a9cdc 5949
3c190163 5950 font = (XFontStruct *) xmalloc (sizeof (XFontStruct));
c6be3860 5951 bzero (font, sizeof (*font));
5ac45f98 5952
33d52f9c
GV
5953 /* Set bdf to NULL to indicate that this is a Windows font. */
5954 font->bdf = NULL;
5ac45f98 5955
3c190163 5956 BLOCK_INPUT;
5ac45f98
GV
5957
5958 font->hfont = CreateFontIndirect (&lf);
ee78dc32 5959
7d0393cf 5960 if (font->hfont == NULL)
1a292d24
AI
5961 {
5962 ok = FALSE;
7d0393cf
JB
5963 }
5964 else
1a292d24
AI
5965 {
5966 HDC hdc;
5967 HANDLE oldobj;
19c291d3
AI
5968
5969 codepage = w32_codepage_for_font (fontname);
1a292d24
AI
5970
5971 hdc = GetDC (dpyinfo->root_window);
5972 oldobj = SelectObject (hdc, font->hfont);
5c6682be 5973
1a292d24 5974 ok = GetTextMetrics (hdc, &font->tm);
5c6682be
JR
5975 if (codepage == CP_UNICODE)
5976 font->double_byte_p = 1;
5977 else
8b77111c
AI
5978 {
5979 /* Unfortunately, some fonts (eg. MingLiU, a big5 ttf font)
5980 don't report themselves as double byte fonts, when
5981 patently they are. So instead of trusting
5982 GetFontLanguageInfo, we check the properties of the
5983 codepage directly, since that is ultimately what we are
5984 working from anyway. */
5985 /* font->double_byte_p = GetFontLanguageInfo(hdc) & GCP_DBCS; */
5986 CPINFO cpi = {0};
5987 GetCPInfo (codepage, &cpi);
5988 font->double_byte_p = cpi.MaxCharSize > 1;
5989 }
5c6682be 5990
1a292d24
AI
5991 SelectObject (hdc, oldobj);
5992 ReleaseDC (dpyinfo->root_window, hdc);
6fc2811b
JR
5993 /* Fill out details in lf according to the font that was
5994 actually loaded. */
5995 lf.lfHeight = font->tm.tmInternalLeading - font->tm.tmHeight;
5996 lf.lfWidth = font->tm.tmAveCharWidth;
5997 lf.lfWeight = font->tm.tmWeight;
5998 lf.lfItalic = font->tm.tmItalic;
5999 lf.lfCharSet = font->tm.tmCharSet;
6000 lf.lfPitchAndFamily = ((font->tm.tmPitchAndFamily & TMPF_FIXED_PITCH)
d88c567c 6001 ? VARIABLE_PITCH : FIXED_PITCH);
6fc2811b
JR
6002 lf.lfOutPrecision = ((font->tm.tmPitchAndFamily & TMPF_VECTOR)
6003 ? OUT_STROKE_PRECIS : OUT_STRING_PRECIS);
c6be3860
AI
6004
6005 w32_cache_char_metrics (font);
1a292d24 6006 }
5ac45f98 6007
1a292d24 6008 UNBLOCK_INPUT;
5ac45f98 6009
4587b026
GV
6010 if (!ok)
6011 {
1a292d24
AI
6012 w32_unload_font (dpyinfo, font);
6013 return (NULL);
6014 }
ee78dc32 6015
6fc2811b
JR
6016 /* Find a free slot in the font table. */
6017 for (i = 0; i < dpyinfo->n_fonts; ++i)
6018 if (dpyinfo->font_table[i].name == NULL)
6019 break;
6020
6021 /* If no free slot found, maybe enlarge the font table. */
6022 if (i == dpyinfo->n_fonts
6023 && dpyinfo->n_fonts == dpyinfo->font_table_size)
4587b026 6024 {
6fc2811b
JR
6025 int sz;
6026 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
6027 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
4587b026 6028 dpyinfo->font_table
6fc2811b 6029 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
4587b026
GV
6030 }
6031
6fc2811b
JR
6032 fontp = dpyinfo->font_table + i;
6033 if (i == dpyinfo->n_fonts)
6034 ++dpyinfo->n_fonts;
4587b026
GV
6035
6036 /* Now fill in the slots of *FONTP. */
6037 BLOCK_INPUT;
6038 fontp->font = font;
6fc2811b 6039 fontp->font_idx = i;
4587b026
GV
6040 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
6041 bcopy (fontname, fontp->name, strlen (fontname) + 1);
6042
767b1ff0
JR
6043 charset = xlfd_charset_of_font (fontname);
6044
19c291d3
AI
6045 /* Cache the W32 codepage for a font. This makes w32_encode_char
6046 (called for every glyph during redisplay) much faster. */
6047 fontp->codepage = codepage;
6048
4587b026
GV
6049 /* Work out the font's full name. */
6050 full_name = (char *)xmalloc (100);
767b1ff0 6051 if (full_name && w32_to_x_font (&lf, full_name, 100, charset))
4587b026
GV
6052 fontp->full_name = full_name;
6053 else
6054 {
6055 /* If all else fails - just use the name we used to load it. */
6056 xfree (full_name);
6057 fontp->full_name = fontp->name;
6058 }
6059
6060 fontp->size = FONT_WIDTH (font);
6061 fontp->height = FONT_HEIGHT (font);
6062
6063 /* The slot `encoding' specifies how to map a character
6064 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
126f2e35
JR
6065 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
6066 (0:0x20..0x7F, 1:0xA0..0xFF,
6067 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
4587b026 6068 2:0xA020..0xFF7F). For the moment, we don't know which charset
6fc2811b 6069 uses this font. So, we set information in fontp->encoding[1]
4587b026
GV
6070 which is never used by any charset. If mapping can't be
6071 decided, set FONT_ENCODING_NOT_DECIDED. */
33d52f9c
GV
6072
6073 /* SJIS fonts need to be set to type 4, all others seem to work as
6074 type FONT_ENCODING_NOT_DECIDED. */
6075 encoding = strrchr (fontp->name, '-');
d84b082d 6076 if (encoding && strnicmp (encoding+1, "sjis", 4) == 0)
1c885fe1 6077 fontp->encoding[1] = 4;
33d52f9c 6078 else
1c885fe1 6079 fontp->encoding[1] = FONT_ENCODING_NOT_DECIDED;
4587b026
GV
6080
6081 /* The following three values are set to 0 under W32, which is
6082 what they get set to if XGetFontProperty fails under X. */
6083 fontp->baseline_offset = 0;
6084 fontp->relative_compose = 0;
33d52f9c 6085 fontp->default_ascent = 0;
4587b026 6086
6fc2811b
JR
6087 /* Set global flag fonts_changed_p to non-zero if the font loaded
6088 has a character with a smaller width than any other character
f7b9d4d1 6089 before, or if the font loaded has a smaller height than any
6fc2811b
JR
6090 other font loaded before. If this happens, it will make a
6091 glyph matrix reallocation necessary. */
f7b9d4d1 6092 fonts_changed_p |= x_compute_min_glyph_bounds (f);
4587b026 6093 UNBLOCK_INPUT;
4587b026
GV
6094 return fontp;
6095 }
6096}
6097
33d52f9c
GV
6098/* Load font named FONTNAME of size SIZE for frame F, and return a
6099 pointer to the structure font_info while allocating it dynamically.
6100 If loading fails, return NULL. */
6101struct font_info *
6102w32_load_font (f,fontname,size)
6103struct frame *f;
6104char * fontname;
6105int size;
6106{
6107 Lisp_Object bdf_fonts;
6108 struct font_info *retval = NULL;
6109
8edb0a6f 6110 bdf_fonts = w32_list_bdf_fonts (build_string (fontname), 1);
33d52f9c
GV
6111
6112 while (!retval && CONSP (bdf_fonts))
6113 {
6114 char *bdf_name, *bdf_file;
6115 Lisp_Object bdf_pair;
6116
d5db4077 6117 bdf_name = SDATA (XCAR (bdf_fonts));
8e713be6 6118 bdf_pair = Fassoc (XCAR (bdf_fonts), Vw32_bdf_filename_alist);
d5db4077 6119 bdf_file = SDATA (XCDR (bdf_pair));
33d52f9c
GV
6120
6121 retval = w32_load_bdf_font (f, bdf_name, size, bdf_file);
6122
8e713be6 6123 bdf_fonts = XCDR (bdf_fonts);
33d52f9c
GV
6124 }
6125
6126 if (retval)
6127 return retval;
6128
6129 return w32_load_system_font(f, fontname, size);
6130}
6131
6132
7d0393cf 6133void
fbd6baed
GV
6134w32_unload_font (dpyinfo, font)
6135 struct w32_display_info *dpyinfo;
ee78dc32
GV
6136 XFontStruct * font;
6137{
7d0393cf 6138 if (font)
ee78dc32 6139 {
c6be3860 6140 if (font->per_char) xfree (font->per_char);
33d52f9c
GV
6141 if (font->bdf) w32_free_bdf_font (font->bdf);
6142
3c190163 6143 if (font->hfont) DeleteObject(font->hfont);
ee78dc32
GV
6144 xfree (font);
6145 }
6146}
6147
fbd6baed 6148/* The font conversion stuff between x and w32 */
ee78dc32
GV
6149
6150/* X font string is as follows (from faces.el)
6151 * (let ((- "[-?]")
6152 * (foundry "[^-]+")
6153 * (family "[^-]+")
6154 * (weight "\\(bold\\|demibold\\|medium\\)") ; 1
6155 * (weight\? "\\([^-]*\\)") ; 1
6156 * (slant "\\([ior]\\)") ; 2
6157 * (slant\? "\\([^-]?\\)") ; 2
6158 * (swidth "\\([^-]*\\)") ; 3
6159 * (adstyle "[^-]*") ; 4
6160 * (pixelsize "[0-9]+")
6161 * (pointsize "[0-9][0-9]+")
6162 * (resx "[0-9][0-9]+")
6163 * (resy "[0-9][0-9]+")
6164 * (spacing "[cmp?*]")
6165 * (avgwidth "[0-9]+")
6166 * (registry "[^-]+")
6167 * (encoding "[^-]+")
6168 * )
ee78dc32 6169 */
ee78dc32 6170
7d0393cf 6171static LONG
fbd6baed 6172x_to_w32_weight (lpw)
ee78dc32
GV
6173 char * lpw;
6174{
6175 if (!lpw) return (FW_DONTCARE);
5ac45f98
GV
6176
6177 if (stricmp (lpw,"heavy") == 0) return FW_HEAVY;
6178 else if (stricmp (lpw,"extrabold") == 0) return FW_EXTRABOLD;
6179 else if (stricmp (lpw,"bold") == 0) return FW_BOLD;
6180 else if (stricmp (lpw,"demibold") == 0) return FW_SEMIBOLD;
1edf84e7 6181 else if (stricmp (lpw,"semibold") == 0) return FW_SEMIBOLD;
5ac45f98
GV
6182 else if (stricmp (lpw,"medium") == 0) return FW_MEDIUM;
6183 else if (stricmp (lpw,"normal") == 0) return FW_NORMAL;
6184 else if (stricmp (lpw,"light") == 0) return FW_LIGHT;
6185 else if (stricmp (lpw,"extralight") == 0) return FW_EXTRALIGHT;
6186 else if (stricmp (lpw,"thin") == 0) return FW_THIN;
ee78dc32 6187 else
5ac45f98 6188 return FW_DONTCARE;
ee78dc32
GV
6189}
6190
5ac45f98 6191
7d0393cf 6192static char *
fbd6baed 6193w32_to_x_weight (fnweight)
ee78dc32
GV
6194 int fnweight;
6195{
5ac45f98
GV
6196 if (fnweight >= FW_HEAVY) return "heavy";
6197 if (fnweight >= FW_EXTRABOLD) return "extrabold";
6198 if (fnweight >= FW_BOLD) return "bold";
03f8fb34 6199 if (fnweight >= FW_SEMIBOLD) return "demibold";
5ac45f98
GV
6200 if (fnweight >= FW_MEDIUM) return "medium";
6201 if (fnweight >= FW_NORMAL) return "normal";
6202 if (fnweight >= FW_LIGHT) return "light";
6203 if (fnweight >= FW_EXTRALIGHT) return "extralight";
6204 if (fnweight >= FW_THIN) return "thin";
6205 else
6206 return "*";
6207}
6208
8edb0a6f 6209static LONG
fbd6baed 6210x_to_w32_charset (lpcs)
5ac45f98
GV
6211 char * lpcs;
6212{
767b1ff0 6213 Lisp_Object this_entry, w32_charset;
8b77111c
AI
6214 char *charset;
6215 int len = strlen (lpcs);
6216
6217 /* Support "*-#nnn" format for unknown charsets. */
6218 if (strncmp (lpcs, "*-#", 3) == 0)
6219 return atoi (lpcs + 3);
6220
6221 /* Handle wildcards by ignoring them; eg. treat "big5*-*" as "big5". */
6222 charset = alloca (len + 1);
6223 strcpy (charset, lpcs);
6224 lpcs = strchr (charset, '*');
6225 if (lpcs)
6226 *lpcs = 0;
4587b026 6227
dfff8a69
JR
6228 /* Look through w32-charset-info-alist for the character set.
6229 Format of each entry is
6230 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
6231 */
8b77111c 6232 this_entry = Fassoc (build_string(charset), Vw32_charset_info_alist);
4587b026 6233
767b1ff0
JR
6234 if (NILP(this_entry))
6235 {
6236 /* At startup, we want iso8859-1 fonts to come up properly. */
8b77111c 6237 if (stricmp(charset, "iso8859-1") == 0)
767b1ff0
JR
6238 return ANSI_CHARSET;
6239 else
6240 return DEFAULT_CHARSET;
6241 }
6242
6243 w32_charset = Fcar (Fcdr (this_entry));
6244
d84b082d 6245 /* Translate Lisp symbol to number. */
767b1ff0
JR
6246 if (w32_charset == Qw32_charset_ansi)
6247 return ANSI_CHARSET;
6248 if (w32_charset == Qw32_charset_symbol)
6249 return SYMBOL_CHARSET;
6250 if (w32_charset == Qw32_charset_shiftjis)
6251 return SHIFTJIS_CHARSET;
6252 if (w32_charset == Qw32_charset_hangeul)
6253 return HANGEUL_CHARSET;
6254 if (w32_charset == Qw32_charset_chinesebig5)
6255 return CHINESEBIG5_CHARSET;
6256 if (w32_charset == Qw32_charset_gb2312)
6257 return GB2312_CHARSET;
6258 if (w32_charset == Qw32_charset_oem)
6259 return OEM_CHARSET;
dfff8a69 6260#ifdef JOHAB_CHARSET
767b1ff0
JR
6261 if (w32_charset == Qw32_charset_johab)
6262 return JOHAB_CHARSET;
6263 if (w32_charset == Qw32_charset_easteurope)
6264 return EASTEUROPE_CHARSET;
6265 if (w32_charset == Qw32_charset_turkish)
6266 return TURKISH_CHARSET;
6267 if (w32_charset == Qw32_charset_baltic)
6268 return BALTIC_CHARSET;
6269 if (w32_charset == Qw32_charset_russian)
6270 return RUSSIAN_CHARSET;
6271 if (w32_charset == Qw32_charset_arabic)
6272 return ARABIC_CHARSET;
6273 if (w32_charset == Qw32_charset_greek)
6274 return GREEK_CHARSET;
6275 if (w32_charset == Qw32_charset_hebrew)
6276 return HEBREW_CHARSET;
6277 if (w32_charset == Qw32_charset_vietnamese)
6278 return VIETNAMESE_CHARSET;
6279 if (w32_charset == Qw32_charset_thai)
6280 return THAI_CHARSET;
6281 if (w32_charset == Qw32_charset_mac)
6282 return MAC_CHARSET;
dfff8a69 6283#endif /* JOHAB_CHARSET */
5ac45f98 6284#ifdef UNICODE_CHARSET
767b1ff0
JR
6285 if (w32_charset == Qw32_charset_unicode)
6286 return UNICODE_CHARSET;
5ac45f98 6287#endif
dfff8a69
JR
6288
6289 return DEFAULT_CHARSET;
5ac45f98
GV
6290}
6291
dfff8a69 6292
8edb0a6f 6293static char *
fbd6baed 6294w32_to_x_charset (fncharset)
5ac45f98
GV
6295 int fncharset;
6296{
5e905a57 6297 static char buf[32];
767b1ff0 6298 Lisp_Object charset_type;
1edf84e7 6299
5ac45f98
GV
6300 switch (fncharset)
6301 {
767b1ff0
JR
6302 case ANSI_CHARSET:
6303 /* Handle startup case of w32-charset-info-alist not
6304 being set up yet. */
6305 if (NILP(Vw32_charset_info_alist))
6306 return "iso8859-1";
6307 charset_type = Qw32_charset_ansi;
6308 break;
6309 case DEFAULT_CHARSET:
6310 charset_type = Qw32_charset_default;
6311 break;
6312 case SYMBOL_CHARSET:
6313 charset_type = Qw32_charset_symbol;
6314 break;
6315 case SHIFTJIS_CHARSET:
6316 charset_type = Qw32_charset_shiftjis;
6317 break;
6318 case HANGEUL_CHARSET:
6319 charset_type = Qw32_charset_hangeul;
6320 break;
6321 case GB2312_CHARSET:
6322 charset_type = Qw32_charset_gb2312;
6323 break;
6324 case CHINESEBIG5_CHARSET:
6325 charset_type = Qw32_charset_chinesebig5;
6326 break;
6327 case OEM_CHARSET:
6328 charset_type = Qw32_charset_oem;
6329 break;
4587b026
GV
6330
6331 /* More recent versions of Windows (95 and NT4.0) define more
6332 character sets. */
6333#ifdef EASTEUROPE_CHARSET
767b1ff0
JR
6334 case EASTEUROPE_CHARSET:
6335 charset_type = Qw32_charset_easteurope;
6336 break;
6337 case TURKISH_CHARSET:
6338 charset_type = Qw32_charset_turkish;
6339 break;
6340 case BALTIC_CHARSET:
6341 charset_type = Qw32_charset_baltic;
6342 break;
33d52f9c 6343 case RUSSIAN_CHARSET:
767b1ff0
JR
6344 charset_type = Qw32_charset_russian;
6345 break;
6346 case ARABIC_CHARSET:
6347 charset_type = Qw32_charset_arabic;
6348 break;
6349 case GREEK_CHARSET:
6350 charset_type = Qw32_charset_greek;
6351 break;
6352 case HEBREW_CHARSET:
6353 charset_type = Qw32_charset_hebrew;
6354 break;
6355 case VIETNAMESE_CHARSET:
6356 charset_type = Qw32_charset_vietnamese;
6357 break;
6358 case THAI_CHARSET:
6359 charset_type = Qw32_charset_thai;
6360 break;
6361 case MAC_CHARSET:
6362 charset_type = Qw32_charset_mac;
6363 break;
6364 case JOHAB_CHARSET:
6365 charset_type = Qw32_charset_johab;
6366 break;
4587b026
GV
6367#endif
6368
5ac45f98 6369#ifdef UNICODE_CHARSET
767b1ff0
JR
6370 case UNICODE_CHARSET:
6371 charset_type = Qw32_charset_unicode;
6372 break;
5ac45f98 6373#endif
767b1ff0
JR
6374 default:
6375 /* Encode numerical value of unknown charset. */
6376 sprintf (buf, "*-#%u", fncharset);
6377 return buf;
5ac45f98 6378 }
7d0393cf 6379
767b1ff0
JR
6380 {
6381 Lisp_Object rest;
6382 char * best_match = NULL;
6383
6384 /* Look through w32-charset-info-alist for the character set.
6385 Prefer ISO codepages, and prefer lower numbers in the ISO
6386 range. Only return charsets for codepages which are installed.
6387
6388 Format of each entry is
6389 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
6390 */
6391 for (rest = Vw32_charset_info_alist; CONSP (rest); rest = XCDR (rest))
6392 {
6393 char * x_charset;
6394 Lisp_Object w32_charset;
6395 Lisp_Object codepage;
6396
6397 Lisp_Object this_entry = XCAR (rest);
6398
6399 /* Skip invalid entries in alist. */
6400 if (!CONSP (this_entry) || !STRINGP (XCAR (this_entry))
6401 || !CONSP (XCDR (this_entry))
6402 || !SYMBOLP (XCAR (XCDR (this_entry))))
6403 continue;
6404
d5db4077 6405 x_charset = SDATA (XCAR (this_entry));
767b1ff0
JR
6406 w32_charset = XCAR (XCDR (this_entry));
6407 codepage = XCDR (XCDR (this_entry));
6408
6409 /* Look for Same charset and a valid codepage (or non-int
6410 which means ignore). */
6411 if (w32_charset == charset_type
6412 && (!INTEGERP (codepage) || codepage == CP_DEFAULT
6413 || IsValidCodePage (XINT (codepage))))
6414 {
6415 /* If we don't have a match already, then this is the
6416 best. */
6417 if (!best_match)
6418 best_match = x_charset;
6419 /* If this is an ISO codepage, and the best so far isn't,
6420 then this is better. */
d84b082d
JR
6421 else if (strnicmp (best_match, "iso", 3) != 0
6422 && strnicmp (x_charset, "iso", 3) == 0)
767b1ff0
JR
6423 best_match = x_charset;
6424 /* If both are ISO8859 codepages, choose the one with the
6425 lowest number in the encoding field. */
d84b082d
JR
6426 else if (strnicmp (best_match, "iso8859-", 8) == 0
6427 && strnicmp (x_charset, "iso8859-", 8) == 0)
767b1ff0
JR
6428 {
6429 int best_enc = atoi (best_match + 8);
6430 int this_enc = atoi (x_charset + 8);
6431 if (this_enc > 0 && this_enc < best_enc)
6432 best_match = x_charset;
7d0393cf 6433 }
767b1ff0
JR
6434 }
6435 }
6436
6437 /* If no match, encode the numeric value. */
6438 if (!best_match)
6439 {
6440 sprintf (buf, "*-#%u", fncharset);
6441 return buf;
6442 }
6443
5e905a57
JR
6444 strncpy(buf, best_match, 31);
6445 buf[31] = '\0';
767b1ff0
JR
6446 return buf;
6447 }
ee78dc32
GV
6448}
6449
dfff8a69 6450
d84b082d
JR
6451/* Return all the X charsets that map to a font. */
6452static Lisp_Object
6453w32_to_all_x_charsets (fncharset)
6454 int fncharset;
6455{
6456 static char buf[32];
6457 Lisp_Object charset_type;
6458 Lisp_Object retval = Qnil;
6459
6460 switch (fncharset)
6461 {
6462 case ANSI_CHARSET:
6463 /* Handle startup case of w32-charset-info-alist not
6464 being set up yet. */
6465 if (NILP(Vw32_charset_info_alist))
d86c35ee
JR
6466 return Fcons (build_string ("iso8859-1"), Qnil);
6467
d84b082d
JR
6468 charset_type = Qw32_charset_ansi;
6469 break;
6470 case DEFAULT_CHARSET:
6471 charset_type = Qw32_charset_default;
6472 break;
6473 case SYMBOL_CHARSET:
6474 charset_type = Qw32_charset_symbol;
6475 break;
6476 case SHIFTJIS_CHARSET:
6477 charset_type = Qw32_charset_shiftjis;
6478 break;
6479 case HANGEUL_CHARSET:
6480 charset_type = Qw32_charset_hangeul;
6481 break;
6482 case GB2312_CHARSET:
6483 charset_type = Qw32_charset_gb2312;
6484 break;
6485 case CHINESEBIG5_CHARSET:
6486 charset_type = Qw32_charset_chinesebig5;
6487 break;
6488 case OEM_CHARSET:
6489 charset_type = Qw32_charset_oem;
6490 break;
6491
6492 /* More recent versions of Windows (95 and NT4.0) define more
6493 character sets. */
6494#ifdef EASTEUROPE_CHARSET
6495 case EASTEUROPE_CHARSET:
6496 charset_type = Qw32_charset_easteurope;
6497 break;
6498 case TURKISH_CHARSET:
6499 charset_type = Qw32_charset_turkish;
6500 break;
6501 case BALTIC_CHARSET:
6502 charset_type = Qw32_charset_baltic;
6503 break;
6504 case RUSSIAN_CHARSET:
6505 charset_type = Qw32_charset_russian;
6506 break;
6507 case ARABIC_CHARSET:
6508 charset_type = Qw32_charset_arabic;
6509 break;
6510 case GREEK_CHARSET:
6511 charset_type = Qw32_charset_greek;
6512 break;
6513 case HEBREW_CHARSET:
6514 charset_type = Qw32_charset_hebrew;
6515 break;
6516 case VIETNAMESE_CHARSET:
6517 charset_type = Qw32_charset_vietnamese;
6518 break;
6519 case THAI_CHARSET:
6520 charset_type = Qw32_charset_thai;
6521 break;
6522 case MAC_CHARSET:
6523 charset_type = Qw32_charset_mac;
6524 break;
6525 case JOHAB_CHARSET:
6526 charset_type = Qw32_charset_johab;
6527 break;
6528#endif
6529
6530#ifdef UNICODE_CHARSET
6531 case UNICODE_CHARSET:
6532 charset_type = Qw32_charset_unicode;
6533 break;
6534#endif
6535 default:
6536 /* Encode numerical value of unknown charset. */
6537 sprintf (buf, "*-#%u", fncharset);
6538 return Fcons (build_string (buf), Qnil);
6539 }
7d0393cf 6540
d84b082d
JR
6541 {
6542 Lisp_Object rest;
6543 /* Look through w32-charset-info-alist for the character set.
6544 Only return charsets for codepages which are installed.
6545
6546 Format of each entry in Vw32_charset_info_alist is
6547 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
6548 */
6549 for (rest = Vw32_charset_info_alist; CONSP (rest); rest = XCDR (rest))
6550 {
6551 Lisp_Object x_charset;
6552 Lisp_Object w32_charset;
6553 Lisp_Object codepage;
6554
6555 Lisp_Object this_entry = XCAR (rest);
6556
6557 /* Skip invalid entries in alist. */
6558 if (!CONSP (this_entry) || !STRINGP (XCAR (this_entry))
6559 || !CONSP (XCDR (this_entry))
6560 || !SYMBOLP (XCAR (XCDR (this_entry))))
6561 continue;
6562
6563 x_charset = XCAR (this_entry);
6564 w32_charset = XCAR (XCDR (this_entry));
6565 codepage = XCDR (XCDR (this_entry));
6566
6567 /* Look for Same charset and a valid codepage (or non-int
6568 which means ignore). */
6569 if (w32_charset == charset_type
6570 && (!INTEGERP (codepage) || codepage == CP_DEFAULT
6571 || IsValidCodePage (XINT (codepage))))
6572 {
6573 retval = Fcons (x_charset, retval);
6574 }
6575 }
6576
6577 /* If no match, encode the numeric value. */
6578 if (NILP (retval))
6579 {
6580 sprintf (buf, "*-#%u", fncharset);
6581 return Fcons (build_string (buf), Qnil);
6582 }
6583
6584 return retval;
6585 }
6586}
6587
dfff8a69
JR
6588/* Get the Windows codepage corresponding to the specified font. The
6589 charset info in the font name is used to look up
6590 w32-charset-to-codepage-alist. */
7d0393cf 6591int
dfff8a69
JR
6592w32_codepage_for_font (char *fontname)
6593{
767b1ff0
JR
6594 Lisp_Object codepage, entry;
6595 char *charset_str, *charset, *end;
dfff8a69 6596
767b1ff0 6597 if (NILP (Vw32_charset_info_alist))
dfff8a69
JR
6598 return CP_DEFAULT;
6599
767b1ff0
JR
6600 /* Extract charset part of font string. */
6601 charset = xlfd_charset_of_font (fontname);
6602
6603 if (!charset)
ceb12877 6604 return CP_UNKNOWN;
767b1ff0 6605
8b77111c 6606 charset_str = (char *) alloca (strlen (charset) + 1);
767b1ff0
JR
6607 strcpy (charset_str, charset);
6608
8b77111c 6609#if 0
dfff8a69
JR
6610 /* Remove leading "*-". */
6611 if (strncmp ("*-", charset_str, 2) == 0)
6612 charset = charset_str + 2;
6613 else
8b77111c 6614#endif
dfff8a69
JR
6615 charset = charset_str;
6616
6617 /* Stop match at wildcard (including preceding '-'). */
6618 if (end = strchr (charset, '*'))
6619 {
6620 if (end > charset && *(end-1) == '-')
6621 end--;
6622 *end = '\0';
6623 }
6624
767b1ff0
JR
6625 entry = Fassoc (build_string(charset), Vw32_charset_info_alist);
6626 if (NILP (entry))
ceb12877 6627 return CP_UNKNOWN;
767b1ff0
JR
6628
6629 codepage = Fcdr (Fcdr (entry));
6630
6631 if (NILP (codepage))
6632 return CP_8BIT;
6633 else if (XFASTINT (codepage) == XFASTINT (Qt))
6634 return CP_UNICODE;
6635 else if (INTEGERP (codepage))
dfff8a69
JR
6636 return XINT (codepage);
6637 else
ceb12877 6638 return CP_UNKNOWN;
dfff8a69
JR
6639}
6640
6641
7d0393cf 6642static BOOL
767b1ff0 6643w32_to_x_font (lplogfont, lpxstr, len, specific_charset)
ee78dc32
GV
6644 LOGFONT * lplogfont;
6645 char * lpxstr;
6646 int len;
767b1ff0 6647 char * specific_charset;
ee78dc32 6648{
6fc2811b 6649 char* fonttype;
f46e6225 6650 char *fontname;
3cb20f4a
RS
6651 char height_pixels[8];
6652 char height_dpi[8];
6653 char width_pixels[8];
4587b026 6654 char *fontname_dash;
ac849ba4
JR
6655 int display_resy = (int) one_w32_display_info.resy;
6656 int display_resx = (int) one_w32_display_info.resx;
f46e6225
GV
6657 int bufsz;
6658 struct coding_system coding;
3cb20f4a
RS
6659
6660 if (!lpxstr) abort ();
ee78dc32 6661
3cb20f4a
RS
6662 if (!lplogfont)
6663 return FALSE;
6664
6fc2811b
JR
6665 if (lplogfont->lfOutPrecision == OUT_STRING_PRECIS)
6666 fonttype = "raster";
6667 else if (lplogfont->lfOutPrecision == OUT_STROKE_PRECIS)
6668 fonttype = "outline";
6669 else
6670 fonttype = "unknown";
6671
1fa3a200 6672 setup_coding_system (Fcheck_coding_system (Vlocale_coding_system),
f46e6225 6673 &coding);
aab5ac44
KH
6674 coding.src_multibyte = 0;
6675 coding.dst_multibyte = 1;
f46e6225 6676 coding.mode |= CODING_MODE_LAST_BLOCK;
65413122
KH
6677 /* We explicitely disable composition handling because selection
6678 data should not contain any composition sequence. */
6679 coding.composing = COMPOSITION_DISABLED;
f46e6225
GV
6680 bufsz = decoding_buffer_size (&coding, LF_FACESIZE);
6681
6682 fontname = alloca(sizeof(*fontname) * bufsz);
6683 decode_coding (&coding, lplogfont->lfFaceName, fontname,
6684 strlen(lplogfont->lfFaceName), bufsz - 1);
6685 *(fontname + coding.produced) = '\0';
4587b026
GV
6686
6687 /* Replace dashes with underscores so the dashes are not
f46e6225 6688 misinterpreted. */
4587b026
GV
6689 fontname_dash = fontname;
6690 while (fontname_dash = strchr (fontname_dash, '-'))
6691 *fontname_dash = '_';
6692
3cb20f4a 6693 if (lplogfont->lfHeight)
ee78dc32 6694 {
3cb20f4a
RS
6695 sprintf (height_pixels, "%u", abs (lplogfont->lfHeight));
6696 sprintf (height_dpi, "%u",
33d52f9c 6697 abs (lplogfont->lfHeight) * 720 / display_resy);
5ac45f98
GV
6698 }
6699 else
ee78dc32 6700 {
3cb20f4a
RS
6701 strcpy (height_pixels, "*");
6702 strcpy (height_dpi, "*");
ee78dc32 6703 }
3cb20f4a
RS
6704 if (lplogfont->lfWidth)
6705 sprintf (width_pixels, "%u", lplogfont->lfWidth * 10);
6706 else
6707 strcpy (width_pixels, "*");
6708
6709 _snprintf (lpxstr, len - 1,
6fc2811b
JR
6710 "-%s-%s-%s-%c-normal-normal-%s-%s-%d-%d-%c-%s-%s",
6711 fonttype, /* foundry */
4587b026
GV
6712 fontname, /* family */
6713 w32_to_x_weight (lplogfont->lfWeight), /* weight */
6714 lplogfont->lfItalic?'i':'r', /* slant */
6715 /* setwidth name */
6716 /* add style name */
6717 height_pixels, /* pixel size */
6718 height_dpi, /* point size */
33d52f9c
GV
6719 display_resx, /* resx */
6720 display_resy, /* resy */
4587b026
GV
6721 ((lplogfont->lfPitchAndFamily & 0x3) == VARIABLE_PITCH)
6722 ? 'p' : 'c', /* spacing */
6723 width_pixels, /* avg width */
767b1ff0 6724 specific_charset ? specific_charset
7d0393cf 6725 : w32_to_x_charset (lplogfont->lfCharSet)
767b1ff0 6726 /* charset registry and encoding */
3cb20f4a
RS
6727 );
6728
ee78dc32
GV
6729 lpxstr[len - 1] = 0; /* just to be sure */
6730 return (TRUE);
6731}
6732
7d0393cf 6733static BOOL
fbd6baed 6734x_to_w32_font (lpxstr, lplogfont)
ee78dc32
GV
6735 char * lpxstr;
6736 LOGFONT * lplogfont;
6737{
f46e6225
GV
6738 struct coding_system coding;
6739
ee78dc32 6740 if (!lplogfont) return (FALSE);
f46e6225 6741
ee78dc32 6742 memset (lplogfont, 0, sizeof (*lplogfont));
5ac45f98 6743
1a292d24 6744 /* Set default value for each field. */
771c47d5 6745#if 1
ee78dc32
GV
6746 lplogfont->lfOutPrecision = OUT_DEFAULT_PRECIS;
6747 lplogfont->lfClipPrecision = CLIP_DEFAULT_PRECIS;
6748 lplogfont->lfQuality = DEFAULT_QUALITY;
5ac45f98
GV
6749#else
6750 /* go for maximum quality */
6751 lplogfont->lfOutPrecision = OUT_STROKE_PRECIS;
6752 lplogfont->lfClipPrecision = CLIP_STROKE_PRECIS;
6753 lplogfont->lfQuality = PROOF_QUALITY;
6754#endif
6755
1a292d24
AI
6756 lplogfont->lfCharSet = DEFAULT_CHARSET;
6757 lplogfont->lfWeight = FW_DONTCARE;
6758 lplogfont->lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
6759
5ac45f98
GV
6760 if (!lpxstr)
6761 return FALSE;
6762
6763 /* Provide a simple escape mechanism for specifying Windows font names
6764 * directly -- if font spec does not beginning with '-', assume this
6765 * format:
6766 * "<font name>[:height in pixels[:width in pixels[:weight]]]"
6767 */
7d0393cf 6768
5ac45f98
GV
6769 if (*lpxstr == '-')
6770 {
33d52f9c
GV
6771 int fields, tem;
6772 char name[50], weight[20], slant, pitch, pixels[10], height[10],
8b77111c 6773 width[10], resy[10], remainder[50];
5ac45f98 6774 char * encoding;
ac849ba4 6775 int dpi = (int) one_w32_display_info.resy;
5ac45f98
GV
6776
6777 fields = sscanf (lpxstr,
8b77111c 6778 "-%*[^-]-%49[^-]-%19[^-]-%c-%*[^-]-%*[^-]-%9[^-]-%9[^-]-%*[^-]-%9[^-]-%c-%9[^-]-%49s",
33d52f9c 6779 name, weight, &slant, pixels, height, resy, &pitch, width, remainder);
8b77111c
AI
6780 if (fields == EOF)
6781 return (FALSE);
6782
6783 /* In the general case when wildcards cover more than one field,
6784 we don't know which field is which, so don't fill any in.
6785 However, we need to cope with this particular form, which is
6786 generated by font_list_1 (invoked by try_font_list):
6787 "-raster-6x10-*-gb2312*-*"
6788 and make sure to correctly parse the charset field. */
6789 if (fields == 3)
6790 {
6791 fields = sscanf (lpxstr,
6792 "-%*[^-]-%49[^-]-*-%49s",
6793 name, remainder);
6794 }
6795 else if (fields < 9)
6796 {
6797 fields = 0;
6798 remainder[0] = 0;
6799 }
6fc2811b 6800
5ac45f98
GV
6801 if (fields > 0 && name[0] != '*')
6802 {
8ea3e054
RS
6803 int bufsize;
6804 unsigned char *buf;
6805
f46e6225 6806 setup_coding_system
1fa3a200 6807 (Fcheck_coding_system (Vlocale_coding_system), &coding);
aab5ac44
KH
6808 coding.src_multibyte = 1;
6809 coding.dst_multibyte = 1;
8ea3e054
RS
6810 bufsize = encoding_buffer_size (&coding, strlen (name));
6811 buf = (unsigned char *) alloca (bufsize);
f46e6225 6812 coding.mode |= CODING_MODE_LAST_BLOCK;
8ea3e054
RS
6813 encode_coding (&coding, name, buf, strlen (name), bufsize);
6814 if (coding.produced >= LF_FACESIZE)
6815 coding.produced = LF_FACESIZE - 1;
6816 buf[coding.produced] = 0;
6817 strcpy (lplogfont->lfFaceName, buf);
5ac45f98
GV
6818 }
6819 else
6820 {
6fc2811b 6821 lplogfont->lfFaceName[0] = '\0';
5ac45f98
GV
6822 }
6823
6824 fields--;
6825
fbd6baed 6826 lplogfont->lfWeight = x_to_w32_weight ((fields > 0 ? weight : ""));
5ac45f98
GV
6827
6828 fields--;
6829
c8874f14 6830 lplogfont->lfItalic = (fields > 0 && slant == 'i');
5ac45f98
GV
6831
6832 fields--;
6833
6834 if (fields > 0 && pixels[0] != '*')
6835 lplogfont->lfHeight = atoi (pixels);
6836
6837 fields--;
5ac45f98 6838 fields--;
33d52f9c
GV
6839 if (fields > 0 && resy[0] != '*')
6840 {
6fc2811b 6841 tem = atoi (resy);
33d52f9c
GV
6842 if (tem > 0) dpi = tem;
6843 }
5ac45f98 6844
33d52f9c
GV
6845 if (fields > -1 && lplogfont->lfHeight == 0 && height[0] != '*')
6846 lplogfont->lfHeight = atoi (height) * dpi / 720;
6847
6848 if (fields > 0)
5ac45f98
GV
6849 lplogfont->lfPitchAndFamily =
6850 (fields > 0 && pitch == 'p') ? VARIABLE_PITCH : FIXED_PITCH;
6851
6852 fields--;
6853
6854 if (fields > 0 && width[0] != '*')
6855 lplogfont->lfWidth = atoi (width) / 10;
6856
6857 fields--;
6858
4587b026 6859 /* Strip the trailing '-' if present. (it shouldn't be, as it
d88c567c 6860 fails the test against xlfd-tight-regexp in fontset.el). */
3c190163 6861 {
5ac45f98
GV
6862 int len = strlen (remainder);
6863 if (len > 0 && remainder[len-1] == '-')
6864 remainder[len-1] = 0;
ee78dc32 6865 }
5ac45f98 6866 encoding = remainder;
8b77111c 6867#if 0
5ac45f98
GV
6868 if (strncmp (encoding, "*-", 2) == 0)
6869 encoding += 2;
8b77111c
AI
6870#endif
6871 lplogfont->lfCharSet = x_to_w32_charset (encoding);
5ac45f98
GV
6872 }
6873 else
6874 {
6875 int fields;
6876 char name[100], height[10], width[10], weight[20];
a1a80b40 6877
5ac45f98
GV
6878 fields = sscanf (lpxstr,
6879 "%99[^:]:%9[^:]:%9[^:]:%19s",
6880 name, height, width, weight);
6881
6882 if (fields == EOF) return (FALSE);
6883
6884 if (fields > 0)
6885 {
6886 strncpy (lplogfont->lfFaceName,name, LF_FACESIZE);
6887 lplogfont->lfFaceName[LF_FACESIZE-1] = 0;
6888 }
6889 else
6890 {
6891 lplogfont->lfFaceName[0] = 0;
6892 }
6893
6894 fields--;
6895
6896 if (fields > 0)
6897 lplogfont->lfHeight = atoi (height);
6898
6899 fields--;
6900
6901 if (fields > 0)
6902 lplogfont->lfWidth = atoi (width);
6903
6904 fields--;
6905
fbd6baed 6906 lplogfont->lfWeight = x_to_w32_weight ((fields > 0 ? weight : ""));
5ac45f98
GV
6907 }
6908
6909 /* This makes TrueType fonts work better. */
6910 lplogfont->lfHeight = - abs (lplogfont->lfHeight);
6fc2811b 6911
ee78dc32
GV
6912 return (TRUE);
6913}
6914
d88c567c
JR
6915/* Strip the pixel height and point height from the given xlfd, and
6916 return the pixel height. If no pixel height is specified, calculate
6917 one from the point height, or if that isn't defined either, return
6918 0 (which usually signifies a scalable font).
6919*/
8edb0a6f
JR
6920static int
6921xlfd_strip_height (char *fontname)
d88c567c 6922{
8edb0a6f 6923 int pixel_height, field_number;
d88c567c
JR
6924 char *read_from, *write_to;
6925
6926 xassert (fontname);
6927
6928 pixel_height = field_number = 0;
6929 write_to = NULL;
6930
6931 /* Look for height fields. */
6932 for (read_from = fontname; *read_from; read_from++)
6933 {
6934 if (*read_from == '-')
6935 {
6936 field_number++;
6937 if (field_number == 7) /* Pixel height. */
6938 {
6939 read_from++;
6940 write_to = read_from;
6941
6942 /* Find end of field. */
6943 for (;*read_from && *read_from != '-'; read_from++)
6944 ;
6945
6946 /* Split the fontname at end of field. */
6947 if (*read_from)
6948 {
6949 *read_from = '\0';
6950 read_from++;
6951 }
6952 pixel_height = atoi (write_to);
6953 /* Blank out field. */
6954 if (read_from > write_to)
6955 {
6956 *write_to = '-';
6957 write_to++;
6958 }
767b1ff0 6959 /* If the pixel height field is at the end (partial xlfd),
d88c567c
JR
6960 return now. */
6961 else
6962 return pixel_height;
6963
6964 /* If we got a pixel height, the point height can be
6965 ignored. Just blank it out and break now. */
6966 if (pixel_height)
6967 {
6968 /* Find end of point size field. */
6969 for (; *read_from && *read_from != '-'; read_from++)
6970 ;
6971
6972 if (*read_from)
6973 read_from++;
6974
6975 /* Blank out the point size field. */
6976 if (read_from > write_to)
6977 {
6978 *write_to = '-';
6979 write_to++;
6980 }
6981 else
6982 return pixel_height;
6983
6984 break;
6985 }
6986 /* If the point height is already blank, break now. */
6987 if (*read_from == '-')
6988 {
6989 read_from++;
6990 break;
6991 }
6992 }
6993 else if (field_number == 8)
6994 {
6995 /* If we didn't get a pixel height, try to get the point
6996 height and convert that. */
6997 int point_size;
6998 char *point_size_start = read_from++;
6999
7000 /* Find end of field. */
7001 for (; *read_from && *read_from != '-'; read_from++)
7002 ;
7003
7004 if (*read_from)
7005 {
7006 *read_from = '\0';
7007 read_from++;
7008 }
7009
7010 point_size = atoi (point_size_start);
7011
7012 /* Convert to pixel height. */
7013 pixel_height = point_size
7014 * one_w32_display_info.height_in / 720;
7015
7016 /* Blank out this field and break. */
7017 *write_to = '-';
7018 write_to++;
7019 break;
7020 }
7021 }
7022 }
7023
7024 /* Shift the rest of the font spec into place. */
7025 if (write_to && read_from > write_to)
7026 {
7027 for (; *read_from; read_from++, write_to++)
7028 *write_to = *read_from;
7029 *write_to = '\0';
7030 }
7031
7032 return pixel_height;
7033}
7034
6fc2811b 7035/* Assume parameter 1 is fully qualified, no wildcards. */
7d0393cf 7036static BOOL
6fc2811b
JR
7037w32_font_match (fontname, pattern)
7038 char * fontname;
7039 char * pattern;
ee78dc32 7040{
e7c72122 7041 char *regex = alloca (strlen (pattern) * 2 + 3);
d88c567c 7042 char *font_name_copy = alloca (strlen (fontname) + 1);
6fc2811b 7043 char *ptr;
ee78dc32 7044
d88c567c
JR
7045 /* Copy fontname so we can modify it during comparison. */
7046 strcpy (font_name_copy, fontname);
7047
6fc2811b
JR
7048 ptr = regex;
7049 *ptr++ = '^';
ee78dc32 7050
6fc2811b
JR
7051 /* Turn pattern into a regexp and do a regexp match. */
7052 for (; *pattern; pattern++)
7053 {
7054 if (*pattern == '?')
7055 *ptr++ = '.';
7056 else if (*pattern == '*')
7057 {
7058 *ptr++ = '.';
7059 *ptr++ = '*';
7060 }
33d52f9c 7061 else
6fc2811b 7062 *ptr++ = *pattern;
ee78dc32 7063 }
6fc2811b
JR
7064 *ptr = '$';
7065 *(ptr + 1) = '\0';
7066
d88c567c
JR
7067 /* Strip out font heights and compare them seperately, since
7068 rounding error can cause mismatches. This also allows a
7069 comparison between a font that declares only a pixel height and a
7070 pattern that declares the point height.
7071 */
7072 {
7073 int font_height, pattern_height;
7074
7075 font_height = xlfd_strip_height (font_name_copy);
7076 pattern_height = xlfd_strip_height (regex);
7077
7078 /* Compare now, and don't bother doing expensive regexp matching
7079 if the heights differ. */
7080 if (font_height && pattern_height && (font_height != pattern_height))
7081 return FALSE;
7082 }
7083
6fc2811b 7084 return (fast_c_string_match_ignore_case (build_string (regex),
d88c567c 7085 font_name_copy) >= 0);
ee78dc32
GV
7086}
7087
5ca0cd71
GV
7088/* Callback functions, and a structure holding info they need, for
7089 listing system fonts on W32. We need one set of functions to do the
7090 job properly, but these don't work on NT 3.51 and earlier, so we
7091 have a second set which don't handle character sets properly to
7092 fall back on.
7093
7094 In both cases, there are two passes made. The first pass gets one
7095 font from each family, the second pass lists all the fonts from
7096 each family. */
7097
7d0393cf 7098typedef struct enumfont_t
ee78dc32
GV
7099{
7100 HDC hdc;
7101 int numFonts;
3cb20f4a 7102 LOGFONT logfont;
ee78dc32 7103 XFontStruct *size_ref;
23afac8f 7104 Lisp_Object pattern;
d84b082d 7105 Lisp_Object list;
ee78dc32
GV
7106} enumfont_t;
7107
d84b082d
JR
7108
7109static void
7110enum_font_maybe_add_to_list (enumfont_t *, LOGFONT *, char *, Lisp_Object);
7111
7112
7d0393cf 7113static int CALLBACK
ee78dc32
GV
7114enum_font_cb2 (lplf, lptm, FontType, lpef)
7115 ENUMLOGFONT * lplf;
7116 NEWTEXTMETRIC * lptm;
7117 int FontType;
7118 enumfont_t * lpef;
7119{
66895301
JR
7120 /* Ignore struck out and underlined versions of fonts. */
7121 if (lplf->elfLogFont.lfStrikeOut || lplf->elfLogFont.lfUnderline)
7122 return 1;
7123
7124 /* Only return fonts with names starting with @ if they were
7125 explicitly specified, since Microsoft uses an initial @ to
7126 denote fonts for vertical writing, without providing a more
7127 convenient way of identifying them. */
7128 if (lplf->elfLogFont.lfFaceName[0] == '@'
7129 && lpef->logfont.lfFaceName[0] != '@')
5e905a57
JR
7130 return 1;
7131
4587b026
GV
7132 /* Check that the character set matches if it was specified */
7133 if (lpef->logfont.lfCharSet != DEFAULT_CHARSET &&
7134 lplf->elfLogFont.lfCharSet != lpef->logfont.lfCharSet)
5e905a57 7135 return 1;
4587b026 7136
6358474d
JR
7137 if (FontType == RASTER_FONTTYPE)
7138 {
7139 /* DBCS raster fonts have problems displaying, so skip them. */
7140 int charset = lplf->elfLogFont.lfCharSet;
7141 if (charset == SHIFTJIS_CHARSET
7142 || charset == HANGEUL_CHARSET
7143 || charset == CHINESEBIG5_CHARSET
7144 || charset == GB2312_CHARSET
7145#ifdef JOHAB_CHARSET
7146 || charset == JOHAB_CHARSET
7147#endif
7148 )
7149 return 1;
7150 }
7151
ee78dc32
GV
7152 {
7153 char buf[100];
4587b026 7154 Lisp_Object width = Qnil;
d84b082d 7155 Lisp_Object charset_list = Qnil;
767b1ff0 7156 char *charset = NULL;
ee78dc32 7157
6fc2811b
JR
7158 /* Truetype fonts do not report their true metrics until loaded */
7159 if (FontType != RASTER_FONTTYPE)
3cb20f4a 7160 {
23afac8f 7161 if (!NILP (lpef->pattern))
6fc2811b
JR
7162 {
7163 /* Scalable fonts are as big as you want them to be. */
7164 lplf->elfLogFont.lfHeight = lpef->logfont.lfHeight;
7165 lplf->elfLogFont.lfWidth = lpef->logfont.lfWidth;
7166 width = make_number (lpef->logfont.lfWidth);
7167 }
7168 else
7169 {
7170 lplf->elfLogFont.lfHeight = 0;
7171 lplf->elfLogFont.lfWidth = 0;
7172 }
3cb20f4a 7173 }
6fc2811b 7174
f46e6225
GV
7175 /* Make sure the height used here is the same as everywhere
7176 else (ie character height, not cell height). */
6fc2811b
JR
7177 if (lplf->elfLogFont.lfHeight > 0)
7178 {
7179 /* lptm can be trusted for RASTER fonts, but not scalable ones. */
7180 if (FontType == RASTER_FONTTYPE)
7181 lplf->elfLogFont.lfHeight = lptm->tmInternalLeading - lptm->tmHeight;
7182 else
7183 lplf->elfLogFont.lfHeight = -lplf->elfLogFont.lfHeight;
7184 }
4587b026 7185
23afac8f 7186 if (!NILP (lpef->pattern))
767b1ff0 7187 {
d5db4077 7188 charset = xlfd_charset_of_font (SDATA (lpef->pattern));
767b1ff0 7189
644cefdf
JR
7190 /* We already checked charsets above, but DEFAULT_CHARSET
7191 slipped through. So only allow exact matches for DEFAULT_CHARSET. */
7192 if (charset
7193 && strncmp (charset, "*-*", 3) != 0
7194 && lpef->logfont.lfCharSet == DEFAULT_CHARSET
7195 && strcmp (charset, w32_to_x_charset (DEFAULT_CHARSET)) != 0)
7196 return 1;
767b1ff0
JR
7197 }
7198
d84b082d
JR
7199 if (charset)
7200 charset_list = Fcons (build_string (charset), Qnil);
7201 else
7202 charset_list = w32_to_all_x_charsets (lplf->elfLogFont.lfCharSet);
ee78dc32 7203
d84b082d
JR
7204 /* Loop through the charsets. */
7205 for ( ; CONSP (charset_list); charset_list = Fcdr (charset_list))
ee78dc32 7206 {
d84b082d 7207 Lisp_Object this_charset = Fcar (charset_list);
d5db4077 7208 charset = SDATA (this_charset);
d84b082d
JR
7209
7210 /* List bold and italic variations if w32-enable-synthesized-fonts
7211 is non-nil and this is a plain font. */
7212 if (w32_enable_synthesized_fonts
7213 && lplf->elfLogFont.lfWeight == FW_NORMAL
7214 && lplf->elfLogFont.lfItalic == FALSE)
7215 {
7216 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
7217 charset, width);
7218 /* bold. */
7219 lplf->elfLogFont.lfWeight = FW_BOLD;
7220 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
7221 charset, width);
7222 /* bold italic. */
7223 lplf->elfLogFont.lfItalic = TRUE;
7224 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
7225 charset, width);
7226 /* italic. */
7227 lplf->elfLogFont.lfWeight = FW_NORMAL;
7228 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
7229 charset, width);
7230 }
7231 else
7232 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
7233 charset, width);
ee78dc32
GV
7234 }
7235 }
6fc2811b 7236
5e905a57 7237 return 1;
ee78dc32
GV
7238}
7239
d84b082d
JR
7240static void
7241enum_font_maybe_add_to_list (lpef, logfont, match_charset, width)
7242 enumfont_t * lpef;
7243 LOGFONT * logfont;
7244 char * match_charset;
7245 Lisp_Object width;
7246{
7247 char buf[100];
7248
7249 if (!w32_to_x_font (logfont, buf, 100, match_charset))
7250 return;
7251
23afac8f 7252 if (NILP (lpef->pattern)
d5db4077 7253 || w32_font_match (buf, SDATA (lpef->pattern)))
d84b082d
JR
7254 {
7255 /* Check if we already listed this font. This may happen if
7256 w32_enable_synthesized_fonts is non-nil, and there are real
7257 bold and italic versions of the font. */
7258 Lisp_Object font_name = build_string (buf);
7259 if (NILP (Fmember (font_name, lpef->list)))
7260 {
23afac8f
JR
7261 Lisp_Object entry = Fcons (font_name, width);
7262 lpef->list = Fcons (entry, lpef->list);
d84b082d
JR
7263 lpef->numFonts++;
7264 }
7265 }
7266}
7267
7268
7d0393cf 7269static int CALLBACK
ee78dc32
GV
7270enum_font_cb1 (lplf, lptm, FontType, lpef)
7271 ENUMLOGFONT * lplf;
7272 NEWTEXTMETRIC * lptm;
7273 int FontType;
7274 enumfont_t * lpef;
7275{
7276 return EnumFontFamilies (lpef->hdc,
7277 lplf->elfLogFont.lfFaceName,
7278 (FONTENUMPROC) enum_font_cb2,
7279 (LPARAM) lpef);
7280}
7281
7282
8edb0a6f 7283static int CALLBACK
5ca0cd71
GV
7284enum_fontex_cb2 (lplf, lptm, font_type, lpef)
7285 ENUMLOGFONTEX * lplf;
7286 NEWTEXTMETRICEX * lptm;
7287 int font_type;
7288 enumfont_t * lpef;
7289{
7290 /* We are not interested in the extra info we get back from the 'Ex
7291 version - only the fact that we get character set variations
7292 enumerated seperately. */
7293 return enum_font_cb2 ((ENUMLOGFONT *) lplf, (NEWTEXTMETRIC *) lptm,
7294 font_type, lpef);
7295}
7296
8edb0a6f 7297static int CALLBACK
5ca0cd71
GV
7298enum_fontex_cb1 (lplf, lptm, font_type, lpef)
7299 ENUMLOGFONTEX * lplf;
7300 NEWTEXTMETRICEX * lptm;
7301 int font_type;
7302 enumfont_t * lpef;
7303{
7304 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
7305 FARPROC enum_font_families_ex
7306 = GetProcAddress ( gdi32, "EnumFontFamiliesExA");
7307 /* We don't really expect EnumFontFamiliesEx to disappear once we
7308 get here, so don't bother handling it gracefully. */
7309 if (enum_font_families_ex == NULL)
7310 error ("gdi32.dll has disappeared!");
7311 return enum_font_families_ex (lpef->hdc,
7312 &lplf->elfLogFont,
7313 (FONTENUMPROC) enum_fontex_cb2,
7314 (LPARAM) lpef, 0);
7315}
7316
4587b026
GV
7317/* Interface to fontset handler. (adapted from mw32font.c in Meadow
7318 and xterm.c in Emacs 20.3) */
7319
8edb0a6f 7320static Lisp_Object w32_list_bdf_fonts (Lisp_Object pattern, int max_names)
33d52f9c
GV
7321{
7322 char *fontname, *ptnstr;
7323 Lisp_Object list, tem, newlist = Qnil;
55dcfc15 7324 int n_fonts = 0;
33d52f9c
GV
7325
7326 list = Vw32_bdf_filename_alist;
d5db4077 7327 ptnstr = SDATA (pattern);
33d52f9c 7328
8e713be6 7329 for ( ; CONSP (list); list = XCDR (list))
33d52f9c 7330 {
8e713be6 7331 tem = XCAR (list);
33d52f9c 7332 if (CONSP (tem))
d5db4077 7333 fontname = SDATA (XCAR (tem));
33d52f9c 7334 else if (STRINGP (tem))
d5db4077 7335 fontname = SDATA (tem);
33d52f9c
GV
7336 else
7337 continue;
7338
7339 if (w32_font_match (fontname, ptnstr))
5ca0cd71 7340 {
8e713be6 7341 newlist = Fcons (XCAR (tem), newlist);
5ca0cd71
GV
7342 n_fonts++;
7343 if (n_fonts >= max_names)
7344 break;
7345 }
33d52f9c
GV
7346 }
7347
7348 return newlist;
7349}
7350
5ca0cd71 7351
4587b026
GV
7352/* Return a list of names of available fonts matching PATTERN on frame
7353 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
7354 to be listed. Frame F NULL means we have not yet created any
7355 frame, which means we can't get proper size info, as we don't have
7356 a device context to use for GetTextMetrics.
7357 MAXNAMES sets a limit on how many fonts to match. */
7358
7359Lisp_Object
dc220243
JR
7360w32_list_fonts (f, pattern, size, maxnames)
7361 struct frame *f;
7362 Lisp_Object pattern;
7363 int size;
7364 int maxnames;
4587b026 7365{
6fc2811b 7366 Lisp_Object patterns, key = Qnil, tem, tpat;
4587b026 7367 Lisp_Object list = Qnil, newlist = Qnil, second_best = Qnil;
33d52f9c 7368 struct w32_display_info *dpyinfo = &one_w32_display_info;
5ca0cd71 7369 int n_fonts = 0;
396594fe 7370
4587b026
GV
7371 patterns = Fassoc (pattern, Valternate_fontname_alist);
7372 if (NILP (patterns))
7373 patterns = Fcons (pattern, Qnil);
7374
8e713be6 7375 for (; CONSP (patterns); patterns = XCDR (patterns))
4587b026
GV
7376 {
7377 enumfont_t ef;
767b1ff0 7378 int codepage;
4587b026 7379
8e713be6 7380 tpat = XCAR (patterns);
4587b026 7381
767b1ff0
JR
7382 if (!STRINGP (tpat))
7383 continue;
7384
7385 /* Avoid expensive EnumFontFamilies functions if we are not
7386 going to be able to output one of these anyway. */
d5db4077 7387 codepage = w32_codepage_for_font (SDATA (tpat));
767b1ff0 7388 if (codepage != CP_8BIT && codepage != CP_UNICODE
ceb12877
AI
7389 && codepage != CP_DEFAULT && codepage != CP_UNKNOWN
7390 && !IsValidCodePage(codepage))
767b1ff0
JR
7391 continue;
7392
4587b026
GV
7393 /* See if we cached the result for this particular query.
7394 The cache is an alist of the form:
7395 ((PATTERN (FONTNAME . WIDTH) ...) ...)
7396 */
8e713be6 7397 if (tem = XCDR (dpyinfo->name_list_element),
33d52f9c 7398 !NILP (list = Fassoc (tpat, tem)))
4587b026
GV
7399 {
7400 list = Fcdr_safe (list);
7401 /* We have a cached list. Don't have to get the list again. */
7402 goto label_cached;
7403 }
7404
7405 BLOCK_INPUT;
7406 /* At first, put PATTERN in the cache. */
23afac8f
JR
7407 ef.pattern = tpat;
7408 ef.list = Qnil;
4587b026 7409 ef.numFonts = 0;
33d52f9c 7410
5ca0cd71
GV
7411 /* Use EnumFontFamiliesEx where it is available, as it knows
7412 about character sets. Fall back to EnumFontFamilies for
7413 older versions of NT that don't support the 'Ex function. */
d5db4077 7414 x_to_w32_font (SDATA (tpat), &ef.logfont);
4587b026 7415 {
5ca0cd71
GV
7416 LOGFONT font_match_pattern;
7417 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
7418 FARPROC enum_font_families_ex
7419 = GetProcAddress ( gdi32, "EnumFontFamiliesExA");
7420
7421 /* We do our own pattern matching so we can handle wildcards. */
7422 font_match_pattern.lfFaceName[0] = 0;
7423 font_match_pattern.lfPitchAndFamily = 0;
7424 /* We can use the charset, because if it is a wildcard it will
7425 be DEFAULT_CHARSET anyway. */
7426 font_match_pattern.lfCharSet = ef.logfont.lfCharSet;
7427
33d52f9c 7428 ef.hdc = GetDC (dpyinfo->root_window);
4587b026 7429
5ca0cd71
GV
7430 if (enum_font_families_ex)
7431 enum_font_families_ex (ef.hdc,
7432 &font_match_pattern,
7433 (FONTENUMPROC) enum_fontex_cb1,
7434 (LPARAM) &ef, 0);
7435 else
7436 EnumFontFamilies (ef.hdc, NULL, (FONTENUMPROC) enum_font_cb1,
7437 (LPARAM)&ef);
4587b026 7438
33d52f9c 7439 ReleaseDC (dpyinfo->root_window, ef.hdc);
4587b026
GV
7440 }
7441
7442 UNBLOCK_INPUT;
23afac8f 7443 list = ef.list;
4587b026
GV
7444
7445 /* Make a list of the fonts we got back.
7446 Store that in the font cache for the display. */
f3fbd155
KR
7447 XSETCDR (dpyinfo->name_list_element,
7448 Fcons (Fcons (tpat, list),
7449 XCDR (dpyinfo->name_list_element)));
4587b026
GV
7450
7451 label_cached:
7452 if (NILP (list)) continue; /* Try the remaining alternatives. */
7453
7454 newlist = second_best = Qnil;
7455
7d0393cf 7456 /* Make a list of the fonts that have the right width. */
8e713be6 7457 for (; CONSP (list); list = XCDR (list))
4587b026
GV
7458 {
7459 int found_size;
8e713be6 7460 tem = XCAR (list);
4587b026
GV
7461
7462 if (!CONSP (tem))
7463 continue;
8e713be6 7464 if (NILP (XCAR (tem)))
4587b026
GV
7465 continue;
7466 if (!size)
7467 {
8e713be6 7468 newlist = Fcons (XCAR (tem), newlist);
5ca0cd71
GV
7469 n_fonts++;
7470 if (n_fonts >= maxnames)
7471 break;
7472 else
7473 continue;
4587b026 7474 }
8e713be6 7475 if (!INTEGERP (XCDR (tem)))
4587b026
GV
7476 {
7477 /* Since we don't yet know the size of the font, we must
7478 load it and try GetTextMetrics. */
4587b026
GV
7479 W32FontStruct thisinfo;
7480 LOGFONT lf;
7481 HDC hdc;
7482 HANDLE oldobj;
7483
d5db4077 7484 if (!x_to_w32_font (SDATA (XCAR (tem)), &lf))
4587b026
GV
7485 continue;
7486
7487 BLOCK_INPUT;
33d52f9c 7488 thisinfo.bdf = NULL;
4587b026
GV
7489 thisinfo.hfont = CreateFontIndirect (&lf);
7490 if (thisinfo.hfont == NULL)
7491 continue;
7492
7493 hdc = GetDC (dpyinfo->root_window);
7494 oldobj = SelectObject (hdc, thisinfo.hfont);
7495 if (GetTextMetrics (hdc, &thisinfo.tm))
f3fbd155 7496 XSETCDR (tem, make_number (FONT_WIDTH (&thisinfo)));
4587b026 7497 else
f3fbd155 7498 XSETCDR (tem, make_number (0));
4587b026
GV
7499 SelectObject (hdc, oldobj);
7500 ReleaseDC (dpyinfo->root_window, hdc);
7501 DeleteObject(thisinfo.hfont);
7502 UNBLOCK_INPUT;
7503 }
8e713be6 7504 found_size = XINT (XCDR (tem));
4587b026 7505 if (found_size == size)
5ca0cd71 7506 {
8e713be6 7507 newlist = Fcons (XCAR (tem), newlist);
5ca0cd71
GV
7508 n_fonts++;
7509 if (n_fonts >= maxnames)
7510 break;
7511 }
4587b026
GV
7512 /* keep track of the closest matching size in case
7513 no exact match is found. */
7514 else if (found_size > 0)
7515 {
7516 if (NILP (second_best))
7517 second_best = tem;
7d0393cf 7518
4587b026
GV
7519 else if (found_size < size)
7520 {
8e713be6
KR
7521 if (XINT (XCDR (second_best)) > size
7522 || XINT (XCDR (second_best)) < found_size)
4587b026
GV
7523 second_best = tem;
7524 }
7525 else
7526 {
8e713be6
KR
7527 if (XINT (XCDR (second_best)) > size
7528 && XINT (XCDR (second_best)) >
4587b026
GV
7529 found_size)
7530 second_best = tem;
7531 }
7532 }
7533 }
7534
7535 if (!NILP (newlist))
7536 break;
7537 else if (!NILP (second_best))
7538 {
8e713be6 7539 newlist = Fcons (XCAR (second_best), Qnil);
4587b026
GV
7540 break;
7541 }
7542 }
7543
33d52f9c 7544 /* Include any bdf fonts. */
5ca0cd71 7545 if (n_fonts < maxnames)
33d52f9c
GV
7546 {
7547 Lisp_Object combined[2];
5ca0cd71 7548 combined[0] = w32_list_bdf_fonts (pattern, maxnames - n_fonts);
33d52f9c
GV
7549 combined[1] = newlist;
7550 newlist = Fnconc(2, combined);
7551 }
7552
4587b026
GV
7553 return newlist;
7554}
7555
5ca0cd71 7556
4587b026
GV
7557/* Return a pointer to struct font_info of font FONT_IDX of frame F. */
7558struct font_info *
7559w32_get_font_info (f, font_idx)
7560 FRAME_PTR f;
7561 int font_idx;
7562{
7563 return (FRAME_W32_FONT_TABLE (f) + font_idx);
7564}
7565
7566
7567struct font_info*
7568w32_query_font (struct frame *f, char *fontname)
7569{
7570 int i;
7571 struct font_info *pfi;
7572
7573 pfi = FRAME_W32_FONT_TABLE (f);
7574
7575 for (i = 0; i < one_w32_display_info.n_fonts ;i++, pfi++)
7576 {
7577 if (strcmp(pfi->name, fontname) == 0) return pfi;
7578 }
7579
7580 return NULL;
7581}
7582
7583/* Find a CCL program for a font specified by FONTP, and set the member
7584 `encoder' of the structure. */
7585
7586void
7587w32_find_ccl_program (fontp)
7588 struct font_info *fontp;
7589{
3545439c 7590 Lisp_Object list, elt;
4587b026 7591
8e713be6 7592 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
4587b026 7593 {
8e713be6 7594 elt = XCAR (list);
4587b026 7595 if (CONSP (elt)
8e713be6
KR
7596 && STRINGP (XCAR (elt))
7597 && (fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
4587b026 7598 >= 0))
3545439c
KH
7599 break;
7600 }
7601 if (! NILP (list))
7602 {
17eedd00
KH
7603 struct ccl_program *ccl
7604 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
3545439c 7605
8e713be6 7606 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
3545439c
KH
7607 xfree (ccl);
7608 else
7609 fontp->font_encoder = ccl;
4587b026
GV
7610 }
7611}
7612
7613\f
8edb0a6f
JR
7614/* Find BDF files in a specified directory. (use GCPRO when calling,
7615 as this calls lisp to get a directory listing). */
7616static Lisp_Object
7617w32_find_bdf_fonts_in_dir (Lisp_Object directory)
7618{
7619 Lisp_Object filelist, list = Qnil;
7620 char fontname[100];
7621
7622 if (!STRINGP(directory))
7623 return Qnil;
7624
7625 filelist = Fdirectory_files (directory, Qt,
7626 build_string (".*\\.[bB][dD][fF]"), Qt);
7627
7628 for ( ; CONSP(filelist); filelist = XCDR (filelist))
7629 {
7630 Lisp_Object filename = XCAR (filelist);
d5db4077 7631 if (w32_BDF_to_x_font (SDATA (filename), fontname, 100))
8edb0a6f
JR
7632 store_in_alist (&list, build_string (fontname), filename);
7633 }
7634 return list;
7635}
7636
6fc2811b
JR
7637DEFUN ("w32-find-bdf-fonts", Fw32_find_bdf_fonts, Sw32_find_bdf_fonts,
7638 1, 1, 0,
b3700ae7
JR
7639 doc: /* Return a list of BDF fonts in DIR.
7640The list is suitable for appending to w32-bdf-filename-alist. Fonts
7641which do not contain an xlfd description will not be included in the
7642list. DIR may be a list of directories. */)
6fc2811b
JR
7643 (directory)
7644 Lisp_Object directory;
7645{
7646 Lisp_Object list = Qnil;
7647 struct gcpro gcpro1, gcpro2;
ee78dc32 7648
6fc2811b
JR
7649 if (!CONSP (directory))
7650 return w32_find_bdf_fonts_in_dir (directory);
ee78dc32 7651
6fc2811b 7652 for ( ; CONSP (directory); directory = XCDR (directory))
ee78dc32 7653 {
6fc2811b
JR
7654 Lisp_Object pair[2];
7655 pair[0] = list;
7656 pair[1] = Qnil;
7657 GCPRO2 (directory, list);
7658 pair[1] = w32_find_bdf_fonts_in_dir( XCAR (directory) );
7659 list = Fnconc( 2, pair );
7660 UNGCPRO;
7661 }
7662 return list;
7663}
ee78dc32 7664
6fc2811b
JR
7665\f
7666DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
74e1aeec 7667 doc: /* Internal function called by `color-defined-p', which see. */)
6fc2811b
JR
7668 (color, frame)
7669 Lisp_Object color, frame;
7670{
7671 XColor foo;
7672 FRAME_PTR f = check_x_frame (frame);
ee78dc32 7673
b7826503 7674 CHECK_STRING (color);
ee78dc32 7675
d5db4077 7676 if (w32_defined_color (f, SDATA (color), &foo, 0))
6fc2811b
JR
7677 return Qt;
7678 else
7679 return Qnil;
7680}
ee78dc32 7681
2d764c78 7682DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
74e1aeec 7683 doc: /* Internal function called by `color-values', which see. */)
ee78dc32
GV
7684 (color, frame)
7685 Lisp_Object color, frame;
7686{
6fc2811b 7687 XColor foo;
ee78dc32
GV
7688 FRAME_PTR f = check_x_frame (frame);
7689
b7826503 7690 CHECK_STRING (color);
ee78dc32 7691
d5db4077 7692 if (w32_defined_color (f, SDATA (color), &foo, 0))
ee78dc32
GV
7693 {
7694 Lisp_Object rgb[3];
7695
6fc2811b
JR
7696 rgb[0] = make_number ((GetRValue (foo.pixel) << 8)
7697 | GetRValue (foo.pixel));
7698 rgb[1] = make_number ((GetGValue (foo.pixel) << 8)
7699 | GetGValue (foo.pixel));
7700 rgb[2] = make_number ((GetBValue (foo.pixel) << 8)
7701 | GetBValue (foo.pixel));
ee78dc32
GV
7702 return Flist (3, rgb);
7703 }
7704 else
7705 return Qnil;
7706}
7707
2d764c78 7708DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
74e1aeec 7709 doc: /* Internal function called by `display-color-p', which see. */)
ee78dc32
GV
7710 (display)
7711 Lisp_Object display;
7712{
fbd6baed 7713 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
7714
7715 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 2)
7716 return Qnil;
7717
7718 return Qt;
7719}
7720
74e1aeec
JR
7721DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p,
7722 Sx_display_grayscale_p, 0, 1, 0,
7723 doc: /* Return t if the X display supports shades of gray.
7724Note that color displays do support shades of gray.
7725The optional argument DISPLAY specifies which display to ask about.
7726DISPLAY should be either a frame or a display name (a string).
7727If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
7728 (display)
7729 Lisp_Object display;
7730{
fbd6baed 7731 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
7732
7733 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 1)
7734 return Qnil;
7735
7736 return Qt;
7737}
7738
74e1aeec
JR
7739DEFUN ("x-display-pixel-width", Fx_display_pixel_width,
7740 Sx_display_pixel_width, 0, 1, 0,
7741 doc: /* Returns the width in pixels of DISPLAY.
7742The optional argument DISPLAY specifies which display to ask about.
7743DISPLAY should be either a frame or a display name (a string).
7744If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
7745 (display)
7746 Lisp_Object display;
7747{
fbd6baed 7748 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
7749
7750 return make_number (dpyinfo->width);
7751}
7752
7753DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
74e1aeec
JR
7754 Sx_display_pixel_height, 0, 1, 0,
7755 doc: /* Returns the height in pixels of DISPLAY.
7756The optional argument DISPLAY specifies which display to ask about.
7757DISPLAY should be either a frame or a display name (a string).
7758If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
7759 (display)
7760 Lisp_Object display;
7761{
fbd6baed 7762 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
7763
7764 return make_number (dpyinfo->height);
7765}
7766
7767DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
74e1aeec
JR
7768 0, 1, 0,
7769 doc: /* Returns the number of bitplanes of DISPLAY.
7770The optional argument DISPLAY specifies which display to ask about.
7771DISPLAY should be either a frame or a display name (a string).
7772If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
7773 (display)
7774 Lisp_Object display;
7775{
fbd6baed 7776 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
7777
7778 return make_number (dpyinfo->n_planes * dpyinfo->n_cbits);
7779}
7780
7781DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
74e1aeec
JR
7782 0, 1, 0,
7783 doc: /* Returns the number of color cells of DISPLAY.
7784The optional argument DISPLAY specifies which display to ask about.
7785DISPLAY should be either a frame or a display name (a string).
7786If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
7787 (display)
7788 Lisp_Object display;
7789{
fbd6baed 7790 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
7791 HDC hdc;
7792 int cap;
7793
5ac45f98
GV
7794 hdc = GetDC (dpyinfo->root_window);
7795 if (dpyinfo->has_palette)
7796 cap = GetDeviceCaps (hdc,SIZEPALETTE);
7797 else
7798 cap = GetDeviceCaps (hdc,NUMCOLORS);
abf8c61b 7799
007776bc
JB
7800 /* We force 24+ bit depths to 24-bit, both to prevent an overflow
7801 and because probably is more meaningful on Windows anyway */
abf8c61b 7802 if (cap < 0)
007776bc 7803 cap = 1 << min(dpyinfo->n_planes * dpyinfo->n_cbits, 24);
7d0393cf 7804
ee78dc32 7805 ReleaseDC (dpyinfo->root_window, hdc);
7d0393cf 7806
ee78dc32
GV
7807 return make_number (cap);
7808}
7809
7810DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
7811 Sx_server_max_request_size,
74e1aeec
JR
7812 0, 1, 0,
7813 doc: /* Returns the maximum request size of the server of DISPLAY.
7814The optional argument DISPLAY specifies which display to ask about.
7815DISPLAY should be either a frame or a display name (a string).
7816If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
7817 (display)
7818 Lisp_Object display;
7819{
fbd6baed 7820 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
7821
7822 return make_number (1);
7823}
7824
7825DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
74e1aeec
JR
7826 doc: /* Returns the vendor ID string of the W32 system (Microsoft).
7827The optional argument DISPLAY specifies which display to ask about.
7828DISPLAY should be either a frame or a display name (a string).
7829If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
7830 (display)
7831 Lisp_Object display;
7832{
dfff8a69 7833 return build_string ("Microsoft Corp.");
ee78dc32
GV
7834}
7835
7836DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
74e1aeec
JR
7837 doc: /* Returns the version numbers of the server of DISPLAY.
7838The value is a list of three integers: the major and minor
7839version numbers, and the vendor-specific release
7840number. See also the function `x-server-vendor'.
7841
7842The optional argument DISPLAY specifies which display to ask about.
7843DISPLAY should be either a frame or a display name (a string).
7844If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
7845 (display)
7846 Lisp_Object display;
7847{
fbd6baed 7848 return Fcons (make_number (w32_major_version),
58e0f0e4
AI
7849 Fcons (make_number (w32_minor_version),
7850 Fcons (make_number (w32_build_number), Qnil)));
ee78dc32
GV
7851}
7852
7853DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
74e1aeec
JR
7854 doc: /* Returns the number of screens on the server of DISPLAY.
7855The optional argument DISPLAY specifies which display to ask about.
7856DISPLAY should be either a frame or a display name (a string).
7857If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
7858 (display)
7859 Lisp_Object display;
7860{
ee78dc32
GV
7861 return make_number (1);
7862}
7863
74e1aeec
JR
7864DEFUN ("x-display-mm-height", Fx_display_mm_height,
7865 Sx_display_mm_height, 0, 1, 0,
7866 doc: /* Returns the height in millimeters of DISPLAY.
7867The optional argument DISPLAY specifies which display to ask about.
7868DISPLAY should be either a frame or a display name (a string).
7869If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
7870 (display)
7871 Lisp_Object display;
7872{
fbd6baed 7873 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
7874 HDC hdc;
7875 int cap;
7876
5ac45f98 7877 hdc = GetDC (dpyinfo->root_window);
7d0393cf 7878
ee78dc32 7879 cap = GetDeviceCaps (hdc, VERTSIZE);
7d0393cf 7880
ee78dc32 7881 ReleaseDC (dpyinfo->root_window, hdc);
7d0393cf 7882
ee78dc32
GV
7883 return make_number (cap);
7884}
7885
7886DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
74e1aeec
JR
7887 doc: /* Returns the width in millimeters of DISPLAY.
7888The optional argument DISPLAY specifies which display to ask about.
7889DISPLAY should be either a frame or a display name (a string).
7890If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
7891 (display)
7892 Lisp_Object display;
7893{
fbd6baed 7894 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
7895
7896 HDC hdc;
7897 int cap;
7898
5ac45f98 7899 hdc = GetDC (dpyinfo->root_window);
7d0393cf 7900
ee78dc32 7901 cap = GetDeviceCaps (hdc, HORZSIZE);
7d0393cf 7902
ee78dc32 7903 ReleaseDC (dpyinfo->root_window, hdc);
7d0393cf 7904
ee78dc32
GV
7905 return make_number (cap);
7906}
7907
7908DEFUN ("x-display-backing-store", Fx_display_backing_store,
74e1aeec
JR
7909 Sx_display_backing_store, 0, 1, 0,
7910 doc: /* Returns an indication of whether DISPLAY does backing store.
7911The value may be `always', `when-mapped', or `not-useful'.
7912The optional argument DISPLAY specifies which display to ask about.
7913DISPLAY should be either a frame or a display name (a string).
7914If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
7915 (display)
7916 Lisp_Object display;
7917{
7918 return intern ("not-useful");
7919}
7920
7921DEFUN ("x-display-visual-class", Fx_display_visual_class,
74e1aeec
JR
7922 Sx_display_visual_class, 0, 1, 0,
7923 doc: /* Returns the visual class of DISPLAY.
7924The value is one of the symbols `static-gray', `gray-scale',
7925`static-color', `pseudo-color', `true-color', or `direct-color'.
7926
7927The optional argument DISPLAY specifies which display to ask about.
7928DISPLAY should be either a frame or a display name (a string).
7929If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
7930 (display)
7931 Lisp_Object display;
7932{
fbd6baed 7933 struct w32_display_info *dpyinfo = check_x_display_info (display);
abf8c61b 7934 Lisp_Object result = Qnil;
ee78dc32 7935
abf8c61b
AI
7936 if (dpyinfo->has_palette)
7937 result = intern ("pseudo-color");
7938 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 1)
7939 result = intern ("static-grey");
7940 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 4)
7941 result = intern ("static-color");
7942 else if (dpyinfo->n_planes * dpyinfo->n_cbits > 8)
7943 result = intern ("true-color");
ee78dc32 7944
abf8c61b 7945 return result;
ee78dc32
GV
7946}
7947
7948DEFUN ("x-display-save-under", Fx_display_save_under,
74e1aeec
JR
7949 Sx_display_save_under, 0, 1, 0,
7950 doc: /* Returns t if DISPLAY supports the save-under feature.
7951The optional argument DISPLAY specifies which display to ask about.
7952DISPLAY should be either a frame or a display name (a string).
7953If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
7954 (display)
7955 Lisp_Object display;
7956{
6fc2811b
JR
7957 return Qnil;
7958}
7959\f
7960int
7961x_pixel_width (f)
7962 register struct frame *f;
7963{
7964 return PIXEL_WIDTH (f);
7965}
7966
7967int
7968x_pixel_height (f)
7969 register struct frame *f;
7970{
7971 return PIXEL_HEIGHT (f);
7972}
7973
7974int
7975x_char_width (f)
7976 register struct frame *f;
7977{
7978 return FONT_WIDTH (f->output_data.w32->font);
7979}
7980
7981int
7982x_char_height (f)
7983 register struct frame *f;
7984{
7985 return f->output_data.w32->line_height;
7986}
7987
7988int
7989x_screen_planes (f)
7990 register struct frame *f;
7991{
7992 return FRAME_W32_DISPLAY_INFO (f)->n_planes;
7993}
7994\f
7995/* Return the display structure for the display named NAME.
7996 Open a new connection if necessary. */
7997
7998struct w32_display_info *
7999x_display_info_for_name (name)
8000 Lisp_Object name;
8001{
8002 Lisp_Object names;
8003 struct w32_display_info *dpyinfo;
8004
b7826503 8005 CHECK_STRING (name);
6fc2811b
JR
8006
8007 for (dpyinfo = &one_w32_display_info, names = w32_display_name_list;
8008 dpyinfo;
8009 dpyinfo = dpyinfo->next, names = XCDR (names))
8010 {
8011 Lisp_Object tem;
8012 tem = Fstring_equal (XCAR (XCAR (names)), name);
8013 if (!NILP (tem))
8014 return dpyinfo;
8015 }
8016
8017 /* Use this general default value to start with. */
8018 Vx_resource_name = Vinvocation_name;
8019
8020 validate_x_resource_name ();
8021
8022 dpyinfo = w32_term_init (name, (unsigned char *)0,
d5db4077 8023 (char *) SDATA (Vx_resource_name));
6fc2811b
JR
8024
8025 if (dpyinfo == 0)
d5db4077 8026 error ("Cannot connect to server %s", SDATA (name));
6fc2811b
JR
8027
8028 w32_in_use = 1;
8029 XSETFASTINT (Vwindow_system_version, 3);
8030
8031 return dpyinfo;
8032}
8033
8034DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
74e1aeec
JR
8035 1, 3, 0, doc: /* Open a connection to a server.
8036DISPLAY is the name of the display to connect to.
8037Optional second arg XRM-STRING is a string of resources in xrdb format.
8038If the optional third arg MUST-SUCCEED is non-nil,
8039terminate Emacs if we can't open the connection. */)
6fc2811b
JR
8040 (display, xrm_string, must_succeed)
8041 Lisp_Object display, xrm_string, must_succeed;
8042{
8043 unsigned char *xrm_option;
8044 struct w32_display_info *dpyinfo;
8045
74e1aeec
JR
8046 /* If initialization has already been done, return now to avoid
8047 overwriting critical parts of one_w32_display_info. */
8048 if (w32_in_use)
8049 return Qnil;
8050
b7826503 8051 CHECK_STRING (display);
6fc2811b 8052 if (! NILP (xrm_string))
b7826503 8053 CHECK_STRING (xrm_string);
6fc2811b
JR
8054
8055 if (! EQ (Vwindow_system, intern ("w32")))
8056 error ("Not using Microsoft Windows");
8057
8058 /* Allow color mapping to be defined externally; first look in user's
8059 HOME directory, then in Emacs etc dir for a file called rgb.txt. */
8060 {
8061 Lisp_Object color_file;
8062 struct gcpro gcpro1;
8063
8064 color_file = build_string("~/rgb.txt");
8065
8066 GCPRO1 (color_file);
8067
8068 if (NILP (Ffile_readable_p (color_file)))
8069 color_file =
8070 Fexpand_file_name (build_string ("rgb.txt"),
8071 Fsymbol_value (intern ("data-directory")));
8072
8073 Vw32_color_map = Fw32_load_color_file (color_file);
8074
8075 UNGCPRO;
8076 }
8077 if (NILP (Vw32_color_map))
8078 Vw32_color_map = Fw32_default_color_map ();
8079
8080 if (! NILP (xrm_string))
d5db4077 8081 xrm_option = (unsigned char *) SDATA (xrm_string);
6fc2811b
JR
8082 else
8083 xrm_option = (unsigned char *) 0;
8084
8085 /* Use this general default value to start with. */
8086 /* First remove .exe suffix from invocation-name - it looks ugly. */
8087 {
8088 char basename[ MAX_PATH ], *str;
8089
d5db4077 8090 strcpy (basename, SDATA (Vinvocation_name));
6fc2811b
JR
8091 str = strrchr (basename, '.');
8092 if (str) *str = 0;
8093 Vinvocation_name = build_string (basename);
8094 }
8095 Vx_resource_name = Vinvocation_name;
8096
8097 validate_x_resource_name ();
8098
8099 /* This is what opens the connection and sets x_current_display.
8100 This also initializes many symbols, such as those used for input. */
8101 dpyinfo = w32_term_init (display, xrm_option,
d5db4077 8102 (char *) SDATA (Vx_resource_name));
6fc2811b
JR
8103
8104 if (dpyinfo == 0)
8105 {
8106 if (!NILP (must_succeed))
8107 fatal ("Cannot connect to server %s.\n",
d5db4077 8108 SDATA (display));
6fc2811b 8109 else
d5db4077 8110 error ("Cannot connect to server %s", SDATA (display));
6fc2811b
JR
8111 }
8112
8113 w32_in_use = 1;
8114
8115 XSETFASTINT (Vwindow_system_version, 3);
8116 return Qnil;
8117}
8118
8119DEFUN ("x-close-connection", Fx_close_connection,
8120 Sx_close_connection, 1, 1, 0,
74e1aeec
JR
8121 doc: /* Close the connection to DISPLAY's server.
8122For DISPLAY, specify either a frame or a display name (a string).
8123If DISPLAY is nil, that stands for the selected frame's display. */)
6fc2811b
JR
8124 (display)
8125 Lisp_Object display;
8126{
8127 struct w32_display_info *dpyinfo = check_x_display_info (display);
8128 int i;
8129
8130 if (dpyinfo->reference_count > 0)
8131 error ("Display still has frames on it");
8132
8133 BLOCK_INPUT;
8134 /* Free the fonts in the font table. */
8135 for (i = 0; i < dpyinfo->n_fonts; i++)
8136 if (dpyinfo->font_table[i].name)
8137 {
126f2e35
JR
8138 if (dpyinfo->font_table[i].name != dpyinfo->font_table[i].full_name)
8139 xfree (dpyinfo->font_table[i].full_name);
6fc2811b 8140 xfree (dpyinfo->font_table[i].name);
6fc2811b
JR
8141 w32_unload_font (dpyinfo, dpyinfo->font_table[i].font);
8142 }
8143 x_destroy_all_bitmaps (dpyinfo);
8144
8145 x_delete_display (dpyinfo);
8146 UNBLOCK_INPUT;
8147
8148 return Qnil;
8149}
8150
8151DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
74e1aeec 8152 doc: /* Return the list of display names that Emacs has connections to. */)
6fc2811b
JR
8153 ()
8154{
8155 Lisp_Object tail, result;
8156
8157 result = Qnil;
8158 for (tail = w32_display_name_list; ! NILP (tail); tail = XCDR (tail))
8159 result = Fcons (XCAR (XCAR (tail)), result);
8160
8161 return result;
8162}
8163
8164DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
0a332240
PJ
8165 doc: /* This is a noop on W32 systems. */)
8166 (on, display)
8167 Lisp_Object display, on;
6fc2811b 8168{
6fc2811b
JR
8169 return Qnil;
8170}
8171
8172\f
6fc2811b
JR
8173/***********************************************************************
8174 Image types
8175 ***********************************************************************/
8176
8177/* Value is the number of elements of vector VECTOR. */
8178
8179#define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR))
8180
8181/* List of supported image types. Use define_image_type to add new
8182 types. Use lookup_image_type to find a type for a given symbol. */
8183
8184static struct image_type *image_types;
8185
6fc2811b
JR
8186/* The symbol `image' which is the car of the lists used to represent
8187 images in Lisp. */
8188
8189extern Lisp_Object Qimage;
8190
8191/* The symbol `xbm' which is used as the type symbol for XBM images. */
8192
8193Lisp_Object Qxbm;
8194
8195/* Keywords. */
8196
6fc2811b 8197extern Lisp_Object QCwidth, QCheight, QCforeground, QCbackground, QCfile;
77814035
KS
8198extern Lisp_Object QCdata, QCtype;
8199Lisp_Object QCascent, QCmargin, QCrelief;
a93f4566 8200Lisp_Object QCconversion, QCcolor_symbols, QCheuristic_mask;
3cf3436e 8201Lisp_Object QCindex, QCmatrix, QCcolor_adjustment, QCmask;
6fc2811b
JR
8202
8203/* Other symbols. */
8204
3cf3436e 8205Lisp_Object Qlaplace, Qemboss, Qedge_detection, Qheuristic;
6fc2811b
JR
8206
8207/* Time in seconds after which images should be removed from the cache
8208 if not displayed. */
8209
8210Lisp_Object Vimage_cache_eviction_delay;
8211
8212/* Function prototypes. */
8213
8214static void define_image_type P_ ((struct image_type *type));
8215static struct image_type *lookup_image_type P_ ((Lisp_Object symbol));
8216static void image_error P_ ((char *format, Lisp_Object, Lisp_Object));
8217static void x_laplace P_ ((struct frame *, struct image *));
3cf3436e 8218static void x_emboss P_ ((struct frame *, struct image *));
6fc2811b
JR
8219static int x_build_heuristic_mask P_ ((struct frame *, struct image *,
8220 Lisp_Object));
8221
dfff8a69 8222
6fc2811b
JR
8223/* Define a new image type from TYPE. This adds a copy of TYPE to
8224 image_types and adds the symbol *TYPE->type to Vimage_types. */
8225
8226static void
8227define_image_type (type)
8228 struct image_type *type;
8229{
8230 /* Make a copy of TYPE to avoid a bus error in a dumped Emacs.
8231 The initialized data segment is read-only. */
8232 struct image_type *p = (struct image_type *) xmalloc (sizeof *p);
8233 bcopy (type, p, sizeof *p);
8234 p->next = image_types;
8235 image_types = p;
8236 Vimage_types = Fcons (*p->type, Vimage_types);
8237}
8238
8239
8240/* Look up image type SYMBOL, and return a pointer to its image_type
8241 structure. Value is null if SYMBOL is not a known image type. */
8242
8243static INLINE struct image_type *
8244lookup_image_type (symbol)
8245 Lisp_Object symbol;
8246{
8247 struct image_type *type;
8248
8249 for (type = image_types; type; type = type->next)
8250 if (EQ (symbol, *type->type))
8251 break;
8252
8253 return type;
8254}
8255
8256
8257/* Value is non-zero if OBJECT is a valid Lisp image specification. A
8258 valid image specification is a list whose car is the symbol
8259 `image', and whose rest is a property list. The property list must
8260 contain a value for key `:type'. That value must be the name of a
8261 supported image type. The rest of the property list depends on the
8262 image type. */
8263
8264int
8265valid_image_p (object)
8266 Lisp_Object object;
8267{
8268 int valid_p = 0;
7d0393cf 8269
6fc2811b
JR
8270 if (CONSP (object) && EQ (XCAR (object), Qimage))
8271 {
3cf3436e
JR
8272 Lisp_Object tem;
8273
8274 for (tem = XCDR (object); CONSP (tem); tem = XCDR (tem))
8275 if (EQ (XCAR (tem), QCtype))
8276 {
8277 tem = XCDR (tem);
8278 if (CONSP (tem) && SYMBOLP (XCAR (tem)))
8279 {
8280 struct image_type *type;
8281 type = lookup_image_type (XCAR (tem));
8282 if (type)
8283 valid_p = type->valid_p (object);
8284 }
8285
8286 break;
8287 }
6fc2811b
JR
8288 }
8289
8290 return valid_p;
8291}
8292
8293
8294/* Log error message with format string FORMAT and argument ARG.
8295 Signaling an error, e.g. when an image cannot be loaded, is not a
8296 good idea because this would interrupt redisplay, and the error
8297 message display would lead to another redisplay. This function
8298 therefore simply displays a message. */
8299
8300static void
8301image_error (format, arg1, arg2)
8302 char *format;
8303 Lisp_Object arg1, arg2;
8304{
8305 add_to_log (format, arg1, arg2);
8306}
8307
8308
8309\f
8310/***********************************************************************
8311 Image specifications
8312 ***********************************************************************/
8313
8314enum image_value_type
8315{
8316 IMAGE_DONT_CHECK_VALUE_TYPE,
8317 IMAGE_STRING_VALUE,
3cf3436e 8318 IMAGE_STRING_OR_NIL_VALUE,
6fc2811b
JR
8319 IMAGE_SYMBOL_VALUE,
8320 IMAGE_POSITIVE_INTEGER_VALUE,
8edb0a6f 8321 IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR,
6fc2811b 8322 IMAGE_NON_NEGATIVE_INTEGER_VALUE,
dfff8a69 8323 IMAGE_ASCENT_VALUE,
6fc2811b
JR
8324 IMAGE_INTEGER_VALUE,
8325 IMAGE_FUNCTION_VALUE,
8326 IMAGE_NUMBER_VALUE,
8327 IMAGE_BOOL_VALUE
8328};
8329
8330/* Structure used when parsing image specifications. */
8331
8332struct image_keyword
8333{
8334 /* Name of keyword. */
8335 char *name;
8336
8337 /* The type of value allowed. */
8338 enum image_value_type type;
8339
8340 /* Non-zero means key must be present. */
8341 int mandatory_p;
8342
8343 /* Used to recognize duplicate keywords in a property list. */
8344 int count;
8345
8346 /* The value that was found. */
8347 Lisp_Object value;
8348};
8349
8350
8351static int parse_image_spec P_ ((Lisp_Object, struct image_keyword *,
8352 int, Lisp_Object));
8353static Lisp_Object image_spec_value P_ ((Lisp_Object, Lisp_Object, int *));
8354
8355
8356/* Parse image spec SPEC according to KEYWORDS. A valid image spec
8357 has the format (image KEYWORD VALUE ...). One of the keyword/
8358 value pairs must be `:type TYPE'. KEYWORDS is a vector of
8359 image_keywords structures of size NKEYWORDS describing other
8360 allowed keyword/value pairs. Value is non-zero if SPEC is valid. */
8361
8362static int
8363parse_image_spec (spec, keywords, nkeywords, type)
8364 Lisp_Object spec;
8365 struct image_keyword *keywords;
8366 int nkeywords;
8367 Lisp_Object type;
8368{
8369 int i;
8370 Lisp_Object plist;
8371
8372 if (!CONSP (spec) || !EQ (XCAR (spec), Qimage))
8373 return 0;
8374
8375 plist = XCDR (spec);
8376 while (CONSP (plist))
8377 {
8378 Lisp_Object key, value;
8379
8380 /* First element of a pair must be a symbol. */
8381 key = XCAR (plist);
8382 plist = XCDR (plist);
8383 if (!SYMBOLP (key))
8384 return 0;
8385
8386 /* There must follow a value. */
8387 if (!CONSP (plist))
8388 return 0;
8389 value = XCAR (plist);
8390 plist = XCDR (plist);
8391
8392 /* Find key in KEYWORDS. Error if not found. */
8393 for (i = 0; i < nkeywords; ++i)
d5db4077 8394 if (strcmp (keywords[i].name, SDATA (SYMBOL_NAME (key))) == 0)
6fc2811b
JR
8395 break;
8396
8397 if (i == nkeywords)
8398 continue;
8399
8400 /* Record that we recognized the keyword. If a keywords
8401 was found more than once, it's an error. */
8402 keywords[i].value = value;
8403 ++keywords[i].count;
7d0393cf 8404
6fc2811b
JR
8405 if (keywords[i].count > 1)
8406 return 0;
8407
8408 /* Check type of value against allowed type. */
8409 switch (keywords[i].type)
8410 {
8411 case IMAGE_STRING_VALUE:
8412 if (!STRINGP (value))
8413 return 0;
8414 break;
8415
3cf3436e
JR
8416 case IMAGE_STRING_OR_NIL_VALUE:
8417 if (!STRINGP (value) && !NILP (value))
8418 return 0;
8419 break;
8420
6fc2811b
JR
8421 case IMAGE_SYMBOL_VALUE:
8422 if (!SYMBOLP (value))
8423 return 0;
8424 break;
8425
8426 case IMAGE_POSITIVE_INTEGER_VALUE:
8427 if (!INTEGERP (value) || XINT (value) <= 0)
8428 return 0;
8429 break;
8430
8edb0a6f
JR
8431 case IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR:
8432 if (INTEGERP (value) && XINT (value) >= 0)
8433 break;
8434 if (CONSP (value)
8435 && INTEGERP (XCAR (value)) && INTEGERP (XCDR (value))
8436 && XINT (XCAR (value)) >= 0 && XINT (XCDR (value)) >= 0)
8437 break;
8438 return 0;
8439
dfff8a69
JR
8440 case IMAGE_ASCENT_VALUE:
8441 if (SYMBOLP (value) && EQ (value, Qcenter))
8442 break;
8443 else if (INTEGERP (value)
8444 && XINT (value) >= 0
8445 && XINT (value) <= 100)
8446 break;
8447 return 0;
8448
6fc2811b
JR
8449 case IMAGE_NON_NEGATIVE_INTEGER_VALUE:
8450 if (!INTEGERP (value) || XINT (value) < 0)
8451 return 0;
8452 break;
8453
8454 case IMAGE_DONT_CHECK_VALUE_TYPE:
8455 break;
8456
8457 case IMAGE_FUNCTION_VALUE:
8458 value = indirect_function (value);
7d0393cf 8459 if (SUBRP (value)
6fc2811b
JR
8460 || COMPILEDP (value)
8461 || (CONSP (value) && EQ (XCAR (value), Qlambda)))
8462 break;
8463 return 0;
8464
8465 case IMAGE_NUMBER_VALUE:
8466 if (!INTEGERP (value) && !FLOATP (value))
8467 return 0;
8468 break;
8469
8470 case IMAGE_INTEGER_VALUE:
8471 if (!INTEGERP (value))
8472 return 0;
8473 break;
8474
8475 case IMAGE_BOOL_VALUE:
8476 if (!NILP (value) && !EQ (value, Qt))
8477 return 0;
8478 break;
8479
8480 default:
8481 abort ();
8482 break;
8483 }
8484
8485 if (EQ (key, QCtype) && !EQ (type, value))
8486 return 0;
8487 }
8488
8489 /* Check that all mandatory fields are present. */
8490 for (i = 0; i < nkeywords; ++i)
8491 if (keywords[i].mandatory_p && keywords[i].count == 0)
8492 return 0;
8493
8494 return NILP (plist);
8495}
8496
8497
8498/* Return the value of KEY in image specification SPEC. Value is nil
8499 if KEY is not present in SPEC. if FOUND is not null, set *FOUND
8500 to 1 if KEY was found in SPEC, set it to 0 otherwise. */
8501
8502static Lisp_Object
8503image_spec_value (spec, key, found)
8504 Lisp_Object spec, key;
8505 int *found;
8506{
8507 Lisp_Object tail;
7d0393cf 8508
6fc2811b
JR
8509 xassert (valid_image_p (spec));
8510
8511 for (tail = XCDR (spec);
8512 CONSP (tail) && CONSP (XCDR (tail));
8513 tail = XCDR (XCDR (tail)))
8514 {
8515 if (EQ (XCAR (tail), key))
8516 {
8517 if (found)
8518 *found = 1;
8519 return XCAR (XCDR (tail));
8520 }
8521 }
7d0393cf 8522
6fc2811b
JR
8523 if (found)
8524 *found = 0;
8525 return Qnil;
8526}
7d0393cf 8527
6fc2811b 8528
ac849ba4
JR
8529DEFUN ("image-size", Fimage_size, Simage_size, 1, 3, 0,
8530 doc: /* Return the size of image SPEC as pair (WIDTH . HEIGHT).
8531PIXELS non-nil means return the size in pixels, otherwise return the
8532size in canonical character units.
8533FRAME is the frame on which the image will be displayed. FRAME nil
8534or omitted means use the selected frame. */)
8535 (spec, pixels, frame)
8536 Lisp_Object spec, pixels, frame;
8537{
8538 Lisp_Object size;
8539
8540 size = Qnil;
8541 if (valid_image_p (spec))
8542 {
8543 struct frame *f = check_x_frame (frame);
8544 int id = lookup_image (f, spec);
8545 struct image *img = IMAGE_FROM_ID (f, id);
8546 int width = img->width + 2 * img->hmargin;
8547 int height = img->height + 2 * img->vmargin;
7d0393cf 8548
ac849ba4
JR
8549 if (NILP (pixels))
8550 size = Fcons (make_float ((double) width / CANON_X_UNIT (f)),
8551 make_float ((double) height / CANON_Y_UNIT (f)));
8552 else
8553 size = Fcons (make_number (width), make_number (height));
8554 }
8555 else
8556 error ("Invalid image specification");
8557
8558 return size;
8559}
8560
8561
8562DEFUN ("image-mask-p", Fimage_mask_p, Simage_mask_p, 1, 2, 0,
8563 doc: /* Return t if image SPEC has a mask bitmap.
8564FRAME is the frame on which the image will be displayed. FRAME nil
8565or omitted means use the selected frame. */)
8566 (spec, frame)
8567 Lisp_Object spec, frame;
8568{
8569 Lisp_Object mask;
8570
8571 mask = Qnil;
8572 if (valid_image_p (spec))
8573 {
8574 struct frame *f = check_x_frame (frame);
8575 int id = lookup_image (f, spec);
8576 struct image *img = IMAGE_FROM_ID (f, id);
8577 if (img->mask)
8578 mask = Qt;
8579 }
8580 else
8581 error ("Invalid image specification");
8582
8583 return mask;
8584}
6fc2811b
JR
8585
8586\f
8587/***********************************************************************
8588 Image type independent image structures
8589 ***********************************************************************/
8590
8591static struct image *make_image P_ ((Lisp_Object spec, unsigned hash));
8592static void free_image P_ ((struct frame *f, struct image *img));
197edd35 8593static void x_destroy_x_image P_ ((XImage *));
6fc2811b
JR
8594
8595
8596/* Allocate and return a new image structure for image specification
8597 SPEC. SPEC has a hash value of HASH. */
8598
8599static struct image *
8600make_image (spec, hash)
8601 Lisp_Object spec;
8602 unsigned hash;
8603{
8604 struct image *img = (struct image *) xmalloc (sizeof *img);
7d0393cf 8605
6fc2811b
JR
8606 xassert (valid_image_p (spec));
8607 bzero (img, sizeof *img);
8608 img->type = lookup_image_type (image_spec_value (spec, QCtype, NULL));
8609 xassert (img->type != NULL);
8610 img->spec = spec;
8611 img->data.lisp_val = Qnil;
8612 img->ascent = DEFAULT_IMAGE_ASCENT;
8613 img->hash = hash;
8614 return img;
8615}
8616
8617
8618/* Free image IMG which was used on frame F, including its resources. */
8619
8620static void
8621free_image (f, img)
8622 struct frame *f;
8623 struct image *img;
8624{
8625 if (img)
8626 {
8627 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
8628
8629 /* Remove IMG from the hash table of its cache. */
8630 if (img->prev)
8631 img->prev->next = img->next;
8632 else
8633 c->buckets[img->hash % IMAGE_CACHE_BUCKETS_SIZE] = img->next;
8634
8635 if (img->next)
8636 img->next->prev = img->prev;
8637
8638 c->images[img->id] = NULL;
8639
8640 /* Free resources, then free IMG. */
8641 img->type->free (f, img);
8642 xfree (img);
8643 }
8644}
8645
8646
8647/* Prepare image IMG for display on frame F. Must be called before
8648 drawing an image. */
8649
8650void
8651prepare_image_for_display (f, img)
8652 struct frame *f;
8653 struct image *img;
8654{
8655 EMACS_TIME t;
8656
8657 /* We're about to display IMG, so set its timestamp to `now'. */
8658 EMACS_GET_TIME (t);
8659 img->timestamp = EMACS_SECS (t);
8660
8661 /* If IMG doesn't have a pixmap yet, load it now, using the image
8662 type dependent loader function. */
8663 if (img->pixmap == 0 && !img->load_failed_p)
8664 img->load_failed_p = img->type->load (f, img) == 0;
8665}
7d0393cf 8666
6fc2811b 8667
dfff8a69
JR
8668/* Value is the number of pixels for the ascent of image IMG when
8669 drawn in face FACE. */
8670
8671int
8672image_ascent (img, face)
8673 struct image *img;
8674 struct face *face;
8675{
8edb0a6f 8676 int height = img->height + img->vmargin;
dfff8a69
JR
8677 int ascent;
8678
8679 if (img->ascent == CENTERED_IMAGE_ASCENT)
8680 {
8681 if (face->font)
8682 ascent = height / 2 - (FONT_DESCENT(face->font)
8683 - FONT_BASE(face->font)) / 2;
8684 else
8685 ascent = height / 2;
8686 }
8687 else
ac849ba4 8688 ascent = (int) (height * img->ascent / 100.0);
dfff8a69
JR
8689
8690 return ascent;
8691}
8692
8693
6fc2811b 8694\f
a05e2bae
JR
8695/* Image background colors. */
8696
ac849ba4
JR
8697/* Find the "best" corner color of a bitmap. XIMG is assumed to a device
8698 context with the bitmap selected. */
8699static COLORREF
197edd35
JR
8700four_corners_best (img_dc, width, height)
8701 HDC img_dc;
a05e2bae
JR
8702 unsigned long width, height;
8703{
ac849ba4 8704 COLORREF corners[4], best;
a05e2bae
JR
8705 int i, best_count;
8706
197edd35
JR
8707 /* Get the colors at the corners of img_dc. */
8708 corners[0] = GetPixel (img_dc, 0, 0);
8709 corners[1] = GetPixel (img_dc, width - 1, 0);
8710 corners[2] = GetPixel (img_dc, width - 1, height - 1);
8711 corners[3] = GetPixel (img_dc, 0, height - 1);
a05e2bae
JR
8712
8713 /* Choose the most frequently found color as background. */
8714 for (i = best_count = 0; i < 4; ++i)
8715 {
8716 int j, n;
7d0393cf 8717
a05e2bae
JR
8718 for (j = n = 0; j < 4; ++j)
8719 if (corners[i] == corners[j])
8720 ++n;
8721
8722 if (n > best_count)
8723 best = corners[i], best_count = n;
8724 }
8725
8726 return best;
a05e2bae
JR
8727}
8728
8729/* Return the `background' field of IMG. If IMG doesn't have one yet,
197edd35
JR
8730 it is guessed heuristically. If non-zero, IMG_DC is an existing
8731 device context with the image selected to use for the heuristic. */
a05e2bae
JR
8732
8733unsigned long
197edd35 8734image_background (img, f, img_dc)
a05e2bae
JR
8735 struct image *img;
8736 struct frame *f;
197edd35 8737 HDC img_dc;
a05e2bae
JR
8738{
8739 if (! img->background_valid)
8740 /* IMG doesn't have a background yet, try to guess a reasonable value. */
8741 {
197edd35
JR
8742 int free_ximg = !img_dc;
8743 HGDIOBJ prev;
8744
8745 if (free_ximg)
8746 {
8747 HDC frame_dc = get_frame_dc (f);
8748 img_dc = CreateCompatibleDC (frame_dc);
8749 release_frame_dc (f, frame_dc);
a05e2bae 8750
197edd35
JR
8751 prev = SelectObject (img_dc, img->pixmap);
8752 }
a05e2bae 8753
197edd35 8754 img->background = four_corners_best (img_dc, img->width, img->height);
a05e2bae
JR
8755
8756 if (free_ximg)
197edd35
JR
8757 {
8758 SelectObject (img_dc, prev);
8759 DeleteDC (img_dc);
8760 }
a05e2bae
JR
8761
8762 img->background_valid = 1;
a05e2bae
JR
8763 }
8764
8765 return img->background;
8766}
8767
8768/* Return the `background_transparent' field of IMG. If IMG doesn't
8769 have one yet, it is guessed heuristically. If non-zero, MASK is an
8770 existing XImage object to use for the heuristic. */
8771
8772int
8773image_background_transparent (img, f, mask)
8774 struct image *img;
8775 struct frame *f;
197edd35 8776 HDC mask;
a05e2bae
JR
8777{
8778 if (! img->background_transparent_valid)
8779 /* IMG doesn't have a background yet, try to guess a reasonable value. */
8780 {
a05e2bae
JR
8781 if (img->mask)
8782 {
8783 int free_mask = !mask;
197edd35 8784 HGDIOBJ prev;
a05e2bae 8785
197edd35
JR
8786 if (free_mask)
8787 {
8788 HDC frame_dc = get_frame_dc (f);
8789 mask = CreateCompatibleDC (frame_dc);
8790 release_frame_dc (f, frame_dc);
8791
8792 prev = SelectObject (mask, img->mask);
8793 }
a05e2bae
JR
8794
8795 img->background_transparent
8796 = !four_corners_best (mask, img->width, img->height);
8797
8798 if (free_mask)
197edd35
JR
8799 {
8800 SelectObject (mask, prev);
8801 DeleteDC (mask);
8802 }
a05e2bae
JR
8803 }
8804 else
a05e2bae
JR
8805 img->background_transparent = 0;
8806
8807 img->background_transparent_valid = 1;
8808 }
8809
8810 return img->background_transparent;
8811}
8812
8813\f
6fc2811b
JR
8814/***********************************************************************
8815 Helper functions for X image types
8816 ***********************************************************************/
8817
a05e2bae
JR
8818static void x_clear_image_1 P_ ((struct frame *, struct image *, int,
8819 int, int));
6fc2811b
JR
8820static void x_clear_image P_ ((struct frame *f, struct image *img));
8821static unsigned long x_alloc_image_color P_ ((struct frame *f,
8822 struct image *img,
8823 Lisp_Object color_name,
8824 unsigned long dflt));
8825
a05e2bae
JR
8826
8827/* Clear X resources of image IMG on frame F. PIXMAP_P non-zero means
8828 free the pixmap if any. MASK_P non-zero means clear the mask
8829 pixmap if any. COLORS_P non-zero means free colors allocated for
8830 the image, if any. */
8831
8832static void
8833x_clear_image_1 (f, img, pixmap_p, mask_p, colors_p)
8834 struct frame *f;
8835 struct image *img;
8836 int pixmap_p, mask_p, colors_p;
8837{
a05e2bae
JR
8838 if (pixmap_p && img->pixmap)
8839 {
ac849ba4
JR
8840 DeleteObject (img->pixmap);
8841 img->pixmap = NULL;
a05e2bae
JR
8842 img->background_valid = 0;
8843 }
8844
8845 if (mask_p && img->mask)
8846 {
ac849ba4
JR
8847 DeleteObject (img->mask);
8848 img->mask = NULL;
a05e2bae
JR
8849 img->background_transparent_valid = 0;
8850 }
7d0393cf 8851
a05e2bae
JR
8852 if (colors_p && img->ncolors)
8853 {
bf76fe9c 8854#if 0 /* TODO: color table support. */
a05e2bae 8855 x_free_colors (f, img->colors, img->ncolors);
bf76fe9c 8856#endif
a05e2bae
JR
8857 xfree (img->colors);
8858 img->colors = NULL;
8859 img->ncolors = 0;
8860 }
a05e2bae
JR
8861}
8862
6fc2811b
JR
8863/* Free X resources of image IMG which is used on frame F. */
8864
8865static void
8866x_clear_image (f, img)
8867 struct frame *f;
8868 struct image *img;
8869{
6fc2811b
JR
8870 if (img->pixmap)
8871 {
8872 BLOCK_INPUT;
ac849ba4 8873 DeleteObject (img->pixmap);
6fc2811b
JR
8874 img->pixmap = 0;
8875 UNBLOCK_INPUT;
8876 }
8877
8878 if (img->ncolors)
8879 {
ac849ba4
JR
8880#if 0 /* TODO: color table support */
8881
6fc2811b 8882 int class = FRAME_W32_DISPLAY_INFO (f)->visual->class;
7d0393cf 8883
6fc2811b
JR
8884 /* If display has an immutable color map, freeing colors is not
8885 necessary and some servers don't allow it. So don't do it. */
8886 if (class != StaticColor
8887 && class != StaticGray
8888 && class != TrueColor)
8889 {
8890 Colormap cmap;
8891 BLOCK_INPUT;
8892 cmap = DefaultColormapOfScreen (FRAME_W32_DISPLAY_INFO (f)->screen);
8893 XFreeColors (FRAME_W32_DISPLAY (f), cmap, img->colors,
8894 img->ncolors, 0);
8895 UNBLOCK_INPUT;
8896 }
ac849ba4 8897#endif
7d0393cf 8898
6fc2811b
JR
8899 xfree (img->colors);
8900 img->colors = NULL;
8901 img->ncolors = 0;
8902 }
6fc2811b
JR
8903}
8904
8905
8906/* Allocate color COLOR_NAME for image IMG on frame F. If color
8907 cannot be allocated, use DFLT. Add a newly allocated color to
8908 IMG->colors, so that it can be freed again. Value is the pixel
8909 color. */
8910
8911static unsigned long
8912x_alloc_image_color (f, img, color_name, dflt)
8913 struct frame *f;
8914 struct image *img;
8915 Lisp_Object color_name;
8916 unsigned long dflt;
8917{
6fc2811b
JR
8918 XColor color;
8919 unsigned long result;
8920
8921 xassert (STRINGP (color_name));
8922
d5db4077 8923 if (w32_defined_color (f, SDATA (color_name), &color, 1))
6fc2811b
JR
8924 {
8925 /* This isn't called frequently so we get away with simply
8926 reallocating the color vector to the needed size, here. */
8927 ++img->ncolors;
8928 img->colors =
8929 (unsigned long *) xrealloc (img->colors,
8930 img->ncolors * sizeof *img->colors);
8931 img->colors[img->ncolors - 1] = color.pixel;
8932 result = color.pixel;
8933 }
8934 else
8935 result = dflt;
8936 return result;
6fc2811b
JR
8937}
8938
8939
8940\f
8941/***********************************************************************
8942 Image Cache
8943 ***********************************************************************/
8944
8945static void cache_image P_ ((struct frame *f, struct image *img));
3cf3436e 8946static void postprocess_image P_ ((struct frame *, struct image *));
197edd35 8947static void x_disable_image P_ ((struct frame *, struct image *));
6fc2811b
JR
8948
8949
8950/* Return a new, initialized image cache that is allocated from the
8951 heap. Call free_image_cache to free an image cache. */
8952
8953struct image_cache *
8954make_image_cache ()
8955{
8956 struct image_cache *c = (struct image_cache *) xmalloc (sizeof *c);
8957 int size;
7d0393cf 8958
6fc2811b
JR
8959 bzero (c, sizeof *c);
8960 c->size = 50;
8961 c->images = (struct image **) xmalloc (c->size * sizeof *c->images);
8962 size = IMAGE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
8963 c->buckets = (struct image **) xmalloc (size);
8964 bzero (c->buckets, size);
8965 return c;
8966}
8967
8968
8969/* Free image cache of frame F. Be aware that X frames share images
8970 caches. */
8971
8972void
8973free_image_cache (f)
8974 struct frame *f;
8975{
8976 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
8977 if (c)
8978 {
8979 int i;
8980
8981 /* Cache should not be referenced by any frame when freed. */
8982 xassert (c->refcount == 0);
7d0393cf 8983
6fc2811b
JR
8984 for (i = 0; i < c->used; ++i)
8985 free_image (f, c->images[i]);
8986 xfree (c->images);
8987 xfree (c);
8988 xfree (c->buckets);
8989 FRAME_X_IMAGE_CACHE (f) = NULL;
8990 }
8991}
8992
8993
8994/* Clear image cache of frame F. FORCE_P non-zero means free all
8995 images. FORCE_P zero means clear only images that haven't been
8996 displayed for some time. Should be called from time to time to
dfff8a69
JR
8997 reduce the number of loaded images. If image-eviction-seconds is
8998 non-nil, this frees images in the cache which weren't displayed for
6fc2811b
JR
8999 at least that many seconds. */
9000
9001void
9002clear_image_cache (f, force_p)
9003 struct frame *f;
9004 int force_p;
9005{
9006 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
9007
9008 if (c && INTEGERP (Vimage_cache_eviction_delay))
9009 {
9010 EMACS_TIME t;
9011 unsigned long old;
0327b4cc 9012 int i, nfreed;
6fc2811b
JR
9013
9014 EMACS_GET_TIME (t);
9015 old = EMACS_SECS (t) - XFASTINT (Vimage_cache_eviction_delay);
7d0393cf 9016
0327b4cc
JR
9017 /* Block input so that we won't be interrupted by a SIGIO
9018 while being in an inconsistent state. */
9019 BLOCK_INPUT;
7d0393cf 9020
0327b4cc 9021 for (i = nfreed = 0; i < c->used; ++i)
6fc2811b
JR
9022 {
9023 struct image *img = c->images[i];
9024 if (img != NULL
0327b4cc 9025 && (force_p || (img->timestamp < old)))
6fc2811b
JR
9026 {
9027 free_image (f, img);
0327b4cc 9028 ++nfreed;
6fc2811b
JR
9029 }
9030 }
9031
9032 /* We may be clearing the image cache because, for example,
9033 Emacs was iconified for a longer period of time. In that
9034 case, current matrices may still contain references to
9035 images freed above. So, clear these matrices. */
0327b4cc 9036 if (nfreed)
6fc2811b 9037 {
0327b4cc 9038 Lisp_Object tail, frame;
7d0393cf 9039
0327b4cc
JR
9040 FOR_EACH_FRAME (tail, frame)
9041 {
9042 struct frame *f = XFRAME (frame);
9043 if (FRAME_W32_P (f)
9044 && FRAME_X_IMAGE_CACHE (f) == c)
9045 clear_current_matrices (f);
9046 }
9047
6fc2811b
JR
9048 ++windows_or_buffers_changed;
9049 }
0327b4cc
JR
9050
9051 UNBLOCK_INPUT;
6fc2811b
JR
9052 }
9053}
9054
9055
9056DEFUN ("clear-image-cache", Fclear_image_cache, Sclear_image_cache,
9057 0, 1, 0,
74e1aeec
JR
9058 doc: /* Clear the image cache of FRAME.
9059FRAME nil or omitted means use the selected frame.
9060FRAME t means clear the image caches of all frames. */)
6fc2811b
JR
9061 (frame)
9062 Lisp_Object frame;
9063{
9064 if (EQ (frame, Qt))
9065 {
9066 Lisp_Object tail;
7d0393cf 9067
6fc2811b
JR
9068 FOR_EACH_FRAME (tail, frame)
9069 if (FRAME_W32_P (XFRAME (frame)))
9070 clear_image_cache (XFRAME (frame), 1);
9071 }
9072 else
9073 clear_image_cache (check_x_frame (frame), 1);
9074
9075 return Qnil;
9076}
9077
9078
3cf3436e
JR
9079/* Compute masks and transform image IMG on frame F, as specified
9080 by the image's specification, */
9081
9082static void
9083postprocess_image (f, img)
9084 struct frame *f;
9085 struct image *img;
9086{
3cf3436e
JR
9087 /* Manipulation of the image's mask. */
9088 if (img->pixmap)
9089 {
9090 Lisp_Object conversion, spec;
9091 Lisp_Object mask;
9092
9093 spec = img->spec;
7d0393cf 9094
3cf3436e
JR
9095 /* `:heuristic-mask t'
9096 `:mask heuristic'
9097 means build a mask heuristically.
9098 `:heuristic-mask (R G B)'
9099 `:mask (heuristic (R G B))'
9100 means build a mask from color (R G B) in the
9101 image.
9102 `:mask nil'
9103 means remove a mask, if any. */
7d0393cf 9104
3cf3436e
JR
9105 mask = image_spec_value (spec, QCheuristic_mask, NULL);
9106 if (!NILP (mask))
9107 x_build_heuristic_mask (f, img, mask);
9108 else
9109 {
9110 int found_p;
7d0393cf 9111
3cf3436e 9112 mask = image_spec_value (spec, QCmask, &found_p);
7d0393cf 9113
3cf3436e
JR
9114 if (EQ (mask, Qheuristic))
9115 x_build_heuristic_mask (f, img, Qt);
9116 else if (CONSP (mask)
9117 && EQ (XCAR (mask), Qheuristic))
9118 {
9119 if (CONSP (XCDR (mask)))
9120 x_build_heuristic_mask (f, img, XCAR (XCDR (mask)));
9121 else
9122 x_build_heuristic_mask (f, img, XCDR (mask));
9123 }
9124 else if (NILP (mask) && found_p && img->mask)
9125 {
ac849ba4 9126 DeleteObject (img->mask);
3cf3436e
JR
9127 img->mask = NULL;
9128 }
9129 }
7d0393cf
JB
9130
9131
3cf3436e
JR
9132 /* Should we apply an image transformation algorithm? */
9133 conversion = image_spec_value (spec, QCconversion, NULL);
9134 if (EQ (conversion, Qdisabled))
9135 x_disable_image (f, img);
9136 else if (EQ (conversion, Qlaplace))
9137 x_laplace (f, img);
9138 else if (EQ (conversion, Qemboss))
9139 x_emboss (f, img);
9140 else if (CONSP (conversion)
9141 && EQ (XCAR (conversion), Qedge_detection))
9142 {
9143 Lisp_Object tem;
9144 tem = XCDR (conversion);
9145 if (CONSP (tem))
9146 x_edge_detection (f, img,
9147 Fplist_get (tem, QCmatrix),
9148 Fplist_get (tem, QCcolor_adjustment));
9149 }
9150 }
3cf3436e
JR
9151}
9152
9153
6fc2811b
JR
9154/* Return the id of image with Lisp specification SPEC on frame F.
9155 SPEC must be a valid Lisp image specification (see valid_image_p). */
9156
9157int
9158lookup_image (f, spec)
9159 struct frame *f;
9160 Lisp_Object spec;
9161{
9162 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
9163 struct image *img;
9164 int i;
9165 unsigned hash;
9166 struct gcpro gcpro1;
9167 EMACS_TIME now;
9168
9169 /* F must be a window-system frame, and SPEC must be a valid image
9170 specification. */
9171 xassert (FRAME_WINDOW_P (f));
9172 xassert (valid_image_p (spec));
7d0393cf 9173
6fc2811b
JR
9174 GCPRO1 (spec);
9175
9176 /* Look up SPEC in the hash table of the image cache. */
9177 hash = sxhash (spec, 0);
9178 i = hash % IMAGE_CACHE_BUCKETS_SIZE;
9179
9180 for (img = c->buckets[i]; img; img = img->next)
9181 if (img->hash == hash && !NILP (Fequal (img->spec, spec)))
9182 break;
9183
9184 /* If not found, create a new image and cache it. */
9185 if (img == NULL)
9186 {
3cf3436e
JR
9187 extern Lisp_Object Qpostscript;
9188
8edb0a6f 9189 BLOCK_INPUT;
6fc2811b
JR
9190 img = make_image (spec, hash);
9191 cache_image (f, img);
9192 img->load_failed_p = img->type->load (f, img) == 0;
6fc2811b
JR
9193
9194 /* If we can't load the image, and we don't have a width and
9195 height, use some arbitrary width and height so that we can
9196 draw a rectangle for it. */
9197 if (img->load_failed_p)
9198 {
9199 Lisp_Object value;
9200
9201 value = image_spec_value (spec, QCwidth, NULL);
9202 img->width = (INTEGERP (value)
9203 ? XFASTINT (value) : DEFAULT_IMAGE_WIDTH);
9204 value = image_spec_value (spec, QCheight, NULL);
9205 img->height = (INTEGERP (value)
9206 ? XFASTINT (value) : DEFAULT_IMAGE_HEIGHT);
9207 }
9208 else
9209 {
9210 /* Handle image type independent image attributes
8f92c555 9211 `:ascent ASCENT', `:margin MARGIN', `:relief RELIEF',
a05e2bae
JR
9212 `:background COLOR'. */
9213 Lisp_Object ascent, margin, relief, bg;
6fc2811b
JR
9214
9215 ascent = image_spec_value (spec, QCascent, NULL);
9216 if (INTEGERP (ascent))
9217 img->ascent = XFASTINT (ascent);
dfff8a69
JR
9218 else if (EQ (ascent, Qcenter))
9219 img->ascent = CENTERED_IMAGE_ASCENT;
9220
6fc2811b
JR
9221 margin = image_spec_value (spec, QCmargin, NULL);
9222 if (INTEGERP (margin) && XINT (margin) >= 0)
8edb0a6f
JR
9223 img->vmargin = img->hmargin = XFASTINT (margin);
9224 else if (CONSP (margin) && INTEGERP (XCAR (margin))
9225 && INTEGERP (XCDR (margin)))
9226 {
9227 if (XINT (XCAR (margin)) > 0)
9228 img->hmargin = XFASTINT (XCAR (margin));
9229 if (XINT (XCDR (margin)) > 0)
9230 img->vmargin = XFASTINT (XCDR (margin));
9231 }
7d0393cf 9232
6fc2811b
JR
9233 relief = image_spec_value (spec, QCrelief, NULL);
9234 if (INTEGERP (relief))
9235 {
9236 img->relief = XINT (relief);
8edb0a6f
JR
9237 img->hmargin += abs (img->relief);
9238 img->vmargin += abs (img->relief);
6fc2811b
JR
9239 }
9240
a05e2bae
JR
9241 if (! img->background_valid)
9242 {
9243 bg = image_spec_value (img->spec, QCbackground, NULL);
9244 if (!NILP (bg))
9245 {
9246 img->background
9247 = x_alloc_image_color (f, img, bg,
9248 FRAME_BACKGROUND_PIXEL (f));
9249 img->background_valid = 1;
9250 }
9251 }
9252
3cf3436e
JR
9253 /* Do image transformations and compute masks, unless we
9254 don't have the image yet. */
9255 if (!EQ (*img->type->type, Qpostscript))
9256 postprocess_image (f, img);
6fc2811b 9257 }
3cf3436e 9258
8edb0a6f
JR
9259 UNBLOCK_INPUT;
9260 xassert (!interrupt_input_blocked);
6fc2811b
JR
9261 }
9262
9263 /* We're using IMG, so set its timestamp to `now'. */
9264 EMACS_GET_TIME (now);
9265 img->timestamp = EMACS_SECS (now);
7d0393cf 9266
6fc2811b 9267 UNGCPRO;
7d0393cf 9268
6fc2811b
JR
9269 /* Value is the image id. */
9270 return img->id;
9271}
9272
9273
9274/* Cache image IMG in the image cache of frame F. */
9275
9276static void
9277cache_image (f, img)
9278 struct frame *f;
9279 struct image *img;
9280{
9281 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
9282 int i;
9283
9284 /* Find a free slot in c->images. */
9285 for (i = 0; i < c->used; ++i)
9286 if (c->images[i] == NULL)
9287 break;
9288
9289 /* If no free slot found, maybe enlarge c->images. */
9290 if (i == c->used && c->used == c->size)
9291 {
9292 c->size *= 2;
9293 c->images = (struct image **) xrealloc (c->images,
9294 c->size * sizeof *c->images);
9295 }
9296
9297 /* Add IMG to c->images, and assign IMG an id. */
9298 c->images[i] = img;
9299 img->id = i;
9300 if (i == c->used)
9301 ++c->used;
9302
9303 /* Add IMG to the cache's hash table. */
9304 i = img->hash % IMAGE_CACHE_BUCKETS_SIZE;
9305 img->next = c->buckets[i];
9306 if (img->next)
9307 img->next->prev = img;
9308 img->prev = NULL;
9309 c->buckets[i] = img;
9310}
9311
9312
9313/* Call FN on every image in the image cache of frame F. Used to mark
9314 Lisp Objects in the image cache. */
9315
9316void
9317forall_images_in_image_cache (f, fn)
9318 struct frame *f;
9319 void (*fn) P_ ((struct image *img));
9320{
9321 if (FRAME_LIVE_P (f) && FRAME_W32_P (f))
9322 {
9323 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
9324 if (c)
9325 {
9326 int i;
9327 for (i = 0; i < c->used; ++i)
9328 if (c->images[i])
9329 fn (c->images[i]);
9330 }
9331 }
9332}
9333
9334
9335\f
9336/***********************************************************************
9337 W32 support code
9338 ***********************************************************************/
9339
6fc2811b
JR
9340static int x_create_x_image_and_pixmap P_ ((struct frame *, int, int, int,
9341 XImage **, Pixmap *));
6fc2811b
JR
9342static void x_put_x_image P_ ((struct frame *, XImage *, Pixmap, int, int));
9343
9344
9345/* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on
9346 frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created.
9347 Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated
ac849ba4
JR
9348 via xmalloc. DEPTH of zero signifies a 24 bit image, otherwise
9349 DEPTH should indicate the bit depth of the image. Print error
9350 messages via image_error if an error occurs. Value is non-zero if
9351 successful. */
6fc2811b
JR
9352
9353static int
9354x_create_x_image_and_pixmap (f, width, height, depth, ximg, pixmap)
9355 struct frame *f;
9356 int width, height, depth;
9357 XImage **ximg;
9358 Pixmap *pixmap;
9359{
ac849ba4
JR
9360 BITMAPINFOHEADER *header;
9361 HDC hdc;
9362 int scanline_width_bits;
9363 int remainder;
9364 int palette_colors = 0;
6fc2811b 9365
ac849ba4
JR
9366 if (depth == 0)
9367 depth = 24;
6fc2811b 9368
ac849ba4
JR
9369 if (depth != 1 && depth != 4 && depth != 8
9370 && depth != 16 && depth != 24 && depth != 32)
9371 {
9372 image_error ("Invalid image bit depth specified", Qnil, Qnil);
9373 return 0;
9374 }
9375
9376 scanline_width_bits = width * depth;
9377 remainder = scanline_width_bits % 32;
9378
9379 if (remainder)
9380 scanline_width_bits += 32 - remainder;
9381
9382 /* Bitmaps with a depth less than 16 need a palette. */
9383 /* BITMAPINFO structure already contains the first RGBQUAD. */
9384 if (depth < 16)
9385 palette_colors = 1 << depth - 1;
9386
9387 *ximg = xmalloc (sizeof (XImage) + palette_colors * sizeof (RGBQUAD));
6fc2811b
JR
9388 if (*ximg == NULL)
9389 {
ac849ba4 9390 image_error ("Unable to allocate memory for XImage", Qnil, Qnil);
6fc2811b
JR
9391 return 0;
9392 }
9393
ac849ba4
JR
9394 header = &((*ximg)->info.bmiHeader);
9395 bzero (&((*ximg)->info), sizeof (BITMAPINFO));
9396 header->biSize = sizeof (*header);
9397 header->biWidth = width;
9398 header->biHeight = -height; /* negative indicates a top-down bitmap. */
9399 header->biPlanes = 1;
9400 header->biBitCount = depth;
9401 header->biCompression = BI_RGB;
9402 header->biClrUsed = palette_colors;
6fc2811b 9403
197edd35 9404 /* TODO: fill in palette. */
35624c03
JR
9405 if (depth == 1)
9406 {
9407 (*ximg)->info.bmiColors[0].rgbBlue = 0;
9408 (*ximg)->info.bmiColors[0].rgbGreen = 0;
9409 (*ximg)->info.bmiColors[0].rgbRed = 0;
9410 (*ximg)->info.bmiColors[0].rgbReserved = 0;
9411 (*ximg)->info.bmiColors[1].rgbBlue = 255;
9412 (*ximg)->info.bmiColors[1].rgbGreen = 255;
9413 (*ximg)->info.bmiColors[1].rgbRed = 255;
9414 (*ximg)->info.bmiColors[1].rgbReserved = 0;
9415 }
197edd35 9416
ac849ba4
JR
9417 hdc = get_frame_dc (f);
9418
9419 /* Create a DIBSection and raster array for the bitmap,
9420 and store its handle in *pixmap. */
197edd35
JR
9421 *pixmap = CreateDIBSection (hdc, &((*ximg)->info),
9422 (depth < 16) ? DIB_PAL_COLORS : DIB_RGB_COLORS,
ac849ba4
JR
9423 &((*ximg)->data), NULL, 0);
9424
9425 /* Realize display palette and garbage all frames. */
9426 release_frame_dc (f, hdc);
9427
9428 if (*pixmap == NULL)
6fc2811b 9429 {
ac849ba4
JR
9430 DWORD err = GetLastError();
9431 Lisp_Object errcode;
9432 /* All system errors are < 10000, so the following is safe. */
9433 XSETINT (errcode, (int) err);
9434 image_error ("Unable to create bitmap, error code %d", errcode, Qnil);
6fc2811b 9435 x_destroy_x_image (*ximg);
6fc2811b
JR
9436 return 0;
9437 }
ac849ba4 9438
6fc2811b
JR
9439 return 1;
9440}
9441
9442
9443/* Destroy XImage XIMG. Free XIMG->data. */
9444
9445static void
9446x_destroy_x_image (ximg)
9447 XImage *ximg;
9448{
9449 xassert (interrupt_input_blocked);
9450 if (ximg)
9451 {
ac849ba4 9452 /* Data will be freed by DestroyObject. */
6fc2811b 9453 ximg->data = NULL;
ac849ba4 9454 xfree (ximg);
6fc2811b
JR
9455 }
9456}
9457
9458
9459/* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT
9460 are width and height of both the image and pixmap. */
9461
9462static void
9463x_put_x_image (f, ximg, pixmap, width, height)
9464 struct frame *f;
9465 XImage *ximg;
9466 Pixmap pixmap;
c9b2104d 9467 int width, height;
6fc2811b 9468{
197edd35
JR
9469#if 0 /* I don't think this is necessary looking at where it is used. */
9470 HDC hdc = get_frame_dc (f);
9471 SetDIBits (hdc, pixmap, 0, height, ximg->data, &(ximg->info), DIB_RGB_COLORS);
9472 release_frame_dc (f, hdc);
6fc2811b 9473#endif
ac849ba4 9474}
6fc2811b
JR
9475
9476\f
9477/***********************************************************************
3cf3436e 9478 File Handling
6fc2811b
JR
9479 ***********************************************************************/
9480
9481static Lisp_Object x_find_image_file P_ ((Lisp_Object));
3cf3436e
JR
9482static char *slurp_file P_ ((char *, int *));
9483
6fc2811b
JR
9484
9485/* Find image file FILE. Look in data-directory, then
9486 x-bitmap-file-path. Value is the full name of the file found, or
9487 nil if not found. */
9488
9489static Lisp_Object
9490x_find_image_file (file)
9491 Lisp_Object file;
9492{
9493 Lisp_Object file_found, search_path;
9494 struct gcpro gcpro1, gcpro2;
9495 int fd;
9496
9497 file_found = Qnil;
9498 search_path = Fcons (Vdata_directory, Vx_bitmap_file_path);
9499 GCPRO2 (file_found, search_path);
9500
9501 /* Try to find FILE in data-directory, then x-bitmap-file-path. */
de2413e9 9502 fd = openp (search_path, file, Qnil, &file_found, Qnil);
7d0393cf 9503
939d6465 9504 if (fd == -1)
6fc2811b
JR
9505 file_found = Qnil;
9506 else
9507 close (fd);
9508
9509 UNGCPRO;
9510 return file_found;
9511}
9512
9513
3cf3436e
JR
9514/* Read FILE into memory. Value is a pointer to a buffer allocated
9515 with xmalloc holding FILE's contents. Value is null if an error
9516 occurred. *SIZE is set to the size of the file. */
9517
9518static char *
9519slurp_file (file, size)
9520 char *file;
9521 int *size;
9522{
9523 FILE *fp = NULL;
9524 char *buf = NULL;
9525 struct stat st;
9526
9527 if (stat (file, &st) == 0
9528 && (fp = fopen (file, "r")) != NULL
9529 && (buf = (char *) xmalloc (st.st_size),
9530 fread (buf, 1, st.st_size, fp) == st.st_size))
9531 {
9532 *size = st.st_size;
9533 fclose (fp);
9534 }
9535 else
9536 {
9537 if (fp)
9538 fclose (fp);
9539 if (buf)
9540 {
9541 xfree (buf);
9542 buf = NULL;
9543 }
9544 }
7d0393cf 9545
3cf3436e
JR
9546 return buf;
9547}
9548
9549
6fc2811b
JR
9550\f
9551/***********************************************************************
9552 XBM images
9553 ***********************************************************************/
9554
217e5be0 9555static int xbm_scan P_ ((char **, char *, char *, int *));
6fc2811b 9556static int xbm_load P_ ((struct frame *f, struct image *img));
217e5be0
JR
9557static int xbm_load_image P_ ((struct frame *f, struct image *img,
9558 char *, char *));
6fc2811b 9559static int xbm_image_p P_ ((Lisp_Object object));
217e5be0
JR
9560static int xbm_read_bitmap_data P_ ((char *, char *, int *, int *,
9561 unsigned char **));
9562static int xbm_file_p P_ ((Lisp_Object));
6fc2811b
JR
9563
9564
9565/* Indices of image specification fields in xbm_format, below. */
9566
9567enum xbm_keyword_index
9568{
9569 XBM_TYPE,
9570 XBM_FILE,
9571 XBM_WIDTH,
9572 XBM_HEIGHT,
9573 XBM_DATA,
9574 XBM_FOREGROUND,
9575 XBM_BACKGROUND,
9576 XBM_ASCENT,
9577 XBM_MARGIN,
9578 XBM_RELIEF,
9579 XBM_ALGORITHM,
9580 XBM_HEURISTIC_MASK,
a05e2bae 9581 XBM_MASK,
6fc2811b
JR
9582 XBM_LAST
9583};
9584
9585/* Vector of image_keyword structures describing the format
9586 of valid XBM image specifications. */
9587
9588static struct image_keyword xbm_format[XBM_LAST] =
9589{
9590 {":type", IMAGE_SYMBOL_VALUE, 1},
9591 {":file", IMAGE_STRING_VALUE, 0},
9592 {":width", IMAGE_POSITIVE_INTEGER_VALUE, 0},
9593 {":height", IMAGE_POSITIVE_INTEGER_VALUE, 0},
9594 {":data", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
217e5be0
JR
9595 {":foreground", IMAGE_STRING_OR_NIL_VALUE, 0},
9596 {":background", IMAGE_STRING_OR_NIL_VALUE, 0},
9597 {":ascent", IMAGE_ASCENT_VALUE, 0},
8edb0a6f 9598 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
6fc2811b 9599 {":relief", IMAGE_INTEGER_VALUE, 0},
a93f4566 9600 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
217e5be0
JR
9601 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9602 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
6fc2811b
JR
9603};
9604
9605/* Structure describing the image type XBM. */
9606
9607static struct image_type xbm_type =
9608{
9609 &Qxbm,
9610 xbm_image_p,
9611 xbm_load,
9612 x_clear_image,
9613 NULL
9614};
9615
9616/* Tokens returned from xbm_scan. */
9617
9618enum xbm_token
9619{
9620 XBM_TK_IDENT = 256,
9621 XBM_TK_NUMBER
9622};
9623
7d0393cf 9624
6fc2811b
JR
9625/* Return non-zero if OBJECT is a valid XBM-type image specification.
9626 A valid specification is a list starting with the symbol `image'
9627 The rest of the list is a property list which must contain an
9628 entry `:type xbm..
9629
9630 If the specification specifies a file to load, it must contain
9631 an entry `:file FILENAME' where FILENAME is a string.
9632
9633 If the specification is for a bitmap loaded from memory it must
9634 contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where
9635 WIDTH and HEIGHT are integers > 0. DATA may be:
9636
9637 1. a string large enough to hold the bitmap data, i.e. it must
9638 have a size >= (WIDTH + 7) / 8 * HEIGHT
9639
9640 2. a bool-vector of size >= WIDTH * HEIGHT
9641
9642 3. a vector of strings or bool-vectors, one for each line of the
9643 bitmap.
9644
217e5be0
JR
9645 4. A string containing an in-memory XBM file. WIDTH and HEIGHT
9646 may not be specified in this case because they are defined in the
9647 XBM file.
9648
6fc2811b
JR
9649 Both the file and data forms may contain the additional entries
9650 `:background COLOR' and `:foreground COLOR'. If not present,
9651 foreground and background of the frame on which the image is
217e5be0 9652 displayed is used. */
6fc2811b
JR
9653
9654static int
9655xbm_image_p (object)
9656 Lisp_Object object;
9657{
9658 struct image_keyword kw[XBM_LAST];
7d0393cf 9659
6fc2811b
JR
9660 bcopy (xbm_format, kw, sizeof kw);
9661 if (!parse_image_spec (object, kw, XBM_LAST, Qxbm))
9662 return 0;
9663
9664 xassert (EQ (kw[XBM_TYPE].value, Qxbm));
9665
9666 if (kw[XBM_FILE].count)
9667 {
9668 if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_DATA].count)
9669 return 0;
9670 }
217e5be0
JR
9671 else if (kw[XBM_DATA].count && xbm_file_p (kw[XBM_DATA].value))
9672 {
9673 /* In-memory XBM file. */
9674 if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_FILE].count)
9675 return 0;
9676 }
6fc2811b
JR
9677 else
9678 {
9679 Lisp_Object data;
9680 int width, height;
9681
9682 /* Entries for `:width', `:height' and `:data' must be present. */
9683 if (!kw[XBM_WIDTH].count
9684 || !kw[XBM_HEIGHT].count
9685 || !kw[XBM_DATA].count)
9686 return 0;
9687
9688 data = kw[XBM_DATA].value;
9689 width = XFASTINT (kw[XBM_WIDTH].value);
9690 height = XFASTINT (kw[XBM_HEIGHT].value);
7d0393cf 9691
6fc2811b
JR
9692 /* Check type of data, and width and height against contents of
9693 data. */
9694 if (VECTORP (data))
9695 {
9696 int i;
7d0393cf 9697
6fc2811b
JR
9698 /* Number of elements of the vector must be >= height. */
9699 if (XVECTOR (data)->size < height)
9700 return 0;
9701
9702 /* Each string or bool-vector in data must be large enough
9703 for one line of the image. */
9704 for (i = 0; i < height; ++i)
9705 {
9706 Lisp_Object elt = XVECTOR (data)->contents[i];
9707
9708 if (STRINGP (elt))
9709 {
d5db4077 9710 if (SCHARS (elt)
6fc2811b
JR
9711 < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR)
9712 return 0;
9713 }
9714 else if (BOOL_VECTOR_P (elt))
9715 {
9716 if (XBOOL_VECTOR (elt)->size < width)
9717 return 0;
9718 }
9719 else
9720 return 0;
9721 }
9722 }
9723 else if (STRINGP (data))
9724 {
d5db4077 9725 if (SCHARS (data)
6fc2811b
JR
9726 < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR * height)
9727 return 0;
9728 }
9729 else if (BOOL_VECTOR_P (data))
9730 {
9731 if (XBOOL_VECTOR (data)->size < width * height)
9732 return 0;
9733 }
9734 else
9735 return 0;
9736 }
9737
6fc2811b
JR
9738 return 1;
9739}
9740
9741
9742/* Scan a bitmap file. FP is the stream to read from. Value is
9743 either an enumerator from enum xbm_token, or a character for a
9744 single-character token, or 0 at end of file. If scanning an
9745 identifier, store the lexeme of the identifier in SVAL. If
9746 scanning a number, store its value in *IVAL. */
9747
9748static int
3cf3436e
JR
9749xbm_scan (s, end, sval, ival)
9750 char **s, *end;
6fc2811b
JR
9751 char *sval;
9752 int *ival;
9753{
9754 int c;
3cf3436e
JR
9755
9756 loop:
9757
6fc2811b 9758 /* Skip white space. */
af3f7be7 9759 while (*s < end && (c = *(*s)++, isspace (c)))
6fc2811b
JR
9760 ;
9761
3cf3436e 9762 if (*s >= end)
6fc2811b
JR
9763 c = 0;
9764 else if (isdigit (c))
9765 {
9766 int value = 0, digit;
7d0393cf 9767
3cf3436e 9768 if (c == '0' && *s < end)
6fc2811b 9769 {
3cf3436e 9770 c = *(*s)++;
6fc2811b
JR
9771 if (c == 'x' || c == 'X')
9772 {
3cf3436e 9773 while (*s < end)
6fc2811b 9774 {
3cf3436e 9775 c = *(*s)++;
6fc2811b
JR
9776 if (isdigit (c))
9777 digit = c - '0';
9778 else if (c >= 'a' && c <= 'f')
9779 digit = c - 'a' + 10;
9780 else if (c >= 'A' && c <= 'F')
9781 digit = c - 'A' + 10;
9782 else
9783 break;
9784 value = 16 * value + digit;
9785 }
9786 }
9787 else if (isdigit (c))
9788 {
9789 value = c - '0';
3cf3436e
JR
9790 while (*s < end
9791 && (c = *(*s)++, isdigit (c)))
6fc2811b
JR
9792 value = 8 * value + c - '0';
9793 }
9794 }
9795 else
9796 {
9797 value = c - '0';
3cf3436e
JR
9798 while (*s < end
9799 && (c = *(*s)++, isdigit (c)))
6fc2811b
JR
9800 value = 10 * value + c - '0';
9801 }
9802
3cf3436e
JR
9803 if (*s < end)
9804 *s = *s - 1;
6fc2811b
JR
9805 *ival = value;
9806 c = XBM_TK_NUMBER;
9807 }
9808 else if (isalpha (c) || c == '_')
9809 {
9810 *sval++ = c;
3cf3436e
JR
9811 while (*s < end
9812 && (c = *(*s)++, (isalnum (c) || c == '_')))
6fc2811b
JR
9813 *sval++ = c;
9814 *sval = 0;
3cf3436e
JR
9815 if (*s < end)
9816 *s = *s - 1;
6fc2811b
JR
9817 c = XBM_TK_IDENT;
9818 }
3cf3436e
JR
9819 else if (c == '/' && **s == '*')
9820 {
9821 /* C-style comment. */
9822 ++*s;
9823 while (**s && (**s != '*' || *(*s + 1) != '/'))
9824 ++*s;
9825 if (**s)
9826 {
9827 *s += 2;
9828 goto loop;
9829 }
9830 }
6fc2811b
JR
9831
9832 return c;
9833}
9834
9835
217e5be0
JR
9836/* XBM bits seem to be backward within bytes compared with how
9837 Windows does things. */
9838static unsigned char reflect_byte (unsigned char orig)
9839{
9840 int i;
9841 unsigned char reflected = 0x00;
9842 for (i = 0; i < 8; i++)
9843 {
9844 if (orig & (0x01 << i))
9845 reflected |= 0x80 >> i;
9846 }
9847 return reflected;
9848}
9849
9850
af3f7be7
JR
9851/* Create a Windows bitmap from X bitmap data. */
9852static HBITMAP
9853w32_create_pixmap_from_bitmap_data (int width, int height, char *data)
9854{
9855 int i, j, w1, w2;
9856 char *bits, *p;
9857 HBITMAP bmp;
9858
9859 w1 = (width + 7) / 8; /* nb of 8bits elt in X bitmap */
9860 w2 = ((width + 15) / 16) * 2; /* nb of 16bits elt in W32 bitmap */
9861 bits = (char *) xmalloc (height * w2);
9862 bzero (bits, height * w2);
9863 for (i = 0; i < height; i++)
9864 {
9865 p = bits + i*w2;
9866 for (j = 0; j < w1; j++)
9867 *p++ = reflect_byte(*data++);
9868 }
9869 bmp = CreateBitmap (width, height, 1, 1, bits);
9870 xfree (bits);
9871
9872 return bmp;
9873}
9874
9875
6fc2811b 9876/* Replacement for XReadBitmapFileData which isn't available under old
3cf3436e
JR
9877 X versions. CONTENTS is a pointer to a buffer to parse; END is the
9878 buffer's end. Set *WIDTH and *HEIGHT to the width and height of
9879 the image. Return in *DATA the bitmap data allocated with xmalloc.
9880 Value is non-zero if successful. DATA null means just test if
9881 CONTENTS looks like an in-memory XBM file. */
6fc2811b
JR
9882
9883static int
3cf3436e
JR
9884xbm_read_bitmap_data (contents, end, width, height, data)
9885 char *contents, *end;
6fc2811b
JR
9886 int *width, *height;
9887 unsigned char **data;
9888{
3cf3436e 9889 char *s = contents;
6fc2811b
JR
9890 char buffer[BUFSIZ];
9891 int padding_p = 0;
9892 int v10 = 0;
af3f7be7 9893 int bytes_per_line, i, nbytes;
6fc2811b
JR
9894 unsigned char *p;
9895 int value;
9896 int LA1;
9897
9898#define match() \
217e5be0 9899 LA1 = xbm_scan (&s, end, buffer, &value)
6fc2811b
JR
9900
9901#define expect(TOKEN) \
9902 if (LA1 != (TOKEN)) \
9903 goto failure; \
9904 else \
7d0393cf 9905 match ()
6fc2811b
JR
9906
9907#define expect_ident(IDENT) \
9908 if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \
9909 match (); \
9910 else \
9911 goto failure
9912
6fc2811b 9913 *width = *height = -1;
3cf3436e
JR
9914 if (data)
9915 *data = NULL;
9916 LA1 = xbm_scan (&s, end, buffer, &value);
6fc2811b
JR
9917
9918 /* Parse defines for width, height and hot-spots. */
9919 while (LA1 == '#')
9920 {
9921 match ();
9922 expect_ident ("define");
9923 expect (XBM_TK_IDENT);
9924
9925 if (LA1 == XBM_TK_NUMBER);
9926 {
9927 char *p = strrchr (buffer, '_');
9928 p = p ? p + 1 : buffer;
9929 if (strcmp (p, "width") == 0)
9930 *width = value;
9931 else if (strcmp (p, "height") == 0)
9932 *height = value;
9933 }
9934 expect (XBM_TK_NUMBER);
9935 }
9936
9937 if (*width < 0 || *height < 0)
9938 goto failure;
3cf3436e
JR
9939 else if (data == NULL)
9940 goto success;
6fc2811b
JR
9941
9942 /* Parse bits. Must start with `static'. */
9943 expect_ident ("static");
9944 if (LA1 == XBM_TK_IDENT)
9945 {
9946 if (strcmp (buffer, "unsigned") == 0)
9947 {
7d0393cf 9948 match ();
6fc2811b
JR
9949 expect_ident ("char");
9950 }
9951 else if (strcmp (buffer, "short") == 0)
9952 {
9953 match ();
9954 v10 = 1;
af3f7be7
JR
9955 if (*width % 16 && *width % 16 < 9)
9956 padding_p = 1;
6fc2811b
JR
9957 }
9958 else if (strcmp (buffer, "char") == 0)
9959 match ();
9960 else
9961 goto failure;
9962 }
7d0393cf 9963 else
6fc2811b
JR
9964 goto failure;
9965
9966 expect (XBM_TK_IDENT);
9967 expect ('[');
9968 expect (']');
9969 expect ('=');
9970 expect ('{');
9971
af3f7be7
JR
9972 bytes_per_line = (*width + 7) / 8 + padding_p;
9973 nbytes = bytes_per_line * *height;
9974 p = *data = (char *) xmalloc (nbytes);
6fc2811b
JR
9975
9976 if (v10)
9977 {
6fc2811b
JR
9978 for (i = 0; i < nbytes; i += 2)
9979 {
9980 int val = value;
9981 expect (XBM_TK_NUMBER);
9982
35624c03 9983 *p++ = ~ val;
af3f7be7 9984 if (!padding_p || ((i + 2) % bytes_per_line))
35624c03 9985 *p++ = ~ (value >> 8);
7d0393cf 9986
6fc2811b
JR
9987 if (LA1 == ',' || LA1 == '}')
9988 match ();
9989 else
9990 goto failure;
9991 }
9992 }
9993 else
9994 {
9995 for (i = 0; i < nbytes; ++i)
9996 {
9997 int val = value;
9998 expect (XBM_TK_NUMBER);
7d0393cf 9999
35624c03 10000 *p++ = ~ val;
217e5be0 10001
6fc2811b
JR
10002 if (LA1 == ',' || LA1 == '}')
10003 match ();
10004 else
10005 goto failure;
10006 }
10007 }
10008
3cf3436e 10009 success:
6fc2811b
JR
10010 return 1;
10011
10012 failure:
3cf3436e
JR
10013
10014 if (data && *data)
6fc2811b
JR
10015 {
10016 xfree (*data);
10017 *data = NULL;
10018 }
10019 return 0;
10020
10021#undef match
10022#undef expect
10023#undef expect_ident
10024}
10025
516eea8e
JR
10026static void convert_mono_to_color_image (f, img, foreground, background)
10027 struct frame *f;
10028 struct image *img;
10029 COLORREF foreground, background;
10030{
10031 HDC hdc, old_img_dc, new_img_dc;
10032 HGDIOBJ old_prev, new_prev;
10033 HBITMAP new_pixmap;
10034
10035 hdc = get_frame_dc (f);
10036 old_img_dc = CreateCompatibleDC (hdc);
10037 new_img_dc = CreateCompatibleDC (hdc);
10038 new_pixmap = CreateCompatibleBitmap (hdc, img->width, img->height);
10039 release_frame_dc (f, hdc);
10040 old_prev = SelectObject (old_img_dc, img->pixmap);
10041 new_prev = SelectObject (new_img_dc, new_pixmap);
10042 SetTextColor (new_img_dc, foreground);
10043 SetBkColor (new_img_dc, background);
10044
10045 BitBlt (new_img_dc, 0, 0, img->width, img->height, old_img_dc,
10046 0, 0, SRCCOPY);
10047
10048 SelectObject (old_img_dc, old_prev);
10049 SelectObject (new_img_dc, new_prev);
10050 DeleteDC (old_img_dc);
10051 DeleteDC (new_img_dc);
10052 DeleteObject (img->pixmap);
10053 if (new_pixmap == 0)
10054 fprintf (stderr, "Failed to convert image to color.\n");
10055 else
10056 img->pixmap = new_pixmap;
10057}
6fc2811b 10058
3cf3436e
JR
10059/* Load XBM image IMG which will be displayed on frame F from buffer
10060 CONTENTS. END is the end of the buffer. Value is non-zero if
10061 successful. */
6fc2811b
JR
10062
10063static int
3cf3436e 10064xbm_load_image (f, img, contents, end)
6fc2811b
JR
10065 struct frame *f;
10066 struct image *img;
3cf3436e 10067 char *contents, *end;
6fc2811b
JR
10068{
10069 int rc;
10070 unsigned char *data;
10071 int success_p = 0;
7d0393cf 10072
3cf3436e 10073 rc = xbm_read_bitmap_data (contents, end, &img->width, &img->height, &data);
6fc2811b
JR
10074 if (rc)
10075 {
6fc2811b
JR
10076 unsigned long foreground = FRAME_FOREGROUND_PIXEL (f);
10077 unsigned long background = FRAME_BACKGROUND_PIXEL (f);
516eea8e 10078 int non_default_colors = 0;
6fc2811b 10079 Lisp_Object value;
7d0393cf 10080
6fc2811b
JR
10081 xassert (img->width > 0 && img->height > 0);
10082
10083 /* Get foreground and background colors, maybe allocate colors. */
10084 value = image_spec_value (img->spec, QCforeground, NULL);
10085 if (!NILP (value))
516eea8e
JR
10086 {
10087 foreground = x_alloc_image_color (f, img, value, foreground);
10088 non_default_colors = 1;
10089 }
6fc2811b
JR
10090 value = image_spec_value (img->spec, QCbackground, NULL);
10091 if (!NILP (value))
a05e2bae
JR
10092 {
10093 background = x_alloc_image_color (f, img, value, background);
10094 img->background = background;
10095 img->background_valid = 1;
516eea8e 10096 non_default_colors = 1;
a05e2bae 10097 }
6fc2811b 10098 img->pixmap
af3f7be7 10099 = w32_create_pixmap_from_bitmap_data (img->width, img->height, data);
ac849ba4 10100
516eea8e
JR
10101 /* If colors were specified, transfer the bitmap to a color one. */
10102 if (non_default_colors)
10103 convert_mono_to_color_image (f, img, foreground, background);
10104
6fc2811b
JR
10105 xfree (data);
10106
10107 if (img->pixmap == 0)
10108 {
10109 x_clear_image (f, img);
3cf3436e 10110 image_error ("Unable to create X pixmap for `%s'", img->spec, Qnil);
6fc2811b
JR
10111 }
10112 else
10113 success_p = 1;
6fc2811b
JR
10114 }
10115 else
10116 image_error ("Error loading XBM image `%s'", img->spec, Qnil);
10117
6fc2811b
JR
10118 return success_p;
10119}
10120
10121
3cf3436e
JR
10122/* Value is non-zero if DATA looks like an in-memory XBM file. */
10123
10124static int
10125xbm_file_p (data)
10126 Lisp_Object data;
10127{
10128 int w, h;
10129 return (STRINGP (data)
d5db4077
KR
10130 && xbm_read_bitmap_data (SDATA (data),
10131 (SDATA (data)
10132 + SBYTES (data)),
3cf3436e
JR
10133 &w, &h, NULL));
10134}
10135
7d0393cf 10136
6fc2811b
JR
10137/* Fill image IMG which is used on frame F with pixmap data. Value is
10138 non-zero if successful. */
10139
10140static int
10141xbm_load (f, img)
10142 struct frame *f;
10143 struct image *img;
10144{
10145 int success_p = 0;
10146 Lisp_Object file_name;
10147
10148 xassert (xbm_image_p (img->spec));
10149
10150 /* If IMG->spec specifies a file name, create a non-file spec from it. */
10151 file_name = image_spec_value (img->spec, QCfile, NULL);
10152 if (STRINGP (file_name))
3cf3436e
JR
10153 {
10154 Lisp_Object file;
10155 char *contents;
10156 int size;
10157 struct gcpro gcpro1;
10158
10159 file = x_find_image_file (file_name);
10160 GCPRO1 (file);
10161 if (!STRINGP (file))
10162 {
10163 image_error ("Cannot find image file `%s'", file_name, Qnil);
10164 UNGCPRO;
10165 return 0;
10166 }
10167
d5db4077 10168 contents = slurp_file (SDATA (file), &size);
3cf3436e
JR
10169 if (contents == NULL)
10170 {
10171 image_error ("Error loading XBM image `%s'", img->spec, Qnil);
10172 UNGCPRO;
10173 return 0;
10174 }
10175
10176 success_p = xbm_load_image (f, img, contents, contents + size);
10177 UNGCPRO;
10178 }
6fc2811b
JR
10179 else
10180 {
10181 struct image_keyword fmt[XBM_LAST];
10182 Lisp_Object data;
6fc2811b
JR
10183 unsigned long foreground = FRAME_FOREGROUND_PIXEL (f);
10184 unsigned long background = FRAME_BACKGROUND_PIXEL (f);
516eea8e 10185 int non_default_colors = 0;
6fc2811b
JR
10186 char *bits;
10187 int parsed_p;
3cf3436e
JR
10188 int in_memory_file_p = 0;
10189
10190 /* See if data looks like an in-memory XBM file. */
10191 data = image_spec_value (img->spec, QCdata, NULL);
10192 in_memory_file_p = xbm_file_p (data);
6fc2811b 10193
217e5be0 10194 /* Parse the image specification. */
6fc2811b
JR
10195 bcopy (xbm_format, fmt, sizeof fmt);
10196 parsed_p = parse_image_spec (img->spec, fmt, XBM_LAST, Qxbm);
10197 xassert (parsed_p);
10198
10199 /* Get specified width, and height. */
3cf3436e
JR
10200 if (!in_memory_file_p)
10201 {
10202 img->width = XFASTINT (fmt[XBM_WIDTH].value);
10203 img->height = XFASTINT (fmt[XBM_HEIGHT].value);
10204 xassert (img->width > 0 && img->height > 0);
10205 }
217e5be0 10206
6fc2811b 10207 /* Get foreground and background colors, maybe allocate colors. */
3cf3436e
JR
10208 if (fmt[XBM_FOREGROUND].count
10209 && STRINGP (fmt[XBM_FOREGROUND].value))
516eea8e
JR
10210 {
10211 foreground = x_alloc_image_color (f, img, fmt[XBM_FOREGROUND].value,
10212 foreground);
10213 non_default_colors = 1;
10214 }
10215
3cf3436e
JR
10216 if (fmt[XBM_BACKGROUND].count
10217 && STRINGP (fmt[XBM_BACKGROUND].value))
516eea8e
JR
10218 {
10219 background = x_alloc_image_color (f, img, fmt[XBM_BACKGROUND].value,
10220 background);
10221 non_default_colors = 1;
10222 }
6fc2811b 10223
3cf3436e 10224 if (in_memory_file_p)
d5db4077
KR
10225 success_p = xbm_load_image (f, img, SDATA (data),
10226 (SDATA (data)
10227 + SBYTES (data)));
3cf3436e 10228 else
6fc2811b 10229 {
3cf3436e
JR
10230 if (VECTORP (data))
10231 {
10232 int i;
10233 char *p;
10234 int nbytes = (img->width + BITS_PER_CHAR - 1) / BITS_PER_CHAR;
7d0393cf 10235
3cf3436e
JR
10236 p = bits = (char *) alloca (nbytes * img->height);
10237 for (i = 0; i < img->height; ++i, p += nbytes)
10238 {
10239 Lisp_Object line = XVECTOR (data)->contents[i];
10240 if (STRINGP (line))
d5db4077 10241 bcopy (SDATA (line), p, nbytes);
3cf3436e
JR
10242 else
10243 bcopy (XBOOL_VECTOR (line)->data, p, nbytes);
10244 }
10245 }
10246 else if (STRINGP (data))
d5db4077 10247 bits = SDATA (data);
3cf3436e
JR
10248 else
10249 bits = XBOOL_VECTOR (data)->data;
af3f7be7 10250
3cf3436e 10251 /* Create the pixmap. */
3cf3436e 10252 img->pixmap
af3f7be7
JR
10253 = w32_create_pixmap_from_bitmap_data (img->width, img->height,
10254 bits);
10255
516eea8e
JR
10256 /* If colors were specified, transfer the bitmap to a color one. */
10257 if (non_default_colors)
10258 convert_mono_to_color_image (f, img, foreground, background);
10259
3cf3436e
JR
10260 if (img->pixmap)
10261 success_p = 1;
10262 else
6fc2811b 10263 {
3cf3436e
JR
10264 image_error ("Unable to create pixmap for XBM image `%s'",
10265 img->spec, Qnil);
10266 x_clear_image (f, img);
6fc2811b
JR
10267 }
10268 }
6fc2811b
JR
10269 }
10270
10271 return success_p;
10272}
7d0393cf 10273
6fc2811b
JR
10274
10275\f
10276/***********************************************************************
10277 XPM images
10278 ***********************************************************************/
10279
7d0393cf 10280#if HAVE_XPM
6fc2811b
JR
10281
10282static int xpm_image_p P_ ((Lisp_Object object));
10283static int xpm_load P_ ((struct frame *f, struct image *img));
10284static int xpm_valid_color_symbols_p P_ ((Lisp_Object));
10285
10286#include "X11/xpm.h"
10287
10288/* The symbol `xpm' identifying XPM-format images. */
10289
10290Lisp_Object Qxpm;
10291
10292/* Indices of image specification fields in xpm_format, below. */
10293
10294enum xpm_keyword_index
10295{
10296 XPM_TYPE,
10297 XPM_FILE,
10298 XPM_DATA,
10299 XPM_ASCENT,
10300 XPM_MARGIN,
10301 XPM_RELIEF,
10302 XPM_ALGORITHM,
10303 XPM_HEURISTIC_MASK,
a05e2bae 10304 XPM_MASK,
6fc2811b 10305 XPM_COLOR_SYMBOLS,
a05e2bae 10306 XPM_BACKGROUND,
6fc2811b
JR
10307 XPM_LAST
10308};
10309
10310/* Vector of image_keyword structures describing the format
10311 of valid XPM image specifications. */
10312
10313static struct image_keyword xpm_format[XPM_LAST] =
10314{
10315 {":type", IMAGE_SYMBOL_VALUE, 1},
10316 {":file", IMAGE_STRING_VALUE, 0},
10317 {":data", IMAGE_STRING_VALUE, 0},
8f92c555 10318 {":ascent", IMAGE_ASCENT_VALUE, 0},
8edb0a6f 10319 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
6fc2811b 10320 {":relief", IMAGE_INTEGER_VALUE, 0},
a93f4566 10321 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6fc2811b 10322 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
a05e2bae
JR
10323 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
10324 {":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
10325 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
6fc2811b
JR
10326};
10327
197edd35 10328/* Structure describing the image type XPM. */
6fc2811b
JR
10329
10330static struct image_type xpm_type =
10331{
10332 &Qxpm,
10333 xpm_image_p,
10334 xpm_load,
10335 x_clear_image,
10336 NULL
10337};
10338
10339
10340/* Value is non-zero if COLOR_SYMBOLS is a valid color symbols list
10341 for XPM images. Such a list must consist of conses whose car and
10342 cdr are strings. */
10343
10344static int
10345xpm_valid_color_symbols_p (color_symbols)
10346 Lisp_Object color_symbols;
10347{
10348 while (CONSP (color_symbols))
10349 {
10350 Lisp_Object sym = XCAR (color_symbols);
10351 if (!CONSP (sym)
10352 || !STRINGP (XCAR (sym))
10353 || !STRINGP (XCDR (sym)))
10354 break;
10355 color_symbols = XCDR (color_symbols);
10356 }
10357
10358 return NILP (color_symbols);
10359}
10360
10361
10362/* Value is non-zero if OBJECT is a valid XPM image specification. */
10363
10364static int
10365xpm_image_p (object)
10366 Lisp_Object object;
10367{
10368 struct image_keyword fmt[XPM_LAST];
10369 bcopy (xpm_format, fmt, sizeof fmt);
10370 return (parse_image_spec (object, fmt, XPM_LAST, Qxpm)
10371 /* Either `:file' or `:data' must be present. */
10372 && fmt[XPM_FILE].count + fmt[XPM_DATA].count == 1
10373 /* Either no `:color-symbols' or it's a list of conses
10374 whose car and cdr are strings. */
10375 && (fmt[XPM_COLOR_SYMBOLS].count == 0
8f92c555 10376 || xpm_valid_color_symbols_p (fmt[XPM_COLOR_SYMBOLS].value)));
6fc2811b
JR
10377}
10378
10379
10380/* Load image IMG which will be displayed on frame F. Value is
10381 non-zero if successful. */
10382
10383static int
10384xpm_load (f, img)
10385 struct frame *f;
10386 struct image *img;
10387{
10388 int rc, i;
10389 XpmAttributes attrs;
10390 Lisp_Object specified_file, color_symbols;
10391
10392 /* Configure the XPM lib. Use the visual of frame F. Allocate
10393 close colors. Return colors allocated. */
10394 bzero (&attrs, sizeof attrs);
dfff8a69
JR
10395 attrs.visual = FRAME_X_VISUAL (f);
10396 attrs.colormap = FRAME_X_COLORMAP (f);
6fc2811b 10397 attrs.valuemask |= XpmVisual;
dfff8a69 10398 attrs.valuemask |= XpmColormap;
6fc2811b 10399 attrs.valuemask |= XpmReturnAllocPixels;
dfff8a69 10400#ifdef XpmAllocCloseColors
6fc2811b
JR
10401 attrs.alloc_close_colors = 1;
10402 attrs.valuemask |= XpmAllocCloseColors;
dfff8a69
JR
10403#else
10404 attrs.closeness = 600;
10405 attrs.valuemask |= XpmCloseness;
10406#endif
6fc2811b
JR
10407
10408 /* If image specification contains symbolic color definitions, add
10409 these to `attrs'. */
10410 color_symbols = image_spec_value (img->spec, QCcolor_symbols, NULL);
10411 if (CONSP (color_symbols))
10412 {
10413 Lisp_Object tail;
10414 XpmColorSymbol *xpm_syms;
10415 int i, size;
7d0393cf 10416
6fc2811b
JR
10417 attrs.valuemask |= XpmColorSymbols;
10418
10419 /* Count number of symbols. */
10420 attrs.numsymbols = 0;
10421 for (tail = color_symbols; CONSP (tail); tail = XCDR (tail))
10422 ++attrs.numsymbols;
10423
10424 /* Allocate an XpmColorSymbol array. */
10425 size = attrs.numsymbols * sizeof *xpm_syms;
10426 xpm_syms = (XpmColorSymbol *) alloca (size);
10427 bzero (xpm_syms, size);
10428 attrs.colorsymbols = xpm_syms;
10429
10430 /* Fill the color symbol array. */
10431 for (tail = color_symbols, i = 0;
10432 CONSP (tail);
10433 ++i, tail = XCDR (tail))
10434 {
10435 Lisp_Object name = XCAR (XCAR (tail));
10436 Lisp_Object color = XCDR (XCAR (tail));
d5db4077
KR
10437 xpm_syms[i].name = (char *) alloca (SCHARS (name) + 1);
10438 strcpy (xpm_syms[i].name, SDATA (name));
10439 xpm_syms[i].value = (char *) alloca (SCHARS (color) + 1);
10440 strcpy (xpm_syms[i].value, SDATA (color));
6fc2811b
JR
10441 }
10442 }
10443
10444 /* Create a pixmap for the image, either from a file, or from a
10445 string buffer containing data in the same format as an XPM file. */
10446 BLOCK_INPUT;
10447 specified_file = image_spec_value (img->spec, QCfile, NULL);
10448 if (STRINGP (specified_file))
10449 {
10450 Lisp_Object file = x_find_image_file (specified_file);
10451 if (!STRINGP (file))
10452 {
10453 image_error ("Cannot find image file `%s'", specified_file, Qnil);
10454 UNBLOCK_INPUT;
10455 return 0;
10456 }
7d0393cf 10457
6fc2811b 10458 rc = XpmReadFileToPixmap (NULL, FRAME_W32_WINDOW (f),
d5db4077 10459 SDATA (file), &img->pixmap, &img->mask,
6fc2811b
JR
10460 &attrs);
10461 }
10462 else
10463 {
10464 Lisp_Object buffer = image_spec_value (img->spec, QCdata, NULL);
10465 rc = XpmCreatePixmapFromBuffer (NULL, FRAME_W32_WINDOW (f),
d5db4077 10466 SDATA (buffer),
6fc2811b
JR
10467 &img->pixmap, &img->mask,
10468 &attrs);
10469 }
10470 UNBLOCK_INPUT;
10471
10472 if (rc == XpmSuccess)
10473 {
10474 /* Remember allocated colors. */
10475 img->ncolors = attrs.nalloc_pixels;
10476 img->colors = (unsigned long *) xmalloc (img->ncolors
10477 * sizeof *img->colors);
10478 for (i = 0; i < attrs.nalloc_pixels; ++i)
10479 img->colors[i] = attrs.alloc_pixels[i];
10480
10481 img->width = attrs.width;
10482 img->height = attrs.height;
10483 xassert (img->width > 0 && img->height > 0);
10484
10485 /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */
10486 BLOCK_INPUT;
10487 XpmFreeAttributes (&attrs);
10488 UNBLOCK_INPUT;
10489 }
10490 else
10491 {
10492 switch (rc)
10493 {
10494 case XpmOpenFailed:
10495 image_error ("Error opening XPM file (%s)", img->spec, Qnil);
10496 break;
7d0393cf 10497
6fc2811b
JR
10498 case XpmFileInvalid:
10499 image_error ("Invalid XPM file (%s)", img->spec, Qnil);
10500 break;
7d0393cf 10501
6fc2811b
JR
10502 case XpmNoMemory:
10503 image_error ("Out of memory (%s)", img->spec, Qnil);
10504 break;
7d0393cf 10505
6fc2811b
JR
10506 case XpmColorFailed:
10507 image_error ("Color allocation error (%s)", img->spec, Qnil);
10508 break;
7d0393cf 10509
6fc2811b
JR
10510 default:
10511 image_error ("Unknown error (%s)", img->spec, Qnil);
10512 break;
10513 }
10514 }
10515
10516 return rc == XpmSuccess;
10517}
10518
10519#endif /* HAVE_XPM != 0 */
10520
10521\f
767b1ff0 10522#if 0 /* TODO : Color tables on W32. */
6fc2811b
JR
10523/***********************************************************************
10524 Color table
10525 ***********************************************************************/
10526
10527/* An entry in the color table mapping an RGB color to a pixel color. */
10528
10529struct ct_color
10530{
10531 int r, g, b;
10532 unsigned long pixel;
10533
10534 /* Next in color table collision list. */
10535 struct ct_color *next;
10536};
10537
10538/* The bucket vector size to use. Must be prime. */
10539
10540#define CT_SIZE 101
10541
10542/* Value is a hash of the RGB color given by R, G, and B. */
10543
10544#define CT_HASH_RGB(R, G, B) (((R) << 16) ^ ((G) << 8) ^ (B))
10545
10546/* The color hash table. */
10547
10548struct ct_color **ct_table;
10549
10550/* Number of entries in the color table. */
10551
10552int ct_colors_allocated;
10553
10554/* Function prototypes. */
10555
10556static void init_color_table P_ ((void));
10557static void free_color_table P_ ((void));
10558static unsigned long *colors_in_color_table P_ ((int *n));
10559static unsigned long lookup_rgb_color P_ ((struct frame *f, int r, int g, int b));
10560static unsigned long lookup_pixel_color P_ ((struct frame *f, unsigned long p));
10561
10562
10563/* Initialize the color table. */
10564
10565static void
10566init_color_table ()
10567{
10568 int size = CT_SIZE * sizeof (*ct_table);
10569 ct_table = (struct ct_color **) xmalloc (size);
10570 bzero (ct_table, size);
10571 ct_colors_allocated = 0;
10572}
10573
10574
10575/* Free memory associated with the color table. */
10576
10577static void
10578free_color_table ()
10579{
10580 int i;
10581 struct ct_color *p, *next;
10582
10583 for (i = 0; i < CT_SIZE; ++i)
10584 for (p = ct_table[i]; p; p = next)
10585 {
10586 next = p->next;
10587 xfree (p);
10588 }
10589
10590 xfree (ct_table);
10591 ct_table = NULL;
10592}
10593
10594
10595/* Value is a pixel color for RGB color R, G, B on frame F. If an
10596 entry for that color already is in the color table, return the
10597 pixel color of that entry. Otherwise, allocate a new color for R,
10598 G, B, and make an entry in the color table. */
10599
10600static unsigned long
10601lookup_rgb_color (f, r, g, b)
10602 struct frame *f;
10603 int r, g, b;
10604{
10605 unsigned hash = CT_HASH_RGB (r, g, b);
10606 int i = hash % CT_SIZE;
10607 struct ct_color *p;
10608
10609 for (p = ct_table[i]; p; p = p->next)
10610 if (p->r == r && p->g == g && p->b == b)
10611 break;
10612
10613 if (p == NULL)
10614 {
10615 COLORREF color;
10616 Colormap cmap;
10617 int rc;
10618
10619 color = PALETTERGB (r, g, b);
10620
10621 ++ct_colors_allocated;
10622
10623 p = (struct ct_color *) xmalloc (sizeof *p);
10624 p->r = r;
10625 p->g = g;
10626 p->b = b;
10627 p->pixel = color;
10628 p->next = ct_table[i];
10629 ct_table[i] = p;
10630 }
10631
10632 return p->pixel;
10633}
10634
10635
10636/* Look up pixel color PIXEL which is used on frame F in the color
10637 table. If not already present, allocate it. Value is PIXEL. */
10638
10639static unsigned long
10640lookup_pixel_color (f, pixel)
10641 struct frame *f;
10642 unsigned long pixel;
10643{
10644 int i = pixel % CT_SIZE;
10645 struct ct_color *p;
10646
10647 for (p = ct_table[i]; p; p = p->next)
10648 if (p->pixel == pixel)
10649 break;
10650
10651 if (p == NULL)
10652 {
10653 XColor color;
10654 Colormap cmap;
10655 int rc;
10656
10657 BLOCK_INPUT;
7d0393cf 10658
6fc2811b
JR
10659 cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f));
10660 color.pixel = pixel;
10661 XQueryColor (NULL, cmap, &color);
10662 rc = x_alloc_nearest_color (f, cmap, &color);
10663 UNBLOCK_INPUT;
10664
10665 if (rc)
10666 {
10667 ++ct_colors_allocated;
7d0393cf 10668
6fc2811b
JR
10669 p = (struct ct_color *) xmalloc (sizeof *p);
10670 p->r = color.red;
10671 p->g = color.green;
10672 p->b = color.blue;
10673 p->pixel = pixel;
10674 p->next = ct_table[i];
10675 ct_table[i] = p;
10676 }
10677 else
10678 return FRAME_FOREGROUND_PIXEL (f);
10679 }
10680 return p->pixel;
10681}
10682
10683
10684/* Value is a vector of all pixel colors contained in the color table,
10685 allocated via xmalloc. Set *N to the number of colors. */
10686
10687static unsigned long *
10688colors_in_color_table (n)
10689 int *n;
10690{
10691 int i, j;
10692 struct ct_color *p;
10693 unsigned long *colors;
10694
10695 if (ct_colors_allocated == 0)
10696 {
10697 *n = 0;
10698 colors = NULL;
10699 }
10700 else
10701 {
10702 colors = (unsigned long *) xmalloc (ct_colors_allocated
10703 * sizeof *colors);
10704 *n = ct_colors_allocated;
7d0393cf 10705
6fc2811b
JR
10706 for (i = j = 0; i < CT_SIZE; ++i)
10707 for (p = ct_table[i]; p; p = p->next)
10708 colors[j++] = p->pixel;
10709 }
10710
10711 return colors;
10712}
10713
767b1ff0 10714#endif /* TODO */
6fc2811b
JR
10715
10716\f
10717/***********************************************************************
10718 Algorithms
10719 ***********************************************************************/
3cf3436e
JR
10720static XColor *x_to_xcolors P_ ((struct frame *, struct image *, int));
10721static void x_from_xcolors P_ ((struct frame *, struct image *, XColor *));
10722static void x_detect_edges P_ ((struct frame *, struct image *, int[9], int));
ac849ba4 10723static void XPutPixel (XImage *, int, int, COLORREF);
3cf3436e
JR
10724
10725/* Non-zero means draw a cross on images having `:conversion
10726 disabled'. */
6fc2811b 10727
3cf3436e 10728int cross_disabled_images;
6fc2811b 10729
3cf3436e
JR
10730/* Edge detection matrices for different edge-detection
10731 strategies. */
6fc2811b 10732
3cf3436e
JR
10733static int emboss_matrix[9] = {
10734 /* x - 1 x x + 1 */
10735 2, -1, 0, /* y - 1 */
10736 -1, 0, 1, /* y */
10737 0, 1, -2 /* y + 1 */
10738};
10739
10740static int laplace_matrix[9] = {
10741 /* x - 1 x x + 1 */
10742 1, 0, 0, /* y - 1 */
10743 0, 0, 0, /* y */
10744 0, 0, -1 /* y + 1 */
10745};
10746
10747/* Value is the intensity of the color whose red/green/blue values
10748 are R, G, and B. */
10749
10750#define COLOR_INTENSITY(R, G, B) ((2 * (R) + 3 * (G) + (B)) / 6)
10751
10752
10753/* On frame F, return an array of XColor structures describing image
10754 IMG->pixmap. Each XColor structure has its pixel color set. RGB_P
10755 non-zero means also fill the red/green/blue members of the XColor
10756 structures. Value is a pointer to the array of XColors structures,
10757 allocated with xmalloc; it must be freed by the caller. */
10758
10759static XColor *
10760x_to_xcolors (f, img, rgb_p)
10761 struct frame *f;
10762 struct image *img;
10763 int rgb_p;
10764{
10765 int x, y;
10766 XColor *colors, *p;
197edd35
JR
10767 HDC hdc, bmpdc;
10768 HGDIOBJ prev;
3cf3436e
JR
10769
10770 colors = (XColor *) xmalloc (img->width * img->height * sizeof *colors);
197edd35
JR
10771
10772 /* Load the image into a memory device context. */
10773 hdc = get_frame_dc (f);
10774 bmpdc = CreateCompatibleDC (hdc);
10775 release_frame_dc (f, hdc);
10776 prev = SelectObject (bmpdc, img->pixmap);
3cf3436e
JR
10777
10778 /* Fill the `pixel' members of the XColor array. I wished there
10779 were an easy and portable way to circumvent XGetPixel. */
10780 p = colors;
10781 for (y = 0; y < img->height; ++y)
10782 {
10783 XColor *row = p;
7d0393cf 10784
3cf3436e 10785 for (x = 0; x < img->width; ++x, ++p)
197edd35
JR
10786 {
10787 /* TODO: palette support needed here? */
10788 p->pixel = GetPixel (bmpdc, x, y);
3cf3436e 10789
197edd35
JR
10790 if (rgb_p)
10791 {
10792 p->red = 256 * GetRValue (p->pixel);
10793 p->green = 256 * GetGValue (p->pixel);
10794 p->blue = 256 * GetBValue (p->pixel);
10795 }
10796 }
3cf3436e
JR
10797 }
10798
197edd35
JR
10799 SelectObject (bmpdc, prev);
10800 DeleteDC (bmpdc);
10801
3cf3436e
JR
10802 return colors;
10803}
10804
ac849ba4
JR
10805/* Put a pixel of COLOR at position X, Y in XIMG. XIMG must have been
10806 created with CreateDIBSection, with the pointer to the bit values
10807 stored in ximg->data. */
10808
10809static void XPutPixel (ximg, x, y, color)
10810 XImage * ximg;
10811 int x, y;
10812 COLORREF color;
10813{
10814 int width = ximg->info.bmiHeader.biWidth;
10815 int height = ximg->info.bmiHeader.biHeight;
10816 int rowbytes = width * 3;
10817 unsigned char * pixel;
10818
10819 /* Don't support putting pixels in images with palettes. */
10820 xassert (ximg->info.bmiHeader.biBitCount == 24);
10821
10822 /* Ensure scanlines are aligned on 4 byte boundaries. */
10823 if (rowbytes % 4)
10824 rowbytes += 4 - (rowbytes % 4);
10825
10826 pixel = ximg->data + y * rowbytes + x * 3;
adda530b
JR
10827 /* Windows bitmaps are in BGR order. */
10828 *pixel = GetBValue (color);
35624c03 10829 *(pixel + 1) = GetGValue (color);
adda530b 10830 *(pixel + 2) = GetRValue (color);
ac849ba4
JR
10831}
10832
3cf3436e
JR
10833
10834/* Create IMG->pixmap from an array COLORS of XColor structures, whose
10835 RGB members are set. F is the frame on which this all happens.
10836 COLORS will be freed; an existing IMG->pixmap will be freed, too. */
6fc2811b
JR
10837
10838static void
3cf3436e 10839x_from_xcolors (f, img, colors)
6fc2811b 10840 struct frame *f;
3cf3436e 10841 struct image *img;
6fc2811b 10842 XColor *colors;
6fc2811b 10843{
3cf3436e
JR
10844 int x, y;
10845 XImage *oimg;
10846 Pixmap pixmap;
10847 XColor *p;
ac849ba4 10848#if 0 /* TODO: color tables. */
3cf3436e 10849 init_color_table ();
ac849ba4 10850#endif
3cf3436e
JR
10851 x_create_x_image_and_pixmap (f, img->width, img->height, 0,
10852 &oimg, &pixmap);
10853 p = colors;
10854 for (y = 0; y < img->height; ++y)
10855 for (x = 0; x < img->width; ++x, ++p)
10856 {
10857 unsigned long pixel;
ac849ba4 10858#if 0 /* TODO: color tables. */
3cf3436e 10859 pixel = lookup_rgb_color (f, p->red, p->green, p->blue);
ac849ba4 10860#else
197edd35 10861 pixel = PALETTERGB (p->red / 256, p->green / 256, p->blue / 256);
ac849ba4 10862#endif
3cf3436e
JR
10863 XPutPixel (oimg, x, y, pixel);
10864 }
6fc2811b 10865
3cf3436e
JR
10866 xfree (colors);
10867 x_clear_image_1 (f, img, 1, 0, 1);
6fc2811b 10868
3cf3436e
JR
10869 x_put_x_image (f, oimg, pixmap, img->width, img->height);
10870 x_destroy_x_image (oimg);
10871 img->pixmap = pixmap;
ac849ba4 10872#if 0 /* TODO: color tables. */
3cf3436e
JR
10873 img->colors = colors_in_color_table (&img->ncolors);
10874 free_color_table ();
ac849ba4 10875#endif
6fc2811b
JR
10876}
10877
10878
3cf3436e
JR
10879/* On frame F, perform edge-detection on image IMG.
10880
10881 MATRIX is a nine-element array specifying the transformation
10882 matrix. See emboss_matrix for an example.
7d0393cf 10883
3cf3436e
JR
10884 COLOR_ADJUST is a color adjustment added to each pixel of the
10885 outgoing image. */
6fc2811b
JR
10886
10887static void
3cf3436e 10888x_detect_edges (f, img, matrix, color_adjust)
6fc2811b 10889 struct frame *f;
3cf3436e
JR
10890 struct image *img;
10891 int matrix[9], color_adjust;
6fc2811b 10892{
3cf3436e
JR
10893 XColor *colors = x_to_xcolors (f, img, 1);
10894 XColor *new, *p;
10895 int x, y, i, sum;
10896
10897 for (i = sum = 0; i < 9; ++i)
10898 sum += abs (matrix[i]);
10899
10900#define COLOR(A, X, Y) ((A) + (Y) * img->width + (X))
10901
10902 new = (XColor *) xmalloc (img->width * img->height * sizeof *new);
10903
10904 for (y = 0; y < img->height; ++y)
10905 {
10906 p = COLOR (new, 0, y);
10907 p->red = p->green = p->blue = 0xffff/2;
10908 p = COLOR (new, img->width - 1, y);
10909 p->red = p->green = p->blue = 0xffff/2;
10910 }
7d0393cf 10911
3cf3436e
JR
10912 for (x = 1; x < img->width - 1; ++x)
10913 {
10914 p = COLOR (new, x, 0);
10915 p->red = p->green = p->blue = 0xffff/2;
10916 p = COLOR (new, x, img->height - 1);
10917 p->red = p->green = p->blue = 0xffff/2;
10918 }
10919
10920 for (y = 1; y < img->height - 1; ++y)
10921 {
10922 p = COLOR (new, 1, y);
7d0393cf 10923
3cf3436e
JR
10924 for (x = 1; x < img->width - 1; ++x, ++p)
10925 {
10926 int r, g, b, y1, x1;
10927
10928 r = g = b = i = 0;
10929 for (y1 = y - 1; y1 < y + 2; ++y1)
10930 for (x1 = x - 1; x1 < x + 2; ++x1, ++i)
10931 if (matrix[i])
10932 {
10933 XColor *t = COLOR (colors, x1, y1);
10934 r += matrix[i] * t->red;
10935 g += matrix[i] * t->green;
10936 b += matrix[i] * t->blue;
10937 }
10938
10939 r = (r / sum + color_adjust) & 0xffff;
10940 g = (g / sum + color_adjust) & 0xffff;
10941 b = (b / sum + color_adjust) & 0xffff;
10942 p->red = p->green = p->blue = COLOR_INTENSITY (r, g, b);
10943 }
10944 }
10945
10946 xfree (colors);
10947 x_from_xcolors (f, img, new);
10948
10949#undef COLOR
10950}
10951
10952
10953/* Perform the pre-defined `emboss' edge-detection on image IMG
10954 on frame F. */
10955
10956static void
10957x_emboss (f, img)
10958 struct frame *f;
10959 struct image *img;
10960{
10961 x_detect_edges (f, img, emboss_matrix, 0xffff / 2);
6fc2811b 10962}
3cf3436e 10963
6fc2811b
JR
10964
10965/* Transform image IMG which is used on frame F with a Laplace
10966 edge-detection algorithm. The result is an image that can be used
10967 to draw disabled buttons, for example. */
10968
10969static void
10970x_laplace (f, img)
10971 struct frame *f;
10972 struct image *img;
10973{
3cf3436e
JR
10974 x_detect_edges (f, img, laplace_matrix, 45000);
10975}
6fc2811b 10976
6fc2811b 10977
3cf3436e
JR
10978/* Perform edge-detection on image IMG on frame F, with specified
10979 transformation matrix MATRIX and color-adjustment COLOR_ADJUST.
6fc2811b 10980
3cf3436e 10981 MATRIX must be either
6fc2811b 10982
3cf3436e
JR
10983 - a list of at least 9 numbers in row-major form
10984 - a vector of at least 9 numbers
6fc2811b 10985
3cf3436e
JR
10986 COLOR_ADJUST nil means use a default; otherwise it must be a
10987 number. */
6fc2811b 10988
3cf3436e
JR
10989static void
10990x_edge_detection (f, img, matrix, color_adjust)
10991 struct frame *f;
10992 struct image *img;
10993 Lisp_Object matrix, color_adjust;
10994{
10995 int i = 0;
10996 int trans[9];
7d0393cf 10997
3cf3436e 10998 if (CONSP (matrix))
6fc2811b 10999 {
3cf3436e
JR
11000 for (i = 0;
11001 i < 9 && CONSP (matrix) && NUMBERP (XCAR (matrix));
11002 ++i, matrix = XCDR (matrix))
11003 trans[i] = XFLOATINT (XCAR (matrix));
11004 }
11005 else if (VECTORP (matrix) && ASIZE (matrix) >= 9)
11006 {
11007 for (i = 0; i < 9 && NUMBERP (AREF (matrix, i)); ++i)
11008 trans[i] = XFLOATINT (AREF (matrix, i));
11009 }
11010
11011 if (NILP (color_adjust))
11012 color_adjust = make_number (0xffff / 2);
11013
11014 if (i == 9 && NUMBERP (color_adjust))
11015 x_detect_edges (f, img, trans, (int) XFLOATINT (color_adjust));
11016}
11017
6fc2811b 11018
3cf3436e 11019/* Transform image IMG on frame F so that it looks disabled. */
6fc2811b 11020
3cf3436e
JR
11021static void
11022x_disable_image (f, img)
11023 struct frame *f;
11024 struct image *img;
11025{
ac849ba4 11026 struct w32_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3cf3436e 11027
ac849ba4 11028 if (dpyinfo->n_planes * dpyinfo->n_cbits >= 2)
3cf3436e
JR
11029 {
11030 /* Color (or grayscale). Convert to gray, and equalize. Just
11031 drawing such images with a stipple can look very odd, so
11032 we're using this method instead. */
11033 XColor *colors = x_to_xcolors (f, img, 1);
11034 XColor *p, *end;
11035 const int h = 15000;
11036 const int l = 30000;
11037
11038 for (p = colors, end = colors + img->width * img->height;
11039 p < end;
11040 ++p)
6fc2811b 11041 {
3cf3436e
JR
11042 int i = COLOR_INTENSITY (p->red, p->green, p->blue);
11043 int i2 = (0xffff - h - l) * i / 0xffff + l;
11044 p->red = p->green = p->blue = i2;
6fc2811b
JR
11045 }
11046
3cf3436e 11047 x_from_xcolors (f, img, colors);
6fc2811b
JR
11048 }
11049
3cf3436e
JR
11050 /* Draw a cross over the disabled image, if we must or if we
11051 should. */
ac849ba4 11052 if (dpyinfo->n_planes * dpyinfo->n_cbits < 2 || cross_disabled_images)
3cf3436e 11053 {
197edd35
JR
11054 HDC hdc, bmpdc;
11055 HGDIOBJ prev;
11056
11057 hdc = get_frame_dc (f);
11058 bmpdc = CreateCompatibleDC (hdc);
11059 release_frame_dc (f, hdc);
11060
11061 prev = SelectObject (bmpdc, img->pixmap);
6fc2811b 11062
197edd35
JR
11063 SetTextColor (bmpdc, BLACK_PIX_DEFAULT (f));
11064 MoveToEx (bmpdc, 0, 0, NULL);
11065 LineTo (bmpdc, img->width - 1, img->height - 1);
11066 MoveToEx (bmpdc, 0, img->height - 1, NULL);
11067 LineTo (bmpdc, img->width - 1, 0);
6fc2811b 11068
3cf3436e
JR
11069 if (img->mask)
11070 {
197edd35
JR
11071 SelectObject (bmpdc, img->mask);
11072 SetTextColor (bmpdc, WHITE_PIX_DEFAULT (f));
11073 MoveToEx (bmpdc, 0, 0, NULL);
11074 LineTo (bmpdc, img->width - 1, img->height - 1);
11075 MoveToEx (bmpdc, 0, img->height - 1, NULL);
11076 LineTo (bmpdc, img->width - 1, 0);
3cf3436e 11077 }
197edd35
JR
11078 SelectObject (bmpdc, prev);
11079 DeleteDC (bmpdc);
3cf3436e 11080 }
6fc2811b
JR
11081}
11082
11083
11084/* Build a mask for image IMG which is used on frame F. FILE is the
11085 name of an image file, for error messages. HOW determines how to
11086 determine the background color of IMG. If it is a list '(R G B)',
11087 with R, G, and B being integers >= 0, take that as the color of the
11088 background. Otherwise, determine the background color of IMG
11089 heuristically. Value is non-zero if successful. */
11090
11091static int
11092x_build_heuristic_mask (f, img, how)
11093 struct frame *f;
11094 struct image *img;
11095 Lisp_Object how;
11096{
197edd35
JR
11097 HDC img_dc, frame_dc;
11098 HGDIOBJ prev;
11099 char *mask_img;
a05e2bae
JR
11100 int x, y, rc, use_img_background;
11101 unsigned long bg = 0;
197edd35 11102 int row_width;
a05e2bae
JR
11103
11104 if (img->mask)
11105 {
197edd35
JR
11106 DeleteObject (img->mask);
11107 img->mask = NULL;
a05e2bae
JR
11108 img->background_transparent_valid = 0;
11109 }
6fc2811b 11110
197edd35
JR
11111 /* Create the bit array serving as mask. */
11112 row_width = (img->width + 7) / 8;
11113 mask_img = xmalloc (row_width * img->height);
11114 bzero (mask_img, row_width * img->height);
6fc2811b 11115
197edd35
JR
11116 /* Create a memory device context for IMG->pixmap. */
11117 frame_dc = get_frame_dc (f);
11118 img_dc = CreateCompatibleDC (frame_dc);
11119 release_frame_dc (f, frame_dc);
11120 prev = SelectObject (img_dc, img->pixmap);
6fc2811b 11121
197edd35 11122 /* Determine the background color of img_dc. If HOW is `(R G B)'
a05e2bae
JR
11123 take that as color. Otherwise, use the image's background color. */
11124 use_img_background = 1;
7d0393cf 11125
6fc2811b
JR
11126 if (CONSP (how))
11127 {
a05e2bae 11128 int rgb[3], i;
6fc2811b 11129
a05e2bae 11130 for (i = 0; i < 3 && CONSP (how) && NATNUMP (XCAR (how)); ++i)
6fc2811b
JR
11131 {
11132 rgb[i] = XFASTINT (XCAR (how)) & 0xffff;
11133 how = XCDR (how);
11134 }
11135
11136 if (i == 3 && NILP (how))
11137 {
11138 char color_name[30];
6fc2811b 11139 sprintf (color_name, "#%04x%04x%04x", rgb[0], rgb[1], rgb[2]);
0040b876 11140 bg = x_alloc_image_color (f, img, build_string (color_name), 0)
8f92c555 11141 & 0x00ffffff; /* Filter out palette info. */
a05e2bae 11142 use_img_background = 0;
6fc2811b
JR
11143 }
11144 }
7d0393cf 11145
a05e2bae 11146 if (use_img_background)
197edd35 11147 bg = four_corners_best (img_dc, img->width, img->height);
6fc2811b
JR
11148
11149 /* Set all bits in mask_img to 1 whose color in ximg is different
11150 from the background color bg. */
11151 for (y = 0; y < img->height; ++y)
11152 for (x = 0; x < img->width; ++x)
197edd35
JR
11153 {
11154 COLORREF p = GetPixel (img_dc, x, y);
11155 if (p != bg)
11156 mask_img[y * row_width + x / 8] |= 1 << (x % 8);
11157 }
11158
11159 /* Create the mask image. */
11160 img->mask = w32_create_pixmap_from_bitmap_data (img->width, img->height,
11161 mask_img);
6fc2811b 11162
a05e2bae 11163 /* Fill in the background_transparent field while we have the mask handy. */
197edd35
JR
11164 SelectObject (img_dc, img->mask);
11165
11166 image_background_transparent (img, f, img_dc);
a05e2bae 11167
6fc2811b 11168 /* Put mask_img into img->mask. */
6fc2811b 11169 x_destroy_x_image (mask_img);
197edd35
JR
11170 SelectObject (img_dc, prev);
11171 DeleteDC (img_dc);
6fc2811b
JR
11172
11173 return 1;
11174}
217e5be0 11175
6fc2811b
JR
11176\f
11177/***********************************************************************
11178 PBM (mono, gray, color)
11179 ***********************************************************************/
6fc2811b
JR
11180
11181static int pbm_image_p P_ ((Lisp_Object object));
11182static int pbm_load P_ ((struct frame *f, struct image *img));
11183static int pbm_scan_number P_ ((unsigned char **, unsigned char *));
11184
11185/* The symbol `pbm' identifying images of this type. */
11186
11187Lisp_Object Qpbm;
11188
11189/* Indices of image specification fields in gs_format, below. */
11190
11191enum pbm_keyword_index
11192{
11193 PBM_TYPE,
11194 PBM_FILE,
11195 PBM_DATA,
11196 PBM_ASCENT,
11197 PBM_MARGIN,
11198 PBM_RELIEF,
11199 PBM_ALGORITHM,
11200 PBM_HEURISTIC_MASK,
a05e2bae
JR
11201 PBM_MASK,
11202 PBM_FOREGROUND,
11203 PBM_BACKGROUND,
6fc2811b
JR
11204 PBM_LAST
11205};
11206
11207/* Vector of image_keyword structures describing the format
11208 of valid user-defined image specifications. */
11209
11210static struct image_keyword pbm_format[PBM_LAST] =
11211{
11212 {":type", IMAGE_SYMBOL_VALUE, 1},
11213 {":file", IMAGE_STRING_VALUE, 0},
11214 {":data", IMAGE_STRING_VALUE, 0},
8f92c555 11215 {":ascent", IMAGE_ASCENT_VALUE, 0},
8edb0a6f 11216 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
6fc2811b 11217 {":relief", IMAGE_INTEGER_VALUE, 0},
a93f4566 11218 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
3cf3436e
JR
11219 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
11220 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
11221 {":foreground", IMAGE_STRING_OR_NIL_VALUE, 0},
11222 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
6fc2811b
JR
11223};
11224
11225/* Structure describing the image type `pbm'. */
11226
11227static struct image_type pbm_type =
11228{
11229 &Qpbm,
11230 pbm_image_p,
11231 pbm_load,
11232 x_clear_image,
11233 NULL
11234};
11235
11236
11237/* Return non-zero if OBJECT is a valid PBM image specification. */
11238
11239static int
11240pbm_image_p (object)
11241 Lisp_Object object;
11242{
11243 struct image_keyword fmt[PBM_LAST];
7d0393cf 11244
6fc2811b 11245 bcopy (pbm_format, fmt, sizeof fmt);
7d0393cf 11246
8f92c555 11247 if (!parse_image_spec (object, fmt, PBM_LAST, Qpbm))
6fc2811b
JR
11248 return 0;
11249
11250 /* Must specify either :data or :file. */
11251 return fmt[PBM_DATA].count + fmt[PBM_FILE].count == 1;
11252}
11253
11254
11255/* Scan a decimal number from *S and return it. Advance *S while
11256 reading the number. END is the end of the string. Value is -1 at
11257 end of input. */
11258
11259static int
11260pbm_scan_number (s, end)
11261 unsigned char **s, *end;
11262{
11263 int c, val = -1;
11264
11265 while (*s < end)
11266 {
11267 /* Skip white-space. */
11268 while (*s < end && (c = *(*s)++, isspace (c)))
11269 ;
11270
11271 if (c == '#')
11272 {
11273 /* Skip comment to end of line. */
11274 while (*s < end && (c = *(*s)++, c != '\n'))
11275 ;
11276 }
11277 else if (isdigit (c))
11278 {
11279 /* Read decimal number. */
11280 val = c - '0';
11281 while (*s < end && (c = *(*s)++, isdigit (c)))
11282 val = 10 * val + c - '0';
11283 break;
11284 }
11285 else
11286 break;
11287 }
11288
11289 return val;
11290}
11291
11292
11293/* Read FILE into memory. Value is a pointer to a buffer allocated
11294 with xmalloc holding FILE's contents. Value is null if an error
6f826971 11295 occurred. *SIZE is set to the size of the file. */
6fc2811b
JR
11296
11297static char *
11298pbm_read_file (file, size)
11299 Lisp_Object file;
11300 int *size;
11301{
11302 FILE *fp = NULL;
11303 char *buf = NULL;
11304 struct stat st;
11305
d5db4077
KR
11306 if (stat (SDATA (file), &st) == 0
11307 && (fp = fopen (SDATA (file), "r")) != NULL
6fc2811b
JR
11308 && (buf = (char *) xmalloc (st.st_size),
11309 fread (buf, 1, st.st_size, fp) == st.st_size))
11310 {
11311 *size = st.st_size;
11312 fclose (fp);
11313 }
11314 else
11315 {
11316 if (fp)
11317 fclose (fp);
11318 if (buf)
11319 {
11320 xfree (buf);
11321 buf = NULL;
11322 }
11323 }
7d0393cf 11324
6fc2811b
JR
11325 return buf;
11326}
11327
11328
11329/* Load PBM image IMG for use on frame F. */
11330
7d0393cf 11331static int
6fc2811b
JR
11332pbm_load (f, img)
11333 struct frame *f;
11334 struct image *img;
11335{
11336 int raw_p, x, y;
11337 int width, height, max_color_idx = 0;
11338 XImage *ximg;
11339 Lisp_Object file, specified_file;
11340 enum {PBM_MONO, PBM_GRAY, PBM_COLOR} type;
11341 struct gcpro gcpro1;
11342 unsigned char *contents = NULL;
11343 unsigned char *end, *p;
11344 int size;
11345
11346 specified_file = image_spec_value (img->spec, QCfile, NULL);
11347 file = Qnil;
11348 GCPRO1 (file);
11349
11350 if (STRINGP (specified_file))
11351 {
11352 file = x_find_image_file (specified_file);
11353 if (!STRINGP (file))
11354 {
11355 image_error ("Cannot find image file `%s'", specified_file, Qnil);
11356 UNGCPRO;
11357 return 0;
11358 }
11359
d5db4077 11360 contents = slurp_file (SDATA (file), &size);
6fc2811b
JR
11361 if (contents == NULL)
11362 {
11363 image_error ("Error reading `%s'", file, Qnil);
11364 UNGCPRO;
11365 return 0;
11366 }
11367
11368 p = contents;
11369 end = contents + size;
11370 }
11371 else
11372 {
11373 Lisp_Object data;
11374 data = image_spec_value (img->spec, QCdata, NULL);
d5db4077
KR
11375 p = SDATA (data);
11376 end = p + SBYTES (data);
6fc2811b
JR
11377 }
11378
11379 /* Check magic number. */
11380 if (end - p < 2 || *p++ != 'P')
11381 {
11382 image_error ("Not a PBM image: `%s'", img->spec, Qnil);
11383 error:
11384 xfree (contents);
11385 UNGCPRO;
11386 return 0;
11387 }
11388
6fc2811b
JR
11389 switch (*p++)
11390 {
11391 case '1':
11392 raw_p = 0, type = PBM_MONO;
11393 break;
7d0393cf 11394
6fc2811b
JR
11395 case '2':
11396 raw_p = 0, type = PBM_GRAY;
11397 break;
11398
11399 case '3':
11400 raw_p = 0, type = PBM_COLOR;
11401 break;
11402
11403 case '4':
11404 raw_p = 1, type = PBM_MONO;
11405 break;
7d0393cf 11406
6fc2811b
JR
11407 case '5':
11408 raw_p = 1, type = PBM_GRAY;
11409 break;
7d0393cf 11410
6fc2811b
JR
11411 case '6':
11412 raw_p = 1, type = PBM_COLOR;
11413 break;
11414
11415 default:
11416 image_error ("Not a PBM image: `%s'", img->spec, Qnil);
11417 goto error;
11418 }
11419
11420 /* Read width, height, maximum color-component. Characters
11421 starting with `#' up to the end of a line are ignored. */
11422 width = pbm_scan_number (&p, end);
11423 height = pbm_scan_number (&p, end);
11424
11425 if (type != PBM_MONO)
11426 {
11427 max_color_idx = pbm_scan_number (&p, end);
11428 if (raw_p && max_color_idx > 255)
11429 max_color_idx = 255;
11430 }
7d0393cf 11431
6fc2811b
JR
11432 if (width < 0
11433 || height < 0
11434 || (type != PBM_MONO && max_color_idx < 0))
11435 goto error;
11436
ac849ba4 11437 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
3cf3436e
JR
11438 goto error;
11439
ac849ba4 11440#if 0 /* TODO: color tables. */
6fc2811b
JR
11441 /* Initialize the color hash table. */
11442 init_color_table ();
ac849ba4 11443#endif
6fc2811b
JR
11444
11445 if (type == PBM_MONO)
11446 {
11447 int c = 0, g;
3cf3436e
JR
11448 struct image_keyword fmt[PBM_LAST];
11449 unsigned long fg = FRAME_FOREGROUND_PIXEL (f);
11450 unsigned long bg = FRAME_BACKGROUND_PIXEL (f);
11451
11452 /* Parse the image specification. */
11453 bcopy (pbm_format, fmt, sizeof fmt);
11454 parse_image_spec (img->spec, fmt, PBM_LAST, Qpbm);
7d0393cf 11455
3cf3436e
JR
11456 /* Get foreground and background colors, maybe allocate colors. */
11457 if (fmt[PBM_FOREGROUND].count
11458 && STRINGP (fmt[PBM_FOREGROUND].value))
11459 fg = x_alloc_image_color (f, img, fmt[PBM_FOREGROUND].value, fg);
11460 if (fmt[PBM_BACKGROUND].count
11461 && STRINGP (fmt[PBM_BACKGROUND].value))
a05e2bae
JR
11462 {
11463 bg = x_alloc_image_color (f, img, fmt[PBM_BACKGROUND].value, bg);
11464 img->background = bg;
11465 img->background_valid = 1;
11466 }
11467
6fc2811b
JR
11468 for (y = 0; y < height; ++y)
11469 for (x = 0; x < width; ++x)
11470 {
11471 if (raw_p)
11472 {
11473 if ((x & 7) == 0)
11474 c = *p++;
11475 g = c & 0x80;
11476 c <<= 1;
11477 }
11478 else
11479 g = pbm_scan_number (&p, end);
11480
3cf3436e 11481 XPutPixel (ximg, x, y, g ? fg : bg);
6fc2811b
JR
11482 }
11483 }
11484 else
11485 {
11486 for (y = 0; y < height; ++y)
11487 for (x = 0; x < width; ++x)
11488 {
11489 int r, g, b;
7d0393cf 11490
6fc2811b
JR
11491 if (type == PBM_GRAY)
11492 r = g = b = raw_p ? *p++ : pbm_scan_number (&p, end);
11493 else if (raw_p)
11494 {
11495 r = *p++;
11496 g = *p++;
11497 b = *p++;
11498 }
11499 else
11500 {
11501 r = pbm_scan_number (&p, end);
11502 g = pbm_scan_number (&p, end);
11503 b = pbm_scan_number (&p, end);
11504 }
7d0393cf 11505
6fc2811b
JR
11506 if (r < 0 || g < 0 || b < 0)
11507 {
ac849ba4 11508 x_destroy_x_image (ximg);
6fc2811b
JR
11509 image_error ("Invalid pixel value in image `%s'",
11510 img->spec, Qnil);
11511 goto error;
11512 }
7d0393cf 11513
6fc2811b 11514 /* RGB values are now in the range 0..max_color_idx.
ac849ba4
JR
11515 Scale this to the range 0..0xff supported by W32. */
11516 r = (int) ((double) r * 255 / max_color_idx);
11517 g = (int) ((double) g * 255 / max_color_idx);
11518 b = (int) ((double) b * 255 / max_color_idx);
11519 XPutPixel (ximg, x, y,
11520#if 0 /* TODO: color tables. */
11521 lookup_rgb_color (f, r, g, b));
11522#else
11523 PALETTERGB (r, g, b));
11524#endif
6fc2811b
JR
11525 }
11526 }
ac849ba4
JR
11527
11528#if 0 /* TODO: color tables. */
6fc2811b
JR
11529 /* Store in IMG->colors the colors allocated for the image, and
11530 free the color table. */
11531 img->colors = colors_in_color_table (&img->ncolors);
11532 free_color_table ();
ac849ba4 11533#endif
a05e2bae
JR
11534 /* Maybe fill in the background field while we have ximg handy. */
11535 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
11536 IMAGE_BACKGROUND (img, f, ximg);
7d0393cf 11537
6fc2811b
JR
11538 /* Put the image into a pixmap. */
11539 x_put_x_image (f, ximg, img->pixmap, width, height);
11540 x_destroy_x_image (ximg);
7d0393cf 11541
6fc2811b
JR
11542 img->width = width;
11543 img->height = height;
11544
11545 UNGCPRO;
11546 xfree (contents);
11547 return 1;
11548}
6fc2811b
JR
11549
11550\f
11551/***********************************************************************
11552 PNG
11553 ***********************************************************************/
11554
11555#if HAVE_PNG
11556
11557#include <png.h>
11558
11559/* Function prototypes. */
11560
11561static int png_image_p P_ ((Lisp_Object object));
11562static int png_load P_ ((struct frame *f, struct image *img));
11563
11564/* The symbol `png' identifying images of this type. */
11565
11566Lisp_Object Qpng;
11567
11568/* Indices of image specification fields in png_format, below. */
11569
11570enum png_keyword_index
11571{
11572 PNG_TYPE,
11573 PNG_DATA,
11574 PNG_FILE,
11575 PNG_ASCENT,
11576 PNG_MARGIN,
11577 PNG_RELIEF,
11578 PNG_ALGORITHM,
11579 PNG_HEURISTIC_MASK,
a05e2bae
JR
11580 PNG_MASK,
11581 PNG_BACKGROUND,
6fc2811b
JR
11582 PNG_LAST
11583};
11584
11585/* Vector of image_keyword structures describing the format
11586 of valid user-defined image specifications. */
11587
11588static struct image_keyword png_format[PNG_LAST] =
11589{
11590 {":type", IMAGE_SYMBOL_VALUE, 1},
11591 {":data", IMAGE_STRING_VALUE, 0},
11592 {":file", IMAGE_STRING_VALUE, 0},
8f92c555 11593 {":ascent", IMAGE_ASCENT_VALUE, 0},
8edb0a6f 11594 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
6fc2811b 11595 {":relief", IMAGE_INTEGER_VALUE, 0},
a93f4566 11596 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
a05e2bae
JR
11597 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
11598 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
11599 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
6fc2811b
JR
11600};
11601
11602/* Structure describing the image type `png'. */
11603
11604static struct image_type png_type =
11605{
11606 &Qpng,
11607 png_image_p,
11608 png_load,
11609 x_clear_image,
11610 NULL
11611};
11612
11613
11614/* Return non-zero if OBJECT is a valid PNG image specification. */
11615
11616static int
11617png_image_p (object)
11618 Lisp_Object object;
11619{
11620 struct image_keyword fmt[PNG_LAST];
11621 bcopy (png_format, fmt, sizeof fmt);
7d0393cf 11622
8f92c555 11623 if (!parse_image_spec (object, fmt, PNG_LAST, Qpng))
6fc2811b
JR
11624 return 0;
11625
11626 /* Must specify either the :data or :file keyword. */
11627 return fmt[PNG_FILE].count + fmt[PNG_DATA].count == 1;
11628}
11629
11630
11631/* Error and warning handlers installed when the PNG library
11632 is initialized. */
11633
11634static void
11635my_png_error (png_ptr, msg)
11636 png_struct *png_ptr;
11637 char *msg;
11638{
11639 xassert (png_ptr != NULL);
11640 image_error ("PNG error: %s", build_string (msg), Qnil);
11641 longjmp (png_ptr->jmpbuf, 1);
11642}
11643
11644
11645static void
11646my_png_warning (png_ptr, msg)
11647 png_struct *png_ptr;
11648 char *msg;
11649{
11650 xassert (png_ptr != NULL);
11651 image_error ("PNG warning: %s", build_string (msg), Qnil);
11652}
11653
6fc2811b
JR
11654/* Memory source for PNG decoding. */
11655
11656struct png_memory_storage
11657{
11658 unsigned char *bytes; /* The data */
11659 size_t len; /* How big is it? */
11660 int index; /* Where are we? */
11661};
11662
11663
11664/* Function set as reader function when reading PNG image from memory.
11665 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
11666 bytes from the input to DATA. */
11667
11668static void
11669png_read_from_memory (png_ptr, data, length)
11670 png_structp png_ptr;
11671 png_bytep data;
11672 png_size_t length;
11673{
11674 struct png_memory_storage *tbr
11675 = (struct png_memory_storage *) png_get_io_ptr (png_ptr);
11676
11677 if (length > tbr->len - tbr->index)
11678 png_error (png_ptr, "Read error");
7d0393cf 11679
6fc2811b
JR
11680 bcopy (tbr->bytes + tbr->index, data, length);
11681 tbr->index = tbr->index + length;
11682}
11683
6fc2811b
JR
11684/* Load PNG image IMG for use on frame F. Value is non-zero if
11685 successful. */
11686
11687static int
11688png_load (f, img)
11689 struct frame *f;
11690 struct image *img;
11691{
11692 Lisp_Object file, specified_file;
11693 Lisp_Object specified_data;
11694 int x, y, i;
11695 XImage *ximg, *mask_img = NULL;
11696 struct gcpro gcpro1;
11697 png_struct *png_ptr = NULL;
11698 png_info *info_ptr = NULL, *end_info = NULL;
a05e2bae 11699 FILE *volatile fp = NULL;
6fc2811b 11700 png_byte sig[8];
a05e2bae
JR
11701 png_byte *volatile pixels = NULL;
11702 png_byte **volatile rows = NULL;
6fc2811b
JR
11703 png_uint_32 width, height;
11704 int bit_depth, color_type, interlace_type;
11705 png_byte channels;
11706 png_uint_32 row_bytes;
11707 int transparent_p;
11708 char *gamma_str;
11709 double screen_gamma, image_gamma;
11710 int intent;
11711 struct png_memory_storage tbr; /* Data to be read */
11712
11713 /* Find out what file to load. */
11714 specified_file = image_spec_value (img->spec, QCfile, NULL);
11715 specified_data = image_spec_value (img->spec, QCdata, NULL);
11716 file = Qnil;
11717 GCPRO1 (file);
11718
11719 if (NILP (specified_data))
11720 {
11721 file = x_find_image_file (specified_file);
11722 if (!STRINGP (file))
11723 {
11724 image_error ("Cannot find image file `%s'", specified_file, Qnil);
11725 UNGCPRO;
11726 return 0;
11727 }
11728
11729 /* Open the image file. */
d5db4077 11730 fp = fopen (SDATA (file), "rb");
6fc2811b
JR
11731 if (!fp)
11732 {
11733 image_error ("Cannot open image file `%s'", file, Qnil);
11734 UNGCPRO;
11735 fclose (fp);
11736 return 0;
11737 }
11738
11739 /* Check PNG signature. */
11740 if (fread (sig, 1, sizeof sig, fp) != sizeof sig
11741 || !png_check_sig (sig, sizeof sig))
11742 {
11743 image_error ("Not a PNG file:` %s'", file, Qnil);
11744 UNGCPRO;
11745 fclose (fp);
11746 return 0;
11747 }
11748 }
11749 else
11750 {
11751 /* Read from memory. */
d5db4077
KR
11752 tbr.bytes = SDATA (specified_data);
11753 tbr.len = SBYTES (specified_data);
6fc2811b
JR
11754 tbr.index = 0;
11755
11756 /* Check PNG signature. */
11757 if (tbr.len < sizeof sig
11758 || !png_check_sig (tbr.bytes, sizeof sig))
11759 {
11760 image_error ("Not a PNG image: `%s'", img->spec, Qnil);
11761 UNGCPRO;
11762 return 0;
11763 }
11764
11765 /* Need to skip past the signature. */
11766 tbr.bytes += sizeof (sig);
11767 }
11768
6fc2811b
JR
11769 /* Initialize read and info structs for PNG lib. */
11770 png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL,
11771 my_png_error, my_png_warning);
11772 if (!png_ptr)
11773 {
11774 if (fp) fclose (fp);
11775 UNGCPRO;
11776 return 0;
11777 }
11778
11779 info_ptr = png_create_info_struct (png_ptr);
11780 if (!info_ptr)
11781 {
11782 png_destroy_read_struct (&png_ptr, NULL, NULL);
11783 if (fp) fclose (fp);
11784 UNGCPRO;
11785 return 0;
11786 }
11787
11788 end_info = png_create_info_struct (png_ptr);
11789 if (!end_info)
11790 {
11791 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
11792 if (fp) fclose (fp);
11793 UNGCPRO;
11794 return 0;
11795 }
11796
11797 /* Set error jump-back. We come back here when the PNG library
11798 detects an error. */
11799 if (setjmp (png_ptr->jmpbuf))
11800 {
11801 error:
11802 if (png_ptr)
11803 png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
11804 xfree (pixels);
11805 xfree (rows);
11806 if (fp) fclose (fp);
11807 UNGCPRO;
11808 return 0;
11809 }
11810
11811 /* Read image info. */
11812 if (!NILP (specified_data))
11813 png_set_read_fn (png_ptr, (void *) &tbr, png_read_from_memory);
11814 else
11815 png_init_io (png_ptr, fp);
11816
11817 png_set_sig_bytes (png_ptr, sizeof sig);
11818 png_read_info (png_ptr, info_ptr);
11819 png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
11820 &interlace_type, NULL, NULL);
11821
7d0393cf 11822 /* If image contains simply transparency data, we prefer to
6fc2811b
JR
11823 construct a clipping mask. */
11824 if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
11825 transparent_p = 1;
11826 else
11827 transparent_p = 0;
11828
7d0393cf 11829 /* This function is easier to write if we only have to handle
6fc2811b
JR
11830 one data format: RGB or RGBA with 8 bits per channel. Let's
11831 transform other formats into that format. */
11832
11833 /* Strip more than 8 bits per channel. */
11834 if (bit_depth == 16)
11835 png_set_strip_16 (png_ptr);
11836
11837 /* Expand data to 24 bit RGB, or 8 bit grayscale, with alpha channel
11838 if available. */
11839 png_set_expand (png_ptr);
11840
11841 /* Convert grayscale images to RGB. */
7d0393cf 11842 if (color_type == PNG_COLOR_TYPE_GRAY
6fc2811b
JR
11843 || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
11844 png_set_gray_to_rgb (png_ptr);
11845
11846 /* The value 2.2 is a guess for PC monitors from PNG example.c. */
11847 gamma_str = getenv ("SCREEN_GAMMA");
11848 screen_gamma = gamma_str ? atof (gamma_str) : 2.2;
11849
11850 /* Tell the PNG lib to handle gamma correction for us. */
11851
11852#if defined(PNG_READ_sRGB_SUPPORTED) || defined(PNG_WRITE_sRGB_SUPPORTED)
11853 if (png_get_sRGB (png_ptr, info_ptr, &intent))
11854 /* There is a special chunk in the image specifying the gamma. */
11855 png_set_sRGB (png_ptr, info_ptr, intent);
11856 else
11857#endif
11858 if (png_get_gAMA (png_ptr, info_ptr, &image_gamma))
11859 /* Image contains gamma information. */
11860 png_set_gamma (png_ptr, screen_gamma, image_gamma);
11861 else
11862 /* Use a default of 0.5 for the image gamma. */
11863 png_set_gamma (png_ptr, screen_gamma, 0.5);
11864
11865 /* Handle alpha channel by combining the image with a background
11866 color. Do this only if a real alpha channel is supplied. For
11867 simple transparency, we prefer a clipping mask. */
11868 if (!transparent_p)
11869 {
11870 png_color_16 *image_background;
a05e2bae
JR
11871 Lisp_Object specified_bg
11872 = image_spec_value (img->spec, QCbackground, NULL);
11873
11874
11875 if (STRINGP (specified_bg))
11876 /* The user specified `:background', use that. */
11877 {
11878 COLORREF color;
d5db4077 11879 if (w32_defined_color (f, SDATA (specified_bg), &color, 0))
a05e2bae
JR
11880 {
11881 png_color_16 user_bg;
11882
11883 bzero (&user_bg, sizeof user_bg);
11884 user_bg.red = color.red;
11885 user_bg.green = color.green;
11886 user_bg.blue = color.blue;
6fc2811b 11887
a05e2bae
JR
11888 png_set_background (png_ptr, &user_bg,
11889 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
11890 }
11891 }
11892 else if (png_get_bKGD (png_ptr, info_ptr, &image_background))
7d0393cf 11893 /* Image contains a background color with which to
6fc2811b
JR
11894 combine the image. */
11895 png_set_background (png_ptr, image_background,
11896 PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
11897 else
11898 {
11899 /* Image does not contain a background color with which
7d0393cf 11900 to combine the image data via an alpha channel. Use
6fc2811b
JR
11901 the frame's background instead. */
11902 XColor color;
11903 Colormap cmap;
11904 png_color_16 frame_background;
11905
a05e2bae 11906 cmap = FRAME_X_COLORMAP (f);
6fc2811b 11907 color.pixel = FRAME_BACKGROUND_PIXEL (f);
a05e2bae 11908 x_query_color (f, &color);
6fc2811b
JR
11909
11910 bzero (&frame_background, sizeof frame_background);
11911 frame_background.red = color.red;
11912 frame_background.green = color.green;
11913 frame_background.blue = color.blue;
11914
11915 png_set_background (png_ptr, &frame_background,
11916 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
11917 }
11918 }
11919
11920 /* Update info structure. */
11921 png_read_update_info (png_ptr, info_ptr);
11922
11923 /* Get number of channels. Valid values are 1 for grayscale images
11924 and images with a palette, 2 for grayscale images with transparency
11925 information (alpha channel), 3 for RGB images, and 4 for RGB
11926 images with alpha channel, i.e. RGBA. If conversions above were
11927 sufficient we should only have 3 or 4 channels here. */
11928 channels = png_get_channels (png_ptr, info_ptr);
11929 xassert (channels == 3 || channels == 4);
11930
11931 /* Number of bytes needed for one row of the image. */
11932 row_bytes = png_get_rowbytes (png_ptr, info_ptr);
11933
11934 /* Allocate memory for the image. */
11935 pixels = (png_byte *) xmalloc (row_bytes * height * sizeof *pixels);
11936 rows = (png_byte **) xmalloc (height * sizeof *rows);
11937 for (i = 0; i < height; ++i)
11938 rows[i] = pixels + i * row_bytes;
11939
11940 /* Read the entire image. */
11941 png_read_image (png_ptr, rows);
11942 png_read_end (png_ptr, info_ptr);
11943 if (fp)
11944 {
11945 fclose (fp);
11946 fp = NULL;
11947 }
11948
6fc2811b
JR
11949 /* Create the X image and pixmap. */
11950 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg,
11951 &img->pixmap))
a05e2bae 11952 goto error;
7d0393cf 11953
6fc2811b
JR
11954 /* Create an image and pixmap serving as mask if the PNG image
11955 contains an alpha channel. */
11956 if (channels == 4
11957 && !transparent_p
11958 && !x_create_x_image_and_pixmap (f, width, height, 1,
11959 &mask_img, &img->mask))
11960 {
11961 x_destroy_x_image (ximg);
11962 XFreePixmap (FRAME_W32_DISPLAY (f), img->pixmap);
11963 img->pixmap = 0;
6fc2811b
JR
11964 goto error;
11965 }
11966
11967 /* Fill the X image and mask from PNG data. */
11968 init_color_table ();
11969
11970 for (y = 0; y < height; ++y)
11971 {
11972 png_byte *p = rows[y];
11973
11974 for (x = 0; x < width; ++x)
11975 {
11976 unsigned r, g, b;
11977
11978 r = *p++ << 8;
11979 g = *p++ << 8;
11980 b = *p++ << 8;
11981 XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b));
11982
11983 /* An alpha channel, aka mask channel, associates variable
7d0393cf
JB
11984 transparency with an image. Where other image formats
11985 support binary transparency---fully transparent or fully
6fc2811b
JR
11986 opaque---PNG allows up to 254 levels of partial transparency.
11987 The PNG library implements partial transparency by combining
11988 the image with a specified background color.
11989
11990 I'm not sure how to handle this here nicely: because the
11991 background on which the image is displayed may change, for
7d0393cf
JB
11992 real alpha channel support, it would be necessary to create
11993 a new image for each possible background.
6fc2811b
JR
11994
11995 What I'm doing now is that a mask is created if we have
11996 boolean transparency information. Otherwise I'm using
11997 the frame's background color to combine the image with. */
11998
11999 if (channels == 4)
12000 {
12001 if (mask_img)
12002 XPutPixel (mask_img, x, y, *p > 0);
12003 ++p;
12004 }
12005 }
12006 }
12007
a05e2bae
JR
12008 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
12009 /* Set IMG's background color from the PNG image, unless the user
12010 overrode it. */
12011 {
12012 png_color_16 *bg;
12013 if (png_get_bKGD (png_ptr, info_ptr, &bg))
12014 {
12015 img->background = lookup_rgb_color (f, bg->red, bg->green, bg->blue);
12016 img->background_valid = 1;
12017 }
12018 }
12019
6fc2811b
JR
12020 /* Remember colors allocated for this image. */
12021 img->colors = colors_in_color_table (&img->ncolors);
12022 free_color_table ();
12023
12024 /* Clean up. */
12025 png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
12026 xfree (rows);
12027 xfree (pixels);
12028
12029 img->width = width;
12030 img->height = height;
12031
a05e2bae
JR
12032 /* Maybe fill in the background field while we have ximg handy. */
12033 IMAGE_BACKGROUND (img, f, ximg);
12034
6fc2811b
JR
12035 /* Put the image into the pixmap, then free the X image and its buffer. */
12036 x_put_x_image (f, ximg, img->pixmap, width, height);
12037 x_destroy_x_image (ximg);
12038
12039 /* Same for the mask. */
12040 if (mask_img)
12041 {
a05e2bae
JR
12042 /* Fill in the background_transparent field while we have the mask
12043 handy. */
12044 image_background_transparent (img, f, mask_img);
12045
6fc2811b
JR
12046 x_put_x_image (f, mask_img, img->mask, img->width, img->height);
12047 x_destroy_x_image (mask_img);
12048 }
12049
6fc2811b
JR
12050 UNGCPRO;
12051 return 1;
12052}
12053
12054#endif /* HAVE_PNG != 0 */
12055
12056
12057\f
12058/***********************************************************************
12059 JPEG
12060 ***********************************************************************/
12061
12062#if HAVE_JPEG
12063
12064/* Work around a warning about HAVE_STDLIB_H being redefined in
12065 jconfig.h. */
12066#ifdef HAVE_STDLIB_H
12067#define HAVE_STDLIB_H_1
12068#undef HAVE_STDLIB_H
12069#endif /* HAVE_STLIB_H */
12070
12071#include <jpeglib.h>
12072#include <jerror.h>
12073#include <setjmp.h>
12074
12075#ifdef HAVE_STLIB_H_1
12076#define HAVE_STDLIB_H 1
12077#endif
12078
12079static int jpeg_image_p P_ ((Lisp_Object object));
12080static int jpeg_load P_ ((struct frame *f, struct image *img));
12081
12082/* The symbol `jpeg' identifying images of this type. */
12083
12084Lisp_Object Qjpeg;
12085
12086/* Indices of image specification fields in gs_format, below. */
12087
12088enum jpeg_keyword_index
12089{
12090 JPEG_TYPE,
12091 JPEG_DATA,
12092 JPEG_FILE,
12093 JPEG_ASCENT,
12094 JPEG_MARGIN,
12095 JPEG_RELIEF,
12096 JPEG_ALGORITHM,
12097 JPEG_HEURISTIC_MASK,
a05e2bae
JR
12098 JPEG_MASK,
12099 JPEG_BACKGROUND,
6fc2811b
JR
12100 JPEG_LAST
12101};
12102
12103/* Vector of image_keyword structures describing the format
12104 of valid user-defined image specifications. */
12105
12106static struct image_keyword jpeg_format[JPEG_LAST] =
12107{
12108 {":type", IMAGE_SYMBOL_VALUE, 1},
12109 {":data", IMAGE_STRING_VALUE, 0},
12110 {":file", IMAGE_STRING_VALUE, 0},
8f92c555 12111 {":ascent", IMAGE_ASCENT_VALUE, 0},
8edb0a6f 12112 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
6fc2811b 12113 {":relief", IMAGE_INTEGER_VALUE, 0},
a05e2bae
JR
12114 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
12115 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
12116 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
12117 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
6fc2811b
JR
12118};
12119
12120/* Structure describing the image type `jpeg'. */
12121
12122static struct image_type jpeg_type =
12123{
12124 &Qjpeg,
12125 jpeg_image_p,
12126 jpeg_load,
12127 x_clear_image,
12128 NULL
12129};
12130
12131
12132/* Return non-zero if OBJECT is a valid JPEG image specification. */
12133
12134static int
12135jpeg_image_p (object)
12136 Lisp_Object object;
12137{
12138 struct image_keyword fmt[JPEG_LAST];
7d0393cf 12139
6fc2811b 12140 bcopy (jpeg_format, fmt, sizeof fmt);
7d0393cf 12141
8f92c555 12142 if (!parse_image_spec (object, fmt, JPEG_LAST, Qjpeg))
6fc2811b
JR
12143 return 0;
12144
12145 /* Must specify either the :data or :file keyword. */
12146 return fmt[JPEG_FILE].count + fmt[JPEG_DATA].count == 1;
12147}
12148
12149
12150struct my_jpeg_error_mgr
12151{
12152 struct jpeg_error_mgr pub;
12153 jmp_buf setjmp_buffer;
12154};
12155
12156static void
12157my_error_exit (cinfo)
12158 j_common_ptr cinfo;
12159{
12160 struct my_jpeg_error_mgr *mgr = (struct my_jpeg_error_mgr *) cinfo->err;
12161 longjmp (mgr->setjmp_buffer, 1);
12162}
12163
6fc2811b
JR
12164/* Init source method for JPEG data source manager. Called by
12165 jpeg_read_header() before any data is actually read. See
12166 libjpeg.doc from the JPEG lib distribution. */
12167
12168static void
12169our_init_source (cinfo)
12170 j_decompress_ptr cinfo;
12171{
12172}
12173
12174
12175/* Fill input buffer method for JPEG data source manager. Called
12176 whenever more data is needed. We read the whole image in one step,
12177 so this only adds a fake end of input marker at the end. */
12178
12179static boolean
12180our_fill_input_buffer (cinfo)
12181 j_decompress_ptr cinfo;
12182{
12183 /* Insert a fake EOI marker. */
12184 struct jpeg_source_mgr *src = cinfo->src;
12185 static JOCTET buffer[2];
12186
12187 buffer[0] = (JOCTET) 0xFF;
12188 buffer[1] = (JOCTET) JPEG_EOI;
12189
12190 src->next_input_byte = buffer;
12191 src->bytes_in_buffer = 2;
12192 return TRUE;
12193}
12194
12195
12196/* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
12197 is the JPEG data source manager. */
12198
12199static void
12200our_skip_input_data (cinfo, num_bytes)
12201 j_decompress_ptr cinfo;
12202 long num_bytes;
12203{
12204 struct jpeg_source_mgr *src = (struct jpeg_source_mgr *) cinfo->src;
12205
12206 if (src)
12207 {
12208 if (num_bytes > src->bytes_in_buffer)
12209 ERREXIT (cinfo, JERR_INPUT_EOF);
7d0393cf 12210
6fc2811b
JR
12211 src->bytes_in_buffer -= num_bytes;
12212 src->next_input_byte += num_bytes;
12213 }
12214}
12215
12216
12217/* Method to terminate data source. Called by
12218 jpeg_finish_decompress() after all data has been processed. */
12219
12220static void
12221our_term_source (cinfo)
12222 j_decompress_ptr cinfo;
12223{
12224}
12225
12226
12227/* Set up the JPEG lib for reading an image from DATA which contains
12228 LEN bytes. CINFO is the decompression info structure created for
12229 reading the image. */
12230
12231static void
12232jpeg_memory_src (cinfo, data, len)
12233 j_decompress_ptr cinfo;
12234 JOCTET *data;
12235 unsigned int len;
12236{
12237 struct jpeg_source_mgr *src;
12238
12239 if (cinfo->src == NULL)
12240 {
12241 /* First time for this JPEG object? */
12242 cinfo->src = (struct jpeg_source_mgr *)
12243 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
12244 sizeof (struct jpeg_source_mgr));
12245 src = (struct jpeg_source_mgr *) cinfo->src;
12246 src->next_input_byte = data;
12247 }
7d0393cf 12248
6fc2811b
JR
12249 src = (struct jpeg_source_mgr *) cinfo->src;
12250 src->init_source = our_init_source;
12251 src->fill_input_buffer = our_fill_input_buffer;
12252 src->skip_input_data = our_skip_input_data;
12253 src->resync_to_restart = jpeg_resync_to_restart; /* Use default method. */
12254 src->term_source = our_term_source;
12255 src->bytes_in_buffer = len;
12256 src->next_input_byte = data;
12257}
12258
12259
12260/* Load image IMG for use on frame F. Patterned after example.c
12261 from the JPEG lib. */
12262
7d0393cf 12263static int
6fc2811b
JR
12264jpeg_load (f, img)
12265 struct frame *f;
12266 struct image *img;
12267{
12268 struct jpeg_decompress_struct cinfo;
12269 struct my_jpeg_error_mgr mgr;
12270 Lisp_Object file, specified_file;
12271 Lisp_Object specified_data;
a05e2bae 12272 FILE * volatile fp = NULL;
6fc2811b
JR
12273 JSAMPARRAY buffer;
12274 int row_stride, x, y;
12275 XImage *ximg = NULL;
12276 int rc;
12277 unsigned long *colors;
12278 int width, height;
12279 struct gcpro gcpro1;
12280
12281 /* Open the JPEG file. */
12282 specified_file = image_spec_value (img->spec, QCfile, NULL);
12283 specified_data = image_spec_value (img->spec, QCdata, NULL);
12284 file = Qnil;
12285 GCPRO1 (file);
12286
6fc2811b
JR
12287 if (NILP (specified_data))
12288 {
12289 file = x_find_image_file (specified_file);
12290 if (!STRINGP (file))
12291 {
12292 image_error ("Cannot find image file `%s'", specified_file, Qnil);
12293 UNGCPRO;
12294 return 0;
12295 }
7d0393cf 12296
d5db4077 12297 fp = fopen (SDATA (file), "r");
6fc2811b
JR
12298 if (fp == NULL)
12299 {
12300 image_error ("Cannot open `%s'", file, Qnil);
12301 UNGCPRO;
12302 return 0;
12303 }
12304 }
7d0393cf 12305
6fc2811b
JR
12306 /* Customize libjpeg's error handling to call my_error_exit when an
12307 error is detected. This function will perform a longjmp. */
6fc2811b 12308 cinfo.err = jpeg_std_error (&mgr.pub);
a05e2bae 12309 mgr.pub.error_exit = my_error_exit;
7d0393cf 12310
6fc2811b
JR
12311 if ((rc = setjmp (mgr.setjmp_buffer)) != 0)
12312 {
12313 if (rc == 1)
12314 {
12315 /* Called from my_error_exit. Display a JPEG error. */
12316 char buffer[JMSG_LENGTH_MAX];
12317 cinfo.err->format_message ((j_common_ptr) &cinfo, buffer);
12318 image_error ("Error reading JPEG image `%s': %s", img->spec,
12319 build_string (buffer));
12320 }
7d0393cf 12321
6fc2811b
JR
12322 /* Close the input file and destroy the JPEG object. */
12323 if (fp)
12324 fclose (fp);
12325 jpeg_destroy_decompress (&cinfo);
7d0393cf 12326
6fc2811b
JR
12327 /* If we already have an XImage, free that. */
12328 x_destroy_x_image (ximg);
12329
12330 /* Free pixmap and colors. */
12331 x_clear_image (f, img);
7d0393cf 12332
6fc2811b
JR
12333 UNGCPRO;
12334 return 0;
12335 }
12336
12337 /* Create the JPEG decompression object. Let it read from fp.
12338 Read the JPEG image header. */
12339 jpeg_create_decompress (&cinfo);
12340
12341 if (NILP (specified_data))
12342 jpeg_stdio_src (&cinfo, fp);
12343 else
d5db4077
KR
12344 jpeg_memory_src (&cinfo, SDATA (specified_data),
12345 SBYTES (specified_data));
6fc2811b
JR
12346
12347 jpeg_read_header (&cinfo, TRUE);
12348
12349 /* Customize decompression so that color quantization will be used.
12350 Start decompression. */
12351 cinfo.quantize_colors = TRUE;
12352 jpeg_start_decompress (&cinfo);
12353 width = img->width = cinfo.output_width;
12354 height = img->height = cinfo.output_height;
12355
6fc2811b
JR
12356 /* Create X image and pixmap. */
12357 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg,
12358 &img->pixmap))
a05e2bae 12359 longjmp (mgr.setjmp_buffer, 2);
6fc2811b
JR
12360
12361 /* Allocate colors. When color quantization is used,
12362 cinfo.actual_number_of_colors has been set with the number of
12363 colors generated, and cinfo.colormap is a two-dimensional array
12364 of color indices in the range 0..cinfo.actual_number_of_colors.
12365 No more than 255 colors will be generated. */
12366 {
12367 int i, ir, ig, ib;
12368
12369 if (cinfo.out_color_components > 2)
12370 ir = 0, ig = 1, ib = 2;
12371 else if (cinfo.out_color_components > 1)
12372 ir = 0, ig = 1, ib = 0;
12373 else
12374 ir = 0, ig = 0, ib = 0;
12375
12376 /* Use the color table mechanism because it handles colors that
12377 cannot be allocated nicely. Such colors will be replaced with
12378 a default color, and we don't have to care about which colors
12379 can be freed safely, and which can't. */
12380 init_color_table ();
12381 colors = (unsigned long *) alloca (cinfo.actual_number_of_colors
12382 * sizeof *colors);
7d0393cf 12383
6fc2811b
JR
12384 for (i = 0; i < cinfo.actual_number_of_colors; ++i)
12385 {
12386 /* Multiply RGB values with 255 because X expects RGB values
12387 in the range 0..0xffff. */
12388 int r = cinfo.colormap[ir][i] << 8;
12389 int g = cinfo.colormap[ig][i] << 8;
12390 int b = cinfo.colormap[ib][i] << 8;
12391 colors[i] = lookup_rgb_color (f, r, g, b);
12392 }
12393
12394 /* Remember those colors actually allocated. */
12395 img->colors = colors_in_color_table (&img->ncolors);
12396 free_color_table ();
12397 }
12398
12399 /* Read pixels. */
12400 row_stride = width * cinfo.output_components;
12401 buffer = cinfo.mem->alloc_sarray ((j_common_ptr) &cinfo, JPOOL_IMAGE,
12402 row_stride, 1);
12403 for (y = 0; y < height; ++y)
12404 {
12405 jpeg_read_scanlines (&cinfo, buffer, 1);
12406 for (x = 0; x < cinfo.output_width; ++x)
12407 XPutPixel (ximg, x, y, colors[buffer[0][x]]);
12408 }
12409
12410 /* Clean up. */
12411 jpeg_finish_decompress (&cinfo);
12412 jpeg_destroy_decompress (&cinfo);
12413 if (fp)
12414 fclose (fp);
7d0393cf 12415
a05e2bae
JR
12416 /* Maybe fill in the background field while we have ximg handy. */
12417 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
12418 IMAGE_BACKGROUND (img, f, ximg);
7d0393cf 12419
6fc2811b
JR
12420 /* Put the image into the pixmap. */
12421 x_put_x_image (f, ximg, img->pixmap, width, height);
12422 x_destroy_x_image (ximg);
12423 UNBLOCK_INPUT;
12424 UNGCPRO;
12425 return 1;
12426}
12427
12428#endif /* HAVE_JPEG */
12429
12430
12431\f
12432/***********************************************************************
12433 TIFF
12434 ***********************************************************************/
12435
12436#if HAVE_TIFF
12437
12438#include <tiffio.h>
12439
12440static int tiff_image_p P_ ((Lisp_Object object));
12441static int tiff_load P_ ((struct frame *f, struct image *img));
12442
12443/* The symbol `tiff' identifying images of this type. */
12444
12445Lisp_Object Qtiff;
12446
12447/* Indices of image specification fields in tiff_format, below. */
12448
12449enum tiff_keyword_index
12450{
12451 TIFF_TYPE,
12452 TIFF_DATA,
12453 TIFF_FILE,
12454 TIFF_ASCENT,
12455 TIFF_MARGIN,
12456 TIFF_RELIEF,
12457 TIFF_ALGORITHM,
12458 TIFF_HEURISTIC_MASK,
a05e2bae
JR
12459 TIFF_MASK,
12460 TIFF_BACKGROUND,
6fc2811b
JR
12461 TIFF_LAST
12462};
12463
12464/* Vector of image_keyword structures describing the format
12465 of valid user-defined image specifications. */
12466
12467static struct image_keyword tiff_format[TIFF_LAST] =
12468{
12469 {":type", IMAGE_SYMBOL_VALUE, 1},
12470 {":data", IMAGE_STRING_VALUE, 0},
12471 {":file", IMAGE_STRING_VALUE, 0},
8f92c555 12472 {":ascent", IMAGE_ASCENT_VALUE, 0},
8edb0a6f 12473 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
6fc2811b 12474 {":relief", IMAGE_INTEGER_VALUE, 0},
a05e2bae
JR
12475 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
12476 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
12477 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
12478 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
6fc2811b
JR
12479};
12480
12481/* Structure describing the image type `tiff'. */
12482
12483static struct image_type tiff_type =
12484{
12485 &Qtiff,
12486 tiff_image_p,
12487 tiff_load,
12488 x_clear_image,
12489 NULL
12490};
12491
12492
12493/* Return non-zero if OBJECT is a valid TIFF image specification. */
12494
12495static int
12496tiff_image_p (object)
12497 Lisp_Object object;
12498{
12499 struct image_keyword fmt[TIFF_LAST];
12500 bcopy (tiff_format, fmt, sizeof fmt);
7d0393cf 12501
8f92c555 12502 if (!parse_image_spec (object, fmt, TIFF_LAST, Qtiff))
6fc2811b 12503 return 0;
7d0393cf 12504
6fc2811b
JR
12505 /* Must specify either the :data or :file keyword. */
12506 return fmt[TIFF_FILE].count + fmt[TIFF_DATA].count == 1;
12507}
12508
12509
12510/* Reading from a memory buffer for TIFF images Based on the PNG
12511 memory source, but we have to provide a lot of extra functions.
12512 Blah.
12513
12514 We really only need to implement read and seek, but I am not
12515 convinced that the TIFF library is smart enough not to destroy
12516 itself if we only hand it the function pointers we need to
12517 override. */
12518
12519typedef struct
12520{
12521 unsigned char *bytes;
12522 size_t len;
12523 int index;
12524}
12525tiff_memory_source;
12526
12527static size_t
12528tiff_read_from_memory (data, buf, size)
12529 thandle_t data;
12530 tdata_t buf;
12531 tsize_t size;
12532{
12533 tiff_memory_source *src = (tiff_memory_source *) data;
12534
12535 if (size > src->len - src->index)
12536 return (size_t) -1;
12537 bcopy (src->bytes + src->index, buf, size);
12538 src->index += size;
12539 return size;
12540}
12541
12542static size_t
12543tiff_write_from_memory (data, buf, size)
12544 thandle_t data;
12545 tdata_t buf;
12546 tsize_t size;
12547{
12548 return (size_t) -1;
12549}
12550
12551static toff_t
12552tiff_seek_in_memory (data, off, whence)
12553 thandle_t data;
12554 toff_t off;
12555 int whence;
12556{
12557 tiff_memory_source *src = (tiff_memory_source *) data;
12558 int idx;
12559
12560 switch (whence)
12561 {
12562 case SEEK_SET: /* Go from beginning of source. */
12563 idx = off;
12564 break;
7d0393cf 12565
6fc2811b
JR
12566 case SEEK_END: /* Go from end of source. */
12567 idx = src->len + off;
12568 break;
7d0393cf 12569
6fc2811b
JR
12570 case SEEK_CUR: /* Go from current position. */
12571 idx = src->index + off;
12572 break;
7d0393cf 12573
6fc2811b
JR
12574 default: /* Invalid `whence'. */
12575 return -1;
12576 }
7d0393cf 12577
6fc2811b
JR
12578 if (idx > src->len || idx < 0)
12579 return -1;
7d0393cf 12580
6fc2811b
JR
12581 src->index = idx;
12582 return src->index;
12583}
12584
12585static int
12586tiff_close_memory (data)
12587 thandle_t data;
12588{
12589 /* NOOP */
12590 return 0;
12591}
12592
12593static int
12594tiff_mmap_memory (data, pbase, psize)
12595 thandle_t data;
12596 tdata_t *pbase;
12597 toff_t *psize;
12598{
12599 /* It is already _IN_ memory. */
12600 return 0;
12601}
12602
12603static void
12604tiff_unmap_memory (data, base, size)
12605 thandle_t data;
12606 tdata_t base;
12607 toff_t size;
12608{
12609 /* We don't need to do this. */
12610}
12611
12612static toff_t
12613tiff_size_of_memory (data)
12614 thandle_t data;
12615{
12616 return ((tiff_memory_source *) data)->len;
12617}
12618
3cf3436e
JR
12619
12620static void
12621tiff_error_handler (title, format, ap)
12622 const char *title, *format;
12623 va_list ap;
12624{
12625 char buf[512];
12626 int len;
7d0393cf 12627
3cf3436e
JR
12628 len = sprintf (buf, "TIFF error: %s ", title);
12629 vsprintf (buf + len, format, ap);
12630 add_to_log (buf, Qnil, Qnil);
12631}
12632
12633
12634static void
12635tiff_warning_handler (title, format, ap)
12636 const char *title, *format;
12637 va_list ap;
12638{
12639 char buf[512];
12640 int len;
7d0393cf 12641
3cf3436e
JR
12642 len = sprintf (buf, "TIFF warning: %s ", title);
12643 vsprintf (buf + len, format, ap);
12644 add_to_log (buf, Qnil, Qnil);
12645}
12646
12647
6fc2811b
JR
12648/* Load TIFF image IMG for use on frame F. Value is non-zero if
12649 successful. */
12650
12651static int
12652tiff_load (f, img)
12653 struct frame *f;
12654 struct image *img;
12655{
12656 Lisp_Object file, specified_file;
12657 Lisp_Object specified_data;
12658 TIFF *tiff;
12659 int width, height, x, y;
12660 uint32 *buf;
12661 int rc;
12662 XImage *ximg;
12663 struct gcpro gcpro1;
12664 tiff_memory_source memsrc;
12665
12666 specified_file = image_spec_value (img->spec, QCfile, NULL);
12667 specified_data = image_spec_value (img->spec, QCdata, NULL);
12668 file = Qnil;
12669 GCPRO1 (file);
12670
3cf3436e
JR
12671 TIFFSetErrorHandler (tiff_error_handler);
12672 TIFFSetWarningHandler (tiff_warning_handler);
12673
6fc2811b
JR
12674 if (NILP (specified_data))
12675 {
12676 /* Read from a file */
12677 file = x_find_image_file (specified_file);
12678 if (!STRINGP (file))
3cf3436e
JR
12679 {
12680 image_error ("Cannot find image file `%s'", file, Qnil);
12681 UNGCPRO;
12682 return 0;
12683 }
7d0393cf 12684
6fc2811b 12685 /* Try to open the image file. */
d5db4077 12686 tiff = TIFFOpen (SDATA (file), "r");
6fc2811b 12687 if (tiff == NULL)
3cf3436e
JR
12688 {
12689 image_error ("Cannot open `%s'", file, Qnil);
12690 UNGCPRO;
12691 return 0;
12692 }
6fc2811b
JR
12693 }
12694 else
12695 {
12696 /* Memory source! */
d5db4077
KR
12697 memsrc.bytes = SDATA (specified_data);
12698 memsrc.len = SBYTES (specified_data);
6fc2811b
JR
12699 memsrc.index = 0;
12700
12701 tiff = TIFFClientOpen ("memory_source", "r", &memsrc,
12702 (TIFFReadWriteProc) tiff_read_from_memory,
12703 (TIFFReadWriteProc) tiff_write_from_memory,
12704 tiff_seek_in_memory,
12705 tiff_close_memory,
12706 tiff_size_of_memory,
12707 tiff_mmap_memory,
12708 tiff_unmap_memory);
12709
12710 if (!tiff)
12711 {
12712 image_error ("Cannot open memory source for `%s'", img->spec, Qnil);
12713 UNGCPRO;
12714 return 0;
12715 }
12716 }
12717
12718 /* Get width and height of the image, and allocate a raster buffer
12719 of width x height 32-bit values. */
12720 TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &width);
12721 TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &height);
12722 buf = (uint32 *) xmalloc (width * height * sizeof *buf);
7d0393cf 12723
6fc2811b
JR
12724 rc = TIFFReadRGBAImage (tiff, width, height, buf, 0);
12725 TIFFClose (tiff);
12726 if (!rc)
12727 {
12728 image_error ("Error reading TIFF image `%s'", img->spec, Qnil);
12729 xfree (buf);
12730 UNGCPRO;
12731 return 0;
12732 }
12733
6fc2811b
JR
12734 /* Create the X image and pixmap. */
12735 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
12736 {
6fc2811b
JR
12737 xfree (buf);
12738 UNGCPRO;
12739 return 0;
12740 }
12741
12742 /* Initialize the color table. */
12743 init_color_table ();
12744
12745 /* Process the pixel raster. Origin is in the lower-left corner. */
12746 for (y = 0; y < height; ++y)
12747 {
12748 uint32 *row = buf + y * width;
7d0393cf 12749
6fc2811b
JR
12750 for (x = 0; x < width; ++x)
12751 {
12752 uint32 abgr = row[x];
12753 int r = TIFFGetR (abgr) << 8;
12754 int g = TIFFGetG (abgr) << 8;
12755 int b = TIFFGetB (abgr) << 8;
7d0393cf 12756 XPutPixel (ximg, x, height - 1 - y, lookup_rgb_color (f, r, g, b));
6fc2811b
JR
12757 }
12758 }
12759
12760 /* Remember the colors allocated for the image. Free the color table. */
12761 img->colors = colors_in_color_table (&img->ncolors);
12762 free_color_table ();
12763
a05e2bae
JR
12764 img->width = width;
12765 img->height = height;
12766
12767 /* Maybe fill in the background field while we have ximg handy. */
12768 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
12769 IMAGE_BACKGROUND (img, f, ximg);
12770
6fc2811b
JR
12771 /* Put the image into the pixmap, then free the X image and its buffer. */
12772 x_put_x_image (f, ximg, img->pixmap, width, height);
12773 x_destroy_x_image (ximg);
12774 xfree (buf);
6fc2811b
JR
12775
12776 UNGCPRO;
12777 return 1;
12778}
12779
12780#endif /* HAVE_TIFF != 0 */
12781
12782
12783\f
12784/***********************************************************************
12785 GIF
12786 ***********************************************************************/
12787
12788#if HAVE_GIF
12789
12790#include <gif_lib.h>
12791
12792static int gif_image_p P_ ((Lisp_Object object));
12793static int gif_load P_ ((struct frame *f, struct image *img));
12794
12795/* The symbol `gif' identifying images of this type. */
12796
12797Lisp_Object Qgif;
12798
12799/* Indices of image specification fields in gif_format, below. */
12800
12801enum gif_keyword_index
12802{
12803 GIF_TYPE,
12804 GIF_DATA,
12805 GIF_FILE,
12806 GIF_ASCENT,
12807 GIF_MARGIN,
12808 GIF_RELIEF,
12809 GIF_ALGORITHM,
12810 GIF_HEURISTIC_MASK,
a05e2bae 12811 GIF_MASK,
6fc2811b 12812 GIF_IMAGE,
a05e2bae 12813 GIF_BACKGROUND,
6fc2811b
JR
12814 GIF_LAST
12815};
12816
12817/* Vector of image_keyword structures describing the format
12818 of valid user-defined image specifications. */
12819
12820static struct image_keyword gif_format[GIF_LAST] =
12821{
12822 {":type", IMAGE_SYMBOL_VALUE, 1},
12823 {":data", IMAGE_STRING_VALUE, 0},
12824 {":file", IMAGE_STRING_VALUE, 0},
8f92c555 12825 {":ascent", IMAGE_ASCENT_VALUE, 0},
8edb0a6f 12826 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
6fc2811b 12827 {":relief", IMAGE_INTEGER_VALUE, 0},
a93f4566 12828 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6fc2811b 12829 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
a05e2bae
JR
12830 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
12831 {":image", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
12832 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
6fc2811b
JR
12833};
12834
12835/* Structure describing the image type `gif'. */
12836
12837static struct image_type gif_type =
12838{
12839 &Qgif,
12840 gif_image_p,
12841 gif_load,
12842 x_clear_image,
12843 NULL
12844};
12845
12846/* Return non-zero if OBJECT is a valid GIF image specification. */
12847
12848static int
12849gif_image_p (object)
12850 Lisp_Object object;
12851{
12852 struct image_keyword fmt[GIF_LAST];
12853 bcopy (gif_format, fmt, sizeof fmt);
7d0393cf 12854
8f92c555 12855 if (!parse_image_spec (object, fmt, GIF_LAST, Qgif))
6fc2811b 12856 return 0;
7d0393cf 12857
6fc2811b
JR
12858 /* Must specify either the :data or :file keyword. */
12859 return fmt[GIF_FILE].count + fmt[GIF_DATA].count == 1;
12860}
12861
12862/* Reading a GIF image from memory
12863 Based on the PNG memory stuff to a certain extent. */
12864
12865typedef struct
12866{
12867 unsigned char *bytes;
12868 size_t len;
12869 int index;
12870}
12871gif_memory_source;
12872
12873/* Make the current memory source available to gif_read_from_memory.
12874 It's done this way because not all versions of libungif support
12875 a UserData field in the GifFileType structure. */
12876static gif_memory_source *current_gif_memory_src;
12877
12878static int
12879gif_read_from_memory (file, buf, len)
12880 GifFileType *file;
12881 GifByteType *buf;
12882 int len;
12883{
12884 gif_memory_source *src = current_gif_memory_src;
12885
12886 if (len > src->len - src->index)
12887 return -1;
12888
12889 bcopy (src->bytes + src->index, buf, len);
12890 src->index += len;
12891 return len;
12892}
12893
12894
12895/* Load GIF image IMG for use on frame F. Value is non-zero if
12896 successful. */
12897
12898static int
12899gif_load (f, img)
12900 struct frame *f;
12901 struct image *img;
12902{
12903 Lisp_Object file, specified_file;
12904 Lisp_Object specified_data;
12905 int rc, width, height, x, y, i;
12906 XImage *ximg;
12907 ColorMapObject *gif_color_map;
12908 unsigned long pixel_colors[256];
12909 GifFileType *gif;
12910 struct gcpro gcpro1;
12911 Lisp_Object image;
12912 int ino, image_left, image_top, image_width, image_height;
12913 gif_memory_source memsrc;
12914 unsigned char *raster;
12915
12916 specified_file = image_spec_value (img->spec, QCfile, NULL);
12917 specified_data = image_spec_value (img->spec, QCdata, NULL);
12918 file = Qnil;
dfff8a69 12919 GCPRO1 (file);
6fc2811b
JR
12920
12921 if (NILP (specified_data))
12922 {
12923 file = x_find_image_file (specified_file);
6fc2811b
JR
12924 if (!STRINGP (file))
12925 {
12926 image_error ("Cannot find image file `%s'", specified_file, Qnil);
12927 UNGCPRO;
12928 return 0;
12929 }
7d0393cf 12930
6fc2811b 12931 /* Open the GIF file. */
d5db4077 12932 gif = DGifOpenFileName (SDATA (file));
6fc2811b
JR
12933 if (gif == NULL)
12934 {
12935 image_error ("Cannot open `%s'", file, Qnil);
12936 UNGCPRO;
12937 return 0;
12938 }
12939 }
12940 else
12941 {
12942 /* Read from memory! */
12943 current_gif_memory_src = &memsrc;
d5db4077
KR
12944 memsrc.bytes = SDATA (specified_data);
12945 memsrc.len = SBYTES (specified_data);
6fc2811b
JR
12946 memsrc.index = 0;
12947
12948 gif = DGifOpen(&memsrc, gif_read_from_memory);
12949 if (!gif)
12950 {
12951 image_error ("Cannot open memory source `%s'", img->spec, Qnil);
12952 UNGCPRO;
12953 return 0;
12954 }
12955 }
12956
12957 /* Read entire contents. */
12958 rc = DGifSlurp (gif);
12959 if (rc == GIF_ERROR)
12960 {
12961 image_error ("Error reading `%s'", img->spec, Qnil);
12962 DGifCloseFile (gif);
12963 UNGCPRO;
12964 return 0;
12965 }
12966
12967 image = image_spec_value (img->spec, QCindex, NULL);
12968 ino = INTEGERP (image) ? XFASTINT (image) : 0;
12969 if (ino >= gif->ImageCount)
12970 {
12971 image_error ("Invalid image number `%s' in image `%s'",
12972 image, img->spec);
12973 DGifCloseFile (gif);
12974 UNGCPRO;
12975 return 0;
12976 }
12977
12978 width = img->width = gif->SWidth;
12979 height = img->height = gif->SHeight;
12980
6fc2811b
JR
12981 /* Create the X image and pixmap. */
12982 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
12983 {
6fc2811b
JR
12984 DGifCloseFile (gif);
12985 UNGCPRO;
12986 return 0;
12987 }
7d0393cf 12988
6fc2811b
JR
12989 /* Allocate colors. */
12990 gif_color_map = gif->SavedImages[ino].ImageDesc.ColorMap;
12991 if (!gif_color_map)
12992 gif_color_map = gif->SColorMap;
12993 init_color_table ();
12994 bzero (pixel_colors, sizeof pixel_colors);
7d0393cf 12995
6fc2811b
JR
12996 for (i = 0; i < gif_color_map->ColorCount; ++i)
12997 {
12998 int r = gif_color_map->Colors[i].Red << 8;
12999 int g = gif_color_map->Colors[i].Green << 8;
13000 int b = gif_color_map->Colors[i].Blue << 8;
13001 pixel_colors[i] = lookup_rgb_color (f, r, g, b);
13002 }
13003
13004 img->colors = colors_in_color_table (&img->ncolors);
13005 free_color_table ();
13006
13007 /* Clear the part of the screen image that are not covered by
7d0393cf 13008 the image from the GIF file. Full animated GIF support
6fc2811b
JR
13009 requires more than can be done here (see the gif89 spec,
13010 disposal methods). Let's simply assume that the part
13011 not covered by a sub-image is in the frame's background color. */
13012 image_top = gif->SavedImages[ino].ImageDesc.Top;
13013 image_left = gif->SavedImages[ino].ImageDesc.Left;
13014 image_width = gif->SavedImages[ino].ImageDesc.Width;
13015 image_height = gif->SavedImages[ino].ImageDesc.Height;
13016
13017 for (y = 0; y < image_top; ++y)
13018 for (x = 0; x < width; ++x)
13019 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
13020
13021 for (y = image_top + image_height; y < height; ++y)
13022 for (x = 0; x < width; ++x)
13023 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
13024
13025 for (y = image_top; y < image_top + image_height; ++y)
13026 {
13027 for (x = 0; x < image_left; ++x)
13028 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
13029 for (x = image_left + image_width; x < width; ++x)
13030 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
13031 }
13032
13033 /* Read the GIF image into the X image. We use a local variable
13034 `raster' here because RasterBits below is a char *, and invites
13035 problems with bytes >= 0x80. */
13036 raster = (unsigned char *) gif->SavedImages[ino].RasterBits;
13037
13038 if (gif->SavedImages[ino].ImageDesc.Interlace)
13039 {
13040 static int interlace_start[] = {0, 4, 2, 1};
13041 static int interlace_increment[] = {8, 8, 4, 2};
a05e2bae 13042 int pass;
6fc2811b
JR
13043 int row = interlace_start[0];
13044
13045 pass = 0;
13046
13047 for (y = 0; y < image_height; y++)
13048 {
13049 if (row >= image_height)
13050 {
13051 row = interlace_start[++pass];
13052 while (row >= image_height)
13053 row = interlace_start[++pass];
13054 }
7d0393cf 13055
6fc2811b
JR
13056 for (x = 0; x < image_width; x++)
13057 {
13058 int i = raster[(y * image_width) + x];
13059 XPutPixel (ximg, x + image_left, row + image_top,
13060 pixel_colors[i]);
13061 }
7d0393cf 13062
6fc2811b
JR
13063 row += interlace_increment[pass];
13064 }
13065 }
13066 else
13067 {
13068 for (y = 0; y < image_height; ++y)
13069 for (x = 0; x < image_width; ++x)
13070 {
13071 int i = raster[y* image_width + x];
13072 XPutPixel (ximg, x + image_left, y + image_top, pixel_colors[i]);
13073 }
13074 }
7d0393cf 13075
6fc2811b 13076 DGifCloseFile (gif);
a05e2bae
JR
13077
13078 /* Maybe fill in the background field while we have ximg handy. */
13079 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
13080 IMAGE_BACKGROUND (img, f, ximg);
13081
6fc2811b
JR
13082 /* Put the image into the pixmap, then free the X image and its buffer. */
13083 x_put_x_image (f, ximg, img->pixmap, width, height);
13084 x_destroy_x_image (ximg);
7d0393cf 13085
6fc2811b
JR
13086 UNGCPRO;
13087 return 1;
13088}
13089
13090#endif /* HAVE_GIF != 0 */
13091
13092
13093\f
13094/***********************************************************************
13095 Ghostscript
13096 ***********************************************************************/
13097
3cf3436e
JR
13098Lisp_Object Qpostscript;
13099
6fc2811b
JR
13100#ifdef HAVE_GHOSTSCRIPT
13101static int gs_image_p P_ ((Lisp_Object object));
13102static int gs_load P_ ((struct frame *f, struct image *img));
13103static void gs_clear_image P_ ((struct frame *f, struct image *img));
13104
13105/* The symbol `postscript' identifying images of this type. */
13106
6fc2811b
JR
13107/* Keyword symbols. */
13108
13109Lisp_Object QCloader, QCbounding_box, QCpt_width, QCpt_height;
13110
13111/* Indices of image specification fields in gs_format, below. */
13112
13113enum gs_keyword_index
13114{
13115 GS_TYPE,
13116 GS_PT_WIDTH,
13117 GS_PT_HEIGHT,
13118 GS_FILE,
13119 GS_LOADER,
13120 GS_BOUNDING_BOX,
13121 GS_ASCENT,
13122 GS_MARGIN,
13123 GS_RELIEF,
13124 GS_ALGORITHM,
13125 GS_HEURISTIC_MASK,
a05e2bae
JR
13126 GS_MASK,
13127 GS_BACKGROUND,
6fc2811b
JR
13128 GS_LAST
13129};
13130
13131/* Vector of image_keyword structures describing the format
13132 of valid user-defined image specifications. */
13133
13134static struct image_keyword gs_format[GS_LAST] =
13135{
13136 {":type", IMAGE_SYMBOL_VALUE, 1},
13137 {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE, 1},
13138 {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE, 1},
13139 {":file", IMAGE_STRING_VALUE, 1},
13140 {":loader", IMAGE_FUNCTION_VALUE, 0},
13141 {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE, 1},
8f92c555 13142 {":ascent", IMAGE_ASCENT_VALUE, 0},
8edb0a6f 13143 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
6fc2811b 13144 {":relief", IMAGE_INTEGER_VALUE, 0},
a93f4566 13145 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
a05e2bae
JR
13146 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
13147 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
13148 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
6fc2811b
JR
13149};
13150
13151/* Structure describing the image type `ghostscript'. */
13152
13153static struct image_type gs_type =
13154{
13155 &Qpostscript,
13156 gs_image_p,
13157 gs_load,
13158 gs_clear_image,
13159 NULL
13160};
13161
13162
13163/* Free X resources of Ghostscript image IMG which is used on frame F. */
13164
13165static void
13166gs_clear_image (f, img)
13167 struct frame *f;
13168 struct image *img;
13169{
13170 /* IMG->data.ptr_val may contain a recorded colormap. */
13171 xfree (img->data.ptr_val);
13172 x_clear_image (f, img);
13173}
13174
13175
13176/* Return non-zero if OBJECT is a valid Ghostscript image
13177 specification. */
13178
13179static int
13180gs_image_p (object)
13181 Lisp_Object object;
13182{
13183 struct image_keyword fmt[GS_LAST];
13184 Lisp_Object tem;
13185 int i;
7d0393cf 13186
6fc2811b 13187 bcopy (gs_format, fmt, sizeof fmt);
7d0393cf 13188
8f92c555 13189 if (!parse_image_spec (object, fmt, GS_LAST, Qpostscript))
6fc2811b
JR
13190 return 0;
13191
13192 /* Bounding box must be a list or vector containing 4 integers. */
13193 tem = fmt[GS_BOUNDING_BOX].value;
13194 if (CONSP (tem))
13195 {
13196 for (i = 0; i < 4; ++i, tem = XCDR (tem))
13197 if (!CONSP (tem) || !INTEGERP (XCAR (tem)))
13198 return 0;
13199 if (!NILP (tem))
13200 return 0;
13201 }
13202 else if (VECTORP (tem))
13203 {
13204 if (XVECTOR (tem)->size != 4)
13205 return 0;
13206 for (i = 0; i < 4; ++i)
13207 if (!INTEGERP (XVECTOR (tem)->contents[i]))
13208 return 0;
13209 }
13210 else
13211 return 0;
13212
13213 return 1;
13214}
13215
13216
13217/* Load Ghostscript image IMG for use on frame F. Value is non-zero
13218 if successful. */
13219
13220static int
13221gs_load (f, img)
13222 struct frame *f;
13223 struct image *img;
13224{
13225 char buffer[100];
13226 Lisp_Object window_and_pixmap_id = Qnil, loader, pt_height, pt_width;
13227 struct gcpro gcpro1, gcpro2;
13228 Lisp_Object frame;
13229 double in_width, in_height;
13230 Lisp_Object pixel_colors = Qnil;
13231
13232 /* Compute pixel size of pixmap needed from the given size in the
13233 image specification. Sizes in the specification are in pt. 1 pt
13234 = 1/72 in, xdpi and ydpi are stored in the frame's X display
13235 info. */
13236 pt_width = image_spec_value (img->spec, QCpt_width, NULL);
13237 in_width = XFASTINT (pt_width) / 72.0;
13238 img->width = in_width * FRAME_W32_DISPLAY_INFO (f)->resx;
13239 pt_height = image_spec_value (img->spec, QCpt_height, NULL);
13240 in_height = XFASTINT (pt_height) / 72.0;
13241 img->height = in_height * FRAME_W32_DISPLAY_INFO (f)->resy;
13242
13243 /* Create the pixmap. */
13244 BLOCK_INPUT;
13245 xassert (img->pixmap == 0);
13246 img->pixmap = XCreatePixmap (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
13247 img->width, img->height,
a05e2bae 13248 one_w32_display_info.n_cbits);
6fc2811b
JR
13249 UNBLOCK_INPUT;
13250
13251 if (!img->pixmap)
13252 {
13253 image_error ("Unable to create pixmap for `%s'", img->spec, Qnil);
13254 return 0;
13255 }
7d0393cf 13256
6fc2811b
JR
13257 /* Call the loader to fill the pixmap. It returns a process object
13258 if successful. We do not record_unwind_protect here because
13259 other places in redisplay like calling window scroll functions
13260 don't either. Let the Lisp loader use `unwind-protect' instead. */
13261 GCPRO2 (window_and_pixmap_id, pixel_colors);
13262
13263 sprintf (buffer, "%lu %lu",
13264 (unsigned long) FRAME_W32_WINDOW (f),
13265 (unsigned long) img->pixmap);
13266 window_and_pixmap_id = build_string (buffer);
7d0393cf 13267
6fc2811b
JR
13268 sprintf (buffer, "%lu %lu",
13269 FRAME_FOREGROUND_PIXEL (f),
13270 FRAME_BACKGROUND_PIXEL (f));
13271 pixel_colors = build_string (buffer);
7d0393cf 13272
6fc2811b
JR
13273 XSETFRAME (frame, f);
13274 loader = image_spec_value (img->spec, QCloader, NULL);
13275 if (NILP (loader))
13276 loader = intern ("gs-load-image");
13277
13278 img->data.lisp_val = call6 (loader, frame, img->spec,
13279 make_number (img->width),
13280 make_number (img->height),
13281 window_and_pixmap_id,
13282 pixel_colors);
13283 UNGCPRO;
13284 return PROCESSP (img->data.lisp_val);
13285}
13286
13287
13288/* Kill the Ghostscript process that was started to fill PIXMAP on
13289 frame F. Called from XTread_socket when receiving an event
13290 telling Emacs that Ghostscript has finished drawing. */
13291
13292void
13293x_kill_gs_process (pixmap, f)
13294 Pixmap pixmap;
13295 struct frame *f;
13296{
13297 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
13298 int class, i;
13299 struct image *img;
13300
13301 /* Find the image containing PIXMAP. */
13302 for (i = 0; i < c->used; ++i)
13303 if (c->images[i]->pixmap == pixmap)
13304 break;
13305
3cf3436e
JR
13306 /* Should someone in between have cleared the image cache, for
13307 instance, give up. */
13308 if (i == c->used)
13309 return;
13310
6fc2811b
JR
13311 /* Kill the GS process. We should have found PIXMAP in the image
13312 cache and its image should contain a process object. */
6fc2811b
JR
13313 img = c->images[i];
13314 xassert (PROCESSP (img->data.lisp_val));
13315 Fkill_process (img->data.lisp_val, Qnil);
13316 img->data.lisp_val = Qnil;
13317
13318 /* On displays with a mutable colormap, figure out the colors
13319 allocated for the image by looking at the pixels of an XImage for
13320 img->pixmap. */
13321 class = FRAME_W32_DISPLAY_INFO (f)->visual->class;
13322 if (class != StaticColor && class != StaticGray && class != TrueColor)
13323 {
13324 XImage *ximg;
13325
13326 BLOCK_INPUT;
13327
13328 /* Try to get an XImage for img->pixmep. */
13329 ximg = XGetImage (FRAME_W32_DISPLAY (f), img->pixmap,
13330 0, 0, img->width, img->height, ~0, ZPixmap);
13331 if (ximg)
13332 {
13333 int x, y;
7d0393cf 13334
6fc2811b
JR
13335 /* Initialize the color table. */
13336 init_color_table ();
7d0393cf 13337
6fc2811b
JR
13338 /* For each pixel of the image, look its color up in the
13339 color table. After having done so, the color table will
13340 contain an entry for each color used by the image. */
13341 for (y = 0; y < img->height; ++y)
13342 for (x = 0; x < img->width; ++x)
13343 {
13344 unsigned long pixel = XGetPixel (ximg, x, y);
13345 lookup_pixel_color (f, pixel);
13346 }
13347
13348 /* Record colors in the image. Free color table and XImage. */
13349 img->colors = colors_in_color_table (&img->ncolors);
13350 free_color_table ();
13351 XDestroyImage (ximg);
13352
13353#if 0 /* This doesn't seem to be the case. If we free the colors
13354 here, we get a BadAccess later in x_clear_image when
13355 freeing the colors. */
13356 /* We have allocated colors once, but Ghostscript has also
13357 allocated colors on behalf of us. So, to get the
13358 reference counts right, free them once. */
13359 if (img->ncolors)
3cf3436e 13360 x_free_colors (FRAME_W32_DISPLAY (f), cmap,
6fc2811b 13361 img->colors, img->ncolors, 0);
6fc2811b
JR
13362#endif
13363 }
13364 else
13365 image_error ("Cannot get X image of `%s'; colors will not be freed",
13366 img->spec, Qnil);
7d0393cf 13367
6fc2811b
JR
13368 UNBLOCK_INPUT;
13369 }
3cf3436e
JR
13370
13371 /* Now that we have the pixmap, compute mask and transform the
13372 image if requested. */
13373 BLOCK_INPUT;
13374 postprocess_image (f, img);
13375 UNBLOCK_INPUT;
6fc2811b
JR
13376}
13377
13378#endif /* HAVE_GHOSTSCRIPT */
13379
13380\f
13381/***********************************************************************
13382 Window properties
13383 ***********************************************************************/
13384
13385DEFUN ("x-change-window-property", Fx_change_window_property,
13386 Sx_change_window_property, 2, 3, 0,
74e1aeec
JR
13387 doc: /* Change window property PROP to VALUE on the X window of FRAME.
13388PROP and VALUE must be strings. FRAME nil or omitted means use the
13389selected frame. Value is VALUE. */)
6fc2811b
JR
13390 (prop, value, frame)
13391 Lisp_Object frame, prop, value;
13392{
767b1ff0 13393#if 0 /* TODO : port window properties to W32 */
6fc2811b
JR
13394 struct frame *f = check_x_frame (frame);
13395 Atom prop_atom;
13396
b7826503
PJ
13397 CHECK_STRING (prop);
13398 CHECK_STRING (value);
6fc2811b
JR
13399
13400 BLOCK_INPUT;
d5db4077 13401 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
6fc2811b
JR
13402 XChangeProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
13403 prop_atom, XA_STRING, 8, PropModeReplace,
d5db4077 13404 SDATA (value), SCHARS (value));
6fc2811b
JR
13405
13406 /* Make sure the property is set when we return. */
13407 XFlush (FRAME_W32_DISPLAY (f));
13408 UNBLOCK_INPUT;
13409
767b1ff0 13410#endif /* TODO */
6fc2811b
JR
13411
13412 return value;
13413}
13414
13415
13416DEFUN ("x-delete-window-property", Fx_delete_window_property,
13417 Sx_delete_window_property, 1, 2, 0,
74e1aeec
JR
13418 doc: /* Remove window property PROP from X window of FRAME.
13419FRAME nil or omitted means use the selected frame. Value is PROP. */)
6fc2811b
JR
13420 (prop, frame)
13421 Lisp_Object prop, frame;
13422{
767b1ff0 13423#if 0 /* TODO : port window properties to W32 */
6fc2811b
JR
13424
13425 struct frame *f = check_x_frame (frame);
13426 Atom prop_atom;
13427
b7826503 13428 CHECK_STRING (prop);
6fc2811b 13429 BLOCK_INPUT;
d5db4077 13430 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
6fc2811b
JR
13431 XDeleteProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), prop_atom);
13432
13433 /* Make sure the property is removed when we return. */
13434 XFlush (FRAME_W32_DISPLAY (f));
13435 UNBLOCK_INPUT;
767b1ff0 13436#endif /* TODO */
6fc2811b
JR
13437
13438 return prop;
13439}
13440
13441
13442DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
13443 1, 2, 0,
74e1aeec
JR
13444 doc: /* Value is the value of window property PROP on FRAME.
13445If FRAME is nil or omitted, use the selected frame. Value is nil
13446if FRAME hasn't a property with name PROP or if PROP has no string
13447value. */)
6fc2811b
JR
13448 (prop, frame)
13449 Lisp_Object prop, frame;
13450{
767b1ff0 13451#if 0 /* TODO : port window properties to W32 */
6fc2811b
JR
13452
13453 struct frame *f = check_x_frame (frame);
13454 Atom prop_atom;
13455 int rc;
13456 Lisp_Object prop_value = Qnil;
13457 char *tmp_data = NULL;
13458 Atom actual_type;
13459 int actual_format;
13460 unsigned long actual_size, bytes_remaining;
13461
b7826503 13462 CHECK_STRING (prop);
6fc2811b 13463 BLOCK_INPUT;
d5db4077 13464 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
6fc2811b
JR
13465 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
13466 prop_atom, 0, 0, False, XA_STRING,
13467 &actual_type, &actual_format, &actual_size,
13468 &bytes_remaining, (unsigned char **) &tmp_data);
13469 if (rc == Success)
13470 {
13471 int size = bytes_remaining;
13472
13473 XFree (tmp_data);
13474 tmp_data = NULL;
13475
13476 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
13477 prop_atom, 0, bytes_remaining,
13478 False, XA_STRING,
7d0393cf
JB
13479 &actual_type, &actual_format,
13480 &actual_size, &bytes_remaining,
6fc2811b
JR
13481 (unsigned char **) &tmp_data);
13482 if (rc == Success)
13483 prop_value = make_string (tmp_data, size);
13484
13485 XFree (tmp_data);
13486 }
13487
13488 UNBLOCK_INPUT;
13489
13490 return prop_value;
13491
767b1ff0 13492#endif /* TODO */
6fc2811b
JR
13493 return Qnil;
13494}
13495
13496
13497\f
13498/***********************************************************************
13499 Busy cursor
13500 ***********************************************************************/
13501
f79e6790 13502/* If non-null, an asynchronous timer that, when it expires, displays
0af913d7 13503 an hourglass cursor on all frames. */
6fc2811b 13504
0af913d7 13505static struct atimer *hourglass_atimer;
6fc2811b 13506
0af913d7 13507/* Non-zero means an hourglass cursor is currently shown. */
6fc2811b 13508
0af913d7 13509static int hourglass_shown_p;
6fc2811b 13510
0af913d7 13511/* Number of seconds to wait before displaying an hourglass cursor. */
6fc2811b 13512
0af913d7 13513static Lisp_Object Vhourglass_delay;
6fc2811b 13514
0af913d7 13515/* Default number of seconds to wait before displaying an hourglass
f79e6790
JR
13516 cursor. */
13517
0af913d7 13518#define DEFAULT_HOURGLASS_DELAY 1
f79e6790
JR
13519
13520/* Function prototypes. */
13521
0af913d7
GM
13522static void show_hourglass P_ ((struct atimer *));
13523static void hide_hourglass P_ ((void));
f79e6790
JR
13524
13525
0af913d7 13526/* Cancel a currently active hourglass timer, and start a new one. */
f79e6790
JR
13527
13528void
0af913d7 13529start_hourglass ()
f79e6790 13530{
767b1ff0 13531#if 0 /* TODO: cursor shape changes. */
f79e6790 13532 EMACS_TIME delay;
dfff8a69 13533 int secs, usecs = 0;
7d0393cf 13534
0af913d7 13535 cancel_hourglass ();
f79e6790 13536
0af913d7
GM
13537 if (INTEGERP (Vhourglass_delay)
13538 && XINT (Vhourglass_delay) > 0)
13539 secs = XFASTINT (Vhourglass_delay);
13540 else if (FLOATP (Vhourglass_delay)
13541 && XFLOAT_DATA (Vhourglass_delay) > 0)
dfff8a69
JR
13542 {
13543 Lisp_Object tem;
0af913d7 13544 tem = Ftruncate (Vhourglass_delay, Qnil);
dfff8a69 13545 secs = XFASTINT (tem);
0af913d7 13546 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
dfff8a69 13547 }
f79e6790 13548 else
0af913d7 13549 secs = DEFAULT_HOURGLASS_DELAY;
7d0393cf 13550
dfff8a69 13551 EMACS_SET_SECS_USECS (delay, secs, usecs);
0af913d7
GM
13552 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
13553 show_hourglass, NULL);
f79e6790
JR
13554#endif
13555}
13556
13557
0af913d7
GM
13558/* Cancel the hourglass cursor timer if active, hide an hourglass
13559 cursor if shown. */
f79e6790
JR
13560
13561void
0af913d7 13562cancel_hourglass ()
f79e6790 13563{
0af913d7 13564 if (hourglass_atimer)
dfff8a69 13565 {
0af913d7
GM
13566 cancel_atimer (hourglass_atimer);
13567 hourglass_atimer = NULL;
dfff8a69 13568 }
7d0393cf 13569
0af913d7
GM
13570 if (hourglass_shown_p)
13571 hide_hourglass ();
f79e6790
JR
13572}
13573
13574
0af913d7
GM
13575/* Timer function of hourglass_atimer. TIMER is equal to
13576 hourglass_atimer.
f79e6790 13577
0af913d7
GM
13578 Display an hourglass cursor on all frames by mapping the frames'
13579 hourglass_window. Set the hourglass_p flag in the frames'
13580 output_data.x structure to indicate that an hourglass cursor is
13581 shown on the frames. */
f79e6790
JR
13582
13583static void
0af913d7 13584show_hourglass (timer)
f79e6790 13585 struct atimer *timer;
6fc2811b 13586{
767b1ff0 13587#if 0 /* TODO: cursor shape changes. */
f79e6790 13588 /* The timer implementation will cancel this timer automatically
0af913d7 13589 after this function has run. Set hourglass_atimer to null
f79e6790 13590 so that we know the timer doesn't have to be canceled. */
0af913d7 13591 hourglass_atimer = NULL;
f79e6790 13592
0af913d7 13593 if (!hourglass_shown_p)
6fc2811b
JR
13594 {
13595 Lisp_Object rest, frame;
7d0393cf 13596
f79e6790 13597 BLOCK_INPUT;
7d0393cf 13598
6fc2811b 13599 FOR_EACH_FRAME (rest, frame)
dc220243 13600 if (FRAME_W32_P (XFRAME (frame)))
6fc2811b
JR
13601 {
13602 struct frame *f = XFRAME (frame);
7d0393cf 13603
0af913d7 13604 f->output_data.w32->hourglass_p = 1;
7d0393cf 13605
0af913d7 13606 if (!f->output_data.w32->hourglass_window)
6fc2811b
JR
13607 {
13608 unsigned long mask = CWCursor;
13609 XSetWindowAttributes attrs;
7d0393cf 13610
0af913d7 13611 attrs.cursor = f->output_data.w32->hourglass_cursor;
7d0393cf 13612
0af913d7 13613 f->output_data.w32->hourglass_window
f79e6790 13614 = XCreateWindow (FRAME_X_DISPLAY (f),
6fc2811b
JR
13615 FRAME_OUTER_WINDOW (f),
13616 0, 0, 32000, 32000, 0, 0,
f79e6790
JR
13617 InputOnly,
13618 CopyFromParent,
6fc2811b
JR
13619 mask, &attrs);
13620 }
7d0393cf 13621
0af913d7
GM
13622 XMapRaised (FRAME_X_DISPLAY (f),
13623 f->output_data.w32->hourglass_window);
f79e6790 13624 XFlush (FRAME_X_DISPLAY (f));
6fc2811b 13625 }
6fc2811b 13626
0af913d7 13627 hourglass_shown_p = 1;
f79e6790
JR
13628 UNBLOCK_INPUT;
13629 }
13630#endif
6fc2811b
JR
13631}
13632
13633
0af913d7 13634/* Hide the hourglass cursor on all frames, if it is currently shown. */
6fc2811b 13635
f79e6790 13636static void
0af913d7 13637hide_hourglass ()
f79e6790 13638{
767b1ff0 13639#if 0 /* TODO: cursor shape changes. */
0af913d7 13640 if (hourglass_shown_p)
6fc2811b 13641 {
f79e6790
JR
13642 Lisp_Object rest, frame;
13643
13644 BLOCK_INPUT;
13645 FOR_EACH_FRAME (rest, frame)
6fc2811b 13646 {
f79e6790 13647 struct frame *f = XFRAME (frame);
7d0393cf 13648
dc220243 13649 if (FRAME_W32_P (f)
f79e6790 13650 /* Watch out for newly created frames. */
0af913d7 13651 && f->output_data.x->hourglass_window)
f79e6790 13652 {
0af913d7
GM
13653 XUnmapWindow (FRAME_X_DISPLAY (f),
13654 f->output_data.x->hourglass_window);
13655 /* Sync here because XTread_socket looks at the
13656 hourglass_p flag that is reset to zero below. */
f79e6790 13657 XSync (FRAME_X_DISPLAY (f), False);
0af913d7 13658 f->output_data.x->hourglass_p = 0;
f79e6790 13659 }
6fc2811b 13660 }
6fc2811b 13661
0af913d7 13662 hourglass_shown_p = 0;
f79e6790
JR
13663 UNBLOCK_INPUT;
13664 }
13665#endif
6fc2811b
JR
13666}
13667
13668
13669\f
13670/***********************************************************************
13671 Tool tips
13672 ***********************************************************************/
13673
13674static Lisp_Object x_create_tip_frame P_ ((struct w32_display_info *,
3cf3436e
JR
13675 Lisp_Object, Lisp_Object));
13676static void compute_tip_xy P_ ((struct frame *, Lisp_Object, Lisp_Object,
13677 Lisp_Object, int, int, int *, int *));
7d0393cf 13678
3cf3436e 13679/* The frame of a currently visible tooltip. */
6fc2811b 13680
937e601e 13681Lisp_Object tip_frame;
6fc2811b
JR
13682
13683/* If non-nil, a timer started that hides the last tooltip when it
13684 fires. */
13685
13686Lisp_Object tip_timer;
13687Window tip_window;
13688
3cf3436e
JR
13689/* If non-nil, a vector of 3 elements containing the last args
13690 with which x-show-tip was called. See there. */
13691
13692Lisp_Object last_show_tip_args;
13693
13694/* Maximum size for tooltips; a cons (COLUMNS . ROWS). */
13695
13696Lisp_Object Vx_max_tooltip_size;
13697
13698
937e601e
AI
13699static Lisp_Object
13700unwind_create_tip_frame (frame)
13701 Lisp_Object frame;
13702{
c844a81a
GM
13703 Lisp_Object deleted;
13704
13705 deleted = unwind_create_frame (frame);
13706 if (EQ (deleted, Qt))
13707 {
13708 tip_window = NULL;
13709 tip_frame = Qnil;
13710 }
7d0393cf 13711
c844a81a 13712 return deleted;
937e601e
AI
13713}
13714
13715
6fc2811b 13716/* Create a frame for a tooltip on the display described by DPYINFO.
3cf3436e
JR
13717 PARMS is a list of frame parameters. TEXT is the string to
13718 display in the tip frame. Value is the frame.
937e601e
AI
13719
13720 Note that functions called here, esp. x_default_parameter can
13721 signal errors, for instance when a specified color name is
13722 undefined. We have to make sure that we're in a consistent state
13723 when this happens. */
6fc2811b
JR
13724
13725static Lisp_Object
3cf3436e 13726x_create_tip_frame (dpyinfo, parms, text)
6fc2811b 13727 struct w32_display_info *dpyinfo;
3cf3436e 13728 Lisp_Object parms, text;
6fc2811b 13729{
6fc2811b
JR
13730 struct frame *f;
13731 Lisp_Object frame, tem;
13732 Lisp_Object name;
13733 long window_prompting = 0;
13734 int width, height;
331379bf 13735 int count = SPECPDL_INDEX ();
6fc2811b
JR
13736 struct gcpro gcpro1, gcpro2, gcpro3;
13737 struct kboard *kb;
3cf3436e
JR
13738 int face_change_count_before = face_change_count;
13739 Lisp_Object buffer;
13740 struct buffer *old_buffer;
6fc2811b 13741
ca56d953 13742 check_w32 ();
6fc2811b
JR
13743
13744 /* Use this general default value to start with until we know if
13745 this frame has a specified name. */
13746 Vx_resource_name = Vinvocation_name;
13747
13748#ifdef MULTI_KBOARD
13749 kb = dpyinfo->kboard;
13750#else
13751 kb = &the_only_kboard;
13752#endif
13753
13754 /* Get the name of the frame to use for resource lookup. */
13755 name = w32_get_arg (parms, Qname, "name", "Name", RES_TYPE_STRING);
13756 if (!STRINGP (name)
13757 && !EQ (name, Qunbound)
13758 && !NILP (name))
13759 error ("Invalid frame name--not a string or nil");
13760 Vx_resource_name = name;
13761
13762 frame = Qnil;
13763 GCPRO3 (parms, name, frame);
9eb16b62
JR
13764 /* Make a frame without minibuffer nor mode-line. */
13765 f = make_frame (0);
13766 f->wants_modeline = 0;
6fc2811b 13767 XSETFRAME (frame, f);
3cf3436e
JR
13768
13769 buffer = Fget_buffer_create (build_string (" *tip*"));
13770 Fset_window_buffer (FRAME_ROOT_WINDOW (f), buffer);
13771 old_buffer = current_buffer;
13772 set_buffer_internal_1 (XBUFFER (buffer));
13773 current_buffer->truncate_lines = Qnil;
13774 Ferase_buffer ();
13775 Finsert (1, &text);
13776 set_buffer_internal_1 (old_buffer);
7d0393cf 13777
6fc2811b 13778 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
937e601e 13779 record_unwind_protect (unwind_create_tip_frame, frame);
6fc2811b 13780
3cf3436e
JR
13781 /* By setting the output method, we're essentially saying that
13782 the frame is live, as per FRAME_LIVE_P. If we get a signal
13783 from this point on, x_destroy_window might screw up reference
13784 counts etc. */
d88c567c 13785 f->output_method = output_w32;
6fc2811b
JR
13786 f->output_data.w32 =
13787 (struct w32_output *) xmalloc (sizeof (struct w32_output));
13788 bzero (f->output_data.w32, sizeof (struct w32_output));
ca56d953
JR
13789
13790 FRAME_FONTSET (f) = -1;
6fc2811b
JR
13791 f->icon_name = Qnil;
13792
ca56d953 13793#if 0 /* GLYPH_DEBUG TODO: image support. */
937e601e
AI
13794 image_cache_refcount = FRAME_X_IMAGE_CACHE (f)->refcount;
13795 dpyinfo_refcount = dpyinfo->reference_count;
13796#endif /* GLYPH_DEBUG */
6fc2811b
JR
13797#ifdef MULTI_KBOARD
13798 FRAME_KBOARD (f) = kb;
13799#endif
13800 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
13801 f->output_data.w32->explicit_parent = 0;
13802
13803 /* Set the name; the functions to which we pass f expect the name to
13804 be set. */
13805 if (EQ (name, Qunbound) || NILP (name))
13806 {
ca56d953 13807 f->name = build_string (dpyinfo->w32_id_name);
6fc2811b
JR
13808 f->explicit_name = 0;
13809 }
13810 else
13811 {
13812 f->name = name;
13813 f->explicit_name = 1;
13814 /* use the frame's title when getting resources for this frame. */
13815 specbind (Qx_resource_name, name);
13816 }
13817
6fc2811b
JR
13818 /* Extract the window parameters from the supplied values
13819 that are needed to determine window geometry. */
13820 {
13821 Lisp_Object font;
13822
13823 font = w32_get_arg (parms, Qfont, "font", "Font", RES_TYPE_STRING);
13824
13825 BLOCK_INPUT;
13826 /* First, try whatever font the caller has specified. */
13827 if (STRINGP (font))
13828 {
13829 tem = Fquery_fontset (font, Qnil);
13830 if (STRINGP (tem))
d5db4077 13831 font = x_new_fontset (f, SDATA (tem));
6fc2811b 13832 else
d5db4077 13833 font = x_new_font (f, SDATA (font));
6fc2811b 13834 }
7d0393cf 13835
6fc2811b
JR
13836 /* Try out a font which we hope has bold and italic variations. */
13837 if (!STRINGP (font))
ca56d953 13838 font = x_new_font (f, "-*-Courier New-normal-r-*-*-*-100-*-*-c-*-iso8859-1");
6fc2811b 13839 if (! STRINGP (font))
ca56d953 13840 font = x_new_font (f, "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1");
6fc2811b
JR
13841 /* If those didn't work, look for something which will at least work. */
13842 if (! STRINGP (font))
ca56d953 13843 font = x_new_font (f, "-*-Fixedsys-normal-r-*-*-12-*-*-*-c-*-iso8859-1");
6fc2811b
JR
13844 UNBLOCK_INPUT;
13845 if (! STRINGP (font))
ca56d953 13846 font = build_string ("Fixedsys");
6fc2811b
JR
13847
13848 x_default_parameter (f, parms, Qfont, font,
13849 "font", "Font", RES_TYPE_STRING);
13850 }
13851
13852 x_default_parameter (f, parms, Qborder_width, make_number (2),
13853 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
6fc2811b
JR
13854 /* This defaults to 2 in order to match xterm. We recognize either
13855 internalBorderWidth or internalBorder (which is what xterm calls
13856 it). */
13857 if (NILP (Fassq (Qinternal_border_width, parms)))
13858 {
13859 Lisp_Object value;
13860
13861 value = w32_get_arg (parms, Qinternal_border_width,
13862 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
13863 if (! EQ (value, Qunbound))
13864 parms = Fcons (Fcons (Qinternal_border_width, value),
13865 parms);
13866 }
bfd6edcc 13867 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
6fc2811b
JR
13868 "internalBorderWidth", "internalBorderWidth",
13869 RES_TYPE_NUMBER);
13870
13871 /* Also do the stuff which must be set before the window exists. */
13872 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
13873 "foreground", "Foreground", RES_TYPE_STRING);
13874 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
13875 "background", "Background", RES_TYPE_STRING);
13876 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
13877 "pointerColor", "Foreground", RES_TYPE_STRING);
13878 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
13879 "cursorColor", "Foreground", RES_TYPE_STRING);
13880 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
13881 "borderColor", "BorderColor", RES_TYPE_STRING);
13882
13883 /* Init faces before x_default_parameter is called for scroll-bar
13884 parameters because that function calls x_set_scroll_bar_width,
13885 which calls change_frame_size, which calls Fset_window_buffer,
13886 which runs hooks, which call Fvertical_motion. At the end, we
13887 end up in init_iterator with a null face cache, which should not
13888 happen. */
13889 init_frame_faces (f);
ca56d953
JR
13890
13891 f->output_data.w32->dwStyle = WS_BORDER | WS_POPUP | WS_DISABLED;
6fc2811b 13892 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
9eb16b62 13893
6fc2811b
JR
13894 window_prompting = x_figure_window_size (f, parms);
13895
9eb16b62
JR
13896 /* No fringes on tip frame. */
13897 f->output_data.w32->fringes_extra = 0;
13898 f->output_data.w32->fringe_cols = 0;
13899 f->output_data.w32->left_fringe_width = 0;
13900 f->output_data.w32->right_fringe_width = 0;
13901
6fc2811b
JR
13902 if (window_prompting & XNegative)
13903 {
13904 if (window_prompting & YNegative)
13905 f->output_data.w32->win_gravity = SouthEastGravity;
13906 else
13907 f->output_data.w32->win_gravity = NorthEastGravity;
13908 }
13909 else
13910 {
13911 if (window_prompting & YNegative)
13912 f->output_data.w32->win_gravity = SouthWestGravity;
13913 else
13914 f->output_data.w32->win_gravity = NorthWestGravity;
13915 }
13916
13917 f->output_data.w32->size_hint_flags = window_prompting;
ca56d953
JR
13918
13919 BLOCK_INPUT;
13920 my_create_tip_window (f);
13921 UNBLOCK_INPUT;
6fc2811b
JR
13922
13923 x_make_gc (f);
13924
13925 x_default_parameter (f, parms, Qauto_raise, Qnil,
13926 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
13927 x_default_parameter (f, parms, Qauto_lower, Qnil,
13928 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
13929 x_default_parameter (f, parms, Qcursor_type, Qbox,
13930 "cursorType", "CursorType", RES_TYPE_SYMBOL);
13931
13932 /* Dimensions, especially f->height, must be done via change_frame_size.
13933 Change will not be effected unless different from the current
13934 f->height. */
13935 width = f->width;
13936 height = f->height;
13937 f->height = 0;
13938 SET_FRAME_WIDTH (f, 0);
13939 change_frame_size (f, height, width, 1, 0, 0);
13940
cd1d850f
JPW
13941 /* Add `tooltip' frame parameter's default value. */
13942 if (NILP (Fframe_parameter (frame, intern ("tooltip"))))
13943 Fmodify_frame_parameters (frame, Fcons (Fcons (intern ("tooltip"), Qt),
13944 Qnil));
7d0393cf 13945
3cf3436e
JR
13946 /* Set up faces after all frame parameters are known. This call
13947 also merges in face attributes specified for new frames.
13948
13949 Frame parameters may be changed if .Xdefaults contains
13950 specifications for the default font. For example, if there is an
13951 `Emacs.default.attributeBackground: pink', the `background-color'
13952 attribute of the frame get's set, which let's the internal border
13953 of the tooltip frame appear in pink. Prevent this. */
13954 {
13955 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
13956
13957 /* Set tip_frame here, so that */
13958 tip_frame = frame;
13959 call1 (Qface_set_after_frame_default, frame);
7d0393cf 13960
3cf3436e
JR
13961 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
13962 Fmodify_frame_parameters (frame, Fcons (Fcons (Qbackground_color, bg),
13963 Qnil));
13964 }
7d0393cf 13965
6fc2811b
JR
13966 f->no_split = 1;
13967
13968 UNGCPRO;
13969
13970 /* It is now ok to make the frame official even if we get an error
13971 below. And the frame needs to be on Vframe_list or making it
13972 visible won't work. */
13973 Vframe_list = Fcons (frame, Vframe_list);
13974
13975 /* Now that the frame is official, it counts as a reference to
13976 its display. */
13977 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
ee78dc32 13978
3cf3436e
JR
13979 /* Setting attributes of faces of the tooltip frame from resources
13980 and similar will increment face_change_count, which leads to the
13981 clearing of all current matrices. Since this isn't necessary
13982 here, avoid it by resetting face_change_count to the value it
13983 had before we created the tip frame. */
13984 face_change_count = face_change_count_before;
13985
13986 /* Discard the unwind_protect. */
6fc2811b 13987 return unbind_to (count, frame);
ee78dc32
GV
13988}
13989
3cf3436e
JR
13990
13991/* Compute where to display tip frame F. PARMS is the list of frame
13992 parameters for F. DX and DY are specified offsets from the current
13993 location of the mouse. WIDTH and HEIGHT are the width and height
13994 of the tooltip. Return coordinates relative to the root window of
13995 the display in *ROOT_X, and *ROOT_Y. */
13996
13997static void
13998compute_tip_xy (f, parms, dx, dy, width, height, root_x, root_y)
13999 struct frame *f;
14000 Lisp_Object parms, dx, dy;
14001 int width, height;
14002 int *root_x, *root_y;
14003{
3cf3436e 14004 Lisp_Object left, top;
7d0393cf 14005
3cf3436e
JR
14006 /* User-specified position? */
14007 left = Fcdr (Fassq (Qleft, parms));
14008 top = Fcdr (Fassq (Qtop, parms));
7d0393cf 14009
3cf3436e
JR
14010 /* Move the tooltip window where the mouse pointer is. Resize and
14011 show it. */
ca56d953 14012 if (!INTEGERP (left) || !INTEGERP (top))
3cf3436e 14013 {
ca56d953
JR
14014 POINT pt;
14015
3cf3436e 14016 BLOCK_INPUT;
ca56d953
JR
14017 GetCursorPos (&pt);
14018 *root_x = pt.x;
14019 *root_y = pt.y;
3cf3436e
JR
14020 UNBLOCK_INPUT;
14021 }
14022
14023 if (INTEGERP (top))
14024 *root_y = XINT (top);
14025 else if (*root_y + XINT (dy) - height < 0)
14026 *root_y -= XINT (dy);
14027 else
14028 {
14029 *root_y -= height;
14030 *root_y += XINT (dy);
14031 }
14032
14033 if (INTEGERP (left))
14034 *root_x = XINT (left);
72e4adef
JR
14035 else if (*root_x + XINT (dx) + width <= FRAME_W32_DISPLAY_INFO (f)->width)
14036 /* It fits to the right of the pointer. */
14037 *root_x += XINT (dx);
14038 else if (width + XINT (dx) <= *root_x)
14039 /* It fits to the left of the pointer. */
3cf3436e
JR
14040 *root_x -= width + XINT (dx);
14041 else
72e4adef
JR
14042 /* Put it left justified on the screen -- it ought to fit that way. */
14043 *root_x = 0;
3cf3436e
JR
14044}
14045
14046
71eab8d1 14047DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
74e1aeec
JR
14048 doc: /* Show STRING in a \"tooltip\" window on frame FRAME.
14049A tooltip window is a small window displaying a string.
14050
14051FRAME nil or omitted means use the selected frame.
14052
14053PARMS is an optional list of frame parameters which can be
14054used to change the tooltip's appearance.
14055
ca56d953
JR
14056Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
14057means use the default timeout of 5 seconds.
74e1aeec 14058
ca56d953 14059If the list of frame parameters PARAMS contains a `left' parameter,
74e1aeec
JR
14060the tooltip is displayed at that x-position. Otherwise it is
14061displayed at the mouse position, with offset DX added (default is 5 if
14062DX isn't specified). Likewise for the y-position; if a `top' frame
14063parameter is specified, it determines the y-position of the tooltip
14064window, otherwise it is displayed at the mouse position, with offset
14065DY added (default is -10).
14066
14067A tooltip's maximum size is specified by `x-max-tooltip-size'.
14068Text larger than the specified size is clipped. */)
71eab8d1
AI
14069 (string, frame, parms, timeout, dx, dy)
14070 Lisp_Object string, frame, parms, timeout, dx, dy;
ee78dc32 14071{
6fc2811b
JR
14072 struct frame *f;
14073 struct window *w;
3cf3436e 14074 int root_x, root_y;
6fc2811b
JR
14075 struct buffer *old_buffer;
14076 struct text_pos pos;
14077 int i, width, height;
6fc2811b
JR
14078 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
14079 int old_windows_or_buffers_changed = windows_or_buffers_changed;
331379bf 14080 int count = SPECPDL_INDEX ();
7d0393cf 14081
6fc2811b 14082 specbind (Qinhibit_redisplay, Qt);
ee78dc32 14083
dfff8a69 14084 GCPRO4 (string, parms, frame, timeout);
ee78dc32 14085
b7826503 14086 CHECK_STRING (string);
6fc2811b
JR
14087 f = check_x_frame (frame);
14088 if (NILP (timeout))
14089 timeout = make_number (5);
14090 else
b7826503 14091 CHECK_NATNUM (timeout);
ee78dc32 14092
71eab8d1
AI
14093 if (NILP (dx))
14094 dx = make_number (5);
14095 else
b7826503 14096 CHECK_NUMBER (dx);
7d0393cf 14097
71eab8d1 14098 if (NILP (dy))
dc220243 14099 dy = make_number (-10);
71eab8d1 14100 else
b7826503 14101 CHECK_NUMBER (dy);
71eab8d1 14102
dc220243
JR
14103 if (NILP (last_show_tip_args))
14104 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
14105
14106 if (!NILP (tip_frame))
14107 {
14108 Lisp_Object last_string = AREF (last_show_tip_args, 0);
14109 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
14110 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
14111
14112 if (EQ (frame, last_frame)
14113 && !NILP (Fequal (last_string, string))
14114 && !NILP (Fequal (last_parms, parms)))
14115 {
14116 struct frame *f = XFRAME (tip_frame);
7d0393cf 14117
dc220243
JR
14118 /* Only DX and DY have changed. */
14119 if (!NILP (tip_timer))
14120 {
14121 Lisp_Object timer = tip_timer;
14122 tip_timer = Qnil;
14123 call1 (Qcancel_timer, timer);
14124 }
14125
14126 BLOCK_INPUT;
ca56d953
JR
14127 compute_tip_xy (f, parms, dx, dy, PIXEL_WIDTH (f),
14128 PIXEL_HEIGHT (f), &root_x, &root_y);
d65a9cdc
JR
14129
14130 /* Put tooltip in topmost group and in position. */
ca56d953
JR
14131 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
14132 root_x, root_y, 0, 0,
14133 SWP_NOSIZE | SWP_NOACTIVATE);
d65a9cdc
JR
14134
14135 /* Ensure tooltip is on top of other topmost windows (eg menus). */
14136 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
14137 0, 0, 0, 0,
14138 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
14139
dc220243
JR
14140 UNBLOCK_INPUT;
14141 goto start_timer;
14142 }
14143 }
14144
6fc2811b
JR
14145 /* Hide a previous tip, if any. */
14146 Fx_hide_tip ();
ee78dc32 14147
dc220243
JR
14148 ASET (last_show_tip_args, 0, string);
14149 ASET (last_show_tip_args, 1, frame);
14150 ASET (last_show_tip_args, 2, parms);
14151
6fc2811b
JR
14152 /* Add default values to frame parameters. */
14153 if (NILP (Fassq (Qname, parms)))
14154 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
14155 if (NILP (Fassq (Qinternal_border_width, parms)))
14156 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
14157 if (NILP (Fassq (Qborder_width, parms)))
14158 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
14159 if (NILP (Fassq (Qborder_color, parms)))
14160 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
14161 if (NILP (Fassq (Qbackground_color, parms)))
14162 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
14163 parms);
14164
0e3fcdef
JR
14165 /* Block input until the tip has been fully drawn, to avoid crashes
14166 when drawing tips in menus. */
14167 BLOCK_INPUT;
14168
6fc2811b
JR
14169 /* Create a frame for the tooltip, and record it in the global
14170 variable tip_frame. */
ca56d953 14171 frame = x_create_tip_frame (FRAME_W32_DISPLAY_INFO (f), parms, string);
937e601e 14172 f = XFRAME (frame);
6fc2811b 14173
3cf3436e 14174 /* Set up the frame's root window. */
6fc2811b
JR
14175 w = XWINDOW (FRAME_ROOT_WINDOW (f));
14176 w->left = w->top = make_number (0);
3cf3436e
JR
14177
14178 if (CONSP (Vx_max_tooltip_size)
14179 && INTEGERP (XCAR (Vx_max_tooltip_size))
14180 && XINT (XCAR (Vx_max_tooltip_size)) > 0
14181 && INTEGERP (XCDR (Vx_max_tooltip_size))
14182 && XINT (XCDR (Vx_max_tooltip_size)) > 0)
14183 {
14184 w->width = XCAR (Vx_max_tooltip_size);
14185 w->height = XCDR (Vx_max_tooltip_size);
14186 }
14187 else
14188 {
14189 w->width = make_number (80);
14190 w->height = make_number (40);
14191 }
7d0393cf 14192
3cf3436e 14193 f->window_width = XINT (w->width);
6fc2811b
JR
14194 adjust_glyphs (f);
14195 w->pseudo_window_p = 1;
14196
14197 /* Display the tooltip text in a temporary buffer. */
6fc2811b 14198 old_buffer = current_buffer;
3cf3436e
JR
14199 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->buffer));
14200 current_buffer->truncate_lines = Qnil;
6fc2811b
JR
14201 clear_glyph_matrix (w->desired_matrix);
14202 clear_glyph_matrix (w->current_matrix);
14203 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
14204 try_window (FRAME_ROOT_WINDOW (f), pos);
14205
14206 /* Compute width and height of the tooltip. */
14207 width = height = 0;
14208 for (i = 0; i < w->desired_matrix->nrows; ++i)
ee78dc32 14209 {
6fc2811b
JR
14210 struct glyph_row *row = &w->desired_matrix->rows[i];
14211 struct glyph *last;
14212 int row_width;
14213
14214 /* Stop at the first empty row at the end. */
14215 if (!row->enabled_p || !row->displays_text_p)
14216 break;
14217
14218 /* Let the row go over the full width of the frame. */
14219 row->full_width_p = 1;
14220
4e3a1c61
JR
14221#ifdef TODO /* Investigate why some fonts need more width than is
14222 calculated for some tooltips. */
6fc2811b
JR
14223 /* There's a glyph at the end of rows that is use to place
14224 the cursor there. Don't include the width of this glyph. */
14225 if (row->used[TEXT_AREA])
14226 {
14227 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
14228 row_width = row->pixel_width - last->pixel_width;
14229 }
14230 else
4e3a1c61 14231#endif
6fc2811b 14232 row_width = row->pixel_width;
7d0393cf 14233
ca56d953 14234 /* TODO: find why tips do not draw along baseline as instructed. */
bfd6edcc 14235 height += row->height;
6fc2811b 14236 width = max (width, row_width);
ee78dc32
GV
14237 }
14238
6fc2811b
JR
14239 /* Add the frame's internal border to the width and height the X
14240 window should have. */
14241 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
14242 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
ee78dc32 14243
6fc2811b
JR
14244 /* Move the tooltip window where the mouse pointer is. Resize and
14245 show it. */
3cf3436e 14246 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
71eab8d1 14247
bfd6edcc
JR
14248 {
14249 /* Adjust Window size to take border into account. */
14250 RECT rect;
14251 rect.left = rect.top = 0;
14252 rect.right = width;
14253 rect.bottom = height;
14254 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
14255 FRAME_EXTERNAL_MENU_BAR (f));
14256
d65a9cdc 14257 /* Position and size tooltip, and put it in the topmost group. */
bfd6edcc
JR
14258 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
14259 root_x, root_y, rect.right - rect.left,
14260 rect.bottom - rect.top, SWP_NOACTIVATE);
14261
d65a9cdc
JR
14262 /* Ensure tooltip is on top of other topmost windows (eg menus). */
14263 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
14264 0, 0, 0, 0,
14265 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
14266
bfd6edcc
JR
14267 /* Let redisplay know that we have made the frame visible already. */
14268 f->async_visible = 1;
14269
14270 ShowWindow (FRAME_W32_WINDOW (f), SW_SHOWNOACTIVATE);
14271 }
ee78dc32 14272
6fc2811b
JR
14273 /* Draw into the window. */
14274 w->must_be_updated_p = 1;
14275 update_single_window (w, 1);
ee78dc32 14276
0e3fcdef
JR
14277 UNBLOCK_INPUT;
14278
6fc2811b
JR
14279 /* Restore original current buffer. */
14280 set_buffer_internal_1 (old_buffer);
14281 windows_or_buffers_changed = old_windows_or_buffers_changed;
ee78dc32 14282
dc220243 14283 start_timer:
6fc2811b
JR
14284 /* Let the tip disappear after timeout seconds. */
14285 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
14286 intern ("x-hide-tip"));
ee78dc32 14287
dfff8a69 14288 UNGCPRO;
6fc2811b 14289 return unbind_to (count, Qnil);
ee78dc32
GV
14290}
14291
ee78dc32 14292
6fc2811b 14293DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
74e1aeec
JR
14294 doc: /* Hide the current tooltip window, if there is any.
14295Value is t if tooltip was open, nil otherwise. */)
6fc2811b
JR
14296 ()
14297{
937e601e
AI
14298 int count;
14299 Lisp_Object deleted, frame, timer;
14300 struct gcpro gcpro1, gcpro2;
14301
14302 /* Return quickly if nothing to do. */
14303 if (NILP (tip_timer) && NILP (tip_frame))
14304 return Qnil;
7d0393cf 14305
937e601e
AI
14306 frame = tip_frame;
14307 timer = tip_timer;
14308 GCPRO2 (frame, timer);
14309 tip_frame = tip_timer = deleted = Qnil;
7d0393cf 14310
331379bf 14311 count = SPECPDL_INDEX ();
6fc2811b 14312 specbind (Qinhibit_redisplay, Qt);
937e601e 14313 specbind (Qinhibit_quit, Qt);
7d0393cf 14314
937e601e 14315 if (!NILP (timer))
dc220243 14316 call1 (Qcancel_timer, timer);
ee78dc32 14317
937e601e 14318 if (FRAMEP (frame))
6fc2811b 14319 {
937e601e
AI
14320 Fdelete_frame (frame, Qnil);
14321 deleted = Qt;
6fc2811b 14322 }
1edf84e7 14323
937e601e
AI
14324 UNGCPRO;
14325 return unbind_to (count, deleted);
6fc2811b 14326}
5ac45f98 14327
5ac45f98 14328
6fc2811b
JR
14329\f
14330/***********************************************************************
14331 File selection dialog
14332 ***********************************************************************/
6fc2811b
JR
14333extern Lisp_Object Qfile_name_history;
14334
1030b26b
JR
14335/* Callback for altering the behaviour of the Open File dialog.
14336 Makes the Filename text field contain "Current Directory" and be
14337 read-only when "Directories" is selected in the filter. This
14338 allows us to work around the fact that the standard Open File
14339 dialog does not support directories. */
14340UINT CALLBACK
14341file_dialog_callback (hwnd, msg, wParam, lParam)
14342 HWND hwnd;
14343 UINT msg;
14344 WPARAM wParam;
14345 LPARAM lParam;
14346{
14347 if (msg == WM_NOTIFY)
14348 {
14349 OFNOTIFY * notify = (OFNOTIFY *)lParam;
14350 /* Detect when the Filter dropdown is changed. */
14351 if (notify->hdr.code == CDN_TYPECHANGE)
14352 {
14353 HWND dialog = GetParent (hwnd);
14354 HWND edit_control = GetDlgItem (dialog, FILE_NAME_TEXT_FIELD);
14355
14356 /* Directories is in index 2. */
14357 if (notify->lpOFN->nFilterIndex == 2)
14358 {
14359 CommDlg_OpenSave_SetControlText (dialog, FILE_NAME_TEXT_FIELD,
14360 "Current Directory");
14361 EnableWindow (edit_control, FALSE);
14362 }
14363 else
14364 {
14365 CommDlg_OpenSave_SetControlText (dialog, FILE_NAME_TEXT_FIELD,
14366 "");
14367 EnableWindow (edit_control, TRUE);
14368 }
14369 }
14370 }
14371 return 0;
14372}
14373
6fc2811b 14374DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 4, 0,
74e1aeec
JR
14375 doc: /* Read file name, prompting with PROMPT in directory DIR.
14376Use a file selection dialog.
14377Select DEFAULT-FILENAME in the dialog's file selection box, if
14378specified. Ensure that file exists if MUSTMATCH is non-nil. */)
6fc2811b
JR
14379 (prompt, dir, default_filename, mustmatch)
14380 Lisp_Object prompt, dir, default_filename, mustmatch;
14381{
14382 struct frame *f = SELECTED_FRAME ();
14383 Lisp_Object file = Qnil;
aed13378 14384 int count = SPECPDL_INDEX ();
6fc2811b
JR
14385 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
14386 char filename[MAX_PATH + 1];
14387 char init_dir[MAX_PATH + 1];
6fc2811b
JR
14388
14389 GCPRO5 (prompt, dir, default_filename, mustmatch, file);
b7826503
PJ
14390 CHECK_STRING (prompt);
14391 CHECK_STRING (dir);
6fc2811b
JR
14392
14393 /* Create the dialog with PROMPT as title, using DIR as initial
14394 directory and using "*" as pattern. */
14395 dir = Fexpand_file_name (dir, Qnil);
d5db4077 14396 strncpy (init_dir, SDATA (dir), MAX_PATH);
6fc2811b
JR
14397 init_dir[MAX_PATH] = '\0';
14398 unixtodos_filename (init_dir);
14399
14400 if (STRINGP (default_filename))
14401 {
14402 char *file_name_only;
d5db4077 14403 char *full_path_name = SDATA (default_filename);
5ac45f98 14404
6fc2811b 14405 unixtodos_filename (full_path_name);
5ac45f98 14406
6fc2811b
JR
14407 file_name_only = strrchr (full_path_name, '\\');
14408 if (!file_name_only)
14409 file_name_only = full_path_name;
14410 else
14411 {
14412 file_name_only++;
6fc2811b 14413 }
ee78dc32 14414
6fc2811b
JR
14415 strncpy (filename, file_name_only, MAX_PATH);
14416 filename[MAX_PATH] = '\0';
14417 }
ee78dc32 14418 else
6fc2811b 14419 filename[0] = '\0';
ee78dc32 14420
1030b26b
JR
14421 {
14422 OPENFILENAME file_details;
5ac45f98 14423
1030b26b
JR
14424 /* Prevent redisplay. */
14425 specbind (Qinhibit_redisplay, Qt);
14426 BLOCK_INPUT;
ee78dc32 14427
1030b26b
JR
14428 bzero (&file_details, sizeof (file_details));
14429 file_details.lStructSize = sizeof (file_details);
14430 file_details.hwndOwner = FRAME_W32_WINDOW (f);
14431 /* Undocumented Bug in Common File Dialog:
14432 If a filter is not specified, shell links are not resolved. */
14433 file_details.lpstrFilter = "All Files (*.*)\0*.*\0Directories\0*|*\0\0";
14434 file_details.lpstrFile = filename;
14435 file_details.nMaxFile = sizeof (filename);
14436 file_details.lpstrInitialDir = init_dir;
d5db4077 14437 file_details.lpstrTitle = SDATA (prompt);
1030b26b
JR
14438 file_details.Flags = (OFN_HIDEREADONLY | OFN_NOCHANGEDIR
14439 | OFN_EXPLORER | OFN_ENABLEHOOK);
14440 if (!NILP (mustmatch))
14441 file_details.Flags |= OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
14442
14443 file_details.lpfnHook = (LPOFNHOOKPROC) file_dialog_callback;
14444
14445 if (GetOpenFileName (&file_details))
14446 {
14447 dostounix_filename (filename);
14448 if (file_details.nFilterIndex == 2)
14449 {
14450 /* "Folder Only" selected - strip dummy file name. */
14451 char * last = strrchr (filename, '/');
14452 *last = '\0';
14453 }
6fc2811b 14454
1030b26b
JR
14455 file = DECODE_FILE(build_string (filename));
14456 }
14457 /* User cancelled the dialog without making a selection. */
14458 else if (!CommDlgExtendedError ())
14459 file = Qnil;
14460 /* An error occurred, fallback on reading from the mini-buffer. */
14461 else
14462 file = Fcompleting_read (prompt, intern ("read-file-name-internal"),
14463 dir, mustmatch, dir, Qfile_name_history,
14464 default_filename, Qnil);
14465
14466 UNBLOCK_INPUT;
14467 file = unbind_to (count, file);
14468 }
ee78dc32 14469
6fc2811b 14470 UNGCPRO;
1edf84e7 14471
6fc2811b
JR
14472 /* Make "Cancel" equivalent to C-g. */
14473 if (NILP (file))
14474 Fsignal (Qquit, Qnil);
ee78dc32 14475
dfff8a69 14476 return unbind_to (count, file);
6fc2811b 14477}
ee78dc32 14478
ee78dc32 14479
6fc2811b 14480\f
6fc2811b
JR
14481/***********************************************************************
14482 w32 specialized functions
14483 ***********************************************************************/
ee78dc32 14484
d84b082d 14485DEFUN ("w32-select-font", Fw32_select_font, Sw32_select_font, 0, 2, 0,
74e1aeec
JR
14486 doc: /* Select a font using the W32 font dialog.
14487Returns an X font string corresponding to the selection. */)
d84b082d
JR
14488 (frame, include_proportional)
14489 Lisp_Object frame, include_proportional;
ee78dc32
GV
14490{
14491 FRAME_PTR f = check_x_frame (frame);
14492 CHOOSEFONT cf;
14493 LOGFONT lf;
f46e6225
GV
14494 TEXTMETRIC tm;
14495 HDC hdc;
14496 HANDLE oldobj;
ee78dc32
GV
14497 char buf[100];
14498
14499 bzero (&cf, sizeof (cf));
f46e6225 14500 bzero (&lf, sizeof (lf));
ee78dc32
GV
14501
14502 cf.lStructSize = sizeof (cf);
fbd6baed 14503 cf.hwndOwner = FRAME_W32_WINDOW (f);
d84b082d
JR
14504 cf.Flags = CF_FORCEFONTEXIST | CF_SCREENFONTS | CF_NOVERTFONTS;
14505
14506 /* Unless include_proportional is non-nil, limit the selection to
14507 monospaced fonts. */
14508 if (NILP (include_proportional))
14509 cf.Flags |= CF_FIXEDPITCHONLY;
14510
ee78dc32
GV
14511 cf.lpLogFont = &lf;
14512
f46e6225
GV
14513 /* Initialize as much of the font details as we can from the current
14514 default font. */
14515 hdc = GetDC (FRAME_W32_WINDOW (f));
14516 oldobj = SelectObject (hdc, FRAME_FONT (f)->hfont);
14517 GetTextFace (hdc, LF_FACESIZE, lf.lfFaceName);
14518 if (GetTextMetrics (hdc, &tm))
14519 {
14520 lf.lfHeight = tm.tmInternalLeading - tm.tmHeight;
14521 lf.lfWeight = tm.tmWeight;
14522 lf.lfItalic = tm.tmItalic;
14523 lf.lfUnderline = tm.tmUnderlined;
14524 lf.lfStrikeOut = tm.tmStruckOut;
f46e6225
GV
14525 lf.lfCharSet = tm.tmCharSet;
14526 cf.Flags |= CF_INITTOLOGFONTSTRUCT;
14527 }
14528 SelectObject (hdc, oldobj);
6fc2811b 14529 ReleaseDC (FRAME_W32_WINDOW (f), hdc);
f46e6225 14530
767b1ff0 14531 if (!ChooseFont (&cf) || !w32_to_x_font (&lf, buf, 100, NULL))
3c190163 14532 return Qnil;
ee78dc32
GV
14533
14534 return build_string (buf);
14535}
14536
74e1aeec
JR
14537DEFUN ("w32-send-sys-command", Fw32_send_sys_command,
14538 Sw32_send_sys_command, 1, 2, 0,
14539 doc: /* Send frame a Windows WM_SYSCOMMAND message of type COMMAND.
d84b082d
JR
14540Some useful values for command are #xf030 to maximise frame (#xf020
14541to minimize), #xf120 to restore frame to original size, and #xf100
14542to activate the menubar for keyboard access. #xf140 activates the
74e1aeec
JR
14543screen saver if defined.
14544
14545If optional parameter FRAME is not specified, use selected frame. */)
1edf84e7
GV
14546 (command, frame)
14547 Lisp_Object command, frame;
14548{
1edf84e7
GV
14549 FRAME_PTR f = check_x_frame (frame);
14550
b7826503 14551 CHECK_NUMBER (command);
1edf84e7 14552
ce6059da 14553 PostMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, XINT (command), 0);
1edf84e7
GV
14554
14555 return Qnil;
14556}
14557
55dcfc15 14558DEFUN ("w32-shell-execute", Fw32_shell_execute, Sw32_shell_execute, 2, 4, 0,
74e1aeec
JR
14559 doc: /* Get Windows to perform OPERATION on DOCUMENT.
14560This is a wrapper around the ShellExecute system function, which
14561invokes the application registered to handle OPERATION for DOCUMENT.
14562OPERATION is typically \"open\", \"print\" or \"explore\" (but can be
14563nil for the default action), and DOCUMENT is typically the name of a
14564document file or URL, but can also be a program executable to run or
14565a directory to open in the Windows Explorer.
14566
14567If DOCUMENT is a program executable, PARAMETERS can be a string
14568containing command line parameters, but otherwise should be nil.
14569
14570SHOW-FLAG can be used to control whether the invoked application is hidden
14571or minimized. If SHOW-FLAG is nil, the application is displayed normally,
14572otherwise it is an integer representing a ShowWindow flag:
14573
14574 0 - start hidden
14575 1 - start normally
14576 3 - start maximized
14577 6 - start minimized */)
55dcfc15
AI
14578 (operation, document, parameters, show_flag)
14579 Lisp_Object operation, document, parameters, show_flag;
14580{
14581 Lisp_Object current_dir;
14582
b7826503 14583 CHECK_STRING (document);
55dcfc15
AI
14584
14585 /* Encode filename and current directory. */
14586 current_dir = ENCODE_FILE (current_buffer->directory);
14587 document = ENCODE_FILE (document);
14588 if ((int) ShellExecute (NULL,
6fc2811b 14589 (STRINGP (operation) ?
d5db4077
KR
14590 SDATA (operation) : NULL),
14591 SDATA (document),
55dcfc15 14592 (STRINGP (parameters) ?
d5db4077
KR
14593 SDATA (parameters) : NULL),
14594 SDATA (current_dir),
55dcfc15
AI
14595 (INTEGERP (show_flag) ?
14596 XINT (show_flag) : SW_SHOWDEFAULT))
14597 > 32)
14598 return Qt;
90d97e64 14599 error ("ShellExecute failed: %s", w32_strerror (0));
55dcfc15
AI
14600}
14601
ccc2d29c
GV
14602/* Lookup virtual keycode from string representing the name of a
14603 non-ascii keystroke into the corresponding virtual key, using
14604 lispy_function_keys. */
14605static int
14606lookup_vk_code (char *key)
14607{
14608 int i;
14609
14610 for (i = 0; i < 256; i++)
14611 if (lispy_function_keys[i] != 0
14612 && strcmp (lispy_function_keys[i], key) == 0)
14613 return i;
14614
14615 return -1;
14616}
14617
14618/* Convert a one-element vector style key sequence to a hot key
14619 definition. */
14620static int
14621w32_parse_hot_key (key)
14622 Lisp_Object key;
14623{
14624 /* Copied from Fdefine_key and store_in_keymap. */
14625 register Lisp_Object c;
14626 int vk_code;
14627 int lisp_modifiers;
14628 int w32_modifiers;
14629 struct gcpro gcpro1;
14630
b7826503 14631 CHECK_VECTOR (key);
ccc2d29c
GV
14632
14633 if (XFASTINT (Flength (key)) != 1)
14634 return Qnil;
14635
14636 GCPRO1 (key);
14637
14638 c = Faref (key, make_number (0));
14639
14640 if (CONSP (c) && lucid_event_type_list_p (c))
14641 c = Fevent_convert_list (c);
14642
14643 UNGCPRO;
14644
14645 if (! INTEGERP (c) && ! SYMBOLP (c))
14646 error ("Key definition is invalid");
14647
14648 /* Work out the base key and the modifiers. */
14649 if (SYMBOLP (c))
14650 {
14651 c = parse_modifiers (c);
14652 lisp_modifiers = Fcar (Fcdr (c));
14653 c = Fcar (c);
14654 if (!SYMBOLP (c))
14655 abort ();
d5db4077 14656 vk_code = lookup_vk_code (SDATA (SYMBOL_NAME (c)));
ccc2d29c
GV
14657 }
14658 else if (INTEGERP (c))
14659 {
14660 lisp_modifiers = XINT (c) & ~CHARACTERBITS;
14661 /* Many ascii characters are their own virtual key code. */
14662 vk_code = XINT (c) & CHARACTERBITS;
14663 }
14664
14665 if (vk_code < 0 || vk_code > 255)
14666 return Qnil;
14667
14668 if ((lisp_modifiers & meta_modifier) != 0
14669 && !NILP (Vw32_alt_is_meta))
14670 lisp_modifiers |= alt_modifier;
14671
71eab8d1
AI
14672 /* Supply defs missing from mingw32. */
14673#ifndef MOD_ALT
14674#define MOD_ALT 0x0001
14675#define MOD_CONTROL 0x0002
14676#define MOD_SHIFT 0x0004
14677#define MOD_WIN 0x0008
14678#endif
14679
ccc2d29c
GV
14680 /* Convert lisp modifiers to Windows hot-key form. */
14681 w32_modifiers = (lisp_modifiers & hyper_modifier) ? MOD_WIN : 0;
14682 w32_modifiers |= (lisp_modifiers & alt_modifier) ? MOD_ALT : 0;
14683 w32_modifiers |= (lisp_modifiers & ctrl_modifier) ? MOD_CONTROL : 0;
14684 w32_modifiers |= (lisp_modifiers & shift_modifier) ? MOD_SHIFT : 0;
14685
14686 return HOTKEY (vk_code, w32_modifiers);
14687}
14688
74e1aeec
JR
14689DEFUN ("w32-register-hot-key", Fw32_register_hot_key,
14690 Sw32_register_hot_key, 1, 1, 0,
14691 doc: /* Register KEY as a hot-key combination.
14692Certain key combinations like Alt-Tab are reserved for system use on
14693Windows, and therefore are normally intercepted by the system. However,
14694most of these key combinations can be received by registering them as
14695hot-keys, overriding their special meaning.
14696
14697KEY must be a one element key definition in vector form that would be
14698acceptable to `define-key' (e.g. [A-tab] for Alt-Tab). The meta
14699modifier is interpreted as Alt if `w32-alt-is-meta' is t, and hyper
14700is always interpreted as the Windows modifier keys.
14701
14702The return value is the hotkey-id if registered, otherwise nil. */)
ccc2d29c
GV
14703 (key)
14704 Lisp_Object key;
14705{
14706 key = w32_parse_hot_key (key);
14707
14708 if (NILP (Fmemq (key, w32_grabbed_keys)))
14709 {
14710 /* Reuse an empty slot if possible. */
14711 Lisp_Object item = Fmemq (Qnil, w32_grabbed_keys);
14712
14713 /* Safe to add new key to list, even if we have focus. */
14714 if (NILP (item))
14715 w32_grabbed_keys = Fcons (key, w32_grabbed_keys);
14716 else
f3fbd155 14717 XSETCAR (item, key);
ccc2d29c
GV
14718
14719 /* Notify input thread about new hot-key definition, so that it
14720 takes effect without needing to switch focus. */
14721 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
14722 (WPARAM) key, 0);
14723 }
14724
14725 return key;
14726}
14727
74e1aeec
JR
14728DEFUN ("w32-unregister-hot-key", Fw32_unregister_hot_key,
14729 Sw32_unregister_hot_key, 1, 1, 0,
14730 doc: /* Unregister HOTKEY as a hot-key combination. */)
ccc2d29c
GV
14731 (key)
14732 Lisp_Object key;
14733{
14734 Lisp_Object item;
14735
14736 if (!INTEGERP (key))
14737 key = w32_parse_hot_key (key);
14738
14739 item = Fmemq (key, w32_grabbed_keys);
14740
14741 if (!NILP (item))
14742 {
14743 /* Notify input thread about hot-key definition being removed, so
14744 that it takes effect without needing focus switch. */
14745 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
14746 (WPARAM) XINT (XCAR (item)), (LPARAM) item))
14747 {
14748 MSG msg;
14749 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
14750 }
14751 return Qt;
14752 }
14753 return Qnil;
14754}
14755
74e1aeec
JR
14756DEFUN ("w32-registered-hot-keys", Fw32_registered_hot_keys,
14757 Sw32_registered_hot_keys, 0, 0, 0,
14758 doc: /* Return list of registered hot-key IDs. */)
ccc2d29c
GV
14759 ()
14760{
14761 return Fcopy_sequence (w32_grabbed_keys);
14762}
14763
74e1aeec
JR
14764DEFUN ("w32-reconstruct-hot-key", Fw32_reconstruct_hot_key,
14765 Sw32_reconstruct_hot_key, 1, 1, 0,
14766 doc: /* Convert hot-key ID to a lisp key combination. */)
ccc2d29c
GV
14767 (hotkeyid)
14768 Lisp_Object hotkeyid;
14769{
14770 int vk_code, w32_modifiers;
14771 Lisp_Object key;
14772
b7826503 14773 CHECK_NUMBER (hotkeyid);
ccc2d29c
GV
14774
14775 vk_code = HOTKEY_VK_CODE (hotkeyid);
14776 w32_modifiers = HOTKEY_MODIFIERS (hotkeyid);
14777
14778 if (lispy_function_keys[vk_code])
14779 key = intern (lispy_function_keys[vk_code]);
14780 else
14781 key = make_number (vk_code);
14782
14783 key = Fcons (key, Qnil);
14784 if (w32_modifiers & MOD_SHIFT)
3ef68e6b 14785 key = Fcons (Qshift, key);
ccc2d29c 14786 if (w32_modifiers & MOD_CONTROL)
3ef68e6b 14787 key = Fcons (Qctrl, key);
ccc2d29c 14788 if (w32_modifiers & MOD_ALT)
3ef68e6b 14789 key = Fcons (NILP (Vw32_alt_is_meta) ? Qalt : Qmeta, key);
ccc2d29c 14790 if (w32_modifiers & MOD_WIN)
3ef68e6b 14791 key = Fcons (Qhyper, key);
ccc2d29c
GV
14792
14793 return key;
14794}
adcc3809 14795
74e1aeec
JR
14796DEFUN ("w32-toggle-lock-key", Fw32_toggle_lock_key,
14797 Sw32_toggle_lock_key, 1, 2, 0,
14798 doc: /* Toggle the state of the lock key KEY.
14799KEY can be `capslock', `kp-numlock', or `scroll'.
14800If the optional parameter NEW-STATE is a number, then the state of KEY
14801is set to off if the low bit of NEW-STATE is zero, otherwise on. */)
adcc3809
GV
14802 (key, new_state)
14803 Lisp_Object key, new_state;
14804{
14805 int vk_code;
adcc3809
GV
14806
14807 if (EQ (key, intern ("capslock")))
14808 vk_code = VK_CAPITAL;
14809 else if (EQ (key, intern ("kp-numlock")))
14810 vk_code = VK_NUMLOCK;
14811 else if (EQ (key, intern ("scroll")))
14812 vk_code = VK_SCROLL;
14813 else
14814 return Qnil;
14815
14816 if (!dwWindowsThreadId)
14817 return make_number (w32_console_toggle_lock_key (vk_code, new_state));
14818
14819 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
14820 (WPARAM) vk_code, (LPARAM) new_state))
14821 {
14822 MSG msg;
14823 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
14824 return make_number (msg.wParam);
14825 }
14826 return Qnil;
14827}
ee78dc32 14828\f
2254bcde 14829DEFUN ("file-system-info", Ffile_system_info, Sfile_system_info, 1, 1, 0,
74e1aeec
JR
14830 doc: /* Return storage information about the file system FILENAME is on.
14831Value is a list of floats (TOTAL FREE AVAIL), where TOTAL is the total
14832storage of the file system, FREE is the free storage, and AVAIL is the
14833storage available to a non-superuser. All 3 numbers are in bytes.
14834If the underlying system call fails, value is nil. */)
2254bcde
AI
14835 (filename)
14836 Lisp_Object filename;
14837{
14838 Lisp_Object encoded, value;
14839
b7826503 14840 CHECK_STRING (filename);
2254bcde
AI
14841 filename = Fexpand_file_name (filename, Qnil);
14842 encoded = ENCODE_FILE (filename);
14843
14844 value = Qnil;
14845
14846 /* Determining the required information on Windows turns out, sadly,
14847 to be more involved than one would hope. The original Win32 api
14848 call for this will return bogus information on some systems, but we
14849 must dynamically probe for the replacement api, since that was
14850 added rather late on. */
14851 {
14852 HMODULE hKernel = GetModuleHandle ("kernel32");
14853 BOOL (*pfn_GetDiskFreeSpaceEx)
14854 (char *, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER)
14855 = (void *) GetProcAddress (hKernel, "GetDiskFreeSpaceEx");
14856
14857 /* On Windows, we may need to specify the root directory of the
14858 volume holding FILENAME. */
14859 char rootname[MAX_PATH];
d5db4077 14860 char *name = SDATA (encoded);
2254bcde
AI
14861
14862 /* find the root name of the volume if given */
14863 if (isalpha (name[0]) && name[1] == ':')
14864 {
14865 rootname[0] = name[0];
14866 rootname[1] = name[1];
14867 rootname[2] = '\\';
14868 rootname[3] = 0;
14869 }
14870 else if (IS_DIRECTORY_SEP (name[0]) && IS_DIRECTORY_SEP (name[1]))
14871 {
14872 char *str = rootname;
14873 int slashes = 4;
14874 do
14875 {
14876 if (IS_DIRECTORY_SEP (*name) && --slashes == 0)
14877 break;
14878 *str++ = *name++;
14879 }
14880 while ( *name );
14881
14882 *str++ = '\\';
14883 *str = 0;
14884 }
14885
14886 if (pfn_GetDiskFreeSpaceEx)
14887 {
ac849ba4
JR
14888 /* Unsigned large integers cannot be cast to double, so
14889 use signed ones instead. */
2254bcde
AI
14890 LARGE_INTEGER availbytes;
14891 LARGE_INTEGER freebytes;
14892 LARGE_INTEGER totalbytes;
14893
14894 if (pfn_GetDiskFreeSpaceEx(rootname,
ac849ba4
JR
14895 (ULARGE_INTEGER *)&availbytes,
14896 (ULARGE_INTEGER *)&totalbytes,
14897 (ULARGE_INTEGER *)&freebytes))
2254bcde
AI
14898 value = list3 (make_float ((double) totalbytes.QuadPart),
14899 make_float ((double) freebytes.QuadPart),
14900 make_float ((double) availbytes.QuadPart));
14901 }
14902 else
14903 {
14904 DWORD sectors_per_cluster;
14905 DWORD bytes_per_sector;
14906 DWORD free_clusters;
14907 DWORD total_clusters;
14908
14909 if (GetDiskFreeSpace(rootname,
14910 &sectors_per_cluster,
14911 &bytes_per_sector,
14912 &free_clusters,
14913 &total_clusters))
14914 value = list3 (make_float ((double) total_clusters
14915 * sectors_per_cluster * bytes_per_sector),
14916 make_float ((double) free_clusters
14917 * sectors_per_cluster * bytes_per_sector),
14918 make_float ((double) free_clusters
14919 * sectors_per_cluster * bytes_per_sector));
14920 }
14921 }
14922
14923 return value;
14924}
14925\f
0e3fcdef
JR
14926/***********************************************************************
14927 Initialization
14928 ***********************************************************************/
14929
14930void
fbd6baed 14931syms_of_w32fns ()
ee78dc32 14932{
9785d95b
BK
14933 globals_of_w32fns ();
14934 /* This is zero if not using MS-Windows. */
1edf84e7 14935 w32_in_use = 0;
9eb16b62
JR
14936 track_mouse_window = NULL;
14937
d285988b
JR
14938 w32_visible_system_caret_hwnd = NULL;
14939
ee78dc32
GV
14940 Qauto_raise = intern ("auto-raise");
14941 staticpro (&Qauto_raise);
14942 Qauto_lower = intern ("auto-lower");
14943 staticpro (&Qauto_lower);
ee78dc32
GV
14944 Qborder_color = intern ("border-color");
14945 staticpro (&Qborder_color);
14946 Qborder_width = intern ("border-width");
14947 staticpro (&Qborder_width);
ee78dc32
GV
14948 Qcursor_color = intern ("cursor-color");
14949 staticpro (&Qcursor_color);
14950 Qcursor_type = intern ("cursor-type");
14951 staticpro (&Qcursor_type);
ee78dc32
GV
14952 Qgeometry = intern ("geometry");
14953 staticpro (&Qgeometry);
14954 Qicon_left = intern ("icon-left");
14955 staticpro (&Qicon_left);
14956 Qicon_top = intern ("icon-top");
14957 staticpro (&Qicon_top);
14958 Qicon_type = intern ("icon-type");
14959 staticpro (&Qicon_type);
14960 Qicon_name = intern ("icon-name");
14961 staticpro (&Qicon_name);
14962 Qinternal_border_width = intern ("internal-border-width");
14963 staticpro (&Qinternal_border_width);
14964 Qleft = intern ("left");
14965 staticpro (&Qleft);
1026b400
RS
14966 Qright = intern ("right");
14967 staticpro (&Qright);
ee78dc32
GV
14968 Qmouse_color = intern ("mouse-color");
14969 staticpro (&Qmouse_color);
14970 Qnone = intern ("none");
14971 staticpro (&Qnone);
14972 Qparent_id = intern ("parent-id");
14973 staticpro (&Qparent_id);
14974 Qscroll_bar_width = intern ("scroll-bar-width");
14975 staticpro (&Qscroll_bar_width);
14976 Qsuppress_icon = intern ("suppress-icon");
14977 staticpro (&Qsuppress_icon);
ee78dc32
GV
14978 Qundefined_color = intern ("undefined-color");
14979 staticpro (&Qundefined_color);
14980 Qvertical_scroll_bars = intern ("vertical-scroll-bars");
14981 staticpro (&Qvertical_scroll_bars);
14982 Qvisibility = intern ("visibility");
14983 staticpro (&Qvisibility);
14984 Qwindow_id = intern ("window-id");
14985 staticpro (&Qwindow_id);
14986 Qx_frame_parameter = intern ("x-frame-parameter");
14987 staticpro (&Qx_frame_parameter);
14988 Qx_resource_name = intern ("x-resource-name");
14989 staticpro (&Qx_resource_name);
14990 Quser_position = intern ("user-position");
14991 staticpro (&Quser_position);
14992 Quser_size = intern ("user-size");
14993 staticpro (&Quser_size);
6fc2811b
JR
14994 Qscreen_gamma = intern ("screen-gamma");
14995 staticpro (&Qscreen_gamma);
dfff8a69
JR
14996 Qline_spacing = intern ("line-spacing");
14997 staticpro (&Qline_spacing);
14998 Qcenter = intern ("center");
14999 staticpro (&Qcenter);
dc220243
JR
15000 Qcancel_timer = intern ("cancel-timer");
15001 staticpro (&Qcancel_timer);
f7b9d4d1
JR
15002 Qfullscreen = intern ("fullscreen");
15003 staticpro (&Qfullscreen);
15004 Qfullwidth = intern ("fullwidth");
15005 staticpro (&Qfullwidth);
15006 Qfullheight = intern ("fullheight");
15007 staticpro (&Qfullheight);
15008 Qfullboth = intern ("fullboth");
15009 staticpro (&Qfullboth);
ee78dc32 15010
adcc3809
GV
15011 Qhyper = intern ("hyper");
15012 staticpro (&Qhyper);
15013 Qsuper = intern ("super");
15014 staticpro (&Qsuper);
15015 Qmeta = intern ("meta");
15016 staticpro (&Qmeta);
15017 Qalt = intern ("alt");
15018 staticpro (&Qalt);
15019 Qctrl = intern ("ctrl");
15020 staticpro (&Qctrl);
15021 Qcontrol = intern ("control");
15022 staticpro (&Qcontrol);
15023 Qshift = intern ("shift");
15024 staticpro (&Qshift);
f7b9d4d1 15025 /* This is the end of symbol initialization. */
adcc3809 15026
6fc2811b
JR
15027 /* Text property `display' should be nonsticky by default. */
15028 Vtext_property_default_nonsticky
15029 = Fcons (Fcons (Qdisplay, Qt), Vtext_property_default_nonsticky);
15030
15031
15032 Qlaplace = intern ("laplace");
15033 staticpro (&Qlaplace);
3cf3436e
JR
15034 Qemboss = intern ("emboss");
15035 staticpro (&Qemboss);
15036 Qedge_detection = intern ("edge-detection");
15037 staticpro (&Qedge_detection);
15038 Qheuristic = intern ("heuristic");
15039 staticpro (&Qheuristic);
15040 QCmatrix = intern (":matrix");
15041 staticpro (&QCmatrix);
15042 QCcolor_adjustment = intern (":color-adjustment");
15043 staticpro (&QCcolor_adjustment);
15044 QCmask = intern (":mask");
15045 staticpro (&QCmask);
6fc2811b 15046
4b817373
RS
15047 Qface_set_after_frame_default = intern ("face-set-after-frame-default");
15048 staticpro (&Qface_set_after_frame_default);
15049
ee78dc32
GV
15050 Fput (Qundefined_color, Qerror_conditions,
15051 Fcons (Qundefined_color, Fcons (Qerror, Qnil)));
15052 Fput (Qundefined_color, Qerror_message,
15053 build_string ("Undefined color"));
15054
ccc2d29c
GV
15055 staticpro (&w32_grabbed_keys);
15056 w32_grabbed_keys = Qnil;
15057
fbd6baed 15058 DEFVAR_LISP ("w32-color-map", &Vw32_color_map,
74e1aeec 15059 doc: /* An array of color name mappings for windows. */);
fbd6baed 15060 Vw32_color_map = Qnil;
ee78dc32 15061
fbd6baed 15062 DEFVAR_LISP ("w32-pass-alt-to-system", &Vw32_pass_alt_to_system,
74e1aeec
JR
15063 doc: /* Non-nil if alt key presses are passed on to Windows.
15064When non-nil, for example, alt pressed and released and then space will
15065open the System menu. When nil, Emacs silently swallows alt key events. */);
fbd6baed 15066 Vw32_pass_alt_to_system = Qnil;
da36a4d6 15067
fbd6baed 15068 DEFVAR_LISP ("w32-alt-is-meta", &Vw32_alt_is_meta,
74e1aeec
JR
15069 doc: /* Non-nil if the alt key is to be considered the same as the meta key.
15070When nil, Emacs will translate the alt key to the Alt modifier, and not Meta. */);
fbd6baed 15071 Vw32_alt_is_meta = Qt;
8c205c63 15072
7d081355 15073 DEFVAR_INT ("w32-quit-key", &Vw32_quit_key,
74e1aeec 15074 doc: /* If non-zero, the virtual key code for an alternative quit key. */);
7d081355
AI
15075 XSETINT (Vw32_quit_key, 0);
15076
7d0393cf 15077 DEFVAR_LISP ("w32-pass-lwindow-to-system",
ccc2d29c 15078 &Vw32_pass_lwindow_to_system,
74e1aeec
JR
15079 doc: /* Non-nil if the left \"Windows\" key is passed on to Windows.
15080When non-nil, the Start menu is opened by tapping the key. */);
ccc2d29c
GV
15081 Vw32_pass_lwindow_to_system = Qt;
15082
7d0393cf 15083 DEFVAR_LISP ("w32-pass-rwindow-to-system",
ccc2d29c 15084 &Vw32_pass_rwindow_to_system,
74e1aeec
JR
15085 doc: /* Non-nil if the right \"Windows\" key is passed on to Windows.
15086When non-nil, the Start menu is opened by tapping the key. */);
ccc2d29c
GV
15087 Vw32_pass_rwindow_to_system = Qt;
15088
adcc3809
GV
15089 DEFVAR_INT ("w32-phantom-key-code",
15090 &Vw32_phantom_key_code,
74e1aeec
JR
15091 doc: /* Virtual key code used to generate \"phantom\" key presses.
15092Value is a number between 0 and 255.
15093
15094Phantom key presses are generated in order to stop the system from
15095acting on \"Windows\" key events when `w32-pass-lwindow-to-system' or
15096`w32-pass-rwindow-to-system' is nil. */);
ce6059da
AI
15097 /* Although 255 is technically not a valid key code, it works and
15098 means that this hack won't interfere with any real key code. */
15099 Vw32_phantom_key_code = 255;
adcc3809 15100
7d0393cf 15101 DEFVAR_LISP ("w32-enable-num-lock",
ccc2d29c 15102 &Vw32_enable_num_lock,
74e1aeec
JR
15103 doc: /* Non-nil if Num Lock should act normally.
15104Set to nil to see Num Lock as the key `kp-numlock'. */);
ccc2d29c
GV
15105 Vw32_enable_num_lock = Qt;
15106
7d0393cf 15107 DEFVAR_LISP ("w32-enable-caps-lock",
ccc2d29c 15108 &Vw32_enable_caps_lock,
74e1aeec
JR
15109 doc: /* Non-nil if Caps Lock should act normally.
15110Set to nil to see Caps Lock as the key `capslock'. */);
ccc2d29c
GV
15111 Vw32_enable_caps_lock = Qt;
15112
15113 DEFVAR_LISP ("w32-scroll-lock-modifier",
15114 &Vw32_scroll_lock_modifier,
74e1aeec
JR
15115 doc: /* Modifier to use for the Scroll Lock on state.
15116The value can be hyper, super, meta, alt, control or shift for the
15117respective modifier, or nil to see Scroll Lock as the key `scroll'.
15118Any other value will cause the key to be ignored. */);
ccc2d29c
GV
15119 Vw32_scroll_lock_modifier = Qt;
15120
15121 DEFVAR_LISP ("w32-lwindow-modifier",
15122 &Vw32_lwindow_modifier,
74e1aeec
JR
15123 doc: /* Modifier to use for the left \"Windows\" key.
15124The value can be hyper, super, meta, alt, control or shift for the
15125respective modifier, or nil to appear as the key `lwindow'.
15126Any other value will cause the key to be ignored. */);
ccc2d29c
GV
15127 Vw32_lwindow_modifier = Qnil;
15128
15129 DEFVAR_LISP ("w32-rwindow-modifier",
15130 &Vw32_rwindow_modifier,
74e1aeec
JR
15131 doc: /* Modifier to use for the right \"Windows\" key.
15132The value can be hyper, super, meta, alt, control or shift for the
15133respective modifier, or nil to appear as the key `rwindow'.
15134Any other value will cause the key to be ignored. */);
ccc2d29c
GV
15135 Vw32_rwindow_modifier = Qnil;
15136
15137 DEFVAR_LISP ("w32-apps-modifier",
15138 &Vw32_apps_modifier,
74e1aeec
JR
15139 doc: /* Modifier to use for the \"Apps\" key.
15140The value can be hyper, super, meta, alt, control or shift for the
15141respective modifier, or nil to appear as the key `apps'.
15142Any other value will cause the key to be ignored. */);
ccc2d29c 15143 Vw32_apps_modifier = Qnil;
da36a4d6 15144
d84b082d 15145 DEFVAR_BOOL ("w32-enable-synthesized-fonts", &w32_enable_synthesized_fonts,
74e1aeec 15146 doc: /* Non-nil enables selection of artificially italicized and bold fonts. */);
d84b082d 15147 w32_enable_synthesized_fonts = 0;
5ac45f98 15148
fbd6baed 15149 DEFVAR_LISP ("w32-enable-palette", &Vw32_enable_palette,
74e1aeec 15150 doc: /* Non-nil enables Windows palette management to map colors exactly. */);
fbd6baed 15151 Vw32_enable_palette = Qt;
5ac45f98 15152
fbd6baed
GV
15153 DEFVAR_INT ("w32-mouse-button-tolerance",
15154 &Vw32_mouse_button_tolerance,
74e1aeec
JR
15155 doc: /* Analogue of double click interval for faking middle mouse events.
15156The value is the minimum time in milliseconds that must elapse between
15157left/right button down events before they are considered distinct events.
15158If both mouse buttons are depressed within this interval, a middle mouse
15159button down event is generated instead. */);
fbd6baed 15160 XSETINT (Vw32_mouse_button_tolerance, GetDoubleClickTime () / 2);
5ac45f98 15161
fbd6baed
GV
15162 DEFVAR_INT ("w32-mouse-move-interval",
15163 &Vw32_mouse_move_interval,
74e1aeec
JR
15164 doc: /* Minimum interval between mouse move events.
15165The value is the minimum time in milliseconds that must elapse between
15166successive mouse move (or scroll bar drag) events before they are
15167reported as lisp events. */);
247be837 15168 XSETINT (Vw32_mouse_move_interval, 0);
84fb1139 15169
74214547
JR
15170 DEFVAR_BOOL ("w32-pass-extra-mouse-buttons-to-system",
15171 &w32_pass_extra_mouse_buttons_to_system,
15172 doc: /* Non-nil if the fourth and fifth mouse buttons are passed to Windows.
15173Recent versions of Windows support mice with up to five buttons.
15174Since most applications don't support these extra buttons, most mouse
15175drivers will allow you to map them to functions at the system level.
15176If this variable is non-nil, Emacs will pass them on, allowing the
15177system to handle them. */);
15178 w32_pass_extra_mouse_buttons_to_system = 0;
15179
ee78dc32
GV
15180 init_x_parm_symbols ();
15181
15182 DEFVAR_LISP ("x-bitmap-file-path", &Vx_bitmap_file_path,
6e2d67d8 15183 doc: /* List of directories to search for window system bitmap files. */);
ee78dc32
GV
15184 Vx_bitmap_file_path = decode_env_path ((char *) 0, "PATH");
15185
15186 DEFVAR_LISP ("x-pointer-shape", &Vx_pointer_shape,
74e1aeec
JR
15187 doc: /* The shape of the pointer when over text.
15188Changing the value does not affect existing frames
15189unless you set the mouse color. */);
ee78dc32
GV
15190 Vx_pointer_shape = Qnil;
15191
15192 DEFVAR_LISP ("x-resource-name", &Vx_resource_name,
74e1aeec
JR
15193 doc: /* The name Emacs uses to look up resources; for internal use only.
15194`x-get-resource' uses this as the first component of the instance name
15195when requesting resource values.
15196Emacs initially sets `x-resource-name' to the name under which Emacs
15197was invoked, or to the value specified with the `-name' or `-rn'
15198switches, if present. */);
ee78dc32
GV
15199 Vx_resource_name = Qnil;
15200
15201 Vx_nontext_pointer_shape = Qnil;
15202
15203 Vx_mode_pointer_shape = Qnil;
15204
0af913d7 15205 DEFVAR_LISP ("x-hourglass-pointer-shape", &Vx_hourglass_pointer_shape,
74e1aeec
JR
15206 doc: /* The shape of the pointer when Emacs is busy.
15207This variable takes effect when you create a new frame
15208or when you set the mouse color. */);
0af913d7 15209 Vx_hourglass_pointer_shape = Qnil;
6fc2811b 15210
0af913d7 15211 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
74e1aeec 15212 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
0af913d7 15213 display_hourglass_p = 1;
7d0393cf 15214
0af913d7 15215 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
74e1aeec
JR
15216 doc: /* *Seconds to wait before displaying an hourglass pointer.
15217Value must be an integer or float. */);
0af913d7 15218 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
f79e6790 15219
6fc2811b 15220 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
ee78dc32 15221 &Vx_sensitive_text_pointer_shape,
74e1aeec
JR
15222 doc: /* The shape of the pointer when over mouse-sensitive text.
15223This variable takes effect when you create a new frame
15224or when you set the mouse color. */);
ee78dc32
GV
15225 Vx_sensitive_text_pointer_shape = Qnil;
15226
4694d762
JR
15227 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
15228 &Vx_window_horizontal_drag_shape,
74e1aeec
JR
15229 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
15230This variable takes effect when you create a new frame
15231or when you set the mouse color. */);
4694d762
JR
15232 Vx_window_horizontal_drag_shape = Qnil;
15233
ee78dc32 15234 DEFVAR_LISP ("x-cursor-fore-pixel", &Vx_cursor_fore_pixel,
74e1aeec 15235 doc: /* A string indicating the foreground color of the cursor box. */);
ee78dc32
GV
15236 Vx_cursor_fore_pixel = Qnil;
15237
3cf3436e 15238 DEFVAR_LISP ("x-max-tooltip-size", &Vx_max_tooltip_size,
b3700ae7
JR
15239 doc: /* Maximum size for tooltips.
15240Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
3cf3436e 15241 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
7d0393cf 15242
ee78dc32 15243 DEFVAR_LISP ("x-no-window-manager", &Vx_no_window_manager,
74e1aeec
JR
15244 doc: /* Non-nil if no window manager is in use.
15245Emacs doesn't try to figure this out; this is always nil
15246unless you set it to something else. */);
ee78dc32
GV
15247 /* We don't have any way to find this out, so set it to nil
15248 and maybe the user would like to set it to t. */
15249 Vx_no_window_manager = Qnil;
15250
4587b026
GV
15251 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
15252 &Vx_pixel_size_width_font_regexp,
74e1aeec
JR
15253 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
15254
15255Since Emacs gets width of a font matching with this regexp from
15256PIXEL_SIZE field of the name, font finding mechanism gets faster for
15257such a font. This is especially effective for such large fonts as
15258Chinese, Japanese, and Korean. */);
4587b026
GV
15259 Vx_pixel_size_width_font_regexp = Qnil;
15260
6fc2811b 15261 DEFVAR_LISP ("image-cache-eviction-delay", &Vimage_cache_eviction_delay,
74e1aeec
JR
15262 doc: /* Time after which cached images are removed from the cache.
15263When an image has not been displayed this many seconds, remove it
15264from the image cache. Value must be an integer or nil with nil
15265meaning don't clear the cache. */);
6fc2811b
JR
15266 Vimage_cache_eviction_delay = make_number (30 * 60);
15267
33d52f9c
GV
15268 DEFVAR_LISP ("w32-bdf-filename-alist",
15269 &Vw32_bdf_filename_alist,
74e1aeec 15270 doc: /* List of bdf fonts and their corresponding filenames. */);
33d52f9c
GV
15271 Vw32_bdf_filename_alist = Qnil;
15272
1075afa9
GV
15273 DEFVAR_BOOL ("w32-strict-fontnames",
15274 &w32_strict_fontnames,
74e1aeec
JR
15275 doc: /* Non-nil means only use fonts that are exact matches for those requested.
15276Default is nil, which allows old fontnames that are not XLFD compliant,
15277and allows third-party CJK display to work by specifying false charset
15278fields to trick Emacs into translating to Big5, SJIS etc.
15279Setting this to t will prevent wrong fonts being selected when
15280fontsets are automatically created. */);
1075afa9
GV
15281 w32_strict_fontnames = 0;
15282
c0611964
AI
15283 DEFVAR_BOOL ("w32-strict-painting",
15284 &w32_strict_painting,
74e1aeec
JR
15285 doc: /* Non-nil means use strict rules for repainting frames.
15286Set this to nil to get the old behaviour for repainting; this should
15287only be necessary if the default setting causes problems. */);
c0611964
AI
15288 w32_strict_painting = 1;
15289
dfff8a69
JR
15290 DEFVAR_LISP ("w32-charset-info-alist",
15291 &Vw32_charset_info_alist,
b3700ae7
JR
15292 doc: /* Alist linking Emacs character sets to Windows fonts and codepages.
15293Each entry should be of the form:
74e1aeec
JR
15294
15295 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE))
15296
15297where CHARSET_NAME is a string used in font names to identify the charset,
15298WINDOWS_CHARSET is a symbol that can be one of:
15299w32-charset-ansi, w32-charset-default, w32-charset-symbol,
15300w32-charset-shiftjis, w32-charset-hangeul, w32-charset-gb2312,
15301w32-charset-chinesebig5,
dfff8a69 15302#ifdef JOHAB_CHARSET
74e1aeec
JR
15303w32-charset-johab, w32-charset-hebrew,
15304w32-charset-arabic, w32-charset-greek, w32-charset-turkish,
15305w32-charset-vietnamese, w32-charset-thai, w32-charset-easteurope,
15306w32-charset-russian, w32-charset-mac, w32-charset-baltic,
dfff8a69
JR
15307#endif
15308#ifdef UNICODE_CHARSET
74e1aeec 15309w32-charset-unicode,
dfff8a69 15310#endif
74e1aeec
JR
15311or w32-charset-oem.
15312CODEPAGE should be an integer specifying the codepage that should be used
15313to display the character set, t to do no translation and output as Unicode,
15314or nil to do no translation and output as 8 bit (or multibyte on far-east
15315versions of Windows) characters. */);
dfff8a69
JR
15316 Vw32_charset_info_alist = Qnil;
15317
15318 staticpro (&Qw32_charset_ansi);
15319 Qw32_charset_ansi = intern ("w32-charset-ansi");
15320 staticpro (&Qw32_charset_symbol);
15321 Qw32_charset_symbol = intern ("w32-charset-symbol");
15322 staticpro (&Qw32_charset_shiftjis);
15323 Qw32_charset_shiftjis = intern ("w32-charset-shiftjis");
767b1ff0
JR
15324 staticpro (&Qw32_charset_hangeul);
15325 Qw32_charset_hangeul = intern ("w32-charset-hangeul");
dfff8a69
JR
15326 staticpro (&Qw32_charset_chinesebig5);
15327 Qw32_charset_chinesebig5 = intern ("w32-charset-chinesebig5");
15328 staticpro (&Qw32_charset_gb2312);
15329 Qw32_charset_gb2312 = intern ("w32-charset-gb2312");
15330 staticpro (&Qw32_charset_oem);
15331 Qw32_charset_oem = intern ("w32-charset-oem");
15332
15333#ifdef JOHAB_CHARSET
15334 {
15335 static int w32_extra_charsets_defined = 1;
74e1aeec
JR
15336 DEFVAR_BOOL ("w32-extra-charsets-defined", &w32_extra_charsets_defined,
15337 doc: /* Internal variable. */);
dfff8a69
JR
15338
15339 staticpro (&Qw32_charset_johab);
15340 Qw32_charset_johab = intern ("w32-charset-johab");
15341 staticpro (&Qw32_charset_easteurope);
15342 Qw32_charset_easteurope = intern ("w32-charset-easteurope");
15343 staticpro (&Qw32_charset_turkish);
15344 Qw32_charset_turkish = intern ("w32-charset-turkish");
15345 staticpro (&Qw32_charset_baltic);
15346 Qw32_charset_baltic = intern ("w32-charset-baltic");
15347 staticpro (&Qw32_charset_russian);
15348 Qw32_charset_russian = intern ("w32-charset-russian");
15349 staticpro (&Qw32_charset_arabic);
15350 Qw32_charset_arabic = intern ("w32-charset-arabic");
15351 staticpro (&Qw32_charset_greek);
15352 Qw32_charset_greek = intern ("w32-charset-greek");
15353 staticpro (&Qw32_charset_hebrew);
15354 Qw32_charset_hebrew = intern ("w32-charset-hebrew");
767b1ff0
JR
15355 staticpro (&Qw32_charset_vietnamese);
15356 Qw32_charset_vietnamese = intern ("w32-charset-vietnamese");
dfff8a69
JR
15357 staticpro (&Qw32_charset_thai);
15358 Qw32_charset_thai = intern ("w32-charset-thai");
15359 staticpro (&Qw32_charset_mac);
15360 Qw32_charset_mac = intern ("w32-charset-mac");
15361 }
15362#endif
15363
15364#ifdef UNICODE_CHARSET
15365 {
15366 static int w32_unicode_charset_defined = 1;
15367 DEFVAR_BOOL ("w32-unicode-charset-defined",
74e1aeec
JR
15368 &w32_unicode_charset_defined,
15369 doc: /* Internal variable. */);
dfff8a69
JR
15370
15371 staticpro (&Qw32_charset_unicode);
15372 Qw32_charset_unicode = intern ("w32-charset-unicode");
15373#endif
15374
ee78dc32 15375 defsubr (&Sx_get_resource);
767b1ff0 15376#if 0 /* TODO: Port to W32 */
6fc2811b
JR
15377 defsubr (&Sx_change_window_property);
15378 defsubr (&Sx_delete_window_property);
15379 defsubr (&Sx_window_property);
15380#endif
2d764c78 15381 defsubr (&Sxw_display_color_p);
ee78dc32 15382 defsubr (&Sx_display_grayscale_p);
2d764c78
EZ
15383 defsubr (&Sxw_color_defined_p);
15384 defsubr (&Sxw_color_values);
ee78dc32
GV
15385 defsubr (&Sx_server_max_request_size);
15386 defsubr (&Sx_server_vendor);
15387 defsubr (&Sx_server_version);
15388 defsubr (&Sx_display_pixel_width);
15389 defsubr (&Sx_display_pixel_height);
15390 defsubr (&Sx_display_mm_width);
15391 defsubr (&Sx_display_mm_height);
15392 defsubr (&Sx_display_screens);
15393 defsubr (&Sx_display_planes);
15394 defsubr (&Sx_display_color_cells);
15395 defsubr (&Sx_display_visual_class);
15396 defsubr (&Sx_display_backing_store);
15397 defsubr (&Sx_display_save_under);
15398 defsubr (&Sx_parse_geometry);
15399 defsubr (&Sx_create_frame);
ee78dc32
GV
15400 defsubr (&Sx_open_connection);
15401 defsubr (&Sx_close_connection);
15402 defsubr (&Sx_display_list);
15403 defsubr (&Sx_synchronize);
15404
fbd6baed 15405 /* W32 specific functions */
ee78dc32 15406
1edf84e7 15407 defsubr (&Sw32_focus_frame);
fbd6baed
GV
15408 defsubr (&Sw32_select_font);
15409 defsubr (&Sw32_define_rgb_color);
15410 defsubr (&Sw32_default_color_map);
15411 defsubr (&Sw32_load_color_file);
1edf84e7 15412 defsubr (&Sw32_send_sys_command);
55dcfc15 15413 defsubr (&Sw32_shell_execute);
ccc2d29c
GV
15414 defsubr (&Sw32_register_hot_key);
15415 defsubr (&Sw32_unregister_hot_key);
15416 defsubr (&Sw32_registered_hot_keys);
15417 defsubr (&Sw32_reconstruct_hot_key);
adcc3809 15418 defsubr (&Sw32_toggle_lock_key);
33d52f9c 15419 defsubr (&Sw32_find_bdf_fonts);
4587b026 15420
2254bcde
AI
15421 defsubr (&Sfile_system_info);
15422
4587b026
GV
15423 /* Setting callback functions for fontset handler. */
15424 get_font_info_func = w32_get_font_info;
6fc2811b
JR
15425
15426#if 0 /* This function pointer doesn't seem to be used anywhere.
15427 And the pointer assigned has the wrong type, anyway. */
4587b026 15428 list_fonts_func = w32_list_fonts;
6fc2811b
JR
15429#endif
15430
4587b026
GV
15431 load_font_func = w32_load_font;
15432 find_ccl_program_func = w32_find_ccl_program;
15433 query_font_func = w32_query_font;
15434 set_frame_fontset_func = x_set_font;
15435 check_window_system_func = check_w32;
6fc2811b 15436
6fc2811b
JR
15437 /* Images. */
15438 Qxbm = intern ("xbm");
15439 staticpro (&Qxbm);
a93f4566
GM
15440 QCconversion = intern (":conversion");
15441 staticpro (&QCconversion);
6fc2811b
JR
15442 QCheuristic_mask = intern (":heuristic-mask");
15443 staticpro (&QCheuristic_mask);
15444 QCcolor_symbols = intern (":color-symbols");
15445 staticpro (&QCcolor_symbols);
6fc2811b
JR
15446 QCascent = intern (":ascent");
15447 staticpro (&QCascent);
15448 QCmargin = intern (":margin");
15449 staticpro (&QCmargin);
15450 QCrelief = intern (":relief");
15451 staticpro (&QCrelief);
15452 Qpostscript = intern ("postscript");
15453 staticpro (&Qpostscript);
ac849ba4 15454#if 0 /* TODO: These need entries at top of file. */
6fc2811b
JR
15455 QCloader = intern (":loader");
15456 staticpro (&QCloader);
15457 QCbounding_box = intern (":bounding-box");
15458 staticpro (&QCbounding_box);
15459 QCpt_width = intern (":pt-width");
15460 staticpro (&QCpt_width);
15461 QCpt_height = intern (":pt-height");
15462 staticpro (&QCpt_height);
ac849ba4 15463#endif
6fc2811b
JR
15464 QCindex = intern (":index");
15465 staticpro (&QCindex);
15466 Qpbm = intern ("pbm");
15467 staticpro (&Qpbm);
15468
15469#if HAVE_XPM
15470 Qxpm = intern ("xpm");
15471 staticpro (&Qxpm);
15472#endif
7d0393cf 15473
6fc2811b
JR
15474#if HAVE_JPEG
15475 Qjpeg = intern ("jpeg");
15476 staticpro (&Qjpeg);
7d0393cf 15477#endif
6fc2811b
JR
15478
15479#if HAVE_TIFF
15480 Qtiff = intern ("tiff");
15481 staticpro (&Qtiff);
7d0393cf 15482#endif
6fc2811b
JR
15483
15484#if HAVE_GIF
15485 Qgif = intern ("gif");
15486 staticpro (&Qgif);
15487#endif
15488
15489#if HAVE_PNG
15490 Qpng = intern ("png");
15491 staticpro (&Qpng);
15492#endif
15493
15494 defsubr (&Sclear_image_cache);
ac849ba4
JR
15495 defsubr (&Simage_size);
15496 defsubr (&Simage_mask_p);
6fc2811b
JR
15497
15498#if GLYPH_DEBUG
15499 defsubr (&Simagep);
15500 defsubr (&Slookup_image);
15501#endif
6fc2811b 15502
0af913d7
GM
15503 hourglass_atimer = NULL;
15504 hourglass_shown_p = 0;
6fc2811b
JR
15505 defsubr (&Sx_show_tip);
15506 defsubr (&Sx_hide_tip);
6fc2811b 15507 tip_timer = Qnil;
57fa2774
JR
15508 staticpro (&tip_timer);
15509 tip_frame = Qnil;
15510 staticpro (&tip_frame);
6fc2811b 15511
ca56d953
JR
15512 last_show_tip_args = Qnil;
15513 staticpro (&last_show_tip_args);
15514
6fc2811b
JR
15515 defsubr (&Sx_file_dialog);
15516}
15517
9785d95b
BK
15518
15519/*
15520 globals_of_w32fns is used to initialize those global variables that
15521 must always be initialized on startup even when the global variable
15522 initialized is non zero (see the function main in emacs.c).
15523 globals_of_w32fns is called from syms_of_w32fns when the global
15524 variable initialized is 0 and directly from main when initialized
15525 is non zero.
15526 */
15527void globals_of_w32fns ()
15528{
15529 HMODULE user32_lib = GetModuleHandle ("user32.dll");
15530 /*
15531 TrackMouseEvent not available in all versions of Windows, so must load
15532 it dynamically. Do it once, here, instead of every time it is used.
15533 */
15534 track_mouse_event_fn = (TrackMouseEvent_Proc) GetProcAddress (user32_lib, "TrackMouseEvent");
15535}
6fc2811b 15536
9785d95b 15537
6fc2811b
JR
15538void
15539init_xfns ()
15540{
15541 image_types = NULL;
15542 Vimage_types = Qnil;
15543
ac849ba4 15544 define_image_type (&pbm_type);
6fc2811b 15545 define_image_type (&xbm_type);
217e5be0 15546#if 0 /* TODO : Image support for W32 */
6fc2811b 15547 define_image_type (&gs_type);
ac849ba4 15548#endif
7d0393cf 15549
6fc2811b
JR
15550#if HAVE_XPM
15551 define_image_type (&xpm_type);
15552#endif
7d0393cf 15553
6fc2811b
JR
15554#if HAVE_JPEG
15555 define_image_type (&jpeg_type);
15556#endif
7d0393cf 15557
6fc2811b
JR
15558#if HAVE_TIFF
15559 define_image_type (&tiff_type);
15560#endif
919f1e88 15561
6fc2811b
JR
15562#if HAVE_GIF
15563 define_image_type (&gif_type);
15564#endif
7d0393cf 15565
6fc2811b
JR
15566#if HAVE_PNG
15567 define_image_type (&png_type);
15568#endif
ee78dc32
GV
15569}
15570
15571#undef abort
15572
7d0393cf 15573void
fbd6baed 15574w32_abort()
ee78dc32 15575{
5ac45f98
GV
15576 int button;
15577 button = MessageBox (NULL,
15578 "A fatal error has occurred!\n\n"
15579 "Select Abort to exit, Retry to debug, Ignore to continue",
15580 "Emacs Abort Dialog",
15581 MB_ICONEXCLAMATION | MB_TASKMODAL
15582 | MB_SETFOREGROUND | MB_ABORTRETRYIGNORE);
15583 switch (button)
15584 {
15585 case IDRETRY:
15586 DebugBreak ();
15587 break;
15588 case IDIGNORE:
15589 break;
15590 case IDABORT:
15591 default:
15592 abort ();
15593 break;
15594 }
ee78dc32 15595}
d573caac 15596
83c75055
GV
15597/* For convenience when debugging. */
15598int
15599w32_last_error()
15600{
15601 return GetLastError ();
15602}