(Fx_create_frame): Don't bother calling
[bpt/emacs.git] / src / w32fns.c
CommitLineData
e9e23e23 1/* Graphical user interface functions for the Microsoft W32 API.
6fc2811b
JR
2 Copyright (C) 1989, 92, 93, 94, 95, 1996, 1997, 1998, 1999
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
GV
34#include "w32term.h"
35#include "frame.h"
36#include "window.h"
37#include "buffer.h"
126f2e35 38#include "fontset.h"
6fc2811b 39#include "intervals.h"
ee78dc32
GV
40#include "keyboard.h"
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
71eab8d1
AI
55#define max(a, b) ((a) > (b) ? (a) : (b))
56
ee78dc32 57extern void free_frame_menubar ();
6fc2811b 58extern double atof ();
adcc3809 59extern int w32_console_toggle_lock_key (int vk_code, Lisp_Object new_state);
5ac45f98 60extern int quit_char;
ee78dc32 61
6fc2811b
JR
62/* A definition of XColor for non-X frames. */
63#ifndef HAVE_X_WINDOWS
64typedef struct {
65 unsigned long pixel;
66 unsigned short red, green, blue;
67 char flags;
68 char pad;
69} XColor;
70#endif
71
ccc2d29c
GV
72extern char *lispy_function_keys[];
73
6fc2811b
JR
74/* The gray bitmap `bitmaps/gray'. This is done because w32term.c uses
75 it, and including `bitmaps/gray' more than once is a problem when
76 config.h defines `static' as an empty replacement string. */
77
78int gray_bitmap_width = gray_width;
79int gray_bitmap_height = gray_height;
80unsigned char *gray_bitmap_bits = gray_bits;
81
ee78dc32 82/* The colormap for converting color names to RGB values */
fbd6baed 83Lisp_Object Vw32_color_map;
ee78dc32 84
da36a4d6 85/* Non nil if alt key presses are passed on to Windows. */
fbd6baed 86Lisp_Object Vw32_pass_alt_to_system;
da36a4d6 87
8c205c63
RS
88/* Non nil if alt key is translated to meta_modifier, nil if it is translated
89 to alt_modifier. */
fbd6baed 90Lisp_Object Vw32_alt_is_meta;
8c205c63 91
7d081355
AI
92/* If non-zero, the windows virtual key code for an alternative quit key. */
93Lisp_Object Vw32_quit_key;
94
ccc2d29c
GV
95/* Non nil if left window key events are passed on to Windows (this only
96 affects whether "tapping" the key opens the Start menu). */
97Lisp_Object Vw32_pass_lwindow_to_system;
98
99/* Non nil if right window key events are passed on to Windows (this
100 only affects whether "tapping" the key opens the Start menu). */
101Lisp_Object Vw32_pass_rwindow_to_system;
102
adcc3809
GV
103/* Virtual key code used to generate "phantom" key presses in order
104 to stop system from acting on Windows key events. */
105Lisp_Object Vw32_phantom_key_code;
106
ccc2d29c
GV
107/* Modifier associated with the left "Windows" key, or nil to act as a
108 normal key. */
109Lisp_Object Vw32_lwindow_modifier;
110
111/* Modifier associated with the right "Windows" key, or nil to act as a
112 normal key. */
113Lisp_Object Vw32_rwindow_modifier;
114
115/* Modifier associated with the "Apps" key, or nil to act as a normal
116 key. */
117Lisp_Object Vw32_apps_modifier;
118
119/* Value is nil if Num Lock acts as a function key. */
120Lisp_Object Vw32_enable_num_lock;
121
122/* Value is nil if Caps Lock acts as a function key. */
123Lisp_Object Vw32_enable_caps_lock;
124
125/* Modifier associated with Scroll Lock, or nil to act as a normal key. */
126Lisp_Object Vw32_scroll_lock_modifier;
da36a4d6 127
7ce9aaca 128/* Switch to control whether we inhibit requests for synthesized bold
6fc2811b
JR
129 and italic versions of fonts. */
130Lisp_Object Vw32_enable_synthesized_fonts;
5ac45f98
GV
131
132/* Enable palette management. */
fbd6baed 133Lisp_Object Vw32_enable_palette;
5ac45f98
GV
134
135/* Control how close left/right button down events must be to
136 be converted to a middle button down event. */
fbd6baed 137Lisp_Object Vw32_mouse_button_tolerance;
5ac45f98 138
84fb1139
KH
139/* Minimum interval between mouse movement (and scroll bar drag)
140 events that are passed on to the event loop. */
fbd6baed 141Lisp_Object Vw32_mouse_move_interval;
84fb1139 142
ee78dc32
GV
143/* The name we're using in resource queries. */
144Lisp_Object Vx_resource_name;
145
146/* Non nil if no window manager is in use. */
147Lisp_Object Vx_no_window_manager;
148
6fc2811b 149/* Non-zero means we're allowed to display a busy cursor. */
dfff8a69 150
6fc2811b
JR
151int display_busy_cursor_p;
152
ee78dc32
GV
153/* The background and shape of the mouse pointer, and shape when not
154 over text or in the modeline. */
dfff8a69 155
ee78dc32 156Lisp_Object Vx_pointer_shape, Vx_nontext_pointer_shape, Vx_mode_pointer_shape;
6fc2811b
JR
157Lisp_Object Vx_busy_pointer_shape;
158
ee78dc32 159/* The shape when over mouse-sensitive text. */
dfff8a69 160
ee78dc32
GV
161Lisp_Object Vx_sensitive_text_pointer_shape;
162
163/* Color of chars displayed in cursor box. */
dfff8a69 164
ee78dc32
GV
165Lisp_Object Vx_cursor_fore_pixel;
166
1edf84e7 167/* Nonzero if using Windows. */
dfff8a69 168
1edf84e7
GV
169static int w32_in_use;
170
ee78dc32 171/* Search path for bitmap files. */
dfff8a69 172
ee78dc32
GV
173Lisp_Object Vx_bitmap_file_path;
174
4587b026 175/* Regexp matching a font name whose width is the same as `PIXEL_SIZE'. */
dfff8a69 176
4587b026
GV
177Lisp_Object Vx_pixel_size_width_font_regexp;
178
33d52f9c
GV
179/* Alist of bdf fonts and the files that define them. */
180Lisp_Object Vw32_bdf_filename_alist;
181
f46e6225
GV
182Lisp_Object Vw32_system_coding_system;
183
f46e6225 184/* A flag to control whether fonts are matched strictly or not. */
1075afa9
GV
185int w32_strict_fontnames;
186
c0611964
AI
187/* A flag to control whether we should only repaint if GetUpdateRect
188 indicates there is an update region. */
189int w32_strict_painting;
190
dfff8a69
JR
191/* Associative list linking character set strings to Windows codepages. */
192Lisp_Object Vw32_charset_info_alist;
193
194/* VIETNAMESE_CHARSET is not defined in some versions of MSVC. */
195#ifndef VIETNAMESE_CHARSET
196#define VIETNAMESE_CHARSET 163
197#endif
198
199
ee78dc32
GV
200/* Evaluate this expression to rebuild the section of syms_of_w32fns
201 that initializes and staticpros the symbols declared below. Note
202 that Emacs 18 has a bug that keeps C-x C-e from being able to
203 evaluate this expression.
204
205(progn
206 ;; Accumulate a list of the symbols we want to initialize from the
207 ;; declarations at the top of the file.
208 (goto-char (point-min))
209 (search-forward "/\*&&& symbols declared here &&&*\/\n")
210 (let (symbol-list)
211 (while (looking-at "Lisp_Object \\(Q[a-z_]+\\)")
212 (setq symbol-list
213 (cons (buffer-substring (match-beginning 1) (match-end 1))
214 symbol-list))
215 (forward-line 1))
216 (setq symbol-list (nreverse symbol-list))
217 ;; Delete the section of syms_of_... where we initialize the symbols.
218 (search-forward "\n /\*&&& init symbols here &&&*\/\n")
219 (let ((start (point)))
220 (while (looking-at "^ Q")
221 (forward-line 2))
222 (kill-region start (point)))
223 ;; Write a new symbol initialization section.
224 (while symbol-list
225 (insert (format " %s = intern (\"" (car symbol-list)))
226 (let ((start (point)))
227 (insert (substring (car symbol-list) 1))
228 (subst-char-in-region start (point) ?_ ?-))
229 (insert (format "\");\n staticpro (&%s);\n" (car symbol-list)))
230 (setq symbol-list (cdr symbol-list)))))
231
232 */
233
234/*&&& symbols declared here &&&*/
235Lisp_Object Qauto_raise;
236Lisp_Object Qauto_lower;
ee78dc32
GV
237Lisp_Object Qbar;
238Lisp_Object Qborder_color;
239Lisp_Object Qborder_width;
240Lisp_Object Qbox;
241Lisp_Object Qcursor_color;
242Lisp_Object Qcursor_type;
ee78dc32
GV
243Lisp_Object Qgeometry;
244Lisp_Object Qicon_left;
245Lisp_Object Qicon_top;
246Lisp_Object Qicon_type;
247Lisp_Object Qicon_name;
248Lisp_Object Qinternal_border_width;
249Lisp_Object Qleft;
1026b400 250Lisp_Object Qright;
ee78dc32
GV
251Lisp_Object Qmouse_color;
252Lisp_Object Qnone;
253Lisp_Object Qparent_id;
254Lisp_Object Qscroll_bar_width;
255Lisp_Object Qsuppress_icon;
ee78dc32
GV
256Lisp_Object Qundefined_color;
257Lisp_Object Qvertical_scroll_bars;
258Lisp_Object Qvisibility;
259Lisp_Object Qwindow_id;
260Lisp_Object Qx_frame_parameter;
261Lisp_Object Qx_resource_name;
262Lisp_Object Quser_position;
263Lisp_Object Quser_size;
6fc2811b 264Lisp_Object Qscreen_gamma;
dfff8a69
JR
265Lisp_Object Qline_spacing;
266Lisp_Object Qcenter;
adcc3809
GV
267Lisp_Object Qhyper;
268Lisp_Object Qsuper;
269Lisp_Object Qmeta;
270Lisp_Object Qalt;
271Lisp_Object Qctrl;
272Lisp_Object Qcontrol;
273Lisp_Object Qshift;
274
dfff8a69
JR
275Lisp_Object Qw32_charset_ansi;
276Lisp_Object Qw32_charset_default;
277Lisp_Object Qw32_charset_symbol;
278Lisp_Object Qw32_charset_shiftjis;
279Lisp_Object Qw32_charset_hangul;
280Lisp_Object Qw32_charset_gb2312;
281Lisp_Object Qw32_charset_chinesebig5;
282Lisp_Object Qw32_charset_oem;
283
71eab8d1
AI
284#ifndef JOHAB_CHARSET
285#define JOHAB_CHARSET 130
286#endif
dfff8a69
JR
287#ifdef JOHAB_CHARSET
288Lisp_Object Qw32_charset_easteurope;
289Lisp_Object Qw32_charset_turkish;
290Lisp_Object Qw32_charset_baltic;
291Lisp_Object Qw32_charset_russian;
292Lisp_Object Qw32_charset_arabic;
293Lisp_Object Qw32_charset_greek;
294Lisp_Object Qw32_charset_hebrew;
295Lisp_Object Qw32_charset_thai;
296Lisp_Object Qw32_charset_johab;
297Lisp_Object Qw32_charset_mac;
298#endif
299
300#ifdef UNICODE_CHARSET
301Lisp_Object Qw32_charset_unicode;
302#endif
303
6fc2811b
JR
304extern Lisp_Object Qtop;
305extern Lisp_Object Qdisplay;
306extern Lisp_Object Qtool_bar_lines;
307
5ac45f98
GV
308/* State variables for emulating a three button mouse. */
309#define LMOUSE 1
310#define MMOUSE 2
311#define RMOUSE 4
312
313static int button_state = 0;
fbd6baed 314static W32Msg saved_mouse_button_msg;
84fb1139 315static unsigned mouse_button_timer; /* non-zero when timer is active */
fbd6baed 316static W32Msg saved_mouse_move_msg;
84fb1139
KH
317static unsigned mouse_move_timer;
318
93fbe8b7
GV
319/* W95 mousewheel handler */
320unsigned int msh_mousewheel = 0;
321
84fb1139
KH
322#define MOUSE_BUTTON_ID 1
323#define MOUSE_MOVE_ID 2
5ac45f98 324
ee78dc32 325/* The below are defined in frame.c. */
dfff8a69 326
ee78dc32 327extern Lisp_Object Qheight, Qminibuffer, Qname, Qonly, Qwidth;
1edf84e7 328extern Lisp_Object Qunsplittable, Qmenu_bar_lines, Qbuffer_predicate, Qtitle;
6fc2811b 329extern Lisp_Object Qtool_bar_lines;
ee78dc32
GV
330
331extern Lisp_Object Vwindow_system_version;
332
4b817373
RS
333Lisp_Object Qface_set_after_frame_default;
334
fbd6baed
GV
335/* From w32term.c. */
336extern Lisp_Object Vw32_num_mouse_buttons;
ccc2d29c 337extern Lisp_Object Vw32_recognize_altgr;
5ac45f98 338
ee78dc32 339\f
1edf84e7
GV
340/* Error if we are not connected to MS-Windows. */
341void
342check_w32 ()
343{
344 if (! w32_in_use)
345 error ("MS-Windows not in use or not initialized");
346}
347
348/* Nonzero if we can use mouse menus.
349 You should not call this unless HAVE_MENUS is defined. */
350
351int
352have_menus_p ()
353{
354 return w32_in_use;
355}
356
ee78dc32 357/* Extract a frame as a FRAME_PTR, defaulting to the selected frame
fbd6baed 358 and checking validity for W32. */
ee78dc32
GV
359
360FRAME_PTR
361check_x_frame (frame)
362 Lisp_Object frame;
363{
364 FRAME_PTR f;
365
366 if (NILP (frame))
6fc2811b
JR
367 frame = selected_frame;
368 CHECK_LIVE_FRAME (frame, 0);
369 f = XFRAME (frame);
fbd6baed
GV
370 if (! FRAME_W32_P (f))
371 error ("non-w32 frame used");
ee78dc32
GV
372 return f;
373}
374
375/* Let the user specify an display with a frame.
fbd6baed 376 nil stands for the selected frame--or, if that is not a w32 frame,
ee78dc32
GV
377 the first display on the list. */
378
fbd6baed 379static struct w32_display_info *
ee78dc32
GV
380check_x_display_info (frame)
381 Lisp_Object frame;
382{
383 if (NILP (frame))
384 {
6fc2811b
JR
385 struct frame *sf = XFRAME (selected_frame);
386
387 if (FRAME_W32_P (sf) && FRAME_LIVE_P (sf))
388 return FRAME_W32_DISPLAY_INFO (sf);
ee78dc32 389 else
fbd6baed 390 return &one_w32_display_info;
ee78dc32
GV
391 }
392 else if (STRINGP (frame))
393 return x_display_info_for_name (frame);
394 else
395 {
396 FRAME_PTR f;
397
398 CHECK_LIVE_FRAME (frame, 0);
399 f = XFRAME (frame);
fbd6baed
GV
400 if (! FRAME_W32_P (f))
401 error ("non-w32 frame used");
402 return FRAME_W32_DISPLAY_INFO (f);
ee78dc32
GV
403 }
404}
405\f
fbd6baed 406/* Return the Emacs frame-object corresponding to an w32 window.
ee78dc32
GV
407 It could be the frame's main window or an icon window. */
408
409/* This function can be called during GC, so use GC_xxx type test macros. */
410
411struct frame *
412x_window_to_frame (dpyinfo, wdesc)
fbd6baed 413 struct w32_display_info *dpyinfo;
ee78dc32
GV
414 HWND wdesc;
415{
416 Lisp_Object tail, frame;
417 struct frame *f;
418
8e713be6 419 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
ee78dc32 420 {
8e713be6 421 frame = XCAR (tail);
ee78dc32
GV
422 if (!GC_FRAMEP (frame))
423 continue;
424 f = XFRAME (frame);
2d764c78 425 if (!FRAME_W32_P (f) || FRAME_W32_DISPLAY_INFO (f) != dpyinfo)
ee78dc32 426 continue;
f79e6790
JR
427 if (f->output_data.w32->busy_window == wdesc)
428 return f;
429
430 /* NTEMACS_TODO: Check tooltips when supported. */
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;
6fc2811b 560#if 0 /* NTEMACS_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
575 && !strcmp (dpyinfo->bitmaps[id].file, (char *) XSTRING (file)->data))
576 {
577 ++dpyinfo->bitmaps[id].refcount;
578 return id + 1;
579 }
580 }
581
582 /* Search bitmap-file-path for the file, if appropriate. */
583 fd = openp (Vx_bitmap_file_path, file, "", &found, 0);
584 if (fd < 0)
585 return -1;
5d7fed93
GV
586 /* LoadLibraryEx won't handle special files handled by Emacs handler. */
587 if (fd == 0)
588 return -1;
6fc2811b 589 emacs_close (fd);
ee78dc32
GV
590
591 filename = (char *) XSTRING (found)->data;
592
593 hinst = LoadLibraryEx (filename, NULL, LOAD_LIBRARY_AS_DATAFILE);
594
595 if (hinst == NULL)
596 return -1;
597
598
fbd6baed 599 result = XReadBitmapFile (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
ee78dc32
GV
600 filename, &width, &height, &bitmap, &xhot, &yhot);
601 if (result != BitmapSuccess)
602 return -1;
603
604 id = x_allocate_bitmap_record (f);
605 dpyinfo->bitmaps[id - 1].pixmap = bitmap;
606 dpyinfo->bitmaps[id - 1].refcount = 1;
607 dpyinfo->bitmaps[id - 1].file = (char *) xmalloc (XSTRING (file)->size + 1);
608 dpyinfo->bitmaps[id - 1].depth = 1;
609 dpyinfo->bitmaps[id - 1].height = height;
610 dpyinfo->bitmaps[id - 1].width = width;
611 strcpy (dpyinfo->bitmaps[id - 1].file, XSTRING (file)->data);
612
613 return id;
6fc2811b 614#endif /* NTEMACS_TODO */
ee78dc32
GV
615}
616
617/* Remove reference to bitmap with id number ID. */
618
33d52f9c 619void
ee78dc32
GV
620x_destroy_bitmap (f, id)
621 FRAME_PTR f;
622 int id;
623{
fbd6baed 624 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
ee78dc32
GV
625
626 if (id > 0)
627 {
628 --dpyinfo->bitmaps[id - 1].refcount;
629 if (dpyinfo->bitmaps[id - 1].refcount == 0)
630 {
631 BLOCK_INPUT;
632 DeleteObject (dpyinfo->bitmaps[id - 1].pixmap);
633 if (dpyinfo->bitmaps[id - 1].file)
634 {
6fc2811b 635 xfree (dpyinfo->bitmaps[id - 1].file);
ee78dc32
GV
636 dpyinfo->bitmaps[id - 1].file = NULL;
637 }
638 UNBLOCK_INPUT;
639 }
640 }
641}
642
643/* Free all the bitmaps for the display specified by DPYINFO. */
644
645static void
646x_destroy_all_bitmaps (dpyinfo)
fbd6baed 647 struct w32_display_info *dpyinfo;
ee78dc32
GV
648{
649 int i;
650 for (i = 0; i < dpyinfo->bitmaps_last; i++)
651 if (dpyinfo->bitmaps[i].refcount > 0)
652 {
653 DeleteObject (dpyinfo->bitmaps[i].pixmap);
654 if (dpyinfo->bitmaps[i].file)
6fc2811b 655 xfree (dpyinfo->bitmaps[i].file);
ee78dc32
GV
656 }
657 dpyinfo->bitmaps_last = 0;
658}
659\f
fbd6baed 660/* Connect the frame-parameter names for W32 frames
ee78dc32
GV
661 to the ways of passing the parameter values to the window system.
662
663 The name of a parameter, as a Lisp symbol,
664 has an `x-frame-parameter' property which is an integer in Lisp
665 but can be interpreted as an `enum x_frame_parm' in C. */
666
667enum x_frame_parm
668{
669 X_PARM_FOREGROUND_COLOR,
670 X_PARM_BACKGROUND_COLOR,
671 X_PARM_MOUSE_COLOR,
672 X_PARM_CURSOR_COLOR,
673 X_PARM_BORDER_COLOR,
674 X_PARM_ICON_TYPE,
675 X_PARM_FONT,
676 X_PARM_BORDER_WIDTH,
677 X_PARM_INTERNAL_BORDER_WIDTH,
678 X_PARM_NAME,
679 X_PARM_AUTORAISE,
680 X_PARM_AUTOLOWER,
681 X_PARM_VERT_SCROLL_BAR,
682 X_PARM_VISIBILITY,
683 X_PARM_MENU_BAR_LINES
684};
685
686
687struct x_frame_parm_table
688{
689 char *name;
6fc2811b 690 void (*setter) P_ ((struct frame *, Lisp_Object, Lisp_Object));
ee78dc32
GV
691};
692
6fc2811b
JR
693/* NTEMACS_TODO: Native Input Method support; see x_create_im. */
694void x_set_foreground_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
dfff8a69 695static void x_set_line_spacing P_ ((struct frame *, Lisp_Object, Lisp_Object));
6fc2811b
JR
696void x_set_background_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
697void x_set_mouse_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
698void x_set_cursor_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
699void x_set_border_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
700void x_set_cursor_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
701void x_set_icon_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
702void x_set_icon_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
703void x_set_font P_ ((struct frame *, Lisp_Object, Lisp_Object));
704void x_set_border_width P_ ((struct frame *, Lisp_Object, Lisp_Object));
705void x_set_internal_border_width P_ ((struct frame *, Lisp_Object,
706 Lisp_Object));
707void x_explicitly_set_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
708void x_set_autoraise P_ ((struct frame *, Lisp_Object, Lisp_Object));
709void x_set_autolower P_ ((struct frame *, Lisp_Object, Lisp_Object));
710void x_set_vertical_scroll_bars P_ ((struct frame *, Lisp_Object,
711 Lisp_Object));
712void x_set_visibility P_ ((struct frame *, Lisp_Object, Lisp_Object));
713void x_set_menu_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
714void x_set_scroll_bar_width P_ ((struct frame *, Lisp_Object, Lisp_Object));
715void x_set_title P_ ((struct frame *, Lisp_Object, Lisp_Object));
716void x_set_unsplittable P_ ((struct frame *, Lisp_Object, Lisp_Object));
717void x_set_tool_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
718static void x_set_screen_gamma P_ ((struct frame *, Lisp_Object, Lisp_Object));
ee78dc32
GV
719
720static struct x_frame_parm_table x_frame_parms[] =
721{
1edf84e7
GV
722 "auto-raise", x_set_autoraise,
723 "auto-lower", x_set_autolower,
ee78dc32 724 "background-color", x_set_background_color,
ee78dc32 725 "border-color", x_set_border_color,
1edf84e7
GV
726 "border-width", x_set_border_width,
727 "cursor-color", x_set_cursor_color,
ee78dc32 728 "cursor-type", x_set_cursor_type,
ee78dc32 729 "font", x_set_font,
1edf84e7
GV
730 "foreground-color", x_set_foreground_color,
731 "icon-name", x_set_icon_name,
732 "icon-type", x_set_icon_type,
ee78dc32 733 "internal-border-width", x_set_internal_border_width,
ee78dc32 734 "menu-bar-lines", x_set_menu_bar_lines,
1edf84e7
GV
735 "mouse-color", x_set_mouse_color,
736 "name", x_explicitly_set_name,
ee78dc32 737 "scroll-bar-width", x_set_scroll_bar_width,
1edf84e7 738 "title", x_set_title,
ee78dc32 739 "unsplittable", x_set_unsplittable,
1edf84e7
GV
740 "vertical-scroll-bars", x_set_vertical_scroll_bars,
741 "visibility", x_set_visibility,
6fc2811b 742 "tool-bar-lines", x_set_tool_bar_lines,
dfff8a69
JR
743 "screen-gamma", x_set_screen_gamma,
744 "line-spacing", x_set_line_spacing
ee78dc32
GV
745};
746
747/* Attach the `x-frame-parameter' properties to
fbd6baed 748 the Lisp symbol names of parameters relevant to W32. */
ee78dc32 749
dfff8a69 750void
ee78dc32
GV
751init_x_parm_symbols ()
752{
753 int i;
754
755 for (i = 0; i < sizeof (x_frame_parms) / sizeof (x_frame_parms[0]); i++)
756 Fput (intern (x_frame_parms[i].name), Qx_frame_parameter,
757 make_number (i));
758}
759\f
dfff8a69 760/* Change the parameters of frame F as specified by ALIST.
ee78dc32
GV
761 If a parameter is not specially recognized, do nothing;
762 otherwise call the `x_set_...' function for that parameter. */
763
764void
765x_set_frame_parameters (f, alist)
766 FRAME_PTR f;
767 Lisp_Object alist;
768{
769 Lisp_Object tail;
770
771 /* If both of these parameters are present, it's more efficient to
772 set them both at once. So we wait until we've looked at the
773 entire list before we set them. */
b839712d 774 int width, height;
ee78dc32
GV
775
776 /* Same here. */
777 Lisp_Object left, top;
778
779 /* Same with these. */
780 Lisp_Object icon_left, icon_top;
781
782 /* Record in these vectors all the parms specified. */
783 Lisp_Object *parms;
784 Lisp_Object *values;
a797a73d 785 int i, p;
ee78dc32
GV
786 int left_no_change = 0, top_no_change = 0;
787 int icon_left_no_change = 0, icon_top_no_change = 0;
788
5878523b
RS
789 struct gcpro gcpro1, gcpro2;
790
ee78dc32
GV
791 i = 0;
792 for (tail = alist; CONSP (tail); tail = Fcdr (tail))
793 i++;
794
795 parms = (Lisp_Object *) alloca (i * sizeof (Lisp_Object));
796 values = (Lisp_Object *) alloca (i * sizeof (Lisp_Object));
797
798 /* Extract parm names and values into those vectors. */
799
800 i = 0;
801 for (tail = alist; CONSP (tail); tail = Fcdr (tail))
802 {
6fc2811b 803 Lisp_Object elt;
ee78dc32
GV
804
805 elt = Fcar (tail);
806 parms[i] = Fcar (elt);
807 values[i] = Fcdr (elt);
808 i++;
809 }
5878523b
RS
810 /* TAIL and ALIST are not used again below here. */
811 alist = tail = Qnil;
812
813 GCPRO2 (*parms, *values);
814 gcpro1.nvars = i;
815 gcpro2.nvars = i;
816
817 /* There is no need to gcpro LEFT, TOP, ICON_LEFT, or ICON_TOP,
818 because their values appear in VALUES and strings are not valid. */
b839712d 819 top = left = Qunbound;
ee78dc32
GV
820 icon_left = icon_top = Qunbound;
821
b839712d 822 /* Provide default values for HEIGHT and WIDTH. */
dfff8a69
JR
823 if (FRAME_NEW_WIDTH (f))
824 width = FRAME_NEW_WIDTH (f);
825 else
826 width = FRAME_WIDTH (f);
827
828 if (FRAME_NEW_HEIGHT (f))
829 height = FRAME_NEW_HEIGHT (f);
830 else
831 height = FRAME_HEIGHT (f);
b839712d 832
a797a73d
GV
833 /* Process foreground_color and background_color before anything else.
834 They are independent of other properties, but other properties (e.g.,
835 cursor_color) are dependent upon them. */
836 for (p = 0; p < i; p++)
837 {
838 Lisp_Object prop, val;
839
840 prop = parms[p];
841 val = values[p];
842 if (EQ (prop, Qforeground_color) || EQ (prop, Qbackground_color))
843 {
844 register Lisp_Object param_index, old_value;
845
846 param_index = Fget (prop, Qx_frame_parameter);
847 old_value = get_frame_param (f, prop);
848 store_frame_param (f, prop, val);
849 if (NATNUMP (param_index)
850 && (XFASTINT (param_index)
851 < sizeof (x_frame_parms)/sizeof (x_frame_parms[0])))
852 (*x_frame_parms[XINT (param_index)].setter)(f, val, old_value);
853 }
854 }
855
ee78dc32
GV
856 /* Now process them in reverse of specified order. */
857 for (i--; i >= 0; i--)
858 {
859 Lisp_Object prop, val;
860
861 prop = parms[i];
862 val = values[i];
863
b839712d
RS
864 if (EQ (prop, Qwidth) && NUMBERP (val))
865 width = XFASTINT (val);
866 else if (EQ (prop, Qheight) && NUMBERP (val))
867 height = XFASTINT (val);
ee78dc32
GV
868 else if (EQ (prop, Qtop))
869 top = val;
870 else if (EQ (prop, Qleft))
871 left = val;
872 else if (EQ (prop, Qicon_top))
873 icon_top = val;
874 else if (EQ (prop, Qicon_left))
875 icon_left = val;
a797a73d
GV
876 else if (EQ (prop, Qforeground_color) || EQ (prop, Qbackground_color))
877 /* Processed above. */
878 continue;
ee78dc32
GV
879 else
880 {
881 register Lisp_Object param_index, old_value;
882
883 param_index = Fget (prop, Qx_frame_parameter);
884 old_value = get_frame_param (f, prop);
885 store_frame_param (f, prop, val);
886 if (NATNUMP (param_index)
887 && (XFASTINT (param_index)
888 < sizeof (x_frame_parms)/sizeof (x_frame_parms[0])))
3c190163 889 (*x_frame_parms[XINT (param_index)].setter)(f, val, old_value);
ee78dc32
GV
890 }
891 }
892
893 /* Don't die if just one of these was set. */
894 if (EQ (left, Qunbound))
895 {
896 left_no_change = 1;
fbd6baed
GV
897 if (f->output_data.w32->left_pos < 0)
898 left = Fcons (Qplus, Fcons (make_number (f->output_data.w32->left_pos), Qnil));
ee78dc32 899 else
fbd6baed 900 XSETINT (left, f->output_data.w32->left_pos);
ee78dc32
GV
901 }
902 if (EQ (top, Qunbound))
903 {
904 top_no_change = 1;
fbd6baed
GV
905 if (f->output_data.w32->top_pos < 0)
906 top = Fcons (Qplus, Fcons (make_number (f->output_data.w32->top_pos), Qnil));
ee78dc32 907 else
fbd6baed 908 XSETINT (top, f->output_data.w32->top_pos);
ee78dc32
GV
909 }
910
911 /* If one of the icon positions was not set, preserve or default it. */
912 if (EQ (icon_left, Qunbound) || ! INTEGERP (icon_left))
913 {
914 icon_left_no_change = 1;
915 icon_left = Fcdr (Fassq (Qicon_left, f->param_alist));
916 if (NILP (icon_left))
917 XSETINT (icon_left, 0);
918 }
919 if (EQ (icon_top, Qunbound) || ! INTEGERP (icon_top))
920 {
921 icon_top_no_change = 1;
922 icon_top = Fcdr (Fassq (Qicon_top, f->param_alist));
923 if (NILP (icon_top))
924 XSETINT (icon_top, 0);
925 }
926
ee78dc32
GV
927 /* Don't set these parameters unless they've been explicitly
928 specified. The window might be mapped or resized while we're in
929 this function, and we don't want to override that unless the lisp
930 code has asked for it.
931
932 Don't set these parameters unless they actually differ from the
933 window's current parameters; the window may not actually exist
934 yet. */
935 {
936 Lisp_Object frame;
937
938 check_frame_size (f, &height, &width);
939
940 XSETFRAME (frame, f);
941
dfff8a69
JR
942 if (width != FRAME_WIDTH (f)
943 || height != FRAME_HEIGHT (f)
944 || FRAME_NEW_HEIGHT (f) || FRAME_NEW_WIDTH (f))
b839712d 945 Fset_frame_size (frame, make_number (width), make_number (height));
ee78dc32
GV
946
947 if ((!NILP (left) || !NILP (top))
948 && ! (left_no_change && top_no_change)
fbd6baed
GV
949 && ! (NUMBERP (left) && XINT (left) == f->output_data.w32->left_pos
950 && NUMBERP (top) && XINT (top) == f->output_data.w32->top_pos))
ee78dc32
GV
951 {
952 int leftpos = 0;
953 int toppos = 0;
954
955 /* Record the signs. */
fbd6baed 956 f->output_data.w32->size_hint_flags &= ~ (XNegative | YNegative);
ee78dc32 957 if (EQ (left, Qminus))
fbd6baed 958 f->output_data.w32->size_hint_flags |= XNegative;
ee78dc32
GV
959 else if (INTEGERP (left))
960 {
961 leftpos = XINT (left);
962 if (leftpos < 0)
fbd6baed 963 f->output_data.w32->size_hint_flags |= XNegative;
ee78dc32 964 }
8e713be6
KR
965 else if (CONSP (left) && EQ (XCAR (left), Qminus)
966 && CONSP (XCDR (left))
967 && INTEGERP (XCAR (XCDR (left))))
ee78dc32 968 {
8e713be6 969 leftpos = - XINT (XCAR (XCDR (left)));
fbd6baed 970 f->output_data.w32->size_hint_flags |= XNegative;
ee78dc32 971 }
8e713be6
KR
972 else if (CONSP (left) && EQ (XCAR (left), Qplus)
973 && CONSP (XCDR (left))
974 && INTEGERP (XCAR (XCDR (left))))
ee78dc32 975 {
8e713be6 976 leftpos = XINT (XCAR (XCDR (left)));
ee78dc32
GV
977 }
978
979 if (EQ (top, Qminus))
fbd6baed 980 f->output_data.w32->size_hint_flags |= YNegative;
ee78dc32
GV
981 else if (INTEGERP (top))
982 {
983 toppos = XINT (top);
984 if (toppos < 0)
fbd6baed 985 f->output_data.w32->size_hint_flags |= YNegative;
ee78dc32 986 }
8e713be6
KR
987 else if (CONSP (top) && EQ (XCAR (top), Qminus)
988 && CONSP (XCDR (top))
989 && INTEGERP (XCAR (XCDR (top))))
ee78dc32 990 {
8e713be6 991 toppos = - XINT (XCAR (XCDR (top)));
fbd6baed 992 f->output_data.w32->size_hint_flags |= YNegative;
ee78dc32 993 }
8e713be6
KR
994 else if (CONSP (top) && EQ (XCAR (top), Qplus)
995 && CONSP (XCDR (top))
996 && INTEGERP (XCAR (XCDR (top))))
ee78dc32 997 {
8e713be6 998 toppos = XINT (XCAR (XCDR (top)));
ee78dc32
GV
999 }
1000
1001
1002 /* Store the numeric value of the position. */
fbd6baed
GV
1003 f->output_data.w32->top_pos = toppos;
1004 f->output_data.w32->left_pos = leftpos;
ee78dc32 1005
fbd6baed 1006 f->output_data.w32->win_gravity = NorthWestGravity;
ee78dc32
GV
1007
1008 /* Actually set that position, and convert to absolute. */
1009 x_set_offset (f, leftpos, toppos, -1);
1010 }
1011
1012 if ((!NILP (icon_left) || !NILP (icon_top))
1013 && ! (icon_left_no_change && icon_top_no_change))
1014 x_wm_set_icon_position (f, XINT (icon_left), XINT (icon_top));
1015 }
5878523b
RS
1016
1017 UNGCPRO;
ee78dc32
GV
1018}
1019
1020/* Store the screen positions of frame F into XPTR and YPTR.
1021 These are the positions of the containing window manager window,
1022 not Emacs's own window. */
1023
1024void
1025x_real_positions (f, xptr, yptr)
1026 FRAME_PTR f;
1027 int *xptr, *yptr;
1028{
1029 POINT pt;
3c190163
GV
1030
1031 {
1032 RECT rect;
ee78dc32 1033
fbd6baed
GV
1034 GetClientRect(FRAME_W32_WINDOW(f), &rect);
1035 AdjustWindowRect(&rect, f->output_data.w32->dwStyle, FRAME_EXTERNAL_MENU_BAR(f));
ee78dc32 1036
3c190163
GV
1037 pt.x = rect.left;
1038 pt.y = rect.top;
1039 }
ee78dc32 1040
fbd6baed 1041 ClientToScreen (FRAME_W32_WINDOW(f), &pt);
ee78dc32
GV
1042
1043 *xptr = pt.x;
1044 *yptr = pt.y;
1045}
1046
1047/* Insert a description of internally-recorded parameters of frame X
1048 into the parameter alist *ALISTPTR that is to be given to the user.
fbd6baed 1049 Only parameters that are specific to W32
ee78dc32
GV
1050 and whose values are not correctly recorded in the frame's
1051 param_alist need to be considered here. */
1052
dfff8a69 1053void
ee78dc32
GV
1054x_report_frame_params (f, alistptr)
1055 struct frame *f;
1056 Lisp_Object *alistptr;
1057{
1058 char buf[16];
1059 Lisp_Object tem;
1060
1061 /* Represent negative positions (off the top or left screen edge)
1062 in a way that Fmodify_frame_parameters will understand correctly. */
fbd6baed
GV
1063 XSETINT (tem, f->output_data.w32->left_pos);
1064 if (f->output_data.w32->left_pos >= 0)
ee78dc32
GV
1065 store_in_alist (alistptr, Qleft, tem);
1066 else
1067 store_in_alist (alistptr, Qleft, Fcons (Qplus, Fcons (tem, Qnil)));
1068
fbd6baed
GV
1069 XSETINT (tem, f->output_data.w32->top_pos);
1070 if (f->output_data.w32->top_pos >= 0)
ee78dc32
GV
1071 store_in_alist (alistptr, Qtop, tem);
1072 else
1073 store_in_alist (alistptr, Qtop, Fcons (Qplus, Fcons (tem, Qnil)));
1074
1075 store_in_alist (alistptr, Qborder_width,
fbd6baed 1076 make_number (f->output_data.w32->border_width));
ee78dc32 1077 store_in_alist (alistptr, Qinternal_border_width,
fbd6baed
GV
1078 make_number (f->output_data.w32->internal_border_width));
1079 sprintf (buf, "%ld", (long) FRAME_W32_WINDOW (f));
ee78dc32
GV
1080 store_in_alist (alistptr, Qwindow_id,
1081 build_string (buf));
1082 store_in_alist (alistptr, Qicon_name, f->icon_name);
1083 FRAME_SAMPLE_VISIBILITY (f);
1084 store_in_alist (alistptr, Qvisibility,
1085 (FRAME_VISIBLE_P (f) ? Qt
1086 : FRAME_ICONIFIED_P (f) ? Qicon : Qnil));
1087 store_in_alist (alistptr, Qdisplay,
8e713be6 1088 XCAR (FRAME_W32_DISPLAY_INFO (f)->name_list_element));
ee78dc32
GV
1089}
1090\f
1091
fbd6baed 1092DEFUN ("w32-define-rgb-color", Fw32_define_rgb_color, Sw32_define_rgb_color, 4, 4, 0,
5ac45f98 1093 "Convert RGB numbers to a windows color reference and associate with NAME (a string).\n\
fbd6baed 1094This adds or updates a named color to w32-color-map, making it available for use.\n\
5ac45f98
GV
1095The original entry's RGB ref is returned, or nil if the entry is new.")
1096 (red, green, blue, name)
1097 Lisp_Object red, green, blue, name;
ee78dc32 1098{
5ac45f98
GV
1099 Lisp_Object rgb;
1100 Lisp_Object oldrgb = Qnil;
1101 Lisp_Object entry;
1102
1103 CHECK_NUMBER (red, 0);
1104 CHECK_NUMBER (green, 0);
1105 CHECK_NUMBER (blue, 0);
1106 CHECK_STRING (name, 0);
ee78dc32 1107
5ac45f98 1108 XSET (rgb, Lisp_Int, RGB(XUINT (red), XUINT (green), XUINT (blue)));
ee78dc32 1109
5ac45f98 1110 BLOCK_INPUT;
ee78dc32 1111
fbd6baed
GV
1112 /* replace existing entry in w32-color-map or add new entry. */
1113 entry = Fassoc (name, Vw32_color_map);
5ac45f98
GV
1114 if (NILP (entry))
1115 {
1116 entry = Fcons (name, rgb);
fbd6baed 1117 Vw32_color_map = Fcons (entry, Vw32_color_map);
5ac45f98
GV
1118 }
1119 else
1120 {
1121 oldrgb = Fcdr (entry);
1122 Fsetcdr (entry, rgb);
1123 }
1124
1125 UNBLOCK_INPUT;
1126
1127 return (oldrgb);
ee78dc32
GV
1128}
1129
fbd6baed 1130DEFUN ("w32-load-color-file", Fw32_load_color_file, Sw32_load_color_file, 1, 1, 0,
5ac45f98 1131 "Create an alist of color entries from an external file (ie. rgb.txt).\n\
fbd6baed 1132Assign this value to w32-color-map to replace the existing color map.\n\
5ac45f98
GV
1133\
1134The file should define one named RGB color per line like so:\
1135 R G B name\n\
1136where R,G,B are numbers between 0 and 255 and name is an arbitrary string.")
1137 (filename)
1138 Lisp_Object filename;
1139{
1140 FILE *fp;
1141 Lisp_Object cmap = Qnil;
1142 Lisp_Object abspath;
1143
1144 CHECK_STRING (filename, 0);
1145 abspath = Fexpand_file_name (filename, Qnil);
1146
1147 fp = fopen (XSTRING (filename)->data, "rt");
1148 if (fp)
1149 {
1150 char buf[512];
1151 int red, green, blue;
1152 int num;
1153
1154 BLOCK_INPUT;
1155
1156 while (fgets (buf, sizeof (buf), fp) != NULL) {
1157 if (sscanf (buf, "%u %u %u %n", &red, &green, &blue, &num) == 3)
1158 {
1159 char *name = buf + num;
1160 num = strlen (name) - 1;
1161 if (name[num] == '\n')
1162 name[num] = 0;
1163 cmap = Fcons (Fcons (build_string (name),
1164 make_number (RGB (red, green, blue))),
1165 cmap);
1166 }
1167 }
1168 fclose (fp);
1169
1170 UNBLOCK_INPUT;
1171 }
1172
1173 return cmap;
1174}
ee78dc32 1175
fbd6baed 1176/* The default colors for the w32 color map */
ee78dc32
GV
1177typedef struct colormap_t
1178{
1179 char *name;
1180 COLORREF colorref;
1181} colormap_t;
1182
fbd6baed 1183colormap_t w32_color_map[] =
ee78dc32 1184{
1da8a614
GV
1185 {"snow" , PALETTERGB (255,250,250)},
1186 {"ghost white" , PALETTERGB (248,248,255)},
1187 {"GhostWhite" , PALETTERGB (248,248,255)},
1188 {"white smoke" , PALETTERGB (245,245,245)},
1189 {"WhiteSmoke" , PALETTERGB (245,245,245)},
1190 {"gainsboro" , PALETTERGB (220,220,220)},
1191 {"floral white" , PALETTERGB (255,250,240)},
1192 {"FloralWhite" , PALETTERGB (255,250,240)},
1193 {"old lace" , PALETTERGB (253,245,230)},
1194 {"OldLace" , PALETTERGB (253,245,230)},
1195 {"linen" , PALETTERGB (250,240,230)},
1196 {"antique white" , PALETTERGB (250,235,215)},
1197 {"AntiqueWhite" , PALETTERGB (250,235,215)},
1198 {"papaya whip" , PALETTERGB (255,239,213)},
1199 {"PapayaWhip" , PALETTERGB (255,239,213)},
1200 {"blanched almond" , PALETTERGB (255,235,205)},
1201 {"BlanchedAlmond" , PALETTERGB (255,235,205)},
1202 {"bisque" , PALETTERGB (255,228,196)},
1203 {"peach puff" , PALETTERGB (255,218,185)},
1204 {"PeachPuff" , PALETTERGB (255,218,185)},
1205 {"navajo white" , PALETTERGB (255,222,173)},
1206 {"NavajoWhite" , PALETTERGB (255,222,173)},
1207 {"moccasin" , PALETTERGB (255,228,181)},
1208 {"cornsilk" , PALETTERGB (255,248,220)},
1209 {"ivory" , PALETTERGB (255,255,240)},
1210 {"lemon chiffon" , PALETTERGB (255,250,205)},
1211 {"LemonChiffon" , PALETTERGB (255,250,205)},
1212 {"seashell" , PALETTERGB (255,245,238)},
1213 {"honeydew" , PALETTERGB (240,255,240)},
1214 {"mint cream" , PALETTERGB (245,255,250)},
1215 {"MintCream" , PALETTERGB (245,255,250)},
1216 {"azure" , PALETTERGB (240,255,255)},
1217 {"alice blue" , PALETTERGB (240,248,255)},
1218 {"AliceBlue" , PALETTERGB (240,248,255)},
1219 {"lavender" , PALETTERGB (230,230,250)},
1220 {"lavender blush" , PALETTERGB (255,240,245)},
1221 {"LavenderBlush" , PALETTERGB (255,240,245)},
1222 {"misty rose" , PALETTERGB (255,228,225)},
1223 {"MistyRose" , PALETTERGB (255,228,225)},
1224 {"white" , PALETTERGB (255,255,255)},
1225 {"black" , PALETTERGB ( 0, 0, 0)},
1226 {"dark slate gray" , PALETTERGB ( 47, 79, 79)},
1227 {"DarkSlateGray" , PALETTERGB ( 47, 79, 79)},
1228 {"dark slate grey" , PALETTERGB ( 47, 79, 79)},
1229 {"DarkSlateGrey" , PALETTERGB ( 47, 79, 79)},
1230 {"dim gray" , PALETTERGB (105,105,105)},
1231 {"DimGray" , PALETTERGB (105,105,105)},
1232 {"dim grey" , PALETTERGB (105,105,105)},
1233 {"DimGrey" , PALETTERGB (105,105,105)},
1234 {"slate gray" , PALETTERGB (112,128,144)},
1235 {"SlateGray" , PALETTERGB (112,128,144)},
1236 {"slate grey" , PALETTERGB (112,128,144)},
1237 {"SlateGrey" , PALETTERGB (112,128,144)},
1238 {"light slate gray" , PALETTERGB (119,136,153)},
1239 {"LightSlateGray" , PALETTERGB (119,136,153)},
1240 {"light slate grey" , PALETTERGB (119,136,153)},
1241 {"LightSlateGrey" , PALETTERGB (119,136,153)},
1242 {"gray" , PALETTERGB (190,190,190)},
1243 {"grey" , PALETTERGB (190,190,190)},
1244 {"light grey" , PALETTERGB (211,211,211)},
1245 {"LightGrey" , PALETTERGB (211,211,211)},
1246 {"light gray" , PALETTERGB (211,211,211)},
1247 {"LightGray" , PALETTERGB (211,211,211)},
1248 {"midnight blue" , PALETTERGB ( 25, 25,112)},
1249 {"MidnightBlue" , PALETTERGB ( 25, 25,112)},
1250 {"navy" , PALETTERGB ( 0, 0,128)},
1251 {"navy blue" , PALETTERGB ( 0, 0,128)},
1252 {"NavyBlue" , PALETTERGB ( 0, 0,128)},
1253 {"cornflower blue" , PALETTERGB (100,149,237)},
1254 {"CornflowerBlue" , PALETTERGB (100,149,237)},
1255 {"dark slate blue" , PALETTERGB ( 72, 61,139)},
1256 {"DarkSlateBlue" , PALETTERGB ( 72, 61,139)},
1257 {"slate blue" , PALETTERGB (106, 90,205)},
1258 {"SlateBlue" , PALETTERGB (106, 90,205)},
1259 {"medium slate blue" , PALETTERGB (123,104,238)},
1260 {"MediumSlateBlue" , PALETTERGB (123,104,238)},
1261 {"light slate blue" , PALETTERGB (132,112,255)},
1262 {"LightSlateBlue" , PALETTERGB (132,112,255)},
1263 {"medium blue" , PALETTERGB ( 0, 0,205)},
1264 {"MediumBlue" , PALETTERGB ( 0, 0,205)},
1265 {"royal blue" , PALETTERGB ( 65,105,225)},
1266 {"RoyalBlue" , PALETTERGB ( 65,105,225)},
1267 {"blue" , PALETTERGB ( 0, 0,255)},
1268 {"dodger blue" , PALETTERGB ( 30,144,255)},
1269 {"DodgerBlue" , PALETTERGB ( 30,144,255)},
1270 {"deep sky blue" , PALETTERGB ( 0,191,255)},
1271 {"DeepSkyBlue" , PALETTERGB ( 0,191,255)},
1272 {"sky blue" , PALETTERGB (135,206,235)},
1273 {"SkyBlue" , PALETTERGB (135,206,235)},
1274 {"light sky blue" , PALETTERGB (135,206,250)},
1275 {"LightSkyBlue" , PALETTERGB (135,206,250)},
1276 {"steel blue" , PALETTERGB ( 70,130,180)},
1277 {"SteelBlue" , PALETTERGB ( 70,130,180)},
1278 {"light steel blue" , PALETTERGB (176,196,222)},
1279 {"LightSteelBlue" , PALETTERGB (176,196,222)},
1280 {"light blue" , PALETTERGB (173,216,230)},
1281 {"LightBlue" , PALETTERGB (173,216,230)},
1282 {"powder blue" , PALETTERGB (176,224,230)},
1283 {"PowderBlue" , PALETTERGB (176,224,230)},
1284 {"pale turquoise" , PALETTERGB (175,238,238)},
1285 {"PaleTurquoise" , PALETTERGB (175,238,238)},
1286 {"dark turquoise" , PALETTERGB ( 0,206,209)},
1287 {"DarkTurquoise" , PALETTERGB ( 0,206,209)},
1288 {"medium turquoise" , PALETTERGB ( 72,209,204)},
1289 {"MediumTurquoise" , PALETTERGB ( 72,209,204)},
1290 {"turquoise" , PALETTERGB ( 64,224,208)},
1291 {"cyan" , PALETTERGB ( 0,255,255)},
1292 {"light cyan" , PALETTERGB (224,255,255)},
1293 {"LightCyan" , PALETTERGB (224,255,255)},
1294 {"cadet blue" , PALETTERGB ( 95,158,160)},
1295 {"CadetBlue" , PALETTERGB ( 95,158,160)},
1296 {"medium aquamarine" , PALETTERGB (102,205,170)},
1297 {"MediumAquamarine" , PALETTERGB (102,205,170)},
1298 {"aquamarine" , PALETTERGB (127,255,212)},
1299 {"dark green" , PALETTERGB ( 0,100, 0)},
1300 {"DarkGreen" , PALETTERGB ( 0,100, 0)},
1301 {"dark olive green" , PALETTERGB ( 85,107, 47)},
1302 {"DarkOliveGreen" , PALETTERGB ( 85,107, 47)},
1303 {"dark sea green" , PALETTERGB (143,188,143)},
1304 {"DarkSeaGreen" , PALETTERGB (143,188,143)},
1305 {"sea green" , PALETTERGB ( 46,139, 87)},
1306 {"SeaGreen" , PALETTERGB ( 46,139, 87)},
1307 {"medium sea green" , PALETTERGB ( 60,179,113)},
1308 {"MediumSeaGreen" , PALETTERGB ( 60,179,113)},
1309 {"light sea green" , PALETTERGB ( 32,178,170)},
1310 {"LightSeaGreen" , PALETTERGB ( 32,178,170)},
1311 {"pale green" , PALETTERGB (152,251,152)},
1312 {"PaleGreen" , PALETTERGB (152,251,152)},
1313 {"spring green" , PALETTERGB ( 0,255,127)},
1314 {"SpringGreen" , PALETTERGB ( 0,255,127)},
1315 {"lawn green" , PALETTERGB (124,252, 0)},
1316 {"LawnGreen" , PALETTERGB (124,252, 0)},
1317 {"green" , PALETTERGB ( 0,255, 0)},
1318 {"chartreuse" , PALETTERGB (127,255, 0)},
1319 {"medium spring green" , PALETTERGB ( 0,250,154)},
1320 {"MediumSpringGreen" , PALETTERGB ( 0,250,154)},
1321 {"green yellow" , PALETTERGB (173,255, 47)},
1322 {"GreenYellow" , PALETTERGB (173,255, 47)},
1323 {"lime green" , PALETTERGB ( 50,205, 50)},
1324 {"LimeGreen" , PALETTERGB ( 50,205, 50)},
1325 {"yellow green" , PALETTERGB (154,205, 50)},
1326 {"YellowGreen" , PALETTERGB (154,205, 50)},
1327 {"forest green" , PALETTERGB ( 34,139, 34)},
1328 {"ForestGreen" , PALETTERGB ( 34,139, 34)},
1329 {"olive drab" , PALETTERGB (107,142, 35)},
1330 {"OliveDrab" , PALETTERGB (107,142, 35)},
1331 {"dark khaki" , PALETTERGB (189,183,107)},
1332 {"DarkKhaki" , PALETTERGB (189,183,107)},
1333 {"khaki" , PALETTERGB (240,230,140)},
1334 {"pale goldenrod" , PALETTERGB (238,232,170)},
1335 {"PaleGoldenrod" , PALETTERGB (238,232,170)},
1336 {"light goldenrod yellow" , PALETTERGB (250,250,210)},
1337 {"LightGoldenrodYellow" , PALETTERGB (250,250,210)},
1338 {"light yellow" , PALETTERGB (255,255,224)},
1339 {"LightYellow" , PALETTERGB (255,255,224)},
1340 {"yellow" , PALETTERGB (255,255, 0)},
1341 {"gold" , PALETTERGB (255,215, 0)},
1342 {"light goldenrod" , PALETTERGB (238,221,130)},
1343 {"LightGoldenrod" , PALETTERGB (238,221,130)},
1344 {"goldenrod" , PALETTERGB (218,165, 32)},
1345 {"dark goldenrod" , PALETTERGB (184,134, 11)},
1346 {"DarkGoldenrod" , PALETTERGB (184,134, 11)},
1347 {"rosy brown" , PALETTERGB (188,143,143)},
1348 {"RosyBrown" , PALETTERGB (188,143,143)},
1349 {"indian red" , PALETTERGB (205, 92, 92)},
1350 {"IndianRed" , PALETTERGB (205, 92, 92)},
1351 {"saddle brown" , PALETTERGB (139, 69, 19)},
1352 {"SaddleBrown" , PALETTERGB (139, 69, 19)},
1353 {"sienna" , PALETTERGB (160, 82, 45)},
1354 {"peru" , PALETTERGB (205,133, 63)},
1355 {"burlywood" , PALETTERGB (222,184,135)},
1356 {"beige" , PALETTERGB (245,245,220)},
1357 {"wheat" , PALETTERGB (245,222,179)},
1358 {"sandy brown" , PALETTERGB (244,164, 96)},
1359 {"SandyBrown" , PALETTERGB (244,164, 96)},
1360 {"tan" , PALETTERGB (210,180,140)},
1361 {"chocolate" , PALETTERGB (210,105, 30)},
1362 {"firebrick" , PALETTERGB (178,34, 34)},
1363 {"brown" , PALETTERGB (165,42, 42)},
1364 {"dark salmon" , PALETTERGB (233,150,122)},
1365 {"DarkSalmon" , PALETTERGB (233,150,122)},
1366 {"salmon" , PALETTERGB (250,128,114)},
1367 {"light salmon" , PALETTERGB (255,160,122)},
1368 {"LightSalmon" , PALETTERGB (255,160,122)},
1369 {"orange" , PALETTERGB (255,165, 0)},
1370 {"dark orange" , PALETTERGB (255,140, 0)},
1371 {"DarkOrange" , PALETTERGB (255,140, 0)},
1372 {"coral" , PALETTERGB (255,127, 80)},
1373 {"light coral" , PALETTERGB (240,128,128)},
1374 {"LightCoral" , PALETTERGB (240,128,128)},
1375 {"tomato" , PALETTERGB (255, 99, 71)},
1376 {"orange red" , PALETTERGB (255, 69, 0)},
1377 {"OrangeRed" , PALETTERGB (255, 69, 0)},
1378 {"red" , PALETTERGB (255, 0, 0)},
1379 {"hot pink" , PALETTERGB (255,105,180)},
1380 {"HotPink" , PALETTERGB (255,105,180)},
1381 {"deep pink" , PALETTERGB (255, 20,147)},
1382 {"DeepPink" , PALETTERGB (255, 20,147)},
1383 {"pink" , PALETTERGB (255,192,203)},
1384 {"light pink" , PALETTERGB (255,182,193)},
1385 {"LightPink" , PALETTERGB (255,182,193)},
1386 {"pale violet red" , PALETTERGB (219,112,147)},
1387 {"PaleVioletRed" , PALETTERGB (219,112,147)},
1388 {"maroon" , PALETTERGB (176, 48, 96)},
1389 {"medium violet red" , PALETTERGB (199, 21,133)},
1390 {"MediumVioletRed" , PALETTERGB (199, 21,133)},
1391 {"violet red" , PALETTERGB (208, 32,144)},
1392 {"VioletRed" , PALETTERGB (208, 32,144)},
1393 {"magenta" , PALETTERGB (255, 0,255)},
1394 {"violet" , PALETTERGB (238,130,238)},
1395 {"plum" , PALETTERGB (221,160,221)},
1396 {"orchid" , PALETTERGB (218,112,214)},
1397 {"medium orchid" , PALETTERGB (186, 85,211)},
1398 {"MediumOrchid" , PALETTERGB (186, 85,211)},
1399 {"dark orchid" , PALETTERGB (153, 50,204)},
1400 {"DarkOrchid" , PALETTERGB (153, 50,204)},
1401 {"dark violet" , PALETTERGB (148, 0,211)},
1402 {"DarkViolet" , PALETTERGB (148, 0,211)},
1403 {"blue violet" , PALETTERGB (138, 43,226)},
1404 {"BlueViolet" , PALETTERGB (138, 43,226)},
1405 {"purple" , PALETTERGB (160, 32,240)},
1406 {"medium purple" , PALETTERGB (147,112,219)},
1407 {"MediumPurple" , PALETTERGB (147,112,219)},
1408 {"thistle" , PALETTERGB (216,191,216)},
1409 {"gray0" , PALETTERGB ( 0, 0, 0)},
1410 {"grey0" , PALETTERGB ( 0, 0, 0)},
1411 {"dark grey" , PALETTERGB (169,169,169)},
1412 {"DarkGrey" , PALETTERGB (169,169,169)},
1413 {"dark gray" , PALETTERGB (169,169,169)},
1414 {"DarkGray" , PALETTERGB (169,169,169)},
1415 {"dark blue" , PALETTERGB ( 0, 0,139)},
1416 {"DarkBlue" , PALETTERGB ( 0, 0,139)},
1417 {"dark cyan" , PALETTERGB ( 0,139,139)},
1418 {"DarkCyan" , PALETTERGB ( 0,139,139)},
1419 {"dark magenta" , PALETTERGB (139, 0,139)},
1420 {"DarkMagenta" , PALETTERGB (139, 0,139)},
1421 {"dark red" , PALETTERGB (139, 0, 0)},
1422 {"DarkRed" , PALETTERGB (139, 0, 0)},
1423 {"light green" , PALETTERGB (144,238,144)},
1424 {"LightGreen" , PALETTERGB (144,238,144)},
ee78dc32
GV
1425};
1426
fbd6baed 1427DEFUN ("w32-default-color-map", Fw32_default_color_map, Sw32_default_color_map,
ee78dc32
GV
1428 0, 0, 0, "Return the default color map.")
1429 ()
1430{
1431 int i;
fbd6baed 1432 colormap_t *pc = w32_color_map;
ee78dc32
GV
1433 Lisp_Object cmap;
1434
1435 BLOCK_INPUT;
1436
1437 cmap = Qnil;
1438
fbd6baed 1439 for (i = 0; i < sizeof (w32_color_map) / sizeof (w32_color_map[0]);
ee78dc32
GV
1440 pc++, i++)
1441 cmap = Fcons (Fcons (build_string (pc->name),
1442 make_number (pc->colorref)),
1443 cmap);
1444
1445 UNBLOCK_INPUT;
1446
1447 return (cmap);
1448}
ee78dc32
GV
1449
1450Lisp_Object
fbd6baed 1451w32_to_x_color (rgb)
ee78dc32
GV
1452 Lisp_Object rgb;
1453{
1454 Lisp_Object color;
1455
1456 CHECK_NUMBER (rgb, 0);
1457
1458 BLOCK_INPUT;
1459
fbd6baed 1460 color = Frassq (rgb, Vw32_color_map);
ee78dc32
GV
1461
1462 UNBLOCK_INPUT;
1463
1464 if (!NILP (color))
1465 return (Fcar (color));
1466 else
1467 return Qnil;
1468}
1469
5d7fed93
GV
1470COLORREF
1471w32_color_map_lookup (colorname)
1472 char *colorname;
1473{
1474 Lisp_Object tail, ret = Qnil;
1475
1476 BLOCK_INPUT;
1477
1478 for (tail = Vw32_color_map; !NILP (tail); tail = Fcdr (tail))
1479 {
1480 register Lisp_Object elt, tem;
1481
1482 elt = Fcar (tail);
1483 if (!CONSP (elt)) continue;
1484
1485 tem = Fcar (elt);
1486
1487 if (lstrcmpi (XSTRING (tem)->data, colorname) == 0)
1488 {
1489 ret = XUINT (Fcdr (elt));
1490 break;
1491 }
1492
1493 QUIT;
1494 }
1495
1496
1497 UNBLOCK_INPUT;
1498
1499 return ret;
1500}
1501
ee78dc32 1502COLORREF
fbd6baed 1503x_to_w32_color (colorname)
ee78dc32
GV
1504 char * colorname;
1505{
1506 register Lisp_Object tail, ret = Qnil;
1507
1508 BLOCK_INPUT;
1edf84e7
GV
1509
1510 if (colorname[0] == '#')
1511 {
1512 /* Could be an old-style RGB Device specification. */
1513 char *color;
1514 int size;
1515 color = colorname + 1;
1516
1517 size = strlen(color);
1518 if (size == 3 || size == 6 || size == 9 || size == 12)
1519 {
1520 UINT colorval;
1521 int i, pos;
1522 pos = 0;
1523 size /= 3;
1524 colorval = 0;
1525
1526 for (i = 0; i < 3; i++)
1527 {
1528 char *end;
1529 char t;
1530 unsigned long value;
1531
1532 /* The check for 'x' in the following conditional takes into
1533 account the fact that strtol allows a "0x" in front of
1534 our numbers, and we don't. */
1535 if (!isxdigit(color[0]) || color[1] == 'x')
1536 break;
1537 t = color[size];
1538 color[size] = '\0';
1539 value = strtoul(color, &end, 16);
1540 color[size] = t;
1541 if (errno == ERANGE || end - color != size)
1542 break;
1543 switch (size)
1544 {
1545 case 1:
1546 value = value * 0x10;
1547 break;
1548 case 2:
1549 break;
1550 case 3:
1551 value /= 0x10;
1552 break;
1553 case 4:
1554 value /= 0x100;
1555 break;
1556 }
1557 colorval |= (value << pos);
1558 pos += 0x8;
1559 if (i == 2)
1560 {
1561 UNBLOCK_INPUT;
1562 return (colorval);
1563 }
1564 color = end;
1565 }
1566 }
1567 }
1568 else if (strnicmp(colorname, "rgb:", 4) == 0)
1569 {
1570 char *color;
1571 UINT colorval;
1572 int i, pos;
1573 pos = 0;
1574
1575 colorval = 0;
1576 color = colorname + 4;
1577 for (i = 0; i < 3; i++)
1578 {
1579 char *end;
1580 unsigned long value;
1581
1582 /* The check for 'x' in the following conditional takes into
1583 account the fact that strtol allows a "0x" in front of
1584 our numbers, and we don't. */
1585 if (!isxdigit(color[0]) || color[1] == 'x')
1586 break;
1587 value = strtoul(color, &end, 16);
1588 if (errno == ERANGE)
1589 break;
1590 switch (end - color)
1591 {
1592 case 1:
1593 value = value * 0x10 + value;
1594 break;
1595 case 2:
1596 break;
1597 case 3:
1598 value /= 0x10;
1599 break;
1600 case 4:
1601 value /= 0x100;
1602 break;
1603 default:
1604 value = ULONG_MAX;
1605 }
1606 if (value == ULONG_MAX)
1607 break;
1608 colorval |= (value << pos);
1609 pos += 0x8;
1610 if (i == 2)
1611 {
1612 if (*end != '\0')
1613 break;
1614 UNBLOCK_INPUT;
1615 return (colorval);
1616 }
1617 if (*end != '/')
1618 break;
1619 color = end + 1;
1620 }
1621 }
1622 else if (strnicmp(colorname, "rgbi:", 5) == 0)
1623 {
1624 /* This is an RGB Intensity specification. */
1625 char *color;
1626 UINT colorval;
1627 int i, pos;
1628 pos = 0;
1629
1630 colorval = 0;
1631 color = colorname + 5;
1632 for (i = 0; i < 3; i++)
1633 {
1634 char *end;
1635 double value;
1636 UINT val;
1637
1638 value = strtod(color, &end);
1639 if (errno == ERANGE)
1640 break;
1641 if (value < 0.0 || value > 1.0)
1642 break;
1643 val = (UINT)(0x100 * value);
1644 /* We used 0x100 instead of 0xFF to give an continuous
1645 range between 0.0 and 1.0 inclusive. The next statement
1646 fixes the 1.0 case. */
1647 if (val == 0x100)
1648 val = 0xFF;
1649 colorval |= (val << pos);
1650 pos += 0x8;
1651 if (i == 2)
1652 {
1653 if (*end != '\0')
1654 break;
1655 UNBLOCK_INPUT;
1656 return (colorval);
1657 }
1658 if (*end != '/')
1659 break;
1660 color = end + 1;
1661 }
1662 }
1663 /* I am not going to attempt to handle any of the CIE color schemes
1664 or TekHVC, since I don't know the algorithms for conversion to
1665 RGB. */
f695b4b1
GV
1666
1667 /* If we fail to lookup the color name in w32_color_map, then check the
1668 colorname to see if it can be crudely approximated: If the X color
1669 ends in a number (e.g., "darkseagreen2"), strip the number and
1670 return the result of looking up the base color name. */
1671 ret = w32_color_map_lookup (colorname);
1672 if (NILP (ret))
ee78dc32 1673 {
f695b4b1 1674 int len = strlen (colorname);
ee78dc32 1675
f695b4b1
GV
1676 if (isdigit (colorname[len - 1]))
1677 {
1678 char *ptr, *approx = alloca (len);
ee78dc32 1679
f695b4b1
GV
1680 strcpy (approx, colorname);
1681 ptr = &approx[len - 1];
1682 while (ptr > approx && isdigit (*ptr))
1683 *ptr-- = '\0';
ee78dc32 1684
f695b4b1 1685 ret = w32_color_map_lookup (approx);
ee78dc32 1686 }
ee78dc32
GV
1687 }
1688
1689 UNBLOCK_INPUT;
ee78dc32
GV
1690 return ret;
1691}
1692
5ac45f98
GV
1693
1694void
fbd6baed 1695w32_regenerate_palette (FRAME_PTR f)
5ac45f98 1696{
fbd6baed 1697 struct w32_palette_entry * list;
5ac45f98
GV
1698 LOGPALETTE * log_palette;
1699 HPALETTE new_palette;
1700 int i;
1701
1702 /* don't bother trying to create palette if not supported */
fbd6baed 1703 if (! FRAME_W32_DISPLAY_INFO (f)->has_palette)
5ac45f98
GV
1704 return;
1705
1706 log_palette = (LOGPALETTE *)
1707 alloca (sizeof (LOGPALETTE) +
fbd6baed 1708 FRAME_W32_DISPLAY_INFO (f)->num_colors * sizeof (PALETTEENTRY));
5ac45f98 1709 log_palette->palVersion = 0x300;
fbd6baed 1710 log_palette->palNumEntries = FRAME_W32_DISPLAY_INFO (f)->num_colors;
5ac45f98 1711
fbd6baed 1712 list = FRAME_W32_DISPLAY_INFO (f)->color_list;
5ac45f98 1713 for (i = 0;
fbd6baed 1714 i < FRAME_W32_DISPLAY_INFO (f)->num_colors;
5ac45f98
GV
1715 i++, list = list->next)
1716 log_palette->palPalEntry[i] = list->entry;
1717
1718 new_palette = CreatePalette (log_palette);
1719
1720 enter_crit ();
1721
fbd6baed
GV
1722 if (FRAME_W32_DISPLAY_INFO (f)->palette)
1723 DeleteObject (FRAME_W32_DISPLAY_INFO (f)->palette);
1724 FRAME_W32_DISPLAY_INFO (f)->palette = new_palette;
5ac45f98
GV
1725
1726 /* Realize display palette and garbage all frames. */
1727 release_frame_dc (f, get_frame_dc (f));
1728
1729 leave_crit ();
1730}
1731
fbd6baed
GV
1732#define W32_COLOR(pe) RGB (pe.peRed, pe.peGreen, pe.peBlue)
1733#define SET_W32_COLOR(pe, color) \
5ac45f98
GV
1734 do \
1735 { \
1736 pe.peRed = GetRValue (color); \
1737 pe.peGreen = GetGValue (color); \
1738 pe.peBlue = GetBValue (color); \
1739 pe.peFlags = 0; \
1740 } while (0)
1741
1742#if 0
1743/* Keep these around in case we ever want to track color usage. */
1744void
fbd6baed 1745w32_map_color (FRAME_PTR f, COLORREF color)
5ac45f98 1746{
fbd6baed 1747 struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
5ac45f98 1748
fbd6baed 1749 if (NILP (Vw32_enable_palette))
5ac45f98
GV
1750 return;
1751
1752 /* check if color is already mapped */
1753 while (list)
1754 {
fbd6baed 1755 if (W32_COLOR (list->entry) == color)
5ac45f98
GV
1756 {
1757 ++list->refcount;
1758 return;
1759 }
1760 list = list->next;
1761 }
1762
1763 /* not already mapped, so add to list and recreate Windows palette */
fbd6baed
GV
1764 list = (struct w32_palette_entry *)
1765 xmalloc (sizeof (struct w32_palette_entry));
1766 SET_W32_COLOR (list->entry, color);
5ac45f98 1767 list->refcount = 1;
fbd6baed
GV
1768 list->next = FRAME_W32_DISPLAY_INFO (f)->color_list;
1769 FRAME_W32_DISPLAY_INFO (f)->color_list = list;
1770 FRAME_W32_DISPLAY_INFO (f)->num_colors++;
5ac45f98
GV
1771
1772 /* set flag that palette must be regenerated */
fbd6baed 1773 FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
5ac45f98
GV
1774}
1775
1776void
fbd6baed 1777w32_unmap_color (FRAME_PTR f, COLORREF color)
5ac45f98 1778{
fbd6baed
GV
1779 struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
1780 struct w32_palette_entry **prev = &FRAME_W32_DISPLAY_INFO (f)->color_list;
5ac45f98 1781
fbd6baed 1782 if (NILP (Vw32_enable_palette))
5ac45f98
GV
1783 return;
1784
1785 /* check if color is already mapped */
1786 while (list)
1787 {
fbd6baed 1788 if (W32_COLOR (list->entry) == color)
5ac45f98
GV
1789 {
1790 if (--list->refcount == 0)
1791 {
1792 *prev = list->next;
1793 xfree (list);
fbd6baed 1794 FRAME_W32_DISPLAY_INFO (f)->num_colors--;
5ac45f98
GV
1795 break;
1796 }
1797 else
1798 return;
1799 }
1800 prev = &list->next;
1801 list = list->next;
1802 }
1803
1804 /* set flag that palette must be regenerated */
fbd6baed 1805 FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
5ac45f98
GV
1806}
1807#endif
1808
6fc2811b
JR
1809
1810/* Gamma-correct COLOR on frame F. */
1811
1812void
1813gamma_correct (f, color)
1814 struct frame *f;
1815 COLORREF *color;
1816{
1817 if (f->gamma)
1818 {
1819 *color = PALETTERGB (
1820 pow (GetRValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
1821 pow (GetGValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
1822 pow (GetBValue (*color) / 255.0, f->gamma) * 255.0 + 0.5);
1823 }
1824}
1825
1826
ee78dc32
GV
1827/* Decide if color named COLOR is valid for the display associated with
1828 the selected frame; if so, return the rgb values in COLOR_DEF.
1829 If ALLOC is nonzero, allocate a new colormap cell. */
1830
1831int
6fc2811b 1832w32_defined_color (f, color, color_def, alloc)
ee78dc32
GV
1833 FRAME_PTR f;
1834 char *color;
6fc2811b 1835 XColor *color_def;
ee78dc32
GV
1836 int alloc;
1837{
1838 register Lisp_Object tem;
6fc2811b 1839 COLORREF w32_color_ref;
3c190163 1840
fbd6baed 1841 tem = x_to_w32_color (color);
3c190163 1842
ee78dc32
GV
1843 if (!NILP (tem))
1844 {
d88c567c
JR
1845 if (f)
1846 {
1847 /* Apply gamma correction. */
1848 w32_color_ref = XUINT (tem);
1849 gamma_correct (f, &w32_color_ref);
1850 XSETINT (tem, w32_color_ref);
1851 }
9badad41
JR
1852
1853 /* Map this color to the palette if it is enabled. */
fbd6baed 1854 if (!NILP (Vw32_enable_palette))
5ac45f98 1855 {
fbd6baed 1856 struct w32_palette_entry * entry =
d88c567c 1857 one_w32_display_info.color_list;
fbd6baed 1858 struct w32_palette_entry ** prev =
d88c567c 1859 &one_w32_display_info.color_list;
5ac45f98
GV
1860
1861 /* check if color is already mapped */
1862 while (entry)
1863 {
fbd6baed 1864 if (W32_COLOR (entry->entry) == XUINT (tem))
5ac45f98
GV
1865 break;
1866 prev = &entry->next;
1867 entry = entry->next;
1868 }
1869
1870 if (entry == NULL && alloc)
1871 {
1872 /* not already mapped, so add to list */
fbd6baed
GV
1873 entry = (struct w32_palette_entry *)
1874 xmalloc (sizeof (struct w32_palette_entry));
1875 SET_W32_COLOR (entry->entry, XUINT (tem));
5ac45f98
GV
1876 entry->next = NULL;
1877 *prev = entry;
d88c567c 1878 one_w32_display_info.num_colors++;
5ac45f98
GV
1879
1880 /* set flag that palette must be regenerated */
d88c567c 1881 one_w32_display_info.regen_palette = TRUE;
5ac45f98
GV
1882 }
1883 }
1884 /* Ensure COLORREF value is snapped to nearest color in (default)
1885 palette by simulating the PALETTERGB macro. This works whether
1886 or not the display device has a palette. */
6fc2811b
JR
1887 w32_color_ref = XUINT (tem) | 0x2000000;
1888
6fc2811b
JR
1889 color_def->pixel = w32_color_ref;
1890 color_def->red = GetRValue (w32_color_ref);
1891 color_def->green = GetGValue (w32_color_ref);
1892 color_def->blue = GetBValue (w32_color_ref);
1893
ee78dc32 1894 return 1;
5ac45f98 1895 }
7fb46567 1896 else
3c190163
GV
1897 {
1898 return 0;
1899 }
ee78dc32
GV
1900}
1901
1902/* Given a string ARG naming a color, compute a pixel value from it
1903 suitable for screen F.
1904 If F is not a color screen, return DEF (default) regardless of what
1905 ARG says. */
1906
1907int
1908x_decode_color (f, arg, def)
1909 FRAME_PTR f;
1910 Lisp_Object arg;
1911 int def;
1912{
6fc2811b 1913 XColor cdef;
ee78dc32
GV
1914
1915 CHECK_STRING (arg, 0);
1916
1917 if (strcmp (XSTRING (arg)->data, "black") == 0)
1918 return BLACK_PIX_DEFAULT (f);
1919 else if (strcmp (XSTRING (arg)->data, "white") == 0)
1920 return WHITE_PIX_DEFAULT (f);
1921
fbd6baed 1922 if ((FRAME_W32_DISPLAY_INFO (f)->n_planes * FRAME_W32_DISPLAY_INFO (f)->n_cbits) == 1)
ee78dc32
GV
1923 return def;
1924
6fc2811b 1925 /* w32_defined_color is responsible for coping with failures
ee78dc32 1926 by looking for a near-miss. */
6fc2811b
JR
1927 if (w32_defined_color (f, XSTRING (arg)->data, &cdef, 1))
1928 return cdef.pixel;
ee78dc32
GV
1929
1930 /* defined_color failed; return an ultimate default. */
1931 return def;
1932}
1933\f
dfff8a69
JR
1934/* Change the `line-spacing' frame parameter of frame F. OLD_VALUE is
1935 the previous value of that parameter, NEW_VALUE is the new value. */
1936
1937static void
1938x_set_line_spacing (f, new_value, old_value)
1939 struct frame *f;
1940 Lisp_Object new_value, old_value;
1941{
1942 if (NILP (new_value))
1943 f->extra_line_spacing = 0;
1944 else if (NATNUMP (new_value))
1945 f->extra_line_spacing = XFASTINT (new_value);
1946 else
1a948b17 1947 Fsignal (Qerror, Fcons (build_string ("Invalid line-spacing"),
dfff8a69
JR
1948 Fcons (new_value, Qnil)));
1949 if (FRAME_VISIBLE_P (f))
1950 redraw_frame (f);
1951}
1952
1953
6fc2811b
JR
1954/* Change the `screen-gamma' frame parameter of frame F. OLD_VALUE is
1955 the previous value of that parameter, NEW_VALUE is the new value. */
1956
1957static void
1958x_set_screen_gamma (f, new_value, old_value)
1959 struct frame *f;
1960 Lisp_Object new_value, old_value;
1961{
1962 if (NILP (new_value))
1963 f->gamma = 0;
1964 else if (NUMBERP (new_value) && XFLOATINT (new_value) > 0)
1965 /* The value 0.4545 is the normal viewing gamma. */
1966 f->gamma = 1.0 / (0.4545 * XFLOATINT (new_value));
1967 else
1a948b17 1968 Fsignal (Qerror, Fcons (build_string ("Invalid screen-gamma"),
6fc2811b
JR
1969 Fcons (new_value, Qnil)));
1970
1971 clear_face_cache (0);
1972}
1973
1974
ee78dc32
GV
1975/* Functions called only from `x_set_frame_param'
1976 to set individual parameters.
1977
fbd6baed 1978 If FRAME_W32_WINDOW (f) is 0,
ee78dc32
GV
1979 the frame is being created and its window does not exist yet.
1980 In that case, just record the parameter's new value
1981 in the standard place; do not attempt to change the window. */
1982
1983void
1984x_set_foreground_color (f, arg, oldval)
1985 struct frame *f;
1986 Lisp_Object arg, oldval;
1987{
6fc2811b 1988 FRAME_FOREGROUND_PIXEL (f)
ee78dc32 1989 = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
5ac45f98 1990
fbd6baed 1991 if (FRAME_W32_WINDOW (f) != 0)
ee78dc32 1992 {
6fc2811b 1993 update_face_from_frame_parameter (f, Qforeground_color, arg);
ee78dc32
GV
1994 if (FRAME_VISIBLE_P (f))
1995 redraw_frame (f);
1996 }
1997}
1998
1999void
2000x_set_background_color (f, arg, oldval)
2001 struct frame *f;
2002 Lisp_Object arg, oldval;
2003{
6fc2811b 2004 FRAME_BACKGROUND_PIXEL (f)
ee78dc32
GV
2005 = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
2006
fbd6baed 2007 if (FRAME_W32_WINDOW (f) != 0)
ee78dc32 2008 {
6fc2811b
JR
2009 SetWindowLong (FRAME_W32_WINDOW (f), WND_BACKGROUND_INDEX,
2010 FRAME_BACKGROUND_PIXEL (f));
ee78dc32 2011
6fc2811b 2012 update_face_from_frame_parameter (f, Qbackground_color, arg);
ee78dc32
GV
2013
2014 if (FRAME_VISIBLE_P (f))
2015 redraw_frame (f);
2016 }
2017}
2018
2019void
2020x_set_mouse_color (f, arg, oldval)
2021 struct frame *f;
2022 Lisp_Object arg, oldval;
2023{
6fc2811b 2024
ee78dc32 2025 Cursor cursor, nontext_cursor, mode_cursor, cross_cursor;
dfc465d3 2026 int count;
ee78dc32
GV
2027 int mask_color;
2028
2029 if (!EQ (Qnil, arg))
fbd6baed 2030 f->output_data.w32->mouse_pixel
ee78dc32 2031 = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
6fc2811b
JR
2032 mask_color = FRAME_BACKGROUND_PIXEL (f);
2033
2034 /* Don't let pointers be invisible. */
fbd6baed 2035 if (mask_color == f->output_data.w32->mouse_pixel
6fc2811b
JR
2036 && mask_color == FRAME_BACKGROUND_PIXEL (f))
2037 f->output_data.w32->mouse_pixel = FRAME_FOREGROUND_PIXEL (f);
ee78dc32 2038
6fc2811b 2039#if 0 /* NTEMACS_TODO : cursor changes */
ee78dc32
GV
2040 BLOCK_INPUT;
2041
2042 /* It's not okay to crash if the user selects a screwy cursor. */
fadca6c6 2043 count = x_catch_errors (FRAME_W32_DISPLAY (f));
ee78dc32
GV
2044
2045 if (!EQ (Qnil, Vx_pointer_shape))
2046 {
2047 CHECK_NUMBER (Vx_pointer_shape, 0);
fbd6baed 2048 cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XINT (Vx_pointer_shape));
ee78dc32
GV
2049 }
2050 else
fbd6baed
GV
2051 cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
2052 x_check_errors (FRAME_W32_DISPLAY (f), "bad text pointer cursor: %s");
ee78dc32
GV
2053
2054 if (!EQ (Qnil, Vx_nontext_pointer_shape))
2055 {
2056 CHECK_NUMBER (Vx_nontext_pointer_shape, 0);
fbd6baed 2057 nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
ee78dc32
GV
2058 XINT (Vx_nontext_pointer_shape));
2059 }
2060 else
fbd6baed
GV
2061 nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_left_ptr);
2062 x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
ee78dc32 2063
6fc2811b
JR
2064 if (!EQ (Qnil, Vx_busy_pointer_shape))
2065 {
2066 CHECK_NUMBER (Vx_busy_pointer_shape, 0);
2067 busy_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
2068 XINT (Vx_busy_pointer_shape));
2069 }
2070 else
2071 busy_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_watch);
2072 x_check_errors (FRAME_W32_DISPLAY (f), "bad busy pointer cursor: %s");
2073
2074 x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
ee78dc32
GV
2075 if (!EQ (Qnil, Vx_mode_pointer_shape))
2076 {
2077 CHECK_NUMBER (Vx_mode_pointer_shape, 0);
fbd6baed 2078 mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
ee78dc32
GV
2079 XINT (Vx_mode_pointer_shape));
2080 }
2081 else
fbd6baed
GV
2082 mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
2083 x_check_errors (FRAME_W32_DISPLAY (f), "bad modeline pointer cursor: %s");
ee78dc32
GV
2084
2085 if (!EQ (Qnil, Vx_sensitive_text_pointer_shape))
2086 {
2087 CHECK_NUMBER (Vx_sensitive_text_pointer_shape, 0);
2088 cross_cursor
fbd6baed 2089 = XCreateFontCursor (FRAME_W32_DISPLAY (f),
ee78dc32
GV
2090 XINT (Vx_sensitive_text_pointer_shape));
2091 }
2092 else
fbd6baed 2093 cross_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_crosshair);
ee78dc32
GV
2094
2095 /* Check and report errors with the above calls. */
fbd6baed 2096 x_check_errors (FRAME_W32_DISPLAY (f), "can't set cursor shape: %s");
fadca6c6 2097 x_uncatch_errors (FRAME_W32_DISPLAY (f), count);
ee78dc32
GV
2098
2099 {
2100 XColor fore_color, back_color;
2101
fbd6baed 2102 fore_color.pixel = f->output_data.w32->mouse_pixel;
ee78dc32 2103 back_color.pixel = mask_color;
fbd6baed
GV
2104 XQueryColor (FRAME_W32_DISPLAY (f),
2105 DefaultColormap (FRAME_W32_DISPLAY (f),
2106 DefaultScreen (FRAME_W32_DISPLAY (f))),
ee78dc32 2107 &fore_color);
fbd6baed
GV
2108 XQueryColor (FRAME_W32_DISPLAY (f),
2109 DefaultColormap (FRAME_W32_DISPLAY (f),
2110 DefaultScreen (FRAME_W32_DISPLAY (f))),
ee78dc32 2111 &back_color);
fbd6baed 2112 XRecolorCursor (FRAME_W32_DISPLAY (f), cursor,
ee78dc32 2113 &fore_color, &back_color);
fbd6baed 2114 XRecolorCursor (FRAME_W32_DISPLAY (f), nontext_cursor,
ee78dc32 2115 &fore_color, &back_color);
fbd6baed 2116 XRecolorCursor (FRAME_W32_DISPLAY (f), mode_cursor,
ee78dc32 2117 &fore_color, &back_color);
fbd6baed 2118 XRecolorCursor (FRAME_W32_DISPLAY (f), cross_cursor,
ee78dc32 2119 &fore_color, &back_color);
6fc2811b
JR
2120 XRecolorCursor (FRAME_W32_DISPLAY (f), busy_cursor,
2121 &fore_color, &back_color);
ee78dc32
GV
2122 }
2123
fbd6baed 2124 if (FRAME_W32_WINDOW (f) != 0)
6fc2811b 2125 XDefineCursor (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), cursor);
ee78dc32 2126
fbd6baed
GV
2127 if (cursor != f->output_data.w32->text_cursor && f->output_data.w32->text_cursor != 0)
2128 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->text_cursor);
2129 f->output_data.w32->text_cursor = cursor;
2130
2131 if (nontext_cursor != f->output_data.w32->nontext_cursor
2132 && f->output_data.w32->nontext_cursor != 0)
2133 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->nontext_cursor);
2134 f->output_data.w32->nontext_cursor = nontext_cursor;
2135
6fc2811b
JR
2136 if (busy_cursor != f->output_data.w32->busy_cursor
2137 && f->output_data.w32->busy_cursor != 0)
2138 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->busy_cursor);
2139 f->output_data.w32->busy_cursor = busy_cursor;
2140
fbd6baed
GV
2141 if (mode_cursor != f->output_data.w32->modeline_cursor
2142 && f->output_data.w32->modeline_cursor != 0)
2143 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->modeline_cursor);
2144 f->output_data.w32->modeline_cursor = mode_cursor;
6fc2811b 2145
fbd6baed
GV
2146 if (cross_cursor != f->output_data.w32->cross_cursor
2147 && f->output_data.w32->cross_cursor != 0)
2148 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->cross_cursor);
2149 f->output_data.w32->cross_cursor = cross_cursor;
2150
2151 XFlush (FRAME_W32_DISPLAY (f));
ee78dc32 2152 UNBLOCK_INPUT;
6fc2811b
JR
2153
2154 update_face_from_frame_parameter (f, Qmouse_color, arg);
2155#endif /* NTEMACS_TODO */
ee78dc32
GV
2156}
2157
2158void
2159x_set_cursor_color (f, arg, oldval)
2160 struct frame *f;
2161 Lisp_Object arg, oldval;
2162{
2163 unsigned long fore_pixel;
2164
dfff8a69 2165 if (!NILP (Vx_cursor_fore_pixel))
ee78dc32
GV
2166 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
2167 WHITE_PIX_DEFAULT (f));
2168 else
6fc2811b 2169 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
fbd6baed 2170 f->output_data.w32->cursor_pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
ee78dc32
GV
2171
2172 /* Make sure that the cursor color differs from the background color. */
6fc2811b 2173 if (f->output_data.w32->cursor_pixel == FRAME_BACKGROUND_PIXEL (f))
ee78dc32 2174 {
fbd6baed
GV
2175 f->output_data.w32->cursor_pixel = f->output_data.w32->mouse_pixel;
2176 if (f->output_data.w32->cursor_pixel == fore_pixel)
6fc2811b 2177 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
ee78dc32 2178 }
6fc2811b 2179 FRAME_FOREGROUND_PIXEL (f) = fore_pixel;
ee78dc32 2180
fbd6baed 2181 if (FRAME_W32_WINDOW (f) != 0)
ee78dc32
GV
2182 {
2183 if (FRAME_VISIBLE_P (f))
2184 {
2185 x_display_cursor (f, 0);
2186 x_display_cursor (f, 1);
2187 }
2188 }
6fc2811b
JR
2189
2190 update_face_from_frame_parameter (f, Qcursor_color, arg);
ee78dc32
GV
2191}
2192
33d52f9c
GV
2193/* Set the border-color of frame F to pixel value PIX.
2194 Note that this does not fully take effect if done before
2195 F has an window. */
2196void
2197x_set_border_pixel (f, pix)
2198 struct frame *f;
2199 int pix;
2200{
2201 f->output_data.w32->border_pixel = pix;
2202
2203 if (FRAME_W32_WINDOW (f) != 0 && f->output_data.w32->border_width > 0)
2204 {
2205 if (FRAME_VISIBLE_P (f))
2206 redraw_frame (f);
2207 }
2208}
2209
ee78dc32
GV
2210/* Set the border-color of frame F to value described by ARG.
2211 ARG can be a string naming a color.
2212 The border-color is used for the border that is drawn by the server.
2213 Note that this does not fully take effect if done before
2214 F has a window; it must be redone when the window is created. */
2215
2216void
2217x_set_border_color (f, arg, oldval)
2218 struct frame *f;
2219 Lisp_Object arg, oldval;
2220{
ee78dc32
GV
2221 int pix;
2222
2223 CHECK_STRING (arg, 0);
ee78dc32 2224 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
ee78dc32 2225 x_set_border_pixel (f, pix);
6fc2811b 2226 update_face_from_frame_parameter (f, Qborder_color, arg);
ee78dc32
GV
2227}
2228
dfff8a69
JR
2229/* Value is the internal representation of the specified cursor type
2230 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
2231 of the bar cursor. */
2232
2233enum text_cursor_kinds
2234x_specified_cursor_type (arg, width)
2235 Lisp_Object arg;
2236 int *width;
ee78dc32 2237{
dfff8a69
JR
2238 enum text_cursor_kinds type;
2239
ee78dc32
GV
2240 if (EQ (arg, Qbar))
2241 {
dfff8a69
JR
2242 type = BAR_CURSOR;
2243 *width = 2;
ee78dc32 2244 }
dfff8a69
JR
2245 else if (CONSP (arg)
2246 && EQ (XCAR (arg), Qbar)
2247 && INTEGERP (XCDR (arg))
2248 && XINT (XCDR (arg)) >= 0)
ee78dc32 2249 {
dfff8a69
JR
2250 type = BAR_CURSOR;
2251 *width = XINT (XCDR (arg));
ee78dc32 2252 }
dfff8a69
JR
2253 else if (NILP (arg))
2254 type = NO_CURSOR;
ee78dc32
GV
2255 else
2256 /* Treat anything unknown as "box cursor".
2257 It was bad to signal an error; people have trouble fixing
2258 .Xdefaults with Emacs, when it has something bad in it. */
dfff8a69
JR
2259 type = FILLED_BOX_CURSOR;
2260
2261 return type;
2262}
2263
2264void
2265x_set_cursor_type (f, arg, oldval)
2266 FRAME_PTR f;
2267 Lisp_Object arg, oldval;
2268{
2269 int width;
2270
2271 FRAME_DESIRED_CURSOR (f) = x_specified_cursor_type (arg, &width);
2272 f->output_data.w32->cursor_width = width;
ee78dc32
GV
2273
2274 /* Make sure the cursor gets redrawn. This is overkill, but how
2275 often do people change cursor types? */
2276 update_mode_lines++;
2277}
dfff8a69 2278\f
ee78dc32
GV
2279void
2280x_set_icon_type (f, arg, oldval)
2281 struct frame *f;
2282 Lisp_Object arg, oldval;
2283{
ee78dc32
GV
2284 int result;
2285
eb7576ce
GV
2286 if (NILP (arg) && NILP (oldval))
2287 return;
2288
2289 if (STRINGP (arg) && STRINGP (oldval)
2290 && EQ (Fstring_equal (oldval, arg), Qt))
2291 return;
2292
2293 if (SYMBOLP (arg) && SYMBOLP (oldval) && EQ (arg, oldval))
ee78dc32
GV
2294 return;
2295
2296 BLOCK_INPUT;
ee78dc32 2297
eb7576ce 2298 result = x_bitmap_icon (f, arg);
ee78dc32
GV
2299 if (result)
2300 {
2301 UNBLOCK_INPUT;
2302 error ("No icon window available");
2303 }
2304
ee78dc32 2305 UNBLOCK_INPUT;
ee78dc32
GV
2306}
2307
2308/* Return non-nil if frame F wants a bitmap icon. */
2309
2310Lisp_Object
2311x_icon_type (f)
2312 FRAME_PTR f;
2313{
2314 Lisp_Object tem;
2315
2316 tem = assq_no_quit (Qicon_type, f->param_alist);
2317 if (CONSP (tem))
8e713be6 2318 return XCDR (tem);
ee78dc32
GV
2319 else
2320 return Qnil;
2321}
2322
2323void
2324x_set_icon_name (f, arg, oldval)
2325 struct frame *f;
2326 Lisp_Object arg, oldval;
2327{
ee78dc32
GV
2328 int result;
2329
2330 if (STRINGP (arg))
2331 {
2332 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
2333 return;
2334 }
2335 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
2336 return;
2337
2338 f->icon_name = arg;
2339
2340#if 0
fbd6baed 2341 if (f->output_data.w32->icon_bitmap != 0)
ee78dc32
GV
2342 return;
2343
2344 BLOCK_INPUT;
2345
2346 result = x_text_icon (f,
1edf84e7 2347 (char *) XSTRING ((!NILP (f->icon_name)
ee78dc32 2348 ? f->icon_name
1edf84e7
GV
2349 : !NILP (f->title)
2350 ? f->title
ee78dc32
GV
2351 : f->name))->data);
2352
2353 if (result)
2354 {
2355 UNBLOCK_INPUT;
2356 error ("No icon window available");
2357 }
2358
2359 /* If the window was unmapped (and its icon was mapped),
2360 the new icon is not mapped, so map the window in its stead. */
2361 if (FRAME_VISIBLE_P (f))
2362 {
2363#ifdef USE_X_TOOLKIT
fbd6baed 2364 XtPopup (f->output_data.w32->widget, XtGrabNone);
ee78dc32 2365#endif
fbd6baed 2366 XMapWindow (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f));
ee78dc32
GV
2367 }
2368
fbd6baed 2369 XFlush (FRAME_W32_DISPLAY (f));
ee78dc32
GV
2370 UNBLOCK_INPUT;
2371#endif
2372}
2373
2374extern Lisp_Object x_new_font ();
4587b026 2375extern Lisp_Object x_new_fontset();
ee78dc32
GV
2376
2377void
2378x_set_font (f, arg, oldval)
2379 struct frame *f;
2380 Lisp_Object arg, oldval;
2381{
2382 Lisp_Object result;
4587b026 2383 Lisp_Object fontset_name;
4b817373 2384 Lisp_Object frame;
ee78dc32
GV
2385
2386 CHECK_STRING (arg, 1);
2387
4587b026
GV
2388 fontset_name = Fquery_fontset (arg, Qnil);
2389
ee78dc32 2390 BLOCK_INPUT;
4587b026
GV
2391 result = (STRINGP (fontset_name)
2392 ? x_new_fontset (f, XSTRING (fontset_name)->data)
2393 : x_new_font (f, XSTRING (arg)->data));
ee78dc32
GV
2394 UNBLOCK_INPUT;
2395
2396 if (EQ (result, Qnil))
dfff8a69 2397 error ("Font `%s' is not defined", XSTRING (arg)->data);
ee78dc32 2398 else if (EQ (result, Qt))
dfff8a69 2399 error ("The characters of the given font have varying widths");
ee78dc32
GV
2400 else if (STRINGP (result))
2401 {
ee78dc32 2402 store_frame_param (f, Qfont, result);
6fc2811b 2403 recompute_basic_faces (f);
ee78dc32
GV
2404 }
2405 else
2406 abort ();
4b817373 2407
6fc2811b
JR
2408 do_pending_window_change (0);
2409
2410 /* Don't call `face-set-after-frame-default' when faces haven't been
2411 initialized yet. This is the case when called from
2412 Fx_create_frame. In that case, the X widget or window doesn't
2413 exist either, and we can end up in x_report_frame_params with a
2414 null widget which gives a segfault. */
2415 if (FRAME_FACE_CACHE (f))
2416 {
2417 XSETFRAME (frame, f);
2418 call1 (Qface_set_after_frame_default, frame);
2419 }
ee78dc32
GV
2420}
2421
2422void
2423x_set_border_width (f, arg, oldval)
2424 struct frame *f;
2425 Lisp_Object arg, oldval;
2426{
2427 CHECK_NUMBER (arg, 0);
2428
fbd6baed 2429 if (XINT (arg) == f->output_data.w32->border_width)
ee78dc32
GV
2430 return;
2431
fbd6baed 2432 if (FRAME_W32_WINDOW (f) != 0)
ee78dc32
GV
2433 error ("Cannot change the border width of a window");
2434
fbd6baed 2435 f->output_data.w32->border_width = XINT (arg);
ee78dc32
GV
2436}
2437
2438void
2439x_set_internal_border_width (f, arg, oldval)
2440 struct frame *f;
2441 Lisp_Object arg, oldval;
2442{
fbd6baed 2443 int old = f->output_data.w32->internal_border_width;
ee78dc32
GV
2444
2445 CHECK_NUMBER (arg, 0);
fbd6baed
GV
2446 f->output_data.w32->internal_border_width = XINT (arg);
2447 if (f->output_data.w32->internal_border_width < 0)
2448 f->output_data.w32->internal_border_width = 0;
ee78dc32 2449
fbd6baed 2450 if (f->output_data.w32->internal_border_width == old)
ee78dc32
GV
2451 return;
2452
fbd6baed 2453 if (FRAME_W32_WINDOW (f) != 0)
ee78dc32 2454 {
ee78dc32 2455 x_set_window_size (f, 0, f->width, f->height);
ee78dc32 2456 SET_FRAME_GARBAGED (f);
6fc2811b 2457 do_pending_window_change (0);
ee78dc32
GV
2458 }
2459}
2460
2461void
2462x_set_visibility (f, value, oldval)
2463 struct frame *f;
2464 Lisp_Object value, oldval;
2465{
2466 Lisp_Object frame;
2467 XSETFRAME (frame, f);
2468
2469 if (NILP (value))
2470 Fmake_frame_invisible (frame, Qt);
2471 else if (EQ (value, Qicon))
2472 Ficonify_frame (frame);
2473 else
2474 Fmake_frame_visible (frame);
2475}
2476
2477void
2478x_set_menu_bar_lines (f, value, oldval)
2479 struct frame *f;
2480 Lisp_Object value, oldval;
2481{
2482 int nlines;
2483 int olines = FRAME_MENU_BAR_LINES (f);
2484
2485 /* Right now, menu bars don't work properly in minibuf-only frames;
2486 most of the commands try to apply themselves to the minibuffer
6fc2811b 2487 frame itself, and get an error because you can't switch buffers
ee78dc32
GV
2488 in or split the minibuffer window. */
2489 if (FRAME_MINIBUF_ONLY_P (f))
2490 return;
2491
2492 if (INTEGERP (value))
2493 nlines = XINT (value);
2494 else
2495 nlines = 0;
2496
2497 FRAME_MENU_BAR_LINES (f) = 0;
2498 if (nlines)
2499 FRAME_EXTERNAL_MENU_BAR (f) = 1;
2500 else
2501 {
2502 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
2503 free_frame_menubar (f);
2504 FRAME_EXTERNAL_MENU_BAR (f) = 0;
1edf84e7
GV
2505
2506 /* Adjust the frame size so that the client (text) dimensions
2507 remain the same. This depends on FRAME_EXTERNAL_MENU_BAR being
2508 set correctly. */
2509 x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
6fc2811b 2510 do_pending_window_change (0);
ee78dc32 2511 }
6fc2811b
JR
2512 adjust_glyphs (f);
2513}
2514
2515
2516/* Set the number of lines used for the tool bar of frame F to VALUE.
2517 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
2518 is the old number of tool bar lines. This function changes the
2519 height of all windows on frame F to match the new tool bar height.
2520 The frame's height doesn't change. */
2521
2522void
2523x_set_tool_bar_lines (f, value, oldval)
2524 struct frame *f;
2525 Lisp_Object value, oldval;
2526{
2527 int delta, nlines;
2528
2529 /* Use VALUE only if an integer >= 0. */
2530 if (INTEGERP (value) && XINT (value) >= 0)
2531 nlines = XFASTINT (value);
2532 else
2533 nlines = 0;
2534
2535 /* Make sure we redisplay all windows in this frame. */
2536 ++windows_or_buffers_changed;
2537
2538 delta = nlines - FRAME_TOOL_BAR_LINES (f);
2539 FRAME_TOOL_BAR_LINES (f) = nlines;
2540 x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
2541 do_pending_window_change (0);
2542 adjust_glyphs (f);
ee78dc32
GV
2543}
2544
6fc2811b 2545
ee78dc32 2546/* Change the name of frame F to NAME. If NAME is nil, set F's name to
fbd6baed 2547 w32_id_name.
ee78dc32
GV
2548
2549 If EXPLICIT is non-zero, that indicates that lisp code is setting the
2550 name; if NAME is a string, set F's name to NAME and set
2551 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
2552
2553 If EXPLICIT is zero, that indicates that Emacs redisplay code is
2554 suggesting a new name, which lisp code should override; if
2555 F->explicit_name is set, ignore the new name; otherwise, set it. */
2556
2557void
2558x_set_name (f, name, explicit)
2559 struct frame *f;
2560 Lisp_Object name;
2561 int explicit;
2562{
2563 /* Make sure that requests from lisp code override requests from
2564 Emacs redisplay code. */
2565 if (explicit)
2566 {
2567 /* If we're switching from explicit to implicit, we had better
2568 update the mode lines and thereby update the title. */
2569 if (f->explicit_name && NILP (name))
2570 update_mode_lines = 1;
2571
2572 f->explicit_name = ! NILP (name);
2573 }
2574 else if (f->explicit_name)
2575 return;
2576
fbd6baed 2577 /* If NAME is nil, set the name to the w32_id_name. */
ee78dc32
GV
2578 if (NILP (name))
2579 {
2580 /* Check for no change needed in this very common case
2581 before we do any consing. */
fbd6baed 2582 if (!strcmp (FRAME_W32_DISPLAY_INFO (f)->w32_id_name,
ee78dc32
GV
2583 XSTRING (f->name)->data))
2584 return;
fbd6baed 2585 name = build_string (FRAME_W32_DISPLAY_INFO (f)->w32_id_name);
ee78dc32
GV
2586 }
2587 else
2588 CHECK_STRING (name, 0);
2589
2590 /* Don't change the name if it's already NAME. */
2591 if (! NILP (Fstring_equal (name, f->name)))
2592 return;
2593
1edf84e7
GV
2594 f->name = name;
2595
2596 /* For setting the frame title, the title parameter should override
2597 the name parameter. */
2598 if (! NILP (f->title))
2599 name = f->title;
2600
fbd6baed 2601 if (FRAME_W32_WINDOW (f))
ee78dc32 2602 {
6fc2811b 2603 if (STRING_MULTIBYTE (name))
dfff8a69 2604 name = ENCODE_SYSTEM (name);
6fc2811b 2605
ee78dc32 2606 BLOCK_INPUT;
fbd6baed 2607 SetWindowText(FRAME_W32_WINDOW (f), XSTRING (name)->data);
ee78dc32
GV
2608 UNBLOCK_INPUT;
2609 }
ee78dc32
GV
2610}
2611
2612/* This function should be called when the user's lisp code has
2613 specified a name for the frame; the name will override any set by the
2614 redisplay code. */
2615void
2616x_explicitly_set_name (f, arg, oldval)
2617 FRAME_PTR f;
2618 Lisp_Object arg, oldval;
2619{
2620 x_set_name (f, arg, 1);
2621}
2622
2623/* This function should be called by Emacs redisplay code to set the
2624 name; names set this way will never override names set by the user's
2625 lisp code. */
2626void
2627x_implicitly_set_name (f, arg, oldval)
2628 FRAME_PTR f;
2629 Lisp_Object arg, oldval;
2630{
2631 x_set_name (f, arg, 0);
2632}
1edf84e7
GV
2633\f
2634/* Change the title of frame F to NAME.
2635 If NAME is nil, use the frame name as the title.
2636
2637 If EXPLICIT is non-zero, that indicates that lisp code is setting the
2638 name; if NAME is a string, set F's name to NAME and set
2639 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
2640
2641 If EXPLICIT is zero, that indicates that Emacs redisplay code is
2642 suggesting a new name, which lisp code should override; if
2643 F->explicit_name is set, ignore the new name; otherwise, set it. */
ee78dc32 2644
1edf84e7 2645void
6fc2811b 2646x_set_title (f, name, old_name)
1edf84e7 2647 struct frame *f;
6fc2811b 2648 Lisp_Object name, old_name;
1edf84e7
GV
2649{
2650 /* Don't change the title if it's already NAME. */
2651 if (EQ (name, f->title))
2652 return;
2653
2654 update_mode_lines = 1;
2655
2656 f->title = name;
2657
2658 if (NILP (name))
2659 name = f->name;
2660
2661 if (FRAME_W32_WINDOW (f))
2662 {
6fc2811b 2663 if (STRING_MULTIBYTE (name))
dfff8a69 2664 name = ENCODE_SYSTEM (name);
6fc2811b 2665
1edf84e7
GV
2666 BLOCK_INPUT;
2667 SetWindowText(FRAME_W32_WINDOW (f), XSTRING (name)->data);
2668 UNBLOCK_INPUT;
2669 }
2670}
2671\f
ee78dc32
GV
2672void
2673x_set_autoraise (f, arg, oldval)
2674 struct frame *f;
2675 Lisp_Object arg, oldval;
2676{
2677 f->auto_raise = !EQ (Qnil, arg);
2678}
2679
2680void
2681x_set_autolower (f, arg, oldval)
2682 struct frame *f;
2683 Lisp_Object arg, oldval;
2684{
2685 f->auto_lower = !EQ (Qnil, arg);
2686}
2687
2688void
2689x_set_unsplittable (f, arg, oldval)
2690 struct frame *f;
2691 Lisp_Object arg, oldval;
2692{
2693 f->no_split = !NILP (arg);
2694}
2695
2696void
2697x_set_vertical_scroll_bars (f, arg, oldval)
2698 struct frame *f;
2699 Lisp_Object arg, oldval;
2700{
1026b400
RS
2701 if ((EQ (arg, Qleft) && FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
2702 || (EQ (arg, Qright) && FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f))
2703 || (NILP (arg) && FRAME_HAS_VERTICAL_SCROLL_BARS (f))
2704 || (!NILP (arg) && ! FRAME_HAS_VERTICAL_SCROLL_BARS (f)))
ee78dc32 2705 {
1026b400
RS
2706 FRAME_VERTICAL_SCROLL_BAR_TYPE (f) = NILP (arg) ?
2707 vertical_scroll_bar_none :
87996783
GV
2708 /* Put scroll bars on the right by default, as is conventional
2709 on MS-Windows. */
2710 EQ (Qleft, arg)
2711 ? vertical_scroll_bar_left
2712 : vertical_scroll_bar_right;
ee78dc32
GV
2713
2714 /* We set this parameter before creating the window for the
2715 frame, so we can get the geometry right from the start.
2716 However, if the window hasn't been created yet, we shouldn't
2717 call x_set_window_size. */
fbd6baed 2718 if (FRAME_W32_WINDOW (f))
ee78dc32 2719 x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
6fc2811b 2720 do_pending_window_change (0);
ee78dc32
GV
2721 }
2722}
2723
2724void
2725x_set_scroll_bar_width (f, arg, oldval)
2726 struct frame *f;
2727 Lisp_Object arg, oldval;
2728{
6fc2811b
JR
2729 int wid = FONT_WIDTH (f->output_data.w32->font);
2730
ee78dc32
GV
2731 if (NILP (arg))
2732 {
6fc2811b
JR
2733 FRAME_SCROLL_BAR_PIXEL_WIDTH (f) = GetSystemMetrics (SM_CXVSCROLL);
2734 FRAME_SCROLL_BAR_COLS (f) = (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) +
2735 wid - 1) / wid;
2736 if (FRAME_W32_WINDOW (f))
2737 x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
2738 do_pending_window_change (0);
ee78dc32
GV
2739 }
2740 else if (INTEGERP (arg) && XINT (arg) > 0
2741 && XFASTINT (arg) != FRAME_SCROLL_BAR_PIXEL_WIDTH (f))
2742 {
ee78dc32 2743 FRAME_SCROLL_BAR_PIXEL_WIDTH (f) = XFASTINT (arg);
6fc2811b
JR
2744 FRAME_SCROLL_BAR_COLS (f) = (FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
2745 + wid-1) / wid;
fbd6baed 2746 if (FRAME_W32_WINDOW (f))
ee78dc32 2747 x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
6fc2811b 2748 do_pending_window_change (0);
ee78dc32 2749 }
6fc2811b
JR
2750 change_frame_size (f, 0, FRAME_WIDTH (f), 0, 0, 0);
2751 XWINDOW (FRAME_SELECTED_WINDOW (f))->cursor.hpos = 0;
2752 XWINDOW (FRAME_SELECTED_WINDOW (f))->cursor.x = 0;
ee78dc32
GV
2753}
2754\f
2755/* Subroutines of creating an frame. */
2756
2757/* Make sure that Vx_resource_name is set to a reasonable value.
2758 Fix it up, or set it to `emacs' if it is too hopeless. */
2759
2760static void
2761validate_x_resource_name ()
2762{
6fc2811b 2763 int len = 0;
ee78dc32
GV
2764 /* Number of valid characters in the resource name. */
2765 int good_count = 0;
2766 /* Number of invalid characters in the resource name. */
2767 int bad_count = 0;
2768 Lisp_Object new;
2769 int i;
2770
2771 if (STRINGP (Vx_resource_name))
2772 {
2773 unsigned char *p = XSTRING (Vx_resource_name)->data;
2774 int i;
2775
dfff8a69 2776 len = STRING_BYTES (XSTRING (Vx_resource_name));
ee78dc32
GV
2777
2778 /* Only letters, digits, - and _ are valid in resource names.
2779 Count the valid characters and count the invalid ones. */
2780 for (i = 0; i < len; i++)
2781 {
2782 int c = p[i];
2783 if (! ((c >= 'a' && c <= 'z')
2784 || (c >= 'A' && c <= 'Z')
2785 || (c >= '0' && c <= '9')
2786 || c == '-' || c == '_'))
2787 bad_count++;
2788 else
2789 good_count++;
2790 }
2791 }
2792 else
2793 /* Not a string => completely invalid. */
2794 bad_count = 5, good_count = 0;
2795
2796 /* If name is valid already, return. */
2797 if (bad_count == 0)
2798 return;
2799
2800 /* If name is entirely invalid, or nearly so, use `emacs'. */
2801 if (good_count == 0
2802 || (good_count == 1 && bad_count > 0))
2803 {
2804 Vx_resource_name = build_string ("emacs");
2805 return;
2806 }
2807
2808 /* Name is partly valid. Copy it and replace the invalid characters
2809 with underscores. */
2810
2811 Vx_resource_name = new = Fcopy_sequence (Vx_resource_name);
2812
2813 for (i = 0; i < len; i++)
2814 {
2815 int c = XSTRING (new)->data[i];
2816 if (! ((c >= 'a' && c <= 'z')
2817 || (c >= 'A' && c <= 'Z')
2818 || (c >= '0' && c <= '9')
2819 || c == '-' || c == '_'))
2820 XSTRING (new)->data[i] = '_';
2821 }
2822}
2823
2824
2825extern char *x_get_string_resource ();
2826
2827DEFUN ("x-get-resource", Fx_get_resource, Sx_get_resource, 2, 4, 0,
2828 "Return the value of ATTRIBUTE, of class CLASS, from the X defaults database.\n\
2829This uses `INSTANCE.ATTRIBUTE' as the key and `Emacs.CLASS' as the\n\
2830class, where INSTANCE is the name under which Emacs was invoked, or\n\
2831the name specified by the `-name' or `-rn' command-line arguments.\n\
2832\n\
2833The optional arguments COMPONENT and SUBCLASS add to the key and the\n\
2834class, respectively. You must specify both of them or neither.\n\
2835If you specify them, the key is `INSTANCE.COMPONENT.ATTRIBUTE'\n\
2836and the class is `Emacs.CLASS.SUBCLASS'.")
2837 (attribute, class, component, subclass)
2838 Lisp_Object attribute, class, component, subclass;
2839{
2840 register char *value;
2841 char *name_key;
2842 char *class_key;
2843
2844 CHECK_STRING (attribute, 0);
2845 CHECK_STRING (class, 0);
2846
2847 if (!NILP (component))
2848 CHECK_STRING (component, 1);
2849 if (!NILP (subclass))
2850 CHECK_STRING (subclass, 2);
2851 if (NILP (component) != NILP (subclass))
2852 error ("x-get-resource: must specify both COMPONENT and SUBCLASS or neither");
2853
2854 validate_x_resource_name ();
2855
2856 /* Allocate space for the components, the dots which separate them,
2857 and the final '\0'. Make them big enough for the worst case. */
dfff8a69 2858 name_key = (char *) alloca (STRING_BYTES (XSTRING (Vx_resource_name))
ee78dc32 2859 + (STRINGP (component)
dfff8a69
JR
2860 ? STRING_BYTES (XSTRING (component)) : 0)
2861 + STRING_BYTES (XSTRING (attribute))
ee78dc32
GV
2862 + 3);
2863
2864 class_key = (char *) alloca ((sizeof (EMACS_CLASS) - 1)
dfff8a69 2865 + STRING_BYTES (XSTRING (class))
ee78dc32 2866 + (STRINGP (subclass)
dfff8a69 2867 ? STRING_BYTES (XSTRING (subclass)) : 0)
ee78dc32
GV
2868 + 3);
2869
2870 /* Start with emacs.FRAMENAME for the name (the specific one)
2871 and with `Emacs' for the class key (the general one). */
2872 strcpy (name_key, XSTRING (Vx_resource_name)->data);
2873 strcpy (class_key, EMACS_CLASS);
2874
2875 strcat (class_key, ".");
2876 strcat (class_key, XSTRING (class)->data);
2877
2878 if (!NILP (component))
2879 {
2880 strcat (class_key, ".");
2881 strcat (class_key, XSTRING (subclass)->data);
2882
2883 strcat (name_key, ".");
2884 strcat (name_key, XSTRING (component)->data);
2885 }
2886
2887 strcat (name_key, ".");
2888 strcat (name_key, XSTRING (attribute)->data);
2889
2890 value = x_get_string_resource (Qnil,
2891 name_key, class_key);
2892
2893 if (value != (char *) 0)
2894 return build_string (value);
2895 else
2896 return Qnil;
2897}
2898
2899/* Used when C code wants a resource value. */
2900
2901char *
2902x_get_resource_string (attribute, class)
2903 char *attribute, *class;
2904{
ee78dc32
GV
2905 char *name_key;
2906 char *class_key;
6fc2811b 2907 struct frame *sf = SELECTED_FRAME ();
ee78dc32
GV
2908
2909 /* Allocate space for the components, the dots which separate them,
2910 and the final '\0'. */
dfff8a69 2911 name_key = (char *) alloca (STRING_BYTES (XSTRING (Vinvocation_name))
ee78dc32
GV
2912 + strlen (attribute) + 2);
2913 class_key = (char *) alloca ((sizeof (EMACS_CLASS) - 1)
2914 + strlen (class) + 2);
2915
2916 sprintf (name_key, "%s.%s",
2917 XSTRING (Vinvocation_name)->data,
2918 attribute);
2919 sprintf (class_key, "%s.%s", EMACS_CLASS, class);
2920
6fc2811b 2921 return x_get_string_resource (sf, name_key, class_key);
ee78dc32
GV
2922}
2923
2924/* Types we might convert a resource string into. */
2925enum resource_types
6fc2811b
JR
2926{
2927 RES_TYPE_NUMBER,
2928 RES_TYPE_FLOAT,
2929 RES_TYPE_BOOLEAN,
2930 RES_TYPE_STRING,
2931 RES_TYPE_SYMBOL
2932};
ee78dc32
GV
2933
2934/* Return the value of parameter PARAM.
2935
2936 First search ALIST, then Vdefault_frame_alist, then the X defaults
2937 database, using ATTRIBUTE as the attribute name and CLASS as its class.
2938
2939 Convert the resource to the type specified by desired_type.
2940
2941 If no default is specified, return Qunbound. If you call
6fc2811b 2942 w32_get_arg, make sure you deal with Qunbound in a reasonable way,
ee78dc32
GV
2943 and don't let it get stored in any Lisp-visible variables! */
2944
2945static Lisp_Object
6fc2811b 2946w32_get_arg (alist, param, attribute, class, type)
ee78dc32
GV
2947 Lisp_Object alist, param;
2948 char *attribute;
2949 char *class;
2950 enum resource_types type;
2951{
2952 register Lisp_Object tem;
2953
2954 tem = Fassq (param, alist);
2955 if (EQ (tem, Qnil))
2956 tem = Fassq (param, Vdefault_frame_alist);
2957 if (EQ (tem, Qnil))
2958 {
2959
2960 if (attribute)
2961 {
2962 tem = Fx_get_resource (build_string (attribute),
2963 build_string (class),
2964 Qnil, Qnil);
2965
2966 if (NILP (tem))
2967 return Qunbound;
2968
2969 switch (type)
2970 {
6fc2811b 2971 case RES_TYPE_NUMBER:
ee78dc32
GV
2972 return make_number (atoi (XSTRING (tem)->data));
2973
6fc2811b
JR
2974 case RES_TYPE_FLOAT:
2975 return make_float (atof (XSTRING (tem)->data));
2976
2977 case RES_TYPE_BOOLEAN:
ee78dc32
GV
2978 tem = Fdowncase (tem);
2979 if (!strcmp (XSTRING (tem)->data, "on")
2980 || !strcmp (XSTRING (tem)->data, "true"))
2981 return Qt;
2982 else
2983 return Qnil;
2984
6fc2811b 2985 case RES_TYPE_STRING:
ee78dc32
GV
2986 return tem;
2987
6fc2811b 2988 case RES_TYPE_SYMBOL:
ee78dc32
GV
2989 /* As a special case, we map the values `true' and `on'
2990 to Qt, and `false' and `off' to Qnil. */
2991 {
2992 Lisp_Object lower;
2993 lower = Fdowncase (tem);
2994 if (!strcmp (XSTRING (lower)->data, "on")
2995 || !strcmp (XSTRING (lower)->data, "true"))
2996 return Qt;
2997 else if (!strcmp (XSTRING (lower)->data, "off")
2998 || !strcmp (XSTRING (lower)->data, "false"))
2999 return Qnil;
3000 else
3001 return Fintern (tem, Qnil);
3002 }
3003
3004 default:
3005 abort ();
3006 }
3007 }
3008 else
3009 return Qunbound;
3010 }
3011 return Fcdr (tem);
3012}
3013
3014/* Record in frame F the specified or default value according to ALIST
dfff8a69
JR
3015 of the parameter named PROP (a Lisp symbol).
3016 If no value is specified for PROP, look for an X default for XPROP
ee78dc32
GV
3017 on the frame named NAME.
3018 If that is not found either, use the value DEFLT. */
3019
3020static Lisp_Object
3021x_default_parameter (f, alist, prop, deflt, xprop, xclass, type)
3022 struct frame *f;
3023 Lisp_Object alist;
3024 Lisp_Object prop;
3025 Lisp_Object deflt;
3026 char *xprop;
3027 char *xclass;
3028 enum resource_types type;
3029{
3030 Lisp_Object tem;
3031
6fc2811b 3032 tem = w32_get_arg (alist, prop, xprop, xclass, type);
ee78dc32
GV
3033 if (EQ (tem, Qunbound))
3034 tem = deflt;
3035 x_set_frame_parameters (f, Fcons (Fcons (prop, tem), Qnil));
3036 return tem;
3037}
3038\f
3039DEFUN ("x-parse-geometry", Fx_parse_geometry, Sx_parse_geometry, 1, 1, 0,
3040 "Parse an X-style geometry string STRING.\n\
3041Returns an alist of the form ((top . TOP), (left . LEFT) ... ).\n\
3042The properties returned may include `top', `left', `height', and `width'.\n\
3043The value of `left' or `top' may be an integer,\n\
3044or a list (+ N) meaning N pixels relative to top/left corner,\n\
3045or a list (- N) meaning -N pixels relative to bottom/right corner.")
3046 (string)
3047 Lisp_Object string;
3048{
3049 int geometry, x, y;
3050 unsigned int width, height;
3051 Lisp_Object result;
3052
3053 CHECK_STRING (string, 0);
3054
3055 geometry = XParseGeometry ((char *) XSTRING (string)->data,
3056 &x, &y, &width, &height);
3057
3058 result = Qnil;
3059 if (geometry & XValue)
3060 {
3061 Lisp_Object element;
3062
3063 if (x >= 0 && (geometry & XNegative))
3064 element = Fcons (Qleft, Fcons (Qminus, Fcons (make_number (-x), Qnil)));
3065 else if (x < 0 && ! (geometry & XNegative))
3066 element = Fcons (Qleft, Fcons (Qplus, Fcons (make_number (x), Qnil)));
3067 else
3068 element = Fcons (Qleft, make_number (x));
3069 result = Fcons (element, result);
3070 }
3071
3072 if (geometry & YValue)
3073 {
3074 Lisp_Object element;
3075
3076 if (y >= 0 && (geometry & YNegative))
3077 element = Fcons (Qtop, Fcons (Qminus, Fcons (make_number (-y), Qnil)));
3078 else if (y < 0 && ! (geometry & YNegative))
3079 element = Fcons (Qtop, Fcons (Qplus, Fcons (make_number (y), Qnil)));
3080 else
3081 element = Fcons (Qtop, make_number (y));
3082 result = Fcons (element, result);
3083 }
3084
3085 if (geometry & WidthValue)
3086 result = Fcons (Fcons (Qwidth, make_number (width)), result);
3087 if (geometry & HeightValue)
3088 result = Fcons (Fcons (Qheight, make_number (height)), result);
3089
3090 return result;
3091}
3092
3093/* Calculate the desired size and position of this window,
3094 and return the flags saying which aspects were specified.
3095
3096 This function does not make the coordinates positive. */
3097
3098#define DEFAULT_ROWS 40
3099#define DEFAULT_COLS 80
3100
3101static int
3102x_figure_window_size (f, parms)
3103 struct frame *f;
3104 Lisp_Object parms;
3105{
3106 register Lisp_Object tem0, tem1, tem2;
ee78dc32
GV
3107 long window_prompting = 0;
3108
3109 /* Default values if we fall through.
3110 Actually, if that happens we should get
3111 window manager prompting. */
1026b400 3112 SET_FRAME_WIDTH (f, DEFAULT_COLS);
ee78dc32
GV
3113 f->height = DEFAULT_ROWS;
3114 /* Window managers expect that if program-specified
3115 positions are not (0,0), they're intentional, not defaults. */
fbd6baed
GV
3116 f->output_data.w32->top_pos = 0;
3117 f->output_data.w32->left_pos = 0;
ee78dc32 3118
6fc2811b
JR
3119 tem0 = w32_get_arg (parms, Qheight, 0, 0, RES_TYPE_NUMBER);
3120 tem1 = w32_get_arg (parms, Qwidth, 0, 0, RES_TYPE_NUMBER);
3121 tem2 = w32_get_arg (parms, Quser_size, 0, 0, RES_TYPE_NUMBER);
ee78dc32
GV
3122 if (! EQ (tem0, Qunbound) || ! EQ (tem1, Qunbound))
3123 {
3124 if (!EQ (tem0, Qunbound))
3125 {
3126 CHECK_NUMBER (tem0, 0);
3127 f->height = XINT (tem0);
3128 }
3129 if (!EQ (tem1, Qunbound))
3130 {
3131 CHECK_NUMBER (tem1, 0);
1026b400 3132 SET_FRAME_WIDTH (f, XINT (tem1));
ee78dc32
GV
3133 }
3134 if (!NILP (tem2) && !EQ (tem2, Qunbound))
3135 window_prompting |= USSize;
3136 else
3137 window_prompting |= PSize;
3138 }
3139
fbd6baed 3140 f->output_data.w32->vertical_scroll_bar_extra
ee78dc32
GV
3141 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
3142 ? 0
3143 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
3144 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
fbd6baed 3145 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.w32->font)));
6fc2811b
JR
3146 f->output_data.w32->flags_areas_extra
3147 = FRAME_FLAGS_AREA_WIDTH (f);
fbd6baed
GV
3148 f->output_data.w32->pixel_width = CHAR_TO_PIXEL_WIDTH (f, f->width);
3149 f->output_data.w32->pixel_height = CHAR_TO_PIXEL_HEIGHT (f, f->height);
ee78dc32 3150
6fc2811b
JR
3151 tem0 = w32_get_arg (parms, Qtop, 0, 0, RES_TYPE_NUMBER);
3152 tem1 = w32_get_arg (parms, Qleft, 0, 0, RES_TYPE_NUMBER);
3153 tem2 = w32_get_arg (parms, Quser_position, 0, 0, RES_TYPE_NUMBER);
ee78dc32
GV
3154 if (! EQ (tem0, Qunbound) || ! EQ (tem1, Qunbound))
3155 {
3156 if (EQ (tem0, Qminus))
3157 {
fbd6baed 3158 f->output_data.w32->top_pos = 0;
ee78dc32
GV
3159 window_prompting |= YNegative;
3160 }
8e713be6
KR
3161 else if (CONSP (tem0) && EQ (XCAR (tem0), Qminus)
3162 && CONSP (XCDR (tem0))
3163 && INTEGERP (XCAR (XCDR (tem0))))
ee78dc32 3164 {
8e713be6 3165 f->output_data.w32->top_pos = - XINT (XCAR (XCDR (tem0)));
ee78dc32
GV
3166 window_prompting |= YNegative;
3167 }
8e713be6
KR
3168 else if (CONSP (tem0) && EQ (XCAR (tem0), Qplus)
3169 && CONSP (XCDR (tem0))
3170 && INTEGERP (XCAR (XCDR (tem0))))
ee78dc32 3171 {
8e713be6 3172 f->output_data.w32->top_pos = XINT (XCAR (XCDR (tem0)));
ee78dc32
GV
3173 }
3174 else if (EQ (tem0, Qunbound))
fbd6baed 3175 f->output_data.w32->top_pos = 0;
ee78dc32
GV
3176 else
3177 {
3178 CHECK_NUMBER (tem0, 0);
fbd6baed
GV
3179 f->output_data.w32->top_pos = XINT (tem0);
3180 if (f->output_data.w32->top_pos < 0)
ee78dc32
GV
3181 window_prompting |= YNegative;
3182 }
3183
3184 if (EQ (tem1, Qminus))
3185 {
fbd6baed 3186 f->output_data.w32->left_pos = 0;
ee78dc32
GV
3187 window_prompting |= XNegative;
3188 }
8e713be6
KR
3189 else if (CONSP (tem1) && EQ (XCAR (tem1), Qminus)
3190 && CONSP (XCDR (tem1))
3191 && INTEGERP (XCAR (XCDR (tem1))))
ee78dc32 3192 {
8e713be6 3193 f->output_data.w32->left_pos = - XINT (XCAR (XCDR (tem1)));
ee78dc32
GV
3194 window_prompting |= XNegative;
3195 }
8e713be6
KR
3196 else if (CONSP (tem1) && EQ (XCAR (tem1), Qplus)
3197 && CONSP (XCDR (tem1))
3198 && INTEGERP (XCAR (XCDR (tem1))))
ee78dc32 3199 {
8e713be6 3200 f->output_data.w32->left_pos = XINT (XCAR (XCDR (tem1)));
ee78dc32
GV
3201 }
3202 else if (EQ (tem1, Qunbound))
fbd6baed 3203 f->output_data.w32->left_pos = 0;
ee78dc32
GV
3204 else
3205 {
3206 CHECK_NUMBER (tem1, 0);
fbd6baed
GV
3207 f->output_data.w32->left_pos = XINT (tem1);
3208 if (f->output_data.w32->left_pos < 0)
ee78dc32
GV
3209 window_prompting |= XNegative;
3210 }
3211
3212 if (!NILP (tem2) && ! EQ (tem2, Qunbound))
3213 window_prompting |= USPosition;
3214 else
3215 window_prompting |= PPosition;
3216 }
3217
3218 return window_prompting;
3219}
3220
3221\f
3222
fbd6baed 3223extern LRESULT CALLBACK w32_wnd_proc ();
ee78dc32
GV
3224
3225BOOL
fbd6baed 3226w32_init_class (hinst)
ee78dc32
GV
3227 HINSTANCE hinst;
3228{
3229 WNDCLASS wc;
3230
5ac45f98 3231 wc.style = CS_HREDRAW | CS_VREDRAW;
fbd6baed 3232 wc.lpfnWndProc = (WNDPROC) w32_wnd_proc;
ee78dc32
GV
3233 wc.cbClsExtra = 0;
3234 wc.cbWndExtra = WND_EXTRA_BYTES;
3235 wc.hInstance = hinst;
3236 wc.hIcon = LoadIcon (hinst, EMACS_CLASS);
3237 wc.hCursor = LoadCursor (NULL, IDC_ARROW);
4587b026 3238 wc.hbrBackground = NULL; /* GetStockObject (WHITE_BRUSH); */
ee78dc32
GV
3239 wc.lpszMenuName = NULL;
3240 wc.lpszClassName = EMACS_CLASS;
3241
3242 return (RegisterClass (&wc));
3243}
3244
3245HWND
fbd6baed 3246w32_createscrollbar (f, bar)
ee78dc32
GV
3247 struct frame *f;
3248 struct scroll_bar * bar;
3249{
3250 return (CreateWindow ("SCROLLBAR", "", SBS_VERT | WS_CHILD | WS_VISIBLE,
3251 /* Position and size of scroll bar. */
6fc2811b
JR
3252 XINT(bar->left) + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
3253 XINT(bar->top),
3254 XINT(bar->width) - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
3255 XINT(bar->height),
fbd6baed 3256 FRAME_W32_WINDOW (f),
ee78dc32
GV
3257 NULL,
3258 hinst,
3259 NULL));
3260}
3261
3262void
fbd6baed 3263w32_createwindow (f)
ee78dc32
GV
3264 struct frame *f;
3265{
3266 HWND hwnd;
1edf84e7
GV
3267 RECT rect;
3268
3269 rect.left = rect.top = 0;
3270 rect.right = PIXEL_WIDTH (f);
3271 rect.bottom = PIXEL_HEIGHT (f);
3272
3273 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
3274 FRAME_EXTERNAL_MENU_BAR (f));
ee78dc32
GV
3275
3276 /* Do first time app init */
3277
3278 if (!hprevinst)
3279 {
fbd6baed 3280 w32_init_class (hinst);
ee78dc32
GV
3281 }
3282
1edf84e7
GV
3283 FRAME_W32_WINDOW (f) = hwnd
3284 = CreateWindow (EMACS_CLASS,
3285 f->namebuf,
3286 f->output_data.w32->dwStyle | WS_CLIPCHILDREN,
3287 f->output_data.w32->left_pos,
3288 f->output_data.w32->top_pos,
3289 rect.right - rect.left,
3290 rect.bottom - rect.top,
3291 NULL,
3292 NULL,
3293 hinst,
3294 NULL);
3295
ee78dc32
GV
3296 if (hwnd)
3297 {
1edf84e7
GV
3298 SetWindowLong (hwnd, WND_FONTWIDTH_INDEX, FONT_WIDTH (f->output_data.w32->font));
3299 SetWindowLong (hwnd, WND_LINEHEIGHT_INDEX, f->output_data.w32->line_height);
3300 SetWindowLong (hwnd, WND_BORDER_INDEX, f->output_data.w32->internal_border_width);
3301 SetWindowLong (hwnd, WND_SCROLLBAR_INDEX, f->output_data.w32->vertical_scroll_bar_extra);
6fc2811b 3302 SetWindowLong (hwnd, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
ee78dc32 3303
cb9e33d4
RS
3304 /* Enable drag-n-drop. */
3305 DragAcceptFiles (hwnd, TRUE);
3306
5ac45f98
GV
3307 /* Do this to discard the default setting specified by our parent. */
3308 ShowWindow (hwnd, SW_HIDE);
3c190163 3309 }
3c190163
GV
3310}
3311
ee78dc32
GV
3312void
3313my_post_msg (wmsg, hwnd, msg, wParam, lParam)
fbd6baed 3314 W32Msg * wmsg;
ee78dc32
GV
3315 HWND hwnd;
3316 UINT msg;
3317 WPARAM wParam;
3318 LPARAM lParam;
3319{
3320 wmsg->msg.hwnd = hwnd;
3321 wmsg->msg.message = msg;
3322 wmsg->msg.wParam = wParam;
3323 wmsg->msg.lParam = lParam;
3324 wmsg->msg.time = GetMessageTime ();
3325
3326 post_msg (wmsg);
3327}
3328
e9e23e23 3329/* GetKeyState and MapVirtualKey on Windows 95 do not actually distinguish
a1a80b40
GV
3330 between left and right keys as advertised. We test for this
3331 support dynamically, and set a flag when the support is absent. If
3332 absent, we keep track of the left and right control and alt keys
3333 ourselves. This is particularly necessary on keyboards that rely
3334 upon the AltGr key, which is represented as having the left control
3335 and right alt keys pressed. For these keyboards, we need to know
3336 when the left alt key has been pressed in addition to the AltGr key
3337 so that we can properly support M-AltGr-key sequences (such as M-@
3338 on Swedish keyboards). */
3339
3340#define EMACS_LCONTROL 0
3341#define EMACS_RCONTROL 1
3342#define EMACS_LMENU 2
3343#define EMACS_RMENU 3
3344
3345static int modifiers[4];
3346static int modifiers_recorded;
3347static int modifier_key_support_tested;
3348
3349static void
3350test_modifier_support (unsigned int wparam)
3351{
3352 unsigned int l, r;
3353
3354 if (wparam != VK_CONTROL && wparam != VK_MENU)
3355 return;
3356 if (wparam == VK_CONTROL)
3357 {
3358 l = VK_LCONTROL;
3359 r = VK_RCONTROL;
3360 }
3361 else
3362 {
3363 l = VK_LMENU;
3364 r = VK_RMENU;
3365 }
3366 if (!(GetKeyState (l) & 0x8000) && !(GetKeyState (r) & 0x8000))
3367 modifiers_recorded = 1;
3368 else
3369 modifiers_recorded = 0;
3370 modifier_key_support_tested = 1;
3371}
3372
3373static void
3374record_keydown (unsigned int wparam, unsigned int lparam)
3375{
3376 int i;
3377
3378 if (!modifier_key_support_tested)
3379 test_modifier_support (wparam);
3380
3381 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
3382 return;
3383
3384 if (wparam == VK_CONTROL)
3385 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
3386 else
3387 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
3388
3389 modifiers[i] = 1;
3390}
3391
3392static void
3393record_keyup (unsigned int wparam, unsigned int lparam)
3394{
3395 int i;
3396
3397 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
3398 return;
3399
3400 if (wparam == VK_CONTROL)
3401 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
3402 else
3403 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
3404
3405 modifiers[i] = 0;
3406}
3407
da36a4d6
GV
3408/* Emacs can lose focus while a modifier key has been pressed. When
3409 it regains focus, be conservative and clear all modifiers since
3410 we cannot reconstruct the left and right modifier state. */
3411static void
3412reset_modifiers ()
3413{
8681157a
RS
3414 SHORT ctrl, alt;
3415
adcc3809
GV
3416 if (GetFocus () == NULL)
3417 /* Emacs doesn't have keyboard focus. Do nothing. */
da36a4d6 3418 return;
8681157a
RS
3419
3420 ctrl = GetAsyncKeyState (VK_CONTROL);
3421 alt = GetAsyncKeyState (VK_MENU);
3422
8681157a
RS
3423 if (!(ctrl & 0x08000))
3424 /* Clear any recorded control modifier state. */
3425 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
3426
3427 if (!(alt & 0x08000))
3428 /* Clear any recorded alt modifier state. */
3429 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
3430
adcc3809
GV
3431 /* Update the state of all modifier keys, because modifiers used in
3432 hot-key combinations can get stuck on if Emacs loses focus as a
3433 result of a hot-key being pressed. */
3434 {
3435 BYTE keystate[256];
3436
3437#define CURRENT_STATE(key) ((GetAsyncKeyState (key) & 0x8000) >> 8)
3438
3439 GetKeyboardState (keystate);
3440 keystate[VK_SHIFT] = CURRENT_STATE (VK_SHIFT);
3441 keystate[VK_CONTROL] = CURRENT_STATE (VK_CONTROL);
3442 keystate[VK_LCONTROL] = CURRENT_STATE (VK_LCONTROL);
3443 keystate[VK_RCONTROL] = CURRENT_STATE (VK_RCONTROL);
3444 keystate[VK_MENU] = CURRENT_STATE (VK_MENU);
3445 keystate[VK_LMENU] = CURRENT_STATE (VK_LMENU);
3446 keystate[VK_RMENU] = CURRENT_STATE (VK_RMENU);
3447 keystate[VK_LWIN] = CURRENT_STATE (VK_LWIN);
3448 keystate[VK_RWIN] = CURRENT_STATE (VK_RWIN);
3449 keystate[VK_APPS] = CURRENT_STATE (VK_APPS);
3450 SetKeyboardState (keystate);
3451 }
da36a4d6
GV
3452}
3453
7830e24b
RS
3454/* Synchronize modifier state with what is reported with the current
3455 keystroke. Even if we cannot distinguish between left and right
3456 modifier keys, we know that, if no modifiers are set, then neither
3457 the left or right modifier should be set. */
3458static void
3459sync_modifiers ()
3460{
3461 if (!modifiers_recorded)
3462 return;
3463
3464 if (!(GetKeyState (VK_CONTROL) & 0x8000))
3465 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
3466
3467 if (!(GetKeyState (VK_MENU) & 0x8000))
3468 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
3469}
3470
a1a80b40
GV
3471static int
3472modifier_set (int vkey)
3473{
ccc2d29c 3474 if (vkey == VK_CAPITAL || vkey == VK_SCROLL)
891560d6 3475 return (GetKeyState (vkey) & 0x1);
a1a80b40
GV
3476 if (!modifiers_recorded)
3477 return (GetKeyState (vkey) & 0x8000);
3478
3479 switch (vkey)
3480 {
3481 case VK_LCONTROL:
3482 return modifiers[EMACS_LCONTROL];
3483 case VK_RCONTROL:
3484 return modifiers[EMACS_RCONTROL];
3485 case VK_LMENU:
3486 return modifiers[EMACS_LMENU];
3487 case VK_RMENU:
3488 return modifiers[EMACS_RMENU];
a1a80b40
GV
3489 }
3490 return (GetKeyState (vkey) & 0x8000);
3491}
3492
ccc2d29c
GV
3493/* Convert between the modifier bits W32 uses and the modifier bits
3494 Emacs uses. */
3495
3496unsigned int
3497w32_key_to_modifier (int key)
3498{
3499 Lisp_Object key_mapping;
3500
3501 switch (key)
3502 {
3503 case VK_LWIN:
3504 key_mapping = Vw32_lwindow_modifier;
3505 break;
3506 case VK_RWIN:
3507 key_mapping = Vw32_rwindow_modifier;
3508 break;
3509 case VK_APPS:
3510 key_mapping = Vw32_apps_modifier;
3511 break;
3512 case VK_SCROLL:
3513 key_mapping = Vw32_scroll_lock_modifier;
3514 break;
3515 default:
3516 key_mapping = Qnil;
3517 }
3518
adcc3809
GV
3519 /* NB. This code runs in the input thread, asychronously to the lisp
3520 thread, so we must be careful to ensure access to lisp data is
3521 thread-safe. The following code is safe because the modifier
3522 variable values are updated atomically from lisp and symbols are
3523 not relocated by GC. Also, we don't have to worry about seeing GC
3524 markbits here. */
3525 if (EQ (key_mapping, Qhyper))
ccc2d29c 3526 return hyper_modifier;
adcc3809 3527 if (EQ (key_mapping, Qsuper))
ccc2d29c 3528 return super_modifier;
adcc3809 3529 if (EQ (key_mapping, Qmeta))
ccc2d29c 3530 return meta_modifier;
adcc3809 3531 if (EQ (key_mapping, Qalt))
ccc2d29c 3532 return alt_modifier;
adcc3809 3533 if (EQ (key_mapping, Qctrl))
ccc2d29c 3534 return ctrl_modifier;
adcc3809 3535 if (EQ (key_mapping, Qcontrol)) /* synonym for ctrl */
ccc2d29c 3536 return ctrl_modifier;
adcc3809 3537 if (EQ (key_mapping, Qshift))
ccc2d29c
GV
3538 return shift_modifier;
3539
3540 /* Don't generate any modifier if not explicitly requested. */
3541 return 0;
3542}
3543
3544unsigned int
3545w32_get_modifiers ()
3546{
3547 return ((modifier_set (VK_SHIFT) ? shift_modifier : 0) |
3548 (modifier_set (VK_CONTROL) ? ctrl_modifier : 0) |
3549 (modifier_set (VK_LWIN) ? w32_key_to_modifier (VK_LWIN) : 0) |
3550 (modifier_set (VK_RWIN) ? w32_key_to_modifier (VK_RWIN) : 0) |
3551 (modifier_set (VK_APPS) ? w32_key_to_modifier (VK_APPS) : 0) |
3552 (modifier_set (VK_SCROLL) ? w32_key_to_modifier (VK_SCROLL) : 0) |
3553 (modifier_set (VK_MENU) ?
3554 ((NILP (Vw32_alt_is_meta)) ? alt_modifier : meta_modifier) : 0));
3555}
3556
a1a80b40
GV
3557/* We map the VK_* modifiers into console modifier constants
3558 so that we can use the same routines to handle both console
3559 and window input. */
3560
3561static int
ccc2d29c 3562construct_console_modifiers ()
a1a80b40
GV
3563{
3564 int mods;
3565
a1a80b40
GV
3566 mods = 0;
3567 mods |= (modifier_set (VK_SHIFT)) ? SHIFT_PRESSED : 0;
3568 mods |= (modifier_set (VK_CAPITAL)) ? CAPSLOCK_ON : 0;
ccc2d29c
GV
3569 mods |= (modifier_set (VK_SCROLL)) ? SCROLLLOCK_ON : 0;
3570 mods |= (modifier_set (VK_NUMLOCK)) ? NUMLOCK_ON : 0;
a1a80b40
GV
3571 mods |= (modifier_set (VK_LCONTROL)) ? LEFT_CTRL_PRESSED : 0;
3572 mods |= (modifier_set (VK_RCONTROL)) ? RIGHT_CTRL_PRESSED : 0;
3573 mods |= (modifier_set (VK_LMENU)) ? LEFT_ALT_PRESSED : 0;
3574 mods |= (modifier_set (VK_RMENU)) ? RIGHT_ALT_PRESSED : 0;
ccc2d29c
GV
3575 mods |= (modifier_set (VK_LWIN)) ? LEFT_WIN_PRESSED : 0;
3576 mods |= (modifier_set (VK_RWIN)) ? RIGHT_WIN_PRESSED : 0;
3577 mods |= (modifier_set (VK_APPS)) ? APPS_PRESSED : 0;
a1a80b40
GV
3578
3579 return mods;
3580}
3581
ccc2d29c
GV
3582static int
3583w32_get_key_modifiers (unsigned int wparam, unsigned int lparam)
da36a4d6 3584{
ccc2d29c
GV
3585 int mods;
3586
3587 /* Convert to emacs modifiers. */
3588 mods = w32_kbd_mods_to_emacs (construct_console_modifiers (), wparam);
3589
3590 return mods;
3591}
da36a4d6 3592
ccc2d29c
GV
3593unsigned int
3594map_keypad_keys (unsigned int virt_key, unsigned int extended)
3595{
3596 if (virt_key < VK_CLEAR || virt_key > VK_DELETE)
3597 return virt_key;
da36a4d6 3598
ccc2d29c 3599 if (virt_key == VK_RETURN)
da36a4d6
GV
3600 return (extended ? VK_NUMPAD_ENTER : VK_RETURN);
3601
ccc2d29c
GV
3602 if (virt_key >= VK_PRIOR && virt_key <= VK_DOWN)
3603 return (!extended ? (VK_NUMPAD_PRIOR + (virt_key - VK_PRIOR)) : virt_key);
3604
3605 if (virt_key == VK_INSERT || virt_key == VK_DELETE)
3606 return (!extended ? (VK_NUMPAD_INSERT + (virt_key - VK_INSERT)) : virt_key);
3607
3608 if (virt_key == VK_CLEAR)
3609 return (!extended ? VK_NUMPAD_CLEAR : virt_key);
3610
3611 return virt_key;
3612}
3613
3614/* List of special key combinations which w32 would normally capture,
3615 but emacs should grab instead. Not directly visible to lisp, to
3616 simplify synchronization. Each item is an integer encoding a virtual
3617 key code and modifier combination to capture. */
3618Lisp_Object w32_grabbed_keys;
3619
3620#define HOTKEY(vk,mods) make_number (((vk) & 255) | ((mods) << 8))
3621#define HOTKEY_ID(k) (XFASTINT (k) & 0xbfff)
3622#define HOTKEY_VK_CODE(k) (XFASTINT (k) & 255)
3623#define HOTKEY_MODIFIERS(k) (XFASTINT (k) >> 8)
3624
3625/* Register hot-keys for reserved key combinations when Emacs has
3626 keyboard focus, since this is the only way Emacs can receive key
3627 combinations like Alt-Tab which are used by the system. */
3628
3629static void
3630register_hot_keys (hwnd)
3631 HWND hwnd;
3632{
3633 Lisp_Object keylist;
3634
3635 /* Use GC_CONSP, since we are called asynchronously. */
3636 for (keylist = w32_grabbed_keys; GC_CONSP (keylist); keylist = XCDR (keylist))
3637 {
3638 Lisp_Object key = XCAR (keylist);
3639
3640 /* Deleted entries get set to nil. */
3641 if (!INTEGERP (key))
3642 continue;
3643
3644 RegisterHotKey (hwnd, HOTKEY_ID (key),
3645 HOTKEY_MODIFIERS (key), HOTKEY_VK_CODE (key));
3646 }
3647}
3648
3649static void
3650unregister_hot_keys (hwnd)
3651 HWND hwnd;
3652{
3653 Lisp_Object keylist;
3654
3655 /* Use GC_CONSP, since we are called asynchronously. */
3656 for (keylist = w32_grabbed_keys; GC_CONSP (keylist); keylist = XCDR (keylist))
3657 {
3658 Lisp_Object key = XCAR (keylist);
3659
3660 if (!INTEGERP (key))
3661 continue;
3662
3663 UnregisterHotKey (hwnd, HOTKEY_ID (key));
3664 }
3665}
3666
5ac45f98
GV
3667/* Main message dispatch loop. */
3668
1edf84e7
GV
3669static void
3670w32_msg_pump (deferred_msg * msg_buf)
5ac45f98
GV
3671{
3672 MSG msg;
ccc2d29c
GV
3673 int result;
3674 HWND focus_window;
93fbe8b7
GV
3675
3676 msh_mousewheel = RegisterWindowMessage (MSH_MOUSEWHEEL);
5ac45f98 3677
5ac45f98
GV
3678 while (GetMessage (&msg, NULL, 0, 0))
3679 {
3680 if (msg.hwnd == NULL)
3681 {
3682 switch (msg.message)
3683 {
3ef68e6b
AI
3684 case WM_NULL:
3685 /* Produced by complete_deferred_msg; just ignore. */
3686 break;
5ac45f98 3687 case WM_EMACS_CREATEWINDOW:
fbd6baed 3688 w32_createwindow ((struct frame *) msg.wParam);
1edf84e7
GV
3689 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
3690 abort ();
5ac45f98 3691 break;
dfdb4047
GV
3692 case WM_EMACS_SETLOCALE:
3693 SetThreadLocale (msg.wParam);
3694 /* Reply is not expected. */
3695 break;
ccc2d29c
GV
3696 case WM_EMACS_SETKEYBOARDLAYOUT:
3697 result = (int) ActivateKeyboardLayout ((HKL) msg.wParam, 0);
3698 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
3699 result, 0))
3700 abort ();
3701 break;
3702 case WM_EMACS_REGISTER_HOT_KEY:
3703 focus_window = GetFocus ();
3704 if (focus_window != NULL)
3705 RegisterHotKey (focus_window,
3706 HOTKEY_ID (msg.wParam),
3707 HOTKEY_MODIFIERS (msg.wParam),
3708 HOTKEY_VK_CODE (msg.wParam));
3709 /* Reply is not expected. */
3710 break;
3711 case WM_EMACS_UNREGISTER_HOT_KEY:
3712 focus_window = GetFocus ();
3713 if (focus_window != NULL)
3714 UnregisterHotKey (focus_window, HOTKEY_ID (msg.wParam));
adcc3809
GV
3715 /* Mark item as erased. NB: this code must be
3716 thread-safe. The next line is okay because the cons
3717 cell is never made into garbage and is not relocated by
3718 GC. */
ccc2d29c
GV
3719 XCAR ((Lisp_Object) msg.lParam) = Qnil;
3720 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
3721 abort ();
3722 break;
adcc3809
GV
3723 case WM_EMACS_TOGGLE_LOCK_KEY:
3724 {
3725 int vk_code = (int) msg.wParam;
3726 int cur_state = (GetKeyState (vk_code) & 1);
3727 Lisp_Object new_state = (Lisp_Object) msg.lParam;
3728
3729 /* NB: This code must be thread-safe. It is safe to
3730 call NILP because symbols are not relocated by GC,
3731 and pointer here is not touched by GC (so the markbit
3732 can't be set). Numbers are safe because they are
3733 immediate values. */
3734 if (NILP (new_state)
3735 || (NUMBERP (new_state)
3736 && (XUINT (new_state)) & 1 != cur_state))
3737 {
3738 one_w32_display_info.faked_key = vk_code;
3739
3740 keybd_event ((BYTE) vk_code,
3741 (BYTE) MapVirtualKey (vk_code, 0),
3742 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
3743 keybd_event ((BYTE) vk_code,
3744 (BYTE) MapVirtualKey (vk_code, 0),
3745 KEYEVENTF_EXTENDEDKEY | 0, 0);
3746 keybd_event ((BYTE) vk_code,
3747 (BYTE) MapVirtualKey (vk_code, 0),
3748 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
3749 cur_state = !cur_state;
3750 }
3751 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
3752 cur_state, 0))
3753 abort ();
3754 }
3755 break;
1edf84e7 3756 default:
1edf84e7 3757 DebPrint (("msg %x not expected by w32_msg_pump\n", msg.message));
5ac45f98
GV
3758 }
3759 }
3760 else
3761 {
3762 DispatchMessage (&msg);
3763 }
1edf84e7
GV
3764
3765 /* Exit nested loop when our deferred message has completed. */
3766 if (msg_buf->completed)
3767 break;
5ac45f98 3768 }
1edf84e7
GV
3769}
3770
3771deferred_msg * deferred_msg_head;
3772
3773static deferred_msg *
3774find_deferred_msg (HWND hwnd, UINT msg)
3775{
3776 deferred_msg * item;
3777
3778 /* Don't actually need synchronization for read access, since
3779 modification of single pointer is always atomic. */
3780 /* enter_crit (); */
3781
3782 for (item = deferred_msg_head; item != NULL; item = item->next)
3783 if (item->w32msg.msg.hwnd == hwnd
3784 && item->w32msg.msg.message == msg)
3785 break;
3786
3787 /* leave_crit (); */
3788
3789 return item;
3790}
3791
3792static LRESULT
3793send_deferred_msg (deferred_msg * msg_buf,
3794 HWND hwnd,
3795 UINT msg,
3796 WPARAM wParam,
3797 LPARAM lParam)
3798{
3799 /* Only input thread can send deferred messages. */
3800 if (GetCurrentThreadId () != dwWindowsThreadId)
3801 abort ();
3802
3803 /* It is an error to send a message that is already deferred. */
3804 if (find_deferred_msg (hwnd, msg) != NULL)
3805 abort ();
3806
3807 /* Enforced synchronization is not needed because this is the only
3808 function that alters deferred_msg_head, and the following critical
3809 section is guaranteed to only be serially reentered (since only the
3810 input thread can call us). */
3811
3812 /* enter_crit (); */
3813
3814 msg_buf->completed = 0;
3815 msg_buf->next = deferred_msg_head;
3816 deferred_msg_head = msg_buf;
3817 my_post_msg (&msg_buf->w32msg, hwnd, msg, wParam, lParam);
3818
3819 /* leave_crit (); */
3820
3821 /* Start a new nested message loop to process other messages until
3822 this one is completed. */
3823 w32_msg_pump (msg_buf);
3824
3825 deferred_msg_head = msg_buf->next;
3826
3827 return msg_buf->result;
3828}
3829
3830void
3831complete_deferred_msg (HWND hwnd, UINT msg, LRESULT result)
3832{
3833 deferred_msg * msg_buf = find_deferred_msg (hwnd, msg);
3834
3835 if (msg_buf == NULL)
3ef68e6b
AI
3836 /* Message may have been cancelled, so don't abort(). */
3837 return;
1edf84e7
GV
3838
3839 msg_buf->result = result;
3840 msg_buf->completed = 1;
3841
3842 /* Ensure input thread is woken so it notices the completion. */
3843 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
3844}
3845
3ef68e6b
AI
3846void
3847cancel_all_deferred_msgs ()
3848{
3849 deferred_msg * item;
3850
3851 /* Don't actually need synchronization for read access, since
3852 modification of single pointer is always atomic. */
3853 /* enter_crit (); */
3854
3855 for (item = deferred_msg_head; item != NULL; item = item->next)
3856 {
3857 item->result = 0;
3858 item->completed = 1;
3859 }
3860
3861 /* leave_crit (); */
3862
3863 /* Ensure input thread is woken so it notices the completion. */
3864 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
3865}
1edf84e7
GV
3866
3867DWORD
3868w32_msg_worker (dw)
3869 DWORD dw;
3870{
3871 MSG msg;
3872 deferred_msg dummy_buf;
3873
3874 /* Ensure our message queue is created */
3875
3876 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
5ac45f98 3877
1edf84e7
GV
3878 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
3879 abort ();
3880
3881 memset (&dummy_buf, 0, sizeof (dummy_buf));
3882 dummy_buf.w32msg.msg.hwnd = NULL;
3883 dummy_buf.w32msg.msg.message = WM_NULL;
3884
3885 /* This is the inital message loop which should only exit when the
3886 application quits. */
3887 w32_msg_pump (&dummy_buf);
3888
3889 return 0;
5ac45f98
GV
3890}
3891
3ef68e6b
AI
3892static void
3893post_character_message (hwnd, msg, wParam, lParam, modifiers)
3894 HWND hwnd;
3895 UINT msg;
3896 WPARAM wParam;
3897 LPARAM lParam;
3898 DWORD modifiers;
3899
3900{
3901 W32Msg wmsg;
3902
3903 wmsg.dwModifiers = modifiers;
3904
3905 /* Detect quit_char and set quit-flag directly. Note that we
3906 still need to post a message to ensure the main thread will be
3907 woken up if blocked in sys_select(), but we do NOT want to post
3908 the quit_char message itself (because it will usually be as if
3909 the user had typed quit_char twice). Instead, we post a dummy
3910 message that has no particular effect. */
3911 {
3912 int c = wParam;
3913 if (isalpha (c) && wmsg.dwModifiers == ctrl_modifier)
3914 c = make_ctrl_char (c) & 0377;
7d081355
AI
3915 if (c == quit_char
3916 || (wmsg.dwModifiers == 0 &&
3917 XFASTINT (Vw32_quit_key) && wParam == XFASTINT (Vw32_quit_key)))
3ef68e6b
AI
3918 {
3919 Vquit_flag = Qt;
3920
3921 /* The choice of message is somewhat arbitrary, as long as
3922 the main thread handler just ignores it. */
3923 msg = WM_NULL;
3924
3925 /* Interrupt any blocking system calls. */
3926 signal_quit ();
3927
3928 /* As a safety precaution, forcibly complete any deferred
3929 messages. This is a kludge, but I don't see any particularly
3930 clean way to handle the situation where a deferred message is
3931 "dropped" in the lisp thread, and will thus never be
3932 completed, eg. by the user trying to activate the menubar
3933 when the lisp thread is busy, and then typing C-g when the
3934 menubar doesn't open promptly (with the result that the
3935 menubar never responds at all because the deferred
3936 WM_INITMENU message is never completed). Another problem
3937 situation is when the lisp thread calls SendMessage (to send
3938 a window manager command) when a message has been deferred;
3939 the lisp thread gets blocked indefinitely waiting for the
3940 deferred message to be completed, which itself is waiting for
3941 the lisp thread to respond.
3942
3943 Note that we don't want to block the input thread waiting for
3944 a reponse from the lisp thread (although that would at least
3945 solve the deadlock problem above), because we want to be able
3946 to receive C-g to interrupt the lisp thread. */
3947 cancel_all_deferred_msgs ();
3948 }
3949 }
3950
3951 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3952}
3953
ee78dc32
GV
3954/* Main window procedure */
3955
ee78dc32 3956LRESULT CALLBACK
fbd6baed 3957w32_wnd_proc (hwnd, msg, wParam, lParam)
ee78dc32
GV
3958 HWND hwnd;
3959 UINT msg;
3960 WPARAM wParam;
3961 LPARAM lParam;
3962{
3963 struct frame *f;
fbd6baed
GV
3964 struct w32_display_info *dpyinfo = &one_w32_display_info;
3965 W32Msg wmsg;
84fb1139 3966 int windows_translate;
576ba81c 3967 int key;
84fb1139 3968
a6085637
KH
3969 /* Note that it is okay to call x_window_to_frame, even though we are
3970 not running in the main lisp thread, because frame deletion
3971 requires the lisp thread to synchronize with this thread. Thus, if
3972 a frame struct is returned, it can be used without concern that the
3973 lisp thread might make it disappear while we are using it.
3974
3975 NB. Walking the frame list in this thread is safe (as long as
3976 writes of Lisp_Object slots are atomic, which they are on Windows).
3977 Although delete-frame can destructively modify the frame list while
3978 we are walking it, a garbage collection cannot occur until after
3979 delete-frame has synchronized with this thread.
3980
3981 It is also safe to use functions that make GDI calls, such as
fbd6baed 3982 w32_clear_rect, because these functions must obtain a DC handle
a6085637
KH
3983 from the frame struct using get_frame_dc which is thread-aware. */
3984
ee78dc32
GV
3985 switch (msg)
3986 {
3987 case WM_ERASEBKGND:
a6085637
KH
3988 f = x_window_to_frame (dpyinfo, hwnd);
3989 if (f)
3990 {
9badad41 3991 HDC hdc = get_frame_dc (f);
a6085637 3992 GetUpdateRect (hwnd, &wmsg.rect, FALSE);
9badad41
JR
3993 w32_clear_rect (f, hdc, &wmsg.rect);
3994 release_frame_dc (f, hdc);
ce6059da
AI
3995
3996#if defined (W32_DEBUG_DISPLAY)
3997 DebPrint (("WM_ERASEBKGND: erasing %d,%d-%d,%d\n",
3998 wmsg.rect.left, wmsg.rect.top, wmsg.rect.right,
3999 wmsg.rect.bottom));
4000#endif /* W32_DEBUG_DISPLAY */
a6085637 4001 }
5ac45f98
GV
4002 return 1;
4003 case WM_PALETTECHANGED:
4004 /* ignore our own changes */
4005 if ((HWND)wParam != hwnd)
4006 {
a6085637
KH
4007 f = x_window_to_frame (dpyinfo, hwnd);
4008 if (f)
4009 /* get_frame_dc will realize our palette and force all
4010 frames to be redrawn if needed. */
4011 release_frame_dc (f, get_frame_dc (f));
5ac45f98
GV
4012 }
4013 return 0;
ee78dc32 4014 case WM_PAINT:
ce6059da 4015 {
55dcfc15
AI
4016 PAINTSTRUCT paintStruct;
4017 RECT update_rect;
4018
4019 /* MSDN Docs say not to call BeginPaint if GetUpdateRect
4020 fails. Apparently this can happen under some
4021 circumstances. */
c0611964 4022 if (!w32_strict_painting || GetUpdateRect (hwnd, &update_rect, FALSE))
55dcfc15
AI
4023 {
4024 enter_crit ();
4025 BeginPaint (hwnd, &paintStruct);
4026
c0611964
AI
4027 if (w32_strict_painting)
4028 /* The rectangles returned by GetUpdateRect and BeginPaint
4029 do not always match. GetUpdateRect seems to be the
4030 more reliable of the two. */
4031 wmsg.rect = update_rect;
4032 else
4033 wmsg.rect = paintStruct.rcPaint;
55dcfc15
AI
4034
4035#if defined (W32_DEBUG_DISPLAY)
4036 DebPrint (("WM_PAINT: painting %d,%d-%d,%d\n", wmsg.rect.left,
4037 wmsg.rect.top, wmsg.rect.right, wmsg.rect.bottom));
4038 DebPrint (("WM_PAINT: update region is %d,%d-%d,%d\n",
4039 update_rect.left, update_rect.top,
4040 update_rect.right, update_rect.bottom));
4041#endif
4042 EndPaint (hwnd, &paintStruct);
4043 leave_crit ();
4044
4045 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4046
4047 return 0;
4048 }
c0611964
AI
4049
4050 /* If GetUpdateRect returns 0 (meaning there is no update
4051 region), assume the whole window needs to be repainted. */
4052 GetClientRect(hwnd, &wmsg.rect);
4053 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4054 return 0;
ee78dc32 4055 }
a1a80b40 4056
ccc2d29c
GV
4057 case WM_INPUTLANGCHANGE:
4058 /* Inform lisp thread of keyboard layout changes. */
4059 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4060
4061 /* Clear dead keys in the keyboard state; for simplicity only
4062 preserve modifier key states. */
4063 {
4064 int i;
4065 BYTE keystate[256];
4066
4067 GetKeyboardState (keystate);
4068 for (i = 0; i < 256; i++)
4069 if (1
4070 && i != VK_SHIFT
4071 && i != VK_LSHIFT
4072 && i != VK_RSHIFT
4073 && i != VK_CAPITAL
4074 && i != VK_NUMLOCK
4075 && i != VK_SCROLL
4076 && i != VK_CONTROL
4077 && i != VK_LCONTROL
4078 && i != VK_RCONTROL
4079 && i != VK_MENU
4080 && i != VK_LMENU
4081 && i != VK_RMENU
4082 && i != VK_LWIN
4083 && i != VK_RWIN)
4084 keystate[i] = 0;
4085 SetKeyboardState (keystate);
4086 }
4087 goto dflt;
4088
4089 case WM_HOTKEY:
4090 /* Synchronize hot keys with normal input. */
4091 PostMessage (hwnd, WM_KEYDOWN, HIWORD (lParam), 0);
4092 return (0);
4093
a1a80b40
GV
4094 case WM_KEYUP:
4095 case WM_SYSKEYUP:
4096 record_keyup (wParam, lParam);
4097 goto dflt;
4098
ee78dc32
GV
4099 case WM_KEYDOWN:
4100 case WM_SYSKEYDOWN:
ccc2d29c
GV
4101 /* Ignore keystrokes we fake ourself; see below. */
4102 if (dpyinfo->faked_key == wParam)
4103 {
4104 dpyinfo->faked_key = 0;
576ba81c
AI
4105 /* Make sure TranslateMessage sees them though (as long as
4106 they don't produce WM_CHAR messages). This ensures that
4107 indicator lights are toggled promptly on Windows 9x, for
4108 example. */
4109 if (lispy_function_keys[wParam] != 0)
4110 {
4111 windows_translate = 1;
4112 goto translate;
4113 }
4114 return 0;
ccc2d29c
GV
4115 }
4116
7830e24b
RS
4117 /* Synchronize modifiers with current keystroke. */
4118 sync_modifiers ();
a1a80b40 4119 record_keydown (wParam, lParam);
ccc2d29c 4120 wParam = map_keypad_keys (wParam, (lParam & 0x1000000L) != 0);
84fb1139
KH
4121
4122 windows_translate = 0;
ccc2d29c
GV
4123
4124 switch (wParam)
4125 {
4126 case VK_LWIN:
4127 if (NILP (Vw32_pass_lwindow_to_system))
4128 {
4129 /* Prevent system from acting on keyup (which opens the
4130 Start menu if no other key was pressed) by simulating a
4131 press of Space which we will ignore. */
4132 if (GetAsyncKeyState (wParam) & 1)
4133 {
adcc3809 4134 if (NUMBERP (Vw32_phantom_key_code))
576ba81c 4135 key = XUINT (Vw32_phantom_key_code) & 255;
adcc3809 4136 else
576ba81c
AI
4137 key = VK_SPACE;
4138 dpyinfo->faked_key = key;
4139 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
ccc2d29c
GV
4140 }
4141 }
4142 if (!NILP (Vw32_lwindow_modifier))
4143 return 0;
4144 break;
4145 case VK_RWIN:
4146 if (NILP (Vw32_pass_rwindow_to_system))
4147 {
4148 if (GetAsyncKeyState (wParam) & 1)
4149 {
adcc3809 4150 if (NUMBERP (Vw32_phantom_key_code))
576ba81c 4151 key = XUINT (Vw32_phantom_key_code) & 255;
adcc3809 4152 else
576ba81c
AI
4153 key = VK_SPACE;
4154 dpyinfo->faked_key = key;
4155 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
ccc2d29c
GV
4156 }
4157 }
4158 if (!NILP (Vw32_rwindow_modifier))
4159 return 0;
4160 break;
576ba81c 4161 case VK_APPS:
ccc2d29c
GV
4162 if (!NILP (Vw32_apps_modifier))
4163 return 0;
4164 break;
4165 case VK_MENU:
4166 if (NILP (Vw32_pass_alt_to_system))
adcc3809
GV
4167 /* Prevent DefWindowProc from activating the menu bar if an
4168 Alt key is pressed and released by itself. */
ccc2d29c 4169 return 0;
84fb1139 4170 windows_translate = 1;
ccc2d29c
GV
4171 break;
4172 case VK_CAPITAL:
4173 /* Decide whether to treat as modifier or function key. */
4174 if (NILP (Vw32_enable_caps_lock))
4175 goto disable_lock_key;
adcc3809
GV
4176 windows_translate = 1;
4177 break;
ccc2d29c
GV
4178 case VK_NUMLOCK:
4179 /* Decide whether to treat as modifier or function key. */
4180 if (NILP (Vw32_enable_num_lock))
4181 goto disable_lock_key;
adcc3809
GV
4182 windows_translate = 1;
4183 break;
ccc2d29c
GV
4184 case VK_SCROLL:
4185 /* Decide whether to treat as modifier or function key. */
4186 if (NILP (Vw32_scroll_lock_modifier))
4187 goto disable_lock_key;
adcc3809
GV
4188 windows_translate = 1;
4189 break;
ccc2d29c 4190 disable_lock_key:
adcc3809
GV
4191 /* Ensure the appropriate lock key state (and indicator light)
4192 remains in the same state. We do this by faking another
4193 press of the relevant key. Apparently, this really is the
4194 only way to toggle the state of the indicator lights. */
4195 dpyinfo->faked_key = wParam;
4196 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
4197 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
4198 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
4199 KEYEVENTF_EXTENDEDKEY | 0, 0);
4200 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
4201 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
4202 /* Ensure indicator lights are updated promptly on Windows 9x
4203 (TranslateMessage apparently does this), after forwarding
4204 input event. */
4205 post_character_message (hwnd, msg, wParam, lParam,
4206 w32_get_key_modifiers (wParam, lParam));
4207 windows_translate = 1;
ccc2d29c
GV
4208 break;
4209 case VK_CONTROL:
4210 case VK_SHIFT:
4211 case VK_PROCESSKEY: /* Generated by IME. */
4212 windows_translate = 1;
4213 break;
adcc3809
GV
4214 case VK_CANCEL:
4215 /* Windows maps Ctrl-Pause (aka Ctrl-Break) into VK_CANCEL,
4216 which is confusing for purposes of key binding; convert
4217 VK_CANCEL events into VK_PAUSE events. */
4218 wParam = VK_PAUSE;
4219 break;
4220 case VK_PAUSE:
4221 /* Windows maps Ctrl-NumLock into VK_PAUSE, which is confusing
4222 for purposes of key binding; convert these back into
4223 VK_NUMLOCK events, at least when we want to see NumLock key
4224 presses. (Note that there is never any possibility that
4225 VK_PAUSE with Ctrl really is C-Pause as per above.) */
4226 if (NILP (Vw32_enable_num_lock) && modifier_set (VK_CONTROL))
4227 wParam = VK_NUMLOCK;
4228 break;
ccc2d29c
GV
4229 default:
4230 /* If not defined as a function key, change it to a WM_CHAR message. */
4231 if (lispy_function_keys[wParam] == 0)
4232 {
adcc3809
GV
4233 DWORD modifiers = construct_console_modifiers ();
4234
ccc2d29c
GV
4235 if (!NILP (Vw32_recognize_altgr)
4236 && modifier_set (VK_LCONTROL) && modifier_set (VK_RMENU))
4237 {
4238 /* Always let TranslateMessage handle AltGr key chords;
4239 for some reason, ToAscii doesn't always process AltGr
4240 chords correctly. */
4241 windows_translate = 1;
4242 }
adcc3809 4243 else if ((modifiers & (~SHIFT_PRESSED & ~CAPSLOCK_ON)) != 0)
ccc2d29c 4244 {
adcc3809
GV
4245 /* Handle key chords including any modifiers other
4246 than shift directly, in order to preserve as much
4247 modifier information as possible. */
ccc2d29c
GV
4248 if ('A' <= wParam && wParam <= 'Z')
4249 {
4250 /* Don't translate modified alphabetic keystrokes,
4251 so the user doesn't need to constantly switch
4252 layout to type control or meta keystrokes when
4253 the normal layout translates alphabetic
4254 characters to non-ascii characters. */
4255 if (!modifier_set (VK_SHIFT))
4256 wParam += ('a' - 'A');
4257 msg = WM_CHAR;
4258 }
4259 else
4260 {
4261 /* Try to handle other keystrokes by determining the
4262 base character (ie. translating the base key plus
4263 shift modifier). */
4264 int add;
4265 int isdead = 0;
4266 KEY_EVENT_RECORD key;
4267
4268 key.bKeyDown = TRUE;
4269 key.wRepeatCount = 1;
4270 key.wVirtualKeyCode = wParam;
4271 key.wVirtualScanCode = (lParam & 0xFF0000) >> 16;
4272 key.uChar.AsciiChar = 0;
adcc3809 4273 key.dwControlKeyState = modifiers;
ccc2d29c
GV
4274
4275 add = w32_kbd_patch_key (&key);
4276 /* 0 means an unrecognised keycode, negative means
4277 dead key. Ignore both. */
4278 while (--add >= 0)
4279 {
4280 /* Forward asciified character sequence. */
4281 post_character_message
4282 (hwnd, WM_CHAR, key.uChar.AsciiChar, lParam,
4283 w32_get_key_modifiers (wParam, lParam));
4284 w32_kbd_patch_key (&key);
4285 }
4286 return 0;
4287 }
4288 }
4289 else
4290 {
4291 /* Let TranslateMessage handle everything else. */
4292 windows_translate = 1;
4293 }
4294 }
4295 }
a1a80b40 4296
adcc3809 4297 translate:
84fb1139
KH
4298 if (windows_translate)
4299 {
e9e23e23 4300 MSG windows_msg = { hwnd, msg, wParam, lParam, 0, {0,0} };
84fb1139 4301
e9e23e23
GV
4302 windows_msg.time = GetMessageTime ();
4303 TranslateMessage (&windows_msg);
84fb1139
KH
4304 goto dflt;
4305 }
4306
ee78dc32
GV
4307 /* Fall through */
4308
4309 case WM_SYSCHAR:
4310 case WM_CHAR:
ccc2d29c
GV
4311 post_character_message (hwnd, msg, wParam, lParam,
4312 w32_get_key_modifiers (wParam, lParam));
ee78dc32 4313 break;
da36a4d6 4314
5ac45f98
GV
4315 /* Simulate middle mouse button events when left and right buttons
4316 are used together, but only if user has two button mouse. */
ee78dc32 4317 case WM_LBUTTONDOWN:
5ac45f98 4318 case WM_RBUTTONDOWN:
7ce9aaca 4319 if (XINT (Vw32_num_mouse_buttons) > 2)
5ac45f98
GV
4320 goto handle_plain_button;
4321
4322 {
4323 int this = (msg == WM_LBUTTONDOWN) ? LMOUSE : RMOUSE;
4324 int other = (msg == WM_LBUTTONDOWN) ? RMOUSE : LMOUSE;
4325
3cb20f4a
RS
4326 if (button_state & this)
4327 return 0;
5ac45f98
GV
4328
4329 if (button_state == 0)
4330 SetCapture (hwnd);
4331
4332 button_state |= this;
4333
4334 if (button_state & other)
4335 {
84fb1139 4336 if (mouse_button_timer)
5ac45f98 4337 {
84fb1139
KH
4338 KillTimer (hwnd, mouse_button_timer);
4339 mouse_button_timer = 0;
5ac45f98
GV
4340
4341 /* Generate middle mouse event instead. */
4342 msg = WM_MBUTTONDOWN;
4343 button_state |= MMOUSE;
4344 }
4345 else if (button_state & MMOUSE)
4346 {
4347 /* Ignore button event if we've already generated a
4348 middle mouse down event. This happens if the
4349 user releases and press one of the two buttons
4350 after we've faked a middle mouse event. */
4351 return 0;
4352 }
4353 else
4354 {
4355 /* Flush out saved message. */
84fb1139 4356 post_msg (&saved_mouse_button_msg);
5ac45f98 4357 }
fbd6baed 4358 wmsg.dwModifiers = w32_get_modifiers ();
5ac45f98
GV
4359 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4360
4361 /* Clear message buffer. */
84fb1139 4362 saved_mouse_button_msg.msg.hwnd = 0;
5ac45f98
GV
4363 }
4364 else
4365 {
4366 /* Hold onto message for now. */
84fb1139 4367 mouse_button_timer =
adcc3809
GV
4368 SetTimer (hwnd, MOUSE_BUTTON_ID,
4369 XINT (Vw32_mouse_button_tolerance), NULL);
84fb1139
KH
4370 saved_mouse_button_msg.msg.hwnd = hwnd;
4371 saved_mouse_button_msg.msg.message = msg;
4372 saved_mouse_button_msg.msg.wParam = wParam;
4373 saved_mouse_button_msg.msg.lParam = lParam;
4374 saved_mouse_button_msg.msg.time = GetMessageTime ();
fbd6baed 4375 saved_mouse_button_msg.dwModifiers = w32_get_modifiers ();
5ac45f98
GV
4376 }
4377 }
4378 return 0;
4379
ee78dc32 4380 case WM_LBUTTONUP:
5ac45f98 4381 case WM_RBUTTONUP:
7ce9aaca 4382 if (XINT (Vw32_num_mouse_buttons) > 2)
5ac45f98
GV
4383 goto handle_plain_button;
4384
4385 {
4386 int this = (msg == WM_LBUTTONUP) ? LMOUSE : RMOUSE;
4387 int other = (msg == WM_LBUTTONUP) ? RMOUSE : LMOUSE;
4388
3cb20f4a
RS
4389 if ((button_state & this) == 0)
4390 return 0;
5ac45f98
GV
4391
4392 button_state &= ~this;
4393
4394 if (button_state & MMOUSE)
4395 {
4396 /* Only generate event when second button is released. */
4397 if ((button_state & other) == 0)
4398 {
4399 msg = WM_MBUTTONUP;
4400 button_state &= ~MMOUSE;
4401
4402 if (button_state) abort ();
4403 }
4404 else
4405 return 0;
4406 }
4407 else
4408 {
4409 /* Flush out saved message if necessary. */
84fb1139 4410 if (saved_mouse_button_msg.msg.hwnd)
5ac45f98 4411 {
84fb1139 4412 post_msg (&saved_mouse_button_msg);
5ac45f98
GV
4413 }
4414 }
fbd6baed 4415 wmsg.dwModifiers = w32_get_modifiers ();
5ac45f98
GV
4416 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4417
4418 /* Always clear message buffer and cancel timer. */
84fb1139
KH
4419 saved_mouse_button_msg.msg.hwnd = 0;
4420 KillTimer (hwnd, mouse_button_timer);
4421 mouse_button_timer = 0;
5ac45f98
GV
4422
4423 if (button_state == 0)
4424 ReleaseCapture ();
4425 }
4426 return 0;
4427
ee78dc32
GV
4428 case WM_MBUTTONDOWN:
4429 case WM_MBUTTONUP:
5ac45f98 4430 handle_plain_button:
ee78dc32
GV
4431 {
4432 BOOL up;
1edf84e7 4433 int button;
ee78dc32 4434
1edf84e7 4435 if (parse_button (msg, &button, &up))
ee78dc32
GV
4436 {
4437 if (up) ReleaseCapture ();
4438 else SetCapture (hwnd);
1edf84e7
GV
4439 button = (button == 0) ? LMOUSE :
4440 ((button == 1) ? MMOUSE : RMOUSE);
4441 if (up)
4442 button_state &= ~button;
4443 else
4444 button_state |= button;
ee78dc32
GV
4445 }
4446 }
4447
fbd6baed 4448 wmsg.dwModifiers = w32_get_modifiers ();
ee78dc32 4449 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
5ac45f98
GV
4450 return 0;
4451
84fb1139 4452 case WM_VSCROLL:
5ac45f98 4453 case WM_MOUSEMOVE:
fbd6baed 4454 if (XINT (Vw32_mouse_move_interval) <= 0
84fb1139
KH
4455 || (msg == WM_MOUSEMOVE && button_state == 0))
4456 {
fbd6baed 4457 wmsg.dwModifiers = w32_get_modifiers ();
84fb1139
KH
4458 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4459 return 0;
4460 }
4461
4462 /* Hang onto mouse move and scroll messages for a bit, to avoid
4463 sending such events to Emacs faster than it can process them.
4464 If we get more events before the timer from the first message
4465 expires, we just replace the first message. */
4466
4467 if (saved_mouse_move_msg.msg.hwnd == 0)
4468 mouse_move_timer =
adcc3809
GV
4469 SetTimer (hwnd, MOUSE_MOVE_ID,
4470 XINT (Vw32_mouse_move_interval), NULL);
84fb1139
KH
4471
4472 /* Hold onto message for now. */
4473 saved_mouse_move_msg.msg.hwnd = hwnd;
4474 saved_mouse_move_msg.msg.message = msg;
4475 saved_mouse_move_msg.msg.wParam = wParam;
4476 saved_mouse_move_msg.msg.lParam = lParam;
4477 saved_mouse_move_msg.msg.time = GetMessageTime ();
fbd6baed 4478 saved_mouse_move_msg.dwModifiers = w32_get_modifiers ();
84fb1139
KH
4479
4480 return 0;
4481
1edf84e7
GV
4482 case WM_MOUSEWHEEL:
4483 wmsg.dwModifiers = w32_get_modifiers ();
4484 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4485 return 0;
4486
cb9e33d4
RS
4487 case WM_DROPFILES:
4488 wmsg.dwModifiers = w32_get_modifiers ();
4489 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4490 return 0;
4491
84fb1139
KH
4492 case WM_TIMER:
4493 /* Flush out saved messages if necessary. */
4494 if (wParam == mouse_button_timer)
5ac45f98 4495 {
84fb1139
KH
4496 if (saved_mouse_button_msg.msg.hwnd)
4497 {
4498 post_msg (&saved_mouse_button_msg);
4499 saved_mouse_button_msg.msg.hwnd = 0;
4500 }
4501 KillTimer (hwnd, mouse_button_timer);
4502 mouse_button_timer = 0;
4503 }
4504 else if (wParam == mouse_move_timer)
4505 {
4506 if (saved_mouse_move_msg.msg.hwnd)
4507 {
4508 post_msg (&saved_mouse_move_msg);
4509 saved_mouse_move_msg.msg.hwnd = 0;
4510 }
4511 KillTimer (hwnd, mouse_move_timer);
4512 mouse_move_timer = 0;
5ac45f98 4513 }
5ac45f98 4514 return 0;
84fb1139
KH
4515
4516 case WM_NCACTIVATE:
4517 /* Windows doesn't send us focus messages when putting up and
e9e23e23 4518 taking down a system popup dialog as for Ctrl-Alt-Del on Windows 95.
84fb1139
KH
4519 The only indication we get that something happened is receiving
4520 this message afterwards. So this is a good time to reset our
4521 keyboard modifiers' state. */
4522 reset_modifiers ();
4523 goto dflt;
da36a4d6 4524
1edf84e7 4525 case WM_INITMENU:
487163ac
AI
4526 button_state = 0;
4527 ReleaseCapture ();
1edf84e7
GV
4528 /* We must ensure menu bar is fully constructed and up to date
4529 before allowing user interaction with it. To achieve this
4530 we send this message to the lisp thread and wait for a
4531 reply (whose value is not actually needed) to indicate that
4532 the menu bar is now ready for use, so we can now return.
4533
4534 To remain responsive in the meantime, we enter a nested message
4535 loop that can process all other messages.
4536
4537 However, we skip all this if the message results from calling
4538 TrackPopupMenu - in fact, we must NOT attempt to send the lisp
4539 thread a message because it is blocked on us at this point. We
4540 set menubar_active before calling TrackPopupMenu to indicate
4541 this (there is no possibility of confusion with real menubar
4542 being active). */
4543
4544 f = x_window_to_frame (dpyinfo, hwnd);
4545 if (f
4546 && (f->output_data.w32->menubar_active
4547 /* We can receive this message even in the absence of a
4548 menubar (ie. when the system menu is activated) - in this
4549 case we do NOT want to forward the message, otherwise it
4550 will cause the menubar to suddenly appear when the user
4551 had requested it to be turned off! */
4552 || f->output_data.w32->menubar_widget == NULL))
4553 return 0;
4554
4555 {
4556 deferred_msg msg_buf;
4557
4558 /* Detect if message has already been deferred; in this case
4559 we cannot return any sensible value to ignore this. */
4560 if (find_deferred_msg (hwnd, msg) != NULL)
4561 abort ();
4562
4563 return send_deferred_msg (&msg_buf, hwnd, msg, wParam, lParam);
4564 }
4565
4566 case WM_EXITMENULOOP:
4567 f = x_window_to_frame (dpyinfo, hwnd);
4568
4569 /* Indicate that menubar can be modified again. */
4570 if (f)
4571 f->output_data.w32->menubar_active = 0;
4572 goto dflt;
4573
126f2e35
JR
4574 case WM_MENUSELECT:
4575 wmsg.dwModifiers = w32_get_modifiers ();
4576 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4577 return 0;
4578
87996783
GV
4579 case WM_MEASUREITEM:
4580 f = x_window_to_frame (dpyinfo, hwnd);
4581 if (f)
4582 {
4583 MEASUREITEMSTRUCT * pMis = (MEASUREITEMSTRUCT *) lParam;
4584
4585 if (pMis->CtlType == ODT_MENU)
4586 {
4587 /* Work out dimensions for popup menu titles. */
4588 char * title = (char *) pMis->itemData;
4589 HDC hdc = GetDC (hwnd);
4590 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
4591 LOGFONT menu_logfont;
4592 HFONT old_font;
4593 SIZE size;
4594
4595 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
4596 menu_logfont.lfWeight = FW_BOLD;
4597 menu_font = CreateFontIndirect (&menu_logfont);
4598 old_font = SelectObject (hdc, menu_font);
4599
dfff8a69
JR
4600 pMis->itemHeight = GetSystemMetrics (SM_CYMENUSIZE);
4601 if (title)
4602 {
4603 GetTextExtentPoint32 (hdc, title, strlen (title), &size);
4604 pMis->itemWidth = size.cx;
4605 if (pMis->itemHeight < size.cy)
4606 pMis->itemHeight = size.cy;
4607 }
4608 else
4609 pMis->itemWidth = 0;
87996783
GV
4610
4611 SelectObject (hdc, old_font);
4612 DeleteObject (menu_font);
4613 ReleaseDC (hwnd, hdc);
4614 return TRUE;
4615 }
4616 }
4617 return 0;
4618
4619 case WM_DRAWITEM:
4620 f = x_window_to_frame (dpyinfo, hwnd);
4621 if (f)
4622 {
4623 DRAWITEMSTRUCT * pDis = (DRAWITEMSTRUCT *) lParam;
4624
4625 if (pDis->CtlType == ODT_MENU)
4626 {
4627 /* Draw popup menu title. */
4628 char * title = (char *) pDis->itemData;
212da13b
JR
4629 if (title)
4630 {
4631 HDC hdc = pDis->hDC;
4632 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
4633 LOGFONT menu_logfont;
4634 HFONT old_font;
4635
4636 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
4637 menu_logfont.lfWeight = FW_BOLD;
4638 menu_font = CreateFontIndirect (&menu_logfont);
4639 old_font = SelectObject (hdc, menu_font);
4640
4641 /* Always draw title as if not selected. */
4642 ExtTextOut (hdc,
4643 pDis->rcItem.left
4644 + GetSystemMetrics (SM_CXMENUCHECK),
4645 pDis->rcItem.top,
4646 ETO_OPAQUE, &pDis->rcItem,
4647 title, strlen (title), NULL);
4648
4649 SelectObject (hdc, old_font);
4650 DeleteObject (menu_font);
4651 }
87996783
GV
4652 return TRUE;
4653 }
4654 }
4655 return 0;
4656
1edf84e7
GV
4657#if 0
4658 /* Still not right - can't distinguish between clicks in the
4659 client area of the frame from clicks forwarded from the scroll
4660 bars - may have to hook WM_NCHITTEST to remember the mouse
4661 position and then check if it is in the client area ourselves. */
4662 case WM_MOUSEACTIVATE:
4663 /* Discard the mouse click that activates a frame, allowing the
4664 user to click anywhere without changing point (or worse!).
4665 Don't eat mouse clicks on scrollbars though!! */
4666 if (LOWORD (lParam) == HTCLIENT )
4667 return MA_ACTIVATEANDEAT;
4668 goto dflt;
4669#endif
4670
1edf84e7 4671 case WM_ACTIVATEAPP:
ccc2d29c 4672 case WM_ACTIVATE:
1edf84e7
GV
4673 case WM_WINDOWPOSCHANGED:
4674 case WM_SHOWWINDOW:
4675 /* Inform lisp thread that a frame might have just been obscured
4676 or exposed, so should recheck visibility of all frames. */
4677 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4678 goto dflt;
4679
da36a4d6 4680 case WM_SETFOCUS:
adcc3809
GV
4681 dpyinfo->faked_key = 0;
4682 reset_modifiers ();
ccc2d29c
GV
4683 register_hot_keys (hwnd);
4684 goto command;
8681157a 4685 case WM_KILLFOCUS:
ccc2d29c 4686 unregister_hot_keys (hwnd);
487163ac
AI
4687 button_state = 0;
4688 ReleaseCapture ();
ee78dc32
GV
4689 case WM_MOVE:
4690 case WM_SIZE:
ee78dc32 4691 case WM_COMMAND:
ccc2d29c 4692 command:
fbd6baed 4693 wmsg.dwModifiers = w32_get_modifiers ();
ee78dc32
GV
4694 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4695 goto dflt;
8847d890
RS
4696
4697 case WM_CLOSE:
fbd6baed 4698 wmsg.dwModifiers = w32_get_modifiers ();
8847d890
RS
4699 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4700 return 0;
4701
ee78dc32
GV
4702 case WM_WINDOWPOSCHANGING:
4703 {
4704 WINDOWPLACEMENT wp;
4705 LPWINDOWPOS lppos = (WINDOWPOS *) lParam;
1edf84e7
GV
4706
4707 wp.length = sizeof (WINDOWPLACEMENT);
ee78dc32
GV
4708 GetWindowPlacement (hwnd, &wp);
4709
1edf84e7 4710 if (wp.showCmd != SW_SHOWMINIMIZED && (lppos->flags & SWP_NOSIZE) == 0)
ee78dc32
GV
4711 {
4712 RECT rect;
4713 int wdiff;
4714 int hdiff;
1edf84e7
GV
4715 DWORD font_width;
4716 DWORD line_height;
4717 DWORD internal_border;
4718 DWORD scrollbar_extra;
ee78dc32
GV
4719 RECT wr;
4720
5ac45f98 4721 wp.length = sizeof(wp);
ee78dc32
GV
4722 GetWindowRect (hwnd, &wr);
4723
3c190163 4724 enter_crit ();
ee78dc32 4725
1edf84e7
GV
4726 font_width = GetWindowLong (hwnd, WND_FONTWIDTH_INDEX);
4727 line_height = GetWindowLong (hwnd, WND_LINEHEIGHT_INDEX);
4728 internal_border = GetWindowLong (hwnd, WND_BORDER_INDEX);
4729 scrollbar_extra = GetWindowLong (hwnd, WND_SCROLLBAR_INDEX);
ee78dc32 4730
3c190163 4731 leave_crit ();
ee78dc32
GV
4732
4733 memset (&rect, 0, sizeof (rect));
4734 AdjustWindowRect (&rect, GetWindowLong (hwnd, GWL_STYLE),
4735 GetMenu (hwnd) != NULL);
4736
1edf84e7
GV
4737 /* Force width and height of client area to be exact
4738 multiples of the character cell dimensions. */
4739 wdiff = (lppos->cx - (rect.right - rect.left)
4740 - 2 * internal_border - scrollbar_extra)
4741 % font_width;
4742 hdiff = (lppos->cy - (rect.bottom - rect.top)
4743 - 2 * internal_border)
4744 % line_height;
ee78dc32
GV
4745
4746 if (wdiff || hdiff)
4747 {
4748 /* For right/bottom sizing we can just fix the sizes.
4749 However for top/left sizing we will need to fix the X
4750 and Y positions as well. */
4751
4752 lppos->cx -= wdiff;
4753 lppos->cy -= hdiff;
4754
4755 if (wp.showCmd != SW_SHOWMAXIMIZED
1edf84e7 4756 && (lppos->flags & SWP_NOMOVE) == 0)
ee78dc32
GV
4757 {
4758 if (lppos->x != wr.left || lppos->y != wr.top)
4759 {
4760 lppos->x += wdiff;
4761 lppos->y += hdiff;
4762 }
4763 else
4764 {
4765 lppos->flags |= SWP_NOMOVE;
4766 }
4767 }
4768
1edf84e7 4769 return 0;
ee78dc32
GV
4770 }
4771 }
4772 }
ee78dc32
GV
4773
4774 goto dflt;
1edf84e7 4775
b1f918f8
GV
4776 case WM_GETMINMAXINFO:
4777 /* Hack to correct bug that allows Emacs frames to be resized
4778 below the Minimum Tracking Size. */
4779 ((LPMINMAXINFO) lParam)->ptMinTrackSize.y++;
4780 return 0;
4781
1edf84e7
GV
4782 case WM_EMACS_CREATESCROLLBAR:
4783 return (LRESULT) w32_createscrollbar ((struct frame *) wParam,
4784 (struct scroll_bar *) lParam);
4785
5ac45f98 4786 case WM_EMACS_SHOWWINDOW:
1edf84e7
GV
4787 return ShowWindow ((HWND) wParam, (WPARAM) lParam);
4788
dfdb4047 4789 case WM_EMACS_SETFOREGROUND:
ce6059da
AI
4790 {
4791 HWND foreground_window;
4792 DWORD foreground_thread, retval;
4793
4794 /* On NT 5.0, and apparently Windows 98, it is necessary to
4795 attach to the thread that currently has focus in order to
4796 pull the focus away from it. */
4797 foreground_window = GetForegroundWindow ();
4798 foreground_thread = GetWindowThreadProcessId (foreground_window, NULL);
4799 if (!foreground_window
4800 || foreground_thread == GetCurrentThreadId ()
4801 || !AttachThreadInput (GetCurrentThreadId (),
4802 foreground_thread, TRUE))
4803 foreground_thread = 0;
4804
4805 retval = SetForegroundWindow ((HWND) wParam);
4806
4807 /* Detach from the previous foreground thread. */
4808 if (foreground_thread)
4809 AttachThreadInput (GetCurrentThreadId (),
4810 foreground_thread, FALSE);
4811
4812 return retval;
4813 }
dfdb4047 4814
5ac45f98
GV
4815 case WM_EMACS_SETWINDOWPOS:
4816 {
1edf84e7
GV
4817 WINDOWPOS * pos = (WINDOWPOS *) wParam;
4818 return SetWindowPos (hwnd, pos->hwndInsertAfter,
5ac45f98
GV
4819 pos->x, pos->y, pos->cx, pos->cy, pos->flags);
4820 }
1edf84e7 4821
ee78dc32 4822 case WM_EMACS_DESTROYWINDOW:
cb9e33d4 4823 DragAcceptFiles ((HWND) wParam, FALSE);
1edf84e7
GV
4824 return DestroyWindow ((HWND) wParam);
4825
4826 case WM_EMACS_TRACKPOPUPMENU:
4827 {
4828 UINT flags;
4829 POINT *pos;
4830 int retval;
4831 pos = (POINT *)lParam;
4832 flags = TPM_CENTERALIGN;
4833 if (button_state & LMOUSE)
4834 flags |= TPM_LEFTBUTTON;
4835 else if (button_state & RMOUSE)
4836 flags |= TPM_RIGHTBUTTON;
4837
87996783
GV
4838 /* Remember we did a SetCapture on the initial mouse down event,
4839 so for safety, we make sure the capture is cancelled now. */
4840 ReleaseCapture ();
490822ff 4841 button_state = 0;
87996783 4842
1edf84e7
GV
4843 /* Use menubar_active to indicate that WM_INITMENU is from
4844 TrackPopupMenu below, and should be ignored. */
4845 f = x_window_to_frame (dpyinfo, hwnd);
4846 if (f)
4847 f->output_data.w32->menubar_active = 1;
4848
4849 if (TrackPopupMenu ((HMENU)wParam, flags, pos->x, pos->y,
4850 0, hwnd, NULL))
4851 {
4852 MSG amsg;
4853 /* Eat any mouse messages during popupmenu */
4854 while (PeekMessage (&amsg, hwnd, WM_MOUSEFIRST, WM_MOUSELAST,
4855 PM_REMOVE));
4856 /* Get the menu selection, if any */
4857 if (PeekMessage (&amsg, hwnd, WM_COMMAND, WM_COMMAND, PM_REMOVE))
4858 {
4859 retval = LOWORD (amsg.wParam);
4860 }
4861 else
4862 {
4863 retval = 0;
4864 }
1edf84e7
GV
4865 }
4866 else
4867 {
4868 retval = -1;
4869 }
4870
4871 return retval;
4872 }
4873
ee78dc32 4874 default:
93fbe8b7
GV
4875 /* Check for messages registered at runtime. */
4876 if (msg == msh_mousewheel)
4877 {
4878 wmsg.dwModifiers = w32_get_modifiers ();
4879 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4880 return 0;
4881 }
4882
ee78dc32
GV
4883 dflt:
4884 return DefWindowProc (hwnd, msg, wParam, lParam);
4885 }
4886
1edf84e7
GV
4887
4888 /* The most common default return code for handled messages is 0. */
4889 return 0;
ee78dc32
GV
4890}
4891
4892void
4893my_create_window (f)
4894 struct frame * f;
4895{
4896 MSG msg;
4897
1edf84e7
GV
4898 if (!PostThreadMessage (dwWindowsThreadId, WM_EMACS_CREATEWINDOW, (WPARAM)f, 0))
4899 abort ();
ee78dc32
GV
4900 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
4901}
4902
fbd6baed 4903/* Create and set up the w32 window for frame F. */
ee78dc32
GV
4904
4905static void
fbd6baed 4906w32_window (f, window_prompting, minibuffer_only)
ee78dc32
GV
4907 struct frame *f;
4908 long window_prompting;
4909 int minibuffer_only;
4910{
4911 BLOCK_INPUT;
4912
4913 /* Use the resource name as the top-level window name
4914 for looking up resources. Make a non-Lisp copy
4915 for the window manager, so GC relocation won't bother it.
4916
4917 Elsewhere we specify the window name for the window manager. */
4918
4919 {
4920 char *str = (char *) XSTRING (Vx_resource_name)->data;
4921 f->namebuf = (char *) xmalloc (strlen (str) + 1);
4922 strcpy (f->namebuf, str);
4923 }
4924
4925 my_create_window (f);
4926
4927 validate_x_resource_name ();
4928
4929 /* x_set_name normally ignores requests to set the name if the
4930 requested name is the same as the current name. This is the one
4931 place where that assumption isn't correct; f->name is set, but
4932 the server hasn't been told. */
4933 {
4934 Lisp_Object name;
4935 int explicit = f->explicit_name;
4936
4937 f->explicit_name = 0;
4938 name = f->name;
4939 f->name = Qnil;
4940 x_set_name (f, name, explicit);
4941 }
4942
4943 UNBLOCK_INPUT;
4944
4945 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
4946 initialize_frame_menubar (f);
4947
fbd6baed 4948 if (FRAME_W32_WINDOW (f) == 0)
ee78dc32
GV
4949 error ("Unable to create window");
4950}
4951
4952/* Handle the icon stuff for this window. Perhaps later we might
4953 want an x_set_icon_position which can be called interactively as
4954 well. */
4955
4956static void
4957x_icon (f, parms)
4958 struct frame *f;
4959 Lisp_Object parms;
4960{
4961 Lisp_Object icon_x, icon_y;
4962
e9e23e23 4963 /* Set the position of the icon. Note that Windows 95 groups all
ee78dc32 4964 icons in the tray. */
6fc2811b
JR
4965 icon_x = w32_get_arg (parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
4966 icon_y = w32_get_arg (parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
ee78dc32
GV
4967 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
4968 {
4969 CHECK_NUMBER (icon_x, 0);
4970 CHECK_NUMBER (icon_y, 0);
4971 }
4972 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
4973 error ("Both left and top icon corners of icon must be specified");
4974
4975 BLOCK_INPUT;
4976
4977 if (! EQ (icon_x, Qunbound))
4978 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
4979
1edf84e7
GV
4980#if 0 /* TODO */
4981 /* Start up iconic or window? */
4982 x_wm_set_window_state
6fc2811b 4983 (f, (EQ (w32_get_arg (parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL), Qicon)
1edf84e7
GV
4984 ? IconicState
4985 : NormalState));
4986
4987 x_text_icon (f, (char *) XSTRING ((!NILP (f->icon_name)
4988 ? f->icon_name
4989 : f->name))->data);
4990#endif
4991
ee78dc32
GV
4992 UNBLOCK_INPUT;
4993}
4994
6fc2811b
JR
4995
4996static void
4997x_make_gc (f)
4998 struct frame *f;
4999{
5000 XGCValues gc_values;
5001
5002 BLOCK_INPUT;
5003
5004 /* Create the GC's of this frame.
5005 Note that many default values are used. */
5006
5007 /* Normal video */
5008 gc_values.font = f->output_data.w32->font;
5009
5010 /* Cursor has cursor-color background, background-color foreground. */
5011 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
5012 gc_values.background = f->output_data.w32->cursor_pixel;
5013 f->output_data.w32->cursor_gc
5014 = XCreateGC (NULL, FRAME_W32_WINDOW (f),
5015 (GCFont | GCForeground | GCBackground),
5016 &gc_values);
5017
5018 /* Reliefs. */
5019 f->output_data.w32->white_relief.gc = 0;
5020 f->output_data.w32->black_relief.gc = 0;
5021
5022 UNBLOCK_INPUT;
5023}
5024
5025
ee78dc32
GV
5026DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
5027 1, 1, 0,
5028 "Make a new window, which is called a \"frame\" in Emacs terms.\n\
5029Returns an Emacs frame object.\n\
5030ALIST is an alist of frame parameters.\n\
5031If the parameters specify that the frame should not have a minibuffer,\n\
5032and do not specify a specific minibuffer window to use,\n\
5033then `default-minibuffer-frame' must be a frame whose minibuffer can\n\
5034be shared by the new frame.\n\
5035\n\
5036This function is an internal primitive--use `make-frame' instead.")
5037 (parms)
5038 Lisp_Object parms;
5039{
5040 struct frame *f;
5041 Lisp_Object frame, tem;
5042 Lisp_Object name;
5043 int minibuffer_only = 0;
5044 long window_prompting = 0;
5045 int width, height;
5046 int count = specpdl_ptr - specpdl;
1edf84e7 5047 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
ee78dc32 5048 Lisp_Object display;
6fc2811b 5049 struct w32_display_info *dpyinfo = NULL;
ee78dc32
GV
5050 Lisp_Object parent;
5051 struct kboard *kb;
5052
4587b026
GV
5053 check_w32 ();
5054
ee78dc32
GV
5055 /* Use this general default value to start with
5056 until we know if this frame has a specified name. */
5057 Vx_resource_name = Vinvocation_name;
5058
6fc2811b 5059 display = w32_get_arg (parms, Qdisplay, 0, 0, RES_TYPE_STRING);
ee78dc32
GV
5060 if (EQ (display, Qunbound))
5061 display = Qnil;
5062 dpyinfo = check_x_display_info (display);
5063#ifdef MULTI_KBOARD
5064 kb = dpyinfo->kboard;
5065#else
5066 kb = &the_only_kboard;
5067#endif
5068
6fc2811b 5069 name = w32_get_arg (parms, Qname, "name", "Name", RES_TYPE_STRING);
ee78dc32
GV
5070 if (!STRINGP (name)
5071 && ! EQ (name, Qunbound)
5072 && ! NILP (name))
5073 error ("Invalid frame name--not a string or nil");
5074
5075 if (STRINGP (name))
5076 Vx_resource_name = name;
5077
5078 /* See if parent window is specified. */
6fc2811b 5079 parent = w32_get_arg (parms, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
ee78dc32
GV
5080 if (EQ (parent, Qunbound))
5081 parent = Qnil;
5082 if (! NILP (parent))
5083 CHECK_NUMBER (parent, 0);
5084
1edf84e7
GV
5085 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
5086 /* No need to protect DISPLAY because that's not used after passing
5087 it to make_frame_without_minibuffer. */
5088 frame = Qnil;
5089 GCPRO4 (parms, parent, name, frame);
1660f34a
JR
5090 tem = w32_get_arg (parms, Qminibuffer, "minibuffer", "Minibuffer",
5091 RES_TYPE_SYMBOL);
ee78dc32
GV
5092 if (EQ (tem, Qnone) || NILP (tem))
5093 f = make_frame_without_minibuffer (Qnil, kb, display);
5094 else if (EQ (tem, Qonly))
5095 {
5096 f = make_minibuffer_frame ();
5097 minibuffer_only = 1;
5098 }
5099 else if (WINDOWP (tem))
5100 f = make_frame_without_minibuffer (tem, kb, display);
5101 else
5102 f = make_frame (1);
5103
1edf84e7
GV
5104 XSETFRAME (frame, f);
5105
ee78dc32
GV
5106 /* Note that Windows does support scroll bars. */
5107 FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;
5ac45f98
GV
5108 /* By default, make scrollbars the system standard width. */
5109 f->scroll_bar_pixel_width = GetSystemMetrics (SM_CXVSCROLL);
ee78dc32 5110
fbd6baed 5111 f->output_method = output_w32;
6fc2811b
JR
5112 f->output_data.w32 =
5113 (struct w32_output *) xmalloc (sizeof (struct w32_output));
fbd6baed 5114 bzero (f->output_data.w32, sizeof (struct w32_output));
ee78dc32 5115
4587b026
GV
5116 FRAME_FONTSET (f) = -1;
5117
1edf84e7 5118 f->icon_name
6fc2811b 5119 = w32_get_arg (parms, Qicon_name, "iconName", "Title", RES_TYPE_STRING);
1edf84e7
GV
5120 if (! STRINGP (f->icon_name))
5121 f->icon_name = Qnil;
5122
fbd6baed 5123/* FRAME_W32_DISPLAY_INFO (f) = dpyinfo; */
ee78dc32
GV
5124#ifdef MULTI_KBOARD
5125 FRAME_KBOARD (f) = kb;
5126#endif
5127
5128 /* Specify the parent under which to make this window. */
5129
5130 if (!NILP (parent))
5131 {
1660f34a 5132 f->output_data.w32->parent_desc = (Window) XFASTINT (parent);
fbd6baed 5133 f->output_data.w32->explicit_parent = 1;
ee78dc32
GV
5134 }
5135 else
5136 {
fbd6baed
GV
5137 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
5138 f->output_data.w32->explicit_parent = 0;
ee78dc32
GV
5139 }
5140
ee78dc32
GV
5141 /* Set the name; the functions to which we pass f expect the name to
5142 be set. */
5143 if (EQ (name, Qunbound) || NILP (name))
5144 {
fbd6baed 5145 f->name = build_string (dpyinfo->w32_id_name);
ee78dc32
GV
5146 f->explicit_name = 0;
5147 }
5148 else
5149 {
5150 f->name = name;
5151 f->explicit_name = 1;
5152 /* use the frame's title when getting resources for this frame. */
5153 specbind (Qx_resource_name, name);
5154 }
5155
5156 /* Extract the window parameters from the supplied values
5157 that are needed to determine window geometry. */
5158 {
5159 Lisp_Object font;
5160
6fc2811b
JR
5161 font = w32_get_arg (parms, Qfont, "font", "Font", RES_TYPE_STRING);
5162
ee78dc32
GV
5163 BLOCK_INPUT;
5164 /* First, try whatever font the caller has specified. */
5165 if (STRINGP (font))
4587b026
GV
5166 {
5167 tem = Fquery_fontset (font, Qnil);
5168 if (STRINGP (tem))
5169 font = x_new_fontset (f, XSTRING (tem)->data);
5170 else
1075afa9 5171 font = x_new_font (f, XSTRING (font)->data);
4587b026 5172 }
ee78dc32
GV
5173 /* Try out a font which we hope has bold and italic variations. */
5174 if (!STRINGP (font))
4587b026 5175 font = x_new_font (f, "-*-Courier New-normal-r-*-*-13-*-*-*-c-*-iso8859-1");
ee78dc32 5176 if (! STRINGP (font))
6fc2811b 5177 font = x_new_font (f, "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1");
ee78dc32
GV
5178 /* If those didn't work, look for something which will at least work. */
5179 if (! STRINGP (font))
6fc2811b 5180 font = x_new_font (f, "-*-Fixedsys-normal-r-*-*-12-*-*-*-c-*-iso8859-1");
ee78dc32
GV
5181 UNBLOCK_INPUT;
5182 if (! STRINGP (font))
1edf84e7 5183 font = build_string ("Fixedsys");
ee78dc32
GV
5184
5185 x_default_parameter (f, parms, Qfont, font,
6fc2811b 5186 "font", "Font", RES_TYPE_STRING);
ee78dc32
GV
5187 }
5188
5189 x_default_parameter (f, parms, Qborder_width, make_number (2),
1660f34a 5190 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
ee78dc32
GV
5191 /* This defaults to 2 in order to match xterm. We recognize either
5192 internalBorderWidth or internalBorder (which is what xterm calls
5193 it). */
5194 if (NILP (Fassq (Qinternal_border_width, parms)))
5195 {
5196 Lisp_Object value;
5197
6fc2811b 5198 value = w32_get_arg (parms, Qinternal_border_width,
1660f34a 5199 "internalBorder", "InternalBorder", RES_TYPE_NUMBER);
ee78dc32
GV
5200 if (! EQ (value, Qunbound))
5201 parms = Fcons (Fcons (Qinternal_border_width, value),
5202 parms);
5203 }
1edf84e7 5204 /* Default internalBorderWidth to 0 on Windows to match other programs. */
ee78dc32 5205 x_default_parameter (f, parms, Qinternal_border_width, make_number (0),
1660f34a
JR
5206 "internalBorderWidth", "InternalBorder", RES_TYPE_NUMBER);
5207 x_default_parameter (f, parms, Qvertical_scroll_bars, Qright,
5208 "verticalScrollBars", "ScrollBars", RES_TYPE_SYMBOL);
ee78dc32
GV
5209
5210 /* Also do the stuff which must be set before the window exists. */
5211 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
6fc2811b 5212 "foreground", "Foreground", RES_TYPE_STRING);
ee78dc32 5213 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
6fc2811b 5214 "background", "Background", RES_TYPE_STRING);
ee78dc32 5215 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
6fc2811b 5216 "pointerColor", "Foreground", RES_TYPE_STRING);
ee78dc32 5217 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
6fc2811b 5218 "cursorColor", "Foreground", RES_TYPE_STRING);
ee78dc32 5219 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
6fc2811b
JR
5220 "borderColor", "BorderColor", RES_TYPE_STRING);
5221 x_default_parameter (f, parms, Qscreen_gamma, Qnil,
5222 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
dfff8a69
JR
5223 x_default_parameter (f, parms, Qline_spacing, Qnil,
5224 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
6fc2811b 5225
ee78dc32 5226
6fc2811b
JR
5227 /* Init faces before x_default_parameter is called for scroll-bar
5228 parameters because that function calls x_set_scroll_bar_width,
5229 which calls change_frame_size, which calls Fset_window_buffer,
5230 which runs hooks, which call Fvertical_motion. At the end, we
5231 end up in init_iterator with a null face cache, which should not
5232 happen. */
5233 init_frame_faces (f);
5234
ee78dc32 5235 x_default_parameter (f, parms, Qmenu_bar_lines, make_number (1),
6fc2811b
JR
5236 "menuBar", "MenuBar", RES_TYPE_NUMBER);
5237 x_default_parameter (f, parms, Qtool_bar_lines, make_number (0),
5238 "toolBar", "ToolBar", RES_TYPE_NUMBER);
1edf84e7 5239 x_default_parameter (f, parms, Qbuffer_predicate, Qnil,
6fc2811b 5240 "bufferPredicate", "BufferPredicate", RES_TYPE_SYMBOL);
1edf84e7 5241 x_default_parameter (f, parms, Qtitle, Qnil,
6fc2811b 5242 "title", "Title", RES_TYPE_STRING);
ee78dc32 5243
fbd6baed
GV
5244 f->output_data.w32->dwStyle = WS_OVERLAPPEDWINDOW;
5245 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
ee78dc32
GV
5246 window_prompting = x_figure_window_size (f, parms);
5247
5248 if (window_prompting & XNegative)
5249 {
5250 if (window_prompting & YNegative)
fbd6baed 5251 f->output_data.w32->win_gravity = SouthEastGravity;
ee78dc32 5252 else
fbd6baed 5253 f->output_data.w32->win_gravity = NorthEastGravity;
ee78dc32
GV
5254 }
5255 else
5256 {
5257 if (window_prompting & YNegative)
fbd6baed 5258 f->output_data.w32->win_gravity = SouthWestGravity;
ee78dc32 5259 else
fbd6baed 5260 f->output_data.w32->win_gravity = NorthWestGravity;
ee78dc32
GV
5261 }
5262
fbd6baed 5263 f->output_data.w32->size_hint_flags = window_prompting;
ee78dc32 5264
6fc2811b
JR
5265 tem = w32_get_arg (parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
5266 f->no_split = minibuffer_only || EQ (tem, Qt);
5267
5268 /* Create the window. Add the tool-bar height to the initial frame
5269 height so that the user gets a text display area of the size he
5270 specified with -g or via the registry. Later changes of the
5271 tool-bar height don't change the frame size. This is done so that
5272 users can create tall Emacs frames without having to guess how
5273 tall the tool-bar will get. */
5274 f->height += FRAME_TOOL_BAR_LINES (f);
fbd6baed 5275 w32_window (f, window_prompting, minibuffer_only);
ee78dc32 5276 x_icon (f, parms);
6fc2811b
JR
5277
5278 x_make_gc (f);
5279
5280 /* Now consider the frame official. */
5281 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
5282 Vframe_list = Fcons (frame, Vframe_list);
ee78dc32
GV
5283
5284 /* We need to do this after creating the window, so that the
5285 icon-creation functions can say whose icon they're describing. */
5286 x_default_parameter (f, parms, Qicon_type, Qnil,
6fc2811b 5287 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
ee78dc32
GV
5288
5289 x_default_parameter (f, parms, Qauto_raise, Qnil,
6fc2811b 5290 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
ee78dc32 5291 x_default_parameter (f, parms, Qauto_lower, Qnil,
6fc2811b 5292 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
ee78dc32 5293 x_default_parameter (f, parms, Qcursor_type, Qbox,
6fc2811b
JR
5294 "cursorType", "CursorType", RES_TYPE_SYMBOL);
5295 x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
5296 "scrollBarWidth", "ScrollBarWidth", RES_TYPE_NUMBER);
ee78dc32
GV
5297
5298 /* Dimensions, especially f->height, must be done via change_frame_size.
5299 Change will not be effected unless different from the current
5300 f->height. */
5301 width = f->width;
5302 height = f->height;
1026b400
RS
5303 f->height = 0;
5304 SET_FRAME_WIDTH (f, 0);
6fc2811b
JR
5305 change_frame_size (f, height, width, 1, 0, 0);
5306
6fc2811b
JR
5307 /* Tell the server what size and position, etc, we want, and how
5308 badly we want them. This should be done after we have the menu
5309 bar so that its size can be taken into account. */
ee78dc32
GV
5310 BLOCK_INPUT;
5311 x_wm_set_size_hint (f, window_prompting, 0);
5312 UNBLOCK_INPUT;
5313
6fc2811b
JR
5314 /* Make the window appear on the frame and enable display, unless
5315 the caller says not to. However, with explicit parent, Emacs
5316 cannot control visibility, so don't try. */
fbd6baed 5317 if (! f->output_data.w32->explicit_parent)
ee78dc32
GV
5318 {
5319 Lisp_Object visibility;
5320
6fc2811b 5321 visibility = w32_get_arg (parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL);
ee78dc32
GV
5322 if (EQ (visibility, Qunbound))
5323 visibility = Qt;
5324
5325 if (EQ (visibility, Qicon))
5326 x_iconify_frame (f);
5327 else if (! NILP (visibility))
5328 x_make_frame_visible (f);
5329 else
5330 /* Must have been Qnil. */
5331 ;
5332 }
6fc2811b 5333 UNGCPRO;
ee78dc32
GV
5334 return unbind_to (count, frame);
5335}
5336
5337/* FRAME is used only to get a handle on the X display. We don't pass the
5338 display info directly because we're called from frame.c, which doesn't
5339 know about that structure. */
5340Lisp_Object
5341x_get_focus_frame (frame)
5342 struct frame *frame;
5343{
fbd6baed 5344 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (frame);
ee78dc32 5345 Lisp_Object xfocus;
fbd6baed 5346 if (! dpyinfo->w32_focus_frame)
ee78dc32
GV
5347 return Qnil;
5348
fbd6baed 5349 XSETFRAME (xfocus, dpyinfo->w32_focus_frame);
ee78dc32
GV
5350 return xfocus;
5351}
1edf84e7
GV
5352
5353DEFUN ("w32-focus-frame", Fw32_focus_frame, Sw32_focus_frame, 1, 1, 0,
5354 "Give FRAME input focus, raising to foreground if necessary.")
5355 (frame)
5356 Lisp_Object frame;
5357{
5358 x_focus_on_frame (check_x_frame (frame));
5359 return Qnil;
5360}
5361
ee78dc32 5362\f
33d52f9c
GV
5363struct font_info *w32_load_bdf_font (struct frame *f, char *fontname,
5364 int size, char* filename);
5365
4587b026 5366struct font_info *
33d52f9c 5367w32_load_system_font (f,fontname,size)
55dcfc15
AI
5368 struct frame *f;
5369 char * fontname;
5370 int size;
ee78dc32 5371{
4587b026
GV
5372 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
5373 Lisp_Object font_names;
5374
4587b026
GV
5375 /* Get a list of all the fonts that match this name. Once we
5376 have a list of matching fonts, we compare them against the fonts
5377 we already have loaded by comparing names. */
5378 font_names = w32_list_fonts (f, build_string (fontname), size, 100);
5379
5380 if (!NILP (font_names))
3c190163 5381 {
4587b026
GV
5382 Lisp_Object tail;
5383 int i;
4587b026
GV
5384
5385 /* First check if any are already loaded, as that is cheaper
5386 than loading another one. */
5387 for (i = 0; i < dpyinfo->n_fonts; i++)
8e713be6 5388 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
6fc2811b
JR
5389 if (dpyinfo->font_table[i].name
5390 && (!strcmp (dpyinfo->font_table[i].name,
5391 XSTRING (XCAR (tail))->data)
5392 || !strcmp (dpyinfo->font_table[i].full_name,
5393 XSTRING (XCAR (tail))->data)))
4587b026 5394 return (dpyinfo->font_table + i);
6fc2811b 5395
8e713be6 5396 fontname = (char *) XSTRING (XCAR (font_names))->data;
4587b026 5397 }
1075afa9 5398 else if (w32_strict_fontnames)
5ca0cd71
GV
5399 {
5400 /* If EnumFontFamiliesEx was available, we got a full list of
5401 fonts back so stop now to avoid the possibility of loading a
5402 random font. If we had to fall back to EnumFontFamilies, the
5403 list is incomplete, so continue whether the font we want was
5404 listed or not. */
5405 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
5406 FARPROC enum_font_families_ex
1075afa9 5407 = GetProcAddress (gdi32, "EnumFontFamiliesExA");
5ca0cd71
GV
5408 if (enum_font_families_ex)
5409 return NULL;
5410 }
4587b026
GV
5411
5412 /* Load the font and add it to the table. */
5413 {
33d52f9c 5414 char *full_name, *encoding;
4587b026
GV
5415 XFontStruct *font;
5416 struct font_info *fontp;
3c190163 5417 LOGFONT lf;
4587b026 5418 BOOL ok;
6fc2811b 5419 int i;
5ac45f98 5420
4587b026 5421 if (!fontname || !x_to_w32_font (fontname, &lf))
3c190163 5422 return (NULL);
5ac45f98 5423
4587b026
GV
5424 if (!*lf.lfFaceName)
5425 /* If no name was specified for the font, we get a random font
5426 from CreateFontIndirect - this is not particularly
5427 desirable, especially since CreateFontIndirect does not
5428 fill out the missing name in lf, so we never know what we
5429 ended up with. */
5430 return NULL;
5431
3c190163 5432 font = (XFontStruct *) xmalloc (sizeof (XFontStruct));
5ac45f98 5433
33d52f9c
GV
5434 /* Set bdf to NULL to indicate that this is a Windows font. */
5435 font->bdf = NULL;
5ac45f98 5436
3c190163 5437 BLOCK_INPUT;
5ac45f98
GV
5438
5439 font->hfont = CreateFontIndirect (&lf);
ee78dc32 5440
1a292d24
AI
5441 if (font->hfont == NULL)
5442 {
5443 ok = FALSE;
5444 }
5445 else
5446 {
5447 HDC hdc;
5448 HANDLE oldobj;
5449
5450 hdc = GetDC (dpyinfo->root_window);
5451 oldobj = SelectObject (hdc, font->hfont);
5452 ok = GetTextMetrics (hdc, &font->tm);
dfff8a69 5453 font->double_byte_p = GetFontLanguageInfo(hdc) & GCP_DBCS;
1a292d24
AI
5454 SelectObject (hdc, oldobj);
5455 ReleaseDC (dpyinfo->root_window, hdc);
6fc2811b
JR
5456 /* Fill out details in lf according to the font that was
5457 actually loaded. */
5458 lf.lfHeight = font->tm.tmInternalLeading - font->tm.tmHeight;
5459 lf.lfWidth = font->tm.tmAveCharWidth;
5460 lf.lfWeight = font->tm.tmWeight;
5461 lf.lfItalic = font->tm.tmItalic;
5462 lf.lfCharSet = font->tm.tmCharSet;
5463 lf.lfPitchAndFamily = ((font->tm.tmPitchAndFamily & TMPF_FIXED_PITCH)
d88c567c 5464 ? VARIABLE_PITCH : FIXED_PITCH);
6fc2811b
JR
5465 lf.lfOutPrecision = ((font->tm.tmPitchAndFamily & TMPF_VECTOR)
5466 ? OUT_STROKE_PRECIS : OUT_STRING_PRECIS);
1a292d24 5467 }
5ac45f98 5468
1a292d24 5469 UNBLOCK_INPUT;
5ac45f98 5470
4587b026
GV
5471 if (!ok)
5472 {
1a292d24
AI
5473 w32_unload_font (dpyinfo, font);
5474 return (NULL);
5475 }
ee78dc32 5476
6fc2811b
JR
5477 /* Find a free slot in the font table. */
5478 for (i = 0; i < dpyinfo->n_fonts; ++i)
5479 if (dpyinfo->font_table[i].name == NULL)
5480 break;
5481
5482 /* If no free slot found, maybe enlarge the font table. */
5483 if (i == dpyinfo->n_fonts
5484 && dpyinfo->n_fonts == dpyinfo->font_table_size)
4587b026 5485 {
6fc2811b
JR
5486 int sz;
5487 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
5488 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
4587b026 5489 dpyinfo->font_table
6fc2811b 5490 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
4587b026
GV
5491 }
5492
6fc2811b
JR
5493 fontp = dpyinfo->font_table + i;
5494 if (i == dpyinfo->n_fonts)
5495 ++dpyinfo->n_fonts;
4587b026
GV
5496
5497 /* Now fill in the slots of *FONTP. */
5498 BLOCK_INPUT;
5499 fontp->font = font;
6fc2811b 5500 fontp->font_idx = i;
4587b026
GV
5501 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
5502 bcopy (fontname, fontp->name, strlen (fontname) + 1);
5503
5504 /* Work out the font's full name. */
5505 full_name = (char *)xmalloc (100);
5506 if (full_name && w32_to_x_font (&lf, full_name, 100))
5507 fontp->full_name = full_name;
5508 else
5509 {
5510 /* If all else fails - just use the name we used to load it. */
5511 xfree (full_name);
5512 fontp->full_name = fontp->name;
5513 }
5514
5515 fontp->size = FONT_WIDTH (font);
5516 fontp->height = FONT_HEIGHT (font);
5517
5518 /* The slot `encoding' specifies how to map a character
5519 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
126f2e35
JR
5520 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
5521 (0:0x20..0x7F, 1:0xA0..0xFF,
5522 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
4587b026 5523 2:0xA020..0xFF7F). For the moment, we don't know which charset
6fc2811b 5524 uses this font. So, we set information in fontp->encoding[1]
4587b026
GV
5525 which is never used by any charset. If mapping can't be
5526 decided, set FONT_ENCODING_NOT_DECIDED. */
33d52f9c
GV
5527
5528 /* SJIS fonts need to be set to type 4, all others seem to work as
5529 type FONT_ENCODING_NOT_DECIDED. */
5530 encoding = strrchr (fontp->name, '-');
5531 if (encoding && stricmp (encoding+1, "sjis") == 0)
1c885fe1 5532 fontp->encoding[1] = 4;
33d52f9c 5533 else
1c885fe1 5534 fontp->encoding[1] = FONT_ENCODING_NOT_DECIDED;
4587b026
GV
5535
5536 /* The following three values are set to 0 under W32, which is
5537 what they get set to if XGetFontProperty fails under X. */
5538 fontp->baseline_offset = 0;
5539 fontp->relative_compose = 0;
33d52f9c 5540 fontp->default_ascent = 0;
4587b026 5541
6fc2811b
JR
5542 /* Set global flag fonts_changed_p to non-zero if the font loaded
5543 has a character with a smaller width than any other character
5544 before, or if the font loaded has a smalle>r height than any
5545 other font loaded before. If this happens, it will make a
5546 glyph matrix reallocation necessary. */
5547 fonts_changed_p = x_compute_min_glyph_bounds (f);
4587b026 5548 UNBLOCK_INPUT;
4587b026
GV
5549 return fontp;
5550 }
5551}
5552
33d52f9c
GV
5553/* Load font named FONTNAME of size SIZE for frame F, and return a
5554 pointer to the structure font_info while allocating it dynamically.
5555 If loading fails, return NULL. */
5556struct font_info *
5557w32_load_font (f,fontname,size)
5558struct frame *f;
5559char * fontname;
5560int size;
5561{
5562 Lisp_Object bdf_fonts;
5563 struct font_info *retval = NULL;
5564
5565 bdf_fonts = w32_list_bdf_fonts (build_string (fontname));
5566
5567 while (!retval && CONSP (bdf_fonts))
5568 {
5569 char *bdf_name, *bdf_file;
5570 Lisp_Object bdf_pair;
5571
8e713be6
KR
5572 bdf_name = XSTRING (XCAR (bdf_fonts))->data;
5573 bdf_pair = Fassoc (XCAR (bdf_fonts), Vw32_bdf_filename_alist);
5574 bdf_file = XSTRING (XCDR (bdf_pair))->data;
33d52f9c
GV
5575
5576 retval = w32_load_bdf_font (f, bdf_name, size, bdf_file);
5577
8e713be6 5578 bdf_fonts = XCDR (bdf_fonts);
33d52f9c
GV
5579 }
5580
5581 if (retval)
5582 return retval;
5583
5584 return w32_load_system_font(f, fontname, size);
5585}
5586
5587
ee78dc32 5588void
fbd6baed
GV
5589w32_unload_font (dpyinfo, font)
5590 struct w32_display_info *dpyinfo;
ee78dc32
GV
5591 XFontStruct * font;
5592{
5593 if (font)
5594 {
33d52f9c
GV
5595 if (font->bdf) w32_free_bdf_font (font->bdf);
5596
3c190163 5597 if (font->hfont) DeleteObject(font->hfont);
ee78dc32
GV
5598 xfree (font);
5599 }
5600}
5601
fbd6baed 5602/* The font conversion stuff between x and w32 */
ee78dc32
GV
5603
5604/* X font string is as follows (from faces.el)
5605 * (let ((- "[-?]")
5606 * (foundry "[^-]+")
5607 * (family "[^-]+")
5608 * (weight "\\(bold\\|demibold\\|medium\\)") ; 1
5609 * (weight\? "\\([^-]*\\)") ; 1
5610 * (slant "\\([ior]\\)") ; 2
5611 * (slant\? "\\([^-]?\\)") ; 2
5612 * (swidth "\\([^-]*\\)") ; 3
5613 * (adstyle "[^-]*") ; 4
5614 * (pixelsize "[0-9]+")
5615 * (pointsize "[0-9][0-9]+")
5616 * (resx "[0-9][0-9]+")
5617 * (resy "[0-9][0-9]+")
5618 * (spacing "[cmp?*]")
5619 * (avgwidth "[0-9]+")
5620 * (registry "[^-]+")
5621 * (encoding "[^-]+")
5622 * )
ee78dc32 5623 */
ee78dc32
GV
5624
5625LONG
fbd6baed 5626x_to_w32_weight (lpw)
ee78dc32
GV
5627 char * lpw;
5628{
5629 if (!lpw) return (FW_DONTCARE);
5ac45f98
GV
5630
5631 if (stricmp (lpw,"heavy") == 0) return FW_HEAVY;
5632 else if (stricmp (lpw,"extrabold") == 0) return FW_EXTRABOLD;
5633 else if (stricmp (lpw,"bold") == 0) return FW_BOLD;
5634 else if (stricmp (lpw,"demibold") == 0) return FW_SEMIBOLD;
1edf84e7 5635 else if (stricmp (lpw,"semibold") == 0) return FW_SEMIBOLD;
5ac45f98
GV
5636 else if (stricmp (lpw,"medium") == 0) return FW_MEDIUM;
5637 else if (stricmp (lpw,"normal") == 0) return FW_NORMAL;
5638 else if (stricmp (lpw,"light") == 0) return FW_LIGHT;
5639 else if (stricmp (lpw,"extralight") == 0) return FW_EXTRALIGHT;
5640 else if (stricmp (lpw,"thin") == 0) return FW_THIN;
ee78dc32 5641 else
5ac45f98 5642 return FW_DONTCARE;
ee78dc32
GV
5643}
5644
5ac45f98 5645
ee78dc32 5646char *
fbd6baed 5647w32_to_x_weight (fnweight)
ee78dc32
GV
5648 int fnweight;
5649{
5ac45f98
GV
5650 if (fnweight >= FW_HEAVY) return "heavy";
5651 if (fnweight >= FW_EXTRABOLD) return "extrabold";
5652 if (fnweight >= FW_BOLD) return "bold";
03f8fb34 5653 if (fnweight >= FW_SEMIBOLD) return "demibold";
5ac45f98
GV
5654 if (fnweight >= FW_MEDIUM) return "medium";
5655 if (fnweight >= FW_NORMAL) return "normal";
5656 if (fnweight >= FW_LIGHT) return "light";
5657 if (fnweight >= FW_EXTRALIGHT) return "extralight";
5658 if (fnweight >= FW_THIN) return "thin";
5659 else
5660 return "*";
5661}
5662
5663LONG
fbd6baed 5664x_to_w32_charset (lpcs)
5ac45f98
GV
5665 char * lpcs;
5666{
dfff8a69 5667 Lisp_Object rest;
4587b026 5668
dfff8a69
JR
5669 /* Look through w32-charset-info-alist for the character set.
5670 Format of each entry is
5671 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
5672 */
5673 for (rest = Vw32_charset_info_alist; CONSP (rest); rest = XCDR (rest))
5674 {
5675 Lisp_Object this_entry = XCAR (rest);
5676 char * x_charset = XSTRING (XCAR (this_entry))->data;
4587b026 5677
dfff8a69
JR
5678 if (strnicmp (lpcs, x_charset, strlen(x_charset)) == 0)
5679 {
5680 Lisp_Object w32_charset = XCAR (XCDR (this_entry));
5681 // Translate Lisp symbol to number.
5682 if (w32_charset == Qw32_charset_ansi)
5683 return ANSI_CHARSET;
5684 if (w32_charset == Qw32_charset_symbol)
5685 return SYMBOL_CHARSET;
5686 if (w32_charset == Qw32_charset_shiftjis)
5687 return SHIFTJIS_CHARSET;
5688 if (w32_charset == Qw32_charset_hangul)
5689 return HANGEUL_CHARSET;
5690 if (w32_charset == Qw32_charset_chinesebig5)
5691 return CHINESEBIG5_CHARSET;
5692 if (w32_charset == Qw32_charset_gb2312)
5693 return GB2312_CHARSET;
5694 if (w32_charset == Qw32_charset_oem)
5695 return OEM_CHARSET;
5696#ifdef JOHAB_CHARSET
5697 if (w32_charset == Qw32_charset_johab)
5698 return JOHAB_CHARSET;
5699 if (w32_charset == Qw32_charset_easteurope)
5700 return EASTEUROPE_CHARSET;
5701 if (w32_charset == Qw32_charset_turkish)
5702 return TURKISH_CHARSET;
5703 if (w32_charset == Qw32_charset_baltic)
5704 return BALTIC_CHARSET;
5705 if (w32_charset == Qw32_charset_russian)
5706 return RUSSIAN_CHARSET;
5707 if (w32_charset == Qw32_charset_arabic)
5708 return ARABIC_CHARSET;
5709 if (w32_charset == Qw32_charset_greek)
5710 return GREEK_CHARSET;
5711 if (w32_charset == Qw32_charset_hebrew)
5712 return HEBREW_CHARSET;
5713 if (w32_charset == Qw32_charset_thai)
5714 return THAI_CHARSET;
5715 if (w32_charset == Qw32_charset_mac)
5716 return MAC_CHARSET;
5717#endif /* JOHAB_CHARSET */
5ac45f98 5718#ifdef UNICODE_CHARSET
dfff8a69
JR
5719 if (w32_charset == Qw32_charset_unicode)
5720 return UNICODE_CHARSET;
5ac45f98 5721#endif
dfff8a69
JR
5722 }
5723 }
5724
5725 return DEFAULT_CHARSET;
5ac45f98
GV
5726}
5727
dfff8a69 5728
5ac45f98 5729char *
fbd6baed 5730w32_to_x_charset (fncharset)
5ac45f98
GV
5731 int fncharset;
5732{
1edf84e7
GV
5733 static char buf[16];
5734
dfff8a69
JR
5735 /* NTEMACS_TODO: use w32-charset-info-alist. Multiple matches
5736 are possible, so this will require more than just a rewrite of
5737 this function. w32_to_x_font is the only user of this function,
5738 and that will require rewriting too, and its users. */
5ac45f98
GV
5739 switch (fncharset)
5740 {
4587b026
GV
5741 /* ansi is considered iso8859-1, as most modern ansi fonts are. */
5742 case ANSI_CHARSET: return "iso8859-1";
5743 case DEFAULT_CHARSET: return "ascii-*";
33d52f9c 5744 case SYMBOL_CHARSET: return "ms-symbol";
a4e691ee 5745 case SHIFTJIS_CHARSET: return "jisx0208-sjis";
33d52f9c 5746 case HANGEUL_CHARSET: return "ksc5601.1987-*";
4587b026
GV
5747 case GB2312_CHARSET: return "gb2312-*";
5748 case CHINESEBIG5_CHARSET: return "big5-*";
33d52f9c 5749 case OEM_CHARSET: return "ms-oem";
4587b026
GV
5750
5751 /* More recent versions of Windows (95 and NT4.0) define more
5752 character sets. */
5753#ifdef EASTEUROPE_CHARSET
5754 case EASTEUROPE_CHARSET: return "iso8859-2";
a4e691ee 5755 case TURKISH_CHARSET: return "iso8859-9";
4587b026 5756 case BALTIC_CHARSET: return "iso8859-4";
33d52f9c
GV
5757
5758 /* W95 with international support but not IE4 often has the
5759 KOI8-R codepage but not ISO8859-5. */
5760 case RUSSIAN_CHARSET:
5761 if (!IsValidCodePage(28595) && IsValidCodePage(20886))
5762 return "koi8-r";
5763 else
5764 return "iso8859-5";
4587b026
GV
5765 case ARABIC_CHARSET: return "iso8859-6";
5766 case GREEK_CHARSET: return "iso8859-7";
5767 case HEBREW_CHARSET: return "iso8859-8";
5768 case VIETNAMESE_CHARSET: return "viscii1.1-*";
5769 case THAI_CHARSET: return "tis620-*";
33d52f9c
GV
5770 case MAC_CHARSET: return "mac-*";
5771 case JOHAB_CHARSET: return "ksc5601.1992-*";
a4e691ee 5772
4587b026
GV
5773#endif
5774
5ac45f98 5775#ifdef UNICODE_CHARSET
4587b026 5776 case UNICODE_CHARSET: return "iso10646-unicode";
5ac45f98
GV
5777#endif
5778 }
1edf84e7 5779 /* Encode numerical value of unknown charset. */
4587b026 5780 sprintf (buf, "*-#%u", fncharset);
1edf84e7 5781 return buf;
ee78dc32
GV
5782}
5783
dfff8a69
JR
5784
5785/* Get the Windows codepage corresponding to the specified font. The
5786 charset info in the font name is used to look up
5787 w32-charset-to-codepage-alist. */
5788int
5789w32_codepage_for_font (char *fontname)
5790{
5791 Lisp_Object codepage;
5792 char charset_str[20], *charset, *end;
5793
5794 /* Extract charset part of font string. */
5795 if (sscanf (fontname,
5796 "-%*[^-]-%*[^-]-%*[^-]-%*[^-]-%*[^-]-%*[^-]-%*[^-]-%*[^-]-%*[^-]-%*[^-]-%*[^-]-%*[^-]-%19s",
5797 charset_str) == EOF)
5798 return CP_DEFAULT;
5799
5800 /* Remove leading "*-". */
5801 if (strncmp ("*-", charset_str, 2) == 0)
5802 charset = charset_str + 2;
5803 else
5804 charset = charset_str;
5805
5806 /* Stop match at wildcard (including preceding '-'). */
5807 if (end = strchr (charset, '*'))
5808 {
5809 if (end > charset && *(end-1) == '-')
5810 end--;
5811 *end = '\0';
5812 }
5813
5814 codepage = Fcdr (Fcdr (Fassoc (build_string(charset),
5815 Vw32_charset_info_alist)));
5816 if (INTEGERP (codepage))
5817 return XINT (codepage);
5818 else
5819 return CP_DEFAULT;
5820}
5821
5822
ee78dc32 5823BOOL
fbd6baed 5824w32_to_x_font (lplogfont, lpxstr, len)
ee78dc32
GV
5825 LOGFONT * lplogfont;
5826 char * lpxstr;
5827 int len;
5828{
6fc2811b 5829 char* fonttype;
f46e6225 5830 char *fontname;
3cb20f4a
RS
5831 char height_pixels[8];
5832 char height_dpi[8];
5833 char width_pixels[8];
4587b026 5834 char *fontname_dash;
d88c567c
JR
5835 int display_resy = one_w32_display_info.resy;
5836 int display_resx = one_w32_display_info.resx;
f46e6225
GV
5837 int bufsz;
5838 struct coding_system coding;
3cb20f4a
RS
5839
5840 if (!lpxstr) abort ();
ee78dc32 5841
3cb20f4a
RS
5842 if (!lplogfont)
5843 return FALSE;
5844
6fc2811b
JR
5845 if (lplogfont->lfOutPrecision == OUT_STRING_PRECIS)
5846 fonttype = "raster";
5847 else if (lplogfont->lfOutPrecision == OUT_STROKE_PRECIS)
5848 fonttype = "outline";
5849 else
5850 fonttype = "unknown";
5851
f46e6225
GV
5852 setup_coding_system (Fcheck_coding_system (Vw32_system_coding_system),
5853 &coding);
aab5ac44
KH
5854 coding.src_multibyte = 0;
5855 coding.dst_multibyte = 1;
f46e6225
GV
5856 coding.mode |= CODING_MODE_LAST_BLOCK;
5857 bufsz = decoding_buffer_size (&coding, LF_FACESIZE);
5858
5859 fontname = alloca(sizeof(*fontname) * bufsz);
5860 decode_coding (&coding, lplogfont->lfFaceName, fontname,
5861 strlen(lplogfont->lfFaceName), bufsz - 1);
5862 *(fontname + coding.produced) = '\0';
4587b026
GV
5863
5864 /* Replace dashes with underscores so the dashes are not
f46e6225 5865 misinterpreted. */
4587b026
GV
5866 fontname_dash = fontname;
5867 while (fontname_dash = strchr (fontname_dash, '-'))
5868 *fontname_dash = '_';
5869
3cb20f4a 5870 if (lplogfont->lfHeight)
ee78dc32 5871 {
3cb20f4a
RS
5872 sprintf (height_pixels, "%u", abs (lplogfont->lfHeight));
5873 sprintf (height_dpi, "%u",
33d52f9c 5874 abs (lplogfont->lfHeight) * 720 / display_resy);
5ac45f98
GV
5875 }
5876 else
ee78dc32 5877 {
3cb20f4a
RS
5878 strcpy (height_pixels, "*");
5879 strcpy (height_dpi, "*");
ee78dc32 5880 }
3cb20f4a
RS
5881 if (lplogfont->lfWidth)
5882 sprintf (width_pixels, "%u", lplogfont->lfWidth * 10);
5883 else
5884 strcpy (width_pixels, "*");
5885
5886 _snprintf (lpxstr, len - 1,
6fc2811b
JR
5887 "-%s-%s-%s-%c-normal-normal-%s-%s-%d-%d-%c-%s-%s",
5888 fonttype, /* foundry */
4587b026
GV
5889 fontname, /* family */
5890 w32_to_x_weight (lplogfont->lfWeight), /* weight */
5891 lplogfont->lfItalic?'i':'r', /* slant */
5892 /* setwidth name */
5893 /* add style name */
5894 height_pixels, /* pixel size */
5895 height_dpi, /* point size */
33d52f9c
GV
5896 display_resx, /* resx */
5897 display_resy, /* resy */
4587b026
GV
5898 ((lplogfont->lfPitchAndFamily & 0x3) == VARIABLE_PITCH)
5899 ? 'p' : 'c', /* spacing */
5900 width_pixels, /* avg width */
5901 w32_to_x_charset (lplogfont->lfCharSet) /* charset registry
5902 and encoding*/
3cb20f4a
RS
5903 );
5904
ee78dc32
GV
5905 lpxstr[len - 1] = 0; /* just to be sure */
5906 return (TRUE);
5907}
5908
5909BOOL
fbd6baed 5910x_to_w32_font (lpxstr, lplogfont)
ee78dc32
GV
5911 char * lpxstr;
5912 LOGFONT * lplogfont;
5913{
f46e6225
GV
5914 struct coding_system coding;
5915
ee78dc32 5916 if (!lplogfont) return (FALSE);
f46e6225 5917
ee78dc32 5918 memset (lplogfont, 0, sizeof (*lplogfont));
5ac45f98 5919
1a292d24 5920 /* Set default value for each field. */
771c47d5 5921#if 1
ee78dc32
GV
5922 lplogfont->lfOutPrecision = OUT_DEFAULT_PRECIS;
5923 lplogfont->lfClipPrecision = CLIP_DEFAULT_PRECIS;
5924 lplogfont->lfQuality = DEFAULT_QUALITY;
5ac45f98
GV
5925#else
5926 /* go for maximum quality */
5927 lplogfont->lfOutPrecision = OUT_STROKE_PRECIS;
5928 lplogfont->lfClipPrecision = CLIP_STROKE_PRECIS;
5929 lplogfont->lfQuality = PROOF_QUALITY;
5930#endif
5931
1a292d24
AI
5932 lplogfont->lfCharSet = DEFAULT_CHARSET;
5933 lplogfont->lfWeight = FW_DONTCARE;
5934 lplogfont->lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
5935
5ac45f98
GV
5936 if (!lpxstr)
5937 return FALSE;
5938
5939 /* Provide a simple escape mechanism for specifying Windows font names
5940 * directly -- if font spec does not beginning with '-', assume this
5941 * format:
5942 * "<font name>[:height in pixels[:width in pixels[:weight]]]"
5943 */
ee78dc32 5944
5ac45f98
GV
5945 if (*lpxstr == '-')
5946 {
33d52f9c
GV
5947 int fields, tem;
5948 char name[50], weight[20], slant, pitch, pixels[10], height[10],
5949 width[10], resy[10], remainder[20];
5ac45f98 5950 char * encoding;
33d52f9c 5951 int dpi = one_w32_display_info.height_in;
5ac45f98
GV
5952
5953 fields = sscanf (lpxstr,
33d52f9c
GV
5954 "-%*[^-]-%49[^-]-%19[^-]-%c-%*[^-]-%*[^-]-%9[^-]-%9[^-]-%*[^-]-%9[^-]-%c-%9[^-]-%19s",
5955 name, weight, &slant, pixels, height, resy, &pitch, width, remainder);
5ac45f98
GV
5956 if (fields == EOF) return (FALSE);
5957
6fc2811b
JR
5958 /* If wildcards cover more than one field, we don't know which
5959 field is which, so don't fill any in. */
5960
5961 if (fields < 9)
5962 fields = 0;
5963
5ac45f98
GV
5964 if (fields > 0 && name[0] != '*')
5965 {
8ea3e054
RS
5966 int bufsize;
5967 unsigned char *buf;
5968
f46e6225
GV
5969 setup_coding_system
5970 (Fcheck_coding_system (Vw32_system_coding_system), &coding);
aab5ac44
KH
5971 coding.src_multibyte = 1;
5972 coding.dst_multibyte = 1;
8ea3e054
RS
5973 bufsize = encoding_buffer_size (&coding, strlen (name));
5974 buf = (unsigned char *) alloca (bufsize);
f46e6225 5975 coding.mode |= CODING_MODE_LAST_BLOCK;
8ea3e054
RS
5976 encode_coding (&coding, name, buf, strlen (name), bufsize);
5977 if (coding.produced >= LF_FACESIZE)
5978 coding.produced = LF_FACESIZE - 1;
5979 buf[coding.produced] = 0;
5980 strcpy (lplogfont->lfFaceName, buf);
5ac45f98
GV
5981 }
5982 else
5983 {
6fc2811b 5984 lplogfont->lfFaceName[0] = '\0';
5ac45f98
GV
5985 }
5986
5987 fields--;
5988
fbd6baed 5989 lplogfont->lfWeight = x_to_w32_weight ((fields > 0 ? weight : ""));
5ac45f98
GV
5990
5991 fields--;
5992
6fc2811b 5993 if (!NILP (Vw32_enable_synthesized_fonts))
5ac45f98
GV
5994 lplogfont->lfItalic = (fields > 0 && slant == 'i');
5995
5996 fields--;
5997
5998 if (fields > 0 && pixels[0] != '*')
5999 lplogfont->lfHeight = atoi (pixels);
6000
6001 fields--;
5ac45f98 6002 fields--;
33d52f9c
GV
6003 if (fields > 0 && resy[0] != '*')
6004 {
6fc2811b 6005 tem = atoi (resy);
33d52f9c
GV
6006 if (tem > 0) dpi = tem;
6007 }
5ac45f98 6008
33d52f9c
GV
6009 if (fields > -1 && lplogfont->lfHeight == 0 && height[0] != '*')
6010 lplogfont->lfHeight = atoi (height) * dpi / 720;
6011
6012 if (fields > 0)
5ac45f98
GV
6013 lplogfont->lfPitchAndFamily =
6014 (fields > 0 && pitch == 'p') ? VARIABLE_PITCH : FIXED_PITCH;
6015
6016 fields--;
6017
6018 if (fields > 0 && width[0] != '*')
6019 lplogfont->lfWidth = atoi (width) / 10;
6020
6021 fields--;
6022
4587b026 6023 /* Strip the trailing '-' if present. (it shouldn't be, as it
d88c567c 6024 fails the test against xlfd-tight-regexp in fontset.el). */
3c190163 6025 {
5ac45f98
GV
6026 int len = strlen (remainder);
6027 if (len > 0 && remainder[len-1] == '-')
6028 remainder[len-1] = 0;
ee78dc32 6029 }
5ac45f98
GV
6030 encoding = remainder;
6031 if (strncmp (encoding, "*-", 2) == 0)
6032 encoding += 2;
fbd6baed 6033 lplogfont->lfCharSet = x_to_w32_charset (fields > 0 ? encoding : "");
5ac45f98
GV
6034 }
6035 else
6036 {
6037 int fields;
6038 char name[100], height[10], width[10], weight[20];
a1a80b40 6039
5ac45f98
GV
6040 fields = sscanf (lpxstr,
6041 "%99[^:]:%9[^:]:%9[^:]:%19s",
6042 name, height, width, weight);
6043
6044 if (fields == EOF) return (FALSE);
6045
6046 if (fields > 0)
6047 {
6048 strncpy (lplogfont->lfFaceName,name, LF_FACESIZE);
6049 lplogfont->lfFaceName[LF_FACESIZE-1] = 0;
6050 }
6051 else
6052 {
6053 lplogfont->lfFaceName[0] = 0;
6054 }
6055
6056 fields--;
6057
6058 if (fields > 0)
6059 lplogfont->lfHeight = atoi (height);
6060
6061 fields--;
6062
6063 if (fields > 0)
6064 lplogfont->lfWidth = atoi (width);
6065
6066 fields--;
6067
fbd6baed 6068 lplogfont->lfWeight = x_to_w32_weight ((fields > 0 ? weight : ""));
5ac45f98
GV
6069 }
6070
6071 /* This makes TrueType fonts work better. */
6072 lplogfont->lfHeight = - abs (lplogfont->lfHeight);
6fc2811b 6073
ee78dc32
GV
6074 return (TRUE);
6075}
6076
d88c567c
JR
6077/* Strip the pixel height and point height from the given xlfd, and
6078 return the pixel height. If no pixel height is specified, calculate
6079 one from the point height, or if that isn't defined either, return
6080 0 (which usually signifies a scalable font).
6081*/
6082int xlfd_strip_height (char *fontname)
6083{
6084 int pixel_height, point_height, dpi, field_number;
6085 char *read_from, *write_to;
6086
6087 xassert (fontname);
6088
6089 pixel_height = field_number = 0;
6090 write_to = NULL;
6091
6092 /* Look for height fields. */
6093 for (read_from = fontname; *read_from; read_from++)
6094 {
6095 if (*read_from == '-')
6096 {
6097 field_number++;
6098 if (field_number == 7) /* Pixel height. */
6099 {
6100 read_from++;
6101 write_to = read_from;
6102
6103 /* Find end of field. */
6104 for (;*read_from && *read_from != '-'; read_from++)
6105 ;
6106
6107 /* Split the fontname at end of field. */
6108 if (*read_from)
6109 {
6110 *read_from = '\0';
6111 read_from++;
6112 }
6113 pixel_height = atoi (write_to);
6114 /* Blank out field. */
6115 if (read_from > write_to)
6116 {
6117 *write_to = '-';
6118 write_to++;
6119 }
6120 /* If the pixel height field is at the end (partial xfld),
6121 return now. */
6122 else
6123 return pixel_height;
6124
6125 /* If we got a pixel height, the point height can be
6126 ignored. Just blank it out and break now. */
6127 if (pixel_height)
6128 {
6129 /* Find end of point size field. */
6130 for (; *read_from && *read_from != '-'; read_from++)
6131 ;
6132
6133 if (*read_from)
6134 read_from++;
6135
6136 /* Blank out the point size field. */
6137 if (read_from > write_to)
6138 {
6139 *write_to = '-';
6140 write_to++;
6141 }
6142 else
6143 return pixel_height;
6144
6145 break;
6146 }
6147 /* If the point height is already blank, break now. */
6148 if (*read_from == '-')
6149 {
6150 read_from++;
6151 break;
6152 }
6153 }
6154 else if (field_number == 8)
6155 {
6156 /* If we didn't get a pixel height, try to get the point
6157 height and convert that. */
6158 int point_size;
6159 char *point_size_start = read_from++;
6160
6161 /* Find end of field. */
6162 for (; *read_from && *read_from != '-'; read_from++)
6163 ;
6164
6165 if (*read_from)
6166 {
6167 *read_from = '\0';
6168 read_from++;
6169 }
6170
6171 point_size = atoi (point_size_start);
6172
6173 /* Convert to pixel height. */
6174 pixel_height = point_size
6175 * one_w32_display_info.height_in / 720;
6176
6177 /* Blank out this field and break. */
6178 *write_to = '-';
6179 write_to++;
6180 break;
6181 }
6182 }
6183 }
6184
6185 /* Shift the rest of the font spec into place. */
6186 if (write_to && read_from > write_to)
6187 {
6188 for (; *read_from; read_from++, write_to++)
6189 *write_to = *read_from;
6190 *write_to = '\0';
6191 }
6192
6193 return pixel_height;
6194}
6195
6fc2811b 6196/* Assume parameter 1 is fully qualified, no wildcards. */
ee78dc32 6197BOOL
6fc2811b
JR
6198w32_font_match (fontname, pattern)
6199 char * fontname;
6200 char * pattern;
ee78dc32 6201{
6fc2811b 6202 char *regex = alloca (strlen (pattern) * 2);
d88c567c 6203 char *font_name_copy = alloca (strlen (fontname) + 1);
6fc2811b 6204 char *ptr;
ee78dc32 6205
d88c567c
JR
6206 /* Copy fontname so we can modify it during comparison. */
6207 strcpy (font_name_copy, fontname);
6208
6fc2811b
JR
6209 ptr = regex;
6210 *ptr++ = '^';
ee78dc32 6211
6fc2811b
JR
6212 /* Turn pattern into a regexp and do a regexp match. */
6213 for (; *pattern; pattern++)
6214 {
6215 if (*pattern == '?')
6216 *ptr++ = '.';
6217 else if (*pattern == '*')
6218 {
6219 *ptr++ = '.';
6220 *ptr++ = '*';
6221 }
33d52f9c 6222 else
6fc2811b 6223 *ptr++ = *pattern;
ee78dc32 6224 }
6fc2811b
JR
6225 *ptr = '$';
6226 *(ptr + 1) = '\0';
6227
d88c567c
JR
6228 /* Strip out font heights and compare them seperately, since
6229 rounding error can cause mismatches. This also allows a
6230 comparison between a font that declares only a pixel height and a
6231 pattern that declares the point height.
6232 */
6233 {
6234 int font_height, pattern_height;
6235
6236 font_height = xlfd_strip_height (font_name_copy);
6237 pattern_height = xlfd_strip_height (regex);
6238
6239 /* Compare now, and don't bother doing expensive regexp matching
6240 if the heights differ. */
6241 if (font_height && pattern_height && (font_height != pattern_height))
6242 return FALSE;
6243 }
6244
6fc2811b 6245 return (fast_c_string_match_ignore_case (build_string (regex),
d88c567c 6246 font_name_copy) >= 0);
ee78dc32
GV
6247}
6248
5ca0cd71
GV
6249/* Callback functions, and a structure holding info they need, for
6250 listing system fonts on W32. We need one set of functions to do the
6251 job properly, but these don't work on NT 3.51 and earlier, so we
6252 have a second set which don't handle character sets properly to
6253 fall back on.
6254
6255 In both cases, there are two passes made. The first pass gets one
6256 font from each family, the second pass lists all the fonts from
6257 each family. */
6258
ee78dc32
GV
6259typedef struct enumfont_t
6260{
6261 HDC hdc;
6262 int numFonts;
3cb20f4a 6263 LOGFONT logfont;
ee78dc32
GV
6264 XFontStruct *size_ref;
6265 Lisp_Object *pattern;
ee78dc32
GV
6266 Lisp_Object *tail;
6267} enumfont_t;
6268
6269int CALLBACK
6270enum_font_cb2 (lplf, lptm, FontType, lpef)
6271 ENUMLOGFONT * lplf;
6272 NEWTEXTMETRIC * lptm;
6273 int FontType;
6274 enumfont_t * lpef;
6275{
1edf84e7 6276 if (lplf->elfLogFont.lfStrikeOut || lplf->elfLogFont.lfUnderline)
ee78dc32
GV
6277 return (1);
6278
4587b026
GV
6279 /* Check that the character set matches if it was specified */
6280 if (lpef->logfont.lfCharSet != DEFAULT_CHARSET &&
6281 lplf->elfLogFont.lfCharSet != lpef->logfont.lfCharSet)
6282 return (1);
6283
ee78dc32
GV
6284 {
6285 char buf[100];
4587b026 6286 Lisp_Object width = Qnil;
ee78dc32 6287
6fc2811b
JR
6288 /* Truetype fonts do not report their true metrics until loaded */
6289 if (FontType != RASTER_FONTTYPE)
3cb20f4a 6290 {
6fc2811b
JR
6291 if (!NILP (*(lpef->pattern)))
6292 {
6293 /* Scalable fonts are as big as you want them to be. */
6294 lplf->elfLogFont.lfHeight = lpef->logfont.lfHeight;
6295 lplf->elfLogFont.lfWidth = lpef->logfont.lfWidth;
6296 width = make_number (lpef->logfont.lfWidth);
6297 }
6298 else
6299 {
6300 lplf->elfLogFont.lfHeight = 0;
6301 lplf->elfLogFont.lfWidth = 0;
6302 }
3cb20f4a 6303 }
6fc2811b 6304
f46e6225
GV
6305 /* Make sure the height used here is the same as everywhere
6306 else (ie character height, not cell height). */
6fc2811b
JR
6307 if (lplf->elfLogFont.lfHeight > 0)
6308 {
6309 /* lptm can be trusted for RASTER fonts, but not scalable ones. */
6310 if (FontType == RASTER_FONTTYPE)
6311 lplf->elfLogFont.lfHeight = lptm->tmInternalLeading - lptm->tmHeight;
6312 else
6313 lplf->elfLogFont.lfHeight = -lplf->elfLogFont.lfHeight;
6314 }
4587b026 6315
33d52f9c
GV
6316 if (!w32_to_x_font (&(lplf->elfLogFont), buf, 100))
6317 return (0);
ee78dc32 6318
5ca0cd71
GV
6319 if (NILP (*(lpef->pattern))
6320 || w32_font_match (buf, XSTRING (*(lpef->pattern))->data))
ee78dc32 6321 {
4587b026 6322 *lpef->tail = Fcons (Fcons (build_string (buf), width), Qnil);
8e713be6 6323 lpef->tail = &(XCDR (*lpef->tail));
ee78dc32
GV
6324 lpef->numFonts++;
6325 }
6326 }
6fc2811b 6327
ee78dc32
GV
6328 return (1);
6329}
6330
6331int CALLBACK
6332enum_font_cb1 (lplf, lptm, FontType, lpef)
6333 ENUMLOGFONT * lplf;
6334 NEWTEXTMETRIC * lptm;
6335 int FontType;
6336 enumfont_t * lpef;
6337{
6338 return EnumFontFamilies (lpef->hdc,
6339 lplf->elfLogFont.lfFaceName,
6340 (FONTENUMPROC) enum_font_cb2,
6341 (LPARAM) lpef);
6342}
6343
6344
5ca0cd71
GV
6345int CALLBACK
6346enum_fontex_cb2 (lplf, lptm, font_type, lpef)
6347 ENUMLOGFONTEX * lplf;
6348 NEWTEXTMETRICEX * lptm;
6349 int font_type;
6350 enumfont_t * lpef;
6351{
6352 /* We are not interested in the extra info we get back from the 'Ex
6353 version - only the fact that we get character set variations
6354 enumerated seperately. */
6355 return enum_font_cb2 ((ENUMLOGFONT *) lplf, (NEWTEXTMETRIC *) lptm,
6356 font_type, lpef);
6357}
6358
6359int CALLBACK
6360enum_fontex_cb1 (lplf, lptm, font_type, lpef)
6361 ENUMLOGFONTEX * lplf;
6362 NEWTEXTMETRICEX * lptm;
6363 int font_type;
6364 enumfont_t * lpef;
6365{
6366 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
6367 FARPROC enum_font_families_ex
6368 = GetProcAddress ( gdi32, "EnumFontFamiliesExA");
6369 /* We don't really expect EnumFontFamiliesEx to disappear once we
6370 get here, so don't bother handling it gracefully. */
6371 if (enum_font_families_ex == NULL)
6372 error ("gdi32.dll has disappeared!");
6373 return enum_font_families_ex (lpef->hdc,
6374 &lplf->elfLogFont,
6375 (FONTENUMPROC) enum_fontex_cb2,
6376 (LPARAM) lpef, 0);
6377}
6378
4587b026
GV
6379/* Interface to fontset handler. (adapted from mw32font.c in Meadow
6380 and xterm.c in Emacs 20.3) */
6381
5ca0cd71 6382Lisp_Object w32_list_bdf_fonts (Lisp_Object pattern, int max_names)
33d52f9c
GV
6383{
6384 char *fontname, *ptnstr;
6385 Lisp_Object list, tem, newlist = Qnil;
55dcfc15 6386 int n_fonts = 0;
33d52f9c
GV
6387
6388 list = Vw32_bdf_filename_alist;
6389 ptnstr = XSTRING (pattern)->data;
6390
8e713be6 6391 for ( ; CONSP (list); list = XCDR (list))
33d52f9c 6392 {
8e713be6 6393 tem = XCAR (list);
33d52f9c 6394 if (CONSP (tem))
8e713be6 6395 fontname = XSTRING (XCAR (tem))->data;
33d52f9c
GV
6396 else if (STRINGP (tem))
6397 fontname = XSTRING (tem)->data;
6398 else
6399 continue;
6400
6401 if (w32_font_match (fontname, ptnstr))
5ca0cd71 6402 {
8e713be6 6403 newlist = Fcons (XCAR (tem), newlist);
5ca0cd71
GV
6404 n_fonts++;
6405 if (n_fonts >= max_names)
6406 break;
6407 }
33d52f9c
GV
6408 }
6409
6410 return newlist;
6411}
6412
5ca0cd71
GV
6413Lisp_Object w32_list_synthesized_fonts (FRAME_PTR f, Lisp_Object pattern,
6414 int size, int max_names);
6415
4587b026
GV
6416/* Return a list of names of available fonts matching PATTERN on frame
6417 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
6418 to be listed. Frame F NULL means we have not yet created any
6419 frame, which means we can't get proper size info, as we don't have
6420 a device context to use for GetTextMetrics.
6421 MAXNAMES sets a limit on how many fonts to match. */
6422
6423Lisp_Object
6424w32_list_fonts (FRAME_PTR f, Lisp_Object pattern, int size, int maxnames )
6425{
6fc2811b 6426 Lisp_Object patterns, key = Qnil, tem, tpat;
4587b026 6427 Lisp_Object list = Qnil, newlist = Qnil, second_best = Qnil;
33d52f9c 6428 struct w32_display_info *dpyinfo = &one_w32_display_info;
5ca0cd71 6429 int n_fonts = 0;
396594fe 6430
4587b026
GV
6431 patterns = Fassoc (pattern, Valternate_fontname_alist);
6432 if (NILP (patterns))
6433 patterns = Fcons (pattern, Qnil);
6434
8e713be6 6435 for (; CONSP (patterns); patterns = XCDR (patterns))
4587b026
GV
6436 {
6437 enumfont_t ef;
6438
8e713be6 6439 tpat = XCAR (patterns);
4587b026
GV
6440
6441 /* See if we cached the result for this particular query.
6442 The cache is an alist of the form:
6443 ((PATTERN (FONTNAME . WIDTH) ...) ...)
6444 */
8e713be6 6445 if (tem = XCDR (dpyinfo->name_list_element),
33d52f9c 6446 !NILP (list = Fassoc (tpat, tem)))
4587b026
GV
6447 {
6448 list = Fcdr_safe (list);
6449 /* We have a cached list. Don't have to get the list again. */
6450 goto label_cached;
6451 }
6452
6453 BLOCK_INPUT;
6454 /* At first, put PATTERN in the cache. */
6455 list = Qnil;
33d52f9c
GV
6456 ef.pattern = &tpat;
6457 ef.tail = &list;
4587b026 6458 ef.numFonts = 0;
33d52f9c 6459
5ca0cd71
GV
6460 /* Use EnumFontFamiliesEx where it is available, as it knows
6461 about character sets. Fall back to EnumFontFamilies for
6462 older versions of NT that don't support the 'Ex function. */
33d52f9c 6463 x_to_w32_font (STRINGP (tpat) ? XSTRING (tpat)->data :
4587b026
GV
6464 NULL, &ef.logfont);
6465 {
5ca0cd71
GV
6466 LOGFONT font_match_pattern;
6467 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
6468 FARPROC enum_font_families_ex
6469 = GetProcAddress ( gdi32, "EnumFontFamiliesExA");
6470
6471 /* We do our own pattern matching so we can handle wildcards. */
6472 font_match_pattern.lfFaceName[0] = 0;
6473 font_match_pattern.lfPitchAndFamily = 0;
6474 /* We can use the charset, because if it is a wildcard it will
6475 be DEFAULT_CHARSET anyway. */
6476 font_match_pattern.lfCharSet = ef.logfont.lfCharSet;
6477
33d52f9c 6478 ef.hdc = GetDC (dpyinfo->root_window);
4587b026 6479
5ca0cd71
GV
6480 if (enum_font_families_ex)
6481 enum_font_families_ex (ef.hdc,
6482 &font_match_pattern,
6483 (FONTENUMPROC) enum_fontex_cb1,
6484 (LPARAM) &ef, 0);
6485 else
6486 EnumFontFamilies (ef.hdc, NULL, (FONTENUMPROC) enum_font_cb1,
6487 (LPARAM)&ef);
4587b026 6488
33d52f9c 6489 ReleaseDC (dpyinfo->root_window, ef.hdc);
4587b026
GV
6490 }
6491
6492 UNBLOCK_INPUT;
6493
6494 /* Make a list of the fonts we got back.
6495 Store that in the font cache for the display. */
8e713be6 6496 XCDR (dpyinfo->name_list_element)
33d52f9c 6497 = Fcons (Fcons (tpat, list),
8e713be6 6498 XCDR (dpyinfo->name_list_element));
4587b026
GV
6499
6500 label_cached:
6501 if (NILP (list)) continue; /* Try the remaining alternatives. */
6502
6503 newlist = second_best = Qnil;
6504
6505 /* Make a list of the fonts that have the right width. */
8e713be6 6506 for (; CONSP (list); list = XCDR (list))
4587b026
GV
6507 {
6508 int found_size;
8e713be6 6509 tem = XCAR (list);
4587b026
GV
6510
6511 if (!CONSP (tem))
6512 continue;
8e713be6 6513 if (NILP (XCAR (tem)))
4587b026
GV
6514 continue;
6515 if (!size)
6516 {
8e713be6 6517 newlist = Fcons (XCAR (tem), newlist);
5ca0cd71
GV
6518 n_fonts++;
6519 if (n_fonts >= maxnames)
6520 break;
6521 else
6522 continue;
4587b026 6523 }
8e713be6 6524 if (!INTEGERP (XCDR (tem)))
4587b026
GV
6525 {
6526 /* Since we don't yet know the size of the font, we must
6527 load it and try GetTextMetrics. */
4587b026
GV
6528 W32FontStruct thisinfo;
6529 LOGFONT lf;
6530 HDC hdc;
6531 HANDLE oldobj;
6532
8e713be6 6533 if (!x_to_w32_font (XSTRING (XCAR (tem))->data, &lf))
4587b026
GV
6534 continue;
6535
6536 BLOCK_INPUT;
33d52f9c 6537 thisinfo.bdf = NULL;
4587b026
GV
6538 thisinfo.hfont = CreateFontIndirect (&lf);
6539 if (thisinfo.hfont == NULL)
6540 continue;
6541
6542 hdc = GetDC (dpyinfo->root_window);
6543 oldobj = SelectObject (hdc, thisinfo.hfont);
6544 if (GetTextMetrics (hdc, &thisinfo.tm))
8e713be6 6545 XCDR (tem) = make_number (FONT_WIDTH (&thisinfo));
4587b026 6546 else
8e713be6 6547 XCDR (tem) = make_number (0);
4587b026
GV
6548 SelectObject (hdc, oldobj);
6549 ReleaseDC (dpyinfo->root_window, hdc);
6550 DeleteObject(thisinfo.hfont);
6551 UNBLOCK_INPUT;
6552 }
8e713be6 6553 found_size = XINT (XCDR (tem));
4587b026 6554 if (found_size == size)
5ca0cd71 6555 {
8e713be6 6556 newlist = Fcons (XCAR (tem), newlist);
5ca0cd71
GV
6557 n_fonts++;
6558 if (n_fonts >= maxnames)
6559 break;
6560 }
4587b026
GV
6561 /* keep track of the closest matching size in case
6562 no exact match is found. */
6563 else if (found_size > 0)
6564 {
6565 if (NILP (second_best))
6566 second_best = tem;
5ca0cd71 6567
4587b026
GV
6568 else if (found_size < size)
6569 {
8e713be6
KR
6570 if (XINT (XCDR (second_best)) > size
6571 || XINT (XCDR (second_best)) < found_size)
4587b026
GV
6572 second_best = tem;
6573 }
6574 else
6575 {
8e713be6
KR
6576 if (XINT (XCDR (second_best)) > size
6577 && XINT (XCDR (second_best)) >
4587b026
GV
6578 found_size)
6579 second_best = tem;
6580 }
6581 }
6582 }
6583
6584 if (!NILP (newlist))
6585 break;
6586 else if (!NILP (second_best))
6587 {
8e713be6 6588 newlist = Fcons (XCAR (second_best), Qnil);
4587b026
GV
6589 break;
6590 }
6591 }
6592
33d52f9c 6593 /* Include any bdf fonts. */
5ca0cd71 6594 if (n_fonts < maxnames)
33d52f9c
GV
6595 {
6596 Lisp_Object combined[2];
5ca0cd71 6597 combined[0] = w32_list_bdf_fonts (pattern, maxnames - n_fonts);
33d52f9c
GV
6598 combined[1] = newlist;
6599 newlist = Fnconc(2, combined);
6600 }
6601
5ca0cd71
GV
6602 /* If we can't find a font that matches, check if Windows would be
6603 able to synthesize it from a different style. */
6fc2811b 6604 if (NILP (newlist) && !NILP (Vw32_enable_synthesized_fonts))
5ca0cd71
GV
6605 newlist = w32_list_synthesized_fonts (f, pattern, size, maxnames);
6606
4587b026
GV
6607 return newlist;
6608}
6609
5ca0cd71
GV
6610Lisp_Object
6611w32_list_synthesized_fonts (f, pattern, size, max_names)
6612 FRAME_PTR f;
6613 Lisp_Object pattern;
6614 int size;
6615 int max_names;
6616{
6617 int fields;
6618 char *full_pattn, *new_pattn, foundary[50], family[50], *pattn_part2;
6619 char style[20], slant;
6620 Lisp_Object matches, match, tem, synthed_matches = Qnil;
6621
6622 full_pattn = XSTRING (pattern)->data;
6623
6624 pattn_part2 = alloca (XSTRING (pattern)->size);
6625 /* Allow some space for wildcard expansion. */
6626 new_pattn = alloca (XSTRING (pattern)->size + 100);
6627
6628 fields = sscanf (full_pattn, "-%49[^-]-%49[^-]-%19[^-]-%c-%s",
6629 foundary, family, style, &slant, pattn_part2);
6630 if (fields == EOF || fields < 5)
6631 return Qnil;
6632
6633 /* If the style and slant are wildcards already there is no point
6634 checking again (and we don't want to keep recursing). */
6635 if (*style == '*' && slant == '*')
6636 return Qnil;
6637
6638 sprintf (new_pattn, "-%s-%s-*-*-%s", foundary, family, pattn_part2);
6639
6640 matches = w32_list_fonts (f, build_string (new_pattn), size, max_names);
6641
8e713be6 6642 for ( ; CONSP (matches); matches = XCDR (matches))
5ca0cd71 6643 {
8e713be6 6644 tem = XCAR (matches);
5ca0cd71
GV
6645 if (!STRINGP (tem))
6646 continue;
6647
6648 full_pattn = XSTRING (tem)->data;
6649 fields = sscanf (full_pattn, "-%49[^-]-%49[^-]-%*[^-]-%*c-%s",
6650 foundary, family, pattn_part2);
6651 if (fields == EOF || fields < 3)
6652 continue;
6653
6654 sprintf (new_pattn, "-%s-%s-%s-%c-%s", foundary, family, style,
6655 slant, pattn_part2);
6656
6657 synthed_matches = Fcons (build_string (new_pattn),
6658 synthed_matches);
6659 }
6660
6661 return synthed_matches;
6662}
6663
6664
4587b026
GV
6665/* Return a pointer to struct font_info of font FONT_IDX of frame F. */
6666struct font_info *
6667w32_get_font_info (f, font_idx)
6668 FRAME_PTR f;
6669 int font_idx;
6670{
6671 return (FRAME_W32_FONT_TABLE (f) + font_idx);
6672}
6673
6674
6675struct font_info*
6676w32_query_font (struct frame *f, char *fontname)
6677{
6678 int i;
6679 struct font_info *pfi;
6680
6681 pfi = FRAME_W32_FONT_TABLE (f);
6682
6683 for (i = 0; i < one_w32_display_info.n_fonts ;i++, pfi++)
6684 {
6685 if (strcmp(pfi->name, fontname) == 0) return pfi;
6686 }
6687
6688 return NULL;
6689}
6690
6691/* Find a CCL program for a font specified by FONTP, and set the member
6692 `encoder' of the structure. */
6693
6694void
6695w32_find_ccl_program (fontp)
6696 struct font_info *fontp;
6697{
3545439c 6698 Lisp_Object list, elt;
4587b026 6699
8e713be6 6700 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
4587b026 6701 {
8e713be6 6702 elt = XCAR (list);
4587b026 6703 if (CONSP (elt)
8e713be6
KR
6704 && STRINGP (XCAR (elt))
6705 && (fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
4587b026 6706 >= 0))
3545439c
KH
6707 break;
6708 }
6709 if (! NILP (list))
6710 {
17eedd00
KH
6711 struct ccl_program *ccl
6712 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
3545439c 6713
8e713be6 6714 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
3545439c
KH
6715 xfree (ccl);
6716 else
6717 fontp->font_encoder = ccl;
4587b026
GV
6718 }
6719}
6720
6721\f
6fc2811b
JR
6722DEFUN ("w32-find-bdf-fonts", Fw32_find_bdf_fonts, Sw32_find_bdf_fonts,
6723 1, 1, 0,
6724 "Return a list of BDF fonts in DIR, suitable for appending to\n\
6725w32-bdf-filename-alist. Fonts which do not contain an xfld description\n\
6726will not be included in the list. DIR may be a list of directories.")
6727 (directory)
6728 Lisp_Object directory;
6729{
6730 Lisp_Object list = Qnil;
6731 struct gcpro gcpro1, gcpro2;
ee78dc32 6732
6fc2811b
JR
6733 if (!CONSP (directory))
6734 return w32_find_bdf_fonts_in_dir (directory);
ee78dc32 6735
6fc2811b 6736 for ( ; CONSP (directory); directory = XCDR (directory))
ee78dc32 6737 {
6fc2811b
JR
6738 Lisp_Object pair[2];
6739 pair[0] = list;
6740 pair[1] = Qnil;
6741 GCPRO2 (directory, list);
6742 pair[1] = w32_find_bdf_fonts_in_dir( XCAR (directory) );
6743 list = Fnconc( 2, pair );
6744 UNGCPRO;
6745 }
6746 return list;
6747}
ee78dc32 6748
6fc2811b
JR
6749/* Find BDF files in a specified directory. (use GCPRO when calling,
6750 as this calls lisp to get a directory listing). */
6751Lisp_Object w32_find_bdf_fonts_in_dir( Lisp_Object directory )
6752{
6753 Lisp_Object filelist, list = Qnil;
6754 char fontname[100];
ee78dc32 6755
6fc2811b
JR
6756 if (!STRINGP(directory))
6757 return Qnil;
ee78dc32 6758
6fc2811b
JR
6759 filelist = Fdirectory_files (directory, Qt,
6760 build_string (".*\\.[bB][dD][fF]"), Qt);
ee78dc32 6761
6fc2811b 6762 for ( ; CONSP(filelist); filelist = XCDR (filelist))
ee78dc32 6763 {
6fc2811b
JR
6764 Lisp_Object filename = XCAR (filelist);
6765 if (w32_BDF_to_x_font (XSTRING (filename)->data, fontname, 100))
6766 store_in_alist (&list, build_string (fontname), filename);
6767 }
6768 return list;
6769}
ee78dc32 6770
6fc2811b
JR
6771\f
6772DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
dfff8a69 6773 "Internal function called by `color-defined-p', which see.")
6fc2811b
JR
6774 (color, frame)
6775 Lisp_Object color, frame;
6776{
6777 XColor foo;
6778 FRAME_PTR f = check_x_frame (frame);
ee78dc32 6779
6fc2811b 6780 CHECK_STRING (color, 1);
ee78dc32 6781
6fc2811b
JR
6782 if (w32_defined_color (f, XSTRING (color)->data, &foo, 0))
6783 return Qt;
6784 else
6785 return Qnil;
6786}
ee78dc32 6787
2d764c78 6788DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
dfff8a69 6789 "Internal function called by `color-values', which see.")
ee78dc32
GV
6790 (color, frame)
6791 Lisp_Object color, frame;
6792{
6fc2811b 6793 XColor foo;
ee78dc32
GV
6794 FRAME_PTR f = check_x_frame (frame);
6795
6796 CHECK_STRING (color, 1);
6797
6fc2811b 6798 if (w32_defined_color (f, XSTRING (color)->data, &foo, 0))
ee78dc32
GV
6799 {
6800 Lisp_Object rgb[3];
6801
6fc2811b
JR
6802 rgb[0] = make_number ((GetRValue (foo.pixel) << 8)
6803 | GetRValue (foo.pixel));
6804 rgb[1] = make_number ((GetGValue (foo.pixel) << 8)
6805 | GetGValue (foo.pixel));
6806 rgb[2] = make_number ((GetBValue (foo.pixel) << 8)
6807 | GetBValue (foo.pixel));
ee78dc32
GV
6808 return Flist (3, rgb);
6809 }
6810 else
6811 return Qnil;
6812}
6813
2d764c78 6814DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
dfff8a69 6815 "Internal function called by `display-color-p', which see.")
ee78dc32
GV
6816 (display)
6817 Lisp_Object display;
6818{
fbd6baed 6819 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
6820
6821 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 2)
6822 return Qnil;
6823
6824 return Qt;
6825}
6826
6827DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p, Sx_display_grayscale_p,
6828 0, 1, 0,
6829 "Return t if the X display supports shades of gray.\n\
6830Note that color displays do support shades of gray.\n\
6831The optional argument DISPLAY specifies which display to ask about.\n\
6832DISPLAY should be either a frame or a display name (a string).\n\
6833If omitted or nil, that stands for the selected frame's display.")
6834 (display)
6835 Lisp_Object display;
6836{
fbd6baed 6837 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
6838
6839 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 1)
6840 return Qnil;
6841
6842 return Qt;
6843}
6844
6845DEFUN ("x-display-pixel-width", Fx_display_pixel_width, Sx_display_pixel_width,
6846 0, 1, 0,
6847 "Returns the width in pixels of the X display DISPLAY.\n\
6848The optional argument DISPLAY specifies which display to ask about.\n\
6849DISPLAY should be either a frame or a display name (a string).\n\
6850If omitted or nil, that stands for the selected frame's display.")
6851 (display)
6852 Lisp_Object display;
6853{
fbd6baed 6854 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
6855
6856 return make_number (dpyinfo->width);
6857}
6858
6859DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
6860 Sx_display_pixel_height, 0, 1, 0,
6861 "Returns the height in pixels of the X display DISPLAY.\n\
6862The optional argument DISPLAY specifies which display to ask about.\n\
6863DISPLAY should be either a frame or a display name (a string).\n\
6864If omitted or nil, that stands for the selected frame's display.")
6865 (display)
6866 Lisp_Object display;
6867{
fbd6baed 6868 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
6869
6870 return make_number (dpyinfo->height);
6871}
6872
6873DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
6874 0, 1, 0,
6875 "Returns the number of bitplanes of the display DISPLAY.\n\
6876The optional argument DISPLAY specifies which display to ask about.\n\
6877DISPLAY should be either a frame or a display name (a string).\n\
6878If omitted or nil, that stands for the selected frame's display.")
6879 (display)
6880 Lisp_Object display;
6881{
fbd6baed 6882 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
6883
6884 return make_number (dpyinfo->n_planes * dpyinfo->n_cbits);
6885}
6886
6887DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
6888 0, 1, 0,
6889 "Returns the number of color cells of the display DISPLAY.\n\
6890The optional argument DISPLAY specifies which display to ask about.\n\
6891DISPLAY should be either a frame or a display name (a string).\n\
6892If omitted or nil, that stands for the selected frame's display.")
6893 (display)
6894 Lisp_Object display;
6895{
fbd6baed 6896 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
6897 HDC hdc;
6898 int cap;
6899
5ac45f98
GV
6900 hdc = GetDC (dpyinfo->root_window);
6901 if (dpyinfo->has_palette)
6902 cap = GetDeviceCaps (hdc,SIZEPALETTE);
6903 else
6904 cap = GetDeviceCaps (hdc,NUMCOLORS);
ee78dc32
GV
6905
6906 ReleaseDC (dpyinfo->root_window, hdc);
6907
6908 return make_number (cap);
6909}
6910
6911DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
6912 Sx_server_max_request_size,
6913 0, 1, 0,
6914 "Returns the maximum request size of the server of display DISPLAY.\n\
6915The optional argument DISPLAY specifies which display to ask about.\n\
6916DISPLAY should be either a frame or a display name (a string).\n\
6917If omitted or nil, that stands for the selected frame's display.")
6918 (display)
6919 Lisp_Object display;
6920{
fbd6baed 6921 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
6922
6923 return make_number (1);
6924}
6925
6926DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
fbd6baed 6927 "Returns the vendor ID string of the W32 system (Microsoft).\n\
ee78dc32
GV
6928The optional argument DISPLAY specifies which display to ask about.\n\
6929DISPLAY should be either a frame or a display name (a string).\n\
6930If omitted or nil, that stands for the selected frame's display.")
6931 (display)
6932 Lisp_Object display;
6933{
dfff8a69 6934 return build_string ("Microsoft Corp.");
ee78dc32
GV
6935}
6936
6937DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
6938 "Returns the version numbers of the server of display DISPLAY.\n\
6939The value is a list of three integers: the major and minor\n\
6940version numbers, and the vendor-specific release\n\
6941number. See also the function `x-server-vendor'.\n\n\
6942The optional argument DISPLAY specifies which display to ask about.\n\
6943DISPLAY should be either a frame or a display name (a string).\n\
6944If omitted or nil, that stands for the selected frame's display.")
6945 (display)
6946 Lisp_Object display;
6947{
fbd6baed
GV
6948 return Fcons (make_number (w32_major_version),
6949 Fcons (make_number (w32_minor_version), Qnil));
ee78dc32
GV
6950}
6951
6952DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
6953 "Returns the number of screens on the server of display DISPLAY.\n\
6954The optional argument DISPLAY specifies which display to ask about.\n\
6955DISPLAY should be either a frame or a display name (a string).\n\
6956If omitted or nil, that stands for the selected frame's display.")
6957 (display)
6958 Lisp_Object display;
6959{
ee78dc32
GV
6960 return make_number (1);
6961}
6962
6963DEFUN ("x-display-mm-height", Fx_display_mm_height, Sx_display_mm_height, 0, 1, 0,
6964 "Returns the height in millimeters of the X display DISPLAY.\n\
6965The optional argument DISPLAY specifies which display to ask about.\n\
6966DISPLAY should be either a frame or a display name (a string).\n\
6967If omitted or nil, that stands for the selected frame's display.")
6968 (display)
6969 Lisp_Object display;
6970{
fbd6baed 6971 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
6972 HDC hdc;
6973 int cap;
6974
5ac45f98 6975 hdc = GetDC (dpyinfo->root_window);
3c190163 6976
ee78dc32 6977 cap = GetDeviceCaps (hdc, VERTSIZE);
3c190163 6978
ee78dc32
GV
6979 ReleaseDC (dpyinfo->root_window, hdc);
6980
6981 return make_number (cap);
6982}
6983
6984DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
6985 "Returns the width in millimeters of the X display DISPLAY.\n\
6986The optional argument DISPLAY specifies which display to ask about.\n\
6987DISPLAY should be either a frame or a display name (a string).\n\
6988If omitted or nil, that stands for the selected frame's display.")
6989 (display)
6990 Lisp_Object display;
6991{
fbd6baed 6992 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
6993
6994 HDC hdc;
6995 int cap;
6996
5ac45f98 6997 hdc = GetDC (dpyinfo->root_window);
3c190163 6998
ee78dc32 6999 cap = GetDeviceCaps (hdc, HORZSIZE);
3c190163 7000
ee78dc32
GV
7001 ReleaseDC (dpyinfo->root_window, hdc);
7002
7003 return make_number (cap);
7004}
7005
7006DEFUN ("x-display-backing-store", Fx_display_backing_store,
7007 Sx_display_backing_store, 0, 1, 0,
7008 "Returns an indication of whether display DISPLAY does backing store.\n\
7009The value may be `always', `when-mapped', or `not-useful'.\n\
7010The optional argument DISPLAY specifies which display to ask about.\n\
7011DISPLAY should be either a frame or a display name (a string).\n\
7012If omitted or nil, that stands for the selected frame's display.")
7013 (display)
7014 Lisp_Object display;
7015{
7016 return intern ("not-useful");
7017}
7018
7019DEFUN ("x-display-visual-class", Fx_display_visual_class,
7020 Sx_display_visual_class, 0, 1, 0,
7021 "Returns the visual class of the display DISPLAY.\n\
7022The value is one of the symbols `static-gray', `gray-scale',\n\
7023`static-color', `pseudo-color', `true-color', or `direct-color'.\n\n\
7024The optional argument DISPLAY specifies which display to ask about.\n\
7025DISPLAY should be either a frame or a display name (a string).\n\
7026If omitted or nil, that stands for the selected frame's display.")
7027 (display)
7028 Lisp_Object display;
7029{
fbd6baed 7030 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
7031
7032#if 0
7033 switch (dpyinfo->visual->class)
7034 {
7035 case StaticGray: return (intern ("static-gray"));
7036 case GrayScale: return (intern ("gray-scale"));
7037 case StaticColor: return (intern ("static-color"));
7038 case PseudoColor: return (intern ("pseudo-color"));
7039 case TrueColor: return (intern ("true-color"));
7040 case DirectColor: return (intern ("direct-color"));
7041 default:
7042 error ("Display has an unknown visual class");
7043 }
7044#endif
7045
7046 error ("Display has an unknown visual class");
7047}
7048
7049DEFUN ("x-display-save-under", Fx_display_save_under,
7050 Sx_display_save_under, 0, 1, 0,
7051 "Returns t if the display DISPLAY supports the save-under feature.\n\
7052The optional argument DISPLAY specifies which display to ask about.\n\
7053DISPLAY should be either a frame or a display name (a string).\n\
7054If omitted or nil, that stands for the selected frame's display.")
7055 (display)
7056 Lisp_Object display;
7057{
6fc2811b
JR
7058 return Qnil;
7059}
7060\f
7061int
7062x_pixel_width (f)
7063 register struct frame *f;
7064{
7065 return PIXEL_WIDTH (f);
7066}
7067
7068int
7069x_pixel_height (f)
7070 register struct frame *f;
7071{
7072 return PIXEL_HEIGHT (f);
7073}
7074
7075int
7076x_char_width (f)
7077 register struct frame *f;
7078{
7079 return FONT_WIDTH (f->output_data.w32->font);
7080}
7081
7082int
7083x_char_height (f)
7084 register struct frame *f;
7085{
7086 return f->output_data.w32->line_height;
7087}
7088
7089int
7090x_screen_planes (f)
7091 register struct frame *f;
7092{
7093 return FRAME_W32_DISPLAY_INFO (f)->n_planes;
7094}
7095\f
7096/* Return the display structure for the display named NAME.
7097 Open a new connection if necessary. */
7098
7099struct w32_display_info *
7100x_display_info_for_name (name)
7101 Lisp_Object name;
7102{
7103 Lisp_Object names;
7104 struct w32_display_info *dpyinfo;
7105
7106 CHECK_STRING (name, 0);
7107
7108 for (dpyinfo = &one_w32_display_info, names = w32_display_name_list;
7109 dpyinfo;
7110 dpyinfo = dpyinfo->next, names = XCDR (names))
7111 {
7112 Lisp_Object tem;
7113 tem = Fstring_equal (XCAR (XCAR (names)), name);
7114 if (!NILP (tem))
7115 return dpyinfo;
7116 }
7117
7118 /* Use this general default value to start with. */
7119 Vx_resource_name = Vinvocation_name;
7120
7121 validate_x_resource_name ();
7122
7123 dpyinfo = w32_term_init (name, (unsigned char *)0,
7124 (char *) XSTRING (Vx_resource_name)->data);
7125
7126 if (dpyinfo == 0)
7127 error ("Cannot connect to server %s", XSTRING (name)->data);
7128
7129 w32_in_use = 1;
7130 XSETFASTINT (Vwindow_system_version, 3);
7131
7132 return dpyinfo;
7133}
7134
7135DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
7136 1, 3, 0, "Open a connection to a server.\n\
7137DISPLAY is the name of the display to connect to.\n\
7138Optional second arg XRM-STRING is a string of resources in xrdb format.\n\
7139If the optional third arg MUST-SUCCEED is non-nil,\n\
7140terminate Emacs if we can't open the connection.")
7141 (display, xrm_string, must_succeed)
7142 Lisp_Object display, xrm_string, must_succeed;
7143{
7144 unsigned char *xrm_option;
7145 struct w32_display_info *dpyinfo;
7146
7147 CHECK_STRING (display, 0);
7148 if (! NILP (xrm_string))
7149 CHECK_STRING (xrm_string, 1);
7150
7151 if (! EQ (Vwindow_system, intern ("w32")))
7152 error ("Not using Microsoft Windows");
7153
7154 /* Allow color mapping to be defined externally; first look in user's
7155 HOME directory, then in Emacs etc dir for a file called rgb.txt. */
7156 {
7157 Lisp_Object color_file;
7158 struct gcpro gcpro1;
7159
7160 color_file = build_string("~/rgb.txt");
7161
7162 GCPRO1 (color_file);
7163
7164 if (NILP (Ffile_readable_p (color_file)))
7165 color_file =
7166 Fexpand_file_name (build_string ("rgb.txt"),
7167 Fsymbol_value (intern ("data-directory")));
7168
7169 Vw32_color_map = Fw32_load_color_file (color_file);
7170
7171 UNGCPRO;
7172 }
7173 if (NILP (Vw32_color_map))
7174 Vw32_color_map = Fw32_default_color_map ();
7175
7176 if (! NILP (xrm_string))
7177 xrm_option = (unsigned char *) XSTRING (xrm_string)->data;
7178 else
7179 xrm_option = (unsigned char *) 0;
7180
7181 /* Use this general default value to start with. */
7182 /* First remove .exe suffix from invocation-name - it looks ugly. */
7183 {
7184 char basename[ MAX_PATH ], *str;
7185
7186 strcpy (basename, XSTRING (Vinvocation_name)->data);
7187 str = strrchr (basename, '.');
7188 if (str) *str = 0;
7189 Vinvocation_name = build_string (basename);
7190 }
7191 Vx_resource_name = Vinvocation_name;
7192
7193 validate_x_resource_name ();
7194
7195 /* This is what opens the connection and sets x_current_display.
7196 This also initializes many symbols, such as those used for input. */
7197 dpyinfo = w32_term_init (display, xrm_option,
7198 (char *) XSTRING (Vx_resource_name)->data);
7199
7200 if (dpyinfo == 0)
7201 {
7202 if (!NILP (must_succeed))
7203 fatal ("Cannot connect to server %s.\n",
7204 XSTRING (display)->data);
7205 else
7206 error ("Cannot connect to server %s", XSTRING (display)->data);
7207 }
7208
7209 w32_in_use = 1;
7210
7211 XSETFASTINT (Vwindow_system_version, 3);
7212 return Qnil;
7213}
7214
7215DEFUN ("x-close-connection", Fx_close_connection,
7216 Sx_close_connection, 1, 1, 0,
7217 "Close the connection to DISPLAY's server.\n\
7218For DISPLAY, specify either a frame or a display name (a string).\n\
7219If DISPLAY is nil, that stands for the selected frame's display.")
7220 (display)
7221 Lisp_Object display;
7222{
7223 struct w32_display_info *dpyinfo = check_x_display_info (display);
7224 int i;
7225
7226 if (dpyinfo->reference_count > 0)
7227 error ("Display still has frames on it");
7228
7229 BLOCK_INPUT;
7230 /* Free the fonts in the font table. */
7231 for (i = 0; i < dpyinfo->n_fonts; i++)
7232 if (dpyinfo->font_table[i].name)
7233 {
126f2e35
JR
7234 if (dpyinfo->font_table[i].name != dpyinfo->font_table[i].full_name)
7235 xfree (dpyinfo->font_table[i].full_name);
6fc2811b 7236 xfree (dpyinfo->font_table[i].name);
6fc2811b
JR
7237 w32_unload_font (dpyinfo, dpyinfo->font_table[i].font);
7238 }
7239 x_destroy_all_bitmaps (dpyinfo);
7240
7241 x_delete_display (dpyinfo);
7242 UNBLOCK_INPUT;
7243
7244 return Qnil;
7245}
7246
7247DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
7248 "Return the list of display names that Emacs has connections to.")
7249 ()
7250{
7251 Lisp_Object tail, result;
7252
7253 result = Qnil;
7254 for (tail = w32_display_name_list; ! NILP (tail); tail = XCDR (tail))
7255 result = Fcons (XCAR (XCAR (tail)), result);
7256
7257 return result;
7258}
7259
7260DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
7261 "If ON is non-nil, report errors as soon as the erring request is made.\n\
7262If ON is nil, allow buffering of requests.\n\
7263This is a noop on W32 systems.\n\
7264The optional second argument DISPLAY specifies which display to act on.\n\
7265DISPLAY should be either a frame or a display name (a string).\n\
7266If DISPLAY is omitted or nil, that stands for the selected frame's display.")
7267 (on, display)
7268 Lisp_Object display, on;
7269{
6fc2811b
JR
7270 return Qnil;
7271}
7272
7273\f
7274\f
7275/***********************************************************************
7276 Image types
7277 ***********************************************************************/
7278
7279/* Value is the number of elements of vector VECTOR. */
7280
7281#define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR))
7282
7283/* List of supported image types. Use define_image_type to add new
7284 types. Use lookup_image_type to find a type for a given symbol. */
7285
7286static struct image_type *image_types;
7287
6fc2811b
JR
7288/* The symbol `image' which is the car of the lists used to represent
7289 images in Lisp. */
7290
7291extern Lisp_Object Qimage;
7292
7293/* The symbol `xbm' which is used as the type symbol for XBM images. */
7294
7295Lisp_Object Qxbm;
7296
7297/* Keywords. */
7298
6fc2811b 7299extern Lisp_Object QCwidth, QCheight, QCforeground, QCbackground, QCfile;
dfff8a69
JR
7300extern Lisp_Object QCdata;
7301Lisp_Object QCtype, QCascent, QCmargin, QCrelief;
6fc2811b 7302Lisp_Object QCalgorithm, QCcolor_symbols, QCheuristic_mask;
dfff8a69 7303Lisp_Object QCindex;
6fc2811b
JR
7304
7305/* Other symbols. */
7306
7307Lisp_Object Qlaplace;
7308
7309/* Time in seconds after which images should be removed from the cache
7310 if not displayed. */
7311
7312Lisp_Object Vimage_cache_eviction_delay;
7313
7314/* Function prototypes. */
7315
7316static void define_image_type P_ ((struct image_type *type));
7317static struct image_type *lookup_image_type P_ ((Lisp_Object symbol));
7318static void image_error P_ ((char *format, Lisp_Object, Lisp_Object));
7319static void x_laplace P_ ((struct frame *, struct image *));
7320static int x_build_heuristic_mask P_ ((struct frame *, struct image *,
7321 Lisp_Object));
7322
dfff8a69 7323
6fc2811b
JR
7324/* Define a new image type from TYPE. This adds a copy of TYPE to
7325 image_types and adds the symbol *TYPE->type to Vimage_types. */
7326
7327static void
7328define_image_type (type)
7329 struct image_type *type;
7330{
7331 /* Make a copy of TYPE to avoid a bus error in a dumped Emacs.
7332 The initialized data segment is read-only. */
7333 struct image_type *p = (struct image_type *) xmalloc (sizeof *p);
7334 bcopy (type, p, sizeof *p);
7335 p->next = image_types;
7336 image_types = p;
7337 Vimage_types = Fcons (*p->type, Vimage_types);
7338}
7339
7340
7341/* Look up image type SYMBOL, and return a pointer to its image_type
7342 structure. Value is null if SYMBOL is not a known image type. */
7343
7344static INLINE struct image_type *
7345lookup_image_type (symbol)
7346 Lisp_Object symbol;
7347{
7348 struct image_type *type;
7349
7350 for (type = image_types; type; type = type->next)
7351 if (EQ (symbol, *type->type))
7352 break;
7353
7354 return type;
7355}
7356
7357
7358/* Value is non-zero if OBJECT is a valid Lisp image specification. A
7359 valid image specification is a list whose car is the symbol
7360 `image', and whose rest is a property list. The property list must
7361 contain a value for key `:type'. That value must be the name of a
7362 supported image type. The rest of the property list depends on the
7363 image type. */
7364
7365int
7366valid_image_p (object)
7367 Lisp_Object object;
7368{
7369 int valid_p = 0;
7370
7371 if (CONSP (object) && EQ (XCAR (object), Qimage))
7372 {
7373 Lisp_Object symbol = Fplist_get (XCDR (object), QCtype);
7374 struct image_type *type = lookup_image_type (symbol);
7375
7376 if (type)
7377 valid_p = type->valid_p (object);
7378 }
7379
7380 return valid_p;
7381}
7382
7383
7384/* Log error message with format string FORMAT and argument ARG.
7385 Signaling an error, e.g. when an image cannot be loaded, is not a
7386 good idea because this would interrupt redisplay, and the error
7387 message display would lead to another redisplay. This function
7388 therefore simply displays a message. */
7389
7390static void
7391image_error (format, arg1, arg2)
7392 char *format;
7393 Lisp_Object arg1, arg2;
7394{
7395 add_to_log (format, arg1, arg2);
7396}
7397
7398
7399\f
7400/***********************************************************************
7401 Image specifications
7402 ***********************************************************************/
7403
7404enum image_value_type
7405{
7406 IMAGE_DONT_CHECK_VALUE_TYPE,
7407 IMAGE_STRING_VALUE,
7408 IMAGE_SYMBOL_VALUE,
7409 IMAGE_POSITIVE_INTEGER_VALUE,
7410 IMAGE_NON_NEGATIVE_INTEGER_VALUE,
dfff8a69 7411 IMAGE_ASCENT_VALUE,
6fc2811b
JR
7412 IMAGE_INTEGER_VALUE,
7413 IMAGE_FUNCTION_VALUE,
7414 IMAGE_NUMBER_VALUE,
7415 IMAGE_BOOL_VALUE
7416};
7417
7418/* Structure used when parsing image specifications. */
7419
7420struct image_keyword
7421{
7422 /* Name of keyword. */
7423 char *name;
7424
7425 /* The type of value allowed. */
7426 enum image_value_type type;
7427
7428 /* Non-zero means key must be present. */
7429 int mandatory_p;
7430
7431 /* Used to recognize duplicate keywords in a property list. */
7432 int count;
7433
7434 /* The value that was found. */
7435 Lisp_Object value;
7436};
7437
7438
7439static int parse_image_spec P_ ((Lisp_Object, struct image_keyword *,
7440 int, Lisp_Object));
7441static Lisp_Object image_spec_value P_ ((Lisp_Object, Lisp_Object, int *));
7442
7443
7444/* Parse image spec SPEC according to KEYWORDS. A valid image spec
7445 has the format (image KEYWORD VALUE ...). One of the keyword/
7446 value pairs must be `:type TYPE'. KEYWORDS is a vector of
7447 image_keywords structures of size NKEYWORDS describing other
7448 allowed keyword/value pairs. Value is non-zero if SPEC is valid. */
7449
7450static int
7451parse_image_spec (spec, keywords, nkeywords, type)
7452 Lisp_Object spec;
7453 struct image_keyword *keywords;
7454 int nkeywords;
7455 Lisp_Object type;
7456{
7457 int i;
7458 Lisp_Object plist;
7459
7460 if (!CONSP (spec) || !EQ (XCAR (spec), Qimage))
7461 return 0;
7462
7463 plist = XCDR (spec);
7464 while (CONSP (plist))
7465 {
7466 Lisp_Object key, value;
7467
7468 /* First element of a pair must be a symbol. */
7469 key = XCAR (plist);
7470 plist = XCDR (plist);
7471 if (!SYMBOLP (key))
7472 return 0;
7473
7474 /* There must follow a value. */
7475 if (!CONSP (plist))
7476 return 0;
7477 value = XCAR (plist);
7478 plist = XCDR (plist);
7479
7480 /* Find key in KEYWORDS. Error if not found. */
7481 for (i = 0; i < nkeywords; ++i)
7482 if (strcmp (keywords[i].name, XSYMBOL (key)->name->data) == 0)
7483 break;
7484
7485 if (i == nkeywords)
7486 continue;
7487
7488 /* Record that we recognized the keyword. If a keywords
7489 was found more than once, it's an error. */
7490 keywords[i].value = value;
7491 ++keywords[i].count;
7492
7493 if (keywords[i].count > 1)
7494 return 0;
7495
7496 /* Check type of value against allowed type. */
7497 switch (keywords[i].type)
7498 {
7499 case IMAGE_STRING_VALUE:
7500 if (!STRINGP (value))
7501 return 0;
7502 break;
7503
7504 case IMAGE_SYMBOL_VALUE:
7505 if (!SYMBOLP (value))
7506 return 0;
7507 break;
7508
7509 case IMAGE_POSITIVE_INTEGER_VALUE:
7510 if (!INTEGERP (value) || XINT (value) <= 0)
7511 return 0;
7512 break;
7513
dfff8a69
JR
7514 case IMAGE_ASCENT_VALUE:
7515 if (SYMBOLP (value) && EQ (value, Qcenter))
7516 break;
7517 else if (INTEGERP (value)
7518 && XINT (value) >= 0
7519 && XINT (value) <= 100)
7520 break;
7521 return 0;
7522
6fc2811b
JR
7523 case IMAGE_NON_NEGATIVE_INTEGER_VALUE:
7524 if (!INTEGERP (value) || XINT (value) < 0)
7525 return 0;
7526 break;
7527
7528 case IMAGE_DONT_CHECK_VALUE_TYPE:
7529 break;
7530
7531 case IMAGE_FUNCTION_VALUE:
7532 value = indirect_function (value);
7533 if (SUBRP (value)
7534 || COMPILEDP (value)
7535 || (CONSP (value) && EQ (XCAR (value), Qlambda)))
7536 break;
7537 return 0;
7538
7539 case IMAGE_NUMBER_VALUE:
7540 if (!INTEGERP (value) && !FLOATP (value))
7541 return 0;
7542 break;
7543
7544 case IMAGE_INTEGER_VALUE:
7545 if (!INTEGERP (value))
7546 return 0;
7547 break;
7548
7549 case IMAGE_BOOL_VALUE:
7550 if (!NILP (value) && !EQ (value, Qt))
7551 return 0;
7552 break;
7553
7554 default:
7555 abort ();
7556 break;
7557 }
7558
7559 if (EQ (key, QCtype) && !EQ (type, value))
7560 return 0;
7561 }
7562
7563 /* Check that all mandatory fields are present. */
7564 for (i = 0; i < nkeywords; ++i)
7565 if (keywords[i].mandatory_p && keywords[i].count == 0)
7566 return 0;
7567
7568 return NILP (plist);
7569}
7570
7571
7572/* Return the value of KEY in image specification SPEC. Value is nil
7573 if KEY is not present in SPEC. if FOUND is not null, set *FOUND
7574 to 1 if KEY was found in SPEC, set it to 0 otherwise. */
7575
7576static Lisp_Object
7577image_spec_value (spec, key, found)
7578 Lisp_Object spec, key;
7579 int *found;
7580{
7581 Lisp_Object tail;
7582
7583 xassert (valid_image_p (spec));
7584
7585 for (tail = XCDR (spec);
7586 CONSP (tail) && CONSP (XCDR (tail));
7587 tail = XCDR (XCDR (tail)))
7588 {
7589 if (EQ (XCAR (tail), key))
7590 {
7591 if (found)
7592 *found = 1;
7593 return XCAR (XCDR (tail));
7594 }
7595 }
7596
7597 if (found)
7598 *found = 0;
7599 return Qnil;
7600}
7601
7602
7603
7604\f
7605/***********************************************************************
7606 Image type independent image structures
7607 ***********************************************************************/
7608
7609static struct image *make_image P_ ((Lisp_Object spec, unsigned hash));
7610static void free_image P_ ((struct frame *f, struct image *img));
7611
7612
7613/* Allocate and return a new image structure for image specification
7614 SPEC. SPEC has a hash value of HASH. */
7615
7616static struct image *
7617make_image (spec, hash)
7618 Lisp_Object spec;
7619 unsigned hash;
7620{
7621 struct image *img = (struct image *) xmalloc (sizeof *img);
7622
7623 xassert (valid_image_p (spec));
7624 bzero (img, sizeof *img);
7625 img->type = lookup_image_type (image_spec_value (spec, QCtype, NULL));
7626 xassert (img->type != NULL);
7627 img->spec = spec;
7628 img->data.lisp_val = Qnil;
7629 img->ascent = DEFAULT_IMAGE_ASCENT;
7630 img->hash = hash;
7631 return img;
7632}
7633
7634
7635/* Free image IMG which was used on frame F, including its resources. */
7636
7637static void
7638free_image (f, img)
7639 struct frame *f;
7640 struct image *img;
7641{
7642 if (img)
7643 {
7644 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
7645
7646 /* Remove IMG from the hash table of its cache. */
7647 if (img->prev)
7648 img->prev->next = img->next;
7649 else
7650 c->buckets[img->hash % IMAGE_CACHE_BUCKETS_SIZE] = img->next;
7651
7652 if (img->next)
7653 img->next->prev = img->prev;
7654
7655 c->images[img->id] = NULL;
7656
7657 /* Free resources, then free IMG. */
7658 img->type->free (f, img);
7659 xfree (img);
7660 }
7661}
7662
7663
7664/* Prepare image IMG for display on frame F. Must be called before
7665 drawing an image. */
7666
7667void
7668prepare_image_for_display (f, img)
7669 struct frame *f;
7670 struct image *img;
7671{
7672 EMACS_TIME t;
7673
7674 /* We're about to display IMG, so set its timestamp to `now'. */
7675 EMACS_GET_TIME (t);
7676 img->timestamp = EMACS_SECS (t);
7677
7678 /* If IMG doesn't have a pixmap yet, load it now, using the image
7679 type dependent loader function. */
7680 if (img->pixmap == 0 && !img->load_failed_p)
7681 img->load_failed_p = img->type->load (f, img) == 0;
7682}
7683
7684
dfff8a69
JR
7685/* Value is the number of pixels for the ascent of image IMG when
7686 drawn in face FACE. */
7687
7688int
7689image_ascent (img, face)
7690 struct image *img;
7691 struct face *face;
7692{
7693 int height = img->height + img->margin;
7694 int ascent;
7695
7696 if (img->ascent == CENTERED_IMAGE_ASCENT)
7697 {
7698 if (face->font)
7699 ascent = height / 2 - (FONT_DESCENT(face->font)
7700 - FONT_BASE(face->font)) / 2;
7701 else
7702 ascent = height / 2;
7703 }
7704 else
7705 ascent = height * img->ascent / 100.0;
7706
7707 return ascent;
7708}
7709
7710
6fc2811b
JR
7711\f
7712/***********************************************************************
7713 Helper functions for X image types
7714 ***********************************************************************/
7715
7716static void x_clear_image P_ ((struct frame *f, struct image *img));
7717static unsigned long x_alloc_image_color P_ ((struct frame *f,
7718 struct image *img,
7719 Lisp_Object color_name,
7720 unsigned long dflt));
7721
7722/* Free X resources of image IMG which is used on frame F. */
7723
7724static void
7725x_clear_image (f, img)
7726 struct frame *f;
7727 struct image *img;
7728{
7729#if 0 /* NTEMACS_TODO: W32 image support */
7730
7731 if (img->pixmap)
7732 {
7733 BLOCK_INPUT;
7734 XFreePixmap (NULL, img->pixmap);
7735 img->pixmap = 0;
7736 UNBLOCK_INPUT;
7737 }
7738
7739 if (img->ncolors)
7740 {
7741 int class = FRAME_W32_DISPLAY_INFO (f)->visual->class;
7742
7743 /* If display has an immutable color map, freeing colors is not
7744 necessary and some servers don't allow it. So don't do it. */
7745 if (class != StaticColor
7746 && class != StaticGray
7747 && class != TrueColor)
7748 {
7749 Colormap cmap;
7750 BLOCK_INPUT;
7751 cmap = DefaultColormapOfScreen (FRAME_W32_DISPLAY_INFO (f)->screen);
7752 XFreeColors (FRAME_W32_DISPLAY (f), cmap, img->colors,
7753 img->ncolors, 0);
7754 UNBLOCK_INPUT;
7755 }
7756
7757 xfree (img->colors);
7758 img->colors = NULL;
7759 img->ncolors = 0;
7760 }
7761#endif
7762}
7763
7764
7765/* Allocate color COLOR_NAME for image IMG on frame F. If color
7766 cannot be allocated, use DFLT. Add a newly allocated color to
7767 IMG->colors, so that it can be freed again. Value is the pixel
7768 color. */
7769
7770static unsigned long
7771x_alloc_image_color (f, img, color_name, dflt)
7772 struct frame *f;
7773 struct image *img;
7774 Lisp_Object color_name;
7775 unsigned long dflt;
7776{
7777#if 0 /* NTEMACS_TODO: allocing colors. */
7778 XColor color;
7779 unsigned long result;
7780
7781 xassert (STRINGP (color_name));
7782
7783 if (w32_defined_color (f, XSTRING (color_name)->data, &color, 1))
7784 {
7785 /* This isn't called frequently so we get away with simply
7786 reallocating the color vector to the needed size, here. */
7787 ++img->ncolors;
7788 img->colors =
7789 (unsigned long *) xrealloc (img->colors,
7790 img->ncolors * sizeof *img->colors);
7791 img->colors[img->ncolors - 1] = color.pixel;
7792 result = color.pixel;
7793 }
7794 else
7795 result = dflt;
7796 return result;
7797#endif
7798 return 0;
7799}
7800
7801
7802\f
7803/***********************************************************************
7804 Image Cache
7805 ***********************************************************************/
7806
7807static void cache_image P_ ((struct frame *f, struct image *img));
7808
7809
7810/* Return a new, initialized image cache that is allocated from the
7811 heap. Call free_image_cache to free an image cache. */
7812
7813struct image_cache *
7814make_image_cache ()
7815{
7816 struct image_cache *c = (struct image_cache *) xmalloc (sizeof *c);
7817 int size;
7818
7819 bzero (c, sizeof *c);
7820 c->size = 50;
7821 c->images = (struct image **) xmalloc (c->size * sizeof *c->images);
7822 size = IMAGE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
7823 c->buckets = (struct image **) xmalloc (size);
7824 bzero (c->buckets, size);
7825 return c;
7826}
7827
7828
7829/* Free image cache of frame F. Be aware that X frames share images
7830 caches. */
7831
7832void
7833free_image_cache (f)
7834 struct frame *f;
7835{
7836 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
7837 if (c)
7838 {
7839 int i;
7840
7841 /* Cache should not be referenced by any frame when freed. */
7842 xassert (c->refcount == 0);
7843
7844 for (i = 0; i < c->used; ++i)
7845 free_image (f, c->images[i]);
7846 xfree (c->images);
7847 xfree (c);
7848 xfree (c->buckets);
7849 FRAME_X_IMAGE_CACHE (f) = NULL;
7850 }
7851}
7852
7853
7854/* Clear image cache of frame F. FORCE_P non-zero means free all
7855 images. FORCE_P zero means clear only images that haven't been
7856 displayed for some time. Should be called from time to time to
dfff8a69
JR
7857 reduce the number of loaded images. If image-eviction-seconds is
7858 non-nil, this frees images in the cache which weren't displayed for
6fc2811b
JR
7859 at least that many seconds. */
7860
7861void
7862clear_image_cache (f, force_p)
7863 struct frame *f;
7864 int force_p;
7865{
7866 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
7867
7868 if (c && INTEGERP (Vimage_cache_eviction_delay))
7869 {
7870 EMACS_TIME t;
7871 unsigned long old;
7872 int i, any_freed_p = 0;
7873
7874 EMACS_GET_TIME (t);
7875 old = EMACS_SECS (t) - XFASTINT (Vimage_cache_eviction_delay);
7876
7877 for (i = 0; i < c->used; ++i)
7878 {
7879 struct image *img = c->images[i];
7880 if (img != NULL
7881 && (force_p
7882 || (img->timestamp > old)))
7883 {
7884 free_image (f, img);
7885 any_freed_p = 1;
7886 }
7887 }
7888
7889 /* We may be clearing the image cache because, for example,
7890 Emacs was iconified for a longer period of time. In that
7891 case, current matrices may still contain references to
7892 images freed above. So, clear these matrices. */
7893 if (any_freed_p)
7894 {
7895 clear_current_matrices (f);
7896 ++windows_or_buffers_changed;
7897 }
7898 }
7899}
7900
7901
7902DEFUN ("clear-image-cache", Fclear_image_cache, Sclear_image_cache,
7903 0, 1, 0,
7904 "Clear the image cache of FRAME.\n\
7905FRAME nil or omitted means use the selected frame.\n\
7906FRAME t means clear the image caches of all frames.")
7907 (frame)
7908 Lisp_Object frame;
7909{
7910 if (EQ (frame, Qt))
7911 {
7912 Lisp_Object tail;
7913
7914 FOR_EACH_FRAME (tail, frame)
7915 if (FRAME_W32_P (XFRAME (frame)))
7916 clear_image_cache (XFRAME (frame), 1);
7917 }
7918 else
7919 clear_image_cache (check_x_frame (frame), 1);
7920
7921 return Qnil;
7922}
7923
7924
7925/* Return the id of image with Lisp specification SPEC on frame F.
7926 SPEC must be a valid Lisp image specification (see valid_image_p). */
7927
7928int
7929lookup_image (f, spec)
7930 struct frame *f;
7931 Lisp_Object spec;
7932{
7933 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
7934 struct image *img;
7935 int i;
7936 unsigned hash;
7937 struct gcpro gcpro1;
7938 EMACS_TIME now;
7939
7940 /* F must be a window-system frame, and SPEC must be a valid image
7941 specification. */
7942 xassert (FRAME_WINDOW_P (f));
7943 xassert (valid_image_p (spec));
7944
7945 GCPRO1 (spec);
7946
7947 /* Look up SPEC in the hash table of the image cache. */
7948 hash = sxhash (spec, 0);
7949 i = hash % IMAGE_CACHE_BUCKETS_SIZE;
7950
7951 for (img = c->buckets[i]; img; img = img->next)
7952 if (img->hash == hash && !NILP (Fequal (img->spec, spec)))
7953 break;
7954
7955 /* If not found, create a new image and cache it. */
7956 if (img == NULL)
7957 {
7958 img = make_image (spec, hash);
7959 cache_image (f, img);
7960 img->load_failed_p = img->type->load (f, img) == 0;
7961 xassert (!interrupt_input_blocked);
7962
7963 /* If we can't load the image, and we don't have a width and
7964 height, use some arbitrary width and height so that we can
7965 draw a rectangle for it. */
7966 if (img->load_failed_p)
7967 {
7968 Lisp_Object value;
7969
7970 value = image_spec_value (spec, QCwidth, NULL);
7971 img->width = (INTEGERP (value)
7972 ? XFASTINT (value) : DEFAULT_IMAGE_WIDTH);
7973 value = image_spec_value (spec, QCheight, NULL);
7974 img->height = (INTEGERP (value)
7975 ? XFASTINT (value) : DEFAULT_IMAGE_HEIGHT);
7976 }
7977 else
7978 {
7979 /* Handle image type independent image attributes
7980 `:ascent PERCENT', `:margin MARGIN', `:relief RELIEF'. */
7981 Lisp_Object ascent, margin, relief, algorithm, heuristic_mask;
7982 Lisp_Object file;
7983
7984 ascent = image_spec_value (spec, QCascent, NULL);
7985 if (INTEGERP (ascent))
7986 img->ascent = XFASTINT (ascent);
dfff8a69
JR
7987 else if (EQ (ascent, Qcenter))
7988 img->ascent = CENTERED_IMAGE_ASCENT;
7989
6fc2811b
JR
7990 margin = image_spec_value (spec, QCmargin, NULL);
7991 if (INTEGERP (margin) && XINT (margin) >= 0)
7992 img->margin = XFASTINT (margin);
7993
7994 relief = image_spec_value (spec, QCrelief, NULL);
7995 if (INTEGERP (relief))
7996 {
7997 img->relief = XINT (relief);
7998 img->margin += abs (img->relief);
7999 }
8000
8001 /* Should we apply a Laplace edge-detection algorithm? */
8002 algorithm = image_spec_value (spec, QCalgorithm, NULL);
8003 if (img->pixmap && EQ (algorithm, Qlaplace))
8004 x_laplace (f, img);
8005
8006 /* Should we built a mask heuristically? */
8007 heuristic_mask = image_spec_value (spec, QCheuristic_mask, NULL);
8008 if (img->pixmap && !img->mask && !NILP (heuristic_mask))
8009 x_build_heuristic_mask (f, img, heuristic_mask);
8010 }
8011 }
8012
8013 /* We're using IMG, so set its timestamp to `now'. */
8014 EMACS_GET_TIME (now);
8015 img->timestamp = EMACS_SECS (now);
8016
8017 UNGCPRO;
8018
8019 /* Value is the image id. */
8020 return img->id;
8021}
8022
8023
8024/* Cache image IMG in the image cache of frame F. */
8025
8026static void
8027cache_image (f, img)
8028 struct frame *f;
8029 struct image *img;
8030{
8031 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
8032 int i;
8033
8034 /* Find a free slot in c->images. */
8035 for (i = 0; i < c->used; ++i)
8036 if (c->images[i] == NULL)
8037 break;
8038
8039 /* If no free slot found, maybe enlarge c->images. */
8040 if (i == c->used && c->used == c->size)
8041 {
8042 c->size *= 2;
8043 c->images = (struct image **) xrealloc (c->images,
8044 c->size * sizeof *c->images);
8045 }
8046
8047 /* Add IMG to c->images, and assign IMG an id. */
8048 c->images[i] = img;
8049 img->id = i;
8050 if (i == c->used)
8051 ++c->used;
8052
8053 /* Add IMG to the cache's hash table. */
8054 i = img->hash % IMAGE_CACHE_BUCKETS_SIZE;
8055 img->next = c->buckets[i];
8056 if (img->next)
8057 img->next->prev = img;
8058 img->prev = NULL;
8059 c->buckets[i] = img;
8060}
8061
8062
8063/* Call FN on every image in the image cache of frame F. Used to mark
8064 Lisp Objects in the image cache. */
8065
8066void
8067forall_images_in_image_cache (f, fn)
8068 struct frame *f;
8069 void (*fn) P_ ((struct image *img));
8070{
8071 if (FRAME_LIVE_P (f) && FRAME_W32_P (f))
8072 {
8073 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
8074 if (c)
8075 {
8076 int i;
8077 for (i = 0; i < c->used; ++i)
8078 if (c->images[i])
8079 fn (c->images[i]);
8080 }
8081 }
8082}
8083
8084
8085\f
8086/***********************************************************************
8087 W32 support code
8088 ***********************************************************************/
8089
8090#if 0 /* NTEMACS_TODO: W32 specific image code. */
8091
8092static int x_create_x_image_and_pixmap P_ ((struct frame *, int, int, int,
8093 XImage **, Pixmap *));
8094static void x_destroy_x_image P_ ((XImage *));
8095static void x_put_x_image P_ ((struct frame *, XImage *, Pixmap, int, int));
8096
8097
8098/* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on
8099 frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created.
8100 Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated
8101 via xmalloc. Print error messages via image_error if an error
8102 occurs. Value is non-zero if successful. */
8103
8104static int
8105x_create_x_image_and_pixmap (f, width, height, depth, ximg, pixmap)
8106 struct frame *f;
8107 int width, height, depth;
8108 XImage **ximg;
8109 Pixmap *pixmap;
8110{
8111#if 0 /* NTEMACS_TODO: Image support for W32 */
8112 Display *display = FRAME_W32_DISPLAY (f);
8113 Screen *screen = FRAME_X_SCREEN (f);
8114 Window window = FRAME_W32_WINDOW (f);
8115
8116 xassert (interrupt_input_blocked);
8117
8118 if (depth <= 0)
8119 depth = DefaultDepthOfScreen (screen);
8120 *ximg = XCreateImage (display, DefaultVisualOfScreen (screen),
8121 depth, ZPixmap, 0, NULL, width, height,
8122 depth > 16 ? 32 : depth > 8 ? 16 : 8, 0);
8123 if (*ximg == NULL)
8124 {
8125 image_error ("Unable to allocate X image", Qnil, Qnil);
8126 return 0;
8127 }
8128
8129 /* Allocate image raster. */
8130 (*ximg)->data = (char *) xmalloc ((*ximg)->bytes_per_line * height);
8131
8132 /* Allocate a pixmap of the same size. */
8133 *pixmap = XCreatePixmap (display, window, width, height, depth);
8134 if (*pixmap == 0)
8135 {
8136 x_destroy_x_image (*ximg);
8137 *ximg = NULL;
8138 image_error ("Unable to create X pixmap", Qnil, Qnil);
8139 return 0;
8140 }
8141#endif
8142 return 1;
8143}
8144
8145
8146/* Destroy XImage XIMG. Free XIMG->data. */
8147
8148static void
8149x_destroy_x_image (ximg)
8150 XImage *ximg;
8151{
8152 xassert (interrupt_input_blocked);
8153 if (ximg)
8154 {
8155 xfree (ximg->data);
8156 ximg->data = NULL;
8157 XDestroyImage (ximg);
8158 }
8159}
8160
8161
8162/* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT
8163 are width and height of both the image and pixmap. */
8164
8165static void
8166x_put_x_image (f, ximg, pixmap, width, height)
8167 struct frame *f;
8168 XImage *ximg;
8169 Pixmap pixmap;
8170{
8171 GC gc;
8172
8173 xassert (interrupt_input_blocked);
8174 gc = XCreateGC (NULL, pixmap, 0, NULL);
8175 XPutImage (NULL, pixmap, gc, ximg, 0, 0, 0, 0, width, height);
8176 XFreeGC (NULL, gc);
8177}
8178
8179#endif
8180
8181\f
8182/***********************************************************************
8183 Searching files
8184 ***********************************************************************/
8185
8186static Lisp_Object x_find_image_file P_ ((Lisp_Object));
8187
8188/* Find image file FILE. Look in data-directory, then
8189 x-bitmap-file-path. Value is the full name of the file found, or
8190 nil if not found. */
8191
8192static Lisp_Object
8193x_find_image_file (file)
8194 Lisp_Object file;
8195{
8196 Lisp_Object file_found, search_path;
8197 struct gcpro gcpro1, gcpro2;
8198 int fd;
8199
8200 file_found = Qnil;
8201 search_path = Fcons (Vdata_directory, Vx_bitmap_file_path);
8202 GCPRO2 (file_found, search_path);
8203
8204 /* Try to find FILE in data-directory, then x-bitmap-file-path. */
8205 fd = openp (search_path, file, "", &file_found, 0);
8206
8207 if (fd < 0)
8208 file_found = Qnil;
8209 else
8210 close (fd);
8211
8212 UNGCPRO;
8213 return file_found;
8214}
8215
8216
8217\f
8218/***********************************************************************
8219 XBM images
8220 ***********************************************************************/
8221
8222static int xbm_load P_ ((struct frame *f, struct image *img));
8223static int xbm_load_image_from_file P_ ((struct frame *f, struct image *img,
8224 Lisp_Object file));
8225static int xbm_image_p P_ ((Lisp_Object object));
8226static int xbm_read_bitmap_file_data P_ ((char *, int *, int *,
8227 unsigned char **));
8228
8229
8230/* Indices of image specification fields in xbm_format, below. */
8231
8232enum xbm_keyword_index
8233{
8234 XBM_TYPE,
8235 XBM_FILE,
8236 XBM_WIDTH,
8237 XBM_HEIGHT,
8238 XBM_DATA,
8239 XBM_FOREGROUND,
8240 XBM_BACKGROUND,
8241 XBM_ASCENT,
8242 XBM_MARGIN,
8243 XBM_RELIEF,
8244 XBM_ALGORITHM,
8245 XBM_HEURISTIC_MASK,
8246 XBM_LAST
8247};
8248
8249/* Vector of image_keyword structures describing the format
8250 of valid XBM image specifications. */
8251
8252static struct image_keyword xbm_format[XBM_LAST] =
8253{
8254 {":type", IMAGE_SYMBOL_VALUE, 1},
8255 {":file", IMAGE_STRING_VALUE, 0},
8256 {":width", IMAGE_POSITIVE_INTEGER_VALUE, 0},
8257 {":height", IMAGE_POSITIVE_INTEGER_VALUE, 0},
8258 {":data", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8259 {":foreground", IMAGE_STRING_VALUE, 0},
8260 {":background", IMAGE_STRING_VALUE, 0},
8261 {":ascent", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
8262 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
8263 {":relief", IMAGE_INTEGER_VALUE, 0},
8264 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8265 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
8266};
8267
8268/* Structure describing the image type XBM. */
8269
8270static struct image_type xbm_type =
8271{
8272 &Qxbm,
8273 xbm_image_p,
8274 xbm_load,
8275 x_clear_image,
8276 NULL
8277};
8278
8279/* Tokens returned from xbm_scan. */
8280
8281enum xbm_token
8282{
8283 XBM_TK_IDENT = 256,
8284 XBM_TK_NUMBER
8285};
8286
8287
8288/* Return non-zero if OBJECT is a valid XBM-type image specification.
8289 A valid specification is a list starting with the symbol `image'
8290 The rest of the list is a property list which must contain an
8291 entry `:type xbm..
8292
8293 If the specification specifies a file to load, it must contain
8294 an entry `:file FILENAME' where FILENAME is a string.
8295
8296 If the specification is for a bitmap loaded from memory it must
8297 contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where
8298 WIDTH and HEIGHT are integers > 0. DATA may be:
8299
8300 1. a string large enough to hold the bitmap data, i.e. it must
8301 have a size >= (WIDTH + 7) / 8 * HEIGHT
8302
8303 2. a bool-vector of size >= WIDTH * HEIGHT
8304
8305 3. a vector of strings or bool-vectors, one for each line of the
8306 bitmap.
8307
8308 Both the file and data forms may contain the additional entries
8309 `:background COLOR' and `:foreground COLOR'. If not present,
8310 foreground and background of the frame on which the image is
8311 displayed, is used. */
8312
8313static int
8314xbm_image_p (object)
8315 Lisp_Object object;
8316{
8317 struct image_keyword kw[XBM_LAST];
8318
8319 bcopy (xbm_format, kw, sizeof kw);
8320 if (!parse_image_spec (object, kw, XBM_LAST, Qxbm))
8321 return 0;
8322
8323 xassert (EQ (kw[XBM_TYPE].value, Qxbm));
8324
8325 if (kw[XBM_FILE].count)
8326 {
8327 if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_DATA].count)
8328 return 0;
8329 }
8330 else
8331 {
8332 Lisp_Object data;
8333 int width, height;
8334
8335 /* Entries for `:width', `:height' and `:data' must be present. */
8336 if (!kw[XBM_WIDTH].count
8337 || !kw[XBM_HEIGHT].count
8338 || !kw[XBM_DATA].count)
8339 return 0;
8340
8341 data = kw[XBM_DATA].value;
8342 width = XFASTINT (kw[XBM_WIDTH].value);
8343 height = XFASTINT (kw[XBM_HEIGHT].value);
8344
8345 /* Check type of data, and width and height against contents of
8346 data. */
8347 if (VECTORP (data))
8348 {
8349 int i;
8350
8351 /* Number of elements of the vector must be >= height. */
8352 if (XVECTOR (data)->size < height)
8353 return 0;
8354
8355 /* Each string or bool-vector in data must be large enough
8356 for one line of the image. */
8357 for (i = 0; i < height; ++i)
8358 {
8359 Lisp_Object elt = XVECTOR (data)->contents[i];
8360
8361 if (STRINGP (elt))
8362 {
8363 if (XSTRING (elt)->size
8364 < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR)
8365 return 0;
8366 }
8367 else if (BOOL_VECTOR_P (elt))
8368 {
8369 if (XBOOL_VECTOR (elt)->size < width)
8370 return 0;
8371 }
8372 else
8373 return 0;
8374 }
8375 }
8376 else if (STRINGP (data))
8377 {
8378 if (XSTRING (data)->size
8379 < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR * height)
8380 return 0;
8381 }
8382 else if (BOOL_VECTOR_P (data))
8383 {
8384 if (XBOOL_VECTOR (data)->size < width * height)
8385 return 0;
8386 }
8387 else
8388 return 0;
8389 }
8390
8391 /* Baseline must be a value between 0 and 100 (a percentage). */
8392 if (kw[XBM_ASCENT].count
8393 && XFASTINT (kw[XBM_ASCENT].value) > 100)
8394 return 0;
8395
8396 return 1;
8397}
8398
8399
8400/* Scan a bitmap file. FP is the stream to read from. Value is
8401 either an enumerator from enum xbm_token, or a character for a
8402 single-character token, or 0 at end of file. If scanning an
8403 identifier, store the lexeme of the identifier in SVAL. If
8404 scanning a number, store its value in *IVAL. */
8405
8406static int
8407xbm_scan (fp, sval, ival)
8408 FILE *fp;
8409 char *sval;
8410 int *ival;
8411{
8412 int c;
8413
8414 /* Skip white space. */
8415 while ((c = fgetc (fp)) != EOF && isspace (c))
8416 ;
8417
8418 if (c == EOF)
8419 c = 0;
8420 else if (isdigit (c))
8421 {
8422 int value = 0, digit;
8423
8424 if (c == '0')
8425 {
8426 c = fgetc (fp);
8427 if (c == 'x' || c == 'X')
8428 {
8429 while ((c = fgetc (fp)) != EOF)
8430 {
8431 if (isdigit (c))
8432 digit = c - '0';
8433 else if (c >= 'a' && c <= 'f')
8434 digit = c - 'a' + 10;
8435 else if (c >= 'A' && c <= 'F')
8436 digit = c - 'A' + 10;
8437 else
8438 break;
8439 value = 16 * value + digit;
8440 }
8441 }
8442 else if (isdigit (c))
8443 {
8444 value = c - '0';
8445 while ((c = fgetc (fp)) != EOF
8446 && isdigit (c))
8447 value = 8 * value + c - '0';
8448 }
8449 }
8450 else
8451 {
8452 value = c - '0';
8453 while ((c = fgetc (fp)) != EOF
8454 && isdigit (c))
8455 value = 10 * value + c - '0';
8456 }
8457
8458 if (c != EOF)
8459 ungetc (c, fp);
8460 *ival = value;
8461 c = XBM_TK_NUMBER;
8462 }
8463 else if (isalpha (c) || c == '_')
8464 {
8465 *sval++ = c;
8466 while ((c = fgetc (fp)) != EOF
8467 && (isalnum (c) || c == '_'))
8468 *sval++ = c;
8469 *sval = 0;
8470 if (c != EOF)
8471 ungetc (c, fp);
8472 c = XBM_TK_IDENT;
8473 }
8474
8475 return c;
8476}
8477
8478
8479/* Replacement for XReadBitmapFileData which isn't available under old
8480 X versions. FILE is the name of the bitmap file to read. Set
8481 *WIDTH and *HEIGHT to the width and height of the image. Return in
8482 *DATA the bitmap data allocated with xmalloc. Value is non-zero if
8483 successful. */
8484
8485static int
8486xbm_read_bitmap_file_data (file, width, height, data)
8487 char *file;
8488 int *width, *height;
8489 unsigned char **data;
8490{
8491 FILE *fp;
8492 char buffer[BUFSIZ];
8493 int padding_p = 0;
8494 int v10 = 0;
8495 int bytes_per_line, i, nbytes;
8496 unsigned char *p;
8497 int value;
8498 int LA1;
8499
8500#define match() \
8501 LA1 = xbm_scan (fp, buffer, &value)
8502
8503#define expect(TOKEN) \
8504 if (LA1 != (TOKEN)) \
8505 goto failure; \
8506 else \
8507 match ()
8508
8509#define expect_ident(IDENT) \
8510 if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \
8511 match (); \
8512 else \
8513 goto failure
8514
8515 fp = fopen (file, "r");
8516 if (fp == NULL)
8517 return 0;
8518
8519 *width = *height = -1;
8520 *data = NULL;
8521 LA1 = xbm_scan (fp, buffer, &value);
8522
8523 /* Parse defines for width, height and hot-spots. */
8524 while (LA1 == '#')
8525 {
8526 match ();
8527 expect_ident ("define");
8528 expect (XBM_TK_IDENT);
8529
8530 if (LA1 == XBM_TK_NUMBER);
8531 {
8532 char *p = strrchr (buffer, '_');
8533 p = p ? p + 1 : buffer;
8534 if (strcmp (p, "width") == 0)
8535 *width = value;
8536 else if (strcmp (p, "height") == 0)
8537 *height = value;
8538 }
8539 expect (XBM_TK_NUMBER);
8540 }
8541
8542 if (*width < 0 || *height < 0)
8543 goto failure;
8544
8545 /* Parse bits. Must start with `static'. */
8546 expect_ident ("static");
8547 if (LA1 == XBM_TK_IDENT)
8548 {
8549 if (strcmp (buffer, "unsigned") == 0)
8550 {
8551 match ();
8552 expect_ident ("char");
8553 }
8554 else if (strcmp (buffer, "short") == 0)
8555 {
8556 match ();
8557 v10 = 1;
8558 if (*width % 16 && *width % 16 < 9)
8559 padding_p = 1;
8560 }
8561 else if (strcmp (buffer, "char") == 0)
8562 match ();
8563 else
8564 goto failure;
8565 }
8566 else
8567 goto failure;
8568
8569 expect (XBM_TK_IDENT);
8570 expect ('[');
8571 expect (']');
8572 expect ('=');
8573 expect ('{');
8574
8575 bytes_per_line = (*width + 7) / 8 + padding_p;
8576 nbytes = bytes_per_line * *height;
8577 p = *data = (char *) xmalloc (nbytes);
8578
8579 if (v10)
8580 {
8581
8582 for (i = 0; i < nbytes; i += 2)
8583 {
8584 int val = value;
8585 expect (XBM_TK_NUMBER);
8586
8587 *p++ = val;
8588 if (!padding_p || ((i + 2) % bytes_per_line))
8589 *p++ = value >> 8;
8590
8591 if (LA1 == ',' || LA1 == '}')
8592 match ();
8593 else
8594 goto failure;
8595 }
8596 }
8597 else
8598 {
8599 for (i = 0; i < nbytes; ++i)
8600 {
8601 int val = value;
8602 expect (XBM_TK_NUMBER);
8603
8604 *p++ = val;
8605
8606 if (LA1 == ',' || LA1 == '}')
8607 match ();
8608 else
8609 goto failure;
8610 }
8611 }
8612
8613 fclose (fp);
8614 return 1;
8615
8616 failure:
8617
8618 fclose (fp);
8619 if (*data)
8620 {
8621 xfree (*data);
8622 *data = NULL;
8623 }
8624 return 0;
8625
8626#undef match
8627#undef expect
8628#undef expect_ident
8629}
8630
8631
8632/* Load XBM image IMG which will be displayed on frame F from file
8633 SPECIFIED_FILE. Value is non-zero if successful. */
8634
8635static int
8636xbm_load_image_from_file (f, img, specified_file)
8637 struct frame *f;
8638 struct image *img;
8639 Lisp_Object specified_file;
8640{
8641 int rc;
8642 unsigned char *data;
8643 int success_p = 0;
8644 Lisp_Object file;
8645 struct gcpro gcpro1;
8646
8647 xassert (STRINGP (specified_file));
8648 file = Qnil;
8649 GCPRO1 (file);
8650
8651 file = x_find_image_file (specified_file);
8652 if (!STRINGP (file))
8653 {
8654 image_error ("Cannot find image file `%s'", specified_file, Qnil);
8655 UNGCPRO;
8656 return 0;
8657 }
8658
8659 rc = xbm_read_bitmap_file_data (XSTRING (file)->data, &img->width,
8660 &img->height, &data);
8661 if (rc)
8662 {
8663 int depth = one_w32_display_info.n_cbits;
8664 unsigned long foreground = FRAME_FOREGROUND_PIXEL (f);
8665 unsigned long background = FRAME_BACKGROUND_PIXEL (f);
8666 Lisp_Object value;
8667
8668 xassert (img->width > 0 && img->height > 0);
8669
8670 /* Get foreground and background colors, maybe allocate colors. */
8671 value = image_spec_value (img->spec, QCforeground, NULL);
8672 if (!NILP (value))
8673 foreground = x_alloc_image_color (f, img, value, foreground);
8674
8675 value = image_spec_value (img->spec, QCbackground, NULL);
8676 if (!NILP (value))
8677 background = x_alloc_image_color (f, img, value, background);
8678
8679#if 0 /* NTEMACS_TODO : Port image display to W32 */
8680 BLOCK_INPUT;
8681 img->pixmap
8682 = XCreatePixmapFromBitmapData (FRAME_W32_DISPLAY (f),
8683 FRAME_W32_WINDOW (f),
8684 data,
8685 img->width, img->height,
8686 foreground, background,
8687 depth);
8688 xfree (data);
8689
8690 if (img->pixmap == 0)
8691 {
8692 x_clear_image (f, img);
8693 image_error ("Unable to create X pixmap for `%s'", file, Qnil);
8694 }
8695 else
8696 success_p = 1;
8697
8698 UNBLOCK_INPUT;
8699#endif
8700 }
8701 else
8702 image_error ("Error loading XBM image `%s'", img->spec, Qnil);
8703
8704 UNGCPRO;
8705 return success_p;
8706}
8707
8708
8709/* Fill image IMG which is used on frame F with pixmap data. Value is
8710 non-zero if successful. */
8711
8712static int
8713xbm_load (f, img)
8714 struct frame *f;
8715 struct image *img;
8716{
8717 int success_p = 0;
8718 Lisp_Object file_name;
8719
8720 xassert (xbm_image_p (img->spec));
8721
8722 /* If IMG->spec specifies a file name, create a non-file spec from it. */
8723 file_name = image_spec_value (img->spec, QCfile, NULL);
8724 if (STRINGP (file_name))
8725 success_p = xbm_load_image_from_file (f, img, file_name);
8726 else
8727 {
8728 struct image_keyword fmt[XBM_LAST];
8729 Lisp_Object data;
8730 int depth;
8731 unsigned long foreground = FRAME_FOREGROUND_PIXEL (f);
8732 unsigned long background = FRAME_BACKGROUND_PIXEL (f);
8733 char *bits;
8734 int parsed_p;
8735
8736 /* Parse the list specification. */
8737 bcopy (xbm_format, fmt, sizeof fmt);
8738 parsed_p = parse_image_spec (img->spec, fmt, XBM_LAST, Qxbm);
8739 xassert (parsed_p);
8740
8741 /* Get specified width, and height. */
8742 img->width = XFASTINT (fmt[XBM_WIDTH].value);
8743 img->height = XFASTINT (fmt[XBM_HEIGHT].value);
8744 xassert (img->width > 0 && img->height > 0);
8745
8746 BLOCK_INPUT;
8747
8748 if (fmt[XBM_ASCENT].count)
8749 img->ascent = XFASTINT (fmt[XBM_ASCENT].value);
8750
8751 /* Get foreground and background colors, maybe allocate colors. */
8752 if (fmt[XBM_FOREGROUND].count)
8753 foreground = x_alloc_image_color (f, img, fmt[XBM_FOREGROUND].value,
8754 foreground);
8755 if (fmt[XBM_BACKGROUND].count)
8756 background = x_alloc_image_color (f, img, fmt[XBM_BACKGROUND].value,
8757 background);
8758
8759 /* Set bits to the bitmap image data. */
8760 data = fmt[XBM_DATA].value;
8761 if (VECTORP (data))
8762 {
8763 int i;
8764 char *p;
8765 int nbytes = (img->width + BITS_PER_CHAR - 1) / BITS_PER_CHAR;
8766
8767 p = bits = (char *) alloca (nbytes * img->height);
8768 for (i = 0; i < img->height; ++i, p += nbytes)
8769 {
8770 Lisp_Object line = XVECTOR (data)->contents[i];
8771 if (STRINGP (line))
8772 bcopy (XSTRING (line)->data, p, nbytes);
8773 else
8774 bcopy (XBOOL_VECTOR (line)->data, p, nbytes);
8775 }
8776 }
8777 else if (STRINGP (data))
8778 bits = XSTRING (data)->data;
8779 else
8780 bits = XBOOL_VECTOR (data)->data;
8781
8782#if 0 /* NTEMACS_TODO : W32 XPM code */
8783 /* Create the pixmap. */
8784 depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
8785 img->pixmap
8786 = XCreatePixmapFromBitmapData (FRAME_W32_DISPLAY (f),
8787 FRAME_W32_WINDOW (f),
8788 bits,
8789 img->width, img->height,
8790 foreground, background,
8791 depth);
8792#endif /* NTEMACS_TODO */
8793
8794 if (img->pixmap)
8795 success_p = 1;
8796 else
8797 {
8798 image_error ("Unable to create pixmap for XBM image `%s'",
8799 img->spec, Qnil);
8800 x_clear_image (f, img);
8801 }
8802
8803 UNBLOCK_INPUT;
8804 }
8805
8806 return success_p;
8807}
8808
8809
8810\f
8811/***********************************************************************
8812 XPM images
8813 ***********************************************************************/
8814
8815#if HAVE_XPM
8816
8817static int xpm_image_p P_ ((Lisp_Object object));
8818static int xpm_load P_ ((struct frame *f, struct image *img));
8819static int xpm_valid_color_symbols_p P_ ((Lisp_Object));
8820
8821#include "X11/xpm.h"
8822
8823/* The symbol `xpm' identifying XPM-format images. */
8824
8825Lisp_Object Qxpm;
8826
8827/* Indices of image specification fields in xpm_format, below. */
8828
8829enum xpm_keyword_index
8830{
8831 XPM_TYPE,
8832 XPM_FILE,
8833 XPM_DATA,
8834 XPM_ASCENT,
8835 XPM_MARGIN,
8836 XPM_RELIEF,
8837 XPM_ALGORITHM,
8838 XPM_HEURISTIC_MASK,
8839 XPM_COLOR_SYMBOLS,
8840 XPM_LAST
8841};
8842
8843/* Vector of image_keyword structures describing the format
8844 of valid XPM image specifications. */
8845
8846static struct image_keyword xpm_format[XPM_LAST] =
8847{
8848 {":type", IMAGE_SYMBOL_VALUE, 1},
8849 {":file", IMAGE_STRING_VALUE, 0},
8850 {":data", IMAGE_STRING_VALUE, 0},
8851 {":ascent", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
8852 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
8853 {":relief", IMAGE_INTEGER_VALUE, 0},
8854 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8855 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8856 {":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
8857};
8858
8859/* Structure describing the image type XBM. */
8860
8861static struct image_type xpm_type =
8862{
8863 &Qxpm,
8864 xpm_image_p,
8865 xpm_load,
8866 x_clear_image,
8867 NULL
8868};
8869
8870
8871/* Value is non-zero if COLOR_SYMBOLS is a valid color symbols list
8872 for XPM images. Such a list must consist of conses whose car and
8873 cdr are strings. */
8874
8875static int
8876xpm_valid_color_symbols_p (color_symbols)
8877 Lisp_Object color_symbols;
8878{
8879 while (CONSP (color_symbols))
8880 {
8881 Lisp_Object sym = XCAR (color_symbols);
8882 if (!CONSP (sym)
8883 || !STRINGP (XCAR (sym))
8884 || !STRINGP (XCDR (sym)))
8885 break;
8886 color_symbols = XCDR (color_symbols);
8887 }
8888
8889 return NILP (color_symbols);
8890}
8891
8892
8893/* Value is non-zero if OBJECT is a valid XPM image specification. */
8894
8895static int
8896xpm_image_p (object)
8897 Lisp_Object object;
8898{
8899 struct image_keyword fmt[XPM_LAST];
8900 bcopy (xpm_format, fmt, sizeof fmt);
8901 return (parse_image_spec (object, fmt, XPM_LAST, Qxpm)
8902 /* Either `:file' or `:data' must be present. */
8903 && fmt[XPM_FILE].count + fmt[XPM_DATA].count == 1
8904 /* Either no `:color-symbols' or it's a list of conses
8905 whose car and cdr are strings. */
8906 && (fmt[XPM_COLOR_SYMBOLS].count == 0
8907 || xpm_valid_color_symbols_p (fmt[XPM_COLOR_SYMBOLS].value))
8908 && (fmt[XPM_ASCENT].count == 0
8909 || XFASTINT (fmt[XPM_ASCENT].value) < 100));
8910}
8911
8912
8913/* Load image IMG which will be displayed on frame F. Value is
8914 non-zero if successful. */
8915
8916static int
8917xpm_load (f, img)
8918 struct frame *f;
8919 struct image *img;
8920{
8921 int rc, i;
8922 XpmAttributes attrs;
8923 Lisp_Object specified_file, color_symbols;
8924
8925 /* Configure the XPM lib. Use the visual of frame F. Allocate
8926 close colors. Return colors allocated. */
8927 bzero (&attrs, sizeof attrs);
dfff8a69
JR
8928 attrs.visual = FRAME_X_VISUAL (f);
8929 attrs.colormap = FRAME_X_COLORMAP (f);
6fc2811b 8930 attrs.valuemask |= XpmVisual;
dfff8a69 8931 attrs.valuemask |= XpmColormap;
6fc2811b 8932 attrs.valuemask |= XpmReturnAllocPixels;
dfff8a69 8933#ifdef XpmAllocCloseColors
6fc2811b
JR
8934 attrs.alloc_close_colors = 1;
8935 attrs.valuemask |= XpmAllocCloseColors;
dfff8a69
JR
8936#else
8937 attrs.closeness = 600;
8938 attrs.valuemask |= XpmCloseness;
8939#endif
6fc2811b
JR
8940
8941 /* If image specification contains symbolic color definitions, add
8942 these to `attrs'. */
8943 color_symbols = image_spec_value (img->spec, QCcolor_symbols, NULL);
8944 if (CONSP (color_symbols))
8945 {
8946 Lisp_Object tail;
8947 XpmColorSymbol *xpm_syms;
8948 int i, size;
8949
8950 attrs.valuemask |= XpmColorSymbols;
8951
8952 /* Count number of symbols. */
8953 attrs.numsymbols = 0;
8954 for (tail = color_symbols; CONSP (tail); tail = XCDR (tail))
8955 ++attrs.numsymbols;
8956
8957 /* Allocate an XpmColorSymbol array. */
8958 size = attrs.numsymbols * sizeof *xpm_syms;
8959 xpm_syms = (XpmColorSymbol *) alloca (size);
8960 bzero (xpm_syms, size);
8961 attrs.colorsymbols = xpm_syms;
8962
8963 /* Fill the color symbol array. */
8964 for (tail = color_symbols, i = 0;
8965 CONSP (tail);
8966 ++i, tail = XCDR (tail))
8967 {
8968 Lisp_Object name = XCAR (XCAR (tail));
8969 Lisp_Object color = XCDR (XCAR (tail));
8970 xpm_syms[i].name = (char *) alloca (XSTRING (name)->size + 1);
8971 strcpy (xpm_syms[i].name, XSTRING (name)->data);
8972 xpm_syms[i].value = (char *) alloca (XSTRING (color)->size + 1);
8973 strcpy (xpm_syms[i].value, XSTRING (color)->data);
8974 }
8975 }
8976
8977 /* Create a pixmap for the image, either from a file, or from a
8978 string buffer containing data in the same format as an XPM file. */
8979 BLOCK_INPUT;
8980 specified_file = image_spec_value (img->spec, QCfile, NULL);
8981 if (STRINGP (specified_file))
8982 {
8983 Lisp_Object file = x_find_image_file (specified_file);
8984 if (!STRINGP (file))
8985 {
8986 image_error ("Cannot find image file `%s'", specified_file, Qnil);
8987 UNBLOCK_INPUT;
8988 return 0;
8989 }
8990
8991 rc = XpmReadFileToPixmap (NULL, FRAME_W32_WINDOW (f),
8992 XSTRING (file)->data, &img->pixmap, &img->mask,
8993 &attrs);
8994 }
8995 else
8996 {
8997 Lisp_Object buffer = image_spec_value (img->spec, QCdata, NULL);
8998 rc = XpmCreatePixmapFromBuffer (NULL, FRAME_W32_WINDOW (f),
8999 XSTRING (buffer)->data,
9000 &img->pixmap, &img->mask,
9001 &attrs);
9002 }
9003 UNBLOCK_INPUT;
9004
9005 if (rc == XpmSuccess)
9006 {
9007 /* Remember allocated colors. */
9008 img->ncolors = attrs.nalloc_pixels;
9009 img->colors = (unsigned long *) xmalloc (img->ncolors
9010 * sizeof *img->colors);
9011 for (i = 0; i < attrs.nalloc_pixels; ++i)
9012 img->colors[i] = attrs.alloc_pixels[i];
9013
9014 img->width = attrs.width;
9015 img->height = attrs.height;
9016 xassert (img->width > 0 && img->height > 0);
9017
9018 /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */
9019 BLOCK_INPUT;
9020 XpmFreeAttributes (&attrs);
9021 UNBLOCK_INPUT;
9022 }
9023 else
9024 {
9025 switch (rc)
9026 {
9027 case XpmOpenFailed:
9028 image_error ("Error opening XPM file (%s)", img->spec, Qnil);
9029 break;
9030
9031 case XpmFileInvalid:
9032 image_error ("Invalid XPM file (%s)", img->spec, Qnil);
9033 break;
9034
9035 case XpmNoMemory:
9036 image_error ("Out of memory (%s)", img->spec, Qnil);
9037 break;
9038
9039 case XpmColorFailed:
9040 image_error ("Color allocation error (%s)", img->spec, Qnil);
9041 break;
9042
9043 default:
9044 image_error ("Unknown error (%s)", img->spec, Qnil);
9045 break;
9046 }
9047 }
9048
9049 return rc == XpmSuccess;
9050}
9051
9052#endif /* HAVE_XPM != 0 */
9053
9054\f
9055#if 0 /* NTEMACS_TODO : Color tables on W32. */
9056/***********************************************************************
9057 Color table
9058 ***********************************************************************/
9059
9060/* An entry in the color table mapping an RGB color to a pixel color. */
9061
9062struct ct_color
9063{
9064 int r, g, b;
9065 unsigned long pixel;
9066
9067 /* Next in color table collision list. */
9068 struct ct_color *next;
9069};
9070
9071/* The bucket vector size to use. Must be prime. */
9072
9073#define CT_SIZE 101
9074
9075/* Value is a hash of the RGB color given by R, G, and B. */
9076
9077#define CT_HASH_RGB(R, G, B) (((R) << 16) ^ ((G) << 8) ^ (B))
9078
9079/* The color hash table. */
9080
9081struct ct_color **ct_table;
9082
9083/* Number of entries in the color table. */
9084
9085int ct_colors_allocated;
9086
9087/* Function prototypes. */
9088
9089static void init_color_table P_ ((void));
9090static void free_color_table P_ ((void));
9091static unsigned long *colors_in_color_table P_ ((int *n));
9092static unsigned long lookup_rgb_color P_ ((struct frame *f, int r, int g, int b));
9093static unsigned long lookup_pixel_color P_ ((struct frame *f, unsigned long p));
9094
9095
9096/* Initialize the color table. */
9097
9098static void
9099init_color_table ()
9100{
9101 int size = CT_SIZE * sizeof (*ct_table);
9102 ct_table = (struct ct_color **) xmalloc (size);
9103 bzero (ct_table, size);
9104 ct_colors_allocated = 0;
9105}
9106
9107
9108/* Free memory associated with the color table. */
9109
9110static void
9111free_color_table ()
9112{
9113 int i;
9114 struct ct_color *p, *next;
9115
9116 for (i = 0; i < CT_SIZE; ++i)
9117 for (p = ct_table[i]; p; p = next)
9118 {
9119 next = p->next;
9120 xfree (p);
9121 }
9122
9123 xfree (ct_table);
9124 ct_table = NULL;
9125}
9126
9127
9128/* Value is a pixel color for RGB color R, G, B on frame F. If an
9129 entry for that color already is in the color table, return the
9130 pixel color of that entry. Otherwise, allocate a new color for R,
9131 G, B, and make an entry in the color table. */
9132
9133static unsigned long
9134lookup_rgb_color (f, r, g, b)
9135 struct frame *f;
9136 int r, g, b;
9137{
9138 unsigned hash = CT_HASH_RGB (r, g, b);
9139 int i = hash % CT_SIZE;
9140 struct ct_color *p;
9141
9142 for (p = ct_table[i]; p; p = p->next)
9143 if (p->r == r && p->g == g && p->b == b)
9144 break;
9145
9146 if (p == NULL)
9147 {
9148 COLORREF color;
9149 Colormap cmap;
9150 int rc;
9151
9152 color = PALETTERGB (r, g, b);
9153
9154 ++ct_colors_allocated;
9155
9156 p = (struct ct_color *) xmalloc (sizeof *p);
9157 p->r = r;
9158 p->g = g;
9159 p->b = b;
9160 p->pixel = color;
9161 p->next = ct_table[i];
9162 ct_table[i] = p;
9163 }
9164
9165 return p->pixel;
9166}
9167
9168
9169/* Look up pixel color PIXEL which is used on frame F in the color
9170 table. If not already present, allocate it. Value is PIXEL. */
9171
9172static unsigned long
9173lookup_pixel_color (f, pixel)
9174 struct frame *f;
9175 unsigned long pixel;
9176{
9177 int i = pixel % CT_SIZE;
9178 struct ct_color *p;
9179
9180 for (p = ct_table[i]; p; p = p->next)
9181 if (p->pixel == pixel)
9182 break;
9183
9184 if (p == NULL)
9185 {
9186 XColor color;
9187 Colormap cmap;
9188 int rc;
9189
9190 BLOCK_INPUT;
9191
9192 cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f));
9193 color.pixel = pixel;
9194 XQueryColor (NULL, cmap, &color);
9195 rc = x_alloc_nearest_color (f, cmap, &color);
9196 UNBLOCK_INPUT;
9197
9198 if (rc)
9199 {
9200 ++ct_colors_allocated;
9201
9202 p = (struct ct_color *) xmalloc (sizeof *p);
9203 p->r = color.red;
9204 p->g = color.green;
9205 p->b = color.blue;
9206 p->pixel = pixel;
9207 p->next = ct_table[i];
9208 ct_table[i] = p;
9209 }
9210 else
9211 return FRAME_FOREGROUND_PIXEL (f);
9212 }
9213 return p->pixel;
9214}
9215
9216
9217/* Value is a vector of all pixel colors contained in the color table,
9218 allocated via xmalloc. Set *N to the number of colors. */
9219
9220static unsigned long *
9221colors_in_color_table (n)
9222 int *n;
9223{
9224 int i, j;
9225 struct ct_color *p;
9226 unsigned long *colors;
9227
9228 if (ct_colors_allocated == 0)
9229 {
9230 *n = 0;
9231 colors = NULL;
9232 }
9233 else
9234 {
9235 colors = (unsigned long *) xmalloc (ct_colors_allocated
9236 * sizeof *colors);
9237 *n = ct_colors_allocated;
9238
9239 for (i = j = 0; i < CT_SIZE; ++i)
9240 for (p = ct_table[i]; p; p = p->next)
9241 colors[j++] = p->pixel;
9242 }
9243
9244 return colors;
9245}
9246
9247#endif /* NTEMACS_TODO */
9248
9249\f
9250/***********************************************************************
9251 Algorithms
9252 ***********************************************************************/
9253
9254#if 0 /* NTEMACS_TODO : W32 versions of low level algorithms */
9255static void x_laplace_write_row P_ ((struct frame *, long *,
9256 int, XImage *, int));
9257static void x_laplace_read_row P_ ((struct frame *, Colormap,
9258 XColor *, int, XImage *, int));
9259
9260
9261/* Fill COLORS with RGB colors from row Y of image XIMG. F is the
9262 frame we operate on, CMAP is the color-map in effect, and WIDTH is
9263 the width of one row in the image. */
9264
9265static void
9266x_laplace_read_row (f, cmap, colors, width, ximg, y)
9267 struct frame *f;
9268 Colormap cmap;
9269 XColor *colors;
9270 int width;
9271 XImage *ximg;
9272 int y;
9273{
9274 int x;
9275
9276 for (x = 0; x < width; ++x)
9277 colors[x].pixel = XGetPixel (ximg, x, y);
9278
9279 XQueryColors (NULL, cmap, colors, width);
9280}
9281
9282
9283/* Write row Y of image XIMG. PIXELS is an array of WIDTH longs
9284 containing the pixel colors to write. F is the frame we are
9285 working on. */
9286
9287static void
9288x_laplace_write_row (f, pixels, width, ximg, y)
9289 struct frame *f;
9290 long *pixels;
9291 int width;
9292 XImage *ximg;
9293 int y;
9294{
9295 int x;
9296
9297 for (x = 0; x < width; ++x)
9298 XPutPixel (ximg, x, y, pixels[x]);
9299}
9300#endif
9301
9302/* Transform image IMG which is used on frame F with a Laplace
9303 edge-detection algorithm. The result is an image that can be used
9304 to draw disabled buttons, for example. */
9305
9306static void
9307x_laplace (f, img)
9308 struct frame *f;
9309 struct image *img;
9310{
9311#if 0 /* NTEMACS_TODO : W32 version */
9312 Colormap cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f));
9313 XImage *ximg, *oimg;
9314 XColor *in[3];
9315 long *out;
9316 Pixmap pixmap;
9317 int x, y, i;
9318 long pixel;
9319 int in_y, out_y, rc;
9320 int mv2 = 45000;
9321
9322 BLOCK_INPUT;
9323
9324 /* Get the X image IMG->pixmap. */
9325 ximg = XGetImage (NULL, img->pixmap,
9326 0, 0, img->width, img->height, ~0, ZPixmap);
9327
9328 /* Allocate 3 input rows, and one output row of colors. */
9329 for (i = 0; i < 3; ++i)
9330 in[i] = (XColor *) alloca (img->width * sizeof (XColor));
9331 out = (long *) alloca (img->width * sizeof (long));
9332
9333 /* Create an X image for output. */
9334 rc = x_create_x_image_and_pixmap (f, img->width, img->height, 0,
9335 &oimg, &pixmap);
9336
9337 /* Fill first two rows. */
9338 x_laplace_read_row (f, cmap, in[0], img->width, ximg, 0);
9339 x_laplace_read_row (f, cmap, in[1], img->width, ximg, 1);
9340 in_y = 2;
9341
9342 /* Write first row, all zeros. */
9343 init_color_table ();
9344 pixel = lookup_rgb_color (f, 0, 0, 0);
9345 for (x = 0; x < img->width; ++x)
9346 out[x] = pixel;
9347 x_laplace_write_row (f, out, img->width, oimg, 0);
9348 out_y = 1;
9349
9350 for (y = 2; y < img->height; ++y)
9351 {
9352 int rowa = y % 3;
9353 int rowb = (y + 2) % 3;
9354
9355 x_laplace_read_row (f, cmap, in[rowa], img->width, ximg, in_y++);
9356
9357 for (x = 0; x < img->width - 2; ++x)
9358 {
9359 int r = in[rowa][x].red + mv2 - in[rowb][x + 2].red;
9360 int g = in[rowa][x].green + mv2 - in[rowb][x + 2].green;
9361 int b = in[rowa][x].blue + mv2 - in[rowb][x + 2].blue;
9362
9363 out[x + 1] = lookup_rgb_color (f, r & 0xffff, g & 0xffff,
9364 b & 0xffff);
9365 }
9366
9367 x_laplace_write_row (f, out, img->width, oimg, out_y++);
9368 }
9369
9370 /* Write last line, all zeros. */
9371 for (x = 0; x < img->width; ++x)
9372 out[x] = pixel;
9373 x_laplace_write_row (f, out, img->width, oimg, out_y);
9374
9375 /* Free the input image, and free resources of IMG. */
9376 XDestroyImage (ximg);
9377 x_clear_image (f, img);
9378
9379 /* Put the output image into pixmap, and destroy it. */
9380 x_put_x_image (f, oimg, pixmap, img->width, img->height);
9381 x_destroy_x_image (oimg);
9382
9383 /* Remember new pixmap and colors in IMG. */
9384 img->pixmap = pixmap;
9385 img->colors = colors_in_color_table (&img->ncolors);
9386 free_color_table ();
9387
9388 UNBLOCK_INPUT;
9389#endif /* NTEMACS_TODO */
9390}
9391
9392
9393/* Build a mask for image IMG which is used on frame F. FILE is the
9394 name of an image file, for error messages. HOW determines how to
9395 determine the background color of IMG. If it is a list '(R G B)',
9396 with R, G, and B being integers >= 0, take that as the color of the
9397 background. Otherwise, determine the background color of IMG
9398 heuristically. Value is non-zero if successful. */
9399
9400static int
9401x_build_heuristic_mask (f, img, how)
9402 struct frame *f;
9403 struct image *img;
9404 Lisp_Object how;
9405{
9406#if 0 /* NTEMACS_TODO : W32 version */
9407 Display *dpy = FRAME_W32_DISPLAY (f);
9408 XImage *ximg, *mask_img;
9409 int x, y, rc, look_at_corners_p;
9410 unsigned long bg;
9411
9412 BLOCK_INPUT;
9413
9414 /* Create an image and pixmap serving as mask. */
9415 rc = x_create_x_image_and_pixmap (f, img->width, img->height, 1,
9416 &mask_img, &img->mask);
9417 if (!rc)
9418 {
9419 UNBLOCK_INPUT;
9420 return 0;
9421 }
9422
9423 /* Get the X image of IMG->pixmap. */
9424 ximg = XGetImage (dpy, img->pixmap, 0, 0, img->width, img->height,
9425 ~0, ZPixmap);
9426
9427 /* Determine the background color of ximg. If HOW is `(R G B)'
9428 take that as color. Otherwise, try to determine the color
9429 heuristically. */
9430 look_at_corners_p = 1;
9431
9432 if (CONSP (how))
9433 {
9434 int rgb[3], i = 0;
9435
9436 while (i < 3
9437 && CONSP (how)
9438 && NATNUMP (XCAR (how)))
9439 {
9440 rgb[i] = XFASTINT (XCAR (how)) & 0xffff;
9441 how = XCDR (how);
9442 }
9443
9444 if (i == 3 && NILP (how))
9445 {
9446 char color_name[30];
9447 XColor exact, color;
9448 Colormap cmap;
9449
9450 sprintf (color_name, "#%04x%04x%04x", rgb[0], rgb[1], rgb[2]);
9451
9452 cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f));
9453 if (XLookupColor (dpy, cmap, color_name, &exact, &color))
9454 {
9455 bg = color.pixel;
9456 look_at_corners_p = 0;
9457 }
9458 }
9459 }
9460
9461 if (look_at_corners_p)
9462 {
9463 unsigned long corners[4];
9464 int i, best_count;
9465
9466 /* Get the colors at the corners of ximg. */
9467 corners[0] = XGetPixel (ximg, 0, 0);
9468 corners[1] = XGetPixel (ximg, img->width - 1, 0);
9469 corners[2] = XGetPixel (ximg, img->width - 1, img->height - 1);
9470 corners[3] = XGetPixel (ximg, 0, img->height - 1);
9471
9472 /* Choose the most frequently found color as background. */
9473 for (i = best_count = 0; i < 4; ++i)
9474 {
9475 int j, n;
9476
9477 for (j = n = 0; j < 4; ++j)
9478 if (corners[i] == corners[j])
9479 ++n;
9480
9481 if (n > best_count)
9482 bg = corners[i], best_count = n;
9483 }
9484 }
9485
9486 /* Set all bits in mask_img to 1 whose color in ximg is different
9487 from the background color bg. */
9488 for (y = 0; y < img->height; ++y)
9489 for (x = 0; x < img->width; ++x)
9490 XPutPixel (mask_img, x, y, XGetPixel (ximg, x, y) != bg);
9491
9492 /* Put mask_img into img->mask. */
9493 x_put_x_image (f, mask_img, img->mask, img->width, img->height);
9494 x_destroy_x_image (mask_img);
9495 XDestroyImage (ximg);
9496
9497 UNBLOCK_INPUT;
9498#endif /* NTEMACS_TODO */
9499
9500 return 1;
9501}
9502
9503
9504\f
9505/***********************************************************************
9506 PBM (mono, gray, color)
9507 ***********************************************************************/
9508#ifdef HAVE_PBM
9509
9510static int pbm_image_p P_ ((Lisp_Object object));
9511static int pbm_load P_ ((struct frame *f, struct image *img));
9512static int pbm_scan_number P_ ((unsigned char **, unsigned char *));
9513
9514/* The symbol `pbm' identifying images of this type. */
9515
9516Lisp_Object Qpbm;
9517
9518/* Indices of image specification fields in gs_format, below. */
9519
9520enum pbm_keyword_index
9521{
9522 PBM_TYPE,
9523 PBM_FILE,
9524 PBM_DATA,
9525 PBM_ASCENT,
9526 PBM_MARGIN,
9527 PBM_RELIEF,
9528 PBM_ALGORITHM,
9529 PBM_HEURISTIC_MASK,
9530 PBM_LAST
9531};
9532
9533/* Vector of image_keyword structures describing the format
9534 of valid user-defined image specifications. */
9535
9536static struct image_keyword pbm_format[PBM_LAST] =
9537{
9538 {":type", IMAGE_SYMBOL_VALUE, 1},
9539 {":file", IMAGE_STRING_VALUE, 0},
9540 {":data", IMAGE_STRING_VALUE, 0},
9541 {":ascent", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
9542 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
9543 {":relief", IMAGE_INTEGER_VALUE, 0},
9544 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9545 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
9546};
9547
9548/* Structure describing the image type `pbm'. */
9549
9550static struct image_type pbm_type =
9551{
9552 &Qpbm,
9553 pbm_image_p,
9554 pbm_load,
9555 x_clear_image,
9556 NULL
9557};
9558
9559
9560/* Return non-zero if OBJECT is a valid PBM image specification. */
9561
9562static int
9563pbm_image_p (object)
9564 Lisp_Object object;
9565{
9566 struct image_keyword fmt[PBM_LAST];
9567
9568 bcopy (pbm_format, fmt, sizeof fmt);
9569
9570 if (!parse_image_spec (object, fmt, PBM_LAST, Qpbm)
9571 || (fmt[PBM_ASCENT].count
9572 && XFASTINT (fmt[PBM_ASCENT].value) > 100))
9573 return 0;
9574
9575 /* Must specify either :data or :file. */
9576 return fmt[PBM_DATA].count + fmt[PBM_FILE].count == 1;
9577}
9578
9579
9580/* Scan a decimal number from *S and return it. Advance *S while
9581 reading the number. END is the end of the string. Value is -1 at
9582 end of input. */
9583
9584static int
9585pbm_scan_number (s, end)
9586 unsigned char **s, *end;
9587{
9588 int c, val = -1;
9589
9590 while (*s < end)
9591 {
9592 /* Skip white-space. */
9593 while (*s < end && (c = *(*s)++, isspace (c)))
9594 ;
9595
9596 if (c == '#')
9597 {
9598 /* Skip comment to end of line. */
9599 while (*s < end && (c = *(*s)++, c != '\n'))
9600 ;
9601 }
9602 else if (isdigit (c))
9603 {
9604 /* Read decimal number. */
9605 val = c - '0';
9606 while (*s < end && (c = *(*s)++, isdigit (c)))
9607 val = 10 * val + c - '0';
9608 break;
9609 }
9610 else
9611 break;
9612 }
9613
9614 return val;
9615}
9616
9617
9618/* Read FILE into memory. Value is a pointer to a buffer allocated
9619 with xmalloc holding FILE's contents. Value is null if an error
9620 occured. *SIZE is set to the size of the file. */
9621
9622static char *
9623pbm_read_file (file, size)
9624 Lisp_Object file;
9625 int *size;
9626{
9627 FILE *fp = NULL;
9628 char *buf = NULL;
9629 struct stat st;
9630
9631 if (stat (XSTRING (file)->data, &st) == 0
9632 && (fp = fopen (XSTRING (file)->data, "r")) != NULL
9633 && (buf = (char *) xmalloc (st.st_size),
9634 fread (buf, 1, st.st_size, fp) == st.st_size))
9635 {
9636 *size = st.st_size;
9637 fclose (fp);
9638 }
9639 else
9640 {
9641 if (fp)
9642 fclose (fp);
9643 if (buf)
9644 {
9645 xfree (buf);
9646 buf = NULL;
9647 }
9648 }
9649
9650 return buf;
9651}
9652
9653
9654/* Load PBM image IMG for use on frame F. */
9655
9656static int
9657pbm_load (f, img)
9658 struct frame *f;
9659 struct image *img;
9660{
9661 int raw_p, x, y;
9662 int width, height, max_color_idx = 0;
9663 XImage *ximg;
9664 Lisp_Object file, specified_file;
9665 enum {PBM_MONO, PBM_GRAY, PBM_COLOR} type;
9666 struct gcpro gcpro1;
9667 unsigned char *contents = NULL;
9668 unsigned char *end, *p;
9669 int size;
9670
9671 specified_file = image_spec_value (img->spec, QCfile, NULL);
9672 file = Qnil;
9673 GCPRO1 (file);
9674
9675 if (STRINGP (specified_file))
9676 {
9677 file = x_find_image_file (specified_file);
9678 if (!STRINGP (file))
9679 {
9680 image_error ("Cannot find image file `%s'", specified_file, Qnil);
9681 UNGCPRO;
9682 return 0;
9683 }
9684
9685 contents = pbm_read_file (file, &size);
9686 if (contents == NULL)
9687 {
9688 image_error ("Error reading `%s'", file, Qnil);
9689 UNGCPRO;
9690 return 0;
9691 }
9692
9693 p = contents;
9694 end = contents + size;
9695 }
9696 else
9697 {
9698 Lisp_Object data;
9699 data = image_spec_value (img->spec, QCdata, NULL);
9700 p = XSTRING (data)->data;
9701 end = p + STRING_BYTES (XSTRING (data));
9702 }
9703
9704 /* Check magic number. */
9705 if (end - p < 2 || *p++ != 'P')
9706 {
9707 image_error ("Not a PBM image: `%s'", img->spec, Qnil);
9708 error:
9709 xfree (contents);
9710 UNGCPRO;
9711 return 0;
9712 }
9713
6fc2811b
JR
9714 switch (*p++)
9715 {
9716 case '1':
9717 raw_p = 0, type = PBM_MONO;
9718 break;
9719
9720 case '2':
9721 raw_p = 0, type = PBM_GRAY;
9722 break;
9723
9724 case '3':
9725 raw_p = 0, type = PBM_COLOR;
9726 break;
9727
9728 case '4':
9729 raw_p = 1, type = PBM_MONO;
9730 break;
9731
9732 case '5':
9733 raw_p = 1, type = PBM_GRAY;
9734 break;
9735
9736 case '6':
9737 raw_p = 1, type = PBM_COLOR;
9738 break;
9739
9740 default:
9741 image_error ("Not a PBM image: `%s'", img->spec, Qnil);
9742 goto error;
9743 }
9744
9745 /* Read width, height, maximum color-component. Characters
9746 starting with `#' up to the end of a line are ignored. */
9747 width = pbm_scan_number (&p, end);
9748 height = pbm_scan_number (&p, end);
9749
9750 if (type != PBM_MONO)
9751 {
9752 max_color_idx = pbm_scan_number (&p, end);
9753 if (raw_p && max_color_idx > 255)
9754 max_color_idx = 255;
9755 }
9756
9757 if (width < 0
9758 || height < 0
9759 || (type != PBM_MONO && max_color_idx < 0))
9760 goto error;
9761
9762 BLOCK_INPUT;
9763 if (!x_create_x_image_and_pixmap (f, width, height, 0,
9764 &ximg, &img->pixmap))
9765 {
9766 UNBLOCK_INPUT;
9767 goto error;
9768 }
9769
9770 /* Initialize the color hash table. */
9771 init_color_table ();
9772
9773 if (type == PBM_MONO)
9774 {
9775 int c = 0, g;
9776
9777 for (y = 0; y < height; ++y)
9778 for (x = 0; x < width; ++x)
9779 {
9780 if (raw_p)
9781 {
9782 if ((x & 7) == 0)
9783 c = *p++;
9784 g = c & 0x80;
9785 c <<= 1;
9786 }
9787 else
9788 g = pbm_scan_number (&p, end);
9789
9790 XPutPixel (ximg, x, y, (g
9791 ? FRAME_FOREGROUND_PIXEL (f)
9792 : FRAME_BACKGROUND_PIXEL (f)));
9793 }
9794 }
9795 else
9796 {
9797 for (y = 0; y < height; ++y)
9798 for (x = 0; x < width; ++x)
9799 {
9800 int r, g, b;
9801
9802 if (type == PBM_GRAY)
9803 r = g = b = raw_p ? *p++ : pbm_scan_number (&p, end);
9804 else if (raw_p)
9805 {
9806 r = *p++;
9807 g = *p++;
9808 b = *p++;
9809 }
9810 else
9811 {
9812 r = pbm_scan_number (&p, end);
9813 g = pbm_scan_number (&p, end);
9814 b = pbm_scan_number (&p, end);
9815 }
9816
9817 if (r < 0 || g < 0 || b < 0)
9818 {
dfff8a69 9819 xfree (ximg->data);
6fc2811b
JR
9820 ximg->data = NULL;
9821 XDestroyImage (ximg);
9822 UNBLOCK_INPUT;
9823 image_error ("Invalid pixel value in image `%s'",
9824 img->spec, Qnil);
9825 goto error;
9826 }
9827
9828 /* RGB values are now in the range 0..max_color_idx.
9829 Scale this to the range 0..0xffff supported by X. */
9830 r = (double) r * 65535 / max_color_idx;
9831 g = (double) g * 65535 / max_color_idx;
9832 b = (double) b * 65535 / max_color_idx;
9833 XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b));
9834 }
9835 }
9836
9837 /* Store in IMG->colors the colors allocated for the image, and
9838 free the color table. */
9839 img->colors = colors_in_color_table (&img->ncolors);
9840 free_color_table ();
9841
9842 /* Put the image into a pixmap. */
9843 x_put_x_image (f, ximg, img->pixmap, width, height);
9844 x_destroy_x_image (ximg);
9845 UNBLOCK_INPUT;
9846
9847 img->width = width;
9848 img->height = height;
9849
9850 UNGCPRO;
9851 xfree (contents);
9852 return 1;
9853}
9854#endif /* HAVE_PBM */
9855
9856\f
9857/***********************************************************************
9858 PNG
9859 ***********************************************************************/
9860
9861#if HAVE_PNG
9862
9863#include <png.h>
9864
9865/* Function prototypes. */
9866
9867static int png_image_p P_ ((Lisp_Object object));
9868static int png_load P_ ((struct frame *f, struct image *img));
9869
9870/* The symbol `png' identifying images of this type. */
9871
9872Lisp_Object Qpng;
9873
9874/* Indices of image specification fields in png_format, below. */
9875
9876enum png_keyword_index
9877{
9878 PNG_TYPE,
9879 PNG_DATA,
9880 PNG_FILE,
9881 PNG_ASCENT,
9882 PNG_MARGIN,
9883 PNG_RELIEF,
9884 PNG_ALGORITHM,
9885 PNG_HEURISTIC_MASK,
9886 PNG_LAST
9887};
9888
9889/* Vector of image_keyword structures describing the format
9890 of valid user-defined image specifications. */
9891
9892static struct image_keyword png_format[PNG_LAST] =
9893{
9894 {":type", IMAGE_SYMBOL_VALUE, 1},
9895 {":data", IMAGE_STRING_VALUE, 0},
9896 {":file", IMAGE_STRING_VALUE, 0},
9897 {":ascent", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
9898 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
9899 {":relief", IMAGE_INTEGER_VALUE, 0},
9900 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9901 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
9902};
9903
9904/* Structure describing the image type `png'. */
9905
9906static struct image_type png_type =
9907{
9908 &Qpng,
9909 png_image_p,
9910 png_load,
9911 x_clear_image,
9912 NULL
9913};
9914
9915
9916/* Return non-zero if OBJECT is a valid PNG image specification. */
9917
9918static int
9919png_image_p (object)
9920 Lisp_Object object;
9921{
9922 struct image_keyword fmt[PNG_LAST];
9923 bcopy (png_format, fmt, sizeof fmt);
9924
9925 if (!parse_image_spec (object, fmt, PNG_LAST, Qpng)
9926 || (fmt[PNG_ASCENT].count
9927 && XFASTINT (fmt[PNG_ASCENT].value) > 100))
9928 return 0;
9929
9930 /* Must specify either the :data or :file keyword. */
9931 return fmt[PNG_FILE].count + fmt[PNG_DATA].count == 1;
9932}
9933
9934
9935/* Error and warning handlers installed when the PNG library
9936 is initialized. */
9937
9938static void
9939my_png_error (png_ptr, msg)
9940 png_struct *png_ptr;
9941 char *msg;
9942{
9943 xassert (png_ptr != NULL);
9944 image_error ("PNG error: %s", build_string (msg), Qnil);
9945 longjmp (png_ptr->jmpbuf, 1);
9946}
9947
9948
9949static void
9950my_png_warning (png_ptr, msg)
9951 png_struct *png_ptr;
9952 char *msg;
9953{
9954 xassert (png_ptr != NULL);
9955 image_error ("PNG warning: %s", build_string (msg), Qnil);
9956}
9957
6fc2811b
JR
9958/* Memory source for PNG decoding. */
9959
9960struct png_memory_storage
9961{
9962 unsigned char *bytes; /* The data */
9963 size_t len; /* How big is it? */
9964 int index; /* Where are we? */
9965};
9966
9967
9968/* Function set as reader function when reading PNG image from memory.
9969 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
9970 bytes from the input to DATA. */
9971
9972static void
9973png_read_from_memory (png_ptr, data, length)
9974 png_structp png_ptr;
9975 png_bytep data;
9976 png_size_t length;
9977{
9978 struct png_memory_storage *tbr
9979 = (struct png_memory_storage *) png_get_io_ptr (png_ptr);
9980
9981 if (length > tbr->len - tbr->index)
9982 png_error (png_ptr, "Read error");
9983
9984 bcopy (tbr->bytes + tbr->index, data, length);
9985 tbr->index = tbr->index + length;
9986}
9987
6fc2811b
JR
9988/* Load PNG image IMG for use on frame F. Value is non-zero if
9989 successful. */
9990
9991static int
9992png_load (f, img)
9993 struct frame *f;
9994 struct image *img;
9995{
9996 Lisp_Object file, specified_file;
9997 Lisp_Object specified_data;
9998 int x, y, i;
9999 XImage *ximg, *mask_img = NULL;
10000 struct gcpro gcpro1;
10001 png_struct *png_ptr = NULL;
10002 png_info *info_ptr = NULL, *end_info = NULL;
10003 FILE *fp = NULL;
10004 png_byte sig[8];
10005 png_byte *pixels = NULL;
10006 png_byte **rows = NULL;
10007 png_uint_32 width, height;
10008 int bit_depth, color_type, interlace_type;
10009 png_byte channels;
10010 png_uint_32 row_bytes;
10011 int transparent_p;
10012 char *gamma_str;
10013 double screen_gamma, image_gamma;
10014 int intent;
10015 struct png_memory_storage tbr; /* Data to be read */
10016
10017 /* Find out what file to load. */
10018 specified_file = image_spec_value (img->spec, QCfile, NULL);
10019 specified_data = image_spec_value (img->spec, QCdata, NULL);
10020 file = Qnil;
10021 GCPRO1 (file);
10022
10023 if (NILP (specified_data))
10024 {
10025 file = x_find_image_file (specified_file);
10026 if (!STRINGP (file))
10027 {
10028 image_error ("Cannot find image file `%s'", specified_file, Qnil);
10029 UNGCPRO;
10030 return 0;
10031 }
10032
10033 /* Open the image file. */
10034 fp = fopen (XSTRING (file)->data, "rb");
10035 if (!fp)
10036 {
10037 image_error ("Cannot open image file `%s'", file, Qnil);
10038 UNGCPRO;
10039 fclose (fp);
10040 return 0;
10041 }
10042
10043 /* Check PNG signature. */
10044 if (fread (sig, 1, sizeof sig, fp) != sizeof sig
10045 || !png_check_sig (sig, sizeof sig))
10046 {
10047 image_error ("Not a PNG file:` %s'", file, Qnil);
10048 UNGCPRO;
10049 fclose (fp);
10050 return 0;
10051 }
10052 }
10053 else
10054 {
10055 /* Read from memory. */
10056 tbr.bytes = XSTRING (specified_data)->data;
10057 tbr.len = STRING_BYTES (XSTRING (specified_data));
10058 tbr.index = 0;
10059
10060 /* Check PNG signature. */
10061 if (tbr.len < sizeof sig
10062 || !png_check_sig (tbr.bytes, sizeof sig))
10063 {
10064 image_error ("Not a PNG image: `%s'", img->spec, Qnil);
10065 UNGCPRO;
10066 return 0;
10067 }
10068
10069 /* Need to skip past the signature. */
10070 tbr.bytes += sizeof (sig);
10071 }
10072
6fc2811b
JR
10073 /* Initialize read and info structs for PNG lib. */
10074 png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL,
10075 my_png_error, my_png_warning);
10076 if (!png_ptr)
10077 {
10078 if (fp) fclose (fp);
10079 UNGCPRO;
10080 return 0;
10081 }
10082
10083 info_ptr = png_create_info_struct (png_ptr);
10084 if (!info_ptr)
10085 {
10086 png_destroy_read_struct (&png_ptr, NULL, NULL);
10087 if (fp) fclose (fp);
10088 UNGCPRO;
10089 return 0;
10090 }
10091
10092 end_info = png_create_info_struct (png_ptr);
10093 if (!end_info)
10094 {
10095 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
10096 if (fp) fclose (fp);
10097 UNGCPRO;
10098 return 0;
10099 }
10100
10101 /* Set error jump-back. We come back here when the PNG library
10102 detects an error. */
10103 if (setjmp (png_ptr->jmpbuf))
10104 {
10105 error:
10106 if (png_ptr)
10107 png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
10108 xfree (pixels);
10109 xfree (rows);
10110 if (fp) fclose (fp);
10111 UNGCPRO;
10112 return 0;
10113 }
10114
10115 /* Read image info. */
10116 if (!NILP (specified_data))
10117 png_set_read_fn (png_ptr, (void *) &tbr, png_read_from_memory);
10118 else
10119 png_init_io (png_ptr, fp);
10120
10121 png_set_sig_bytes (png_ptr, sizeof sig);
10122 png_read_info (png_ptr, info_ptr);
10123 png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
10124 &interlace_type, NULL, NULL);
10125
10126 /* If image contains simply transparency data, we prefer to
10127 construct a clipping mask. */
10128 if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
10129 transparent_p = 1;
10130 else
10131 transparent_p = 0;
10132
10133 /* This function is easier to write if we only have to handle
10134 one data format: RGB or RGBA with 8 bits per channel. Let's
10135 transform other formats into that format. */
10136
10137 /* Strip more than 8 bits per channel. */
10138 if (bit_depth == 16)
10139 png_set_strip_16 (png_ptr);
10140
10141 /* Expand data to 24 bit RGB, or 8 bit grayscale, with alpha channel
10142 if available. */
10143 png_set_expand (png_ptr);
10144
10145 /* Convert grayscale images to RGB. */
10146 if (color_type == PNG_COLOR_TYPE_GRAY
10147 || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
10148 png_set_gray_to_rgb (png_ptr);
10149
10150 /* The value 2.2 is a guess for PC monitors from PNG example.c. */
10151 gamma_str = getenv ("SCREEN_GAMMA");
10152 screen_gamma = gamma_str ? atof (gamma_str) : 2.2;
10153
10154 /* Tell the PNG lib to handle gamma correction for us. */
10155
10156#if defined(PNG_READ_sRGB_SUPPORTED) || defined(PNG_WRITE_sRGB_SUPPORTED)
10157 if (png_get_sRGB (png_ptr, info_ptr, &intent))
10158 /* There is a special chunk in the image specifying the gamma. */
10159 png_set_sRGB (png_ptr, info_ptr, intent);
10160 else
10161#endif
10162 if (png_get_gAMA (png_ptr, info_ptr, &image_gamma))
10163 /* Image contains gamma information. */
10164 png_set_gamma (png_ptr, screen_gamma, image_gamma);
10165 else
10166 /* Use a default of 0.5 for the image gamma. */
10167 png_set_gamma (png_ptr, screen_gamma, 0.5);
10168
10169 /* Handle alpha channel by combining the image with a background
10170 color. Do this only if a real alpha channel is supplied. For
10171 simple transparency, we prefer a clipping mask. */
10172 if (!transparent_p)
10173 {
10174 png_color_16 *image_background;
10175
10176 if (png_get_bKGD (png_ptr, info_ptr, &image_background))
10177 /* Image contains a background color with which to
10178 combine the image. */
10179 png_set_background (png_ptr, image_background,
10180 PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
10181 else
10182 {
10183 /* Image does not contain a background color with which
10184 to combine the image data via an alpha channel. Use
10185 the frame's background instead. */
10186 XColor color;
10187 Colormap cmap;
10188 png_color_16 frame_background;
10189
10190 BLOCK_INPUT;
10191 cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f));
10192 color.pixel = FRAME_BACKGROUND_PIXEL (f);
10193 XQueryColor (FRAME_W32_DISPLAY (f), cmap, &color);
10194 UNBLOCK_INPUT;
10195
10196 bzero (&frame_background, sizeof frame_background);
10197 frame_background.red = color.red;
10198 frame_background.green = color.green;
10199 frame_background.blue = color.blue;
10200
10201 png_set_background (png_ptr, &frame_background,
10202 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
10203 }
10204 }
10205
10206 /* Update info structure. */
10207 png_read_update_info (png_ptr, info_ptr);
10208
10209 /* Get number of channels. Valid values are 1 for grayscale images
10210 and images with a palette, 2 for grayscale images with transparency
10211 information (alpha channel), 3 for RGB images, and 4 for RGB
10212 images with alpha channel, i.e. RGBA. If conversions above were
10213 sufficient we should only have 3 or 4 channels here. */
10214 channels = png_get_channels (png_ptr, info_ptr);
10215 xassert (channels == 3 || channels == 4);
10216
10217 /* Number of bytes needed for one row of the image. */
10218 row_bytes = png_get_rowbytes (png_ptr, info_ptr);
10219
10220 /* Allocate memory for the image. */
10221 pixels = (png_byte *) xmalloc (row_bytes * height * sizeof *pixels);
10222 rows = (png_byte **) xmalloc (height * sizeof *rows);
10223 for (i = 0; i < height; ++i)
10224 rows[i] = pixels + i * row_bytes;
10225
10226 /* Read the entire image. */
10227 png_read_image (png_ptr, rows);
10228 png_read_end (png_ptr, info_ptr);
10229 if (fp)
10230 {
10231 fclose (fp);
10232 fp = NULL;
10233 }
10234
10235 BLOCK_INPUT;
10236
10237 /* Create the X image and pixmap. */
10238 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg,
10239 &img->pixmap))
10240 {
10241 UNBLOCK_INPUT;
10242 goto error;
10243 }
10244
10245 /* Create an image and pixmap serving as mask if the PNG image
10246 contains an alpha channel. */
10247 if (channels == 4
10248 && !transparent_p
10249 && !x_create_x_image_and_pixmap (f, width, height, 1,
10250 &mask_img, &img->mask))
10251 {
10252 x_destroy_x_image (ximg);
10253 XFreePixmap (FRAME_W32_DISPLAY (f), img->pixmap);
10254 img->pixmap = 0;
10255 UNBLOCK_INPUT;
10256 goto error;
10257 }
10258
10259 /* Fill the X image and mask from PNG data. */
10260 init_color_table ();
10261
10262 for (y = 0; y < height; ++y)
10263 {
10264 png_byte *p = rows[y];
10265
10266 for (x = 0; x < width; ++x)
10267 {
10268 unsigned r, g, b;
10269
10270 r = *p++ << 8;
10271 g = *p++ << 8;
10272 b = *p++ << 8;
10273 XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b));
10274
10275 /* An alpha channel, aka mask channel, associates variable
10276 transparency with an image. Where other image formats
10277 support binary transparency---fully transparent or fully
10278 opaque---PNG allows up to 254 levels of partial transparency.
10279 The PNG library implements partial transparency by combining
10280 the image with a specified background color.
10281
10282 I'm not sure how to handle this here nicely: because the
10283 background on which the image is displayed may change, for
10284 real alpha channel support, it would be necessary to create
10285 a new image for each possible background.
10286
10287 What I'm doing now is that a mask is created if we have
10288 boolean transparency information. Otherwise I'm using
10289 the frame's background color to combine the image with. */
10290
10291 if (channels == 4)
10292 {
10293 if (mask_img)
10294 XPutPixel (mask_img, x, y, *p > 0);
10295 ++p;
10296 }
10297 }
10298 }
10299
10300 /* Remember colors allocated for this image. */
10301 img->colors = colors_in_color_table (&img->ncolors);
10302 free_color_table ();
10303
10304 /* Clean up. */
10305 png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
10306 xfree (rows);
10307 xfree (pixels);
10308
10309 img->width = width;
10310 img->height = height;
10311
10312 /* Put the image into the pixmap, then free the X image and its buffer. */
10313 x_put_x_image (f, ximg, img->pixmap, width, height);
10314 x_destroy_x_image (ximg);
10315
10316 /* Same for the mask. */
10317 if (mask_img)
10318 {
10319 x_put_x_image (f, mask_img, img->mask, img->width, img->height);
10320 x_destroy_x_image (mask_img);
10321 }
10322
10323 UNBLOCK_INPUT;
10324 UNGCPRO;
10325 return 1;
10326}
10327
10328#endif /* HAVE_PNG != 0 */
10329
10330
10331\f
10332/***********************************************************************
10333 JPEG
10334 ***********************************************************************/
10335
10336#if HAVE_JPEG
10337
10338/* Work around a warning about HAVE_STDLIB_H being redefined in
10339 jconfig.h. */
10340#ifdef HAVE_STDLIB_H
10341#define HAVE_STDLIB_H_1
10342#undef HAVE_STDLIB_H
10343#endif /* HAVE_STLIB_H */
10344
10345#include <jpeglib.h>
10346#include <jerror.h>
10347#include <setjmp.h>
10348
10349#ifdef HAVE_STLIB_H_1
10350#define HAVE_STDLIB_H 1
10351#endif
10352
10353static int jpeg_image_p P_ ((Lisp_Object object));
10354static int jpeg_load P_ ((struct frame *f, struct image *img));
10355
10356/* The symbol `jpeg' identifying images of this type. */
10357
10358Lisp_Object Qjpeg;
10359
10360/* Indices of image specification fields in gs_format, below. */
10361
10362enum jpeg_keyword_index
10363{
10364 JPEG_TYPE,
10365 JPEG_DATA,
10366 JPEG_FILE,
10367 JPEG_ASCENT,
10368 JPEG_MARGIN,
10369 JPEG_RELIEF,
10370 JPEG_ALGORITHM,
10371 JPEG_HEURISTIC_MASK,
10372 JPEG_LAST
10373};
10374
10375/* Vector of image_keyword structures describing the format
10376 of valid user-defined image specifications. */
10377
10378static struct image_keyword jpeg_format[JPEG_LAST] =
10379{
10380 {":type", IMAGE_SYMBOL_VALUE, 1},
10381 {":data", IMAGE_STRING_VALUE, 0},
10382 {":file", IMAGE_STRING_VALUE, 0},
10383 {":ascent", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
10384 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
10385 {":relief", IMAGE_INTEGER_VALUE, 0},
10386 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
10387 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
10388};
10389
10390/* Structure describing the image type `jpeg'. */
10391
10392static struct image_type jpeg_type =
10393{
10394 &Qjpeg,
10395 jpeg_image_p,
10396 jpeg_load,
10397 x_clear_image,
10398 NULL
10399};
10400
10401
10402/* Return non-zero if OBJECT is a valid JPEG image specification. */
10403
10404static int
10405jpeg_image_p (object)
10406 Lisp_Object object;
10407{
10408 struct image_keyword fmt[JPEG_LAST];
10409
10410 bcopy (jpeg_format, fmt, sizeof fmt);
10411
10412 if (!parse_image_spec (object, fmt, JPEG_LAST, Qjpeg)
10413 || (fmt[JPEG_ASCENT].count
10414 && XFASTINT (fmt[JPEG_ASCENT].value) > 100))
10415 return 0;
10416
10417 /* Must specify either the :data or :file keyword. */
10418 return fmt[JPEG_FILE].count + fmt[JPEG_DATA].count == 1;
10419}
10420
10421
10422struct my_jpeg_error_mgr
10423{
10424 struct jpeg_error_mgr pub;
10425 jmp_buf setjmp_buffer;
10426};
10427
10428static void
10429my_error_exit (cinfo)
10430 j_common_ptr cinfo;
10431{
10432 struct my_jpeg_error_mgr *mgr = (struct my_jpeg_error_mgr *) cinfo->err;
10433 longjmp (mgr->setjmp_buffer, 1);
10434}
10435
6fc2811b
JR
10436/* Init source method for JPEG data source manager. Called by
10437 jpeg_read_header() before any data is actually read. See
10438 libjpeg.doc from the JPEG lib distribution. */
10439
10440static void
10441our_init_source (cinfo)
10442 j_decompress_ptr cinfo;
10443{
10444}
10445
10446
10447/* Fill input buffer method for JPEG data source manager. Called
10448 whenever more data is needed. We read the whole image in one step,
10449 so this only adds a fake end of input marker at the end. */
10450
10451static boolean
10452our_fill_input_buffer (cinfo)
10453 j_decompress_ptr cinfo;
10454{
10455 /* Insert a fake EOI marker. */
10456 struct jpeg_source_mgr *src = cinfo->src;
10457 static JOCTET buffer[2];
10458
10459 buffer[0] = (JOCTET) 0xFF;
10460 buffer[1] = (JOCTET) JPEG_EOI;
10461
10462 src->next_input_byte = buffer;
10463 src->bytes_in_buffer = 2;
10464 return TRUE;
10465}
10466
10467
10468/* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
10469 is the JPEG data source manager. */
10470
10471static void
10472our_skip_input_data (cinfo, num_bytes)
10473 j_decompress_ptr cinfo;
10474 long num_bytes;
10475{
10476 struct jpeg_source_mgr *src = (struct jpeg_source_mgr *) cinfo->src;
10477
10478 if (src)
10479 {
10480 if (num_bytes > src->bytes_in_buffer)
10481 ERREXIT (cinfo, JERR_INPUT_EOF);
10482
10483 src->bytes_in_buffer -= num_bytes;
10484 src->next_input_byte += num_bytes;
10485 }
10486}
10487
10488
10489/* Method to terminate data source. Called by
10490 jpeg_finish_decompress() after all data has been processed. */
10491
10492static void
10493our_term_source (cinfo)
10494 j_decompress_ptr cinfo;
10495{
10496}
10497
10498
10499/* Set up the JPEG lib for reading an image from DATA which contains
10500 LEN bytes. CINFO is the decompression info structure created for
10501 reading the image. */
10502
10503static void
10504jpeg_memory_src (cinfo, data, len)
10505 j_decompress_ptr cinfo;
10506 JOCTET *data;
10507 unsigned int len;
10508{
10509 struct jpeg_source_mgr *src;
10510
10511 if (cinfo->src == NULL)
10512 {
10513 /* First time for this JPEG object? */
10514 cinfo->src = (struct jpeg_source_mgr *)
10515 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
10516 sizeof (struct jpeg_source_mgr));
10517 src = (struct jpeg_source_mgr *) cinfo->src;
10518 src->next_input_byte = data;
10519 }
10520
10521 src = (struct jpeg_source_mgr *) cinfo->src;
10522 src->init_source = our_init_source;
10523 src->fill_input_buffer = our_fill_input_buffer;
10524 src->skip_input_data = our_skip_input_data;
10525 src->resync_to_restart = jpeg_resync_to_restart; /* Use default method. */
10526 src->term_source = our_term_source;
10527 src->bytes_in_buffer = len;
10528 src->next_input_byte = data;
10529}
10530
10531
10532/* Load image IMG for use on frame F. Patterned after example.c
10533 from the JPEG lib. */
10534
10535static int
10536jpeg_load (f, img)
10537 struct frame *f;
10538 struct image *img;
10539{
10540 struct jpeg_decompress_struct cinfo;
10541 struct my_jpeg_error_mgr mgr;
10542 Lisp_Object file, specified_file;
10543 Lisp_Object specified_data;
10544 FILE *fp = NULL;
10545 JSAMPARRAY buffer;
10546 int row_stride, x, y;
10547 XImage *ximg = NULL;
10548 int rc;
10549 unsigned long *colors;
10550 int width, height;
10551 struct gcpro gcpro1;
10552
10553 /* Open the JPEG file. */
10554 specified_file = image_spec_value (img->spec, QCfile, NULL);
10555 specified_data = image_spec_value (img->spec, QCdata, NULL);
10556 file = Qnil;
10557 GCPRO1 (file);
10558
6fc2811b
JR
10559 if (NILP (specified_data))
10560 {
10561 file = x_find_image_file (specified_file);
10562 if (!STRINGP (file))
10563 {
10564 image_error ("Cannot find image file `%s'", specified_file, Qnil);
10565 UNGCPRO;
10566 return 0;
10567 }
10568
10569 fp = fopen (XSTRING (file)->data, "r");
10570 if (fp == NULL)
10571 {
10572 image_error ("Cannot open `%s'", file, Qnil);
10573 UNGCPRO;
10574 return 0;
10575 }
10576 }
10577
10578 /* Customize libjpeg's error handling to call my_error_exit when an
10579 error is detected. This function will perform a longjmp. */
10580 mgr.pub.error_exit = my_error_exit;
10581 cinfo.err = jpeg_std_error (&mgr.pub);
10582
10583 if ((rc = setjmp (mgr.setjmp_buffer)) != 0)
10584 {
10585 if (rc == 1)
10586 {
10587 /* Called from my_error_exit. Display a JPEG error. */
10588 char buffer[JMSG_LENGTH_MAX];
10589 cinfo.err->format_message ((j_common_ptr) &cinfo, buffer);
10590 image_error ("Error reading JPEG image `%s': %s", img->spec,
10591 build_string (buffer));
10592 }
10593
10594 /* Close the input file and destroy the JPEG object. */
10595 if (fp)
10596 fclose (fp);
10597 jpeg_destroy_decompress (&cinfo);
10598
10599 BLOCK_INPUT;
10600
10601 /* If we already have an XImage, free that. */
10602 x_destroy_x_image (ximg);
10603
10604 /* Free pixmap and colors. */
10605 x_clear_image (f, img);
10606
10607 UNBLOCK_INPUT;
10608 UNGCPRO;
10609 return 0;
10610 }
10611
10612 /* Create the JPEG decompression object. Let it read from fp.
10613 Read the JPEG image header. */
10614 jpeg_create_decompress (&cinfo);
10615
10616 if (NILP (specified_data))
10617 jpeg_stdio_src (&cinfo, fp);
10618 else
10619 jpeg_memory_src (&cinfo, XSTRING (specified_data)->data,
10620 STRING_BYTES (XSTRING (specified_data)));
10621
10622 jpeg_read_header (&cinfo, TRUE);
10623
10624 /* Customize decompression so that color quantization will be used.
10625 Start decompression. */
10626 cinfo.quantize_colors = TRUE;
10627 jpeg_start_decompress (&cinfo);
10628 width = img->width = cinfo.output_width;
10629 height = img->height = cinfo.output_height;
10630
10631 BLOCK_INPUT;
10632
10633 /* Create X image and pixmap. */
10634 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg,
10635 &img->pixmap))
10636 {
10637 UNBLOCK_INPUT;
10638 longjmp (mgr.setjmp_buffer, 2);
10639 }
10640
10641 /* Allocate colors. When color quantization is used,
10642 cinfo.actual_number_of_colors has been set with the number of
10643 colors generated, and cinfo.colormap is a two-dimensional array
10644 of color indices in the range 0..cinfo.actual_number_of_colors.
10645 No more than 255 colors will be generated. */
10646 {
10647 int i, ir, ig, ib;
10648
10649 if (cinfo.out_color_components > 2)
10650 ir = 0, ig = 1, ib = 2;
10651 else if (cinfo.out_color_components > 1)
10652 ir = 0, ig = 1, ib = 0;
10653 else
10654 ir = 0, ig = 0, ib = 0;
10655
10656 /* Use the color table mechanism because it handles colors that
10657 cannot be allocated nicely. Such colors will be replaced with
10658 a default color, and we don't have to care about which colors
10659 can be freed safely, and which can't. */
10660 init_color_table ();
10661 colors = (unsigned long *) alloca (cinfo.actual_number_of_colors
10662 * sizeof *colors);
10663
10664 for (i = 0; i < cinfo.actual_number_of_colors; ++i)
10665 {
10666 /* Multiply RGB values with 255 because X expects RGB values
10667 in the range 0..0xffff. */
10668 int r = cinfo.colormap[ir][i] << 8;
10669 int g = cinfo.colormap[ig][i] << 8;
10670 int b = cinfo.colormap[ib][i] << 8;
10671 colors[i] = lookup_rgb_color (f, r, g, b);
10672 }
10673
10674 /* Remember those colors actually allocated. */
10675 img->colors = colors_in_color_table (&img->ncolors);
10676 free_color_table ();
10677 }
10678
10679 /* Read pixels. */
10680 row_stride = width * cinfo.output_components;
10681 buffer = cinfo.mem->alloc_sarray ((j_common_ptr) &cinfo, JPOOL_IMAGE,
10682 row_stride, 1);
10683 for (y = 0; y < height; ++y)
10684 {
10685 jpeg_read_scanlines (&cinfo, buffer, 1);
10686 for (x = 0; x < cinfo.output_width; ++x)
10687 XPutPixel (ximg, x, y, colors[buffer[0][x]]);
10688 }
10689
10690 /* Clean up. */
10691 jpeg_finish_decompress (&cinfo);
10692 jpeg_destroy_decompress (&cinfo);
10693 if (fp)
10694 fclose (fp);
10695
10696 /* Put the image into the pixmap. */
10697 x_put_x_image (f, ximg, img->pixmap, width, height);
10698 x_destroy_x_image (ximg);
10699 UNBLOCK_INPUT;
10700 UNGCPRO;
10701 return 1;
10702}
10703
10704#endif /* HAVE_JPEG */
10705
10706
10707\f
10708/***********************************************************************
10709 TIFF
10710 ***********************************************************************/
10711
10712#if HAVE_TIFF
10713
10714#include <tiffio.h>
10715
10716static int tiff_image_p P_ ((Lisp_Object object));
10717static int tiff_load P_ ((struct frame *f, struct image *img));
10718
10719/* The symbol `tiff' identifying images of this type. */
10720
10721Lisp_Object Qtiff;
10722
10723/* Indices of image specification fields in tiff_format, below. */
10724
10725enum tiff_keyword_index
10726{
10727 TIFF_TYPE,
10728 TIFF_DATA,
10729 TIFF_FILE,
10730 TIFF_ASCENT,
10731 TIFF_MARGIN,
10732 TIFF_RELIEF,
10733 TIFF_ALGORITHM,
10734 TIFF_HEURISTIC_MASK,
10735 TIFF_LAST
10736};
10737
10738/* Vector of image_keyword structures describing the format
10739 of valid user-defined image specifications. */
10740
10741static struct image_keyword tiff_format[TIFF_LAST] =
10742{
10743 {":type", IMAGE_SYMBOL_VALUE, 1},
10744 {":data", IMAGE_STRING_VALUE, 0},
10745 {":file", IMAGE_STRING_VALUE, 0},
10746 {":ascent", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
10747 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
10748 {":relief", IMAGE_INTEGER_VALUE, 0},
10749 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
10750 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
10751};
10752
10753/* Structure describing the image type `tiff'. */
10754
10755static struct image_type tiff_type =
10756{
10757 &Qtiff,
10758 tiff_image_p,
10759 tiff_load,
10760 x_clear_image,
10761 NULL
10762};
10763
10764
10765/* Return non-zero if OBJECT is a valid TIFF image specification. */
10766
10767static int
10768tiff_image_p (object)
10769 Lisp_Object object;
10770{
10771 struct image_keyword fmt[TIFF_LAST];
10772 bcopy (tiff_format, fmt, sizeof fmt);
10773
10774 if (!parse_image_spec (object, fmt, TIFF_LAST, Qtiff)
10775 || (fmt[TIFF_ASCENT].count
10776 && XFASTINT (fmt[TIFF_ASCENT].value) > 100))
10777 return 0;
10778
10779 /* Must specify either the :data or :file keyword. */
10780 return fmt[TIFF_FILE].count + fmt[TIFF_DATA].count == 1;
10781}
10782
10783
10784/* Reading from a memory buffer for TIFF images Based on the PNG
10785 memory source, but we have to provide a lot of extra functions.
10786 Blah.
10787
10788 We really only need to implement read and seek, but I am not
10789 convinced that the TIFF library is smart enough not to destroy
10790 itself if we only hand it the function pointers we need to
10791 override. */
10792
10793typedef struct
10794{
10795 unsigned char *bytes;
10796 size_t len;
10797 int index;
10798}
10799tiff_memory_source;
10800
10801static size_t
10802tiff_read_from_memory (data, buf, size)
10803 thandle_t data;
10804 tdata_t buf;
10805 tsize_t size;
10806{
10807 tiff_memory_source *src = (tiff_memory_source *) data;
10808
10809 if (size > src->len - src->index)
10810 return (size_t) -1;
10811 bcopy (src->bytes + src->index, buf, size);
10812 src->index += size;
10813 return size;
10814}
10815
10816static size_t
10817tiff_write_from_memory (data, buf, size)
10818 thandle_t data;
10819 tdata_t buf;
10820 tsize_t size;
10821{
10822 return (size_t) -1;
10823}
10824
10825static toff_t
10826tiff_seek_in_memory (data, off, whence)
10827 thandle_t data;
10828 toff_t off;
10829 int whence;
10830{
10831 tiff_memory_source *src = (tiff_memory_source *) data;
10832 int idx;
10833
10834 switch (whence)
10835 {
10836 case SEEK_SET: /* Go from beginning of source. */
10837 idx = off;
10838 break;
10839
10840 case SEEK_END: /* Go from end of source. */
10841 idx = src->len + off;
10842 break;
10843
10844 case SEEK_CUR: /* Go from current position. */
10845 idx = src->index + off;
10846 break;
10847
10848 default: /* Invalid `whence'. */
10849 return -1;
10850 }
10851
10852 if (idx > src->len || idx < 0)
10853 return -1;
10854
10855 src->index = idx;
10856 return src->index;
10857}
10858
10859static int
10860tiff_close_memory (data)
10861 thandle_t data;
10862{
10863 /* NOOP */
10864 return 0;
10865}
10866
10867static int
10868tiff_mmap_memory (data, pbase, psize)
10869 thandle_t data;
10870 tdata_t *pbase;
10871 toff_t *psize;
10872{
10873 /* It is already _IN_ memory. */
10874 return 0;
10875}
10876
10877static void
10878tiff_unmap_memory (data, base, size)
10879 thandle_t data;
10880 tdata_t base;
10881 toff_t size;
10882{
10883 /* We don't need to do this. */
10884}
10885
10886static toff_t
10887tiff_size_of_memory (data)
10888 thandle_t data;
10889{
10890 return ((tiff_memory_source *) data)->len;
10891}
10892
6fc2811b
JR
10893/* Load TIFF image IMG for use on frame F. Value is non-zero if
10894 successful. */
10895
10896static int
10897tiff_load (f, img)
10898 struct frame *f;
10899 struct image *img;
10900{
10901 Lisp_Object file, specified_file;
10902 Lisp_Object specified_data;
10903 TIFF *tiff;
10904 int width, height, x, y;
10905 uint32 *buf;
10906 int rc;
10907 XImage *ximg;
10908 struct gcpro gcpro1;
10909 tiff_memory_source memsrc;
10910
10911 specified_file = image_spec_value (img->spec, QCfile, NULL);
10912 specified_data = image_spec_value (img->spec, QCdata, NULL);
10913 file = Qnil;
10914 GCPRO1 (file);
10915
10916 if (NILP (specified_data))
10917 {
10918 /* Read from a file */
10919 file = x_find_image_file (specified_file);
10920 if (!STRINGP (file))
10921 {
10922 image_error ("Cannot find image file `%s'", file, Qnil);
10923 UNGCPRO;
10924 return 0;
10925 }
10926
10927 /* Try to open the image file. */
10928 tiff = TIFFOpen (XSTRING (file)->data, "r");
10929 if (tiff == NULL)
10930 {
10931 image_error ("Cannot open `%s'", file, Qnil);
10932 UNGCPRO;
10933 return 0;
10934 }
10935 }
10936 else
10937 {
10938 /* Memory source! */
10939 memsrc.bytes = XSTRING (specified_data)->data;
10940 memsrc.len = STRING_BYTES (XSTRING (specified_data));
10941 memsrc.index = 0;
10942
10943 tiff = TIFFClientOpen ("memory_source", "r", &memsrc,
10944 (TIFFReadWriteProc) tiff_read_from_memory,
10945 (TIFFReadWriteProc) tiff_write_from_memory,
10946 tiff_seek_in_memory,
10947 tiff_close_memory,
10948 tiff_size_of_memory,
10949 tiff_mmap_memory,
10950 tiff_unmap_memory);
10951
10952 if (!tiff)
10953 {
10954 image_error ("Cannot open memory source for `%s'", img->spec, Qnil);
10955 UNGCPRO;
10956 return 0;
10957 }
10958 }
10959
10960 /* Get width and height of the image, and allocate a raster buffer
10961 of width x height 32-bit values. */
10962 TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &width);
10963 TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &height);
10964 buf = (uint32 *) xmalloc (width * height * sizeof *buf);
10965
10966 rc = TIFFReadRGBAImage (tiff, width, height, buf, 0);
10967 TIFFClose (tiff);
10968 if (!rc)
10969 {
10970 image_error ("Error reading TIFF image `%s'", img->spec, Qnil);
10971 xfree (buf);
10972 UNGCPRO;
10973 return 0;
10974 }
10975
10976 BLOCK_INPUT;
10977
10978 /* Create the X image and pixmap. */
10979 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
10980 {
10981 UNBLOCK_INPUT;
10982 xfree (buf);
10983 UNGCPRO;
10984 return 0;
10985 }
10986
10987 /* Initialize the color table. */
10988 init_color_table ();
10989
10990 /* Process the pixel raster. Origin is in the lower-left corner. */
10991 for (y = 0; y < height; ++y)
10992 {
10993 uint32 *row = buf + y * width;
10994
10995 for (x = 0; x < width; ++x)
10996 {
10997 uint32 abgr = row[x];
10998 int r = TIFFGetR (abgr) << 8;
10999 int g = TIFFGetG (abgr) << 8;
11000 int b = TIFFGetB (abgr) << 8;
11001 XPutPixel (ximg, x, height - 1 - y, lookup_rgb_color (f, r, g, b));
11002 }
11003 }
11004
11005 /* Remember the colors allocated for the image. Free the color table. */
11006 img->colors = colors_in_color_table (&img->ncolors);
11007 free_color_table ();
11008
11009 /* Put the image into the pixmap, then free the X image and its buffer. */
11010 x_put_x_image (f, ximg, img->pixmap, width, height);
11011 x_destroy_x_image (ximg);
11012 xfree (buf);
11013 UNBLOCK_INPUT;
11014
11015 img->width = width;
11016 img->height = height;
11017
11018 UNGCPRO;
11019 return 1;
11020}
11021
11022#endif /* HAVE_TIFF != 0 */
11023
11024
11025\f
11026/***********************************************************************
11027 GIF
11028 ***********************************************************************/
11029
11030#if HAVE_GIF
11031
11032#include <gif_lib.h>
11033
11034static int gif_image_p P_ ((Lisp_Object object));
11035static int gif_load P_ ((struct frame *f, struct image *img));
11036
11037/* The symbol `gif' identifying images of this type. */
11038
11039Lisp_Object Qgif;
11040
11041/* Indices of image specification fields in gif_format, below. */
11042
11043enum gif_keyword_index
11044{
11045 GIF_TYPE,
11046 GIF_DATA,
11047 GIF_FILE,
11048 GIF_ASCENT,
11049 GIF_MARGIN,
11050 GIF_RELIEF,
11051 GIF_ALGORITHM,
11052 GIF_HEURISTIC_MASK,
11053 GIF_IMAGE,
11054 GIF_LAST
11055};
11056
11057/* Vector of image_keyword structures describing the format
11058 of valid user-defined image specifications. */
11059
11060static struct image_keyword gif_format[GIF_LAST] =
11061{
11062 {":type", IMAGE_SYMBOL_VALUE, 1},
11063 {":data", IMAGE_STRING_VALUE, 0},
11064 {":file", IMAGE_STRING_VALUE, 0},
11065 {":ascent", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
11066 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
11067 {":relief", IMAGE_INTEGER_VALUE, 0},
11068 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
11069 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
11070 {":image", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0}
11071};
11072
11073/* Structure describing the image type `gif'. */
11074
11075static struct image_type gif_type =
11076{
11077 &Qgif,
11078 gif_image_p,
11079 gif_load,
11080 x_clear_image,
11081 NULL
11082};
11083
11084/* Return non-zero if OBJECT is a valid GIF image specification. */
11085
11086static int
11087gif_image_p (object)
11088 Lisp_Object object;
11089{
11090 struct image_keyword fmt[GIF_LAST];
11091 bcopy (gif_format, fmt, sizeof fmt);
11092
11093 if (!parse_image_spec (object, fmt, GIF_LAST, Qgif)
11094 || (fmt[GIF_ASCENT].count
11095 && XFASTINT (fmt[GIF_ASCENT].value) > 100))
11096 return 0;
11097
11098 /* Must specify either the :data or :file keyword. */
11099 return fmt[GIF_FILE].count + fmt[GIF_DATA].count == 1;
11100}
11101
11102/* Reading a GIF image from memory
11103 Based on the PNG memory stuff to a certain extent. */
11104
11105typedef struct
11106{
11107 unsigned char *bytes;
11108 size_t len;
11109 int index;
11110}
11111gif_memory_source;
11112
11113/* Make the current memory source available to gif_read_from_memory.
11114 It's done this way because not all versions of libungif support
11115 a UserData field in the GifFileType structure. */
11116static gif_memory_source *current_gif_memory_src;
11117
11118static int
11119gif_read_from_memory (file, buf, len)
11120 GifFileType *file;
11121 GifByteType *buf;
11122 int len;
11123{
11124 gif_memory_source *src = current_gif_memory_src;
11125
11126 if (len > src->len - src->index)
11127 return -1;
11128
11129 bcopy (src->bytes + src->index, buf, len);
11130 src->index += len;
11131 return len;
11132}
11133
11134
11135/* Load GIF image IMG for use on frame F. Value is non-zero if
11136 successful. */
11137
11138static int
11139gif_load (f, img)
11140 struct frame *f;
11141 struct image *img;
11142{
11143 Lisp_Object file, specified_file;
11144 Lisp_Object specified_data;
11145 int rc, width, height, x, y, i;
11146 XImage *ximg;
11147 ColorMapObject *gif_color_map;
11148 unsigned long pixel_colors[256];
11149 GifFileType *gif;
11150 struct gcpro gcpro1;
11151 Lisp_Object image;
11152 int ino, image_left, image_top, image_width, image_height;
11153 gif_memory_source memsrc;
11154 unsigned char *raster;
11155
11156 specified_file = image_spec_value (img->spec, QCfile, NULL);
11157 specified_data = image_spec_value (img->spec, QCdata, NULL);
11158 file = Qnil;
dfff8a69 11159 GCPRO1 (file);
6fc2811b
JR
11160
11161 if (NILP (specified_data))
11162 {
11163 file = x_find_image_file (specified_file);
6fc2811b
JR
11164 if (!STRINGP (file))
11165 {
11166 image_error ("Cannot find image file `%s'", specified_file, Qnil);
11167 UNGCPRO;
11168 return 0;
11169 }
11170
11171 /* Open the GIF file. */
11172 gif = DGifOpenFileName (XSTRING (file)->data);
11173 if (gif == NULL)
11174 {
11175 image_error ("Cannot open `%s'", file, Qnil);
11176 UNGCPRO;
11177 return 0;
11178 }
11179 }
11180 else
11181 {
11182 /* Read from memory! */
11183 current_gif_memory_src = &memsrc;
11184 memsrc.bytes = XSTRING (specified_data)->data;
11185 memsrc.len = STRING_BYTES (XSTRING (specified_data));
11186 memsrc.index = 0;
11187
11188 gif = DGifOpen(&memsrc, gif_read_from_memory);
11189 if (!gif)
11190 {
11191 image_error ("Cannot open memory source `%s'", img->spec, Qnil);
11192 UNGCPRO;
11193 return 0;
11194 }
11195 }
11196
11197 /* Read entire contents. */
11198 rc = DGifSlurp (gif);
11199 if (rc == GIF_ERROR)
11200 {
11201 image_error ("Error reading `%s'", img->spec, Qnil);
11202 DGifCloseFile (gif);
11203 UNGCPRO;
11204 return 0;
11205 }
11206
11207 image = image_spec_value (img->spec, QCindex, NULL);
11208 ino = INTEGERP (image) ? XFASTINT (image) : 0;
11209 if (ino >= gif->ImageCount)
11210 {
11211 image_error ("Invalid image number `%s' in image `%s'",
11212 image, img->spec);
11213 DGifCloseFile (gif);
11214 UNGCPRO;
11215 return 0;
11216 }
11217
11218 width = img->width = gif->SWidth;
11219 height = img->height = gif->SHeight;
11220
11221 BLOCK_INPUT;
11222
11223 /* Create the X image and pixmap. */
11224 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
11225 {
11226 UNBLOCK_INPUT;
11227 DGifCloseFile (gif);
11228 UNGCPRO;
11229 return 0;
11230 }
11231
11232 /* Allocate colors. */
11233 gif_color_map = gif->SavedImages[ino].ImageDesc.ColorMap;
11234 if (!gif_color_map)
11235 gif_color_map = gif->SColorMap;
11236 init_color_table ();
11237 bzero (pixel_colors, sizeof pixel_colors);
11238
11239 for (i = 0; i < gif_color_map->ColorCount; ++i)
11240 {
11241 int r = gif_color_map->Colors[i].Red << 8;
11242 int g = gif_color_map->Colors[i].Green << 8;
11243 int b = gif_color_map->Colors[i].Blue << 8;
11244 pixel_colors[i] = lookup_rgb_color (f, r, g, b);
11245 }
11246
11247 img->colors = colors_in_color_table (&img->ncolors);
11248 free_color_table ();
11249
11250 /* Clear the part of the screen image that are not covered by
11251 the image from the GIF file. Full animated GIF support
11252 requires more than can be done here (see the gif89 spec,
11253 disposal methods). Let's simply assume that the part
11254 not covered by a sub-image is in the frame's background color. */
11255 image_top = gif->SavedImages[ino].ImageDesc.Top;
11256 image_left = gif->SavedImages[ino].ImageDesc.Left;
11257 image_width = gif->SavedImages[ino].ImageDesc.Width;
11258 image_height = gif->SavedImages[ino].ImageDesc.Height;
11259
11260 for (y = 0; y < image_top; ++y)
11261 for (x = 0; x < width; ++x)
11262 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
11263
11264 for (y = image_top + image_height; y < height; ++y)
11265 for (x = 0; x < width; ++x)
11266 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
11267
11268 for (y = image_top; y < image_top + image_height; ++y)
11269 {
11270 for (x = 0; x < image_left; ++x)
11271 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
11272 for (x = image_left + image_width; x < width; ++x)
11273 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
11274 }
11275
11276 /* Read the GIF image into the X image. We use a local variable
11277 `raster' here because RasterBits below is a char *, and invites
11278 problems with bytes >= 0x80. */
11279 raster = (unsigned char *) gif->SavedImages[ino].RasterBits;
11280
11281 if (gif->SavedImages[ino].ImageDesc.Interlace)
11282 {
11283 static int interlace_start[] = {0, 4, 2, 1};
11284 static int interlace_increment[] = {8, 8, 4, 2};
11285 int pass, inc;
11286 int row = interlace_start[0];
11287
11288 pass = 0;
11289
11290 for (y = 0; y < image_height; y++)
11291 {
11292 if (row >= image_height)
11293 {
11294 row = interlace_start[++pass];
11295 while (row >= image_height)
11296 row = interlace_start[++pass];
11297 }
11298
11299 for (x = 0; x < image_width; x++)
11300 {
11301 int i = raster[(y * image_width) + x];
11302 XPutPixel (ximg, x + image_left, row + image_top,
11303 pixel_colors[i]);
11304 }
11305
11306 row += interlace_increment[pass];
11307 }
11308 }
11309 else
11310 {
11311 for (y = 0; y < image_height; ++y)
11312 for (x = 0; x < image_width; ++x)
11313 {
11314 int i = raster[y* image_width + x];
11315 XPutPixel (ximg, x + image_left, y + image_top, pixel_colors[i]);
11316 }
11317 }
11318
11319 DGifCloseFile (gif);
11320
11321 /* Put the image into the pixmap, then free the X image and its buffer. */
11322 x_put_x_image (f, ximg, img->pixmap, width, height);
11323 x_destroy_x_image (ximg);
11324 UNBLOCK_INPUT;
11325
11326 UNGCPRO;
11327 return 1;
11328}
11329
11330#endif /* HAVE_GIF != 0 */
11331
11332
11333\f
11334/***********************************************************************
11335 Ghostscript
11336 ***********************************************************************/
11337
11338#ifdef HAVE_GHOSTSCRIPT
11339static int gs_image_p P_ ((Lisp_Object object));
11340static int gs_load P_ ((struct frame *f, struct image *img));
11341static void gs_clear_image P_ ((struct frame *f, struct image *img));
11342
11343/* The symbol `postscript' identifying images of this type. */
11344
11345Lisp_Object Qpostscript;
11346
11347/* Keyword symbols. */
11348
11349Lisp_Object QCloader, QCbounding_box, QCpt_width, QCpt_height;
11350
11351/* Indices of image specification fields in gs_format, below. */
11352
11353enum gs_keyword_index
11354{
11355 GS_TYPE,
11356 GS_PT_WIDTH,
11357 GS_PT_HEIGHT,
11358 GS_FILE,
11359 GS_LOADER,
11360 GS_BOUNDING_BOX,
11361 GS_ASCENT,
11362 GS_MARGIN,
11363 GS_RELIEF,
11364 GS_ALGORITHM,
11365 GS_HEURISTIC_MASK,
11366 GS_LAST
11367};
11368
11369/* Vector of image_keyword structures describing the format
11370 of valid user-defined image specifications. */
11371
11372static struct image_keyword gs_format[GS_LAST] =
11373{
11374 {":type", IMAGE_SYMBOL_VALUE, 1},
11375 {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE, 1},
11376 {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE, 1},
11377 {":file", IMAGE_STRING_VALUE, 1},
11378 {":loader", IMAGE_FUNCTION_VALUE, 0},
11379 {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE, 1},
11380 {":ascent", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
11381 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
11382 {":relief", IMAGE_INTEGER_VALUE, 0},
11383 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
11384 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
11385};
11386
11387/* Structure describing the image type `ghostscript'. */
11388
11389static struct image_type gs_type =
11390{
11391 &Qpostscript,
11392 gs_image_p,
11393 gs_load,
11394 gs_clear_image,
11395 NULL
11396};
11397
11398
11399/* Free X resources of Ghostscript image IMG which is used on frame F. */
11400
11401static void
11402gs_clear_image (f, img)
11403 struct frame *f;
11404 struct image *img;
11405{
11406 /* IMG->data.ptr_val may contain a recorded colormap. */
11407 xfree (img->data.ptr_val);
11408 x_clear_image (f, img);
11409}
11410
11411
11412/* Return non-zero if OBJECT is a valid Ghostscript image
11413 specification. */
11414
11415static int
11416gs_image_p (object)
11417 Lisp_Object object;
11418{
11419 struct image_keyword fmt[GS_LAST];
11420 Lisp_Object tem;
11421 int i;
11422
11423 bcopy (gs_format, fmt, sizeof fmt);
11424
11425 if (!parse_image_spec (object, fmt, GS_LAST, Qpostscript)
11426 || (fmt[GS_ASCENT].count
11427 && XFASTINT (fmt[GS_ASCENT].value) > 100))
11428 return 0;
11429
11430 /* Bounding box must be a list or vector containing 4 integers. */
11431 tem = fmt[GS_BOUNDING_BOX].value;
11432 if (CONSP (tem))
11433 {
11434 for (i = 0; i < 4; ++i, tem = XCDR (tem))
11435 if (!CONSP (tem) || !INTEGERP (XCAR (tem)))
11436 return 0;
11437 if (!NILP (tem))
11438 return 0;
11439 }
11440 else if (VECTORP (tem))
11441 {
11442 if (XVECTOR (tem)->size != 4)
11443 return 0;
11444 for (i = 0; i < 4; ++i)
11445 if (!INTEGERP (XVECTOR (tem)->contents[i]))
11446 return 0;
11447 }
11448 else
11449 return 0;
11450
11451 return 1;
11452}
11453
11454
11455/* Load Ghostscript image IMG for use on frame F. Value is non-zero
11456 if successful. */
11457
11458static int
11459gs_load (f, img)
11460 struct frame *f;
11461 struct image *img;
11462{
11463 char buffer[100];
11464 Lisp_Object window_and_pixmap_id = Qnil, loader, pt_height, pt_width;
11465 struct gcpro gcpro1, gcpro2;
11466 Lisp_Object frame;
11467 double in_width, in_height;
11468 Lisp_Object pixel_colors = Qnil;
11469
11470 /* Compute pixel size of pixmap needed from the given size in the
11471 image specification. Sizes in the specification are in pt. 1 pt
11472 = 1/72 in, xdpi and ydpi are stored in the frame's X display
11473 info. */
11474 pt_width = image_spec_value (img->spec, QCpt_width, NULL);
11475 in_width = XFASTINT (pt_width) / 72.0;
11476 img->width = in_width * FRAME_W32_DISPLAY_INFO (f)->resx;
11477 pt_height = image_spec_value (img->spec, QCpt_height, NULL);
11478 in_height = XFASTINT (pt_height) / 72.0;
11479 img->height = in_height * FRAME_W32_DISPLAY_INFO (f)->resy;
11480
11481 /* Create the pixmap. */
11482 BLOCK_INPUT;
11483 xassert (img->pixmap == 0);
11484 img->pixmap = XCreatePixmap (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
11485 img->width, img->height,
11486 DefaultDepthOfScreen (FRAME_X_SCREEN (f)));
11487 UNBLOCK_INPUT;
11488
11489 if (!img->pixmap)
11490 {
11491 image_error ("Unable to create pixmap for `%s'", img->spec, Qnil);
11492 return 0;
11493 }
11494
11495 /* Call the loader to fill the pixmap. It returns a process object
11496 if successful. We do not record_unwind_protect here because
11497 other places in redisplay like calling window scroll functions
11498 don't either. Let the Lisp loader use `unwind-protect' instead. */
11499 GCPRO2 (window_and_pixmap_id, pixel_colors);
11500
11501 sprintf (buffer, "%lu %lu",
11502 (unsigned long) FRAME_W32_WINDOW (f),
11503 (unsigned long) img->pixmap);
11504 window_and_pixmap_id = build_string (buffer);
11505
11506 sprintf (buffer, "%lu %lu",
11507 FRAME_FOREGROUND_PIXEL (f),
11508 FRAME_BACKGROUND_PIXEL (f));
11509 pixel_colors = build_string (buffer);
11510
11511 XSETFRAME (frame, f);
11512 loader = image_spec_value (img->spec, QCloader, NULL);
11513 if (NILP (loader))
11514 loader = intern ("gs-load-image");
11515
11516 img->data.lisp_val = call6 (loader, frame, img->spec,
11517 make_number (img->width),
11518 make_number (img->height),
11519 window_and_pixmap_id,
11520 pixel_colors);
11521 UNGCPRO;
11522 return PROCESSP (img->data.lisp_val);
11523}
11524
11525
11526/* Kill the Ghostscript process that was started to fill PIXMAP on
11527 frame F. Called from XTread_socket when receiving an event
11528 telling Emacs that Ghostscript has finished drawing. */
11529
11530void
11531x_kill_gs_process (pixmap, f)
11532 Pixmap pixmap;
11533 struct frame *f;
11534{
11535 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
11536 int class, i;
11537 struct image *img;
11538
11539 /* Find the image containing PIXMAP. */
11540 for (i = 0; i < c->used; ++i)
11541 if (c->images[i]->pixmap == pixmap)
11542 break;
11543
11544 /* Kill the GS process. We should have found PIXMAP in the image
11545 cache and its image should contain a process object. */
11546 xassert (i < c->used);
11547 img = c->images[i];
11548 xassert (PROCESSP (img->data.lisp_val));
11549 Fkill_process (img->data.lisp_val, Qnil);
11550 img->data.lisp_val = Qnil;
11551
11552 /* On displays with a mutable colormap, figure out the colors
11553 allocated for the image by looking at the pixels of an XImage for
11554 img->pixmap. */
11555 class = FRAME_W32_DISPLAY_INFO (f)->visual->class;
11556 if (class != StaticColor && class != StaticGray && class != TrueColor)
11557 {
11558 XImage *ximg;
11559
11560 BLOCK_INPUT;
11561
11562 /* Try to get an XImage for img->pixmep. */
11563 ximg = XGetImage (FRAME_W32_DISPLAY (f), img->pixmap,
11564 0, 0, img->width, img->height, ~0, ZPixmap);
11565 if (ximg)
11566 {
11567 int x, y;
11568
11569 /* Initialize the color table. */
11570 init_color_table ();
11571
11572 /* For each pixel of the image, look its color up in the
11573 color table. After having done so, the color table will
11574 contain an entry for each color used by the image. */
11575 for (y = 0; y < img->height; ++y)
11576 for (x = 0; x < img->width; ++x)
11577 {
11578 unsigned long pixel = XGetPixel (ximg, x, y);
11579 lookup_pixel_color (f, pixel);
11580 }
11581
11582 /* Record colors in the image. Free color table and XImage. */
11583 img->colors = colors_in_color_table (&img->ncolors);
11584 free_color_table ();
11585 XDestroyImage (ximg);
11586
11587#if 0 /* This doesn't seem to be the case. If we free the colors
11588 here, we get a BadAccess later in x_clear_image when
11589 freeing the colors. */
11590 /* We have allocated colors once, but Ghostscript has also
11591 allocated colors on behalf of us. So, to get the
11592 reference counts right, free them once. */
11593 if (img->ncolors)
11594 {
11595 Colormap cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f));
11596 XFreeColors (FRAME_W32_DISPLAY (f), cmap,
11597 img->colors, img->ncolors, 0);
11598 }
11599#endif
11600 }
11601 else
11602 image_error ("Cannot get X image of `%s'; colors will not be freed",
11603 img->spec, Qnil);
11604
11605 UNBLOCK_INPUT;
11606 }
11607}
11608
11609#endif /* HAVE_GHOSTSCRIPT */
11610
11611\f
11612/***********************************************************************
11613 Window properties
11614 ***********************************************************************/
11615
11616DEFUN ("x-change-window-property", Fx_change_window_property,
11617 Sx_change_window_property, 2, 3, 0,
11618 "Change window property PROP to VALUE on the X window of FRAME.\n\
11619PROP and VALUE must be strings. FRAME nil or omitted means use the\n\
11620selected frame. Value is VALUE.")
11621 (prop, value, frame)
11622 Lisp_Object frame, prop, value;
11623{
11624#if 0 /* NTEMACS_TODO : port window properties to W32 */
11625 struct frame *f = check_x_frame (frame);
11626 Atom prop_atom;
11627
11628 CHECK_STRING (prop, 1);
11629 CHECK_STRING (value, 2);
11630
11631 BLOCK_INPUT;
11632 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), XSTRING (prop)->data, False);
11633 XChangeProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
11634 prop_atom, XA_STRING, 8, PropModeReplace,
11635 XSTRING (value)->data, XSTRING (value)->size);
11636
11637 /* Make sure the property is set when we return. */
11638 XFlush (FRAME_W32_DISPLAY (f));
11639 UNBLOCK_INPUT;
11640
11641#endif /* NTEMACS_TODO */
11642
11643 return value;
11644}
11645
11646
11647DEFUN ("x-delete-window-property", Fx_delete_window_property,
11648 Sx_delete_window_property, 1, 2, 0,
11649 "Remove window property PROP from X window of FRAME.\n\
11650FRAME nil or omitted means use the selected frame. Value is PROP.")
11651 (prop, frame)
11652 Lisp_Object prop, frame;
11653{
11654#if 0 /* NTEMACS_TODO : port window properties to W32 */
11655
11656 struct frame *f = check_x_frame (frame);
11657 Atom prop_atom;
11658
11659 CHECK_STRING (prop, 1);
11660 BLOCK_INPUT;
11661 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), XSTRING (prop)->data, False);
11662 XDeleteProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), prop_atom);
11663
11664 /* Make sure the property is removed when we return. */
11665 XFlush (FRAME_W32_DISPLAY (f));
11666 UNBLOCK_INPUT;
11667#endif /* NTEMACS_TODO */
11668
11669 return prop;
11670}
11671
11672
11673DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
11674 1, 2, 0,
11675 "Value is the value of window property PROP on FRAME.\n\
11676If FRAME is nil or omitted, use the selected frame. Value is nil\n\
11677if FRAME hasn't a property with name PROP or if PROP has no string\n\
11678value.")
11679 (prop, frame)
11680 Lisp_Object prop, frame;
11681{
11682#if 0 /* NTEMACS_TODO : port window properties to W32 */
11683
11684 struct frame *f = check_x_frame (frame);
11685 Atom prop_atom;
11686 int rc;
11687 Lisp_Object prop_value = Qnil;
11688 char *tmp_data = NULL;
11689 Atom actual_type;
11690 int actual_format;
11691 unsigned long actual_size, bytes_remaining;
11692
11693 CHECK_STRING (prop, 1);
11694 BLOCK_INPUT;
11695 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), XSTRING (prop)->data, False);
11696 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
11697 prop_atom, 0, 0, False, XA_STRING,
11698 &actual_type, &actual_format, &actual_size,
11699 &bytes_remaining, (unsigned char **) &tmp_data);
11700 if (rc == Success)
11701 {
11702 int size = bytes_remaining;
11703
11704 XFree (tmp_data);
11705 tmp_data = NULL;
11706
11707 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
11708 prop_atom, 0, bytes_remaining,
11709 False, XA_STRING,
11710 &actual_type, &actual_format,
11711 &actual_size, &bytes_remaining,
11712 (unsigned char **) &tmp_data);
11713 if (rc == Success)
11714 prop_value = make_string (tmp_data, size);
11715
11716 XFree (tmp_data);
11717 }
11718
11719 UNBLOCK_INPUT;
11720
11721 return prop_value;
11722
11723#endif /* NTEMACS_TODO */
11724 return Qnil;
11725}
11726
11727
11728\f
11729/***********************************************************************
11730 Busy cursor
11731 ***********************************************************************/
11732
f79e6790
JR
11733/* If non-null, an asynchronous timer that, when it expires, displays
11734 a busy cursor on all frames. */
6fc2811b 11735
f79e6790 11736static struct atimer *busy_cursor_atimer;
6fc2811b 11737
f79e6790 11738/* Non-zero means a busy cursor is currently shown. */
6fc2811b 11739
f79e6790 11740static int busy_cursor_shown_p;
6fc2811b 11741
f79e6790 11742/* Number of seconds to wait before displaying a busy cursor. */
6fc2811b 11743
f79e6790 11744static Lisp_Object Vbusy_cursor_delay;
6fc2811b 11745
f79e6790
JR
11746/* Default number of seconds to wait before displaying a busy
11747 cursor. */
11748
11749#define DEFAULT_BUSY_CURSOR_DELAY 1
11750
11751/* Function prototypes. */
11752
11753static void show_busy_cursor P_ ((struct atimer *));
11754static void hide_busy_cursor P_ ((void));
11755
11756
11757/* Cancel a currently active busy-cursor timer, and start a new one. */
11758
11759void
11760start_busy_cursor ()
11761{
11762#if 0 /* NTEMACS_TODO: cursor shape changes. */
11763 EMACS_TIME delay;
dfff8a69 11764 int secs, usecs = 0;
f79e6790
JR
11765
11766 cancel_busy_cursor ();
11767
11768 if (INTEGERP (Vbusy_cursor_delay)
11769 && XINT (Vbusy_cursor_delay) > 0)
11770 secs = XFASTINT (Vbusy_cursor_delay);
dfff8a69
JR
11771 else if (FLOATP (Vbusy_cursor_delay)
11772 && XFLOAT_DATA (Vbusy_cursor_delay) > 0)
11773 {
11774 Lisp_Object tem;
11775 tem = Ftruncate (Vbusy_cursor_delay, Qnil);
11776 secs = XFASTINT (tem);
11777 usecs = (XFLOAT_DATA (Vbusy_cursor_delay) - secs) * 1000000;
11778 }
f79e6790
JR
11779 else
11780 secs = DEFAULT_BUSY_CURSOR_DELAY;
11781
dfff8a69 11782 EMACS_SET_SECS_USECS (delay, secs, usecs);
f79e6790
JR
11783 busy_cursor_atimer = start_atimer (ATIMER_RELATIVE, delay,
11784 show_busy_cursor, NULL);
11785#endif
11786}
11787
11788
11789/* Cancel the busy cursor timer if active, hide a busy cursor if
11790 shown. */
11791
11792void
11793cancel_busy_cursor ()
11794{
11795 if (busy_cursor_atimer)
dfff8a69
JR
11796 {
11797 cancel_atimer (busy_cursor_atimer);
11798 busy_cursor_atimer = NULL;
11799 }
11800
f79e6790
JR
11801 if (busy_cursor_shown_p)
11802 hide_busy_cursor ();
11803}
11804
11805
11806/* Timer function of busy_cursor_atimer. TIMER is equal to
11807 busy_cursor_atimer.
11808
11809 Display a busy cursor on all frames by mapping the frames'
11810 busy_window. Set the busy_p flag in the frames' output_data.x
11811 structure to indicate that a busy cursor is shown on the
11812 frames. */
11813
11814static void
11815show_busy_cursor (timer)
11816 struct atimer *timer;
6fc2811b 11817{
f79e6790
JR
11818#if 0 /* NTEMACS_TODO: cursor shape changes. */
11819 /* The timer implementation will cancel this timer automatically
11820 after this function has run. Set busy_cursor_atimer to null
11821 so that we know the timer doesn't have to be canceled. */
11822 busy_cursor_atimer = NULL;
11823
11824 if (!busy_cursor_shown_p)
6fc2811b
JR
11825 {
11826 Lisp_Object rest, frame;
f79e6790
JR
11827
11828 BLOCK_INPUT;
11829
6fc2811b
JR
11830 FOR_EACH_FRAME (rest, frame)
11831 if (FRAME_X_P (XFRAME (frame)))
11832 {
11833 struct frame *f = XFRAME (frame);
f79e6790 11834
6fc2811b 11835 f->output_data.w32->busy_p = 1;
f79e6790 11836
6fc2811b
JR
11837 if (!f->output_data.w32->busy_window)
11838 {
11839 unsigned long mask = CWCursor;
11840 XSetWindowAttributes attrs;
f79e6790 11841
6fc2811b 11842 attrs.cursor = f->output_data.w32->busy_cursor;
f79e6790 11843
6fc2811b 11844 f->output_data.w32->busy_window
f79e6790 11845 = XCreateWindow (FRAME_X_DISPLAY (f),
6fc2811b
JR
11846 FRAME_OUTER_WINDOW (f),
11847 0, 0, 32000, 32000, 0, 0,
f79e6790
JR
11848 InputOnly,
11849 CopyFromParent,
6fc2811b
JR
11850 mask, &attrs);
11851 }
f79e6790
JR
11852
11853 XMapRaised (FRAME_X_DISPLAY (f), f->output_data.w32->busy_window);
11854 XFlush (FRAME_X_DISPLAY (f));
6fc2811b 11855 }
6fc2811b 11856
f79e6790
JR
11857 busy_cursor_shown_p = 1;
11858 UNBLOCK_INPUT;
11859 }
11860#endif
6fc2811b
JR
11861}
11862
11863
f79e6790 11864/* Hide the busy cursor on all frames, if it is currently shown. */
6fc2811b 11865
f79e6790
JR
11866static void
11867hide_busy_cursor ()
11868{
11869#if 0 /* NTEMACS_TODO: cursor shape changes. */
11870 if (busy_cursor_shown_p)
6fc2811b 11871 {
f79e6790
JR
11872 Lisp_Object rest, frame;
11873
11874 BLOCK_INPUT;
11875 FOR_EACH_FRAME (rest, frame)
6fc2811b 11876 {
f79e6790
JR
11877 struct frame *f = XFRAME (frame);
11878
11879 if (FRAME_X_P (f)
11880 /* Watch out for newly created frames. */
11881 && f->output_data.x->busy_window)
11882 {
11883 XUnmapWindow (FRAME_X_DISPLAY (f), f->output_data.x->busy_window);
11884 /* Sync here because XTread_socket looks at the busy_p flag
11885 that is reset to zero below. */
11886 XSync (FRAME_X_DISPLAY (f), False);
11887 f->output_data.x->busy_p = 0;
11888 }
6fc2811b 11889 }
6fc2811b 11890
f79e6790
JR
11891 busy_cursor_shown_p = 0;
11892 UNBLOCK_INPUT;
11893 }
11894#endif
6fc2811b
JR
11895}
11896
11897
11898\f
11899/***********************************************************************
11900 Tool tips
11901 ***********************************************************************/
11902
11903static Lisp_Object x_create_tip_frame P_ ((struct w32_display_info *,
11904 Lisp_Object));
11905
11906/* The frame of a currently visible tooltip, or null. */
11907
11908struct frame *tip_frame;
11909
11910/* If non-nil, a timer started that hides the last tooltip when it
11911 fires. */
11912
11913Lisp_Object tip_timer;
11914Window tip_window;
11915
11916/* Create a frame for a tooltip on the display described by DPYINFO.
11917 PARMS is a list of frame parameters. Value is the frame. */
11918
11919static Lisp_Object
11920x_create_tip_frame (dpyinfo, parms)
11921 struct w32_display_info *dpyinfo;
11922 Lisp_Object parms;
11923{
11924#if 0 /* NTEMACS_TODO : w32 version */
11925 struct frame *f;
11926 Lisp_Object frame, tem;
11927 Lisp_Object name;
11928 long window_prompting = 0;
11929 int width, height;
11930 int count = specpdl_ptr - specpdl;
11931 struct gcpro gcpro1, gcpro2, gcpro3;
11932 struct kboard *kb;
11933
11934 check_x ();
11935
11936 /* Use this general default value to start with until we know if
11937 this frame has a specified name. */
11938 Vx_resource_name = Vinvocation_name;
11939
11940#ifdef MULTI_KBOARD
11941 kb = dpyinfo->kboard;
11942#else
11943 kb = &the_only_kboard;
11944#endif
11945
11946 /* Get the name of the frame to use for resource lookup. */
11947 name = w32_get_arg (parms, Qname, "name", "Name", RES_TYPE_STRING);
11948 if (!STRINGP (name)
11949 && !EQ (name, Qunbound)
11950 && !NILP (name))
11951 error ("Invalid frame name--not a string or nil");
11952 Vx_resource_name = name;
11953
11954 frame = Qnil;
11955 GCPRO3 (parms, name, frame);
11956 tip_frame = f = make_frame (1);
11957 XSETFRAME (frame, f);
11958 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
11959
d88c567c 11960 f->output_method = output_w32;
6fc2811b
JR
11961 f->output_data.w32 =
11962 (struct w32_output *) xmalloc (sizeof (struct w32_output));
11963 bzero (f->output_data.w32, sizeof (struct w32_output));
11964#if 0
11965 f->output_data.w32->icon_bitmap = -1;
11966#endif
11967 f->output_data.w32->fontset = -1;
11968 f->icon_name = Qnil;
11969
11970#ifdef MULTI_KBOARD
11971 FRAME_KBOARD (f) = kb;
11972#endif
11973 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
11974 f->output_data.w32->explicit_parent = 0;
11975
11976 /* Set the name; the functions to which we pass f expect the name to
11977 be set. */
11978 if (EQ (name, Qunbound) || NILP (name))
11979 {
11980 f->name = build_string (dpyinfo->x_id_name);
11981 f->explicit_name = 0;
11982 }
11983 else
11984 {
11985 f->name = name;
11986 f->explicit_name = 1;
11987 /* use the frame's title when getting resources for this frame. */
11988 specbind (Qx_resource_name, name);
11989 }
11990
6fc2811b
JR
11991 /* Extract the window parameters from the supplied values
11992 that are needed to determine window geometry. */
11993 {
11994 Lisp_Object font;
11995
11996 font = w32_get_arg (parms, Qfont, "font", "Font", RES_TYPE_STRING);
11997
11998 BLOCK_INPUT;
11999 /* First, try whatever font the caller has specified. */
12000 if (STRINGP (font))
12001 {
12002 tem = Fquery_fontset (font, Qnil);
12003 if (STRINGP (tem))
12004 font = x_new_fontset (f, XSTRING (tem)->data);
12005 else
12006 font = x_new_font (f, XSTRING (font)->data);
12007 }
12008
12009 /* Try out a font which we hope has bold and italic variations. */
12010 if (!STRINGP (font))
12011 font = x_new_font (f, "-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1");
12012 if (!STRINGP (font))
12013 font = x_new_font (f, "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
12014 if (! STRINGP (font))
12015 font = x_new_font (f, "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
12016 if (! STRINGP (font))
12017 /* This was formerly the first thing tried, but it finds too many fonts
12018 and takes too long. */
12019 font = x_new_font (f, "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1");
12020 /* If those didn't work, look for something which will at least work. */
12021 if (! STRINGP (font))
12022 font = x_new_font (f, "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1");
12023 UNBLOCK_INPUT;
12024 if (! STRINGP (font))
12025 font = build_string ("fixed");
12026
12027 x_default_parameter (f, parms, Qfont, font,
12028 "font", "Font", RES_TYPE_STRING);
12029 }
12030
12031 x_default_parameter (f, parms, Qborder_width, make_number (2),
12032 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
12033
12034 /* This defaults to 2 in order to match xterm. We recognize either
12035 internalBorderWidth or internalBorder (which is what xterm calls
12036 it). */
12037 if (NILP (Fassq (Qinternal_border_width, parms)))
12038 {
12039 Lisp_Object value;
12040
12041 value = w32_get_arg (parms, Qinternal_border_width,
12042 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
12043 if (! EQ (value, Qunbound))
12044 parms = Fcons (Fcons (Qinternal_border_width, value),
12045 parms);
12046 }
12047
12048 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
12049 "internalBorderWidth", "internalBorderWidth",
12050 RES_TYPE_NUMBER);
12051
12052 /* Also do the stuff which must be set before the window exists. */
12053 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
12054 "foreground", "Foreground", RES_TYPE_STRING);
12055 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
12056 "background", "Background", RES_TYPE_STRING);
12057 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
12058 "pointerColor", "Foreground", RES_TYPE_STRING);
12059 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
12060 "cursorColor", "Foreground", RES_TYPE_STRING);
12061 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
12062 "borderColor", "BorderColor", RES_TYPE_STRING);
12063
12064 /* Init faces before x_default_parameter is called for scroll-bar
12065 parameters because that function calls x_set_scroll_bar_width,
12066 which calls change_frame_size, which calls Fset_window_buffer,
12067 which runs hooks, which call Fvertical_motion. At the end, we
12068 end up in init_iterator with a null face cache, which should not
12069 happen. */
12070 init_frame_faces (f);
12071
12072 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
12073 window_prompting = x_figure_window_size (f, parms);
12074
12075 if (window_prompting & XNegative)
12076 {
12077 if (window_prompting & YNegative)
12078 f->output_data.w32->win_gravity = SouthEastGravity;
12079 else
12080 f->output_data.w32->win_gravity = NorthEastGravity;
12081 }
12082 else
12083 {
12084 if (window_prompting & YNegative)
12085 f->output_data.w32->win_gravity = SouthWestGravity;
12086 else
12087 f->output_data.w32->win_gravity = NorthWestGravity;
12088 }
12089
12090 f->output_data.w32->size_hint_flags = window_prompting;
12091 {
12092 XSetWindowAttributes attrs;
12093 unsigned long mask;
12094
12095 BLOCK_INPUT;
12096 mask = CWBackPixel | CWOverrideRedirect | CWSaveUnder | CWEventMask;
12097 /* Window managers looks at the override-redirect flag to
12098 determine whether or net to give windows a decoration (Xlib
12099 3.2.8). */
12100 attrs.override_redirect = True;
12101 attrs.save_under = True;
12102 attrs.background_pixel = FRAME_BACKGROUND_PIXEL (f);
12103 /* Arrange for getting MapNotify and UnmapNotify events. */
12104 attrs.event_mask = StructureNotifyMask;
12105 tip_window
12106 = FRAME_W32_WINDOW (f)
12107 = XCreateWindow (FRAME_W32_DISPLAY (f),
12108 FRAME_W32_DISPLAY_INFO (f)->root_window,
12109 /* x, y, width, height */
12110 0, 0, 1, 1,
12111 /* Border. */
12112 1,
12113 CopyFromParent, InputOutput, CopyFromParent,
12114 mask, &attrs);
12115 UNBLOCK_INPUT;
12116 }
12117
12118 x_make_gc (f);
12119
12120 x_default_parameter (f, parms, Qauto_raise, Qnil,
12121 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
12122 x_default_parameter (f, parms, Qauto_lower, Qnil,
12123 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
12124 x_default_parameter (f, parms, Qcursor_type, Qbox,
12125 "cursorType", "CursorType", RES_TYPE_SYMBOL);
12126
12127 /* Dimensions, especially f->height, must be done via change_frame_size.
12128 Change will not be effected unless different from the current
12129 f->height. */
12130 width = f->width;
12131 height = f->height;
12132 f->height = 0;
12133 SET_FRAME_WIDTH (f, 0);
12134 change_frame_size (f, height, width, 1, 0, 0);
12135
12136 f->no_split = 1;
12137
12138 UNGCPRO;
12139
12140 /* It is now ok to make the frame official even if we get an error
12141 below. And the frame needs to be on Vframe_list or making it
12142 visible won't work. */
12143 Vframe_list = Fcons (frame, Vframe_list);
12144
12145 /* Now that the frame is official, it counts as a reference to
12146 its display. */
12147 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
ee78dc32 12148
6fc2811b
JR
12149 return unbind_to (count, frame);
12150#endif /* NTEMACS_TODO */
12151 return Qnil;
ee78dc32
GV
12152}
12153
ee78dc32 12154
71eab8d1 12155DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
6fc2811b 12156 "Show STRING in a \"tooltip\" window on frame FRAME.\n\
71eab8d1
AI
12157A tooltip window is a small X window displaying a string.\n\
12158\n\
6fc2811b 12159FRAME nil or omitted means use the selected frame.\n\
71eab8d1 12160\n\
6fc2811b
JR
12161PARMS is an optional list of frame parameters which can be\n\
12162used to change the tooltip's appearance.\n\
71eab8d1 12163\n\
6fc2811b 12164Automatically hide the tooltip after TIMEOUT seconds.\n\
71eab8d1
AI
12165TIMEOUT nil means use the default timeout of 5 seconds.\n\
12166\n\
12167If the list of frame parameters PARAMS contains a `left' parameters,\n\
12168the tooltip is displayed at that x-position. Otherwise it is\n\
12169displayed at the mouse position, with offset DX added (default is 5 if\n\
12170DX isn't specified). Likewise for the y-position; if a `top' frame\n\
12171parameter is specified, it determines the y-position of the tooltip\n\
12172window, otherwise it is displayed at the mouse position, with offset\n\
12173DY added (default is -5).")
12174 (string, frame, parms, timeout, dx, dy)
12175 Lisp_Object string, frame, parms, timeout, dx, dy;
ee78dc32 12176{
6fc2811b
JR
12177 struct frame *f;
12178 struct window *w;
12179 Window root, child;
71eab8d1 12180 Lisp_Object buffer, top, left;
6fc2811b
JR
12181 struct buffer *old_buffer;
12182 struct text_pos pos;
12183 int i, width, height;
12184 int root_x, root_y, win_x, win_y;
12185 unsigned pmask;
12186 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
12187 int old_windows_or_buffers_changed = windows_or_buffers_changed;
12188 int count = specpdl_ptr - specpdl;
12189
12190 specbind (Qinhibit_redisplay, Qt);
ee78dc32 12191
dfff8a69 12192 GCPRO4 (string, parms, frame, timeout);
ee78dc32 12193
6fc2811b
JR
12194 CHECK_STRING (string, 0);
12195 f = check_x_frame (frame);
12196 if (NILP (timeout))
12197 timeout = make_number (5);
12198 else
12199 CHECK_NATNUM (timeout, 2);
ee78dc32 12200
71eab8d1
AI
12201 if (NILP (dx))
12202 dx = make_number (5);
12203 else
12204 CHECK_NUMBER (dx, 5);
12205
12206 if (NILP (dy))
12207 dy = make_number (-5);
12208 else
12209 CHECK_NUMBER (dy, 6);
12210
6fc2811b
JR
12211 /* Hide a previous tip, if any. */
12212 Fx_hide_tip ();
ee78dc32 12213
6fc2811b
JR
12214 /* Add default values to frame parameters. */
12215 if (NILP (Fassq (Qname, parms)))
12216 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
12217 if (NILP (Fassq (Qinternal_border_width, parms)))
12218 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
12219 if (NILP (Fassq (Qborder_width, parms)))
12220 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
12221 if (NILP (Fassq (Qborder_color, parms)))
12222 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
12223 if (NILP (Fassq (Qbackground_color, parms)))
12224 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
12225 parms);
12226
12227 /* Create a frame for the tooltip, and record it in the global
12228 variable tip_frame. */
12229 frame = x_create_tip_frame (FRAME_W32_DISPLAY_INFO (f), parms);
12230 tip_frame = f = XFRAME (frame);
12231
12232 /* Set up the frame's root window. Currently we use a size of 80
12233 columns x 40 lines. If someone wants to show a larger tip, he
12234 will loose. I don't think this is a realistic case. */
12235 w = XWINDOW (FRAME_ROOT_WINDOW (f));
12236 w->left = w->top = make_number (0);
12237 w->width = 80;
12238 w->height = 40;
12239 adjust_glyphs (f);
12240 w->pseudo_window_p = 1;
12241
12242 /* Display the tooltip text in a temporary buffer. */
12243 buffer = Fget_buffer_create (build_string (" *tip*"));
12244 Fset_window_buffer (FRAME_ROOT_WINDOW (f), buffer);
12245 old_buffer = current_buffer;
12246 set_buffer_internal_1 (XBUFFER (buffer));
12247 Ferase_buffer ();
12248 Finsert (make_number (1), &string);
12249 clear_glyph_matrix (w->desired_matrix);
12250 clear_glyph_matrix (w->current_matrix);
12251 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
12252 try_window (FRAME_ROOT_WINDOW (f), pos);
12253
12254 /* Compute width and height of the tooltip. */
12255 width = height = 0;
12256 for (i = 0; i < w->desired_matrix->nrows; ++i)
ee78dc32 12257 {
6fc2811b
JR
12258 struct glyph_row *row = &w->desired_matrix->rows[i];
12259 struct glyph *last;
12260 int row_width;
12261
12262 /* Stop at the first empty row at the end. */
12263 if (!row->enabled_p || !row->displays_text_p)
12264 break;
12265
12266 /* Let the row go over the full width of the frame. */
12267 row->full_width_p = 1;
12268
12269 /* There's a glyph at the end of rows that is use to place
12270 the cursor there. Don't include the width of this glyph. */
12271 if (row->used[TEXT_AREA])
12272 {
12273 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
12274 row_width = row->pixel_width - last->pixel_width;
12275 }
12276 else
12277 row_width = row->pixel_width;
12278
12279 height += row->height;
12280 width = max (width, row_width);
ee78dc32
GV
12281 }
12282
6fc2811b
JR
12283 /* Add the frame's internal border to the width and height the X
12284 window should have. */
12285 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
12286 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
ee78dc32 12287
71eab8d1
AI
12288 /* User-specified position? */
12289 left = Fcdr (Fassq (Qleft, parms));
12290 top = Fcdr (Fassq (Qtop, parms));
12291
6fc2811b
JR
12292 /* Move the tooltip window where the mouse pointer is. Resize and
12293 show it. */
12294#if 0 /* NTEMACS_TODO : W32 specifics */
12295 BLOCK_INPUT;
71eab8d1 12296 XQueryPointer (FRAME_X_DISPLAY (f), FRAME_X_DISPLAY_INFO (f)->root_window,
6fc2811b 12297 &root, &child, &root_x, &root_y, &win_x, &win_y, &pmask);
71eab8d1
AI
12298 UNBLOCK_INPUT;
12299
12300 root_x += XINT (dx);
12301 root_y += XINT (dy);
12302
12303 if (INTEGERP (left))
12304 root_x = XINT (left);
12305 if (INTEGERP (top))
12306 root_y = XINT (top);
12307
12308 BLOCK_INPUT;
12309 XMoveResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12310 root_x, root_y - height, width, height);
12311 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
6fc2811b
JR
12312 UNBLOCK_INPUT;
12313#endif /* NTEMACS_TODO */
ee78dc32 12314
6fc2811b
JR
12315 /* Draw into the window. */
12316 w->must_be_updated_p = 1;
12317 update_single_window (w, 1);
ee78dc32 12318
6fc2811b
JR
12319 /* Restore original current buffer. */
12320 set_buffer_internal_1 (old_buffer);
12321 windows_or_buffers_changed = old_windows_or_buffers_changed;
ee78dc32 12322
6fc2811b
JR
12323 /* Let the tip disappear after timeout seconds. */
12324 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
12325 intern ("x-hide-tip"));
ee78dc32 12326
dfff8a69 12327 UNGCPRO;
6fc2811b 12328 return unbind_to (count, Qnil);
ee78dc32
GV
12329}
12330
ee78dc32 12331
6fc2811b
JR
12332DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
12333 "Hide the current tooltip window, if there is any.\n\
12334Value is t is tooltip was open, nil otherwise.")
12335 ()
12336{
12337 int count = specpdl_ptr - specpdl;
12338 int deleted_p = 0;
12339
12340 specbind (Qinhibit_redisplay, Qt);
12341
12342 if (!NILP (tip_timer))
12343 {
12344 call1 (intern ("cancel-timer"), tip_timer);
12345 tip_timer = Qnil;
12346 }
ee78dc32 12347
6fc2811b
JR
12348 if (tip_frame)
12349 {
12350 Lisp_Object frame;
12351
12352 XSETFRAME (frame, tip_frame);
12353 Fdelete_frame (frame, Qt);
12354 tip_frame = NULL;
12355 deleted_p = 1;
12356 }
1edf84e7 12357
6fc2811b
JR
12358 return unbind_to (count, deleted_p ? Qt : Qnil);
12359}
5ac45f98 12360
5ac45f98 12361
6fc2811b
JR
12362\f
12363/***********************************************************************
12364 File selection dialog
12365 ***********************************************************************/
12366
12367extern Lisp_Object Qfile_name_history;
12368
12369DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 4, 0,
12370 "Read file name, prompting with PROMPT in directory DIR.\n\
12371Use a file selection dialog.\n\
12372Select DEFAULT-FILENAME in the dialog's file selection box, if\n\
12373specified. Don't let the user enter a file name in the file\n\
12374selection dialog's entry field, if MUSTMATCH is non-nil.")
12375 (prompt, dir, default_filename, mustmatch)
12376 Lisp_Object prompt, dir, default_filename, mustmatch;
12377{
12378 struct frame *f = SELECTED_FRAME ();
12379 Lisp_Object file = Qnil;
12380 int count = specpdl_ptr - specpdl;
12381 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
12382 char filename[MAX_PATH + 1];
12383 char init_dir[MAX_PATH + 1];
12384 int use_dialog_p = 1;
12385
12386 GCPRO5 (prompt, dir, default_filename, mustmatch, file);
12387 CHECK_STRING (prompt, 0);
12388 CHECK_STRING (dir, 1);
12389
12390 /* Create the dialog with PROMPT as title, using DIR as initial
12391 directory and using "*" as pattern. */
12392 dir = Fexpand_file_name (dir, Qnil);
12393 strncpy (init_dir, XSTRING (dir)->data, MAX_PATH);
12394 init_dir[MAX_PATH] = '\0';
12395 unixtodos_filename (init_dir);
12396
12397 if (STRINGP (default_filename))
12398 {
12399 char *file_name_only;
12400 char *full_path_name = XSTRING (default_filename)->data;
5ac45f98 12401
6fc2811b 12402 unixtodos_filename (full_path_name);
5ac45f98 12403
6fc2811b
JR
12404 file_name_only = strrchr (full_path_name, '\\');
12405 if (!file_name_only)
12406 file_name_only = full_path_name;
12407 else
12408 {
12409 file_name_only++;
5ac45f98 12410
6fc2811b
JR
12411 /* If default_file_name is a directory, don't use the open
12412 file dialog, as it does not support selecting
12413 directories. */
12414 if (!(*file_name_only))
12415 use_dialog_p = 0;
12416 }
ee78dc32 12417
6fc2811b
JR
12418 strncpy (filename, file_name_only, MAX_PATH);
12419 filename[MAX_PATH] = '\0';
12420 }
ee78dc32 12421 else
6fc2811b 12422 filename[0] = '\0';
ee78dc32 12423
6fc2811b
JR
12424 if (use_dialog_p)
12425 {
12426 OPENFILENAME file_details;
12427 char *filename_file;
5ac45f98 12428
6fc2811b
JR
12429 /* Prevent redisplay. */
12430 specbind (Qinhibit_redisplay, Qt);
12431 BLOCK_INPUT;
ee78dc32 12432
6fc2811b
JR
12433 bzero (&file_details, sizeof (file_details));
12434 file_details.lStructSize = sizeof (file_details);
12435 file_details.hwndOwner = FRAME_W32_WINDOW (f);
12436 file_details.lpstrFile = filename;
12437 file_details.nMaxFile = sizeof (filename);
12438 file_details.lpstrInitialDir = init_dir;
12439 file_details.lpstrTitle = XSTRING (prompt)->data;
12440 file_details.Flags = OFN_HIDEREADONLY | OFN_NOCHANGEDIR;
ee78dc32 12441
6fc2811b
JR
12442 if (!NILP (mustmatch))
12443 file_details.Flags |= OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
ee78dc32 12444
6fc2811b
JR
12445 if (GetOpenFileName (&file_details))
12446 {
12447 dostounix_filename (filename);
12448 file = build_string (filename);
12449 }
ee78dc32 12450 else
6fc2811b
JR
12451 file = Qnil;
12452
12453 UNBLOCK_INPUT;
12454 file = unbind_to (count, file);
ee78dc32 12455 }
6fc2811b
JR
12456 /* Open File dialog will not allow folders to be selected, so resort
12457 to minibuffer completing reads for directories. */
12458 else
12459 file = Fcompleting_read (prompt, intern ("read-file-name-internal"),
12460 dir, mustmatch, dir, Qfile_name_history,
12461 default_filename, Qnil);
ee78dc32 12462
6fc2811b 12463 UNGCPRO;
1edf84e7 12464
6fc2811b
JR
12465 /* Make "Cancel" equivalent to C-g. */
12466 if (NILP (file))
12467 Fsignal (Qquit, Qnil);
ee78dc32 12468
dfff8a69 12469 return unbind_to (count, file);
6fc2811b 12470}
ee78dc32 12471
ee78dc32 12472
6fc2811b
JR
12473\f
12474/***********************************************************************
12475 Tests
12476 ***********************************************************************/
ee78dc32 12477
6fc2811b 12478#if GLYPH_DEBUG
ee78dc32 12479
6fc2811b
JR
12480DEFUN ("imagep", Fimagep, Simagep, 1, 1, 0,
12481 "Value is non-nil if SPEC is a valid image specification.")
12482 (spec)
12483 Lisp_Object spec;
12484{
12485 return valid_image_p (spec) ? Qt : Qnil;
ee78dc32
GV
12486}
12487
ee78dc32 12488
6fc2811b
JR
12489DEFUN ("lookup-image", Flookup_image, Slookup_image, 1, 1, 0, "")
12490 (spec)
12491 Lisp_Object spec;
12492{
12493 int id = -1;
12494
12495 if (valid_image_p (spec))
12496 id = lookup_image (SELECTED_FRAME (), spec);
ee78dc32 12497
6fc2811b
JR
12498 debug_print (spec);
12499 return make_number (id);
ee78dc32
GV
12500}
12501
6fc2811b 12502#endif /* GLYPH_DEBUG != 0 */
ee78dc32 12503
ee78dc32
GV
12504
12505\f
6fc2811b
JR
12506/***********************************************************************
12507 w32 specialized functions
12508 ***********************************************************************/
ee78dc32 12509
fbd6baed
GV
12510DEFUN ("w32-select-font", Fw32_select_font, Sw32_select_font, 0, 1, 0,
12511 "This will display the W32 font dialog and return an X font string corresponding to the selection.")
ee78dc32
GV
12512 (frame)
12513 Lisp_Object frame;
12514{
12515 FRAME_PTR f = check_x_frame (frame);
12516 CHOOSEFONT cf;
12517 LOGFONT lf;
f46e6225
GV
12518 TEXTMETRIC tm;
12519 HDC hdc;
12520 HANDLE oldobj;
ee78dc32
GV
12521 char buf[100];
12522
12523 bzero (&cf, sizeof (cf));
f46e6225 12524 bzero (&lf, sizeof (lf));
ee78dc32
GV
12525
12526 cf.lStructSize = sizeof (cf);
fbd6baed 12527 cf.hwndOwner = FRAME_W32_WINDOW (f);
6fc2811b 12528 cf.Flags = CF_FORCEFONTEXIST | CF_SCREENFONTS;
ee78dc32
GV
12529 cf.lpLogFont = &lf;
12530
f46e6225
GV
12531 /* Initialize as much of the font details as we can from the current
12532 default font. */
12533 hdc = GetDC (FRAME_W32_WINDOW (f));
12534 oldobj = SelectObject (hdc, FRAME_FONT (f)->hfont);
12535 GetTextFace (hdc, LF_FACESIZE, lf.lfFaceName);
12536 if (GetTextMetrics (hdc, &tm))
12537 {
12538 lf.lfHeight = tm.tmInternalLeading - tm.tmHeight;
12539 lf.lfWeight = tm.tmWeight;
12540 lf.lfItalic = tm.tmItalic;
12541 lf.lfUnderline = tm.tmUnderlined;
12542 lf.lfStrikeOut = tm.tmStruckOut;
f46e6225
GV
12543 lf.lfCharSet = tm.tmCharSet;
12544 cf.Flags |= CF_INITTOLOGFONTSTRUCT;
12545 }
12546 SelectObject (hdc, oldobj);
6fc2811b 12547 ReleaseDC (FRAME_W32_WINDOW (f), hdc);
f46e6225 12548
fbd6baed 12549 if (!ChooseFont (&cf) || !w32_to_x_font (&lf, buf, 100))
3c190163 12550 return Qnil;
ee78dc32
GV
12551
12552 return build_string (buf);
12553}
12554
1edf84e7
GV
12555DEFUN ("w32-send-sys-command", Fw32_send_sys_command, Sw32_send_sys_command, 1, 2, 0,
12556 "Send frame a Windows WM_SYSCOMMAND message of type COMMAND.\n\
12557Some useful values for command are 0xf030 to maximise frame (0xf020\n\
12558to minimize), 0xf120 to restore frame to original size, and 0xf100\n\
12559to activate the menubar for keyboard access. 0xf140 activates the\n\
12560screen saver if defined.\n\
12561\n\
12562If optional parameter FRAME is not specified, use selected frame.")
12563 (command, frame)
12564 Lisp_Object command, frame;
12565{
12566 WPARAM code;
12567 FRAME_PTR f = check_x_frame (frame);
12568
12569 CHECK_NUMBER (command, 0);
12570
ce6059da 12571 PostMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, XINT (command), 0);
1edf84e7
GV
12572
12573 return Qnil;
12574}
12575
55dcfc15
AI
12576DEFUN ("w32-shell-execute", Fw32_shell_execute, Sw32_shell_execute, 2, 4, 0,
12577 "Get Windows to perform OPERATION on DOCUMENT.\n\
12578This is a wrapper around the ShellExecute system function, which\n\
12579invokes the application registered to handle OPERATION for DOCUMENT.\n\
6fc2811b
JR
12580OPERATION is typically \"open\", \"print\" or \"explore\" (but can be\n\
12581nil for the default action), and DOCUMENT is typically the name of a\n\
12582document file or URL, but can also be a program executable to run or\n\
12583a directory to open in the Windows Explorer.\n\
55dcfc15 12584\n\
6fc2811b
JR
12585If DOCUMENT is a program executable, PARAMETERS can be a string\n\
12586containing command line parameters, but otherwise should be nil.\n\
55dcfc15
AI
12587\n\
12588SHOW-FLAG can be used to control whether the invoked application is hidden\n\
6fc2811b 12589or minimized. If SHOW-FLAG is nil, the application is displayed normally,\n\
55dcfc15
AI
12590otherwise it is an integer representing a ShowWindow flag:\n\
12591\n\
12592 0 - start hidden\n\
12593 1 - start normally\n\
12594 3 - start maximized\n\
12595 6 - start minimized")
12596 (operation, document, parameters, show_flag)
12597 Lisp_Object operation, document, parameters, show_flag;
12598{
12599 Lisp_Object current_dir;
12600
55dcfc15
AI
12601 CHECK_STRING (document, 0);
12602
12603 /* Encode filename and current directory. */
12604 current_dir = ENCODE_FILE (current_buffer->directory);
12605 document = ENCODE_FILE (document);
12606 if ((int) ShellExecute (NULL,
6fc2811b
JR
12607 (STRINGP (operation) ?
12608 XSTRING (operation)->data : NULL),
55dcfc15
AI
12609 XSTRING (document)->data,
12610 (STRINGP (parameters) ?
12611 XSTRING (parameters)->data : NULL),
12612 XSTRING (current_dir)->data,
12613 (INTEGERP (show_flag) ?
12614 XINT (show_flag) : SW_SHOWDEFAULT))
12615 > 32)
12616 return Qt;
12617 error ("ShellExecute failed");
12618}
12619
ccc2d29c
GV
12620/* Lookup virtual keycode from string representing the name of a
12621 non-ascii keystroke into the corresponding virtual key, using
12622 lispy_function_keys. */
12623static int
12624lookup_vk_code (char *key)
12625{
12626 int i;
12627
12628 for (i = 0; i < 256; i++)
12629 if (lispy_function_keys[i] != 0
12630 && strcmp (lispy_function_keys[i], key) == 0)
12631 return i;
12632
12633 return -1;
12634}
12635
12636/* Convert a one-element vector style key sequence to a hot key
12637 definition. */
12638static int
12639w32_parse_hot_key (key)
12640 Lisp_Object key;
12641{
12642 /* Copied from Fdefine_key and store_in_keymap. */
12643 register Lisp_Object c;
12644 int vk_code;
12645 int lisp_modifiers;
12646 int w32_modifiers;
12647 struct gcpro gcpro1;
12648
12649 CHECK_VECTOR (key, 0);
12650
12651 if (XFASTINT (Flength (key)) != 1)
12652 return Qnil;
12653
12654 GCPRO1 (key);
12655
12656 c = Faref (key, make_number (0));
12657
12658 if (CONSP (c) && lucid_event_type_list_p (c))
12659 c = Fevent_convert_list (c);
12660
12661 UNGCPRO;
12662
12663 if (! INTEGERP (c) && ! SYMBOLP (c))
12664 error ("Key definition is invalid");
12665
12666 /* Work out the base key and the modifiers. */
12667 if (SYMBOLP (c))
12668 {
12669 c = parse_modifiers (c);
12670 lisp_modifiers = Fcar (Fcdr (c));
12671 c = Fcar (c);
12672 if (!SYMBOLP (c))
12673 abort ();
12674 vk_code = lookup_vk_code (XSYMBOL (c)->name->data);
12675 }
12676 else if (INTEGERP (c))
12677 {
12678 lisp_modifiers = XINT (c) & ~CHARACTERBITS;
12679 /* Many ascii characters are their own virtual key code. */
12680 vk_code = XINT (c) & CHARACTERBITS;
12681 }
12682
12683 if (vk_code < 0 || vk_code > 255)
12684 return Qnil;
12685
12686 if ((lisp_modifiers & meta_modifier) != 0
12687 && !NILP (Vw32_alt_is_meta))
12688 lisp_modifiers |= alt_modifier;
12689
71eab8d1
AI
12690 /* Supply defs missing from mingw32. */
12691#ifndef MOD_ALT
12692#define MOD_ALT 0x0001
12693#define MOD_CONTROL 0x0002
12694#define MOD_SHIFT 0x0004
12695#define MOD_WIN 0x0008
12696#endif
12697
ccc2d29c
GV
12698 /* Convert lisp modifiers to Windows hot-key form. */
12699 w32_modifiers = (lisp_modifiers & hyper_modifier) ? MOD_WIN : 0;
12700 w32_modifiers |= (lisp_modifiers & alt_modifier) ? MOD_ALT : 0;
12701 w32_modifiers |= (lisp_modifiers & ctrl_modifier) ? MOD_CONTROL : 0;
12702 w32_modifiers |= (lisp_modifiers & shift_modifier) ? MOD_SHIFT : 0;
12703
12704 return HOTKEY (vk_code, w32_modifiers);
12705}
12706
12707DEFUN ("w32-register-hot-key", Fw32_register_hot_key, Sw32_register_hot_key, 1, 1, 0,
12708 "Register KEY as a hot-key combination.\n\
12709Certain key combinations like Alt-Tab are reserved for system use on\n\
12710Windows, and therefore are normally intercepted by the system. However,\n\
12711most of these key combinations can be received by registering them as\n\
12712hot-keys, overriding their special meaning.\n\
12713\n\
12714KEY must be a one element key definition in vector form that would be\n\
12715acceptable to `define-key' (e.g. [A-tab] for Alt-Tab). The meta\n\
12716modifier is interpreted as Alt if `w32-alt-is-meta' is t, and hyper\n\
12717is always interpreted as the Windows modifier keys.\n\
12718\n\
12719The return value is the hotkey-id if registered, otherwise nil.")
12720 (key)
12721 Lisp_Object key;
12722{
12723 key = w32_parse_hot_key (key);
12724
12725 if (NILP (Fmemq (key, w32_grabbed_keys)))
12726 {
12727 /* Reuse an empty slot if possible. */
12728 Lisp_Object item = Fmemq (Qnil, w32_grabbed_keys);
12729
12730 /* Safe to add new key to list, even if we have focus. */
12731 if (NILP (item))
12732 w32_grabbed_keys = Fcons (key, w32_grabbed_keys);
12733 else
12734 XCAR (item) = key;
12735
12736 /* Notify input thread about new hot-key definition, so that it
12737 takes effect without needing to switch focus. */
12738 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
12739 (WPARAM) key, 0);
12740 }
12741
12742 return key;
12743}
12744
12745DEFUN ("w32-unregister-hot-key", Fw32_unregister_hot_key, Sw32_unregister_hot_key, 1, 1, 0,
12746 "Unregister HOTKEY as a hot-key combination.")
12747 (key)
12748 Lisp_Object key;
12749{
12750 Lisp_Object item;
12751
12752 if (!INTEGERP (key))
12753 key = w32_parse_hot_key (key);
12754
12755 item = Fmemq (key, w32_grabbed_keys);
12756
12757 if (!NILP (item))
12758 {
12759 /* Notify input thread about hot-key definition being removed, so
12760 that it takes effect without needing focus switch. */
12761 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
12762 (WPARAM) XINT (XCAR (item)), (LPARAM) item))
12763 {
12764 MSG msg;
12765 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
12766 }
12767 return Qt;
12768 }
12769 return Qnil;
12770}
12771
12772DEFUN ("w32-registered-hot-keys", Fw32_registered_hot_keys, Sw32_registered_hot_keys, 0, 0, 0,
12773 "Return list of registered hot-key IDs.")
12774 ()
12775{
12776 return Fcopy_sequence (w32_grabbed_keys);
12777}
12778
12779DEFUN ("w32-reconstruct-hot-key", Fw32_reconstruct_hot_key, Sw32_reconstruct_hot_key, 1, 1, 0,
12780 "Convert hot-key ID to a lisp key combination.")
12781 (hotkeyid)
12782 Lisp_Object hotkeyid;
12783{
12784 int vk_code, w32_modifiers;
12785 Lisp_Object key;
12786
12787 CHECK_NUMBER (hotkeyid, 0);
12788
12789 vk_code = HOTKEY_VK_CODE (hotkeyid);
12790 w32_modifiers = HOTKEY_MODIFIERS (hotkeyid);
12791
12792 if (lispy_function_keys[vk_code])
12793 key = intern (lispy_function_keys[vk_code]);
12794 else
12795 key = make_number (vk_code);
12796
12797 key = Fcons (key, Qnil);
12798 if (w32_modifiers & MOD_SHIFT)
3ef68e6b 12799 key = Fcons (Qshift, key);
ccc2d29c 12800 if (w32_modifiers & MOD_CONTROL)
3ef68e6b 12801 key = Fcons (Qctrl, key);
ccc2d29c 12802 if (w32_modifiers & MOD_ALT)
3ef68e6b 12803 key = Fcons (NILP (Vw32_alt_is_meta) ? Qalt : Qmeta, key);
ccc2d29c 12804 if (w32_modifiers & MOD_WIN)
3ef68e6b 12805 key = Fcons (Qhyper, key);
ccc2d29c
GV
12806
12807 return key;
12808}
adcc3809
GV
12809
12810DEFUN ("w32-toggle-lock-key", Fw32_toggle_lock_key, Sw32_toggle_lock_key, 1, 2, 0,
12811 "Toggle the state of the lock key KEY.\n\
12812KEY can be `capslock', `kp-numlock', or `scroll'.\n\
12813If the optional parameter NEW-STATE is a number, then the state of KEY\n\
12814is set to off if the low bit of NEW-STATE is zero, otherwise on.")
12815 (key, new_state)
12816 Lisp_Object key, new_state;
12817{
12818 int vk_code;
12819 int cur_state;
12820
12821 if (EQ (key, intern ("capslock")))
12822 vk_code = VK_CAPITAL;
12823 else if (EQ (key, intern ("kp-numlock")))
12824 vk_code = VK_NUMLOCK;
12825 else if (EQ (key, intern ("scroll")))
12826 vk_code = VK_SCROLL;
12827 else
12828 return Qnil;
12829
12830 if (!dwWindowsThreadId)
12831 return make_number (w32_console_toggle_lock_key (vk_code, new_state));
12832
12833 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
12834 (WPARAM) vk_code, (LPARAM) new_state))
12835 {
12836 MSG msg;
12837 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
12838 return make_number (msg.wParam);
12839 }
12840 return Qnil;
12841}
ee78dc32 12842\f
fbd6baed 12843syms_of_w32fns ()
ee78dc32 12844{
1edf84e7
GV
12845 /* This is zero if not using MS-Windows. */
12846 w32_in_use = 0;
12847
ee78dc32
GV
12848 /* The section below is built by the lisp expression at the top of the file,
12849 just above where these variables are declared. */
12850 /*&&& init symbols here &&&*/
12851 Qauto_raise = intern ("auto-raise");
12852 staticpro (&Qauto_raise);
12853 Qauto_lower = intern ("auto-lower");
12854 staticpro (&Qauto_lower);
ee78dc32
GV
12855 Qbar = intern ("bar");
12856 staticpro (&Qbar);
12857 Qborder_color = intern ("border-color");
12858 staticpro (&Qborder_color);
12859 Qborder_width = intern ("border-width");
12860 staticpro (&Qborder_width);
12861 Qbox = intern ("box");
12862 staticpro (&Qbox);
12863 Qcursor_color = intern ("cursor-color");
12864 staticpro (&Qcursor_color);
12865 Qcursor_type = intern ("cursor-type");
12866 staticpro (&Qcursor_type);
ee78dc32
GV
12867 Qgeometry = intern ("geometry");
12868 staticpro (&Qgeometry);
12869 Qicon_left = intern ("icon-left");
12870 staticpro (&Qicon_left);
12871 Qicon_top = intern ("icon-top");
12872 staticpro (&Qicon_top);
12873 Qicon_type = intern ("icon-type");
12874 staticpro (&Qicon_type);
12875 Qicon_name = intern ("icon-name");
12876 staticpro (&Qicon_name);
12877 Qinternal_border_width = intern ("internal-border-width");
12878 staticpro (&Qinternal_border_width);
12879 Qleft = intern ("left");
12880 staticpro (&Qleft);
1026b400
RS
12881 Qright = intern ("right");
12882 staticpro (&Qright);
ee78dc32
GV
12883 Qmouse_color = intern ("mouse-color");
12884 staticpro (&Qmouse_color);
12885 Qnone = intern ("none");
12886 staticpro (&Qnone);
12887 Qparent_id = intern ("parent-id");
12888 staticpro (&Qparent_id);
12889 Qscroll_bar_width = intern ("scroll-bar-width");
12890 staticpro (&Qscroll_bar_width);
12891 Qsuppress_icon = intern ("suppress-icon");
12892 staticpro (&Qsuppress_icon);
ee78dc32
GV
12893 Qundefined_color = intern ("undefined-color");
12894 staticpro (&Qundefined_color);
12895 Qvertical_scroll_bars = intern ("vertical-scroll-bars");
12896 staticpro (&Qvertical_scroll_bars);
12897 Qvisibility = intern ("visibility");
12898 staticpro (&Qvisibility);
12899 Qwindow_id = intern ("window-id");
12900 staticpro (&Qwindow_id);
12901 Qx_frame_parameter = intern ("x-frame-parameter");
12902 staticpro (&Qx_frame_parameter);
12903 Qx_resource_name = intern ("x-resource-name");
12904 staticpro (&Qx_resource_name);
12905 Quser_position = intern ("user-position");
12906 staticpro (&Quser_position);
12907 Quser_size = intern ("user-size");
12908 staticpro (&Quser_size);
6fc2811b
JR
12909 Qscreen_gamma = intern ("screen-gamma");
12910 staticpro (&Qscreen_gamma);
dfff8a69
JR
12911 Qline_spacing = intern ("line-spacing");
12912 staticpro (&Qline_spacing);
12913 Qcenter = intern ("center");
12914 staticpro (&Qcenter);
ee78dc32
GV
12915 /* This is the end of symbol initialization. */
12916
adcc3809
GV
12917 Qhyper = intern ("hyper");
12918 staticpro (&Qhyper);
12919 Qsuper = intern ("super");
12920 staticpro (&Qsuper);
12921 Qmeta = intern ("meta");
12922 staticpro (&Qmeta);
12923 Qalt = intern ("alt");
12924 staticpro (&Qalt);
12925 Qctrl = intern ("ctrl");
12926 staticpro (&Qctrl);
12927 Qcontrol = intern ("control");
12928 staticpro (&Qcontrol);
12929 Qshift = intern ("shift");
12930 staticpro (&Qshift);
12931
6fc2811b
JR
12932 /* Text property `display' should be nonsticky by default. */
12933 Vtext_property_default_nonsticky
12934 = Fcons (Fcons (Qdisplay, Qt), Vtext_property_default_nonsticky);
12935
12936
12937 Qlaplace = intern ("laplace");
12938 staticpro (&Qlaplace);
12939
4b817373
RS
12940 Qface_set_after_frame_default = intern ("face-set-after-frame-default");
12941 staticpro (&Qface_set_after_frame_default);
12942
ee78dc32
GV
12943 Fput (Qundefined_color, Qerror_conditions,
12944 Fcons (Qundefined_color, Fcons (Qerror, Qnil)));
12945 Fput (Qundefined_color, Qerror_message,
12946 build_string ("Undefined color"));
12947
ccc2d29c
GV
12948 staticpro (&w32_grabbed_keys);
12949 w32_grabbed_keys = Qnil;
12950
fbd6baed 12951 DEFVAR_LISP ("w32-color-map", &Vw32_color_map,
ccc2d29c 12952 "An array of color name mappings for windows.");
fbd6baed 12953 Vw32_color_map = Qnil;
ee78dc32 12954
fbd6baed 12955 DEFVAR_LISP ("w32-pass-alt-to-system", &Vw32_pass_alt_to_system,
da36a4d6
GV
12956 "Non-nil if alt key presses are passed on to Windows.\n\
12957When non-nil, for example, alt pressed and released and then space will\n\
12958open the System menu. When nil, Emacs silently swallows alt key events.");
fbd6baed 12959 Vw32_pass_alt_to_system = Qnil;
da36a4d6 12960
fbd6baed 12961 DEFVAR_LISP ("w32-alt-is-meta", &Vw32_alt_is_meta,
8c205c63
RS
12962 "Non-nil if the alt key is to be considered the same as the meta key.\n\
12963When nil, Emacs will translate the alt key to the Alt modifier, and not Meta.");
fbd6baed 12964 Vw32_alt_is_meta = Qt;
8c205c63 12965
7d081355
AI
12966 DEFVAR_INT ("w32-quit-key", &Vw32_quit_key,
12967 "If non-zero, the virtual key code for an alternative quit key.");
12968 XSETINT (Vw32_quit_key, 0);
12969
ccc2d29c
GV
12970 DEFVAR_LISP ("w32-pass-lwindow-to-system",
12971 &Vw32_pass_lwindow_to_system,
12972 "Non-nil if the left \"Windows\" key is passed on to Windows.\n\
12973When non-nil, the Start menu is opened by tapping the key.");
12974 Vw32_pass_lwindow_to_system = Qt;
12975
12976 DEFVAR_LISP ("w32-pass-rwindow-to-system",
12977 &Vw32_pass_rwindow_to_system,
12978 "Non-nil if the right \"Windows\" key is passed on to Windows.\n\
12979When non-nil, the Start menu is opened by tapping the key.");
12980 Vw32_pass_rwindow_to_system = Qt;
12981
adcc3809
GV
12982 DEFVAR_INT ("w32-phantom-key-code",
12983 &Vw32_phantom_key_code,
12984 "Virtual key code used to generate \"phantom\" key presses.\n\
12985Value is a number between 0 and 255.\n\
12986\n\
12987Phantom key presses are generated in order to stop the system from\n\
12988acting on \"Windows\" key events when `w32-pass-lwindow-to-system' or\n\
12989`w32-pass-rwindow-to-system' is nil.");
ce6059da
AI
12990 /* Although 255 is technically not a valid key code, it works and
12991 means that this hack won't interfere with any real key code. */
12992 Vw32_phantom_key_code = 255;
adcc3809 12993
ccc2d29c
GV
12994 DEFVAR_LISP ("w32-enable-num-lock",
12995 &Vw32_enable_num_lock,
12996 "Non-nil if Num Lock should act normally.\n\
12997Set to nil to see Num Lock as the key `kp-numlock'.");
12998 Vw32_enable_num_lock = Qt;
12999
13000 DEFVAR_LISP ("w32-enable-caps-lock",
13001 &Vw32_enable_caps_lock,
13002 "Non-nil if Caps Lock should act normally.\n\
13003Set to nil to see Caps Lock as the key `capslock'.");
13004 Vw32_enable_caps_lock = Qt;
13005
13006 DEFVAR_LISP ("w32-scroll-lock-modifier",
13007 &Vw32_scroll_lock_modifier,
13008 "Modifier to use for the Scroll Lock on state.\n\
13009The value can be hyper, super, meta, alt, control or shift for the\n\
13010respective modifier, or nil to see Scroll Lock as the key `scroll'.\n\
13011Any other value will cause the key to be ignored.");
13012 Vw32_scroll_lock_modifier = Qt;
13013
13014 DEFVAR_LISP ("w32-lwindow-modifier",
13015 &Vw32_lwindow_modifier,
13016 "Modifier to use for the left \"Windows\" key.\n\
13017The value can be hyper, super, meta, alt, control or shift for the\n\
13018respective modifier, or nil to appear as the key `lwindow'.\n\
13019Any other value will cause the key to be ignored.");
13020 Vw32_lwindow_modifier = Qnil;
13021
13022 DEFVAR_LISP ("w32-rwindow-modifier",
13023 &Vw32_rwindow_modifier,
13024 "Modifier to use for the right \"Windows\" key.\n\
13025The value can be hyper, super, meta, alt, control or shift for the\n\
13026respective modifier, or nil to appear as the key `rwindow'.\n\
13027Any other value will cause the key to be ignored.");
13028 Vw32_rwindow_modifier = Qnil;
13029
13030 DEFVAR_LISP ("w32-apps-modifier",
13031 &Vw32_apps_modifier,
13032 "Modifier to use for the \"Apps\" key.\n\
13033The value can be hyper, super, meta, alt, control or shift for the\n\
13034respective modifier, or nil to appear as the key `apps'.\n\
13035Any other value will cause the key to be ignored.");
13036 Vw32_apps_modifier = Qnil;
da36a4d6 13037
212da13b 13038 DEFVAR_LISP ("w32-enable-synthesized-fonts", &Vw32_enable_synthesized_fonts,
6fc2811b
JR
13039 "Non-nil enables selection of artificially italicized and bold fonts.");
13040 Vw32_enable_synthesized_fonts = Qnil;
5ac45f98 13041
fbd6baed 13042 DEFVAR_LISP ("w32-enable-palette", &Vw32_enable_palette,
6fc2811b 13043 "Non-nil enables Windows palette management to map colors exactly.");
fbd6baed 13044 Vw32_enable_palette = Qt;
5ac45f98 13045
fbd6baed
GV
13046 DEFVAR_INT ("w32-mouse-button-tolerance",
13047 &Vw32_mouse_button_tolerance,
6fc2811b 13048 "Analogue of double click interval for faking middle mouse events.\n\
5ac45f98
GV
13049The value is the minimum time in milliseconds that must elapse between\n\
13050left/right button down events before they are considered distinct events.\n\
13051If both mouse buttons are depressed within this interval, a middle mouse\n\
13052button down event is generated instead.");
fbd6baed 13053 XSETINT (Vw32_mouse_button_tolerance, GetDoubleClickTime () / 2);
5ac45f98 13054
fbd6baed
GV
13055 DEFVAR_INT ("w32-mouse-move-interval",
13056 &Vw32_mouse_move_interval,
84fb1139
KH
13057 "Minimum interval between mouse move events.\n\
13058The value is the minimum time in milliseconds that must elapse between\n\
13059successive mouse move (or scroll bar drag) events before they are\n\
13060reported as lisp events.");
247be837 13061 XSETINT (Vw32_mouse_move_interval, 0);
84fb1139 13062
ee78dc32
GV
13063 init_x_parm_symbols ();
13064
13065 DEFVAR_LISP ("x-bitmap-file-path", &Vx_bitmap_file_path,
fbd6baed 13066 "List of directories to search for bitmap files for w32.");
ee78dc32
GV
13067 Vx_bitmap_file_path = decode_env_path ((char *) 0, "PATH");
13068
13069 DEFVAR_LISP ("x-pointer-shape", &Vx_pointer_shape,
13070 "The shape of the pointer when over text.\n\
13071Changing the value does not affect existing frames\n\
13072unless you set the mouse color.");
13073 Vx_pointer_shape = Qnil;
13074
13075 DEFVAR_LISP ("x-resource-name", &Vx_resource_name,
13076 "The name Emacs uses to look up resources; for internal use only.\n\
13077`x-get-resource' uses this as the first component of the instance name\n\
13078when requesting resource values.\n\
13079Emacs initially sets `x-resource-name' to the name under which Emacs\n\
13080was invoked, or to the value specified with the `-name' or `-rn'\n\
13081switches, if present.");
13082 Vx_resource_name = Qnil;
13083
13084 Vx_nontext_pointer_shape = Qnil;
13085
13086 Vx_mode_pointer_shape = Qnil;
13087
6fc2811b
JR
13088 DEFVAR_LISP ("x-busy-pointer-shape", &Vx_busy_pointer_shape,
13089 "The shape of the pointer when Emacs is busy.\n\
13090This variable takes effect when you create a new frame\n\
13091or when you set the mouse color.");
13092 Vx_busy_pointer_shape = Qnil;
13093
13094 DEFVAR_BOOL ("display-busy-cursor", &display_busy_cursor_p,
13095 "Non-zero means Emacs displays a busy cursor on window systems.");
13096 display_busy_cursor_p = 1;
13097
f79e6790
JR
13098 DEFVAR_LISP ("busy-cursor-delay", &Vbusy_cursor_delay,
13099 "*Seconds to wait before displaying a busy-cursor.\n\
dfff8a69 13100Value must be an integer or float.");
f79e6790
JR
13101 Vbusy_cursor_delay = make_number (DEFAULT_BUSY_CURSOR_DELAY);
13102
6fc2811b 13103 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
ee78dc32
GV
13104 &Vx_sensitive_text_pointer_shape,
13105 "The shape of the pointer when over mouse-sensitive text.\n\
13106This variable takes effect when you create a new frame\n\
13107or when you set the mouse color.");
13108 Vx_sensitive_text_pointer_shape = Qnil;
13109
13110 DEFVAR_LISP ("x-cursor-fore-pixel", &Vx_cursor_fore_pixel,
13111 "A string indicating the foreground color of the cursor box.");
13112 Vx_cursor_fore_pixel = Qnil;
13113
13114 DEFVAR_LISP ("x-no-window-manager", &Vx_no_window_manager,
13115 "Non-nil if no window manager is in use.\n\
13116Emacs doesn't try to figure this out; this is always nil\n\
13117unless you set it to something else.");
13118 /* We don't have any way to find this out, so set it to nil
13119 and maybe the user would like to set it to t. */
13120 Vx_no_window_manager = Qnil;
13121
4587b026
GV
13122 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
13123 &Vx_pixel_size_width_font_regexp,
13124 "Regexp matching a font name whose width is the same as `PIXEL_SIZE'.\n\
13125\n\
13126Since Emacs gets width of a font matching with this regexp from\n\
13127PIXEL_SIZE field of the name, font finding mechanism gets faster for\n\
13128such a font. This is especially effective for such large fonts as\n\
13129Chinese, Japanese, and Korean.");
13130 Vx_pixel_size_width_font_regexp = Qnil;
13131
6fc2811b
JR
13132 DEFVAR_LISP ("image-cache-eviction-delay", &Vimage_cache_eviction_delay,
13133 "Time after which cached images are removed from the cache.\n\
13134When an image has not been displayed this many seconds, remove it\n\
13135from the image cache. Value must be an integer or nil with nil\n\
13136meaning don't clear the cache.");
13137 Vimage_cache_eviction_delay = make_number (30 * 60);
13138
33d52f9c
GV
13139 DEFVAR_LISP ("w32-bdf-filename-alist",
13140 &Vw32_bdf_filename_alist,
13141 "List of bdf fonts and their corresponding filenames.");
13142 Vw32_bdf_filename_alist = Qnil;
13143
1075afa9
GV
13144 DEFVAR_BOOL ("w32-strict-fontnames",
13145 &w32_strict_fontnames,
13146 "Non-nil means only use fonts that are exact matches for those requested.\n\
13147Default is nil, which allows old fontnames that are not XLFD compliant,\n\
13148and allows third-party CJK display to work by specifying false charset\n\
13149fields to trick Emacs into translating to Big5, SJIS etc.\n\
13150Setting this to t will prevent wrong fonts being selected when\n\
13151fontsets are automatically created.");
13152 w32_strict_fontnames = 0;
13153
c0611964
AI
13154 DEFVAR_BOOL ("w32-strict-painting",
13155 &w32_strict_painting,
13156 "Non-nil means use strict rules for repainting frames.\n\
13157Set this to nil to get the old behaviour for repainting; this should\n\
13158only be necessary if the default setting causes problems.");
13159 w32_strict_painting = 1;
13160
f46e6225
GV
13161 DEFVAR_LISP ("w32-system-coding-system",
13162 &Vw32_system_coding_system,
13163 "Coding system used by Windows system functions, such as for font names.");
13164 Vw32_system_coding_system = Qnil;
13165
dfff8a69
JR
13166 DEFVAR_LISP ("w32-charset-info-alist",
13167 &Vw32_charset_info_alist,
13168 "Alist linking Emacs character sets to Windows fonts\n\
13169and codepages. Each entry should be of the form:\n\
13170\n\
13171 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE))\n\
13172\n\
13173where CHARSET_NAME is a string used in font names to identify the charset,\n\
13174WINDOWS_CHARSET is a symbol that can be one of:\n\
13175w32-charset-ansi, w32-charset-default, w32-charset-symbol,\n\
13176w32-charset-shiftjis, w32-charset-hangul, w32-charset-gb2312,\n\
13177w32-charset-chinesebig5, "
13178#ifdef JOHAB_CHARSET
13179"w32-charset-johab, w32-charset-hebrew,\n\
13180w32-charset-arabic, w32-charset-greek, w32-charset-turkish,\n\
13181w32-charset-vietnamese, w32-charset-thai, w32-charset-easteurope,\n\
13182w32-charset-russian, w32-charset-mac, w32-charset-baltic,\n"
13183#endif
13184#ifdef UNICODE_CHARSET
13185"w32-charset-unicode, "
13186#endif
13187"or w32-charset-oem.\n\
13188CODEPAGE should be an integer specifying the codepage that should be used\n\
13189to display the character set, t to do no translation and output as Unicode,\n\
13190or nil to do no translation and output as 8 bit (or multibyte on far-east\n\
13191versions of Windows) characters.");
13192 Vw32_charset_info_alist = Qnil;
13193
13194 staticpro (&Qw32_charset_ansi);
13195 Qw32_charset_ansi = intern ("w32-charset-ansi");
13196 staticpro (&Qw32_charset_symbol);
13197 Qw32_charset_symbol = intern ("w32-charset-symbol");
13198 staticpro (&Qw32_charset_shiftjis);
13199 Qw32_charset_shiftjis = intern ("w32-charset-shiftjis");
13200 staticpro (&Qw32_charset_hangul);
13201 Qw32_charset_hangul = intern ("w32-charset-hangul");
13202 staticpro (&Qw32_charset_chinesebig5);
13203 Qw32_charset_chinesebig5 = intern ("w32-charset-chinesebig5");
13204 staticpro (&Qw32_charset_gb2312);
13205 Qw32_charset_gb2312 = intern ("w32-charset-gb2312");
13206 staticpro (&Qw32_charset_oem);
13207 Qw32_charset_oem = intern ("w32-charset-oem");
13208
13209#ifdef JOHAB_CHARSET
13210 {
13211 static int w32_extra_charsets_defined = 1;
13212 DEFVAR_BOOL ("w32-extra-charsets-defined", w32_extra_charsets_defined, "");
13213
13214 staticpro (&Qw32_charset_johab);
13215 Qw32_charset_johab = intern ("w32-charset-johab");
13216 staticpro (&Qw32_charset_easteurope);
13217 Qw32_charset_easteurope = intern ("w32-charset-easteurope");
13218 staticpro (&Qw32_charset_turkish);
13219 Qw32_charset_turkish = intern ("w32-charset-turkish");
13220 staticpro (&Qw32_charset_baltic);
13221 Qw32_charset_baltic = intern ("w32-charset-baltic");
13222 staticpro (&Qw32_charset_russian);
13223 Qw32_charset_russian = intern ("w32-charset-russian");
13224 staticpro (&Qw32_charset_arabic);
13225 Qw32_charset_arabic = intern ("w32-charset-arabic");
13226 staticpro (&Qw32_charset_greek);
13227 Qw32_charset_greek = intern ("w32-charset-greek");
13228 staticpro (&Qw32_charset_hebrew);
13229 Qw32_charset_hebrew = intern ("w32-charset-hebrew");
13230 staticpro (&Qw32_charset_thai);
13231 Qw32_charset_thai = intern ("w32-charset-thai");
13232 staticpro (&Qw32_charset_mac);
13233 Qw32_charset_mac = intern ("w32-charset-mac");
13234 }
13235#endif
13236
13237#ifdef UNICODE_CHARSET
13238 {
13239 static int w32_unicode_charset_defined = 1;
13240 DEFVAR_BOOL ("w32-unicode-charset-defined",
13241 w32_unicode_charset_defined, "");
13242
13243 staticpro (&Qw32_charset_unicode);
13244 Qw32_charset_unicode = intern ("w32-charset-unicode");
13245#endif
13246
ee78dc32 13247 defsubr (&Sx_get_resource);
6fc2811b
JR
13248#if 0 /* NTEMACS_TODO: Port to W32 */
13249 defsubr (&Sx_change_window_property);
13250 defsubr (&Sx_delete_window_property);
13251 defsubr (&Sx_window_property);
13252#endif
2d764c78 13253 defsubr (&Sxw_display_color_p);
ee78dc32 13254 defsubr (&Sx_display_grayscale_p);
2d764c78
EZ
13255 defsubr (&Sxw_color_defined_p);
13256 defsubr (&Sxw_color_values);
ee78dc32
GV
13257 defsubr (&Sx_server_max_request_size);
13258 defsubr (&Sx_server_vendor);
13259 defsubr (&Sx_server_version);
13260 defsubr (&Sx_display_pixel_width);
13261 defsubr (&Sx_display_pixel_height);
13262 defsubr (&Sx_display_mm_width);
13263 defsubr (&Sx_display_mm_height);
13264 defsubr (&Sx_display_screens);
13265 defsubr (&Sx_display_planes);
13266 defsubr (&Sx_display_color_cells);
13267 defsubr (&Sx_display_visual_class);
13268 defsubr (&Sx_display_backing_store);
13269 defsubr (&Sx_display_save_under);
13270 defsubr (&Sx_parse_geometry);
13271 defsubr (&Sx_create_frame);
ee78dc32
GV
13272 defsubr (&Sx_open_connection);
13273 defsubr (&Sx_close_connection);
13274 defsubr (&Sx_display_list);
13275 defsubr (&Sx_synchronize);
13276
fbd6baed 13277 /* W32 specific functions */
ee78dc32 13278
1edf84e7 13279 defsubr (&Sw32_focus_frame);
fbd6baed
GV
13280 defsubr (&Sw32_select_font);
13281 defsubr (&Sw32_define_rgb_color);
13282 defsubr (&Sw32_default_color_map);
13283 defsubr (&Sw32_load_color_file);
1edf84e7 13284 defsubr (&Sw32_send_sys_command);
55dcfc15 13285 defsubr (&Sw32_shell_execute);
ccc2d29c
GV
13286 defsubr (&Sw32_register_hot_key);
13287 defsubr (&Sw32_unregister_hot_key);
13288 defsubr (&Sw32_registered_hot_keys);
13289 defsubr (&Sw32_reconstruct_hot_key);
adcc3809 13290 defsubr (&Sw32_toggle_lock_key);
33d52f9c 13291 defsubr (&Sw32_find_bdf_fonts);
4587b026
GV
13292
13293 /* Setting callback functions for fontset handler. */
13294 get_font_info_func = w32_get_font_info;
6fc2811b
JR
13295
13296#if 0 /* This function pointer doesn't seem to be used anywhere.
13297 And the pointer assigned has the wrong type, anyway. */
4587b026 13298 list_fonts_func = w32_list_fonts;
6fc2811b
JR
13299#endif
13300
4587b026
GV
13301 load_font_func = w32_load_font;
13302 find_ccl_program_func = w32_find_ccl_program;
13303 query_font_func = w32_query_font;
13304 set_frame_fontset_func = x_set_font;
13305 check_window_system_func = check_w32;
6fc2811b
JR
13306
13307#if 0 /* NTEMACS_TODO Image support for W32 */
13308 /* Images. */
13309 Qxbm = intern ("xbm");
13310 staticpro (&Qxbm);
13311 QCtype = intern (":type");
13312 staticpro (&QCtype);
13313 QCalgorithm = intern (":algorithm");
13314 staticpro (&QCalgorithm);
13315 QCheuristic_mask = intern (":heuristic-mask");
13316 staticpro (&QCheuristic_mask);
13317 QCcolor_symbols = intern (":color-symbols");
13318 staticpro (&QCcolor_symbols);
6fc2811b
JR
13319 QCascent = intern (":ascent");
13320 staticpro (&QCascent);
13321 QCmargin = intern (":margin");
13322 staticpro (&QCmargin);
13323 QCrelief = intern (":relief");
13324 staticpro (&QCrelief);
13325 Qpostscript = intern ("postscript");
13326 staticpro (&Qpostscript);
13327 QCloader = intern (":loader");
13328 staticpro (&QCloader);
13329 QCbounding_box = intern (":bounding-box");
13330 staticpro (&QCbounding_box);
13331 QCpt_width = intern (":pt-width");
13332 staticpro (&QCpt_width);
13333 QCpt_height = intern (":pt-height");
13334 staticpro (&QCpt_height);
13335 QCindex = intern (":index");
13336 staticpro (&QCindex);
13337 Qpbm = intern ("pbm");
13338 staticpro (&Qpbm);
13339
13340#if HAVE_XPM
13341 Qxpm = intern ("xpm");
13342 staticpro (&Qxpm);
13343#endif
13344
13345#if HAVE_JPEG
13346 Qjpeg = intern ("jpeg");
13347 staticpro (&Qjpeg);
13348#endif
13349
13350#if HAVE_TIFF
13351 Qtiff = intern ("tiff");
13352 staticpro (&Qtiff);
13353#endif
13354
13355#if HAVE_GIF
13356 Qgif = intern ("gif");
13357 staticpro (&Qgif);
13358#endif
13359
13360#if HAVE_PNG
13361 Qpng = intern ("png");
13362 staticpro (&Qpng);
13363#endif
13364
13365 defsubr (&Sclear_image_cache);
13366
13367#if GLYPH_DEBUG
13368 defsubr (&Simagep);
13369 defsubr (&Slookup_image);
13370#endif
13371#endif /* NTEMACS_TODO */
13372
dfff8a69
JR
13373 busy_cursor_atimer = NULL;
13374 busy_cursor_shown_p = 0;
13375
6fc2811b
JR
13376 defsubr (&Sx_show_tip);
13377 defsubr (&Sx_hide_tip);
13378 staticpro (&tip_timer);
13379 tip_timer = Qnil;
13380
13381 defsubr (&Sx_file_dialog);
13382}
13383
13384
13385void
13386init_xfns ()
13387{
13388 image_types = NULL;
13389 Vimage_types = Qnil;
13390
13391#if 0 /* NTEMACS_TODO : Image support for W32 */
13392 define_image_type (&xbm_type);
13393 define_image_type (&gs_type);
13394 define_image_type (&pbm_type);
13395
13396#if HAVE_XPM
13397 define_image_type (&xpm_type);
13398#endif
13399
13400#if HAVE_JPEG
13401 define_image_type (&jpeg_type);
13402#endif
13403
13404#if HAVE_TIFF
13405 define_image_type (&tiff_type);
13406#endif
13407
13408#if HAVE_GIF
13409 define_image_type (&gif_type);
13410#endif
13411
13412#if HAVE_PNG
13413 define_image_type (&png_type);
13414#endif
13415#endif /* NTEMACS_TODO */
ee78dc32
GV
13416}
13417
13418#undef abort
13419
13420void
fbd6baed 13421w32_abort()
ee78dc32 13422{
5ac45f98
GV
13423 int button;
13424 button = MessageBox (NULL,
13425 "A fatal error has occurred!\n\n"
13426 "Select Abort to exit, Retry to debug, Ignore to continue",
13427 "Emacs Abort Dialog",
13428 MB_ICONEXCLAMATION | MB_TASKMODAL
13429 | MB_SETFOREGROUND | MB_ABORTRETRYIGNORE);
13430 switch (button)
13431 {
13432 case IDRETRY:
13433 DebugBreak ();
13434 break;
13435 case IDIGNORE:
13436 break;
13437 case IDABORT:
13438 default:
13439 abort ();
13440 break;
13441 }
ee78dc32 13442}
d573caac 13443
83c75055
GV
13444/* For convenience when debugging. */
13445int
13446w32_last_error()
13447{
13448 return GetLastError ();
13449}