Remove period at end of error message.
[bpt/emacs.git] / src / w32fns.c
CommitLineData
e9e23e23 1/* Graphical user interface functions for the Microsoft W32 API.
a93f4566 2 Copyright (C) 1989, 92, 93, 94, 95, 1996, 1997, 1998, 1999, 2000, 2001
6fc2811b 3 Free Software Foundation, Inc.
ee78dc32
GV
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
ee78dc32
GV
21
22/* Added by Kevin Gallo */
23
ee78dc32 24#include <config.h>
1edf84e7
GV
25
26#include <signal.h>
ee78dc32 27#include <stdio.h>
1edf84e7
GV
28#include <limits.h>
29#include <errno.h>
ee78dc32
GV
30
31#include "lisp.h"
4587b026 32#include "charset.h"
71eab8d1 33#include "dispextern.h"
ee78dc32 34#include "w32term.h"
c7501041 35#include "keyboard.h"
ee78dc32
GV
36#include "frame.h"
37#include "window.h"
38#include "buffer.h"
126f2e35 39#include "fontset.h"
6fc2811b 40#include "intervals.h"
ee78dc32 41#include "blockinput.h"
57bda87a 42#include "epaths.h"
489f9371 43#include "w32heap.h"
ee78dc32 44#include "termhooks.h"
4587b026 45#include "coding.h"
3545439c 46#include "ccl.h"
6fc2811b
JR
47#include "systime.h"
48
49#include "bitmaps/gray.xbm"
ee78dc32
GV
50
51#include <commdlg.h>
cb9e33d4 52#include <shellapi.h>
6fc2811b 53#include <ctype.h>
ee78dc32 54
1030b26b
JR
55#include <dlgs.h>
56#define FILE_NAME_TEXT_FIELD edt1
57
9785d95b
BK
58void syms_of_w32fns ();
59void globals_of_w32fns ();
839b1909 60static void init_external_image_libraries ();
9785d95b 61
ee78dc32 62extern void free_frame_menubar ();
6fc2811b 63extern double atof ();
9eb16b62
JR
64extern int w32_console_toggle_lock_key P_ ((int, Lisp_Object));
65extern void w32_menu_display_help P_ ((HWND, HMENU, UINT, UINT));
66extern void w32_free_menu_strings P_ ((HWND));
67
5ac45f98 68extern int quit_char;
ee78dc32 69
ccc2d29c
GV
70extern char *lispy_function_keys[];
71
6fc2811b
JR
72/* The gray bitmap `bitmaps/gray'. This is done because w32term.c uses
73 it, and including `bitmaps/gray' more than once is a problem when
74 config.h defines `static' as an empty replacement string. */
75
76int gray_bitmap_width = gray_width;
77int gray_bitmap_height = gray_height;
78unsigned char *gray_bitmap_bits = gray_bits;
79
ee78dc32 80/* The colormap for converting color names to RGB values */
fbd6baed 81Lisp_Object Vw32_color_map;
ee78dc32 82
da36a4d6 83/* Non nil if alt key presses are passed on to Windows. */
fbd6baed 84Lisp_Object Vw32_pass_alt_to_system;
da36a4d6 85
8c205c63
RS
86/* Non nil if alt key is translated to meta_modifier, nil if it is translated
87 to alt_modifier. */
fbd6baed 88Lisp_Object Vw32_alt_is_meta;
8c205c63 89
7d081355
AI
90/* If non-zero, the windows virtual key code for an alternative quit key. */
91Lisp_Object Vw32_quit_key;
92
ccc2d29c
GV
93/* Non nil if left window key events are passed on to Windows (this only
94 affects whether "tapping" the key opens the Start menu). */
95Lisp_Object Vw32_pass_lwindow_to_system;
96
97/* Non nil if right window key events are passed on to Windows (this
98 only affects whether "tapping" the key opens the Start menu). */
99Lisp_Object Vw32_pass_rwindow_to_system;
100
adcc3809
GV
101/* Virtual key code used to generate "phantom" key presses in order
102 to stop system from acting on Windows key events. */
103Lisp_Object Vw32_phantom_key_code;
104
ccc2d29c
GV
105/* Modifier associated with the left "Windows" key, or nil to act as a
106 normal key. */
107Lisp_Object Vw32_lwindow_modifier;
108
109/* Modifier associated with the right "Windows" key, or nil to act as a
110 normal key. */
111Lisp_Object Vw32_rwindow_modifier;
112
113/* Modifier associated with the "Apps" key, or nil to act as a normal
114 key. */
115Lisp_Object Vw32_apps_modifier;
116
117/* Value is nil if Num Lock acts as a function key. */
118Lisp_Object Vw32_enable_num_lock;
119
120/* Value is nil if Caps Lock acts as a function key. */
121Lisp_Object Vw32_enable_caps_lock;
122
123/* Modifier associated with Scroll Lock, or nil to act as a normal key. */
124Lisp_Object Vw32_scroll_lock_modifier;
da36a4d6 125
7ce9aaca 126/* Switch to control whether we inhibit requests for synthesized bold
6fc2811b 127 and italic versions of fonts. */
d84b082d 128int w32_enable_synthesized_fonts;
5ac45f98
GV
129
130/* Enable palette management. */
fbd6baed 131Lisp_Object Vw32_enable_palette;
5ac45f98
GV
132
133/* Control how close left/right button down events must be to
134 be converted to a middle button down event. */
fbd6baed 135Lisp_Object Vw32_mouse_button_tolerance;
5ac45f98 136
84fb1139
KH
137/* Minimum interval between mouse movement (and scroll bar drag)
138 events that are passed on to the event loop. */
fbd6baed 139Lisp_Object Vw32_mouse_move_interval;
84fb1139 140
74214547
JR
141/* Flag to indicate if XBUTTON events should be passed on to Windows. */
142int w32_pass_extra_mouse_buttons_to_system;
143
ee78dc32
GV
144/* Non nil if no window manager is in use. */
145Lisp_Object Vx_no_window_manager;
146
0af913d7 147/* Non-zero means we're allowed to display a hourglass pointer. */
dfff8a69 148
0af913d7 149int display_hourglass_p;
6fc2811b 150
ee78dc32
GV
151/* The background and shape of the mouse pointer, and shape when not
152 over text or in the modeline. */
dfff8a69 153
ee78dc32 154Lisp_Object Vx_pointer_shape, Vx_nontext_pointer_shape, Vx_mode_pointer_shape;
c9b2104d 155Lisp_Object Vx_hourglass_pointer_shape, Vx_window_horizontal_drag_shape, Vx_hand_shape;
6fc2811b 156
ee78dc32 157/* The shape when over mouse-sensitive text. */
dfff8a69 158
ee78dc32
GV
159Lisp_Object Vx_sensitive_text_pointer_shape;
160
c9b2104d
JR
161#ifndef IDC_HAND
162#define IDC_HAND MAKEINTRESOURCE(32649)
163#endif
164
ee78dc32 165/* Color of chars displayed in cursor box. */
dfff8a69 166
ee78dc32
GV
167Lisp_Object Vx_cursor_fore_pixel;
168
1edf84e7 169/* Nonzero if using Windows. */
dfff8a69 170
1edf84e7
GV
171static int w32_in_use;
172
ee78dc32 173/* Search path for bitmap files. */
dfff8a69 174
ee78dc32
GV
175Lisp_Object Vx_bitmap_file_path;
176
4587b026 177/* Regexp matching a font name whose width is the same as `PIXEL_SIZE'. */
dfff8a69 178
4587b026
GV
179Lisp_Object Vx_pixel_size_width_font_regexp;
180
33d52f9c
GV
181/* Alist of bdf fonts and the files that define them. */
182Lisp_Object Vw32_bdf_filename_alist;
183
f46e6225 184/* A flag to control whether fonts are matched strictly or not. */
1075afa9
GV
185int w32_strict_fontnames;
186
c0611964
AI
187/* A flag to control whether we should only repaint if GetUpdateRect
188 indicates there is an update region. */
189int w32_strict_painting;
190
dfff8a69
JR
191/* Associative list linking character set strings to Windows codepages. */
192Lisp_Object Vw32_charset_info_alist;
193
194/* VIETNAMESE_CHARSET is not defined in some versions of MSVC. */
195#ifndef VIETNAMESE_CHARSET
196#define VIETNAMESE_CHARSET 163
197#endif
198
ee78dc32 199Lisp_Object Qnone;
ee78dc32 200Lisp_Object Qsuppress_icon;
ee78dc32 201Lisp_Object Qundefined_color;
dfff8a69 202Lisp_Object Qcenter;
dc220243 203Lisp_Object Qcancel_timer;
adcc3809
GV
204Lisp_Object Qhyper;
205Lisp_Object Qsuper;
206Lisp_Object Qmeta;
207Lisp_Object Qalt;
208Lisp_Object Qctrl;
209Lisp_Object Qcontrol;
210Lisp_Object Qshift;
211
dfff8a69
JR
212Lisp_Object Qw32_charset_ansi;
213Lisp_Object Qw32_charset_default;
214Lisp_Object Qw32_charset_symbol;
215Lisp_Object Qw32_charset_shiftjis;
767b1ff0 216Lisp_Object Qw32_charset_hangeul;
dfff8a69
JR
217Lisp_Object Qw32_charset_gb2312;
218Lisp_Object Qw32_charset_chinesebig5;
219Lisp_Object Qw32_charset_oem;
220
71eab8d1
AI
221#ifndef JOHAB_CHARSET
222#define JOHAB_CHARSET 130
223#endif
dfff8a69
JR
224#ifdef JOHAB_CHARSET
225Lisp_Object Qw32_charset_easteurope;
226Lisp_Object Qw32_charset_turkish;
227Lisp_Object Qw32_charset_baltic;
228Lisp_Object Qw32_charset_russian;
229Lisp_Object Qw32_charset_arabic;
230Lisp_Object Qw32_charset_greek;
231Lisp_Object Qw32_charset_hebrew;
767b1ff0 232Lisp_Object Qw32_charset_vietnamese;
dfff8a69
JR
233Lisp_Object Qw32_charset_thai;
234Lisp_Object Qw32_charset_johab;
235Lisp_Object Qw32_charset_mac;
236#endif
237
238#ifdef UNICODE_CHARSET
239Lisp_Object Qw32_charset_unicode;
240#endif
241
5a8a15ec
JR
242/* Prefix for system colors. */
243#define SYSTEM_COLOR_PREFIX "System"
244#define SYSTEM_COLOR_PREFIX_LEN (sizeof (SYSTEM_COLOR_PREFIX) - 1)
245
5ac45f98
GV
246/* State variables for emulating a three button mouse. */
247#define LMOUSE 1
248#define MMOUSE 2
249#define RMOUSE 4
250
251static int button_state = 0;
fbd6baed 252static W32Msg saved_mouse_button_msg;
48094ace 253static unsigned mouse_button_timer = 0; /* non-zero when timer is active */
fbd6baed 254static W32Msg saved_mouse_move_msg;
48094ace 255static unsigned mouse_move_timer = 0;
84fb1139 256
9eb16b62
JR
257/* Window that is tracking the mouse. */
258static HWND track_mouse_window;
f60ae425 259
ccc0fdaa
JR
260typedef BOOL (WINAPI * TrackMouseEvent_Proc)
261 (IN OUT LPTRACKMOUSEEVENT lpEventTrack);
f60ae425 262
ccc0fdaa
JR
263TrackMouseEvent_Proc track_mouse_event_fn = NULL;
264ClipboardSequence_Proc clipboard_sequence_fn = NULL;
9eb16b62 265
93fbe8b7 266/* W95 mousewheel handler */
7d0393cf 267unsigned int msh_mousewheel = 0;
93fbe8b7 268
48094ace 269/* Timers */
84fb1139
KH
270#define MOUSE_BUTTON_ID 1
271#define MOUSE_MOVE_ID 2
48094ace
JR
272#define MENU_FREE_ID 3
273/* The delay (milliseconds) before a menu is freed after WM_EXITMENULOOP
274 is received. */
275#define MENU_FREE_DELAY 1000
276static unsigned menu_free_timer = 0;
5ac45f98 277
ee78dc32 278/* The below are defined in frame.c. */
dfff8a69 279
ee78dc32
GV
280extern Lisp_Object Vwindow_system_version;
281
937e601e
AI
282#ifdef GLYPH_DEBUG
283int image_cache_refcount, dpyinfo_refcount;
284#endif
285
286
fbd6baed
GV
287/* From w32term.c. */
288extern Lisp_Object Vw32_num_mouse_buttons;
ccc2d29c 289extern Lisp_Object Vw32_recognize_altgr;
5ac45f98 290
65906840 291extern HWND w32_system_caret_hwnd;
93f2ca61 292
65906840
JR
293extern int w32_system_caret_height;
294extern int w32_system_caret_x;
295extern int w32_system_caret_y;
93f2ca61
JR
296extern int w32_use_visible_system_caret;
297
d285988b 298static HWND w32_visible_system_caret_hwnd;
65906840 299
ee78dc32 300\f
1edf84e7
GV
301/* Error if we are not connected to MS-Windows. */
302void
303check_w32 ()
304{
305 if (! w32_in_use)
306 error ("MS-Windows not in use or not initialized");
307}
308
309/* Nonzero if we can use mouse menus.
310 You should not call this unless HAVE_MENUS is defined. */
7d0393cf 311
1edf84e7
GV
312int
313have_menus_p ()
314{
315 return w32_in_use;
316}
317
ee78dc32 318/* Extract a frame as a FRAME_PTR, defaulting to the selected frame
fbd6baed 319 and checking validity for W32. */
ee78dc32
GV
320
321FRAME_PTR
322check_x_frame (frame)
323 Lisp_Object frame;
324{
325 FRAME_PTR f;
326
327 if (NILP (frame))
6fc2811b 328 frame = selected_frame;
b7826503 329 CHECK_LIVE_FRAME (frame);
6fc2811b 330 f = XFRAME (frame);
fbd6baed
GV
331 if (! FRAME_W32_P (f))
332 error ("non-w32 frame used");
ee78dc32
GV
333 return f;
334}
335
7d0393cf 336/* Let the user specify a display with a frame.
fbd6baed 337 nil stands for the selected frame--or, if that is not a w32 frame,
ee78dc32
GV
338 the first display on the list. */
339
6d906347 340struct w32_display_info *
ee78dc32
GV
341check_x_display_info (frame)
342 Lisp_Object frame;
343{
344 if (NILP (frame))
345 {
6fc2811b 346 struct frame *sf = XFRAME (selected_frame);
7d0393cf 347
6fc2811b
JR
348 if (FRAME_W32_P (sf) && FRAME_LIVE_P (sf))
349 return FRAME_W32_DISPLAY_INFO (sf);
ee78dc32 350 else
fbd6baed 351 return &one_w32_display_info;
ee78dc32
GV
352 }
353 else if (STRINGP (frame))
354 return x_display_info_for_name (frame);
355 else
356 {
357 FRAME_PTR f;
358
b7826503 359 CHECK_LIVE_FRAME (frame);
ee78dc32 360 f = XFRAME (frame);
fbd6baed
GV
361 if (! FRAME_W32_P (f))
362 error ("non-w32 frame used");
363 return FRAME_W32_DISPLAY_INFO (f);
ee78dc32
GV
364 }
365}
366\f
fbd6baed 367/* Return the Emacs frame-object corresponding to an w32 window.
ee78dc32
GV
368 It could be the frame's main window or an icon window. */
369
370/* This function can be called during GC, so use GC_xxx type test macros. */
371
372struct frame *
373x_window_to_frame (dpyinfo, wdesc)
fbd6baed 374 struct w32_display_info *dpyinfo;
ee78dc32
GV
375 HWND wdesc;
376{
377 Lisp_Object tail, frame;
378 struct frame *f;
379
8e713be6 380 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
ee78dc32 381 {
8e713be6 382 frame = XCAR (tail);
ee78dc32
GV
383 if (!GC_FRAMEP (frame))
384 continue;
385 f = XFRAME (frame);
2d764c78 386 if (!FRAME_W32_P (f) || FRAME_W32_DISPLAY_INFO (f) != dpyinfo)
ee78dc32 387 continue;
0af913d7 388 if (f->output_data.w32->hourglass_window == wdesc)
f79e6790
JR
389 return f;
390
fbd6baed 391 if (FRAME_W32_WINDOW (f) == wdesc)
ee78dc32
GV
392 return f;
393 }
394 return 0;
395}
396
397\f
398
399/* Code to deal with bitmaps. Bitmaps are referenced by their bitmap
400 id, which is just an int that this section returns. Bitmaps are
401 reference counted so they can be shared among frames.
402
403 Bitmap indices are guaranteed to be > 0, so a negative number can
404 be used to indicate no bitmap.
405
406 If you use x_create_bitmap_from_data, then you must keep track of
407 the bitmaps yourself. That is, creating a bitmap from the same
408 data more than once will not be caught. */
409
410
411/* Functions to access the contents of a bitmap, given an id. */
412
413int
414x_bitmap_height (f, id)
415 FRAME_PTR f;
416 int id;
417{
fbd6baed 418 return FRAME_W32_DISPLAY_INFO (f)->bitmaps[id - 1].height;
ee78dc32
GV
419}
420
421int
422x_bitmap_width (f, id)
423 FRAME_PTR f;
424 int id;
425{
fbd6baed 426 return FRAME_W32_DISPLAY_INFO (f)->bitmaps[id - 1].width;
ee78dc32
GV
427}
428
429int
430x_bitmap_pixmap (f, id)
431 FRAME_PTR f;
432 int id;
433{
fbd6baed 434 return (int) FRAME_W32_DISPLAY_INFO (f)->bitmaps[id - 1].pixmap;
ee78dc32
GV
435}
436
437
438/* Allocate a new bitmap record. Returns index of new record. */
439
440static int
441x_allocate_bitmap_record (f)
442 FRAME_PTR f;
443{
fbd6baed 444 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
ee78dc32
GV
445 int i;
446
447 if (dpyinfo->bitmaps == NULL)
448 {
449 dpyinfo->bitmaps_size = 10;
450 dpyinfo->bitmaps
fbd6baed 451 = (struct w32_bitmap_record *) xmalloc (dpyinfo->bitmaps_size * sizeof (struct w32_bitmap_record));
ee78dc32
GV
452 dpyinfo->bitmaps_last = 1;
453 return 1;
454 }
455
456 if (dpyinfo->bitmaps_last < dpyinfo->bitmaps_size)
457 return ++dpyinfo->bitmaps_last;
458
459 for (i = 0; i < dpyinfo->bitmaps_size; ++i)
460 if (dpyinfo->bitmaps[i].refcount == 0)
461 return i + 1;
462
463 dpyinfo->bitmaps_size *= 2;
464 dpyinfo->bitmaps
fbd6baed
GV
465 = (struct w32_bitmap_record *) xrealloc (dpyinfo->bitmaps,
466 dpyinfo->bitmaps_size * sizeof (struct w32_bitmap_record));
ee78dc32
GV
467 return ++dpyinfo->bitmaps_last;
468}
469
470/* Add one reference to the reference count of the bitmap with id ID. */
471
472void
473x_reference_bitmap (f, id)
474 FRAME_PTR f;
475 int id;
476{
fbd6baed 477 ++FRAME_W32_DISPLAY_INFO (f)->bitmaps[id - 1].refcount;
ee78dc32
GV
478}
479
480/* Create a bitmap for frame F from a HEIGHT x WIDTH array of bits at BITS. */
481
482int
483x_create_bitmap_from_data (f, bits, width, height)
484 struct frame *f;
485 char *bits;
486 unsigned int width, height;
487{
fbd6baed 488 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
ee78dc32
GV
489 Pixmap bitmap;
490 int id;
491
492 bitmap = CreateBitmap (width, height,
fbd6baed
GV
493 FRAME_W32_DISPLAY_INFO (XFRAME (frame))->n_planes,
494 FRAME_W32_DISPLAY_INFO (XFRAME (frame))->n_cbits,
ee78dc32
GV
495 bits);
496
497 if (! bitmap)
498 return -1;
499
500 id = x_allocate_bitmap_record (f);
501 dpyinfo->bitmaps[id - 1].pixmap = bitmap;
502 dpyinfo->bitmaps[id - 1].file = NULL;
503 dpyinfo->bitmaps[id - 1].hinst = NULL;
504 dpyinfo->bitmaps[id - 1].refcount = 1;
505 dpyinfo->bitmaps[id - 1].depth = 1;
506 dpyinfo->bitmaps[id - 1].height = height;
507 dpyinfo->bitmaps[id - 1].width = width;
508
509 return id;
510}
511
512/* Create bitmap from file FILE for frame F. */
513
514int
515x_create_bitmap_from_file (f, file)
516 struct frame *f;
517 Lisp_Object file;
518{
519 return -1;
767b1ff0 520#if 0 /* TODO : bitmap support */
fbd6baed 521 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
ee78dc32 522 unsigned int width, height;
6fc2811b 523 HBITMAP bitmap;
ee78dc32
GV
524 int xhot, yhot, result, id;
525 Lisp_Object found;
526 int fd;
527 char *filename;
528 HINSTANCE hinst;
529
530 /* Look for an existing bitmap with the same name. */
531 for (id = 0; id < dpyinfo->bitmaps_last; ++id)
532 {
533 if (dpyinfo->bitmaps[id].refcount
534 && dpyinfo->bitmaps[id].file
d5db4077 535 && !strcmp (dpyinfo->bitmaps[id].file, (char *) SDATA (file)))
ee78dc32
GV
536 {
537 ++dpyinfo->bitmaps[id].refcount;
538 return id + 1;
539 }
540 }
541
542 /* Search bitmap-file-path for the file, if appropriate. */
de2413e9 543 fd = openp (Vx_bitmap_file_path, file, Qnil, &found, Qnil);
ee78dc32
GV
544 if (fd < 0)
545 return -1;
6fc2811b 546 emacs_close (fd);
ee78dc32 547
d5db4077 548 filename = (char *) SDATA (found);
ee78dc32
GV
549
550 hinst = LoadLibraryEx (filename, NULL, LOAD_LIBRARY_AS_DATAFILE);
551
552 if (hinst == NULL)
553 return -1;
554
7d0393cf 555
fbd6baed 556 result = XReadBitmapFile (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
ee78dc32
GV
557 filename, &width, &height, &bitmap, &xhot, &yhot);
558 if (result != BitmapSuccess)
559 return -1;
560
561 id = x_allocate_bitmap_record (f);
562 dpyinfo->bitmaps[id - 1].pixmap = bitmap;
563 dpyinfo->bitmaps[id - 1].refcount = 1;
d5db4077 564 dpyinfo->bitmaps[id - 1].file = (char *) xmalloc (SCHARS (file) + 1);
ee78dc32
GV
565 dpyinfo->bitmaps[id - 1].depth = 1;
566 dpyinfo->bitmaps[id - 1].height = height;
567 dpyinfo->bitmaps[id - 1].width = width;
d5db4077 568 strcpy (dpyinfo->bitmaps[id - 1].file, SDATA (file));
ee78dc32
GV
569
570 return id;
767b1ff0 571#endif /* TODO */
ee78dc32
GV
572}
573
574/* Remove reference to bitmap with id number ID. */
575
33d52f9c 576void
ee78dc32
GV
577x_destroy_bitmap (f, id)
578 FRAME_PTR f;
579 int id;
580{
fbd6baed 581 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
ee78dc32
GV
582
583 if (id > 0)
584 {
585 --dpyinfo->bitmaps[id - 1].refcount;
586 if (dpyinfo->bitmaps[id - 1].refcount == 0)
587 {
588 BLOCK_INPUT;
589 DeleteObject (dpyinfo->bitmaps[id - 1].pixmap);
590 if (dpyinfo->bitmaps[id - 1].file)
591 {
6fc2811b 592 xfree (dpyinfo->bitmaps[id - 1].file);
ee78dc32
GV
593 dpyinfo->bitmaps[id - 1].file = NULL;
594 }
595 UNBLOCK_INPUT;
596 }
597 }
598}
599
600/* Free all the bitmaps for the display specified by DPYINFO. */
601
602static void
603x_destroy_all_bitmaps (dpyinfo)
fbd6baed 604 struct w32_display_info *dpyinfo;
ee78dc32
GV
605{
606 int i;
607 for (i = 0; i < dpyinfo->bitmaps_last; i++)
608 if (dpyinfo->bitmaps[i].refcount > 0)
609 {
610 DeleteObject (dpyinfo->bitmaps[i].pixmap);
611 if (dpyinfo->bitmaps[i].file)
6fc2811b 612 xfree (dpyinfo->bitmaps[i].file);
ee78dc32
GV
613 }
614 dpyinfo->bitmaps_last = 0;
615}
616\f
ca56d953
JR
617BOOL my_show_window P_ ((struct frame *, HWND, int));
618void my_set_window_pos P_ ((HWND, HWND, int, int, int, int, UINT));
937e601e
AI
619static Lisp_Object unwind_create_frame P_ ((Lisp_Object));
620static Lisp_Object unwind_create_tip_frame P_ ((Lisp_Object));
6d906347 621
767b1ff0 622/* TODO: Native Input Method support; see x_create_im. */
6fc2811b
JR
623void x_set_foreground_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
624void x_set_background_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
625void x_set_mouse_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
626void x_set_cursor_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
627void x_set_border_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
628void x_set_cursor_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
629void x_set_icon_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
630void x_set_icon_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
6fc2811b 631void x_explicitly_set_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
6fc2811b 632void x_set_menu_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
6fc2811b 633void x_set_title P_ ((struct frame *, Lisp_Object, Lisp_Object));
6fc2811b 634void x_set_tool_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
3cf3436e
JR
635static void x_edge_detection P_ ((struct frame *, struct image *, Lisp_Object,
636 Lisp_Object));
ee78dc32 637
ee78dc32 638
ee78dc32 639\f
ee78dc32
GV
640
641/* Store the screen positions of frame F into XPTR and YPTR.
642 These are the positions of the containing window manager window,
643 not Emacs's own window. */
644
645void
646x_real_positions (f, xptr, yptr)
647 FRAME_PTR f;
648 int *xptr, *yptr;
649{
650 POINT pt;
f7b9d4d1 651 RECT rect;
3c190163 652
f7b9d4d1
JR
653 GetClientRect(FRAME_W32_WINDOW(f), &rect);
654 AdjustWindowRect(&rect, f->output_data.w32->dwStyle, FRAME_EXTERNAL_MENU_BAR(f));
655
656 pt.x = rect.left;
657 pt.y = rect.top;
ee78dc32 658
fbd6baed 659 ClientToScreen (FRAME_W32_WINDOW(f), &pt);
ee78dc32 660
f7b9d4d1 661 /* Remember x_pixels_diff and y_pixels_diff. */
be786000
KS
662 f->x_pixels_diff = pt.x - rect.left;
663 f->y_pixels_diff = pt.y - rect.top;
f7b9d4d1 664
ee78dc32
GV
665 *xptr = pt.x;
666 *yptr = pt.y;
667}
668
ee78dc32
GV
669\f
670
74e1aeec
JR
671DEFUN ("w32-define-rgb-color", Fw32_define_rgb_color,
672 Sw32_define_rgb_color, 4, 4, 0,
673 doc: /* Convert RGB numbers to a windows color reference and associate with NAME.
674This adds or updates a named color to w32-color-map, making it
675available for use. The original entry's RGB ref is returned, or nil
676if the entry is new. */)
5ac45f98
GV
677 (red, green, blue, name)
678 Lisp_Object red, green, blue, name;
ee78dc32 679{
5ac45f98
GV
680 Lisp_Object rgb;
681 Lisp_Object oldrgb = Qnil;
682 Lisp_Object entry;
683
b7826503
PJ
684 CHECK_NUMBER (red);
685 CHECK_NUMBER (green);
686 CHECK_NUMBER (blue);
687 CHECK_STRING (name);
ee78dc32 688
5ac45f98 689 XSET (rgb, Lisp_Int, RGB(XUINT (red), XUINT (green), XUINT (blue)));
ee78dc32 690
5ac45f98 691 BLOCK_INPUT;
ee78dc32 692
fbd6baed
GV
693 /* replace existing entry in w32-color-map or add new entry. */
694 entry = Fassoc (name, Vw32_color_map);
5ac45f98
GV
695 if (NILP (entry))
696 {
697 entry = Fcons (name, rgb);
fbd6baed 698 Vw32_color_map = Fcons (entry, Vw32_color_map);
5ac45f98
GV
699 }
700 else
701 {
702 oldrgb = Fcdr (entry);
703 Fsetcdr (entry, rgb);
704 }
705
706 UNBLOCK_INPUT;
707
708 return (oldrgb);
ee78dc32
GV
709}
710
74e1aeec
JR
711DEFUN ("w32-load-color-file", Fw32_load_color_file,
712 Sw32_load_color_file, 1, 1, 0,
713 doc: /* Create an alist of color entries from an external file.
714Assign this value to w32-color-map to replace the existing color map.
715
716The file should define one named RGB color per line like so:
717 R G B name
718where R,G,B are numbers between 0 and 255 and name is an arbitrary string. */)
5ac45f98
GV
719 (filename)
720 Lisp_Object filename;
721{
722 FILE *fp;
723 Lisp_Object cmap = Qnil;
724 Lisp_Object abspath;
725
b7826503 726 CHECK_STRING (filename);
5ac45f98
GV
727 abspath = Fexpand_file_name (filename, Qnil);
728
d5db4077 729 fp = fopen (SDATA (filename), "rt");
5ac45f98
GV
730 if (fp)
731 {
732 char buf[512];
733 int red, green, blue;
734 int num;
735
736 BLOCK_INPUT;
737
738 while (fgets (buf, sizeof (buf), fp) != NULL) {
739 if (sscanf (buf, "%u %u %u %n", &red, &green, &blue, &num) == 3)
740 {
741 char *name = buf + num;
742 num = strlen (name) - 1;
743 if (name[num] == '\n')
744 name[num] = 0;
745 cmap = Fcons (Fcons (build_string (name),
746 make_number (RGB (red, green, blue))),
747 cmap);
748 }
749 }
750 fclose (fp);
751
752 UNBLOCK_INPUT;
753 }
754
755 return cmap;
756}
ee78dc32 757
fbd6baed 758/* The default colors for the w32 color map */
7d0393cf 759typedef struct colormap_t
ee78dc32
GV
760{
761 char *name;
762 COLORREF colorref;
763} colormap_t;
764
7d0393cf 765colormap_t w32_color_map[] =
ee78dc32 766{
1da8a614
GV
767 {"snow" , PALETTERGB (255,250,250)},
768 {"ghost white" , PALETTERGB (248,248,255)},
769 {"GhostWhite" , PALETTERGB (248,248,255)},
770 {"white smoke" , PALETTERGB (245,245,245)},
771 {"WhiteSmoke" , PALETTERGB (245,245,245)},
772 {"gainsboro" , PALETTERGB (220,220,220)},
773 {"floral white" , PALETTERGB (255,250,240)},
774 {"FloralWhite" , PALETTERGB (255,250,240)},
775 {"old lace" , PALETTERGB (253,245,230)},
776 {"OldLace" , PALETTERGB (253,245,230)},
777 {"linen" , PALETTERGB (250,240,230)},
778 {"antique white" , PALETTERGB (250,235,215)},
779 {"AntiqueWhite" , PALETTERGB (250,235,215)},
780 {"papaya whip" , PALETTERGB (255,239,213)},
781 {"PapayaWhip" , PALETTERGB (255,239,213)},
782 {"blanched almond" , PALETTERGB (255,235,205)},
783 {"BlanchedAlmond" , PALETTERGB (255,235,205)},
784 {"bisque" , PALETTERGB (255,228,196)},
785 {"peach puff" , PALETTERGB (255,218,185)},
786 {"PeachPuff" , PALETTERGB (255,218,185)},
787 {"navajo white" , PALETTERGB (255,222,173)},
788 {"NavajoWhite" , PALETTERGB (255,222,173)},
789 {"moccasin" , PALETTERGB (255,228,181)},
790 {"cornsilk" , PALETTERGB (255,248,220)},
791 {"ivory" , PALETTERGB (255,255,240)},
792 {"lemon chiffon" , PALETTERGB (255,250,205)},
793 {"LemonChiffon" , PALETTERGB (255,250,205)},
794 {"seashell" , PALETTERGB (255,245,238)},
795 {"honeydew" , PALETTERGB (240,255,240)},
796 {"mint cream" , PALETTERGB (245,255,250)},
797 {"MintCream" , PALETTERGB (245,255,250)},
798 {"azure" , PALETTERGB (240,255,255)},
799 {"alice blue" , PALETTERGB (240,248,255)},
800 {"AliceBlue" , PALETTERGB (240,248,255)},
801 {"lavender" , PALETTERGB (230,230,250)},
802 {"lavender blush" , PALETTERGB (255,240,245)},
803 {"LavenderBlush" , PALETTERGB (255,240,245)},
804 {"misty rose" , PALETTERGB (255,228,225)},
805 {"MistyRose" , PALETTERGB (255,228,225)},
806 {"white" , PALETTERGB (255,255,255)},
807 {"black" , PALETTERGB ( 0, 0, 0)},
808 {"dark slate gray" , PALETTERGB ( 47, 79, 79)},
809 {"DarkSlateGray" , PALETTERGB ( 47, 79, 79)},
810 {"dark slate grey" , PALETTERGB ( 47, 79, 79)},
811 {"DarkSlateGrey" , PALETTERGB ( 47, 79, 79)},
812 {"dim gray" , PALETTERGB (105,105,105)},
813 {"DimGray" , PALETTERGB (105,105,105)},
814 {"dim grey" , PALETTERGB (105,105,105)},
815 {"DimGrey" , PALETTERGB (105,105,105)},
816 {"slate gray" , PALETTERGB (112,128,144)},
817 {"SlateGray" , PALETTERGB (112,128,144)},
818 {"slate grey" , PALETTERGB (112,128,144)},
819 {"SlateGrey" , PALETTERGB (112,128,144)},
820 {"light slate gray" , PALETTERGB (119,136,153)},
821 {"LightSlateGray" , PALETTERGB (119,136,153)},
822 {"light slate grey" , PALETTERGB (119,136,153)},
823 {"LightSlateGrey" , PALETTERGB (119,136,153)},
824 {"gray" , PALETTERGB (190,190,190)},
825 {"grey" , PALETTERGB (190,190,190)},
826 {"light grey" , PALETTERGB (211,211,211)},
827 {"LightGrey" , PALETTERGB (211,211,211)},
828 {"light gray" , PALETTERGB (211,211,211)},
829 {"LightGray" , PALETTERGB (211,211,211)},
830 {"midnight blue" , PALETTERGB ( 25, 25,112)},
831 {"MidnightBlue" , PALETTERGB ( 25, 25,112)},
832 {"navy" , PALETTERGB ( 0, 0,128)},
833 {"navy blue" , PALETTERGB ( 0, 0,128)},
834 {"NavyBlue" , PALETTERGB ( 0, 0,128)},
835 {"cornflower blue" , PALETTERGB (100,149,237)},
836 {"CornflowerBlue" , PALETTERGB (100,149,237)},
837 {"dark slate blue" , PALETTERGB ( 72, 61,139)},
838 {"DarkSlateBlue" , PALETTERGB ( 72, 61,139)},
839 {"slate blue" , PALETTERGB (106, 90,205)},
840 {"SlateBlue" , PALETTERGB (106, 90,205)},
841 {"medium slate blue" , PALETTERGB (123,104,238)},
842 {"MediumSlateBlue" , PALETTERGB (123,104,238)},
843 {"light slate blue" , PALETTERGB (132,112,255)},
844 {"LightSlateBlue" , PALETTERGB (132,112,255)},
845 {"medium blue" , PALETTERGB ( 0, 0,205)},
846 {"MediumBlue" , PALETTERGB ( 0, 0,205)},
847 {"royal blue" , PALETTERGB ( 65,105,225)},
848 {"RoyalBlue" , PALETTERGB ( 65,105,225)},
849 {"blue" , PALETTERGB ( 0, 0,255)},
850 {"dodger blue" , PALETTERGB ( 30,144,255)},
851 {"DodgerBlue" , PALETTERGB ( 30,144,255)},
852 {"deep sky blue" , PALETTERGB ( 0,191,255)},
853 {"DeepSkyBlue" , PALETTERGB ( 0,191,255)},
854 {"sky blue" , PALETTERGB (135,206,235)},
855 {"SkyBlue" , PALETTERGB (135,206,235)},
856 {"light sky blue" , PALETTERGB (135,206,250)},
857 {"LightSkyBlue" , PALETTERGB (135,206,250)},
858 {"steel blue" , PALETTERGB ( 70,130,180)},
859 {"SteelBlue" , PALETTERGB ( 70,130,180)},
860 {"light steel blue" , PALETTERGB (176,196,222)},
861 {"LightSteelBlue" , PALETTERGB (176,196,222)},
862 {"light blue" , PALETTERGB (173,216,230)},
863 {"LightBlue" , PALETTERGB (173,216,230)},
864 {"powder blue" , PALETTERGB (176,224,230)},
865 {"PowderBlue" , PALETTERGB (176,224,230)},
866 {"pale turquoise" , PALETTERGB (175,238,238)},
867 {"PaleTurquoise" , PALETTERGB (175,238,238)},
868 {"dark turquoise" , PALETTERGB ( 0,206,209)},
869 {"DarkTurquoise" , PALETTERGB ( 0,206,209)},
870 {"medium turquoise" , PALETTERGB ( 72,209,204)},
871 {"MediumTurquoise" , PALETTERGB ( 72,209,204)},
872 {"turquoise" , PALETTERGB ( 64,224,208)},
873 {"cyan" , PALETTERGB ( 0,255,255)},
874 {"light cyan" , PALETTERGB (224,255,255)},
875 {"LightCyan" , PALETTERGB (224,255,255)},
876 {"cadet blue" , PALETTERGB ( 95,158,160)},
877 {"CadetBlue" , PALETTERGB ( 95,158,160)},
878 {"medium aquamarine" , PALETTERGB (102,205,170)},
879 {"MediumAquamarine" , PALETTERGB (102,205,170)},
880 {"aquamarine" , PALETTERGB (127,255,212)},
881 {"dark green" , PALETTERGB ( 0,100, 0)},
882 {"DarkGreen" , PALETTERGB ( 0,100, 0)},
883 {"dark olive green" , PALETTERGB ( 85,107, 47)},
884 {"DarkOliveGreen" , PALETTERGB ( 85,107, 47)},
885 {"dark sea green" , PALETTERGB (143,188,143)},
886 {"DarkSeaGreen" , PALETTERGB (143,188,143)},
887 {"sea green" , PALETTERGB ( 46,139, 87)},
888 {"SeaGreen" , PALETTERGB ( 46,139, 87)},
889 {"medium sea green" , PALETTERGB ( 60,179,113)},
890 {"MediumSeaGreen" , PALETTERGB ( 60,179,113)},
891 {"light sea green" , PALETTERGB ( 32,178,170)},
892 {"LightSeaGreen" , PALETTERGB ( 32,178,170)},
893 {"pale green" , PALETTERGB (152,251,152)},
894 {"PaleGreen" , PALETTERGB (152,251,152)},
895 {"spring green" , PALETTERGB ( 0,255,127)},
896 {"SpringGreen" , PALETTERGB ( 0,255,127)},
897 {"lawn green" , PALETTERGB (124,252, 0)},
898 {"LawnGreen" , PALETTERGB (124,252, 0)},
899 {"green" , PALETTERGB ( 0,255, 0)},
900 {"chartreuse" , PALETTERGB (127,255, 0)},
901 {"medium spring green" , PALETTERGB ( 0,250,154)},
902 {"MediumSpringGreen" , PALETTERGB ( 0,250,154)},
903 {"green yellow" , PALETTERGB (173,255, 47)},
904 {"GreenYellow" , PALETTERGB (173,255, 47)},
905 {"lime green" , PALETTERGB ( 50,205, 50)},
906 {"LimeGreen" , PALETTERGB ( 50,205, 50)},
907 {"yellow green" , PALETTERGB (154,205, 50)},
908 {"YellowGreen" , PALETTERGB (154,205, 50)},
909 {"forest green" , PALETTERGB ( 34,139, 34)},
910 {"ForestGreen" , PALETTERGB ( 34,139, 34)},
911 {"olive drab" , PALETTERGB (107,142, 35)},
912 {"OliveDrab" , PALETTERGB (107,142, 35)},
913 {"dark khaki" , PALETTERGB (189,183,107)},
914 {"DarkKhaki" , PALETTERGB (189,183,107)},
915 {"khaki" , PALETTERGB (240,230,140)},
916 {"pale goldenrod" , PALETTERGB (238,232,170)},
917 {"PaleGoldenrod" , PALETTERGB (238,232,170)},
918 {"light goldenrod yellow" , PALETTERGB (250,250,210)},
919 {"LightGoldenrodYellow" , PALETTERGB (250,250,210)},
920 {"light yellow" , PALETTERGB (255,255,224)},
921 {"LightYellow" , PALETTERGB (255,255,224)},
922 {"yellow" , PALETTERGB (255,255, 0)},
923 {"gold" , PALETTERGB (255,215, 0)},
924 {"light goldenrod" , PALETTERGB (238,221,130)},
925 {"LightGoldenrod" , PALETTERGB (238,221,130)},
926 {"goldenrod" , PALETTERGB (218,165, 32)},
927 {"dark goldenrod" , PALETTERGB (184,134, 11)},
928 {"DarkGoldenrod" , PALETTERGB (184,134, 11)},
929 {"rosy brown" , PALETTERGB (188,143,143)},
930 {"RosyBrown" , PALETTERGB (188,143,143)},
931 {"indian red" , PALETTERGB (205, 92, 92)},
932 {"IndianRed" , PALETTERGB (205, 92, 92)},
933 {"saddle brown" , PALETTERGB (139, 69, 19)},
934 {"SaddleBrown" , PALETTERGB (139, 69, 19)},
935 {"sienna" , PALETTERGB (160, 82, 45)},
936 {"peru" , PALETTERGB (205,133, 63)},
937 {"burlywood" , PALETTERGB (222,184,135)},
938 {"beige" , PALETTERGB (245,245,220)},
939 {"wheat" , PALETTERGB (245,222,179)},
940 {"sandy brown" , PALETTERGB (244,164, 96)},
941 {"SandyBrown" , PALETTERGB (244,164, 96)},
942 {"tan" , PALETTERGB (210,180,140)},
943 {"chocolate" , PALETTERGB (210,105, 30)},
944 {"firebrick" , PALETTERGB (178,34, 34)},
945 {"brown" , PALETTERGB (165,42, 42)},
946 {"dark salmon" , PALETTERGB (233,150,122)},
947 {"DarkSalmon" , PALETTERGB (233,150,122)},
948 {"salmon" , PALETTERGB (250,128,114)},
949 {"light salmon" , PALETTERGB (255,160,122)},
950 {"LightSalmon" , PALETTERGB (255,160,122)},
951 {"orange" , PALETTERGB (255,165, 0)},
952 {"dark orange" , PALETTERGB (255,140, 0)},
953 {"DarkOrange" , PALETTERGB (255,140, 0)},
954 {"coral" , PALETTERGB (255,127, 80)},
955 {"light coral" , PALETTERGB (240,128,128)},
956 {"LightCoral" , PALETTERGB (240,128,128)},
957 {"tomato" , PALETTERGB (255, 99, 71)},
958 {"orange red" , PALETTERGB (255, 69, 0)},
959 {"OrangeRed" , PALETTERGB (255, 69, 0)},
960 {"red" , PALETTERGB (255, 0, 0)},
961 {"hot pink" , PALETTERGB (255,105,180)},
962 {"HotPink" , PALETTERGB (255,105,180)},
963 {"deep pink" , PALETTERGB (255, 20,147)},
964 {"DeepPink" , PALETTERGB (255, 20,147)},
965 {"pink" , PALETTERGB (255,192,203)},
966 {"light pink" , PALETTERGB (255,182,193)},
967 {"LightPink" , PALETTERGB (255,182,193)},
968 {"pale violet red" , PALETTERGB (219,112,147)},
969 {"PaleVioletRed" , PALETTERGB (219,112,147)},
970 {"maroon" , PALETTERGB (176, 48, 96)},
971 {"medium violet red" , PALETTERGB (199, 21,133)},
972 {"MediumVioletRed" , PALETTERGB (199, 21,133)},
973 {"violet red" , PALETTERGB (208, 32,144)},
974 {"VioletRed" , PALETTERGB (208, 32,144)},
975 {"magenta" , PALETTERGB (255, 0,255)},
976 {"violet" , PALETTERGB (238,130,238)},
977 {"plum" , PALETTERGB (221,160,221)},
978 {"orchid" , PALETTERGB (218,112,214)},
979 {"medium orchid" , PALETTERGB (186, 85,211)},
980 {"MediumOrchid" , PALETTERGB (186, 85,211)},
981 {"dark orchid" , PALETTERGB (153, 50,204)},
982 {"DarkOrchid" , PALETTERGB (153, 50,204)},
983 {"dark violet" , PALETTERGB (148, 0,211)},
984 {"DarkViolet" , PALETTERGB (148, 0,211)},
985 {"blue violet" , PALETTERGB (138, 43,226)},
986 {"BlueViolet" , PALETTERGB (138, 43,226)},
987 {"purple" , PALETTERGB (160, 32,240)},
988 {"medium purple" , PALETTERGB (147,112,219)},
989 {"MediumPurple" , PALETTERGB (147,112,219)},
990 {"thistle" , PALETTERGB (216,191,216)},
991 {"gray0" , PALETTERGB ( 0, 0, 0)},
992 {"grey0" , PALETTERGB ( 0, 0, 0)},
993 {"dark grey" , PALETTERGB (169,169,169)},
994 {"DarkGrey" , PALETTERGB (169,169,169)},
995 {"dark gray" , PALETTERGB (169,169,169)},
996 {"DarkGray" , PALETTERGB (169,169,169)},
997 {"dark blue" , PALETTERGB ( 0, 0,139)},
998 {"DarkBlue" , PALETTERGB ( 0, 0,139)},
999 {"dark cyan" , PALETTERGB ( 0,139,139)},
1000 {"DarkCyan" , PALETTERGB ( 0,139,139)},
1001 {"dark magenta" , PALETTERGB (139, 0,139)},
1002 {"DarkMagenta" , PALETTERGB (139, 0,139)},
1003 {"dark red" , PALETTERGB (139, 0, 0)},
1004 {"DarkRed" , PALETTERGB (139, 0, 0)},
1005 {"light green" , PALETTERGB (144,238,144)},
1006 {"LightGreen" , PALETTERGB (144,238,144)},
ee78dc32
GV
1007};
1008
fbd6baed 1009DEFUN ("w32-default-color-map", Fw32_default_color_map, Sw32_default_color_map,
74e1aeec 1010 0, 0, 0, doc: /* Return the default color map. */)
ee78dc32
GV
1011 ()
1012{
1013 int i;
fbd6baed 1014 colormap_t *pc = w32_color_map;
ee78dc32 1015 Lisp_Object cmap;
7d0393cf 1016
ee78dc32 1017 BLOCK_INPUT;
7d0393cf 1018
ee78dc32 1019 cmap = Qnil;
7d0393cf
JB
1020
1021 for (i = 0; i < sizeof (w32_color_map) / sizeof (w32_color_map[0]);
ee78dc32
GV
1022 pc++, i++)
1023 cmap = Fcons (Fcons (build_string (pc->name),
1024 make_number (pc->colorref)),
1025 cmap);
7d0393cf 1026
ee78dc32 1027 UNBLOCK_INPUT;
7d0393cf 1028
ee78dc32
GV
1029 return (cmap);
1030}
ee78dc32 1031
7d0393cf 1032Lisp_Object
fbd6baed 1033w32_to_x_color (rgb)
ee78dc32
GV
1034 Lisp_Object rgb;
1035{
1036 Lisp_Object color;
7d0393cf 1037
b7826503 1038 CHECK_NUMBER (rgb);
7d0393cf 1039
ee78dc32 1040 BLOCK_INPUT;
7d0393cf 1041
fbd6baed 1042 color = Frassq (rgb, Vw32_color_map);
7d0393cf 1043
ee78dc32 1044 UNBLOCK_INPUT;
7d0393cf 1045
ee78dc32
GV
1046 if (!NILP (color))
1047 return (Fcar (color));
1048 else
1049 return Qnil;
1050}
1051
5d7fed93
GV
1052COLORREF
1053w32_color_map_lookup (colorname)
1054 char *colorname;
1055{
1056 Lisp_Object tail, ret = Qnil;
1057
1058 BLOCK_INPUT;
1059
1060 for (tail = Vw32_color_map; !NILP (tail); tail = Fcdr (tail))
1061 {
1062 register Lisp_Object elt, tem;
1063
1064 elt = Fcar (tail);
1065 if (!CONSP (elt)) continue;
1066
1067 tem = Fcar (elt);
1068
d5db4077 1069 if (lstrcmpi (SDATA (tem), colorname) == 0)
5d7fed93
GV
1070 {
1071 ret = XUINT (Fcdr (elt));
1072 break;
1073 }
1074
1075 QUIT;
1076 }
1077
1078
1079 UNBLOCK_INPUT;
1080
1081 return ret;
1082}
1083
5a8a15ec
JR
1084
1085static void
1086add_system_logical_colors_to_map (system_colors)
1087 Lisp_Object *system_colors;
1088{
1089 HKEY colors_key;
1090
1091 /* Other registry operations are done with input blocked. */
1092 BLOCK_INPUT;
1093
1094 /* Look for "Control Panel/Colors" under User and Machine registry
1095 settings. */
1096 if (RegOpenKeyEx (HKEY_CURRENT_USER, "Control Panel\\Colors", 0,
1097 KEY_READ, &colors_key) == ERROR_SUCCESS
1098 || RegOpenKeyEx (HKEY_LOCAL_MACHINE, "Control Panel\\Colors", 0,
1099 KEY_READ, &colors_key) == ERROR_SUCCESS)
1100 {
1101 /* List all keys. */
1102 char color_buffer[64];
1103 char full_name_buffer[MAX_PATH + SYSTEM_COLOR_PREFIX_LEN];
1104 int index = 0;
1105 DWORD name_size, color_size;
1106 char *name_buffer = full_name_buffer + SYSTEM_COLOR_PREFIX_LEN;
1107
1108 name_size = sizeof (full_name_buffer) - SYSTEM_COLOR_PREFIX_LEN;
1109 color_size = sizeof (color_buffer);
1110
1111 strcpy (full_name_buffer, SYSTEM_COLOR_PREFIX);
1112
1113 while (RegEnumValueA (colors_key, index, name_buffer, &name_size,
1114 NULL, NULL, color_buffer, &color_size)
1115 == ERROR_SUCCESS)
1116 {
1117 int r, g, b;
1118 if (sscanf (color_buffer, " %u %u %u", &r, &g, &b) == 3)
1119 *system_colors = Fcons (Fcons (build_string (full_name_buffer),
1120 make_number (RGB (r, g, b))),
1121 *system_colors);
1122
1123 name_size = sizeof (full_name_buffer) - SYSTEM_COLOR_PREFIX_LEN;
1124 color_size = sizeof (color_buffer);
1125 index++;
1126 }
1127 RegCloseKey (colors_key);
1128 }
1129
1130 UNBLOCK_INPUT;
1131}
1132
1133
7d0393cf 1134COLORREF
fbd6baed 1135x_to_w32_color (colorname)
ee78dc32
GV
1136 char * colorname;
1137{
8edb0a6f
JR
1138 register Lisp_Object ret = Qnil;
1139
ee78dc32 1140 BLOCK_INPUT;
1edf84e7
GV
1141
1142 if (colorname[0] == '#')
1143 {
1144 /* Could be an old-style RGB Device specification. */
1145 char *color;
1146 int size;
1147 color = colorname + 1;
7d0393cf 1148
1edf84e7
GV
1149 size = strlen(color);
1150 if (size == 3 || size == 6 || size == 9 || size == 12)
1151 {
1152 UINT colorval;
1153 int i, pos;
1154 pos = 0;
1155 size /= 3;
1156 colorval = 0;
7d0393cf 1157
1edf84e7
GV
1158 for (i = 0; i < 3; i++)
1159 {
1160 char *end;
1161 char t;
1162 unsigned long value;
1163
1164 /* The check for 'x' in the following conditional takes into
1165 account the fact that strtol allows a "0x" in front of
1166 our numbers, and we don't. */
1167 if (!isxdigit(color[0]) || color[1] == 'x')
1168 break;
1169 t = color[size];
1170 color[size] = '\0';
1171 value = strtoul(color, &end, 16);
1172 color[size] = t;
1173 if (errno == ERANGE || end - color != size)
1174 break;
1175 switch (size)
1176 {
1177 case 1:
1178 value = value * 0x10;
1179 break;
1180 case 2:
1181 break;
1182 case 3:
1183 value /= 0x10;
1184 break;
1185 case 4:
1186 value /= 0x100;
1187 break;
1188 }
1189 colorval |= (value << pos);
1190 pos += 0x8;
1191 if (i == 2)
1192 {
1193 UNBLOCK_INPUT;
1194 return (colorval);
1195 }
1196 color = end;
1197 }
1198 }
1199 }
1200 else if (strnicmp(colorname, "rgb:", 4) == 0)
1201 {
1202 char *color;
1203 UINT colorval;
1204 int i, pos;
1205 pos = 0;
1206
1207 colorval = 0;
1208 color = colorname + 4;
1209 for (i = 0; i < 3; i++)
1210 {
1211 char *end;
1212 unsigned long value;
7d0393cf 1213
1edf84e7
GV
1214 /* The check for 'x' in the following conditional takes into
1215 account the fact that strtol allows a "0x" in front of
1216 our numbers, and we don't. */
1217 if (!isxdigit(color[0]) || color[1] == 'x')
1218 break;
1219 value = strtoul(color, &end, 16);
1220 if (errno == ERANGE)
1221 break;
1222 switch (end - color)
1223 {
1224 case 1:
1225 value = value * 0x10 + value;
1226 break;
1227 case 2:
1228 break;
1229 case 3:
1230 value /= 0x10;
1231 break;
1232 case 4:
1233 value /= 0x100;
1234 break;
1235 default:
1236 value = ULONG_MAX;
1237 }
1238 if (value == ULONG_MAX)
1239 break;
1240 colorval |= (value << pos);
1241 pos += 0x8;
1242 if (i == 2)
1243 {
1244 if (*end != '\0')
1245 break;
1246 UNBLOCK_INPUT;
1247 return (colorval);
1248 }
1249 if (*end != '/')
1250 break;
1251 color = end + 1;
1252 }
1253 }
1254 else if (strnicmp(colorname, "rgbi:", 5) == 0)
1255 {
1256 /* This is an RGB Intensity specification. */
1257 char *color;
1258 UINT colorval;
1259 int i, pos;
1260 pos = 0;
1261
1262 colorval = 0;
1263 color = colorname + 5;
1264 for (i = 0; i < 3; i++)
1265 {
1266 char *end;
1267 double value;
1268 UINT val;
1269
1270 value = strtod(color, &end);
1271 if (errno == ERANGE)
1272 break;
1273 if (value < 0.0 || value > 1.0)
1274 break;
1275 val = (UINT)(0x100 * value);
7d0393cf 1276 /* We used 0x100 instead of 0xFF to give a continuous
1edf84e7
GV
1277 range between 0.0 and 1.0 inclusive. The next statement
1278 fixes the 1.0 case. */
1279 if (val == 0x100)
1280 val = 0xFF;
1281 colorval |= (val << pos);
1282 pos += 0x8;
1283 if (i == 2)
1284 {
1285 if (*end != '\0')
1286 break;
1287 UNBLOCK_INPUT;
1288 return (colorval);
1289 }
1290 if (*end != '/')
1291 break;
1292 color = end + 1;
1293 }
1294 }
1295 /* I am not going to attempt to handle any of the CIE color schemes
1296 or TekHVC, since I don't know the algorithms for conversion to
1297 RGB. */
f695b4b1
GV
1298
1299 /* If we fail to lookup the color name in w32_color_map, then check the
7d0393cf 1300 colorname to see if it can be crudely approximated: If the X color
f695b4b1
GV
1301 ends in a number (e.g., "darkseagreen2"), strip the number and
1302 return the result of looking up the base color name. */
1303 ret = w32_color_map_lookup (colorname);
7d0393cf 1304 if (NILP (ret))
ee78dc32 1305 {
f695b4b1 1306 int len = strlen (colorname);
ee78dc32 1307
7d0393cf 1308 if (isdigit (colorname[len - 1]))
f695b4b1 1309 {
8b77111c 1310 char *ptr, *approx = alloca (len + 1);
ee78dc32 1311
f695b4b1
GV
1312 strcpy (approx, colorname);
1313 ptr = &approx[len - 1];
7d0393cf 1314 while (ptr > approx && isdigit (*ptr))
f695b4b1 1315 *ptr-- = '\0';
ee78dc32 1316
f695b4b1 1317 ret = w32_color_map_lookup (approx);
ee78dc32 1318 }
ee78dc32 1319 }
7d0393cf 1320
ee78dc32 1321 UNBLOCK_INPUT;
ee78dc32
GV
1322 return ret;
1323}
1324
5ac45f98 1325void
fbd6baed 1326w32_regenerate_palette (FRAME_PTR f)
5ac45f98 1327{
fbd6baed 1328 struct w32_palette_entry * list;
5ac45f98
GV
1329 LOGPALETTE * log_palette;
1330 HPALETTE new_palette;
1331 int i;
1332
1333 /* don't bother trying to create palette if not supported */
fbd6baed 1334 if (! FRAME_W32_DISPLAY_INFO (f)->has_palette)
5ac45f98
GV
1335 return;
1336
1337 log_palette = (LOGPALETTE *)
1338 alloca (sizeof (LOGPALETTE) +
fbd6baed 1339 FRAME_W32_DISPLAY_INFO (f)->num_colors * sizeof (PALETTEENTRY));
5ac45f98 1340 log_palette->palVersion = 0x300;
fbd6baed 1341 log_palette->palNumEntries = FRAME_W32_DISPLAY_INFO (f)->num_colors;
5ac45f98 1342
fbd6baed 1343 list = FRAME_W32_DISPLAY_INFO (f)->color_list;
5ac45f98 1344 for (i = 0;
fbd6baed 1345 i < FRAME_W32_DISPLAY_INFO (f)->num_colors;
5ac45f98
GV
1346 i++, list = list->next)
1347 log_palette->palPalEntry[i] = list->entry;
1348
1349 new_palette = CreatePalette (log_palette);
1350
1351 enter_crit ();
1352
fbd6baed
GV
1353 if (FRAME_W32_DISPLAY_INFO (f)->palette)
1354 DeleteObject (FRAME_W32_DISPLAY_INFO (f)->palette);
1355 FRAME_W32_DISPLAY_INFO (f)->palette = new_palette;
5ac45f98
GV
1356
1357 /* Realize display palette and garbage all frames. */
1358 release_frame_dc (f, get_frame_dc (f));
1359
1360 leave_crit ();
1361}
1362
fbd6baed
GV
1363#define W32_COLOR(pe) RGB (pe.peRed, pe.peGreen, pe.peBlue)
1364#define SET_W32_COLOR(pe, color) \
5ac45f98
GV
1365 do \
1366 { \
1367 pe.peRed = GetRValue (color); \
1368 pe.peGreen = GetGValue (color); \
1369 pe.peBlue = GetBValue (color); \
1370 pe.peFlags = 0; \
1371 } while (0)
1372
1373#if 0
1374/* Keep these around in case we ever want to track color usage. */
1375void
fbd6baed 1376w32_map_color (FRAME_PTR f, COLORREF color)
5ac45f98 1377{
fbd6baed 1378 struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
5ac45f98 1379
fbd6baed 1380 if (NILP (Vw32_enable_palette))
5ac45f98
GV
1381 return;
1382
1383 /* check if color is already mapped */
1384 while (list)
1385 {
fbd6baed 1386 if (W32_COLOR (list->entry) == color)
5ac45f98
GV
1387 {
1388 ++list->refcount;
1389 return;
1390 }
1391 list = list->next;
1392 }
1393
1394 /* not already mapped, so add to list and recreate Windows palette */
fbd6baed
GV
1395 list = (struct w32_palette_entry *)
1396 xmalloc (sizeof (struct w32_palette_entry));
1397 SET_W32_COLOR (list->entry, color);
5ac45f98 1398 list->refcount = 1;
fbd6baed
GV
1399 list->next = FRAME_W32_DISPLAY_INFO (f)->color_list;
1400 FRAME_W32_DISPLAY_INFO (f)->color_list = list;
1401 FRAME_W32_DISPLAY_INFO (f)->num_colors++;
5ac45f98
GV
1402
1403 /* set flag that palette must be regenerated */
fbd6baed 1404 FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
5ac45f98
GV
1405}
1406
1407void
fbd6baed 1408w32_unmap_color (FRAME_PTR f, COLORREF color)
5ac45f98 1409{
fbd6baed
GV
1410 struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
1411 struct w32_palette_entry **prev = &FRAME_W32_DISPLAY_INFO (f)->color_list;
5ac45f98 1412
fbd6baed 1413 if (NILP (Vw32_enable_palette))
5ac45f98
GV
1414 return;
1415
1416 /* check if color is already mapped */
1417 while (list)
1418 {
fbd6baed 1419 if (W32_COLOR (list->entry) == color)
5ac45f98
GV
1420 {
1421 if (--list->refcount == 0)
1422 {
1423 *prev = list->next;
1424 xfree (list);
fbd6baed 1425 FRAME_W32_DISPLAY_INFO (f)->num_colors--;
5ac45f98
GV
1426 break;
1427 }
1428 else
1429 return;
1430 }
1431 prev = &list->next;
1432 list = list->next;
1433 }
1434
1435 /* set flag that palette must be regenerated */
fbd6baed 1436 FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
5ac45f98
GV
1437}
1438#endif
1439
6fc2811b
JR
1440
1441/* Gamma-correct COLOR on frame F. */
1442
1443void
1444gamma_correct (f, color)
1445 struct frame *f;
1446 COLORREF *color;
1447{
1448 if (f->gamma)
1449 {
1450 *color = PALETTERGB (
1451 pow (GetRValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
1452 pow (GetGValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
1453 pow (GetBValue (*color) / 255.0, f->gamma) * 255.0 + 0.5);
1454 }
1455}
1456
1457
ee78dc32
GV
1458/* Decide if color named COLOR is valid for the display associated with
1459 the selected frame; if so, return the rgb values in COLOR_DEF.
1460 If ALLOC is nonzero, allocate a new colormap cell. */
1461
1462int
6fc2811b 1463w32_defined_color (f, color, color_def, alloc)
ee78dc32
GV
1464 FRAME_PTR f;
1465 char *color;
6fc2811b 1466 XColor *color_def;
ee78dc32
GV
1467 int alloc;
1468{
1469 register Lisp_Object tem;
6fc2811b 1470 COLORREF w32_color_ref;
3c190163 1471
fbd6baed 1472 tem = x_to_w32_color (color);
3c190163 1473
7d0393cf 1474 if (!NILP (tem))
ee78dc32 1475 {
d88c567c
JR
1476 if (f)
1477 {
1478 /* Apply gamma correction. */
1479 w32_color_ref = XUINT (tem);
1480 gamma_correct (f, &w32_color_ref);
1481 XSETINT (tem, w32_color_ref);
1482 }
9badad41
JR
1483
1484 /* Map this color to the palette if it is enabled. */
fbd6baed 1485 if (!NILP (Vw32_enable_palette))
5ac45f98 1486 {
fbd6baed 1487 struct w32_palette_entry * entry =
d88c567c 1488 one_w32_display_info.color_list;
fbd6baed 1489 struct w32_palette_entry ** prev =
d88c567c 1490 &one_w32_display_info.color_list;
7d0393cf 1491
5ac45f98
GV
1492 /* check if color is already mapped */
1493 while (entry)
1494 {
fbd6baed 1495 if (W32_COLOR (entry->entry) == XUINT (tem))
5ac45f98
GV
1496 break;
1497 prev = &entry->next;
1498 entry = entry->next;
1499 }
1500
1501 if (entry == NULL && alloc)
1502 {
1503 /* not already mapped, so add to list */
fbd6baed
GV
1504 entry = (struct w32_palette_entry *)
1505 xmalloc (sizeof (struct w32_palette_entry));
1506 SET_W32_COLOR (entry->entry, XUINT (tem));
5ac45f98
GV
1507 entry->next = NULL;
1508 *prev = entry;
d88c567c 1509 one_w32_display_info.num_colors++;
5ac45f98
GV
1510
1511 /* set flag that palette must be regenerated */
d88c567c 1512 one_w32_display_info.regen_palette = TRUE;
5ac45f98
GV
1513 }
1514 }
1515 /* Ensure COLORREF value is snapped to nearest color in (default)
1516 palette by simulating the PALETTERGB macro. This works whether
1517 or not the display device has a palette. */
6fc2811b
JR
1518 w32_color_ref = XUINT (tem) | 0x2000000;
1519
6fc2811b 1520 color_def->pixel = w32_color_ref;
197edd35
JR
1521 color_def->red = GetRValue (w32_color_ref) * 256;
1522 color_def->green = GetGValue (w32_color_ref) * 256;
1523 color_def->blue = GetBValue (w32_color_ref) * 256;
6fc2811b 1524
ee78dc32 1525 return 1;
5ac45f98 1526 }
7d0393cf 1527 else
3c190163
GV
1528 {
1529 return 0;
1530 }
ee78dc32
GV
1531}
1532
1533/* Given a string ARG naming a color, compute a pixel value from it
1534 suitable for screen F.
1535 If F is not a color screen, return DEF (default) regardless of what
1536 ARG says. */
1537
1538int
1539x_decode_color (f, arg, def)
1540 FRAME_PTR f;
1541 Lisp_Object arg;
1542 int def;
1543{
6fc2811b 1544 XColor cdef;
ee78dc32 1545
b7826503 1546 CHECK_STRING (arg);
ee78dc32 1547
d5db4077 1548 if (strcmp (SDATA (arg), "black") == 0)
ee78dc32 1549 return BLACK_PIX_DEFAULT (f);
d5db4077 1550 else if (strcmp (SDATA (arg), "white") == 0)
ee78dc32
GV
1551 return WHITE_PIX_DEFAULT (f);
1552
fbd6baed 1553 if ((FRAME_W32_DISPLAY_INFO (f)->n_planes * FRAME_W32_DISPLAY_INFO (f)->n_cbits) == 1)
ee78dc32
GV
1554 return def;
1555
6fc2811b 1556 /* w32_defined_color is responsible for coping with failures
ee78dc32 1557 by looking for a near-miss. */
d5db4077 1558 if (w32_defined_color (f, SDATA (arg), &cdef, 1))
6fc2811b 1559 return cdef.pixel;
ee78dc32
GV
1560
1561 /* defined_color failed; return an ultimate default. */
1562 return def;
1563}
1564\f
6fc2811b
JR
1565
1566
ee78dc32
GV
1567/* Functions called only from `x_set_frame_param'
1568 to set individual parameters.
1569
fbd6baed 1570 If FRAME_W32_WINDOW (f) is 0,
ee78dc32
GV
1571 the frame is being created and its window does not exist yet.
1572 In that case, just record the parameter's new value
1573 in the standard place; do not attempt to change the window. */
1574
1575void
1576x_set_foreground_color (f, arg, oldval)
1577 struct frame *f;
1578 Lisp_Object arg, oldval;
1579{
3cf3436e
JR
1580 struct w32_output *x = f->output_data.w32;
1581 PIX_TYPE fg, old_fg;
1582
1583 fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1584 old_fg = FRAME_FOREGROUND_PIXEL (f);
1585 FRAME_FOREGROUND_PIXEL (f) = fg;
5ac45f98 1586
fbd6baed 1587 if (FRAME_W32_WINDOW (f) != 0)
ee78dc32 1588 {
3cf3436e
JR
1589 if (x->cursor_pixel == old_fg)
1590 x->cursor_pixel = fg;
1591
6fc2811b 1592 update_face_from_frame_parameter (f, Qforeground_color, arg);
ee78dc32
GV
1593 if (FRAME_VISIBLE_P (f))
1594 redraw_frame (f);
1595 }
1596}
1597
1598void
1599x_set_background_color (f, arg, oldval)
1600 struct frame *f;
1601 Lisp_Object arg, oldval;
1602{
6fc2811b 1603 FRAME_BACKGROUND_PIXEL (f)
ee78dc32
GV
1604 = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
1605
fbd6baed 1606 if (FRAME_W32_WINDOW (f) != 0)
ee78dc32 1607 {
6fc2811b
JR
1608 SetWindowLong (FRAME_W32_WINDOW (f), WND_BACKGROUND_INDEX,
1609 FRAME_BACKGROUND_PIXEL (f));
ee78dc32 1610
6fc2811b 1611 update_face_from_frame_parameter (f, Qbackground_color, arg);
ee78dc32
GV
1612
1613 if (FRAME_VISIBLE_P (f))
1614 redraw_frame (f);
1615 }
1616}
1617
1618void
1619x_set_mouse_color (f, arg, oldval)
1620 struct frame *f;
1621 Lisp_Object arg, oldval;
1622{
7d63e5e3 1623 Cursor cursor, nontext_cursor, mode_cursor, hand_cursor;
dfc465d3 1624 int count;
ee78dc32
GV
1625 int mask_color;
1626
1627 if (!EQ (Qnil, arg))
fbd6baed 1628 f->output_data.w32->mouse_pixel
ee78dc32 1629 = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
6fc2811b
JR
1630 mask_color = FRAME_BACKGROUND_PIXEL (f);
1631
1632 /* Don't let pointers be invisible. */
fbd6baed 1633 if (mask_color == f->output_data.w32->mouse_pixel
6fc2811b
JR
1634 && mask_color == FRAME_BACKGROUND_PIXEL (f))
1635 f->output_data.w32->mouse_pixel = FRAME_FOREGROUND_PIXEL (f);
ee78dc32 1636
767b1ff0 1637#if 0 /* TODO : cursor changes */
ee78dc32
GV
1638 BLOCK_INPUT;
1639
1640 /* It's not okay to crash if the user selects a screwy cursor. */
fadca6c6 1641 count = x_catch_errors (FRAME_W32_DISPLAY (f));
ee78dc32
GV
1642
1643 if (!EQ (Qnil, Vx_pointer_shape))
1644 {
b7826503 1645 CHECK_NUMBER (Vx_pointer_shape);
fbd6baed 1646 cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XINT (Vx_pointer_shape));
ee78dc32
GV
1647 }
1648 else
fbd6baed
GV
1649 cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
1650 x_check_errors (FRAME_W32_DISPLAY (f), "bad text pointer cursor: %s");
ee78dc32
GV
1651
1652 if (!EQ (Qnil, Vx_nontext_pointer_shape))
1653 {
b7826503 1654 CHECK_NUMBER (Vx_nontext_pointer_shape);
fbd6baed 1655 nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
ee78dc32
GV
1656 XINT (Vx_nontext_pointer_shape));
1657 }
1658 else
fbd6baed
GV
1659 nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_left_ptr);
1660 x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
ee78dc32 1661
0af913d7 1662 if (!EQ (Qnil, Vx_hourglass_pointer_shape))
6fc2811b 1663 {
b7826503 1664 CHECK_NUMBER (Vx_hourglass_pointer_shape);
0af913d7
GM
1665 hourglass_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
1666 XINT (Vx_hourglass_pointer_shape));
6fc2811b
JR
1667 }
1668 else
0af913d7 1669 hourglass_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_watch);
6fc2811b 1670 x_check_errors (FRAME_W32_DISPLAY (f), "bad busy pointer cursor: %s");
7d0393cf 1671
6fc2811b 1672 x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
ee78dc32
GV
1673 if (!EQ (Qnil, Vx_mode_pointer_shape))
1674 {
b7826503 1675 CHECK_NUMBER (Vx_mode_pointer_shape);
fbd6baed 1676 mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
ee78dc32
GV
1677 XINT (Vx_mode_pointer_shape));
1678 }
1679 else
fbd6baed
GV
1680 mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
1681 x_check_errors (FRAME_W32_DISPLAY (f), "bad modeline pointer cursor: %s");
ee78dc32
GV
1682
1683 if (!EQ (Qnil, Vx_sensitive_text_pointer_shape))
1684 {
b7826503 1685 CHECK_NUMBER (Vx_sensitive_text_pointer_shape);
7d63e5e3 1686 hand_cursor
fbd6baed 1687 = XCreateFontCursor (FRAME_W32_DISPLAY (f),
ee78dc32
GV
1688 XINT (Vx_sensitive_text_pointer_shape));
1689 }
1690 else
7d63e5e3 1691 hand_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_crosshair);
ee78dc32 1692
4694d762
JR
1693 if (!NILP (Vx_window_horizontal_drag_shape))
1694 {
b7826503 1695 CHECK_NUMBER (Vx_window_horizontal_drag_shape);
4694d762
JR
1696 horizontal_drag_cursor
1697 = XCreateFontCursor (FRAME_X_DISPLAY (f),
1698 XINT (Vx_window_horizontal_drag_shape));
1699 }
1700 else
1701 horizontal_drag_cursor
1702 = XCreateFontCursor (FRAME_X_DISPLAY (f), XC_sb_h_double_arrow);
1703
ee78dc32 1704 /* Check and report errors with the above calls. */
fbd6baed 1705 x_check_errors (FRAME_W32_DISPLAY (f), "can't set cursor shape: %s");
fadca6c6 1706 x_uncatch_errors (FRAME_W32_DISPLAY (f), count);
ee78dc32
GV
1707
1708 {
1709 XColor fore_color, back_color;
1710
fbd6baed 1711 fore_color.pixel = f->output_data.w32->mouse_pixel;
ee78dc32 1712 back_color.pixel = mask_color;
fbd6baed
GV
1713 XQueryColor (FRAME_W32_DISPLAY (f),
1714 DefaultColormap (FRAME_W32_DISPLAY (f),
1715 DefaultScreen (FRAME_W32_DISPLAY (f))),
ee78dc32 1716 &fore_color);
fbd6baed
GV
1717 XQueryColor (FRAME_W32_DISPLAY (f),
1718 DefaultColormap (FRAME_W32_DISPLAY (f),
1719 DefaultScreen (FRAME_W32_DISPLAY (f))),
ee78dc32 1720 &back_color);
fbd6baed 1721 XRecolorCursor (FRAME_W32_DISPLAY (f), cursor,
ee78dc32 1722 &fore_color, &back_color);
fbd6baed 1723 XRecolorCursor (FRAME_W32_DISPLAY (f), nontext_cursor,
ee78dc32 1724 &fore_color, &back_color);
fbd6baed 1725 XRecolorCursor (FRAME_W32_DISPLAY (f), mode_cursor,
ee78dc32 1726 &fore_color, &back_color);
7d63e5e3 1727 XRecolorCursor (FRAME_W32_DISPLAY (f), hand_cursor,
ee78dc32 1728 &fore_color, &back_color);
0af913d7 1729 XRecolorCursor (FRAME_W32_DISPLAY (f), hourglass_cursor,
6fc2811b 1730 &fore_color, &back_color);
ee78dc32
GV
1731 }
1732
fbd6baed 1733 if (FRAME_W32_WINDOW (f) != 0)
6fc2811b 1734 XDefineCursor (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), cursor);
ee78dc32 1735
fbd6baed
GV
1736 if (cursor != f->output_data.w32->text_cursor && f->output_data.w32->text_cursor != 0)
1737 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->text_cursor);
1738 f->output_data.w32->text_cursor = cursor;
1739
1740 if (nontext_cursor != f->output_data.w32->nontext_cursor
1741 && f->output_data.w32->nontext_cursor != 0)
1742 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->nontext_cursor);
1743 f->output_data.w32->nontext_cursor = nontext_cursor;
1744
0af913d7
GM
1745 if (hourglass_cursor != f->output_data.w32->hourglass_cursor
1746 && f->output_data.w32->hourglass_cursor != 0)
1747 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->hourglass_cursor);
1748 f->output_data.w32->hourglass_cursor = hourglass_cursor;
6fc2811b 1749
fbd6baed
GV
1750 if (mode_cursor != f->output_data.w32->modeline_cursor
1751 && f->output_data.w32->modeline_cursor != 0)
1752 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->modeline_cursor);
1753 f->output_data.w32->modeline_cursor = mode_cursor;
7d0393cf 1754
7d63e5e3
KS
1755 if (hand_cursor != f->output_data.w32->hand_cursor
1756 && f->output_data.w32->hand_cursor != 0)
1757 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->hand_cursor);
1758 f->output_data.w32->hand_cursor = hand_cursor;
fbd6baed
GV
1759
1760 XFlush (FRAME_W32_DISPLAY (f));
ee78dc32 1761 UNBLOCK_INPUT;
6fc2811b
JR
1762
1763 update_face_from_frame_parameter (f, Qmouse_color, arg);
767b1ff0 1764#endif /* TODO */
ee78dc32
GV
1765}
1766
70a0239a 1767/* Defined in w32term.c. */
ee78dc32
GV
1768void
1769x_set_cursor_color (f, arg, oldval)
1770 struct frame *f;
1771 Lisp_Object arg, oldval;
1772{
70a0239a 1773 unsigned long fore_pixel, pixel;
ee78dc32 1774
dfff8a69 1775 if (!NILP (Vx_cursor_fore_pixel))
ee78dc32 1776 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
70a0239a 1777 WHITE_PIX_DEFAULT (f));
ee78dc32 1778 else
6fc2811b 1779 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
70a0239a 1780
6759f872 1781 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
7d0393cf 1782
ee78dc32 1783 /* Make sure that the cursor color differs from the background color. */
70a0239a 1784 if (pixel == FRAME_BACKGROUND_PIXEL (f))
ee78dc32 1785 {
70a0239a
JR
1786 pixel = f->output_data.w32->mouse_pixel;
1787 if (pixel == fore_pixel)
6fc2811b 1788 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
ee78dc32 1789 }
70a0239a 1790
ac849ba4 1791 f->output_data.w32->cursor_foreground_pixel = fore_pixel;
70a0239a 1792 f->output_data.w32->cursor_pixel = pixel;
ee78dc32 1793
fbd6baed 1794 if (FRAME_W32_WINDOW (f) != 0)
ee78dc32 1795 {
0327b4cc
JR
1796 BLOCK_INPUT;
1797 /* Update frame's cursor_gc. */
1798 f->output_data.w32->cursor_gc->foreground = fore_pixel;
1799 f->output_data.w32->cursor_gc->background = pixel;
1800
1801 UNBLOCK_INPUT;
1802
ee78dc32
GV
1803 if (FRAME_VISIBLE_P (f))
1804 {
70a0239a
JR
1805 x_update_cursor (f, 0);
1806 x_update_cursor (f, 1);
ee78dc32
GV
1807 }
1808 }
6fc2811b
JR
1809
1810 update_face_from_frame_parameter (f, Qcursor_color, arg);
ee78dc32
GV
1811}
1812
33d52f9c
GV
1813/* Set the border-color of frame F to pixel value PIX.
1814 Note that this does not fully take effect if done before
7d0393cf 1815 F has a window. */
6d906347 1816
33d52f9c
GV
1817void
1818x_set_border_pixel (f, pix)
1819 struct frame *f;
1820 int pix;
1821{
6d906347 1822
33d52f9c
GV
1823 f->output_data.w32->border_pixel = pix;
1824
be786000 1825 if (FRAME_W32_WINDOW (f) != 0 && f->border_width > 0)
33d52f9c
GV
1826 {
1827 if (FRAME_VISIBLE_P (f))
1828 redraw_frame (f);
1829 }
1830}
1831
ee78dc32
GV
1832/* Set the border-color of frame F to value described by ARG.
1833 ARG can be a string naming a color.
1834 The border-color is used for the border that is drawn by the server.
1835 Note that this does not fully take effect if done before
1836 F has a window; it must be redone when the window is created. */
1837
1838void
1839x_set_border_color (f, arg, oldval)
1840 struct frame *f;
1841 Lisp_Object arg, oldval;
1842{
ee78dc32
GV
1843 int pix;
1844
b7826503 1845 CHECK_STRING (arg);
ee78dc32 1846 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
ee78dc32 1847 x_set_border_pixel (f, pix);
6fc2811b 1848 update_face_from_frame_parameter (f, Qborder_color, arg);
ee78dc32
GV
1849}
1850
dfff8a69
JR
1851
1852void
1853x_set_cursor_type (f, arg, oldval)
1854 FRAME_PTR f;
1855 Lisp_Object arg, oldval;
1856{
50e363e6 1857 set_frame_cursor_types (f, arg);
ee78dc32 1858
623cdbf2 1859 /* Make sure the cursor gets redrawn. */
c922a224 1860 cursor_type_changed = 1;
ee78dc32 1861}
dfff8a69 1862\f
ee78dc32
GV
1863void
1864x_set_icon_type (f, arg, oldval)
1865 struct frame *f;
1866 Lisp_Object arg, oldval;
1867{
ee78dc32
GV
1868 int result;
1869
eb7576ce
GV
1870 if (NILP (arg) && NILP (oldval))
1871 return;
1872
7d0393cf 1873 if (STRINGP (arg) && STRINGP (oldval)
eb7576ce
GV
1874 && EQ (Fstring_equal (oldval, arg), Qt))
1875 return;
1876
1877 if (SYMBOLP (arg) && SYMBOLP (oldval) && EQ (arg, oldval))
ee78dc32
GV
1878 return;
1879
1880 BLOCK_INPUT;
ee78dc32 1881
eb7576ce 1882 result = x_bitmap_icon (f, arg);
ee78dc32
GV
1883 if (result)
1884 {
1885 UNBLOCK_INPUT;
1886 error ("No icon window available");
1887 }
1888
ee78dc32 1889 UNBLOCK_INPUT;
ee78dc32
GV
1890}
1891
ee78dc32
GV
1892void
1893x_set_icon_name (f, arg, oldval)
1894 struct frame *f;
1895 Lisp_Object arg, oldval;
1896{
ee78dc32
GV
1897 if (STRINGP (arg))
1898 {
1899 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1900 return;
1901 }
1902 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
1903 return;
1904
1905 f->icon_name = arg;
1906
1907#if 0
fbd6baed 1908 if (f->output_data.w32->icon_bitmap != 0)
ee78dc32
GV
1909 return;
1910
1911 BLOCK_INPUT;
1912
1913 result = x_text_icon (f,
d5db4077
KR
1914 (char *) SDATA ((!NILP (f->icon_name)
1915 ? f->icon_name
1916 : !NILP (f->title)
1917 ? f->title
1918 : f->name)));
ee78dc32
GV
1919
1920 if (result)
1921 {
1922 UNBLOCK_INPUT;
1923 error ("No icon window available");
1924 }
1925
1926 /* If the window was unmapped (and its icon was mapped),
1927 the new icon is not mapped, so map the window in its stead. */
1928 if (FRAME_VISIBLE_P (f))
1929 {
1930#ifdef USE_X_TOOLKIT
fbd6baed 1931 XtPopup (f->output_data.w32->widget, XtGrabNone);
ee78dc32 1932#endif
fbd6baed 1933 XMapWindow (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f));
ee78dc32
GV
1934 }
1935
fbd6baed 1936 XFlush (FRAME_W32_DISPLAY (f));
ee78dc32
GV
1937 UNBLOCK_INPUT;
1938#endif
1939}
1940
a1258667 1941\f
ee78dc32
GV
1942void
1943x_set_menu_bar_lines (f, value, oldval)
1944 struct frame *f;
1945 Lisp_Object value, oldval;
1946{
1947 int nlines;
1948 int olines = FRAME_MENU_BAR_LINES (f);
1949
1950 /* Right now, menu bars don't work properly in minibuf-only frames;
1951 most of the commands try to apply themselves to the minibuffer
6fc2811b 1952 frame itself, and get an error because you can't switch buffers
ee78dc32
GV
1953 in or split the minibuffer window. */
1954 if (FRAME_MINIBUF_ONLY_P (f))
1955 return;
1956
1957 if (INTEGERP (value))
1958 nlines = XINT (value);
1959 else
1960 nlines = 0;
1961
1962 FRAME_MENU_BAR_LINES (f) = 0;
1963 if (nlines)
1964 FRAME_EXTERNAL_MENU_BAR (f) = 1;
1965 else
1966 {
1967 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
1968 free_frame_menubar (f);
1969 FRAME_EXTERNAL_MENU_BAR (f) = 0;
1edf84e7
GV
1970
1971 /* Adjust the frame size so that the client (text) dimensions
1972 remain the same. This depends on FRAME_EXTERNAL_MENU_BAR being
1973 set correctly. */
be786000 1974 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
6fc2811b 1975 do_pending_window_change (0);
ee78dc32 1976 }
6fc2811b
JR
1977 adjust_glyphs (f);
1978}
1979
1980
1981/* Set the number of lines used for the tool bar of frame F to VALUE.
1982 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
1983 is the old number of tool bar lines. This function changes the
1984 height of all windows on frame F to match the new tool bar height.
1985 The frame's height doesn't change. */
1986
1987void
1988x_set_tool_bar_lines (f, value, oldval)
1989 struct frame *f;
1990 Lisp_Object value, oldval;
1991{
36f8209a
JR
1992 int delta, nlines, root_height;
1993 Lisp_Object root_window;
6fc2811b 1994
dc220243
JR
1995 /* Treat tool bars like menu bars. */
1996 if (FRAME_MINIBUF_ONLY_P (f))
1997 return;
1998
6fc2811b
JR
1999 /* Use VALUE only if an integer >= 0. */
2000 if (INTEGERP (value) && XINT (value) >= 0)
2001 nlines = XFASTINT (value);
2002 else
2003 nlines = 0;
2004
2005 /* Make sure we redisplay all windows in this frame. */
2006 ++windows_or_buffers_changed;
2007
2008 delta = nlines - FRAME_TOOL_BAR_LINES (f);
36f8209a
JR
2009
2010 /* Don't resize the tool-bar to more than we have room for. */
2011 root_window = FRAME_ROOT_WINDOW (f);
be786000 2012 root_height = WINDOW_TOTAL_LINES (XWINDOW (root_window));
36f8209a
JR
2013 if (root_height - delta < 1)
2014 {
2015 delta = root_height - 1;
2016 nlines = FRAME_TOOL_BAR_LINES (f) + delta;
2017 }
2018
6fc2811b 2019 FRAME_TOOL_BAR_LINES (f) = nlines;
6d906347 2020 change_window_heights (root_window, delta);
6fc2811b 2021 adjust_glyphs (f);
36f8209a
JR
2022
2023 /* We also have to make sure that the internal border at the top of
2024 the frame, below the menu bar or tool bar, is redrawn when the
2025 tool bar disappears. This is so because the internal border is
2026 below the tool bar if one is displayed, but is below the menu bar
2027 if there isn't a tool bar. The tool bar draws into the area
2028 below the menu bar. */
2029 if (FRAME_W32_WINDOW (f) && FRAME_TOOL_BAR_LINES (f) == 0)
2030 {
2031 updating_frame = f;
2032 clear_frame ();
2033 clear_current_matrices (f);
2034 updating_frame = NULL;
2035 }
2036
2037 /* If the tool bar gets smaller, the internal border below it
2038 has to be cleared. It was formerly part of the display
2039 of the larger tool bar, and updating windows won't clear it. */
2040 if (delta < 0)
2041 {
2042 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
be786000
KS
2043 int width = FRAME_PIXEL_WIDTH (f);
2044 int y = nlines * FRAME_LINE_HEIGHT (f);
36f8209a
JR
2045
2046 BLOCK_INPUT;
2047 {
2048 HDC hdc = get_frame_dc (f);
2049 w32_clear_area (f, hdc, 0, y, width, height);
2050 release_frame_dc (f, hdc);
2051 }
2052 UNBLOCK_INPUT;
3cf3436e
JR
2053
2054 if (WINDOWP (f->tool_bar_window))
2055 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
36f8209a 2056 }
ee78dc32
GV
2057}
2058
6fc2811b 2059
ee78dc32 2060/* Change the name of frame F to NAME. If NAME is nil, set F's name to
fbd6baed 2061 w32_id_name.
ee78dc32
GV
2062
2063 If EXPLICIT is non-zero, that indicates that lisp code is setting the
2064 name; if NAME is a string, set F's name to NAME and set
2065 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
2066
2067 If EXPLICIT is zero, that indicates that Emacs redisplay code is
2068 suggesting a new name, which lisp code should override; if
2069 F->explicit_name is set, ignore the new name; otherwise, set it. */
2070
2071void
2072x_set_name (f, name, explicit)
2073 struct frame *f;
2074 Lisp_Object name;
2075 int explicit;
2076{
7d0393cf 2077 /* Make sure that requests from lisp code override requests from
ee78dc32
GV
2078 Emacs redisplay code. */
2079 if (explicit)
2080 {
2081 /* If we're switching from explicit to implicit, we had better
2082 update the mode lines and thereby update the title. */
2083 if (f->explicit_name && NILP (name))
2084 update_mode_lines = 1;
2085
2086 f->explicit_name = ! NILP (name);
2087 }
2088 else if (f->explicit_name)
2089 return;
2090
fbd6baed 2091 /* If NAME is nil, set the name to the w32_id_name. */
ee78dc32
GV
2092 if (NILP (name))
2093 {
2094 /* Check for no change needed in this very common case
2095 before we do any consing. */
fbd6baed 2096 if (!strcmp (FRAME_W32_DISPLAY_INFO (f)->w32_id_name,
d5db4077 2097 SDATA (f->name)))
ee78dc32 2098 return;
fbd6baed 2099 name = build_string (FRAME_W32_DISPLAY_INFO (f)->w32_id_name);
ee78dc32
GV
2100 }
2101 else
b7826503 2102 CHECK_STRING (name);
ee78dc32
GV
2103
2104 /* Don't change the name if it's already NAME. */
2105 if (! NILP (Fstring_equal (name, f->name)))
2106 return;
2107
1edf84e7
GV
2108 f->name = name;
2109
2110 /* For setting the frame title, the title parameter should override
2111 the name parameter. */
2112 if (! NILP (f->title))
2113 name = f->title;
2114
fbd6baed 2115 if (FRAME_W32_WINDOW (f))
ee78dc32 2116 {
6fc2811b 2117 if (STRING_MULTIBYTE (name))
dfff8a69 2118 name = ENCODE_SYSTEM (name);
6fc2811b 2119
ee78dc32 2120 BLOCK_INPUT;
d5db4077 2121 SetWindowText(FRAME_W32_WINDOW (f), SDATA (name));
ee78dc32
GV
2122 UNBLOCK_INPUT;
2123 }
ee78dc32
GV
2124}
2125
2126/* This function should be called when the user's lisp code has
2127 specified a name for the frame; the name will override any set by the
2128 redisplay code. */
2129void
2130x_explicitly_set_name (f, arg, oldval)
2131 FRAME_PTR f;
2132 Lisp_Object arg, oldval;
2133{
2134 x_set_name (f, arg, 1);
2135}
2136
2137/* This function should be called by Emacs redisplay code to set the
2138 name; names set this way will never override names set by the user's
2139 lisp code. */
2140void
2141x_implicitly_set_name (f, arg, oldval)
2142 FRAME_PTR f;
2143 Lisp_Object arg, oldval;
2144{
2145 x_set_name (f, arg, 0);
2146}
1edf84e7
GV
2147\f
2148/* Change the title of frame F to NAME.
2149 If NAME is nil, use the frame name as the title.
2150
2151 If EXPLICIT is non-zero, that indicates that lisp code is setting the
2152 name; if NAME is a string, set F's name to NAME and set
2153 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
2154
2155 If EXPLICIT is zero, that indicates that Emacs redisplay code is
2156 suggesting a new name, which lisp code should override; if
2157 F->explicit_name is set, ignore the new name; otherwise, set it. */
ee78dc32 2158
1edf84e7 2159void
6fc2811b 2160x_set_title (f, name, old_name)
1edf84e7 2161 struct frame *f;
6fc2811b 2162 Lisp_Object name, old_name;
1edf84e7
GV
2163{
2164 /* Don't change the title if it's already NAME. */
2165 if (EQ (name, f->title))
2166 return;
2167
2168 update_mode_lines = 1;
2169
2170 f->title = name;
2171
2172 if (NILP (name))
2173 name = f->name;
2174
2175 if (FRAME_W32_WINDOW (f))
2176 {
6fc2811b 2177 if (STRING_MULTIBYTE (name))
dfff8a69 2178 name = ENCODE_SYSTEM (name);
6fc2811b 2179
1edf84e7 2180 BLOCK_INPUT;
d5db4077 2181 SetWindowText(FRAME_W32_WINDOW (f), SDATA (name));
1edf84e7
GV
2182 UNBLOCK_INPUT;
2183 }
2184}
ee78dc32 2185
ee78dc32 2186
19f093e5 2187void x_set_scroll_bar_default_width (f)
ee78dc32 2188 struct frame *f;
ee78dc32 2189{
be786000 2190 int wid = FRAME_COLUMN_WIDTH (f);
6fc2811b 2191
be786000
KS
2192 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = GetSystemMetrics (SM_CXVSCROLL);
2193 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) +
2194 wid - 1) / wid;
ee78dc32 2195}
6d906347 2196
ee78dc32 2197\f
7d0393cf 2198/* Subroutines of creating a frame. */
ee78dc32 2199
ee78dc32
GV
2200
2201/* Return the value of parameter PARAM.
2202
2203 First search ALIST, then Vdefault_frame_alist, then the X defaults
2204 database, using ATTRIBUTE as the attribute name and CLASS as its class.
2205
2206 Convert the resource to the type specified by desired_type.
2207
2208 If no default is specified, return Qunbound. If you call
6fc2811b 2209 w32_get_arg, make sure you deal with Qunbound in a reasonable way,
ee78dc32
GV
2210 and don't let it get stored in any Lisp-visible variables! */
2211
2212static Lisp_Object
6fc2811b 2213w32_get_arg (alist, param, attribute, class, type)
ee78dc32
GV
2214 Lisp_Object alist, param;
2215 char *attribute;
2216 char *class;
2217 enum resource_types type;
2218{
6d906347
KS
2219 return x_get_arg (check_x_display_info (Qnil),
2220 alist, param, attribute, class, type);
ee78dc32
GV
2221}
2222
2223\f
c9b2104d
JR
2224Cursor
2225w32_load_cursor (LPCTSTR name)
2226{
2227 /* Try first to load cursor from application resource. */
2228 Cursor cursor = LoadImage ((HINSTANCE) GetModuleHandle(NULL),
2229 name, IMAGE_CURSOR, 0, 0,
2230 LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_SHARED);
2231 if (!cursor)
2232 {
2233 /* Then try to load a shared predefined cursor. */
2234 cursor = LoadImage (NULL, name, IMAGE_CURSOR, 0, 0,
2235 LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_SHARED);
2236 }
2237 return cursor;
2238}
ee78dc32 2239
fbd6baed 2240extern LRESULT CALLBACK w32_wnd_proc ();
ee78dc32 2241
7d0393cf 2242BOOL
fbd6baed 2243w32_init_class (hinst)
ee78dc32
GV
2244 HINSTANCE hinst;
2245{
2246 WNDCLASS wc;
2247
5ac45f98 2248 wc.style = CS_HREDRAW | CS_VREDRAW;
fbd6baed 2249 wc.lpfnWndProc = (WNDPROC) w32_wnd_proc;
ee78dc32
GV
2250 wc.cbClsExtra = 0;
2251 wc.cbWndExtra = WND_EXTRA_BYTES;
2252 wc.hInstance = hinst;
2253 wc.hIcon = LoadIcon (hinst, EMACS_CLASS);
c9b2104d 2254 wc.hCursor = w32_load_cursor (IDC_ARROW);
4587b026 2255 wc.hbrBackground = NULL; /* GetStockObject (WHITE_BRUSH); */
ee78dc32
GV
2256 wc.lpszMenuName = NULL;
2257 wc.lpszClassName = EMACS_CLASS;
2258
2259 return (RegisterClass (&wc));
2260}
2261
7d0393cf 2262HWND
fbd6baed 2263w32_createscrollbar (f, bar)
ee78dc32
GV
2264 struct frame *f;
2265 struct scroll_bar * bar;
2266{
2267 return (CreateWindow ("SCROLLBAR", "", SBS_VERT | WS_CHILD | WS_VISIBLE,
2268 /* Position and size of scroll bar. */
6fc2811b 2269 XINT(bar->left) + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7d0393cf 2270 XINT(bar->top),
6fc2811b
JR
2271 XINT(bar->width) - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
2272 XINT(bar->height),
fbd6baed 2273 FRAME_W32_WINDOW (f),
ee78dc32
GV
2274 NULL,
2275 hinst,
2276 NULL));
2277}
2278
7d0393cf 2279void
fbd6baed 2280w32_createwindow (f)
ee78dc32
GV
2281 struct frame *f;
2282{
2283 HWND hwnd;
1edf84e7
GV
2284 RECT rect;
2285
2286 rect.left = rect.top = 0;
be786000
KS
2287 rect.right = FRAME_PIXEL_WIDTH (f);
2288 rect.bottom = FRAME_PIXEL_HEIGHT (f);
7d0393cf 2289
1edf84e7
GV
2290 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
2291 FRAME_EXTERNAL_MENU_BAR (f));
7d0393cf 2292
ee78dc32 2293 /* Do first time app init */
7d0393cf 2294
ee78dc32
GV
2295 if (!hprevinst)
2296 {
fbd6baed 2297 w32_init_class (hinst);
ee78dc32 2298 }
7d0393cf 2299
1edf84e7
GV
2300 FRAME_W32_WINDOW (f) = hwnd
2301 = CreateWindow (EMACS_CLASS,
2302 f->namebuf,
9ead1b60 2303 f->output_data.w32->dwStyle | WS_CLIPCHILDREN,
be786000
KS
2304 f->left_pos,
2305 f->top_pos,
1edf84e7
GV
2306 rect.right - rect.left,
2307 rect.bottom - rect.top,
2308 NULL,
2309 NULL,
2310 hinst,
2311 NULL);
2312
ee78dc32
GV
2313 if (hwnd)
2314 {
be786000
KS
2315 SetWindowLong (hwnd, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
2316 SetWindowLong (hwnd, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
2317 SetWindowLong (hwnd, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
2318 SetWindowLong (hwnd, WND_SCROLLBAR_INDEX, f->scroll_bar_actual_width);
6fc2811b 2319 SetWindowLong (hwnd, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
ee78dc32 2320
cb9e33d4
RS
2321 /* Enable drag-n-drop. */
2322 DragAcceptFiles (hwnd, TRUE);
7d0393cf 2323
5ac45f98
GV
2324 /* Do this to discard the default setting specified by our parent. */
2325 ShowWindow (hwnd, SW_HIDE);
3c190163 2326 }
3c190163
GV
2327}
2328
7d0393cf 2329void
ee78dc32 2330my_post_msg (wmsg, hwnd, msg, wParam, lParam)
fbd6baed 2331 W32Msg * wmsg;
ee78dc32
GV
2332 HWND hwnd;
2333 UINT msg;
2334 WPARAM wParam;
2335 LPARAM lParam;
2336{
2337 wmsg->msg.hwnd = hwnd;
2338 wmsg->msg.message = msg;
2339 wmsg->msg.wParam = wParam;
2340 wmsg->msg.lParam = lParam;
2341 wmsg->msg.time = GetMessageTime ();
2342
2343 post_msg (wmsg);
2344}
2345
e9e23e23 2346/* GetKeyState and MapVirtualKey on Windows 95 do not actually distinguish
a1a80b40
GV
2347 between left and right keys as advertised. We test for this
2348 support dynamically, and set a flag when the support is absent. If
2349 absent, we keep track of the left and right control and alt keys
2350 ourselves. This is particularly necessary on keyboards that rely
2351 upon the AltGr key, which is represented as having the left control
2352 and right alt keys pressed. For these keyboards, we need to know
2353 when the left alt key has been pressed in addition to the AltGr key
2354 so that we can properly support M-AltGr-key sequences (such as M-@
2355 on Swedish keyboards). */
2356
2357#define EMACS_LCONTROL 0
2358#define EMACS_RCONTROL 1
2359#define EMACS_LMENU 2
2360#define EMACS_RMENU 3
2361
2362static int modifiers[4];
2363static int modifiers_recorded;
2364static int modifier_key_support_tested;
2365
2366static void
2367test_modifier_support (unsigned int wparam)
2368{
2369 unsigned int l, r;
2370
2371 if (wparam != VK_CONTROL && wparam != VK_MENU)
2372 return;
2373 if (wparam == VK_CONTROL)
2374 {
2375 l = VK_LCONTROL;
2376 r = VK_RCONTROL;
2377 }
2378 else
2379 {
2380 l = VK_LMENU;
2381 r = VK_RMENU;
2382 }
2383 if (!(GetKeyState (l) & 0x8000) && !(GetKeyState (r) & 0x8000))
2384 modifiers_recorded = 1;
2385 else
2386 modifiers_recorded = 0;
2387 modifier_key_support_tested = 1;
2388}
2389
2390static void
2391record_keydown (unsigned int wparam, unsigned int lparam)
2392{
2393 int i;
2394
2395 if (!modifier_key_support_tested)
2396 test_modifier_support (wparam);
2397
2398 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
2399 return;
2400
2401 if (wparam == VK_CONTROL)
2402 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
2403 else
2404 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
2405
2406 modifiers[i] = 1;
2407}
2408
2409static void
2410record_keyup (unsigned int wparam, unsigned int lparam)
2411{
2412 int i;
2413
2414 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
2415 return;
2416
2417 if (wparam == VK_CONTROL)
2418 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
2419 else
2420 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
2421
2422 modifiers[i] = 0;
2423}
2424
da36a4d6 2425/* Emacs can lose focus while a modifier key has been pressed. When
7d0393cf 2426 it regains focus, be conservative and clear all modifiers since
da36a4d6
GV
2427 we cannot reconstruct the left and right modifier state. */
2428static void
2429reset_modifiers ()
2430{
8681157a
RS
2431 SHORT ctrl, alt;
2432
adcc3809
GV
2433 if (GetFocus () == NULL)
2434 /* Emacs doesn't have keyboard focus. Do nothing. */
da36a4d6 2435 return;
8681157a
RS
2436
2437 ctrl = GetAsyncKeyState (VK_CONTROL);
2438 alt = GetAsyncKeyState (VK_MENU);
2439
8681157a
RS
2440 if (!(ctrl & 0x08000))
2441 /* Clear any recorded control modifier state. */
2442 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
2443
2444 if (!(alt & 0x08000))
2445 /* Clear any recorded alt modifier state. */
2446 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
2447
adcc3809
GV
2448 /* Update the state of all modifier keys, because modifiers used in
2449 hot-key combinations can get stuck on if Emacs loses focus as a
2450 result of a hot-key being pressed. */
2451 {
2452 BYTE keystate[256];
2453
2454#define CURRENT_STATE(key) ((GetAsyncKeyState (key) & 0x8000) >> 8)
2455
2456 GetKeyboardState (keystate);
2457 keystate[VK_SHIFT] = CURRENT_STATE (VK_SHIFT);
2458 keystate[VK_CONTROL] = CURRENT_STATE (VK_CONTROL);
2459 keystate[VK_LCONTROL] = CURRENT_STATE (VK_LCONTROL);
2460 keystate[VK_RCONTROL] = CURRENT_STATE (VK_RCONTROL);
2461 keystate[VK_MENU] = CURRENT_STATE (VK_MENU);
2462 keystate[VK_LMENU] = CURRENT_STATE (VK_LMENU);
2463 keystate[VK_RMENU] = CURRENT_STATE (VK_RMENU);
2464 keystate[VK_LWIN] = CURRENT_STATE (VK_LWIN);
2465 keystate[VK_RWIN] = CURRENT_STATE (VK_RWIN);
2466 keystate[VK_APPS] = CURRENT_STATE (VK_APPS);
2467 SetKeyboardState (keystate);
2468 }
da36a4d6
GV
2469}
2470
7830e24b
RS
2471/* Synchronize modifier state with what is reported with the current
2472 keystroke. Even if we cannot distinguish between left and right
2473 modifier keys, we know that, if no modifiers are set, then neither
2474 the left or right modifier should be set. */
2475static void
2476sync_modifiers ()
2477{
2478 if (!modifiers_recorded)
2479 return;
2480
7d0393cf 2481 if (!(GetKeyState (VK_CONTROL) & 0x8000))
7830e24b
RS
2482 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
2483
7d0393cf 2484 if (!(GetKeyState (VK_MENU) & 0x8000))
7830e24b
RS
2485 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
2486}
2487
a1a80b40
GV
2488static int
2489modifier_set (int vkey)
2490{
ccc2d29c 2491 if (vkey == VK_CAPITAL || vkey == VK_SCROLL)
891560d6 2492 return (GetKeyState (vkey) & 0x1);
a1a80b40
GV
2493 if (!modifiers_recorded)
2494 return (GetKeyState (vkey) & 0x8000);
2495
2496 switch (vkey)
2497 {
2498 case VK_LCONTROL:
2499 return modifiers[EMACS_LCONTROL];
2500 case VK_RCONTROL:
2501 return modifiers[EMACS_RCONTROL];
2502 case VK_LMENU:
2503 return modifiers[EMACS_LMENU];
2504 case VK_RMENU:
2505 return modifiers[EMACS_RMENU];
a1a80b40
GV
2506 }
2507 return (GetKeyState (vkey) & 0x8000);
2508}
2509
ccc2d29c
GV
2510/* Convert between the modifier bits W32 uses and the modifier bits
2511 Emacs uses. */
2512
2513unsigned int
2514w32_key_to_modifier (int key)
2515{
2516 Lisp_Object key_mapping;
2517
2518 switch (key)
2519 {
2520 case VK_LWIN:
2521 key_mapping = Vw32_lwindow_modifier;
2522 break;
2523 case VK_RWIN:
2524 key_mapping = Vw32_rwindow_modifier;
2525 break;
2526 case VK_APPS:
2527 key_mapping = Vw32_apps_modifier;
2528 break;
2529 case VK_SCROLL:
2530 key_mapping = Vw32_scroll_lock_modifier;
2531 break;
2532 default:
2533 key_mapping = Qnil;
2534 }
2535
adcc3809
GV
2536 /* NB. This code runs in the input thread, asychronously to the lisp
2537 thread, so we must be careful to ensure access to lisp data is
2538 thread-safe. The following code is safe because the modifier
2539 variable values are updated atomically from lisp and symbols are
2540 not relocated by GC. Also, we don't have to worry about seeing GC
2541 markbits here. */
2542 if (EQ (key_mapping, Qhyper))
ccc2d29c 2543 return hyper_modifier;
adcc3809 2544 if (EQ (key_mapping, Qsuper))
ccc2d29c 2545 return super_modifier;
adcc3809 2546 if (EQ (key_mapping, Qmeta))
ccc2d29c 2547 return meta_modifier;
adcc3809 2548 if (EQ (key_mapping, Qalt))
ccc2d29c 2549 return alt_modifier;
adcc3809 2550 if (EQ (key_mapping, Qctrl))
ccc2d29c 2551 return ctrl_modifier;
adcc3809 2552 if (EQ (key_mapping, Qcontrol)) /* synonym for ctrl */
ccc2d29c 2553 return ctrl_modifier;
adcc3809 2554 if (EQ (key_mapping, Qshift))
ccc2d29c
GV
2555 return shift_modifier;
2556
2557 /* Don't generate any modifier if not explicitly requested. */
2558 return 0;
2559}
2560
2561unsigned int
2562w32_get_modifiers ()
2563{
2564 return ((modifier_set (VK_SHIFT) ? shift_modifier : 0) |
2565 (modifier_set (VK_CONTROL) ? ctrl_modifier : 0) |
2566 (modifier_set (VK_LWIN) ? w32_key_to_modifier (VK_LWIN) : 0) |
2567 (modifier_set (VK_RWIN) ? w32_key_to_modifier (VK_RWIN) : 0) |
2568 (modifier_set (VK_APPS) ? w32_key_to_modifier (VK_APPS) : 0) |
2569 (modifier_set (VK_SCROLL) ? w32_key_to_modifier (VK_SCROLL) : 0) |
2570 (modifier_set (VK_MENU) ?
2571 ((NILP (Vw32_alt_is_meta)) ? alt_modifier : meta_modifier) : 0));
2572}
2573
a1a80b40
GV
2574/* We map the VK_* modifiers into console modifier constants
2575 so that we can use the same routines to handle both console
2576 and window input. */
2577
2578static int
ccc2d29c 2579construct_console_modifiers ()
a1a80b40
GV
2580{
2581 int mods;
2582
a1a80b40
GV
2583 mods = 0;
2584 mods |= (modifier_set (VK_SHIFT)) ? SHIFT_PRESSED : 0;
2585 mods |= (modifier_set (VK_CAPITAL)) ? CAPSLOCK_ON : 0;
ccc2d29c
GV
2586 mods |= (modifier_set (VK_SCROLL)) ? SCROLLLOCK_ON : 0;
2587 mods |= (modifier_set (VK_NUMLOCK)) ? NUMLOCK_ON : 0;
a1a80b40
GV
2588 mods |= (modifier_set (VK_LCONTROL)) ? LEFT_CTRL_PRESSED : 0;
2589 mods |= (modifier_set (VK_RCONTROL)) ? RIGHT_CTRL_PRESSED : 0;
2590 mods |= (modifier_set (VK_LMENU)) ? LEFT_ALT_PRESSED : 0;
2591 mods |= (modifier_set (VK_RMENU)) ? RIGHT_ALT_PRESSED : 0;
ccc2d29c
GV
2592 mods |= (modifier_set (VK_LWIN)) ? LEFT_WIN_PRESSED : 0;
2593 mods |= (modifier_set (VK_RWIN)) ? RIGHT_WIN_PRESSED : 0;
2594 mods |= (modifier_set (VK_APPS)) ? APPS_PRESSED : 0;
a1a80b40
GV
2595
2596 return mods;
2597}
2598
ccc2d29c
GV
2599static int
2600w32_get_key_modifiers (unsigned int wparam, unsigned int lparam)
da36a4d6 2601{
ccc2d29c
GV
2602 int mods;
2603
2604 /* Convert to emacs modifiers. */
2605 mods = w32_kbd_mods_to_emacs (construct_console_modifiers (), wparam);
2606
2607 return mods;
2608}
da36a4d6 2609
ccc2d29c
GV
2610unsigned int
2611map_keypad_keys (unsigned int virt_key, unsigned int extended)
2612{
2613 if (virt_key < VK_CLEAR || virt_key > VK_DELETE)
2614 return virt_key;
da36a4d6 2615
ccc2d29c 2616 if (virt_key == VK_RETURN)
da36a4d6
GV
2617 return (extended ? VK_NUMPAD_ENTER : VK_RETURN);
2618
ccc2d29c
GV
2619 if (virt_key >= VK_PRIOR && virt_key <= VK_DOWN)
2620 return (!extended ? (VK_NUMPAD_PRIOR + (virt_key - VK_PRIOR)) : virt_key);
2621
2622 if (virt_key == VK_INSERT || virt_key == VK_DELETE)
2623 return (!extended ? (VK_NUMPAD_INSERT + (virt_key - VK_INSERT)) : virt_key);
2624
2625 if (virt_key == VK_CLEAR)
2626 return (!extended ? VK_NUMPAD_CLEAR : virt_key);
2627
2628 return virt_key;
2629}
2630
2631/* List of special key combinations which w32 would normally capture,
2632 but emacs should grab instead. Not directly visible to lisp, to
2633 simplify synchronization. Each item is an integer encoding a virtual
2634 key code and modifier combination to capture. */
2635Lisp_Object w32_grabbed_keys;
2636
2637#define HOTKEY(vk,mods) make_number (((vk) & 255) | ((mods) << 8))
2638#define HOTKEY_ID(k) (XFASTINT (k) & 0xbfff)
2639#define HOTKEY_VK_CODE(k) (XFASTINT (k) & 255)
2640#define HOTKEY_MODIFIERS(k) (XFASTINT (k) >> 8)
2641
2642/* Register hot-keys for reserved key combinations when Emacs has
2643 keyboard focus, since this is the only way Emacs can receive key
2644 combinations like Alt-Tab which are used by the system. */
2645
2646static void
2647register_hot_keys (hwnd)
2648 HWND hwnd;
2649{
2650 Lisp_Object keylist;
2651
2652 /* Use GC_CONSP, since we are called asynchronously. */
2653 for (keylist = w32_grabbed_keys; GC_CONSP (keylist); keylist = XCDR (keylist))
2654 {
2655 Lisp_Object key = XCAR (keylist);
2656
2657 /* Deleted entries get set to nil. */
2658 if (!INTEGERP (key))
2659 continue;
2660
2661 RegisterHotKey (hwnd, HOTKEY_ID (key),
2662 HOTKEY_MODIFIERS (key), HOTKEY_VK_CODE (key));
2663 }
2664}
2665
2666static void
2667unregister_hot_keys (hwnd)
2668 HWND hwnd;
2669{
2670 Lisp_Object keylist;
2671
2672 /* Use GC_CONSP, since we are called asynchronously. */
2673 for (keylist = w32_grabbed_keys; GC_CONSP (keylist); keylist = XCDR (keylist))
2674 {
2675 Lisp_Object key = XCAR (keylist);
2676
2677 if (!INTEGERP (key))
2678 continue;
2679
2680 UnregisterHotKey (hwnd, HOTKEY_ID (key));
2681 }
2682}
2683
5ac45f98
GV
2684/* Main message dispatch loop. */
2685
1edf84e7
GV
2686static void
2687w32_msg_pump (deferred_msg * msg_buf)
5ac45f98
GV
2688{
2689 MSG msg;
ccc2d29c
GV
2690 int result;
2691 HWND focus_window;
93fbe8b7
GV
2692
2693 msh_mousewheel = RegisterWindowMessage (MSH_MOUSEWHEEL);
7d0393cf 2694
5ac45f98
GV
2695 while (GetMessage (&msg, NULL, 0, 0))
2696 {
2697 if (msg.hwnd == NULL)
2698 {
2699 switch (msg.message)
2700 {
3ef68e6b
AI
2701 case WM_NULL:
2702 /* Produced by complete_deferred_msg; just ignore. */
2703 break;
5ac45f98 2704 case WM_EMACS_CREATEWINDOW:
fbd6baed 2705 w32_createwindow ((struct frame *) msg.wParam);
1edf84e7
GV
2706 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2707 abort ();
5ac45f98 2708 break;
dfdb4047
GV
2709 case WM_EMACS_SETLOCALE:
2710 SetThreadLocale (msg.wParam);
2711 /* Reply is not expected. */
2712 break;
ccc2d29c
GV
2713 case WM_EMACS_SETKEYBOARDLAYOUT:
2714 result = (int) ActivateKeyboardLayout ((HKL) msg.wParam, 0);
2715 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
2716 result, 0))
2717 abort ();
2718 break;
2719 case WM_EMACS_REGISTER_HOT_KEY:
2720 focus_window = GetFocus ();
2721 if (focus_window != NULL)
2722 RegisterHotKey (focus_window,
2723 HOTKEY_ID (msg.wParam),
2724 HOTKEY_MODIFIERS (msg.wParam),
2725 HOTKEY_VK_CODE (msg.wParam));
2726 /* Reply is not expected. */
2727 break;
2728 case WM_EMACS_UNREGISTER_HOT_KEY:
2729 focus_window = GetFocus ();
2730 if (focus_window != NULL)
2731 UnregisterHotKey (focus_window, HOTKEY_ID (msg.wParam));
adcc3809
GV
2732 /* Mark item as erased. NB: this code must be
2733 thread-safe. The next line is okay because the cons
2734 cell is never made into garbage and is not relocated by
2735 GC. */
f3fbd155 2736 XSETCAR ((Lisp_Object) msg.lParam, Qnil);
ccc2d29c
GV
2737 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2738 abort ();
2739 break;
adcc3809
GV
2740 case WM_EMACS_TOGGLE_LOCK_KEY:
2741 {
2742 int vk_code = (int) msg.wParam;
2743 int cur_state = (GetKeyState (vk_code) & 1);
2744 Lisp_Object new_state = (Lisp_Object) msg.lParam;
2745
2746 /* NB: This code must be thread-safe. It is safe to
2747 call NILP because symbols are not relocated by GC,
2748 and pointer here is not touched by GC (so the markbit
2749 can't be set). Numbers are safe because they are
2750 immediate values. */
2751 if (NILP (new_state)
2752 || (NUMBERP (new_state)
8edb0a6f 2753 && ((XUINT (new_state)) & 1) != cur_state))
adcc3809
GV
2754 {
2755 one_w32_display_info.faked_key = vk_code;
2756
2757 keybd_event ((BYTE) vk_code,
2758 (BYTE) MapVirtualKey (vk_code, 0),
2759 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2760 keybd_event ((BYTE) vk_code,
2761 (BYTE) MapVirtualKey (vk_code, 0),
2762 KEYEVENTF_EXTENDEDKEY | 0, 0);
2763 keybd_event ((BYTE) vk_code,
2764 (BYTE) MapVirtualKey (vk_code, 0),
2765 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
2766 cur_state = !cur_state;
2767 }
2768 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
2769 cur_state, 0))
2770 abort ();
2771 }
2772 break;
1edf84e7 2773 default:
1edf84e7 2774 DebPrint (("msg %x not expected by w32_msg_pump\n", msg.message));
5ac45f98
GV
2775 }
2776 }
2777 else
2778 {
2779 DispatchMessage (&msg);
2780 }
1edf84e7
GV
2781
2782 /* Exit nested loop when our deferred message has completed. */
2783 if (msg_buf->completed)
2784 break;
5ac45f98 2785 }
1edf84e7
GV
2786}
2787
2788deferred_msg * deferred_msg_head;
2789
2790static deferred_msg *
2791find_deferred_msg (HWND hwnd, UINT msg)
2792{
2793 deferred_msg * item;
2794
2795 /* Don't actually need synchronization for read access, since
2796 modification of single pointer is always atomic. */
2797 /* enter_crit (); */
2798
2799 for (item = deferred_msg_head; item != NULL; item = item->next)
2800 if (item->w32msg.msg.hwnd == hwnd
2801 && item->w32msg.msg.message == msg)
2802 break;
2803
2804 /* leave_crit (); */
2805
2806 return item;
2807}
2808
2809static LRESULT
2810send_deferred_msg (deferred_msg * msg_buf,
2811 HWND hwnd,
2812 UINT msg,
2813 WPARAM wParam,
2814 LPARAM lParam)
2815{
2816 /* Only input thread can send deferred messages. */
2817 if (GetCurrentThreadId () != dwWindowsThreadId)
2818 abort ();
2819
2820 /* It is an error to send a message that is already deferred. */
2821 if (find_deferred_msg (hwnd, msg) != NULL)
2822 abort ();
2823
2824 /* Enforced synchronization is not needed because this is the only
2825 function that alters deferred_msg_head, and the following critical
2826 section is guaranteed to only be serially reentered (since only the
2827 input thread can call us). */
2828
2829 /* enter_crit (); */
2830
2831 msg_buf->completed = 0;
2832 msg_buf->next = deferred_msg_head;
2833 deferred_msg_head = msg_buf;
2834 my_post_msg (&msg_buf->w32msg, hwnd, msg, wParam, lParam);
2835
2836 /* leave_crit (); */
2837
2838 /* Start a new nested message loop to process other messages until
2839 this one is completed. */
2840 w32_msg_pump (msg_buf);
2841
2842 deferred_msg_head = msg_buf->next;
2843
2844 return msg_buf->result;
2845}
2846
2847void
2848complete_deferred_msg (HWND hwnd, UINT msg, LRESULT result)
2849{
2850 deferred_msg * msg_buf = find_deferred_msg (hwnd, msg);
2851
2852 if (msg_buf == NULL)
3ef68e6b
AI
2853 /* Message may have been cancelled, so don't abort(). */
2854 return;
1edf84e7
GV
2855
2856 msg_buf->result = result;
2857 msg_buf->completed = 1;
2858
2859 /* Ensure input thread is woken so it notices the completion. */
2860 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
2861}
2862
3ef68e6b
AI
2863void
2864cancel_all_deferred_msgs ()
2865{
2866 deferred_msg * item;
2867
2868 /* Don't actually need synchronization for read access, since
2869 modification of single pointer is always atomic. */
2870 /* enter_crit (); */
2871
2872 for (item = deferred_msg_head; item != NULL; item = item->next)
2873 {
2874 item->result = 0;
2875 item->completed = 1;
2876 }
2877
2878 /* leave_crit (); */
2879
2880 /* Ensure input thread is woken so it notices the completion. */
2881 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
2882}
1edf84e7 2883
7d0393cf 2884DWORD
1edf84e7
GV
2885w32_msg_worker (dw)
2886 DWORD dw;
2887{
2888 MSG msg;
2889 deferred_msg dummy_buf;
2890
2891 /* Ensure our message queue is created */
7d0393cf 2892
1edf84e7 2893 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
7d0393cf 2894
1edf84e7
GV
2895 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
2896 abort ();
2897
2898 memset (&dummy_buf, 0, sizeof (dummy_buf));
2899 dummy_buf.w32msg.msg.hwnd = NULL;
2900 dummy_buf.w32msg.msg.message = WM_NULL;
2901
2902 /* This is the inital message loop which should only exit when the
2903 application quits. */
2904 w32_msg_pump (&dummy_buf);
2905
2906 return 0;
5ac45f98
GV
2907}
2908
3ef68e6b
AI
2909static void
2910post_character_message (hwnd, msg, wParam, lParam, modifiers)
2911 HWND hwnd;
2912 UINT msg;
2913 WPARAM wParam;
2914 LPARAM lParam;
2915 DWORD modifiers;
2916
2917{
2918 W32Msg wmsg;
2919
2920 wmsg.dwModifiers = modifiers;
2921
2922 /* Detect quit_char and set quit-flag directly. Note that we
2923 still need to post a message to ensure the main thread will be
2924 woken up if blocked in sys_select(), but we do NOT want to post
2925 the quit_char message itself (because it will usually be as if
2926 the user had typed quit_char twice). Instead, we post a dummy
2927 message that has no particular effect. */
2928 {
2929 int c = wParam;
2930 if (isalpha (c) && wmsg.dwModifiers == ctrl_modifier)
2931 c = make_ctrl_char (c) & 0377;
7d081355
AI
2932 if (c == quit_char
2933 || (wmsg.dwModifiers == 0 &&
2934 XFASTINT (Vw32_quit_key) && wParam == XFASTINT (Vw32_quit_key)))
3ef68e6b
AI
2935 {
2936 Vquit_flag = Qt;
2937
2938 /* The choice of message is somewhat arbitrary, as long as
2939 the main thread handler just ignores it. */
2940 msg = WM_NULL;
2941
2942 /* Interrupt any blocking system calls. */
2943 signal_quit ();
2944
2945 /* As a safety precaution, forcibly complete any deferred
2946 messages. This is a kludge, but I don't see any particularly
2947 clean way to handle the situation where a deferred message is
2948 "dropped" in the lisp thread, and will thus never be
2949 completed, eg. by the user trying to activate the menubar
2950 when the lisp thread is busy, and then typing C-g when the
2951 menubar doesn't open promptly (with the result that the
2952 menubar never responds at all because the deferred
2953 WM_INITMENU message is never completed). Another problem
2954 situation is when the lisp thread calls SendMessage (to send
2955 a window manager command) when a message has been deferred;
2956 the lisp thread gets blocked indefinitely waiting for the
2957 deferred message to be completed, which itself is waiting for
2958 the lisp thread to respond.
2959
2960 Note that we don't want to block the input thread waiting for
2961 a reponse from the lisp thread (although that would at least
2962 solve the deadlock problem above), because we want to be able
2963 to receive C-g to interrupt the lisp thread. */
2964 cancel_all_deferred_msgs ();
2965 }
2966 }
2967
2968 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
2969}
2970
ee78dc32
GV
2971/* Main window procedure */
2972
7d0393cf 2973LRESULT CALLBACK
fbd6baed 2974w32_wnd_proc (hwnd, msg, wParam, lParam)
ee78dc32
GV
2975 HWND hwnd;
2976 UINT msg;
2977 WPARAM wParam;
2978 LPARAM lParam;
2979{
2980 struct frame *f;
fbd6baed
GV
2981 struct w32_display_info *dpyinfo = &one_w32_display_info;
2982 W32Msg wmsg;
84fb1139 2983 int windows_translate;
576ba81c 2984 int key;
84fb1139 2985
a6085637
KH
2986 /* Note that it is okay to call x_window_to_frame, even though we are
2987 not running in the main lisp thread, because frame deletion
2988 requires the lisp thread to synchronize with this thread. Thus, if
2989 a frame struct is returned, it can be used without concern that the
2990 lisp thread might make it disappear while we are using it.
2991
2992 NB. Walking the frame list in this thread is safe (as long as
2993 writes of Lisp_Object slots are atomic, which they are on Windows).
2994 Although delete-frame can destructively modify the frame list while
2995 we are walking it, a garbage collection cannot occur until after
2996 delete-frame has synchronized with this thread.
2997
2998 It is also safe to use functions that make GDI calls, such as
fbd6baed 2999 w32_clear_rect, because these functions must obtain a DC handle
a6085637
KH
3000 from the frame struct using get_frame_dc which is thread-aware. */
3001
7d0393cf 3002 switch (msg)
ee78dc32
GV
3003 {
3004 case WM_ERASEBKGND:
a6085637
KH
3005 f = x_window_to_frame (dpyinfo, hwnd);
3006 if (f)
3007 {
9badad41 3008 HDC hdc = get_frame_dc (f);
a6085637 3009 GetUpdateRect (hwnd, &wmsg.rect, FALSE);
9badad41
JR
3010 w32_clear_rect (f, hdc, &wmsg.rect);
3011 release_frame_dc (f, hdc);
ce6059da
AI
3012
3013#if defined (W32_DEBUG_DISPLAY)
18f0b342
AI
3014 DebPrint (("WM_ERASEBKGND (frame %p): erasing %d,%d-%d,%d\n",
3015 f,
3016 wmsg.rect.left, wmsg.rect.top,
3017 wmsg.rect.right, wmsg.rect.bottom));
ce6059da 3018#endif /* W32_DEBUG_DISPLAY */
a6085637 3019 }
5ac45f98
GV
3020 return 1;
3021 case WM_PALETTECHANGED:
3022 /* ignore our own changes */
3023 if ((HWND)wParam != hwnd)
3024 {
a6085637
KH
3025 f = x_window_to_frame (dpyinfo, hwnd);
3026 if (f)
3027 /* get_frame_dc will realize our palette and force all
3028 frames to be redrawn if needed. */
3029 release_frame_dc (f, get_frame_dc (f));
5ac45f98
GV
3030 }
3031 return 0;
ee78dc32 3032 case WM_PAINT:
ce6059da 3033 {
55dcfc15
AI
3034 PAINTSTRUCT paintStruct;
3035 RECT update_rect;
aa35b6ad 3036 bzero (&update_rect, sizeof (update_rect));
55dcfc15 3037
18f0b342
AI
3038 f = x_window_to_frame (dpyinfo, hwnd);
3039 if (f == 0)
3040 {
3041 DebPrint (("WM_PAINT received for unknown window %p\n", hwnd));
3042 return 0;
3043 }
3044
55dcfc15
AI
3045 /* MSDN Docs say not to call BeginPaint if GetUpdateRect
3046 fails. Apparently this can happen under some
3047 circumstances. */
aa35b6ad 3048 if (GetUpdateRect (hwnd, &update_rect, FALSE) || !w32_strict_painting)
55dcfc15
AI
3049 {
3050 enter_crit ();
3051 BeginPaint (hwnd, &paintStruct);
3052
aa35b6ad
JR
3053 /* The rectangles returned by GetUpdateRect and BeginPaint
3054 do not always match. Play it safe by assuming both areas
3055 are invalid. */
3056 UnionRect (&(wmsg.rect), &update_rect, &(paintStruct.rcPaint));
55dcfc15
AI
3057
3058#if defined (W32_DEBUG_DISPLAY)
18f0b342
AI
3059 DebPrint (("WM_PAINT (frame %p): painting %d,%d-%d,%d\n",
3060 f,
3061 wmsg.rect.left, wmsg.rect.top,
3062 wmsg.rect.right, wmsg.rect.bottom));
3063 DebPrint ((" [update region is %d,%d-%d,%d]\n",
55dcfc15
AI
3064 update_rect.left, update_rect.top,
3065 update_rect.right, update_rect.bottom));
3066#endif
3067 EndPaint (hwnd, &paintStruct);
3068 leave_crit ();
3069
3070 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
7d0393cf 3071
55dcfc15
AI
3072 return 0;
3073 }
c0611964
AI
3074
3075 /* If GetUpdateRect returns 0 (meaning there is no update
3076 region), assume the whole window needs to be repainted. */
3077 GetClientRect(hwnd, &wmsg.rect);
3078 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3079 return 0;
ee78dc32 3080 }
a1a80b40 3081
ccc2d29c
GV
3082 case WM_INPUTLANGCHANGE:
3083 /* Inform lisp thread of keyboard layout changes. */
3084 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3085
3086 /* Clear dead keys in the keyboard state; for simplicity only
3087 preserve modifier key states. */
3088 {
3089 int i;
3090 BYTE keystate[256];
3091
3092 GetKeyboardState (keystate);
3093 for (i = 0; i < 256; i++)
3094 if (1
3095 && i != VK_SHIFT
3096 && i != VK_LSHIFT
3097 && i != VK_RSHIFT
3098 && i != VK_CAPITAL
3099 && i != VK_NUMLOCK
3100 && i != VK_SCROLL
3101 && i != VK_CONTROL
3102 && i != VK_LCONTROL
3103 && i != VK_RCONTROL
3104 && i != VK_MENU
3105 && i != VK_LMENU
3106 && i != VK_RMENU
3107 && i != VK_LWIN
3108 && i != VK_RWIN)
3109 keystate[i] = 0;
3110 SetKeyboardState (keystate);
3111 }
3112 goto dflt;
3113
3114 case WM_HOTKEY:
3115 /* Synchronize hot keys with normal input. */
3116 PostMessage (hwnd, WM_KEYDOWN, HIWORD (lParam), 0);
3117 return (0);
3118
a1a80b40
GV
3119 case WM_KEYUP:
3120 case WM_SYSKEYUP:
3121 record_keyup (wParam, lParam);
3122 goto dflt;
3123
ee78dc32
GV
3124 case WM_KEYDOWN:
3125 case WM_SYSKEYDOWN:
ccc2d29c
GV
3126 /* Ignore keystrokes we fake ourself; see below. */
3127 if (dpyinfo->faked_key == wParam)
3128 {
3129 dpyinfo->faked_key = 0;
576ba81c
AI
3130 /* Make sure TranslateMessage sees them though (as long as
3131 they don't produce WM_CHAR messages). This ensures that
3132 indicator lights are toggled promptly on Windows 9x, for
3133 example. */
3134 if (lispy_function_keys[wParam] != 0)
3135 {
3136 windows_translate = 1;
3137 goto translate;
3138 }
3139 return 0;
ccc2d29c
GV
3140 }
3141
7830e24b
RS
3142 /* Synchronize modifiers with current keystroke. */
3143 sync_modifiers ();
a1a80b40 3144 record_keydown (wParam, lParam);
ccc2d29c 3145 wParam = map_keypad_keys (wParam, (lParam & 0x1000000L) != 0);
84fb1139
KH
3146
3147 windows_translate = 0;
ccc2d29c
GV
3148
3149 switch (wParam)
3150 {
3151 case VK_LWIN:
3152 if (NILP (Vw32_pass_lwindow_to_system))
3153 {
3154 /* Prevent system from acting on keyup (which opens the
3155 Start menu if no other key was pressed) by simulating a
3156 press of Space which we will ignore. */
3157 if (GetAsyncKeyState (wParam) & 1)
3158 {
adcc3809 3159 if (NUMBERP (Vw32_phantom_key_code))
576ba81c 3160 key = XUINT (Vw32_phantom_key_code) & 255;
adcc3809 3161 else
576ba81c
AI
3162 key = VK_SPACE;
3163 dpyinfo->faked_key = key;
3164 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
ccc2d29c
GV
3165 }
3166 }
3167 if (!NILP (Vw32_lwindow_modifier))
3168 return 0;
3169 break;
3170 case VK_RWIN:
3171 if (NILP (Vw32_pass_rwindow_to_system))
3172 {
3173 if (GetAsyncKeyState (wParam) & 1)
3174 {
adcc3809 3175 if (NUMBERP (Vw32_phantom_key_code))
576ba81c 3176 key = XUINT (Vw32_phantom_key_code) & 255;
adcc3809 3177 else
576ba81c
AI
3178 key = VK_SPACE;
3179 dpyinfo->faked_key = key;
3180 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
ccc2d29c
GV
3181 }
3182 }
3183 if (!NILP (Vw32_rwindow_modifier))
3184 return 0;
3185 break;
576ba81c 3186 case VK_APPS:
ccc2d29c
GV
3187 if (!NILP (Vw32_apps_modifier))
3188 return 0;
3189 break;
3190 case VK_MENU:
7d0393cf 3191 if (NILP (Vw32_pass_alt_to_system))
adcc3809
GV
3192 /* Prevent DefWindowProc from activating the menu bar if an
3193 Alt key is pressed and released by itself. */
ccc2d29c 3194 return 0;
84fb1139 3195 windows_translate = 1;
ccc2d29c 3196 break;
7d0393cf 3197 case VK_CAPITAL:
ccc2d29c
GV
3198 /* Decide whether to treat as modifier or function key. */
3199 if (NILP (Vw32_enable_caps_lock))
3200 goto disable_lock_key;
adcc3809
GV
3201 windows_translate = 1;
3202 break;
ccc2d29c
GV
3203 case VK_NUMLOCK:
3204 /* Decide whether to treat as modifier or function key. */
3205 if (NILP (Vw32_enable_num_lock))
3206 goto disable_lock_key;
adcc3809
GV
3207 windows_translate = 1;
3208 break;
ccc2d29c
GV
3209 case VK_SCROLL:
3210 /* Decide whether to treat as modifier or function key. */
3211 if (NILP (Vw32_scroll_lock_modifier))
3212 goto disable_lock_key;
adcc3809
GV
3213 windows_translate = 1;
3214 break;
ccc2d29c 3215 disable_lock_key:
adcc3809
GV
3216 /* Ensure the appropriate lock key state (and indicator light)
3217 remains in the same state. We do this by faking another
3218 press of the relevant key. Apparently, this really is the
3219 only way to toggle the state of the indicator lights. */
3220 dpyinfo->faked_key = wParam;
3221 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
3222 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
3223 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
3224 KEYEVENTF_EXTENDEDKEY | 0, 0);
3225 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
3226 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
3227 /* Ensure indicator lights are updated promptly on Windows 9x
3228 (TranslateMessage apparently does this), after forwarding
3229 input event. */
3230 post_character_message (hwnd, msg, wParam, lParam,
3231 w32_get_key_modifiers (wParam, lParam));
3232 windows_translate = 1;
ccc2d29c 3233 break;
7d0393cf 3234 case VK_CONTROL:
ccc2d29c
GV
3235 case VK_SHIFT:
3236 case VK_PROCESSKEY: /* Generated by IME. */
3237 windows_translate = 1;
3238 break;
adcc3809
GV
3239 case VK_CANCEL:
3240 /* Windows maps Ctrl-Pause (aka Ctrl-Break) into VK_CANCEL,
3241 which is confusing for purposes of key binding; convert
3242 VK_CANCEL events into VK_PAUSE events. */
3243 wParam = VK_PAUSE;
3244 break;
3245 case VK_PAUSE:
3246 /* Windows maps Ctrl-NumLock into VK_PAUSE, which is confusing
3247 for purposes of key binding; convert these back into
3248 VK_NUMLOCK events, at least when we want to see NumLock key
3249 presses. (Note that there is never any possibility that
3250 VK_PAUSE with Ctrl really is C-Pause as per above.) */
3251 if (NILP (Vw32_enable_num_lock) && modifier_set (VK_CONTROL))
3252 wParam = VK_NUMLOCK;
3253 break;
ccc2d29c
GV
3254 default:
3255 /* If not defined as a function key, change it to a WM_CHAR message. */
3256 if (lispy_function_keys[wParam] == 0)
3257 {
adcc3809
GV
3258 DWORD modifiers = construct_console_modifiers ();
3259
ccc2d29c
GV
3260 if (!NILP (Vw32_recognize_altgr)
3261 && modifier_set (VK_LCONTROL) && modifier_set (VK_RMENU))
3262 {
3263 /* Always let TranslateMessage handle AltGr key chords;
3264 for some reason, ToAscii doesn't always process AltGr
3265 chords correctly. */
3266 windows_translate = 1;
3267 }
adcc3809 3268 else if ((modifiers & (~SHIFT_PRESSED & ~CAPSLOCK_ON)) != 0)
ccc2d29c 3269 {
adcc3809
GV
3270 /* Handle key chords including any modifiers other
3271 than shift directly, in order to preserve as much
3272 modifier information as possible. */
ccc2d29c
GV
3273 if ('A' <= wParam && wParam <= 'Z')
3274 {
3275 /* Don't translate modified alphabetic keystrokes,
3276 so the user doesn't need to constantly switch
3277 layout to type control or meta keystrokes when
3278 the normal layout translates alphabetic
3279 characters to non-ascii characters. */
3280 if (!modifier_set (VK_SHIFT))
3281 wParam += ('a' - 'A');
3282 msg = WM_CHAR;
3283 }
3284 else
3285 {
3286 /* Try to handle other keystrokes by determining the
3287 base character (ie. translating the base key plus
3288 shift modifier). */
3289 int add;
3290 int isdead = 0;
3291 KEY_EVENT_RECORD key;
7d0393cf 3292
ccc2d29c
GV
3293 key.bKeyDown = TRUE;
3294 key.wRepeatCount = 1;
3295 key.wVirtualKeyCode = wParam;
3296 key.wVirtualScanCode = (lParam & 0xFF0000) >> 16;
3297 key.uChar.AsciiChar = 0;
adcc3809 3298 key.dwControlKeyState = modifiers;
ccc2d29c
GV
3299
3300 add = w32_kbd_patch_key (&key);
3301 /* 0 means an unrecognised keycode, negative means
3302 dead key. Ignore both. */
3303 while (--add >= 0)
3304 {
3305 /* Forward asciified character sequence. */
3306 post_character_message
3307 (hwnd, WM_CHAR, key.uChar.AsciiChar, lParam,
3308 w32_get_key_modifiers (wParam, lParam));
3309 w32_kbd_patch_key (&key);
3310 }
3311 return 0;
3312 }
3313 }
3314 else
3315 {
3316 /* Let TranslateMessage handle everything else. */
3317 windows_translate = 1;
3318 }
3319 }
3320 }
a1a80b40 3321
adcc3809 3322 translate:
84fb1139
KH
3323 if (windows_translate)
3324 {
e9e23e23 3325 MSG windows_msg = { hwnd, msg, wParam, lParam, 0, {0,0} };
84fb1139 3326
e9e23e23
GV
3327 windows_msg.time = GetMessageTime ();
3328 TranslateMessage (&windows_msg);
84fb1139
KH
3329 goto dflt;
3330 }
3331
ee78dc32 3332 /* Fall through */
7d0393cf 3333
ee78dc32
GV
3334 case WM_SYSCHAR:
3335 case WM_CHAR:
ccc2d29c
GV
3336 post_character_message (hwnd, msg, wParam, lParam,
3337 w32_get_key_modifiers (wParam, lParam));
ee78dc32 3338 break;
da36a4d6 3339
5ac45f98
GV
3340 /* Simulate middle mouse button events when left and right buttons
3341 are used together, but only if user has two button mouse. */
ee78dc32 3342 case WM_LBUTTONDOWN:
5ac45f98 3343 case WM_RBUTTONDOWN:
7ce9aaca 3344 if (XINT (Vw32_num_mouse_buttons) > 2)
5ac45f98
GV
3345 goto handle_plain_button;
3346
3347 {
3348 int this = (msg == WM_LBUTTONDOWN) ? LMOUSE : RMOUSE;
3349 int other = (msg == WM_LBUTTONDOWN) ? RMOUSE : LMOUSE;
3350
3cb20f4a
RS
3351 if (button_state & this)
3352 return 0;
5ac45f98
GV
3353
3354 if (button_state == 0)
3355 SetCapture (hwnd);
3356
3357 button_state |= this;
3358
3359 if (button_state & other)
3360 {
84fb1139 3361 if (mouse_button_timer)
5ac45f98 3362 {
84fb1139
KH
3363 KillTimer (hwnd, mouse_button_timer);
3364 mouse_button_timer = 0;
5ac45f98
GV
3365
3366 /* Generate middle mouse event instead. */
3367 msg = WM_MBUTTONDOWN;
3368 button_state |= MMOUSE;
3369 }
3370 else if (button_state & MMOUSE)
3371 {
3372 /* Ignore button event if we've already generated a
3373 middle mouse down event. This happens if the
3374 user releases and press one of the two buttons
3375 after we've faked a middle mouse event. */
3376 return 0;
3377 }
3378 else
3379 {
3380 /* Flush out saved message. */
84fb1139 3381 post_msg (&saved_mouse_button_msg);
5ac45f98 3382 }
fbd6baed 3383 wmsg.dwModifiers = w32_get_modifiers ();
5ac45f98
GV
3384 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3385
3386 /* Clear message buffer. */
84fb1139 3387 saved_mouse_button_msg.msg.hwnd = 0;
5ac45f98
GV
3388 }
3389 else
3390 {
3391 /* Hold onto message for now. */
84fb1139 3392 mouse_button_timer =
adcc3809
GV
3393 SetTimer (hwnd, MOUSE_BUTTON_ID,
3394 XINT (Vw32_mouse_button_tolerance), NULL);
84fb1139
KH
3395 saved_mouse_button_msg.msg.hwnd = hwnd;
3396 saved_mouse_button_msg.msg.message = msg;
3397 saved_mouse_button_msg.msg.wParam = wParam;
3398 saved_mouse_button_msg.msg.lParam = lParam;
3399 saved_mouse_button_msg.msg.time = GetMessageTime ();
fbd6baed 3400 saved_mouse_button_msg.dwModifiers = w32_get_modifiers ();
5ac45f98
GV
3401 }
3402 }
3403 return 0;
3404
ee78dc32 3405 case WM_LBUTTONUP:
5ac45f98 3406 case WM_RBUTTONUP:
7ce9aaca 3407 if (XINT (Vw32_num_mouse_buttons) > 2)
5ac45f98
GV
3408 goto handle_plain_button;
3409
3410 {
3411 int this = (msg == WM_LBUTTONUP) ? LMOUSE : RMOUSE;
3412 int other = (msg == WM_LBUTTONUP) ? RMOUSE : LMOUSE;
3413
3cb20f4a
RS
3414 if ((button_state & this) == 0)
3415 return 0;
5ac45f98
GV
3416
3417 button_state &= ~this;
3418
3419 if (button_state & MMOUSE)
3420 {
3421 /* Only generate event when second button is released. */
3422 if ((button_state & other) == 0)
3423 {
3424 msg = WM_MBUTTONUP;
3425 button_state &= ~MMOUSE;
3426
3427 if (button_state) abort ();
3428 }
3429 else
3430 return 0;
3431 }
3432 else
3433 {
3434 /* Flush out saved message if necessary. */
84fb1139 3435 if (saved_mouse_button_msg.msg.hwnd)
5ac45f98 3436 {
84fb1139 3437 post_msg (&saved_mouse_button_msg);
5ac45f98
GV
3438 }
3439 }
fbd6baed 3440 wmsg.dwModifiers = w32_get_modifiers ();
5ac45f98
GV
3441 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3442
3443 /* Always clear message buffer and cancel timer. */
84fb1139
KH
3444 saved_mouse_button_msg.msg.hwnd = 0;
3445 KillTimer (hwnd, mouse_button_timer);
3446 mouse_button_timer = 0;
5ac45f98
GV
3447
3448 if (button_state == 0)
3449 ReleaseCapture ();
3450 }
3451 return 0;
3452
74214547
JR
3453 case WM_XBUTTONDOWN:
3454 case WM_XBUTTONUP:
3455 if (w32_pass_extra_mouse_buttons_to_system)
3456 goto dflt;
3457 /* else fall through and process them. */
ee78dc32
GV
3458 case WM_MBUTTONDOWN:
3459 case WM_MBUTTONUP:
5ac45f98 3460 handle_plain_button:
ee78dc32
GV
3461 {
3462 BOOL up;
1edf84e7 3463 int button;
ee78dc32 3464
74214547 3465 if (parse_button (msg, HIWORD (wParam), &button, &up))
ee78dc32
GV
3466 {
3467 if (up) ReleaseCapture ();
3468 else SetCapture (hwnd);
7d0393cf 3469 button = (button == 0) ? LMOUSE :
1edf84e7
GV
3470 ((button == 1) ? MMOUSE : RMOUSE);
3471 if (up)
3472 button_state &= ~button;
3473 else
3474 button_state |= button;
ee78dc32
GV
3475 }
3476 }
7d0393cf 3477
fbd6baed 3478 wmsg.dwModifiers = w32_get_modifiers ();
ee78dc32 3479 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
74214547
JR
3480
3481 /* Need to return true for XBUTTON messages, false for others,
3482 to indicate that we processed the message. */
3483 return (msg == WM_XBUTTONDOWN || msg == WM_XBUTTONUP);
5ac45f98 3484
5ac45f98 3485 case WM_MOUSEMOVE:
9eb16b62
JR
3486 /* If the mouse has just moved into the frame, start tracking
3487 it, so we will be notified when it leaves the frame. Mouse
3488 tracking only works under W98 and NT4 and later. On earlier
3489 versions, there is no way of telling when the mouse leaves the
3490 frame, so we just have to put up with help-echo and mouse
3491 highlighting remaining while the frame is not active. */
3492 if (track_mouse_event_fn && !track_mouse_window)
3493 {
3494 TRACKMOUSEEVENT tme;
3495 tme.cbSize = sizeof (tme);
3496 tme.dwFlags = TME_LEAVE;
3497 tme.hwndTrack = hwnd;
3498
3499 track_mouse_event_fn (&tme);
3500 track_mouse_window = hwnd;
3501 }
3502 case WM_VSCROLL:
fbd6baed 3503 if (XINT (Vw32_mouse_move_interval) <= 0
84fb1139
KH
3504 || (msg == WM_MOUSEMOVE && button_state == 0))
3505 {
fbd6baed 3506 wmsg.dwModifiers = w32_get_modifiers ();
84fb1139
KH
3507 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3508 return 0;
3509 }
7d0393cf 3510
84fb1139
KH
3511 /* Hang onto mouse move and scroll messages for a bit, to avoid
3512 sending such events to Emacs faster than it can process them.
3513 If we get more events before the timer from the first message
3514 expires, we just replace the first message. */
3515
3516 if (saved_mouse_move_msg.msg.hwnd == 0)
3517 mouse_move_timer =
adcc3809
GV
3518 SetTimer (hwnd, MOUSE_MOVE_ID,
3519 XINT (Vw32_mouse_move_interval), NULL);
84fb1139
KH
3520
3521 /* Hold onto message for now. */
3522 saved_mouse_move_msg.msg.hwnd = hwnd;
3523 saved_mouse_move_msg.msg.message = msg;
3524 saved_mouse_move_msg.msg.wParam = wParam;
3525 saved_mouse_move_msg.msg.lParam = lParam;
3526 saved_mouse_move_msg.msg.time = GetMessageTime ();
fbd6baed 3527 saved_mouse_move_msg.dwModifiers = w32_get_modifiers ();
7d0393cf 3528
84fb1139
KH
3529 return 0;
3530
1edf84e7
GV
3531 case WM_MOUSEWHEEL:
3532 wmsg.dwModifiers = w32_get_modifiers ();
3533 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3534 return 0;
3535
cb9e33d4
RS
3536 case WM_DROPFILES:
3537 wmsg.dwModifiers = w32_get_modifiers ();
3538 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3539 return 0;
3540
84fb1139
KH
3541 case WM_TIMER:
3542 /* Flush out saved messages if necessary. */
3543 if (wParam == mouse_button_timer)
5ac45f98 3544 {
84fb1139
KH
3545 if (saved_mouse_button_msg.msg.hwnd)
3546 {
3547 post_msg (&saved_mouse_button_msg);
3548 saved_mouse_button_msg.msg.hwnd = 0;
3549 }
3550 KillTimer (hwnd, mouse_button_timer);
3551 mouse_button_timer = 0;
3552 }
3553 else if (wParam == mouse_move_timer)
3554 {
3555 if (saved_mouse_move_msg.msg.hwnd)
3556 {
3557 post_msg (&saved_mouse_move_msg);
3558 saved_mouse_move_msg.msg.hwnd = 0;
3559 }
3560 KillTimer (hwnd, mouse_move_timer);
3561 mouse_move_timer = 0;
5ac45f98 3562 }
48094ace
JR
3563 else if (wParam == menu_free_timer)
3564 {
3565 KillTimer (hwnd, menu_free_timer);
3566 menu_free_timer = 0;
27605fa7 3567 f = x_window_to_frame (dpyinfo, hwnd);
48094ace
JR
3568 if (!f->output_data.w32->menu_command_in_progress)
3569 {
3570 /* Free memory used by owner-drawn and help-echo strings. */
3571 w32_free_menu_strings (hwnd);
3572 f->output_data.w32->menubar_active = 0;
3573 }
3574 }
5ac45f98 3575 return 0;
7d0393cf 3576
84fb1139
KH
3577 case WM_NCACTIVATE:
3578 /* Windows doesn't send us focus messages when putting up and
e9e23e23 3579 taking down a system popup dialog as for Ctrl-Alt-Del on Windows 95.
84fb1139
KH
3580 The only indication we get that something happened is receiving
3581 this message afterwards. So this is a good time to reset our
3582 keyboard modifiers' state. */
3583 reset_modifiers ();
3584 goto dflt;
da36a4d6 3585
1edf84e7 3586 case WM_INITMENU:
487163ac
AI
3587 button_state = 0;
3588 ReleaseCapture ();
1edf84e7
GV
3589 /* We must ensure menu bar is fully constructed and up to date
3590 before allowing user interaction with it. To achieve this
3591 we send this message to the lisp thread and wait for a
3592 reply (whose value is not actually needed) to indicate that
3593 the menu bar is now ready for use, so we can now return.
3594
3595 To remain responsive in the meantime, we enter a nested message
3596 loop that can process all other messages.
3597
3598 However, we skip all this if the message results from calling
3599 TrackPopupMenu - in fact, we must NOT attempt to send the lisp
3600 thread a message because it is blocked on us at this point. We
3601 set menubar_active before calling TrackPopupMenu to indicate
3602 this (there is no possibility of confusion with real menubar
3603 being active). */
3604
3605 f = x_window_to_frame (dpyinfo, hwnd);
3606 if (f
3607 && (f->output_data.w32->menubar_active
3608 /* We can receive this message even in the absence of a
3609 menubar (ie. when the system menu is activated) - in this
3610 case we do NOT want to forward the message, otherwise it
3611 will cause the menubar to suddenly appear when the user
3612 had requested it to be turned off! */
3613 || f->output_data.w32->menubar_widget == NULL))
3614 return 0;
3615
3616 {
3617 deferred_msg msg_buf;
3618
3619 /* Detect if message has already been deferred; in this case
3620 we cannot return any sensible value to ignore this. */
3621 if (find_deferred_msg (hwnd, msg) != NULL)
3622 abort ();
3623
3624 return send_deferred_msg (&msg_buf, hwnd, msg, wParam, lParam);
3625 }
3626
3627 case WM_EXITMENULOOP:
3628 f = x_window_to_frame (dpyinfo, hwnd);
3629
48094ace
JR
3630 /* If a menu command is not already in progress, check again
3631 after a short delay, since Windows often (always?) sends the
3632 WM_EXITMENULOOP before the corresponding WM_COMMAND message. */
3633 if (f && !f->output_data.w32->menu_command_in_progress)
3634 menu_free_timer = SetTimer (hwnd, MENU_FREE_ID, MENU_FREE_DELAY, NULL);
1edf84e7
GV
3635 goto dflt;
3636
126f2e35 3637 case WM_MENUSELECT:
4e3a1c61
JR
3638 /* Direct handling of help_echo in menus. Should be safe now
3639 that we generate the help_echo by placing a help event in the
3640 keyboard buffer. */
ca56d953 3641 {
ca56d953
JR
3642 HMENU menu = (HMENU) lParam;
3643 UINT menu_item = (UINT) LOWORD (wParam);
3644 UINT flags = (UINT) HIWORD (wParam);
3645
4e3a1c61 3646 w32_menu_display_help (hwnd, menu, menu_item, flags);
ca56d953 3647 }
126f2e35
JR
3648 return 0;
3649
87996783
GV
3650 case WM_MEASUREITEM:
3651 f = x_window_to_frame (dpyinfo, hwnd);
3652 if (f)
3653 {
3654 MEASUREITEMSTRUCT * pMis = (MEASUREITEMSTRUCT *) lParam;
3655
3656 if (pMis->CtlType == ODT_MENU)
3657 {
3658 /* Work out dimensions for popup menu titles. */
3659 char * title = (char *) pMis->itemData;
3660 HDC hdc = GetDC (hwnd);
3661 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
3662 LOGFONT menu_logfont;
3663 HFONT old_font;
3664 SIZE size;
3665
3666 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
3667 menu_logfont.lfWeight = FW_BOLD;
3668 menu_font = CreateFontIndirect (&menu_logfont);
3669 old_font = SelectObject (hdc, menu_font);
3670
dfff8a69
JR
3671 pMis->itemHeight = GetSystemMetrics (SM_CYMENUSIZE);
3672 if (title)
3673 {
3674 GetTextExtentPoint32 (hdc, title, strlen (title), &size);
3675 pMis->itemWidth = size.cx;
3676 if (pMis->itemHeight < size.cy)
3677 pMis->itemHeight = size.cy;
3678 }
3679 else
3680 pMis->itemWidth = 0;
87996783
GV
3681
3682 SelectObject (hdc, old_font);
3683 DeleteObject (menu_font);
3684 ReleaseDC (hwnd, hdc);
3685 return TRUE;
3686 }
3687 }
3688 return 0;
3689
3690 case WM_DRAWITEM:
3691 f = x_window_to_frame (dpyinfo, hwnd);
3692 if (f)
3693 {
3694 DRAWITEMSTRUCT * pDis = (DRAWITEMSTRUCT *) lParam;
3695
3696 if (pDis->CtlType == ODT_MENU)
3697 {
3698 /* Draw popup menu title. */
3699 char * title = (char *) pDis->itemData;
212da13b
JR
3700 if (title)
3701 {
3702 HDC hdc = pDis->hDC;
3703 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
3704 LOGFONT menu_logfont;
3705 HFONT old_font;
3706
3707 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
3708 menu_logfont.lfWeight = FW_BOLD;
3709 menu_font = CreateFontIndirect (&menu_logfont);
3710 old_font = SelectObject (hdc, menu_font);
3711
3712 /* Always draw title as if not selected. */
3713 ExtTextOut (hdc,
3714 pDis->rcItem.left
3715 + GetSystemMetrics (SM_CXMENUCHECK),
3716 pDis->rcItem.top,
3717 ETO_OPAQUE, &pDis->rcItem,
3718 title, strlen (title), NULL);
3719
3720 SelectObject (hdc, old_font);
3721 DeleteObject (menu_font);
3722 }
87996783
GV
3723 return TRUE;
3724 }
3725 }
3726 return 0;
3727
1edf84e7
GV
3728#if 0
3729 /* Still not right - can't distinguish between clicks in the
3730 client area of the frame from clicks forwarded from the scroll
3731 bars - may have to hook WM_NCHITTEST to remember the mouse
3732 position and then check if it is in the client area ourselves. */
3733 case WM_MOUSEACTIVATE:
3734 /* Discard the mouse click that activates a frame, allowing the
3735 user to click anywhere without changing point (or worse!).
3736 Don't eat mouse clicks on scrollbars though!! */
3737 if (LOWORD (lParam) == HTCLIENT )
3738 return MA_ACTIVATEANDEAT;
3739 goto dflt;
3740#endif
3741
9eb16b62
JR
3742 case WM_MOUSELEAVE:
3743 /* No longer tracking mouse. */
3744 track_mouse_window = NULL;
3745
1edf84e7 3746 case WM_ACTIVATEAPP:
ccc2d29c 3747 case WM_ACTIVATE:
1edf84e7
GV
3748 case WM_WINDOWPOSCHANGED:
3749 case WM_SHOWWINDOW:
3750 /* Inform lisp thread that a frame might have just been obscured
3751 or exposed, so should recheck visibility of all frames. */
3752 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3753 goto dflt;
3754
da36a4d6 3755 case WM_SETFOCUS:
adcc3809
GV
3756 dpyinfo->faked_key = 0;
3757 reset_modifiers ();
ccc2d29c
GV
3758 register_hot_keys (hwnd);
3759 goto command;
8681157a 3760 case WM_KILLFOCUS:
ccc2d29c 3761 unregister_hot_keys (hwnd);
487163ac
AI
3762 button_state = 0;
3763 ReleaseCapture ();
65906840
JR
3764 /* Relinquish the system caret. */
3765 if (w32_system_caret_hwnd)
3766 {
93f2ca61 3767 w32_visible_system_caret_hwnd = NULL;
d285988b
JR
3768 w32_system_caret_hwnd = NULL;
3769 DestroyCaret ();
65906840 3770 }
48094ace
JR
3771 goto command;
3772 case WM_COMMAND:
3773 f = x_window_to_frame (dpyinfo, hwnd);
3774 if (f && HIWORD (wParam) == 0)
3775 {
3776 f->output_data.w32->menu_command_in_progress = 1;
3777 if (menu_free_timer)
3778 {
3779 KillTimer (hwnd, menu_free_timer);
7d0393cf 3780 menu_free_timer = 0;
48094ace
JR
3781 }
3782 }
ee78dc32
GV
3783 case WM_MOVE:
3784 case WM_SIZE:
ccc2d29c 3785 command:
fbd6baed 3786 wmsg.dwModifiers = w32_get_modifiers ();
ee78dc32
GV
3787 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3788 goto dflt;
8847d890
RS
3789
3790 case WM_CLOSE:
fbd6baed 3791 wmsg.dwModifiers = w32_get_modifiers ();
8847d890
RS
3792 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
3793 return 0;
3794
ee78dc32 3795 case WM_WINDOWPOSCHANGING:
bfd6edcc
JR
3796 /* Don't restrict the sizing of tip frames. */
3797 if (hwnd == tip_window)
3798 return 0;
ee78dc32
GV
3799 {
3800 WINDOWPLACEMENT wp;
3801 LPWINDOWPOS lppos = (WINDOWPOS *) lParam;
1edf84e7
GV
3802
3803 wp.length = sizeof (WINDOWPLACEMENT);
ee78dc32 3804 GetWindowPlacement (hwnd, &wp);
7d0393cf 3805
1edf84e7 3806 if (wp.showCmd != SW_SHOWMINIMIZED && (lppos->flags & SWP_NOSIZE) == 0)
ee78dc32
GV
3807 {
3808 RECT rect;
3809 int wdiff;
3810 int hdiff;
1edf84e7
GV
3811 DWORD font_width;
3812 DWORD line_height;
3813 DWORD internal_border;
3814 DWORD scrollbar_extra;
ee78dc32 3815 RECT wr;
7d0393cf 3816
5ac45f98 3817 wp.length = sizeof(wp);
ee78dc32 3818 GetWindowRect (hwnd, &wr);
7d0393cf 3819
3c190163 3820 enter_crit ();
7d0393cf 3821
1edf84e7
GV
3822 font_width = GetWindowLong (hwnd, WND_FONTWIDTH_INDEX);
3823 line_height = GetWindowLong (hwnd, WND_LINEHEIGHT_INDEX);
3824 internal_border = GetWindowLong (hwnd, WND_BORDER_INDEX);
3825 scrollbar_extra = GetWindowLong (hwnd, WND_SCROLLBAR_INDEX);
7d0393cf 3826
3c190163 3827 leave_crit ();
7d0393cf 3828
ee78dc32 3829 memset (&rect, 0, sizeof (rect));
7d0393cf 3830 AdjustWindowRect (&rect, GetWindowLong (hwnd, GWL_STYLE),
ee78dc32
GV
3831 GetMenu (hwnd) != NULL);
3832
1edf84e7
GV
3833 /* Force width and height of client area to be exact
3834 multiples of the character cell dimensions. */
3835 wdiff = (lppos->cx - (rect.right - rect.left)
3836 - 2 * internal_border - scrollbar_extra)
3837 % font_width;
3838 hdiff = (lppos->cy - (rect.bottom - rect.top)
3839 - 2 * internal_border)
3840 % line_height;
7d0393cf 3841
ee78dc32
GV
3842 if (wdiff || hdiff)
3843 {
7d0393cf
JB
3844 /* For right/bottom sizing we can just fix the sizes.
3845 However for top/left sizing we will need to fix the X
ee78dc32 3846 and Y positions as well. */
7d0393cf 3847
ee78dc32
GV
3848 lppos->cx -= wdiff;
3849 lppos->cy -= hdiff;
7d0393cf
JB
3850
3851 if (wp.showCmd != SW_SHOWMAXIMIZED
1edf84e7 3852 && (lppos->flags & SWP_NOMOVE) == 0)
ee78dc32
GV
3853 {
3854 if (lppos->x != wr.left || lppos->y != wr.top)
3855 {
3856 lppos->x += wdiff;
3857 lppos->y += hdiff;
3858 }
3859 else
3860 {
3861 lppos->flags |= SWP_NOMOVE;
3862 }
3863 }
7d0393cf 3864
1edf84e7 3865 return 0;
ee78dc32
GV
3866 }
3867 }
3868 }
7d0393cf 3869
ee78dc32 3870 goto dflt;
1edf84e7 3871
b1f918f8
GV
3872 case WM_GETMINMAXINFO:
3873 /* Hack to correct bug that allows Emacs frames to be resized
3874 below the Minimum Tracking Size. */
3875 ((LPMINMAXINFO) lParam)->ptMinTrackSize.y++;
bf853fee
AI
3876 /* Hack to allow resizing the Emacs frame above the screen size.
3877 Note that Windows 9x limits coordinates to 16-bits. */
3878 ((LPMINMAXINFO) lParam)->ptMaxTrackSize.x = 32767;
3879 ((LPMINMAXINFO) lParam)->ptMaxTrackSize.y = 32767;
b1f918f8
GV
3880 return 0;
3881
c9b2104d
JR
3882 case WM_SETCURSOR:
3883 if (LOWORD (lParam) == HTCLIENT)
3884 return 0;
3885
3886 goto dflt;
c922a224 3887
c9b2104d
JR
3888 case WM_EMACS_SETCURSOR:
3889 {
3890 Cursor cursor = (Cursor) wParam;
3891 if (cursor)
3892 SetCursor (cursor);
3893 return 0;
3894 }
c922a224 3895
1edf84e7
GV
3896 case WM_EMACS_CREATESCROLLBAR:
3897 return (LRESULT) w32_createscrollbar ((struct frame *) wParam,
3898 (struct scroll_bar *) lParam);
3899
5ac45f98 3900 case WM_EMACS_SHOWWINDOW:
1edf84e7
GV
3901 return ShowWindow ((HWND) wParam, (WPARAM) lParam);
3902
dfdb4047 3903 case WM_EMACS_SETFOREGROUND:
ce6059da
AI
3904 {
3905 HWND foreground_window;
3906 DWORD foreground_thread, retval;
3907
3908 /* On NT 5.0, and apparently Windows 98, it is necessary to
3909 attach to the thread that currently has focus in order to
3910 pull the focus away from it. */
3911 foreground_window = GetForegroundWindow ();
3912 foreground_thread = GetWindowThreadProcessId (foreground_window, NULL);
3913 if (!foreground_window
3914 || foreground_thread == GetCurrentThreadId ()
3915 || !AttachThreadInput (GetCurrentThreadId (),
3916 foreground_thread, TRUE))
3917 foreground_thread = 0;
3918
3919 retval = SetForegroundWindow ((HWND) wParam);
3920
3921 /* Detach from the previous foreground thread. */
3922 if (foreground_thread)
3923 AttachThreadInput (GetCurrentThreadId (),
3924 foreground_thread, FALSE);
3925
3926 return retval;
3927 }
dfdb4047 3928
5ac45f98
GV
3929 case WM_EMACS_SETWINDOWPOS:
3930 {
1edf84e7
GV
3931 WINDOWPOS * pos = (WINDOWPOS *) wParam;
3932 return SetWindowPos (hwnd, pos->hwndInsertAfter,
5ac45f98
GV
3933 pos->x, pos->y, pos->cx, pos->cy, pos->flags);
3934 }
1edf84e7 3935
ee78dc32 3936 case WM_EMACS_DESTROYWINDOW:
cb9e33d4 3937 DragAcceptFiles ((HWND) wParam, FALSE);
1edf84e7
GV
3938 return DestroyWindow ((HWND) wParam);
3939
93f2ca61
JR
3940 case WM_EMACS_HIDE_CARET:
3941 return HideCaret (hwnd);
3942
3943 case WM_EMACS_SHOW_CARET:
3944 return ShowCaret (hwnd);
3945
65906840
JR
3946 case WM_EMACS_DESTROY_CARET:
3947 w32_system_caret_hwnd = NULL;
93f2ca61 3948 w32_visible_system_caret_hwnd = NULL;
65906840
JR
3949 return DestroyCaret ();
3950
3951 case WM_EMACS_TRACK_CARET:
3952 /* If there is currently no system caret, create one. */
3953 if (w32_system_caret_hwnd == NULL)
3954 {
93f2ca61
JR
3955 /* Use the default caret width, and avoid changing it
3956 unneccesarily, as it confuses screen reader software. */
65906840 3957 w32_system_caret_hwnd = hwnd;
93f2ca61 3958 CreateCaret (hwnd, NULL, 0,
65906840
JR
3959 w32_system_caret_height);
3960 }
7d0393cf 3961
93f2ca61
JR
3962 if (!SetCaretPos (w32_system_caret_x, w32_system_caret_y))
3963 return 0;
3964 /* Ensure visible caret gets turned on when requested. */
3965 else if (w32_use_visible_system_caret
3966 && w32_visible_system_caret_hwnd != hwnd)
3967 {
3968 w32_visible_system_caret_hwnd = hwnd;
3969 return ShowCaret (hwnd);
3970 }
3971 /* Ensure visible caret gets turned off when requested. */
3972 else if (!w32_use_visible_system_caret
3973 && w32_visible_system_caret_hwnd)
3974 {
3975 w32_visible_system_caret_hwnd = NULL;
3976 return HideCaret (hwnd);
3977 }
3978 else
3979 return 1;
65906840 3980
1edf84e7
GV
3981 case WM_EMACS_TRACKPOPUPMENU:
3982 {
3983 UINT flags;
3984 POINT *pos;
3985 int retval;
3986 pos = (POINT *)lParam;
3987 flags = TPM_CENTERALIGN;
3988 if (button_state & LMOUSE)
3989 flags |= TPM_LEFTBUTTON;
3990 else if (button_state & RMOUSE)
3991 flags |= TPM_RIGHTBUTTON;
7d0393cf 3992
87996783
GV
3993 /* Remember we did a SetCapture on the initial mouse down event,
3994 so for safety, we make sure the capture is cancelled now. */
3995 ReleaseCapture ();
490822ff 3996 button_state = 0;
87996783 3997
1edf84e7
GV
3998 /* Use menubar_active to indicate that WM_INITMENU is from
3999 TrackPopupMenu below, and should be ignored. */
4000 f = x_window_to_frame (dpyinfo, hwnd);
4001 if (f)
4002 f->output_data.w32->menubar_active = 1;
7d0393cf
JB
4003
4004 if (TrackPopupMenu ((HMENU)wParam, flags, pos->x, pos->y,
1edf84e7
GV
4005 0, hwnd, NULL))
4006 {
4007 MSG amsg;
4008 /* Eat any mouse messages during popupmenu */
4009 while (PeekMessage (&amsg, hwnd, WM_MOUSEFIRST, WM_MOUSELAST,
4010 PM_REMOVE));
4011 /* Get the menu selection, if any */
4012 if (PeekMessage (&amsg, hwnd, WM_COMMAND, WM_COMMAND, PM_REMOVE))
4013 {
4014 retval = LOWORD (amsg.wParam);
4015 }
4016 else
4017 {
4018 retval = 0;
4019 }
1edf84e7
GV
4020 }
4021 else
4022 {
4023 retval = -1;
4024 }
4025
4026 return retval;
4027 }
4028
ee78dc32 4029 default:
93fbe8b7
GV
4030 /* Check for messages registered at runtime. */
4031 if (msg == msh_mousewheel)
4032 {
4033 wmsg.dwModifiers = w32_get_modifiers ();
4034 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4035 return 0;
4036 }
7d0393cf 4037
ee78dc32
GV
4038 dflt:
4039 return DefWindowProc (hwnd, msg, wParam, lParam);
4040 }
7d0393cf 4041
1edf84e7
GV
4042
4043 /* The most common default return code for handled messages is 0. */
4044 return 0;
ee78dc32
GV
4045}
4046
7d0393cf 4047void
ee78dc32
GV
4048my_create_window (f)
4049 struct frame * f;
4050{
4051 MSG msg;
4052
1edf84e7
GV
4053 if (!PostThreadMessage (dwWindowsThreadId, WM_EMACS_CREATEWINDOW, (WPARAM)f, 0))
4054 abort ();
ee78dc32
GV
4055 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
4056}
4057
ca56d953
JR
4058
4059/* Create a tooltip window. Unlike my_create_window, we do not do this
4060 indirectly via the Window thread, as we do not need to process Window
4061 messages for the tooltip. Creating tooltips indirectly also creates
4062 deadlocks when tooltips are created for menu items. */
7d0393cf 4063void
ca56d953
JR
4064my_create_tip_window (f)
4065 struct frame *f;
4066{
bfd6edcc 4067 RECT rect;
ca56d953 4068
bfd6edcc 4069 rect.left = rect.top = 0;
be786000
KS
4070 rect.right = FRAME_PIXEL_WIDTH (f);
4071 rect.bottom = FRAME_PIXEL_HEIGHT (f);
bfd6edcc
JR
4072
4073 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
4074 FRAME_EXTERNAL_MENU_BAR (f));
4075
4076 tip_window = FRAME_W32_WINDOW (f)
ca56d953
JR
4077 = CreateWindow (EMACS_CLASS,
4078 f->namebuf,
4079 f->output_data.w32->dwStyle,
be786000
KS
4080 f->left_pos,
4081 f->top_pos,
bfd6edcc
JR
4082 rect.right - rect.left,
4083 rect.bottom - rect.top,
ca56d953
JR
4084 FRAME_W32_WINDOW (SELECTED_FRAME ()), /* owner */
4085 NULL,
4086 hinst,
4087 NULL);
4088
bfd6edcc 4089 if (tip_window)
ca56d953 4090 {
be786000
KS
4091 SetWindowLong (tip_window, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
4092 SetWindowLong (tip_window, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
4093 SetWindowLong (tip_window, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
bfd6edcc
JR
4094 SetWindowLong (tip_window, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
4095
4096 /* Tip frames have no scrollbars. */
4097 SetWindowLong (tip_window, WND_SCROLLBAR_INDEX, 0);
ca56d953
JR
4098
4099 /* Do this to discard the default setting specified by our parent. */
bfd6edcc 4100 ShowWindow (tip_window, SW_HIDE);
ca56d953
JR
4101 }
4102}
4103
4104
fbd6baed 4105/* Create and set up the w32 window for frame F. */
ee78dc32
GV
4106
4107static void
fbd6baed 4108w32_window (f, window_prompting, minibuffer_only)
ee78dc32
GV
4109 struct frame *f;
4110 long window_prompting;
4111 int minibuffer_only;
4112{
4113 BLOCK_INPUT;
4114
4115 /* Use the resource name as the top-level window name
4116 for looking up resources. Make a non-Lisp copy
4117 for the window manager, so GC relocation won't bother it.
4118
4119 Elsewhere we specify the window name for the window manager. */
7d0393cf 4120
ee78dc32 4121 {
d5db4077 4122 char *str = (char *) SDATA (Vx_resource_name);
ee78dc32
GV
4123 f->namebuf = (char *) xmalloc (strlen (str) + 1);
4124 strcpy (f->namebuf, str);
4125 }
4126
4127 my_create_window (f);
4128
4129 validate_x_resource_name ();
4130
4131 /* x_set_name normally ignores requests to set the name if the
4132 requested name is the same as the current name. This is the one
4133 place where that assumption isn't correct; f->name is set, but
4134 the server hasn't been told. */
4135 {
4136 Lisp_Object name;
4137 int explicit = f->explicit_name;
4138
4139 f->explicit_name = 0;
4140 name = f->name;
4141 f->name = Qnil;
4142 x_set_name (f, name, explicit);
4143 }
4144
4145 UNBLOCK_INPUT;
4146
4147 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
4148 initialize_frame_menubar (f);
4149
fbd6baed 4150 if (FRAME_W32_WINDOW (f) == 0)
ee78dc32
GV
4151 error ("Unable to create window");
4152}
4153
4154/* Handle the icon stuff for this window. Perhaps later we might
4155 want an x_set_icon_position which can be called interactively as
4156 well. */
4157
4158static void
4159x_icon (f, parms)
4160 struct frame *f;
4161 Lisp_Object parms;
4162{
4163 Lisp_Object icon_x, icon_y;
4164
e9e23e23 4165 /* Set the position of the icon. Note that Windows 95 groups all
ee78dc32 4166 icons in the tray. */
6fc2811b
JR
4167 icon_x = w32_get_arg (parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
4168 icon_y = w32_get_arg (parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
ee78dc32
GV
4169 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
4170 {
b7826503
PJ
4171 CHECK_NUMBER (icon_x);
4172 CHECK_NUMBER (icon_y);
ee78dc32
GV
4173 }
4174 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
4175 error ("Both left and top icon corners of icon must be specified");
4176
4177 BLOCK_INPUT;
4178
4179 if (! EQ (icon_x, Qunbound))
4180 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
4181
1edf84e7
GV
4182#if 0 /* TODO */
4183 /* Start up iconic or window? */
4184 x_wm_set_window_state
6fc2811b 4185 (f, (EQ (w32_get_arg (parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL), Qicon)
1edf84e7
GV
4186 ? IconicState
4187 : NormalState));
4188
d5db4077 4189 x_text_icon (f, (char *) SDATA ((!NILP (f->icon_name)
1edf84e7 4190 ? f->icon_name
d5db4077 4191 : f->name)));
1edf84e7
GV
4192#endif
4193
ee78dc32
GV
4194 UNBLOCK_INPUT;
4195}
4196
6fc2811b
JR
4197
4198static void
4199x_make_gc (f)
4200 struct frame *f;
4201{
4202 XGCValues gc_values;
4203
4204 BLOCK_INPUT;
4205
4206 /* Create the GC's of this frame.
4207 Note that many default values are used. */
4208
4209 /* Normal video */
be786000 4210 gc_values.font = FRAME_FONT (f);
6fc2811b
JR
4211
4212 /* Cursor has cursor-color background, background-color foreground. */
4213 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
4214 gc_values.background = f->output_data.w32->cursor_pixel;
4215 f->output_data.w32->cursor_gc
4216 = XCreateGC (NULL, FRAME_W32_WINDOW (f),
4217 (GCFont | GCForeground | GCBackground),
4218 &gc_values);
4219
4220 /* Reliefs. */
4221 f->output_data.w32->white_relief.gc = 0;
4222 f->output_data.w32->black_relief.gc = 0;
4223
4224 UNBLOCK_INPUT;
4225}
4226
4227
937e601e
AI
4228/* Handler for signals raised during x_create_frame and
4229 x_create_top_frame. FRAME is the frame which is partially
4230 constructed. */
4231
4232static Lisp_Object
4233unwind_create_frame (frame)
4234 Lisp_Object frame;
4235{
4236 struct frame *f = XFRAME (frame);
4237
4238 /* If frame is ``official'', nothing to do. */
4239 if (!CONSP (Vframe_list) || !EQ (XCAR (Vframe_list), frame))
4240 {
4241#ifdef GLYPH_DEBUG
4242 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4243#endif
7d0393cf 4244
937e601e
AI
4245 x_free_frame_resources (f);
4246
4247 /* Check that reference counts are indeed correct. */
4248 xassert (dpyinfo->reference_count == dpyinfo_refcount);
4249 xassert (dpyinfo->image_cache->refcount == image_cache_refcount);
c844a81a
GM
4250
4251 return Qt;
937e601e 4252 }
7d0393cf 4253
937e601e
AI
4254 return Qnil;
4255}
4256
4257
ee78dc32
GV
4258DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
4259 1, 1, 0,
74e1aeec
JR
4260 doc: /* Make a new window, which is called a \"frame\" in Emacs terms.
4261Returns an Emacs frame object.
4262ALIST is an alist of frame parameters.
4263If the parameters specify that the frame should not have a minibuffer,
4264and do not specify a specific minibuffer window to use,
4265then `default-minibuffer-frame' must be a frame whose minibuffer can
4266be shared by the new frame.
4267
4268This function is an internal primitive--use `make-frame' instead. */)
ee78dc32
GV
4269 (parms)
4270 Lisp_Object parms;
4271{
4272 struct frame *f;
4273 Lisp_Object frame, tem;
4274 Lisp_Object name;
4275 int minibuffer_only = 0;
4276 long window_prompting = 0;
4277 int width, height;
331379bf 4278 int count = SPECPDL_INDEX ();
1edf84e7 4279 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
ee78dc32 4280 Lisp_Object display;
6fc2811b 4281 struct w32_display_info *dpyinfo = NULL;
ee78dc32
GV
4282 Lisp_Object parent;
4283 struct kboard *kb;
4284
4587b026
GV
4285 check_w32 ();
4286
ee78dc32
GV
4287 /* Use this general default value to start with
4288 until we know if this frame has a specified name. */
4289 Vx_resource_name = Vinvocation_name;
4290
6fc2811b 4291 display = w32_get_arg (parms, Qdisplay, 0, 0, RES_TYPE_STRING);
ee78dc32
GV
4292 if (EQ (display, Qunbound))
4293 display = Qnil;
4294 dpyinfo = check_x_display_info (display);
4295#ifdef MULTI_KBOARD
4296 kb = dpyinfo->kboard;
4297#else
4298 kb = &the_only_kboard;
4299#endif
4300
6fc2811b 4301 name = w32_get_arg (parms, Qname, "name", "Name", RES_TYPE_STRING);
ee78dc32
GV
4302 if (!STRINGP (name)
4303 && ! EQ (name, Qunbound)
4304 && ! NILP (name))
4305 error ("Invalid frame name--not a string or nil");
4306
4307 if (STRINGP (name))
4308 Vx_resource_name = name;
4309
4310 /* See if parent window is specified. */
6fc2811b 4311 parent = w32_get_arg (parms, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
ee78dc32
GV
4312 if (EQ (parent, Qunbound))
4313 parent = Qnil;
4314 if (! NILP (parent))
b7826503 4315 CHECK_NUMBER (parent);
ee78dc32 4316
1edf84e7
GV
4317 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
4318 /* No need to protect DISPLAY because that's not used after passing
4319 it to make_frame_without_minibuffer. */
4320 frame = Qnil;
4321 GCPRO4 (parms, parent, name, frame);
1660f34a
JR
4322 tem = w32_get_arg (parms, Qminibuffer, "minibuffer", "Minibuffer",
4323 RES_TYPE_SYMBOL);
ee78dc32
GV
4324 if (EQ (tem, Qnone) || NILP (tem))
4325 f = make_frame_without_minibuffer (Qnil, kb, display);
4326 else if (EQ (tem, Qonly))
4327 {
4328 f = make_minibuffer_frame ();
4329 minibuffer_only = 1;
4330 }
4331 else if (WINDOWP (tem))
4332 f = make_frame_without_minibuffer (tem, kb, display);
4333 else
4334 f = make_frame (1);
4335
1edf84e7
GV
4336 XSETFRAME (frame, f);
4337
ee78dc32
GV
4338 /* Note that Windows does support scroll bars. */
4339 FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;
6d906347 4340
5ac45f98 4341 /* By default, make scrollbars the system standard width. */
be786000 4342 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = GetSystemMetrics (SM_CXVSCROLL);
ee78dc32 4343
fbd6baed 4344 f->output_method = output_w32;
6fc2811b
JR
4345 f->output_data.w32 =
4346 (struct w32_output *) xmalloc (sizeof (struct w32_output));
fbd6baed 4347 bzero (f->output_data.w32, sizeof (struct w32_output));
4587b026 4348 FRAME_FONTSET (f) = -1;
937e601e 4349 record_unwind_protect (unwind_create_frame, frame);
4587b026 4350
1edf84e7 4351 f->icon_name
6fc2811b 4352 = w32_get_arg (parms, Qicon_name, "iconName", "Title", RES_TYPE_STRING);
1edf84e7
GV
4353 if (! STRINGP (f->icon_name))
4354 f->icon_name = Qnil;
4355
fbd6baed 4356/* FRAME_W32_DISPLAY_INFO (f) = dpyinfo; */
ee78dc32
GV
4357#ifdef MULTI_KBOARD
4358 FRAME_KBOARD (f) = kb;
4359#endif
4360
4361 /* Specify the parent under which to make this window. */
4362
4363 if (!NILP (parent))
4364 {
1660f34a 4365 f->output_data.w32->parent_desc = (Window) XFASTINT (parent);
fbd6baed 4366 f->output_data.w32->explicit_parent = 1;
ee78dc32
GV
4367 }
4368 else
4369 {
fbd6baed
GV
4370 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
4371 f->output_data.w32->explicit_parent = 0;
ee78dc32
GV
4372 }
4373
ee78dc32
GV
4374 /* Set the name; the functions to which we pass f expect the name to
4375 be set. */
4376 if (EQ (name, Qunbound) || NILP (name))
4377 {
fbd6baed 4378 f->name = build_string (dpyinfo->w32_id_name);
ee78dc32
GV
4379 f->explicit_name = 0;
4380 }
4381 else
4382 {
4383 f->name = name;
4384 f->explicit_name = 1;
4385 /* use the frame's title when getting resources for this frame. */
4386 specbind (Qx_resource_name, name);
4387 }
4388
4389 /* Extract the window parameters from the supplied values
4390 that are needed to determine window geometry. */
4391 {
4392 Lisp_Object font;
4393
6fc2811b
JR
4394 font = w32_get_arg (parms, Qfont, "font", "Font", RES_TYPE_STRING);
4395
ee78dc32
GV
4396 BLOCK_INPUT;
4397 /* First, try whatever font the caller has specified. */
4398 if (STRINGP (font))
4587b026
GV
4399 {
4400 tem = Fquery_fontset (font, Qnil);
4401 if (STRINGP (tem))
d5db4077 4402 font = x_new_fontset (f, SDATA (tem));
4587b026 4403 else
d5db4077 4404 font = x_new_font (f, SDATA (font));
4587b026 4405 }
ee78dc32
GV
4406 /* Try out a font which we hope has bold and italic variations. */
4407 if (!STRINGP (font))
e39649be 4408 font = x_new_font (f, "-*-Courier New-normal-r-*-*-*-100-*-*-c-*-iso8859-1");
ee78dc32 4409 if (! STRINGP (font))
6fc2811b 4410 font = x_new_font (f, "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1");
ee78dc32
GV
4411 /* If those didn't work, look for something which will at least work. */
4412 if (! STRINGP (font))
6fc2811b 4413 font = x_new_font (f, "-*-Fixedsys-normal-r-*-*-12-*-*-*-c-*-iso8859-1");
ee78dc32
GV
4414 UNBLOCK_INPUT;
4415 if (! STRINGP (font))
1edf84e7 4416 font = build_string ("Fixedsys");
ee78dc32 4417
7d0393cf 4418 x_default_parameter (f, parms, Qfont, font,
6fc2811b 4419 "font", "Font", RES_TYPE_STRING);
ee78dc32
GV
4420 }
4421
4422 x_default_parameter (f, parms, Qborder_width, make_number (2),
1660f34a 4423 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
ee78dc32
GV
4424 /* This defaults to 2 in order to match xterm. We recognize either
4425 internalBorderWidth or internalBorder (which is what xterm calls
4426 it). */
4427 if (NILP (Fassq (Qinternal_border_width, parms)))
4428 {
4429 Lisp_Object value;
4430
6fc2811b 4431 value = w32_get_arg (parms, Qinternal_border_width,
1660f34a 4432 "internalBorder", "InternalBorder", RES_TYPE_NUMBER);
ee78dc32
GV
4433 if (! EQ (value, Qunbound))
4434 parms = Fcons (Fcons (Qinternal_border_width, value),
4435 parms);
4436 }
1edf84e7 4437 /* Default internalBorderWidth to 0 on Windows to match other programs. */
ee78dc32 4438 x_default_parameter (f, parms, Qinternal_border_width, make_number (0),
1660f34a
JR
4439 "internalBorderWidth", "InternalBorder", RES_TYPE_NUMBER);
4440 x_default_parameter (f, parms, Qvertical_scroll_bars, Qright,
4441 "verticalScrollBars", "ScrollBars", RES_TYPE_SYMBOL);
ee78dc32
GV
4442
4443 /* Also do the stuff which must be set before the window exists. */
4444 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
6fc2811b 4445 "foreground", "Foreground", RES_TYPE_STRING);
ee78dc32 4446 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
6fc2811b 4447 "background", "Background", RES_TYPE_STRING);
ee78dc32 4448 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
6fc2811b 4449 "pointerColor", "Foreground", RES_TYPE_STRING);
ee78dc32 4450 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
6fc2811b 4451 "cursorColor", "Foreground", RES_TYPE_STRING);
ee78dc32 4452 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
6fc2811b
JR
4453 "borderColor", "BorderColor", RES_TYPE_STRING);
4454 x_default_parameter (f, parms, Qscreen_gamma, Qnil,
4455 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
dfff8a69
JR
4456 x_default_parameter (f, parms, Qline_spacing, Qnil,
4457 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
41c1bdd9
KS
4458 x_default_parameter (f, parms, Qleft_fringe, Qnil,
4459 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
4460 x_default_parameter (f, parms, Qright_fringe, Qnil,
4461 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
6fc2811b 4462
ee78dc32 4463
6fc2811b
JR
4464 /* Init faces before x_default_parameter is called for scroll-bar
4465 parameters because that function calls x_set_scroll_bar_width,
4466 which calls change_frame_size, which calls Fset_window_buffer,
4467 which runs hooks, which call Fvertical_motion. At the end, we
4468 end up in init_iterator with a null face cache, which should not
4469 happen. */
4470 init_frame_faces (f);
7d0393cf 4471
ee78dc32 4472 x_default_parameter (f, parms, Qmenu_bar_lines, make_number (1),
6fc2811b 4473 "menuBar", "MenuBar", RES_TYPE_NUMBER);
d3109773 4474 x_default_parameter (f, parms, Qtool_bar_lines, make_number (1),
6fc2811b 4475 "toolBar", "ToolBar", RES_TYPE_NUMBER);
919f1e88 4476
1edf84e7 4477 x_default_parameter (f, parms, Qbuffer_predicate, Qnil,
6fc2811b 4478 "bufferPredicate", "BufferPredicate", RES_TYPE_SYMBOL);
1edf84e7 4479 x_default_parameter (f, parms, Qtitle, Qnil,
6fc2811b 4480 "title", "Title", RES_TYPE_STRING);
f7b9d4d1
JR
4481 x_default_parameter (f, parms, Qfullscreen, Qnil,
4482 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
ee78dc32 4483
fbd6baed
GV
4484 f->output_data.w32->dwStyle = WS_OVERLAPPEDWINDOW;
4485 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
3cf3436e 4486
c9b2104d
JR
4487 f->output_data.w32->text_cursor = w32_load_cursor (IDC_IBEAM);
4488 f->output_data.w32->nontext_cursor = w32_load_cursor (IDC_ARROW);
4489 f->output_data.w32->modeline_cursor = w32_load_cursor (IDC_ARROW);
7d63e5e3 4490 f->output_data.w32->hand_cursor = w32_load_cursor (IDC_HAND);
c9b2104d
JR
4491 f->output_data.w32->hourglass_cursor = w32_load_cursor (IDC_WAIT);
4492 f->output_data.w32->horizontal_drag_cursor = w32_load_cursor (IDC_SIZEWE);
c9b2104d 4493
6d906347 4494 window_prompting = x_figure_window_size (f, parms, 1);
ee78dc32 4495
6fc2811b
JR
4496 tem = w32_get_arg (parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
4497 f->no_split = minibuffer_only || EQ (tem, Qt);
4498
fbd6baed 4499 w32_window (f, window_prompting, minibuffer_only);
ee78dc32 4500 x_icon (f, parms);
6fc2811b
JR
4501
4502 x_make_gc (f);
4503
4504 /* Now consider the frame official. */
4505 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
4506 Vframe_list = Fcons (frame, Vframe_list);
ee78dc32
GV
4507
4508 /* We need to do this after creating the window, so that the
4509 icon-creation functions can say whose icon they're describing. */
4510 x_default_parameter (f, parms, Qicon_type, Qnil,
6fc2811b 4511 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
ee78dc32
GV
4512
4513 x_default_parameter (f, parms, Qauto_raise, Qnil,
6fc2811b 4514 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
ee78dc32 4515 x_default_parameter (f, parms, Qauto_lower, Qnil,
6fc2811b 4516 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
ee78dc32 4517 x_default_parameter (f, parms, Qcursor_type, Qbox,
6fc2811b
JR
4518 "cursorType", "CursorType", RES_TYPE_SYMBOL);
4519 x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
4520 "scrollBarWidth", "ScrollBarWidth", RES_TYPE_NUMBER);
ee78dc32 4521
be786000 4522 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
ee78dc32 4523 Change will not be effected unless different from the current
be786000
KS
4524 FRAME_LINES (f). */
4525 width = FRAME_COLS (f);
4526 height = FRAME_LINES (f);
dc220243 4527
be786000
KS
4528 FRAME_LINES (f) = 0;
4529 SET_FRAME_COLS (f, 0);
6fc2811b
JR
4530 change_frame_size (f, height, width, 1, 0, 0);
4531
6fc2811b
JR
4532 /* Tell the server what size and position, etc, we want, and how
4533 badly we want them. This should be done after we have the menu
4534 bar so that its size can be taken into account. */
ee78dc32
GV
4535 BLOCK_INPUT;
4536 x_wm_set_size_hint (f, window_prompting, 0);
4537 UNBLOCK_INPUT;
4538
815d969e
JR
4539 /* Avoid a bug that causes the new frame to never become visible if
4540 an echo area message is displayed during the following call1. */
4541 specbind(Qredisplay_dont_pause, Qt);
4542
4694d762
JR
4543 /* Set up faces after all frame parameters are known. This call
4544 also merges in face attributes specified for new frames. If we
4545 don't do this, the `menu' face for instance won't have the right
4546 colors, and the menu bar won't appear in the specified colors for
4547 new frames. */
4548 call1 (Qface_set_after_frame_default, frame);
4549
6fc2811b
JR
4550 /* Make the window appear on the frame and enable display, unless
4551 the caller says not to. However, with explicit parent, Emacs
4552 cannot control visibility, so don't try. */
fbd6baed 4553 if (! f->output_data.w32->explicit_parent)
ee78dc32
GV
4554 {
4555 Lisp_Object visibility;
4556
6fc2811b 4557 visibility = w32_get_arg (parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL);
ee78dc32
GV
4558 if (EQ (visibility, Qunbound))
4559 visibility = Qt;
4560
4561 if (EQ (visibility, Qicon))
4562 x_iconify_frame (f);
4563 else if (! NILP (visibility))
4564 x_make_frame_visible (f);
4565 else
4566 /* Must have been Qnil. */
4567 ;
4568 }
6fc2811b 4569 UNGCPRO;
7d0393cf 4570
9e57df62
GM
4571 /* Make sure windows on this frame appear in calls to next-window
4572 and similar functions. */
4573 Vwindow_list = Qnil;
7d0393cf 4574
ee78dc32
GV
4575 return unbind_to (count, frame);
4576}
4577
4578/* FRAME is used only to get a handle on the X display. We don't pass the
4579 display info directly because we're called from frame.c, which doesn't
4580 know about that structure. */
4581Lisp_Object
4582x_get_focus_frame (frame)
4583 struct frame *frame;
4584{
fbd6baed 4585 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (frame);
ee78dc32 4586 Lisp_Object xfocus;
fbd6baed 4587 if (! dpyinfo->w32_focus_frame)
ee78dc32
GV
4588 return Qnil;
4589
fbd6baed 4590 XSETFRAME (xfocus, dpyinfo->w32_focus_frame);
ee78dc32
GV
4591 return xfocus;
4592}
1edf84e7
GV
4593
4594DEFUN ("w32-focus-frame", Fw32_focus_frame, Sw32_focus_frame, 1, 1, 0,
74e1aeec 4595 doc: /* Give FRAME input focus, raising to foreground if necessary. */)
1edf84e7
GV
4596 (frame)
4597 Lisp_Object frame;
4598{
4599 x_focus_on_frame (check_x_frame (frame));
4600 return Qnil;
4601}
4602
ee78dc32 4603\f
767b1ff0
JR
4604/* Return the charset portion of a font name. */
4605char * xlfd_charset_of_font (char * fontname)
4606{
4607 char *charset, *encoding;
4608
4609 encoding = strrchr(fontname, '-');
ceb12877 4610 if (!encoding || encoding == fontname)
767b1ff0
JR
4611 return NULL;
4612
478ea067
AI
4613 for (charset = encoding - 1; charset >= fontname; charset--)
4614 if (*charset == '-')
4615 break;
767b1ff0 4616
478ea067 4617 if (charset == fontname || strcmp(charset, "-*-*") == 0)
767b1ff0
JR
4618 return NULL;
4619
4620 return charset + 1;
4621}
4622
33d52f9c
GV
4623struct font_info *w32_load_bdf_font (struct frame *f, char *fontname,
4624 int size, char* filename);
8edb0a6f 4625static Lisp_Object w32_list_bdf_fonts (Lisp_Object pattern, int max_names);
c7501041
EZ
4626static BOOL w32_to_x_font (LOGFONT * lplf, char * lpxstr, int len,
4627 char * charset);
4628static BOOL x_to_w32_font (char *lpxstr, LOGFONT *lplogfont);
33d52f9c 4629
8edb0a6f 4630static struct font_info *
33d52f9c 4631w32_load_system_font (f,fontname,size)
55dcfc15
AI
4632 struct frame *f;
4633 char * fontname;
4634 int size;
ee78dc32 4635{
4587b026
GV
4636 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
4637 Lisp_Object font_names;
4638
4587b026
GV
4639 /* Get a list of all the fonts that match this name. Once we
4640 have a list of matching fonts, we compare them against the fonts
4641 we already have loaded by comparing names. */
4642 font_names = w32_list_fonts (f, build_string (fontname), size, 100);
4643
4644 if (!NILP (font_names))
3c190163 4645 {
4587b026
GV
4646 Lisp_Object tail;
4647 int i;
4587b026
GV
4648
4649 /* First check if any are already loaded, as that is cheaper
4650 than loading another one. */
4651 for (i = 0; i < dpyinfo->n_fonts; i++)
8e713be6 4652 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
6fc2811b
JR
4653 if (dpyinfo->font_table[i].name
4654 && (!strcmp (dpyinfo->font_table[i].name,
d5db4077 4655 SDATA (XCAR (tail)))
6fc2811b 4656 || !strcmp (dpyinfo->font_table[i].full_name,
d5db4077 4657 SDATA (XCAR (tail)))))
4587b026 4658 return (dpyinfo->font_table + i);
6fc2811b 4659
d5db4077 4660 fontname = (char *) SDATA (XCAR (font_names));
4587b026 4661 }
1075afa9 4662 else if (w32_strict_fontnames)
5ca0cd71
GV
4663 {
4664 /* If EnumFontFamiliesEx was available, we got a full list of
4665 fonts back so stop now to avoid the possibility of loading a
4666 random font. If we had to fall back to EnumFontFamilies, the
4667 list is incomplete, so continue whether the font we want was
4668 listed or not. */
4669 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
4670 FARPROC enum_font_families_ex
1075afa9 4671 = GetProcAddress (gdi32, "EnumFontFamiliesExA");
5ca0cd71
GV
4672 if (enum_font_families_ex)
4673 return NULL;
4674 }
4587b026
GV
4675
4676 /* Load the font and add it to the table. */
4677 {
767b1ff0 4678 char *full_name, *encoding, *charset;
4587b026
GV
4679 XFontStruct *font;
4680 struct font_info *fontp;
3c190163 4681 LOGFONT lf;
4587b026 4682 BOOL ok;
19c291d3 4683 int codepage;
6fc2811b 4684 int i;
5ac45f98 4685
4587b026 4686 if (!fontname || !x_to_w32_font (fontname, &lf))
3c190163 4687 return (NULL);
5ac45f98 4688
4587b026
GV
4689 if (!*lf.lfFaceName)
4690 /* If no name was specified for the font, we get a random font
4691 from CreateFontIndirect - this is not particularly
4692 desirable, especially since CreateFontIndirect does not
4693 fill out the missing name in lf, so we never know what we
4694 ended up with. */
4695 return NULL;
4696
c8d88d08 4697 lf.lfQuality = DEFAULT_QUALITY;
d65a9cdc 4698
3c190163 4699 font = (XFontStruct *) xmalloc (sizeof (XFontStruct));
c6be3860 4700 bzero (font, sizeof (*font));
5ac45f98 4701
33d52f9c
GV
4702 /* Set bdf to NULL to indicate that this is a Windows font. */
4703 font->bdf = NULL;
5ac45f98 4704
3c190163 4705 BLOCK_INPUT;
5ac45f98
GV
4706
4707 font->hfont = CreateFontIndirect (&lf);
ee78dc32 4708
7d0393cf 4709 if (font->hfont == NULL)
1a292d24
AI
4710 {
4711 ok = FALSE;
7d0393cf
JB
4712 }
4713 else
1a292d24
AI
4714 {
4715 HDC hdc;
4716 HANDLE oldobj;
19c291d3
AI
4717
4718 codepage = w32_codepage_for_font (fontname);
1a292d24
AI
4719
4720 hdc = GetDC (dpyinfo->root_window);
4721 oldobj = SelectObject (hdc, font->hfont);
5c6682be 4722
1a292d24 4723 ok = GetTextMetrics (hdc, &font->tm);
5c6682be
JR
4724 if (codepage == CP_UNICODE)
4725 font->double_byte_p = 1;
4726 else
8b77111c
AI
4727 {
4728 /* Unfortunately, some fonts (eg. MingLiU, a big5 ttf font)
4729 don't report themselves as double byte fonts, when
4730 patently they are. So instead of trusting
4731 GetFontLanguageInfo, we check the properties of the
4732 codepage directly, since that is ultimately what we are
4733 working from anyway. */
4734 /* font->double_byte_p = GetFontLanguageInfo(hdc) & GCP_DBCS; */
4735 CPINFO cpi = {0};
4736 GetCPInfo (codepage, &cpi);
4737 font->double_byte_p = cpi.MaxCharSize > 1;
4738 }
5c6682be 4739
1a292d24
AI
4740 SelectObject (hdc, oldobj);
4741 ReleaseDC (dpyinfo->root_window, hdc);
6fc2811b
JR
4742 /* Fill out details in lf according to the font that was
4743 actually loaded. */
4744 lf.lfHeight = font->tm.tmInternalLeading - font->tm.tmHeight;
4745 lf.lfWidth = font->tm.tmAveCharWidth;
4746 lf.lfWeight = font->tm.tmWeight;
4747 lf.lfItalic = font->tm.tmItalic;
4748 lf.lfCharSet = font->tm.tmCharSet;
4749 lf.lfPitchAndFamily = ((font->tm.tmPitchAndFamily & TMPF_FIXED_PITCH)
d88c567c 4750 ? VARIABLE_PITCH : FIXED_PITCH);
6fc2811b
JR
4751 lf.lfOutPrecision = ((font->tm.tmPitchAndFamily & TMPF_VECTOR)
4752 ? OUT_STROKE_PRECIS : OUT_STRING_PRECIS);
c6be3860
AI
4753
4754 w32_cache_char_metrics (font);
1a292d24 4755 }
5ac45f98 4756
1a292d24 4757 UNBLOCK_INPUT;
5ac45f98 4758
4587b026
GV
4759 if (!ok)
4760 {
1a292d24
AI
4761 w32_unload_font (dpyinfo, font);
4762 return (NULL);
4763 }
ee78dc32 4764
6fc2811b
JR
4765 /* Find a free slot in the font table. */
4766 for (i = 0; i < dpyinfo->n_fonts; ++i)
4767 if (dpyinfo->font_table[i].name == NULL)
4768 break;
4769
4770 /* If no free slot found, maybe enlarge the font table. */
4771 if (i == dpyinfo->n_fonts
4772 && dpyinfo->n_fonts == dpyinfo->font_table_size)
4587b026 4773 {
6fc2811b
JR
4774 int sz;
4775 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
4776 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
4587b026 4777 dpyinfo->font_table
6fc2811b 4778 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
4587b026
GV
4779 }
4780
6fc2811b
JR
4781 fontp = dpyinfo->font_table + i;
4782 if (i == dpyinfo->n_fonts)
4783 ++dpyinfo->n_fonts;
4587b026
GV
4784
4785 /* Now fill in the slots of *FONTP. */
4786 BLOCK_INPUT;
0d4c2dc2 4787 bzero (fontp, sizeof (*fontp));
4587b026 4788 fontp->font = font;
6fc2811b 4789 fontp->font_idx = i;
4587b026
GV
4790 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
4791 bcopy (fontname, fontp->name, strlen (fontname) + 1);
4792
767b1ff0
JR
4793 charset = xlfd_charset_of_font (fontname);
4794
19c291d3
AI
4795 /* Cache the W32 codepage for a font. This makes w32_encode_char
4796 (called for every glyph during redisplay) much faster. */
4797 fontp->codepage = codepage;
4798
4587b026
GV
4799 /* Work out the font's full name. */
4800 full_name = (char *)xmalloc (100);
767b1ff0 4801 if (full_name && w32_to_x_font (&lf, full_name, 100, charset))
4587b026
GV
4802 fontp->full_name = full_name;
4803 else
4804 {
4805 /* If all else fails - just use the name we used to load it. */
4806 xfree (full_name);
4807 fontp->full_name = fontp->name;
4808 }
4809
4810 fontp->size = FONT_WIDTH (font);
4811 fontp->height = FONT_HEIGHT (font);
4812
4813 /* The slot `encoding' specifies how to map a character
4814 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
126f2e35
JR
4815 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
4816 (0:0x20..0x7F, 1:0xA0..0xFF,
4817 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
4587b026 4818 2:0xA020..0xFF7F). For the moment, we don't know which charset
6fc2811b 4819 uses this font. So, we set information in fontp->encoding[1]
4587b026
GV
4820 which is never used by any charset. If mapping can't be
4821 decided, set FONT_ENCODING_NOT_DECIDED. */
33d52f9c
GV
4822
4823 /* SJIS fonts need to be set to type 4, all others seem to work as
4824 type FONT_ENCODING_NOT_DECIDED. */
4825 encoding = strrchr (fontp->name, '-');
d84b082d 4826 if (encoding && strnicmp (encoding+1, "sjis", 4) == 0)
1c885fe1 4827 fontp->encoding[1] = 4;
33d52f9c 4828 else
1c885fe1 4829 fontp->encoding[1] = FONT_ENCODING_NOT_DECIDED;
4587b026
GV
4830
4831 /* The following three values are set to 0 under W32, which is
4832 what they get set to if XGetFontProperty fails under X. */
4833 fontp->baseline_offset = 0;
4834 fontp->relative_compose = 0;
33d52f9c 4835 fontp->default_ascent = 0;
4587b026 4836
6fc2811b
JR
4837 /* Set global flag fonts_changed_p to non-zero if the font loaded
4838 has a character with a smaller width than any other character
f7b9d4d1 4839 before, or if the font loaded has a smaller height than any
6fc2811b
JR
4840 other font loaded before. If this happens, it will make a
4841 glyph matrix reallocation necessary. */
f7b9d4d1 4842 fonts_changed_p |= x_compute_min_glyph_bounds (f);
4587b026 4843 UNBLOCK_INPUT;
4587b026
GV
4844 return fontp;
4845 }
4846}
4847
33d52f9c
GV
4848/* Load font named FONTNAME of size SIZE for frame F, and return a
4849 pointer to the structure font_info while allocating it dynamically.
4850 If loading fails, return NULL. */
4851struct font_info *
4852w32_load_font (f,fontname,size)
4853struct frame *f;
4854char * fontname;
4855int size;
4856{
4857 Lisp_Object bdf_fonts;
4858 struct font_info *retval = NULL;
4859
8edb0a6f 4860 bdf_fonts = w32_list_bdf_fonts (build_string (fontname), 1);
33d52f9c
GV
4861
4862 while (!retval && CONSP (bdf_fonts))
4863 {
4864 char *bdf_name, *bdf_file;
4865 Lisp_Object bdf_pair;
4866
d5db4077 4867 bdf_name = SDATA (XCAR (bdf_fonts));
8e713be6 4868 bdf_pair = Fassoc (XCAR (bdf_fonts), Vw32_bdf_filename_alist);
d5db4077 4869 bdf_file = SDATA (XCDR (bdf_pair));
33d52f9c
GV
4870
4871 retval = w32_load_bdf_font (f, bdf_name, size, bdf_file);
4872
8e713be6 4873 bdf_fonts = XCDR (bdf_fonts);
33d52f9c
GV
4874 }
4875
4876 if (retval)
4877 return retval;
4878
4879 return w32_load_system_font(f, fontname, size);
4880}
4881
4882
7d0393cf 4883void
fbd6baed
GV
4884w32_unload_font (dpyinfo, font)
4885 struct w32_display_info *dpyinfo;
ee78dc32
GV
4886 XFontStruct * font;
4887{
7d0393cf 4888 if (font)
ee78dc32 4889 {
c6be3860 4890 if (font->per_char) xfree (font->per_char);
33d52f9c
GV
4891 if (font->bdf) w32_free_bdf_font (font->bdf);
4892
3c190163 4893 if (font->hfont) DeleteObject(font->hfont);
ee78dc32
GV
4894 xfree (font);
4895 }
4896}
4897
fbd6baed 4898/* The font conversion stuff between x and w32 */
ee78dc32
GV
4899
4900/* X font string is as follows (from faces.el)
4901 * (let ((- "[-?]")
4902 * (foundry "[^-]+")
4903 * (family "[^-]+")
4904 * (weight "\\(bold\\|demibold\\|medium\\)") ; 1
4905 * (weight\? "\\([^-]*\\)") ; 1
4906 * (slant "\\([ior]\\)") ; 2
4907 * (slant\? "\\([^-]?\\)") ; 2
4908 * (swidth "\\([^-]*\\)") ; 3
4909 * (adstyle "[^-]*") ; 4
4910 * (pixelsize "[0-9]+")
4911 * (pointsize "[0-9][0-9]+")
4912 * (resx "[0-9][0-9]+")
4913 * (resy "[0-9][0-9]+")
4914 * (spacing "[cmp?*]")
4915 * (avgwidth "[0-9]+")
4916 * (registry "[^-]+")
4917 * (encoding "[^-]+")
4918 * )
ee78dc32 4919 */
ee78dc32 4920
7d0393cf 4921static LONG
fbd6baed 4922x_to_w32_weight (lpw)
ee78dc32
GV
4923 char * lpw;
4924{
4925 if (!lpw) return (FW_DONTCARE);
5ac45f98
GV
4926
4927 if (stricmp (lpw,"heavy") == 0) return FW_HEAVY;
4928 else if (stricmp (lpw,"extrabold") == 0) return FW_EXTRABOLD;
4929 else if (stricmp (lpw,"bold") == 0) return FW_BOLD;
4930 else if (stricmp (lpw,"demibold") == 0) return FW_SEMIBOLD;
1edf84e7 4931 else if (stricmp (lpw,"semibold") == 0) return FW_SEMIBOLD;
5ac45f98
GV
4932 else if (stricmp (lpw,"medium") == 0) return FW_MEDIUM;
4933 else if (stricmp (lpw,"normal") == 0) return FW_NORMAL;
4934 else if (stricmp (lpw,"light") == 0) return FW_LIGHT;
4935 else if (stricmp (lpw,"extralight") == 0) return FW_EXTRALIGHT;
4936 else if (stricmp (lpw,"thin") == 0) return FW_THIN;
ee78dc32 4937 else
5ac45f98 4938 return FW_DONTCARE;
ee78dc32
GV
4939}
4940
5ac45f98 4941
7d0393cf 4942static char *
fbd6baed 4943w32_to_x_weight (fnweight)
ee78dc32
GV
4944 int fnweight;
4945{
5ac45f98
GV
4946 if (fnweight >= FW_HEAVY) return "heavy";
4947 if (fnweight >= FW_EXTRABOLD) return "extrabold";
4948 if (fnweight >= FW_BOLD) return "bold";
03f8fb34 4949 if (fnweight >= FW_SEMIBOLD) return "demibold";
5ac45f98
GV
4950 if (fnweight >= FW_MEDIUM) return "medium";
4951 if (fnweight >= FW_NORMAL) return "normal";
4952 if (fnweight >= FW_LIGHT) return "light";
4953 if (fnweight >= FW_EXTRALIGHT) return "extralight";
4954 if (fnweight >= FW_THIN) return "thin";
4955 else
4956 return "*";
4957}
4958
8edb0a6f 4959static LONG
fbd6baed 4960x_to_w32_charset (lpcs)
5ac45f98
GV
4961 char * lpcs;
4962{
767b1ff0 4963 Lisp_Object this_entry, w32_charset;
8b77111c
AI
4964 char *charset;
4965 int len = strlen (lpcs);
4966
4967 /* Support "*-#nnn" format for unknown charsets. */
4968 if (strncmp (lpcs, "*-#", 3) == 0)
4969 return atoi (lpcs + 3);
4970
4971 /* Handle wildcards by ignoring them; eg. treat "big5*-*" as "big5". */
4972 charset = alloca (len + 1);
4973 strcpy (charset, lpcs);
4974 lpcs = strchr (charset, '*');
4975 if (lpcs)
4976 *lpcs = 0;
4587b026 4977
dfff8a69
JR
4978 /* Look through w32-charset-info-alist for the character set.
4979 Format of each entry is
4980 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
4981 */
8b77111c 4982 this_entry = Fassoc (build_string(charset), Vw32_charset_info_alist);
4587b026 4983
767b1ff0
JR
4984 if (NILP(this_entry))
4985 {
4986 /* At startup, we want iso8859-1 fonts to come up properly. */
8b77111c 4987 if (stricmp(charset, "iso8859-1") == 0)
767b1ff0
JR
4988 return ANSI_CHARSET;
4989 else
4990 return DEFAULT_CHARSET;
4991 }
4992
4993 w32_charset = Fcar (Fcdr (this_entry));
4994
d84b082d 4995 /* Translate Lisp symbol to number. */
767b1ff0
JR
4996 if (w32_charset == Qw32_charset_ansi)
4997 return ANSI_CHARSET;
4998 if (w32_charset == Qw32_charset_symbol)
4999 return SYMBOL_CHARSET;
5000 if (w32_charset == Qw32_charset_shiftjis)
5001 return SHIFTJIS_CHARSET;
5002 if (w32_charset == Qw32_charset_hangeul)
5003 return HANGEUL_CHARSET;
5004 if (w32_charset == Qw32_charset_chinesebig5)
5005 return CHINESEBIG5_CHARSET;
5006 if (w32_charset == Qw32_charset_gb2312)
5007 return GB2312_CHARSET;
5008 if (w32_charset == Qw32_charset_oem)
5009 return OEM_CHARSET;
dfff8a69 5010#ifdef JOHAB_CHARSET
767b1ff0
JR
5011 if (w32_charset == Qw32_charset_johab)
5012 return JOHAB_CHARSET;
5013 if (w32_charset == Qw32_charset_easteurope)
5014 return EASTEUROPE_CHARSET;
5015 if (w32_charset == Qw32_charset_turkish)
5016 return TURKISH_CHARSET;
5017 if (w32_charset == Qw32_charset_baltic)
5018 return BALTIC_CHARSET;
5019 if (w32_charset == Qw32_charset_russian)
5020 return RUSSIAN_CHARSET;
5021 if (w32_charset == Qw32_charset_arabic)
5022 return ARABIC_CHARSET;
5023 if (w32_charset == Qw32_charset_greek)
5024 return GREEK_CHARSET;
5025 if (w32_charset == Qw32_charset_hebrew)
5026 return HEBREW_CHARSET;
5027 if (w32_charset == Qw32_charset_vietnamese)
5028 return VIETNAMESE_CHARSET;
5029 if (w32_charset == Qw32_charset_thai)
5030 return THAI_CHARSET;
5031 if (w32_charset == Qw32_charset_mac)
5032 return MAC_CHARSET;
dfff8a69 5033#endif /* JOHAB_CHARSET */
5ac45f98 5034#ifdef UNICODE_CHARSET
767b1ff0
JR
5035 if (w32_charset == Qw32_charset_unicode)
5036 return UNICODE_CHARSET;
5ac45f98 5037#endif
dfff8a69
JR
5038
5039 return DEFAULT_CHARSET;
5ac45f98
GV
5040}
5041
dfff8a69 5042
8edb0a6f 5043static char *
fbd6baed 5044w32_to_x_charset (fncharset)
5ac45f98
GV
5045 int fncharset;
5046{
5e905a57 5047 static char buf[32];
767b1ff0 5048 Lisp_Object charset_type;
1edf84e7 5049
5ac45f98
GV
5050 switch (fncharset)
5051 {
767b1ff0
JR
5052 case ANSI_CHARSET:
5053 /* Handle startup case of w32-charset-info-alist not
5054 being set up yet. */
5055 if (NILP(Vw32_charset_info_alist))
5056 return "iso8859-1";
5057 charset_type = Qw32_charset_ansi;
5058 break;
5059 case DEFAULT_CHARSET:
5060 charset_type = Qw32_charset_default;
5061 break;
5062 case SYMBOL_CHARSET:
5063 charset_type = Qw32_charset_symbol;
5064 break;
5065 case SHIFTJIS_CHARSET:
5066 charset_type = Qw32_charset_shiftjis;
5067 break;
5068 case HANGEUL_CHARSET:
5069 charset_type = Qw32_charset_hangeul;
5070 break;
5071 case GB2312_CHARSET:
5072 charset_type = Qw32_charset_gb2312;
5073 break;
5074 case CHINESEBIG5_CHARSET:
5075 charset_type = Qw32_charset_chinesebig5;
5076 break;
5077 case OEM_CHARSET:
5078 charset_type = Qw32_charset_oem;
5079 break;
4587b026
GV
5080
5081 /* More recent versions of Windows (95 and NT4.0) define more
5082 character sets. */
5083#ifdef EASTEUROPE_CHARSET
767b1ff0
JR
5084 case EASTEUROPE_CHARSET:
5085 charset_type = Qw32_charset_easteurope;
5086 break;
5087 case TURKISH_CHARSET:
5088 charset_type = Qw32_charset_turkish;
5089 break;
5090 case BALTIC_CHARSET:
5091 charset_type = Qw32_charset_baltic;
5092 break;
33d52f9c 5093 case RUSSIAN_CHARSET:
767b1ff0
JR
5094 charset_type = Qw32_charset_russian;
5095 break;
5096 case ARABIC_CHARSET:
5097 charset_type = Qw32_charset_arabic;
5098 break;
5099 case GREEK_CHARSET:
5100 charset_type = Qw32_charset_greek;
5101 break;
5102 case HEBREW_CHARSET:
5103 charset_type = Qw32_charset_hebrew;
5104 break;
5105 case VIETNAMESE_CHARSET:
5106 charset_type = Qw32_charset_vietnamese;
5107 break;
5108 case THAI_CHARSET:
5109 charset_type = Qw32_charset_thai;
5110 break;
5111 case MAC_CHARSET:
5112 charset_type = Qw32_charset_mac;
5113 break;
5114 case JOHAB_CHARSET:
5115 charset_type = Qw32_charset_johab;
5116 break;
4587b026
GV
5117#endif
5118
5ac45f98 5119#ifdef UNICODE_CHARSET
767b1ff0
JR
5120 case UNICODE_CHARSET:
5121 charset_type = Qw32_charset_unicode;
5122 break;
5ac45f98 5123#endif
767b1ff0
JR
5124 default:
5125 /* Encode numerical value of unknown charset. */
5126 sprintf (buf, "*-#%u", fncharset);
5127 return buf;
5ac45f98 5128 }
7d0393cf 5129
767b1ff0
JR
5130 {
5131 Lisp_Object rest;
5132 char * best_match = NULL;
5133
5134 /* Look through w32-charset-info-alist for the character set.
5135 Prefer ISO codepages, and prefer lower numbers in the ISO
5136 range. Only return charsets for codepages which are installed.
5137
5138 Format of each entry is
5139 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
5140 */
5141 for (rest = Vw32_charset_info_alist; CONSP (rest); rest = XCDR (rest))
5142 {
5143 char * x_charset;
5144 Lisp_Object w32_charset;
5145 Lisp_Object codepage;
5146
5147 Lisp_Object this_entry = XCAR (rest);
5148
5149 /* Skip invalid entries in alist. */
5150 if (!CONSP (this_entry) || !STRINGP (XCAR (this_entry))
5151 || !CONSP (XCDR (this_entry))
5152 || !SYMBOLP (XCAR (XCDR (this_entry))))
5153 continue;
5154
d5db4077 5155 x_charset = SDATA (XCAR (this_entry));
767b1ff0
JR
5156 w32_charset = XCAR (XCDR (this_entry));
5157 codepage = XCDR (XCDR (this_entry));
5158
5159 /* Look for Same charset and a valid codepage (or non-int
5160 which means ignore). */
5161 if (w32_charset == charset_type
5162 && (!INTEGERP (codepage) || codepage == CP_DEFAULT
5163 || IsValidCodePage (XINT (codepage))))
5164 {
5165 /* If we don't have a match already, then this is the
5166 best. */
5167 if (!best_match)
5168 best_match = x_charset;
5169 /* If this is an ISO codepage, and the best so far isn't,
5170 then this is better. */
d84b082d
JR
5171 else if (strnicmp (best_match, "iso", 3) != 0
5172 && strnicmp (x_charset, "iso", 3) == 0)
767b1ff0
JR
5173 best_match = x_charset;
5174 /* If both are ISO8859 codepages, choose the one with the
5175 lowest number in the encoding field. */
d84b082d
JR
5176 else if (strnicmp (best_match, "iso8859-", 8) == 0
5177 && strnicmp (x_charset, "iso8859-", 8) == 0)
767b1ff0
JR
5178 {
5179 int best_enc = atoi (best_match + 8);
5180 int this_enc = atoi (x_charset + 8);
5181 if (this_enc > 0 && this_enc < best_enc)
5182 best_match = x_charset;
7d0393cf 5183 }
767b1ff0
JR
5184 }
5185 }
5186
5187 /* If no match, encode the numeric value. */
5188 if (!best_match)
5189 {
5190 sprintf (buf, "*-#%u", fncharset);
5191 return buf;
5192 }
5193
5e905a57
JR
5194 strncpy(buf, best_match, 31);
5195 buf[31] = '\0';
767b1ff0
JR
5196 return buf;
5197 }
ee78dc32
GV
5198}
5199
dfff8a69 5200
d84b082d
JR
5201/* Return all the X charsets that map to a font. */
5202static Lisp_Object
5203w32_to_all_x_charsets (fncharset)
5204 int fncharset;
5205{
5206 static char buf[32];
5207 Lisp_Object charset_type;
5208 Lisp_Object retval = Qnil;
5209
5210 switch (fncharset)
5211 {
5212 case ANSI_CHARSET:
5213 /* Handle startup case of w32-charset-info-alist not
5214 being set up yet. */
5215 if (NILP(Vw32_charset_info_alist))
d86c35ee
JR
5216 return Fcons (build_string ("iso8859-1"), Qnil);
5217
d84b082d
JR
5218 charset_type = Qw32_charset_ansi;
5219 break;
5220 case DEFAULT_CHARSET:
5221 charset_type = Qw32_charset_default;
5222 break;
5223 case SYMBOL_CHARSET:
5224 charset_type = Qw32_charset_symbol;
5225 break;
5226 case SHIFTJIS_CHARSET:
5227 charset_type = Qw32_charset_shiftjis;
5228 break;
5229 case HANGEUL_CHARSET:
5230 charset_type = Qw32_charset_hangeul;
5231 break;
5232 case GB2312_CHARSET:
5233 charset_type = Qw32_charset_gb2312;
5234 break;
5235 case CHINESEBIG5_CHARSET:
5236 charset_type = Qw32_charset_chinesebig5;
5237 break;
5238 case OEM_CHARSET:
5239 charset_type = Qw32_charset_oem;
5240 break;
5241
5242 /* More recent versions of Windows (95 and NT4.0) define more
5243 character sets. */
5244#ifdef EASTEUROPE_CHARSET
5245 case EASTEUROPE_CHARSET:
5246 charset_type = Qw32_charset_easteurope;
5247 break;
5248 case TURKISH_CHARSET:
5249 charset_type = Qw32_charset_turkish;
5250 break;
5251 case BALTIC_CHARSET:
5252 charset_type = Qw32_charset_baltic;
5253 break;
5254 case RUSSIAN_CHARSET:
5255 charset_type = Qw32_charset_russian;
5256 break;
5257 case ARABIC_CHARSET:
5258 charset_type = Qw32_charset_arabic;
5259 break;
5260 case GREEK_CHARSET:
5261 charset_type = Qw32_charset_greek;
5262 break;
5263 case HEBREW_CHARSET:
5264 charset_type = Qw32_charset_hebrew;
5265 break;
5266 case VIETNAMESE_CHARSET:
5267 charset_type = Qw32_charset_vietnamese;
5268 break;
5269 case THAI_CHARSET:
5270 charset_type = Qw32_charset_thai;
5271 break;
5272 case MAC_CHARSET:
5273 charset_type = Qw32_charset_mac;
5274 break;
5275 case JOHAB_CHARSET:
5276 charset_type = Qw32_charset_johab;
5277 break;
5278#endif
5279
5280#ifdef UNICODE_CHARSET
5281 case UNICODE_CHARSET:
5282 charset_type = Qw32_charset_unicode;
5283 break;
5284#endif
5285 default:
5286 /* Encode numerical value of unknown charset. */
5287 sprintf (buf, "*-#%u", fncharset);
5288 return Fcons (build_string (buf), Qnil);
5289 }
7d0393cf 5290
d84b082d
JR
5291 {
5292 Lisp_Object rest;
5293 /* Look through w32-charset-info-alist for the character set.
5294 Only return charsets for codepages which are installed.
5295
5296 Format of each entry in Vw32_charset_info_alist is
5297 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
5298 */
5299 for (rest = Vw32_charset_info_alist; CONSP (rest); rest = XCDR (rest))
5300 {
5301 Lisp_Object x_charset;
5302 Lisp_Object w32_charset;
5303 Lisp_Object codepage;
5304
5305 Lisp_Object this_entry = XCAR (rest);
5306
5307 /* Skip invalid entries in alist. */
5308 if (!CONSP (this_entry) || !STRINGP (XCAR (this_entry))
5309 || !CONSP (XCDR (this_entry))
5310 || !SYMBOLP (XCAR (XCDR (this_entry))))
5311 continue;
5312
5313 x_charset = XCAR (this_entry);
5314 w32_charset = XCAR (XCDR (this_entry));
5315 codepage = XCDR (XCDR (this_entry));
5316
5317 /* Look for Same charset and a valid codepage (or non-int
5318 which means ignore). */
5319 if (w32_charset == charset_type
5320 && (!INTEGERP (codepage) || codepage == CP_DEFAULT
5321 || IsValidCodePage (XINT (codepage))))
5322 {
5323 retval = Fcons (x_charset, retval);
5324 }
5325 }
5326
5327 /* If no match, encode the numeric value. */
5328 if (NILP (retval))
5329 {
5330 sprintf (buf, "*-#%u", fncharset);
5331 return Fcons (build_string (buf), Qnil);
5332 }
5333
5334 return retval;
5335 }
5336}
5337
dfff8a69
JR
5338/* Get the Windows codepage corresponding to the specified font. The
5339 charset info in the font name is used to look up
5340 w32-charset-to-codepage-alist. */
7d0393cf 5341int
dfff8a69
JR
5342w32_codepage_for_font (char *fontname)
5343{
767b1ff0
JR
5344 Lisp_Object codepage, entry;
5345 char *charset_str, *charset, *end;
dfff8a69 5346
767b1ff0 5347 if (NILP (Vw32_charset_info_alist))
dfff8a69
JR
5348 return CP_DEFAULT;
5349
767b1ff0
JR
5350 /* Extract charset part of font string. */
5351 charset = xlfd_charset_of_font (fontname);
5352
5353 if (!charset)
ceb12877 5354 return CP_UNKNOWN;
767b1ff0 5355
8b77111c 5356 charset_str = (char *) alloca (strlen (charset) + 1);
767b1ff0
JR
5357 strcpy (charset_str, charset);
5358
8b77111c 5359#if 0
dfff8a69
JR
5360 /* Remove leading "*-". */
5361 if (strncmp ("*-", charset_str, 2) == 0)
5362 charset = charset_str + 2;
5363 else
8b77111c 5364#endif
dfff8a69
JR
5365 charset = charset_str;
5366
5367 /* Stop match at wildcard (including preceding '-'). */
5368 if (end = strchr (charset, '*'))
5369 {
5370 if (end > charset && *(end-1) == '-')
5371 end--;
5372 *end = '\0';
5373 }
5374
767b1ff0
JR
5375 entry = Fassoc (build_string(charset), Vw32_charset_info_alist);
5376 if (NILP (entry))
ceb12877 5377 return CP_UNKNOWN;
767b1ff0
JR
5378
5379 codepage = Fcdr (Fcdr (entry));
5380
5381 if (NILP (codepage))
5382 return CP_8BIT;
5383 else if (XFASTINT (codepage) == XFASTINT (Qt))
5384 return CP_UNICODE;
5385 else if (INTEGERP (codepage))
dfff8a69
JR
5386 return XINT (codepage);
5387 else
ceb12877 5388 return CP_UNKNOWN;
dfff8a69
JR
5389}
5390
5391
7d0393cf 5392static BOOL
767b1ff0 5393w32_to_x_font (lplogfont, lpxstr, len, specific_charset)
ee78dc32
GV
5394 LOGFONT * lplogfont;
5395 char * lpxstr;
5396 int len;
767b1ff0 5397 char * specific_charset;
ee78dc32 5398{
6fc2811b 5399 char* fonttype;
f46e6225 5400 char *fontname;
3cb20f4a
RS
5401 char height_pixels[8];
5402 char height_dpi[8];
5403 char width_pixels[8];
4587b026 5404 char *fontname_dash;
ac849ba4
JR
5405 int display_resy = (int) one_w32_display_info.resy;
5406 int display_resx = (int) one_w32_display_info.resx;
f46e6225
GV
5407 int bufsz;
5408 struct coding_system coding;
3cb20f4a
RS
5409
5410 if (!lpxstr) abort ();
ee78dc32 5411
3cb20f4a
RS
5412 if (!lplogfont)
5413 return FALSE;
5414
6fc2811b
JR
5415 if (lplogfont->lfOutPrecision == OUT_STRING_PRECIS)
5416 fonttype = "raster";
5417 else if (lplogfont->lfOutPrecision == OUT_STROKE_PRECIS)
5418 fonttype = "outline";
5419 else
5420 fonttype = "unknown";
5421
1fa3a200 5422 setup_coding_system (Fcheck_coding_system (Vlocale_coding_system),
f46e6225 5423 &coding);
aab5ac44
KH
5424 coding.src_multibyte = 0;
5425 coding.dst_multibyte = 1;
f46e6225 5426 coding.mode |= CODING_MODE_LAST_BLOCK;
65413122
KH
5427 /* We explicitely disable composition handling because selection
5428 data should not contain any composition sequence. */
5429 coding.composing = COMPOSITION_DISABLED;
f46e6225
GV
5430 bufsz = decoding_buffer_size (&coding, LF_FACESIZE);
5431
5432 fontname = alloca(sizeof(*fontname) * bufsz);
5433 decode_coding (&coding, lplogfont->lfFaceName, fontname,
5434 strlen(lplogfont->lfFaceName), bufsz - 1);
5435 *(fontname + coding.produced) = '\0';
4587b026
GV
5436
5437 /* Replace dashes with underscores so the dashes are not
f46e6225 5438 misinterpreted. */
4587b026
GV
5439 fontname_dash = fontname;
5440 while (fontname_dash = strchr (fontname_dash, '-'))
5441 *fontname_dash = '_';
5442
3cb20f4a 5443 if (lplogfont->lfHeight)
ee78dc32 5444 {
3cb20f4a
RS
5445 sprintf (height_pixels, "%u", abs (lplogfont->lfHeight));
5446 sprintf (height_dpi, "%u",
33d52f9c 5447 abs (lplogfont->lfHeight) * 720 / display_resy);
5ac45f98
GV
5448 }
5449 else
ee78dc32 5450 {
3cb20f4a
RS
5451 strcpy (height_pixels, "*");
5452 strcpy (height_dpi, "*");
ee78dc32 5453 }
3cb20f4a
RS
5454 if (lplogfont->lfWidth)
5455 sprintf (width_pixels, "%u", lplogfont->lfWidth * 10);
5456 else
5457 strcpy (width_pixels, "*");
5458
5459 _snprintf (lpxstr, len - 1,
6fc2811b
JR
5460 "-%s-%s-%s-%c-normal-normal-%s-%s-%d-%d-%c-%s-%s",
5461 fonttype, /* foundry */
4587b026
GV
5462 fontname, /* family */
5463 w32_to_x_weight (lplogfont->lfWeight), /* weight */
5464 lplogfont->lfItalic?'i':'r', /* slant */
5465 /* setwidth name */
5466 /* add style name */
5467 height_pixels, /* pixel size */
5468 height_dpi, /* point size */
33d52f9c
GV
5469 display_resx, /* resx */
5470 display_resy, /* resy */
4587b026
GV
5471 ((lplogfont->lfPitchAndFamily & 0x3) == VARIABLE_PITCH)
5472 ? 'p' : 'c', /* spacing */
5473 width_pixels, /* avg width */
767b1ff0 5474 specific_charset ? specific_charset
7d0393cf 5475 : w32_to_x_charset (lplogfont->lfCharSet)
767b1ff0 5476 /* charset registry and encoding */
3cb20f4a
RS
5477 );
5478
ee78dc32
GV
5479 lpxstr[len - 1] = 0; /* just to be sure */
5480 return (TRUE);
5481}
5482
7d0393cf 5483static BOOL
fbd6baed 5484x_to_w32_font (lpxstr, lplogfont)
ee78dc32
GV
5485 char * lpxstr;
5486 LOGFONT * lplogfont;
5487{
f46e6225
GV
5488 struct coding_system coding;
5489
ee78dc32 5490 if (!lplogfont) return (FALSE);
f46e6225 5491
ee78dc32 5492 memset (lplogfont, 0, sizeof (*lplogfont));
5ac45f98 5493
1a292d24 5494 /* Set default value for each field. */
771c47d5 5495#if 1
ee78dc32
GV
5496 lplogfont->lfOutPrecision = OUT_DEFAULT_PRECIS;
5497 lplogfont->lfClipPrecision = CLIP_DEFAULT_PRECIS;
5498 lplogfont->lfQuality = DEFAULT_QUALITY;
5ac45f98
GV
5499#else
5500 /* go for maximum quality */
5501 lplogfont->lfOutPrecision = OUT_STROKE_PRECIS;
5502 lplogfont->lfClipPrecision = CLIP_STROKE_PRECIS;
5503 lplogfont->lfQuality = PROOF_QUALITY;
5504#endif
5505
1a292d24
AI
5506 lplogfont->lfCharSet = DEFAULT_CHARSET;
5507 lplogfont->lfWeight = FW_DONTCARE;
5508 lplogfont->lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
5509
5ac45f98
GV
5510 if (!lpxstr)
5511 return FALSE;
5512
5513 /* Provide a simple escape mechanism for specifying Windows font names
5514 * directly -- if font spec does not beginning with '-', assume this
5515 * format:
5516 * "<font name>[:height in pixels[:width in pixels[:weight]]]"
5517 */
7d0393cf 5518
5ac45f98
GV
5519 if (*lpxstr == '-')
5520 {
33d52f9c
GV
5521 int fields, tem;
5522 char name[50], weight[20], slant, pitch, pixels[10], height[10],
8b77111c 5523 width[10], resy[10], remainder[50];
5ac45f98 5524 char * encoding;
ac849ba4 5525 int dpi = (int) one_w32_display_info.resy;
5ac45f98
GV
5526
5527 fields = sscanf (lpxstr,
8b77111c 5528 "-%*[^-]-%49[^-]-%19[^-]-%c-%*[^-]-%*[^-]-%9[^-]-%9[^-]-%*[^-]-%9[^-]-%c-%9[^-]-%49s",
33d52f9c 5529 name, weight, &slant, pixels, height, resy, &pitch, width, remainder);
8b77111c
AI
5530 if (fields == EOF)
5531 return (FALSE);
5532
5533 /* In the general case when wildcards cover more than one field,
5534 we don't know which field is which, so don't fill any in.
5535 However, we need to cope with this particular form, which is
5536 generated by font_list_1 (invoked by try_font_list):
5537 "-raster-6x10-*-gb2312*-*"
5538 and make sure to correctly parse the charset field. */
5539 if (fields == 3)
5540 {
5541 fields = sscanf (lpxstr,
5542 "-%*[^-]-%49[^-]-*-%49s",
5543 name, remainder);
5544 }
5545 else if (fields < 9)
5546 {
5547 fields = 0;
5548 remainder[0] = 0;
5549 }
6fc2811b 5550
5ac45f98
GV
5551 if (fields > 0 && name[0] != '*')
5552 {
8ea3e054
RS
5553 int bufsize;
5554 unsigned char *buf;
5555
f46e6225 5556 setup_coding_system
1fa3a200 5557 (Fcheck_coding_system (Vlocale_coding_system), &coding);
aab5ac44
KH
5558 coding.src_multibyte = 1;
5559 coding.dst_multibyte = 1;
8ea3e054
RS
5560 bufsize = encoding_buffer_size (&coding, strlen (name));
5561 buf = (unsigned char *) alloca (bufsize);
f46e6225 5562 coding.mode |= CODING_MODE_LAST_BLOCK;
8ea3e054
RS
5563 encode_coding (&coding, name, buf, strlen (name), bufsize);
5564 if (coding.produced >= LF_FACESIZE)
5565 coding.produced = LF_FACESIZE - 1;
5566 buf[coding.produced] = 0;
5567 strcpy (lplogfont->lfFaceName, buf);
5ac45f98
GV
5568 }
5569 else
5570 {
6fc2811b 5571 lplogfont->lfFaceName[0] = '\0';
5ac45f98
GV
5572 }
5573
5574 fields--;
5575
fbd6baed 5576 lplogfont->lfWeight = x_to_w32_weight ((fields > 0 ? weight : ""));
5ac45f98
GV
5577
5578 fields--;
5579
c8874f14 5580 lplogfont->lfItalic = (fields > 0 && slant == 'i');
5ac45f98
GV
5581
5582 fields--;
5583
5584 if (fields > 0 && pixels[0] != '*')
5585 lplogfont->lfHeight = atoi (pixels);
5586
5587 fields--;
5ac45f98 5588 fields--;
33d52f9c
GV
5589 if (fields > 0 && resy[0] != '*')
5590 {
6fc2811b 5591 tem = atoi (resy);
33d52f9c
GV
5592 if (tem > 0) dpi = tem;
5593 }
5ac45f98 5594
33d52f9c
GV
5595 if (fields > -1 && lplogfont->lfHeight == 0 && height[0] != '*')
5596 lplogfont->lfHeight = atoi (height) * dpi / 720;
5597
5598 if (fields > 0)
5ac45f98
GV
5599 lplogfont->lfPitchAndFamily =
5600 (fields > 0 && pitch == 'p') ? VARIABLE_PITCH : FIXED_PITCH;
5601
5602 fields--;
5603
5604 if (fields > 0 && width[0] != '*')
5605 lplogfont->lfWidth = atoi (width) / 10;
5606
5607 fields--;
5608
4587b026 5609 /* Strip the trailing '-' if present. (it shouldn't be, as it
d88c567c 5610 fails the test against xlfd-tight-regexp in fontset.el). */
3c190163 5611 {
5ac45f98
GV
5612 int len = strlen (remainder);
5613 if (len > 0 && remainder[len-1] == '-')
5614 remainder[len-1] = 0;
ee78dc32 5615 }
5ac45f98 5616 encoding = remainder;
8b77111c 5617#if 0
5ac45f98
GV
5618 if (strncmp (encoding, "*-", 2) == 0)
5619 encoding += 2;
8b77111c
AI
5620#endif
5621 lplogfont->lfCharSet = x_to_w32_charset (encoding);
5ac45f98
GV
5622 }
5623 else
5624 {
5625 int fields;
5626 char name[100], height[10], width[10], weight[20];
a1a80b40 5627
5ac45f98
GV
5628 fields = sscanf (lpxstr,
5629 "%99[^:]:%9[^:]:%9[^:]:%19s",
5630 name, height, width, weight);
5631
5632 if (fields == EOF) return (FALSE);
5633
5634 if (fields > 0)
5635 {
5636 strncpy (lplogfont->lfFaceName,name, LF_FACESIZE);
5637 lplogfont->lfFaceName[LF_FACESIZE-1] = 0;
5638 }
5639 else
5640 {
5641 lplogfont->lfFaceName[0] = 0;
5642 }
5643
5644 fields--;
5645
5646 if (fields > 0)
5647 lplogfont->lfHeight = atoi (height);
5648
5649 fields--;
5650
5651 if (fields > 0)
5652 lplogfont->lfWidth = atoi (width);
5653
5654 fields--;
5655
fbd6baed 5656 lplogfont->lfWeight = x_to_w32_weight ((fields > 0 ? weight : ""));
5ac45f98
GV
5657 }
5658
5659 /* This makes TrueType fonts work better. */
5660 lplogfont->lfHeight = - abs (lplogfont->lfHeight);
6fc2811b 5661
ee78dc32
GV
5662 return (TRUE);
5663}
5664
d88c567c
JR
5665/* Strip the pixel height and point height from the given xlfd, and
5666 return the pixel height. If no pixel height is specified, calculate
5667 one from the point height, or if that isn't defined either, return
5668 0 (which usually signifies a scalable font).
5669*/
8edb0a6f
JR
5670static int
5671xlfd_strip_height (char *fontname)
d88c567c 5672{
8edb0a6f 5673 int pixel_height, field_number;
d88c567c
JR
5674 char *read_from, *write_to;
5675
5676 xassert (fontname);
5677
5678 pixel_height = field_number = 0;
5679 write_to = NULL;
5680
5681 /* Look for height fields. */
5682 for (read_from = fontname; *read_from; read_from++)
5683 {
5684 if (*read_from == '-')
5685 {
5686 field_number++;
5687 if (field_number == 7) /* Pixel height. */
5688 {
5689 read_from++;
5690 write_to = read_from;
5691
5692 /* Find end of field. */
5693 for (;*read_from && *read_from != '-'; read_from++)
5694 ;
5695
5696 /* Split the fontname at end of field. */
5697 if (*read_from)
5698 {
5699 *read_from = '\0';
5700 read_from++;
5701 }
5702 pixel_height = atoi (write_to);
5703 /* Blank out field. */
5704 if (read_from > write_to)
5705 {
5706 *write_to = '-';
5707 write_to++;
5708 }
767b1ff0 5709 /* If the pixel height field is at the end (partial xlfd),
d88c567c
JR
5710 return now. */
5711 else
5712 return pixel_height;
5713
5714 /* If we got a pixel height, the point height can be
5715 ignored. Just blank it out and break now. */
5716 if (pixel_height)
5717 {
5718 /* Find end of point size field. */
5719 for (; *read_from && *read_from != '-'; read_from++)
5720 ;
5721
5722 if (*read_from)
5723 read_from++;
5724
5725 /* Blank out the point size field. */
5726 if (read_from > write_to)
5727 {
5728 *write_to = '-';
5729 write_to++;
5730 }
5731 else
5732 return pixel_height;
5733
5734 break;
5735 }
5736 /* If the point height is already blank, break now. */
5737 if (*read_from == '-')
5738 {
5739 read_from++;
5740 break;
5741 }
5742 }
5743 else if (field_number == 8)
5744 {
5745 /* If we didn't get a pixel height, try to get the point
5746 height and convert that. */
5747 int point_size;
5748 char *point_size_start = read_from++;
5749
5750 /* Find end of field. */
5751 for (; *read_from && *read_from != '-'; read_from++)
5752 ;
5753
5754 if (*read_from)
5755 {
5756 *read_from = '\0';
5757 read_from++;
5758 }
5759
5760 point_size = atoi (point_size_start);
5761
5762 /* Convert to pixel height. */
5763 pixel_height = point_size
5764 * one_w32_display_info.height_in / 720;
5765
5766 /* Blank out this field and break. */
5767 *write_to = '-';
5768 write_to++;
5769 break;
5770 }
5771 }
5772 }
5773
5774 /* Shift the rest of the font spec into place. */
5775 if (write_to && read_from > write_to)
5776 {
5777 for (; *read_from; read_from++, write_to++)
5778 *write_to = *read_from;
5779 *write_to = '\0';
5780 }
5781
5782 return pixel_height;
5783}
5784
6fc2811b 5785/* Assume parameter 1 is fully qualified, no wildcards. */
7d0393cf 5786static BOOL
6fc2811b
JR
5787w32_font_match (fontname, pattern)
5788 char * fontname;
5789 char * pattern;
ee78dc32 5790{
e7c72122 5791 char *regex = alloca (strlen (pattern) * 2 + 3);
d88c567c 5792 char *font_name_copy = alloca (strlen (fontname) + 1);
6fc2811b 5793 char *ptr;
ee78dc32 5794
d88c567c
JR
5795 /* Copy fontname so we can modify it during comparison. */
5796 strcpy (font_name_copy, fontname);
5797
6fc2811b
JR
5798 ptr = regex;
5799 *ptr++ = '^';
ee78dc32 5800
6fc2811b
JR
5801 /* Turn pattern into a regexp and do a regexp match. */
5802 for (; *pattern; pattern++)
5803 {
5804 if (*pattern == '?')
5805 *ptr++ = '.';
5806 else if (*pattern == '*')
5807 {
5808 *ptr++ = '.';
5809 *ptr++ = '*';
5810 }
33d52f9c 5811 else
6fc2811b 5812 *ptr++ = *pattern;
ee78dc32 5813 }
6fc2811b
JR
5814 *ptr = '$';
5815 *(ptr + 1) = '\0';
5816
d88c567c
JR
5817 /* Strip out font heights and compare them seperately, since
5818 rounding error can cause mismatches. This also allows a
5819 comparison between a font that declares only a pixel height and a
5820 pattern that declares the point height.
5821 */
5822 {
5823 int font_height, pattern_height;
5824
5825 font_height = xlfd_strip_height (font_name_copy);
5826 pattern_height = xlfd_strip_height (regex);
5827
5828 /* Compare now, and don't bother doing expensive regexp matching
5829 if the heights differ. */
5830 if (font_height && pattern_height && (font_height != pattern_height))
5831 return FALSE;
5832 }
5833
6fc2811b 5834 return (fast_c_string_match_ignore_case (build_string (regex),
d88c567c 5835 font_name_copy) >= 0);
ee78dc32
GV
5836}
5837
5ca0cd71
GV
5838/* Callback functions, and a structure holding info they need, for
5839 listing system fonts on W32. We need one set of functions to do the
5840 job properly, but these don't work on NT 3.51 and earlier, so we
5841 have a second set which don't handle character sets properly to
5842 fall back on.
5843
5844 In both cases, there are two passes made. The first pass gets one
5845 font from each family, the second pass lists all the fonts from
5846 each family. */
5847
7d0393cf 5848typedef struct enumfont_t
ee78dc32
GV
5849{
5850 HDC hdc;
5851 int numFonts;
3cb20f4a 5852 LOGFONT logfont;
ee78dc32 5853 XFontStruct *size_ref;
23afac8f 5854 Lisp_Object pattern;
d84b082d 5855 Lisp_Object list;
ee78dc32
GV
5856} enumfont_t;
5857
d84b082d
JR
5858
5859static void
5860enum_font_maybe_add_to_list (enumfont_t *, LOGFONT *, char *, Lisp_Object);
5861
5862
7d0393cf 5863static int CALLBACK
ee78dc32
GV
5864enum_font_cb2 (lplf, lptm, FontType, lpef)
5865 ENUMLOGFONT * lplf;
5866 NEWTEXTMETRIC * lptm;
5867 int FontType;
5868 enumfont_t * lpef;
5869{
66895301
JR
5870 /* Ignore struck out and underlined versions of fonts. */
5871 if (lplf->elfLogFont.lfStrikeOut || lplf->elfLogFont.lfUnderline)
5872 return 1;
5873
5874 /* Only return fonts with names starting with @ if they were
5875 explicitly specified, since Microsoft uses an initial @ to
5876 denote fonts for vertical writing, without providing a more
5877 convenient way of identifying them. */
5878 if (lplf->elfLogFont.lfFaceName[0] == '@'
5879 && lpef->logfont.lfFaceName[0] != '@')
5e905a57
JR
5880 return 1;
5881
4587b026
GV
5882 /* Check that the character set matches if it was specified */
5883 if (lpef->logfont.lfCharSet != DEFAULT_CHARSET &&
5884 lplf->elfLogFont.lfCharSet != lpef->logfont.lfCharSet)
5e905a57 5885 return 1;
4587b026 5886
6358474d
JR
5887 if (FontType == RASTER_FONTTYPE)
5888 {
5889 /* DBCS raster fonts have problems displaying, so skip them. */
5890 int charset = lplf->elfLogFont.lfCharSet;
5891 if (charset == SHIFTJIS_CHARSET
5892 || charset == HANGEUL_CHARSET
5893 || charset == CHINESEBIG5_CHARSET
5894 || charset == GB2312_CHARSET
5895#ifdef JOHAB_CHARSET
5896 || charset == JOHAB_CHARSET
5897#endif
5898 )
5899 return 1;
5900 }
5901
ee78dc32
GV
5902 {
5903 char buf[100];
4587b026 5904 Lisp_Object width = Qnil;
d84b082d 5905 Lisp_Object charset_list = Qnil;
767b1ff0 5906 char *charset = NULL;
ee78dc32 5907
6fc2811b
JR
5908 /* Truetype fonts do not report their true metrics until loaded */
5909 if (FontType != RASTER_FONTTYPE)
3cb20f4a 5910 {
23afac8f 5911 if (!NILP (lpef->pattern))
6fc2811b
JR
5912 {
5913 /* Scalable fonts are as big as you want them to be. */
5914 lplf->elfLogFont.lfHeight = lpef->logfont.lfHeight;
5915 lplf->elfLogFont.lfWidth = lpef->logfont.lfWidth;
5916 width = make_number (lpef->logfont.lfWidth);
5917 }
5918 else
5919 {
5920 lplf->elfLogFont.lfHeight = 0;
5921 lplf->elfLogFont.lfWidth = 0;
5922 }
3cb20f4a 5923 }
6fc2811b 5924
f46e6225
GV
5925 /* Make sure the height used here is the same as everywhere
5926 else (ie character height, not cell height). */
6fc2811b
JR
5927 if (lplf->elfLogFont.lfHeight > 0)
5928 {
5929 /* lptm can be trusted for RASTER fonts, but not scalable ones. */
5930 if (FontType == RASTER_FONTTYPE)
5931 lplf->elfLogFont.lfHeight = lptm->tmInternalLeading - lptm->tmHeight;
5932 else
5933 lplf->elfLogFont.lfHeight = -lplf->elfLogFont.lfHeight;
5934 }
4587b026 5935
23afac8f 5936 if (!NILP (lpef->pattern))
767b1ff0 5937 {
d5db4077 5938 charset = xlfd_charset_of_font (SDATA (lpef->pattern));
767b1ff0 5939
644cefdf
JR
5940 /* We already checked charsets above, but DEFAULT_CHARSET
5941 slipped through. So only allow exact matches for DEFAULT_CHARSET. */
5942 if (charset
5943 && strncmp (charset, "*-*", 3) != 0
5944 && lpef->logfont.lfCharSet == DEFAULT_CHARSET
5945 && strcmp (charset, w32_to_x_charset (DEFAULT_CHARSET)) != 0)
5946 return 1;
767b1ff0
JR
5947 }
5948
d84b082d
JR
5949 if (charset)
5950 charset_list = Fcons (build_string (charset), Qnil);
5951 else
5952 charset_list = w32_to_all_x_charsets (lplf->elfLogFont.lfCharSet);
ee78dc32 5953
d84b082d
JR
5954 /* Loop through the charsets. */
5955 for ( ; CONSP (charset_list); charset_list = Fcdr (charset_list))
ee78dc32 5956 {
d84b082d 5957 Lisp_Object this_charset = Fcar (charset_list);
d5db4077 5958 charset = SDATA (this_charset);
d84b082d
JR
5959
5960 /* List bold and italic variations if w32-enable-synthesized-fonts
5961 is non-nil and this is a plain font. */
5962 if (w32_enable_synthesized_fonts
5963 && lplf->elfLogFont.lfWeight == FW_NORMAL
5964 && lplf->elfLogFont.lfItalic == FALSE)
5965 {
5966 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5967 charset, width);
5968 /* bold. */
5969 lplf->elfLogFont.lfWeight = FW_BOLD;
5970 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5971 charset, width);
5972 /* bold italic. */
5973 lplf->elfLogFont.lfItalic = TRUE;
5974 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5975 charset, width);
5976 /* italic. */
5977 lplf->elfLogFont.lfWeight = FW_NORMAL;
5978 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5979 charset, width);
5980 }
5981 else
5982 enum_font_maybe_add_to_list (lpef, &(lplf->elfLogFont),
5983 charset, width);
ee78dc32
GV
5984 }
5985 }
6fc2811b 5986
5e905a57 5987 return 1;
ee78dc32
GV
5988}
5989
d84b082d
JR
5990static void
5991enum_font_maybe_add_to_list (lpef, logfont, match_charset, width)
5992 enumfont_t * lpef;
5993 LOGFONT * logfont;
5994 char * match_charset;
5995 Lisp_Object width;
5996{
5997 char buf[100];
5998
5999 if (!w32_to_x_font (logfont, buf, 100, match_charset))
6000 return;
6001
23afac8f 6002 if (NILP (lpef->pattern)
d5db4077 6003 || w32_font_match (buf, SDATA (lpef->pattern)))
d84b082d
JR
6004 {
6005 /* Check if we already listed this font. This may happen if
6006 w32_enable_synthesized_fonts is non-nil, and there are real
6007 bold and italic versions of the font. */
6008 Lisp_Object font_name = build_string (buf);
6009 if (NILP (Fmember (font_name, lpef->list)))
6010 {
23afac8f
JR
6011 Lisp_Object entry = Fcons (font_name, width);
6012 lpef->list = Fcons (entry, lpef->list);
d84b082d
JR
6013 lpef->numFonts++;
6014 }
6015 }
6016}
6017
6018
7d0393cf 6019static int CALLBACK
ee78dc32
GV
6020enum_font_cb1 (lplf, lptm, FontType, lpef)
6021 ENUMLOGFONT * lplf;
6022 NEWTEXTMETRIC * lptm;
6023 int FontType;
6024 enumfont_t * lpef;
6025{
6026 return EnumFontFamilies (lpef->hdc,
6027 lplf->elfLogFont.lfFaceName,
6028 (FONTENUMPROC) enum_font_cb2,
6029 (LPARAM) lpef);
6030}
6031
6032
8edb0a6f 6033static int CALLBACK
5ca0cd71
GV
6034enum_fontex_cb2 (lplf, lptm, font_type, lpef)
6035 ENUMLOGFONTEX * lplf;
6036 NEWTEXTMETRICEX * lptm;
6037 int font_type;
6038 enumfont_t * lpef;
6039{
6040 /* We are not interested in the extra info we get back from the 'Ex
6041 version - only the fact that we get character set variations
6042 enumerated seperately. */
6043 return enum_font_cb2 ((ENUMLOGFONT *) lplf, (NEWTEXTMETRIC *) lptm,
6044 font_type, lpef);
6045}
6046
8edb0a6f 6047static int CALLBACK
5ca0cd71
GV
6048enum_fontex_cb1 (lplf, lptm, font_type, lpef)
6049 ENUMLOGFONTEX * lplf;
6050 NEWTEXTMETRICEX * lptm;
6051 int font_type;
6052 enumfont_t * lpef;
6053{
6054 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
6055 FARPROC enum_font_families_ex
6056 = GetProcAddress ( gdi32, "EnumFontFamiliesExA");
6057 /* We don't really expect EnumFontFamiliesEx to disappear once we
6058 get here, so don't bother handling it gracefully. */
6059 if (enum_font_families_ex == NULL)
6060 error ("gdi32.dll has disappeared!");
6061 return enum_font_families_ex (lpef->hdc,
6062 &lplf->elfLogFont,
6063 (FONTENUMPROC) enum_fontex_cb2,
6064 (LPARAM) lpef, 0);
6065}
6066
4587b026
GV
6067/* Interface to fontset handler. (adapted from mw32font.c in Meadow
6068 and xterm.c in Emacs 20.3) */
6069
8edb0a6f 6070static Lisp_Object w32_list_bdf_fonts (Lisp_Object pattern, int max_names)
33d52f9c
GV
6071{
6072 char *fontname, *ptnstr;
6073 Lisp_Object list, tem, newlist = Qnil;
55dcfc15 6074 int n_fonts = 0;
33d52f9c
GV
6075
6076 list = Vw32_bdf_filename_alist;
d5db4077 6077 ptnstr = SDATA (pattern);
33d52f9c 6078
8e713be6 6079 for ( ; CONSP (list); list = XCDR (list))
33d52f9c 6080 {
8e713be6 6081 tem = XCAR (list);
33d52f9c 6082 if (CONSP (tem))
d5db4077 6083 fontname = SDATA (XCAR (tem));
33d52f9c 6084 else if (STRINGP (tem))
d5db4077 6085 fontname = SDATA (tem);
33d52f9c
GV
6086 else
6087 continue;
6088
6089 if (w32_font_match (fontname, ptnstr))
5ca0cd71 6090 {
8e713be6 6091 newlist = Fcons (XCAR (tem), newlist);
5ca0cd71 6092 n_fonts++;
bd11cc09 6093 if (max_names >= 0 && n_fonts >= max_names)
5ca0cd71
GV
6094 break;
6095 }
33d52f9c
GV
6096 }
6097
6098 return newlist;
6099}
6100
5ca0cd71 6101
4587b026
GV
6102/* Return a list of names of available fonts matching PATTERN on frame
6103 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
6104 to be listed. Frame F NULL means we have not yet created any
6105 frame, which means we can't get proper size info, as we don't have
6106 a device context to use for GetTextMetrics.
bd11cc09
JR
6107 MAXNAMES sets a limit on how many fonts to match. If MAXNAMES is
6108 negative, then all matching fonts are returned. */
4587b026
GV
6109
6110Lisp_Object
dc220243
JR
6111w32_list_fonts (f, pattern, size, maxnames)
6112 struct frame *f;
6113 Lisp_Object pattern;
6114 int size;
6115 int maxnames;
4587b026 6116{
6fc2811b 6117 Lisp_Object patterns, key = Qnil, tem, tpat;
4587b026 6118 Lisp_Object list = Qnil, newlist = Qnil, second_best = Qnil;
33d52f9c 6119 struct w32_display_info *dpyinfo = &one_w32_display_info;
5ca0cd71 6120 int n_fonts = 0;
396594fe 6121
4587b026
GV
6122 patterns = Fassoc (pattern, Valternate_fontname_alist);
6123 if (NILP (patterns))
6124 patterns = Fcons (pattern, Qnil);
6125
8e713be6 6126 for (; CONSP (patterns); patterns = XCDR (patterns))
4587b026
GV
6127 {
6128 enumfont_t ef;
767b1ff0 6129 int codepage;
4587b026 6130
8e713be6 6131 tpat = XCAR (patterns);
4587b026 6132
767b1ff0
JR
6133 if (!STRINGP (tpat))
6134 continue;
6135
6136 /* Avoid expensive EnumFontFamilies functions if we are not
6137 going to be able to output one of these anyway. */
d5db4077 6138 codepage = w32_codepage_for_font (SDATA (tpat));
767b1ff0 6139 if (codepage != CP_8BIT && codepage != CP_UNICODE
ceb12877
AI
6140 && codepage != CP_DEFAULT && codepage != CP_UNKNOWN
6141 && !IsValidCodePage(codepage))
767b1ff0
JR
6142 continue;
6143
4587b026
GV
6144 /* See if we cached the result for this particular query.
6145 The cache is an alist of the form:
6146 ((PATTERN (FONTNAME . WIDTH) ...) ...)
6147 */
8e713be6 6148 if (tem = XCDR (dpyinfo->name_list_element),
33d52f9c 6149 !NILP (list = Fassoc (tpat, tem)))
4587b026
GV
6150 {
6151 list = Fcdr_safe (list);
6152 /* We have a cached list. Don't have to get the list again. */
6153 goto label_cached;
6154 }
6155
6156 BLOCK_INPUT;
6157 /* At first, put PATTERN in the cache. */
23afac8f
JR
6158 ef.pattern = tpat;
6159 ef.list = Qnil;
4587b026 6160 ef.numFonts = 0;
33d52f9c 6161
5ca0cd71
GV
6162 /* Use EnumFontFamiliesEx where it is available, as it knows
6163 about character sets. Fall back to EnumFontFamilies for
6164 older versions of NT that don't support the 'Ex function. */
d5db4077 6165 x_to_w32_font (SDATA (tpat), &ef.logfont);
4587b026 6166 {
5ca0cd71
GV
6167 LOGFONT font_match_pattern;
6168 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
6169 FARPROC enum_font_families_ex
6170 = GetProcAddress ( gdi32, "EnumFontFamiliesExA");
6171
6172 /* We do our own pattern matching so we can handle wildcards. */
6173 font_match_pattern.lfFaceName[0] = 0;
6174 font_match_pattern.lfPitchAndFamily = 0;
6175 /* We can use the charset, because if it is a wildcard it will
6176 be DEFAULT_CHARSET anyway. */
6177 font_match_pattern.lfCharSet = ef.logfont.lfCharSet;
6178
33d52f9c 6179 ef.hdc = GetDC (dpyinfo->root_window);
4587b026 6180
5ca0cd71
GV
6181 if (enum_font_families_ex)
6182 enum_font_families_ex (ef.hdc,
6183 &font_match_pattern,
6184 (FONTENUMPROC) enum_fontex_cb1,
6185 (LPARAM) &ef, 0);
6186 else
6187 EnumFontFamilies (ef.hdc, NULL, (FONTENUMPROC) enum_font_cb1,
6188 (LPARAM)&ef);
4587b026 6189
33d52f9c 6190 ReleaseDC (dpyinfo->root_window, ef.hdc);
4587b026
GV
6191 }
6192
6193 UNBLOCK_INPUT;
23afac8f 6194 list = ef.list;
4587b026
GV
6195
6196 /* Make a list of the fonts we got back.
6197 Store that in the font cache for the display. */
f3fbd155
KR
6198 XSETCDR (dpyinfo->name_list_element,
6199 Fcons (Fcons (tpat, list),
6200 XCDR (dpyinfo->name_list_element)));
4587b026
GV
6201
6202 label_cached:
6203 if (NILP (list)) continue; /* Try the remaining alternatives. */
6204
6205 newlist = second_best = Qnil;
6206
7d0393cf 6207 /* Make a list of the fonts that have the right width. */
8e713be6 6208 for (; CONSP (list); list = XCDR (list))
4587b026
GV
6209 {
6210 int found_size;
8e713be6 6211 tem = XCAR (list);
4587b026
GV
6212
6213 if (!CONSP (tem))
6214 continue;
8e713be6 6215 if (NILP (XCAR (tem)))
4587b026
GV
6216 continue;
6217 if (!size)
6218 {
8e713be6 6219 newlist = Fcons (XCAR (tem), newlist);
5ca0cd71 6220 n_fonts++;
bd11cc09 6221 if (maxnames >= 0 && n_fonts >= maxnames)
5ca0cd71
GV
6222 break;
6223 else
6224 continue;
4587b026 6225 }
8e713be6 6226 if (!INTEGERP (XCDR (tem)))
4587b026
GV
6227 {
6228 /* Since we don't yet know the size of the font, we must
6229 load it and try GetTextMetrics. */
4587b026
GV
6230 W32FontStruct thisinfo;
6231 LOGFONT lf;
6232 HDC hdc;
6233 HANDLE oldobj;
6234
d5db4077 6235 if (!x_to_w32_font (SDATA (XCAR (tem)), &lf))
4587b026
GV
6236 continue;
6237
6238 BLOCK_INPUT;
33d52f9c 6239 thisinfo.bdf = NULL;
4587b026
GV
6240 thisinfo.hfont = CreateFontIndirect (&lf);
6241 if (thisinfo.hfont == NULL)
6242 continue;
6243
6244 hdc = GetDC (dpyinfo->root_window);
6245 oldobj = SelectObject (hdc, thisinfo.hfont);
6246 if (GetTextMetrics (hdc, &thisinfo.tm))
f3fbd155 6247 XSETCDR (tem, make_number (FONT_WIDTH (&thisinfo)));
4587b026 6248 else
f3fbd155 6249 XSETCDR (tem, make_number (0));
4587b026
GV
6250 SelectObject (hdc, oldobj);
6251 ReleaseDC (dpyinfo->root_window, hdc);
6252 DeleteObject(thisinfo.hfont);
6253 UNBLOCK_INPUT;
6254 }
8e713be6 6255 found_size = XINT (XCDR (tem));
4587b026 6256 if (found_size == size)
5ca0cd71 6257 {
8e713be6 6258 newlist = Fcons (XCAR (tem), newlist);
5ca0cd71 6259 n_fonts++;
bd11cc09 6260 if (maxnames >= 0 && n_fonts >= maxnames)
5ca0cd71
GV
6261 break;
6262 }
4587b026
GV
6263 /* keep track of the closest matching size in case
6264 no exact match is found. */
6265 else if (found_size > 0)
6266 {
6267 if (NILP (second_best))
6268 second_best = tem;
7d0393cf 6269
4587b026
GV
6270 else if (found_size < size)
6271 {
8e713be6
KR
6272 if (XINT (XCDR (second_best)) > size
6273 || XINT (XCDR (second_best)) < found_size)
4587b026
GV
6274 second_best = tem;
6275 }
6276 else
6277 {
8e713be6
KR
6278 if (XINT (XCDR (second_best)) > size
6279 && XINT (XCDR (second_best)) >
4587b026
GV
6280 found_size)
6281 second_best = tem;
6282 }
6283 }
6284 }
6285
6286 if (!NILP (newlist))
6287 break;
6288 else if (!NILP (second_best))
6289 {
8e713be6 6290 newlist = Fcons (XCAR (second_best), Qnil);
4587b026
GV
6291 break;
6292 }
6293 }
6294
33d52f9c 6295 /* Include any bdf fonts. */
bd11cc09 6296 if (n_fonts < maxnames || maxnames < 0)
33d52f9c
GV
6297 {
6298 Lisp_Object combined[2];
5ca0cd71 6299 combined[0] = w32_list_bdf_fonts (pattern, maxnames - n_fonts);
33d52f9c
GV
6300 combined[1] = newlist;
6301 newlist = Fnconc(2, combined);
6302 }
6303
4587b026
GV
6304 return newlist;
6305}
6306
5ca0cd71 6307
4587b026
GV
6308/* Return a pointer to struct font_info of font FONT_IDX of frame F. */
6309struct font_info *
6310w32_get_font_info (f, font_idx)
6311 FRAME_PTR f;
6312 int font_idx;
6313{
6314 return (FRAME_W32_FONT_TABLE (f) + font_idx);
6315}
6316
6317
6318struct font_info*
6319w32_query_font (struct frame *f, char *fontname)
6320{
6321 int i;
6322 struct font_info *pfi;
6323
6324 pfi = FRAME_W32_FONT_TABLE (f);
6325
6326 for (i = 0; i < one_w32_display_info.n_fonts ;i++, pfi++)
6327 {
6328 if (strcmp(pfi->name, fontname) == 0) return pfi;
6329 }
6330
6331 return NULL;
6332}
6333
6334/* Find a CCL program for a font specified by FONTP, and set the member
6335 `encoder' of the structure. */
6336
6337void
6338w32_find_ccl_program (fontp)
6339 struct font_info *fontp;
6340{
3545439c 6341 Lisp_Object list, elt;
4587b026 6342
8e713be6 6343 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
4587b026 6344 {
8e713be6 6345 elt = XCAR (list);
4587b026 6346 if (CONSP (elt)
8e713be6
KR
6347 && STRINGP (XCAR (elt))
6348 && (fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
4587b026 6349 >= 0))
3545439c
KH
6350 break;
6351 }
6352 if (! NILP (list))
6353 {
17eedd00
KH
6354 struct ccl_program *ccl
6355 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
3545439c 6356
8e713be6 6357 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
3545439c
KH
6358 xfree (ccl);
6359 else
6360 fontp->font_encoder = ccl;
4587b026
GV
6361 }
6362}
6363
6364\f
8edb0a6f
JR
6365/* Find BDF files in a specified directory. (use GCPRO when calling,
6366 as this calls lisp to get a directory listing). */
6367static Lisp_Object
6368w32_find_bdf_fonts_in_dir (Lisp_Object directory)
6369{
6370 Lisp_Object filelist, list = Qnil;
6371 char fontname[100];
6372
6373 if (!STRINGP(directory))
6374 return Qnil;
6375
6376 filelist = Fdirectory_files (directory, Qt,
6377 build_string (".*\\.[bB][dD][fF]"), Qt);
6378
6379 for ( ; CONSP(filelist); filelist = XCDR (filelist))
6380 {
6381 Lisp_Object filename = XCAR (filelist);
d5db4077 6382 if (w32_BDF_to_x_font (SDATA (filename), fontname, 100))
8edb0a6f
JR
6383 store_in_alist (&list, build_string (fontname), filename);
6384 }
6385 return list;
6386}
6387
6fc2811b
JR
6388DEFUN ("w32-find-bdf-fonts", Fw32_find_bdf_fonts, Sw32_find_bdf_fonts,
6389 1, 1, 0,
b3700ae7
JR
6390 doc: /* Return a list of BDF fonts in DIR.
6391The list is suitable for appending to w32-bdf-filename-alist. Fonts
6392which do not contain an xlfd description will not be included in the
6393list. DIR may be a list of directories. */)
6fc2811b
JR
6394 (directory)
6395 Lisp_Object directory;
6396{
6397 Lisp_Object list = Qnil;
6398 struct gcpro gcpro1, gcpro2;
ee78dc32 6399
6fc2811b
JR
6400 if (!CONSP (directory))
6401 return w32_find_bdf_fonts_in_dir (directory);
ee78dc32 6402
6fc2811b 6403 for ( ; CONSP (directory); directory = XCDR (directory))
ee78dc32 6404 {
6fc2811b
JR
6405 Lisp_Object pair[2];
6406 pair[0] = list;
6407 pair[1] = Qnil;
6408 GCPRO2 (directory, list);
6409 pair[1] = w32_find_bdf_fonts_in_dir( XCAR (directory) );
6410 list = Fnconc( 2, pair );
6411 UNGCPRO;
6412 }
6413 return list;
6414}
ee78dc32 6415
6fc2811b
JR
6416\f
6417DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
74e1aeec 6418 doc: /* Internal function called by `color-defined-p', which see. */)
6fc2811b
JR
6419 (color, frame)
6420 Lisp_Object color, frame;
6421{
6422 XColor foo;
6423 FRAME_PTR f = check_x_frame (frame);
ee78dc32 6424
b7826503 6425 CHECK_STRING (color);
ee78dc32 6426
d5db4077 6427 if (w32_defined_color (f, SDATA (color), &foo, 0))
6fc2811b
JR
6428 return Qt;
6429 else
6430 return Qnil;
6431}
ee78dc32 6432
2d764c78 6433DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
74e1aeec 6434 doc: /* Internal function called by `color-values', which see. */)
ee78dc32
GV
6435 (color, frame)
6436 Lisp_Object color, frame;
6437{
6fc2811b 6438 XColor foo;
ee78dc32
GV
6439 FRAME_PTR f = check_x_frame (frame);
6440
b7826503 6441 CHECK_STRING (color);
ee78dc32 6442
d5db4077 6443 if (w32_defined_color (f, SDATA (color), &foo, 0))
ee78dc32
GV
6444 {
6445 Lisp_Object rgb[3];
6446
6fc2811b
JR
6447 rgb[0] = make_number ((GetRValue (foo.pixel) << 8)
6448 | GetRValue (foo.pixel));
6449 rgb[1] = make_number ((GetGValue (foo.pixel) << 8)
6450 | GetGValue (foo.pixel));
6451 rgb[2] = make_number ((GetBValue (foo.pixel) << 8)
6452 | GetBValue (foo.pixel));
ee78dc32
GV
6453 return Flist (3, rgb);
6454 }
6455 else
6456 return Qnil;
6457}
6458
2d764c78 6459DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
74e1aeec 6460 doc: /* Internal function called by `display-color-p', which see. */)
ee78dc32
GV
6461 (display)
6462 Lisp_Object display;
6463{
fbd6baed 6464 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
6465
6466 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 2)
6467 return Qnil;
6468
6469 return Qt;
6470}
6471
74e1aeec
JR
6472DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p,
6473 Sx_display_grayscale_p, 0, 1, 0,
6474 doc: /* Return t if the X display supports shades of gray.
6475Note that color displays do support shades of gray.
6476The optional argument DISPLAY specifies which display to ask about.
6477DISPLAY should be either a frame or a display name (a string).
6478If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
6479 (display)
6480 Lisp_Object display;
6481{
fbd6baed 6482 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
6483
6484 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 1)
6485 return Qnil;
6486
6487 return Qt;
6488}
6489
74e1aeec
JR
6490DEFUN ("x-display-pixel-width", Fx_display_pixel_width,
6491 Sx_display_pixel_width, 0, 1, 0,
6492 doc: /* Returns the width in pixels of DISPLAY.
6493The optional argument DISPLAY specifies which display to ask about.
6494DISPLAY should be either a frame or a display name (a string).
6495If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
6496 (display)
6497 Lisp_Object display;
6498{
fbd6baed 6499 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
6500
6501 return make_number (dpyinfo->width);
6502}
6503
6504DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
74e1aeec
JR
6505 Sx_display_pixel_height, 0, 1, 0,
6506 doc: /* Returns the height in pixels of DISPLAY.
6507The optional argument DISPLAY specifies which display to ask about.
6508DISPLAY should be either a frame or a display name (a string).
6509If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
6510 (display)
6511 Lisp_Object display;
6512{
fbd6baed 6513 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
6514
6515 return make_number (dpyinfo->height);
6516}
6517
6518DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
74e1aeec
JR
6519 0, 1, 0,
6520 doc: /* Returns the number of bitplanes of DISPLAY.
6521The optional argument DISPLAY specifies which display to ask about.
6522DISPLAY should be either a frame or a display name (a string).
6523If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
6524 (display)
6525 Lisp_Object display;
6526{
fbd6baed 6527 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
6528
6529 return make_number (dpyinfo->n_planes * dpyinfo->n_cbits);
6530}
6531
6532DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
74e1aeec
JR
6533 0, 1, 0,
6534 doc: /* Returns the number of color cells of DISPLAY.
6535The optional argument DISPLAY specifies which display to ask about.
6536DISPLAY should be either a frame or a display name (a string).
6537If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
6538 (display)
6539 Lisp_Object display;
6540{
fbd6baed 6541 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
6542 HDC hdc;
6543 int cap;
6544
5ac45f98
GV
6545 hdc = GetDC (dpyinfo->root_window);
6546 if (dpyinfo->has_palette)
6547 cap = GetDeviceCaps (hdc,SIZEPALETTE);
6548 else
6549 cap = GetDeviceCaps (hdc,NUMCOLORS);
abf8c61b 6550
007776bc
JB
6551 /* We force 24+ bit depths to 24-bit, both to prevent an overflow
6552 and because probably is more meaningful on Windows anyway */
abf8c61b 6553 if (cap < 0)
007776bc 6554 cap = 1 << min(dpyinfo->n_planes * dpyinfo->n_cbits, 24);
7d0393cf 6555
ee78dc32 6556 ReleaseDC (dpyinfo->root_window, hdc);
7d0393cf 6557
ee78dc32
GV
6558 return make_number (cap);
6559}
6560
6561DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
6562 Sx_server_max_request_size,
74e1aeec
JR
6563 0, 1, 0,
6564 doc: /* Returns the maximum request size of the server of DISPLAY.
6565The optional argument DISPLAY specifies which display to ask about.
6566DISPLAY should be either a frame or a display name (a string).
6567If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
6568 (display)
6569 Lisp_Object display;
6570{
fbd6baed 6571 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
6572
6573 return make_number (1);
6574}
6575
6576DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
74e1aeec
JR
6577 doc: /* Returns the vendor ID string of the W32 system (Microsoft).
6578The optional argument DISPLAY specifies which display to ask about.
6579DISPLAY should be either a frame or a display name (a string).
6580If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
6581 (display)
6582 Lisp_Object display;
6583{
dfff8a69 6584 return build_string ("Microsoft Corp.");
ee78dc32
GV
6585}
6586
6587DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
74e1aeec
JR
6588 doc: /* Returns the version numbers of the server of DISPLAY.
6589The value is a list of three integers: the major and minor
6590version numbers, and the vendor-specific release
6591number. See also the function `x-server-vendor'.
6592
6593The optional argument DISPLAY specifies which display to ask about.
6594DISPLAY should be either a frame or a display name (a string).
6595If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
6596 (display)
6597 Lisp_Object display;
6598{
fbd6baed 6599 return Fcons (make_number (w32_major_version),
58e0f0e4
AI
6600 Fcons (make_number (w32_minor_version),
6601 Fcons (make_number (w32_build_number), Qnil)));
ee78dc32
GV
6602}
6603
6604DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
74e1aeec
JR
6605 doc: /* Returns the number of screens on the server of DISPLAY.
6606The optional argument DISPLAY specifies which display to ask about.
6607DISPLAY should be either a frame or a display name (a string).
6608If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
6609 (display)
6610 Lisp_Object display;
6611{
ee78dc32
GV
6612 return make_number (1);
6613}
6614
74e1aeec
JR
6615DEFUN ("x-display-mm-height", Fx_display_mm_height,
6616 Sx_display_mm_height, 0, 1, 0,
6617 doc: /* Returns the height in millimeters of DISPLAY.
6618The optional argument DISPLAY specifies which display to ask about.
6619DISPLAY should be either a frame or a display name (a string).
6620If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
6621 (display)
6622 Lisp_Object display;
6623{
fbd6baed 6624 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
6625 HDC hdc;
6626 int cap;
6627
5ac45f98 6628 hdc = GetDC (dpyinfo->root_window);
7d0393cf 6629
ee78dc32 6630 cap = GetDeviceCaps (hdc, VERTSIZE);
7d0393cf 6631
ee78dc32 6632 ReleaseDC (dpyinfo->root_window, hdc);
7d0393cf 6633
ee78dc32
GV
6634 return make_number (cap);
6635}
6636
6637DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
74e1aeec
JR
6638 doc: /* Returns the width in millimeters of DISPLAY.
6639The optional argument DISPLAY specifies which display to ask about.
6640DISPLAY should be either a frame or a display name (a string).
6641If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
6642 (display)
6643 Lisp_Object display;
6644{
fbd6baed 6645 struct w32_display_info *dpyinfo = check_x_display_info (display);
ee78dc32
GV
6646
6647 HDC hdc;
6648 int cap;
6649
5ac45f98 6650 hdc = GetDC (dpyinfo->root_window);
7d0393cf 6651
ee78dc32 6652 cap = GetDeviceCaps (hdc, HORZSIZE);
7d0393cf 6653
ee78dc32 6654 ReleaseDC (dpyinfo->root_window, hdc);
7d0393cf 6655
ee78dc32
GV
6656 return make_number (cap);
6657}
6658
6659DEFUN ("x-display-backing-store", Fx_display_backing_store,
74e1aeec
JR
6660 Sx_display_backing_store, 0, 1, 0,
6661 doc: /* Returns an indication of whether DISPLAY does backing store.
6662The value may be `always', `when-mapped', or `not-useful'.
6663The optional argument DISPLAY specifies which display to ask about.
6664DISPLAY should be either a frame or a display name (a string).
6665If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
6666 (display)
6667 Lisp_Object display;
6668{
6669 return intern ("not-useful");
6670}
6671
6672DEFUN ("x-display-visual-class", Fx_display_visual_class,
74e1aeec
JR
6673 Sx_display_visual_class, 0, 1, 0,
6674 doc: /* Returns the visual class of DISPLAY.
6675The value is one of the symbols `static-gray', `gray-scale',
6676`static-color', `pseudo-color', `true-color', or `direct-color'.
6677
6678The optional argument DISPLAY specifies which display to ask about.
6679DISPLAY should be either a frame or a display name (a string).
6680If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
6681 (display)
6682 Lisp_Object display;
6683{
fbd6baed 6684 struct w32_display_info *dpyinfo = check_x_display_info (display);
abf8c61b 6685 Lisp_Object result = Qnil;
ee78dc32 6686
abf8c61b
AI
6687 if (dpyinfo->has_palette)
6688 result = intern ("pseudo-color");
6689 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 1)
6690 result = intern ("static-grey");
6691 else if (dpyinfo->n_planes * dpyinfo->n_cbits == 4)
6692 result = intern ("static-color");
6693 else if (dpyinfo->n_planes * dpyinfo->n_cbits > 8)
6694 result = intern ("true-color");
ee78dc32 6695
abf8c61b 6696 return result;
ee78dc32
GV
6697}
6698
6699DEFUN ("x-display-save-under", Fx_display_save_under,
74e1aeec
JR
6700 Sx_display_save_under, 0, 1, 0,
6701 doc: /* Returns t if DISPLAY supports the save-under feature.
6702The optional argument DISPLAY specifies which display to ask about.
6703DISPLAY should be either a frame or a display name (a string).
6704If omitted or nil, that stands for the selected frame's display. */)
ee78dc32
GV
6705 (display)
6706 Lisp_Object display;
6707{
6fc2811b
JR
6708 return Qnil;
6709}
6710\f
6711int
6712x_pixel_width (f)
6713 register struct frame *f;
6714{
be786000 6715 return FRAME_PIXEL_WIDTH (f);
6fc2811b
JR
6716}
6717
6718int
6719x_pixel_height (f)
6720 register struct frame *f;
6721{
be786000 6722 return FRAME_PIXEL_HEIGHT (f);
6fc2811b
JR
6723}
6724
6725int
6726x_char_width (f)
6727 register struct frame *f;
6728{
be786000 6729 return FRAME_COLUMN_WIDTH (f);
6fc2811b
JR
6730}
6731
6732int
6733x_char_height (f)
6734 register struct frame *f;
6735{
be786000 6736 return FRAME_LINE_HEIGHT (f);
6fc2811b
JR
6737}
6738
6739int
6740x_screen_planes (f)
6741 register struct frame *f;
6742{
6743 return FRAME_W32_DISPLAY_INFO (f)->n_planes;
6744}
6745\f
6746/* Return the display structure for the display named NAME.
6747 Open a new connection if necessary. */
6748
6749struct w32_display_info *
6750x_display_info_for_name (name)
6751 Lisp_Object name;
6752{
6753 Lisp_Object names;
6754 struct w32_display_info *dpyinfo;
6755
b7826503 6756 CHECK_STRING (name);
6fc2811b
JR
6757
6758 for (dpyinfo = &one_w32_display_info, names = w32_display_name_list;
6759 dpyinfo;
6760 dpyinfo = dpyinfo->next, names = XCDR (names))
6761 {
6762 Lisp_Object tem;
6763 tem = Fstring_equal (XCAR (XCAR (names)), name);
6764 if (!NILP (tem))
6765 return dpyinfo;
6766 }
6767
6768 /* Use this general default value to start with. */
6769 Vx_resource_name = Vinvocation_name;
6770
6771 validate_x_resource_name ();
6772
6773 dpyinfo = w32_term_init (name, (unsigned char *)0,
d5db4077 6774 (char *) SDATA (Vx_resource_name));
6fc2811b
JR
6775
6776 if (dpyinfo == 0)
d5db4077 6777 error ("Cannot connect to server %s", SDATA (name));
6fc2811b
JR
6778
6779 w32_in_use = 1;
6780 XSETFASTINT (Vwindow_system_version, 3);
6781
6782 return dpyinfo;
6783}
6784
6785DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
74e1aeec
JR
6786 1, 3, 0, doc: /* Open a connection to a server.
6787DISPLAY is the name of the display to connect to.
6788Optional second arg XRM-STRING is a string of resources in xrdb format.
6789If the optional third arg MUST-SUCCEED is non-nil,
6790terminate Emacs if we can't open the connection. */)
6fc2811b
JR
6791 (display, xrm_string, must_succeed)
6792 Lisp_Object display, xrm_string, must_succeed;
6793{
6794 unsigned char *xrm_option;
6795 struct w32_display_info *dpyinfo;
6796
74e1aeec
JR
6797 /* If initialization has already been done, return now to avoid
6798 overwriting critical parts of one_w32_display_info. */
6799 if (w32_in_use)
6800 return Qnil;
6801
b7826503 6802 CHECK_STRING (display);
6fc2811b 6803 if (! NILP (xrm_string))
b7826503 6804 CHECK_STRING (xrm_string);
6fc2811b
JR
6805
6806 if (! EQ (Vwindow_system, intern ("w32")))
6807 error ("Not using Microsoft Windows");
6808
6809 /* Allow color mapping to be defined externally; first look in user's
6810 HOME directory, then in Emacs etc dir for a file called rgb.txt. */
6811 {
6812 Lisp_Object color_file;
6813 struct gcpro gcpro1;
6814
6815 color_file = build_string("~/rgb.txt");
6816
6817 GCPRO1 (color_file);
6818
6819 if (NILP (Ffile_readable_p (color_file)))
6820 color_file =
6821 Fexpand_file_name (build_string ("rgb.txt"),
6822 Fsymbol_value (intern ("data-directory")));
6823
6824 Vw32_color_map = Fw32_load_color_file (color_file);
6825
6826 UNGCPRO;
6827 }
6828 if (NILP (Vw32_color_map))
6829 Vw32_color_map = Fw32_default_color_map ();
6830
5a8a15ec
JR
6831 /* Merge in system logical colors. */
6832 add_system_logical_colors_to_map (&Vw32_color_map);
6833
6fc2811b 6834 if (! NILP (xrm_string))
d5db4077 6835 xrm_option = (unsigned char *) SDATA (xrm_string);
6fc2811b
JR
6836 else
6837 xrm_option = (unsigned char *) 0;
6838
6839 /* Use this general default value to start with. */
6840 /* First remove .exe suffix from invocation-name - it looks ugly. */
6841 {
6842 char basename[ MAX_PATH ], *str;
6843
d5db4077 6844 strcpy (basename, SDATA (Vinvocation_name));
6fc2811b
JR
6845 str = strrchr (basename, '.');
6846 if (str) *str = 0;
6847 Vinvocation_name = build_string (basename);
6848 }
6849 Vx_resource_name = Vinvocation_name;
6850
6851 validate_x_resource_name ();
6852
6853 /* This is what opens the connection and sets x_current_display.
6854 This also initializes many symbols, such as those used for input. */
6855 dpyinfo = w32_term_init (display, xrm_option,
d5db4077 6856 (char *) SDATA (Vx_resource_name));
6fc2811b
JR
6857
6858 if (dpyinfo == 0)
6859 {
6860 if (!NILP (must_succeed))
6861 fatal ("Cannot connect to server %s.\n",
d5db4077 6862 SDATA (display));
6fc2811b 6863 else
d5db4077 6864 error ("Cannot connect to server %s", SDATA (display));
6fc2811b
JR
6865 }
6866
6867 w32_in_use = 1;
6868
6869 XSETFASTINT (Vwindow_system_version, 3);
6870 return Qnil;
6871}
6872
6873DEFUN ("x-close-connection", Fx_close_connection,
6874 Sx_close_connection, 1, 1, 0,
74e1aeec
JR
6875 doc: /* Close the connection to DISPLAY's server.
6876For DISPLAY, specify either a frame or a display name (a string).
6877If DISPLAY is nil, that stands for the selected frame's display. */)
6fc2811b
JR
6878 (display)
6879 Lisp_Object display;
6880{
6881 struct w32_display_info *dpyinfo = check_x_display_info (display);
6882 int i;
6883
6884 if (dpyinfo->reference_count > 0)
6885 error ("Display still has frames on it");
6886
6887 BLOCK_INPUT;
6888 /* Free the fonts in the font table. */
6889 for (i = 0; i < dpyinfo->n_fonts; i++)
6890 if (dpyinfo->font_table[i].name)
6891 {
126f2e35
JR
6892 if (dpyinfo->font_table[i].name != dpyinfo->font_table[i].full_name)
6893 xfree (dpyinfo->font_table[i].full_name);
6fc2811b 6894 xfree (dpyinfo->font_table[i].name);
6fc2811b
JR
6895 w32_unload_font (dpyinfo, dpyinfo->font_table[i].font);
6896 }
6897 x_destroy_all_bitmaps (dpyinfo);
6898
6899 x_delete_display (dpyinfo);
6900 UNBLOCK_INPUT;
6901
6902 return Qnil;
6903}
6904
6905DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
74e1aeec 6906 doc: /* Return the list of display names that Emacs has connections to. */)
6fc2811b
JR
6907 ()
6908{
6909 Lisp_Object tail, result;
6910
6911 result = Qnil;
6912 for (tail = w32_display_name_list; ! NILP (tail); tail = XCDR (tail))
6913 result = Fcons (XCAR (XCAR (tail)), result);
6914
6915 return result;
6916}
6917
6918DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
0a332240
PJ
6919 doc: /* This is a noop on W32 systems. */)
6920 (on, display)
6921 Lisp_Object display, on;
6fc2811b 6922{
6fc2811b
JR
6923 return Qnil;
6924}
6925
6926\f
6fc2811b
JR
6927/***********************************************************************
6928 Image types
6929 ***********************************************************************/
6930
6931/* Value is the number of elements of vector VECTOR. */
6932
6933#define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR))
6934
6935/* List of supported image types. Use define_image_type to add new
6936 types. Use lookup_image_type to find a type for a given symbol. */
6937
6938static struct image_type *image_types;
6939
6fc2811b
JR
6940/* The symbol `image' which is the car of the lists used to represent
6941 images in Lisp. */
6942
6943extern Lisp_Object Qimage;
6944
6945/* The symbol `xbm' which is used as the type symbol for XBM images. */
6946
6947Lisp_Object Qxbm;
6948
6949/* Keywords. */
6950
6fc2811b 6951extern Lisp_Object QCwidth, QCheight, QCforeground, QCbackground, QCfile;
77814035
KS
6952extern Lisp_Object QCdata, QCtype;
6953Lisp_Object QCascent, QCmargin, QCrelief;
a93f4566 6954Lisp_Object QCconversion, QCcolor_symbols, QCheuristic_mask;
3cf3436e 6955Lisp_Object QCindex, QCmatrix, QCcolor_adjustment, QCmask;
6fc2811b
JR
6956
6957/* Other symbols. */
6958
3cf3436e 6959Lisp_Object Qlaplace, Qemboss, Qedge_detection, Qheuristic;
6fc2811b
JR
6960
6961/* Time in seconds after which images should be removed from the cache
6962 if not displayed. */
6963
6964Lisp_Object Vimage_cache_eviction_delay;
6965
6966/* Function prototypes. */
6967
6968static void define_image_type P_ ((struct image_type *type));
6969static struct image_type *lookup_image_type P_ ((Lisp_Object symbol));
6970static void image_error P_ ((char *format, Lisp_Object, Lisp_Object));
6971static void x_laplace P_ ((struct frame *, struct image *));
3cf3436e 6972static void x_emboss P_ ((struct frame *, struct image *));
6fc2811b
JR
6973static int x_build_heuristic_mask P_ ((struct frame *, struct image *,
6974 Lisp_Object));
6975
dfff8a69 6976
6fc2811b
JR
6977/* Define a new image type from TYPE. This adds a copy of TYPE to
6978 image_types and adds the symbol *TYPE->type to Vimage_types. */
6979
6980static void
6981define_image_type (type)
6982 struct image_type *type;
6983{
6984 /* Make a copy of TYPE to avoid a bus error in a dumped Emacs.
6985 The initialized data segment is read-only. */
6986 struct image_type *p = (struct image_type *) xmalloc (sizeof *p);
6987 bcopy (type, p, sizeof *p);
6988 p->next = image_types;
6989 image_types = p;
6990 Vimage_types = Fcons (*p->type, Vimage_types);
6991}
6992
6993
6994/* Look up image type SYMBOL, and return a pointer to its image_type
6995 structure. Value is null if SYMBOL is not a known image type. */
6996
6997static INLINE struct image_type *
6998lookup_image_type (symbol)
6999 Lisp_Object symbol;
7000{
7001 struct image_type *type;
7002
7003 for (type = image_types; type; type = type->next)
7004 if (EQ (symbol, *type->type))
7005 break;
7006
7007 return type;
7008}
7009
7010
7011/* Value is non-zero if OBJECT is a valid Lisp image specification. A
7012 valid image specification is a list whose car is the symbol
7013 `image', and whose rest is a property list. The property list must
7014 contain a value for key `:type'. That value must be the name of a
7015 supported image type. The rest of the property list depends on the
7016 image type. */
7017
7018int
7019valid_image_p (object)
7020 Lisp_Object object;
7021{
7022 int valid_p = 0;
7d0393cf 7023
6fc2811b
JR
7024 if (CONSP (object) && EQ (XCAR (object), Qimage))
7025 {
3cf3436e
JR
7026 Lisp_Object tem;
7027
7028 for (tem = XCDR (object); CONSP (tem); tem = XCDR (tem))
7029 if (EQ (XCAR (tem), QCtype))
7030 {
7031 tem = XCDR (tem);
7032 if (CONSP (tem) && SYMBOLP (XCAR (tem)))
7033 {
7034 struct image_type *type;
7035 type = lookup_image_type (XCAR (tem));
7036 if (type)
7037 valid_p = type->valid_p (object);
7038 }
7039
7040 break;
7041 }
6fc2811b
JR
7042 }
7043
7044 return valid_p;
7045}
7046
7047
7048/* Log error message with format string FORMAT and argument ARG.
7049 Signaling an error, e.g. when an image cannot be loaded, is not a
7050 good idea because this would interrupt redisplay, and the error
7051 message display would lead to another redisplay. This function
7052 therefore simply displays a message. */
7053
7054static void
7055image_error (format, arg1, arg2)
7056 char *format;
7057 Lisp_Object arg1, arg2;
7058{
7059 add_to_log (format, arg1, arg2);
7060}
7061
7062
7063\f
7064/***********************************************************************
7065 Image specifications
7066 ***********************************************************************/
7067
7068enum image_value_type
7069{
7070 IMAGE_DONT_CHECK_VALUE_TYPE,
7071 IMAGE_STRING_VALUE,
3cf3436e 7072 IMAGE_STRING_OR_NIL_VALUE,
6fc2811b
JR
7073 IMAGE_SYMBOL_VALUE,
7074 IMAGE_POSITIVE_INTEGER_VALUE,
8edb0a6f 7075 IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR,
6fc2811b 7076 IMAGE_NON_NEGATIVE_INTEGER_VALUE,
dfff8a69 7077 IMAGE_ASCENT_VALUE,
6fc2811b
JR
7078 IMAGE_INTEGER_VALUE,
7079 IMAGE_FUNCTION_VALUE,
7080 IMAGE_NUMBER_VALUE,
7081 IMAGE_BOOL_VALUE
7082};
7083
7084/* Structure used when parsing image specifications. */
7085
7086struct image_keyword
7087{
7088 /* Name of keyword. */
7089 char *name;
7090
7091 /* The type of value allowed. */
7092 enum image_value_type type;
7093
7094 /* Non-zero means key must be present. */
7095 int mandatory_p;
7096
7097 /* Used to recognize duplicate keywords in a property list. */
7098 int count;
7099
7100 /* The value that was found. */
7101 Lisp_Object value;
7102};
7103
7104
7105static int parse_image_spec P_ ((Lisp_Object, struct image_keyword *,
7106 int, Lisp_Object));
7107static Lisp_Object image_spec_value P_ ((Lisp_Object, Lisp_Object, int *));
7108
7109
7110/* Parse image spec SPEC according to KEYWORDS. A valid image spec
7111 has the format (image KEYWORD VALUE ...). One of the keyword/
7112 value pairs must be `:type TYPE'. KEYWORDS is a vector of
7113 image_keywords structures of size NKEYWORDS describing other
7114 allowed keyword/value pairs. Value is non-zero if SPEC is valid. */
7115
7116static int
7117parse_image_spec (spec, keywords, nkeywords, type)
7118 Lisp_Object spec;
7119 struct image_keyword *keywords;
7120 int nkeywords;
7121 Lisp_Object type;
7122{
7123 int i;
7124 Lisp_Object plist;
7125
7126 if (!CONSP (spec) || !EQ (XCAR (spec), Qimage))
7127 return 0;
7128
7129 plist = XCDR (spec);
7130 while (CONSP (plist))
7131 {
7132 Lisp_Object key, value;
7133
7134 /* First element of a pair must be a symbol. */
7135 key = XCAR (plist);
7136 plist = XCDR (plist);
7137 if (!SYMBOLP (key))
7138 return 0;
7139
7140 /* There must follow a value. */
7141 if (!CONSP (plist))
7142 return 0;
7143 value = XCAR (plist);
7144 plist = XCDR (plist);
7145
7146 /* Find key in KEYWORDS. Error if not found. */
7147 for (i = 0; i < nkeywords; ++i)
d5db4077 7148 if (strcmp (keywords[i].name, SDATA (SYMBOL_NAME (key))) == 0)
6fc2811b
JR
7149 break;
7150
7151 if (i == nkeywords)
7152 continue;
7153
7154 /* Record that we recognized the keyword. If a keywords
7155 was found more than once, it's an error. */
7156 keywords[i].value = value;
7157 ++keywords[i].count;
7d0393cf 7158
6fc2811b
JR
7159 if (keywords[i].count > 1)
7160 return 0;
7161
7162 /* Check type of value against allowed type. */
7163 switch (keywords[i].type)
7164 {
7165 case IMAGE_STRING_VALUE:
7166 if (!STRINGP (value))
7167 return 0;
7168 break;
7169
3cf3436e
JR
7170 case IMAGE_STRING_OR_NIL_VALUE:
7171 if (!STRINGP (value) && !NILP (value))
7172 return 0;
7173 break;
7174
6fc2811b
JR
7175 case IMAGE_SYMBOL_VALUE:
7176 if (!SYMBOLP (value))
7177 return 0;
7178 break;
7179
7180 case IMAGE_POSITIVE_INTEGER_VALUE:
7181 if (!INTEGERP (value) || XINT (value) <= 0)
7182 return 0;
7183 break;
7184
8edb0a6f
JR
7185 case IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR:
7186 if (INTEGERP (value) && XINT (value) >= 0)
7187 break;
7188 if (CONSP (value)
7189 && INTEGERP (XCAR (value)) && INTEGERP (XCDR (value))
7190 && XINT (XCAR (value)) >= 0 && XINT (XCDR (value)) >= 0)
7191 break;
7192 return 0;
7193
dfff8a69
JR
7194 case IMAGE_ASCENT_VALUE:
7195 if (SYMBOLP (value) && EQ (value, Qcenter))
7196 break;
7197 else if (INTEGERP (value)
7198 && XINT (value) >= 0
7199 && XINT (value) <= 100)
7200 break;
7201 return 0;
7202
6fc2811b
JR
7203 case IMAGE_NON_NEGATIVE_INTEGER_VALUE:
7204 if (!INTEGERP (value) || XINT (value) < 0)
7205 return 0;
7206 break;
7207
7208 case IMAGE_DONT_CHECK_VALUE_TYPE:
7209 break;
7210
7211 case IMAGE_FUNCTION_VALUE:
7212 value = indirect_function (value);
7d0393cf 7213 if (SUBRP (value)
6fc2811b
JR
7214 || COMPILEDP (value)
7215 || (CONSP (value) && EQ (XCAR (value), Qlambda)))
7216 break;
7217 return 0;
7218
7219 case IMAGE_NUMBER_VALUE:
7220 if (!INTEGERP (value) && !FLOATP (value))
7221 return 0;
7222 break;
7223
7224 case IMAGE_INTEGER_VALUE:
7225 if (!INTEGERP (value))
7226 return 0;
7227 break;
7228
7229 case IMAGE_BOOL_VALUE:
7230 if (!NILP (value) && !EQ (value, Qt))
7231 return 0;
7232 break;
7233
7234 default:
7235 abort ();
7236 break;
7237 }
7238
7239 if (EQ (key, QCtype) && !EQ (type, value))
7240 return 0;
7241 }
7242
7243 /* Check that all mandatory fields are present. */
7244 for (i = 0; i < nkeywords; ++i)
7245 if (keywords[i].mandatory_p && keywords[i].count == 0)
7246 return 0;
7247
7248 return NILP (plist);
7249}
7250
7251
7252/* Return the value of KEY in image specification SPEC. Value is nil
7253 if KEY is not present in SPEC. if FOUND is not null, set *FOUND
7254 to 1 if KEY was found in SPEC, set it to 0 otherwise. */
7255
7256static Lisp_Object
7257image_spec_value (spec, key, found)
7258 Lisp_Object spec, key;
7259 int *found;
7260{
7261 Lisp_Object tail;
7d0393cf 7262
6fc2811b
JR
7263 xassert (valid_image_p (spec));
7264
7265 for (tail = XCDR (spec);
7266 CONSP (tail) && CONSP (XCDR (tail));
7267 tail = XCDR (XCDR (tail)))
7268 {
7269 if (EQ (XCAR (tail), key))
7270 {
7271 if (found)
7272 *found = 1;
7273 return XCAR (XCDR (tail));
7274 }
7275 }
7d0393cf 7276
6fc2811b
JR
7277 if (found)
7278 *found = 0;
7279 return Qnil;
7280}
7d0393cf 7281
6fc2811b 7282
ac849ba4
JR
7283DEFUN ("image-size", Fimage_size, Simage_size, 1, 3, 0,
7284 doc: /* Return the size of image SPEC as pair (WIDTH . HEIGHT).
7285PIXELS non-nil means return the size in pixels, otherwise return the
7286size in canonical character units.
7287FRAME is the frame on which the image will be displayed. FRAME nil
7288or omitted means use the selected frame. */)
7289 (spec, pixels, frame)
7290 Lisp_Object spec, pixels, frame;
7291{
7292 Lisp_Object size;
7293
7294 size = Qnil;
7295 if (valid_image_p (spec))
7296 {
7297 struct frame *f = check_x_frame (frame);
7298 int id = lookup_image (f, spec);
7299 struct image *img = IMAGE_FROM_ID (f, id);
7300 int width = img->width + 2 * img->hmargin;
7301 int height = img->height + 2 * img->vmargin;
7d0393cf 7302
ac849ba4 7303 if (NILP (pixels))
be786000
KS
7304 size = Fcons (make_float ((double) width / FRAME_COLUMN_WIDTH (f)),
7305 make_float ((double) height / FRAME_LINE_HEIGHT (f)));
ac849ba4
JR
7306 else
7307 size = Fcons (make_number (width), make_number (height));
7308 }
7309 else
7310 error ("Invalid image specification");
7311
7312 return size;
7313}
7314
7315
7316DEFUN ("image-mask-p", Fimage_mask_p, Simage_mask_p, 1, 2, 0,
7317 doc: /* Return t if image SPEC has a mask bitmap.
7318FRAME is the frame on which the image will be displayed. FRAME nil
7319or omitted means use the selected frame. */)
7320 (spec, frame)
7321 Lisp_Object spec, frame;
7322{
7323 Lisp_Object mask;
7324
7325 mask = Qnil;
7326 if (valid_image_p (spec))
7327 {
7328 struct frame *f = check_x_frame (frame);
7329 int id = lookup_image (f, spec);
7330 struct image *img = IMAGE_FROM_ID (f, id);
7331 if (img->mask)
7332 mask = Qt;
7333 }
7334 else
7335 error ("Invalid image specification");
7336
7337 return mask;
7338}
6fc2811b
JR
7339
7340\f
7341/***********************************************************************
7342 Image type independent image structures
7343 ***********************************************************************/
7344
7345static struct image *make_image P_ ((Lisp_Object spec, unsigned hash));
7346static void free_image P_ ((struct frame *f, struct image *img));
197edd35 7347static void x_destroy_x_image P_ ((XImage *));
6fc2811b
JR
7348
7349
7350/* Allocate and return a new image structure for image specification
7351 SPEC. SPEC has a hash value of HASH. */
7352
7353static struct image *
7354make_image (spec, hash)
7355 Lisp_Object spec;
7356 unsigned hash;
7357{
7358 struct image *img = (struct image *) xmalloc (sizeof *img);
7d0393cf 7359
6fc2811b
JR
7360 xassert (valid_image_p (spec));
7361 bzero (img, sizeof *img);
7362 img->type = lookup_image_type (image_spec_value (spec, QCtype, NULL));
7363 xassert (img->type != NULL);
7364 img->spec = spec;
7365 img->data.lisp_val = Qnil;
7366 img->ascent = DEFAULT_IMAGE_ASCENT;
7367 img->hash = hash;
7368 return img;
7369}
7370
7371
7372/* Free image IMG which was used on frame F, including its resources. */
7373
7374static void
7375free_image (f, img)
7376 struct frame *f;
7377 struct image *img;
7378{
7379 if (img)
7380 {
7381 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
7382
7383 /* Remove IMG from the hash table of its cache. */
7384 if (img->prev)
7385 img->prev->next = img->next;
7386 else
7387 c->buckets[img->hash % IMAGE_CACHE_BUCKETS_SIZE] = img->next;
7388
7389 if (img->next)
7390 img->next->prev = img->prev;
7391
7392 c->images[img->id] = NULL;
7393
7394 /* Free resources, then free IMG. */
7395 img->type->free (f, img);
7396 xfree (img);
7397 }
7398}
7399
7400
7401/* Prepare image IMG for display on frame F. Must be called before
7402 drawing an image. */
7403
7404void
7405prepare_image_for_display (f, img)
7406 struct frame *f;
7407 struct image *img;
7408{
7409 EMACS_TIME t;
7410
7411 /* We're about to display IMG, so set its timestamp to `now'. */
7412 EMACS_GET_TIME (t);
7413 img->timestamp = EMACS_SECS (t);
7414
7415 /* If IMG doesn't have a pixmap yet, load it now, using the image
7416 type dependent loader function. */
7417 if (img->pixmap == 0 && !img->load_failed_p)
7418 img->load_failed_p = img->type->load (f, img) == 0;
7419}
7d0393cf 7420
6fc2811b 7421
dfff8a69
JR
7422/* Value is the number of pixels for the ascent of image IMG when
7423 drawn in face FACE. */
7424
7425int
7426image_ascent (img, face)
7427 struct image *img;
7428 struct face *face;
7429{
8edb0a6f 7430 int height = img->height + img->vmargin;
dfff8a69
JR
7431 int ascent;
7432
7433 if (img->ascent == CENTERED_IMAGE_ASCENT)
7434 {
7435 if (face->font)
7436 ascent = height / 2 - (FONT_DESCENT(face->font)
7437 - FONT_BASE(face->font)) / 2;
7438 else
7439 ascent = height / 2;
7440 }
7441 else
ac849ba4 7442 ascent = (int) (height * img->ascent / 100.0);
dfff8a69
JR
7443
7444 return ascent;
7445}
7446
7447
6fc2811b 7448\f
a05e2bae
JR
7449/* Image background colors. */
7450
ac849ba4
JR
7451/* Find the "best" corner color of a bitmap. XIMG is assumed to a device
7452 context with the bitmap selected. */
7453static COLORREF
197edd35
JR
7454four_corners_best (img_dc, width, height)
7455 HDC img_dc;
a05e2bae
JR
7456 unsigned long width, height;
7457{
ac849ba4 7458 COLORREF corners[4], best;
a05e2bae
JR
7459 int i, best_count;
7460
197edd35
JR
7461 /* Get the colors at the corners of img_dc. */
7462 corners[0] = GetPixel (img_dc, 0, 0);
7463 corners[1] = GetPixel (img_dc, width - 1, 0);
7464 corners[2] = GetPixel (img_dc, width - 1, height - 1);
7465 corners[3] = GetPixel (img_dc, 0, height - 1);
a05e2bae
JR
7466
7467 /* Choose the most frequently found color as background. */
7468 for (i = best_count = 0; i < 4; ++i)
7469 {
7470 int j, n;
7d0393cf 7471
a05e2bae
JR
7472 for (j = n = 0; j < 4; ++j)
7473 if (corners[i] == corners[j])
7474 ++n;
7475
7476 if (n > best_count)
7477 best = corners[i], best_count = n;
7478 }
7479
7480 return best;
a05e2bae
JR
7481}
7482
7483/* Return the `background' field of IMG. If IMG doesn't have one yet,
197edd35
JR
7484 it is guessed heuristically. If non-zero, IMG_DC is an existing
7485 device context with the image selected to use for the heuristic. */
a05e2bae
JR
7486
7487unsigned long
197edd35 7488image_background (img, f, img_dc)
a05e2bae
JR
7489 struct image *img;
7490 struct frame *f;
197edd35 7491 HDC img_dc;
a05e2bae
JR
7492{
7493 if (! img->background_valid)
7494 /* IMG doesn't have a background yet, try to guess a reasonable value. */
7495 {
197edd35
JR
7496 int free_ximg = !img_dc;
7497 HGDIOBJ prev;
7498
7499 if (free_ximg)
7500 {
7501 HDC frame_dc = get_frame_dc (f);
7502 img_dc = CreateCompatibleDC (frame_dc);
7503 release_frame_dc (f, frame_dc);
a05e2bae 7504
197edd35
JR
7505 prev = SelectObject (img_dc, img->pixmap);
7506 }
a05e2bae 7507
197edd35 7508 img->background = four_corners_best (img_dc, img->width, img->height);
a05e2bae
JR
7509
7510 if (free_ximg)
197edd35
JR
7511 {
7512 SelectObject (img_dc, prev);
7513 DeleteDC (img_dc);
7514 }
a05e2bae
JR
7515
7516 img->background_valid = 1;
a05e2bae
JR
7517 }
7518
7519 return img->background;
7520}
7521
7522/* Return the `background_transparent' field of IMG. If IMG doesn't
7523 have one yet, it is guessed heuristically. If non-zero, MASK is an
7524 existing XImage object to use for the heuristic. */
7525
7526int
7527image_background_transparent (img, f, mask)
7528 struct image *img;
7529 struct frame *f;
197edd35 7530 HDC mask;
a05e2bae
JR
7531{
7532 if (! img->background_transparent_valid)
7533 /* IMG doesn't have a background yet, try to guess a reasonable value. */
7534 {
a05e2bae
JR
7535 if (img->mask)
7536 {
7537 int free_mask = !mask;
197edd35 7538 HGDIOBJ prev;
a05e2bae 7539
197edd35
JR
7540 if (free_mask)
7541 {
7542 HDC frame_dc = get_frame_dc (f);
7543 mask = CreateCompatibleDC (frame_dc);
7544 release_frame_dc (f, frame_dc);
7545
c922a224 7546 prev = SelectObject (mask, img->mask);
197edd35 7547 }
a05e2bae
JR
7548
7549 img->background_transparent
7550 = !four_corners_best (mask, img->width, img->height);
7551
7552 if (free_mask)
197edd35
JR
7553 {
7554 SelectObject (mask, prev);
7555 DeleteDC (mask);
7556 }
a05e2bae
JR
7557 }
7558 else
a05e2bae
JR
7559 img->background_transparent = 0;
7560
7561 img->background_transparent_valid = 1;
7562 }
7563
7564 return img->background_transparent;
7565}
7566
7567\f
6fc2811b
JR
7568/***********************************************************************
7569 Helper functions for X image types
7570 ***********************************************************************/
7571
a05e2bae
JR
7572static void x_clear_image_1 P_ ((struct frame *, struct image *, int,
7573 int, int));
6fc2811b
JR
7574static void x_clear_image P_ ((struct frame *f, struct image *img));
7575static unsigned long x_alloc_image_color P_ ((struct frame *f,
7576 struct image *img,
7577 Lisp_Object color_name,
7578 unsigned long dflt));
7579
a05e2bae
JR
7580
7581/* Clear X resources of image IMG on frame F. PIXMAP_P non-zero means
7582 free the pixmap if any. MASK_P non-zero means clear the mask
7583 pixmap if any. COLORS_P non-zero means free colors allocated for
7584 the image, if any. */
7585
7586static void
7587x_clear_image_1 (f, img, pixmap_p, mask_p, colors_p)
7588 struct frame *f;
7589 struct image *img;
7590 int pixmap_p, mask_p, colors_p;
7591{
a05e2bae
JR
7592 if (pixmap_p && img->pixmap)
7593 {
ac849ba4
JR
7594 DeleteObject (img->pixmap);
7595 img->pixmap = NULL;
a05e2bae
JR
7596 img->background_valid = 0;
7597 }
7598
7599 if (mask_p && img->mask)
7600 {
ac849ba4
JR
7601 DeleteObject (img->mask);
7602 img->mask = NULL;
a05e2bae
JR
7603 img->background_transparent_valid = 0;
7604 }
7d0393cf 7605
a05e2bae
JR
7606 if (colors_p && img->ncolors)
7607 {
bf76fe9c 7608#if 0 /* TODO: color table support. */
a05e2bae 7609 x_free_colors (f, img->colors, img->ncolors);
bf76fe9c 7610#endif
a05e2bae
JR
7611 xfree (img->colors);
7612 img->colors = NULL;
7613 img->ncolors = 0;
7614 }
a05e2bae
JR
7615}
7616
6fc2811b
JR
7617/* Free X resources of image IMG which is used on frame F. */
7618
7619static void
7620x_clear_image (f, img)
7621 struct frame *f;
7622 struct image *img;
7623{
6fc2811b
JR
7624 if (img->pixmap)
7625 {
7626 BLOCK_INPUT;
ac849ba4 7627 DeleteObject (img->pixmap);
6fc2811b
JR
7628 img->pixmap = 0;
7629 UNBLOCK_INPUT;
7630 }
7631
7632 if (img->ncolors)
7633 {
ac849ba4
JR
7634#if 0 /* TODO: color table support */
7635
6fc2811b 7636 int class = FRAME_W32_DISPLAY_INFO (f)->visual->class;
7d0393cf 7637
6fc2811b
JR
7638 /* If display has an immutable color map, freeing colors is not
7639 necessary and some servers don't allow it. So don't do it. */
7640 if (class != StaticColor
7641 && class != StaticGray
7642 && class != TrueColor)
7643 {
7644 Colormap cmap;
7645 BLOCK_INPUT;
7646 cmap = DefaultColormapOfScreen (FRAME_W32_DISPLAY_INFO (f)->screen);
7647 XFreeColors (FRAME_W32_DISPLAY (f), cmap, img->colors,
7648 img->ncolors, 0);
7649 UNBLOCK_INPUT;
7650 }
ac849ba4 7651#endif
7d0393cf 7652
6fc2811b
JR
7653 xfree (img->colors);
7654 img->colors = NULL;
7655 img->ncolors = 0;
7656 }
6fc2811b
JR
7657}
7658
7659
7660/* Allocate color COLOR_NAME for image IMG on frame F. If color
7661 cannot be allocated, use DFLT. Add a newly allocated color to
7662 IMG->colors, so that it can be freed again. Value is the pixel
7663 color. */
7664
7665static unsigned long
7666x_alloc_image_color (f, img, color_name, dflt)
7667 struct frame *f;
7668 struct image *img;
7669 Lisp_Object color_name;
7670 unsigned long dflt;
7671{
6fc2811b
JR
7672 XColor color;
7673 unsigned long result;
7674
7675 xassert (STRINGP (color_name));
7676
d5db4077 7677 if (w32_defined_color (f, SDATA (color_name), &color, 1))
6fc2811b
JR
7678 {
7679 /* This isn't called frequently so we get away with simply
7680 reallocating the color vector to the needed size, here. */
7681 ++img->ncolors;
7682 img->colors =
7683 (unsigned long *) xrealloc (img->colors,
7684 img->ncolors * sizeof *img->colors);
7685 img->colors[img->ncolors - 1] = color.pixel;
7686 result = color.pixel;
7687 }
7688 else
7689 result = dflt;
7690 return result;
6fc2811b
JR
7691}
7692
7693
7694\f
7695/***********************************************************************
7696 Image Cache
7697 ***********************************************************************/
7698
7699static void cache_image P_ ((struct frame *f, struct image *img));
3cf3436e 7700static void postprocess_image P_ ((struct frame *, struct image *));
197edd35 7701static void x_disable_image P_ ((struct frame *, struct image *));
6fc2811b
JR
7702
7703
7704/* Return a new, initialized image cache that is allocated from the
7705 heap. Call free_image_cache to free an image cache. */
7706
7707struct image_cache *
7708make_image_cache ()
7709{
7710 struct image_cache *c = (struct image_cache *) xmalloc (sizeof *c);
7711 int size;
7d0393cf 7712
6fc2811b
JR
7713 bzero (c, sizeof *c);
7714 c->size = 50;
7715 c->images = (struct image **) xmalloc (c->size * sizeof *c->images);
7716 size = IMAGE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
7717 c->buckets = (struct image **) xmalloc (size);
7718 bzero (c->buckets, size);
7719 return c;
7720}
7721
7722
7723/* Free image cache of frame F. Be aware that X frames share images
7724 caches. */
7725
7726void
7727free_image_cache (f)
7728 struct frame *f;
7729{
7730 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
7731 if (c)
7732 {
7733 int i;
7734
7735 /* Cache should not be referenced by any frame when freed. */
7736 xassert (c->refcount == 0);
7d0393cf 7737
6fc2811b
JR
7738 for (i = 0; i < c->used; ++i)
7739 free_image (f, c->images[i]);
7740 xfree (c->images);
7741 xfree (c);
7742 xfree (c->buckets);
7743 FRAME_X_IMAGE_CACHE (f) = NULL;
7744 }
7745}
7746
7747
7748/* Clear image cache of frame F. FORCE_P non-zero means free all
7749 images. FORCE_P zero means clear only images that haven't been
7750 displayed for some time. Should be called from time to time to
dfff8a69
JR
7751 reduce the number of loaded images. If image-eviction-seconds is
7752 non-nil, this frees images in the cache which weren't displayed for
6fc2811b
JR
7753 at least that many seconds. */
7754
7755void
7756clear_image_cache (f, force_p)
7757 struct frame *f;
7758 int force_p;
7759{
7760 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
7761
7762 if (c && INTEGERP (Vimage_cache_eviction_delay))
7763 {
7764 EMACS_TIME t;
7765 unsigned long old;
0327b4cc 7766 int i, nfreed;
6fc2811b
JR
7767
7768 EMACS_GET_TIME (t);
7769 old = EMACS_SECS (t) - XFASTINT (Vimage_cache_eviction_delay);
7d0393cf 7770
0327b4cc
JR
7771 /* Block input so that we won't be interrupted by a SIGIO
7772 while being in an inconsistent state. */
7773 BLOCK_INPUT;
7d0393cf 7774
0327b4cc 7775 for (i = nfreed = 0; i < c->used; ++i)
6fc2811b
JR
7776 {
7777 struct image *img = c->images[i];
7778 if (img != NULL
0327b4cc 7779 && (force_p || (img->timestamp < old)))
6fc2811b
JR
7780 {
7781 free_image (f, img);
0327b4cc 7782 ++nfreed;
6fc2811b
JR
7783 }
7784 }
7785
7786 /* We may be clearing the image cache because, for example,
7787 Emacs was iconified for a longer period of time. In that
7788 case, current matrices may still contain references to
7789 images freed above. So, clear these matrices. */
0327b4cc 7790 if (nfreed)
6fc2811b 7791 {
0327b4cc 7792 Lisp_Object tail, frame;
7d0393cf 7793
0327b4cc
JR
7794 FOR_EACH_FRAME (tail, frame)
7795 {
7796 struct frame *f = XFRAME (frame);
7797 if (FRAME_W32_P (f)
7798 && FRAME_X_IMAGE_CACHE (f) == c)
7799 clear_current_matrices (f);
7800 }
7801
6fc2811b
JR
7802 ++windows_or_buffers_changed;
7803 }
0327b4cc
JR
7804
7805 UNBLOCK_INPUT;
6fc2811b
JR
7806 }
7807}
7808
7809
7810DEFUN ("clear-image-cache", Fclear_image_cache, Sclear_image_cache,
7811 0, 1, 0,
74e1aeec
JR
7812 doc: /* Clear the image cache of FRAME.
7813FRAME nil or omitted means use the selected frame.
7814FRAME t means clear the image caches of all frames. */)
6fc2811b
JR
7815 (frame)
7816 Lisp_Object frame;
7817{
7818 if (EQ (frame, Qt))
7819 {
7820 Lisp_Object tail;
7d0393cf 7821
6fc2811b
JR
7822 FOR_EACH_FRAME (tail, frame)
7823 if (FRAME_W32_P (XFRAME (frame)))
7824 clear_image_cache (XFRAME (frame), 1);
7825 }
7826 else
7827 clear_image_cache (check_x_frame (frame), 1);
7828
7829 return Qnil;
7830}
7831
7832
3cf3436e
JR
7833/* Compute masks and transform image IMG on frame F, as specified
7834 by the image's specification, */
7835
7836static void
7837postprocess_image (f, img)
7838 struct frame *f;
7839 struct image *img;
7840{
3cf3436e
JR
7841 /* Manipulation of the image's mask. */
7842 if (img->pixmap)
7843 {
7844 Lisp_Object conversion, spec;
7845 Lisp_Object mask;
7846
7847 spec = img->spec;
7d0393cf 7848
3cf3436e
JR
7849 /* `:heuristic-mask t'
7850 `:mask heuristic'
7851 means build a mask heuristically.
7852 `:heuristic-mask (R G B)'
7853 `:mask (heuristic (R G B))'
7854 means build a mask from color (R G B) in the
7855 image.
7856 `:mask nil'
7857 means remove a mask, if any. */
7d0393cf 7858
3cf3436e
JR
7859 mask = image_spec_value (spec, QCheuristic_mask, NULL);
7860 if (!NILP (mask))
7861 x_build_heuristic_mask (f, img, mask);
7862 else
7863 {
7864 int found_p;
7d0393cf 7865
3cf3436e 7866 mask = image_spec_value (spec, QCmask, &found_p);
7d0393cf 7867
3cf3436e
JR
7868 if (EQ (mask, Qheuristic))
7869 x_build_heuristic_mask (f, img, Qt);
7870 else if (CONSP (mask)
7871 && EQ (XCAR (mask), Qheuristic))
7872 {
7873 if (CONSP (XCDR (mask)))
7874 x_build_heuristic_mask (f, img, XCAR (XCDR (mask)));
7875 else
7876 x_build_heuristic_mask (f, img, XCDR (mask));
7877 }
7878 else if (NILP (mask) && found_p && img->mask)
7879 {
ac849ba4 7880 DeleteObject (img->mask);
3cf3436e
JR
7881 img->mask = NULL;
7882 }
7883 }
7d0393cf
JB
7884
7885
3cf3436e
JR
7886 /* Should we apply an image transformation algorithm? */
7887 conversion = image_spec_value (spec, QCconversion, NULL);
7888 if (EQ (conversion, Qdisabled))
7889 x_disable_image (f, img);
7890 else if (EQ (conversion, Qlaplace))
7891 x_laplace (f, img);
7892 else if (EQ (conversion, Qemboss))
7893 x_emboss (f, img);
7894 else if (CONSP (conversion)
7895 && EQ (XCAR (conversion), Qedge_detection))
7896 {
7897 Lisp_Object tem;
7898 tem = XCDR (conversion);
7899 if (CONSP (tem))
7900 x_edge_detection (f, img,
7901 Fplist_get (tem, QCmatrix),
7902 Fplist_get (tem, QCcolor_adjustment));
7903 }
7904 }
3cf3436e
JR
7905}
7906
7907
6fc2811b
JR
7908/* Return the id of image with Lisp specification SPEC on frame F.
7909 SPEC must be a valid Lisp image specification (see valid_image_p). */
7910
7911int
7912lookup_image (f, spec)
7913 struct frame *f;
7914 Lisp_Object spec;
7915{
7916 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
7917 struct image *img;
7918 int i;
7919 unsigned hash;
7920 struct gcpro gcpro1;
7921 EMACS_TIME now;
7922
7923 /* F must be a window-system frame, and SPEC must be a valid image
7924 specification. */
7925 xassert (FRAME_WINDOW_P (f));
7926 xassert (valid_image_p (spec));
7d0393cf 7927
6fc2811b
JR
7928 GCPRO1 (spec);
7929
7930 /* Look up SPEC in the hash table of the image cache. */
7931 hash = sxhash (spec, 0);
7932 i = hash % IMAGE_CACHE_BUCKETS_SIZE;
7933
7934 for (img = c->buckets[i]; img; img = img->next)
7935 if (img->hash == hash && !NILP (Fequal (img->spec, spec)))
7936 break;
7937
7938 /* If not found, create a new image and cache it. */
7939 if (img == NULL)
7940 {
3cf3436e
JR
7941 extern Lisp_Object Qpostscript;
7942
8edb0a6f 7943 BLOCK_INPUT;
6fc2811b
JR
7944 img = make_image (spec, hash);
7945 cache_image (f, img);
7946 img->load_failed_p = img->type->load (f, img) == 0;
6fc2811b
JR
7947
7948 /* If we can't load the image, and we don't have a width and
7949 height, use some arbitrary width and height so that we can
7950 draw a rectangle for it. */
7951 if (img->load_failed_p)
7952 {
7953 Lisp_Object value;
7954
7955 value = image_spec_value (spec, QCwidth, NULL);
7956 img->width = (INTEGERP (value)
7957 ? XFASTINT (value) : DEFAULT_IMAGE_WIDTH);
7958 value = image_spec_value (spec, QCheight, NULL);
7959 img->height = (INTEGERP (value)
7960 ? XFASTINT (value) : DEFAULT_IMAGE_HEIGHT);
7961 }
7962 else
7963 {
7964 /* Handle image type independent image attributes
8f92c555 7965 `:ascent ASCENT', `:margin MARGIN', `:relief RELIEF',
a05e2bae
JR
7966 `:background COLOR'. */
7967 Lisp_Object ascent, margin, relief, bg;
6fc2811b
JR
7968
7969 ascent = image_spec_value (spec, QCascent, NULL);
7970 if (INTEGERP (ascent))
7971 img->ascent = XFASTINT (ascent);
dfff8a69
JR
7972 else if (EQ (ascent, Qcenter))
7973 img->ascent = CENTERED_IMAGE_ASCENT;
7974
6fc2811b
JR
7975 margin = image_spec_value (spec, QCmargin, NULL);
7976 if (INTEGERP (margin) && XINT (margin) >= 0)
8edb0a6f
JR
7977 img->vmargin = img->hmargin = XFASTINT (margin);
7978 else if (CONSP (margin) && INTEGERP (XCAR (margin))
7979 && INTEGERP (XCDR (margin)))
7980 {
7981 if (XINT (XCAR (margin)) > 0)
7982 img->hmargin = XFASTINT (XCAR (margin));
7983 if (XINT (XCDR (margin)) > 0)
7984 img->vmargin = XFASTINT (XCDR (margin));
7985 }
7d0393cf 7986
6fc2811b
JR
7987 relief = image_spec_value (spec, QCrelief, NULL);
7988 if (INTEGERP (relief))
7989 {
7990 img->relief = XINT (relief);
8edb0a6f
JR
7991 img->hmargin += abs (img->relief);
7992 img->vmargin += abs (img->relief);
6fc2811b
JR
7993 }
7994
a05e2bae
JR
7995 if (! img->background_valid)
7996 {
7997 bg = image_spec_value (img->spec, QCbackground, NULL);
7998 if (!NILP (bg))
7999 {
8000 img->background
8001 = x_alloc_image_color (f, img, bg,
8002 FRAME_BACKGROUND_PIXEL (f));
8003 img->background_valid = 1;
8004 }
8005 }
8006
3cf3436e
JR
8007 /* Do image transformations and compute masks, unless we
8008 don't have the image yet. */
8009 if (!EQ (*img->type->type, Qpostscript))
8010 postprocess_image (f, img);
6fc2811b 8011 }
3cf3436e 8012
8edb0a6f
JR
8013 UNBLOCK_INPUT;
8014 xassert (!interrupt_input_blocked);
6fc2811b
JR
8015 }
8016
8017 /* We're using IMG, so set its timestamp to `now'. */
8018 EMACS_GET_TIME (now);
8019 img->timestamp = EMACS_SECS (now);
7d0393cf 8020
6fc2811b 8021 UNGCPRO;
7d0393cf 8022
6fc2811b
JR
8023 /* Value is the image id. */
8024 return img->id;
8025}
8026
8027
8028/* Cache image IMG in the image cache of frame F. */
8029
8030static void
8031cache_image (f, img)
8032 struct frame *f;
8033 struct image *img;
8034{
8035 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
8036 int i;
8037
8038 /* Find a free slot in c->images. */
8039 for (i = 0; i < c->used; ++i)
8040 if (c->images[i] == NULL)
8041 break;
8042
8043 /* If no free slot found, maybe enlarge c->images. */
8044 if (i == c->used && c->used == c->size)
8045 {
8046 c->size *= 2;
8047 c->images = (struct image **) xrealloc (c->images,
8048 c->size * sizeof *c->images);
8049 }
8050
8051 /* Add IMG to c->images, and assign IMG an id. */
8052 c->images[i] = img;
8053 img->id = i;
8054 if (i == c->used)
8055 ++c->used;
8056
8057 /* Add IMG to the cache's hash table. */
8058 i = img->hash % IMAGE_CACHE_BUCKETS_SIZE;
8059 img->next = c->buckets[i];
8060 if (img->next)
8061 img->next->prev = img;
8062 img->prev = NULL;
8063 c->buckets[i] = img;
8064}
8065
8066
8067/* Call FN on every image in the image cache of frame F. Used to mark
8068 Lisp Objects in the image cache. */
8069
8070void
8071forall_images_in_image_cache (f, fn)
8072 struct frame *f;
8073 void (*fn) P_ ((struct image *img));
8074{
8075 if (FRAME_LIVE_P (f) && FRAME_W32_P (f))
8076 {
8077 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
8078 if (c)
8079 {
8080 int i;
8081 for (i = 0; i < c->used; ++i)
8082 if (c->images[i])
8083 fn (c->images[i]);
8084 }
8085 }
8086}
8087
8088
8089\f
8090/***********************************************************************
8091 W32 support code
8092 ***********************************************************************/
8093
839b1909
JR
8094/* Macro for defining functions that will be loaded from image DLLs. */
8095#define DEF_IMGLIB_FN(func) FARPROC fn_##func
c922a224 8096
839b1909
JR
8097/* Macro for loading those image functions from the library. */
8098#define LOAD_IMGLIB_FN(lib,func) { \
8099 fn_##func = (void *) GetProcAddress (lib, #func); \
8100 if (!fn_##func) return 0; \
c922a224 8101 }
839b1909 8102
6fc2811b
JR
8103static int x_create_x_image_and_pixmap P_ ((struct frame *, int, int, int,
8104 XImage **, Pixmap *));
6fc2811b
JR
8105static void x_put_x_image P_ ((struct frame *, XImage *, Pixmap, int, int));
8106
8107
8108/* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on
8109 frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created.
8110 Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated
ac849ba4
JR
8111 via xmalloc. DEPTH of zero signifies a 24 bit image, otherwise
8112 DEPTH should indicate the bit depth of the image. Print error
8113 messages via image_error if an error occurs. Value is non-zero if
8114 successful. */
6fc2811b
JR
8115
8116static int
8117x_create_x_image_and_pixmap (f, width, height, depth, ximg, pixmap)
8118 struct frame *f;
8119 int width, height, depth;
8120 XImage **ximg;
8121 Pixmap *pixmap;
8122{
ac849ba4
JR
8123 BITMAPINFOHEADER *header;
8124 HDC hdc;
8125 int scanline_width_bits;
8126 int remainder;
8127 int palette_colors = 0;
6fc2811b 8128
ac849ba4
JR
8129 if (depth == 0)
8130 depth = 24;
6fc2811b 8131
ac849ba4
JR
8132 if (depth != 1 && depth != 4 && depth != 8
8133 && depth != 16 && depth != 24 && depth != 32)
8134 {
8135 image_error ("Invalid image bit depth specified", Qnil, Qnil);
8136 return 0;
8137 }
8138
8139 scanline_width_bits = width * depth;
8140 remainder = scanline_width_bits % 32;
8141
8142 if (remainder)
8143 scanline_width_bits += 32 - remainder;
8144
8145 /* Bitmaps with a depth less than 16 need a palette. */
8146 /* BITMAPINFO structure already contains the first RGBQUAD. */
8147 if (depth < 16)
8148 palette_colors = 1 << depth - 1;
8149
8150 *ximg = xmalloc (sizeof (XImage) + palette_colors * sizeof (RGBQUAD));
6fc2811b
JR
8151 if (*ximg == NULL)
8152 {
ac849ba4 8153 image_error ("Unable to allocate memory for XImage", Qnil, Qnil);
6fc2811b
JR
8154 return 0;
8155 }
8156
ac849ba4
JR
8157 header = &((*ximg)->info.bmiHeader);
8158 bzero (&((*ximg)->info), sizeof (BITMAPINFO));
8159 header->biSize = sizeof (*header);
8160 header->biWidth = width;
8161 header->biHeight = -height; /* negative indicates a top-down bitmap. */
8162 header->biPlanes = 1;
8163 header->biBitCount = depth;
8164 header->biCompression = BI_RGB;
8165 header->biClrUsed = palette_colors;
6fc2811b 8166
197edd35 8167 /* TODO: fill in palette. */
35624c03
JR
8168 if (depth == 1)
8169 {
8170 (*ximg)->info.bmiColors[0].rgbBlue = 0;
8171 (*ximg)->info.bmiColors[0].rgbGreen = 0;
8172 (*ximg)->info.bmiColors[0].rgbRed = 0;
8173 (*ximg)->info.bmiColors[0].rgbReserved = 0;
8174 (*ximg)->info.bmiColors[1].rgbBlue = 255;
8175 (*ximg)->info.bmiColors[1].rgbGreen = 255;
8176 (*ximg)->info.bmiColors[1].rgbRed = 255;
8177 (*ximg)->info.bmiColors[1].rgbReserved = 0;
8178 }
197edd35 8179
ac849ba4
JR
8180 hdc = get_frame_dc (f);
8181
8182 /* Create a DIBSection and raster array for the bitmap,
8183 and store its handle in *pixmap. */
197edd35
JR
8184 *pixmap = CreateDIBSection (hdc, &((*ximg)->info),
8185 (depth < 16) ? DIB_PAL_COLORS : DIB_RGB_COLORS,
ac849ba4
JR
8186 &((*ximg)->data), NULL, 0);
8187
8188 /* Realize display palette and garbage all frames. */
8189 release_frame_dc (f, hdc);
8190
8191 if (*pixmap == NULL)
6fc2811b 8192 {
ac849ba4
JR
8193 DWORD err = GetLastError();
8194 Lisp_Object errcode;
8195 /* All system errors are < 10000, so the following is safe. */
8196 XSETINT (errcode, (int) err);
8197 image_error ("Unable to create bitmap, error code %d", errcode, Qnil);
6fc2811b 8198 x_destroy_x_image (*ximg);
6fc2811b
JR
8199 return 0;
8200 }
ac849ba4 8201
6fc2811b
JR
8202 return 1;
8203}
8204
8205
8206/* Destroy XImage XIMG. Free XIMG->data. */
8207
8208static void
8209x_destroy_x_image (ximg)
8210 XImage *ximg;
8211{
8212 xassert (interrupt_input_blocked);
8213 if (ximg)
8214 {
ac849ba4 8215 /* Data will be freed by DestroyObject. */
6fc2811b 8216 ximg->data = NULL;
ac849ba4 8217 xfree (ximg);
6fc2811b
JR
8218 }
8219}
8220
8221
8222/* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT
8223 are width and height of both the image and pixmap. */
8224
8225static void
8226x_put_x_image (f, ximg, pixmap, width, height)
8227 struct frame *f;
8228 XImage *ximg;
8229 Pixmap pixmap;
c9b2104d 8230 int width, height;
6fc2811b 8231{
197edd35
JR
8232#if 0 /* I don't think this is necessary looking at where it is used. */
8233 HDC hdc = get_frame_dc (f);
8234 SetDIBits (hdc, pixmap, 0, height, ximg->data, &(ximg->info), DIB_RGB_COLORS);
8235 release_frame_dc (f, hdc);
6fc2811b 8236#endif
ac849ba4 8237}
6fc2811b
JR
8238
8239\f
8240/***********************************************************************
3cf3436e 8241 File Handling
6fc2811b
JR
8242 ***********************************************************************/
8243
8244static Lisp_Object x_find_image_file P_ ((Lisp_Object));
3cf3436e
JR
8245static char *slurp_file P_ ((char *, int *));
8246
6fc2811b
JR
8247
8248/* Find image file FILE. Look in data-directory, then
8249 x-bitmap-file-path. Value is the full name of the file found, or
8250 nil if not found. */
8251
8252static Lisp_Object
8253x_find_image_file (file)
8254 Lisp_Object file;
8255{
8256 Lisp_Object file_found, search_path;
8257 struct gcpro gcpro1, gcpro2;
8258 int fd;
8259
8260 file_found = Qnil;
8261 search_path = Fcons (Vdata_directory, Vx_bitmap_file_path);
8262 GCPRO2 (file_found, search_path);
8263
8264 /* Try to find FILE in data-directory, then x-bitmap-file-path. */
de2413e9 8265 fd = openp (search_path, file, Qnil, &file_found, Qnil);
7d0393cf 8266
939d6465 8267 if (fd == -1)
6fc2811b
JR
8268 file_found = Qnil;
8269 else
8270 close (fd);
8271
8272 UNGCPRO;
8273 return file_found;
8274}
8275
8276
3cf3436e
JR
8277/* Read FILE into memory. Value is a pointer to a buffer allocated
8278 with xmalloc holding FILE's contents. Value is null if an error
8279 occurred. *SIZE is set to the size of the file. */
8280
8281static char *
8282slurp_file (file, size)
8283 char *file;
8284 int *size;
8285{
8286 FILE *fp = NULL;
8287 char *buf = NULL;
8288 struct stat st;
8289
8290 if (stat (file, &st) == 0
c45bb3b2 8291 && (fp = fopen (file, "rb")) != NULL
3cf3436e
JR
8292 && (buf = (char *) xmalloc (st.st_size),
8293 fread (buf, 1, st.st_size, fp) == st.st_size))
8294 {
8295 *size = st.st_size;
8296 fclose (fp);
8297 }
8298 else
8299 {
8300 if (fp)
8301 fclose (fp);
8302 if (buf)
8303 {
8304 xfree (buf);
8305 buf = NULL;
8306 }
8307 }
7d0393cf 8308
3cf3436e
JR
8309 return buf;
8310}
8311
8312
6fc2811b
JR
8313\f
8314/***********************************************************************
8315 XBM images
8316 ***********************************************************************/
8317
217e5be0 8318static int xbm_scan P_ ((char **, char *, char *, int *));
6fc2811b 8319static int xbm_load P_ ((struct frame *f, struct image *img));
217e5be0
JR
8320static int xbm_load_image P_ ((struct frame *f, struct image *img,
8321 char *, char *));
6fc2811b 8322static int xbm_image_p P_ ((Lisp_Object object));
217e5be0
JR
8323static int xbm_read_bitmap_data P_ ((char *, char *, int *, int *,
8324 unsigned char **));
8325static int xbm_file_p P_ ((Lisp_Object));
6fc2811b
JR
8326
8327
8328/* Indices of image specification fields in xbm_format, below. */
8329
8330enum xbm_keyword_index
8331{
8332 XBM_TYPE,
8333 XBM_FILE,
8334 XBM_WIDTH,
8335 XBM_HEIGHT,
8336 XBM_DATA,
8337 XBM_FOREGROUND,
8338 XBM_BACKGROUND,
8339 XBM_ASCENT,
8340 XBM_MARGIN,
8341 XBM_RELIEF,
8342 XBM_ALGORITHM,
8343 XBM_HEURISTIC_MASK,
a05e2bae 8344 XBM_MASK,
6fc2811b
JR
8345 XBM_LAST
8346};
8347
8348/* Vector of image_keyword structures describing the format
8349 of valid XBM image specifications. */
8350
8351static struct image_keyword xbm_format[XBM_LAST] =
8352{
8353 {":type", IMAGE_SYMBOL_VALUE, 1},
8354 {":file", IMAGE_STRING_VALUE, 0},
8355 {":width", IMAGE_POSITIVE_INTEGER_VALUE, 0},
8356 {":height", IMAGE_POSITIVE_INTEGER_VALUE, 0},
8357 {":data", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
217e5be0
JR
8358 {":foreground", IMAGE_STRING_OR_NIL_VALUE, 0},
8359 {":background", IMAGE_STRING_OR_NIL_VALUE, 0},
8360 {":ascent", IMAGE_ASCENT_VALUE, 0},
8edb0a6f 8361 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
6fc2811b 8362 {":relief", IMAGE_INTEGER_VALUE, 0},
a93f4566 8363 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
217e5be0
JR
8364 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8365 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
6fc2811b
JR
8366};
8367
8368/* Structure describing the image type XBM. */
8369
8370static struct image_type xbm_type =
8371{
8372 &Qxbm,
8373 xbm_image_p,
8374 xbm_load,
8375 x_clear_image,
8376 NULL
8377};
8378
8379/* Tokens returned from xbm_scan. */
8380
8381enum xbm_token
8382{
8383 XBM_TK_IDENT = 256,
8384 XBM_TK_NUMBER
8385};
8386
7d0393cf 8387
6fc2811b
JR
8388/* Return non-zero if OBJECT is a valid XBM-type image specification.
8389 A valid specification is a list starting with the symbol `image'
8390 The rest of the list is a property list which must contain an
8391 entry `:type xbm..
8392
8393 If the specification specifies a file to load, it must contain
8394 an entry `:file FILENAME' where FILENAME is a string.
8395
8396 If the specification is for a bitmap loaded from memory it must
8397 contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where
8398 WIDTH and HEIGHT are integers > 0. DATA may be:
8399
8400 1. a string large enough to hold the bitmap data, i.e. it must
8401 have a size >= (WIDTH + 7) / 8 * HEIGHT
8402
8403 2. a bool-vector of size >= WIDTH * HEIGHT
8404
8405 3. a vector of strings or bool-vectors, one for each line of the
8406 bitmap.
8407
217e5be0
JR
8408 4. A string containing an in-memory XBM file. WIDTH and HEIGHT
8409 may not be specified in this case because they are defined in the
8410 XBM file.
8411
6fc2811b
JR
8412 Both the file and data forms may contain the additional entries
8413 `:background COLOR' and `:foreground COLOR'. If not present,
8414 foreground and background of the frame on which the image is
217e5be0 8415 displayed is used. */
6fc2811b
JR
8416
8417static int
8418xbm_image_p (object)
8419 Lisp_Object object;
8420{
8421 struct image_keyword kw[XBM_LAST];
7d0393cf 8422
6fc2811b
JR
8423 bcopy (xbm_format, kw, sizeof kw);
8424 if (!parse_image_spec (object, kw, XBM_LAST, Qxbm))
8425 return 0;
8426
8427 xassert (EQ (kw[XBM_TYPE].value, Qxbm));
8428
8429 if (kw[XBM_FILE].count)
8430 {
8431 if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_DATA].count)
8432 return 0;
8433 }
217e5be0
JR
8434 else if (kw[XBM_DATA].count && xbm_file_p (kw[XBM_DATA].value))
8435 {
8436 /* In-memory XBM file. */
8437 if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_FILE].count)
8438 return 0;
8439 }
6fc2811b
JR
8440 else
8441 {
8442 Lisp_Object data;
8443 int width, height;
8444
8445 /* Entries for `:width', `:height' and `:data' must be present. */
8446 if (!kw[XBM_WIDTH].count
8447 || !kw[XBM_HEIGHT].count
8448 || !kw[XBM_DATA].count)
8449 return 0;
8450
8451 data = kw[XBM_DATA].value;
8452 width = XFASTINT (kw[XBM_WIDTH].value);
8453 height = XFASTINT (kw[XBM_HEIGHT].value);
7d0393cf 8454
6fc2811b
JR
8455 /* Check type of data, and width and height against contents of
8456 data. */
8457 if (VECTORP (data))
8458 {
8459 int i;
7d0393cf 8460
6fc2811b
JR
8461 /* Number of elements of the vector must be >= height. */
8462 if (XVECTOR (data)->size < height)
8463 return 0;
8464
8465 /* Each string or bool-vector in data must be large enough
8466 for one line of the image. */
8467 for (i = 0; i < height; ++i)
8468 {
8469 Lisp_Object elt = XVECTOR (data)->contents[i];
8470
8471 if (STRINGP (elt))
8472 {
d5db4077 8473 if (SCHARS (elt)
6fc2811b
JR
8474 < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR)
8475 return 0;
8476 }
8477 else if (BOOL_VECTOR_P (elt))
8478 {
8479 if (XBOOL_VECTOR (elt)->size < width)
8480 return 0;
8481 }
8482 else
8483 return 0;
8484 }
8485 }
8486 else if (STRINGP (data))
8487 {
d5db4077 8488 if (SCHARS (data)
6fc2811b
JR
8489 < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR * height)
8490 return 0;
8491 }
8492 else if (BOOL_VECTOR_P (data))
8493 {
8494 if (XBOOL_VECTOR (data)->size < width * height)
8495 return 0;
8496 }
8497 else
8498 return 0;
8499 }
8500
6fc2811b
JR
8501 return 1;
8502}
8503
8504
8505/* Scan a bitmap file. FP is the stream to read from. Value is
8506 either an enumerator from enum xbm_token, or a character for a
8507 single-character token, or 0 at end of file. If scanning an
8508 identifier, store the lexeme of the identifier in SVAL. If
8509 scanning a number, store its value in *IVAL. */
8510
8511static int
3cf3436e
JR
8512xbm_scan (s, end, sval, ival)
8513 char **s, *end;
6fc2811b
JR
8514 char *sval;
8515 int *ival;
8516{
8517 int c;
3cf3436e
JR
8518
8519 loop:
8520
6fc2811b 8521 /* Skip white space. */
af3f7be7 8522 while (*s < end && (c = *(*s)++, isspace (c)))
6fc2811b
JR
8523 ;
8524
3cf3436e 8525 if (*s >= end)
6fc2811b
JR
8526 c = 0;
8527 else if (isdigit (c))
8528 {
8529 int value = 0, digit;
7d0393cf 8530
3cf3436e 8531 if (c == '0' && *s < end)
6fc2811b 8532 {
3cf3436e 8533 c = *(*s)++;
6fc2811b
JR
8534 if (c == 'x' || c == 'X')
8535 {
3cf3436e 8536 while (*s < end)
6fc2811b 8537 {
3cf3436e 8538 c = *(*s)++;
6fc2811b
JR
8539 if (isdigit (c))
8540 digit = c - '0';
8541 else if (c >= 'a' && c <= 'f')
8542 digit = c - 'a' + 10;
8543 else if (c >= 'A' && c <= 'F')
8544 digit = c - 'A' + 10;
8545 else
8546 break;
8547 value = 16 * value + digit;
8548 }
8549 }
8550 else if (isdigit (c))
8551 {
8552 value = c - '0';
3cf3436e
JR
8553 while (*s < end
8554 && (c = *(*s)++, isdigit (c)))
6fc2811b
JR
8555 value = 8 * value + c - '0';
8556 }
8557 }
8558 else
8559 {
8560 value = c - '0';
3cf3436e
JR
8561 while (*s < end
8562 && (c = *(*s)++, isdigit (c)))
6fc2811b
JR
8563 value = 10 * value + c - '0';
8564 }
8565
3cf3436e
JR
8566 if (*s < end)
8567 *s = *s - 1;
6fc2811b
JR
8568 *ival = value;
8569 c = XBM_TK_NUMBER;
8570 }
8571 else if (isalpha (c) || c == '_')
8572 {
8573 *sval++ = c;
3cf3436e
JR
8574 while (*s < end
8575 && (c = *(*s)++, (isalnum (c) || c == '_')))
6fc2811b
JR
8576 *sval++ = c;
8577 *sval = 0;
3cf3436e
JR
8578 if (*s < end)
8579 *s = *s - 1;
6fc2811b
JR
8580 c = XBM_TK_IDENT;
8581 }
3cf3436e
JR
8582 else if (c == '/' && **s == '*')
8583 {
8584 /* C-style comment. */
8585 ++*s;
8586 while (**s && (**s != '*' || *(*s + 1) != '/'))
8587 ++*s;
8588 if (**s)
8589 {
8590 *s += 2;
8591 goto loop;
8592 }
8593 }
6fc2811b
JR
8594
8595 return c;
8596}
8597
8598
217e5be0
JR
8599/* XBM bits seem to be backward within bytes compared with how
8600 Windows does things. */
8601static unsigned char reflect_byte (unsigned char orig)
8602{
8603 int i;
8604 unsigned char reflected = 0x00;
8605 for (i = 0; i < 8; i++)
8606 {
8607 if (orig & (0x01 << i))
8608 reflected |= 0x80 >> i;
8609 }
8610 return reflected;
8611}
8612
8613
af3f7be7
JR
8614/* Create a Windows bitmap from X bitmap data. */
8615static HBITMAP
8616w32_create_pixmap_from_bitmap_data (int width, int height, char *data)
8617{
8618 int i, j, w1, w2;
8619 char *bits, *p;
8620 HBITMAP bmp;
8621
8622 w1 = (width + 7) / 8; /* nb of 8bits elt in X bitmap */
8623 w2 = ((width + 15) / 16) * 2; /* nb of 16bits elt in W32 bitmap */
c736ffda 8624 bits = (char *) alloca (height * w2);
af3f7be7
JR
8625 bzero (bits, height * w2);
8626 for (i = 0; i < height; i++)
8627 {
8628 p = bits + i*w2;
8629 for (j = 0; j < w1; j++)
8630 *p++ = reflect_byte(*data++);
8631 }
8632 bmp = CreateBitmap (width, height, 1, 1, bits);
af3f7be7
JR
8633
8634 return bmp;
8635}
8636
8637
6fc2811b 8638/* Replacement for XReadBitmapFileData which isn't available under old
3cf3436e
JR
8639 X versions. CONTENTS is a pointer to a buffer to parse; END is the
8640 buffer's end. Set *WIDTH and *HEIGHT to the width and height of
8641 the image. Return in *DATA the bitmap data allocated with xmalloc.
8642 Value is non-zero if successful. DATA null means just test if
8643 CONTENTS looks like an in-memory XBM file. */
6fc2811b
JR
8644
8645static int
3cf3436e
JR
8646xbm_read_bitmap_data (contents, end, width, height, data)
8647 char *contents, *end;
6fc2811b
JR
8648 int *width, *height;
8649 unsigned char **data;
8650{
3cf3436e 8651 char *s = contents;
6fc2811b
JR
8652 char buffer[BUFSIZ];
8653 int padding_p = 0;
8654 int v10 = 0;
af3f7be7 8655 int bytes_per_line, i, nbytes;
6fc2811b
JR
8656 unsigned char *p;
8657 int value;
8658 int LA1;
8659
8660#define match() \
217e5be0 8661 LA1 = xbm_scan (&s, end, buffer, &value)
6fc2811b
JR
8662
8663#define expect(TOKEN) \
8664 if (LA1 != (TOKEN)) \
8665 goto failure; \
8666 else \
7d0393cf 8667 match ()
6fc2811b
JR
8668
8669#define expect_ident(IDENT) \
8670 if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \
8671 match (); \
8672 else \
8673 goto failure
8674
6fc2811b 8675 *width = *height = -1;
3cf3436e
JR
8676 if (data)
8677 *data = NULL;
8678 LA1 = xbm_scan (&s, end, buffer, &value);
6fc2811b
JR
8679
8680 /* Parse defines for width, height and hot-spots. */
8681 while (LA1 == '#')
8682 {
8683 match ();
8684 expect_ident ("define");
8685 expect (XBM_TK_IDENT);
8686
8687 if (LA1 == XBM_TK_NUMBER);
8688 {
8689 char *p = strrchr (buffer, '_');
8690 p = p ? p + 1 : buffer;
8691 if (strcmp (p, "width") == 0)
8692 *width = value;
8693 else if (strcmp (p, "height") == 0)
8694 *height = value;
8695 }
8696 expect (XBM_TK_NUMBER);
8697 }
8698
8699 if (*width < 0 || *height < 0)
8700 goto failure;
3cf3436e
JR
8701 else if (data == NULL)
8702 goto success;
6fc2811b
JR
8703
8704 /* Parse bits. Must start with `static'. */
8705 expect_ident ("static");
8706 if (LA1 == XBM_TK_IDENT)
8707 {
8708 if (strcmp (buffer, "unsigned") == 0)
8709 {
7d0393cf 8710 match ();
6fc2811b
JR
8711 expect_ident ("char");
8712 }
8713 else if (strcmp (buffer, "short") == 0)
8714 {
8715 match ();
8716 v10 = 1;
af3f7be7
JR
8717 if (*width % 16 && *width % 16 < 9)
8718 padding_p = 1;
6fc2811b
JR
8719 }
8720 else if (strcmp (buffer, "char") == 0)
8721 match ();
8722 else
8723 goto failure;
8724 }
7d0393cf 8725 else
6fc2811b
JR
8726 goto failure;
8727
8728 expect (XBM_TK_IDENT);
8729 expect ('[');
8730 expect (']');
8731 expect ('=');
8732 expect ('{');
8733
af3f7be7
JR
8734 bytes_per_line = (*width + 7) / 8 + padding_p;
8735 nbytes = bytes_per_line * *height;
8736 p = *data = (char *) xmalloc (nbytes);
6fc2811b
JR
8737
8738 if (v10)
8739 {
6fc2811b
JR
8740 for (i = 0; i < nbytes; i += 2)
8741 {
8742 int val = value;
8743 expect (XBM_TK_NUMBER);
8744
35624c03 8745 *p++ = ~ val;
af3f7be7 8746 if (!padding_p || ((i + 2) % bytes_per_line))
35624c03 8747 *p++ = ~ (value >> 8);
7d0393cf 8748
6fc2811b
JR
8749 if (LA1 == ',' || LA1 == '}')
8750 match ();
8751 else
8752 goto failure;
8753 }
8754 }
8755 else
8756 {
8757 for (i = 0; i < nbytes; ++i)
8758 {
8759 int val = value;
8760 expect (XBM_TK_NUMBER);
7d0393cf 8761
35624c03 8762 *p++ = ~ val;
217e5be0 8763
6fc2811b
JR
8764 if (LA1 == ',' || LA1 == '}')
8765 match ();
8766 else
8767 goto failure;
8768 }
8769 }
8770
3cf3436e 8771 success:
6fc2811b
JR
8772 return 1;
8773
8774 failure:
3cf3436e
JR
8775
8776 if (data && *data)
6fc2811b
JR
8777 {
8778 xfree (*data);
8779 *data = NULL;
8780 }
8781 return 0;
8782
8783#undef match
8784#undef expect
8785#undef expect_ident
8786}
8787
516eea8e
JR
8788static void convert_mono_to_color_image (f, img, foreground, background)
8789 struct frame *f;
8790 struct image *img;
8791 COLORREF foreground, background;
8792{
8793 HDC hdc, old_img_dc, new_img_dc;
8794 HGDIOBJ old_prev, new_prev;
8795 HBITMAP new_pixmap;
8796
8797 hdc = get_frame_dc (f);
8798 old_img_dc = CreateCompatibleDC (hdc);
8799 new_img_dc = CreateCompatibleDC (hdc);
8800 new_pixmap = CreateCompatibleBitmap (hdc, img->width, img->height);
8801 release_frame_dc (f, hdc);
8802 old_prev = SelectObject (old_img_dc, img->pixmap);
8803 new_prev = SelectObject (new_img_dc, new_pixmap);
8804 SetTextColor (new_img_dc, foreground);
8805 SetBkColor (new_img_dc, background);
8806
8807 BitBlt (new_img_dc, 0, 0, img->width, img->height, old_img_dc,
8808 0, 0, SRCCOPY);
8809
8810 SelectObject (old_img_dc, old_prev);
c922a224 8811 SelectObject (new_img_dc, new_prev);
516eea8e
JR
8812 DeleteDC (old_img_dc);
8813 DeleteDC (new_img_dc);
8814 DeleteObject (img->pixmap);
8815 if (new_pixmap == 0)
8816 fprintf (stderr, "Failed to convert image to color.\n");
8817 else
8818 img->pixmap = new_pixmap;
8819}
6fc2811b 8820
3cf3436e
JR
8821/* Load XBM image IMG which will be displayed on frame F from buffer
8822 CONTENTS. END is the end of the buffer. Value is non-zero if
8823 successful. */
6fc2811b
JR
8824
8825static int
3cf3436e 8826xbm_load_image (f, img, contents, end)
6fc2811b
JR
8827 struct frame *f;
8828 struct image *img;
3cf3436e 8829 char *contents, *end;
6fc2811b
JR
8830{
8831 int rc;
8832 unsigned char *data;
8833 int success_p = 0;
7d0393cf 8834
3cf3436e 8835 rc = xbm_read_bitmap_data (contents, end, &img->width, &img->height, &data);
6fc2811b
JR
8836 if (rc)
8837 {
6fc2811b
JR
8838 unsigned long foreground = FRAME_FOREGROUND_PIXEL (f);
8839 unsigned long background = FRAME_BACKGROUND_PIXEL (f);
516eea8e 8840 int non_default_colors = 0;
6fc2811b 8841 Lisp_Object value;
7d0393cf 8842
6fc2811b
JR
8843 xassert (img->width > 0 && img->height > 0);
8844
8845 /* Get foreground and background colors, maybe allocate colors. */
8846 value = image_spec_value (img->spec, QCforeground, NULL);
8847 if (!NILP (value))
516eea8e
JR
8848 {
8849 foreground = x_alloc_image_color (f, img, value, foreground);
8850 non_default_colors = 1;
8851 }
6fc2811b
JR
8852 value = image_spec_value (img->spec, QCbackground, NULL);
8853 if (!NILP (value))
a05e2bae
JR
8854 {
8855 background = x_alloc_image_color (f, img, value, background);
8856 img->background = background;
8857 img->background_valid = 1;
516eea8e 8858 non_default_colors = 1;
a05e2bae 8859 }
6fc2811b 8860 img->pixmap
af3f7be7 8861 = w32_create_pixmap_from_bitmap_data (img->width, img->height, data);
ac849ba4 8862
516eea8e
JR
8863 /* If colors were specified, transfer the bitmap to a color one. */
8864 if (non_default_colors)
8865 convert_mono_to_color_image (f, img, foreground, background);
8866
6fc2811b
JR
8867 xfree (data);
8868
8869 if (img->pixmap == 0)
8870 {
8871 x_clear_image (f, img);
3cf3436e 8872 image_error ("Unable to create X pixmap for `%s'", img->spec, Qnil);
6fc2811b
JR
8873 }
8874 else
8875 success_p = 1;
6fc2811b
JR
8876 }
8877 else
8878 image_error ("Error loading XBM image `%s'", img->spec, Qnil);
8879
6fc2811b
JR
8880 return success_p;
8881}
8882
8883
3cf3436e
JR
8884/* Value is non-zero if DATA looks like an in-memory XBM file. */
8885
8886static int
8887xbm_file_p (data)
8888 Lisp_Object data;
8889{
8890 int w, h;
8891 return (STRINGP (data)
d5db4077
KR
8892 && xbm_read_bitmap_data (SDATA (data),
8893 (SDATA (data)
8894 + SBYTES (data)),
3cf3436e
JR
8895 &w, &h, NULL));
8896}
8897
7d0393cf 8898
6fc2811b
JR
8899/* Fill image IMG which is used on frame F with pixmap data. Value is
8900 non-zero if successful. */
8901
8902static int
8903xbm_load (f, img)
8904 struct frame *f;
8905 struct image *img;
8906{
8907 int success_p = 0;
8908 Lisp_Object file_name;
8909
8910 xassert (xbm_image_p (img->spec));
8911
8912 /* If IMG->spec specifies a file name, create a non-file spec from it. */
8913 file_name = image_spec_value (img->spec, QCfile, NULL);
8914 if (STRINGP (file_name))
3cf3436e
JR
8915 {
8916 Lisp_Object file;
8917 char *contents;
8918 int size;
8919 struct gcpro gcpro1;
8920
8921 file = x_find_image_file (file_name);
8922 GCPRO1 (file);
8923 if (!STRINGP (file))
8924 {
8925 image_error ("Cannot find image file `%s'", file_name, Qnil);
8926 UNGCPRO;
8927 return 0;
8928 }
8929
d5db4077 8930 contents = slurp_file (SDATA (file), &size);
3cf3436e
JR
8931 if (contents == NULL)
8932 {
8933 image_error ("Error loading XBM image `%s'", img->spec, Qnil);
8934 UNGCPRO;
8935 return 0;
8936 }
8937
8938 success_p = xbm_load_image (f, img, contents, contents + size);
8939 UNGCPRO;
8940 }
6fc2811b
JR
8941 else
8942 {
8943 struct image_keyword fmt[XBM_LAST];
8944 Lisp_Object data;
6fc2811b
JR
8945 unsigned long foreground = FRAME_FOREGROUND_PIXEL (f);
8946 unsigned long background = FRAME_BACKGROUND_PIXEL (f);
516eea8e 8947 int non_default_colors = 0;
6fc2811b
JR
8948 char *bits;
8949 int parsed_p;
3cf3436e
JR
8950 int in_memory_file_p = 0;
8951
8952 /* See if data looks like an in-memory XBM file. */
8953 data = image_spec_value (img->spec, QCdata, NULL);
8954 in_memory_file_p = xbm_file_p (data);
6fc2811b 8955
217e5be0 8956 /* Parse the image specification. */
6fc2811b
JR
8957 bcopy (xbm_format, fmt, sizeof fmt);
8958 parsed_p = parse_image_spec (img->spec, fmt, XBM_LAST, Qxbm);
8959 xassert (parsed_p);
8960
8961 /* Get specified width, and height. */
3cf3436e
JR
8962 if (!in_memory_file_p)
8963 {
8964 img->width = XFASTINT (fmt[XBM_WIDTH].value);
8965 img->height = XFASTINT (fmt[XBM_HEIGHT].value);
8966 xassert (img->width > 0 && img->height > 0);
8967 }
217e5be0 8968
6fc2811b 8969 /* Get foreground and background colors, maybe allocate colors. */
3cf3436e
JR
8970 if (fmt[XBM_FOREGROUND].count
8971 && STRINGP (fmt[XBM_FOREGROUND].value))
516eea8e
JR
8972 {
8973 foreground = x_alloc_image_color (f, img, fmt[XBM_FOREGROUND].value,
8974 foreground);
8975 non_default_colors = 1;
8976 }
8977
3cf3436e
JR
8978 if (fmt[XBM_BACKGROUND].count
8979 && STRINGP (fmt[XBM_BACKGROUND].value))
516eea8e
JR
8980 {
8981 background = x_alloc_image_color (f, img, fmt[XBM_BACKGROUND].value,
8982 background);
8983 non_default_colors = 1;
8984 }
6fc2811b 8985
3cf3436e 8986 if (in_memory_file_p)
d5db4077
KR
8987 success_p = xbm_load_image (f, img, SDATA (data),
8988 (SDATA (data)
8989 + SBYTES (data)));
3cf3436e 8990 else
6fc2811b 8991 {
3cf3436e
JR
8992 if (VECTORP (data))
8993 {
8994 int i;
8995 char *p;
8996 int nbytes = (img->width + BITS_PER_CHAR - 1) / BITS_PER_CHAR;
7d0393cf 8997
3cf3436e
JR
8998 p = bits = (char *) alloca (nbytes * img->height);
8999 for (i = 0; i < img->height; ++i, p += nbytes)
9000 {
9001 Lisp_Object line = XVECTOR (data)->contents[i];
9002 if (STRINGP (line))
d5db4077 9003 bcopy (SDATA (line), p, nbytes);
3cf3436e
JR
9004 else
9005 bcopy (XBOOL_VECTOR (line)->data, p, nbytes);
9006 }
9007 }
9008 else if (STRINGP (data))
d5db4077 9009 bits = SDATA (data);
3cf3436e
JR
9010 else
9011 bits = XBOOL_VECTOR (data)->data;
af3f7be7 9012
3cf3436e 9013 /* Create the pixmap. */
3cf3436e 9014 img->pixmap
af3f7be7
JR
9015 = w32_create_pixmap_from_bitmap_data (img->width, img->height,
9016 bits);
9017
516eea8e
JR
9018 /* If colors were specified, transfer the bitmap to a color one. */
9019 if (non_default_colors)
9020 convert_mono_to_color_image (f, img, foreground, background);
9021
3cf3436e
JR
9022 if (img->pixmap)
9023 success_p = 1;
9024 else
6fc2811b 9025 {
3cf3436e
JR
9026 image_error ("Unable to create pixmap for XBM image `%s'",
9027 img->spec, Qnil);
9028 x_clear_image (f, img);
6fc2811b
JR
9029 }
9030 }
6fc2811b
JR
9031 }
9032
9033 return success_p;
9034}
7d0393cf 9035
6fc2811b
JR
9036
9037\f
9038/***********************************************************************
9039 XPM images
9040 ***********************************************************************/
9041
7d0393cf 9042#if HAVE_XPM
6fc2811b
JR
9043
9044static int xpm_image_p P_ ((Lisp_Object object));
9045static int xpm_load P_ ((struct frame *f, struct image *img));
9046static int xpm_valid_color_symbols_p P_ ((Lisp_Object));
9047
c736ffda
JR
9048/* Indicate to xpm.h that we don't have Xlib. */
9049#define FOR_MSW
9050/* simx.h in xpm defines XColor and XImage differently than Emacs. */
9051#define XColor xpm_XColor
9052#define XImage xpm_XImage
9053#define PIXEL_ALREADY_TYPEDEFED
6fc2811b 9054#include "X11/xpm.h"
c736ffda
JR
9055#undef FOR_MSW
9056#undef XColor
9057#undef XImage
9058#undef PIXEL_ALREADY_TYPEDEFED
6fc2811b
JR
9059
9060/* The symbol `xpm' identifying XPM-format images. */
9061
9062Lisp_Object Qxpm;
9063
9064/* Indices of image specification fields in xpm_format, below. */
9065
9066enum xpm_keyword_index
9067{
9068 XPM_TYPE,
9069 XPM_FILE,
9070 XPM_DATA,
9071 XPM_ASCENT,
9072 XPM_MARGIN,
9073 XPM_RELIEF,
9074 XPM_ALGORITHM,
9075 XPM_HEURISTIC_MASK,
a05e2bae 9076 XPM_MASK,
6fc2811b 9077 XPM_COLOR_SYMBOLS,
a05e2bae 9078 XPM_BACKGROUND,
6fc2811b
JR
9079 XPM_LAST
9080};
9081
9082/* Vector of image_keyword structures describing the format
9083 of valid XPM image specifications. */
9084
9085static struct image_keyword xpm_format[XPM_LAST] =
9086{
9087 {":type", IMAGE_SYMBOL_VALUE, 1},
9088 {":file", IMAGE_STRING_VALUE, 0},
9089 {":data", IMAGE_STRING_VALUE, 0},
8f92c555 9090 {":ascent", IMAGE_ASCENT_VALUE, 0},
8edb0a6f 9091 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
6fc2811b 9092 {":relief", IMAGE_INTEGER_VALUE, 0},
a93f4566 9093 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6fc2811b 9094 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
a05e2bae
JR
9095 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9096 {":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9097 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
6fc2811b
JR
9098};
9099
197edd35 9100/* Structure describing the image type XPM. */
6fc2811b
JR
9101
9102static struct image_type xpm_type =
9103{
9104 &Qxpm,
9105 xpm_image_p,
9106 xpm_load,
9107 x_clear_image,
9108 NULL
9109};
9110
9111
c736ffda
JR
9112/* XPM library details. */
9113
9114DEF_IMGLIB_FN (XpmFreeAttributes);
9115DEF_IMGLIB_FN (XpmCreateImageFromBuffer);
9116DEF_IMGLIB_FN (XpmReadFileToImage);
9117DEF_IMGLIB_FN (XImageFree);
9118
9119
9120static int
9121init_xpm_functions (library)
9122 HMODULE library;
9123{
9124 LOAD_IMGLIB_FN (library, XpmFreeAttributes);
9125 LOAD_IMGLIB_FN (library, XpmCreateImageFromBuffer);
9126 LOAD_IMGLIB_FN (library, XpmReadFileToImage);
9127 LOAD_IMGLIB_FN (library, XImageFree);
9128
9129 return 1;
9130}
9131
6fc2811b
JR
9132/* Value is non-zero if COLOR_SYMBOLS is a valid color symbols list
9133 for XPM images. Such a list must consist of conses whose car and
9134 cdr are strings. */
9135
9136static int
9137xpm_valid_color_symbols_p (color_symbols)
9138 Lisp_Object color_symbols;
9139{
9140 while (CONSP (color_symbols))
9141 {
9142 Lisp_Object sym = XCAR (color_symbols);
9143 if (!CONSP (sym)
9144 || !STRINGP (XCAR (sym))
9145 || !STRINGP (XCDR (sym)))
9146 break;
9147 color_symbols = XCDR (color_symbols);
9148 }
9149
9150 return NILP (color_symbols);
9151}
9152
9153
9154/* Value is non-zero if OBJECT is a valid XPM image specification. */
9155
9156static int
9157xpm_image_p (object)
9158 Lisp_Object object;
9159{
9160 struct image_keyword fmt[XPM_LAST];
9161 bcopy (xpm_format, fmt, sizeof fmt);
9162 return (parse_image_spec (object, fmt, XPM_LAST, Qxpm)
9163 /* Either `:file' or `:data' must be present. */
9164 && fmt[XPM_FILE].count + fmt[XPM_DATA].count == 1
9165 /* Either no `:color-symbols' or it's a list of conses
9166 whose car and cdr are strings. */
9167 && (fmt[XPM_COLOR_SYMBOLS].count == 0
8f92c555 9168 || xpm_valid_color_symbols_p (fmt[XPM_COLOR_SYMBOLS].value)));
6fc2811b
JR
9169}
9170
9171
9172/* Load image IMG which will be displayed on frame F. Value is
9173 non-zero if successful. */
9174
9175static int
9176xpm_load (f, img)
9177 struct frame *f;
9178 struct image *img;
9179{
c736ffda
JR
9180 HDC hdc;
9181 int rc;
6fc2811b
JR
9182 XpmAttributes attrs;
9183 Lisp_Object specified_file, color_symbols;
c736ffda 9184 xpm_XImage * xpm_image, * xpm_mask;
6fc2811b
JR
9185
9186 /* Configure the XPM lib. Use the visual of frame F. Allocate
9187 close colors. Return colors allocated. */
9188 bzero (&attrs, sizeof attrs);
c736ffda
JR
9189 xpm_image = xpm_mask = NULL;
9190
9191#if 0
dfff8a69
JR
9192 attrs.visual = FRAME_X_VISUAL (f);
9193 attrs.colormap = FRAME_X_COLORMAP (f);
6fc2811b 9194 attrs.valuemask |= XpmVisual;
dfff8a69 9195 attrs.valuemask |= XpmColormap;
c736ffda 9196#endif
6fc2811b 9197 attrs.valuemask |= XpmReturnAllocPixels;
dfff8a69 9198#ifdef XpmAllocCloseColors
6fc2811b
JR
9199 attrs.alloc_close_colors = 1;
9200 attrs.valuemask |= XpmAllocCloseColors;
dfff8a69
JR
9201#else
9202 attrs.closeness = 600;
9203 attrs.valuemask |= XpmCloseness;
9204#endif
6fc2811b
JR
9205
9206 /* If image specification contains symbolic color definitions, add
9207 these to `attrs'. */
9208 color_symbols = image_spec_value (img->spec, QCcolor_symbols, NULL);
9209 if (CONSP (color_symbols))
9210 {
9211 Lisp_Object tail;
9212 XpmColorSymbol *xpm_syms;
9213 int i, size;
7d0393cf 9214
6fc2811b
JR
9215 attrs.valuemask |= XpmColorSymbols;
9216
9217 /* Count number of symbols. */
9218 attrs.numsymbols = 0;
9219 for (tail = color_symbols; CONSP (tail); tail = XCDR (tail))
9220 ++attrs.numsymbols;
9221
9222 /* Allocate an XpmColorSymbol array. */
9223 size = attrs.numsymbols * sizeof *xpm_syms;
9224 xpm_syms = (XpmColorSymbol *) alloca (size);
9225 bzero (xpm_syms, size);
9226 attrs.colorsymbols = xpm_syms;
9227
9228 /* Fill the color symbol array. */
9229 for (tail = color_symbols, i = 0;
9230 CONSP (tail);
9231 ++i, tail = XCDR (tail))
9232 {
9233 Lisp_Object name = XCAR (XCAR (tail));
9234 Lisp_Object color = XCDR (XCAR (tail));
d5db4077
KR
9235 xpm_syms[i].name = (char *) alloca (SCHARS (name) + 1);
9236 strcpy (xpm_syms[i].name, SDATA (name));
9237 xpm_syms[i].value = (char *) alloca (SCHARS (color) + 1);
9238 strcpy (xpm_syms[i].value, SDATA (color));
6fc2811b
JR
9239 }
9240 }
9241
9242 /* Create a pixmap for the image, either from a file, or from a
9243 string buffer containing data in the same format as an XPM file. */
c736ffda 9244
6fc2811b 9245 specified_file = image_spec_value (img->spec, QCfile, NULL);
177c0ea7 9246
c736ffda
JR
9247 {
9248 HDC frame_dc = get_frame_dc (f);
9249 hdc = CreateCompatibleDC (frame_dc);
9250 release_frame_dc (f, frame_dc);
9251 }
9252
6fc2811b
JR
9253 if (STRINGP (specified_file))
9254 {
9255 Lisp_Object file = x_find_image_file (specified_file);
9256 if (!STRINGP (file))
9257 {
9258 image_error ("Cannot find image file `%s'", specified_file, Qnil);
6fc2811b
JR
9259 return 0;
9260 }
7d0393cf 9261
c736ffda
JR
9262 /* XpmReadFileToPixmap is not available in the Windows port of
9263 libxpm. But XpmReadFileToImage almost does what we want. */
9264 rc = fn_XpmReadFileToImage (&hdc, SDATA (file),
9265 &xpm_image, &xpm_mask,
9266 &attrs);
6fc2811b
JR
9267 }
9268 else
9269 {
9270 Lisp_Object buffer = image_spec_value (img->spec, QCdata, NULL);
c736ffda
JR
9271 /* XpmCreatePixmapFromBuffer is not available in the Windows port
9272 of libxpm. But XpmCreateImageFromBuffer almost does what we want. */
9273 rc = fn_XpmCreateImageFromBuffer (&hdc, SDATA (buffer),
9274 &xpm_image, &xpm_mask,
9275 &attrs);
6fc2811b 9276 }
6fc2811b
JR
9277
9278 if (rc == XpmSuccess)
9279 {
c736ffda
JR
9280 int i;
9281
9282 /* W32 XPM uses XImage to wrap what W32 Emacs calls a Pixmap,
9283 plus some duplicate attributes. */
9284 if (xpm_image && xpm_image->bitmap)
9285 {
9286 img->pixmap = xpm_image->bitmap;
9287 /* XImageFree in libXpm frees XImage struct without destroying
9288 the bitmap, which is what we want. */
9289 fn_XImageFree (xpm_image);
9290 }
9291 if (xpm_mask && xpm_mask->bitmap)
177c0ea7 9292 {
c736ffda
JR
9293 /* The mask appears to be inverted compared with what we expect.
9294 TODO: invert our expectations. See other places where we
9295 have to invert bits because our idea of masks is backwards. */
9296 HGDIOBJ old_obj;
9297 old_obj = SelectObject (hdc, xpm_mask->bitmap);
9298
9299 PatBlt (hdc, 0, 0, xpm_mask->width, xpm_mask->height, DSTINVERT);
9300 SelectObject (hdc, old_obj);
9301
9302 img->mask = xpm_mask->bitmap;
177c0ea7 9303 fn_XImageFree (xpm_mask);
c736ffda
JR
9304 DeleteDC (hdc);
9305 }
9306
9307 DeleteDC (hdc);
9308
6fc2811b
JR
9309 /* Remember allocated colors. */
9310 img->ncolors = attrs.nalloc_pixels;
9311 img->colors = (unsigned long *) xmalloc (img->ncolors
9312 * sizeof *img->colors);
9313 for (i = 0; i < attrs.nalloc_pixels; ++i)
9314 img->colors[i] = attrs.alloc_pixels[i];
9315
9316 img->width = attrs.width;
9317 img->height = attrs.height;
9318 xassert (img->width > 0 && img->height > 0);
9319
9320 /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */
c736ffda 9321 fn_XpmFreeAttributes (&attrs);
6fc2811b
JR
9322 }
9323 else
9324 {
c736ffda
JR
9325 DeleteDC (hdc);
9326
6fc2811b
JR
9327 switch (rc)
9328 {
9329 case XpmOpenFailed:
9330 image_error ("Error opening XPM file (%s)", img->spec, Qnil);
9331 break;
7d0393cf 9332
6fc2811b
JR
9333 case XpmFileInvalid:
9334 image_error ("Invalid XPM file (%s)", img->spec, Qnil);
9335 break;
7d0393cf 9336
6fc2811b
JR
9337 case XpmNoMemory:
9338 image_error ("Out of memory (%s)", img->spec, Qnil);
9339 break;
7d0393cf 9340
6fc2811b
JR
9341 case XpmColorFailed:
9342 image_error ("Color allocation error (%s)", img->spec, Qnil);
9343 break;
7d0393cf 9344
6fc2811b
JR
9345 default:
9346 image_error ("Unknown error (%s)", img->spec, Qnil);
9347 break;
9348 }
9349 }
9350
9351 return rc == XpmSuccess;
9352}
9353
9354#endif /* HAVE_XPM != 0 */
9355
9356\f
767b1ff0 9357#if 0 /* TODO : Color tables on W32. */
6fc2811b
JR
9358/***********************************************************************
9359 Color table
9360 ***********************************************************************/
9361
9362/* An entry in the color table mapping an RGB color to a pixel color. */
9363
9364struct ct_color
9365{
9366 int r, g, b;
9367 unsigned long pixel;
9368
9369 /* Next in color table collision list. */
9370 struct ct_color *next;
9371};
9372
9373/* The bucket vector size to use. Must be prime. */
9374
9375#define CT_SIZE 101
9376
9377/* Value is a hash of the RGB color given by R, G, and B. */
9378
9379#define CT_HASH_RGB(R, G, B) (((R) << 16) ^ ((G) << 8) ^ (B))
9380
9381/* The color hash table. */
9382
9383struct ct_color **ct_table;
9384
9385/* Number of entries in the color table. */
9386
9387int ct_colors_allocated;
9388
9389/* Function prototypes. */
9390
9391static void init_color_table P_ ((void));
9392static void free_color_table P_ ((void));
9393static unsigned long *colors_in_color_table P_ ((int *n));
9394static unsigned long lookup_rgb_color P_ ((struct frame *f, int r, int g, int b));
9395static unsigned long lookup_pixel_color P_ ((struct frame *f, unsigned long p));
9396
9397
9398/* Initialize the color table. */
9399
9400static void
9401init_color_table ()
9402{
9403 int size = CT_SIZE * sizeof (*ct_table);
9404 ct_table = (struct ct_color **) xmalloc (size);
9405 bzero (ct_table, size);
9406 ct_colors_allocated = 0;
9407}
9408
9409
9410/* Free memory associated with the color table. */
9411
9412static void
9413free_color_table ()
9414{
9415 int i;
9416 struct ct_color *p, *next;
9417
9418 for (i = 0; i < CT_SIZE; ++i)
9419 for (p = ct_table[i]; p; p = next)
9420 {
9421 next = p->next;
9422 xfree (p);
9423 }
9424
9425 xfree (ct_table);
9426 ct_table = NULL;
9427}
9428
9429
9430/* Value is a pixel color for RGB color R, G, B on frame F. If an
9431 entry for that color already is in the color table, return the
9432 pixel color of that entry. Otherwise, allocate a new color for R,
9433 G, B, and make an entry in the color table. */
9434
9435static unsigned long
9436lookup_rgb_color (f, r, g, b)
9437 struct frame *f;
9438 int r, g, b;
9439{
9440 unsigned hash = CT_HASH_RGB (r, g, b);
9441 int i = hash % CT_SIZE;
9442 struct ct_color *p;
9443
9444 for (p = ct_table[i]; p; p = p->next)
9445 if (p->r == r && p->g == g && p->b == b)
9446 break;
9447
9448 if (p == NULL)
9449 {
9450 COLORREF color;
9451 Colormap cmap;
9452 int rc;
9453
9454 color = PALETTERGB (r, g, b);
9455
9456 ++ct_colors_allocated;
9457
9458 p = (struct ct_color *) xmalloc (sizeof *p);
9459 p->r = r;
9460 p->g = g;
9461 p->b = b;
9462 p->pixel = color;
9463 p->next = ct_table[i];
9464 ct_table[i] = p;
9465 }
9466
9467 return p->pixel;
9468}
9469
9470
9471/* Look up pixel color PIXEL which is used on frame F in the color
9472 table. If not already present, allocate it. Value is PIXEL. */
9473
9474static unsigned long
9475lookup_pixel_color (f, pixel)
9476 struct frame *f;
9477 unsigned long pixel;
9478{
9479 int i = pixel % CT_SIZE;
9480 struct ct_color *p;
9481
9482 for (p = ct_table[i]; p; p = p->next)
9483 if (p->pixel == pixel)
9484 break;
9485
9486 if (p == NULL)
9487 {
9488 XColor color;
9489 Colormap cmap;
9490 int rc;
9491
9492 BLOCK_INPUT;
7d0393cf 9493
6fc2811b
JR
9494 cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f));
9495 color.pixel = pixel;
9496 XQueryColor (NULL, cmap, &color);
9497 rc = x_alloc_nearest_color (f, cmap, &color);
9498 UNBLOCK_INPUT;
9499
9500 if (rc)
9501 {
9502 ++ct_colors_allocated;
7d0393cf 9503
6fc2811b
JR
9504 p = (struct ct_color *) xmalloc (sizeof *p);
9505 p->r = color.red;
9506 p->g = color.green;
9507 p->b = color.blue;
9508 p->pixel = pixel;
9509 p->next = ct_table[i];
9510 ct_table[i] = p;
9511 }
9512 else
9513 return FRAME_FOREGROUND_PIXEL (f);
9514 }
9515 return p->pixel;
9516}
9517
9518
9519/* Value is a vector of all pixel colors contained in the color table,
9520 allocated via xmalloc. Set *N to the number of colors. */
9521
9522static unsigned long *
9523colors_in_color_table (n)
9524 int *n;
9525{
9526 int i, j;
9527 struct ct_color *p;
9528 unsigned long *colors;
9529
9530 if (ct_colors_allocated == 0)
9531 {
9532 *n = 0;
9533 colors = NULL;
9534 }
9535 else
9536 {
9537 colors = (unsigned long *) xmalloc (ct_colors_allocated
9538 * sizeof *colors);
9539 *n = ct_colors_allocated;
7d0393cf 9540
6fc2811b
JR
9541 for (i = j = 0; i < CT_SIZE; ++i)
9542 for (p = ct_table[i]; p; p = p->next)
9543 colors[j++] = p->pixel;
9544 }
9545
9546 return colors;
9547}
9548
767b1ff0 9549#endif /* TODO */
6fc2811b
JR
9550
9551\f
9552/***********************************************************************
9553 Algorithms
9554 ***********************************************************************/
3cf3436e
JR
9555static XColor *x_to_xcolors P_ ((struct frame *, struct image *, int));
9556static void x_from_xcolors P_ ((struct frame *, struct image *, XColor *));
9557static void x_detect_edges P_ ((struct frame *, struct image *, int[9], int));
ac849ba4 9558static void XPutPixel (XImage *, int, int, COLORREF);
3cf3436e
JR
9559
9560/* Non-zero means draw a cross on images having `:conversion
9561 disabled'. */
6fc2811b 9562
3cf3436e 9563int cross_disabled_images;
6fc2811b 9564
3cf3436e
JR
9565/* Edge detection matrices for different edge-detection
9566 strategies. */
6fc2811b 9567
3cf3436e
JR
9568static int emboss_matrix[9] = {
9569 /* x - 1 x x + 1 */
9570 2, -1, 0, /* y - 1 */
9571 -1, 0, 1, /* y */
9572 0, 1, -2 /* y + 1 */
9573};
9574
9575static int laplace_matrix[9] = {
9576 /* x - 1 x x + 1 */
9577 1, 0, 0, /* y - 1 */
9578 0, 0, 0, /* y */
9579 0, 0, -1 /* y + 1 */
9580};
9581
9582/* Value is the intensity of the color whose red/green/blue values
9583 are R, G, and B. */
9584
9585#define COLOR_INTENSITY(R, G, B) ((2 * (R) + 3 * (G) + (B)) / 6)
9586
9587
9588/* On frame F, return an array of XColor structures describing image
9589 IMG->pixmap. Each XColor structure has its pixel color set. RGB_P
9590 non-zero means also fill the red/green/blue members of the XColor
9591 structures. Value is a pointer to the array of XColors structures,
9592 allocated with xmalloc; it must be freed by the caller. */
9593
9594static XColor *
9595x_to_xcolors (f, img, rgb_p)
9596 struct frame *f;
9597 struct image *img;
9598 int rgb_p;
9599{
9600 int x, y;
9601 XColor *colors, *p;
197edd35
JR
9602 HDC hdc, bmpdc;
9603 HGDIOBJ prev;
3cf3436e
JR
9604
9605 colors = (XColor *) xmalloc (img->width * img->height * sizeof *colors);
197edd35
JR
9606
9607 /* Load the image into a memory device context. */
9608 hdc = get_frame_dc (f);
9609 bmpdc = CreateCompatibleDC (hdc);
9610 release_frame_dc (f, hdc);
9611 prev = SelectObject (bmpdc, img->pixmap);
3cf3436e
JR
9612
9613 /* Fill the `pixel' members of the XColor array. I wished there
9614 were an easy and portable way to circumvent XGetPixel. */
9615 p = colors;
9616 for (y = 0; y < img->height; ++y)
9617 {
9618 XColor *row = p;
7d0393cf 9619
3cf3436e 9620 for (x = 0; x < img->width; ++x, ++p)
197edd35
JR
9621 {
9622 /* TODO: palette support needed here? */
9623 p->pixel = GetPixel (bmpdc, x, y);
3cf3436e 9624
197edd35
JR
9625 if (rgb_p)
9626 {
9627 p->red = 256 * GetRValue (p->pixel);
9628 p->green = 256 * GetGValue (p->pixel);
9629 p->blue = 256 * GetBValue (p->pixel);
9630 }
9631 }
3cf3436e
JR
9632 }
9633
197edd35
JR
9634 SelectObject (bmpdc, prev);
9635 DeleteDC (bmpdc);
9636
3cf3436e
JR
9637 return colors;
9638}
9639
ac849ba4
JR
9640/* Put a pixel of COLOR at position X, Y in XIMG. XIMG must have been
9641 created with CreateDIBSection, with the pointer to the bit values
9642 stored in ximg->data. */
9643
9644static void XPutPixel (ximg, x, y, color)
9645 XImage * ximg;
9646 int x, y;
9647 COLORREF color;
9648{
9649 int width = ximg->info.bmiHeader.biWidth;
9650 int height = ximg->info.bmiHeader.biHeight;
ac849ba4
JR
9651 unsigned char * pixel;
9652
54eefef1
JR
9653 /* True color images. */
9654 if (ximg->info.bmiHeader.biBitCount == 24)
9655 {
9656 int rowbytes = width * 3;
9657 /* Ensure scanlines are aligned on 4 byte boundaries. */
9658 if (rowbytes % 4)
9659 rowbytes += 4 - (rowbytes % 4);
9660
9661 pixel = ximg->data + y * rowbytes + x * 3;
9662 /* Windows bitmaps are in BGR order. */
9663 *pixel = GetBValue (color);
9664 *(pixel + 1) = GetGValue (color);
9665 *(pixel + 2) = GetRValue (color);
9666 }
9667 /* Monochrome images. */
9668 else if (ximg->info.bmiHeader.biBitCount == 1)
9669 {
9670 int rowbytes = width / 8;
9671 /* Ensure scanlines are aligned on 4 byte boundaries. */
9672 if (rowbytes % 4)
9673 rowbytes += 4 - (rowbytes % 4);
9674 pixel = ximg->data + y * rowbytes + x / 8;
9675 /* Filter out palette info. */
9676 if (color & 0x00ffffff)
9677 *pixel = *pixel | (1 << x % 8);
9678 else
9679 *pixel = *pixel & ~(1 << x % 8);
9680 }
9681 else
7c402969 9682 image_error ("XPutPixel: palette image not supported", Qnil, Qnil);
ac849ba4
JR
9683}
9684
3cf3436e
JR
9685/* Create IMG->pixmap from an array COLORS of XColor structures, whose
9686 RGB members are set. F is the frame on which this all happens.
9687 COLORS will be freed; an existing IMG->pixmap will be freed, too. */
6fc2811b
JR
9688
9689static void
3cf3436e 9690x_from_xcolors (f, img, colors)
6fc2811b 9691 struct frame *f;
3cf3436e 9692 struct image *img;
6fc2811b 9693 XColor *colors;
6fc2811b 9694{
3cf3436e
JR
9695 int x, y;
9696 XImage *oimg;
9697 Pixmap pixmap;
9698 XColor *p;
ac849ba4 9699#if 0 /* TODO: color tables. */
3cf3436e 9700 init_color_table ();
ac849ba4 9701#endif
3cf3436e
JR
9702 x_create_x_image_and_pixmap (f, img->width, img->height, 0,
9703 &oimg, &pixmap);
9704 p = colors;
9705 for (y = 0; y < img->height; ++y)
9706 for (x = 0; x < img->width; ++x, ++p)
9707 {
9708 unsigned long pixel;
ac849ba4 9709#if 0 /* TODO: color tables. */
3cf3436e 9710 pixel = lookup_rgb_color (f, p->red, p->green, p->blue);
ac849ba4 9711#else
197edd35 9712 pixel = PALETTERGB (p->red / 256, p->green / 256, p->blue / 256);
ac849ba4 9713#endif
3cf3436e
JR
9714 XPutPixel (oimg, x, y, pixel);
9715 }
6fc2811b 9716
3cf3436e
JR
9717 xfree (colors);
9718 x_clear_image_1 (f, img, 1, 0, 1);
6fc2811b 9719
3cf3436e
JR
9720 x_put_x_image (f, oimg, pixmap, img->width, img->height);
9721 x_destroy_x_image (oimg);
9722 img->pixmap = pixmap;
ac849ba4 9723#if 0 /* TODO: color tables. */
3cf3436e
JR
9724 img->colors = colors_in_color_table (&img->ncolors);
9725 free_color_table ();
ac849ba4 9726#endif
6fc2811b
JR
9727}
9728
9729
3cf3436e
JR
9730/* On frame F, perform edge-detection on image IMG.
9731
9732 MATRIX is a nine-element array specifying the transformation
9733 matrix. See emboss_matrix for an example.
7d0393cf 9734
3cf3436e
JR
9735 COLOR_ADJUST is a color adjustment added to each pixel of the
9736 outgoing image. */
6fc2811b
JR
9737
9738static void
3cf3436e 9739x_detect_edges (f, img, matrix, color_adjust)
6fc2811b 9740 struct frame *f;
3cf3436e
JR
9741 struct image *img;
9742 int matrix[9], color_adjust;
6fc2811b 9743{
3cf3436e
JR
9744 XColor *colors = x_to_xcolors (f, img, 1);
9745 XColor *new, *p;
9746 int x, y, i, sum;
9747
9748 for (i = sum = 0; i < 9; ++i)
9749 sum += abs (matrix[i]);
9750
9751#define COLOR(A, X, Y) ((A) + (Y) * img->width + (X))
9752
9753 new = (XColor *) xmalloc (img->width * img->height * sizeof *new);
9754
9755 for (y = 0; y < img->height; ++y)
9756 {
9757 p = COLOR (new, 0, y);
9758 p->red = p->green = p->blue = 0xffff/2;
9759 p = COLOR (new, img->width - 1, y);
9760 p->red = p->green = p->blue = 0xffff/2;
9761 }
7d0393cf 9762
3cf3436e
JR
9763 for (x = 1; x < img->width - 1; ++x)
9764 {
9765 p = COLOR (new, x, 0);
9766 p->red = p->green = p->blue = 0xffff/2;
9767 p = COLOR (new, x, img->height - 1);
9768 p->red = p->green = p->blue = 0xffff/2;
9769 }
9770
9771 for (y = 1; y < img->height - 1; ++y)
9772 {
9773 p = COLOR (new, 1, y);
7d0393cf 9774
3cf3436e
JR
9775 for (x = 1; x < img->width - 1; ++x, ++p)
9776 {
9777 int r, g, b, y1, x1;
9778
9779 r = g = b = i = 0;
9780 for (y1 = y - 1; y1 < y + 2; ++y1)
9781 for (x1 = x - 1; x1 < x + 2; ++x1, ++i)
9782 if (matrix[i])
9783 {
9784 XColor *t = COLOR (colors, x1, y1);
9785 r += matrix[i] * t->red;
9786 g += matrix[i] * t->green;
9787 b += matrix[i] * t->blue;
9788 }
9789
9790 r = (r / sum + color_adjust) & 0xffff;
9791 g = (g / sum + color_adjust) & 0xffff;
9792 b = (b / sum + color_adjust) & 0xffff;
9793 p->red = p->green = p->blue = COLOR_INTENSITY (r, g, b);
9794 }
9795 }
9796
9797 xfree (colors);
9798 x_from_xcolors (f, img, new);
9799
9800#undef COLOR
9801}
9802
9803
9804/* Perform the pre-defined `emboss' edge-detection on image IMG
9805 on frame F. */
9806
9807static void
9808x_emboss (f, img)
9809 struct frame *f;
9810 struct image *img;
9811{
9812 x_detect_edges (f, img, emboss_matrix, 0xffff / 2);
6fc2811b 9813}
3cf3436e 9814
6fc2811b
JR
9815
9816/* Transform image IMG which is used on frame F with a Laplace
9817 edge-detection algorithm. The result is an image that can be used
9818 to draw disabled buttons, for example. */
9819
9820static void
9821x_laplace (f, img)
9822 struct frame *f;
9823 struct image *img;
9824{
3cf3436e
JR
9825 x_detect_edges (f, img, laplace_matrix, 45000);
9826}
6fc2811b 9827
6fc2811b 9828
3cf3436e
JR
9829/* Perform edge-detection on image IMG on frame F, with specified
9830 transformation matrix MATRIX and color-adjustment COLOR_ADJUST.
6fc2811b 9831
3cf3436e 9832 MATRIX must be either
6fc2811b 9833
3cf3436e
JR
9834 - a list of at least 9 numbers in row-major form
9835 - a vector of at least 9 numbers
6fc2811b 9836
3cf3436e
JR
9837 COLOR_ADJUST nil means use a default; otherwise it must be a
9838 number. */
6fc2811b 9839
3cf3436e
JR
9840static void
9841x_edge_detection (f, img, matrix, color_adjust)
9842 struct frame *f;
9843 struct image *img;
9844 Lisp_Object matrix, color_adjust;
9845{
9846 int i = 0;
9847 int trans[9];
7d0393cf 9848
3cf3436e 9849 if (CONSP (matrix))
6fc2811b 9850 {
3cf3436e
JR
9851 for (i = 0;
9852 i < 9 && CONSP (matrix) && NUMBERP (XCAR (matrix));
9853 ++i, matrix = XCDR (matrix))
9854 trans[i] = XFLOATINT (XCAR (matrix));
9855 }
9856 else if (VECTORP (matrix) && ASIZE (matrix) >= 9)
9857 {
9858 for (i = 0; i < 9 && NUMBERP (AREF (matrix, i)); ++i)
9859 trans[i] = XFLOATINT (AREF (matrix, i));
9860 }
9861
9862 if (NILP (color_adjust))
9863 color_adjust = make_number (0xffff / 2);
9864
9865 if (i == 9 && NUMBERP (color_adjust))
9866 x_detect_edges (f, img, trans, (int) XFLOATINT (color_adjust));
9867}
9868
6fc2811b 9869
3cf3436e 9870/* Transform image IMG on frame F so that it looks disabled. */
6fc2811b 9871
3cf3436e
JR
9872static void
9873x_disable_image (f, img)
9874 struct frame *f;
9875 struct image *img;
9876{
ac849ba4 9877 struct w32_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3cf3436e 9878
ac849ba4 9879 if (dpyinfo->n_planes * dpyinfo->n_cbits >= 2)
3cf3436e
JR
9880 {
9881 /* Color (or grayscale). Convert to gray, and equalize. Just
9882 drawing such images with a stipple can look very odd, so
9883 we're using this method instead. */
9884 XColor *colors = x_to_xcolors (f, img, 1);
9885 XColor *p, *end;
9886 const int h = 15000;
9887 const int l = 30000;
9888
9889 for (p = colors, end = colors + img->width * img->height;
9890 p < end;
9891 ++p)
6fc2811b 9892 {
3cf3436e
JR
9893 int i = COLOR_INTENSITY (p->red, p->green, p->blue);
9894 int i2 = (0xffff - h - l) * i / 0xffff + l;
9895 p->red = p->green = p->blue = i2;
6fc2811b
JR
9896 }
9897
3cf3436e 9898 x_from_xcolors (f, img, colors);
6fc2811b
JR
9899 }
9900
3cf3436e
JR
9901 /* Draw a cross over the disabled image, if we must or if we
9902 should. */
ac849ba4 9903 if (dpyinfo->n_planes * dpyinfo->n_cbits < 2 || cross_disabled_images)
3cf3436e 9904 {
197edd35
JR
9905 HDC hdc, bmpdc;
9906 HGDIOBJ prev;
9907
9908 hdc = get_frame_dc (f);
9909 bmpdc = CreateCompatibleDC (hdc);
9910 release_frame_dc (f, hdc);
9911
9912 prev = SelectObject (bmpdc, img->pixmap);
6fc2811b 9913
197edd35
JR
9914 SetTextColor (bmpdc, BLACK_PIX_DEFAULT (f));
9915 MoveToEx (bmpdc, 0, 0, NULL);
9916 LineTo (bmpdc, img->width - 1, img->height - 1);
9917 MoveToEx (bmpdc, 0, img->height - 1, NULL);
9918 LineTo (bmpdc, img->width - 1, 0);
6fc2811b 9919
3cf3436e
JR
9920 if (img->mask)
9921 {
197edd35
JR
9922 SelectObject (bmpdc, img->mask);
9923 SetTextColor (bmpdc, WHITE_PIX_DEFAULT (f));
9924 MoveToEx (bmpdc, 0, 0, NULL);
9925 LineTo (bmpdc, img->width - 1, img->height - 1);
9926 MoveToEx (bmpdc, 0, img->height - 1, NULL);
9927 LineTo (bmpdc, img->width - 1, 0);
3cf3436e 9928 }
197edd35
JR
9929 SelectObject (bmpdc, prev);
9930 DeleteDC (bmpdc);
3cf3436e 9931 }
6fc2811b
JR
9932}
9933
9934
9935/* Build a mask for image IMG which is used on frame F. FILE is the
9936 name of an image file, for error messages. HOW determines how to
9937 determine the background color of IMG. If it is a list '(R G B)',
9938 with R, G, and B being integers >= 0, take that as the color of the
9939 background. Otherwise, determine the background color of IMG
9940 heuristically. Value is non-zero if successful. */
9941
9942static int
9943x_build_heuristic_mask (f, img, how)
9944 struct frame *f;
9945 struct image *img;
9946 Lisp_Object how;
9947{
197edd35
JR
9948 HDC img_dc, frame_dc;
9949 HGDIOBJ prev;
9950 char *mask_img;
a05e2bae
JR
9951 int x, y, rc, use_img_background;
9952 unsigned long bg = 0;
197edd35 9953 int row_width;
a05e2bae
JR
9954
9955 if (img->mask)
9956 {
197edd35
JR
9957 DeleteObject (img->mask);
9958 img->mask = NULL;
a05e2bae
JR
9959 img->background_transparent_valid = 0;
9960 }
6fc2811b 9961
197edd35
JR
9962 /* Create the bit array serving as mask. */
9963 row_width = (img->width + 7) / 8;
9964 mask_img = xmalloc (row_width * img->height);
9965 bzero (mask_img, row_width * img->height);
6fc2811b 9966
197edd35
JR
9967 /* Create a memory device context for IMG->pixmap. */
9968 frame_dc = get_frame_dc (f);
9969 img_dc = CreateCompatibleDC (frame_dc);
9970 release_frame_dc (f, frame_dc);
9971 prev = SelectObject (img_dc, img->pixmap);
6fc2811b 9972
197edd35 9973 /* Determine the background color of img_dc. If HOW is `(R G B)'
a05e2bae
JR
9974 take that as color. Otherwise, use the image's background color. */
9975 use_img_background = 1;
7d0393cf 9976
6fc2811b
JR
9977 if (CONSP (how))
9978 {
a05e2bae 9979 int rgb[3], i;
6fc2811b 9980
a05e2bae 9981 for (i = 0; i < 3 && CONSP (how) && NATNUMP (XCAR (how)); ++i)
6fc2811b
JR
9982 {
9983 rgb[i] = XFASTINT (XCAR (how)) & 0xffff;
9984 how = XCDR (how);
9985 }
9986
9987 if (i == 3 && NILP (how))
9988 {
9989 char color_name[30];
6fc2811b 9990 sprintf (color_name, "#%04x%04x%04x", rgb[0], rgb[1], rgb[2]);
0040b876 9991 bg = x_alloc_image_color (f, img, build_string (color_name), 0)
8f92c555 9992 & 0x00ffffff; /* Filter out palette info. */
a05e2bae 9993 use_img_background = 0;
6fc2811b
JR
9994 }
9995 }
7d0393cf 9996
a05e2bae 9997 if (use_img_background)
197edd35 9998 bg = four_corners_best (img_dc, img->width, img->height);
6fc2811b
JR
9999
10000 /* Set all bits in mask_img to 1 whose color in ximg is different
10001 from the background color bg. */
10002 for (y = 0; y < img->height; ++y)
10003 for (x = 0; x < img->width; ++x)
197edd35
JR
10004 {
10005 COLORREF p = GetPixel (img_dc, x, y);
10006 if (p != bg)
10007 mask_img[y * row_width + x / 8] |= 1 << (x % 8);
10008 }
10009
10010 /* Create the mask image. */
10011 img->mask = w32_create_pixmap_from_bitmap_data (img->width, img->height,
10012 mask_img);
6fc2811b 10013
a05e2bae 10014 /* Fill in the background_transparent field while we have the mask handy. */
197edd35
JR
10015 SelectObject (img_dc, img->mask);
10016
10017 image_background_transparent (img, f, img_dc);
a05e2bae 10018
6fc2811b 10019 /* Put mask_img into img->mask. */
54eefef1 10020 x_destroy_x_image ((XImage *)mask_img);
197edd35
JR
10021 SelectObject (img_dc, prev);
10022 DeleteDC (img_dc);
6fc2811b
JR
10023
10024 return 1;
10025}
217e5be0 10026
6fc2811b
JR
10027\f
10028/***********************************************************************
10029 PBM (mono, gray, color)
10030 ***********************************************************************/
6fc2811b
JR
10031
10032static int pbm_image_p P_ ((Lisp_Object object));
10033static int pbm_load P_ ((struct frame *f, struct image *img));
10034static int pbm_scan_number P_ ((unsigned char **, unsigned char *));
10035
10036/* The symbol `pbm' identifying images of this type. */
10037
10038Lisp_Object Qpbm;
10039
10040/* Indices of image specification fields in gs_format, below. */
10041
10042enum pbm_keyword_index
10043{
10044 PBM_TYPE,
10045 PBM_FILE,
10046 PBM_DATA,
10047 PBM_ASCENT,
10048 PBM_MARGIN,
10049 PBM_RELIEF,
10050 PBM_ALGORITHM,
10051 PBM_HEURISTIC_MASK,
a05e2bae
JR
10052 PBM_MASK,
10053 PBM_FOREGROUND,
10054 PBM_BACKGROUND,
6fc2811b
JR
10055 PBM_LAST
10056};
10057
10058/* Vector of image_keyword structures describing the format
10059 of valid user-defined image specifications. */
10060
10061static struct image_keyword pbm_format[PBM_LAST] =
10062{
10063 {":type", IMAGE_SYMBOL_VALUE, 1},
10064 {":file", IMAGE_STRING_VALUE, 0},
10065 {":data", IMAGE_STRING_VALUE, 0},
8f92c555 10066 {":ascent", IMAGE_ASCENT_VALUE, 0},
8edb0a6f 10067 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
6fc2811b 10068 {":relief", IMAGE_INTEGER_VALUE, 0},
a93f4566 10069 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
3cf3436e
JR
10070 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
10071 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
10072 {":foreground", IMAGE_STRING_OR_NIL_VALUE, 0},
10073 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
6fc2811b
JR
10074};
10075
10076/* Structure describing the image type `pbm'. */
10077
10078static struct image_type pbm_type =
10079{
10080 &Qpbm,
10081 pbm_image_p,
10082 pbm_load,
10083 x_clear_image,
10084 NULL
10085};
10086
10087
10088/* Return non-zero if OBJECT is a valid PBM image specification. */
10089
10090static int
10091pbm_image_p (object)
10092 Lisp_Object object;
10093{
10094 struct image_keyword fmt[PBM_LAST];
7d0393cf 10095
6fc2811b 10096 bcopy (pbm_format, fmt, sizeof fmt);
7d0393cf 10097
8f92c555 10098 if (!parse_image_spec (object, fmt, PBM_LAST, Qpbm))
6fc2811b
JR
10099 return 0;
10100
10101 /* Must specify either :data or :file. */
10102 return fmt[PBM_DATA].count + fmt[PBM_FILE].count == 1;
10103}
10104
10105
10106/* Scan a decimal number from *S and return it. Advance *S while
10107 reading the number. END is the end of the string. Value is -1 at
10108 end of input. */
10109
10110static int
10111pbm_scan_number (s, end)
10112 unsigned char **s, *end;
10113{
10114 int c, val = -1;
10115
10116 while (*s < end)
10117 {
10118 /* Skip white-space. */
10119 while (*s < end && (c = *(*s)++, isspace (c)))
10120 ;
10121
10122 if (c == '#')
10123 {
10124 /* Skip comment to end of line. */
10125 while (*s < end && (c = *(*s)++, c != '\n'))
10126 ;
10127 }
10128 else if (isdigit (c))
10129 {
10130 /* Read decimal number. */
10131 val = c - '0';
10132 while (*s < end && (c = *(*s)++, isdigit (c)))
10133 val = 10 * val + c - '0';
10134 break;
10135 }
10136 else
10137 break;
10138 }
10139
10140 return val;
10141}
10142
10143
10144/* Read FILE into memory. Value is a pointer to a buffer allocated
10145 with xmalloc holding FILE's contents. Value is null if an error
6f826971 10146 occurred. *SIZE is set to the size of the file. */
6fc2811b
JR
10147
10148static char *
10149pbm_read_file (file, size)
10150 Lisp_Object file;
10151 int *size;
10152{
10153 FILE *fp = NULL;
10154 char *buf = NULL;
10155 struct stat st;
10156
d5db4077 10157 if (stat (SDATA (file), &st) == 0
c45bb3b2 10158 && (fp = fopen (SDATA (file), "rb")) != NULL
6fc2811b
JR
10159 && (buf = (char *) xmalloc (st.st_size),
10160 fread (buf, 1, st.st_size, fp) == st.st_size))
10161 {
10162 *size = st.st_size;
10163 fclose (fp);
10164 }
10165 else
10166 {
10167 if (fp)
10168 fclose (fp);
10169 if (buf)
10170 {
10171 xfree (buf);
10172 buf = NULL;
10173 }
10174 }
7d0393cf 10175
6fc2811b
JR
10176 return buf;
10177}
10178
10179
10180/* Load PBM image IMG for use on frame F. */
10181
7d0393cf 10182static int
6fc2811b
JR
10183pbm_load (f, img)
10184 struct frame *f;
10185 struct image *img;
10186{
10187 int raw_p, x, y;
10188 int width, height, max_color_idx = 0;
10189 XImage *ximg;
10190 Lisp_Object file, specified_file;
10191 enum {PBM_MONO, PBM_GRAY, PBM_COLOR} type;
10192 struct gcpro gcpro1;
10193 unsigned char *contents = NULL;
10194 unsigned char *end, *p;
10195 int size;
10196
10197 specified_file = image_spec_value (img->spec, QCfile, NULL);
10198 file = Qnil;
10199 GCPRO1 (file);
10200
10201 if (STRINGP (specified_file))
10202 {
10203 file = x_find_image_file (specified_file);
10204 if (!STRINGP (file))
10205 {
10206 image_error ("Cannot find image file `%s'", specified_file, Qnil);
10207 UNGCPRO;
10208 return 0;
10209 }
10210
d5db4077 10211 contents = slurp_file (SDATA (file), &size);
6fc2811b
JR
10212 if (contents == NULL)
10213 {
10214 image_error ("Error reading `%s'", file, Qnil);
10215 UNGCPRO;
10216 return 0;
10217 }
10218
10219 p = contents;
10220 end = contents + size;
10221 }
10222 else
10223 {
10224 Lisp_Object data;
10225 data = image_spec_value (img->spec, QCdata, NULL);
d5db4077
KR
10226 p = SDATA (data);
10227 end = p + SBYTES (data);
6fc2811b
JR
10228 }
10229
10230 /* Check magic number. */
10231 if (end - p < 2 || *p++ != 'P')
10232 {
10233 image_error ("Not a PBM image: `%s'", img->spec, Qnil);
10234 error:
10235 xfree (contents);
10236 UNGCPRO;
10237 return 0;
10238 }
10239
6fc2811b
JR
10240 switch (*p++)
10241 {
10242 case '1':
10243 raw_p = 0, type = PBM_MONO;
10244 break;
7d0393cf 10245
6fc2811b
JR
10246 case '2':
10247 raw_p = 0, type = PBM_GRAY;
10248 break;
10249
10250 case '3':
10251 raw_p = 0, type = PBM_COLOR;
10252 break;
10253
10254 case '4':
10255 raw_p = 1, type = PBM_MONO;
10256 break;
7d0393cf 10257
6fc2811b
JR
10258 case '5':
10259 raw_p = 1, type = PBM_GRAY;
10260 break;
7d0393cf 10261
6fc2811b
JR
10262 case '6':
10263 raw_p = 1, type = PBM_COLOR;
10264 break;
10265
10266 default:
10267 image_error ("Not a PBM image: `%s'", img->spec, Qnil);
10268 goto error;
10269 }
10270
10271 /* Read width, height, maximum color-component. Characters
10272 starting with `#' up to the end of a line are ignored. */
10273 width = pbm_scan_number (&p, end);
10274 height = pbm_scan_number (&p, end);
10275
10276 if (type != PBM_MONO)
10277 {
10278 max_color_idx = pbm_scan_number (&p, end);
10279 if (raw_p && max_color_idx > 255)
10280 max_color_idx = 255;
10281 }
7d0393cf 10282
6fc2811b
JR
10283 if (width < 0
10284 || height < 0
10285 || (type != PBM_MONO && max_color_idx < 0))
10286 goto error;
10287
ac849ba4 10288 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
3cf3436e
JR
10289 goto error;
10290
ac849ba4 10291#if 0 /* TODO: color tables. */
6fc2811b
JR
10292 /* Initialize the color hash table. */
10293 init_color_table ();
ac849ba4 10294#endif
6fc2811b
JR
10295
10296 if (type == PBM_MONO)
10297 {
10298 int c = 0, g;
3cf3436e
JR
10299 struct image_keyword fmt[PBM_LAST];
10300 unsigned long fg = FRAME_FOREGROUND_PIXEL (f);
10301 unsigned long bg = FRAME_BACKGROUND_PIXEL (f);
10302
10303 /* Parse the image specification. */
10304 bcopy (pbm_format, fmt, sizeof fmt);
10305 parse_image_spec (img->spec, fmt, PBM_LAST, Qpbm);
7d0393cf 10306
3cf3436e
JR
10307 /* Get foreground and background colors, maybe allocate colors. */
10308 if (fmt[PBM_FOREGROUND].count
10309 && STRINGP (fmt[PBM_FOREGROUND].value))
10310 fg = x_alloc_image_color (f, img, fmt[PBM_FOREGROUND].value, fg);
10311 if (fmt[PBM_BACKGROUND].count
10312 && STRINGP (fmt[PBM_BACKGROUND].value))
a05e2bae
JR
10313 {
10314 bg = x_alloc_image_color (f, img, fmt[PBM_BACKGROUND].value, bg);
10315 img->background = bg;
10316 img->background_valid = 1;
10317 }
10318
6fc2811b
JR
10319 for (y = 0; y < height; ++y)
10320 for (x = 0; x < width; ++x)
10321 {
10322 if (raw_p)
10323 {
10324 if ((x & 7) == 0)
10325 c = *p++;
10326 g = c & 0x80;
10327 c <<= 1;
10328 }
10329 else
10330 g = pbm_scan_number (&p, end);
10331
3cf3436e 10332 XPutPixel (ximg, x, y, g ? fg : bg);
6fc2811b
JR
10333 }
10334 }
10335 else
10336 {
10337 for (y = 0; y < height; ++y)
10338 for (x = 0; x < width; ++x)
10339 {
10340 int r, g, b;
7d0393cf 10341
6fc2811b
JR
10342 if (type == PBM_GRAY)
10343 r = g = b = raw_p ? *p++ : pbm_scan_number (&p, end);
10344 else if (raw_p)
10345 {
10346 r = *p++;
10347 g = *p++;
10348 b = *p++;
10349 }
10350 else
10351 {
10352 r = pbm_scan_number (&p, end);
10353 g = pbm_scan_number (&p, end);
10354 b = pbm_scan_number (&p, end);
10355 }
7d0393cf 10356
6fc2811b
JR
10357 if (r < 0 || g < 0 || b < 0)
10358 {
ac849ba4 10359 x_destroy_x_image (ximg);
6fc2811b
JR
10360 image_error ("Invalid pixel value in image `%s'",
10361 img->spec, Qnil);
10362 goto error;
10363 }
7d0393cf 10364
6fc2811b 10365 /* RGB values are now in the range 0..max_color_idx.
ac849ba4
JR
10366 Scale this to the range 0..0xff supported by W32. */
10367 r = (int) ((double) r * 255 / max_color_idx);
10368 g = (int) ((double) g * 255 / max_color_idx);
10369 b = (int) ((double) b * 255 / max_color_idx);
10370 XPutPixel (ximg, x, y,
10371#if 0 /* TODO: color tables. */
10372 lookup_rgb_color (f, r, g, b));
10373#else
10374 PALETTERGB (r, g, b));
10375#endif
6fc2811b
JR
10376 }
10377 }
ac849ba4
JR
10378
10379#if 0 /* TODO: color tables. */
6fc2811b
JR
10380 /* Store in IMG->colors the colors allocated for the image, and
10381 free the color table. */
10382 img->colors = colors_in_color_table (&img->ncolors);
10383 free_color_table ();
ac849ba4 10384#endif
a05e2bae
JR
10385 /* Maybe fill in the background field while we have ximg handy. */
10386 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
10387 IMAGE_BACKGROUND (img, f, ximg);
7d0393cf 10388
6fc2811b
JR
10389 /* Put the image into a pixmap. */
10390 x_put_x_image (f, ximg, img->pixmap, width, height);
10391 x_destroy_x_image (ximg);
7d0393cf 10392
6fc2811b
JR
10393 img->width = width;
10394 img->height = height;
10395
10396 UNGCPRO;
10397 xfree (contents);
10398 return 1;
10399}
6fc2811b
JR
10400
10401\f
10402/***********************************************************************
10403 PNG
10404 ***********************************************************************/
10405
10406#if HAVE_PNG
10407
10408#include <png.h>
10409
10410/* Function prototypes. */
10411
10412static int png_image_p P_ ((Lisp_Object object));
10413static int png_load P_ ((struct frame *f, struct image *img));
10414
10415/* The symbol `png' identifying images of this type. */
10416
10417Lisp_Object Qpng;
10418
10419/* Indices of image specification fields in png_format, below. */
10420
10421enum png_keyword_index
10422{
10423 PNG_TYPE,
10424 PNG_DATA,
10425 PNG_FILE,
10426 PNG_ASCENT,
10427 PNG_MARGIN,
10428 PNG_RELIEF,
10429 PNG_ALGORITHM,
10430 PNG_HEURISTIC_MASK,
a05e2bae
JR
10431 PNG_MASK,
10432 PNG_BACKGROUND,
6fc2811b
JR
10433 PNG_LAST
10434};
10435
10436/* Vector of image_keyword structures describing the format
10437 of valid user-defined image specifications. */
10438
10439static struct image_keyword png_format[PNG_LAST] =
10440{
10441 {":type", IMAGE_SYMBOL_VALUE, 1},
10442 {":data", IMAGE_STRING_VALUE, 0},
10443 {":file", IMAGE_STRING_VALUE, 0},
8f92c555 10444 {":ascent", IMAGE_ASCENT_VALUE, 0},
8edb0a6f 10445 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
6fc2811b 10446 {":relief", IMAGE_INTEGER_VALUE, 0},
a93f4566 10447 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
a05e2bae
JR
10448 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
10449 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
10450 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
6fc2811b
JR
10451};
10452
10453/* Structure describing the image type `png'. */
10454
10455static struct image_type png_type =
10456{
10457 &Qpng,
10458 png_image_p,
10459 png_load,
10460 x_clear_image,
10461 NULL
10462};
10463
839b1909
JR
10464/* PNG library details. */
10465
10466DEF_IMGLIB_FN (png_get_io_ptr);
10467DEF_IMGLIB_FN (png_check_sig);
10468DEF_IMGLIB_FN (png_create_read_struct);
10469DEF_IMGLIB_FN (png_create_info_struct);
10470DEF_IMGLIB_FN (png_destroy_read_struct);
10471DEF_IMGLIB_FN (png_set_read_fn);
c922a224 10472DEF_IMGLIB_FN (png_init_io);
839b1909
JR
10473DEF_IMGLIB_FN (png_set_sig_bytes);
10474DEF_IMGLIB_FN (png_read_info);
10475DEF_IMGLIB_FN (png_get_IHDR);
10476DEF_IMGLIB_FN (png_get_valid);
10477DEF_IMGLIB_FN (png_set_strip_16);
10478DEF_IMGLIB_FN (png_set_expand);
10479DEF_IMGLIB_FN (png_set_gray_to_rgb);
10480DEF_IMGLIB_FN (png_set_background);
10481DEF_IMGLIB_FN (png_get_bKGD);
10482DEF_IMGLIB_FN (png_read_update_info);
10483DEF_IMGLIB_FN (png_get_channels);
10484DEF_IMGLIB_FN (png_get_rowbytes);
10485DEF_IMGLIB_FN (png_read_image);
10486DEF_IMGLIB_FN (png_read_end);
10487DEF_IMGLIB_FN (png_error);
10488
10489static int
10490init_png_functions (library)
10491 HMODULE library;
10492{
10493 LOAD_IMGLIB_FN (library, png_get_io_ptr);
10494 LOAD_IMGLIB_FN (library, png_check_sig);
10495 LOAD_IMGLIB_FN (library, png_create_read_struct);
10496 LOAD_IMGLIB_FN (library, png_create_info_struct);
10497 LOAD_IMGLIB_FN (library, png_destroy_read_struct);
10498 LOAD_IMGLIB_FN (library, png_set_read_fn);
c922a224 10499 LOAD_IMGLIB_FN (library, png_init_io);
839b1909
JR
10500 LOAD_IMGLIB_FN (library, png_set_sig_bytes);
10501 LOAD_IMGLIB_FN (library, png_read_info);
10502 LOAD_IMGLIB_FN (library, png_get_IHDR);
10503 LOAD_IMGLIB_FN (library, png_get_valid);
10504 LOAD_IMGLIB_FN (library, png_set_strip_16);
10505 LOAD_IMGLIB_FN (library, png_set_expand);
10506 LOAD_IMGLIB_FN (library, png_set_gray_to_rgb);
10507 LOAD_IMGLIB_FN (library, png_set_background);
10508 LOAD_IMGLIB_FN (library, png_get_bKGD);
10509 LOAD_IMGLIB_FN (library, png_read_update_info);
10510 LOAD_IMGLIB_FN (library, png_get_channels);
10511 LOAD_IMGLIB_FN (library, png_get_rowbytes);
10512 LOAD_IMGLIB_FN (library, png_read_image);
10513 LOAD_IMGLIB_FN (library, png_read_end);
10514 LOAD_IMGLIB_FN (library, png_error);
10515 return 1;
10516}
6fc2811b
JR
10517
10518/* Return non-zero if OBJECT is a valid PNG image specification. */
10519
10520static int
10521png_image_p (object)
10522 Lisp_Object object;
10523{
10524 struct image_keyword fmt[PNG_LAST];
10525 bcopy (png_format, fmt, sizeof fmt);
c922a224 10526
8f92c555 10527 if (!parse_image_spec (object, fmt, PNG_LAST, Qpng))
6fc2811b
JR
10528 return 0;
10529
10530 /* Must specify either the :data or :file keyword. */
10531 return fmt[PNG_FILE].count + fmt[PNG_DATA].count == 1;
10532}
10533
10534
10535/* Error and warning handlers installed when the PNG library
10536 is initialized. */
10537
10538static void
10539my_png_error (png_ptr, msg)
10540 png_struct *png_ptr;
10541 char *msg;
10542{
10543 xassert (png_ptr != NULL);
10544 image_error ("PNG error: %s", build_string (msg), Qnil);
10545 longjmp (png_ptr->jmpbuf, 1);
10546}
10547
10548
10549static void
10550my_png_warning (png_ptr, msg)
10551 png_struct *png_ptr;
10552 char *msg;
10553{
10554 xassert (png_ptr != NULL);
10555 image_error ("PNG warning: %s", build_string (msg), Qnil);
10556}
10557
6fc2811b
JR
10558/* Memory source for PNG decoding. */
10559
10560struct png_memory_storage
10561{
10562 unsigned char *bytes; /* The data */
10563 size_t len; /* How big is it? */
10564 int index; /* Where are we? */
10565};
10566
10567
10568/* Function set as reader function when reading PNG image from memory.
10569 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
10570 bytes from the input to DATA. */
10571
10572static void
10573png_read_from_memory (png_ptr, data, length)
10574 png_structp png_ptr;
10575 png_bytep data;
10576 png_size_t length;
10577{
10578 struct png_memory_storage *tbr
839b1909 10579 = (struct png_memory_storage *) fn_png_get_io_ptr (png_ptr);
6fc2811b
JR
10580
10581 if (length > tbr->len - tbr->index)
839b1909 10582 fn_png_error (png_ptr, "Read error");
c922a224 10583
6fc2811b
JR
10584 bcopy (tbr->bytes + tbr->index, data, length);
10585 tbr->index = tbr->index + length;
10586}
10587
6fc2811b
JR
10588/* Load PNG image IMG for use on frame F. Value is non-zero if
10589 successful. */
10590
10591static int
10592png_load (f, img)
10593 struct frame *f;
10594 struct image *img;
10595{
10596 Lisp_Object file, specified_file;
10597 Lisp_Object specified_data;
10598 int x, y, i;
10599 XImage *ximg, *mask_img = NULL;
10600 struct gcpro gcpro1;
10601 png_struct *png_ptr = NULL;
10602 png_info *info_ptr = NULL, *end_info = NULL;
a05e2bae 10603 FILE *volatile fp = NULL;
6fc2811b 10604 png_byte sig[8];
54eefef1
JR
10605 png_byte * volatile pixels = NULL;
10606 png_byte ** volatile rows = NULL;
6fc2811b
JR
10607 png_uint_32 width, height;
10608 int bit_depth, color_type, interlace_type;
10609 png_byte channels;
10610 png_uint_32 row_bytes;
10611 int transparent_p;
6fc2811b
JR
10612 double screen_gamma, image_gamma;
10613 int intent;
10614 struct png_memory_storage tbr; /* Data to be read */
10615
10616 /* Find out what file to load. */
10617 specified_file = image_spec_value (img->spec, QCfile, NULL);
10618 specified_data = image_spec_value (img->spec, QCdata, NULL);
10619 file = Qnil;
10620 GCPRO1 (file);
10621
10622 if (NILP (specified_data))
10623 {
10624 file = x_find_image_file (specified_file);
10625 if (!STRINGP (file))
54eefef1
JR
10626 {
10627 image_error ("Cannot find image file `%s'", specified_file, Qnil);
10628 UNGCPRO;
10629 return 0;
10630 }
6fc2811b
JR
10631
10632 /* Open the image file. */
d5db4077 10633 fp = fopen (SDATA (file), "rb");
6fc2811b 10634 if (!fp)
54eefef1
JR
10635 {
10636 image_error ("Cannot open image file `%s'", file, Qnil);
10637 UNGCPRO;
10638 fclose (fp);
10639 return 0;
10640 }
6fc2811b
JR
10641
10642 /* Check PNG signature. */
10643 if (fread (sig, 1, sizeof sig, fp) != sizeof sig
839b1909 10644 || !fn_png_check_sig (sig, sizeof sig))
54eefef1
JR
10645 {
10646 image_error ("Not a PNG file: `%s'", file, Qnil);
10647 UNGCPRO;
10648 fclose (fp);
10649 return 0;
10650 }
6fc2811b
JR
10651 }
10652 else
10653 {
10654 /* Read from memory. */
d5db4077
KR
10655 tbr.bytes = SDATA (specified_data);
10656 tbr.len = SBYTES (specified_data);
6fc2811b
JR
10657 tbr.index = 0;
10658
10659 /* Check PNG signature. */
10660 if (tbr.len < sizeof sig
839b1909 10661 || !fn_png_check_sig (tbr.bytes, sizeof sig))
6fc2811b
JR
10662 {
10663 image_error ("Not a PNG image: `%s'", img->spec, Qnil);
10664 UNGCPRO;
10665 return 0;
10666 }
10667
10668 /* Need to skip past the signature. */
10669 tbr.bytes += sizeof (sig);
10670 }
10671
6fc2811b 10672 /* Initialize read and info structs for PNG lib. */
839b1909
JR
10673 png_ptr = fn_png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL,
10674 my_png_error, my_png_warning);
6fc2811b
JR
10675 if (!png_ptr)
10676 {
10677 if (fp) fclose (fp);
10678 UNGCPRO;
10679 return 0;
10680 }
10681
839b1909 10682 info_ptr = fn_png_create_info_struct (png_ptr);
6fc2811b
JR
10683 if (!info_ptr)
10684 {
839b1909 10685 fn_png_destroy_read_struct (&png_ptr, NULL, NULL);
6fc2811b
JR
10686 if (fp) fclose (fp);
10687 UNGCPRO;
10688 return 0;
10689 }
10690
839b1909 10691 end_info = fn_png_create_info_struct (png_ptr);
6fc2811b
JR
10692 if (!end_info)
10693 {
839b1909 10694 fn_png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
6fc2811b
JR
10695 if (fp) fclose (fp);
10696 UNGCPRO;
10697 return 0;
10698 }
10699
10700 /* Set error jump-back. We come back here when the PNG library
10701 detects an error. */
10702 if (setjmp (png_ptr->jmpbuf))
10703 {
10704 error:
10705 if (png_ptr)
839b1909 10706 fn_png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
6fc2811b
JR
10707 xfree (pixels);
10708 xfree (rows);
10709 if (fp) fclose (fp);
10710 UNGCPRO;
10711 return 0;
10712 }
10713
10714 /* Read image info. */
10715 if (!NILP (specified_data))
839b1909 10716 fn_png_set_read_fn (png_ptr, (void *) &tbr, png_read_from_memory);
6fc2811b 10717 else
839b1909 10718 fn_png_init_io (png_ptr, fp);
6fc2811b 10719
839b1909
JR
10720 fn_png_set_sig_bytes (png_ptr, sizeof sig);
10721 fn_png_read_info (png_ptr, info_ptr);
10722 fn_png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
10723 &interlace_type, NULL, NULL);
6fc2811b 10724
c922a224 10725 /* If image contains simply transparency data, we prefer to
6fc2811b 10726 construct a clipping mask. */
839b1909 10727 if (fn_png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
6fc2811b
JR
10728 transparent_p = 1;
10729 else
10730 transparent_p = 0;
10731
c922a224 10732 /* This function is easier to write if we only have to handle
6fc2811b
JR
10733 one data format: RGB or RGBA with 8 bits per channel. Let's
10734 transform other formats into that format. */
10735
10736 /* Strip more than 8 bits per channel. */
10737 if (bit_depth == 16)
839b1909 10738 fn_png_set_strip_16 (png_ptr);
6fc2811b
JR
10739
10740 /* Expand data to 24 bit RGB, or 8 bit grayscale, with alpha channel
10741 if available. */
839b1909 10742 fn_png_set_expand (png_ptr);
6fc2811b
JR
10743
10744 /* Convert grayscale images to RGB. */
c922a224 10745 if (color_type == PNG_COLOR_TYPE_GRAY
6fc2811b 10746 || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
839b1909 10747 fn_png_set_gray_to_rgb (png_ptr);
6fc2811b 10748
54eefef1 10749 screen_gamma = (f->gamma ? 1 / f->gamma / 0.45455 : 2.2);
6fc2811b 10750
54eefef1 10751#if 0 /* Avoid double gamma correction for PNG images. */
6fc2811b 10752 /* Tell the PNG lib to handle gamma correction for us. */
6fc2811b
JR
10753#if defined(PNG_READ_sRGB_SUPPORTED) || defined(PNG_WRITE_sRGB_SUPPORTED)
10754 if (png_get_sRGB (png_ptr, info_ptr, &intent))
54eefef1
JR
10755 /* The libpng documentation says this is right in this case. */
10756 png_set_gamma (png_ptr, screen_gamma, 0.45455);
6fc2811b
JR
10757 else
10758#endif
10759 if (png_get_gAMA (png_ptr, info_ptr, &image_gamma))
10760 /* Image contains gamma information. */
10761 png_set_gamma (png_ptr, screen_gamma, image_gamma);
10762 else
54eefef1
JR
10763 /* Use the standard default for the image gamma. */
10764 png_set_gamma (png_ptr, screen_gamma, 0.45455);
10765#endif /* if 0 */
6fc2811b
JR
10766
10767 /* Handle alpha channel by combining the image with a background
10768 color. Do this only if a real alpha channel is supplied. For
10769 simple transparency, we prefer a clipping mask. */
10770 if (!transparent_p)
10771 {
54eefef1 10772 png_color_16 *image_bg;
a05e2bae
JR
10773 Lisp_Object specified_bg
10774 = image_spec_value (img->spec, QCbackground, NULL);
10775
a05e2bae
JR
10776 if (STRINGP (specified_bg))
10777 /* The user specified `:background', use that. */
10778 {
10779 COLORREF color;
d5db4077 10780 if (w32_defined_color (f, SDATA (specified_bg), &color, 0))
a05e2bae
JR
10781 {
10782 png_color_16 user_bg;
10783
10784 bzero (&user_bg, sizeof user_bg);
54eefef1
JR
10785 user_bg.red = 256 * GetRValue (color);
10786 user_bg.green = 256 * GetGValue (color);
10787 user_bg.blue = 256 * GetBValue (color);
6fc2811b 10788
839b1909
JR
10789 fn_png_set_background (png_ptr, &user_bg,
10790 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
a05e2bae
JR
10791 }
10792 }
839b1909 10793 else if (fn_png_get_bKGD (png_ptr, info_ptr, &image_bg))
c922a224 10794 /* Image contains a background color with which to
6fc2811b 10795 combine the image. */
839b1909
JR
10796 fn_png_set_background (png_ptr, image_bg,
10797 PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
6fc2811b
JR
10798 else
10799 {
10800 /* Image does not contain a background color with which
c922a224 10801 to combine the image data via an alpha channel. Use
6fc2811b 10802 the frame's background instead. */
54eefef1 10803 COLORREF color;
6fc2811b 10804 png_color_16 frame_background;
54eefef1
JR
10805 color = FRAME_BACKGROUND_PIXEL (f);
10806#if 0 /* TODO : Colormap support. */
10807 Colormap cmap;
6fc2811b 10808
a05e2bae 10809 cmap = FRAME_X_COLORMAP (f);
a05e2bae 10810 x_query_color (f, &color);
54eefef1 10811#endif
6fc2811b
JR
10812
10813 bzero (&frame_background, sizeof frame_background);
54eefef1
JR
10814 frame_background.red = 256 * GetRValue (color);
10815 frame_background.green = 256 * GetGValue (color);
10816 frame_background.blue = 256 * GetBValue (color);
6fc2811b 10817
839b1909
JR
10818 fn_png_set_background (png_ptr, &frame_background,
10819 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
6fc2811b
JR
10820 }
10821 }
10822
10823 /* Update info structure. */
839b1909 10824 fn_png_read_update_info (png_ptr, info_ptr);
6fc2811b
JR
10825
10826 /* Get number of channels. Valid values are 1 for grayscale images
10827 and images with a palette, 2 for grayscale images with transparency
10828 information (alpha channel), 3 for RGB images, and 4 for RGB
10829 images with alpha channel, i.e. RGBA. If conversions above were
10830 sufficient we should only have 3 or 4 channels here. */
839b1909 10831 channels = fn_png_get_channels (png_ptr, info_ptr);
6fc2811b
JR
10832 xassert (channels == 3 || channels == 4);
10833
10834 /* Number of bytes needed for one row of the image. */
839b1909 10835 row_bytes = fn_png_get_rowbytes (png_ptr, info_ptr);
6fc2811b
JR
10836
10837 /* Allocate memory for the image. */
10838 pixels = (png_byte *) xmalloc (row_bytes * height * sizeof *pixels);
10839 rows = (png_byte **) xmalloc (height * sizeof *rows);
10840 for (i = 0; i < height; ++i)
10841 rows[i] = pixels + i * row_bytes;
10842
10843 /* Read the entire image. */
839b1909
JR
10844 fn_png_read_image (png_ptr, rows);
10845 fn_png_read_end (png_ptr, info_ptr);
6fc2811b
JR
10846 if (fp)
10847 {
10848 fclose (fp);
10849 fp = NULL;
10850 }
c922a224 10851
6fc2811b
JR
10852 /* Create the X image and pixmap. */
10853 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg,
10854 &img->pixmap))
a05e2bae 10855 goto error;
c922a224 10856
6fc2811b
JR
10857 /* Create an image and pixmap serving as mask if the PNG image
10858 contains an alpha channel. */
10859 if (channels == 4
10860 && !transparent_p
10861 && !x_create_x_image_and_pixmap (f, width, height, 1,
10862 &mask_img, &img->mask))
10863 {
10864 x_destroy_x_image (ximg);
54eefef1 10865 DeleteObject (img->pixmap);
6fc2811b 10866 img->pixmap = 0;
6fc2811b
JR
10867 goto error;
10868 }
6fc2811b 10869 /* Fill the X image and mask from PNG data. */
54eefef1 10870#if 0 /* TODO: Color tables. */
6fc2811b 10871 init_color_table ();
54eefef1 10872#endif
6fc2811b
JR
10873
10874 for (y = 0; y < height; ++y)
10875 {
10876 png_byte *p = rows[y];
10877
10878 for (x = 0; x < width; ++x)
10879 {
10880 unsigned r, g, b;
10881
54eefef1
JR
10882 r = *p++;
10883 g = *p++;
10884 b = *p++;
10885#if 0 /* TODO: Color tables. */
6fc2811b 10886 XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b));
54eefef1
JR
10887#else
10888 XPutPixel (ximg, x, y, PALETTERGB (r, g, b));
10889#endif
6fc2811b 10890 /* An alpha channel, aka mask channel, associates variable
c922a224
JB
10891 transparency with an image. Where other image formats
10892 support binary transparency---fully transparent or fully
6fc2811b
JR
10893 opaque---PNG allows up to 254 levels of partial transparency.
10894 The PNG library implements partial transparency by combining
10895 the image with a specified background color.
10896
10897 I'm not sure how to handle this here nicely: because the
10898 background on which the image is displayed may change, for
c922a224
JB
10899 real alpha channel support, it would be necessary to create
10900 a new image for each possible background.
6fc2811b
JR
10901
10902 What I'm doing now is that a mask is created if we have
10903 boolean transparency information. Otherwise I'm using
10904 the frame's background color to combine the image with. */
10905
10906 if (channels == 4)
10907 {
10908 if (mask_img)
10909 XPutPixel (mask_img, x, y, *p > 0);
10910 ++p;
10911 }
10912 }
10913 }
10914
a05e2bae
JR
10915 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
10916 /* Set IMG's background color from the PNG image, unless the user
10917 overrode it. */
10918 {
10919 png_color_16 *bg;
839b1909 10920 if (fn_png_get_bKGD (png_ptr, info_ptr, &bg))
a05e2bae 10921 {
54eefef1 10922#if 0 /* TODO: Color tables. */
a05e2bae 10923 img->background = lookup_rgb_color (f, bg->red, bg->green, bg->blue);
54eefef1
JR
10924#else
10925 img->background = PALETTERGB (bg->red / 256, bg->green / 256,
10926 bg->blue / 256);
10927#endif
a05e2bae
JR
10928 img->background_valid = 1;
10929 }
10930 }
10931
54eefef1 10932#if 0 /* TODO: Color tables. */
6fc2811b
JR
10933 /* Remember colors allocated for this image. */
10934 img->colors = colors_in_color_table (&img->ncolors);
10935 free_color_table ();
54eefef1 10936#endif
6fc2811b
JR
10937
10938 /* Clean up. */
839b1909 10939 fn_png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
6fc2811b
JR
10940 xfree (rows);
10941 xfree (pixels);
10942
10943 img->width = width;
10944 img->height = height;
10945
a05e2bae
JR
10946 /* Maybe fill in the background field while we have ximg handy. */
10947 IMAGE_BACKGROUND (img, f, ximg);
10948
6fc2811b
JR
10949 /* Put the image into the pixmap, then free the X image and its buffer. */
10950 x_put_x_image (f, ximg, img->pixmap, width, height);
10951 x_destroy_x_image (ximg);
10952
10953 /* Same for the mask. */
10954 if (mask_img)
10955 {
a05e2bae
JR
10956 /* Fill in the background_transparent field while we have the mask
10957 handy. */
10958 image_background_transparent (img, f, mask_img);
10959
6fc2811b
JR
10960 x_put_x_image (f, mask_img, img->mask, img->width, img->height);
10961 x_destroy_x_image (mask_img);
10962 }
10963
6fc2811b
JR
10964 UNGCPRO;
10965 return 1;
10966}
10967
10968#endif /* HAVE_PNG != 0 */
10969
10970
10971\f
10972/***********************************************************************
10973 JPEG
10974 ***********************************************************************/
10975
10976#if HAVE_JPEG
10977
10978/* Work around a warning about HAVE_STDLIB_H being redefined in
10979 jconfig.h. */
10980#ifdef HAVE_STDLIB_H
10981#define HAVE_STDLIB_H_1
10982#undef HAVE_STDLIB_H
10983#endif /* HAVE_STLIB_H */
10984
10985#include <jpeglib.h>
10986#include <jerror.h>
10987#include <setjmp.h>
10988
10989#ifdef HAVE_STLIB_H_1
10990#define HAVE_STDLIB_H 1
10991#endif
10992
10993static int jpeg_image_p P_ ((Lisp_Object object));
10994static int jpeg_load P_ ((struct frame *f, struct image *img));
10995
10996/* The symbol `jpeg' identifying images of this type. */
10997
10998Lisp_Object Qjpeg;
10999
11000/* Indices of image specification fields in gs_format, below. */
11001
11002enum jpeg_keyword_index
11003{
11004 JPEG_TYPE,
11005 JPEG_DATA,
11006 JPEG_FILE,
11007 JPEG_ASCENT,
11008 JPEG_MARGIN,
11009 JPEG_RELIEF,
11010 JPEG_ALGORITHM,
11011 JPEG_HEURISTIC_MASK,
a05e2bae
JR
11012 JPEG_MASK,
11013 JPEG_BACKGROUND,
6fc2811b
JR
11014 JPEG_LAST
11015};
11016
11017/* Vector of image_keyword structures describing the format
11018 of valid user-defined image specifications. */
11019
11020static struct image_keyword jpeg_format[JPEG_LAST] =
11021{
11022 {":type", IMAGE_SYMBOL_VALUE, 1},
11023 {":data", IMAGE_STRING_VALUE, 0},
11024 {":file", IMAGE_STRING_VALUE, 0},
8f92c555 11025 {":ascent", IMAGE_ASCENT_VALUE, 0},
8edb0a6f 11026 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
6fc2811b 11027 {":relief", IMAGE_INTEGER_VALUE, 0},
a05e2bae
JR
11028 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
11029 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
11030 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
11031 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
6fc2811b
JR
11032};
11033
11034/* Structure describing the image type `jpeg'. */
11035
11036static struct image_type jpeg_type =
11037{
11038 &Qjpeg,
11039 jpeg_image_p,
11040 jpeg_load,
11041 x_clear_image,
11042 NULL
11043};
11044
11045
afc390dc
JR
11046/* JPEG library details. */
11047DEF_IMGLIB_FN (jpeg_CreateDecompress);
11048DEF_IMGLIB_FN (jpeg_start_decompress);
11049DEF_IMGLIB_FN (jpeg_finish_decompress);
11050DEF_IMGLIB_FN (jpeg_destroy_decompress);
11051DEF_IMGLIB_FN (jpeg_read_header);
11052DEF_IMGLIB_FN (jpeg_read_scanlines);
11053DEF_IMGLIB_FN (jpeg_stdio_src);
11054DEF_IMGLIB_FN (jpeg_std_error);
11055DEF_IMGLIB_FN (jpeg_resync_to_restart);
11056
11057static int
11058init_jpeg_functions (library)
11059 HMODULE library;
11060{
11061 LOAD_IMGLIB_FN (library, jpeg_finish_decompress);
11062 LOAD_IMGLIB_FN (library, jpeg_read_scanlines);
11063 LOAD_IMGLIB_FN (library, jpeg_start_decompress);
11064 LOAD_IMGLIB_FN (library, jpeg_read_header);
11065 LOAD_IMGLIB_FN (library, jpeg_stdio_src);
11066 LOAD_IMGLIB_FN (library, jpeg_CreateDecompress);
11067 LOAD_IMGLIB_FN (library, jpeg_destroy_decompress);
11068 LOAD_IMGLIB_FN (library, jpeg_std_error);
11069 LOAD_IMGLIB_FN (library, jpeg_resync_to_restart);
11070 return 1;
11071}
11072
11073/* Wrapper since we can't directly assign the function pointer
11074 to another function pointer that was declared more completely easily. */
11075static boolean
11076jpeg_resync_to_restart_wrapper(cinfo, desired)
11077 j_decompress_ptr cinfo;
11078 int desired;
11079{
11080 return fn_jpeg_resync_to_restart (cinfo, desired);
11081}
11082
11083
6fc2811b
JR
11084/* Return non-zero if OBJECT is a valid JPEG image specification. */
11085
11086static int
11087jpeg_image_p (object)
11088 Lisp_Object object;
11089{
11090 struct image_keyword fmt[JPEG_LAST];
c922a224 11091
6fc2811b 11092 bcopy (jpeg_format, fmt, sizeof fmt);
c922a224 11093
8f92c555 11094 if (!parse_image_spec (object, fmt, JPEG_LAST, Qjpeg))
6fc2811b
JR
11095 return 0;
11096
11097 /* Must specify either the :data or :file keyword. */
11098 return fmt[JPEG_FILE].count + fmt[JPEG_DATA].count == 1;
11099}
11100
11101
11102struct my_jpeg_error_mgr
11103{
11104 struct jpeg_error_mgr pub;
11105 jmp_buf setjmp_buffer;
11106};
11107
afc390dc 11108
6fc2811b
JR
11109static void
11110my_error_exit (cinfo)
11111 j_common_ptr cinfo;
11112{
11113 struct my_jpeg_error_mgr *mgr = (struct my_jpeg_error_mgr *) cinfo->err;
11114 longjmp (mgr->setjmp_buffer, 1);
11115}
11116
afc390dc 11117
6fc2811b
JR
11118/* Init source method for JPEG data source manager. Called by
11119 jpeg_read_header() before any data is actually read. See
11120 libjpeg.doc from the JPEG lib distribution. */
11121
11122static void
11123our_init_source (cinfo)
11124 j_decompress_ptr cinfo;
11125{
11126}
11127
11128
11129/* Fill input buffer method for JPEG data source manager. Called
11130 whenever more data is needed. We read the whole image in one step,
11131 so this only adds a fake end of input marker at the end. */
11132
11133static boolean
11134our_fill_input_buffer (cinfo)
11135 j_decompress_ptr cinfo;
11136{
11137 /* Insert a fake EOI marker. */
11138 struct jpeg_source_mgr *src = cinfo->src;
11139 static JOCTET buffer[2];
11140
11141 buffer[0] = (JOCTET) 0xFF;
11142 buffer[1] = (JOCTET) JPEG_EOI;
11143
11144 src->next_input_byte = buffer;
11145 src->bytes_in_buffer = 2;
11146 return TRUE;
11147}
11148
11149
11150/* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
11151 is the JPEG data source manager. */
11152
11153static void
11154our_skip_input_data (cinfo, num_bytes)
11155 j_decompress_ptr cinfo;
11156 long num_bytes;
11157{
11158 struct jpeg_source_mgr *src = (struct jpeg_source_mgr *) cinfo->src;
11159
11160 if (src)
11161 {
11162 if (num_bytes > src->bytes_in_buffer)
11163 ERREXIT (cinfo, JERR_INPUT_EOF);
c922a224 11164
6fc2811b
JR
11165 src->bytes_in_buffer -= num_bytes;
11166 src->next_input_byte += num_bytes;
11167 }
11168}
11169
11170
11171/* Method to terminate data source. Called by
11172 jpeg_finish_decompress() after all data has been processed. */
11173
11174static void
11175our_term_source (cinfo)
11176 j_decompress_ptr cinfo;
11177{
11178}
11179
11180
11181/* Set up the JPEG lib for reading an image from DATA which contains
11182 LEN bytes. CINFO is the decompression info structure created for
11183 reading the image. */
11184
11185static void
11186jpeg_memory_src (cinfo, data, len)
11187 j_decompress_ptr cinfo;
11188 JOCTET *data;
11189 unsigned int len;
11190{
11191 struct jpeg_source_mgr *src;
11192
11193 if (cinfo->src == NULL)
11194 {
11195 /* First time for this JPEG object? */
11196 cinfo->src = (struct jpeg_source_mgr *)
11197 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
11198 sizeof (struct jpeg_source_mgr));
11199 src = (struct jpeg_source_mgr *) cinfo->src;
11200 src->next_input_byte = data;
11201 }
c922a224 11202
6fc2811b
JR
11203 src = (struct jpeg_source_mgr *) cinfo->src;
11204 src->init_source = our_init_source;
11205 src->fill_input_buffer = our_fill_input_buffer;
11206 src->skip_input_data = our_skip_input_data;
afc390dc 11207 src->resync_to_restart = jpeg_resync_to_restart_wrapper; /* Use default method. */
6fc2811b
JR
11208 src->term_source = our_term_source;
11209 src->bytes_in_buffer = len;
11210 src->next_input_byte = data;
11211}
11212
11213
11214/* Load image IMG for use on frame F. Patterned after example.c
11215 from the JPEG lib. */
11216
c922a224 11217static int
6fc2811b
JR
11218jpeg_load (f, img)
11219 struct frame *f;
11220 struct image *img;
11221{
11222 struct jpeg_decompress_struct cinfo;
11223 struct my_jpeg_error_mgr mgr;
11224 Lisp_Object file, specified_file;
11225 Lisp_Object specified_data;
a05e2bae 11226 FILE * volatile fp = NULL;
6fc2811b
JR
11227 JSAMPARRAY buffer;
11228 int row_stride, x, y;
11229 XImage *ximg = NULL;
11230 int rc;
11231 unsigned long *colors;
11232 int width, height;
11233 struct gcpro gcpro1;
11234
11235 /* Open the JPEG file. */
11236 specified_file = image_spec_value (img->spec, QCfile, NULL);
11237 specified_data = image_spec_value (img->spec, QCdata, NULL);
11238 file = Qnil;
11239 GCPRO1 (file);
11240
6fc2811b
JR
11241 if (NILP (specified_data))
11242 {
11243 file = x_find_image_file (specified_file);
11244 if (!STRINGP (file))
afc390dc
JR
11245 {
11246 image_error ("Cannot find image file `%s'", specified_file, Qnil);
11247 UNGCPRO;
11248 return 0;
11249 }
c922a224 11250
c45bb3b2 11251 fp = fopen (SDATA (file), "rb");
6fc2811b 11252 if (fp == NULL)
afc390dc
JR
11253 {
11254 image_error ("Cannot open `%s'", file, Qnil);
11255 UNGCPRO;
11256 return 0;
11257 }
6fc2811b 11258 }
7d0393cf 11259
6fc2811b 11260 /* Customize libjpeg's error handling to call my_error_exit when an
afc390dc
JR
11261 error is detected. This function will perform a longjmp. */
11262 cinfo.err = fn_jpeg_std_error (&mgr.pub);
a05e2bae 11263 mgr.pub.error_exit = my_error_exit;
c922a224 11264
6fc2811b
JR
11265 if ((rc = setjmp (mgr.setjmp_buffer)) != 0)
11266 {
11267 if (rc == 1)
11268 {
11269 /* Called from my_error_exit. Display a JPEG error. */
11270 char buffer[JMSG_LENGTH_MAX];
11271 cinfo.err->format_message ((j_common_ptr) &cinfo, buffer);
11272 image_error ("Error reading JPEG image `%s': %s", img->spec,
11273 build_string (buffer));
11274 }
c922a224 11275
6fc2811b
JR
11276 /* Close the input file and destroy the JPEG object. */
11277 if (fp)
afc390dc
JR
11278 fclose ((FILE *) fp);
11279 fn_jpeg_destroy_decompress (&cinfo);
7d0393cf 11280
6fc2811b
JR
11281 /* If we already have an XImage, free that. */
11282 x_destroy_x_image (ximg);
11283
11284 /* Free pixmap and colors. */
11285 x_clear_image (f, img);
c922a224 11286
6fc2811b
JR
11287 UNGCPRO;
11288 return 0;
11289 }
11290
11291 /* Create the JPEG decompression object. Let it read from fp.
afc390dc
JR
11292 Read the JPEG image header. */
11293 fn_jpeg_CreateDecompress (&cinfo, JPEG_LIB_VERSION, sizeof (cinfo));
6fc2811b
JR
11294
11295 if (NILP (specified_data))
afc390dc 11296 fn_jpeg_stdio_src (&cinfo, (FILE *) fp);
6fc2811b 11297 else
d5db4077
KR
11298 jpeg_memory_src (&cinfo, SDATA (specified_data),
11299 SBYTES (specified_data));
6fc2811b 11300
afc390dc 11301 fn_jpeg_read_header (&cinfo, TRUE);
6fc2811b
JR
11302
11303 /* Customize decompression so that color quantization will be used.
afc390dc 11304 Start decompression. */
6fc2811b 11305 cinfo.quantize_colors = TRUE;
afc390dc 11306 fn_jpeg_start_decompress (&cinfo);
6fc2811b
JR
11307 width = img->width = cinfo.output_width;
11308 height = img->height = cinfo.output_height;
11309
6fc2811b 11310 /* Create X image and pixmap. */
afc390dc 11311 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
a05e2bae 11312 longjmp (mgr.setjmp_buffer, 2);
6fc2811b
JR
11313
11314 /* Allocate colors. When color quantization is used,
11315 cinfo.actual_number_of_colors has been set with the number of
11316 colors generated, and cinfo.colormap is a two-dimensional array
11317 of color indices in the range 0..cinfo.actual_number_of_colors.
11318 No more than 255 colors will be generated. */
11319 {
11320 int i, ir, ig, ib;
11321
11322 if (cinfo.out_color_components > 2)
11323 ir = 0, ig = 1, ib = 2;
11324 else if (cinfo.out_color_components > 1)
11325 ir = 0, ig = 1, ib = 0;
11326 else
11327 ir = 0, ig = 0, ib = 0;
11328
afc390dc 11329#if 0 /* TODO: Color tables. */
6fc2811b
JR
11330 /* Use the color table mechanism because it handles colors that
11331 cannot be allocated nicely. Such colors will be replaced with
11332 a default color, and we don't have to care about which colors
11333 can be freed safely, and which can't. */
11334 init_color_table ();
afc390dc 11335#endif
6fc2811b
JR
11336 colors = (unsigned long *) alloca (cinfo.actual_number_of_colors
11337 * sizeof *colors);
c922a224 11338
6fc2811b
JR
11339 for (i = 0; i < cinfo.actual_number_of_colors; ++i)
11340 {
afc390dc
JR
11341 int r = cinfo.colormap[ir][i];
11342 int g = cinfo.colormap[ig][i];
11343 int b = cinfo.colormap[ib][i];
11344#if 0 /* TODO: Color tables. */
6fc2811b 11345 colors[i] = lookup_rgb_color (f, r, g, b);
afc390dc
JR
11346#else
11347 colors[i] = PALETTERGB (r, g, b);
11348#endif
6fc2811b
JR
11349 }
11350
afc390dc 11351#if 0 /* TODO: Color tables. */
6fc2811b
JR
11352 /* Remember those colors actually allocated. */
11353 img->colors = colors_in_color_table (&img->ncolors);
11354 free_color_table ();
afc390dc 11355#endif
6fc2811b
JR
11356 }
11357
11358 /* Read pixels. */
11359 row_stride = width * cinfo.output_components;
11360 buffer = cinfo.mem->alloc_sarray ((j_common_ptr) &cinfo, JPOOL_IMAGE,
11361 row_stride, 1);
11362 for (y = 0; y < height; ++y)
11363 {
afc390dc 11364 fn_jpeg_read_scanlines (&cinfo, buffer, 1);
6fc2811b
JR
11365 for (x = 0; x < cinfo.output_width; ++x)
11366 XPutPixel (ximg, x, y, colors[buffer[0][x]]);
11367 }
11368
11369 /* Clean up. */
afc390dc
JR
11370 fn_jpeg_finish_decompress (&cinfo);
11371 fn_jpeg_destroy_decompress (&cinfo);
6fc2811b 11372 if (fp)
afc390dc 11373 fclose ((FILE *) fp);
7d0393cf 11374
a05e2bae
JR
11375 /* Maybe fill in the background field while we have ximg handy. */
11376 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
11377 IMAGE_BACKGROUND (img, f, ximg);
c922a224 11378
6fc2811b
JR
11379 /* Put the image into the pixmap. */
11380 x_put_x_image (f, ximg, img->pixmap, width, height);
11381 x_destroy_x_image (ximg);
6fc2811b
JR
11382 UNGCPRO;
11383 return 1;
11384}
11385
11386#endif /* HAVE_JPEG */
11387
11388
11389\f
11390/***********************************************************************
11391 TIFF
11392 ***********************************************************************/
11393
11394#if HAVE_TIFF
11395
11396#include <tiffio.h>
11397
11398static int tiff_image_p P_ ((Lisp_Object object));
11399static int tiff_load P_ ((struct frame *f, struct image *img));
11400
11401/* The symbol `tiff' identifying images of this type. */
11402
11403Lisp_Object Qtiff;
11404
11405/* Indices of image specification fields in tiff_format, below. */
11406
11407enum tiff_keyword_index
11408{
11409 TIFF_TYPE,
11410 TIFF_DATA,
11411 TIFF_FILE,
11412 TIFF_ASCENT,
11413 TIFF_MARGIN,
11414 TIFF_RELIEF,
11415 TIFF_ALGORITHM,
11416 TIFF_HEURISTIC_MASK,
a05e2bae
JR
11417 TIFF_MASK,
11418 TIFF_BACKGROUND,
6fc2811b
JR
11419 TIFF_LAST
11420};
11421
11422/* Vector of image_keyword structures describing the format
11423 of valid user-defined image specifications. */
11424
11425static struct image_keyword tiff_format[TIFF_LAST] =
11426{
11427 {":type", IMAGE_SYMBOL_VALUE, 1},
11428 {":data", IMAGE_STRING_VALUE, 0},
11429 {":file", IMAGE_STRING_VALUE, 0},
8f92c555 11430 {":ascent", IMAGE_ASCENT_VALUE, 0},
8edb0a6f 11431 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
6fc2811b 11432 {":relief", IMAGE_INTEGER_VALUE, 0},
a05e2bae
JR
11433 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
11434 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
11435 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
11436 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
6fc2811b
JR
11437};
11438
11439/* Structure describing the image type `tiff'. */
11440
11441static struct image_type tiff_type =
11442{
11443 &Qtiff,
11444 tiff_image_p,
11445 tiff_load,
11446 x_clear_image,
11447 NULL
11448};
11449
12b918b2
JB
11450/* TIFF library details. */
11451DEF_IMGLIB_FN (TIFFSetErrorHandler);
11452DEF_IMGLIB_FN (TIFFSetWarningHandler);
11453DEF_IMGLIB_FN (TIFFOpen);
11454DEF_IMGLIB_FN (TIFFClientOpen);
11455DEF_IMGLIB_FN (TIFFGetField);
11456DEF_IMGLIB_FN (TIFFReadRGBAImage);
11457DEF_IMGLIB_FN (TIFFClose);
11458
11459static int
11460init_tiff_functions (library)
11461 HMODULE library;
11462{
11463 LOAD_IMGLIB_FN (library, TIFFSetErrorHandler);
11464 LOAD_IMGLIB_FN (library, TIFFSetWarningHandler);
11465 LOAD_IMGLIB_FN (library, TIFFOpen);
11466 LOAD_IMGLIB_FN (library, TIFFClientOpen);
11467 LOAD_IMGLIB_FN (library, TIFFGetField);
11468 LOAD_IMGLIB_FN (library, TIFFReadRGBAImage);
11469 LOAD_IMGLIB_FN (library, TIFFClose);
11470 return 1;
11471}
6fc2811b
JR
11472
11473/* Return non-zero if OBJECT is a valid TIFF image specification. */
11474
11475static int
11476tiff_image_p (object)
11477 Lisp_Object object;
11478{
11479 struct image_keyword fmt[TIFF_LAST];
11480 bcopy (tiff_format, fmt, sizeof fmt);
7d0393cf 11481
8f92c555 11482 if (!parse_image_spec (object, fmt, TIFF_LAST, Qtiff))
6fc2811b 11483 return 0;
7d0393cf 11484
6fc2811b
JR
11485 /* Must specify either the :data or :file keyword. */
11486 return fmt[TIFF_FILE].count + fmt[TIFF_DATA].count == 1;
11487}
11488
11489
11490/* Reading from a memory buffer for TIFF images Based on the PNG
11491 memory source, but we have to provide a lot of extra functions.
11492 Blah.
11493
11494 We really only need to implement read and seek, but I am not
11495 convinced that the TIFF library is smart enough not to destroy
11496 itself if we only hand it the function pointers we need to
11497 override. */
11498
11499typedef struct
11500{
11501 unsigned char *bytes;
11502 size_t len;
11503 int index;
11504}
11505tiff_memory_source;
11506
11507static size_t
11508tiff_read_from_memory (data, buf, size)
11509 thandle_t data;
11510 tdata_t buf;
11511 tsize_t size;
11512{
11513 tiff_memory_source *src = (tiff_memory_source *) data;
11514
11515 if (size > src->len - src->index)
11516 return (size_t) -1;
11517 bcopy (src->bytes + src->index, buf, size);
11518 src->index += size;
11519 return size;
11520}
11521
11522static size_t
11523tiff_write_from_memory (data, buf, size)
11524 thandle_t data;
11525 tdata_t buf;
11526 tsize_t size;
11527{
11528 return (size_t) -1;
11529}
11530
11531static toff_t
11532tiff_seek_in_memory (data, off, whence)
11533 thandle_t data;
11534 toff_t off;
11535 int whence;
11536{
11537 tiff_memory_source *src = (tiff_memory_source *) data;
11538 int idx;
11539
11540 switch (whence)
11541 {
11542 case SEEK_SET: /* Go from beginning of source. */
11543 idx = off;
11544 break;
7d0393cf 11545
6fc2811b
JR
11546 case SEEK_END: /* Go from end of source. */
11547 idx = src->len + off;
11548 break;
7d0393cf 11549
6fc2811b
JR
11550 case SEEK_CUR: /* Go from current position. */
11551 idx = src->index + off;
11552 break;
7d0393cf 11553
6fc2811b
JR
11554 default: /* Invalid `whence'. */
11555 return -1;
11556 }
7d0393cf 11557
6fc2811b
JR
11558 if (idx > src->len || idx < 0)
11559 return -1;
7d0393cf 11560
6fc2811b
JR
11561 src->index = idx;
11562 return src->index;
11563}
11564
11565static int
11566tiff_close_memory (data)
11567 thandle_t data;
11568{
11569 /* NOOP */
11570 return 0;
11571}
11572
11573static int
11574tiff_mmap_memory (data, pbase, psize)
11575 thandle_t data;
11576 tdata_t *pbase;
11577 toff_t *psize;
11578{
11579 /* It is already _IN_ memory. */
11580 return 0;
11581}
11582
11583static void
11584tiff_unmap_memory (data, base, size)
11585 thandle_t data;
11586 tdata_t base;
11587 toff_t size;
11588{
11589 /* We don't need to do this. */
11590}
11591
11592static toff_t
11593tiff_size_of_memory (data)
11594 thandle_t data;
11595{
11596 return ((tiff_memory_source *) data)->len;
11597}
11598
3cf3436e
JR
11599
11600static void
11601tiff_error_handler (title, format, ap)
11602 const char *title, *format;
11603 va_list ap;
11604{
11605 char buf[512];
11606 int len;
7d0393cf 11607
3cf3436e
JR
11608 len = sprintf (buf, "TIFF error: %s ", title);
11609 vsprintf (buf + len, format, ap);
11610 add_to_log (buf, Qnil, Qnil);
11611}
11612
11613
11614static void
11615tiff_warning_handler (title, format, ap)
11616 const char *title, *format;
11617 va_list ap;
11618{
11619 char buf[512];
11620 int len;
7d0393cf 11621
3cf3436e
JR
11622 len = sprintf (buf, "TIFF warning: %s ", title);
11623 vsprintf (buf + len, format, ap);
11624 add_to_log (buf, Qnil, Qnil);
11625}
11626
11627
6fc2811b
JR
11628/* Load TIFF image IMG for use on frame F. Value is non-zero if
11629 successful. */
11630
11631static int
11632tiff_load (f, img)
11633 struct frame *f;
11634 struct image *img;
11635{
11636 Lisp_Object file, specified_file;
11637 Lisp_Object specified_data;
11638 TIFF *tiff;
11639 int width, height, x, y;
11640 uint32 *buf;
11641 int rc;
11642 XImage *ximg;
11643 struct gcpro gcpro1;
11644 tiff_memory_source memsrc;
11645
11646 specified_file = image_spec_value (img->spec, QCfile, NULL);
11647 specified_data = image_spec_value (img->spec, QCdata, NULL);
11648 file = Qnil;
11649 GCPRO1 (file);
11650
12b918b2
JB
11651 fn_TIFFSetErrorHandler (tiff_error_handler);
11652 fn_TIFFSetWarningHandler (tiff_warning_handler);
3cf3436e 11653
6fc2811b
JR
11654 if (NILP (specified_data))
11655 {
11656 /* Read from a file */
11657 file = x_find_image_file (specified_file);
11658 if (!STRINGP (file))
3cf3436e
JR
11659 {
11660 image_error ("Cannot find image file `%s'", file, Qnil);
11661 UNGCPRO;
11662 return 0;
11663 }
7d0393cf 11664
6fc2811b 11665 /* Try to open the image file. */
12b918b2 11666 tiff = fn_TIFFOpen (SDATA (file), "r");
6fc2811b 11667 if (tiff == NULL)
3cf3436e
JR
11668 {
11669 image_error ("Cannot open `%s'", file, Qnil);
11670 UNGCPRO;
11671 return 0;
11672 }
6fc2811b
JR
11673 }
11674 else
11675 {
11676 /* Memory source! */
d5db4077
KR
11677 memsrc.bytes = SDATA (specified_data);
11678 memsrc.len = SBYTES (specified_data);
6fc2811b
JR
11679 memsrc.index = 0;
11680
12b918b2
JB
11681 tiff = fn_TIFFClientOpen ("memory_source", "r", &memsrc,
11682 (TIFFReadWriteProc) tiff_read_from_memory,
11683 (TIFFReadWriteProc) tiff_write_from_memory,
11684 tiff_seek_in_memory,
11685 tiff_close_memory,
11686 tiff_size_of_memory,
11687 tiff_mmap_memory,
11688 tiff_unmap_memory);
6fc2811b
JR
11689
11690 if (!tiff)
11691 {
11692 image_error ("Cannot open memory source for `%s'", img->spec, Qnil);
11693 UNGCPRO;
11694 return 0;
11695 }
11696 }
11697
11698 /* Get width and height of the image, and allocate a raster buffer
11699 of width x height 32-bit values. */
12b918b2
JB
11700 fn_TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &width);
11701 fn_TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &height);
6fc2811b 11702 buf = (uint32 *) xmalloc (width * height * sizeof *buf);
7d0393cf 11703
12b918b2
JB
11704 rc = fn_TIFFReadRGBAImage (tiff, width, height, buf, 0);
11705 fn_TIFFClose (tiff);
6fc2811b
JR
11706 if (!rc)
11707 {
11708 image_error ("Error reading TIFF image `%s'", img->spec, Qnil);
11709 xfree (buf);
11710 UNGCPRO;
11711 return 0;
11712 }
11713
6fc2811b
JR
11714 /* Create the X image and pixmap. */
11715 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
11716 {
6fc2811b
JR
11717 xfree (buf);
11718 UNGCPRO;
11719 return 0;
11720 }
11721
12b918b2 11722#if 0 /* TODO: Color tables. */
6fc2811b
JR
11723 /* Initialize the color table. */
11724 init_color_table ();
12b918b2 11725#endif
6fc2811b
JR
11726
11727 /* Process the pixel raster. Origin is in the lower-left corner. */
11728 for (y = 0; y < height; ++y)
11729 {
11730 uint32 *row = buf + y * width;
7d0393cf 11731
6fc2811b
JR
11732 for (x = 0; x < width; ++x)
11733 {
11734 uint32 abgr = row[x];
12b918b2
JB
11735 int r = TIFFGetR (abgr);
11736 int g = TIFFGetG (abgr);
11737 int b = TIFFGetB (abgr);
11738#if 0 /* TODO: Color tables. */
7d0393cf 11739 XPutPixel (ximg, x, height - 1 - y, lookup_rgb_color (f, r, g, b));
12b918b2
JB
11740#else
11741 XPutPixel (ximg, x, height - 1 - y, PALETTERGB (r, g, b));
11742#endif
6fc2811b
JR
11743 }
11744 }
11745
12b918b2 11746#if 0 /* TODO: Color tables. */
6fc2811b
JR
11747 /* Remember the colors allocated for the image. Free the color table. */
11748 img->colors = colors_in_color_table (&img->ncolors);
11749 free_color_table ();
12b918b2 11750#endif
6fc2811b 11751
a05e2bae
JR
11752 img->width = width;
11753 img->height = height;
11754
11755 /* Maybe fill in the background field while we have ximg handy. */
11756 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
11757 IMAGE_BACKGROUND (img, f, ximg);
11758
6fc2811b
JR
11759 /* Put the image into the pixmap, then free the X image and its buffer. */
11760 x_put_x_image (f, ximg, img->pixmap, width, height);
11761 x_destroy_x_image (ximg);
11762 xfree (buf);
6fc2811b
JR
11763
11764 UNGCPRO;
11765 return 1;
11766}
11767
11768#endif /* HAVE_TIFF != 0 */
11769
11770
11771\f
11772/***********************************************************************
11773 GIF
11774 ***********************************************************************/
11775
11776#if HAVE_GIF
11777
1ffb278b 11778#define DrawText gif_DrawText
6fc2811b 11779#include <gif_lib.h>
1ffb278b 11780#undef DrawText
6fc2811b
JR
11781
11782static int gif_image_p P_ ((Lisp_Object object));
11783static int gif_load P_ ((struct frame *f, struct image *img));
11784
11785/* The symbol `gif' identifying images of this type. */
11786
11787Lisp_Object Qgif;
11788
11789/* Indices of image specification fields in gif_format, below. */
11790
11791enum gif_keyword_index
11792{
11793 GIF_TYPE,
11794 GIF_DATA,
11795 GIF_FILE,
11796 GIF_ASCENT,
11797 GIF_MARGIN,
11798 GIF_RELIEF,
11799 GIF_ALGORITHM,
11800 GIF_HEURISTIC_MASK,
a05e2bae 11801 GIF_MASK,
6fc2811b 11802 GIF_IMAGE,
a05e2bae 11803 GIF_BACKGROUND,
6fc2811b
JR
11804 GIF_LAST
11805};
11806
11807/* Vector of image_keyword structures describing the format
11808 of valid user-defined image specifications. */
11809
11810static struct image_keyword gif_format[GIF_LAST] =
11811{
11812 {":type", IMAGE_SYMBOL_VALUE, 1},
11813 {":data", IMAGE_STRING_VALUE, 0},
11814 {":file", IMAGE_STRING_VALUE, 0},
8f92c555 11815 {":ascent", IMAGE_ASCENT_VALUE, 0},
8edb0a6f 11816 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
6fc2811b 11817 {":relief", IMAGE_INTEGER_VALUE, 0},
a93f4566 11818 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6fc2811b 11819 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
a05e2bae
JR
11820 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
11821 {":image", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
11822 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
6fc2811b
JR
11823};
11824
11825/* Structure describing the image type `gif'. */
11826
11827static struct image_type gif_type =
11828{
11829 &Qgif,
11830 gif_image_p,
11831 gif_load,
11832 x_clear_image,
11833 NULL
11834};
11835
1ffb278b
JB
11836
11837/* GIF library details. */
11838DEF_IMGLIB_FN (DGifCloseFile);
11839DEF_IMGLIB_FN (DGifSlurp);
11840DEF_IMGLIB_FN (DGifOpen);
11841DEF_IMGLIB_FN (DGifOpenFileName);
11842
11843static int
11844init_gif_functions (library)
11845 HMODULE library;
11846{
11847 LOAD_IMGLIB_FN (library, DGifCloseFile);
11848 LOAD_IMGLIB_FN (library, DGifSlurp);
11849 LOAD_IMGLIB_FN (library, DGifOpen);
11850 LOAD_IMGLIB_FN (library, DGifOpenFileName);
11851 return 1;
11852}
11853
11854
6fc2811b
JR
11855/* Return non-zero if OBJECT is a valid GIF image specification. */
11856
11857static int
11858gif_image_p (object)
11859 Lisp_Object object;
11860{
11861 struct image_keyword fmt[GIF_LAST];
11862 bcopy (gif_format, fmt, sizeof fmt);
7d0393cf 11863
8f92c555 11864 if (!parse_image_spec (object, fmt, GIF_LAST, Qgif))
6fc2811b 11865 return 0;
7d0393cf 11866
6fc2811b
JR
11867 /* Must specify either the :data or :file keyword. */
11868 return fmt[GIF_FILE].count + fmt[GIF_DATA].count == 1;
11869}
11870
11871/* Reading a GIF image from memory
11872 Based on the PNG memory stuff to a certain extent. */
11873
11874typedef struct
11875{
11876 unsigned char *bytes;
11877 size_t len;
11878 int index;
11879}
11880gif_memory_source;
11881
11882/* Make the current memory source available to gif_read_from_memory.
11883 It's done this way because not all versions of libungif support
11884 a UserData field in the GifFileType structure. */
11885static gif_memory_source *current_gif_memory_src;
11886
11887static int
11888gif_read_from_memory (file, buf, len)
11889 GifFileType *file;
11890 GifByteType *buf;
11891 int len;
11892{
11893 gif_memory_source *src = current_gif_memory_src;
11894
11895 if (len > src->len - src->index)
11896 return -1;
11897
11898 bcopy (src->bytes + src->index, buf, len);
11899 src->index += len;
11900 return len;
11901}
11902
11903
11904/* Load GIF image IMG for use on frame F. Value is non-zero if
11905 successful. */
11906
11907static int
11908gif_load (f, img)
11909 struct frame *f;
11910 struct image *img;
11911{
11912 Lisp_Object file, specified_file;
11913 Lisp_Object specified_data;
11914 int rc, width, height, x, y, i;
11915 XImage *ximg;
11916 ColorMapObject *gif_color_map;
11917 unsigned long pixel_colors[256];
11918 GifFileType *gif;
11919 struct gcpro gcpro1;
11920 Lisp_Object image;
11921 int ino, image_left, image_top, image_width, image_height;
11922 gif_memory_source memsrc;
11923 unsigned char *raster;
11924
11925 specified_file = image_spec_value (img->spec, QCfile, NULL);
11926 specified_data = image_spec_value (img->spec, QCdata, NULL);
11927 file = Qnil;
dfff8a69 11928 GCPRO1 (file);
6fc2811b
JR
11929
11930 if (NILP (specified_data))
11931 {
11932 file = x_find_image_file (specified_file);
6fc2811b
JR
11933 if (!STRINGP (file))
11934 {
11935 image_error ("Cannot find image file `%s'", specified_file, Qnil);
11936 UNGCPRO;
11937 return 0;
11938 }
7d0393cf 11939
6fc2811b 11940 /* Open the GIF file. */
1ffb278b 11941 gif = fn_DGifOpenFileName (SDATA (file));
6fc2811b
JR
11942 if (gif == NULL)
11943 {
11944 image_error ("Cannot open `%s'", file, Qnil);
11945 UNGCPRO;
11946 return 0;
11947 }
11948 }
11949 else
11950 {
11951 /* Read from memory! */
11952 current_gif_memory_src = &memsrc;
d5db4077
KR
11953 memsrc.bytes = SDATA (specified_data);
11954 memsrc.len = SBYTES (specified_data);
6fc2811b
JR
11955 memsrc.index = 0;
11956
1ffb278b 11957 gif = fn_DGifOpen(&memsrc, gif_read_from_memory);
6fc2811b
JR
11958 if (!gif)
11959 {
11960 image_error ("Cannot open memory source `%s'", img->spec, Qnil);
11961 UNGCPRO;
11962 return 0;
11963 }
11964 }
11965
11966 /* Read entire contents. */
1ffb278b 11967 rc = fn_DGifSlurp (gif);
6fc2811b
JR
11968 if (rc == GIF_ERROR)
11969 {
11970 image_error ("Error reading `%s'", img->spec, Qnil);
1ffb278b 11971 fn_DGifCloseFile (gif);
6fc2811b
JR
11972 UNGCPRO;
11973 return 0;
11974 }
11975
11976 image = image_spec_value (img->spec, QCindex, NULL);
11977 ino = INTEGERP (image) ? XFASTINT (image) : 0;
11978 if (ino >= gif->ImageCount)
11979 {
11980 image_error ("Invalid image number `%s' in image `%s'",
11981 image, img->spec);
1ffb278b 11982 fn_DGifCloseFile (gif);
6fc2811b
JR
11983 UNGCPRO;
11984 return 0;
11985 }
11986
1ffb278b
JB
11987 width = img->width = max (gif->SWidth, gif->Image.Left + gif->Image.Width);
11988 height = img->height = max (gif->SHeight, gif->Image.Top + gif->Image.Height);
6fc2811b 11989
6fc2811b
JR
11990 /* Create the X image and pixmap. */
11991 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
11992 {
1ffb278b 11993 fn_DGifCloseFile (gif);
6fc2811b
JR
11994 UNGCPRO;
11995 return 0;
11996 }
7d0393cf 11997
6fc2811b
JR
11998 /* Allocate colors. */
11999 gif_color_map = gif->SavedImages[ino].ImageDesc.ColorMap;
12000 if (!gif_color_map)
12001 gif_color_map = gif->SColorMap;
1ffb278b 12002#if 0 /* TODO: Color tables */
6fc2811b 12003 init_color_table ();
1ffb278b 12004#endif
6fc2811b 12005 bzero (pixel_colors, sizeof pixel_colors);
7d0393cf 12006
6fc2811b
JR
12007 for (i = 0; i < gif_color_map->ColorCount; ++i)
12008 {
1ffb278b
JB
12009 int r = gif_color_map->Colors[i].Red;
12010 int g = gif_color_map->Colors[i].Green;
12011 int b = gif_color_map->Colors[i].Blue;
12012#if 0 /* TODO: Color tables */
6fc2811b 12013 pixel_colors[i] = lookup_rgb_color (f, r, g, b);
1ffb278b
JB
12014#else
12015 pixel_colors[i] = PALETTERGB (r, g, b);
12016#endif
6fc2811b
JR
12017 }
12018
1ffb278b 12019#if 0 /* TODO: Color tables */
6fc2811b
JR
12020 img->colors = colors_in_color_table (&img->ncolors);
12021 free_color_table ();
1ffb278b 12022#endif
6fc2811b
JR
12023
12024 /* Clear the part of the screen image that are not covered by
7d0393cf 12025 the image from the GIF file. Full animated GIF support
6fc2811b
JR
12026 requires more than can be done here (see the gif89 spec,
12027 disposal methods). Let's simply assume that the part
12028 not covered by a sub-image is in the frame's background color. */
12029 image_top = gif->SavedImages[ino].ImageDesc.Top;
12030 image_left = gif->SavedImages[ino].ImageDesc.Left;
12031 image_width = gif->SavedImages[ino].ImageDesc.Width;
12032 image_height = gif->SavedImages[ino].ImageDesc.Height;
12033
12034 for (y = 0; y < image_top; ++y)
12035 for (x = 0; x < width; ++x)
12036 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
12037
12038 for (y = image_top + image_height; y < height; ++y)
12039 for (x = 0; x < width; ++x)
12040 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
12041
12042 for (y = image_top; y < image_top + image_height; ++y)
12043 {
12044 for (x = 0; x < image_left; ++x)
12045 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
12046 for (x = image_left + image_width; x < width; ++x)
12047 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
12048 }
12049
12050 /* Read the GIF image into the X image. We use a local variable
12051 `raster' here because RasterBits below is a char *, and invites
12052 problems with bytes >= 0x80. */
12053 raster = (unsigned char *) gif->SavedImages[ino].RasterBits;
12054
12055 if (gif->SavedImages[ino].ImageDesc.Interlace)
12056 {
12057 static int interlace_start[] = {0, 4, 2, 1};
12058 static int interlace_increment[] = {8, 8, 4, 2};
a05e2bae 12059 int pass;
6fc2811b
JR
12060 int row = interlace_start[0];
12061
12062 pass = 0;
12063
12064 for (y = 0; y < image_height; y++)
12065 {
12066 if (row >= image_height)
12067 {
12068 row = interlace_start[++pass];
12069 while (row >= image_height)
12070 row = interlace_start[++pass];
12071 }
7d0393cf 12072
6fc2811b
JR
12073 for (x = 0; x < image_width; x++)
12074 {
12075 int i = raster[(y * image_width) + x];
12076 XPutPixel (ximg, x + image_left, row + image_top,
12077 pixel_colors[i]);
12078 }
7d0393cf 12079
6fc2811b
JR
12080 row += interlace_increment[pass];
12081 }
12082 }
12083 else
12084 {
12085 for (y = 0; y < image_height; ++y)
12086 for (x = 0; x < image_width; ++x)
12087 {
12088 int i = raster[y* image_width + x];
12089 XPutPixel (ximg, x + image_left, y + image_top, pixel_colors[i]);
12090 }
12091 }
7d0393cf 12092
1ffb278b 12093 fn_DGifCloseFile (gif);
a05e2bae
JR
12094
12095 /* Maybe fill in the background field while we have ximg handy. */
12096 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
12097 IMAGE_BACKGROUND (img, f, ximg);
12098
6fc2811b
JR
12099 /* Put the image into the pixmap, then free the X image and its buffer. */
12100 x_put_x_image (f, ximg, img->pixmap, width, height);
12101 x_destroy_x_image (ximg);
7d0393cf 12102
6fc2811b
JR
12103 UNGCPRO;
12104 return 1;
12105}
12106
12107#endif /* HAVE_GIF != 0 */
12108
12109
12110\f
12111/***********************************************************************
12112 Ghostscript
12113 ***********************************************************************/
12114
3cf3436e
JR
12115Lisp_Object Qpostscript;
12116
839b1909
JR
12117/* Keyword symbols. */
12118
12119Lisp_Object QCloader, QCbounding_box, QCpt_width, QCpt_height;
12120
6fc2811b
JR
12121#ifdef HAVE_GHOSTSCRIPT
12122static int gs_image_p P_ ((Lisp_Object object));
12123static int gs_load P_ ((struct frame *f, struct image *img));
12124static void gs_clear_image P_ ((struct frame *f, struct image *img));
12125
12126/* The symbol `postscript' identifying images of this type. */
12127
6fc2811b
JR
12128/* Keyword symbols. */
12129
12130Lisp_Object QCloader, QCbounding_box, QCpt_width, QCpt_height;
12131
12132/* Indices of image specification fields in gs_format, below. */
12133
12134enum gs_keyword_index
12135{
12136 GS_TYPE,
12137 GS_PT_WIDTH,
12138 GS_PT_HEIGHT,
12139 GS_FILE,
12140 GS_LOADER,
12141 GS_BOUNDING_BOX,
12142 GS_ASCENT,
12143 GS_MARGIN,
12144 GS_RELIEF,
12145 GS_ALGORITHM,
12146 GS_HEURISTIC_MASK,
a05e2bae
JR
12147 GS_MASK,
12148 GS_BACKGROUND,
6fc2811b
JR
12149 GS_LAST
12150};
12151
12152/* Vector of image_keyword structures describing the format
12153 of valid user-defined image specifications. */
12154
12155static struct image_keyword gs_format[GS_LAST] =
12156{
12157 {":type", IMAGE_SYMBOL_VALUE, 1},
12158 {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE, 1},
12159 {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE, 1},
12160 {":file", IMAGE_STRING_VALUE, 1},
12161 {":loader", IMAGE_FUNCTION_VALUE, 0},
12162 {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE, 1},
8f92c555 12163 {":ascent", IMAGE_ASCENT_VALUE, 0},
8edb0a6f 12164 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
6fc2811b 12165 {":relief", IMAGE_INTEGER_VALUE, 0},
a93f4566 12166 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
a05e2bae
JR
12167 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
12168 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
12169 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
6fc2811b
JR
12170};
12171
12172/* Structure describing the image type `ghostscript'. */
12173
12174static struct image_type gs_type =
12175{
12176 &Qpostscript,
12177 gs_image_p,
12178 gs_load,
12179 gs_clear_image,
12180 NULL
12181};
12182
12183
12184/* Free X resources of Ghostscript image IMG which is used on frame F. */
12185
12186static void
12187gs_clear_image (f, img)
12188 struct frame *f;
12189 struct image *img;
12190{
12191 /* IMG->data.ptr_val may contain a recorded colormap. */
12192 xfree (img->data.ptr_val);
12193 x_clear_image (f, img);
12194}
12195
12196
12197/* Return non-zero if OBJECT is a valid Ghostscript image
12198 specification. */
12199
12200static int
12201gs_image_p (object)
12202 Lisp_Object object;
12203{
12204 struct image_keyword fmt[GS_LAST];
12205 Lisp_Object tem;
12206 int i;
7d0393cf 12207
6fc2811b 12208 bcopy (gs_format, fmt, sizeof fmt);
7d0393cf 12209
8f92c555 12210 if (!parse_image_spec (object, fmt, GS_LAST, Qpostscript))
6fc2811b
JR
12211 return 0;
12212
12213 /* Bounding box must be a list or vector containing 4 integers. */
12214 tem = fmt[GS_BOUNDING_BOX].value;
12215 if (CONSP (tem))
12216 {
12217 for (i = 0; i < 4; ++i, tem = XCDR (tem))
12218 if (!CONSP (tem) || !INTEGERP (XCAR (tem)))
12219 return 0;
12220 if (!NILP (tem))
12221 return 0;
12222 }
12223 else if (VECTORP (tem))
12224 {
12225 if (XVECTOR (tem)->size != 4)
12226 return 0;
12227 for (i = 0; i < 4; ++i)
12228 if (!INTEGERP (XVECTOR (tem)->contents[i]))
12229 return 0;
12230 }
12231 else
12232 return 0;
12233
12234 return 1;
12235}
12236
12237
12238/* Load Ghostscript image IMG for use on frame F. Value is non-zero
12239 if successful. */
12240
12241static int
12242gs_load (f, img)
12243 struct frame *f;
12244 struct image *img;
12245{
12246 char buffer[100];
12247 Lisp_Object window_and_pixmap_id = Qnil, loader, pt_height, pt_width;
12248 struct gcpro gcpro1, gcpro2;
12249 Lisp_Object frame;
12250 double in_width, in_height;
12251 Lisp_Object pixel_colors = Qnil;
12252
12253 /* Compute pixel size of pixmap needed from the given size in the
12254 image specification. Sizes in the specification are in pt. 1 pt
12255 = 1/72 in, xdpi and ydpi are stored in the frame's X display
12256 info. */
12257 pt_width = image_spec_value (img->spec, QCpt_width, NULL);
12258 in_width = XFASTINT (pt_width) / 72.0;
12259 img->width = in_width * FRAME_W32_DISPLAY_INFO (f)->resx;
12260 pt_height = image_spec_value (img->spec, QCpt_height, NULL);
12261 in_height = XFASTINT (pt_height) / 72.0;
12262 img->height = in_height * FRAME_W32_DISPLAY_INFO (f)->resy;
12263
12264 /* Create the pixmap. */
12265 BLOCK_INPUT;
12266 xassert (img->pixmap == 0);
12267 img->pixmap = XCreatePixmap (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
12268 img->width, img->height,
a05e2bae 12269 one_w32_display_info.n_cbits);
6fc2811b
JR
12270 UNBLOCK_INPUT;
12271
12272 if (!img->pixmap)
12273 {
12274 image_error ("Unable to create pixmap for `%s'", img->spec, Qnil);
12275 return 0;
12276 }
7d0393cf 12277
6fc2811b
JR
12278 /* Call the loader to fill the pixmap. It returns a process object
12279 if successful. We do not record_unwind_protect here because
12280 other places in redisplay like calling window scroll functions
12281 don't either. Let the Lisp loader use `unwind-protect' instead. */
12282 GCPRO2 (window_and_pixmap_id, pixel_colors);
12283
12284 sprintf (buffer, "%lu %lu",
12285 (unsigned long) FRAME_W32_WINDOW (f),
12286 (unsigned long) img->pixmap);
12287 window_and_pixmap_id = build_string (buffer);
7d0393cf 12288
6fc2811b
JR
12289 sprintf (buffer, "%lu %lu",
12290 FRAME_FOREGROUND_PIXEL (f),
12291 FRAME_BACKGROUND_PIXEL (f));
12292 pixel_colors = build_string (buffer);
7d0393cf 12293
6fc2811b
JR
12294 XSETFRAME (frame, f);
12295 loader = image_spec_value (img->spec, QCloader, NULL);
12296 if (NILP (loader))
12297 loader = intern ("gs-load-image");
12298
12299 img->data.lisp_val = call6 (loader, frame, img->spec,
12300 make_number (img->width),
12301 make_number (img->height),
12302 window_and_pixmap_id,
12303 pixel_colors);
12304 UNGCPRO;
12305 return PROCESSP (img->data.lisp_val);
12306}
12307
12308
12309/* Kill the Ghostscript process that was started to fill PIXMAP on
12310 frame F. Called from XTread_socket when receiving an event
12311 telling Emacs that Ghostscript has finished drawing. */
12312
12313void
12314x_kill_gs_process (pixmap, f)
12315 Pixmap pixmap;
12316 struct frame *f;
12317{
12318 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
12319 int class, i;
12320 struct image *img;
12321
12322 /* Find the image containing PIXMAP. */
12323 for (i = 0; i < c->used; ++i)
12324 if (c->images[i]->pixmap == pixmap)
12325 break;
12326
3cf3436e
JR
12327 /* Should someone in between have cleared the image cache, for
12328 instance, give up. */
12329 if (i == c->used)
12330 return;
12331
6fc2811b
JR
12332 /* Kill the GS process. We should have found PIXMAP in the image
12333 cache and its image should contain a process object. */
6fc2811b
JR
12334 img = c->images[i];
12335 xassert (PROCESSP (img->data.lisp_val));
12336 Fkill_process (img->data.lisp_val, Qnil);
12337 img->data.lisp_val = Qnil;
12338
12339 /* On displays with a mutable colormap, figure out the colors
12340 allocated for the image by looking at the pixels of an XImage for
12341 img->pixmap. */
12342 class = FRAME_W32_DISPLAY_INFO (f)->visual->class;
12343 if (class != StaticColor && class != StaticGray && class != TrueColor)
12344 {
12345 XImage *ximg;
12346
12347 BLOCK_INPUT;
12348
12349 /* Try to get an XImage for img->pixmep. */
12350 ximg = XGetImage (FRAME_W32_DISPLAY (f), img->pixmap,
12351 0, 0, img->width, img->height, ~0, ZPixmap);
12352 if (ximg)
12353 {
12354 int x, y;
7d0393cf 12355
6fc2811b
JR
12356 /* Initialize the color table. */
12357 init_color_table ();
7d0393cf 12358
6fc2811b
JR
12359 /* For each pixel of the image, look its color up in the
12360 color table. After having done so, the color table will
12361 contain an entry for each color used by the image. */
12362 for (y = 0; y < img->height; ++y)
12363 for (x = 0; x < img->width; ++x)
12364 {
12365 unsigned long pixel = XGetPixel (ximg, x, y);
12366 lookup_pixel_color (f, pixel);
12367 }
12368
12369 /* Record colors in the image. Free color table and XImage. */
12370 img->colors = colors_in_color_table (&img->ncolors);
12371 free_color_table ();
12372 XDestroyImage (ximg);
12373
12374#if 0 /* This doesn't seem to be the case. If we free the colors
12375 here, we get a BadAccess later in x_clear_image when
12376 freeing the colors. */
12377 /* We have allocated colors once, but Ghostscript has also
12378 allocated colors on behalf of us. So, to get the
12379 reference counts right, free them once. */
12380 if (img->ncolors)
3cf3436e 12381 x_free_colors (FRAME_W32_DISPLAY (f), cmap,
6fc2811b 12382 img->colors, img->ncolors, 0);
6fc2811b
JR
12383#endif
12384 }
12385 else
12386 image_error ("Cannot get X image of `%s'; colors will not be freed",
12387 img->spec, Qnil);
7d0393cf 12388
6fc2811b
JR
12389 UNBLOCK_INPUT;
12390 }
3cf3436e
JR
12391
12392 /* Now that we have the pixmap, compute mask and transform the
12393 image if requested. */
12394 BLOCK_INPUT;
12395 postprocess_image (f, img);
12396 UNBLOCK_INPUT;
6fc2811b
JR
12397}
12398
12399#endif /* HAVE_GHOSTSCRIPT */
12400
12401\f
12402/***********************************************************************
12403 Window properties
12404 ***********************************************************************/
12405
12406DEFUN ("x-change-window-property", Fx_change_window_property,
12407 Sx_change_window_property, 2, 3, 0,
74e1aeec
JR
12408 doc: /* Change window property PROP to VALUE on the X window of FRAME.
12409PROP and VALUE must be strings. FRAME nil or omitted means use the
12410selected frame. Value is VALUE. */)
6fc2811b
JR
12411 (prop, value, frame)
12412 Lisp_Object frame, prop, value;
12413{
767b1ff0 12414#if 0 /* TODO : port window properties to W32 */
6fc2811b
JR
12415 struct frame *f = check_x_frame (frame);
12416 Atom prop_atom;
12417
b7826503
PJ
12418 CHECK_STRING (prop);
12419 CHECK_STRING (value);
6fc2811b
JR
12420
12421 BLOCK_INPUT;
d5db4077 12422 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
6fc2811b
JR
12423 XChangeProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
12424 prop_atom, XA_STRING, 8, PropModeReplace,
d5db4077 12425 SDATA (value), SCHARS (value));
6fc2811b
JR
12426
12427 /* Make sure the property is set when we return. */
12428 XFlush (FRAME_W32_DISPLAY (f));
12429 UNBLOCK_INPUT;
12430
767b1ff0 12431#endif /* TODO */
6fc2811b
JR
12432
12433 return value;
12434}
12435
12436
12437DEFUN ("x-delete-window-property", Fx_delete_window_property,
12438 Sx_delete_window_property, 1, 2, 0,
74e1aeec
JR
12439 doc: /* Remove window property PROP from X window of FRAME.
12440FRAME nil or omitted means use the selected frame. Value is PROP. */)
6fc2811b
JR
12441 (prop, frame)
12442 Lisp_Object prop, frame;
12443{
767b1ff0 12444#if 0 /* TODO : port window properties to W32 */
6fc2811b
JR
12445
12446 struct frame *f = check_x_frame (frame);
12447 Atom prop_atom;
12448
b7826503 12449 CHECK_STRING (prop);
6fc2811b 12450 BLOCK_INPUT;
d5db4077 12451 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
6fc2811b
JR
12452 XDeleteProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), prop_atom);
12453
12454 /* Make sure the property is removed when we return. */
12455 XFlush (FRAME_W32_DISPLAY (f));
12456 UNBLOCK_INPUT;
767b1ff0 12457#endif /* TODO */
6fc2811b
JR
12458
12459 return prop;
12460}
12461
12462
12463DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
12464 1, 2, 0,
74e1aeec
JR
12465 doc: /* Value is the value of window property PROP on FRAME.
12466If FRAME is nil or omitted, use the selected frame. Value is nil
12467if FRAME hasn't a property with name PROP or if PROP has no string
12468value. */)
6fc2811b
JR
12469 (prop, frame)
12470 Lisp_Object prop, frame;
12471{
767b1ff0 12472#if 0 /* TODO : port window properties to W32 */
6fc2811b
JR
12473
12474 struct frame *f = check_x_frame (frame);
12475 Atom prop_atom;
12476 int rc;
12477 Lisp_Object prop_value = Qnil;
12478 char *tmp_data = NULL;
12479 Atom actual_type;
12480 int actual_format;
12481 unsigned long actual_size, bytes_remaining;
12482
b7826503 12483 CHECK_STRING (prop);
6fc2811b 12484 BLOCK_INPUT;
d5db4077 12485 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), SDATA (prop), False);
6fc2811b
JR
12486 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
12487 prop_atom, 0, 0, False, XA_STRING,
12488 &actual_type, &actual_format, &actual_size,
12489 &bytes_remaining, (unsigned char **) &tmp_data);
12490 if (rc == Success)
12491 {
12492 int size = bytes_remaining;
12493
12494 XFree (tmp_data);
12495 tmp_data = NULL;
12496
12497 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
12498 prop_atom, 0, bytes_remaining,
12499 False, XA_STRING,
7d0393cf
JB
12500 &actual_type, &actual_format,
12501 &actual_size, &bytes_remaining,
6fc2811b
JR
12502 (unsigned char **) &tmp_data);
12503 if (rc == Success)
12504 prop_value = make_string (tmp_data, size);
12505
12506 XFree (tmp_data);
12507 }
12508
12509 UNBLOCK_INPUT;
12510
12511 return prop_value;
12512
767b1ff0 12513#endif /* TODO */
6fc2811b
JR
12514 return Qnil;
12515}
12516
12517
12518\f
12519/***********************************************************************
12520 Busy cursor
12521 ***********************************************************************/
12522
f79e6790 12523/* If non-null, an asynchronous timer that, when it expires, displays
0af913d7 12524 an hourglass cursor on all frames. */
6fc2811b 12525
0af913d7 12526static struct atimer *hourglass_atimer;
6fc2811b 12527
0af913d7 12528/* Non-zero means an hourglass cursor is currently shown. */
6fc2811b 12529
0af913d7 12530static int hourglass_shown_p;
6fc2811b 12531
0af913d7 12532/* Number of seconds to wait before displaying an hourglass cursor. */
6fc2811b 12533
0af913d7 12534static Lisp_Object Vhourglass_delay;
6fc2811b 12535
0af913d7 12536/* Default number of seconds to wait before displaying an hourglass
f79e6790
JR
12537 cursor. */
12538
0af913d7 12539#define DEFAULT_HOURGLASS_DELAY 1
f79e6790
JR
12540
12541/* Function prototypes. */
12542
0af913d7
GM
12543static void show_hourglass P_ ((struct atimer *));
12544static void hide_hourglass P_ ((void));
f79e6790
JR
12545
12546
0af913d7 12547/* Cancel a currently active hourglass timer, and start a new one. */
f79e6790
JR
12548
12549void
0af913d7 12550start_hourglass ()
f79e6790 12551{
767b1ff0 12552#if 0 /* TODO: cursor shape changes. */
f79e6790 12553 EMACS_TIME delay;
dfff8a69 12554 int secs, usecs = 0;
7d0393cf 12555
0af913d7 12556 cancel_hourglass ();
f79e6790 12557
0af913d7
GM
12558 if (INTEGERP (Vhourglass_delay)
12559 && XINT (Vhourglass_delay) > 0)
12560 secs = XFASTINT (Vhourglass_delay);
12561 else if (FLOATP (Vhourglass_delay)
12562 && XFLOAT_DATA (Vhourglass_delay) > 0)
dfff8a69
JR
12563 {
12564 Lisp_Object tem;
0af913d7 12565 tem = Ftruncate (Vhourglass_delay, Qnil);
dfff8a69 12566 secs = XFASTINT (tem);
0af913d7 12567 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
dfff8a69 12568 }
f79e6790 12569 else
0af913d7 12570 secs = DEFAULT_HOURGLASS_DELAY;
7d0393cf 12571
dfff8a69 12572 EMACS_SET_SECS_USECS (delay, secs, usecs);
0af913d7
GM
12573 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
12574 show_hourglass, NULL);
f79e6790
JR
12575#endif
12576}
12577
12578
0af913d7
GM
12579/* Cancel the hourglass cursor timer if active, hide an hourglass
12580 cursor if shown. */
f79e6790
JR
12581
12582void
0af913d7 12583cancel_hourglass ()
f79e6790 12584{
0af913d7 12585 if (hourglass_atimer)
dfff8a69 12586 {
0af913d7
GM
12587 cancel_atimer (hourglass_atimer);
12588 hourglass_atimer = NULL;
dfff8a69 12589 }
7d0393cf 12590
0af913d7
GM
12591 if (hourglass_shown_p)
12592 hide_hourglass ();
f79e6790
JR
12593}
12594
12595
0af913d7
GM
12596/* Timer function of hourglass_atimer. TIMER is equal to
12597 hourglass_atimer.
f79e6790 12598
0af913d7
GM
12599 Display an hourglass cursor on all frames by mapping the frames'
12600 hourglass_window. Set the hourglass_p flag in the frames'
12601 output_data.x structure to indicate that an hourglass cursor is
12602 shown on the frames. */
f79e6790
JR
12603
12604static void
0af913d7 12605show_hourglass (timer)
f79e6790 12606 struct atimer *timer;
6fc2811b 12607{
767b1ff0 12608#if 0 /* TODO: cursor shape changes. */
f79e6790 12609 /* The timer implementation will cancel this timer automatically
0af913d7 12610 after this function has run. Set hourglass_atimer to null
f79e6790 12611 so that we know the timer doesn't have to be canceled. */
0af913d7 12612 hourglass_atimer = NULL;
f79e6790 12613
0af913d7 12614 if (!hourglass_shown_p)
6fc2811b
JR
12615 {
12616 Lisp_Object rest, frame;
7d0393cf 12617
f79e6790 12618 BLOCK_INPUT;
7d0393cf 12619
6fc2811b 12620 FOR_EACH_FRAME (rest, frame)
dc220243 12621 if (FRAME_W32_P (XFRAME (frame)))
6fc2811b
JR
12622 {
12623 struct frame *f = XFRAME (frame);
7d0393cf 12624
0af913d7 12625 f->output_data.w32->hourglass_p = 1;
7d0393cf 12626
0af913d7 12627 if (!f->output_data.w32->hourglass_window)
6fc2811b
JR
12628 {
12629 unsigned long mask = CWCursor;
12630 XSetWindowAttributes attrs;
7d0393cf 12631
0af913d7 12632 attrs.cursor = f->output_data.w32->hourglass_cursor;
7d0393cf 12633
0af913d7 12634 f->output_data.w32->hourglass_window
f79e6790 12635 = XCreateWindow (FRAME_X_DISPLAY (f),
6fc2811b
JR
12636 FRAME_OUTER_WINDOW (f),
12637 0, 0, 32000, 32000, 0, 0,
f79e6790
JR
12638 InputOnly,
12639 CopyFromParent,
6fc2811b
JR
12640 mask, &attrs);
12641 }
7d0393cf 12642
0af913d7
GM
12643 XMapRaised (FRAME_X_DISPLAY (f),
12644 f->output_data.w32->hourglass_window);
f79e6790 12645 XFlush (FRAME_X_DISPLAY (f));
6fc2811b 12646 }
6fc2811b 12647
0af913d7 12648 hourglass_shown_p = 1;
f79e6790
JR
12649 UNBLOCK_INPUT;
12650 }
12651#endif
6fc2811b
JR
12652}
12653
12654
0af913d7 12655/* Hide the hourglass cursor on all frames, if it is currently shown. */
6fc2811b 12656
f79e6790 12657static void
0af913d7 12658hide_hourglass ()
f79e6790 12659{
767b1ff0 12660#if 0 /* TODO: cursor shape changes. */
0af913d7 12661 if (hourglass_shown_p)
6fc2811b 12662 {
f79e6790
JR
12663 Lisp_Object rest, frame;
12664
12665 BLOCK_INPUT;
12666 FOR_EACH_FRAME (rest, frame)
6fc2811b 12667 {
f79e6790 12668 struct frame *f = XFRAME (frame);
7d0393cf 12669
dc220243 12670 if (FRAME_W32_P (f)
f79e6790 12671 /* Watch out for newly created frames. */
0af913d7 12672 && f->output_data.x->hourglass_window)
f79e6790 12673 {
0af913d7
GM
12674 XUnmapWindow (FRAME_X_DISPLAY (f),
12675 f->output_data.x->hourglass_window);
12676 /* Sync here because XTread_socket looks at the
12677 hourglass_p flag that is reset to zero below. */
f79e6790 12678 XSync (FRAME_X_DISPLAY (f), False);
0af913d7 12679 f->output_data.x->hourglass_p = 0;
f79e6790 12680 }
6fc2811b 12681 }
6fc2811b 12682
0af913d7 12683 hourglass_shown_p = 0;
f79e6790
JR
12684 UNBLOCK_INPUT;
12685 }
12686#endif
6fc2811b
JR
12687}
12688
12689
12690\f
12691/***********************************************************************
12692 Tool tips
12693 ***********************************************************************/
12694
12695static Lisp_Object x_create_tip_frame P_ ((struct w32_display_info *,
3cf3436e
JR
12696 Lisp_Object, Lisp_Object));
12697static void compute_tip_xy P_ ((struct frame *, Lisp_Object, Lisp_Object,
12698 Lisp_Object, int, int, int *, int *));
7d0393cf 12699
3cf3436e 12700/* The frame of a currently visible tooltip. */
6fc2811b 12701
937e601e 12702Lisp_Object tip_frame;
6fc2811b
JR
12703
12704/* If non-nil, a timer started that hides the last tooltip when it
12705 fires. */
12706
12707Lisp_Object tip_timer;
12708Window tip_window;
12709
3cf3436e
JR
12710/* If non-nil, a vector of 3 elements containing the last args
12711 with which x-show-tip was called. See there. */
12712
12713Lisp_Object last_show_tip_args;
12714
12715/* Maximum size for tooltips; a cons (COLUMNS . ROWS). */
12716
12717Lisp_Object Vx_max_tooltip_size;
12718
12719
937e601e
AI
12720static Lisp_Object
12721unwind_create_tip_frame (frame)
12722 Lisp_Object frame;
12723{
c844a81a
GM
12724 Lisp_Object deleted;
12725
12726 deleted = unwind_create_frame (frame);
12727 if (EQ (deleted, Qt))
12728 {
12729 tip_window = NULL;
12730 tip_frame = Qnil;
12731 }
7d0393cf 12732
c844a81a 12733 return deleted;
937e601e
AI
12734}
12735
12736
6fc2811b 12737/* Create a frame for a tooltip on the display described by DPYINFO.
3cf3436e
JR
12738 PARMS is a list of frame parameters. TEXT is the string to
12739 display in the tip frame. Value is the frame.
937e601e
AI
12740
12741 Note that functions called here, esp. x_default_parameter can
12742 signal errors, for instance when a specified color name is
12743 undefined. We have to make sure that we're in a consistent state
12744 when this happens. */
6fc2811b
JR
12745
12746static Lisp_Object
3cf3436e 12747x_create_tip_frame (dpyinfo, parms, text)
6fc2811b 12748 struct w32_display_info *dpyinfo;
3cf3436e 12749 Lisp_Object parms, text;
6fc2811b 12750{
6fc2811b
JR
12751 struct frame *f;
12752 Lisp_Object frame, tem;
12753 Lisp_Object name;
12754 long window_prompting = 0;
12755 int width, height;
331379bf 12756 int count = SPECPDL_INDEX ();
6fc2811b
JR
12757 struct gcpro gcpro1, gcpro2, gcpro3;
12758 struct kboard *kb;
3cf3436e
JR
12759 int face_change_count_before = face_change_count;
12760 Lisp_Object buffer;
12761 struct buffer *old_buffer;
6fc2811b 12762
ca56d953 12763 check_w32 ();
6fc2811b
JR
12764
12765 /* Use this general default value to start with until we know if
12766 this frame has a specified name. */
12767 Vx_resource_name = Vinvocation_name;
12768
12769#ifdef MULTI_KBOARD
12770 kb = dpyinfo->kboard;
12771#else
12772 kb = &the_only_kboard;
12773#endif
12774
12775 /* Get the name of the frame to use for resource lookup. */
12776 name = w32_get_arg (parms, Qname, "name", "Name", RES_TYPE_STRING);
12777 if (!STRINGP (name)
12778 && !EQ (name, Qunbound)
12779 && !NILP (name))
12780 error ("Invalid frame name--not a string or nil");
12781 Vx_resource_name = name;
12782
12783 frame = Qnil;
12784 GCPRO3 (parms, name, frame);
9eb16b62
JR
12785 /* Make a frame without minibuffer nor mode-line. */
12786 f = make_frame (0);
12787 f->wants_modeline = 0;
6fc2811b 12788 XSETFRAME (frame, f);
3cf3436e
JR
12789
12790 buffer = Fget_buffer_create (build_string (" *tip*"));
be786000 12791 Fset_window_buffer (FRAME_ROOT_WINDOW (f), buffer, Qnil);
3cf3436e
JR
12792 old_buffer = current_buffer;
12793 set_buffer_internal_1 (XBUFFER (buffer));
12794 current_buffer->truncate_lines = Qnil;
12795 Ferase_buffer ();
12796 Finsert (1, &text);
12797 set_buffer_internal_1 (old_buffer);
7d0393cf 12798
6fc2811b 12799 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
937e601e 12800 record_unwind_protect (unwind_create_tip_frame, frame);
6fc2811b 12801
3cf3436e
JR
12802 /* By setting the output method, we're essentially saying that
12803 the frame is live, as per FRAME_LIVE_P. If we get a signal
12804 from this point on, x_destroy_window might screw up reference
12805 counts etc. */
d88c567c 12806 f->output_method = output_w32;
6fc2811b
JR
12807 f->output_data.w32 =
12808 (struct w32_output *) xmalloc (sizeof (struct w32_output));
12809 bzero (f->output_data.w32, sizeof (struct w32_output));
ca56d953
JR
12810
12811 FRAME_FONTSET (f) = -1;
6fc2811b
JR
12812 f->icon_name = Qnil;
12813
ca56d953 12814#if 0 /* GLYPH_DEBUG TODO: image support. */
937e601e
AI
12815 image_cache_refcount = FRAME_X_IMAGE_CACHE (f)->refcount;
12816 dpyinfo_refcount = dpyinfo->reference_count;
12817#endif /* GLYPH_DEBUG */
6fc2811b
JR
12818#ifdef MULTI_KBOARD
12819 FRAME_KBOARD (f) = kb;
12820#endif
12821 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
12822 f->output_data.w32->explicit_parent = 0;
12823
12824 /* Set the name; the functions to which we pass f expect the name to
12825 be set. */
12826 if (EQ (name, Qunbound) || NILP (name))
12827 {
ca56d953 12828 f->name = build_string (dpyinfo->w32_id_name);
6fc2811b
JR
12829 f->explicit_name = 0;
12830 }
12831 else
12832 {
12833 f->name = name;
12834 f->explicit_name = 1;
12835 /* use the frame's title when getting resources for this frame. */
12836 specbind (Qx_resource_name, name);
12837 }
12838
6fc2811b
JR
12839 /* Extract the window parameters from the supplied values
12840 that are needed to determine window geometry. */
12841 {
12842 Lisp_Object font;
12843
12844 font = w32_get_arg (parms, Qfont, "font", "Font", RES_TYPE_STRING);
12845
12846 BLOCK_INPUT;
12847 /* First, try whatever font the caller has specified. */
12848 if (STRINGP (font))
12849 {
12850 tem = Fquery_fontset (font, Qnil);
12851 if (STRINGP (tem))
d5db4077 12852 font = x_new_fontset (f, SDATA (tem));
6fc2811b 12853 else
d5db4077 12854 font = x_new_font (f, SDATA (font));
6fc2811b 12855 }
7d0393cf 12856
6fc2811b
JR
12857 /* Try out a font which we hope has bold and italic variations. */
12858 if (!STRINGP (font))
ca56d953 12859 font = x_new_font (f, "-*-Courier New-normal-r-*-*-*-100-*-*-c-*-iso8859-1");
6fc2811b 12860 if (! STRINGP (font))
ca56d953 12861 font = x_new_font (f, "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1");
6fc2811b
JR
12862 /* If those didn't work, look for something which will at least work. */
12863 if (! STRINGP (font))
ca56d953 12864 font = x_new_font (f, "-*-Fixedsys-normal-r-*-*-12-*-*-*-c-*-iso8859-1");
6fc2811b
JR
12865 UNBLOCK_INPUT;
12866 if (! STRINGP (font))
ca56d953 12867 font = build_string ("Fixedsys");
6fc2811b
JR
12868
12869 x_default_parameter (f, parms, Qfont, font,
12870 "font", "Font", RES_TYPE_STRING);
12871 }
12872
12873 x_default_parameter (f, parms, Qborder_width, make_number (2),
12874 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
6fc2811b
JR
12875 /* This defaults to 2 in order to match xterm. We recognize either
12876 internalBorderWidth or internalBorder (which is what xterm calls
12877 it). */
12878 if (NILP (Fassq (Qinternal_border_width, parms)))
12879 {
12880 Lisp_Object value;
12881
12882 value = w32_get_arg (parms, Qinternal_border_width,
12883 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
12884 if (! EQ (value, Qunbound))
12885 parms = Fcons (Fcons (Qinternal_border_width, value),
12886 parms);
12887 }
bfd6edcc 12888 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
6fc2811b
JR
12889 "internalBorderWidth", "internalBorderWidth",
12890 RES_TYPE_NUMBER);
12891
12892 /* Also do the stuff which must be set before the window exists. */
12893 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
12894 "foreground", "Foreground", RES_TYPE_STRING);
12895 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
12896 "background", "Background", RES_TYPE_STRING);
12897 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
12898 "pointerColor", "Foreground", RES_TYPE_STRING);
12899 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
12900 "cursorColor", "Foreground", RES_TYPE_STRING);
12901 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
12902 "borderColor", "BorderColor", RES_TYPE_STRING);
12903
12904 /* Init faces before x_default_parameter is called for scroll-bar
12905 parameters because that function calls x_set_scroll_bar_width,
12906 which calls change_frame_size, which calls Fset_window_buffer,
12907 which runs hooks, which call Fvertical_motion. At the end, we
12908 end up in init_iterator with a null face cache, which should not
12909 happen. */
12910 init_frame_faces (f);
ca56d953
JR
12911
12912 f->output_data.w32->dwStyle = WS_BORDER | WS_POPUP | WS_DISABLED;
6fc2811b 12913 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
9eb16b62 12914
6d906347 12915 window_prompting = x_figure_window_size (f, parms, 0);
6fc2811b 12916
9eb16b62 12917 /* No fringes on tip frame. */
be786000
KS
12918 f->fringe_cols = 0;
12919 f->left_fringe_width = 0;
12920 f->right_fringe_width = 0;
9eb16b62 12921
ca56d953
JR
12922 BLOCK_INPUT;
12923 my_create_tip_window (f);
12924 UNBLOCK_INPUT;
6fc2811b
JR
12925
12926 x_make_gc (f);
12927
12928 x_default_parameter (f, parms, Qauto_raise, Qnil,
12929 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
12930 x_default_parameter (f, parms, Qauto_lower, Qnil,
12931 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
12932 x_default_parameter (f, parms, Qcursor_type, Qbox,
12933 "cursorType", "CursorType", RES_TYPE_SYMBOL);
12934
be786000 12935 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
6fc2811b 12936 Change will not be effected unless different from the current
be786000
KS
12937 FRAME_LINES (f). */
12938 width = FRAME_COLS (f);
12939 height = FRAME_LINES (f);
12940 FRAME_LINES (f) = 0;
12941 SET_FRAME_COLS (f, 0);
6fc2811b
JR
12942 change_frame_size (f, height, width, 1, 0, 0);
12943
cd1d850f
JPW
12944 /* Add `tooltip' frame parameter's default value. */
12945 if (NILP (Fframe_parameter (frame, intern ("tooltip"))))
12946 Fmodify_frame_parameters (frame, Fcons (Fcons (intern ("tooltip"), Qt),
12947 Qnil));
7d0393cf 12948
3cf3436e
JR
12949 /* Set up faces after all frame parameters are known. This call
12950 also merges in face attributes specified for new frames.
12951
12952 Frame parameters may be changed if .Xdefaults contains
12953 specifications for the default font. For example, if there is an
12954 `Emacs.default.attributeBackground: pink', the `background-color'
12955 attribute of the frame get's set, which let's the internal border
12956 of the tooltip frame appear in pink. Prevent this. */
12957 {
12958 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
12959
12960 /* Set tip_frame here, so that */
12961 tip_frame = frame;
12962 call1 (Qface_set_after_frame_default, frame);
7d0393cf 12963
3cf3436e
JR
12964 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
12965 Fmodify_frame_parameters (frame, Fcons (Fcons (Qbackground_color, bg),
12966 Qnil));
12967 }
7d0393cf 12968
6fc2811b
JR
12969 f->no_split = 1;
12970
12971 UNGCPRO;
12972
12973 /* It is now ok to make the frame official even if we get an error
12974 below. And the frame needs to be on Vframe_list or making it
12975 visible won't work. */
12976 Vframe_list = Fcons (frame, Vframe_list);
12977
12978 /* Now that the frame is official, it counts as a reference to
12979 its display. */
12980 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
ee78dc32 12981
3cf3436e
JR
12982 /* Setting attributes of faces of the tooltip frame from resources
12983 and similar will increment face_change_count, which leads to the
12984 clearing of all current matrices. Since this isn't necessary
12985 here, avoid it by resetting face_change_count to the value it
12986 had before we created the tip frame. */
12987 face_change_count = face_change_count_before;
12988
12989 /* Discard the unwind_protect. */
6fc2811b 12990 return unbind_to (count, frame);
ee78dc32
GV
12991}
12992
3cf3436e
JR
12993
12994/* Compute where to display tip frame F. PARMS is the list of frame
12995 parameters for F. DX and DY are specified offsets from the current
12996 location of the mouse. WIDTH and HEIGHT are the width and height
12997 of the tooltip. Return coordinates relative to the root window of
12998 the display in *ROOT_X, and *ROOT_Y. */
12999
13000static void
13001compute_tip_xy (f, parms, dx, dy, width, height, root_x, root_y)
13002 struct frame *f;
13003 Lisp_Object parms, dx, dy;
13004 int width, height;
13005 int *root_x, *root_y;
13006{
3cf3436e 13007 Lisp_Object left, top;
7d0393cf 13008
3cf3436e
JR
13009 /* User-specified position? */
13010 left = Fcdr (Fassq (Qleft, parms));
13011 top = Fcdr (Fassq (Qtop, parms));
7d0393cf 13012
3cf3436e
JR
13013 /* Move the tooltip window where the mouse pointer is. Resize and
13014 show it. */
ca56d953 13015 if (!INTEGERP (left) || !INTEGERP (top))
3cf3436e 13016 {
ca56d953
JR
13017 POINT pt;
13018
3cf3436e 13019 BLOCK_INPUT;
ca56d953
JR
13020 GetCursorPos (&pt);
13021 *root_x = pt.x;
13022 *root_y = pt.y;
3cf3436e
JR
13023 UNBLOCK_INPUT;
13024 }
13025
13026 if (INTEGERP (top))
13027 *root_y = XINT (top);
13028 else if (*root_y + XINT (dy) - height < 0)
13029 *root_y -= XINT (dy);
13030 else
13031 {
13032 *root_y -= height;
13033 *root_y += XINT (dy);
13034 }
13035
13036 if (INTEGERP (left))
13037 *root_x = XINT (left);
72e4adef
JR
13038 else if (*root_x + XINT (dx) + width <= FRAME_W32_DISPLAY_INFO (f)->width)
13039 /* It fits to the right of the pointer. */
13040 *root_x += XINT (dx);
13041 else if (width + XINT (dx) <= *root_x)
13042 /* It fits to the left of the pointer. */
3cf3436e
JR
13043 *root_x -= width + XINT (dx);
13044 else
72e4adef
JR
13045 /* Put it left justified on the screen -- it ought to fit that way. */
13046 *root_x = 0;
3cf3436e
JR
13047}
13048
13049
71eab8d1 13050DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
74e1aeec
JR
13051 doc: /* Show STRING in a \"tooltip\" window on frame FRAME.
13052A tooltip window is a small window displaying a string.
13053
13054FRAME nil or omitted means use the selected frame.
13055
13056PARMS is an optional list of frame parameters which can be
13057used to change the tooltip's appearance.
13058
ca56d953
JR
13059Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
13060means use the default timeout of 5 seconds.
74e1aeec 13061
ca56d953 13062If the list of frame parameters PARAMS contains a `left' parameter,
74e1aeec
JR
13063the tooltip is displayed at that x-position. Otherwise it is
13064displayed at the mouse position, with offset DX added (default is 5 if
13065DX isn't specified). Likewise for the y-position; if a `top' frame
13066parameter is specified, it determines the y-position of the tooltip
13067window, otherwise it is displayed at the mouse position, with offset
13068DY added (default is -10).
13069
13070A tooltip's maximum size is specified by `x-max-tooltip-size'.
13071Text larger than the specified size is clipped. */)
71eab8d1
AI
13072 (string, frame, parms, timeout, dx, dy)
13073 Lisp_Object string, frame, parms, timeout, dx, dy;
ee78dc32 13074{
6fc2811b
JR
13075 struct frame *f;
13076 struct window *w;
3cf3436e 13077 int root_x, root_y;
6fc2811b
JR
13078 struct buffer *old_buffer;
13079 struct text_pos pos;
13080 int i, width, height;
6fc2811b
JR
13081 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
13082 int old_windows_or_buffers_changed = windows_or_buffers_changed;
331379bf 13083 int count = SPECPDL_INDEX ();
7d0393cf 13084
6fc2811b 13085 specbind (Qinhibit_redisplay, Qt);
ee78dc32 13086
dfff8a69 13087 GCPRO4 (string, parms, frame, timeout);
ee78dc32 13088
b7826503 13089 CHECK_STRING (string);
6fc2811b
JR
13090 f = check_x_frame (frame);
13091 if (NILP (timeout))
13092 timeout = make_number (5);
13093 else
b7826503 13094 CHECK_NATNUM (timeout);
ee78dc32 13095
71eab8d1
AI
13096 if (NILP (dx))
13097 dx = make_number (5);
13098 else
b7826503 13099 CHECK_NUMBER (dx);
7d0393cf 13100
71eab8d1 13101 if (NILP (dy))
dc220243 13102 dy = make_number (-10);
71eab8d1 13103 else
b7826503 13104 CHECK_NUMBER (dy);
71eab8d1 13105
dc220243
JR
13106 if (NILP (last_show_tip_args))
13107 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
13108
13109 if (!NILP (tip_frame))
13110 {
13111 Lisp_Object last_string = AREF (last_show_tip_args, 0);
13112 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
13113 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
13114
13115 if (EQ (frame, last_frame)
13116 && !NILP (Fequal (last_string, string))
13117 && !NILP (Fequal (last_parms, parms)))
13118 {
13119 struct frame *f = XFRAME (tip_frame);
7d0393cf 13120
dc220243
JR
13121 /* Only DX and DY have changed. */
13122 if (!NILP (tip_timer))
13123 {
13124 Lisp_Object timer = tip_timer;
13125 tip_timer = Qnil;
13126 call1 (Qcancel_timer, timer);
13127 }
13128
13129 BLOCK_INPUT;
be786000
KS
13130 compute_tip_xy (f, parms, dx, dy, FRAME_PIXEL_WIDTH (f),
13131 FRAME_PIXEL_HEIGHT (f), &root_x, &root_y);
d65a9cdc
JR
13132
13133 /* Put tooltip in topmost group and in position. */
ca56d953
JR
13134 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
13135 root_x, root_y, 0, 0,
13136 SWP_NOSIZE | SWP_NOACTIVATE);
d65a9cdc
JR
13137
13138 /* Ensure tooltip is on top of other topmost windows (eg menus). */
13139 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
13140 0, 0, 0, 0,
13141 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
13142
dc220243
JR
13143 UNBLOCK_INPUT;
13144 goto start_timer;
13145 }
13146 }
13147
6fc2811b
JR
13148 /* Hide a previous tip, if any. */
13149 Fx_hide_tip ();
ee78dc32 13150
dc220243
JR
13151 ASET (last_show_tip_args, 0, string);
13152 ASET (last_show_tip_args, 1, frame);
13153 ASET (last_show_tip_args, 2, parms);
13154
6fc2811b
JR
13155 /* Add default values to frame parameters. */
13156 if (NILP (Fassq (Qname, parms)))
13157 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
13158 if (NILP (Fassq (Qinternal_border_width, parms)))
13159 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
13160 if (NILP (Fassq (Qborder_width, parms)))
13161 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
13162 if (NILP (Fassq (Qborder_color, parms)))
13163 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
13164 if (NILP (Fassq (Qbackground_color, parms)))
13165 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
13166 parms);
13167
0e3fcdef
JR
13168 /* Block input until the tip has been fully drawn, to avoid crashes
13169 when drawing tips in menus. */
13170 BLOCK_INPUT;
13171
6fc2811b
JR
13172 /* Create a frame for the tooltip, and record it in the global
13173 variable tip_frame. */
ca56d953 13174 frame = x_create_tip_frame (FRAME_W32_DISPLAY_INFO (f), parms, string);
937e601e 13175 f = XFRAME (frame);
6fc2811b 13176
3cf3436e 13177 /* Set up the frame's root window. */
6fc2811b 13178 w = XWINDOW (FRAME_ROOT_WINDOW (f));
be786000 13179 w->left_col = w->top_line = make_number (0);
3cf3436e
JR
13180
13181 if (CONSP (Vx_max_tooltip_size)
13182 && INTEGERP (XCAR (Vx_max_tooltip_size))
13183 && XINT (XCAR (Vx_max_tooltip_size)) > 0
13184 && INTEGERP (XCDR (Vx_max_tooltip_size))
13185 && XINT (XCDR (Vx_max_tooltip_size)) > 0)
13186 {
be786000
KS
13187 w->total_cols = XCAR (Vx_max_tooltip_size);
13188 w->total_lines = XCDR (Vx_max_tooltip_size);
3cf3436e
JR
13189 }
13190 else
13191 {
be786000
KS
13192 w->total_cols = make_number (80);
13193 w->total_lines = make_number (40);
3cf3436e 13194 }
7d0393cf 13195
be786000 13196 FRAME_TOTAL_COLS (f) = XINT (w->total_cols);
6fc2811b
JR
13197 adjust_glyphs (f);
13198 w->pseudo_window_p = 1;
13199
13200 /* Display the tooltip text in a temporary buffer. */
6fc2811b 13201 old_buffer = current_buffer;
3cf3436e
JR
13202 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->buffer));
13203 current_buffer->truncate_lines = Qnil;
6fc2811b
JR
13204 clear_glyph_matrix (w->desired_matrix);
13205 clear_glyph_matrix (w->current_matrix);
13206 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
13207 try_window (FRAME_ROOT_WINDOW (f), pos);
13208
13209 /* Compute width and height of the tooltip. */
13210 width = height = 0;
13211 for (i = 0; i < w->desired_matrix->nrows; ++i)
ee78dc32 13212 {
6fc2811b
JR
13213 struct glyph_row *row = &w->desired_matrix->rows[i];
13214 struct glyph *last;
13215 int row_width;
13216
13217 /* Stop at the first empty row at the end. */
13218 if (!row->enabled_p || !row->displays_text_p)
13219 break;
13220
13221 /* Let the row go over the full width of the frame. */
13222 row->full_width_p = 1;
13223
4e3a1c61
JR
13224#ifdef TODO /* Investigate why some fonts need more width than is
13225 calculated for some tooltips. */
6fc2811b
JR
13226 /* There's a glyph at the end of rows that is use to place
13227 the cursor there. Don't include the width of this glyph. */
13228 if (row->used[TEXT_AREA])
13229 {
13230 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
13231 row_width = row->pixel_width - last->pixel_width;
13232 }
13233 else
4e3a1c61 13234#endif
6fc2811b 13235 row_width = row->pixel_width;
7d0393cf 13236
ca56d953 13237 /* TODO: find why tips do not draw along baseline as instructed. */
bfd6edcc 13238 height += row->height;
6fc2811b 13239 width = max (width, row_width);
ee78dc32
GV
13240 }
13241
6fc2811b
JR
13242 /* Add the frame's internal border to the width and height the X
13243 window should have. */
13244 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
13245 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
ee78dc32 13246
6fc2811b
JR
13247 /* Move the tooltip window where the mouse pointer is. Resize and
13248 show it. */
3cf3436e 13249 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
71eab8d1 13250
bfd6edcc
JR
13251 {
13252 /* Adjust Window size to take border into account. */
13253 RECT rect;
13254 rect.left = rect.top = 0;
13255 rect.right = width;
13256 rect.bottom = height;
13257 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
13258 FRAME_EXTERNAL_MENU_BAR (f));
13259
d65a9cdc 13260 /* Position and size tooltip, and put it in the topmost group. */
bfd6edcc
JR
13261 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOPMOST,
13262 root_x, root_y, rect.right - rect.left,
13263 rect.bottom - rect.top, SWP_NOACTIVATE);
13264
d65a9cdc
JR
13265 /* Ensure tooltip is on top of other topmost windows (eg menus). */
13266 SetWindowPos (FRAME_W32_WINDOW (f), HWND_TOP,
13267 0, 0, 0, 0,
13268 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
13269
bfd6edcc
JR
13270 /* Let redisplay know that we have made the frame visible already. */
13271 f->async_visible = 1;
13272
13273 ShowWindow (FRAME_W32_WINDOW (f), SW_SHOWNOACTIVATE);
13274 }
ee78dc32 13275
6fc2811b
JR
13276 /* Draw into the window. */
13277 w->must_be_updated_p = 1;
13278 update_single_window (w, 1);
ee78dc32 13279
0e3fcdef
JR
13280 UNBLOCK_INPUT;
13281
6fc2811b
JR
13282 /* Restore original current buffer. */
13283 set_buffer_internal_1 (old_buffer);
13284 windows_or_buffers_changed = old_windows_or_buffers_changed;
ee78dc32 13285
dc220243 13286 start_timer:
6fc2811b
JR
13287 /* Let the tip disappear after timeout seconds. */
13288 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
13289 intern ("x-hide-tip"));
ee78dc32 13290
dfff8a69 13291 UNGCPRO;
6fc2811b 13292 return unbind_to (count, Qnil);
ee78dc32
GV
13293}
13294
ee78dc32 13295
6fc2811b 13296DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
74e1aeec
JR
13297 doc: /* Hide the current tooltip window, if there is any.
13298Value is t if tooltip was open, nil otherwise. */)
6fc2811b
JR
13299 ()
13300{
937e601e
AI
13301 int count;
13302 Lisp_Object deleted, frame, timer;
13303 struct gcpro gcpro1, gcpro2;
13304
13305 /* Return quickly if nothing to do. */
13306 if (NILP (tip_timer) && NILP (tip_frame))
13307 return Qnil;
7d0393cf 13308
937e601e
AI
13309 frame = tip_frame;
13310 timer = tip_timer;
13311 GCPRO2 (frame, timer);
13312 tip_frame = tip_timer = deleted = Qnil;
7d0393cf 13313
331379bf 13314 count = SPECPDL_INDEX ();
6fc2811b 13315 specbind (Qinhibit_redisplay, Qt);
937e601e 13316 specbind (Qinhibit_quit, Qt);
7d0393cf 13317
937e601e 13318 if (!NILP (timer))
dc220243 13319 call1 (Qcancel_timer, timer);
ee78dc32 13320
937e601e 13321 if (FRAMEP (frame))
6fc2811b 13322 {
937e601e
AI
13323 Fdelete_frame (frame, Qnil);
13324 deleted = Qt;
6fc2811b 13325 }
1edf84e7 13326
937e601e
AI
13327 UNGCPRO;
13328 return unbind_to (count, deleted);
6fc2811b 13329}
5ac45f98 13330
5ac45f98 13331
6fc2811b
JR
13332\f
13333/***********************************************************************
13334 File selection dialog
13335 ***********************************************************************/
6fc2811b
JR
13336extern Lisp_Object Qfile_name_history;
13337
1030b26b
JR
13338/* Callback for altering the behaviour of the Open File dialog.
13339 Makes the Filename text field contain "Current Directory" and be
13340 read-only when "Directories" is selected in the filter. This
13341 allows us to work around the fact that the standard Open File
13342 dialog does not support directories. */
13343UINT CALLBACK
13344file_dialog_callback (hwnd, msg, wParam, lParam)
13345 HWND hwnd;
13346 UINT msg;
13347 WPARAM wParam;
13348 LPARAM lParam;
13349{
13350 if (msg == WM_NOTIFY)
13351 {
13352 OFNOTIFY * notify = (OFNOTIFY *)lParam;
13353 /* Detect when the Filter dropdown is changed. */
13354 if (notify->hdr.code == CDN_TYPECHANGE)
13355 {
13356 HWND dialog = GetParent (hwnd);
13357 HWND edit_control = GetDlgItem (dialog, FILE_NAME_TEXT_FIELD);
13358
13359 /* Directories is in index 2. */
13360 if (notify->lpOFN->nFilterIndex == 2)
13361 {
13362 CommDlg_OpenSave_SetControlText (dialog, FILE_NAME_TEXT_FIELD,
13363 "Current Directory");
13364 EnableWindow (edit_control, FALSE);
13365 }
13366 else
13367 {
13368 CommDlg_OpenSave_SetControlText (dialog, FILE_NAME_TEXT_FIELD,
13369 "");
13370 EnableWindow (edit_control, TRUE);
13371 }
13372 }
13373 }
13374 return 0;
13375}
13376
6fc2811b 13377DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 4, 0,
74e1aeec
JR
13378 doc: /* Read file name, prompting with PROMPT in directory DIR.
13379Use a file selection dialog.
13380Select DEFAULT-FILENAME in the dialog's file selection box, if
13381specified. Ensure that file exists if MUSTMATCH is non-nil. */)
6fc2811b
JR
13382 (prompt, dir, default_filename, mustmatch)
13383 Lisp_Object prompt, dir, default_filename, mustmatch;
13384{
13385 struct frame *f = SELECTED_FRAME ();
13386 Lisp_Object file = Qnil;
aed13378 13387 int count = SPECPDL_INDEX ();
6fc2811b
JR
13388 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
13389 char filename[MAX_PATH + 1];
13390 char init_dir[MAX_PATH + 1];
6fc2811b
JR
13391
13392 GCPRO5 (prompt, dir, default_filename, mustmatch, file);
b7826503
PJ
13393 CHECK_STRING (prompt);
13394 CHECK_STRING (dir);
6fc2811b
JR
13395
13396 /* Create the dialog with PROMPT as title, using DIR as initial
13397 directory and using "*" as pattern. */
13398 dir = Fexpand_file_name (dir, Qnil);
d5db4077 13399 strncpy (init_dir, SDATA (dir), MAX_PATH);
6fc2811b
JR
13400 init_dir[MAX_PATH] = '\0';
13401 unixtodos_filename (init_dir);
13402
13403 if (STRINGP (default_filename))
13404 {
13405 char *file_name_only;
d5db4077 13406 char *full_path_name = SDATA (default_filename);
5ac45f98 13407
6fc2811b 13408 unixtodos_filename (full_path_name);
5ac45f98 13409
6fc2811b
JR
13410 file_name_only = strrchr (full_path_name, '\\');
13411 if (!file_name_only)
13412 file_name_only = full_path_name;
13413 else
13414 {
13415 file_name_only++;
6fc2811b 13416 }
ee78dc32 13417
6fc2811b
JR
13418 strncpy (filename, file_name_only, MAX_PATH);
13419 filename[MAX_PATH] = '\0';
13420 }
ee78dc32 13421 else
6fc2811b 13422 filename[0] = '\0';
ee78dc32 13423
1030b26b
JR
13424 {
13425 OPENFILENAME file_details;
5ac45f98 13426
1030b26b
JR
13427 /* Prevent redisplay. */
13428 specbind (Qinhibit_redisplay, Qt);
13429 BLOCK_INPUT;
ee78dc32 13430
1030b26b
JR
13431 bzero (&file_details, sizeof (file_details));
13432 file_details.lStructSize = sizeof (file_details);
13433 file_details.hwndOwner = FRAME_W32_WINDOW (f);
13434 /* Undocumented Bug in Common File Dialog:
13435 If a filter is not specified, shell links are not resolved. */
13436 file_details.lpstrFilter = "All Files (*.*)\0*.*\0Directories\0*|*\0\0";
13437 file_details.lpstrFile = filename;
13438 file_details.nMaxFile = sizeof (filename);
13439 file_details.lpstrInitialDir = init_dir;
d5db4077 13440 file_details.lpstrTitle = SDATA (prompt);
1030b26b
JR
13441 file_details.Flags = (OFN_HIDEREADONLY | OFN_NOCHANGEDIR
13442 | OFN_EXPLORER | OFN_ENABLEHOOK);
13443 if (!NILP (mustmatch))
13444 file_details.Flags |= OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
13445
13446 file_details.lpfnHook = (LPOFNHOOKPROC) file_dialog_callback;
13447
13448 if (GetOpenFileName (&file_details))
13449 {
13450 dostounix_filename (filename);
13451 if (file_details.nFilterIndex == 2)
13452 {
13453 /* "Folder Only" selected - strip dummy file name. */
13454 char * last = strrchr (filename, '/');
13455 *last = '\0';
13456 }
6fc2811b 13457
1030b26b
JR
13458 file = DECODE_FILE(build_string (filename));
13459 }
13460 /* User cancelled the dialog without making a selection. */
13461 else if (!CommDlgExtendedError ())
13462 file = Qnil;
13463 /* An error occurred, fallback on reading from the mini-buffer. */
13464 else
13465 file = Fcompleting_read (prompt, intern ("read-file-name-internal"),
13466 dir, mustmatch, dir, Qfile_name_history,
13467 default_filename, Qnil);
13468
13469 UNBLOCK_INPUT;
13470 file = unbind_to (count, file);
13471 }
ee78dc32 13472
6fc2811b 13473 UNGCPRO;
1edf84e7 13474
6fc2811b
JR
13475 /* Make "Cancel" equivalent to C-g. */
13476 if (NILP (file))
13477 Fsignal (Qquit, Qnil);
ee78dc32 13478
dfff8a69 13479 return unbind_to (count, file);
6fc2811b 13480}
ee78dc32 13481
ee78dc32 13482
6fc2811b 13483\f
6fc2811b
JR
13484/***********************************************************************
13485 w32 specialized functions
13486 ***********************************************************************/
ee78dc32 13487
d84b082d 13488DEFUN ("w32-select-font", Fw32_select_font, Sw32_select_font, 0, 2, 0,
74e1aeec
JR
13489 doc: /* Select a font using the W32 font dialog.
13490Returns an X font string corresponding to the selection. */)
d84b082d
JR
13491 (frame, include_proportional)
13492 Lisp_Object frame, include_proportional;
ee78dc32
GV
13493{
13494 FRAME_PTR f = check_x_frame (frame);
13495 CHOOSEFONT cf;
13496 LOGFONT lf;
f46e6225
GV
13497 TEXTMETRIC tm;
13498 HDC hdc;
13499 HANDLE oldobj;
ee78dc32
GV
13500 char buf[100];
13501
13502 bzero (&cf, sizeof (cf));
f46e6225 13503 bzero (&lf, sizeof (lf));
ee78dc32
GV
13504
13505 cf.lStructSize = sizeof (cf);
fbd6baed 13506 cf.hwndOwner = FRAME_W32_WINDOW (f);
d84b082d
JR
13507 cf.Flags = CF_FORCEFONTEXIST | CF_SCREENFONTS | CF_NOVERTFONTS;
13508
13509 /* Unless include_proportional is non-nil, limit the selection to
13510 monospaced fonts. */
13511 if (NILP (include_proportional))
13512 cf.Flags |= CF_FIXEDPITCHONLY;
13513
ee78dc32
GV
13514 cf.lpLogFont = &lf;
13515
f46e6225
GV
13516 /* Initialize as much of the font details as we can from the current
13517 default font. */
13518 hdc = GetDC (FRAME_W32_WINDOW (f));
13519 oldobj = SelectObject (hdc, FRAME_FONT (f)->hfont);
13520 GetTextFace (hdc, LF_FACESIZE, lf.lfFaceName);
13521 if (GetTextMetrics (hdc, &tm))
13522 {
13523 lf.lfHeight = tm.tmInternalLeading - tm.tmHeight;
13524 lf.lfWeight = tm.tmWeight;
13525 lf.lfItalic = tm.tmItalic;
13526 lf.lfUnderline = tm.tmUnderlined;
13527 lf.lfStrikeOut = tm.tmStruckOut;
f46e6225
GV
13528 lf.lfCharSet = tm.tmCharSet;
13529 cf.Flags |= CF_INITTOLOGFONTSTRUCT;
13530 }
13531 SelectObject (hdc, oldobj);
6fc2811b 13532 ReleaseDC (FRAME_W32_WINDOW (f), hdc);
f46e6225 13533
767b1ff0 13534 if (!ChooseFont (&cf) || !w32_to_x_font (&lf, buf, 100, NULL))
3c190163 13535 return Qnil;
ee78dc32
GV
13536
13537 return build_string (buf);
13538}
13539
74e1aeec
JR
13540DEFUN ("w32-send-sys-command", Fw32_send_sys_command,
13541 Sw32_send_sys_command, 1, 2, 0,
13542 doc: /* Send frame a Windows WM_SYSCOMMAND message of type COMMAND.
d84b082d
JR
13543Some useful values for command are #xf030 to maximise frame (#xf020
13544to minimize), #xf120 to restore frame to original size, and #xf100
13545to activate the menubar for keyboard access. #xf140 activates the
74e1aeec
JR
13546screen saver if defined.
13547
13548If optional parameter FRAME is not specified, use selected frame. */)
1edf84e7
GV
13549 (command, frame)
13550 Lisp_Object command, frame;
13551{
1edf84e7
GV
13552 FRAME_PTR f = check_x_frame (frame);
13553
b7826503 13554 CHECK_NUMBER (command);
1edf84e7 13555
ce6059da 13556 PostMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, XINT (command), 0);
1edf84e7
GV
13557
13558 return Qnil;
13559}
13560
55dcfc15 13561DEFUN ("w32-shell-execute", Fw32_shell_execute, Sw32_shell_execute, 2, 4, 0,
74e1aeec
JR
13562 doc: /* Get Windows to perform OPERATION on DOCUMENT.
13563This is a wrapper around the ShellExecute system function, which
13564invokes the application registered to handle OPERATION for DOCUMENT.
13565OPERATION is typically \"open\", \"print\" or \"explore\" (but can be
13566nil for the default action), and DOCUMENT is typically the name of a
13567document file or URL, but can also be a program executable to run or
13568a directory to open in the Windows Explorer.
13569
13570If DOCUMENT is a program executable, PARAMETERS can be a string
13571containing command line parameters, but otherwise should be nil.
13572
13573SHOW-FLAG can be used to control whether the invoked application is hidden
13574or minimized. If SHOW-FLAG is nil, the application is displayed normally,
13575otherwise it is an integer representing a ShowWindow flag:
13576
13577 0 - start hidden
13578 1 - start normally
13579 3 - start maximized
13580 6 - start minimized */)
55dcfc15
AI
13581 (operation, document, parameters, show_flag)
13582 Lisp_Object operation, document, parameters, show_flag;
13583{
13584 Lisp_Object current_dir;
13585
b7826503 13586 CHECK_STRING (document);
55dcfc15
AI
13587
13588 /* Encode filename and current directory. */
13589 current_dir = ENCODE_FILE (current_buffer->directory);
13590 document = ENCODE_FILE (document);
13591 if ((int) ShellExecute (NULL,
6fc2811b 13592 (STRINGP (operation) ?
d5db4077
KR
13593 SDATA (operation) : NULL),
13594 SDATA (document),
55dcfc15 13595 (STRINGP (parameters) ?
d5db4077
KR
13596 SDATA (parameters) : NULL),
13597 SDATA (current_dir),
55dcfc15
AI
13598 (INTEGERP (show_flag) ?
13599 XINT (show_flag) : SW_SHOWDEFAULT))
13600 > 32)
13601 return Qt;
90d97e64 13602 error ("ShellExecute failed: %s", w32_strerror (0));
55dcfc15
AI
13603}
13604
ccc2d29c
GV
13605/* Lookup virtual keycode from string representing the name of a
13606 non-ascii keystroke into the corresponding virtual key, using
13607 lispy_function_keys. */
13608static int
13609lookup_vk_code (char *key)
13610{
13611 int i;
13612
13613 for (i = 0; i < 256; i++)
13614 if (lispy_function_keys[i] != 0
13615 && strcmp (lispy_function_keys[i], key) == 0)
13616 return i;
13617
13618 return -1;
13619}
13620
13621/* Convert a one-element vector style key sequence to a hot key
13622 definition. */
13623static int
13624w32_parse_hot_key (key)
13625 Lisp_Object key;
13626{
13627 /* Copied from Fdefine_key and store_in_keymap. */
13628 register Lisp_Object c;
13629 int vk_code;
13630 int lisp_modifiers;
13631 int w32_modifiers;
13632 struct gcpro gcpro1;
13633
b7826503 13634 CHECK_VECTOR (key);
ccc2d29c
GV
13635
13636 if (XFASTINT (Flength (key)) != 1)
13637 return Qnil;
13638
13639 GCPRO1 (key);
13640
13641 c = Faref (key, make_number (0));
13642
13643 if (CONSP (c) && lucid_event_type_list_p (c))
13644 c = Fevent_convert_list (c);
13645
13646 UNGCPRO;
13647
13648 if (! INTEGERP (c) && ! SYMBOLP (c))
13649 error ("Key definition is invalid");
13650
13651 /* Work out the base key and the modifiers. */
13652 if (SYMBOLP (c))
13653 {
13654 c = parse_modifiers (c);
13655 lisp_modifiers = Fcar (Fcdr (c));
13656 c = Fcar (c);
13657 if (!SYMBOLP (c))
13658 abort ();
d5db4077 13659 vk_code = lookup_vk_code (SDATA (SYMBOL_NAME (c)));
ccc2d29c
GV
13660 }
13661 else if (INTEGERP (c))
13662 {
13663 lisp_modifiers = XINT (c) & ~CHARACTERBITS;
13664 /* Many ascii characters are their own virtual key code. */
13665 vk_code = XINT (c) & CHARACTERBITS;
13666 }
13667
13668 if (vk_code < 0 || vk_code > 255)
13669 return Qnil;
13670
13671 if ((lisp_modifiers & meta_modifier) != 0
13672 && !NILP (Vw32_alt_is_meta))
13673 lisp_modifiers |= alt_modifier;
13674
71eab8d1
AI
13675 /* Supply defs missing from mingw32. */
13676#ifndef MOD_ALT
13677#define MOD_ALT 0x0001
13678#define MOD_CONTROL 0x0002
13679#define MOD_SHIFT 0x0004
13680#define MOD_WIN 0x0008
13681#endif
13682
ccc2d29c
GV
13683 /* Convert lisp modifiers to Windows hot-key form. */
13684 w32_modifiers = (lisp_modifiers & hyper_modifier) ? MOD_WIN : 0;
13685 w32_modifiers |= (lisp_modifiers & alt_modifier) ? MOD_ALT : 0;
13686 w32_modifiers |= (lisp_modifiers & ctrl_modifier) ? MOD_CONTROL : 0;
13687 w32_modifiers |= (lisp_modifiers & shift_modifier) ? MOD_SHIFT : 0;
13688
13689 return HOTKEY (vk_code, w32_modifiers);
13690}
13691
74e1aeec
JR
13692DEFUN ("w32-register-hot-key", Fw32_register_hot_key,
13693 Sw32_register_hot_key, 1, 1, 0,
13694 doc: /* Register KEY as a hot-key combination.
13695Certain key combinations like Alt-Tab are reserved for system use on
13696Windows, and therefore are normally intercepted by the system. However,
13697most of these key combinations can be received by registering them as
13698hot-keys, overriding their special meaning.
13699
13700KEY must be a one element key definition in vector form that would be
13701acceptable to `define-key' (e.g. [A-tab] for Alt-Tab). The meta
13702modifier is interpreted as Alt if `w32-alt-is-meta' is t, and hyper
13703is always interpreted as the Windows modifier keys.
13704
13705The return value is the hotkey-id if registered, otherwise nil. */)
ccc2d29c
GV
13706 (key)
13707 Lisp_Object key;
13708{
13709 key = w32_parse_hot_key (key);
13710
13711 if (NILP (Fmemq (key, w32_grabbed_keys)))
13712 {
13713 /* Reuse an empty slot if possible. */
13714 Lisp_Object item = Fmemq (Qnil, w32_grabbed_keys);
13715
13716 /* Safe to add new key to list, even if we have focus. */
13717 if (NILP (item))
13718 w32_grabbed_keys = Fcons (key, w32_grabbed_keys);
13719 else
f3fbd155 13720 XSETCAR (item, key);
ccc2d29c
GV
13721
13722 /* Notify input thread about new hot-key definition, so that it
13723 takes effect without needing to switch focus. */
13724 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
13725 (WPARAM) key, 0);
13726 }
13727
13728 return key;
13729}
13730
74e1aeec
JR
13731DEFUN ("w32-unregister-hot-key", Fw32_unregister_hot_key,
13732 Sw32_unregister_hot_key, 1, 1, 0,
13733 doc: /* Unregister HOTKEY as a hot-key combination. */)
ccc2d29c
GV
13734 (key)
13735 Lisp_Object key;
13736{
13737 Lisp_Object item;
13738
13739 if (!INTEGERP (key))
13740 key = w32_parse_hot_key (key);
13741
13742 item = Fmemq (key, w32_grabbed_keys);
13743
13744 if (!NILP (item))
13745 {
13746 /* Notify input thread about hot-key definition being removed, so
13747 that it takes effect without needing focus switch. */
13748 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
13749 (WPARAM) XINT (XCAR (item)), (LPARAM) item))
13750 {
13751 MSG msg;
13752 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
13753 }
13754 return Qt;
13755 }
13756 return Qnil;
13757}
13758
74e1aeec
JR
13759DEFUN ("w32-registered-hot-keys", Fw32_registered_hot_keys,
13760 Sw32_registered_hot_keys, 0, 0, 0,
13761 doc: /* Return list of registered hot-key IDs. */)
ccc2d29c
GV
13762 ()
13763{
13764 return Fcopy_sequence (w32_grabbed_keys);
13765}
13766
74e1aeec
JR
13767DEFUN ("w32-reconstruct-hot-key", Fw32_reconstruct_hot_key,
13768 Sw32_reconstruct_hot_key, 1, 1, 0,
13769 doc: /* Convert hot-key ID to a lisp key combination. */)
ccc2d29c
GV
13770 (hotkeyid)
13771 Lisp_Object hotkeyid;
13772{
13773 int vk_code, w32_modifiers;
13774 Lisp_Object key;
13775
b7826503 13776 CHECK_NUMBER (hotkeyid);
ccc2d29c
GV
13777
13778 vk_code = HOTKEY_VK_CODE (hotkeyid);
13779 w32_modifiers = HOTKEY_MODIFIERS (hotkeyid);
13780
13781 if (lispy_function_keys[vk_code])
13782 key = intern (lispy_function_keys[vk_code]);
13783 else
13784 key = make_number (vk_code);
13785
13786 key = Fcons (key, Qnil);
13787 if (w32_modifiers & MOD_SHIFT)
3ef68e6b 13788 key = Fcons (Qshift, key);
ccc2d29c 13789 if (w32_modifiers & MOD_CONTROL)
3ef68e6b 13790 key = Fcons (Qctrl, key);
ccc2d29c 13791 if (w32_modifiers & MOD_ALT)
3ef68e6b 13792 key = Fcons (NILP (Vw32_alt_is_meta) ? Qalt : Qmeta, key);
ccc2d29c 13793 if (w32_modifiers & MOD_WIN)
3ef68e6b 13794 key = Fcons (Qhyper, key);
ccc2d29c
GV
13795
13796 return key;
13797}
adcc3809 13798
74e1aeec
JR
13799DEFUN ("w32-toggle-lock-key", Fw32_toggle_lock_key,
13800 Sw32_toggle_lock_key, 1, 2, 0,
13801 doc: /* Toggle the state of the lock key KEY.
13802KEY can be `capslock', `kp-numlock', or `scroll'.
13803If the optional parameter NEW-STATE is a number, then the state of KEY
13804is set to off if the low bit of NEW-STATE is zero, otherwise on. */)
adcc3809
GV
13805 (key, new_state)
13806 Lisp_Object key, new_state;
13807{
13808 int vk_code;
adcc3809
GV
13809
13810 if (EQ (key, intern ("capslock")))
13811 vk_code = VK_CAPITAL;
13812 else if (EQ (key, intern ("kp-numlock")))
13813 vk_code = VK_NUMLOCK;
13814 else if (EQ (key, intern ("scroll")))
13815 vk_code = VK_SCROLL;
13816 else
13817 return Qnil;
13818
13819 if (!dwWindowsThreadId)
13820 return make_number (w32_console_toggle_lock_key (vk_code, new_state));
13821
13822 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
13823 (WPARAM) vk_code, (LPARAM) new_state))
13824 {
13825 MSG msg;
13826 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
13827 return make_number (msg.wParam);
13828 }
13829 return Qnil;
13830}
ee78dc32 13831\f
2254bcde 13832DEFUN ("file-system-info", Ffile_system_info, Sfile_system_info, 1, 1, 0,
74e1aeec
JR
13833 doc: /* Return storage information about the file system FILENAME is on.
13834Value is a list of floats (TOTAL FREE AVAIL), where TOTAL is the total
13835storage of the file system, FREE is the free storage, and AVAIL is the
13836storage available to a non-superuser. All 3 numbers are in bytes.
13837If the underlying system call fails, value is nil. */)
2254bcde
AI
13838 (filename)
13839 Lisp_Object filename;
13840{
13841 Lisp_Object encoded, value;
13842
b7826503 13843 CHECK_STRING (filename);
2254bcde
AI
13844 filename = Fexpand_file_name (filename, Qnil);
13845 encoded = ENCODE_FILE (filename);
13846
13847 value = Qnil;
13848
13849 /* Determining the required information on Windows turns out, sadly,
13850 to be more involved than one would hope. The original Win32 api
13851 call for this will return bogus information on some systems, but we
13852 must dynamically probe for the replacement api, since that was
13853 added rather late on. */
13854 {
13855 HMODULE hKernel = GetModuleHandle ("kernel32");
13856 BOOL (*pfn_GetDiskFreeSpaceEx)
13857 (char *, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER)
13858 = (void *) GetProcAddress (hKernel, "GetDiskFreeSpaceEx");
13859
13860 /* On Windows, we may need to specify the root directory of the
13861 volume holding FILENAME. */
13862 char rootname[MAX_PATH];
d5db4077 13863 char *name = SDATA (encoded);
2254bcde
AI
13864
13865 /* find the root name of the volume if given */
13866 if (isalpha (name[0]) && name[1] == ':')
13867 {
13868 rootname[0] = name[0];
13869 rootname[1] = name[1];
13870 rootname[2] = '\\';
13871 rootname[3] = 0;
13872 }
13873 else if (IS_DIRECTORY_SEP (name[0]) && IS_DIRECTORY_SEP (name[1]))
13874 {
13875 char *str = rootname;
13876 int slashes = 4;
13877 do
13878 {
13879 if (IS_DIRECTORY_SEP (*name) && --slashes == 0)
13880 break;
13881 *str++ = *name++;
13882 }
13883 while ( *name );
13884
13885 *str++ = '\\';
13886 *str = 0;
13887 }
13888
13889 if (pfn_GetDiskFreeSpaceEx)
13890 {
ac849ba4
JR
13891 /* Unsigned large integers cannot be cast to double, so
13892 use signed ones instead. */
2254bcde
AI
13893 LARGE_INTEGER availbytes;
13894 LARGE_INTEGER freebytes;
13895 LARGE_INTEGER totalbytes;
13896
13897 if (pfn_GetDiskFreeSpaceEx(rootname,
ac849ba4
JR
13898 (ULARGE_INTEGER *)&availbytes,
13899 (ULARGE_INTEGER *)&totalbytes,
13900 (ULARGE_INTEGER *)&freebytes))
2254bcde
AI
13901 value = list3 (make_float ((double) totalbytes.QuadPart),
13902 make_float ((double) freebytes.QuadPart),
13903 make_float ((double) availbytes.QuadPart));
13904 }
13905 else
13906 {
13907 DWORD sectors_per_cluster;
13908 DWORD bytes_per_sector;
13909 DWORD free_clusters;
13910 DWORD total_clusters;
13911
13912 if (GetDiskFreeSpace(rootname,
13913 &sectors_per_cluster,
13914 &bytes_per_sector,
13915 &free_clusters,
13916 &total_clusters))
13917 value = list3 (make_float ((double) total_clusters
13918 * sectors_per_cluster * bytes_per_sector),
13919 make_float ((double) free_clusters
13920 * sectors_per_cluster * bytes_per_sector),
13921 make_float ((double) free_clusters
13922 * sectors_per_cluster * bytes_per_sector));
13923 }
13924 }
13925
13926 return value;
13927}
13928\f
0e3fcdef
JR
13929/***********************************************************************
13930 Initialization
13931 ***********************************************************************/
13932
6d906347
KS
13933/* Keep this list in the same order as frame_parms in frame.c.
13934 Use 0 for unsupported frame parameters. */
13935
13936frame_parm_handler w32_frame_parm_handlers[] =
13937{
13938 x_set_autoraise,
13939 x_set_autolower,
13940 x_set_background_color,
13941 x_set_border_color,
13942 x_set_border_width,
13943 x_set_cursor_color,
13944 x_set_cursor_type,
13945 x_set_font,
13946 x_set_foreground_color,
13947 x_set_icon_name,
13948 x_set_icon_type,
13949 x_set_internal_border_width,
13950 x_set_menu_bar_lines,
13951 x_set_mouse_color,
13952 x_explicitly_set_name,
13953 x_set_scroll_bar_width,
13954 x_set_title,
13955 x_set_unsplittable,
13956 x_set_vertical_scroll_bars,
13957 x_set_visibility,
13958 x_set_tool_bar_lines,
13959 0, /* x_set_scroll_bar_foreground, */
13960 0, /* x_set_scroll_bar_background, */
13961 x_set_screen_gamma,
13962 x_set_line_spacing,
13963 x_set_fringe_width,
13964 x_set_fringe_width,
13965 0, /* x_set_wait_for_wm, */
13966 x_set_fullscreen,
13967};
13968
0e3fcdef 13969void
fbd6baed 13970syms_of_w32fns ()
ee78dc32 13971{
afc390dc
JR
13972 globals_of_w32fns ();
13973 /* This is zero if not using MS-Windows. */
1edf84e7 13974 w32_in_use = 0;
9eb16b62
JR
13975 track_mouse_window = NULL;
13976
d285988b
JR
13977 w32_visible_system_caret_hwnd = NULL;
13978
ee78dc32
GV
13979 Qnone = intern ("none");
13980 staticpro (&Qnone);
ee78dc32
GV
13981 Qsuppress_icon = intern ("suppress-icon");
13982 staticpro (&Qsuppress_icon);
ee78dc32
GV
13983 Qundefined_color = intern ("undefined-color");
13984 staticpro (&Qundefined_color);
dfff8a69
JR
13985 Qcenter = intern ("center");
13986 staticpro (&Qcenter);
dc220243
JR
13987 Qcancel_timer = intern ("cancel-timer");
13988 staticpro (&Qcancel_timer);
ee78dc32 13989
adcc3809
GV
13990 Qhyper = intern ("hyper");
13991 staticpro (&Qhyper);
13992 Qsuper = intern ("super");
13993 staticpro (&Qsuper);
13994 Qmeta = intern ("meta");
13995 staticpro (&Qmeta);
13996 Qalt = intern ("alt");
13997 staticpro (&Qalt);
13998 Qctrl = intern ("ctrl");
13999 staticpro (&Qctrl);
14000 Qcontrol = intern ("control");
14001 staticpro (&Qcontrol);
14002 Qshift = intern ("shift");
14003 staticpro (&Qshift);
f7b9d4d1 14004 /* This is the end of symbol initialization. */
adcc3809 14005
6fc2811b
JR
14006 /* Text property `display' should be nonsticky by default. */
14007 Vtext_property_default_nonsticky
14008 = Fcons (Fcons (Qdisplay, Qt), Vtext_property_default_nonsticky);
14009
14010
14011 Qlaplace = intern ("laplace");
14012 staticpro (&Qlaplace);
3cf3436e
JR
14013 Qemboss = intern ("emboss");
14014 staticpro (&Qemboss);
14015 Qedge_detection = intern ("edge-detection");
14016 staticpro (&Qedge_detection);
14017 Qheuristic = intern ("heuristic");
14018 staticpro (&Qheuristic);
14019 QCmatrix = intern (":matrix");
14020 staticpro (&QCmatrix);
14021 QCcolor_adjustment = intern (":color-adjustment");
14022 staticpro (&QCcolor_adjustment);
14023 QCmask = intern (":mask");
14024 staticpro (&QCmask);
6fc2811b 14025
ee78dc32
GV
14026 Fput (Qundefined_color, Qerror_conditions,
14027 Fcons (Qundefined_color, Fcons (Qerror, Qnil)));
14028 Fput (Qundefined_color, Qerror_message,
14029 build_string ("Undefined color"));
14030
ccc2d29c
GV
14031 staticpro (&w32_grabbed_keys);
14032 w32_grabbed_keys = Qnil;
14033
fbd6baed 14034 DEFVAR_LISP ("w32-color-map", &Vw32_color_map,
74e1aeec 14035 doc: /* An array of color name mappings for windows. */);
fbd6baed 14036 Vw32_color_map = Qnil;
ee78dc32 14037
fbd6baed 14038 DEFVAR_LISP ("w32-pass-alt-to-system", &Vw32_pass_alt_to_system,
74e1aeec
JR
14039 doc: /* Non-nil if alt key presses are passed on to Windows.
14040When non-nil, for example, alt pressed and released and then space will
14041open the System menu. When nil, Emacs silently swallows alt key events. */);
fbd6baed 14042 Vw32_pass_alt_to_system = Qnil;
da36a4d6 14043
fbd6baed 14044 DEFVAR_LISP ("w32-alt-is-meta", &Vw32_alt_is_meta,
74e1aeec
JR
14045 doc: /* Non-nil if the alt key is to be considered the same as the meta key.
14046When nil, Emacs will translate the alt key to the Alt modifier, and not Meta. */);
fbd6baed 14047 Vw32_alt_is_meta = Qt;
8c205c63 14048
7d081355 14049 DEFVAR_INT ("w32-quit-key", &Vw32_quit_key,
74e1aeec 14050 doc: /* If non-zero, the virtual key code for an alternative quit key. */);
7d081355
AI
14051 XSETINT (Vw32_quit_key, 0);
14052
7d0393cf 14053 DEFVAR_LISP ("w32-pass-lwindow-to-system",
ccc2d29c 14054 &Vw32_pass_lwindow_to_system,
74e1aeec
JR
14055 doc: /* Non-nil if the left \"Windows\" key is passed on to Windows.
14056When non-nil, the Start menu is opened by tapping the key. */);
ccc2d29c
GV
14057 Vw32_pass_lwindow_to_system = Qt;
14058
7d0393cf 14059 DEFVAR_LISP ("w32-pass-rwindow-to-system",
ccc2d29c 14060 &Vw32_pass_rwindow_to_system,
74e1aeec
JR
14061 doc: /* Non-nil if the right \"Windows\" key is passed on to Windows.
14062When non-nil, the Start menu is opened by tapping the key. */);
ccc2d29c
GV
14063 Vw32_pass_rwindow_to_system = Qt;
14064
adcc3809
GV
14065 DEFVAR_INT ("w32-phantom-key-code",
14066 &Vw32_phantom_key_code,
74e1aeec
JR
14067 doc: /* Virtual key code used to generate \"phantom\" key presses.
14068Value is a number between 0 and 255.
14069
14070Phantom key presses are generated in order to stop the system from
14071acting on \"Windows\" key events when `w32-pass-lwindow-to-system' or
14072`w32-pass-rwindow-to-system' is nil. */);
ce6059da
AI
14073 /* Although 255 is technically not a valid key code, it works and
14074 means that this hack won't interfere with any real key code. */
14075 Vw32_phantom_key_code = 255;
adcc3809 14076
7d0393cf 14077 DEFVAR_LISP ("w32-enable-num-lock",
ccc2d29c 14078 &Vw32_enable_num_lock,
74e1aeec
JR
14079 doc: /* Non-nil if Num Lock should act normally.
14080Set to nil to see Num Lock as the key `kp-numlock'. */);
ccc2d29c
GV
14081 Vw32_enable_num_lock = Qt;
14082
7d0393cf 14083 DEFVAR_LISP ("w32-enable-caps-lock",
ccc2d29c 14084 &Vw32_enable_caps_lock,
74e1aeec
JR
14085 doc: /* Non-nil if Caps Lock should act normally.
14086Set to nil to see Caps Lock as the key `capslock'. */);
ccc2d29c
GV
14087 Vw32_enable_caps_lock = Qt;
14088
14089 DEFVAR_LISP ("w32-scroll-lock-modifier",
14090 &Vw32_scroll_lock_modifier,
74e1aeec
JR
14091 doc: /* Modifier to use for the Scroll Lock on state.
14092The value can be hyper, super, meta, alt, control or shift for the
14093respective modifier, or nil to see Scroll Lock as the key `scroll'.
14094Any other value will cause the key to be ignored. */);
ccc2d29c
GV
14095 Vw32_scroll_lock_modifier = Qt;
14096
14097 DEFVAR_LISP ("w32-lwindow-modifier",
14098 &Vw32_lwindow_modifier,
74e1aeec
JR
14099 doc: /* Modifier to use for the left \"Windows\" key.
14100The value can be hyper, super, meta, alt, control or shift for the
14101respective modifier, or nil to appear as the key `lwindow'.
14102Any other value will cause the key to be ignored. */);
ccc2d29c
GV
14103 Vw32_lwindow_modifier = Qnil;
14104
14105 DEFVAR_LISP ("w32-rwindow-modifier",
14106 &Vw32_rwindow_modifier,
74e1aeec
JR
14107 doc: /* Modifier to use for the right \"Windows\" key.
14108The value can be hyper, super, meta, alt, control or shift for the
14109respective modifier, or nil to appear as the key `rwindow'.
14110Any other value will cause the key to be ignored. */);
ccc2d29c
GV
14111 Vw32_rwindow_modifier = Qnil;
14112
14113 DEFVAR_LISP ("w32-apps-modifier",
14114 &Vw32_apps_modifier,
74e1aeec
JR
14115 doc: /* Modifier to use for the \"Apps\" key.
14116The value can be hyper, super, meta, alt, control or shift for the
14117respective modifier, or nil to appear as the key `apps'.
14118Any other value will cause the key to be ignored. */);
ccc2d29c 14119 Vw32_apps_modifier = Qnil;
da36a4d6 14120
d84b082d 14121 DEFVAR_BOOL ("w32-enable-synthesized-fonts", &w32_enable_synthesized_fonts,
74e1aeec 14122 doc: /* Non-nil enables selection of artificially italicized and bold fonts. */);
d84b082d 14123 w32_enable_synthesized_fonts = 0;
5ac45f98 14124
fbd6baed 14125 DEFVAR_LISP ("w32-enable-palette", &Vw32_enable_palette,
74e1aeec 14126 doc: /* Non-nil enables Windows palette management to map colors exactly. */);
fbd6baed 14127 Vw32_enable_palette = Qt;
5ac45f98 14128
fbd6baed
GV
14129 DEFVAR_INT ("w32-mouse-button-tolerance",
14130 &Vw32_mouse_button_tolerance,
74e1aeec
JR
14131 doc: /* Analogue of double click interval for faking middle mouse events.
14132The value is the minimum time in milliseconds that must elapse between
14133left/right button down events before they are considered distinct events.
14134If both mouse buttons are depressed within this interval, a middle mouse
14135button down event is generated instead. */);
fbd6baed 14136 XSETINT (Vw32_mouse_button_tolerance, GetDoubleClickTime () / 2);
5ac45f98 14137
fbd6baed
GV
14138 DEFVAR_INT ("w32-mouse-move-interval",
14139 &Vw32_mouse_move_interval,
74e1aeec
JR
14140 doc: /* Minimum interval between mouse move events.
14141The value is the minimum time in milliseconds that must elapse between
14142successive mouse move (or scroll bar drag) events before they are
14143reported as lisp events. */);
247be837 14144 XSETINT (Vw32_mouse_move_interval, 0);
84fb1139 14145
74214547
JR
14146 DEFVAR_BOOL ("w32-pass-extra-mouse-buttons-to-system",
14147 &w32_pass_extra_mouse_buttons_to_system,
14148 doc: /* Non-nil if the fourth and fifth mouse buttons are passed to Windows.
14149Recent versions of Windows support mice with up to five buttons.
14150Since most applications don't support these extra buttons, most mouse
14151drivers will allow you to map them to functions at the system level.
14152If this variable is non-nil, Emacs will pass them on, allowing the
14153system to handle them. */);
14154 w32_pass_extra_mouse_buttons_to_system = 0;
14155
ee78dc32 14156 DEFVAR_LISP ("x-bitmap-file-path", &Vx_bitmap_file_path,
6e2d67d8 14157 doc: /* List of directories to search for window system bitmap files. */);
ee78dc32
GV
14158 Vx_bitmap_file_path = decode_env_path ((char *) 0, "PATH");
14159
14160 DEFVAR_LISP ("x-pointer-shape", &Vx_pointer_shape,
74e1aeec
JR
14161 doc: /* The shape of the pointer when over text.
14162Changing the value does not affect existing frames
14163unless you set the mouse color. */);
ee78dc32
GV
14164 Vx_pointer_shape = Qnil;
14165
ee78dc32
GV
14166 Vx_nontext_pointer_shape = Qnil;
14167
14168 Vx_mode_pointer_shape = Qnil;
14169
0af913d7 14170 DEFVAR_LISP ("x-hourglass-pointer-shape", &Vx_hourglass_pointer_shape,
74e1aeec
JR
14171 doc: /* The shape of the pointer when Emacs is busy.
14172This variable takes effect when you create a new frame
14173or when you set the mouse color. */);
0af913d7 14174 Vx_hourglass_pointer_shape = Qnil;
6fc2811b 14175
0af913d7 14176 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
74e1aeec 14177 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
0af913d7 14178 display_hourglass_p = 1;
7d0393cf 14179
0af913d7 14180 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
74e1aeec
JR
14181 doc: /* *Seconds to wait before displaying an hourglass pointer.
14182Value must be an integer or float. */);
0af913d7 14183 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
f79e6790 14184
6fc2811b 14185 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
ee78dc32 14186 &Vx_sensitive_text_pointer_shape,
74e1aeec
JR
14187 doc: /* The shape of the pointer when over mouse-sensitive text.
14188This variable takes effect when you create a new frame
14189or when you set the mouse color. */);
ee78dc32
GV
14190 Vx_sensitive_text_pointer_shape = Qnil;
14191
4694d762
JR
14192 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
14193 &Vx_window_horizontal_drag_shape,
74e1aeec
JR
14194 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
14195This variable takes effect when you create a new frame
14196or when you set the mouse color. */);
4694d762
JR
14197 Vx_window_horizontal_drag_shape = Qnil;
14198
ee78dc32 14199 DEFVAR_LISP ("x-cursor-fore-pixel", &Vx_cursor_fore_pixel,
74e1aeec 14200 doc: /* A string indicating the foreground color of the cursor box. */);
ee78dc32
GV
14201 Vx_cursor_fore_pixel = Qnil;
14202
3cf3436e 14203 DEFVAR_LISP ("x-max-tooltip-size", &Vx_max_tooltip_size,
b3700ae7
JR
14204 doc: /* Maximum size for tooltips.
14205Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
3cf3436e 14206 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
7d0393cf 14207
ee78dc32 14208 DEFVAR_LISP ("x-no-window-manager", &Vx_no_window_manager,
74e1aeec
JR
14209 doc: /* Non-nil if no window manager is in use.
14210Emacs doesn't try to figure this out; this is always nil
14211unless you set it to something else. */);
ee78dc32
GV
14212 /* We don't have any way to find this out, so set it to nil
14213 and maybe the user would like to set it to t. */
14214 Vx_no_window_manager = Qnil;
14215
4587b026
GV
14216 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
14217 &Vx_pixel_size_width_font_regexp,
74e1aeec
JR
14218 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
14219
14220Since Emacs gets width of a font matching with this regexp from
14221PIXEL_SIZE field of the name, font finding mechanism gets faster for
14222such a font. This is especially effective for such large fonts as
14223Chinese, Japanese, and Korean. */);
4587b026
GV
14224 Vx_pixel_size_width_font_regexp = Qnil;
14225
6fc2811b 14226 DEFVAR_LISP ("image-cache-eviction-delay", &Vimage_cache_eviction_delay,
74e1aeec
JR
14227 doc: /* Time after which cached images are removed from the cache.
14228When an image has not been displayed this many seconds, remove it
14229from the image cache. Value must be an integer or nil with nil
14230meaning don't clear the cache. */);
6fc2811b
JR
14231 Vimage_cache_eviction_delay = make_number (30 * 60);
14232
33d52f9c
GV
14233 DEFVAR_LISP ("w32-bdf-filename-alist",
14234 &Vw32_bdf_filename_alist,
74e1aeec 14235 doc: /* List of bdf fonts and their corresponding filenames. */);
33d52f9c
GV
14236 Vw32_bdf_filename_alist = Qnil;
14237
1075afa9
GV
14238 DEFVAR_BOOL ("w32-strict-fontnames",
14239 &w32_strict_fontnames,
74e1aeec
JR
14240 doc: /* Non-nil means only use fonts that are exact matches for those requested.
14241Default is nil, which allows old fontnames that are not XLFD compliant,
14242and allows third-party CJK display to work by specifying false charset
14243fields to trick Emacs into translating to Big5, SJIS etc.
14244Setting this to t will prevent wrong fonts being selected when
14245fontsets are automatically created. */);
1075afa9
GV
14246 w32_strict_fontnames = 0;
14247
c0611964
AI
14248 DEFVAR_BOOL ("w32-strict-painting",
14249 &w32_strict_painting,
74e1aeec
JR
14250 doc: /* Non-nil means use strict rules for repainting frames.
14251Set this to nil to get the old behaviour for repainting; this should
14252only be necessary if the default setting causes problems. */);
c0611964
AI
14253 w32_strict_painting = 1;
14254
dfff8a69
JR
14255 DEFVAR_LISP ("w32-charset-info-alist",
14256 &Vw32_charset_info_alist,
b3700ae7
JR
14257 doc: /* Alist linking Emacs character sets to Windows fonts and codepages.
14258Each entry should be of the form:
74e1aeec
JR
14259
14260 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE))
14261
14262where CHARSET_NAME is a string used in font names to identify the charset,
14263WINDOWS_CHARSET is a symbol that can be one of:
14264w32-charset-ansi, w32-charset-default, w32-charset-symbol,
14265w32-charset-shiftjis, w32-charset-hangeul, w32-charset-gb2312,
14266w32-charset-chinesebig5,
74e1aeec
JR
14267w32-charset-johab, w32-charset-hebrew,
14268w32-charset-arabic, w32-charset-greek, w32-charset-turkish,
14269w32-charset-vietnamese, w32-charset-thai, w32-charset-easteurope,
14270w32-charset-russian, w32-charset-mac, w32-charset-baltic,
74e1aeec 14271w32-charset-unicode,
74e1aeec
JR
14272or w32-charset-oem.
14273CODEPAGE should be an integer specifying the codepage that should be used
14274to display the character set, t to do no translation and output as Unicode,
14275or nil to do no translation and output as 8 bit (or multibyte on far-east
14276versions of Windows) characters. */);
dfff8a69
JR
14277 Vw32_charset_info_alist = Qnil;
14278
14279 staticpro (&Qw32_charset_ansi);
14280 Qw32_charset_ansi = intern ("w32-charset-ansi");
14281 staticpro (&Qw32_charset_symbol);
14282 Qw32_charset_symbol = intern ("w32-charset-symbol");
14283 staticpro (&Qw32_charset_shiftjis);
14284 Qw32_charset_shiftjis = intern ("w32-charset-shiftjis");
767b1ff0
JR
14285 staticpro (&Qw32_charset_hangeul);
14286 Qw32_charset_hangeul = intern ("w32-charset-hangeul");
dfff8a69
JR
14287 staticpro (&Qw32_charset_chinesebig5);
14288 Qw32_charset_chinesebig5 = intern ("w32-charset-chinesebig5");
14289 staticpro (&Qw32_charset_gb2312);
14290 Qw32_charset_gb2312 = intern ("w32-charset-gb2312");
14291 staticpro (&Qw32_charset_oem);
14292 Qw32_charset_oem = intern ("w32-charset-oem");
14293
14294#ifdef JOHAB_CHARSET
14295 {
14296 static int w32_extra_charsets_defined = 1;
74e1aeec
JR
14297 DEFVAR_BOOL ("w32-extra-charsets-defined", &w32_extra_charsets_defined,
14298 doc: /* Internal variable. */);
dfff8a69
JR
14299
14300 staticpro (&Qw32_charset_johab);
14301 Qw32_charset_johab = intern ("w32-charset-johab");
14302 staticpro (&Qw32_charset_easteurope);
14303 Qw32_charset_easteurope = intern ("w32-charset-easteurope");
14304 staticpro (&Qw32_charset_turkish);
14305 Qw32_charset_turkish = intern ("w32-charset-turkish");
14306 staticpro (&Qw32_charset_baltic);
14307 Qw32_charset_baltic = intern ("w32-charset-baltic");
14308 staticpro (&Qw32_charset_russian);
14309 Qw32_charset_russian = intern ("w32-charset-russian");
14310 staticpro (&Qw32_charset_arabic);
14311 Qw32_charset_arabic = intern ("w32-charset-arabic");
14312 staticpro (&Qw32_charset_greek);
14313 Qw32_charset_greek = intern ("w32-charset-greek");
14314 staticpro (&Qw32_charset_hebrew);
14315 Qw32_charset_hebrew = intern ("w32-charset-hebrew");
767b1ff0
JR
14316 staticpro (&Qw32_charset_vietnamese);
14317 Qw32_charset_vietnamese = intern ("w32-charset-vietnamese");
dfff8a69
JR
14318 staticpro (&Qw32_charset_thai);
14319 Qw32_charset_thai = intern ("w32-charset-thai");
14320 staticpro (&Qw32_charset_mac);
14321 Qw32_charset_mac = intern ("w32-charset-mac");
14322 }
14323#endif
14324
14325#ifdef UNICODE_CHARSET
14326 {
14327 static int w32_unicode_charset_defined = 1;
14328 DEFVAR_BOOL ("w32-unicode-charset-defined",
74e1aeec
JR
14329 &w32_unicode_charset_defined,
14330 doc: /* Internal variable. */);
dfff8a69
JR
14331
14332 staticpro (&Qw32_charset_unicode);
14333 Qw32_charset_unicode = intern ("w32-charset-unicode");
14334#endif
14335
767b1ff0 14336#if 0 /* TODO: Port to W32 */
6fc2811b
JR
14337 defsubr (&Sx_change_window_property);
14338 defsubr (&Sx_delete_window_property);
14339 defsubr (&Sx_window_property);
14340#endif
2d764c78 14341 defsubr (&Sxw_display_color_p);
ee78dc32 14342 defsubr (&Sx_display_grayscale_p);
2d764c78
EZ
14343 defsubr (&Sxw_color_defined_p);
14344 defsubr (&Sxw_color_values);
ee78dc32
GV
14345 defsubr (&Sx_server_max_request_size);
14346 defsubr (&Sx_server_vendor);
14347 defsubr (&Sx_server_version);
14348 defsubr (&Sx_display_pixel_width);
14349 defsubr (&Sx_display_pixel_height);
14350 defsubr (&Sx_display_mm_width);
14351 defsubr (&Sx_display_mm_height);
14352 defsubr (&Sx_display_screens);
14353 defsubr (&Sx_display_planes);
14354 defsubr (&Sx_display_color_cells);
14355 defsubr (&Sx_display_visual_class);
14356 defsubr (&Sx_display_backing_store);
14357 defsubr (&Sx_display_save_under);
ee78dc32 14358 defsubr (&Sx_create_frame);
ee78dc32
GV
14359 defsubr (&Sx_open_connection);
14360 defsubr (&Sx_close_connection);
14361 defsubr (&Sx_display_list);
14362 defsubr (&Sx_synchronize);
14363
fbd6baed 14364 /* W32 specific functions */
ee78dc32 14365
1edf84e7 14366 defsubr (&Sw32_focus_frame);
fbd6baed
GV
14367 defsubr (&Sw32_select_font);
14368 defsubr (&Sw32_define_rgb_color);
14369 defsubr (&Sw32_default_color_map);
14370 defsubr (&Sw32_load_color_file);
1edf84e7 14371 defsubr (&Sw32_send_sys_command);
55dcfc15 14372 defsubr (&Sw32_shell_execute);
ccc2d29c
GV
14373 defsubr (&Sw32_register_hot_key);
14374 defsubr (&Sw32_unregister_hot_key);
14375 defsubr (&Sw32_registered_hot_keys);
14376 defsubr (&Sw32_reconstruct_hot_key);
adcc3809 14377 defsubr (&Sw32_toggle_lock_key);
33d52f9c 14378 defsubr (&Sw32_find_bdf_fonts);
4587b026 14379
2254bcde
AI
14380 defsubr (&Sfile_system_info);
14381
4587b026
GV
14382 /* Setting callback functions for fontset handler. */
14383 get_font_info_func = w32_get_font_info;
6fc2811b
JR
14384
14385#if 0 /* This function pointer doesn't seem to be used anywhere.
14386 And the pointer assigned has the wrong type, anyway. */
4587b026 14387 list_fonts_func = w32_list_fonts;
6fc2811b
JR
14388#endif
14389
4587b026
GV
14390 load_font_func = w32_load_font;
14391 find_ccl_program_func = w32_find_ccl_program;
14392 query_font_func = w32_query_font;
14393 set_frame_fontset_func = x_set_font;
14394 check_window_system_func = check_w32;
6fc2811b 14395
6fc2811b
JR
14396 /* Images. */
14397 Qxbm = intern ("xbm");
14398 staticpro (&Qxbm);
a93f4566
GM
14399 QCconversion = intern (":conversion");
14400 staticpro (&QCconversion);
6fc2811b
JR
14401 QCheuristic_mask = intern (":heuristic-mask");
14402 staticpro (&QCheuristic_mask);
14403 QCcolor_symbols = intern (":color-symbols");
14404 staticpro (&QCcolor_symbols);
6fc2811b
JR
14405 QCascent = intern (":ascent");
14406 staticpro (&QCascent);
14407 QCmargin = intern (":margin");
14408 staticpro (&QCmargin);
14409 QCrelief = intern (":relief");
14410 staticpro (&QCrelief);
14411 Qpostscript = intern ("postscript");
14412 staticpro (&Qpostscript);
14413 QCloader = intern (":loader");
14414 staticpro (&QCloader);
14415 QCbounding_box = intern (":bounding-box");
14416 staticpro (&QCbounding_box);
14417 QCpt_width = intern (":pt-width");
14418 staticpro (&QCpt_width);
14419 QCpt_height = intern (":pt-height");
14420 staticpro (&QCpt_height);
14421 QCindex = intern (":index");
14422 staticpro (&QCindex);
14423 Qpbm = intern ("pbm");
14424 staticpro (&Qpbm);
14425
14426#if HAVE_XPM
14427 Qxpm = intern ("xpm");
14428 staticpro (&Qxpm);
14429#endif
7d0393cf 14430
6fc2811b
JR
14431#if HAVE_JPEG
14432 Qjpeg = intern ("jpeg");
14433 staticpro (&Qjpeg);
7d0393cf 14434#endif
6fc2811b
JR
14435
14436#if HAVE_TIFF
14437 Qtiff = intern ("tiff");
14438 staticpro (&Qtiff);
7d0393cf 14439#endif
6fc2811b
JR
14440
14441#if HAVE_GIF
14442 Qgif = intern ("gif");
14443 staticpro (&Qgif);
14444#endif
14445
14446#if HAVE_PNG
14447 Qpng = intern ("png");
14448 staticpro (&Qpng);
14449#endif
14450
14451 defsubr (&Sclear_image_cache);
ac849ba4
JR
14452 defsubr (&Simage_size);
14453 defsubr (&Simage_mask_p);
6fc2811b 14454
0af913d7
GM
14455 hourglass_atimer = NULL;
14456 hourglass_shown_p = 0;
6fc2811b
JR
14457 defsubr (&Sx_show_tip);
14458 defsubr (&Sx_hide_tip);
6fc2811b 14459 tip_timer = Qnil;
57fa2774
JR
14460 staticpro (&tip_timer);
14461 tip_frame = Qnil;
14462 staticpro (&tip_frame);
6fc2811b 14463
ca56d953
JR
14464 last_show_tip_args = Qnil;
14465 staticpro (&last_show_tip_args);
14466
6fc2811b
JR
14467 defsubr (&Sx_file_dialog);
14468}
14469
c922a224 14470
9785d95b
BK
14471/*
14472 globals_of_w32fns is used to initialize those global variables that
14473 must always be initialized on startup even when the global variable
14474 initialized is non zero (see the function main in emacs.c).
14475 globals_of_w32fns is called from syms_of_w32fns when the global
14476 variable initialized is 0 and directly from main when initialized
14477 is non zero.
14478 */
14479void globals_of_w32fns ()
14480{
14481 HMODULE user32_lib = GetModuleHandle ("user32.dll");
ccc0fdaa
JR
14482 /*
14483 TrackMouseEvent not available in all versions of Windows, so must load
14484 it dynamically. Do it once, here, instead of every time it is used.
9785d95b 14485 */
ccc0fdaa
JR
14486 track_mouse_event_fn = (TrackMouseEvent_Proc)
14487 GetProcAddress (user32_lib, "TrackMouseEvent");
14488 /* ditto for GetClipboardSequenceNumber. */
14489 clipboard_sequence_fn = (ClipboardSequence_Proc)
14490 GetProcAddress (user32_lib, "GetClipboardSequenceNumber");
9785d95b 14491}
6fc2811b 14492
839b1909
JR
14493/* Initialize image types. Based on which libraries are available. */
14494static void
14495init_external_image_libraries ()
6fc2811b 14496{
afc390dc 14497 HINSTANCE library;
7d0393cf 14498
6fc2811b 14499#if HAVE_XPM
c736ffda
JR
14500 if ((library = LoadLibrary ("libXpm.dll")))
14501 {
14502 if (init_xpm_functions (library))
14503 define_image_type (&xpm_type);
14504 }
14505
6fc2811b 14506#endif
7d0393cf 14507
6fc2811b 14508#if HAVE_JPEG
afc390dc 14509 /* Try loading jpeg library under probable names. */
c922a224 14510 if ((library = LoadLibrary ("libjpeg.dll"))
100dcd40 14511 || (library = LoadLibrary ("jpeg-62.dll"))
c922a224 14512 || (library = LoadLibrary ("jpeg.dll")))
afc390dc
JR
14513 {
14514 if (init_jpeg_functions (library))
14515 define_image_type (&jpeg_type);
14516 }
6fc2811b 14517#endif
7d0393cf 14518
6fc2811b 14519#if HAVE_TIFF
12b918b2
JB
14520 if (library = LoadLibrary ("libtiff.dll"))
14521 {
14522 if (init_tiff_functions (library))
14523 define_image_type (&tiff_type);
14524 }
6fc2811b 14525#endif
919f1e88 14526
6fc2811b 14527#if HAVE_GIF
1ffb278b
JB
14528 if (library = LoadLibrary ("libungif.dll"))
14529 {
14530 if (init_gif_functions (library))
14531 define_image_type (&gif_type);
14532 }
6fc2811b 14533#endif
7d0393cf 14534
6fc2811b 14535#if HAVE_PNG
839b1909
JR
14536 /* Ensure zlib is loaded. Try debug version first. */
14537 if (!LoadLibrary ("zlibd.dll"))
14538 LoadLibrary ("zlib.dll");
14539
14540 /* Try loading libpng under probable names. */
afc390dc
JR
14541 if ((library = LoadLibrary ("libpng13d.dll"))
14542 || (library = LoadLibrary ("libpng13.dll"))
14543 || (library = LoadLibrary ("libpng12d.dll"))
14544 || (library = LoadLibrary ("libpng12.dll"))
14545 || (library = LoadLibrary ("libpng.dll")))
839b1909 14546 {
afc390dc 14547 if (init_png_functions (library))
839b1909
JR
14548 define_image_type (&png_type);
14549 }
6fc2811b 14550#endif
ee78dc32
GV
14551}
14552
839b1909
JR
14553void
14554init_xfns ()
14555{
14556 image_types = NULL;
14557 Vimage_types = Qnil;
14558
14559 define_image_type (&pbm_type);
14560 define_image_type (&xbm_type);
14561
14562#if 0 /* TODO : Ghostscript support for W32 */
14563 define_image_type (&gs_type);
14564#endif
14565
14566 /* Image types that rely on external libraries are loaded dynamically
14567 if the library is available. */
14568 init_external_image_libraries ();
14569}
14570
ee78dc32
GV
14571#undef abort
14572
7d0393cf 14573void
fbd6baed 14574w32_abort()
ee78dc32 14575{
5ac45f98
GV
14576 int button;
14577 button = MessageBox (NULL,
14578 "A fatal error has occurred!\n\n"
14579 "Select Abort to exit, Retry to debug, Ignore to continue",
14580 "Emacs Abort Dialog",
14581 MB_ICONEXCLAMATION | MB_TASKMODAL
14582 | MB_SETFOREGROUND | MB_ABORTRETRYIGNORE);
14583 switch (button)
14584 {
14585 case IDRETRY:
14586 DebugBreak ();
14587 break;
14588 case IDIGNORE:
14589 break;
14590 case IDABORT:
14591 default:
14592 abort ();
14593 break;
14594 }
ee78dc32 14595}
d573caac 14596
83c75055
GV
14597/* For convenience when debugging. */
14598int
14599w32_last_error()
14600{
14601 return GetLastError ();
14602}
ab5796a9
MB
14603
14604/* arch-tag: 707589ab-b9be-4638-8cdd-74629cc9b446
14605 (do not change this comment) */