(w32_strerror): New extern.
[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;
4694d762 157Lisp_Object Vx_busy_pointer_shape, Vx_window_horizontal_drag_shape;
6fc2811b 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;
767b1ff0 279Lisp_Object Qw32_charset_hangeul;
dfff8a69
JR
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;
767b1ff0 295Lisp_Object Qw32_charset_vietnamese;
dfff8a69
JR
296Lisp_Object Qw32_charset_thai;
297Lisp_Object Qw32_charset_johab;
298Lisp_Object Qw32_charset_mac;
299#endif
300
301#ifdef UNICODE_CHARSET
302Lisp_Object Qw32_charset_unicode;
303#endif
304
6fc2811b
JR
305extern Lisp_Object Qtop;
306extern Lisp_Object Qdisplay;
307extern Lisp_Object Qtool_bar_lines;
308
5ac45f98
GV
309/* State variables for emulating a three button mouse. */
310#define LMOUSE 1
311#define MMOUSE 2
312#define RMOUSE 4
313
314static int button_state = 0;
fbd6baed 315static W32Msg saved_mouse_button_msg;
84fb1139 316static unsigned mouse_button_timer; /* non-zero when timer is active */
fbd6baed 317static W32Msg saved_mouse_move_msg;
84fb1139
KH
318static unsigned mouse_move_timer;
319
93fbe8b7
GV
320/* W95 mousewheel handler */
321unsigned int msh_mousewheel = 0;
322
84fb1139
KH
323#define MOUSE_BUTTON_ID 1
324#define MOUSE_MOVE_ID 2
5ac45f98 325
ee78dc32 326/* The below are defined in frame.c. */
dfff8a69 327
ee78dc32 328extern Lisp_Object Qheight, Qminibuffer, Qname, Qonly, Qwidth;
1edf84e7 329extern Lisp_Object Qunsplittable, Qmenu_bar_lines, Qbuffer_predicate, Qtitle;
6fc2811b 330extern Lisp_Object Qtool_bar_lines;
ee78dc32
GV
331
332extern Lisp_Object Vwindow_system_version;
333
4b817373
RS
334Lisp_Object Qface_set_after_frame_default;
335
937e601e
AI
336#ifdef GLYPH_DEBUG
337int image_cache_refcount, dpyinfo_refcount;
338#endif
339
340
fbd6baed
GV
341/* From w32term.c. */
342extern Lisp_Object Vw32_num_mouse_buttons;
ccc2d29c 343extern Lisp_Object Vw32_recognize_altgr;
5ac45f98 344
ee78dc32 345\f
1edf84e7
GV
346/* Error if we are not connected to MS-Windows. */
347void
348check_w32 ()
349{
350 if (! w32_in_use)
351 error ("MS-Windows not in use or not initialized");
352}
353
354/* Nonzero if we can use mouse menus.
355 You should not call this unless HAVE_MENUS is defined. */
356
357int
358have_menus_p ()
359{
360 return w32_in_use;
361}
362
ee78dc32 363/* Extract a frame as a FRAME_PTR, defaulting to the selected frame
fbd6baed 364 and checking validity for W32. */
ee78dc32
GV
365
366FRAME_PTR
367check_x_frame (frame)
368 Lisp_Object frame;
369{
370 FRAME_PTR f;
371
372 if (NILP (frame))
6fc2811b
JR
373 frame = selected_frame;
374 CHECK_LIVE_FRAME (frame, 0);
375 f = XFRAME (frame);
fbd6baed
GV
376 if (! FRAME_W32_P (f))
377 error ("non-w32 frame used");
ee78dc32
GV
378 return f;
379}
380
381/* Let the user specify an display with a frame.
fbd6baed 382 nil stands for the selected frame--or, if that is not a w32 frame,
ee78dc32
GV
383 the first display on the list. */
384
fbd6baed 385static struct w32_display_info *
ee78dc32
GV
386check_x_display_info (frame)
387 Lisp_Object frame;
388{
389 if (NILP (frame))
390 {
6fc2811b
JR
391 struct frame *sf = XFRAME (selected_frame);
392
393 if (FRAME_W32_P (sf) && FRAME_LIVE_P (sf))
394 return FRAME_W32_DISPLAY_INFO (sf);
ee78dc32 395 else
fbd6baed 396 return &one_w32_display_info;
ee78dc32
GV
397 }
398 else if (STRINGP (frame))
399 return x_display_info_for_name (frame);
400 else
401 {
402 FRAME_PTR f;
403
404 CHECK_LIVE_FRAME (frame, 0);
405 f = XFRAME (frame);
fbd6baed
GV
406 if (! FRAME_W32_P (f))
407 error ("non-w32 frame used");
408 return FRAME_W32_DISPLAY_INFO (f);
ee78dc32
GV
409 }
410}
411\f
fbd6baed 412/* Return the Emacs frame-object corresponding to an w32 window.
ee78dc32
GV
413 It could be the frame's main window or an icon window. */
414
415/* This function can be called during GC, so use GC_xxx type test macros. */
416
417struct frame *
418x_window_to_frame (dpyinfo, wdesc)
fbd6baed 419 struct w32_display_info *dpyinfo;
ee78dc32
GV
420 HWND wdesc;
421{
422 Lisp_Object tail, frame;
423 struct frame *f;
424
8e713be6 425 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
ee78dc32 426 {
8e713be6 427 frame = XCAR (tail);
ee78dc32
GV
428 if (!GC_FRAMEP (frame))
429 continue;
430 f = XFRAME (frame);
2d764c78 431 if (!FRAME_W32_P (f) || FRAME_W32_DISPLAY_INFO (f) != dpyinfo)
ee78dc32 432 continue;
f79e6790
JR
433 if (f->output_data.w32->busy_window == wdesc)
434 return f;
435
767b1ff0 436 /* TODO: Check tooltips when supported. */
fbd6baed 437 if (FRAME_W32_WINDOW (f) == wdesc)
ee78dc32
GV
438 return f;
439 }
440 return 0;
441}
442
443\f
444
445/* Code to deal with bitmaps. Bitmaps are referenced by their bitmap
446 id, which is just an int that this section returns. Bitmaps are
447 reference counted so they can be shared among frames.
448
449 Bitmap indices are guaranteed to be > 0, so a negative number can
450 be used to indicate no bitmap.
451
452 If you use x_create_bitmap_from_data, then you must keep track of
453 the bitmaps yourself. That is, creating a bitmap from the same
454 data more than once will not be caught. */
455
456
457/* Functions to access the contents of a bitmap, given an id. */
458
459int
460x_bitmap_height (f, id)
461 FRAME_PTR f;
462 int id;
463{
fbd6baed 464 return FRAME_W32_DISPLAY_INFO (f)->bitmaps[id - 1].height;
ee78dc32
GV
465}
466
467int
468x_bitmap_width (f, id)
469 FRAME_PTR f;
470 int id;
471{
fbd6baed 472 return FRAME_W32_DISPLAY_INFO (f)->bitmaps[id - 1].width;
ee78dc32
GV
473}
474
475int
476x_bitmap_pixmap (f, id)
477 FRAME_PTR f;
478 int id;
479{
fbd6baed 480 return (int) FRAME_W32_DISPLAY_INFO (f)->bitmaps[id - 1].pixmap;
ee78dc32
GV
481}
482
483
484/* Allocate a new bitmap record. Returns index of new record. */
485
486static int
487x_allocate_bitmap_record (f)
488 FRAME_PTR f;
489{
fbd6baed 490 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
ee78dc32
GV
491 int i;
492
493 if (dpyinfo->bitmaps == NULL)
494 {
495 dpyinfo->bitmaps_size = 10;
496 dpyinfo->bitmaps
fbd6baed 497 = (struct w32_bitmap_record *) xmalloc (dpyinfo->bitmaps_size * sizeof (struct w32_bitmap_record));
ee78dc32
GV
498 dpyinfo->bitmaps_last = 1;
499 return 1;
500 }
501
502 if (dpyinfo->bitmaps_last < dpyinfo->bitmaps_size)
503 return ++dpyinfo->bitmaps_last;
504
505 for (i = 0; i < dpyinfo->bitmaps_size; ++i)
506 if (dpyinfo->bitmaps[i].refcount == 0)
507 return i + 1;
508
509 dpyinfo->bitmaps_size *= 2;
510 dpyinfo->bitmaps
fbd6baed
GV
511 = (struct w32_bitmap_record *) xrealloc (dpyinfo->bitmaps,
512 dpyinfo->bitmaps_size * sizeof (struct w32_bitmap_record));
ee78dc32
GV
513 return ++dpyinfo->bitmaps_last;
514}
515
516/* Add one reference to the reference count of the bitmap with id ID. */
517
518void
519x_reference_bitmap (f, id)
520 FRAME_PTR f;
521 int id;
522{
fbd6baed 523 ++FRAME_W32_DISPLAY_INFO (f)->bitmaps[id - 1].refcount;
ee78dc32
GV
524}
525
526/* Create a bitmap for frame F from a HEIGHT x WIDTH array of bits at BITS. */
527
528int
529x_create_bitmap_from_data (f, bits, width, height)
530 struct frame *f;
531 char *bits;
532 unsigned int width, height;
533{
fbd6baed 534 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
ee78dc32
GV
535 Pixmap bitmap;
536 int id;
537
538 bitmap = CreateBitmap (width, height,
fbd6baed
GV
539 FRAME_W32_DISPLAY_INFO (XFRAME (frame))->n_planes,
540 FRAME_W32_DISPLAY_INFO (XFRAME (frame))->n_cbits,
ee78dc32
GV
541 bits);
542
543 if (! bitmap)
544 return -1;
545
546 id = x_allocate_bitmap_record (f);
547 dpyinfo->bitmaps[id - 1].pixmap = bitmap;
548 dpyinfo->bitmaps[id - 1].file = NULL;
549 dpyinfo->bitmaps[id - 1].hinst = NULL;
550 dpyinfo->bitmaps[id - 1].refcount = 1;
551 dpyinfo->bitmaps[id - 1].depth = 1;
552 dpyinfo->bitmaps[id - 1].height = height;
553 dpyinfo->bitmaps[id - 1].width = width;
554
555 return id;
556}
557
558/* Create bitmap from file FILE for frame F. */
559
560int
561x_create_bitmap_from_file (f, file)
562 struct frame *f;
563 Lisp_Object file;
564{
565 return -1;
767b1ff0 566#if 0 /* TODO : bitmap support */
fbd6baed 567 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
ee78dc32 568 unsigned int width, height;
6fc2811b 569 HBITMAP bitmap;
ee78dc32
GV
570 int xhot, yhot, result, id;
571 Lisp_Object found;
572 int fd;
573 char *filename;
574 HINSTANCE hinst;
575
576 /* Look for an existing bitmap with the same name. */
577 for (id = 0; id < dpyinfo->bitmaps_last; ++id)
578 {
579 if (dpyinfo->bitmaps[id].refcount
580 && dpyinfo->bitmaps[id].file
581 && !strcmp (dpyinfo->bitmaps[id].file, (char *) XSTRING (file)->data))
582 {
583 ++dpyinfo->bitmaps[id].refcount;
584 return id + 1;
585 }
586 }
587
588 /* Search bitmap-file-path for the file, if appropriate. */
589 fd = openp (Vx_bitmap_file_path, file, "", &found, 0);
590 if (fd < 0)
591 return -1;
6fc2811b 592 emacs_close (fd);
ee78dc32
GV
593
594 filename = (char *) XSTRING (found)->data;
595
596 hinst = LoadLibraryEx (filename, NULL, LOAD_LIBRARY_AS_DATAFILE);
597
598 if (hinst == NULL)
599 return -1;
600
601
fbd6baed 602 result = XReadBitmapFile (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
ee78dc32
GV
603 filename, &width, &height, &bitmap, &xhot, &yhot);
604 if (result != BitmapSuccess)
605 return -1;
606
607 id = x_allocate_bitmap_record (f);
608 dpyinfo->bitmaps[id - 1].pixmap = bitmap;
609 dpyinfo->bitmaps[id - 1].refcount = 1;
610 dpyinfo->bitmaps[id - 1].file = (char *) xmalloc (XSTRING (file)->size + 1);
611 dpyinfo->bitmaps[id - 1].depth = 1;
612 dpyinfo->bitmaps[id - 1].height = height;
613 dpyinfo->bitmaps[id - 1].width = width;
614 strcpy (dpyinfo->bitmaps[id - 1].file, XSTRING (file)->data);
615
616 return id;
767b1ff0 617#endif /* TODO */
ee78dc32
GV
618}
619
620/* Remove reference to bitmap with id number ID. */
621
33d52f9c 622void
ee78dc32
GV
623x_destroy_bitmap (f, id)
624 FRAME_PTR f;
625 int id;
626{
fbd6baed 627 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
ee78dc32
GV
628
629 if (id > 0)
630 {
631 --dpyinfo->bitmaps[id - 1].refcount;
632 if (dpyinfo->bitmaps[id - 1].refcount == 0)
633 {
634 BLOCK_INPUT;
635 DeleteObject (dpyinfo->bitmaps[id - 1].pixmap);
636 if (dpyinfo->bitmaps[id - 1].file)
637 {
6fc2811b 638 xfree (dpyinfo->bitmaps[id - 1].file);
ee78dc32
GV
639 dpyinfo->bitmaps[id - 1].file = NULL;
640 }
641 UNBLOCK_INPUT;
642 }
643 }
644}
645
646/* Free all the bitmaps for the display specified by DPYINFO. */
647
648static void
649x_destroy_all_bitmaps (dpyinfo)
fbd6baed 650 struct w32_display_info *dpyinfo;
ee78dc32
GV
651{
652 int i;
653 for (i = 0; i < dpyinfo->bitmaps_last; i++)
654 if (dpyinfo->bitmaps[i].refcount > 0)
655 {
656 DeleteObject (dpyinfo->bitmaps[i].pixmap);
657 if (dpyinfo->bitmaps[i].file)
6fc2811b 658 xfree (dpyinfo->bitmaps[i].file);
ee78dc32
GV
659 }
660 dpyinfo->bitmaps_last = 0;
661}
662\f
fbd6baed 663/* Connect the frame-parameter names for W32 frames
ee78dc32
GV
664 to the ways of passing the parameter values to the window system.
665
666 The name of a parameter, as a Lisp symbol,
667 has an `x-frame-parameter' property which is an integer in Lisp
668 but can be interpreted as an `enum x_frame_parm' in C. */
669
670enum x_frame_parm
671{
672 X_PARM_FOREGROUND_COLOR,
673 X_PARM_BACKGROUND_COLOR,
674 X_PARM_MOUSE_COLOR,
675 X_PARM_CURSOR_COLOR,
676 X_PARM_BORDER_COLOR,
677 X_PARM_ICON_TYPE,
678 X_PARM_FONT,
679 X_PARM_BORDER_WIDTH,
680 X_PARM_INTERNAL_BORDER_WIDTH,
681 X_PARM_NAME,
682 X_PARM_AUTORAISE,
683 X_PARM_AUTOLOWER,
684 X_PARM_VERT_SCROLL_BAR,
685 X_PARM_VISIBILITY,
686 X_PARM_MENU_BAR_LINES
687};
688
689
690struct x_frame_parm_table
691{
692 char *name;
6fc2811b 693 void (*setter) P_ ((struct frame *, Lisp_Object, Lisp_Object));
ee78dc32
GV
694};
695
937e601e
AI
696static Lisp_Object unwind_create_frame P_ ((Lisp_Object));
697static Lisp_Object unwind_create_tip_frame P_ ((Lisp_Object));
698static void x_change_window_heights P_ ((Lisp_Object, int));
767b1ff0 699/* TODO: Native Input Method support; see x_create_im. */
6fc2811b 700void x_set_foreground_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
dfff8a69 701static void x_set_line_spacing P_ ((struct frame *, Lisp_Object, Lisp_Object));
6fc2811b
JR
702void x_set_background_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
703void x_set_mouse_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
704void x_set_cursor_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
705void x_set_border_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
706void x_set_cursor_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
707void x_set_icon_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
708void x_set_icon_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
709void x_set_font P_ ((struct frame *, Lisp_Object, Lisp_Object));
710void x_set_border_width P_ ((struct frame *, Lisp_Object, Lisp_Object));
711void x_set_internal_border_width P_ ((struct frame *, Lisp_Object,
712 Lisp_Object));
713void x_explicitly_set_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
714void x_set_autoraise P_ ((struct frame *, Lisp_Object, Lisp_Object));
715void x_set_autolower P_ ((struct frame *, Lisp_Object, Lisp_Object));
716void x_set_vertical_scroll_bars P_ ((struct frame *, Lisp_Object,
717 Lisp_Object));
718void x_set_visibility P_ ((struct frame *, Lisp_Object, Lisp_Object));
719void x_set_menu_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
720void x_set_scroll_bar_width P_ ((struct frame *, Lisp_Object, Lisp_Object));
721void x_set_title P_ ((struct frame *, Lisp_Object, Lisp_Object));
722void x_set_unsplittable P_ ((struct frame *, Lisp_Object, Lisp_Object));
723void x_set_tool_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
724static void x_set_screen_gamma P_ ((struct frame *, Lisp_Object, Lisp_Object));
ee78dc32
GV
725
726static struct x_frame_parm_table x_frame_parms[] =
727{
1edf84e7
GV
728 "auto-raise", x_set_autoraise,
729 "auto-lower", x_set_autolower,
ee78dc32 730 "background-color", x_set_background_color,
ee78dc32 731 "border-color", x_set_border_color,
1edf84e7
GV
732 "border-width", x_set_border_width,
733 "cursor-color", x_set_cursor_color,
ee78dc32 734 "cursor-type", x_set_cursor_type,
ee78dc32 735 "font", x_set_font,
1edf84e7
GV
736 "foreground-color", x_set_foreground_color,
737 "icon-name", x_set_icon_name,
738 "icon-type", x_set_icon_type,
ee78dc32 739 "internal-border-width", x_set_internal_border_width,
ee78dc32 740 "menu-bar-lines", x_set_menu_bar_lines,
1edf84e7
GV
741 "mouse-color", x_set_mouse_color,
742 "name", x_explicitly_set_name,
ee78dc32 743 "scroll-bar-width", x_set_scroll_bar_width,
1edf84e7 744 "title", x_set_title,
ee78dc32 745 "unsplittable", x_set_unsplittable,
1edf84e7
GV
746 "vertical-scroll-bars", x_set_vertical_scroll_bars,
747 "visibility", x_set_visibility,
6fc2811b 748 "tool-bar-lines", x_set_tool_bar_lines,
dfff8a69
JR
749 "screen-gamma", x_set_screen_gamma,
750 "line-spacing", x_set_line_spacing
ee78dc32
GV
751};
752
753/* Attach the `x-frame-parameter' properties to
fbd6baed 754 the Lisp symbol names of parameters relevant to W32. */
ee78dc32 755
dfff8a69 756void
ee78dc32
GV
757init_x_parm_symbols ()
758{
759 int i;
760
761 for (i = 0; i < sizeof (x_frame_parms) / sizeof (x_frame_parms[0]); i++)
762 Fput (intern (x_frame_parms[i].name), Qx_frame_parameter,
763 make_number (i));
764}
765\f
dfff8a69 766/* Change the parameters of frame F as specified by ALIST.
ee78dc32
GV
767 If a parameter is not specially recognized, do nothing;
768 otherwise call the `x_set_...' function for that parameter. */
769
770void
771x_set_frame_parameters (f, alist)
772 FRAME_PTR f;
773 Lisp_Object alist;
774{
775 Lisp_Object tail;
776
777 /* If both of these parameters are present, it's more efficient to
778 set them both at once. So we wait until we've looked at the
779 entire list before we set them. */
b839712d 780 int width, height;
ee78dc32
GV
781
782 /* Same here. */
783 Lisp_Object left, top;
784
785 /* Same with these. */
786 Lisp_Object icon_left, icon_top;
787
788 /* Record in these vectors all the parms specified. */
789 Lisp_Object *parms;
790 Lisp_Object *values;
a797a73d 791 int i, p;
ee78dc32
GV
792 int left_no_change = 0, top_no_change = 0;
793 int icon_left_no_change = 0, icon_top_no_change = 0;
794
5878523b
RS
795 struct gcpro gcpro1, gcpro2;
796
ee78dc32
GV
797 i = 0;
798 for (tail = alist; CONSP (tail); tail = Fcdr (tail))
799 i++;
800
801 parms = (Lisp_Object *) alloca (i * sizeof (Lisp_Object));
802 values = (Lisp_Object *) alloca (i * sizeof (Lisp_Object));
803
804 /* Extract parm names and values into those vectors. */
805
806 i = 0;
807 for (tail = alist; CONSP (tail); tail = Fcdr (tail))
808 {
6fc2811b 809 Lisp_Object elt;
ee78dc32
GV
810
811 elt = Fcar (tail);
812 parms[i] = Fcar (elt);
813 values[i] = Fcdr (elt);
814 i++;
815 }
5878523b
RS
816 /* TAIL and ALIST are not used again below here. */
817 alist = tail = Qnil;
818
819 GCPRO2 (*parms, *values);
820 gcpro1.nvars = i;
821 gcpro2.nvars = i;
822
823 /* There is no need to gcpro LEFT, TOP, ICON_LEFT, or ICON_TOP,
824 because their values appear in VALUES and strings are not valid. */
b839712d 825 top = left = Qunbound;
ee78dc32
GV
826 icon_left = icon_top = Qunbound;
827
b839712d 828 /* Provide default values for HEIGHT and WIDTH. */
dfff8a69
JR
829 if (FRAME_NEW_WIDTH (f))
830 width = FRAME_NEW_WIDTH (f);
831 else
832 width = FRAME_WIDTH (f);
833
834 if (FRAME_NEW_HEIGHT (f))
835 height = FRAME_NEW_HEIGHT (f);
836 else
837 height = FRAME_HEIGHT (f);
b839712d 838
a797a73d
GV
839 /* Process foreground_color and background_color before anything else.
840 They are independent of other properties, but other properties (e.g.,
841 cursor_color) are dependent upon them. */
842 for (p = 0; p < i; p++)
843 {
844 Lisp_Object prop, val;
845
846 prop = parms[p];
847 val = values[p];
848 if (EQ (prop, Qforeground_color) || EQ (prop, Qbackground_color))
849 {
850 register Lisp_Object param_index, old_value;
851
852 param_index = Fget (prop, Qx_frame_parameter);
853 old_value = get_frame_param (f, prop);
854 store_frame_param (f, prop, val);
855 if (NATNUMP (param_index)
856 && (XFASTINT (param_index)
857 < sizeof (x_frame_parms)/sizeof (x_frame_parms[0])))
858 (*x_frame_parms[XINT (param_index)].setter)(f, val, old_value);
859 }
860 }
861
ee78dc32
GV
862 /* Now process them in reverse of specified order. */
863 for (i--; i >= 0; i--)
864 {
865 Lisp_Object prop, val;
866
867 prop = parms[i];
868 val = values[i];
869
b839712d
RS
870 if (EQ (prop, Qwidth) && NUMBERP (val))
871 width = XFASTINT (val);
872 else if (EQ (prop, Qheight) && NUMBERP (val))
873 height = XFASTINT (val);
ee78dc32
GV
874 else if (EQ (prop, Qtop))
875 top = val;
876 else if (EQ (prop, Qleft))
877 left = val;
878 else if (EQ (prop, Qicon_top))
879 icon_top = val;
880 else if (EQ (prop, Qicon_left))
881 icon_left = val;
a797a73d
GV
882 else if (EQ (prop, Qforeground_color) || EQ (prop, Qbackground_color))
883 /* Processed above. */
884 continue;
ee78dc32
GV
885 else
886 {
887 register Lisp_Object param_index, old_value;
888
889 param_index = Fget (prop, Qx_frame_parameter);
890 old_value = get_frame_param (f, prop);
891 store_frame_param (f, prop, val);
892 if (NATNUMP (param_index)
893 && (XFASTINT (param_index)
894 < sizeof (x_frame_parms)/sizeof (x_frame_parms[0])))
3c190163 895 (*x_frame_parms[XINT (param_index)].setter)(f, val, old_value);
ee78dc32
GV
896 }
897 }
898
899 /* Don't die if just one of these was set. */
900 if (EQ (left, Qunbound))
901 {
902 left_no_change = 1;
fbd6baed
GV
903 if (f->output_data.w32->left_pos < 0)
904 left = Fcons (Qplus, Fcons (make_number (f->output_data.w32->left_pos), Qnil));
ee78dc32 905 else
fbd6baed 906 XSETINT (left, f->output_data.w32->left_pos);
ee78dc32
GV
907 }
908 if (EQ (top, Qunbound))
909 {
910 top_no_change = 1;
fbd6baed
GV
911 if (f->output_data.w32->top_pos < 0)
912 top = Fcons (Qplus, Fcons (make_number (f->output_data.w32->top_pos), Qnil));
ee78dc32 913 else
fbd6baed 914 XSETINT (top, f->output_data.w32->top_pos);
ee78dc32
GV
915 }
916
917 /* If one of the icon positions was not set, preserve or default it. */
918 if (EQ (icon_left, Qunbound) || ! INTEGERP (icon_left))
919 {
920 icon_left_no_change = 1;
921 icon_left = Fcdr (Fassq (Qicon_left, f->param_alist));
922 if (NILP (icon_left))
923 XSETINT (icon_left, 0);
924 }
925 if (EQ (icon_top, Qunbound) || ! INTEGERP (icon_top))
926 {
927 icon_top_no_change = 1;
928 icon_top = Fcdr (Fassq (Qicon_top, f->param_alist));
929 if (NILP (icon_top))
930 XSETINT (icon_top, 0);
931 }
932
ee78dc32
GV
933 /* Don't set these parameters unless they've been explicitly
934 specified. The window might be mapped or resized while we're in
935 this function, and we don't want to override that unless the lisp
936 code has asked for it.
937
938 Don't set these parameters unless they actually differ from the
939 window's current parameters; the window may not actually exist
940 yet. */
941 {
942 Lisp_Object frame;
943
944 check_frame_size (f, &height, &width);
945
946 XSETFRAME (frame, f);
947
dfff8a69
JR
948 if (width != FRAME_WIDTH (f)
949 || height != FRAME_HEIGHT (f)
950 || FRAME_NEW_HEIGHT (f) || FRAME_NEW_WIDTH (f))
b839712d 951 Fset_frame_size (frame, make_number (width), make_number (height));
ee78dc32
GV
952
953 if ((!NILP (left) || !NILP (top))
954 && ! (left_no_change && top_no_change)
fbd6baed
GV
955 && ! (NUMBERP (left) && XINT (left) == f->output_data.w32->left_pos
956 && NUMBERP (top) && XINT (top) == f->output_data.w32->top_pos))
ee78dc32
GV
957 {
958 int leftpos = 0;
959 int toppos = 0;
960
961 /* Record the signs. */
fbd6baed 962 f->output_data.w32->size_hint_flags &= ~ (XNegative | YNegative);
ee78dc32 963 if (EQ (left, Qminus))
fbd6baed 964 f->output_data.w32->size_hint_flags |= XNegative;
ee78dc32
GV
965 else if (INTEGERP (left))
966 {
967 leftpos = XINT (left);
968 if (leftpos < 0)
fbd6baed 969 f->output_data.w32->size_hint_flags |= XNegative;
ee78dc32 970 }
8e713be6
KR
971 else if (CONSP (left) && EQ (XCAR (left), Qminus)
972 && CONSP (XCDR (left))
973 && INTEGERP (XCAR (XCDR (left))))
ee78dc32 974 {
8e713be6 975 leftpos = - XINT (XCAR (XCDR (left)));
fbd6baed 976 f->output_data.w32->size_hint_flags |= XNegative;
ee78dc32 977 }
8e713be6
KR
978 else if (CONSP (left) && EQ (XCAR (left), Qplus)
979 && CONSP (XCDR (left))
980 && INTEGERP (XCAR (XCDR (left))))
ee78dc32 981 {
8e713be6 982 leftpos = XINT (XCAR (XCDR (left)));
ee78dc32
GV
983 }
984
985 if (EQ (top, Qminus))
fbd6baed 986 f->output_data.w32->size_hint_flags |= YNegative;
ee78dc32
GV
987 else if (INTEGERP (top))
988 {
989 toppos = XINT (top);
990 if (toppos < 0)
fbd6baed 991 f->output_data.w32->size_hint_flags |= YNegative;
ee78dc32 992 }
8e713be6
KR
993 else if (CONSP (top) && EQ (XCAR (top), Qminus)
994 && CONSP (XCDR (top))
995 && INTEGERP (XCAR (XCDR (top))))
ee78dc32 996 {
8e713be6 997 toppos = - XINT (XCAR (XCDR (top)));
fbd6baed 998 f->output_data.w32->size_hint_flags |= YNegative;
ee78dc32 999 }
8e713be6
KR
1000 else if (CONSP (top) && EQ (XCAR (top), Qplus)
1001 && CONSP (XCDR (top))
1002 && INTEGERP (XCAR (XCDR (top))))
ee78dc32 1003 {
8e713be6 1004 toppos = XINT (XCAR (XCDR (top)));
ee78dc32
GV
1005 }
1006
1007
1008 /* Store the numeric value of the position. */
fbd6baed
GV
1009 f->output_data.w32->top_pos = toppos;
1010 f->output_data.w32->left_pos = leftpos;
ee78dc32 1011
fbd6baed 1012 f->output_data.w32->win_gravity = NorthWestGravity;
ee78dc32
GV
1013
1014 /* Actually set that position, and convert to absolute. */
1015 x_set_offset (f, leftpos, toppos, -1);
1016 }
1017
1018 if ((!NILP (icon_left) || !NILP (icon_top))
1019 && ! (icon_left_no_change && icon_top_no_change))
1020 x_wm_set_icon_position (f, XINT (icon_left), XINT (icon_top));
1021 }
5878523b
RS
1022
1023 UNGCPRO;
ee78dc32
GV
1024}
1025
1026/* Store the screen positions of frame F into XPTR and YPTR.
1027 These are the positions of the containing window manager window,
1028 not Emacs's own window. */
1029
1030void
1031x_real_positions (f, xptr, yptr)
1032 FRAME_PTR f;
1033 int *xptr, *yptr;
1034{
1035 POINT pt;
3c190163
GV
1036
1037 {
1038 RECT rect;
ee78dc32 1039
fbd6baed
GV
1040 GetClientRect(FRAME_W32_WINDOW(f), &rect);
1041 AdjustWindowRect(&rect, f->output_data.w32->dwStyle, FRAME_EXTERNAL_MENU_BAR(f));
ee78dc32 1042
3c190163
GV
1043 pt.x = rect.left;
1044 pt.y = rect.top;
1045 }
ee78dc32 1046
fbd6baed 1047 ClientToScreen (FRAME_W32_WINDOW(f), &pt);
ee78dc32
GV
1048
1049 *xptr = pt.x;
1050 *yptr = pt.y;
1051}
1052
1053/* Insert a description of internally-recorded parameters of frame X
1054 into the parameter alist *ALISTPTR that is to be given to the user.
fbd6baed 1055 Only parameters that are specific to W32
ee78dc32
GV
1056 and whose values are not correctly recorded in the frame's
1057 param_alist need to be considered here. */
1058
dfff8a69 1059void
ee78dc32
GV
1060x_report_frame_params (f, alistptr)
1061 struct frame *f;
1062 Lisp_Object *alistptr;
1063{
1064 char buf[16];
1065 Lisp_Object tem;
1066
1067 /* Represent negative positions (off the top or left screen edge)
1068 in a way that Fmodify_frame_parameters will understand correctly. */
fbd6baed
GV
1069 XSETINT (tem, f->output_data.w32->left_pos);
1070 if (f->output_data.w32->left_pos >= 0)
ee78dc32
GV
1071 store_in_alist (alistptr, Qleft, tem);
1072 else
1073 store_in_alist (alistptr, Qleft, Fcons (Qplus, Fcons (tem, Qnil)));
1074
fbd6baed
GV
1075 XSETINT (tem, f->output_data.w32->top_pos);
1076 if (f->output_data.w32->top_pos >= 0)
ee78dc32
GV
1077 store_in_alist (alistptr, Qtop, tem);
1078 else
1079 store_in_alist (alistptr, Qtop, Fcons (Qplus, Fcons (tem, Qnil)));
1080
1081 store_in_alist (alistptr, Qborder_width,
fbd6baed 1082 make_number (f->output_data.w32->border_width));
ee78dc32 1083 store_in_alist (alistptr, Qinternal_border_width,
fbd6baed
GV
1084 make_number (f->output_data.w32->internal_border_width));
1085 sprintf (buf, "%ld", (long) FRAME_W32_WINDOW (f));
ee78dc32
GV
1086 store_in_alist (alistptr, Qwindow_id,
1087 build_string (buf));
1088 store_in_alist (alistptr, Qicon_name, f->icon_name);
1089 FRAME_SAMPLE_VISIBILITY (f);
1090 store_in_alist (alistptr, Qvisibility,
1091 (FRAME_VISIBLE_P (f) ? Qt
1092 : FRAME_ICONIFIED_P (f) ? Qicon : Qnil));
1093 store_in_alist (alistptr, Qdisplay,
8e713be6 1094 XCAR (FRAME_W32_DISPLAY_INFO (f)->name_list_element));
ee78dc32
GV
1095}
1096\f
1097
fbd6baed 1098DEFUN ("w32-define-rgb-color", Fw32_define_rgb_color, Sw32_define_rgb_color, 4, 4, 0,
5ac45f98 1099 "Convert RGB numbers to a windows color reference and associate with NAME (a string).\n\
fbd6baed 1100This adds or updates a named color to w32-color-map, making it available for use.\n\
5ac45f98
GV
1101The original entry's RGB ref is returned, or nil if the entry is new.")
1102 (red, green, blue, name)
1103 Lisp_Object red, green, blue, name;
ee78dc32 1104{
5ac45f98
GV
1105 Lisp_Object rgb;
1106 Lisp_Object oldrgb = Qnil;
1107 Lisp_Object entry;
1108
1109 CHECK_NUMBER (red, 0);
1110 CHECK_NUMBER (green, 0);
1111 CHECK_NUMBER (blue, 0);
1112 CHECK_STRING (name, 0);
ee78dc32 1113
5ac45f98 1114 XSET (rgb, Lisp_Int, RGB(XUINT (red), XUINT (green), XUINT (blue)));
ee78dc32 1115
5ac45f98 1116 BLOCK_INPUT;
ee78dc32 1117
fbd6baed
GV
1118 /* replace existing entry in w32-color-map or add new entry. */
1119 entry = Fassoc (name, Vw32_color_map);
5ac45f98
GV
1120 if (NILP (entry))
1121 {
1122 entry = Fcons (name, rgb);
fbd6baed 1123 Vw32_color_map = Fcons (entry, Vw32_color_map);
5ac45f98
GV
1124 }
1125 else
1126 {
1127 oldrgb = Fcdr (entry);
1128 Fsetcdr (entry, rgb);
1129 }
1130
1131 UNBLOCK_INPUT;
1132
1133 return (oldrgb);
ee78dc32
GV
1134}
1135
fbd6baed 1136DEFUN ("w32-load-color-file", Fw32_load_color_file, Sw32_load_color_file, 1, 1, 0,
5ac45f98 1137 "Create an alist of color entries from an external file (ie. rgb.txt).\n\
fbd6baed 1138Assign this value to w32-color-map to replace the existing color map.\n\
5ac45f98
GV
1139\
1140The file should define one named RGB color per line like so:\
1141 R G B name\n\
1142where R,G,B are numbers between 0 and 255 and name is an arbitrary string.")
1143 (filename)
1144 Lisp_Object filename;
1145{
1146 FILE *fp;
1147 Lisp_Object cmap = Qnil;
1148 Lisp_Object abspath;
1149
1150 CHECK_STRING (filename, 0);
1151 abspath = Fexpand_file_name (filename, Qnil);
1152
1153 fp = fopen (XSTRING (filename)->data, "rt");
1154 if (fp)
1155 {
1156 char buf[512];
1157 int red, green, blue;
1158 int num;
1159
1160 BLOCK_INPUT;
1161
1162 while (fgets (buf, sizeof (buf), fp) != NULL) {
1163 if (sscanf (buf, "%u %u %u %n", &red, &green, &blue, &num) == 3)
1164 {
1165 char *name = buf + num;
1166 num = strlen (name) - 1;
1167 if (name[num] == '\n')
1168 name[num] = 0;
1169 cmap = Fcons (Fcons (build_string (name),
1170 make_number (RGB (red, green, blue))),
1171 cmap);
1172 }
1173 }
1174 fclose (fp);
1175
1176 UNBLOCK_INPUT;
1177 }
1178
1179 return cmap;
1180}
ee78dc32 1181
fbd6baed 1182/* The default colors for the w32 color map */
ee78dc32
GV
1183typedef struct colormap_t
1184{
1185 char *name;
1186 COLORREF colorref;
1187} colormap_t;
1188
fbd6baed 1189colormap_t w32_color_map[] =
ee78dc32 1190{
1da8a614
GV
1191 {"snow" , PALETTERGB (255,250,250)},
1192 {"ghost white" , PALETTERGB (248,248,255)},
1193 {"GhostWhite" , PALETTERGB (248,248,255)},
1194 {"white smoke" , PALETTERGB (245,245,245)},
1195 {"WhiteSmoke" , PALETTERGB (245,245,245)},
1196 {"gainsboro" , PALETTERGB (220,220,220)},
1197 {"floral white" , PALETTERGB (255,250,240)},
1198 {"FloralWhite" , PALETTERGB (255,250,240)},
1199 {"old lace" , PALETTERGB (253,245,230)},
1200 {"OldLace" , PALETTERGB (253,245,230)},
1201 {"linen" , PALETTERGB (250,240,230)},
1202 {"antique white" , PALETTERGB (250,235,215)},
1203 {"AntiqueWhite" , PALETTERGB (250,235,215)},
1204 {"papaya whip" , PALETTERGB (255,239,213)},
1205 {"PapayaWhip" , PALETTERGB (255,239,213)},
1206 {"blanched almond" , PALETTERGB (255,235,205)},
1207 {"BlanchedAlmond" , PALETTERGB (255,235,205)},
1208 {"bisque" , PALETTERGB (255,228,196)},
1209 {"peach puff" , PALETTERGB (255,218,185)},
1210 {"PeachPuff" , PALETTERGB (255,218,185)},
1211 {"navajo white" , PALETTERGB (255,222,173)},
1212 {"NavajoWhite" , PALETTERGB (255,222,173)},
1213 {"moccasin" , PALETTERGB (255,228,181)},
1214 {"cornsilk" , PALETTERGB (255,248,220)},
1215 {"ivory" , PALETTERGB (255,255,240)},
1216 {"lemon chiffon" , PALETTERGB (255,250,205)},
1217 {"LemonChiffon" , PALETTERGB (255,250,205)},
1218 {"seashell" , PALETTERGB (255,245,238)},
1219 {"honeydew" , PALETTERGB (240,255,240)},
1220 {"mint cream" , PALETTERGB (245,255,250)},
1221 {"MintCream" , PALETTERGB (245,255,250)},
1222 {"azure" , PALETTERGB (240,255,255)},
1223 {"alice blue" , PALETTERGB (240,248,255)},
1224 {"AliceBlue" , PALETTERGB (240,248,255)},
1225 {"lavender" , PALETTERGB (230,230,250)},
1226 {"lavender blush" , PALETTERGB (255,240,245)},
1227 {"LavenderBlush" , PALETTERGB (255,240,245)},
1228 {"misty rose" , PALETTERGB (255,228,225)},
1229 {"MistyRose" , PALETTERGB (255,228,225)},
1230 {"white" , PALETTERGB (255,255,255)},
1231 {"black" , PALETTERGB ( 0, 0, 0)},
1232 {"dark slate gray" , PALETTERGB ( 47, 79, 79)},
1233 {"DarkSlateGray" , PALETTERGB ( 47, 79, 79)},
1234 {"dark slate grey" , PALETTERGB ( 47, 79, 79)},
1235 {"DarkSlateGrey" , PALETTERGB ( 47, 79, 79)},
1236 {"dim gray" , PALETTERGB (105,105,105)},
1237 {"DimGray" , PALETTERGB (105,105,105)},
1238 {"dim grey" , PALETTERGB (105,105,105)},
1239 {"DimGrey" , PALETTERGB (105,105,105)},
1240 {"slate gray" , PALETTERGB (112,128,144)},
1241 {"SlateGray" , PALETTERGB (112,128,144)},
1242 {"slate grey" , PALETTERGB (112,128,144)},
1243 {"SlateGrey" , PALETTERGB (112,128,144)},
1244 {"light slate gray" , PALETTERGB (119,136,153)},
1245 {"LightSlateGray" , PALETTERGB (119,136,153)},
1246 {"light slate grey" , PALETTERGB (119,136,153)},
1247 {"LightSlateGrey" , PALETTERGB (119,136,153)},
1248 {"gray" , PALETTERGB (190,190,190)},
1249 {"grey" , PALETTERGB (190,190,190)},
1250 {"light grey" , PALETTERGB (211,211,211)},
1251 {"LightGrey" , PALETTERGB (211,211,211)},
1252 {"light gray" , PALETTERGB (211,211,211)},
1253 {"LightGray" , PALETTERGB (211,211,211)},
1254 {"midnight blue" , PALETTERGB ( 25, 25,112)},
1255 {"MidnightBlue" , PALETTERGB ( 25, 25,112)},
1256 {"navy" , PALETTERGB ( 0, 0,128)},
1257 {"navy blue" , PALETTERGB ( 0, 0,128)},
1258 {"NavyBlue" , PALETTERGB ( 0, 0,128)},
1259 {"cornflower blue" , PALETTERGB (100,149,237)},
1260 {"CornflowerBlue" , PALETTERGB (100,149,237)},
1261 {"dark slate blue" , PALETTERGB ( 72, 61,139)},
1262 {"DarkSlateBlue" , PALETTERGB ( 72, 61,139)},
1263 {"slate blue" , PALETTERGB (106, 90,205)},
1264 {"SlateBlue" , PALETTERGB (106, 90,205)},
1265 {"medium slate blue" , PALETTERGB (123,104,238)},
1266 {"MediumSlateBlue" , PALETTERGB (123,104,238)},
1267 {"light slate blue" , PALETTERGB (132,112,255)},
1268 {"LightSlateBlue" , PALETTERGB (132,112,255)},
1269 {"medium blue" , PALETTERGB ( 0, 0,205)},
1270 {"MediumBlue" , PALETTERGB ( 0, 0,205)},
1271 {"royal blue" , PALETTERGB ( 65,105,225)},
1272 {"RoyalBlue" , PALETTERGB ( 65,105,225)},
1273 {"blue" , PALETTERGB ( 0, 0,255)},
1274 {"dodger blue" , PALETTERGB ( 30,144,255)},
1275 {"DodgerBlue" , PALETTERGB ( 30,144,255)},
1276 {"deep sky blue" , PALETTERGB ( 0,191,255)},
1277 {"DeepSkyBlue" , PALETTERGB ( 0,191,255)},
1278 {"sky blue" , PALETTERGB (135,206,235)},
1279 {"SkyBlue" , PALETTERGB (135,206,235)},
1280 {"light sky blue" , PALETTERGB (135,206,250)},
1281 {"LightSkyBlue" , PALETTERGB (135,206,250)},
1282 {"steel blue" , PALETTERGB ( 70,130,180)},
1283 {"SteelBlue" , PALETTERGB ( 70,130,180)},
1284 {"light steel blue" , PALETTERGB (176,196,222)},
1285 {"LightSteelBlue" , PALETTERGB (176,196,222)},
1286 {"light blue" , PALETTERGB (173,216,230)},
1287 {"LightBlue" , PALETTERGB (173,216,230)},
1288 {"powder blue" , PALETTERGB (176,224,230)},
1289 {"PowderBlue" , PALETTERGB (176,224,230)},
1290 {"pale turquoise" , PALETTERGB (175,238,238)},
1291 {"PaleTurquoise" , PALETTERGB (175,238,238)},
1292 {"dark turquoise" , PALETTERGB ( 0,206,209)},
1293 {"DarkTurquoise" , PALETTERGB ( 0,206,209)},
1294 {"medium turquoise" , PALETTERGB ( 72,209,204)},
1295 {"MediumTurquoise" , PALETTERGB ( 72,209,204)},
1296 {"turquoise" , PALETTERGB ( 64,224,208)},
1297 {"cyan" , PALETTERGB ( 0,255,255)},
1298 {"light cyan" , PALETTERGB (224,255,255)},
1299 {"LightCyan" , PALETTERGB (224,255,255)},
1300 {"cadet blue" , PALETTERGB ( 95,158,160)},
1301 {"CadetBlue" , PALETTERGB ( 95,158,160)},
1302 {"medium aquamarine" , PALETTERGB (102,205,170)},
1303 {"MediumAquamarine" , PALETTERGB (102,205,170)},
1304 {"aquamarine" , PALETTERGB (127,255,212)},
1305 {"dark green" , PALETTERGB ( 0,100, 0)},
1306 {"DarkGreen" , PALETTERGB ( 0,100, 0)},
1307 {"dark olive green" , PALETTERGB ( 85,107, 47)},
1308 {"DarkOliveGreen" , PALETTERGB ( 85,107, 47)},
1309 {"dark sea green" , PALETTERGB (143,188,143)},
1310 {"DarkSeaGreen" , PALETTERGB (143,188,143)},
1311 {"sea green" , PALETTERGB ( 46,139, 87)},
1312 {"SeaGreen" , PALETTERGB ( 46,139, 87)},
1313 {"medium sea green" , PALETTERGB ( 60,179,113)},
1314 {"MediumSeaGreen" , PALETTERGB ( 60,179,113)},
1315 {"light sea green" , PALETTERGB ( 32,178,170)},
1316 {"LightSeaGreen" , PALETTERGB ( 32,178,170)},
1317 {"pale green" , PALETTERGB (152,251,152)},
1318 {"PaleGreen" , PALETTERGB (152,251,152)},
1319 {"spring green" , PALETTERGB ( 0,255,127)},
1320 {"SpringGreen" , PALETTERGB ( 0,255,127)},
1321 {"lawn green" , PALETTERGB (124,252, 0)},
1322 {"LawnGreen" , PALETTERGB (124,252, 0)},
1323 {"green" , PALETTERGB ( 0,255, 0)},
1324 {"chartreuse" , PALETTERGB (127,255, 0)},
1325 {"medium spring green" , PALETTERGB ( 0,250,154)},
1326 {"MediumSpringGreen" , PALETTERGB ( 0,250,154)},
1327 {"green yellow" , PALETTERGB (173,255, 47)},
1328 {"GreenYellow" , PALETTERGB (173,255, 47)},
1329 {"lime green" , PALETTERGB ( 50,205, 50)},
1330 {"LimeGreen" , PALETTERGB ( 50,205, 50)},
1331 {"yellow green" , PALETTERGB (154,205, 50)},
1332 {"YellowGreen" , PALETTERGB (154,205, 50)},
1333 {"forest green" , PALETTERGB ( 34,139, 34)},
1334 {"ForestGreen" , PALETTERGB ( 34,139, 34)},
1335 {"olive drab" , PALETTERGB (107,142, 35)},
1336 {"OliveDrab" , PALETTERGB (107,142, 35)},
1337 {"dark khaki" , PALETTERGB (189,183,107)},
1338 {"DarkKhaki" , PALETTERGB (189,183,107)},
1339 {"khaki" , PALETTERGB (240,230,140)},
1340 {"pale goldenrod" , PALETTERGB (238,232,170)},
1341 {"PaleGoldenrod" , PALETTERGB (238,232,170)},
1342 {"light goldenrod yellow" , PALETTERGB (250,250,210)},
1343 {"LightGoldenrodYellow" , PALETTERGB (250,250,210)},
1344 {"light yellow" , PALETTERGB (255,255,224)},
1345 {"LightYellow" , PALETTERGB (255,255,224)},
1346 {"yellow" , PALETTERGB (255,255, 0)},
1347 {"gold" , PALETTERGB (255,215, 0)},
1348 {"light goldenrod" , PALETTERGB (238,221,130)},
1349 {"LightGoldenrod" , PALETTERGB (238,221,130)},
1350 {"goldenrod" , PALETTERGB (218,165, 32)},
1351 {"dark goldenrod" , PALETTERGB (184,134, 11)},
1352 {"DarkGoldenrod" , PALETTERGB (184,134, 11)},
1353 {"rosy brown" , PALETTERGB (188,143,143)},
1354 {"RosyBrown" , PALETTERGB (188,143,143)},
1355 {"indian red" , PALETTERGB (205, 92, 92)},
1356 {"IndianRed" , PALETTERGB (205, 92, 92)},
1357 {"saddle brown" , PALETTERGB (139, 69, 19)},
1358 {"SaddleBrown" , PALETTERGB (139, 69, 19)},
1359 {"sienna" , PALETTERGB (160, 82, 45)},
1360 {"peru" , PALETTERGB (205,133, 63)},
1361 {"burlywood" , PALETTERGB (222,184,135)},
1362 {"beige" , PALETTERGB (245,245,220)},
1363 {"wheat" , PALETTERGB (245,222,179)},
1364 {"sandy brown" , PALETTERGB (244,164, 96)},
1365 {"SandyBrown" , PALETTERGB (244,164, 96)},
1366 {"tan" , PALETTERGB (210,180,140)},
1367 {"chocolate" , PALETTERGB (210,105, 30)},
1368 {"firebrick" , PALETTERGB (178,34, 34)},
1369 {"brown" , PALETTERGB (165,42, 42)},
1370 {"dark salmon" , PALETTERGB (233,150,122)},
1371 {"DarkSalmon" , PALETTERGB (233,150,122)},
1372 {"salmon" , PALETTERGB (250,128,114)},
1373 {"light salmon" , PALETTERGB (255,160,122)},
1374 {"LightSalmon" , PALETTERGB (255,160,122)},
1375 {"orange" , PALETTERGB (255,165, 0)},
1376 {"dark orange" , PALETTERGB (255,140, 0)},
1377 {"DarkOrange" , PALETTERGB (255,140, 0)},
1378 {"coral" , PALETTERGB (255,127, 80)},
1379 {"light coral" , PALETTERGB (240,128,128)},
1380 {"LightCoral" , PALETTERGB (240,128,128)},
1381 {"tomato" , PALETTERGB (255, 99, 71)},
1382 {"orange red" , PALETTERGB (255, 69, 0)},
1383 {"OrangeRed" , PALETTERGB (255, 69, 0)},
1384 {"red" , PALETTERGB (255, 0, 0)},
1385 {"hot pink" , PALETTERGB (255,105,180)},
1386 {"HotPink" , PALETTERGB (255,105,180)},
1387 {"deep pink" , PALETTERGB (255, 20,147)},
1388 {"DeepPink" , PALETTERGB (255, 20,147)},
1389 {"pink" , PALETTERGB (255,192,203)},
1390 {"light pink" , PALETTERGB (255,182,193)},
1391 {"LightPink" , PALETTERGB (255,182,193)},
1392 {"pale violet red" , PALETTERGB (219,112,147)},
1393 {"PaleVioletRed" , PALETTERGB (219,112,147)},
1394 {"maroon" , PALETTERGB (176, 48, 96)},
1395 {"medium violet red" , PALETTERGB (199, 21,133)},
1396 {"MediumVioletRed" , PALETTERGB (199, 21,133)},
1397 {"violet red" , PALETTERGB (208, 32,144)},
1398 {"VioletRed" , PALETTERGB (208, 32,144)},
1399 {"magenta" , PALETTERGB (255, 0,255)},
1400 {"violet" , PALETTERGB (238,130,238)},
1401 {"plum" , PALETTERGB (221,160,221)},
1402 {"orchid" , PALETTERGB (218,112,214)},
1403 {"medium orchid" , PALETTERGB (186, 85,211)},
1404 {"MediumOrchid" , PALETTERGB (186, 85,211)},
1405 {"dark orchid" , PALETTERGB (153, 50,204)},
1406 {"DarkOrchid" , PALETTERGB (153, 50,204)},
1407 {"dark violet" , PALETTERGB (148, 0,211)},
1408 {"DarkViolet" , PALETTERGB (148, 0,211)},
1409 {"blue violet" , PALETTERGB (138, 43,226)},
1410 {"BlueViolet" , PALETTERGB (138, 43,226)},
1411 {"purple" , PALETTERGB (160, 32,240)},
1412 {"medium purple" , PALETTERGB (147,112,219)},
1413 {"MediumPurple" , PALETTERGB (147,112,219)},
1414 {"thistle" , PALETTERGB (216,191,216)},
1415 {"gray0" , PALETTERGB ( 0, 0, 0)},
1416 {"grey0" , PALETTERGB ( 0, 0, 0)},
1417 {"dark grey" , PALETTERGB (169,169,169)},
1418 {"DarkGrey" , PALETTERGB (169,169,169)},
1419 {"dark gray" , PALETTERGB (169,169,169)},
1420 {"DarkGray" , PALETTERGB (169,169,169)},
1421 {"dark blue" , PALETTERGB ( 0, 0,139)},
1422 {"DarkBlue" , PALETTERGB ( 0, 0,139)},
1423 {"dark cyan" , PALETTERGB ( 0,139,139)},
1424 {"DarkCyan" , PALETTERGB ( 0,139,139)},
1425 {"dark magenta" , PALETTERGB (139, 0,139)},
1426 {"DarkMagenta" , PALETTERGB (139, 0,139)},
1427 {"dark red" , PALETTERGB (139, 0, 0)},
1428 {"DarkRed" , PALETTERGB (139, 0, 0)},
1429 {"light green" , PALETTERGB (144,238,144)},
1430 {"LightGreen" , PALETTERGB (144,238,144)},
ee78dc32
GV
1431};
1432
fbd6baed 1433DEFUN ("w32-default-color-map", Fw32_default_color_map, Sw32_default_color_map,
ee78dc32
GV
1434 0, 0, 0, "Return the default color map.")
1435 ()
1436{
1437 int i;
fbd6baed 1438 colormap_t *pc = w32_color_map;
ee78dc32
GV
1439 Lisp_Object cmap;
1440
1441 BLOCK_INPUT;
1442
1443 cmap = Qnil;
1444
fbd6baed 1445 for (i = 0; i < sizeof (w32_color_map) / sizeof (w32_color_map[0]);
ee78dc32
GV
1446 pc++, i++)
1447 cmap = Fcons (Fcons (build_string (pc->name),
1448 make_number (pc->colorref)),
1449 cmap);
1450
1451 UNBLOCK_INPUT;
1452
1453 return (cmap);
1454}
ee78dc32
GV
1455
1456Lisp_Object
fbd6baed 1457w32_to_x_color (rgb)
ee78dc32
GV
1458 Lisp_Object rgb;
1459{
1460 Lisp_Object color;
1461
1462 CHECK_NUMBER (rgb, 0);
1463
1464 BLOCK_INPUT;
1465
fbd6baed 1466 color = Frassq (rgb, Vw32_color_map);
ee78dc32
GV
1467
1468 UNBLOCK_INPUT;
1469
1470 if (!NILP (color))
1471 return (Fcar (color));
1472 else
1473 return Qnil;
1474}
1475
5d7fed93
GV
1476COLORREF
1477w32_color_map_lookup (colorname)
1478 char *colorname;
1479{
1480 Lisp_Object tail, ret = Qnil;
1481
1482 BLOCK_INPUT;
1483
1484 for (tail = Vw32_color_map; !NILP (tail); tail = Fcdr (tail))
1485 {
1486 register Lisp_Object elt, tem;
1487
1488 elt = Fcar (tail);
1489 if (!CONSP (elt)) continue;
1490
1491 tem = Fcar (elt);
1492
1493 if (lstrcmpi (XSTRING (tem)->data, colorname) == 0)
1494 {
1495 ret = XUINT (Fcdr (elt));
1496 break;
1497 }
1498
1499 QUIT;
1500 }
1501
1502
1503 UNBLOCK_INPUT;
1504
1505 return ret;
1506}
1507
ee78dc32 1508COLORREF
fbd6baed 1509x_to_w32_color (colorname)
ee78dc32
GV
1510 char * colorname;
1511{
1512 register Lisp_Object tail, ret = Qnil;
1513
1514 BLOCK_INPUT;
1edf84e7
GV
1515
1516 if (colorname[0] == '#')
1517 {
1518 /* Could be an old-style RGB Device specification. */
1519 char *color;
1520 int size;
1521 color = colorname + 1;
1522
1523 size = strlen(color);
1524 if (size == 3 || size == 6 || size == 9 || size == 12)
1525 {
1526 UINT colorval;
1527 int i, pos;
1528 pos = 0;
1529 size /= 3;
1530 colorval = 0;
1531
1532 for (i = 0; i < 3; i++)
1533 {
1534 char *end;
1535 char t;
1536 unsigned long value;
1537
1538 /* The check for 'x' in the following conditional takes into
1539 account the fact that strtol allows a "0x" in front of
1540 our numbers, and we don't. */
1541 if (!isxdigit(color[0]) || color[1] == 'x')
1542 break;
1543 t = color[size];
1544 color[size] = '\0';
1545 value = strtoul(color, &end, 16);
1546 color[size] = t;
1547 if (errno == ERANGE || end - color != size)
1548 break;
1549 switch (size)
1550 {
1551 case 1:
1552 value = value * 0x10;
1553 break;
1554 case 2:
1555 break;
1556 case 3:
1557 value /= 0x10;
1558 break;
1559 case 4:
1560 value /= 0x100;
1561 break;
1562 }
1563 colorval |= (value << pos);
1564 pos += 0x8;
1565 if (i == 2)
1566 {
1567 UNBLOCK_INPUT;
1568 return (colorval);
1569 }
1570 color = end;
1571 }
1572 }
1573 }
1574 else if (strnicmp(colorname, "rgb:", 4) == 0)
1575 {
1576 char *color;
1577 UINT colorval;
1578 int i, pos;
1579 pos = 0;
1580
1581 colorval = 0;
1582 color = colorname + 4;
1583 for (i = 0; i < 3; i++)
1584 {
1585 char *end;
1586 unsigned long value;
1587
1588 /* The check for 'x' in the following conditional takes into
1589 account the fact that strtol allows a "0x" in front of
1590 our numbers, and we don't. */
1591 if (!isxdigit(color[0]) || color[1] == 'x')
1592 break;
1593 value = strtoul(color, &end, 16);
1594 if (errno == ERANGE)
1595 break;
1596 switch (end - color)
1597 {
1598 case 1:
1599 value = value * 0x10 + value;
1600 break;
1601 case 2:
1602 break;
1603 case 3:
1604 value /= 0x10;
1605 break;
1606 case 4:
1607 value /= 0x100;
1608 break;
1609 default:
1610 value = ULONG_MAX;
1611 }
1612 if (value == ULONG_MAX)
1613 break;
1614 colorval |= (value << pos);
1615 pos += 0x8;
1616 if (i == 2)
1617 {
1618 if (*end != '\0')
1619 break;
1620 UNBLOCK_INPUT;
1621 return (colorval);
1622 }
1623 if (*end != '/')
1624 break;
1625 color = end + 1;
1626 }
1627 }
1628 else if (strnicmp(colorname, "rgbi:", 5) == 0)
1629 {
1630 /* This is an RGB Intensity specification. */
1631 char *color;
1632 UINT colorval;
1633 int i, pos;
1634 pos = 0;
1635
1636 colorval = 0;
1637 color = colorname + 5;
1638 for (i = 0; i < 3; i++)
1639 {
1640 char *end;
1641 double value;
1642 UINT val;
1643
1644 value = strtod(color, &end);
1645 if (errno == ERANGE)
1646 break;
1647 if (value < 0.0 || value > 1.0)
1648 break;
1649 val = (UINT)(0x100 * value);
1650 /* We used 0x100 instead of 0xFF to give an continuous
1651 range between 0.0 and 1.0 inclusive. The next statement
1652 fixes the 1.0 case. */
1653 if (val == 0x100)
1654 val = 0xFF;
1655 colorval |= (val << pos);
1656 pos += 0x8;
1657 if (i == 2)
1658 {
1659 if (*end != '\0')
1660 break;
1661 UNBLOCK_INPUT;
1662 return (colorval);
1663 }
1664 if (*end != '/')
1665 break;
1666 color = end + 1;
1667 }
1668 }
1669 /* I am not going to attempt to handle any of the CIE color schemes
1670 or TekHVC, since I don't know the algorithms for conversion to
1671 RGB. */
f695b4b1
GV
1672
1673 /* If we fail to lookup the color name in w32_color_map, then check the
1674 colorname to see if it can be crudely approximated: If the X color
1675 ends in a number (e.g., "darkseagreen2"), strip the number and
1676 return the result of looking up the base color name. */
1677 ret = w32_color_map_lookup (colorname);
1678 if (NILP (ret))
ee78dc32 1679 {
f695b4b1 1680 int len = strlen (colorname);
ee78dc32 1681
f695b4b1
GV
1682 if (isdigit (colorname[len - 1]))
1683 {
1684 char *ptr, *approx = alloca (len);
ee78dc32 1685
f695b4b1
GV
1686 strcpy (approx, colorname);
1687 ptr = &approx[len - 1];
1688 while (ptr > approx && isdigit (*ptr))
1689 *ptr-- = '\0';
ee78dc32 1690
f695b4b1 1691 ret = w32_color_map_lookup (approx);
ee78dc32 1692 }
ee78dc32
GV
1693 }
1694
1695 UNBLOCK_INPUT;
ee78dc32
GV
1696 return ret;
1697}
1698
5ac45f98
GV
1699
1700void
fbd6baed 1701w32_regenerate_palette (FRAME_PTR f)
5ac45f98 1702{
fbd6baed 1703 struct w32_palette_entry * list;
5ac45f98
GV
1704 LOGPALETTE * log_palette;
1705 HPALETTE new_palette;
1706 int i;
1707
1708 /* don't bother trying to create palette if not supported */
fbd6baed 1709 if (! FRAME_W32_DISPLAY_INFO (f)->has_palette)
5ac45f98
GV
1710 return;
1711
1712 log_palette = (LOGPALETTE *)
1713 alloca (sizeof (LOGPALETTE) +
fbd6baed 1714 FRAME_W32_DISPLAY_INFO (f)->num_colors * sizeof (PALETTEENTRY));
5ac45f98 1715 log_palette->palVersion = 0x300;
fbd6baed 1716 log_palette->palNumEntries = FRAME_W32_DISPLAY_INFO (f)->num_colors;
5ac45f98 1717
fbd6baed 1718 list = FRAME_W32_DISPLAY_INFO (f)->color_list;
5ac45f98 1719 for (i = 0;
fbd6baed 1720 i < FRAME_W32_DISPLAY_INFO (f)->num_colors;
5ac45f98
GV
1721 i++, list = list->next)
1722 log_palette->palPalEntry[i] = list->entry;
1723
1724 new_palette = CreatePalette (log_palette);
1725
1726 enter_crit ();
1727
fbd6baed
GV
1728 if (FRAME_W32_DISPLAY_INFO (f)->palette)
1729 DeleteObject (FRAME_W32_DISPLAY_INFO (f)->palette);
1730 FRAME_W32_DISPLAY_INFO (f)->palette = new_palette;
5ac45f98
GV
1731
1732 /* Realize display palette and garbage all frames. */
1733 release_frame_dc (f, get_frame_dc (f));
1734
1735 leave_crit ();
1736}
1737
fbd6baed
GV
1738#define W32_COLOR(pe) RGB (pe.peRed, pe.peGreen, pe.peBlue)
1739#define SET_W32_COLOR(pe, color) \
5ac45f98
GV
1740 do \
1741 { \
1742 pe.peRed = GetRValue (color); \
1743 pe.peGreen = GetGValue (color); \
1744 pe.peBlue = GetBValue (color); \
1745 pe.peFlags = 0; \
1746 } while (0)
1747
1748#if 0
1749/* Keep these around in case we ever want to track color usage. */
1750void
fbd6baed 1751w32_map_color (FRAME_PTR f, COLORREF color)
5ac45f98 1752{
fbd6baed 1753 struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
5ac45f98 1754
fbd6baed 1755 if (NILP (Vw32_enable_palette))
5ac45f98
GV
1756 return;
1757
1758 /* check if color is already mapped */
1759 while (list)
1760 {
fbd6baed 1761 if (W32_COLOR (list->entry) == color)
5ac45f98
GV
1762 {
1763 ++list->refcount;
1764 return;
1765 }
1766 list = list->next;
1767 }
1768
1769 /* not already mapped, so add to list and recreate Windows palette */
fbd6baed
GV
1770 list = (struct w32_palette_entry *)
1771 xmalloc (sizeof (struct w32_palette_entry));
1772 SET_W32_COLOR (list->entry, color);
5ac45f98 1773 list->refcount = 1;
fbd6baed
GV
1774 list->next = FRAME_W32_DISPLAY_INFO (f)->color_list;
1775 FRAME_W32_DISPLAY_INFO (f)->color_list = list;
1776 FRAME_W32_DISPLAY_INFO (f)->num_colors++;
5ac45f98
GV
1777
1778 /* set flag that palette must be regenerated */
fbd6baed 1779 FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
5ac45f98
GV
1780}
1781
1782void
fbd6baed 1783w32_unmap_color (FRAME_PTR f, COLORREF color)
5ac45f98 1784{
fbd6baed
GV
1785 struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
1786 struct w32_palette_entry **prev = &FRAME_W32_DISPLAY_INFO (f)->color_list;
5ac45f98 1787
fbd6baed 1788 if (NILP (Vw32_enable_palette))
5ac45f98
GV
1789 return;
1790
1791 /* check if color is already mapped */
1792 while (list)
1793 {
fbd6baed 1794 if (W32_COLOR (list->entry) == color)
5ac45f98
GV
1795 {
1796 if (--list->refcount == 0)
1797 {
1798 *prev = list->next;
1799 xfree (list);
fbd6baed 1800 FRAME_W32_DISPLAY_INFO (f)->num_colors--;
5ac45f98
GV
1801 break;
1802 }
1803 else
1804 return;
1805 }
1806 prev = &list->next;
1807 list = list->next;
1808 }
1809
1810 /* set flag that palette must be regenerated */
fbd6baed 1811 FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
5ac45f98
GV
1812}
1813#endif
1814
6fc2811b
JR
1815
1816/* Gamma-correct COLOR on frame F. */
1817
1818void
1819gamma_correct (f, color)
1820 struct frame *f;
1821 COLORREF *color;
1822{
1823 if (f->gamma)
1824 {
1825 *color = PALETTERGB (
1826 pow (GetRValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
1827 pow (GetGValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
1828 pow (GetBValue (*color) / 255.0, f->gamma) * 255.0 + 0.5);
1829 }
1830}
1831
1832
ee78dc32
GV
1833/* Decide if color named COLOR is valid for the display associated with
1834 the selected frame; if so, return the rgb values in COLOR_DEF.
1835 If ALLOC is nonzero, allocate a new colormap cell. */
1836
1837int
6fc2811b 1838w32_defined_color (f, color, color_def, alloc)
ee78dc32
GV
1839 FRAME_PTR f;
1840 char *color;
6fc2811b 1841 XColor *color_def;
ee78dc32
GV
1842 int alloc;
1843{
1844 register Lisp_Object tem;
6fc2811b 1845 COLORREF w32_color_ref;
3c190163 1846
fbd6baed 1847 tem = x_to_w32_color (color);
3c190163 1848
ee78dc32
GV
1849 if (!NILP (tem))
1850 {
d88c567c
JR
1851 if (f)
1852 {
1853 /* Apply gamma correction. */
1854 w32_color_ref = XUINT (tem);
1855 gamma_correct (f, &w32_color_ref);
1856 XSETINT (tem, w32_color_ref);
1857 }
9badad41
JR
1858
1859 /* Map this color to the palette if it is enabled. */
fbd6baed 1860 if (!NILP (Vw32_enable_palette))
5ac45f98 1861 {
fbd6baed 1862 struct w32_palette_entry * entry =
d88c567c 1863 one_w32_display_info.color_list;
fbd6baed 1864 struct w32_palette_entry ** prev =
d88c567c 1865 &one_w32_display_info.color_list;
5ac45f98
GV
1866
1867 /* check if color is already mapped */
1868 while (entry)
1869 {
fbd6baed 1870 if (W32_COLOR (entry->entry) == XUINT (tem))
5ac45f98
GV
1871 break;
1872 prev = &entry->next;
1873 entry = entry->next;
1874 }
1875
1876 if (entry == NULL && alloc)
1877 {
1878 /* not already mapped, so add to list */
fbd6baed
GV
1879 entry = (struct w32_palette_entry *)
1880 xmalloc (sizeof (struct w32_palette_entry));
1881 SET_W32_COLOR (entry->entry, XUINT (tem));
5ac45f98
GV
1882 entry->next = NULL;
1883 *prev = entry;
d88c567c 1884 one_w32_display_info.num_colors++;
5ac45f98
GV
1885
1886 /* set flag that palette must be regenerated */
d88c567c 1887 one_w32_display_info.regen_palette = TRUE;
5ac45f98
GV
1888 }
1889 }
1890 /* Ensure COLORREF value is snapped to nearest color in (default)
1891 palette by simulating the PALETTERGB macro. This works whether
1892 or not the display device has a palette. */
6fc2811b
JR
1893 w32_color_ref = XUINT (tem) | 0x2000000;
1894
6fc2811b
JR
1895 color_def->pixel = w32_color_ref;
1896 color_def->red = GetRValue (w32_color_ref);
1897 color_def->green = GetGValue (w32_color_ref);
1898 color_def->blue = GetBValue (w32_color_ref);
1899
ee78dc32 1900 return 1;
5ac45f98 1901 }
7fb46567 1902 else
3c190163
GV
1903 {
1904 return 0;
1905 }
ee78dc32
GV
1906}
1907
1908/* Given a string ARG naming a color, compute a pixel value from it
1909 suitable for screen F.
1910 If F is not a color screen, return DEF (default) regardless of what
1911 ARG says. */
1912
1913int
1914x_decode_color (f, arg, def)
1915 FRAME_PTR f;
1916 Lisp_Object arg;
1917 int def;
1918{
6fc2811b 1919 XColor cdef;
ee78dc32
GV
1920
1921 CHECK_STRING (arg, 0);
1922
1923 if (strcmp (XSTRING (arg)->data, "black") == 0)
1924 return BLACK_PIX_DEFAULT (f);
1925 else if (strcmp (XSTRING (arg)->data, "white") == 0)
1926 return WHITE_PIX_DEFAULT (f);
1927
fbd6baed 1928 if ((FRAME_W32_DISPLAY_INFO (f)->n_planes * FRAME_W32_DISPLAY_INFO (f)->n_cbits) == 1)
ee78dc32
GV
1929 return def;
1930
6fc2811b 1931 /* w32_defined_color is responsible for coping with failures
ee78dc32 1932 by looking for a near-miss. */
6fc2811b
JR
1933 if (w32_defined_color (f, XSTRING (arg)->data, &cdef, 1))
1934 return cdef.pixel;
ee78dc32
GV
1935
1936 /* defined_color failed; return an ultimate default. */
1937 return def;
1938}
1939\f
dfff8a69
JR
1940/* Change the `line-spacing' frame parameter of frame F. OLD_VALUE is
1941 the previous value of that parameter, NEW_VALUE is the new value. */
1942
1943static void
1944x_set_line_spacing (f, new_value, old_value)
1945 struct frame *f;
1946 Lisp_Object new_value, old_value;
1947{
1948 if (NILP (new_value))
1949 f->extra_line_spacing = 0;
1950 else if (NATNUMP (new_value))
1951 f->extra_line_spacing = XFASTINT (new_value);
1952 else
1a948b17 1953 Fsignal (Qerror, Fcons (build_string ("Invalid line-spacing"),
dfff8a69
JR
1954 Fcons (new_value, Qnil)));
1955 if (FRAME_VISIBLE_P (f))
1956 redraw_frame (f);
1957}
1958
1959
6fc2811b
JR
1960/* Change the `screen-gamma' frame parameter of frame F. OLD_VALUE is
1961 the previous value of that parameter, NEW_VALUE is the new value. */
1962
1963static void
1964x_set_screen_gamma (f, new_value, old_value)
1965 struct frame *f;
1966 Lisp_Object new_value, old_value;
1967{
1968 if (NILP (new_value))
1969 f->gamma = 0;
1970 else if (NUMBERP (new_value) && XFLOATINT (new_value) > 0)
1971 /* The value 0.4545 is the normal viewing gamma. */
1972 f->gamma = 1.0 / (0.4545 * XFLOATINT (new_value));
1973 else
1a948b17 1974 Fsignal (Qerror, Fcons (build_string ("Invalid screen-gamma"),
6fc2811b
JR
1975 Fcons (new_value, Qnil)));
1976
1977 clear_face_cache (0);
1978}
1979
1980
ee78dc32
GV
1981/* Functions called only from `x_set_frame_param'
1982 to set individual parameters.
1983
fbd6baed 1984 If FRAME_W32_WINDOW (f) is 0,
ee78dc32
GV
1985 the frame is being created and its window does not exist yet.
1986 In that case, just record the parameter's new value
1987 in the standard place; do not attempt to change the window. */
1988
1989void
1990x_set_foreground_color (f, arg, oldval)
1991 struct frame *f;
1992 Lisp_Object arg, oldval;
1993{
6fc2811b 1994 FRAME_FOREGROUND_PIXEL (f)
ee78dc32 1995 = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
5ac45f98 1996
fbd6baed 1997 if (FRAME_W32_WINDOW (f) != 0)
ee78dc32 1998 {
6fc2811b 1999 update_face_from_frame_parameter (f, Qforeground_color, arg);
ee78dc32
GV
2000 if (FRAME_VISIBLE_P (f))
2001 redraw_frame (f);
2002 }
2003}
2004
2005void
2006x_set_background_color (f, arg, oldval)
2007 struct frame *f;
2008 Lisp_Object arg, oldval;
2009{
6fc2811b 2010 FRAME_BACKGROUND_PIXEL (f)
ee78dc32
GV
2011 = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
2012
fbd6baed 2013 if (FRAME_W32_WINDOW (f) != 0)
ee78dc32 2014 {
6fc2811b
JR
2015 SetWindowLong (FRAME_W32_WINDOW (f), WND_BACKGROUND_INDEX,
2016 FRAME_BACKGROUND_PIXEL (f));
ee78dc32 2017
6fc2811b 2018 update_face_from_frame_parameter (f, Qbackground_color, arg);
ee78dc32
GV
2019
2020 if (FRAME_VISIBLE_P (f))
2021 redraw_frame (f);
2022 }
2023}
2024
2025void
2026x_set_mouse_color (f, arg, oldval)
2027 struct frame *f;
2028 Lisp_Object arg, oldval;
2029{
ee78dc32 2030 Cursor cursor, nontext_cursor, mode_cursor, cross_cursor;
dfc465d3 2031 int count;
ee78dc32
GV
2032 int mask_color;
2033
2034 if (!EQ (Qnil, arg))
fbd6baed 2035 f->output_data.w32->mouse_pixel
ee78dc32 2036 = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
6fc2811b
JR
2037 mask_color = FRAME_BACKGROUND_PIXEL (f);
2038
2039 /* Don't let pointers be invisible. */
fbd6baed 2040 if (mask_color == f->output_data.w32->mouse_pixel
6fc2811b
JR
2041 && mask_color == FRAME_BACKGROUND_PIXEL (f))
2042 f->output_data.w32->mouse_pixel = FRAME_FOREGROUND_PIXEL (f);
ee78dc32 2043
767b1ff0 2044#if 0 /* TODO : cursor changes */
ee78dc32
GV
2045 BLOCK_INPUT;
2046
2047 /* It's not okay to crash if the user selects a screwy cursor. */
fadca6c6 2048 count = x_catch_errors (FRAME_W32_DISPLAY (f));
ee78dc32
GV
2049
2050 if (!EQ (Qnil, Vx_pointer_shape))
2051 {
2052 CHECK_NUMBER (Vx_pointer_shape, 0);
fbd6baed 2053 cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XINT (Vx_pointer_shape));
ee78dc32
GV
2054 }
2055 else
fbd6baed
GV
2056 cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
2057 x_check_errors (FRAME_W32_DISPLAY (f), "bad text pointer cursor: %s");
ee78dc32
GV
2058
2059 if (!EQ (Qnil, Vx_nontext_pointer_shape))
2060 {
2061 CHECK_NUMBER (Vx_nontext_pointer_shape, 0);
fbd6baed 2062 nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
ee78dc32
GV
2063 XINT (Vx_nontext_pointer_shape));
2064 }
2065 else
fbd6baed
GV
2066 nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_left_ptr);
2067 x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
ee78dc32 2068
6fc2811b
JR
2069 if (!EQ (Qnil, Vx_busy_pointer_shape))
2070 {
2071 CHECK_NUMBER (Vx_busy_pointer_shape, 0);
2072 busy_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
2073 XINT (Vx_busy_pointer_shape));
2074 }
2075 else
2076 busy_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_watch);
2077 x_check_errors (FRAME_W32_DISPLAY (f), "bad busy pointer cursor: %s");
2078
2079 x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
ee78dc32
GV
2080 if (!EQ (Qnil, Vx_mode_pointer_shape))
2081 {
2082 CHECK_NUMBER (Vx_mode_pointer_shape, 0);
fbd6baed 2083 mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
ee78dc32
GV
2084 XINT (Vx_mode_pointer_shape));
2085 }
2086 else
fbd6baed
GV
2087 mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
2088 x_check_errors (FRAME_W32_DISPLAY (f), "bad modeline pointer cursor: %s");
ee78dc32
GV
2089
2090 if (!EQ (Qnil, Vx_sensitive_text_pointer_shape))
2091 {
2092 CHECK_NUMBER (Vx_sensitive_text_pointer_shape, 0);
2093 cross_cursor
fbd6baed 2094 = XCreateFontCursor (FRAME_W32_DISPLAY (f),
ee78dc32
GV
2095 XINT (Vx_sensitive_text_pointer_shape));
2096 }
2097 else
fbd6baed 2098 cross_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_crosshair);
ee78dc32 2099
4694d762
JR
2100 if (!NILP (Vx_window_horizontal_drag_shape))
2101 {
2102 CHECK_NUMBER (Vx_window_horizontal_drag_shape, 0);
2103 horizontal_drag_cursor
2104 = XCreateFontCursor (FRAME_X_DISPLAY (f),
2105 XINT (Vx_window_horizontal_drag_shape));
2106 }
2107 else
2108 horizontal_drag_cursor
2109 = XCreateFontCursor (FRAME_X_DISPLAY (f), XC_sb_h_double_arrow);
2110
ee78dc32 2111 /* Check and report errors with the above calls. */
fbd6baed 2112 x_check_errors (FRAME_W32_DISPLAY (f), "can't set cursor shape: %s");
fadca6c6 2113 x_uncatch_errors (FRAME_W32_DISPLAY (f), count);
ee78dc32
GV
2114
2115 {
2116 XColor fore_color, back_color;
2117
fbd6baed 2118 fore_color.pixel = f->output_data.w32->mouse_pixel;
ee78dc32 2119 back_color.pixel = mask_color;
fbd6baed
GV
2120 XQueryColor (FRAME_W32_DISPLAY (f),
2121 DefaultColormap (FRAME_W32_DISPLAY (f),
2122 DefaultScreen (FRAME_W32_DISPLAY (f))),
ee78dc32 2123 &fore_color);
fbd6baed
GV
2124 XQueryColor (FRAME_W32_DISPLAY (f),
2125 DefaultColormap (FRAME_W32_DISPLAY (f),
2126 DefaultScreen (FRAME_W32_DISPLAY (f))),
ee78dc32 2127 &back_color);
fbd6baed 2128 XRecolorCursor (FRAME_W32_DISPLAY (f), cursor,
ee78dc32 2129 &fore_color, &back_color);
fbd6baed 2130 XRecolorCursor (FRAME_W32_DISPLAY (f), nontext_cursor,
ee78dc32 2131 &fore_color, &back_color);
fbd6baed 2132 XRecolorCursor (FRAME_W32_DISPLAY (f), mode_cursor,
ee78dc32 2133 &fore_color, &back_color);
fbd6baed 2134 XRecolorCursor (FRAME_W32_DISPLAY (f), cross_cursor,
ee78dc32 2135 &fore_color, &back_color);
6fc2811b
JR
2136 XRecolorCursor (FRAME_W32_DISPLAY (f), busy_cursor,
2137 &fore_color, &back_color);
ee78dc32
GV
2138 }
2139
fbd6baed 2140 if (FRAME_W32_WINDOW (f) != 0)
6fc2811b 2141 XDefineCursor (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), cursor);
ee78dc32 2142
fbd6baed
GV
2143 if (cursor != f->output_data.w32->text_cursor && f->output_data.w32->text_cursor != 0)
2144 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->text_cursor);
2145 f->output_data.w32->text_cursor = cursor;
2146
2147 if (nontext_cursor != f->output_data.w32->nontext_cursor
2148 && f->output_data.w32->nontext_cursor != 0)
2149 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->nontext_cursor);
2150 f->output_data.w32->nontext_cursor = nontext_cursor;
2151
6fc2811b
JR
2152 if (busy_cursor != f->output_data.w32->busy_cursor
2153 && f->output_data.w32->busy_cursor != 0)
2154 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->busy_cursor);
2155 f->output_data.w32->busy_cursor = busy_cursor;
2156
fbd6baed
GV
2157 if (mode_cursor != f->output_data.w32->modeline_cursor
2158 && f->output_data.w32->modeline_cursor != 0)
2159 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->modeline_cursor);
2160 f->output_data.w32->modeline_cursor = mode_cursor;
6fc2811b 2161
fbd6baed
GV
2162 if (cross_cursor != f->output_data.w32->cross_cursor
2163 && f->output_data.w32->cross_cursor != 0)
2164 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->cross_cursor);
2165 f->output_data.w32->cross_cursor = cross_cursor;
2166
2167 XFlush (FRAME_W32_DISPLAY (f));
ee78dc32 2168 UNBLOCK_INPUT;
6fc2811b
JR
2169
2170 update_face_from_frame_parameter (f, Qmouse_color, arg);
767b1ff0 2171#endif /* TODO */
ee78dc32
GV
2172}
2173
70a0239a
JR
2174/* Defined in w32term.c. */
2175void x_update_cursor (struct frame *f, int on_p);
2176
ee78dc32
GV
2177void
2178x_set_cursor_color (f, arg, oldval)
2179 struct frame *f;
2180 Lisp_Object arg, oldval;
2181{
70a0239a 2182 unsigned long fore_pixel, pixel;
ee78dc32 2183
dfff8a69 2184 if (!NILP (Vx_cursor_fore_pixel))
ee78dc32 2185 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
70a0239a 2186 WHITE_PIX_DEFAULT (f));
ee78dc32 2187 else
6fc2811b 2188 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
70a0239a 2189
6759f872 2190 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
ee78dc32
GV
2191
2192 /* Make sure that the cursor color differs from the background color. */
70a0239a 2193 if (pixel == FRAME_BACKGROUND_PIXEL (f))
ee78dc32 2194 {
70a0239a
JR
2195 pixel = f->output_data.w32->mouse_pixel;
2196 if (pixel == fore_pixel)
6fc2811b 2197 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
ee78dc32 2198 }
70a0239a 2199
6fc2811b 2200 FRAME_FOREGROUND_PIXEL (f) = fore_pixel;
70a0239a 2201 f->output_data.w32->cursor_pixel = pixel;
ee78dc32 2202
fbd6baed 2203 if (FRAME_W32_WINDOW (f) != 0)
ee78dc32
GV
2204 {
2205 if (FRAME_VISIBLE_P (f))
2206 {
70a0239a
JR
2207 x_update_cursor (f, 0);
2208 x_update_cursor (f, 1);
ee78dc32
GV
2209 }
2210 }
6fc2811b
JR
2211
2212 update_face_from_frame_parameter (f, Qcursor_color, arg);
ee78dc32
GV
2213}
2214
33d52f9c
GV
2215/* Set the border-color of frame F to pixel value PIX.
2216 Note that this does not fully take effect if done before
2217 F has an window. */
2218void
2219x_set_border_pixel (f, pix)
2220 struct frame *f;
2221 int pix;
2222{
2223 f->output_data.w32->border_pixel = pix;
2224
2225 if (FRAME_W32_WINDOW (f) != 0 && f->output_data.w32->border_width > 0)
2226 {
2227 if (FRAME_VISIBLE_P (f))
2228 redraw_frame (f);
2229 }
2230}
2231
ee78dc32
GV
2232/* Set the border-color of frame F to value described by ARG.
2233 ARG can be a string naming a color.
2234 The border-color is used for the border that is drawn by the server.
2235 Note that this does not fully take effect if done before
2236 F has a window; it must be redone when the window is created. */
2237
2238void
2239x_set_border_color (f, arg, oldval)
2240 struct frame *f;
2241 Lisp_Object arg, oldval;
2242{
ee78dc32
GV
2243 int pix;
2244
2245 CHECK_STRING (arg, 0);
ee78dc32 2246 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
ee78dc32 2247 x_set_border_pixel (f, pix);
6fc2811b 2248 update_face_from_frame_parameter (f, Qborder_color, arg);
ee78dc32
GV
2249}
2250
dfff8a69
JR
2251/* Value is the internal representation of the specified cursor type
2252 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
2253 of the bar cursor. */
2254
2255enum text_cursor_kinds
2256x_specified_cursor_type (arg, width)
2257 Lisp_Object arg;
2258 int *width;
ee78dc32 2259{
dfff8a69
JR
2260 enum text_cursor_kinds type;
2261
ee78dc32
GV
2262 if (EQ (arg, Qbar))
2263 {
dfff8a69
JR
2264 type = BAR_CURSOR;
2265 *width = 2;
ee78dc32 2266 }
dfff8a69
JR
2267 else if (CONSP (arg)
2268 && EQ (XCAR (arg), Qbar)
2269 && INTEGERP (XCDR (arg))
2270 && XINT (XCDR (arg)) >= 0)
ee78dc32 2271 {
dfff8a69
JR
2272 type = BAR_CURSOR;
2273 *width = XINT (XCDR (arg));
ee78dc32 2274 }
dfff8a69
JR
2275 else if (NILP (arg))
2276 type = NO_CURSOR;
ee78dc32
GV
2277 else
2278 /* Treat anything unknown as "box cursor".
2279 It was bad to signal an error; people have trouble fixing
2280 .Xdefaults with Emacs, when it has something bad in it. */
dfff8a69
JR
2281 type = FILLED_BOX_CURSOR;
2282
2283 return type;
2284}
2285
2286void
2287x_set_cursor_type (f, arg, oldval)
2288 FRAME_PTR f;
2289 Lisp_Object arg, oldval;
2290{
2291 int width;
2292
2293 FRAME_DESIRED_CURSOR (f) = x_specified_cursor_type (arg, &width);
2294 f->output_data.w32->cursor_width = width;
ee78dc32
GV
2295
2296 /* Make sure the cursor gets redrawn. This is overkill, but how
2297 often do people change cursor types? */
2298 update_mode_lines++;
2299}
dfff8a69 2300\f
ee78dc32
GV
2301void
2302x_set_icon_type (f, arg, oldval)
2303 struct frame *f;
2304 Lisp_Object arg, oldval;
2305{
ee78dc32
GV
2306 int result;
2307
eb7576ce
GV
2308 if (NILP (arg) && NILP (oldval))
2309 return;
2310
2311 if (STRINGP (arg) && STRINGP (oldval)
2312 && EQ (Fstring_equal (oldval, arg), Qt))
2313 return;
2314
2315 if (SYMBOLP (arg) && SYMBOLP (oldval) && EQ (arg, oldval))
ee78dc32
GV
2316 return;
2317
2318 BLOCK_INPUT;
ee78dc32 2319
eb7576ce 2320 result = x_bitmap_icon (f, arg);
ee78dc32
GV
2321 if (result)
2322 {
2323 UNBLOCK_INPUT;
2324 error ("No icon window available");
2325 }
2326
ee78dc32 2327 UNBLOCK_INPUT;
ee78dc32
GV
2328}
2329
2330/* Return non-nil if frame F wants a bitmap icon. */
2331
2332Lisp_Object
2333x_icon_type (f)
2334 FRAME_PTR f;
2335{
2336 Lisp_Object tem;
2337
2338 tem = assq_no_quit (Qicon_type, f->param_alist);
2339 if (CONSP (tem))
8e713be6 2340 return XCDR (tem);
ee78dc32
GV
2341 else
2342 return Qnil;
2343}
2344
2345void
2346x_set_icon_name (f, arg, oldval)
2347 struct frame *f;
2348 Lisp_Object arg, oldval;
2349{
ee78dc32
GV
2350 int result;
2351
2352 if (STRINGP (arg))
2353 {
2354 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
2355 return;
2356 }
2357 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
2358 return;
2359
2360 f->icon_name = arg;
2361
2362#if 0
fbd6baed 2363 if (f->output_data.w32->icon_bitmap != 0)
ee78dc32
GV
2364 return;
2365
2366 BLOCK_INPUT;
2367
2368 result = x_text_icon (f,
1edf84e7 2369 (char *) XSTRING ((!NILP (f->icon_name)
ee78dc32 2370 ? f->icon_name
1edf84e7
GV
2371 : !NILP (f->title)
2372 ? f->title
ee78dc32
GV
2373 : f->name))->data);
2374
2375 if (result)
2376 {
2377 UNBLOCK_INPUT;
2378 error ("No icon window available");
2379 }
2380
2381 /* If the window was unmapped (and its icon was mapped),
2382 the new icon is not mapped, so map the window in its stead. */
2383 if (FRAME_VISIBLE_P (f))
2384 {
2385#ifdef USE_X_TOOLKIT
fbd6baed 2386 XtPopup (f->output_data.w32->widget, XtGrabNone);
ee78dc32 2387#endif
fbd6baed 2388 XMapWindow (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f));
ee78dc32
GV
2389 }
2390
fbd6baed 2391 XFlush (FRAME_W32_DISPLAY (f));
ee78dc32
GV
2392 UNBLOCK_INPUT;
2393#endif
2394}
2395
2396extern Lisp_Object x_new_font ();
4587b026 2397extern Lisp_Object x_new_fontset();
ee78dc32
GV
2398
2399void
2400x_set_font (f, arg, oldval)
2401 struct frame *f;
2402 Lisp_Object arg, oldval;
2403{
2404 Lisp_Object result;
4587b026 2405 Lisp_Object fontset_name;
4b817373 2406 Lisp_Object frame;
ee78dc32
GV
2407
2408 CHECK_STRING (arg, 1);
2409
4587b026
GV
2410 fontset_name = Fquery_fontset (arg, Qnil);
2411
ee78dc32 2412 BLOCK_INPUT;
4587b026
GV
2413 result = (STRINGP (fontset_name)
2414 ? x_new_fontset (f, XSTRING (fontset_name)->data)
2415 : x_new_font (f, XSTRING (arg)->data));
ee78dc32
GV
2416 UNBLOCK_INPUT;
2417
2418 if (EQ (result, Qnil))
dfff8a69 2419 error ("Font `%s' is not defined", XSTRING (arg)->data);
ee78dc32 2420 else if (EQ (result, Qt))
dfff8a69 2421 error ("The characters of the given font have varying widths");
ee78dc32
GV
2422 else if (STRINGP (result))
2423 {
ee78dc32 2424 store_frame_param (f, Qfont, result);
6fc2811b 2425 recompute_basic_faces (f);
ee78dc32
GV
2426 }
2427 else
2428 abort ();
4b817373 2429
6fc2811b
JR
2430 do_pending_window_change (0);
2431
2432 /* Don't call `face-set-after-frame-default' when faces haven't been
2433 initialized yet. This is the case when called from
2434 Fx_create_frame. In that case, the X widget or window doesn't
2435 exist either, and we can end up in x_report_frame_params with a
2436 null widget which gives a segfault. */
2437 if (FRAME_FACE_CACHE (f))
2438 {
2439 XSETFRAME (frame, f);
2440 call1 (Qface_set_after_frame_default, frame);
2441 }
ee78dc32
GV
2442}
2443
2444void
2445x_set_border_width (f, arg, oldval)
2446 struct frame *f;
2447 Lisp_Object arg, oldval;
2448{
2449 CHECK_NUMBER (arg, 0);
2450
fbd6baed 2451 if (XINT (arg) == f->output_data.w32->border_width)
ee78dc32
GV
2452 return;
2453
fbd6baed 2454 if (FRAME_W32_WINDOW (f) != 0)
ee78dc32
GV
2455 error ("Cannot change the border width of a window");
2456
fbd6baed 2457 f->output_data.w32->border_width = XINT (arg);
ee78dc32
GV
2458}
2459
2460void
2461x_set_internal_border_width (f, arg, oldval)
2462 struct frame *f;
2463 Lisp_Object arg, oldval;
2464{
fbd6baed 2465 int old = f->output_data.w32->internal_border_width;
ee78dc32
GV
2466
2467 CHECK_NUMBER (arg, 0);
fbd6baed
GV
2468 f->output_data.w32->internal_border_width = XINT (arg);
2469 if (f->output_data.w32->internal_border_width < 0)
2470 f->output_data.w32->internal_border_width = 0;
ee78dc32 2471
fbd6baed 2472 if (f->output_data.w32->internal_border_width == old)
ee78dc32
GV
2473 return;
2474
fbd6baed 2475 if (FRAME_W32_WINDOW (f) != 0)
ee78dc32 2476 {
ee78dc32 2477 x_set_window_size (f, 0, f->width, f->height);
ee78dc32 2478 SET_FRAME_GARBAGED (f);
6fc2811b 2479 do_pending_window_change (0);
ee78dc32
GV
2480 }
2481}
2482
2483void
2484x_set_visibility (f, value, oldval)
2485 struct frame *f;
2486 Lisp_Object value, oldval;
2487{
2488 Lisp_Object frame;
2489 XSETFRAME (frame, f);
2490
2491 if (NILP (value))
2492 Fmake_frame_invisible (frame, Qt);
2493 else if (EQ (value, Qicon))
2494 Ficonify_frame (frame);
2495 else
2496 Fmake_frame_visible (frame);
2497}
2498
a1258667
JR
2499\f
2500/* Change window heights in windows rooted in WINDOW by N lines. */
2501
2502static void
2503x_change_window_heights (window, n)
2504 Lisp_Object window;
2505 int n;
2506{
2507 struct window *w = XWINDOW (window);
2508
2509 XSETFASTINT (w->top, XFASTINT (w->top) + n);
2510 XSETFASTINT (w->height, XFASTINT (w->height) - n);
2511
2512 if (INTEGERP (w->orig_top))
2513 XSETFASTINT (w->orig_top, XFASTINT (w->orig_top) + n);
2514 if (INTEGERP (w->orig_height))
2515 XSETFASTINT (w->orig_height, XFASTINT (w->orig_height) - n);
2516
2517 /* Handle just the top child in a vertical split. */
2518 if (!NILP (w->vchild))
2519 x_change_window_heights (w->vchild, n);
2520
2521 /* Adjust all children in a horizontal split. */
2522 for (window = w->hchild; !NILP (window); window = w->next)
2523 {
2524 w = XWINDOW (window);
2525 x_change_window_heights (window, n);
2526 }
2527}
2528
ee78dc32
GV
2529void
2530x_set_menu_bar_lines (f, value, oldval)
2531 struct frame *f;
2532 Lisp_Object value, oldval;
2533{
2534 int nlines;
2535 int olines = FRAME_MENU_BAR_LINES (f);
2536
2537 /* Right now, menu bars don't work properly in minibuf-only frames;
2538 most of the commands try to apply themselves to the minibuffer
6fc2811b 2539 frame itself, and get an error because you can't switch buffers
ee78dc32
GV
2540 in or split the minibuffer window. */
2541 if (FRAME_MINIBUF_ONLY_P (f))
2542 return;
2543
2544 if (INTEGERP (value))
2545 nlines = XINT (value);
2546 else
2547 nlines = 0;
2548
2549 FRAME_MENU_BAR_LINES (f) = 0;
2550 if (nlines)
2551 FRAME_EXTERNAL_MENU_BAR (f) = 1;
2552 else
2553 {
2554 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
2555 free_frame_menubar (f);
2556 FRAME_EXTERNAL_MENU_BAR (f) = 0;
1edf84e7
GV
2557
2558 /* Adjust the frame size so that the client (text) dimensions
2559 remain the same. This depends on FRAME_EXTERNAL_MENU_BAR being
2560 set correctly. */
2561 x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
6fc2811b 2562 do_pending_window_change (0);
ee78dc32 2563 }
6fc2811b
JR
2564 adjust_glyphs (f);
2565}
2566
2567
2568/* Set the number of lines used for the tool bar of frame F to VALUE.
2569 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
2570 is the old number of tool bar lines. This function changes the
2571 height of all windows on frame F to match the new tool bar height.
2572 The frame's height doesn't change. */
2573
2574void
2575x_set_tool_bar_lines (f, value, oldval)
2576 struct frame *f;
2577 Lisp_Object value, oldval;
2578{
36f8209a
JR
2579 int delta, nlines, root_height;
2580 Lisp_Object root_window;
6fc2811b
JR
2581
2582 /* Use VALUE only if an integer >= 0. */
2583 if (INTEGERP (value) && XINT (value) >= 0)
2584 nlines = XFASTINT (value);
2585 else
2586 nlines = 0;
2587
2588 /* Make sure we redisplay all windows in this frame. */
2589 ++windows_or_buffers_changed;
2590
2591 delta = nlines - FRAME_TOOL_BAR_LINES (f);
36f8209a
JR
2592
2593 /* Don't resize the tool-bar to more than we have room for. */
2594 root_window = FRAME_ROOT_WINDOW (f);
2595 root_height = XINT (XWINDOW (root_window)->height);
2596 if (root_height - delta < 1)
2597 {
2598 delta = root_height - 1;
2599 nlines = FRAME_TOOL_BAR_LINES (f) + delta;
2600 }
2601
6fc2811b 2602 FRAME_TOOL_BAR_LINES (f) = nlines;
36f8209a 2603 x_change_window_heights (root_window, delta);
6fc2811b 2604 adjust_glyphs (f);
36f8209a
JR
2605
2606 /* We also have to make sure that the internal border at the top of
2607 the frame, below the menu bar or tool bar, is redrawn when the
2608 tool bar disappears. This is so because the internal border is
2609 below the tool bar if one is displayed, but is below the menu bar
2610 if there isn't a tool bar. The tool bar draws into the area
2611 below the menu bar. */
2612 if (FRAME_W32_WINDOW (f) && FRAME_TOOL_BAR_LINES (f) == 0)
2613 {
2614 updating_frame = f;
2615 clear_frame ();
2616 clear_current_matrices (f);
2617 updating_frame = NULL;
2618 }
2619
2620 /* If the tool bar gets smaller, the internal border below it
2621 has to be cleared. It was formerly part of the display
2622 of the larger tool bar, and updating windows won't clear it. */
2623 if (delta < 0)
2624 {
2625 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
2626 int width = PIXEL_WIDTH (f);
2627 int y = nlines * CANON_Y_UNIT (f);
2628
2629 BLOCK_INPUT;
2630 {
2631 HDC hdc = get_frame_dc (f);
2632 w32_clear_area (f, hdc, 0, y, width, height);
2633 release_frame_dc (f, hdc);
2634 }
2635 UNBLOCK_INPUT;
2636 }
ee78dc32
GV
2637}
2638
6fc2811b 2639
ee78dc32 2640/* Change the name of frame F to NAME. If NAME is nil, set F's name to
fbd6baed 2641 w32_id_name.
ee78dc32
GV
2642
2643 If EXPLICIT is non-zero, that indicates that lisp code is setting the
2644 name; if NAME is a string, set F's name to NAME and set
2645 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
2646
2647 If EXPLICIT is zero, that indicates that Emacs redisplay code is
2648 suggesting a new name, which lisp code should override; if
2649 F->explicit_name is set, ignore the new name; otherwise, set it. */
2650
2651void
2652x_set_name (f, name, explicit)
2653 struct frame *f;
2654 Lisp_Object name;
2655 int explicit;
2656{
2657 /* Make sure that requests from lisp code override requests from
2658 Emacs redisplay code. */
2659 if (explicit)
2660 {
2661 /* If we're switching from explicit to implicit, we had better
2662 update the mode lines and thereby update the title. */
2663 if (f->explicit_name && NILP (name))
2664 update_mode_lines = 1;
2665
2666 f->explicit_name = ! NILP (name);
2667 }
2668 else if (f->explicit_name)
2669 return;
2670
fbd6baed 2671 /* If NAME is nil, set the name to the w32_id_name. */
ee78dc32
GV
2672 if (NILP (name))
2673 {
2674 /* Check for no change needed in this very common case
2675 before we do any consing. */
fbd6baed 2676 if (!strcmp (FRAME_W32_DISPLAY_INFO (f)->w32_id_name,
ee78dc32
GV
2677 XSTRING (f->name)->data))
2678 return;
fbd6baed 2679 name = build_string (FRAME_W32_DISPLAY_INFO (f)->w32_id_name);
ee78dc32
GV
2680 }
2681 else
2682 CHECK_STRING (name, 0);
2683
2684 /* Don't change the name if it's already NAME. */
2685 if (! NILP (Fstring_equal (name, f->name)))
2686 return;
2687
1edf84e7
GV
2688 f->name = name;
2689
2690 /* For setting the frame title, the title parameter should override
2691 the name parameter. */
2692 if (! NILP (f->title))
2693 name = f->title;
2694
fbd6baed 2695 if (FRAME_W32_WINDOW (f))
ee78dc32 2696 {
6fc2811b 2697 if (STRING_MULTIBYTE (name))
dfff8a69 2698 name = ENCODE_SYSTEM (name);
6fc2811b 2699
ee78dc32 2700 BLOCK_INPUT;
fbd6baed 2701 SetWindowText(FRAME_W32_WINDOW (f), XSTRING (name)->data);
ee78dc32
GV
2702 UNBLOCK_INPUT;
2703 }
ee78dc32
GV
2704}
2705
2706/* This function should be called when the user's lisp code has
2707 specified a name for the frame; the name will override any set by the
2708 redisplay code. */
2709void
2710x_explicitly_set_name (f, arg, oldval)
2711 FRAME_PTR f;
2712 Lisp_Object arg, oldval;
2713{
2714 x_set_name (f, arg, 1);
2715}
2716
2717/* This function should be called by Emacs redisplay code to set the
2718 name; names set this way will never override names set by the user's
2719 lisp code. */
2720void
2721x_implicitly_set_name (f, arg, oldval)
2722 FRAME_PTR f;
2723 Lisp_Object arg, oldval;
2724{
2725 x_set_name (f, arg, 0);
2726}
1edf84e7
GV
2727\f
2728/* Change the title of frame F to NAME.
2729 If NAME is nil, use the frame name as the title.
2730
2731 If EXPLICIT is non-zero, that indicates that lisp code is setting the
2732 name; if NAME is a string, set F's name to NAME and set
2733 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
2734
2735 If EXPLICIT is zero, that indicates that Emacs redisplay code is
2736 suggesting a new name, which lisp code should override; if
2737 F->explicit_name is set, ignore the new name; otherwise, set it. */
ee78dc32 2738
1edf84e7 2739void
6fc2811b 2740x_set_title (f, name, old_name)
1edf84e7 2741 struct frame *f;
6fc2811b 2742 Lisp_Object name, old_name;
1edf84e7
GV
2743{
2744 /* Don't change the title if it's already NAME. */
2745 if (EQ (name, f->title))
2746 return;
2747
2748 update_mode_lines = 1;
2749
2750 f->title = name;
2751
2752 if (NILP (name))
2753 name = f->name;
2754
2755 if (FRAME_W32_WINDOW (f))
2756 {
6fc2811b 2757 if (STRING_MULTIBYTE (name))
dfff8a69 2758 name = ENCODE_SYSTEM (name);
6fc2811b 2759
1edf84e7
GV
2760 BLOCK_INPUT;
2761 SetWindowText(FRAME_W32_WINDOW (f), XSTRING (name)->data);
2762 UNBLOCK_INPUT;
2763 }
2764}
2765\f
ee78dc32
GV
2766void
2767x_set_autoraise (f, arg, oldval)
2768 struct frame *f;
2769 Lisp_Object arg, oldval;
2770{
2771 f->auto_raise = !EQ (Qnil, arg);
2772}
2773
2774void
2775x_set_autolower (f, arg, oldval)
2776 struct frame *f;
2777 Lisp_Object arg, oldval;
2778{
2779 f->auto_lower = !EQ (Qnil, arg);
2780}
2781
2782void
2783x_set_unsplittable (f, arg, oldval)
2784 struct frame *f;
2785 Lisp_Object arg, oldval;
2786{
2787 f->no_split = !NILP (arg);
2788}
2789
2790void
2791x_set_vertical_scroll_bars (f, arg, oldval)
2792 struct frame *f;
2793 Lisp_Object arg, oldval;
2794{
1026b400
RS
2795 if ((EQ (arg, Qleft) && FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
2796 || (EQ (arg, Qright) && FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f))
2797 || (NILP (arg) && FRAME_HAS_VERTICAL_SCROLL_BARS (f))
2798 || (!NILP (arg) && ! FRAME_HAS_VERTICAL_SCROLL_BARS (f)))
ee78dc32 2799 {
1026b400
RS
2800 FRAME_VERTICAL_SCROLL_BAR_TYPE (f) = NILP (arg) ?
2801 vertical_scroll_bar_none :
87996783
GV
2802 /* Put scroll bars on the right by default, as is conventional
2803 on MS-Windows. */
2804 EQ (Qleft, arg)
2805 ? vertical_scroll_bar_left
2806 : vertical_scroll_bar_right;
ee78dc32
GV
2807
2808 /* We set this parameter before creating the window for the
2809 frame, so we can get the geometry right from the start.
2810 However, if the window hasn't been created yet, we shouldn't
2811 call x_set_window_size. */
fbd6baed 2812 if (FRAME_W32_WINDOW (f))
ee78dc32 2813 x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
6fc2811b 2814 do_pending_window_change (0);
ee78dc32
GV
2815 }
2816}
2817
2818void
2819x_set_scroll_bar_width (f, arg, oldval)
2820 struct frame *f;
2821 Lisp_Object arg, oldval;
2822{
6fc2811b
JR
2823 int wid = FONT_WIDTH (f->output_data.w32->font);
2824
ee78dc32
GV
2825 if (NILP (arg))
2826 {
6fc2811b
JR
2827 FRAME_SCROLL_BAR_PIXEL_WIDTH (f) = GetSystemMetrics (SM_CXVSCROLL);
2828 FRAME_SCROLL_BAR_COLS (f) = (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) +
2829 wid - 1) / wid;
2830 if (FRAME_W32_WINDOW (f))
2831 x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
2832 do_pending_window_change (0);
ee78dc32
GV
2833 }
2834 else if (INTEGERP (arg) && XINT (arg) > 0
2835 && XFASTINT (arg) != FRAME_SCROLL_BAR_PIXEL_WIDTH (f))
2836 {
ee78dc32 2837 FRAME_SCROLL_BAR_PIXEL_WIDTH (f) = XFASTINT (arg);
6fc2811b
JR
2838 FRAME_SCROLL_BAR_COLS (f) = (FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
2839 + wid-1) / wid;
fbd6baed 2840 if (FRAME_W32_WINDOW (f))
ee78dc32 2841 x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
6fc2811b 2842 do_pending_window_change (0);
ee78dc32 2843 }
6fc2811b
JR
2844 change_frame_size (f, 0, FRAME_WIDTH (f), 0, 0, 0);
2845 XWINDOW (FRAME_SELECTED_WINDOW (f))->cursor.hpos = 0;
2846 XWINDOW (FRAME_SELECTED_WINDOW (f))->cursor.x = 0;
ee78dc32
GV
2847}
2848\f
2849/* Subroutines of creating an frame. */
2850
2851/* Make sure that Vx_resource_name is set to a reasonable value.
2852 Fix it up, or set it to `emacs' if it is too hopeless. */
2853
2854static void
2855validate_x_resource_name ()
2856{
6fc2811b 2857 int len = 0;
ee78dc32
GV
2858 /* Number of valid characters in the resource name. */
2859 int good_count = 0;
2860 /* Number of invalid characters in the resource name. */
2861 int bad_count = 0;
2862 Lisp_Object new;
2863 int i;
2864
2865 if (STRINGP (Vx_resource_name))
2866 {
2867 unsigned char *p = XSTRING (Vx_resource_name)->data;
2868 int i;
2869
dfff8a69 2870 len = STRING_BYTES (XSTRING (Vx_resource_name));
ee78dc32
GV
2871
2872 /* Only letters, digits, - and _ are valid in resource names.
2873 Count the valid characters and count the invalid ones. */
2874 for (i = 0; i < len; i++)
2875 {
2876 int c = p[i];
2877 if (! ((c >= 'a' && c <= 'z')
2878 || (c >= 'A' && c <= 'Z')
2879 || (c >= '0' && c <= '9')
2880 || c == '-' || c == '_'))
2881 bad_count++;
2882 else
2883 good_count++;
2884 }
2885 }
2886 else
2887 /* Not a string => completely invalid. */
2888 bad_count = 5, good_count = 0;
2889
2890 /* If name is valid already, return. */
2891 if (bad_count == 0)
2892 return;
2893
2894 /* If name is entirely invalid, or nearly so, use `emacs'. */
2895 if (good_count == 0
2896 || (good_count == 1 && bad_count > 0))
2897 {
2898 Vx_resource_name = build_string ("emacs");
2899 return;
2900 }
2901
2902 /* Name is partly valid. Copy it and replace the invalid characters
2903 with underscores. */
2904
2905 Vx_resource_name = new = Fcopy_sequence (Vx_resource_name);
2906
2907 for (i = 0; i < len; i++)
2908 {
2909 int c = XSTRING (new)->data[i];
2910 if (! ((c >= 'a' && c <= 'z')
2911 || (c >= 'A' && c <= 'Z')
2912 || (c >= '0' && c <= '9')
2913 || c == '-' || c == '_'))
2914 XSTRING (new)->data[i] = '_';
2915 }
2916}
2917
2918
2919extern char *x_get_string_resource ();
2920
2921DEFUN ("x-get-resource", Fx_get_resource, Sx_get_resource, 2, 4, 0,
2922 "Return the value of ATTRIBUTE, of class CLASS, from the X defaults database.\n\
2923This uses `INSTANCE.ATTRIBUTE' as the key and `Emacs.CLASS' as the\n\
2924class, where INSTANCE is the name under which Emacs was invoked, or\n\
2925the name specified by the `-name' or `-rn' command-line arguments.\n\
2926\n\
2927The optional arguments COMPONENT and SUBCLASS add to the key and the\n\
2928class, respectively. You must specify both of them or neither.\n\
2929If you specify them, the key is `INSTANCE.COMPONENT.ATTRIBUTE'\n\
2930and the class is `Emacs.CLASS.SUBCLASS'.")
2931 (attribute, class, component, subclass)
2932 Lisp_Object attribute, class, component, subclass;
2933{
2934 register char *value;
2935 char *name_key;
2936 char *class_key;
2937
2938 CHECK_STRING (attribute, 0);
2939 CHECK_STRING (class, 0);
2940
2941 if (!NILP (component))
2942 CHECK_STRING (component, 1);
2943 if (!NILP (subclass))
2944 CHECK_STRING (subclass, 2);
2945 if (NILP (component) != NILP (subclass))
2946 error ("x-get-resource: must specify both COMPONENT and SUBCLASS or neither");
2947
2948 validate_x_resource_name ();
2949
2950 /* Allocate space for the components, the dots which separate them,
2951 and the final '\0'. Make them big enough for the worst case. */
dfff8a69 2952 name_key = (char *) alloca (STRING_BYTES (XSTRING (Vx_resource_name))
ee78dc32 2953 + (STRINGP (component)
dfff8a69
JR
2954 ? STRING_BYTES (XSTRING (component)) : 0)
2955 + STRING_BYTES (XSTRING (attribute))
ee78dc32
GV
2956 + 3);
2957
2958 class_key = (char *) alloca ((sizeof (EMACS_CLASS) - 1)
dfff8a69 2959 + STRING_BYTES (XSTRING (class))
ee78dc32 2960 + (STRINGP (subclass)
dfff8a69 2961 ? STRING_BYTES (XSTRING (subclass)) : 0)
ee78dc32
GV
2962 + 3);
2963
2964 /* Start with emacs.FRAMENAME for the name (the specific one)
2965 and with `Emacs' for the class key (the general one). */
2966 strcpy (name_key, XSTRING (Vx_resource_name)->data);
2967 strcpy (class_key, EMACS_CLASS);
2968
2969 strcat (class_key, ".");
2970 strcat (class_key, XSTRING (class)->data);
2971
2972 if (!NILP (component))
2973 {
2974 strcat (class_key, ".");
2975 strcat (class_key, XSTRING (subclass)->data);
2976
2977 strcat (name_key, ".");
2978 strcat (name_key, XSTRING (component)->data);
2979 }
2980
2981 strcat (name_key, ".");
2982 strcat (name_key, XSTRING (attribute)->data);
2983
2984 value = x_get_string_resource (Qnil,
2985 name_key, class_key);
2986
2987 if (value != (char *) 0)
2988 return build_string (value);
2989 else
2990 return Qnil;
2991}
2992
2993/* Used when C code wants a resource value. */
2994
2995char *
2996x_get_resource_string (attribute, class)
2997 char *attribute, *class;
2998{
ee78dc32
GV
2999 char *name_key;
3000 char *class_key;
6fc2811b 3001 struct frame *sf = SELECTED_FRAME ();
ee78dc32
GV
3002
3003 /* Allocate space for the components, the dots which separate them,
3004 and the final '\0'. */
dfff8a69 3005 name_key = (char *) alloca (STRING_BYTES (XSTRING (Vinvocation_name))
ee78dc32
GV
3006 + strlen (attribute) + 2);
3007 class_key = (char *) alloca ((sizeof (EMACS_CLASS) - 1)
3008 + strlen (class) + 2);
3009
3010 sprintf (name_key, "%s.%s",
3011 XSTRING (Vinvocation_name)->data,
3012 attribute);
3013 sprintf (class_key, "%s.%s", EMACS_CLASS, class);
3014
6fc2811b 3015 return x_get_string_resource (sf, name_key, class_key);
ee78dc32
GV
3016}
3017
3018/* Types we might convert a resource string into. */
3019enum resource_types
6fc2811b
JR
3020{
3021 RES_TYPE_NUMBER,
3022 RES_TYPE_FLOAT,
3023 RES_TYPE_BOOLEAN,
3024 RES_TYPE_STRING,
3025 RES_TYPE_SYMBOL
3026};
ee78dc32
GV
3027
3028/* Return the value of parameter PARAM.
3029
3030 First search ALIST, then Vdefault_frame_alist, then the X defaults
3031 database, using ATTRIBUTE as the attribute name and CLASS as its class.
3032
3033 Convert the resource to the type specified by desired_type.
3034
3035 If no default is specified, return Qunbound. If you call
6fc2811b 3036 w32_get_arg, make sure you deal with Qunbound in a reasonable way,
ee78dc32
GV
3037 and don't let it get stored in any Lisp-visible variables! */
3038
3039static Lisp_Object
6fc2811b 3040w32_get_arg (alist, param, attribute, class, type)
ee78dc32
GV
3041 Lisp_Object alist, param;
3042 char *attribute;
3043 char *class;
3044 enum resource_types type;
3045{
3046 register Lisp_Object tem;
3047
3048 tem = Fassq (param, alist);
3049 if (EQ (tem, Qnil))
3050 tem = Fassq (param, Vdefault_frame_alist);
3051 if (EQ (tem, Qnil))
3052 {
3053
3054 if (attribute)
3055 {
3056 tem = Fx_get_resource (build_string (attribute),
3057 build_string (class),
3058 Qnil, Qnil);
3059
3060 if (NILP (tem))
3061 return Qunbound;
3062
3063 switch (type)
3064 {
6fc2811b 3065 case RES_TYPE_NUMBER:
ee78dc32
GV
3066 return make_number (atoi (XSTRING (tem)->data));
3067
6fc2811b
JR
3068 case RES_TYPE_FLOAT:
3069 return make_float (atof (XSTRING (tem)->data));
3070
3071 case RES_TYPE_BOOLEAN:
ee78dc32
GV
3072 tem = Fdowncase (tem);
3073 if (!strcmp (XSTRING (tem)->data, "on")
3074 || !strcmp (XSTRING (tem)->data, "true"))
3075 return Qt;
3076 else
3077 return Qnil;
3078
6fc2811b 3079 case RES_TYPE_STRING:
ee78dc32
GV
3080 return tem;
3081
6fc2811b 3082 case RES_TYPE_SYMBOL:
ee78dc32
GV
3083 /* As a special case, we map the values `true' and `on'
3084 to Qt, and `false' and `off' to Qnil. */
3085 {
3086 Lisp_Object lower;
3087 lower = Fdowncase (tem);
3088 if (!strcmp (XSTRING (lower)->data, "on")
3089 || !strcmp (XSTRING (lower)->data, "true"))
3090 return Qt;
3091 else if (!strcmp (XSTRING (lower)->data, "off")
3092 || !strcmp (XSTRING (lower)->data, "false"))
3093 return Qnil;
3094 else
3095 return Fintern (tem, Qnil);
3096 }
3097
3098 default:
3099 abort ();
3100 }
3101 }
3102 else
3103 return Qunbound;
3104 }
3105 return Fcdr (tem);
3106}
3107
3108/* Record in frame F the specified or default value according to ALIST
dfff8a69
JR
3109 of the parameter named PROP (a Lisp symbol).
3110 If no value is specified for PROP, look for an X default for XPROP
ee78dc32
GV
3111 on the frame named NAME.
3112 If that is not found either, use the value DEFLT. */
3113
3114static Lisp_Object
3115x_default_parameter (f, alist, prop, deflt, xprop, xclass, type)
3116 struct frame *f;
3117 Lisp_Object alist;
3118 Lisp_Object prop;
3119 Lisp_Object deflt;
3120 char *xprop;
3121 char *xclass;
3122 enum resource_types type;
3123{
3124 Lisp_Object tem;
3125
6fc2811b 3126 tem = w32_get_arg (alist, prop, xprop, xclass, type);
ee78dc32
GV
3127 if (EQ (tem, Qunbound))
3128 tem = deflt;
3129 x_set_frame_parameters (f, Fcons (Fcons (prop, tem), Qnil));
3130 return tem;
3131}
3132\f
3133DEFUN ("x-parse-geometry", Fx_parse_geometry, Sx_parse_geometry, 1, 1, 0,
3134 "Parse an X-style geometry string STRING.\n\
3135Returns an alist of the form ((top . TOP), (left . LEFT) ... ).\n\
3136The properties returned may include `top', `left', `height', and `width'.\n\
3137The value of `left' or `top' may be an integer,\n\
3138or a list (+ N) meaning N pixels relative to top/left corner,\n\
3139or a list (- N) meaning -N pixels relative to bottom/right corner.")
3140 (string)
3141 Lisp_Object string;
3142{
3143 int geometry, x, y;
3144 unsigned int width, height;
3145 Lisp_Object result;
3146
3147 CHECK_STRING (string, 0);
3148
3149 geometry = XParseGeometry ((char *) XSTRING (string)->data,
3150 &x, &y, &width, &height);
3151
3152 result = Qnil;
3153 if (geometry & XValue)
3154 {
3155 Lisp_Object element;
3156
3157 if (x >= 0 && (geometry & XNegative))
3158 element = Fcons (Qleft, Fcons (Qminus, Fcons (make_number (-x), Qnil)));
3159 else if (x < 0 && ! (geometry & XNegative))
3160 element = Fcons (Qleft, Fcons (Qplus, Fcons (make_number (x), Qnil)));
3161 else
3162 element = Fcons (Qleft, make_number (x));
3163 result = Fcons (element, result);
3164 }
3165
3166 if (geometry & YValue)
3167 {
3168 Lisp_Object element;
3169
3170 if (y >= 0 && (geometry & YNegative))
3171 element = Fcons (Qtop, Fcons (Qminus, Fcons (make_number (-y), Qnil)));
3172 else if (y < 0 && ! (geometry & YNegative))
3173 element = Fcons (Qtop, Fcons (Qplus, Fcons (make_number (y), Qnil)));
3174 else
3175 element = Fcons (Qtop, make_number (y));
3176 result = Fcons (element, result);
3177 }
3178
3179 if (geometry & WidthValue)
3180 result = Fcons (Fcons (Qwidth, make_number (width)), result);
3181 if (geometry & HeightValue)
3182 result = Fcons (Fcons (Qheight, make_number (height)), result);
3183
3184 return result;
3185}
3186
3187/* Calculate the desired size and position of this window,
3188 and return the flags saying which aspects were specified.
3189
3190 This function does not make the coordinates positive. */
3191
3192#define DEFAULT_ROWS 40
3193#define DEFAULT_COLS 80
3194
3195static int
3196x_figure_window_size (f, parms)
3197 struct frame *f;
3198 Lisp_Object parms;
3199{
3200 register Lisp_Object tem0, tem1, tem2;
ee78dc32
GV
3201 long window_prompting = 0;
3202
3203 /* Default values if we fall through.
3204 Actually, if that happens we should get
3205 window manager prompting. */
1026b400 3206 SET_FRAME_WIDTH (f, DEFAULT_COLS);
ee78dc32
GV
3207 f->height = DEFAULT_ROWS;
3208 /* Window managers expect that if program-specified
3209 positions are not (0,0), they're intentional, not defaults. */
fbd6baed
GV
3210 f->output_data.w32->top_pos = 0;
3211 f->output_data.w32->left_pos = 0;
ee78dc32 3212
6fc2811b
JR
3213 tem0 = w32_get_arg (parms, Qheight, 0, 0, RES_TYPE_NUMBER);
3214 tem1 = w32_get_arg (parms, Qwidth, 0, 0, RES_TYPE_NUMBER);
3215 tem2 = w32_get_arg (parms, Quser_size, 0, 0, RES_TYPE_NUMBER);
ee78dc32
GV
3216 if (! EQ (tem0, Qunbound) || ! EQ (tem1, Qunbound))
3217 {
3218 if (!EQ (tem0, Qunbound))
3219 {
3220 CHECK_NUMBER (tem0, 0);
3221 f->height = XINT (tem0);
3222 }
3223 if (!EQ (tem1, Qunbound))
3224 {
3225 CHECK_NUMBER (tem1, 0);
1026b400 3226 SET_FRAME_WIDTH (f, XINT (tem1));
ee78dc32
GV
3227 }
3228 if (!NILP (tem2) && !EQ (tem2, Qunbound))
3229 window_prompting |= USSize;
3230 else
3231 window_prompting |= PSize;
3232 }
3233
fbd6baed 3234 f->output_data.w32->vertical_scroll_bar_extra
ee78dc32
GV
3235 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
3236 ? 0
3237 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
3238 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
fbd6baed 3239 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.w32->font)));
6fc2811b
JR
3240 f->output_data.w32->flags_areas_extra
3241 = FRAME_FLAGS_AREA_WIDTH (f);
fbd6baed
GV
3242 f->output_data.w32->pixel_width = CHAR_TO_PIXEL_WIDTH (f, f->width);
3243 f->output_data.w32->pixel_height = CHAR_TO_PIXEL_HEIGHT (f, f->height);
ee78dc32 3244
6fc2811b
JR
3245 tem0 = w32_get_arg (parms, Qtop, 0, 0, RES_TYPE_NUMBER);
3246 tem1 = w32_get_arg (parms, Qleft, 0, 0, RES_TYPE_NUMBER);
3247 tem2 = w32_get_arg (parms, Quser_position, 0, 0, RES_TYPE_NUMBER);
ee78dc32
GV
3248 if (! EQ (tem0, Qunbound) || ! EQ (tem1, Qunbound))
3249 {
3250 if (EQ (tem0, Qminus))
3251 {
fbd6baed 3252 f->output_data.w32->top_pos = 0;
ee78dc32
GV
3253 window_prompting |= YNegative;
3254 }
8e713be6
KR
3255 else if (CONSP (tem0) && EQ (XCAR (tem0), Qminus)
3256 && CONSP (XCDR (tem0))
3257 && INTEGERP (XCAR (XCDR (tem0))))
ee78dc32 3258 {
8e713be6 3259 f->output_data.w32->top_pos = - XINT (XCAR (XCDR (tem0)));
ee78dc32
GV
3260 window_prompting |= YNegative;
3261 }
8e713be6
KR
3262 else if (CONSP (tem0) && EQ (XCAR (tem0), Qplus)
3263 && CONSP (XCDR (tem0))
3264 && INTEGERP (XCAR (XCDR (tem0))))
ee78dc32 3265 {
8e713be6 3266 f->output_data.w32->top_pos = XINT (XCAR (XCDR (tem0)));
ee78dc32
GV
3267 }
3268 else if (EQ (tem0, Qunbound))
fbd6baed 3269 f->output_data.w32->top_pos = 0;
ee78dc32
GV
3270 else
3271 {
3272 CHECK_NUMBER (tem0, 0);
fbd6baed
GV
3273 f->output_data.w32->top_pos = XINT (tem0);
3274 if (f->output_data.w32->top_pos < 0)
ee78dc32
GV
3275 window_prompting |= YNegative;
3276 }
3277
3278 if (EQ (tem1, Qminus))
3279 {
fbd6baed 3280 f->output_data.w32->left_pos = 0;
ee78dc32
GV
3281 window_prompting |= XNegative;
3282 }
8e713be6
KR
3283 else if (CONSP (tem1) && EQ (XCAR (tem1), Qminus)
3284 && CONSP (XCDR (tem1))
3285 && INTEGERP (XCAR (XCDR (tem1))))
ee78dc32 3286 {
8e713be6 3287 f->output_data.w32->left_pos = - XINT (XCAR (XCDR (tem1)));
ee78dc32
GV
3288 window_prompting |= XNegative;
3289 }
8e713be6
KR
3290 else if (CONSP (tem1) && EQ (XCAR (tem1), Qplus)
3291 && CONSP (XCDR (tem1))
3292 && INTEGERP (XCAR (XCDR (tem1))))
ee78dc32 3293 {
8e713be6 3294 f->output_data.w32->left_pos = XINT (XCAR (XCDR (tem1)));
ee78dc32
GV
3295 }
3296 else if (EQ (tem1, Qunbound))
fbd6baed 3297 f->output_data.w32->left_pos = 0;
ee78dc32
GV
3298 else
3299 {
3300 CHECK_NUMBER (tem1, 0);
fbd6baed
GV
3301 f->output_data.w32->left_pos = XINT (tem1);
3302 if (f->output_data.w32->left_pos < 0)
ee78dc32
GV
3303 window_prompting |= XNegative;
3304 }
3305
3306 if (!NILP (tem2) && ! EQ (tem2, Qunbound))
3307 window_prompting |= USPosition;
3308 else
3309 window_prompting |= PPosition;
3310 }
3311
3312 return window_prompting;
3313}
3314
3315\f
3316
fbd6baed 3317extern LRESULT CALLBACK w32_wnd_proc ();
ee78dc32
GV
3318
3319BOOL
fbd6baed 3320w32_init_class (hinst)
ee78dc32
GV
3321 HINSTANCE hinst;
3322{
3323 WNDCLASS wc;
3324
5ac45f98 3325 wc.style = CS_HREDRAW | CS_VREDRAW;
fbd6baed 3326 wc.lpfnWndProc = (WNDPROC) w32_wnd_proc;
ee78dc32
GV
3327 wc.cbClsExtra = 0;
3328 wc.cbWndExtra = WND_EXTRA_BYTES;
3329 wc.hInstance = hinst;
3330 wc.hIcon = LoadIcon (hinst, EMACS_CLASS);
3331 wc.hCursor = LoadCursor (NULL, IDC_ARROW);
4587b026 3332 wc.hbrBackground = NULL; /* GetStockObject (WHITE_BRUSH); */
ee78dc32
GV
3333 wc.lpszMenuName = NULL;
3334 wc.lpszClassName = EMACS_CLASS;
3335
3336 return (RegisterClass (&wc));
3337}
3338
3339HWND
fbd6baed 3340w32_createscrollbar (f, bar)
ee78dc32
GV
3341 struct frame *f;
3342 struct scroll_bar * bar;
3343{
3344 return (CreateWindow ("SCROLLBAR", "", SBS_VERT | WS_CHILD | WS_VISIBLE,
3345 /* Position and size of scroll bar. */
6fc2811b
JR
3346 XINT(bar->left) + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
3347 XINT(bar->top),
3348 XINT(bar->width) - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
3349 XINT(bar->height),
fbd6baed 3350 FRAME_W32_WINDOW (f),
ee78dc32
GV
3351 NULL,
3352 hinst,
3353 NULL));
3354}
3355
3356void
fbd6baed 3357w32_createwindow (f)
ee78dc32
GV
3358 struct frame *f;
3359{
3360 HWND hwnd;
1edf84e7
GV
3361 RECT rect;
3362
3363 rect.left = rect.top = 0;
3364 rect.right = PIXEL_WIDTH (f);
3365 rect.bottom = PIXEL_HEIGHT (f);
3366
3367 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
3368 FRAME_EXTERNAL_MENU_BAR (f));
ee78dc32
GV
3369
3370 /* Do first time app init */
3371
3372 if (!hprevinst)
3373 {
fbd6baed 3374 w32_init_class (hinst);
ee78dc32
GV
3375 }
3376
1edf84e7
GV
3377 FRAME_W32_WINDOW (f) = hwnd
3378 = CreateWindow (EMACS_CLASS,
3379 f->namebuf,
3380 f->output_data.w32->dwStyle | WS_CLIPCHILDREN,
3381 f->output_data.w32->left_pos,
3382 f->output_data.w32->top_pos,
3383 rect.right - rect.left,
3384 rect.bottom - rect.top,
3385 NULL,
3386 NULL,
3387 hinst,
3388 NULL);
3389
ee78dc32
GV
3390 if (hwnd)
3391 {
1edf84e7
GV
3392 SetWindowLong (hwnd, WND_FONTWIDTH_INDEX, FONT_WIDTH (f->output_data.w32->font));
3393 SetWindowLong (hwnd, WND_LINEHEIGHT_INDEX, f->output_data.w32->line_height);
3394 SetWindowLong (hwnd, WND_BORDER_INDEX, f->output_data.w32->internal_border_width);
3395 SetWindowLong (hwnd, WND_SCROLLBAR_INDEX, f->output_data.w32->vertical_scroll_bar_extra);
6fc2811b 3396 SetWindowLong (hwnd, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
ee78dc32 3397
cb9e33d4
RS
3398 /* Enable drag-n-drop. */
3399 DragAcceptFiles (hwnd, TRUE);
3400
5ac45f98
GV
3401 /* Do this to discard the default setting specified by our parent. */
3402 ShowWindow (hwnd, SW_HIDE);
3c190163 3403 }
3c190163
GV
3404}
3405
ee78dc32
GV
3406void
3407my_post_msg (wmsg, hwnd, msg, wParam, lParam)
fbd6baed 3408 W32Msg * wmsg;
ee78dc32
GV
3409 HWND hwnd;
3410 UINT msg;
3411 WPARAM wParam;
3412 LPARAM lParam;
3413{
3414 wmsg->msg.hwnd = hwnd;
3415 wmsg->msg.message = msg;
3416 wmsg->msg.wParam = wParam;
3417 wmsg->msg.lParam = lParam;
3418 wmsg->msg.time = GetMessageTime ();
3419
3420 post_msg (wmsg);
3421}
3422
e9e23e23 3423/* GetKeyState and MapVirtualKey on Windows 95 do not actually distinguish
a1a80b40
GV
3424 between left and right keys as advertised. We test for this
3425 support dynamically, and set a flag when the support is absent. If
3426 absent, we keep track of the left and right control and alt keys
3427 ourselves. This is particularly necessary on keyboards that rely
3428 upon the AltGr key, which is represented as having the left control
3429 and right alt keys pressed. For these keyboards, we need to know
3430 when the left alt key has been pressed in addition to the AltGr key
3431 so that we can properly support M-AltGr-key sequences (such as M-@
3432 on Swedish keyboards). */
3433
3434#define EMACS_LCONTROL 0
3435#define EMACS_RCONTROL 1
3436#define EMACS_LMENU 2
3437#define EMACS_RMENU 3
3438
3439static int modifiers[4];
3440static int modifiers_recorded;
3441static int modifier_key_support_tested;
3442
3443static void
3444test_modifier_support (unsigned int wparam)
3445{
3446 unsigned int l, r;
3447
3448 if (wparam != VK_CONTROL && wparam != VK_MENU)
3449 return;
3450 if (wparam == VK_CONTROL)
3451 {
3452 l = VK_LCONTROL;
3453 r = VK_RCONTROL;
3454 }
3455 else
3456 {
3457 l = VK_LMENU;
3458 r = VK_RMENU;
3459 }
3460 if (!(GetKeyState (l) & 0x8000) && !(GetKeyState (r) & 0x8000))
3461 modifiers_recorded = 1;
3462 else
3463 modifiers_recorded = 0;
3464 modifier_key_support_tested = 1;
3465}
3466
3467static void
3468record_keydown (unsigned int wparam, unsigned int lparam)
3469{
3470 int i;
3471
3472 if (!modifier_key_support_tested)
3473 test_modifier_support (wparam);
3474
3475 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
3476 return;
3477
3478 if (wparam == VK_CONTROL)
3479 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
3480 else
3481 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
3482
3483 modifiers[i] = 1;
3484}
3485
3486static void
3487record_keyup (unsigned int wparam, unsigned int lparam)
3488{
3489 int i;
3490
3491 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
3492 return;
3493
3494 if (wparam == VK_CONTROL)
3495 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
3496 else
3497 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
3498
3499 modifiers[i] = 0;
3500}
3501
da36a4d6
GV
3502/* Emacs can lose focus while a modifier key has been pressed. When
3503 it regains focus, be conservative and clear all modifiers since
3504 we cannot reconstruct the left and right modifier state. */
3505static void
3506reset_modifiers ()
3507{
8681157a
RS
3508 SHORT ctrl, alt;
3509
adcc3809
GV
3510 if (GetFocus () == NULL)
3511 /* Emacs doesn't have keyboard focus. Do nothing. */
da36a4d6 3512 return;
8681157a
RS
3513
3514 ctrl = GetAsyncKeyState (VK_CONTROL);
3515 alt = GetAsyncKeyState (VK_MENU);
3516
8681157a
RS
3517 if (!(ctrl & 0x08000))
3518 /* Clear any recorded control modifier state. */
3519 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
3520
3521 if (!(alt & 0x08000))
3522 /* Clear any recorded alt modifier state. */
3523 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
3524
adcc3809
GV
3525 /* Update the state of all modifier keys, because modifiers used in
3526 hot-key combinations can get stuck on if Emacs loses focus as a
3527 result of a hot-key being pressed. */
3528 {
3529 BYTE keystate[256];
3530
3531#define CURRENT_STATE(key) ((GetAsyncKeyState (key) & 0x8000) >> 8)
3532
3533 GetKeyboardState (keystate);
3534 keystate[VK_SHIFT] = CURRENT_STATE (VK_SHIFT);
3535 keystate[VK_CONTROL] = CURRENT_STATE (VK_CONTROL);
3536 keystate[VK_LCONTROL] = CURRENT_STATE (VK_LCONTROL);
3537 keystate[VK_RCONTROL] = CURRENT_STATE (VK_RCONTROL);
3538 keystate[VK_MENU] = CURRENT_STATE (VK_MENU);
3539 keystate[VK_LMENU] = CURRENT_STATE (VK_LMENU);
3540 keystate[VK_RMENU] = CURRENT_STATE (VK_RMENU);
3541 keystate[VK_LWIN] = CURRENT_STATE (VK_LWIN);
3542 keystate[VK_RWIN] = CURRENT_STATE (VK_RWIN);
3543 keystate[VK_APPS] = CURRENT_STATE (VK_APPS);
3544 SetKeyboardState (keystate);
3545 }
da36a4d6
GV
3546}
3547
7830e24b
RS
3548/* Synchronize modifier state with what is reported with the current
3549 keystroke. Even if we cannot distinguish between left and right
3550 modifier keys, we know that, if no modifiers are set, then neither
3551 the left or right modifier should be set. */
3552static void
3553sync_modifiers ()
3554{
3555 if (!modifiers_recorded)
3556 return;
3557
3558 if (!(GetKeyState (VK_CONTROL) & 0x8000))
3559 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
3560
3561 if (!(GetKeyState (VK_MENU) & 0x8000))
3562 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
3563}
3564
a1a80b40
GV
3565static int
3566modifier_set (int vkey)
3567{
ccc2d29c 3568 if (vkey == VK_CAPITAL || vkey == VK_SCROLL)
891560d6 3569 return (GetKeyState (vkey) & 0x1);
a1a80b40
GV
3570 if (!modifiers_recorded)
3571 return (GetKeyState (vkey) & 0x8000);
3572
3573 switch (vkey)
3574 {
3575 case VK_LCONTROL:
3576 return modifiers[EMACS_LCONTROL];
3577 case VK_RCONTROL:
3578 return modifiers[EMACS_RCONTROL];
3579 case VK_LMENU:
3580 return modifiers[EMACS_LMENU];
3581 case VK_RMENU:
3582 return modifiers[EMACS_RMENU];
a1a80b40
GV
3583 }
3584 return (GetKeyState (vkey) & 0x8000);
3585}
3586
ccc2d29c
GV
3587/* Convert between the modifier bits W32 uses and the modifier bits
3588 Emacs uses. */
3589
3590unsigned int
3591w32_key_to_modifier (int key)
3592{
3593 Lisp_Object key_mapping;
3594
3595 switch (key)
3596 {
3597 case VK_LWIN:
3598 key_mapping = Vw32_lwindow_modifier;
3599 break;
3600 case VK_RWIN:
3601 key_mapping = Vw32_rwindow_modifier;
3602 break;
3603 case VK_APPS:
3604 key_mapping = Vw32_apps_modifier;
3605 break;
3606 case VK_SCROLL:
3607 key_mapping = Vw32_scroll_lock_modifier;
3608 break;
3609 default:
3610 key_mapping = Qnil;
3611 }
3612
adcc3809
GV
3613 /* NB. This code runs in the input thread, asychronously to the lisp
3614 thread, so we must be careful to ensure access to lisp data is
3615 thread-safe. The following code is safe because the modifier
3616 variable values are updated atomically from lisp and symbols are
3617 not relocated by GC. Also, we don't have to worry about seeing GC
3618 markbits here. */
3619 if (EQ (key_mapping, Qhyper))
ccc2d29c 3620 return hyper_modifier;
adcc3809 3621 if (EQ (key_mapping, Qsuper))
ccc2d29c 3622 return super_modifier;
adcc3809 3623 if (EQ (key_mapping, Qmeta))
ccc2d29c 3624 return meta_modifier;
adcc3809 3625 if (EQ (key_mapping, Qalt))
ccc2d29c 3626 return alt_modifier;
adcc3809 3627 if (EQ (key_mapping, Qctrl))
ccc2d29c 3628 return ctrl_modifier;
adcc3809 3629 if (EQ (key_mapping, Qcontrol)) /* synonym for ctrl */
ccc2d29c 3630 return ctrl_modifier;
adcc3809 3631 if (EQ (key_mapping, Qshift))
ccc2d29c
GV
3632 return shift_modifier;
3633
3634 /* Don't generate any modifier if not explicitly requested. */
3635 return 0;
3636}
3637
3638unsigned int
3639w32_get_modifiers ()
3640{
3641 return ((modifier_set (VK_SHIFT) ? shift_modifier : 0) |
3642 (modifier_set (VK_CONTROL) ? ctrl_modifier : 0) |
3643 (modifier_set (VK_LWIN) ? w32_key_to_modifier (VK_LWIN) : 0) |
3644 (modifier_set (VK_RWIN) ? w32_key_to_modifier (VK_RWIN) : 0) |
3645 (modifier_set (VK_APPS) ? w32_key_to_modifier (VK_APPS) : 0) |
3646 (modifier_set (VK_SCROLL) ? w32_key_to_modifier (VK_SCROLL) : 0) |
3647 (modifier_set (VK_MENU) ?
3648 ((NILP (Vw32_alt_is_meta)) ? alt_modifier : meta_modifier) : 0));
3649}
3650
a1a80b40
GV
3651/* We map the VK_* modifiers into console modifier constants
3652 so that we can use the same routines to handle both console
3653 and window input. */
3654
3655static int
ccc2d29c 3656construct_console_modifiers ()
a1a80b40
GV
3657{
3658 int mods;
3659
a1a80b40
GV
3660 mods = 0;
3661 mods |= (modifier_set (VK_SHIFT)) ? SHIFT_PRESSED : 0;
3662 mods |= (modifier_set (VK_CAPITAL)) ? CAPSLOCK_ON : 0;
ccc2d29c
GV
3663 mods |= (modifier_set (VK_SCROLL)) ? SCROLLLOCK_ON : 0;
3664 mods |= (modifier_set (VK_NUMLOCK)) ? NUMLOCK_ON : 0;
a1a80b40
GV
3665 mods |= (modifier_set (VK_LCONTROL)) ? LEFT_CTRL_PRESSED : 0;
3666 mods |= (modifier_set (VK_RCONTROL)) ? RIGHT_CTRL_PRESSED : 0;
3667 mods |= (modifier_set (VK_LMENU)) ? LEFT_ALT_PRESSED : 0;
3668 mods |= (modifier_set (VK_RMENU)) ? RIGHT_ALT_PRESSED : 0;
ccc2d29c
GV
3669 mods |= (modifier_set (VK_LWIN)) ? LEFT_WIN_PRESSED : 0;
3670 mods |= (modifier_set (VK_RWIN)) ? RIGHT_WIN_PRESSED : 0;
3671 mods |= (modifier_set (VK_APPS)) ? APPS_PRESSED : 0;
a1a80b40
GV
3672
3673 return mods;
3674}
3675
ccc2d29c
GV
3676static int
3677w32_get_key_modifiers (unsigned int wparam, unsigned int lparam)
da36a4d6 3678{
ccc2d29c
GV
3679 int mods;
3680
3681 /* Convert to emacs modifiers. */
3682 mods = w32_kbd_mods_to_emacs (construct_console_modifiers (), wparam);
3683
3684 return mods;
3685}
da36a4d6 3686
ccc2d29c
GV
3687unsigned int
3688map_keypad_keys (unsigned int virt_key, unsigned int extended)
3689{
3690 if (virt_key < VK_CLEAR || virt_key > VK_DELETE)
3691 return virt_key;
da36a4d6 3692
ccc2d29c 3693 if (virt_key == VK_RETURN)
da36a4d6
GV
3694 return (extended ? VK_NUMPAD_ENTER : VK_RETURN);
3695
ccc2d29c
GV
3696 if (virt_key >= VK_PRIOR && virt_key <= VK_DOWN)
3697 return (!extended ? (VK_NUMPAD_PRIOR + (virt_key - VK_PRIOR)) : virt_key);
3698
3699 if (virt_key == VK_INSERT || virt_key == VK_DELETE)
3700 return (!extended ? (VK_NUMPAD_INSERT + (virt_key - VK_INSERT)) : virt_key);
3701
3702 if (virt_key == VK_CLEAR)
3703 return (!extended ? VK_NUMPAD_CLEAR : virt_key);
3704
3705 return virt_key;
3706}
3707
3708/* List of special key combinations which w32 would normally capture,
3709 but emacs should grab instead. Not directly visible to lisp, to
3710 simplify synchronization. Each item is an integer encoding a virtual
3711 key code and modifier combination to capture. */
3712Lisp_Object w32_grabbed_keys;
3713
3714#define HOTKEY(vk,mods) make_number (((vk) & 255) | ((mods) << 8))
3715#define HOTKEY_ID(k) (XFASTINT (k) & 0xbfff)
3716#define HOTKEY_VK_CODE(k) (XFASTINT (k) & 255)
3717#define HOTKEY_MODIFIERS(k) (XFASTINT (k) >> 8)
3718
3719/* Register hot-keys for reserved key combinations when Emacs has
3720 keyboard focus, since this is the only way Emacs can receive key
3721 combinations like Alt-Tab which are used by the system. */
3722
3723static void
3724register_hot_keys (hwnd)
3725 HWND hwnd;
3726{
3727 Lisp_Object keylist;
3728
3729 /* Use GC_CONSP, since we are called asynchronously. */
3730 for (keylist = w32_grabbed_keys; GC_CONSP (keylist); keylist = XCDR (keylist))
3731 {
3732 Lisp_Object key = XCAR (keylist);
3733
3734 /* Deleted entries get set to nil. */
3735 if (!INTEGERP (key))
3736 continue;
3737
3738 RegisterHotKey (hwnd, HOTKEY_ID (key),
3739 HOTKEY_MODIFIERS (key), HOTKEY_VK_CODE (key));
3740 }
3741}
3742
3743static void
3744unregister_hot_keys (hwnd)
3745 HWND hwnd;
3746{
3747 Lisp_Object keylist;
3748
3749 /* Use GC_CONSP, since we are called asynchronously. */
3750 for (keylist = w32_grabbed_keys; GC_CONSP (keylist); keylist = XCDR (keylist))
3751 {
3752 Lisp_Object key = XCAR (keylist);
3753
3754 if (!INTEGERP (key))
3755 continue;
3756
3757 UnregisterHotKey (hwnd, HOTKEY_ID (key));
3758 }
3759}
3760
5ac45f98
GV
3761/* Main message dispatch loop. */
3762
1edf84e7
GV
3763static void
3764w32_msg_pump (deferred_msg * msg_buf)
5ac45f98
GV
3765{
3766 MSG msg;
ccc2d29c
GV
3767 int result;
3768 HWND focus_window;
93fbe8b7
GV
3769
3770 msh_mousewheel = RegisterWindowMessage (MSH_MOUSEWHEEL);
5ac45f98 3771
5ac45f98
GV
3772 while (GetMessage (&msg, NULL, 0, 0))
3773 {
3774 if (msg.hwnd == NULL)
3775 {
3776 switch (msg.message)
3777 {
3ef68e6b
AI
3778 case WM_NULL:
3779 /* Produced by complete_deferred_msg; just ignore. */
3780 break;
5ac45f98 3781 case WM_EMACS_CREATEWINDOW:
fbd6baed 3782 w32_createwindow ((struct frame *) msg.wParam);
1edf84e7
GV
3783 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
3784 abort ();
5ac45f98 3785 break;
dfdb4047
GV
3786 case WM_EMACS_SETLOCALE:
3787 SetThreadLocale (msg.wParam);
3788 /* Reply is not expected. */
3789 break;
ccc2d29c
GV
3790 case WM_EMACS_SETKEYBOARDLAYOUT:
3791 result = (int) ActivateKeyboardLayout ((HKL) msg.wParam, 0);
3792 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
3793 result, 0))
3794 abort ();
3795 break;
3796 case WM_EMACS_REGISTER_HOT_KEY:
3797 focus_window = GetFocus ();
3798 if (focus_window != NULL)
3799 RegisterHotKey (focus_window,
3800 HOTKEY_ID (msg.wParam),
3801 HOTKEY_MODIFIERS (msg.wParam),
3802 HOTKEY_VK_CODE (msg.wParam));
3803 /* Reply is not expected. */
3804 break;
3805 case WM_EMACS_UNREGISTER_HOT_KEY:
3806 focus_window = GetFocus ();
3807 if (focus_window != NULL)
3808 UnregisterHotKey (focus_window, HOTKEY_ID (msg.wParam));
adcc3809
GV
3809 /* Mark item as erased. NB: this code must be
3810 thread-safe. The next line is okay because the cons
3811 cell is never made into garbage and is not relocated by
3812 GC. */
ccc2d29c
GV
3813 XCAR ((Lisp_Object) msg.lParam) = Qnil;
3814 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
3815 abort ();
3816 break;
adcc3809
GV
3817 case WM_EMACS_TOGGLE_LOCK_KEY:
3818 {
3819 int vk_code = (int) msg.wParam;
3820 int cur_state = (GetKeyState (vk_code) & 1);
3821 Lisp_Object new_state = (Lisp_Object) msg.lParam;
3822
3823 /* NB: This code must be thread-safe. It is safe to
3824 call NILP because symbols are not relocated by GC,
3825 and pointer here is not touched by GC (so the markbit
3826 can't be set). Numbers are safe because they are
3827 immediate values. */
3828 if (NILP (new_state)
3829 || (NUMBERP (new_state)
3830 && (XUINT (new_state)) & 1 != cur_state))
3831 {
3832 one_w32_display_info.faked_key = vk_code;
3833
3834 keybd_event ((BYTE) vk_code,
3835 (BYTE) MapVirtualKey (vk_code, 0),
3836 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
3837 keybd_event ((BYTE) vk_code,
3838 (BYTE) MapVirtualKey (vk_code, 0),
3839 KEYEVENTF_EXTENDEDKEY | 0, 0);
3840 keybd_event ((BYTE) vk_code,
3841 (BYTE) MapVirtualKey (vk_code, 0),
3842 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
3843 cur_state = !cur_state;
3844 }
3845 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
3846 cur_state, 0))
3847 abort ();
3848 }
3849 break;
1edf84e7 3850 default:
1edf84e7 3851 DebPrint (("msg %x not expected by w32_msg_pump\n", msg.message));
5ac45f98
GV
3852 }
3853 }
3854 else
3855 {
3856 DispatchMessage (&msg);
3857 }
1edf84e7
GV
3858
3859 /* Exit nested loop when our deferred message has completed. */
3860 if (msg_buf->completed)
3861 break;
5ac45f98 3862 }
1edf84e7
GV
3863}
3864
3865deferred_msg * deferred_msg_head;
3866
3867static deferred_msg *
3868find_deferred_msg (HWND hwnd, UINT msg)
3869{
3870 deferred_msg * item;
3871
3872 /* Don't actually need synchronization for read access, since
3873 modification of single pointer is always atomic. */
3874 /* enter_crit (); */
3875
3876 for (item = deferred_msg_head; item != NULL; item = item->next)
3877 if (item->w32msg.msg.hwnd == hwnd
3878 && item->w32msg.msg.message == msg)
3879 break;
3880
3881 /* leave_crit (); */
3882
3883 return item;
3884}
3885
3886static LRESULT
3887send_deferred_msg (deferred_msg * msg_buf,
3888 HWND hwnd,
3889 UINT msg,
3890 WPARAM wParam,
3891 LPARAM lParam)
3892{
3893 /* Only input thread can send deferred messages. */
3894 if (GetCurrentThreadId () != dwWindowsThreadId)
3895 abort ();
3896
3897 /* It is an error to send a message that is already deferred. */
3898 if (find_deferred_msg (hwnd, msg) != NULL)
3899 abort ();
3900
3901 /* Enforced synchronization is not needed because this is the only
3902 function that alters deferred_msg_head, and the following critical
3903 section is guaranteed to only be serially reentered (since only the
3904 input thread can call us). */
3905
3906 /* enter_crit (); */
3907
3908 msg_buf->completed = 0;
3909 msg_buf->next = deferred_msg_head;
3910 deferred_msg_head = msg_buf;
3911 my_post_msg (&msg_buf->w32msg, hwnd, msg, wParam, lParam);
3912
3913 /* leave_crit (); */
3914
3915 /* Start a new nested message loop to process other messages until
3916 this one is completed. */
3917 w32_msg_pump (msg_buf);
3918
3919 deferred_msg_head = msg_buf->next;
3920
3921 return msg_buf->result;
3922}
3923
3924void
3925complete_deferred_msg (HWND hwnd, UINT msg, LRESULT result)
3926{
3927 deferred_msg * msg_buf = find_deferred_msg (hwnd, msg);
3928
3929 if (msg_buf == NULL)
3ef68e6b
AI
3930 /* Message may have been cancelled, so don't abort(). */
3931 return;
1edf84e7
GV
3932
3933 msg_buf->result = result;
3934 msg_buf->completed = 1;
3935
3936 /* Ensure input thread is woken so it notices the completion. */
3937 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
3938}
3939
3ef68e6b
AI
3940void
3941cancel_all_deferred_msgs ()
3942{
3943 deferred_msg * item;
3944
3945 /* Don't actually need synchronization for read access, since
3946 modification of single pointer is always atomic. */
3947 /* enter_crit (); */
3948
3949 for (item = deferred_msg_head; item != NULL; item = item->next)
3950 {
3951 item->result = 0;
3952 item->completed = 1;
3953 }
3954
3955 /* leave_crit (); */
3956
3957 /* Ensure input thread is woken so it notices the completion. */
3958 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
3959}
1edf84e7
GV
3960
3961DWORD
3962w32_msg_worker (dw)
3963 DWORD dw;
3964{
3965 MSG msg;
3966 deferred_msg dummy_buf;
3967
3968 /* Ensure our message queue is created */
3969
3970 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
5ac45f98 3971
1edf84e7
GV
3972 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
3973 abort ();
3974
3975 memset (&dummy_buf, 0, sizeof (dummy_buf));
3976 dummy_buf.w32msg.msg.hwnd = NULL;
3977 dummy_buf.w32msg.msg.message = WM_NULL;
3978
3979 /* This is the inital message loop which should only exit when the
3980 application quits. */
3981 w32_msg_pump (&dummy_buf);
3982
3983 return 0;
5ac45f98
GV
3984}
3985
3ef68e6b
AI
3986static void
3987post_character_message (hwnd, msg, wParam, lParam, modifiers)
3988 HWND hwnd;
3989 UINT msg;
3990 WPARAM wParam;
3991 LPARAM lParam;
3992 DWORD modifiers;
3993
3994{
3995 W32Msg wmsg;
3996
3997 wmsg.dwModifiers = modifiers;
3998
3999 /* Detect quit_char and set quit-flag directly. Note that we
4000 still need to post a message to ensure the main thread will be
4001 woken up if blocked in sys_select(), but we do NOT want to post
4002 the quit_char message itself (because it will usually be as if
4003 the user had typed quit_char twice). Instead, we post a dummy
4004 message that has no particular effect. */
4005 {
4006 int c = wParam;
4007 if (isalpha (c) && wmsg.dwModifiers == ctrl_modifier)
4008 c = make_ctrl_char (c) & 0377;
7d081355
AI
4009 if (c == quit_char
4010 || (wmsg.dwModifiers == 0 &&
4011 XFASTINT (Vw32_quit_key) && wParam == XFASTINT (Vw32_quit_key)))
3ef68e6b
AI
4012 {
4013 Vquit_flag = Qt;
4014
4015 /* The choice of message is somewhat arbitrary, as long as
4016 the main thread handler just ignores it. */
4017 msg = WM_NULL;
4018
4019 /* Interrupt any blocking system calls. */
4020 signal_quit ();
4021
4022 /* As a safety precaution, forcibly complete any deferred
4023 messages. This is a kludge, but I don't see any particularly
4024 clean way to handle the situation where a deferred message is
4025 "dropped" in the lisp thread, and will thus never be
4026 completed, eg. by the user trying to activate the menubar
4027 when the lisp thread is busy, and then typing C-g when the
4028 menubar doesn't open promptly (with the result that the
4029 menubar never responds at all because the deferred
4030 WM_INITMENU message is never completed). Another problem
4031 situation is when the lisp thread calls SendMessage (to send
4032 a window manager command) when a message has been deferred;
4033 the lisp thread gets blocked indefinitely waiting for the
4034 deferred message to be completed, which itself is waiting for
4035 the lisp thread to respond.
4036
4037 Note that we don't want to block the input thread waiting for
4038 a reponse from the lisp thread (although that would at least
4039 solve the deadlock problem above), because we want to be able
4040 to receive C-g to interrupt the lisp thread. */
4041 cancel_all_deferred_msgs ();
4042 }
4043 }
4044
4045 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4046}
4047
ee78dc32
GV
4048/* Main window procedure */
4049
ee78dc32 4050LRESULT CALLBACK
fbd6baed 4051w32_wnd_proc (hwnd, msg, wParam, lParam)
ee78dc32
GV
4052 HWND hwnd;
4053 UINT msg;
4054 WPARAM wParam;
4055 LPARAM lParam;
4056{
4057 struct frame *f;
fbd6baed
GV
4058 struct w32_display_info *dpyinfo = &one_w32_display_info;
4059 W32Msg wmsg;
84fb1139 4060 int windows_translate;
576ba81c 4061 int key;
84fb1139 4062
a6085637
KH
4063 /* Note that it is okay to call x_window_to_frame, even though we are
4064 not running in the main lisp thread, because frame deletion
4065 requires the lisp thread to synchronize with this thread. Thus, if
4066 a frame struct is returned, it can be used without concern that the
4067 lisp thread might make it disappear while we are using it.
4068
4069 NB. Walking the frame list in this thread is safe (as long as
4070 writes of Lisp_Object slots are atomic, which they are on Windows).
4071 Although delete-frame can destructively modify the frame list while
4072 we are walking it, a garbage collection cannot occur until after
4073 delete-frame has synchronized with this thread.
4074
4075 It is also safe to use functions that make GDI calls, such as
fbd6baed 4076 w32_clear_rect, because these functions must obtain a DC handle
a6085637
KH
4077 from the frame struct using get_frame_dc which is thread-aware. */
4078
ee78dc32
GV
4079 switch (msg)
4080 {
4081 case WM_ERASEBKGND:
a6085637
KH
4082 f = x_window_to_frame (dpyinfo, hwnd);
4083 if (f)
4084 {
9badad41 4085 HDC hdc = get_frame_dc (f);
a6085637 4086 GetUpdateRect (hwnd, &wmsg.rect, FALSE);
9badad41
JR
4087 w32_clear_rect (f, hdc, &wmsg.rect);
4088 release_frame_dc (f, hdc);
ce6059da
AI
4089
4090#if defined (W32_DEBUG_DISPLAY)
4091 DebPrint (("WM_ERASEBKGND: erasing %d,%d-%d,%d\n",
4092 wmsg.rect.left, wmsg.rect.top, wmsg.rect.right,
4093 wmsg.rect.bottom));
4094#endif /* W32_DEBUG_DISPLAY */
a6085637 4095 }
5ac45f98
GV
4096 return 1;
4097 case WM_PALETTECHANGED:
4098 /* ignore our own changes */
4099 if ((HWND)wParam != hwnd)
4100 {
a6085637
KH
4101 f = x_window_to_frame (dpyinfo, hwnd);
4102 if (f)
4103 /* get_frame_dc will realize our palette and force all
4104 frames to be redrawn if needed. */
4105 release_frame_dc (f, get_frame_dc (f));
5ac45f98
GV
4106 }
4107 return 0;
ee78dc32 4108 case WM_PAINT:
ce6059da 4109 {
55dcfc15
AI
4110 PAINTSTRUCT paintStruct;
4111 RECT update_rect;
4112
4113 /* MSDN Docs say not to call BeginPaint if GetUpdateRect
4114 fails. Apparently this can happen under some
4115 circumstances. */
c0611964 4116 if (!w32_strict_painting || GetUpdateRect (hwnd, &update_rect, FALSE))
55dcfc15
AI
4117 {
4118 enter_crit ();
4119 BeginPaint (hwnd, &paintStruct);
4120
c0611964
AI
4121 if (w32_strict_painting)
4122 /* The rectangles returned by GetUpdateRect and BeginPaint
4123 do not always match. GetUpdateRect seems to be the
4124 more reliable of the two. */
4125 wmsg.rect = update_rect;
4126 else
4127 wmsg.rect = paintStruct.rcPaint;
55dcfc15
AI
4128
4129#if defined (W32_DEBUG_DISPLAY)
4130 DebPrint (("WM_PAINT: painting %d,%d-%d,%d\n", wmsg.rect.left,
4131 wmsg.rect.top, wmsg.rect.right, wmsg.rect.bottom));
4132 DebPrint (("WM_PAINT: update region is %d,%d-%d,%d\n",
4133 update_rect.left, update_rect.top,
4134 update_rect.right, update_rect.bottom));
4135#endif
4136 EndPaint (hwnd, &paintStruct);
4137 leave_crit ();
4138
4139 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4140
4141 return 0;
4142 }
c0611964
AI
4143
4144 /* If GetUpdateRect returns 0 (meaning there is no update
4145 region), assume the whole window needs to be repainted. */
4146 GetClientRect(hwnd, &wmsg.rect);
4147 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4148 return 0;
ee78dc32 4149 }
a1a80b40 4150
ccc2d29c
GV
4151 case WM_INPUTLANGCHANGE:
4152 /* Inform lisp thread of keyboard layout changes. */
4153 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4154
4155 /* Clear dead keys in the keyboard state; for simplicity only
4156 preserve modifier key states. */
4157 {
4158 int i;
4159 BYTE keystate[256];
4160
4161 GetKeyboardState (keystate);
4162 for (i = 0; i < 256; i++)
4163 if (1
4164 && i != VK_SHIFT
4165 && i != VK_LSHIFT
4166 && i != VK_RSHIFT
4167 && i != VK_CAPITAL
4168 && i != VK_NUMLOCK
4169 && i != VK_SCROLL
4170 && i != VK_CONTROL
4171 && i != VK_LCONTROL
4172 && i != VK_RCONTROL
4173 && i != VK_MENU
4174 && i != VK_LMENU
4175 && i != VK_RMENU
4176 && i != VK_LWIN
4177 && i != VK_RWIN)
4178 keystate[i] = 0;
4179 SetKeyboardState (keystate);
4180 }
4181 goto dflt;
4182
4183 case WM_HOTKEY:
4184 /* Synchronize hot keys with normal input. */
4185 PostMessage (hwnd, WM_KEYDOWN, HIWORD (lParam), 0);
4186 return (0);
4187
a1a80b40
GV
4188 case WM_KEYUP:
4189 case WM_SYSKEYUP:
4190 record_keyup (wParam, lParam);
4191 goto dflt;
4192
ee78dc32
GV
4193 case WM_KEYDOWN:
4194 case WM_SYSKEYDOWN:
ccc2d29c
GV
4195 /* Ignore keystrokes we fake ourself; see below. */
4196 if (dpyinfo->faked_key == wParam)
4197 {
4198 dpyinfo->faked_key = 0;
576ba81c
AI
4199 /* Make sure TranslateMessage sees them though (as long as
4200 they don't produce WM_CHAR messages). This ensures that
4201 indicator lights are toggled promptly on Windows 9x, for
4202 example. */
4203 if (lispy_function_keys[wParam] != 0)
4204 {
4205 windows_translate = 1;
4206 goto translate;
4207 }
4208 return 0;
ccc2d29c
GV
4209 }
4210
7830e24b
RS
4211 /* Synchronize modifiers with current keystroke. */
4212 sync_modifiers ();
a1a80b40 4213 record_keydown (wParam, lParam);
ccc2d29c 4214 wParam = map_keypad_keys (wParam, (lParam & 0x1000000L) != 0);
84fb1139
KH
4215
4216 windows_translate = 0;
ccc2d29c
GV
4217
4218 switch (wParam)
4219 {
4220 case VK_LWIN:
4221 if (NILP (Vw32_pass_lwindow_to_system))
4222 {
4223 /* Prevent system from acting on keyup (which opens the
4224 Start menu if no other key was pressed) by simulating a
4225 press of Space which we will ignore. */
4226 if (GetAsyncKeyState (wParam) & 1)
4227 {
adcc3809 4228 if (NUMBERP (Vw32_phantom_key_code))
576ba81c 4229 key = XUINT (Vw32_phantom_key_code) & 255;
adcc3809 4230 else
576ba81c
AI
4231 key = VK_SPACE;
4232 dpyinfo->faked_key = key;
4233 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
ccc2d29c
GV
4234 }
4235 }
4236 if (!NILP (Vw32_lwindow_modifier))
4237 return 0;
4238 break;
4239 case VK_RWIN:
4240 if (NILP (Vw32_pass_rwindow_to_system))
4241 {
4242 if (GetAsyncKeyState (wParam) & 1)
4243 {
adcc3809 4244 if (NUMBERP (Vw32_phantom_key_code))
576ba81c 4245 key = XUINT (Vw32_phantom_key_code) & 255;
adcc3809 4246 else
576ba81c
AI
4247 key = VK_SPACE;
4248 dpyinfo->faked_key = key;
4249 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
ccc2d29c
GV
4250 }
4251 }
4252 if (!NILP (Vw32_rwindow_modifier))
4253 return 0;
4254 break;
576ba81c 4255 case VK_APPS:
ccc2d29c
GV
4256 if (!NILP (Vw32_apps_modifier))
4257 return 0;
4258 break;
4259 case VK_MENU:
4260 if (NILP (Vw32_pass_alt_to_system))
adcc3809
GV
4261 /* Prevent DefWindowProc from activating the menu bar if an
4262 Alt key is pressed and released by itself. */
ccc2d29c 4263 return 0;
84fb1139 4264 windows_translate = 1;
ccc2d29c
GV
4265 break;
4266 case VK_CAPITAL:
4267 /* Decide whether to treat as modifier or function key. */
4268 if (NILP (Vw32_enable_caps_lock))
4269 goto disable_lock_key;
adcc3809
GV
4270 windows_translate = 1;
4271 break;
ccc2d29c
GV
4272 case VK_NUMLOCK:
4273 /* Decide whether to treat as modifier or function key. */
4274 if (NILP (Vw32_enable_num_lock))
4275 goto disable_lock_key;
adcc3809
GV
4276 windows_translate = 1;
4277 break;
ccc2d29c
GV
4278 case VK_SCROLL:
4279 /* Decide whether to treat as modifier or function key. */
4280 if (NILP (Vw32_scroll_lock_modifier))
4281 goto disable_lock_key;
adcc3809
GV
4282 windows_translate = 1;
4283 break;
ccc2d29c 4284 disable_lock_key:
adcc3809
GV
4285 /* Ensure the appropriate lock key state (and indicator light)
4286 remains in the same state. We do this by faking another
4287 press of the relevant key. Apparently, this really is the
4288 only way to toggle the state of the indicator lights. */
4289 dpyinfo->faked_key = wParam;
4290 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
4291 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
4292 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
4293 KEYEVENTF_EXTENDEDKEY | 0, 0);
4294 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
4295 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
4296 /* Ensure indicator lights are updated promptly on Windows 9x
4297 (TranslateMessage apparently does this), after forwarding
4298 input event. */
4299 post_character_message (hwnd, msg, wParam, lParam,
4300 w32_get_key_modifiers (wParam, lParam));
4301 windows_translate = 1;
ccc2d29c
GV
4302 break;
4303 case VK_CONTROL:
4304 case VK_SHIFT:
4305 case VK_PROCESSKEY: /* Generated by IME. */
4306 windows_translate = 1;
4307 break;
adcc3809
GV
4308 case VK_CANCEL:
4309 /* Windows maps Ctrl-Pause (aka Ctrl-Break) into VK_CANCEL,
4310 which is confusing for purposes of key binding; convert
4311 VK_CANCEL events into VK_PAUSE events. */
4312 wParam = VK_PAUSE;
4313 break;
4314 case VK_PAUSE:
4315 /* Windows maps Ctrl-NumLock into VK_PAUSE, which is confusing
4316 for purposes of key binding; convert these back into
4317 VK_NUMLOCK events, at least when we want to see NumLock key
4318 presses. (Note that there is never any possibility that
4319 VK_PAUSE with Ctrl really is C-Pause as per above.) */
4320 if (NILP (Vw32_enable_num_lock) && modifier_set (VK_CONTROL))
4321 wParam = VK_NUMLOCK;
4322 break;
ccc2d29c
GV
4323 default:
4324 /* If not defined as a function key, change it to a WM_CHAR message. */
4325 if (lispy_function_keys[wParam] == 0)
4326 {
adcc3809
GV
4327 DWORD modifiers = construct_console_modifiers ();
4328
ccc2d29c
GV
4329 if (!NILP (Vw32_recognize_altgr)
4330 && modifier_set (VK_LCONTROL) && modifier_set (VK_RMENU))
4331 {
4332 /* Always let TranslateMessage handle AltGr key chords;
4333 for some reason, ToAscii doesn't always process AltGr
4334 chords correctly. */
4335 windows_translate = 1;
4336 }
adcc3809 4337 else if ((modifiers & (~SHIFT_PRESSED & ~CAPSLOCK_ON)) != 0)
ccc2d29c 4338 {
adcc3809
GV
4339 /* Handle key chords including any modifiers other
4340 than shift directly, in order to preserve as much
4341 modifier information as possible. */
ccc2d29c
GV
4342 if ('A' <= wParam && wParam <= 'Z')
4343 {
4344 /* Don't translate modified alphabetic keystrokes,
4345 so the user doesn't need to constantly switch
4346 layout to type control or meta keystrokes when
4347 the normal layout translates alphabetic
4348 characters to non-ascii characters. */
4349 if (!modifier_set (VK_SHIFT))
4350 wParam += ('a' - 'A');
4351 msg = WM_CHAR;
4352 }
4353 else
4354 {
4355 /* Try to handle other keystrokes by determining the
4356 base character (ie. translating the base key plus
4357 shift modifier). */
4358 int add;
4359 int isdead = 0;
4360 KEY_EVENT_RECORD key;
4361
4362 key.bKeyDown = TRUE;
4363 key.wRepeatCount = 1;
4364 key.wVirtualKeyCode = wParam;
4365 key.wVirtualScanCode = (lParam & 0xFF0000) >> 16;
4366 key.uChar.AsciiChar = 0;
adcc3809 4367 key.dwControlKeyState = modifiers;
ccc2d29c
GV
4368
4369 add = w32_kbd_patch_key (&key);
4370 /* 0 means an unrecognised keycode, negative means
4371 dead key. Ignore both. */
4372 while (--add >= 0)
4373 {
4374 /* Forward asciified character sequence. */
4375 post_character_message
4376 (hwnd, WM_CHAR, key.uChar.AsciiChar, lParam,
4377 w32_get_key_modifiers (wParam, lParam));
4378 w32_kbd_patch_key (&key);
4379 }
4380 return 0;
4381 }
4382 }
4383 else
4384 {
4385 /* Let TranslateMessage handle everything else. */
4386 windows_translate = 1;
4387 }
4388 }
4389 }
a1a80b40 4390
adcc3809 4391 translate:
84fb1139
KH
4392 if (windows_translate)
4393 {
e9e23e23 4394 MSG windows_msg = { hwnd, msg, wParam, lParam, 0, {0,0} };
84fb1139 4395
e9e23e23
GV
4396 windows_msg.time = GetMessageTime ();
4397 TranslateMessage (&windows_msg);
84fb1139
KH
4398 goto dflt;
4399 }
4400
ee78dc32
GV
4401 /* Fall through */
4402
4403 case WM_SYSCHAR:
4404 case WM_CHAR:
ccc2d29c
GV
4405 post_character_message (hwnd, msg, wParam, lParam,
4406 w32_get_key_modifiers (wParam, lParam));
ee78dc32 4407 break;
da36a4d6 4408
5ac45f98
GV
4409 /* Simulate middle mouse button events when left and right buttons
4410 are used together, but only if user has two button mouse. */
ee78dc32 4411 case WM_LBUTTONDOWN:
5ac45f98 4412 case WM_RBUTTONDOWN:
7ce9aaca 4413 if (XINT (Vw32_num_mouse_buttons) > 2)
5ac45f98
GV
4414 goto handle_plain_button;
4415
4416 {
4417 int this = (msg == WM_LBUTTONDOWN) ? LMOUSE : RMOUSE;
4418 int other = (msg == WM_LBUTTONDOWN) ? RMOUSE : LMOUSE;
4419
3cb20f4a
RS
4420 if (button_state & this)
4421 return 0;
5ac45f98
GV
4422
4423 if (button_state == 0)
4424 SetCapture (hwnd);
4425
4426 button_state |= this;
4427
4428 if (button_state & other)
4429 {
84fb1139 4430 if (mouse_button_timer)
5ac45f98 4431 {
84fb1139
KH
4432 KillTimer (hwnd, mouse_button_timer);
4433 mouse_button_timer = 0;
5ac45f98
GV
4434
4435 /* Generate middle mouse event instead. */
4436 msg = WM_MBUTTONDOWN;
4437 button_state |= MMOUSE;
4438 }
4439 else if (button_state & MMOUSE)
4440 {
4441 /* Ignore button event if we've already generated a
4442 middle mouse down event. This happens if the
4443 user releases and press one of the two buttons
4444 after we've faked a middle mouse event. */
4445 return 0;
4446 }
4447 else
4448 {
4449 /* Flush out saved message. */
84fb1139 4450 post_msg (&saved_mouse_button_msg);
5ac45f98 4451 }
fbd6baed 4452 wmsg.dwModifiers = w32_get_modifiers ();
5ac45f98
GV
4453 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4454
4455 /* Clear message buffer. */
84fb1139 4456 saved_mouse_button_msg.msg.hwnd = 0;
5ac45f98
GV
4457 }
4458 else
4459 {
4460 /* Hold onto message for now. */
84fb1139 4461 mouse_button_timer =
adcc3809
GV
4462 SetTimer (hwnd, MOUSE_BUTTON_ID,
4463 XINT (Vw32_mouse_button_tolerance), NULL);
84fb1139
KH
4464 saved_mouse_button_msg.msg.hwnd = hwnd;
4465 saved_mouse_button_msg.msg.message = msg;
4466 saved_mouse_button_msg.msg.wParam = wParam;
4467 saved_mouse_button_msg.msg.lParam = lParam;
4468 saved_mouse_button_msg.msg.time = GetMessageTime ();
fbd6baed 4469 saved_mouse_button_msg.dwModifiers = w32_get_modifiers ();
5ac45f98
GV
4470 }
4471 }
4472 return 0;
4473
ee78dc32 4474 case WM_LBUTTONUP:
5ac45f98 4475 case WM_RBUTTONUP:
7ce9aaca 4476 if (XINT (Vw32_num_mouse_buttons) > 2)
5ac45f98
GV
4477 goto handle_plain_button;
4478
4479 {
4480 int this = (msg == WM_LBUTTONUP) ? LMOUSE : RMOUSE;
4481 int other = (msg == WM_LBUTTONUP) ? RMOUSE : LMOUSE;
4482
3cb20f4a
RS
4483 if ((button_state & this) == 0)
4484 return 0;
5ac45f98
GV
4485
4486 button_state &= ~this;
4487
4488 if (button_state & MMOUSE)
4489 {
4490 /* Only generate event when second button is released. */
4491 if ((button_state & other) == 0)
4492 {
4493 msg = WM_MBUTTONUP;
4494 button_state &= ~MMOUSE;
4495
4496 if (button_state) abort ();
4497 }
4498 else
4499 return 0;
4500 }
4501 else
4502 {
4503 /* Flush out saved message if necessary. */
84fb1139 4504 if (saved_mouse_button_msg.msg.hwnd)
5ac45f98 4505 {
84fb1139 4506 post_msg (&saved_mouse_button_msg);
5ac45f98
GV
4507 }
4508 }
fbd6baed 4509 wmsg.dwModifiers = w32_get_modifiers ();
5ac45f98
GV
4510 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4511
4512 /* Always clear message buffer and cancel timer. */
84fb1139
KH
4513 saved_mouse_button_msg.msg.hwnd = 0;
4514 KillTimer (hwnd, mouse_button_timer);
4515 mouse_button_timer = 0;
5ac45f98
GV
4516
4517 if (button_state == 0)
4518 ReleaseCapture ();
4519 }
4520 return 0;
4521
ee78dc32
GV
4522 case WM_MBUTTONDOWN:
4523 case WM_MBUTTONUP:
5ac45f98 4524 handle_plain_button:
ee78dc32
GV
4525 {
4526 BOOL up;
1edf84e7 4527 int button;
ee78dc32 4528
1edf84e7 4529 if (parse_button (msg, &button, &up))
ee78dc32
GV
4530 {
4531 if (up) ReleaseCapture ();
4532 else SetCapture (hwnd);
1edf84e7
GV
4533 button = (button == 0) ? LMOUSE :
4534 ((button == 1) ? MMOUSE : RMOUSE);
4535 if (up)
4536 button_state &= ~button;
4537 else
4538 button_state |= button;
ee78dc32
GV
4539 }
4540 }
4541
fbd6baed 4542 wmsg.dwModifiers = w32_get_modifiers ();
ee78dc32 4543 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
5ac45f98
GV
4544 return 0;
4545
84fb1139 4546 case WM_VSCROLL:
5ac45f98 4547 case WM_MOUSEMOVE:
fbd6baed 4548 if (XINT (Vw32_mouse_move_interval) <= 0
84fb1139
KH
4549 || (msg == WM_MOUSEMOVE && button_state == 0))
4550 {
fbd6baed 4551 wmsg.dwModifiers = w32_get_modifiers ();
84fb1139
KH
4552 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4553 return 0;
4554 }
4555
4556 /* Hang onto mouse move and scroll messages for a bit, to avoid
4557 sending such events to Emacs faster than it can process them.
4558 If we get more events before the timer from the first message
4559 expires, we just replace the first message. */
4560
4561 if (saved_mouse_move_msg.msg.hwnd == 0)
4562 mouse_move_timer =
adcc3809
GV
4563 SetTimer (hwnd, MOUSE_MOVE_ID,
4564 XINT (Vw32_mouse_move_interval), NULL);
84fb1139
KH
4565
4566 /* Hold onto message for now. */
4567 saved_mouse_move_msg.msg.hwnd = hwnd;
4568 saved_mouse_move_msg.msg.message = msg;
4569 saved_mouse_move_msg.msg.wParam = wParam;
4570 saved_mouse_move_msg.msg.lParam = lParam;
4571 saved_mouse_move_msg.msg.time = GetMessageTime ();
fbd6baed 4572 saved_mouse_move_msg.dwModifiers = w32_get_modifiers ();
84fb1139
KH
4573
4574 return 0;
4575
1edf84e7
GV
4576 case WM_MOUSEWHEEL:
4577 wmsg.dwModifiers = w32_get_modifiers ();
4578 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4579 return 0;
4580
cb9e33d4
RS
4581 case WM_DROPFILES:
4582 wmsg.dwModifiers = w32_get_modifiers ();
4583 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4584 return 0;
4585
84fb1139
KH
4586 case WM_TIMER:
4587 /* Flush out saved messages if necessary. */
4588 if (wParam == mouse_button_timer)
5ac45f98 4589 {
84fb1139
KH
4590 if (saved_mouse_button_msg.msg.hwnd)
4591 {
4592 post_msg (&saved_mouse_button_msg);
4593 saved_mouse_button_msg.msg.hwnd = 0;
4594 }
4595 KillTimer (hwnd, mouse_button_timer);
4596 mouse_button_timer = 0;
4597 }
4598 else if (wParam == mouse_move_timer)
4599 {
4600 if (saved_mouse_move_msg.msg.hwnd)
4601 {
4602 post_msg (&saved_mouse_move_msg);
4603 saved_mouse_move_msg.msg.hwnd = 0;
4604 }
4605 KillTimer (hwnd, mouse_move_timer);
4606 mouse_move_timer = 0;
5ac45f98 4607 }
5ac45f98 4608 return 0;
84fb1139
KH
4609
4610 case WM_NCACTIVATE:
4611 /* Windows doesn't send us focus messages when putting up and
e9e23e23 4612 taking down a system popup dialog as for Ctrl-Alt-Del on Windows 95.
84fb1139
KH
4613 The only indication we get that something happened is receiving
4614 this message afterwards. So this is a good time to reset our
4615 keyboard modifiers' state. */
4616 reset_modifiers ();
4617 goto dflt;
da36a4d6 4618
1edf84e7 4619 case WM_INITMENU:
487163ac
AI
4620 button_state = 0;
4621 ReleaseCapture ();
1edf84e7
GV
4622 /* We must ensure menu bar is fully constructed and up to date
4623 before allowing user interaction with it. To achieve this
4624 we send this message to the lisp thread and wait for a
4625 reply (whose value is not actually needed) to indicate that
4626 the menu bar is now ready for use, so we can now return.
4627
4628 To remain responsive in the meantime, we enter a nested message
4629 loop that can process all other messages.
4630
4631 However, we skip all this if the message results from calling
4632 TrackPopupMenu - in fact, we must NOT attempt to send the lisp
4633 thread a message because it is blocked on us at this point. We
4634 set menubar_active before calling TrackPopupMenu to indicate
4635 this (there is no possibility of confusion with real menubar
4636 being active). */
4637
4638 f = x_window_to_frame (dpyinfo, hwnd);
4639 if (f
4640 && (f->output_data.w32->menubar_active
4641 /* We can receive this message even in the absence of a
4642 menubar (ie. when the system menu is activated) - in this
4643 case we do NOT want to forward the message, otherwise it
4644 will cause the menubar to suddenly appear when the user
4645 had requested it to be turned off! */
4646 || f->output_data.w32->menubar_widget == NULL))
4647 return 0;
4648
4649 {
4650 deferred_msg msg_buf;
4651
4652 /* Detect if message has already been deferred; in this case
4653 we cannot return any sensible value to ignore this. */
4654 if (find_deferred_msg (hwnd, msg) != NULL)
4655 abort ();
4656
4657 return send_deferred_msg (&msg_buf, hwnd, msg, wParam, lParam);
4658 }
4659
4660 case WM_EXITMENULOOP:
4661 f = x_window_to_frame (dpyinfo, hwnd);
4662
4663 /* Indicate that menubar can be modified again. */
4664 if (f)
4665 f->output_data.w32->menubar_active = 0;
4666 goto dflt;
4667
126f2e35
JR
4668 case WM_MENUSELECT:
4669 wmsg.dwModifiers = w32_get_modifiers ();
4670 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4671 return 0;
4672
87996783
GV
4673 case WM_MEASUREITEM:
4674 f = x_window_to_frame (dpyinfo, hwnd);
4675 if (f)
4676 {
4677 MEASUREITEMSTRUCT * pMis = (MEASUREITEMSTRUCT *) lParam;
4678
4679 if (pMis->CtlType == ODT_MENU)
4680 {
4681 /* Work out dimensions for popup menu titles. */
4682 char * title = (char *) pMis->itemData;
4683 HDC hdc = GetDC (hwnd);
4684 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
4685 LOGFONT menu_logfont;
4686 HFONT old_font;
4687 SIZE size;
4688
4689 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
4690 menu_logfont.lfWeight = FW_BOLD;
4691 menu_font = CreateFontIndirect (&menu_logfont);
4692 old_font = SelectObject (hdc, menu_font);
4693
dfff8a69
JR
4694 pMis->itemHeight = GetSystemMetrics (SM_CYMENUSIZE);
4695 if (title)
4696 {
4697 GetTextExtentPoint32 (hdc, title, strlen (title), &size);
4698 pMis->itemWidth = size.cx;
4699 if (pMis->itemHeight < size.cy)
4700 pMis->itemHeight = size.cy;
4701 }
4702 else
4703 pMis->itemWidth = 0;
87996783
GV
4704
4705 SelectObject (hdc, old_font);
4706 DeleteObject (menu_font);
4707 ReleaseDC (hwnd, hdc);
4708 return TRUE;
4709 }
4710 }
4711 return 0;
4712
4713 case WM_DRAWITEM:
4714 f = x_window_to_frame (dpyinfo, hwnd);
4715 if (f)
4716 {
4717 DRAWITEMSTRUCT * pDis = (DRAWITEMSTRUCT *) lParam;
4718
4719 if (pDis->CtlType == ODT_MENU)
4720 {
4721 /* Draw popup menu title. */
4722 char * title = (char *) pDis->itemData;
212da13b
JR
4723 if (title)
4724 {
4725 HDC hdc = pDis->hDC;
4726 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
4727 LOGFONT menu_logfont;
4728 HFONT old_font;
4729
4730 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
4731 menu_logfont.lfWeight = FW_BOLD;
4732 menu_font = CreateFontIndirect (&menu_logfont);
4733 old_font = SelectObject (hdc, menu_font);
4734
4735 /* Always draw title as if not selected. */
4736 ExtTextOut (hdc,
4737 pDis->rcItem.left
4738 + GetSystemMetrics (SM_CXMENUCHECK),
4739 pDis->rcItem.top,
4740 ETO_OPAQUE, &pDis->rcItem,
4741 title, strlen (title), NULL);
4742
4743 SelectObject (hdc, old_font);
4744 DeleteObject (menu_font);
4745 }
87996783
GV
4746 return TRUE;
4747 }
4748 }
4749 return 0;
4750
1edf84e7
GV
4751#if 0
4752 /* Still not right - can't distinguish between clicks in the
4753 client area of the frame from clicks forwarded from the scroll
4754 bars - may have to hook WM_NCHITTEST to remember the mouse
4755 position and then check if it is in the client area ourselves. */
4756 case WM_MOUSEACTIVATE:
4757 /* Discard the mouse click that activates a frame, allowing the
4758 user to click anywhere without changing point (or worse!).
4759 Don't eat mouse clicks on scrollbars though!! */
4760 if (LOWORD (lParam) == HTCLIENT )
4761 return MA_ACTIVATEANDEAT;
4762 goto dflt;
4763#endif
4764
1edf84e7 4765 case WM_ACTIVATEAPP:
ccc2d29c 4766 case WM_ACTIVATE:
1edf84e7
GV
4767 case WM_WINDOWPOSCHANGED:
4768 case WM_SHOWWINDOW:
4769 /* Inform lisp thread that a frame might have just been obscured
4770 or exposed, so should recheck visibility of all frames. */
4771 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4772 goto dflt;
4773
da36a4d6 4774 case WM_SETFOCUS:
adcc3809
GV
4775 dpyinfo->faked_key = 0;
4776 reset_modifiers ();
ccc2d29c
GV
4777 register_hot_keys (hwnd);
4778 goto command;
8681157a 4779 case WM_KILLFOCUS:
ccc2d29c 4780 unregister_hot_keys (hwnd);
487163ac
AI
4781 button_state = 0;
4782 ReleaseCapture ();
ee78dc32
GV
4783 case WM_MOVE:
4784 case WM_SIZE:
ee78dc32 4785 case WM_COMMAND:
ccc2d29c 4786 command:
fbd6baed 4787 wmsg.dwModifiers = w32_get_modifiers ();
ee78dc32
GV
4788 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4789 goto dflt;
8847d890
RS
4790
4791 case WM_CLOSE:
fbd6baed 4792 wmsg.dwModifiers = w32_get_modifiers ();
8847d890
RS
4793 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4794 return 0;
4795
ee78dc32
GV
4796 case WM_WINDOWPOSCHANGING:
4797 {
4798 WINDOWPLACEMENT wp;
4799 LPWINDOWPOS lppos = (WINDOWPOS *) lParam;
1edf84e7
GV
4800
4801 wp.length = sizeof (WINDOWPLACEMENT);
ee78dc32
GV
4802 GetWindowPlacement (hwnd, &wp);
4803
1edf84e7 4804 if (wp.showCmd != SW_SHOWMINIMIZED && (lppos->flags & SWP_NOSIZE) == 0)
ee78dc32
GV
4805 {
4806 RECT rect;
4807 int wdiff;
4808 int hdiff;
1edf84e7
GV
4809 DWORD font_width;
4810 DWORD line_height;
4811 DWORD internal_border;
4812 DWORD scrollbar_extra;
ee78dc32
GV
4813 RECT wr;
4814
5ac45f98 4815 wp.length = sizeof(wp);
ee78dc32
GV
4816 GetWindowRect (hwnd, &wr);
4817
3c190163 4818 enter_crit ();
ee78dc32 4819
1edf84e7
GV
4820 font_width = GetWindowLong (hwnd, WND_FONTWIDTH_INDEX);
4821 line_height = GetWindowLong (hwnd, WND_LINEHEIGHT_INDEX);
4822 internal_border = GetWindowLong (hwnd, WND_BORDER_INDEX);
4823 scrollbar_extra = GetWindowLong (hwnd, WND_SCROLLBAR_INDEX);
ee78dc32 4824
3c190163 4825 leave_crit ();
ee78dc32
GV
4826
4827 memset (&rect, 0, sizeof (rect));
4828 AdjustWindowRect (&rect, GetWindowLong (hwnd, GWL_STYLE),
4829 GetMenu (hwnd) != NULL);
4830
1edf84e7
GV
4831 /* Force width and height of client area to be exact
4832 multiples of the character cell dimensions. */
4833 wdiff = (lppos->cx - (rect.right - rect.left)
4834 - 2 * internal_border - scrollbar_extra)
4835 % font_width;
4836 hdiff = (lppos->cy - (rect.bottom - rect.top)
4837 - 2 * internal_border)
4838 % line_height;
ee78dc32
GV
4839
4840 if (wdiff || hdiff)
4841 {
4842 /* For right/bottom sizing we can just fix the sizes.
4843 However for top/left sizing we will need to fix the X
4844 and Y positions as well. */
4845
4846 lppos->cx -= wdiff;
4847 lppos->cy -= hdiff;
4848
4849 if (wp.showCmd != SW_SHOWMAXIMIZED
1edf84e7 4850 && (lppos->flags & SWP_NOMOVE) == 0)
ee78dc32
GV
4851 {
4852 if (lppos->x != wr.left || lppos->y != wr.top)
4853 {
4854 lppos->x += wdiff;
4855 lppos->y += hdiff;
4856 }
4857 else
4858 {
4859 lppos->flags |= SWP_NOMOVE;
4860 }
4861 }
4862
1edf84e7 4863 return 0;
ee78dc32
GV
4864 }
4865 }
4866 }
ee78dc32
GV
4867
4868 goto dflt;
1edf84e7 4869
b1f918f8
GV
4870 case WM_GETMINMAXINFO:
4871 /* Hack to correct bug that allows Emacs frames to be resized
4872 below the Minimum Tracking Size. */
4873 ((LPMINMAXINFO) lParam)->ptMinTrackSize.y++;
4874 return 0;
4875
1edf84e7
GV
4876 case WM_EMACS_CREATESCROLLBAR:
4877 return (LRESULT) w32_createscrollbar ((struct frame *) wParam,
4878 (struct scroll_bar *) lParam);
4879
5ac45f98 4880 case WM_EMACS_SHOWWINDOW:
1edf84e7
GV
4881 return ShowWindow ((HWND) wParam, (WPARAM) lParam);
4882
dfdb4047 4883 case WM_EMACS_SETFOREGROUND:
ce6059da
AI
4884 {
4885 HWND foreground_window;
4886 DWORD foreground_thread, retval;
4887
4888 /* On NT 5.0, and apparently Windows 98, it is necessary to
4889 attach to the thread that currently has focus in order to
4890 pull the focus away from it. */
4891 foreground_window = GetForegroundWindow ();
4892 foreground_thread = GetWindowThreadProcessId (foreground_window, NULL);
4893 if (!foreground_window
4894 || foreground_thread == GetCurrentThreadId ()
4895 || !AttachThreadInput (GetCurrentThreadId (),
4896 foreground_thread, TRUE))
4897 foreground_thread = 0;
4898
4899 retval = SetForegroundWindow ((HWND) wParam);
4900
4901 /* Detach from the previous foreground thread. */
4902 if (foreground_thread)
4903 AttachThreadInput (GetCurrentThreadId (),
4904 foreground_thread, FALSE);
4905
4906 return retval;
4907 }
dfdb4047 4908
5ac45f98
GV
4909 case WM_EMACS_SETWINDOWPOS:
4910 {
1edf84e7
GV
4911 WINDOWPOS * pos = (WINDOWPOS *) wParam;
4912 return SetWindowPos (hwnd, pos->hwndInsertAfter,
5ac45f98
GV
4913 pos->x, pos->y, pos->cx, pos->cy, pos->flags);
4914 }
1edf84e7 4915
ee78dc32 4916 case WM_EMACS_DESTROYWINDOW:
cb9e33d4 4917 DragAcceptFiles ((HWND) wParam, FALSE);
1edf84e7
GV
4918 return DestroyWindow ((HWND) wParam);
4919
4920 case WM_EMACS_TRACKPOPUPMENU:
4921 {
4922 UINT flags;
4923 POINT *pos;
4924 int retval;
4925 pos = (POINT *)lParam;
4926 flags = TPM_CENTERALIGN;
4927 if (button_state & LMOUSE)
4928 flags |= TPM_LEFTBUTTON;
4929 else if (button_state & RMOUSE)
4930 flags |= TPM_RIGHTBUTTON;
4931
87996783
GV
4932 /* Remember we did a SetCapture on the initial mouse down event,
4933 so for safety, we make sure the capture is cancelled now. */
4934 ReleaseCapture ();
490822ff 4935 button_state = 0;
87996783 4936
1edf84e7
GV
4937 /* Use menubar_active to indicate that WM_INITMENU is from
4938 TrackPopupMenu below, and should be ignored. */
4939 f = x_window_to_frame (dpyinfo, hwnd);
4940 if (f)
4941 f->output_data.w32->menubar_active = 1;
4942
4943 if (TrackPopupMenu ((HMENU)wParam, flags, pos->x, pos->y,
4944 0, hwnd, NULL))
4945 {
4946 MSG amsg;
4947 /* Eat any mouse messages during popupmenu */
4948 while (PeekMessage (&amsg, hwnd, WM_MOUSEFIRST, WM_MOUSELAST,
4949 PM_REMOVE));
4950 /* Get the menu selection, if any */
4951 if (PeekMessage (&amsg, hwnd, WM_COMMAND, WM_COMMAND, PM_REMOVE))
4952 {
4953 retval = LOWORD (amsg.wParam);
4954 }
4955 else
4956 {
4957 retval = 0;
4958 }
1edf84e7
GV
4959 }
4960 else
4961 {
4962 retval = -1;
4963 }
4964
4965 return retval;
4966 }
4967
ee78dc32 4968 default:
93fbe8b7
GV
4969 /* Check for messages registered at runtime. */
4970 if (msg == msh_mousewheel)
4971 {
4972 wmsg.dwModifiers = w32_get_modifiers ();
4973 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4974 return 0;
4975 }
4976
ee78dc32
GV
4977 dflt:
4978 return DefWindowProc (hwnd, msg, wParam, lParam);
4979 }
4980
1edf84e7
GV
4981
4982 /* The most common default return code for handled messages is 0. */
4983 return 0;
ee78dc32
GV
4984}
4985
4986void
4987my_create_window (f)
4988 struct frame * f;
4989{
4990 MSG msg;
4991
1edf84e7
GV
4992 if (!PostThreadMessage (dwWindowsThreadId, WM_EMACS_CREATEWINDOW, (WPARAM)f, 0))
4993 abort ();
ee78dc32
GV
4994 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
4995}
4996
fbd6baed 4997/* Create and set up the w32 window for frame F. */
ee78dc32
GV
4998
4999static void
fbd6baed 5000w32_window (f, window_prompting, minibuffer_only)
ee78dc32
GV
5001 struct frame *f;
5002 long window_prompting;
5003 int minibuffer_only;
5004{
5005 BLOCK_INPUT;
5006
5007 /* Use the resource name as the top-level window name
5008 for looking up resources. Make a non-Lisp copy
5009 for the window manager, so GC relocation won't bother it.
5010
5011 Elsewhere we specify the window name for the window manager. */
5012
5013 {
5014 char *str = (char *) XSTRING (Vx_resource_name)->data;
5015 f->namebuf = (char *) xmalloc (strlen (str) + 1);
5016 strcpy (f->namebuf, str);
5017 }
5018
5019 my_create_window (f);
5020
5021 validate_x_resource_name ();
5022
5023 /* x_set_name normally ignores requests to set the name if the
5024 requested name is the same as the current name. This is the one
5025 place where that assumption isn't correct; f->name is set, but
5026 the server hasn't been told. */
5027 {
5028 Lisp_Object name;
5029 int explicit = f->explicit_name;
5030
5031 f->explicit_name = 0;
5032 name = f->name;
5033 f->name = Qnil;
5034 x_set_name (f, name, explicit);
5035 }
5036
5037 UNBLOCK_INPUT;
5038
5039 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
5040 initialize_frame_menubar (f);
5041
fbd6baed 5042 if (FRAME_W32_WINDOW (f) == 0)
ee78dc32
GV
5043 error ("Unable to create window");
5044}
5045
5046/* Handle the icon stuff for this window. Perhaps later we might
5047 want an x_set_icon_position which can be called interactively as
5048 well. */
5049
5050static void
5051x_icon (f, parms)
5052 struct frame *f;
5053 Lisp_Object parms;
5054{
5055 Lisp_Object icon_x, icon_y;
5056
e9e23e23 5057 /* Set the position of the icon. Note that Windows 95 groups all
ee78dc32 5058 icons in the tray. */
6fc2811b
JR
5059 icon_x = w32_get_arg (parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
5060 icon_y = w32_get_arg (parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
ee78dc32
GV
5061 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
5062 {
5063 CHECK_NUMBER (icon_x, 0);
5064 CHECK_NUMBER (icon_y, 0);
5065 }
5066 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
5067 error ("Both left and top icon corners of icon must be specified");
5068
5069 BLOCK_INPUT;
5070
5071 if (! EQ (icon_x, Qunbound))
5072 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
5073
1edf84e7
GV
5074#if 0 /* TODO */
5075 /* Start up iconic or window? */
5076 x_wm_set_window_state
6fc2811b 5077 (f, (EQ (w32_get_arg (parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL), Qicon)
1edf84e7
GV
5078 ? IconicState
5079 : NormalState));
5080
5081 x_text_icon (f, (char *) XSTRING ((!NILP (f->icon_name)
5082 ? f->icon_name
5083 : f->name))->data);
5084#endif
5085
ee78dc32
GV
5086 UNBLOCK_INPUT;
5087}
5088
6fc2811b
JR
5089
5090static void
5091x_make_gc (f)
5092 struct frame *f;
5093{
5094 XGCValues gc_values;
5095
5096 BLOCK_INPUT;
5097
5098 /* Create the GC's of this frame.
5099 Note that many default values are used. */
5100
5101 /* Normal video */
5102 gc_values.font = f->output_data.w32->font;
5103
5104 /* Cursor has cursor-color background, background-color foreground. */
5105 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
5106 gc_values.background = f->output_data.w32->cursor_pixel;
5107 f->output_data.w32->cursor_gc
5108 = XCreateGC (NULL, FRAME_W32_WINDOW (f),
5109 (GCFont | GCForeground | GCBackground),
5110 &gc_values);
5111
5112 /* Reliefs. */
5113 f->output_data.w32->white_relief.gc = 0;
5114 f->output_data.w32->black_relief.gc = 0;
5115
5116 UNBLOCK_INPUT;
5117}
5118
5119
937e601e
AI
5120/* Handler for signals raised during x_create_frame and
5121 x_create_top_frame. FRAME is the frame which is partially
5122 constructed. */
5123
5124static Lisp_Object
5125unwind_create_frame (frame)
5126 Lisp_Object frame;
5127{
5128 struct frame *f = XFRAME (frame);
5129
5130 /* If frame is ``official'', nothing to do. */
5131 if (!CONSP (Vframe_list) || !EQ (XCAR (Vframe_list), frame))
5132 {
5133#ifdef GLYPH_DEBUG
5134 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
5135#endif
5136
5137 x_free_frame_resources (f);
5138
5139 /* Check that reference counts are indeed correct. */
5140 xassert (dpyinfo->reference_count == dpyinfo_refcount);
5141 xassert (dpyinfo->image_cache->refcount == image_cache_refcount);
c844a81a
GM
5142
5143 return Qt;
937e601e
AI
5144 }
5145
5146 return Qnil;
5147}
5148
5149
ee78dc32
GV
5150DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
5151 1, 1, 0,
5152 "Make a new window, which is called a \"frame\" in Emacs terms.\n\
5153Returns an Emacs frame object.\n\
5154ALIST is an alist of frame parameters.\n\
5155If the parameters specify that the frame should not have a minibuffer,\n\
5156and do not specify a specific minibuffer window to use,\n\
5157then `default-minibuffer-frame' must be a frame whose minibuffer can\n\
5158be shared by the new frame.\n\
5159\n\
5160This function is an internal primitive--use `make-frame' instead.")
5161 (parms)
5162 Lisp_Object parms;
5163{
5164 struct frame *f;
5165 Lisp_Object frame, tem;
5166 Lisp_Object name;
5167 int minibuffer_only = 0;
5168 long window_prompting = 0;
5169 int width, height;
5170 int count = specpdl_ptr - specpdl;
1edf84e7 5171 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
ee78dc32 5172 Lisp_Object display;
6fc2811b 5173 struct w32_display_info *dpyinfo = NULL;
ee78dc32
GV
5174 Lisp_Object parent;
5175 struct kboard *kb;
5176
4587b026
GV
5177 check_w32 ();
5178
ee78dc32
GV
5179 /* Use this general default value to start with
5180 until we know if this frame has a specified name. */
5181 Vx_resource_name = Vinvocation_name;
5182
6fc2811b 5183 display = w32_get_arg (parms, Qdisplay, 0, 0, RES_TYPE_STRING);
ee78dc32
GV
5184 if (EQ (display, Qunbound))
5185 display = Qnil;
5186 dpyinfo = check_x_display_info (display);
5187#ifdef MULTI_KBOARD
5188 kb = dpyinfo->kboard;
5189#else
5190 kb = &the_only_kboard;
5191#endif
5192
6fc2811b 5193 name = w32_get_arg (parms, Qname, "name", "Name", RES_TYPE_STRING);
ee78dc32
GV
5194 if (!STRINGP (name)
5195 && ! EQ (name, Qunbound)
5196 && ! NILP (name))
5197 error ("Invalid frame name--not a string or nil");
5198
5199 if (STRINGP (name))
5200 Vx_resource_name = name;
5201
5202 /* See if parent window is specified. */
6fc2811b 5203 parent = w32_get_arg (parms, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
ee78dc32
GV
5204 if (EQ (parent, Qunbound))
5205 parent = Qnil;
5206 if (! NILP (parent))
5207 CHECK_NUMBER (parent, 0);
5208
1edf84e7
GV
5209 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
5210 /* No need to protect DISPLAY because that's not used after passing
5211 it to make_frame_without_minibuffer. */
5212 frame = Qnil;
5213 GCPRO4 (parms, parent, name, frame);
1660f34a
JR
5214 tem = w32_get_arg (parms, Qminibuffer, "minibuffer", "Minibuffer",
5215 RES_TYPE_SYMBOL);
ee78dc32
GV
5216 if (EQ (tem, Qnone) || NILP (tem))
5217 f = make_frame_without_minibuffer (Qnil, kb, display);
5218 else if (EQ (tem, Qonly))
5219 {
5220 f = make_minibuffer_frame ();
5221 minibuffer_only = 1;
5222 }
5223 else if (WINDOWP (tem))
5224 f = make_frame_without_minibuffer (tem, kb, display);
5225 else
5226 f = make_frame (1);
5227
1edf84e7
GV
5228 XSETFRAME (frame, f);
5229
ee78dc32
GV
5230 /* Note that Windows does support scroll bars. */
5231 FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;
5ac45f98
GV
5232 /* By default, make scrollbars the system standard width. */
5233 f->scroll_bar_pixel_width = GetSystemMetrics (SM_CXVSCROLL);
ee78dc32 5234
fbd6baed 5235 f->output_method = output_w32;
6fc2811b
JR
5236 f->output_data.w32 =
5237 (struct w32_output *) xmalloc (sizeof (struct w32_output));
fbd6baed 5238 bzero (f->output_data.w32, sizeof (struct w32_output));
4587b026 5239 FRAME_FONTSET (f) = -1;
937e601e 5240 record_unwind_protect (unwind_create_frame, frame);
4587b026 5241
1edf84e7 5242 f->icon_name
6fc2811b 5243 = w32_get_arg (parms, Qicon_name, "iconName", "Title", RES_TYPE_STRING);
1edf84e7
GV
5244 if (! STRINGP (f->icon_name))
5245 f->icon_name = Qnil;
5246
fbd6baed 5247/* FRAME_W32_DISPLAY_INFO (f) = dpyinfo; */
ee78dc32
GV
5248#ifdef MULTI_KBOARD
5249 FRAME_KBOARD (f) = kb;
5250#endif
5251
5252 /* Specify the parent under which to make this window. */
5253
5254 if (!NILP (parent))
5255 {
1660f34a 5256 f->output_data.w32->parent_desc = (Window) XFASTINT (parent);
fbd6baed 5257 f->output_data.w32->explicit_parent = 1;
ee78dc32
GV
5258 }
5259 else
5260 {
fbd6baed
GV
5261 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
5262 f->output_data.w32->explicit_parent = 0;
ee78dc32
GV
5263 }
5264
ee78dc32
GV
5265 /* Set the name; the functions to which we pass f expect the name to
5266 be set. */
5267 if (EQ (name, Qunbound) || NILP (name))
5268 {
fbd6baed 5269 f->name = build_string (dpyinfo->w32_id_name);
ee78dc32
GV
5270 f->explicit_name = 0;
5271 }
5272 else
5273 {
5274 f->name = name;
5275 f->explicit_name = 1;
5276 /* use the frame's title when getting resources for this frame. */
5277 specbind (Qx_resource_name, name);
5278 }
5279
5280 /* Extract the window parameters from the supplied values
5281 that are needed to determine window geometry. */
5282 {
5283 Lisp_Object font;
5284
6fc2811b
JR
5285 font = w32_get_arg (parms, Qfont, "font", "Font", RES_TYPE_STRING);
5286
ee78dc32
GV
5287 BLOCK_INPUT;
5288 /* First, try whatever font the caller has specified. */
5289 if (STRINGP (font))
4587b026
GV
5290 {
5291 tem = Fquery_fontset (font, Qnil);
5292 if (STRINGP (tem))
5293 font = x_new_fontset (f, XSTRING (tem)->data);
5294 else
1075afa9 5295 font = x_new_font (f, XSTRING (font)->data);
4587b026 5296 }
ee78dc32
GV
5297 /* Try out a font which we hope has bold and italic variations. */
5298 if (!STRINGP (font))
e39649be 5299 font = x_new_font (f, "-*-Courier New-normal-r-*-*-*-100-*-*-c-*-iso8859-1");
ee78dc32 5300 if (! STRINGP (font))
6fc2811b 5301 font = x_new_font (f, "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1");
ee78dc32
GV
5302 /* If those didn't work, look for something which will at least work. */
5303 if (! STRINGP (font))
6fc2811b 5304 font = x_new_font (f, "-*-Fixedsys-normal-r-*-*-12-*-*-*-c-*-iso8859-1");
ee78dc32
GV
5305 UNBLOCK_INPUT;
5306 if (! STRINGP (font))
1edf84e7 5307 font = build_string ("Fixedsys");
ee78dc32
GV
5308
5309 x_default_parameter (f, parms, Qfont, font,
6fc2811b 5310 "font", "Font", RES_TYPE_STRING);
ee78dc32
GV
5311 }
5312
5313 x_default_parameter (f, parms, Qborder_width, make_number (2),
1660f34a 5314 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
ee78dc32
GV
5315 /* This defaults to 2 in order to match xterm. We recognize either
5316 internalBorderWidth or internalBorder (which is what xterm calls
5317 it). */
5318 if (NILP (Fassq (Qinternal_border_width, parms)))
5319 {
5320 Lisp_Object value;
5321
6fc2811b 5322 value = w32_get_arg (parms, Qinternal_border_width,
1660f34a 5323 "internalBorder", "InternalBorder", RES_TYPE_NUMBER);
ee78dc32
GV
5324 if (! EQ (value, Qunbound))
5325 parms = Fcons (Fcons (Qinternal_border_width, value),
5326 parms);
5327 }
1edf84e7 5328 /* Default internalBorderWidth to 0 on Windows to match other programs. */
ee78dc32 5329 x_default_parameter (f, parms, Qinternal_border_width, make_number (0),
1660f34a
JR
5330 "internalBorderWidth", "InternalBorder", RES_TYPE_NUMBER);
5331 x_default_parameter (f, parms, Qvertical_scroll_bars, Qright,
5332 "verticalScrollBars", "ScrollBars", RES_TYPE_SYMBOL);
ee78dc32
GV
5333
5334 /* Also do the stuff which must be set before the window exists. */
5335 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
6fc2811b 5336 "foreground", "Foreground", RES_TYPE_STRING);
ee78dc32 5337 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
6fc2811b 5338 "background", "Background", RES_TYPE_STRING);
ee78dc32 5339 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
6fc2811b 5340 "pointerColor", "Foreground", RES_TYPE_STRING);
ee78dc32 5341 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
6fc2811b 5342 "cursorColor", "Foreground", RES_TYPE_STRING);
ee78dc32 5343 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
6fc2811b
JR
5344 "borderColor", "BorderColor", RES_TYPE_STRING);
5345 x_default_parameter (f, parms, Qscreen_gamma, Qnil,
5346 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
dfff8a69
JR
5347 x_default_parameter (f, parms, Qline_spacing, Qnil,
5348 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
6fc2811b 5349
ee78dc32 5350
6fc2811b
JR
5351 /* Init faces before x_default_parameter is called for scroll-bar
5352 parameters because that function calls x_set_scroll_bar_width,
5353 which calls change_frame_size, which calls Fset_window_buffer,
5354 which runs hooks, which call Fvertical_motion. At the end, we
5355 end up in init_iterator with a null face cache, which should not
5356 happen. */
5357 init_frame_faces (f);
5358
ee78dc32 5359 x_default_parameter (f, parms, Qmenu_bar_lines, make_number (1),
6fc2811b
JR
5360 "menuBar", "MenuBar", RES_TYPE_NUMBER);
5361 x_default_parameter (f, parms, Qtool_bar_lines, make_number (0),
5362 "toolBar", "ToolBar", RES_TYPE_NUMBER);
1edf84e7 5363 x_default_parameter (f, parms, Qbuffer_predicate, Qnil,
6fc2811b 5364 "bufferPredicate", "BufferPredicate", RES_TYPE_SYMBOL);
1edf84e7 5365 x_default_parameter (f, parms, Qtitle, Qnil,
6fc2811b 5366 "title", "Title", RES_TYPE_STRING);
ee78dc32 5367
fbd6baed
GV
5368 f->output_data.w32->dwStyle = WS_OVERLAPPEDWINDOW;
5369 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
ee78dc32
GV
5370 window_prompting = x_figure_window_size (f, parms);
5371
5372 if (window_prompting & XNegative)
5373 {
5374 if (window_prompting & YNegative)
fbd6baed 5375 f->output_data.w32->win_gravity = SouthEastGravity;
ee78dc32 5376 else
fbd6baed 5377 f->output_data.w32->win_gravity = NorthEastGravity;
ee78dc32
GV
5378 }
5379 else
5380 {
5381 if (window_prompting & YNegative)
fbd6baed 5382 f->output_data.w32->win_gravity = SouthWestGravity;
ee78dc32 5383 else
fbd6baed 5384 f->output_data.w32->win_gravity = NorthWestGravity;
ee78dc32
GV
5385 }
5386
fbd6baed 5387 f->output_data.w32->size_hint_flags = window_prompting;
ee78dc32 5388
6fc2811b
JR
5389 tem = w32_get_arg (parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
5390 f->no_split = minibuffer_only || EQ (tem, Qt);
5391
5392 /* Create the window. Add the tool-bar height to the initial frame
5393 height so that the user gets a text display area of the size he
5394 specified with -g or via the registry. Later changes of the
5395 tool-bar height don't change the frame size. This is done so that
5396 users can create tall Emacs frames without having to guess how
5397 tall the tool-bar will get. */
5398 f->height += FRAME_TOOL_BAR_LINES (f);
fbd6baed 5399 w32_window (f, window_prompting, minibuffer_only);
ee78dc32 5400 x_icon (f, parms);
6fc2811b
JR
5401
5402 x_make_gc (f);
5403
5404 /* Now consider the frame official. */
5405 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
5406 Vframe_list = Fcons (frame, Vframe_list);
ee78dc32
GV
5407
5408 /* We need to do this after creating the window, so that the
5409 icon-creation functions can say whose icon they're describing. */
5410 x_default_parameter (f, parms, Qicon_type, Qnil,
6fc2811b 5411 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
ee78dc32
GV
5412
5413 x_default_parameter (f, parms, Qauto_raise, Qnil,
6fc2811b 5414 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
ee78dc32 5415 x_default_parameter (f, parms, Qauto_lower, Qnil,
6fc2811b 5416 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
ee78dc32 5417 x_default_parameter (f, parms, Qcursor_type, Qbox,
6fc2811b
JR
5418 "cursorType", "CursorType", RES_TYPE_SYMBOL);
5419 x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
5420 "scrollBarWidth", "ScrollBarWidth", RES_TYPE_NUMBER);
ee78dc32
GV
5421
5422 /* Dimensions, especially f->height, must be done via change_frame_size.
5423 Change will not be effected unless different from the current
5424 f->height. */
5425 width = f->width;
5426 height = f->height;
1026b400
RS
5427 f->height = 0;
5428 SET_FRAME_WIDTH (f, 0);
6fc2811b
JR
5429 change_frame_size (f, height, width, 1, 0, 0);
5430
6fc2811b
JR
5431 /* Tell the server what size and position, etc, we want, and how
5432 badly we want them. This should be done after we have the menu
5433 bar so that its size can be taken into account. */
ee78dc32
GV
5434 BLOCK_INPUT;
5435 x_wm_set_size_hint (f, window_prompting, 0);
5436 UNBLOCK_INPUT;
5437
4694d762
JR
5438 /* Set up faces after all frame parameters are known. This call
5439 also merges in face attributes specified for new frames. If we
5440 don't do this, the `menu' face for instance won't have the right
5441 colors, and the menu bar won't appear in the specified colors for
5442 new frames. */
5443 call1 (Qface_set_after_frame_default, frame);
5444
6fc2811b
JR
5445 /* Make the window appear on the frame and enable display, unless
5446 the caller says not to. However, with explicit parent, Emacs
5447 cannot control visibility, so don't try. */
fbd6baed 5448 if (! f->output_data.w32->explicit_parent)
ee78dc32
GV
5449 {
5450 Lisp_Object visibility;
5451
6fc2811b 5452 visibility = w32_get_arg (parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL);
ee78dc32
GV
5453 if (EQ (visibility, Qunbound))
5454 visibility = Qt;
5455
5456 if (EQ (visibility, Qicon))
5457 x_iconify_frame (f);
5458 else if (! NILP (visibility))
5459 x_make_frame_visible (f);
5460 else
5461 /* Must have been Qnil. */
5462 ;
5463 }
6fc2811b 5464 UNGCPRO;
ee78dc32
GV
5465 return unbind_to (count, frame);
5466}
5467
5468/* FRAME is used only to get a handle on the X display. We don't pass the
5469 display info directly because we're called from frame.c, which doesn't
5470 know about that structure. */
5471Lisp_Object
5472x_get_focus_frame (frame)
5473 struct frame *frame;
5474{
fbd6baed 5475 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (frame);
ee78dc32 5476 Lisp_Object xfocus;
fbd6baed 5477 if (! dpyinfo->w32_focus_frame)
ee78dc32
GV
5478 return Qnil;
5479
fbd6baed 5480 XSETFRAME (xfocus, dpyinfo->w32_focus_frame);
ee78dc32
GV
5481 return xfocus;
5482}
1edf84e7
GV
5483
5484DEFUN ("w32-focus-frame", Fw32_focus_frame, Sw32_focus_frame, 1, 1, 0,
5485 "Give FRAME input focus, raising to foreground if necessary.")
5486 (frame)
5487 Lisp_Object frame;
5488{
5489 x_focus_on_frame (check_x_frame (frame));
5490 return Qnil;
5491}
5492
ee78dc32 5493\f
767b1ff0
JR
5494/* Return the charset portion of a font name. */
5495char * xlfd_charset_of_font (char * fontname)
5496{
5497 char *charset, *encoding;
5498
5499 encoding = strrchr(fontname, '-');
ceb12877 5500 if (!encoding || encoding == fontname)
767b1ff0
JR
5501 return NULL;
5502
478ea067
AI
5503 for (charset = encoding - 1; charset >= fontname; charset--)
5504 if (*charset == '-')
5505 break;
767b1ff0 5506
478ea067 5507 if (charset == fontname || strcmp(charset, "-*-*") == 0)
767b1ff0
JR
5508 return NULL;
5509
5510 return charset + 1;
5511}
5512
33d52f9c
GV
5513struct font_info *w32_load_bdf_font (struct frame *f, char *fontname,
5514 int size, char* filename);
767b1ff0 5515BOOL w32_to_x_font (LOGFONT * lplf, char * lpxstr, int len, char * charset);
33d52f9c 5516
4587b026 5517struct font_info *
33d52f9c 5518w32_load_system_font (f,fontname,size)
55dcfc15
AI
5519 struct frame *f;
5520 char * fontname;
5521 int size;
ee78dc32 5522{
4587b026
GV
5523 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
5524 Lisp_Object font_names;
5525
4587b026
GV
5526 /* Get a list of all the fonts that match this name. Once we
5527 have a list of matching fonts, we compare them against the fonts
5528 we already have loaded by comparing names. */
5529 font_names = w32_list_fonts (f, build_string (fontname), size, 100);
5530
5531 if (!NILP (font_names))
3c190163 5532 {
4587b026
GV
5533 Lisp_Object tail;
5534 int i;
4587b026
GV
5535
5536 /* First check if any are already loaded, as that is cheaper
5537 than loading another one. */
5538 for (i = 0; i < dpyinfo->n_fonts; i++)
8e713be6 5539 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
6fc2811b
JR
5540 if (dpyinfo->font_table[i].name
5541 && (!strcmp (dpyinfo->font_table[i].name,
5542 XSTRING (XCAR (tail))->data)
5543 || !strcmp (dpyinfo->font_table[i].full_name,
5544 XSTRING (XCAR (tail))->data)))
4587b026 5545 return (dpyinfo->font_table + i);
6fc2811b 5546
8e713be6 5547 fontname = (char *) XSTRING (XCAR (font_names))->data;
4587b026 5548 }
1075afa9 5549 else if (w32_strict_fontnames)
5ca0cd71
GV
5550 {
5551 /* If EnumFontFamiliesEx was available, we got a full list of
5552 fonts back so stop now to avoid the possibility of loading a
5553 random font. If we had to fall back to EnumFontFamilies, the
5554 list is incomplete, so continue whether the font we want was
5555 listed or not. */
5556 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
5557 FARPROC enum_font_families_ex
1075afa9 5558 = GetProcAddress (gdi32, "EnumFontFamiliesExA");
5ca0cd71
GV
5559 if (enum_font_families_ex)
5560 return NULL;
5561 }
4587b026
GV
5562
5563 /* Load the font and add it to the table. */
5564 {
767b1ff0 5565 char *full_name, *encoding, *charset;
4587b026
GV
5566 XFontStruct *font;
5567 struct font_info *fontp;
3c190163 5568 LOGFONT lf;
4587b026 5569 BOOL ok;
6fc2811b 5570 int i;
5ac45f98 5571
4587b026 5572 if (!fontname || !x_to_w32_font (fontname, &lf))
3c190163 5573 return (NULL);
5ac45f98 5574
4587b026
GV
5575 if (!*lf.lfFaceName)
5576 /* If no name was specified for the font, we get a random font
5577 from CreateFontIndirect - this is not particularly
5578 desirable, especially since CreateFontIndirect does not
5579 fill out the missing name in lf, so we never know what we
5580 ended up with. */
5581 return NULL;
5582
3c190163 5583 font = (XFontStruct *) xmalloc (sizeof (XFontStruct));
c6be3860 5584 bzero (font, sizeof (*font));
5ac45f98 5585
33d52f9c
GV
5586 /* Set bdf to NULL to indicate that this is a Windows font. */
5587 font->bdf = NULL;
5ac45f98 5588
3c190163 5589 BLOCK_INPUT;
5ac45f98
GV
5590
5591 font->hfont = CreateFontIndirect (&lf);
ee78dc32 5592
1a292d24
AI
5593 if (font->hfont == NULL)
5594 {
5595 ok = FALSE;
5596 }
5597 else
5598 {
5599 HDC hdc;
5600 HANDLE oldobj;
5c6682be 5601 int codepage = w32_codepage_for_font (fontname);
1a292d24
AI
5602
5603 hdc = GetDC (dpyinfo->root_window);
5604 oldobj = SelectObject (hdc, font->hfont);
5c6682be 5605
1a292d24 5606 ok = GetTextMetrics (hdc, &font->tm);
5c6682be
JR
5607 if (codepage == CP_UNICODE)
5608 font->double_byte_p = 1;
5609 else
5610 font->double_byte_p = GetFontLanguageInfo(hdc) & GCP_DBCS;
5611
1a292d24
AI
5612 SelectObject (hdc, oldobj);
5613 ReleaseDC (dpyinfo->root_window, hdc);
6fc2811b
JR
5614 /* Fill out details in lf according to the font that was
5615 actually loaded. */
5616 lf.lfHeight = font->tm.tmInternalLeading - font->tm.tmHeight;
5617 lf.lfWidth = font->tm.tmAveCharWidth;
5618 lf.lfWeight = font->tm.tmWeight;
5619 lf.lfItalic = font->tm.tmItalic;
5620 lf.lfCharSet = font->tm.tmCharSet;
5621 lf.lfPitchAndFamily = ((font->tm.tmPitchAndFamily & TMPF_FIXED_PITCH)
d88c567c 5622 ? VARIABLE_PITCH : FIXED_PITCH);
6fc2811b
JR
5623 lf.lfOutPrecision = ((font->tm.tmPitchAndFamily & TMPF_VECTOR)
5624 ? OUT_STROKE_PRECIS : OUT_STRING_PRECIS);
c6be3860
AI
5625
5626 w32_cache_char_metrics (font);
1a292d24 5627 }
5ac45f98 5628
1a292d24 5629 UNBLOCK_INPUT;
5ac45f98 5630
4587b026
GV
5631 if (!ok)
5632 {
1a292d24
AI
5633 w32_unload_font (dpyinfo, font);
5634 return (NULL);
5635 }
ee78dc32 5636
6fc2811b
JR
5637 /* Find a free slot in the font table. */
5638 for (i = 0; i < dpyinfo->n_fonts; ++i)
5639 if (dpyinfo->font_table[i].name == NULL)
5640 break;
5641
5642 /* If no free slot found, maybe enlarge the font table. */
5643 if (i == dpyinfo->n_fonts
5644 && dpyinfo->n_fonts == dpyinfo->font_table_size)
4587b026 5645 {
6fc2811b
JR
5646 int sz;
5647 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
5648 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
4587b026 5649 dpyinfo->font_table
6fc2811b 5650 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
4587b026
GV
5651 }
5652
6fc2811b
JR
5653 fontp = dpyinfo->font_table + i;
5654 if (i == dpyinfo->n_fonts)
5655 ++dpyinfo->n_fonts;
4587b026
GV
5656
5657 /* Now fill in the slots of *FONTP. */
5658 BLOCK_INPUT;
5659 fontp->font = font;
6fc2811b 5660 fontp->font_idx = i;
4587b026
GV
5661 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
5662 bcopy (fontname, fontp->name, strlen (fontname) + 1);
5663
767b1ff0
JR
5664 charset = xlfd_charset_of_font (fontname);
5665
4587b026
GV
5666 /* Work out the font's full name. */
5667 full_name = (char *)xmalloc (100);
767b1ff0 5668 if (full_name && w32_to_x_font (&lf, full_name, 100, charset))
4587b026
GV
5669 fontp->full_name = full_name;
5670 else
5671 {
5672 /* If all else fails - just use the name we used to load it. */
5673 xfree (full_name);
5674 fontp->full_name = fontp->name;
5675 }
5676
5677 fontp->size = FONT_WIDTH (font);
5678 fontp->height = FONT_HEIGHT (font);
5679
5680 /* The slot `encoding' specifies how to map a character
5681 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
126f2e35
JR
5682 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
5683 (0:0x20..0x7F, 1:0xA0..0xFF,
5684 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
4587b026 5685 2:0xA020..0xFF7F). For the moment, we don't know which charset
6fc2811b 5686 uses this font. So, we set information in fontp->encoding[1]
4587b026
GV
5687 which is never used by any charset. If mapping can't be
5688 decided, set FONT_ENCODING_NOT_DECIDED. */
33d52f9c
GV
5689
5690 /* SJIS fonts need to be set to type 4, all others seem to work as
5691 type FONT_ENCODING_NOT_DECIDED. */
5692 encoding = strrchr (fontp->name, '-');
5693 if (encoding && stricmp (encoding+1, "sjis") == 0)
1c885fe1 5694 fontp->encoding[1] = 4;
33d52f9c 5695 else
1c885fe1 5696 fontp->encoding[1] = FONT_ENCODING_NOT_DECIDED;
4587b026
GV
5697
5698 /* The following three values are set to 0 under W32, which is
5699 what they get set to if XGetFontProperty fails under X. */
5700 fontp->baseline_offset = 0;
5701 fontp->relative_compose = 0;
33d52f9c 5702 fontp->default_ascent = 0;
4587b026 5703
6fc2811b
JR
5704 /* Set global flag fonts_changed_p to non-zero if the font loaded
5705 has a character with a smaller width than any other character
5706 before, or if the font loaded has a smalle>r height than any
5707 other font loaded before. If this happens, it will make a
5708 glyph matrix reallocation necessary. */
5709 fonts_changed_p = x_compute_min_glyph_bounds (f);
4587b026 5710 UNBLOCK_INPUT;
4587b026
GV
5711 return fontp;
5712 }
5713}
5714
33d52f9c
GV
5715/* Load font named FONTNAME of size SIZE for frame F, and return a
5716 pointer to the structure font_info while allocating it dynamically.
5717 If loading fails, return NULL. */
5718struct font_info *
5719w32_load_font (f,fontname,size)
5720struct frame *f;
5721char * fontname;
5722int size;
5723{
5724 Lisp_Object bdf_fonts;
5725 struct font_info *retval = NULL;
5726
5727 bdf_fonts = w32_list_bdf_fonts (build_string (fontname));
5728
5729 while (!retval && CONSP (bdf_fonts))
5730 {
5731 char *bdf_name, *bdf_file;
5732 Lisp_Object bdf_pair;
5733
8e713be6
KR
5734 bdf_name = XSTRING (XCAR (bdf_fonts))->data;
5735 bdf_pair = Fassoc (XCAR (bdf_fonts), Vw32_bdf_filename_alist);
5736 bdf_file = XSTRING (XCDR (bdf_pair))->data;
33d52f9c
GV
5737
5738 retval = w32_load_bdf_font (f, bdf_name, size, bdf_file);
5739
8e713be6 5740 bdf_fonts = XCDR (bdf_fonts);
33d52f9c
GV
5741 }
5742
5743 if (retval)
5744 return retval;
5745
5746 return w32_load_system_font(f, fontname, size);
5747}
5748
5749
ee78dc32 5750void
fbd6baed
GV
5751w32_unload_font (dpyinfo, font)
5752 struct w32_display_info *dpyinfo;
ee78dc32
GV
5753 XFontStruct * font;
5754{
5755 if (font)
5756 {
c6be3860 5757 if (font->per_char) xfree (font->per_char);
33d52f9c
GV
5758 if (font->bdf) w32_free_bdf_font (font->bdf);
5759
3c190163 5760 if (font->hfont) DeleteObject(font->hfont);
ee78dc32
GV
5761 xfree (font);
5762 }
5763}
5764
fbd6baed 5765/* The font conversion stuff between x and w32 */
ee78dc32
GV
5766
5767/* X font string is as follows (from faces.el)
5768 * (let ((- "[-?]")
5769 * (foundry "[^-]+")
5770 * (family "[^-]+")
5771 * (weight "\\(bold\\|demibold\\|medium\\)") ; 1
5772 * (weight\? "\\([^-]*\\)") ; 1
5773 * (slant "\\([ior]\\)") ; 2
5774 * (slant\? "\\([^-]?\\)") ; 2
5775 * (swidth "\\([^-]*\\)") ; 3
5776 * (adstyle "[^-]*") ; 4
5777 * (pixelsize "[0-9]+")
5778 * (pointsize "[0-9][0-9]+")
5779 * (resx "[0-9][0-9]+")
5780 * (resy "[0-9][0-9]+")
5781 * (spacing "[cmp?*]")
5782 * (avgwidth "[0-9]+")
5783 * (registry "[^-]+")
5784 * (encoding "[^-]+")
5785 * )
ee78dc32 5786 */
ee78dc32
GV
5787
5788LONG
fbd6baed 5789x_to_w32_weight (lpw)
ee78dc32
GV
5790 char * lpw;
5791{
5792 if (!lpw) return (FW_DONTCARE);
5ac45f98
GV
5793
5794 if (stricmp (lpw,"heavy") == 0) return FW_HEAVY;
5795 else if (stricmp (lpw,"extrabold") == 0) return FW_EXTRABOLD;
5796 else if (stricmp (lpw,"bold") == 0) return FW_BOLD;
5797 else if (stricmp (lpw,"demibold") == 0) return FW_SEMIBOLD;
1edf84e7 5798 else if (stricmp (lpw,"semibold") == 0) return FW_SEMIBOLD;
5ac45f98
GV
5799 else if (stricmp (lpw,"medium") == 0) return FW_MEDIUM;
5800 else if (stricmp (lpw,"normal") == 0) return FW_NORMAL;
5801 else if (stricmp (lpw,"light") == 0) return FW_LIGHT;
5802 else if (stricmp (lpw,"extralight") == 0) return FW_EXTRALIGHT;
5803 else if (stricmp (lpw,"thin") == 0) return FW_THIN;
ee78dc32 5804 else
5ac45f98 5805 return FW_DONTCARE;
ee78dc32
GV
5806}
5807
5ac45f98 5808
ee78dc32 5809char *
fbd6baed 5810w32_to_x_weight (fnweight)
ee78dc32
GV
5811 int fnweight;
5812{
5ac45f98
GV
5813 if (fnweight >= FW_HEAVY) return "heavy";
5814 if (fnweight >= FW_EXTRABOLD) return "extrabold";
5815 if (fnweight >= FW_BOLD) return "bold";
03f8fb34 5816 if (fnweight >= FW_SEMIBOLD) return "demibold";
5ac45f98
GV
5817 if (fnweight >= FW_MEDIUM) return "medium";
5818 if (fnweight >= FW_NORMAL) return "normal";
5819 if (fnweight >= FW_LIGHT) return "light";
5820 if (fnweight >= FW_EXTRALIGHT) return "extralight";
5821 if (fnweight >= FW_THIN) return "thin";
5822 else
5823 return "*";
5824}
5825
5826LONG
fbd6baed 5827x_to_w32_charset (lpcs)
5ac45f98
GV
5828 char * lpcs;
5829{
767b1ff0 5830 Lisp_Object this_entry, w32_charset;
4587b026 5831
dfff8a69
JR
5832 /* Look through w32-charset-info-alist for the character set.
5833 Format of each entry is
5834 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
5835 */
767b1ff0 5836 this_entry = Fassoc (build_string(lpcs), Vw32_charset_info_alist);
4587b026 5837
767b1ff0
JR
5838 if (NILP(this_entry))
5839 {
5840 /* At startup, we want iso8859-1 fonts to come up properly. */
5841 if (stricmp(lpcs, "iso8859-1") == 0)
5842 return ANSI_CHARSET;
5843 else
5844 return DEFAULT_CHARSET;
5845 }
5846
5847 w32_charset = Fcar (Fcdr (this_entry));
5848
5849 // Translate Lisp symbol to number.
5850 if (w32_charset == Qw32_charset_ansi)
5851 return ANSI_CHARSET;
5852 if (w32_charset == Qw32_charset_symbol)
5853 return SYMBOL_CHARSET;
5854 if (w32_charset == Qw32_charset_shiftjis)
5855 return SHIFTJIS_CHARSET;
5856 if (w32_charset == Qw32_charset_hangeul)
5857 return HANGEUL_CHARSET;
5858 if (w32_charset == Qw32_charset_chinesebig5)
5859 return CHINESEBIG5_CHARSET;
5860 if (w32_charset == Qw32_charset_gb2312)
5861 return GB2312_CHARSET;
5862 if (w32_charset == Qw32_charset_oem)
5863 return OEM_CHARSET;
dfff8a69 5864#ifdef JOHAB_CHARSET
767b1ff0
JR
5865 if (w32_charset == Qw32_charset_johab)
5866 return JOHAB_CHARSET;
5867 if (w32_charset == Qw32_charset_easteurope)
5868 return EASTEUROPE_CHARSET;
5869 if (w32_charset == Qw32_charset_turkish)
5870 return TURKISH_CHARSET;
5871 if (w32_charset == Qw32_charset_baltic)
5872 return BALTIC_CHARSET;
5873 if (w32_charset == Qw32_charset_russian)
5874 return RUSSIAN_CHARSET;
5875 if (w32_charset == Qw32_charset_arabic)
5876 return ARABIC_CHARSET;
5877 if (w32_charset == Qw32_charset_greek)
5878 return GREEK_CHARSET;
5879 if (w32_charset == Qw32_charset_hebrew)
5880 return HEBREW_CHARSET;
5881 if (w32_charset == Qw32_charset_vietnamese)
5882 return VIETNAMESE_CHARSET;
5883 if (w32_charset == Qw32_charset_thai)
5884 return THAI_CHARSET;
5885 if (w32_charset == Qw32_charset_mac)
5886 return MAC_CHARSET;
dfff8a69 5887#endif /* JOHAB_CHARSET */
5ac45f98 5888#ifdef UNICODE_CHARSET
767b1ff0
JR
5889 if (w32_charset == Qw32_charset_unicode)
5890 return UNICODE_CHARSET;
5ac45f98 5891#endif
dfff8a69
JR
5892
5893 return DEFAULT_CHARSET;
5ac45f98
GV
5894}
5895
dfff8a69 5896
5ac45f98 5897char *
fbd6baed 5898w32_to_x_charset (fncharset)
5ac45f98
GV
5899 int fncharset;
5900{
1edf84e7 5901 static char buf[16];
767b1ff0 5902 Lisp_Object charset_type;
1edf84e7 5903
5ac45f98
GV
5904 switch (fncharset)
5905 {
767b1ff0
JR
5906 case ANSI_CHARSET:
5907 /* Handle startup case of w32-charset-info-alist not
5908 being set up yet. */
5909 if (NILP(Vw32_charset_info_alist))
5910 return "iso8859-1";
5911 charset_type = Qw32_charset_ansi;
5912 break;
5913 case DEFAULT_CHARSET:
5914 charset_type = Qw32_charset_default;
5915 break;
5916 case SYMBOL_CHARSET:
5917 charset_type = Qw32_charset_symbol;
5918 break;
5919 case SHIFTJIS_CHARSET:
5920 charset_type = Qw32_charset_shiftjis;
5921 break;
5922 case HANGEUL_CHARSET:
5923 charset_type = Qw32_charset_hangeul;
5924 break;
5925 case GB2312_CHARSET:
5926 charset_type = Qw32_charset_gb2312;
5927 break;
5928 case CHINESEBIG5_CHARSET:
5929 charset_type = Qw32_charset_chinesebig5;
5930 break;
5931 case OEM_CHARSET:
5932 charset_type = Qw32_charset_oem;
5933 break;
4587b026
GV
5934
5935 /* More recent versions of Windows (95 and NT4.0) define more
5936 character sets. */
5937#ifdef EASTEUROPE_CHARSET
767b1ff0
JR
5938 case EASTEUROPE_CHARSET:
5939 charset_type = Qw32_charset_easteurope;
5940 break;
5941 case TURKISH_CHARSET:
5942 charset_type = Qw32_charset_turkish;
5943 break;
5944 case BALTIC_CHARSET:
5945 charset_type = Qw32_charset_baltic;
5946 break;
33d52f9c 5947 case RUSSIAN_CHARSET:
767b1ff0
JR
5948 charset_type = Qw32_charset_russian;
5949 break;
5950 case ARABIC_CHARSET:
5951 charset_type = Qw32_charset_arabic;
5952 break;
5953 case GREEK_CHARSET:
5954 charset_type = Qw32_charset_greek;
5955 break;
5956 case HEBREW_CHARSET:
5957 charset_type = Qw32_charset_hebrew;
5958 break;
5959 case VIETNAMESE_CHARSET:
5960 charset_type = Qw32_charset_vietnamese;
5961 break;
5962 case THAI_CHARSET:
5963 charset_type = Qw32_charset_thai;
5964 break;
5965 case MAC_CHARSET:
5966 charset_type = Qw32_charset_mac;
5967 break;
5968 case JOHAB_CHARSET:
5969 charset_type = Qw32_charset_johab;
5970 break;
4587b026
GV
5971#endif
5972
5ac45f98 5973#ifdef UNICODE_CHARSET
767b1ff0
JR
5974 case UNICODE_CHARSET:
5975 charset_type = Qw32_charset_unicode;
5976 break;
5ac45f98 5977#endif
767b1ff0
JR
5978 default:
5979 /* Encode numerical value of unknown charset. */
5980 sprintf (buf, "*-#%u", fncharset);
5981 return buf;
5ac45f98 5982 }
767b1ff0
JR
5983
5984 {
5985 Lisp_Object rest;
5986 char * best_match = NULL;
5987
5988 /* Look through w32-charset-info-alist for the character set.
5989 Prefer ISO codepages, and prefer lower numbers in the ISO
5990 range. Only return charsets for codepages which are installed.
5991
5992 Format of each entry is
5993 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
5994 */
5995 for (rest = Vw32_charset_info_alist; CONSP (rest); rest = XCDR (rest))
5996 {
5997 char * x_charset;
5998 Lisp_Object w32_charset;
5999 Lisp_Object codepage;
6000
6001 Lisp_Object this_entry = XCAR (rest);
6002
6003 /* Skip invalid entries in alist. */
6004 if (!CONSP (this_entry) || !STRINGP (XCAR (this_entry))
6005 || !CONSP (XCDR (this_entry))
6006 || !SYMBOLP (XCAR (XCDR (this_entry))))
6007 continue;
6008
6009 x_charset = XSTRING (XCAR (this_entry))->data;
6010 w32_charset = XCAR (XCDR (this_entry));
6011 codepage = XCDR (XCDR (this_entry));
6012
6013 /* Look for Same charset and a valid codepage (or non-int
6014 which means ignore). */
6015 if (w32_charset == charset_type
6016 && (!INTEGERP (codepage) || codepage == CP_DEFAULT
6017 || IsValidCodePage (XINT (codepage))))
6018 {
6019 /* If we don't have a match already, then this is the
6020 best. */
6021 if (!best_match)
6022 best_match = x_charset;
6023 /* If this is an ISO codepage, and the best so far isn't,
6024 then this is better. */
6025 else if (stricmp (best_match, "iso") != 0
6026 && stricmp (x_charset, "iso") == 0)
6027 best_match = x_charset;
6028 /* If both are ISO8859 codepages, choose the one with the
6029 lowest number in the encoding field. */
6030 else if (stricmp (best_match, "iso8859-") == 0
6031 && stricmp (x_charset, "iso8859-") == 0)
6032 {
6033 int best_enc = atoi (best_match + 8);
6034 int this_enc = atoi (x_charset + 8);
6035 if (this_enc > 0 && this_enc < best_enc)
6036 best_match = x_charset;
6037 }
6038 }
6039 }
6040
6041 /* If no match, encode the numeric value. */
6042 if (!best_match)
6043 {
6044 sprintf (buf, "*-#%u", fncharset);
6045 return buf;
6046 }
6047
6048 strncpy(buf, best_match, 15);
6049 buf[15] = '\0';
6050 return buf;
6051 }
ee78dc32
GV
6052}
6053
dfff8a69
JR
6054
6055/* Get the Windows codepage corresponding to the specified font. The
6056 charset info in the font name is used to look up
6057 w32-charset-to-codepage-alist. */
6058int
6059w32_codepage_for_font (char *fontname)
6060{
767b1ff0
JR
6061 Lisp_Object codepage, entry;
6062 char *charset_str, *charset, *end;
dfff8a69 6063
767b1ff0 6064 if (NILP (Vw32_charset_info_alist))
dfff8a69
JR
6065 return CP_DEFAULT;
6066
767b1ff0
JR
6067 /* Extract charset part of font string. */
6068 charset = xlfd_charset_of_font (fontname);
6069
6070 if (!charset)
ceb12877 6071 return CP_UNKNOWN;
767b1ff0
JR
6072
6073 charset_str = (char *) alloca (strlen (charset));
6074 strcpy (charset_str, charset);
6075
dfff8a69
JR
6076 /* Remove leading "*-". */
6077 if (strncmp ("*-", charset_str, 2) == 0)
6078 charset = charset_str + 2;
6079 else
6080 charset = charset_str;
6081
6082 /* Stop match at wildcard (including preceding '-'). */
6083 if (end = strchr (charset, '*'))
6084 {
6085 if (end > charset && *(end-1) == '-')
6086 end--;
6087 *end = '\0';
6088 }
6089
767b1ff0
JR
6090 entry = Fassoc (build_string(charset), Vw32_charset_info_alist);
6091 if (NILP (entry))
ceb12877 6092 return CP_UNKNOWN;
767b1ff0
JR
6093
6094 codepage = Fcdr (Fcdr (entry));
6095
6096 if (NILP (codepage))
6097 return CP_8BIT;
6098 else if (XFASTINT (codepage) == XFASTINT (Qt))
6099 return CP_UNICODE;
6100 else if (INTEGERP (codepage))
dfff8a69
JR
6101 return XINT (codepage);
6102 else
ceb12877 6103 return CP_UNKNOWN;
dfff8a69
JR
6104}
6105
6106
ee78dc32 6107BOOL
767b1ff0 6108w32_to_x_font (lplogfont, lpxstr, len, specific_charset)
ee78dc32
GV
6109 LOGFONT * lplogfont;
6110 char * lpxstr;
6111 int len;
767b1ff0 6112 char * specific_charset;
ee78dc32 6113{
6fc2811b 6114 char* fonttype;
f46e6225 6115 char *fontname;
3cb20f4a
RS
6116 char height_pixels[8];
6117 char height_dpi[8];
6118 char width_pixels[8];
4587b026 6119 char *fontname_dash;
d88c567c
JR
6120 int display_resy = one_w32_display_info.resy;
6121 int display_resx = one_w32_display_info.resx;
f46e6225
GV
6122 int bufsz;
6123 struct coding_system coding;
3cb20f4a
RS
6124
6125 if (!lpxstr) abort ();
ee78dc32 6126
3cb20f4a
RS
6127 if (!lplogfont)
6128 return FALSE;
6129
6fc2811b
JR
6130 if (lplogfont->lfOutPrecision == OUT_STRING_PRECIS)
6131 fonttype = "raster";
6132 else if (lplogfont->lfOutPrecision == OUT_STROKE_PRECIS)
6133 fonttype = "outline";
6134 else
6135 fonttype = "unknown";
6136
f46e6225
GV
6137 setup_coding_system (Fcheck_coding_system (Vw32_system_coding_system),
6138 &coding);
aab5ac44
KH
6139 coding.src_multibyte = 0;
6140 coding.dst_multibyte = 1;
f46e6225
GV
6141 coding.mode |= CODING_MODE_LAST_BLOCK;
6142 bufsz = decoding_buffer_size (&coding, LF_FACESIZE);
6143
6144 fontname = alloca(sizeof(*fontname) * bufsz);
6145 decode_coding (&coding, lplogfont->lfFaceName, fontname,
6146 strlen(lplogfont->lfFaceName), bufsz - 1);
6147 *(fontname + coding.produced) = '\0';
4587b026
GV
6148
6149 /* Replace dashes with underscores so the dashes are not
f46e6225 6150 misinterpreted. */
4587b026
GV
6151 fontname_dash = fontname;
6152 while (fontname_dash = strchr (fontname_dash, '-'))
6153 *fontname_dash = '_';
6154
3cb20f4a 6155 if (lplogfont->lfHeight)
ee78dc32 6156 {
3cb20f4a
RS
6157 sprintf (height_pixels, "%u", abs (lplogfont->lfHeight));
6158 sprintf (height_dpi, "%u",
33d52f9c 6159 abs (lplogfont->lfHeight) * 720 / display_resy);
5ac45f98
GV
6160 }
6161 else
ee78dc32 6162 {
3cb20f4a
RS
6163 strcpy (height_pixels, "*");
6164 strcpy (height_dpi, "*");
ee78dc32 6165 }
3cb20f4a
RS
6166 if (lplogfont->lfWidth)
6167 sprintf (width_pixels, "%u", lplogfont->lfWidth * 10);
6168 else
6169 strcpy (width_pixels, "*");
6170
6171 _snprintf (lpxstr, len - 1,
6fc2811b
JR
6172 "-%s-%s-%s-%c-normal-normal-%s-%s-%d-%d-%c-%s-%s",
6173 fonttype, /* foundry */
4587b026
GV
6174 fontname, /* family */
6175 w32_to_x_weight (lplogfont->lfWeight), /* weight */
6176 lplogfont->lfItalic?'i':'r', /* slant */
6177 /* setwidth name */
6178 /* add style name */
6179 height_pixels, /* pixel size */
6180 height_dpi, /* point size */
33d52f9c
GV
6181 display_resx, /* resx */
6182 display_resy, /* resy */
4587b026
GV
6183 ((lplogfont->lfPitchAndFamily & 0x3) == VARIABLE_PITCH)
6184 ? 'p' : 'c', /* spacing */
6185 width_pixels, /* avg width */
767b1ff0
JR
6186 specific_charset ? specific_charset
6187 : w32_to_x_charset (lplogfont->lfCharSet)
6188 /* charset registry and encoding */
3cb20f4a
RS
6189 );
6190
ee78dc32
GV
6191 lpxstr[len - 1] = 0; /* just to be sure */
6192 return (TRUE);
6193}
6194
6195BOOL
fbd6baed 6196x_to_w32_font (lpxstr, lplogfont)
ee78dc32
GV
6197 char * lpxstr;
6198 LOGFONT * lplogfont;
6199{
f46e6225
GV
6200 struct coding_system coding;
6201
ee78dc32 6202 if (!lplogfont) return (FALSE);
f46e6225 6203
ee78dc32 6204 memset (lplogfont, 0, sizeof (*lplogfont));
5ac45f98 6205
1a292d24 6206 /* Set default value for each field. */
771c47d5 6207#if 1
ee78dc32
GV
6208 lplogfont->lfOutPrecision = OUT_DEFAULT_PRECIS;
6209 lplogfont->lfClipPrecision = CLIP_DEFAULT_PRECIS;
6210 lplogfont->lfQuality = DEFAULT_QUALITY;
5ac45f98
GV
6211#else
6212 /* go for maximum quality */
6213 lplogfont->lfOutPrecision = OUT_STROKE_PRECIS;
6214 lplogfont->lfClipPrecision = CLIP_STROKE_PRECIS;
6215 lplogfont->lfQuality = PROOF_QUALITY;
6216#endif
6217
1a292d24
AI
6218 lplogfont->lfCharSet = DEFAULT_CHARSET;
6219 lplogfont->lfWeight = FW_DONTCARE;
6220 lplogfont->lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
6221
5ac45f98
GV
6222 if (!lpxstr)
6223 return FALSE;
6224
6225 /* Provide a simple escape mechanism for specifying Windows font names
6226 * directly -- if font spec does not beginning with '-', assume this
6227 * format:
6228 * "<font name>[:height in pixels[:width in pixels[:weight]]]"
6229 */
ee78dc32 6230
5ac45f98
GV
6231 if (*lpxstr == '-')
6232 {
33d52f9c
GV
6233 int fields, tem;
6234 char name[50], weight[20], slant, pitch, pixels[10], height[10],
6235 width[10], resy[10], remainder[20];
5ac45f98 6236 char * encoding;
d98c0337 6237 int dpi = one_w32_display_info.resy;
5ac45f98
GV
6238
6239 fields = sscanf (lpxstr,
33d52f9c
GV
6240 "-%*[^-]-%49[^-]-%19[^-]-%c-%*[^-]-%*[^-]-%9[^-]-%9[^-]-%*[^-]-%9[^-]-%c-%9[^-]-%19s",
6241 name, weight, &slant, pixels, height, resy, &pitch, width, remainder);
5ac45f98
GV
6242 if (fields == EOF) return (FALSE);
6243
6fc2811b
JR
6244 /* If wildcards cover more than one field, we don't know which
6245 field is which, so don't fill any in. */
6246
6247 if (fields < 9)
6248 fields = 0;
6249
5ac45f98
GV
6250 if (fields > 0 && name[0] != '*')
6251 {
8ea3e054
RS
6252 int bufsize;
6253 unsigned char *buf;
6254
f46e6225
GV
6255 setup_coding_system
6256 (Fcheck_coding_system (Vw32_system_coding_system), &coding);
aab5ac44
KH
6257 coding.src_multibyte = 1;
6258 coding.dst_multibyte = 1;
8ea3e054
RS
6259 bufsize = encoding_buffer_size (&coding, strlen (name));
6260 buf = (unsigned char *) alloca (bufsize);
f46e6225 6261 coding.mode |= CODING_MODE_LAST_BLOCK;
8ea3e054
RS
6262 encode_coding (&coding, name, buf, strlen (name), bufsize);
6263 if (coding.produced >= LF_FACESIZE)
6264 coding.produced = LF_FACESIZE - 1;
6265 buf[coding.produced] = 0;
6266 strcpy (lplogfont->lfFaceName, buf);
5ac45f98
GV
6267 }
6268 else
6269 {
6fc2811b 6270 lplogfont->lfFaceName[0] = '\0';
5ac45f98
GV
6271 }
6272
6273 fields--;
6274
fbd6baed 6275 lplogfont->lfWeight = x_to_w32_weight ((fields > 0 ? weight : ""));
5ac45f98
GV
6276
6277 fields--;
6278
c8874f14 6279 lplogfont->lfItalic = (fields > 0 && slant == 'i');
5ac45f98
GV
6280
6281 fields--;
6282
6283 if (fields > 0 && pixels[0] != '*')
6284 lplogfont->lfHeight = atoi (pixels);
6285
6286 fields--;
5ac45f98 6287 fields--;
33d52f9c
GV
6288 if (fields > 0 && resy[0] != '*')
6289 {
6fc2811b 6290 tem = atoi (resy);
33d52f9c
GV
6291 if (tem > 0) dpi = tem;
6292 }
5ac45f98 6293
33d52f9c
GV
6294 if (fields > -1 && lplogfont->lfHeight == 0 && height[0] != '*')
6295 lplogfont->lfHeight = atoi (height) * dpi / 720;
6296
6297 if (fields > 0)
5ac45f98
GV
6298 lplogfont->lfPitchAndFamily =
6299 (fields > 0 && pitch == 'p') ? VARIABLE_PITCH : FIXED_PITCH;
6300
6301 fields--;
6302
6303 if (fields > 0 && width[0] != '*')
6304 lplogfont->lfWidth = atoi (width) / 10;
6305
6306 fields--;
6307
4587b026 6308 /* Strip the trailing '-' if present. (it shouldn't be, as it
d88c567c 6309 fails the test against xlfd-tight-regexp in fontset.el). */
3c190163 6310 {
5ac45f98
GV
6311 int len = strlen (remainder);
6312 if (len > 0 && remainder[len-1] == '-')
6313 remainder[len-1] = 0;
ee78dc32 6314 }
5ac45f98
GV
6315 encoding = remainder;
6316 if (strncmp (encoding, "*-", 2) == 0)
6317 encoding += 2;
fbd6baed 6318 lplogfont->lfCharSet = x_to_w32_charset (fields > 0 ? encoding : "");
5ac45f98
GV
6319 }
6320 else
6321 {
6322 int fields;
6323 char name[100], height[10], width[10], weight[20];
a1a80b40 6324
5ac45f98
GV
6325 fields = sscanf (lpxstr,
6326 "%99[^:]:%9[^:]:%9[^:]:%19s",
6327 name, height, width, weight);
6328
6329 if (fields == EOF) return (FALSE);
6330
6331 if (fields > 0)
6332 {
6333 strncpy (lplogfont->lfFaceName,name, LF_FACESIZE);
6334 lplogfont->lfFaceName[LF_FACESIZE-1] = 0;
6335 }
6336 else
6337 {
6338 lplogfont->lfFaceName[0] = 0;
6339 }
6340
6341 fields--;
6342
6343 if (fields > 0)
6344 lplogfont->lfHeight = atoi (height);
6345
6346 fields--;
6347
6348 if (fields > 0)
6349 lplogfont->lfWidth = atoi (width);
6350
6351 fields--;
6352
fbd6baed 6353 lplogfont->lfWeight = x_to_w32_weight ((fields > 0 ? weight : ""));
5ac45f98
GV
6354 }
6355
6356 /* This makes TrueType fonts work better. */
6357 lplogfont->lfHeight = - abs (lplogfont->lfHeight);
6fc2811b 6358
ee78dc32
GV
6359 return (TRUE);
6360}
6361
d88c567c
JR
6362/* Strip the pixel height and point height from the given xlfd, and
6363 return the pixel height. If no pixel height is specified, calculate
6364 one from the point height, or if that isn't defined either, return
6365 0 (which usually signifies a scalable font).
6366*/
6367int xlfd_strip_height (char *fontname)
6368{
6369 int pixel_height, point_height, dpi, field_number;
6370 char *read_from, *write_to;
6371
6372 xassert (fontname);
6373
6374 pixel_height = field_number = 0;
6375 write_to = NULL;
6376
6377 /* Look for height fields. */
6378 for (read_from = fontname; *read_from; read_from++)
6379 {
6380 if (*read_from == '-')
6381 {
6382 field_number++;
6383 if (field_number == 7) /* Pixel height. */
6384 {
6385 read_from++;
6386 write_to = read_from;
6387
6388 /* Find end of field. */
6389 for (;*read_from && *read_from != '-'; read_from++)
6390 ;
6391
6392 /* Split the fontname at end of field. */
6393 if (*read_from)
6394 {
6395 *read_from = '\0';
6396 read_from++;
6397 }
6398 pixel_height = atoi (write_to);
6399 /* Blank out field. */
6400 if (read_from > write_to)
6401 {
6402 *write_to = '-';
6403 write_to++;
6404 }
767b1ff0 6405 /* If the pixel height field is at the end (partial xlfd),
d88c567c
JR
6406 return now. */
6407 else
6408 return pixel_height;
6409
6410 /* If we got a pixel height, the point height can be
6411 ignored. Just blank it out and break now. */
6412 if (pixel_height)
6413 {
6414 /* Find end of point size field. */
6415 for (; *read_from && *read_from != '-'; read_from++)
6416 ;
6417
6418 if (*read_from)
6419 read_from++;
6420
6421 /* Blank out the point size field. */
6422 if (read_from > write_to)
6423 {
6424 *write_to = '-';
6425 write_to++;
6426 }
6427 else
6428 return pixel_height;
6429
6430 break;
6431 }
6432 /* If the point height is already blank, break now. */
6433 if (*read_from == '-')
6434 {
6435 read_from++;
6436 break;
6437 }
6438 }
6439 else if (field_number == 8)
6440 {
6441 /* If we didn't get a pixel height, try to get the point
6442 height and convert that. */
6443 int point_size;
6444 char *point_size_start = read_from++;
6445
6446 /* Find end of field. */
6447 for (; *read_from && *read_from != '-'; read_from++)
6448 ;
6449
6450 if (*read_from)
6451 {
6452 *read_from = '\0';
6453 read_from++;
6454 }
6455
6456 point_size = atoi (point_size_start);
6457
6458 /* Convert to pixel height. */
6459 pixel_height = point_size
6460 * one_w32_display_info.height_in / 720;
6461
6462 /* Blank out this field and break. */
6463 *write_to = '-';
6464 write_to++;
6465 break;
6466 }
6467 }
6468 }
6469
6470 /* Shift the rest of the font spec into place. */
6471 if (write_to && read_from > write_to)
6472 {
6473 for (; *read_from; read_from++, write_to++)
6474 *write_to = *read_from;
6475 *write_to = '\0';
6476 }
6477
6478 return pixel_height;
6479}
6480
6fc2811b 6481/* Assume parameter 1 is fully qualified, no wildcards. */
ee78dc32 6482BOOL
6fc2811b
JR
6483w32_font_match (fontname, pattern)
6484 char * fontname;
6485 char * pattern;
ee78dc32 6486{
6fc2811b 6487 char *regex = alloca (strlen (pattern) * 2);
d88c567c 6488 char *font_name_copy = alloca (strlen (fontname) + 1);
6fc2811b 6489 char *ptr;
ee78dc32 6490
d88c567c
JR
6491 /* Copy fontname so we can modify it during comparison. */
6492 strcpy (font_name_copy, fontname);
6493
6fc2811b
JR
6494 ptr = regex;
6495 *ptr++ = '^';
ee78dc32 6496
6fc2811b
JR
6497 /* Turn pattern into a regexp and do a regexp match. */
6498 for (; *pattern; pattern++)
6499 {
6500 if (*pattern == '?')
6501 *ptr++ = '.';
6502 else if (*pattern == '*')
6503 {
6504 *ptr++ = '.';
6505 *ptr++ = '*';
6506 }
33d52f9c 6507 else
6fc2811b 6508 *ptr++ = *pattern;
ee78dc32 6509 }
6fc2811b
JR
6510 *ptr = '$';
6511 *(ptr + 1) = '\0';
6512
d88c567c
JR
6513 /* Strip out font heights and compare them seperately, since
6514 rounding error can cause mismatches. This also allows a
6515 comparison between a font that declares only a pixel height and a
6516 pattern that declares the point height.
6517 */
6518 {
6519 int font_height, pattern_height;
6520
6521 font_height = xlfd_strip_height (font_name_copy);
6522 pattern_height = xlfd_strip_height (regex);
6523
6524 /* Compare now, and don't bother doing expensive regexp matching
6525 if the heights differ. */
6526 if (font_height && pattern_height && (font_height != pattern_height))
6527 return FALSE;
6528 }
6529
6fc2811b 6530 return (fast_c_string_match_ignore_case (build_string (regex),
d88c567c 6531 font_name_copy) >= 0);
ee78dc32
GV
6532}
6533
5ca0cd71
GV
6534/* Callback functions, and a structure holding info they need, for
6535 listing system fonts on W32. We need one set of functions to do the
6536 job properly, but these don't work on NT 3.51 and earlier, so we
6537 have a second set which don't handle character sets properly to
6538 fall back on.
6539
6540 In both cases, there are two passes made. The first pass gets one
6541 font from each family, the second pass lists all the fonts from
6542 each family. */
6543
ee78dc32
GV
6544typedef struct enumfont_t
6545{
6546 HDC hdc;
6547 int numFonts;
3cb20f4a 6548 LOGFONT logfont;
ee78dc32
GV
6549 XFontStruct *size_ref;
6550 Lisp_Object *pattern;
ee78dc32
GV
6551 Lisp_Object *tail;
6552} enumfont_t;
6553
6554int CALLBACK
6555enum_font_cb2 (lplf, lptm, FontType, lpef)
6556 ENUMLOGFONT * lplf;
6557 NEWTEXTMETRIC * lptm;
6558 int FontType;
6559 enumfont_t * lpef;
6560{
1edf84e7 6561 if (lplf->elfLogFont.lfStrikeOut || lplf->elfLogFont.lfUnderline)
ee78dc32
GV
6562 return (1);
6563
4587b026
GV
6564 /* Check that the character set matches if it was specified */
6565 if (lpef->logfont.lfCharSet != DEFAULT_CHARSET &&
6566 lplf->elfLogFont.lfCharSet != lpef->logfont.lfCharSet)
6567 return (1);
6568
ee78dc32
GV
6569 {
6570 char buf[100];
4587b026 6571 Lisp_Object width = Qnil;
767b1ff0 6572 char *charset = NULL;
ee78dc32 6573
6fc2811b
JR
6574 /* Truetype fonts do not report their true metrics until loaded */
6575 if (FontType != RASTER_FONTTYPE)
3cb20f4a 6576 {
6fc2811b
JR
6577 if (!NILP (*(lpef->pattern)))
6578 {
6579 /* Scalable fonts are as big as you want them to be. */
6580 lplf->elfLogFont.lfHeight = lpef->logfont.lfHeight;
6581 lplf->elfLogFont.lfWidth = lpef->logfont.lfWidth;
6582 width = make_number (lpef->logfont.lfWidth);
6583 }
6584 else
6585 {
6586 lplf->elfLogFont.lfHeight = 0;
6587 lplf->elfLogFont.lfWidth = 0;
6588 }
3cb20f4a 6589 }
6fc2811b 6590
f46e6225
GV
6591 /* Make sure the height used here is the same as everywhere
6592 else (ie character height, not cell height). */
6fc2811b
JR
6593 if (lplf->elfLogFont.lfHeight > 0)
6594 {
6595 /* lptm can be trusted for RASTER fonts, but not scalable ones. */
6596 if (FontType == RASTER_FONTTYPE)
6597 lplf->elfLogFont.lfHeight = lptm->tmInternalLeading - lptm->tmHeight;
6598 else
6599 lplf->elfLogFont.lfHeight = -lplf->elfLogFont.lfHeight;
6600 }
4587b026 6601
767b1ff0
JR
6602 if (!NILP (*(lpef->pattern)))
6603 {
6604 charset = xlfd_charset_of_font (XSTRING(*(lpef->pattern))->data);
6605
6606 /* Ensure that charset is valid for this font. */
6607 if (charset
6608 && (x_to_w32_charset (charset) != lplf->elfLogFont.lfCharSet))
6609 charset = NULL;
6610 }
6611
6612 /* TODO: List all relevant charsets if charset not specified. */
6613 if (!w32_to_x_font (&(lplf->elfLogFont), buf, 100, charset))
33d52f9c 6614 return (0);
ee78dc32 6615
5ca0cd71
GV
6616 if (NILP (*(lpef->pattern))
6617 || w32_font_match (buf, XSTRING (*(lpef->pattern))->data))
ee78dc32 6618 {
4587b026 6619 *lpef->tail = Fcons (Fcons (build_string (buf), width), Qnil);
8e713be6 6620 lpef->tail = &(XCDR (*lpef->tail));
ee78dc32
GV
6621 lpef->numFonts++;
6622 }
6623 }
6fc2811b 6624
ee78dc32
GV
6625 return (1);
6626}
6627
6628int CALLBACK
6629enum_font_cb1 (lplf, lptm, FontType, lpef)
6630 ENUMLOGFONT * lplf;
6631 NEWTEXTMETRIC * lptm;
6632 int FontType;
6633 enumfont_t * lpef;
6634{
6635 return EnumFontFamilies (lpef->hdc,
6636 lplf->elfLogFont.lfFaceName,
6637 (FONTENUMPROC) enum_font_cb2,
6638 (LPARAM) lpef);
6639}
6640
6641
5ca0cd71
GV
6642int CALLBACK
6643enum_fontex_cb2 (lplf, lptm, font_type, lpef)
6644 ENUMLOGFONTEX * lplf;
6645 NEWTEXTMETRICEX * lptm;
6646 int font_type;
6647 enumfont_t * lpef;
6648{
6649 /* We are not interested in the extra info we get back from the 'Ex
6650 version - only the fact that we get character set variations
6651 enumerated seperately. */
6652 return enum_font_cb2 ((ENUMLOGFONT *) lplf, (NEWTEXTMETRIC *) lptm,
6653 font_type, lpef);
6654}
6655
6656int CALLBACK
6657enum_fontex_cb1 (lplf, lptm, font_type, lpef)
6658 ENUMLOGFONTEX * lplf;
6659 NEWTEXTMETRICEX * lptm;
6660 int font_type;
6661 enumfont_t * lpef;
6662{
6663 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
6664 FARPROC enum_font_families_ex
6665 = GetProcAddress ( gdi32, "EnumFontFamiliesExA");
6666 /* We don't really expect EnumFontFamiliesEx to disappear once we
6667 get here, so don't bother handling it gracefully. */
6668 if (enum_font_families_ex == NULL)
6669 error ("gdi32.dll has disappeared!");
6670 return enum_font_families_ex (lpef->hdc,
6671 &lplf->elfLogFont,
6672 (FONTENUMPROC) enum_fontex_cb2,
6673 (LPARAM) lpef, 0);
6674}
6675
4587b026
GV
6676/* Interface to fontset handler. (adapted from mw32font.c in Meadow
6677 and xterm.c in Emacs 20.3) */
6678
5ca0cd71 6679Lisp_Object w32_list_bdf_fonts (Lisp_Object pattern, int max_names)
33d52f9c
GV
6680{
6681 char *fontname, *ptnstr;
6682 Lisp_Object list, tem, newlist = Qnil;
55dcfc15 6683 int n_fonts = 0;
33d52f9c
GV
6684
6685 list = Vw32_bdf_filename_alist;
6686 ptnstr = XSTRING (pattern)->data;
6687
8e713be6 6688 for ( ; CONSP (list); list = XCDR (list))
33d52f9c 6689 {
8e713be6 6690 tem = XCAR (list);
33d52f9c 6691 if (CONSP (tem))
8e713be6 6692 fontname = XSTRING (XCAR (tem))->data;
33d52f9c
GV
6693 else if (STRINGP (tem))
6694 fontname = XSTRING (tem)->data;
6695 else
6696 continue;
6697
6698 if (w32_font_match (fontname, ptnstr))
5ca0cd71 6699 {
8e713be6 6700 newlist = Fcons (XCAR (tem), newlist);
5ca0cd71
GV
6701 n_fonts++;
6702 if (n_fonts >= max_names)
6703 break;
6704 }
33d52f9c
GV
6705 }
6706
6707 return newlist;
6708}
6709
5ca0cd71
GV
6710Lisp_Object w32_list_synthesized_fonts (FRAME_PTR f, Lisp_Object pattern,
6711 int size, int max_names);
6712
4587b026
GV
6713/* Return a list of names of available fonts matching PATTERN on frame
6714 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
6715 to be listed. Frame F NULL means we have not yet created any
6716 frame, which means we can't get proper size info, as we don't have
6717 a device context to use for GetTextMetrics.
6718 MAXNAMES sets a limit on how many fonts to match. */
6719
6720Lisp_Object
6721w32_list_fonts (FRAME_PTR f, Lisp_Object pattern, int size, int maxnames )
6722{
6fc2811b 6723 Lisp_Object patterns, key = Qnil, tem, tpat;
4587b026 6724 Lisp_Object list = Qnil, newlist = Qnil, second_best = Qnil;
33d52f9c 6725 struct w32_display_info *dpyinfo = &one_w32_display_info;
5ca0cd71 6726 int n_fonts = 0;
396594fe 6727
4587b026
GV
6728 patterns = Fassoc (pattern, Valternate_fontname_alist);
6729 if (NILP (patterns))
6730 patterns = Fcons (pattern, Qnil);
6731
8e713be6 6732 for (; CONSP (patterns); patterns = XCDR (patterns))
4587b026
GV
6733 {
6734 enumfont_t ef;
767b1ff0 6735 int codepage;
4587b026 6736
8e713be6 6737 tpat = XCAR (patterns);
4587b026 6738
767b1ff0
JR
6739 if (!STRINGP (tpat))
6740 continue;
6741
6742 /* Avoid expensive EnumFontFamilies functions if we are not
6743 going to be able to output one of these anyway. */
6744 codepage = w32_codepage_for_font (XSTRING (tpat)->data);
6745 if (codepage != CP_8BIT && codepage != CP_UNICODE
ceb12877
AI
6746 && codepage != CP_DEFAULT && codepage != CP_UNKNOWN
6747 && !IsValidCodePage(codepage))
767b1ff0
JR
6748 continue;
6749
4587b026
GV
6750 /* See if we cached the result for this particular query.
6751 The cache is an alist of the form:
6752 ((PATTERN (FONTNAME . WIDTH) ...) ...)
6753 */
8e713be6 6754 if (tem = XCDR (dpyinfo->name_list_element),
33d52f9c 6755 !NILP (list = Fassoc (tpat, tem)))
4587b026
GV
6756 {
6757 list = Fcdr_safe (list);
6758 /* We have a cached list. Don't have to get the list again. */
6759 goto label_cached;
6760 }
6761
6762 BLOCK_INPUT;
6763 /* At first, put PATTERN in the cache. */
6764 list = Qnil;
33d52f9c
GV
6765 ef.pattern = &tpat;
6766 ef.tail = &list;
4587b026 6767 ef.numFonts = 0;
33d52f9c 6768
5ca0cd71
GV
6769 /* Use EnumFontFamiliesEx where it is available, as it knows
6770 about character sets. Fall back to EnumFontFamilies for
6771 older versions of NT that don't support the 'Ex function. */
767b1ff0 6772 x_to_w32_font (XSTRING (tpat)->data, &ef.logfont);
4587b026 6773 {
5ca0cd71
GV
6774 LOGFONT font_match_pattern;
6775 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
6776 FARPROC enum_font_families_ex
6777 = GetProcAddress ( gdi32, "EnumFontFamiliesExA");
6778
6779 /* We do our own pattern matching so we can handle wildcards. */
6780 font_match_pattern.lfFaceName[0] = 0;
6781 font_match_pattern.lfPitchAndFamily = 0;
6782 /* We can use the charset, because if it is a wildcard it will
6783 be DEFAULT_CHARSET anyway. */
6784 font_match_pattern.lfCharSet = ef.logfont.lfCharSet;
6785
33d52f9c 6786 ef.hdc = GetDC (dpyinfo->root_window);
4587b026 6787
5ca0cd71
GV
6788 if (enum_font_families_ex)
6789 enum_font_families_ex (ef.hdc,
6790 &font_match_pattern,
6791 (FONTENUMPROC) enum_fontex_cb1,
6792 (LPARAM) &ef, 0);
6793 else
6794 EnumFontFamilies (ef.hdc, NULL, (FONTENUMPROC) enum_font_cb1,
6795 (LPARAM)&ef);
4587b026 6796
33d52f9c 6797 ReleaseDC (dpyinfo->root_window, ef.hdc);
4587b026
GV
6798 }
6799
6800 UNBLOCK_INPUT;
6801
6802 /* Make a list of the fonts we got back.
6803 Store that in the font cache for the display. */
8e713be6 6804 XCDR (dpyinfo->name_list_element)
33d52f9c 6805 = Fcons (Fcons (tpat, list),
8e713be6 6806 XCDR (dpyinfo->name_list_element));
4587b026
GV
6807
6808 label_cached:
6809 if (NILP (list)) continue; /* Try the remaining alternatives. */
6810
6811 newlist = second_best = Qnil;
6812
6813 /* Make a list of the fonts that have the right width. */
8e713be6 6814 for (; CONSP (list); list = XCDR (list))
4587b026
GV
6815 {
6816 int found_size;
8e713be6 6817 tem = XCAR (list);
4587b026
GV
6818
6819 if (!CONSP (tem))
6820 continue;
8e713be6 6821 if (NILP (XCAR (tem)))
4587b026
GV
6822 continue;
6823 if (!size)
6824 {
8e713be6 6825 newlist = Fcons (XCAR (tem), newlist);
5ca0cd71
GV
6826 n_fonts++;
6827 if (n_fonts >= maxnames)
6828 break;
6829 else
6830 continue;
4587b026 6831 }
8e713be6 6832 if (!INTEGERP (XCDR (tem)))
4587b026
GV
6833 {
6834 /* Since we don't yet know the size of the font, we must
6835 load it and try GetTextMetrics. */
4587b026
GV
6836 W32FontStruct thisinfo;
6837 LOGFONT lf;
6838 HDC hdc;
6839 HANDLE oldobj;
6840
8e713be6 6841 if (!x_to_w32_font (XSTRING (XCAR (tem))->data, &lf))
4587b026
GV
6842 continue;
6843
6844 BLOCK_INPUT;
33d52f9c 6845 thisinfo.bdf = NULL;
4587b026
GV
6846 thisinfo.hfont = CreateFontIndirect (&lf);
6847 if (thisinfo.hfont == NULL)
6848 continue;
6849
6850 hdc = GetDC (dpyinfo->root_window);
6851 oldobj = SelectObject (hdc, thisinfo.hfont);
6852 if (GetTextMetrics (hdc, &thisinfo.tm))
8e713be6 6853 XCDR (tem) = make_number (FONT_WIDTH (&thisinfo));
4587b026 6854 else
8e713be6 6855 XCDR (tem) = make_number (0);
4587b026
GV
6856 SelectObject (hdc, oldobj);
6857 ReleaseDC (dpyinfo->root_window, hdc);
6858 DeleteObject(thisinfo.hfont);
6859 UNBLOCK_INPUT;
6860 }
8e713be6 6861 found_size = XINT (XCDR (tem));
4587b026 6862 if (found_size == size)
5ca0cd71 6863 {
8e713be6 6864 newlist = Fcons (XCAR (tem), newlist);
5ca0cd71
GV
6865 n_fonts++;
6866 if (n_fonts >= maxnames)
6867 break;
6868 }
4587b026
GV
6869 /* keep track of the closest matching size in case
6870 no exact match is found. */
6871 else if (found_size > 0)
6872 {
6873 if (NILP (second_best))
6874 second_best = tem;
5ca0cd71 6875
4587b026
GV
6876 else if (found_size < size)
6877 {
8e713be6
KR
6878 if (XINT (XCDR (second_best)) > size
6879 || XINT (XCDR (second_best)) < found_size)
4587b026
GV
6880 second_best = tem;
6881 }
6882 else
6883 {
8e713be6
KR
6884 if (XINT (XCDR (second_best)) > size
6885 && XINT (XCDR (second_best)) >
4587b026
GV
6886 found_size)
6887 second_best = tem;
6888 }
6889 }
6890 }
6891
6892 if (!NILP (newlist))
6893 break;
6894 else if (!NILP (second_best))
6895 {
8e713be6 6896 newlist = Fcons (XCAR (second_best), Qnil);
4587b026
GV
6897 break;
6898 }
6899 }
6900
33d52f9c 6901 /* Include any bdf fonts. */
5ca0cd71 6902 if (n_fonts < maxnames)
33d52f9c
GV
6903 {
6904 Lisp_Object combined[2];
5ca0cd71 6905 combined[0] = w32_list_bdf_fonts (pattern, maxnames - n_fonts);
33d52f9c
GV
6906 combined[1] = newlist;
6907 newlist = Fnconc(2, combined);
6908 }
6909
5ca0cd71
GV
6910 /* If we can't find a font that matches, check if Windows would be
6911 able to synthesize it from a different style. */
6fc2811b 6912 if (NILP (newlist) && !NILP (Vw32_enable_synthesized_fonts))
5ca0cd71
GV
6913 newlist = w32_list_synthesized_fonts (f, pattern, size, maxnames);
6914
4587b026
GV
6915 return newlist;
6916}
6917
5ca0cd71
GV
6918Lisp_Object
6919w32_list_synthesized_fonts (f, pattern, size, max_names)
6920 FRAME_PTR f;
6921 Lisp_Object pattern;
6922 int size;
6923 int max_names;
6924{
6925 int fields;
6926 char *full_pattn, *new_pattn, foundary[50], family[50], *pattn_part2;
6927 char style[20], slant;
6928 Lisp_Object matches, match, tem, synthed_matches = Qnil;
6929
6930 full_pattn = XSTRING (pattern)->data;
6931
6932 pattn_part2 = alloca (XSTRING (pattern)->size);
6933 /* Allow some space for wildcard expansion. */
6934 new_pattn = alloca (XSTRING (pattern)->size + 100);
6935
6936 fields = sscanf (full_pattn, "-%49[^-]-%49[^-]-%19[^-]-%c-%s",
6937 foundary, family, style, &slant, pattn_part2);
6938 if (fields == EOF || fields < 5)
6939 return Qnil;
6940
6941 /* If the style and slant are wildcards already there is no point
6942 checking again (and we don't want to keep recursing). */
6943 if (*style == '*' && slant == '*')
6944 return Qnil;
6945
6946 sprintf (new_pattn, "-%s-%s-*-*-%s", foundary, family, pattn_part2);
6947
6948 matches = w32_list_fonts (f, build_string (new_pattn), size, max_names);
6949
8e713be6 6950 for ( ; CONSP (matches); matches = XCDR (matches))
5ca0cd71 6951 {
8e713be6 6952 tem = XCAR (matches);
5ca0cd71
GV
6953 if (!STRINGP (tem))
6954 continue;
6955
6956 full_pattn = XSTRING (tem)->data;
6957 fields = sscanf (full_pattn, "-%49[^-]-%49[^-]-%*[^-]-%*c-%s",
6958 foundary, family, pattn_part2);
6959 if (fields == EOF || fields < 3)
6960 continue;
6961
6962 sprintf (new_pattn, "-%s-%s-%s-%c-%s", foundary, family, style,
6963 slant, pattn_part2);
6964
6965 synthed_matches = Fcons (build_string (new_pattn),
6966 synthed_matches);
6967 }
6968
6969 return synthed_matches;
6970}
6971
6972
4587b026
GV
6973/* Return a pointer to struct font_info of font FONT_IDX of frame F. */
6974struct font_info *
6975w32_get_font_info (f, font_idx)
6976 FRAME_PTR f;
6977 int font_idx;
6978{
6979 return (FRAME_W32_FONT_TABLE (f) + font_idx);
6980}
6981
6982
6983struct font_info*
6984w32_query_font (struct frame *f, char *fontname)
6985{
6986 int i;
6987 struct font_info *pfi;
6988
6989 pfi = FRAME_W32_FONT_TABLE (f);
6990
6991 for (i = 0; i < one_w32_display_info.n_fonts ;i++, pfi++)
6992 {
6993 if (strcmp(pfi->name, fontname) == 0) return pfi;
6994 }
6995
6996 return NULL;
6997}
6998
6999/* Find a CCL program for a font specified by FONTP, and set the member
7000 `encoder' of the structure. */
7001
7002void
7003w32_find_ccl_program (fontp)
7004 struct font_info *fontp;
7005{
3545439c 7006 Lisp_Object list, elt;
4587b026 7007
8e713be6 7008 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
4587b026 7009 {
8e713be6 7010 elt = XCAR (list);
4587b026 7011 if (CONSP (elt)
8e713be6
KR
7012 && STRINGP (XCAR (elt))
7013 && (fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
4587b026 7014 >= 0))
3545439c
KH
7015 break;
7016 }
7017 if (! NILP (list))
7018 {
17eedd00
KH
7019 struct ccl_program *ccl
7020 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
3545439c 7021
8e713be6 7022 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
3545439c
KH
7023 xfree (ccl);
7024 else
7025 fontp->font_encoder = ccl;
4587b026
GV
7026 }
7027}
7028
7029\f
6fc2811b
JR
7030DEFUN ("w32-find-bdf-fonts", Fw32_find_bdf_fonts, Sw32_find_bdf_fonts,
7031 1, 1, 0,
7032 "Return a list of BDF fonts in DIR, suitable for appending to\n\
767b1ff0 7033w32-bdf-filename-alist. Fonts which do not contain an xlfd description\n\
6fc2811b
JR
7034will not be included in the list. DIR may be a list of directories.")
7035 (directory)
7036 Lisp_Object directory;
7037{
7038 Lisp_Object list = Qnil;
7039 struct gcpro gcpro1, gcpro2;
ee78dc32 7040
6fc2811b
JR
7041 if (!CONSP (directory))
7042 return w32_find_bdf_fonts_in_dir (directory);
ee78dc32 7043
6fc2811b 7044 for ( ; CONSP (directory); directory = XCDR (directory))
ee78dc32 7045 {
6fc2811b
JR
7046 Lisp_Object pair[2];
7047 pair[0] = list;
7048 pair[1] = Qnil;
7049 GCPRO2 (directory, list);
7050 pair[1] = w32_find_bdf_fonts_in_dir( XCAR (directory) );
7051 list = Fnconc( 2, pair );
7052 UNGCPRO;
7053 }
7054 return list;
7055}
ee78dc32 7056
6fc2811b
JR
7057/* Find BDF files in a specified directory. (use GCPRO when calling,
7058 as this calls lisp to get a directory listing). */
7059Lisp_Object w32_find_bdf_fonts_in_dir( Lisp_Object directory )
7060{
7061 Lisp_Object filelist, list = Qnil;
7062 char fontname[100];
ee78dc32 7063
6fc2811b
JR
7064 if (!STRINGP(directory))
7065 return Qnil;
ee78dc32 7066
6fc2811b
JR
7067 filelist = Fdirectory_files (directory, Qt,
7068 build_string (".*\\.[bB][dD][fF]"), Qt);
ee78dc32 7069
6fc2811b 7070 for ( ; CONSP(filelist); filelist = XCDR (filelist))
ee78dc32 7071 {
6fc2811b
JR
7072 Lisp_Object filename = XCAR (filelist);
7073 if (w32_BDF_to_x_font (XSTRING (filename)->data, fontname, 100))
7074 store_in_alist (&list, build_string (fontname), filename);
7075 }
7076 return list;
7077}
ee78dc32 7078
6fc2811b
JR
7079\f
7080DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
dfff8a69 7081 "Internal function called by `color-defined-p', which see.")
6fc2811b
JR
7082 (color, frame)
7083 Lisp_Object color, frame;
7084{
7085 XColor foo;
7086 FRAME_PTR f = check_x_frame (frame);
ee78dc32 7087
6fc2811b 7088 CHECK_STRING (color, 1);
ee78dc32 7089
6fc2811b
JR
7090 if (w32_defined_color (f, XSTRING (color)->data, &foo, 0))
7091 return Qt;
7092 else
7093 return Qnil;
7094}
ee78dc32 7095
2d764c78 7096DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
dfff8a69 7097 "Internal function called by `color-values', which see.")
ee78dc32
GV
7098 (color, frame)
7099 Lisp_Object color, frame;
7100{
6fc2811b 7101 XColor foo;
ee78dc32
GV
7102 FRAME_PTR f = check_x_frame (frame);
7103
7104 CHECK_STRING (color, 1);
7105
6fc2811b 7106 if (w32_defined_color (f, XSTRING (color)->data, &foo, 0))
ee78dc32
GV
7107 {
7108 Lisp_Object rgb[3];
7109
6fc2811b
JR
7110 rgb[0] = make_number ((GetRValue (foo.pixel) << 8)
7111 | GetRValue (foo.pixel));
7112 rgb[1] = make_number ((GetGValue (foo.pixel) << 8)
7113 | GetGValue (foo.pixel));
7114 rgb[2] = make_number ((GetBValue (foo.pixel) << 8)
7115 | GetBValue (foo.pixel));
ee78dc32
GV
7116 return Flist (3, rgb);
7117 }
7118 else
7119 return Qnil;
7120}
7121
2d764c78 7122DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
dfff8a69 7123 "Internal function called by `display-color-p', which see.")
ee78dc32
GV
7124 (display)
7125 Lisp_Object display;
7126{
fbd6baed 7127 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
7128
7129 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 2)
7130 return Qnil;
7131
7132 return Qt;
7133}
7134
7135DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p, Sx_display_grayscale_p,
7136 0, 1, 0,
7137 "Return t if the X display supports shades of gray.\n\
7138Note that color displays do support shades of gray.\n\
7139The optional argument DISPLAY specifies which display to ask about.\n\
7140DISPLAY should be either a frame or a display name (a string).\n\
7141If omitted or nil, that stands for the selected frame's display.")
7142 (display)
7143 Lisp_Object display;
7144{
fbd6baed 7145 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
7146
7147 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 1)
7148 return Qnil;
7149
7150 return Qt;
7151}
7152
7153DEFUN ("x-display-pixel-width", Fx_display_pixel_width, Sx_display_pixel_width,
7154 0, 1, 0,
7155 "Returns the width in pixels of the X display DISPLAY.\n\
7156The optional argument DISPLAY specifies which display to ask about.\n\
7157DISPLAY should be either a frame or a display name (a string).\n\
7158If omitted or nil, that stands for the selected frame's display.")
7159 (display)
7160 Lisp_Object display;
7161{
fbd6baed 7162 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
7163
7164 return make_number (dpyinfo->width);
7165}
7166
7167DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
7168 Sx_display_pixel_height, 0, 1, 0,
7169 "Returns the height in pixels of the X display DISPLAY.\n\
7170The optional argument DISPLAY specifies which display to ask about.\n\
7171DISPLAY should be either a frame or a display name (a string).\n\
7172If omitted or nil, that stands for the selected frame's display.")
7173 (display)
7174 Lisp_Object display;
7175{
fbd6baed 7176 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
7177
7178 return make_number (dpyinfo->height);
7179}
7180
7181DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
7182 0, 1, 0,
7183 "Returns the number of bitplanes of the display DISPLAY.\n\
7184The optional argument DISPLAY specifies which display to ask about.\n\
7185DISPLAY should be either a frame or a display name (a string).\n\
7186If omitted or nil, that stands for the selected frame's display.")
7187 (display)
7188 Lisp_Object display;
7189{
fbd6baed 7190 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
7191
7192 return make_number (dpyinfo->n_planes * dpyinfo->n_cbits);
7193}
7194
7195DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
7196 0, 1, 0,
7197 "Returns the number of color cells of the display DISPLAY.\n\
7198The optional argument DISPLAY specifies which display to ask about.\n\
7199DISPLAY should be either a frame or a display name (a string).\n\
7200If omitted or nil, that stands for the selected frame's display.")
7201 (display)
7202 Lisp_Object display;
7203{
fbd6baed 7204 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
7205 HDC hdc;
7206 int cap;
7207
5ac45f98
GV
7208 hdc = GetDC (dpyinfo->root_window);
7209 if (dpyinfo->has_palette)
7210 cap = GetDeviceCaps (hdc,SIZEPALETTE);
7211 else
7212 cap = GetDeviceCaps (hdc,NUMCOLORS);
ee78dc32
GV
7213
7214 ReleaseDC (dpyinfo->root_window, hdc);
7215
7216 return make_number (cap);
7217}
7218
7219DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
7220 Sx_server_max_request_size,
7221 0, 1, 0,
7222 "Returns the maximum request size of the server of display DISPLAY.\n\
7223The optional argument DISPLAY specifies which display to ask about.\n\
7224DISPLAY should be either a frame or a display name (a string).\n\
7225If omitted or nil, that stands for the selected frame's display.")
7226 (display)
7227 Lisp_Object display;
7228{
fbd6baed 7229 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
7230
7231 return make_number (1);
7232}
7233
7234DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
fbd6baed 7235 "Returns the vendor ID string of the W32 system (Microsoft).\n\
ee78dc32
GV
7236The optional argument DISPLAY specifies which display to ask about.\n\
7237DISPLAY should be either a frame or a display name (a string).\n\
7238If omitted or nil, that stands for the selected frame's display.")
7239 (display)
7240 Lisp_Object display;
7241{
dfff8a69 7242 return build_string ("Microsoft Corp.");
ee78dc32
GV
7243}
7244
7245DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
7246 "Returns the version numbers of the server of display DISPLAY.\n\
7247The value is a list of three integers: the major and minor\n\
7248version numbers, and the vendor-specific release\n\
7249number. See also the function `x-server-vendor'.\n\n\
7250The optional argument DISPLAY specifies which display to ask about.\n\
7251DISPLAY should be either a frame or a display name (a string).\n\
7252If omitted or nil, that stands for the selected frame's display.")
7253 (display)
7254 Lisp_Object display;
7255{
fbd6baed 7256 return Fcons (make_number (w32_major_version),
58e0f0e4
AI
7257 Fcons (make_number (w32_minor_version),
7258 Fcons (make_number (w32_build_number), Qnil)));
ee78dc32
GV
7259}
7260
7261DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
7262 "Returns the number of screens on the server of display DISPLAY.\n\
7263The optional argument DISPLAY specifies which display to ask about.\n\
7264DISPLAY should be either a frame or a display name (a string).\n\
7265If omitted or nil, that stands for the selected frame's display.")
7266 (display)
7267 Lisp_Object display;
7268{
ee78dc32
GV
7269 return make_number (1);
7270}
7271
7272DEFUN ("x-display-mm-height", Fx_display_mm_height, Sx_display_mm_height, 0, 1, 0,
7273 "Returns the height in millimeters of the X display DISPLAY.\n\
7274The optional argument DISPLAY specifies which display to ask about.\n\
7275DISPLAY should be either a frame or a display name (a string).\n\
7276If omitted or nil, that stands for the selected frame's display.")
7277 (display)
7278 Lisp_Object display;
7279{
fbd6baed 7280 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
7281 HDC hdc;
7282 int cap;
7283
5ac45f98 7284 hdc = GetDC (dpyinfo->root_window);
3c190163 7285
ee78dc32 7286 cap = GetDeviceCaps (hdc, VERTSIZE);
3c190163 7287
ee78dc32
GV
7288 ReleaseDC (dpyinfo->root_window, hdc);
7289
7290 return make_number (cap);
7291}
7292
7293DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
7294 "Returns the width in millimeters of the X display DISPLAY.\n\
7295The optional argument DISPLAY specifies which display to ask about.\n\
7296DISPLAY should be either a frame or a display name (a string).\n\
7297If omitted or nil, that stands for the selected frame's display.")
7298 (display)
7299 Lisp_Object display;
7300{
fbd6baed 7301 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
7302
7303 HDC hdc;
7304 int cap;
7305
5ac45f98 7306 hdc = GetDC (dpyinfo->root_window);
3c190163 7307
ee78dc32 7308 cap = GetDeviceCaps (hdc, HORZSIZE);
3c190163 7309
ee78dc32
GV
7310 ReleaseDC (dpyinfo->root_window, hdc);
7311
7312 return make_number (cap);
7313}
7314
7315DEFUN ("x-display-backing-store", Fx_display_backing_store,
7316 Sx_display_backing_store, 0, 1, 0,
7317 "Returns an indication of whether display DISPLAY does backing store.\n\
7318The value may be `always', `when-mapped', or `not-useful'.\n\
7319The optional argument DISPLAY specifies which display to ask about.\n\
7320DISPLAY should be either a frame or a display name (a string).\n\
7321If omitted or nil, that stands for the selected frame's display.")
7322 (display)
7323 Lisp_Object display;
7324{
7325 return intern ("not-useful");
7326}
7327
7328DEFUN ("x-display-visual-class", Fx_display_visual_class,
7329 Sx_display_visual_class, 0, 1, 0,
7330 "Returns the visual class of the display DISPLAY.\n\
7331The value is one of the symbols `static-gray', `gray-scale',\n\
7332`static-color', `pseudo-color', `true-color', or `direct-color'.\n\n\
7333The optional argument DISPLAY specifies which display to ask about.\n\
7334DISPLAY should be either a frame or a display name (a string).\n\
7335If omitted or nil, that stands for the selected frame's display.")
7336 (display)
7337 Lisp_Object display;
7338{
fbd6baed 7339 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
7340
7341#if 0
7342 switch (dpyinfo->visual->class)
7343 {
7344 case StaticGray: return (intern ("static-gray"));
7345 case GrayScale: return (intern ("gray-scale"));
7346 case StaticColor: return (intern ("static-color"));
7347 case PseudoColor: return (intern ("pseudo-color"));
7348 case TrueColor: return (intern ("true-color"));
7349 case DirectColor: return (intern ("direct-color"));
7350 default:
7351 error ("Display has an unknown visual class");
7352 }
7353#endif
7354
7355 error ("Display has an unknown visual class");
7356}
7357
7358DEFUN ("x-display-save-under", Fx_display_save_under,
7359 Sx_display_save_under, 0, 1, 0,
7360 "Returns t if the display DISPLAY supports the save-under feature.\n\
7361The optional argument DISPLAY specifies which display to ask about.\n\
7362DISPLAY should be either a frame or a display name (a string).\n\
7363If omitted or nil, that stands for the selected frame's display.")
7364 (display)
7365 Lisp_Object display;
7366{
6fc2811b
JR
7367 return Qnil;
7368}
7369\f
7370int
7371x_pixel_width (f)
7372 register struct frame *f;
7373{
7374 return PIXEL_WIDTH (f);
7375}
7376
7377int
7378x_pixel_height (f)
7379 register struct frame *f;
7380{
7381 return PIXEL_HEIGHT (f);
7382}
7383
7384int
7385x_char_width (f)
7386 register struct frame *f;
7387{
7388 return FONT_WIDTH (f->output_data.w32->font);
7389}
7390
7391int
7392x_char_height (f)
7393 register struct frame *f;
7394{
7395 return f->output_data.w32->line_height;
7396}
7397
7398int
7399x_screen_planes (f)
7400 register struct frame *f;
7401{
7402 return FRAME_W32_DISPLAY_INFO (f)->n_planes;
7403}
7404\f
7405/* Return the display structure for the display named NAME.
7406 Open a new connection if necessary. */
7407
7408struct w32_display_info *
7409x_display_info_for_name (name)
7410 Lisp_Object name;
7411{
7412 Lisp_Object names;
7413 struct w32_display_info *dpyinfo;
7414
7415 CHECK_STRING (name, 0);
7416
7417 for (dpyinfo = &one_w32_display_info, names = w32_display_name_list;
7418 dpyinfo;
7419 dpyinfo = dpyinfo->next, names = XCDR (names))
7420 {
7421 Lisp_Object tem;
7422 tem = Fstring_equal (XCAR (XCAR (names)), name);
7423 if (!NILP (tem))
7424 return dpyinfo;
7425 }
7426
7427 /* Use this general default value to start with. */
7428 Vx_resource_name = Vinvocation_name;
7429
7430 validate_x_resource_name ();
7431
7432 dpyinfo = w32_term_init (name, (unsigned char *)0,
7433 (char *) XSTRING (Vx_resource_name)->data);
7434
7435 if (dpyinfo == 0)
7436 error ("Cannot connect to server %s", XSTRING (name)->data);
7437
7438 w32_in_use = 1;
7439 XSETFASTINT (Vwindow_system_version, 3);
7440
7441 return dpyinfo;
7442}
7443
7444DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
7445 1, 3, 0, "Open a connection to a server.\n\
7446DISPLAY is the name of the display to connect to.\n\
7447Optional second arg XRM-STRING is a string of resources in xrdb format.\n\
7448If the optional third arg MUST-SUCCEED is non-nil,\n\
7449terminate Emacs if we can't open the connection.")
7450 (display, xrm_string, must_succeed)
7451 Lisp_Object display, xrm_string, must_succeed;
7452{
7453 unsigned char *xrm_option;
7454 struct w32_display_info *dpyinfo;
7455
7456 CHECK_STRING (display, 0);
7457 if (! NILP (xrm_string))
7458 CHECK_STRING (xrm_string, 1);
7459
7460 if (! EQ (Vwindow_system, intern ("w32")))
7461 error ("Not using Microsoft Windows");
7462
7463 /* Allow color mapping to be defined externally; first look in user's
7464 HOME directory, then in Emacs etc dir for a file called rgb.txt. */
7465 {
7466 Lisp_Object color_file;
7467 struct gcpro gcpro1;
7468
7469 color_file = build_string("~/rgb.txt");
7470
7471 GCPRO1 (color_file);
7472
7473 if (NILP (Ffile_readable_p (color_file)))
7474 color_file =
7475 Fexpand_file_name (build_string ("rgb.txt"),
7476 Fsymbol_value (intern ("data-directory")));
7477
7478 Vw32_color_map = Fw32_load_color_file (color_file);
7479
7480 UNGCPRO;
7481 }
7482 if (NILP (Vw32_color_map))
7483 Vw32_color_map = Fw32_default_color_map ();
7484
7485 if (! NILP (xrm_string))
7486 xrm_option = (unsigned char *) XSTRING (xrm_string)->data;
7487 else
7488 xrm_option = (unsigned char *) 0;
7489
7490 /* Use this general default value to start with. */
7491 /* First remove .exe suffix from invocation-name - it looks ugly. */
7492 {
7493 char basename[ MAX_PATH ], *str;
7494
7495 strcpy (basename, XSTRING (Vinvocation_name)->data);
7496 str = strrchr (basename, '.');
7497 if (str) *str = 0;
7498 Vinvocation_name = build_string (basename);
7499 }
7500 Vx_resource_name = Vinvocation_name;
7501
7502 validate_x_resource_name ();
7503
7504 /* This is what opens the connection and sets x_current_display.
7505 This also initializes many symbols, such as those used for input. */
7506 dpyinfo = w32_term_init (display, xrm_option,
7507 (char *) XSTRING (Vx_resource_name)->data);
7508
7509 if (dpyinfo == 0)
7510 {
7511 if (!NILP (must_succeed))
7512 fatal ("Cannot connect to server %s.\n",
7513 XSTRING (display)->data);
7514 else
7515 error ("Cannot connect to server %s", XSTRING (display)->data);
7516 }
7517
7518 w32_in_use = 1;
7519
7520 XSETFASTINT (Vwindow_system_version, 3);
7521 return Qnil;
7522}
7523
7524DEFUN ("x-close-connection", Fx_close_connection,
7525 Sx_close_connection, 1, 1, 0,
7526 "Close the connection to DISPLAY's server.\n\
7527For DISPLAY, specify either a frame or a display name (a string).\n\
7528If DISPLAY is nil, that stands for the selected frame's display.")
7529 (display)
7530 Lisp_Object display;
7531{
7532 struct w32_display_info *dpyinfo = check_x_display_info (display);
7533 int i;
7534
7535 if (dpyinfo->reference_count > 0)
7536 error ("Display still has frames on it");
7537
7538 BLOCK_INPUT;
7539 /* Free the fonts in the font table. */
7540 for (i = 0; i < dpyinfo->n_fonts; i++)
7541 if (dpyinfo->font_table[i].name)
7542 {
126f2e35
JR
7543 if (dpyinfo->font_table[i].name != dpyinfo->font_table[i].full_name)
7544 xfree (dpyinfo->font_table[i].full_name);
6fc2811b 7545 xfree (dpyinfo->font_table[i].name);
6fc2811b
JR
7546 w32_unload_font (dpyinfo, dpyinfo->font_table[i].font);
7547 }
7548 x_destroy_all_bitmaps (dpyinfo);
7549
7550 x_delete_display (dpyinfo);
7551 UNBLOCK_INPUT;
7552
7553 return Qnil;
7554}
7555
7556DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
7557 "Return the list of display names that Emacs has connections to.")
7558 ()
7559{
7560 Lisp_Object tail, result;
7561
7562 result = Qnil;
7563 for (tail = w32_display_name_list; ! NILP (tail); tail = XCDR (tail))
7564 result = Fcons (XCAR (XCAR (tail)), result);
7565
7566 return result;
7567}
7568
7569DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
7570 "If ON is non-nil, report errors as soon as the erring request is made.\n\
7571If ON is nil, allow buffering of requests.\n\
7572This is a noop on W32 systems.\n\
7573The optional second argument DISPLAY specifies which display to act on.\n\
7574DISPLAY should be either a frame or a display name (a string).\n\
7575If DISPLAY is omitted or nil, that stands for the selected frame's display.")
7576 (on, display)
7577 Lisp_Object display, on;
7578{
6fc2811b
JR
7579 return Qnil;
7580}
7581
7582\f
7583\f
7584/***********************************************************************
7585 Image types
7586 ***********************************************************************/
7587
7588/* Value is the number of elements of vector VECTOR. */
7589
7590#define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR))
7591
7592/* List of supported image types. Use define_image_type to add new
7593 types. Use lookup_image_type to find a type for a given symbol. */
7594
7595static struct image_type *image_types;
7596
6fc2811b
JR
7597/* The symbol `image' which is the car of the lists used to represent
7598 images in Lisp. */
7599
7600extern Lisp_Object Qimage;
7601
7602/* The symbol `xbm' which is used as the type symbol for XBM images. */
7603
7604Lisp_Object Qxbm;
7605
7606/* Keywords. */
7607
6fc2811b 7608extern Lisp_Object QCwidth, QCheight, QCforeground, QCbackground, QCfile;
dfff8a69
JR
7609extern Lisp_Object QCdata;
7610Lisp_Object QCtype, QCascent, QCmargin, QCrelief;
6fc2811b 7611Lisp_Object QCalgorithm, QCcolor_symbols, QCheuristic_mask;
dfff8a69 7612Lisp_Object QCindex;
6fc2811b
JR
7613
7614/* Other symbols. */
7615
7616Lisp_Object Qlaplace;
7617
7618/* Time in seconds after which images should be removed from the cache
7619 if not displayed. */
7620
7621Lisp_Object Vimage_cache_eviction_delay;
7622
7623/* Function prototypes. */
7624
7625static void define_image_type P_ ((struct image_type *type));
7626static struct image_type *lookup_image_type P_ ((Lisp_Object symbol));
7627static void image_error P_ ((char *format, Lisp_Object, Lisp_Object));
7628static void x_laplace P_ ((struct frame *, struct image *));
7629static int x_build_heuristic_mask P_ ((struct frame *, struct image *,
7630 Lisp_Object));
7631
dfff8a69 7632
6fc2811b
JR
7633/* Define a new image type from TYPE. This adds a copy of TYPE to
7634 image_types and adds the symbol *TYPE->type to Vimage_types. */
7635
7636static void
7637define_image_type (type)
7638 struct image_type *type;
7639{
7640 /* Make a copy of TYPE to avoid a bus error in a dumped Emacs.
7641 The initialized data segment is read-only. */
7642 struct image_type *p = (struct image_type *) xmalloc (sizeof *p);
7643 bcopy (type, p, sizeof *p);
7644 p->next = image_types;
7645 image_types = p;
7646 Vimage_types = Fcons (*p->type, Vimage_types);
7647}
7648
7649
7650/* Look up image type SYMBOL, and return a pointer to its image_type
7651 structure. Value is null if SYMBOL is not a known image type. */
7652
7653static INLINE struct image_type *
7654lookup_image_type (symbol)
7655 Lisp_Object symbol;
7656{
7657 struct image_type *type;
7658
7659 for (type = image_types; type; type = type->next)
7660 if (EQ (symbol, *type->type))
7661 break;
7662
7663 return type;
7664}
7665
7666
7667/* Value is non-zero if OBJECT is a valid Lisp image specification. A
7668 valid image specification is a list whose car is the symbol
7669 `image', and whose rest is a property list. The property list must
7670 contain a value for key `:type'. That value must be the name of a
7671 supported image type. The rest of the property list depends on the
7672 image type. */
7673
7674int
7675valid_image_p (object)
7676 Lisp_Object object;
7677{
7678 int valid_p = 0;
7679
7680 if (CONSP (object) && EQ (XCAR (object), Qimage))
7681 {
7682 Lisp_Object symbol = Fplist_get (XCDR (object), QCtype);
7683 struct image_type *type = lookup_image_type (symbol);
7684
7685 if (type)
7686 valid_p = type->valid_p (object);
7687 }
7688
7689 return valid_p;
7690}
7691
7692
7693/* Log error message with format string FORMAT and argument ARG.
7694 Signaling an error, e.g. when an image cannot be loaded, is not a
7695 good idea because this would interrupt redisplay, and the error
7696 message display would lead to another redisplay. This function
7697 therefore simply displays a message. */
7698
7699static void
7700image_error (format, arg1, arg2)
7701 char *format;
7702 Lisp_Object arg1, arg2;
7703{
7704 add_to_log (format, arg1, arg2);
7705}
7706
7707
7708\f
7709/***********************************************************************
7710 Image specifications
7711 ***********************************************************************/
7712
7713enum image_value_type
7714{
7715 IMAGE_DONT_CHECK_VALUE_TYPE,
7716 IMAGE_STRING_VALUE,
7717 IMAGE_SYMBOL_VALUE,
7718 IMAGE_POSITIVE_INTEGER_VALUE,
7719 IMAGE_NON_NEGATIVE_INTEGER_VALUE,
dfff8a69 7720 IMAGE_ASCENT_VALUE,
6fc2811b
JR
7721 IMAGE_INTEGER_VALUE,
7722 IMAGE_FUNCTION_VALUE,
7723 IMAGE_NUMBER_VALUE,
7724 IMAGE_BOOL_VALUE
7725};
7726
7727/* Structure used when parsing image specifications. */
7728
7729struct image_keyword
7730{
7731 /* Name of keyword. */
7732 char *name;
7733
7734 /* The type of value allowed. */
7735 enum image_value_type type;
7736
7737 /* Non-zero means key must be present. */
7738 int mandatory_p;
7739
7740 /* Used to recognize duplicate keywords in a property list. */
7741 int count;
7742
7743 /* The value that was found. */
7744 Lisp_Object value;
7745};
7746
7747
7748static int parse_image_spec P_ ((Lisp_Object, struct image_keyword *,
7749 int, Lisp_Object));
7750static Lisp_Object image_spec_value P_ ((Lisp_Object, Lisp_Object, int *));
7751
7752
7753/* Parse image spec SPEC according to KEYWORDS. A valid image spec
7754 has the format (image KEYWORD VALUE ...). One of the keyword/
7755 value pairs must be `:type TYPE'. KEYWORDS is a vector of
7756 image_keywords structures of size NKEYWORDS describing other
7757 allowed keyword/value pairs. Value is non-zero if SPEC is valid. */
7758
7759static int
7760parse_image_spec (spec, keywords, nkeywords, type)
7761 Lisp_Object spec;
7762 struct image_keyword *keywords;
7763 int nkeywords;
7764 Lisp_Object type;
7765{
7766 int i;
7767 Lisp_Object plist;
7768
7769 if (!CONSP (spec) || !EQ (XCAR (spec), Qimage))
7770 return 0;
7771
7772 plist = XCDR (spec);
7773 while (CONSP (plist))
7774 {
7775 Lisp_Object key, value;
7776
7777 /* First element of a pair must be a symbol. */
7778 key = XCAR (plist);
7779 plist = XCDR (plist);
7780 if (!SYMBOLP (key))
7781 return 0;
7782
7783 /* There must follow a value. */
7784 if (!CONSP (plist))
7785 return 0;
7786 value = XCAR (plist);
7787 plist = XCDR (plist);
7788
7789 /* Find key in KEYWORDS. Error if not found. */
7790 for (i = 0; i < nkeywords; ++i)
7791 if (strcmp (keywords[i].name, XSYMBOL (key)->name->data) == 0)
7792 break;
7793
7794 if (i == nkeywords)
7795 continue;
7796
7797 /* Record that we recognized the keyword. If a keywords
7798 was found more than once, it's an error. */
7799 keywords[i].value = value;
7800 ++keywords[i].count;
7801
7802 if (keywords[i].count > 1)
7803 return 0;
7804
7805 /* Check type of value against allowed type. */
7806 switch (keywords[i].type)
7807 {
7808 case IMAGE_STRING_VALUE:
7809 if (!STRINGP (value))
7810 return 0;
7811 break;
7812
7813 case IMAGE_SYMBOL_VALUE:
7814 if (!SYMBOLP (value))
7815 return 0;
7816 break;
7817
7818 case IMAGE_POSITIVE_INTEGER_VALUE:
7819 if (!INTEGERP (value) || XINT (value) <= 0)
7820 return 0;
7821 break;
7822
dfff8a69
JR
7823 case IMAGE_ASCENT_VALUE:
7824 if (SYMBOLP (value) && EQ (value, Qcenter))
7825 break;
7826 else if (INTEGERP (value)
7827 && XINT (value) >= 0
7828 && XINT (value) <= 100)
7829 break;
7830 return 0;
7831
6fc2811b
JR
7832 case IMAGE_NON_NEGATIVE_INTEGER_VALUE:
7833 if (!INTEGERP (value) || XINT (value) < 0)
7834 return 0;
7835 break;
7836
7837 case IMAGE_DONT_CHECK_VALUE_TYPE:
7838 break;
7839
7840 case IMAGE_FUNCTION_VALUE:
7841 value = indirect_function (value);
7842 if (SUBRP (value)
7843 || COMPILEDP (value)
7844 || (CONSP (value) && EQ (XCAR (value), Qlambda)))
7845 break;
7846 return 0;
7847
7848 case IMAGE_NUMBER_VALUE:
7849 if (!INTEGERP (value) && !FLOATP (value))
7850 return 0;
7851 break;
7852
7853 case IMAGE_INTEGER_VALUE:
7854 if (!INTEGERP (value))
7855 return 0;
7856 break;
7857
7858 case IMAGE_BOOL_VALUE:
7859 if (!NILP (value) && !EQ (value, Qt))
7860 return 0;
7861 break;
7862
7863 default:
7864 abort ();
7865 break;
7866 }
7867
7868 if (EQ (key, QCtype) && !EQ (type, value))
7869 return 0;
7870 }
7871
7872 /* Check that all mandatory fields are present. */
7873 for (i = 0; i < nkeywords; ++i)
7874 if (keywords[i].mandatory_p && keywords[i].count == 0)
7875 return 0;
7876
7877 return NILP (plist);
7878}
7879
7880
7881/* Return the value of KEY in image specification SPEC. Value is nil
7882 if KEY is not present in SPEC. if FOUND is not null, set *FOUND
7883 to 1 if KEY was found in SPEC, set it to 0 otherwise. */
7884
7885static Lisp_Object
7886image_spec_value (spec, key, found)
7887 Lisp_Object spec, key;
7888 int *found;
7889{
7890 Lisp_Object tail;
7891
7892 xassert (valid_image_p (spec));
7893
7894 for (tail = XCDR (spec);
7895 CONSP (tail) && CONSP (XCDR (tail));
7896 tail = XCDR (XCDR (tail)))
7897 {
7898 if (EQ (XCAR (tail), key))
7899 {
7900 if (found)
7901 *found = 1;
7902 return XCAR (XCDR (tail));
7903 }
7904 }
7905
7906 if (found)
7907 *found = 0;
7908 return Qnil;
7909}
7910
7911
7912
7913\f
7914/***********************************************************************
7915 Image type independent image structures
7916 ***********************************************************************/
7917
7918static struct image *make_image P_ ((Lisp_Object spec, unsigned hash));
7919static void free_image P_ ((struct frame *f, struct image *img));
7920
7921
7922/* Allocate and return a new image structure for image specification
7923 SPEC. SPEC has a hash value of HASH. */
7924
7925static struct image *
7926make_image (spec, hash)
7927 Lisp_Object spec;
7928 unsigned hash;
7929{
7930 struct image *img = (struct image *) xmalloc (sizeof *img);
7931
7932 xassert (valid_image_p (spec));
7933 bzero (img, sizeof *img);
7934 img->type = lookup_image_type (image_spec_value (spec, QCtype, NULL));
7935 xassert (img->type != NULL);
7936 img->spec = spec;
7937 img->data.lisp_val = Qnil;
7938 img->ascent = DEFAULT_IMAGE_ASCENT;
7939 img->hash = hash;
7940 return img;
7941}
7942
7943
7944/* Free image IMG which was used on frame F, including its resources. */
7945
7946static void
7947free_image (f, img)
7948 struct frame *f;
7949 struct image *img;
7950{
7951 if (img)
7952 {
7953 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
7954
7955 /* Remove IMG from the hash table of its cache. */
7956 if (img->prev)
7957 img->prev->next = img->next;
7958 else
7959 c->buckets[img->hash % IMAGE_CACHE_BUCKETS_SIZE] = img->next;
7960
7961 if (img->next)
7962 img->next->prev = img->prev;
7963
7964 c->images[img->id] = NULL;
7965
7966 /* Free resources, then free IMG. */
7967 img->type->free (f, img);
7968 xfree (img);
7969 }
7970}
7971
7972
7973/* Prepare image IMG for display on frame F. Must be called before
7974 drawing an image. */
7975
7976void
7977prepare_image_for_display (f, img)
7978 struct frame *f;
7979 struct image *img;
7980{
7981 EMACS_TIME t;
7982
7983 /* We're about to display IMG, so set its timestamp to `now'. */
7984 EMACS_GET_TIME (t);
7985 img->timestamp = EMACS_SECS (t);
7986
7987 /* If IMG doesn't have a pixmap yet, load it now, using the image
7988 type dependent loader function. */
7989 if (img->pixmap == 0 && !img->load_failed_p)
7990 img->load_failed_p = img->type->load (f, img) == 0;
7991}
7992
7993
dfff8a69
JR
7994/* Value is the number of pixels for the ascent of image IMG when
7995 drawn in face FACE. */
7996
7997int
7998image_ascent (img, face)
7999 struct image *img;
8000 struct face *face;
8001{
8002 int height = img->height + img->margin;
8003 int ascent;
8004
8005 if (img->ascent == CENTERED_IMAGE_ASCENT)
8006 {
8007 if (face->font)
8008 ascent = height / 2 - (FONT_DESCENT(face->font)
8009 - FONT_BASE(face->font)) / 2;
8010 else
8011 ascent = height / 2;
8012 }
8013 else
8014 ascent = height * img->ascent / 100.0;
8015
8016 return ascent;
8017}
8018
8019
6fc2811b
JR
8020\f
8021/***********************************************************************
8022 Helper functions for X image types
8023 ***********************************************************************/
8024
8025static void x_clear_image P_ ((struct frame *f, struct image *img));
8026static unsigned long x_alloc_image_color P_ ((struct frame *f,
8027 struct image *img,
8028 Lisp_Object color_name,
8029 unsigned long dflt));
8030
8031/* Free X resources of image IMG which is used on frame F. */
8032
8033static void
8034x_clear_image (f, img)
8035 struct frame *f;
8036 struct image *img;
8037{
767b1ff0 8038#if 0 /* TODO: W32 image support */
6fc2811b
JR
8039
8040 if (img->pixmap)
8041 {
8042 BLOCK_INPUT;
8043 XFreePixmap (NULL, img->pixmap);
8044 img->pixmap = 0;
8045 UNBLOCK_INPUT;
8046 }
8047
8048 if (img->ncolors)
8049 {
8050 int class = FRAME_W32_DISPLAY_INFO (f)->visual->class;
8051
8052 /* If display has an immutable color map, freeing colors is not
8053 necessary and some servers don't allow it. So don't do it. */
8054 if (class != StaticColor
8055 && class != StaticGray
8056 && class != TrueColor)
8057 {
8058 Colormap cmap;
8059 BLOCK_INPUT;
8060 cmap = DefaultColormapOfScreen (FRAME_W32_DISPLAY_INFO (f)->screen);
8061 XFreeColors (FRAME_W32_DISPLAY (f), cmap, img->colors,
8062 img->ncolors, 0);
8063 UNBLOCK_INPUT;
8064 }
8065
8066 xfree (img->colors);
8067 img->colors = NULL;
8068 img->ncolors = 0;
8069 }
8070#endif
8071}
8072
8073
8074/* Allocate color COLOR_NAME for image IMG on frame F. If color
8075 cannot be allocated, use DFLT. Add a newly allocated color to
8076 IMG->colors, so that it can be freed again. Value is the pixel
8077 color. */
8078
8079static unsigned long
8080x_alloc_image_color (f, img, color_name, dflt)
8081 struct frame *f;
8082 struct image *img;
8083 Lisp_Object color_name;
8084 unsigned long dflt;
8085{
767b1ff0 8086#if 0 /* TODO: allocing colors. */
6fc2811b
JR
8087 XColor color;
8088 unsigned long result;
8089
8090 xassert (STRINGP (color_name));
8091
8092 if (w32_defined_color (f, XSTRING (color_name)->data, &color, 1))
8093 {
8094 /* This isn't called frequently so we get away with simply
8095 reallocating the color vector to the needed size, here. */
8096 ++img->ncolors;
8097 img->colors =
8098 (unsigned long *) xrealloc (img->colors,
8099 img->ncolors * sizeof *img->colors);
8100 img->colors[img->ncolors - 1] = color.pixel;
8101 result = color.pixel;
8102 }
8103 else
8104 result = dflt;
8105 return result;
8106#endif
8107 return 0;
8108}
8109
8110
8111\f
8112/***********************************************************************
8113 Image Cache
8114 ***********************************************************************/
8115
8116static void cache_image P_ ((struct frame *f, struct image *img));
8117
8118
8119/* Return a new, initialized image cache that is allocated from the
8120 heap. Call free_image_cache to free an image cache. */
8121
8122struct image_cache *
8123make_image_cache ()
8124{
8125 struct image_cache *c = (struct image_cache *) xmalloc (sizeof *c);
8126 int size;
8127
8128 bzero (c, sizeof *c);
8129 c->size = 50;
8130 c->images = (struct image **) xmalloc (c->size * sizeof *c->images);
8131 size = IMAGE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
8132 c->buckets = (struct image **) xmalloc (size);
8133 bzero (c->buckets, size);
8134 return c;
8135}
8136
8137
8138/* Free image cache of frame F. Be aware that X frames share images
8139 caches. */
8140
8141void
8142free_image_cache (f)
8143 struct frame *f;
8144{
8145 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
8146 if (c)
8147 {
8148 int i;
8149
8150 /* Cache should not be referenced by any frame when freed. */
8151 xassert (c->refcount == 0);
8152
8153 for (i = 0; i < c->used; ++i)
8154 free_image (f, c->images[i]);
8155 xfree (c->images);
8156 xfree (c);
8157 xfree (c->buckets);
8158 FRAME_X_IMAGE_CACHE (f) = NULL;
8159 }
8160}
8161
8162
8163/* Clear image cache of frame F. FORCE_P non-zero means free all
8164 images. FORCE_P zero means clear only images that haven't been
8165 displayed for some time. Should be called from time to time to
dfff8a69
JR
8166 reduce the number of loaded images. If image-eviction-seconds is
8167 non-nil, this frees images in the cache which weren't displayed for
6fc2811b
JR
8168 at least that many seconds. */
8169
8170void
8171clear_image_cache (f, force_p)
8172 struct frame *f;
8173 int force_p;
8174{
8175 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
8176
8177 if (c && INTEGERP (Vimage_cache_eviction_delay))
8178 {
8179 EMACS_TIME t;
8180 unsigned long old;
8181 int i, any_freed_p = 0;
8182
8183 EMACS_GET_TIME (t);
8184 old = EMACS_SECS (t) - XFASTINT (Vimage_cache_eviction_delay);
8185
8186 for (i = 0; i < c->used; ++i)
8187 {
8188 struct image *img = c->images[i];
8189 if (img != NULL
8190 && (force_p
8191 || (img->timestamp > old)))
8192 {
8193 free_image (f, img);
8194 any_freed_p = 1;
8195 }
8196 }
8197
8198 /* We may be clearing the image cache because, for example,
8199 Emacs was iconified for a longer period of time. In that
8200 case, current matrices may still contain references to
8201 images freed above. So, clear these matrices. */
8202 if (any_freed_p)
8203 {
8204 clear_current_matrices (f);
8205 ++windows_or_buffers_changed;
8206 }
8207 }
8208}
8209
8210
8211DEFUN ("clear-image-cache", Fclear_image_cache, Sclear_image_cache,
8212 0, 1, 0,
8213 "Clear the image cache of FRAME.\n\
8214FRAME nil or omitted means use the selected frame.\n\
8215FRAME t means clear the image caches of all frames.")
8216 (frame)
8217 Lisp_Object frame;
8218{
8219 if (EQ (frame, Qt))
8220 {
8221 Lisp_Object tail;
8222
8223 FOR_EACH_FRAME (tail, frame)
8224 if (FRAME_W32_P (XFRAME (frame)))
8225 clear_image_cache (XFRAME (frame), 1);
8226 }
8227 else
8228 clear_image_cache (check_x_frame (frame), 1);
8229
8230 return Qnil;
8231}
8232
8233
8234/* Return the id of image with Lisp specification SPEC on frame F.
8235 SPEC must be a valid Lisp image specification (see valid_image_p). */
8236
8237int
8238lookup_image (f, spec)
8239 struct frame *f;
8240 Lisp_Object spec;
8241{
8242 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
8243 struct image *img;
8244 int i;
8245 unsigned hash;
8246 struct gcpro gcpro1;
8247 EMACS_TIME now;
8248
8249 /* F must be a window-system frame, and SPEC must be a valid image
8250 specification. */
8251 xassert (FRAME_WINDOW_P (f));
8252 xassert (valid_image_p (spec));
8253
8254 GCPRO1 (spec);
8255
8256 /* Look up SPEC in the hash table of the image cache. */
8257 hash = sxhash (spec, 0);
8258 i = hash % IMAGE_CACHE_BUCKETS_SIZE;
8259
8260 for (img = c->buckets[i]; img; img = img->next)
8261 if (img->hash == hash && !NILP (Fequal (img->spec, spec)))
8262 break;
8263
8264 /* If not found, create a new image and cache it. */
8265 if (img == NULL)
8266 {
8267 img = make_image (spec, hash);
8268 cache_image (f, img);
8269 img->load_failed_p = img->type->load (f, img) == 0;
8270 xassert (!interrupt_input_blocked);
8271
8272 /* If we can't load the image, and we don't have a width and
8273 height, use some arbitrary width and height so that we can
8274 draw a rectangle for it. */
8275 if (img->load_failed_p)
8276 {
8277 Lisp_Object value;
8278
8279 value = image_spec_value (spec, QCwidth, NULL);
8280 img->width = (INTEGERP (value)
8281 ? XFASTINT (value) : DEFAULT_IMAGE_WIDTH);
8282 value = image_spec_value (spec, QCheight, NULL);
8283 img->height = (INTEGERP (value)
8284 ? XFASTINT (value) : DEFAULT_IMAGE_HEIGHT);
8285 }
8286 else
8287 {
8288 /* Handle image type independent image attributes
8289 `:ascent PERCENT', `:margin MARGIN', `:relief RELIEF'. */
8290 Lisp_Object ascent, margin, relief, algorithm, heuristic_mask;
8291 Lisp_Object file;
8292
8293 ascent = image_spec_value (spec, QCascent, NULL);
8294 if (INTEGERP (ascent))
8295 img->ascent = XFASTINT (ascent);
dfff8a69
JR
8296 else if (EQ (ascent, Qcenter))
8297 img->ascent = CENTERED_IMAGE_ASCENT;
8298
6fc2811b
JR
8299 margin = image_spec_value (spec, QCmargin, NULL);
8300 if (INTEGERP (margin) && XINT (margin) >= 0)
8301 img->margin = XFASTINT (margin);
8302
8303 relief = image_spec_value (spec, QCrelief, NULL);
8304 if (INTEGERP (relief))
8305 {
8306 img->relief = XINT (relief);
8307 img->margin += abs (img->relief);
8308 }
8309
8310 /* Should we apply a Laplace edge-detection algorithm? */
8311 algorithm = image_spec_value (spec, QCalgorithm, NULL);
8312 if (img->pixmap && EQ (algorithm, Qlaplace))
8313 x_laplace (f, img);
8314
8315 /* Should we built a mask heuristically? */
8316 heuristic_mask = image_spec_value (spec, QCheuristic_mask, NULL);
8317 if (img->pixmap && !img->mask && !NILP (heuristic_mask))
8318 x_build_heuristic_mask (f, img, heuristic_mask);
8319 }
8320 }
8321
8322 /* We're using IMG, so set its timestamp to `now'. */
8323 EMACS_GET_TIME (now);
8324 img->timestamp = EMACS_SECS (now);
8325
8326 UNGCPRO;
8327
8328 /* Value is the image id. */
8329 return img->id;
8330}
8331
8332
8333/* Cache image IMG in the image cache of frame F. */
8334
8335static void
8336cache_image (f, img)
8337 struct frame *f;
8338 struct image *img;
8339{
8340 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
8341 int i;
8342
8343 /* Find a free slot in c->images. */
8344 for (i = 0; i < c->used; ++i)
8345 if (c->images[i] == NULL)
8346 break;
8347
8348 /* If no free slot found, maybe enlarge c->images. */
8349 if (i == c->used && c->used == c->size)
8350 {
8351 c->size *= 2;
8352 c->images = (struct image **) xrealloc (c->images,
8353 c->size * sizeof *c->images);
8354 }
8355
8356 /* Add IMG to c->images, and assign IMG an id. */
8357 c->images[i] = img;
8358 img->id = i;
8359 if (i == c->used)
8360 ++c->used;
8361
8362 /* Add IMG to the cache's hash table. */
8363 i = img->hash % IMAGE_CACHE_BUCKETS_SIZE;
8364 img->next = c->buckets[i];
8365 if (img->next)
8366 img->next->prev = img;
8367 img->prev = NULL;
8368 c->buckets[i] = img;
8369}
8370
8371
8372/* Call FN on every image in the image cache of frame F. Used to mark
8373 Lisp Objects in the image cache. */
8374
8375void
8376forall_images_in_image_cache (f, fn)
8377 struct frame *f;
8378 void (*fn) P_ ((struct image *img));
8379{
8380 if (FRAME_LIVE_P (f) && FRAME_W32_P (f))
8381 {
8382 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
8383 if (c)
8384 {
8385 int i;
8386 for (i = 0; i < c->used; ++i)
8387 if (c->images[i])
8388 fn (c->images[i]);
8389 }
8390 }
8391}
8392
8393
8394\f
8395/***********************************************************************
8396 W32 support code
8397 ***********************************************************************/
8398
767b1ff0 8399#if 0 /* TODO: W32 specific image code. */
6fc2811b
JR
8400
8401static int x_create_x_image_and_pixmap P_ ((struct frame *, int, int, int,
8402 XImage **, Pixmap *));
8403static void x_destroy_x_image P_ ((XImage *));
8404static void x_put_x_image P_ ((struct frame *, XImage *, Pixmap, int, int));
8405
8406
8407/* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on
8408 frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created.
8409 Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated
8410 via xmalloc. Print error messages via image_error if an error
8411 occurs. Value is non-zero if successful. */
8412
8413static int
8414x_create_x_image_and_pixmap (f, width, height, depth, ximg, pixmap)
8415 struct frame *f;
8416 int width, height, depth;
8417 XImage **ximg;
8418 Pixmap *pixmap;
8419{
767b1ff0 8420#if 0 /* TODO: Image support for W32 */
6fc2811b
JR
8421 Display *display = FRAME_W32_DISPLAY (f);
8422 Screen *screen = FRAME_X_SCREEN (f);
8423 Window window = FRAME_W32_WINDOW (f);
8424
8425 xassert (interrupt_input_blocked);
8426
8427 if (depth <= 0)
8428 depth = DefaultDepthOfScreen (screen);
8429 *ximg = XCreateImage (display, DefaultVisualOfScreen (screen),
8430 depth, ZPixmap, 0, NULL, width, height,
8431 depth > 16 ? 32 : depth > 8 ? 16 : 8, 0);
8432 if (*ximg == NULL)
8433 {
8434 image_error ("Unable to allocate X image", Qnil, Qnil);
8435 return 0;
8436 }
8437
8438 /* Allocate image raster. */
8439 (*ximg)->data = (char *) xmalloc ((*ximg)->bytes_per_line * height);
8440
8441 /* Allocate a pixmap of the same size. */
8442 *pixmap = XCreatePixmap (display, window, width, height, depth);
8443 if (*pixmap == 0)
8444 {
8445 x_destroy_x_image (*ximg);
8446 *ximg = NULL;
8447 image_error ("Unable to create X pixmap", Qnil, Qnil);
8448 return 0;
8449 }
8450#endif
8451 return 1;
8452}
8453
8454
8455/* Destroy XImage XIMG. Free XIMG->data. */
8456
8457static void
8458x_destroy_x_image (ximg)
8459 XImage *ximg;
8460{
8461 xassert (interrupt_input_blocked);
8462 if (ximg)
8463 {
8464 xfree (ximg->data);
8465 ximg->data = NULL;
8466 XDestroyImage (ximg);
8467 }
8468}
8469
8470
8471/* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT
8472 are width and height of both the image and pixmap. */
8473
8474static void
8475x_put_x_image (f, ximg, pixmap, width, height)
8476 struct frame *f;
8477 XImage *ximg;
8478 Pixmap pixmap;
8479{
8480 GC gc;
8481
8482 xassert (interrupt_input_blocked);
8483 gc = XCreateGC (NULL, pixmap, 0, NULL);
8484 XPutImage (NULL, pixmap, gc, ximg, 0, 0, 0, 0, width, height);
8485 XFreeGC (NULL, gc);
8486}
8487
8488#endif
8489
8490\f
8491/***********************************************************************
8492 Searching files
8493 ***********************************************************************/
8494
8495static Lisp_Object x_find_image_file P_ ((Lisp_Object));
8496
8497/* Find image file FILE. Look in data-directory, then
8498 x-bitmap-file-path. Value is the full name of the file found, or
8499 nil if not found. */
8500
8501static Lisp_Object
8502x_find_image_file (file)
8503 Lisp_Object file;
8504{
8505 Lisp_Object file_found, search_path;
8506 struct gcpro gcpro1, gcpro2;
8507 int fd;
8508
8509 file_found = Qnil;
8510 search_path = Fcons (Vdata_directory, Vx_bitmap_file_path);
8511 GCPRO2 (file_found, search_path);
8512
8513 /* Try to find FILE in data-directory, then x-bitmap-file-path. */
8514 fd = openp (search_path, file, "", &file_found, 0);
8515
939d6465 8516 if (fd == -1)
6fc2811b
JR
8517 file_found = Qnil;
8518 else
8519 close (fd);
8520
8521 UNGCPRO;
8522 return file_found;
8523}
8524
8525
8526\f
8527/***********************************************************************
8528 XBM images
8529 ***********************************************************************/
8530
8531static int xbm_load P_ ((struct frame *f, struct image *img));
8532static int xbm_load_image_from_file P_ ((struct frame *f, struct image *img,
8533 Lisp_Object file));
8534static int xbm_image_p P_ ((Lisp_Object object));
8535static int xbm_read_bitmap_file_data P_ ((char *, int *, int *,
8536 unsigned char **));
8537
8538
8539/* Indices of image specification fields in xbm_format, below. */
8540
8541enum xbm_keyword_index
8542{
8543 XBM_TYPE,
8544 XBM_FILE,
8545 XBM_WIDTH,
8546 XBM_HEIGHT,
8547 XBM_DATA,
8548 XBM_FOREGROUND,
8549 XBM_BACKGROUND,
8550 XBM_ASCENT,
8551 XBM_MARGIN,
8552 XBM_RELIEF,
8553 XBM_ALGORITHM,
8554 XBM_HEURISTIC_MASK,
8555 XBM_LAST
8556};
8557
8558/* Vector of image_keyword structures describing the format
8559 of valid XBM image specifications. */
8560
8561static struct image_keyword xbm_format[XBM_LAST] =
8562{
8563 {":type", IMAGE_SYMBOL_VALUE, 1},
8564 {":file", IMAGE_STRING_VALUE, 0},
8565 {":width", IMAGE_POSITIVE_INTEGER_VALUE, 0},
8566 {":height", IMAGE_POSITIVE_INTEGER_VALUE, 0},
8567 {":data", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8568 {":foreground", IMAGE_STRING_VALUE, 0},
8569 {":background", IMAGE_STRING_VALUE, 0},
8570 {":ascent", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
8571 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
8572 {":relief", IMAGE_INTEGER_VALUE, 0},
8573 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8574 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
8575};
8576
8577/* Structure describing the image type XBM. */
8578
8579static struct image_type xbm_type =
8580{
8581 &Qxbm,
8582 xbm_image_p,
8583 xbm_load,
8584 x_clear_image,
8585 NULL
8586};
8587
8588/* Tokens returned from xbm_scan. */
8589
8590enum xbm_token
8591{
8592 XBM_TK_IDENT = 256,
8593 XBM_TK_NUMBER
8594};
8595
8596
8597/* Return non-zero if OBJECT is a valid XBM-type image specification.
8598 A valid specification is a list starting with the symbol `image'
8599 The rest of the list is a property list which must contain an
8600 entry `:type xbm..
8601
8602 If the specification specifies a file to load, it must contain
8603 an entry `:file FILENAME' where FILENAME is a string.
8604
8605 If the specification is for a bitmap loaded from memory it must
8606 contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where
8607 WIDTH and HEIGHT are integers > 0. DATA may be:
8608
8609 1. a string large enough to hold the bitmap data, i.e. it must
8610 have a size >= (WIDTH + 7) / 8 * HEIGHT
8611
8612 2. a bool-vector of size >= WIDTH * HEIGHT
8613
8614 3. a vector of strings or bool-vectors, one for each line of the
8615 bitmap.
8616
8617 Both the file and data forms may contain the additional entries
8618 `:background COLOR' and `:foreground COLOR'. If not present,
8619 foreground and background of the frame on which the image is
8620 displayed, is used. */
8621
8622static int
8623xbm_image_p (object)
8624 Lisp_Object object;
8625{
8626 struct image_keyword kw[XBM_LAST];
8627
8628 bcopy (xbm_format, kw, sizeof kw);
8629 if (!parse_image_spec (object, kw, XBM_LAST, Qxbm))
8630 return 0;
8631
8632 xassert (EQ (kw[XBM_TYPE].value, Qxbm));
8633
8634 if (kw[XBM_FILE].count)
8635 {
8636 if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_DATA].count)
8637 return 0;
8638 }
8639 else
8640 {
8641 Lisp_Object data;
8642 int width, height;
8643
8644 /* Entries for `:width', `:height' and `:data' must be present. */
8645 if (!kw[XBM_WIDTH].count
8646 || !kw[XBM_HEIGHT].count
8647 || !kw[XBM_DATA].count)
8648 return 0;
8649
8650 data = kw[XBM_DATA].value;
8651 width = XFASTINT (kw[XBM_WIDTH].value);
8652 height = XFASTINT (kw[XBM_HEIGHT].value);
8653
8654 /* Check type of data, and width and height against contents of
8655 data. */
8656 if (VECTORP (data))
8657 {
8658 int i;
8659
8660 /* Number of elements of the vector must be >= height. */
8661 if (XVECTOR (data)->size < height)
8662 return 0;
8663
8664 /* Each string or bool-vector in data must be large enough
8665 for one line of the image. */
8666 for (i = 0; i < height; ++i)
8667 {
8668 Lisp_Object elt = XVECTOR (data)->contents[i];
8669
8670 if (STRINGP (elt))
8671 {
8672 if (XSTRING (elt)->size
8673 < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR)
8674 return 0;
8675 }
8676 else if (BOOL_VECTOR_P (elt))
8677 {
8678 if (XBOOL_VECTOR (elt)->size < width)
8679 return 0;
8680 }
8681 else
8682 return 0;
8683 }
8684 }
8685 else if (STRINGP (data))
8686 {
8687 if (XSTRING (data)->size
8688 < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR * height)
8689 return 0;
8690 }
8691 else if (BOOL_VECTOR_P (data))
8692 {
8693 if (XBOOL_VECTOR (data)->size < width * height)
8694 return 0;
8695 }
8696 else
8697 return 0;
8698 }
8699
8700 /* Baseline must be a value between 0 and 100 (a percentage). */
8701 if (kw[XBM_ASCENT].count
8702 && XFASTINT (kw[XBM_ASCENT].value) > 100)
8703 return 0;
8704
8705 return 1;
8706}
8707
8708
8709/* Scan a bitmap file. FP is the stream to read from. Value is
8710 either an enumerator from enum xbm_token, or a character for a
8711 single-character token, or 0 at end of file. If scanning an
8712 identifier, store the lexeme of the identifier in SVAL. If
8713 scanning a number, store its value in *IVAL. */
8714
8715static int
8716xbm_scan (fp, sval, ival)
8717 FILE *fp;
8718 char *sval;
8719 int *ival;
8720{
8721 int c;
8722
8723 /* Skip white space. */
8724 while ((c = fgetc (fp)) != EOF && isspace (c))
8725 ;
8726
8727 if (c == EOF)
8728 c = 0;
8729 else if (isdigit (c))
8730 {
8731 int value = 0, digit;
8732
8733 if (c == '0')
8734 {
8735 c = fgetc (fp);
8736 if (c == 'x' || c == 'X')
8737 {
8738 while ((c = fgetc (fp)) != EOF)
8739 {
8740 if (isdigit (c))
8741 digit = c - '0';
8742 else if (c >= 'a' && c <= 'f')
8743 digit = c - 'a' + 10;
8744 else if (c >= 'A' && c <= 'F')
8745 digit = c - 'A' + 10;
8746 else
8747 break;
8748 value = 16 * value + digit;
8749 }
8750 }
8751 else if (isdigit (c))
8752 {
8753 value = c - '0';
8754 while ((c = fgetc (fp)) != EOF
8755 && isdigit (c))
8756 value = 8 * value + c - '0';
8757 }
8758 }
8759 else
8760 {
8761 value = c - '0';
8762 while ((c = fgetc (fp)) != EOF
8763 && isdigit (c))
8764 value = 10 * value + c - '0';
8765 }
8766
8767 if (c != EOF)
8768 ungetc (c, fp);
8769 *ival = value;
8770 c = XBM_TK_NUMBER;
8771 }
8772 else if (isalpha (c) || c == '_')
8773 {
8774 *sval++ = c;
8775 while ((c = fgetc (fp)) != EOF
8776 && (isalnum (c) || c == '_'))
8777 *sval++ = c;
8778 *sval = 0;
8779 if (c != EOF)
8780 ungetc (c, fp);
8781 c = XBM_TK_IDENT;
8782 }
8783
8784 return c;
8785}
8786
8787
8788/* Replacement for XReadBitmapFileData which isn't available under old
8789 X versions. FILE is the name of the bitmap file to read. Set
8790 *WIDTH and *HEIGHT to the width and height of the image. Return in
8791 *DATA the bitmap data allocated with xmalloc. Value is non-zero if
8792 successful. */
8793
8794static int
8795xbm_read_bitmap_file_data (file, width, height, data)
8796 char *file;
8797 int *width, *height;
8798 unsigned char **data;
8799{
8800 FILE *fp;
8801 char buffer[BUFSIZ];
8802 int padding_p = 0;
8803 int v10 = 0;
8804 int bytes_per_line, i, nbytes;
8805 unsigned char *p;
8806 int value;
8807 int LA1;
8808
8809#define match() \
8810 LA1 = xbm_scan (fp, buffer, &value)
8811
8812#define expect(TOKEN) \
8813 if (LA1 != (TOKEN)) \
8814 goto failure; \
8815 else \
8816 match ()
8817
8818#define expect_ident(IDENT) \
8819 if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \
8820 match (); \
8821 else \
8822 goto failure
8823
8824 fp = fopen (file, "r");
8825 if (fp == NULL)
8826 return 0;
8827
8828 *width = *height = -1;
8829 *data = NULL;
8830 LA1 = xbm_scan (fp, buffer, &value);
8831
8832 /* Parse defines for width, height and hot-spots. */
8833 while (LA1 == '#')
8834 {
8835 match ();
8836 expect_ident ("define");
8837 expect (XBM_TK_IDENT);
8838
8839 if (LA1 == XBM_TK_NUMBER);
8840 {
8841 char *p = strrchr (buffer, '_');
8842 p = p ? p + 1 : buffer;
8843 if (strcmp (p, "width") == 0)
8844 *width = value;
8845 else if (strcmp (p, "height") == 0)
8846 *height = value;
8847 }
8848 expect (XBM_TK_NUMBER);
8849 }
8850
8851 if (*width < 0 || *height < 0)
8852 goto failure;
8853
8854 /* Parse bits. Must start with `static'. */
8855 expect_ident ("static");
8856 if (LA1 == XBM_TK_IDENT)
8857 {
8858 if (strcmp (buffer, "unsigned") == 0)
8859 {
8860 match ();
8861 expect_ident ("char");
8862 }
8863 else if (strcmp (buffer, "short") == 0)
8864 {
8865 match ();
8866 v10 = 1;
8867 if (*width % 16 && *width % 16 < 9)
8868 padding_p = 1;
8869 }
8870 else if (strcmp (buffer, "char") == 0)
8871 match ();
8872 else
8873 goto failure;
8874 }
8875 else
8876 goto failure;
8877
8878 expect (XBM_TK_IDENT);
8879 expect ('[');
8880 expect (']');
8881 expect ('=');
8882 expect ('{');
8883
8884 bytes_per_line = (*width + 7) / 8 + padding_p;
8885 nbytes = bytes_per_line * *height;
8886 p = *data = (char *) xmalloc (nbytes);
8887
8888 if (v10)
8889 {
8890
8891 for (i = 0; i < nbytes; i += 2)
8892 {
8893 int val = value;
8894 expect (XBM_TK_NUMBER);
8895
8896 *p++ = val;
8897 if (!padding_p || ((i + 2) % bytes_per_line))
8898 *p++ = value >> 8;
8899
8900 if (LA1 == ',' || LA1 == '}')
8901 match ();
8902 else
8903 goto failure;
8904 }
8905 }
8906 else
8907 {
8908 for (i = 0; i < nbytes; ++i)
8909 {
8910 int val = value;
8911 expect (XBM_TK_NUMBER);
8912
8913 *p++ = val;
8914
8915 if (LA1 == ',' || LA1 == '}')
8916 match ();
8917 else
8918 goto failure;
8919 }
8920 }
8921
8922 fclose (fp);
8923 return 1;
8924
8925 failure:
8926
8927 fclose (fp);
8928 if (*data)
8929 {
8930 xfree (*data);
8931 *data = NULL;
8932 }
8933 return 0;
8934
8935#undef match
8936#undef expect
8937#undef expect_ident
8938}
8939
8940
8941/* Load XBM image IMG which will be displayed on frame F from file
8942 SPECIFIED_FILE. Value is non-zero if successful. */
8943
8944static int
8945xbm_load_image_from_file (f, img, specified_file)
8946 struct frame *f;
8947 struct image *img;
8948 Lisp_Object specified_file;
8949{
8950 int rc;
8951 unsigned char *data;
8952 int success_p = 0;
8953 Lisp_Object file;
8954 struct gcpro gcpro1;
8955
8956 xassert (STRINGP (specified_file));
8957 file = Qnil;
8958 GCPRO1 (file);
8959
8960 file = x_find_image_file (specified_file);
8961 if (!STRINGP (file))
8962 {
8963 image_error ("Cannot find image file `%s'", specified_file, Qnil);
8964 UNGCPRO;
8965 return 0;
8966 }
8967
8968 rc = xbm_read_bitmap_file_data (XSTRING (file)->data, &img->width,
8969 &img->height, &data);
8970 if (rc)
8971 {
8972 int depth = one_w32_display_info.n_cbits;
8973 unsigned long foreground = FRAME_FOREGROUND_PIXEL (f);
8974 unsigned long background = FRAME_BACKGROUND_PIXEL (f);
8975 Lisp_Object value;
8976
8977 xassert (img->width > 0 && img->height > 0);
8978
8979 /* Get foreground and background colors, maybe allocate colors. */
8980 value = image_spec_value (img->spec, QCforeground, NULL);
8981 if (!NILP (value))
8982 foreground = x_alloc_image_color (f, img, value, foreground);
8983
8984 value = image_spec_value (img->spec, QCbackground, NULL);
8985 if (!NILP (value))
8986 background = x_alloc_image_color (f, img, value, background);
8987
767b1ff0 8988#if 0 /* TODO : Port image display to W32 */
6fc2811b
JR
8989 BLOCK_INPUT;
8990 img->pixmap
8991 = XCreatePixmapFromBitmapData (FRAME_W32_DISPLAY (f),
8992 FRAME_W32_WINDOW (f),
8993 data,
8994 img->width, img->height,
8995 foreground, background,
8996 depth);
8997 xfree (data);
8998
8999 if (img->pixmap == 0)
9000 {
9001 x_clear_image (f, img);
9002 image_error ("Unable to create X pixmap for `%s'", file, Qnil);
9003 }
9004 else
9005 success_p = 1;
9006
9007 UNBLOCK_INPUT;
9008#endif
9009 }
9010 else
9011 image_error ("Error loading XBM image `%s'", img->spec, Qnil);
9012
9013 UNGCPRO;
9014 return success_p;
9015}
9016
9017
9018/* Fill image IMG which is used on frame F with pixmap data. Value is
9019 non-zero if successful. */
9020
9021static int
9022xbm_load (f, img)
9023 struct frame *f;
9024 struct image *img;
9025{
9026 int success_p = 0;
9027 Lisp_Object file_name;
9028
9029 xassert (xbm_image_p (img->spec));
9030
9031 /* If IMG->spec specifies a file name, create a non-file spec from it. */
9032 file_name = image_spec_value (img->spec, QCfile, NULL);
9033 if (STRINGP (file_name))
9034 success_p = xbm_load_image_from_file (f, img, file_name);
9035 else
9036 {
9037 struct image_keyword fmt[XBM_LAST];
9038 Lisp_Object data;
9039 int depth;
9040 unsigned long foreground = FRAME_FOREGROUND_PIXEL (f);
9041 unsigned long background = FRAME_BACKGROUND_PIXEL (f);
9042 char *bits;
9043 int parsed_p;
9044
9045 /* Parse the list specification. */
9046 bcopy (xbm_format, fmt, sizeof fmt);
9047 parsed_p = parse_image_spec (img->spec, fmt, XBM_LAST, Qxbm);
9048 xassert (parsed_p);
9049
9050 /* Get specified width, and height. */
9051 img->width = XFASTINT (fmt[XBM_WIDTH].value);
9052 img->height = XFASTINT (fmt[XBM_HEIGHT].value);
9053 xassert (img->width > 0 && img->height > 0);
9054
9055 BLOCK_INPUT;
9056
9057 if (fmt[XBM_ASCENT].count)
9058 img->ascent = XFASTINT (fmt[XBM_ASCENT].value);
9059
9060 /* Get foreground and background colors, maybe allocate colors. */
9061 if (fmt[XBM_FOREGROUND].count)
9062 foreground = x_alloc_image_color (f, img, fmt[XBM_FOREGROUND].value,
9063 foreground);
9064 if (fmt[XBM_BACKGROUND].count)
9065 background = x_alloc_image_color (f, img, fmt[XBM_BACKGROUND].value,
9066 background);
9067
9068 /* Set bits to the bitmap image data. */
9069 data = fmt[XBM_DATA].value;
9070 if (VECTORP (data))
9071 {
9072 int i;
9073 char *p;
9074 int nbytes = (img->width + BITS_PER_CHAR - 1) / BITS_PER_CHAR;
9075
9076 p = bits = (char *) alloca (nbytes * img->height);
9077 for (i = 0; i < img->height; ++i, p += nbytes)
9078 {
9079 Lisp_Object line = XVECTOR (data)->contents[i];
9080 if (STRINGP (line))
9081 bcopy (XSTRING (line)->data, p, nbytes);
9082 else
9083 bcopy (XBOOL_VECTOR (line)->data, p, nbytes);
9084 }
9085 }
9086 else if (STRINGP (data))
9087 bits = XSTRING (data)->data;
9088 else
9089 bits = XBOOL_VECTOR (data)->data;
9090
767b1ff0 9091#if 0 /* TODO : W32 XPM code */
6fc2811b
JR
9092 /* Create the pixmap. */
9093 depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
9094 img->pixmap
9095 = XCreatePixmapFromBitmapData (FRAME_W32_DISPLAY (f),
9096 FRAME_W32_WINDOW (f),
9097 bits,
9098 img->width, img->height,
9099 foreground, background,
9100 depth);
767b1ff0 9101#endif /* TODO */
6fc2811b
JR
9102
9103 if (img->pixmap)
9104 success_p = 1;
9105 else
9106 {
9107 image_error ("Unable to create pixmap for XBM image `%s'",
9108 img->spec, Qnil);
9109 x_clear_image (f, img);
9110 }
9111
9112 UNBLOCK_INPUT;
9113 }
9114
9115 return success_p;
9116}
9117
9118
9119\f
9120/***********************************************************************
9121 XPM images
9122 ***********************************************************************/
9123
9124#if HAVE_XPM
9125
9126static int xpm_image_p P_ ((Lisp_Object object));
9127static int xpm_load P_ ((struct frame *f, struct image *img));
9128static int xpm_valid_color_symbols_p P_ ((Lisp_Object));
9129
9130#include "X11/xpm.h"
9131
9132/* The symbol `xpm' identifying XPM-format images. */
9133
9134Lisp_Object Qxpm;
9135
9136/* Indices of image specification fields in xpm_format, below. */
9137
9138enum xpm_keyword_index
9139{
9140 XPM_TYPE,
9141 XPM_FILE,
9142 XPM_DATA,
9143 XPM_ASCENT,
9144 XPM_MARGIN,
9145 XPM_RELIEF,
9146 XPM_ALGORITHM,
9147 XPM_HEURISTIC_MASK,
9148 XPM_COLOR_SYMBOLS,
9149 XPM_LAST
9150};
9151
9152/* Vector of image_keyword structures describing the format
9153 of valid XPM image specifications. */
9154
9155static struct image_keyword xpm_format[XPM_LAST] =
9156{
9157 {":type", IMAGE_SYMBOL_VALUE, 1},
9158 {":file", IMAGE_STRING_VALUE, 0},
9159 {":data", IMAGE_STRING_VALUE, 0},
9160 {":ascent", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
9161 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
9162 {":relief", IMAGE_INTEGER_VALUE, 0},
9163 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9164 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9165 {":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
9166};
9167
9168/* Structure describing the image type XBM. */
9169
9170static struct image_type xpm_type =
9171{
9172 &Qxpm,
9173 xpm_image_p,
9174 xpm_load,
9175 x_clear_image,
9176 NULL
9177};
9178
9179
9180/* Value is non-zero if COLOR_SYMBOLS is a valid color symbols list
9181 for XPM images. Such a list must consist of conses whose car and
9182 cdr are strings. */
9183
9184static int
9185xpm_valid_color_symbols_p (color_symbols)
9186 Lisp_Object color_symbols;
9187{
9188 while (CONSP (color_symbols))
9189 {
9190 Lisp_Object sym = XCAR (color_symbols);
9191 if (!CONSP (sym)
9192 || !STRINGP (XCAR (sym))
9193 || !STRINGP (XCDR (sym)))
9194 break;
9195 color_symbols = XCDR (color_symbols);
9196 }
9197
9198 return NILP (color_symbols);
9199}
9200
9201
9202/* Value is non-zero if OBJECT is a valid XPM image specification. */
9203
9204static int
9205xpm_image_p (object)
9206 Lisp_Object object;
9207{
9208 struct image_keyword fmt[XPM_LAST];
9209 bcopy (xpm_format, fmt, sizeof fmt);
9210 return (parse_image_spec (object, fmt, XPM_LAST, Qxpm)
9211 /* Either `:file' or `:data' must be present. */
9212 && fmt[XPM_FILE].count + fmt[XPM_DATA].count == 1
9213 /* Either no `:color-symbols' or it's a list of conses
9214 whose car and cdr are strings. */
9215 && (fmt[XPM_COLOR_SYMBOLS].count == 0
9216 || xpm_valid_color_symbols_p (fmt[XPM_COLOR_SYMBOLS].value))
9217 && (fmt[XPM_ASCENT].count == 0
9218 || XFASTINT (fmt[XPM_ASCENT].value) < 100));
9219}
9220
9221
9222/* Load image IMG which will be displayed on frame F. Value is
9223 non-zero if successful. */
9224
9225static int
9226xpm_load (f, img)
9227 struct frame *f;
9228 struct image *img;
9229{
9230 int rc, i;
9231 XpmAttributes attrs;
9232 Lisp_Object specified_file, color_symbols;
9233
9234 /* Configure the XPM lib. Use the visual of frame F. Allocate
9235 close colors. Return colors allocated. */
9236 bzero (&attrs, sizeof attrs);
dfff8a69
JR
9237 attrs.visual = FRAME_X_VISUAL (f);
9238 attrs.colormap = FRAME_X_COLORMAP (f);
6fc2811b 9239 attrs.valuemask |= XpmVisual;
dfff8a69 9240 attrs.valuemask |= XpmColormap;
6fc2811b 9241 attrs.valuemask |= XpmReturnAllocPixels;
dfff8a69 9242#ifdef XpmAllocCloseColors
6fc2811b
JR
9243 attrs.alloc_close_colors = 1;
9244 attrs.valuemask |= XpmAllocCloseColors;
dfff8a69
JR
9245#else
9246 attrs.closeness = 600;
9247 attrs.valuemask |= XpmCloseness;
9248#endif
6fc2811b
JR
9249
9250 /* If image specification contains symbolic color definitions, add
9251 these to `attrs'. */
9252 color_symbols = image_spec_value (img->spec, QCcolor_symbols, NULL);
9253 if (CONSP (color_symbols))
9254 {
9255 Lisp_Object tail;
9256 XpmColorSymbol *xpm_syms;
9257 int i, size;
9258
9259 attrs.valuemask |= XpmColorSymbols;
9260
9261 /* Count number of symbols. */
9262 attrs.numsymbols = 0;
9263 for (tail = color_symbols; CONSP (tail); tail = XCDR (tail))
9264 ++attrs.numsymbols;
9265
9266 /* Allocate an XpmColorSymbol array. */
9267 size = attrs.numsymbols * sizeof *xpm_syms;
9268 xpm_syms = (XpmColorSymbol *) alloca (size);
9269 bzero (xpm_syms, size);
9270 attrs.colorsymbols = xpm_syms;
9271
9272 /* Fill the color symbol array. */
9273 for (tail = color_symbols, i = 0;
9274 CONSP (tail);
9275 ++i, tail = XCDR (tail))
9276 {
9277 Lisp_Object name = XCAR (XCAR (tail));
9278 Lisp_Object color = XCDR (XCAR (tail));
9279 xpm_syms[i].name = (char *) alloca (XSTRING (name)->size + 1);
9280 strcpy (xpm_syms[i].name, XSTRING (name)->data);
9281 xpm_syms[i].value = (char *) alloca (XSTRING (color)->size + 1);
9282 strcpy (xpm_syms[i].value, XSTRING (color)->data);
9283 }
9284 }
9285
9286 /* Create a pixmap for the image, either from a file, or from a
9287 string buffer containing data in the same format as an XPM file. */
9288 BLOCK_INPUT;
9289 specified_file = image_spec_value (img->spec, QCfile, NULL);
9290 if (STRINGP (specified_file))
9291 {
9292 Lisp_Object file = x_find_image_file (specified_file);
9293 if (!STRINGP (file))
9294 {
9295 image_error ("Cannot find image file `%s'", specified_file, Qnil);
9296 UNBLOCK_INPUT;
9297 return 0;
9298 }
9299
9300 rc = XpmReadFileToPixmap (NULL, FRAME_W32_WINDOW (f),
9301 XSTRING (file)->data, &img->pixmap, &img->mask,
9302 &attrs);
9303 }
9304 else
9305 {
9306 Lisp_Object buffer = image_spec_value (img->spec, QCdata, NULL);
9307 rc = XpmCreatePixmapFromBuffer (NULL, FRAME_W32_WINDOW (f),
9308 XSTRING (buffer)->data,
9309 &img->pixmap, &img->mask,
9310 &attrs);
9311 }
9312 UNBLOCK_INPUT;
9313
9314 if (rc == XpmSuccess)
9315 {
9316 /* Remember allocated colors. */
9317 img->ncolors = attrs.nalloc_pixels;
9318 img->colors = (unsigned long *) xmalloc (img->ncolors
9319 * sizeof *img->colors);
9320 for (i = 0; i < attrs.nalloc_pixels; ++i)
9321 img->colors[i] = attrs.alloc_pixels[i];
9322
9323 img->width = attrs.width;
9324 img->height = attrs.height;
9325 xassert (img->width > 0 && img->height > 0);
9326
9327 /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */
9328 BLOCK_INPUT;
9329 XpmFreeAttributes (&attrs);
9330 UNBLOCK_INPUT;
9331 }
9332 else
9333 {
9334 switch (rc)
9335 {
9336 case XpmOpenFailed:
9337 image_error ("Error opening XPM file (%s)", img->spec, Qnil);
9338 break;
9339
9340 case XpmFileInvalid:
9341 image_error ("Invalid XPM file (%s)", img->spec, Qnil);
9342 break;
9343
9344 case XpmNoMemory:
9345 image_error ("Out of memory (%s)", img->spec, Qnil);
9346 break;
9347
9348 case XpmColorFailed:
9349 image_error ("Color allocation error (%s)", img->spec, Qnil);
9350 break;
9351
9352 default:
9353 image_error ("Unknown error (%s)", img->spec, Qnil);
9354 break;
9355 }
9356 }
9357
9358 return rc == XpmSuccess;
9359}
9360
9361#endif /* HAVE_XPM != 0 */
9362
9363\f
767b1ff0 9364#if 0 /* TODO : Color tables on W32. */
6fc2811b
JR
9365/***********************************************************************
9366 Color table
9367 ***********************************************************************/
9368
9369/* An entry in the color table mapping an RGB color to a pixel color. */
9370
9371struct ct_color
9372{
9373 int r, g, b;
9374 unsigned long pixel;
9375
9376 /* Next in color table collision list. */
9377 struct ct_color *next;
9378};
9379
9380/* The bucket vector size to use. Must be prime. */
9381
9382#define CT_SIZE 101
9383
9384/* Value is a hash of the RGB color given by R, G, and B. */
9385
9386#define CT_HASH_RGB(R, G, B) (((R) << 16) ^ ((G) << 8) ^ (B))
9387
9388/* The color hash table. */
9389
9390struct ct_color **ct_table;
9391
9392/* Number of entries in the color table. */
9393
9394int ct_colors_allocated;
9395
9396/* Function prototypes. */
9397
9398static void init_color_table P_ ((void));
9399static void free_color_table P_ ((void));
9400static unsigned long *colors_in_color_table P_ ((int *n));
9401static unsigned long lookup_rgb_color P_ ((struct frame *f, int r, int g, int b));
9402static unsigned long lookup_pixel_color P_ ((struct frame *f, unsigned long p));
9403
9404
9405/* Initialize the color table. */
9406
9407static void
9408init_color_table ()
9409{
9410 int size = CT_SIZE * sizeof (*ct_table);
9411 ct_table = (struct ct_color **) xmalloc (size);
9412 bzero (ct_table, size);
9413 ct_colors_allocated = 0;
9414}
9415
9416
9417/* Free memory associated with the color table. */
9418
9419static void
9420free_color_table ()
9421{
9422 int i;
9423 struct ct_color *p, *next;
9424
9425 for (i = 0; i < CT_SIZE; ++i)
9426 for (p = ct_table[i]; p; p = next)
9427 {
9428 next = p->next;
9429 xfree (p);
9430 }
9431
9432 xfree (ct_table);
9433 ct_table = NULL;
9434}
9435
9436
9437/* Value is a pixel color for RGB color R, G, B on frame F. If an
9438 entry for that color already is in the color table, return the
9439 pixel color of that entry. Otherwise, allocate a new color for R,
9440 G, B, and make an entry in the color table. */
9441
9442static unsigned long
9443lookup_rgb_color (f, r, g, b)
9444 struct frame *f;
9445 int r, g, b;
9446{
9447 unsigned hash = CT_HASH_RGB (r, g, b);
9448 int i = hash % CT_SIZE;
9449 struct ct_color *p;
9450
9451 for (p = ct_table[i]; p; p = p->next)
9452 if (p->r == r && p->g == g && p->b == b)
9453 break;
9454
9455 if (p == NULL)
9456 {
9457 COLORREF color;
9458 Colormap cmap;
9459 int rc;
9460
9461 color = PALETTERGB (r, g, b);
9462
9463 ++ct_colors_allocated;
9464
9465 p = (struct ct_color *) xmalloc (sizeof *p);
9466 p->r = r;
9467 p->g = g;
9468 p->b = b;
9469 p->pixel = color;
9470 p->next = ct_table[i];
9471 ct_table[i] = p;
9472 }
9473
9474 return p->pixel;
9475}
9476
9477
9478/* Look up pixel color PIXEL which is used on frame F in the color
9479 table. If not already present, allocate it. Value is PIXEL. */
9480
9481static unsigned long
9482lookup_pixel_color (f, pixel)
9483 struct frame *f;
9484 unsigned long pixel;
9485{
9486 int i = pixel % CT_SIZE;
9487 struct ct_color *p;
9488
9489 for (p = ct_table[i]; p; p = p->next)
9490 if (p->pixel == pixel)
9491 break;
9492
9493 if (p == NULL)
9494 {
9495 XColor color;
9496 Colormap cmap;
9497 int rc;
9498
9499 BLOCK_INPUT;
9500
9501 cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f));
9502 color.pixel = pixel;
9503 XQueryColor (NULL, cmap, &color);
9504 rc = x_alloc_nearest_color (f, cmap, &color);
9505 UNBLOCK_INPUT;
9506
9507 if (rc)
9508 {
9509 ++ct_colors_allocated;
9510
9511 p = (struct ct_color *) xmalloc (sizeof *p);
9512 p->r = color.red;
9513 p->g = color.green;
9514 p->b = color.blue;
9515 p->pixel = pixel;
9516 p->next = ct_table[i];
9517 ct_table[i] = p;
9518 }
9519 else
9520 return FRAME_FOREGROUND_PIXEL (f);
9521 }
9522 return p->pixel;
9523}
9524
9525
9526/* Value is a vector of all pixel colors contained in the color table,
9527 allocated via xmalloc. Set *N to the number of colors. */
9528
9529static unsigned long *
9530colors_in_color_table (n)
9531 int *n;
9532{
9533 int i, j;
9534 struct ct_color *p;
9535 unsigned long *colors;
9536
9537 if (ct_colors_allocated == 0)
9538 {
9539 *n = 0;
9540 colors = NULL;
9541 }
9542 else
9543 {
9544 colors = (unsigned long *) xmalloc (ct_colors_allocated
9545 * sizeof *colors);
9546 *n = ct_colors_allocated;
9547
9548 for (i = j = 0; i < CT_SIZE; ++i)
9549 for (p = ct_table[i]; p; p = p->next)
9550 colors[j++] = p->pixel;
9551 }
9552
9553 return colors;
9554}
9555
767b1ff0 9556#endif /* TODO */
6fc2811b
JR
9557
9558\f
9559/***********************************************************************
9560 Algorithms
9561 ***********************************************************************/
9562
767b1ff0 9563#if 0 /* TODO : W32 versions of low level algorithms */
6fc2811b
JR
9564static void x_laplace_write_row P_ ((struct frame *, long *,
9565 int, XImage *, int));
9566static void x_laplace_read_row P_ ((struct frame *, Colormap,
9567 XColor *, int, XImage *, int));
9568
9569
9570/* Fill COLORS with RGB colors from row Y of image XIMG. F is the
9571 frame we operate on, CMAP is the color-map in effect, and WIDTH is
9572 the width of one row in the image. */
9573
9574static void
9575x_laplace_read_row (f, cmap, colors, width, ximg, y)
9576 struct frame *f;
9577 Colormap cmap;
9578 XColor *colors;
9579 int width;
9580 XImage *ximg;
9581 int y;
9582{
9583 int x;
9584
9585 for (x = 0; x < width; ++x)
9586 colors[x].pixel = XGetPixel (ximg, x, y);
9587
9588 XQueryColors (NULL, cmap, colors, width);
9589}
9590
9591
9592/* Write row Y of image XIMG. PIXELS is an array of WIDTH longs
9593 containing the pixel colors to write. F is the frame we are
9594 working on. */
9595
9596static void
9597x_laplace_write_row (f, pixels, width, ximg, y)
9598 struct frame *f;
9599 long *pixels;
9600 int width;
9601 XImage *ximg;
9602 int y;
9603{
9604 int x;
9605
9606 for (x = 0; x < width; ++x)
9607 XPutPixel (ximg, x, y, pixels[x]);
9608}
9609#endif
9610
9611/* Transform image IMG which is used on frame F with a Laplace
9612 edge-detection algorithm. The result is an image that can be used
9613 to draw disabled buttons, for example. */
9614
9615static void
9616x_laplace (f, img)
9617 struct frame *f;
9618 struct image *img;
9619{
767b1ff0 9620#if 0 /* TODO : W32 version */
6fc2811b
JR
9621 Colormap cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f));
9622 XImage *ximg, *oimg;
9623 XColor *in[3];
9624 long *out;
9625 Pixmap pixmap;
9626 int x, y, i;
9627 long pixel;
9628 int in_y, out_y, rc;
9629 int mv2 = 45000;
9630
9631 BLOCK_INPUT;
9632
9633 /* Get the X image IMG->pixmap. */
9634 ximg = XGetImage (NULL, img->pixmap,
9635 0, 0, img->width, img->height, ~0, ZPixmap);
9636
9637 /* Allocate 3 input rows, and one output row of colors. */
9638 for (i = 0; i < 3; ++i)
9639 in[i] = (XColor *) alloca (img->width * sizeof (XColor));
9640 out = (long *) alloca (img->width * sizeof (long));
9641
9642 /* Create an X image for output. */
9643 rc = x_create_x_image_and_pixmap (f, img->width, img->height, 0,
9644 &oimg, &pixmap);
9645
9646 /* Fill first two rows. */
9647 x_laplace_read_row (f, cmap, in[0], img->width, ximg, 0);
9648 x_laplace_read_row (f, cmap, in[1], img->width, ximg, 1);
9649 in_y = 2;
9650
9651 /* Write first row, all zeros. */
9652 init_color_table ();
9653 pixel = lookup_rgb_color (f, 0, 0, 0);
9654 for (x = 0; x < img->width; ++x)
9655 out[x] = pixel;
9656 x_laplace_write_row (f, out, img->width, oimg, 0);
9657 out_y = 1;
9658
9659 for (y = 2; y < img->height; ++y)
9660 {
9661 int rowa = y % 3;
9662 int rowb = (y + 2) % 3;
9663
9664 x_laplace_read_row (f, cmap, in[rowa], img->width, ximg, in_y++);
9665
9666 for (x = 0; x < img->width - 2; ++x)
9667 {
9668 int r = in[rowa][x].red + mv2 - in[rowb][x + 2].red;
9669 int g = in[rowa][x].green + mv2 - in[rowb][x + 2].green;
9670 int b = in[rowa][x].blue + mv2 - in[rowb][x + 2].blue;
9671
9672 out[x + 1] = lookup_rgb_color (f, r & 0xffff, g & 0xffff,
9673 b & 0xffff);
9674 }
9675
9676 x_laplace_write_row (f, out, img->width, oimg, out_y++);
9677 }
9678
9679 /* Write last line, all zeros. */
9680 for (x = 0; x < img->width; ++x)
9681 out[x] = pixel;
9682 x_laplace_write_row (f, out, img->width, oimg, out_y);
9683
9684 /* Free the input image, and free resources of IMG. */
9685 XDestroyImage (ximg);
9686 x_clear_image (f, img);
9687
9688 /* Put the output image into pixmap, and destroy it. */
9689 x_put_x_image (f, oimg, pixmap, img->width, img->height);
9690 x_destroy_x_image (oimg);
9691
9692 /* Remember new pixmap and colors in IMG. */
9693 img->pixmap = pixmap;
9694 img->colors = colors_in_color_table (&img->ncolors);
9695 free_color_table ();
9696
9697 UNBLOCK_INPUT;
767b1ff0 9698#endif /* TODO */
6fc2811b
JR
9699}
9700
9701
9702/* Build a mask for image IMG which is used on frame F. FILE is the
9703 name of an image file, for error messages. HOW determines how to
9704 determine the background color of IMG. If it is a list '(R G B)',
9705 with R, G, and B being integers >= 0, take that as the color of the
9706 background. Otherwise, determine the background color of IMG
9707 heuristically. Value is non-zero if successful. */
9708
9709static int
9710x_build_heuristic_mask (f, img, how)
9711 struct frame *f;
9712 struct image *img;
9713 Lisp_Object how;
9714{
767b1ff0 9715#if 0 /* TODO : W32 version */
6fc2811b
JR
9716 Display *dpy = FRAME_W32_DISPLAY (f);
9717 XImage *ximg, *mask_img;
9718 int x, y, rc, look_at_corners_p;
9719 unsigned long bg;
9720
9721 BLOCK_INPUT;
9722
9723 /* Create an image and pixmap serving as mask. */
9724 rc = x_create_x_image_and_pixmap (f, img->width, img->height, 1,
9725 &mask_img, &img->mask);
9726 if (!rc)
9727 {
9728 UNBLOCK_INPUT;
9729 return 0;
9730 }
9731
9732 /* Get the X image of IMG->pixmap. */
9733 ximg = XGetImage (dpy, img->pixmap, 0, 0, img->width, img->height,
9734 ~0, ZPixmap);
9735
9736 /* Determine the background color of ximg. If HOW is `(R G B)'
9737 take that as color. Otherwise, try to determine the color
9738 heuristically. */
9739 look_at_corners_p = 1;
9740
9741 if (CONSP (how))
9742 {
9743 int rgb[3], i = 0;
9744
9745 while (i < 3
9746 && CONSP (how)
9747 && NATNUMP (XCAR (how)))
9748 {
9749 rgb[i] = XFASTINT (XCAR (how)) & 0xffff;
9750 how = XCDR (how);
9751 }
9752
9753 if (i == 3 && NILP (how))
9754 {
9755 char color_name[30];
9756 XColor exact, color;
9757 Colormap cmap;
9758
9759 sprintf (color_name, "#%04x%04x%04x", rgb[0], rgb[1], rgb[2]);
9760
9761 cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f));
9762 if (XLookupColor (dpy, cmap, color_name, &exact, &color))
9763 {
9764 bg = color.pixel;
9765 look_at_corners_p = 0;
9766 }
9767 }
9768 }
9769
9770 if (look_at_corners_p)
9771 {
9772 unsigned long corners[4];
9773 int i, best_count;
9774
9775 /* Get the colors at the corners of ximg. */
9776 corners[0] = XGetPixel (ximg, 0, 0);
9777 corners[1] = XGetPixel (ximg, img->width - 1, 0);
9778 corners[2] = XGetPixel (ximg, img->width - 1, img->height - 1);
9779 corners[3] = XGetPixel (ximg, 0, img->height - 1);
9780
9781 /* Choose the most frequently found color as background. */
9782 for (i = best_count = 0; i < 4; ++i)
9783 {
9784 int j, n;
9785
9786 for (j = n = 0; j < 4; ++j)
9787 if (corners[i] == corners[j])
9788 ++n;
9789
9790 if (n > best_count)
9791 bg = corners[i], best_count = n;
9792 }
9793 }
9794
9795 /* Set all bits in mask_img to 1 whose color in ximg is different
9796 from the background color bg. */
9797 for (y = 0; y < img->height; ++y)
9798 for (x = 0; x < img->width; ++x)
9799 XPutPixel (mask_img, x, y, XGetPixel (ximg, x, y) != bg);
9800
9801 /* Put mask_img into img->mask. */
9802 x_put_x_image (f, mask_img, img->mask, img->width, img->height);
9803 x_destroy_x_image (mask_img);
9804 XDestroyImage (ximg);
9805
9806 UNBLOCK_INPUT;
767b1ff0 9807#endif /* TODO */
6fc2811b
JR
9808
9809 return 1;
9810}
9811
9812
9813\f
9814/***********************************************************************
9815 PBM (mono, gray, color)
9816 ***********************************************************************/
9817#ifdef HAVE_PBM
9818
9819static int pbm_image_p P_ ((Lisp_Object object));
9820static int pbm_load P_ ((struct frame *f, struct image *img));
9821static int pbm_scan_number P_ ((unsigned char **, unsigned char *));
9822
9823/* The symbol `pbm' identifying images of this type. */
9824
9825Lisp_Object Qpbm;
9826
9827/* Indices of image specification fields in gs_format, below. */
9828
9829enum pbm_keyword_index
9830{
9831 PBM_TYPE,
9832 PBM_FILE,
9833 PBM_DATA,
9834 PBM_ASCENT,
9835 PBM_MARGIN,
9836 PBM_RELIEF,
9837 PBM_ALGORITHM,
9838 PBM_HEURISTIC_MASK,
9839 PBM_LAST
9840};
9841
9842/* Vector of image_keyword structures describing the format
9843 of valid user-defined image specifications. */
9844
9845static struct image_keyword pbm_format[PBM_LAST] =
9846{
9847 {":type", IMAGE_SYMBOL_VALUE, 1},
9848 {":file", IMAGE_STRING_VALUE, 0},
9849 {":data", IMAGE_STRING_VALUE, 0},
9850 {":ascent", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
9851 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
9852 {":relief", IMAGE_INTEGER_VALUE, 0},
9853 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9854 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
9855};
9856
9857/* Structure describing the image type `pbm'. */
9858
9859static struct image_type pbm_type =
9860{
9861 &Qpbm,
9862 pbm_image_p,
9863 pbm_load,
9864 x_clear_image,
9865 NULL
9866};
9867
9868
9869/* Return non-zero if OBJECT is a valid PBM image specification. */
9870
9871static int
9872pbm_image_p (object)
9873 Lisp_Object object;
9874{
9875 struct image_keyword fmt[PBM_LAST];
9876
9877 bcopy (pbm_format, fmt, sizeof fmt);
9878
9879 if (!parse_image_spec (object, fmt, PBM_LAST, Qpbm)
9880 || (fmt[PBM_ASCENT].count
9881 && XFASTINT (fmt[PBM_ASCENT].value) > 100))
9882 return 0;
9883
9884 /* Must specify either :data or :file. */
9885 return fmt[PBM_DATA].count + fmt[PBM_FILE].count == 1;
9886}
9887
9888
9889/* Scan a decimal number from *S and return it. Advance *S while
9890 reading the number. END is the end of the string. Value is -1 at
9891 end of input. */
9892
9893static int
9894pbm_scan_number (s, end)
9895 unsigned char **s, *end;
9896{
9897 int c, val = -1;
9898
9899 while (*s < end)
9900 {
9901 /* Skip white-space. */
9902 while (*s < end && (c = *(*s)++, isspace (c)))
9903 ;
9904
9905 if (c == '#')
9906 {
9907 /* Skip comment to end of line. */
9908 while (*s < end && (c = *(*s)++, c != '\n'))
9909 ;
9910 }
9911 else if (isdigit (c))
9912 {
9913 /* Read decimal number. */
9914 val = c - '0';
9915 while (*s < end && (c = *(*s)++, isdigit (c)))
9916 val = 10 * val + c - '0';
9917 break;
9918 }
9919 else
9920 break;
9921 }
9922
9923 return val;
9924}
9925
9926
9927/* Read FILE into memory. Value is a pointer to a buffer allocated
9928 with xmalloc holding FILE's contents. Value is null if an error
9929 occured. *SIZE is set to the size of the file. */
9930
9931static char *
9932pbm_read_file (file, size)
9933 Lisp_Object file;
9934 int *size;
9935{
9936 FILE *fp = NULL;
9937 char *buf = NULL;
9938 struct stat st;
9939
9940 if (stat (XSTRING (file)->data, &st) == 0
9941 && (fp = fopen (XSTRING (file)->data, "r")) != NULL
9942 && (buf = (char *) xmalloc (st.st_size),
9943 fread (buf, 1, st.st_size, fp) == st.st_size))
9944 {
9945 *size = st.st_size;
9946 fclose (fp);
9947 }
9948 else
9949 {
9950 if (fp)
9951 fclose (fp);
9952 if (buf)
9953 {
9954 xfree (buf);
9955 buf = NULL;
9956 }
9957 }
9958
9959 return buf;
9960}
9961
9962
9963/* Load PBM image IMG for use on frame F. */
9964
9965static int
9966pbm_load (f, img)
9967 struct frame *f;
9968 struct image *img;
9969{
9970 int raw_p, x, y;
9971 int width, height, max_color_idx = 0;
9972 XImage *ximg;
9973 Lisp_Object file, specified_file;
9974 enum {PBM_MONO, PBM_GRAY, PBM_COLOR} type;
9975 struct gcpro gcpro1;
9976 unsigned char *contents = NULL;
9977 unsigned char *end, *p;
9978 int size;
9979
9980 specified_file = image_spec_value (img->spec, QCfile, NULL);
9981 file = Qnil;
9982 GCPRO1 (file);
9983
9984 if (STRINGP (specified_file))
9985 {
9986 file = x_find_image_file (specified_file);
9987 if (!STRINGP (file))
9988 {
9989 image_error ("Cannot find image file `%s'", specified_file, Qnil);
9990 UNGCPRO;
9991 return 0;
9992 }
9993
9994 contents = pbm_read_file (file, &size);
9995 if (contents == NULL)
9996 {
9997 image_error ("Error reading `%s'", file, Qnil);
9998 UNGCPRO;
9999 return 0;
10000 }
10001
10002 p = contents;
10003 end = contents + size;
10004 }
10005 else
10006 {
10007 Lisp_Object data;
10008 data = image_spec_value (img->spec, QCdata, NULL);
10009 p = XSTRING (data)->data;
10010 end = p + STRING_BYTES (XSTRING (data));
10011 }
10012
10013 /* Check magic number. */
10014 if (end - p < 2 || *p++ != 'P')
10015 {
10016 image_error ("Not a PBM image: `%s'", img->spec, Qnil);
10017 error:
10018 xfree (contents);
10019 UNGCPRO;
10020 return 0;
10021 }
10022
6fc2811b
JR
10023 switch (*p++)
10024 {
10025 case '1':
10026 raw_p = 0, type = PBM_MONO;
10027 break;
10028
10029 case '2':
10030 raw_p = 0, type = PBM_GRAY;
10031 break;
10032
10033 case '3':
10034 raw_p = 0, type = PBM_COLOR;
10035 break;
10036
10037 case '4':
10038 raw_p = 1, type = PBM_MONO;
10039 break;
10040
10041 case '5':
10042 raw_p = 1, type = PBM_GRAY;
10043 break;
10044
10045 case '6':
10046 raw_p = 1, type = PBM_COLOR;
10047 break;
10048
10049 default:
10050 image_error ("Not a PBM image: `%s'", img->spec, Qnil);
10051 goto error;
10052 }
10053
10054 /* Read width, height, maximum color-component. Characters
10055 starting with `#' up to the end of a line are ignored. */
10056 width = pbm_scan_number (&p, end);
10057 height = pbm_scan_number (&p, end);
10058
10059 if (type != PBM_MONO)
10060 {
10061 max_color_idx = pbm_scan_number (&p, end);
10062 if (raw_p && max_color_idx > 255)
10063 max_color_idx = 255;
10064 }
10065
10066 if (width < 0
10067 || height < 0
10068 || (type != PBM_MONO && max_color_idx < 0))
10069 goto error;
10070
10071 BLOCK_INPUT;
10072 if (!x_create_x_image_and_pixmap (f, width, height, 0,
10073 &ximg, &img->pixmap))
10074 {
10075 UNBLOCK_INPUT;
10076 goto error;
10077 }
10078
10079 /* Initialize the color hash table. */
10080 init_color_table ();
10081
10082 if (type == PBM_MONO)
10083 {
10084 int c = 0, g;
10085
10086 for (y = 0; y < height; ++y)
10087 for (x = 0; x < width; ++x)
10088 {
10089 if (raw_p)
10090 {
10091 if ((x & 7) == 0)
10092 c = *p++;
10093 g = c & 0x80;
10094 c <<= 1;
10095 }
10096 else
10097 g = pbm_scan_number (&p, end);
10098
10099 XPutPixel (ximg, x, y, (g
10100 ? FRAME_FOREGROUND_PIXEL (f)
10101 : FRAME_BACKGROUND_PIXEL (f)));
10102 }
10103 }
10104 else
10105 {
10106 for (y = 0; y < height; ++y)
10107 for (x = 0; x < width; ++x)
10108 {
10109 int r, g, b;
10110
10111 if (type == PBM_GRAY)
10112 r = g = b = raw_p ? *p++ : pbm_scan_number (&p, end);
10113 else if (raw_p)
10114 {
10115 r = *p++;
10116 g = *p++;
10117 b = *p++;
10118 }
10119 else
10120 {
10121 r = pbm_scan_number (&p, end);
10122 g = pbm_scan_number (&p, end);
10123 b = pbm_scan_number (&p, end);
10124 }
10125
10126 if (r < 0 || g < 0 || b < 0)
10127 {
dfff8a69 10128 xfree (ximg->data);
6fc2811b
JR
10129 ximg->data = NULL;
10130 XDestroyImage (ximg);
10131 UNBLOCK_INPUT;
10132 image_error ("Invalid pixel value in image `%s'",
10133 img->spec, Qnil);
10134 goto error;
10135 }
10136
10137 /* RGB values are now in the range 0..max_color_idx.
10138 Scale this to the range 0..0xffff supported by X. */
10139 r = (double) r * 65535 / max_color_idx;
10140 g = (double) g * 65535 / max_color_idx;
10141 b = (double) b * 65535 / max_color_idx;
10142 XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b));
10143 }
10144 }
10145
10146 /* Store in IMG->colors the colors allocated for the image, and
10147 free the color table. */
10148 img->colors = colors_in_color_table (&img->ncolors);
10149 free_color_table ();
10150
10151 /* Put the image into a pixmap. */
10152 x_put_x_image (f, ximg, img->pixmap, width, height);
10153 x_destroy_x_image (ximg);
10154 UNBLOCK_INPUT;
10155
10156 img->width = width;
10157 img->height = height;
10158
10159 UNGCPRO;
10160 xfree (contents);
10161 return 1;
10162}
10163#endif /* HAVE_PBM */
10164
10165\f
10166/***********************************************************************
10167 PNG
10168 ***********************************************************************/
10169
10170#if HAVE_PNG
10171
10172#include <png.h>
10173
10174/* Function prototypes. */
10175
10176static int png_image_p P_ ((Lisp_Object object));
10177static int png_load P_ ((struct frame *f, struct image *img));
10178
10179/* The symbol `png' identifying images of this type. */
10180
10181Lisp_Object Qpng;
10182
10183/* Indices of image specification fields in png_format, below. */
10184
10185enum png_keyword_index
10186{
10187 PNG_TYPE,
10188 PNG_DATA,
10189 PNG_FILE,
10190 PNG_ASCENT,
10191 PNG_MARGIN,
10192 PNG_RELIEF,
10193 PNG_ALGORITHM,
10194 PNG_HEURISTIC_MASK,
10195 PNG_LAST
10196};
10197
10198/* Vector of image_keyword structures describing the format
10199 of valid user-defined image specifications. */
10200
10201static struct image_keyword png_format[PNG_LAST] =
10202{
10203 {":type", IMAGE_SYMBOL_VALUE, 1},
10204 {":data", IMAGE_STRING_VALUE, 0},
10205 {":file", IMAGE_STRING_VALUE, 0},
10206 {":ascent", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
10207 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
10208 {":relief", IMAGE_INTEGER_VALUE, 0},
10209 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
10210 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
10211};
10212
10213/* Structure describing the image type `png'. */
10214
10215static struct image_type png_type =
10216{
10217 &Qpng,
10218 png_image_p,
10219 png_load,
10220 x_clear_image,
10221 NULL
10222};
10223
10224
10225/* Return non-zero if OBJECT is a valid PNG image specification. */
10226
10227static int
10228png_image_p (object)
10229 Lisp_Object object;
10230{
10231 struct image_keyword fmt[PNG_LAST];
10232 bcopy (png_format, fmt, sizeof fmt);
10233
10234 if (!parse_image_spec (object, fmt, PNG_LAST, Qpng)
10235 || (fmt[PNG_ASCENT].count
10236 && XFASTINT (fmt[PNG_ASCENT].value) > 100))
10237 return 0;
10238
10239 /* Must specify either the :data or :file keyword. */
10240 return fmt[PNG_FILE].count + fmt[PNG_DATA].count == 1;
10241}
10242
10243
10244/* Error and warning handlers installed when the PNG library
10245 is initialized. */
10246
10247static void
10248my_png_error (png_ptr, msg)
10249 png_struct *png_ptr;
10250 char *msg;
10251{
10252 xassert (png_ptr != NULL);
10253 image_error ("PNG error: %s", build_string (msg), Qnil);
10254 longjmp (png_ptr->jmpbuf, 1);
10255}
10256
10257
10258static void
10259my_png_warning (png_ptr, msg)
10260 png_struct *png_ptr;
10261 char *msg;
10262{
10263 xassert (png_ptr != NULL);
10264 image_error ("PNG warning: %s", build_string (msg), Qnil);
10265}
10266
6fc2811b
JR
10267/* Memory source for PNG decoding. */
10268
10269struct png_memory_storage
10270{
10271 unsigned char *bytes; /* The data */
10272 size_t len; /* How big is it? */
10273 int index; /* Where are we? */
10274};
10275
10276
10277/* Function set as reader function when reading PNG image from memory.
10278 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
10279 bytes from the input to DATA. */
10280
10281static void
10282png_read_from_memory (png_ptr, data, length)
10283 png_structp png_ptr;
10284 png_bytep data;
10285 png_size_t length;
10286{
10287 struct png_memory_storage *tbr
10288 = (struct png_memory_storage *) png_get_io_ptr (png_ptr);
10289
10290 if (length > tbr->len - tbr->index)
10291 png_error (png_ptr, "Read error");
10292
10293 bcopy (tbr->bytes + tbr->index, data, length);
10294 tbr->index = tbr->index + length;
10295}
10296
6fc2811b
JR
10297/* Load PNG image IMG for use on frame F. Value is non-zero if
10298 successful. */
10299
10300static int
10301png_load (f, img)
10302 struct frame *f;
10303 struct image *img;
10304{
10305 Lisp_Object file, specified_file;
10306 Lisp_Object specified_data;
10307 int x, y, i;
10308 XImage *ximg, *mask_img = NULL;
10309 struct gcpro gcpro1;
10310 png_struct *png_ptr = NULL;
10311 png_info *info_ptr = NULL, *end_info = NULL;
10312 FILE *fp = NULL;
10313 png_byte sig[8];
10314 png_byte *pixels = NULL;
10315 png_byte **rows = NULL;
10316 png_uint_32 width, height;
10317 int bit_depth, color_type, interlace_type;
10318 png_byte channels;
10319 png_uint_32 row_bytes;
10320 int transparent_p;
10321 char *gamma_str;
10322 double screen_gamma, image_gamma;
10323 int intent;
10324 struct png_memory_storage tbr; /* Data to be read */
10325
10326 /* Find out what file to load. */
10327 specified_file = image_spec_value (img->spec, QCfile, NULL);
10328 specified_data = image_spec_value (img->spec, QCdata, NULL);
10329 file = Qnil;
10330 GCPRO1 (file);
10331
10332 if (NILP (specified_data))
10333 {
10334 file = x_find_image_file (specified_file);
10335 if (!STRINGP (file))
10336 {
10337 image_error ("Cannot find image file `%s'", specified_file, Qnil);
10338 UNGCPRO;
10339 return 0;
10340 }
10341
10342 /* Open the image file. */
10343 fp = fopen (XSTRING (file)->data, "rb");
10344 if (!fp)
10345 {
10346 image_error ("Cannot open image file `%s'", file, Qnil);
10347 UNGCPRO;
10348 fclose (fp);
10349 return 0;
10350 }
10351
10352 /* Check PNG signature. */
10353 if (fread (sig, 1, sizeof sig, fp) != sizeof sig
10354 || !png_check_sig (sig, sizeof sig))
10355 {
10356 image_error ("Not a PNG file:` %s'", file, Qnil);
10357 UNGCPRO;
10358 fclose (fp);
10359 return 0;
10360 }
10361 }
10362 else
10363 {
10364 /* Read from memory. */
10365 tbr.bytes = XSTRING (specified_data)->data;
10366 tbr.len = STRING_BYTES (XSTRING (specified_data));
10367 tbr.index = 0;
10368
10369 /* Check PNG signature. */
10370 if (tbr.len < sizeof sig
10371 || !png_check_sig (tbr.bytes, sizeof sig))
10372 {
10373 image_error ("Not a PNG image: `%s'", img->spec, Qnil);
10374 UNGCPRO;
10375 return 0;
10376 }
10377
10378 /* Need to skip past the signature. */
10379 tbr.bytes += sizeof (sig);
10380 }
10381
6fc2811b
JR
10382 /* Initialize read and info structs for PNG lib. */
10383 png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL,
10384 my_png_error, my_png_warning);
10385 if (!png_ptr)
10386 {
10387 if (fp) fclose (fp);
10388 UNGCPRO;
10389 return 0;
10390 }
10391
10392 info_ptr = png_create_info_struct (png_ptr);
10393 if (!info_ptr)
10394 {
10395 png_destroy_read_struct (&png_ptr, NULL, NULL);
10396 if (fp) fclose (fp);
10397 UNGCPRO;
10398 return 0;
10399 }
10400
10401 end_info = png_create_info_struct (png_ptr);
10402 if (!end_info)
10403 {
10404 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
10405 if (fp) fclose (fp);
10406 UNGCPRO;
10407 return 0;
10408 }
10409
10410 /* Set error jump-back. We come back here when the PNG library
10411 detects an error. */
10412 if (setjmp (png_ptr->jmpbuf))
10413 {
10414 error:
10415 if (png_ptr)
10416 png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
10417 xfree (pixels);
10418 xfree (rows);
10419 if (fp) fclose (fp);
10420 UNGCPRO;
10421 return 0;
10422 }
10423
10424 /* Read image info. */
10425 if (!NILP (specified_data))
10426 png_set_read_fn (png_ptr, (void *) &tbr, png_read_from_memory);
10427 else
10428 png_init_io (png_ptr, fp);
10429
10430 png_set_sig_bytes (png_ptr, sizeof sig);
10431 png_read_info (png_ptr, info_ptr);
10432 png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
10433 &interlace_type, NULL, NULL);
10434
10435 /* If image contains simply transparency data, we prefer to
10436 construct a clipping mask. */
10437 if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
10438 transparent_p = 1;
10439 else
10440 transparent_p = 0;
10441
10442 /* This function is easier to write if we only have to handle
10443 one data format: RGB or RGBA with 8 bits per channel. Let's
10444 transform other formats into that format. */
10445
10446 /* Strip more than 8 bits per channel. */
10447 if (bit_depth == 16)
10448 png_set_strip_16 (png_ptr);
10449
10450 /* Expand data to 24 bit RGB, or 8 bit grayscale, with alpha channel
10451 if available. */
10452 png_set_expand (png_ptr);
10453
10454 /* Convert grayscale images to RGB. */
10455 if (color_type == PNG_COLOR_TYPE_GRAY
10456 || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
10457 png_set_gray_to_rgb (png_ptr);
10458
10459 /* The value 2.2 is a guess for PC monitors from PNG example.c. */
10460 gamma_str = getenv ("SCREEN_GAMMA");
10461 screen_gamma = gamma_str ? atof (gamma_str) : 2.2;
10462
10463 /* Tell the PNG lib to handle gamma correction for us. */
10464
10465#if defined(PNG_READ_sRGB_SUPPORTED) || defined(PNG_WRITE_sRGB_SUPPORTED)
10466 if (png_get_sRGB (png_ptr, info_ptr, &intent))
10467 /* There is a special chunk in the image specifying the gamma. */
10468 png_set_sRGB (png_ptr, info_ptr, intent);
10469 else
10470#endif
10471 if (png_get_gAMA (png_ptr, info_ptr, &image_gamma))
10472 /* Image contains gamma information. */
10473 png_set_gamma (png_ptr, screen_gamma, image_gamma);
10474 else
10475 /* Use a default of 0.5 for the image gamma. */
10476 png_set_gamma (png_ptr, screen_gamma, 0.5);
10477
10478 /* Handle alpha channel by combining the image with a background
10479 color. Do this only if a real alpha channel is supplied. For
10480 simple transparency, we prefer a clipping mask. */
10481 if (!transparent_p)
10482 {
10483 png_color_16 *image_background;
10484
10485 if (png_get_bKGD (png_ptr, info_ptr, &image_background))
10486 /* Image contains a background color with which to
10487 combine the image. */
10488 png_set_background (png_ptr, image_background,
10489 PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
10490 else
10491 {
10492 /* Image does not contain a background color with which
10493 to combine the image data via an alpha channel. Use
10494 the frame's background instead. */
10495 XColor color;
10496 Colormap cmap;
10497 png_color_16 frame_background;
10498
10499 BLOCK_INPUT;
10500 cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f));
10501 color.pixel = FRAME_BACKGROUND_PIXEL (f);
10502 XQueryColor (FRAME_W32_DISPLAY (f), cmap, &color);
10503 UNBLOCK_INPUT;
10504
10505 bzero (&frame_background, sizeof frame_background);
10506 frame_background.red = color.red;
10507 frame_background.green = color.green;
10508 frame_background.blue = color.blue;
10509
10510 png_set_background (png_ptr, &frame_background,
10511 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
10512 }
10513 }
10514
10515 /* Update info structure. */
10516 png_read_update_info (png_ptr, info_ptr);
10517
10518 /* Get number of channels. Valid values are 1 for grayscale images
10519 and images with a palette, 2 for grayscale images with transparency
10520 information (alpha channel), 3 for RGB images, and 4 for RGB
10521 images with alpha channel, i.e. RGBA. If conversions above were
10522 sufficient we should only have 3 or 4 channels here. */
10523 channels = png_get_channels (png_ptr, info_ptr);
10524 xassert (channels == 3 || channels == 4);
10525
10526 /* Number of bytes needed for one row of the image. */
10527 row_bytes = png_get_rowbytes (png_ptr, info_ptr);
10528
10529 /* Allocate memory for the image. */
10530 pixels = (png_byte *) xmalloc (row_bytes * height * sizeof *pixels);
10531 rows = (png_byte **) xmalloc (height * sizeof *rows);
10532 for (i = 0; i < height; ++i)
10533 rows[i] = pixels + i * row_bytes;
10534
10535 /* Read the entire image. */
10536 png_read_image (png_ptr, rows);
10537 png_read_end (png_ptr, info_ptr);
10538 if (fp)
10539 {
10540 fclose (fp);
10541 fp = NULL;
10542 }
10543
10544 BLOCK_INPUT;
10545
10546 /* Create the X image and pixmap. */
10547 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg,
10548 &img->pixmap))
10549 {
10550 UNBLOCK_INPUT;
10551 goto error;
10552 }
10553
10554 /* Create an image and pixmap serving as mask if the PNG image
10555 contains an alpha channel. */
10556 if (channels == 4
10557 && !transparent_p
10558 && !x_create_x_image_and_pixmap (f, width, height, 1,
10559 &mask_img, &img->mask))
10560 {
10561 x_destroy_x_image (ximg);
10562 XFreePixmap (FRAME_W32_DISPLAY (f), img->pixmap);
10563 img->pixmap = 0;
10564 UNBLOCK_INPUT;
10565 goto error;
10566 }
10567
10568 /* Fill the X image and mask from PNG data. */
10569 init_color_table ();
10570
10571 for (y = 0; y < height; ++y)
10572 {
10573 png_byte *p = rows[y];
10574
10575 for (x = 0; x < width; ++x)
10576 {
10577 unsigned r, g, b;
10578
10579 r = *p++ << 8;
10580 g = *p++ << 8;
10581 b = *p++ << 8;
10582 XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b));
10583
10584 /* An alpha channel, aka mask channel, associates variable
10585 transparency with an image. Where other image formats
10586 support binary transparency---fully transparent or fully
10587 opaque---PNG allows up to 254 levels of partial transparency.
10588 The PNG library implements partial transparency by combining
10589 the image with a specified background color.
10590
10591 I'm not sure how to handle this here nicely: because the
10592 background on which the image is displayed may change, for
10593 real alpha channel support, it would be necessary to create
10594 a new image for each possible background.
10595
10596 What I'm doing now is that a mask is created if we have
10597 boolean transparency information. Otherwise I'm using
10598 the frame's background color to combine the image with. */
10599
10600 if (channels == 4)
10601 {
10602 if (mask_img)
10603 XPutPixel (mask_img, x, y, *p > 0);
10604 ++p;
10605 }
10606 }
10607 }
10608
10609 /* Remember colors allocated for this image. */
10610 img->colors = colors_in_color_table (&img->ncolors);
10611 free_color_table ();
10612
10613 /* Clean up. */
10614 png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
10615 xfree (rows);
10616 xfree (pixels);
10617
10618 img->width = width;
10619 img->height = height;
10620
10621 /* Put the image into the pixmap, then free the X image and its buffer. */
10622 x_put_x_image (f, ximg, img->pixmap, width, height);
10623 x_destroy_x_image (ximg);
10624
10625 /* Same for the mask. */
10626 if (mask_img)
10627 {
10628 x_put_x_image (f, mask_img, img->mask, img->width, img->height);
10629 x_destroy_x_image (mask_img);
10630 }
10631
10632 UNBLOCK_INPUT;
10633 UNGCPRO;
10634 return 1;
10635}
10636
10637#endif /* HAVE_PNG != 0 */
10638
10639
10640\f
10641/***********************************************************************
10642 JPEG
10643 ***********************************************************************/
10644
10645#if HAVE_JPEG
10646
10647/* Work around a warning about HAVE_STDLIB_H being redefined in
10648 jconfig.h. */
10649#ifdef HAVE_STDLIB_H
10650#define HAVE_STDLIB_H_1
10651#undef HAVE_STDLIB_H
10652#endif /* HAVE_STLIB_H */
10653
10654#include <jpeglib.h>
10655#include <jerror.h>
10656#include <setjmp.h>
10657
10658#ifdef HAVE_STLIB_H_1
10659#define HAVE_STDLIB_H 1
10660#endif
10661
10662static int jpeg_image_p P_ ((Lisp_Object object));
10663static int jpeg_load P_ ((struct frame *f, struct image *img));
10664
10665/* The symbol `jpeg' identifying images of this type. */
10666
10667Lisp_Object Qjpeg;
10668
10669/* Indices of image specification fields in gs_format, below. */
10670
10671enum jpeg_keyword_index
10672{
10673 JPEG_TYPE,
10674 JPEG_DATA,
10675 JPEG_FILE,
10676 JPEG_ASCENT,
10677 JPEG_MARGIN,
10678 JPEG_RELIEF,
10679 JPEG_ALGORITHM,
10680 JPEG_HEURISTIC_MASK,
10681 JPEG_LAST
10682};
10683
10684/* Vector of image_keyword structures describing the format
10685 of valid user-defined image specifications. */
10686
10687static struct image_keyword jpeg_format[JPEG_LAST] =
10688{
10689 {":type", IMAGE_SYMBOL_VALUE, 1},
10690 {":data", IMAGE_STRING_VALUE, 0},
10691 {":file", IMAGE_STRING_VALUE, 0},
10692 {":ascent", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
10693 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
10694 {":relief", IMAGE_INTEGER_VALUE, 0},
10695 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
10696 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
10697};
10698
10699/* Structure describing the image type `jpeg'. */
10700
10701static struct image_type jpeg_type =
10702{
10703 &Qjpeg,
10704 jpeg_image_p,
10705 jpeg_load,
10706 x_clear_image,
10707 NULL
10708};
10709
10710
10711/* Return non-zero if OBJECT is a valid JPEG image specification. */
10712
10713static int
10714jpeg_image_p (object)
10715 Lisp_Object object;
10716{
10717 struct image_keyword fmt[JPEG_LAST];
10718
10719 bcopy (jpeg_format, fmt, sizeof fmt);
10720
10721 if (!parse_image_spec (object, fmt, JPEG_LAST, Qjpeg)
10722 || (fmt[JPEG_ASCENT].count
10723 && XFASTINT (fmt[JPEG_ASCENT].value) > 100))
10724 return 0;
10725
10726 /* Must specify either the :data or :file keyword. */
10727 return fmt[JPEG_FILE].count + fmt[JPEG_DATA].count == 1;
10728}
10729
10730
10731struct my_jpeg_error_mgr
10732{
10733 struct jpeg_error_mgr pub;
10734 jmp_buf setjmp_buffer;
10735};
10736
10737static void
10738my_error_exit (cinfo)
10739 j_common_ptr cinfo;
10740{
10741 struct my_jpeg_error_mgr *mgr = (struct my_jpeg_error_mgr *) cinfo->err;
10742 longjmp (mgr->setjmp_buffer, 1);
10743}
10744
6fc2811b
JR
10745/* Init source method for JPEG data source manager. Called by
10746 jpeg_read_header() before any data is actually read. See
10747 libjpeg.doc from the JPEG lib distribution. */
10748
10749static void
10750our_init_source (cinfo)
10751 j_decompress_ptr cinfo;
10752{
10753}
10754
10755
10756/* Fill input buffer method for JPEG data source manager. Called
10757 whenever more data is needed. We read the whole image in one step,
10758 so this only adds a fake end of input marker at the end. */
10759
10760static boolean
10761our_fill_input_buffer (cinfo)
10762 j_decompress_ptr cinfo;
10763{
10764 /* Insert a fake EOI marker. */
10765 struct jpeg_source_mgr *src = cinfo->src;
10766 static JOCTET buffer[2];
10767
10768 buffer[0] = (JOCTET) 0xFF;
10769 buffer[1] = (JOCTET) JPEG_EOI;
10770
10771 src->next_input_byte = buffer;
10772 src->bytes_in_buffer = 2;
10773 return TRUE;
10774}
10775
10776
10777/* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
10778 is the JPEG data source manager. */
10779
10780static void
10781our_skip_input_data (cinfo, num_bytes)
10782 j_decompress_ptr cinfo;
10783 long num_bytes;
10784{
10785 struct jpeg_source_mgr *src = (struct jpeg_source_mgr *) cinfo->src;
10786
10787 if (src)
10788 {
10789 if (num_bytes > src->bytes_in_buffer)
10790 ERREXIT (cinfo, JERR_INPUT_EOF);
10791
10792 src->bytes_in_buffer -= num_bytes;
10793 src->next_input_byte += num_bytes;
10794 }
10795}
10796
10797
10798/* Method to terminate data source. Called by
10799 jpeg_finish_decompress() after all data has been processed. */
10800
10801static void
10802our_term_source (cinfo)
10803 j_decompress_ptr cinfo;
10804{
10805}
10806
10807
10808/* Set up the JPEG lib for reading an image from DATA which contains
10809 LEN bytes. CINFO is the decompression info structure created for
10810 reading the image. */
10811
10812static void
10813jpeg_memory_src (cinfo, data, len)
10814 j_decompress_ptr cinfo;
10815 JOCTET *data;
10816 unsigned int len;
10817{
10818 struct jpeg_source_mgr *src;
10819
10820 if (cinfo->src == NULL)
10821 {
10822 /* First time for this JPEG object? */
10823 cinfo->src = (struct jpeg_source_mgr *)
10824 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
10825 sizeof (struct jpeg_source_mgr));
10826 src = (struct jpeg_source_mgr *) cinfo->src;
10827 src->next_input_byte = data;
10828 }
10829
10830 src = (struct jpeg_source_mgr *) cinfo->src;
10831 src->init_source = our_init_source;
10832 src->fill_input_buffer = our_fill_input_buffer;
10833 src->skip_input_data = our_skip_input_data;
10834 src->resync_to_restart = jpeg_resync_to_restart; /* Use default method. */
10835 src->term_source = our_term_source;
10836 src->bytes_in_buffer = len;
10837 src->next_input_byte = data;
10838}
10839
10840
10841/* Load image IMG for use on frame F. Patterned after example.c
10842 from the JPEG lib. */
10843
10844static int
10845jpeg_load (f, img)
10846 struct frame *f;
10847 struct image *img;
10848{
10849 struct jpeg_decompress_struct cinfo;
10850 struct my_jpeg_error_mgr mgr;
10851 Lisp_Object file, specified_file;
10852 Lisp_Object specified_data;
10853 FILE *fp = NULL;
10854 JSAMPARRAY buffer;
10855 int row_stride, x, y;
10856 XImage *ximg = NULL;
10857 int rc;
10858 unsigned long *colors;
10859 int width, height;
10860 struct gcpro gcpro1;
10861
10862 /* Open the JPEG file. */
10863 specified_file = image_spec_value (img->spec, QCfile, NULL);
10864 specified_data = image_spec_value (img->spec, QCdata, NULL);
10865 file = Qnil;
10866 GCPRO1 (file);
10867
6fc2811b
JR
10868 if (NILP (specified_data))
10869 {
10870 file = x_find_image_file (specified_file);
10871 if (!STRINGP (file))
10872 {
10873 image_error ("Cannot find image file `%s'", specified_file, Qnil);
10874 UNGCPRO;
10875 return 0;
10876 }
10877
10878 fp = fopen (XSTRING (file)->data, "r");
10879 if (fp == NULL)
10880 {
10881 image_error ("Cannot open `%s'", file, Qnil);
10882 UNGCPRO;
10883 return 0;
10884 }
10885 }
10886
10887 /* Customize libjpeg's error handling to call my_error_exit when an
10888 error is detected. This function will perform a longjmp. */
10889 mgr.pub.error_exit = my_error_exit;
10890 cinfo.err = jpeg_std_error (&mgr.pub);
10891
10892 if ((rc = setjmp (mgr.setjmp_buffer)) != 0)
10893 {
10894 if (rc == 1)
10895 {
10896 /* Called from my_error_exit. Display a JPEG error. */
10897 char buffer[JMSG_LENGTH_MAX];
10898 cinfo.err->format_message ((j_common_ptr) &cinfo, buffer);
10899 image_error ("Error reading JPEG image `%s': %s", img->spec,
10900 build_string (buffer));
10901 }
10902
10903 /* Close the input file and destroy the JPEG object. */
10904 if (fp)
10905 fclose (fp);
10906 jpeg_destroy_decompress (&cinfo);
10907
10908 BLOCK_INPUT;
10909
10910 /* If we already have an XImage, free that. */
10911 x_destroy_x_image (ximg);
10912
10913 /* Free pixmap and colors. */
10914 x_clear_image (f, img);
10915
10916 UNBLOCK_INPUT;
10917 UNGCPRO;
10918 return 0;
10919 }
10920
10921 /* Create the JPEG decompression object. Let it read from fp.
10922 Read the JPEG image header. */
10923 jpeg_create_decompress (&cinfo);
10924
10925 if (NILP (specified_data))
10926 jpeg_stdio_src (&cinfo, fp);
10927 else
10928 jpeg_memory_src (&cinfo, XSTRING (specified_data)->data,
10929 STRING_BYTES (XSTRING (specified_data)));
10930
10931 jpeg_read_header (&cinfo, TRUE);
10932
10933 /* Customize decompression so that color quantization will be used.
10934 Start decompression. */
10935 cinfo.quantize_colors = TRUE;
10936 jpeg_start_decompress (&cinfo);
10937 width = img->width = cinfo.output_width;
10938 height = img->height = cinfo.output_height;
10939
10940 BLOCK_INPUT;
10941
10942 /* Create X image and pixmap. */
10943 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg,
10944 &img->pixmap))
10945 {
10946 UNBLOCK_INPUT;
10947 longjmp (mgr.setjmp_buffer, 2);
10948 }
10949
10950 /* Allocate colors. When color quantization is used,
10951 cinfo.actual_number_of_colors has been set with the number of
10952 colors generated, and cinfo.colormap is a two-dimensional array
10953 of color indices in the range 0..cinfo.actual_number_of_colors.
10954 No more than 255 colors will be generated. */
10955 {
10956 int i, ir, ig, ib;
10957
10958 if (cinfo.out_color_components > 2)
10959 ir = 0, ig = 1, ib = 2;
10960 else if (cinfo.out_color_components > 1)
10961 ir = 0, ig = 1, ib = 0;
10962 else
10963 ir = 0, ig = 0, ib = 0;
10964
10965 /* Use the color table mechanism because it handles colors that
10966 cannot be allocated nicely. Such colors will be replaced with
10967 a default color, and we don't have to care about which colors
10968 can be freed safely, and which can't. */
10969 init_color_table ();
10970 colors = (unsigned long *) alloca (cinfo.actual_number_of_colors
10971 * sizeof *colors);
10972
10973 for (i = 0; i < cinfo.actual_number_of_colors; ++i)
10974 {
10975 /* Multiply RGB values with 255 because X expects RGB values
10976 in the range 0..0xffff. */
10977 int r = cinfo.colormap[ir][i] << 8;
10978 int g = cinfo.colormap[ig][i] << 8;
10979 int b = cinfo.colormap[ib][i] << 8;
10980 colors[i] = lookup_rgb_color (f, r, g, b);
10981 }
10982
10983 /* Remember those colors actually allocated. */
10984 img->colors = colors_in_color_table (&img->ncolors);
10985 free_color_table ();
10986 }
10987
10988 /* Read pixels. */
10989 row_stride = width * cinfo.output_components;
10990 buffer = cinfo.mem->alloc_sarray ((j_common_ptr) &cinfo, JPOOL_IMAGE,
10991 row_stride, 1);
10992 for (y = 0; y < height; ++y)
10993 {
10994 jpeg_read_scanlines (&cinfo, buffer, 1);
10995 for (x = 0; x < cinfo.output_width; ++x)
10996 XPutPixel (ximg, x, y, colors[buffer[0][x]]);
10997 }
10998
10999 /* Clean up. */
11000 jpeg_finish_decompress (&cinfo);
11001 jpeg_destroy_decompress (&cinfo);
11002 if (fp)
11003 fclose (fp);
11004
11005 /* Put the image into the pixmap. */
11006 x_put_x_image (f, ximg, img->pixmap, width, height);
11007 x_destroy_x_image (ximg);
11008 UNBLOCK_INPUT;
11009 UNGCPRO;
11010 return 1;
11011}
11012
11013#endif /* HAVE_JPEG */
11014
11015
11016\f
11017/***********************************************************************
11018 TIFF
11019 ***********************************************************************/
11020
11021#if HAVE_TIFF
11022
11023#include <tiffio.h>
11024
11025static int tiff_image_p P_ ((Lisp_Object object));
11026static int tiff_load P_ ((struct frame *f, struct image *img));
11027
11028/* The symbol `tiff' identifying images of this type. */
11029
11030Lisp_Object Qtiff;
11031
11032/* Indices of image specification fields in tiff_format, below. */
11033
11034enum tiff_keyword_index
11035{
11036 TIFF_TYPE,
11037 TIFF_DATA,
11038 TIFF_FILE,
11039 TIFF_ASCENT,
11040 TIFF_MARGIN,
11041 TIFF_RELIEF,
11042 TIFF_ALGORITHM,
11043 TIFF_HEURISTIC_MASK,
11044 TIFF_LAST
11045};
11046
11047/* Vector of image_keyword structures describing the format
11048 of valid user-defined image specifications. */
11049
11050static struct image_keyword tiff_format[TIFF_LAST] =
11051{
11052 {":type", IMAGE_SYMBOL_VALUE, 1},
11053 {":data", IMAGE_STRING_VALUE, 0},
11054 {":file", IMAGE_STRING_VALUE, 0},
11055 {":ascent", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
11056 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
11057 {":relief", IMAGE_INTEGER_VALUE, 0},
11058 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
11059 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
11060};
11061
11062/* Structure describing the image type `tiff'. */
11063
11064static struct image_type tiff_type =
11065{
11066 &Qtiff,
11067 tiff_image_p,
11068 tiff_load,
11069 x_clear_image,
11070 NULL
11071};
11072
11073
11074/* Return non-zero if OBJECT is a valid TIFF image specification. */
11075
11076static int
11077tiff_image_p (object)
11078 Lisp_Object object;
11079{
11080 struct image_keyword fmt[TIFF_LAST];
11081 bcopy (tiff_format, fmt, sizeof fmt);
11082
11083 if (!parse_image_spec (object, fmt, TIFF_LAST, Qtiff)
11084 || (fmt[TIFF_ASCENT].count
11085 && XFASTINT (fmt[TIFF_ASCENT].value) > 100))
11086 return 0;
11087
11088 /* Must specify either the :data or :file keyword. */
11089 return fmt[TIFF_FILE].count + fmt[TIFF_DATA].count == 1;
11090}
11091
11092
11093/* Reading from a memory buffer for TIFF images Based on the PNG
11094 memory source, but we have to provide a lot of extra functions.
11095 Blah.
11096
11097 We really only need to implement read and seek, but I am not
11098 convinced that the TIFF library is smart enough not to destroy
11099 itself if we only hand it the function pointers we need to
11100 override. */
11101
11102typedef struct
11103{
11104 unsigned char *bytes;
11105 size_t len;
11106 int index;
11107}
11108tiff_memory_source;
11109
11110static size_t
11111tiff_read_from_memory (data, buf, size)
11112 thandle_t data;
11113 tdata_t buf;
11114 tsize_t size;
11115{
11116 tiff_memory_source *src = (tiff_memory_source *) data;
11117
11118 if (size > src->len - src->index)
11119 return (size_t) -1;
11120 bcopy (src->bytes + src->index, buf, size);
11121 src->index += size;
11122 return size;
11123}
11124
11125static size_t
11126tiff_write_from_memory (data, buf, size)
11127 thandle_t data;
11128 tdata_t buf;
11129 tsize_t size;
11130{
11131 return (size_t) -1;
11132}
11133
11134static toff_t
11135tiff_seek_in_memory (data, off, whence)
11136 thandle_t data;
11137 toff_t off;
11138 int whence;
11139{
11140 tiff_memory_source *src = (tiff_memory_source *) data;
11141 int idx;
11142
11143 switch (whence)
11144 {
11145 case SEEK_SET: /* Go from beginning of source. */
11146 idx = off;
11147 break;
11148
11149 case SEEK_END: /* Go from end of source. */
11150 idx = src->len + off;
11151 break;
11152
11153 case SEEK_CUR: /* Go from current position. */
11154 idx = src->index + off;
11155 break;
11156
11157 default: /* Invalid `whence'. */
11158 return -1;
11159 }
11160
11161 if (idx > src->len || idx < 0)
11162 return -1;
11163
11164 src->index = idx;
11165 return src->index;
11166}
11167
11168static int
11169tiff_close_memory (data)
11170 thandle_t data;
11171{
11172 /* NOOP */
11173 return 0;
11174}
11175
11176static int
11177tiff_mmap_memory (data, pbase, psize)
11178 thandle_t data;
11179 tdata_t *pbase;
11180 toff_t *psize;
11181{
11182 /* It is already _IN_ memory. */
11183 return 0;
11184}
11185
11186static void
11187tiff_unmap_memory (data, base, size)
11188 thandle_t data;
11189 tdata_t base;
11190 toff_t size;
11191{
11192 /* We don't need to do this. */
11193}
11194
11195static toff_t
11196tiff_size_of_memory (data)
11197 thandle_t data;
11198{
11199 return ((tiff_memory_source *) data)->len;
11200}
11201
6fc2811b
JR
11202/* Load TIFF image IMG for use on frame F. Value is non-zero if
11203 successful. */
11204
11205static int
11206tiff_load (f, img)
11207 struct frame *f;
11208 struct image *img;
11209{
11210 Lisp_Object file, specified_file;
11211 Lisp_Object specified_data;
11212 TIFF *tiff;
11213 int width, height, x, y;
11214 uint32 *buf;
11215 int rc;
11216 XImage *ximg;
11217 struct gcpro gcpro1;
11218 tiff_memory_source memsrc;
11219
11220 specified_file = image_spec_value (img->spec, QCfile, NULL);
11221 specified_data = image_spec_value (img->spec, QCdata, NULL);
11222 file = Qnil;
11223 GCPRO1 (file);
11224
11225 if (NILP (specified_data))
11226 {
11227 /* Read from a file */
11228 file = x_find_image_file (specified_file);
11229 if (!STRINGP (file))
11230 {
11231 image_error ("Cannot find image file `%s'", file, Qnil);
11232 UNGCPRO;
11233 return 0;
11234 }
11235
11236 /* Try to open the image file. */
11237 tiff = TIFFOpen (XSTRING (file)->data, "r");
11238 if (tiff == NULL)
11239 {
11240 image_error ("Cannot open `%s'", file, Qnil);
11241 UNGCPRO;
11242 return 0;
11243 }
11244 }
11245 else
11246 {
11247 /* Memory source! */
11248 memsrc.bytes = XSTRING (specified_data)->data;
11249 memsrc.len = STRING_BYTES (XSTRING (specified_data));
11250 memsrc.index = 0;
11251
11252 tiff = TIFFClientOpen ("memory_source", "r", &memsrc,
11253 (TIFFReadWriteProc) tiff_read_from_memory,
11254 (TIFFReadWriteProc) tiff_write_from_memory,
11255 tiff_seek_in_memory,
11256 tiff_close_memory,
11257 tiff_size_of_memory,
11258 tiff_mmap_memory,
11259 tiff_unmap_memory);
11260
11261 if (!tiff)
11262 {
11263 image_error ("Cannot open memory source for `%s'", img->spec, Qnil);
11264 UNGCPRO;
11265 return 0;
11266 }
11267 }
11268
11269 /* Get width and height of the image, and allocate a raster buffer
11270 of width x height 32-bit values. */
11271 TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &width);
11272 TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &height);
11273 buf = (uint32 *) xmalloc (width * height * sizeof *buf);
11274
11275 rc = TIFFReadRGBAImage (tiff, width, height, buf, 0);
11276 TIFFClose (tiff);
11277 if (!rc)
11278 {
11279 image_error ("Error reading TIFF image `%s'", img->spec, Qnil);
11280 xfree (buf);
11281 UNGCPRO;
11282 return 0;
11283 }
11284
11285 BLOCK_INPUT;
11286
11287 /* Create the X image and pixmap. */
11288 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
11289 {
11290 UNBLOCK_INPUT;
11291 xfree (buf);
11292 UNGCPRO;
11293 return 0;
11294 }
11295
11296 /* Initialize the color table. */
11297 init_color_table ();
11298
11299 /* Process the pixel raster. Origin is in the lower-left corner. */
11300 for (y = 0; y < height; ++y)
11301 {
11302 uint32 *row = buf + y * width;
11303
11304 for (x = 0; x < width; ++x)
11305 {
11306 uint32 abgr = row[x];
11307 int r = TIFFGetR (abgr) << 8;
11308 int g = TIFFGetG (abgr) << 8;
11309 int b = TIFFGetB (abgr) << 8;
11310 XPutPixel (ximg, x, height - 1 - y, lookup_rgb_color (f, r, g, b));
11311 }
11312 }
11313
11314 /* Remember the colors allocated for the image. Free the color table. */
11315 img->colors = colors_in_color_table (&img->ncolors);
11316 free_color_table ();
11317
11318 /* Put the image into the pixmap, then free the X image and its buffer. */
11319 x_put_x_image (f, ximg, img->pixmap, width, height);
11320 x_destroy_x_image (ximg);
11321 xfree (buf);
11322 UNBLOCK_INPUT;
11323
11324 img->width = width;
11325 img->height = height;
11326
11327 UNGCPRO;
11328 return 1;
11329}
11330
11331#endif /* HAVE_TIFF != 0 */
11332
11333
11334\f
11335/***********************************************************************
11336 GIF
11337 ***********************************************************************/
11338
11339#if HAVE_GIF
11340
11341#include <gif_lib.h>
11342
11343static int gif_image_p P_ ((Lisp_Object object));
11344static int gif_load P_ ((struct frame *f, struct image *img));
11345
11346/* The symbol `gif' identifying images of this type. */
11347
11348Lisp_Object Qgif;
11349
11350/* Indices of image specification fields in gif_format, below. */
11351
11352enum gif_keyword_index
11353{
11354 GIF_TYPE,
11355 GIF_DATA,
11356 GIF_FILE,
11357 GIF_ASCENT,
11358 GIF_MARGIN,
11359 GIF_RELIEF,
11360 GIF_ALGORITHM,
11361 GIF_HEURISTIC_MASK,
11362 GIF_IMAGE,
11363 GIF_LAST
11364};
11365
11366/* Vector of image_keyword structures describing the format
11367 of valid user-defined image specifications. */
11368
11369static struct image_keyword gif_format[GIF_LAST] =
11370{
11371 {":type", IMAGE_SYMBOL_VALUE, 1},
11372 {":data", IMAGE_STRING_VALUE, 0},
11373 {":file", IMAGE_STRING_VALUE, 0},
11374 {":ascent", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
11375 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
11376 {":relief", IMAGE_INTEGER_VALUE, 0},
11377 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
11378 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
11379 {":image", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0}
11380};
11381
11382/* Structure describing the image type `gif'. */
11383
11384static struct image_type gif_type =
11385{
11386 &Qgif,
11387 gif_image_p,
11388 gif_load,
11389 x_clear_image,
11390 NULL
11391};
11392
11393/* Return non-zero if OBJECT is a valid GIF image specification. */
11394
11395static int
11396gif_image_p (object)
11397 Lisp_Object object;
11398{
11399 struct image_keyword fmt[GIF_LAST];
11400 bcopy (gif_format, fmt, sizeof fmt);
11401
11402 if (!parse_image_spec (object, fmt, GIF_LAST, Qgif)
11403 || (fmt[GIF_ASCENT].count
11404 && XFASTINT (fmt[GIF_ASCENT].value) > 100))
11405 return 0;
11406
11407 /* Must specify either the :data or :file keyword. */
11408 return fmt[GIF_FILE].count + fmt[GIF_DATA].count == 1;
11409}
11410
11411/* Reading a GIF image from memory
11412 Based on the PNG memory stuff to a certain extent. */
11413
11414typedef struct
11415{
11416 unsigned char *bytes;
11417 size_t len;
11418 int index;
11419}
11420gif_memory_source;
11421
11422/* Make the current memory source available to gif_read_from_memory.
11423 It's done this way because not all versions of libungif support
11424 a UserData field in the GifFileType structure. */
11425static gif_memory_source *current_gif_memory_src;
11426
11427static int
11428gif_read_from_memory (file, buf, len)
11429 GifFileType *file;
11430 GifByteType *buf;
11431 int len;
11432{
11433 gif_memory_source *src = current_gif_memory_src;
11434
11435 if (len > src->len - src->index)
11436 return -1;
11437
11438 bcopy (src->bytes + src->index, buf, len);
11439 src->index += len;
11440 return len;
11441}
11442
11443
11444/* Load GIF image IMG for use on frame F. Value is non-zero if
11445 successful. */
11446
11447static int
11448gif_load (f, img)
11449 struct frame *f;
11450 struct image *img;
11451{
11452 Lisp_Object file, specified_file;
11453 Lisp_Object specified_data;
11454 int rc, width, height, x, y, i;
11455 XImage *ximg;
11456 ColorMapObject *gif_color_map;
11457 unsigned long pixel_colors[256];
11458 GifFileType *gif;
11459 struct gcpro gcpro1;
11460 Lisp_Object image;
11461 int ino, image_left, image_top, image_width, image_height;
11462 gif_memory_source memsrc;
11463 unsigned char *raster;
11464
11465 specified_file = image_spec_value (img->spec, QCfile, NULL);
11466 specified_data = image_spec_value (img->spec, QCdata, NULL);
11467 file = Qnil;
dfff8a69 11468 GCPRO1 (file);
6fc2811b
JR
11469
11470 if (NILP (specified_data))
11471 {
11472 file = x_find_image_file (specified_file);
6fc2811b
JR
11473 if (!STRINGP (file))
11474 {
11475 image_error ("Cannot find image file `%s'", specified_file, Qnil);
11476 UNGCPRO;
11477 return 0;
11478 }
11479
11480 /* Open the GIF file. */
11481 gif = DGifOpenFileName (XSTRING (file)->data);
11482 if (gif == NULL)
11483 {
11484 image_error ("Cannot open `%s'", file, Qnil);
11485 UNGCPRO;
11486 return 0;
11487 }
11488 }
11489 else
11490 {
11491 /* Read from memory! */
11492 current_gif_memory_src = &memsrc;
11493 memsrc.bytes = XSTRING (specified_data)->data;
11494 memsrc.len = STRING_BYTES (XSTRING (specified_data));
11495 memsrc.index = 0;
11496
11497 gif = DGifOpen(&memsrc, gif_read_from_memory);
11498 if (!gif)
11499 {
11500 image_error ("Cannot open memory source `%s'", img->spec, Qnil);
11501 UNGCPRO;
11502 return 0;
11503 }
11504 }
11505
11506 /* Read entire contents. */
11507 rc = DGifSlurp (gif);
11508 if (rc == GIF_ERROR)
11509 {
11510 image_error ("Error reading `%s'", img->spec, Qnil);
11511 DGifCloseFile (gif);
11512 UNGCPRO;
11513 return 0;
11514 }
11515
11516 image = image_spec_value (img->spec, QCindex, NULL);
11517 ino = INTEGERP (image) ? XFASTINT (image) : 0;
11518 if (ino >= gif->ImageCount)
11519 {
11520 image_error ("Invalid image number `%s' in image `%s'",
11521 image, img->spec);
11522 DGifCloseFile (gif);
11523 UNGCPRO;
11524 return 0;
11525 }
11526
11527 width = img->width = gif->SWidth;
11528 height = img->height = gif->SHeight;
11529
11530 BLOCK_INPUT;
11531
11532 /* Create the X image and pixmap. */
11533 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
11534 {
11535 UNBLOCK_INPUT;
11536 DGifCloseFile (gif);
11537 UNGCPRO;
11538 return 0;
11539 }
11540
11541 /* Allocate colors. */
11542 gif_color_map = gif->SavedImages[ino].ImageDesc.ColorMap;
11543 if (!gif_color_map)
11544 gif_color_map = gif->SColorMap;
11545 init_color_table ();
11546 bzero (pixel_colors, sizeof pixel_colors);
11547
11548 for (i = 0; i < gif_color_map->ColorCount; ++i)
11549 {
11550 int r = gif_color_map->Colors[i].Red << 8;
11551 int g = gif_color_map->Colors[i].Green << 8;
11552 int b = gif_color_map->Colors[i].Blue << 8;
11553 pixel_colors[i] = lookup_rgb_color (f, r, g, b);
11554 }
11555
11556 img->colors = colors_in_color_table (&img->ncolors);
11557 free_color_table ();
11558
11559 /* Clear the part of the screen image that are not covered by
11560 the image from the GIF file. Full animated GIF support
11561 requires more than can be done here (see the gif89 spec,
11562 disposal methods). Let's simply assume that the part
11563 not covered by a sub-image is in the frame's background color. */
11564 image_top = gif->SavedImages[ino].ImageDesc.Top;
11565 image_left = gif->SavedImages[ino].ImageDesc.Left;
11566 image_width = gif->SavedImages[ino].ImageDesc.Width;
11567 image_height = gif->SavedImages[ino].ImageDesc.Height;
11568
11569 for (y = 0; y < image_top; ++y)
11570 for (x = 0; x < width; ++x)
11571 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
11572
11573 for (y = image_top + image_height; y < height; ++y)
11574 for (x = 0; x < width; ++x)
11575 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
11576
11577 for (y = image_top; y < image_top + image_height; ++y)
11578 {
11579 for (x = 0; x < image_left; ++x)
11580 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
11581 for (x = image_left + image_width; x < width; ++x)
11582 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
11583 }
11584
11585 /* Read the GIF image into the X image. We use a local variable
11586 `raster' here because RasterBits below is a char *, and invites
11587 problems with bytes >= 0x80. */
11588 raster = (unsigned char *) gif->SavedImages[ino].RasterBits;
11589
11590 if (gif->SavedImages[ino].ImageDesc.Interlace)
11591 {
11592 static int interlace_start[] = {0, 4, 2, 1};
11593 static int interlace_increment[] = {8, 8, 4, 2};
11594 int pass, inc;
11595 int row = interlace_start[0];
11596
11597 pass = 0;
11598
11599 for (y = 0; y < image_height; y++)
11600 {
11601 if (row >= image_height)
11602 {
11603 row = interlace_start[++pass];
11604 while (row >= image_height)
11605 row = interlace_start[++pass];
11606 }
11607
11608 for (x = 0; x < image_width; x++)
11609 {
11610 int i = raster[(y * image_width) + x];
11611 XPutPixel (ximg, x + image_left, row + image_top,
11612 pixel_colors[i]);
11613 }
11614
11615 row += interlace_increment[pass];
11616 }
11617 }
11618 else
11619 {
11620 for (y = 0; y < image_height; ++y)
11621 for (x = 0; x < image_width; ++x)
11622 {
11623 int i = raster[y* image_width + x];
11624 XPutPixel (ximg, x + image_left, y + image_top, pixel_colors[i]);
11625 }
11626 }
11627
11628 DGifCloseFile (gif);
11629
11630 /* Put the image into the pixmap, then free the X image and its buffer. */
11631 x_put_x_image (f, ximg, img->pixmap, width, height);
11632 x_destroy_x_image (ximg);
11633 UNBLOCK_INPUT;
11634
11635 UNGCPRO;
11636 return 1;
11637}
11638
11639#endif /* HAVE_GIF != 0 */
11640
11641
11642\f
11643/***********************************************************************
11644 Ghostscript
11645 ***********************************************************************/
11646
11647#ifdef HAVE_GHOSTSCRIPT
11648static int gs_image_p P_ ((Lisp_Object object));
11649static int gs_load P_ ((struct frame *f, struct image *img));
11650static void gs_clear_image P_ ((struct frame *f, struct image *img));
11651
11652/* The symbol `postscript' identifying images of this type. */
11653
11654Lisp_Object Qpostscript;
11655
11656/* Keyword symbols. */
11657
11658Lisp_Object QCloader, QCbounding_box, QCpt_width, QCpt_height;
11659
11660/* Indices of image specification fields in gs_format, below. */
11661
11662enum gs_keyword_index
11663{
11664 GS_TYPE,
11665 GS_PT_WIDTH,
11666 GS_PT_HEIGHT,
11667 GS_FILE,
11668 GS_LOADER,
11669 GS_BOUNDING_BOX,
11670 GS_ASCENT,
11671 GS_MARGIN,
11672 GS_RELIEF,
11673 GS_ALGORITHM,
11674 GS_HEURISTIC_MASK,
11675 GS_LAST
11676};
11677
11678/* Vector of image_keyword structures describing the format
11679 of valid user-defined image specifications. */
11680
11681static struct image_keyword gs_format[GS_LAST] =
11682{
11683 {":type", IMAGE_SYMBOL_VALUE, 1},
11684 {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE, 1},
11685 {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE, 1},
11686 {":file", IMAGE_STRING_VALUE, 1},
11687 {":loader", IMAGE_FUNCTION_VALUE, 0},
11688 {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE, 1},
11689 {":ascent", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
11690 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
11691 {":relief", IMAGE_INTEGER_VALUE, 0},
11692 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
11693 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
11694};
11695
11696/* Structure describing the image type `ghostscript'. */
11697
11698static struct image_type gs_type =
11699{
11700 &Qpostscript,
11701 gs_image_p,
11702 gs_load,
11703 gs_clear_image,
11704 NULL
11705};
11706
11707
11708/* Free X resources of Ghostscript image IMG which is used on frame F. */
11709
11710static void
11711gs_clear_image (f, img)
11712 struct frame *f;
11713 struct image *img;
11714{
11715 /* IMG->data.ptr_val may contain a recorded colormap. */
11716 xfree (img->data.ptr_val);
11717 x_clear_image (f, img);
11718}
11719
11720
11721/* Return non-zero if OBJECT is a valid Ghostscript image
11722 specification. */
11723
11724static int
11725gs_image_p (object)
11726 Lisp_Object object;
11727{
11728 struct image_keyword fmt[GS_LAST];
11729 Lisp_Object tem;
11730 int i;
11731
11732 bcopy (gs_format, fmt, sizeof fmt);
11733
11734 if (!parse_image_spec (object, fmt, GS_LAST, Qpostscript)
11735 || (fmt[GS_ASCENT].count
11736 && XFASTINT (fmt[GS_ASCENT].value) > 100))
11737 return 0;
11738
11739 /* Bounding box must be a list or vector containing 4 integers. */
11740 tem = fmt[GS_BOUNDING_BOX].value;
11741 if (CONSP (tem))
11742 {
11743 for (i = 0; i < 4; ++i, tem = XCDR (tem))
11744 if (!CONSP (tem) || !INTEGERP (XCAR (tem)))
11745 return 0;
11746 if (!NILP (tem))
11747 return 0;
11748 }
11749 else if (VECTORP (tem))
11750 {
11751 if (XVECTOR (tem)->size != 4)
11752 return 0;
11753 for (i = 0; i < 4; ++i)
11754 if (!INTEGERP (XVECTOR (tem)->contents[i]))
11755 return 0;
11756 }
11757 else
11758 return 0;
11759
11760 return 1;
11761}
11762
11763
11764/* Load Ghostscript image IMG for use on frame F. Value is non-zero
11765 if successful. */
11766
11767static int
11768gs_load (f, img)
11769 struct frame *f;
11770 struct image *img;
11771{
11772 char buffer[100];
11773 Lisp_Object window_and_pixmap_id = Qnil, loader, pt_height, pt_width;
11774 struct gcpro gcpro1, gcpro2;
11775 Lisp_Object frame;
11776 double in_width, in_height;
11777 Lisp_Object pixel_colors = Qnil;
11778
11779 /* Compute pixel size of pixmap needed from the given size in the
11780 image specification. Sizes in the specification are in pt. 1 pt
11781 = 1/72 in, xdpi and ydpi are stored in the frame's X display
11782 info. */
11783 pt_width = image_spec_value (img->spec, QCpt_width, NULL);
11784 in_width = XFASTINT (pt_width) / 72.0;
11785 img->width = in_width * FRAME_W32_DISPLAY_INFO (f)->resx;
11786 pt_height = image_spec_value (img->spec, QCpt_height, NULL);
11787 in_height = XFASTINT (pt_height) / 72.0;
11788 img->height = in_height * FRAME_W32_DISPLAY_INFO (f)->resy;
11789
11790 /* Create the pixmap. */
11791 BLOCK_INPUT;
11792 xassert (img->pixmap == 0);
11793 img->pixmap = XCreatePixmap (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
11794 img->width, img->height,
11795 DefaultDepthOfScreen (FRAME_X_SCREEN (f)));
11796 UNBLOCK_INPUT;
11797
11798 if (!img->pixmap)
11799 {
11800 image_error ("Unable to create pixmap for `%s'", img->spec, Qnil);
11801 return 0;
11802 }
11803
11804 /* Call the loader to fill the pixmap. It returns a process object
11805 if successful. We do not record_unwind_protect here because
11806 other places in redisplay like calling window scroll functions
11807 don't either. Let the Lisp loader use `unwind-protect' instead. */
11808 GCPRO2 (window_and_pixmap_id, pixel_colors);
11809
11810 sprintf (buffer, "%lu %lu",
11811 (unsigned long) FRAME_W32_WINDOW (f),
11812 (unsigned long) img->pixmap);
11813 window_and_pixmap_id = build_string (buffer);
11814
11815 sprintf (buffer, "%lu %lu",
11816 FRAME_FOREGROUND_PIXEL (f),
11817 FRAME_BACKGROUND_PIXEL (f));
11818 pixel_colors = build_string (buffer);
11819
11820 XSETFRAME (frame, f);
11821 loader = image_spec_value (img->spec, QCloader, NULL);
11822 if (NILP (loader))
11823 loader = intern ("gs-load-image");
11824
11825 img->data.lisp_val = call6 (loader, frame, img->spec,
11826 make_number (img->width),
11827 make_number (img->height),
11828 window_and_pixmap_id,
11829 pixel_colors);
11830 UNGCPRO;
11831 return PROCESSP (img->data.lisp_val);
11832}
11833
11834
11835/* Kill the Ghostscript process that was started to fill PIXMAP on
11836 frame F. Called from XTread_socket when receiving an event
11837 telling Emacs that Ghostscript has finished drawing. */
11838
11839void
11840x_kill_gs_process (pixmap, f)
11841 Pixmap pixmap;
11842 struct frame *f;
11843{
11844 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
11845 int class, i;
11846 struct image *img;
11847
11848 /* Find the image containing PIXMAP. */
11849 for (i = 0; i < c->used; ++i)
11850 if (c->images[i]->pixmap == pixmap)
11851 break;
11852
11853 /* Kill the GS process. We should have found PIXMAP in the image
11854 cache and its image should contain a process object. */
11855 xassert (i < c->used);
11856 img = c->images[i];
11857 xassert (PROCESSP (img->data.lisp_val));
11858 Fkill_process (img->data.lisp_val, Qnil);
11859 img->data.lisp_val = Qnil;
11860
11861 /* On displays with a mutable colormap, figure out the colors
11862 allocated for the image by looking at the pixels of an XImage for
11863 img->pixmap. */
11864 class = FRAME_W32_DISPLAY_INFO (f)->visual->class;
11865 if (class != StaticColor && class != StaticGray && class != TrueColor)
11866 {
11867 XImage *ximg;
11868
11869 BLOCK_INPUT;
11870
11871 /* Try to get an XImage for img->pixmep. */
11872 ximg = XGetImage (FRAME_W32_DISPLAY (f), img->pixmap,
11873 0, 0, img->width, img->height, ~0, ZPixmap);
11874 if (ximg)
11875 {
11876 int x, y;
11877
11878 /* Initialize the color table. */
11879 init_color_table ();
11880
11881 /* For each pixel of the image, look its color up in the
11882 color table. After having done so, the color table will
11883 contain an entry for each color used by the image. */
11884 for (y = 0; y < img->height; ++y)
11885 for (x = 0; x < img->width; ++x)
11886 {
11887 unsigned long pixel = XGetPixel (ximg, x, y);
11888 lookup_pixel_color (f, pixel);
11889 }
11890
11891 /* Record colors in the image. Free color table and XImage. */
11892 img->colors = colors_in_color_table (&img->ncolors);
11893 free_color_table ();
11894 XDestroyImage (ximg);
11895
11896#if 0 /* This doesn't seem to be the case. If we free the colors
11897 here, we get a BadAccess later in x_clear_image when
11898 freeing the colors. */
11899 /* We have allocated colors once, but Ghostscript has also
11900 allocated colors on behalf of us. So, to get the
11901 reference counts right, free them once. */
11902 if (img->ncolors)
11903 {
11904 Colormap cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f));
11905 XFreeColors (FRAME_W32_DISPLAY (f), cmap,
11906 img->colors, img->ncolors, 0);
11907 }
11908#endif
11909 }
11910 else
11911 image_error ("Cannot get X image of `%s'; colors will not be freed",
11912 img->spec, Qnil);
11913
11914 UNBLOCK_INPUT;
11915 }
11916}
11917
11918#endif /* HAVE_GHOSTSCRIPT */
11919
11920\f
11921/***********************************************************************
11922 Window properties
11923 ***********************************************************************/
11924
11925DEFUN ("x-change-window-property", Fx_change_window_property,
11926 Sx_change_window_property, 2, 3, 0,
11927 "Change window property PROP to VALUE on the X window of FRAME.\n\
11928PROP and VALUE must be strings. FRAME nil or omitted means use the\n\
11929selected frame. Value is VALUE.")
11930 (prop, value, frame)
11931 Lisp_Object frame, prop, value;
11932{
767b1ff0 11933#if 0 /* TODO : port window properties to W32 */
6fc2811b
JR
11934 struct frame *f = check_x_frame (frame);
11935 Atom prop_atom;
11936
11937 CHECK_STRING (prop, 1);
11938 CHECK_STRING (value, 2);
11939
11940 BLOCK_INPUT;
11941 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), XSTRING (prop)->data, False);
11942 XChangeProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
11943 prop_atom, XA_STRING, 8, PropModeReplace,
11944 XSTRING (value)->data, XSTRING (value)->size);
11945
11946 /* Make sure the property is set when we return. */
11947 XFlush (FRAME_W32_DISPLAY (f));
11948 UNBLOCK_INPUT;
11949
767b1ff0 11950#endif /* TODO */
6fc2811b
JR
11951
11952 return value;
11953}
11954
11955
11956DEFUN ("x-delete-window-property", Fx_delete_window_property,
11957 Sx_delete_window_property, 1, 2, 0,
11958 "Remove window property PROP from X window of FRAME.\n\
11959FRAME nil or omitted means use the selected frame. Value is PROP.")
11960 (prop, frame)
11961 Lisp_Object prop, frame;
11962{
767b1ff0 11963#if 0 /* TODO : port window properties to W32 */
6fc2811b
JR
11964
11965 struct frame *f = check_x_frame (frame);
11966 Atom prop_atom;
11967
11968 CHECK_STRING (prop, 1);
11969 BLOCK_INPUT;
11970 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), XSTRING (prop)->data, False);
11971 XDeleteProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), prop_atom);
11972
11973 /* Make sure the property is removed when we return. */
11974 XFlush (FRAME_W32_DISPLAY (f));
11975 UNBLOCK_INPUT;
767b1ff0 11976#endif /* TODO */
6fc2811b
JR
11977
11978 return prop;
11979}
11980
11981
11982DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
11983 1, 2, 0,
11984 "Value is the value of window property PROP on FRAME.\n\
11985If FRAME is nil or omitted, use the selected frame. Value is nil\n\
11986if FRAME hasn't a property with name PROP or if PROP has no string\n\
11987value.")
11988 (prop, frame)
11989 Lisp_Object prop, frame;
11990{
767b1ff0 11991#if 0 /* TODO : port window properties to W32 */
6fc2811b
JR
11992
11993 struct frame *f = check_x_frame (frame);
11994 Atom prop_atom;
11995 int rc;
11996 Lisp_Object prop_value = Qnil;
11997 char *tmp_data = NULL;
11998 Atom actual_type;
11999 int actual_format;
12000 unsigned long actual_size, bytes_remaining;
12001
12002 CHECK_STRING (prop, 1);
12003 BLOCK_INPUT;
12004 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), XSTRING (prop)->data, False);
12005 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
12006 prop_atom, 0, 0, False, XA_STRING,
12007 &actual_type, &actual_format, &actual_size,
12008 &bytes_remaining, (unsigned char **) &tmp_data);
12009 if (rc == Success)
12010 {
12011 int size = bytes_remaining;
12012
12013 XFree (tmp_data);
12014 tmp_data = NULL;
12015
12016 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
12017 prop_atom, 0, bytes_remaining,
12018 False, XA_STRING,
12019 &actual_type, &actual_format,
12020 &actual_size, &bytes_remaining,
12021 (unsigned char **) &tmp_data);
12022 if (rc == Success)
12023 prop_value = make_string (tmp_data, size);
12024
12025 XFree (tmp_data);
12026 }
12027
12028 UNBLOCK_INPUT;
12029
12030 return prop_value;
12031
767b1ff0 12032#endif /* TODO */
6fc2811b
JR
12033 return Qnil;
12034}
12035
12036
12037\f
12038/***********************************************************************
12039 Busy cursor
12040 ***********************************************************************/
12041
f79e6790
JR
12042/* If non-null, an asynchronous timer that, when it expires, displays
12043 a busy cursor on all frames. */
6fc2811b 12044
f79e6790 12045static struct atimer *busy_cursor_atimer;
6fc2811b 12046
f79e6790 12047/* Non-zero means a busy cursor is currently shown. */
6fc2811b 12048
f79e6790 12049static int busy_cursor_shown_p;
6fc2811b 12050
f79e6790 12051/* Number of seconds to wait before displaying a busy cursor. */
6fc2811b 12052
f79e6790 12053static Lisp_Object Vbusy_cursor_delay;
6fc2811b 12054
f79e6790
JR
12055/* Default number of seconds to wait before displaying a busy
12056 cursor. */
12057
12058#define DEFAULT_BUSY_CURSOR_DELAY 1
12059
12060/* Function prototypes. */
12061
12062static void show_busy_cursor P_ ((struct atimer *));
12063static void hide_busy_cursor P_ ((void));
12064
12065
12066/* Cancel a currently active busy-cursor timer, and start a new one. */
12067
12068void
12069start_busy_cursor ()
12070{
767b1ff0 12071#if 0 /* TODO: cursor shape changes. */
f79e6790 12072 EMACS_TIME delay;
dfff8a69 12073 int secs, usecs = 0;
f79e6790
JR
12074
12075 cancel_busy_cursor ();
12076
12077 if (INTEGERP (Vbusy_cursor_delay)
12078 && XINT (Vbusy_cursor_delay) > 0)
12079 secs = XFASTINT (Vbusy_cursor_delay);
dfff8a69
JR
12080 else if (FLOATP (Vbusy_cursor_delay)
12081 && XFLOAT_DATA (Vbusy_cursor_delay) > 0)
12082 {
12083 Lisp_Object tem;
12084 tem = Ftruncate (Vbusy_cursor_delay, Qnil);
12085 secs = XFASTINT (tem);
12086 usecs = (XFLOAT_DATA (Vbusy_cursor_delay) - secs) * 1000000;
12087 }
f79e6790
JR
12088 else
12089 secs = DEFAULT_BUSY_CURSOR_DELAY;
12090
dfff8a69 12091 EMACS_SET_SECS_USECS (delay, secs, usecs);
f79e6790
JR
12092 busy_cursor_atimer = start_atimer (ATIMER_RELATIVE, delay,
12093 show_busy_cursor, NULL);
12094#endif
12095}
12096
12097
12098/* Cancel the busy cursor timer if active, hide a busy cursor if
12099 shown. */
12100
12101void
12102cancel_busy_cursor ()
12103{
12104 if (busy_cursor_atimer)
dfff8a69
JR
12105 {
12106 cancel_atimer (busy_cursor_atimer);
12107 busy_cursor_atimer = NULL;
12108 }
12109
f79e6790
JR
12110 if (busy_cursor_shown_p)
12111 hide_busy_cursor ();
12112}
12113
12114
12115/* Timer function of busy_cursor_atimer. TIMER is equal to
12116 busy_cursor_atimer.
12117
12118 Display a busy cursor on all frames by mapping the frames'
12119 busy_window. Set the busy_p flag in the frames' output_data.x
12120 structure to indicate that a busy cursor is shown on the
12121 frames. */
12122
12123static void
12124show_busy_cursor (timer)
12125 struct atimer *timer;
6fc2811b 12126{
767b1ff0 12127#if 0 /* TODO: cursor shape changes. */
f79e6790
JR
12128 /* The timer implementation will cancel this timer automatically
12129 after this function has run. Set busy_cursor_atimer to null
12130 so that we know the timer doesn't have to be canceled. */
12131 busy_cursor_atimer = NULL;
12132
12133 if (!busy_cursor_shown_p)
6fc2811b
JR
12134 {
12135 Lisp_Object rest, frame;
f79e6790
JR
12136
12137 BLOCK_INPUT;
12138
6fc2811b
JR
12139 FOR_EACH_FRAME (rest, frame)
12140 if (FRAME_X_P (XFRAME (frame)))
12141 {
12142 struct frame *f = XFRAME (frame);
f79e6790 12143
6fc2811b 12144 f->output_data.w32->busy_p = 1;
f79e6790 12145
6fc2811b
JR
12146 if (!f->output_data.w32->busy_window)
12147 {
12148 unsigned long mask = CWCursor;
12149 XSetWindowAttributes attrs;
f79e6790 12150
6fc2811b 12151 attrs.cursor = f->output_data.w32->busy_cursor;
f79e6790 12152
6fc2811b 12153 f->output_data.w32->busy_window
f79e6790 12154 = XCreateWindow (FRAME_X_DISPLAY (f),
6fc2811b
JR
12155 FRAME_OUTER_WINDOW (f),
12156 0, 0, 32000, 32000, 0, 0,
f79e6790
JR
12157 InputOnly,
12158 CopyFromParent,
6fc2811b
JR
12159 mask, &attrs);
12160 }
f79e6790
JR
12161
12162 XMapRaised (FRAME_X_DISPLAY (f), f->output_data.w32->busy_window);
12163 XFlush (FRAME_X_DISPLAY (f));
6fc2811b 12164 }
6fc2811b 12165
f79e6790
JR
12166 busy_cursor_shown_p = 1;
12167 UNBLOCK_INPUT;
12168 }
12169#endif
6fc2811b
JR
12170}
12171
12172
f79e6790 12173/* Hide the busy cursor on all frames, if it is currently shown. */
6fc2811b 12174
f79e6790
JR
12175static void
12176hide_busy_cursor ()
12177{
767b1ff0 12178#if 0 /* TODO: cursor shape changes. */
f79e6790 12179 if (busy_cursor_shown_p)
6fc2811b 12180 {
f79e6790
JR
12181 Lisp_Object rest, frame;
12182
12183 BLOCK_INPUT;
12184 FOR_EACH_FRAME (rest, frame)
6fc2811b 12185 {
f79e6790
JR
12186 struct frame *f = XFRAME (frame);
12187
12188 if (FRAME_X_P (f)
12189 /* Watch out for newly created frames. */
12190 && f->output_data.x->busy_window)
12191 {
12192 XUnmapWindow (FRAME_X_DISPLAY (f), f->output_data.x->busy_window);
12193 /* Sync here because XTread_socket looks at the busy_p flag
12194 that is reset to zero below. */
12195 XSync (FRAME_X_DISPLAY (f), False);
12196 f->output_data.x->busy_p = 0;
12197 }
6fc2811b 12198 }
6fc2811b 12199
f79e6790
JR
12200 busy_cursor_shown_p = 0;
12201 UNBLOCK_INPUT;
12202 }
12203#endif
6fc2811b
JR
12204}
12205
12206
12207\f
12208/***********************************************************************
12209 Tool tips
12210 ***********************************************************************/
12211
12212static Lisp_Object x_create_tip_frame P_ ((struct w32_display_info *,
12213 Lisp_Object));
12214
12215/* The frame of a currently visible tooltip, or null. */
12216
937e601e 12217Lisp_Object tip_frame;
6fc2811b
JR
12218
12219/* If non-nil, a timer started that hides the last tooltip when it
12220 fires. */
12221
12222Lisp_Object tip_timer;
12223Window tip_window;
12224
937e601e
AI
12225static Lisp_Object
12226unwind_create_tip_frame (frame)
12227 Lisp_Object frame;
12228{
c844a81a
GM
12229 Lisp_Object deleted;
12230
12231 deleted = unwind_create_frame (frame);
12232 if (EQ (deleted, Qt))
12233 {
12234 tip_window = NULL;
12235 tip_frame = Qnil;
12236 }
12237
12238 return deleted;
937e601e
AI
12239}
12240
12241
6fc2811b 12242/* Create a frame for a tooltip on the display described by DPYINFO.
937e601e
AI
12243 PARMS is a list of frame parameters. Value is the frame.
12244
12245 Note that functions called here, esp. x_default_parameter can
12246 signal errors, for instance when a specified color name is
12247 undefined. We have to make sure that we're in a consistent state
12248 when this happens. */
6fc2811b
JR
12249
12250static Lisp_Object
12251x_create_tip_frame (dpyinfo, parms)
12252 struct w32_display_info *dpyinfo;
12253 Lisp_Object parms;
12254{
767b1ff0 12255#if 0 /* TODO : w32 version */
6fc2811b
JR
12256 struct frame *f;
12257 Lisp_Object frame, tem;
12258 Lisp_Object name;
12259 long window_prompting = 0;
12260 int width, height;
12261 int count = specpdl_ptr - specpdl;
12262 struct gcpro gcpro1, gcpro2, gcpro3;
12263 struct kboard *kb;
12264
12265 check_x ();
12266
12267 /* Use this general default value to start with until we know if
12268 this frame has a specified name. */
12269 Vx_resource_name = Vinvocation_name;
12270
12271#ifdef MULTI_KBOARD
12272 kb = dpyinfo->kboard;
12273#else
12274 kb = &the_only_kboard;
12275#endif
12276
12277 /* Get the name of the frame to use for resource lookup. */
12278 name = w32_get_arg (parms, Qname, "name", "Name", RES_TYPE_STRING);
12279 if (!STRINGP (name)
12280 && !EQ (name, Qunbound)
12281 && !NILP (name))
12282 error ("Invalid frame name--not a string or nil");
12283 Vx_resource_name = name;
12284
12285 frame = Qnil;
12286 GCPRO3 (parms, name, frame);
937e601e 12287 f = make_frame (1);
6fc2811b
JR
12288 XSETFRAME (frame, f);
12289 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
937e601e 12290 record_unwind_protect (unwind_create_tip_frame, frame);
6fc2811b 12291
d88c567c 12292 f->output_method = output_w32;
6fc2811b
JR
12293 f->output_data.w32 =
12294 (struct w32_output *) xmalloc (sizeof (struct w32_output));
12295 bzero (f->output_data.w32, sizeof (struct w32_output));
12296#if 0
12297 f->output_data.w32->icon_bitmap = -1;
12298#endif
12299 f->output_data.w32->fontset = -1;
12300 f->icon_name = Qnil;
12301
937e601e
AI
12302#ifdef GLYPH_DEBUG
12303 image_cache_refcount = FRAME_X_IMAGE_CACHE (f)->refcount;
12304 dpyinfo_refcount = dpyinfo->reference_count;
12305#endif /* GLYPH_DEBUG */
6fc2811b
JR
12306#ifdef MULTI_KBOARD
12307 FRAME_KBOARD (f) = kb;
12308#endif
12309 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
12310 f->output_data.w32->explicit_parent = 0;
12311
12312 /* Set the name; the functions to which we pass f expect the name to
12313 be set. */
12314 if (EQ (name, Qunbound) || NILP (name))
12315 {
12316 f->name = build_string (dpyinfo->x_id_name);
12317 f->explicit_name = 0;
12318 }
12319 else
12320 {
12321 f->name = name;
12322 f->explicit_name = 1;
12323 /* use the frame's title when getting resources for this frame. */
12324 specbind (Qx_resource_name, name);
12325 }
12326
6fc2811b
JR
12327 /* Extract the window parameters from the supplied values
12328 that are needed to determine window geometry. */
12329 {
12330 Lisp_Object font;
12331
12332 font = w32_get_arg (parms, Qfont, "font", "Font", RES_TYPE_STRING);
12333
12334 BLOCK_INPUT;
12335 /* First, try whatever font the caller has specified. */
12336 if (STRINGP (font))
12337 {
12338 tem = Fquery_fontset (font, Qnil);
12339 if (STRINGP (tem))
12340 font = x_new_fontset (f, XSTRING (tem)->data);
12341 else
12342 font = x_new_font (f, XSTRING (font)->data);
12343 }
12344
12345 /* Try out a font which we hope has bold and italic variations. */
12346 if (!STRINGP (font))
e39649be 12347 font = x_new_font (f, "-*-courier new-normal-r-*-*-*-100-*-*-*-*-iso8859-1");
6fc2811b
JR
12348 if (!STRINGP (font))
12349 font = x_new_font (f, "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
12350 if (! STRINGP (font))
12351 font = x_new_font (f, "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
12352 if (! STRINGP (font))
12353 /* This was formerly the first thing tried, but it finds too many fonts
12354 and takes too long. */
12355 font = x_new_font (f, "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1");
12356 /* If those didn't work, look for something which will at least work. */
12357 if (! STRINGP (font))
12358 font = x_new_font (f, "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1");
12359 UNBLOCK_INPUT;
12360 if (! STRINGP (font))
12361 font = build_string ("fixed");
12362
12363 x_default_parameter (f, parms, Qfont, font,
12364 "font", "Font", RES_TYPE_STRING);
12365 }
12366
12367 x_default_parameter (f, parms, Qborder_width, make_number (2),
12368 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
12369
12370 /* This defaults to 2 in order to match xterm. We recognize either
12371 internalBorderWidth or internalBorder (which is what xterm calls
12372 it). */
12373 if (NILP (Fassq (Qinternal_border_width, parms)))
12374 {
12375 Lisp_Object value;
12376
12377 value = w32_get_arg (parms, Qinternal_border_width,
12378 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
12379 if (! EQ (value, Qunbound))
12380 parms = Fcons (Fcons (Qinternal_border_width, value),
12381 parms);
12382 }
12383
12384 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
12385 "internalBorderWidth", "internalBorderWidth",
12386 RES_TYPE_NUMBER);
12387
12388 /* Also do the stuff which must be set before the window exists. */
12389 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
12390 "foreground", "Foreground", RES_TYPE_STRING);
12391 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
12392 "background", "Background", RES_TYPE_STRING);
12393 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
12394 "pointerColor", "Foreground", RES_TYPE_STRING);
12395 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
12396 "cursorColor", "Foreground", RES_TYPE_STRING);
12397 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
12398 "borderColor", "BorderColor", RES_TYPE_STRING);
12399
12400 /* Init faces before x_default_parameter is called for scroll-bar
12401 parameters because that function calls x_set_scroll_bar_width,
12402 which calls change_frame_size, which calls Fset_window_buffer,
12403 which runs hooks, which call Fvertical_motion. At the end, we
12404 end up in init_iterator with a null face cache, which should not
12405 happen. */
12406 init_frame_faces (f);
12407
12408 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
12409 window_prompting = x_figure_window_size (f, parms);
12410
12411 if (window_prompting & XNegative)
12412 {
12413 if (window_prompting & YNegative)
12414 f->output_data.w32->win_gravity = SouthEastGravity;
12415 else
12416 f->output_data.w32->win_gravity = NorthEastGravity;
12417 }
12418 else
12419 {
12420 if (window_prompting & YNegative)
12421 f->output_data.w32->win_gravity = SouthWestGravity;
12422 else
12423 f->output_data.w32->win_gravity = NorthWestGravity;
12424 }
12425
12426 f->output_data.w32->size_hint_flags = window_prompting;
12427 {
12428 XSetWindowAttributes attrs;
12429 unsigned long mask;
12430
12431 BLOCK_INPUT;
12432 mask = CWBackPixel | CWOverrideRedirect | CWSaveUnder | CWEventMask;
12433 /* Window managers looks at the override-redirect flag to
12434 determine whether or net to give windows a decoration (Xlib
12435 3.2.8). */
12436 attrs.override_redirect = True;
12437 attrs.save_under = True;
12438 attrs.background_pixel = FRAME_BACKGROUND_PIXEL (f);
12439 /* Arrange for getting MapNotify and UnmapNotify events. */
12440 attrs.event_mask = StructureNotifyMask;
12441 tip_window
12442 = FRAME_W32_WINDOW (f)
12443 = XCreateWindow (FRAME_W32_DISPLAY (f),
12444 FRAME_W32_DISPLAY_INFO (f)->root_window,
12445 /* x, y, width, height */
12446 0, 0, 1, 1,
12447 /* Border. */
12448 1,
12449 CopyFromParent, InputOutput, CopyFromParent,
12450 mask, &attrs);
12451 UNBLOCK_INPUT;
12452 }
12453
12454 x_make_gc (f);
12455
12456 x_default_parameter (f, parms, Qauto_raise, Qnil,
12457 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
12458 x_default_parameter (f, parms, Qauto_lower, Qnil,
12459 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
12460 x_default_parameter (f, parms, Qcursor_type, Qbox,
12461 "cursorType", "CursorType", RES_TYPE_SYMBOL);
12462
12463 /* Dimensions, especially f->height, must be done via change_frame_size.
12464 Change will not be effected unless different from the current
12465 f->height. */
12466 width = f->width;
12467 height = f->height;
12468 f->height = 0;
12469 SET_FRAME_WIDTH (f, 0);
12470 change_frame_size (f, height, width, 1, 0, 0);
12471
12472 f->no_split = 1;
12473
12474 UNGCPRO;
12475
12476 /* It is now ok to make the frame official even if we get an error
12477 below. And the frame needs to be on Vframe_list or making it
12478 visible won't work. */
12479 Vframe_list = Fcons (frame, Vframe_list);
937e601e 12480 tip_frame = frame;
6fc2811b
JR
12481
12482 /* Now that the frame is official, it counts as a reference to
12483 its display. */
12484 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
ee78dc32 12485
6fc2811b 12486 return unbind_to (count, frame);
767b1ff0 12487#endif /* TODO */
6fc2811b 12488 return Qnil;
ee78dc32
GV
12489}
12490
767b1ff0 12491#ifdef TODO /* Tooltip support not complete. */
71eab8d1 12492DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
6fc2811b 12493 "Show STRING in a \"tooltip\" window on frame FRAME.\n\
71eab8d1
AI
12494A tooltip window is a small X window displaying a string.\n\
12495\n\
6fc2811b 12496FRAME nil or omitted means use the selected frame.\n\
71eab8d1 12497\n\
6fc2811b
JR
12498PARMS is an optional list of frame parameters which can be\n\
12499used to change the tooltip's appearance.\n\
71eab8d1 12500\n\
6fc2811b 12501Automatically hide the tooltip after TIMEOUT seconds.\n\
71eab8d1
AI
12502TIMEOUT nil means use the default timeout of 5 seconds.\n\
12503\n\
12504If the list of frame parameters PARAMS contains a `left' parameters,\n\
12505the tooltip is displayed at that x-position. Otherwise it is\n\
12506displayed at the mouse position, with offset DX added (default is 5 if\n\
12507DX isn't specified). Likewise for the y-position; if a `top' frame\n\
12508parameter is specified, it determines the y-position of the tooltip\n\
12509window, otherwise it is displayed at the mouse position, with offset\n\
12510DY added (default is -5).")
12511 (string, frame, parms, timeout, dx, dy)
12512 Lisp_Object string, frame, parms, timeout, dx, dy;
ee78dc32 12513{
6fc2811b
JR
12514 struct frame *f;
12515 struct window *w;
12516 Window root, child;
71eab8d1 12517 Lisp_Object buffer, top, left;
6fc2811b
JR
12518 struct buffer *old_buffer;
12519 struct text_pos pos;
12520 int i, width, height;
12521 int root_x, root_y, win_x, win_y;
12522 unsigned pmask;
12523 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
12524 int old_windows_or_buffers_changed = windows_or_buffers_changed;
12525 int count = specpdl_ptr - specpdl;
12526
12527 specbind (Qinhibit_redisplay, Qt);
ee78dc32 12528
dfff8a69 12529 GCPRO4 (string, parms, frame, timeout);
ee78dc32 12530
6fc2811b
JR
12531 CHECK_STRING (string, 0);
12532 f = check_x_frame (frame);
12533 if (NILP (timeout))
12534 timeout = make_number (5);
12535 else
12536 CHECK_NATNUM (timeout, 2);
ee78dc32 12537
71eab8d1
AI
12538 if (NILP (dx))
12539 dx = make_number (5);
12540 else
12541 CHECK_NUMBER (dx, 5);
12542
12543 if (NILP (dy))
12544 dy = make_number (-5);
12545 else
12546 CHECK_NUMBER (dy, 6);
12547
6fc2811b
JR
12548 /* Hide a previous tip, if any. */
12549 Fx_hide_tip ();
ee78dc32 12550
6fc2811b
JR
12551 /* Add default values to frame parameters. */
12552 if (NILP (Fassq (Qname, parms)))
12553 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
12554 if (NILP (Fassq (Qinternal_border_width, parms)))
12555 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
12556 if (NILP (Fassq (Qborder_width, parms)))
12557 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
12558 if (NILP (Fassq (Qborder_color, parms)))
12559 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
12560 if (NILP (Fassq (Qbackground_color, parms)))
12561 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
12562 parms);
12563
12564 /* Create a frame for the tooltip, and record it in the global
12565 variable tip_frame. */
12566 frame = x_create_tip_frame (FRAME_W32_DISPLAY_INFO (f), parms);
937e601e 12567 f = XFRAME (frame);
6fc2811b
JR
12568
12569 /* Set up the frame's root window. Currently we use a size of 80
12570 columns x 40 lines. If someone wants to show a larger tip, he
12571 will loose. I don't think this is a realistic case. */
12572 w = XWINDOW (FRAME_ROOT_WINDOW (f));
12573 w->left = w->top = make_number (0);
12574 w->width = 80;
12575 w->height = 40;
12576 adjust_glyphs (f);
12577 w->pseudo_window_p = 1;
12578
12579 /* Display the tooltip text in a temporary buffer. */
12580 buffer = Fget_buffer_create (build_string (" *tip*"));
12581 Fset_window_buffer (FRAME_ROOT_WINDOW (f), buffer);
12582 old_buffer = current_buffer;
12583 set_buffer_internal_1 (XBUFFER (buffer));
12584 Ferase_buffer ();
12585 Finsert (make_number (1), &string);
12586 clear_glyph_matrix (w->desired_matrix);
12587 clear_glyph_matrix (w->current_matrix);
12588 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
12589 try_window (FRAME_ROOT_WINDOW (f), pos);
12590
12591 /* Compute width and height of the tooltip. */
12592 width = height = 0;
12593 for (i = 0; i < w->desired_matrix->nrows; ++i)
ee78dc32 12594 {
6fc2811b
JR
12595 struct glyph_row *row = &w->desired_matrix->rows[i];
12596 struct glyph *last;
12597 int row_width;
12598
12599 /* Stop at the first empty row at the end. */
12600 if (!row->enabled_p || !row->displays_text_p)
12601 break;
12602
12603 /* Let the row go over the full width of the frame. */
12604 row->full_width_p = 1;
12605
12606 /* There's a glyph at the end of rows that is use to place
12607 the cursor there. Don't include the width of this glyph. */
12608 if (row->used[TEXT_AREA])
12609 {
12610 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
12611 row_width = row->pixel_width - last->pixel_width;
12612 }
12613 else
12614 row_width = row->pixel_width;
12615
12616 height += row->height;
12617 width = max (width, row_width);
ee78dc32
GV
12618 }
12619
6fc2811b
JR
12620 /* Add the frame's internal border to the width and height the X
12621 window should have. */
12622 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
12623 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
ee78dc32 12624
71eab8d1
AI
12625 /* User-specified position? */
12626 left = Fcdr (Fassq (Qleft, parms));
12627 top = Fcdr (Fassq (Qtop, parms));
12628
6fc2811b
JR
12629 /* Move the tooltip window where the mouse pointer is. Resize and
12630 show it. */
767b1ff0 12631#if 0 /* TODO : W32 specifics */
6fc2811b 12632 BLOCK_INPUT;
71eab8d1 12633 XQueryPointer (FRAME_X_DISPLAY (f), FRAME_X_DISPLAY_INFO (f)->root_window,
6fc2811b 12634 &root, &child, &root_x, &root_y, &win_x, &win_y, &pmask);
71eab8d1
AI
12635 UNBLOCK_INPUT;
12636
12637 root_x += XINT (dx);
12638 root_y += XINT (dy);
12639
12640 if (INTEGERP (left))
12641 root_x = XINT (left);
12642 if (INTEGERP (top))
12643 root_y = XINT (top);
12644
12645 BLOCK_INPUT;
12646 XMoveResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12647 root_x, root_y - height, width, height);
12648 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
6fc2811b 12649 UNBLOCK_INPUT;
767b1ff0 12650#endif /* TODO */
ee78dc32 12651
6fc2811b
JR
12652 /* Draw into the window. */
12653 w->must_be_updated_p = 1;
12654 update_single_window (w, 1);
ee78dc32 12655
6fc2811b
JR
12656 /* Restore original current buffer. */
12657 set_buffer_internal_1 (old_buffer);
12658 windows_or_buffers_changed = old_windows_or_buffers_changed;
ee78dc32 12659
6fc2811b
JR
12660 /* Let the tip disappear after timeout seconds. */
12661 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
12662 intern ("x-hide-tip"));
ee78dc32 12663
dfff8a69 12664 UNGCPRO;
6fc2811b 12665 return unbind_to (count, Qnil);
ee78dc32
GV
12666}
12667
ee78dc32 12668
6fc2811b
JR
12669DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
12670 "Hide the current tooltip window, if there is any.\n\
12671Value is t is tooltip was open, nil otherwise.")
12672 ()
12673{
937e601e
AI
12674 int count;
12675 Lisp_Object deleted, frame, timer;
12676 struct gcpro gcpro1, gcpro2;
12677
12678 /* Return quickly if nothing to do. */
12679 if (NILP (tip_timer) && NILP (tip_frame))
12680 return Qnil;
12681
12682 frame = tip_frame;
12683 timer = tip_timer;
12684 GCPRO2 (frame, timer);
12685 tip_frame = tip_timer = deleted = Qnil;
6fc2811b 12686
937e601e 12687 count = BINDING_STACK_SIZE ();
6fc2811b 12688 specbind (Qinhibit_redisplay, Qt);
937e601e 12689 specbind (Qinhibit_quit, Qt);
6fc2811b 12690
937e601e
AI
12691 if (!NILP (timer))
12692 call1 (intern ("cancel-timer"), timer);
ee78dc32 12693
937e601e 12694 if (FRAMEP (frame))
6fc2811b 12695 {
937e601e
AI
12696 Fdelete_frame (frame, Qnil);
12697 deleted = Qt;
6fc2811b 12698 }
1edf84e7 12699
937e601e
AI
12700 UNGCPRO;
12701 return unbind_to (count, deleted);
6fc2811b 12702}
767b1ff0 12703#endif
5ac45f98 12704
5ac45f98 12705
6fc2811b
JR
12706\f
12707/***********************************************************************
12708 File selection dialog
12709 ***********************************************************************/
12710
12711extern Lisp_Object Qfile_name_history;
12712
12713DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 4, 0,
12714 "Read file name, prompting with PROMPT in directory DIR.\n\
12715Use a file selection dialog.\n\
12716Select DEFAULT-FILENAME in the dialog's file selection box, if\n\
12717specified. Don't let the user enter a file name in the file\n\
12718selection dialog's entry field, if MUSTMATCH is non-nil.")
12719 (prompt, dir, default_filename, mustmatch)
12720 Lisp_Object prompt, dir, default_filename, mustmatch;
12721{
12722 struct frame *f = SELECTED_FRAME ();
12723 Lisp_Object file = Qnil;
12724 int count = specpdl_ptr - specpdl;
12725 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
12726 char filename[MAX_PATH + 1];
12727 char init_dir[MAX_PATH + 1];
12728 int use_dialog_p = 1;
12729
12730 GCPRO5 (prompt, dir, default_filename, mustmatch, file);
12731 CHECK_STRING (prompt, 0);
12732 CHECK_STRING (dir, 1);
12733
12734 /* Create the dialog with PROMPT as title, using DIR as initial
12735 directory and using "*" as pattern. */
12736 dir = Fexpand_file_name (dir, Qnil);
12737 strncpy (init_dir, XSTRING (dir)->data, MAX_PATH);
12738 init_dir[MAX_PATH] = '\0';
12739 unixtodos_filename (init_dir);
12740
12741 if (STRINGP (default_filename))
12742 {
12743 char *file_name_only;
12744 char *full_path_name = XSTRING (default_filename)->data;
5ac45f98 12745
6fc2811b 12746 unixtodos_filename (full_path_name);
5ac45f98 12747
6fc2811b
JR
12748 file_name_only = strrchr (full_path_name, '\\');
12749 if (!file_name_only)
12750 file_name_only = full_path_name;
12751 else
12752 {
12753 file_name_only++;
5ac45f98 12754
6fc2811b
JR
12755 /* If default_file_name is a directory, don't use the open
12756 file dialog, as it does not support selecting
12757 directories. */
12758 if (!(*file_name_only))
12759 use_dialog_p = 0;
12760 }
ee78dc32 12761
6fc2811b
JR
12762 strncpy (filename, file_name_only, MAX_PATH);
12763 filename[MAX_PATH] = '\0';
12764 }
ee78dc32 12765 else
6fc2811b 12766 filename[0] = '\0';
ee78dc32 12767
6fc2811b
JR
12768 if (use_dialog_p)
12769 {
12770 OPENFILENAME file_details;
12771 char *filename_file;
5ac45f98 12772
6fc2811b
JR
12773 /* Prevent redisplay. */
12774 specbind (Qinhibit_redisplay, Qt);
12775 BLOCK_INPUT;
ee78dc32 12776
6fc2811b
JR
12777 bzero (&file_details, sizeof (file_details));
12778 file_details.lStructSize = sizeof (file_details);
12779 file_details.hwndOwner = FRAME_W32_WINDOW (f);
12780 file_details.lpstrFile = filename;
12781 file_details.nMaxFile = sizeof (filename);
12782 file_details.lpstrInitialDir = init_dir;
12783 file_details.lpstrTitle = XSTRING (prompt)->data;
12784 file_details.Flags = OFN_HIDEREADONLY | OFN_NOCHANGEDIR;
ee78dc32 12785
6fc2811b
JR
12786 if (!NILP (mustmatch))
12787 file_details.Flags |= OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
ee78dc32 12788
6fc2811b
JR
12789 if (GetOpenFileName (&file_details))
12790 {
12791 dostounix_filename (filename);
12792 file = build_string (filename);
12793 }
ee78dc32 12794 else
6fc2811b
JR
12795 file = Qnil;
12796
12797 UNBLOCK_INPUT;
12798 file = unbind_to (count, file);
ee78dc32 12799 }
6fc2811b
JR
12800 /* Open File dialog will not allow folders to be selected, so resort
12801 to minibuffer completing reads for directories. */
12802 else
12803 file = Fcompleting_read (prompt, intern ("read-file-name-internal"),
12804 dir, mustmatch, dir, Qfile_name_history,
12805 default_filename, Qnil);
ee78dc32 12806
6fc2811b 12807 UNGCPRO;
1edf84e7 12808
6fc2811b
JR
12809 /* Make "Cancel" equivalent to C-g. */
12810 if (NILP (file))
12811 Fsignal (Qquit, Qnil);
ee78dc32 12812
dfff8a69 12813 return unbind_to (count, file);
6fc2811b 12814}
ee78dc32 12815
ee78dc32 12816
6fc2811b
JR
12817\f
12818/***********************************************************************
12819 Tests
12820 ***********************************************************************/
ee78dc32 12821
6fc2811b 12822#if GLYPH_DEBUG
ee78dc32 12823
6fc2811b
JR
12824DEFUN ("imagep", Fimagep, Simagep, 1, 1, 0,
12825 "Value is non-nil if SPEC is a valid image specification.")
12826 (spec)
12827 Lisp_Object spec;
12828{
12829 return valid_image_p (spec) ? Qt : Qnil;
ee78dc32
GV
12830}
12831
ee78dc32 12832
6fc2811b
JR
12833DEFUN ("lookup-image", Flookup_image, Slookup_image, 1, 1, 0, "")
12834 (spec)
12835 Lisp_Object spec;
12836{
12837 int id = -1;
12838
12839 if (valid_image_p (spec))
12840 id = lookup_image (SELECTED_FRAME (), spec);
ee78dc32 12841
6fc2811b
JR
12842 debug_print (spec);
12843 return make_number (id);
ee78dc32
GV
12844}
12845
6fc2811b 12846#endif /* GLYPH_DEBUG != 0 */
ee78dc32 12847
ee78dc32
GV
12848
12849\f
6fc2811b
JR
12850/***********************************************************************
12851 w32 specialized functions
12852 ***********************************************************************/
ee78dc32 12853
fbd6baed
GV
12854DEFUN ("w32-select-font", Fw32_select_font, Sw32_select_font, 0, 1, 0,
12855 "This will display the W32 font dialog and return an X font string corresponding to the selection.")
ee78dc32
GV
12856 (frame)
12857 Lisp_Object frame;
12858{
12859 FRAME_PTR f = check_x_frame (frame);
12860 CHOOSEFONT cf;
12861 LOGFONT lf;
f46e6225
GV
12862 TEXTMETRIC tm;
12863 HDC hdc;
12864 HANDLE oldobj;
ee78dc32
GV
12865 char buf[100];
12866
12867 bzero (&cf, sizeof (cf));
f46e6225 12868 bzero (&lf, sizeof (lf));
ee78dc32
GV
12869
12870 cf.lStructSize = sizeof (cf);
fbd6baed 12871 cf.hwndOwner = FRAME_W32_WINDOW (f);
6fc2811b 12872 cf.Flags = CF_FORCEFONTEXIST | CF_SCREENFONTS;
ee78dc32
GV
12873 cf.lpLogFont = &lf;
12874
f46e6225
GV
12875 /* Initialize as much of the font details as we can from the current
12876 default font. */
12877 hdc = GetDC (FRAME_W32_WINDOW (f));
12878 oldobj = SelectObject (hdc, FRAME_FONT (f)->hfont);
12879 GetTextFace (hdc, LF_FACESIZE, lf.lfFaceName);
12880 if (GetTextMetrics (hdc, &tm))
12881 {
12882 lf.lfHeight = tm.tmInternalLeading - tm.tmHeight;
12883 lf.lfWeight = tm.tmWeight;
12884 lf.lfItalic = tm.tmItalic;
12885 lf.lfUnderline = tm.tmUnderlined;
12886 lf.lfStrikeOut = tm.tmStruckOut;
f46e6225
GV
12887 lf.lfCharSet = tm.tmCharSet;
12888 cf.Flags |= CF_INITTOLOGFONTSTRUCT;
12889 }
12890 SelectObject (hdc, oldobj);
6fc2811b 12891 ReleaseDC (FRAME_W32_WINDOW (f), hdc);
f46e6225 12892
767b1ff0 12893 if (!ChooseFont (&cf) || !w32_to_x_font (&lf, buf, 100, NULL))
3c190163 12894 return Qnil;
ee78dc32
GV
12895
12896 return build_string (buf);
12897}
12898
1edf84e7
GV
12899DEFUN ("w32-send-sys-command", Fw32_send_sys_command, Sw32_send_sys_command, 1, 2, 0,
12900 "Send frame a Windows WM_SYSCOMMAND message of type COMMAND.\n\
12901Some useful values for command are 0xf030 to maximise frame (0xf020\n\
12902to minimize), 0xf120 to restore frame to original size, and 0xf100\n\
12903to activate the menubar for keyboard access. 0xf140 activates the\n\
12904screen saver if defined.\n\
12905\n\
12906If optional parameter FRAME is not specified, use selected frame.")
12907 (command, frame)
12908 Lisp_Object command, frame;
12909{
12910 WPARAM code;
12911 FRAME_PTR f = check_x_frame (frame);
12912
12913 CHECK_NUMBER (command, 0);
12914
ce6059da 12915 PostMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, XINT (command), 0);
1edf84e7
GV
12916
12917 return Qnil;
12918}
12919
55dcfc15
AI
12920DEFUN ("w32-shell-execute", Fw32_shell_execute, Sw32_shell_execute, 2, 4, 0,
12921 "Get Windows to perform OPERATION on DOCUMENT.\n\
12922This is a wrapper around the ShellExecute system function, which\n\
12923invokes the application registered to handle OPERATION for DOCUMENT.\n\
6fc2811b
JR
12924OPERATION is typically \"open\", \"print\" or \"explore\" (but can be\n\
12925nil for the default action), and DOCUMENT is typically the name of a\n\
12926document file or URL, but can also be a program executable to run or\n\
12927a directory to open in the Windows Explorer.\n\
55dcfc15 12928\n\
6fc2811b
JR
12929If DOCUMENT is a program executable, PARAMETERS can be a string\n\
12930containing command line parameters, but otherwise should be nil.\n\
55dcfc15
AI
12931\n\
12932SHOW-FLAG can be used to control whether the invoked application is hidden\n\
6fc2811b 12933or minimized. If SHOW-FLAG is nil, the application is displayed normally,\n\
55dcfc15
AI
12934otherwise it is an integer representing a ShowWindow flag:\n\
12935\n\
12936 0 - start hidden\n\
12937 1 - start normally\n\
12938 3 - start maximized\n\
12939 6 - start minimized")
12940 (operation, document, parameters, show_flag)
12941 Lisp_Object operation, document, parameters, show_flag;
12942{
12943 Lisp_Object current_dir;
12944
55dcfc15
AI
12945 CHECK_STRING (document, 0);
12946
12947 /* Encode filename and current directory. */
12948 current_dir = ENCODE_FILE (current_buffer->directory);
12949 document = ENCODE_FILE (document);
12950 if ((int) ShellExecute (NULL,
6fc2811b
JR
12951 (STRINGP (operation) ?
12952 XSTRING (operation)->data : NULL),
55dcfc15
AI
12953 XSTRING (document)->data,
12954 (STRINGP (parameters) ?
12955 XSTRING (parameters)->data : NULL),
12956 XSTRING (current_dir)->data,
12957 (INTEGERP (show_flag) ?
12958 XINT (show_flag) : SW_SHOWDEFAULT))
12959 > 32)
12960 return Qt;
12961 error ("ShellExecute failed");
12962}
12963
ccc2d29c
GV
12964/* Lookup virtual keycode from string representing the name of a
12965 non-ascii keystroke into the corresponding virtual key, using
12966 lispy_function_keys. */
12967static int
12968lookup_vk_code (char *key)
12969{
12970 int i;
12971
12972 for (i = 0; i < 256; i++)
12973 if (lispy_function_keys[i] != 0
12974 && strcmp (lispy_function_keys[i], key) == 0)
12975 return i;
12976
12977 return -1;
12978}
12979
12980/* Convert a one-element vector style key sequence to a hot key
12981 definition. */
12982static int
12983w32_parse_hot_key (key)
12984 Lisp_Object key;
12985{
12986 /* Copied from Fdefine_key and store_in_keymap. */
12987 register Lisp_Object c;
12988 int vk_code;
12989 int lisp_modifiers;
12990 int w32_modifiers;
12991 struct gcpro gcpro1;
12992
12993 CHECK_VECTOR (key, 0);
12994
12995 if (XFASTINT (Flength (key)) != 1)
12996 return Qnil;
12997
12998 GCPRO1 (key);
12999
13000 c = Faref (key, make_number (0));
13001
13002 if (CONSP (c) && lucid_event_type_list_p (c))
13003 c = Fevent_convert_list (c);
13004
13005 UNGCPRO;
13006
13007 if (! INTEGERP (c) && ! SYMBOLP (c))
13008 error ("Key definition is invalid");
13009
13010 /* Work out the base key and the modifiers. */
13011 if (SYMBOLP (c))
13012 {
13013 c = parse_modifiers (c);
13014 lisp_modifiers = Fcar (Fcdr (c));
13015 c = Fcar (c);
13016 if (!SYMBOLP (c))
13017 abort ();
13018 vk_code = lookup_vk_code (XSYMBOL (c)->name->data);
13019 }
13020 else if (INTEGERP (c))
13021 {
13022 lisp_modifiers = XINT (c) & ~CHARACTERBITS;
13023 /* Many ascii characters are their own virtual key code. */
13024 vk_code = XINT (c) & CHARACTERBITS;
13025 }
13026
13027 if (vk_code < 0 || vk_code > 255)
13028 return Qnil;
13029
13030 if ((lisp_modifiers & meta_modifier) != 0
13031 && !NILP (Vw32_alt_is_meta))
13032 lisp_modifiers |= alt_modifier;
13033
71eab8d1
AI
13034 /* Supply defs missing from mingw32. */
13035#ifndef MOD_ALT
13036#define MOD_ALT 0x0001
13037#define MOD_CONTROL 0x0002
13038#define MOD_SHIFT 0x0004
13039#define MOD_WIN 0x0008
13040#endif
13041
ccc2d29c
GV
13042 /* Convert lisp modifiers to Windows hot-key form. */
13043 w32_modifiers = (lisp_modifiers & hyper_modifier) ? MOD_WIN : 0;
13044 w32_modifiers |= (lisp_modifiers & alt_modifier) ? MOD_ALT : 0;
13045 w32_modifiers |= (lisp_modifiers & ctrl_modifier) ? MOD_CONTROL : 0;
13046 w32_modifiers |= (lisp_modifiers & shift_modifier) ? MOD_SHIFT : 0;
13047
13048 return HOTKEY (vk_code, w32_modifiers);
13049}
13050
13051DEFUN ("w32-register-hot-key", Fw32_register_hot_key, Sw32_register_hot_key, 1, 1, 0,
13052 "Register KEY as a hot-key combination.\n\
13053Certain key combinations like Alt-Tab are reserved for system use on\n\
13054Windows, and therefore are normally intercepted by the system. However,\n\
13055most of these key combinations can be received by registering them as\n\
13056hot-keys, overriding their special meaning.\n\
13057\n\
13058KEY must be a one element key definition in vector form that would be\n\
13059acceptable to `define-key' (e.g. [A-tab] for Alt-Tab). The meta\n\
13060modifier is interpreted as Alt if `w32-alt-is-meta' is t, and hyper\n\
13061is always interpreted as the Windows modifier keys.\n\
13062\n\
13063The return value is the hotkey-id if registered, otherwise nil.")
13064 (key)
13065 Lisp_Object key;
13066{
13067 key = w32_parse_hot_key (key);
13068
13069 if (NILP (Fmemq (key, w32_grabbed_keys)))
13070 {
13071 /* Reuse an empty slot if possible. */
13072 Lisp_Object item = Fmemq (Qnil, w32_grabbed_keys);
13073
13074 /* Safe to add new key to list, even if we have focus. */
13075 if (NILP (item))
13076 w32_grabbed_keys = Fcons (key, w32_grabbed_keys);
13077 else
13078 XCAR (item) = key;
13079
13080 /* Notify input thread about new hot-key definition, so that it
13081 takes effect without needing to switch focus. */
13082 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
13083 (WPARAM) key, 0);
13084 }
13085
13086 return key;
13087}
13088
13089DEFUN ("w32-unregister-hot-key", Fw32_unregister_hot_key, Sw32_unregister_hot_key, 1, 1, 0,
13090 "Unregister HOTKEY as a hot-key combination.")
13091 (key)
13092 Lisp_Object key;
13093{
13094 Lisp_Object item;
13095
13096 if (!INTEGERP (key))
13097 key = w32_parse_hot_key (key);
13098
13099 item = Fmemq (key, w32_grabbed_keys);
13100
13101 if (!NILP (item))
13102 {
13103 /* Notify input thread about hot-key definition being removed, so
13104 that it takes effect without needing focus switch. */
13105 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
13106 (WPARAM) XINT (XCAR (item)), (LPARAM) item))
13107 {
13108 MSG msg;
13109 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
13110 }
13111 return Qt;
13112 }
13113 return Qnil;
13114}
13115
13116DEFUN ("w32-registered-hot-keys", Fw32_registered_hot_keys, Sw32_registered_hot_keys, 0, 0, 0,
13117 "Return list of registered hot-key IDs.")
13118 ()
13119{
13120 return Fcopy_sequence (w32_grabbed_keys);
13121}
13122
13123DEFUN ("w32-reconstruct-hot-key", Fw32_reconstruct_hot_key, Sw32_reconstruct_hot_key, 1, 1, 0,
13124 "Convert hot-key ID to a lisp key combination.")
13125 (hotkeyid)
13126 Lisp_Object hotkeyid;
13127{
13128 int vk_code, w32_modifiers;
13129 Lisp_Object key;
13130
13131 CHECK_NUMBER (hotkeyid, 0);
13132
13133 vk_code = HOTKEY_VK_CODE (hotkeyid);
13134 w32_modifiers = HOTKEY_MODIFIERS (hotkeyid);
13135
13136 if (lispy_function_keys[vk_code])
13137 key = intern (lispy_function_keys[vk_code]);
13138 else
13139 key = make_number (vk_code);
13140
13141 key = Fcons (key, Qnil);
13142 if (w32_modifiers & MOD_SHIFT)
3ef68e6b 13143 key = Fcons (Qshift, key);
ccc2d29c 13144 if (w32_modifiers & MOD_CONTROL)
3ef68e6b 13145 key = Fcons (Qctrl, key);
ccc2d29c 13146 if (w32_modifiers & MOD_ALT)
3ef68e6b 13147 key = Fcons (NILP (Vw32_alt_is_meta) ? Qalt : Qmeta, key);
ccc2d29c 13148 if (w32_modifiers & MOD_WIN)
3ef68e6b 13149 key = Fcons (Qhyper, key);
ccc2d29c
GV
13150
13151 return key;
13152}
adcc3809
GV
13153
13154DEFUN ("w32-toggle-lock-key", Fw32_toggle_lock_key, Sw32_toggle_lock_key, 1, 2, 0,
13155 "Toggle the state of the lock key KEY.\n\
13156KEY can be `capslock', `kp-numlock', or `scroll'.\n\
13157If the optional parameter NEW-STATE is a number, then the state of KEY\n\
13158is set to off if the low bit of NEW-STATE is zero, otherwise on.")
13159 (key, new_state)
13160 Lisp_Object key, new_state;
13161{
13162 int vk_code;
13163 int cur_state;
13164
13165 if (EQ (key, intern ("capslock")))
13166 vk_code = VK_CAPITAL;
13167 else if (EQ (key, intern ("kp-numlock")))
13168 vk_code = VK_NUMLOCK;
13169 else if (EQ (key, intern ("scroll")))
13170 vk_code = VK_SCROLL;
13171 else
13172 return Qnil;
13173
13174 if (!dwWindowsThreadId)
13175 return make_number (w32_console_toggle_lock_key (vk_code, new_state));
13176
13177 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
13178 (WPARAM) vk_code, (LPARAM) new_state))
13179 {
13180 MSG msg;
13181 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
13182 return make_number (msg.wParam);
13183 }
13184 return Qnil;
13185}
ee78dc32 13186\f
2254bcde
AI
13187DEFUN ("file-system-info", Ffile_system_info, Sfile_system_info, 1, 1, 0,
13188 "Return storage information about the file system FILENAME is on.\n\
13189Value is a list of floats (TOTAL FREE AVAIL), where TOTAL is the total\n\
13190storage of the file system, FREE is the free storage, and AVAIL is the\n\
13191storage available to a non-superuser. All 3 numbers are in bytes.\n\
13192If the underlying system call fails, value is nil.")
13193 (filename)
13194 Lisp_Object filename;
13195{
13196 Lisp_Object encoded, value;
13197
13198 CHECK_STRING (filename, 0);
13199 filename = Fexpand_file_name (filename, Qnil);
13200 encoded = ENCODE_FILE (filename);
13201
13202 value = Qnil;
13203
13204 /* Determining the required information on Windows turns out, sadly,
13205 to be more involved than one would hope. The original Win32 api
13206 call for this will return bogus information on some systems, but we
13207 must dynamically probe for the replacement api, since that was
13208 added rather late on. */
13209 {
13210 HMODULE hKernel = GetModuleHandle ("kernel32");
13211 BOOL (*pfn_GetDiskFreeSpaceEx)
13212 (char *, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER)
13213 = (void *) GetProcAddress (hKernel, "GetDiskFreeSpaceEx");
13214
13215 /* On Windows, we may need to specify the root directory of the
13216 volume holding FILENAME. */
13217 char rootname[MAX_PATH];
13218 char *name = XSTRING (encoded)->data;
13219
13220 /* find the root name of the volume if given */
13221 if (isalpha (name[0]) && name[1] == ':')
13222 {
13223 rootname[0] = name[0];
13224 rootname[1] = name[1];
13225 rootname[2] = '\\';
13226 rootname[3] = 0;
13227 }
13228 else if (IS_DIRECTORY_SEP (name[0]) && IS_DIRECTORY_SEP (name[1]))
13229 {
13230 char *str = rootname;
13231 int slashes = 4;
13232 do
13233 {
13234 if (IS_DIRECTORY_SEP (*name) && --slashes == 0)
13235 break;
13236 *str++ = *name++;
13237 }
13238 while ( *name );
13239
13240 *str++ = '\\';
13241 *str = 0;
13242 }
13243
13244 if (pfn_GetDiskFreeSpaceEx)
13245 {
13246 LARGE_INTEGER availbytes;
13247 LARGE_INTEGER freebytes;
13248 LARGE_INTEGER totalbytes;
13249
13250 if (pfn_GetDiskFreeSpaceEx(rootname,
13251 &availbytes,
13252 &totalbytes,
13253 &freebytes))
13254 value = list3 (make_float ((double) totalbytes.QuadPart),
13255 make_float ((double) freebytes.QuadPart),
13256 make_float ((double) availbytes.QuadPart));
13257 }
13258 else
13259 {
13260 DWORD sectors_per_cluster;
13261 DWORD bytes_per_sector;
13262 DWORD free_clusters;
13263 DWORD total_clusters;
13264
13265 if (GetDiskFreeSpace(rootname,
13266 &sectors_per_cluster,
13267 &bytes_per_sector,
13268 &free_clusters,
13269 &total_clusters))
13270 value = list3 (make_float ((double) total_clusters
13271 * sectors_per_cluster * bytes_per_sector),
13272 make_float ((double) free_clusters
13273 * sectors_per_cluster * bytes_per_sector),
13274 make_float ((double) free_clusters
13275 * sectors_per_cluster * bytes_per_sector));
13276 }
13277 }
13278
13279 return value;
13280}
13281\f
fbd6baed 13282syms_of_w32fns ()
ee78dc32 13283{
1edf84e7
GV
13284 /* This is zero if not using MS-Windows. */
13285 w32_in_use = 0;
13286
ee78dc32
GV
13287 /* The section below is built by the lisp expression at the top of the file,
13288 just above where these variables are declared. */
13289 /*&&& init symbols here &&&*/
13290 Qauto_raise = intern ("auto-raise");
13291 staticpro (&Qauto_raise);
13292 Qauto_lower = intern ("auto-lower");
13293 staticpro (&Qauto_lower);
ee78dc32
GV
13294 Qbar = intern ("bar");
13295 staticpro (&Qbar);
13296 Qborder_color = intern ("border-color");
13297 staticpro (&Qborder_color);
13298 Qborder_width = intern ("border-width");
13299 staticpro (&Qborder_width);
13300 Qbox = intern ("box");
13301 staticpro (&Qbox);
13302 Qcursor_color = intern ("cursor-color");
13303 staticpro (&Qcursor_color);
13304 Qcursor_type = intern ("cursor-type");
13305 staticpro (&Qcursor_type);
ee78dc32
GV
13306 Qgeometry = intern ("geometry");
13307 staticpro (&Qgeometry);
13308 Qicon_left = intern ("icon-left");
13309 staticpro (&Qicon_left);
13310 Qicon_top = intern ("icon-top");
13311 staticpro (&Qicon_top);
13312 Qicon_type = intern ("icon-type");
13313 staticpro (&Qicon_type);
13314 Qicon_name = intern ("icon-name");
13315 staticpro (&Qicon_name);
13316 Qinternal_border_width = intern ("internal-border-width");
13317 staticpro (&Qinternal_border_width);
13318 Qleft = intern ("left");
13319 staticpro (&Qleft);
1026b400
RS
13320 Qright = intern ("right");
13321 staticpro (&Qright);
ee78dc32
GV
13322 Qmouse_color = intern ("mouse-color");
13323 staticpro (&Qmouse_color);
13324 Qnone = intern ("none");
13325 staticpro (&Qnone);
13326 Qparent_id = intern ("parent-id");
13327 staticpro (&Qparent_id);
13328 Qscroll_bar_width = intern ("scroll-bar-width");
13329 staticpro (&Qscroll_bar_width);
13330 Qsuppress_icon = intern ("suppress-icon");
13331 staticpro (&Qsuppress_icon);
ee78dc32
GV
13332 Qundefined_color = intern ("undefined-color");
13333 staticpro (&Qundefined_color);
13334 Qvertical_scroll_bars = intern ("vertical-scroll-bars");
13335 staticpro (&Qvertical_scroll_bars);
13336 Qvisibility = intern ("visibility");
13337 staticpro (&Qvisibility);
13338 Qwindow_id = intern ("window-id");
13339 staticpro (&Qwindow_id);
13340 Qx_frame_parameter = intern ("x-frame-parameter");
13341 staticpro (&Qx_frame_parameter);
13342 Qx_resource_name = intern ("x-resource-name");
13343 staticpro (&Qx_resource_name);
13344 Quser_position = intern ("user-position");
13345 staticpro (&Quser_position);
13346 Quser_size = intern ("user-size");
13347 staticpro (&Quser_size);
6fc2811b
JR
13348 Qscreen_gamma = intern ("screen-gamma");
13349 staticpro (&Qscreen_gamma);
dfff8a69
JR
13350 Qline_spacing = intern ("line-spacing");
13351 staticpro (&Qline_spacing);
13352 Qcenter = intern ("center");
13353 staticpro (&Qcenter);
ee78dc32
GV
13354 /* This is the end of symbol initialization. */
13355
adcc3809
GV
13356 Qhyper = intern ("hyper");
13357 staticpro (&Qhyper);
13358 Qsuper = intern ("super");
13359 staticpro (&Qsuper);
13360 Qmeta = intern ("meta");
13361 staticpro (&Qmeta);
13362 Qalt = intern ("alt");
13363 staticpro (&Qalt);
13364 Qctrl = intern ("ctrl");
13365 staticpro (&Qctrl);
13366 Qcontrol = intern ("control");
13367 staticpro (&Qcontrol);
13368 Qshift = intern ("shift");
13369 staticpro (&Qshift);
13370
6fc2811b
JR
13371 /* Text property `display' should be nonsticky by default. */
13372 Vtext_property_default_nonsticky
13373 = Fcons (Fcons (Qdisplay, Qt), Vtext_property_default_nonsticky);
13374
13375
13376 Qlaplace = intern ("laplace");
13377 staticpro (&Qlaplace);
13378
4b817373
RS
13379 Qface_set_after_frame_default = intern ("face-set-after-frame-default");
13380 staticpro (&Qface_set_after_frame_default);
13381
ee78dc32
GV
13382 Fput (Qundefined_color, Qerror_conditions,
13383 Fcons (Qundefined_color, Fcons (Qerror, Qnil)));
13384 Fput (Qundefined_color, Qerror_message,
13385 build_string ("Undefined color"));
13386
ccc2d29c
GV
13387 staticpro (&w32_grabbed_keys);
13388 w32_grabbed_keys = Qnil;
13389
fbd6baed 13390 DEFVAR_LISP ("w32-color-map", &Vw32_color_map,
ccc2d29c 13391 "An array of color name mappings for windows.");
fbd6baed 13392 Vw32_color_map = Qnil;
ee78dc32 13393
fbd6baed 13394 DEFVAR_LISP ("w32-pass-alt-to-system", &Vw32_pass_alt_to_system,
da36a4d6
GV
13395 "Non-nil if alt key presses are passed on to Windows.\n\
13396When non-nil, for example, alt pressed and released and then space will\n\
13397open the System menu. When nil, Emacs silently swallows alt key events.");
fbd6baed 13398 Vw32_pass_alt_to_system = Qnil;
da36a4d6 13399
fbd6baed 13400 DEFVAR_LISP ("w32-alt-is-meta", &Vw32_alt_is_meta,
8c205c63
RS
13401 "Non-nil if the alt key is to be considered the same as the meta key.\n\
13402When nil, Emacs will translate the alt key to the Alt modifier, and not Meta.");
fbd6baed 13403 Vw32_alt_is_meta = Qt;
8c205c63 13404
7d081355
AI
13405 DEFVAR_INT ("w32-quit-key", &Vw32_quit_key,
13406 "If non-zero, the virtual key code for an alternative quit key.");
13407 XSETINT (Vw32_quit_key, 0);
13408
ccc2d29c
GV
13409 DEFVAR_LISP ("w32-pass-lwindow-to-system",
13410 &Vw32_pass_lwindow_to_system,
13411 "Non-nil if the left \"Windows\" key is passed on to Windows.\n\
13412When non-nil, the Start menu is opened by tapping the key.");
13413 Vw32_pass_lwindow_to_system = Qt;
13414
13415 DEFVAR_LISP ("w32-pass-rwindow-to-system",
13416 &Vw32_pass_rwindow_to_system,
13417 "Non-nil if the right \"Windows\" key is passed on to Windows.\n\
13418When non-nil, the Start menu is opened by tapping the key.");
13419 Vw32_pass_rwindow_to_system = Qt;
13420
adcc3809
GV
13421 DEFVAR_INT ("w32-phantom-key-code",
13422 &Vw32_phantom_key_code,
13423 "Virtual key code used to generate \"phantom\" key presses.\n\
13424Value is a number between 0 and 255.\n\
13425\n\
13426Phantom key presses are generated in order to stop the system from\n\
13427acting on \"Windows\" key events when `w32-pass-lwindow-to-system' or\n\
13428`w32-pass-rwindow-to-system' is nil.");
ce6059da
AI
13429 /* Although 255 is technically not a valid key code, it works and
13430 means that this hack won't interfere with any real key code. */
13431 Vw32_phantom_key_code = 255;
adcc3809 13432
ccc2d29c
GV
13433 DEFVAR_LISP ("w32-enable-num-lock",
13434 &Vw32_enable_num_lock,
13435 "Non-nil if Num Lock should act normally.\n\
13436Set to nil to see Num Lock as the key `kp-numlock'.");
13437 Vw32_enable_num_lock = Qt;
13438
13439 DEFVAR_LISP ("w32-enable-caps-lock",
13440 &Vw32_enable_caps_lock,
13441 "Non-nil if Caps Lock should act normally.\n\
13442Set to nil to see Caps Lock as the key `capslock'.");
13443 Vw32_enable_caps_lock = Qt;
13444
13445 DEFVAR_LISP ("w32-scroll-lock-modifier",
13446 &Vw32_scroll_lock_modifier,
13447 "Modifier to use for the Scroll Lock on state.\n\
13448The value can be hyper, super, meta, alt, control or shift for the\n\
13449respective modifier, or nil to see Scroll Lock as the key `scroll'.\n\
13450Any other value will cause the key to be ignored.");
13451 Vw32_scroll_lock_modifier = Qt;
13452
13453 DEFVAR_LISP ("w32-lwindow-modifier",
13454 &Vw32_lwindow_modifier,
13455 "Modifier to use for the left \"Windows\" key.\n\
13456The value can be hyper, super, meta, alt, control or shift for the\n\
13457respective modifier, or nil to appear as the key `lwindow'.\n\
13458Any other value will cause the key to be ignored.");
13459 Vw32_lwindow_modifier = Qnil;
13460
13461 DEFVAR_LISP ("w32-rwindow-modifier",
13462 &Vw32_rwindow_modifier,
13463 "Modifier to use for the right \"Windows\" key.\n\
13464The value can be hyper, super, meta, alt, control or shift for the\n\
13465respective modifier, or nil to appear as the key `rwindow'.\n\
13466Any other value will cause the key to be ignored.");
13467 Vw32_rwindow_modifier = Qnil;
13468
13469 DEFVAR_LISP ("w32-apps-modifier",
13470 &Vw32_apps_modifier,
13471 "Modifier to use for the \"Apps\" key.\n\
13472The value can be hyper, super, meta, alt, control or shift for the\n\
13473respective modifier, or nil to appear as the key `apps'.\n\
13474Any other value will cause the key to be ignored.");
13475 Vw32_apps_modifier = Qnil;
da36a4d6 13476
212da13b 13477 DEFVAR_LISP ("w32-enable-synthesized-fonts", &Vw32_enable_synthesized_fonts,
6fc2811b
JR
13478 "Non-nil enables selection of artificially italicized and bold fonts.");
13479 Vw32_enable_synthesized_fonts = Qnil;
5ac45f98 13480
fbd6baed 13481 DEFVAR_LISP ("w32-enable-palette", &Vw32_enable_palette,
6fc2811b 13482 "Non-nil enables Windows palette management to map colors exactly.");
fbd6baed 13483 Vw32_enable_palette = Qt;
5ac45f98 13484
fbd6baed
GV
13485 DEFVAR_INT ("w32-mouse-button-tolerance",
13486 &Vw32_mouse_button_tolerance,
6fc2811b 13487 "Analogue of double click interval for faking middle mouse events.\n\
5ac45f98
GV
13488The value is the minimum time in milliseconds that must elapse between\n\
13489left/right button down events before they are considered distinct events.\n\
13490If both mouse buttons are depressed within this interval, a middle mouse\n\
13491button down event is generated instead.");
fbd6baed 13492 XSETINT (Vw32_mouse_button_tolerance, GetDoubleClickTime () / 2);
5ac45f98 13493
fbd6baed
GV
13494 DEFVAR_INT ("w32-mouse-move-interval",
13495 &Vw32_mouse_move_interval,
84fb1139
KH
13496 "Minimum interval between mouse move events.\n\
13497The value is the minimum time in milliseconds that must elapse between\n\
13498successive mouse move (or scroll bar drag) events before they are\n\
13499reported as lisp events.");
247be837 13500 XSETINT (Vw32_mouse_move_interval, 0);
84fb1139 13501
ee78dc32
GV
13502 init_x_parm_symbols ();
13503
13504 DEFVAR_LISP ("x-bitmap-file-path", &Vx_bitmap_file_path,
fbd6baed 13505 "List of directories to search for bitmap files for w32.");
ee78dc32
GV
13506 Vx_bitmap_file_path = decode_env_path ((char *) 0, "PATH");
13507
13508 DEFVAR_LISP ("x-pointer-shape", &Vx_pointer_shape,
13509 "The shape of the pointer when over text.\n\
13510Changing the value does not affect existing frames\n\
13511unless you set the mouse color.");
13512 Vx_pointer_shape = Qnil;
13513
13514 DEFVAR_LISP ("x-resource-name", &Vx_resource_name,
13515 "The name Emacs uses to look up resources; for internal use only.\n\
13516`x-get-resource' uses this as the first component of the instance name\n\
13517when requesting resource values.\n\
13518Emacs initially sets `x-resource-name' to the name under which Emacs\n\
13519was invoked, or to the value specified with the `-name' or `-rn'\n\
13520switches, if present.");
13521 Vx_resource_name = Qnil;
13522
13523 Vx_nontext_pointer_shape = Qnil;
13524
13525 Vx_mode_pointer_shape = Qnil;
13526
6fc2811b
JR
13527 DEFVAR_LISP ("x-busy-pointer-shape", &Vx_busy_pointer_shape,
13528 "The shape of the pointer when Emacs is busy.\n\
13529This variable takes effect when you create a new frame\n\
13530or when you set the mouse color.");
13531 Vx_busy_pointer_shape = Qnil;
13532
13533 DEFVAR_BOOL ("display-busy-cursor", &display_busy_cursor_p,
13534 "Non-zero means Emacs displays a busy cursor on window systems.");
13535 display_busy_cursor_p = 1;
13536
f79e6790
JR
13537 DEFVAR_LISP ("busy-cursor-delay", &Vbusy_cursor_delay,
13538 "*Seconds to wait before displaying a busy-cursor.\n\
dfff8a69 13539Value must be an integer or float.");
f79e6790
JR
13540 Vbusy_cursor_delay = make_number (DEFAULT_BUSY_CURSOR_DELAY);
13541
6fc2811b 13542 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
ee78dc32
GV
13543 &Vx_sensitive_text_pointer_shape,
13544 "The shape of the pointer when over mouse-sensitive text.\n\
13545This variable takes effect when you create a new frame\n\
13546or when you set the mouse color.");
13547 Vx_sensitive_text_pointer_shape = Qnil;
13548
4694d762
JR
13549 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
13550 &Vx_window_horizontal_drag_shape,
13551 "Pointer shape to use for indicating a window can be dragged horizontally.\n\
13552This variable takes effect when you create a new frame\n\
13553or when you set the mouse color.");
13554 Vx_window_horizontal_drag_shape = Qnil;
13555
ee78dc32
GV
13556 DEFVAR_LISP ("x-cursor-fore-pixel", &Vx_cursor_fore_pixel,
13557 "A string indicating the foreground color of the cursor box.");
13558 Vx_cursor_fore_pixel = Qnil;
13559
13560 DEFVAR_LISP ("x-no-window-manager", &Vx_no_window_manager,
13561 "Non-nil if no window manager is in use.\n\
13562Emacs doesn't try to figure this out; this is always nil\n\
13563unless you set it to something else.");
13564 /* We don't have any way to find this out, so set it to nil
13565 and maybe the user would like to set it to t. */
13566 Vx_no_window_manager = Qnil;
13567
4587b026
GV
13568 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
13569 &Vx_pixel_size_width_font_regexp,
13570 "Regexp matching a font name whose width is the same as `PIXEL_SIZE'.\n\
13571\n\
13572Since Emacs gets width of a font matching with this regexp from\n\
13573PIXEL_SIZE field of the name, font finding mechanism gets faster for\n\
13574such a font. This is especially effective for such large fonts as\n\
13575Chinese, Japanese, and Korean.");
13576 Vx_pixel_size_width_font_regexp = Qnil;
13577
6fc2811b
JR
13578 DEFVAR_LISP ("image-cache-eviction-delay", &Vimage_cache_eviction_delay,
13579 "Time after which cached images are removed from the cache.\n\
13580When an image has not been displayed this many seconds, remove it\n\
13581from the image cache. Value must be an integer or nil with nil\n\
13582meaning don't clear the cache.");
13583 Vimage_cache_eviction_delay = make_number (30 * 60);
13584
33d52f9c
GV
13585 DEFVAR_LISP ("w32-bdf-filename-alist",
13586 &Vw32_bdf_filename_alist,
13587 "List of bdf fonts and their corresponding filenames.");
13588 Vw32_bdf_filename_alist = Qnil;
13589
1075afa9
GV
13590 DEFVAR_BOOL ("w32-strict-fontnames",
13591 &w32_strict_fontnames,
13592 "Non-nil means only use fonts that are exact matches for those requested.\n\
13593Default is nil, which allows old fontnames that are not XLFD compliant,\n\
13594and allows third-party CJK display to work by specifying false charset\n\
13595fields to trick Emacs into translating to Big5, SJIS etc.\n\
13596Setting this to t will prevent wrong fonts being selected when\n\
13597fontsets are automatically created.");
13598 w32_strict_fontnames = 0;
13599
c0611964
AI
13600 DEFVAR_BOOL ("w32-strict-painting",
13601 &w32_strict_painting,
13602 "Non-nil means use strict rules for repainting frames.\n\
13603Set this to nil to get the old behaviour for repainting; this should\n\
13604only be necessary if the default setting causes problems.");
13605 w32_strict_painting = 1;
13606
f46e6225
GV
13607 DEFVAR_LISP ("w32-system-coding-system",
13608 &Vw32_system_coding_system,
13609 "Coding system used by Windows system functions, such as for font names.");
13610 Vw32_system_coding_system = Qnil;
13611
dfff8a69
JR
13612 DEFVAR_LISP ("w32-charset-info-alist",
13613 &Vw32_charset_info_alist,
13614 "Alist linking Emacs character sets to Windows fonts\n\
13615and codepages. Each entry should be of the form:\n\
13616\n\
13617 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE))\n\
13618\n\
13619where CHARSET_NAME is a string used in font names to identify the charset,\n\
13620WINDOWS_CHARSET is a symbol that can be one of:\n\
13621w32-charset-ansi, w32-charset-default, w32-charset-symbol,\n\
767b1ff0 13622w32-charset-shiftjis, w32-charset-hangeul, w32-charset-gb2312,\n\
dfff8a69
JR
13623w32-charset-chinesebig5, "
13624#ifdef JOHAB_CHARSET
13625"w32-charset-johab, w32-charset-hebrew,\n\
13626w32-charset-arabic, w32-charset-greek, w32-charset-turkish,\n\
13627w32-charset-vietnamese, w32-charset-thai, w32-charset-easteurope,\n\
13628w32-charset-russian, w32-charset-mac, w32-charset-baltic,\n"
13629#endif
13630#ifdef UNICODE_CHARSET
13631"w32-charset-unicode, "
13632#endif
13633"or w32-charset-oem.\n\
13634CODEPAGE should be an integer specifying the codepage that should be used\n\
13635to display the character set, t to do no translation and output as Unicode,\n\
13636or nil to do no translation and output as 8 bit (or multibyte on far-east\n\
13637versions of Windows) characters.");
13638 Vw32_charset_info_alist = Qnil;
13639
13640 staticpro (&Qw32_charset_ansi);
13641 Qw32_charset_ansi = intern ("w32-charset-ansi");
13642 staticpro (&Qw32_charset_symbol);
13643 Qw32_charset_symbol = intern ("w32-charset-symbol");
13644 staticpro (&Qw32_charset_shiftjis);
13645 Qw32_charset_shiftjis = intern ("w32-charset-shiftjis");
767b1ff0
JR
13646 staticpro (&Qw32_charset_hangeul);
13647 Qw32_charset_hangeul = intern ("w32-charset-hangeul");
dfff8a69
JR
13648 staticpro (&Qw32_charset_chinesebig5);
13649 Qw32_charset_chinesebig5 = intern ("w32-charset-chinesebig5");
13650 staticpro (&Qw32_charset_gb2312);
13651 Qw32_charset_gb2312 = intern ("w32-charset-gb2312");
13652 staticpro (&Qw32_charset_oem);
13653 Qw32_charset_oem = intern ("w32-charset-oem");
13654
13655#ifdef JOHAB_CHARSET
13656 {
13657 static int w32_extra_charsets_defined = 1;
767b1ff0 13658 DEFVAR_BOOL ("w32-extra-charsets-defined", &w32_extra_charsets_defined, "");
dfff8a69
JR
13659
13660 staticpro (&Qw32_charset_johab);
13661 Qw32_charset_johab = intern ("w32-charset-johab");
13662 staticpro (&Qw32_charset_easteurope);
13663 Qw32_charset_easteurope = intern ("w32-charset-easteurope");
13664 staticpro (&Qw32_charset_turkish);
13665 Qw32_charset_turkish = intern ("w32-charset-turkish");
13666 staticpro (&Qw32_charset_baltic);
13667 Qw32_charset_baltic = intern ("w32-charset-baltic");
13668 staticpro (&Qw32_charset_russian);
13669 Qw32_charset_russian = intern ("w32-charset-russian");
13670 staticpro (&Qw32_charset_arabic);
13671 Qw32_charset_arabic = intern ("w32-charset-arabic");
13672 staticpro (&Qw32_charset_greek);
13673 Qw32_charset_greek = intern ("w32-charset-greek");
13674 staticpro (&Qw32_charset_hebrew);
13675 Qw32_charset_hebrew = intern ("w32-charset-hebrew");
767b1ff0
JR
13676 staticpro (&Qw32_charset_vietnamese);
13677 Qw32_charset_vietnamese = intern ("w32-charset-vietnamese");
dfff8a69
JR
13678 staticpro (&Qw32_charset_thai);
13679 Qw32_charset_thai = intern ("w32-charset-thai");
13680 staticpro (&Qw32_charset_mac);
13681 Qw32_charset_mac = intern ("w32-charset-mac");
13682 }
13683#endif
13684
13685#ifdef UNICODE_CHARSET
13686 {
13687 static int w32_unicode_charset_defined = 1;
13688 DEFVAR_BOOL ("w32-unicode-charset-defined",
767b1ff0 13689 &w32_unicode_charset_defined, "");
dfff8a69
JR
13690
13691 staticpro (&Qw32_charset_unicode);
13692 Qw32_charset_unicode = intern ("w32-charset-unicode");
13693#endif
13694
ee78dc32 13695 defsubr (&Sx_get_resource);
767b1ff0 13696#if 0 /* TODO: Port to W32 */
6fc2811b
JR
13697 defsubr (&Sx_change_window_property);
13698 defsubr (&Sx_delete_window_property);
13699 defsubr (&Sx_window_property);
13700#endif
2d764c78 13701 defsubr (&Sxw_display_color_p);
ee78dc32 13702 defsubr (&Sx_display_grayscale_p);
2d764c78
EZ
13703 defsubr (&Sxw_color_defined_p);
13704 defsubr (&Sxw_color_values);
ee78dc32
GV
13705 defsubr (&Sx_server_max_request_size);
13706 defsubr (&Sx_server_vendor);
13707 defsubr (&Sx_server_version);
13708 defsubr (&Sx_display_pixel_width);
13709 defsubr (&Sx_display_pixel_height);
13710 defsubr (&Sx_display_mm_width);
13711 defsubr (&Sx_display_mm_height);
13712 defsubr (&Sx_display_screens);
13713 defsubr (&Sx_display_planes);
13714 defsubr (&Sx_display_color_cells);
13715 defsubr (&Sx_display_visual_class);
13716 defsubr (&Sx_display_backing_store);
13717 defsubr (&Sx_display_save_under);
13718 defsubr (&Sx_parse_geometry);
13719 defsubr (&Sx_create_frame);
ee78dc32
GV
13720 defsubr (&Sx_open_connection);
13721 defsubr (&Sx_close_connection);
13722 defsubr (&Sx_display_list);
13723 defsubr (&Sx_synchronize);
13724
fbd6baed 13725 /* W32 specific functions */
ee78dc32 13726
1edf84e7 13727 defsubr (&Sw32_focus_frame);
fbd6baed
GV
13728 defsubr (&Sw32_select_font);
13729 defsubr (&Sw32_define_rgb_color);
13730 defsubr (&Sw32_default_color_map);
13731 defsubr (&Sw32_load_color_file);
1edf84e7 13732 defsubr (&Sw32_send_sys_command);
55dcfc15 13733 defsubr (&Sw32_shell_execute);
ccc2d29c
GV
13734 defsubr (&Sw32_register_hot_key);
13735 defsubr (&Sw32_unregister_hot_key);
13736 defsubr (&Sw32_registered_hot_keys);
13737 defsubr (&Sw32_reconstruct_hot_key);
adcc3809 13738 defsubr (&Sw32_toggle_lock_key);
33d52f9c 13739 defsubr (&Sw32_find_bdf_fonts);
4587b026 13740
2254bcde
AI
13741 defsubr (&Sfile_system_info);
13742
4587b026
GV
13743 /* Setting callback functions for fontset handler. */
13744 get_font_info_func = w32_get_font_info;
6fc2811b
JR
13745
13746#if 0 /* This function pointer doesn't seem to be used anywhere.
13747 And the pointer assigned has the wrong type, anyway. */
4587b026 13748 list_fonts_func = w32_list_fonts;
6fc2811b
JR
13749#endif
13750
4587b026
GV
13751 load_font_func = w32_load_font;
13752 find_ccl_program_func = w32_find_ccl_program;
13753 query_font_func = w32_query_font;
13754 set_frame_fontset_func = x_set_font;
13755 check_window_system_func = check_w32;
6fc2811b 13756
767b1ff0 13757#if 0 /* TODO Image support for W32 */
6fc2811b
JR
13758 /* Images. */
13759 Qxbm = intern ("xbm");
13760 staticpro (&Qxbm);
13761 QCtype = intern (":type");
13762 staticpro (&QCtype);
13763 QCalgorithm = intern (":algorithm");
13764 staticpro (&QCalgorithm);
13765 QCheuristic_mask = intern (":heuristic-mask");
13766 staticpro (&QCheuristic_mask);
13767 QCcolor_symbols = intern (":color-symbols");
13768 staticpro (&QCcolor_symbols);
6fc2811b
JR
13769 QCascent = intern (":ascent");
13770 staticpro (&QCascent);
13771 QCmargin = intern (":margin");
13772 staticpro (&QCmargin);
13773 QCrelief = intern (":relief");
13774 staticpro (&QCrelief);
13775 Qpostscript = intern ("postscript");
13776 staticpro (&Qpostscript);
13777 QCloader = intern (":loader");
13778 staticpro (&QCloader);
13779 QCbounding_box = intern (":bounding-box");
13780 staticpro (&QCbounding_box);
13781 QCpt_width = intern (":pt-width");
13782 staticpro (&QCpt_width);
13783 QCpt_height = intern (":pt-height");
13784 staticpro (&QCpt_height);
13785 QCindex = intern (":index");
13786 staticpro (&QCindex);
13787 Qpbm = intern ("pbm");
13788 staticpro (&Qpbm);
13789
13790#if HAVE_XPM
13791 Qxpm = intern ("xpm");
13792 staticpro (&Qxpm);
13793#endif
13794
13795#if HAVE_JPEG
13796 Qjpeg = intern ("jpeg");
13797 staticpro (&Qjpeg);
13798#endif
13799
13800#if HAVE_TIFF
13801 Qtiff = intern ("tiff");
13802 staticpro (&Qtiff);
13803#endif
13804
13805#if HAVE_GIF
13806 Qgif = intern ("gif");
13807 staticpro (&Qgif);
13808#endif
13809
13810#if HAVE_PNG
13811 Qpng = intern ("png");
13812 staticpro (&Qpng);
13813#endif
13814
13815 defsubr (&Sclear_image_cache);
13816
13817#if GLYPH_DEBUG
13818 defsubr (&Simagep);
13819 defsubr (&Slookup_image);
13820#endif
767b1ff0 13821#endif /* TODO */
6fc2811b 13822
dfff8a69
JR
13823 busy_cursor_atimer = NULL;
13824 busy_cursor_shown_p = 0;
767b1ff0 13825#ifdef TODO /* Tooltip support not complete. */
6fc2811b
JR
13826 defsubr (&Sx_show_tip);
13827 defsubr (&Sx_hide_tip);
767b1ff0 13828#endif
6fc2811b
JR
13829 staticpro (&tip_timer);
13830 tip_timer = Qnil;
13831
13832 defsubr (&Sx_file_dialog);
13833}
13834
13835
13836void
13837init_xfns ()
13838{
13839 image_types = NULL;
13840 Vimage_types = Qnil;
13841
767b1ff0 13842#if 0 /* TODO : Image support for W32 */
6fc2811b
JR
13843 define_image_type (&xbm_type);
13844 define_image_type (&gs_type);
13845 define_image_type (&pbm_type);
13846
13847#if HAVE_XPM
13848 define_image_type (&xpm_type);
13849#endif
13850
13851#if HAVE_JPEG
13852 define_image_type (&jpeg_type);
13853#endif
13854
13855#if HAVE_TIFF
13856 define_image_type (&tiff_type);
13857#endif
13858
13859#if HAVE_GIF
13860 define_image_type (&gif_type);
13861#endif
13862
13863#if HAVE_PNG
13864 define_image_type (&png_type);
13865#endif
767b1ff0 13866#endif /* TODO */
ee78dc32
GV
13867}
13868
13869#undef abort
13870
13871void
fbd6baed 13872w32_abort()
ee78dc32 13873{
5ac45f98
GV
13874 int button;
13875 button = MessageBox (NULL,
13876 "A fatal error has occurred!\n\n"
13877 "Select Abort to exit, Retry to debug, Ignore to continue",
13878 "Emacs Abort Dialog",
13879 MB_ICONEXCLAMATION | MB_TASKMODAL
13880 | MB_SETFOREGROUND | MB_ABORTRETRYIGNORE);
13881 switch (button)
13882 {
13883 case IDRETRY:
13884 DebugBreak ();
13885 break;
13886 case IDIGNORE:
13887 break;
13888 case IDABORT:
13889 default:
13890 abort ();
13891 break;
13892 }
ee78dc32 13893}
d573caac 13894
83c75055
GV
13895/* For convenience when debugging. */
13896int
13897w32_last_error()
13898{
13899 return GetLastError ();
13900}