(QCtype): Remove duplicate declaration and
[bpt/emacs.git] / src / w32fns.c
CommitLineData
e9e23e23 1/* Graphical user interface functions for the Microsoft W32 API.
a93f4566 2 Copyright (C) 1989, 92, 93, 94, 95, 1996, 1997, 1998, 1999, 2000, 2001
6fc2811b 3 Free Software Foundation, Inc.
ee78dc32
GV
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
ee78dc32
GV
21
22/* Added by Kevin Gallo */
23
ee78dc32 24#include <config.h>
1edf84e7
GV
25
26#include <signal.h>
ee78dc32 27#include <stdio.h>
1edf84e7
GV
28#include <limits.h>
29#include <errno.h>
ee78dc32
GV
30
31#include "lisp.h"
4587b026 32#include "charset.h"
71eab8d1 33#include "dispextern.h"
ee78dc32 34#include "w32term.h"
c7501041 35#include "keyboard.h"
ee78dc32
GV
36#include "frame.h"
37#include "window.h"
38#include "buffer.h"
126f2e35 39#include "fontset.h"
6fc2811b 40#include "intervals.h"
ee78dc32 41#include "blockinput.h"
57bda87a 42#include "epaths.h"
489f9371 43#include "w32heap.h"
ee78dc32 44#include "termhooks.h"
4587b026 45#include "coding.h"
3545439c 46#include "ccl.h"
6fc2811b
JR
47#include "systime.h"
48
49#include "bitmaps/gray.xbm"
ee78dc32
GV
50
51#include <commdlg.h>
cb9e33d4 52#include <shellapi.h>
6fc2811b 53#include <ctype.h>
ee78dc32 54
ee78dc32 55extern void free_frame_menubar ();
9eb16b62 56extern void x_compute_fringe_widths P_ ((struct frame *, int));
6fc2811b 57extern double atof ();
9eb16b62
JR
58extern int w32_console_toggle_lock_key P_ ((int, Lisp_Object));
59extern void w32_menu_display_help P_ ((HWND, HMENU, UINT, UINT));
60extern void w32_free_menu_strings P_ ((HWND));
61
5ac45f98 62extern int quit_char;
ee78dc32 63
6fc2811b
JR
64/* A definition of XColor for non-X frames. */
65#ifndef HAVE_X_WINDOWS
66typedef struct {
67 unsigned long pixel;
68 unsigned short red, green, blue;
69 char flags;
70 char pad;
71} XColor;
72#endif
73
ccc2d29c
GV
74extern char *lispy_function_keys[];
75
6fc2811b
JR
76/* The gray bitmap `bitmaps/gray'. This is done because w32term.c uses
77 it, and including `bitmaps/gray' more than once is a problem when
78 config.h defines `static' as an empty replacement string. */
79
80int gray_bitmap_width = gray_width;
81int gray_bitmap_height = gray_height;
82unsigned char *gray_bitmap_bits = gray_bits;
83
ee78dc32 84/* The colormap for converting color names to RGB values */
fbd6baed 85Lisp_Object Vw32_color_map;
ee78dc32 86
da36a4d6 87/* Non nil if alt key presses are passed on to Windows. */
fbd6baed 88Lisp_Object Vw32_pass_alt_to_system;
da36a4d6 89
8c205c63
RS
90/* Non nil if alt key is translated to meta_modifier, nil if it is translated
91 to alt_modifier. */
fbd6baed 92Lisp_Object Vw32_alt_is_meta;
8c205c63 93
7d081355
AI
94/* If non-zero, the windows virtual key code for an alternative quit key. */
95Lisp_Object Vw32_quit_key;
96
ccc2d29c
GV
97/* Non nil if left window key events are passed on to Windows (this only
98 affects whether "tapping" the key opens the Start menu). */
99Lisp_Object Vw32_pass_lwindow_to_system;
100
101/* Non nil if right window key events are passed on to Windows (this
102 only affects whether "tapping" the key opens the Start menu). */
103Lisp_Object Vw32_pass_rwindow_to_system;
104
adcc3809
GV
105/* Virtual key code used to generate "phantom" key presses in order
106 to stop system from acting on Windows key events. */
107Lisp_Object Vw32_phantom_key_code;
108
ccc2d29c
GV
109/* Modifier associated with the left "Windows" key, or nil to act as a
110 normal key. */
111Lisp_Object Vw32_lwindow_modifier;
112
113/* Modifier associated with the right "Windows" key, or nil to act as a
114 normal key. */
115Lisp_Object Vw32_rwindow_modifier;
116
117/* Modifier associated with the "Apps" key, or nil to act as a normal
118 key. */
119Lisp_Object Vw32_apps_modifier;
120
121/* Value is nil if Num Lock acts as a function key. */
122Lisp_Object Vw32_enable_num_lock;
123
124/* Value is nil if Caps Lock acts as a function key. */
125Lisp_Object Vw32_enable_caps_lock;
126
127/* Modifier associated with Scroll Lock, or nil to act as a normal key. */
128Lisp_Object Vw32_scroll_lock_modifier;
da36a4d6 129
7ce9aaca 130/* Switch to control whether we inhibit requests for synthesized bold
6fc2811b 131 and italic versions of fonts. */
d84b082d 132int w32_enable_synthesized_fonts;
5ac45f98
GV
133
134/* Enable palette management. */
fbd6baed 135Lisp_Object Vw32_enable_palette;
5ac45f98
GV
136
137/* Control how close left/right button down events must be to
138 be converted to a middle button down event. */
fbd6baed 139Lisp_Object Vw32_mouse_button_tolerance;
5ac45f98 140
84fb1139
KH
141/* Minimum interval between mouse movement (and scroll bar drag)
142 events that are passed on to the event loop. */
fbd6baed 143Lisp_Object Vw32_mouse_move_interval;
84fb1139 144
74214547
JR
145/* Flag to indicate if XBUTTON events should be passed on to Windows. */
146int w32_pass_extra_mouse_buttons_to_system;
147
ee78dc32
GV
148/* The name we're using in resource queries. */
149Lisp_Object Vx_resource_name;
150
151/* Non nil if no window manager is in use. */
152Lisp_Object Vx_no_window_manager;
153
0af913d7 154/* Non-zero means we're allowed to display a hourglass pointer. */
dfff8a69 155
0af913d7 156int display_hourglass_p;
6fc2811b 157
ee78dc32
GV
158/* The background and shape of the mouse pointer, and shape when not
159 over text or in the modeline. */
dfff8a69 160
ee78dc32 161Lisp_Object Vx_pointer_shape, Vx_nontext_pointer_shape, Vx_mode_pointer_shape;
0af913d7 162Lisp_Object Vx_hourglass_pointer_shape, Vx_window_horizontal_drag_shape;
6fc2811b 163
ee78dc32 164/* The shape when over mouse-sensitive text. */
dfff8a69 165
ee78dc32
GV
166Lisp_Object Vx_sensitive_text_pointer_shape;
167
168/* Color of chars displayed in cursor box. */
dfff8a69 169
ee78dc32
GV
170Lisp_Object Vx_cursor_fore_pixel;
171
1edf84e7 172/* Nonzero if using Windows. */
dfff8a69 173
1edf84e7
GV
174static int w32_in_use;
175
ee78dc32 176/* Search path for bitmap files. */
dfff8a69 177
ee78dc32
GV
178Lisp_Object Vx_bitmap_file_path;
179
4587b026 180/* Regexp matching a font name whose width is the same as `PIXEL_SIZE'. */
dfff8a69 181
4587b026
GV
182Lisp_Object Vx_pixel_size_width_font_regexp;
183
33d52f9c
GV
184/* Alist of bdf fonts and the files that define them. */
185Lisp_Object Vw32_bdf_filename_alist;
186
f46e6225 187/* A flag to control whether fonts are matched strictly or not. */
1075afa9
GV
188int w32_strict_fontnames;
189
c0611964
AI
190/* A flag to control whether we should only repaint if GetUpdateRect
191 indicates there is an update region. */
192int w32_strict_painting;
193
dfff8a69
JR
194/* Associative list linking character set strings to Windows codepages. */
195Lisp_Object Vw32_charset_info_alist;
196
197/* VIETNAMESE_CHARSET is not defined in some versions of MSVC. */
198#ifndef VIETNAMESE_CHARSET
199#define VIETNAMESE_CHARSET 163
200#endif
201
ee78dc32
GV
202Lisp_Object Qauto_raise;
203Lisp_Object Qauto_lower;
ee78dc32
GV
204Lisp_Object Qbar;
205Lisp_Object Qborder_color;
206Lisp_Object Qborder_width;
207Lisp_Object Qbox;
208Lisp_Object Qcursor_color;
209Lisp_Object Qcursor_type;
ee78dc32
GV
210Lisp_Object Qgeometry;
211Lisp_Object Qicon_left;
212Lisp_Object Qicon_top;
213Lisp_Object Qicon_type;
214Lisp_Object Qicon_name;
215Lisp_Object Qinternal_border_width;
216Lisp_Object Qleft;
1026b400 217Lisp_Object Qright;
ee78dc32
GV
218Lisp_Object Qmouse_color;
219Lisp_Object Qnone;
220Lisp_Object Qparent_id;
221Lisp_Object Qscroll_bar_width;
222Lisp_Object Qsuppress_icon;
ee78dc32
GV
223Lisp_Object Qundefined_color;
224Lisp_Object Qvertical_scroll_bars;
225Lisp_Object Qvisibility;
226Lisp_Object Qwindow_id;
227Lisp_Object Qx_frame_parameter;
228Lisp_Object Qx_resource_name;
229Lisp_Object Quser_position;
230Lisp_Object Quser_size;
6fc2811b 231Lisp_Object Qscreen_gamma;
dfff8a69
JR
232Lisp_Object Qline_spacing;
233Lisp_Object Qcenter;
dc220243 234Lisp_Object Qcancel_timer;
adcc3809
GV
235Lisp_Object Qhyper;
236Lisp_Object Qsuper;
237Lisp_Object Qmeta;
238Lisp_Object Qalt;
239Lisp_Object Qctrl;
240Lisp_Object Qcontrol;
241Lisp_Object Qshift;
242
dfff8a69
JR
243Lisp_Object Qw32_charset_ansi;
244Lisp_Object Qw32_charset_default;
245Lisp_Object Qw32_charset_symbol;
246Lisp_Object Qw32_charset_shiftjis;
767b1ff0 247Lisp_Object Qw32_charset_hangeul;
dfff8a69
JR
248Lisp_Object Qw32_charset_gb2312;
249Lisp_Object Qw32_charset_chinesebig5;
250Lisp_Object Qw32_charset_oem;
251
71eab8d1
AI
252#ifndef JOHAB_CHARSET
253#define JOHAB_CHARSET 130
254#endif
dfff8a69
JR
255#ifdef JOHAB_CHARSET
256Lisp_Object Qw32_charset_easteurope;
257Lisp_Object Qw32_charset_turkish;
258Lisp_Object Qw32_charset_baltic;
259Lisp_Object Qw32_charset_russian;
260Lisp_Object Qw32_charset_arabic;
261Lisp_Object Qw32_charset_greek;
262Lisp_Object Qw32_charset_hebrew;
767b1ff0 263Lisp_Object Qw32_charset_vietnamese;
dfff8a69
JR
264Lisp_Object Qw32_charset_thai;
265Lisp_Object Qw32_charset_johab;
266Lisp_Object Qw32_charset_mac;
267#endif
268
269#ifdef UNICODE_CHARSET
270Lisp_Object Qw32_charset_unicode;
271#endif
272
f7b9d4d1
JR
273Lisp_Object Qfullscreen;
274Lisp_Object Qfullwidth;
275Lisp_Object Qfullheight;
276Lisp_Object Qfullboth;
277
6fc2811b
JR
278extern Lisp_Object Qtop;
279extern Lisp_Object Qdisplay;
280extern Lisp_Object Qtool_bar_lines;
281
5ac45f98
GV
282/* State variables for emulating a three button mouse. */
283#define LMOUSE 1
284#define MMOUSE 2
285#define RMOUSE 4
286
287static int button_state = 0;
fbd6baed 288static W32Msg saved_mouse_button_msg;
48094ace 289static unsigned mouse_button_timer = 0; /* non-zero when timer is active */
fbd6baed 290static W32Msg saved_mouse_move_msg;
48094ace 291static unsigned mouse_move_timer = 0;
84fb1139 292
9eb16b62
JR
293/* Window that is tracking the mouse. */
294static HWND track_mouse_window;
295FARPROC track_mouse_event_fn;
296
93fbe8b7
GV
297/* W95 mousewheel handler */
298unsigned int msh_mousewheel = 0;
299
48094ace 300/* Timers */
84fb1139
KH
301#define MOUSE_BUTTON_ID 1
302#define MOUSE_MOVE_ID 2
48094ace
JR
303#define MENU_FREE_ID 3
304/* The delay (milliseconds) before a menu is freed after WM_EXITMENULOOP
305 is received. */
306#define MENU_FREE_DELAY 1000
307static unsigned menu_free_timer = 0;
5ac45f98 308
ee78dc32 309/* The below are defined in frame.c. */
dfff8a69 310
ee78dc32 311extern Lisp_Object Qheight, Qminibuffer, Qname, Qonly, Qwidth;
1edf84e7 312extern Lisp_Object Qunsplittable, Qmenu_bar_lines, Qbuffer_predicate, Qtitle;
6fc2811b 313extern Lisp_Object Qtool_bar_lines;
ee78dc32
GV
314
315extern Lisp_Object Vwindow_system_version;
316
4b817373
RS
317Lisp_Object Qface_set_after_frame_default;
318
937e601e
AI
319#ifdef GLYPH_DEBUG
320int image_cache_refcount, dpyinfo_refcount;
321#endif
322
323
fbd6baed
GV
324/* From w32term.c. */
325extern Lisp_Object Vw32_num_mouse_buttons;
ccc2d29c 326extern Lisp_Object Vw32_recognize_altgr;
5ac45f98 327
65906840 328extern HWND w32_system_caret_hwnd;
93f2ca61 329
65906840
JR
330extern int w32_system_caret_height;
331extern int w32_system_caret_x;
332extern int w32_system_caret_y;
93f2ca61
JR
333extern int w32_use_visible_system_caret;
334
d285988b 335static HWND w32_visible_system_caret_hwnd;
65906840 336
ee78dc32 337\f
1edf84e7
GV
338/* Error if we are not connected to MS-Windows. */
339void
340check_w32 ()
341{
342 if (! w32_in_use)
343 error ("MS-Windows not in use or not initialized");
344}
345
346/* Nonzero if we can use mouse menus.
347 You should not call this unless HAVE_MENUS is defined. */
348
349int
350have_menus_p ()
351{
352 return w32_in_use;
353}
354
ee78dc32 355/* Extract a frame as a FRAME_PTR, defaulting to the selected frame
fbd6baed 356 and checking validity for W32. */
ee78dc32
GV
357
358FRAME_PTR
359check_x_frame (frame)
360 Lisp_Object frame;
361{
362 FRAME_PTR f;
363
364 if (NILP (frame))
6fc2811b 365 frame = selected_frame;
b7826503 366 CHECK_LIVE_FRAME (frame);
6fc2811b 367 f = XFRAME (frame);
fbd6baed
GV
368 if (! FRAME_W32_P (f))
369 error ("non-w32 frame used");
ee78dc32
GV
370 return f;
371}
372
373/* Let the user specify an display with a frame.
fbd6baed 374 nil stands for the selected frame--or, if that is not a w32 frame,
ee78dc32
GV
375 the first display on the list. */
376
fbd6baed 377static struct w32_display_info *
ee78dc32
GV
378check_x_display_info (frame)
379 Lisp_Object frame;
380{
381 if (NILP (frame))
382 {
6fc2811b
JR
383 struct frame *sf = XFRAME (selected_frame);
384
385 if (FRAME_W32_P (sf) && FRAME_LIVE_P (sf))
386 return FRAME_W32_DISPLAY_INFO (sf);
ee78dc32 387 else
fbd6baed 388 return &one_w32_display_info;
ee78dc32
GV
389 }
390 else if (STRINGP (frame))
391 return x_display_info_for_name (frame);
392 else
393 {
394 FRAME_PTR f;
395
b7826503 396 CHECK_LIVE_FRAME (frame);
ee78dc32 397 f = XFRAME (frame);
fbd6baed
GV
398 if (! FRAME_W32_P (f))
399 error ("non-w32 frame used");
400 return FRAME_W32_DISPLAY_INFO (f);
ee78dc32
GV
401 }
402}
403\f
fbd6baed 404/* Return the Emacs frame-object corresponding to an w32 window.
ee78dc32
GV
405 It could be the frame's main window or an icon window. */
406
407/* This function can be called during GC, so use GC_xxx type test macros. */
408
409struct frame *
410x_window_to_frame (dpyinfo, wdesc)
fbd6baed 411 struct w32_display_info *dpyinfo;
ee78dc32
GV
412 HWND wdesc;
413{
414 Lisp_Object tail, frame;
415 struct frame *f;
416
8e713be6 417 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
ee78dc32 418 {
8e713be6 419 frame = XCAR (tail);
ee78dc32
GV
420 if (!GC_FRAMEP (frame))
421 continue;
422 f = XFRAME (frame);
2d764c78 423 if (!FRAME_W32_P (f) || FRAME_W32_DISPLAY_INFO (f) != dpyinfo)
ee78dc32 424 continue;
0af913d7 425 if (f->output_data.w32->hourglass_window == wdesc)
f79e6790
JR
426 return f;
427
fbd6baed 428 if (FRAME_W32_WINDOW (f) == wdesc)
ee78dc32
GV
429 return f;
430 }
431 return 0;
432}
433
434\f
435
436/* Code to deal with bitmaps. Bitmaps are referenced by their bitmap
437 id, which is just an int that this section returns. Bitmaps are
438 reference counted so they can be shared among frames.
439
440 Bitmap indices are guaranteed to be > 0, so a negative number can
441 be used to indicate no bitmap.
442
443 If you use x_create_bitmap_from_data, then you must keep track of
444 the bitmaps yourself. That is, creating a bitmap from the same
445 data more than once will not be caught. */
446
447
448/* Functions to access the contents of a bitmap, given an id. */
449
450int
451x_bitmap_height (f, id)
452 FRAME_PTR f;
453 int id;
454{
fbd6baed 455 return FRAME_W32_DISPLAY_INFO (f)->bitmaps[id - 1].height;
ee78dc32
GV
456}
457
458int
459x_bitmap_width (f, id)
460 FRAME_PTR f;
461 int id;
462{
fbd6baed 463 return FRAME_W32_DISPLAY_INFO (f)->bitmaps[id - 1].width;
ee78dc32
GV
464}
465
466int
467x_bitmap_pixmap (f, id)
468 FRAME_PTR f;
469 int id;
470{
fbd6baed 471 return (int) FRAME_W32_DISPLAY_INFO (f)->bitmaps[id - 1].pixmap;
ee78dc32
GV
472}
473
474
475/* Allocate a new bitmap record. Returns index of new record. */
476
477static int
478x_allocate_bitmap_record (f)
479 FRAME_PTR f;
480{
fbd6baed 481 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
ee78dc32
GV
482 int i;
483
484 if (dpyinfo->bitmaps == NULL)
485 {
486 dpyinfo->bitmaps_size = 10;
487 dpyinfo->bitmaps
fbd6baed 488 = (struct w32_bitmap_record *) xmalloc (dpyinfo->bitmaps_size * sizeof (struct w32_bitmap_record));
ee78dc32
GV
489 dpyinfo->bitmaps_last = 1;
490 return 1;
491 }
492
493 if (dpyinfo->bitmaps_last < dpyinfo->bitmaps_size)
494 return ++dpyinfo->bitmaps_last;
495
496 for (i = 0; i < dpyinfo->bitmaps_size; ++i)
497 if (dpyinfo->bitmaps[i].refcount == 0)
498 return i + 1;
499
500 dpyinfo->bitmaps_size *= 2;
501 dpyinfo->bitmaps
fbd6baed
GV
502 = (struct w32_bitmap_record *) xrealloc (dpyinfo->bitmaps,
503 dpyinfo->bitmaps_size * sizeof (struct w32_bitmap_record));
ee78dc32
GV
504 return ++dpyinfo->bitmaps_last;
505}
506
507/* Add one reference to the reference count of the bitmap with id ID. */
508
509void
510x_reference_bitmap (f, id)
511 FRAME_PTR f;
512 int id;
513{
fbd6baed 514 ++FRAME_W32_DISPLAY_INFO (f)->bitmaps[id - 1].refcount;
ee78dc32
GV
515}
516
517/* Create a bitmap for frame F from a HEIGHT x WIDTH array of bits at BITS. */
518
519int
520x_create_bitmap_from_data (f, bits, width, height)
521 struct frame *f;
522 char *bits;
523 unsigned int width, height;
524{
fbd6baed 525 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
ee78dc32
GV
526 Pixmap bitmap;
527 int id;
528
529 bitmap = CreateBitmap (width, height,
fbd6baed
GV
530 FRAME_W32_DISPLAY_INFO (XFRAME (frame))->n_planes,
531 FRAME_W32_DISPLAY_INFO (XFRAME (frame))->n_cbits,
ee78dc32
GV
532 bits);
533
534 if (! bitmap)
535 return -1;
536
537 id = x_allocate_bitmap_record (f);
538 dpyinfo->bitmaps[id - 1].pixmap = bitmap;
539 dpyinfo->bitmaps[id - 1].file = NULL;
540 dpyinfo->bitmaps[id - 1].hinst = NULL;
541 dpyinfo->bitmaps[id - 1].refcount = 1;
542 dpyinfo->bitmaps[id - 1].depth = 1;
543 dpyinfo->bitmaps[id - 1].height = height;
544 dpyinfo->bitmaps[id - 1].width = width;
545
546 return id;
547}
548
549/* Create bitmap from file FILE for frame F. */
550
551int
552x_create_bitmap_from_file (f, file)
553 struct frame *f;
554 Lisp_Object file;
555{
556 return -1;
767b1ff0 557#if 0 /* TODO : bitmap support */
fbd6baed 558 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
ee78dc32 559 unsigned int width, height;
6fc2811b 560 HBITMAP bitmap;
ee78dc32
GV
561 int xhot, yhot, result, id;
562 Lisp_Object found;
563 int fd;
564 char *filename;
565 HINSTANCE hinst;
566
567 /* Look for an existing bitmap with the same name. */
568 for (id = 0; id < dpyinfo->bitmaps_last; ++id)
569 {
570 if (dpyinfo->bitmaps[id].refcount
571 && dpyinfo->bitmaps[id].file
572 && !strcmp (dpyinfo->bitmaps[id].file, (char *) XSTRING (file)->data))
573 {
574 ++dpyinfo->bitmaps[id].refcount;
575 return id + 1;
576 }
577 }
578
579 /* Search bitmap-file-path for the file, if appropriate. */
c0ec53ad 580 fd = openp (Vx_bitmap_file_path, file, Qnil, &found, 0);
ee78dc32
GV
581 if (fd < 0)
582 return -1;
6fc2811b 583 emacs_close (fd);
ee78dc32
GV
584
585 filename = (char *) XSTRING (found)->data;
586
587 hinst = LoadLibraryEx (filename, NULL, LOAD_LIBRARY_AS_DATAFILE);
588
589 if (hinst == NULL)
590 return -1;
591
592
fbd6baed 593 result = XReadBitmapFile (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
ee78dc32
GV
594 filename, &width, &height, &bitmap, &xhot, &yhot);
595 if (result != BitmapSuccess)
596 return -1;
597
598 id = x_allocate_bitmap_record (f);
599 dpyinfo->bitmaps[id - 1].pixmap = bitmap;
600 dpyinfo->bitmaps[id - 1].refcount = 1;
601 dpyinfo->bitmaps[id - 1].file = (char *) xmalloc (XSTRING (file)->size + 1);
602 dpyinfo->bitmaps[id - 1].depth = 1;
603 dpyinfo->bitmaps[id - 1].height = height;
604 dpyinfo->bitmaps[id - 1].width = width;
605 strcpy (dpyinfo->bitmaps[id - 1].file, XSTRING (file)->data);
606
607 return id;
767b1ff0 608#endif /* TODO */
ee78dc32
GV
609}
610
611/* Remove reference to bitmap with id number ID. */
612
33d52f9c 613void
ee78dc32
GV
614x_destroy_bitmap (f, id)
615 FRAME_PTR f;
616 int id;
617{
fbd6baed 618 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
ee78dc32
GV
619
620 if (id > 0)
621 {
622 --dpyinfo->bitmaps[id - 1].refcount;
623 if (dpyinfo->bitmaps[id - 1].refcount == 0)
624 {
625 BLOCK_INPUT;
626 DeleteObject (dpyinfo->bitmaps[id - 1].pixmap);
627 if (dpyinfo->bitmaps[id - 1].file)
628 {
6fc2811b 629 xfree (dpyinfo->bitmaps[id - 1].file);
ee78dc32
GV
630 dpyinfo->bitmaps[id - 1].file = NULL;
631 }
632 UNBLOCK_INPUT;
633 }
634 }
635}
636
637/* Free all the bitmaps for the display specified by DPYINFO. */
638
639static void
640x_destroy_all_bitmaps (dpyinfo)
fbd6baed 641 struct w32_display_info *dpyinfo;
ee78dc32
GV
642{
643 int i;
644 for (i = 0; i < dpyinfo->bitmaps_last; i++)
645 if (dpyinfo->bitmaps[i].refcount > 0)
646 {
647 DeleteObject (dpyinfo->bitmaps[i].pixmap);
648 if (dpyinfo->bitmaps[i].file)
6fc2811b 649 xfree (dpyinfo->bitmaps[i].file);
ee78dc32
GV
650 }
651 dpyinfo->bitmaps_last = 0;
652}
653\f
fbd6baed 654/* Connect the frame-parameter names for W32 frames
ee78dc32
GV
655 to the ways of passing the parameter values to the window system.
656
657 The name of a parameter, as a Lisp symbol,
658 has an `x-frame-parameter' property which is an integer in Lisp
659 but can be interpreted as an `enum x_frame_parm' in C. */
660
661enum x_frame_parm
662{
663 X_PARM_FOREGROUND_COLOR,
664 X_PARM_BACKGROUND_COLOR,
665 X_PARM_MOUSE_COLOR,
666 X_PARM_CURSOR_COLOR,
667 X_PARM_BORDER_COLOR,
668 X_PARM_ICON_TYPE,
669 X_PARM_FONT,
670 X_PARM_BORDER_WIDTH,
671 X_PARM_INTERNAL_BORDER_WIDTH,
672 X_PARM_NAME,
673 X_PARM_AUTORAISE,
674 X_PARM_AUTOLOWER,
675 X_PARM_VERT_SCROLL_BAR,
676 X_PARM_VISIBILITY,
677 X_PARM_MENU_BAR_LINES
678};
679
680
681struct x_frame_parm_table
682{
683 char *name;
6fc2811b 684 void (*setter) P_ ((struct frame *, Lisp_Object, Lisp_Object));
ee78dc32
GV
685};
686
ca56d953
JR
687BOOL my_show_window P_ ((struct frame *, HWND, int));
688void my_set_window_pos P_ ((HWND, HWND, int, int, int, int, UINT));
937e601e
AI
689static Lisp_Object unwind_create_frame P_ ((Lisp_Object));
690static Lisp_Object unwind_create_tip_frame P_ ((Lisp_Object));
691static void x_change_window_heights P_ ((Lisp_Object, int));
767b1ff0 692/* TODO: Native Input Method support; see x_create_im. */
6fc2811b 693void x_set_foreground_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
dfff8a69 694static void x_set_line_spacing P_ ((struct frame *, Lisp_Object, Lisp_Object));
f7b9d4d1 695static void x_set_fullscreen P_ ((struct frame *, Lisp_Object, Lisp_Object));
6fc2811b
JR
696void x_set_background_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
697void x_set_mouse_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
698void x_set_cursor_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
699void x_set_border_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
700void x_set_cursor_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
701void x_set_icon_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
702void x_set_icon_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
703void x_set_font P_ ((struct frame *, Lisp_Object, Lisp_Object));
41c1bdd9 704static void x_set_fringe_width P_ ((struct frame *, Lisp_Object, Lisp_Object));
6fc2811b
JR
705void x_set_border_width P_ ((struct frame *, Lisp_Object, Lisp_Object));
706void x_set_internal_border_width P_ ((struct frame *, Lisp_Object,
707 Lisp_Object));
708void x_explicitly_set_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
709void x_set_autoraise P_ ((struct frame *, Lisp_Object, Lisp_Object));
710void x_set_autolower P_ ((struct frame *, Lisp_Object, Lisp_Object));
711void x_set_vertical_scroll_bars P_ ((struct frame *, Lisp_Object,
712 Lisp_Object));
713void x_set_visibility P_ ((struct frame *, Lisp_Object, Lisp_Object));
714void x_set_menu_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
715void x_set_scroll_bar_width P_ ((struct frame *, Lisp_Object, Lisp_Object));
716void x_set_title P_ ((struct frame *, Lisp_Object, Lisp_Object));
717void x_set_unsplittable P_ ((struct frame *, Lisp_Object, Lisp_Object));
718void x_set_tool_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
719static void x_set_screen_gamma P_ ((struct frame *, Lisp_Object, Lisp_Object));
3cf3436e
JR
720static void x_edge_detection P_ ((struct frame *, struct image *, Lisp_Object,
721 Lisp_Object));
ee78dc32
GV
722
723static struct x_frame_parm_table x_frame_parms[] =
724{
72e4adef
JR
725 {"auto-raise", x_set_autoraise},
726 {"auto-lower", x_set_autolower},
727 {"background-color", x_set_background_color},
728 {"border-color", x_set_border_color},
729 {"border-width", x_set_border_width},
730 {"cursor-color", x_set_cursor_color},
731 {"cursor-type", x_set_cursor_type},
732 {"font", x_set_font},
733 {"foreground-color", x_set_foreground_color},
734 {"icon-name", x_set_icon_name},
735 {"icon-type", x_set_icon_type},
736 {"internal-border-width", x_set_internal_border_width},
737 {"menu-bar-lines", x_set_menu_bar_lines},
738 {"mouse-color", x_set_mouse_color},
739 {"name", x_explicitly_set_name},
740 {"scroll-bar-width", x_set_scroll_bar_width},
741 {"title", x_set_title},
742 {"unsplittable", x_set_unsplittable},
743 {"vertical-scroll-bars", x_set_vertical_scroll_bars},
744 {"visibility", x_set_visibility},
745 {"tool-bar-lines", x_set_tool_bar_lines},
746 {"screen-gamma", x_set_screen_gamma},
747 {"line-spacing", x_set_line_spacing},
748 {"left-fringe", x_set_fringe_width},
f7b9d4d1
JR
749 {"right-fringe", x_set_fringe_width},
750 {"fullscreen", x_set_fullscreen},
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
f7b9d4d1
JR
766/* Really try to move where we want to be in case of fullscreen. Some WMs
767 moves the window where we tell them. Some (mwm, twm) moves the outer
768 window manager window there instead.
769 Try to compensate for those WM here. */
770static void
771x_fullscreen_move (f, new_top, new_left)
772 struct frame *f;
773 int new_top;
774 int new_left;
775{
776 if (new_top != f->output_data.w32->top_pos
777 || new_left != f->output_data.w32->left_pos)
778 {
779 int move_x = new_left;
780 int move_y = new_top;
781
782 f->output_data.w32->want_fullscreen |= FULLSCREEN_MOVE_WAIT;
783 x_set_offset (f, move_x, move_y, 1);
784 }
785}
786
dfff8a69 787/* Change the parameters of frame F as specified by ALIST.
ee78dc32
GV
788 If a parameter is not specially recognized, do nothing;
789 otherwise call the `x_set_...' function for that parameter. */
790
791void
792x_set_frame_parameters (f, alist)
793 FRAME_PTR f;
794 Lisp_Object alist;
795{
796 Lisp_Object tail;
797
798 /* If both of these parameters are present, it's more efficient to
799 set them both at once. So we wait until we've looked at the
800 entire list before we set them. */
b839712d 801 int width, height;
ee78dc32
GV
802
803 /* Same here. */
804 Lisp_Object left, top;
805
806 /* Same with these. */
807 Lisp_Object icon_left, icon_top;
808
809 /* Record in these vectors all the parms specified. */
810 Lisp_Object *parms;
811 Lisp_Object *values;
a797a73d 812 int i, p;
ee78dc32
GV
813 int left_no_change = 0, top_no_change = 0;
814 int icon_left_no_change = 0, icon_top_no_change = 0;
f7b9d4d1 815 int fullscreen_is_being_set = 0;
ee78dc32 816
5878523b
RS
817 struct gcpro gcpro1, gcpro2;
818
ee78dc32
GV
819 i = 0;
820 for (tail = alist; CONSP (tail); tail = Fcdr (tail))
821 i++;
822
823 parms = (Lisp_Object *) alloca (i * sizeof (Lisp_Object));
824 values = (Lisp_Object *) alloca (i * sizeof (Lisp_Object));
825
826 /* Extract parm names and values into those vectors. */
827
828 i = 0;
829 for (tail = alist; CONSP (tail); tail = Fcdr (tail))
830 {
6fc2811b 831 Lisp_Object elt;
ee78dc32
GV
832
833 elt = Fcar (tail);
834 parms[i] = Fcar (elt);
835 values[i] = Fcdr (elt);
836 i++;
837 }
5878523b
RS
838 /* TAIL and ALIST are not used again below here. */
839 alist = tail = Qnil;
840
841 GCPRO2 (*parms, *values);
842 gcpro1.nvars = i;
843 gcpro2.nvars = i;
844
845 /* There is no need to gcpro LEFT, TOP, ICON_LEFT, or ICON_TOP,
846 because their values appear in VALUES and strings are not valid. */
b839712d 847 top = left = Qunbound;
ee78dc32
GV
848 icon_left = icon_top = Qunbound;
849
b839712d 850 /* Provide default values for HEIGHT and WIDTH. */
dfff8a69
JR
851 if (FRAME_NEW_WIDTH (f))
852 width = FRAME_NEW_WIDTH (f);
853 else
854 width = FRAME_WIDTH (f);
855
856 if (FRAME_NEW_HEIGHT (f))
857 height = FRAME_NEW_HEIGHT (f);
858 else
859 height = FRAME_HEIGHT (f);
b839712d 860
a797a73d
GV
861 /* Process foreground_color and background_color before anything else.
862 They are independent of other properties, but other properties (e.g.,
863 cursor_color) are dependent upon them. */
41c1bdd9 864 /* Process default font as well, since fringe widths depends on it. */
a797a73d
GV
865 for (p = 0; p < i; p++)
866 {
867 Lisp_Object prop, val;
868
869 prop = parms[p];
870 val = values[p];
41c1bdd9
KS
871 if (EQ (prop, Qforeground_color)
872 || EQ (prop, Qbackground_color)
f7b9d4d1
JR
873 || EQ (prop, Qfont)
874 || EQ (prop, Qfullscreen))
a797a73d
GV
875 {
876 register Lisp_Object param_index, old_value;
877
a797a73d 878 old_value = get_frame_param (f, prop);
f7b9d4d1 879 fullscreen_is_being_set |= EQ (prop, Qfullscreen);
a05e2bae
JR
880
881 if (NILP (Fequal (val, old_value)))
882 {
883 store_frame_param (f, prop, val);
884
885 param_index = Fget (prop, Qx_frame_parameter);
886 if (NATNUMP (param_index)
887 && (XFASTINT (param_index)
888 < sizeof (x_frame_parms)/sizeof (x_frame_parms[0])))
889 (*x_frame_parms[XINT (param_index)].setter)(f, val, old_value);
890 }
a797a73d
GV
891 }
892 }
893
ee78dc32
GV
894 /* Now process them in reverse of specified order. */
895 for (i--; i >= 0; i--)
896 {
897 Lisp_Object prop, val;
898
899 prop = parms[i];
900 val = values[i];
901
b839712d
RS
902 if (EQ (prop, Qwidth) && NUMBERP (val))
903 width = XFASTINT (val);
904 else if (EQ (prop, Qheight) && NUMBERP (val))
905 height = XFASTINT (val);
ee78dc32
GV
906 else if (EQ (prop, Qtop))
907 top = val;
908 else if (EQ (prop, Qleft))
909 left = val;
910 else if (EQ (prop, Qicon_top))
911 icon_top = val;
912 else if (EQ (prop, Qicon_left))
913 icon_left = val;
41c1bdd9
KS
914 else if (EQ (prop, Qforeground_color)
915 || EQ (prop, Qbackground_color)
f7b9d4d1
JR
916 || EQ (prop, Qfont)
917 || EQ (prop, Qfullscreen))
a797a73d
GV
918 /* Processed above. */
919 continue;
ee78dc32
GV
920 else
921 {
922 register Lisp_Object param_index, old_value;
923
ee78dc32 924 old_value = get_frame_param (f, prop);
a05e2bae 925
ee78dc32 926 store_frame_param (f, prop, val);
a05e2bae
JR
927
928 param_index = Fget (prop, Qx_frame_parameter);
ee78dc32
GV
929 if (NATNUMP (param_index)
930 && (XFASTINT (param_index)
931 < sizeof (x_frame_parms)/sizeof (x_frame_parms[0])))
3c190163 932 (*x_frame_parms[XINT (param_index)].setter)(f, val, old_value);
ee78dc32
GV
933 }
934 }
935
936 /* Don't die if just one of these was set. */
937 if (EQ (left, Qunbound))
938 {
939 left_no_change = 1;
fbd6baed
GV
940 if (f->output_data.w32->left_pos < 0)
941 left = Fcons (Qplus, Fcons (make_number (f->output_data.w32->left_pos), Qnil));
ee78dc32 942 else
fbd6baed 943 XSETINT (left, f->output_data.w32->left_pos);
ee78dc32
GV
944 }
945 if (EQ (top, Qunbound))
946 {
947 top_no_change = 1;
fbd6baed
GV
948 if (f->output_data.w32->top_pos < 0)
949 top = Fcons (Qplus, Fcons (make_number (f->output_data.w32->top_pos), Qnil));
ee78dc32 950 else
fbd6baed 951 XSETINT (top, f->output_data.w32->top_pos);
ee78dc32
GV
952 }
953
954 /* If one of the icon positions was not set, preserve or default it. */
955 if (EQ (icon_left, Qunbound) || ! INTEGERP (icon_left))
956 {
957 icon_left_no_change = 1;
958 icon_left = Fcdr (Fassq (Qicon_left, f->param_alist));
959 if (NILP (icon_left))
960 XSETINT (icon_left, 0);
961 }
962 if (EQ (icon_top, Qunbound) || ! INTEGERP (icon_top))
963 {
964 icon_top_no_change = 1;
965 icon_top = Fcdr (Fassq (Qicon_top, f->param_alist));
966 if (NILP (icon_top))
967 XSETINT (icon_top, 0);
968 }
969
f7b9d4d1
JR
970 if (FRAME_VISIBLE_P (f) && fullscreen_is_being_set)
971 {
972 /* If the frame is visible already and the fullscreen parameter is
973 being set, it is too late to set WM manager hints to specify
974 size and position.
975 Here we first get the width, height and position that applies to
976 fullscreen. We then move the frame to the appropriate
977 position. Resize of the frame is taken care of in the code after
978 this if-statement. */
979 int new_left, new_top;
980
981 x_fullscreen_adjust (f, &width, &height, &new_top, &new_left);
982 x_fullscreen_move (f, new_top, new_left);
983 }
984
ee78dc32
GV
985 /* Don't set these parameters unless they've been explicitly
986 specified. The window might be mapped or resized while we're in
987 this function, and we don't want to override that unless the lisp
988 code has asked for it.
989
990 Don't set these parameters unless they actually differ from the
991 window's current parameters; the window may not actually exist
992 yet. */
993 {
994 Lisp_Object frame;
995
996 check_frame_size (f, &height, &width);
997
998 XSETFRAME (frame, f);
999
dfff8a69
JR
1000 if (width != FRAME_WIDTH (f)
1001 || height != FRAME_HEIGHT (f)
1002 || FRAME_NEW_HEIGHT (f) || FRAME_NEW_WIDTH (f))
b839712d 1003 Fset_frame_size (frame, make_number (width), make_number (height));
ee78dc32
GV
1004
1005 if ((!NILP (left) || !NILP (top))
1006 && ! (left_no_change && top_no_change)
fbd6baed
GV
1007 && ! (NUMBERP (left) && XINT (left) == f->output_data.w32->left_pos
1008 && NUMBERP (top) && XINT (top) == f->output_data.w32->top_pos))
ee78dc32
GV
1009 {
1010 int leftpos = 0;
1011 int toppos = 0;
1012
1013 /* Record the signs. */
fbd6baed 1014 f->output_data.w32->size_hint_flags &= ~ (XNegative | YNegative);
ee78dc32 1015 if (EQ (left, Qminus))
fbd6baed 1016 f->output_data.w32->size_hint_flags |= XNegative;
ee78dc32
GV
1017 else if (INTEGERP (left))
1018 {
1019 leftpos = XINT (left);
1020 if (leftpos < 0)
fbd6baed 1021 f->output_data.w32->size_hint_flags |= XNegative;
ee78dc32 1022 }
8e713be6
KR
1023 else if (CONSP (left) && EQ (XCAR (left), Qminus)
1024 && CONSP (XCDR (left))
1025 && INTEGERP (XCAR (XCDR (left))))
ee78dc32 1026 {
8e713be6 1027 leftpos = - XINT (XCAR (XCDR (left)));
fbd6baed 1028 f->output_data.w32->size_hint_flags |= XNegative;
ee78dc32 1029 }
8e713be6
KR
1030 else if (CONSP (left) && EQ (XCAR (left), Qplus)
1031 && CONSP (XCDR (left))
1032 && INTEGERP (XCAR (XCDR (left))))
ee78dc32 1033 {
8e713be6 1034 leftpos = XINT (XCAR (XCDR (left)));
ee78dc32
GV
1035 }
1036
1037 if (EQ (top, Qminus))
fbd6baed 1038 f->output_data.w32->size_hint_flags |= YNegative;
ee78dc32
GV
1039 else if (INTEGERP (top))
1040 {
1041 toppos = XINT (top);
1042 if (toppos < 0)
fbd6baed 1043 f->output_data.w32->size_hint_flags |= YNegative;
ee78dc32 1044 }
8e713be6
KR
1045 else if (CONSP (top) && EQ (XCAR (top), Qminus)
1046 && CONSP (XCDR (top))
1047 && INTEGERP (XCAR (XCDR (top))))
ee78dc32 1048 {
8e713be6 1049 toppos = - XINT (XCAR (XCDR (top)));
fbd6baed 1050 f->output_data.w32->size_hint_flags |= YNegative;
ee78dc32 1051 }
8e713be6
KR
1052 else if (CONSP (top) && EQ (XCAR (top), Qplus)
1053 && CONSP (XCDR (top))
1054 && INTEGERP (XCAR (XCDR (top))))
ee78dc32 1055 {
8e713be6 1056 toppos = XINT (XCAR (XCDR (top)));
ee78dc32
GV
1057 }
1058
1059
1060 /* Store the numeric value of the position. */
fbd6baed
GV
1061 f->output_data.w32->top_pos = toppos;
1062 f->output_data.w32->left_pos = leftpos;
ee78dc32 1063
fbd6baed 1064 f->output_data.w32->win_gravity = NorthWestGravity;
ee78dc32
GV
1065
1066 /* Actually set that position, and convert to absolute. */
1067 x_set_offset (f, leftpos, toppos, -1);
1068 }
1069
1070 if ((!NILP (icon_left) || !NILP (icon_top))
1071 && ! (icon_left_no_change && icon_top_no_change))
1072 x_wm_set_icon_position (f, XINT (icon_left), XINT (icon_top));
1073 }
5878523b
RS
1074
1075 UNGCPRO;
ee78dc32
GV
1076}
1077
1078/* Store the screen positions of frame F into XPTR and YPTR.
1079 These are the positions of the containing window manager window,
1080 not Emacs's own window. */
1081
1082void
1083x_real_positions (f, xptr, yptr)
1084 FRAME_PTR f;
1085 int *xptr, *yptr;
1086{
1087 POINT pt;
f7b9d4d1 1088 RECT rect;
3c190163 1089
f7b9d4d1
JR
1090 GetClientRect(FRAME_W32_WINDOW(f), &rect);
1091 AdjustWindowRect(&rect, f->output_data.w32->dwStyle, FRAME_EXTERNAL_MENU_BAR(f));
1092
1093 pt.x = rect.left;
1094 pt.y = rect.top;
ee78dc32 1095
fbd6baed 1096 ClientToScreen (FRAME_W32_WINDOW(f), &pt);
ee78dc32 1097
f7b9d4d1
JR
1098 /* Remember x_pixels_diff and y_pixels_diff. */
1099 f->output_data.w32->x_pixels_diff = pt.x - rect.left;
1100 f->output_data.w32->y_pixels_diff = pt.y - rect.top;
1101
ee78dc32
GV
1102 *xptr = pt.x;
1103 *yptr = pt.y;
1104}
1105
1106/* Insert a description of internally-recorded parameters of frame X
1107 into the parameter alist *ALISTPTR that is to be given to the user.
fbd6baed 1108 Only parameters that are specific to W32
ee78dc32
GV
1109 and whose values are not correctly recorded in the frame's
1110 param_alist need to be considered here. */
1111
dfff8a69 1112void
ee78dc32
GV
1113x_report_frame_params (f, alistptr)
1114 struct frame *f;
1115 Lisp_Object *alistptr;
1116{
1117 char buf[16];
1118 Lisp_Object tem;
1119
1120 /* Represent negative positions (off the top or left screen edge)
1121 in a way that Fmodify_frame_parameters will understand correctly. */
fbd6baed
GV
1122 XSETINT (tem, f->output_data.w32->left_pos);
1123 if (f->output_data.w32->left_pos >= 0)
ee78dc32
GV
1124 store_in_alist (alistptr, Qleft, tem);
1125 else
1126 store_in_alist (alistptr, Qleft, Fcons (Qplus, Fcons (tem, Qnil)));
1127
fbd6baed
GV
1128 XSETINT (tem, f->output_data.w32->top_pos);
1129 if (f->output_data.w32->top_pos >= 0)
ee78dc32
GV
1130 store_in_alist (alistptr, Qtop, tem);
1131 else
1132 store_in_alist (alistptr, Qtop, Fcons (Qplus, Fcons (tem, Qnil)));
1133
1134 store_in_alist (alistptr, Qborder_width,
fbd6baed 1135 make_number (f->output_data.w32->border_width));
ee78dc32 1136 store_in_alist (alistptr, Qinternal_border_width,
fbd6baed 1137 make_number (f->output_data.w32->internal_border_width));
e90c3f90
KS
1138 store_in_alist (alistptr, Qleft_fringe,
1139 make_number (f->output_data.w32->left_fringe_width));
1140 store_in_alist (alistptr, Qright_fringe,
1141 make_number (f->output_data.w32->right_fringe_width));
aa17b858
EZ
1142 store_in_alist (alistptr, Qscroll_bar_width,
1143 make_number (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
1144 ? FRAME_SCROLL_BAR_PIXEL_WIDTH(f)
1145 : 0));
fbd6baed 1146 sprintf (buf, "%ld", (long) FRAME_W32_WINDOW (f));
ee78dc32
GV
1147 store_in_alist (alistptr, Qwindow_id,
1148 build_string (buf));
1149 store_in_alist (alistptr, Qicon_name, f->icon_name);
1150 FRAME_SAMPLE_VISIBILITY (f);
1151 store_in_alist (alistptr, Qvisibility,
1152 (FRAME_VISIBLE_P (f) ? Qt
1153 : FRAME_ICONIFIED_P (f) ? Qicon : Qnil));
1154 store_in_alist (alistptr, Qdisplay,
8e713be6 1155 XCAR (FRAME_W32_DISPLAY_INFO (f)->name_list_element));
ee78dc32
GV
1156}
1157\f
1158
74e1aeec
JR
1159DEFUN ("w32-define-rgb-color", Fw32_define_rgb_color,
1160 Sw32_define_rgb_color, 4, 4, 0,
1161 doc: /* Convert RGB numbers to a windows color reference and associate with NAME.
1162This adds or updates a named color to w32-color-map, making it
1163available for use. The original entry's RGB ref is returned, or nil
1164if the entry is new. */)
5ac45f98
GV
1165 (red, green, blue, name)
1166 Lisp_Object red, green, blue, name;
ee78dc32 1167{
5ac45f98
GV
1168 Lisp_Object rgb;
1169 Lisp_Object oldrgb = Qnil;
1170 Lisp_Object entry;
1171
b7826503
PJ
1172 CHECK_NUMBER (red);
1173 CHECK_NUMBER (green);
1174 CHECK_NUMBER (blue);
1175 CHECK_STRING (name);
ee78dc32 1176
5ac45f98 1177 XSET (rgb, Lisp_Int, RGB(XUINT (red), XUINT (green), XUINT (blue)));
ee78dc32 1178
5ac45f98 1179 BLOCK_INPUT;
ee78dc32 1180
fbd6baed
GV
1181 /* replace existing entry in w32-color-map or add new entry. */
1182 entry = Fassoc (name, Vw32_color_map);
5ac45f98
GV
1183 if (NILP (entry))
1184 {
1185 entry = Fcons (name, rgb);
fbd6baed 1186 Vw32_color_map = Fcons (entry, Vw32_color_map);
5ac45f98
GV
1187 }
1188 else
1189 {
1190 oldrgb = Fcdr (entry);
1191 Fsetcdr (entry, rgb);
1192 }
1193
1194 UNBLOCK_INPUT;
1195
1196 return (oldrgb);
ee78dc32
GV
1197}
1198
74e1aeec
JR
1199DEFUN ("w32-load-color-file", Fw32_load_color_file,
1200 Sw32_load_color_file, 1, 1, 0,
1201 doc: /* Create an alist of color entries from an external file.
1202Assign this value to w32-color-map to replace the existing color map.
1203
1204The file should define one named RGB color per line like so:
1205 R G B name
1206where R,G,B are numbers between 0 and 255 and name is an arbitrary string. */)
5ac45f98
GV
1207 (filename)
1208 Lisp_Object filename;
1209{
1210 FILE *fp;
1211 Lisp_Object cmap = Qnil;
1212 Lisp_Object abspath;
1213
b7826503 1214 CHECK_STRING (filename);
5ac45f98
GV
1215 abspath = Fexpand_file_name (filename, Qnil);
1216
1217 fp = fopen (XSTRING (filename)->data, "rt");
1218 if (fp)
1219 {
1220 char buf[512];
1221 int red, green, blue;
1222 int num;
1223
1224 BLOCK_INPUT;
1225
1226 while (fgets (buf, sizeof (buf), fp) != NULL) {
1227 if (sscanf (buf, "%u %u %u %n", &red, &green, &blue, &num) == 3)
1228 {
1229 char *name = buf + num;
1230 num = strlen (name) - 1;
1231 if (name[num] == '\n')
1232 name[num] = 0;
1233 cmap = Fcons (Fcons (build_string (name),
1234 make_number (RGB (red, green, blue))),
1235 cmap);
1236 }
1237 }
1238 fclose (fp);
1239
1240 UNBLOCK_INPUT;
1241 }
1242
1243 return cmap;
1244}
ee78dc32 1245
fbd6baed 1246/* The default colors for the w32 color map */
ee78dc32
GV
1247typedef struct colormap_t
1248{
1249 char *name;
1250 COLORREF colorref;
1251} colormap_t;
1252
fbd6baed 1253colormap_t w32_color_map[] =
ee78dc32 1254{
1da8a614
GV
1255 {"snow" , PALETTERGB (255,250,250)},
1256 {"ghost white" , PALETTERGB (248,248,255)},
1257 {"GhostWhite" , PALETTERGB (248,248,255)},
1258 {"white smoke" , PALETTERGB (245,245,245)},
1259 {"WhiteSmoke" , PALETTERGB (245,245,245)},
1260 {"gainsboro" , PALETTERGB (220,220,220)},
1261 {"floral white" , PALETTERGB (255,250,240)},
1262 {"FloralWhite" , PALETTERGB (255,250,240)},
1263 {"old lace" , PALETTERGB (253,245,230)},
1264 {"OldLace" , PALETTERGB (253,245,230)},
1265 {"linen" , PALETTERGB (250,240,230)},
1266 {"antique white" , PALETTERGB (250,235,215)},
1267 {"AntiqueWhite" , PALETTERGB (250,235,215)},
1268 {"papaya whip" , PALETTERGB (255,239,213)},
1269 {"PapayaWhip" , PALETTERGB (255,239,213)},
1270 {"blanched almond" , PALETTERGB (255,235,205)},
1271 {"BlanchedAlmond" , PALETTERGB (255,235,205)},
1272 {"bisque" , PALETTERGB (255,228,196)},
1273 {"peach puff" , PALETTERGB (255,218,185)},
1274 {"PeachPuff" , PALETTERGB (255,218,185)},
1275 {"navajo white" , PALETTERGB (255,222,173)},
1276 {"NavajoWhite" , PALETTERGB (255,222,173)},
1277 {"moccasin" , PALETTERGB (255,228,181)},
1278 {"cornsilk" , PALETTERGB (255,248,220)},
1279 {"ivory" , PALETTERGB (255,255,240)},
1280 {"lemon chiffon" , PALETTERGB (255,250,205)},
1281 {"LemonChiffon" , PALETTERGB (255,250,205)},
1282 {"seashell" , PALETTERGB (255,245,238)},
1283 {"honeydew" , PALETTERGB (240,255,240)},
1284 {"mint cream" , PALETTERGB (245,255,250)},
1285 {"MintCream" , PALETTERGB (245,255,250)},
1286 {"azure" , PALETTERGB (240,255,255)},
1287 {"alice blue" , PALETTERGB (240,248,255)},
1288 {"AliceBlue" , PALETTERGB (240,248,255)},
1289 {"lavender" , PALETTERGB (230,230,250)},
1290 {"lavender blush" , PALETTERGB (255,240,245)},
1291 {"LavenderBlush" , PALETTERGB (255,240,245)},
1292 {"misty rose" , PALETTERGB (255,228,225)},
1293 {"MistyRose" , PALETTERGB (255,228,225)},
1294 {"white" , PALETTERGB (255,255,255)},
1295 {"black" , PALETTERGB ( 0, 0, 0)},
1296 {"dark slate gray" , PALETTERGB ( 47, 79, 79)},
1297 {"DarkSlateGray" , PALETTERGB ( 47, 79, 79)},
1298 {"dark slate grey" , PALETTERGB ( 47, 79, 79)},
1299 {"DarkSlateGrey" , PALETTERGB ( 47, 79, 79)},
1300 {"dim gray" , PALETTERGB (105,105,105)},
1301 {"DimGray" , PALETTERGB (105,105,105)},
1302 {"dim grey" , PALETTERGB (105,105,105)},
1303 {"DimGrey" , PALETTERGB (105,105,105)},
1304 {"slate gray" , PALETTERGB (112,128,144)},
1305 {"SlateGray" , PALETTERGB (112,128,144)},
1306 {"slate grey" , PALETTERGB (112,128,144)},
1307 {"SlateGrey" , PALETTERGB (112,128,144)},
1308 {"light slate gray" , PALETTERGB (119,136,153)},
1309 {"LightSlateGray" , PALETTERGB (119,136,153)},
1310 {"light slate grey" , PALETTERGB (119,136,153)},
1311 {"LightSlateGrey" , PALETTERGB (119,136,153)},
1312 {"gray" , PALETTERGB (190,190,190)},
1313 {"grey" , PALETTERGB (190,190,190)},
1314 {"light grey" , PALETTERGB (211,211,211)},
1315 {"LightGrey" , PALETTERGB (211,211,211)},
1316 {"light gray" , PALETTERGB (211,211,211)},
1317 {"LightGray" , PALETTERGB (211,211,211)},
1318 {"midnight blue" , PALETTERGB ( 25, 25,112)},
1319 {"MidnightBlue" , PALETTERGB ( 25, 25,112)},
1320 {"navy" , PALETTERGB ( 0, 0,128)},
1321 {"navy blue" , PALETTERGB ( 0, 0,128)},
1322 {"NavyBlue" , PALETTERGB ( 0, 0,128)},
1323 {"cornflower blue" , PALETTERGB (100,149,237)},
1324 {"CornflowerBlue" , PALETTERGB (100,149,237)},
1325 {"dark slate blue" , PALETTERGB ( 72, 61,139)},
1326 {"DarkSlateBlue" , PALETTERGB ( 72, 61,139)},
1327 {"slate blue" , PALETTERGB (106, 90,205)},
1328 {"SlateBlue" , PALETTERGB (106, 90,205)},
1329 {"medium slate blue" , PALETTERGB (123,104,238)},
1330 {"MediumSlateBlue" , PALETTERGB (123,104,238)},
1331 {"light slate blue" , PALETTERGB (132,112,255)},
1332 {"LightSlateBlue" , PALETTERGB (132,112,255)},
1333 {"medium blue" , PALETTERGB ( 0, 0,205)},
1334 {"MediumBlue" , PALETTERGB ( 0, 0,205)},
1335 {"royal blue" , PALETTERGB ( 65,105,225)},
1336 {"RoyalBlue" , PALETTERGB ( 65,105,225)},
1337 {"blue" , PALETTERGB ( 0, 0,255)},
1338 {"dodger blue" , PALETTERGB ( 30,144,255)},
1339 {"DodgerBlue" , PALETTERGB ( 30,144,255)},
1340 {"deep sky blue" , PALETTERGB ( 0,191,255)},
1341 {"DeepSkyBlue" , PALETTERGB ( 0,191,255)},
1342 {"sky blue" , PALETTERGB (135,206,235)},
1343 {"SkyBlue" , PALETTERGB (135,206,235)},
1344 {"light sky blue" , PALETTERGB (135,206,250)},
1345 {"LightSkyBlue" , PALETTERGB (135,206,250)},
1346 {"steel blue" , PALETTERGB ( 70,130,180)},
1347 {"SteelBlue" , PALETTERGB ( 70,130,180)},
1348 {"light steel blue" , PALETTERGB (176,196,222)},
1349 {"LightSteelBlue" , PALETTERGB (176,196,222)},
1350 {"light blue" , PALETTERGB (173,216,230)},
1351 {"LightBlue" , PALETTERGB (173,216,230)},
1352 {"powder blue" , PALETTERGB (176,224,230)},
1353 {"PowderBlue" , PALETTERGB (176,224,230)},
1354 {"pale turquoise" , PALETTERGB (175,238,238)},
1355 {"PaleTurquoise" , PALETTERGB (175,238,238)},
1356 {"dark turquoise" , PALETTERGB ( 0,206,209)},
1357 {"DarkTurquoise" , PALETTERGB ( 0,206,209)},
1358 {"medium turquoise" , PALETTERGB ( 72,209,204)},
1359 {"MediumTurquoise" , PALETTERGB ( 72,209,204)},
1360 {"turquoise" , PALETTERGB ( 64,224,208)},
1361 {"cyan" , PALETTERGB ( 0,255,255)},
1362 {"light cyan" , PALETTERGB (224,255,255)},
1363 {"LightCyan" , PALETTERGB (224,255,255)},
1364 {"cadet blue" , PALETTERGB ( 95,158,160)},
1365 {"CadetBlue" , PALETTERGB ( 95,158,160)},
1366 {"medium aquamarine" , PALETTERGB (102,205,170)},
1367 {"MediumAquamarine" , PALETTERGB (102,205,170)},
1368 {"aquamarine" , PALETTERGB (127,255,212)},
1369 {"dark green" , PALETTERGB ( 0,100, 0)},
1370 {"DarkGreen" , PALETTERGB ( 0,100, 0)},
1371 {"dark olive green" , PALETTERGB ( 85,107, 47)},
1372 {"DarkOliveGreen" , PALETTERGB ( 85,107, 47)},
1373 {"dark sea green" , PALETTERGB (143,188,143)},
1374 {"DarkSeaGreen" , PALETTERGB (143,188,143)},
1375 {"sea green" , PALETTERGB ( 46,139, 87)},
1376 {"SeaGreen" , PALETTERGB ( 46,139, 87)},
1377 {"medium sea green" , PALETTERGB ( 60,179,113)},
1378 {"MediumSeaGreen" , PALETTERGB ( 60,179,113)},
1379 {"light sea green" , PALETTERGB ( 32,178,170)},
1380 {"LightSeaGreen" , PALETTERGB ( 32,178,170)},
1381 {"pale green" , PALETTERGB (152,251,152)},
1382 {"PaleGreen" , PALETTERGB (152,251,152)},
1383 {"spring green" , PALETTERGB ( 0,255,127)},
1384 {"SpringGreen" , PALETTERGB ( 0,255,127)},
1385 {"lawn green" , PALETTERGB (124,252, 0)},
1386 {"LawnGreen" , PALETTERGB (124,252, 0)},
1387 {"green" , PALETTERGB ( 0,255, 0)},
1388 {"chartreuse" , PALETTERGB (127,255, 0)},
1389 {"medium spring green" , PALETTERGB ( 0,250,154)},
1390 {"MediumSpringGreen" , PALETTERGB ( 0,250,154)},
1391 {"green yellow" , PALETTERGB (173,255, 47)},
1392 {"GreenYellow" , PALETTERGB (173,255, 47)},
1393 {"lime green" , PALETTERGB ( 50,205, 50)},
1394 {"LimeGreen" , PALETTERGB ( 50,205, 50)},
1395 {"yellow green" , PALETTERGB (154,205, 50)},
1396 {"YellowGreen" , PALETTERGB (154,205, 50)},
1397 {"forest green" , PALETTERGB ( 34,139, 34)},
1398 {"ForestGreen" , PALETTERGB ( 34,139, 34)},
1399 {"olive drab" , PALETTERGB (107,142, 35)},
1400 {"OliveDrab" , PALETTERGB (107,142, 35)},
1401 {"dark khaki" , PALETTERGB (189,183,107)},
1402 {"DarkKhaki" , PALETTERGB (189,183,107)},
1403 {"khaki" , PALETTERGB (240,230,140)},
1404 {"pale goldenrod" , PALETTERGB (238,232,170)},
1405 {"PaleGoldenrod" , PALETTERGB (238,232,170)},
1406 {"light goldenrod yellow" , PALETTERGB (250,250,210)},
1407 {"LightGoldenrodYellow" , PALETTERGB (250,250,210)},
1408 {"light yellow" , PALETTERGB (255,255,224)},
1409 {"LightYellow" , PALETTERGB (255,255,224)},
1410 {"yellow" , PALETTERGB (255,255, 0)},
1411 {"gold" , PALETTERGB (255,215, 0)},
1412 {"light goldenrod" , PALETTERGB (238,221,130)},
1413 {"LightGoldenrod" , PALETTERGB (238,221,130)},
1414 {"goldenrod" , PALETTERGB (218,165, 32)},
1415 {"dark goldenrod" , PALETTERGB (184,134, 11)},
1416 {"DarkGoldenrod" , PALETTERGB (184,134, 11)},
1417 {"rosy brown" , PALETTERGB (188,143,143)},
1418 {"RosyBrown" , PALETTERGB (188,143,143)},
1419 {"indian red" , PALETTERGB (205, 92, 92)},
1420 {"IndianRed" , PALETTERGB (205, 92, 92)},
1421 {"saddle brown" , PALETTERGB (139, 69, 19)},
1422 {"SaddleBrown" , PALETTERGB (139, 69, 19)},
1423 {"sienna" , PALETTERGB (160, 82, 45)},
1424 {"peru" , PALETTERGB (205,133, 63)},
1425 {"burlywood" , PALETTERGB (222,184,135)},
1426 {"beige" , PALETTERGB (245,245,220)},
1427 {"wheat" , PALETTERGB (245,222,179)},
1428 {"sandy brown" , PALETTERGB (244,164, 96)},
1429 {"SandyBrown" , PALETTERGB (244,164, 96)},
1430 {"tan" , PALETTERGB (210,180,140)},
1431 {"chocolate" , PALETTERGB (210,105, 30)},
1432 {"firebrick" , PALETTERGB (178,34, 34)},
1433 {"brown" , PALETTERGB (165,42, 42)},
1434 {"dark salmon" , PALETTERGB (233,150,122)},
1435 {"DarkSalmon" , PALETTERGB (233,150,122)},
1436 {"salmon" , PALETTERGB (250,128,114)},
1437 {"light salmon" , PALETTERGB (255,160,122)},
1438 {"LightSalmon" , PALETTERGB (255,160,122)},
1439 {"orange" , PALETTERGB (255,165, 0)},
1440 {"dark orange" , PALETTERGB (255,140, 0)},
1441 {"DarkOrange" , PALETTERGB (255,140, 0)},
1442 {"coral" , PALETTERGB (255,127, 80)},
1443 {"light coral" , PALETTERGB (240,128,128)},
1444 {"LightCoral" , PALETTERGB (240,128,128)},
1445 {"tomato" , PALETTERGB (255, 99, 71)},
1446 {"orange red" , PALETTERGB (255, 69, 0)},
1447 {"OrangeRed" , PALETTERGB (255, 69, 0)},
1448 {"red" , PALETTERGB (255, 0, 0)},
1449 {"hot pink" , PALETTERGB (255,105,180)},
1450 {"HotPink" , PALETTERGB (255,105,180)},
1451 {"deep pink" , PALETTERGB (255, 20,147)},
1452 {"DeepPink" , PALETTERGB (255, 20,147)},
1453 {"pink" , PALETTERGB (255,192,203)},
1454 {"light pink" , PALETTERGB (255,182,193)},
1455 {"LightPink" , PALETTERGB (255,182,193)},
1456 {"pale violet red" , PALETTERGB (219,112,147)},
1457 {"PaleVioletRed" , PALETTERGB (219,112,147)},
1458 {"maroon" , PALETTERGB (176, 48, 96)},
1459 {"medium violet red" , PALETTERGB (199, 21,133)},
1460 {"MediumVioletRed" , PALETTERGB (199, 21,133)},
1461 {"violet red" , PALETTERGB (208, 32,144)},
1462 {"VioletRed" , PALETTERGB (208, 32,144)},
1463 {"magenta" , PALETTERGB (255, 0,255)},
1464 {"violet" , PALETTERGB (238,130,238)},
1465 {"plum" , PALETTERGB (221,160,221)},
1466 {"orchid" , PALETTERGB (218,112,214)},
1467 {"medium orchid" , PALETTERGB (186, 85,211)},
1468 {"MediumOrchid" , PALETTERGB (186, 85,211)},
1469 {"dark orchid" , PALETTERGB (153, 50,204)},
1470 {"DarkOrchid" , PALETTERGB (153, 50,204)},
1471 {"dark violet" , PALETTERGB (148, 0,211)},
1472 {"DarkViolet" , PALETTERGB (148, 0,211)},
1473 {"blue violet" , PALETTERGB (138, 43,226)},
1474 {"BlueViolet" , PALETTERGB (138, 43,226)},
1475 {"purple" , PALETTERGB (160, 32,240)},
1476 {"medium purple" , PALETTERGB (147,112,219)},
1477 {"MediumPurple" , PALETTERGB (147,112,219)},
1478 {"thistle" , PALETTERGB (216,191,216)},
1479 {"gray0" , PALETTERGB ( 0, 0, 0)},
1480 {"grey0" , PALETTERGB ( 0, 0, 0)},
1481 {"dark grey" , PALETTERGB (169,169,169)},
1482 {"DarkGrey" , PALETTERGB (169,169,169)},
1483 {"dark gray" , PALETTERGB (169,169,169)},
1484 {"DarkGray" , PALETTERGB (169,169,169)},
1485 {"dark blue" , PALETTERGB ( 0, 0,139)},
1486 {"DarkBlue" , PALETTERGB ( 0, 0,139)},
1487 {"dark cyan" , PALETTERGB ( 0,139,139)},
1488 {"DarkCyan" , PALETTERGB ( 0,139,139)},
1489 {"dark magenta" , PALETTERGB (139, 0,139)},
1490 {"DarkMagenta" , PALETTERGB (139, 0,139)},
1491 {"dark red" , PALETTERGB (139, 0, 0)},
1492 {"DarkRed" , PALETTERGB (139, 0, 0)},
1493 {"light green" , PALETTERGB (144,238,144)},
1494 {"LightGreen" , PALETTERGB (144,238,144)},
ee78dc32
GV
1495};
1496
fbd6baed 1497DEFUN ("w32-default-color-map", Fw32_default_color_map, Sw32_default_color_map,
74e1aeec 1498 0, 0, 0, doc: /* Return the default color map. */)
ee78dc32
GV
1499 ()
1500{
1501 int i;
fbd6baed 1502 colormap_t *pc = w32_color_map;
ee78dc32
GV
1503 Lisp_Object cmap;
1504
1505 BLOCK_INPUT;
1506
1507 cmap = Qnil;
1508
fbd6baed 1509 for (i = 0; i < sizeof (w32_color_map) / sizeof (w32_color_map[0]);
ee78dc32
GV
1510 pc++, i++)
1511 cmap = Fcons (Fcons (build_string (pc->name),
1512 make_number (pc->colorref)),
1513 cmap);
1514
1515 UNBLOCK_INPUT;
1516
1517 return (cmap);
1518}
ee78dc32
GV
1519
1520Lisp_Object
fbd6baed 1521w32_to_x_color (rgb)
ee78dc32
GV
1522 Lisp_Object rgb;
1523{
1524 Lisp_Object color;
1525
b7826503 1526 CHECK_NUMBER (rgb);
ee78dc32
GV
1527
1528 BLOCK_INPUT;
1529
fbd6baed 1530 color = Frassq (rgb, Vw32_color_map);
ee78dc32
GV
1531
1532 UNBLOCK_INPUT;
1533
1534 if (!NILP (color))
1535 return (Fcar (color));
1536 else
1537 return Qnil;
1538}
1539
5d7fed93
GV
1540COLORREF
1541w32_color_map_lookup (colorname)
1542 char *colorname;
1543{
1544 Lisp_Object tail, ret = Qnil;
1545
1546 BLOCK_INPUT;
1547
1548 for (tail = Vw32_color_map; !NILP (tail); tail = Fcdr (tail))
1549 {
1550 register Lisp_Object elt, tem;
1551
1552 elt = Fcar (tail);
1553 if (!CONSP (elt)) continue;
1554
1555 tem = Fcar (elt);
1556
1557 if (lstrcmpi (XSTRING (tem)->data, colorname) == 0)
1558 {
1559 ret = XUINT (Fcdr (elt));
1560 break;
1561 }
1562
1563 QUIT;
1564 }
1565
1566
1567 UNBLOCK_INPUT;
1568
1569 return ret;
1570}
1571
ee78dc32 1572COLORREF
fbd6baed 1573x_to_w32_color (colorname)
ee78dc32
GV
1574 char * colorname;
1575{
8edb0a6f
JR
1576 register Lisp_Object ret = Qnil;
1577
ee78dc32 1578 BLOCK_INPUT;
1edf84e7
GV
1579
1580 if (colorname[0] == '#')
1581 {
1582 /* Could be an old-style RGB Device specification. */
1583 char *color;
1584 int size;
1585 color = colorname + 1;
1586
1587 size = strlen(color);
1588 if (size == 3 || size == 6 || size == 9 || size == 12)
1589 {
1590 UINT colorval;
1591 int i, pos;
1592 pos = 0;
1593 size /= 3;
1594 colorval = 0;
1595
1596 for (i = 0; i < 3; i++)
1597 {
1598 char *end;
1599 char t;
1600 unsigned long value;
1601
1602 /* The check for 'x' in the following conditional takes into
1603 account the fact that strtol allows a "0x" in front of
1604 our numbers, and we don't. */
1605 if (!isxdigit(color[0]) || color[1] == 'x')
1606 break;
1607 t = color[size];
1608 color[size] = '\0';
1609 value = strtoul(color, &end, 16);
1610 color[size] = t;
1611 if (errno == ERANGE || end - color != size)
1612 break;
1613 switch (size)
1614 {
1615 case 1:
1616 value = value * 0x10;
1617 break;
1618 case 2:
1619 break;
1620 case 3:
1621 value /= 0x10;
1622 break;
1623 case 4:
1624 value /= 0x100;
1625 break;
1626 }
1627 colorval |= (value << pos);
1628 pos += 0x8;
1629 if (i == 2)
1630 {
1631 UNBLOCK_INPUT;
1632 return (colorval);
1633 }
1634 color = end;
1635 }
1636 }
1637 }
1638 else if (strnicmp(colorname, "rgb:", 4) == 0)
1639 {
1640 char *color;
1641 UINT colorval;
1642 int i, pos;
1643 pos = 0;
1644
1645 colorval = 0;
1646 color = colorname + 4;
1647 for (i = 0; i < 3; i++)
1648 {
1649 char *end;
1650 unsigned long value;
1651
1652 /* The check for 'x' in the following conditional takes into
1653 account the fact that strtol allows a "0x" in front of
1654 our numbers, and we don't. */
1655 if (!isxdigit(color[0]) || color[1] == 'x')
1656 break;
1657 value = strtoul(color, &end, 16);
1658 if (errno == ERANGE)
1659 break;
1660 switch (end - color)
1661 {
1662 case 1:
1663 value = value * 0x10 + value;
1664 break;
1665 case 2:
1666 break;
1667 case 3:
1668 value /= 0x10;
1669 break;
1670 case 4:
1671 value /= 0x100;
1672 break;
1673 default:
1674 value = ULONG_MAX;
1675 }
1676 if (value == ULONG_MAX)
1677 break;
1678 colorval |= (value << pos);
1679 pos += 0x8;
1680 if (i == 2)
1681 {
1682 if (*end != '\0')
1683 break;
1684 UNBLOCK_INPUT;
1685 return (colorval);
1686 }
1687 if (*end != '/')
1688 break;
1689 color = end + 1;
1690 }
1691 }
1692 else if (strnicmp(colorname, "rgbi:", 5) == 0)
1693 {
1694 /* This is an RGB Intensity specification. */
1695 char *color;
1696 UINT colorval;
1697 int i, pos;
1698 pos = 0;
1699
1700 colorval = 0;
1701 color = colorname + 5;
1702 for (i = 0; i < 3; i++)
1703 {
1704 char *end;
1705 double value;
1706 UINT val;
1707
1708 value = strtod(color, &end);
1709 if (errno == ERANGE)
1710 break;
1711 if (value < 0.0 || value > 1.0)
1712 break;
1713 val = (UINT)(0x100 * value);
1714 /* We used 0x100 instead of 0xFF to give an continuous
1715 range between 0.0 and 1.0 inclusive. The next statement
1716 fixes the 1.0 case. */
1717 if (val == 0x100)
1718 val = 0xFF;
1719 colorval |= (val << pos);
1720 pos += 0x8;
1721 if (i == 2)
1722 {
1723 if (*end != '\0')
1724 break;
1725 UNBLOCK_INPUT;
1726 return (colorval);
1727 }
1728 if (*end != '/')
1729 break;
1730 color = end + 1;
1731 }
1732 }
1733 /* I am not going to attempt to handle any of the CIE color schemes
1734 or TekHVC, since I don't know the algorithms for conversion to
1735 RGB. */
f695b4b1
GV
1736
1737 /* If we fail to lookup the color name in w32_color_map, then check the
1738 colorname to see if it can be crudely approximated: If the X color
1739 ends in a number (e.g., "darkseagreen2"), strip the number and
1740 return the result of looking up the base color name. */
1741 ret = w32_color_map_lookup (colorname);
1742 if (NILP (ret))
ee78dc32 1743 {
f695b4b1 1744 int len = strlen (colorname);
ee78dc32 1745
f695b4b1
GV
1746 if (isdigit (colorname[len - 1]))
1747 {
8b77111c 1748 char *ptr, *approx = alloca (len + 1);
ee78dc32 1749
f695b4b1
GV
1750 strcpy (approx, colorname);
1751 ptr = &approx[len - 1];
1752 while (ptr > approx && isdigit (*ptr))
1753 *ptr-- = '\0';
ee78dc32 1754
f695b4b1 1755 ret = w32_color_map_lookup (approx);
ee78dc32 1756 }
ee78dc32
GV
1757 }
1758
1759 UNBLOCK_INPUT;
ee78dc32
GV
1760 return ret;
1761}
1762
5ac45f98
GV
1763
1764void
fbd6baed 1765w32_regenerate_palette (FRAME_PTR f)
5ac45f98 1766{
fbd6baed 1767 struct w32_palette_entry * list;
5ac45f98
GV
1768 LOGPALETTE * log_palette;
1769 HPALETTE new_palette;
1770 int i;
1771
1772 /* don't bother trying to create palette if not supported */
fbd6baed 1773 if (! FRAME_W32_DISPLAY_INFO (f)->has_palette)
5ac45f98
GV
1774 return;
1775
1776 log_palette = (LOGPALETTE *)
1777 alloca (sizeof (LOGPALETTE) +
fbd6baed 1778 FRAME_W32_DISPLAY_INFO (f)->num_colors * sizeof (PALETTEENTRY));
5ac45f98 1779 log_palette->palVersion = 0x300;
fbd6baed 1780 log_palette->palNumEntries = FRAME_W32_DISPLAY_INFO (f)->num_colors;
5ac45f98 1781
fbd6baed 1782 list = FRAME_W32_DISPLAY_INFO (f)->color_list;
5ac45f98 1783 for (i = 0;
fbd6baed 1784 i < FRAME_W32_DISPLAY_INFO (f)->num_colors;
5ac45f98
GV
1785 i++, list = list->next)
1786 log_palette->palPalEntry[i] = list->entry;
1787
1788 new_palette = CreatePalette (log_palette);
1789
1790 enter_crit ();
1791
fbd6baed
GV
1792 if (FRAME_W32_DISPLAY_INFO (f)->palette)
1793 DeleteObject (FRAME_W32_DISPLAY_INFO (f)->palette);
1794 FRAME_W32_DISPLAY_INFO (f)->palette = new_palette;
5ac45f98
GV
1795
1796 /* Realize display palette and garbage all frames. */
1797 release_frame_dc (f, get_frame_dc (f));
1798
1799 leave_crit ();
1800}
1801
fbd6baed
GV
1802#define W32_COLOR(pe) RGB (pe.peRed, pe.peGreen, pe.peBlue)
1803#define SET_W32_COLOR(pe, color) \
5ac45f98
GV
1804 do \
1805 { \
1806 pe.peRed = GetRValue (color); \
1807 pe.peGreen = GetGValue (color); \
1808 pe.peBlue = GetBValue (color); \
1809 pe.peFlags = 0; \
1810 } while (0)
1811
1812#if 0
1813/* Keep these around in case we ever want to track color usage. */
1814void
fbd6baed 1815w32_map_color (FRAME_PTR f, COLORREF color)
5ac45f98 1816{
fbd6baed 1817 struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
5ac45f98 1818
fbd6baed 1819 if (NILP (Vw32_enable_palette))
5ac45f98
GV
1820 return;
1821
1822 /* check if color is already mapped */
1823 while (list)
1824 {
fbd6baed 1825 if (W32_COLOR (list->entry) == color)
5ac45f98
GV
1826 {
1827 ++list->refcount;
1828 return;
1829 }
1830 list = list->next;
1831 }
1832
1833 /* not already mapped, so add to list and recreate Windows palette */
fbd6baed
GV
1834 list = (struct w32_palette_entry *)
1835 xmalloc (sizeof (struct w32_palette_entry));
1836 SET_W32_COLOR (list->entry, color);
5ac45f98 1837 list->refcount = 1;
fbd6baed
GV
1838 list->next = FRAME_W32_DISPLAY_INFO (f)->color_list;
1839 FRAME_W32_DISPLAY_INFO (f)->color_list = list;
1840 FRAME_W32_DISPLAY_INFO (f)->num_colors++;
5ac45f98
GV
1841
1842 /* set flag that palette must be regenerated */
fbd6baed 1843 FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
5ac45f98
GV
1844}
1845
1846void
fbd6baed 1847w32_unmap_color (FRAME_PTR f, COLORREF color)
5ac45f98 1848{
fbd6baed
GV
1849 struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
1850 struct w32_palette_entry **prev = &FRAME_W32_DISPLAY_INFO (f)->color_list;
5ac45f98 1851
fbd6baed 1852 if (NILP (Vw32_enable_palette))
5ac45f98
GV
1853 return;
1854
1855 /* check if color is already mapped */
1856 while (list)
1857 {
fbd6baed 1858 if (W32_COLOR (list->entry) == color)
5ac45f98
GV
1859 {
1860 if (--list->refcount == 0)
1861 {
1862 *prev = list->next;
1863 xfree (list);
fbd6baed 1864 FRAME_W32_DISPLAY_INFO (f)->num_colors--;
5ac45f98
GV
1865 break;
1866 }
1867 else
1868 return;
1869 }
1870 prev = &list->next;
1871 list = list->next;
1872 }
1873
1874 /* set flag that palette must be regenerated */
fbd6baed 1875 FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
5ac45f98
GV
1876}
1877#endif
1878
6fc2811b
JR
1879
1880/* Gamma-correct COLOR on frame F. */
1881
1882void
1883gamma_correct (f, color)
1884 struct frame *f;
1885 COLORREF *color;
1886{
1887 if (f->gamma)
1888 {
1889 *color = PALETTERGB (
1890 pow (GetRValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
1891 pow (GetGValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
1892 pow (GetBValue (*color) / 255.0, f->gamma) * 255.0 + 0.5);
1893 }
1894}
1895
1896
ee78dc32
GV
1897/* Decide if color named COLOR is valid for the display associated with
1898 the selected frame; if so, return the rgb values in COLOR_DEF.
1899 If ALLOC is nonzero, allocate a new colormap cell. */
1900
1901int
6fc2811b 1902w32_defined_color (f, color, color_def, alloc)
ee78dc32
GV
1903 FRAME_PTR f;
1904 char *color;
6fc2811b 1905 XColor *color_def;
ee78dc32
GV
1906 int alloc;
1907{
1908 register Lisp_Object tem;
6fc2811b 1909 COLORREF w32_color_ref;
3c190163 1910
fbd6baed 1911 tem = x_to_w32_color (color);
3c190163 1912
ee78dc32
GV
1913 if (!NILP (tem))
1914 {
d88c567c
JR
1915 if (f)
1916 {
1917 /* Apply gamma correction. */
1918 w32_color_ref = XUINT (tem);
1919 gamma_correct (f, &w32_color_ref);
1920 XSETINT (tem, w32_color_ref);
1921 }
9badad41
JR
1922
1923 /* Map this color to the palette if it is enabled. */
fbd6baed 1924 if (!NILP (Vw32_enable_palette))
5ac45f98 1925 {
fbd6baed 1926 struct w32_palette_entry * entry =
d88c567c 1927 one_w32_display_info.color_list;
fbd6baed 1928 struct w32_palette_entry ** prev =
d88c567c 1929 &one_w32_display_info.color_list;
5ac45f98
GV
1930
1931 /* check if color is already mapped */
1932 while (entry)
1933 {
fbd6baed 1934 if (W32_COLOR (entry->entry) == XUINT (tem))
5ac45f98
GV
1935 break;
1936 prev = &entry->next;
1937 entry = entry->next;
1938 }
1939
1940 if (entry == NULL && alloc)
1941 {
1942 /* not already mapped, so add to list */
fbd6baed
GV
1943 entry = (struct w32_palette_entry *)
1944 xmalloc (sizeof (struct w32_palette_entry));
1945 SET_W32_COLOR (entry->entry, XUINT (tem));
5ac45f98
GV
1946 entry->next = NULL;
1947 *prev = entry;
d88c567c 1948 one_w32_display_info.num_colors++;
5ac45f98
GV
1949
1950 /* set flag that palette must be regenerated */
d88c567c 1951 one_w32_display_info.regen_palette = TRUE;
5ac45f98
GV
1952 }
1953 }
1954 /* Ensure COLORREF value is snapped to nearest color in (default)
1955 palette by simulating the PALETTERGB macro. This works whether
1956 or not the display device has a palette. */
6fc2811b
JR
1957 w32_color_ref = XUINT (tem) | 0x2000000;
1958
6fc2811b
JR
1959 color_def->pixel = w32_color_ref;
1960 color_def->red = GetRValue (w32_color_ref);
1961 color_def->green = GetGValue (w32_color_ref);
1962 color_def->blue = GetBValue (w32_color_ref);
1963
ee78dc32 1964 return 1;
5ac45f98 1965 }
7fb46567 1966 else
3c190163
GV
1967 {
1968 return 0;
1969 }
ee78dc32
GV
1970}
1971
1972/* Given a string ARG naming a color, compute a pixel value from it
1973 suitable for screen F.
1974 If F is not a color screen, return DEF (default) regardless of what
1975 ARG says. */
1976
1977int
1978x_decode_color (f, arg, def)
1979 FRAME_PTR f;
1980 Lisp_Object arg;
1981 int def;
1982{
6fc2811b 1983 XColor cdef;
ee78dc32 1984
b7826503 1985 CHECK_STRING (arg);
ee78dc32
GV
1986
1987 if (strcmp (XSTRING (arg)->data, "black") == 0)
1988 return BLACK_PIX_DEFAULT (f);
1989 else if (strcmp (XSTRING (arg)->data, "white") == 0)
1990 return WHITE_PIX_DEFAULT (f);
1991
fbd6baed 1992 if ((FRAME_W32_DISPLAY_INFO (f)->n_planes * FRAME_W32_DISPLAY_INFO (f)->n_cbits) == 1)
ee78dc32
GV
1993 return def;
1994
6fc2811b 1995 /* w32_defined_color is responsible for coping with failures
ee78dc32 1996 by looking for a near-miss. */
6fc2811b
JR
1997 if (w32_defined_color (f, XSTRING (arg)->data, &cdef, 1))
1998 return cdef.pixel;
ee78dc32
GV
1999
2000 /* defined_color failed; return an ultimate default. */
2001 return def;
2002}
2003\f
dfff8a69
JR
2004/* Change the `line-spacing' frame parameter of frame F. OLD_VALUE is
2005 the previous value of that parameter, NEW_VALUE is the new value. */
2006
2007static void
2008x_set_line_spacing (f, new_value, old_value)
2009 struct frame *f;
2010 Lisp_Object new_value, old_value;
2011{
2012 if (NILP (new_value))
2013 f->extra_line_spacing = 0;
2014 else if (NATNUMP (new_value))
2015 f->extra_line_spacing = XFASTINT (new_value);
2016 else
1a948b17 2017 Fsignal (Qerror, Fcons (build_string ("Invalid line-spacing"),
dfff8a69
JR
2018 Fcons (new_value, Qnil)));
2019 if (FRAME_VISIBLE_P (f))
2020 redraw_frame (f);
2021}
2022
2023
f7b9d4d1
JR
2024/* Change the `fullscreen' frame parameter of frame F. OLD_VALUE is
2025 the previous value of that parameter, NEW_VALUE is the new value. */
2026
2027static void
2028x_set_fullscreen (f, new_value, old_value)
2029 struct frame *f;
2030 Lisp_Object new_value, old_value;
2031{
2032 if (NILP (new_value))
2033 f->output_data.w32->want_fullscreen = FULLSCREEN_NONE;
2034 else if (EQ (new_value, Qfullboth))
2035 f->output_data.w32->want_fullscreen = FULLSCREEN_BOTH;
2036 else if (EQ (new_value, Qfullwidth))
2037 f->output_data.w32->want_fullscreen = FULLSCREEN_WIDTH;
2038 else if (EQ (new_value, Qfullheight))
2039 f->output_data.w32->want_fullscreen = FULLSCREEN_HEIGHT;
2040}
2041
2042
6fc2811b
JR
2043/* Change the `screen-gamma' frame parameter of frame F. OLD_VALUE is
2044 the previous value of that parameter, NEW_VALUE is the new value. */
2045
2046static void
2047x_set_screen_gamma (f, new_value, old_value)
2048 struct frame *f;
2049 Lisp_Object new_value, old_value;
2050{
2051 if (NILP (new_value))
2052 f->gamma = 0;
2053 else if (NUMBERP (new_value) && XFLOATINT (new_value) > 0)
2054 /* The value 0.4545 is the normal viewing gamma. */
2055 f->gamma = 1.0 / (0.4545 * XFLOATINT (new_value));
2056 else
1a948b17 2057 Fsignal (Qerror, Fcons (build_string ("Invalid screen-gamma"),
6fc2811b
JR
2058 Fcons (new_value, Qnil)));
2059
2060 clear_face_cache (0);
2061}
2062
2063
ee78dc32
GV
2064/* Functions called only from `x_set_frame_param'
2065 to set individual parameters.
2066
fbd6baed 2067 If FRAME_W32_WINDOW (f) is 0,
ee78dc32
GV
2068 the frame is being created and its window does not exist yet.
2069 In that case, just record the parameter's new value
2070 in the standard place; do not attempt to change the window. */
2071
2072void
2073x_set_foreground_color (f, arg, oldval)
2074 struct frame *f;
2075 Lisp_Object arg, oldval;
2076{
3cf3436e
JR
2077 struct w32_output *x = f->output_data.w32;
2078 PIX_TYPE fg, old_fg;
2079
2080 fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
2081 old_fg = FRAME_FOREGROUND_PIXEL (f);
2082 FRAME_FOREGROUND_PIXEL (f) = fg;
5ac45f98 2083
fbd6baed 2084 if (FRAME_W32_WINDOW (f) != 0)
ee78dc32 2085 {
3cf3436e
JR
2086 if (x->cursor_pixel == old_fg)
2087 x->cursor_pixel = fg;
2088
6fc2811b 2089 update_face_from_frame_parameter (f, Qforeground_color, arg);
ee78dc32
GV
2090 if (FRAME_VISIBLE_P (f))
2091 redraw_frame (f);
2092 }
2093}
2094
2095void
2096x_set_background_color (f, arg, oldval)
2097 struct frame *f;
2098 Lisp_Object arg, oldval;
2099{
6fc2811b 2100 FRAME_BACKGROUND_PIXEL (f)
ee78dc32
GV
2101 = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
2102
fbd6baed 2103 if (FRAME_W32_WINDOW (f) != 0)
ee78dc32 2104 {
6fc2811b
JR
2105 SetWindowLong (FRAME_W32_WINDOW (f), WND_BACKGROUND_INDEX,
2106 FRAME_BACKGROUND_PIXEL (f));
ee78dc32 2107
6fc2811b 2108 update_face_from_frame_parameter (f, Qbackground_color, arg);
ee78dc32
GV
2109
2110 if (FRAME_VISIBLE_P (f))
2111 redraw_frame (f);
2112 }
2113}
2114
2115void
2116x_set_mouse_color (f, arg, oldval)
2117 struct frame *f;
2118 Lisp_Object arg, oldval;
2119{
ee78dc32 2120 Cursor cursor, nontext_cursor, mode_cursor, cross_cursor;
dfc465d3 2121 int count;
ee78dc32
GV
2122 int mask_color;
2123
2124 if (!EQ (Qnil, arg))
fbd6baed 2125 f->output_data.w32->mouse_pixel
ee78dc32 2126 = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
6fc2811b
JR
2127 mask_color = FRAME_BACKGROUND_PIXEL (f);
2128
2129 /* Don't let pointers be invisible. */
fbd6baed 2130 if (mask_color == f->output_data.w32->mouse_pixel
6fc2811b
JR
2131 && mask_color == FRAME_BACKGROUND_PIXEL (f))
2132 f->output_data.w32->mouse_pixel = FRAME_FOREGROUND_PIXEL (f);
ee78dc32 2133
767b1ff0 2134#if 0 /* TODO : cursor changes */
ee78dc32
GV
2135 BLOCK_INPUT;
2136
2137 /* It's not okay to crash if the user selects a screwy cursor. */
fadca6c6 2138 count = x_catch_errors (FRAME_W32_DISPLAY (f));
ee78dc32
GV
2139
2140 if (!EQ (Qnil, Vx_pointer_shape))
2141 {
b7826503 2142 CHECK_NUMBER (Vx_pointer_shape);
fbd6baed 2143 cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XINT (Vx_pointer_shape));
ee78dc32
GV
2144 }
2145 else
fbd6baed
GV
2146 cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
2147 x_check_errors (FRAME_W32_DISPLAY (f), "bad text pointer cursor: %s");
ee78dc32
GV
2148
2149 if (!EQ (Qnil, Vx_nontext_pointer_shape))
2150 {
b7826503 2151 CHECK_NUMBER (Vx_nontext_pointer_shape);
fbd6baed 2152 nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
ee78dc32
GV
2153 XINT (Vx_nontext_pointer_shape));
2154 }
2155 else
fbd6baed
GV
2156 nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_left_ptr);
2157 x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
ee78dc32 2158
0af913d7 2159 if (!EQ (Qnil, Vx_hourglass_pointer_shape))
6fc2811b 2160 {
b7826503 2161 CHECK_NUMBER (Vx_hourglass_pointer_shape);
0af913d7
GM
2162 hourglass_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
2163 XINT (Vx_hourglass_pointer_shape));
6fc2811b
JR
2164 }
2165 else
0af913d7 2166 hourglass_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_watch);
6fc2811b
JR
2167 x_check_errors (FRAME_W32_DISPLAY (f), "bad busy pointer cursor: %s");
2168
2169 x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
ee78dc32
GV
2170 if (!EQ (Qnil, Vx_mode_pointer_shape))
2171 {
b7826503 2172 CHECK_NUMBER (Vx_mode_pointer_shape);
fbd6baed 2173 mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
ee78dc32
GV
2174 XINT (Vx_mode_pointer_shape));
2175 }
2176 else
fbd6baed
GV
2177 mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
2178 x_check_errors (FRAME_W32_DISPLAY (f), "bad modeline pointer cursor: %s");
ee78dc32
GV
2179
2180 if (!EQ (Qnil, Vx_sensitive_text_pointer_shape))
2181 {
b7826503 2182 CHECK_NUMBER (Vx_sensitive_text_pointer_shape);
ee78dc32 2183 cross_cursor
fbd6baed 2184 = XCreateFontCursor (FRAME_W32_DISPLAY (f),
ee78dc32
GV
2185 XINT (Vx_sensitive_text_pointer_shape));
2186 }
2187 else
fbd6baed 2188 cross_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_crosshair);
ee78dc32 2189
4694d762
JR
2190 if (!NILP (Vx_window_horizontal_drag_shape))
2191 {
b7826503 2192 CHECK_NUMBER (Vx_window_horizontal_drag_shape);
4694d762
JR
2193 horizontal_drag_cursor
2194 = XCreateFontCursor (FRAME_X_DISPLAY (f),
2195 XINT (Vx_window_horizontal_drag_shape));
2196 }
2197 else
2198 horizontal_drag_cursor
2199 = XCreateFontCursor (FRAME_X_DISPLAY (f), XC_sb_h_double_arrow);
2200
ee78dc32 2201 /* Check and report errors with the above calls. */
fbd6baed 2202 x_check_errors (FRAME_W32_DISPLAY (f), "can't set cursor shape: %s");
fadca6c6 2203 x_uncatch_errors (FRAME_W32_DISPLAY (f), count);
ee78dc32
GV
2204
2205 {
2206 XColor fore_color, back_color;
2207
fbd6baed 2208 fore_color.pixel = f->output_data.w32->mouse_pixel;
ee78dc32 2209 back_color.pixel = mask_color;
fbd6baed
GV
2210 XQueryColor (FRAME_W32_DISPLAY (f),
2211 DefaultColormap (FRAME_W32_DISPLAY (f),
2212 DefaultScreen (FRAME_W32_DISPLAY (f))),
ee78dc32 2213 &fore_color);
fbd6baed
GV
2214 XQueryColor (FRAME_W32_DISPLAY (f),
2215 DefaultColormap (FRAME_W32_DISPLAY (f),
2216 DefaultScreen (FRAME_W32_DISPLAY (f))),
ee78dc32 2217 &back_color);
fbd6baed 2218 XRecolorCursor (FRAME_W32_DISPLAY (f), cursor,
ee78dc32 2219 &fore_color, &back_color);
fbd6baed 2220 XRecolorCursor (FRAME_W32_DISPLAY (f), nontext_cursor,
ee78dc32 2221 &fore_color, &back_color);
fbd6baed 2222 XRecolorCursor (FRAME_W32_DISPLAY (f), mode_cursor,
ee78dc32 2223 &fore_color, &back_color);
fbd6baed 2224 XRecolorCursor (FRAME_W32_DISPLAY (f), cross_cursor,
ee78dc32 2225 &fore_color, &back_color);
0af913d7 2226 XRecolorCursor (FRAME_W32_DISPLAY (f), hourglass_cursor,
6fc2811b 2227 &fore_color, &back_color);
ee78dc32
GV
2228 }
2229
fbd6baed 2230 if (FRAME_W32_WINDOW (f) != 0)
6fc2811b 2231 XDefineCursor (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), cursor);
ee78dc32 2232
fbd6baed
GV
2233 if (cursor != f->output_data.w32->text_cursor && f->output_data.w32->text_cursor != 0)
2234 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->text_cursor);
2235 f->output_data.w32->text_cursor = cursor;
2236
2237 if (nontext_cursor != f->output_data.w32->nontext_cursor
2238 && f->output_data.w32->nontext_cursor != 0)
2239 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->nontext_cursor);
2240 f->output_data.w32->nontext_cursor = nontext_cursor;
2241
0af913d7
GM
2242 if (hourglass_cursor != f->output_data.w32->hourglass_cursor
2243 && f->output_data.w32->hourglass_cursor != 0)
2244 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->hourglass_cursor);
2245 f->output_data.w32->hourglass_cursor = hourglass_cursor;
6fc2811b 2246
fbd6baed
GV
2247 if (mode_cursor != f->output_data.w32->modeline_cursor
2248 && f->output_data.w32->modeline_cursor != 0)
2249 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->modeline_cursor);
2250 f->output_data.w32->modeline_cursor = mode_cursor;
6fc2811b 2251
fbd6baed
GV
2252 if (cross_cursor != f->output_data.w32->cross_cursor
2253 && f->output_data.w32->cross_cursor != 0)
2254 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->cross_cursor);
2255 f->output_data.w32->cross_cursor = cross_cursor;
2256
2257 XFlush (FRAME_W32_DISPLAY (f));
ee78dc32 2258 UNBLOCK_INPUT;
6fc2811b
JR
2259
2260 update_face_from_frame_parameter (f, Qmouse_color, arg);
767b1ff0 2261#endif /* TODO */
ee78dc32
GV
2262}
2263
70a0239a
JR
2264/* Defined in w32term.c. */
2265void x_update_cursor (struct frame *f, int on_p);
2266
ee78dc32
GV
2267void
2268x_set_cursor_color (f, arg, oldval)
2269 struct frame *f;
2270 Lisp_Object arg, oldval;
2271{
70a0239a 2272 unsigned long fore_pixel, pixel;
ee78dc32 2273
dfff8a69 2274 if (!NILP (Vx_cursor_fore_pixel))
ee78dc32 2275 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
70a0239a 2276 WHITE_PIX_DEFAULT (f));
ee78dc32 2277 else
6fc2811b 2278 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
70a0239a 2279
6759f872 2280 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
ee78dc32
GV
2281
2282 /* Make sure that the cursor color differs from the background color. */
70a0239a 2283 if (pixel == FRAME_BACKGROUND_PIXEL (f))
ee78dc32 2284 {
70a0239a
JR
2285 pixel = f->output_data.w32->mouse_pixel;
2286 if (pixel == fore_pixel)
6fc2811b 2287 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
ee78dc32 2288 }
70a0239a 2289
ac849ba4 2290 f->output_data.w32->cursor_foreground_pixel = fore_pixel;
70a0239a 2291 f->output_data.w32->cursor_pixel = pixel;
ee78dc32 2292
fbd6baed 2293 if (FRAME_W32_WINDOW (f) != 0)
ee78dc32
GV
2294 {
2295 if (FRAME_VISIBLE_P (f))
2296 {
70a0239a
JR
2297 x_update_cursor (f, 0);
2298 x_update_cursor (f, 1);
ee78dc32
GV
2299 }
2300 }
6fc2811b
JR
2301
2302 update_face_from_frame_parameter (f, Qcursor_color, arg);
ee78dc32
GV
2303}
2304
33d52f9c
GV
2305/* Set the border-color of frame F to pixel value PIX.
2306 Note that this does not fully take effect if done before
2307 F has an window. */
2308void
2309x_set_border_pixel (f, pix)
2310 struct frame *f;
2311 int pix;
2312{
2313 f->output_data.w32->border_pixel = pix;
2314
2315 if (FRAME_W32_WINDOW (f) != 0 && f->output_data.w32->border_width > 0)
2316 {
2317 if (FRAME_VISIBLE_P (f))
2318 redraw_frame (f);
2319 }
2320}
2321
ee78dc32
GV
2322/* Set the border-color of frame F to value described by ARG.
2323 ARG can be a string naming a color.
2324 The border-color is used for the border that is drawn by the server.
2325 Note that this does not fully take effect if done before
2326 F has a window; it must be redone when the window is created. */
2327
2328void
2329x_set_border_color (f, arg, oldval)
2330 struct frame *f;
2331 Lisp_Object arg, oldval;
2332{
ee78dc32
GV
2333 int pix;
2334
b7826503 2335 CHECK_STRING (arg);
ee78dc32 2336 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
ee78dc32 2337 x_set_border_pixel (f, pix);
6fc2811b 2338 update_face_from_frame_parameter (f, Qborder_color, arg);
ee78dc32
GV
2339}
2340
dfff8a69
JR
2341/* Value is the internal representation of the specified cursor type
2342 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
2343 of the bar cursor. */
2344
2345enum text_cursor_kinds
2346x_specified_cursor_type (arg, width)
2347 Lisp_Object arg;
2348 int *width;
ee78dc32 2349{
dfff8a69
JR
2350 enum text_cursor_kinds type;
2351
ee78dc32
GV
2352 if (EQ (arg, Qbar))
2353 {
dfff8a69
JR
2354 type = BAR_CURSOR;
2355 *width = 2;
ee78dc32 2356 }
dfff8a69
JR
2357 else if (CONSP (arg)
2358 && EQ (XCAR (arg), Qbar)
2359 && INTEGERP (XCDR (arg))
2360 && XINT (XCDR (arg)) >= 0)
ee78dc32 2361 {
dfff8a69
JR
2362 type = BAR_CURSOR;
2363 *width = XINT (XCDR (arg));
ee78dc32 2364 }
dfff8a69
JR
2365 else if (NILP (arg))
2366 type = NO_CURSOR;
ee78dc32
GV
2367 else
2368 /* Treat anything unknown as "box cursor".
2369 It was bad to signal an error; people have trouble fixing
2370 .Xdefaults with Emacs, when it has something bad in it. */
dfff8a69
JR
2371 type = FILLED_BOX_CURSOR;
2372
2373 return type;
2374}
2375
2376void
2377x_set_cursor_type (f, arg, oldval)
2378 FRAME_PTR f;
2379 Lisp_Object arg, oldval;
2380{
2381 int width;
2382
2383 FRAME_DESIRED_CURSOR (f) = x_specified_cursor_type (arg, &width);
2384 f->output_data.w32->cursor_width = width;
ee78dc32
GV
2385
2386 /* Make sure the cursor gets redrawn. This is overkill, but how
2387 often do people change cursor types? */
2388 update_mode_lines++;
2389}
dfff8a69 2390\f
ee78dc32
GV
2391void
2392x_set_icon_type (f, arg, oldval)
2393 struct frame *f;
2394 Lisp_Object arg, oldval;
2395{
ee78dc32
GV
2396 int result;
2397
eb7576ce
GV
2398 if (NILP (arg) && NILP (oldval))
2399 return;
2400
2401 if (STRINGP (arg) && STRINGP (oldval)
2402 && EQ (Fstring_equal (oldval, arg), Qt))
2403 return;
2404
2405 if (SYMBOLP (arg) && SYMBOLP (oldval) && EQ (arg, oldval))
ee78dc32
GV
2406 return;
2407
2408 BLOCK_INPUT;
ee78dc32 2409
eb7576ce 2410 result = x_bitmap_icon (f, arg);
ee78dc32
GV
2411 if (result)
2412 {
2413 UNBLOCK_INPUT;
2414 error ("No icon window available");
2415 }
2416
ee78dc32 2417 UNBLOCK_INPUT;
ee78dc32
GV
2418}
2419
2420/* Return non-nil if frame F wants a bitmap icon. */
2421
2422Lisp_Object
2423x_icon_type (f)
2424 FRAME_PTR f;
2425{
2426 Lisp_Object tem;
2427
2428 tem = assq_no_quit (Qicon_type, f->param_alist);
2429 if (CONSP (tem))
8e713be6 2430 return XCDR (tem);
ee78dc32
GV
2431 else
2432 return Qnil;
2433}
2434
2435void
2436x_set_icon_name (f, arg, oldval)
2437 struct frame *f;
2438 Lisp_Object arg, oldval;
2439{
ee78dc32
GV
2440 if (STRINGP (arg))
2441 {
2442 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
2443 return;
2444 }
2445 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
2446 return;
2447
2448 f->icon_name = arg;
2449
2450#if 0
fbd6baed 2451 if (f->output_data.w32->icon_bitmap != 0)
ee78dc32
GV
2452 return;
2453
2454 BLOCK_INPUT;
2455
2456 result = x_text_icon (f,
1edf84e7 2457 (char *) XSTRING ((!NILP (f->icon_name)
ee78dc32 2458 ? f->icon_name
1edf84e7
GV
2459 : !NILP (f->title)
2460 ? f->title
ee78dc32
GV
2461 : f->name))->data);
2462
2463 if (result)
2464 {
2465 UNBLOCK_INPUT;
2466 error ("No icon window available");
2467 }
2468
2469 /* If the window was unmapped (and its icon was mapped),
2470 the new icon is not mapped, so map the window in its stead. */
2471 if (FRAME_VISIBLE_P (f))
2472 {
2473#ifdef USE_X_TOOLKIT
fbd6baed 2474 XtPopup (f->output_data.w32->widget, XtGrabNone);
ee78dc32 2475#endif
fbd6baed 2476 XMapWindow (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f));
ee78dc32
GV
2477 }
2478
fbd6baed 2479 XFlush (FRAME_W32_DISPLAY (f));
ee78dc32
GV
2480 UNBLOCK_INPUT;
2481#endif
2482}
2483
2484extern Lisp_Object x_new_font ();
4587b026 2485extern Lisp_Object x_new_fontset();
ee78dc32
GV
2486
2487void
2488x_set_font (f, arg, oldval)
2489 struct frame *f;
2490 Lisp_Object arg, oldval;
2491{
2492 Lisp_Object result;
4587b026 2493 Lisp_Object fontset_name;
4b817373 2494 Lisp_Object frame;
3cf3436e 2495 int old_fontset = FRAME_FONTSET(f);
ee78dc32 2496
b7826503 2497 CHECK_STRING (arg);
ee78dc32 2498
4587b026
GV
2499 fontset_name = Fquery_fontset (arg, Qnil);
2500
ee78dc32 2501 BLOCK_INPUT;
4587b026
GV
2502 result = (STRINGP (fontset_name)
2503 ? x_new_fontset (f, XSTRING (fontset_name)->data)
2504 : x_new_font (f, XSTRING (arg)->data));
ee78dc32
GV
2505 UNBLOCK_INPUT;
2506
2507 if (EQ (result, Qnil))
dfff8a69 2508 error ("Font `%s' is not defined", XSTRING (arg)->data);
ee78dc32 2509 else if (EQ (result, Qt))
dfff8a69 2510 error ("The characters of the given font have varying widths");
ee78dc32
GV
2511 else if (STRINGP (result))
2512 {
3cf3436e
JR
2513 if (STRINGP (fontset_name))
2514 {
2515 /* Fontset names are built from ASCII font names, so the
2516 names may be equal despite there was a change. */
2517 if (old_fontset == FRAME_FONTSET (f))
2518 return;
2519 }
2520 else if (!NILP (Fequal (result, oldval)))
dc220243 2521 return;
3cf3436e 2522
ee78dc32 2523 store_frame_param (f, Qfont, result);
6fc2811b 2524 recompute_basic_faces (f);
ee78dc32
GV
2525 }
2526 else
2527 abort ();
4b817373 2528
6fc2811b
JR
2529 do_pending_window_change (0);
2530
2531 /* Don't call `face-set-after-frame-default' when faces haven't been
2532 initialized yet. This is the case when called from
2533 Fx_create_frame. In that case, the X widget or window doesn't
2534 exist either, and we can end up in x_report_frame_params with a
2535 null widget which gives a segfault. */
2536 if (FRAME_FACE_CACHE (f))
2537 {
2538 XSETFRAME (frame, f);
2539 call1 (Qface_set_after_frame_default, frame);
2540 }
ee78dc32
GV
2541}
2542
41c1bdd9
KS
2543static void
2544x_set_fringe_width (f, new_value, old_value)
2545 struct frame *f;
2546 Lisp_Object new_value, old_value;
2547{
2548 x_compute_fringe_widths (f, 1);
2549}
2550
ee78dc32
GV
2551void
2552x_set_border_width (f, arg, oldval)
2553 struct frame *f;
2554 Lisp_Object arg, oldval;
2555{
b7826503 2556 CHECK_NUMBER (arg);
ee78dc32 2557
fbd6baed 2558 if (XINT (arg) == f->output_data.w32->border_width)
ee78dc32
GV
2559 return;
2560
fbd6baed 2561 if (FRAME_W32_WINDOW (f) != 0)
ee78dc32
GV
2562 error ("Cannot change the border width of a window");
2563
fbd6baed 2564 f->output_data.w32->border_width = XINT (arg);
ee78dc32
GV
2565}
2566
2567void
2568x_set_internal_border_width (f, arg, oldval)
2569 struct frame *f;
2570 Lisp_Object arg, oldval;
2571{
fbd6baed 2572 int old = f->output_data.w32->internal_border_width;
ee78dc32 2573
b7826503 2574 CHECK_NUMBER (arg);
fbd6baed
GV
2575 f->output_data.w32->internal_border_width = XINT (arg);
2576 if (f->output_data.w32->internal_border_width < 0)
2577 f->output_data.w32->internal_border_width = 0;
ee78dc32 2578
fbd6baed 2579 if (f->output_data.w32->internal_border_width == old)
ee78dc32
GV
2580 return;
2581
fbd6baed 2582 if (FRAME_W32_WINDOW (f) != 0)
ee78dc32 2583 {
ee78dc32 2584 x_set_window_size (f, 0, f->width, f->height);
ee78dc32 2585 SET_FRAME_GARBAGED (f);
6fc2811b 2586 do_pending_window_change (0);
ee78dc32 2587 }
a05e2bae
JR
2588 else
2589 SET_FRAME_GARBAGED (f);
ee78dc32
GV
2590}
2591
2592void
2593x_set_visibility (f, value, oldval)
2594 struct frame *f;
2595 Lisp_Object value, oldval;
2596{
2597 Lisp_Object frame;
2598 XSETFRAME (frame, f);
2599
2600 if (NILP (value))
2601 Fmake_frame_invisible (frame, Qt);
2602 else if (EQ (value, Qicon))
2603 Ficonify_frame (frame);
2604 else
2605 Fmake_frame_visible (frame);
2606}
2607
a1258667
JR
2608\f
2609/* Change window heights in windows rooted in WINDOW by N lines. */
2610
2611static void
2612x_change_window_heights (window, n)
2613 Lisp_Object window;
2614 int n;
2615{
2616 struct window *w = XWINDOW (window);
2617
2618 XSETFASTINT (w->top, XFASTINT (w->top) + n);
2619 XSETFASTINT (w->height, XFASTINT (w->height) - n);
2620
2621 if (INTEGERP (w->orig_top))
2622 XSETFASTINT (w->orig_top, XFASTINT (w->orig_top) + n);
2623 if (INTEGERP (w->orig_height))
2624 XSETFASTINT (w->orig_height, XFASTINT (w->orig_height) - n);
2625
2626 /* Handle just the top child in a vertical split. */
2627 if (!NILP (w->vchild))
2628 x_change_window_heights (w->vchild, n);
2629
2630 /* Adjust all children in a horizontal split. */
2631 for (window = w->hchild; !NILP (window); window = w->next)
2632 {
2633 w = XWINDOW (window);
2634 x_change_window_heights (window, n);
2635 }
2636}
2637
ee78dc32
GV
2638void
2639x_set_menu_bar_lines (f, value, oldval)
2640 struct frame *f;
2641 Lisp_Object value, oldval;
2642{
2643 int nlines;
2644 int olines = FRAME_MENU_BAR_LINES (f);
2645
2646 /* Right now, menu bars don't work properly in minibuf-only frames;
2647 most of the commands try to apply themselves to the minibuffer
6fc2811b 2648 frame itself, and get an error because you can't switch buffers
ee78dc32
GV
2649 in or split the minibuffer window. */
2650 if (FRAME_MINIBUF_ONLY_P (f))
2651 return;
2652
2653 if (INTEGERP (value))
2654 nlines = XINT (value);
2655 else
2656 nlines = 0;
2657
2658 FRAME_MENU_BAR_LINES (f) = 0;
2659 if (nlines)
2660 FRAME_EXTERNAL_MENU_BAR (f) = 1;
2661 else
2662 {
2663 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
2664 free_frame_menubar (f);
2665 FRAME_EXTERNAL_MENU_BAR (f) = 0;
1edf84e7
GV
2666
2667 /* Adjust the frame size so that the client (text) dimensions
2668 remain the same. This depends on FRAME_EXTERNAL_MENU_BAR being
2669 set correctly. */
2670 x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
6fc2811b 2671 do_pending_window_change (0);
ee78dc32 2672 }
6fc2811b
JR
2673 adjust_glyphs (f);
2674}
2675
2676
2677/* Set the number of lines used for the tool bar of frame F to VALUE.
2678 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
2679 is the old number of tool bar lines. This function changes the
2680 height of all windows on frame F to match the new tool bar height.
2681 The frame's height doesn't change. */
2682
2683void
2684x_set_tool_bar_lines (f, value, oldval)
2685 struct frame *f;
2686 Lisp_Object value, oldval;
2687{
36f8209a
JR
2688 int delta, nlines, root_height;
2689 Lisp_Object root_window;
6fc2811b 2690
dc220243
JR
2691 /* Treat tool bars like menu bars. */
2692 if (FRAME_MINIBUF_ONLY_P (f))
2693 return;
2694
6fc2811b
JR
2695 /* Use VALUE only if an integer >= 0. */
2696 if (INTEGERP (value) && XINT (value) >= 0)
2697 nlines = XFASTINT (value);
2698 else
2699 nlines = 0;
2700
2701 /* Make sure we redisplay all windows in this frame. */
2702 ++windows_or_buffers_changed;
2703
2704 delta = nlines - FRAME_TOOL_BAR_LINES (f);
36f8209a
JR
2705
2706 /* Don't resize the tool-bar to more than we have room for. */
2707 root_window = FRAME_ROOT_WINDOW (f);
2708 root_height = XINT (XWINDOW (root_window)->height);
2709 if (root_height - delta < 1)
2710 {
2711 delta = root_height - 1;
2712 nlines = FRAME_TOOL_BAR_LINES (f) + delta;
2713 }
2714
6fc2811b 2715 FRAME_TOOL_BAR_LINES (f) = nlines;
36f8209a 2716 x_change_window_heights (root_window, delta);
6fc2811b 2717 adjust_glyphs (f);
36f8209a
JR
2718
2719 /* We also have to make sure that the internal border at the top of
2720 the frame, below the menu bar or tool bar, is redrawn when the
2721 tool bar disappears. This is so because the internal border is
2722 below the tool bar if one is displayed, but is below the menu bar
2723 if there isn't a tool bar. The tool bar draws into the area
2724 below the menu bar. */
2725 if (FRAME_W32_WINDOW (f) && FRAME_TOOL_BAR_LINES (f) == 0)
2726 {
2727 updating_frame = f;
2728 clear_frame ();
2729 clear_current_matrices (f);
2730 updating_frame = NULL;
2731 }
2732
2733 /* If the tool bar gets smaller, the internal border below it
2734 has to be cleared. It was formerly part of the display
2735 of the larger tool bar, and updating windows won't clear it. */
2736 if (delta < 0)
2737 {
2738 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
2739 int width = PIXEL_WIDTH (f);
2740 int y = nlines * CANON_Y_UNIT (f);
2741
2742 BLOCK_INPUT;
2743 {
2744 HDC hdc = get_frame_dc (f);
2745 w32_clear_area (f, hdc, 0, y, width, height);
2746 release_frame_dc (f, hdc);
2747 }
2748 UNBLOCK_INPUT;
3cf3436e
JR
2749
2750 if (WINDOWP (f->tool_bar_window))
2751 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
36f8209a 2752 }
ee78dc32
GV
2753}
2754
6fc2811b 2755
ee78dc32 2756/* Change the name of frame F to NAME. If NAME is nil, set F's name to
fbd6baed 2757 w32_id_name.
ee78dc32
GV
2758
2759 If EXPLICIT is non-zero, that indicates that lisp code is setting the
2760 name; if NAME is a string, set F's name to NAME and set
2761 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
2762
2763 If EXPLICIT is zero, that indicates that Emacs redisplay code is
2764 suggesting a new name, which lisp code should override; if
2765 F->explicit_name is set, ignore the new name; otherwise, set it. */
2766
2767void
2768x_set_name (f, name, explicit)
2769 struct frame *f;
2770 Lisp_Object name;
2771 int explicit;
2772{
2773 /* Make sure that requests from lisp code override requests from
2774 Emacs redisplay code. */
2775 if (explicit)
2776 {
2777 /* If we're switching from explicit to implicit, we had better
2778 update the mode lines and thereby update the title. */
2779 if (f->explicit_name && NILP (name))
2780 update_mode_lines = 1;
2781
2782 f->explicit_name = ! NILP (name);
2783 }
2784 else if (f->explicit_name)
2785 return;
2786
fbd6baed 2787 /* If NAME is nil, set the name to the w32_id_name. */
ee78dc32
GV
2788 if (NILP (name))
2789 {
2790 /* Check for no change needed in this very common case
2791 before we do any consing. */
fbd6baed 2792 if (!strcmp (FRAME_W32_DISPLAY_INFO (f)->w32_id_name,
ee78dc32
GV
2793 XSTRING (f->name)->data))
2794 return;
fbd6baed 2795 name = build_string (FRAME_W32_DISPLAY_INFO (f)->w32_id_name);
ee78dc32
GV
2796 }
2797 else
b7826503 2798 CHECK_STRING (name);
ee78dc32
GV
2799
2800 /* Don't change the name if it's already NAME. */
2801 if (! NILP (Fstring_equal (name, f->name)))
2802 return;
2803
1edf84e7
GV
2804 f->name = name;
2805
2806 /* For setting the frame title, the title parameter should override
2807 the name parameter. */
2808 if (! NILP (f->title))
2809 name = f->title;
2810
fbd6baed 2811 if (FRAME_W32_WINDOW (f))
ee78dc32 2812 {
6fc2811b 2813 if (STRING_MULTIBYTE (name))
dfff8a69 2814 name = ENCODE_SYSTEM (name);
6fc2811b 2815
ee78dc32 2816 BLOCK_INPUT;
fbd6baed 2817 SetWindowText(FRAME_W32_WINDOW (f), XSTRING (name)->data);
ee78dc32
GV
2818 UNBLOCK_INPUT;
2819 }
ee78dc32
GV
2820}
2821
2822/* This function should be called when the user's lisp code has
2823 specified a name for the frame; the name will override any set by the
2824 redisplay code. */
2825void
2826x_explicitly_set_name (f, arg, oldval)
2827 FRAME_PTR f;
2828 Lisp_Object arg, oldval;
2829{
2830 x_set_name (f, arg, 1);
2831}
2832
2833/* This function should be called by Emacs redisplay code to set the
2834 name; names set this way will never override names set by the user's
2835 lisp code. */
2836void
2837x_implicitly_set_name (f, arg, oldval)
2838 FRAME_PTR f;
2839 Lisp_Object arg, oldval;
2840{
2841 x_set_name (f, arg, 0);
2842}
1edf84e7
GV
2843\f
2844/* Change the title of frame F to NAME.
2845 If NAME is nil, use the frame name as the title.
2846
2847 If EXPLICIT is non-zero, that indicates that lisp code is setting the
2848 name; if NAME is a string, set F's name to NAME and set
2849 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
2850
2851 If EXPLICIT is zero, that indicates that Emacs redisplay code is
2852 suggesting a new name, which lisp code should override; if
2853 F->explicit_name is set, ignore the new name; otherwise, set it. */
ee78dc32 2854
1edf84e7 2855void
6fc2811b 2856x_set_title (f, name, old_name)
1edf84e7 2857 struct frame *f;
6fc2811b 2858 Lisp_Object name, old_name;
1edf84e7
GV
2859{
2860 /* Don't change the title if it's already NAME. */
2861 if (EQ (name, f->title))
2862 return;
2863
2864 update_mode_lines = 1;
2865
2866 f->title = name;
2867
2868 if (NILP (name))
2869 name = f->name;
2870
2871 if (FRAME_W32_WINDOW (f))
2872 {
6fc2811b 2873 if (STRING_MULTIBYTE (name))
dfff8a69 2874 name = ENCODE_SYSTEM (name);
6fc2811b 2875
1edf84e7
GV
2876 BLOCK_INPUT;
2877 SetWindowText(FRAME_W32_WINDOW (f), XSTRING (name)->data);
2878 UNBLOCK_INPUT;
2879 }
2880}
2881\f
ee78dc32
GV
2882void
2883x_set_autoraise (f, arg, oldval)
2884 struct frame *f;
2885 Lisp_Object arg, oldval;
2886{
2887 f->auto_raise = !EQ (Qnil, arg);
2888}
2889
2890void
2891x_set_autolower (f, arg, oldval)
2892 struct frame *f;
2893 Lisp_Object arg, oldval;
2894{
2895 f->auto_lower = !EQ (Qnil, arg);
2896}
2897
2898void
2899x_set_unsplittable (f, arg, oldval)
2900 struct frame *f;
2901 Lisp_Object arg, oldval;
2902{
2903 f->no_split = !NILP (arg);
2904}
2905
2906void
2907x_set_vertical_scroll_bars (f, arg, oldval)
2908 struct frame *f;
2909 Lisp_Object arg, oldval;
2910{
1026b400
RS
2911 if ((EQ (arg, Qleft) && FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
2912 || (EQ (arg, Qright) && FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f))
2913 || (NILP (arg) && FRAME_HAS_VERTICAL_SCROLL_BARS (f))
2914 || (!NILP (arg) && ! FRAME_HAS_VERTICAL_SCROLL_BARS (f)))
ee78dc32 2915 {
1026b400
RS
2916 FRAME_VERTICAL_SCROLL_BAR_TYPE (f) = NILP (arg) ?
2917 vertical_scroll_bar_none :
87996783
GV
2918 /* Put scroll bars on the right by default, as is conventional
2919 on MS-Windows. */
2920 EQ (Qleft, arg)
2921 ? vertical_scroll_bar_left
2922 : vertical_scroll_bar_right;
ee78dc32
GV
2923
2924 /* We set this parameter before creating the window for the
2925 frame, so we can get the geometry right from the start.
2926 However, if the window hasn't been created yet, we shouldn't
2927 call x_set_window_size. */
fbd6baed 2928 if (FRAME_W32_WINDOW (f))
ee78dc32 2929 x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
6fc2811b 2930 do_pending_window_change (0);
ee78dc32
GV
2931 }
2932}
2933
2934void
2935x_set_scroll_bar_width (f, arg, oldval)
2936 struct frame *f;
2937 Lisp_Object arg, oldval;
2938{
6fc2811b
JR
2939 int wid = FONT_WIDTH (f->output_data.w32->font);
2940
ee78dc32
GV
2941 if (NILP (arg))
2942 {
6fc2811b
JR
2943 FRAME_SCROLL_BAR_PIXEL_WIDTH (f) = GetSystemMetrics (SM_CXVSCROLL);
2944 FRAME_SCROLL_BAR_COLS (f) = (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) +
2945 wid - 1) / wid;
2946 if (FRAME_W32_WINDOW (f))
2947 x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
2948 do_pending_window_change (0);
ee78dc32
GV
2949 }
2950 else if (INTEGERP (arg) && XINT (arg) > 0
2951 && XFASTINT (arg) != FRAME_SCROLL_BAR_PIXEL_WIDTH (f))
2952 {
ee78dc32 2953 FRAME_SCROLL_BAR_PIXEL_WIDTH (f) = XFASTINT (arg);
6fc2811b
JR
2954 FRAME_SCROLL_BAR_COLS (f) = (FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
2955 + wid-1) / wid;
fbd6baed 2956 if (FRAME_W32_WINDOW (f))
ee78dc32 2957 x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
6fc2811b 2958 do_pending_window_change (0);
ee78dc32 2959 }
6fc2811b
JR
2960 change_frame_size (f, 0, FRAME_WIDTH (f), 0, 0, 0);
2961 XWINDOW (FRAME_SELECTED_WINDOW (f))->cursor.hpos = 0;
2962 XWINDOW (FRAME_SELECTED_WINDOW (f))->cursor.x = 0;
ee78dc32
GV
2963}
2964\f
2965/* Subroutines of creating an frame. */
2966
2967/* Make sure that Vx_resource_name is set to a reasonable value.
2968 Fix it up, or set it to `emacs' if it is too hopeless. */
2969
2970static void
2971validate_x_resource_name ()
2972{
6fc2811b 2973 int len = 0;
ee78dc32
GV
2974 /* Number of valid characters in the resource name. */
2975 int good_count = 0;
2976 /* Number of invalid characters in the resource name. */
2977 int bad_count = 0;
2978 Lisp_Object new;
2979 int i;
2980
2981 if (STRINGP (Vx_resource_name))
2982 {
2983 unsigned char *p = XSTRING (Vx_resource_name)->data;
2984 int i;
2985
dfff8a69 2986 len = STRING_BYTES (XSTRING (Vx_resource_name));
ee78dc32
GV
2987
2988 /* Only letters, digits, - and _ are valid in resource names.
2989 Count the valid characters and count the invalid ones. */
2990 for (i = 0; i < len; i++)
2991 {
2992 int c = p[i];
2993 if (! ((c >= 'a' && c <= 'z')
2994 || (c >= 'A' && c <= 'Z')
2995 || (c >= '0' && c <= '9')
2996 || c == '-' || c == '_'))
2997 bad_count++;
2998 else
2999 good_count++;
3000 }
3001 }
3002 else
3003 /* Not a string => completely invalid. */
3004 bad_count = 5, good_count = 0;
3005
3006 /* If name is valid already, return. */
3007 if (bad_count == 0)
3008 return;
3009
3010 /* If name is entirely invalid, or nearly so, use `emacs'. */
3011 if (good_count == 0
3012 || (good_count == 1 && bad_count > 0))
3013 {
3014 Vx_resource_name = build_string ("emacs");
3015 return;
3016 }
3017
3018 /* Name is partly valid. Copy it and replace the invalid characters
3019 with underscores. */
3020
3021 Vx_resource_name = new = Fcopy_sequence (Vx_resource_name);
3022
3023 for (i = 0; i < len; i++)
3024 {
3025 int c = XSTRING (new)->data[i];
3026 if (! ((c >= 'a' && c <= 'z')
3027 || (c >= 'A' && c <= 'Z')
3028 || (c >= '0' && c <= '9')
3029 || c == '-' || c == '_'))
3030 XSTRING (new)->data[i] = '_';
3031 }
3032}
3033
3034
3035extern char *x_get_string_resource ();
3036
3037DEFUN ("x-get-resource", Fx_get_resource, Sx_get_resource, 2, 4, 0,
74e1aeec
JR
3038 doc: /* Return the value of ATTRIBUTE, of class CLASS, from the X defaults database.
3039This uses `INSTANCE.ATTRIBUTE' as the key and `Emacs.CLASS' as the
3040class, where INSTANCE is the name under which Emacs was invoked, or
3041the name specified by the `-name' or `-rn' command-line arguments.
3042
3043The optional arguments COMPONENT and SUBCLASS add to the key and the
3044class, respectively. You must specify both of them or neither.
3045If you specify them, the key is `INSTANCE.COMPONENT.ATTRIBUTE'
3046and the class is `Emacs.CLASS.SUBCLASS'. */)
ee78dc32
GV
3047 (attribute, class, component, subclass)
3048 Lisp_Object attribute, class, component, subclass;
3049{
3050 register char *value;
3051 char *name_key;
3052 char *class_key;
3053
b7826503
PJ
3054 CHECK_STRING (attribute);
3055 CHECK_STRING (class);
ee78dc32
GV
3056
3057 if (!NILP (component))
b7826503 3058 CHECK_STRING (component);
ee78dc32 3059 if (!NILP (subclass))
b7826503 3060 CHECK_STRING (subclass);
ee78dc32
GV
3061 if (NILP (component) != NILP (subclass))
3062 error ("x-get-resource: must specify both COMPONENT and SUBCLASS or neither");
3063
3064 validate_x_resource_name ();
3065
3066 /* Allocate space for the components, the dots which separate them,
3067 and the final '\0'. Make them big enough for the worst case. */
dfff8a69 3068 name_key = (char *) alloca (STRING_BYTES (XSTRING (Vx_resource_name))
ee78dc32 3069 + (STRINGP (component)
dfff8a69
JR
3070 ? STRING_BYTES (XSTRING (component)) : 0)
3071 + STRING_BYTES (XSTRING (attribute))
ee78dc32
GV
3072 + 3);
3073
3074 class_key = (char *) alloca ((sizeof (EMACS_CLASS) - 1)
dfff8a69 3075 + STRING_BYTES (XSTRING (class))
ee78dc32 3076 + (STRINGP (subclass)
dfff8a69 3077 ? STRING_BYTES (XSTRING (subclass)) : 0)
ee78dc32
GV
3078 + 3);
3079
3080 /* Start with emacs.FRAMENAME for the name (the specific one)
3081 and with `Emacs' for the class key (the general one). */
3082 strcpy (name_key, XSTRING (Vx_resource_name)->data);
3083 strcpy (class_key, EMACS_CLASS);
3084
3085 strcat (class_key, ".");
3086 strcat (class_key, XSTRING (class)->data);
3087
3088 if (!NILP (component))
3089 {
3090 strcat (class_key, ".");
3091 strcat (class_key, XSTRING (subclass)->data);
3092
3093 strcat (name_key, ".");
3094 strcat (name_key, XSTRING (component)->data);
3095 }
3096
3097 strcat (name_key, ".");
3098 strcat (name_key, XSTRING (attribute)->data);
3099
3100 value = x_get_string_resource (Qnil,
3101 name_key, class_key);
3102
3103 if (value != (char *) 0)
3104 return build_string (value);
3105 else
3106 return Qnil;
3107}
3108
3109/* Used when C code wants a resource value. */
3110
3111char *
3112x_get_resource_string (attribute, class)
3113 char *attribute, *class;
3114{
ee78dc32
GV
3115 char *name_key;
3116 char *class_key;
6fc2811b 3117 struct frame *sf = SELECTED_FRAME ();
ee78dc32
GV
3118
3119 /* Allocate space for the components, the dots which separate them,
3120 and the final '\0'. */
dfff8a69 3121 name_key = (char *) alloca (STRING_BYTES (XSTRING (Vinvocation_name))
ee78dc32
GV
3122 + strlen (attribute) + 2);
3123 class_key = (char *) alloca ((sizeof (EMACS_CLASS) - 1)
3124 + strlen (class) + 2);
3125
3126 sprintf (name_key, "%s.%s",
3127 XSTRING (Vinvocation_name)->data,
3128 attribute);
3129 sprintf (class_key, "%s.%s", EMACS_CLASS, class);
3130
6fc2811b 3131 return x_get_string_resource (sf, name_key, class_key);
ee78dc32
GV
3132}
3133
3134/* Types we might convert a resource string into. */
3135enum resource_types
6fc2811b
JR
3136{
3137 RES_TYPE_NUMBER,
3138 RES_TYPE_FLOAT,
3139 RES_TYPE_BOOLEAN,
3140 RES_TYPE_STRING,
3141 RES_TYPE_SYMBOL
3142};
ee78dc32
GV
3143
3144/* Return the value of parameter PARAM.
3145
3146 First search ALIST, then Vdefault_frame_alist, then the X defaults
3147 database, using ATTRIBUTE as the attribute name and CLASS as its class.
3148
3149 Convert the resource to the type specified by desired_type.
3150
3151 If no default is specified, return Qunbound. If you call
6fc2811b 3152 w32_get_arg, make sure you deal with Qunbound in a reasonable way,
ee78dc32
GV
3153 and don't let it get stored in any Lisp-visible variables! */
3154
3155static Lisp_Object
6fc2811b 3156w32_get_arg (alist, param, attribute, class, type)
ee78dc32
GV
3157 Lisp_Object alist, param;
3158 char *attribute;
3159 char *class;
3160 enum resource_types type;
3161{
3162 register Lisp_Object tem;
3163
3164 tem = Fassq (param, alist);
3165 if (EQ (tem, Qnil))
3166 tem = Fassq (param, Vdefault_frame_alist);
3167 if (EQ (tem, Qnil))
3168 {
3169
3170 if (attribute)
3171 {
3172 tem = Fx_get_resource (build_string (attribute),
3173 build_string (class),
3174 Qnil, Qnil);
3175
3176 if (NILP (tem))
3177 return Qunbound;
3178
3179 switch (type)
3180 {
6fc2811b 3181 case RES_TYPE_NUMBER:
ee78dc32
GV
3182 return make_number (atoi (XSTRING (tem)->data));
3183
6fc2811b
JR
3184 case RES_TYPE_FLOAT:
3185 return make_float (atof (XSTRING (tem)->data));
3186
3187 case RES_TYPE_BOOLEAN:
ee78dc32
GV
3188 tem = Fdowncase (tem);
3189 if (!strcmp (XSTRING (tem)->data, "on")
3190 || !strcmp (XSTRING (tem)->data, "true"))
3191 return Qt;
3192 else
3193 return Qnil;
3194
6fc2811b 3195 case RES_TYPE_STRING:
ee78dc32
GV
3196 return tem;
3197
6fc2811b 3198 case RES_TYPE_SYMBOL:
ee78dc32
GV
3199 /* As a special case, we map the values `true' and `on'
3200 to Qt, and `false' and `off' to Qnil. */
3201 {
3202 Lisp_Object lower;
3203 lower = Fdowncase (tem);
3204 if (!strcmp (XSTRING (lower)->data, "on")
3205 || !strcmp (XSTRING (lower)->data, "true"))
3206 return Qt;
3207 else if (!strcmp (XSTRING (lower)->data, "off")
3208 || !strcmp (XSTRING (lower)->data, "false"))
3209 return Qnil;
3210 else
3211 return Fintern (tem, Qnil);
3212 }
3213
3214 default:
3215 abort ();
3216 }
3217 }
3218 else
3219 return Qunbound;
3220 }
3221 return Fcdr (tem);
3222}
3223
3224/* Record in frame F the specified or default value according to ALIST
dfff8a69
JR
3225 of the parameter named PROP (a Lisp symbol).
3226 If no value is specified for PROP, look for an X default for XPROP
ee78dc32
GV
3227 on the frame named NAME.
3228 If that is not found either, use the value DEFLT. */
3229
3230static Lisp_Object
3231x_default_parameter (f, alist, prop, deflt, xprop, xclass, type)
3232 struct frame *f;
3233 Lisp_Object alist;
3234 Lisp_Object prop;
3235 Lisp_Object deflt;
3236 char *xprop;
3237 char *xclass;
3238 enum resource_types type;
3239{
3240 Lisp_Object tem;
3241
6fc2811b 3242 tem = w32_get_arg (alist, prop, xprop, xclass, type);
ee78dc32
GV
3243 if (EQ (tem, Qunbound))
3244 tem = deflt;
3245 x_set_frame_parameters (f, Fcons (Fcons (prop, tem), Qnil));
3246 return tem;
3247}
3248\f
3249DEFUN ("x-parse-geometry", Fx_parse_geometry, Sx_parse_geometry, 1, 1, 0,
74e1aeec
JR
3250 doc: /* Parse an X-style geometry string STRING.
3251Returns an alist of the form ((top . TOP), (left . LEFT) ... ).
3252The properties returned may include `top', `left', `height', and `width'.
3253The value of `left' or `top' may be an integer,
3254or a list (+ N) meaning N pixels relative to top/left corner,
3255or a list (- N) meaning -N pixels relative to bottom/right corner. */)
ee78dc32
GV
3256 (string)
3257 Lisp_Object string;
3258{
3259 int geometry, x, y;
3260 unsigned int width, height;
3261 Lisp_Object result;
3262
b7826503 3263 CHECK_STRING (string);
ee78dc32
GV
3264
3265 geometry = XParseGeometry ((char *) XSTRING (string)->data,
3266 &x, &y, &width, &height);
3267
3268 result = Qnil;
3269 if (geometry & XValue)
3270 {
3271 Lisp_Object element;
3272
3273 if (x >= 0 && (geometry & XNegative))
3274 element = Fcons (Qleft, Fcons (Qminus, Fcons (make_number (-x), Qnil)));
3275 else if (x < 0 && ! (geometry & XNegative))
3276 element = Fcons (Qleft, Fcons (Qplus, Fcons (make_number (x), Qnil)));
3277 else
3278 element = Fcons (Qleft, make_number (x));
3279 result = Fcons (element, result);
3280 }
3281
3282 if (geometry & YValue)
3283 {
3284 Lisp_Object element;
3285
3286 if (y >= 0 && (geometry & YNegative))
3287 element = Fcons (Qtop, Fcons (Qminus, Fcons (make_number (-y), Qnil)));
3288 else if (y < 0 && ! (geometry & YNegative))
3289 element = Fcons (Qtop, Fcons (Qplus, Fcons (make_number (y), Qnil)));
3290 else
3291 element = Fcons (Qtop, make_number (y));
3292 result = Fcons (element, result);
3293 }
3294
3295 if (geometry & WidthValue)
3296 result = Fcons (Fcons (Qwidth, make_number (width)), result);
3297 if (geometry & HeightValue)
3298 result = Fcons (Fcons (Qheight, make_number (height)), result);
3299
3300 return result;
3301}
3302
3303/* Calculate the desired size and position of this window,
3304 and return the flags saying which aspects were specified.
3305
3306 This function does not make the coordinates positive. */
3307
3308#define DEFAULT_ROWS 40
3309#define DEFAULT_COLS 80
3310
3311static int
3312x_figure_window_size (f, parms)
3313 struct frame *f;
3314 Lisp_Object parms;
3315{
3316 register Lisp_Object tem0, tem1, tem2;
ee78dc32
GV
3317 long window_prompting = 0;
3318
3319 /* Default values if we fall through.
3320 Actually, if that happens we should get
3321 window manager prompting. */
1026b400 3322 SET_FRAME_WIDTH (f, DEFAULT_COLS);
ee78dc32
GV
3323 f->height = DEFAULT_ROWS;
3324 /* Window managers expect that if program-specified
3325 positions are not (0,0), they're intentional, not defaults. */
fbd6baed
GV
3326 f->output_data.w32->top_pos = 0;
3327 f->output_data.w32->left_pos = 0;
ee78dc32 3328
35b41202
JR
3329 /* Ensure that old new_width and new_height will not override the
3330 values set here. */
3331 FRAME_NEW_WIDTH (f) = 0;
3332 FRAME_NEW_HEIGHT (f) = 0;
3333
6fc2811b
JR
3334 tem0 = w32_get_arg (parms, Qheight, 0, 0, RES_TYPE_NUMBER);
3335 tem1 = w32_get_arg (parms, Qwidth, 0, 0, RES_TYPE_NUMBER);
3336 tem2 = w32_get_arg (parms, Quser_size, 0, 0, RES_TYPE_NUMBER);
ee78dc32
GV
3337 if (! EQ (tem0, Qunbound) || ! EQ (tem1, Qunbound))
3338 {
3339 if (!EQ (tem0, Qunbound))
3340 {
b7826503 3341 CHECK_NUMBER (tem0);
ee78dc32
GV
3342 f->height = XINT (tem0);
3343 }
3344 if (!EQ (tem1, Qunbound))
3345 {
b7826503 3346 CHECK_NUMBER (tem1);
1026b400 3347 SET_FRAME_WIDTH (f, XINT (tem1));
ee78dc32
GV
3348 }
3349 if (!NILP (tem2) && !EQ (tem2, Qunbound))
3350 window_prompting |= USSize;
3351 else
3352 window_prompting |= PSize;
3353 }
3354
fbd6baed 3355 f->output_data.w32->vertical_scroll_bar_extra
ee78dc32
GV
3356 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
3357 ? 0
3358 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
3359 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
fbd6baed 3360 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.w32->font)));
f7b9d4d1 3361
41c1bdd9 3362 x_compute_fringe_widths (f, 0);
f7b9d4d1 3363
fbd6baed
GV
3364 f->output_data.w32->pixel_width = CHAR_TO_PIXEL_WIDTH (f, f->width);
3365 f->output_data.w32->pixel_height = CHAR_TO_PIXEL_HEIGHT (f, f->height);
ee78dc32 3366
6fc2811b
JR
3367 tem0 = w32_get_arg (parms, Qtop, 0, 0, RES_TYPE_NUMBER);
3368 tem1 = w32_get_arg (parms, Qleft, 0, 0, RES_TYPE_NUMBER);
3369 tem2 = w32_get_arg (parms, Quser_position, 0, 0, RES_TYPE_NUMBER);
ee78dc32
GV
3370 if (! EQ (tem0, Qunbound) || ! EQ (tem1, Qunbound))
3371 {
3372 if (EQ (tem0, Qminus))
3373 {
fbd6baed 3374 f->output_data.w32->top_pos = 0;
ee78dc32
GV
3375 window_prompting |= YNegative;
3376 }
8e713be6
KR
3377 else if (CONSP (tem0) && EQ (XCAR (tem0), Qminus)
3378 && CONSP (XCDR (tem0))
3379 && INTEGERP (XCAR (XCDR (tem0))))
ee78dc32 3380 {
8e713be6 3381 f->output_data.w32->top_pos = - XINT (XCAR (XCDR (tem0)));
ee78dc32
GV
3382 window_prompting |= YNegative;
3383 }
8e713be6
KR
3384 else if (CONSP (tem0) && EQ (XCAR (tem0), Qplus)
3385 && CONSP (XCDR (tem0))
3386 && INTEGERP (XCAR (XCDR (tem0))))
ee78dc32 3387 {
8e713be6 3388 f->output_data.w32->top_pos = XINT (XCAR (XCDR (tem0)));
ee78dc32
GV
3389 }
3390 else if (EQ (tem0, Qunbound))
fbd6baed 3391 f->output_data.w32->top_pos = 0;
ee78dc32
GV
3392 else
3393 {
b7826503 3394 CHECK_NUMBER (tem0);
fbd6baed
GV
3395 f->output_data.w32->top_pos = XINT (tem0);
3396 if (f->output_data.w32->top_pos < 0)
ee78dc32
GV
3397 window_prompting |= YNegative;
3398 }
3399
3400 if (EQ (tem1, Qminus))
3401 {
fbd6baed 3402 f->output_data.w32->left_pos = 0;
ee78dc32
GV
3403 window_prompting |= XNegative;
3404 }
8e713be6
KR
3405 else if (CONSP (tem1) && EQ (XCAR (tem1), Qminus)
3406 && CONSP (XCDR (tem1))
3407 && INTEGERP (XCAR (XCDR (tem1))))
ee78dc32 3408 {
8e713be6 3409 f->output_data.w32->left_pos = - XINT (XCAR (XCDR (tem1)));
ee78dc32
GV
3410 window_prompting |= XNegative;
3411 }
8e713be6
KR
3412 else if (CONSP (tem1) && EQ (XCAR (tem1), Qplus)
3413 && CONSP (XCDR (tem1))
3414 && INTEGERP (XCAR (XCDR (tem1))))
ee78dc32 3415 {
8e713be6 3416 f->output_data.w32->left_pos = XINT (XCAR (XCDR (tem1)));
ee78dc32
GV
3417 }
3418 else if (EQ (tem1, Qunbound))
fbd6baed 3419 f->output_data.w32->left_pos = 0;
ee78dc32
GV
3420 else
3421 {
b7826503 3422 CHECK_NUMBER (tem1);
fbd6baed
GV
3423 f->output_data.w32->left_pos = XINT (tem1);
3424 if (f->output_data.w32->left_pos < 0)
ee78dc32
GV
3425 window_prompting |= XNegative;
3426 }
3427
3428 if (!NILP (tem2) && ! EQ (tem2, Qunbound))
3429 window_prompting |= USPosition;
3430 else
3431 window_prompting |= PPosition;
3432 }
3433
f7b9d4d1
JR
3434 if (f->output_data.w32->want_fullscreen != FULLSCREEN_NONE)
3435 {
3436 int left, top;
3437 int width, height;
3438
3439 /* It takes both for some WM:s to place it where we want */
3440 window_prompting = USPosition | PPosition;
3441 x_fullscreen_adjust (f, &width, &height, &top, &left);
3442 f->width = width;
3443 f->height = height;
3444 f->output_data.w32->pixel_width = CHAR_TO_PIXEL_WIDTH (f, f->width);
3445 f->output_data.w32->pixel_height = CHAR_TO_PIXEL_HEIGHT (f, f->height);
3446 f->output_data.w32->left_pos = left;
3447 f->output_data.w32->top_pos = top;
3448 }
3449
ee78dc32
GV
3450 return window_prompting;
3451}
3452
3453\f
3454
fbd6baed 3455extern LRESULT CALLBACK w32_wnd_proc ();
ee78dc32
GV
3456
3457BOOL
fbd6baed 3458w32_init_class (hinst)
ee78dc32
GV
3459 HINSTANCE hinst;
3460{
3461 WNDCLASS wc;
3462
5ac45f98 3463 wc.style = CS_HREDRAW | CS_VREDRAW;
fbd6baed 3464 wc.lpfnWndProc = (WNDPROC) w32_wnd_proc;
ee78dc32
GV
3465 wc.cbClsExtra = 0;
3466 wc.cbWndExtra = WND_EXTRA_BYTES;
3467 wc.hInstance = hinst;
3468 wc.hIcon = LoadIcon (hinst, EMACS_CLASS);
3469 wc.hCursor = LoadCursor (NULL, IDC_ARROW);
4587b026 3470 wc.hbrBackground = NULL; /* GetStockObject (WHITE_BRUSH); */
ee78dc32
GV
3471 wc.lpszMenuName = NULL;
3472 wc.lpszClassName = EMACS_CLASS;
3473
3474 return (RegisterClass (&wc));
3475}
3476
3477HWND
fbd6baed 3478w32_createscrollbar (f, bar)
ee78dc32
GV
3479 struct frame *f;
3480 struct scroll_bar * bar;
3481{
3482 return (CreateWindow ("SCROLLBAR", "", SBS_VERT | WS_CHILD | WS_VISIBLE,
3483 /* Position and size of scroll bar. */
6fc2811b
JR
3484 XINT(bar->left) + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
3485 XINT(bar->top),
3486 XINT(bar->width) - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
3487 XINT(bar->height),
fbd6baed 3488 FRAME_W32_WINDOW (f),
ee78dc32
GV
3489 NULL,
3490 hinst,
3491 NULL));
3492}
3493
3494void
fbd6baed 3495w32_createwindow (f)
ee78dc32
GV
3496 struct frame *f;
3497{
3498 HWND hwnd;
1edf84e7
GV
3499 RECT rect;
3500
3501 rect.left = rect.top = 0;
3502 rect.right = PIXEL_WIDTH (f);
3503 rect.bottom = PIXEL_HEIGHT (f);
3504
3505 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
3506 FRAME_EXTERNAL_MENU_BAR (f));
ee78dc32
GV
3507
3508 /* Do first time app init */
3509
3510 if (!hprevinst)
3511 {
fbd6baed 3512 w32_init_class (hinst);
ee78dc32
GV
3513 }
3514
1edf84e7
GV
3515 FRAME_W32_WINDOW (f) = hwnd
3516 = CreateWindow (EMACS_CLASS,
3517 f->namebuf,
9ead1b60 3518 f->output_data.w32->dwStyle | WS_CLIPCHILDREN,
1edf84e7
GV
3519 f->output_data.w32->left_pos,
3520 f->output_data.w32->top_pos,
3521 rect.right - rect.left,
3522 rect.bottom - rect.top,
3523 NULL,
3524 NULL,
3525 hinst,
3526 NULL);
3527
ee78dc32
GV
3528 if (hwnd)
3529 {
1edf84e7
GV
3530 SetWindowLong (hwnd, WND_FONTWIDTH_INDEX, FONT_WIDTH (f->output_data.w32->font));
3531 SetWindowLong (hwnd, WND_LINEHEIGHT_INDEX, f->output_data.w32->line_height);
3532 SetWindowLong (hwnd, WND_BORDER_INDEX, f->output_data.w32->internal_border_width);
3533 SetWindowLong (hwnd, WND_SCROLLBAR_INDEX, f->output_data.w32->vertical_scroll_bar_extra);
6fc2811b 3534 SetWindowLong (hwnd, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
ee78dc32 3535
cb9e33d4
RS
3536 /* Enable drag-n-drop. */
3537 DragAcceptFiles (hwnd, TRUE);
3538
5ac45f98
GV
3539 /* Do this to discard the default setting specified by our parent. */
3540 ShowWindow (hwnd, SW_HIDE);
3c190163 3541 }
3c190163
GV
3542}
3543
ee78dc32
GV
3544void
3545my_post_msg (wmsg, hwnd, msg, wParam, lParam)
fbd6baed 3546 W32Msg * wmsg;
ee78dc32
GV
3547 HWND hwnd;
3548 UINT msg;
3549 WPARAM wParam;
3550 LPARAM lParam;
3551{
3552 wmsg->msg.hwnd = hwnd;
3553 wmsg->msg.message = msg;
3554 wmsg->msg.wParam = wParam;
3555 wmsg->msg.lParam = lParam;
3556 wmsg->msg.time = GetMessageTime ();
3557
3558 post_msg (wmsg);
3559}
3560
e9e23e23 3561/* GetKeyState and MapVirtualKey on Windows 95 do not actually distinguish
a1a80b40
GV
3562 between left and right keys as advertised. We test for this
3563 support dynamically, and set a flag when the support is absent. If
3564 absent, we keep track of the left and right control and alt keys
3565 ourselves. This is particularly necessary on keyboards that rely
3566 upon the AltGr key, which is represented as having the left control
3567 and right alt keys pressed. For these keyboards, we need to know
3568 when the left alt key has been pressed in addition to the AltGr key
3569 so that we can properly support M-AltGr-key sequences (such as M-@
3570 on Swedish keyboards). */
3571
3572#define EMACS_LCONTROL 0
3573#define EMACS_RCONTROL 1
3574#define EMACS_LMENU 2
3575#define EMACS_RMENU 3
3576
3577static int modifiers[4];
3578static int modifiers_recorded;
3579static int modifier_key_support_tested;
3580
3581static void
3582test_modifier_support (unsigned int wparam)
3583{
3584 unsigned int l, r;
3585
3586 if (wparam != VK_CONTROL && wparam != VK_MENU)
3587 return;
3588 if (wparam == VK_CONTROL)
3589 {
3590 l = VK_LCONTROL;
3591 r = VK_RCONTROL;
3592 }
3593 else
3594 {
3595 l = VK_LMENU;
3596 r = VK_RMENU;
3597 }
3598 if (!(GetKeyState (l) & 0x8000) && !(GetKeyState (r) & 0x8000))
3599 modifiers_recorded = 1;
3600 else
3601 modifiers_recorded = 0;
3602 modifier_key_support_tested = 1;
3603}
3604
3605static void
3606record_keydown (unsigned int wparam, unsigned int lparam)
3607{
3608 int i;
3609
3610 if (!modifier_key_support_tested)
3611 test_modifier_support (wparam);
3612
3613 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
3614 return;
3615
3616 if (wparam == VK_CONTROL)
3617 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
3618 else
3619 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
3620
3621 modifiers[i] = 1;
3622}
3623
3624static void
3625record_keyup (unsigned int wparam, unsigned int lparam)
3626{
3627 int i;
3628
3629 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
3630 return;
3631
3632 if (wparam == VK_CONTROL)
3633 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
3634 else
3635 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
3636
3637 modifiers[i] = 0;
3638}
3639
da36a4d6
GV
3640/* Emacs can lose focus while a modifier key has been pressed. When
3641 it regains focus, be conservative and clear all modifiers since
3642 we cannot reconstruct the left and right modifier state. */
3643static void
3644reset_modifiers ()
3645{
8681157a
RS
3646 SHORT ctrl, alt;
3647
adcc3809
GV
3648 if (GetFocus () == NULL)
3649 /* Emacs doesn't have keyboard focus. Do nothing. */
da36a4d6 3650 return;
8681157a
RS
3651
3652 ctrl = GetAsyncKeyState (VK_CONTROL);
3653 alt = GetAsyncKeyState (VK_MENU);
3654
8681157a
RS
3655 if (!(ctrl & 0x08000))
3656 /* Clear any recorded control modifier state. */
3657 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
3658
3659 if (!(alt & 0x08000))
3660 /* Clear any recorded alt modifier state. */
3661 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
3662
adcc3809
GV
3663 /* Update the state of all modifier keys, because modifiers used in
3664 hot-key combinations can get stuck on if Emacs loses focus as a
3665 result of a hot-key being pressed. */
3666 {
3667 BYTE keystate[256];
3668
3669#define CURRENT_STATE(key) ((GetAsyncKeyState (key) & 0x8000) >> 8)
3670
3671 GetKeyboardState (keystate);
3672 keystate[VK_SHIFT] = CURRENT_STATE (VK_SHIFT);
3673 keystate[VK_CONTROL] = CURRENT_STATE (VK_CONTROL);
3674 keystate[VK_LCONTROL] = CURRENT_STATE (VK_LCONTROL);
3675 keystate[VK_RCONTROL] = CURRENT_STATE (VK_RCONTROL);
3676 keystate[VK_MENU] = CURRENT_STATE (VK_MENU);
3677 keystate[VK_LMENU] = CURRENT_STATE (VK_LMENU);
3678 keystate[VK_RMENU] = CURRENT_STATE (VK_RMENU);
3679 keystate[VK_LWIN] = CURRENT_STATE (VK_LWIN);
3680 keystate[VK_RWIN] = CURRENT_STATE (VK_RWIN);
3681 keystate[VK_APPS] = CURRENT_STATE (VK_APPS);
3682 SetKeyboardState (keystate);
3683 }
da36a4d6
GV
3684}
3685
7830e24b
RS
3686/* Synchronize modifier state with what is reported with the current
3687 keystroke. Even if we cannot distinguish between left and right
3688 modifier keys, we know that, if no modifiers are set, then neither
3689 the left or right modifier should be set. */
3690static void
3691sync_modifiers ()
3692{
3693 if (!modifiers_recorded)
3694 return;
3695
3696 if (!(GetKeyState (VK_CONTROL) & 0x8000))
3697 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
3698
3699 if (!(GetKeyState (VK_MENU) & 0x8000))
3700 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
3701}
3702
a1a80b40
GV
3703static int
3704modifier_set (int vkey)
3705{
ccc2d29c 3706 if (vkey == VK_CAPITAL || vkey == VK_SCROLL)
891560d6 3707 return (GetKeyState (vkey) & 0x1);
a1a80b40
GV
3708 if (!modifiers_recorded)
3709 return (GetKeyState (vkey) & 0x8000);
3710
3711 switch (vkey)
3712 {
3713 case VK_LCONTROL:
3714 return modifiers[EMACS_LCONTROL];
3715 case VK_RCONTROL:
3716 return modifiers[EMACS_RCONTROL];
3717 case VK_LMENU:
3718 return modifiers[EMACS_LMENU];
3719 case VK_RMENU:
3720 return modifiers[EMACS_RMENU];
a1a80b40
GV
3721 }
3722 return (GetKeyState (vkey) & 0x8000);
3723}
3724
ccc2d29c
GV
3725/* Convert between the modifier bits W32 uses and the modifier bits
3726 Emacs uses. */
3727
3728unsigned int
3729w32_key_to_modifier (int key)
3730{
3731 Lisp_Object key_mapping;
3732
3733 switch (key)
3734 {
3735 case VK_LWIN:
3736 key_mapping = Vw32_lwindow_modifier;
3737 break;
3738 case VK_RWIN:
3739 key_mapping = Vw32_rwindow_modifier;
3740 break;
3741 case VK_APPS:
3742 key_mapping = Vw32_apps_modifier;
3743 break;
3744 case VK_SCROLL:
3745 key_mapping = Vw32_scroll_lock_modifier;
3746 break;
3747 default:
3748 key_mapping = Qnil;
3749 }
3750
adcc3809
GV
3751 /* NB. This code runs in the input thread, asychronously to the lisp
3752 thread, so we must be careful to ensure access to lisp data is
3753 thread-safe. The following code is safe because the modifier
3754 variable values are updated atomically from lisp and symbols are
3755 not relocated by GC. Also, we don't have to worry about seeing GC
3756 markbits here. */
3757 if (EQ (key_mapping, Qhyper))
ccc2d29c 3758 return hyper_modifier;
adcc3809 3759 if (EQ (key_mapping, Qsuper))
ccc2d29c 3760 return super_modifier;
adcc3809 3761 if (EQ (key_mapping, Qmeta))
ccc2d29c 3762 return meta_modifier;
adcc3809 3763 if (EQ (key_mapping, Qalt))
ccc2d29c 3764 return alt_modifier;
adcc3809 3765 if (EQ (key_mapping, Qctrl))
ccc2d29c 3766 return ctrl_modifier;
adcc3809 3767 if (EQ (key_mapping, Qcontrol)) /* synonym for ctrl */
ccc2d29c 3768 return ctrl_modifier;
adcc3809 3769 if (EQ (key_mapping, Qshift))
ccc2d29c
GV
3770 return shift_modifier;
3771
3772 /* Don't generate any modifier if not explicitly requested. */
3773 return 0;
3774}
3775
3776unsigned int
3777w32_get_modifiers ()
3778{
3779 return ((modifier_set (VK_SHIFT) ? shift_modifier : 0) |
3780 (modifier_set (VK_CONTROL) ? ctrl_modifier : 0) |
3781 (modifier_set (VK_LWIN) ? w32_key_to_modifier (VK_LWIN) : 0) |
3782 (modifier_set (VK_RWIN) ? w32_key_to_modifier (VK_RWIN) : 0) |
3783 (modifier_set (VK_APPS) ? w32_key_to_modifier (VK_APPS) : 0) |
3784 (modifier_set (VK_SCROLL) ? w32_key_to_modifier (VK_SCROLL) : 0) |
3785 (modifier_set (VK_MENU) ?
3786 ((NILP (Vw32_alt_is_meta)) ? alt_modifier : meta_modifier) : 0));
3787}
3788
a1a80b40
GV
3789/* We map the VK_* modifiers into console modifier constants
3790 so that we can use the same routines to handle both console
3791 and window input. */
3792
3793static int
ccc2d29c 3794construct_console_modifiers ()
a1a80b40
GV
3795{
3796 int mods;
3797
a1a80b40
GV
3798 mods = 0;
3799 mods |= (modifier_set (VK_SHIFT)) ? SHIFT_PRESSED : 0;
3800 mods |= (modifier_set (VK_CAPITAL)) ? CAPSLOCK_ON : 0;
ccc2d29c
GV
3801 mods |= (modifier_set (VK_SCROLL)) ? SCROLLLOCK_ON : 0;
3802 mods |= (modifier_set (VK_NUMLOCK)) ? NUMLOCK_ON : 0;
a1a80b40
GV
3803 mods |= (modifier_set (VK_LCONTROL)) ? LEFT_CTRL_PRESSED : 0;
3804 mods |= (modifier_set (VK_RCONTROL)) ? RIGHT_CTRL_PRESSED : 0;
3805 mods |= (modifier_set (VK_LMENU)) ? LEFT_ALT_PRESSED : 0;
3806 mods |= (modifier_set (VK_RMENU)) ? RIGHT_ALT_PRESSED : 0;
ccc2d29c
GV
3807 mods |= (modifier_set (VK_LWIN)) ? LEFT_WIN_PRESSED : 0;
3808 mods |= (modifier_set (VK_RWIN)) ? RIGHT_WIN_PRESSED : 0;
3809 mods |= (modifier_set (VK_APPS)) ? APPS_PRESSED : 0;
a1a80b40
GV
3810
3811 return mods;
3812}
3813
ccc2d29c
GV
3814static int
3815w32_get_key_modifiers (unsigned int wparam, unsigned int lparam)
da36a4d6 3816{
ccc2d29c
GV
3817 int mods;
3818
3819 /* Convert to emacs modifiers. */
3820 mods = w32_kbd_mods_to_emacs (construct_console_modifiers (), wparam);
3821
3822 return mods;
3823}
da36a4d6 3824
ccc2d29c
GV
3825unsigned int
3826map_keypad_keys (unsigned int virt_key, unsigned int extended)
3827{
3828 if (virt_key < VK_CLEAR || virt_key > VK_DELETE)
3829 return virt_key;
da36a4d6 3830
ccc2d29c 3831 if (virt_key == VK_RETURN)
da36a4d6
GV
3832 return (extended ? VK_NUMPAD_ENTER : VK_RETURN);
3833
ccc2d29c
GV
3834 if (virt_key >= VK_PRIOR && virt_key <= VK_DOWN)
3835 return (!extended ? (VK_NUMPAD_PRIOR + (virt_key - VK_PRIOR)) : virt_key);
3836
3837 if (virt_key == VK_INSERT || virt_key == VK_DELETE)
3838 return (!extended ? (VK_NUMPAD_INSERT + (virt_key - VK_INSERT)) : virt_key);
3839
3840 if (virt_key == VK_CLEAR)
3841 return (!extended ? VK_NUMPAD_CLEAR : virt_key);
3842
3843 return virt_key;
3844}
3845
3846/* List of special key combinations which w32 would normally capture,
3847 but emacs should grab instead. Not directly visible to lisp, to
3848 simplify synchronization. Each item is an integer encoding a virtual
3849 key code and modifier combination to capture. */
3850Lisp_Object w32_grabbed_keys;
3851
3852#define HOTKEY(vk,mods) make_number (((vk) & 255) | ((mods) << 8))
3853#define HOTKEY_ID(k) (XFASTINT (k) & 0xbfff)
3854#define HOTKEY_VK_CODE(k) (XFASTINT (k) & 255)
3855#define HOTKEY_MODIFIERS(k) (XFASTINT (k) >> 8)
3856
3857/* Register hot-keys for reserved key combinations when Emacs has
3858 keyboard focus, since this is the only way Emacs can receive key
3859 combinations like Alt-Tab which are used by the system. */
3860
3861static void
3862register_hot_keys (hwnd)
3863 HWND hwnd;
3864{
3865 Lisp_Object keylist;
3866
3867 /* Use GC_CONSP, since we are called asynchronously. */
3868 for (keylist = w32_grabbed_keys; GC_CONSP (keylist); keylist = XCDR (keylist))
3869 {
3870 Lisp_Object key = XCAR (keylist);
3871
3872 /* Deleted entries get set to nil. */
3873 if (!INTEGERP (key))
3874 continue;
3875
3876 RegisterHotKey (hwnd, HOTKEY_ID (key),
3877 HOTKEY_MODIFIERS (key), HOTKEY_VK_CODE (key));
3878 }
3879}
3880
3881static void
3882unregister_hot_keys (hwnd)
3883 HWND hwnd;
3884{
3885 Lisp_Object keylist;
3886
3887 /* Use GC_CONSP, since we are called asynchronously. */
3888 for (keylist = w32_grabbed_keys; GC_CONSP (keylist); keylist = XCDR (keylist))
3889 {
3890 Lisp_Object key = XCAR (keylist);
3891
3892 if (!INTEGERP (key))
3893 continue;
3894
3895 UnregisterHotKey (hwnd, HOTKEY_ID (key));
3896 }
3897}
3898
5ac45f98
GV
3899/* Main message dispatch loop. */
3900
1edf84e7
GV
3901static void
3902w32_msg_pump (deferred_msg * msg_buf)
5ac45f98
GV
3903{
3904 MSG msg;
ccc2d29c
GV
3905 int result;
3906 HWND focus_window;
93fbe8b7
GV
3907
3908 msh_mousewheel = RegisterWindowMessage (MSH_MOUSEWHEEL);
5ac45f98 3909
5ac45f98
GV
3910 while (GetMessage (&msg, NULL, 0, 0))
3911 {
3912 if (msg.hwnd == NULL)
3913 {
3914 switch (msg.message)
3915 {
3ef68e6b
AI
3916 case WM_NULL:
3917 /* Produced by complete_deferred_msg; just ignore. */
3918 break;
5ac45f98 3919 case WM_EMACS_CREATEWINDOW:
fbd6baed 3920 w32_createwindow ((struct frame *) msg.wParam);
1edf84e7
GV
3921 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
3922 abort ();
5ac45f98 3923 break;
dfdb4047
GV
3924 case WM_EMACS_SETLOCALE:
3925 SetThreadLocale (msg.wParam);
3926 /* Reply is not expected. */
3927 break;
ccc2d29c
GV
3928 case WM_EMACS_SETKEYBOARDLAYOUT:
3929 result = (int) ActivateKeyboardLayout ((HKL) msg.wParam, 0);
3930 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
3931 result, 0))
3932 abort ();
3933 break;
3934 case WM_EMACS_REGISTER_HOT_KEY:
3935 focus_window = GetFocus ();
3936 if (focus_window != NULL)
3937 RegisterHotKey (focus_window,
3938 HOTKEY_ID (msg.wParam),
3939 HOTKEY_MODIFIERS (msg.wParam),
3940 HOTKEY_VK_CODE (msg.wParam));
3941 /* Reply is not expected. */
3942 break;
3943 case WM_EMACS_UNREGISTER_HOT_KEY:
3944 focus_window = GetFocus ();
3945 if (focus_window != NULL)
3946 UnregisterHotKey (focus_window, HOTKEY_ID (msg.wParam));
adcc3809
GV
3947 /* Mark item as erased. NB: this code must be
3948 thread-safe. The next line is okay because the cons
3949 cell is never made into garbage and is not relocated by
3950 GC. */
f3fbd155 3951 XSETCAR ((Lisp_Object) msg.lParam, Qnil);
ccc2d29c
GV
3952 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
3953 abort ();
3954 break;
adcc3809
GV
3955 case WM_EMACS_TOGGLE_LOCK_KEY:
3956 {
3957 int vk_code = (int) msg.wParam;
3958 int cur_state = (GetKeyState (vk_code) & 1);
3959 Lisp_Object new_state = (Lisp_Object) msg.lParam;
3960
3961 /* NB: This code must be thread-safe. It is safe to
3962 call NILP because symbols are not relocated by GC,
3963 and pointer here is not touched by GC (so the markbit
3964 can't be set). Numbers are safe because they are
3965 immediate values. */
3966 if (NILP (new_state)
3967 || (NUMBERP (new_state)
8edb0a6f 3968 && ((XUINT (new_state)) & 1) != cur_state))
adcc3809
GV
3969 {
3970 one_w32_display_info.faked_key = vk_code;
3971
3972 keybd_event ((BYTE) vk_code,
3973 (BYTE) MapVirtualKey (vk_code, 0),
3974 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
3975 keybd_event ((BYTE) vk_code,
3976 (BYTE) MapVirtualKey (vk_code, 0),
3977 KEYEVENTF_EXTENDEDKEY | 0, 0);
3978 keybd_event ((BYTE) vk_code,
3979 (BYTE) MapVirtualKey (vk_code, 0),
3980 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
3981 cur_state = !cur_state;
3982 }
3983 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
3984 cur_state, 0))
3985 abort ();
3986 }
3987 break;
1edf84e7 3988 default:
1edf84e7 3989 DebPrint (("msg %x not expected by w32_msg_pump\n", msg.message));
5ac45f98
GV
3990 }
3991 }
3992 else
3993 {
3994 DispatchMessage (&msg);
3995 }
1edf84e7
GV
3996
3997 /* Exit nested loop when our deferred message has completed. */
3998 if (msg_buf->completed)
3999 break;
5ac45f98 4000 }
1edf84e7
GV
4001}
4002
4003deferred_msg * deferred_msg_head;
4004
4005static deferred_msg *
4006find_deferred_msg (HWND hwnd, UINT msg)
4007{
4008 deferred_msg * item;
4009
4010 /* Don't actually need synchronization for read access, since
4011 modification of single pointer is always atomic. */
4012 /* enter_crit (); */
4013
4014 for (item = deferred_msg_head; item != NULL; item = item->next)
4015 if (item->w32msg.msg.hwnd == hwnd
4016 && item->w32msg.msg.message == msg)
4017 break;
4018
4019 /* leave_crit (); */
4020
4021 return item;
4022}
4023
4024static LRESULT
4025send_deferred_msg (deferred_msg * msg_buf,
4026 HWND hwnd,
4027 UINT msg,
4028 WPARAM wParam,
4029 LPARAM lParam)
4030{
4031 /* Only input thread can send deferred messages. */
4032 if (GetCurrentThreadId () != dwWindowsThreadId)
4033 abort ();
4034
4035 /* It is an error to send a message that is already deferred. */
4036 if (find_deferred_msg (hwnd, msg) != NULL)
4037 abort ();
4038
4039 /* Enforced synchronization is not needed because this is the only
4040 function that alters deferred_msg_head, and the following critical
4041 section is guaranteed to only be serially reentered (since only the
4042 input thread can call us). */
4043
4044 /* enter_crit (); */
4045
4046 msg_buf->completed = 0;
4047 msg_buf->next = deferred_msg_head;
4048 deferred_msg_head = msg_buf;
4049 my_post_msg (&msg_buf->w32msg, hwnd, msg, wParam, lParam);
4050
4051 /* leave_crit (); */
4052
4053 /* Start a new nested message loop to process other messages until
4054 this one is completed. */
4055 w32_msg_pump (msg_buf);
4056
4057 deferred_msg_head = msg_buf->next;
4058
4059 return msg_buf->result;
4060}
4061
4062void
4063complete_deferred_msg (HWND hwnd, UINT msg, LRESULT result)
4064{
4065 deferred_msg * msg_buf = find_deferred_msg (hwnd, msg);
4066
4067 if (msg_buf == NULL)
3ef68e6b
AI
4068 /* Message may have been cancelled, so don't abort(). */
4069 return;
1edf84e7
GV
4070
4071 msg_buf->result = result;
4072 msg_buf->completed = 1;
4073
4074 /* Ensure input thread is woken so it notices the completion. */
4075 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
4076}
4077
3ef68e6b
AI
4078void
4079cancel_all_deferred_msgs ()
4080{
4081 deferred_msg * item;
4082
4083 /* Don't actually need synchronization for read access, since
4084 modification of single pointer is always atomic. */
4085 /* enter_crit (); */
4086
4087 for (item = deferred_msg_head; item != NULL; item = item->next)
4088 {
4089 item->result = 0;
4090 item->completed = 1;
4091 }
4092
4093 /* leave_crit (); */
4094
4095 /* Ensure input thread is woken so it notices the completion. */
4096 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
4097}
1edf84e7
GV
4098
4099DWORD
4100w32_msg_worker (dw)
4101 DWORD dw;
4102{
4103 MSG msg;
4104 deferred_msg dummy_buf;
4105
4106 /* Ensure our message queue is created */
4107
4108 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
5ac45f98 4109
1edf84e7
GV
4110 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
4111 abort ();
4112
4113 memset (&dummy_buf, 0, sizeof (dummy_buf));
4114 dummy_buf.w32msg.msg.hwnd = NULL;
4115 dummy_buf.w32msg.msg.message = WM_NULL;
4116
4117 /* This is the inital message loop which should only exit when the
4118 application quits. */
4119 w32_msg_pump (&dummy_buf);
4120
4121 return 0;
5ac45f98
GV
4122}
4123
3ef68e6b
AI
4124static void
4125post_character_message (hwnd, msg, wParam, lParam, modifiers)
4126 HWND hwnd;
4127 UINT msg;
4128 WPARAM wParam;
4129 LPARAM lParam;
4130 DWORD modifiers;
4131
4132{
4133 W32Msg wmsg;
4134
4135 wmsg.dwModifiers = modifiers;
4136
4137 /* Detect quit_char and set quit-flag directly. Note that we
4138 still need to post a message to ensure the main thread will be
4139 woken up if blocked in sys_select(), but we do NOT want to post
4140 the quit_char message itself (because it will usually be as if
4141 the user had typed quit_char twice). Instead, we post a dummy
4142 message that has no particular effect. */
4143 {
4144 int c = wParam;
4145 if (isalpha (c) && wmsg.dwModifiers == ctrl_modifier)
4146 c = make_ctrl_char (c) & 0377;
7d081355
AI
4147 if (c == quit_char
4148 || (wmsg.dwModifiers == 0 &&
4149 XFASTINT (Vw32_quit_key) && wParam == XFASTINT (Vw32_quit_key)))
3ef68e6b
AI
4150 {
4151 Vquit_flag = Qt;
4152
4153 /* The choice of message is somewhat arbitrary, as long as
4154 the main thread handler just ignores it. */
4155 msg = WM_NULL;
4156
4157 /* Interrupt any blocking system calls. */
4158 signal_quit ();
4159
4160 /* As a safety precaution, forcibly complete any deferred
4161 messages. This is a kludge, but I don't see any particularly
4162 clean way to handle the situation where a deferred message is
4163 "dropped" in the lisp thread, and will thus never be
4164 completed, eg. by the user trying to activate the menubar
4165 when the lisp thread is busy, and then typing C-g when the
4166 menubar doesn't open promptly (with the result that the
4167 menubar never responds at all because the deferred
4168 WM_INITMENU message is never completed). Another problem
4169 situation is when the lisp thread calls SendMessage (to send
4170 a window manager command) when a message has been deferred;
4171 the lisp thread gets blocked indefinitely waiting for the
4172 deferred message to be completed, which itself is waiting for
4173 the lisp thread to respond.
4174
4175 Note that we don't want to block the input thread waiting for
4176 a reponse from the lisp thread (although that would at least
4177 solve the deadlock problem above), because we want to be able
4178 to receive C-g to interrupt the lisp thread. */
4179 cancel_all_deferred_msgs ();
4180 }
4181 }
4182
4183 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4184}
4185
ee78dc32
GV
4186/* Main window procedure */
4187
ee78dc32 4188LRESULT CALLBACK
fbd6baed 4189w32_wnd_proc (hwnd, msg, wParam, lParam)
ee78dc32
GV
4190 HWND hwnd;
4191 UINT msg;
4192 WPARAM wParam;
4193 LPARAM lParam;
4194{
4195 struct frame *f;
fbd6baed
GV
4196 struct w32_display_info *dpyinfo = &one_w32_display_info;
4197 W32Msg wmsg;
84fb1139 4198 int windows_translate;
576ba81c 4199 int key;
84fb1139 4200
a6085637
KH
4201 /* Note that it is okay to call x_window_to_frame, even though we are
4202 not running in the main lisp thread, because frame deletion
4203 requires the lisp thread to synchronize with this thread. Thus, if
4204 a frame struct is returned, it can be used without concern that the
4205 lisp thread might make it disappear while we are using it.
4206
4207 NB. Walking the frame list in this thread is safe (as long as
4208 writes of Lisp_Object slots are atomic, which they are on Windows).
4209 Although delete-frame can destructively modify the frame list while
4210 we are walking it, a garbage collection cannot occur until after
4211 delete-frame has synchronized with this thread.
4212
4213 It is also safe to use functions that make GDI calls, such as
fbd6baed 4214 w32_clear_rect, because these functions must obtain a DC handle
a6085637
KH
4215 from the frame struct using get_frame_dc which is thread-aware. */
4216
ee78dc32
GV
4217 switch (msg)
4218 {
4219 case WM_ERASEBKGND:
a6085637
KH
4220 f = x_window_to_frame (dpyinfo, hwnd);
4221 if (f)
4222 {
9badad41 4223 HDC hdc = get_frame_dc (f);
a6085637 4224 GetUpdateRect (hwnd, &wmsg.rect, FALSE);
9badad41
JR
4225 w32_clear_rect (f, hdc, &wmsg.rect);
4226 release_frame_dc (f, hdc);
ce6059da
AI
4227
4228#if defined (W32_DEBUG_DISPLAY)
18f0b342
AI
4229 DebPrint (("WM_ERASEBKGND (frame %p): erasing %d,%d-%d,%d\n",
4230 f,
4231 wmsg.rect.left, wmsg.rect.top,
4232 wmsg.rect.right, wmsg.rect.bottom));
ce6059da 4233#endif /* W32_DEBUG_DISPLAY */
a6085637 4234 }
5ac45f98
GV
4235 return 1;
4236 case WM_PALETTECHANGED:
4237 /* ignore our own changes */
4238 if ((HWND)wParam != hwnd)
4239 {
a6085637
KH
4240 f = x_window_to_frame (dpyinfo, hwnd);
4241 if (f)
4242 /* get_frame_dc will realize our palette and force all
4243 frames to be redrawn if needed. */
4244 release_frame_dc (f, get_frame_dc (f));
5ac45f98
GV
4245 }
4246 return 0;
ee78dc32 4247 case WM_PAINT:
ce6059da 4248 {
55dcfc15
AI
4249 PAINTSTRUCT paintStruct;
4250 RECT update_rect;
aa35b6ad 4251 bzero (&update_rect, sizeof (update_rect));
55dcfc15 4252
18f0b342
AI
4253 f = x_window_to_frame (dpyinfo, hwnd);
4254 if (f == 0)
4255 {
4256 DebPrint (("WM_PAINT received for unknown window %p\n", hwnd));
4257 return 0;
4258 }
4259
55dcfc15
AI
4260 /* MSDN Docs say not to call BeginPaint if GetUpdateRect
4261 fails. Apparently this can happen under some
4262 circumstances. */
aa35b6ad 4263 if (GetUpdateRect (hwnd, &update_rect, FALSE) || !w32_strict_painting)
55dcfc15
AI
4264 {
4265 enter_crit ();
4266 BeginPaint (hwnd, &paintStruct);
4267
aa35b6ad
JR
4268 /* The rectangles returned by GetUpdateRect and BeginPaint
4269 do not always match. Play it safe by assuming both areas
4270 are invalid. */
4271 UnionRect (&(wmsg.rect), &update_rect, &(paintStruct.rcPaint));
55dcfc15
AI
4272
4273#if defined (W32_DEBUG_DISPLAY)
18f0b342
AI
4274 DebPrint (("WM_PAINT (frame %p): painting %d,%d-%d,%d\n",
4275 f,
4276 wmsg.rect.left, wmsg.rect.top,
4277 wmsg.rect.right, wmsg.rect.bottom));
4278 DebPrint ((" [update region is %d,%d-%d,%d]\n",
55dcfc15
AI
4279 update_rect.left, update_rect.top,
4280 update_rect.right, update_rect.bottom));
4281#endif
4282 EndPaint (hwnd, &paintStruct);
4283 leave_crit ();
4284
4285 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4286
4287 return 0;
4288 }
c0611964
AI
4289
4290 /* If GetUpdateRect returns 0 (meaning there is no update
4291 region), assume the whole window needs to be repainted. */
4292 GetClientRect(hwnd, &wmsg.rect);
4293 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4294 return 0;
ee78dc32 4295 }
a1a80b40 4296
ccc2d29c
GV
4297 case WM_INPUTLANGCHANGE:
4298 /* Inform lisp thread of keyboard layout changes. */
4299 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4300
4301 /* Clear dead keys in the keyboard state; for simplicity only
4302 preserve modifier key states. */
4303 {
4304 int i;
4305 BYTE keystate[256];
4306
4307 GetKeyboardState (keystate);
4308 for (i = 0; i < 256; i++)
4309 if (1
4310 && i != VK_SHIFT
4311 && i != VK_LSHIFT
4312 && i != VK_RSHIFT
4313 && i != VK_CAPITAL
4314 && i != VK_NUMLOCK
4315 && i != VK_SCROLL
4316 && i != VK_CONTROL
4317 && i != VK_LCONTROL
4318 && i != VK_RCONTROL
4319 && i != VK_MENU
4320 && i != VK_LMENU
4321 && i != VK_RMENU
4322 && i != VK_LWIN
4323 && i != VK_RWIN)
4324 keystate[i] = 0;
4325 SetKeyboardState (keystate);
4326 }
4327 goto dflt;
4328
4329 case WM_HOTKEY:
4330 /* Synchronize hot keys with normal input. */
4331 PostMessage (hwnd, WM_KEYDOWN, HIWORD (lParam), 0);
4332 return (0);
4333
a1a80b40
GV
4334 case WM_KEYUP:
4335 case WM_SYSKEYUP:
4336 record_keyup (wParam, lParam);
4337 goto dflt;
4338
ee78dc32
GV
4339 case WM_KEYDOWN:
4340 case WM_SYSKEYDOWN:
ccc2d29c
GV
4341 /* Ignore keystrokes we fake ourself; see below. */
4342 if (dpyinfo->faked_key == wParam)
4343 {
4344 dpyinfo->faked_key = 0;
576ba81c
AI
4345 /* Make sure TranslateMessage sees them though (as long as
4346 they don't produce WM_CHAR messages). This ensures that
4347 indicator lights are toggled promptly on Windows 9x, for
4348 example. */
4349 if (lispy_function_keys[wParam] != 0)
4350 {
4351 windows_translate = 1;
4352 goto translate;
4353 }
4354 return 0;
ccc2d29c
GV
4355 }
4356
7830e24b
RS
4357 /* Synchronize modifiers with current keystroke. */
4358 sync_modifiers ();
a1a80b40 4359 record_keydown (wParam, lParam);
ccc2d29c 4360 wParam = map_keypad_keys (wParam, (lParam & 0x1000000L) != 0);
84fb1139
KH
4361
4362 windows_translate = 0;
ccc2d29c
GV
4363
4364 switch (wParam)
4365 {
4366 case VK_LWIN:
4367 if (NILP (Vw32_pass_lwindow_to_system))
4368 {
4369 /* Prevent system from acting on keyup (which opens the
4370 Start menu if no other key was pressed) by simulating a
4371 press of Space which we will ignore. */
4372 if (GetAsyncKeyState (wParam) & 1)
4373 {
adcc3809 4374 if (NUMBERP (Vw32_phantom_key_code))
576ba81c 4375 key = XUINT (Vw32_phantom_key_code) & 255;
adcc3809 4376 else
576ba81c
AI
4377 key = VK_SPACE;
4378 dpyinfo->faked_key = key;
4379 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
ccc2d29c
GV
4380 }
4381 }
4382 if (!NILP (Vw32_lwindow_modifier))
4383 return 0;
4384 break;
4385 case VK_RWIN:
4386 if (NILP (Vw32_pass_rwindow_to_system))
4387 {
4388 if (GetAsyncKeyState (wParam) & 1)
4389 {
adcc3809 4390 if (NUMBERP (Vw32_phantom_key_code))
576ba81c 4391 key = XUINT (Vw32_phantom_key_code) & 255;
adcc3809 4392 else
576ba81c
AI
4393 key = VK_SPACE;
4394 dpyinfo->faked_key = key;
4395 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
ccc2d29c
GV
4396 }
4397 }
4398 if (!NILP (Vw32_rwindow_modifier))
4399 return 0;
4400 break;
576ba81c 4401 case VK_APPS:
ccc2d29c
GV
4402 if (!NILP (Vw32_apps_modifier))
4403 return 0;
4404 break;
4405 case VK_MENU:
4406 if (NILP (Vw32_pass_alt_to_system))
adcc3809
GV
4407 /* Prevent DefWindowProc from activating the menu bar if an
4408 Alt key is pressed and released by itself. */
ccc2d29c 4409 return 0;
84fb1139 4410 windows_translate = 1;
ccc2d29c
GV
4411 break;
4412 case VK_CAPITAL:
4413 /* Decide whether to treat as modifier or function key. */
4414 if (NILP (Vw32_enable_caps_lock))
4415 goto disable_lock_key;
adcc3809
GV
4416 windows_translate = 1;
4417 break;
ccc2d29c
GV
4418 case VK_NUMLOCK:
4419 /* Decide whether to treat as modifier or function key. */
4420 if (NILP (Vw32_enable_num_lock))
4421 goto disable_lock_key;
adcc3809
GV
4422 windows_translate = 1;
4423 break;
ccc2d29c
GV
4424 case VK_SCROLL:
4425 /* Decide whether to treat as modifier or function key. */
4426 if (NILP (Vw32_scroll_lock_modifier))
4427 goto disable_lock_key;
adcc3809
GV
4428 windows_translate = 1;
4429 break;
ccc2d29c 4430 disable_lock_key:
adcc3809
GV
4431 /* Ensure the appropriate lock key state (and indicator light)
4432 remains in the same state. We do this by faking another
4433 press of the relevant key. Apparently, this really is the
4434 only way to toggle the state of the indicator lights. */
4435 dpyinfo->faked_key = wParam;
4436 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
4437 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
4438 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
4439 KEYEVENTF_EXTENDEDKEY | 0, 0);
4440 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
4441 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
4442 /* Ensure indicator lights are updated promptly on Windows 9x
4443 (TranslateMessage apparently does this), after forwarding
4444 input event. */
4445 post_character_message (hwnd, msg, wParam, lParam,
4446 w32_get_key_modifiers (wParam, lParam));
4447 windows_translate = 1;
ccc2d29c
GV
4448 break;
4449 case VK_CONTROL:
4450 case VK_SHIFT:
4451 case VK_PROCESSKEY: /* Generated by IME. */
4452 windows_translate = 1;
4453 break;
adcc3809
GV
4454 case VK_CANCEL:
4455 /* Windows maps Ctrl-Pause (aka Ctrl-Break) into VK_CANCEL,
4456 which is confusing for purposes of key binding; convert
4457 VK_CANCEL events into VK_PAUSE events. */
4458 wParam = VK_PAUSE;
4459 break;
4460 case VK_PAUSE:
4461 /* Windows maps Ctrl-NumLock into VK_PAUSE, which is confusing
4462 for purposes of key binding; convert these back into
4463 VK_NUMLOCK events, at least when we want to see NumLock key
4464 presses. (Note that there is never any possibility that
4465 VK_PAUSE with Ctrl really is C-Pause as per above.) */
4466 if (NILP (Vw32_enable_num_lock) && modifier_set (VK_CONTROL))
4467 wParam = VK_NUMLOCK;
4468 break;
ccc2d29c
GV
4469 default:
4470 /* If not defined as a function key, change it to a WM_CHAR message. */
4471 if (lispy_function_keys[wParam] == 0)
4472 {
adcc3809
GV
4473 DWORD modifiers = construct_console_modifiers ();
4474
ccc2d29c
GV
4475 if (!NILP (Vw32_recognize_altgr)
4476 && modifier_set (VK_LCONTROL) && modifier_set (VK_RMENU))
4477 {
4478 /* Always let TranslateMessage handle AltGr key chords;
4479 for some reason, ToAscii doesn't always process AltGr
4480 chords correctly. */
4481 windows_translate = 1;
4482 }
adcc3809 4483 else if ((modifiers & (~SHIFT_PRESSED & ~CAPSLOCK_ON)) != 0)
ccc2d29c 4484 {
adcc3809
GV
4485 /* Handle key chords including any modifiers other
4486 than shift directly, in order to preserve as much
4487 modifier information as possible. */
ccc2d29c
GV
4488 if ('A' <= wParam && wParam <= 'Z')
4489 {
4490 /* Don't translate modified alphabetic keystrokes,
4491 so the user doesn't need to constantly switch
4492 layout to type control or meta keystrokes when
4493 the normal layout translates alphabetic
4494 characters to non-ascii characters. */
4495 if (!modifier_set (VK_SHIFT))
4496 wParam += ('a' - 'A');
4497 msg = WM_CHAR;
4498 }
4499 else
4500 {
4501 /* Try to handle other keystrokes by determining the
4502 base character (ie. translating the base key plus
4503 shift modifier). */
4504 int add;
4505 int isdead = 0;
4506 KEY_EVENT_RECORD key;
4507
4508 key.bKeyDown = TRUE;
4509 key.wRepeatCount = 1;
4510 key.wVirtualKeyCode = wParam;
4511 key.wVirtualScanCode = (lParam & 0xFF0000) >> 16;
4512 key.uChar.AsciiChar = 0;
adcc3809 4513 key.dwControlKeyState = modifiers;
ccc2d29c
GV
4514
4515 add = w32_kbd_patch_key (&key);
4516 /* 0 means an unrecognised keycode, negative means
4517 dead key. Ignore both. */
4518 while (--add >= 0)
4519 {
4520 /* Forward asciified character sequence. */
4521 post_character_message
4522 (hwnd, WM_CHAR, key.uChar.AsciiChar, lParam,
4523 w32_get_key_modifiers (wParam, lParam));
4524 w32_kbd_patch_key (&key);
4525 }
4526 return 0;
4527 }
4528 }
4529 else
4530 {
4531 /* Let TranslateMessage handle everything else. */
4532 windows_translate = 1;
4533 }
4534 }
4535 }
a1a80b40 4536
adcc3809 4537 translate:
84fb1139
KH
4538 if (windows_translate)
4539 {
e9e23e23 4540 MSG windows_msg = { hwnd, msg, wParam, lParam, 0, {0,0} };
84fb1139 4541
e9e23e23
GV
4542 windows_msg.time = GetMessageTime ();
4543 TranslateMessage (&windows_msg);
84fb1139
KH
4544 goto dflt;
4545 }
4546
ee78dc32
GV
4547 /* Fall through */
4548
4549 case WM_SYSCHAR:
4550 case WM_CHAR:
ccc2d29c
GV
4551 post_character_message (hwnd, msg, wParam, lParam,
4552 w32_get_key_modifiers (wParam, lParam));
ee78dc32 4553 break;
da36a4d6 4554
5ac45f98
GV
4555 /* Simulate middle mouse button events when left and right buttons
4556 are used together, but only if user has two button mouse. */
ee78dc32 4557 case WM_LBUTTONDOWN:
5ac45f98 4558 case WM_RBUTTONDOWN:
7ce9aaca 4559 if (XINT (Vw32_num_mouse_buttons) > 2)
5ac45f98
GV
4560 goto handle_plain_button;
4561
4562 {
4563 int this = (msg == WM_LBUTTONDOWN) ? LMOUSE : RMOUSE;
4564 int other = (msg == WM_LBUTTONDOWN) ? RMOUSE : LMOUSE;
4565
3cb20f4a
RS
4566 if (button_state & this)
4567 return 0;
5ac45f98
GV
4568
4569 if (button_state == 0)
4570 SetCapture (hwnd);
4571
4572 button_state |= this;
4573
4574 if (button_state & other)
4575 {
84fb1139 4576 if (mouse_button_timer)
5ac45f98 4577 {
84fb1139
KH
4578 KillTimer (hwnd, mouse_button_timer);
4579 mouse_button_timer = 0;
5ac45f98
GV
4580
4581 /* Generate middle mouse event instead. */
4582 msg = WM_MBUTTONDOWN;
4583 button_state |= MMOUSE;
4584 }
4585 else if (button_state & MMOUSE)
4586 {
4587 /* Ignore button event if we've already generated a
4588 middle mouse down event. This happens if the
4589 user releases and press one of the two buttons
4590 after we've faked a middle mouse event. */
4591 return 0;
4592 }
4593 else
4594 {
4595 /* Flush out saved message. */
84fb1139 4596 post_msg (&saved_mouse_button_msg);
5ac45f98 4597 }
fbd6baed 4598 wmsg.dwModifiers = w32_get_modifiers ();
5ac45f98
GV
4599 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4600
4601 /* Clear message buffer. */
84fb1139 4602 saved_mouse_button_msg.msg.hwnd = 0;
5ac45f98
GV
4603 }
4604 else
4605 {
4606 /* Hold onto message for now. */
84fb1139 4607 mouse_button_timer =
adcc3809
GV
4608 SetTimer (hwnd, MOUSE_BUTTON_ID,
4609 XINT (Vw32_mouse_button_tolerance), NULL);
84fb1139
KH
4610 saved_mouse_button_msg.msg.hwnd = hwnd;
4611 saved_mouse_button_msg.msg.message = msg;
4612 saved_mouse_button_msg.msg.wParam = wParam;
4613 saved_mouse_button_msg.msg.lParam = lParam;
4614 saved_mouse_button_msg.msg.time = GetMessageTime ();
fbd6baed 4615 saved_mouse_button_msg.dwModifiers = w32_get_modifiers ();
5ac45f98
GV
4616 }
4617 }
4618 return 0;
4619
ee78dc32 4620 case WM_LBUTTONUP:
5ac45f98 4621 case WM_RBUTTONUP:
7ce9aaca 4622 if (XINT (Vw32_num_mouse_buttons) > 2)
5ac45f98
GV
4623 goto handle_plain_button;
4624
4625 {
4626 int this = (msg == WM_LBUTTONUP) ? LMOUSE : RMOUSE;
4627 int other = (msg == WM_LBUTTONUP) ? RMOUSE : LMOUSE;
4628
3cb20f4a
RS
4629 if ((button_state & this) == 0)
4630 return 0;
5ac45f98
GV
4631
4632 button_state &= ~this;
4633
4634 if (button_state & MMOUSE)
4635 {
4636 /* Only generate event when second button is released. */
4637 if ((button_state & other) == 0)
4638 {
4639 msg = WM_MBUTTONUP;
4640 button_state &= ~MMOUSE;
4641
4642 if (button_state) abort ();
4643 }
4644 else
4645 return 0;
4646 }
4647 else
4648 {
4649 /* Flush out saved message if necessary. */
84fb1139 4650 if (saved_mouse_button_msg.msg.hwnd)
5ac45f98 4651 {
84fb1139 4652 post_msg (&saved_mouse_button_msg);
5ac45f98
GV
4653 }
4654 }
fbd6baed 4655 wmsg.dwModifiers = w32_get_modifiers ();
5ac45f98
GV
4656 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4657
4658 /* Always clear message buffer and cancel timer. */
84fb1139
KH
4659 saved_mouse_button_msg.msg.hwnd = 0;
4660 KillTimer (hwnd, mouse_button_timer);
4661 mouse_button_timer = 0;
5ac45f98
GV
4662
4663 if (button_state == 0)
4664 ReleaseCapture ();
4665 }
4666 return 0;
4667
74214547
JR
4668 case WM_XBUTTONDOWN:
4669 case WM_XBUTTONUP:
4670 if (w32_pass_extra_mouse_buttons_to_system)
4671 goto dflt;
4672 /* else fall through and process them. */
ee78dc32
GV
4673 case WM_MBUTTONDOWN:
4674 case WM_MBUTTONUP:
5ac45f98 4675 handle_plain_button:
ee78dc32
GV
4676 {
4677 BOOL up;
1edf84e7 4678 int button;
ee78dc32 4679
74214547 4680 if (parse_button (msg, HIWORD (wParam), &button, &up))
ee78dc32
GV
4681 {
4682 if (up) ReleaseCapture ();
4683 else SetCapture (hwnd);
1edf84e7
GV
4684 button = (button == 0) ? LMOUSE :
4685 ((button == 1) ? MMOUSE : RMOUSE);
4686 if (up)
4687 button_state &= ~button;
4688 else
4689 button_state |= button;
ee78dc32
GV
4690 }
4691 }
4692
fbd6baed 4693 wmsg.dwModifiers = w32_get_modifiers ();
ee78dc32 4694 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
74214547
JR
4695
4696 /* Need to return true for XBUTTON messages, false for others,
4697 to indicate that we processed the message. */
4698 return (msg == WM_XBUTTONDOWN || msg == WM_XBUTTONUP);
5ac45f98 4699
5ac45f98 4700 case WM_MOUSEMOVE:
9eb16b62
JR
4701 /* If the mouse has just moved into the frame, start tracking
4702 it, so we will be notified when it leaves the frame. Mouse
4703 tracking only works under W98 and NT4 and later. On earlier
4704 versions, there is no way of telling when the mouse leaves the
4705 frame, so we just have to put up with help-echo and mouse
4706 highlighting remaining while the frame is not active. */
4707 if (track_mouse_event_fn && !track_mouse_window)
4708 {
4709 TRACKMOUSEEVENT tme;
4710 tme.cbSize = sizeof (tme);
4711 tme.dwFlags = TME_LEAVE;
4712 tme.hwndTrack = hwnd;
4713
4714 track_mouse_event_fn (&tme);
4715 track_mouse_window = hwnd;
4716 }
4717 case WM_VSCROLL:
fbd6baed 4718 if (XINT (Vw32_mouse_move_interval) <= 0
84fb1139
KH
4719 || (msg == WM_MOUSEMOVE && button_state == 0))
4720 {
fbd6baed 4721 wmsg.dwModifiers = w32_get_modifiers ();
84fb1139
KH
4722 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4723 return 0;
4724 }
4725
4726 /* Hang onto mouse move and scroll messages for a bit, to avoid
4727 sending such events to Emacs faster than it can process them.
4728 If we get more events before the timer from the first message
4729 expires, we just replace the first message. */
4730
4731 if (saved_mouse_move_msg.msg.hwnd == 0)
4732 mouse_move_timer =
adcc3809
GV
4733 SetTimer (hwnd, MOUSE_MOVE_ID,
4734 XINT (Vw32_mouse_move_interval), NULL);
84fb1139
KH
4735
4736 /* Hold onto message for now. */
4737 saved_mouse_move_msg.msg.hwnd = hwnd;
4738 saved_mouse_move_msg.msg.message = msg;
4739 saved_mouse_move_msg.msg.wParam = wParam;
4740 saved_mouse_move_msg.msg.lParam = lParam;
4741 saved_mouse_move_msg.msg.time = GetMessageTime ();
fbd6baed 4742 saved_mouse_move_msg.dwModifiers = w32_get_modifiers ();
84fb1139
KH
4743
4744 return 0;
4745
1edf84e7
GV
4746 case WM_MOUSEWHEEL:
4747 wmsg.dwModifiers = w32_get_modifiers ();
4748 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4749 return 0;
4750
cb9e33d4
RS
4751 case WM_DROPFILES:
4752 wmsg.dwModifiers = w32_get_modifiers ();
4753 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4754 return 0;
4755
84fb1139
KH
4756 case WM_TIMER:
4757 /* Flush out saved messages if necessary. */
4758 if (wParam == mouse_button_timer)
5ac45f98 4759 {
84fb1139
KH
4760 if (saved_mouse_button_msg.msg.hwnd)
4761 {
4762 post_msg (&saved_mouse_button_msg);
4763 saved_mouse_button_msg.msg.hwnd = 0;
4764 }
4765 KillTimer (hwnd, mouse_button_timer);
4766 mouse_button_timer = 0;
4767 }
4768 else if (wParam == mouse_move_timer)
4769 {
4770 if (saved_mouse_move_msg.msg.hwnd)
4771 {
4772 post_msg (&saved_mouse_move_msg);
4773 saved_mouse_move_msg.msg.hwnd = 0;
4774 }
4775 KillTimer (hwnd, mouse_move_timer);
4776 mouse_move_timer = 0;
5ac45f98 4777 }
48094ace
JR
4778 else if (wParam == menu_free_timer)
4779 {
4780 KillTimer (hwnd, menu_free_timer);
4781 menu_free_timer = 0;
27605fa7 4782 f = x_window_to_frame (dpyinfo, hwnd);
48094ace
JR
4783 if (!f->output_data.w32->menu_command_in_progress)
4784 {
4785 /* Free memory used by owner-drawn and help-echo strings. */
4786 w32_free_menu_strings (hwnd);
4787 f->output_data.w32->menubar_active = 0;
4788 }
4789 }
5ac45f98 4790 return 0;
84fb1139
KH
4791
4792 case WM_NCACTIVATE:
4793 /* Windows doesn't send us focus messages when putting up and
e9e23e23 4794 taking down a system popup dialog as for Ctrl-Alt-Del on Windows 95.
84fb1139
KH
4795 The only indication we get that something happened is receiving
4796 this message afterwards. So this is a good time to reset our
4797 keyboard modifiers' state. */
4798 reset_modifiers ();
4799 goto dflt;
da36a4d6 4800
1edf84e7 4801 case WM_INITMENU:
487163ac
AI
4802 button_state = 0;
4803 ReleaseCapture ();
1edf84e7
GV
4804 /* We must ensure menu bar is fully constructed and up to date
4805 before allowing user interaction with it. To achieve this
4806 we send this message to the lisp thread and wait for a
4807 reply (whose value is not actually needed) to indicate that
4808 the menu bar is now ready for use, so we can now return.
4809
4810 To remain responsive in the meantime, we enter a nested message
4811 loop that can process all other messages.
4812
4813 However, we skip all this if the message results from calling
4814 TrackPopupMenu - in fact, we must NOT attempt to send the lisp
4815 thread a message because it is blocked on us at this point. We
4816 set menubar_active before calling TrackPopupMenu to indicate
4817 this (there is no possibility of confusion with real menubar
4818 being active). */
4819
4820 f = x_window_to_frame (dpyinfo, hwnd);
4821 if (f
4822 && (f->output_data.w32->menubar_active
4823 /* We can receive this message even in the absence of a
4824 menubar (ie. when the system menu is activated) - in this
4825 case we do NOT want to forward the message, otherwise it
4826 will cause the menubar to suddenly appear when the user
4827 had requested it to be turned off! */
4828 || f->output_data.w32->menubar_widget == NULL))
4829 return 0;
4830
4831 {
4832 deferred_msg msg_buf;
4833
4834 /* Detect if message has already been deferred; in this case
4835 we cannot return any sensible value to ignore this. */
4836 if (find_deferred_msg (hwnd, msg) != NULL)
4837 abort ();
4838
4839 return send_deferred_msg (&msg_buf, hwnd, msg, wParam, lParam);
4840 }
4841
4842 case WM_EXITMENULOOP:
4843 f = x_window_to_frame (dpyinfo, hwnd);
4844
48094ace
JR
4845 /* If a menu command is not already in progress, check again
4846 after a short delay, since Windows often (always?) sends the
4847 WM_EXITMENULOOP before the corresponding WM_COMMAND message. */
4848 if (f && !f->output_data.w32->menu_command_in_progress)
4849 menu_free_timer = SetTimer (hwnd, MENU_FREE_ID, MENU_FREE_DELAY, NULL);
1edf84e7
GV
4850 goto dflt;
4851
126f2e35 4852 case WM_MENUSELECT:
4e3a1c61
JR
4853 /* Direct handling of help_echo in menus. Should be safe now
4854 that we generate the help_echo by placing a help event in the
4855 keyboard buffer. */
ca56d953 4856 {
ca56d953
JR
4857 HMENU menu = (HMENU) lParam;
4858 UINT menu_item = (UINT) LOWORD (wParam);
4859 UINT flags = (UINT) HIWORD (wParam);
4860
4e3a1c61 4861 w32_menu_display_help (hwnd, menu, menu_item, flags);
ca56d953 4862 }
126f2e35
JR
4863 return 0;
4864
87996783
GV
4865 case WM_MEASUREITEM:
4866 f = x_window_to_frame (dpyinfo, hwnd);
4867 if (f)
4868 {
4869 MEASUREITEMSTRUCT * pMis = (MEASUREITEMSTRUCT *) lParam;
4870
4871 if (pMis->CtlType == ODT_MENU)
4872 {
4873 /* Work out dimensions for popup menu titles. */
4874 char * title = (char *) pMis->itemData;
4875 HDC hdc = GetDC (hwnd);
4876 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
4877 LOGFONT menu_logfont;
4878 HFONT old_font;
4879 SIZE size;
4880
4881 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
4882 menu_logfont.lfWeight = FW_BOLD;
4883 menu_font = CreateFontIndirect (&menu_logfont);
4884 old_font = SelectObject (hdc, menu_font);
4885
dfff8a69
JR
4886 pMis->itemHeight = GetSystemMetrics (SM_CYMENUSIZE);
4887 if (title)
4888 {
4889 GetTextExtentPoint32 (hdc, title, strlen (title), &size);
4890 pMis->itemWidth = size.cx;
4891 if (pMis->itemHeight < size.cy)
4892 pMis->itemHeight = size.cy;
4893 }
4894 else
4895 pMis->itemWidth = 0;
87996783
GV
4896
4897 SelectObject (hdc, old_font);
4898 DeleteObject (menu_font);
4899 ReleaseDC (hwnd, hdc);
4900 return TRUE;
4901 }
4902 }
4903 return 0;
4904
4905 case WM_DRAWITEM:
4906 f = x_window_to_frame (dpyinfo, hwnd);
4907 if (f)
4908 {
4909 DRAWITEMSTRUCT * pDis = (DRAWITEMSTRUCT *) lParam;
4910
4911 if (pDis->CtlType == ODT_MENU)
4912 {
4913 /* Draw popup menu title. */
4914 char * title = (char *) pDis->itemData;
212da13b
JR
4915 if (title)
4916 {
4917 HDC hdc = pDis->hDC;
4918 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
4919 LOGFONT menu_logfont;
4920 HFONT old_font;
4921
4922 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
4923 menu_logfont.lfWeight = FW_BOLD;
4924 menu_font = CreateFontIndirect (&menu_logfont);
4925 old_font = SelectObject (hdc, menu_font);
4926
4927 /* Always draw title as if not selected. */
4928 ExtTextOut (hdc,
4929 pDis->rcItem.left
4930 + GetSystemMetrics (SM_CXMENUCHECK),
4931 pDis->rcItem.top,
4932 ETO_OPAQUE, &pDis->rcItem,
4933 title, strlen (title), NULL);
4934
4935 SelectObject (hdc, old_font);
4936 DeleteObject (menu_font);
4937 }
87996783
GV
4938 return TRUE;
4939 }
4940 }
4941 return 0;
4942
1edf84e7
GV
4943#if 0
4944 /* Still not right - can't distinguish between clicks in the
4945 client area of the frame from clicks forwarded from the scroll
4946 bars - may have to hook WM_NCHITTEST to remember the mouse
4947 position and then check if it is in the client area ourselves. */
4948 case WM_MOUSEACTIVATE:
4949 /* Discard the mouse click that activates a frame, allowing the
4950 user to click anywhere without changing point (or worse!).
4951 Don't eat mouse clicks on scrollbars though!! */
4952 if (LOWORD (lParam) == HTCLIENT )
4953 return MA_ACTIVATEANDEAT;
4954 goto dflt;
4955#endif
4956
9eb16b62
JR
4957 case WM_MOUSELEAVE:
4958 /* No longer tracking mouse. */
4959 track_mouse_window = NULL;
4960
1edf84e7 4961 case WM_ACTIVATEAPP:
ccc2d29c 4962 case WM_ACTIVATE:
1edf84e7
GV
4963 case WM_WINDOWPOSCHANGED:
4964 case WM_SHOWWINDOW:
4965 /* Inform lisp thread that a frame might have just been obscured
4966 or exposed, so should recheck visibility of all frames. */
4967 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4968 goto dflt;
4969
da36a4d6 4970 case WM_SETFOCUS:
adcc3809
GV
4971 dpyinfo->faked_key = 0;
4972 reset_modifiers ();
ccc2d29c
GV
4973 register_hot_keys (hwnd);
4974 goto command;
8681157a 4975 case WM_KILLFOCUS:
ccc2d29c 4976 unregister_hot_keys (hwnd);
487163ac
AI
4977 button_state = 0;
4978 ReleaseCapture ();
65906840
JR
4979 /* Relinquish the system caret. */
4980 if (w32_system_caret_hwnd)
4981 {
93f2ca61 4982 w32_visible_system_caret_hwnd = NULL;
d285988b
JR
4983 w32_system_caret_hwnd = NULL;
4984 DestroyCaret ();
65906840 4985 }
48094ace
JR
4986 goto command;
4987 case WM_COMMAND:
4988 f = x_window_to_frame (dpyinfo, hwnd);
4989 if (f && HIWORD (wParam) == 0)
4990 {
4991 f->output_data.w32->menu_command_in_progress = 1;
4992 if (menu_free_timer)
4993 {
4994 KillTimer (hwnd, menu_free_timer);
4995 menu_free_timer = 0;
4996 }
4997 }
ee78dc32
GV
4998 case WM_MOVE:
4999 case WM_SIZE:
ccc2d29c 5000 command:
fbd6baed 5001 wmsg.dwModifiers = w32_get_modifiers ();
ee78dc32
GV
5002 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
5003 goto dflt;
8847d890
RS
5004
5005 case WM_CLOSE:
fbd6baed 5006 wmsg.dwModifiers = w32_get_modifiers ();
8847d890
RS
5007 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
5008 return 0;
5009
ee78dc32 5010 case WM_WINDOWPOSCHANGING:
bfd6edcc
JR
5011 /* Don't restrict the sizing of tip frames. */
5012 if (hwnd == tip_window)
5013 return 0;
ee78dc32
GV
5014 {
5015 WINDOWPLACEMENT wp;
5016 LPWINDOWPOS lppos = (WINDOWPOS *) lParam;
1edf84e7
GV
5017
5018 wp.length = sizeof (WINDOWPLACEMENT);
ee78dc32
GV
5019 GetWindowPlacement (hwnd, &wp);
5020
1edf84e7 5021 if (wp.showCmd != SW_SHOWMINIMIZED && (lppos->flags & SWP_NOSIZE) == 0)
ee78dc32
GV
5022 {
5023 RECT rect;
5024 int wdiff;
5025 int hdiff;
1edf84e7
GV
5026 DWORD font_width;
5027 DWORD line_height;
5028 DWORD internal_border;
5029 DWORD scrollbar_extra;
ee78dc32
GV
5030 RECT wr;
5031
5ac45f98 5032 wp.length = sizeof(wp);
ee78dc32
GV
5033 GetWindowRect (hwnd, &wr);
5034
3c190163 5035 enter_crit ();
ee78dc32 5036
1edf84e7
GV
5037 font_width = GetWindowLong (hwnd, WND_FONTWIDTH_INDEX);
5038 line_height = GetWindowLong (hwnd, WND_LINEHEIGHT_INDEX);
5039 internal_border = GetWindowLong (hwnd, WND_BORDER_INDEX);
5040 scrollbar_extra = GetWindowLong (hwnd, WND_SCROLLBAR_INDEX);
ee78dc32 5041
3c190163 5042 leave_crit ();
ee78dc32
GV
5043
5044 memset (&rect, 0, sizeof (rect));
5045 AdjustWindowRect (&rect, GetWindowLong (hwnd, GWL_STYLE),
5046 GetMenu (hwnd) != NULL);
5047
1edf84e7
GV
5048 /* Force width and height of client area to be exact
5049 multiples of the character cell dimensions. */
5050 wdiff = (lppos->cx - (rect.right - rect.left)
5051 - 2 * internal_border - scrollbar_extra)
5052 % font_width;
5053 hdiff = (lppos->cy - (rect.bottom - rect.top)
5054 - 2 * internal_border)
5055 % line_height;
ee78dc32
GV
5056
5057 if (wdiff || hdiff)
5058 {
5059 /* For right/bottom sizing we can just fix the sizes.
5060 However for top/left sizing we will need to fix the X
5061 and Y positions as well. */
5062
5063 lppos->cx -= wdiff;
5064 lppos->cy -= hdiff;
5065
5066 if (wp.showCmd != SW_SHOWMAXIMIZED
1edf84e7 5067 && (lppos->flags & SWP_NOMOVE) == 0)
ee78dc32
GV
5068 {
5069 if (lppos->x != wr.left || lppos->y != wr.top)
5070 {
5071 lppos->x += wdiff;
5072 lppos->y += hdiff;
5073 }
5074 else
5075 {
5076 lppos->flags |= SWP_NOMOVE;
5077 }
5078 }
5079
1edf84e7 5080 return 0;
ee78dc32
GV
5081 }
5082 }
5083 }
ee78dc32
GV
5084
5085 goto dflt;
1edf84e7 5086
b1f918f8
GV
5087 case WM_GETMINMAXINFO:
5088 /* Hack to correct bug that allows Emacs frames to be resized
5089 below the Minimum Tracking Size. */
5090 ((LPMINMAXINFO) lParam)->ptMinTrackSize.y++;
bf853fee
AI
5091 /* Hack to allow resizing the Emacs frame above the screen size.
5092 Note that Windows 9x limits coordinates to 16-bits. */
5093 ((LPMINMAXINFO) lParam)->ptMaxTrackSize.x = 32767;
5094 ((LPMINMAXINFO) lParam)->ptMaxTrackSize.y = 32767;
b1f918f8
GV
5095 return 0;
5096
1edf84e7
GV
5097 case WM_EMACS_CREATESCROLLBAR:
5098 return (LRESULT) w32_createscrollbar ((struct frame *) wParam,
5099 (struct scroll_bar *) lParam);
5100
5ac45f98 5101 case WM_EMACS_SHOWWINDOW:
1edf84e7
GV
5102 return ShowWindow ((HWND) wParam, (WPARAM) lParam);
5103
dfdb4047 5104 case WM_EMACS_SETFOREGROUND:
ce6059da
AI
5105 {
5106 HWND foreground_window;
5107 DWORD foreground_thread, retval;
5108
5109 /* On NT 5.0, and apparently Windows 98, it is necessary to
5110 attach to the thread that currently has focus in order to
5111 pull the focus away from it. */
5112 foreground_window = GetForegroundWindow ();
5113 foreground_thread = GetWindowThreadProcessId (foreground_window, NULL);
5114 if (!foreground_window
5115 || foreground_thread == GetCurrentThreadId ()
5116 || !AttachThreadInput (GetCurrentThreadId (),
5117 foreground_thread, TRUE))
5118 foreground_thread = 0;
5119
5120 retval = SetForegroundWindow ((HWND) wParam);
5121
5122 /* Detach from the previous foreground thread. */
5123 if (foreground_thread)
5124 AttachThreadInput (GetCurrentThreadId (),
5125 foreground_thread, FALSE);
5126
5127 return retval;
5128 }
dfdb4047 5129
5ac45f98
GV
5130 case WM_EMACS_SETWINDOWPOS:
5131 {
1edf84e7
GV
5132 WINDOWPOS * pos = (WINDOWPOS *) wParam;
5133 return SetWindowPos (hwnd, pos->hwndInsertAfter,
5ac45f98
GV
5134 pos->x, pos->y, pos->cx, pos->cy, pos->flags);
5135 }
1edf84e7 5136
ee78dc32 5137 case WM_EMACS_DESTROYWINDOW:
cb9e33d4 5138 DragAcceptFiles ((HWND) wParam, FALSE);
1edf84e7
GV
5139 return DestroyWindow ((HWND) wParam);
5140
93f2ca61
JR
5141 case WM_EMACS_HIDE_CARET:
5142 return HideCaret (hwnd);
5143
5144 case WM_EMACS_SHOW_CARET:
5145 return ShowCaret (hwnd);
5146
65906840
JR
5147 case WM_EMACS_DESTROY_CARET:
5148 w32_system_caret_hwnd = NULL;
93f2ca61 5149 w32_visible_system_caret_hwnd = NULL;
65906840
JR
5150 return DestroyCaret ();
5151
5152 case WM_EMACS_TRACK_CARET:
5153 /* If there is currently no system caret, create one. */
5154 if (w32_system_caret_hwnd == NULL)
5155 {
93f2ca61
JR
5156 /* Use the default caret width, and avoid changing it
5157 unneccesarily, as it confuses screen reader software. */
65906840 5158 w32_system_caret_hwnd = hwnd;
93f2ca61 5159 CreateCaret (hwnd, NULL, 0,
65906840
JR
5160 w32_system_caret_height);
5161 }
93f2ca61
JR
5162
5163 if (!SetCaretPos (w32_system_caret_x, w32_system_caret_y))
5164 return 0;
5165 /* Ensure visible caret gets turned on when requested. */
5166 else if (w32_use_visible_system_caret
5167 && w32_visible_system_caret_hwnd != hwnd)
5168 {
5169 w32_visible_system_caret_hwnd = hwnd;
5170 return ShowCaret (hwnd);
5171 }
5172 /* Ensure visible caret gets turned off when requested. */
5173 else if (!w32_use_visible_system_caret
5174 && w32_visible_system_caret_hwnd)
5175 {
5176 w32_visible_system_caret_hwnd = NULL;
5177 return HideCaret (hwnd);
5178 }
5179 else
5180 return 1;
65906840 5181
1edf84e7
GV
5182 case WM_EMACS_TRACKPOPUPMENU:
5183 {
5184 UINT flags;
5185 POINT *pos;
5186 int retval;
5187 pos = (POINT *)lParam;
5188 flags = TPM_CENTERALIGN;
5189 if (button_state & LMOUSE)
5190 flags |= TPM_LEFTBUTTON;
5191 else if (button_state & RMOUSE)
5192 flags |= TPM_RIGHTBUTTON;
5193
87996783
GV
5194 /* Remember we did a SetCapture on the initial mouse down event,
5195 so for safety, we make sure the capture is cancelled now. */
5196 ReleaseCapture ();
490822ff 5197 button_state = 0;
87996783 5198
1edf84e7
GV
5199 /* Use menubar_active to indicate that WM_INITMENU is from
5200 TrackPopupMenu below, and should be ignored. */
5201 f = x_window_to_frame (dpyinfo, hwnd);
5202 if (f)
5203 f->output_data.w32->menubar_active = 1;
5204
5205 if (TrackPopupMenu ((HMENU)wParam, flags, pos->x, pos->y,
5206 0, hwnd, NULL))
5207 {
5208 MSG amsg;
5209 /* Eat any mouse messages during popupmenu */
5210 while (PeekMessage (&amsg, hwnd, WM_MOUSEFIRST, WM_MOUSELAST,
5211 PM_REMOVE));
5212 /* Get the menu selection, if any */
5213 if (PeekMessage (&amsg, hwnd, WM_COMMAND, WM_COMMAND, PM_REMOVE))
5214 {
5215 retval = LOWORD (amsg.wParam);
5216 }
5217 else
5218 {
5219 retval = 0;
5220 }
1edf84e7
GV
5221 }
5222 else
5223 {
5224 retval = -1;
5225 }
5226
5227 return retval;
5228 }
5229
ee78dc32 5230 default:
93fbe8b7
GV
5231 /* Check for messages registered at runtime. */
5232 if (msg == msh_mousewheel)
5233 {
5234 wmsg.dwModifiers = w32_get_modifiers ();
5235 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
5236 return 0;
5237 }
5238
ee78dc32
GV
5239 dflt:
5240 return DefWindowProc (hwnd, msg, wParam, lParam);
5241 }
5242
1edf84e7
GV
5243
5244 /* The most common default return code for handled messages is 0. */
5245 return 0;
ee78dc32
GV
5246}
5247
5248void
5249my_create_window (f)
5250 struct frame * f;
5251{
5252 MSG msg;
5253
1edf84e7
GV
5254 if (!PostThreadMessage (dwWindowsThreadId, WM_EMACS_CREATEWINDOW, (WPARAM)f, 0))
5255 abort ();
ee78dc32
GV
5256 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
5257}
5258
ca56d953
JR
5259
5260/* Create a tooltip window. Unlike my_create_window, we do not do this
5261 indirectly via the Window thread, as we do not need to process Window
5262 messages for the tooltip. Creating tooltips indirectly also creates
5263 deadlocks when tooltips are created for menu items. */
5264void
5265my_create_tip_window (f)
5266 struct frame *f;
5267{
bfd6edcc 5268 RECT rect;
ca56d953 5269
bfd6edcc
JR
5270 rect.left = rect.top = 0;
5271 rect.right = PIXEL_WIDTH (f);
5272 rect.bottom = PIXEL_HEIGHT (f);
5273
5274 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
5275 FRAME_EXTERNAL_MENU_BAR (f));
5276
5277 tip_window = FRAME_W32_WINDOW (f)
ca56d953
JR
5278 = CreateWindow (EMACS_CLASS,
5279 f->namebuf,
5280 f->output_data.w32->dwStyle,
5281 f->output_data.w32->left_pos,
5282 f->output_data.w32->top_pos,
bfd6edcc
JR
5283 rect.right - rect.left,
5284 rect.bottom - rect.top,
ca56d953
JR
5285 FRAME_W32_WINDOW (SELECTED_FRAME ()), /* owner */
5286 NULL,
5287 hinst,
5288 NULL);
5289
bfd6edcc 5290 if (tip_window)
ca56d953 5291 {
bfd6edcc
JR
5292 SetWindowLong (tip_window, WND_FONTWIDTH_INDEX, FONT_WIDTH (f->output_data.w32->font));
5293 SetWindowLong (tip_window, WND_LINEHEIGHT_INDEX, f->output_data.w32->line_height);
5294 SetWindowLong (tip_window, WND_BORDER_INDEX, f->output_data.w32->internal_border_width);
5295 SetWindowLong (tip_window, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
5296
5297 /* Tip frames have no scrollbars. */
5298 SetWindowLong (tip_window, WND_SCROLLBAR_INDEX, 0);
ca56d953
JR
5299
5300 /* Do this to discard the default setting specified by our parent. */
bfd6edcc 5301 ShowWindow (tip_window, SW_HIDE);
ca56d953
JR
5302 }
5303}
5304
5305
fbd6baed 5306/* Create and set up the w32 window for frame F. */
ee78dc32
GV
5307
5308static void
fbd6baed 5309w32_window (f, window_prompting, minibuffer_only)
ee78dc32
GV
5310 struct frame *f;
5311 long window_prompting;
5312 int minibuffer_only;
5313{
5314 BLOCK_INPUT;
5315
5316 /* Use the resource name as the top-level window name
5317 for looking up resources. Make a non-Lisp copy
5318 for the window manager, so GC relocation won't bother it.
5319
5320 Elsewhere we specify the window name for the window manager. */
5321
5322 {
5323 char *str = (char *) XSTRING (Vx_resource_name)->data;
5324 f->namebuf = (char *) xmalloc (strlen (str) + 1);
5325 strcpy (f->namebuf, str);
5326 }
5327
5328 my_create_window (f);
5329
5330 validate_x_resource_name ();
5331
5332 /* x_set_name normally ignores requests to set the name if the
5333 requested name is the same as the current name. This is the one
5334 place where that assumption isn't correct; f->name is set, but
5335 the server hasn't been told. */
5336 {
5337 Lisp_Object name;
5338 int explicit = f->explicit_name;
5339
5340 f->explicit_name = 0;
5341 name = f->name;
5342 f->name = Qnil;
5343 x_set_name (f, name, explicit);
5344 }
5345
5346 UNBLOCK_INPUT;
5347
5348 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
5349 initialize_frame_menubar (f);
5350
fbd6baed 5351 if (FRAME_W32_WINDOW (f) == 0)
ee78dc32
GV
5352 error ("Unable to create window");
5353}
5354
5355/* Handle the icon stuff for this window. Perhaps later we might
5356 want an x_set_icon_position which can be called interactively as
5357 well. */
5358
5359static void
5360x_icon (f, parms)
5361 struct frame *f;
5362 Lisp_Object parms;
5363{
5364 Lisp_Object icon_x, icon_y;
5365
e9e23e23 5366 /* Set the position of the icon. Note that Windows 95 groups all
ee78dc32 5367 icons in the tray. */
6fc2811b
JR
5368 icon_x = w32_get_arg (parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
5369 icon_y = w32_get_arg (parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
ee78dc32
GV
5370 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
5371 {
b7826503
PJ
5372 CHECK_NUMBER (icon_x);
5373 CHECK_NUMBER (icon_y);
ee78dc32
GV
5374 }
5375 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
5376 error ("Both left and top icon corners of icon must be specified");
5377
5378 BLOCK_INPUT;
5379
5380 if (! EQ (icon_x, Qunbound))
5381 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
5382
1edf84e7
GV
5383#if 0 /* TODO */
5384 /* Start up iconic or window? */
5385 x_wm_set_window_state
6fc2811b 5386 (f, (EQ (w32_get_arg (parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL), Qicon)
1edf84e7
GV
5387 ? IconicState
5388 : NormalState));
5389
5390 x_text_icon (f, (char *) XSTRING ((!NILP (f->icon_name)
5391 ? f->icon_name
5392 : f->name))->data);
5393#endif
5394
ee78dc32
GV
5395 UNBLOCK_INPUT;
5396}
5397
6fc2811b
JR
5398
5399static void
5400x_make_gc (f)
5401 struct frame *f;
5402{
5403 XGCValues gc_values;
5404
5405 BLOCK_INPUT;
5406
5407 /* Create the GC's of this frame.
5408 Note that many default values are used. */
5409
5410 /* Normal video */
5411 gc_values.font = f->output_data.w32->font;
5412
5413 /* Cursor has cursor-color background, background-color foreground. */
5414 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
5415 gc_values.background = f->output_data.w32->cursor_pixel;
5416 f->output_data.w32->cursor_gc
5417 = XCreateGC (NULL, FRAME_W32_WINDOW (f),
5418 (GCFont | GCForeground | GCBackground),
5419 &gc_values);
5420
5421 /* Reliefs. */
5422 f->output_data.w32->white_relief.gc = 0;
5423 f->output_data.w32->black_relief.gc = 0;
5424
5425 UNBLOCK_INPUT;
5426}
5427
5428
937e601e
AI
5429/* Handler for signals raised during x_create_frame and
5430 x_create_top_frame. FRAME is the frame which is partially
5431 constructed. */
5432
5433static Lisp_Object
5434unwind_create_frame (frame)
5435 Lisp_Object frame;
5436{
5437 struct frame *f = XFRAME (frame);
5438
5439 /* If frame is ``official'', nothing to do. */
5440 if (!CONSP (Vframe_list) || !EQ (XCAR (Vframe_list), frame))
5441 {
5442#ifdef GLYPH_DEBUG
5443 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
5444#endif
5445
5446 x_free_frame_resources (f);
5447
5448 /* Check that reference counts are indeed correct. */
5449 xassert (dpyinfo->reference_count == dpyinfo_refcount);
5450 xassert (dpyinfo->image_cache->refcount == image_cache_refcount);
c844a81a
GM
5451
5452 return Qt;
937e601e
AI
5453 }
5454
5455 return Qnil;
5456}
5457
5458
ee78dc32
GV
5459DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
5460 1, 1, 0,
74e1aeec
JR
5461 doc: /* Make a new window, which is called a \"frame\" in Emacs terms.
5462Returns an Emacs frame object.
5463ALIST is an alist of frame parameters.
5464If the parameters specify that the frame should not have a minibuffer,
5465and do not specify a specific minibuffer window to use,
5466then `default-minibuffer-frame' must be a frame whose minibuffer can
5467be shared by the new frame.
5468
5469This function is an internal primitive--use `make-frame' instead. */)
ee78dc32
GV
5470 (parms)
5471 Lisp_Object parms;
5472{
5473 struct frame *f;
5474 Lisp_Object frame, tem;
5475 Lisp_Object name;
5476 int minibuffer_only = 0;
5477 long window_prompting = 0;
5478 int width, height;
dc220243 5479 int count = BINDING_STACK_SIZE ();
1edf84e7 5480 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
ee78dc32 5481 Lisp_Object display;
6fc2811b 5482 struct w32_display_info *dpyinfo = NULL;
ee78dc32
GV
5483 Lisp_Object parent;
5484 struct kboard *kb;
5485
4587b026
GV
5486 check_w32 ();
5487
ee78dc32
GV
5488 /* Use this general default value to start with
5489 until we know if this frame has a specified name. */
5490 Vx_resource_name = Vinvocation_name;
5491
6fc2811b 5492 display = w32_get_arg (parms, Qdisplay, 0, 0, RES_TYPE_STRING);
ee78dc32
GV
5493 if (EQ (display, Qunbound))
5494 display = Qnil;
5495 dpyinfo = check_x_display_info (display);
5496#ifdef MULTI_KBOARD
5497 kb = dpyinfo->kboard;
5498#else
5499 kb = &the_only_kboard;
5500#endif
5501
6fc2811b 5502 name = w32_get_arg (parms, Qname, "name", "Name", RES_TYPE_STRING);
ee78dc32
GV
5503 if (!STRINGP (name)
5504 && ! EQ (name, Qunbound)
5505 && ! NILP (name))
5506 error ("Invalid frame name--not a string or nil");
5507
5508 if (STRINGP (name))
5509 Vx_resource_name = name;
5510
5511 /* See if parent window is specified. */
6fc2811b 5512 parent = w32_get_arg (parms, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
ee78dc32
GV
5513 if (EQ (parent, Qunbound))
5514 parent = Qnil;
5515 if (! NILP (parent))
b7826503 5516 CHECK_NUMBER (parent);
ee78dc32 5517
1edf84e7
GV
5518 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
5519 /* No need to protect DISPLAY because that's not used after passing
5520 it to make_frame_without_minibuffer. */
5521 frame = Qnil;
5522 GCPRO4 (parms, parent, name, frame);
1660f34a
JR
5523 tem = w32_get_arg (parms, Qminibuffer, "minibuffer", "Minibuffer",
5524 RES_TYPE_SYMBOL);
ee78dc32
GV
5525 if (EQ (tem, Qnone) || NILP (tem))
5526 f = make_frame_without_minibuffer (Qnil, kb, display);
5527 else if (EQ (tem, Qonly))
5528 {
5529 f = make_minibuffer_frame ();
5530 minibuffer_only = 1;
5531 }
5532 else if (WINDOWP (tem))
5533 f = make_frame_without_minibuffer (tem, kb, display);
5534 else
5535 f = make_frame (1);
5536
1edf84e7
GV
5537 XSETFRAME (frame, f);
5538
ee78dc32
GV
5539 /* Note that Windows does support scroll bars. */
5540 FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;
5ac45f98
GV
5541 /* By default, make scrollbars the system standard width. */
5542 f->scroll_bar_pixel_width = GetSystemMetrics (SM_CXVSCROLL);
ee78dc32 5543
fbd6baed 5544 f->output_method = output_w32;
6fc2811b
JR
5545 f->output_data.w32 =
5546 (struct w32_output *) xmalloc (sizeof (struct w32_output));
fbd6baed 5547 bzero (f->output_data.w32, sizeof (struct w32_output));
4587b026 5548 FRAME_FONTSET (f) = -1;
937e601e 5549 record_unwind_protect (unwind_create_frame, frame);
4587b026 5550
1edf84e7 5551 f->icon_name
6fc2811b 5552 = w32_get_arg (parms, Qicon_name, "iconName", "Title", RES_TYPE_STRING);
1edf84e7
GV
5553 if (! STRINGP (f->icon_name))
5554 f->icon_name = Qnil;
5555
fbd6baed 5556/* FRAME_W32_DISPLAY_INFO (f) = dpyinfo; */
ee78dc32
GV
5557#ifdef MULTI_KBOARD
5558 FRAME_KBOARD (f) = kb;
5559#endif
5560
5561 /* Specify the parent under which to make this window. */
5562
5563 if (!NILP (parent))
5564 {
1660f34a 5565 f->output_data.w32->parent_desc = (Window) XFASTINT (parent);
fbd6baed 5566 f->output_data.w32->explicit_parent = 1;
ee78dc32
GV
5567 }
5568 else
5569 {
fbd6baed
GV
5570 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
5571 f->output_data.w32->explicit_parent = 0;
ee78dc32
GV
5572 }
5573
ee78dc32
GV
5574 /* Set the name; the functions to which we pass f expect the name to
5575 be set. */
5576 if (EQ (name, Qunbound) || NILP (name))
5577 {
fbd6baed 5578 f->name = build_string (dpyinfo->w32_id_name);
ee78dc32
GV
5579 f->explicit_name = 0;
5580 }
5581 else
5582 {
5583 f->name = name;
5584 f->explicit_name = 1;
5585 /* use the frame's title when getting resources for this frame. */
5586 specbind (Qx_resource_name, name);
5587 }
5588
5589 /* Extract the window parameters from the supplied values
5590 that are needed to determine window geometry. */
5591 {
5592 Lisp_Object font;
5593
6fc2811b
JR
5594 font = w32_get_arg (parms, Qfont, "font", "Font", RES_TYPE_STRING);
5595
ee78dc32
GV
5596 BLOCK_INPUT;
5597 /* First, try whatever font the caller has specified. */
5598 if (STRINGP (font))
4587b026
GV
5599 {
5600 tem = Fquery_fontset (font, Qnil);
5601 if (STRINGP (tem))
5602 font = x_new_fontset (f, XSTRING (tem)->data);
5603 else
1075afa9 5604 font = x_new_font (f, XSTRING (font)->data);
4587b026 5605 }
ee78dc32
GV
5606 /* Try out a font which we hope has bold and italic variations. */
5607 if (!STRINGP (font))
e39649be 5608 font = x_new_font (f, "-*-Courier New-normal-r-*-*-*-100-*-*-c-*-iso8859-1");
ee78dc32 5609 if (! STRINGP (font))
6fc2811b 5610 font = x_new_font (f, "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1");
ee78dc32
GV
5611 /* If those didn't work, look for something which will at least work. */
5612 if (! STRINGP (font))
6fc2811b 5613 font = x_new_font (f, "-*-Fixedsys-normal-r-*-*-12-*-*-*-c-*-iso8859-1");
ee78dc32
GV
5614 UNBLOCK_INPUT;
5615 if (! STRINGP (font))
1edf84e7 5616 font = build_string ("Fixedsys");
ee78dc32
GV
5617
5618 x_default_parameter (f, parms, Qfont, font,
6fc2811b 5619 "font", "Font", RES_TYPE_STRING);
ee78dc32
GV
5620 }
5621
5622 x_default_parameter (f, parms, Qborder_width, make_number (2),
1660f34a 5623 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
ee78dc32
GV
5624 /* This defaults to 2 in order to match xterm. We recognize either
5625 internalBorderWidth or internalBorder (which is what xterm calls
5626 it). */
5627 if (NILP (Fassq (Qinternal_border_width, parms)))
5628 {
5629 Lisp_Object value;
5630
6fc2811b 5631 value = w32_get_arg (parms, Qinternal_border_width,
1660f34a 5632 "internalBorder", "InternalBorder", RES_TYPE_NUMBER);
ee78dc32
GV
5633 if (! EQ (value, Qunbound))
5634 parms = Fcons (Fcons (Qinternal_border_width, value),
5635 parms);
5636 }
1edf84e7 5637 /* Default internalBorderWidth to 0 on Windows to match other programs. */
ee78dc32 5638 x_default_parameter (f, parms, Qinternal_border_width, make_number (0),
1660f34a
JR
5639 "internalBorderWidth", "InternalBorder", RES_TYPE_NUMBER);
5640 x_default_parameter (f, parms, Qvertical_scroll_bars, Qright,
5641 "verticalScrollBars", "ScrollBars", RES_TYPE_SYMBOL);
ee78dc32
GV
5642
5643 /* Also do the stuff which must be set before the window exists. */
5644 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
6fc2811b 5645 "foreground", "Foreground", RES_TYPE_STRING);
ee78dc32 5646 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
6fc2811b 5647 "background", "Background", RES_TYPE_STRING);
ee78dc32 5648 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
6fc2811b 5649 "pointerColor", "Foreground", RES_TYPE_STRING);
ee78dc32 5650 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
6fc2811b 5651 "cursorColor", "Foreground", RES_TYPE_STRING);
ee78dc32 5652 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
6fc2811b
JR
5653 "borderColor", "BorderColor", RES_TYPE_STRING);
5654 x_default_parameter (f, parms, Qscreen_gamma, Qnil,
5655 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
dfff8a69
JR
5656 x_default_parameter (f, parms, Qline_spacing, Qnil,
5657 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
41c1bdd9
KS
5658 x_default_parameter (f, parms, Qleft_fringe, Qnil,
5659 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
5660 x_default_parameter (f, parms, Qright_fringe, Qnil,
5661 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
6fc2811b 5662
ee78dc32 5663
6fc2811b
JR
5664 /* Init faces before x_default_parameter is called for scroll-bar
5665 parameters because that function calls x_set_scroll_bar_width,
5666 which calls change_frame_size, which calls Fset_window_buffer,
5667 which runs hooks, which call Fvertical_motion. At the end, we
5668 end up in init_iterator with a null face cache, which should not
5669 happen. */
5670 init_frame_faces (f);
5671
ee78dc32 5672 x_default_parameter (f, parms, Qmenu_bar_lines, make_number (1),
6fc2811b
JR
5673 "menuBar", "MenuBar", RES_TYPE_NUMBER);
5674 x_default_parameter (f, parms, Qtool_bar_lines, make_number (0),
5675 "toolBar", "ToolBar", RES_TYPE_NUMBER);
1edf84e7 5676 x_default_parameter (f, parms, Qbuffer_predicate, Qnil,
6fc2811b 5677 "bufferPredicate", "BufferPredicate", RES_TYPE_SYMBOL);
1edf84e7 5678 x_default_parameter (f, parms, Qtitle, Qnil,
6fc2811b 5679 "title", "Title", RES_TYPE_STRING);
f7b9d4d1
JR
5680 x_default_parameter (f, parms, Qfullscreen, Qnil,
5681 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
ee78dc32 5682
fbd6baed
GV
5683 f->output_data.w32->dwStyle = WS_OVERLAPPEDWINDOW;
5684 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
3cf3436e
JR
5685
5686 /* Add the tool-bar height to the initial frame height so that the
5687 user gets a text display area of the size he specified with -g or
5688 via .Xdefaults. Later changes of the tool-bar height don't
5689 change the frame size. This is done so that users can create
5690 tall Emacs frames without having to guess how tall the tool-bar
5691 will get. */
5692 if (FRAME_TOOL_BAR_LINES (f))
5693 {
5694 int margin, relief, bar_height;
5695
a05e2bae 5696 relief = (tool_bar_button_relief >= 0
3cf3436e
JR
5697 ? tool_bar_button_relief
5698 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
5699
5700 if (INTEGERP (Vtool_bar_button_margin)
5701 && XINT (Vtool_bar_button_margin) > 0)
5702 margin = XFASTINT (Vtool_bar_button_margin);
5703 else if (CONSP (Vtool_bar_button_margin)
5704 && INTEGERP (XCDR (Vtool_bar_button_margin))
5705 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
5706 margin = XFASTINT (XCDR (Vtool_bar_button_margin));
5707 else
5708 margin = 0;
5709
5710 bar_height = DEFAULT_TOOL_BAR_IMAGE_HEIGHT + 2 * margin + 2 * relief;
5711 f->height += (bar_height + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
5712 }
5713
ee78dc32
GV
5714 window_prompting = x_figure_window_size (f, parms);
5715
5716 if (window_prompting & XNegative)
5717 {
5718 if (window_prompting & YNegative)
fbd6baed 5719 f->output_data.w32->win_gravity = SouthEastGravity;
ee78dc32 5720 else
fbd6baed 5721 f->output_data.w32->win_gravity = NorthEastGravity;
ee78dc32
GV
5722 }
5723 else
5724 {
5725 if (window_prompting & YNegative)
fbd6baed 5726 f->output_data.w32->win_gravity = SouthWestGravity;
ee78dc32 5727 else
fbd6baed 5728 f->output_data.w32->win_gravity = NorthWestGravity;
ee78dc32
GV
5729 }
5730
fbd6baed 5731 f->output_data.w32->size_hint_flags = window_prompting;
ee78dc32 5732
6fc2811b
JR
5733 tem = w32_get_arg (parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
5734 f->no_split = minibuffer_only || EQ (tem, Qt);
5735
fbd6baed 5736 w32_window (f, window_prompting, minibuffer_only);
ee78dc32 5737 x_icon (f, parms);
6fc2811b
JR
5738
5739 x_make_gc (f);
5740
5741 /* Now consider the frame official. */
5742 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
5743 Vframe_list = Fcons (frame, Vframe_list);
ee78dc32
GV
5744
5745 /* We need to do this after creating the window, so that the
5746 icon-creation functions can say whose icon they're describing. */
5747 x_default_parameter (f, parms, Qicon_type, Qnil,
6fc2811b 5748 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
ee78dc32
GV
5749
5750 x_default_parameter (f, parms, Qauto_raise, Qnil,
6fc2811b 5751 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
ee78dc32 5752 x_default_parameter (f, parms, Qauto_lower, Qnil,
6fc2811b 5753 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
ee78dc32 5754 x_default_parameter (f, parms, Qcursor_type, Qbox,
6fc2811b
JR
5755 "cursorType", "CursorType", RES_TYPE_SYMBOL);
5756 x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
5757 "scrollBarWidth", "ScrollBarWidth", RES_TYPE_NUMBER);
ee78dc32
GV
5758
5759 /* Dimensions, especially f->height, must be done via change_frame_size.
5760 Change will not be effected unless different from the current
5761 f->height. */
5762 width = f->width;
5763 height = f->height;
dc220243 5764
1026b400
RS
5765 f->height = 0;
5766 SET_FRAME_WIDTH (f, 0);
6fc2811b
JR
5767 change_frame_size (f, height, width, 1, 0, 0);
5768
6fc2811b
JR
5769 /* Tell the server what size and position, etc, we want, and how
5770 badly we want them. This should be done after we have the menu
5771 bar so that its size can be taken into account. */
ee78dc32
GV
5772 BLOCK_INPUT;
5773 x_wm_set_size_hint (f, window_prompting, 0);
5774 UNBLOCK_INPUT;
5775
815d969e
JR
5776 /* Avoid a bug that causes the new frame to never become visible if
5777 an echo area message is displayed during the following call1. */
5778 specbind(Qredisplay_dont_pause, Qt);
5779
4694d762
JR
5780 /* Set up faces after all frame parameters are known. This call
5781 also merges in face attributes specified for new frames. If we
5782 don't do this, the `menu' face for instance won't have the right
5783 colors, and the menu bar won't appear in the specified colors for
5784 new frames. */
5785 call1 (Qface_set_after_frame_default, frame);
5786
6fc2811b
JR
5787 /* Make the window appear on the frame and enable display, unless
5788 the caller says not to. However, with explicit parent, Emacs
5789 cannot control visibility, so don't try. */
fbd6baed 5790 if (! f->output_data.w32->explicit_parent)
ee78dc32
GV
5791 {
5792 Lisp_Object visibility;
5793
6fc2811b 5794 visibility = w32_get_arg (parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL);
ee78dc32
GV
5795 if (EQ (visibility, Qunbound))
5796 visibility = Qt;
5797
5798 if (EQ (visibility, Qicon))
5799 x_iconify_frame (f);
5800 else if (! NILP (visibility))
5801 x_make_frame_visible (f);
5802 else
5803 /* Must have been Qnil. */
5804 ;
5805 }
6fc2811b 5806 UNGCPRO;
9e57df62
GM
5807
5808 /* Make sure windows on this frame appear in calls to next-window
5809 and similar functions. */
5810 Vwindow_list = Qnil;
5811
ee78dc32
GV
5812 return unbind_to (count, frame);
5813}
5814
5815/* FRAME is used only to get a handle on the X display. We don't pass the
5816 display info directly because we're called from frame.c, which doesn't
5817 know about that structure. */
5818Lisp_Object
5819x_get_focus_frame (frame)
5820 struct frame *frame;
5821{
fbd6baed 5822 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (frame);
ee78dc32 5823 Lisp_Object xfocus;
fbd6baed 5824 if (! dpyinfo->w32_focus_frame)
ee78dc32
GV
5825 return Qnil;
5826
fbd6baed 5827 XSETFRAME (xfocus, dpyinfo->w32_focus_frame);
ee78dc32
GV
5828 return xfocus;
5829}
1edf84e7
GV
5830
5831DEFUN ("w32-focus-frame", Fw32_focus_frame, Sw32_focus_frame, 1, 1, 0,
74e1aeec 5832 doc: /* Give FRAME input focus, raising to foreground if necessary. */)
1edf84e7
GV
5833 (frame)
5834 Lisp_Object frame;
5835{
5836 x_focus_on_frame (check_x_frame (frame));
5837 return Qnil;
5838}
5839
ee78dc32 5840\f
767b1ff0
JR
5841/* Return the charset portion of a font name. */
5842char * xlfd_charset_of_font (char * fontname)
5843{
5844 char *charset, *encoding;
5845
5846 encoding = strrchr(fontname, '-');
ceb12877 5847 if (!encoding || encoding == fontname)
767b1ff0
JR
5848 return NULL;
5849
478ea067
AI
5850 for (charset = encoding - 1; charset >= fontname; charset--)
5851 if (*charset == '-')
5852 break;
767b1ff0 5853
478ea067 5854 if (charset == fontname || strcmp(charset, "-*-*") == 0)
767b1ff0
JR
5855 return NULL;
5856
5857 return charset + 1;
5858}
5859
33d52f9c
GV
5860struct font_info *w32_load_bdf_font (struct frame *f, char *fontname,
5861 int size, char* filename);
8edb0a6f 5862static Lisp_Object w32_list_bdf_fonts (Lisp_Object pattern, int max_names);
c7501041
EZ
5863static BOOL w32_to_x_font (LOGFONT * lplf, char * lpxstr, int len,
5864 char * charset);
5865static BOOL x_to_w32_font (char *lpxstr, LOGFONT *lplogfont);
33d52f9c 5866
8edb0a6f 5867static struct font_info *
33d52f9c 5868w32_load_system_font (f,fontname,size)
55dcfc15
AI
5869 struct frame *f;
5870 char * fontname;
5871 int size;
ee78dc32 5872{
4587b026
GV
5873 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
5874 Lisp_Object font_names;
5875
4587b026
GV
5876 /* Get a list of all the fonts that match this name. Once we
5877 have a list of matching fonts, we compare them against the fonts
5878 we already have loaded by comparing names. */
5879 font_names = w32_list_fonts (f, build_string (fontname), size, 100);
5880
5881 if (!NILP (font_names))
3c190163 5882 {
4587b026
GV
5883 Lisp_Object tail;
5884 int i;
4587b026
GV
5885
5886 /* First check if any are already loaded, as that is cheaper
5887 than loading another one. */
5888 for (i = 0; i < dpyinfo->n_fonts; i++)
8e713be6 5889 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
6fc2811b
JR
5890 if (dpyinfo->font_table[i].name
5891 && (!strcmp (dpyinfo->font_table[i].name,
5892 XSTRING (XCAR (tail))->data)
5893 || !strcmp (dpyinfo->font_table[i].full_name,
5894 XSTRING (XCAR (tail))->data)))
4587b026 5895 return (dpyinfo->font_table + i);
6fc2811b 5896
8e713be6 5897 fontname = (char *) XSTRING (XCAR (font_names))->data;
4587b026 5898 }
1075afa9 5899 else if (w32_strict_fontnames)
5ca0cd71
GV
5900 {
5901 /* If EnumFontFamiliesEx was available, we got a full list of
5902 fonts back so stop now to avoid the possibility of loading a
5903 random font. If we had to fall back to EnumFontFamilies, the
5904 list is incomplete, so continue whether the font we want was
5905 listed or not. */
5906 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
5907 FARPROC enum_font_families_ex
1075afa9 5908 = GetProcAddress (gdi32, "EnumFontFamiliesExA");
5ca0cd71
GV
5909 if (enum_font_families_ex)
5910 return NULL;
5911 }
4587b026
GV
5912
5913 /* Load the font and add it to the table. */
5914 {
767b1ff0 5915 char *full_name, *encoding, *charset;
4587b026
GV
5916 XFontStruct *font;
5917 struct font_info *fontp;
3c190163 5918 LOGFONT lf;
4587b026 5919 BOOL ok;
19c291d3 5920 int codepage;
6fc2811b 5921 int i;
5ac45f98 5922
4587b026 5923 if (!fontname || !x_to_w32_font (fontname, &lf))
3c190163 5924 return (NULL);
5ac45f98 5925
4587b026
GV
5926 if (!*lf.lfFaceName)
5927 /* If no name was specified for the font, we get a random font
5928 from CreateFontIndirect - this is not particularly
5929 desirable, especially since CreateFontIndirect does not
5930 fill out the missing name in lf, so we never know what we
5931 ended up with. */
5932 return NULL;
5933
d65a9cdc
JR
5934 /* Specify anti-aliasing to prevent Cleartype fonts being used,
5935 since those fonts leave garbage behind. */
5936 lf.lfQuality = ANTIALIASED_QUALITY;
5937
3c190163 5938 font = (XFontStruct *) xmalloc (sizeof (XFontStruct));
c6be3860 5939 bzero (font, sizeof (*font));
5ac45f98 5940
33d52f9c
GV
5941 /* Set bdf to NULL to indicate that this is a Windows font. */
5942 font->bdf = NULL;
5ac45f98 5943
3c190163 5944 BLOCK_INPUT;
5ac45f98
GV
5945
5946 font->hfont = CreateFontIndirect (&lf);
ee78dc32 5947
1a292d24
AI
5948 if (font->hfont == NULL)
5949 {
5950 ok = FALSE;
5951 }
5952 else
5953 {
5954 HDC hdc;
5955 HANDLE oldobj;
19c291d3
AI
5956
5957 codepage = w32_codepage_for_font (fontname);
1a292d24
AI
5958
5959 hdc = GetDC (dpyinfo->root_window);
5960 oldobj = SelectObject (hdc, font->hfont);
5c6682be 5961
1a292d24 5962 ok = GetTextMetrics (hdc, &font->tm);
5c6682be
JR
5963 if (codepage == CP_UNICODE)
5964 font->double_byte_p = 1;
5965 else
8b77111c
AI
5966 {
5967 /* Unfortunately, some fonts (eg. MingLiU, a big5 ttf font)
5968 don't report themselves as double byte fonts, when
5969 patently they are. So instead of trusting
5970 GetFontLanguageInfo, we check the properties of the
5971 codepage directly, since that is ultimately what we are
5972 working from anyway. */
5973 /* font->double_byte_p = GetFontLanguageInfo(hdc) & GCP_DBCS; */
5974 CPINFO cpi = {0};
5975 GetCPInfo (codepage, &cpi);
5976 font->double_byte_p = cpi.MaxCharSize > 1;
5977 }
5c6682be 5978
1a292d24
AI
5979 SelectObject (hdc, oldobj);
5980 ReleaseDC (dpyinfo->root_window, hdc);
6fc2811b
JR
5981 /* Fill out details in lf according to the font that was
5982 actually loaded. */
5983 lf.lfHeight = font->tm.tmInternalLeading - font->tm.tmHeight;
5984 lf.lfWidth = font->tm.tmAveCharWidth;
5985 lf.lfWeight = font->tm.tmWeight;
5986 lf.lfItalic = font->tm.tmItalic;
5987 lf.lfCharSet = font->tm.tmCharSet;
5988 lf.lfPitchAndFamily = ((font->tm.tmPitchAndFamily & TMPF_FIXED_PITCH)
d88c567c 5989 ? VARIABLE_PITCH : FIXED_PITCH);
6fc2811b
JR
5990 lf.lfOutPrecision = ((font->tm.tmPitchAndFamily & TMPF_VECTOR)
5991 ? OUT_STROKE_PRECIS : OUT_STRING_PRECIS);
c6be3860
AI
5992
5993 w32_cache_char_metrics (font);
1a292d24 5994 }
5ac45f98 5995
1a292d24 5996 UNBLOCK_INPUT;
5ac45f98 5997
4587b026
GV
5998 if (!ok)
5999 {
1a292d24
AI
6000 w32_unload_font (dpyinfo, font);
6001 return (NULL);
6002 }
ee78dc32 6003
6fc2811b
JR
6004 /* Find a free slot in the font table. */
6005 for (i = 0; i < dpyinfo->n_fonts; ++i)
6006 if (dpyinfo->font_table[i].name == NULL)
6007 break;
6008
6009 /* If no free slot found, maybe enlarge the font table. */
6010 if (i == dpyinfo->n_fonts
6011 && dpyinfo->n_fonts == dpyinfo->font_table_size)
4587b026 6012 {
6fc2811b
JR
6013 int sz;
6014 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
6015 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
4587b026 6016 dpyinfo->font_table
6fc2811b 6017 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
4587b026
GV
6018 }
6019
6fc2811b
JR
6020 fontp = dpyinfo->font_table + i;
6021 if (i == dpyinfo->n_fonts)
6022 ++dpyinfo->n_fonts;
4587b026
GV
6023
6024 /* Now fill in the slots of *FONTP. */
6025 BLOCK_INPUT;
6026 fontp->font = font;
6fc2811b 6027 fontp->font_idx = i;
4587b026
GV
6028 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
6029 bcopy (fontname, fontp->name, strlen (fontname) + 1);
6030
767b1ff0
JR
6031 charset = xlfd_charset_of_font (fontname);
6032
19c291d3
AI
6033 /* Cache the W32 codepage for a font. This makes w32_encode_char
6034 (called for every glyph during redisplay) much faster. */
6035 fontp->codepage = codepage;
6036
4587b026
GV
6037 /* Work out the font's full name. */
6038 full_name = (char *)xmalloc (100);
767b1ff0 6039 if (full_name && w32_to_x_font (&lf, full_name, 100, charset))
4587b026
GV
6040 fontp->full_name = full_name;
6041 else
6042 {
6043 /* If all else fails - just use the name we used to load it. */
6044 xfree (full_name);
6045 fontp->full_name = fontp->name;
6046 }
6047
6048 fontp->size = FONT_WIDTH (font);
6049 fontp->height = FONT_HEIGHT (font);
6050
6051 /* The slot `encoding' specifies how to map a character
6052 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
126f2e35
JR
6053 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
6054 (0:0x20..0x7F, 1:0xA0..0xFF,
6055 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
4587b026 6056 2:0xA020..0xFF7F). For the moment, we don't know which charset
6fc2811b 6057 uses this font. So, we set information in fontp->encoding[1]
4587b026
GV
6058 which is never used by any charset. If mapping can't be
6059 decided, set FONT_ENCODING_NOT_DECIDED. */
33d52f9c
GV
6060
6061 /* SJIS fonts need to be set to type 4, all others seem to work as
6062 type FONT_ENCODING_NOT_DECIDED. */
6063 encoding = strrchr (fontp->name, '-');
d84b082d 6064 if (encoding && strnicmp (encoding+1, "sjis", 4) == 0)
1c885fe1 6065 fontp->encoding[1] = 4;
33d52f9c 6066 else
1c885fe1 6067 fontp->encoding[1] = FONT_ENCODING_NOT_DECIDED;
4587b026
GV
6068
6069 /* The following three values are set to 0 under W32, which is
6070 what they get set to if XGetFontProperty fails under X. */
6071 fontp->baseline_offset = 0;
6072 fontp->relative_compose = 0;
33d52f9c 6073 fontp->default_ascent = 0;
4587b026 6074
6fc2811b
JR
6075 /* Set global flag fonts_changed_p to non-zero if the font loaded
6076 has a character with a smaller width than any other character
f7b9d4d1 6077 before, or if the font loaded has a smaller height than any
6fc2811b
JR
6078 other font loaded before. If this happens, it will make a
6079 glyph matrix reallocation necessary. */
f7b9d4d1 6080 fonts_changed_p |= x_compute_min_glyph_bounds (f);
4587b026 6081 UNBLOCK_INPUT;
4587b026
GV
6082 return fontp;
6083 }
6084}
6085
33d52f9c
GV
6086/* Load font named FONTNAME of size SIZE for frame F, and return a
6087 pointer to the structure font_info while allocating it dynamically.
6088 If loading fails, return NULL. */
6089struct font_info *
6090w32_load_font (f,fontname,size)
6091struct frame *f;
6092char * fontname;
6093int size;
6094{
6095 Lisp_Object bdf_fonts;
6096 struct font_info *retval = NULL;
6097
8edb0a6f 6098 bdf_fonts = w32_list_bdf_fonts (build_string (fontname), 1);
33d52f9c
GV
6099
6100 while (!retval && CONSP (bdf_fonts))
6101 {
6102 char *bdf_name, *bdf_file;
6103 Lisp_Object bdf_pair;
6104
8e713be6
KR
6105 bdf_name = XSTRING (XCAR (bdf_fonts))->data;
6106 bdf_pair = Fassoc (XCAR (bdf_fonts), Vw32_bdf_filename_alist);
6107 bdf_file = XSTRING (XCDR (bdf_pair))->data;
33d52f9c
GV
6108
6109 retval = w32_load_bdf_font (f, bdf_name, size, bdf_file);
6110
8e713be6 6111 bdf_fonts = XCDR (bdf_fonts);
33d52f9c
GV
6112 }
6113
6114 if (retval)
6115 return retval;
6116
6117 return w32_load_system_font(f, fontname, size);
6118}
6119
6120
ee78dc32 6121void
fbd6baed
GV
6122w32_unload_font (dpyinfo, font)
6123 struct w32_display_info *dpyinfo;
ee78dc32
GV
6124 XFontStruct * font;
6125{
6126 if (font)
6127 {
c6be3860 6128 if (font->per_char) xfree (font->per_char);
33d52f9c
GV
6129 if (font->bdf) w32_free_bdf_font (font->bdf);
6130
3c190163 6131 if (font->hfont) DeleteObject(font->hfont);
ee78dc32
GV
6132 xfree (font);
6133 }
6134}
6135
fbd6baed 6136/* The font conversion stuff between x and w32 */
ee78dc32
GV
6137
6138/* X font string is as follows (from faces.el)
6139 * (let ((- "[-?]")
6140 * (foundry "[^-]+")
6141 * (family "[^-]+")
6142 * (weight "\\(bold\\|demibold\\|medium\\)") ; 1
6143 * (weight\? "\\([^-]*\\)") ; 1
6144 * (slant "\\([ior]\\)") ; 2
6145 * (slant\? "\\([^-]?\\)") ; 2
6146 * (swidth "\\([^-]*\\)") ; 3
6147 * (adstyle "[^-]*") ; 4
6148 * (pixelsize "[0-9]+")
6149 * (pointsize "[0-9][0-9]+")
6150 * (resx "[0-9][0-9]+")
6151 * (resy "[0-9][0-9]+")
6152 * (spacing "[cmp?*]")
6153 * (avgwidth "[0-9]+")
6154 * (registry "[^-]+")
6155 * (encoding "[^-]+")
6156 * )
ee78dc32 6157 */
ee78dc32 6158
8edb0a6f 6159static LONG
fbd6baed 6160x_to_w32_weight (lpw)
ee78dc32
GV
6161 char * lpw;
6162{
6163 if (!lpw) return (FW_DONTCARE);
5ac45f98
GV
6164
6165 if (stricmp (lpw,"heavy") == 0) return FW_HEAVY;
6166 else if (stricmp (lpw,"extrabold") == 0) return FW_EXTRABOLD;
6167 else if (stricmp (lpw,"bold") == 0) return FW_BOLD;
6168 else if (stricmp (lpw,"demibold") == 0) return FW_SEMIBOLD;
1edf84e7 6169 else if (stricmp (lpw,"semibold") == 0) return FW_SEMIBOLD;
5ac45f98
GV
6170 else if (stricmp (lpw,"medium") == 0) return FW_MEDIUM;
6171 else if (stricmp (lpw,"normal") == 0) return FW_NORMAL;
6172 else if (stricmp (lpw,"light") == 0) return FW_LIGHT;
6173 else if (stricmp (lpw,"extralight") == 0) return FW_EXTRALIGHT;
6174 else if (stricmp (lpw,"thin") == 0) return FW_THIN;
ee78dc32 6175 else
5ac45f98 6176 return FW_DONTCARE;
ee78dc32
GV
6177}
6178
5ac45f98 6179
8edb0a6f 6180static char *
fbd6baed 6181w32_to_x_weight (fnweight)
ee78dc32
GV
6182 int fnweight;
6183{
5ac45f98
GV
6184 if (fnweight >= FW_HEAVY) return "heavy";
6185 if (fnweight >= FW_EXTRABOLD) return "extrabold";
6186 if (fnweight >= FW_BOLD) return "bold";
03f8fb34 6187 if (fnweight >= FW_SEMIBOLD) return "demibold";
5ac45f98
GV
6188 if (fnweight >= FW_MEDIUM) return "medium";
6189 if (fnweight >= FW_NORMAL) return "normal";
6190 if (fnweight >= FW_LIGHT) return "light";
6191 if (fnweight >= FW_EXTRALIGHT) return "extralight";
6192 if (fnweight >= FW_THIN) return "thin";
6193 else
6194 return "*";
6195}
6196
8edb0a6f 6197static LONG
fbd6baed 6198x_to_w32_charset (lpcs)
5ac45f98
GV
6199 char * lpcs;
6200{
767b1ff0 6201 Lisp_Object this_entry, w32_charset;
8b77111c
AI
6202 char *charset;
6203 int len = strlen (lpcs);
6204
6205 /* Support "*-#nnn" format for unknown charsets. */
6206 if (strncmp (lpcs, "*-#", 3) == 0)
6207 return atoi (lpcs + 3);
6208
6209 /* Handle wildcards by ignoring them; eg. treat "big5*-*" as "big5". */
6210 charset = alloca (len + 1);
6211 strcpy (charset, lpcs);
6212 lpcs = strchr (charset, '*');
6213 if (lpcs)
6214 *lpcs = 0;
4587b026 6215
dfff8a69
JR
6216 /* Look through w32-charset-info-alist for the character set.
6217 Format of each entry is
6218 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
6219 */
8b77111c 6220 this_entry = Fassoc (build_string(charset), Vw32_charset_info_alist);
4587b026 6221
767b1ff0
JR
6222 if (NILP(this_entry))
6223 {
6224 /* At startup, we want iso8859-1 fonts to come up properly. */
8b77111c 6225 if (stricmp(charset, "iso8859-1") == 0)
767b1ff0
JR
6226 return ANSI_CHARSET;
6227 else
6228 return DEFAULT_CHARSET;
6229 }
6230
6231 w32_charset = Fcar (Fcdr (this_entry));
6232
d84b082d 6233 /* Translate Lisp symbol to number. */
767b1ff0
JR
6234 if (w32_charset == Qw32_charset_ansi)
6235 return ANSI_CHARSET;
6236 if (w32_charset == Qw32_charset_symbol)
6237 return SYMBOL_CHARSET;
6238 if (w32_charset == Qw32_charset_shiftjis)
6239 return SHIFTJIS_CHARSET;
6240 if (w32_charset == Qw32_charset_hangeul)
6241 return HANGEUL_CHARSET;
6242 if (w32_charset == Qw32_charset_chinesebig5)
6243 return CHINESEBIG5_CHARSET;
6244 if (w32_charset == Qw32_charset_gb2312)
6245 return GB2312_CHARSET;
6246 if (w32_charset == Qw32_charset_oem)
6247 return OEM_CHARSET;
dfff8a69 6248#ifdef JOHAB_CHARSET
767b1ff0
JR
6249 if (w32_charset == Qw32_charset_johab)
6250 return JOHAB_CHARSET;
6251 if (w32_charset == Qw32_charset_easteurope)
6252 return EASTEUROPE_CHARSET;
6253 if (w32_charset == Qw32_charset_turkish)
6254 return TURKISH_CHARSET;
6255 if (w32_charset == Qw32_charset_baltic)
6256 return BALTIC_CHARSET;
6257 if (w32_charset == Qw32_charset_russian)
6258 return RUSSIAN_CHARSET;
6259 if (w32_charset == Qw32_charset_arabic)
6260 return ARABIC_CHARSET;
6261 if (w32_charset == Qw32_charset_greek)
6262 return GREEK_CHARSET;
6263 if (w32_charset == Qw32_charset_hebrew)
6264 return HEBREW_CHARSET;
6265 if (w32_charset == Qw32_charset_vietnamese)
6266 return VIETNAMESE_CHARSET;
6267 if (w32_charset == Qw32_charset_thai)
6268 return THAI_CHARSET;
6269 if (w32_charset == Qw32_charset_mac)
6270 return MAC_CHARSET;
dfff8a69 6271#endif /* JOHAB_CHARSET */
5ac45f98 6272#ifdef UNICODE_CHARSET
767b1ff0
JR
6273 if (w32_charset == Qw32_charset_unicode)
6274 return UNICODE_CHARSET;
5ac45f98 6275#endif
dfff8a69
JR
6276
6277 return DEFAULT_CHARSET;
5ac45f98
GV
6278}
6279
dfff8a69 6280
8edb0a6f 6281static char *
fbd6baed 6282w32_to_x_charset (fncharset)
5ac45f98
GV
6283 int fncharset;
6284{
5e905a57 6285 static char buf[32];
767b1ff0 6286 Lisp_Object charset_type;
1edf84e7 6287
5ac45f98
GV
6288 switch (fncharset)
6289 {
767b1ff0
JR
6290 case ANSI_CHARSET:
6291 /* Handle startup case of w32-charset-info-alist not
6292 being set up yet. */
6293 if (NILP(Vw32_charset_info_alist))
6294 return "iso8859-1";
6295 charset_type = Qw32_charset_ansi;
6296 break;
6297 case DEFAULT_CHARSET:
6298 charset_type = Qw32_charset_default;
6299 break;
6300 case SYMBOL_CHARSET:
6301 charset_type = Qw32_charset_symbol;
6302 break;
6303 case SHIFTJIS_CHARSET:
6304 charset_type = Qw32_charset_shiftjis;
6305 break;
6306 case HANGEUL_CHARSET:
6307 charset_type = Qw32_charset_hangeul;
6308 break;
6309 case GB2312_CHARSET:
6310 charset_type = Qw32_charset_gb2312;
6311 break;
6312 case CHINESEBIG5_CHARSET:
6313 charset_type = Qw32_charset_chinesebig5;
6314 break;
6315 case OEM_CHARSET:
6316 charset_type = Qw32_charset_oem;
6317 break;
4587b026
GV
6318
6319 /* More recent versions of Windows (95 and NT4.0) define more
6320 character sets. */
6321#ifdef EASTEUROPE_CHARSET
767b1ff0
JR
6322 case EASTEUROPE_CHARSET:
6323 charset_type = Qw32_charset_easteurope;
6324 break;
6325 case TURKISH_CHARSET:
6326 charset_type = Qw32_charset_turkish;
6327 break;
6328 case BALTIC_CHARSET:
6329 charset_type = Qw32_charset_baltic;
6330 break;
33d52f9c 6331 case RUSSIAN_CHARSET:
767b1ff0
JR
6332 charset_type = Qw32_charset_russian;
6333 break;
6334 case ARABIC_CHARSET:
6335 charset_type = Qw32_charset_arabic;
6336 break;
6337 case GREEK_CHARSET:
6338 charset_type = Qw32_charset_greek;
6339 break;
6340 case HEBREW_CHARSET:
6341 charset_type = Qw32_charset_hebrew;
6342 break;
6343 case VIETNAMESE_CHARSET:
6344 charset_type = Qw32_charset_vietnamese;
6345 break;
6346 case THAI_CHARSET:
6347 charset_type = Qw32_charset_thai;
6348 break;
6349 case MAC_CHARSET:
6350 charset_type = Qw32_charset_mac;
6351 break;
6352 case JOHAB_CHARSET:
6353 charset_type = Qw32_charset_johab;
6354 break;
4587b026
GV
6355#endif
6356
5ac45f98 6357#ifdef UNICODE_CHARSET
767b1ff0
JR
6358 case UNICODE_CHARSET:
6359 charset_type = Qw32_charset_unicode;
6360 break;
5ac45f98 6361#endif
767b1ff0
JR
6362 default:
6363 /* Encode numerical value of unknown charset. */
6364 sprintf (buf, "*-#%u", fncharset);
6365 return buf;
5ac45f98 6366 }
767b1ff0
JR
6367
6368 {
6369 Lisp_Object rest;
6370 char * best_match = NULL;
6371
6372 /* Look through w32-charset-info-alist for the character set.
6373 Prefer ISO codepages, and prefer lower numbers in the ISO
6374 range. Only return charsets for codepages which are installed.
6375
6376 Format of each entry is
6377 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
6378 */
6379 for (rest = Vw32_charset_info_alist; CONSP (rest); rest = XCDR (rest))
6380 {
6381 char * x_charset;
6382 Lisp_Object w32_charset;
6383 Lisp_Object codepage;
6384
6385 Lisp_Object this_entry = XCAR (rest);
6386
6387 /* Skip invalid entries in alist. */
6388 if (!CONSP (this_entry) || !STRINGP (XCAR (this_entry))
6389 || !CONSP (XCDR (this_entry))
6390 || !SYMBOLP (XCAR (XCDR (this_entry))))
6391 continue;
6392
6393 x_charset = XSTRING (XCAR (this_entry))->data;
6394 w32_charset = XCAR (XCDR (this_entry));
6395 codepage = XCDR (XCDR (this_entry));
6396
6397 /* Look for Same charset and a valid codepage (or non-int
6398 which means ignore). */
6399 if (w32_charset == charset_type
6400 && (!INTEGERP (codepage) || codepage == CP_DEFAULT
6401 || IsValidCodePage (XINT (codepage))))
6402 {
6403 /* If we don't have a match already, then this is the
6404 best. */
6405 if (!best_match)
6406 best_match = x_charset;
6407 /* If this is an ISO codepage, and the best so far isn't,
6408 then this is better. */
d84b082d
JR
6409 else if (strnicmp (best_match, "iso", 3) != 0
6410 && strnicmp (x_charset, "iso", 3) == 0)
767b1ff0
JR
6411 best_match = x_charset;
6412 /* If both are ISO8859 codepages, choose the one with the
6413 lowest number in the encoding field. */
d84b082d
JR
6414 else if (strnicmp (best_match, "iso8859-", 8) == 0
6415 && strnicmp (x_charset, "iso8859-", 8) == 0)
767b1ff0
JR
6416 {
6417 int best_enc = atoi (best_match + 8);
6418 int this_enc = atoi (x_charset + 8);
6419 if (this_enc > 0 && this_enc < best_enc)
6420 best_match = x_charset;
6421 }
6422 }
6423 }
6424
6425 /* If no match, encode the numeric value. */
6426 if (!best_match)
6427 {
6428 sprintf (buf, "*-#%u", fncharset);
6429 return buf;
6430 }
6431
5e905a57
JR
6432 strncpy(buf, best_match, 31);
6433 buf[31] = '\0';
767b1ff0
JR
6434 return buf;
6435 }
ee78dc32
GV
6436}
6437
dfff8a69 6438
d84b082d
JR
6439/* Return all the X charsets that map to a font. */
6440static Lisp_Object
6441w32_to_all_x_charsets (fncharset)
6442 int fncharset;
6443{
6444 static char buf[32];
6445 Lisp_Object charset_type;
6446 Lisp_Object retval = Qnil;
6447
6448 switch (fncharset)
6449 {
6450 case ANSI_CHARSET:
6451 /* Handle startup case of w32-charset-info-alist not
6452 being set up yet. */
6453 if (NILP(Vw32_charset_info_alist))
d86c35ee
JR
6454 return Fcons (build_string ("iso8859-1"), Qnil);
6455
d84b082d
JR
6456 charset_type = Qw32_charset_ansi;
6457 break;
6458 case DEFAULT_CHARSET:
6459 charset_type = Qw32_charset_default;
6460 break;
6461 case SYMBOL_CHARSET:
6462 charset_type = Qw32_charset_symbol;
6463 break;
6464 case SHIFTJIS_CHARSET:
6465 charset_type = Qw32_charset_shiftjis;
6466 break;
6467 case HANGEUL_CHARSET:
6468 charset_type = Qw32_charset_hangeul;
6469 break;
6470 case GB2312_CHARSET:
6471 charset_type = Qw32_charset_gb2312;
6472 break;
6473 case CHINESEBIG5_CHARSET:
6474 charset_type = Qw32_charset_chinesebig5;
6475 break;
6476 case OEM_CHARSET:
6477 charset_type = Qw32_charset_oem;
6478 break;
6479
6480 /* More recent versions of Windows (95 and NT4.0) define more
6481 character sets. */
6482#ifdef EASTEUROPE_CHARSET
6483 case EASTEUROPE_CHARSET:
6484 charset_type = Qw32_charset_easteurope;
6485 break;
6486 case TURKISH_CHARSET:
6487 charset_type = Qw32_charset_turkish;
6488 break;
6489 case BALTIC_CHARSET:
6490 charset_type = Qw32_charset_baltic;
6491 break;
6492 case RUSSIAN_CHARSET:
6493 charset_type = Qw32_charset_russian;
6494 break;
6495 case ARABIC_CHARSET:
6496 charset_type = Qw32_charset_arabic;
6497 break;
6498 case GREEK_CHARSET:
6499 charset_type = Qw32_charset_greek;
6500 break;
6501 case HEBREW_CHARSET:
6502 charset_type = Qw32_charset_hebrew;
6503 break;
6504 case VIETNAMESE_CHARSET:
6505 charset_type = Qw32_charset_vietnamese;
6506 break;
6507 case THAI_CHARSET:
6508 charset_type = Qw32_charset_thai;
6509 break;
6510 case MAC_CHARSET:
6511 charset_type = Qw32_charset_mac;
6512 break;
6513 case JOHAB_CHARSET:
6514 charset_type = Qw32_charset_johab;
6515 break;
6516#endif
6517
6518#ifdef UNICODE_CHARSET
6519 case UNICODE_CHARSET:
6520 charset_type = Qw32_charset_unicode;
6521 break;
6522#endif
6523 default:
6524 /* Encode numerical value of unknown charset. */
6525 sprintf (buf, "*-#%u", fncharset);
6526 return Fcons (build_string (buf), Qnil);
6527 }
6528
6529 {
6530 Lisp_Object rest;
6531 /* Look through w32-charset-info-alist for the character set.
6532 Only return charsets for codepages which are installed.
6533
6534 Format of each entry in Vw32_charset_info_alist is
6535 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
6536 */
6537 for (rest = Vw32_charset_info_alist; CONSP (rest); rest = XCDR (rest))
6538 {
6539 Lisp_Object x_charset;
6540 Lisp_Object w32_charset;
6541 Lisp_Object codepage;
6542
6543 Lisp_Object this_entry = XCAR (rest);
6544
6545 /* Skip invalid entries in alist. */
6546 if (!CONSP (this_entry) || !STRINGP (XCAR (this_entry))
6547 || !CONSP (XCDR (this_entry))
6548 || !SYMBOLP (XCAR (XCDR (this_entry))))
6549 continue;
6550
6551 x_charset = XCAR (this_entry);
6552 w32_charset = XCAR (XCDR (this_entry));
6553 codepage = XCDR (XCDR (this_entry));
6554
6555 /* Look for Same charset and a valid codepage (or non-int
6556 which means ignore). */
6557 if (w32_charset == charset_type
6558 && (!INTEGERP (codepage) || codepage == CP_DEFAULT
6559 || IsValidCodePage (XINT (codepage))))
6560 {
6561 retval = Fcons (x_charset, retval);
6562 }
6563 }
6564
6565 /* If no match, encode the numeric value. */
6566 if (NILP (retval))
6567 {
6568 sprintf (buf, "*-#%u", fncharset);
6569 return Fcons (build_string (buf), Qnil);
6570 }
6571
6572 return retval;
6573 }
6574}
6575
dfff8a69
JR
6576/* Get the Windows codepage corresponding to the specified font. The
6577 charset info in the font name is used to look up
6578 w32-charset-to-codepage-alist. */
6579int
6580w32_codepage_for_font (char *fontname)
6581{
767b1ff0
JR
6582 Lisp_Object codepage, entry;
6583 char *charset_str, *charset, *end;
dfff8a69 6584
767b1ff0 6585 if (NILP (Vw32_charset_info_alist))
dfff8a69
JR
6586 return CP_DEFAULT;
6587
767b1ff0
JR
6588 /* Extract charset part of font string. */
6589 charset = xlfd_charset_of_font (fontname);
6590
6591 if (!charset)
ceb12877 6592 return CP_UNKNOWN;
767b1ff0 6593
8b77111c 6594 charset_str = (char *) alloca (strlen (charset) + 1);
767b1ff0
JR
6595 strcpy (charset_str, charset);
6596
8b77111c 6597#if 0
dfff8a69
JR
6598 /* Remove leading "*-". */
6599 if (strncmp ("*-", charset_str, 2) == 0)
6600 charset = charset_str + 2;
6601 else
8b77111c 6602#endif
dfff8a69
JR
6603 charset = charset_str;
6604
6605 /* Stop match at wildcard (including preceding '-'). */
6606 if (end = strchr (charset, '*'))
6607 {
6608 if (end > charset && *(end-1) == '-')
6609 end--;
6610 *end = '\0';
6611 }
6612
767b1ff0
JR
6613 entry = Fassoc (build_string(charset), Vw32_charset_info_alist);
6614 if (NILP (entry))
ceb12877 6615 return CP_UNKNOWN;
767b1ff0
JR
6616
6617 codepage = Fcdr (Fcdr (entry));
6618
6619 if (NILP (codepage))
6620 return CP_8BIT;
6621 else if (XFASTINT (codepage) == XFASTINT (Qt))
6622 return CP_UNICODE;
6623 else if (INTEGERP (codepage))
dfff8a69
JR
6624 return XINT (codepage);
6625 else
ceb12877 6626 return CP_UNKNOWN;
dfff8a69
JR
6627}
6628
6629
8edb0a6f 6630static BOOL
767b1ff0 6631w32_to_x_font (lplogfont, lpxstr, len, specific_charset)
ee78dc32
GV
6632 LOGFONT * lplogfont;
6633 char * lpxstr;
6634 int len;
767b1ff0 6635 char * specific_charset;
ee78dc32 6636{
6fc2811b 6637 char* fonttype;
f46e6225 6638 char *fontname;
3cb20f4a
RS
6639 char height_pixels[8];
6640 char height_dpi[8];
6641 char width_pixels[8];
4587b026 6642 char *fontname_dash;
ac849ba4
JR
6643 int display_resy = (int) one_w32_display_info.resy;
6644 int display_resx = (int) one_w32_display_info.resx;
f46e6225
GV
6645 int bufsz;
6646 struct coding_system coding;
3cb20f4a
RS
6647
6648 if (!lpxstr) abort ();
ee78dc32 6649
3cb20f4a
RS
6650 if (!lplogfont)
6651 return FALSE;
6652
6fc2811b
JR
6653 if (lplogfont->lfOutPrecision == OUT_STRING_PRECIS)
6654 fonttype = "raster";
6655 else if (lplogfont->lfOutPrecision == OUT_STROKE_PRECIS)
6656 fonttype = "outline";
6657 else
6658 fonttype = "unknown";
6659
1fa3a200 6660 setup_coding_system (Fcheck_coding_system (Vlocale_coding_system),
f46e6225 6661 &coding);
aab5ac44
KH
6662 coding.src_multibyte = 0;
6663 coding.dst_multibyte = 1;
f46e6225
GV
6664 coding.mode |= CODING_MODE_LAST_BLOCK;
6665 bufsz = decoding_buffer_size (&coding, LF_FACESIZE);
6666
6667 fontname = alloca(sizeof(*fontname) * bufsz);
6668 decode_coding (&coding, lplogfont->lfFaceName, fontname,
6669 strlen(lplogfont->lfFaceName), bufsz - 1);
6670 *(fontname + coding.produced) = '\0';
4587b026
GV
6671
6672 /* Replace dashes with underscores so the dashes are not
f46e6225 6673 misinterpreted. */
4587b026
GV
6674 fontname_dash = fontname;
6675 while (fontname_dash = strchr (fontname_dash, '-'))
6676 *fontname_dash = '_';
6677
3cb20f4a 6678 if (lplogfont->lfHeight)
ee78dc32 6679 {
3cb20f4a
RS
6680 sprintf (height_pixels, "%u", abs (lplogfont->lfHeight));
6681 sprintf (height_dpi, "%u",
33d52f9c 6682 abs (lplogfont->lfHeight) * 720 / display_resy);
5ac45f98
GV
6683 }
6684 else
ee78dc32 6685 {
3cb20f4a
RS
6686 strcpy (height_pixels, "*");
6687 strcpy (height_dpi, "*");
ee78dc32 6688 }
3cb20f4a
RS
6689 if (lplogfont->lfWidth)
6690 sprintf (width_pixels, "%u", lplogfont->lfWidth * 10);
6691 else
6692 strcpy (width_pixels, "*");
6693
6694 _snprintf (lpxstr, len - 1,
6fc2811b
JR
6695 "-%s-%s-%s-%c-normal-normal-%s-%s-%d-%d-%c-%s-%s",
6696 fonttype, /* foundry */
4587b026
GV
6697 fontname, /* family */
6698 w32_to_x_weight (lplogfont->lfWeight), /* weight */
6699 lplogfont->lfItalic?'i':'r', /* slant */
6700 /* setwidth name */
6701 /* add style name */
6702 height_pixels, /* pixel size */
6703 height_dpi, /* point size */
33d52f9c
GV
6704 display_resx, /* resx */
6705 display_resy, /* resy */
4587b026
GV
6706 ((lplogfont->lfPitchAndFamily & 0x3) == VARIABLE_PITCH)
6707 ? 'p' : 'c', /* spacing */
6708 width_pixels, /* avg width */
767b1ff0
JR
6709 specific_charset ? specific_charset
6710 : w32_to_x_charset (lplogfont->lfCharSet)
6711 /* charset registry and encoding */
3cb20f4a
RS
6712 );
6713
ee78dc32
GV
6714 lpxstr[len - 1] = 0; /* just to be sure */
6715 return (TRUE);
6716}
6717
8edb0a6f 6718static BOOL
fbd6baed 6719x_to_w32_font (lpxstr, lplogfont)
ee78dc32
GV
6720 char * lpxstr;
6721 LOGFONT * lplogfont;
6722{
f46e6225
GV
6723 struct coding_system coding;
6724
ee78dc32 6725 if (!lplogfont) return (FALSE);
f46e6225 6726
ee78dc32 6727 memset (lplogfont, 0, sizeof (*lplogfont));
5ac45f98 6728
1a292d24 6729 /* Set default value for each field. */
771c47d5 6730#if 1
ee78dc32
GV
6731 lplogfont->lfOutPrecision = OUT_DEFAULT_PRECIS;
6732 lplogfont->lfClipPrecision = CLIP_DEFAULT_PRECIS;
6733 lplogfont->lfQuality = DEFAULT_QUALITY;
5ac45f98
GV
6734#else
6735 /* go for maximum quality */
6736 lplogfont->lfOutPrecision = OUT_STROKE_PRECIS;
6737 lplogfont->lfClipPrecision = CLIP_STROKE_PRECIS;
6738 lplogfont->lfQuality = PROOF_QUALITY;
6739#endif
6740
1a292d24
AI
6741 lplogfont->lfCharSet = DEFAULT_CHARSET;
6742 lplogfont->lfWeight = FW_DONTCARE;
6743 lplogfont->lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
6744
5ac45f98
GV
6745 if (!lpxstr)
6746 return FALSE;
6747
6748 /* Provide a simple escape mechanism for specifying Windows font names
6749 * directly -- if font spec does not beginning with '-', assume this
6750 * format:
6751 * "<font name>[:height in pixels[:width in pixels[:weight]]]"
6752 */
ee78dc32 6753
5ac45f98
GV
6754 if (*lpxstr == '-')
6755 {
33d52f9c
GV
6756 int fields, tem;
6757 char name[50], weight[20], slant, pitch, pixels[10], height[10],
8b77111c 6758 width[10], resy[10], remainder[50];
5ac45f98 6759 char * encoding;
ac849ba4 6760 int dpi = (int) one_w32_display_info.resy;
5ac45f98
GV
6761
6762 fields = sscanf (lpxstr,
8b77111c 6763 "-%*[^-]-%49[^-]-%19[^-]-%c-%*[^-]-%*[^-]-%9[^-]-%9[^-]-%*[^-]-%9[^-]-%c-%9[^-]-%49s",
33d52f9c 6764 name, weight, &slant, pixels, height, resy, &pitch, width, remainder);
8b77111c
AI
6765 if (fields == EOF)
6766 return (FALSE);
6767
6768 /* In the general case when wildcards cover more than one field,
6769 we don't know which field is which, so don't fill any in.
6770 However, we need to cope with this particular form, which is
6771 generated by font_list_1 (invoked by try_font_list):
6772 "-raster-6x10-*-gb2312*-*"
6773 and make sure to correctly parse the charset field. */
6774 if (fields == 3)
6775 {
6776 fields = sscanf (lpxstr,
6777 "-%*[^-]-%49[^-]-*-%49s",
6778 name, remainder);
6779 }
6780 else if (fields < 9)
6781 {
6782 fields = 0;
6783 remainder[0] = 0;
6784 }
6fc2811b 6785
5ac45f98
GV
6786 if (fields > 0 && name[0] != '*')
6787 {
8ea3e054
RS
6788 int bufsize;
6789 unsigned char *buf;
6790
f46e6225 6791 setup_coding_system
1fa3a200 6792 (Fcheck_coding_system (Vlocale_coding_system), &coding);
aab5ac44
KH
6793 coding.src_multibyte = 1;
6794 coding.dst_multibyte = 1;
8ea3e054
RS
6795 bufsize = encoding_buffer_size (&coding, strlen (name));
6796 buf = (unsigned char *) alloca (bufsize);
f46e6225 6797 coding.mode |= CODING_MODE_LAST_BLOCK;
8ea3e054
RS
6798 encode_coding (&coding, name, buf, strlen (name), bufsize);
6799 if (coding.produced >= LF_FACESIZE)
6800 coding.produced = LF_FACESIZE - 1;
6801 buf[coding.produced] = 0;
6802 strcpy (lplogfont->lfFaceName, buf);
5ac45f98
GV
6803 }
6804 else
6805 {
6fc2811b 6806 lplogfont->lfFaceName[0] = '\0';
5ac45f98
GV
6807 }
6808
6809 fields--;
6810
fbd6baed 6811 lplogfont->lfWeight = x_to_w32_weight ((fields > 0 ? weight : ""));
5ac45f98
GV
6812
6813 fields--;
6814
c8874f14 6815 lplogfont->lfItalic = (fields > 0 && slant == 'i');
5ac45f98
GV
6816
6817 fields--;
6818
6819 if (fields > 0 && pixels[0] != '*')
6820 lplogfont->lfHeight = atoi (pixels);
6821
6822 fields--;
5ac45f98 6823 fields--;
33d52f9c
GV
6824 if (fields > 0 && resy[0] != '*')
6825 {
6fc2811b 6826 tem = atoi (resy);
33d52f9c
GV
6827 if (tem > 0) dpi = tem;
6828 }
5ac45f98 6829
33d52f9c
GV
6830 if (fields > -1 && lplogfont->lfHeight == 0 && height[0] != '*')
6831 lplogfont->lfHeight = atoi (height) * dpi / 720;
6832
6833 if (fields > 0)
5ac45f98
GV
6834 lplogfont->lfPitchAndFamily =
6835 (fields > 0 && pitch == 'p') ? VARIABLE_PITCH : FIXED_PITCH;
6836
6837 fields--;
6838
6839 if (fields > 0 && width[0] != '*')
6840 lplogfont->lfWidth = atoi (width) / 10;
6841
6842 fields--;
6843
4587b026 6844 /* Strip the trailing '-' if present. (it shouldn't be, as it
d88c567c 6845 fails the test against xlfd-tight-regexp in fontset.el). */
3c190163 6846 {
5ac45f98
GV
6847 int len = strlen (remainder);
6848 if (len > 0 && remainder[len-1] == '-')
6849 remainder[len-1] = 0;
ee78dc32 6850 }
5ac45f98 6851 encoding = remainder;
8b77111c 6852#if 0
5ac45f98
GV
6853 if (strncmp (encoding, "*-", 2) == 0)
6854 encoding += 2;
8b77111c
AI
6855#endif
6856 lplogfont->lfCharSet = x_to_w32_charset (encoding);
5ac45f98
GV
6857 }
6858 else
6859 {
6860 int fields;
6861 char name[100], height[10], width[10], weight[20];
a1a80b40 6862
5ac45f98
GV
6863 fields = sscanf (lpxstr,
6864 "%99[^:]:%9[^:]:%9[^:]:%19s",
6865 name, height, width, weight);
6866
6867 if (fields == EOF) return (FALSE);
6868
6869 if (fields > 0)
6870 {
6871 strncpy (lplogfont->lfFaceName,name, LF_FACESIZE);
6872 lplogfont->lfFaceName[LF_FACESIZE-1] = 0;
6873 }
6874 else
6875 {
6876 lplogfont->lfFaceName[0] = 0;
6877 }
6878
6879 fields--;
6880
6881 if (fields > 0)
6882 lplogfont->lfHeight = atoi (height);
6883
6884 fields--;
6885
6886 if (fields > 0)
6887 lplogfont->lfWidth = atoi (width);
6888
6889 fields--;
6890
fbd6baed 6891 lplogfont->lfWeight = x_to_w32_weight ((fields > 0 ? weight : ""));
5ac45f98
GV
6892 }
6893
6894 /* This makes TrueType fonts work better. */
6895 lplogfont->lfHeight = - abs (lplogfont->lfHeight);
6fc2811b 6896
ee78dc32
GV
6897 return (TRUE);
6898}
6899
d88c567c
JR
6900/* Strip the pixel height and point height from the given xlfd, and
6901 return the pixel height. If no pixel height is specified, calculate
6902 one from the point height, or if that isn't defined either, return
6903 0 (which usually signifies a scalable font).
6904*/
8edb0a6f
JR
6905static int
6906xlfd_strip_height (char *fontname)
d88c567c 6907{
8edb0a6f 6908 int pixel_height, field_number;
d88c567c
JR
6909 char *read_from, *write_to;
6910
6911 xassert (fontname);
6912
6913 pixel_height = field_number = 0;
6914 write_to = NULL;
6915
6916 /* Look for height fields. */
6917 for (read_from = fontname; *read_from; read_from++)
6918 {
6919 if (*read_from == '-')
6920 {
6921 field_number++;
6922 if (field_number == 7) /* Pixel height. */
6923 {
6924 read_from++;
6925 write_to = read_from;
6926
6927 /* Find end of field. */
6928 for (;*read_from && *read_from != '-'; read_from++)
6929 ;
6930
6931 /* Split the fontname at end of field. */
6932 if (*read_from)
6933 {
6934 *read_from = '\0';
6935 read_from++;
6936 }
6937 pixel_height = atoi (write_to);
6938 /* Blank out field. */
6939 if (read_from > write_to)
6940 {
6941 *write_to = '-';
6942 write_to++;
6943 }
767b1ff0 6944 /* If the pixel height field is at the end (partial xlfd),
d88c567c
JR
6945 return now. */
6946 else
6947 return pixel_height;
6948
6949 /* If we got a pixel height, the point height can be
6950 ignored. Just blank it out and break now. */
6951 if (pixel_height)
6952 {
6953 /* Find end of point size field. */
6954 for (; *read_from && *read_from != '-'; read_from++)
6955 ;
6956
6957 if (*read_from)
6958 read_from++;
6959
6960 /* Blank out the point size field. */
6961 if (read_from > write_to)
6962 {
6963 *write_to = '-';
6964 write_to++;
6965 }
6966 else
6967 return pixel_height;
6968
6969 break;
6970 }
6971 /* If the point height is already blank, break now. */
6972 if (*read_from == '-')
6973 {
6974 read_from++;
6975 break;
6976 }
6977 }
6978 else if (field_number == 8)
6979 {
6980 /* If we didn't get a pixel height, try to get the point
6981 height and convert that. */
6982 int point_size;
6983 char *point_size_start = read_from++;
6984
6985 /* Find end of field. */
6986 for (; *read_from && *read_from != '-'; read_from++)
6987 ;
6988
6989 if (*read_from)
6990 {
6991 *read_from = '\0';
6992 read_from++;
6993 }
6994
6995 point_size = atoi (point_size_start);
6996
6997 /* Convert to pixel height. */
6998 pixel_height = point_size
6999 * one_w32_display_info.height_in / 720;
7000
7001 /* Blank out this field and break. */
7002 *write_to = '-';
7003 write_to++;
7004 break;
7005 }
7006 }
7007 }
7008
7009 /* Shift the rest of the font spec into place. */
7010 if (write_to && read_from > write_to)
7011 {
7012 for (; *read_from; read_from++, write_to++)
7013 *write_to = *read_from;
7014 *write_to = '\0';
7015 }
7016
7017 return pixel_height;
7018}
7019
6fc2811b 7020/* Assume parameter 1 is fully qualified, no wildcards. */
8edb0a6f 7021static BOOL
6fc2811b
JR
7022w32_font_match (fontname, pattern)
7023 char * fontname;
7024 char * pattern;
ee78dc32 7025{
e7c72122 7026 char *regex = alloca (strlen (pattern) * 2 + 3);
d88c567c 7027 char *font_name_copy = alloca (strlen (fontname) + 1);
6fc2811b 7028 char *ptr;
ee78dc32 7029
d88c567c
JR
7030 /* Copy fontname so we can modify it during comparison. */
7031 strcpy (font_name_copy, fontname);
7032
6fc2811b
JR
7033 ptr = regex;
7034 *ptr++ = '^';
ee78dc32 7035
6fc2811b
JR
7036 /* Turn pattern into a regexp and do a regexp match. */
7037 for (; *pattern; pattern++)
7038 {
7039 if (*pattern == '?')
7040 *ptr++ = '.';
7041 else if (*pattern == '*')
7042 {
7043 *ptr++ = '.';
7044 *ptr++ = '*';
7045 }
33d52f9c 7046 else
6fc2811b 7047 *ptr++ = *pattern;
ee78dc32 7048 }
6fc2811b
JR
7049 *ptr = '$';
7050 *(ptr + 1) = '\0';
7051
d88c567c
JR
7052 /* Strip out font heights and compare them seperately, since
7053 rounding error can cause mismatches. This also allows a
7054 comparison between a font that declares only a pixel height and a
7055 pattern that declares the point height.
7056 */
7057 {
7058 int font_height, pattern_height;
7059
7060 font_height = xlfd_strip_height (font_name_copy);
7061 pattern_height = xlfd_strip_height (regex);
7062
7063 /* Compare now, and don't bother doing expensive regexp matching
7064 if the heights differ. */
7065 if (font_height && pattern_height && (font_height != pattern_height))
7066 return FALSE;
7067 }
7068
6fc2811b 7069 return (fast_c_string_match_ignore_case (build_string (regex),
d88c567c 7070 font_name_copy) >= 0);
ee78dc32
GV
7071}
7072
5ca0cd71
GV
7073/* Callback functions, and a structure holding info they need, for
7074 listing system fonts on W32. We need one set of functions to do the
7075 job properly, but these don't work on NT 3.51 and earlier, so we
7076 have a second set which don't handle character sets properly to
7077 fall back on.
7078
7079 In both cases, there are two passes made. The first pass gets one
7080 font from each family, the second pass lists all the fonts from
7081 each family. */
7082
ee78dc32
GV
7083typedef struct enumfont_t
7084{
7085 HDC hdc;
7086 int numFonts;
3cb20f4a 7087 LOGFONT logfont;
ee78dc32
GV
7088 XFontStruct *size_ref;
7089 Lisp_Object *pattern;
d84b082d 7090 Lisp_Object list;
ee78dc32
GV
7091 Lisp_Object *tail;
7092} enumfont_t;
7093
d84b082d
JR
7094
7095static void
7096enum_font_maybe_add_to_list (enumfont_t *, LOGFONT *, char *, Lisp_Object);
7097
7098
8edb0a6f 7099static int CALLBACK
ee78dc32
GV
7100enum_font_cb2 (lplf, lptm, FontType, lpef)
7101 ENUMLOGFONT * lplf;
7102 NEWTEXTMETRIC * lptm;
7103 int FontType;
7104 enumfont_t * lpef;
7105{
66895301
JR
7106 /* Ignore struck out and underlined versions of fonts. */
7107 if (lplf->elfLogFont.lfStrikeOut || lplf->elfLogFont.lfUnderline)
7108 return 1;
7109
7110 /* Only return fonts with names starting with @ if they were
7111 explicitly specified, since Microsoft uses an initial @ to
7112 denote fonts for vertical writing, without providing a more
7113 convenient way of identifying them. */
7114 if (lplf->elfLogFont.lfFaceName[0] == '@'
7115 && lpef->logfont.lfFaceName[0] != '@')
5e905a57
JR
7116 return 1;
7117
4587b026
GV
7118 /* Check that the character set matches if it was specified */
7119 if (lpef->logfont.lfCharSet != DEFAULT_CHARSET &&
7120 lplf->elfLogFont.lfCharSet != lpef->logfont.lfCharSet)
5e905a57 7121 return 1;
4587b026 7122
ee78dc32
GV
7123 {
7124 char buf[100];
4587b026 7125 Lisp_Object width = Qnil;
d84b082d 7126 Lisp_Object charset_list = Qnil;
767b1ff0 7127 char *charset = NULL;
ee78dc32 7128
6fc2811b
JR
7129 /* Truetype fonts do not report their true metrics until loaded */
7130 if (FontType != RASTER_FONTTYPE)
3cb20f4a 7131 {
6fc2811b
JR
7132 if (!NILP (*(lpef->pattern)))
7133 {
7134 /* Scalable fonts are as big as you want them to be. */
7135 lplf->elfLogFont.lfHeight = lpef->logfont.lfHeight;
7136 lplf->elfLogFont.lfWidth = lpef->logfont.lfWidth;
7137 width = make_number (lpef->logfont.lfWidth);
7138 }
7139 else
7140 {
7141 lplf->elfLogFont.lfHeight = 0;
7142 lplf->elfLogFont.lfWidth = 0;
7143 }
3cb20f4a 7144 }
6fc2811b 7145
f46e6225
GV
7146 /* Make sure the height used here is the same as everywhere
7147 else (ie character height, not cell height). */
6fc2811b
JR
7148 if (lplf->elfLogFont.lfHeight > 0)
7149 {
7150 /* lptm can be trusted for RASTER fonts, but not scalable ones. */
7151 if (FontType == RASTER_FONTTYPE)
7152 lplf->elfLogFont.lfHeight = lptm->tmInternalLeading - lptm->tmHeight;
7153 else
7154 lplf->elfLogFont.lfHeight = -lplf->elfLogFont.lfHeight;
7155 }
4587b026 7156
767b1ff0
JR
7157 if (!NILP (*(lpef->pattern)))
7158 {
7159 charset = xlfd_charset_of_font (XSTRING(*(lpef->pattern))->data);
7160
644cefdf
JR
7161 /* We already checked charsets above, but DEFAULT_CHARSET
7162 slipped through. So only allow exact matches for DEFAULT_CHARSET. */
7163 if (charset
7164 && strncmp (charset, "*-*", 3) != 0
7165 && lpef->logfont.lfCharSet == DEFAULT_CHARSET
7166 && strcmp (charset, w32_to_x_charset (DEFAULT_CHARSET)) != 0)
7167 return 1;
767b1ff0
JR
7168 }
7169
d84b082d
JR
7170 if (charset)
7171 charset_list = Fcons (build_string (charset), Qnil);
7172 else
7173 charset_list = w32_to_all_x_charsets (lplf->elfLogFont.lfCharSet);
ee78dc32 7174
d84b082d
JR
7175 /* Loop through the charsets. */
7176 for ( ; CONSP (charset_list); charset_list = Fcdr (charset_list))
ee78dc32 7177 {
d84b082d
JR
7178 Lisp_Object this_charset = Fcar (charset_list);
7179 charset = XSTRING (this_charset)->data;
7180
7181 /* List bold and italic variations if w32-enable-synthesized-fonts
7182 is non-nil and this is a plain font. */
7183 if (w32_enable_synthesized_fonts
7184 && lplf->elfLogFont.lfWeight == FW_NORMAL
7185 && lplf->elfLogFont.lfItalic == FALSE)
7186 {
7187 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
7188 charset, width);
7189 /* bold. */
7190 lplf->elfLogFont.lfWeight = FW_BOLD;
7191 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
7192 charset, width);
7193 /* bold italic. */
7194 lplf->elfLogFont.lfItalic = TRUE;
7195 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
7196 charset, width);
7197 /* italic. */
7198 lplf->elfLogFont.lfWeight = FW_NORMAL;
7199 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
7200 charset, width);
7201 }
7202 else
7203 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
7204 charset, width);
ee78dc32
GV
7205 }
7206 }
6fc2811b 7207
5e905a57 7208 return 1;
ee78dc32
GV
7209}
7210
d84b082d
JR
7211static void
7212enum_font_maybe_add_to_list (lpef, logfont, match_charset, width)
7213 enumfont_t * lpef;
7214 LOGFONT * logfont;
7215 char * match_charset;
7216 Lisp_Object width;
7217{
7218 char buf[100];
7219
7220 if (!w32_to_x_font (logfont, buf, 100, match_charset))
7221 return;
7222
7223 if (NILP (*(lpef->pattern))
7224 || w32_font_match (buf, XSTRING (*(lpef->pattern))->data))
7225 {
7226 /* Check if we already listed this font. This may happen if
7227 w32_enable_synthesized_fonts is non-nil, and there are real
7228 bold and italic versions of the font. */
7229 Lisp_Object font_name = build_string (buf);
7230 if (NILP (Fmember (font_name, lpef->list)))
7231 {
7232 *lpef->tail = Fcons (Fcons (build_string (buf), width), Qnil);
7233 lpef->tail = &(XCDR (*lpef->tail));
7234 lpef->numFonts++;
7235 }
7236 }
7237}
7238
7239
8edb0a6f 7240static int CALLBACK
ee78dc32
GV
7241enum_font_cb1 (lplf, lptm, FontType, lpef)
7242 ENUMLOGFONT * lplf;
7243 NEWTEXTMETRIC * lptm;
7244 int FontType;
7245 enumfont_t * lpef;
7246{
7247 return EnumFontFamilies (lpef->hdc,
7248 lplf->elfLogFont.lfFaceName,
7249 (FONTENUMPROC) enum_font_cb2,
7250 (LPARAM) lpef);
7251}
7252
7253
8edb0a6f 7254static int CALLBACK
5ca0cd71
GV
7255enum_fontex_cb2 (lplf, lptm, font_type, lpef)
7256 ENUMLOGFONTEX * lplf;
7257 NEWTEXTMETRICEX * lptm;
7258 int font_type;
7259 enumfont_t * lpef;
7260{
7261 /* We are not interested in the extra info we get back from the 'Ex
7262 version - only the fact that we get character set variations
7263 enumerated seperately. */
7264 return enum_font_cb2 ((ENUMLOGFONT *) lplf, (NEWTEXTMETRIC *) lptm,
7265 font_type, lpef);
7266}
7267
8edb0a6f 7268static int CALLBACK
5ca0cd71
GV
7269enum_fontex_cb1 (lplf, lptm, font_type, lpef)
7270 ENUMLOGFONTEX * lplf;
7271 NEWTEXTMETRICEX * lptm;
7272 int font_type;
7273 enumfont_t * lpef;
7274{
7275 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
7276 FARPROC enum_font_families_ex
7277 = GetProcAddress ( gdi32, "EnumFontFamiliesExA");
7278 /* We don't really expect EnumFontFamiliesEx to disappear once we
7279 get here, so don't bother handling it gracefully. */
7280 if (enum_font_families_ex == NULL)
7281 error ("gdi32.dll has disappeared!");
7282 return enum_font_families_ex (lpef->hdc,
7283 &lplf->elfLogFont,
7284 (FONTENUMPROC) enum_fontex_cb2,
7285 (LPARAM) lpef, 0);
7286}
7287
4587b026
GV
7288/* Interface to fontset handler. (adapted from mw32font.c in Meadow
7289 and xterm.c in Emacs 20.3) */
7290
8edb0a6f 7291static Lisp_Object w32_list_bdf_fonts (Lisp_Object pattern, int max_names)
33d52f9c
GV
7292{
7293 char *fontname, *ptnstr;
7294 Lisp_Object list, tem, newlist = Qnil;
55dcfc15 7295 int n_fonts = 0;
33d52f9c
GV
7296
7297 list = Vw32_bdf_filename_alist;
7298 ptnstr = XSTRING (pattern)->data;
7299
8e713be6 7300 for ( ; CONSP (list); list = XCDR (list))
33d52f9c 7301 {
8e713be6 7302 tem = XCAR (list);
33d52f9c 7303 if (CONSP (tem))
8e713be6 7304 fontname = XSTRING (XCAR (tem))->data;
33d52f9c
GV
7305 else if (STRINGP (tem))
7306 fontname = XSTRING (tem)->data;
7307 else
7308 continue;
7309
7310 if (w32_font_match (fontname, ptnstr))
5ca0cd71 7311 {
8e713be6 7312 newlist = Fcons (XCAR (tem), newlist);
5ca0cd71
GV
7313 n_fonts++;
7314 if (n_fonts >= max_names)
7315 break;
7316 }
33d52f9c
GV
7317 }
7318
7319 return newlist;
7320}
7321
5ca0cd71 7322
4587b026
GV
7323/* Return a list of names of available fonts matching PATTERN on frame
7324 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
7325 to be listed. Frame F NULL means we have not yet created any
7326 frame, which means we can't get proper size info, as we don't have
7327 a device context to use for GetTextMetrics.
7328 MAXNAMES sets a limit on how many fonts to match. */
7329
7330Lisp_Object
dc220243
JR
7331w32_list_fonts (f, pattern, size, maxnames)
7332 struct frame *f;
7333 Lisp_Object pattern;
7334 int size;
7335 int maxnames;
4587b026 7336{
6fc2811b 7337 Lisp_Object patterns, key = Qnil, tem, tpat;
4587b026 7338 Lisp_Object list = Qnil, newlist = Qnil, second_best = Qnil;
33d52f9c 7339 struct w32_display_info *dpyinfo = &one_w32_display_info;
5ca0cd71 7340 int n_fonts = 0;
396594fe 7341
4587b026
GV
7342 patterns = Fassoc (pattern, Valternate_fontname_alist);
7343 if (NILP (patterns))
7344 patterns = Fcons (pattern, Qnil);
7345
8e713be6 7346 for (; CONSP (patterns); patterns = XCDR (patterns))
4587b026
GV
7347 {
7348 enumfont_t ef;
767b1ff0 7349 int codepage;
4587b026 7350
8e713be6 7351 tpat = XCAR (patterns);
4587b026 7352
767b1ff0
JR
7353 if (!STRINGP (tpat))
7354 continue;
7355
7356 /* Avoid expensive EnumFontFamilies functions if we are not
7357 going to be able to output one of these anyway. */
7358 codepage = w32_codepage_for_font (XSTRING (tpat)->data);
7359 if (codepage != CP_8BIT && codepage != CP_UNICODE
ceb12877
AI
7360 && codepage != CP_DEFAULT && codepage != CP_UNKNOWN
7361 && !IsValidCodePage(codepage))
767b1ff0
JR
7362 continue;
7363
4587b026
GV
7364 /* See if we cached the result for this particular query.
7365 The cache is an alist of the form:
7366 ((PATTERN (FONTNAME . WIDTH) ...) ...)
7367 */
8e713be6 7368 if (tem = XCDR (dpyinfo->name_list_element),
33d52f9c 7369 !NILP (list = Fassoc (tpat, tem)))
4587b026
GV
7370 {
7371 list = Fcdr_safe (list);
7372 /* We have a cached list. Don't have to get the list again. */
7373 goto label_cached;
7374 }
7375
7376 BLOCK_INPUT;
7377 /* At first, put PATTERN in the cache. */
7378 list = Qnil;
33d52f9c 7379 ef.pattern = &tpat;
d84b082d 7380 ef.list = list;
33d52f9c 7381 ef.tail = &list;
4587b026 7382 ef.numFonts = 0;
33d52f9c 7383
5ca0cd71
GV
7384 /* Use EnumFontFamiliesEx where it is available, as it knows
7385 about character sets. Fall back to EnumFontFamilies for
7386 older versions of NT that don't support the 'Ex function. */
767b1ff0 7387 x_to_w32_font (XSTRING (tpat)->data, &ef.logfont);
4587b026 7388 {
5ca0cd71
GV
7389 LOGFONT font_match_pattern;
7390 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
7391 FARPROC enum_font_families_ex
7392 = GetProcAddress ( gdi32, "EnumFontFamiliesExA");
7393
7394 /* We do our own pattern matching so we can handle wildcards. */
7395 font_match_pattern.lfFaceName[0] = 0;
7396 font_match_pattern.lfPitchAndFamily = 0;
7397 /* We can use the charset, because if it is a wildcard it will
7398 be DEFAULT_CHARSET anyway. */
7399 font_match_pattern.lfCharSet = ef.logfont.lfCharSet;
7400
33d52f9c 7401 ef.hdc = GetDC (dpyinfo->root_window);
4587b026 7402
5ca0cd71
GV
7403 if (enum_font_families_ex)
7404 enum_font_families_ex (ef.hdc,
7405 &font_match_pattern,
7406 (FONTENUMPROC) enum_fontex_cb1,
7407 (LPARAM) &ef, 0);
7408 else
7409 EnumFontFamilies (ef.hdc, NULL, (FONTENUMPROC) enum_font_cb1,
7410 (LPARAM)&ef);
4587b026 7411
33d52f9c 7412 ReleaseDC (dpyinfo->root_window, ef.hdc);
4587b026
GV
7413 }
7414
7415 UNBLOCK_INPUT;
7416
7417 /* Make a list of the fonts we got back.
7418 Store that in the font cache for the display. */
f3fbd155
KR
7419 XSETCDR (dpyinfo->name_list_element,
7420 Fcons (Fcons (tpat, list),
7421 XCDR (dpyinfo->name_list_element)));
4587b026
GV
7422
7423 label_cached:
7424 if (NILP (list)) continue; /* Try the remaining alternatives. */
7425
7426 newlist = second_best = Qnil;
7427
7428 /* Make a list of the fonts that have the right width. */
8e713be6 7429 for (; CONSP (list); list = XCDR (list))
4587b026
GV
7430 {
7431 int found_size;
8e713be6 7432 tem = XCAR (list);
4587b026
GV
7433
7434 if (!CONSP (tem))
7435 continue;
8e713be6 7436 if (NILP (XCAR (tem)))
4587b026
GV
7437 continue;
7438 if (!size)
7439 {
8e713be6 7440 newlist = Fcons (XCAR (tem), newlist);
5ca0cd71
GV
7441 n_fonts++;
7442 if (n_fonts >= maxnames)
7443 break;
7444 else
7445 continue;
4587b026 7446 }
8e713be6 7447 if (!INTEGERP (XCDR (tem)))
4587b026
GV
7448 {
7449 /* Since we don't yet know the size of the font, we must
7450 load it and try GetTextMetrics. */
4587b026
GV
7451 W32FontStruct thisinfo;
7452 LOGFONT lf;
7453 HDC hdc;
7454 HANDLE oldobj;
7455
8e713be6 7456 if (!x_to_w32_font (XSTRING (XCAR (tem))->data, &lf))
4587b026
GV
7457 continue;
7458
7459 BLOCK_INPUT;
33d52f9c 7460 thisinfo.bdf = NULL;
4587b026
GV
7461 thisinfo.hfont = CreateFontIndirect (&lf);
7462 if (thisinfo.hfont == NULL)
7463 continue;
7464
7465 hdc = GetDC (dpyinfo->root_window);
7466 oldobj = SelectObject (hdc, thisinfo.hfont);
7467 if (GetTextMetrics (hdc, &thisinfo.tm))
f3fbd155 7468 XSETCDR (tem, make_number (FONT_WIDTH (&thisinfo)));
4587b026 7469 else
f3fbd155 7470 XSETCDR (tem, make_number (0));
4587b026
GV
7471 SelectObject (hdc, oldobj);
7472 ReleaseDC (dpyinfo->root_window, hdc);
7473 DeleteObject(thisinfo.hfont);
7474 UNBLOCK_INPUT;
7475 }
8e713be6 7476 found_size = XINT (XCDR (tem));
4587b026 7477 if (found_size == size)
5ca0cd71 7478 {
8e713be6 7479 newlist = Fcons (XCAR (tem), newlist);
5ca0cd71
GV
7480 n_fonts++;
7481 if (n_fonts >= maxnames)
7482 break;
7483 }
4587b026
GV
7484 /* keep track of the closest matching size in case
7485 no exact match is found. */
7486 else if (found_size > 0)
7487 {
7488 if (NILP (second_best))
7489 second_best = tem;
5ca0cd71 7490
4587b026
GV
7491 else if (found_size < size)
7492 {
8e713be6
KR
7493 if (XINT (XCDR (second_best)) > size
7494 || XINT (XCDR (second_best)) < found_size)
4587b026
GV
7495 second_best = tem;
7496 }
7497 else
7498 {
8e713be6
KR
7499 if (XINT (XCDR (second_best)) > size
7500 && XINT (XCDR (second_best)) >
4587b026
GV
7501 found_size)
7502 second_best = tem;
7503 }
7504 }
7505 }
7506
7507 if (!NILP (newlist))
7508 break;
7509 else if (!NILP (second_best))
7510 {
8e713be6 7511 newlist = Fcons (XCAR (second_best), Qnil);
4587b026
GV
7512 break;
7513 }
7514 }
7515
33d52f9c 7516 /* Include any bdf fonts. */
5ca0cd71 7517 if (n_fonts < maxnames)
33d52f9c
GV
7518 {
7519 Lisp_Object combined[2];
5ca0cd71 7520 combined[0] = w32_list_bdf_fonts (pattern, maxnames - n_fonts);
33d52f9c
GV
7521 combined[1] = newlist;
7522 newlist = Fnconc(2, combined);
7523 }
7524
4587b026
GV
7525 return newlist;
7526}
7527
5ca0cd71 7528
4587b026
GV
7529/* Return a pointer to struct font_info of font FONT_IDX of frame F. */
7530struct font_info *
7531w32_get_font_info (f, font_idx)
7532 FRAME_PTR f;
7533 int font_idx;
7534{
7535 return (FRAME_W32_FONT_TABLE (f) + font_idx);
7536}
7537
7538
7539struct font_info*
7540w32_query_font (struct frame *f, char *fontname)
7541{
7542 int i;
7543 struct font_info *pfi;
7544
7545 pfi = FRAME_W32_FONT_TABLE (f);
7546
7547 for (i = 0; i < one_w32_display_info.n_fonts ;i++, pfi++)
7548 {
7549 if (strcmp(pfi->name, fontname) == 0) return pfi;
7550 }
7551
7552 return NULL;
7553}
7554
7555/* Find a CCL program for a font specified by FONTP, and set the member
7556 `encoder' of the structure. */
7557
7558void
7559w32_find_ccl_program (fontp)
7560 struct font_info *fontp;
7561{
3545439c 7562 Lisp_Object list, elt;
4587b026 7563
8e713be6 7564 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
4587b026 7565 {
8e713be6 7566 elt = XCAR (list);
4587b026 7567 if (CONSP (elt)
8e713be6
KR
7568 && STRINGP (XCAR (elt))
7569 && (fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
4587b026 7570 >= 0))
3545439c
KH
7571 break;
7572 }
7573 if (! NILP (list))
7574 {
17eedd00
KH
7575 struct ccl_program *ccl
7576 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
3545439c 7577
8e713be6 7578 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
3545439c
KH
7579 xfree (ccl);
7580 else
7581 fontp->font_encoder = ccl;
4587b026
GV
7582 }
7583}
7584
7585\f
8edb0a6f
JR
7586/* Find BDF files in a specified directory. (use GCPRO when calling,
7587 as this calls lisp to get a directory listing). */
7588static Lisp_Object
7589w32_find_bdf_fonts_in_dir (Lisp_Object directory)
7590{
7591 Lisp_Object filelist, list = Qnil;
7592 char fontname[100];
7593
7594 if (!STRINGP(directory))
7595 return Qnil;
7596
7597 filelist = Fdirectory_files (directory, Qt,
7598 build_string (".*\\.[bB][dD][fF]"), Qt);
7599
7600 for ( ; CONSP(filelist); filelist = XCDR (filelist))
7601 {
7602 Lisp_Object filename = XCAR (filelist);
7603 if (w32_BDF_to_x_font (XSTRING (filename)->data, fontname, 100))
7604 store_in_alist (&list, build_string (fontname), filename);
7605 }
7606 return list;
7607}
7608
6fc2811b
JR
7609DEFUN ("w32-find-bdf-fonts", Fw32_find_bdf_fonts, Sw32_find_bdf_fonts,
7610 1, 1, 0,
b3700ae7
JR
7611 doc: /* Return a list of BDF fonts in DIR.
7612The list is suitable for appending to w32-bdf-filename-alist. Fonts
7613which do not contain an xlfd description will not be included in the
7614list. DIR may be a list of directories. */)
6fc2811b
JR
7615 (directory)
7616 Lisp_Object directory;
7617{
7618 Lisp_Object list = Qnil;
7619 struct gcpro gcpro1, gcpro2;
ee78dc32 7620
6fc2811b
JR
7621 if (!CONSP (directory))
7622 return w32_find_bdf_fonts_in_dir (directory);
ee78dc32 7623
6fc2811b 7624 for ( ; CONSP (directory); directory = XCDR (directory))
ee78dc32 7625 {
6fc2811b
JR
7626 Lisp_Object pair[2];
7627 pair[0] = list;
7628 pair[1] = Qnil;
7629 GCPRO2 (directory, list);
7630 pair[1] = w32_find_bdf_fonts_in_dir( XCAR (directory) );
7631 list = Fnconc( 2, pair );
7632 UNGCPRO;
7633 }
7634 return list;
7635}
ee78dc32 7636
6fc2811b
JR
7637\f
7638DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
74e1aeec 7639 doc: /* Internal function called by `color-defined-p', which see. */)
6fc2811b
JR
7640 (color, frame)
7641 Lisp_Object color, frame;
7642{
7643 XColor foo;
7644 FRAME_PTR f = check_x_frame (frame);
ee78dc32 7645
b7826503 7646 CHECK_STRING (color);
ee78dc32 7647
6fc2811b
JR
7648 if (w32_defined_color (f, XSTRING (color)->data, &foo, 0))
7649 return Qt;
7650 else
7651 return Qnil;
7652}
ee78dc32 7653
2d764c78 7654DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
74e1aeec 7655 doc: /* Internal function called by `color-values', which see. */)
ee78dc32
GV
7656 (color, frame)
7657 Lisp_Object color, frame;
7658{
6fc2811b 7659 XColor foo;
ee78dc32
GV
7660 FRAME_PTR f = check_x_frame (frame);
7661
b7826503 7662 CHECK_STRING (color);
ee78dc32 7663
6fc2811b 7664 if (w32_defined_color (f, XSTRING (color)->data, &foo, 0))
ee78dc32
GV
7665 {
7666 Lisp_Object rgb[3];
7667
6fc2811b
JR
7668 rgb[0] = make_number ((GetRValue (foo.pixel) << 8)
7669 | GetRValue (foo.pixel));
7670 rgb[1] = make_number ((GetGValue (foo.pixel) << 8)
7671 | GetGValue (foo.pixel));
7672 rgb[2] = make_number ((GetBValue (foo.pixel) << 8)
7673 | GetBValue (foo.pixel));
ee78dc32
GV
7674 return Flist (3, rgb);
7675 }
7676 else
7677 return Qnil;
7678}
7679
2d764c78 7680DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
74e1aeec 7681 doc: /* Internal function called by `display-color-p', which see. */)
ee78dc32
GV
7682 (display)
7683 Lisp_Object display;
7684{
fbd6baed 7685 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
7686
7687 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 2)
7688 return Qnil;
7689
7690 return Qt;
7691}
7692
74e1aeec
JR
7693DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p,
7694 Sx_display_grayscale_p, 0, 1, 0,
7695 doc: /* Return t if the X display supports shades of gray.
7696Note that color displays do support shades of gray.
7697The optional argument DISPLAY specifies which display to ask about.
7698DISPLAY should be either a frame or a display name (a string).
7699If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
7700 (display)
7701 Lisp_Object display;
7702{
fbd6baed 7703 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
7704
7705 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 1)
7706 return Qnil;
7707
7708 return Qt;
7709}
7710
74e1aeec
JR
7711DEFUN ("x-display-pixel-width", Fx_display_pixel_width,
7712 Sx_display_pixel_width, 0, 1, 0,
7713 doc: /* Returns the width in pixels of DISPLAY.
7714The optional argument DISPLAY specifies which display to ask about.
7715DISPLAY should be either a frame or a display name (a string).
7716If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
7717 (display)
7718 Lisp_Object display;
7719{
fbd6baed 7720 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
7721
7722 return make_number (dpyinfo->width);
7723}
7724
7725DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
74e1aeec
JR
7726 Sx_display_pixel_height, 0, 1, 0,
7727 doc: /* Returns the height in pixels of DISPLAY.
7728The optional argument DISPLAY specifies which display to ask about.
7729DISPLAY should be either a frame or a display name (a string).
7730If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
7731 (display)
7732 Lisp_Object display;
7733{
fbd6baed 7734 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
7735
7736 return make_number (dpyinfo->height);
7737}
7738
7739DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
74e1aeec
JR
7740 0, 1, 0,
7741 doc: /* Returns the number of bitplanes of DISPLAY.
7742The optional argument DISPLAY specifies which display to ask about.
7743DISPLAY should be either a frame or a display name (a string).
7744If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
7745 (display)
7746 Lisp_Object display;
7747{
fbd6baed 7748 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
7749
7750 return make_number (dpyinfo->n_planes * dpyinfo->n_cbits);
7751}
7752
7753DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
74e1aeec
JR
7754 0, 1, 0,
7755 doc: /* Returns the number of color cells of DISPLAY.
7756The optional argument DISPLAY specifies which display to ask about.
7757DISPLAY should be either a frame or a display name (a string).
7758If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
7759 (display)
7760 Lisp_Object display;
7761{
fbd6baed 7762 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
7763 HDC hdc;
7764 int cap;
7765
5ac45f98
GV
7766 hdc = GetDC (dpyinfo->root_window);
7767 if (dpyinfo->has_palette)
7768 cap = GetDeviceCaps (hdc,SIZEPALETTE);
7769 else
7770 cap = GetDeviceCaps (hdc,NUMCOLORS);
abf8c61b 7771
007776bc
JB
7772 /* We force 24+ bit depths to 24-bit, both to prevent an overflow
7773 and because probably is more meaningful on Windows anyway */
abf8c61b 7774 if (cap < 0)
007776bc 7775 cap = 1 << min(dpyinfo->n_planes * dpyinfo->n_cbits, 24);
ee78dc32
GV
7776
7777 ReleaseDC (dpyinfo->root_window, hdc);
7778
7779 return make_number (cap);
7780}
7781
7782DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
7783 Sx_server_max_request_size,
74e1aeec
JR
7784 0, 1, 0,
7785 doc: /* Returns the maximum request size of the server of DISPLAY.
7786The optional argument DISPLAY specifies which display to ask about.
7787DISPLAY should be either a frame or a display name (a string).
7788If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
7789 (display)
7790 Lisp_Object display;
7791{
fbd6baed 7792 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
7793
7794 return make_number (1);
7795}
7796
7797DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
74e1aeec
JR
7798 doc: /* Returns the vendor ID string of the W32 system (Microsoft).
7799The optional argument DISPLAY specifies which display to ask about.
7800DISPLAY should be either a frame or a display name (a string).
7801If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
7802 (display)
7803 Lisp_Object display;
7804{
dfff8a69 7805 return build_string ("Microsoft Corp.");
ee78dc32
GV
7806}
7807
7808DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
74e1aeec
JR
7809 doc: /* Returns the version numbers of the server of DISPLAY.
7810The value is a list of three integers: the major and minor
7811version numbers, and the vendor-specific release
7812number. See also the function `x-server-vendor'.
7813
7814The optional argument DISPLAY specifies which display to ask about.
7815DISPLAY should be either a frame or a display name (a string).
7816If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
7817 (display)
7818 Lisp_Object display;
7819{
fbd6baed 7820 return Fcons (make_number (w32_major_version),
58e0f0e4
AI
7821 Fcons (make_number (w32_minor_version),
7822 Fcons (make_number (w32_build_number), Qnil)));
ee78dc32
GV
7823}
7824
7825DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
74e1aeec
JR
7826 doc: /* Returns the number of screens on the server of DISPLAY.
7827The optional argument DISPLAY specifies which display to ask about.
7828DISPLAY should be either a frame or a display name (a string).
7829If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
7830 (display)
7831 Lisp_Object display;
7832{
ee78dc32
GV
7833 return make_number (1);
7834}
7835
74e1aeec
JR
7836DEFUN ("x-display-mm-height", Fx_display_mm_height,
7837 Sx_display_mm_height, 0, 1, 0,
7838 doc: /* Returns the height in millimeters of DISPLAY.
7839The optional argument DISPLAY specifies which display to ask about.
7840DISPLAY should be either a frame or a display name (a string).
7841If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
7842 (display)
7843 Lisp_Object display;
7844{
fbd6baed 7845 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
7846 HDC hdc;
7847 int cap;
7848
5ac45f98 7849 hdc = GetDC (dpyinfo->root_window);
3c190163 7850
ee78dc32 7851 cap = GetDeviceCaps (hdc, VERTSIZE);
3c190163 7852
ee78dc32
GV
7853 ReleaseDC (dpyinfo->root_window, hdc);
7854
7855 return make_number (cap);
7856}
7857
7858DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
74e1aeec
JR
7859 doc: /* Returns the width in millimeters of DISPLAY.
7860The optional argument DISPLAY specifies which display to ask about.
7861DISPLAY should be either a frame or a display name (a string).
7862If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
7863 (display)
7864 Lisp_Object display;
7865{
fbd6baed 7866 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
7867
7868 HDC hdc;
7869 int cap;
7870
5ac45f98 7871 hdc = GetDC (dpyinfo->root_window);
3c190163 7872
ee78dc32 7873 cap = GetDeviceCaps (hdc, HORZSIZE);
3c190163 7874
ee78dc32
GV
7875 ReleaseDC (dpyinfo->root_window, hdc);
7876
7877 return make_number (cap);
7878}
7879
7880DEFUN ("x-display-backing-store", Fx_display_backing_store,
74e1aeec
JR
7881 Sx_display_backing_store, 0, 1, 0,
7882 doc: /* Returns an indication of whether DISPLAY does backing store.
7883The value may be `always', `when-mapped', or `not-useful'.
7884The optional argument DISPLAY specifies which display to ask about.
7885DISPLAY should be either a frame or a display name (a string).
7886If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
7887 (display)
7888 Lisp_Object display;
7889{
7890 return intern ("not-useful");
7891}
7892
7893DEFUN ("x-display-visual-class", Fx_display_visual_class,
74e1aeec
JR
7894 Sx_display_visual_class, 0, 1, 0,
7895 doc: /* Returns the visual class of DISPLAY.
7896The value is one of the symbols `static-gray', `gray-scale',
7897`static-color', `pseudo-color', `true-color', or `direct-color'.
7898
7899The optional argument DISPLAY specifies which display to ask about.
7900DISPLAY should be either a frame or a display name (a string).
7901If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
7902 (display)
7903 Lisp_Object display;
7904{
fbd6baed 7905 struct w32_display_info *dpyinfo = check_x_display_info (display);
abf8c61b 7906 Lisp_Object result = Qnil;
ee78dc32 7907
abf8c61b
AI
7908 if (dpyinfo->has_palette)
7909 result = intern ("pseudo-color");
7910 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 1)
7911 result = intern ("static-grey");
7912 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 4)
7913 result = intern ("static-color");
7914 else if (dpyinfo->n_planes * dpyinfo->n_cbits > 8)
7915 result = intern ("true-color");
ee78dc32 7916
abf8c61b 7917 return result;
ee78dc32
GV
7918}
7919
7920DEFUN ("x-display-save-under", Fx_display_save_under,
74e1aeec
JR
7921 Sx_display_save_under, 0, 1, 0,
7922 doc: /* Returns t if DISPLAY supports the save-under feature.
7923The optional argument DISPLAY specifies which display to ask about.
7924DISPLAY should be either a frame or a display name (a string).
7925If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
7926 (display)
7927 Lisp_Object display;
7928{
6fc2811b
JR
7929 return Qnil;
7930}
7931\f
7932int
7933x_pixel_width (f)
7934 register struct frame *f;
7935{
7936 return PIXEL_WIDTH (f);
7937}
7938
7939int
7940x_pixel_height (f)
7941 register struct frame *f;
7942{
7943 return PIXEL_HEIGHT (f);
7944}
7945
7946int
7947x_char_width (f)
7948 register struct frame *f;
7949{
7950 return FONT_WIDTH (f->output_data.w32->font);
7951}
7952
7953int
7954x_char_height (f)
7955 register struct frame *f;
7956{
7957 return f->output_data.w32->line_height;
7958}
7959
7960int
7961x_screen_planes (f)
7962 register struct frame *f;
7963{
7964 return FRAME_W32_DISPLAY_INFO (f)->n_planes;
7965}
7966\f
7967/* Return the display structure for the display named NAME.
7968 Open a new connection if necessary. */
7969
7970struct w32_display_info *
7971x_display_info_for_name (name)
7972 Lisp_Object name;
7973{
7974 Lisp_Object names;
7975 struct w32_display_info *dpyinfo;
7976
b7826503 7977 CHECK_STRING (name);
6fc2811b
JR
7978
7979 for (dpyinfo = &one_w32_display_info, names = w32_display_name_list;
7980 dpyinfo;
7981 dpyinfo = dpyinfo->next, names = XCDR (names))
7982 {
7983 Lisp_Object tem;
7984 tem = Fstring_equal (XCAR (XCAR (names)), name);
7985 if (!NILP (tem))
7986 return dpyinfo;
7987 }
7988
7989 /* Use this general default value to start with. */
7990 Vx_resource_name = Vinvocation_name;
7991
7992 validate_x_resource_name ();
7993
7994 dpyinfo = w32_term_init (name, (unsigned char *)0,
7995 (char *) XSTRING (Vx_resource_name)->data);
7996
7997 if (dpyinfo == 0)
7998 error ("Cannot connect to server %s", XSTRING (name)->data);
7999
8000 w32_in_use = 1;
8001 XSETFASTINT (Vwindow_system_version, 3);
8002
8003 return dpyinfo;
8004}
8005
8006DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
74e1aeec
JR
8007 1, 3, 0, doc: /* Open a connection to a server.
8008DISPLAY is the name of the display to connect to.
8009Optional second arg XRM-STRING is a string of resources in xrdb format.
8010If the optional third arg MUST-SUCCEED is non-nil,
8011terminate Emacs if we can't open the connection. */)
6fc2811b
JR
8012 (display, xrm_string, must_succeed)
8013 Lisp_Object display, xrm_string, must_succeed;
8014{
8015 unsigned char *xrm_option;
8016 struct w32_display_info *dpyinfo;
8017
74e1aeec
JR
8018 /* If initialization has already been done, return now to avoid
8019 overwriting critical parts of one_w32_display_info. */
8020 if (w32_in_use)
8021 return Qnil;
8022
b7826503 8023 CHECK_STRING (display);
6fc2811b 8024 if (! NILP (xrm_string))
b7826503 8025 CHECK_STRING (xrm_string);
6fc2811b
JR
8026
8027 if (! EQ (Vwindow_system, intern ("w32")))
8028 error ("Not using Microsoft Windows");
8029
8030 /* Allow color mapping to be defined externally; first look in user's
8031 HOME directory, then in Emacs etc dir for a file called rgb.txt. */
8032 {
8033 Lisp_Object color_file;
8034 struct gcpro gcpro1;
8035
8036 color_file = build_string("~/rgb.txt");
8037
8038 GCPRO1 (color_file);
8039
8040 if (NILP (Ffile_readable_p (color_file)))
8041 color_file =
8042 Fexpand_file_name (build_string ("rgb.txt"),
8043 Fsymbol_value (intern ("data-directory")));
8044
8045 Vw32_color_map = Fw32_load_color_file (color_file);
8046
8047 UNGCPRO;
8048 }
8049 if (NILP (Vw32_color_map))
8050 Vw32_color_map = Fw32_default_color_map ();
8051
8052 if (! NILP (xrm_string))
8053 xrm_option = (unsigned char *) XSTRING (xrm_string)->data;
8054 else
8055 xrm_option = (unsigned char *) 0;
8056
8057 /* Use this general default value to start with. */
8058 /* First remove .exe suffix from invocation-name - it looks ugly. */
8059 {
8060 char basename[ MAX_PATH ], *str;
8061
8062 strcpy (basename, XSTRING (Vinvocation_name)->data);
8063 str = strrchr (basename, '.');
8064 if (str) *str = 0;
8065 Vinvocation_name = build_string (basename);
8066 }
8067 Vx_resource_name = Vinvocation_name;
8068
8069 validate_x_resource_name ();
8070
8071 /* This is what opens the connection and sets x_current_display.
8072 This also initializes many symbols, such as those used for input. */
8073 dpyinfo = w32_term_init (display, xrm_option,
8074 (char *) XSTRING (Vx_resource_name)->data);
8075
8076 if (dpyinfo == 0)
8077 {
8078 if (!NILP (must_succeed))
8079 fatal ("Cannot connect to server %s.\n",
8080 XSTRING (display)->data);
8081 else
8082 error ("Cannot connect to server %s", XSTRING (display)->data);
8083 }
8084
8085 w32_in_use = 1;
8086
8087 XSETFASTINT (Vwindow_system_version, 3);
8088 return Qnil;
8089}
8090
8091DEFUN ("x-close-connection", Fx_close_connection,
8092 Sx_close_connection, 1, 1, 0,
74e1aeec
JR
8093 doc: /* Close the connection to DISPLAY's server.
8094For DISPLAY, specify either a frame or a display name (a string).
8095If DISPLAY is nil, that stands for the selected frame's display. */)
6fc2811b
JR
8096 (display)
8097 Lisp_Object display;
8098{
8099 struct w32_display_info *dpyinfo = check_x_display_info (display);
8100 int i;
8101
8102 if (dpyinfo->reference_count > 0)
8103 error ("Display still has frames on it");
8104
8105 BLOCK_INPUT;
8106 /* Free the fonts in the font table. */
8107 for (i = 0; i < dpyinfo->n_fonts; i++)
8108 if (dpyinfo->font_table[i].name)
8109 {
126f2e35
JR
8110 if (dpyinfo->font_table[i].name != dpyinfo->font_table[i].full_name)
8111 xfree (dpyinfo->font_table[i].full_name);
6fc2811b 8112 xfree (dpyinfo->font_table[i].name);
6fc2811b
JR
8113 w32_unload_font (dpyinfo, dpyinfo->font_table[i].font);
8114 }
8115 x_destroy_all_bitmaps (dpyinfo);
8116
8117 x_delete_display (dpyinfo);
8118 UNBLOCK_INPUT;
8119
8120 return Qnil;
8121}
8122
8123DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
74e1aeec 8124 doc: /* Return the list of display names that Emacs has connections to. */)
6fc2811b
JR
8125 ()
8126{
8127 Lisp_Object tail, result;
8128
8129 result = Qnil;
8130 for (tail = w32_display_name_list; ! NILP (tail); tail = XCDR (tail))
8131 result = Fcons (XCAR (XCAR (tail)), result);
8132
8133 return result;
8134}
8135
8136DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
0a332240
PJ
8137 doc: /* This is a noop on W32 systems. */)
8138 (on, display)
8139 Lisp_Object display, on;
6fc2811b 8140{
6fc2811b
JR
8141 return Qnil;
8142}
8143
8144\f
8145\f
8146/***********************************************************************
8147 Image types
8148 ***********************************************************************/
8149
8150/* Value is the number of elements of vector VECTOR. */
8151
8152#define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR))
8153
8154/* List of supported image types. Use define_image_type to add new
8155 types. Use lookup_image_type to find a type for a given symbol. */
8156
8157static struct image_type *image_types;
8158
6fc2811b
JR
8159/* The symbol `image' which is the car of the lists used to represent
8160 images in Lisp. */
8161
8162extern Lisp_Object Qimage;
8163
8164/* The symbol `xbm' which is used as the type symbol for XBM images. */
8165
8166Lisp_Object Qxbm;
8167
8168/* Keywords. */
8169
6fc2811b 8170extern Lisp_Object QCwidth, QCheight, QCforeground, QCbackground, QCfile;
77814035
KS
8171extern Lisp_Object QCdata, QCtype;
8172Lisp_Object QCascent, QCmargin, QCrelief;
a93f4566 8173Lisp_Object QCconversion, QCcolor_symbols, QCheuristic_mask;
3cf3436e 8174Lisp_Object QCindex, QCmatrix, QCcolor_adjustment, QCmask;
6fc2811b
JR
8175
8176/* Other symbols. */
8177
3cf3436e 8178Lisp_Object Qlaplace, Qemboss, Qedge_detection, Qheuristic;
6fc2811b
JR
8179
8180/* Time in seconds after which images should be removed from the cache
8181 if not displayed. */
8182
8183Lisp_Object Vimage_cache_eviction_delay;
8184
8185/* Function prototypes. */
8186
8187static void define_image_type P_ ((struct image_type *type));
8188static struct image_type *lookup_image_type P_ ((Lisp_Object symbol));
8189static void image_error P_ ((char *format, Lisp_Object, Lisp_Object));
8190static void x_laplace P_ ((struct frame *, struct image *));
3cf3436e 8191static void x_emboss P_ ((struct frame *, struct image *));
6fc2811b
JR
8192static int x_build_heuristic_mask P_ ((struct frame *, struct image *,
8193 Lisp_Object));
8194
dfff8a69 8195
6fc2811b
JR
8196/* Define a new image type from TYPE. This adds a copy of TYPE to
8197 image_types and adds the symbol *TYPE->type to Vimage_types. */
8198
8199static void
8200define_image_type (type)
8201 struct image_type *type;
8202{
8203 /* Make a copy of TYPE to avoid a bus error in a dumped Emacs.
8204 The initialized data segment is read-only. */
8205 struct image_type *p = (struct image_type *) xmalloc (sizeof *p);
8206 bcopy (type, p, sizeof *p);
8207 p->next = image_types;
8208 image_types = p;
8209 Vimage_types = Fcons (*p->type, Vimage_types);
8210}
8211
8212
8213/* Look up image type SYMBOL, and return a pointer to its image_type
8214 structure. Value is null if SYMBOL is not a known image type. */
8215
8216static INLINE struct image_type *
8217lookup_image_type (symbol)
8218 Lisp_Object symbol;
8219{
8220 struct image_type *type;
8221
8222 for (type = image_types; type; type = type->next)
8223 if (EQ (symbol, *type->type))
8224 break;
8225
8226 return type;
8227}
8228
8229
8230/* Value is non-zero if OBJECT is a valid Lisp image specification. A
8231 valid image specification is a list whose car is the symbol
8232 `image', and whose rest is a property list. The property list must
8233 contain a value for key `:type'. That value must be the name of a
8234 supported image type. The rest of the property list depends on the
8235 image type. */
8236
8237int
8238valid_image_p (object)
8239 Lisp_Object object;
8240{
8241 int valid_p = 0;
8242
8243 if (CONSP (object) && EQ (XCAR (object), Qimage))
8244 {
3cf3436e
JR
8245 Lisp_Object tem;
8246
8247 for (tem = XCDR (object); CONSP (tem); tem = XCDR (tem))
8248 if (EQ (XCAR (tem), QCtype))
8249 {
8250 tem = XCDR (tem);
8251 if (CONSP (tem) && SYMBOLP (XCAR (tem)))
8252 {
8253 struct image_type *type;
8254 type = lookup_image_type (XCAR (tem));
8255 if (type)
8256 valid_p = type->valid_p (object);
8257 }
8258
8259 break;
8260 }
6fc2811b
JR
8261 }
8262
8263 return valid_p;
8264}
8265
8266
8267/* Log error message with format string FORMAT and argument ARG.
8268 Signaling an error, e.g. when an image cannot be loaded, is not a
8269 good idea because this would interrupt redisplay, and the error
8270 message display would lead to another redisplay. This function
8271 therefore simply displays a message. */
8272
8273static void
8274image_error (format, arg1, arg2)
8275 char *format;
8276 Lisp_Object arg1, arg2;
8277{
8278 add_to_log (format, arg1, arg2);
8279}
8280
8281
8282\f
8283/***********************************************************************
8284 Image specifications
8285 ***********************************************************************/
8286
8287enum image_value_type
8288{
8289 IMAGE_DONT_CHECK_VALUE_TYPE,
8290 IMAGE_STRING_VALUE,
3cf3436e 8291 IMAGE_STRING_OR_NIL_VALUE,
6fc2811b
JR
8292 IMAGE_SYMBOL_VALUE,
8293 IMAGE_POSITIVE_INTEGER_VALUE,
8edb0a6f 8294 IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR,
6fc2811b 8295 IMAGE_NON_NEGATIVE_INTEGER_VALUE,
dfff8a69 8296 IMAGE_ASCENT_VALUE,
6fc2811b
JR
8297 IMAGE_INTEGER_VALUE,
8298 IMAGE_FUNCTION_VALUE,
8299 IMAGE_NUMBER_VALUE,
8300 IMAGE_BOOL_VALUE
8301};
8302
8303/* Structure used when parsing image specifications. */
8304
8305struct image_keyword
8306{
8307 /* Name of keyword. */
8308 char *name;
8309
8310 /* The type of value allowed. */
8311 enum image_value_type type;
8312
8313 /* Non-zero means key must be present. */
8314 int mandatory_p;
8315
8316 /* Used to recognize duplicate keywords in a property list. */
8317 int count;
8318
8319 /* The value that was found. */
8320 Lisp_Object value;
8321};
8322
8323
8324static int parse_image_spec P_ ((Lisp_Object, struct image_keyword *,
8325 int, Lisp_Object));
8326static Lisp_Object image_spec_value P_ ((Lisp_Object, Lisp_Object, int *));
8327
8328
8329/* Parse image spec SPEC according to KEYWORDS. A valid image spec
8330 has the format (image KEYWORD VALUE ...). One of the keyword/
8331 value pairs must be `:type TYPE'. KEYWORDS is a vector of
8332 image_keywords structures of size NKEYWORDS describing other
8333 allowed keyword/value pairs. Value is non-zero if SPEC is valid. */
8334
8335static int
8336parse_image_spec (spec, keywords, nkeywords, type)
8337 Lisp_Object spec;
8338 struct image_keyword *keywords;
8339 int nkeywords;
8340 Lisp_Object type;
8341{
8342 int i;
8343 Lisp_Object plist;
8344
8345 if (!CONSP (spec) || !EQ (XCAR (spec), Qimage))
8346 return 0;
8347
8348 plist = XCDR (spec);
8349 while (CONSP (plist))
8350 {
8351 Lisp_Object key, value;
8352
8353 /* First element of a pair must be a symbol. */
8354 key = XCAR (plist);
8355 plist = XCDR (plist);
8356 if (!SYMBOLP (key))
8357 return 0;
8358
8359 /* There must follow a value. */
8360 if (!CONSP (plist))
8361 return 0;
8362 value = XCAR (plist);
8363 plist = XCDR (plist);
8364
8365 /* Find key in KEYWORDS. Error if not found. */
8366 for (i = 0; i < nkeywords; ++i)
8367 if (strcmp (keywords[i].name, XSYMBOL (key)->name->data) == 0)
8368 break;
8369
8370 if (i == nkeywords)
8371 continue;
8372
8373 /* Record that we recognized the keyword. If a keywords
8374 was found more than once, it's an error. */
8375 keywords[i].value = value;
8376 ++keywords[i].count;
8377
8378 if (keywords[i].count > 1)
8379 return 0;
8380
8381 /* Check type of value against allowed type. */
8382 switch (keywords[i].type)
8383 {
8384 case IMAGE_STRING_VALUE:
8385 if (!STRINGP (value))
8386 return 0;
8387 break;
8388
3cf3436e
JR
8389 case IMAGE_STRING_OR_NIL_VALUE:
8390 if (!STRINGP (value) && !NILP (value))
8391 return 0;
8392 break;
8393
6fc2811b
JR
8394 case IMAGE_SYMBOL_VALUE:
8395 if (!SYMBOLP (value))
8396 return 0;
8397 break;
8398
8399 case IMAGE_POSITIVE_INTEGER_VALUE:
8400 if (!INTEGERP (value) || XINT (value) <= 0)
8401 return 0;
8402 break;
8403
8edb0a6f
JR
8404 case IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR:
8405 if (INTEGERP (value) && XINT (value) >= 0)
8406 break;
8407 if (CONSP (value)
8408 && INTEGERP (XCAR (value)) && INTEGERP (XCDR (value))
8409 && XINT (XCAR (value)) >= 0 && XINT (XCDR (value)) >= 0)
8410 break;
8411 return 0;
8412
dfff8a69
JR
8413 case IMAGE_ASCENT_VALUE:
8414 if (SYMBOLP (value) && EQ (value, Qcenter))
8415 break;
8416 else if (INTEGERP (value)
8417 && XINT (value) >= 0
8418 && XINT (value) <= 100)
8419 break;
8420 return 0;
8421
6fc2811b
JR
8422 case IMAGE_NON_NEGATIVE_INTEGER_VALUE:
8423 if (!INTEGERP (value) || XINT (value) < 0)
8424 return 0;
8425 break;
8426
8427 case IMAGE_DONT_CHECK_VALUE_TYPE:
8428 break;
8429
8430 case IMAGE_FUNCTION_VALUE:
8431 value = indirect_function (value);
8432 if (SUBRP (value)
8433 || COMPILEDP (value)
8434 || (CONSP (value) && EQ (XCAR (value), Qlambda)))
8435 break;
8436 return 0;
8437
8438 case IMAGE_NUMBER_VALUE:
8439 if (!INTEGERP (value) && !FLOATP (value))
8440 return 0;
8441 break;
8442
8443 case IMAGE_INTEGER_VALUE:
8444 if (!INTEGERP (value))
8445 return 0;
8446 break;
8447
8448 case IMAGE_BOOL_VALUE:
8449 if (!NILP (value) && !EQ (value, Qt))
8450 return 0;
8451 break;
8452
8453 default:
8454 abort ();
8455 break;
8456 }
8457
8458 if (EQ (key, QCtype) && !EQ (type, value))
8459 return 0;
8460 }
8461
8462 /* Check that all mandatory fields are present. */
8463 for (i = 0; i < nkeywords; ++i)
8464 if (keywords[i].mandatory_p && keywords[i].count == 0)
8465 return 0;
8466
8467 return NILP (plist);
8468}
8469
8470
8471/* Return the value of KEY in image specification SPEC. Value is nil
8472 if KEY is not present in SPEC. if FOUND is not null, set *FOUND
8473 to 1 if KEY was found in SPEC, set it to 0 otherwise. */
8474
8475static Lisp_Object
8476image_spec_value (spec, key, found)
8477 Lisp_Object spec, key;
8478 int *found;
8479{
8480 Lisp_Object tail;
8481
8482 xassert (valid_image_p (spec));
8483
8484 for (tail = XCDR (spec);
8485 CONSP (tail) && CONSP (XCDR (tail));
8486 tail = XCDR (XCDR (tail)))
8487 {
8488 if (EQ (XCAR (tail), key))
8489 {
8490 if (found)
8491 *found = 1;
8492 return XCAR (XCDR (tail));
8493 }
8494 }
8495
8496 if (found)
8497 *found = 0;
8498 return Qnil;
8499}
8500
8501
ac849ba4
JR
8502#ifdef HAVE_IMAGES
8503DEFUN ("image-size", Fimage_size, Simage_size, 1, 3, 0,
8504 doc: /* Return the size of image SPEC as pair (WIDTH . HEIGHT).
8505PIXELS non-nil means return the size in pixels, otherwise return the
8506size in canonical character units.
8507FRAME is the frame on which the image will be displayed. FRAME nil
8508or omitted means use the selected frame. */)
8509 (spec, pixels, frame)
8510 Lisp_Object spec, pixels, frame;
8511{
8512 Lisp_Object size;
8513
8514 size = Qnil;
8515 if (valid_image_p (spec))
8516 {
8517 struct frame *f = check_x_frame (frame);
8518 int id = lookup_image (f, spec);
8519 struct image *img = IMAGE_FROM_ID (f, id);
8520 int width = img->width + 2 * img->hmargin;
8521 int height = img->height + 2 * img->vmargin;
8522
8523 if (NILP (pixels))
8524 size = Fcons (make_float ((double) width / CANON_X_UNIT (f)),
8525 make_float ((double) height / CANON_Y_UNIT (f)));
8526 else
8527 size = Fcons (make_number (width), make_number (height));
8528 }
8529 else
8530 error ("Invalid image specification");
8531
8532 return size;
8533}
8534
8535
8536DEFUN ("image-mask-p", Fimage_mask_p, Simage_mask_p, 1, 2, 0,
8537 doc: /* Return t if image SPEC has a mask bitmap.
8538FRAME is the frame on which the image will be displayed. FRAME nil
8539or omitted means use the selected frame. */)
8540 (spec, frame)
8541 Lisp_Object spec, frame;
8542{
8543 Lisp_Object mask;
8544
8545 mask = Qnil;
8546 if (valid_image_p (spec))
8547 {
8548 struct frame *f = check_x_frame (frame);
8549 int id = lookup_image (f, spec);
8550 struct image *img = IMAGE_FROM_ID (f, id);
8551 if (img->mask)
8552 mask = Qt;
8553 }
8554 else
8555 error ("Invalid image specification");
8556
8557 return mask;
8558}
8559#endif
6fc2811b
JR
8560
8561\f
8562/***********************************************************************
8563 Image type independent image structures
8564 ***********************************************************************/
8565
8566static struct image *make_image P_ ((Lisp_Object spec, unsigned hash));
8567static void free_image P_ ((struct frame *f, struct image *img));
8568
8569
8570/* Allocate and return a new image structure for image specification
8571 SPEC. SPEC has a hash value of HASH. */
8572
8573static struct image *
8574make_image (spec, hash)
8575 Lisp_Object spec;
8576 unsigned hash;
8577{
8578 struct image *img = (struct image *) xmalloc (sizeof *img);
8579
8580 xassert (valid_image_p (spec));
8581 bzero (img, sizeof *img);
8582 img->type = lookup_image_type (image_spec_value (spec, QCtype, NULL));
8583 xassert (img->type != NULL);
8584 img->spec = spec;
8585 img->data.lisp_val = Qnil;
8586 img->ascent = DEFAULT_IMAGE_ASCENT;
8587 img->hash = hash;
8588 return img;
8589}
8590
8591
8592/* Free image IMG which was used on frame F, including its resources. */
8593
8594static void
8595free_image (f, img)
8596 struct frame *f;
8597 struct image *img;
8598{
8599 if (img)
8600 {
8601 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
8602
8603 /* Remove IMG from the hash table of its cache. */
8604 if (img->prev)
8605 img->prev->next = img->next;
8606 else
8607 c->buckets[img->hash % IMAGE_CACHE_BUCKETS_SIZE] = img->next;
8608
8609 if (img->next)
8610 img->next->prev = img->prev;
8611
8612 c->images[img->id] = NULL;
8613
8614 /* Free resources, then free IMG. */
8615 img->type->free (f, img);
8616 xfree (img);
8617 }
8618}
8619
8620
8621/* Prepare image IMG for display on frame F. Must be called before
8622 drawing an image. */
8623
8624void
8625prepare_image_for_display (f, img)
8626 struct frame *f;
8627 struct image *img;
8628{
8629 EMACS_TIME t;
8630
8631 /* We're about to display IMG, so set its timestamp to `now'. */
8632 EMACS_GET_TIME (t);
8633 img->timestamp = EMACS_SECS (t);
8634
8635 /* If IMG doesn't have a pixmap yet, load it now, using the image
8636 type dependent loader function. */
8637 if (img->pixmap == 0 && !img->load_failed_p)
8638 img->load_failed_p = img->type->load (f, img) == 0;
8639}
8640
8641
dfff8a69
JR
8642/* Value is the number of pixels for the ascent of image IMG when
8643 drawn in face FACE. */
8644
8645int
8646image_ascent (img, face)
8647 struct image *img;
8648 struct face *face;
8649{
8edb0a6f 8650 int height = img->height + img->vmargin;
dfff8a69
JR
8651 int ascent;
8652
8653 if (img->ascent == CENTERED_IMAGE_ASCENT)
8654 {
8655 if (face->font)
8656 ascent = height / 2 - (FONT_DESCENT(face->font)
8657 - FONT_BASE(face->font)) / 2;
8658 else
8659 ascent = height / 2;
8660 }
8661 else
ac849ba4 8662 ascent = (int) (height * img->ascent / 100.0);
dfff8a69
JR
8663
8664 return ascent;
8665}
8666
8667
6fc2811b 8668\f
a05e2bae
JR
8669/* Image background colors. */
8670
ac849ba4
JR
8671/* Find the "best" corner color of a bitmap. XIMG is assumed to a device
8672 context with the bitmap selected. */
8673static COLORREF
a05e2bae 8674four_corners_best (ximg, width, height)
ac849ba4 8675 HDC ximg;
a05e2bae
JR
8676 unsigned long width, height;
8677{
ac849ba4 8678 COLORREF corners[4], best;
a05e2bae
JR
8679 int i, best_count;
8680
8681 /* Get the colors at the corners of ximg. */
ac849ba4
JR
8682 corners[0] = GetPixel (ximg, 0, 0);
8683 corners[1] = GetPixel (ximg, width - 1, 0);
8684 corners[2] = GetPixel (ximg, width - 1, height - 1);
8685 corners[3] = GetPixel (ximg, 0, height - 1);
a05e2bae
JR
8686
8687 /* Choose the most frequently found color as background. */
8688 for (i = best_count = 0; i < 4; ++i)
8689 {
8690 int j, n;
8691
8692 for (j = n = 0; j < 4; ++j)
8693 if (corners[i] == corners[j])
8694 ++n;
8695
8696 if (n > best_count)
8697 best = corners[i], best_count = n;
8698 }
8699
8700 return best;
a05e2bae
JR
8701}
8702
8703/* Return the `background' field of IMG. If IMG doesn't have one yet,
8704 it is guessed heuristically. If non-zero, XIMG is an existing XImage
8705 object to use for the heuristic. */
8706
8707unsigned long
8708image_background (img, f, ximg)
8709 struct image *img;
8710 struct frame *f;
8711 XImage *ximg;
8712{
8713 if (! img->background_valid)
8714 /* IMG doesn't have a background yet, try to guess a reasonable value. */
8715 {
8716#if 0 /* TODO: Image support. */
8717 int free_ximg = !ximg;
8718
8719 if (! ximg)
8720 ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap,
8721 0, 0, img->width, img->height, ~0, ZPixmap);
8722
8723 img->background = four_corners_best (ximg, img->width, img->height);
8724
8725 if (free_ximg)
8726 XDestroyImage (ximg);
8727
8728 img->background_valid = 1;
8729#endif
8730 }
8731
8732 return img->background;
8733}
8734
8735/* Return the `background_transparent' field of IMG. If IMG doesn't
8736 have one yet, it is guessed heuristically. If non-zero, MASK is an
8737 existing XImage object to use for the heuristic. */
8738
8739int
8740image_background_transparent (img, f, mask)
8741 struct image *img;
8742 struct frame *f;
8743 XImage *mask;
8744{
8745 if (! img->background_transparent_valid)
8746 /* IMG doesn't have a background yet, try to guess a reasonable value. */
8747 {
8748#if 0 /* TODO: Image support. */
8749 if (img->mask)
8750 {
8751 int free_mask = !mask;
8752
8753 if (! mask)
8754 mask = XGetImage (FRAME_X_DISPLAY (f), img->mask,
8755 0, 0, img->width, img->height, ~0, ZPixmap);
8756
8757 img->background_transparent
8758 = !four_corners_best (mask, img->width, img->height);
8759
8760 if (free_mask)
8761 XDestroyImage (mask);
8762 }
8763 else
8764#endif
8765 img->background_transparent = 0;
8766
8767 img->background_transparent_valid = 1;
8768 }
8769
8770 return img->background_transparent;
8771}
8772
8773\f
6fc2811b
JR
8774/***********************************************************************
8775 Helper functions for X image types
8776 ***********************************************************************/
8777
a05e2bae
JR
8778static void x_clear_image_1 P_ ((struct frame *, struct image *, int,
8779 int, int));
6fc2811b
JR
8780static void x_clear_image P_ ((struct frame *f, struct image *img));
8781static unsigned long x_alloc_image_color P_ ((struct frame *f,
8782 struct image *img,
8783 Lisp_Object color_name,
8784 unsigned long dflt));
8785
a05e2bae
JR
8786
8787/* Clear X resources of image IMG on frame F. PIXMAP_P non-zero means
8788 free the pixmap if any. MASK_P non-zero means clear the mask
8789 pixmap if any. COLORS_P non-zero means free colors allocated for
8790 the image, if any. */
8791
8792static void
8793x_clear_image_1 (f, img, pixmap_p, mask_p, colors_p)
8794 struct frame *f;
8795 struct image *img;
8796 int pixmap_p, mask_p, colors_p;
8797{
a05e2bae
JR
8798 if (pixmap_p && img->pixmap)
8799 {
ac849ba4
JR
8800 DeleteObject (img->pixmap);
8801 img->pixmap = NULL;
a05e2bae
JR
8802 img->background_valid = 0;
8803 }
8804
8805 if (mask_p && img->mask)
8806 {
ac849ba4
JR
8807 DeleteObject (img->mask);
8808 img->mask = NULL;
a05e2bae
JR
8809 img->background_transparent_valid = 0;
8810 }
8811
8812 if (colors_p && img->ncolors)
8813 {
8814 x_free_colors (f, img->colors, img->ncolors);
8815 xfree (img->colors);
8816 img->colors = NULL;
8817 img->ncolors = 0;
8818 }
a05e2bae
JR
8819}
8820
6fc2811b
JR
8821/* Free X resources of image IMG which is used on frame F. */
8822
8823static void
8824x_clear_image (f, img)
8825 struct frame *f;
8826 struct image *img;
8827{
6fc2811b
JR
8828 if (img->pixmap)
8829 {
8830 BLOCK_INPUT;
ac849ba4 8831 DeleteObject (img->pixmap);
6fc2811b
JR
8832 img->pixmap = 0;
8833 UNBLOCK_INPUT;
8834 }
8835
8836 if (img->ncolors)
8837 {
ac849ba4
JR
8838#if 0 /* TODO: color table support */
8839
6fc2811b
JR
8840 int class = FRAME_W32_DISPLAY_INFO (f)->visual->class;
8841
8842 /* If display has an immutable color map, freeing colors is not
8843 necessary and some servers don't allow it. So don't do it. */
8844 if (class != StaticColor
8845 && class != StaticGray
8846 && class != TrueColor)
8847 {
8848 Colormap cmap;
8849 BLOCK_INPUT;
8850 cmap = DefaultColormapOfScreen (FRAME_W32_DISPLAY_INFO (f)->screen);
8851 XFreeColors (FRAME_W32_DISPLAY (f), cmap, img->colors,
8852 img->ncolors, 0);
8853 UNBLOCK_INPUT;
8854 }
ac849ba4 8855#endif
6fc2811b
JR
8856
8857 xfree (img->colors);
8858 img->colors = NULL;
8859 img->ncolors = 0;
8860 }
6fc2811b
JR
8861}
8862
8863
8864/* Allocate color COLOR_NAME for image IMG on frame F. If color
8865 cannot be allocated, use DFLT. Add a newly allocated color to
8866 IMG->colors, so that it can be freed again. Value is the pixel
8867 color. */
8868
8869static unsigned long
8870x_alloc_image_color (f, img, color_name, dflt)
8871 struct frame *f;
8872 struct image *img;
8873 Lisp_Object color_name;
8874 unsigned long dflt;
8875{
6fc2811b
JR
8876 XColor color;
8877 unsigned long result;
8878
8879 xassert (STRINGP (color_name));
8880
8881 if (w32_defined_color (f, XSTRING (color_name)->data, &color, 1))
8882 {
8883 /* This isn't called frequently so we get away with simply
8884 reallocating the color vector to the needed size, here. */
8885 ++img->ncolors;
8886 img->colors =
8887 (unsigned long *) xrealloc (img->colors,
8888 img->ncolors * sizeof *img->colors);
8889 img->colors[img->ncolors - 1] = color.pixel;
8890 result = color.pixel;
8891 }
8892 else
8893 result = dflt;
8894 return result;
6fc2811b
JR
8895}
8896
8897
8898\f
8899/***********************************************************************
8900 Image Cache
8901 ***********************************************************************/
8902
8903static void cache_image P_ ((struct frame *f, struct image *img));
3cf3436e 8904static void postprocess_image P_ ((struct frame *, struct image *));
6fc2811b
JR
8905
8906
8907/* Return a new, initialized image cache that is allocated from the
8908 heap. Call free_image_cache to free an image cache. */
8909
8910struct image_cache *
8911make_image_cache ()
8912{
8913 struct image_cache *c = (struct image_cache *) xmalloc (sizeof *c);
8914 int size;
8915
8916 bzero (c, sizeof *c);
8917 c->size = 50;
8918 c->images = (struct image **) xmalloc (c->size * sizeof *c->images);
8919 size = IMAGE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
8920 c->buckets = (struct image **) xmalloc (size);
8921 bzero (c->buckets, size);
8922 return c;
8923}
8924
8925
8926/* Free image cache of frame F. Be aware that X frames share images
8927 caches. */
8928
8929void
8930free_image_cache (f)
8931 struct frame *f;
8932{
8933 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
8934 if (c)
8935 {
8936 int i;
8937
8938 /* Cache should not be referenced by any frame when freed. */
8939 xassert (c->refcount == 0);
8940
8941 for (i = 0; i < c->used; ++i)
8942 free_image (f, c->images[i]);
8943 xfree (c->images);
8944 xfree (c);
8945 xfree (c->buckets);
8946 FRAME_X_IMAGE_CACHE (f) = NULL;
8947 }
8948}
8949
8950
8951/* Clear image cache of frame F. FORCE_P non-zero means free all
8952 images. FORCE_P zero means clear only images that haven't been
8953 displayed for some time. Should be called from time to time to
dfff8a69
JR
8954 reduce the number of loaded images. If image-eviction-seconds is
8955 non-nil, this frees images in the cache which weren't displayed for
6fc2811b
JR
8956 at least that many seconds. */
8957
8958void
8959clear_image_cache (f, force_p)
8960 struct frame *f;
8961 int force_p;
8962{
8963 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
8964
8965 if (c && INTEGERP (Vimage_cache_eviction_delay))
8966 {
8967 EMACS_TIME t;
8968 unsigned long old;
8969 int i, any_freed_p = 0;
8970
8971 EMACS_GET_TIME (t);
8972 old = EMACS_SECS (t) - XFASTINT (Vimage_cache_eviction_delay);
8973
8974 for (i = 0; i < c->used; ++i)
8975 {
8976 struct image *img = c->images[i];
8977 if (img != NULL
8978 && (force_p
8979 || (img->timestamp > old)))
8980 {
8981 free_image (f, img);
8982 any_freed_p = 1;
8983 }
8984 }
8985
8986 /* We may be clearing the image cache because, for example,
8987 Emacs was iconified for a longer period of time. In that
8988 case, current matrices may still contain references to
8989 images freed above. So, clear these matrices. */
8990 if (any_freed_p)
8991 {
8992 clear_current_matrices (f);
8993 ++windows_or_buffers_changed;
8994 }
8995 }
8996}
8997
8998
8999DEFUN ("clear-image-cache", Fclear_image_cache, Sclear_image_cache,
9000 0, 1, 0,
74e1aeec
JR
9001 doc: /* Clear the image cache of FRAME.
9002FRAME nil or omitted means use the selected frame.
9003FRAME t means clear the image caches of all frames. */)
6fc2811b
JR
9004 (frame)
9005 Lisp_Object frame;
9006{
9007 if (EQ (frame, Qt))
9008 {
9009 Lisp_Object tail;
9010
9011 FOR_EACH_FRAME (tail, frame)
9012 if (FRAME_W32_P (XFRAME (frame)))
9013 clear_image_cache (XFRAME (frame), 1);
9014 }
9015 else
9016 clear_image_cache (check_x_frame (frame), 1);
9017
9018 return Qnil;
9019}
9020
9021
3cf3436e
JR
9022/* Compute masks and transform image IMG on frame F, as specified
9023 by the image's specification, */
9024
9025static void
9026postprocess_image (f, img)
9027 struct frame *f;
9028 struct image *img;
9029{
9030#if 0 /* TODO: image support. */
9031 /* Manipulation of the image's mask. */
9032 if (img->pixmap)
9033 {
9034 Lisp_Object conversion, spec;
9035 Lisp_Object mask;
9036
9037 spec = img->spec;
9038
9039 /* `:heuristic-mask t'
9040 `:mask heuristic'
9041 means build a mask heuristically.
9042 `:heuristic-mask (R G B)'
9043 `:mask (heuristic (R G B))'
9044 means build a mask from color (R G B) in the
9045 image.
9046 `:mask nil'
9047 means remove a mask, if any. */
9048
9049 mask = image_spec_value (spec, QCheuristic_mask, NULL);
9050 if (!NILP (mask))
9051 x_build_heuristic_mask (f, img, mask);
9052 else
9053 {
9054 int found_p;
9055
9056 mask = image_spec_value (spec, QCmask, &found_p);
9057
9058 if (EQ (mask, Qheuristic))
9059 x_build_heuristic_mask (f, img, Qt);
9060 else if (CONSP (mask)
9061 && EQ (XCAR (mask), Qheuristic))
9062 {
9063 if (CONSP (XCDR (mask)))
9064 x_build_heuristic_mask (f, img, XCAR (XCDR (mask)));
9065 else
9066 x_build_heuristic_mask (f, img, XCDR (mask));
9067 }
9068 else if (NILP (mask) && found_p && img->mask)
9069 {
ac849ba4 9070 DeleteObject (img->mask);
3cf3436e
JR
9071 img->mask = NULL;
9072 }
9073 }
9074
9075
9076 /* Should we apply an image transformation algorithm? */
9077 conversion = image_spec_value (spec, QCconversion, NULL);
9078 if (EQ (conversion, Qdisabled))
9079 x_disable_image (f, img);
9080 else if (EQ (conversion, Qlaplace))
9081 x_laplace (f, img);
9082 else if (EQ (conversion, Qemboss))
9083 x_emboss (f, img);
9084 else if (CONSP (conversion)
9085 && EQ (XCAR (conversion), Qedge_detection))
9086 {
9087 Lisp_Object tem;
9088 tem = XCDR (conversion);
9089 if (CONSP (tem))
9090 x_edge_detection (f, img,
9091 Fplist_get (tem, QCmatrix),
9092 Fplist_get (tem, QCcolor_adjustment));
9093 }
9094 }
9095#endif
9096}
9097
9098
6fc2811b
JR
9099/* Return the id of image with Lisp specification SPEC on frame F.
9100 SPEC must be a valid Lisp image specification (see valid_image_p). */
9101
9102int
9103lookup_image (f, spec)
9104 struct frame *f;
9105 Lisp_Object spec;
9106{
9107 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
9108 struct image *img;
9109 int i;
9110 unsigned hash;
9111 struct gcpro gcpro1;
9112 EMACS_TIME now;
9113
9114 /* F must be a window-system frame, and SPEC must be a valid image
9115 specification. */
9116 xassert (FRAME_WINDOW_P (f));
9117 xassert (valid_image_p (spec));
9118
9119 GCPRO1 (spec);
9120
9121 /* Look up SPEC in the hash table of the image cache. */
9122 hash = sxhash (spec, 0);
9123 i = hash % IMAGE_CACHE_BUCKETS_SIZE;
9124
9125 for (img = c->buckets[i]; img; img = img->next)
9126 if (img->hash == hash && !NILP (Fequal (img->spec, spec)))
9127 break;
9128
9129 /* If not found, create a new image and cache it. */
9130 if (img == NULL)
9131 {
3cf3436e
JR
9132 extern Lisp_Object Qpostscript;
9133
8edb0a6f 9134 BLOCK_INPUT;
6fc2811b
JR
9135 img = make_image (spec, hash);
9136 cache_image (f, img);
9137 img->load_failed_p = img->type->load (f, img) == 0;
6fc2811b
JR
9138
9139 /* If we can't load the image, and we don't have a width and
9140 height, use some arbitrary width and height so that we can
9141 draw a rectangle for it. */
9142 if (img->load_failed_p)
9143 {
9144 Lisp_Object value;
9145
9146 value = image_spec_value (spec, QCwidth, NULL);
9147 img->width = (INTEGERP (value)
9148 ? XFASTINT (value) : DEFAULT_IMAGE_WIDTH);
9149 value = image_spec_value (spec, QCheight, NULL);
9150 img->height = (INTEGERP (value)
9151 ? XFASTINT (value) : DEFAULT_IMAGE_HEIGHT);
9152 }
9153 else
9154 {
9155 /* Handle image type independent image attributes
a05e2bae
JR
9156 `:ascent PERCENT', `:margin MARGIN', `:relief RELIEF',
9157 `:background COLOR'. */
9158 Lisp_Object ascent, margin, relief, bg;
6fc2811b
JR
9159
9160 ascent = image_spec_value (spec, QCascent, NULL);
9161 if (INTEGERP (ascent))
9162 img->ascent = XFASTINT (ascent);
dfff8a69
JR
9163 else if (EQ (ascent, Qcenter))
9164 img->ascent = CENTERED_IMAGE_ASCENT;
9165
6fc2811b
JR
9166 margin = image_spec_value (spec, QCmargin, NULL);
9167 if (INTEGERP (margin) && XINT (margin) >= 0)
8edb0a6f
JR
9168 img->vmargin = img->hmargin = XFASTINT (margin);
9169 else if (CONSP (margin) && INTEGERP (XCAR (margin))
9170 && INTEGERP (XCDR (margin)))
9171 {
9172 if (XINT (XCAR (margin)) > 0)
9173 img->hmargin = XFASTINT (XCAR (margin));
9174 if (XINT (XCDR (margin)) > 0)
9175 img->vmargin = XFASTINT (XCDR (margin));
9176 }
6fc2811b
JR
9177
9178 relief = image_spec_value (spec, QCrelief, NULL);
9179 if (INTEGERP (relief))
9180 {
9181 img->relief = XINT (relief);
8edb0a6f
JR
9182 img->hmargin += abs (img->relief);
9183 img->vmargin += abs (img->relief);
6fc2811b
JR
9184 }
9185
a05e2bae
JR
9186 if (! img->background_valid)
9187 {
9188 bg = image_spec_value (img->spec, QCbackground, NULL);
9189 if (!NILP (bg))
9190 {
9191 img->background
9192 = x_alloc_image_color (f, img, bg,
9193 FRAME_BACKGROUND_PIXEL (f));
9194 img->background_valid = 1;
9195 }
9196 }
9197
3cf3436e
JR
9198 /* Do image transformations and compute masks, unless we
9199 don't have the image yet. */
9200 if (!EQ (*img->type->type, Qpostscript))
9201 postprocess_image (f, img);
6fc2811b 9202 }
3cf3436e 9203
8edb0a6f
JR
9204 UNBLOCK_INPUT;
9205 xassert (!interrupt_input_blocked);
6fc2811b
JR
9206 }
9207
9208 /* We're using IMG, so set its timestamp to `now'. */
9209 EMACS_GET_TIME (now);
9210 img->timestamp = EMACS_SECS (now);
9211
9212 UNGCPRO;
9213
9214 /* Value is the image id. */
9215 return img->id;
9216}
9217
9218
9219/* Cache image IMG in the image cache of frame F. */
9220
9221static void
9222cache_image (f, img)
9223 struct frame *f;
9224 struct image *img;
9225{
9226 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
9227 int i;
9228
9229 /* Find a free slot in c->images. */
9230 for (i = 0; i < c->used; ++i)
9231 if (c->images[i] == NULL)
9232 break;
9233
9234 /* If no free slot found, maybe enlarge c->images. */
9235 if (i == c->used && c->used == c->size)
9236 {
9237 c->size *= 2;
9238 c->images = (struct image **) xrealloc (c->images,
9239 c->size * sizeof *c->images);
9240 }
9241
9242 /* Add IMG to c->images, and assign IMG an id. */
9243 c->images[i] = img;
9244 img->id = i;
9245 if (i == c->used)
9246 ++c->used;
9247
9248 /* Add IMG to the cache's hash table. */
9249 i = img->hash % IMAGE_CACHE_BUCKETS_SIZE;
9250 img->next = c->buckets[i];
9251 if (img->next)
9252 img->next->prev = img;
9253 img->prev = NULL;
9254 c->buckets[i] = img;
9255}
9256
9257
9258/* Call FN on every image in the image cache of frame F. Used to mark
9259 Lisp Objects in the image cache. */
9260
9261void
9262forall_images_in_image_cache (f, fn)
9263 struct frame *f;
9264 void (*fn) P_ ((struct image *img));
9265{
9266 if (FRAME_LIVE_P (f) && FRAME_W32_P (f))
9267 {
9268 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
9269 if (c)
9270 {
9271 int i;
9272 for (i = 0; i < c->used; ++i)
9273 if (c->images[i])
9274 fn (c->images[i]);
9275 }
9276 }
9277}
9278
9279
9280\f
9281/***********************************************************************
9282 W32 support code
9283 ***********************************************************************/
9284
6fc2811b
JR
9285static int x_create_x_image_and_pixmap P_ ((struct frame *, int, int, int,
9286 XImage **, Pixmap *));
9287static void x_destroy_x_image P_ ((XImage *));
9288static void x_put_x_image P_ ((struct frame *, XImage *, Pixmap, int, int));
9289
9290
9291/* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on
9292 frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created.
9293 Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated
ac849ba4
JR
9294 via xmalloc. DEPTH of zero signifies a 24 bit image, otherwise
9295 DEPTH should indicate the bit depth of the image. Print error
9296 messages via image_error if an error occurs. Value is non-zero if
9297 successful. */
6fc2811b
JR
9298
9299static int
9300x_create_x_image_and_pixmap (f, width, height, depth, ximg, pixmap)
9301 struct frame *f;
9302 int width, height, depth;
9303 XImage **ximg;
9304 Pixmap *pixmap;
9305{
ac849ba4
JR
9306 BITMAPINFOHEADER *header;
9307 HDC hdc;
9308 int scanline_width_bits;
9309 int remainder;
9310 int palette_colors = 0;
6fc2811b 9311
ac849ba4
JR
9312 if (depth == 0)
9313 depth = 24;
6fc2811b 9314
ac849ba4
JR
9315 if (depth != 1 && depth != 4 && depth != 8
9316 && depth != 16 && depth != 24 && depth != 32)
9317 {
9318 image_error ("Invalid image bit depth specified", Qnil, Qnil);
9319 return 0;
9320 }
9321
9322 scanline_width_bits = width * depth;
9323 remainder = scanline_width_bits % 32;
9324
9325 if (remainder)
9326 scanline_width_bits += 32 - remainder;
9327
9328 /* Bitmaps with a depth less than 16 need a palette. */
9329 /* BITMAPINFO structure already contains the first RGBQUAD. */
9330 if (depth < 16)
9331 palette_colors = 1 << depth - 1;
9332
9333 *ximg = xmalloc (sizeof (XImage) + palette_colors * sizeof (RGBQUAD));
6fc2811b
JR
9334 if (*ximg == NULL)
9335 {
ac849ba4 9336 image_error ("Unable to allocate memory for XImage", Qnil, Qnil);
6fc2811b
JR
9337 return 0;
9338 }
9339
ac849ba4
JR
9340 header = &((*ximg)->info.bmiHeader);
9341 bzero (&((*ximg)->info), sizeof (BITMAPINFO));
9342 header->biSize = sizeof (*header);
9343 header->biWidth = width;
9344 header->biHeight = -height; /* negative indicates a top-down bitmap. */
9345 header->biPlanes = 1;
9346 header->biBitCount = depth;
9347 header->biCompression = BI_RGB;
9348 header->biClrUsed = palette_colors;
6fc2811b 9349
ac849ba4
JR
9350 hdc = get_frame_dc (f);
9351
9352 /* Create a DIBSection and raster array for the bitmap,
9353 and store its handle in *pixmap. */
9354 *pixmap = CreateDIBSection (hdc, &((*ximg)->info), DIB_RGB_COLORS,
9355 &((*ximg)->data), NULL, 0);
9356
9357 /* Realize display palette and garbage all frames. */
9358 release_frame_dc (f, hdc);
9359
9360 if (*pixmap == NULL)
6fc2811b 9361 {
ac849ba4
JR
9362 DWORD err = GetLastError();
9363 Lisp_Object errcode;
9364 /* All system errors are < 10000, so the following is safe. */
9365 XSETINT (errcode, (int) err);
9366 image_error ("Unable to create bitmap, error code %d", errcode, Qnil);
6fc2811b 9367 x_destroy_x_image (*ximg);
6fc2811b
JR
9368 return 0;
9369 }
ac849ba4 9370
6fc2811b
JR
9371 return 1;
9372}
9373
9374
9375/* Destroy XImage XIMG. Free XIMG->data. */
9376
9377static void
9378x_destroy_x_image (ximg)
9379 XImage *ximg;
9380{
9381 xassert (interrupt_input_blocked);
9382 if (ximg)
9383 {
ac849ba4 9384 /* Data will be freed by DestroyObject. */
6fc2811b 9385 ximg->data = NULL;
ac849ba4 9386 xfree (ximg);
6fc2811b
JR
9387 }
9388}
9389
9390
9391/* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT
9392 are width and height of both the image and pixmap. */
9393
9394static void
9395x_put_x_image (f, ximg, pixmap, width, height)
9396 struct frame *f;
9397 XImage *ximg;
9398 Pixmap pixmap;
9399{
ac849ba4
JR
9400
9401#if TODO /* W32 specific image code. */
6fc2811b 9402 GC gc;
ac849ba4 9403
6fc2811b
JR
9404 xassert (interrupt_input_blocked);
9405 gc = XCreateGC (NULL, pixmap, 0, NULL);
9406 XPutImage (NULL, pixmap, gc, ximg, 0, 0, 0, 0, width, height);
9407 XFreeGC (NULL, gc);
6fc2811b 9408#endif
ac849ba4 9409}
6fc2811b
JR
9410
9411\f
9412/***********************************************************************
3cf3436e 9413 File Handling
6fc2811b
JR
9414 ***********************************************************************/
9415
9416static Lisp_Object x_find_image_file P_ ((Lisp_Object));
3cf3436e
JR
9417static char *slurp_file P_ ((char *, int *));
9418
6fc2811b
JR
9419
9420/* Find image file FILE. Look in data-directory, then
9421 x-bitmap-file-path. Value is the full name of the file found, or
9422 nil if not found. */
9423
9424static Lisp_Object
9425x_find_image_file (file)
9426 Lisp_Object file;
9427{
9428 Lisp_Object file_found, search_path;
9429 struct gcpro gcpro1, gcpro2;
9430 int fd;
9431
9432 file_found = Qnil;
9433 search_path = Fcons (Vdata_directory, Vx_bitmap_file_path);
9434 GCPRO2 (file_found, search_path);
9435
9436 /* Try to find FILE in data-directory, then x-bitmap-file-path. */
c0ec53ad 9437 fd = openp (search_path, file, Qnil, &file_found, 0);
6fc2811b 9438
939d6465 9439 if (fd == -1)
6fc2811b
JR
9440 file_found = Qnil;
9441 else
9442 close (fd);
9443
9444 UNGCPRO;
9445 return file_found;
9446}
9447
9448
3cf3436e
JR
9449/* Read FILE into memory. Value is a pointer to a buffer allocated
9450 with xmalloc holding FILE's contents. Value is null if an error
9451 occurred. *SIZE is set to the size of the file. */
9452
9453static char *
9454slurp_file (file, size)
9455 char *file;
9456 int *size;
9457{
9458 FILE *fp = NULL;
9459 char *buf = NULL;
9460 struct stat st;
9461
9462 if (stat (file, &st) == 0
9463 && (fp = fopen (file, "r")) != NULL
9464 && (buf = (char *) xmalloc (st.st_size),
9465 fread (buf, 1, st.st_size, fp) == st.st_size))
9466 {
9467 *size = st.st_size;
9468 fclose (fp);
9469 }
9470 else
9471 {
9472 if (fp)
9473 fclose (fp);
9474 if (buf)
9475 {
9476 xfree (buf);
9477 buf = NULL;
9478 }
9479 }
9480
9481 return buf;
9482}
9483
9484
6fc2811b
JR
9485\f
9486/***********************************************************************
9487 XBM images
9488 ***********************************************************************/
9489
9490static int xbm_load P_ ((struct frame *f, struct image *img));
9491static int xbm_load_image_from_file P_ ((struct frame *f, struct image *img,
9492 Lisp_Object file));
9493static int xbm_image_p P_ ((Lisp_Object object));
9494static int xbm_read_bitmap_file_data P_ ((char *, int *, int *,
9495 unsigned char **));
9496
9497
9498/* Indices of image specification fields in xbm_format, below. */
9499
9500enum xbm_keyword_index
9501{
9502 XBM_TYPE,
9503 XBM_FILE,
9504 XBM_WIDTH,
9505 XBM_HEIGHT,
9506 XBM_DATA,
9507 XBM_FOREGROUND,
9508 XBM_BACKGROUND,
9509 XBM_ASCENT,
9510 XBM_MARGIN,
9511 XBM_RELIEF,
9512 XBM_ALGORITHM,
9513 XBM_HEURISTIC_MASK,
a05e2bae 9514 XBM_MASK,
6fc2811b
JR
9515 XBM_LAST
9516};
9517
9518/* Vector of image_keyword structures describing the format
9519 of valid XBM image specifications. */
9520
9521static struct image_keyword xbm_format[XBM_LAST] =
9522{
9523 {":type", IMAGE_SYMBOL_VALUE, 1},
9524 {":file", IMAGE_STRING_VALUE, 0},
9525 {":width", IMAGE_POSITIVE_INTEGER_VALUE, 0},
9526 {":height", IMAGE_POSITIVE_INTEGER_VALUE, 0},
9527 {":data", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
3cf3436e
JR
9528 {":foreground", IMAGE_STRING_OR_NIL_VALUE, 0},
9529 {":background", IMAGE_STRING_OR_NIL_VALUE, 0},
6fc2811b 9530 {":ascent", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
8edb0a6f 9531 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
6fc2811b 9532 {":relief", IMAGE_INTEGER_VALUE, 0},
a93f4566 9533 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6fc2811b
JR
9534 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
9535};
9536
9537/* Structure describing the image type XBM. */
9538
9539static struct image_type xbm_type =
9540{
9541 &Qxbm,
9542 xbm_image_p,
9543 xbm_load,
9544 x_clear_image,
9545 NULL
9546};
9547
9548/* Tokens returned from xbm_scan. */
9549
9550enum xbm_token
9551{
9552 XBM_TK_IDENT = 256,
9553 XBM_TK_NUMBER
9554};
9555
9556
9557/* Return non-zero if OBJECT is a valid XBM-type image specification.
9558 A valid specification is a list starting with the symbol `image'
9559 The rest of the list is a property list which must contain an
9560 entry `:type xbm..
9561
9562 If the specification specifies a file to load, it must contain
9563 an entry `:file FILENAME' where FILENAME is a string.
9564
9565 If the specification is for a bitmap loaded from memory it must
9566 contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where
9567 WIDTH and HEIGHT are integers > 0. DATA may be:
9568
9569 1. a string large enough to hold the bitmap data, i.e. it must
9570 have a size >= (WIDTH + 7) / 8 * HEIGHT
9571
9572 2. a bool-vector of size >= WIDTH * HEIGHT
9573
9574 3. a vector of strings or bool-vectors, one for each line of the
9575 bitmap.
9576
9577 Both the file and data forms may contain the additional entries
9578 `:background COLOR' and `:foreground COLOR'. If not present,
9579 foreground and background of the frame on which the image is
9580 displayed, is used. */
9581
9582static int
9583xbm_image_p (object)
9584 Lisp_Object object;
9585{
9586 struct image_keyword kw[XBM_LAST];
9587
9588 bcopy (xbm_format, kw, sizeof kw);
9589 if (!parse_image_spec (object, kw, XBM_LAST, Qxbm))
9590 return 0;
9591
9592 xassert (EQ (kw[XBM_TYPE].value, Qxbm));
9593
9594 if (kw[XBM_FILE].count)
9595 {
9596 if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_DATA].count)
9597 return 0;
9598 }
9599 else
9600 {
9601 Lisp_Object data;
9602 int width, height;
9603
9604 /* Entries for `:width', `:height' and `:data' must be present. */
9605 if (!kw[XBM_WIDTH].count
9606 || !kw[XBM_HEIGHT].count
9607 || !kw[XBM_DATA].count)
9608 return 0;
9609
9610 data = kw[XBM_DATA].value;
9611 width = XFASTINT (kw[XBM_WIDTH].value);
9612 height = XFASTINT (kw[XBM_HEIGHT].value);
9613
9614 /* Check type of data, and width and height against contents of
9615 data. */
9616 if (VECTORP (data))
9617 {
9618 int i;
9619
9620 /* Number of elements of the vector must be >= height. */
9621 if (XVECTOR (data)->size < height)
9622 return 0;
9623
9624 /* Each string or bool-vector in data must be large enough
9625 for one line of the image. */
9626 for (i = 0; i < height; ++i)
9627 {
9628 Lisp_Object elt = XVECTOR (data)->contents[i];
9629
9630 if (STRINGP (elt))
9631 {
9632 if (XSTRING (elt)->size
9633 < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR)
9634 return 0;
9635 }
9636 else if (BOOL_VECTOR_P (elt))
9637 {
9638 if (XBOOL_VECTOR (elt)->size < width)
9639 return 0;
9640 }
9641 else
9642 return 0;
9643 }
9644 }
9645 else if (STRINGP (data))
9646 {
9647 if (XSTRING (data)->size
9648 < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR * height)
9649 return 0;
9650 }
9651 else if (BOOL_VECTOR_P (data))
9652 {
9653 if (XBOOL_VECTOR (data)->size < width * height)
9654 return 0;
9655 }
9656 else
9657 return 0;
9658 }
9659
9660 /* Baseline must be a value between 0 and 100 (a percentage). */
9661 if (kw[XBM_ASCENT].count
9662 && XFASTINT (kw[XBM_ASCENT].value) > 100)
9663 return 0;
9664
9665 return 1;
9666}
9667
9668
9669/* Scan a bitmap file. FP is the stream to read from. Value is
9670 either an enumerator from enum xbm_token, or a character for a
9671 single-character token, or 0 at end of file. If scanning an
9672 identifier, store the lexeme of the identifier in SVAL. If
9673 scanning a number, store its value in *IVAL. */
9674
9675static int
3cf3436e
JR
9676xbm_scan (s, end, sval, ival)
9677 char **s, *end;
6fc2811b
JR
9678 char *sval;
9679 int *ival;
9680{
9681 int c;
3cf3436e
JR
9682
9683 loop:
9684
6fc2811b 9685 /* Skip white space. */
3cf3436e 9686 while (*s < end &&(c = *(*s)++, isspace (c)))
6fc2811b
JR
9687 ;
9688
3cf3436e 9689 if (*s >= end)
6fc2811b
JR
9690 c = 0;
9691 else if (isdigit (c))
9692 {
9693 int value = 0, digit;
9694
3cf3436e 9695 if (c == '0' && *s < end)
6fc2811b 9696 {
3cf3436e 9697 c = *(*s)++;
6fc2811b
JR
9698 if (c == 'x' || c == 'X')
9699 {
3cf3436e 9700 while (*s < end)
6fc2811b 9701 {
3cf3436e 9702 c = *(*s)++;
6fc2811b
JR
9703 if (isdigit (c))
9704 digit = c - '0';
9705 else if (c >= 'a' && c <= 'f')
9706 digit = c - 'a' + 10;
9707 else if (c >= 'A' && c <= 'F')
9708 digit = c - 'A' + 10;
9709 else
9710 break;
9711 value = 16 * value + digit;
9712 }
9713 }
9714 else if (isdigit (c))
9715 {
9716 value = c - '0';
3cf3436e
JR
9717 while (*s < end
9718 && (c = *(*s)++, isdigit (c)))
6fc2811b
JR
9719 value = 8 * value + c - '0';
9720 }
9721 }
9722 else
9723 {
9724 value = c - '0';
3cf3436e
JR
9725 while (*s < end
9726 && (c = *(*s)++, isdigit (c)))
6fc2811b
JR
9727 value = 10 * value + c - '0';
9728 }
9729
3cf3436e
JR
9730 if (*s < end)
9731 *s = *s - 1;
6fc2811b
JR
9732 *ival = value;
9733 c = XBM_TK_NUMBER;
9734 }
9735 else if (isalpha (c) || c == '_')
9736 {
9737 *sval++ = c;
3cf3436e
JR
9738 while (*s < end
9739 && (c = *(*s)++, (isalnum (c) || c == '_')))
6fc2811b
JR
9740 *sval++ = c;
9741 *sval = 0;
3cf3436e
JR
9742 if (*s < end)
9743 *s = *s - 1;
6fc2811b
JR
9744 c = XBM_TK_IDENT;
9745 }
3cf3436e
JR
9746 else if (c == '/' && **s == '*')
9747 {
9748 /* C-style comment. */
9749 ++*s;
9750 while (**s && (**s != '*' || *(*s + 1) != '/'))
9751 ++*s;
9752 if (**s)
9753 {
9754 *s += 2;
9755 goto loop;
9756 }
9757 }
6fc2811b
JR
9758
9759 return c;
9760}
9761
9762
9763/* Replacement for XReadBitmapFileData which isn't available under old
3cf3436e
JR
9764 X versions. CONTENTS is a pointer to a buffer to parse; END is the
9765 buffer's end. Set *WIDTH and *HEIGHT to the width and height of
9766 the image. Return in *DATA the bitmap data allocated with xmalloc.
9767 Value is non-zero if successful. DATA null means just test if
9768 CONTENTS looks like an in-memory XBM file. */
6fc2811b
JR
9769
9770static int
3cf3436e
JR
9771xbm_read_bitmap_data (contents, end, width, height, data)
9772 char *contents, *end;
6fc2811b
JR
9773 int *width, *height;
9774 unsigned char **data;
9775{
3cf3436e 9776 char *s = contents;
6fc2811b
JR
9777 char buffer[BUFSIZ];
9778 int padding_p = 0;
9779 int v10 = 0;
9780 int bytes_per_line, i, nbytes;
9781 unsigned char *p;
9782 int value;
9783 int LA1;
9784
9785#define match() \
3cf3436e 9786 LA1 = xbm_scan (contents, end, buffer, &value)
6fc2811b
JR
9787
9788#define expect(TOKEN) \
9789 if (LA1 != (TOKEN)) \
9790 goto failure; \
9791 else \
9792 match ()
9793
9794#define expect_ident(IDENT) \
9795 if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \
9796 match (); \
9797 else \
9798 goto failure
9799
6fc2811b 9800 *width = *height = -1;
3cf3436e
JR
9801 if (data)
9802 *data = NULL;
9803 LA1 = xbm_scan (&s, end, buffer, &value);
6fc2811b
JR
9804
9805 /* Parse defines for width, height and hot-spots. */
9806 while (LA1 == '#')
9807 {
9808 match ();
9809 expect_ident ("define");
9810 expect (XBM_TK_IDENT);
9811
9812 if (LA1 == XBM_TK_NUMBER);
9813 {
9814 char *p = strrchr (buffer, '_');
9815 p = p ? p + 1 : buffer;
9816 if (strcmp (p, "width") == 0)
9817 *width = value;
9818 else if (strcmp (p, "height") == 0)
9819 *height = value;
9820 }
9821 expect (XBM_TK_NUMBER);
9822 }
9823
9824 if (*width < 0 || *height < 0)
9825 goto failure;
3cf3436e
JR
9826 else if (data == NULL)
9827 goto success;
6fc2811b
JR
9828
9829 /* Parse bits. Must start with `static'. */
9830 expect_ident ("static");
9831 if (LA1 == XBM_TK_IDENT)
9832 {
9833 if (strcmp (buffer, "unsigned") == 0)
9834 {
9835 match ();
9836 expect_ident ("char");
9837 }
9838 else if (strcmp (buffer, "short") == 0)
9839 {
9840 match ();
9841 v10 = 1;
9842 if (*width % 16 && *width % 16 < 9)
9843 padding_p = 1;
9844 }
9845 else if (strcmp (buffer, "char") == 0)
9846 match ();
9847 else
9848 goto failure;
9849 }
9850 else
9851 goto failure;
9852
9853 expect (XBM_TK_IDENT);
9854 expect ('[');
9855 expect (']');
9856 expect ('=');
9857 expect ('{');
9858
9859 bytes_per_line = (*width + 7) / 8 + padding_p;
9860 nbytes = bytes_per_line * *height;
9861 p = *data = (char *) xmalloc (nbytes);
9862
9863 if (v10)
9864 {
9865
9866 for (i = 0; i < nbytes; i += 2)
9867 {
9868 int val = value;
9869 expect (XBM_TK_NUMBER);
9870
9871 *p++ = val;
9872 if (!padding_p || ((i + 2) % bytes_per_line))
9873 *p++ = value >> 8;
9874
9875 if (LA1 == ',' || LA1 == '}')
9876 match ();
9877 else
9878 goto failure;
9879 }
9880 }
9881 else
9882 {
9883 for (i = 0; i < nbytes; ++i)
9884 {
9885 int val = value;
9886 expect (XBM_TK_NUMBER);
9887
9888 *p++ = val;
9889
9890 if (LA1 == ',' || LA1 == '}')
9891 match ();
9892 else
9893 goto failure;
9894 }
9895 }
9896
3cf3436e 9897 success:
6fc2811b
JR
9898 return 1;
9899
9900 failure:
3cf3436e
JR
9901
9902 if (data && *data)
6fc2811b
JR
9903 {
9904 xfree (*data);
9905 *data = NULL;
9906 }
9907 return 0;
9908
9909#undef match
9910#undef expect
9911#undef expect_ident
9912}
9913
9914
3cf3436e
JR
9915/* Load XBM image IMG which will be displayed on frame F from buffer
9916 CONTENTS. END is the end of the buffer. Value is non-zero if
9917 successful. */
6fc2811b
JR
9918
9919static int
3cf3436e 9920xbm_load_image (f, img, contents, end)
6fc2811b
JR
9921 struct frame *f;
9922 struct image *img;
3cf3436e 9923 char *contents, *end;
6fc2811b
JR
9924{
9925 int rc;
9926 unsigned char *data;
9927 int success_p = 0;
6fc2811b 9928
3cf3436e 9929 rc = xbm_read_bitmap_data (contents, end, &img->width, &img->height, &data);
6fc2811b
JR
9930 if (rc)
9931 {
9932 int depth = one_w32_display_info.n_cbits;
ac849ba4
JR
9933 int planes = one_w32_display_info.n_planes;
9934
6fc2811b
JR
9935 unsigned long foreground = FRAME_FOREGROUND_PIXEL (f);
9936 unsigned long background = FRAME_BACKGROUND_PIXEL (f);
9937 Lisp_Object value;
9938
9939 xassert (img->width > 0 && img->height > 0);
9940
9941 /* Get foreground and background colors, maybe allocate colors. */
9942 value = image_spec_value (img->spec, QCforeground, NULL);
9943 if (!NILP (value))
9944 foreground = x_alloc_image_color (f, img, value, foreground);
6fc2811b
JR
9945 value = image_spec_value (img->spec, QCbackground, NULL);
9946 if (!NILP (value))
a05e2bae
JR
9947 {
9948 background = x_alloc_image_color (f, img, value, background);
9949 img->background = background;
9950 img->background_valid = 1;
9951 }
6fc2811b 9952 img->pixmap
ac849ba4
JR
9953 = CreateBitmap (img->width, img->height, planes, depth, data);
9954
6fc2811b
JR
9955 xfree (data);
9956
9957 if (img->pixmap == 0)
9958 {
9959 x_clear_image (f, img);
3cf3436e 9960 image_error ("Unable to create X pixmap for `%s'", img->spec, Qnil);
6fc2811b
JR
9961 }
9962 else
9963 success_p = 1;
6fc2811b
JR
9964 }
9965 else
9966 image_error ("Error loading XBM image `%s'", img->spec, Qnil);
9967
6fc2811b
JR
9968 return success_p;
9969}
9970
9971
3cf3436e
JR
9972/* Value is non-zero if DATA looks like an in-memory XBM file. */
9973
9974static int
9975xbm_file_p (data)
9976 Lisp_Object data;
9977{
9978 int w, h;
9979 return (STRINGP (data)
9980 && xbm_read_bitmap_data (XSTRING (data)->data,
9981 (XSTRING (data)->data
9982 + STRING_BYTES (XSTRING (data))),
9983 &w, &h, NULL));
9984}
9985
9986
6fc2811b
JR
9987/* Fill image IMG which is used on frame F with pixmap data. Value is
9988 non-zero if successful. */
9989
9990static int
9991xbm_load (f, img)
9992 struct frame *f;
9993 struct image *img;
9994{
9995 int success_p = 0;
9996 Lisp_Object file_name;
9997
9998 xassert (xbm_image_p (img->spec));
9999
10000 /* If IMG->spec specifies a file name, create a non-file spec from it. */
10001 file_name = image_spec_value (img->spec, QCfile, NULL);
10002 if (STRINGP (file_name))
3cf3436e
JR
10003 {
10004 Lisp_Object file;
10005 char *contents;
10006 int size;
10007 struct gcpro gcpro1;
10008
10009 file = x_find_image_file (file_name);
10010 GCPRO1 (file);
10011 if (!STRINGP (file))
10012 {
10013 image_error ("Cannot find image file `%s'", file_name, Qnil);
10014 UNGCPRO;
10015 return 0;
10016 }
10017
10018 contents = slurp_file (XSTRING (file)->data, &size);
10019 if (contents == NULL)
10020 {
10021 image_error ("Error loading XBM image `%s'", img->spec, Qnil);
10022 UNGCPRO;
10023 return 0;
10024 }
10025
10026 success_p = xbm_load_image (f, img, contents, contents + size);
10027 UNGCPRO;
10028 }
6fc2811b
JR
10029 else
10030 {
10031 struct image_keyword fmt[XBM_LAST];
10032 Lisp_Object data;
10033 int depth;
10034 unsigned long foreground = FRAME_FOREGROUND_PIXEL (f);
10035 unsigned long background = FRAME_BACKGROUND_PIXEL (f);
10036 char *bits;
10037 int parsed_p;
3cf3436e
JR
10038 int in_memory_file_p = 0;
10039
10040 /* See if data looks like an in-memory XBM file. */
10041 data = image_spec_value (img->spec, QCdata, NULL);
10042 in_memory_file_p = xbm_file_p (data);
6fc2811b
JR
10043
10044 /* Parse the list specification. */
10045 bcopy (xbm_format, fmt, sizeof fmt);
10046 parsed_p = parse_image_spec (img->spec, fmt, XBM_LAST, Qxbm);
10047 xassert (parsed_p);
10048
10049 /* Get specified width, and height. */
3cf3436e
JR
10050 if (!in_memory_file_p)
10051 {
10052 img->width = XFASTINT (fmt[XBM_WIDTH].value);
10053 img->height = XFASTINT (fmt[XBM_HEIGHT].value);
10054 xassert (img->width > 0 && img->height > 0);
10055 }
6fc2811b 10056 /* Get foreground and background colors, maybe allocate colors. */
3cf3436e
JR
10057 if (fmt[XBM_FOREGROUND].count
10058 && STRINGP (fmt[XBM_FOREGROUND].value))
6fc2811b
JR
10059 foreground = x_alloc_image_color (f, img, fmt[XBM_FOREGROUND].value,
10060 foreground);
3cf3436e
JR
10061 if (fmt[XBM_BACKGROUND].count
10062 && STRINGP (fmt[XBM_BACKGROUND].value))
6fc2811b
JR
10063 background = x_alloc_image_color (f, img, fmt[XBM_BACKGROUND].value,
10064 background);
10065
3cf3436e
JR
10066 if (in_memory_file_p)
10067 success_p = xbm_load_image (f, img, XSTRING (data)->data,
10068 (XSTRING (data)->data
10069 + STRING_BYTES (XSTRING (data))));
10070 else
6fc2811b 10071 {
3cf3436e
JR
10072 if (VECTORP (data))
10073 {
10074 int i;
10075 char *p;
10076 int nbytes = (img->width + BITS_PER_CHAR - 1) / BITS_PER_CHAR;
6fc2811b 10077
3cf3436e
JR
10078 p = bits = (char *) alloca (nbytes * img->height);
10079 for (i = 0; i < img->height; ++i, p += nbytes)
10080 {
10081 Lisp_Object line = XVECTOR (data)->contents[i];
10082 if (STRINGP (line))
10083 bcopy (XSTRING (line)->data, p, nbytes);
10084 else
10085 bcopy (XBOOL_VECTOR (line)->data, p, nbytes);
10086 }
10087 }
10088 else if (STRINGP (data))
10089 bits = XSTRING (data)->data;
10090 else
10091 bits = XBOOL_VECTOR (data)->data;
10092#ifdef TODO /* image support. */
10093 /* Create the pixmap. */
a05e2bae 10094 depth = one_w32_display_info.n_cbits;
3cf3436e
JR
10095 img->pixmap
10096 = XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f),
10097 FRAME_X_WINDOW (f),
10098 bits,
10099 img->width, img->height,
10100 foreground, background,
10101 depth);
10102#endif
10103 if (img->pixmap)
10104 success_p = 1;
10105 else
6fc2811b 10106 {
3cf3436e
JR
10107 image_error ("Unable to create pixmap for XBM image `%s'",
10108 img->spec, Qnil);
10109 x_clear_image (f, img);
6fc2811b
JR
10110 }
10111 }
6fc2811b
JR
10112 }
10113
10114 return success_p;
10115}
10116
10117
10118\f
10119/***********************************************************************
10120 XPM images
10121 ***********************************************************************/
10122
10123#if HAVE_XPM
10124
10125static int xpm_image_p P_ ((Lisp_Object object));
10126static int xpm_load P_ ((struct frame *f, struct image *img));
10127static int xpm_valid_color_symbols_p P_ ((Lisp_Object));
10128
10129#include "X11/xpm.h"
10130
10131/* The symbol `xpm' identifying XPM-format images. */
10132
10133Lisp_Object Qxpm;
10134
10135/* Indices of image specification fields in xpm_format, below. */
10136
10137enum xpm_keyword_index
10138{
10139 XPM_TYPE,
10140 XPM_FILE,
10141 XPM_DATA,
10142 XPM_ASCENT,
10143 XPM_MARGIN,
10144 XPM_RELIEF,
10145 XPM_ALGORITHM,
10146 XPM_HEURISTIC_MASK,
a05e2bae 10147 XPM_MASK,
6fc2811b 10148 XPM_COLOR_SYMBOLS,
a05e2bae 10149 XPM_BACKGROUND,
6fc2811b
JR
10150 XPM_LAST
10151};
10152
10153/* Vector of image_keyword structures describing the format
10154 of valid XPM image specifications. */
10155
10156static struct image_keyword xpm_format[XPM_LAST] =
10157{
10158 {":type", IMAGE_SYMBOL_VALUE, 1},
10159 {":file", IMAGE_STRING_VALUE, 0},
10160 {":data", IMAGE_STRING_VALUE, 0},
10161 {":ascent", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
8edb0a6f 10162 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
6fc2811b 10163 {":relief", IMAGE_INTEGER_VALUE, 0},
a93f4566 10164 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6fc2811b 10165 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
a05e2bae
JR
10166 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
10167 {":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
10168 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
6fc2811b
JR
10169};
10170
10171/* Structure describing the image type XBM. */
10172
10173static struct image_type xpm_type =
10174{
10175 &Qxpm,
10176 xpm_image_p,
10177 xpm_load,
10178 x_clear_image,
10179 NULL
10180};
10181
10182
10183/* Value is non-zero if COLOR_SYMBOLS is a valid color symbols list
10184 for XPM images. Such a list must consist of conses whose car and
10185 cdr are strings. */
10186
10187static int
10188xpm_valid_color_symbols_p (color_symbols)
10189 Lisp_Object color_symbols;
10190{
10191 while (CONSP (color_symbols))
10192 {
10193 Lisp_Object sym = XCAR (color_symbols);
10194 if (!CONSP (sym)
10195 || !STRINGP (XCAR (sym))
10196 || !STRINGP (XCDR (sym)))
10197 break;
10198 color_symbols = XCDR (color_symbols);
10199 }
10200
10201 return NILP (color_symbols);
10202}
10203
10204
10205/* Value is non-zero if OBJECT is a valid XPM image specification. */
10206
10207static int
10208xpm_image_p (object)
10209 Lisp_Object object;
10210{
10211 struct image_keyword fmt[XPM_LAST];
10212 bcopy (xpm_format, fmt, sizeof fmt);
10213 return (parse_image_spec (object, fmt, XPM_LAST, Qxpm)
10214 /* Either `:file' or `:data' must be present. */
10215 && fmt[XPM_FILE].count + fmt[XPM_DATA].count == 1
10216 /* Either no `:color-symbols' or it's a list of conses
10217 whose car and cdr are strings. */
10218 && (fmt[XPM_COLOR_SYMBOLS].count == 0
10219 || xpm_valid_color_symbols_p (fmt[XPM_COLOR_SYMBOLS].value))
10220 && (fmt[XPM_ASCENT].count == 0
10221 || XFASTINT (fmt[XPM_ASCENT].value) < 100));
10222}
10223
10224
10225/* Load image IMG which will be displayed on frame F. Value is
10226 non-zero if successful. */
10227
10228static int
10229xpm_load (f, img)
10230 struct frame *f;
10231 struct image *img;
10232{
10233 int rc, i;
10234 XpmAttributes attrs;
10235 Lisp_Object specified_file, color_symbols;
10236
10237 /* Configure the XPM lib. Use the visual of frame F. Allocate
10238 close colors. Return colors allocated. */
10239 bzero (&attrs, sizeof attrs);
dfff8a69
JR
10240 attrs.visual = FRAME_X_VISUAL (f);
10241 attrs.colormap = FRAME_X_COLORMAP (f);
6fc2811b 10242 attrs.valuemask |= XpmVisual;
dfff8a69 10243 attrs.valuemask |= XpmColormap;
6fc2811b 10244 attrs.valuemask |= XpmReturnAllocPixels;
dfff8a69 10245#ifdef XpmAllocCloseColors
6fc2811b
JR
10246 attrs.alloc_close_colors = 1;
10247 attrs.valuemask |= XpmAllocCloseColors;
dfff8a69
JR
10248#else
10249 attrs.closeness = 600;
10250 attrs.valuemask |= XpmCloseness;
10251#endif
6fc2811b
JR
10252
10253 /* If image specification contains symbolic color definitions, add
10254 these to `attrs'. */
10255 color_symbols = image_spec_value (img->spec, QCcolor_symbols, NULL);
10256 if (CONSP (color_symbols))
10257 {
10258 Lisp_Object tail;
10259 XpmColorSymbol *xpm_syms;
10260 int i, size;
10261
10262 attrs.valuemask |= XpmColorSymbols;
10263
10264 /* Count number of symbols. */
10265 attrs.numsymbols = 0;
10266 for (tail = color_symbols; CONSP (tail); tail = XCDR (tail))
10267 ++attrs.numsymbols;
10268
10269 /* Allocate an XpmColorSymbol array. */
10270 size = attrs.numsymbols * sizeof *xpm_syms;
10271 xpm_syms = (XpmColorSymbol *) alloca (size);
10272 bzero (xpm_syms, size);
10273 attrs.colorsymbols = xpm_syms;
10274
10275 /* Fill the color symbol array. */
10276 for (tail = color_symbols, i = 0;
10277 CONSP (tail);
10278 ++i, tail = XCDR (tail))
10279 {
10280 Lisp_Object name = XCAR (XCAR (tail));
10281 Lisp_Object color = XCDR (XCAR (tail));
10282 xpm_syms[i].name = (char *) alloca (XSTRING (name)->size + 1);
10283 strcpy (xpm_syms[i].name, XSTRING (name)->data);
10284 xpm_syms[i].value = (char *) alloca (XSTRING (color)->size + 1);
10285 strcpy (xpm_syms[i].value, XSTRING (color)->data);
10286 }
10287 }
10288
10289 /* Create a pixmap for the image, either from a file, or from a
10290 string buffer containing data in the same format as an XPM file. */
10291 BLOCK_INPUT;
10292 specified_file = image_spec_value (img->spec, QCfile, NULL);
10293 if (STRINGP (specified_file))
10294 {
10295 Lisp_Object file = x_find_image_file (specified_file);
10296 if (!STRINGP (file))
10297 {
10298 image_error ("Cannot find image file `%s'", specified_file, Qnil);
10299 UNBLOCK_INPUT;
10300 return 0;
10301 }
10302
10303 rc = XpmReadFileToPixmap (NULL, FRAME_W32_WINDOW (f),
10304 XSTRING (file)->data, &img->pixmap, &img->mask,
10305 &attrs);
10306 }
10307 else
10308 {
10309 Lisp_Object buffer = image_spec_value (img->spec, QCdata, NULL);
10310 rc = XpmCreatePixmapFromBuffer (NULL, FRAME_W32_WINDOW (f),
10311 XSTRING (buffer)->data,
10312 &img->pixmap, &img->mask,
10313 &attrs);
10314 }
10315 UNBLOCK_INPUT;
10316
10317 if (rc == XpmSuccess)
10318 {
10319 /* Remember allocated colors. */
10320 img->ncolors = attrs.nalloc_pixels;
10321 img->colors = (unsigned long *) xmalloc (img->ncolors
10322 * sizeof *img->colors);
10323 for (i = 0; i < attrs.nalloc_pixels; ++i)
10324 img->colors[i] = attrs.alloc_pixels[i];
10325
10326 img->width = attrs.width;
10327 img->height = attrs.height;
10328 xassert (img->width > 0 && img->height > 0);
10329
10330 /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */
10331 BLOCK_INPUT;
10332 XpmFreeAttributes (&attrs);
10333 UNBLOCK_INPUT;
10334 }
10335 else
10336 {
10337 switch (rc)
10338 {
10339 case XpmOpenFailed:
10340 image_error ("Error opening XPM file (%s)", img->spec, Qnil);
10341 break;
10342
10343 case XpmFileInvalid:
10344 image_error ("Invalid XPM file (%s)", img->spec, Qnil);
10345 break;
10346
10347 case XpmNoMemory:
10348 image_error ("Out of memory (%s)", img->spec, Qnil);
10349 break;
10350
10351 case XpmColorFailed:
10352 image_error ("Color allocation error (%s)", img->spec, Qnil);
10353 break;
10354
10355 default:
10356 image_error ("Unknown error (%s)", img->spec, Qnil);
10357 break;
10358 }
10359 }
10360
10361 return rc == XpmSuccess;
10362}
10363
10364#endif /* HAVE_XPM != 0 */
10365
10366\f
767b1ff0 10367#if 0 /* TODO : Color tables on W32. */
6fc2811b
JR
10368/***********************************************************************
10369 Color table
10370 ***********************************************************************/
10371
10372/* An entry in the color table mapping an RGB color to a pixel color. */
10373
10374struct ct_color
10375{
10376 int r, g, b;
10377 unsigned long pixel;
10378
10379 /* Next in color table collision list. */
10380 struct ct_color *next;
10381};
10382
10383/* The bucket vector size to use. Must be prime. */
10384
10385#define CT_SIZE 101
10386
10387/* Value is a hash of the RGB color given by R, G, and B. */
10388
10389#define CT_HASH_RGB(R, G, B) (((R) << 16) ^ ((G) << 8) ^ (B))
10390
10391/* The color hash table. */
10392
10393struct ct_color **ct_table;
10394
10395/* Number of entries in the color table. */
10396
10397int ct_colors_allocated;
10398
10399/* Function prototypes. */
10400
10401static void init_color_table P_ ((void));
10402static void free_color_table P_ ((void));
10403static unsigned long *colors_in_color_table P_ ((int *n));
10404static unsigned long lookup_rgb_color P_ ((struct frame *f, int r, int g, int b));
10405static unsigned long lookup_pixel_color P_ ((struct frame *f, unsigned long p));
10406
10407
10408/* Initialize the color table. */
10409
10410static void
10411init_color_table ()
10412{
10413 int size = CT_SIZE * sizeof (*ct_table);
10414 ct_table = (struct ct_color **) xmalloc (size);
10415 bzero (ct_table, size);
10416 ct_colors_allocated = 0;
10417}
10418
10419
10420/* Free memory associated with the color table. */
10421
10422static void
10423free_color_table ()
10424{
10425 int i;
10426 struct ct_color *p, *next;
10427
10428 for (i = 0; i < CT_SIZE; ++i)
10429 for (p = ct_table[i]; p; p = next)
10430 {
10431 next = p->next;
10432 xfree (p);
10433 }
10434
10435 xfree (ct_table);
10436 ct_table = NULL;
10437}
10438
10439
10440/* Value is a pixel color for RGB color R, G, B on frame F. If an
10441 entry for that color already is in the color table, return the
10442 pixel color of that entry. Otherwise, allocate a new color for R,
10443 G, B, and make an entry in the color table. */
10444
10445static unsigned long
10446lookup_rgb_color (f, r, g, b)
10447 struct frame *f;
10448 int r, g, b;
10449{
10450 unsigned hash = CT_HASH_RGB (r, g, b);
10451 int i = hash % CT_SIZE;
10452 struct ct_color *p;
10453
10454 for (p = ct_table[i]; p; p = p->next)
10455 if (p->r == r && p->g == g && p->b == b)
10456 break;
10457
10458 if (p == NULL)
10459 {
10460 COLORREF color;
10461 Colormap cmap;
10462 int rc;
10463
10464 color = PALETTERGB (r, g, b);
10465
10466 ++ct_colors_allocated;
10467
10468 p = (struct ct_color *) xmalloc (sizeof *p);
10469 p->r = r;
10470 p->g = g;
10471 p->b = b;
10472 p->pixel = color;
10473 p->next = ct_table[i];
10474 ct_table[i] = p;
10475 }
10476
10477 return p->pixel;
10478}
10479
10480
10481/* Look up pixel color PIXEL which is used on frame F in the color
10482 table. If not already present, allocate it. Value is PIXEL. */
10483
10484static unsigned long
10485lookup_pixel_color (f, pixel)
10486 struct frame *f;
10487 unsigned long pixel;
10488{
10489 int i = pixel % CT_SIZE;
10490 struct ct_color *p;
10491
10492 for (p = ct_table[i]; p; p = p->next)
10493 if (p->pixel == pixel)
10494 break;
10495
10496 if (p == NULL)
10497 {
10498 XColor color;
10499 Colormap cmap;
10500 int rc;
10501
10502 BLOCK_INPUT;
10503
10504 cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f));
10505 color.pixel = pixel;
10506 XQueryColor (NULL, cmap, &color);
10507 rc = x_alloc_nearest_color (f, cmap, &color);
10508 UNBLOCK_INPUT;
10509
10510 if (rc)
10511 {
10512 ++ct_colors_allocated;
10513
10514 p = (struct ct_color *) xmalloc (sizeof *p);
10515 p->r = color.red;
10516 p->g = color.green;
10517 p->b = color.blue;
10518 p->pixel = pixel;
10519 p->next = ct_table[i];
10520 ct_table[i] = p;
10521 }
10522 else
10523 return FRAME_FOREGROUND_PIXEL (f);
10524 }
10525 return p->pixel;
10526}
10527
10528
10529/* Value is a vector of all pixel colors contained in the color table,
10530 allocated via xmalloc. Set *N to the number of colors. */
10531
10532static unsigned long *
10533colors_in_color_table (n)
10534 int *n;
10535{
10536 int i, j;
10537 struct ct_color *p;
10538 unsigned long *colors;
10539
10540 if (ct_colors_allocated == 0)
10541 {
10542 *n = 0;
10543 colors = NULL;
10544 }
10545 else
10546 {
10547 colors = (unsigned long *) xmalloc (ct_colors_allocated
10548 * sizeof *colors);
10549 *n = ct_colors_allocated;
10550
10551 for (i = j = 0; i < CT_SIZE; ++i)
10552 for (p = ct_table[i]; p; p = p->next)
10553 colors[j++] = p->pixel;
10554 }
10555
10556 return colors;
10557}
10558
767b1ff0 10559#endif /* TODO */
6fc2811b
JR
10560
10561\f
ac849ba4 10562#ifdef HAVE_IMAGES /* TODO */
6fc2811b
JR
10563/***********************************************************************
10564 Algorithms
10565 ***********************************************************************/
3cf3436e
JR
10566static XColor *x_to_xcolors P_ ((struct frame *, struct image *, int));
10567static void x_from_xcolors P_ ((struct frame *, struct image *, XColor *));
10568static void x_detect_edges P_ ((struct frame *, struct image *, int[9], int));
ac849ba4 10569static void XPutPixel (XImage *, int, int, COLORREF);
3cf3436e
JR
10570
10571/* Non-zero means draw a cross on images having `:conversion
10572 disabled'. */
6fc2811b 10573
3cf3436e 10574int cross_disabled_images;
6fc2811b 10575
3cf3436e
JR
10576/* Edge detection matrices for different edge-detection
10577 strategies. */
6fc2811b 10578
3cf3436e
JR
10579static int emboss_matrix[9] = {
10580 /* x - 1 x x + 1 */
10581 2, -1, 0, /* y - 1 */
10582 -1, 0, 1, /* y */
10583 0, 1, -2 /* y + 1 */
10584};
10585
10586static int laplace_matrix[9] = {
10587 /* x - 1 x x + 1 */
10588 1, 0, 0, /* y - 1 */
10589 0, 0, 0, /* y */
10590 0, 0, -1 /* y + 1 */
10591};
10592
10593/* Value is the intensity of the color whose red/green/blue values
10594 are R, G, and B. */
10595
10596#define COLOR_INTENSITY(R, G, B) ((2 * (R) + 3 * (G) + (B)) / 6)
10597
10598
10599/* On frame F, return an array of XColor structures describing image
10600 IMG->pixmap. Each XColor structure has its pixel color set. RGB_P
10601 non-zero means also fill the red/green/blue members of the XColor
10602 structures. Value is a pointer to the array of XColors structures,
10603 allocated with xmalloc; it must be freed by the caller. */
10604
10605static XColor *
10606x_to_xcolors (f, img, rgb_p)
10607 struct frame *f;
10608 struct image *img;
10609 int rgb_p;
10610{
10611 int x, y;
10612 XColor *colors, *p;
10613 XImage *ximg;
10614
10615 colors = (XColor *) xmalloc (img->width * img->height * sizeof *colors);
ac849ba4 10616#if 0 /* TODO: implement image colors. */
3cf3436e
JR
10617 /* Get the X image IMG->pixmap. */
10618 ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap,
10619 0, 0, img->width, img->height, ~0, ZPixmap);
10620
10621 /* Fill the `pixel' members of the XColor array. I wished there
10622 were an easy and portable way to circumvent XGetPixel. */
10623 p = colors;
10624 for (y = 0; y < img->height; ++y)
10625 {
10626 XColor *row = p;
10627
10628 for (x = 0; x < img->width; ++x, ++p)
10629 p->pixel = XGetPixel (ximg, x, y);
10630
10631 if (rgb_p)
10632 x_query_colors (f, row, img->width);
10633 }
10634
10635 XDestroyImage (ximg);
ac849ba4 10636#endif
3cf3436e
JR
10637 return colors;
10638}
10639
ac849ba4
JR
10640/* Put a pixel of COLOR at position X, Y in XIMG. XIMG must have been
10641 created with CreateDIBSection, with the pointer to the bit values
10642 stored in ximg->data. */
10643
10644static void XPutPixel (ximg, x, y, color)
10645 XImage * ximg;
10646 int x, y;
10647 COLORREF color;
10648{
10649 int width = ximg->info.bmiHeader.biWidth;
10650 int height = ximg->info.bmiHeader.biHeight;
10651 int rowbytes = width * 3;
10652 unsigned char * pixel;
10653
10654 /* Don't support putting pixels in images with palettes. */
10655 xassert (ximg->info.bmiHeader.biBitCount == 24);
10656
10657 /* Ensure scanlines are aligned on 4 byte boundaries. */
10658 if (rowbytes % 4)
10659 rowbytes += 4 - (rowbytes % 4);
10660
10661 pixel = ximg->data + y * rowbytes + x * 3;
10662 *pixel = 255 - GetRValue (color);
10663 *(pixel + 1) = 255 - GetGValue (color);
10664 *(pixel + 2) = 255 - GetBValue (color);
10665}
10666
3cf3436e
JR
10667
10668/* Create IMG->pixmap from an array COLORS of XColor structures, whose
10669 RGB members are set. F is the frame on which this all happens.
10670 COLORS will be freed; an existing IMG->pixmap will be freed, too. */
6fc2811b
JR
10671
10672static void
3cf3436e 10673x_from_xcolors (f, img, colors)
6fc2811b 10674 struct frame *f;
3cf3436e 10675 struct image *img;
6fc2811b 10676 XColor *colors;
6fc2811b 10677{
3cf3436e
JR
10678 int x, y;
10679 XImage *oimg;
10680 Pixmap pixmap;
10681 XColor *p;
ac849ba4 10682#if 0 /* TODO: color tables. */
3cf3436e 10683 init_color_table ();
ac849ba4 10684#endif
3cf3436e
JR
10685 x_create_x_image_and_pixmap (f, img->width, img->height, 0,
10686 &oimg, &pixmap);
10687 p = colors;
10688 for (y = 0; y < img->height; ++y)
10689 for (x = 0; x < img->width; ++x, ++p)
10690 {
10691 unsigned long pixel;
ac849ba4 10692#if 0 /* TODO: color tables. */
3cf3436e 10693 pixel = lookup_rgb_color (f, p->red, p->green, p->blue);
ac849ba4
JR
10694#else
10695 pixel = PALETTERGB (p->red, p->green, p->blue);
10696#endif
3cf3436e
JR
10697 XPutPixel (oimg, x, y, pixel);
10698 }
6fc2811b 10699
3cf3436e
JR
10700 xfree (colors);
10701 x_clear_image_1 (f, img, 1, 0, 1);
6fc2811b 10702
3cf3436e
JR
10703 x_put_x_image (f, oimg, pixmap, img->width, img->height);
10704 x_destroy_x_image (oimg);
10705 img->pixmap = pixmap;
ac849ba4 10706#if 0 /* TODO: color tables. */
3cf3436e
JR
10707 img->colors = colors_in_color_table (&img->ncolors);
10708 free_color_table ();
ac849ba4 10709#endif
6fc2811b
JR
10710}
10711
10712
3cf3436e
JR
10713/* On frame F, perform edge-detection on image IMG.
10714
10715 MATRIX is a nine-element array specifying the transformation
10716 matrix. See emboss_matrix for an example.
10717
10718 COLOR_ADJUST is a color adjustment added to each pixel of the
10719 outgoing image. */
6fc2811b
JR
10720
10721static void
3cf3436e 10722x_detect_edges (f, img, matrix, color_adjust)
6fc2811b 10723 struct frame *f;
3cf3436e
JR
10724 struct image *img;
10725 int matrix[9], color_adjust;
6fc2811b 10726{
3cf3436e
JR
10727 XColor *colors = x_to_xcolors (f, img, 1);
10728 XColor *new, *p;
10729 int x, y, i, sum;
10730
10731 for (i = sum = 0; i < 9; ++i)
10732 sum += abs (matrix[i]);
10733
10734#define COLOR(A, X, Y) ((A) + (Y) * img->width + (X))
10735
10736 new = (XColor *) xmalloc (img->width * img->height * sizeof *new);
10737
10738 for (y = 0; y < img->height; ++y)
10739 {
10740 p = COLOR (new, 0, y);
10741 p->red = p->green = p->blue = 0xffff/2;
10742 p = COLOR (new, img->width - 1, y);
10743 p->red = p->green = p->blue = 0xffff/2;
10744 }
6fc2811b 10745
3cf3436e
JR
10746 for (x = 1; x < img->width - 1; ++x)
10747 {
10748 p = COLOR (new, x, 0);
10749 p->red = p->green = p->blue = 0xffff/2;
10750 p = COLOR (new, x, img->height - 1);
10751 p->red = p->green = p->blue = 0xffff/2;
10752 }
10753
10754 for (y = 1; y < img->height - 1; ++y)
10755 {
10756 p = COLOR (new, 1, y);
10757
10758 for (x = 1; x < img->width - 1; ++x, ++p)
10759 {
10760 int r, g, b, y1, x1;
10761
10762 r = g = b = i = 0;
10763 for (y1 = y - 1; y1 < y + 2; ++y1)
10764 for (x1 = x - 1; x1 < x + 2; ++x1, ++i)
10765 if (matrix[i])
10766 {
10767 XColor *t = COLOR (colors, x1, y1);
10768 r += matrix[i] * t->red;
10769 g += matrix[i] * t->green;
10770 b += matrix[i] * t->blue;
10771 }
10772
10773 r = (r / sum + color_adjust) & 0xffff;
10774 g = (g / sum + color_adjust) & 0xffff;
10775 b = (b / sum + color_adjust) & 0xffff;
10776 p->red = p->green = p->blue = COLOR_INTENSITY (r, g, b);
10777 }
10778 }
10779
10780 xfree (colors);
10781 x_from_xcolors (f, img, new);
10782
10783#undef COLOR
10784}
10785
10786
10787/* Perform the pre-defined `emboss' edge-detection on image IMG
10788 on frame F. */
10789
10790static void
10791x_emboss (f, img)
10792 struct frame *f;
10793 struct image *img;
10794{
10795 x_detect_edges (f, img, emboss_matrix, 0xffff / 2);
6fc2811b 10796}
3cf3436e 10797
6fc2811b
JR
10798
10799/* Transform image IMG which is used on frame F with a Laplace
10800 edge-detection algorithm. The result is an image that can be used
10801 to draw disabled buttons, for example. */
10802
10803static void
10804x_laplace (f, img)
10805 struct frame *f;
10806 struct image *img;
10807{
3cf3436e
JR
10808 x_detect_edges (f, img, laplace_matrix, 45000);
10809}
6fc2811b 10810
6fc2811b 10811
3cf3436e
JR
10812/* Perform edge-detection on image IMG on frame F, with specified
10813 transformation matrix MATRIX and color-adjustment COLOR_ADJUST.
6fc2811b 10814
3cf3436e 10815 MATRIX must be either
6fc2811b 10816
3cf3436e
JR
10817 - a list of at least 9 numbers in row-major form
10818 - a vector of at least 9 numbers
6fc2811b 10819
3cf3436e
JR
10820 COLOR_ADJUST nil means use a default; otherwise it must be a
10821 number. */
6fc2811b 10822
3cf3436e
JR
10823static void
10824x_edge_detection (f, img, matrix, color_adjust)
10825 struct frame *f;
10826 struct image *img;
10827 Lisp_Object matrix, color_adjust;
10828{
10829 int i = 0;
10830 int trans[9];
10831
10832 if (CONSP (matrix))
6fc2811b 10833 {
3cf3436e
JR
10834 for (i = 0;
10835 i < 9 && CONSP (matrix) && NUMBERP (XCAR (matrix));
10836 ++i, matrix = XCDR (matrix))
10837 trans[i] = XFLOATINT (XCAR (matrix));
10838 }
10839 else if (VECTORP (matrix) && ASIZE (matrix) >= 9)
10840 {
10841 for (i = 0; i < 9 && NUMBERP (AREF (matrix, i)); ++i)
10842 trans[i] = XFLOATINT (AREF (matrix, i));
10843 }
10844
10845 if (NILP (color_adjust))
10846 color_adjust = make_number (0xffff / 2);
10847
10848 if (i == 9 && NUMBERP (color_adjust))
10849 x_detect_edges (f, img, trans, (int) XFLOATINT (color_adjust));
10850}
10851
6fc2811b 10852
3cf3436e 10853/* Transform image IMG on frame F so that it looks disabled. */
6fc2811b 10854
3cf3436e
JR
10855static void
10856x_disable_image (f, img)
10857 struct frame *f;
10858 struct image *img;
10859{
ac849ba4 10860 struct w32_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3cf3436e 10861
ac849ba4 10862 if (dpyinfo->n_planes * dpyinfo->n_cbits >= 2)
3cf3436e
JR
10863 {
10864 /* Color (or grayscale). Convert to gray, and equalize. Just
10865 drawing such images with a stipple can look very odd, so
10866 we're using this method instead. */
10867 XColor *colors = x_to_xcolors (f, img, 1);
10868 XColor *p, *end;
10869 const int h = 15000;
10870 const int l = 30000;
10871
10872 for (p = colors, end = colors + img->width * img->height;
10873 p < end;
10874 ++p)
6fc2811b 10875 {
3cf3436e
JR
10876 int i = COLOR_INTENSITY (p->red, p->green, p->blue);
10877 int i2 = (0xffff - h - l) * i / 0xffff + l;
10878 p->red = p->green = p->blue = i2;
6fc2811b
JR
10879 }
10880
3cf3436e 10881 x_from_xcolors (f, img, colors);
6fc2811b
JR
10882 }
10883
3cf3436e
JR
10884 /* Draw a cross over the disabled image, if we must or if we
10885 should. */
ac849ba4 10886 if (dpyinfo->n_planes * dpyinfo->n_cbits < 2 || cross_disabled_images)
3cf3436e 10887 {
ac849ba4 10888#if 0 /* TODO: full image support */
3cf3436e
JR
10889 Display *dpy = FRAME_X_DISPLAY (f);
10890 GC gc;
6fc2811b 10891
3cf3436e
JR
10892 gc = XCreateGC (dpy, img->pixmap, 0, NULL);
10893 XSetForeground (dpy, gc, BLACK_PIX_DEFAULT (f));
10894 XDrawLine (dpy, img->pixmap, gc, 0, 0,
10895 img->width - 1, img->height - 1);
10896 XDrawLine (dpy, img->pixmap, gc, 0, img->height - 1,
10897 img->width - 1, 0);
10898 XFreeGC (dpy, gc);
6fc2811b 10899
3cf3436e
JR
10900 if (img->mask)
10901 {
10902 gc = XCreateGC (dpy, img->mask, 0, NULL);
10903 XSetForeground (dpy, gc, WHITE_PIX_DEFAULT (f));
10904 XDrawLine (dpy, img->mask, gc, 0, 0,
10905 img->width - 1, img->height - 1);
10906 XDrawLine (dpy, img->mask, gc, 0, img->height - 1,
10907 img->width - 1, 0);
10908 XFreeGC (dpy, gc);
10909 }
ac849ba4 10910#endif
3cf3436e 10911 }
6fc2811b
JR
10912}
10913
10914
10915/* Build a mask for image IMG which is used on frame F. FILE is the
10916 name of an image file, for error messages. HOW determines how to
10917 determine the background color of IMG. If it is a list '(R G B)',
10918 with R, G, and B being integers >= 0, take that as the color of the
10919 background. Otherwise, determine the background color of IMG
10920 heuristically. Value is non-zero if successful. */
10921
10922static int
10923x_build_heuristic_mask (f, img, how)
10924 struct frame *f;
10925 struct image *img;
10926 Lisp_Object how;
10927{
ac849ba4 10928#if 0 /* TODO: full image support. */
6fc2811b
JR
10929 Display *dpy = FRAME_W32_DISPLAY (f);
10930 XImage *ximg, *mask_img;
a05e2bae
JR
10931 int x, y, rc, use_img_background;
10932 unsigned long bg = 0;
10933
10934 if (img->mask)
10935 {
10936 XFreePixmap (FRAME_X_DISPLAY (f), img->mask);
10937 img->mask = None;
10938 img->background_transparent_valid = 0;
10939 }
6fc2811b 10940
6fc2811b
JR
10941 /* Create an image and pixmap serving as mask. */
10942 rc = x_create_x_image_and_pixmap (f, img->width, img->height, 1,
10943 &mask_img, &img->mask);
10944 if (!rc)
a05e2bae 10945 return 0;
6fc2811b
JR
10946
10947 /* Get the X image of IMG->pixmap. */
10948 ximg = XGetImage (dpy, img->pixmap, 0, 0, img->width, img->height,
10949 ~0, ZPixmap);
10950
10951 /* Determine the background color of ximg. If HOW is `(R G B)'
a05e2bae
JR
10952 take that as color. Otherwise, use the image's background color. */
10953 use_img_background = 1;
6fc2811b
JR
10954
10955 if (CONSP (how))
10956 {
a05e2bae 10957 int rgb[3], i;
6fc2811b 10958
a05e2bae 10959 for (i = 0; i < 3 && CONSP (how) && NATNUMP (XCAR (how)); ++i)
6fc2811b
JR
10960 {
10961 rgb[i] = XFASTINT (XCAR (how)) & 0xffff;
10962 how = XCDR (how);
10963 }
10964
10965 if (i == 3 && NILP (how))
10966 {
10967 char color_name[30];
6fc2811b 10968 sprintf (color_name, "#%04x%04x%04x", rgb[0], rgb[1], rgb[2]);
a05e2bae
JR
10969 bg = x_alloc_image_color (f, img, build_string (color_name), 0);
10970 use_img_background = 0;
6fc2811b
JR
10971 }
10972 }
10973
a05e2bae
JR
10974 if (use_img_background)
10975 bg = four_corners_best (ximg, img->width, img->height);
6fc2811b
JR
10976
10977 /* Set all bits in mask_img to 1 whose color in ximg is different
10978 from the background color bg. */
10979 for (y = 0; y < img->height; ++y)
10980 for (x = 0; x < img->width; ++x)
10981 XPutPixel (mask_img, x, y, XGetPixel (ximg, x, y) != bg);
10982
a05e2bae
JR
10983 /* Fill in the background_transparent field while we have the mask handy. */
10984 image_background_transparent (img, f, mask_img);
10985
6fc2811b
JR
10986 /* Put mask_img into img->mask. */
10987 x_put_x_image (f, mask_img, img->mask, img->width, img->height);
10988 x_destroy_x_image (mask_img);
10989 XDestroyImage (ximg);
6fc2811b
JR
10990
10991 return 1;
ac849ba4
JR
10992#else
10993 return 0;
10994#endif
6fc2811b 10995}
ac849ba4 10996#endif
6fc2811b
JR
10997\f
10998/***********************************************************************
10999 PBM (mono, gray, color)
11000 ***********************************************************************/
11001#ifdef HAVE_PBM
11002
11003static int pbm_image_p P_ ((Lisp_Object object));
11004static int pbm_load P_ ((struct frame *f, struct image *img));
11005static int pbm_scan_number P_ ((unsigned char **, unsigned char *));
11006
11007/* The symbol `pbm' identifying images of this type. */
11008
11009Lisp_Object Qpbm;
11010
11011/* Indices of image specification fields in gs_format, below. */
11012
11013enum pbm_keyword_index
11014{
11015 PBM_TYPE,
11016 PBM_FILE,
11017 PBM_DATA,
11018 PBM_ASCENT,
11019 PBM_MARGIN,
11020 PBM_RELIEF,
11021 PBM_ALGORITHM,
11022 PBM_HEURISTIC_MASK,
a05e2bae
JR
11023 PBM_MASK,
11024 PBM_FOREGROUND,
11025 PBM_BACKGROUND,
6fc2811b
JR
11026 PBM_LAST
11027};
11028
11029/* Vector of image_keyword structures describing the format
11030 of valid user-defined image specifications. */
11031
11032static struct image_keyword pbm_format[PBM_LAST] =
11033{
11034 {":type", IMAGE_SYMBOL_VALUE, 1},
11035 {":file", IMAGE_STRING_VALUE, 0},
11036 {":data", IMAGE_STRING_VALUE, 0},
11037 {":ascent", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
8edb0a6f 11038 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
6fc2811b 11039 {":relief", IMAGE_INTEGER_VALUE, 0},
a93f4566 11040 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
3cf3436e
JR
11041 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
11042 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
11043 {":foreground", IMAGE_STRING_OR_NIL_VALUE, 0},
11044 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
6fc2811b
JR
11045};
11046
11047/* Structure describing the image type `pbm'. */
11048
11049static struct image_type pbm_type =
11050{
11051 &Qpbm,
11052 pbm_image_p,
11053 pbm_load,
11054 x_clear_image,
11055 NULL
11056};
11057
11058
11059/* Return non-zero if OBJECT is a valid PBM image specification. */
11060
11061static int
11062pbm_image_p (object)
11063 Lisp_Object object;
11064{
11065 struct image_keyword fmt[PBM_LAST];
11066
11067 bcopy (pbm_format, fmt, sizeof fmt);
11068
11069 if (!parse_image_spec (object, fmt, PBM_LAST, Qpbm)
11070 || (fmt[PBM_ASCENT].count
11071 && XFASTINT (fmt[PBM_ASCENT].value) > 100))
11072 return 0;
11073
11074 /* Must specify either :data or :file. */
11075 return fmt[PBM_DATA].count + fmt[PBM_FILE].count == 1;
11076}
11077
11078
11079/* Scan a decimal number from *S and return it. Advance *S while
11080 reading the number. END is the end of the string. Value is -1 at
11081 end of input. */
11082
11083static int
11084pbm_scan_number (s, end)
11085 unsigned char **s, *end;
11086{
11087 int c, val = -1;
11088
11089 while (*s < end)
11090 {
11091 /* Skip white-space. */
11092 while (*s < end && (c = *(*s)++, isspace (c)))
11093 ;
11094
11095 if (c == '#')
11096 {
11097 /* Skip comment to end of line. */
11098 while (*s < end && (c = *(*s)++, c != '\n'))
11099 ;
11100 }
11101 else if (isdigit (c))
11102 {
11103 /* Read decimal number. */
11104 val = c - '0';
11105 while (*s < end && (c = *(*s)++, isdigit (c)))
11106 val = 10 * val + c - '0';
11107 break;
11108 }
11109 else
11110 break;
11111 }
11112
11113 return val;
11114}
11115
11116
11117/* Read FILE into memory. Value is a pointer to a buffer allocated
11118 with xmalloc holding FILE's contents. Value is null if an error
11119 occured. *SIZE is set to the size of the file. */
11120
11121static char *
11122pbm_read_file (file, size)
11123 Lisp_Object file;
11124 int *size;
11125{
11126 FILE *fp = NULL;
11127 char *buf = NULL;
11128 struct stat st;
11129
11130 if (stat (XSTRING (file)->data, &st) == 0
11131 && (fp = fopen (XSTRING (file)->data, "r")) != NULL
11132 && (buf = (char *) xmalloc (st.st_size),
11133 fread (buf, 1, st.st_size, fp) == st.st_size))
11134 {
11135 *size = st.st_size;
11136 fclose (fp);
11137 }
11138 else
11139 {
11140 if (fp)
11141 fclose (fp);
11142 if (buf)
11143 {
11144 xfree (buf);
11145 buf = NULL;
11146 }
11147 }
11148
11149 return buf;
11150}
11151
11152
11153/* Load PBM image IMG for use on frame F. */
11154
11155static int
11156pbm_load (f, img)
11157 struct frame *f;
11158 struct image *img;
11159{
11160 int raw_p, x, y;
11161 int width, height, max_color_idx = 0;
11162 XImage *ximg;
11163 Lisp_Object file, specified_file;
11164 enum {PBM_MONO, PBM_GRAY, PBM_COLOR} type;
11165 struct gcpro gcpro1;
11166 unsigned char *contents = NULL;
11167 unsigned char *end, *p;
11168 int size;
11169
11170 specified_file = image_spec_value (img->spec, QCfile, NULL);
11171 file = Qnil;
11172 GCPRO1 (file);
11173
11174 if (STRINGP (specified_file))
11175 {
11176 file = x_find_image_file (specified_file);
11177 if (!STRINGP (file))
11178 {
11179 image_error ("Cannot find image file `%s'", specified_file, Qnil);
11180 UNGCPRO;
11181 return 0;
11182 }
11183
3cf3436e 11184 contents = slurp_file (XSTRING (file)->data, &size);
6fc2811b
JR
11185 if (contents == NULL)
11186 {
11187 image_error ("Error reading `%s'", file, Qnil);
11188 UNGCPRO;
11189 return 0;
11190 }
11191
11192 p = contents;
11193 end = contents + size;
11194 }
11195 else
11196 {
11197 Lisp_Object data;
11198 data = image_spec_value (img->spec, QCdata, NULL);
11199 p = XSTRING (data)->data;
11200 end = p + STRING_BYTES (XSTRING (data));
11201 }
11202
11203 /* Check magic number. */
11204 if (end - p < 2 || *p++ != 'P')
11205 {
11206 image_error ("Not a PBM image: `%s'", img->spec, Qnil);
11207 error:
11208 xfree (contents);
11209 UNGCPRO;
11210 return 0;
11211 }
11212
6fc2811b
JR
11213 switch (*p++)
11214 {
11215 case '1':
11216 raw_p = 0, type = PBM_MONO;
11217 break;
11218
11219 case '2':
11220 raw_p = 0, type = PBM_GRAY;
11221 break;
11222
11223 case '3':
11224 raw_p = 0, type = PBM_COLOR;
11225 break;
11226
11227 case '4':
11228 raw_p = 1, type = PBM_MONO;
11229 break;
11230
11231 case '5':
11232 raw_p = 1, type = PBM_GRAY;
11233 break;
11234
11235 case '6':
11236 raw_p = 1, type = PBM_COLOR;
11237 break;
11238
11239 default:
11240 image_error ("Not a PBM image: `%s'", img->spec, Qnil);
11241 goto error;
11242 }
11243
11244 /* Read width, height, maximum color-component. Characters
11245 starting with `#' up to the end of a line are ignored. */
11246 width = pbm_scan_number (&p, end);
11247 height = pbm_scan_number (&p, end);
11248
11249 if (type != PBM_MONO)
11250 {
11251 max_color_idx = pbm_scan_number (&p, end);
11252 if (raw_p && max_color_idx > 255)
11253 max_color_idx = 255;
11254 }
11255
11256 if (width < 0
11257 || height < 0
11258 || (type != PBM_MONO && max_color_idx < 0))
11259 goto error;
11260
ac849ba4 11261 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
3cf3436e
JR
11262 goto error;
11263
ac849ba4 11264#if 0 /* TODO: color tables. */
6fc2811b
JR
11265 /* Initialize the color hash table. */
11266 init_color_table ();
ac849ba4 11267#endif
6fc2811b
JR
11268
11269 if (type == PBM_MONO)
11270 {
11271 int c = 0, g;
3cf3436e
JR
11272 struct image_keyword fmt[PBM_LAST];
11273 unsigned long fg = FRAME_FOREGROUND_PIXEL (f);
11274 unsigned long bg = FRAME_BACKGROUND_PIXEL (f);
11275
11276 /* Parse the image specification. */
11277 bcopy (pbm_format, fmt, sizeof fmt);
11278 parse_image_spec (img->spec, fmt, PBM_LAST, Qpbm);
11279
11280 /* Get foreground and background colors, maybe allocate colors. */
11281 if (fmt[PBM_FOREGROUND].count
11282 && STRINGP (fmt[PBM_FOREGROUND].value))
11283 fg = x_alloc_image_color (f, img, fmt[PBM_FOREGROUND].value, fg);
11284 if (fmt[PBM_BACKGROUND].count
11285 && STRINGP (fmt[PBM_BACKGROUND].value))
a05e2bae
JR
11286 {
11287 bg = x_alloc_image_color (f, img, fmt[PBM_BACKGROUND].value, bg);
11288 img->background = bg;
11289 img->background_valid = 1;
11290 }
11291
6fc2811b
JR
11292 for (y = 0; y < height; ++y)
11293 for (x = 0; x < width; ++x)
11294 {
11295 if (raw_p)
11296 {
11297 if ((x & 7) == 0)
11298 c = *p++;
11299 g = c & 0x80;
11300 c <<= 1;
11301 }
11302 else
11303 g = pbm_scan_number (&p, end);
11304
3cf3436e 11305 XPutPixel (ximg, x, y, g ? fg : bg);
6fc2811b
JR
11306 }
11307 }
11308 else
11309 {
11310 for (y = 0; y < height; ++y)
11311 for (x = 0; x < width; ++x)
11312 {
11313 int r, g, b;
11314
11315 if (type == PBM_GRAY)
11316 r = g = b = raw_p ? *p++ : pbm_scan_number (&p, end);
11317 else if (raw_p)
11318 {
11319 r = *p++;
11320 g = *p++;
11321 b = *p++;
11322 }
11323 else
11324 {
11325 r = pbm_scan_number (&p, end);
11326 g = pbm_scan_number (&p, end);
11327 b = pbm_scan_number (&p, end);
11328 }
11329
11330 if (r < 0 || g < 0 || b < 0)
11331 {
ac849ba4 11332 x_destroy_x_image (ximg);
6fc2811b
JR
11333 image_error ("Invalid pixel value in image `%s'",
11334 img->spec, Qnil);
11335 goto error;
11336 }
11337
11338 /* RGB values are now in the range 0..max_color_idx.
ac849ba4
JR
11339 Scale this to the range 0..0xff supported by W32. */
11340 r = (int) ((double) r * 255 / max_color_idx);
11341 g = (int) ((double) g * 255 / max_color_idx);
11342 b = (int) ((double) b * 255 / max_color_idx);
11343 XPutPixel (ximg, x, y,
11344#if 0 /* TODO: color tables. */
11345 lookup_rgb_color (f, r, g, b));
11346#else
11347 PALETTERGB (r, g, b));
11348#endif
6fc2811b
JR
11349 }
11350 }
ac849ba4
JR
11351
11352#if 0 /* TODO: color tables. */
6fc2811b
JR
11353 /* Store in IMG->colors the colors allocated for the image, and
11354 free the color table. */
11355 img->colors = colors_in_color_table (&img->ncolors);
11356 free_color_table ();
ac849ba4 11357#endif
a05e2bae
JR
11358 /* Maybe fill in the background field while we have ximg handy. */
11359 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
11360 IMAGE_BACKGROUND (img, f, ximg);
11361
6fc2811b
JR
11362 /* Put the image into a pixmap. */
11363 x_put_x_image (f, ximg, img->pixmap, width, height);
11364 x_destroy_x_image (ximg);
6fc2811b
JR
11365
11366 img->width = width;
11367 img->height = height;
11368
11369 UNGCPRO;
11370 xfree (contents);
11371 return 1;
11372}
11373#endif /* HAVE_PBM */
11374
11375\f
11376/***********************************************************************
11377 PNG
11378 ***********************************************************************/
11379
11380#if HAVE_PNG
11381
11382#include <png.h>
11383
11384/* Function prototypes. */
11385
11386static int png_image_p P_ ((Lisp_Object object));
11387static int png_load P_ ((struct frame *f, struct image *img));
11388
11389/* The symbol `png' identifying images of this type. */
11390
11391Lisp_Object Qpng;
11392
11393/* Indices of image specification fields in png_format, below. */
11394
11395enum png_keyword_index
11396{
11397 PNG_TYPE,
11398 PNG_DATA,
11399 PNG_FILE,
11400 PNG_ASCENT,
11401 PNG_MARGIN,
11402 PNG_RELIEF,
11403 PNG_ALGORITHM,
11404 PNG_HEURISTIC_MASK,
a05e2bae
JR
11405 PNG_MASK,
11406 PNG_BACKGROUND,
6fc2811b
JR
11407 PNG_LAST
11408};
11409
11410/* Vector of image_keyword structures describing the format
11411 of valid user-defined image specifications. */
11412
11413static struct image_keyword png_format[PNG_LAST] =
11414{
11415 {":type", IMAGE_SYMBOL_VALUE, 1},
11416 {":data", IMAGE_STRING_VALUE, 0},
11417 {":file", IMAGE_STRING_VALUE, 0},
11418 {":ascent", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
8edb0a6f 11419 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
6fc2811b 11420 {":relief", IMAGE_INTEGER_VALUE, 0},
a93f4566 11421 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
a05e2bae
JR
11422 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
11423 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
11424 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
6fc2811b
JR
11425};
11426
11427/* Structure describing the image type `png'. */
11428
11429static struct image_type png_type =
11430{
11431 &Qpng,
11432 png_image_p,
11433 png_load,
11434 x_clear_image,
11435 NULL
11436};
11437
11438
11439/* Return non-zero if OBJECT is a valid PNG image specification. */
11440
11441static int
11442png_image_p (object)
11443 Lisp_Object object;
11444{
11445 struct image_keyword fmt[PNG_LAST];
11446 bcopy (png_format, fmt, sizeof fmt);
11447
11448 if (!parse_image_spec (object, fmt, PNG_LAST, Qpng)
11449 || (fmt[PNG_ASCENT].count
11450 && XFASTINT (fmt[PNG_ASCENT].value) > 100))
11451 return 0;
11452
11453 /* Must specify either the :data or :file keyword. */
11454 return fmt[PNG_FILE].count + fmt[PNG_DATA].count == 1;
11455}
11456
11457
11458/* Error and warning handlers installed when the PNG library
11459 is initialized. */
11460
11461static void
11462my_png_error (png_ptr, msg)
11463 png_struct *png_ptr;
11464 char *msg;
11465{
11466 xassert (png_ptr != NULL);
11467 image_error ("PNG error: %s", build_string (msg), Qnil);
11468 longjmp (png_ptr->jmpbuf, 1);
11469}
11470
11471
11472static void
11473my_png_warning (png_ptr, msg)
11474 png_struct *png_ptr;
11475 char *msg;
11476{
11477 xassert (png_ptr != NULL);
11478 image_error ("PNG warning: %s", build_string (msg), Qnil);
11479}
11480
6fc2811b
JR
11481/* Memory source for PNG decoding. */
11482
11483struct png_memory_storage
11484{
11485 unsigned char *bytes; /* The data */
11486 size_t len; /* How big is it? */
11487 int index; /* Where are we? */
11488};
11489
11490
11491/* Function set as reader function when reading PNG image from memory.
11492 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
11493 bytes from the input to DATA. */
11494
11495static void
11496png_read_from_memory (png_ptr, data, length)
11497 png_structp png_ptr;
11498 png_bytep data;
11499 png_size_t length;
11500{
11501 struct png_memory_storage *tbr
11502 = (struct png_memory_storage *) png_get_io_ptr (png_ptr);
11503
11504 if (length > tbr->len - tbr->index)
11505 png_error (png_ptr, "Read error");
11506
11507 bcopy (tbr->bytes + tbr->index, data, length);
11508 tbr->index = tbr->index + length;
11509}
11510
6fc2811b
JR
11511/* Load PNG image IMG for use on frame F. Value is non-zero if
11512 successful. */
11513
11514static int
11515png_load (f, img)
11516 struct frame *f;
11517 struct image *img;
11518{
11519 Lisp_Object file, specified_file;
11520 Lisp_Object specified_data;
11521 int x, y, i;
11522 XImage *ximg, *mask_img = NULL;
11523 struct gcpro gcpro1;
11524 png_struct *png_ptr = NULL;
11525 png_info *info_ptr = NULL, *end_info = NULL;
a05e2bae 11526 FILE *volatile fp = NULL;
6fc2811b 11527 png_byte sig[8];
a05e2bae
JR
11528 png_byte *volatile pixels = NULL;
11529 png_byte **volatile rows = NULL;
6fc2811b
JR
11530 png_uint_32 width, height;
11531 int bit_depth, color_type, interlace_type;
11532 png_byte channels;
11533 png_uint_32 row_bytes;
11534 int transparent_p;
11535 char *gamma_str;
11536 double screen_gamma, image_gamma;
11537 int intent;
11538 struct png_memory_storage tbr; /* Data to be read */
11539
11540 /* Find out what file to load. */
11541 specified_file = image_spec_value (img->spec, QCfile, NULL);
11542 specified_data = image_spec_value (img->spec, QCdata, NULL);
11543 file = Qnil;
11544 GCPRO1 (file);
11545
11546 if (NILP (specified_data))
11547 {
11548 file = x_find_image_file (specified_file);
11549 if (!STRINGP (file))
11550 {
11551 image_error ("Cannot find image file `%s'", specified_file, Qnil);
11552 UNGCPRO;
11553 return 0;
11554 }
11555
11556 /* Open the image file. */
11557 fp = fopen (XSTRING (file)->data, "rb");
11558 if (!fp)
11559 {
11560 image_error ("Cannot open image file `%s'", file, Qnil);
11561 UNGCPRO;
11562 fclose (fp);
11563 return 0;
11564 }
11565
11566 /* Check PNG signature. */
11567 if (fread (sig, 1, sizeof sig, fp) != sizeof sig
11568 || !png_check_sig (sig, sizeof sig))
11569 {
11570 image_error ("Not a PNG file:` %s'", file, Qnil);
11571 UNGCPRO;
11572 fclose (fp);
11573 return 0;
11574 }
11575 }
11576 else
11577 {
11578 /* Read from memory. */
11579 tbr.bytes = XSTRING (specified_data)->data;
11580 tbr.len = STRING_BYTES (XSTRING (specified_data));
11581 tbr.index = 0;
11582
11583 /* Check PNG signature. */
11584 if (tbr.len < sizeof sig
11585 || !png_check_sig (tbr.bytes, sizeof sig))
11586 {
11587 image_error ("Not a PNG image: `%s'", img->spec, Qnil);
11588 UNGCPRO;
11589 return 0;
11590 }
11591
11592 /* Need to skip past the signature. */
11593 tbr.bytes += sizeof (sig);
11594 }
11595
6fc2811b
JR
11596 /* Initialize read and info structs for PNG lib. */
11597 png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL,
11598 my_png_error, my_png_warning);
11599 if (!png_ptr)
11600 {
11601 if (fp) fclose (fp);
11602 UNGCPRO;
11603 return 0;
11604 }
11605
11606 info_ptr = png_create_info_struct (png_ptr);
11607 if (!info_ptr)
11608 {
11609 png_destroy_read_struct (&png_ptr, NULL, NULL);
11610 if (fp) fclose (fp);
11611 UNGCPRO;
11612 return 0;
11613 }
11614
11615 end_info = png_create_info_struct (png_ptr);
11616 if (!end_info)
11617 {
11618 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
11619 if (fp) fclose (fp);
11620 UNGCPRO;
11621 return 0;
11622 }
11623
11624 /* Set error jump-back. We come back here when the PNG library
11625 detects an error. */
11626 if (setjmp (png_ptr->jmpbuf))
11627 {
11628 error:
11629 if (png_ptr)
11630 png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
11631 xfree (pixels);
11632 xfree (rows);
11633 if (fp) fclose (fp);
11634 UNGCPRO;
11635 return 0;
11636 }
11637
11638 /* Read image info. */
11639 if (!NILP (specified_data))
11640 png_set_read_fn (png_ptr, (void *) &tbr, png_read_from_memory);
11641 else
11642 png_init_io (png_ptr, fp);
11643
11644 png_set_sig_bytes (png_ptr, sizeof sig);
11645 png_read_info (png_ptr, info_ptr);
11646 png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
11647 &interlace_type, NULL, NULL);
11648
11649 /* If image contains simply transparency data, we prefer to
11650 construct a clipping mask. */
11651 if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
11652 transparent_p = 1;
11653 else
11654 transparent_p = 0;
11655
11656 /* This function is easier to write if we only have to handle
11657 one data format: RGB or RGBA with 8 bits per channel. Let's
11658 transform other formats into that format. */
11659
11660 /* Strip more than 8 bits per channel. */
11661 if (bit_depth == 16)
11662 png_set_strip_16 (png_ptr);
11663
11664 /* Expand data to 24 bit RGB, or 8 bit grayscale, with alpha channel
11665 if available. */
11666 png_set_expand (png_ptr);
11667
11668 /* Convert grayscale images to RGB. */
11669 if (color_type == PNG_COLOR_TYPE_GRAY
11670 || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
11671 png_set_gray_to_rgb (png_ptr);
11672
11673 /* The value 2.2 is a guess for PC monitors from PNG example.c. */
11674 gamma_str = getenv ("SCREEN_GAMMA");
11675 screen_gamma = gamma_str ? atof (gamma_str) : 2.2;
11676
11677 /* Tell the PNG lib to handle gamma correction for us. */
11678
11679#if defined(PNG_READ_sRGB_SUPPORTED) || defined(PNG_WRITE_sRGB_SUPPORTED)
11680 if (png_get_sRGB (png_ptr, info_ptr, &intent))
11681 /* There is a special chunk in the image specifying the gamma. */
11682 png_set_sRGB (png_ptr, info_ptr, intent);
11683 else
11684#endif
11685 if (png_get_gAMA (png_ptr, info_ptr, &image_gamma))
11686 /* Image contains gamma information. */
11687 png_set_gamma (png_ptr, screen_gamma, image_gamma);
11688 else
11689 /* Use a default of 0.5 for the image gamma. */
11690 png_set_gamma (png_ptr, screen_gamma, 0.5);
11691
11692 /* Handle alpha channel by combining the image with a background
11693 color. Do this only if a real alpha channel is supplied. For
11694 simple transparency, we prefer a clipping mask. */
11695 if (!transparent_p)
11696 {
11697 png_color_16 *image_background;
a05e2bae
JR
11698 Lisp_Object specified_bg
11699 = image_spec_value (img->spec, QCbackground, NULL);
11700
11701
11702 if (STRINGP (specified_bg))
11703 /* The user specified `:background', use that. */
11704 {
11705 COLORREF color;
11706 if (w32_defined_color (f, XSTRING (specified_bg)->data, &color, 0))
11707 {
11708 png_color_16 user_bg;
11709
11710 bzero (&user_bg, sizeof user_bg);
11711 user_bg.red = color.red;
11712 user_bg.green = color.green;
11713 user_bg.blue = color.blue;
6fc2811b 11714
a05e2bae
JR
11715 png_set_background (png_ptr, &user_bg,
11716 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
11717 }
11718 }
11719 else if (png_get_bKGD (png_ptr, info_ptr, &image_background))
6fc2811b
JR
11720 /* Image contains a background color with which to
11721 combine the image. */
11722 png_set_background (png_ptr, image_background,
11723 PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
11724 else
11725 {
11726 /* Image does not contain a background color with which
11727 to combine the image data via an alpha channel. Use
11728 the frame's background instead. */
11729 XColor color;
11730 Colormap cmap;
11731 png_color_16 frame_background;
11732
a05e2bae 11733 cmap = FRAME_X_COLORMAP (f);
6fc2811b 11734 color.pixel = FRAME_BACKGROUND_PIXEL (f);
a05e2bae 11735 x_query_color (f, &color);
6fc2811b
JR
11736
11737 bzero (&frame_background, sizeof frame_background);
11738 frame_background.red = color.red;
11739 frame_background.green = color.green;
11740 frame_background.blue = color.blue;
11741
11742 png_set_background (png_ptr, &frame_background,
11743 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
11744 }
11745 }
11746
11747 /* Update info structure. */
11748 png_read_update_info (png_ptr, info_ptr);
11749
11750 /* Get number of channels. Valid values are 1 for grayscale images
11751 and images with a palette, 2 for grayscale images with transparency
11752 information (alpha channel), 3 for RGB images, and 4 for RGB
11753 images with alpha channel, i.e. RGBA. If conversions above were
11754 sufficient we should only have 3 or 4 channels here. */
11755 channels = png_get_channels (png_ptr, info_ptr);
11756 xassert (channels == 3 || channels == 4);
11757
11758 /* Number of bytes needed for one row of the image. */
11759 row_bytes = png_get_rowbytes (png_ptr, info_ptr);
11760
11761 /* Allocate memory for the image. */
11762 pixels = (png_byte *) xmalloc (row_bytes * height * sizeof *pixels);
11763 rows = (png_byte **) xmalloc (height * sizeof *rows);
11764 for (i = 0; i < height; ++i)
11765 rows[i] = pixels + i * row_bytes;
11766
11767 /* Read the entire image. */
11768 png_read_image (png_ptr, rows);
11769 png_read_end (png_ptr, info_ptr);
11770 if (fp)
11771 {
11772 fclose (fp);
11773 fp = NULL;
11774 }
11775
6fc2811b
JR
11776 /* Create the X image and pixmap. */
11777 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg,
11778 &img->pixmap))
a05e2bae 11779 goto error;
6fc2811b
JR
11780
11781 /* Create an image and pixmap serving as mask if the PNG image
11782 contains an alpha channel. */
11783 if (channels == 4
11784 && !transparent_p
11785 && !x_create_x_image_and_pixmap (f, width, height, 1,
11786 &mask_img, &img->mask))
11787 {
11788 x_destroy_x_image (ximg);
11789 XFreePixmap (FRAME_W32_DISPLAY (f), img->pixmap);
11790 img->pixmap = 0;
6fc2811b
JR
11791 goto error;
11792 }
11793
11794 /* Fill the X image and mask from PNG data. */
11795 init_color_table ();
11796
11797 for (y = 0; y < height; ++y)
11798 {
11799 png_byte *p = rows[y];
11800
11801 for (x = 0; x < width; ++x)
11802 {
11803 unsigned r, g, b;
11804
11805 r = *p++ << 8;
11806 g = *p++ << 8;
11807 b = *p++ << 8;
11808 XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b));
11809
11810 /* An alpha channel, aka mask channel, associates variable
11811 transparency with an image. Where other image formats
11812 support binary transparency---fully transparent or fully
11813 opaque---PNG allows up to 254 levels of partial transparency.
11814 The PNG library implements partial transparency by combining
11815 the image with a specified background color.
11816
11817 I'm not sure how to handle this here nicely: because the
11818 background on which the image is displayed may change, for
11819 real alpha channel support, it would be necessary to create
11820 a new image for each possible background.
11821
11822 What I'm doing now is that a mask is created if we have
11823 boolean transparency information. Otherwise I'm using
11824 the frame's background color to combine the image with. */
11825
11826 if (channels == 4)
11827 {
11828 if (mask_img)
11829 XPutPixel (mask_img, x, y, *p > 0);
11830 ++p;
11831 }
11832 }
11833 }
11834
a05e2bae
JR
11835 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
11836 /* Set IMG's background color from the PNG image, unless the user
11837 overrode it. */
11838 {
11839 png_color_16 *bg;
11840 if (png_get_bKGD (png_ptr, info_ptr, &bg))
11841 {
11842 img->background = lookup_rgb_color (f, bg->red, bg->green, bg->blue);
11843 img->background_valid = 1;
11844 }
11845 }
11846
6fc2811b
JR
11847 /* Remember colors allocated for this image. */
11848 img->colors = colors_in_color_table (&img->ncolors);
11849 free_color_table ();
11850
11851 /* Clean up. */
11852 png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
11853 xfree (rows);
11854 xfree (pixels);
11855
11856 img->width = width;
11857 img->height = height;
11858
a05e2bae
JR
11859 /* Maybe fill in the background field while we have ximg handy. */
11860 IMAGE_BACKGROUND (img, f, ximg);
11861
6fc2811b
JR
11862 /* Put the image into the pixmap, then free the X image and its buffer. */
11863 x_put_x_image (f, ximg, img->pixmap, width, height);
11864 x_destroy_x_image (ximg);
11865
11866 /* Same for the mask. */
11867 if (mask_img)
11868 {
a05e2bae
JR
11869 /* Fill in the background_transparent field while we have the mask
11870 handy. */
11871 image_background_transparent (img, f, mask_img);
11872
6fc2811b
JR
11873 x_put_x_image (f, mask_img, img->mask, img->width, img->height);
11874 x_destroy_x_image (mask_img);
11875 }
11876
6fc2811b
JR
11877 UNGCPRO;
11878 return 1;
11879}
11880
11881#endif /* HAVE_PNG != 0 */
11882
11883
11884\f
11885/***********************************************************************
11886 JPEG
11887 ***********************************************************************/
11888
11889#if HAVE_JPEG
11890
11891/* Work around a warning about HAVE_STDLIB_H being redefined in
11892 jconfig.h. */
11893#ifdef HAVE_STDLIB_H
11894#define HAVE_STDLIB_H_1
11895#undef HAVE_STDLIB_H
11896#endif /* HAVE_STLIB_H */
11897
11898#include <jpeglib.h>
11899#include <jerror.h>
11900#include <setjmp.h>
11901
11902#ifdef HAVE_STLIB_H_1
11903#define HAVE_STDLIB_H 1
11904#endif
11905
11906static int jpeg_image_p P_ ((Lisp_Object object));
11907static int jpeg_load P_ ((struct frame *f, struct image *img));
11908
11909/* The symbol `jpeg' identifying images of this type. */
11910
11911Lisp_Object Qjpeg;
11912
11913/* Indices of image specification fields in gs_format, below. */
11914
11915enum jpeg_keyword_index
11916{
11917 JPEG_TYPE,
11918 JPEG_DATA,
11919 JPEG_FILE,
11920 JPEG_ASCENT,
11921 JPEG_MARGIN,
11922 JPEG_RELIEF,
11923 JPEG_ALGORITHM,
11924 JPEG_HEURISTIC_MASK,
a05e2bae
JR
11925 JPEG_MASK,
11926 JPEG_BACKGROUND,
6fc2811b
JR
11927 JPEG_LAST
11928};
11929
11930/* Vector of image_keyword structures describing the format
11931 of valid user-defined image specifications. */
11932
11933static struct image_keyword jpeg_format[JPEG_LAST] =
11934{
11935 {":type", IMAGE_SYMBOL_VALUE, 1},
11936 {":data", IMAGE_STRING_VALUE, 0},
11937 {":file", IMAGE_STRING_VALUE, 0},
11938 {":ascent", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
8edb0a6f 11939 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
6fc2811b 11940 {":relief", IMAGE_INTEGER_VALUE, 0},
a05e2bae
JR
11941 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
11942 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
11943 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
11944 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
6fc2811b
JR
11945};
11946
11947/* Structure describing the image type `jpeg'. */
11948
11949static struct image_type jpeg_type =
11950{
11951 &Qjpeg,
11952 jpeg_image_p,
11953 jpeg_load,
11954 x_clear_image,
11955 NULL
11956};
11957
11958
11959/* Return non-zero if OBJECT is a valid JPEG image specification. */
11960
11961static int
11962jpeg_image_p (object)
11963 Lisp_Object object;
11964{
11965 struct image_keyword fmt[JPEG_LAST];
11966
11967 bcopy (jpeg_format, fmt, sizeof fmt);
11968
11969 if (!parse_image_spec (object, fmt, JPEG_LAST, Qjpeg)
11970 || (fmt[JPEG_ASCENT].count
11971 && XFASTINT (fmt[JPEG_ASCENT].value) > 100))
11972 return 0;
11973
11974 /* Must specify either the :data or :file keyword. */
11975 return fmt[JPEG_FILE].count + fmt[JPEG_DATA].count == 1;
11976}
11977
11978
11979struct my_jpeg_error_mgr
11980{
11981 struct jpeg_error_mgr pub;
11982 jmp_buf setjmp_buffer;
11983};
11984
11985static void
11986my_error_exit (cinfo)
11987 j_common_ptr cinfo;
11988{
11989 struct my_jpeg_error_mgr *mgr = (struct my_jpeg_error_mgr *) cinfo->err;
11990 longjmp (mgr->setjmp_buffer, 1);
11991}
11992
6fc2811b
JR
11993/* Init source method for JPEG data source manager. Called by
11994 jpeg_read_header() before any data is actually read. See
11995 libjpeg.doc from the JPEG lib distribution. */
11996
11997static void
11998our_init_source (cinfo)
11999 j_decompress_ptr cinfo;
12000{
12001}
12002
12003
12004/* Fill input buffer method for JPEG data source manager. Called
12005 whenever more data is needed. We read the whole image in one step,
12006 so this only adds a fake end of input marker at the end. */
12007
12008static boolean
12009our_fill_input_buffer (cinfo)
12010 j_decompress_ptr cinfo;
12011{
12012 /* Insert a fake EOI marker. */
12013 struct jpeg_source_mgr *src = cinfo->src;
12014 static JOCTET buffer[2];
12015
12016 buffer[0] = (JOCTET) 0xFF;
12017 buffer[1] = (JOCTET) JPEG_EOI;
12018
12019 src->next_input_byte = buffer;
12020 src->bytes_in_buffer = 2;
12021 return TRUE;
12022}
12023
12024
12025/* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
12026 is the JPEG data source manager. */
12027
12028static void
12029our_skip_input_data (cinfo, num_bytes)
12030 j_decompress_ptr cinfo;
12031 long num_bytes;
12032{
12033 struct jpeg_source_mgr *src = (struct jpeg_source_mgr *) cinfo->src;
12034
12035 if (src)
12036 {
12037 if (num_bytes > src->bytes_in_buffer)
12038 ERREXIT (cinfo, JERR_INPUT_EOF);
12039
12040 src->bytes_in_buffer -= num_bytes;
12041 src->next_input_byte += num_bytes;
12042 }
12043}
12044
12045
12046/* Method to terminate data source. Called by
12047 jpeg_finish_decompress() after all data has been processed. */
12048
12049static void
12050our_term_source (cinfo)
12051 j_decompress_ptr cinfo;
12052{
12053}
12054
12055
12056/* Set up the JPEG lib for reading an image from DATA which contains
12057 LEN bytes. CINFO is the decompression info structure created for
12058 reading the image. */
12059
12060static void
12061jpeg_memory_src (cinfo, data, len)
12062 j_decompress_ptr cinfo;
12063 JOCTET *data;
12064 unsigned int len;
12065{
12066 struct jpeg_source_mgr *src;
12067
12068 if (cinfo->src == NULL)
12069 {
12070 /* First time for this JPEG object? */
12071 cinfo->src = (struct jpeg_source_mgr *)
12072 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
12073 sizeof (struct jpeg_source_mgr));
12074 src = (struct jpeg_source_mgr *) cinfo->src;
12075 src->next_input_byte = data;
12076 }
12077
12078 src = (struct jpeg_source_mgr *) cinfo->src;
12079 src->init_source = our_init_source;
12080 src->fill_input_buffer = our_fill_input_buffer;
12081 src->skip_input_data = our_skip_input_data;
12082 src->resync_to_restart = jpeg_resync_to_restart; /* Use default method. */
12083 src->term_source = our_term_source;
12084 src->bytes_in_buffer = len;
12085 src->next_input_byte = data;
12086}
12087
12088
12089/* Load image IMG for use on frame F. Patterned after example.c
12090 from the JPEG lib. */
12091
12092static int
12093jpeg_load (f, img)
12094 struct frame *f;
12095 struct image *img;
12096{
12097 struct jpeg_decompress_struct cinfo;
12098 struct my_jpeg_error_mgr mgr;
12099 Lisp_Object file, specified_file;
12100 Lisp_Object specified_data;
a05e2bae 12101 FILE * volatile fp = NULL;
6fc2811b
JR
12102 JSAMPARRAY buffer;
12103 int row_stride, x, y;
12104 XImage *ximg = NULL;
12105 int rc;
12106 unsigned long *colors;
12107 int width, height;
12108 struct gcpro gcpro1;
12109
12110 /* Open the JPEG file. */
12111 specified_file = image_spec_value (img->spec, QCfile, NULL);
12112 specified_data = image_spec_value (img->spec, QCdata, NULL);
12113 file = Qnil;
12114 GCPRO1 (file);
12115
6fc2811b
JR
12116 if (NILP (specified_data))
12117 {
12118 file = x_find_image_file (specified_file);
12119 if (!STRINGP (file))
12120 {
12121 image_error ("Cannot find image file `%s'", specified_file, Qnil);
12122 UNGCPRO;
12123 return 0;
12124 }
12125
12126 fp = fopen (XSTRING (file)->data, "r");
12127 if (fp == NULL)
12128 {
12129 image_error ("Cannot open `%s'", file, Qnil);
12130 UNGCPRO;
12131 return 0;
12132 }
12133 }
12134
12135 /* Customize libjpeg's error handling to call my_error_exit when an
12136 error is detected. This function will perform a longjmp. */
6fc2811b 12137 cinfo.err = jpeg_std_error (&mgr.pub);
a05e2bae 12138 mgr.pub.error_exit = my_error_exit;
6fc2811b
JR
12139
12140 if ((rc = setjmp (mgr.setjmp_buffer)) != 0)
12141 {
12142 if (rc == 1)
12143 {
12144 /* Called from my_error_exit. Display a JPEG error. */
12145 char buffer[JMSG_LENGTH_MAX];
12146 cinfo.err->format_message ((j_common_ptr) &cinfo, buffer);
12147 image_error ("Error reading JPEG image `%s': %s", img->spec,
12148 build_string (buffer));
12149 }
12150
12151 /* Close the input file and destroy the JPEG object. */
12152 if (fp)
12153 fclose (fp);
12154 jpeg_destroy_decompress (&cinfo);
6fc2811b
JR
12155
12156 /* If we already have an XImage, free that. */
12157 x_destroy_x_image (ximg);
12158
12159 /* Free pixmap and colors. */
12160 x_clear_image (f, img);
12161
6fc2811b
JR
12162 UNGCPRO;
12163 return 0;
12164 }
12165
12166 /* Create the JPEG decompression object. Let it read from fp.
12167 Read the JPEG image header. */
12168 jpeg_create_decompress (&cinfo);
12169
12170 if (NILP (specified_data))
12171 jpeg_stdio_src (&cinfo, fp);
12172 else
12173 jpeg_memory_src (&cinfo, XSTRING (specified_data)->data,
12174 STRING_BYTES (XSTRING (specified_data)));
12175
12176 jpeg_read_header (&cinfo, TRUE);
12177
12178 /* Customize decompression so that color quantization will be used.
12179 Start decompression. */
12180 cinfo.quantize_colors = TRUE;
12181 jpeg_start_decompress (&cinfo);
12182 width = img->width = cinfo.output_width;
12183 height = img->height = cinfo.output_height;
12184
6fc2811b
JR
12185 /* Create X image and pixmap. */
12186 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg,
12187 &img->pixmap))
a05e2bae 12188 longjmp (mgr.setjmp_buffer, 2);
6fc2811b
JR
12189
12190 /* Allocate colors. When color quantization is used,
12191 cinfo.actual_number_of_colors has been set with the number of
12192 colors generated, and cinfo.colormap is a two-dimensional array
12193 of color indices in the range 0..cinfo.actual_number_of_colors.
12194 No more than 255 colors will be generated. */
12195 {
12196 int i, ir, ig, ib;
12197
12198 if (cinfo.out_color_components > 2)
12199 ir = 0, ig = 1, ib = 2;
12200 else if (cinfo.out_color_components > 1)
12201 ir = 0, ig = 1, ib = 0;
12202 else
12203 ir = 0, ig = 0, ib = 0;
12204
12205 /* Use the color table mechanism because it handles colors that
12206 cannot be allocated nicely. Such colors will be replaced with
12207 a default color, and we don't have to care about which colors
12208 can be freed safely, and which can't. */
12209 init_color_table ();
12210 colors = (unsigned long *) alloca (cinfo.actual_number_of_colors
12211 * sizeof *colors);
12212
12213 for (i = 0; i < cinfo.actual_number_of_colors; ++i)
12214 {
12215 /* Multiply RGB values with 255 because X expects RGB values
12216 in the range 0..0xffff. */
12217 int r = cinfo.colormap[ir][i] << 8;
12218 int g = cinfo.colormap[ig][i] << 8;
12219 int b = cinfo.colormap[ib][i] << 8;
12220 colors[i] = lookup_rgb_color (f, r, g, b);
12221 }
12222
12223 /* Remember those colors actually allocated. */
12224 img->colors = colors_in_color_table (&img->ncolors);
12225 free_color_table ();
12226 }
12227
12228 /* Read pixels. */
12229 row_stride = width * cinfo.output_components;
12230 buffer = cinfo.mem->alloc_sarray ((j_common_ptr) &cinfo, JPOOL_IMAGE,
12231 row_stride, 1);
12232 for (y = 0; y < height; ++y)
12233 {
12234 jpeg_read_scanlines (&cinfo, buffer, 1);
12235 for (x = 0; x < cinfo.output_width; ++x)
12236 XPutPixel (ximg, x, y, colors[buffer[0][x]]);
12237 }
12238
12239 /* Clean up. */
12240 jpeg_finish_decompress (&cinfo);
12241 jpeg_destroy_decompress (&cinfo);
12242 if (fp)
12243 fclose (fp);
12244
a05e2bae
JR
12245 /* Maybe fill in the background field while we have ximg handy. */
12246 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
12247 IMAGE_BACKGROUND (img, f, ximg);
12248
6fc2811b
JR
12249 /* Put the image into the pixmap. */
12250 x_put_x_image (f, ximg, img->pixmap, width, height);
12251 x_destroy_x_image (ximg);
12252 UNBLOCK_INPUT;
12253 UNGCPRO;
12254 return 1;
12255}
12256
12257#endif /* HAVE_JPEG */
12258
12259
12260\f
12261/***********************************************************************
12262 TIFF
12263 ***********************************************************************/
12264
12265#if HAVE_TIFF
12266
12267#include <tiffio.h>
12268
12269static int tiff_image_p P_ ((Lisp_Object object));
12270static int tiff_load P_ ((struct frame *f, struct image *img));
12271
12272/* The symbol `tiff' identifying images of this type. */
12273
12274Lisp_Object Qtiff;
12275
12276/* Indices of image specification fields in tiff_format, below. */
12277
12278enum tiff_keyword_index
12279{
12280 TIFF_TYPE,
12281 TIFF_DATA,
12282 TIFF_FILE,
12283 TIFF_ASCENT,
12284 TIFF_MARGIN,
12285 TIFF_RELIEF,
12286 TIFF_ALGORITHM,
12287 TIFF_HEURISTIC_MASK,
a05e2bae
JR
12288 TIFF_MASK,
12289 TIFF_BACKGROUND,
6fc2811b
JR
12290 TIFF_LAST
12291};
12292
12293/* Vector of image_keyword structures describing the format
12294 of valid user-defined image specifications. */
12295
12296static struct image_keyword tiff_format[TIFF_LAST] =
12297{
12298 {":type", IMAGE_SYMBOL_VALUE, 1},
12299 {":data", IMAGE_STRING_VALUE, 0},
12300 {":file", IMAGE_STRING_VALUE, 0},
12301 {":ascent", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
8edb0a6f 12302 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
6fc2811b 12303 {":relief", IMAGE_INTEGER_VALUE, 0},
a05e2bae
JR
12304 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
12305 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
12306 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
12307 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
6fc2811b
JR
12308};
12309
12310/* Structure describing the image type `tiff'. */
12311
12312static struct image_type tiff_type =
12313{
12314 &Qtiff,
12315 tiff_image_p,
12316 tiff_load,
12317 x_clear_image,
12318 NULL
12319};
12320
12321
12322/* Return non-zero if OBJECT is a valid TIFF image specification. */
12323
12324static int
12325tiff_image_p (object)
12326 Lisp_Object object;
12327{
12328 struct image_keyword fmt[TIFF_LAST];
12329 bcopy (tiff_format, fmt, sizeof fmt);
12330
12331 if (!parse_image_spec (object, fmt, TIFF_LAST, Qtiff)
12332 || (fmt[TIFF_ASCENT].count
12333 && XFASTINT (fmt[TIFF_ASCENT].value) > 100))
12334 return 0;
12335
12336 /* Must specify either the :data or :file keyword. */
12337 return fmt[TIFF_FILE].count + fmt[TIFF_DATA].count == 1;
12338}
12339
12340
12341/* Reading from a memory buffer for TIFF images Based on the PNG
12342 memory source, but we have to provide a lot of extra functions.
12343 Blah.
12344
12345 We really only need to implement read and seek, but I am not
12346 convinced that the TIFF library is smart enough not to destroy
12347 itself if we only hand it the function pointers we need to
12348 override. */
12349
12350typedef struct
12351{
12352 unsigned char *bytes;
12353 size_t len;
12354 int index;
12355}
12356tiff_memory_source;
12357
12358static size_t
12359tiff_read_from_memory (data, buf, size)
12360 thandle_t data;
12361 tdata_t buf;
12362 tsize_t size;
12363{
12364 tiff_memory_source *src = (tiff_memory_source *) data;
12365
12366 if (size > src->len - src->index)
12367 return (size_t) -1;
12368 bcopy (src->bytes + src->index, buf, size);
12369 src->index += size;
12370 return size;
12371}
12372
12373static size_t
12374tiff_write_from_memory (data, buf, size)
12375 thandle_t data;
12376 tdata_t buf;
12377 tsize_t size;
12378{
12379 return (size_t) -1;
12380}
12381
12382static toff_t
12383tiff_seek_in_memory (data, off, whence)
12384 thandle_t data;
12385 toff_t off;
12386 int whence;
12387{
12388 tiff_memory_source *src = (tiff_memory_source *) data;
12389 int idx;
12390
12391 switch (whence)
12392 {
12393 case SEEK_SET: /* Go from beginning of source. */
12394 idx = off;
12395 break;
12396
12397 case SEEK_END: /* Go from end of source. */
12398 idx = src->len + off;
12399 break;
12400
12401 case SEEK_CUR: /* Go from current position. */
12402 idx = src->index + off;
12403 break;
12404
12405 default: /* Invalid `whence'. */
12406 return -1;
12407 }
12408
12409 if (idx > src->len || idx < 0)
12410 return -1;
12411
12412 src->index = idx;
12413 return src->index;
12414}
12415
12416static int
12417tiff_close_memory (data)
12418 thandle_t data;
12419{
12420 /* NOOP */
12421 return 0;
12422}
12423
12424static int
12425tiff_mmap_memory (data, pbase, psize)
12426 thandle_t data;
12427 tdata_t *pbase;
12428 toff_t *psize;
12429{
12430 /* It is already _IN_ memory. */
12431 return 0;
12432}
12433
12434static void
12435tiff_unmap_memory (data, base, size)
12436 thandle_t data;
12437 tdata_t base;
12438 toff_t size;
12439{
12440 /* We don't need to do this. */
12441}
12442
12443static toff_t
12444tiff_size_of_memory (data)
12445 thandle_t data;
12446{
12447 return ((tiff_memory_source *) data)->len;
12448}
12449
3cf3436e
JR
12450
12451static void
12452tiff_error_handler (title, format, ap)
12453 const char *title, *format;
12454 va_list ap;
12455{
12456 char buf[512];
12457 int len;
12458
12459 len = sprintf (buf, "TIFF error: %s ", title);
12460 vsprintf (buf + len, format, ap);
12461 add_to_log (buf, Qnil, Qnil);
12462}
12463
12464
12465static void
12466tiff_warning_handler (title, format, ap)
12467 const char *title, *format;
12468 va_list ap;
12469{
12470 char buf[512];
12471 int len;
12472
12473 len = sprintf (buf, "TIFF warning: %s ", title);
12474 vsprintf (buf + len, format, ap);
12475 add_to_log (buf, Qnil, Qnil);
12476}
12477
12478
6fc2811b
JR
12479/* Load TIFF image IMG for use on frame F. Value is non-zero if
12480 successful. */
12481
12482static int
12483tiff_load (f, img)
12484 struct frame *f;
12485 struct image *img;
12486{
12487 Lisp_Object file, specified_file;
12488 Lisp_Object specified_data;
12489 TIFF *tiff;
12490 int width, height, x, y;
12491 uint32 *buf;
12492 int rc;
12493 XImage *ximg;
12494 struct gcpro gcpro1;
12495 tiff_memory_source memsrc;
12496
12497 specified_file = image_spec_value (img->spec, QCfile, NULL);
12498 specified_data = image_spec_value (img->spec, QCdata, NULL);
12499 file = Qnil;
12500 GCPRO1 (file);
12501
3cf3436e
JR
12502 TIFFSetErrorHandler (tiff_error_handler);
12503 TIFFSetWarningHandler (tiff_warning_handler);
12504
6fc2811b
JR
12505 if (NILP (specified_data))
12506 {
12507 /* Read from a file */
12508 file = x_find_image_file (specified_file);
12509 if (!STRINGP (file))
3cf3436e
JR
12510 {
12511 image_error ("Cannot find image file `%s'", file, Qnil);
12512 UNGCPRO;
12513 return 0;
12514 }
12515
6fc2811b
JR
12516 /* Try to open the image file. */
12517 tiff = TIFFOpen (XSTRING (file)->data, "r");
12518 if (tiff == NULL)
3cf3436e
JR
12519 {
12520 image_error ("Cannot open `%s'", file, Qnil);
12521 UNGCPRO;
12522 return 0;
12523 }
6fc2811b
JR
12524 }
12525 else
12526 {
12527 /* Memory source! */
12528 memsrc.bytes = XSTRING (specified_data)->data;
12529 memsrc.len = STRING_BYTES (XSTRING (specified_data));
12530 memsrc.index = 0;
12531
12532 tiff = TIFFClientOpen ("memory_source", "r", &memsrc,
12533 (TIFFReadWriteProc) tiff_read_from_memory,
12534 (TIFFReadWriteProc) tiff_write_from_memory,
12535 tiff_seek_in_memory,
12536 tiff_close_memory,
12537 tiff_size_of_memory,
12538 tiff_mmap_memory,
12539 tiff_unmap_memory);
12540
12541 if (!tiff)
12542 {
12543 image_error ("Cannot open memory source for `%s'", img->spec, Qnil);
12544 UNGCPRO;
12545 return 0;
12546 }
12547 }
12548
12549 /* Get width and height of the image, and allocate a raster buffer
12550 of width x height 32-bit values. */
12551 TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &width);
12552 TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &height);
12553 buf = (uint32 *) xmalloc (width * height * sizeof *buf);
12554
12555 rc = TIFFReadRGBAImage (tiff, width, height, buf, 0);
12556 TIFFClose (tiff);
12557 if (!rc)
12558 {
12559 image_error ("Error reading TIFF image `%s'", img->spec, Qnil);
12560 xfree (buf);
12561 UNGCPRO;
12562 return 0;
12563 }
12564
6fc2811b
JR
12565 /* Create the X image and pixmap. */
12566 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
12567 {
6fc2811b
JR
12568 xfree (buf);
12569 UNGCPRO;
12570 return 0;
12571 }
12572
12573 /* Initialize the color table. */
12574 init_color_table ();
12575
12576 /* Process the pixel raster. Origin is in the lower-left corner. */
12577 for (y = 0; y < height; ++y)
12578 {
12579 uint32 *row = buf + y * width;
12580
12581 for (x = 0; x < width; ++x)
12582 {
12583 uint32 abgr = row[x];
12584 int r = TIFFGetR (abgr) << 8;
12585 int g = TIFFGetG (abgr) << 8;
12586 int b = TIFFGetB (abgr) << 8;
12587 XPutPixel (ximg, x, height - 1 - y, lookup_rgb_color (f, r, g, b));
12588 }
12589 }
12590
12591 /* Remember the colors allocated for the image. Free the color table. */
12592 img->colors = colors_in_color_table (&img->ncolors);
12593 free_color_table ();
12594
a05e2bae
JR
12595 img->width = width;
12596 img->height = height;
12597
12598 /* Maybe fill in the background field while we have ximg handy. */
12599 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
12600 IMAGE_BACKGROUND (img, f, ximg);
12601
6fc2811b
JR
12602 /* Put the image into the pixmap, then free the X image and its buffer. */
12603 x_put_x_image (f, ximg, img->pixmap, width, height);
12604 x_destroy_x_image (ximg);
12605 xfree (buf);
6fc2811b
JR
12606
12607 UNGCPRO;
12608 return 1;
12609}
12610
12611#endif /* HAVE_TIFF != 0 */
12612
12613
12614\f
12615/***********************************************************************
12616 GIF
12617 ***********************************************************************/
12618
12619#if HAVE_GIF
12620
12621#include <gif_lib.h>
12622
12623static int gif_image_p P_ ((Lisp_Object object));
12624static int gif_load P_ ((struct frame *f, struct image *img));
12625
12626/* The symbol `gif' identifying images of this type. */
12627
12628Lisp_Object Qgif;
12629
12630/* Indices of image specification fields in gif_format, below. */
12631
12632enum gif_keyword_index
12633{
12634 GIF_TYPE,
12635 GIF_DATA,
12636 GIF_FILE,
12637 GIF_ASCENT,
12638 GIF_MARGIN,
12639 GIF_RELIEF,
12640 GIF_ALGORITHM,
12641 GIF_HEURISTIC_MASK,
a05e2bae 12642 GIF_MASK,
6fc2811b 12643 GIF_IMAGE,
a05e2bae 12644 GIF_BACKGROUND,
6fc2811b
JR
12645 GIF_LAST
12646};
12647
12648/* Vector of image_keyword structures describing the format
12649 of valid user-defined image specifications. */
12650
12651static struct image_keyword gif_format[GIF_LAST] =
12652{
12653 {":type", IMAGE_SYMBOL_VALUE, 1},
12654 {":data", IMAGE_STRING_VALUE, 0},
12655 {":file", IMAGE_STRING_VALUE, 0},
12656 {":ascent", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
8edb0a6f 12657 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
6fc2811b 12658 {":relief", IMAGE_INTEGER_VALUE, 0},
a93f4566 12659 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6fc2811b 12660 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
a05e2bae
JR
12661 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
12662 {":image", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
12663 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
6fc2811b
JR
12664};
12665
12666/* Structure describing the image type `gif'. */
12667
12668static struct image_type gif_type =
12669{
12670 &Qgif,
12671 gif_image_p,
12672 gif_load,
12673 x_clear_image,
12674 NULL
12675};
12676
12677/* Return non-zero if OBJECT is a valid GIF image specification. */
12678
12679static int
12680gif_image_p (object)
12681 Lisp_Object object;
12682{
12683 struct image_keyword fmt[GIF_LAST];
12684 bcopy (gif_format, fmt, sizeof fmt);
12685
12686 if (!parse_image_spec (object, fmt, GIF_LAST, Qgif)
12687 || (fmt[GIF_ASCENT].count
12688 && XFASTINT (fmt[GIF_ASCENT].value) > 100))
12689 return 0;
12690
12691 /* Must specify either the :data or :file keyword. */
12692 return fmt[GIF_FILE].count + fmt[GIF_DATA].count == 1;
12693}
12694
12695/* Reading a GIF image from memory
12696 Based on the PNG memory stuff to a certain extent. */
12697
12698typedef struct
12699{
12700 unsigned char *bytes;
12701 size_t len;
12702 int index;
12703}
12704gif_memory_source;
12705
12706/* Make the current memory source available to gif_read_from_memory.
12707 It's done this way because not all versions of libungif support
12708 a UserData field in the GifFileType structure. */
12709static gif_memory_source *current_gif_memory_src;
12710
12711static int
12712gif_read_from_memory (file, buf, len)
12713 GifFileType *file;
12714 GifByteType *buf;
12715 int len;
12716{
12717 gif_memory_source *src = current_gif_memory_src;
12718
12719 if (len > src->len - src->index)
12720 return -1;
12721
12722 bcopy (src->bytes + src->index, buf, len);
12723 src->index += len;
12724 return len;
12725}
12726
12727
12728/* Load GIF image IMG for use on frame F. Value is non-zero if
12729 successful. */
12730
12731static int
12732gif_load (f, img)
12733 struct frame *f;
12734 struct image *img;
12735{
12736 Lisp_Object file, specified_file;
12737 Lisp_Object specified_data;
12738 int rc, width, height, x, y, i;
12739 XImage *ximg;
12740 ColorMapObject *gif_color_map;
12741 unsigned long pixel_colors[256];
12742 GifFileType *gif;
12743 struct gcpro gcpro1;
12744 Lisp_Object image;
12745 int ino, image_left, image_top, image_width, image_height;
12746 gif_memory_source memsrc;
12747 unsigned char *raster;
12748
12749 specified_file = image_spec_value (img->spec, QCfile, NULL);
12750 specified_data = image_spec_value (img->spec, QCdata, NULL);
12751 file = Qnil;
dfff8a69 12752 GCPRO1 (file);
6fc2811b
JR
12753
12754 if (NILP (specified_data))
12755 {
12756 file = x_find_image_file (specified_file);
6fc2811b
JR
12757 if (!STRINGP (file))
12758 {
12759 image_error ("Cannot find image file `%s'", specified_file, Qnil);
12760 UNGCPRO;
12761 return 0;
12762 }
12763
12764 /* Open the GIF file. */
12765 gif = DGifOpenFileName (XSTRING (file)->data);
12766 if (gif == NULL)
12767 {
12768 image_error ("Cannot open `%s'", file, Qnil);
12769 UNGCPRO;
12770 return 0;
12771 }
12772 }
12773 else
12774 {
12775 /* Read from memory! */
12776 current_gif_memory_src = &memsrc;
12777 memsrc.bytes = XSTRING (specified_data)->data;
12778 memsrc.len = STRING_BYTES (XSTRING (specified_data));
12779 memsrc.index = 0;
12780
12781 gif = DGifOpen(&memsrc, gif_read_from_memory);
12782 if (!gif)
12783 {
12784 image_error ("Cannot open memory source `%s'", img->spec, Qnil);
12785 UNGCPRO;
12786 return 0;
12787 }
12788 }
12789
12790 /* Read entire contents. */
12791 rc = DGifSlurp (gif);
12792 if (rc == GIF_ERROR)
12793 {
12794 image_error ("Error reading `%s'", img->spec, Qnil);
12795 DGifCloseFile (gif);
12796 UNGCPRO;
12797 return 0;
12798 }
12799
12800 image = image_spec_value (img->spec, QCindex, NULL);
12801 ino = INTEGERP (image) ? XFASTINT (image) : 0;
12802 if (ino >= gif->ImageCount)
12803 {
12804 image_error ("Invalid image number `%s' in image `%s'",
12805 image, img->spec);
12806 DGifCloseFile (gif);
12807 UNGCPRO;
12808 return 0;
12809 }
12810
12811 width = img->width = gif->SWidth;
12812 height = img->height = gif->SHeight;
12813
6fc2811b
JR
12814 /* Create the X image and pixmap. */
12815 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
12816 {
6fc2811b
JR
12817 DGifCloseFile (gif);
12818 UNGCPRO;
12819 return 0;
12820 }
12821
12822 /* Allocate colors. */
12823 gif_color_map = gif->SavedImages[ino].ImageDesc.ColorMap;
12824 if (!gif_color_map)
12825 gif_color_map = gif->SColorMap;
12826 init_color_table ();
12827 bzero (pixel_colors, sizeof pixel_colors);
12828
12829 for (i = 0; i < gif_color_map->ColorCount; ++i)
12830 {
12831 int r = gif_color_map->Colors[i].Red << 8;
12832 int g = gif_color_map->Colors[i].Green << 8;
12833 int b = gif_color_map->Colors[i].Blue << 8;
12834 pixel_colors[i] = lookup_rgb_color (f, r, g, b);
12835 }
12836
12837 img->colors = colors_in_color_table (&img->ncolors);
12838 free_color_table ();
12839
12840 /* Clear the part of the screen image that are not covered by
12841 the image from the GIF file. Full animated GIF support
12842 requires more than can be done here (see the gif89 spec,
12843 disposal methods). Let's simply assume that the part
12844 not covered by a sub-image is in the frame's background color. */
12845 image_top = gif->SavedImages[ino].ImageDesc.Top;
12846 image_left = gif->SavedImages[ino].ImageDesc.Left;
12847 image_width = gif->SavedImages[ino].ImageDesc.Width;
12848 image_height = gif->SavedImages[ino].ImageDesc.Height;
12849
12850 for (y = 0; y < image_top; ++y)
12851 for (x = 0; x < width; ++x)
12852 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
12853
12854 for (y = image_top + image_height; y < height; ++y)
12855 for (x = 0; x < width; ++x)
12856 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
12857
12858 for (y = image_top; y < image_top + image_height; ++y)
12859 {
12860 for (x = 0; x < image_left; ++x)
12861 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
12862 for (x = image_left + image_width; x < width; ++x)
12863 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
12864 }
12865
12866 /* Read the GIF image into the X image. We use a local variable
12867 `raster' here because RasterBits below is a char *, and invites
12868 problems with bytes >= 0x80. */
12869 raster = (unsigned char *) gif->SavedImages[ino].RasterBits;
12870
12871 if (gif->SavedImages[ino].ImageDesc.Interlace)
12872 {
12873 static int interlace_start[] = {0, 4, 2, 1};
12874 static int interlace_increment[] = {8, 8, 4, 2};
a05e2bae 12875 int pass;
6fc2811b
JR
12876 int row = interlace_start[0];
12877
12878 pass = 0;
12879
12880 for (y = 0; y < image_height; y++)
12881 {
12882 if (row >= image_height)
12883 {
12884 row = interlace_start[++pass];
12885 while (row >= image_height)
12886 row = interlace_start[++pass];
12887 }
12888
12889 for (x = 0; x < image_width; x++)
12890 {
12891 int i = raster[(y * image_width) + x];
12892 XPutPixel (ximg, x + image_left, row + image_top,
12893 pixel_colors[i]);
12894 }
12895
12896 row += interlace_increment[pass];
12897 }
12898 }
12899 else
12900 {
12901 for (y = 0; y < image_height; ++y)
12902 for (x = 0; x < image_width; ++x)
12903 {
12904 int i = raster[y* image_width + x];
12905 XPutPixel (ximg, x + image_left, y + image_top, pixel_colors[i]);
12906 }
12907 }
12908
12909 DGifCloseFile (gif);
a05e2bae
JR
12910
12911 /* Maybe fill in the background field while we have ximg handy. */
12912 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
12913 IMAGE_BACKGROUND (img, f, ximg);
12914
6fc2811b
JR
12915 /* Put the image into the pixmap, then free the X image and its buffer. */
12916 x_put_x_image (f, ximg, img->pixmap, width, height);
12917 x_destroy_x_image (ximg);
6fc2811b
JR
12918
12919 UNGCPRO;
12920 return 1;
12921}
12922
12923#endif /* HAVE_GIF != 0 */
12924
12925
12926\f
12927/***********************************************************************
12928 Ghostscript
12929 ***********************************************************************/
12930
3cf3436e
JR
12931Lisp_Object Qpostscript;
12932
6fc2811b
JR
12933#ifdef HAVE_GHOSTSCRIPT
12934static int gs_image_p P_ ((Lisp_Object object));
12935static int gs_load P_ ((struct frame *f, struct image *img));
12936static void gs_clear_image P_ ((struct frame *f, struct image *img));
12937
12938/* The symbol `postscript' identifying images of this type. */
12939
6fc2811b
JR
12940/* Keyword symbols. */
12941
12942Lisp_Object QCloader, QCbounding_box, QCpt_width, QCpt_height;
12943
12944/* Indices of image specification fields in gs_format, below. */
12945
12946enum gs_keyword_index
12947{
12948 GS_TYPE,
12949 GS_PT_WIDTH,
12950 GS_PT_HEIGHT,
12951 GS_FILE,
12952 GS_LOADER,
12953 GS_BOUNDING_BOX,
12954 GS_ASCENT,
12955 GS_MARGIN,
12956 GS_RELIEF,
12957 GS_ALGORITHM,
12958 GS_HEURISTIC_MASK,
a05e2bae
JR
12959 GS_MASK,
12960 GS_BACKGROUND,
6fc2811b
JR
12961 GS_LAST
12962};
12963
12964/* Vector of image_keyword structures describing the format
12965 of valid user-defined image specifications. */
12966
12967static struct image_keyword gs_format[GS_LAST] =
12968{
12969 {":type", IMAGE_SYMBOL_VALUE, 1},
12970 {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE, 1},
12971 {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE, 1},
12972 {":file", IMAGE_STRING_VALUE, 1},
12973 {":loader", IMAGE_FUNCTION_VALUE, 0},
12974 {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE, 1},
12975 {":ascent", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
8edb0a6f 12976 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
6fc2811b 12977 {":relief", IMAGE_INTEGER_VALUE, 0},
a93f4566 12978 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
a05e2bae
JR
12979 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
12980 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
12981 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
6fc2811b
JR
12982};
12983
12984/* Structure describing the image type `ghostscript'. */
12985
12986static struct image_type gs_type =
12987{
12988 &Qpostscript,
12989 gs_image_p,
12990 gs_load,
12991 gs_clear_image,
12992 NULL
12993};
12994
12995
12996/* Free X resources of Ghostscript image IMG which is used on frame F. */
12997
12998static void
12999gs_clear_image (f, img)
13000 struct frame *f;
13001 struct image *img;
13002{
13003 /* IMG->data.ptr_val may contain a recorded colormap. */
13004 xfree (img->data.ptr_val);
13005 x_clear_image (f, img);
13006}
13007
13008
13009/* Return non-zero if OBJECT is a valid Ghostscript image
13010 specification. */
13011
13012static int
13013gs_image_p (object)
13014 Lisp_Object object;
13015{
13016 struct image_keyword fmt[GS_LAST];
13017 Lisp_Object tem;
13018 int i;
13019
13020 bcopy (gs_format, fmt, sizeof fmt);
13021
13022 if (!parse_image_spec (object, fmt, GS_LAST, Qpostscript)
13023 || (fmt[GS_ASCENT].count
13024 && XFASTINT (fmt[GS_ASCENT].value) > 100))
13025 return 0;
13026
13027 /* Bounding box must be a list or vector containing 4 integers. */
13028 tem = fmt[GS_BOUNDING_BOX].value;
13029 if (CONSP (tem))
13030 {
13031 for (i = 0; i < 4; ++i, tem = XCDR (tem))
13032 if (!CONSP (tem) || !INTEGERP (XCAR (tem)))
13033 return 0;
13034 if (!NILP (tem))
13035 return 0;
13036 }
13037 else if (VECTORP (tem))
13038 {
13039 if (XVECTOR (tem)->size != 4)
13040 return 0;
13041 for (i = 0; i < 4; ++i)
13042 if (!INTEGERP (XVECTOR (tem)->contents[i]))
13043 return 0;
13044 }
13045 else
13046 return 0;
13047
13048 return 1;
13049}
13050
13051
13052/* Load Ghostscript image IMG for use on frame F. Value is non-zero
13053 if successful. */
13054
13055static int
13056gs_load (f, img)
13057 struct frame *f;
13058 struct image *img;
13059{
13060 char buffer[100];
13061 Lisp_Object window_and_pixmap_id = Qnil, loader, pt_height, pt_width;
13062 struct gcpro gcpro1, gcpro2;
13063 Lisp_Object frame;
13064 double in_width, in_height;
13065 Lisp_Object pixel_colors = Qnil;
13066
13067 /* Compute pixel size of pixmap needed from the given size in the
13068 image specification. Sizes in the specification are in pt. 1 pt
13069 = 1/72 in, xdpi and ydpi are stored in the frame's X display
13070 info. */
13071 pt_width = image_spec_value (img->spec, QCpt_width, NULL);
13072 in_width = XFASTINT (pt_width) / 72.0;
13073 img->width = in_width * FRAME_W32_DISPLAY_INFO (f)->resx;
13074 pt_height = image_spec_value (img->spec, QCpt_height, NULL);
13075 in_height = XFASTINT (pt_height) / 72.0;
13076 img->height = in_height * FRAME_W32_DISPLAY_INFO (f)->resy;
13077
13078 /* Create the pixmap. */
13079 BLOCK_INPUT;
13080 xassert (img->pixmap == 0);
13081 img->pixmap = XCreatePixmap (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
13082 img->width, img->height,
a05e2bae 13083 one_w32_display_info.n_cbits);
6fc2811b
JR
13084 UNBLOCK_INPUT;
13085
13086 if (!img->pixmap)
13087 {
13088 image_error ("Unable to create pixmap for `%s'", img->spec, Qnil);
13089 return 0;
13090 }
13091
13092 /* Call the loader to fill the pixmap. It returns a process object
13093 if successful. We do not record_unwind_protect here because
13094 other places in redisplay like calling window scroll functions
13095 don't either. Let the Lisp loader use `unwind-protect' instead. */
13096 GCPRO2 (window_and_pixmap_id, pixel_colors);
13097
13098 sprintf (buffer, "%lu %lu",
13099 (unsigned long) FRAME_W32_WINDOW (f),
13100 (unsigned long) img->pixmap);
13101 window_and_pixmap_id = build_string (buffer);
13102
13103 sprintf (buffer, "%lu %lu",
13104 FRAME_FOREGROUND_PIXEL (f),
13105 FRAME_BACKGROUND_PIXEL (f));
13106 pixel_colors = build_string (buffer);
13107
13108 XSETFRAME (frame, f);
13109 loader = image_spec_value (img->spec, QCloader, NULL);
13110 if (NILP (loader))
13111 loader = intern ("gs-load-image");
13112
13113 img->data.lisp_val = call6 (loader, frame, img->spec,
13114 make_number (img->width),
13115 make_number (img->height),
13116 window_and_pixmap_id,
13117 pixel_colors);
13118 UNGCPRO;
13119 return PROCESSP (img->data.lisp_val);
13120}
13121
13122
13123/* Kill the Ghostscript process that was started to fill PIXMAP on
13124 frame F. Called from XTread_socket when receiving an event
13125 telling Emacs that Ghostscript has finished drawing. */
13126
13127void
13128x_kill_gs_process (pixmap, f)
13129 Pixmap pixmap;
13130 struct frame *f;
13131{
13132 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
13133 int class, i;
13134 struct image *img;
13135
13136 /* Find the image containing PIXMAP. */
13137 for (i = 0; i < c->used; ++i)
13138 if (c->images[i]->pixmap == pixmap)
13139 break;
13140
3cf3436e
JR
13141 /* Should someone in between have cleared the image cache, for
13142 instance, give up. */
13143 if (i == c->used)
13144 return;
13145
6fc2811b
JR
13146 /* Kill the GS process. We should have found PIXMAP in the image
13147 cache and its image should contain a process object. */
6fc2811b
JR
13148 img = c->images[i];
13149 xassert (PROCESSP (img->data.lisp_val));
13150 Fkill_process (img->data.lisp_val, Qnil);
13151 img->data.lisp_val = Qnil;
13152
13153 /* On displays with a mutable colormap, figure out the colors
13154 allocated for the image by looking at the pixels of an XImage for
13155 img->pixmap. */
13156 class = FRAME_W32_DISPLAY_INFO (f)->visual->class;
13157 if (class != StaticColor && class != StaticGray && class != TrueColor)
13158 {
13159 XImage *ximg;
13160
13161 BLOCK_INPUT;
13162
13163 /* Try to get an XImage for img->pixmep. */
13164 ximg = XGetImage (FRAME_W32_DISPLAY (f), img->pixmap,
13165 0, 0, img->width, img->height, ~0, ZPixmap);
13166 if (ximg)
13167 {
13168 int x, y;
13169
13170 /* Initialize the color table. */
13171 init_color_table ();
13172
13173 /* For each pixel of the image, look its color up in the
13174 color table. After having done so, the color table will
13175 contain an entry for each color used by the image. */
13176 for (y = 0; y < img->height; ++y)
13177 for (x = 0; x < img->width; ++x)
13178 {
13179 unsigned long pixel = XGetPixel (ximg, x, y);
13180 lookup_pixel_color (f, pixel);
13181 }
13182
13183 /* Record colors in the image. Free color table and XImage. */
13184 img->colors = colors_in_color_table (&img->ncolors);
13185 free_color_table ();
13186 XDestroyImage (ximg);
13187
13188#if 0 /* This doesn't seem to be the case. If we free the colors
13189 here, we get a BadAccess later in x_clear_image when
13190 freeing the colors. */
13191 /* We have allocated colors once, but Ghostscript has also
13192 allocated colors on behalf of us. So, to get the
13193 reference counts right, free them once. */
13194 if (img->ncolors)
3cf3436e 13195 x_free_colors (FRAME_W32_DISPLAY (f), cmap,
6fc2811b 13196 img->colors, img->ncolors, 0);
6fc2811b
JR
13197#endif
13198 }
13199 else
13200 image_error ("Cannot get X image of `%s'; colors will not be freed",
13201 img->spec, Qnil);
13202
13203 UNBLOCK_INPUT;
13204 }
3cf3436e
JR
13205
13206 /* Now that we have the pixmap, compute mask and transform the
13207 image if requested. */
13208 BLOCK_INPUT;
13209 postprocess_image (f, img);
13210 UNBLOCK_INPUT;
6fc2811b
JR
13211}
13212
13213#endif /* HAVE_GHOSTSCRIPT */
13214
13215\f
13216/***********************************************************************
13217 Window properties
13218 ***********************************************************************/
13219
13220DEFUN ("x-change-window-property", Fx_change_window_property,
13221 Sx_change_window_property, 2, 3, 0,
74e1aeec
JR
13222 doc: /* Change window property PROP to VALUE on the X window of FRAME.
13223PROP and VALUE must be strings. FRAME nil or omitted means use the
13224selected frame. Value is VALUE. */)
6fc2811b
JR
13225 (prop, value, frame)
13226 Lisp_Object frame, prop, value;
13227{
767b1ff0 13228#if 0 /* TODO : port window properties to W32 */
6fc2811b
JR
13229 struct frame *f = check_x_frame (frame);
13230 Atom prop_atom;
13231
b7826503
PJ
13232 CHECK_STRING (prop);
13233 CHECK_STRING (value);
6fc2811b
JR
13234
13235 BLOCK_INPUT;
13236 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), XSTRING (prop)->data, False);
13237 XChangeProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
13238 prop_atom, XA_STRING, 8, PropModeReplace,
13239 XSTRING (value)->data, XSTRING (value)->size);
13240
13241 /* Make sure the property is set when we return. */
13242 XFlush (FRAME_W32_DISPLAY (f));
13243 UNBLOCK_INPUT;
13244
767b1ff0 13245#endif /* TODO */
6fc2811b
JR
13246
13247 return value;
13248}
13249
13250
13251DEFUN ("x-delete-window-property", Fx_delete_window_property,
13252 Sx_delete_window_property, 1, 2, 0,
74e1aeec
JR
13253 doc: /* Remove window property PROP from X window of FRAME.
13254FRAME nil or omitted means use the selected frame. Value is PROP. */)
6fc2811b
JR
13255 (prop, frame)
13256 Lisp_Object prop, frame;
13257{
767b1ff0 13258#if 0 /* TODO : port window properties to W32 */
6fc2811b
JR
13259
13260 struct frame *f = check_x_frame (frame);
13261 Atom prop_atom;
13262
b7826503 13263 CHECK_STRING (prop);
6fc2811b
JR
13264 BLOCK_INPUT;
13265 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), XSTRING (prop)->data, False);
13266 XDeleteProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), prop_atom);
13267
13268 /* Make sure the property is removed when we return. */
13269 XFlush (FRAME_W32_DISPLAY (f));
13270 UNBLOCK_INPUT;
767b1ff0 13271#endif /* TODO */
6fc2811b
JR
13272
13273 return prop;
13274}
13275
13276
13277DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
13278 1, 2, 0,
74e1aeec
JR
13279 doc: /* Value is the value of window property PROP on FRAME.
13280If FRAME is nil or omitted, use the selected frame. Value is nil
13281if FRAME hasn't a property with name PROP or if PROP has no string
13282value. */)
6fc2811b
JR
13283 (prop, frame)
13284 Lisp_Object prop, frame;
13285{
767b1ff0 13286#if 0 /* TODO : port window properties to W32 */
6fc2811b
JR
13287
13288 struct frame *f = check_x_frame (frame);
13289 Atom prop_atom;
13290 int rc;
13291 Lisp_Object prop_value = Qnil;
13292 char *tmp_data = NULL;
13293 Atom actual_type;
13294 int actual_format;
13295 unsigned long actual_size, bytes_remaining;
13296
b7826503 13297 CHECK_STRING (prop);
6fc2811b
JR
13298 BLOCK_INPUT;
13299 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), XSTRING (prop)->data, False);
13300 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
13301 prop_atom, 0, 0, False, XA_STRING,
13302 &actual_type, &actual_format, &actual_size,
13303 &bytes_remaining, (unsigned char **) &tmp_data);
13304 if (rc == Success)
13305 {
13306 int size = bytes_remaining;
13307
13308 XFree (tmp_data);
13309 tmp_data = NULL;
13310
13311 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
13312 prop_atom, 0, bytes_remaining,
13313 False, XA_STRING,
13314 &actual_type, &actual_format,
13315 &actual_size, &bytes_remaining,
13316 (unsigned char **) &tmp_data);
13317 if (rc == Success)
13318 prop_value = make_string (tmp_data, size);
13319
13320 XFree (tmp_data);
13321 }
13322
13323 UNBLOCK_INPUT;
13324
13325 return prop_value;
13326
767b1ff0 13327#endif /* TODO */
6fc2811b
JR
13328 return Qnil;
13329}
13330
13331
13332\f
13333/***********************************************************************
13334 Busy cursor
13335 ***********************************************************************/
13336
f79e6790 13337/* If non-null, an asynchronous timer that, when it expires, displays
0af913d7 13338 an hourglass cursor on all frames. */
6fc2811b 13339
0af913d7 13340static struct atimer *hourglass_atimer;
6fc2811b 13341
0af913d7 13342/* Non-zero means an hourglass cursor is currently shown. */
6fc2811b 13343
0af913d7 13344static int hourglass_shown_p;
6fc2811b 13345
0af913d7 13346/* Number of seconds to wait before displaying an hourglass cursor. */
6fc2811b 13347
0af913d7 13348static Lisp_Object Vhourglass_delay;
6fc2811b 13349
0af913d7 13350/* Default number of seconds to wait before displaying an hourglass
f79e6790
JR
13351 cursor. */
13352
0af913d7 13353#define DEFAULT_HOURGLASS_DELAY 1
f79e6790
JR
13354
13355/* Function prototypes. */
13356
0af913d7
GM
13357static void show_hourglass P_ ((struct atimer *));
13358static void hide_hourglass P_ ((void));
f79e6790
JR
13359
13360
0af913d7 13361/* Cancel a currently active hourglass timer, and start a new one. */
f79e6790
JR
13362
13363void
0af913d7 13364start_hourglass ()
f79e6790 13365{
767b1ff0 13366#if 0 /* TODO: cursor shape changes. */
f79e6790 13367 EMACS_TIME delay;
dfff8a69 13368 int secs, usecs = 0;
f79e6790 13369
0af913d7 13370 cancel_hourglass ();
f79e6790 13371
0af913d7
GM
13372 if (INTEGERP (Vhourglass_delay)
13373 && XINT (Vhourglass_delay) > 0)
13374 secs = XFASTINT (Vhourglass_delay);
13375 else if (FLOATP (Vhourglass_delay)
13376 && XFLOAT_DATA (Vhourglass_delay) > 0)
dfff8a69
JR
13377 {
13378 Lisp_Object tem;
0af913d7 13379 tem = Ftruncate (Vhourglass_delay, Qnil);
dfff8a69 13380 secs = XFASTINT (tem);
0af913d7 13381 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
dfff8a69 13382 }
f79e6790 13383 else
0af913d7 13384 secs = DEFAULT_HOURGLASS_DELAY;
f79e6790 13385
dfff8a69 13386 EMACS_SET_SECS_USECS (delay, secs, usecs);
0af913d7
GM
13387 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
13388 show_hourglass, NULL);
f79e6790
JR
13389#endif
13390}
13391
13392
0af913d7
GM
13393/* Cancel the hourglass cursor timer if active, hide an hourglass
13394 cursor if shown. */
f79e6790
JR
13395
13396void
0af913d7 13397cancel_hourglass ()
f79e6790 13398{
0af913d7 13399 if (hourglass_atimer)
dfff8a69 13400 {
0af913d7
GM
13401 cancel_atimer (hourglass_atimer);
13402 hourglass_atimer = NULL;
dfff8a69
JR
13403 }
13404
0af913d7
GM
13405 if (hourglass_shown_p)
13406 hide_hourglass ();
f79e6790
JR
13407}
13408
13409
0af913d7
GM
13410/* Timer function of hourglass_atimer. TIMER is equal to
13411 hourglass_atimer.
f79e6790 13412
0af913d7
GM
13413 Display an hourglass cursor on all frames by mapping the frames'
13414 hourglass_window. Set the hourglass_p flag in the frames'
13415 output_data.x structure to indicate that an hourglass cursor is
13416 shown on the frames. */
f79e6790
JR
13417
13418static void
0af913d7 13419show_hourglass (timer)
f79e6790 13420 struct atimer *timer;
6fc2811b 13421{
767b1ff0 13422#if 0 /* TODO: cursor shape changes. */
f79e6790 13423 /* The timer implementation will cancel this timer automatically
0af913d7 13424 after this function has run. Set hourglass_atimer to null
f79e6790 13425 so that we know the timer doesn't have to be canceled. */
0af913d7 13426 hourglass_atimer = NULL;
f79e6790 13427
0af913d7 13428 if (!hourglass_shown_p)
6fc2811b
JR
13429 {
13430 Lisp_Object rest, frame;
f79e6790
JR
13431
13432 BLOCK_INPUT;
13433
6fc2811b 13434 FOR_EACH_FRAME (rest, frame)
dc220243 13435 if (FRAME_W32_P (XFRAME (frame)))
6fc2811b
JR
13436 {
13437 struct frame *f = XFRAME (frame);
f79e6790 13438
0af913d7 13439 f->output_data.w32->hourglass_p = 1;
f79e6790 13440
0af913d7 13441 if (!f->output_data.w32->hourglass_window)
6fc2811b
JR
13442 {
13443 unsigned long mask = CWCursor;
13444 XSetWindowAttributes attrs;
f79e6790 13445
0af913d7 13446 attrs.cursor = f->output_data.w32->hourglass_cursor;
f79e6790 13447
0af913d7 13448 f->output_data.w32->hourglass_window
f79e6790 13449 = XCreateWindow (FRAME_X_DISPLAY (f),
6fc2811b
JR
13450 FRAME_OUTER_WINDOW (f),
13451 0, 0, 32000, 32000, 0, 0,
f79e6790
JR
13452 InputOnly,
13453 CopyFromParent,
6fc2811b
JR
13454 mask, &attrs);
13455 }
f79e6790 13456
0af913d7
GM
13457 XMapRaised (FRAME_X_DISPLAY (f),
13458 f->output_data.w32->hourglass_window);
f79e6790 13459 XFlush (FRAME_X_DISPLAY (f));
6fc2811b 13460 }
6fc2811b 13461
0af913d7 13462 hourglass_shown_p = 1;
f79e6790
JR
13463 UNBLOCK_INPUT;
13464 }
13465#endif
6fc2811b
JR
13466}
13467
13468
0af913d7 13469/* Hide the hourglass cursor on all frames, if it is currently shown. */
6fc2811b 13470
f79e6790 13471static void
0af913d7 13472hide_hourglass ()
f79e6790 13473{
767b1ff0 13474#if 0 /* TODO: cursor shape changes. */
0af913d7 13475 if (hourglass_shown_p)
6fc2811b 13476 {
f79e6790
JR
13477 Lisp_Object rest, frame;
13478
13479 BLOCK_INPUT;
13480 FOR_EACH_FRAME (rest, frame)
6fc2811b 13481 {
f79e6790
JR
13482 struct frame *f = XFRAME (frame);
13483
dc220243 13484 if (FRAME_W32_P (f)
f79e6790 13485 /* Watch out for newly created frames. */
0af913d7 13486 && f->output_data.x->hourglass_window)
f79e6790 13487 {
0af913d7
GM
13488 XUnmapWindow (FRAME_X_DISPLAY (f),
13489 f->output_data.x->hourglass_window);
13490 /* Sync here because XTread_socket looks at the
13491 hourglass_p flag that is reset to zero below. */
f79e6790 13492 XSync (FRAME_X_DISPLAY (f), False);
0af913d7 13493 f->output_data.x->hourglass_p = 0;
f79e6790 13494 }
6fc2811b 13495 }
6fc2811b 13496
0af913d7 13497 hourglass_shown_p = 0;
f79e6790
JR
13498 UNBLOCK_INPUT;
13499 }
13500#endif
6fc2811b
JR
13501}
13502
13503
13504\f
13505/***********************************************************************
13506 Tool tips
13507 ***********************************************************************/
13508
13509static Lisp_Object x_create_tip_frame P_ ((struct w32_display_info *,
3cf3436e
JR
13510 Lisp_Object, Lisp_Object));
13511static void compute_tip_xy P_ ((struct frame *, Lisp_Object, Lisp_Object,
13512 Lisp_Object, int, int, int *, int *));
6fc2811b 13513
3cf3436e 13514/* The frame of a currently visible tooltip. */
6fc2811b 13515
937e601e 13516Lisp_Object tip_frame;
6fc2811b
JR
13517
13518/* If non-nil, a timer started that hides the last tooltip when it
13519 fires. */
13520
13521Lisp_Object tip_timer;
13522Window tip_window;
13523
3cf3436e
JR
13524/* If non-nil, a vector of 3 elements containing the last args
13525 with which x-show-tip was called. See there. */
13526
13527Lisp_Object last_show_tip_args;
13528
13529/* Maximum size for tooltips; a cons (COLUMNS . ROWS). */
13530
13531Lisp_Object Vx_max_tooltip_size;
13532
13533
937e601e
AI
13534static Lisp_Object
13535unwind_create_tip_frame (frame)
13536 Lisp_Object frame;
13537{
c844a81a
GM
13538 Lisp_Object deleted;
13539
13540 deleted = unwind_create_frame (frame);
13541 if (EQ (deleted, Qt))
13542 {
13543 tip_window = NULL;
13544 tip_frame = Qnil;
13545 }
13546
13547 return deleted;
937e601e
AI
13548}
13549
13550
6fc2811b 13551/* Create a frame for a tooltip on the display described by DPYINFO.
3cf3436e
JR
13552 PARMS is a list of frame parameters. TEXT is the string to
13553 display in the tip frame. Value is the frame.
937e601e
AI
13554
13555 Note that functions called here, esp. x_default_parameter can
13556 signal errors, for instance when a specified color name is
13557 undefined. We have to make sure that we're in a consistent state
13558 when this happens. */
6fc2811b
JR
13559
13560static Lisp_Object
3cf3436e 13561x_create_tip_frame (dpyinfo, parms, text)
6fc2811b 13562 struct w32_display_info *dpyinfo;
3cf3436e 13563 Lisp_Object parms, text;
6fc2811b 13564{
6fc2811b
JR
13565 struct frame *f;
13566 Lisp_Object frame, tem;
13567 Lisp_Object name;
13568 long window_prompting = 0;
13569 int width, height;
dc220243 13570 int count = BINDING_STACK_SIZE ();
6fc2811b
JR
13571 struct gcpro gcpro1, gcpro2, gcpro3;
13572 struct kboard *kb;
3cf3436e
JR
13573 int face_change_count_before = face_change_count;
13574 Lisp_Object buffer;
13575 struct buffer *old_buffer;
6fc2811b 13576
ca56d953 13577 check_w32 ();
6fc2811b
JR
13578
13579 /* Use this general default value to start with until we know if
13580 this frame has a specified name. */
13581 Vx_resource_name = Vinvocation_name;
13582
13583#ifdef MULTI_KBOARD
13584 kb = dpyinfo->kboard;
13585#else
13586 kb = &the_only_kboard;
13587#endif
13588
13589 /* Get the name of the frame to use for resource lookup. */
13590 name = w32_get_arg (parms, Qname, "name", "Name", RES_TYPE_STRING);
13591 if (!STRINGP (name)
13592 && !EQ (name, Qunbound)
13593 && !NILP (name))
13594 error ("Invalid frame name--not a string or nil");
13595 Vx_resource_name = name;
13596
13597 frame = Qnil;
13598 GCPRO3 (parms, name, frame);
9eb16b62
JR
13599 /* Make a frame without minibuffer nor mode-line. */
13600 f = make_frame (0);
13601 f->wants_modeline = 0;
6fc2811b 13602 XSETFRAME (frame, f);
3cf3436e
JR
13603
13604 buffer = Fget_buffer_create (build_string (" *tip*"));
13605 Fset_window_buffer (FRAME_ROOT_WINDOW (f), buffer);
13606 old_buffer = current_buffer;
13607 set_buffer_internal_1 (XBUFFER (buffer));
13608 current_buffer->truncate_lines = Qnil;
13609 Ferase_buffer ();
13610 Finsert (1, &text);
13611 set_buffer_internal_1 (old_buffer);
13612
6fc2811b 13613 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
937e601e 13614 record_unwind_protect (unwind_create_tip_frame, frame);
6fc2811b 13615
3cf3436e
JR
13616 /* By setting the output method, we're essentially saying that
13617 the frame is live, as per FRAME_LIVE_P. If we get a signal
13618 from this point on, x_destroy_window might screw up reference
13619 counts etc. */
d88c567c 13620 f->output_method = output_w32;
6fc2811b
JR
13621 f->output_data.w32 =
13622 (struct w32_output *) xmalloc (sizeof (struct w32_output));
13623 bzero (f->output_data.w32, sizeof (struct w32_output));
ca56d953
JR
13624
13625 FRAME_FONTSET (f) = -1;
6fc2811b
JR
13626 f->icon_name = Qnil;
13627
ca56d953 13628#if 0 /* GLYPH_DEBUG TODO: image support. */
937e601e
AI
13629 image_cache_refcount = FRAME_X_IMAGE_CACHE (f)->refcount;
13630 dpyinfo_refcount = dpyinfo->reference_count;
13631#endif /* GLYPH_DEBUG */
6fc2811b
JR
13632#ifdef MULTI_KBOARD
13633 FRAME_KBOARD (f) = kb;
13634#endif
13635 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
13636 f->output_data.w32->explicit_parent = 0;
13637
13638 /* Set the name; the functions to which we pass f expect the name to
13639 be set. */
13640 if (EQ (name, Qunbound) || NILP (name))
13641 {
ca56d953 13642 f->name = build_string (dpyinfo->w32_id_name);
6fc2811b
JR
13643 f->explicit_name = 0;
13644 }
13645 else
13646 {
13647 f->name = name;
13648 f->explicit_name = 1;
13649 /* use the frame's title when getting resources for this frame. */
13650 specbind (Qx_resource_name, name);
13651 }
13652
6fc2811b
JR
13653 /* Extract the window parameters from the supplied values
13654 that are needed to determine window geometry. */
13655 {
13656 Lisp_Object font;
13657
13658 font = w32_get_arg (parms, Qfont, "font", "Font", RES_TYPE_STRING);
13659
13660 BLOCK_INPUT;
13661 /* First, try whatever font the caller has specified. */
13662 if (STRINGP (font))
13663 {
13664 tem = Fquery_fontset (font, Qnil);
13665 if (STRINGP (tem))
13666 font = x_new_fontset (f, XSTRING (tem)->data);
13667 else
13668 font = x_new_font (f, XSTRING (font)->data);
13669 }
13670
13671 /* Try out a font which we hope has bold and italic variations. */
13672 if (!STRINGP (font))
ca56d953 13673 font = x_new_font (f, "-*-Courier New-normal-r-*-*-*-100-*-*-c-*-iso8859-1");
6fc2811b 13674 if (! STRINGP (font))
ca56d953 13675 font = x_new_font (f, "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1");
6fc2811b
JR
13676 /* If those didn't work, look for something which will at least work. */
13677 if (! STRINGP (font))
ca56d953 13678 font = x_new_font (f, "-*-Fixedsys-normal-r-*-*-12-*-*-*-c-*-iso8859-1");
6fc2811b
JR
13679 UNBLOCK_INPUT;
13680 if (! STRINGP (font))
ca56d953 13681 font = build_string ("Fixedsys");
6fc2811b
JR
13682
13683 x_default_parameter (f, parms, Qfont, font,
13684 "font", "Font", RES_TYPE_STRING);
13685 }
13686
13687 x_default_parameter (f, parms, Qborder_width, make_number (2),
13688 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
6fc2811b
JR
13689 /* This defaults to 2 in order to match xterm. We recognize either
13690 internalBorderWidth or internalBorder (which is what xterm calls
13691 it). */
13692 if (NILP (Fassq (Qinternal_border_width, parms)))
13693 {
13694 Lisp_Object value;
13695
13696 value = w32_get_arg (parms, Qinternal_border_width,
13697 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
13698 if (! EQ (value, Qunbound))
13699 parms = Fcons (Fcons (Qinternal_border_width, value),
13700 parms);
13701 }
bfd6edcc 13702 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
6fc2811b
JR
13703 "internalBorderWidth", "internalBorderWidth",
13704 RES_TYPE_NUMBER);
13705
13706 /* Also do the stuff which must be set before the window exists. */
13707 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
13708 "foreground", "Foreground", RES_TYPE_STRING);
13709 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
13710 "background", "Background", RES_TYPE_STRING);
13711 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
13712 "pointerColor", "Foreground", RES_TYPE_STRING);
13713 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
13714 "cursorColor", "Foreground", RES_TYPE_STRING);
13715 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
13716 "borderColor", "BorderColor", RES_TYPE_STRING);
13717
13718 /* Init faces before x_default_parameter is called for scroll-bar
13719 parameters because that function calls x_set_scroll_bar_width,
13720 which calls change_frame_size, which calls Fset_window_buffer,
13721 which runs hooks, which call Fvertical_motion. At the end, we
13722 end up in init_iterator with a null face cache, which should not
13723 happen. */
13724 init_frame_faces (f);
ca56d953
JR
13725
13726 f->output_data.w32->dwStyle = WS_BORDER | WS_POPUP | WS_DISABLED;
6fc2811b 13727 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
9eb16b62 13728
6fc2811b
JR
13729 window_prompting = x_figure_window_size (f, parms);
13730
9eb16b62
JR
13731 /* No fringes on tip frame. */
13732 f->output_data.w32->fringes_extra = 0;
13733 f->output_data.w32->fringe_cols = 0;
13734 f->output_data.w32->left_fringe_width = 0;
13735 f->output_data.w32->right_fringe_width = 0;
13736
6fc2811b
JR
13737 if (window_prompting & XNegative)
13738 {
13739 if (window_prompting & YNegative)
13740 f->output_data.w32->win_gravity = SouthEastGravity;
13741 else
13742 f->output_data.w32->win_gravity = NorthEastGravity;
13743 }
13744 else
13745 {
13746 if (window_prompting & YNegative)
13747 f->output_data.w32->win_gravity = SouthWestGravity;
13748 else
13749 f->output_data.w32->win_gravity = NorthWestGravity;
13750 }
13751
13752 f->output_data.w32->size_hint_flags = window_prompting;
ca56d953
JR
13753
13754 BLOCK_INPUT;
13755 my_create_tip_window (f);
13756 UNBLOCK_INPUT;
6fc2811b
JR
13757
13758 x_make_gc (f);
13759
13760 x_default_parameter (f, parms, Qauto_raise, Qnil,
13761 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
13762 x_default_parameter (f, parms, Qauto_lower, Qnil,
13763 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
13764 x_default_parameter (f, parms, Qcursor_type, Qbox,
13765 "cursorType", "CursorType", RES_TYPE_SYMBOL);
13766
13767 /* Dimensions, especially f->height, must be done via change_frame_size.
13768 Change will not be effected unless different from the current
13769 f->height. */
13770 width = f->width;
13771 height = f->height;
13772 f->height = 0;
13773 SET_FRAME_WIDTH (f, 0);
13774 change_frame_size (f, height, width, 1, 0, 0);
13775
3cf3436e
JR
13776 /* Set up faces after all frame parameters are known. This call
13777 also merges in face attributes specified for new frames.
13778
13779 Frame parameters may be changed if .Xdefaults contains
13780 specifications for the default font. For example, if there is an
13781 `Emacs.default.attributeBackground: pink', the `background-color'
13782 attribute of the frame get's set, which let's the internal border
13783 of the tooltip frame appear in pink. Prevent this. */
13784 {
13785 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
13786
13787 /* Set tip_frame here, so that */
13788 tip_frame = frame;
13789 call1 (Qface_set_after_frame_default, frame);
13790
13791 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
13792 Fmodify_frame_parameters (frame, Fcons (Fcons (Qbackground_color, bg),
13793 Qnil));
13794 }
13795
6fc2811b
JR
13796 f->no_split = 1;
13797
13798 UNGCPRO;
13799
13800 /* It is now ok to make the frame official even if we get an error
13801 below. And the frame needs to be on Vframe_list or making it
13802 visible won't work. */
13803 Vframe_list = Fcons (frame, Vframe_list);
13804
13805 /* Now that the frame is official, it counts as a reference to
13806 its display. */
13807 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
ee78dc32 13808
3cf3436e
JR
13809 /* Setting attributes of faces of the tooltip frame from resources
13810 and similar will increment face_change_count, which leads to the
13811 clearing of all current matrices. Since this isn't necessary
13812 here, avoid it by resetting face_change_count to the value it
13813 had before we created the tip frame. */
13814 face_change_count = face_change_count_before;
13815
13816 /* Discard the unwind_protect. */
6fc2811b 13817 return unbind_to (count, frame);
ee78dc32
GV
13818}
13819
3cf3436e
JR
13820
13821/* Compute where to display tip frame F. PARMS is the list of frame
13822 parameters for F. DX and DY are specified offsets from the current
13823 location of the mouse. WIDTH and HEIGHT are the width and height
13824 of the tooltip. Return coordinates relative to the root window of
13825 the display in *ROOT_X, and *ROOT_Y. */
13826
13827static void
13828compute_tip_xy (f, parms, dx, dy, width, height, root_x, root_y)
13829 struct frame *f;
13830 Lisp_Object parms, dx, dy;
13831 int width, height;
13832 int *root_x, *root_y;
13833{
3cf3436e 13834 Lisp_Object left, top;
3cf3436e
JR
13835
13836 /* User-specified position? */
13837 left = Fcdr (Fassq (Qleft, parms));
13838 top = Fcdr (Fassq (Qtop, parms));
13839
13840 /* Move the tooltip window where the mouse pointer is. Resize and
13841 show it. */
ca56d953 13842 if (!INTEGERP (left) || !INTEGERP (top))
3cf3436e 13843 {
ca56d953
JR
13844 POINT pt;
13845
3cf3436e 13846 BLOCK_INPUT;
ca56d953
JR
13847 GetCursorPos (&pt);
13848 *root_x = pt.x;
13849 *root_y = pt.y;
3cf3436e
JR
13850 UNBLOCK_INPUT;
13851 }
13852
13853 if (INTEGERP (top))
13854 *root_y = XINT (top);
13855 else if (*root_y + XINT (dy) - height < 0)
13856 *root_y -= XINT (dy);
13857 else
13858 {
13859 *root_y -= height;
13860 *root_y += XINT (dy);
13861 }
13862
13863 if (INTEGERP (left))
13864 *root_x = XINT (left);
72e4adef
JR
13865 else if (*root_x + XINT (dx) + width <= FRAME_W32_DISPLAY_INFO (f)->width)
13866 /* It fits to the right of the pointer. */
13867 *root_x += XINT (dx);
13868 else if (width + XINT (dx) <= *root_x)
13869 /* It fits to the left of the pointer. */
3cf3436e
JR
13870 *root_x -= width + XINT (dx);
13871 else
72e4adef
JR
13872 /* Put it left justified on the screen -- it ought to fit that way. */
13873 *root_x = 0;
3cf3436e
JR
13874}
13875
13876
71eab8d1 13877DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
74e1aeec
JR
13878 doc: /* Show STRING in a \"tooltip\" window on frame FRAME.
13879A tooltip window is a small window displaying a string.
13880
13881FRAME nil or omitted means use the selected frame.
13882
13883PARMS is an optional list of frame parameters which can be
13884used to change the tooltip's appearance.
13885
ca56d953
JR
13886Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
13887means use the default timeout of 5 seconds.
74e1aeec 13888
ca56d953 13889If the list of frame parameters PARAMS contains a `left' parameter,
74e1aeec
JR
13890the tooltip is displayed at that x-position. Otherwise it is
13891displayed at the mouse position, with offset DX added (default is 5 if
13892DX isn't specified). Likewise for the y-position; if a `top' frame
13893parameter is specified, it determines the y-position of the tooltip
13894window, otherwise it is displayed at the mouse position, with offset
13895DY added (default is -10).
13896
13897A tooltip's maximum size is specified by `x-max-tooltip-size'.
13898Text larger than the specified size is clipped. */)
71eab8d1
AI
13899 (string, frame, parms, timeout, dx, dy)
13900 Lisp_Object string, frame, parms, timeout, dx, dy;
ee78dc32 13901{
6fc2811b
JR
13902 struct frame *f;
13903 struct window *w;
3cf3436e 13904 int root_x, root_y;
6fc2811b
JR
13905 struct buffer *old_buffer;
13906 struct text_pos pos;
13907 int i, width, height;
6fc2811b
JR
13908 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
13909 int old_windows_or_buffers_changed = windows_or_buffers_changed;
ca56d953 13910 int count = BINDING_STACK_SIZE ();
6fc2811b
JR
13911
13912 specbind (Qinhibit_redisplay, Qt);
ee78dc32 13913
dfff8a69 13914 GCPRO4 (string, parms, frame, timeout);
ee78dc32 13915
b7826503 13916 CHECK_STRING (string);
6fc2811b
JR
13917 f = check_x_frame (frame);
13918 if (NILP (timeout))
13919 timeout = make_number (5);
13920 else
b7826503 13921 CHECK_NATNUM (timeout);
ee78dc32 13922
71eab8d1
AI
13923 if (NILP (dx))
13924 dx = make_number (5);
13925 else
b7826503 13926 CHECK_NUMBER (dx);
71eab8d1
AI
13927
13928 if (NILP (dy))
dc220243 13929 dy = make_number (-10);
71eab8d1 13930 else
b7826503 13931 CHECK_NUMBER (dy);
71eab8d1 13932
dc220243
JR
13933 if (NILP (last_show_tip_args))
13934 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
13935
13936 if (!NILP (tip_frame))
13937 {
13938 Lisp_Object last_string = AREF (last_show_tip_args, 0);
13939 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
13940 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
13941
13942 if (EQ (frame, last_frame)
13943 && !NILP (Fequal (last_string, string))
13944 && !NILP (Fequal (last_parms, parms)))
13945 {
13946 struct frame *f = XFRAME (tip_frame);
13947
13948 /* Only DX and DY have changed. */
13949 if (!NILP (tip_timer))
13950 {
13951 Lisp_Object timer = tip_timer;
13952 tip_timer = Qnil;
13953 call1 (Qcancel_timer, timer);
13954 }
13955
13956 BLOCK_INPUT;
ca56d953
JR
13957 compute_tip_xy (f, parms, dx, dy, PIXEL_WIDTH (f),
13958 PIXEL_HEIGHT (f), &root_x, &root_y);
d65a9cdc
JR
13959
13960 /* Put tooltip in topmost group and in position. */
ca56d953
JR
13961 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
13962 root_x, root_y, 0, 0,
13963 SWP_NOSIZE | SWP_NOACTIVATE);
d65a9cdc
JR
13964
13965 /* Ensure tooltip is on top of other topmost windows (eg menus). */
13966 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
13967 0, 0, 0, 0,
13968 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
13969
dc220243
JR
13970 UNBLOCK_INPUT;
13971 goto start_timer;
13972 }
13973 }
13974
6fc2811b
JR
13975 /* Hide a previous tip, if any. */
13976 Fx_hide_tip ();
ee78dc32 13977
dc220243
JR
13978 ASET (last_show_tip_args, 0, string);
13979 ASET (last_show_tip_args, 1, frame);
13980 ASET (last_show_tip_args, 2, parms);
13981
6fc2811b
JR
13982 /* Add default values to frame parameters. */
13983 if (NILP (Fassq (Qname, parms)))
13984 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
13985 if (NILP (Fassq (Qinternal_border_width, parms)))
13986 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
13987 if (NILP (Fassq (Qborder_width, parms)))
13988 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
13989 if (NILP (Fassq (Qborder_color, parms)))
13990 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
13991 if (NILP (Fassq (Qbackground_color, parms)))
13992 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
13993 parms);
13994
0e3fcdef
JR
13995 /* Block input until the tip has been fully drawn, to avoid crashes
13996 when drawing tips in menus. */
13997 BLOCK_INPUT;
13998
6fc2811b
JR
13999 /* Create a frame for the tooltip, and record it in the global
14000 variable tip_frame. */
ca56d953 14001 frame = x_create_tip_frame (FRAME_W32_DISPLAY_INFO (f), parms, string);
937e601e 14002 f = XFRAME (frame);
6fc2811b 14003
3cf3436e 14004 /* Set up the frame's root window. */
6fc2811b
JR
14005 w = XWINDOW (FRAME_ROOT_WINDOW (f));
14006 w->left = w->top = make_number (0);
3cf3436e
JR
14007
14008 if (CONSP (Vx_max_tooltip_size)
14009 && INTEGERP (XCAR (Vx_max_tooltip_size))
14010 && XINT (XCAR (Vx_max_tooltip_size)) > 0
14011 && INTEGERP (XCDR (Vx_max_tooltip_size))
14012 && XINT (XCDR (Vx_max_tooltip_size)) > 0)
14013 {
14014 w->width = XCAR (Vx_max_tooltip_size);
14015 w->height = XCDR (Vx_max_tooltip_size);
14016 }
14017 else
14018 {
14019 w->width = make_number (80);
14020 w->height = make_number (40);
14021 }
14022
14023 f->window_width = XINT (w->width);
6fc2811b
JR
14024 adjust_glyphs (f);
14025 w->pseudo_window_p = 1;
14026
14027 /* Display the tooltip text in a temporary buffer. */
6fc2811b 14028 old_buffer = current_buffer;
3cf3436e
JR
14029 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->buffer));
14030 current_buffer->truncate_lines = Qnil;
6fc2811b
JR
14031 clear_glyph_matrix (w->desired_matrix);
14032 clear_glyph_matrix (w->current_matrix);
14033 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
14034 try_window (FRAME_ROOT_WINDOW (f), pos);
14035
14036 /* Compute width and height of the tooltip. */
14037 width = height = 0;
14038 for (i = 0; i < w->desired_matrix->nrows; ++i)
ee78dc32 14039 {
6fc2811b
JR
14040 struct glyph_row *row = &w->desired_matrix->rows[i];
14041 struct glyph *last;
14042 int row_width;
14043
14044 /* Stop at the first empty row at the end. */
14045 if (!row->enabled_p || !row->displays_text_p)
14046 break;
14047
14048 /* Let the row go over the full width of the frame. */
14049 row->full_width_p = 1;
14050
4e3a1c61
JR
14051#ifdef TODO /* Investigate why some fonts need more width than is
14052 calculated for some tooltips. */
6fc2811b
JR
14053 /* There's a glyph at the end of rows that is use to place
14054 the cursor there. Don't include the width of this glyph. */
14055 if (row->used[TEXT_AREA])
14056 {
14057 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
14058 row_width = row->pixel_width - last->pixel_width;
14059 }
14060 else
4e3a1c61 14061#endif
6fc2811b
JR
14062 row_width = row->pixel_width;
14063
ca56d953 14064 /* TODO: find why tips do not draw along baseline as instructed. */
bfd6edcc 14065 height += row->height;
6fc2811b 14066 width = max (width, row_width);
ee78dc32
GV
14067 }
14068
6fc2811b
JR
14069 /* Add the frame's internal border to the width and height the X
14070 window should have. */
14071 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
14072 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
ee78dc32 14073
6fc2811b
JR
14074 /* Move the tooltip window where the mouse pointer is. Resize and
14075 show it. */
3cf3436e 14076 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
71eab8d1 14077
bfd6edcc
JR
14078 {
14079 /* Adjust Window size to take border into account. */
14080 RECT rect;
14081 rect.left = rect.top = 0;
14082 rect.right = width;
14083 rect.bottom = height;
14084 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
14085 FRAME_EXTERNAL_MENU_BAR (f));
14086
d65a9cdc 14087 /* Position and size tooltip, and put it in the topmost group. */
bfd6edcc
JR
14088 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
14089 root_x, root_y, rect.right - rect.left,
14090 rect.bottom - rect.top, SWP_NOACTIVATE);
14091
d65a9cdc
JR
14092 /* Ensure tooltip is on top of other topmost windows (eg menus). */
14093 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
14094 0, 0, 0, 0,
14095 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
14096
bfd6edcc
JR
14097 /* Let redisplay know that we have made the frame visible already. */
14098 f->async_visible = 1;
14099
14100 ShowWindow (FRAME_W32_WINDOW (f), SW_SHOWNOACTIVATE);
14101 }
ee78dc32 14102
6fc2811b
JR
14103 /* Draw into the window. */
14104 w->must_be_updated_p = 1;
14105 update_single_window (w, 1);
ee78dc32 14106
0e3fcdef
JR
14107 UNBLOCK_INPUT;
14108
6fc2811b
JR
14109 /* Restore original current buffer. */
14110 set_buffer_internal_1 (old_buffer);
14111 windows_or_buffers_changed = old_windows_or_buffers_changed;
ee78dc32 14112
dc220243 14113 start_timer:
6fc2811b
JR
14114 /* Let the tip disappear after timeout seconds. */
14115 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
14116 intern ("x-hide-tip"));
ee78dc32 14117
dfff8a69 14118 UNGCPRO;
6fc2811b 14119 return unbind_to (count, Qnil);
ee78dc32
GV
14120}
14121
ee78dc32 14122
6fc2811b 14123DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
74e1aeec
JR
14124 doc: /* Hide the current tooltip window, if there is any.
14125Value is t if tooltip was open, nil otherwise. */)
6fc2811b
JR
14126 ()
14127{
937e601e
AI
14128 int count;
14129 Lisp_Object deleted, frame, timer;
14130 struct gcpro gcpro1, gcpro2;
14131
14132 /* Return quickly if nothing to do. */
14133 if (NILP (tip_timer) && NILP (tip_frame))
14134 return Qnil;
14135
14136 frame = tip_frame;
14137 timer = tip_timer;
14138 GCPRO2 (frame, timer);
14139 tip_frame = tip_timer = deleted = Qnil;
6fc2811b 14140
937e601e 14141 count = BINDING_STACK_SIZE ();
6fc2811b 14142 specbind (Qinhibit_redisplay, Qt);
937e601e 14143 specbind (Qinhibit_quit, Qt);
6fc2811b 14144
937e601e 14145 if (!NILP (timer))
dc220243 14146 call1 (Qcancel_timer, timer);
ee78dc32 14147
937e601e 14148 if (FRAMEP (frame))
6fc2811b 14149 {
937e601e
AI
14150 Fdelete_frame (frame, Qnil);
14151 deleted = Qt;
6fc2811b 14152 }
1edf84e7 14153
937e601e
AI
14154 UNGCPRO;
14155 return unbind_to (count, deleted);
6fc2811b 14156}
5ac45f98 14157
5ac45f98 14158
6fc2811b
JR
14159\f
14160/***********************************************************************
14161 File selection dialog
14162 ***********************************************************************/
14163
14164extern Lisp_Object Qfile_name_history;
14165
14166DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 4, 0,
74e1aeec
JR
14167 doc: /* Read file name, prompting with PROMPT in directory DIR.
14168Use a file selection dialog.
14169Select DEFAULT-FILENAME in the dialog's file selection box, if
14170specified. Ensure that file exists if MUSTMATCH is non-nil. */)
6fc2811b
JR
14171 (prompt, dir, default_filename, mustmatch)
14172 Lisp_Object prompt, dir, default_filename, mustmatch;
14173{
14174 struct frame *f = SELECTED_FRAME ();
14175 Lisp_Object file = Qnil;
14176 int count = specpdl_ptr - specpdl;
14177 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
14178 char filename[MAX_PATH + 1];
14179 char init_dir[MAX_PATH + 1];
14180 int use_dialog_p = 1;
14181
14182 GCPRO5 (prompt, dir, default_filename, mustmatch, file);
b7826503
PJ
14183 CHECK_STRING (prompt);
14184 CHECK_STRING (dir);
6fc2811b
JR
14185
14186 /* Create the dialog with PROMPT as title, using DIR as initial
14187 directory and using "*" as pattern. */
14188 dir = Fexpand_file_name (dir, Qnil);
14189 strncpy (init_dir, XSTRING (dir)->data, MAX_PATH);
14190 init_dir[MAX_PATH] = '\0';
14191 unixtodos_filename (init_dir);
14192
14193 if (STRINGP (default_filename))
14194 {
14195 char *file_name_only;
14196 char *full_path_name = XSTRING (default_filename)->data;
5ac45f98 14197
6fc2811b 14198 unixtodos_filename (full_path_name);
5ac45f98 14199
6fc2811b
JR
14200 file_name_only = strrchr (full_path_name, '\\');
14201 if (!file_name_only)
14202 file_name_only = full_path_name;
14203 else
14204 {
14205 file_name_only++;
5ac45f98 14206
6fc2811b
JR
14207 /* If default_file_name is a directory, don't use the open
14208 file dialog, as it does not support selecting
14209 directories. */
14210 if (!(*file_name_only))
14211 use_dialog_p = 0;
14212 }
ee78dc32 14213
6fc2811b
JR
14214 strncpy (filename, file_name_only, MAX_PATH);
14215 filename[MAX_PATH] = '\0';
14216 }
ee78dc32 14217 else
6fc2811b 14218 filename[0] = '\0';
ee78dc32 14219
6fc2811b
JR
14220 if (use_dialog_p)
14221 {
14222 OPENFILENAME file_details;
5ac45f98 14223
6fc2811b
JR
14224 /* Prevent redisplay. */
14225 specbind (Qinhibit_redisplay, Qt);
14226 BLOCK_INPUT;
ee78dc32 14227
6fc2811b
JR
14228 bzero (&file_details, sizeof (file_details));
14229 file_details.lStructSize = sizeof (file_details);
14230 file_details.hwndOwner = FRAME_W32_WINDOW (f);
3cf3436e
JR
14231 /* Undocumented Bug in Common File Dialog:
14232 If a filter is not specified, shell links are not resolved. */
14233 file_details.lpstrFilter = "ALL Files (*.*)\0*.*\0\0";
6fc2811b
JR
14234 file_details.lpstrFile = filename;
14235 file_details.nMaxFile = sizeof (filename);
14236 file_details.lpstrInitialDir = init_dir;
14237 file_details.lpstrTitle = XSTRING (prompt)->data;
14238 file_details.Flags = OFN_HIDEREADONLY | OFN_NOCHANGEDIR;
ee78dc32 14239
6fc2811b
JR
14240 if (!NILP (mustmatch))
14241 file_details.Flags |= OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
ee78dc32 14242
6fc2811b
JR
14243 if (GetOpenFileName (&file_details))
14244 {
14245 dostounix_filename (filename);
14246 file = build_string (filename);
14247 }
ee78dc32 14248 else
6fc2811b
JR
14249 file = Qnil;
14250
14251 UNBLOCK_INPUT;
14252 file = unbind_to (count, file);
ee78dc32 14253 }
6fc2811b
JR
14254 /* Open File dialog will not allow folders to be selected, so resort
14255 to minibuffer completing reads for directories. */
14256 else
14257 file = Fcompleting_read (prompt, intern ("read-file-name-internal"),
14258 dir, mustmatch, dir, Qfile_name_history,
14259 default_filename, Qnil);
ee78dc32 14260
6fc2811b 14261 UNGCPRO;
1edf84e7 14262
6fc2811b
JR
14263 /* Make "Cancel" equivalent to C-g. */
14264 if (NILP (file))
14265 Fsignal (Qquit, Qnil);
ee78dc32 14266
dfff8a69 14267 return unbind_to (count, file);
6fc2811b 14268}
ee78dc32 14269
ee78dc32 14270
6fc2811b 14271\f
6fc2811b
JR
14272/***********************************************************************
14273 w32 specialized functions
14274 ***********************************************************************/
ee78dc32 14275
d84b082d 14276DEFUN ("w32-select-font", Fw32_select_font, Sw32_select_font, 0, 2, 0,
74e1aeec
JR
14277 doc: /* Select a font using the W32 font dialog.
14278Returns an X font string corresponding to the selection. */)
d84b082d
JR
14279 (frame, include_proportional)
14280 Lisp_Object frame, include_proportional;
ee78dc32
GV
14281{
14282 FRAME_PTR f = check_x_frame (frame);
14283 CHOOSEFONT cf;
14284 LOGFONT lf;
f46e6225
GV
14285 TEXTMETRIC tm;
14286 HDC hdc;
14287 HANDLE oldobj;
ee78dc32
GV
14288 char buf[100];
14289
14290 bzero (&cf, sizeof (cf));
f46e6225 14291 bzero (&lf, sizeof (lf));
ee78dc32
GV
14292
14293 cf.lStructSize = sizeof (cf);
fbd6baed 14294 cf.hwndOwner = FRAME_W32_WINDOW (f);
d84b082d
JR
14295 cf.Flags = CF_FORCEFONTEXIST | CF_SCREENFONTS | CF_NOVERTFONTS;
14296
14297 /* Unless include_proportional is non-nil, limit the selection to
14298 monospaced fonts. */
14299 if (NILP (include_proportional))
14300 cf.Flags |= CF_FIXEDPITCHONLY;
14301
ee78dc32
GV
14302 cf.lpLogFont = &lf;
14303
f46e6225
GV
14304 /* Initialize as much of the font details as we can from the current
14305 default font. */
14306 hdc = GetDC (FRAME_W32_WINDOW (f));
14307 oldobj = SelectObject (hdc, FRAME_FONT (f)->hfont);
14308 GetTextFace (hdc, LF_FACESIZE, lf.lfFaceName);
14309 if (GetTextMetrics (hdc, &tm))
14310 {
14311 lf.lfHeight = tm.tmInternalLeading - tm.tmHeight;
14312 lf.lfWeight = tm.tmWeight;
14313 lf.lfItalic = tm.tmItalic;
14314 lf.lfUnderline = tm.tmUnderlined;
14315 lf.lfStrikeOut = tm.tmStruckOut;
f46e6225
GV
14316 lf.lfCharSet = tm.tmCharSet;
14317 cf.Flags |= CF_INITTOLOGFONTSTRUCT;
14318 }
14319 SelectObject (hdc, oldobj);
6fc2811b 14320 ReleaseDC (FRAME_W32_WINDOW (f), hdc);
f46e6225 14321
767b1ff0 14322 if (!ChooseFont (&cf) || !w32_to_x_font (&lf, buf, 100, NULL))
3c190163 14323 return Qnil;
ee78dc32
GV
14324
14325 return build_string (buf);
14326}
14327
74e1aeec
JR
14328DEFUN ("w32-send-sys-command", Fw32_send_sys_command,
14329 Sw32_send_sys_command, 1, 2, 0,
14330 doc: /* Send frame a Windows WM_SYSCOMMAND message of type COMMAND.
d84b082d
JR
14331Some useful values for command are #xf030 to maximise frame (#xf020
14332to minimize), #xf120 to restore frame to original size, and #xf100
14333to activate the menubar for keyboard access. #xf140 activates the
74e1aeec
JR
14334screen saver if defined.
14335
14336If optional parameter FRAME is not specified, use selected frame. */)
1edf84e7
GV
14337 (command, frame)
14338 Lisp_Object command, frame;
14339{
1edf84e7
GV
14340 FRAME_PTR f = check_x_frame (frame);
14341
b7826503 14342 CHECK_NUMBER (command);
1edf84e7 14343
ce6059da 14344 PostMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, XINT (command), 0);
1edf84e7
GV
14345
14346 return Qnil;
14347}
14348
55dcfc15 14349DEFUN ("w32-shell-execute", Fw32_shell_execute, Sw32_shell_execute, 2, 4, 0,
74e1aeec
JR
14350 doc: /* Get Windows to perform OPERATION on DOCUMENT.
14351This is a wrapper around the ShellExecute system function, which
14352invokes the application registered to handle OPERATION for DOCUMENT.
14353OPERATION is typically \"open\", \"print\" or \"explore\" (but can be
14354nil for the default action), and DOCUMENT is typically the name of a
14355document file or URL, but can also be a program executable to run or
14356a directory to open in the Windows Explorer.
14357
14358If DOCUMENT is a program executable, PARAMETERS can be a string
14359containing command line parameters, but otherwise should be nil.
14360
14361SHOW-FLAG can be used to control whether the invoked application is hidden
14362or minimized. If SHOW-FLAG is nil, the application is displayed normally,
14363otherwise it is an integer representing a ShowWindow flag:
14364
14365 0 - start hidden
14366 1 - start normally
14367 3 - start maximized
14368 6 - start minimized */)
55dcfc15
AI
14369 (operation, document, parameters, show_flag)
14370 Lisp_Object operation, document, parameters, show_flag;
14371{
14372 Lisp_Object current_dir;
14373
b7826503 14374 CHECK_STRING (document);
55dcfc15
AI
14375
14376 /* Encode filename and current directory. */
14377 current_dir = ENCODE_FILE (current_buffer->directory);
14378 document = ENCODE_FILE (document);
14379 if ((int) ShellExecute (NULL,
6fc2811b
JR
14380 (STRINGP (operation) ?
14381 XSTRING (operation)->data : NULL),
55dcfc15
AI
14382 XSTRING (document)->data,
14383 (STRINGP (parameters) ?
14384 XSTRING (parameters)->data : NULL),
14385 XSTRING (current_dir)->data,
14386 (INTEGERP (show_flag) ?
14387 XINT (show_flag) : SW_SHOWDEFAULT))
14388 > 32)
14389 return Qt;
90d97e64 14390 error ("ShellExecute failed: %s", w32_strerror (0));
55dcfc15
AI
14391}
14392
ccc2d29c
GV
14393/* Lookup virtual keycode from string representing the name of a
14394 non-ascii keystroke into the corresponding virtual key, using
14395 lispy_function_keys. */
14396static int
14397lookup_vk_code (char *key)
14398{
14399 int i;
14400
14401 for (i = 0; i < 256; i++)
14402 if (lispy_function_keys[i] != 0
14403 && strcmp (lispy_function_keys[i], key) == 0)
14404 return i;
14405
14406 return -1;
14407}
14408
14409/* Convert a one-element vector style key sequence to a hot key
14410 definition. */
14411static int
14412w32_parse_hot_key (key)
14413 Lisp_Object key;
14414{
14415 /* Copied from Fdefine_key and store_in_keymap. */
14416 register Lisp_Object c;
14417 int vk_code;
14418 int lisp_modifiers;
14419 int w32_modifiers;
14420 struct gcpro gcpro1;
14421
b7826503 14422 CHECK_VECTOR (key);
ccc2d29c
GV
14423
14424 if (XFASTINT (Flength (key)) != 1)
14425 return Qnil;
14426
14427 GCPRO1 (key);
14428
14429 c = Faref (key, make_number (0));
14430
14431 if (CONSP (c) && lucid_event_type_list_p (c))
14432 c = Fevent_convert_list (c);
14433
14434 UNGCPRO;
14435
14436 if (! INTEGERP (c) && ! SYMBOLP (c))
14437 error ("Key definition is invalid");
14438
14439 /* Work out the base key and the modifiers. */
14440 if (SYMBOLP (c))
14441 {
14442 c = parse_modifiers (c);
14443 lisp_modifiers = Fcar (Fcdr (c));
14444 c = Fcar (c);
14445 if (!SYMBOLP (c))
14446 abort ();
14447 vk_code = lookup_vk_code (XSYMBOL (c)->name->data);
14448 }
14449 else if (INTEGERP (c))
14450 {
14451 lisp_modifiers = XINT (c) & ~CHARACTERBITS;
14452 /* Many ascii characters are their own virtual key code. */
14453 vk_code = XINT (c) & CHARACTERBITS;
14454 }
14455
14456 if (vk_code < 0 || vk_code > 255)
14457 return Qnil;
14458
14459 if ((lisp_modifiers & meta_modifier) != 0
14460 && !NILP (Vw32_alt_is_meta))
14461 lisp_modifiers |= alt_modifier;
14462
71eab8d1
AI
14463 /* Supply defs missing from mingw32. */
14464#ifndef MOD_ALT
14465#define MOD_ALT 0x0001
14466#define MOD_CONTROL 0x0002
14467#define MOD_SHIFT 0x0004
14468#define MOD_WIN 0x0008
14469#endif
14470
ccc2d29c
GV
14471 /* Convert lisp modifiers to Windows hot-key form. */
14472 w32_modifiers = (lisp_modifiers & hyper_modifier) ? MOD_WIN : 0;
14473 w32_modifiers |= (lisp_modifiers & alt_modifier) ? MOD_ALT : 0;
14474 w32_modifiers |= (lisp_modifiers & ctrl_modifier) ? MOD_CONTROL : 0;
14475 w32_modifiers |= (lisp_modifiers & shift_modifier) ? MOD_SHIFT : 0;
14476
14477 return HOTKEY (vk_code, w32_modifiers);
14478}
14479
74e1aeec
JR
14480DEFUN ("w32-register-hot-key", Fw32_register_hot_key,
14481 Sw32_register_hot_key, 1, 1, 0,
14482 doc: /* Register KEY as a hot-key combination.
14483Certain key combinations like Alt-Tab are reserved for system use on
14484Windows, and therefore are normally intercepted by the system. However,
14485most of these key combinations can be received by registering them as
14486hot-keys, overriding their special meaning.
14487
14488KEY must be a one element key definition in vector form that would be
14489acceptable to `define-key' (e.g. [A-tab] for Alt-Tab). The meta
14490modifier is interpreted as Alt if `w32-alt-is-meta' is t, and hyper
14491is always interpreted as the Windows modifier keys.
14492
14493The return value is the hotkey-id if registered, otherwise nil. */)
ccc2d29c
GV
14494 (key)
14495 Lisp_Object key;
14496{
14497 key = w32_parse_hot_key (key);
14498
14499 if (NILP (Fmemq (key, w32_grabbed_keys)))
14500 {
14501 /* Reuse an empty slot if possible. */
14502 Lisp_Object item = Fmemq (Qnil, w32_grabbed_keys);
14503
14504 /* Safe to add new key to list, even if we have focus. */
14505 if (NILP (item))
14506 w32_grabbed_keys = Fcons (key, w32_grabbed_keys);
14507 else
f3fbd155 14508 XSETCAR (item, key);
ccc2d29c
GV
14509
14510 /* Notify input thread about new hot-key definition, so that it
14511 takes effect without needing to switch focus. */
14512 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
14513 (WPARAM) key, 0);
14514 }
14515
14516 return key;
14517}
14518
74e1aeec
JR
14519DEFUN ("w32-unregister-hot-key", Fw32_unregister_hot_key,
14520 Sw32_unregister_hot_key, 1, 1, 0,
14521 doc: /* Unregister HOTKEY as a hot-key combination. */)
ccc2d29c
GV
14522 (key)
14523 Lisp_Object key;
14524{
14525 Lisp_Object item;
14526
14527 if (!INTEGERP (key))
14528 key = w32_parse_hot_key (key);
14529
14530 item = Fmemq (key, w32_grabbed_keys);
14531
14532 if (!NILP (item))
14533 {
14534 /* Notify input thread about hot-key definition being removed, so
14535 that it takes effect without needing focus switch. */
14536 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
14537 (WPARAM) XINT (XCAR (item)), (LPARAM) item))
14538 {
14539 MSG msg;
14540 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
14541 }
14542 return Qt;
14543 }
14544 return Qnil;
14545}
14546
74e1aeec
JR
14547DEFUN ("w32-registered-hot-keys", Fw32_registered_hot_keys,
14548 Sw32_registered_hot_keys, 0, 0, 0,
14549 doc: /* Return list of registered hot-key IDs. */)
ccc2d29c
GV
14550 ()
14551{
14552 return Fcopy_sequence (w32_grabbed_keys);
14553}
14554
74e1aeec
JR
14555DEFUN ("w32-reconstruct-hot-key", Fw32_reconstruct_hot_key,
14556 Sw32_reconstruct_hot_key, 1, 1, 0,
14557 doc: /* Convert hot-key ID to a lisp key combination. */)
ccc2d29c
GV
14558 (hotkeyid)
14559 Lisp_Object hotkeyid;
14560{
14561 int vk_code, w32_modifiers;
14562 Lisp_Object key;
14563
b7826503 14564 CHECK_NUMBER (hotkeyid);
ccc2d29c
GV
14565
14566 vk_code = HOTKEY_VK_CODE (hotkeyid);
14567 w32_modifiers = HOTKEY_MODIFIERS (hotkeyid);
14568
14569 if (lispy_function_keys[vk_code])
14570 key = intern (lispy_function_keys[vk_code]);
14571 else
14572 key = make_number (vk_code);
14573
14574 key = Fcons (key, Qnil);
14575 if (w32_modifiers & MOD_SHIFT)
3ef68e6b 14576 key = Fcons (Qshift, key);
ccc2d29c 14577 if (w32_modifiers & MOD_CONTROL)
3ef68e6b 14578 key = Fcons (Qctrl, key);
ccc2d29c 14579 if (w32_modifiers & MOD_ALT)
3ef68e6b 14580 key = Fcons (NILP (Vw32_alt_is_meta) ? Qalt : Qmeta, key);
ccc2d29c 14581 if (w32_modifiers & MOD_WIN)
3ef68e6b 14582 key = Fcons (Qhyper, key);
ccc2d29c
GV
14583
14584 return key;
14585}
adcc3809 14586
74e1aeec
JR
14587DEFUN ("w32-toggle-lock-key", Fw32_toggle_lock_key,
14588 Sw32_toggle_lock_key, 1, 2, 0,
14589 doc: /* Toggle the state of the lock key KEY.
14590KEY can be `capslock', `kp-numlock', or `scroll'.
14591If the optional parameter NEW-STATE is a number, then the state of KEY
14592is set to off if the low bit of NEW-STATE is zero, otherwise on. */)
adcc3809
GV
14593 (key, new_state)
14594 Lisp_Object key, new_state;
14595{
14596 int vk_code;
adcc3809
GV
14597
14598 if (EQ (key, intern ("capslock")))
14599 vk_code = VK_CAPITAL;
14600 else if (EQ (key, intern ("kp-numlock")))
14601 vk_code = VK_NUMLOCK;
14602 else if (EQ (key, intern ("scroll")))
14603 vk_code = VK_SCROLL;
14604 else
14605 return Qnil;
14606
14607 if (!dwWindowsThreadId)
14608 return make_number (w32_console_toggle_lock_key (vk_code, new_state));
14609
14610 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
14611 (WPARAM) vk_code, (LPARAM) new_state))
14612 {
14613 MSG msg;
14614 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
14615 return make_number (msg.wParam);
14616 }
14617 return Qnil;
14618}
ee78dc32 14619\f
2254bcde 14620DEFUN ("file-system-info", Ffile_system_info, Sfile_system_info, 1, 1, 0,
74e1aeec
JR
14621 doc: /* Return storage information about the file system FILENAME is on.
14622Value is a list of floats (TOTAL FREE AVAIL), where TOTAL is the total
14623storage of the file system, FREE is the free storage, and AVAIL is the
14624storage available to a non-superuser. All 3 numbers are in bytes.
14625If the underlying system call fails, value is nil. */)
2254bcde
AI
14626 (filename)
14627 Lisp_Object filename;
14628{
14629 Lisp_Object encoded, value;
14630
b7826503 14631 CHECK_STRING (filename);
2254bcde
AI
14632 filename = Fexpand_file_name (filename, Qnil);
14633 encoded = ENCODE_FILE (filename);
14634
14635 value = Qnil;
14636
14637 /* Determining the required information on Windows turns out, sadly,
14638 to be more involved than one would hope. The original Win32 api
14639 call for this will return bogus information on some systems, but we
14640 must dynamically probe for the replacement api, since that was
14641 added rather late on. */
14642 {
14643 HMODULE hKernel = GetModuleHandle ("kernel32");
14644 BOOL (*pfn_GetDiskFreeSpaceEx)
14645 (char *, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER)
14646 = (void *) GetProcAddress (hKernel, "GetDiskFreeSpaceEx");
14647
14648 /* On Windows, we may need to specify the root directory of the
14649 volume holding FILENAME. */
14650 char rootname[MAX_PATH];
14651 char *name = XSTRING (encoded)->data;
14652
14653 /* find the root name of the volume if given */
14654 if (isalpha (name[0]) && name[1] == ':')
14655 {
14656 rootname[0] = name[0];
14657 rootname[1] = name[1];
14658 rootname[2] = '\\';
14659 rootname[3] = 0;
14660 }
14661 else if (IS_DIRECTORY_SEP (name[0]) && IS_DIRECTORY_SEP (name[1]))
14662 {
14663 char *str = rootname;
14664 int slashes = 4;
14665 do
14666 {
14667 if (IS_DIRECTORY_SEP (*name) && --slashes == 0)
14668 break;
14669 *str++ = *name++;
14670 }
14671 while ( *name );
14672
14673 *str++ = '\\';
14674 *str = 0;
14675 }
14676
14677 if (pfn_GetDiskFreeSpaceEx)
14678 {
ac849ba4
JR
14679 /* Unsigned large integers cannot be cast to double, so
14680 use signed ones instead. */
2254bcde
AI
14681 LARGE_INTEGER availbytes;
14682 LARGE_INTEGER freebytes;
14683 LARGE_INTEGER totalbytes;
14684
14685 if (pfn_GetDiskFreeSpaceEx(rootname,
ac849ba4
JR
14686 (ULARGE_INTEGER *)&availbytes,
14687 (ULARGE_INTEGER *)&totalbytes,
14688 (ULARGE_INTEGER *)&freebytes))
2254bcde
AI
14689 value = list3 (make_float ((double) totalbytes.QuadPart),
14690 make_float ((double) freebytes.QuadPart),
14691 make_float ((double) availbytes.QuadPart));
14692 }
14693 else
14694 {
14695 DWORD sectors_per_cluster;
14696 DWORD bytes_per_sector;
14697 DWORD free_clusters;
14698 DWORD total_clusters;
14699
14700 if (GetDiskFreeSpace(rootname,
14701 &sectors_per_cluster,
14702 &bytes_per_sector,
14703 &free_clusters,
14704 &total_clusters))
14705 value = list3 (make_float ((double) total_clusters
14706 * sectors_per_cluster * bytes_per_sector),
14707 make_float ((double) free_clusters
14708 * sectors_per_cluster * bytes_per_sector),
14709 make_float ((double) free_clusters
14710 * sectors_per_cluster * bytes_per_sector));
14711 }
14712 }
14713
14714 return value;
14715}
14716\f
0e3fcdef
JR
14717/***********************************************************************
14718 Initialization
14719 ***********************************************************************/
14720
14721void
fbd6baed 14722syms_of_w32fns ()
ee78dc32 14723{
9eb16b62
JR
14724 HMODULE user32_lib = GetModuleHandle ("user32.dll");
14725
1edf84e7
GV
14726 /* This is zero if not using MS-Windows. */
14727 w32_in_use = 0;
14728
9eb16b62
JR
14729 /* TrackMouseEvent not available in all versions of Windows, so must load
14730 it dynamically. Do it once, here, instead of every time it is used. */
14731 track_mouse_event_fn = GetProcAddress (user32_lib, "TrackMouseEvent");
14732 track_mouse_window = NULL;
14733
d285988b
JR
14734 w32_visible_system_caret_hwnd = NULL;
14735
ee78dc32
GV
14736 Qauto_raise = intern ("auto-raise");
14737 staticpro (&Qauto_raise);
14738 Qauto_lower = intern ("auto-lower");
14739 staticpro (&Qauto_lower);
ee78dc32
GV
14740 Qbar = intern ("bar");
14741 staticpro (&Qbar);
14742 Qborder_color = intern ("border-color");
14743 staticpro (&Qborder_color);
14744 Qborder_width = intern ("border-width");
14745 staticpro (&Qborder_width);
14746 Qbox = intern ("box");
14747 staticpro (&Qbox);
14748 Qcursor_color = intern ("cursor-color");
14749 staticpro (&Qcursor_color);
14750 Qcursor_type = intern ("cursor-type");
14751 staticpro (&Qcursor_type);
ee78dc32
GV
14752 Qgeometry = intern ("geometry");
14753 staticpro (&Qgeometry);
14754 Qicon_left = intern ("icon-left");
14755 staticpro (&Qicon_left);
14756 Qicon_top = intern ("icon-top");
14757 staticpro (&Qicon_top);
14758 Qicon_type = intern ("icon-type");
14759 staticpro (&Qicon_type);
14760 Qicon_name = intern ("icon-name");
14761 staticpro (&Qicon_name);
14762 Qinternal_border_width = intern ("internal-border-width");
14763 staticpro (&Qinternal_border_width);
14764 Qleft = intern ("left");
14765 staticpro (&Qleft);
1026b400
RS
14766 Qright = intern ("right");
14767 staticpro (&Qright);
ee78dc32
GV
14768 Qmouse_color = intern ("mouse-color");
14769 staticpro (&Qmouse_color);
14770 Qnone = intern ("none");
14771 staticpro (&Qnone);
14772 Qparent_id = intern ("parent-id");
14773 staticpro (&Qparent_id);
14774 Qscroll_bar_width = intern ("scroll-bar-width");
14775 staticpro (&Qscroll_bar_width);
14776 Qsuppress_icon = intern ("suppress-icon");
14777 staticpro (&Qsuppress_icon);
ee78dc32
GV
14778 Qundefined_color = intern ("undefined-color");
14779 staticpro (&Qundefined_color);
14780 Qvertical_scroll_bars = intern ("vertical-scroll-bars");
14781 staticpro (&Qvertical_scroll_bars);
14782 Qvisibility = intern ("visibility");
14783 staticpro (&Qvisibility);
14784 Qwindow_id = intern ("window-id");
14785 staticpro (&Qwindow_id);
14786 Qx_frame_parameter = intern ("x-frame-parameter");
14787 staticpro (&Qx_frame_parameter);
14788 Qx_resource_name = intern ("x-resource-name");
14789 staticpro (&Qx_resource_name);
14790 Quser_position = intern ("user-position");
14791 staticpro (&Quser_position);
14792 Quser_size = intern ("user-size");
14793 staticpro (&Quser_size);
6fc2811b
JR
14794 Qscreen_gamma = intern ("screen-gamma");
14795 staticpro (&Qscreen_gamma);
dfff8a69
JR
14796 Qline_spacing = intern ("line-spacing");
14797 staticpro (&Qline_spacing);
14798 Qcenter = intern ("center");
14799 staticpro (&Qcenter);
dc220243
JR
14800 Qcancel_timer = intern ("cancel-timer");
14801 staticpro (&Qcancel_timer);
f7b9d4d1
JR
14802 Qfullscreen = intern ("fullscreen");
14803 staticpro (&Qfullscreen);
14804 Qfullwidth = intern ("fullwidth");
14805 staticpro (&Qfullwidth);
14806 Qfullheight = intern ("fullheight");
14807 staticpro (&Qfullheight);
14808 Qfullboth = intern ("fullboth");
14809 staticpro (&Qfullboth);
ee78dc32 14810
adcc3809
GV
14811 Qhyper = intern ("hyper");
14812 staticpro (&Qhyper);
14813 Qsuper = intern ("super");
14814 staticpro (&Qsuper);
14815 Qmeta = intern ("meta");
14816 staticpro (&Qmeta);
14817 Qalt = intern ("alt");
14818 staticpro (&Qalt);
14819 Qctrl = intern ("ctrl");
14820 staticpro (&Qctrl);
14821 Qcontrol = intern ("control");
14822 staticpro (&Qcontrol);
14823 Qshift = intern ("shift");
14824 staticpro (&Qshift);
f7b9d4d1 14825 /* This is the end of symbol initialization. */
adcc3809 14826
6fc2811b
JR
14827 /* Text property `display' should be nonsticky by default. */
14828 Vtext_property_default_nonsticky
14829 = Fcons (Fcons (Qdisplay, Qt), Vtext_property_default_nonsticky);
14830
14831
14832 Qlaplace = intern ("laplace");
14833 staticpro (&Qlaplace);
3cf3436e
JR
14834 Qemboss = intern ("emboss");
14835 staticpro (&Qemboss);
14836 Qedge_detection = intern ("edge-detection");
14837 staticpro (&Qedge_detection);
14838 Qheuristic = intern ("heuristic");
14839 staticpro (&Qheuristic);
14840 QCmatrix = intern (":matrix");
14841 staticpro (&QCmatrix);
14842 QCcolor_adjustment = intern (":color-adjustment");
14843 staticpro (&QCcolor_adjustment);
14844 QCmask = intern (":mask");
14845 staticpro (&QCmask);
6fc2811b 14846
4b817373
RS
14847 Qface_set_after_frame_default = intern ("face-set-after-frame-default");
14848 staticpro (&Qface_set_after_frame_default);
14849
ee78dc32
GV
14850 Fput (Qundefined_color, Qerror_conditions,
14851 Fcons (Qundefined_color, Fcons (Qerror, Qnil)));
14852 Fput (Qundefined_color, Qerror_message,
14853 build_string ("Undefined color"));
14854
ccc2d29c
GV
14855 staticpro (&w32_grabbed_keys);
14856 w32_grabbed_keys = Qnil;
14857
fbd6baed 14858 DEFVAR_LISP ("w32-color-map", &Vw32_color_map,
74e1aeec 14859 doc: /* An array of color name mappings for windows. */);
fbd6baed 14860 Vw32_color_map = Qnil;
ee78dc32 14861
fbd6baed 14862 DEFVAR_LISP ("w32-pass-alt-to-system", &Vw32_pass_alt_to_system,
74e1aeec
JR
14863 doc: /* Non-nil if alt key presses are passed on to Windows.
14864When non-nil, for example, alt pressed and released and then space will
14865open the System menu. When nil, Emacs silently swallows alt key events. */);
fbd6baed 14866 Vw32_pass_alt_to_system = Qnil;
da36a4d6 14867
fbd6baed 14868 DEFVAR_LISP ("w32-alt-is-meta", &Vw32_alt_is_meta,
74e1aeec
JR
14869 doc: /* Non-nil if the alt key is to be considered the same as the meta key.
14870When nil, Emacs will translate the alt key to the Alt modifier, and not Meta. */);
fbd6baed 14871 Vw32_alt_is_meta = Qt;
8c205c63 14872
7d081355 14873 DEFVAR_INT ("w32-quit-key", &Vw32_quit_key,
74e1aeec 14874 doc: /* If non-zero, the virtual key code for an alternative quit key. */);
7d081355
AI
14875 XSETINT (Vw32_quit_key, 0);
14876
ccc2d29c
GV
14877 DEFVAR_LISP ("w32-pass-lwindow-to-system",
14878 &Vw32_pass_lwindow_to_system,
74e1aeec
JR
14879 doc: /* Non-nil if the left \"Windows\" key is passed on to Windows.
14880When non-nil, the Start menu is opened by tapping the key. */);
ccc2d29c
GV
14881 Vw32_pass_lwindow_to_system = Qt;
14882
14883 DEFVAR_LISP ("w32-pass-rwindow-to-system",
14884 &Vw32_pass_rwindow_to_system,
74e1aeec
JR
14885 doc: /* Non-nil if the right \"Windows\" key is passed on to Windows.
14886When non-nil, the Start menu is opened by tapping the key. */);
ccc2d29c
GV
14887 Vw32_pass_rwindow_to_system = Qt;
14888
adcc3809
GV
14889 DEFVAR_INT ("w32-phantom-key-code",
14890 &Vw32_phantom_key_code,
74e1aeec
JR
14891 doc: /* Virtual key code used to generate \"phantom\" key presses.
14892Value is a number between 0 and 255.
14893
14894Phantom key presses are generated in order to stop the system from
14895acting on \"Windows\" key events when `w32-pass-lwindow-to-system' or
14896`w32-pass-rwindow-to-system' is nil. */);
ce6059da
AI
14897 /* Although 255 is technically not a valid key code, it works and
14898 means that this hack won't interfere with any real key code. */
14899 Vw32_phantom_key_code = 255;
adcc3809 14900
ccc2d29c
GV
14901 DEFVAR_LISP ("w32-enable-num-lock",
14902 &Vw32_enable_num_lock,
74e1aeec
JR
14903 doc: /* Non-nil if Num Lock should act normally.
14904Set to nil to see Num Lock as the key `kp-numlock'. */);
ccc2d29c
GV
14905 Vw32_enable_num_lock = Qt;
14906
14907 DEFVAR_LISP ("w32-enable-caps-lock",
14908 &Vw32_enable_caps_lock,
74e1aeec
JR
14909 doc: /* Non-nil if Caps Lock should act normally.
14910Set to nil to see Caps Lock as the key `capslock'. */);
ccc2d29c
GV
14911 Vw32_enable_caps_lock = Qt;
14912
14913 DEFVAR_LISP ("w32-scroll-lock-modifier",
14914 &Vw32_scroll_lock_modifier,
74e1aeec
JR
14915 doc: /* Modifier to use for the Scroll Lock on state.
14916The value can be hyper, super, meta, alt, control or shift for the
14917respective modifier, or nil to see Scroll Lock as the key `scroll'.
14918Any other value will cause the key to be ignored. */);
ccc2d29c
GV
14919 Vw32_scroll_lock_modifier = Qt;
14920
14921 DEFVAR_LISP ("w32-lwindow-modifier",
14922 &Vw32_lwindow_modifier,
74e1aeec
JR
14923 doc: /* Modifier to use for the left \"Windows\" key.
14924The value can be hyper, super, meta, alt, control or shift for the
14925respective modifier, or nil to appear as the key `lwindow'.
14926Any other value will cause the key to be ignored. */);
ccc2d29c
GV
14927 Vw32_lwindow_modifier = Qnil;
14928
14929 DEFVAR_LISP ("w32-rwindow-modifier",
14930 &Vw32_rwindow_modifier,
74e1aeec
JR
14931 doc: /* Modifier to use for the right \"Windows\" key.
14932The value can be hyper, super, meta, alt, control or shift for the
14933respective modifier, or nil to appear as the key `rwindow'.
14934Any other value will cause the key to be ignored. */);
ccc2d29c
GV
14935 Vw32_rwindow_modifier = Qnil;
14936
14937 DEFVAR_LISP ("w32-apps-modifier",
14938 &Vw32_apps_modifier,
74e1aeec
JR
14939 doc: /* Modifier to use for the \"Apps\" key.
14940The value can be hyper, super, meta, alt, control or shift for the
14941respective modifier, or nil to appear as the key `apps'.
14942Any other value will cause the key to be ignored. */);
ccc2d29c 14943 Vw32_apps_modifier = Qnil;
da36a4d6 14944
d84b082d 14945 DEFVAR_BOOL ("w32-enable-synthesized-fonts", &w32_enable_synthesized_fonts,
74e1aeec 14946 doc: /* Non-nil enables selection of artificially italicized and bold fonts. */);
d84b082d 14947 w32_enable_synthesized_fonts = 0;
5ac45f98 14948
fbd6baed 14949 DEFVAR_LISP ("w32-enable-palette", &Vw32_enable_palette,
74e1aeec 14950 doc: /* Non-nil enables Windows palette management to map colors exactly. */);
fbd6baed 14951 Vw32_enable_palette = Qt;
5ac45f98 14952
fbd6baed
GV
14953 DEFVAR_INT ("w32-mouse-button-tolerance",
14954 &Vw32_mouse_button_tolerance,
74e1aeec
JR
14955 doc: /* Analogue of double click interval for faking middle mouse events.
14956The value is the minimum time in milliseconds that must elapse between
14957left/right button down events before they are considered distinct events.
14958If both mouse buttons are depressed within this interval, a middle mouse
14959button down event is generated instead. */);
fbd6baed 14960 XSETINT (Vw32_mouse_button_tolerance, GetDoubleClickTime () / 2);
5ac45f98 14961
fbd6baed
GV
14962 DEFVAR_INT ("w32-mouse-move-interval",
14963 &Vw32_mouse_move_interval,
74e1aeec
JR
14964 doc: /* Minimum interval between mouse move events.
14965The value is the minimum time in milliseconds that must elapse between
14966successive mouse move (or scroll bar drag) events before they are
14967reported as lisp events. */);
247be837 14968 XSETINT (Vw32_mouse_move_interval, 0);
84fb1139 14969
74214547
JR
14970 DEFVAR_BOOL ("w32-pass-extra-mouse-buttons-to-system",
14971 &w32_pass_extra_mouse_buttons_to_system,
14972 doc: /* Non-nil if the fourth and fifth mouse buttons are passed to Windows.
14973Recent versions of Windows support mice with up to five buttons.
14974Since most applications don't support these extra buttons, most mouse
14975drivers will allow you to map them to functions at the system level.
14976If this variable is non-nil, Emacs will pass them on, allowing the
14977system to handle them. */);
14978 w32_pass_extra_mouse_buttons_to_system = 0;
14979
ee78dc32
GV
14980 init_x_parm_symbols ();
14981
14982 DEFVAR_LISP ("x-bitmap-file-path", &Vx_bitmap_file_path,
74e1aeec 14983 doc: /* List of directories to search for bitmap files for w32. */);
ee78dc32
GV
14984 Vx_bitmap_file_path = decode_env_path ((char *) 0, "PATH");
14985
14986 DEFVAR_LISP ("x-pointer-shape", &Vx_pointer_shape,
74e1aeec
JR
14987 doc: /* The shape of the pointer when over text.
14988Changing the value does not affect existing frames
14989unless you set the mouse color. */);
ee78dc32
GV
14990 Vx_pointer_shape = Qnil;
14991
14992 DEFVAR_LISP ("x-resource-name", &Vx_resource_name,
74e1aeec
JR
14993 doc: /* The name Emacs uses to look up resources; for internal use only.
14994`x-get-resource' uses this as the first component of the instance name
14995when requesting resource values.
14996Emacs initially sets `x-resource-name' to the name under which Emacs
14997was invoked, or to the value specified with the `-name' or `-rn'
14998switches, if present. */);
ee78dc32
GV
14999 Vx_resource_name = Qnil;
15000
15001 Vx_nontext_pointer_shape = Qnil;
15002
15003 Vx_mode_pointer_shape = Qnil;
15004
0af913d7 15005 DEFVAR_LISP ("x-hourglass-pointer-shape", &Vx_hourglass_pointer_shape,
74e1aeec
JR
15006 doc: /* The shape of the pointer when Emacs is busy.
15007This variable takes effect when you create a new frame
15008or when you set the mouse color. */);
0af913d7 15009 Vx_hourglass_pointer_shape = Qnil;
6fc2811b 15010
0af913d7 15011 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
74e1aeec 15012 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
0af913d7 15013 display_hourglass_p = 1;
6fc2811b 15014
0af913d7 15015 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
74e1aeec
JR
15016 doc: /* *Seconds to wait before displaying an hourglass pointer.
15017Value must be an integer or float. */);
0af913d7 15018 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
f79e6790 15019
6fc2811b 15020 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
ee78dc32 15021 &Vx_sensitive_text_pointer_shape,
74e1aeec
JR
15022 doc: /* The shape of the pointer when over mouse-sensitive text.
15023This variable takes effect when you create a new frame
15024or when you set the mouse color. */);
ee78dc32
GV
15025 Vx_sensitive_text_pointer_shape = Qnil;
15026
4694d762
JR
15027 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
15028 &Vx_window_horizontal_drag_shape,
74e1aeec
JR
15029 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
15030This variable takes effect when you create a new frame
15031or when you set the mouse color. */);
4694d762
JR
15032 Vx_window_horizontal_drag_shape = Qnil;
15033
ee78dc32 15034 DEFVAR_LISP ("x-cursor-fore-pixel", &Vx_cursor_fore_pixel,
74e1aeec 15035 doc: /* A string indicating the foreground color of the cursor box. */);
ee78dc32
GV
15036 Vx_cursor_fore_pixel = Qnil;
15037
3cf3436e 15038 DEFVAR_LISP ("x-max-tooltip-size", &Vx_max_tooltip_size,
b3700ae7
JR
15039 doc: /* Maximum size for tooltips.
15040Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
3cf3436e
JR
15041 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
15042
ee78dc32 15043 DEFVAR_LISP ("x-no-window-manager", &Vx_no_window_manager,
74e1aeec
JR
15044 doc: /* Non-nil if no window manager is in use.
15045Emacs doesn't try to figure this out; this is always nil
15046unless you set it to something else. */);
ee78dc32
GV
15047 /* We don't have any way to find this out, so set it to nil
15048 and maybe the user would like to set it to t. */
15049 Vx_no_window_manager = Qnil;
15050
4587b026
GV
15051 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
15052 &Vx_pixel_size_width_font_regexp,
74e1aeec
JR
15053 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
15054
15055Since Emacs gets width of a font matching with this regexp from
15056PIXEL_SIZE field of the name, font finding mechanism gets faster for
15057such a font. This is especially effective for such large fonts as
15058Chinese, Japanese, and Korean. */);
4587b026
GV
15059 Vx_pixel_size_width_font_regexp = Qnil;
15060
6fc2811b 15061 DEFVAR_LISP ("image-cache-eviction-delay", &Vimage_cache_eviction_delay,
74e1aeec
JR
15062 doc: /* Time after which cached images are removed from the cache.
15063When an image has not been displayed this many seconds, remove it
15064from the image cache. Value must be an integer or nil with nil
15065meaning don't clear the cache. */);
6fc2811b
JR
15066 Vimage_cache_eviction_delay = make_number (30 * 60);
15067
33d52f9c
GV
15068 DEFVAR_LISP ("w32-bdf-filename-alist",
15069 &Vw32_bdf_filename_alist,
74e1aeec 15070 doc: /* List of bdf fonts and their corresponding filenames. */);
33d52f9c
GV
15071 Vw32_bdf_filename_alist = Qnil;
15072
1075afa9
GV
15073 DEFVAR_BOOL ("w32-strict-fontnames",
15074 &w32_strict_fontnames,
74e1aeec
JR
15075 doc: /* Non-nil means only use fonts that are exact matches for those requested.
15076Default is nil, which allows old fontnames that are not XLFD compliant,
15077and allows third-party CJK display to work by specifying false charset
15078fields to trick Emacs into translating to Big5, SJIS etc.
15079Setting this to t will prevent wrong fonts being selected when
15080fontsets are automatically created. */);
1075afa9
GV
15081 w32_strict_fontnames = 0;
15082
c0611964
AI
15083 DEFVAR_BOOL ("w32-strict-painting",
15084 &w32_strict_painting,
74e1aeec
JR
15085 doc: /* Non-nil means use strict rules for repainting frames.
15086Set this to nil to get the old behaviour for repainting; this should
15087only be necessary if the default setting causes problems. */);
c0611964
AI
15088 w32_strict_painting = 1;
15089
dfff8a69
JR
15090 DEFVAR_LISP ("w32-charset-info-alist",
15091 &Vw32_charset_info_alist,
b3700ae7
JR
15092 doc: /* Alist linking Emacs character sets to Windows fonts and codepages.
15093Each entry should be of the form:
74e1aeec
JR
15094
15095 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE))
15096
15097where CHARSET_NAME is a string used in font names to identify the charset,
15098WINDOWS_CHARSET is a symbol that can be one of:
15099w32-charset-ansi, w32-charset-default, w32-charset-symbol,
15100w32-charset-shiftjis, w32-charset-hangeul, w32-charset-gb2312,
15101w32-charset-chinesebig5,
dfff8a69 15102#ifdef JOHAB_CHARSET
74e1aeec
JR
15103w32-charset-johab, w32-charset-hebrew,
15104w32-charset-arabic, w32-charset-greek, w32-charset-turkish,
15105w32-charset-vietnamese, w32-charset-thai, w32-charset-easteurope,
15106w32-charset-russian, w32-charset-mac, w32-charset-baltic,
dfff8a69
JR
15107#endif
15108#ifdef UNICODE_CHARSET
74e1aeec 15109w32-charset-unicode,
dfff8a69 15110#endif
74e1aeec
JR
15111or w32-charset-oem.
15112CODEPAGE should be an integer specifying the codepage that should be used
15113to display the character set, t to do no translation and output as Unicode,
15114or nil to do no translation and output as 8 bit (or multibyte on far-east
15115versions of Windows) characters. */);
dfff8a69
JR
15116 Vw32_charset_info_alist = Qnil;
15117
15118 staticpro (&Qw32_charset_ansi);
15119 Qw32_charset_ansi = intern ("w32-charset-ansi");
15120 staticpro (&Qw32_charset_symbol);
15121 Qw32_charset_symbol = intern ("w32-charset-symbol");
15122 staticpro (&Qw32_charset_shiftjis);
15123 Qw32_charset_shiftjis = intern ("w32-charset-shiftjis");
767b1ff0
JR
15124 staticpro (&Qw32_charset_hangeul);
15125 Qw32_charset_hangeul = intern ("w32-charset-hangeul");
dfff8a69
JR
15126 staticpro (&Qw32_charset_chinesebig5);
15127 Qw32_charset_chinesebig5 = intern ("w32-charset-chinesebig5");
15128 staticpro (&Qw32_charset_gb2312);
15129 Qw32_charset_gb2312 = intern ("w32-charset-gb2312");
15130 staticpro (&Qw32_charset_oem);
15131 Qw32_charset_oem = intern ("w32-charset-oem");
15132
15133#ifdef JOHAB_CHARSET
15134 {
15135 static int w32_extra_charsets_defined = 1;
74e1aeec
JR
15136 DEFVAR_BOOL ("w32-extra-charsets-defined", &w32_extra_charsets_defined,
15137 doc: /* Internal variable. */);
dfff8a69
JR
15138
15139 staticpro (&Qw32_charset_johab);
15140 Qw32_charset_johab = intern ("w32-charset-johab");
15141 staticpro (&Qw32_charset_easteurope);
15142 Qw32_charset_easteurope = intern ("w32-charset-easteurope");
15143 staticpro (&Qw32_charset_turkish);
15144 Qw32_charset_turkish = intern ("w32-charset-turkish");
15145 staticpro (&Qw32_charset_baltic);
15146 Qw32_charset_baltic = intern ("w32-charset-baltic");
15147 staticpro (&Qw32_charset_russian);
15148 Qw32_charset_russian = intern ("w32-charset-russian");
15149 staticpro (&Qw32_charset_arabic);
15150 Qw32_charset_arabic = intern ("w32-charset-arabic");
15151 staticpro (&Qw32_charset_greek);
15152 Qw32_charset_greek = intern ("w32-charset-greek");
15153 staticpro (&Qw32_charset_hebrew);
15154 Qw32_charset_hebrew = intern ("w32-charset-hebrew");
767b1ff0
JR
15155 staticpro (&Qw32_charset_vietnamese);
15156 Qw32_charset_vietnamese = intern ("w32-charset-vietnamese");
dfff8a69
JR
15157 staticpro (&Qw32_charset_thai);
15158 Qw32_charset_thai = intern ("w32-charset-thai");
15159 staticpro (&Qw32_charset_mac);
15160 Qw32_charset_mac = intern ("w32-charset-mac");
15161 }
15162#endif
15163
15164#ifdef UNICODE_CHARSET
15165 {
15166 static int w32_unicode_charset_defined = 1;
15167 DEFVAR_BOOL ("w32-unicode-charset-defined",
74e1aeec
JR
15168 &w32_unicode_charset_defined,
15169 doc: /* Internal variable. */);
dfff8a69
JR
15170
15171 staticpro (&Qw32_charset_unicode);
15172 Qw32_charset_unicode = intern ("w32-charset-unicode");
15173#endif
15174
ee78dc32 15175 defsubr (&Sx_get_resource);
767b1ff0 15176#if 0 /* TODO: Port to W32 */
6fc2811b
JR
15177 defsubr (&Sx_change_window_property);
15178 defsubr (&Sx_delete_window_property);
15179 defsubr (&Sx_window_property);
15180#endif
2d764c78 15181 defsubr (&Sxw_display_color_p);
ee78dc32 15182 defsubr (&Sx_display_grayscale_p);
2d764c78
EZ
15183 defsubr (&Sxw_color_defined_p);
15184 defsubr (&Sxw_color_values);
ee78dc32
GV
15185 defsubr (&Sx_server_max_request_size);
15186 defsubr (&Sx_server_vendor);
15187 defsubr (&Sx_server_version);
15188 defsubr (&Sx_display_pixel_width);
15189 defsubr (&Sx_display_pixel_height);
15190 defsubr (&Sx_display_mm_width);
15191 defsubr (&Sx_display_mm_height);
15192 defsubr (&Sx_display_screens);
15193 defsubr (&Sx_display_planes);
15194 defsubr (&Sx_display_color_cells);
15195 defsubr (&Sx_display_visual_class);
15196 defsubr (&Sx_display_backing_store);
15197 defsubr (&Sx_display_save_under);
15198 defsubr (&Sx_parse_geometry);
15199 defsubr (&Sx_create_frame);
ee78dc32
GV
15200 defsubr (&Sx_open_connection);
15201 defsubr (&Sx_close_connection);
15202 defsubr (&Sx_display_list);
15203 defsubr (&Sx_synchronize);
15204
fbd6baed 15205 /* W32 specific functions */
ee78dc32 15206
1edf84e7 15207 defsubr (&Sw32_focus_frame);
fbd6baed
GV
15208 defsubr (&Sw32_select_font);
15209 defsubr (&Sw32_define_rgb_color);
15210 defsubr (&Sw32_default_color_map);
15211 defsubr (&Sw32_load_color_file);
1edf84e7 15212 defsubr (&Sw32_send_sys_command);
55dcfc15 15213 defsubr (&Sw32_shell_execute);
ccc2d29c
GV
15214 defsubr (&Sw32_register_hot_key);
15215 defsubr (&Sw32_unregister_hot_key);
15216 defsubr (&Sw32_registered_hot_keys);
15217 defsubr (&Sw32_reconstruct_hot_key);
adcc3809 15218 defsubr (&Sw32_toggle_lock_key);
33d52f9c 15219 defsubr (&Sw32_find_bdf_fonts);
4587b026 15220
2254bcde
AI
15221 defsubr (&Sfile_system_info);
15222
4587b026
GV
15223 /* Setting callback functions for fontset handler. */
15224 get_font_info_func = w32_get_font_info;
6fc2811b
JR
15225
15226#if 0 /* This function pointer doesn't seem to be used anywhere.
15227 And the pointer assigned has the wrong type, anyway. */
4587b026 15228 list_fonts_func = w32_list_fonts;
6fc2811b
JR
15229#endif
15230
4587b026
GV
15231 load_font_func = w32_load_font;
15232 find_ccl_program_func = w32_find_ccl_program;
15233 query_font_func = w32_query_font;
15234 set_frame_fontset_func = x_set_font;
15235 check_window_system_func = check_w32;
6fc2811b 15236
ac849ba4 15237#ifdef IMAGES
6fc2811b
JR
15238 /* Images. */
15239 Qxbm = intern ("xbm");
15240 staticpro (&Qxbm);
a93f4566
GM
15241 QCconversion = intern (":conversion");
15242 staticpro (&QCconversion);
6fc2811b
JR
15243 QCheuristic_mask = intern (":heuristic-mask");
15244 staticpro (&QCheuristic_mask);
15245 QCcolor_symbols = intern (":color-symbols");
15246 staticpro (&QCcolor_symbols);
6fc2811b
JR
15247 QCascent = intern (":ascent");
15248 staticpro (&QCascent);
15249 QCmargin = intern (":margin");
15250 staticpro (&QCmargin);
15251 QCrelief = intern (":relief");
15252 staticpro (&QCrelief);
15253 Qpostscript = intern ("postscript");
15254 staticpro (&Qpostscript);
ac849ba4 15255#if 0 /* TODO: These need entries at top of file. */
6fc2811b
JR
15256 QCloader = intern (":loader");
15257 staticpro (&QCloader);
15258 QCbounding_box = intern (":bounding-box");
15259 staticpro (&QCbounding_box);
15260 QCpt_width = intern (":pt-width");
15261 staticpro (&QCpt_width);
15262 QCpt_height = intern (":pt-height");
15263 staticpro (&QCpt_height);
ac849ba4 15264#endif
6fc2811b
JR
15265 QCindex = intern (":index");
15266 staticpro (&QCindex);
15267 Qpbm = intern ("pbm");
15268 staticpro (&Qpbm);
ac849ba4 15269#endif
6fc2811b
JR
15270
15271#if HAVE_XPM
15272 Qxpm = intern ("xpm");
15273 staticpro (&Qxpm);
15274#endif
15275
15276#if HAVE_JPEG
15277 Qjpeg = intern ("jpeg");
15278 staticpro (&Qjpeg);
15279#endif
15280
15281#if HAVE_TIFF
15282 Qtiff = intern ("tiff");
15283 staticpro (&Qtiff);
15284#endif
15285
15286#if HAVE_GIF
15287 Qgif = intern ("gif");
15288 staticpro (&Qgif);
15289#endif
15290
15291#if HAVE_PNG
15292 Qpng = intern ("png");
15293 staticpro (&Qpng);
15294#endif
15295
ac849ba4 15296#ifdef HAVE_IMAGES
6fc2811b 15297 defsubr (&Sclear_image_cache);
ac849ba4
JR
15298 defsubr (&Simage_size);
15299 defsubr (&Simage_mask_p);
15300#endif
6fc2811b
JR
15301
15302#if GLYPH_DEBUG
15303 defsubr (&Simagep);
15304 defsubr (&Slookup_image);
15305#endif
6fc2811b 15306
0af913d7
GM
15307 hourglass_atimer = NULL;
15308 hourglass_shown_p = 0;
6fc2811b
JR
15309 defsubr (&Sx_show_tip);
15310 defsubr (&Sx_hide_tip);
6fc2811b 15311 tip_timer = Qnil;
57fa2774
JR
15312 staticpro (&tip_timer);
15313 tip_frame = Qnil;
15314 staticpro (&tip_frame);
6fc2811b 15315
ca56d953
JR
15316 last_show_tip_args = Qnil;
15317 staticpro (&last_show_tip_args);
15318
6fc2811b
JR
15319 defsubr (&Sx_file_dialog);
15320}
15321
15322
15323void
15324init_xfns ()
15325{
15326 image_types = NULL;
15327 Vimage_types = Qnil;
15328
ac849ba4
JR
15329#if HAVE_PBM
15330 define_image_type (&pbm_type);
15331#endif
15332
767b1ff0 15333#if 0 /* TODO : Image support for W32 */
6fc2811b
JR
15334 define_image_type (&xbm_type);
15335 define_image_type (&gs_type);
ac849ba4 15336#endif
6fc2811b
JR
15337
15338#if HAVE_XPM
15339 define_image_type (&xpm_type);
15340#endif
15341
15342#if HAVE_JPEG
15343 define_image_type (&jpeg_type);
15344#endif
15345
15346#if HAVE_TIFF
15347 define_image_type (&tiff_type);
15348#endif
15349
15350#if HAVE_GIF
15351 define_image_type (&gif_type);
15352#endif
15353
15354#if HAVE_PNG
15355 define_image_type (&png_type);
15356#endif
ee78dc32
GV
15357}
15358
15359#undef abort
15360
15361void
fbd6baed 15362w32_abort()
ee78dc32 15363{
5ac45f98
GV
15364 int button;
15365 button = MessageBox (NULL,
15366 "A fatal error has occurred!\n\n"
15367 "Select Abort to exit, Retry to debug, Ignore to continue",
15368 "Emacs Abort Dialog",
15369 MB_ICONEXCLAMATION | MB_TASKMODAL
15370 | MB_SETFOREGROUND | MB_ABORTRETRYIGNORE);
15371 switch (button)
15372 {
15373 case IDRETRY:
15374 DebugBreak ();
15375 break;
15376 case IDIGNORE:
15377 break;
15378 case IDABORT:
15379 default:
15380 abort ();
15381 break;
15382 }
ee78dc32 15383}
d573caac 15384
83c75055
GV
15385/* For convenience when debugging. */
15386int
15387w32_last_error()
15388{
15389 return GetLastError ();
15390}