]> code.delx.au - gnu-emacs/blob - src/w32fns.c
(x_change_window_heights): New function.
[gnu-emacs] / src / w32fns.c
1 /* Graphical user interface functions for the Microsoft W32 API.
2 Copyright (C) 1989, 92, 93, 94, 95, 1996, 1997, 1998, 1999
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* Added by Kevin Gallo */
23
24 #include <config.h>
25
26 #include <signal.h>
27 #include <stdio.h>
28 #include <limits.h>
29 #include <errno.h>
30
31 #include "lisp.h"
32 #include "charset.h"
33 #include "dispextern.h"
34 #include "w32term.h"
35 #include "frame.h"
36 #include "window.h"
37 #include "buffer.h"
38 #include "fontset.h"
39 #include "intervals.h"
40 #include "keyboard.h"
41 #include "blockinput.h"
42 #include "epaths.h"
43 #include "w32heap.h"
44 #include "termhooks.h"
45 #include "coding.h"
46 #include "ccl.h"
47 #include "systime.h"
48
49 #include "bitmaps/gray.xbm"
50
51 #include <commdlg.h>
52 #include <shellapi.h>
53 #include <ctype.h>
54
55 #define max(a, b) ((a) > (b) ? (a) : (b))
56
57 extern void free_frame_menubar ();
58 extern double atof ();
59 extern int w32_console_toggle_lock_key (int vk_code, Lisp_Object new_state);
60 extern int quit_char;
61
62 /* A definition of XColor for non-X frames. */
63 #ifndef HAVE_X_WINDOWS
64 typedef struct {
65 unsigned long pixel;
66 unsigned short red, green, blue;
67 char flags;
68 char pad;
69 } XColor;
70 #endif
71
72 extern char *lispy_function_keys[];
73
74 /* The gray bitmap `bitmaps/gray'. This is done because w32term.c uses
75 it, and including `bitmaps/gray' more than once is a problem when
76 config.h defines `static' as an empty replacement string. */
77
78 int gray_bitmap_width = gray_width;
79 int gray_bitmap_height = gray_height;
80 unsigned char *gray_bitmap_bits = gray_bits;
81
82 /* The colormap for converting color names to RGB values */
83 Lisp_Object Vw32_color_map;
84
85 /* Non nil if alt key presses are passed on to Windows. */
86 Lisp_Object Vw32_pass_alt_to_system;
87
88 /* Non nil if alt key is translated to meta_modifier, nil if it is translated
89 to alt_modifier. */
90 Lisp_Object Vw32_alt_is_meta;
91
92 /* If non-zero, the windows virtual key code for an alternative quit key. */
93 Lisp_Object Vw32_quit_key;
94
95 /* Non nil if left window key events are passed on to Windows (this only
96 affects whether "tapping" the key opens the Start menu). */
97 Lisp_Object Vw32_pass_lwindow_to_system;
98
99 /* Non nil if right window key events are passed on to Windows (this
100 only affects whether "tapping" the key opens the Start menu). */
101 Lisp_Object Vw32_pass_rwindow_to_system;
102
103 /* Virtual key code used to generate "phantom" key presses in order
104 to stop system from acting on Windows key events. */
105 Lisp_Object Vw32_phantom_key_code;
106
107 /* Modifier associated with the left "Windows" key, or nil to act as a
108 normal key. */
109 Lisp_Object Vw32_lwindow_modifier;
110
111 /* Modifier associated with the right "Windows" key, or nil to act as a
112 normal key. */
113 Lisp_Object Vw32_rwindow_modifier;
114
115 /* Modifier associated with the "Apps" key, or nil to act as a normal
116 key. */
117 Lisp_Object Vw32_apps_modifier;
118
119 /* Value is nil if Num Lock acts as a function key. */
120 Lisp_Object Vw32_enable_num_lock;
121
122 /* Value is nil if Caps Lock acts as a function key. */
123 Lisp_Object Vw32_enable_caps_lock;
124
125 /* Modifier associated with Scroll Lock, or nil to act as a normal key. */
126 Lisp_Object Vw32_scroll_lock_modifier;
127
128 /* Switch to control whether we inhibit requests for synthesized bold
129 and italic versions of fonts. */
130 Lisp_Object Vw32_enable_synthesized_fonts;
131
132 /* Enable palette management. */
133 Lisp_Object Vw32_enable_palette;
134
135 /* Control how close left/right button down events must be to
136 be converted to a middle button down event. */
137 Lisp_Object Vw32_mouse_button_tolerance;
138
139 /* Minimum interval between mouse movement (and scroll bar drag)
140 events that are passed on to the event loop. */
141 Lisp_Object Vw32_mouse_move_interval;
142
143 /* The name we're using in resource queries. */
144 Lisp_Object Vx_resource_name;
145
146 /* Non nil if no window manager is in use. */
147 Lisp_Object Vx_no_window_manager;
148
149 /* Non-zero means we're allowed to display a busy cursor. */
150
151 int display_busy_cursor_p;
152
153 /* The background and shape of the mouse pointer, and shape when not
154 over text or in the modeline. */
155
156 Lisp_Object Vx_pointer_shape, Vx_nontext_pointer_shape, Vx_mode_pointer_shape;
157 Lisp_Object Vx_busy_pointer_shape;
158
159 /* The shape when over mouse-sensitive text. */
160
161 Lisp_Object Vx_sensitive_text_pointer_shape;
162
163 /* Color of chars displayed in cursor box. */
164
165 Lisp_Object Vx_cursor_fore_pixel;
166
167 /* Nonzero if using Windows. */
168
169 static int w32_in_use;
170
171 /* Search path for bitmap files. */
172
173 Lisp_Object Vx_bitmap_file_path;
174
175 /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'. */
176
177 Lisp_Object Vx_pixel_size_width_font_regexp;
178
179 /* Alist of bdf fonts and the files that define them. */
180 Lisp_Object Vw32_bdf_filename_alist;
181
182 Lisp_Object Vw32_system_coding_system;
183
184 /* A flag to control whether fonts are matched strictly or not. */
185 int w32_strict_fontnames;
186
187 /* A flag to control whether we should only repaint if GetUpdateRect
188 indicates there is an update region. */
189 int w32_strict_painting;
190
191 /* Associative list linking character set strings to Windows codepages. */
192 Lisp_Object Vw32_charset_info_alist;
193
194 /* VIETNAMESE_CHARSET is not defined in some versions of MSVC. */
195 #ifndef VIETNAMESE_CHARSET
196 #define VIETNAMESE_CHARSET 163
197 #endif
198
199
200 /* Evaluate this expression to rebuild the section of syms_of_w32fns
201 that initializes and staticpros the symbols declared below. Note
202 that Emacs 18 has a bug that keeps C-x C-e from being able to
203 evaluate this expression.
204
205 (progn
206 ;; Accumulate a list of the symbols we want to initialize from the
207 ;; declarations at the top of the file.
208 (goto-char (point-min))
209 (search-forward "/\*&&& symbols declared here &&&*\/\n")
210 (let (symbol-list)
211 (while (looking-at "Lisp_Object \\(Q[a-z_]+\\)")
212 (setq symbol-list
213 (cons (buffer-substring (match-beginning 1) (match-end 1))
214 symbol-list))
215 (forward-line 1))
216 (setq symbol-list (nreverse symbol-list))
217 ;; Delete the section of syms_of_... where we initialize the symbols.
218 (search-forward "\n /\*&&& init symbols here &&&*\/\n")
219 (let ((start (point)))
220 (while (looking-at "^ Q")
221 (forward-line 2))
222 (kill-region start (point)))
223 ;; Write a new symbol initialization section.
224 (while symbol-list
225 (insert (format " %s = intern (\"" (car symbol-list)))
226 (let ((start (point)))
227 (insert (substring (car symbol-list) 1))
228 (subst-char-in-region start (point) ?_ ?-))
229 (insert (format "\");\n staticpro (&%s);\n" (car symbol-list)))
230 (setq symbol-list (cdr symbol-list)))))
231
232 */
233
234 /*&&& symbols declared here &&&*/
235 Lisp_Object Qauto_raise;
236 Lisp_Object Qauto_lower;
237 Lisp_Object Qbar;
238 Lisp_Object Qborder_color;
239 Lisp_Object Qborder_width;
240 Lisp_Object Qbox;
241 Lisp_Object Qcursor_color;
242 Lisp_Object Qcursor_type;
243 Lisp_Object Qgeometry;
244 Lisp_Object Qicon_left;
245 Lisp_Object Qicon_top;
246 Lisp_Object Qicon_type;
247 Lisp_Object Qicon_name;
248 Lisp_Object Qinternal_border_width;
249 Lisp_Object Qleft;
250 Lisp_Object Qright;
251 Lisp_Object Qmouse_color;
252 Lisp_Object Qnone;
253 Lisp_Object Qparent_id;
254 Lisp_Object Qscroll_bar_width;
255 Lisp_Object Qsuppress_icon;
256 Lisp_Object Qundefined_color;
257 Lisp_Object Qvertical_scroll_bars;
258 Lisp_Object Qvisibility;
259 Lisp_Object Qwindow_id;
260 Lisp_Object Qx_frame_parameter;
261 Lisp_Object Qx_resource_name;
262 Lisp_Object Quser_position;
263 Lisp_Object Quser_size;
264 Lisp_Object Qscreen_gamma;
265 Lisp_Object Qline_spacing;
266 Lisp_Object Qcenter;
267 Lisp_Object Qhyper;
268 Lisp_Object Qsuper;
269 Lisp_Object Qmeta;
270 Lisp_Object Qalt;
271 Lisp_Object Qctrl;
272 Lisp_Object Qcontrol;
273 Lisp_Object Qshift;
274
275 Lisp_Object Qw32_charset_ansi;
276 Lisp_Object Qw32_charset_default;
277 Lisp_Object Qw32_charset_symbol;
278 Lisp_Object Qw32_charset_shiftjis;
279 Lisp_Object Qw32_charset_hangeul;
280 Lisp_Object Qw32_charset_gb2312;
281 Lisp_Object Qw32_charset_chinesebig5;
282 Lisp_Object Qw32_charset_oem;
283
284 #ifndef JOHAB_CHARSET
285 #define JOHAB_CHARSET 130
286 #endif
287 #ifdef JOHAB_CHARSET
288 Lisp_Object Qw32_charset_easteurope;
289 Lisp_Object Qw32_charset_turkish;
290 Lisp_Object Qw32_charset_baltic;
291 Lisp_Object Qw32_charset_russian;
292 Lisp_Object Qw32_charset_arabic;
293 Lisp_Object Qw32_charset_greek;
294 Lisp_Object Qw32_charset_hebrew;
295 Lisp_Object Qw32_charset_vietnamese;
296 Lisp_Object Qw32_charset_thai;
297 Lisp_Object Qw32_charset_johab;
298 Lisp_Object Qw32_charset_mac;
299 #endif
300
301 #ifdef UNICODE_CHARSET
302 Lisp_Object Qw32_charset_unicode;
303 #endif
304
305 extern Lisp_Object Qtop;
306 extern Lisp_Object Qdisplay;
307 extern Lisp_Object Qtool_bar_lines;
308
309 /* State variables for emulating a three button mouse. */
310 #define LMOUSE 1
311 #define MMOUSE 2
312 #define RMOUSE 4
313
314 static int button_state = 0;
315 static W32Msg saved_mouse_button_msg;
316 static unsigned mouse_button_timer; /* non-zero when timer is active */
317 static W32Msg saved_mouse_move_msg;
318 static unsigned mouse_move_timer;
319
320 /* W95 mousewheel handler */
321 unsigned int msh_mousewheel = 0;
322
323 #define MOUSE_BUTTON_ID 1
324 #define MOUSE_MOVE_ID 2
325
326 /* The below are defined in frame.c. */
327
328 extern Lisp_Object Qheight, Qminibuffer, Qname, Qonly, Qwidth;
329 extern Lisp_Object Qunsplittable, Qmenu_bar_lines, Qbuffer_predicate, Qtitle;
330 extern Lisp_Object Qtool_bar_lines;
331
332 extern Lisp_Object Vwindow_system_version;
333
334 Lisp_Object Qface_set_after_frame_default;
335
336 /* From w32term.c. */
337 extern Lisp_Object Vw32_num_mouse_buttons;
338 extern Lisp_Object Vw32_recognize_altgr;
339
340 \f
341 /* Error if we are not connected to MS-Windows. */
342 void
343 check_w32 ()
344 {
345 if (! w32_in_use)
346 error ("MS-Windows not in use or not initialized");
347 }
348
349 /* Nonzero if we can use mouse menus.
350 You should not call this unless HAVE_MENUS is defined. */
351
352 int
353 have_menus_p ()
354 {
355 return w32_in_use;
356 }
357
358 /* Extract a frame as a FRAME_PTR, defaulting to the selected frame
359 and checking validity for W32. */
360
361 FRAME_PTR
362 check_x_frame (frame)
363 Lisp_Object frame;
364 {
365 FRAME_PTR f;
366
367 if (NILP (frame))
368 frame = selected_frame;
369 CHECK_LIVE_FRAME (frame, 0);
370 f = XFRAME (frame);
371 if (! FRAME_W32_P (f))
372 error ("non-w32 frame used");
373 return f;
374 }
375
376 /* Let the user specify an display with a frame.
377 nil stands for the selected frame--or, if that is not a w32 frame,
378 the first display on the list. */
379
380 static struct w32_display_info *
381 check_x_display_info (frame)
382 Lisp_Object frame;
383 {
384 if (NILP (frame))
385 {
386 struct frame *sf = XFRAME (selected_frame);
387
388 if (FRAME_W32_P (sf) && FRAME_LIVE_P (sf))
389 return FRAME_W32_DISPLAY_INFO (sf);
390 else
391 return &one_w32_display_info;
392 }
393 else if (STRINGP (frame))
394 return x_display_info_for_name (frame);
395 else
396 {
397 FRAME_PTR f;
398
399 CHECK_LIVE_FRAME (frame, 0);
400 f = XFRAME (frame);
401 if (! FRAME_W32_P (f))
402 error ("non-w32 frame used");
403 return FRAME_W32_DISPLAY_INFO (f);
404 }
405 }
406 \f
407 /* Return the Emacs frame-object corresponding to an w32 window.
408 It could be the frame's main window or an icon window. */
409
410 /* This function can be called during GC, so use GC_xxx type test macros. */
411
412 struct frame *
413 x_window_to_frame (dpyinfo, wdesc)
414 struct w32_display_info *dpyinfo;
415 HWND wdesc;
416 {
417 Lisp_Object tail, frame;
418 struct frame *f;
419
420 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
421 {
422 frame = XCAR (tail);
423 if (!GC_FRAMEP (frame))
424 continue;
425 f = XFRAME (frame);
426 if (!FRAME_W32_P (f) || FRAME_W32_DISPLAY_INFO (f) != dpyinfo)
427 continue;
428 if (f->output_data.w32->busy_window == wdesc)
429 return f;
430
431 /* TODO: Check tooltips when supported. */
432 if (FRAME_W32_WINDOW (f) == wdesc)
433 return f;
434 }
435 return 0;
436 }
437
438 \f
439
440 /* Code to deal with bitmaps. Bitmaps are referenced by their bitmap
441 id, which is just an int that this section returns. Bitmaps are
442 reference counted so they can be shared among frames.
443
444 Bitmap indices are guaranteed to be > 0, so a negative number can
445 be used to indicate no bitmap.
446
447 If you use x_create_bitmap_from_data, then you must keep track of
448 the bitmaps yourself. That is, creating a bitmap from the same
449 data more than once will not be caught. */
450
451
452 /* Functions to access the contents of a bitmap, given an id. */
453
454 int
455 x_bitmap_height (f, id)
456 FRAME_PTR f;
457 int id;
458 {
459 return FRAME_W32_DISPLAY_INFO (f)->bitmaps[id - 1].height;
460 }
461
462 int
463 x_bitmap_width (f, id)
464 FRAME_PTR f;
465 int id;
466 {
467 return FRAME_W32_DISPLAY_INFO (f)->bitmaps[id - 1].width;
468 }
469
470 int
471 x_bitmap_pixmap (f, id)
472 FRAME_PTR f;
473 int id;
474 {
475 return (int) FRAME_W32_DISPLAY_INFO (f)->bitmaps[id - 1].pixmap;
476 }
477
478
479 /* Allocate a new bitmap record. Returns index of new record. */
480
481 static int
482 x_allocate_bitmap_record (f)
483 FRAME_PTR f;
484 {
485 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
486 int i;
487
488 if (dpyinfo->bitmaps == NULL)
489 {
490 dpyinfo->bitmaps_size = 10;
491 dpyinfo->bitmaps
492 = (struct w32_bitmap_record *) xmalloc (dpyinfo->bitmaps_size * sizeof (struct w32_bitmap_record));
493 dpyinfo->bitmaps_last = 1;
494 return 1;
495 }
496
497 if (dpyinfo->bitmaps_last < dpyinfo->bitmaps_size)
498 return ++dpyinfo->bitmaps_last;
499
500 for (i = 0; i < dpyinfo->bitmaps_size; ++i)
501 if (dpyinfo->bitmaps[i].refcount == 0)
502 return i + 1;
503
504 dpyinfo->bitmaps_size *= 2;
505 dpyinfo->bitmaps
506 = (struct w32_bitmap_record *) xrealloc (dpyinfo->bitmaps,
507 dpyinfo->bitmaps_size * sizeof (struct w32_bitmap_record));
508 return ++dpyinfo->bitmaps_last;
509 }
510
511 /* Add one reference to the reference count of the bitmap with id ID. */
512
513 void
514 x_reference_bitmap (f, id)
515 FRAME_PTR f;
516 int id;
517 {
518 ++FRAME_W32_DISPLAY_INFO (f)->bitmaps[id - 1].refcount;
519 }
520
521 /* Create a bitmap for frame F from a HEIGHT x WIDTH array of bits at BITS. */
522
523 int
524 x_create_bitmap_from_data (f, bits, width, height)
525 struct frame *f;
526 char *bits;
527 unsigned int width, height;
528 {
529 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
530 Pixmap bitmap;
531 int id;
532
533 bitmap = CreateBitmap (width, height,
534 FRAME_W32_DISPLAY_INFO (XFRAME (frame))->n_planes,
535 FRAME_W32_DISPLAY_INFO (XFRAME (frame))->n_cbits,
536 bits);
537
538 if (! bitmap)
539 return -1;
540
541 id = x_allocate_bitmap_record (f);
542 dpyinfo->bitmaps[id - 1].pixmap = bitmap;
543 dpyinfo->bitmaps[id - 1].file = NULL;
544 dpyinfo->bitmaps[id - 1].hinst = NULL;
545 dpyinfo->bitmaps[id - 1].refcount = 1;
546 dpyinfo->bitmaps[id - 1].depth = 1;
547 dpyinfo->bitmaps[id - 1].height = height;
548 dpyinfo->bitmaps[id - 1].width = width;
549
550 return id;
551 }
552
553 /* Create bitmap from file FILE for frame F. */
554
555 int
556 x_create_bitmap_from_file (f, file)
557 struct frame *f;
558 Lisp_Object file;
559 {
560 return -1;
561 #if 0 /* TODO : bitmap support */
562 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
563 unsigned int width, height;
564 HBITMAP bitmap;
565 int xhot, yhot, result, id;
566 Lisp_Object found;
567 int fd;
568 char *filename;
569 HINSTANCE hinst;
570
571 /* Look for an existing bitmap with the same name. */
572 for (id = 0; id < dpyinfo->bitmaps_last; ++id)
573 {
574 if (dpyinfo->bitmaps[id].refcount
575 && dpyinfo->bitmaps[id].file
576 && !strcmp (dpyinfo->bitmaps[id].file, (char *) XSTRING (file)->data))
577 {
578 ++dpyinfo->bitmaps[id].refcount;
579 return id + 1;
580 }
581 }
582
583 /* Search bitmap-file-path for the file, if appropriate. */
584 fd = openp (Vx_bitmap_file_path, file, "", &found, 0);
585 if (fd < 0)
586 return -1;
587 emacs_close (fd);
588
589 filename = (char *) XSTRING (found)->data;
590
591 hinst = LoadLibraryEx (filename, NULL, LOAD_LIBRARY_AS_DATAFILE);
592
593 if (hinst == NULL)
594 return -1;
595
596
597 result = XReadBitmapFile (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
598 filename, &width, &height, &bitmap, &xhot, &yhot);
599 if (result != BitmapSuccess)
600 return -1;
601
602 id = x_allocate_bitmap_record (f);
603 dpyinfo->bitmaps[id - 1].pixmap = bitmap;
604 dpyinfo->bitmaps[id - 1].refcount = 1;
605 dpyinfo->bitmaps[id - 1].file = (char *) xmalloc (XSTRING (file)->size + 1);
606 dpyinfo->bitmaps[id - 1].depth = 1;
607 dpyinfo->bitmaps[id - 1].height = height;
608 dpyinfo->bitmaps[id - 1].width = width;
609 strcpy (dpyinfo->bitmaps[id - 1].file, XSTRING (file)->data);
610
611 return id;
612 #endif /* TODO */
613 }
614
615 /* Remove reference to bitmap with id number ID. */
616
617 void
618 x_destroy_bitmap (f, id)
619 FRAME_PTR f;
620 int id;
621 {
622 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
623
624 if (id > 0)
625 {
626 --dpyinfo->bitmaps[id - 1].refcount;
627 if (dpyinfo->bitmaps[id - 1].refcount == 0)
628 {
629 BLOCK_INPUT;
630 DeleteObject (dpyinfo->bitmaps[id - 1].pixmap);
631 if (dpyinfo->bitmaps[id - 1].file)
632 {
633 xfree (dpyinfo->bitmaps[id - 1].file);
634 dpyinfo->bitmaps[id - 1].file = NULL;
635 }
636 UNBLOCK_INPUT;
637 }
638 }
639 }
640
641 /* Free all the bitmaps for the display specified by DPYINFO. */
642
643 static void
644 x_destroy_all_bitmaps (dpyinfo)
645 struct w32_display_info *dpyinfo;
646 {
647 int i;
648 for (i = 0; i < dpyinfo->bitmaps_last; i++)
649 if (dpyinfo->bitmaps[i].refcount > 0)
650 {
651 DeleteObject (dpyinfo->bitmaps[i].pixmap);
652 if (dpyinfo->bitmaps[i].file)
653 xfree (dpyinfo->bitmaps[i].file);
654 }
655 dpyinfo->bitmaps_last = 0;
656 }
657 \f
658 /* Connect the frame-parameter names for W32 frames
659 to the ways of passing the parameter values to the window system.
660
661 The name of a parameter, as a Lisp symbol,
662 has an `x-frame-parameter' property which is an integer in Lisp
663 but can be interpreted as an `enum x_frame_parm' in C. */
664
665 enum x_frame_parm
666 {
667 X_PARM_FOREGROUND_COLOR,
668 X_PARM_BACKGROUND_COLOR,
669 X_PARM_MOUSE_COLOR,
670 X_PARM_CURSOR_COLOR,
671 X_PARM_BORDER_COLOR,
672 X_PARM_ICON_TYPE,
673 X_PARM_FONT,
674 X_PARM_BORDER_WIDTH,
675 X_PARM_INTERNAL_BORDER_WIDTH,
676 X_PARM_NAME,
677 X_PARM_AUTORAISE,
678 X_PARM_AUTOLOWER,
679 X_PARM_VERT_SCROLL_BAR,
680 X_PARM_VISIBILITY,
681 X_PARM_MENU_BAR_LINES
682 };
683
684
685 struct x_frame_parm_table
686 {
687 char *name;
688 void (*setter) P_ ((struct frame *, Lisp_Object, Lisp_Object));
689 };
690
691 /* TODO: Native Input Method support; see x_create_im. */
692 void x_set_foreground_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
693 static void x_set_line_spacing P_ ((struct frame *, Lisp_Object, Lisp_Object));
694 void x_set_background_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
695 void x_set_mouse_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
696 void x_set_cursor_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
697 void x_set_border_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
698 void x_set_cursor_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
699 void x_set_icon_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
700 void x_set_icon_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
701 void x_set_font P_ ((struct frame *, Lisp_Object, Lisp_Object));
702 void x_set_border_width P_ ((struct frame *, Lisp_Object, Lisp_Object));
703 void x_set_internal_border_width P_ ((struct frame *, Lisp_Object,
704 Lisp_Object));
705 void x_explicitly_set_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
706 void x_set_autoraise P_ ((struct frame *, Lisp_Object, Lisp_Object));
707 void x_set_autolower P_ ((struct frame *, Lisp_Object, Lisp_Object));
708 void x_set_vertical_scroll_bars P_ ((struct frame *, Lisp_Object,
709 Lisp_Object));
710 void x_set_visibility P_ ((struct frame *, Lisp_Object, Lisp_Object));
711 void x_set_menu_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
712 void x_set_scroll_bar_width P_ ((struct frame *, Lisp_Object, Lisp_Object));
713 void x_set_title P_ ((struct frame *, Lisp_Object, Lisp_Object));
714 void x_set_unsplittable P_ ((struct frame *, Lisp_Object, Lisp_Object));
715 void x_set_tool_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
716 static void x_set_screen_gamma P_ ((struct frame *, Lisp_Object, Lisp_Object));
717
718 static struct x_frame_parm_table x_frame_parms[] =
719 {
720 "auto-raise", x_set_autoraise,
721 "auto-lower", x_set_autolower,
722 "background-color", x_set_background_color,
723 "border-color", x_set_border_color,
724 "border-width", x_set_border_width,
725 "cursor-color", x_set_cursor_color,
726 "cursor-type", x_set_cursor_type,
727 "font", x_set_font,
728 "foreground-color", x_set_foreground_color,
729 "icon-name", x_set_icon_name,
730 "icon-type", x_set_icon_type,
731 "internal-border-width", x_set_internal_border_width,
732 "menu-bar-lines", x_set_menu_bar_lines,
733 "mouse-color", x_set_mouse_color,
734 "name", x_explicitly_set_name,
735 "scroll-bar-width", x_set_scroll_bar_width,
736 "title", x_set_title,
737 "unsplittable", x_set_unsplittable,
738 "vertical-scroll-bars", x_set_vertical_scroll_bars,
739 "visibility", x_set_visibility,
740 "tool-bar-lines", x_set_tool_bar_lines,
741 "screen-gamma", x_set_screen_gamma,
742 "line-spacing", x_set_line_spacing
743 };
744
745 /* Attach the `x-frame-parameter' properties to
746 the Lisp symbol names of parameters relevant to W32. */
747
748 void
749 init_x_parm_symbols ()
750 {
751 int i;
752
753 for (i = 0; i < sizeof (x_frame_parms) / sizeof (x_frame_parms[0]); i++)
754 Fput (intern (x_frame_parms[i].name), Qx_frame_parameter,
755 make_number (i));
756 }
757 \f
758 /* Change the parameters of frame F as specified by ALIST.
759 If a parameter is not specially recognized, do nothing;
760 otherwise call the `x_set_...' function for that parameter. */
761
762 void
763 x_set_frame_parameters (f, alist)
764 FRAME_PTR f;
765 Lisp_Object alist;
766 {
767 Lisp_Object tail;
768
769 /* If both of these parameters are present, it's more efficient to
770 set them both at once. So we wait until we've looked at the
771 entire list before we set them. */
772 int width, height;
773
774 /* Same here. */
775 Lisp_Object left, top;
776
777 /* Same with these. */
778 Lisp_Object icon_left, icon_top;
779
780 /* Record in these vectors all the parms specified. */
781 Lisp_Object *parms;
782 Lisp_Object *values;
783 int i, p;
784 int left_no_change = 0, top_no_change = 0;
785 int icon_left_no_change = 0, icon_top_no_change = 0;
786
787 struct gcpro gcpro1, gcpro2;
788
789 i = 0;
790 for (tail = alist; CONSP (tail); tail = Fcdr (tail))
791 i++;
792
793 parms = (Lisp_Object *) alloca (i * sizeof (Lisp_Object));
794 values = (Lisp_Object *) alloca (i * sizeof (Lisp_Object));
795
796 /* Extract parm names and values into those vectors. */
797
798 i = 0;
799 for (tail = alist; CONSP (tail); tail = Fcdr (tail))
800 {
801 Lisp_Object elt;
802
803 elt = Fcar (tail);
804 parms[i] = Fcar (elt);
805 values[i] = Fcdr (elt);
806 i++;
807 }
808 /* TAIL and ALIST are not used again below here. */
809 alist = tail = Qnil;
810
811 GCPRO2 (*parms, *values);
812 gcpro1.nvars = i;
813 gcpro2.nvars = i;
814
815 /* There is no need to gcpro LEFT, TOP, ICON_LEFT, or ICON_TOP,
816 because their values appear in VALUES and strings are not valid. */
817 top = left = Qunbound;
818 icon_left = icon_top = Qunbound;
819
820 /* Provide default values for HEIGHT and WIDTH. */
821 if (FRAME_NEW_WIDTH (f))
822 width = FRAME_NEW_WIDTH (f);
823 else
824 width = FRAME_WIDTH (f);
825
826 if (FRAME_NEW_HEIGHT (f))
827 height = FRAME_NEW_HEIGHT (f);
828 else
829 height = FRAME_HEIGHT (f);
830
831 /* Process foreground_color and background_color before anything else.
832 They are independent of other properties, but other properties (e.g.,
833 cursor_color) are dependent upon them. */
834 for (p = 0; p < i; p++)
835 {
836 Lisp_Object prop, val;
837
838 prop = parms[p];
839 val = values[p];
840 if (EQ (prop, Qforeground_color) || EQ (prop, Qbackground_color))
841 {
842 register Lisp_Object param_index, old_value;
843
844 param_index = Fget (prop, Qx_frame_parameter);
845 old_value = get_frame_param (f, prop);
846 store_frame_param (f, prop, val);
847 if (NATNUMP (param_index)
848 && (XFASTINT (param_index)
849 < sizeof (x_frame_parms)/sizeof (x_frame_parms[0])))
850 (*x_frame_parms[XINT (param_index)].setter)(f, val, old_value);
851 }
852 }
853
854 /* Now process them in reverse of specified order. */
855 for (i--; i >= 0; i--)
856 {
857 Lisp_Object prop, val;
858
859 prop = parms[i];
860 val = values[i];
861
862 if (EQ (prop, Qwidth) && NUMBERP (val))
863 width = XFASTINT (val);
864 else if (EQ (prop, Qheight) && NUMBERP (val))
865 height = XFASTINT (val);
866 else if (EQ (prop, Qtop))
867 top = val;
868 else if (EQ (prop, Qleft))
869 left = val;
870 else if (EQ (prop, Qicon_top))
871 icon_top = val;
872 else if (EQ (prop, Qicon_left))
873 icon_left = val;
874 else if (EQ (prop, Qforeground_color) || EQ (prop, Qbackground_color))
875 /* Processed above. */
876 continue;
877 else
878 {
879 register Lisp_Object param_index, old_value;
880
881 param_index = Fget (prop, Qx_frame_parameter);
882 old_value = get_frame_param (f, prop);
883 store_frame_param (f, prop, val);
884 if (NATNUMP (param_index)
885 && (XFASTINT (param_index)
886 < sizeof (x_frame_parms)/sizeof (x_frame_parms[0])))
887 (*x_frame_parms[XINT (param_index)].setter)(f, val, old_value);
888 }
889 }
890
891 /* Don't die if just one of these was set. */
892 if (EQ (left, Qunbound))
893 {
894 left_no_change = 1;
895 if (f->output_data.w32->left_pos < 0)
896 left = Fcons (Qplus, Fcons (make_number (f->output_data.w32->left_pos), Qnil));
897 else
898 XSETINT (left, f->output_data.w32->left_pos);
899 }
900 if (EQ (top, Qunbound))
901 {
902 top_no_change = 1;
903 if (f->output_data.w32->top_pos < 0)
904 top = Fcons (Qplus, Fcons (make_number (f->output_data.w32->top_pos), Qnil));
905 else
906 XSETINT (top, f->output_data.w32->top_pos);
907 }
908
909 /* If one of the icon positions was not set, preserve or default it. */
910 if (EQ (icon_left, Qunbound) || ! INTEGERP (icon_left))
911 {
912 icon_left_no_change = 1;
913 icon_left = Fcdr (Fassq (Qicon_left, f->param_alist));
914 if (NILP (icon_left))
915 XSETINT (icon_left, 0);
916 }
917 if (EQ (icon_top, Qunbound) || ! INTEGERP (icon_top))
918 {
919 icon_top_no_change = 1;
920 icon_top = Fcdr (Fassq (Qicon_top, f->param_alist));
921 if (NILP (icon_top))
922 XSETINT (icon_top, 0);
923 }
924
925 /* Don't set these parameters unless they've been explicitly
926 specified. The window might be mapped or resized while we're in
927 this function, and we don't want to override that unless the lisp
928 code has asked for it.
929
930 Don't set these parameters unless they actually differ from the
931 window's current parameters; the window may not actually exist
932 yet. */
933 {
934 Lisp_Object frame;
935
936 check_frame_size (f, &height, &width);
937
938 XSETFRAME (frame, f);
939
940 if (width != FRAME_WIDTH (f)
941 || height != FRAME_HEIGHT (f)
942 || FRAME_NEW_HEIGHT (f) || FRAME_NEW_WIDTH (f))
943 Fset_frame_size (frame, make_number (width), make_number (height));
944
945 if ((!NILP (left) || !NILP (top))
946 && ! (left_no_change && top_no_change)
947 && ! (NUMBERP (left) && XINT (left) == f->output_data.w32->left_pos
948 && NUMBERP (top) && XINT (top) == f->output_data.w32->top_pos))
949 {
950 int leftpos = 0;
951 int toppos = 0;
952
953 /* Record the signs. */
954 f->output_data.w32->size_hint_flags &= ~ (XNegative | YNegative);
955 if (EQ (left, Qminus))
956 f->output_data.w32->size_hint_flags |= XNegative;
957 else if (INTEGERP (left))
958 {
959 leftpos = XINT (left);
960 if (leftpos < 0)
961 f->output_data.w32->size_hint_flags |= XNegative;
962 }
963 else if (CONSP (left) && EQ (XCAR (left), Qminus)
964 && CONSP (XCDR (left))
965 && INTEGERP (XCAR (XCDR (left))))
966 {
967 leftpos = - XINT (XCAR (XCDR (left)));
968 f->output_data.w32->size_hint_flags |= XNegative;
969 }
970 else if (CONSP (left) && EQ (XCAR (left), Qplus)
971 && CONSP (XCDR (left))
972 && INTEGERP (XCAR (XCDR (left))))
973 {
974 leftpos = XINT (XCAR (XCDR (left)));
975 }
976
977 if (EQ (top, Qminus))
978 f->output_data.w32->size_hint_flags |= YNegative;
979 else if (INTEGERP (top))
980 {
981 toppos = XINT (top);
982 if (toppos < 0)
983 f->output_data.w32->size_hint_flags |= YNegative;
984 }
985 else if (CONSP (top) && EQ (XCAR (top), Qminus)
986 && CONSP (XCDR (top))
987 && INTEGERP (XCAR (XCDR (top))))
988 {
989 toppos = - XINT (XCAR (XCDR (top)));
990 f->output_data.w32->size_hint_flags |= YNegative;
991 }
992 else if (CONSP (top) && EQ (XCAR (top), Qplus)
993 && CONSP (XCDR (top))
994 && INTEGERP (XCAR (XCDR (top))))
995 {
996 toppos = XINT (XCAR (XCDR (top)));
997 }
998
999
1000 /* Store the numeric value of the position. */
1001 f->output_data.w32->top_pos = toppos;
1002 f->output_data.w32->left_pos = leftpos;
1003
1004 f->output_data.w32->win_gravity = NorthWestGravity;
1005
1006 /* Actually set that position, and convert to absolute. */
1007 x_set_offset (f, leftpos, toppos, -1);
1008 }
1009
1010 if ((!NILP (icon_left) || !NILP (icon_top))
1011 && ! (icon_left_no_change && icon_top_no_change))
1012 x_wm_set_icon_position (f, XINT (icon_left), XINT (icon_top));
1013 }
1014
1015 UNGCPRO;
1016 }
1017
1018 /* Store the screen positions of frame F into XPTR and YPTR.
1019 These are the positions of the containing window manager window,
1020 not Emacs's own window. */
1021
1022 void
1023 x_real_positions (f, xptr, yptr)
1024 FRAME_PTR f;
1025 int *xptr, *yptr;
1026 {
1027 POINT pt;
1028
1029 {
1030 RECT rect;
1031
1032 GetClientRect(FRAME_W32_WINDOW(f), &rect);
1033 AdjustWindowRect(&rect, f->output_data.w32->dwStyle, FRAME_EXTERNAL_MENU_BAR(f));
1034
1035 pt.x = rect.left;
1036 pt.y = rect.top;
1037 }
1038
1039 ClientToScreen (FRAME_W32_WINDOW(f), &pt);
1040
1041 *xptr = pt.x;
1042 *yptr = pt.y;
1043 }
1044
1045 /* Insert a description of internally-recorded parameters of frame X
1046 into the parameter alist *ALISTPTR that is to be given to the user.
1047 Only parameters that are specific to W32
1048 and whose values are not correctly recorded in the frame's
1049 param_alist need to be considered here. */
1050
1051 void
1052 x_report_frame_params (f, alistptr)
1053 struct frame *f;
1054 Lisp_Object *alistptr;
1055 {
1056 char buf[16];
1057 Lisp_Object tem;
1058
1059 /* Represent negative positions (off the top or left screen edge)
1060 in a way that Fmodify_frame_parameters will understand correctly. */
1061 XSETINT (tem, f->output_data.w32->left_pos);
1062 if (f->output_data.w32->left_pos >= 0)
1063 store_in_alist (alistptr, Qleft, tem);
1064 else
1065 store_in_alist (alistptr, Qleft, Fcons (Qplus, Fcons (tem, Qnil)));
1066
1067 XSETINT (tem, f->output_data.w32->top_pos);
1068 if (f->output_data.w32->top_pos >= 0)
1069 store_in_alist (alistptr, Qtop, tem);
1070 else
1071 store_in_alist (alistptr, Qtop, Fcons (Qplus, Fcons (tem, Qnil)));
1072
1073 store_in_alist (alistptr, Qborder_width,
1074 make_number (f->output_data.w32->border_width));
1075 store_in_alist (alistptr, Qinternal_border_width,
1076 make_number (f->output_data.w32->internal_border_width));
1077 sprintf (buf, "%ld", (long) FRAME_W32_WINDOW (f));
1078 store_in_alist (alistptr, Qwindow_id,
1079 build_string (buf));
1080 store_in_alist (alistptr, Qicon_name, f->icon_name);
1081 FRAME_SAMPLE_VISIBILITY (f);
1082 store_in_alist (alistptr, Qvisibility,
1083 (FRAME_VISIBLE_P (f) ? Qt
1084 : FRAME_ICONIFIED_P (f) ? Qicon : Qnil));
1085 store_in_alist (alistptr, Qdisplay,
1086 XCAR (FRAME_W32_DISPLAY_INFO (f)->name_list_element));
1087 }
1088 \f
1089
1090 DEFUN ("w32-define-rgb-color", Fw32_define_rgb_color, Sw32_define_rgb_color, 4, 4, 0,
1091 "Convert RGB numbers to a windows color reference and associate with NAME (a string).\n\
1092 This adds or updates a named color to w32-color-map, making it available for use.\n\
1093 The original entry's RGB ref is returned, or nil if the entry is new.")
1094 (red, green, blue, name)
1095 Lisp_Object red, green, blue, name;
1096 {
1097 Lisp_Object rgb;
1098 Lisp_Object oldrgb = Qnil;
1099 Lisp_Object entry;
1100
1101 CHECK_NUMBER (red, 0);
1102 CHECK_NUMBER (green, 0);
1103 CHECK_NUMBER (blue, 0);
1104 CHECK_STRING (name, 0);
1105
1106 XSET (rgb, Lisp_Int, RGB(XUINT (red), XUINT (green), XUINT (blue)));
1107
1108 BLOCK_INPUT;
1109
1110 /* replace existing entry in w32-color-map or add new entry. */
1111 entry = Fassoc (name, Vw32_color_map);
1112 if (NILP (entry))
1113 {
1114 entry = Fcons (name, rgb);
1115 Vw32_color_map = Fcons (entry, Vw32_color_map);
1116 }
1117 else
1118 {
1119 oldrgb = Fcdr (entry);
1120 Fsetcdr (entry, rgb);
1121 }
1122
1123 UNBLOCK_INPUT;
1124
1125 return (oldrgb);
1126 }
1127
1128 DEFUN ("w32-load-color-file", Fw32_load_color_file, Sw32_load_color_file, 1, 1, 0,
1129 "Create an alist of color entries from an external file (ie. rgb.txt).\n\
1130 Assign this value to w32-color-map to replace the existing color map.\n\
1131 \
1132 The file should define one named RGB color per line like so:\
1133 R G B name\n\
1134 where R,G,B are numbers between 0 and 255 and name is an arbitrary string.")
1135 (filename)
1136 Lisp_Object filename;
1137 {
1138 FILE *fp;
1139 Lisp_Object cmap = Qnil;
1140 Lisp_Object abspath;
1141
1142 CHECK_STRING (filename, 0);
1143 abspath = Fexpand_file_name (filename, Qnil);
1144
1145 fp = fopen (XSTRING (filename)->data, "rt");
1146 if (fp)
1147 {
1148 char buf[512];
1149 int red, green, blue;
1150 int num;
1151
1152 BLOCK_INPUT;
1153
1154 while (fgets (buf, sizeof (buf), fp) != NULL) {
1155 if (sscanf (buf, "%u %u %u %n", &red, &green, &blue, &num) == 3)
1156 {
1157 char *name = buf + num;
1158 num = strlen (name) - 1;
1159 if (name[num] == '\n')
1160 name[num] = 0;
1161 cmap = Fcons (Fcons (build_string (name),
1162 make_number (RGB (red, green, blue))),
1163 cmap);
1164 }
1165 }
1166 fclose (fp);
1167
1168 UNBLOCK_INPUT;
1169 }
1170
1171 return cmap;
1172 }
1173
1174 /* The default colors for the w32 color map */
1175 typedef struct colormap_t
1176 {
1177 char *name;
1178 COLORREF colorref;
1179 } colormap_t;
1180
1181 colormap_t w32_color_map[] =
1182 {
1183 {"snow" , PALETTERGB (255,250,250)},
1184 {"ghost white" , PALETTERGB (248,248,255)},
1185 {"GhostWhite" , PALETTERGB (248,248,255)},
1186 {"white smoke" , PALETTERGB (245,245,245)},
1187 {"WhiteSmoke" , PALETTERGB (245,245,245)},
1188 {"gainsboro" , PALETTERGB (220,220,220)},
1189 {"floral white" , PALETTERGB (255,250,240)},
1190 {"FloralWhite" , PALETTERGB (255,250,240)},
1191 {"old lace" , PALETTERGB (253,245,230)},
1192 {"OldLace" , PALETTERGB (253,245,230)},
1193 {"linen" , PALETTERGB (250,240,230)},
1194 {"antique white" , PALETTERGB (250,235,215)},
1195 {"AntiqueWhite" , PALETTERGB (250,235,215)},
1196 {"papaya whip" , PALETTERGB (255,239,213)},
1197 {"PapayaWhip" , PALETTERGB (255,239,213)},
1198 {"blanched almond" , PALETTERGB (255,235,205)},
1199 {"BlanchedAlmond" , PALETTERGB (255,235,205)},
1200 {"bisque" , PALETTERGB (255,228,196)},
1201 {"peach puff" , PALETTERGB (255,218,185)},
1202 {"PeachPuff" , PALETTERGB (255,218,185)},
1203 {"navajo white" , PALETTERGB (255,222,173)},
1204 {"NavajoWhite" , PALETTERGB (255,222,173)},
1205 {"moccasin" , PALETTERGB (255,228,181)},
1206 {"cornsilk" , PALETTERGB (255,248,220)},
1207 {"ivory" , PALETTERGB (255,255,240)},
1208 {"lemon chiffon" , PALETTERGB (255,250,205)},
1209 {"LemonChiffon" , PALETTERGB (255,250,205)},
1210 {"seashell" , PALETTERGB (255,245,238)},
1211 {"honeydew" , PALETTERGB (240,255,240)},
1212 {"mint cream" , PALETTERGB (245,255,250)},
1213 {"MintCream" , PALETTERGB (245,255,250)},
1214 {"azure" , PALETTERGB (240,255,255)},
1215 {"alice blue" , PALETTERGB (240,248,255)},
1216 {"AliceBlue" , PALETTERGB (240,248,255)},
1217 {"lavender" , PALETTERGB (230,230,250)},
1218 {"lavender blush" , PALETTERGB (255,240,245)},
1219 {"LavenderBlush" , PALETTERGB (255,240,245)},
1220 {"misty rose" , PALETTERGB (255,228,225)},
1221 {"MistyRose" , PALETTERGB (255,228,225)},
1222 {"white" , PALETTERGB (255,255,255)},
1223 {"black" , PALETTERGB ( 0, 0, 0)},
1224 {"dark slate gray" , PALETTERGB ( 47, 79, 79)},
1225 {"DarkSlateGray" , PALETTERGB ( 47, 79, 79)},
1226 {"dark slate grey" , PALETTERGB ( 47, 79, 79)},
1227 {"DarkSlateGrey" , PALETTERGB ( 47, 79, 79)},
1228 {"dim gray" , PALETTERGB (105,105,105)},
1229 {"DimGray" , PALETTERGB (105,105,105)},
1230 {"dim grey" , PALETTERGB (105,105,105)},
1231 {"DimGrey" , PALETTERGB (105,105,105)},
1232 {"slate gray" , PALETTERGB (112,128,144)},
1233 {"SlateGray" , PALETTERGB (112,128,144)},
1234 {"slate grey" , PALETTERGB (112,128,144)},
1235 {"SlateGrey" , PALETTERGB (112,128,144)},
1236 {"light slate gray" , PALETTERGB (119,136,153)},
1237 {"LightSlateGray" , PALETTERGB (119,136,153)},
1238 {"light slate grey" , PALETTERGB (119,136,153)},
1239 {"LightSlateGrey" , PALETTERGB (119,136,153)},
1240 {"gray" , PALETTERGB (190,190,190)},
1241 {"grey" , PALETTERGB (190,190,190)},
1242 {"light grey" , PALETTERGB (211,211,211)},
1243 {"LightGrey" , PALETTERGB (211,211,211)},
1244 {"light gray" , PALETTERGB (211,211,211)},
1245 {"LightGray" , PALETTERGB (211,211,211)},
1246 {"midnight blue" , PALETTERGB ( 25, 25,112)},
1247 {"MidnightBlue" , PALETTERGB ( 25, 25,112)},
1248 {"navy" , PALETTERGB ( 0, 0,128)},
1249 {"navy blue" , PALETTERGB ( 0, 0,128)},
1250 {"NavyBlue" , PALETTERGB ( 0, 0,128)},
1251 {"cornflower blue" , PALETTERGB (100,149,237)},
1252 {"CornflowerBlue" , PALETTERGB (100,149,237)},
1253 {"dark slate blue" , PALETTERGB ( 72, 61,139)},
1254 {"DarkSlateBlue" , PALETTERGB ( 72, 61,139)},
1255 {"slate blue" , PALETTERGB (106, 90,205)},
1256 {"SlateBlue" , PALETTERGB (106, 90,205)},
1257 {"medium slate blue" , PALETTERGB (123,104,238)},
1258 {"MediumSlateBlue" , PALETTERGB (123,104,238)},
1259 {"light slate blue" , PALETTERGB (132,112,255)},
1260 {"LightSlateBlue" , PALETTERGB (132,112,255)},
1261 {"medium blue" , PALETTERGB ( 0, 0,205)},
1262 {"MediumBlue" , PALETTERGB ( 0, 0,205)},
1263 {"royal blue" , PALETTERGB ( 65,105,225)},
1264 {"RoyalBlue" , PALETTERGB ( 65,105,225)},
1265 {"blue" , PALETTERGB ( 0, 0,255)},
1266 {"dodger blue" , PALETTERGB ( 30,144,255)},
1267 {"DodgerBlue" , PALETTERGB ( 30,144,255)},
1268 {"deep sky blue" , PALETTERGB ( 0,191,255)},
1269 {"DeepSkyBlue" , PALETTERGB ( 0,191,255)},
1270 {"sky blue" , PALETTERGB (135,206,235)},
1271 {"SkyBlue" , PALETTERGB (135,206,235)},
1272 {"light sky blue" , PALETTERGB (135,206,250)},
1273 {"LightSkyBlue" , PALETTERGB (135,206,250)},
1274 {"steel blue" , PALETTERGB ( 70,130,180)},
1275 {"SteelBlue" , PALETTERGB ( 70,130,180)},
1276 {"light steel blue" , PALETTERGB (176,196,222)},
1277 {"LightSteelBlue" , PALETTERGB (176,196,222)},
1278 {"light blue" , PALETTERGB (173,216,230)},
1279 {"LightBlue" , PALETTERGB (173,216,230)},
1280 {"powder blue" , PALETTERGB (176,224,230)},
1281 {"PowderBlue" , PALETTERGB (176,224,230)},
1282 {"pale turquoise" , PALETTERGB (175,238,238)},
1283 {"PaleTurquoise" , PALETTERGB (175,238,238)},
1284 {"dark turquoise" , PALETTERGB ( 0,206,209)},
1285 {"DarkTurquoise" , PALETTERGB ( 0,206,209)},
1286 {"medium turquoise" , PALETTERGB ( 72,209,204)},
1287 {"MediumTurquoise" , PALETTERGB ( 72,209,204)},
1288 {"turquoise" , PALETTERGB ( 64,224,208)},
1289 {"cyan" , PALETTERGB ( 0,255,255)},
1290 {"light cyan" , PALETTERGB (224,255,255)},
1291 {"LightCyan" , PALETTERGB (224,255,255)},
1292 {"cadet blue" , PALETTERGB ( 95,158,160)},
1293 {"CadetBlue" , PALETTERGB ( 95,158,160)},
1294 {"medium aquamarine" , PALETTERGB (102,205,170)},
1295 {"MediumAquamarine" , PALETTERGB (102,205,170)},
1296 {"aquamarine" , PALETTERGB (127,255,212)},
1297 {"dark green" , PALETTERGB ( 0,100, 0)},
1298 {"DarkGreen" , PALETTERGB ( 0,100, 0)},
1299 {"dark olive green" , PALETTERGB ( 85,107, 47)},
1300 {"DarkOliveGreen" , PALETTERGB ( 85,107, 47)},
1301 {"dark sea green" , PALETTERGB (143,188,143)},
1302 {"DarkSeaGreen" , PALETTERGB (143,188,143)},
1303 {"sea green" , PALETTERGB ( 46,139, 87)},
1304 {"SeaGreen" , PALETTERGB ( 46,139, 87)},
1305 {"medium sea green" , PALETTERGB ( 60,179,113)},
1306 {"MediumSeaGreen" , PALETTERGB ( 60,179,113)},
1307 {"light sea green" , PALETTERGB ( 32,178,170)},
1308 {"LightSeaGreen" , PALETTERGB ( 32,178,170)},
1309 {"pale green" , PALETTERGB (152,251,152)},
1310 {"PaleGreen" , PALETTERGB (152,251,152)},
1311 {"spring green" , PALETTERGB ( 0,255,127)},
1312 {"SpringGreen" , PALETTERGB ( 0,255,127)},
1313 {"lawn green" , PALETTERGB (124,252, 0)},
1314 {"LawnGreen" , PALETTERGB (124,252, 0)},
1315 {"green" , PALETTERGB ( 0,255, 0)},
1316 {"chartreuse" , PALETTERGB (127,255, 0)},
1317 {"medium spring green" , PALETTERGB ( 0,250,154)},
1318 {"MediumSpringGreen" , PALETTERGB ( 0,250,154)},
1319 {"green yellow" , PALETTERGB (173,255, 47)},
1320 {"GreenYellow" , PALETTERGB (173,255, 47)},
1321 {"lime green" , PALETTERGB ( 50,205, 50)},
1322 {"LimeGreen" , PALETTERGB ( 50,205, 50)},
1323 {"yellow green" , PALETTERGB (154,205, 50)},
1324 {"YellowGreen" , PALETTERGB (154,205, 50)},
1325 {"forest green" , PALETTERGB ( 34,139, 34)},
1326 {"ForestGreen" , PALETTERGB ( 34,139, 34)},
1327 {"olive drab" , PALETTERGB (107,142, 35)},
1328 {"OliveDrab" , PALETTERGB (107,142, 35)},
1329 {"dark khaki" , PALETTERGB (189,183,107)},
1330 {"DarkKhaki" , PALETTERGB (189,183,107)},
1331 {"khaki" , PALETTERGB (240,230,140)},
1332 {"pale goldenrod" , PALETTERGB (238,232,170)},
1333 {"PaleGoldenrod" , PALETTERGB (238,232,170)},
1334 {"light goldenrod yellow" , PALETTERGB (250,250,210)},
1335 {"LightGoldenrodYellow" , PALETTERGB (250,250,210)},
1336 {"light yellow" , PALETTERGB (255,255,224)},
1337 {"LightYellow" , PALETTERGB (255,255,224)},
1338 {"yellow" , PALETTERGB (255,255, 0)},
1339 {"gold" , PALETTERGB (255,215, 0)},
1340 {"light goldenrod" , PALETTERGB (238,221,130)},
1341 {"LightGoldenrod" , PALETTERGB (238,221,130)},
1342 {"goldenrod" , PALETTERGB (218,165, 32)},
1343 {"dark goldenrod" , PALETTERGB (184,134, 11)},
1344 {"DarkGoldenrod" , PALETTERGB (184,134, 11)},
1345 {"rosy brown" , PALETTERGB (188,143,143)},
1346 {"RosyBrown" , PALETTERGB (188,143,143)},
1347 {"indian red" , PALETTERGB (205, 92, 92)},
1348 {"IndianRed" , PALETTERGB (205, 92, 92)},
1349 {"saddle brown" , PALETTERGB (139, 69, 19)},
1350 {"SaddleBrown" , PALETTERGB (139, 69, 19)},
1351 {"sienna" , PALETTERGB (160, 82, 45)},
1352 {"peru" , PALETTERGB (205,133, 63)},
1353 {"burlywood" , PALETTERGB (222,184,135)},
1354 {"beige" , PALETTERGB (245,245,220)},
1355 {"wheat" , PALETTERGB (245,222,179)},
1356 {"sandy brown" , PALETTERGB (244,164, 96)},
1357 {"SandyBrown" , PALETTERGB (244,164, 96)},
1358 {"tan" , PALETTERGB (210,180,140)},
1359 {"chocolate" , PALETTERGB (210,105, 30)},
1360 {"firebrick" , PALETTERGB (178,34, 34)},
1361 {"brown" , PALETTERGB (165,42, 42)},
1362 {"dark salmon" , PALETTERGB (233,150,122)},
1363 {"DarkSalmon" , PALETTERGB (233,150,122)},
1364 {"salmon" , PALETTERGB (250,128,114)},
1365 {"light salmon" , PALETTERGB (255,160,122)},
1366 {"LightSalmon" , PALETTERGB (255,160,122)},
1367 {"orange" , PALETTERGB (255,165, 0)},
1368 {"dark orange" , PALETTERGB (255,140, 0)},
1369 {"DarkOrange" , PALETTERGB (255,140, 0)},
1370 {"coral" , PALETTERGB (255,127, 80)},
1371 {"light coral" , PALETTERGB (240,128,128)},
1372 {"LightCoral" , PALETTERGB (240,128,128)},
1373 {"tomato" , PALETTERGB (255, 99, 71)},
1374 {"orange red" , PALETTERGB (255, 69, 0)},
1375 {"OrangeRed" , PALETTERGB (255, 69, 0)},
1376 {"red" , PALETTERGB (255, 0, 0)},
1377 {"hot pink" , PALETTERGB (255,105,180)},
1378 {"HotPink" , PALETTERGB (255,105,180)},
1379 {"deep pink" , PALETTERGB (255, 20,147)},
1380 {"DeepPink" , PALETTERGB (255, 20,147)},
1381 {"pink" , PALETTERGB (255,192,203)},
1382 {"light pink" , PALETTERGB (255,182,193)},
1383 {"LightPink" , PALETTERGB (255,182,193)},
1384 {"pale violet red" , PALETTERGB (219,112,147)},
1385 {"PaleVioletRed" , PALETTERGB (219,112,147)},
1386 {"maroon" , PALETTERGB (176, 48, 96)},
1387 {"medium violet red" , PALETTERGB (199, 21,133)},
1388 {"MediumVioletRed" , PALETTERGB (199, 21,133)},
1389 {"violet red" , PALETTERGB (208, 32,144)},
1390 {"VioletRed" , PALETTERGB (208, 32,144)},
1391 {"magenta" , PALETTERGB (255, 0,255)},
1392 {"violet" , PALETTERGB (238,130,238)},
1393 {"plum" , PALETTERGB (221,160,221)},
1394 {"orchid" , PALETTERGB (218,112,214)},
1395 {"medium orchid" , PALETTERGB (186, 85,211)},
1396 {"MediumOrchid" , PALETTERGB (186, 85,211)},
1397 {"dark orchid" , PALETTERGB (153, 50,204)},
1398 {"DarkOrchid" , PALETTERGB (153, 50,204)},
1399 {"dark violet" , PALETTERGB (148, 0,211)},
1400 {"DarkViolet" , PALETTERGB (148, 0,211)},
1401 {"blue violet" , PALETTERGB (138, 43,226)},
1402 {"BlueViolet" , PALETTERGB (138, 43,226)},
1403 {"purple" , PALETTERGB (160, 32,240)},
1404 {"medium purple" , PALETTERGB (147,112,219)},
1405 {"MediumPurple" , PALETTERGB (147,112,219)},
1406 {"thistle" , PALETTERGB (216,191,216)},
1407 {"gray0" , PALETTERGB ( 0, 0, 0)},
1408 {"grey0" , PALETTERGB ( 0, 0, 0)},
1409 {"dark grey" , PALETTERGB (169,169,169)},
1410 {"DarkGrey" , PALETTERGB (169,169,169)},
1411 {"dark gray" , PALETTERGB (169,169,169)},
1412 {"DarkGray" , PALETTERGB (169,169,169)},
1413 {"dark blue" , PALETTERGB ( 0, 0,139)},
1414 {"DarkBlue" , PALETTERGB ( 0, 0,139)},
1415 {"dark cyan" , PALETTERGB ( 0,139,139)},
1416 {"DarkCyan" , PALETTERGB ( 0,139,139)},
1417 {"dark magenta" , PALETTERGB (139, 0,139)},
1418 {"DarkMagenta" , PALETTERGB (139, 0,139)},
1419 {"dark red" , PALETTERGB (139, 0, 0)},
1420 {"DarkRed" , PALETTERGB (139, 0, 0)},
1421 {"light green" , PALETTERGB (144,238,144)},
1422 {"LightGreen" , PALETTERGB (144,238,144)},
1423 };
1424
1425 DEFUN ("w32-default-color-map", Fw32_default_color_map, Sw32_default_color_map,
1426 0, 0, 0, "Return the default color map.")
1427 ()
1428 {
1429 int i;
1430 colormap_t *pc = w32_color_map;
1431 Lisp_Object cmap;
1432
1433 BLOCK_INPUT;
1434
1435 cmap = Qnil;
1436
1437 for (i = 0; i < sizeof (w32_color_map) / sizeof (w32_color_map[0]);
1438 pc++, i++)
1439 cmap = Fcons (Fcons (build_string (pc->name),
1440 make_number (pc->colorref)),
1441 cmap);
1442
1443 UNBLOCK_INPUT;
1444
1445 return (cmap);
1446 }
1447
1448 Lisp_Object
1449 w32_to_x_color (rgb)
1450 Lisp_Object rgb;
1451 {
1452 Lisp_Object color;
1453
1454 CHECK_NUMBER (rgb, 0);
1455
1456 BLOCK_INPUT;
1457
1458 color = Frassq (rgb, Vw32_color_map);
1459
1460 UNBLOCK_INPUT;
1461
1462 if (!NILP (color))
1463 return (Fcar (color));
1464 else
1465 return Qnil;
1466 }
1467
1468 COLORREF
1469 w32_color_map_lookup (colorname)
1470 char *colorname;
1471 {
1472 Lisp_Object tail, ret = Qnil;
1473
1474 BLOCK_INPUT;
1475
1476 for (tail = Vw32_color_map; !NILP (tail); tail = Fcdr (tail))
1477 {
1478 register Lisp_Object elt, tem;
1479
1480 elt = Fcar (tail);
1481 if (!CONSP (elt)) continue;
1482
1483 tem = Fcar (elt);
1484
1485 if (lstrcmpi (XSTRING (tem)->data, colorname) == 0)
1486 {
1487 ret = XUINT (Fcdr (elt));
1488 break;
1489 }
1490
1491 QUIT;
1492 }
1493
1494
1495 UNBLOCK_INPUT;
1496
1497 return ret;
1498 }
1499
1500 COLORREF
1501 x_to_w32_color (colorname)
1502 char * colorname;
1503 {
1504 register Lisp_Object tail, ret = Qnil;
1505
1506 BLOCK_INPUT;
1507
1508 if (colorname[0] == '#')
1509 {
1510 /* Could be an old-style RGB Device specification. */
1511 char *color;
1512 int size;
1513 color = colorname + 1;
1514
1515 size = strlen(color);
1516 if (size == 3 || size == 6 || size == 9 || size == 12)
1517 {
1518 UINT colorval;
1519 int i, pos;
1520 pos = 0;
1521 size /= 3;
1522 colorval = 0;
1523
1524 for (i = 0; i < 3; i++)
1525 {
1526 char *end;
1527 char t;
1528 unsigned long value;
1529
1530 /* The check for 'x' in the following conditional takes into
1531 account the fact that strtol allows a "0x" in front of
1532 our numbers, and we don't. */
1533 if (!isxdigit(color[0]) || color[1] == 'x')
1534 break;
1535 t = color[size];
1536 color[size] = '\0';
1537 value = strtoul(color, &end, 16);
1538 color[size] = t;
1539 if (errno == ERANGE || end - color != size)
1540 break;
1541 switch (size)
1542 {
1543 case 1:
1544 value = value * 0x10;
1545 break;
1546 case 2:
1547 break;
1548 case 3:
1549 value /= 0x10;
1550 break;
1551 case 4:
1552 value /= 0x100;
1553 break;
1554 }
1555 colorval |= (value << pos);
1556 pos += 0x8;
1557 if (i == 2)
1558 {
1559 UNBLOCK_INPUT;
1560 return (colorval);
1561 }
1562 color = end;
1563 }
1564 }
1565 }
1566 else if (strnicmp(colorname, "rgb:", 4) == 0)
1567 {
1568 char *color;
1569 UINT colorval;
1570 int i, pos;
1571 pos = 0;
1572
1573 colorval = 0;
1574 color = colorname + 4;
1575 for (i = 0; i < 3; i++)
1576 {
1577 char *end;
1578 unsigned long value;
1579
1580 /* The check for 'x' in the following conditional takes into
1581 account the fact that strtol allows a "0x" in front of
1582 our numbers, and we don't. */
1583 if (!isxdigit(color[0]) || color[1] == 'x')
1584 break;
1585 value = strtoul(color, &end, 16);
1586 if (errno == ERANGE)
1587 break;
1588 switch (end - color)
1589 {
1590 case 1:
1591 value = value * 0x10 + value;
1592 break;
1593 case 2:
1594 break;
1595 case 3:
1596 value /= 0x10;
1597 break;
1598 case 4:
1599 value /= 0x100;
1600 break;
1601 default:
1602 value = ULONG_MAX;
1603 }
1604 if (value == ULONG_MAX)
1605 break;
1606 colorval |= (value << pos);
1607 pos += 0x8;
1608 if (i == 2)
1609 {
1610 if (*end != '\0')
1611 break;
1612 UNBLOCK_INPUT;
1613 return (colorval);
1614 }
1615 if (*end != '/')
1616 break;
1617 color = end + 1;
1618 }
1619 }
1620 else if (strnicmp(colorname, "rgbi:", 5) == 0)
1621 {
1622 /* This is an RGB Intensity specification. */
1623 char *color;
1624 UINT colorval;
1625 int i, pos;
1626 pos = 0;
1627
1628 colorval = 0;
1629 color = colorname + 5;
1630 for (i = 0; i < 3; i++)
1631 {
1632 char *end;
1633 double value;
1634 UINT val;
1635
1636 value = strtod(color, &end);
1637 if (errno == ERANGE)
1638 break;
1639 if (value < 0.0 || value > 1.0)
1640 break;
1641 val = (UINT)(0x100 * value);
1642 /* We used 0x100 instead of 0xFF to give an continuous
1643 range between 0.0 and 1.0 inclusive. The next statement
1644 fixes the 1.0 case. */
1645 if (val == 0x100)
1646 val = 0xFF;
1647 colorval |= (val << pos);
1648 pos += 0x8;
1649 if (i == 2)
1650 {
1651 if (*end != '\0')
1652 break;
1653 UNBLOCK_INPUT;
1654 return (colorval);
1655 }
1656 if (*end != '/')
1657 break;
1658 color = end + 1;
1659 }
1660 }
1661 /* I am not going to attempt to handle any of the CIE color schemes
1662 or TekHVC, since I don't know the algorithms for conversion to
1663 RGB. */
1664
1665 /* If we fail to lookup the color name in w32_color_map, then check the
1666 colorname to see if it can be crudely approximated: If the X color
1667 ends in a number (e.g., "darkseagreen2"), strip the number and
1668 return the result of looking up the base color name. */
1669 ret = w32_color_map_lookup (colorname);
1670 if (NILP (ret))
1671 {
1672 int len = strlen (colorname);
1673
1674 if (isdigit (colorname[len - 1]))
1675 {
1676 char *ptr, *approx = alloca (len);
1677
1678 strcpy (approx, colorname);
1679 ptr = &approx[len - 1];
1680 while (ptr > approx && isdigit (*ptr))
1681 *ptr-- = '\0';
1682
1683 ret = w32_color_map_lookup (approx);
1684 }
1685 }
1686
1687 UNBLOCK_INPUT;
1688 return ret;
1689 }
1690
1691
1692 void
1693 w32_regenerate_palette (FRAME_PTR f)
1694 {
1695 struct w32_palette_entry * list;
1696 LOGPALETTE * log_palette;
1697 HPALETTE new_palette;
1698 int i;
1699
1700 /* don't bother trying to create palette if not supported */
1701 if (! FRAME_W32_DISPLAY_INFO (f)->has_palette)
1702 return;
1703
1704 log_palette = (LOGPALETTE *)
1705 alloca (sizeof (LOGPALETTE) +
1706 FRAME_W32_DISPLAY_INFO (f)->num_colors * sizeof (PALETTEENTRY));
1707 log_palette->palVersion = 0x300;
1708 log_palette->palNumEntries = FRAME_W32_DISPLAY_INFO (f)->num_colors;
1709
1710 list = FRAME_W32_DISPLAY_INFO (f)->color_list;
1711 for (i = 0;
1712 i < FRAME_W32_DISPLAY_INFO (f)->num_colors;
1713 i++, list = list->next)
1714 log_palette->palPalEntry[i] = list->entry;
1715
1716 new_palette = CreatePalette (log_palette);
1717
1718 enter_crit ();
1719
1720 if (FRAME_W32_DISPLAY_INFO (f)->palette)
1721 DeleteObject (FRAME_W32_DISPLAY_INFO (f)->palette);
1722 FRAME_W32_DISPLAY_INFO (f)->palette = new_palette;
1723
1724 /* Realize display palette and garbage all frames. */
1725 release_frame_dc (f, get_frame_dc (f));
1726
1727 leave_crit ();
1728 }
1729
1730 #define W32_COLOR(pe) RGB (pe.peRed, pe.peGreen, pe.peBlue)
1731 #define SET_W32_COLOR(pe, color) \
1732 do \
1733 { \
1734 pe.peRed = GetRValue (color); \
1735 pe.peGreen = GetGValue (color); \
1736 pe.peBlue = GetBValue (color); \
1737 pe.peFlags = 0; \
1738 } while (0)
1739
1740 #if 0
1741 /* Keep these around in case we ever want to track color usage. */
1742 void
1743 w32_map_color (FRAME_PTR f, COLORREF color)
1744 {
1745 struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
1746
1747 if (NILP (Vw32_enable_palette))
1748 return;
1749
1750 /* check if color is already mapped */
1751 while (list)
1752 {
1753 if (W32_COLOR (list->entry) == color)
1754 {
1755 ++list->refcount;
1756 return;
1757 }
1758 list = list->next;
1759 }
1760
1761 /* not already mapped, so add to list and recreate Windows palette */
1762 list = (struct w32_palette_entry *)
1763 xmalloc (sizeof (struct w32_palette_entry));
1764 SET_W32_COLOR (list->entry, color);
1765 list->refcount = 1;
1766 list->next = FRAME_W32_DISPLAY_INFO (f)->color_list;
1767 FRAME_W32_DISPLAY_INFO (f)->color_list = list;
1768 FRAME_W32_DISPLAY_INFO (f)->num_colors++;
1769
1770 /* set flag that palette must be regenerated */
1771 FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
1772 }
1773
1774 void
1775 w32_unmap_color (FRAME_PTR f, COLORREF color)
1776 {
1777 struct w32_palette_entry * list = FRAME_W32_DISPLAY_INFO (f)->color_list;
1778 struct w32_palette_entry **prev = &FRAME_W32_DISPLAY_INFO (f)->color_list;
1779
1780 if (NILP (Vw32_enable_palette))
1781 return;
1782
1783 /* check if color is already mapped */
1784 while (list)
1785 {
1786 if (W32_COLOR (list->entry) == color)
1787 {
1788 if (--list->refcount == 0)
1789 {
1790 *prev = list->next;
1791 xfree (list);
1792 FRAME_W32_DISPLAY_INFO (f)->num_colors--;
1793 break;
1794 }
1795 else
1796 return;
1797 }
1798 prev = &list->next;
1799 list = list->next;
1800 }
1801
1802 /* set flag that palette must be regenerated */
1803 FRAME_W32_DISPLAY_INFO (f)->regen_palette = TRUE;
1804 }
1805 #endif
1806
1807
1808 /* Gamma-correct COLOR on frame F. */
1809
1810 void
1811 gamma_correct (f, color)
1812 struct frame *f;
1813 COLORREF *color;
1814 {
1815 if (f->gamma)
1816 {
1817 *color = PALETTERGB (
1818 pow (GetRValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
1819 pow (GetGValue (*color) / 255.0, f->gamma) * 255.0 + 0.5,
1820 pow (GetBValue (*color) / 255.0, f->gamma) * 255.0 + 0.5);
1821 }
1822 }
1823
1824
1825 /* Decide if color named COLOR is valid for the display associated with
1826 the selected frame; if so, return the rgb values in COLOR_DEF.
1827 If ALLOC is nonzero, allocate a new colormap cell. */
1828
1829 int
1830 w32_defined_color (f, color, color_def, alloc)
1831 FRAME_PTR f;
1832 char *color;
1833 XColor *color_def;
1834 int alloc;
1835 {
1836 register Lisp_Object tem;
1837 COLORREF w32_color_ref;
1838
1839 tem = x_to_w32_color (color);
1840
1841 if (!NILP (tem))
1842 {
1843 if (f)
1844 {
1845 /* Apply gamma correction. */
1846 w32_color_ref = XUINT (tem);
1847 gamma_correct (f, &w32_color_ref);
1848 XSETINT (tem, w32_color_ref);
1849 }
1850
1851 /* Map this color to the palette if it is enabled. */
1852 if (!NILP (Vw32_enable_palette))
1853 {
1854 struct w32_palette_entry * entry =
1855 one_w32_display_info.color_list;
1856 struct w32_palette_entry ** prev =
1857 &one_w32_display_info.color_list;
1858
1859 /* check if color is already mapped */
1860 while (entry)
1861 {
1862 if (W32_COLOR (entry->entry) == XUINT (tem))
1863 break;
1864 prev = &entry->next;
1865 entry = entry->next;
1866 }
1867
1868 if (entry == NULL && alloc)
1869 {
1870 /* not already mapped, so add to list */
1871 entry = (struct w32_palette_entry *)
1872 xmalloc (sizeof (struct w32_palette_entry));
1873 SET_W32_COLOR (entry->entry, XUINT (tem));
1874 entry->next = NULL;
1875 *prev = entry;
1876 one_w32_display_info.num_colors++;
1877
1878 /* set flag that palette must be regenerated */
1879 one_w32_display_info.regen_palette = TRUE;
1880 }
1881 }
1882 /* Ensure COLORREF value is snapped to nearest color in (default)
1883 palette by simulating the PALETTERGB macro. This works whether
1884 or not the display device has a palette. */
1885 w32_color_ref = XUINT (tem) | 0x2000000;
1886
1887 color_def->pixel = w32_color_ref;
1888 color_def->red = GetRValue (w32_color_ref);
1889 color_def->green = GetGValue (w32_color_ref);
1890 color_def->blue = GetBValue (w32_color_ref);
1891
1892 return 1;
1893 }
1894 else
1895 {
1896 return 0;
1897 }
1898 }
1899
1900 /* Given a string ARG naming a color, compute a pixel value from it
1901 suitable for screen F.
1902 If F is not a color screen, return DEF (default) regardless of what
1903 ARG says. */
1904
1905 int
1906 x_decode_color (f, arg, def)
1907 FRAME_PTR f;
1908 Lisp_Object arg;
1909 int def;
1910 {
1911 XColor cdef;
1912
1913 CHECK_STRING (arg, 0);
1914
1915 if (strcmp (XSTRING (arg)->data, "black") == 0)
1916 return BLACK_PIX_DEFAULT (f);
1917 else if (strcmp (XSTRING (arg)->data, "white") == 0)
1918 return WHITE_PIX_DEFAULT (f);
1919
1920 if ((FRAME_W32_DISPLAY_INFO (f)->n_planes * FRAME_W32_DISPLAY_INFO (f)->n_cbits) == 1)
1921 return def;
1922
1923 /* w32_defined_color is responsible for coping with failures
1924 by looking for a near-miss. */
1925 if (w32_defined_color (f, XSTRING (arg)->data, &cdef, 1))
1926 return cdef.pixel;
1927
1928 /* defined_color failed; return an ultimate default. */
1929 return def;
1930 }
1931 \f
1932 /* Change the `line-spacing' frame parameter of frame F. OLD_VALUE is
1933 the previous value of that parameter, NEW_VALUE is the new value. */
1934
1935 static void
1936 x_set_line_spacing (f, new_value, old_value)
1937 struct frame *f;
1938 Lisp_Object new_value, old_value;
1939 {
1940 if (NILP (new_value))
1941 f->extra_line_spacing = 0;
1942 else if (NATNUMP (new_value))
1943 f->extra_line_spacing = XFASTINT (new_value);
1944 else
1945 Fsignal (Qerror, Fcons (build_string ("Invalid line-spacing"),
1946 Fcons (new_value, Qnil)));
1947 if (FRAME_VISIBLE_P (f))
1948 redraw_frame (f);
1949 }
1950
1951
1952 /* Change the `screen-gamma' frame parameter of frame F. OLD_VALUE is
1953 the previous value of that parameter, NEW_VALUE is the new value. */
1954
1955 static void
1956 x_set_screen_gamma (f, new_value, old_value)
1957 struct frame *f;
1958 Lisp_Object new_value, old_value;
1959 {
1960 if (NILP (new_value))
1961 f->gamma = 0;
1962 else if (NUMBERP (new_value) && XFLOATINT (new_value) > 0)
1963 /* The value 0.4545 is the normal viewing gamma. */
1964 f->gamma = 1.0 / (0.4545 * XFLOATINT (new_value));
1965 else
1966 Fsignal (Qerror, Fcons (build_string ("Invalid screen-gamma"),
1967 Fcons (new_value, Qnil)));
1968
1969 clear_face_cache (0);
1970 }
1971
1972
1973 /* Functions called only from `x_set_frame_param'
1974 to set individual parameters.
1975
1976 If FRAME_W32_WINDOW (f) is 0,
1977 the frame is being created and its window does not exist yet.
1978 In that case, just record the parameter's new value
1979 in the standard place; do not attempt to change the window. */
1980
1981 void
1982 x_set_foreground_color (f, arg, oldval)
1983 struct frame *f;
1984 Lisp_Object arg, oldval;
1985 {
1986 FRAME_FOREGROUND_PIXEL (f)
1987 = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1988
1989 if (FRAME_W32_WINDOW (f) != 0)
1990 {
1991 update_face_from_frame_parameter (f, Qforeground_color, arg);
1992 if (FRAME_VISIBLE_P (f))
1993 redraw_frame (f);
1994 }
1995 }
1996
1997 void
1998 x_set_background_color (f, arg, oldval)
1999 struct frame *f;
2000 Lisp_Object arg, oldval;
2001 {
2002 FRAME_BACKGROUND_PIXEL (f)
2003 = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
2004
2005 if (FRAME_W32_WINDOW (f) != 0)
2006 {
2007 SetWindowLong (FRAME_W32_WINDOW (f), WND_BACKGROUND_INDEX,
2008 FRAME_BACKGROUND_PIXEL (f));
2009
2010 update_face_from_frame_parameter (f, Qbackground_color, arg);
2011
2012 if (FRAME_VISIBLE_P (f))
2013 redraw_frame (f);
2014 }
2015 }
2016
2017 void
2018 x_set_mouse_color (f, arg, oldval)
2019 struct frame *f;
2020 Lisp_Object arg, oldval;
2021 {
2022
2023 Cursor cursor, nontext_cursor, mode_cursor, cross_cursor;
2024 int count;
2025 int mask_color;
2026
2027 if (!EQ (Qnil, arg))
2028 f->output_data.w32->mouse_pixel
2029 = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
2030 mask_color = FRAME_BACKGROUND_PIXEL (f);
2031
2032 /* Don't let pointers be invisible. */
2033 if (mask_color == f->output_data.w32->mouse_pixel
2034 && mask_color == FRAME_BACKGROUND_PIXEL (f))
2035 f->output_data.w32->mouse_pixel = FRAME_FOREGROUND_PIXEL (f);
2036
2037 #if 0 /* TODO : cursor changes */
2038 BLOCK_INPUT;
2039
2040 /* It's not okay to crash if the user selects a screwy cursor. */
2041 count = x_catch_errors (FRAME_W32_DISPLAY (f));
2042
2043 if (!EQ (Qnil, Vx_pointer_shape))
2044 {
2045 CHECK_NUMBER (Vx_pointer_shape, 0);
2046 cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XINT (Vx_pointer_shape));
2047 }
2048 else
2049 cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
2050 x_check_errors (FRAME_W32_DISPLAY (f), "bad text pointer cursor: %s");
2051
2052 if (!EQ (Qnil, Vx_nontext_pointer_shape))
2053 {
2054 CHECK_NUMBER (Vx_nontext_pointer_shape, 0);
2055 nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
2056 XINT (Vx_nontext_pointer_shape));
2057 }
2058 else
2059 nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_left_ptr);
2060 x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
2061
2062 if (!EQ (Qnil, Vx_busy_pointer_shape))
2063 {
2064 CHECK_NUMBER (Vx_busy_pointer_shape, 0);
2065 busy_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
2066 XINT (Vx_busy_pointer_shape));
2067 }
2068 else
2069 busy_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_watch);
2070 x_check_errors (FRAME_W32_DISPLAY (f), "bad busy pointer cursor: %s");
2071
2072 x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
2073 if (!EQ (Qnil, Vx_mode_pointer_shape))
2074 {
2075 CHECK_NUMBER (Vx_mode_pointer_shape, 0);
2076 mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
2077 XINT (Vx_mode_pointer_shape));
2078 }
2079 else
2080 mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
2081 x_check_errors (FRAME_W32_DISPLAY (f), "bad modeline pointer cursor: %s");
2082
2083 if (!EQ (Qnil, Vx_sensitive_text_pointer_shape))
2084 {
2085 CHECK_NUMBER (Vx_sensitive_text_pointer_shape, 0);
2086 cross_cursor
2087 = XCreateFontCursor (FRAME_W32_DISPLAY (f),
2088 XINT (Vx_sensitive_text_pointer_shape));
2089 }
2090 else
2091 cross_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_crosshair);
2092
2093 /* Check and report errors with the above calls. */
2094 x_check_errors (FRAME_W32_DISPLAY (f), "can't set cursor shape: %s");
2095 x_uncatch_errors (FRAME_W32_DISPLAY (f), count);
2096
2097 {
2098 XColor fore_color, back_color;
2099
2100 fore_color.pixel = f->output_data.w32->mouse_pixel;
2101 back_color.pixel = mask_color;
2102 XQueryColor (FRAME_W32_DISPLAY (f),
2103 DefaultColormap (FRAME_W32_DISPLAY (f),
2104 DefaultScreen (FRAME_W32_DISPLAY (f))),
2105 &fore_color);
2106 XQueryColor (FRAME_W32_DISPLAY (f),
2107 DefaultColormap (FRAME_W32_DISPLAY (f),
2108 DefaultScreen (FRAME_W32_DISPLAY (f))),
2109 &back_color);
2110 XRecolorCursor (FRAME_W32_DISPLAY (f), cursor,
2111 &fore_color, &back_color);
2112 XRecolorCursor (FRAME_W32_DISPLAY (f), nontext_cursor,
2113 &fore_color, &back_color);
2114 XRecolorCursor (FRAME_W32_DISPLAY (f), mode_cursor,
2115 &fore_color, &back_color);
2116 XRecolorCursor (FRAME_W32_DISPLAY (f), cross_cursor,
2117 &fore_color, &back_color);
2118 XRecolorCursor (FRAME_W32_DISPLAY (f), busy_cursor,
2119 &fore_color, &back_color);
2120 }
2121
2122 if (FRAME_W32_WINDOW (f) != 0)
2123 XDefineCursor (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), cursor);
2124
2125 if (cursor != f->output_data.w32->text_cursor && f->output_data.w32->text_cursor != 0)
2126 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->text_cursor);
2127 f->output_data.w32->text_cursor = cursor;
2128
2129 if (nontext_cursor != f->output_data.w32->nontext_cursor
2130 && f->output_data.w32->nontext_cursor != 0)
2131 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->nontext_cursor);
2132 f->output_data.w32->nontext_cursor = nontext_cursor;
2133
2134 if (busy_cursor != f->output_data.w32->busy_cursor
2135 && f->output_data.w32->busy_cursor != 0)
2136 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->busy_cursor);
2137 f->output_data.w32->busy_cursor = busy_cursor;
2138
2139 if (mode_cursor != f->output_data.w32->modeline_cursor
2140 && f->output_data.w32->modeline_cursor != 0)
2141 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->modeline_cursor);
2142 f->output_data.w32->modeline_cursor = mode_cursor;
2143
2144 if (cross_cursor != f->output_data.w32->cross_cursor
2145 && f->output_data.w32->cross_cursor != 0)
2146 XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->cross_cursor);
2147 f->output_data.w32->cross_cursor = cross_cursor;
2148
2149 XFlush (FRAME_W32_DISPLAY (f));
2150 UNBLOCK_INPUT;
2151
2152 update_face_from_frame_parameter (f, Qmouse_color, arg);
2153 #endif /* TODO */
2154 }
2155
2156 void
2157 x_set_cursor_color (f, arg, oldval)
2158 struct frame *f;
2159 Lisp_Object arg, oldval;
2160 {
2161 unsigned long fore_pixel;
2162
2163 if (!NILP (Vx_cursor_fore_pixel))
2164 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
2165 WHITE_PIX_DEFAULT (f));
2166 else
2167 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
2168 f->output_data.w32->cursor_pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
2169
2170 /* Make sure that the cursor color differs from the background color. */
2171 if (f->output_data.w32->cursor_pixel == FRAME_BACKGROUND_PIXEL (f))
2172 {
2173 f->output_data.w32->cursor_pixel = f->output_data.w32->mouse_pixel;
2174 if (f->output_data.w32->cursor_pixel == fore_pixel)
2175 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
2176 }
2177 FRAME_FOREGROUND_PIXEL (f) = fore_pixel;
2178
2179 if (FRAME_W32_WINDOW (f) != 0)
2180 {
2181 if (FRAME_VISIBLE_P (f))
2182 {
2183 x_display_cursor (f, 0);
2184 x_display_cursor (f, 1);
2185 }
2186 }
2187
2188 update_face_from_frame_parameter (f, Qcursor_color, arg);
2189 }
2190
2191 /* Set the border-color of frame F to pixel value PIX.
2192 Note that this does not fully take effect if done before
2193 F has an window. */
2194 void
2195 x_set_border_pixel (f, pix)
2196 struct frame *f;
2197 int pix;
2198 {
2199 f->output_data.w32->border_pixel = pix;
2200
2201 if (FRAME_W32_WINDOW (f) != 0 && f->output_data.w32->border_width > 0)
2202 {
2203 if (FRAME_VISIBLE_P (f))
2204 redraw_frame (f);
2205 }
2206 }
2207
2208 /* Set the border-color of frame F to value described by ARG.
2209 ARG can be a string naming a color.
2210 The border-color is used for the border that is drawn by the server.
2211 Note that this does not fully take effect if done before
2212 F has a window; it must be redone when the window is created. */
2213
2214 void
2215 x_set_border_color (f, arg, oldval)
2216 struct frame *f;
2217 Lisp_Object arg, oldval;
2218 {
2219 int pix;
2220
2221 CHECK_STRING (arg, 0);
2222 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
2223 x_set_border_pixel (f, pix);
2224 update_face_from_frame_parameter (f, Qborder_color, arg);
2225 }
2226
2227 /* Value is the internal representation of the specified cursor type
2228 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
2229 of the bar cursor. */
2230
2231 enum text_cursor_kinds
2232 x_specified_cursor_type (arg, width)
2233 Lisp_Object arg;
2234 int *width;
2235 {
2236 enum text_cursor_kinds type;
2237
2238 if (EQ (arg, Qbar))
2239 {
2240 type = BAR_CURSOR;
2241 *width = 2;
2242 }
2243 else if (CONSP (arg)
2244 && EQ (XCAR (arg), Qbar)
2245 && INTEGERP (XCDR (arg))
2246 && XINT (XCDR (arg)) >= 0)
2247 {
2248 type = BAR_CURSOR;
2249 *width = XINT (XCDR (arg));
2250 }
2251 else if (NILP (arg))
2252 type = NO_CURSOR;
2253 else
2254 /* Treat anything unknown as "box cursor".
2255 It was bad to signal an error; people have trouble fixing
2256 .Xdefaults with Emacs, when it has something bad in it. */
2257 type = FILLED_BOX_CURSOR;
2258
2259 return type;
2260 }
2261
2262 void
2263 x_set_cursor_type (f, arg, oldval)
2264 FRAME_PTR f;
2265 Lisp_Object arg, oldval;
2266 {
2267 int width;
2268
2269 FRAME_DESIRED_CURSOR (f) = x_specified_cursor_type (arg, &width);
2270 f->output_data.w32->cursor_width = width;
2271
2272 /* Make sure the cursor gets redrawn. This is overkill, but how
2273 often do people change cursor types? */
2274 update_mode_lines++;
2275 }
2276 \f
2277 void
2278 x_set_icon_type (f, arg, oldval)
2279 struct frame *f;
2280 Lisp_Object arg, oldval;
2281 {
2282 int result;
2283
2284 if (NILP (arg) && NILP (oldval))
2285 return;
2286
2287 if (STRINGP (arg) && STRINGP (oldval)
2288 && EQ (Fstring_equal (oldval, arg), Qt))
2289 return;
2290
2291 if (SYMBOLP (arg) && SYMBOLP (oldval) && EQ (arg, oldval))
2292 return;
2293
2294 BLOCK_INPUT;
2295
2296 result = x_bitmap_icon (f, arg);
2297 if (result)
2298 {
2299 UNBLOCK_INPUT;
2300 error ("No icon window available");
2301 }
2302
2303 UNBLOCK_INPUT;
2304 }
2305
2306 /* Return non-nil if frame F wants a bitmap icon. */
2307
2308 Lisp_Object
2309 x_icon_type (f)
2310 FRAME_PTR f;
2311 {
2312 Lisp_Object tem;
2313
2314 tem = assq_no_quit (Qicon_type, f->param_alist);
2315 if (CONSP (tem))
2316 return XCDR (tem);
2317 else
2318 return Qnil;
2319 }
2320
2321 void
2322 x_set_icon_name (f, arg, oldval)
2323 struct frame *f;
2324 Lisp_Object arg, oldval;
2325 {
2326 int result;
2327
2328 if (STRINGP (arg))
2329 {
2330 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
2331 return;
2332 }
2333 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
2334 return;
2335
2336 f->icon_name = arg;
2337
2338 #if 0
2339 if (f->output_data.w32->icon_bitmap != 0)
2340 return;
2341
2342 BLOCK_INPUT;
2343
2344 result = x_text_icon (f,
2345 (char *) XSTRING ((!NILP (f->icon_name)
2346 ? f->icon_name
2347 : !NILP (f->title)
2348 ? f->title
2349 : f->name))->data);
2350
2351 if (result)
2352 {
2353 UNBLOCK_INPUT;
2354 error ("No icon window available");
2355 }
2356
2357 /* If the window was unmapped (and its icon was mapped),
2358 the new icon is not mapped, so map the window in its stead. */
2359 if (FRAME_VISIBLE_P (f))
2360 {
2361 #ifdef USE_X_TOOLKIT
2362 XtPopup (f->output_data.w32->widget, XtGrabNone);
2363 #endif
2364 XMapWindow (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f));
2365 }
2366
2367 XFlush (FRAME_W32_DISPLAY (f));
2368 UNBLOCK_INPUT;
2369 #endif
2370 }
2371
2372 extern Lisp_Object x_new_font ();
2373 extern Lisp_Object x_new_fontset();
2374
2375 void
2376 x_set_font (f, arg, oldval)
2377 struct frame *f;
2378 Lisp_Object arg, oldval;
2379 {
2380 Lisp_Object result;
2381 Lisp_Object fontset_name;
2382 Lisp_Object frame;
2383
2384 CHECK_STRING (arg, 1);
2385
2386 fontset_name = Fquery_fontset (arg, Qnil);
2387
2388 BLOCK_INPUT;
2389 result = (STRINGP (fontset_name)
2390 ? x_new_fontset (f, XSTRING (fontset_name)->data)
2391 : x_new_font (f, XSTRING (arg)->data));
2392 UNBLOCK_INPUT;
2393
2394 if (EQ (result, Qnil))
2395 error ("Font `%s' is not defined", XSTRING (arg)->data);
2396 else if (EQ (result, Qt))
2397 error ("The characters of the given font have varying widths");
2398 else if (STRINGP (result))
2399 {
2400 store_frame_param (f, Qfont, result);
2401 recompute_basic_faces (f);
2402 }
2403 else
2404 abort ();
2405
2406 do_pending_window_change (0);
2407
2408 /* Don't call `face-set-after-frame-default' when faces haven't been
2409 initialized yet. This is the case when called from
2410 Fx_create_frame. In that case, the X widget or window doesn't
2411 exist either, and we can end up in x_report_frame_params with a
2412 null widget which gives a segfault. */
2413 if (FRAME_FACE_CACHE (f))
2414 {
2415 XSETFRAME (frame, f);
2416 call1 (Qface_set_after_frame_default, frame);
2417 }
2418 }
2419
2420 void
2421 x_set_border_width (f, arg, oldval)
2422 struct frame *f;
2423 Lisp_Object arg, oldval;
2424 {
2425 CHECK_NUMBER (arg, 0);
2426
2427 if (XINT (arg) == f->output_data.w32->border_width)
2428 return;
2429
2430 if (FRAME_W32_WINDOW (f) != 0)
2431 error ("Cannot change the border width of a window");
2432
2433 f->output_data.w32->border_width = XINT (arg);
2434 }
2435
2436 void
2437 x_set_internal_border_width (f, arg, oldval)
2438 struct frame *f;
2439 Lisp_Object arg, oldval;
2440 {
2441 int old = f->output_data.w32->internal_border_width;
2442
2443 CHECK_NUMBER (arg, 0);
2444 f->output_data.w32->internal_border_width = XINT (arg);
2445 if (f->output_data.w32->internal_border_width < 0)
2446 f->output_data.w32->internal_border_width = 0;
2447
2448 if (f->output_data.w32->internal_border_width == old)
2449 return;
2450
2451 if (FRAME_W32_WINDOW (f) != 0)
2452 {
2453 x_set_window_size (f, 0, f->width, f->height);
2454 SET_FRAME_GARBAGED (f);
2455 do_pending_window_change (0);
2456 }
2457 }
2458
2459 void
2460 x_set_visibility (f, value, oldval)
2461 struct frame *f;
2462 Lisp_Object value, oldval;
2463 {
2464 Lisp_Object frame;
2465 XSETFRAME (frame, f);
2466
2467 if (NILP (value))
2468 Fmake_frame_invisible (frame, Qt);
2469 else if (EQ (value, Qicon))
2470 Ficonify_frame (frame);
2471 else
2472 Fmake_frame_visible (frame);
2473 }
2474
2475 \f
2476 /* Change window heights in windows rooted in WINDOW by N lines. */
2477
2478 static void
2479 x_change_window_heights (window, n)
2480 Lisp_Object window;
2481 int n;
2482 {
2483 struct window *w = XWINDOW (window);
2484
2485 XSETFASTINT (w->top, XFASTINT (w->top) + n);
2486 XSETFASTINT (w->height, XFASTINT (w->height) - n);
2487
2488 if (INTEGERP (w->orig_top))
2489 XSETFASTINT (w->orig_top, XFASTINT (w->orig_top) + n);
2490 if (INTEGERP (w->orig_height))
2491 XSETFASTINT (w->orig_height, XFASTINT (w->orig_height) - n);
2492
2493 /* Handle just the top child in a vertical split. */
2494 if (!NILP (w->vchild))
2495 x_change_window_heights (w->vchild, n);
2496
2497 /* Adjust all children in a horizontal split. */
2498 for (window = w->hchild; !NILP (window); window = w->next)
2499 {
2500 w = XWINDOW (window);
2501 x_change_window_heights (window, n);
2502 }
2503 }
2504
2505 void
2506 x_set_menu_bar_lines (f, value, oldval)
2507 struct frame *f;
2508 Lisp_Object value, oldval;
2509 {
2510 int nlines;
2511 int olines = FRAME_MENU_BAR_LINES (f);
2512
2513 /* Right now, menu bars don't work properly in minibuf-only frames;
2514 most of the commands try to apply themselves to the minibuffer
2515 frame itself, and get an error because you can't switch buffers
2516 in or split the minibuffer window. */
2517 if (FRAME_MINIBUF_ONLY_P (f))
2518 return;
2519
2520 if (INTEGERP (value))
2521 nlines = XINT (value);
2522 else
2523 nlines = 0;
2524
2525 FRAME_MENU_BAR_LINES (f) = 0;
2526 if (nlines)
2527 FRAME_EXTERNAL_MENU_BAR (f) = 1;
2528 else
2529 {
2530 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
2531 free_frame_menubar (f);
2532 FRAME_EXTERNAL_MENU_BAR (f) = 0;
2533
2534 /* Adjust the frame size so that the client (text) dimensions
2535 remain the same. This depends on FRAME_EXTERNAL_MENU_BAR being
2536 set correctly. */
2537 x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
2538 do_pending_window_change (0);
2539 }
2540 adjust_glyphs (f);
2541 }
2542
2543
2544 /* Set the number of lines used for the tool bar of frame F to VALUE.
2545 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
2546 is the old number of tool bar lines. This function changes the
2547 height of all windows on frame F to match the new tool bar height.
2548 The frame's height doesn't change. */
2549
2550 void
2551 x_set_tool_bar_lines (f, value, oldval)
2552 struct frame *f;
2553 Lisp_Object value, oldval;
2554 {
2555 int delta, nlines, root_height;
2556 Lisp_Object root_window;
2557
2558 /* Use VALUE only if an integer >= 0. */
2559 if (INTEGERP (value) && XINT (value) >= 0)
2560 nlines = XFASTINT (value);
2561 else
2562 nlines = 0;
2563
2564 /* Make sure we redisplay all windows in this frame. */
2565 ++windows_or_buffers_changed;
2566
2567 delta = nlines - FRAME_TOOL_BAR_LINES (f);
2568
2569 /* Don't resize the tool-bar to more than we have room for. */
2570 root_window = FRAME_ROOT_WINDOW (f);
2571 root_height = XINT (XWINDOW (root_window)->height);
2572 if (root_height - delta < 1)
2573 {
2574 delta = root_height - 1;
2575 nlines = FRAME_TOOL_BAR_LINES (f) + delta;
2576 }
2577
2578 FRAME_TOOL_BAR_LINES (f) = nlines;
2579 x_change_window_heights (root_window, delta);
2580 adjust_glyphs (f);
2581
2582 /* We also have to make sure that the internal border at the top of
2583 the frame, below the menu bar or tool bar, is redrawn when the
2584 tool bar disappears. This is so because the internal border is
2585 below the tool bar if one is displayed, but is below the menu bar
2586 if there isn't a tool bar. The tool bar draws into the area
2587 below the menu bar. */
2588 if (FRAME_W32_WINDOW (f) && FRAME_TOOL_BAR_LINES (f) == 0)
2589 {
2590 updating_frame = f;
2591 clear_frame ();
2592 clear_current_matrices (f);
2593 updating_frame = NULL;
2594 }
2595
2596 /* If the tool bar gets smaller, the internal border below it
2597 has to be cleared. It was formerly part of the display
2598 of the larger tool bar, and updating windows won't clear it. */
2599 if (delta < 0)
2600 {
2601 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
2602 int width = PIXEL_WIDTH (f);
2603 int y = nlines * CANON_Y_UNIT (f);
2604
2605 BLOCK_INPUT;
2606 {
2607 HDC hdc = get_frame_dc (f);
2608 w32_clear_area (f, hdc, 0, y, width, height);
2609 release_frame_dc (f, hdc);
2610 }
2611 UNBLOCK_INPUT;
2612 }
2613 }
2614
2615
2616 /* Change the name of frame F to NAME. If NAME is nil, set F's name to
2617 w32_id_name.
2618
2619 If EXPLICIT is non-zero, that indicates that lisp code is setting the
2620 name; if NAME is a string, set F's name to NAME and set
2621 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
2622
2623 If EXPLICIT is zero, that indicates that Emacs redisplay code is
2624 suggesting a new name, which lisp code should override; if
2625 F->explicit_name is set, ignore the new name; otherwise, set it. */
2626
2627 void
2628 x_set_name (f, name, explicit)
2629 struct frame *f;
2630 Lisp_Object name;
2631 int explicit;
2632 {
2633 /* Make sure that requests from lisp code override requests from
2634 Emacs redisplay code. */
2635 if (explicit)
2636 {
2637 /* If we're switching from explicit to implicit, we had better
2638 update the mode lines and thereby update the title. */
2639 if (f->explicit_name && NILP (name))
2640 update_mode_lines = 1;
2641
2642 f->explicit_name = ! NILP (name);
2643 }
2644 else if (f->explicit_name)
2645 return;
2646
2647 /* If NAME is nil, set the name to the w32_id_name. */
2648 if (NILP (name))
2649 {
2650 /* Check for no change needed in this very common case
2651 before we do any consing. */
2652 if (!strcmp (FRAME_W32_DISPLAY_INFO (f)->w32_id_name,
2653 XSTRING (f->name)->data))
2654 return;
2655 name = build_string (FRAME_W32_DISPLAY_INFO (f)->w32_id_name);
2656 }
2657 else
2658 CHECK_STRING (name, 0);
2659
2660 /* Don't change the name if it's already NAME. */
2661 if (! NILP (Fstring_equal (name, f->name)))
2662 return;
2663
2664 f->name = name;
2665
2666 /* For setting the frame title, the title parameter should override
2667 the name parameter. */
2668 if (! NILP (f->title))
2669 name = f->title;
2670
2671 if (FRAME_W32_WINDOW (f))
2672 {
2673 if (STRING_MULTIBYTE (name))
2674 name = ENCODE_SYSTEM (name);
2675
2676 BLOCK_INPUT;
2677 SetWindowText(FRAME_W32_WINDOW (f), XSTRING (name)->data);
2678 UNBLOCK_INPUT;
2679 }
2680 }
2681
2682 /* This function should be called when the user's lisp code has
2683 specified a name for the frame; the name will override any set by the
2684 redisplay code. */
2685 void
2686 x_explicitly_set_name (f, arg, oldval)
2687 FRAME_PTR f;
2688 Lisp_Object arg, oldval;
2689 {
2690 x_set_name (f, arg, 1);
2691 }
2692
2693 /* This function should be called by Emacs redisplay code to set the
2694 name; names set this way will never override names set by the user's
2695 lisp code. */
2696 void
2697 x_implicitly_set_name (f, arg, oldval)
2698 FRAME_PTR f;
2699 Lisp_Object arg, oldval;
2700 {
2701 x_set_name (f, arg, 0);
2702 }
2703 \f
2704 /* Change the title of frame F to NAME.
2705 If NAME is nil, use the frame name as the title.
2706
2707 If EXPLICIT is non-zero, that indicates that lisp code is setting the
2708 name; if NAME is a string, set F's name to NAME and set
2709 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
2710
2711 If EXPLICIT is zero, that indicates that Emacs redisplay code is
2712 suggesting a new name, which lisp code should override; if
2713 F->explicit_name is set, ignore the new name; otherwise, set it. */
2714
2715 void
2716 x_set_title (f, name, old_name)
2717 struct frame *f;
2718 Lisp_Object name, old_name;
2719 {
2720 /* Don't change the title if it's already NAME. */
2721 if (EQ (name, f->title))
2722 return;
2723
2724 update_mode_lines = 1;
2725
2726 f->title = name;
2727
2728 if (NILP (name))
2729 name = f->name;
2730
2731 if (FRAME_W32_WINDOW (f))
2732 {
2733 if (STRING_MULTIBYTE (name))
2734 name = ENCODE_SYSTEM (name);
2735
2736 BLOCK_INPUT;
2737 SetWindowText(FRAME_W32_WINDOW (f), XSTRING (name)->data);
2738 UNBLOCK_INPUT;
2739 }
2740 }
2741 \f
2742 void
2743 x_set_autoraise (f, arg, oldval)
2744 struct frame *f;
2745 Lisp_Object arg, oldval;
2746 {
2747 f->auto_raise = !EQ (Qnil, arg);
2748 }
2749
2750 void
2751 x_set_autolower (f, arg, oldval)
2752 struct frame *f;
2753 Lisp_Object arg, oldval;
2754 {
2755 f->auto_lower = !EQ (Qnil, arg);
2756 }
2757
2758 void
2759 x_set_unsplittable (f, arg, oldval)
2760 struct frame *f;
2761 Lisp_Object arg, oldval;
2762 {
2763 f->no_split = !NILP (arg);
2764 }
2765
2766 void
2767 x_set_vertical_scroll_bars (f, arg, oldval)
2768 struct frame *f;
2769 Lisp_Object arg, oldval;
2770 {
2771 if ((EQ (arg, Qleft) && FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
2772 || (EQ (arg, Qright) && FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f))
2773 || (NILP (arg) && FRAME_HAS_VERTICAL_SCROLL_BARS (f))
2774 || (!NILP (arg) && ! FRAME_HAS_VERTICAL_SCROLL_BARS (f)))
2775 {
2776 FRAME_VERTICAL_SCROLL_BAR_TYPE (f) = NILP (arg) ?
2777 vertical_scroll_bar_none :
2778 /* Put scroll bars on the right by default, as is conventional
2779 on MS-Windows. */
2780 EQ (Qleft, arg)
2781 ? vertical_scroll_bar_left
2782 : vertical_scroll_bar_right;
2783
2784 /* We set this parameter before creating the window for the
2785 frame, so we can get the geometry right from the start.
2786 However, if the window hasn't been created yet, we shouldn't
2787 call x_set_window_size. */
2788 if (FRAME_W32_WINDOW (f))
2789 x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
2790 do_pending_window_change (0);
2791 }
2792 }
2793
2794 void
2795 x_set_scroll_bar_width (f, arg, oldval)
2796 struct frame *f;
2797 Lisp_Object arg, oldval;
2798 {
2799 int wid = FONT_WIDTH (f->output_data.w32->font);
2800
2801 if (NILP (arg))
2802 {
2803 FRAME_SCROLL_BAR_PIXEL_WIDTH (f) = GetSystemMetrics (SM_CXVSCROLL);
2804 FRAME_SCROLL_BAR_COLS (f) = (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) +
2805 wid - 1) / wid;
2806 if (FRAME_W32_WINDOW (f))
2807 x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
2808 do_pending_window_change (0);
2809 }
2810 else if (INTEGERP (arg) && XINT (arg) > 0
2811 && XFASTINT (arg) != FRAME_SCROLL_BAR_PIXEL_WIDTH (f))
2812 {
2813 FRAME_SCROLL_BAR_PIXEL_WIDTH (f) = XFASTINT (arg);
2814 FRAME_SCROLL_BAR_COLS (f) = (FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
2815 + wid-1) / wid;
2816 if (FRAME_W32_WINDOW (f))
2817 x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
2818 do_pending_window_change (0);
2819 }
2820 change_frame_size (f, 0, FRAME_WIDTH (f), 0, 0, 0);
2821 XWINDOW (FRAME_SELECTED_WINDOW (f))->cursor.hpos = 0;
2822 XWINDOW (FRAME_SELECTED_WINDOW (f))->cursor.x = 0;
2823 }
2824 \f
2825 /* Subroutines of creating an frame. */
2826
2827 /* Make sure that Vx_resource_name is set to a reasonable value.
2828 Fix it up, or set it to `emacs' if it is too hopeless. */
2829
2830 static void
2831 validate_x_resource_name ()
2832 {
2833 int len = 0;
2834 /* Number of valid characters in the resource name. */
2835 int good_count = 0;
2836 /* Number of invalid characters in the resource name. */
2837 int bad_count = 0;
2838 Lisp_Object new;
2839 int i;
2840
2841 if (STRINGP (Vx_resource_name))
2842 {
2843 unsigned char *p = XSTRING (Vx_resource_name)->data;
2844 int i;
2845
2846 len = STRING_BYTES (XSTRING (Vx_resource_name));
2847
2848 /* Only letters, digits, - and _ are valid in resource names.
2849 Count the valid characters and count the invalid ones. */
2850 for (i = 0; i < len; i++)
2851 {
2852 int c = p[i];
2853 if (! ((c >= 'a' && c <= 'z')
2854 || (c >= 'A' && c <= 'Z')
2855 || (c >= '0' && c <= '9')
2856 || c == '-' || c == '_'))
2857 bad_count++;
2858 else
2859 good_count++;
2860 }
2861 }
2862 else
2863 /* Not a string => completely invalid. */
2864 bad_count = 5, good_count = 0;
2865
2866 /* If name is valid already, return. */
2867 if (bad_count == 0)
2868 return;
2869
2870 /* If name is entirely invalid, or nearly so, use `emacs'. */
2871 if (good_count == 0
2872 || (good_count == 1 && bad_count > 0))
2873 {
2874 Vx_resource_name = build_string ("emacs");
2875 return;
2876 }
2877
2878 /* Name is partly valid. Copy it and replace the invalid characters
2879 with underscores. */
2880
2881 Vx_resource_name = new = Fcopy_sequence (Vx_resource_name);
2882
2883 for (i = 0; i < len; i++)
2884 {
2885 int c = XSTRING (new)->data[i];
2886 if (! ((c >= 'a' && c <= 'z')
2887 || (c >= 'A' && c <= 'Z')
2888 || (c >= '0' && c <= '9')
2889 || c == '-' || c == '_'))
2890 XSTRING (new)->data[i] = '_';
2891 }
2892 }
2893
2894
2895 extern char *x_get_string_resource ();
2896
2897 DEFUN ("x-get-resource", Fx_get_resource, Sx_get_resource, 2, 4, 0,
2898 "Return the value of ATTRIBUTE, of class CLASS, from the X defaults database.\n\
2899 This uses `INSTANCE.ATTRIBUTE' as the key and `Emacs.CLASS' as the\n\
2900 class, where INSTANCE is the name under which Emacs was invoked, or\n\
2901 the name specified by the `-name' or `-rn' command-line arguments.\n\
2902 \n\
2903 The optional arguments COMPONENT and SUBCLASS add to the key and the\n\
2904 class, respectively. You must specify both of them or neither.\n\
2905 If you specify them, the key is `INSTANCE.COMPONENT.ATTRIBUTE'\n\
2906 and the class is `Emacs.CLASS.SUBCLASS'.")
2907 (attribute, class, component, subclass)
2908 Lisp_Object attribute, class, component, subclass;
2909 {
2910 register char *value;
2911 char *name_key;
2912 char *class_key;
2913
2914 CHECK_STRING (attribute, 0);
2915 CHECK_STRING (class, 0);
2916
2917 if (!NILP (component))
2918 CHECK_STRING (component, 1);
2919 if (!NILP (subclass))
2920 CHECK_STRING (subclass, 2);
2921 if (NILP (component) != NILP (subclass))
2922 error ("x-get-resource: must specify both COMPONENT and SUBCLASS or neither");
2923
2924 validate_x_resource_name ();
2925
2926 /* Allocate space for the components, the dots which separate them,
2927 and the final '\0'. Make them big enough for the worst case. */
2928 name_key = (char *) alloca (STRING_BYTES (XSTRING (Vx_resource_name))
2929 + (STRINGP (component)
2930 ? STRING_BYTES (XSTRING (component)) : 0)
2931 + STRING_BYTES (XSTRING (attribute))
2932 + 3);
2933
2934 class_key = (char *) alloca ((sizeof (EMACS_CLASS) - 1)
2935 + STRING_BYTES (XSTRING (class))
2936 + (STRINGP (subclass)
2937 ? STRING_BYTES (XSTRING (subclass)) : 0)
2938 + 3);
2939
2940 /* Start with emacs.FRAMENAME for the name (the specific one)
2941 and with `Emacs' for the class key (the general one). */
2942 strcpy (name_key, XSTRING (Vx_resource_name)->data);
2943 strcpy (class_key, EMACS_CLASS);
2944
2945 strcat (class_key, ".");
2946 strcat (class_key, XSTRING (class)->data);
2947
2948 if (!NILP (component))
2949 {
2950 strcat (class_key, ".");
2951 strcat (class_key, XSTRING (subclass)->data);
2952
2953 strcat (name_key, ".");
2954 strcat (name_key, XSTRING (component)->data);
2955 }
2956
2957 strcat (name_key, ".");
2958 strcat (name_key, XSTRING (attribute)->data);
2959
2960 value = x_get_string_resource (Qnil,
2961 name_key, class_key);
2962
2963 if (value != (char *) 0)
2964 return build_string (value);
2965 else
2966 return Qnil;
2967 }
2968
2969 /* Used when C code wants a resource value. */
2970
2971 char *
2972 x_get_resource_string (attribute, class)
2973 char *attribute, *class;
2974 {
2975 char *name_key;
2976 char *class_key;
2977 struct frame *sf = SELECTED_FRAME ();
2978
2979 /* Allocate space for the components, the dots which separate them,
2980 and the final '\0'. */
2981 name_key = (char *) alloca (STRING_BYTES (XSTRING (Vinvocation_name))
2982 + strlen (attribute) + 2);
2983 class_key = (char *) alloca ((sizeof (EMACS_CLASS) - 1)
2984 + strlen (class) + 2);
2985
2986 sprintf (name_key, "%s.%s",
2987 XSTRING (Vinvocation_name)->data,
2988 attribute);
2989 sprintf (class_key, "%s.%s", EMACS_CLASS, class);
2990
2991 return x_get_string_resource (sf, name_key, class_key);
2992 }
2993
2994 /* Types we might convert a resource string into. */
2995 enum resource_types
2996 {
2997 RES_TYPE_NUMBER,
2998 RES_TYPE_FLOAT,
2999 RES_TYPE_BOOLEAN,
3000 RES_TYPE_STRING,
3001 RES_TYPE_SYMBOL
3002 };
3003
3004 /* Return the value of parameter PARAM.
3005
3006 First search ALIST, then Vdefault_frame_alist, then the X defaults
3007 database, using ATTRIBUTE as the attribute name and CLASS as its class.
3008
3009 Convert the resource to the type specified by desired_type.
3010
3011 If no default is specified, return Qunbound. If you call
3012 w32_get_arg, make sure you deal with Qunbound in a reasonable way,
3013 and don't let it get stored in any Lisp-visible variables! */
3014
3015 static Lisp_Object
3016 w32_get_arg (alist, param, attribute, class, type)
3017 Lisp_Object alist, param;
3018 char *attribute;
3019 char *class;
3020 enum resource_types type;
3021 {
3022 register Lisp_Object tem;
3023
3024 tem = Fassq (param, alist);
3025 if (EQ (tem, Qnil))
3026 tem = Fassq (param, Vdefault_frame_alist);
3027 if (EQ (tem, Qnil))
3028 {
3029
3030 if (attribute)
3031 {
3032 tem = Fx_get_resource (build_string (attribute),
3033 build_string (class),
3034 Qnil, Qnil);
3035
3036 if (NILP (tem))
3037 return Qunbound;
3038
3039 switch (type)
3040 {
3041 case RES_TYPE_NUMBER:
3042 return make_number (atoi (XSTRING (tem)->data));
3043
3044 case RES_TYPE_FLOAT:
3045 return make_float (atof (XSTRING (tem)->data));
3046
3047 case RES_TYPE_BOOLEAN:
3048 tem = Fdowncase (tem);
3049 if (!strcmp (XSTRING (tem)->data, "on")
3050 || !strcmp (XSTRING (tem)->data, "true"))
3051 return Qt;
3052 else
3053 return Qnil;
3054
3055 case RES_TYPE_STRING:
3056 return tem;
3057
3058 case RES_TYPE_SYMBOL:
3059 /* As a special case, we map the values `true' and `on'
3060 to Qt, and `false' and `off' to Qnil. */
3061 {
3062 Lisp_Object lower;
3063 lower = Fdowncase (tem);
3064 if (!strcmp (XSTRING (lower)->data, "on")
3065 || !strcmp (XSTRING (lower)->data, "true"))
3066 return Qt;
3067 else if (!strcmp (XSTRING (lower)->data, "off")
3068 || !strcmp (XSTRING (lower)->data, "false"))
3069 return Qnil;
3070 else
3071 return Fintern (tem, Qnil);
3072 }
3073
3074 default:
3075 abort ();
3076 }
3077 }
3078 else
3079 return Qunbound;
3080 }
3081 return Fcdr (tem);
3082 }
3083
3084 /* Record in frame F the specified or default value according to ALIST
3085 of the parameter named PROP (a Lisp symbol).
3086 If no value is specified for PROP, look for an X default for XPROP
3087 on the frame named NAME.
3088 If that is not found either, use the value DEFLT. */
3089
3090 static Lisp_Object
3091 x_default_parameter (f, alist, prop, deflt, xprop, xclass, type)
3092 struct frame *f;
3093 Lisp_Object alist;
3094 Lisp_Object prop;
3095 Lisp_Object deflt;
3096 char *xprop;
3097 char *xclass;
3098 enum resource_types type;
3099 {
3100 Lisp_Object tem;
3101
3102 tem = w32_get_arg (alist, prop, xprop, xclass, type);
3103 if (EQ (tem, Qunbound))
3104 tem = deflt;
3105 x_set_frame_parameters (f, Fcons (Fcons (prop, tem), Qnil));
3106 return tem;
3107 }
3108 \f
3109 DEFUN ("x-parse-geometry", Fx_parse_geometry, Sx_parse_geometry, 1, 1, 0,
3110 "Parse an X-style geometry string STRING.\n\
3111 Returns an alist of the form ((top . TOP), (left . LEFT) ... ).\n\
3112 The properties returned may include `top', `left', `height', and `width'.\n\
3113 The value of `left' or `top' may be an integer,\n\
3114 or a list (+ N) meaning N pixels relative to top/left corner,\n\
3115 or a list (- N) meaning -N pixels relative to bottom/right corner.")
3116 (string)
3117 Lisp_Object string;
3118 {
3119 int geometry, x, y;
3120 unsigned int width, height;
3121 Lisp_Object result;
3122
3123 CHECK_STRING (string, 0);
3124
3125 geometry = XParseGeometry ((char *) XSTRING (string)->data,
3126 &x, &y, &width, &height);
3127
3128 result = Qnil;
3129 if (geometry & XValue)
3130 {
3131 Lisp_Object element;
3132
3133 if (x >= 0 && (geometry & XNegative))
3134 element = Fcons (Qleft, Fcons (Qminus, Fcons (make_number (-x), Qnil)));
3135 else if (x < 0 && ! (geometry & XNegative))
3136 element = Fcons (Qleft, Fcons (Qplus, Fcons (make_number (x), Qnil)));
3137 else
3138 element = Fcons (Qleft, make_number (x));
3139 result = Fcons (element, result);
3140 }
3141
3142 if (geometry & YValue)
3143 {
3144 Lisp_Object element;
3145
3146 if (y >= 0 && (geometry & YNegative))
3147 element = Fcons (Qtop, Fcons (Qminus, Fcons (make_number (-y), Qnil)));
3148 else if (y < 0 && ! (geometry & YNegative))
3149 element = Fcons (Qtop, Fcons (Qplus, Fcons (make_number (y), Qnil)));
3150 else
3151 element = Fcons (Qtop, make_number (y));
3152 result = Fcons (element, result);
3153 }
3154
3155 if (geometry & WidthValue)
3156 result = Fcons (Fcons (Qwidth, make_number (width)), result);
3157 if (geometry & HeightValue)
3158 result = Fcons (Fcons (Qheight, make_number (height)), result);
3159
3160 return result;
3161 }
3162
3163 /* Calculate the desired size and position of this window,
3164 and return the flags saying which aspects were specified.
3165
3166 This function does not make the coordinates positive. */
3167
3168 #define DEFAULT_ROWS 40
3169 #define DEFAULT_COLS 80
3170
3171 static int
3172 x_figure_window_size (f, parms)
3173 struct frame *f;
3174 Lisp_Object parms;
3175 {
3176 register Lisp_Object tem0, tem1, tem2;
3177 long window_prompting = 0;
3178
3179 /* Default values if we fall through.
3180 Actually, if that happens we should get
3181 window manager prompting. */
3182 SET_FRAME_WIDTH (f, DEFAULT_COLS);
3183 f->height = DEFAULT_ROWS;
3184 /* Window managers expect that if program-specified
3185 positions are not (0,0), they're intentional, not defaults. */
3186 f->output_data.w32->top_pos = 0;
3187 f->output_data.w32->left_pos = 0;
3188
3189 tem0 = w32_get_arg (parms, Qheight, 0, 0, RES_TYPE_NUMBER);
3190 tem1 = w32_get_arg (parms, Qwidth, 0, 0, RES_TYPE_NUMBER);
3191 tem2 = w32_get_arg (parms, Quser_size, 0, 0, RES_TYPE_NUMBER);
3192 if (! EQ (tem0, Qunbound) || ! EQ (tem1, Qunbound))
3193 {
3194 if (!EQ (tem0, Qunbound))
3195 {
3196 CHECK_NUMBER (tem0, 0);
3197 f->height = XINT (tem0);
3198 }
3199 if (!EQ (tem1, Qunbound))
3200 {
3201 CHECK_NUMBER (tem1, 0);
3202 SET_FRAME_WIDTH (f, XINT (tem1));
3203 }
3204 if (!NILP (tem2) && !EQ (tem2, Qunbound))
3205 window_prompting |= USSize;
3206 else
3207 window_prompting |= PSize;
3208 }
3209
3210 f->output_data.w32->vertical_scroll_bar_extra
3211 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
3212 ? 0
3213 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
3214 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
3215 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.w32->font)));
3216 f->output_data.w32->flags_areas_extra
3217 = FRAME_FLAGS_AREA_WIDTH (f);
3218 f->output_data.w32->pixel_width = CHAR_TO_PIXEL_WIDTH (f, f->width);
3219 f->output_data.w32->pixel_height = CHAR_TO_PIXEL_HEIGHT (f, f->height);
3220
3221 tem0 = w32_get_arg (parms, Qtop, 0, 0, RES_TYPE_NUMBER);
3222 tem1 = w32_get_arg (parms, Qleft, 0, 0, RES_TYPE_NUMBER);
3223 tem2 = w32_get_arg (parms, Quser_position, 0, 0, RES_TYPE_NUMBER);
3224 if (! EQ (tem0, Qunbound) || ! EQ (tem1, Qunbound))
3225 {
3226 if (EQ (tem0, Qminus))
3227 {
3228 f->output_data.w32->top_pos = 0;
3229 window_prompting |= YNegative;
3230 }
3231 else if (CONSP (tem0) && EQ (XCAR (tem0), Qminus)
3232 && CONSP (XCDR (tem0))
3233 && INTEGERP (XCAR (XCDR (tem0))))
3234 {
3235 f->output_data.w32->top_pos = - XINT (XCAR (XCDR (tem0)));
3236 window_prompting |= YNegative;
3237 }
3238 else if (CONSP (tem0) && EQ (XCAR (tem0), Qplus)
3239 && CONSP (XCDR (tem0))
3240 && INTEGERP (XCAR (XCDR (tem0))))
3241 {
3242 f->output_data.w32->top_pos = XINT (XCAR (XCDR (tem0)));
3243 }
3244 else if (EQ (tem0, Qunbound))
3245 f->output_data.w32->top_pos = 0;
3246 else
3247 {
3248 CHECK_NUMBER (tem0, 0);
3249 f->output_data.w32->top_pos = XINT (tem0);
3250 if (f->output_data.w32->top_pos < 0)
3251 window_prompting |= YNegative;
3252 }
3253
3254 if (EQ (tem1, Qminus))
3255 {
3256 f->output_data.w32->left_pos = 0;
3257 window_prompting |= XNegative;
3258 }
3259 else if (CONSP (tem1) && EQ (XCAR (tem1), Qminus)
3260 && CONSP (XCDR (tem1))
3261 && INTEGERP (XCAR (XCDR (tem1))))
3262 {
3263 f->output_data.w32->left_pos = - XINT (XCAR (XCDR (tem1)));
3264 window_prompting |= XNegative;
3265 }
3266 else if (CONSP (tem1) && EQ (XCAR (tem1), Qplus)
3267 && CONSP (XCDR (tem1))
3268 && INTEGERP (XCAR (XCDR (tem1))))
3269 {
3270 f->output_data.w32->left_pos = XINT (XCAR (XCDR (tem1)));
3271 }
3272 else if (EQ (tem1, Qunbound))
3273 f->output_data.w32->left_pos = 0;
3274 else
3275 {
3276 CHECK_NUMBER (tem1, 0);
3277 f->output_data.w32->left_pos = XINT (tem1);
3278 if (f->output_data.w32->left_pos < 0)
3279 window_prompting |= XNegative;
3280 }
3281
3282 if (!NILP (tem2) && ! EQ (tem2, Qunbound))
3283 window_prompting |= USPosition;
3284 else
3285 window_prompting |= PPosition;
3286 }
3287
3288 return window_prompting;
3289 }
3290
3291 \f
3292
3293 extern LRESULT CALLBACK w32_wnd_proc ();
3294
3295 BOOL
3296 w32_init_class (hinst)
3297 HINSTANCE hinst;
3298 {
3299 WNDCLASS wc;
3300
3301 wc.style = CS_HREDRAW | CS_VREDRAW;
3302 wc.lpfnWndProc = (WNDPROC) w32_wnd_proc;
3303 wc.cbClsExtra = 0;
3304 wc.cbWndExtra = WND_EXTRA_BYTES;
3305 wc.hInstance = hinst;
3306 wc.hIcon = LoadIcon (hinst, EMACS_CLASS);
3307 wc.hCursor = LoadCursor (NULL, IDC_ARROW);
3308 wc.hbrBackground = NULL; /* GetStockObject (WHITE_BRUSH); */
3309 wc.lpszMenuName = NULL;
3310 wc.lpszClassName = EMACS_CLASS;
3311
3312 return (RegisterClass (&wc));
3313 }
3314
3315 HWND
3316 w32_createscrollbar (f, bar)
3317 struct frame *f;
3318 struct scroll_bar * bar;
3319 {
3320 return (CreateWindow ("SCROLLBAR", "", SBS_VERT | WS_CHILD | WS_VISIBLE,
3321 /* Position and size of scroll bar. */
3322 XINT(bar->left) + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
3323 XINT(bar->top),
3324 XINT(bar->width) - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
3325 XINT(bar->height),
3326 FRAME_W32_WINDOW (f),
3327 NULL,
3328 hinst,
3329 NULL));
3330 }
3331
3332 void
3333 w32_createwindow (f)
3334 struct frame *f;
3335 {
3336 HWND hwnd;
3337 RECT rect;
3338
3339 rect.left = rect.top = 0;
3340 rect.right = PIXEL_WIDTH (f);
3341 rect.bottom = PIXEL_HEIGHT (f);
3342
3343 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
3344 FRAME_EXTERNAL_MENU_BAR (f));
3345
3346 /* Do first time app init */
3347
3348 if (!hprevinst)
3349 {
3350 w32_init_class (hinst);
3351 }
3352
3353 FRAME_W32_WINDOW (f) = hwnd
3354 = CreateWindow (EMACS_CLASS,
3355 f->namebuf,
3356 f->output_data.w32->dwStyle | WS_CLIPCHILDREN,
3357 f->output_data.w32->left_pos,
3358 f->output_data.w32->top_pos,
3359 rect.right - rect.left,
3360 rect.bottom - rect.top,
3361 NULL,
3362 NULL,
3363 hinst,
3364 NULL);
3365
3366 if (hwnd)
3367 {
3368 SetWindowLong (hwnd, WND_FONTWIDTH_INDEX, FONT_WIDTH (f->output_data.w32->font));
3369 SetWindowLong (hwnd, WND_LINEHEIGHT_INDEX, f->output_data.w32->line_height);
3370 SetWindowLong (hwnd, WND_BORDER_INDEX, f->output_data.w32->internal_border_width);
3371 SetWindowLong (hwnd, WND_SCROLLBAR_INDEX, f->output_data.w32->vertical_scroll_bar_extra);
3372 SetWindowLong (hwnd, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
3373
3374 /* Enable drag-n-drop. */
3375 DragAcceptFiles (hwnd, TRUE);
3376
3377 /* Do this to discard the default setting specified by our parent. */
3378 ShowWindow (hwnd, SW_HIDE);
3379 }
3380 }
3381
3382 void
3383 my_post_msg (wmsg, hwnd, msg, wParam, lParam)
3384 W32Msg * wmsg;
3385 HWND hwnd;
3386 UINT msg;
3387 WPARAM wParam;
3388 LPARAM lParam;
3389 {
3390 wmsg->msg.hwnd = hwnd;
3391 wmsg->msg.message = msg;
3392 wmsg->msg.wParam = wParam;
3393 wmsg->msg.lParam = lParam;
3394 wmsg->msg.time = GetMessageTime ();
3395
3396 post_msg (wmsg);
3397 }
3398
3399 /* GetKeyState and MapVirtualKey on Windows 95 do not actually distinguish
3400 between left and right keys as advertised. We test for this
3401 support dynamically, and set a flag when the support is absent. If
3402 absent, we keep track of the left and right control and alt keys
3403 ourselves. This is particularly necessary on keyboards that rely
3404 upon the AltGr key, which is represented as having the left control
3405 and right alt keys pressed. For these keyboards, we need to know
3406 when the left alt key has been pressed in addition to the AltGr key
3407 so that we can properly support M-AltGr-key sequences (such as M-@
3408 on Swedish keyboards). */
3409
3410 #define EMACS_LCONTROL 0
3411 #define EMACS_RCONTROL 1
3412 #define EMACS_LMENU 2
3413 #define EMACS_RMENU 3
3414
3415 static int modifiers[4];
3416 static int modifiers_recorded;
3417 static int modifier_key_support_tested;
3418
3419 static void
3420 test_modifier_support (unsigned int wparam)
3421 {
3422 unsigned int l, r;
3423
3424 if (wparam != VK_CONTROL && wparam != VK_MENU)
3425 return;
3426 if (wparam == VK_CONTROL)
3427 {
3428 l = VK_LCONTROL;
3429 r = VK_RCONTROL;
3430 }
3431 else
3432 {
3433 l = VK_LMENU;
3434 r = VK_RMENU;
3435 }
3436 if (!(GetKeyState (l) & 0x8000) && !(GetKeyState (r) & 0x8000))
3437 modifiers_recorded = 1;
3438 else
3439 modifiers_recorded = 0;
3440 modifier_key_support_tested = 1;
3441 }
3442
3443 static void
3444 record_keydown (unsigned int wparam, unsigned int lparam)
3445 {
3446 int i;
3447
3448 if (!modifier_key_support_tested)
3449 test_modifier_support (wparam);
3450
3451 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
3452 return;
3453
3454 if (wparam == VK_CONTROL)
3455 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
3456 else
3457 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
3458
3459 modifiers[i] = 1;
3460 }
3461
3462 static void
3463 record_keyup (unsigned int wparam, unsigned int lparam)
3464 {
3465 int i;
3466
3467 if ((wparam != VK_CONTROL && wparam != VK_MENU) || !modifiers_recorded)
3468 return;
3469
3470 if (wparam == VK_CONTROL)
3471 i = (lparam & 0x1000000) ? EMACS_RCONTROL : EMACS_LCONTROL;
3472 else
3473 i = (lparam & 0x1000000) ? EMACS_RMENU : EMACS_LMENU;
3474
3475 modifiers[i] = 0;
3476 }
3477
3478 /* Emacs can lose focus while a modifier key has been pressed. When
3479 it regains focus, be conservative and clear all modifiers since
3480 we cannot reconstruct the left and right modifier state. */
3481 static void
3482 reset_modifiers ()
3483 {
3484 SHORT ctrl, alt;
3485
3486 if (GetFocus () == NULL)
3487 /* Emacs doesn't have keyboard focus. Do nothing. */
3488 return;
3489
3490 ctrl = GetAsyncKeyState (VK_CONTROL);
3491 alt = GetAsyncKeyState (VK_MENU);
3492
3493 if (!(ctrl & 0x08000))
3494 /* Clear any recorded control modifier state. */
3495 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
3496
3497 if (!(alt & 0x08000))
3498 /* Clear any recorded alt modifier state. */
3499 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
3500
3501 /* Update the state of all modifier keys, because modifiers used in
3502 hot-key combinations can get stuck on if Emacs loses focus as a
3503 result of a hot-key being pressed. */
3504 {
3505 BYTE keystate[256];
3506
3507 #define CURRENT_STATE(key) ((GetAsyncKeyState (key) & 0x8000) >> 8)
3508
3509 GetKeyboardState (keystate);
3510 keystate[VK_SHIFT] = CURRENT_STATE (VK_SHIFT);
3511 keystate[VK_CONTROL] = CURRENT_STATE (VK_CONTROL);
3512 keystate[VK_LCONTROL] = CURRENT_STATE (VK_LCONTROL);
3513 keystate[VK_RCONTROL] = CURRENT_STATE (VK_RCONTROL);
3514 keystate[VK_MENU] = CURRENT_STATE (VK_MENU);
3515 keystate[VK_LMENU] = CURRENT_STATE (VK_LMENU);
3516 keystate[VK_RMENU] = CURRENT_STATE (VK_RMENU);
3517 keystate[VK_LWIN] = CURRENT_STATE (VK_LWIN);
3518 keystate[VK_RWIN] = CURRENT_STATE (VK_RWIN);
3519 keystate[VK_APPS] = CURRENT_STATE (VK_APPS);
3520 SetKeyboardState (keystate);
3521 }
3522 }
3523
3524 /* Synchronize modifier state with what is reported with the current
3525 keystroke. Even if we cannot distinguish between left and right
3526 modifier keys, we know that, if no modifiers are set, then neither
3527 the left or right modifier should be set. */
3528 static void
3529 sync_modifiers ()
3530 {
3531 if (!modifiers_recorded)
3532 return;
3533
3534 if (!(GetKeyState (VK_CONTROL) & 0x8000))
3535 modifiers[EMACS_RCONTROL] = modifiers[EMACS_LCONTROL] = 0;
3536
3537 if (!(GetKeyState (VK_MENU) & 0x8000))
3538 modifiers[EMACS_RMENU] = modifiers[EMACS_LMENU] = 0;
3539 }
3540
3541 static int
3542 modifier_set (int vkey)
3543 {
3544 if (vkey == VK_CAPITAL || vkey == VK_SCROLL)
3545 return (GetKeyState (vkey) & 0x1);
3546 if (!modifiers_recorded)
3547 return (GetKeyState (vkey) & 0x8000);
3548
3549 switch (vkey)
3550 {
3551 case VK_LCONTROL:
3552 return modifiers[EMACS_LCONTROL];
3553 case VK_RCONTROL:
3554 return modifiers[EMACS_RCONTROL];
3555 case VK_LMENU:
3556 return modifiers[EMACS_LMENU];
3557 case VK_RMENU:
3558 return modifiers[EMACS_RMENU];
3559 }
3560 return (GetKeyState (vkey) & 0x8000);
3561 }
3562
3563 /* Convert between the modifier bits W32 uses and the modifier bits
3564 Emacs uses. */
3565
3566 unsigned int
3567 w32_key_to_modifier (int key)
3568 {
3569 Lisp_Object key_mapping;
3570
3571 switch (key)
3572 {
3573 case VK_LWIN:
3574 key_mapping = Vw32_lwindow_modifier;
3575 break;
3576 case VK_RWIN:
3577 key_mapping = Vw32_rwindow_modifier;
3578 break;
3579 case VK_APPS:
3580 key_mapping = Vw32_apps_modifier;
3581 break;
3582 case VK_SCROLL:
3583 key_mapping = Vw32_scroll_lock_modifier;
3584 break;
3585 default:
3586 key_mapping = Qnil;
3587 }
3588
3589 /* NB. This code runs in the input thread, asychronously to the lisp
3590 thread, so we must be careful to ensure access to lisp data is
3591 thread-safe. The following code is safe because the modifier
3592 variable values are updated atomically from lisp and symbols are
3593 not relocated by GC. Also, we don't have to worry about seeing GC
3594 markbits here. */
3595 if (EQ (key_mapping, Qhyper))
3596 return hyper_modifier;
3597 if (EQ (key_mapping, Qsuper))
3598 return super_modifier;
3599 if (EQ (key_mapping, Qmeta))
3600 return meta_modifier;
3601 if (EQ (key_mapping, Qalt))
3602 return alt_modifier;
3603 if (EQ (key_mapping, Qctrl))
3604 return ctrl_modifier;
3605 if (EQ (key_mapping, Qcontrol)) /* synonym for ctrl */
3606 return ctrl_modifier;
3607 if (EQ (key_mapping, Qshift))
3608 return shift_modifier;
3609
3610 /* Don't generate any modifier if not explicitly requested. */
3611 return 0;
3612 }
3613
3614 unsigned int
3615 w32_get_modifiers ()
3616 {
3617 return ((modifier_set (VK_SHIFT) ? shift_modifier : 0) |
3618 (modifier_set (VK_CONTROL) ? ctrl_modifier : 0) |
3619 (modifier_set (VK_LWIN) ? w32_key_to_modifier (VK_LWIN) : 0) |
3620 (modifier_set (VK_RWIN) ? w32_key_to_modifier (VK_RWIN) : 0) |
3621 (modifier_set (VK_APPS) ? w32_key_to_modifier (VK_APPS) : 0) |
3622 (modifier_set (VK_SCROLL) ? w32_key_to_modifier (VK_SCROLL) : 0) |
3623 (modifier_set (VK_MENU) ?
3624 ((NILP (Vw32_alt_is_meta)) ? alt_modifier : meta_modifier) : 0));
3625 }
3626
3627 /* We map the VK_* modifiers into console modifier constants
3628 so that we can use the same routines to handle both console
3629 and window input. */
3630
3631 static int
3632 construct_console_modifiers ()
3633 {
3634 int mods;
3635
3636 mods = 0;
3637 mods |= (modifier_set (VK_SHIFT)) ? SHIFT_PRESSED : 0;
3638 mods |= (modifier_set (VK_CAPITAL)) ? CAPSLOCK_ON : 0;
3639 mods |= (modifier_set (VK_SCROLL)) ? SCROLLLOCK_ON : 0;
3640 mods |= (modifier_set (VK_NUMLOCK)) ? NUMLOCK_ON : 0;
3641 mods |= (modifier_set (VK_LCONTROL)) ? LEFT_CTRL_PRESSED : 0;
3642 mods |= (modifier_set (VK_RCONTROL)) ? RIGHT_CTRL_PRESSED : 0;
3643 mods |= (modifier_set (VK_LMENU)) ? LEFT_ALT_PRESSED : 0;
3644 mods |= (modifier_set (VK_RMENU)) ? RIGHT_ALT_PRESSED : 0;
3645 mods |= (modifier_set (VK_LWIN)) ? LEFT_WIN_PRESSED : 0;
3646 mods |= (modifier_set (VK_RWIN)) ? RIGHT_WIN_PRESSED : 0;
3647 mods |= (modifier_set (VK_APPS)) ? APPS_PRESSED : 0;
3648
3649 return mods;
3650 }
3651
3652 static int
3653 w32_get_key_modifiers (unsigned int wparam, unsigned int lparam)
3654 {
3655 int mods;
3656
3657 /* Convert to emacs modifiers. */
3658 mods = w32_kbd_mods_to_emacs (construct_console_modifiers (), wparam);
3659
3660 return mods;
3661 }
3662
3663 unsigned int
3664 map_keypad_keys (unsigned int virt_key, unsigned int extended)
3665 {
3666 if (virt_key < VK_CLEAR || virt_key > VK_DELETE)
3667 return virt_key;
3668
3669 if (virt_key == VK_RETURN)
3670 return (extended ? VK_NUMPAD_ENTER : VK_RETURN);
3671
3672 if (virt_key >= VK_PRIOR && virt_key <= VK_DOWN)
3673 return (!extended ? (VK_NUMPAD_PRIOR + (virt_key - VK_PRIOR)) : virt_key);
3674
3675 if (virt_key == VK_INSERT || virt_key == VK_DELETE)
3676 return (!extended ? (VK_NUMPAD_INSERT + (virt_key - VK_INSERT)) : virt_key);
3677
3678 if (virt_key == VK_CLEAR)
3679 return (!extended ? VK_NUMPAD_CLEAR : virt_key);
3680
3681 return virt_key;
3682 }
3683
3684 /* List of special key combinations which w32 would normally capture,
3685 but emacs should grab instead. Not directly visible to lisp, to
3686 simplify synchronization. Each item is an integer encoding a virtual
3687 key code and modifier combination to capture. */
3688 Lisp_Object w32_grabbed_keys;
3689
3690 #define HOTKEY(vk,mods) make_number (((vk) & 255) | ((mods) << 8))
3691 #define HOTKEY_ID(k) (XFASTINT (k) & 0xbfff)
3692 #define HOTKEY_VK_CODE(k) (XFASTINT (k) & 255)
3693 #define HOTKEY_MODIFIERS(k) (XFASTINT (k) >> 8)
3694
3695 /* Register hot-keys for reserved key combinations when Emacs has
3696 keyboard focus, since this is the only way Emacs can receive key
3697 combinations like Alt-Tab which are used by the system. */
3698
3699 static void
3700 register_hot_keys (hwnd)
3701 HWND hwnd;
3702 {
3703 Lisp_Object keylist;
3704
3705 /* Use GC_CONSP, since we are called asynchronously. */
3706 for (keylist = w32_grabbed_keys; GC_CONSP (keylist); keylist = XCDR (keylist))
3707 {
3708 Lisp_Object key = XCAR (keylist);
3709
3710 /* Deleted entries get set to nil. */
3711 if (!INTEGERP (key))
3712 continue;
3713
3714 RegisterHotKey (hwnd, HOTKEY_ID (key),
3715 HOTKEY_MODIFIERS (key), HOTKEY_VK_CODE (key));
3716 }
3717 }
3718
3719 static void
3720 unregister_hot_keys (hwnd)
3721 HWND hwnd;
3722 {
3723 Lisp_Object keylist;
3724
3725 /* Use GC_CONSP, since we are called asynchronously. */
3726 for (keylist = w32_grabbed_keys; GC_CONSP (keylist); keylist = XCDR (keylist))
3727 {
3728 Lisp_Object key = XCAR (keylist);
3729
3730 if (!INTEGERP (key))
3731 continue;
3732
3733 UnregisterHotKey (hwnd, HOTKEY_ID (key));
3734 }
3735 }
3736
3737 /* Main message dispatch loop. */
3738
3739 static void
3740 w32_msg_pump (deferred_msg * msg_buf)
3741 {
3742 MSG msg;
3743 int result;
3744 HWND focus_window;
3745
3746 msh_mousewheel = RegisterWindowMessage (MSH_MOUSEWHEEL);
3747
3748 while (GetMessage (&msg, NULL, 0, 0))
3749 {
3750 if (msg.hwnd == NULL)
3751 {
3752 switch (msg.message)
3753 {
3754 case WM_NULL:
3755 /* Produced by complete_deferred_msg; just ignore. */
3756 break;
3757 case WM_EMACS_CREATEWINDOW:
3758 w32_createwindow ((struct frame *) msg.wParam);
3759 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
3760 abort ();
3761 break;
3762 case WM_EMACS_SETLOCALE:
3763 SetThreadLocale (msg.wParam);
3764 /* Reply is not expected. */
3765 break;
3766 case WM_EMACS_SETKEYBOARDLAYOUT:
3767 result = (int) ActivateKeyboardLayout ((HKL) msg.wParam, 0);
3768 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
3769 result, 0))
3770 abort ();
3771 break;
3772 case WM_EMACS_REGISTER_HOT_KEY:
3773 focus_window = GetFocus ();
3774 if (focus_window != NULL)
3775 RegisterHotKey (focus_window,
3776 HOTKEY_ID (msg.wParam),
3777 HOTKEY_MODIFIERS (msg.wParam),
3778 HOTKEY_VK_CODE (msg.wParam));
3779 /* Reply is not expected. */
3780 break;
3781 case WM_EMACS_UNREGISTER_HOT_KEY:
3782 focus_window = GetFocus ();
3783 if (focus_window != NULL)
3784 UnregisterHotKey (focus_window, HOTKEY_ID (msg.wParam));
3785 /* Mark item as erased. NB: this code must be
3786 thread-safe. The next line is okay because the cons
3787 cell is never made into garbage and is not relocated by
3788 GC. */
3789 XCAR ((Lisp_Object) msg.lParam) = Qnil;
3790 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
3791 abort ();
3792 break;
3793 case WM_EMACS_TOGGLE_LOCK_KEY:
3794 {
3795 int vk_code = (int) msg.wParam;
3796 int cur_state = (GetKeyState (vk_code) & 1);
3797 Lisp_Object new_state = (Lisp_Object) msg.lParam;
3798
3799 /* NB: This code must be thread-safe. It is safe to
3800 call NILP because symbols are not relocated by GC,
3801 and pointer here is not touched by GC (so the markbit
3802 can't be set). Numbers are safe because they are
3803 immediate values. */
3804 if (NILP (new_state)
3805 || (NUMBERP (new_state)
3806 && (XUINT (new_state)) & 1 != cur_state))
3807 {
3808 one_w32_display_info.faked_key = vk_code;
3809
3810 keybd_event ((BYTE) vk_code,
3811 (BYTE) MapVirtualKey (vk_code, 0),
3812 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
3813 keybd_event ((BYTE) vk_code,
3814 (BYTE) MapVirtualKey (vk_code, 0),
3815 KEYEVENTF_EXTENDEDKEY | 0, 0);
3816 keybd_event ((BYTE) vk_code,
3817 (BYTE) MapVirtualKey (vk_code, 0),
3818 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
3819 cur_state = !cur_state;
3820 }
3821 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE,
3822 cur_state, 0))
3823 abort ();
3824 }
3825 break;
3826 default:
3827 DebPrint (("msg %x not expected by w32_msg_pump\n", msg.message));
3828 }
3829 }
3830 else
3831 {
3832 DispatchMessage (&msg);
3833 }
3834
3835 /* Exit nested loop when our deferred message has completed. */
3836 if (msg_buf->completed)
3837 break;
3838 }
3839 }
3840
3841 deferred_msg * deferred_msg_head;
3842
3843 static deferred_msg *
3844 find_deferred_msg (HWND hwnd, UINT msg)
3845 {
3846 deferred_msg * item;
3847
3848 /* Don't actually need synchronization for read access, since
3849 modification of single pointer is always atomic. */
3850 /* enter_crit (); */
3851
3852 for (item = deferred_msg_head; item != NULL; item = item->next)
3853 if (item->w32msg.msg.hwnd == hwnd
3854 && item->w32msg.msg.message == msg)
3855 break;
3856
3857 /* leave_crit (); */
3858
3859 return item;
3860 }
3861
3862 static LRESULT
3863 send_deferred_msg (deferred_msg * msg_buf,
3864 HWND hwnd,
3865 UINT msg,
3866 WPARAM wParam,
3867 LPARAM lParam)
3868 {
3869 /* Only input thread can send deferred messages. */
3870 if (GetCurrentThreadId () != dwWindowsThreadId)
3871 abort ();
3872
3873 /* It is an error to send a message that is already deferred. */
3874 if (find_deferred_msg (hwnd, msg) != NULL)
3875 abort ();
3876
3877 /* Enforced synchronization is not needed because this is the only
3878 function that alters deferred_msg_head, and the following critical
3879 section is guaranteed to only be serially reentered (since only the
3880 input thread can call us). */
3881
3882 /* enter_crit (); */
3883
3884 msg_buf->completed = 0;
3885 msg_buf->next = deferred_msg_head;
3886 deferred_msg_head = msg_buf;
3887 my_post_msg (&msg_buf->w32msg, hwnd, msg, wParam, lParam);
3888
3889 /* leave_crit (); */
3890
3891 /* Start a new nested message loop to process other messages until
3892 this one is completed. */
3893 w32_msg_pump (msg_buf);
3894
3895 deferred_msg_head = msg_buf->next;
3896
3897 return msg_buf->result;
3898 }
3899
3900 void
3901 complete_deferred_msg (HWND hwnd, UINT msg, LRESULT result)
3902 {
3903 deferred_msg * msg_buf = find_deferred_msg (hwnd, msg);
3904
3905 if (msg_buf == NULL)
3906 /* Message may have been cancelled, so don't abort(). */
3907 return;
3908
3909 msg_buf->result = result;
3910 msg_buf->completed = 1;
3911
3912 /* Ensure input thread is woken so it notices the completion. */
3913 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
3914 }
3915
3916 void
3917 cancel_all_deferred_msgs ()
3918 {
3919 deferred_msg * item;
3920
3921 /* Don't actually need synchronization for read access, since
3922 modification of single pointer is always atomic. */
3923 /* enter_crit (); */
3924
3925 for (item = deferred_msg_head; item != NULL; item = item->next)
3926 {
3927 item->result = 0;
3928 item->completed = 1;
3929 }
3930
3931 /* leave_crit (); */
3932
3933 /* Ensure input thread is woken so it notices the completion. */
3934 PostThreadMessage (dwWindowsThreadId, WM_NULL, 0, 0);
3935 }
3936
3937 DWORD
3938 w32_msg_worker (dw)
3939 DWORD dw;
3940 {
3941 MSG msg;
3942 deferred_msg dummy_buf;
3943
3944 /* Ensure our message queue is created */
3945
3946 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
3947
3948 if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
3949 abort ();
3950
3951 memset (&dummy_buf, 0, sizeof (dummy_buf));
3952 dummy_buf.w32msg.msg.hwnd = NULL;
3953 dummy_buf.w32msg.msg.message = WM_NULL;
3954
3955 /* This is the inital message loop which should only exit when the
3956 application quits. */
3957 w32_msg_pump (&dummy_buf);
3958
3959 return 0;
3960 }
3961
3962 static void
3963 post_character_message (hwnd, msg, wParam, lParam, modifiers)
3964 HWND hwnd;
3965 UINT msg;
3966 WPARAM wParam;
3967 LPARAM lParam;
3968 DWORD modifiers;
3969
3970 {
3971 W32Msg wmsg;
3972
3973 wmsg.dwModifiers = modifiers;
3974
3975 /* Detect quit_char and set quit-flag directly. Note that we
3976 still need to post a message to ensure the main thread will be
3977 woken up if blocked in sys_select(), but we do NOT want to post
3978 the quit_char message itself (because it will usually be as if
3979 the user had typed quit_char twice). Instead, we post a dummy
3980 message that has no particular effect. */
3981 {
3982 int c = wParam;
3983 if (isalpha (c) && wmsg.dwModifiers == ctrl_modifier)
3984 c = make_ctrl_char (c) & 0377;
3985 if (c == quit_char
3986 || (wmsg.dwModifiers == 0 &&
3987 XFASTINT (Vw32_quit_key) && wParam == XFASTINT (Vw32_quit_key)))
3988 {
3989 Vquit_flag = Qt;
3990
3991 /* The choice of message is somewhat arbitrary, as long as
3992 the main thread handler just ignores it. */
3993 msg = WM_NULL;
3994
3995 /* Interrupt any blocking system calls. */
3996 signal_quit ();
3997
3998 /* As a safety precaution, forcibly complete any deferred
3999 messages. This is a kludge, but I don't see any particularly
4000 clean way to handle the situation where a deferred message is
4001 "dropped" in the lisp thread, and will thus never be
4002 completed, eg. by the user trying to activate the menubar
4003 when the lisp thread is busy, and then typing C-g when the
4004 menubar doesn't open promptly (with the result that the
4005 menubar never responds at all because the deferred
4006 WM_INITMENU message is never completed). Another problem
4007 situation is when the lisp thread calls SendMessage (to send
4008 a window manager command) when a message has been deferred;
4009 the lisp thread gets blocked indefinitely waiting for the
4010 deferred message to be completed, which itself is waiting for
4011 the lisp thread to respond.
4012
4013 Note that we don't want to block the input thread waiting for
4014 a reponse from the lisp thread (although that would at least
4015 solve the deadlock problem above), because we want to be able
4016 to receive C-g to interrupt the lisp thread. */
4017 cancel_all_deferred_msgs ();
4018 }
4019 }
4020
4021 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4022 }
4023
4024 /* Main window procedure */
4025
4026 LRESULT CALLBACK
4027 w32_wnd_proc (hwnd, msg, wParam, lParam)
4028 HWND hwnd;
4029 UINT msg;
4030 WPARAM wParam;
4031 LPARAM lParam;
4032 {
4033 struct frame *f;
4034 struct w32_display_info *dpyinfo = &one_w32_display_info;
4035 W32Msg wmsg;
4036 int windows_translate;
4037 int key;
4038
4039 /* Note that it is okay to call x_window_to_frame, even though we are
4040 not running in the main lisp thread, because frame deletion
4041 requires the lisp thread to synchronize with this thread. Thus, if
4042 a frame struct is returned, it can be used without concern that the
4043 lisp thread might make it disappear while we are using it.
4044
4045 NB. Walking the frame list in this thread is safe (as long as
4046 writes of Lisp_Object slots are atomic, which they are on Windows).
4047 Although delete-frame can destructively modify the frame list while
4048 we are walking it, a garbage collection cannot occur until after
4049 delete-frame has synchronized with this thread.
4050
4051 It is also safe to use functions that make GDI calls, such as
4052 w32_clear_rect, because these functions must obtain a DC handle
4053 from the frame struct using get_frame_dc which is thread-aware. */
4054
4055 switch (msg)
4056 {
4057 case WM_ERASEBKGND:
4058 f = x_window_to_frame (dpyinfo, hwnd);
4059 if (f)
4060 {
4061 HDC hdc = get_frame_dc (f);
4062 GetUpdateRect (hwnd, &wmsg.rect, FALSE);
4063 w32_clear_rect (f, hdc, &wmsg.rect);
4064 release_frame_dc (f, hdc);
4065
4066 #if defined (W32_DEBUG_DISPLAY)
4067 DebPrint (("WM_ERASEBKGND: erasing %d,%d-%d,%d\n",
4068 wmsg.rect.left, wmsg.rect.top, wmsg.rect.right,
4069 wmsg.rect.bottom));
4070 #endif /* W32_DEBUG_DISPLAY */
4071 }
4072 return 1;
4073 case WM_PALETTECHANGED:
4074 /* ignore our own changes */
4075 if ((HWND)wParam != hwnd)
4076 {
4077 f = x_window_to_frame (dpyinfo, hwnd);
4078 if (f)
4079 /* get_frame_dc will realize our palette and force all
4080 frames to be redrawn if needed. */
4081 release_frame_dc (f, get_frame_dc (f));
4082 }
4083 return 0;
4084 case WM_PAINT:
4085 {
4086 PAINTSTRUCT paintStruct;
4087 RECT update_rect;
4088
4089 /* MSDN Docs say not to call BeginPaint if GetUpdateRect
4090 fails. Apparently this can happen under some
4091 circumstances. */
4092 if (!w32_strict_painting || GetUpdateRect (hwnd, &update_rect, FALSE))
4093 {
4094 enter_crit ();
4095 BeginPaint (hwnd, &paintStruct);
4096
4097 if (w32_strict_painting)
4098 /* The rectangles returned by GetUpdateRect and BeginPaint
4099 do not always match. GetUpdateRect seems to be the
4100 more reliable of the two. */
4101 wmsg.rect = update_rect;
4102 else
4103 wmsg.rect = paintStruct.rcPaint;
4104
4105 #if defined (W32_DEBUG_DISPLAY)
4106 DebPrint (("WM_PAINT: painting %d,%d-%d,%d\n", wmsg.rect.left,
4107 wmsg.rect.top, wmsg.rect.right, wmsg.rect.bottom));
4108 DebPrint (("WM_PAINT: update region is %d,%d-%d,%d\n",
4109 update_rect.left, update_rect.top,
4110 update_rect.right, update_rect.bottom));
4111 #endif
4112 EndPaint (hwnd, &paintStruct);
4113 leave_crit ();
4114
4115 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4116
4117 return 0;
4118 }
4119
4120 /* If GetUpdateRect returns 0 (meaning there is no update
4121 region), assume the whole window needs to be repainted. */
4122 GetClientRect(hwnd, &wmsg.rect);
4123 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4124 return 0;
4125 }
4126
4127 case WM_INPUTLANGCHANGE:
4128 /* Inform lisp thread of keyboard layout changes. */
4129 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4130
4131 /* Clear dead keys in the keyboard state; for simplicity only
4132 preserve modifier key states. */
4133 {
4134 int i;
4135 BYTE keystate[256];
4136
4137 GetKeyboardState (keystate);
4138 for (i = 0; i < 256; i++)
4139 if (1
4140 && i != VK_SHIFT
4141 && i != VK_LSHIFT
4142 && i != VK_RSHIFT
4143 && i != VK_CAPITAL
4144 && i != VK_NUMLOCK
4145 && i != VK_SCROLL
4146 && i != VK_CONTROL
4147 && i != VK_LCONTROL
4148 && i != VK_RCONTROL
4149 && i != VK_MENU
4150 && i != VK_LMENU
4151 && i != VK_RMENU
4152 && i != VK_LWIN
4153 && i != VK_RWIN)
4154 keystate[i] = 0;
4155 SetKeyboardState (keystate);
4156 }
4157 goto dflt;
4158
4159 case WM_HOTKEY:
4160 /* Synchronize hot keys with normal input. */
4161 PostMessage (hwnd, WM_KEYDOWN, HIWORD (lParam), 0);
4162 return (0);
4163
4164 case WM_KEYUP:
4165 case WM_SYSKEYUP:
4166 record_keyup (wParam, lParam);
4167 goto dflt;
4168
4169 case WM_KEYDOWN:
4170 case WM_SYSKEYDOWN:
4171 /* Ignore keystrokes we fake ourself; see below. */
4172 if (dpyinfo->faked_key == wParam)
4173 {
4174 dpyinfo->faked_key = 0;
4175 /* Make sure TranslateMessage sees them though (as long as
4176 they don't produce WM_CHAR messages). This ensures that
4177 indicator lights are toggled promptly on Windows 9x, for
4178 example. */
4179 if (lispy_function_keys[wParam] != 0)
4180 {
4181 windows_translate = 1;
4182 goto translate;
4183 }
4184 return 0;
4185 }
4186
4187 /* Synchronize modifiers with current keystroke. */
4188 sync_modifiers ();
4189 record_keydown (wParam, lParam);
4190 wParam = map_keypad_keys (wParam, (lParam & 0x1000000L) != 0);
4191
4192 windows_translate = 0;
4193
4194 switch (wParam)
4195 {
4196 case VK_LWIN:
4197 if (NILP (Vw32_pass_lwindow_to_system))
4198 {
4199 /* Prevent system from acting on keyup (which opens the
4200 Start menu if no other key was pressed) by simulating a
4201 press of Space which we will ignore. */
4202 if (GetAsyncKeyState (wParam) & 1)
4203 {
4204 if (NUMBERP (Vw32_phantom_key_code))
4205 key = XUINT (Vw32_phantom_key_code) & 255;
4206 else
4207 key = VK_SPACE;
4208 dpyinfo->faked_key = key;
4209 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
4210 }
4211 }
4212 if (!NILP (Vw32_lwindow_modifier))
4213 return 0;
4214 break;
4215 case VK_RWIN:
4216 if (NILP (Vw32_pass_rwindow_to_system))
4217 {
4218 if (GetAsyncKeyState (wParam) & 1)
4219 {
4220 if (NUMBERP (Vw32_phantom_key_code))
4221 key = XUINT (Vw32_phantom_key_code) & 255;
4222 else
4223 key = VK_SPACE;
4224 dpyinfo->faked_key = key;
4225 keybd_event (key, (BYTE) MapVirtualKey (key, 0), 0, 0);
4226 }
4227 }
4228 if (!NILP (Vw32_rwindow_modifier))
4229 return 0;
4230 break;
4231 case VK_APPS:
4232 if (!NILP (Vw32_apps_modifier))
4233 return 0;
4234 break;
4235 case VK_MENU:
4236 if (NILP (Vw32_pass_alt_to_system))
4237 /* Prevent DefWindowProc from activating the menu bar if an
4238 Alt key is pressed and released by itself. */
4239 return 0;
4240 windows_translate = 1;
4241 break;
4242 case VK_CAPITAL:
4243 /* Decide whether to treat as modifier or function key. */
4244 if (NILP (Vw32_enable_caps_lock))
4245 goto disable_lock_key;
4246 windows_translate = 1;
4247 break;
4248 case VK_NUMLOCK:
4249 /* Decide whether to treat as modifier or function key. */
4250 if (NILP (Vw32_enable_num_lock))
4251 goto disable_lock_key;
4252 windows_translate = 1;
4253 break;
4254 case VK_SCROLL:
4255 /* Decide whether to treat as modifier or function key. */
4256 if (NILP (Vw32_scroll_lock_modifier))
4257 goto disable_lock_key;
4258 windows_translate = 1;
4259 break;
4260 disable_lock_key:
4261 /* Ensure the appropriate lock key state (and indicator light)
4262 remains in the same state. We do this by faking another
4263 press of the relevant key. Apparently, this really is the
4264 only way to toggle the state of the indicator lights. */
4265 dpyinfo->faked_key = wParam;
4266 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
4267 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
4268 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
4269 KEYEVENTF_EXTENDEDKEY | 0, 0);
4270 keybd_event ((BYTE) wParam, (BYTE) MapVirtualKey (wParam, 0),
4271 KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
4272 /* Ensure indicator lights are updated promptly on Windows 9x
4273 (TranslateMessage apparently does this), after forwarding
4274 input event. */
4275 post_character_message (hwnd, msg, wParam, lParam,
4276 w32_get_key_modifiers (wParam, lParam));
4277 windows_translate = 1;
4278 break;
4279 case VK_CONTROL:
4280 case VK_SHIFT:
4281 case VK_PROCESSKEY: /* Generated by IME. */
4282 windows_translate = 1;
4283 break;
4284 case VK_CANCEL:
4285 /* Windows maps Ctrl-Pause (aka Ctrl-Break) into VK_CANCEL,
4286 which is confusing for purposes of key binding; convert
4287 VK_CANCEL events into VK_PAUSE events. */
4288 wParam = VK_PAUSE;
4289 break;
4290 case VK_PAUSE:
4291 /* Windows maps Ctrl-NumLock into VK_PAUSE, which is confusing
4292 for purposes of key binding; convert these back into
4293 VK_NUMLOCK events, at least when we want to see NumLock key
4294 presses. (Note that there is never any possibility that
4295 VK_PAUSE with Ctrl really is C-Pause as per above.) */
4296 if (NILP (Vw32_enable_num_lock) && modifier_set (VK_CONTROL))
4297 wParam = VK_NUMLOCK;
4298 break;
4299 default:
4300 /* If not defined as a function key, change it to a WM_CHAR message. */
4301 if (lispy_function_keys[wParam] == 0)
4302 {
4303 DWORD modifiers = construct_console_modifiers ();
4304
4305 if (!NILP (Vw32_recognize_altgr)
4306 && modifier_set (VK_LCONTROL) && modifier_set (VK_RMENU))
4307 {
4308 /* Always let TranslateMessage handle AltGr key chords;
4309 for some reason, ToAscii doesn't always process AltGr
4310 chords correctly. */
4311 windows_translate = 1;
4312 }
4313 else if ((modifiers & (~SHIFT_PRESSED & ~CAPSLOCK_ON)) != 0)
4314 {
4315 /* Handle key chords including any modifiers other
4316 than shift directly, in order to preserve as much
4317 modifier information as possible. */
4318 if ('A' <= wParam && wParam <= 'Z')
4319 {
4320 /* Don't translate modified alphabetic keystrokes,
4321 so the user doesn't need to constantly switch
4322 layout to type control or meta keystrokes when
4323 the normal layout translates alphabetic
4324 characters to non-ascii characters. */
4325 if (!modifier_set (VK_SHIFT))
4326 wParam += ('a' - 'A');
4327 msg = WM_CHAR;
4328 }
4329 else
4330 {
4331 /* Try to handle other keystrokes by determining the
4332 base character (ie. translating the base key plus
4333 shift modifier). */
4334 int add;
4335 int isdead = 0;
4336 KEY_EVENT_RECORD key;
4337
4338 key.bKeyDown = TRUE;
4339 key.wRepeatCount = 1;
4340 key.wVirtualKeyCode = wParam;
4341 key.wVirtualScanCode = (lParam & 0xFF0000) >> 16;
4342 key.uChar.AsciiChar = 0;
4343 key.dwControlKeyState = modifiers;
4344
4345 add = w32_kbd_patch_key (&key);
4346 /* 0 means an unrecognised keycode, negative means
4347 dead key. Ignore both. */
4348 while (--add >= 0)
4349 {
4350 /* Forward asciified character sequence. */
4351 post_character_message
4352 (hwnd, WM_CHAR, key.uChar.AsciiChar, lParam,
4353 w32_get_key_modifiers (wParam, lParam));
4354 w32_kbd_patch_key (&key);
4355 }
4356 return 0;
4357 }
4358 }
4359 else
4360 {
4361 /* Let TranslateMessage handle everything else. */
4362 windows_translate = 1;
4363 }
4364 }
4365 }
4366
4367 translate:
4368 if (windows_translate)
4369 {
4370 MSG windows_msg = { hwnd, msg, wParam, lParam, 0, {0,0} };
4371
4372 windows_msg.time = GetMessageTime ();
4373 TranslateMessage (&windows_msg);
4374 goto dflt;
4375 }
4376
4377 /* Fall through */
4378
4379 case WM_SYSCHAR:
4380 case WM_CHAR:
4381 post_character_message (hwnd, msg, wParam, lParam,
4382 w32_get_key_modifiers (wParam, lParam));
4383 break;
4384
4385 /* Simulate middle mouse button events when left and right buttons
4386 are used together, but only if user has two button mouse. */
4387 case WM_LBUTTONDOWN:
4388 case WM_RBUTTONDOWN:
4389 if (XINT (Vw32_num_mouse_buttons) > 2)
4390 goto handle_plain_button;
4391
4392 {
4393 int this = (msg == WM_LBUTTONDOWN) ? LMOUSE : RMOUSE;
4394 int other = (msg == WM_LBUTTONDOWN) ? RMOUSE : LMOUSE;
4395
4396 if (button_state & this)
4397 return 0;
4398
4399 if (button_state == 0)
4400 SetCapture (hwnd);
4401
4402 button_state |= this;
4403
4404 if (button_state & other)
4405 {
4406 if (mouse_button_timer)
4407 {
4408 KillTimer (hwnd, mouse_button_timer);
4409 mouse_button_timer = 0;
4410
4411 /* Generate middle mouse event instead. */
4412 msg = WM_MBUTTONDOWN;
4413 button_state |= MMOUSE;
4414 }
4415 else if (button_state & MMOUSE)
4416 {
4417 /* Ignore button event if we've already generated a
4418 middle mouse down event. This happens if the
4419 user releases and press one of the two buttons
4420 after we've faked a middle mouse event. */
4421 return 0;
4422 }
4423 else
4424 {
4425 /* Flush out saved message. */
4426 post_msg (&saved_mouse_button_msg);
4427 }
4428 wmsg.dwModifiers = w32_get_modifiers ();
4429 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4430
4431 /* Clear message buffer. */
4432 saved_mouse_button_msg.msg.hwnd = 0;
4433 }
4434 else
4435 {
4436 /* Hold onto message for now. */
4437 mouse_button_timer =
4438 SetTimer (hwnd, MOUSE_BUTTON_ID,
4439 XINT (Vw32_mouse_button_tolerance), NULL);
4440 saved_mouse_button_msg.msg.hwnd = hwnd;
4441 saved_mouse_button_msg.msg.message = msg;
4442 saved_mouse_button_msg.msg.wParam = wParam;
4443 saved_mouse_button_msg.msg.lParam = lParam;
4444 saved_mouse_button_msg.msg.time = GetMessageTime ();
4445 saved_mouse_button_msg.dwModifiers = w32_get_modifiers ();
4446 }
4447 }
4448 return 0;
4449
4450 case WM_LBUTTONUP:
4451 case WM_RBUTTONUP:
4452 if (XINT (Vw32_num_mouse_buttons) > 2)
4453 goto handle_plain_button;
4454
4455 {
4456 int this = (msg == WM_LBUTTONUP) ? LMOUSE : RMOUSE;
4457 int other = (msg == WM_LBUTTONUP) ? RMOUSE : LMOUSE;
4458
4459 if ((button_state & this) == 0)
4460 return 0;
4461
4462 button_state &= ~this;
4463
4464 if (button_state & MMOUSE)
4465 {
4466 /* Only generate event when second button is released. */
4467 if ((button_state & other) == 0)
4468 {
4469 msg = WM_MBUTTONUP;
4470 button_state &= ~MMOUSE;
4471
4472 if (button_state) abort ();
4473 }
4474 else
4475 return 0;
4476 }
4477 else
4478 {
4479 /* Flush out saved message if necessary. */
4480 if (saved_mouse_button_msg.msg.hwnd)
4481 {
4482 post_msg (&saved_mouse_button_msg);
4483 }
4484 }
4485 wmsg.dwModifiers = w32_get_modifiers ();
4486 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4487
4488 /* Always clear message buffer and cancel timer. */
4489 saved_mouse_button_msg.msg.hwnd = 0;
4490 KillTimer (hwnd, mouse_button_timer);
4491 mouse_button_timer = 0;
4492
4493 if (button_state == 0)
4494 ReleaseCapture ();
4495 }
4496 return 0;
4497
4498 case WM_MBUTTONDOWN:
4499 case WM_MBUTTONUP:
4500 handle_plain_button:
4501 {
4502 BOOL up;
4503 int button;
4504
4505 if (parse_button (msg, &button, &up))
4506 {
4507 if (up) ReleaseCapture ();
4508 else SetCapture (hwnd);
4509 button = (button == 0) ? LMOUSE :
4510 ((button == 1) ? MMOUSE : RMOUSE);
4511 if (up)
4512 button_state &= ~button;
4513 else
4514 button_state |= button;
4515 }
4516 }
4517
4518 wmsg.dwModifiers = w32_get_modifiers ();
4519 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4520 return 0;
4521
4522 case WM_VSCROLL:
4523 case WM_MOUSEMOVE:
4524 if (XINT (Vw32_mouse_move_interval) <= 0
4525 || (msg == WM_MOUSEMOVE && button_state == 0))
4526 {
4527 wmsg.dwModifiers = w32_get_modifiers ();
4528 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4529 return 0;
4530 }
4531
4532 /* Hang onto mouse move and scroll messages for a bit, to avoid
4533 sending such events to Emacs faster than it can process them.
4534 If we get more events before the timer from the first message
4535 expires, we just replace the first message. */
4536
4537 if (saved_mouse_move_msg.msg.hwnd == 0)
4538 mouse_move_timer =
4539 SetTimer (hwnd, MOUSE_MOVE_ID,
4540 XINT (Vw32_mouse_move_interval), NULL);
4541
4542 /* Hold onto message for now. */
4543 saved_mouse_move_msg.msg.hwnd = hwnd;
4544 saved_mouse_move_msg.msg.message = msg;
4545 saved_mouse_move_msg.msg.wParam = wParam;
4546 saved_mouse_move_msg.msg.lParam = lParam;
4547 saved_mouse_move_msg.msg.time = GetMessageTime ();
4548 saved_mouse_move_msg.dwModifiers = w32_get_modifiers ();
4549
4550 return 0;
4551
4552 case WM_MOUSEWHEEL:
4553 wmsg.dwModifiers = w32_get_modifiers ();
4554 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4555 return 0;
4556
4557 case WM_DROPFILES:
4558 wmsg.dwModifiers = w32_get_modifiers ();
4559 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4560 return 0;
4561
4562 case WM_TIMER:
4563 /* Flush out saved messages if necessary. */
4564 if (wParam == mouse_button_timer)
4565 {
4566 if (saved_mouse_button_msg.msg.hwnd)
4567 {
4568 post_msg (&saved_mouse_button_msg);
4569 saved_mouse_button_msg.msg.hwnd = 0;
4570 }
4571 KillTimer (hwnd, mouse_button_timer);
4572 mouse_button_timer = 0;
4573 }
4574 else if (wParam == mouse_move_timer)
4575 {
4576 if (saved_mouse_move_msg.msg.hwnd)
4577 {
4578 post_msg (&saved_mouse_move_msg);
4579 saved_mouse_move_msg.msg.hwnd = 0;
4580 }
4581 KillTimer (hwnd, mouse_move_timer);
4582 mouse_move_timer = 0;
4583 }
4584 return 0;
4585
4586 case WM_NCACTIVATE:
4587 /* Windows doesn't send us focus messages when putting up and
4588 taking down a system popup dialog as for Ctrl-Alt-Del on Windows 95.
4589 The only indication we get that something happened is receiving
4590 this message afterwards. So this is a good time to reset our
4591 keyboard modifiers' state. */
4592 reset_modifiers ();
4593 goto dflt;
4594
4595 case WM_INITMENU:
4596 button_state = 0;
4597 ReleaseCapture ();
4598 /* We must ensure menu bar is fully constructed and up to date
4599 before allowing user interaction with it. To achieve this
4600 we send this message to the lisp thread and wait for a
4601 reply (whose value is not actually needed) to indicate that
4602 the menu bar is now ready for use, so we can now return.
4603
4604 To remain responsive in the meantime, we enter a nested message
4605 loop that can process all other messages.
4606
4607 However, we skip all this if the message results from calling
4608 TrackPopupMenu - in fact, we must NOT attempt to send the lisp
4609 thread a message because it is blocked on us at this point. We
4610 set menubar_active before calling TrackPopupMenu to indicate
4611 this (there is no possibility of confusion with real menubar
4612 being active). */
4613
4614 f = x_window_to_frame (dpyinfo, hwnd);
4615 if (f
4616 && (f->output_data.w32->menubar_active
4617 /* We can receive this message even in the absence of a
4618 menubar (ie. when the system menu is activated) - in this
4619 case we do NOT want to forward the message, otherwise it
4620 will cause the menubar to suddenly appear when the user
4621 had requested it to be turned off! */
4622 || f->output_data.w32->menubar_widget == NULL))
4623 return 0;
4624
4625 {
4626 deferred_msg msg_buf;
4627
4628 /* Detect if message has already been deferred; in this case
4629 we cannot return any sensible value to ignore this. */
4630 if (find_deferred_msg (hwnd, msg) != NULL)
4631 abort ();
4632
4633 return send_deferred_msg (&msg_buf, hwnd, msg, wParam, lParam);
4634 }
4635
4636 case WM_EXITMENULOOP:
4637 f = x_window_to_frame (dpyinfo, hwnd);
4638
4639 /* Indicate that menubar can be modified again. */
4640 if (f)
4641 f->output_data.w32->menubar_active = 0;
4642 goto dflt;
4643
4644 case WM_MENUSELECT:
4645 wmsg.dwModifiers = w32_get_modifiers ();
4646 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4647 return 0;
4648
4649 case WM_MEASUREITEM:
4650 f = x_window_to_frame (dpyinfo, hwnd);
4651 if (f)
4652 {
4653 MEASUREITEMSTRUCT * pMis = (MEASUREITEMSTRUCT *) lParam;
4654
4655 if (pMis->CtlType == ODT_MENU)
4656 {
4657 /* Work out dimensions for popup menu titles. */
4658 char * title = (char *) pMis->itemData;
4659 HDC hdc = GetDC (hwnd);
4660 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
4661 LOGFONT menu_logfont;
4662 HFONT old_font;
4663 SIZE size;
4664
4665 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
4666 menu_logfont.lfWeight = FW_BOLD;
4667 menu_font = CreateFontIndirect (&menu_logfont);
4668 old_font = SelectObject (hdc, menu_font);
4669
4670 pMis->itemHeight = GetSystemMetrics (SM_CYMENUSIZE);
4671 if (title)
4672 {
4673 GetTextExtentPoint32 (hdc, title, strlen (title), &size);
4674 pMis->itemWidth = size.cx;
4675 if (pMis->itemHeight < size.cy)
4676 pMis->itemHeight = size.cy;
4677 }
4678 else
4679 pMis->itemWidth = 0;
4680
4681 SelectObject (hdc, old_font);
4682 DeleteObject (menu_font);
4683 ReleaseDC (hwnd, hdc);
4684 return TRUE;
4685 }
4686 }
4687 return 0;
4688
4689 case WM_DRAWITEM:
4690 f = x_window_to_frame (dpyinfo, hwnd);
4691 if (f)
4692 {
4693 DRAWITEMSTRUCT * pDis = (DRAWITEMSTRUCT *) lParam;
4694
4695 if (pDis->CtlType == ODT_MENU)
4696 {
4697 /* Draw popup menu title. */
4698 char * title = (char *) pDis->itemData;
4699 if (title)
4700 {
4701 HDC hdc = pDis->hDC;
4702 HFONT menu_font = GetCurrentObject (hdc, OBJ_FONT);
4703 LOGFONT menu_logfont;
4704 HFONT old_font;
4705
4706 GetObject (menu_font, sizeof (menu_logfont), &menu_logfont);
4707 menu_logfont.lfWeight = FW_BOLD;
4708 menu_font = CreateFontIndirect (&menu_logfont);
4709 old_font = SelectObject (hdc, menu_font);
4710
4711 /* Always draw title as if not selected. */
4712 ExtTextOut (hdc,
4713 pDis->rcItem.left
4714 + GetSystemMetrics (SM_CXMENUCHECK),
4715 pDis->rcItem.top,
4716 ETO_OPAQUE, &pDis->rcItem,
4717 title, strlen (title), NULL);
4718
4719 SelectObject (hdc, old_font);
4720 DeleteObject (menu_font);
4721 }
4722 return TRUE;
4723 }
4724 }
4725 return 0;
4726
4727 #if 0
4728 /* Still not right - can't distinguish between clicks in the
4729 client area of the frame from clicks forwarded from the scroll
4730 bars - may have to hook WM_NCHITTEST to remember the mouse
4731 position and then check if it is in the client area ourselves. */
4732 case WM_MOUSEACTIVATE:
4733 /* Discard the mouse click that activates a frame, allowing the
4734 user to click anywhere without changing point (or worse!).
4735 Don't eat mouse clicks on scrollbars though!! */
4736 if (LOWORD (lParam) == HTCLIENT )
4737 return MA_ACTIVATEANDEAT;
4738 goto dflt;
4739 #endif
4740
4741 case WM_ACTIVATEAPP:
4742 case WM_ACTIVATE:
4743 case WM_WINDOWPOSCHANGED:
4744 case WM_SHOWWINDOW:
4745 /* Inform lisp thread that a frame might have just been obscured
4746 or exposed, so should recheck visibility of all frames. */
4747 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4748 goto dflt;
4749
4750 case WM_SETFOCUS:
4751 dpyinfo->faked_key = 0;
4752 reset_modifiers ();
4753 register_hot_keys (hwnd);
4754 goto command;
4755 case WM_KILLFOCUS:
4756 unregister_hot_keys (hwnd);
4757 button_state = 0;
4758 ReleaseCapture ();
4759 case WM_MOVE:
4760 case WM_SIZE:
4761 case WM_COMMAND:
4762 command:
4763 wmsg.dwModifiers = w32_get_modifiers ();
4764 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4765 goto dflt;
4766
4767 case WM_CLOSE:
4768 wmsg.dwModifiers = w32_get_modifiers ();
4769 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4770 return 0;
4771
4772 case WM_WINDOWPOSCHANGING:
4773 {
4774 WINDOWPLACEMENT wp;
4775 LPWINDOWPOS lppos = (WINDOWPOS *) lParam;
4776
4777 wp.length = sizeof (WINDOWPLACEMENT);
4778 GetWindowPlacement (hwnd, &wp);
4779
4780 if (wp.showCmd != SW_SHOWMINIMIZED && (lppos->flags & SWP_NOSIZE) == 0)
4781 {
4782 RECT rect;
4783 int wdiff;
4784 int hdiff;
4785 DWORD font_width;
4786 DWORD line_height;
4787 DWORD internal_border;
4788 DWORD scrollbar_extra;
4789 RECT wr;
4790
4791 wp.length = sizeof(wp);
4792 GetWindowRect (hwnd, &wr);
4793
4794 enter_crit ();
4795
4796 font_width = GetWindowLong (hwnd, WND_FONTWIDTH_INDEX);
4797 line_height = GetWindowLong (hwnd, WND_LINEHEIGHT_INDEX);
4798 internal_border = GetWindowLong (hwnd, WND_BORDER_INDEX);
4799 scrollbar_extra = GetWindowLong (hwnd, WND_SCROLLBAR_INDEX);
4800
4801 leave_crit ();
4802
4803 memset (&rect, 0, sizeof (rect));
4804 AdjustWindowRect (&rect, GetWindowLong (hwnd, GWL_STYLE),
4805 GetMenu (hwnd) != NULL);
4806
4807 /* Force width and height of client area to be exact
4808 multiples of the character cell dimensions. */
4809 wdiff = (lppos->cx - (rect.right - rect.left)
4810 - 2 * internal_border - scrollbar_extra)
4811 % font_width;
4812 hdiff = (lppos->cy - (rect.bottom - rect.top)
4813 - 2 * internal_border)
4814 % line_height;
4815
4816 if (wdiff || hdiff)
4817 {
4818 /* For right/bottom sizing we can just fix the sizes.
4819 However for top/left sizing we will need to fix the X
4820 and Y positions as well. */
4821
4822 lppos->cx -= wdiff;
4823 lppos->cy -= hdiff;
4824
4825 if (wp.showCmd != SW_SHOWMAXIMIZED
4826 && (lppos->flags & SWP_NOMOVE) == 0)
4827 {
4828 if (lppos->x != wr.left || lppos->y != wr.top)
4829 {
4830 lppos->x += wdiff;
4831 lppos->y += hdiff;
4832 }
4833 else
4834 {
4835 lppos->flags |= SWP_NOMOVE;
4836 }
4837 }
4838
4839 return 0;
4840 }
4841 }
4842 }
4843
4844 goto dflt;
4845
4846 case WM_GETMINMAXINFO:
4847 /* Hack to correct bug that allows Emacs frames to be resized
4848 below the Minimum Tracking Size. */
4849 ((LPMINMAXINFO) lParam)->ptMinTrackSize.y++;
4850 return 0;
4851
4852 case WM_EMACS_CREATESCROLLBAR:
4853 return (LRESULT) w32_createscrollbar ((struct frame *) wParam,
4854 (struct scroll_bar *) lParam);
4855
4856 case WM_EMACS_SHOWWINDOW:
4857 return ShowWindow ((HWND) wParam, (WPARAM) lParam);
4858
4859 case WM_EMACS_SETFOREGROUND:
4860 {
4861 HWND foreground_window;
4862 DWORD foreground_thread, retval;
4863
4864 /* On NT 5.0, and apparently Windows 98, it is necessary to
4865 attach to the thread that currently has focus in order to
4866 pull the focus away from it. */
4867 foreground_window = GetForegroundWindow ();
4868 foreground_thread = GetWindowThreadProcessId (foreground_window, NULL);
4869 if (!foreground_window
4870 || foreground_thread == GetCurrentThreadId ()
4871 || !AttachThreadInput (GetCurrentThreadId (),
4872 foreground_thread, TRUE))
4873 foreground_thread = 0;
4874
4875 retval = SetForegroundWindow ((HWND) wParam);
4876
4877 /* Detach from the previous foreground thread. */
4878 if (foreground_thread)
4879 AttachThreadInput (GetCurrentThreadId (),
4880 foreground_thread, FALSE);
4881
4882 return retval;
4883 }
4884
4885 case WM_EMACS_SETWINDOWPOS:
4886 {
4887 WINDOWPOS * pos = (WINDOWPOS *) wParam;
4888 return SetWindowPos (hwnd, pos->hwndInsertAfter,
4889 pos->x, pos->y, pos->cx, pos->cy, pos->flags);
4890 }
4891
4892 case WM_EMACS_DESTROYWINDOW:
4893 DragAcceptFiles ((HWND) wParam, FALSE);
4894 return DestroyWindow ((HWND) wParam);
4895
4896 case WM_EMACS_TRACKPOPUPMENU:
4897 {
4898 UINT flags;
4899 POINT *pos;
4900 int retval;
4901 pos = (POINT *)lParam;
4902 flags = TPM_CENTERALIGN;
4903 if (button_state & LMOUSE)
4904 flags |= TPM_LEFTBUTTON;
4905 else if (button_state & RMOUSE)
4906 flags |= TPM_RIGHTBUTTON;
4907
4908 /* Remember we did a SetCapture on the initial mouse down event,
4909 so for safety, we make sure the capture is cancelled now. */
4910 ReleaseCapture ();
4911 button_state = 0;
4912
4913 /* Use menubar_active to indicate that WM_INITMENU is from
4914 TrackPopupMenu below, and should be ignored. */
4915 f = x_window_to_frame (dpyinfo, hwnd);
4916 if (f)
4917 f->output_data.w32->menubar_active = 1;
4918
4919 if (TrackPopupMenu ((HMENU)wParam, flags, pos->x, pos->y,
4920 0, hwnd, NULL))
4921 {
4922 MSG amsg;
4923 /* Eat any mouse messages during popupmenu */
4924 while (PeekMessage (&amsg, hwnd, WM_MOUSEFIRST, WM_MOUSELAST,
4925 PM_REMOVE));
4926 /* Get the menu selection, if any */
4927 if (PeekMessage (&amsg, hwnd, WM_COMMAND, WM_COMMAND, PM_REMOVE))
4928 {
4929 retval = LOWORD (amsg.wParam);
4930 }
4931 else
4932 {
4933 retval = 0;
4934 }
4935 }
4936 else
4937 {
4938 retval = -1;
4939 }
4940
4941 return retval;
4942 }
4943
4944 default:
4945 /* Check for messages registered at runtime. */
4946 if (msg == msh_mousewheel)
4947 {
4948 wmsg.dwModifiers = w32_get_modifiers ();
4949 my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
4950 return 0;
4951 }
4952
4953 dflt:
4954 return DefWindowProc (hwnd, msg, wParam, lParam);
4955 }
4956
4957
4958 /* The most common default return code for handled messages is 0. */
4959 return 0;
4960 }
4961
4962 void
4963 my_create_window (f)
4964 struct frame * f;
4965 {
4966 MSG msg;
4967
4968 if (!PostThreadMessage (dwWindowsThreadId, WM_EMACS_CREATEWINDOW, (WPARAM)f, 0))
4969 abort ();
4970 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
4971 }
4972
4973 /* Create and set up the w32 window for frame F. */
4974
4975 static void
4976 w32_window (f, window_prompting, minibuffer_only)
4977 struct frame *f;
4978 long window_prompting;
4979 int minibuffer_only;
4980 {
4981 BLOCK_INPUT;
4982
4983 /* Use the resource name as the top-level window name
4984 for looking up resources. Make a non-Lisp copy
4985 for the window manager, so GC relocation won't bother it.
4986
4987 Elsewhere we specify the window name for the window manager. */
4988
4989 {
4990 char *str = (char *) XSTRING (Vx_resource_name)->data;
4991 f->namebuf = (char *) xmalloc (strlen (str) + 1);
4992 strcpy (f->namebuf, str);
4993 }
4994
4995 my_create_window (f);
4996
4997 validate_x_resource_name ();
4998
4999 /* x_set_name normally ignores requests to set the name if the
5000 requested name is the same as the current name. This is the one
5001 place where that assumption isn't correct; f->name is set, but
5002 the server hasn't been told. */
5003 {
5004 Lisp_Object name;
5005 int explicit = f->explicit_name;
5006
5007 f->explicit_name = 0;
5008 name = f->name;
5009 f->name = Qnil;
5010 x_set_name (f, name, explicit);
5011 }
5012
5013 UNBLOCK_INPUT;
5014
5015 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
5016 initialize_frame_menubar (f);
5017
5018 if (FRAME_W32_WINDOW (f) == 0)
5019 error ("Unable to create window");
5020 }
5021
5022 /* Handle the icon stuff for this window. Perhaps later we might
5023 want an x_set_icon_position which can be called interactively as
5024 well. */
5025
5026 static void
5027 x_icon (f, parms)
5028 struct frame *f;
5029 Lisp_Object parms;
5030 {
5031 Lisp_Object icon_x, icon_y;
5032
5033 /* Set the position of the icon. Note that Windows 95 groups all
5034 icons in the tray. */
5035 icon_x = w32_get_arg (parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
5036 icon_y = w32_get_arg (parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
5037 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
5038 {
5039 CHECK_NUMBER (icon_x, 0);
5040 CHECK_NUMBER (icon_y, 0);
5041 }
5042 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
5043 error ("Both left and top icon corners of icon must be specified");
5044
5045 BLOCK_INPUT;
5046
5047 if (! EQ (icon_x, Qunbound))
5048 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
5049
5050 #if 0 /* TODO */
5051 /* Start up iconic or window? */
5052 x_wm_set_window_state
5053 (f, (EQ (w32_get_arg (parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL), Qicon)
5054 ? IconicState
5055 : NormalState));
5056
5057 x_text_icon (f, (char *) XSTRING ((!NILP (f->icon_name)
5058 ? f->icon_name
5059 : f->name))->data);
5060 #endif
5061
5062 UNBLOCK_INPUT;
5063 }
5064
5065
5066 static void
5067 x_make_gc (f)
5068 struct frame *f;
5069 {
5070 XGCValues gc_values;
5071
5072 BLOCK_INPUT;
5073
5074 /* Create the GC's of this frame.
5075 Note that many default values are used. */
5076
5077 /* Normal video */
5078 gc_values.font = f->output_data.w32->font;
5079
5080 /* Cursor has cursor-color background, background-color foreground. */
5081 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
5082 gc_values.background = f->output_data.w32->cursor_pixel;
5083 f->output_data.w32->cursor_gc
5084 = XCreateGC (NULL, FRAME_W32_WINDOW (f),
5085 (GCFont | GCForeground | GCBackground),
5086 &gc_values);
5087
5088 /* Reliefs. */
5089 f->output_data.w32->white_relief.gc = 0;
5090 f->output_data.w32->black_relief.gc = 0;
5091
5092 UNBLOCK_INPUT;
5093 }
5094
5095
5096 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
5097 1, 1, 0,
5098 "Make a new window, which is called a \"frame\" in Emacs terms.\n\
5099 Returns an Emacs frame object.\n\
5100 ALIST is an alist of frame parameters.\n\
5101 If the parameters specify that the frame should not have a minibuffer,\n\
5102 and do not specify a specific minibuffer window to use,\n\
5103 then `default-minibuffer-frame' must be a frame whose minibuffer can\n\
5104 be shared by the new frame.\n\
5105 \n\
5106 This function is an internal primitive--use `make-frame' instead.")
5107 (parms)
5108 Lisp_Object parms;
5109 {
5110 struct frame *f;
5111 Lisp_Object frame, tem;
5112 Lisp_Object name;
5113 int minibuffer_only = 0;
5114 long window_prompting = 0;
5115 int width, height;
5116 int count = specpdl_ptr - specpdl;
5117 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5118 Lisp_Object display;
5119 struct w32_display_info *dpyinfo = NULL;
5120 Lisp_Object parent;
5121 struct kboard *kb;
5122
5123 check_w32 ();
5124
5125 /* Use this general default value to start with
5126 until we know if this frame has a specified name. */
5127 Vx_resource_name = Vinvocation_name;
5128
5129 display = w32_get_arg (parms, Qdisplay, 0, 0, RES_TYPE_STRING);
5130 if (EQ (display, Qunbound))
5131 display = Qnil;
5132 dpyinfo = check_x_display_info (display);
5133 #ifdef MULTI_KBOARD
5134 kb = dpyinfo->kboard;
5135 #else
5136 kb = &the_only_kboard;
5137 #endif
5138
5139 name = w32_get_arg (parms, Qname, "name", "Name", RES_TYPE_STRING);
5140 if (!STRINGP (name)
5141 && ! EQ (name, Qunbound)
5142 && ! NILP (name))
5143 error ("Invalid frame name--not a string or nil");
5144
5145 if (STRINGP (name))
5146 Vx_resource_name = name;
5147
5148 /* See if parent window is specified. */
5149 parent = w32_get_arg (parms, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
5150 if (EQ (parent, Qunbound))
5151 parent = Qnil;
5152 if (! NILP (parent))
5153 CHECK_NUMBER (parent, 0);
5154
5155 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
5156 /* No need to protect DISPLAY because that's not used after passing
5157 it to make_frame_without_minibuffer. */
5158 frame = Qnil;
5159 GCPRO4 (parms, parent, name, frame);
5160 tem = w32_get_arg (parms, Qminibuffer, "minibuffer", "Minibuffer",
5161 RES_TYPE_SYMBOL);
5162 if (EQ (tem, Qnone) || NILP (tem))
5163 f = make_frame_without_minibuffer (Qnil, kb, display);
5164 else if (EQ (tem, Qonly))
5165 {
5166 f = make_minibuffer_frame ();
5167 minibuffer_only = 1;
5168 }
5169 else if (WINDOWP (tem))
5170 f = make_frame_without_minibuffer (tem, kb, display);
5171 else
5172 f = make_frame (1);
5173
5174 XSETFRAME (frame, f);
5175
5176 /* Note that Windows does support scroll bars. */
5177 FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;
5178 /* By default, make scrollbars the system standard width. */
5179 f->scroll_bar_pixel_width = GetSystemMetrics (SM_CXVSCROLL);
5180
5181 f->output_method = output_w32;
5182 f->output_data.w32 =
5183 (struct w32_output *) xmalloc (sizeof (struct w32_output));
5184 bzero (f->output_data.w32, sizeof (struct w32_output));
5185
5186 FRAME_FONTSET (f) = -1;
5187
5188 f->icon_name
5189 = w32_get_arg (parms, Qicon_name, "iconName", "Title", RES_TYPE_STRING);
5190 if (! STRINGP (f->icon_name))
5191 f->icon_name = Qnil;
5192
5193 /* FRAME_W32_DISPLAY_INFO (f) = dpyinfo; */
5194 #ifdef MULTI_KBOARD
5195 FRAME_KBOARD (f) = kb;
5196 #endif
5197
5198 /* Specify the parent under which to make this window. */
5199
5200 if (!NILP (parent))
5201 {
5202 f->output_data.w32->parent_desc = (Window) XFASTINT (parent);
5203 f->output_data.w32->explicit_parent = 1;
5204 }
5205 else
5206 {
5207 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
5208 f->output_data.w32->explicit_parent = 0;
5209 }
5210
5211 /* Set the name; the functions to which we pass f expect the name to
5212 be set. */
5213 if (EQ (name, Qunbound) || NILP (name))
5214 {
5215 f->name = build_string (dpyinfo->w32_id_name);
5216 f->explicit_name = 0;
5217 }
5218 else
5219 {
5220 f->name = name;
5221 f->explicit_name = 1;
5222 /* use the frame's title when getting resources for this frame. */
5223 specbind (Qx_resource_name, name);
5224 }
5225
5226 /* Extract the window parameters from the supplied values
5227 that are needed to determine window geometry. */
5228 {
5229 Lisp_Object font;
5230
5231 font = w32_get_arg (parms, Qfont, "font", "Font", RES_TYPE_STRING);
5232
5233 BLOCK_INPUT;
5234 /* First, try whatever font the caller has specified. */
5235 if (STRINGP (font))
5236 {
5237 tem = Fquery_fontset (font, Qnil);
5238 if (STRINGP (tem))
5239 font = x_new_fontset (f, XSTRING (tem)->data);
5240 else
5241 font = x_new_font (f, XSTRING (font)->data);
5242 }
5243 /* Try out a font which we hope has bold and italic variations. */
5244 if (!STRINGP (font))
5245 font = x_new_font (f, "-*-Courier New-normal-r-*-*-*-100-*-*-c-*-iso8859-1");
5246 if (! STRINGP (font))
5247 font = x_new_font (f, "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1");
5248 /* If those didn't work, look for something which will at least work. */
5249 if (! STRINGP (font))
5250 font = x_new_font (f, "-*-Fixedsys-normal-r-*-*-12-*-*-*-c-*-iso8859-1");
5251 UNBLOCK_INPUT;
5252 if (! STRINGP (font))
5253 font = build_string ("Fixedsys");
5254
5255 x_default_parameter (f, parms, Qfont, font,
5256 "font", "Font", RES_TYPE_STRING);
5257 }
5258
5259 x_default_parameter (f, parms, Qborder_width, make_number (2),
5260 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
5261 /* This defaults to 2 in order to match xterm. We recognize either
5262 internalBorderWidth or internalBorder (which is what xterm calls
5263 it). */
5264 if (NILP (Fassq (Qinternal_border_width, parms)))
5265 {
5266 Lisp_Object value;
5267
5268 value = w32_get_arg (parms, Qinternal_border_width,
5269 "internalBorder", "InternalBorder", RES_TYPE_NUMBER);
5270 if (! EQ (value, Qunbound))
5271 parms = Fcons (Fcons (Qinternal_border_width, value),
5272 parms);
5273 }
5274 /* Default internalBorderWidth to 0 on Windows to match other programs. */
5275 x_default_parameter (f, parms, Qinternal_border_width, make_number (0),
5276 "internalBorderWidth", "InternalBorder", RES_TYPE_NUMBER);
5277 x_default_parameter (f, parms, Qvertical_scroll_bars, Qright,
5278 "verticalScrollBars", "ScrollBars", RES_TYPE_SYMBOL);
5279
5280 /* Also do the stuff which must be set before the window exists. */
5281 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
5282 "foreground", "Foreground", RES_TYPE_STRING);
5283 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
5284 "background", "Background", RES_TYPE_STRING);
5285 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
5286 "pointerColor", "Foreground", RES_TYPE_STRING);
5287 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
5288 "cursorColor", "Foreground", RES_TYPE_STRING);
5289 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
5290 "borderColor", "BorderColor", RES_TYPE_STRING);
5291 x_default_parameter (f, parms, Qscreen_gamma, Qnil,
5292 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
5293 x_default_parameter (f, parms, Qline_spacing, Qnil,
5294 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
5295
5296
5297 /* Init faces before x_default_parameter is called for scroll-bar
5298 parameters because that function calls x_set_scroll_bar_width,
5299 which calls change_frame_size, which calls Fset_window_buffer,
5300 which runs hooks, which call Fvertical_motion. At the end, we
5301 end up in init_iterator with a null face cache, which should not
5302 happen. */
5303 init_frame_faces (f);
5304
5305 x_default_parameter (f, parms, Qmenu_bar_lines, make_number (1),
5306 "menuBar", "MenuBar", RES_TYPE_NUMBER);
5307 x_default_parameter (f, parms, Qtool_bar_lines, make_number (0),
5308 "toolBar", "ToolBar", RES_TYPE_NUMBER);
5309 x_default_parameter (f, parms, Qbuffer_predicate, Qnil,
5310 "bufferPredicate", "BufferPredicate", RES_TYPE_SYMBOL);
5311 x_default_parameter (f, parms, Qtitle, Qnil,
5312 "title", "Title", RES_TYPE_STRING);
5313
5314 f->output_data.w32->dwStyle = WS_OVERLAPPEDWINDOW;
5315 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
5316 window_prompting = x_figure_window_size (f, parms);
5317
5318 if (window_prompting & XNegative)
5319 {
5320 if (window_prompting & YNegative)
5321 f->output_data.w32->win_gravity = SouthEastGravity;
5322 else
5323 f->output_data.w32->win_gravity = NorthEastGravity;
5324 }
5325 else
5326 {
5327 if (window_prompting & YNegative)
5328 f->output_data.w32->win_gravity = SouthWestGravity;
5329 else
5330 f->output_data.w32->win_gravity = NorthWestGravity;
5331 }
5332
5333 f->output_data.w32->size_hint_flags = window_prompting;
5334
5335 tem = w32_get_arg (parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
5336 f->no_split = minibuffer_only || EQ (tem, Qt);
5337
5338 /* Create the window. Add the tool-bar height to the initial frame
5339 height so that the user gets a text display area of the size he
5340 specified with -g or via the registry. Later changes of the
5341 tool-bar height don't change the frame size. This is done so that
5342 users can create tall Emacs frames without having to guess how
5343 tall the tool-bar will get. */
5344 f->height += FRAME_TOOL_BAR_LINES (f);
5345 w32_window (f, window_prompting, minibuffer_only);
5346 x_icon (f, parms);
5347
5348 x_make_gc (f);
5349
5350 /* Now consider the frame official. */
5351 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
5352 Vframe_list = Fcons (frame, Vframe_list);
5353
5354 /* We need to do this after creating the window, so that the
5355 icon-creation functions can say whose icon they're describing. */
5356 x_default_parameter (f, parms, Qicon_type, Qnil,
5357 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
5358
5359 x_default_parameter (f, parms, Qauto_raise, Qnil,
5360 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5361 x_default_parameter (f, parms, Qauto_lower, Qnil,
5362 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5363 x_default_parameter (f, parms, Qcursor_type, Qbox,
5364 "cursorType", "CursorType", RES_TYPE_SYMBOL);
5365 x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
5366 "scrollBarWidth", "ScrollBarWidth", RES_TYPE_NUMBER);
5367
5368 /* Dimensions, especially f->height, must be done via change_frame_size.
5369 Change will not be effected unless different from the current
5370 f->height. */
5371 width = f->width;
5372 height = f->height;
5373 f->height = 0;
5374 SET_FRAME_WIDTH (f, 0);
5375 change_frame_size (f, height, width, 1, 0, 0);
5376
5377 /* Tell the server what size and position, etc, we want, and how
5378 badly we want them. This should be done after we have the menu
5379 bar so that its size can be taken into account. */
5380 BLOCK_INPUT;
5381 x_wm_set_size_hint (f, window_prompting, 0);
5382 UNBLOCK_INPUT;
5383
5384 /* Make the window appear on the frame and enable display, unless
5385 the caller says not to. However, with explicit parent, Emacs
5386 cannot control visibility, so don't try. */
5387 if (! f->output_data.w32->explicit_parent)
5388 {
5389 Lisp_Object visibility;
5390
5391 visibility = w32_get_arg (parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL);
5392 if (EQ (visibility, Qunbound))
5393 visibility = Qt;
5394
5395 if (EQ (visibility, Qicon))
5396 x_iconify_frame (f);
5397 else if (! NILP (visibility))
5398 x_make_frame_visible (f);
5399 else
5400 /* Must have been Qnil. */
5401 ;
5402 }
5403 UNGCPRO;
5404 return unbind_to (count, frame);
5405 }
5406
5407 /* FRAME is used only to get a handle on the X display. We don't pass the
5408 display info directly because we're called from frame.c, which doesn't
5409 know about that structure. */
5410 Lisp_Object
5411 x_get_focus_frame (frame)
5412 struct frame *frame;
5413 {
5414 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (frame);
5415 Lisp_Object xfocus;
5416 if (! dpyinfo->w32_focus_frame)
5417 return Qnil;
5418
5419 XSETFRAME (xfocus, dpyinfo->w32_focus_frame);
5420 return xfocus;
5421 }
5422
5423 DEFUN ("w32-focus-frame", Fw32_focus_frame, Sw32_focus_frame, 1, 1, 0,
5424 "Give FRAME input focus, raising to foreground if necessary.")
5425 (frame)
5426 Lisp_Object frame;
5427 {
5428 x_focus_on_frame (check_x_frame (frame));
5429 return Qnil;
5430 }
5431
5432 \f
5433 /* Return the charset portion of a font name. */
5434 char * xlfd_charset_of_font (char * fontname)
5435 {
5436 char *charset, *encoding;
5437
5438 encoding = strrchr(fontname, '-');
5439 if (!encoding)
5440 return NULL;
5441
5442 *encoding = 0;
5443 charset = strrchr(fontname, '-');
5444 *encoding = '-';
5445
5446 if (!charset || strcmp(charset, "-*-*") == 0)
5447 return NULL;
5448
5449 return charset + 1;
5450 }
5451
5452 struct font_info *w32_load_bdf_font (struct frame *f, char *fontname,
5453 int size, char* filename);
5454 BOOL w32_to_x_font (LOGFONT * lplf, char * lpxstr, int len, char * charset);
5455
5456 struct font_info *
5457 w32_load_system_font (f,fontname,size)
5458 struct frame *f;
5459 char * fontname;
5460 int size;
5461 {
5462 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
5463 Lisp_Object font_names;
5464
5465 /* Get a list of all the fonts that match this name. Once we
5466 have a list of matching fonts, we compare them against the fonts
5467 we already have loaded by comparing names. */
5468 font_names = w32_list_fonts (f, build_string (fontname), size, 100);
5469
5470 if (!NILP (font_names))
5471 {
5472 Lisp_Object tail;
5473 int i;
5474
5475 /* First check if any are already loaded, as that is cheaper
5476 than loading another one. */
5477 for (i = 0; i < dpyinfo->n_fonts; i++)
5478 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
5479 if (dpyinfo->font_table[i].name
5480 && (!strcmp (dpyinfo->font_table[i].name,
5481 XSTRING (XCAR (tail))->data)
5482 || !strcmp (dpyinfo->font_table[i].full_name,
5483 XSTRING (XCAR (tail))->data)))
5484 return (dpyinfo->font_table + i);
5485
5486 fontname = (char *) XSTRING (XCAR (font_names))->data;
5487 }
5488 else if (w32_strict_fontnames)
5489 {
5490 /* If EnumFontFamiliesEx was available, we got a full list of
5491 fonts back so stop now to avoid the possibility of loading a
5492 random font. If we had to fall back to EnumFontFamilies, the
5493 list is incomplete, so continue whether the font we want was
5494 listed or not. */
5495 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
5496 FARPROC enum_font_families_ex
5497 = GetProcAddress (gdi32, "EnumFontFamiliesExA");
5498 if (enum_font_families_ex)
5499 return NULL;
5500 }
5501
5502 /* Load the font and add it to the table. */
5503 {
5504 char *full_name, *encoding, *charset;
5505 XFontStruct *font;
5506 struct font_info *fontp;
5507 LOGFONT lf;
5508 BOOL ok;
5509 int i;
5510
5511 if (!fontname || !x_to_w32_font (fontname, &lf))
5512 return (NULL);
5513
5514 if (!*lf.lfFaceName)
5515 /* If no name was specified for the font, we get a random font
5516 from CreateFontIndirect - this is not particularly
5517 desirable, especially since CreateFontIndirect does not
5518 fill out the missing name in lf, so we never know what we
5519 ended up with. */
5520 return NULL;
5521
5522 font = (XFontStruct *) xmalloc (sizeof (XFontStruct));
5523 bzero (font, sizeof (*font));
5524
5525 /* Set bdf to NULL to indicate that this is a Windows font. */
5526 font->bdf = NULL;
5527
5528 BLOCK_INPUT;
5529
5530 font->hfont = CreateFontIndirect (&lf);
5531
5532 if (font->hfont == NULL)
5533 {
5534 ok = FALSE;
5535 }
5536 else
5537 {
5538 HDC hdc;
5539 HANDLE oldobj;
5540
5541 hdc = GetDC (dpyinfo->root_window);
5542 oldobj = SelectObject (hdc, font->hfont);
5543 ok = GetTextMetrics (hdc, &font->tm);
5544 font->double_byte_p = GetFontLanguageInfo(hdc) & GCP_DBCS;
5545 SelectObject (hdc, oldobj);
5546 ReleaseDC (dpyinfo->root_window, hdc);
5547 /* Fill out details in lf according to the font that was
5548 actually loaded. */
5549 lf.lfHeight = font->tm.tmInternalLeading - font->tm.tmHeight;
5550 lf.lfWidth = font->tm.tmAveCharWidth;
5551 lf.lfWeight = font->tm.tmWeight;
5552 lf.lfItalic = font->tm.tmItalic;
5553 lf.lfCharSet = font->tm.tmCharSet;
5554 lf.lfPitchAndFamily = ((font->tm.tmPitchAndFamily & TMPF_FIXED_PITCH)
5555 ? VARIABLE_PITCH : FIXED_PITCH);
5556 lf.lfOutPrecision = ((font->tm.tmPitchAndFamily & TMPF_VECTOR)
5557 ? OUT_STROKE_PRECIS : OUT_STRING_PRECIS);
5558
5559 w32_cache_char_metrics (font);
5560 }
5561
5562 UNBLOCK_INPUT;
5563
5564 if (!ok)
5565 {
5566 w32_unload_font (dpyinfo, font);
5567 return (NULL);
5568 }
5569
5570 /* Find a free slot in the font table. */
5571 for (i = 0; i < dpyinfo->n_fonts; ++i)
5572 if (dpyinfo->font_table[i].name == NULL)
5573 break;
5574
5575 /* If no free slot found, maybe enlarge the font table. */
5576 if (i == dpyinfo->n_fonts
5577 && dpyinfo->n_fonts == dpyinfo->font_table_size)
5578 {
5579 int sz;
5580 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
5581 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
5582 dpyinfo->font_table
5583 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
5584 }
5585
5586 fontp = dpyinfo->font_table + i;
5587 if (i == dpyinfo->n_fonts)
5588 ++dpyinfo->n_fonts;
5589
5590 /* Now fill in the slots of *FONTP. */
5591 BLOCK_INPUT;
5592 fontp->font = font;
5593 fontp->font_idx = i;
5594 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
5595 bcopy (fontname, fontp->name, strlen (fontname) + 1);
5596
5597 charset = xlfd_charset_of_font (fontname);
5598
5599 /* Work out the font's full name. */
5600 full_name = (char *)xmalloc (100);
5601 if (full_name && w32_to_x_font (&lf, full_name, 100, charset))
5602 fontp->full_name = full_name;
5603 else
5604 {
5605 /* If all else fails - just use the name we used to load it. */
5606 xfree (full_name);
5607 fontp->full_name = fontp->name;
5608 }
5609
5610 fontp->size = FONT_WIDTH (font);
5611 fontp->height = FONT_HEIGHT (font);
5612
5613 /* The slot `encoding' specifies how to map a character
5614 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
5615 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
5616 (0:0x20..0x7F, 1:0xA0..0xFF,
5617 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
5618 2:0xA020..0xFF7F). For the moment, we don't know which charset
5619 uses this font. So, we set information in fontp->encoding[1]
5620 which is never used by any charset. If mapping can't be
5621 decided, set FONT_ENCODING_NOT_DECIDED. */
5622
5623 /* SJIS fonts need to be set to type 4, all others seem to work as
5624 type FONT_ENCODING_NOT_DECIDED. */
5625 encoding = strrchr (fontp->name, '-');
5626 if (encoding && stricmp (encoding+1, "sjis") == 0)
5627 fontp->encoding[1] = 4;
5628 else
5629 fontp->encoding[1] = FONT_ENCODING_NOT_DECIDED;
5630
5631 /* The following three values are set to 0 under W32, which is
5632 what they get set to if XGetFontProperty fails under X. */
5633 fontp->baseline_offset = 0;
5634 fontp->relative_compose = 0;
5635 fontp->default_ascent = 0;
5636
5637 /* Set global flag fonts_changed_p to non-zero if the font loaded
5638 has a character with a smaller width than any other character
5639 before, or if the font loaded has a smalle>r height than any
5640 other font loaded before. If this happens, it will make a
5641 glyph matrix reallocation necessary. */
5642 fonts_changed_p = x_compute_min_glyph_bounds (f);
5643 UNBLOCK_INPUT;
5644 return fontp;
5645 }
5646 }
5647
5648 /* Load font named FONTNAME of size SIZE for frame F, and return a
5649 pointer to the structure font_info while allocating it dynamically.
5650 If loading fails, return NULL. */
5651 struct font_info *
5652 w32_load_font (f,fontname,size)
5653 struct frame *f;
5654 char * fontname;
5655 int size;
5656 {
5657 Lisp_Object bdf_fonts;
5658 struct font_info *retval = NULL;
5659
5660 bdf_fonts = w32_list_bdf_fonts (build_string (fontname));
5661
5662 while (!retval && CONSP (bdf_fonts))
5663 {
5664 char *bdf_name, *bdf_file;
5665 Lisp_Object bdf_pair;
5666
5667 bdf_name = XSTRING (XCAR (bdf_fonts))->data;
5668 bdf_pair = Fassoc (XCAR (bdf_fonts), Vw32_bdf_filename_alist);
5669 bdf_file = XSTRING (XCDR (bdf_pair))->data;
5670
5671 retval = w32_load_bdf_font (f, bdf_name, size, bdf_file);
5672
5673 bdf_fonts = XCDR (bdf_fonts);
5674 }
5675
5676 if (retval)
5677 return retval;
5678
5679 return w32_load_system_font(f, fontname, size);
5680 }
5681
5682
5683 void
5684 w32_unload_font (dpyinfo, font)
5685 struct w32_display_info *dpyinfo;
5686 XFontStruct * font;
5687 {
5688 if (font)
5689 {
5690 if (font->per_char) xfree (font->per_char);
5691 if (font->bdf) w32_free_bdf_font (font->bdf);
5692
5693 if (font->hfont) DeleteObject(font->hfont);
5694 xfree (font);
5695 }
5696 }
5697
5698 /* The font conversion stuff between x and w32 */
5699
5700 /* X font string is as follows (from faces.el)
5701 * (let ((- "[-?]")
5702 * (foundry "[^-]+")
5703 * (family "[^-]+")
5704 * (weight "\\(bold\\|demibold\\|medium\\)") ; 1
5705 * (weight\? "\\([^-]*\\)") ; 1
5706 * (slant "\\([ior]\\)") ; 2
5707 * (slant\? "\\([^-]?\\)") ; 2
5708 * (swidth "\\([^-]*\\)") ; 3
5709 * (adstyle "[^-]*") ; 4
5710 * (pixelsize "[0-9]+")
5711 * (pointsize "[0-9][0-9]+")
5712 * (resx "[0-9][0-9]+")
5713 * (resy "[0-9][0-9]+")
5714 * (spacing "[cmp?*]")
5715 * (avgwidth "[0-9]+")
5716 * (registry "[^-]+")
5717 * (encoding "[^-]+")
5718 * )
5719 */
5720
5721 LONG
5722 x_to_w32_weight (lpw)
5723 char * lpw;
5724 {
5725 if (!lpw) return (FW_DONTCARE);
5726
5727 if (stricmp (lpw,"heavy") == 0) return FW_HEAVY;
5728 else if (stricmp (lpw,"extrabold") == 0) return FW_EXTRABOLD;
5729 else if (stricmp (lpw,"bold") == 0) return FW_BOLD;
5730 else if (stricmp (lpw,"demibold") == 0) return FW_SEMIBOLD;
5731 else if (stricmp (lpw,"semibold") == 0) return FW_SEMIBOLD;
5732 else if (stricmp (lpw,"medium") == 0) return FW_MEDIUM;
5733 else if (stricmp (lpw,"normal") == 0) return FW_NORMAL;
5734 else if (stricmp (lpw,"light") == 0) return FW_LIGHT;
5735 else if (stricmp (lpw,"extralight") == 0) return FW_EXTRALIGHT;
5736 else if (stricmp (lpw,"thin") == 0) return FW_THIN;
5737 else
5738 return FW_DONTCARE;
5739 }
5740
5741
5742 char *
5743 w32_to_x_weight (fnweight)
5744 int fnweight;
5745 {
5746 if (fnweight >= FW_HEAVY) return "heavy";
5747 if (fnweight >= FW_EXTRABOLD) return "extrabold";
5748 if (fnweight >= FW_BOLD) return "bold";
5749 if (fnweight >= FW_SEMIBOLD) return "demibold";
5750 if (fnweight >= FW_MEDIUM) return "medium";
5751 if (fnweight >= FW_NORMAL) return "normal";
5752 if (fnweight >= FW_LIGHT) return "light";
5753 if (fnweight >= FW_EXTRALIGHT) return "extralight";
5754 if (fnweight >= FW_THIN) return "thin";
5755 else
5756 return "*";
5757 }
5758
5759 LONG
5760 x_to_w32_charset (lpcs)
5761 char * lpcs;
5762 {
5763 Lisp_Object this_entry, w32_charset;
5764
5765 /* Look through w32-charset-info-alist for the character set.
5766 Format of each entry is
5767 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
5768 */
5769 this_entry = Fassoc (build_string(lpcs), Vw32_charset_info_alist);
5770
5771 if (NILP(this_entry))
5772 {
5773 /* At startup, we want iso8859-1 fonts to come up properly. */
5774 if (stricmp(lpcs, "iso8859-1") == 0)
5775 return ANSI_CHARSET;
5776 else
5777 return DEFAULT_CHARSET;
5778 }
5779
5780 w32_charset = Fcar (Fcdr (this_entry));
5781
5782 // Translate Lisp symbol to number.
5783 if (w32_charset == Qw32_charset_ansi)
5784 return ANSI_CHARSET;
5785 if (w32_charset == Qw32_charset_symbol)
5786 return SYMBOL_CHARSET;
5787 if (w32_charset == Qw32_charset_shiftjis)
5788 return SHIFTJIS_CHARSET;
5789 if (w32_charset == Qw32_charset_hangeul)
5790 return HANGEUL_CHARSET;
5791 if (w32_charset == Qw32_charset_chinesebig5)
5792 return CHINESEBIG5_CHARSET;
5793 if (w32_charset == Qw32_charset_gb2312)
5794 return GB2312_CHARSET;
5795 if (w32_charset == Qw32_charset_oem)
5796 return OEM_CHARSET;
5797 #ifdef JOHAB_CHARSET
5798 if (w32_charset == Qw32_charset_johab)
5799 return JOHAB_CHARSET;
5800 if (w32_charset == Qw32_charset_easteurope)
5801 return EASTEUROPE_CHARSET;
5802 if (w32_charset == Qw32_charset_turkish)
5803 return TURKISH_CHARSET;
5804 if (w32_charset == Qw32_charset_baltic)
5805 return BALTIC_CHARSET;
5806 if (w32_charset == Qw32_charset_russian)
5807 return RUSSIAN_CHARSET;
5808 if (w32_charset == Qw32_charset_arabic)
5809 return ARABIC_CHARSET;
5810 if (w32_charset == Qw32_charset_greek)
5811 return GREEK_CHARSET;
5812 if (w32_charset == Qw32_charset_hebrew)
5813 return HEBREW_CHARSET;
5814 if (w32_charset == Qw32_charset_vietnamese)
5815 return VIETNAMESE_CHARSET;
5816 if (w32_charset == Qw32_charset_thai)
5817 return THAI_CHARSET;
5818 if (w32_charset == Qw32_charset_mac)
5819 return MAC_CHARSET;
5820 #endif /* JOHAB_CHARSET */
5821 #ifdef UNICODE_CHARSET
5822 if (w32_charset == Qw32_charset_unicode)
5823 return UNICODE_CHARSET;
5824 #endif
5825
5826 return DEFAULT_CHARSET;
5827 }
5828
5829
5830 char *
5831 w32_to_x_charset (fncharset)
5832 int fncharset;
5833 {
5834 static char buf[16];
5835 Lisp_Object charset_type;
5836
5837 switch (fncharset)
5838 {
5839 case ANSI_CHARSET:
5840 /* Handle startup case of w32-charset-info-alist not
5841 being set up yet. */
5842 if (NILP(Vw32_charset_info_alist))
5843 return "iso8859-1";
5844 charset_type = Qw32_charset_ansi;
5845 break;
5846 case DEFAULT_CHARSET:
5847 charset_type = Qw32_charset_default;
5848 break;
5849 case SYMBOL_CHARSET:
5850 charset_type = Qw32_charset_symbol;
5851 break;
5852 case SHIFTJIS_CHARSET:
5853 charset_type = Qw32_charset_shiftjis;
5854 break;
5855 case HANGEUL_CHARSET:
5856 charset_type = Qw32_charset_hangeul;
5857 break;
5858 case GB2312_CHARSET:
5859 charset_type = Qw32_charset_gb2312;
5860 break;
5861 case CHINESEBIG5_CHARSET:
5862 charset_type = Qw32_charset_chinesebig5;
5863 break;
5864 case OEM_CHARSET:
5865 charset_type = Qw32_charset_oem;
5866 break;
5867
5868 /* More recent versions of Windows (95 and NT4.0) define more
5869 character sets. */
5870 #ifdef EASTEUROPE_CHARSET
5871 case EASTEUROPE_CHARSET:
5872 charset_type = Qw32_charset_easteurope;
5873 break;
5874 case TURKISH_CHARSET:
5875 charset_type = Qw32_charset_turkish;
5876 break;
5877 case BALTIC_CHARSET:
5878 charset_type = Qw32_charset_baltic;
5879 break;
5880 case RUSSIAN_CHARSET:
5881 charset_type = Qw32_charset_russian;
5882 break;
5883 case ARABIC_CHARSET:
5884 charset_type = Qw32_charset_arabic;
5885 break;
5886 case GREEK_CHARSET:
5887 charset_type = Qw32_charset_greek;
5888 break;
5889 case HEBREW_CHARSET:
5890 charset_type = Qw32_charset_hebrew;
5891 break;
5892 case VIETNAMESE_CHARSET:
5893 charset_type = Qw32_charset_vietnamese;
5894 break;
5895 case THAI_CHARSET:
5896 charset_type = Qw32_charset_thai;
5897 break;
5898 case MAC_CHARSET:
5899 charset_type = Qw32_charset_mac;
5900 break;
5901 case JOHAB_CHARSET:
5902 charset_type = Qw32_charset_johab;
5903 break;
5904 #endif
5905
5906 #ifdef UNICODE_CHARSET
5907 case UNICODE_CHARSET:
5908 charset_type = Qw32_charset_unicode;
5909 break;
5910 #endif
5911 default:
5912 /* Encode numerical value of unknown charset. */
5913 sprintf (buf, "*-#%u", fncharset);
5914 return buf;
5915 }
5916
5917 {
5918 Lisp_Object rest;
5919 char * best_match = NULL;
5920
5921 /* Look through w32-charset-info-alist for the character set.
5922 Prefer ISO codepages, and prefer lower numbers in the ISO
5923 range. Only return charsets for codepages which are installed.
5924
5925 Format of each entry is
5926 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
5927 */
5928 for (rest = Vw32_charset_info_alist; CONSP (rest); rest = XCDR (rest))
5929 {
5930 char * x_charset;
5931 Lisp_Object w32_charset;
5932 Lisp_Object codepage;
5933
5934 Lisp_Object this_entry = XCAR (rest);
5935
5936 /* Skip invalid entries in alist. */
5937 if (!CONSP (this_entry) || !STRINGP (XCAR (this_entry))
5938 || !CONSP (XCDR (this_entry))
5939 || !SYMBOLP (XCAR (XCDR (this_entry))))
5940 continue;
5941
5942 x_charset = XSTRING (XCAR (this_entry))->data;
5943 w32_charset = XCAR (XCDR (this_entry));
5944 codepage = XCDR (XCDR (this_entry));
5945
5946 /* Look for Same charset and a valid codepage (or non-int
5947 which means ignore). */
5948 if (w32_charset == charset_type
5949 && (!INTEGERP (codepage) || codepage == CP_DEFAULT
5950 || IsValidCodePage (XINT (codepage))))
5951 {
5952 /* If we don't have a match already, then this is the
5953 best. */
5954 if (!best_match)
5955 best_match = x_charset;
5956 /* If this is an ISO codepage, and the best so far isn't,
5957 then this is better. */
5958 else if (stricmp (best_match, "iso") != 0
5959 && stricmp (x_charset, "iso") == 0)
5960 best_match = x_charset;
5961 /* If both are ISO8859 codepages, choose the one with the
5962 lowest number in the encoding field. */
5963 else if (stricmp (best_match, "iso8859-") == 0
5964 && stricmp (x_charset, "iso8859-") == 0)
5965 {
5966 int best_enc = atoi (best_match + 8);
5967 int this_enc = atoi (x_charset + 8);
5968 if (this_enc > 0 && this_enc < best_enc)
5969 best_match = x_charset;
5970 }
5971 }
5972 }
5973
5974 /* If no match, encode the numeric value. */
5975 if (!best_match)
5976 {
5977 sprintf (buf, "*-#%u", fncharset);
5978 return buf;
5979 }
5980
5981 strncpy(buf, best_match, 15);
5982 buf[15] = '\0';
5983 return buf;
5984 }
5985 }
5986
5987
5988 /* Get the Windows codepage corresponding to the specified font. The
5989 charset info in the font name is used to look up
5990 w32-charset-to-codepage-alist. */
5991 int
5992 w32_codepage_for_font (char *fontname)
5993 {
5994 Lisp_Object codepage, entry;
5995 char *charset_str, *charset, *end;
5996
5997 if (NILP (Vw32_charset_info_alist))
5998 return CP_DEFAULT;
5999
6000 /* Extract charset part of font string. */
6001 charset = xlfd_charset_of_font (fontname);
6002
6003 if (!charset)
6004 return CP_INVALID;
6005
6006 charset_str = (char *) alloca (strlen (charset));
6007 strcpy (charset_str, charset);
6008
6009 /* Remove leading "*-". */
6010 if (strncmp ("*-", charset_str, 2) == 0)
6011 charset = charset_str + 2;
6012 else
6013 charset = charset_str;
6014
6015 /* Stop match at wildcard (including preceding '-'). */
6016 if (end = strchr (charset, '*'))
6017 {
6018 if (end > charset && *(end-1) == '-')
6019 end--;
6020 *end = '\0';
6021 }
6022
6023 entry = Fassoc (build_string(charset), Vw32_charset_info_alist);
6024 if (NILP (entry))
6025 return CP_INVALID;
6026
6027 codepage = Fcdr (Fcdr (entry));
6028
6029 if (NILP (codepage))
6030 return CP_8BIT;
6031 else if (XFASTINT (codepage) == XFASTINT (Qt))
6032 return CP_UNICODE;
6033 else if (INTEGERP (codepage))
6034 return XINT (codepage);
6035 else
6036 return CP_INVALID;
6037 }
6038
6039
6040 BOOL
6041 w32_to_x_font (lplogfont, lpxstr, len, specific_charset)
6042 LOGFONT * lplogfont;
6043 char * lpxstr;
6044 int len;
6045 char * specific_charset;
6046 {
6047 char* fonttype;
6048 char *fontname;
6049 char height_pixels[8];
6050 char height_dpi[8];
6051 char width_pixels[8];
6052 char *fontname_dash;
6053 int display_resy = one_w32_display_info.resy;
6054 int display_resx = one_w32_display_info.resx;
6055 int bufsz;
6056 struct coding_system coding;
6057
6058 if (!lpxstr) abort ();
6059
6060 if (!lplogfont)
6061 return FALSE;
6062
6063 if (lplogfont->lfOutPrecision == OUT_STRING_PRECIS)
6064 fonttype = "raster";
6065 else if (lplogfont->lfOutPrecision == OUT_STROKE_PRECIS)
6066 fonttype = "outline";
6067 else
6068 fonttype = "unknown";
6069
6070 setup_coding_system (Fcheck_coding_system (Vw32_system_coding_system),
6071 &coding);
6072 coding.src_multibyte = 0;
6073 coding.dst_multibyte = 1;
6074 coding.mode |= CODING_MODE_LAST_BLOCK;
6075 bufsz = decoding_buffer_size (&coding, LF_FACESIZE);
6076
6077 fontname = alloca(sizeof(*fontname) * bufsz);
6078 decode_coding (&coding, lplogfont->lfFaceName, fontname,
6079 strlen(lplogfont->lfFaceName), bufsz - 1);
6080 *(fontname + coding.produced) = '\0';
6081
6082 /* Replace dashes with underscores so the dashes are not
6083 misinterpreted. */
6084 fontname_dash = fontname;
6085 while (fontname_dash = strchr (fontname_dash, '-'))
6086 *fontname_dash = '_';
6087
6088 if (lplogfont->lfHeight)
6089 {
6090 sprintf (height_pixels, "%u", abs (lplogfont->lfHeight));
6091 sprintf (height_dpi, "%u",
6092 abs (lplogfont->lfHeight) * 720 / display_resy);
6093 }
6094 else
6095 {
6096 strcpy (height_pixels, "*");
6097 strcpy (height_dpi, "*");
6098 }
6099 if (lplogfont->lfWidth)
6100 sprintf (width_pixels, "%u", lplogfont->lfWidth * 10);
6101 else
6102 strcpy (width_pixels, "*");
6103
6104 _snprintf (lpxstr, len - 1,
6105 "-%s-%s-%s-%c-normal-normal-%s-%s-%d-%d-%c-%s-%s",
6106 fonttype, /* foundry */
6107 fontname, /* family */
6108 w32_to_x_weight (lplogfont->lfWeight), /* weight */
6109 lplogfont->lfItalic?'i':'r', /* slant */
6110 /* setwidth name */
6111 /* add style name */
6112 height_pixels, /* pixel size */
6113 height_dpi, /* point size */
6114 display_resx, /* resx */
6115 display_resy, /* resy */
6116 ((lplogfont->lfPitchAndFamily & 0x3) == VARIABLE_PITCH)
6117 ? 'p' : 'c', /* spacing */
6118 width_pixels, /* avg width */
6119 specific_charset ? specific_charset
6120 : w32_to_x_charset (lplogfont->lfCharSet)
6121 /* charset registry and encoding */
6122 );
6123
6124 lpxstr[len - 1] = 0; /* just to be sure */
6125 return (TRUE);
6126 }
6127
6128 BOOL
6129 x_to_w32_font (lpxstr, lplogfont)
6130 char * lpxstr;
6131 LOGFONT * lplogfont;
6132 {
6133 struct coding_system coding;
6134
6135 if (!lplogfont) return (FALSE);
6136
6137 memset (lplogfont, 0, sizeof (*lplogfont));
6138
6139 /* Set default value for each field. */
6140 #if 1
6141 lplogfont->lfOutPrecision = OUT_DEFAULT_PRECIS;
6142 lplogfont->lfClipPrecision = CLIP_DEFAULT_PRECIS;
6143 lplogfont->lfQuality = DEFAULT_QUALITY;
6144 #else
6145 /* go for maximum quality */
6146 lplogfont->lfOutPrecision = OUT_STROKE_PRECIS;
6147 lplogfont->lfClipPrecision = CLIP_STROKE_PRECIS;
6148 lplogfont->lfQuality = PROOF_QUALITY;
6149 #endif
6150
6151 lplogfont->lfCharSet = DEFAULT_CHARSET;
6152 lplogfont->lfWeight = FW_DONTCARE;
6153 lplogfont->lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
6154
6155 if (!lpxstr)
6156 return FALSE;
6157
6158 /* Provide a simple escape mechanism for specifying Windows font names
6159 * directly -- if font spec does not beginning with '-', assume this
6160 * format:
6161 * "<font name>[:height in pixels[:width in pixels[:weight]]]"
6162 */
6163
6164 if (*lpxstr == '-')
6165 {
6166 int fields, tem;
6167 char name[50], weight[20], slant, pitch, pixels[10], height[10],
6168 width[10], resy[10], remainder[20];
6169 char * encoding;
6170 int dpi = one_w32_display_info.resy;
6171
6172 fields = sscanf (lpxstr,
6173 "-%*[^-]-%49[^-]-%19[^-]-%c-%*[^-]-%*[^-]-%9[^-]-%9[^-]-%*[^-]-%9[^-]-%c-%9[^-]-%19s",
6174 name, weight, &slant, pixels, height, resy, &pitch, width, remainder);
6175 if (fields == EOF) return (FALSE);
6176
6177 /* If wildcards cover more than one field, we don't know which
6178 field is which, so don't fill any in. */
6179
6180 if (fields < 9)
6181 fields = 0;
6182
6183 if (fields > 0 && name[0] != '*')
6184 {
6185 int bufsize;
6186 unsigned char *buf;
6187
6188 setup_coding_system
6189 (Fcheck_coding_system (Vw32_system_coding_system), &coding);
6190 coding.src_multibyte = 1;
6191 coding.dst_multibyte = 1;
6192 bufsize = encoding_buffer_size (&coding, strlen (name));
6193 buf = (unsigned char *) alloca (bufsize);
6194 coding.mode |= CODING_MODE_LAST_BLOCK;
6195 encode_coding (&coding, name, buf, strlen (name), bufsize);
6196 if (coding.produced >= LF_FACESIZE)
6197 coding.produced = LF_FACESIZE - 1;
6198 buf[coding.produced] = 0;
6199 strcpy (lplogfont->lfFaceName, buf);
6200 }
6201 else
6202 {
6203 lplogfont->lfFaceName[0] = '\0';
6204 }
6205
6206 fields--;
6207
6208 lplogfont->lfWeight = x_to_w32_weight ((fields > 0 ? weight : ""));
6209
6210 fields--;
6211
6212 if (!NILP (Vw32_enable_synthesized_fonts))
6213 lplogfont->lfItalic = (fields > 0 && slant == 'i');
6214
6215 fields--;
6216
6217 if (fields > 0 && pixels[0] != '*')
6218 lplogfont->lfHeight = atoi (pixels);
6219
6220 fields--;
6221 fields--;
6222 if (fields > 0 && resy[0] != '*')
6223 {
6224 tem = atoi (resy);
6225 if (tem > 0) dpi = tem;
6226 }
6227
6228 if (fields > -1 && lplogfont->lfHeight == 0 && height[0] != '*')
6229 lplogfont->lfHeight = atoi (height) * dpi / 720;
6230
6231 if (fields > 0)
6232 lplogfont->lfPitchAndFamily =
6233 (fields > 0 && pitch == 'p') ? VARIABLE_PITCH : FIXED_PITCH;
6234
6235 fields--;
6236
6237 if (fields > 0 && width[0] != '*')
6238 lplogfont->lfWidth = atoi (width) / 10;
6239
6240 fields--;
6241
6242 /* Strip the trailing '-' if present. (it shouldn't be, as it
6243 fails the test against xlfd-tight-regexp in fontset.el). */
6244 {
6245 int len = strlen (remainder);
6246 if (len > 0 && remainder[len-1] == '-')
6247 remainder[len-1] = 0;
6248 }
6249 encoding = remainder;
6250 if (strncmp (encoding, "*-", 2) == 0)
6251 encoding += 2;
6252 lplogfont->lfCharSet = x_to_w32_charset (fields > 0 ? encoding : "");
6253 }
6254 else
6255 {
6256 int fields;
6257 char name[100], height[10], width[10], weight[20];
6258
6259 fields = sscanf (lpxstr,
6260 "%99[^:]:%9[^:]:%9[^:]:%19s",
6261 name, height, width, weight);
6262
6263 if (fields == EOF) return (FALSE);
6264
6265 if (fields > 0)
6266 {
6267 strncpy (lplogfont->lfFaceName,name, LF_FACESIZE);
6268 lplogfont->lfFaceName[LF_FACESIZE-1] = 0;
6269 }
6270 else
6271 {
6272 lplogfont->lfFaceName[0] = 0;
6273 }
6274
6275 fields--;
6276
6277 if (fields > 0)
6278 lplogfont->lfHeight = atoi (height);
6279
6280 fields--;
6281
6282 if (fields > 0)
6283 lplogfont->lfWidth = atoi (width);
6284
6285 fields--;
6286
6287 lplogfont->lfWeight = x_to_w32_weight ((fields > 0 ? weight : ""));
6288 }
6289
6290 /* This makes TrueType fonts work better. */
6291 lplogfont->lfHeight = - abs (lplogfont->lfHeight);
6292
6293 return (TRUE);
6294 }
6295
6296 /* Strip the pixel height and point height from the given xlfd, and
6297 return the pixel height. If no pixel height is specified, calculate
6298 one from the point height, or if that isn't defined either, return
6299 0 (which usually signifies a scalable font).
6300 */
6301 int xlfd_strip_height (char *fontname)
6302 {
6303 int pixel_height, point_height, dpi, field_number;
6304 char *read_from, *write_to;
6305
6306 xassert (fontname);
6307
6308 pixel_height = field_number = 0;
6309 write_to = NULL;
6310
6311 /* Look for height fields. */
6312 for (read_from = fontname; *read_from; read_from++)
6313 {
6314 if (*read_from == '-')
6315 {
6316 field_number++;
6317 if (field_number == 7) /* Pixel height. */
6318 {
6319 read_from++;
6320 write_to = read_from;
6321
6322 /* Find end of field. */
6323 for (;*read_from && *read_from != '-'; read_from++)
6324 ;
6325
6326 /* Split the fontname at end of field. */
6327 if (*read_from)
6328 {
6329 *read_from = '\0';
6330 read_from++;
6331 }
6332 pixel_height = atoi (write_to);
6333 /* Blank out field. */
6334 if (read_from > write_to)
6335 {
6336 *write_to = '-';
6337 write_to++;
6338 }
6339 /* If the pixel height field is at the end (partial xlfd),
6340 return now. */
6341 else
6342 return pixel_height;
6343
6344 /* If we got a pixel height, the point height can be
6345 ignored. Just blank it out and break now. */
6346 if (pixel_height)
6347 {
6348 /* Find end of point size field. */
6349 for (; *read_from && *read_from != '-'; read_from++)
6350 ;
6351
6352 if (*read_from)
6353 read_from++;
6354
6355 /* Blank out the point size field. */
6356 if (read_from > write_to)
6357 {
6358 *write_to = '-';
6359 write_to++;
6360 }
6361 else
6362 return pixel_height;
6363
6364 break;
6365 }
6366 /* If the point height is already blank, break now. */
6367 if (*read_from == '-')
6368 {
6369 read_from++;
6370 break;
6371 }
6372 }
6373 else if (field_number == 8)
6374 {
6375 /* If we didn't get a pixel height, try to get the point
6376 height and convert that. */
6377 int point_size;
6378 char *point_size_start = read_from++;
6379
6380 /* Find end of field. */
6381 for (; *read_from && *read_from != '-'; read_from++)
6382 ;
6383
6384 if (*read_from)
6385 {
6386 *read_from = '\0';
6387 read_from++;
6388 }
6389
6390 point_size = atoi (point_size_start);
6391
6392 /* Convert to pixel height. */
6393 pixel_height = point_size
6394 * one_w32_display_info.height_in / 720;
6395
6396 /* Blank out this field and break. */
6397 *write_to = '-';
6398 write_to++;
6399 break;
6400 }
6401 }
6402 }
6403
6404 /* Shift the rest of the font spec into place. */
6405 if (write_to && read_from > write_to)
6406 {
6407 for (; *read_from; read_from++, write_to++)
6408 *write_to = *read_from;
6409 *write_to = '\0';
6410 }
6411
6412 return pixel_height;
6413 }
6414
6415 /* Assume parameter 1 is fully qualified, no wildcards. */
6416 BOOL
6417 w32_font_match (fontname, pattern)
6418 char * fontname;
6419 char * pattern;
6420 {
6421 char *regex = alloca (strlen (pattern) * 2);
6422 char *font_name_copy = alloca (strlen (fontname) + 1);
6423 char *ptr;
6424
6425 /* Copy fontname so we can modify it during comparison. */
6426 strcpy (font_name_copy, fontname);
6427
6428 ptr = regex;
6429 *ptr++ = '^';
6430
6431 /* Turn pattern into a regexp and do a regexp match. */
6432 for (; *pattern; pattern++)
6433 {
6434 if (*pattern == '?')
6435 *ptr++ = '.';
6436 else if (*pattern == '*')
6437 {
6438 *ptr++ = '.';
6439 *ptr++ = '*';
6440 }
6441 else
6442 *ptr++ = *pattern;
6443 }
6444 *ptr = '$';
6445 *(ptr + 1) = '\0';
6446
6447 /* Strip out font heights and compare them seperately, since
6448 rounding error can cause mismatches. This also allows a
6449 comparison between a font that declares only a pixel height and a
6450 pattern that declares the point height.
6451 */
6452 {
6453 int font_height, pattern_height;
6454
6455 font_height = xlfd_strip_height (font_name_copy);
6456 pattern_height = xlfd_strip_height (regex);
6457
6458 /* Compare now, and don't bother doing expensive regexp matching
6459 if the heights differ. */
6460 if (font_height && pattern_height && (font_height != pattern_height))
6461 return FALSE;
6462 }
6463
6464 return (fast_c_string_match_ignore_case (build_string (regex),
6465 font_name_copy) >= 0);
6466 }
6467
6468 /* Callback functions, and a structure holding info they need, for
6469 listing system fonts on W32. We need one set of functions to do the
6470 job properly, but these don't work on NT 3.51 and earlier, so we
6471 have a second set which don't handle character sets properly to
6472 fall back on.
6473
6474 In both cases, there are two passes made. The first pass gets one
6475 font from each family, the second pass lists all the fonts from
6476 each family. */
6477
6478 typedef struct enumfont_t
6479 {
6480 HDC hdc;
6481 int numFonts;
6482 LOGFONT logfont;
6483 XFontStruct *size_ref;
6484 Lisp_Object *pattern;
6485 Lisp_Object *tail;
6486 } enumfont_t;
6487
6488 int CALLBACK
6489 enum_font_cb2 (lplf, lptm, FontType, lpef)
6490 ENUMLOGFONT * lplf;
6491 NEWTEXTMETRIC * lptm;
6492 int FontType;
6493 enumfont_t * lpef;
6494 {
6495 if (lplf->elfLogFont.lfStrikeOut || lplf->elfLogFont.lfUnderline)
6496 return (1);
6497
6498 /* Check that the character set matches if it was specified */
6499 if (lpef->logfont.lfCharSet != DEFAULT_CHARSET &&
6500 lplf->elfLogFont.lfCharSet != lpef->logfont.lfCharSet)
6501 return (1);
6502
6503 {
6504 char buf[100];
6505 Lisp_Object width = Qnil;
6506 char *charset = NULL;
6507
6508 /* Truetype fonts do not report their true metrics until loaded */
6509 if (FontType != RASTER_FONTTYPE)
6510 {
6511 if (!NILP (*(lpef->pattern)))
6512 {
6513 /* Scalable fonts are as big as you want them to be. */
6514 lplf->elfLogFont.lfHeight = lpef->logfont.lfHeight;
6515 lplf->elfLogFont.lfWidth = lpef->logfont.lfWidth;
6516 width = make_number (lpef->logfont.lfWidth);
6517 }
6518 else
6519 {
6520 lplf->elfLogFont.lfHeight = 0;
6521 lplf->elfLogFont.lfWidth = 0;
6522 }
6523 }
6524
6525 /* Make sure the height used here is the same as everywhere
6526 else (ie character height, not cell height). */
6527 if (lplf->elfLogFont.lfHeight > 0)
6528 {
6529 /* lptm can be trusted for RASTER fonts, but not scalable ones. */
6530 if (FontType == RASTER_FONTTYPE)
6531 lplf->elfLogFont.lfHeight = lptm->tmInternalLeading - lptm->tmHeight;
6532 else
6533 lplf->elfLogFont.lfHeight = -lplf->elfLogFont.lfHeight;
6534 }
6535
6536 if (!NILP (*(lpef->pattern)))
6537 {
6538 charset = xlfd_charset_of_font (XSTRING(*(lpef->pattern))->data);
6539
6540 /* Ensure that charset is valid for this font. */
6541 if (charset
6542 && (x_to_w32_charset (charset) != lplf->elfLogFont.lfCharSet))
6543 charset = NULL;
6544 }
6545
6546 /* TODO: List all relevant charsets if charset not specified. */
6547 if (!w32_to_x_font (&(lplf->elfLogFont), buf, 100, charset))
6548 return (0);
6549
6550 if (NILP (*(lpef->pattern))
6551 || w32_font_match (buf, XSTRING (*(lpef->pattern))->data))
6552 {
6553 *lpef->tail = Fcons (Fcons (build_string (buf), width), Qnil);
6554 lpef->tail = &(XCDR (*lpef->tail));
6555 lpef->numFonts++;
6556 }
6557 }
6558
6559 return (1);
6560 }
6561
6562 int CALLBACK
6563 enum_font_cb1 (lplf, lptm, FontType, lpef)
6564 ENUMLOGFONT * lplf;
6565 NEWTEXTMETRIC * lptm;
6566 int FontType;
6567 enumfont_t * lpef;
6568 {
6569 return EnumFontFamilies (lpef->hdc,
6570 lplf->elfLogFont.lfFaceName,
6571 (FONTENUMPROC) enum_font_cb2,
6572 (LPARAM) lpef);
6573 }
6574
6575
6576 int CALLBACK
6577 enum_fontex_cb2 (lplf, lptm, font_type, lpef)
6578 ENUMLOGFONTEX * lplf;
6579 NEWTEXTMETRICEX * lptm;
6580 int font_type;
6581 enumfont_t * lpef;
6582 {
6583 /* We are not interested in the extra info we get back from the 'Ex
6584 version - only the fact that we get character set variations
6585 enumerated seperately. */
6586 return enum_font_cb2 ((ENUMLOGFONT *) lplf, (NEWTEXTMETRIC *) lptm,
6587 font_type, lpef);
6588 }
6589
6590 int CALLBACK
6591 enum_fontex_cb1 (lplf, lptm, font_type, lpef)
6592 ENUMLOGFONTEX * lplf;
6593 NEWTEXTMETRICEX * lptm;
6594 int font_type;
6595 enumfont_t * lpef;
6596 {
6597 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
6598 FARPROC enum_font_families_ex
6599 = GetProcAddress ( gdi32, "EnumFontFamiliesExA");
6600 /* We don't really expect EnumFontFamiliesEx to disappear once we
6601 get here, so don't bother handling it gracefully. */
6602 if (enum_font_families_ex == NULL)
6603 error ("gdi32.dll has disappeared!");
6604 return enum_font_families_ex (lpef->hdc,
6605 &lplf->elfLogFont,
6606 (FONTENUMPROC) enum_fontex_cb2,
6607 (LPARAM) lpef, 0);
6608 }
6609
6610 /* Interface to fontset handler. (adapted from mw32font.c in Meadow
6611 and xterm.c in Emacs 20.3) */
6612
6613 Lisp_Object w32_list_bdf_fonts (Lisp_Object pattern, int max_names)
6614 {
6615 char *fontname, *ptnstr;
6616 Lisp_Object list, tem, newlist = Qnil;
6617 int n_fonts = 0;
6618
6619 list = Vw32_bdf_filename_alist;
6620 ptnstr = XSTRING (pattern)->data;
6621
6622 for ( ; CONSP (list); list = XCDR (list))
6623 {
6624 tem = XCAR (list);
6625 if (CONSP (tem))
6626 fontname = XSTRING (XCAR (tem))->data;
6627 else if (STRINGP (tem))
6628 fontname = XSTRING (tem)->data;
6629 else
6630 continue;
6631
6632 if (w32_font_match (fontname, ptnstr))
6633 {
6634 newlist = Fcons (XCAR (tem), newlist);
6635 n_fonts++;
6636 if (n_fonts >= max_names)
6637 break;
6638 }
6639 }
6640
6641 return newlist;
6642 }
6643
6644 Lisp_Object w32_list_synthesized_fonts (FRAME_PTR f, Lisp_Object pattern,
6645 int size, int max_names);
6646
6647 /* Return a list of names of available fonts matching PATTERN on frame
6648 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
6649 to be listed. Frame F NULL means we have not yet created any
6650 frame, which means we can't get proper size info, as we don't have
6651 a device context to use for GetTextMetrics.
6652 MAXNAMES sets a limit on how many fonts to match. */
6653
6654 Lisp_Object
6655 w32_list_fonts (FRAME_PTR f, Lisp_Object pattern, int size, int maxnames )
6656 {
6657 Lisp_Object patterns, key = Qnil, tem, tpat;
6658 Lisp_Object list = Qnil, newlist = Qnil, second_best = Qnil;
6659 struct w32_display_info *dpyinfo = &one_w32_display_info;
6660 int n_fonts = 0;
6661
6662 patterns = Fassoc (pattern, Valternate_fontname_alist);
6663 if (NILP (patterns))
6664 patterns = Fcons (pattern, Qnil);
6665
6666 for (; CONSP (patterns); patterns = XCDR (patterns))
6667 {
6668 enumfont_t ef;
6669 int codepage;
6670
6671 tpat = XCAR (patterns);
6672
6673 if (!STRINGP (tpat))
6674 continue;
6675
6676 /* Avoid expensive EnumFontFamilies functions if we are not
6677 going to be able to output one of these anyway. */
6678 codepage = w32_codepage_for_font (XSTRING (tpat)->data);
6679 if (codepage != CP_8BIT && codepage != CP_UNICODE
6680 && codepage != CP_DEFAULT && !IsValidCodePage(codepage))
6681 continue;
6682
6683 /* See if we cached the result for this particular query.
6684 The cache is an alist of the form:
6685 ((PATTERN (FONTNAME . WIDTH) ...) ...)
6686 */
6687 if (tem = XCDR (dpyinfo->name_list_element),
6688 !NILP (list = Fassoc (tpat, tem)))
6689 {
6690 list = Fcdr_safe (list);
6691 /* We have a cached list. Don't have to get the list again. */
6692 goto label_cached;
6693 }
6694
6695 BLOCK_INPUT;
6696 /* At first, put PATTERN in the cache. */
6697 list = Qnil;
6698 ef.pattern = &tpat;
6699 ef.tail = &list;
6700 ef.numFonts = 0;
6701
6702 /* Use EnumFontFamiliesEx where it is available, as it knows
6703 about character sets. Fall back to EnumFontFamilies for
6704 older versions of NT that don't support the 'Ex function. */
6705 x_to_w32_font (XSTRING (tpat)->data, &ef.logfont);
6706 {
6707 LOGFONT font_match_pattern;
6708 HMODULE gdi32 = GetModuleHandle ("gdi32.dll");
6709 FARPROC enum_font_families_ex
6710 = GetProcAddress ( gdi32, "EnumFontFamiliesExA");
6711
6712 /* We do our own pattern matching so we can handle wildcards. */
6713 font_match_pattern.lfFaceName[0] = 0;
6714 font_match_pattern.lfPitchAndFamily = 0;
6715 /* We can use the charset, because if it is a wildcard it will
6716 be DEFAULT_CHARSET anyway. */
6717 font_match_pattern.lfCharSet = ef.logfont.lfCharSet;
6718
6719 ef.hdc = GetDC (dpyinfo->root_window);
6720
6721 if (enum_font_families_ex)
6722 enum_font_families_ex (ef.hdc,
6723 &font_match_pattern,
6724 (FONTENUMPROC) enum_fontex_cb1,
6725 (LPARAM) &ef, 0);
6726 else
6727 EnumFontFamilies (ef.hdc, NULL, (FONTENUMPROC) enum_font_cb1,
6728 (LPARAM)&ef);
6729
6730 ReleaseDC (dpyinfo->root_window, ef.hdc);
6731 }
6732
6733 UNBLOCK_INPUT;
6734
6735 /* Make a list of the fonts we got back.
6736 Store that in the font cache for the display. */
6737 XCDR (dpyinfo->name_list_element)
6738 = Fcons (Fcons (tpat, list),
6739 XCDR (dpyinfo->name_list_element));
6740
6741 label_cached:
6742 if (NILP (list)) continue; /* Try the remaining alternatives. */
6743
6744 newlist = second_best = Qnil;
6745
6746 /* Make a list of the fonts that have the right width. */
6747 for (; CONSP (list); list = XCDR (list))
6748 {
6749 int found_size;
6750 tem = XCAR (list);
6751
6752 if (!CONSP (tem))
6753 continue;
6754 if (NILP (XCAR (tem)))
6755 continue;
6756 if (!size)
6757 {
6758 newlist = Fcons (XCAR (tem), newlist);
6759 n_fonts++;
6760 if (n_fonts >= maxnames)
6761 break;
6762 else
6763 continue;
6764 }
6765 if (!INTEGERP (XCDR (tem)))
6766 {
6767 /* Since we don't yet know the size of the font, we must
6768 load it and try GetTextMetrics. */
6769 W32FontStruct thisinfo;
6770 LOGFONT lf;
6771 HDC hdc;
6772 HANDLE oldobj;
6773
6774 if (!x_to_w32_font (XSTRING (XCAR (tem))->data, &lf))
6775 continue;
6776
6777 BLOCK_INPUT;
6778 thisinfo.bdf = NULL;
6779 thisinfo.hfont = CreateFontIndirect (&lf);
6780 if (thisinfo.hfont == NULL)
6781 continue;
6782
6783 hdc = GetDC (dpyinfo->root_window);
6784 oldobj = SelectObject (hdc, thisinfo.hfont);
6785 if (GetTextMetrics (hdc, &thisinfo.tm))
6786 XCDR (tem) = make_number (FONT_WIDTH (&thisinfo));
6787 else
6788 XCDR (tem) = make_number (0);
6789 SelectObject (hdc, oldobj);
6790 ReleaseDC (dpyinfo->root_window, hdc);
6791 DeleteObject(thisinfo.hfont);
6792 UNBLOCK_INPUT;
6793 }
6794 found_size = XINT (XCDR (tem));
6795 if (found_size == size)
6796 {
6797 newlist = Fcons (XCAR (tem), newlist);
6798 n_fonts++;
6799 if (n_fonts >= maxnames)
6800 break;
6801 }
6802 /* keep track of the closest matching size in case
6803 no exact match is found. */
6804 else if (found_size > 0)
6805 {
6806 if (NILP (second_best))
6807 second_best = tem;
6808
6809 else if (found_size < size)
6810 {
6811 if (XINT (XCDR (second_best)) > size
6812 || XINT (XCDR (second_best)) < found_size)
6813 second_best = tem;
6814 }
6815 else
6816 {
6817 if (XINT (XCDR (second_best)) > size
6818 && XINT (XCDR (second_best)) >
6819 found_size)
6820 second_best = tem;
6821 }
6822 }
6823 }
6824
6825 if (!NILP (newlist))
6826 break;
6827 else if (!NILP (second_best))
6828 {
6829 newlist = Fcons (XCAR (second_best), Qnil);
6830 break;
6831 }
6832 }
6833
6834 /* Include any bdf fonts. */
6835 if (n_fonts < maxnames)
6836 {
6837 Lisp_Object combined[2];
6838 combined[0] = w32_list_bdf_fonts (pattern, maxnames - n_fonts);
6839 combined[1] = newlist;
6840 newlist = Fnconc(2, combined);
6841 }
6842
6843 /* If we can't find a font that matches, check if Windows would be
6844 able to synthesize it from a different style. */
6845 if (NILP (newlist) && !NILP (Vw32_enable_synthesized_fonts))
6846 newlist = w32_list_synthesized_fonts (f, pattern, size, maxnames);
6847
6848 return newlist;
6849 }
6850
6851 Lisp_Object
6852 w32_list_synthesized_fonts (f, pattern, size, max_names)
6853 FRAME_PTR f;
6854 Lisp_Object pattern;
6855 int size;
6856 int max_names;
6857 {
6858 int fields;
6859 char *full_pattn, *new_pattn, foundary[50], family[50], *pattn_part2;
6860 char style[20], slant;
6861 Lisp_Object matches, match, tem, synthed_matches = Qnil;
6862
6863 full_pattn = XSTRING (pattern)->data;
6864
6865 pattn_part2 = alloca (XSTRING (pattern)->size);
6866 /* Allow some space for wildcard expansion. */
6867 new_pattn = alloca (XSTRING (pattern)->size + 100);
6868
6869 fields = sscanf (full_pattn, "-%49[^-]-%49[^-]-%19[^-]-%c-%s",
6870 foundary, family, style, &slant, pattn_part2);
6871 if (fields == EOF || fields < 5)
6872 return Qnil;
6873
6874 /* If the style and slant are wildcards already there is no point
6875 checking again (and we don't want to keep recursing). */
6876 if (*style == '*' && slant == '*')
6877 return Qnil;
6878
6879 sprintf (new_pattn, "-%s-%s-*-*-%s", foundary, family, pattn_part2);
6880
6881 matches = w32_list_fonts (f, build_string (new_pattn), size, max_names);
6882
6883 for ( ; CONSP (matches); matches = XCDR (matches))
6884 {
6885 tem = XCAR (matches);
6886 if (!STRINGP (tem))
6887 continue;
6888
6889 full_pattn = XSTRING (tem)->data;
6890 fields = sscanf (full_pattn, "-%49[^-]-%49[^-]-%*[^-]-%*c-%s",
6891 foundary, family, pattn_part2);
6892 if (fields == EOF || fields < 3)
6893 continue;
6894
6895 sprintf (new_pattn, "-%s-%s-%s-%c-%s", foundary, family, style,
6896 slant, pattn_part2);
6897
6898 synthed_matches = Fcons (build_string (new_pattn),
6899 synthed_matches);
6900 }
6901
6902 return synthed_matches;
6903 }
6904
6905
6906 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
6907 struct font_info *
6908 w32_get_font_info (f, font_idx)
6909 FRAME_PTR f;
6910 int font_idx;
6911 {
6912 return (FRAME_W32_FONT_TABLE (f) + font_idx);
6913 }
6914
6915
6916 struct font_info*
6917 w32_query_font (struct frame *f, char *fontname)
6918 {
6919 int i;
6920 struct font_info *pfi;
6921
6922 pfi = FRAME_W32_FONT_TABLE (f);
6923
6924 for (i = 0; i < one_w32_display_info.n_fonts ;i++, pfi++)
6925 {
6926 if (strcmp(pfi->name, fontname) == 0) return pfi;
6927 }
6928
6929 return NULL;
6930 }
6931
6932 /* Find a CCL program for a font specified by FONTP, and set the member
6933 `encoder' of the structure. */
6934
6935 void
6936 w32_find_ccl_program (fontp)
6937 struct font_info *fontp;
6938 {
6939 Lisp_Object list, elt;
6940
6941 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
6942 {
6943 elt = XCAR (list);
6944 if (CONSP (elt)
6945 && STRINGP (XCAR (elt))
6946 && (fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
6947 >= 0))
6948 break;
6949 }
6950 if (! NILP (list))
6951 {
6952 struct ccl_program *ccl
6953 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
6954
6955 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
6956 xfree (ccl);
6957 else
6958 fontp->font_encoder = ccl;
6959 }
6960 }
6961
6962 \f
6963 DEFUN ("w32-find-bdf-fonts", Fw32_find_bdf_fonts, Sw32_find_bdf_fonts,
6964 1, 1, 0,
6965 "Return a list of BDF fonts in DIR, suitable for appending to\n\
6966 w32-bdf-filename-alist. Fonts which do not contain an xlfd description\n\
6967 will not be included in the list. DIR may be a list of directories.")
6968 (directory)
6969 Lisp_Object directory;
6970 {
6971 Lisp_Object list = Qnil;
6972 struct gcpro gcpro1, gcpro2;
6973
6974 if (!CONSP (directory))
6975 return w32_find_bdf_fonts_in_dir (directory);
6976
6977 for ( ; CONSP (directory); directory = XCDR (directory))
6978 {
6979 Lisp_Object pair[2];
6980 pair[0] = list;
6981 pair[1] = Qnil;
6982 GCPRO2 (directory, list);
6983 pair[1] = w32_find_bdf_fonts_in_dir( XCAR (directory) );
6984 list = Fnconc( 2, pair );
6985 UNGCPRO;
6986 }
6987 return list;
6988 }
6989
6990 /* Find BDF files in a specified directory. (use GCPRO when calling,
6991 as this calls lisp to get a directory listing). */
6992 Lisp_Object w32_find_bdf_fonts_in_dir( Lisp_Object directory )
6993 {
6994 Lisp_Object filelist, list = Qnil;
6995 char fontname[100];
6996
6997 if (!STRINGP(directory))
6998 return Qnil;
6999
7000 filelist = Fdirectory_files (directory, Qt,
7001 build_string (".*\\.[bB][dD][fF]"), Qt);
7002
7003 for ( ; CONSP(filelist); filelist = XCDR (filelist))
7004 {
7005 Lisp_Object filename = XCAR (filelist);
7006 if (w32_BDF_to_x_font (XSTRING (filename)->data, fontname, 100))
7007 store_in_alist (&list, build_string (fontname), filename);
7008 }
7009 return list;
7010 }
7011
7012 \f
7013 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
7014 "Internal function called by `color-defined-p', which see.")
7015 (color, frame)
7016 Lisp_Object color, frame;
7017 {
7018 XColor foo;
7019 FRAME_PTR f = check_x_frame (frame);
7020
7021 CHECK_STRING (color, 1);
7022
7023 if (w32_defined_color (f, XSTRING (color)->data, &foo, 0))
7024 return Qt;
7025 else
7026 return Qnil;
7027 }
7028
7029 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
7030 "Internal function called by `color-values', which see.")
7031 (color, frame)
7032 Lisp_Object color, frame;
7033 {
7034 XColor foo;
7035 FRAME_PTR f = check_x_frame (frame);
7036
7037 CHECK_STRING (color, 1);
7038
7039 if (w32_defined_color (f, XSTRING (color)->data, &foo, 0))
7040 {
7041 Lisp_Object rgb[3];
7042
7043 rgb[0] = make_number ((GetRValue (foo.pixel) << 8)
7044 | GetRValue (foo.pixel));
7045 rgb[1] = make_number ((GetGValue (foo.pixel) << 8)
7046 | GetGValue (foo.pixel));
7047 rgb[2] = make_number ((GetBValue (foo.pixel) << 8)
7048 | GetBValue (foo.pixel));
7049 return Flist (3, rgb);
7050 }
7051 else
7052 return Qnil;
7053 }
7054
7055 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
7056 "Internal function called by `display-color-p', which see.")
7057 (display)
7058 Lisp_Object display;
7059 {
7060 struct w32_display_info *dpyinfo = check_x_display_info (display);
7061
7062 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 2)
7063 return Qnil;
7064
7065 return Qt;
7066 }
7067
7068 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p, Sx_display_grayscale_p,
7069 0, 1, 0,
7070 "Return t if the X display supports shades of gray.\n\
7071 Note that color displays do support shades of gray.\n\
7072 The optional argument DISPLAY specifies which display to ask about.\n\
7073 DISPLAY should be either a frame or a display name (a string).\n\
7074 If omitted or nil, that stands for the selected frame's display.")
7075 (display)
7076 Lisp_Object display;
7077 {
7078 struct w32_display_info *dpyinfo = check_x_display_info (display);
7079
7080 if ((dpyinfo->n_planes * dpyinfo->n_cbits) <= 1)
7081 return Qnil;
7082
7083 return Qt;
7084 }
7085
7086 DEFUN ("x-display-pixel-width", Fx_display_pixel_width, Sx_display_pixel_width,
7087 0, 1, 0,
7088 "Returns the width in pixels of the X display DISPLAY.\n\
7089 The optional argument DISPLAY specifies which display to ask about.\n\
7090 DISPLAY should be either a frame or a display name (a string).\n\
7091 If omitted or nil, that stands for the selected frame's display.")
7092 (display)
7093 Lisp_Object display;
7094 {
7095 struct w32_display_info *dpyinfo = check_x_display_info (display);
7096
7097 return make_number (dpyinfo->width);
7098 }
7099
7100 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
7101 Sx_display_pixel_height, 0, 1, 0,
7102 "Returns the height in pixels of the X display DISPLAY.\n\
7103 The optional argument DISPLAY specifies which display to ask about.\n\
7104 DISPLAY should be either a frame or a display name (a string).\n\
7105 If omitted or nil, that stands for the selected frame's display.")
7106 (display)
7107 Lisp_Object display;
7108 {
7109 struct w32_display_info *dpyinfo = check_x_display_info (display);
7110
7111 return make_number (dpyinfo->height);
7112 }
7113
7114 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
7115 0, 1, 0,
7116 "Returns the number of bitplanes of the display DISPLAY.\n\
7117 The optional argument DISPLAY specifies which display to ask about.\n\
7118 DISPLAY should be either a frame or a display name (a string).\n\
7119 If omitted or nil, that stands for the selected frame's display.")
7120 (display)
7121 Lisp_Object display;
7122 {
7123 struct w32_display_info *dpyinfo = check_x_display_info (display);
7124
7125 return make_number (dpyinfo->n_planes * dpyinfo->n_cbits);
7126 }
7127
7128 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
7129 0, 1, 0,
7130 "Returns the number of color cells of the display DISPLAY.\n\
7131 The optional argument DISPLAY specifies which display to ask about.\n\
7132 DISPLAY should be either a frame or a display name (a string).\n\
7133 If omitted or nil, that stands for the selected frame's display.")
7134 (display)
7135 Lisp_Object display;
7136 {
7137 struct w32_display_info *dpyinfo = check_x_display_info (display);
7138 HDC hdc;
7139 int cap;
7140
7141 hdc = GetDC (dpyinfo->root_window);
7142 if (dpyinfo->has_palette)
7143 cap = GetDeviceCaps (hdc,SIZEPALETTE);
7144 else
7145 cap = GetDeviceCaps (hdc,NUMCOLORS);
7146
7147 ReleaseDC (dpyinfo->root_window, hdc);
7148
7149 return make_number (cap);
7150 }
7151
7152 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
7153 Sx_server_max_request_size,
7154 0, 1, 0,
7155 "Returns the maximum request size of the server of display DISPLAY.\n\
7156 The optional argument DISPLAY specifies which display to ask about.\n\
7157 DISPLAY should be either a frame or a display name (a string).\n\
7158 If omitted or nil, that stands for the selected frame's display.")
7159 (display)
7160 Lisp_Object display;
7161 {
7162 struct w32_display_info *dpyinfo = check_x_display_info (display);
7163
7164 return make_number (1);
7165 }
7166
7167 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
7168 "Returns the vendor ID string of the W32 system (Microsoft).\n\
7169 The optional argument DISPLAY specifies which display to ask about.\n\
7170 DISPLAY should be either a frame or a display name (a string).\n\
7171 If omitted or nil, that stands for the selected frame's display.")
7172 (display)
7173 Lisp_Object display;
7174 {
7175 return build_string ("Microsoft Corp.");
7176 }
7177
7178 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
7179 "Returns the version numbers of the server of display DISPLAY.\n\
7180 The value is a list of three integers: the major and minor\n\
7181 version numbers, and the vendor-specific release\n\
7182 number. See also the function `x-server-vendor'.\n\n\
7183 The optional argument DISPLAY specifies which display to ask about.\n\
7184 DISPLAY should be either a frame or a display name (a string).\n\
7185 If omitted or nil, that stands for the selected frame's display.")
7186 (display)
7187 Lisp_Object display;
7188 {
7189 return Fcons (make_number (w32_major_version),
7190 Fcons (make_number (w32_minor_version),
7191 Fcons (make_number (w32_build_number), Qnil)));
7192 }
7193
7194 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
7195 "Returns the number of screens on the server of display DISPLAY.\n\
7196 The optional argument DISPLAY specifies which display to ask about.\n\
7197 DISPLAY should be either a frame or a display name (a string).\n\
7198 If omitted or nil, that stands for the selected frame's display.")
7199 (display)
7200 Lisp_Object display;
7201 {
7202 return make_number (1);
7203 }
7204
7205 DEFUN ("x-display-mm-height", Fx_display_mm_height, Sx_display_mm_height, 0, 1, 0,
7206 "Returns the height in millimeters of the X display DISPLAY.\n\
7207 The optional argument DISPLAY specifies which display to ask about.\n\
7208 DISPLAY should be either a frame or a display name (a string).\n\
7209 If omitted or nil, that stands for the selected frame's display.")
7210 (display)
7211 Lisp_Object display;
7212 {
7213 struct w32_display_info *dpyinfo = check_x_display_info (display);
7214 HDC hdc;
7215 int cap;
7216
7217 hdc = GetDC (dpyinfo->root_window);
7218
7219 cap = GetDeviceCaps (hdc, VERTSIZE);
7220
7221 ReleaseDC (dpyinfo->root_window, hdc);
7222
7223 return make_number (cap);
7224 }
7225
7226 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
7227 "Returns the width in millimeters of the X display DISPLAY.\n\
7228 The optional argument DISPLAY specifies which display to ask about.\n\
7229 DISPLAY should be either a frame or a display name (a string).\n\
7230 If omitted or nil, that stands for the selected frame's display.")
7231 (display)
7232 Lisp_Object display;
7233 {
7234 struct w32_display_info *dpyinfo = check_x_display_info (display);
7235
7236 HDC hdc;
7237 int cap;
7238
7239 hdc = GetDC (dpyinfo->root_window);
7240
7241 cap = GetDeviceCaps (hdc, HORZSIZE);
7242
7243 ReleaseDC (dpyinfo->root_window, hdc);
7244
7245 return make_number (cap);
7246 }
7247
7248 DEFUN ("x-display-backing-store", Fx_display_backing_store,
7249 Sx_display_backing_store, 0, 1, 0,
7250 "Returns an indication of whether display DISPLAY does backing store.\n\
7251 The value may be `always', `when-mapped', or `not-useful'.\n\
7252 The optional argument DISPLAY specifies which display to ask about.\n\
7253 DISPLAY should be either a frame or a display name (a string).\n\
7254 If omitted or nil, that stands for the selected frame's display.")
7255 (display)
7256 Lisp_Object display;
7257 {
7258 return intern ("not-useful");
7259 }
7260
7261 DEFUN ("x-display-visual-class", Fx_display_visual_class,
7262 Sx_display_visual_class, 0, 1, 0,
7263 "Returns the visual class of the display DISPLAY.\n\
7264 The value is one of the symbols `static-gray', `gray-scale',\n\
7265 `static-color', `pseudo-color', `true-color', or `direct-color'.\n\n\
7266 The optional argument DISPLAY specifies which display to ask about.\n\
7267 DISPLAY should be either a frame or a display name (a string).\n\
7268 If omitted or nil, that stands for the selected frame's display.")
7269 (display)
7270 Lisp_Object display;
7271 {
7272 struct w32_display_info *dpyinfo = check_x_display_info (display);
7273
7274 #if 0
7275 switch (dpyinfo->visual->class)
7276 {
7277 case StaticGray: return (intern ("static-gray"));
7278 case GrayScale: return (intern ("gray-scale"));
7279 case StaticColor: return (intern ("static-color"));
7280 case PseudoColor: return (intern ("pseudo-color"));
7281 case TrueColor: return (intern ("true-color"));
7282 case DirectColor: return (intern ("direct-color"));
7283 default:
7284 error ("Display has an unknown visual class");
7285 }
7286 #endif
7287
7288 error ("Display has an unknown visual class");
7289 }
7290
7291 DEFUN ("x-display-save-under", Fx_display_save_under,
7292 Sx_display_save_under, 0, 1, 0,
7293 "Returns t if the display DISPLAY supports the save-under feature.\n\
7294 The optional argument DISPLAY specifies which display to ask about.\n\
7295 DISPLAY should be either a frame or a display name (a string).\n\
7296 If omitted or nil, that stands for the selected frame's display.")
7297 (display)
7298 Lisp_Object display;
7299 {
7300 return Qnil;
7301 }
7302 \f
7303 int
7304 x_pixel_width (f)
7305 register struct frame *f;
7306 {
7307 return PIXEL_WIDTH (f);
7308 }
7309
7310 int
7311 x_pixel_height (f)
7312 register struct frame *f;
7313 {
7314 return PIXEL_HEIGHT (f);
7315 }
7316
7317 int
7318 x_char_width (f)
7319 register struct frame *f;
7320 {
7321 return FONT_WIDTH (f->output_data.w32->font);
7322 }
7323
7324 int
7325 x_char_height (f)
7326 register struct frame *f;
7327 {
7328 return f->output_data.w32->line_height;
7329 }
7330
7331 int
7332 x_screen_planes (f)
7333 register struct frame *f;
7334 {
7335 return FRAME_W32_DISPLAY_INFO (f)->n_planes;
7336 }
7337 \f
7338 /* Return the display structure for the display named NAME.
7339 Open a new connection if necessary. */
7340
7341 struct w32_display_info *
7342 x_display_info_for_name (name)
7343 Lisp_Object name;
7344 {
7345 Lisp_Object names;
7346 struct w32_display_info *dpyinfo;
7347
7348 CHECK_STRING (name, 0);
7349
7350 for (dpyinfo = &one_w32_display_info, names = w32_display_name_list;
7351 dpyinfo;
7352 dpyinfo = dpyinfo->next, names = XCDR (names))
7353 {
7354 Lisp_Object tem;
7355 tem = Fstring_equal (XCAR (XCAR (names)), name);
7356 if (!NILP (tem))
7357 return dpyinfo;
7358 }
7359
7360 /* Use this general default value to start with. */
7361 Vx_resource_name = Vinvocation_name;
7362
7363 validate_x_resource_name ();
7364
7365 dpyinfo = w32_term_init (name, (unsigned char *)0,
7366 (char *) XSTRING (Vx_resource_name)->data);
7367
7368 if (dpyinfo == 0)
7369 error ("Cannot connect to server %s", XSTRING (name)->data);
7370
7371 w32_in_use = 1;
7372 XSETFASTINT (Vwindow_system_version, 3);
7373
7374 return dpyinfo;
7375 }
7376
7377 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
7378 1, 3, 0, "Open a connection to a server.\n\
7379 DISPLAY is the name of the display to connect to.\n\
7380 Optional second arg XRM-STRING is a string of resources in xrdb format.\n\
7381 If the optional third arg MUST-SUCCEED is non-nil,\n\
7382 terminate Emacs if we can't open the connection.")
7383 (display, xrm_string, must_succeed)
7384 Lisp_Object display, xrm_string, must_succeed;
7385 {
7386 unsigned char *xrm_option;
7387 struct w32_display_info *dpyinfo;
7388
7389 CHECK_STRING (display, 0);
7390 if (! NILP (xrm_string))
7391 CHECK_STRING (xrm_string, 1);
7392
7393 if (! EQ (Vwindow_system, intern ("w32")))
7394 error ("Not using Microsoft Windows");
7395
7396 /* Allow color mapping to be defined externally; first look in user's
7397 HOME directory, then in Emacs etc dir for a file called rgb.txt. */
7398 {
7399 Lisp_Object color_file;
7400 struct gcpro gcpro1;
7401
7402 color_file = build_string("~/rgb.txt");
7403
7404 GCPRO1 (color_file);
7405
7406 if (NILP (Ffile_readable_p (color_file)))
7407 color_file =
7408 Fexpand_file_name (build_string ("rgb.txt"),
7409 Fsymbol_value (intern ("data-directory")));
7410
7411 Vw32_color_map = Fw32_load_color_file (color_file);
7412
7413 UNGCPRO;
7414 }
7415 if (NILP (Vw32_color_map))
7416 Vw32_color_map = Fw32_default_color_map ();
7417
7418 if (! NILP (xrm_string))
7419 xrm_option = (unsigned char *) XSTRING (xrm_string)->data;
7420 else
7421 xrm_option = (unsigned char *) 0;
7422
7423 /* Use this general default value to start with. */
7424 /* First remove .exe suffix from invocation-name - it looks ugly. */
7425 {
7426 char basename[ MAX_PATH ], *str;
7427
7428 strcpy (basename, XSTRING (Vinvocation_name)->data);
7429 str = strrchr (basename, '.');
7430 if (str) *str = 0;
7431 Vinvocation_name = build_string (basename);
7432 }
7433 Vx_resource_name = Vinvocation_name;
7434
7435 validate_x_resource_name ();
7436
7437 /* This is what opens the connection and sets x_current_display.
7438 This also initializes many symbols, such as those used for input. */
7439 dpyinfo = w32_term_init (display, xrm_option,
7440 (char *) XSTRING (Vx_resource_name)->data);
7441
7442 if (dpyinfo == 0)
7443 {
7444 if (!NILP (must_succeed))
7445 fatal ("Cannot connect to server %s.\n",
7446 XSTRING (display)->data);
7447 else
7448 error ("Cannot connect to server %s", XSTRING (display)->data);
7449 }
7450
7451 w32_in_use = 1;
7452
7453 XSETFASTINT (Vwindow_system_version, 3);
7454 return Qnil;
7455 }
7456
7457 DEFUN ("x-close-connection", Fx_close_connection,
7458 Sx_close_connection, 1, 1, 0,
7459 "Close the connection to DISPLAY's server.\n\
7460 For DISPLAY, specify either a frame or a display name (a string).\n\
7461 If DISPLAY is nil, that stands for the selected frame's display.")
7462 (display)
7463 Lisp_Object display;
7464 {
7465 struct w32_display_info *dpyinfo = check_x_display_info (display);
7466 int i;
7467
7468 if (dpyinfo->reference_count > 0)
7469 error ("Display still has frames on it");
7470
7471 BLOCK_INPUT;
7472 /* Free the fonts in the font table. */
7473 for (i = 0; i < dpyinfo->n_fonts; i++)
7474 if (dpyinfo->font_table[i].name)
7475 {
7476 if (dpyinfo->font_table[i].name != dpyinfo->font_table[i].full_name)
7477 xfree (dpyinfo->font_table[i].full_name);
7478 xfree (dpyinfo->font_table[i].name);
7479 w32_unload_font (dpyinfo, dpyinfo->font_table[i].font);
7480 }
7481 x_destroy_all_bitmaps (dpyinfo);
7482
7483 x_delete_display (dpyinfo);
7484 UNBLOCK_INPUT;
7485
7486 return Qnil;
7487 }
7488
7489 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
7490 "Return the list of display names that Emacs has connections to.")
7491 ()
7492 {
7493 Lisp_Object tail, result;
7494
7495 result = Qnil;
7496 for (tail = w32_display_name_list; ! NILP (tail); tail = XCDR (tail))
7497 result = Fcons (XCAR (XCAR (tail)), result);
7498
7499 return result;
7500 }
7501
7502 DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
7503 "If ON is non-nil, report errors as soon as the erring request is made.\n\
7504 If ON is nil, allow buffering of requests.\n\
7505 This is a noop on W32 systems.\n\
7506 The optional second argument DISPLAY specifies which display to act on.\n\
7507 DISPLAY should be either a frame or a display name (a string).\n\
7508 If DISPLAY is omitted or nil, that stands for the selected frame's display.")
7509 (on, display)
7510 Lisp_Object display, on;
7511 {
7512 return Qnil;
7513 }
7514
7515 \f
7516 \f
7517 /***********************************************************************
7518 Image types
7519 ***********************************************************************/
7520
7521 /* Value is the number of elements of vector VECTOR. */
7522
7523 #define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR))
7524
7525 /* List of supported image types. Use define_image_type to add new
7526 types. Use lookup_image_type to find a type for a given symbol. */
7527
7528 static struct image_type *image_types;
7529
7530 /* The symbol `image' which is the car of the lists used to represent
7531 images in Lisp. */
7532
7533 extern Lisp_Object Qimage;
7534
7535 /* The symbol `xbm' which is used as the type symbol for XBM images. */
7536
7537 Lisp_Object Qxbm;
7538
7539 /* Keywords. */
7540
7541 extern Lisp_Object QCwidth, QCheight, QCforeground, QCbackground, QCfile;
7542 extern Lisp_Object QCdata;
7543 Lisp_Object QCtype, QCascent, QCmargin, QCrelief;
7544 Lisp_Object QCalgorithm, QCcolor_symbols, QCheuristic_mask;
7545 Lisp_Object QCindex;
7546
7547 /* Other symbols. */
7548
7549 Lisp_Object Qlaplace;
7550
7551 /* Time in seconds after which images should be removed from the cache
7552 if not displayed. */
7553
7554 Lisp_Object Vimage_cache_eviction_delay;
7555
7556 /* Function prototypes. */
7557
7558 static void define_image_type P_ ((struct image_type *type));
7559 static struct image_type *lookup_image_type P_ ((Lisp_Object symbol));
7560 static void image_error P_ ((char *format, Lisp_Object, Lisp_Object));
7561 static void x_laplace P_ ((struct frame *, struct image *));
7562 static int x_build_heuristic_mask P_ ((struct frame *, struct image *,
7563 Lisp_Object));
7564
7565
7566 /* Define a new image type from TYPE. This adds a copy of TYPE to
7567 image_types and adds the symbol *TYPE->type to Vimage_types. */
7568
7569 static void
7570 define_image_type (type)
7571 struct image_type *type;
7572 {
7573 /* Make a copy of TYPE to avoid a bus error in a dumped Emacs.
7574 The initialized data segment is read-only. */
7575 struct image_type *p = (struct image_type *) xmalloc (sizeof *p);
7576 bcopy (type, p, sizeof *p);
7577 p->next = image_types;
7578 image_types = p;
7579 Vimage_types = Fcons (*p->type, Vimage_types);
7580 }
7581
7582
7583 /* Look up image type SYMBOL, and return a pointer to its image_type
7584 structure. Value is null if SYMBOL is not a known image type. */
7585
7586 static INLINE struct image_type *
7587 lookup_image_type (symbol)
7588 Lisp_Object symbol;
7589 {
7590 struct image_type *type;
7591
7592 for (type = image_types; type; type = type->next)
7593 if (EQ (symbol, *type->type))
7594 break;
7595
7596 return type;
7597 }
7598
7599
7600 /* Value is non-zero if OBJECT is a valid Lisp image specification. A
7601 valid image specification is a list whose car is the symbol
7602 `image', and whose rest is a property list. The property list must
7603 contain a value for key `:type'. That value must be the name of a
7604 supported image type. The rest of the property list depends on the
7605 image type. */
7606
7607 int
7608 valid_image_p (object)
7609 Lisp_Object object;
7610 {
7611 int valid_p = 0;
7612
7613 if (CONSP (object) && EQ (XCAR (object), Qimage))
7614 {
7615 Lisp_Object symbol = Fplist_get (XCDR (object), QCtype);
7616 struct image_type *type = lookup_image_type (symbol);
7617
7618 if (type)
7619 valid_p = type->valid_p (object);
7620 }
7621
7622 return valid_p;
7623 }
7624
7625
7626 /* Log error message with format string FORMAT and argument ARG.
7627 Signaling an error, e.g. when an image cannot be loaded, is not a
7628 good idea because this would interrupt redisplay, and the error
7629 message display would lead to another redisplay. This function
7630 therefore simply displays a message. */
7631
7632 static void
7633 image_error (format, arg1, arg2)
7634 char *format;
7635 Lisp_Object arg1, arg2;
7636 {
7637 add_to_log (format, arg1, arg2);
7638 }
7639
7640
7641 \f
7642 /***********************************************************************
7643 Image specifications
7644 ***********************************************************************/
7645
7646 enum image_value_type
7647 {
7648 IMAGE_DONT_CHECK_VALUE_TYPE,
7649 IMAGE_STRING_VALUE,
7650 IMAGE_SYMBOL_VALUE,
7651 IMAGE_POSITIVE_INTEGER_VALUE,
7652 IMAGE_NON_NEGATIVE_INTEGER_VALUE,
7653 IMAGE_ASCENT_VALUE,
7654 IMAGE_INTEGER_VALUE,
7655 IMAGE_FUNCTION_VALUE,
7656 IMAGE_NUMBER_VALUE,
7657 IMAGE_BOOL_VALUE
7658 };
7659
7660 /* Structure used when parsing image specifications. */
7661
7662 struct image_keyword
7663 {
7664 /* Name of keyword. */
7665 char *name;
7666
7667 /* The type of value allowed. */
7668 enum image_value_type type;
7669
7670 /* Non-zero means key must be present. */
7671 int mandatory_p;
7672
7673 /* Used to recognize duplicate keywords in a property list. */
7674 int count;
7675
7676 /* The value that was found. */
7677 Lisp_Object value;
7678 };
7679
7680
7681 static int parse_image_spec P_ ((Lisp_Object, struct image_keyword *,
7682 int, Lisp_Object));
7683 static Lisp_Object image_spec_value P_ ((Lisp_Object, Lisp_Object, int *));
7684
7685
7686 /* Parse image spec SPEC according to KEYWORDS. A valid image spec
7687 has the format (image KEYWORD VALUE ...). One of the keyword/
7688 value pairs must be `:type TYPE'. KEYWORDS is a vector of
7689 image_keywords structures of size NKEYWORDS describing other
7690 allowed keyword/value pairs. Value is non-zero if SPEC is valid. */
7691
7692 static int
7693 parse_image_spec (spec, keywords, nkeywords, type)
7694 Lisp_Object spec;
7695 struct image_keyword *keywords;
7696 int nkeywords;
7697 Lisp_Object type;
7698 {
7699 int i;
7700 Lisp_Object plist;
7701
7702 if (!CONSP (spec) || !EQ (XCAR (spec), Qimage))
7703 return 0;
7704
7705 plist = XCDR (spec);
7706 while (CONSP (plist))
7707 {
7708 Lisp_Object key, value;
7709
7710 /* First element of a pair must be a symbol. */
7711 key = XCAR (plist);
7712 plist = XCDR (plist);
7713 if (!SYMBOLP (key))
7714 return 0;
7715
7716 /* There must follow a value. */
7717 if (!CONSP (plist))
7718 return 0;
7719 value = XCAR (plist);
7720 plist = XCDR (plist);
7721
7722 /* Find key in KEYWORDS. Error if not found. */
7723 for (i = 0; i < nkeywords; ++i)
7724 if (strcmp (keywords[i].name, XSYMBOL (key)->name->data) == 0)
7725 break;
7726
7727 if (i == nkeywords)
7728 continue;
7729
7730 /* Record that we recognized the keyword. If a keywords
7731 was found more than once, it's an error. */
7732 keywords[i].value = value;
7733 ++keywords[i].count;
7734
7735 if (keywords[i].count > 1)
7736 return 0;
7737
7738 /* Check type of value against allowed type. */
7739 switch (keywords[i].type)
7740 {
7741 case IMAGE_STRING_VALUE:
7742 if (!STRINGP (value))
7743 return 0;
7744 break;
7745
7746 case IMAGE_SYMBOL_VALUE:
7747 if (!SYMBOLP (value))
7748 return 0;
7749 break;
7750
7751 case IMAGE_POSITIVE_INTEGER_VALUE:
7752 if (!INTEGERP (value) || XINT (value) <= 0)
7753 return 0;
7754 break;
7755
7756 case IMAGE_ASCENT_VALUE:
7757 if (SYMBOLP (value) && EQ (value, Qcenter))
7758 break;
7759 else if (INTEGERP (value)
7760 && XINT (value) >= 0
7761 && XINT (value) <= 100)
7762 break;
7763 return 0;
7764
7765 case IMAGE_NON_NEGATIVE_INTEGER_VALUE:
7766 if (!INTEGERP (value) || XINT (value) < 0)
7767 return 0;
7768 break;
7769
7770 case IMAGE_DONT_CHECK_VALUE_TYPE:
7771 break;
7772
7773 case IMAGE_FUNCTION_VALUE:
7774 value = indirect_function (value);
7775 if (SUBRP (value)
7776 || COMPILEDP (value)
7777 || (CONSP (value) && EQ (XCAR (value), Qlambda)))
7778 break;
7779 return 0;
7780
7781 case IMAGE_NUMBER_VALUE:
7782 if (!INTEGERP (value) && !FLOATP (value))
7783 return 0;
7784 break;
7785
7786 case IMAGE_INTEGER_VALUE:
7787 if (!INTEGERP (value))
7788 return 0;
7789 break;
7790
7791 case IMAGE_BOOL_VALUE:
7792 if (!NILP (value) && !EQ (value, Qt))
7793 return 0;
7794 break;
7795
7796 default:
7797 abort ();
7798 break;
7799 }
7800
7801 if (EQ (key, QCtype) && !EQ (type, value))
7802 return 0;
7803 }
7804
7805 /* Check that all mandatory fields are present. */
7806 for (i = 0; i < nkeywords; ++i)
7807 if (keywords[i].mandatory_p && keywords[i].count == 0)
7808 return 0;
7809
7810 return NILP (plist);
7811 }
7812
7813
7814 /* Return the value of KEY in image specification SPEC. Value is nil
7815 if KEY is not present in SPEC. if FOUND is not null, set *FOUND
7816 to 1 if KEY was found in SPEC, set it to 0 otherwise. */
7817
7818 static Lisp_Object
7819 image_spec_value (spec, key, found)
7820 Lisp_Object spec, key;
7821 int *found;
7822 {
7823 Lisp_Object tail;
7824
7825 xassert (valid_image_p (spec));
7826
7827 for (tail = XCDR (spec);
7828 CONSP (tail) && CONSP (XCDR (tail));
7829 tail = XCDR (XCDR (tail)))
7830 {
7831 if (EQ (XCAR (tail), key))
7832 {
7833 if (found)
7834 *found = 1;
7835 return XCAR (XCDR (tail));
7836 }
7837 }
7838
7839 if (found)
7840 *found = 0;
7841 return Qnil;
7842 }
7843
7844
7845
7846 \f
7847 /***********************************************************************
7848 Image type independent image structures
7849 ***********************************************************************/
7850
7851 static struct image *make_image P_ ((Lisp_Object spec, unsigned hash));
7852 static void free_image P_ ((struct frame *f, struct image *img));
7853
7854
7855 /* Allocate and return a new image structure for image specification
7856 SPEC. SPEC has a hash value of HASH. */
7857
7858 static struct image *
7859 make_image (spec, hash)
7860 Lisp_Object spec;
7861 unsigned hash;
7862 {
7863 struct image *img = (struct image *) xmalloc (sizeof *img);
7864
7865 xassert (valid_image_p (spec));
7866 bzero (img, sizeof *img);
7867 img->type = lookup_image_type (image_spec_value (spec, QCtype, NULL));
7868 xassert (img->type != NULL);
7869 img->spec = spec;
7870 img->data.lisp_val = Qnil;
7871 img->ascent = DEFAULT_IMAGE_ASCENT;
7872 img->hash = hash;
7873 return img;
7874 }
7875
7876
7877 /* Free image IMG which was used on frame F, including its resources. */
7878
7879 static void
7880 free_image (f, img)
7881 struct frame *f;
7882 struct image *img;
7883 {
7884 if (img)
7885 {
7886 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
7887
7888 /* Remove IMG from the hash table of its cache. */
7889 if (img->prev)
7890 img->prev->next = img->next;
7891 else
7892 c->buckets[img->hash % IMAGE_CACHE_BUCKETS_SIZE] = img->next;
7893
7894 if (img->next)
7895 img->next->prev = img->prev;
7896
7897 c->images[img->id] = NULL;
7898
7899 /* Free resources, then free IMG. */
7900 img->type->free (f, img);
7901 xfree (img);
7902 }
7903 }
7904
7905
7906 /* Prepare image IMG for display on frame F. Must be called before
7907 drawing an image. */
7908
7909 void
7910 prepare_image_for_display (f, img)
7911 struct frame *f;
7912 struct image *img;
7913 {
7914 EMACS_TIME t;
7915
7916 /* We're about to display IMG, so set its timestamp to `now'. */
7917 EMACS_GET_TIME (t);
7918 img->timestamp = EMACS_SECS (t);
7919
7920 /* If IMG doesn't have a pixmap yet, load it now, using the image
7921 type dependent loader function. */
7922 if (img->pixmap == 0 && !img->load_failed_p)
7923 img->load_failed_p = img->type->load (f, img) == 0;
7924 }
7925
7926
7927 /* Value is the number of pixels for the ascent of image IMG when
7928 drawn in face FACE. */
7929
7930 int
7931 image_ascent (img, face)
7932 struct image *img;
7933 struct face *face;
7934 {
7935 int height = img->height + img->margin;
7936 int ascent;
7937
7938 if (img->ascent == CENTERED_IMAGE_ASCENT)
7939 {
7940 if (face->font)
7941 ascent = height / 2 - (FONT_DESCENT(face->font)
7942 - FONT_BASE(face->font)) / 2;
7943 else
7944 ascent = height / 2;
7945 }
7946 else
7947 ascent = height * img->ascent / 100.0;
7948
7949 return ascent;
7950 }
7951
7952
7953 \f
7954 /***********************************************************************
7955 Helper functions for X image types
7956 ***********************************************************************/
7957
7958 static void x_clear_image P_ ((struct frame *f, struct image *img));
7959 static unsigned long x_alloc_image_color P_ ((struct frame *f,
7960 struct image *img,
7961 Lisp_Object color_name,
7962 unsigned long dflt));
7963
7964 /* Free X resources of image IMG which is used on frame F. */
7965
7966 static void
7967 x_clear_image (f, img)
7968 struct frame *f;
7969 struct image *img;
7970 {
7971 #if 0 /* TODO: W32 image support */
7972
7973 if (img->pixmap)
7974 {
7975 BLOCK_INPUT;
7976 XFreePixmap (NULL, img->pixmap);
7977 img->pixmap = 0;
7978 UNBLOCK_INPUT;
7979 }
7980
7981 if (img->ncolors)
7982 {
7983 int class = FRAME_W32_DISPLAY_INFO (f)->visual->class;
7984
7985 /* If display has an immutable color map, freeing colors is not
7986 necessary and some servers don't allow it. So don't do it. */
7987 if (class != StaticColor
7988 && class != StaticGray
7989 && class != TrueColor)
7990 {
7991 Colormap cmap;
7992 BLOCK_INPUT;
7993 cmap = DefaultColormapOfScreen (FRAME_W32_DISPLAY_INFO (f)->screen);
7994 XFreeColors (FRAME_W32_DISPLAY (f), cmap, img->colors,
7995 img->ncolors, 0);
7996 UNBLOCK_INPUT;
7997 }
7998
7999 xfree (img->colors);
8000 img->colors = NULL;
8001 img->ncolors = 0;
8002 }
8003 #endif
8004 }
8005
8006
8007 /* Allocate color COLOR_NAME for image IMG on frame F. If color
8008 cannot be allocated, use DFLT. Add a newly allocated color to
8009 IMG->colors, so that it can be freed again. Value is the pixel
8010 color. */
8011
8012 static unsigned long
8013 x_alloc_image_color (f, img, color_name, dflt)
8014 struct frame *f;
8015 struct image *img;
8016 Lisp_Object color_name;
8017 unsigned long dflt;
8018 {
8019 #if 0 /* TODO: allocing colors. */
8020 XColor color;
8021 unsigned long result;
8022
8023 xassert (STRINGP (color_name));
8024
8025 if (w32_defined_color (f, XSTRING (color_name)->data, &color, 1))
8026 {
8027 /* This isn't called frequently so we get away with simply
8028 reallocating the color vector to the needed size, here. */
8029 ++img->ncolors;
8030 img->colors =
8031 (unsigned long *) xrealloc (img->colors,
8032 img->ncolors * sizeof *img->colors);
8033 img->colors[img->ncolors - 1] = color.pixel;
8034 result = color.pixel;
8035 }
8036 else
8037 result = dflt;
8038 return result;
8039 #endif
8040 return 0;
8041 }
8042
8043
8044 \f
8045 /***********************************************************************
8046 Image Cache
8047 ***********************************************************************/
8048
8049 static void cache_image P_ ((struct frame *f, struct image *img));
8050
8051
8052 /* Return a new, initialized image cache that is allocated from the
8053 heap. Call free_image_cache to free an image cache. */
8054
8055 struct image_cache *
8056 make_image_cache ()
8057 {
8058 struct image_cache *c = (struct image_cache *) xmalloc (sizeof *c);
8059 int size;
8060
8061 bzero (c, sizeof *c);
8062 c->size = 50;
8063 c->images = (struct image **) xmalloc (c->size * sizeof *c->images);
8064 size = IMAGE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
8065 c->buckets = (struct image **) xmalloc (size);
8066 bzero (c->buckets, size);
8067 return c;
8068 }
8069
8070
8071 /* Free image cache of frame F. Be aware that X frames share images
8072 caches. */
8073
8074 void
8075 free_image_cache (f)
8076 struct frame *f;
8077 {
8078 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
8079 if (c)
8080 {
8081 int i;
8082
8083 /* Cache should not be referenced by any frame when freed. */
8084 xassert (c->refcount == 0);
8085
8086 for (i = 0; i < c->used; ++i)
8087 free_image (f, c->images[i]);
8088 xfree (c->images);
8089 xfree (c);
8090 xfree (c->buckets);
8091 FRAME_X_IMAGE_CACHE (f) = NULL;
8092 }
8093 }
8094
8095
8096 /* Clear image cache of frame F. FORCE_P non-zero means free all
8097 images. FORCE_P zero means clear only images that haven't been
8098 displayed for some time. Should be called from time to time to
8099 reduce the number of loaded images. If image-eviction-seconds is
8100 non-nil, this frees images in the cache which weren't displayed for
8101 at least that many seconds. */
8102
8103 void
8104 clear_image_cache (f, force_p)
8105 struct frame *f;
8106 int force_p;
8107 {
8108 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
8109
8110 if (c && INTEGERP (Vimage_cache_eviction_delay))
8111 {
8112 EMACS_TIME t;
8113 unsigned long old;
8114 int i, any_freed_p = 0;
8115
8116 EMACS_GET_TIME (t);
8117 old = EMACS_SECS (t) - XFASTINT (Vimage_cache_eviction_delay);
8118
8119 for (i = 0; i < c->used; ++i)
8120 {
8121 struct image *img = c->images[i];
8122 if (img != NULL
8123 && (force_p
8124 || (img->timestamp > old)))
8125 {
8126 free_image (f, img);
8127 any_freed_p = 1;
8128 }
8129 }
8130
8131 /* We may be clearing the image cache because, for example,
8132 Emacs was iconified for a longer period of time. In that
8133 case, current matrices may still contain references to
8134 images freed above. So, clear these matrices. */
8135 if (any_freed_p)
8136 {
8137 clear_current_matrices (f);
8138 ++windows_or_buffers_changed;
8139 }
8140 }
8141 }
8142
8143
8144 DEFUN ("clear-image-cache", Fclear_image_cache, Sclear_image_cache,
8145 0, 1, 0,
8146 "Clear the image cache of FRAME.\n\
8147 FRAME nil or omitted means use the selected frame.\n\
8148 FRAME t means clear the image caches of all frames.")
8149 (frame)
8150 Lisp_Object frame;
8151 {
8152 if (EQ (frame, Qt))
8153 {
8154 Lisp_Object tail;
8155
8156 FOR_EACH_FRAME (tail, frame)
8157 if (FRAME_W32_P (XFRAME (frame)))
8158 clear_image_cache (XFRAME (frame), 1);
8159 }
8160 else
8161 clear_image_cache (check_x_frame (frame), 1);
8162
8163 return Qnil;
8164 }
8165
8166
8167 /* Return the id of image with Lisp specification SPEC on frame F.
8168 SPEC must be a valid Lisp image specification (see valid_image_p). */
8169
8170 int
8171 lookup_image (f, spec)
8172 struct frame *f;
8173 Lisp_Object spec;
8174 {
8175 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
8176 struct image *img;
8177 int i;
8178 unsigned hash;
8179 struct gcpro gcpro1;
8180 EMACS_TIME now;
8181
8182 /* F must be a window-system frame, and SPEC must be a valid image
8183 specification. */
8184 xassert (FRAME_WINDOW_P (f));
8185 xassert (valid_image_p (spec));
8186
8187 GCPRO1 (spec);
8188
8189 /* Look up SPEC in the hash table of the image cache. */
8190 hash = sxhash (spec, 0);
8191 i = hash % IMAGE_CACHE_BUCKETS_SIZE;
8192
8193 for (img = c->buckets[i]; img; img = img->next)
8194 if (img->hash == hash && !NILP (Fequal (img->spec, spec)))
8195 break;
8196
8197 /* If not found, create a new image and cache it. */
8198 if (img == NULL)
8199 {
8200 img = make_image (spec, hash);
8201 cache_image (f, img);
8202 img->load_failed_p = img->type->load (f, img) == 0;
8203 xassert (!interrupt_input_blocked);
8204
8205 /* If we can't load the image, and we don't have a width and
8206 height, use some arbitrary width and height so that we can
8207 draw a rectangle for it. */
8208 if (img->load_failed_p)
8209 {
8210 Lisp_Object value;
8211
8212 value = image_spec_value (spec, QCwidth, NULL);
8213 img->width = (INTEGERP (value)
8214 ? XFASTINT (value) : DEFAULT_IMAGE_WIDTH);
8215 value = image_spec_value (spec, QCheight, NULL);
8216 img->height = (INTEGERP (value)
8217 ? XFASTINT (value) : DEFAULT_IMAGE_HEIGHT);
8218 }
8219 else
8220 {
8221 /* Handle image type independent image attributes
8222 `:ascent PERCENT', `:margin MARGIN', `:relief RELIEF'. */
8223 Lisp_Object ascent, margin, relief, algorithm, heuristic_mask;
8224 Lisp_Object file;
8225
8226 ascent = image_spec_value (spec, QCascent, NULL);
8227 if (INTEGERP (ascent))
8228 img->ascent = XFASTINT (ascent);
8229 else if (EQ (ascent, Qcenter))
8230 img->ascent = CENTERED_IMAGE_ASCENT;
8231
8232 margin = image_spec_value (spec, QCmargin, NULL);
8233 if (INTEGERP (margin) && XINT (margin) >= 0)
8234 img->margin = XFASTINT (margin);
8235
8236 relief = image_spec_value (spec, QCrelief, NULL);
8237 if (INTEGERP (relief))
8238 {
8239 img->relief = XINT (relief);
8240 img->margin += abs (img->relief);
8241 }
8242
8243 /* Should we apply a Laplace edge-detection algorithm? */
8244 algorithm = image_spec_value (spec, QCalgorithm, NULL);
8245 if (img->pixmap && EQ (algorithm, Qlaplace))
8246 x_laplace (f, img);
8247
8248 /* Should we built a mask heuristically? */
8249 heuristic_mask = image_spec_value (spec, QCheuristic_mask, NULL);
8250 if (img->pixmap && !img->mask && !NILP (heuristic_mask))
8251 x_build_heuristic_mask (f, img, heuristic_mask);
8252 }
8253 }
8254
8255 /* We're using IMG, so set its timestamp to `now'. */
8256 EMACS_GET_TIME (now);
8257 img->timestamp = EMACS_SECS (now);
8258
8259 UNGCPRO;
8260
8261 /* Value is the image id. */
8262 return img->id;
8263 }
8264
8265
8266 /* Cache image IMG in the image cache of frame F. */
8267
8268 static void
8269 cache_image (f, img)
8270 struct frame *f;
8271 struct image *img;
8272 {
8273 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
8274 int i;
8275
8276 /* Find a free slot in c->images. */
8277 for (i = 0; i < c->used; ++i)
8278 if (c->images[i] == NULL)
8279 break;
8280
8281 /* If no free slot found, maybe enlarge c->images. */
8282 if (i == c->used && c->used == c->size)
8283 {
8284 c->size *= 2;
8285 c->images = (struct image **) xrealloc (c->images,
8286 c->size * sizeof *c->images);
8287 }
8288
8289 /* Add IMG to c->images, and assign IMG an id. */
8290 c->images[i] = img;
8291 img->id = i;
8292 if (i == c->used)
8293 ++c->used;
8294
8295 /* Add IMG to the cache's hash table. */
8296 i = img->hash % IMAGE_CACHE_BUCKETS_SIZE;
8297 img->next = c->buckets[i];
8298 if (img->next)
8299 img->next->prev = img;
8300 img->prev = NULL;
8301 c->buckets[i] = img;
8302 }
8303
8304
8305 /* Call FN on every image in the image cache of frame F. Used to mark
8306 Lisp Objects in the image cache. */
8307
8308 void
8309 forall_images_in_image_cache (f, fn)
8310 struct frame *f;
8311 void (*fn) P_ ((struct image *img));
8312 {
8313 if (FRAME_LIVE_P (f) && FRAME_W32_P (f))
8314 {
8315 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
8316 if (c)
8317 {
8318 int i;
8319 for (i = 0; i < c->used; ++i)
8320 if (c->images[i])
8321 fn (c->images[i]);
8322 }
8323 }
8324 }
8325
8326
8327 \f
8328 /***********************************************************************
8329 W32 support code
8330 ***********************************************************************/
8331
8332 #if 0 /* TODO: W32 specific image code. */
8333
8334 static int x_create_x_image_and_pixmap P_ ((struct frame *, int, int, int,
8335 XImage **, Pixmap *));
8336 static void x_destroy_x_image P_ ((XImage *));
8337 static void x_put_x_image P_ ((struct frame *, XImage *, Pixmap, int, int));
8338
8339
8340 /* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on
8341 frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created.
8342 Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated
8343 via xmalloc. Print error messages via image_error if an error
8344 occurs. Value is non-zero if successful. */
8345
8346 static int
8347 x_create_x_image_and_pixmap (f, width, height, depth, ximg, pixmap)
8348 struct frame *f;
8349 int width, height, depth;
8350 XImage **ximg;
8351 Pixmap *pixmap;
8352 {
8353 #if 0 /* TODO: Image support for W32 */
8354 Display *display = FRAME_W32_DISPLAY (f);
8355 Screen *screen = FRAME_X_SCREEN (f);
8356 Window window = FRAME_W32_WINDOW (f);
8357
8358 xassert (interrupt_input_blocked);
8359
8360 if (depth <= 0)
8361 depth = DefaultDepthOfScreen (screen);
8362 *ximg = XCreateImage (display, DefaultVisualOfScreen (screen),
8363 depth, ZPixmap, 0, NULL, width, height,
8364 depth > 16 ? 32 : depth > 8 ? 16 : 8, 0);
8365 if (*ximg == NULL)
8366 {
8367 image_error ("Unable to allocate X image", Qnil, Qnil);
8368 return 0;
8369 }
8370
8371 /* Allocate image raster. */
8372 (*ximg)->data = (char *) xmalloc ((*ximg)->bytes_per_line * height);
8373
8374 /* Allocate a pixmap of the same size. */
8375 *pixmap = XCreatePixmap (display, window, width, height, depth);
8376 if (*pixmap == 0)
8377 {
8378 x_destroy_x_image (*ximg);
8379 *ximg = NULL;
8380 image_error ("Unable to create X pixmap", Qnil, Qnil);
8381 return 0;
8382 }
8383 #endif
8384 return 1;
8385 }
8386
8387
8388 /* Destroy XImage XIMG. Free XIMG->data. */
8389
8390 static void
8391 x_destroy_x_image (ximg)
8392 XImage *ximg;
8393 {
8394 xassert (interrupt_input_blocked);
8395 if (ximg)
8396 {
8397 xfree (ximg->data);
8398 ximg->data = NULL;
8399 XDestroyImage (ximg);
8400 }
8401 }
8402
8403
8404 /* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT
8405 are width and height of both the image and pixmap. */
8406
8407 static void
8408 x_put_x_image (f, ximg, pixmap, width, height)
8409 struct frame *f;
8410 XImage *ximg;
8411 Pixmap pixmap;
8412 {
8413 GC gc;
8414
8415 xassert (interrupt_input_blocked);
8416 gc = XCreateGC (NULL, pixmap, 0, NULL);
8417 XPutImage (NULL, pixmap, gc, ximg, 0, 0, 0, 0, width, height);
8418 XFreeGC (NULL, gc);
8419 }
8420
8421 #endif
8422
8423 \f
8424 /***********************************************************************
8425 Searching files
8426 ***********************************************************************/
8427
8428 static Lisp_Object x_find_image_file P_ ((Lisp_Object));
8429
8430 /* Find image file FILE. Look in data-directory, then
8431 x-bitmap-file-path. Value is the full name of the file found, or
8432 nil if not found. */
8433
8434 static Lisp_Object
8435 x_find_image_file (file)
8436 Lisp_Object file;
8437 {
8438 Lisp_Object file_found, search_path;
8439 struct gcpro gcpro1, gcpro2;
8440 int fd;
8441
8442 file_found = Qnil;
8443 search_path = Fcons (Vdata_directory, Vx_bitmap_file_path);
8444 GCPRO2 (file_found, search_path);
8445
8446 /* Try to find FILE in data-directory, then x-bitmap-file-path. */
8447 fd = openp (search_path, file, "", &file_found, 0);
8448
8449 if (fd == -1)
8450 file_found = Qnil;
8451 else
8452 close (fd);
8453
8454 UNGCPRO;
8455 return file_found;
8456 }
8457
8458
8459 \f
8460 /***********************************************************************
8461 XBM images
8462 ***********************************************************************/
8463
8464 static int xbm_load P_ ((struct frame *f, struct image *img));
8465 static int xbm_load_image_from_file P_ ((struct frame *f, struct image *img,
8466 Lisp_Object file));
8467 static int xbm_image_p P_ ((Lisp_Object object));
8468 static int xbm_read_bitmap_file_data P_ ((char *, int *, int *,
8469 unsigned char **));
8470
8471
8472 /* Indices of image specification fields in xbm_format, below. */
8473
8474 enum xbm_keyword_index
8475 {
8476 XBM_TYPE,
8477 XBM_FILE,
8478 XBM_WIDTH,
8479 XBM_HEIGHT,
8480 XBM_DATA,
8481 XBM_FOREGROUND,
8482 XBM_BACKGROUND,
8483 XBM_ASCENT,
8484 XBM_MARGIN,
8485 XBM_RELIEF,
8486 XBM_ALGORITHM,
8487 XBM_HEURISTIC_MASK,
8488 XBM_LAST
8489 };
8490
8491 /* Vector of image_keyword structures describing the format
8492 of valid XBM image specifications. */
8493
8494 static struct image_keyword xbm_format[XBM_LAST] =
8495 {
8496 {":type", IMAGE_SYMBOL_VALUE, 1},
8497 {":file", IMAGE_STRING_VALUE, 0},
8498 {":width", IMAGE_POSITIVE_INTEGER_VALUE, 0},
8499 {":height", IMAGE_POSITIVE_INTEGER_VALUE, 0},
8500 {":data", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8501 {":foreground", IMAGE_STRING_VALUE, 0},
8502 {":background", IMAGE_STRING_VALUE, 0},
8503 {":ascent", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
8504 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
8505 {":relief", IMAGE_INTEGER_VALUE, 0},
8506 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8507 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
8508 };
8509
8510 /* Structure describing the image type XBM. */
8511
8512 static struct image_type xbm_type =
8513 {
8514 &Qxbm,
8515 xbm_image_p,
8516 xbm_load,
8517 x_clear_image,
8518 NULL
8519 };
8520
8521 /* Tokens returned from xbm_scan. */
8522
8523 enum xbm_token
8524 {
8525 XBM_TK_IDENT = 256,
8526 XBM_TK_NUMBER
8527 };
8528
8529
8530 /* Return non-zero if OBJECT is a valid XBM-type image specification.
8531 A valid specification is a list starting with the symbol `image'
8532 The rest of the list is a property list which must contain an
8533 entry `:type xbm..
8534
8535 If the specification specifies a file to load, it must contain
8536 an entry `:file FILENAME' where FILENAME is a string.
8537
8538 If the specification is for a bitmap loaded from memory it must
8539 contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where
8540 WIDTH and HEIGHT are integers > 0. DATA may be:
8541
8542 1. a string large enough to hold the bitmap data, i.e. it must
8543 have a size >= (WIDTH + 7) / 8 * HEIGHT
8544
8545 2. a bool-vector of size >= WIDTH * HEIGHT
8546
8547 3. a vector of strings or bool-vectors, one for each line of the
8548 bitmap.
8549
8550 Both the file and data forms may contain the additional entries
8551 `:background COLOR' and `:foreground COLOR'. If not present,
8552 foreground and background of the frame on which the image is
8553 displayed, is used. */
8554
8555 static int
8556 xbm_image_p (object)
8557 Lisp_Object object;
8558 {
8559 struct image_keyword kw[XBM_LAST];
8560
8561 bcopy (xbm_format, kw, sizeof kw);
8562 if (!parse_image_spec (object, kw, XBM_LAST, Qxbm))
8563 return 0;
8564
8565 xassert (EQ (kw[XBM_TYPE].value, Qxbm));
8566
8567 if (kw[XBM_FILE].count)
8568 {
8569 if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_DATA].count)
8570 return 0;
8571 }
8572 else
8573 {
8574 Lisp_Object data;
8575 int width, height;
8576
8577 /* Entries for `:width', `:height' and `:data' must be present. */
8578 if (!kw[XBM_WIDTH].count
8579 || !kw[XBM_HEIGHT].count
8580 || !kw[XBM_DATA].count)
8581 return 0;
8582
8583 data = kw[XBM_DATA].value;
8584 width = XFASTINT (kw[XBM_WIDTH].value);
8585 height = XFASTINT (kw[XBM_HEIGHT].value);
8586
8587 /* Check type of data, and width and height against contents of
8588 data. */
8589 if (VECTORP (data))
8590 {
8591 int i;
8592
8593 /* Number of elements of the vector must be >= height. */
8594 if (XVECTOR (data)->size < height)
8595 return 0;
8596
8597 /* Each string or bool-vector in data must be large enough
8598 for one line of the image. */
8599 for (i = 0; i < height; ++i)
8600 {
8601 Lisp_Object elt = XVECTOR (data)->contents[i];
8602
8603 if (STRINGP (elt))
8604 {
8605 if (XSTRING (elt)->size
8606 < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR)
8607 return 0;
8608 }
8609 else if (BOOL_VECTOR_P (elt))
8610 {
8611 if (XBOOL_VECTOR (elt)->size < width)
8612 return 0;
8613 }
8614 else
8615 return 0;
8616 }
8617 }
8618 else if (STRINGP (data))
8619 {
8620 if (XSTRING (data)->size
8621 < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR * height)
8622 return 0;
8623 }
8624 else if (BOOL_VECTOR_P (data))
8625 {
8626 if (XBOOL_VECTOR (data)->size < width * height)
8627 return 0;
8628 }
8629 else
8630 return 0;
8631 }
8632
8633 /* Baseline must be a value between 0 and 100 (a percentage). */
8634 if (kw[XBM_ASCENT].count
8635 && XFASTINT (kw[XBM_ASCENT].value) > 100)
8636 return 0;
8637
8638 return 1;
8639 }
8640
8641
8642 /* Scan a bitmap file. FP is the stream to read from. Value is
8643 either an enumerator from enum xbm_token, or a character for a
8644 single-character token, or 0 at end of file. If scanning an
8645 identifier, store the lexeme of the identifier in SVAL. If
8646 scanning a number, store its value in *IVAL. */
8647
8648 static int
8649 xbm_scan (fp, sval, ival)
8650 FILE *fp;
8651 char *sval;
8652 int *ival;
8653 {
8654 int c;
8655
8656 /* Skip white space. */
8657 while ((c = fgetc (fp)) != EOF && isspace (c))
8658 ;
8659
8660 if (c == EOF)
8661 c = 0;
8662 else if (isdigit (c))
8663 {
8664 int value = 0, digit;
8665
8666 if (c == '0')
8667 {
8668 c = fgetc (fp);
8669 if (c == 'x' || c == 'X')
8670 {
8671 while ((c = fgetc (fp)) != EOF)
8672 {
8673 if (isdigit (c))
8674 digit = c - '0';
8675 else if (c >= 'a' && c <= 'f')
8676 digit = c - 'a' + 10;
8677 else if (c >= 'A' && c <= 'F')
8678 digit = c - 'A' + 10;
8679 else
8680 break;
8681 value = 16 * value + digit;
8682 }
8683 }
8684 else if (isdigit (c))
8685 {
8686 value = c - '0';
8687 while ((c = fgetc (fp)) != EOF
8688 && isdigit (c))
8689 value = 8 * value + c - '0';
8690 }
8691 }
8692 else
8693 {
8694 value = c - '0';
8695 while ((c = fgetc (fp)) != EOF
8696 && isdigit (c))
8697 value = 10 * value + c - '0';
8698 }
8699
8700 if (c != EOF)
8701 ungetc (c, fp);
8702 *ival = value;
8703 c = XBM_TK_NUMBER;
8704 }
8705 else if (isalpha (c) || c == '_')
8706 {
8707 *sval++ = c;
8708 while ((c = fgetc (fp)) != EOF
8709 && (isalnum (c) || c == '_'))
8710 *sval++ = c;
8711 *sval = 0;
8712 if (c != EOF)
8713 ungetc (c, fp);
8714 c = XBM_TK_IDENT;
8715 }
8716
8717 return c;
8718 }
8719
8720
8721 /* Replacement for XReadBitmapFileData which isn't available under old
8722 X versions. FILE is the name of the bitmap file to read. Set
8723 *WIDTH and *HEIGHT to the width and height of the image. Return in
8724 *DATA the bitmap data allocated with xmalloc. Value is non-zero if
8725 successful. */
8726
8727 static int
8728 xbm_read_bitmap_file_data (file, width, height, data)
8729 char *file;
8730 int *width, *height;
8731 unsigned char **data;
8732 {
8733 FILE *fp;
8734 char buffer[BUFSIZ];
8735 int padding_p = 0;
8736 int v10 = 0;
8737 int bytes_per_line, i, nbytes;
8738 unsigned char *p;
8739 int value;
8740 int LA1;
8741
8742 #define match() \
8743 LA1 = xbm_scan (fp, buffer, &value)
8744
8745 #define expect(TOKEN) \
8746 if (LA1 != (TOKEN)) \
8747 goto failure; \
8748 else \
8749 match ()
8750
8751 #define expect_ident(IDENT) \
8752 if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \
8753 match (); \
8754 else \
8755 goto failure
8756
8757 fp = fopen (file, "r");
8758 if (fp == NULL)
8759 return 0;
8760
8761 *width = *height = -1;
8762 *data = NULL;
8763 LA1 = xbm_scan (fp, buffer, &value);
8764
8765 /* Parse defines for width, height and hot-spots. */
8766 while (LA1 == '#')
8767 {
8768 match ();
8769 expect_ident ("define");
8770 expect (XBM_TK_IDENT);
8771
8772 if (LA1 == XBM_TK_NUMBER);
8773 {
8774 char *p = strrchr (buffer, '_');
8775 p = p ? p + 1 : buffer;
8776 if (strcmp (p, "width") == 0)
8777 *width = value;
8778 else if (strcmp (p, "height") == 0)
8779 *height = value;
8780 }
8781 expect (XBM_TK_NUMBER);
8782 }
8783
8784 if (*width < 0 || *height < 0)
8785 goto failure;
8786
8787 /* Parse bits. Must start with `static'. */
8788 expect_ident ("static");
8789 if (LA1 == XBM_TK_IDENT)
8790 {
8791 if (strcmp (buffer, "unsigned") == 0)
8792 {
8793 match ();
8794 expect_ident ("char");
8795 }
8796 else if (strcmp (buffer, "short") == 0)
8797 {
8798 match ();
8799 v10 = 1;
8800 if (*width % 16 && *width % 16 < 9)
8801 padding_p = 1;
8802 }
8803 else if (strcmp (buffer, "char") == 0)
8804 match ();
8805 else
8806 goto failure;
8807 }
8808 else
8809 goto failure;
8810
8811 expect (XBM_TK_IDENT);
8812 expect ('[');
8813 expect (']');
8814 expect ('=');
8815 expect ('{');
8816
8817 bytes_per_line = (*width + 7) / 8 + padding_p;
8818 nbytes = bytes_per_line * *height;
8819 p = *data = (char *) xmalloc (nbytes);
8820
8821 if (v10)
8822 {
8823
8824 for (i = 0; i < nbytes; i += 2)
8825 {
8826 int val = value;
8827 expect (XBM_TK_NUMBER);
8828
8829 *p++ = val;
8830 if (!padding_p || ((i + 2) % bytes_per_line))
8831 *p++ = value >> 8;
8832
8833 if (LA1 == ',' || LA1 == '}')
8834 match ();
8835 else
8836 goto failure;
8837 }
8838 }
8839 else
8840 {
8841 for (i = 0; i < nbytes; ++i)
8842 {
8843 int val = value;
8844 expect (XBM_TK_NUMBER);
8845
8846 *p++ = val;
8847
8848 if (LA1 == ',' || LA1 == '}')
8849 match ();
8850 else
8851 goto failure;
8852 }
8853 }
8854
8855 fclose (fp);
8856 return 1;
8857
8858 failure:
8859
8860 fclose (fp);
8861 if (*data)
8862 {
8863 xfree (*data);
8864 *data = NULL;
8865 }
8866 return 0;
8867
8868 #undef match
8869 #undef expect
8870 #undef expect_ident
8871 }
8872
8873
8874 /* Load XBM image IMG which will be displayed on frame F from file
8875 SPECIFIED_FILE. Value is non-zero if successful. */
8876
8877 static int
8878 xbm_load_image_from_file (f, img, specified_file)
8879 struct frame *f;
8880 struct image *img;
8881 Lisp_Object specified_file;
8882 {
8883 int rc;
8884 unsigned char *data;
8885 int success_p = 0;
8886 Lisp_Object file;
8887 struct gcpro gcpro1;
8888
8889 xassert (STRINGP (specified_file));
8890 file = Qnil;
8891 GCPRO1 (file);
8892
8893 file = x_find_image_file (specified_file);
8894 if (!STRINGP (file))
8895 {
8896 image_error ("Cannot find image file `%s'", specified_file, Qnil);
8897 UNGCPRO;
8898 return 0;
8899 }
8900
8901 rc = xbm_read_bitmap_file_data (XSTRING (file)->data, &img->width,
8902 &img->height, &data);
8903 if (rc)
8904 {
8905 int depth = one_w32_display_info.n_cbits;
8906 unsigned long foreground = FRAME_FOREGROUND_PIXEL (f);
8907 unsigned long background = FRAME_BACKGROUND_PIXEL (f);
8908 Lisp_Object value;
8909
8910 xassert (img->width > 0 && img->height > 0);
8911
8912 /* Get foreground and background colors, maybe allocate colors. */
8913 value = image_spec_value (img->spec, QCforeground, NULL);
8914 if (!NILP (value))
8915 foreground = x_alloc_image_color (f, img, value, foreground);
8916
8917 value = image_spec_value (img->spec, QCbackground, NULL);
8918 if (!NILP (value))
8919 background = x_alloc_image_color (f, img, value, background);
8920
8921 #if 0 /* TODO : Port image display to W32 */
8922 BLOCK_INPUT;
8923 img->pixmap
8924 = XCreatePixmapFromBitmapData (FRAME_W32_DISPLAY (f),
8925 FRAME_W32_WINDOW (f),
8926 data,
8927 img->width, img->height,
8928 foreground, background,
8929 depth);
8930 xfree (data);
8931
8932 if (img->pixmap == 0)
8933 {
8934 x_clear_image (f, img);
8935 image_error ("Unable to create X pixmap for `%s'", file, Qnil);
8936 }
8937 else
8938 success_p = 1;
8939
8940 UNBLOCK_INPUT;
8941 #endif
8942 }
8943 else
8944 image_error ("Error loading XBM image `%s'", img->spec, Qnil);
8945
8946 UNGCPRO;
8947 return success_p;
8948 }
8949
8950
8951 /* Fill image IMG which is used on frame F with pixmap data. Value is
8952 non-zero if successful. */
8953
8954 static int
8955 xbm_load (f, img)
8956 struct frame *f;
8957 struct image *img;
8958 {
8959 int success_p = 0;
8960 Lisp_Object file_name;
8961
8962 xassert (xbm_image_p (img->spec));
8963
8964 /* If IMG->spec specifies a file name, create a non-file spec from it. */
8965 file_name = image_spec_value (img->spec, QCfile, NULL);
8966 if (STRINGP (file_name))
8967 success_p = xbm_load_image_from_file (f, img, file_name);
8968 else
8969 {
8970 struct image_keyword fmt[XBM_LAST];
8971 Lisp_Object data;
8972 int depth;
8973 unsigned long foreground = FRAME_FOREGROUND_PIXEL (f);
8974 unsigned long background = FRAME_BACKGROUND_PIXEL (f);
8975 char *bits;
8976 int parsed_p;
8977
8978 /* Parse the list specification. */
8979 bcopy (xbm_format, fmt, sizeof fmt);
8980 parsed_p = parse_image_spec (img->spec, fmt, XBM_LAST, Qxbm);
8981 xassert (parsed_p);
8982
8983 /* Get specified width, and height. */
8984 img->width = XFASTINT (fmt[XBM_WIDTH].value);
8985 img->height = XFASTINT (fmt[XBM_HEIGHT].value);
8986 xassert (img->width > 0 && img->height > 0);
8987
8988 BLOCK_INPUT;
8989
8990 if (fmt[XBM_ASCENT].count)
8991 img->ascent = XFASTINT (fmt[XBM_ASCENT].value);
8992
8993 /* Get foreground and background colors, maybe allocate colors. */
8994 if (fmt[XBM_FOREGROUND].count)
8995 foreground = x_alloc_image_color (f, img, fmt[XBM_FOREGROUND].value,
8996 foreground);
8997 if (fmt[XBM_BACKGROUND].count)
8998 background = x_alloc_image_color (f, img, fmt[XBM_BACKGROUND].value,
8999 background);
9000
9001 /* Set bits to the bitmap image data. */
9002 data = fmt[XBM_DATA].value;
9003 if (VECTORP (data))
9004 {
9005 int i;
9006 char *p;
9007 int nbytes = (img->width + BITS_PER_CHAR - 1) / BITS_PER_CHAR;
9008
9009 p = bits = (char *) alloca (nbytes * img->height);
9010 for (i = 0; i < img->height; ++i, p += nbytes)
9011 {
9012 Lisp_Object line = XVECTOR (data)->contents[i];
9013 if (STRINGP (line))
9014 bcopy (XSTRING (line)->data, p, nbytes);
9015 else
9016 bcopy (XBOOL_VECTOR (line)->data, p, nbytes);
9017 }
9018 }
9019 else if (STRINGP (data))
9020 bits = XSTRING (data)->data;
9021 else
9022 bits = XBOOL_VECTOR (data)->data;
9023
9024 #if 0 /* TODO : W32 XPM code */
9025 /* Create the pixmap. */
9026 depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
9027 img->pixmap
9028 = XCreatePixmapFromBitmapData (FRAME_W32_DISPLAY (f),
9029 FRAME_W32_WINDOW (f),
9030 bits,
9031 img->width, img->height,
9032 foreground, background,
9033 depth);
9034 #endif /* TODO */
9035
9036 if (img->pixmap)
9037 success_p = 1;
9038 else
9039 {
9040 image_error ("Unable to create pixmap for XBM image `%s'",
9041 img->spec, Qnil);
9042 x_clear_image (f, img);
9043 }
9044
9045 UNBLOCK_INPUT;
9046 }
9047
9048 return success_p;
9049 }
9050
9051
9052 \f
9053 /***********************************************************************
9054 XPM images
9055 ***********************************************************************/
9056
9057 #if HAVE_XPM
9058
9059 static int xpm_image_p P_ ((Lisp_Object object));
9060 static int xpm_load P_ ((struct frame *f, struct image *img));
9061 static int xpm_valid_color_symbols_p P_ ((Lisp_Object));
9062
9063 #include "X11/xpm.h"
9064
9065 /* The symbol `xpm' identifying XPM-format images. */
9066
9067 Lisp_Object Qxpm;
9068
9069 /* Indices of image specification fields in xpm_format, below. */
9070
9071 enum xpm_keyword_index
9072 {
9073 XPM_TYPE,
9074 XPM_FILE,
9075 XPM_DATA,
9076 XPM_ASCENT,
9077 XPM_MARGIN,
9078 XPM_RELIEF,
9079 XPM_ALGORITHM,
9080 XPM_HEURISTIC_MASK,
9081 XPM_COLOR_SYMBOLS,
9082 XPM_LAST
9083 };
9084
9085 /* Vector of image_keyword structures describing the format
9086 of valid XPM image specifications. */
9087
9088 static struct image_keyword xpm_format[XPM_LAST] =
9089 {
9090 {":type", IMAGE_SYMBOL_VALUE, 1},
9091 {":file", IMAGE_STRING_VALUE, 0},
9092 {":data", IMAGE_STRING_VALUE, 0},
9093 {":ascent", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
9094 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
9095 {":relief", IMAGE_INTEGER_VALUE, 0},
9096 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9097 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9098 {":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
9099 };
9100
9101 /* Structure describing the image type XBM. */
9102
9103 static struct image_type xpm_type =
9104 {
9105 &Qxpm,
9106 xpm_image_p,
9107 xpm_load,
9108 x_clear_image,
9109 NULL
9110 };
9111
9112
9113 /* Value is non-zero if COLOR_SYMBOLS is a valid color symbols list
9114 for XPM images. Such a list must consist of conses whose car and
9115 cdr are strings. */
9116
9117 static int
9118 xpm_valid_color_symbols_p (color_symbols)
9119 Lisp_Object color_symbols;
9120 {
9121 while (CONSP (color_symbols))
9122 {
9123 Lisp_Object sym = XCAR (color_symbols);
9124 if (!CONSP (sym)
9125 || !STRINGP (XCAR (sym))
9126 || !STRINGP (XCDR (sym)))
9127 break;
9128 color_symbols = XCDR (color_symbols);
9129 }
9130
9131 return NILP (color_symbols);
9132 }
9133
9134
9135 /* Value is non-zero if OBJECT is a valid XPM image specification. */
9136
9137 static int
9138 xpm_image_p (object)
9139 Lisp_Object object;
9140 {
9141 struct image_keyword fmt[XPM_LAST];
9142 bcopy (xpm_format, fmt, sizeof fmt);
9143 return (parse_image_spec (object, fmt, XPM_LAST, Qxpm)
9144 /* Either `:file' or `:data' must be present. */
9145 && fmt[XPM_FILE].count + fmt[XPM_DATA].count == 1
9146 /* Either no `:color-symbols' or it's a list of conses
9147 whose car and cdr are strings. */
9148 && (fmt[XPM_COLOR_SYMBOLS].count == 0
9149 || xpm_valid_color_symbols_p (fmt[XPM_COLOR_SYMBOLS].value))
9150 && (fmt[XPM_ASCENT].count == 0
9151 || XFASTINT (fmt[XPM_ASCENT].value) < 100));
9152 }
9153
9154
9155 /* Load image IMG which will be displayed on frame F. Value is
9156 non-zero if successful. */
9157
9158 static int
9159 xpm_load (f, img)
9160 struct frame *f;
9161 struct image *img;
9162 {
9163 int rc, i;
9164 XpmAttributes attrs;
9165 Lisp_Object specified_file, color_symbols;
9166
9167 /* Configure the XPM lib. Use the visual of frame F. Allocate
9168 close colors. Return colors allocated. */
9169 bzero (&attrs, sizeof attrs);
9170 attrs.visual = FRAME_X_VISUAL (f);
9171 attrs.colormap = FRAME_X_COLORMAP (f);
9172 attrs.valuemask |= XpmVisual;
9173 attrs.valuemask |= XpmColormap;
9174 attrs.valuemask |= XpmReturnAllocPixels;
9175 #ifdef XpmAllocCloseColors
9176 attrs.alloc_close_colors = 1;
9177 attrs.valuemask |= XpmAllocCloseColors;
9178 #else
9179 attrs.closeness = 600;
9180 attrs.valuemask |= XpmCloseness;
9181 #endif
9182
9183 /* If image specification contains symbolic color definitions, add
9184 these to `attrs'. */
9185 color_symbols = image_spec_value (img->spec, QCcolor_symbols, NULL);
9186 if (CONSP (color_symbols))
9187 {
9188 Lisp_Object tail;
9189 XpmColorSymbol *xpm_syms;
9190 int i, size;
9191
9192 attrs.valuemask |= XpmColorSymbols;
9193
9194 /* Count number of symbols. */
9195 attrs.numsymbols = 0;
9196 for (tail = color_symbols; CONSP (tail); tail = XCDR (tail))
9197 ++attrs.numsymbols;
9198
9199 /* Allocate an XpmColorSymbol array. */
9200 size = attrs.numsymbols * sizeof *xpm_syms;
9201 xpm_syms = (XpmColorSymbol *) alloca (size);
9202 bzero (xpm_syms, size);
9203 attrs.colorsymbols = xpm_syms;
9204
9205 /* Fill the color symbol array. */
9206 for (tail = color_symbols, i = 0;
9207 CONSP (tail);
9208 ++i, tail = XCDR (tail))
9209 {
9210 Lisp_Object name = XCAR (XCAR (tail));
9211 Lisp_Object color = XCDR (XCAR (tail));
9212 xpm_syms[i].name = (char *) alloca (XSTRING (name)->size + 1);
9213 strcpy (xpm_syms[i].name, XSTRING (name)->data);
9214 xpm_syms[i].value = (char *) alloca (XSTRING (color)->size + 1);
9215 strcpy (xpm_syms[i].value, XSTRING (color)->data);
9216 }
9217 }
9218
9219 /* Create a pixmap for the image, either from a file, or from a
9220 string buffer containing data in the same format as an XPM file. */
9221 BLOCK_INPUT;
9222 specified_file = image_spec_value (img->spec, QCfile, NULL);
9223 if (STRINGP (specified_file))
9224 {
9225 Lisp_Object file = x_find_image_file (specified_file);
9226 if (!STRINGP (file))
9227 {
9228 image_error ("Cannot find image file `%s'", specified_file, Qnil);
9229 UNBLOCK_INPUT;
9230 return 0;
9231 }
9232
9233 rc = XpmReadFileToPixmap (NULL, FRAME_W32_WINDOW (f),
9234 XSTRING (file)->data, &img->pixmap, &img->mask,
9235 &attrs);
9236 }
9237 else
9238 {
9239 Lisp_Object buffer = image_spec_value (img->spec, QCdata, NULL);
9240 rc = XpmCreatePixmapFromBuffer (NULL, FRAME_W32_WINDOW (f),
9241 XSTRING (buffer)->data,
9242 &img->pixmap, &img->mask,
9243 &attrs);
9244 }
9245 UNBLOCK_INPUT;
9246
9247 if (rc == XpmSuccess)
9248 {
9249 /* Remember allocated colors. */
9250 img->ncolors = attrs.nalloc_pixels;
9251 img->colors = (unsigned long *) xmalloc (img->ncolors
9252 * sizeof *img->colors);
9253 for (i = 0; i < attrs.nalloc_pixels; ++i)
9254 img->colors[i] = attrs.alloc_pixels[i];
9255
9256 img->width = attrs.width;
9257 img->height = attrs.height;
9258 xassert (img->width > 0 && img->height > 0);
9259
9260 /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */
9261 BLOCK_INPUT;
9262 XpmFreeAttributes (&attrs);
9263 UNBLOCK_INPUT;
9264 }
9265 else
9266 {
9267 switch (rc)
9268 {
9269 case XpmOpenFailed:
9270 image_error ("Error opening XPM file (%s)", img->spec, Qnil);
9271 break;
9272
9273 case XpmFileInvalid:
9274 image_error ("Invalid XPM file (%s)", img->spec, Qnil);
9275 break;
9276
9277 case XpmNoMemory:
9278 image_error ("Out of memory (%s)", img->spec, Qnil);
9279 break;
9280
9281 case XpmColorFailed:
9282 image_error ("Color allocation error (%s)", img->spec, Qnil);
9283 break;
9284
9285 default:
9286 image_error ("Unknown error (%s)", img->spec, Qnil);
9287 break;
9288 }
9289 }
9290
9291 return rc == XpmSuccess;
9292 }
9293
9294 #endif /* HAVE_XPM != 0 */
9295
9296 \f
9297 #if 0 /* TODO : Color tables on W32. */
9298 /***********************************************************************
9299 Color table
9300 ***********************************************************************/
9301
9302 /* An entry in the color table mapping an RGB color to a pixel color. */
9303
9304 struct ct_color
9305 {
9306 int r, g, b;
9307 unsigned long pixel;
9308
9309 /* Next in color table collision list. */
9310 struct ct_color *next;
9311 };
9312
9313 /* The bucket vector size to use. Must be prime. */
9314
9315 #define CT_SIZE 101
9316
9317 /* Value is a hash of the RGB color given by R, G, and B. */
9318
9319 #define CT_HASH_RGB(R, G, B) (((R) << 16) ^ ((G) << 8) ^ (B))
9320
9321 /* The color hash table. */
9322
9323 struct ct_color **ct_table;
9324
9325 /* Number of entries in the color table. */
9326
9327 int ct_colors_allocated;
9328
9329 /* Function prototypes. */
9330
9331 static void init_color_table P_ ((void));
9332 static void free_color_table P_ ((void));
9333 static unsigned long *colors_in_color_table P_ ((int *n));
9334 static unsigned long lookup_rgb_color P_ ((struct frame *f, int r, int g, int b));
9335 static unsigned long lookup_pixel_color P_ ((struct frame *f, unsigned long p));
9336
9337
9338 /* Initialize the color table. */
9339
9340 static void
9341 init_color_table ()
9342 {
9343 int size = CT_SIZE * sizeof (*ct_table);
9344 ct_table = (struct ct_color **) xmalloc (size);
9345 bzero (ct_table, size);
9346 ct_colors_allocated = 0;
9347 }
9348
9349
9350 /* Free memory associated with the color table. */
9351
9352 static void
9353 free_color_table ()
9354 {
9355 int i;
9356 struct ct_color *p, *next;
9357
9358 for (i = 0; i < CT_SIZE; ++i)
9359 for (p = ct_table[i]; p; p = next)
9360 {
9361 next = p->next;
9362 xfree (p);
9363 }
9364
9365 xfree (ct_table);
9366 ct_table = NULL;
9367 }
9368
9369
9370 /* Value is a pixel color for RGB color R, G, B on frame F. If an
9371 entry for that color already is in the color table, return the
9372 pixel color of that entry. Otherwise, allocate a new color for R,
9373 G, B, and make an entry in the color table. */
9374
9375 static unsigned long
9376 lookup_rgb_color (f, r, g, b)
9377 struct frame *f;
9378 int r, g, b;
9379 {
9380 unsigned hash = CT_HASH_RGB (r, g, b);
9381 int i = hash % CT_SIZE;
9382 struct ct_color *p;
9383
9384 for (p = ct_table[i]; p; p = p->next)
9385 if (p->r == r && p->g == g && p->b == b)
9386 break;
9387
9388 if (p == NULL)
9389 {
9390 COLORREF color;
9391 Colormap cmap;
9392 int rc;
9393
9394 color = PALETTERGB (r, g, b);
9395
9396 ++ct_colors_allocated;
9397
9398 p = (struct ct_color *) xmalloc (sizeof *p);
9399 p->r = r;
9400 p->g = g;
9401 p->b = b;
9402 p->pixel = color;
9403 p->next = ct_table[i];
9404 ct_table[i] = p;
9405 }
9406
9407 return p->pixel;
9408 }
9409
9410
9411 /* Look up pixel color PIXEL which is used on frame F in the color
9412 table. If not already present, allocate it. Value is PIXEL. */
9413
9414 static unsigned long
9415 lookup_pixel_color (f, pixel)
9416 struct frame *f;
9417 unsigned long pixel;
9418 {
9419 int i = pixel % CT_SIZE;
9420 struct ct_color *p;
9421
9422 for (p = ct_table[i]; p; p = p->next)
9423 if (p->pixel == pixel)
9424 break;
9425
9426 if (p == NULL)
9427 {
9428 XColor color;
9429 Colormap cmap;
9430 int rc;
9431
9432 BLOCK_INPUT;
9433
9434 cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f));
9435 color.pixel = pixel;
9436 XQueryColor (NULL, cmap, &color);
9437 rc = x_alloc_nearest_color (f, cmap, &color);
9438 UNBLOCK_INPUT;
9439
9440 if (rc)
9441 {
9442 ++ct_colors_allocated;
9443
9444 p = (struct ct_color *) xmalloc (sizeof *p);
9445 p->r = color.red;
9446 p->g = color.green;
9447 p->b = color.blue;
9448 p->pixel = pixel;
9449 p->next = ct_table[i];
9450 ct_table[i] = p;
9451 }
9452 else
9453 return FRAME_FOREGROUND_PIXEL (f);
9454 }
9455 return p->pixel;
9456 }
9457
9458
9459 /* Value is a vector of all pixel colors contained in the color table,
9460 allocated via xmalloc. Set *N to the number of colors. */
9461
9462 static unsigned long *
9463 colors_in_color_table (n)
9464 int *n;
9465 {
9466 int i, j;
9467 struct ct_color *p;
9468 unsigned long *colors;
9469
9470 if (ct_colors_allocated == 0)
9471 {
9472 *n = 0;
9473 colors = NULL;
9474 }
9475 else
9476 {
9477 colors = (unsigned long *) xmalloc (ct_colors_allocated
9478 * sizeof *colors);
9479 *n = ct_colors_allocated;
9480
9481 for (i = j = 0; i < CT_SIZE; ++i)
9482 for (p = ct_table[i]; p; p = p->next)
9483 colors[j++] = p->pixel;
9484 }
9485
9486 return colors;
9487 }
9488
9489 #endif /* TODO */
9490
9491 \f
9492 /***********************************************************************
9493 Algorithms
9494 ***********************************************************************/
9495
9496 #if 0 /* TODO : W32 versions of low level algorithms */
9497 static void x_laplace_write_row P_ ((struct frame *, long *,
9498 int, XImage *, int));
9499 static void x_laplace_read_row P_ ((struct frame *, Colormap,
9500 XColor *, int, XImage *, int));
9501
9502
9503 /* Fill COLORS with RGB colors from row Y of image XIMG. F is the
9504 frame we operate on, CMAP is the color-map in effect, and WIDTH is
9505 the width of one row in the image. */
9506
9507 static void
9508 x_laplace_read_row (f, cmap, colors, width, ximg, y)
9509 struct frame *f;
9510 Colormap cmap;
9511 XColor *colors;
9512 int width;
9513 XImage *ximg;
9514 int y;
9515 {
9516 int x;
9517
9518 for (x = 0; x < width; ++x)
9519 colors[x].pixel = XGetPixel (ximg, x, y);
9520
9521 XQueryColors (NULL, cmap, colors, width);
9522 }
9523
9524
9525 /* Write row Y of image XIMG. PIXELS is an array of WIDTH longs
9526 containing the pixel colors to write. F is the frame we are
9527 working on. */
9528
9529 static void
9530 x_laplace_write_row (f, pixels, width, ximg, y)
9531 struct frame *f;
9532 long *pixels;
9533 int width;
9534 XImage *ximg;
9535 int y;
9536 {
9537 int x;
9538
9539 for (x = 0; x < width; ++x)
9540 XPutPixel (ximg, x, y, pixels[x]);
9541 }
9542 #endif
9543
9544 /* Transform image IMG which is used on frame F with a Laplace
9545 edge-detection algorithm. The result is an image that can be used
9546 to draw disabled buttons, for example. */
9547
9548 static void
9549 x_laplace (f, img)
9550 struct frame *f;
9551 struct image *img;
9552 {
9553 #if 0 /* TODO : W32 version */
9554 Colormap cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f));
9555 XImage *ximg, *oimg;
9556 XColor *in[3];
9557 long *out;
9558 Pixmap pixmap;
9559 int x, y, i;
9560 long pixel;
9561 int in_y, out_y, rc;
9562 int mv2 = 45000;
9563
9564 BLOCK_INPUT;
9565
9566 /* Get the X image IMG->pixmap. */
9567 ximg = XGetImage (NULL, img->pixmap,
9568 0, 0, img->width, img->height, ~0, ZPixmap);
9569
9570 /* Allocate 3 input rows, and one output row of colors. */
9571 for (i = 0; i < 3; ++i)
9572 in[i] = (XColor *) alloca (img->width * sizeof (XColor));
9573 out = (long *) alloca (img->width * sizeof (long));
9574
9575 /* Create an X image for output. */
9576 rc = x_create_x_image_and_pixmap (f, img->width, img->height, 0,
9577 &oimg, &pixmap);
9578
9579 /* Fill first two rows. */
9580 x_laplace_read_row (f, cmap, in[0], img->width, ximg, 0);
9581 x_laplace_read_row (f, cmap, in[1], img->width, ximg, 1);
9582 in_y = 2;
9583
9584 /* Write first row, all zeros. */
9585 init_color_table ();
9586 pixel = lookup_rgb_color (f, 0, 0, 0);
9587 for (x = 0; x < img->width; ++x)
9588 out[x] = pixel;
9589 x_laplace_write_row (f, out, img->width, oimg, 0);
9590 out_y = 1;
9591
9592 for (y = 2; y < img->height; ++y)
9593 {
9594 int rowa = y % 3;
9595 int rowb = (y + 2) % 3;
9596
9597 x_laplace_read_row (f, cmap, in[rowa], img->width, ximg, in_y++);
9598
9599 for (x = 0; x < img->width - 2; ++x)
9600 {
9601 int r = in[rowa][x].red + mv2 - in[rowb][x + 2].red;
9602 int g = in[rowa][x].green + mv2 - in[rowb][x + 2].green;
9603 int b = in[rowa][x].blue + mv2 - in[rowb][x + 2].blue;
9604
9605 out[x + 1] = lookup_rgb_color (f, r & 0xffff, g & 0xffff,
9606 b & 0xffff);
9607 }
9608
9609 x_laplace_write_row (f, out, img->width, oimg, out_y++);
9610 }
9611
9612 /* Write last line, all zeros. */
9613 for (x = 0; x < img->width; ++x)
9614 out[x] = pixel;
9615 x_laplace_write_row (f, out, img->width, oimg, out_y);
9616
9617 /* Free the input image, and free resources of IMG. */
9618 XDestroyImage (ximg);
9619 x_clear_image (f, img);
9620
9621 /* Put the output image into pixmap, and destroy it. */
9622 x_put_x_image (f, oimg, pixmap, img->width, img->height);
9623 x_destroy_x_image (oimg);
9624
9625 /* Remember new pixmap and colors in IMG. */
9626 img->pixmap = pixmap;
9627 img->colors = colors_in_color_table (&img->ncolors);
9628 free_color_table ();
9629
9630 UNBLOCK_INPUT;
9631 #endif /* TODO */
9632 }
9633
9634
9635 /* Build a mask for image IMG which is used on frame F. FILE is the
9636 name of an image file, for error messages. HOW determines how to
9637 determine the background color of IMG. If it is a list '(R G B)',
9638 with R, G, and B being integers >= 0, take that as the color of the
9639 background. Otherwise, determine the background color of IMG
9640 heuristically. Value is non-zero if successful. */
9641
9642 static int
9643 x_build_heuristic_mask (f, img, how)
9644 struct frame *f;
9645 struct image *img;
9646 Lisp_Object how;
9647 {
9648 #if 0 /* TODO : W32 version */
9649 Display *dpy = FRAME_W32_DISPLAY (f);
9650 XImage *ximg, *mask_img;
9651 int x, y, rc, look_at_corners_p;
9652 unsigned long bg;
9653
9654 BLOCK_INPUT;
9655
9656 /* Create an image and pixmap serving as mask. */
9657 rc = x_create_x_image_and_pixmap (f, img->width, img->height, 1,
9658 &mask_img, &img->mask);
9659 if (!rc)
9660 {
9661 UNBLOCK_INPUT;
9662 return 0;
9663 }
9664
9665 /* Get the X image of IMG->pixmap. */
9666 ximg = XGetImage (dpy, img->pixmap, 0, 0, img->width, img->height,
9667 ~0, ZPixmap);
9668
9669 /* Determine the background color of ximg. If HOW is `(R G B)'
9670 take that as color. Otherwise, try to determine the color
9671 heuristically. */
9672 look_at_corners_p = 1;
9673
9674 if (CONSP (how))
9675 {
9676 int rgb[3], i = 0;
9677
9678 while (i < 3
9679 && CONSP (how)
9680 && NATNUMP (XCAR (how)))
9681 {
9682 rgb[i] = XFASTINT (XCAR (how)) & 0xffff;
9683 how = XCDR (how);
9684 }
9685
9686 if (i == 3 && NILP (how))
9687 {
9688 char color_name[30];
9689 XColor exact, color;
9690 Colormap cmap;
9691
9692 sprintf (color_name, "#%04x%04x%04x", rgb[0], rgb[1], rgb[2]);
9693
9694 cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f));
9695 if (XLookupColor (dpy, cmap, color_name, &exact, &color))
9696 {
9697 bg = color.pixel;
9698 look_at_corners_p = 0;
9699 }
9700 }
9701 }
9702
9703 if (look_at_corners_p)
9704 {
9705 unsigned long corners[4];
9706 int i, best_count;
9707
9708 /* Get the colors at the corners of ximg. */
9709 corners[0] = XGetPixel (ximg, 0, 0);
9710 corners[1] = XGetPixel (ximg, img->width - 1, 0);
9711 corners[2] = XGetPixel (ximg, img->width - 1, img->height - 1);
9712 corners[3] = XGetPixel (ximg, 0, img->height - 1);
9713
9714 /* Choose the most frequently found color as background. */
9715 for (i = best_count = 0; i < 4; ++i)
9716 {
9717 int j, n;
9718
9719 for (j = n = 0; j < 4; ++j)
9720 if (corners[i] == corners[j])
9721 ++n;
9722
9723 if (n > best_count)
9724 bg = corners[i], best_count = n;
9725 }
9726 }
9727
9728 /* Set all bits in mask_img to 1 whose color in ximg is different
9729 from the background color bg. */
9730 for (y = 0; y < img->height; ++y)
9731 for (x = 0; x < img->width; ++x)
9732 XPutPixel (mask_img, x, y, XGetPixel (ximg, x, y) != bg);
9733
9734 /* Put mask_img into img->mask. */
9735 x_put_x_image (f, mask_img, img->mask, img->width, img->height);
9736 x_destroy_x_image (mask_img);
9737 XDestroyImage (ximg);
9738
9739 UNBLOCK_INPUT;
9740 #endif /* TODO */
9741
9742 return 1;
9743 }
9744
9745
9746 \f
9747 /***********************************************************************
9748 PBM (mono, gray, color)
9749 ***********************************************************************/
9750 #ifdef HAVE_PBM
9751
9752 static int pbm_image_p P_ ((Lisp_Object object));
9753 static int pbm_load P_ ((struct frame *f, struct image *img));
9754 static int pbm_scan_number P_ ((unsigned char **, unsigned char *));
9755
9756 /* The symbol `pbm' identifying images of this type. */
9757
9758 Lisp_Object Qpbm;
9759
9760 /* Indices of image specification fields in gs_format, below. */
9761
9762 enum pbm_keyword_index
9763 {
9764 PBM_TYPE,
9765 PBM_FILE,
9766 PBM_DATA,
9767 PBM_ASCENT,
9768 PBM_MARGIN,
9769 PBM_RELIEF,
9770 PBM_ALGORITHM,
9771 PBM_HEURISTIC_MASK,
9772 PBM_LAST
9773 };
9774
9775 /* Vector of image_keyword structures describing the format
9776 of valid user-defined image specifications. */
9777
9778 static struct image_keyword pbm_format[PBM_LAST] =
9779 {
9780 {":type", IMAGE_SYMBOL_VALUE, 1},
9781 {":file", IMAGE_STRING_VALUE, 0},
9782 {":data", IMAGE_STRING_VALUE, 0},
9783 {":ascent", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
9784 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
9785 {":relief", IMAGE_INTEGER_VALUE, 0},
9786 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9787 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
9788 };
9789
9790 /* Structure describing the image type `pbm'. */
9791
9792 static struct image_type pbm_type =
9793 {
9794 &Qpbm,
9795 pbm_image_p,
9796 pbm_load,
9797 x_clear_image,
9798 NULL
9799 };
9800
9801
9802 /* Return non-zero if OBJECT is a valid PBM image specification. */
9803
9804 static int
9805 pbm_image_p (object)
9806 Lisp_Object object;
9807 {
9808 struct image_keyword fmt[PBM_LAST];
9809
9810 bcopy (pbm_format, fmt, sizeof fmt);
9811
9812 if (!parse_image_spec (object, fmt, PBM_LAST, Qpbm)
9813 || (fmt[PBM_ASCENT].count
9814 && XFASTINT (fmt[PBM_ASCENT].value) > 100))
9815 return 0;
9816
9817 /* Must specify either :data or :file. */
9818 return fmt[PBM_DATA].count + fmt[PBM_FILE].count == 1;
9819 }
9820
9821
9822 /* Scan a decimal number from *S and return it. Advance *S while
9823 reading the number. END is the end of the string. Value is -1 at
9824 end of input. */
9825
9826 static int
9827 pbm_scan_number (s, end)
9828 unsigned char **s, *end;
9829 {
9830 int c, val = -1;
9831
9832 while (*s < end)
9833 {
9834 /* Skip white-space. */
9835 while (*s < end && (c = *(*s)++, isspace (c)))
9836 ;
9837
9838 if (c == '#')
9839 {
9840 /* Skip comment to end of line. */
9841 while (*s < end && (c = *(*s)++, c != '\n'))
9842 ;
9843 }
9844 else if (isdigit (c))
9845 {
9846 /* Read decimal number. */
9847 val = c - '0';
9848 while (*s < end && (c = *(*s)++, isdigit (c)))
9849 val = 10 * val + c - '0';
9850 break;
9851 }
9852 else
9853 break;
9854 }
9855
9856 return val;
9857 }
9858
9859
9860 /* Read FILE into memory. Value is a pointer to a buffer allocated
9861 with xmalloc holding FILE's contents. Value is null if an error
9862 occured. *SIZE is set to the size of the file. */
9863
9864 static char *
9865 pbm_read_file (file, size)
9866 Lisp_Object file;
9867 int *size;
9868 {
9869 FILE *fp = NULL;
9870 char *buf = NULL;
9871 struct stat st;
9872
9873 if (stat (XSTRING (file)->data, &st) == 0
9874 && (fp = fopen (XSTRING (file)->data, "r")) != NULL
9875 && (buf = (char *) xmalloc (st.st_size),
9876 fread (buf, 1, st.st_size, fp) == st.st_size))
9877 {
9878 *size = st.st_size;
9879 fclose (fp);
9880 }
9881 else
9882 {
9883 if (fp)
9884 fclose (fp);
9885 if (buf)
9886 {
9887 xfree (buf);
9888 buf = NULL;
9889 }
9890 }
9891
9892 return buf;
9893 }
9894
9895
9896 /* Load PBM image IMG for use on frame F. */
9897
9898 static int
9899 pbm_load (f, img)
9900 struct frame *f;
9901 struct image *img;
9902 {
9903 int raw_p, x, y;
9904 int width, height, max_color_idx = 0;
9905 XImage *ximg;
9906 Lisp_Object file, specified_file;
9907 enum {PBM_MONO, PBM_GRAY, PBM_COLOR} type;
9908 struct gcpro gcpro1;
9909 unsigned char *contents = NULL;
9910 unsigned char *end, *p;
9911 int size;
9912
9913 specified_file = image_spec_value (img->spec, QCfile, NULL);
9914 file = Qnil;
9915 GCPRO1 (file);
9916
9917 if (STRINGP (specified_file))
9918 {
9919 file = x_find_image_file (specified_file);
9920 if (!STRINGP (file))
9921 {
9922 image_error ("Cannot find image file `%s'", specified_file, Qnil);
9923 UNGCPRO;
9924 return 0;
9925 }
9926
9927 contents = pbm_read_file (file, &size);
9928 if (contents == NULL)
9929 {
9930 image_error ("Error reading `%s'", file, Qnil);
9931 UNGCPRO;
9932 return 0;
9933 }
9934
9935 p = contents;
9936 end = contents + size;
9937 }
9938 else
9939 {
9940 Lisp_Object data;
9941 data = image_spec_value (img->spec, QCdata, NULL);
9942 p = XSTRING (data)->data;
9943 end = p + STRING_BYTES (XSTRING (data));
9944 }
9945
9946 /* Check magic number. */
9947 if (end - p < 2 || *p++ != 'P')
9948 {
9949 image_error ("Not a PBM image: `%s'", img->spec, Qnil);
9950 error:
9951 xfree (contents);
9952 UNGCPRO;
9953 return 0;
9954 }
9955
9956 switch (*p++)
9957 {
9958 case '1':
9959 raw_p = 0, type = PBM_MONO;
9960 break;
9961
9962 case '2':
9963 raw_p = 0, type = PBM_GRAY;
9964 break;
9965
9966 case '3':
9967 raw_p = 0, type = PBM_COLOR;
9968 break;
9969
9970 case '4':
9971 raw_p = 1, type = PBM_MONO;
9972 break;
9973
9974 case '5':
9975 raw_p = 1, type = PBM_GRAY;
9976 break;
9977
9978 case '6':
9979 raw_p = 1, type = PBM_COLOR;
9980 break;
9981
9982 default:
9983 image_error ("Not a PBM image: `%s'", img->spec, Qnil);
9984 goto error;
9985 }
9986
9987 /* Read width, height, maximum color-component. Characters
9988 starting with `#' up to the end of a line are ignored. */
9989 width = pbm_scan_number (&p, end);
9990 height = pbm_scan_number (&p, end);
9991
9992 if (type != PBM_MONO)
9993 {
9994 max_color_idx = pbm_scan_number (&p, end);
9995 if (raw_p && max_color_idx > 255)
9996 max_color_idx = 255;
9997 }
9998
9999 if (width < 0
10000 || height < 0
10001 || (type != PBM_MONO && max_color_idx < 0))
10002 goto error;
10003
10004 BLOCK_INPUT;
10005 if (!x_create_x_image_and_pixmap (f, width, height, 0,
10006 &ximg, &img->pixmap))
10007 {
10008 UNBLOCK_INPUT;
10009 goto error;
10010 }
10011
10012 /* Initialize the color hash table. */
10013 init_color_table ();
10014
10015 if (type == PBM_MONO)
10016 {
10017 int c = 0, g;
10018
10019 for (y = 0; y < height; ++y)
10020 for (x = 0; x < width; ++x)
10021 {
10022 if (raw_p)
10023 {
10024 if ((x & 7) == 0)
10025 c = *p++;
10026 g = c & 0x80;
10027 c <<= 1;
10028 }
10029 else
10030 g = pbm_scan_number (&p, end);
10031
10032 XPutPixel (ximg, x, y, (g
10033 ? FRAME_FOREGROUND_PIXEL (f)
10034 : FRAME_BACKGROUND_PIXEL (f)));
10035 }
10036 }
10037 else
10038 {
10039 for (y = 0; y < height; ++y)
10040 for (x = 0; x < width; ++x)
10041 {
10042 int r, g, b;
10043
10044 if (type == PBM_GRAY)
10045 r = g = b = raw_p ? *p++ : pbm_scan_number (&p, end);
10046 else if (raw_p)
10047 {
10048 r = *p++;
10049 g = *p++;
10050 b = *p++;
10051 }
10052 else
10053 {
10054 r = pbm_scan_number (&p, end);
10055 g = pbm_scan_number (&p, end);
10056 b = pbm_scan_number (&p, end);
10057 }
10058
10059 if (r < 0 || g < 0 || b < 0)
10060 {
10061 xfree (ximg->data);
10062 ximg->data = NULL;
10063 XDestroyImage (ximg);
10064 UNBLOCK_INPUT;
10065 image_error ("Invalid pixel value in image `%s'",
10066 img->spec, Qnil);
10067 goto error;
10068 }
10069
10070 /* RGB values are now in the range 0..max_color_idx.
10071 Scale this to the range 0..0xffff supported by X. */
10072 r = (double) r * 65535 / max_color_idx;
10073 g = (double) g * 65535 / max_color_idx;
10074 b = (double) b * 65535 / max_color_idx;
10075 XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b));
10076 }
10077 }
10078
10079 /* Store in IMG->colors the colors allocated for the image, and
10080 free the color table. */
10081 img->colors = colors_in_color_table (&img->ncolors);
10082 free_color_table ();
10083
10084 /* Put the image into a pixmap. */
10085 x_put_x_image (f, ximg, img->pixmap, width, height);
10086 x_destroy_x_image (ximg);
10087 UNBLOCK_INPUT;
10088
10089 img->width = width;
10090 img->height = height;
10091
10092 UNGCPRO;
10093 xfree (contents);
10094 return 1;
10095 }
10096 #endif /* HAVE_PBM */
10097
10098 \f
10099 /***********************************************************************
10100 PNG
10101 ***********************************************************************/
10102
10103 #if HAVE_PNG
10104
10105 #include <png.h>
10106
10107 /* Function prototypes. */
10108
10109 static int png_image_p P_ ((Lisp_Object object));
10110 static int png_load P_ ((struct frame *f, struct image *img));
10111
10112 /* The symbol `png' identifying images of this type. */
10113
10114 Lisp_Object Qpng;
10115
10116 /* Indices of image specification fields in png_format, below. */
10117
10118 enum png_keyword_index
10119 {
10120 PNG_TYPE,
10121 PNG_DATA,
10122 PNG_FILE,
10123 PNG_ASCENT,
10124 PNG_MARGIN,
10125 PNG_RELIEF,
10126 PNG_ALGORITHM,
10127 PNG_HEURISTIC_MASK,
10128 PNG_LAST
10129 };
10130
10131 /* Vector of image_keyword structures describing the format
10132 of valid user-defined image specifications. */
10133
10134 static struct image_keyword png_format[PNG_LAST] =
10135 {
10136 {":type", IMAGE_SYMBOL_VALUE, 1},
10137 {":data", IMAGE_STRING_VALUE, 0},
10138 {":file", IMAGE_STRING_VALUE, 0},
10139 {":ascent", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
10140 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
10141 {":relief", IMAGE_INTEGER_VALUE, 0},
10142 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
10143 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
10144 };
10145
10146 /* Structure describing the image type `png'. */
10147
10148 static struct image_type png_type =
10149 {
10150 &Qpng,
10151 png_image_p,
10152 png_load,
10153 x_clear_image,
10154 NULL
10155 };
10156
10157
10158 /* Return non-zero if OBJECT is a valid PNG image specification. */
10159
10160 static int
10161 png_image_p (object)
10162 Lisp_Object object;
10163 {
10164 struct image_keyword fmt[PNG_LAST];
10165 bcopy (png_format, fmt, sizeof fmt);
10166
10167 if (!parse_image_spec (object, fmt, PNG_LAST, Qpng)
10168 || (fmt[PNG_ASCENT].count
10169 && XFASTINT (fmt[PNG_ASCENT].value) > 100))
10170 return 0;
10171
10172 /* Must specify either the :data or :file keyword. */
10173 return fmt[PNG_FILE].count + fmt[PNG_DATA].count == 1;
10174 }
10175
10176
10177 /* Error and warning handlers installed when the PNG library
10178 is initialized. */
10179
10180 static void
10181 my_png_error (png_ptr, msg)
10182 png_struct *png_ptr;
10183 char *msg;
10184 {
10185 xassert (png_ptr != NULL);
10186 image_error ("PNG error: %s", build_string (msg), Qnil);
10187 longjmp (png_ptr->jmpbuf, 1);
10188 }
10189
10190
10191 static void
10192 my_png_warning (png_ptr, msg)
10193 png_struct *png_ptr;
10194 char *msg;
10195 {
10196 xassert (png_ptr != NULL);
10197 image_error ("PNG warning: %s", build_string (msg), Qnil);
10198 }
10199
10200 /* Memory source for PNG decoding. */
10201
10202 struct png_memory_storage
10203 {
10204 unsigned char *bytes; /* The data */
10205 size_t len; /* How big is it? */
10206 int index; /* Where are we? */
10207 };
10208
10209
10210 /* Function set as reader function when reading PNG image from memory.
10211 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
10212 bytes from the input to DATA. */
10213
10214 static void
10215 png_read_from_memory (png_ptr, data, length)
10216 png_structp png_ptr;
10217 png_bytep data;
10218 png_size_t length;
10219 {
10220 struct png_memory_storage *tbr
10221 = (struct png_memory_storage *) png_get_io_ptr (png_ptr);
10222
10223 if (length > tbr->len - tbr->index)
10224 png_error (png_ptr, "Read error");
10225
10226 bcopy (tbr->bytes + tbr->index, data, length);
10227 tbr->index = tbr->index + length;
10228 }
10229
10230 /* Load PNG image IMG for use on frame F. Value is non-zero if
10231 successful. */
10232
10233 static int
10234 png_load (f, img)
10235 struct frame *f;
10236 struct image *img;
10237 {
10238 Lisp_Object file, specified_file;
10239 Lisp_Object specified_data;
10240 int x, y, i;
10241 XImage *ximg, *mask_img = NULL;
10242 struct gcpro gcpro1;
10243 png_struct *png_ptr = NULL;
10244 png_info *info_ptr = NULL, *end_info = NULL;
10245 FILE *fp = NULL;
10246 png_byte sig[8];
10247 png_byte *pixels = NULL;
10248 png_byte **rows = NULL;
10249 png_uint_32 width, height;
10250 int bit_depth, color_type, interlace_type;
10251 png_byte channels;
10252 png_uint_32 row_bytes;
10253 int transparent_p;
10254 char *gamma_str;
10255 double screen_gamma, image_gamma;
10256 int intent;
10257 struct png_memory_storage tbr; /* Data to be read */
10258
10259 /* Find out what file to load. */
10260 specified_file = image_spec_value (img->spec, QCfile, NULL);
10261 specified_data = image_spec_value (img->spec, QCdata, NULL);
10262 file = Qnil;
10263 GCPRO1 (file);
10264
10265 if (NILP (specified_data))
10266 {
10267 file = x_find_image_file (specified_file);
10268 if (!STRINGP (file))
10269 {
10270 image_error ("Cannot find image file `%s'", specified_file, Qnil);
10271 UNGCPRO;
10272 return 0;
10273 }
10274
10275 /* Open the image file. */
10276 fp = fopen (XSTRING (file)->data, "rb");
10277 if (!fp)
10278 {
10279 image_error ("Cannot open image file `%s'", file, Qnil);
10280 UNGCPRO;
10281 fclose (fp);
10282 return 0;
10283 }
10284
10285 /* Check PNG signature. */
10286 if (fread (sig, 1, sizeof sig, fp) != sizeof sig
10287 || !png_check_sig (sig, sizeof sig))
10288 {
10289 image_error ("Not a PNG file:` %s'", file, Qnil);
10290 UNGCPRO;
10291 fclose (fp);
10292 return 0;
10293 }
10294 }
10295 else
10296 {
10297 /* Read from memory. */
10298 tbr.bytes = XSTRING (specified_data)->data;
10299 tbr.len = STRING_BYTES (XSTRING (specified_data));
10300 tbr.index = 0;
10301
10302 /* Check PNG signature. */
10303 if (tbr.len < sizeof sig
10304 || !png_check_sig (tbr.bytes, sizeof sig))
10305 {
10306 image_error ("Not a PNG image: `%s'", img->spec, Qnil);
10307 UNGCPRO;
10308 return 0;
10309 }
10310
10311 /* Need to skip past the signature. */
10312 tbr.bytes += sizeof (sig);
10313 }
10314
10315 /* Initialize read and info structs for PNG lib. */
10316 png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL,
10317 my_png_error, my_png_warning);
10318 if (!png_ptr)
10319 {
10320 if (fp) fclose (fp);
10321 UNGCPRO;
10322 return 0;
10323 }
10324
10325 info_ptr = png_create_info_struct (png_ptr);
10326 if (!info_ptr)
10327 {
10328 png_destroy_read_struct (&png_ptr, NULL, NULL);
10329 if (fp) fclose (fp);
10330 UNGCPRO;
10331 return 0;
10332 }
10333
10334 end_info = png_create_info_struct (png_ptr);
10335 if (!end_info)
10336 {
10337 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
10338 if (fp) fclose (fp);
10339 UNGCPRO;
10340 return 0;
10341 }
10342
10343 /* Set error jump-back. We come back here when the PNG library
10344 detects an error. */
10345 if (setjmp (png_ptr->jmpbuf))
10346 {
10347 error:
10348 if (png_ptr)
10349 png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
10350 xfree (pixels);
10351 xfree (rows);
10352 if (fp) fclose (fp);
10353 UNGCPRO;
10354 return 0;
10355 }
10356
10357 /* Read image info. */
10358 if (!NILP (specified_data))
10359 png_set_read_fn (png_ptr, (void *) &tbr, png_read_from_memory);
10360 else
10361 png_init_io (png_ptr, fp);
10362
10363 png_set_sig_bytes (png_ptr, sizeof sig);
10364 png_read_info (png_ptr, info_ptr);
10365 png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
10366 &interlace_type, NULL, NULL);
10367
10368 /* If image contains simply transparency data, we prefer to
10369 construct a clipping mask. */
10370 if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
10371 transparent_p = 1;
10372 else
10373 transparent_p = 0;
10374
10375 /* This function is easier to write if we only have to handle
10376 one data format: RGB or RGBA with 8 bits per channel. Let's
10377 transform other formats into that format. */
10378
10379 /* Strip more than 8 bits per channel. */
10380 if (bit_depth == 16)
10381 png_set_strip_16 (png_ptr);
10382
10383 /* Expand data to 24 bit RGB, or 8 bit grayscale, with alpha channel
10384 if available. */
10385 png_set_expand (png_ptr);
10386
10387 /* Convert grayscale images to RGB. */
10388 if (color_type == PNG_COLOR_TYPE_GRAY
10389 || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
10390 png_set_gray_to_rgb (png_ptr);
10391
10392 /* The value 2.2 is a guess for PC monitors from PNG example.c. */
10393 gamma_str = getenv ("SCREEN_GAMMA");
10394 screen_gamma = gamma_str ? atof (gamma_str) : 2.2;
10395
10396 /* Tell the PNG lib to handle gamma correction for us. */
10397
10398 #if defined(PNG_READ_sRGB_SUPPORTED) || defined(PNG_WRITE_sRGB_SUPPORTED)
10399 if (png_get_sRGB (png_ptr, info_ptr, &intent))
10400 /* There is a special chunk in the image specifying the gamma. */
10401 png_set_sRGB (png_ptr, info_ptr, intent);
10402 else
10403 #endif
10404 if (png_get_gAMA (png_ptr, info_ptr, &image_gamma))
10405 /* Image contains gamma information. */
10406 png_set_gamma (png_ptr, screen_gamma, image_gamma);
10407 else
10408 /* Use a default of 0.5 for the image gamma. */
10409 png_set_gamma (png_ptr, screen_gamma, 0.5);
10410
10411 /* Handle alpha channel by combining the image with a background
10412 color. Do this only if a real alpha channel is supplied. For
10413 simple transparency, we prefer a clipping mask. */
10414 if (!transparent_p)
10415 {
10416 png_color_16 *image_background;
10417
10418 if (png_get_bKGD (png_ptr, info_ptr, &image_background))
10419 /* Image contains a background color with which to
10420 combine the image. */
10421 png_set_background (png_ptr, image_background,
10422 PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
10423 else
10424 {
10425 /* Image does not contain a background color with which
10426 to combine the image data via an alpha channel. Use
10427 the frame's background instead. */
10428 XColor color;
10429 Colormap cmap;
10430 png_color_16 frame_background;
10431
10432 BLOCK_INPUT;
10433 cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f));
10434 color.pixel = FRAME_BACKGROUND_PIXEL (f);
10435 XQueryColor (FRAME_W32_DISPLAY (f), cmap, &color);
10436 UNBLOCK_INPUT;
10437
10438 bzero (&frame_background, sizeof frame_background);
10439 frame_background.red = color.red;
10440 frame_background.green = color.green;
10441 frame_background.blue = color.blue;
10442
10443 png_set_background (png_ptr, &frame_background,
10444 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
10445 }
10446 }
10447
10448 /* Update info structure. */
10449 png_read_update_info (png_ptr, info_ptr);
10450
10451 /* Get number of channels. Valid values are 1 for grayscale images
10452 and images with a palette, 2 for grayscale images with transparency
10453 information (alpha channel), 3 for RGB images, and 4 for RGB
10454 images with alpha channel, i.e. RGBA. If conversions above were
10455 sufficient we should only have 3 or 4 channels here. */
10456 channels = png_get_channels (png_ptr, info_ptr);
10457 xassert (channels == 3 || channels == 4);
10458
10459 /* Number of bytes needed for one row of the image. */
10460 row_bytes = png_get_rowbytes (png_ptr, info_ptr);
10461
10462 /* Allocate memory for the image. */
10463 pixels = (png_byte *) xmalloc (row_bytes * height * sizeof *pixels);
10464 rows = (png_byte **) xmalloc (height * sizeof *rows);
10465 for (i = 0; i < height; ++i)
10466 rows[i] = pixels + i * row_bytes;
10467
10468 /* Read the entire image. */
10469 png_read_image (png_ptr, rows);
10470 png_read_end (png_ptr, info_ptr);
10471 if (fp)
10472 {
10473 fclose (fp);
10474 fp = NULL;
10475 }
10476
10477 BLOCK_INPUT;
10478
10479 /* Create the X image and pixmap. */
10480 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg,
10481 &img->pixmap))
10482 {
10483 UNBLOCK_INPUT;
10484 goto error;
10485 }
10486
10487 /* Create an image and pixmap serving as mask if the PNG image
10488 contains an alpha channel. */
10489 if (channels == 4
10490 && !transparent_p
10491 && !x_create_x_image_and_pixmap (f, width, height, 1,
10492 &mask_img, &img->mask))
10493 {
10494 x_destroy_x_image (ximg);
10495 XFreePixmap (FRAME_W32_DISPLAY (f), img->pixmap);
10496 img->pixmap = 0;
10497 UNBLOCK_INPUT;
10498 goto error;
10499 }
10500
10501 /* Fill the X image and mask from PNG data. */
10502 init_color_table ();
10503
10504 for (y = 0; y < height; ++y)
10505 {
10506 png_byte *p = rows[y];
10507
10508 for (x = 0; x < width; ++x)
10509 {
10510 unsigned r, g, b;
10511
10512 r = *p++ << 8;
10513 g = *p++ << 8;
10514 b = *p++ << 8;
10515 XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b));
10516
10517 /* An alpha channel, aka mask channel, associates variable
10518 transparency with an image. Where other image formats
10519 support binary transparency---fully transparent or fully
10520 opaque---PNG allows up to 254 levels of partial transparency.
10521 The PNG library implements partial transparency by combining
10522 the image with a specified background color.
10523
10524 I'm not sure how to handle this here nicely: because the
10525 background on which the image is displayed may change, for
10526 real alpha channel support, it would be necessary to create
10527 a new image for each possible background.
10528
10529 What I'm doing now is that a mask is created if we have
10530 boolean transparency information. Otherwise I'm using
10531 the frame's background color to combine the image with. */
10532
10533 if (channels == 4)
10534 {
10535 if (mask_img)
10536 XPutPixel (mask_img, x, y, *p > 0);
10537 ++p;
10538 }
10539 }
10540 }
10541
10542 /* Remember colors allocated for this image. */
10543 img->colors = colors_in_color_table (&img->ncolors);
10544 free_color_table ();
10545
10546 /* Clean up. */
10547 png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
10548 xfree (rows);
10549 xfree (pixels);
10550
10551 img->width = width;
10552 img->height = height;
10553
10554 /* Put the image into the pixmap, then free the X image and its buffer. */
10555 x_put_x_image (f, ximg, img->pixmap, width, height);
10556 x_destroy_x_image (ximg);
10557
10558 /* Same for the mask. */
10559 if (mask_img)
10560 {
10561 x_put_x_image (f, mask_img, img->mask, img->width, img->height);
10562 x_destroy_x_image (mask_img);
10563 }
10564
10565 UNBLOCK_INPUT;
10566 UNGCPRO;
10567 return 1;
10568 }
10569
10570 #endif /* HAVE_PNG != 0 */
10571
10572
10573 \f
10574 /***********************************************************************
10575 JPEG
10576 ***********************************************************************/
10577
10578 #if HAVE_JPEG
10579
10580 /* Work around a warning about HAVE_STDLIB_H being redefined in
10581 jconfig.h. */
10582 #ifdef HAVE_STDLIB_H
10583 #define HAVE_STDLIB_H_1
10584 #undef HAVE_STDLIB_H
10585 #endif /* HAVE_STLIB_H */
10586
10587 #include <jpeglib.h>
10588 #include <jerror.h>
10589 #include <setjmp.h>
10590
10591 #ifdef HAVE_STLIB_H_1
10592 #define HAVE_STDLIB_H 1
10593 #endif
10594
10595 static int jpeg_image_p P_ ((Lisp_Object object));
10596 static int jpeg_load P_ ((struct frame *f, struct image *img));
10597
10598 /* The symbol `jpeg' identifying images of this type. */
10599
10600 Lisp_Object Qjpeg;
10601
10602 /* Indices of image specification fields in gs_format, below. */
10603
10604 enum jpeg_keyword_index
10605 {
10606 JPEG_TYPE,
10607 JPEG_DATA,
10608 JPEG_FILE,
10609 JPEG_ASCENT,
10610 JPEG_MARGIN,
10611 JPEG_RELIEF,
10612 JPEG_ALGORITHM,
10613 JPEG_HEURISTIC_MASK,
10614 JPEG_LAST
10615 };
10616
10617 /* Vector of image_keyword structures describing the format
10618 of valid user-defined image specifications. */
10619
10620 static struct image_keyword jpeg_format[JPEG_LAST] =
10621 {
10622 {":type", IMAGE_SYMBOL_VALUE, 1},
10623 {":data", IMAGE_STRING_VALUE, 0},
10624 {":file", IMAGE_STRING_VALUE, 0},
10625 {":ascent", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
10626 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
10627 {":relief", IMAGE_INTEGER_VALUE, 0},
10628 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
10629 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
10630 };
10631
10632 /* Structure describing the image type `jpeg'. */
10633
10634 static struct image_type jpeg_type =
10635 {
10636 &Qjpeg,
10637 jpeg_image_p,
10638 jpeg_load,
10639 x_clear_image,
10640 NULL
10641 };
10642
10643
10644 /* Return non-zero if OBJECT is a valid JPEG image specification. */
10645
10646 static int
10647 jpeg_image_p (object)
10648 Lisp_Object object;
10649 {
10650 struct image_keyword fmt[JPEG_LAST];
10651
10652 bcopy (jpeg_format, fmt, sizeof fmt);
10653
10654 if (!parse_image_spec (object, fmt, JPEG_LAST, Qjpeg)
10655 || (fmt[JPEG_ASCENT].count
10656 && XFASTINT (fmt[JPEG_ASCENT].value) > 100))
10657 return 0;
10658
10659 /* Must specify either the :data or :file keyword. */
10660 return fmt[JPEG_FILE].count + fmt[JPEG_DATA].count == 1;
10661 }
10662
10663
10664 struct my_jpeg_error_mgr
10665 {
10666 struct jpeg_error_mgr pub;
10667 jmp_buf setjmp_buffer;
10668 };
10669
10670 static void
10671 my_error_exit (cinfo)
10672 j_common_ptr cinfo;
10673 {
10674 struct my_jpeg_error_mgr *mgr = (struct my_jpeg_error_mgr *) cinfo->err;
10675 longjmp (mgr->setjmp_buffer, 1);
10676 }
10677
10678 /* Init source method for JPEG data source manager. Called by
10679 jpeg_read_header() before any data is actually read. See
10680 libjpeg.doc from the JPEG lib distribution. */
10681
10682 static void
10683 our_init_source (cinfo)
10684 j_decompress_ptr cinfo;
10685 {
10686 }
10687
10688
10689 /* Fill input buffer method for JPEG data source manager. Called
10690 whenever more data is needed. We read the whole image in one step,
10691 so this only adds a fake end of input marker at the end. */
10692
10693 static boolean
10694 our_fill_input_buffer (cinfo)
10695 j_decompress_ptr cinfo;
10696 {
10697 /* Insert a fake EOI marker. */
10698 struct jpeg_source_mgr *src = cinfo->src;
10699 static JOCTET buffer[2];
10700
10701 buffer[0] = (JOCTET) 0xFF;
10702 buffer[1] = (JOCTET) JPEG_EOI;
10703
10704 src->next_input_byte = buffer;
10705 src->bytes_in_buffer = 2;
10706 return TRUE;
10707 }
10708
10709
10710 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
10711 is the JPEG data source manager. */
10712
10713 static void
10714 our_skip_input_data (cinfo, num_bytes)
10715 j_decompress_ptr cinfo;
10716 long num_bytes;
10717 {
10718 struct jpeg_source_mgr *src = (struct jpeg_source_mgr *) cinfo->src;
10719
10720 if (src)
10721 {
10722 if (num_bytes > src->bytes_in_buffer)
10723 ERREXIT (cinfo, JERR_INPUT_EOF);
10724
10725 src->bytes_in_buffer -= num_bytes;
10726 src->next_input_byte += num_bytes;
10727 }
10728 }
10729
10730
10731 /* Method to terminate data source. Called by
10732 jpeg_finish_decompress() after all data has been processed. */
10733
10734 static void
10735 our_term_source (cinfo)
10736 j_decompress_ptr cinfo;
10737 {
10738 }
10739
10740
10741 /* Set up the JPEG lib for reading an image from DATA which contains
10742 LEN bytes. CINFO is the decompression info structure created for
10743 reading the image. */
10744
10745 static void
10746 jpeg_memory_src (cinfo, data, len)
10747 j_decompress_ptr cinfo;
10748 JOCTET *data;
10749 unsigned int len;
10750 {
10751 struct jpeg_source_mgr *src;
10752
10753 if (cinfo->src == NULL)
10754 {
10755 /* First time for this JPEG object? */
10756 cinfo->src = (struct jpeg_source_mgr *)
10757 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
10758 sizeof (struct jpeg_source_mgr));
10759 src = (struct jpeg_source_mgr *) cinfo->src;
10760 src->next_input_byte = data;
10761 }
10762
10763 src = (struct jpeg_source_mgr *) cinfo->src;
10764 src->init_source = our_init_source;
10765 src->fill_input_buffer = our_fill_input_buffer;
10766 src->skip_input_data = our_skip_input_data;
10767 src->resync_to_restart = jpeg_resync_to_restart; /* Use default method. */
10768 src->term_source = our_term_source;
10769 src->bytes_in_buffer = len;
10770 src->next_input_byte = data;
10771 }
10772
10773
10774 /* Load image IMG for use on frame F. Patterned after example.c
10775 from the JPEG lib. */
10776
10777 static int
10778 jpeg_load (f, img)
10779 struct frame *f;
10780 struct image *img;
10781 {
10782 struct jpeg_decompress_struct cinfo;
10783 struct my_jpeg_error_mgr mgr;
10784 Lisp_Object file, specified_file;
10785 Lisp_Object specified_data;
10786 FILE *fp = NULL;
10787 JSAMPARRAY buffer;
10788 int row_stride, x, y;
10789 XImage *ximg = NULL;
10790 int rc;
10791 unsigned long *colors;
10792 int width, height;
10793 struct gcpro gcpro1;
10794
10795 /* Open the JPEG file. */
10796 specified_file = image_spec_value (img->spec, QCfile, NULL);
10797 specified_data = image_spec_value (img->spec, QCdata, NULL);
10798 file = Qnil;
10799 GCPRO1 (file);
10800
10801 if (NILP (specified_data))
10802 {
10803 file = x_find_image_file (specified_file);
10804 if (!STRINGP (file))
10805 {
10806 image_error ("Cannot find image file `%s'", specified_file, Qnil);
10807 UNGCPRO;
10808 return 0;
10809 }
10810
10811 fp = fopen (XSTRING (file)->data, "r");
10812 if (fp == NULL)
10813 {
10814 image_error ("Cannot open `%s'", file, Qnil);
10815 UNGCPRO;
10816 return 0;
10817 }
10818 }
10819
10820 /* Customize libjpeg's error handling to call my_error_exit when an
10821 error is detected. This function will perform a longjmp. */
10822 mgr.pub.error_exit = my_error_exit;
10823 cinfo.err = jpeg_std_error (&mgr.pub);
10824
10825 if ((rc = setjmp (mgr.setjmp_buffer)) != 0)
10826 {
10827 if (rc == 1)
10828 {
10829 /* Called from my_error_exit. Display a JPEG error. */
10830 char buffer[JMSG_LENGTH_MAX];
10831 cinfo.err->format_message ((j_common_ptr) &cinfo, buffer);
10832 image_error ("Error reading JPEG image `%s': %s", img->spec,
10833 build_string (buffer));
10834 }
10835
10836 /* Close the input file and destroy the JPEG object. */
10837 if (fp)
10838 fclose (fp);
10839 jpeg_destroy_decompress (&cinfo);
10840
10841 BLOCK_INPUT;
10842
10843 /* If we already have an XImage, free that. */
10844 x_destroy_x_image (ximg);
10845
10846 /* Free pixmap and colors. */
10847 x_clear_image (f, img);
10848
10849 UNBLOCK_INPUT;
10850 UNGCPRO;
10851 return 0;
10852 }
10853
10854 /* Create the JPEG decompression object. Let it read from fp.
10855 Read the JPEG image header. */
10856 jpeg_create_decompress (&cinfo);
10857
10858 if (NILP (specified_data))
10859 jpeg_stdio_src (&cinfo, fp);
10860 else
10861 jpeg_memory_src (&cinfo, XSTRING (specified_data)->data,
10862 STRING_BYTES (XSTRING (specified_data)));
10863
10864 jpeg_read_header (&cinfo, TRUE);
10865
10866 /* Customize decompression so that color quantization will be used.
10867 Start decompression. */
10868 cinfo.quantize_colors = TRUE;
10869 jpeg_start_decompress (&cinfo);
10870 width = img->width = cinfo.output_width;
10871 height = img->height = cinfo.output_height;
10872
10873 BLOCK_INPUT;
10874
10875 /* Create X image and pixmap. */
10876 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg,
10877 &img->pixmap))
10878 {
10879 UNBLOCK_INPUT;
10880 longjmp (mgr.setjmp_buffer, 2);
10881 }
10882
10883 /* Allocate colors. When color quantization is used,
10884 cinfo.actual_number_of_colors has been set with the number of
10885 colors generated, and cinfo.colormap is a two-dimensional array
10886 of color indices in the range 0..cinfo.actual_number_of_colors.
10887 No more than 255 colors will be generated. */
10888 {
10889 int i, ir, ig, ib;
10890
10891 if (cinfo.out_color_components > 2)
10892 ir = 0, ig = 1, ib = 2;
10893 else if (cinfo.out_color_components > 1)
10894 ir = 0, ig = 1, ib = 0;
10895 else
10896 ir = 0, ig = 0, ib = 0;
10897
10898 /* Use the color table mechanism because it handles colors that
10899 cannot be allocated nicely. Such colors will be replaced with
10900 a default color, and we don't have to care about which colors
10901 can be freed safely, and which can't. */
10902 init_color_table ();
10903 colors = (unsigned long *) alloca (cinfo.actual_number_of_colors
10904 * sizeof *colors);
10905
10906 for (i = 0; i < cinfo.actual_number_of_colors; ++i)
10907 {
10908 /* Multiply RGB values with 255 because X expects RGB values
10909 in the range 0..0xffff. */
10910 int r = cinfo.colormap[ir][i] << 8;
10911 int g = cinfo.colormap[ig][i] << 8;
10912 int b = cinfo.colormap[ib][i] << 8;
10913 colors[i] = lookup_rgb_color (f, r, g, b);
10914 }
10915
10916 /* Remember those colors actually allocated. */
10917 img->colors = colors_in_color_table (&img->ncolors);
10918 free_color_table ();
10919 }
10920
10921 /* Read pixels. */
10922 row_stride = width * cinfo.output_components;
10923 buffer = cinfo.mem->alloc_sarray ((j_common_ptr) &cinfo, JPOOL_IMAGE,
10924 row_stride, 1);
10925 for (y = 0; y < height; ++y)
10926 {
10927 jpeg_read_scanlines (&cinfo, buffer, 1);
10928 for (x = 0; x < cinfo.output_width; ++x)
10929 XPutPixel (ximg, x, y, colors[buffer[0][x]]);
10930 }
10931
10932 /* Clean up. */
10933 jpeg_finish_decompress (&cinfo);
10934 jpeg_destroy_decompress (&cinfo);
10935 if (fp)
10936 fclose (fp);
10937
10938 /* Put the image into the pixmap. */
10939 x_put_x_image (f, ximg, img->pixmap, width, height);
10940 x_destroy_x_image (ximg);
10941 UNBLOCK_INPUT;
10942 UNGCPRO;
10943 return 1;
10944 }
10945
10946 #endif /* HAVE_JPEG */
10947
10948
10949 \f
10950 /***********************************************************************
10951 TIFF
10952 ***********************************************************************/
10953
10954 #if HAVE_TIFF
10955
10956 #include <tiffio.h>
10957
10958 static int tiff_image_p P_ ((Lisp_Object object));
10959 static int tiff_load P_ ((struct frame *f, struct image *img));
10960
10961 /* The symbol `tiff' identifying images of this type. */
10962
10963 Lisp_Object Qtiff;
10964
10965 /* Indices of image specification fields in tiff_format, below. */
10966
10967 enum tiff_keyword_index
10968 {
10969 TIFF_TYPE,
10970 TIFF_DATA,
10971 TIFF_FILE,
10972 TIFF_ASCENT,
10973 TIFF_MARGIN,
10974 TIFF_RELIEF,
10975 TIFF_ALGORITHM,
10976 TIFF_HEURISTIC_MASK,
10977 TIFF_LAST
10978 };
10979
10980 /* Vector of image_keyword structures describing the format
10981 of valid user-defined image specifications. */
10982
10983 static struct image_keyword tiff_format[TIFF_LAST] =
10984 {
10985 {":type", IMAGE_SYMBOL_VALUE, 1},
10986 {":data", IMAGE_STRING_VALUE, 0},
10987 {":file", IMAGE_STRING_VALUE, 0},
10988 {":ascent", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
10989 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
10990 {":relief", IMAGE_INTEGER_VALUE, 0},
10991 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
10992 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
10993 };
10994
10995 /* Structure describing the image type `tiff'. */
10996
10997 static struct image_type tiff_type =
10998 {
10999 &Qtiff,
11000 tiff_image_p,
11001 tiff_load,
11002 x_clear_image,
11003 NULL
11004 };
11005
11006
11007 /* Return non-zero if OBJECT is a valid TIFF image specification. */
11008
11009 static int
11010 tiff_image_p (object)
11011 Lisp_Object object;
11012 {
11013 struct image_keyword fmt[TIFF_LAST];
11014 bcopy (tiff_format, fmt, sizeof fmt);
11015
11016 if (!parse_image_spec (object, fmt, TIFF_LAST, Qtiff)
11017 || (fmt[TIFF_ASCENT].count
11018 && XFASTINT (fmt[TIFF_ASCENT].value) > 100))
11019 return 0;
11020
11021 /* Must specify either the :data or :file keyword. */
11022 return fmt[TIFF_FILE].count + fmt[TIFF_DATA].count == 1;
11023 }
11024
11025
11026 /* Reading from a memory buffer for TIFF images Based on the PNG
11027 memory source, but we have to provide a lot of extra functions.
11028 Blah.
11029
11030 We really only need to implement read and seek, but I am not
11031 convinced that the TIFF library is smart enough not to destroy
11032 itself if we only hand it the function pointers we need to
11033 override. */
11034
11035 typedef struct
11036 {
11037 unsigned char *bytes;
11038 size_t len;
11039 int index;
11040 }
11041 tiff_memory_source;
11042
11043 static size_t
11044 tiff_read_from_memory (data, buf, size)
11045 thandle_t data;
11046 tdata_t buf;
11047 tsize_t size;
11048 {
11049 tiff_memory_source *src = (tiff_memory_source *) data;
11050
11051 if (size > src->len - src->index)
11052 return (size_t) -1;
11053 bcopy (src->bytes + src->index, buf, size);
11054 src->index += size;
11055 return size;
11056 }
11057
11058 static size_t
11059 tiff_write_from_memory (data, buf, size)
11060 thandle_t data;
11061 tdata_t buf;
11062 tsize_t size;
11063 {
11064 return (size_t) -1;
11065 }
11066
11067 static toff_t
11068 tiff_seek_in_memory (data, off, whence)
11069 thandle_t data;
11070 toff_t off;
11071 int whence;
11072 {
11073 tiff_memory_source *src = (tiff_memory_source *) data;
11074 int idx;
11075
11076 switch (whence)
11077 {
11078 case SEEK_SET: /* Go from beginning of source. */
11079 idx = off;
11080 break;
11081
11082 case SEEK_END: /* Go from end of source. */
11083 idx = src->len + off;
11084 break;
11085
11086 case SEEK_CUR: /* Go from current position. */
11087 idx = src->index + off;
11088 break;
11089
11090 default: /* Invalid `whence'. */
11091 return -1;
11092 }
11093
11094 if (idx > src->len || idx < 0)
11095 return -1;
11096
11097 src->index = idx;
11098 return src->index;
11099 }
11100
11101 static int
11102 tiff_close_memory (data)
11103 thandle_t data;
11104 {
11105 /* NOOP */
11106 return 0;
11107 }
11108
11109 static int
11110 tiff_mmap_memory (data, pbase, psize)
11111 thandle_t data;
11112 tdata_t *pbase;
11113 toff_t *psize;
11114 {
11115 /* It is already _IN_ memory. */
11116 return 0;
11117 }
11118
11119 static void
11120 tiff_unmap_memory (data, base, size)
11121 thandle_t data;
11122 tdata_t base;
11123 toff_t size;
11124 {
11125 /* We don't need to do this. */
11126 }
11127
11128 static toff_t
11129 tiff_size_of_memory (data)
11130 thandle_t data;
11131 {
11132 return ((tiff_memory_source *) data)->len;
11133 }
11134
11135 /* Load TIFF image IMG for use on frame F. Value is non-zero if
11136 successful. */
11137
11138 static int
11139 tiff_load (f, img)
11140 struct frame *f;
11141 struct image *img;
11142 {
11143 Lisp_Object file, specified_file;
11144 Lisp_Object specified_data;
11145 TIFF *tiff;
11146 int width, height, x, y;
11147 uint32 *buf;
11148 int rc;
11149 XImage *ximg;
11150 struct gcpro gcpro1;
11151 tiff_memory_source memsrc;
11152
11153 specified_file = image_spec_value (img->spec, QCfile, NULL);
11154 specified_data = image_spec_value (img->spec, QCdata, NULL);
11155 file = Qnil;
11156 GCPRO1 (file);
11157
11158 if (NILP (specified_data))
11159 {
11160 /* Read from a file */
11161 file = x_find_image_file (specified_file);
11162 if (!STRINGP (file))
11163 {
11164 image_error ("Cannot find image file `%s'", file, Qnil);
11165 UNGCPRO;
11166 return 0;
11167 }
11168
11169 /* Try to open the image file. */
11170 tiff = TIFFOpen (XSTRING (file)->data, "r");
11171 if (tiff == NULL)
11172 {
11173 image_error ("Cannot open `%s'", file, Qnil);
11174 UNGCPRO;
11175 return 0;
11176 }
11177 }
11178 else
11179 {
11180 /* Memory source! */
11181 memsrc.bytes = XSTRING (specified_data)->data;
11182 memsrc.len = STRING_BYTES (XSTRING (specified_data));
11183 memsrc.index = 0;
11184
11185 tiff = TIFFClientOpen ("memory_source", "r", &memsrc,
11186 (TIFFReadWriteProc) tiff_read_from_memory,
11187 (TIFFReadWriteProc) tiff_write_from_memory,
11188 tiff_seek_in_memory,
11189 tiff_close_memory,
11190 tiff_size_of_memory,
11191 tiff_mmap_memory,
11192 tiff_unmap_memory);
11193
11194 if (!tiff)
11195 {
11196 image_error ("Cannot open memory source for `%s'", img->spec, Qnil);
11197 UNGCPRO;
11198 return 0;
11199 }
11200 }
11201
11202 /* Get width and height of the image, and allocate a raster buffer
11203 of width x height 32-bit values. */
11204 TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &width);
11205 TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &height);
11206 buf = (uint32 *) xmalloc (width * height * sizeof *buf);
11207
11208 rc = TIFFReadRGBAImage (tiff, width, height, buf, 0);
11209 TIFFClose (tiff);
11210 if (!rc)
11211 {
11212 image_error ("Error reading TIFF image `%s'", img->spec, Qnil);
11213 xfree (buf);
11214 UNGCPRO;
11215 return 0;
11216 }
11217
11218 BLOCK_INPUT;
11219
11220 /* Create the X image and pixmap. */
11221 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
11222 {
11223 UNBLOCK_INPUT;
11224 xfree (buf);
11225 UNGCPRO;
11226 return 0;
11227 }
11228
11229 /* Initialize the color table. */
11230 init_color_table ();
11231
11232 /* Process the pixel raster. Origin is in the lower-left corner. */
11233 for (y = 0; y < height; ++y)
11234 {
11235 uint32 *row = buf + y * width;
11236
11237 for (x = 0; x < width; ++x)
11238 {
11239 uint32 abgr = row[x];
11240 int r = TIFFGetR (abgr) << 8;
11241 int g = TIFFGetG (abgr) << 8;
11242 int b = TIFFGetB (abgr) << 8;
11243 XPutPixel (ximg, x, height - 1 - y, lookup_rgb_color (f, r, g, b));
11244 }
11245 }
11246
11247 /* Remember the colors allocated for the image. Free the color table. */
11248 img->colors = colors_in_color_table (&img->ncolors);
11249 free_color_table ();
11250
11251 /* Put the image into the pixmap, then free the X image and its buffer. */
11252 x_put_x_image (f, ximg, img->pixmap, width, height);
11253 x_destroy_x_image (ximg);
11254 xfree (buf);
11255 UNBLOCK_INPUT;
11256
11257 img->width = width;
11258 img->height = height;
11259
11260 UNGCPRO;
11261 return 1;
11262 }
11263
11264 #endif /* HAVE_TIFF != 0 */
11265
11266
11267 \f
11268 /***********************************************************************
11269 GIF
11270 ***********************************************************************/
11271
11272 #if HAVE_GIF
11273
11274 #include <gif_lib.h>
11275
11276 static int gif_image_p P_ ((Lisp_Object object));
11277 static int gif_load P_ ((struct frame *f, struct image *img));
11278
11279 /* The symbol `gif' identifying images of this type. */
11280
11281 Lisp_Object Qgif;
11282
11283 /* Indices of image specification fields in gif_format, below. */
11284
11285 enum gif_keyword_index
11286 {
11287 GIF_TYPE,
11288 GIF_DATA,
11289 GIF_FILE,
11290 GIF_ASCENT,
11291 GIF_MARGIN,
11292 GIF_RELIEF,
11293 GIF_ALGORITHM,
11294 GIF_HEURISTIC_MASK,
11295 GIF_IMAGE,
11296 GIF_LAST
11297 };
11298
11299 /* Vector of image_keyword structures describing the format
11300 of valid user-defined image specifications. */
11301
11302 static struct image_keyword gif_format[GIF_LAST] =
11303 {
11304 {":type", IMAGE_SYMBOL_VALUE, 1},
11305 {":data", IMAGE_STRING_VALUE, 0},
11306 {":file", IMAGE_STRING_VALUE, 0},
11307 {":ascent", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
11308 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
11309 {":relief", IMAGE_INTEGER_VALUE, 0},
11310 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
11311 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
11312 {":image", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0}
11313 };
11314
11315 /* Structure describing the image type `gif'. */
11316
11317 static struct image_type gif_type =
11318 {
11319 &Qgif,
11320 gif_image_p,
11321 gif_load,
11322 x_clear_image,
11323 NULL
11324 };
11325
11326 /* Return non-zero if OBJECT is a valid GIF image specification. */
11327
11328 static int
11329 gif_image_p (object)
11330 Lisp_Object object;
11331 {
11332 struct image_keyword fmt[GIF_LAST];
11333 bcopy (gif_format, fmt, sizeof fmt);
11334
11335 if (!parse_image_spec (object, fmt, GIF_LAST, Qgif)
11336 || (fmt[GIF_ASCENT].count
11337 && XFASTINT (fmt[GIF_ASCENT].value) > 100))
11338 return 0;
11339
11340 /* Must specify either the :data or :file keyword. */
11341 return fmt[GIF_FILE].count + fmt[GIF_DATA].count == 1;
11342 }
11343
11344 /* Reading a GIF image from memory
11345 Based on the PNG memory stuff to a certain extent. */
11346
11347 typedef struct
11348 {
11349 unsigned char *bytes;
11350 size_t len;
11351 int index;
11352 }
11353 gif_memory_source;
11354
11355 /* Make the current memory source available to gif_read_from_memory.
11356 It's done this way because not all versions of libungif support
11357 a UserData field in the GifFileType structure. */
11358 static gif_memory_source *current_gif_memory_src;
11359
11360 static int
11361 gif_read_from_memory (file, buf, len)
11362 GifFileType *file;
11363 GifByteType *buf;
11364 int len;
11365 {
11366 gif_memory_source *src = current_gif_memory_src;
11367
11368 if (len > src->len - src->index)
11369 return -1;
11370
11371 bcopy (src->bytes + src->index, buf, len);
11372 src->index += len;
11373 return len;
11374 }
11375
11376
11377 /* Load GIF image IMG for use on frame F. Value is non-zero if
11378 successful. */
11379
11380 static int
11381 gif_load (f, img)
11382 struct frame *f;
11383 struct image *img;
11384 {
11385 Lisp_Object file, specified_file;
11386 Lisp_Object specified_data;
11387 int rc, width, height, x, y, i;
11388 XImage *ximg;
11389 ColorMapObject *gif_color_map;
11390 unsigned long pixel_colors[256];
11391 GifFileType *gif;
11392 struct gcpro gcpro1;
11393 Lisp_Object image;
11394 int ino, image_left, image_top, image_width, image_height;
11395 gif_memory_source memsrc;
11396 unsigned char *raster;
11397
11398 specified_file = image_spec_value (img->spec, QCfile, NULL);
11399 specified_data = image_spec_value (img->spec, QCdata, NULL);
11400 file = Qnil;
11401 GCPRO1 (file);
11402
11403 if (NILP (specified_data))
11404 {
11405 file = x_find_image_file (specified_file);
11406 if (!STRINGP (file))
11407 {
11408 image_error ("Cannot find image file `%s'", specified_file, Qnil);
11409 UNGCPRO;
11410 return 0;
11411 }
11412
11413 /* Open the GIF file. */
11414 gif = DGifOpenFileName (XSTRING (file)->data);
11415 if (gif == NULL)
11416 {
11417 image_error ("Cannot open `%s'", file, Qnil);
11418 UNGCPRO;
11419 return 0;
11420 }
11421 }
11422 else
11423 {
11424 /* Read from memory! */
11425 current_gif_memory_src = &memsrc;
11426 memsrc.bytes = XSTRING (specified_data)->data;
11427 memsrc.len = STRING_BYTES (XSTRING (specified_data));
11428 memsrc.index = 0;
11429
11430 gif = DGifOpen(&memsrc, gif_read_from_memory);
11431 if (!gif)
11432 {
11433 image_error ("Cannot open memory source `%s'", img->spec, Qnil);
11434 UNGCPRO;
11435 return 0;
11436 }
11437 }
11438
11439 /* Read entire contents. */
11440 rc = DGifSlurp (gif);
11441 if (rc == GIF_ERROR)
11442 {
11443 image_error ("Error reading `%s'", img->spec, Qnil);
11444 DGifCloseFile (gif);
11445 UNGCPRO;
11446 return 0;
11447 }
11448
11449 image = image_spec_value (img->spec, QCindex, NULL);
11450 ino = INTEGERP (image) ? XFASTINT (image) : 0;
11451 if (ino >= gif->ImageCount)
11452 {
11453 image_error ("Invalid image number `%s' in image `%s'",
11454 image, img->spec);
11455 DGifCloseFile (gif);
11456 UNGCPRO;
11457 return 0;
11458 }
11459
11460 width = img->width = gif->SWidth;
11461 height = img->height = gif->SHeight;
11462
11463 BLOCK_INPUT;
11464
11465 /* Create the X image and pixmap. */
11466 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
11467 {
11468 UNBLOCK_INPUT;
11469 DGifCloseFile (gif);
11470 UNGCPRO;
11471 return 0;
11472 }
11473
11474 /* Allocate colors. */
11475 gif_color_map = gif->SavedImages[ino].ImageDesc.ColorMap;
11476 if (!gif_color_map)
11477 gif_color_map = gif->SColorMap;
11478 init_color_table ();
11479 bzero (pixel_colors, sizeof pixel_colors);
11480
11481 for (i = 0; i < gif_color_map->ColorCount; ++i)
11482 {
11483 int r = gif_color_map->Colors[i].Red << 8;
11484 int g = gif_color_map->Colors[i].Green << 8;
11485 int b = gif_color_map->Colors[i].Blue << 8;
11486 pixel_colors[i] = lookup_rgb_color (f, r, g, b);
11487 }
11488
11489 img->colors = colors_in_color_table (&img->ncolors);
11490 free_color_table ();
11491
11492 /* Clear the part of the screen image that are not covered by
11493 the image from the GIF file. Full animated GIF support
11494 requires more than can be done here (see the gif89 spec,
11495 disposal methods). Let's simply assume that the part
11496 not covered by a sub-image is in the frame's background color. */
11497 image_top = gif->SavedImages[ino].ImageDesc.Top;
11498 image_left = gif->SavedImages[ino].ImageDesc.Left;
11499 image_width = gif->SavedImages[ino].ImageDesc.Width;
11500 image_height = gif->SavedImages[ino].ImageDesc.Height;
11501
11502 for (y = 0; y < image_top; ++y)
11503 for (x = 0; x < width; ++x)
11504 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
11505
11506 for (y = image_top + image_height; y < height; ++y)
11507 for (x = 0; x < width; ++x)
11508 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
11509
11510 for (y = image_top; y < image_top + image_height; ++y)
11511 {
11512 for (x = 0; x < image_left; ++x)
11513 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
11514 for (x = image_left + image_width; x < width; ++x)
11515 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
11516 }
11517
11518 /* Read the GIF image into the X image. We use a local variable
11519 `raster' here because RasterBits below is a char *, and invites
11520 problems with bytes >= 0x80. */
11521 raster = (unsigned char *) gif->SavedImages[ino].RasterBits;
11522
11523 if (gif->SavedImages[ino].ImageDesc.Interlace)
11524 {
11525 static int interlace_start[] = {0, 4, 2, 1};
11526 static int interlace_increment[] = {8, 8, 4, 2};
11527 int pass, inc;
11528 int row = interlace_start[0];
11529
11530 pass = 0;
11531
11532 for (y = 0; y < image_height; y++)
11533 {
11534 if (row >= image_height)
11535 {
11536 row = interlace_start[++pass];
11537 while (row >= image_height)
11538 row = interlace_start[++pass];
11539 }
11540
11541 for (x = 0; x < image_width; x++)
11542 {
11543 int i = raster[(y * image_width) + x];
11544 XPutPixel (ximg, x + image_left, row + image_top,
11545 pixel_colors[i]);
11546 }
11547
11548 row += interlace_increment[pass];
11549 }
11550 }
11551 else
11552 {
11553 for (y = 0; y < image_height; ++y)
11554 for (x = 0; x < image_width; ++x)
11555 {
11556 int i = raster[y* image_width + x];
11557 XPutPixel (ximg, x + image_left, y + image_top, pixel_colors[i]);
11558 }
11559 }
11560
11561 DGifCloseFile (gif);
11562
11563 /* Put the image into the pixmap, then free the X image and its buffer. */
11564 x_put_x_image (f, ximg, img->pixmap, width, height);
11565 x_destroy_x_image (ximg);
11566 UNBLOCK_INPUT;
11567
11568 UNGCPRO;
11569 return 1;
11570 }
11571
11572 #endif /* HAVE_GIF != 0 */
11573
11574
11575 \f
11576 /***********************************************************************
11577 Ghostscript
11578 ***********************************************************************/
11579
11580 #ifdef HAVE_GHOSTSCRIPT
11581 static int gs_image_p P_ ((Lisp_Object object));
11582 static int gs_load P_ ((struct frame *f, struct image *img));
11583 static void gs_clear_image P_ ((struct frame *f, struct image *img));
11584
11585 /* The symbol `postscript' identifying images of this type. */
11586
11587 Lisp_Object Qpostscript;
11588
11589 /* Keyword symbols. */
11590
11591 Lisp_Object QCloader, QCbounding_box, QCpt_width, QCpt_height;
11592
11593 /* Indices of image specification fields in gs_format, below. */
11594
11595 enum gs_keyword_index
11596 {
11597 GS_TYPE,
11598 GS_PT_WIDTH,
11599 GS_PT_HEIGHT,
11600 GS_FILE,
11601 GS_LOADER,
11602 GS_BOUNDING_BOX,
11603 GS_ASCENT,
11604 GS_MARGIN,
11605 GS_RELIEF,
11606 GS_ALGORITHM,
11607 GS_HEURISTIC_MASK,
11608 GS_LAST
11609 };
11610
11611 /* Vector of image_keyword structures describing the format
11612 of valid user-defined image specifications. */
11613
11614 static struct image_keyword gs_format[GS_LAST] =
11615 {
11616 {":type", IMAGE_SYMBOL_VALUE, 1},
11617 {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE, 1},
11618 {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE, 1},
11619 {":file", IMAGE_STRING_VALUE, 1},
11620 {":loader", IMAGE_FUNCTION_VALUE, 0},
11621 {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE, 1},
11622 {":ascent", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
11623 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
11624 {":relief", IMAGE_INTEGER_VALUE, 0},
11625 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
11626 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
11627 };
11628
11629 /* Structure describing the image type `ghostscript'. */
11630
11631 static struct image_type gs_type =
11632 {
11633 &Qpostscript,
11634 gs_image_p,
11635 gs_load,
11636 gs_clear_image,
11637 NULL
11638 };
11639
11640
11641 /* Free X resources of Ghostscript image IMG which is used on frame F. */
11642
11643 static void
11644 gs_clear_image (f, img)
11645 struct frame *f;
11646 struct image *img;
11647 {
11648 /* IMG->data.ptr_val may contain a recorded colormap. */
11649 xfree (img->data.ptr_val);
11650 x_clear_image (f, img);
11651 }
11652
11653
11654 /* Return non-zero if OBJECT is a valid Ghostscript image
11655 specification. */
11656
11657 static int
11658 gs_image_p (object)
11659 Lisp_Object object;
11660 {
11661 struct image_keyword fmt[GS_LAST];
11662 Lisp_Object tem;
11663 int i;
11664
11665 bcopy (gs_format, fmt, sizeof fmt);
11666
11667 if (!parse_image_spec (object, fmt, GS_LAST, Qpostscript)
11668 || (fmt[GS_ASCENT].count
11669 && XFASTINT (fmt[GS_ASCENT].value) > 100))
11670 return 0;
11671
11672 /* Bounding box must be a list or vector containing 4 integers. */
11673 tem = fmt[GS_BOUNDING_BOX].value;
11674 if (CONSP (tem))
11675 {
11676 for (i = 0; i < 4; ++i, tem = XCDR (tem))
11677 if (!CONSP (tem) || !INTEGERP (XCAR (tem)))
11678 return 0;
11679 if (!NILP (tem))
11680 return 0;
11681 }
11682 else if (VECTORP (tem))
11683 {
11684 if (XVECTOR (tem)->size != 4)
11685 return 0;
11686 for (i = 0; i < 4; ++i)
11687 if (!INTEGERP (XVECTOR (tem)->contents[i]))
11688 return 0;
11689 }
11690 else
11691 return 0;
11692
11693 return 1;
11694 }
11695
11696
11697 /* Load Ghostscript image IMG for use on frame F. Value is non-zero
11698 if successful. */
11699
11700 static int
11701 gs_load (f, img)
11702 struct frame *f;
11703 struct image *img;
11704 {
11705 char buffer[100];
11706 Lisp_Object window_and_pixmap_id = Qnil, loader, pt_height, pt_width;
11707 struct gcpro gcpro1, gcpro2;
11708 Lisp_Object frame;
11709 double in_width, in_height;
11710 Lisp_Object pixel_colors = Qnil;
11711
11712 /* Compute pixel size of pixmap needed from the given size in the
11713 image specification. Sizes in the specification are in pt. 1 pt
11714 = 1/72 in, xdpi and ydpi are stored in the frame's X display
11715 info. */
11716 pt_width = image_spec_value (img->spec, QCpt_width, NULL);
11717 in_width = XFASTINT (pt_width) / 72.0;
11718 img->width = in_width * FRAME_W32_DISPLAY_INFO (f)->resx;
11719 pt_height = image_spec_value (img->spec, QCpt_height, NULL);
11720 in_height = XFASTINT (pt_height) / 72.0;
11721 img->height = in_height * FRAME_W32_DISPLAY_INFO (f)->resy;
11722
11723 /* Create the pixmap. */
11724 BLOCK_INPUT;
11725 xassert (img->pixmap == 0);
11726 img->pixmap = XCreatePixmap (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
11727 img->width, img->height,
11728 DefaultDepthOfScreen (FRAME_X_SCREEN (f)));
11729 UNBLOCK_INPUT;
11730
11731 if (!img->pixmap)
11732 {
11733 image_error ("Unable to create pixmap for `%s'", img->spec, Qnil);
11734 return 0;
11735 }
11736
11737 /* Call the loader to fill the pixmap. It returns a process object
11738 if successful. We do not record_unwind_protect here because
11739 other places in redisplay like calling window scroll functions
11740 don't either. Let the Lisp loader use `unwind-protect' instead. */
11741 GCPRO2 (window_and_pixmap_id, pixel_colors);
11742
11743 sprintf (buffer, "%lu %lu",
11744 (unsigned long) FRAME_W32_WINDOW (f),
11745 (unsigned long) img->pixmap);
11746 window_and_pixmap_id = build_string (buffer);
11747
11748 sprintf (buffer, "%lu %lu",
11749 FRAME_FOREGROUND_PIXEL (f),
11750 FRAME_BACKGROUND_PIXEL (f));
11751 pixel_colors = build_string (buffer);
11752
11753 XSETFRAME (frame, f);
11754 loader = image_spec_value (img->spec, QCloader, NULL);
11755 if (NILP (loader))
11756 loader = intern ("gs-load-image");
11757
11758 img->data.lisp_val = call6 (loader, frame, img->spec,
11759 make_number (img->width),
11760 make_number (img->height),
11761 window_and_pixmap_id,
11762 pixel_colors);
11763 UNGCPRO;
11764 return PROCESSP (img->data.lisp_val);
11765 }
11766
11767
11768 /* Kill the Ghostscript process that was started to fill PIXMAP on
11769 frame F. Called from XTread_socket when receiving an event
11770 telling Emacs that Ghostscript has finished drawing. */
11771
11772 void
11773 x_kill_gs_process (pixmap, f)
11774 Pixmap pixmap;
11775 struct frame *f;
11776 {
11777 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
11778 int class, i;
11779 struct image *img;
11780
11781 /* Find the image containing PIXMAP. */
11782 for (i = 0; i < c->used; ++i)
11783 if (c->images[i]->pixmap == pixmap)
11784 break;
11785
11786 /* Kill the GS process. We should have found PIXMAP in the image
11787 cache and its image should contain a process object. */
11788 xassert (i < c->used);
11789 img = c->images[i];
11790 xassert (PROCESSP (img->data.lisp_val));
11791 Fkill_process (img->data.lisp_val, Qnil);
11792 img->data.lisp_val = Qnil;
11793
11794 /* On displays with a mutable colormap, figure out the colors
11795 allocated for the image by looking at the pixels of an XImage for
11796 img->pixmap. */
11797 class = FRAME_W32_DISPLAY_INFO (f)->visual->class;
11798 if (class != StaticColor && class != StaticGray && class != TrueColor)
11799 {
11800 XImage *ximg;
11801
11802 BLOCK_INPUT;
11803
11804 /* Try to get an XImage for img->pixmep. */
11805 ximg = XGetImage (FRAME_W32_DISPLAY (f), img->pixmap,
11806 0, 0, img->width, img->height, ~0, ZPixmap);
11807 if (ximg)
11808 {
11809 int x, y;
11810
11811 /* Initialize the color table. */
11812 init_color_table ();
11813
11814 /* For each pixel of the image, look its color up in the
11815 color table. After having done so, the color table will
11816 contain an entry for each color used by the image. */
11817 for (y = 0; y < img->height; ++y)
11818 for (x = 0; x < img->width; ++x)
11819 {
11820 unsigned long pixel = XGetPixel (ximg, x, y);
11821 lookup_pixel_color (f, pixel);
11822 }
11823
11824 /* Record colors in the image. Free color table and XImage. */
11825 img->colors = colors_in_color_table (&img->ncolors);
11826 free_color_table ();
11827 XDestroyImage (ximg);
11828
11829 #if 0 /* This doesn't seem to be the case. If we free the colors
11830 here, we get a BadAccess later in x_clear_image when
11831 freeing the colors. */
11832 /* We have allocated colors once, but Ghostscript has also
11833 allocated colors on behalf of us. So, to get the
11834 reference counts right, free them once. */
11835 if (img->ncolors)
11836 {
11837 Colormap cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f));
11838 XFreeColors (FRAME_W32_DISPLAY (f), cmap,
11839 img->colors, img->ncolors, 0);
11840 }
11841 #endif
11842 }
11843 else
11844 image_error ("Cannot get X image of `%s'; colors will not be freed",
11845 img->spec, Qnil);
11846
11847 UNBLOCK_INPUT;
11848 }
11849 }
11850
11851 #endif /* HAVE_GHOSTSCRIPT */
11852
11853 \f
11854 /***********************************************************************
11855 Window properties
11856 ***********************************************************************/
11857
11858 DEFUN ("x-change-window-property", Fx_change_window_property,
11859 Sx_change_window_property, 2, 3, 0,
11860 "Change window property PROP to VALUE on the X window of FRAME.\n\
11861 PROP and VALUE must be strings. FRAME nil or omitted means use the\n\
11862 selected frame. Value is VALUE.")
11863 (prop, value, frame)
11864 Lisp_Object frame, prop, value;
11865 {
11866 #if 0 /* TODO : port window properties to W32 */
11867 struct frame *f = check_x_frame (frame);
11868 Atom prop_atom;
11869
11870 CHECK_STRING (prop, 1);
11871 CHECK_STRING (value, 2);
11872
11873 BLOCK_INPUT;
11874 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), XSTRING (prop)->data, False);
11875 XChangeProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
11876 prop_atom, XA_STRING, 8, PropModeReplace,
11877 XSTRING (value)->data, XSTRING (value)->size);
11878
11879 /* Make sure the property is set when we return. */
11880 XFlush (FRAME_W32_DISPLAY (f));
11881 UNBLOCK_INPUT;
11882
11883 #endif /* TODO */
11884
11885 return value;
11886 }
11887
11888
11889 DEFUN ("x-delete-window-property", Fx_delete_window_property,
11890 Sx_delete_window_property, 1, 2, 0,
11891 "Remove window property PROP from X window of FRAME.\n\
11892 FRAME nil or omitted means use the selected frame. Value is PROP.")
11893 (prop, frame)
11894 Lisp_Object prop, frame;
11895 {
11896 #if 0 /* TODO : port window properties to W32 */
11897
11898 struct frame *f = check_x_frame (frame);
11899 Atom prop_atom;
11900
11901 CHECK_STRING (prop, 1);
11902 BLOCK_INPUT;
11903 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), XSTRING (prop)->data, False);
11904 XDeleteProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), prop_atom);
11905
11906 /* Make sure the property is removed when we return. */
11907 XFlush (FRAME_W32_DISPLAY (f));
11908 UNBLOCK_INPUT;
11909 #endif /* TODO */
11910
11911 return prop;
11912 }
11913
11914
11915 DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
11916 1, 2, 0,
11917 "Value is the value of window property PROP on FRAME.\n\
11918 If FRAME is nil or omitted, use the selected frame. Value is nil\n\
11919 if FRAME hasn't a property with name PROP or if PROP has no string\n\
11920 value.")
11921 (prop, frame)
11922 Lisp_Object prop, frame;
11923 {
11924 #if 0 /* TODO : port window properties to W32 */
11925
11926 struct frame *f = check_x_frame (frame);
11927 Atom prop_atom;
11928 int rc;
11929 Lisp_Object prop_value = Qnil;
11930 char *tmp_data = NULL;
11931 Atom actual_type;
11932 int actual_format;
11933 unsigned long actual_size, bytes_remaining;
11934
11935 CHECK_STRING (prop, 1);
11936 BLOCK_INPUT;
11937 prop_atom = XInternAtom (FRAME_W32_DISPLAY (f), XSTRING (prop)->data, False);
11938 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
11939 prop_atom, 0, 0, False, XA_STRING,
11940 &actual_type, &actual_format, &actual_size,
11941 &bytes_remaining, (unsigned char **) &tmp_data);
11942 if (rc == Success)
11943 {
11944 int size = bytes_remaining;
11945
11946 XFree (tmp_data);
11947 tmp_data = NULL;
11948
11949 rc = XGetWindowProperty (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
11950 prop_atom, 0, bytes_remaining,
11951 False, XA_STRING,
11952 &actual_type, &actual_format,
11953 &actual_size, &bytes_remaining,
11954 (unsigned char **) &tmp_data);
11955 if (rc == Success)
11956 prop_value = make_string (tmp_data, size);
11957
11958 XFree (tmp_data);
11959 }
11960
11961 UNBLOCK_INPUT;
11962
11963 return prop_value;
11964
11965 #endif /* TODO */
11966 return Qnil;
11967 }
11968
11969
11970 \f
11971 /***********************************************************************
11972 Busy cursor
11973 ***********************************************************************/
11974
11975 /* If non-null, an asynchronous timer that, when it expires, displays
11976 a busy cursor on all frames. */
11977
11978 static struct atimer *busy_cursor_atimer;
11979
11980 /* Non-zero means a busy cursor is currently shown. */
11981
11982 static int busy_cursor_shown_p;
11983
11984 /* Number of seconds to wait before displaying a busy cursor. */
11985
11986 static Lisp_Object Vbusy_cursor_delay;
11987
11988 /* Default number of seconds to wait before displaying a busy
11989 cursor. */
11990
11991 #define DEFAULT_BUSY_CURSOR_DELAY 1
11992
11993 /* Function prototypes. */
11994
11995 static void show_busy_cursor P_ ((struct atimer *));
11996 static void hide_busy_cursor P_ ((void));
11997
11998
11999 /* Cancel a currently active busy-cursor timer, and start a new one. */
12000
12001 void
12002 start_busy_cursor ()
12003 {
12004 #if 0 /* TODO: cursor shape changes. */
12005 EMACS_TIME delay;
12006 int secs, usecs = 0;
12007
12008 cancel_busy_cursor ();
12009
12010 if (INTEGERP (Vbusy_cursor_delay)
12011 && XINT (Vbusy_cursor_delay) > 0)
12012 secs = XFASTINT (Vbusy_cursor_delay);
12013 else if (FLOATP (Vbusy_cursor_delay)
12014 && XFLOAT_DATA (Vbusy_cursor_delay) > 0)
12015 {
12016 Lisp_Object tem;
12017 tem = Ftruncate (Vbusy_cursor_delay, Qnil);
12018 secs = XFASTINT (tem);
12019 usecs = (XFLOAT_DATA (Vbusy_cursor_delay) - secs) * 1000000;
12020 }
12021 else
12022 secs = DEFAULT_BUSY_CURSOR_DELAY;
12023
12024 EMACS_SET_SECS_USECS (delay, secs, usecs);
12025 busy_cursor_atimer = start_atimer (ATIMER_RELATIVE, delay,
12026 show_busy_cursor, NULL);
12027 #endif
12028 }
12029
12030
12031 /* Cancel the busy cursor timer if active, hide a busy cursor if
12032 shown. */
12033
12034 void
12035 cancel_busy_cursor ()
12036 {
12037 if (busy_cursor_atimer)
12038 {
12039 cancel_atimer (busy_cursor_atimer);
12040 busy_cursor_atimer = NULL;
12041 }
12042
12043 if (busy_cursor_shown_p)
12044 hide_busy_cursor ();
12045 }
12046
12047
12048 /* Timer function of busy_cursor_atimer. TIMER is equal to
12049 busy_cursor_atimer.
12050
12051 Display a busy cursor on all frames by mapping the frames'
12052 busy_window. Set the busy_p flag in the frames' output_data.x
12053 structure to indicate that a busy cursor is shown on the
12054 frames. */
12055
12056 static void
12057 show_busy_cursor (timer)
12058 struct atimer *timer;
12059 {
12060 #if 0 /* TODO: cursor shape changes. */
12061 /* The timer implementation will cancel this timer automatically
12062 after this function has run. Set busy_cursor_atimer to null
12063 so that we know the timer doesn't have to be canceled. */
12064 busy_cursor_atimer = NULL;
12065
12066 if (!busy_cursor_shown_p)
12067 {
12068 Lisp_Object rest, frame;
12069
12070 BLOCK_INPUT;
12071
12072 FOR_EACH_FRAME (rest, frame)
12073 if (FRAME_X_P (XFRAME (frame)))
12074 {
12075 struct frame *f = XFRAME (frame);
12076
12077 f->output_data.w32->busy_p = 1;
12078
12079 if (!f->output_data.w32->busy_window)
12080 {
12081 unsigned long mask = CWCursor;
12082 XSetWindowAttributes attrs;
12083
12084 attrs.cursor = f->output_data.w32->busy_cursor;
12085
12086 f->output_data.w32->busy_window
12087 = XCreateWindow (FRAME_X_DISPLAY (f),
12088 FRAME_OUTER_WINDOW (f),
12089 0, 0, 32000, 32000, 0, 0,
12090 InputOnly,
12091 CopyFromParent,
12092 mask, &attrs);
12093 }
12094
12095 XMapRaised (FRAME_X_DISPLAY (f), f->output_data.w32->busy_window);
12096 XFlush (FRAME_X_DISPLAY (f));
12097 }
12098
12099 busy_cursor_shown_p = 1;
12100 UNBLOCK_INPUT;
12101 }
12102 #endif
12103 }
12104
12105
12106 /* Hide the busy cursor on all frames, if it is currently shown. */
12107
12108 static void
12109 hide_busy_cursor ()
12110 {
12111 #if 0 /* TODO: cursor shape changes. */
12112 if (busy_cursor_shown_p)
12113 {
12114 Lisp_Object rest, frame;
12115
12116 BLOCK_INPUT;
12117 FOR_EACH_FRAME (rest, frame)
12118 {
12119 struct frame *f = XFRAME (frame);
12120
12121 if (FRAME_X_P (f)
12122 /* Watch out for newly created frames. */
12123 && f->output_data.x->busy_window)
12124 {
12125 XUnmapWindow (FRAME_X_DISPLAY (f), f->output_data.x->busy_window);
12126 /* Sync here because XTread_socket looks at the busy_p flag
12127 that is reset to zero below. */
12128 XSync (FRAME_X_DISPLAY (f), False);
12129 f->output_data.x->busy_p = 0;
12130 }
12131 }
12132
12133 busy_cursor_shown_p = 0;
12134 UNBLOCK_INPUT;
12135 }
12136 #endif
12137 }
12138
12139
12140 \f
12141 /***********************************************************************
12142 Tool tips
12143 ***********************************************************************/
12144
12145 static Lisp_Object x_create_tip_frame P_ ((struct w32_display_info *,
12146 Lisp_Object));
12147
12148 /* The frame of a currently visible tooltip, or null. */
12149
12150 struct frame *tip_frame;
12151
12152 /* If non-nil, a timer started that hides the last tooltip when it
12153 fires. */
12154
12155 Lisp_Object tip_timer;
12156 Window tip_window;
12157
12158 /* Create a frame for a tooltip on the display described by DPYINFO.
12159 PARMS is a list of frame parameters. Value is the frame. */
12160
12161 static Lisp_Object
12162 x_create_tip_frame (dpyinfo, parms)
12163 struct w32_display_info *dpyinfo;
12164 Lisp_Object parms;
12165 {
12166 #if 0 /* TODO : w32 version */
12167 struct frame *f;
12168 Lisp_Object frame, tem;
12169 Lisp_Object name;
12170 long window_prompting = 0;
12171 int width, height;
12172 int count = specpdl_ptr - specpdl;
12173 struct gcpro gcpro1, gcpro2, gcpro3;
12174 struct kboard *kb;
12175
12176 check_x ();
12177
12178 /* Use this general default value to start with until we know if
12179 this frame has a specified name. */
12180 Vx_resource_name = Vinvocation_name;
12181
12182 #ifdef MULTI_KBOARD
12183 kb = dpyinfo->kboard;
12184 #else
12185 kb = &the_only_kboard;
12186 #endif
12187
12188 /* Get the name of the frame to use for resource lookup. */
12189 name = w32_get_arg (parms, Qname, "name", "Name", RES_TYPE_STRING);
12190 if (!STRINGP (name)
12191 && !EQ (name, Qunbound)
12192 && !NILP (name))
12193 error ("Invalid frame name--not a string or nil");
12194 Vx_resource_name = name;
12195
12196 frame = Qnil;
12197 GCPRO3 (parms, name, frame);
12198 tip_frame = f = make_frame (1);
12199 XSETFRAME (frame, f);
12200 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
12201
12202 f->output_method = output_w32;
12203 f->output_data.w32 =
12204 (struct w32_output *) xmalloc (sizeof (struct w32_output));
12205 bzero (f->output_data.w32, sizeof (struct w32_output));
12206 #if 0
12207 f->output_data.w32->icon_bitmap = -1;
12208 #endif
12209 f->output_data.w32->fontset = -1;
12210 f->icon_name = Qnil;
12211
12212 #ifdef MULTI_KBOARD
12213 FRAME_KBOARD (f) = kb;
12214 #endif
12215 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
12216 f->output_data.w32->explicit_parent = 0;
12217
12218 /* Set the name; the functions to which we pass f expect the name to
12219 be set. */
12220 if (EQ (name, Qunbound) || NILP (name))
12221 {
12222 f->name = build_string (dpyinfo->x_id_name);
12223 f->explicit_name = 0;
12224 }
12225 else
12226 {
12227 f->name = name;
12228 f->explicit_name = 1;
12229 /* use the frame's title when getting resources for this frame. */
12230 specbind (Qx_resource_name, name);
12231 }
12232
12233 /* Extract the window parameters from the supplied values
12234 that are needed to determine window geometry. */
12235 {
12236 Lisp_Object font;
12237
12238 font = w32_get_arg (parms, Qfont, "font", "Font", RES_TYPE_STRING);
12239
12240 BLOCK_INPUT;
12241 /* First, try whatever font the caller has specified. */
12242 if (STRINGP (font))
12243 {
12244 tem = Fquery_fontset (font, Qnil);
12245 if (STRINGP (tem))
12246 font = x_new_fontset (f, XSTRING (tem)->data);
12247 else
12248 font = x_new_font (f, XSTRING (font)->data);
12249 }
12250
12251 /* Try out a font which we hope has bold and italic variations. */
12252 if (!STRINGP (font))
12253 font = x_new_font (f, "-*-courier new-normal-r-*-*-*-100-*-*-*-*-iso8859-1");
12254 if (!STRINGP (font))
12255 font = x_new_font (f, "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
12256 if (! STRINGP (font))
12257 font = x_new_font (f, "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
12258 if (! STRINGP (font))
12259 /* This was formerly the first thing tried, but it finds too many fonts
12260 and takes too long. */
12261 font = x_new_font (f, "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1");
12262 /* If those didn't work, look for something which will at least work. */
12263 if (! STRINGP (font))
12264 font = x_new_font (f, "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1");
12265 UNBLOCK_INPUT;
12266 if (! STRINGP (font))
12267 font = build_string ("fixed");
12268
12269 x_default_parameter (f, parms, Qfont, font,
12270 "font", "Font", RES_TYPE_STRING);
12271 }
12272
12273 x_default_parameter (f, parms, Qborder_width, make_number (2),
12274 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
12275
12276 /* This defaults to 2 in order to match xterm. We recognize either
12277 internalBorderWidth or internalBorder (which is what xterm calls
12278 it). */
12279 if (NILP (Fassq (Qinternal_border_width, parms)))
12280 {
12281 Lisp_Object value;
12282
12283 value = w32_get_arg (parms, Qinternal_border_width,
12284 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
12285 if (! EQ (value, Qunbound))
12286 parms = Fcons (Fcons (Qinternal_border_width, value),
12287 parms);
12288 }
12289
12290 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
12291 "internalBorderWidth", "internalBorderWidth",
12292 RES_TYPE_NUMBER);
12293
12294 /* Also do the stuff which must be set before the window exists. */
12295 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
12296 "foreground", "Foreground", RES_TYPE_STRING);
12297 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
12298 "background", "Background", RES_TYPE_STRING);
12299 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
12300 "pointerColor", "Foreground", RES_TYPE_STRING);
12301 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
12302 "cursorColor", "Foreground", RES_TYPE_STRING);
12303 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
12304 "borderColor", "BorderColor", RES_TYPE_STRING);
12305
12306 /* Init faces before x_default_parameter is called for scroll-bar
12307 parameters because that function calls x_set_scroll_bar_width,
12308 which calls change_frame_size, which calls Fset_window_buffer,
12309 which runs hooks, which call Fvertical_motion. At the end, we
12310 end up in init_iterator with a null face cache, which should not
12311 happen. */
12312 init_frame_faces (f);
12313
12314 f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
12315 window_prompting = x_figure_window_size (f, parms);
12316
12317 if (window_prompting & XNegative)
12318 {
12319 if (window_prompting & YNegative)
12320 f->output_data.w32->win_gravity = SouthEastGravity;
12321 else
12322 f->output_data.w32->win_gravity = NorthEastGravity;
12323 }
12324 else
12325 {
12326 if (window_prompting & YNegative)
12327 f->output_data.w32->win_gravity = SouthWestGravity;
12328 else
12329 f->output_data.w32->win_gravity = NorthWestGravity;
12330 }
12331
12332 f->output_data.w32->size_hint_flags = window_prompting;
12333 {
12334 XSetWindowAttributes attrs;
12335 unsigned long mask;
12336
12337 BLOCK_INPUT;
12338 mask = CWBackPixel | CWOverrideRedirect | CWSaveUnder | CWEventMask;
12339 /* Window managers looks at the override-redirect flag to
12340 determine whether or net to give windows a decoration (Xlib
12341 3.2.8). */
12342 attrs.override_redirect = True;
12343 attrs.save_under = True;
12344 attrs.background_pixel = FRAME_BACKGROUND_PIXEL (f);
12345 /* Arrange for getting MapNotify and UnmapNotify events. */
12346 attrs.event_mask = StructureNotifyMask;
12347 tip_window
12348 = FRAME_W32_WINDOW (f)
12349 = XCreateWindow (FRAME_W32_DISPLAY (f),
12350 FRAME_W32_DISPLAY_INFO (f)->root_window,
12351 /* x, y, width, height */
12352 0, 0, 1, 1,
12353 /* Border. */
12354 1,
12355 CopyFromParent, InputOutput, CopyFromParent,
12356 mask, &attrs);
12357 UNBLOCK_INPUT;
12358 }
12359
12360 x_make_gc (f);
12361
12362 x_default_parameter (f, parms, Qauto_raise, Qnil,
12363 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
12364 x_default_parameter (f, parms, Qauto_lower, Qnil,
12365 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
12366 x_default_parameter (f, parms, Qcursor_type, Qbox,
12367 "cursorType", "CursorType", RES_TYPE_SYMBOL);
12368
12369 /* Dimensions, especially f->height, must be done via change_frame_size.
12370 Change will not be effected unless different from the current
12371 f->height. */
12372 width = f->width;
12373 height = f->height;
12374 f->height = 0;
12375 SET_FRAME_WIDTH (f, 0);
12376 change_frame_size (f, height, width, 1, 0, 0);
12377
12378 f->no_split = 1;
12379
12380 UNGCPRO;
12381
12382 /* It is now ok to make the frame official even if we get an error
12383 below. And the frame needs to be on Vframe_list or making it
12384 visible won't work. */
12385 Vframe_list = Fcons (frame, Vframe_list);
12386
12387 /* Now that the frame is official, it counts as a reference to
12388 its display. */
12389 FRAME_W32_DISPLAY_INFO (f)->reference_count++;
12390
12391 return unbind_to (count, frame);
12392 #endif /* TODO */
12393 return Qnil;
12394 }
12395
12396 #ifdef TODO /* Tooltip support not complete. */
12397 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
12398 "Show STRING in a \"tooltip\" window on frame FRAME.\n\
12399 A tooltip window is a small X window displaying a string.\n\
12400 \n\
12401 FRAME nil or omitted means use the selected frame.\n\
12402 \n\
12403 PARMS is an optional list of frame parameters which can be\n\
12404 used to change the tooltip's appearance.\n\
12405 \n\
12406 Automatically hide the tooltip after TIMEOUT seconds.\n\
12407 TIMEOUT nil means use the default timeout of 5 seconds.\n\
12408 \n\
12409 If the list of frame parameters PARAMS contains a `left' parameters,\n\
12410 the tooltip is displayed at that x-position. Otherwise it is\n\
12411 displayed at the mouse position, with offset DX added (default is 5 if\n\
12412 DX isn't specified). Likewise for the y-position; if a `top' frame\n\
12413 parameter is specified, it determines the y-position of the tooltip\n\
12414 window, otherwise it is displayed at the mouse position, with offset\n\
12415 DY added (default is -5).")
12416 (string, frame, parms, timeout, dx, dy)
12417 Lisp_Object string, frame, parms, timeout, dx, dy;
12418 {
12419 struct frame *f;
12420 struct window *w;
12421 Window root, child;
12422 Lisp_Object buffer, top, left;
12423 struct buffer *old_buffer;
12424 struct text_pos pos;
12425 int i, width, height;
12426 int root_x, root_y, win_x, win_y;
12427 unsigned pmask;
12428 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
12429 int old_windows_or_buffers_changed = windows_or_buffers_changed;
12430 int count = specpdl_ptr - specpdl;
12431
12432 specbind (Qinhibit_redisplay, Qt);
12433
12434 GCPRO4 (string, parms, frame, timeout);
12435
12436 CHECK_STRING (string, 0);
12437 f = check_x_frame (frame);
12438 if (NILP (timeout))
12439 timeout = make_number (5);
12440 else
12441 CHECK_NATNUM (timeout, 2);
12442
12443 if (NILP (dx))
12444 dx = make_number (5);
12445 else
12446 CHECK_NUMBER (dx, 5);
12447
12448 if (NILP (dy))
12449 dy = make_number (-5);
12450 else
12451 CHECK_NUMBER (dy, 6);
12452
12453 /* Hide a previous tip, if any. */
12454 Fx_hide_tip ();
12455
12456 /* Add default values to frame parameters. */
12457 if (NILP (Fassq (Qname, parms)))
12458 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
12459 if (NILP (Fassq (Qinternal_border_width, parms)))
12460 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
12461 if (NILP (Fassq (Qborder_width, parms)))
12462 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
12463 if (NILP (Fassq (Qborder_color, parms)))
12464 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
12465 if (NILP (Fassq (Qbackground_color, parms)))
12466 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
12467 parms);
12468
12469 /* Create a frame for the tooltip, and record it in the global
12470 variable tip_frame. */
12471 frame = x_create_tip_frame (FRAME_W32_DISPLAY_INFO (f), parms);
12472 tip_frame = f = XFRAME (frame);
12473
12474 /* Set up the frame's root window. Currently we use a size of 80
12475 columns x 40 lines. If someone wants to show a larger tip, he
12476 will loose. I don't think this is a realistic case. */
12477 w = XWINDOW (FRAME_ROOT_WINDOW (f));
12478 w->left = w->top = make_number (0);
12479 w->width = 80;
12480 w->height = 40;
12481 adjust_glyphs (f);
12482 w->pseudo_window_p = 1;
12483
12484 /* Display the tooltip text in a temporary buffer. */
12485 buffer = Fget_buffer_create (build_string (" *tip*"));
12486 Fset_window_buffer (FRAME_ROOT_WINDOW (f), buffer);
12487 old_buffer = current_buffer;
12488 set_buffer_internal_1 (XBUFFER (buffer));
12489 Ferase_buffer ();
12490 Finsert (make_number (1), &string);
12491 clear_glyph_matrix (w->desired_matrix);
12492 clear_glyph_matrix (w->current_matrix);
12493 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
12494 try_window (FRAME_ROOT_WINDOW (f), pos);
12495
12496 /* Compute width and height of the tooltip. */
12497 width = height = 0;
12498 for (i = 0; i < w->desired_matrix->nrows; ++i)
12499 {
12500 struct glyph_row *row = &w->desired_matrix->rows[i];
12501 struct glyph *last;
12502 int row_width;
12503
12504 /* Stop at the first empty row at the end. */
12505 if (!row->enabled_p || !row->displays_text_p)
12506 break;
12507
12508 /* Let the row go over the full width of the frame. */
12509 row->full_width_p = 1;
12510
12511 /* There's a glyph at the end of rows that is use to place
12512 the cursor there. Don't include the width of this glyph. */
12513 if (row->used[TEXT_AREA])
12514 {
12515 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
12516 row_width = row->pixel_width - last->pixel_width;
12517 }
12518 else
12519 row_width = row->pixel_width;
12520
12521 height += row->height;
12522 width = max (width, row_width);
12523 }
12524
12525 /* Add the frame's internal border to the width and height the X
12526 window should have. */
12527 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
12528 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
12529
12530 /* User-specified position? */
12531 left = Fcdr (Fassq (Qleft, parms));
12532 top = Fcdr (Fassq (Qtop, parms));
12533
12534 /* Move the tooltip window where the mouse pointer is. Resize and
12535 show it. */
12536 #if 0 /* TODO : W32 specifics */
12537 BLOCK_INPUT;
12538 XQueryPointer (FRAME_X_DISPLAY (f), FRAME_X_DISPLAY_INFO (f)->root_window,
12539 &root, &child, &root_x, &root_y, &win_x, &win_y, &pmask);
12540 UNBLOCK_INPUT;
12541
12542 root_x += XINT (dx);
12543 root_y += XINT (dy);
12544
12545 if (INTEGERP (left))
12546 root_x = XINT (left);
12547 if (INTEGERP (top))
12548 root_y = XINT (top);
12549
12550 BLOCK_INPUT;
12551 XMoveResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12552 root_x, root_y - height, width, height);
12553 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12554 UNBLOCK_INPUT;
12555 #endif /* TODO */
12556
12557 /* Draw into the window. */
12558 w->must_be_updated_p = 1;
12559 update_single_window (w, 1);
12560
12561 /* Restore original current buffer. */
12562 set_buffer_internal_1 (old_buffer);
12563 windows_or_buffers_changed = old_windows_or_buffers_changed;
12564
12565 /* Let the tip disappear after timeout seconds. */
12566 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
12567 intern ("x-hide-tip"));
12568
12569 UNGCPRO;
12570 return unbind_to (count, Qnil);
12571 }
12572
12573
12574 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
12575 "Hide the current tooltip window, if there is any.\n\
12576 Value is t is tooltip was open, nil otherwise.")
12577 ()
12578 {
12579 int count = specpdl_ptr - specpdl;
12580 int deleted_p = 0;
12581
12582 specbind (Qinhibit_redisplay, Qt);
12583
12584 if (!NILP (tip_timer))
12585 {
12586 call1 (intern ("cancel-timer"), tip_timer);
12587 tip_timer = Qnil;
12588 }
12589
12590 if (tip_frame)
12591 {
12592 Lisp_Object frame;
12593
12594 XSETFRAME (frame, tip_frame);
12595 Fdelete_frame (frame, Qt);
12596 tip_frame = NULL;
12597 deleted_p = 1;
12598 }
12599
12600 return unbind_to (count, deleted_p ? Qt : Qnil);
12601 }
12602 #endif
12603
12604
12605 \f
12606 /***********************************************************************
12607 File selection dialog
12608 ***********************************************************************/
12609
12610 extern Lisp_Object Qfile_name_history;
12611
12612 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 4, 0,
12613 "Read file name, prompting with PROMPT in directory DIR.\n\
12614 Use a file selection dialog.\n\
12615 Select DEFAULT-FILENAME in the dialog's file selection box, if\n\
12616 specified. Don't let the user enter a file name in the file\n\
12617 selection dialog's entry field, if MUSTMATCH is non-nil.")
12618 (prompt, dir, default_filename, mustmatch)
12619 Lisp_Object prompt, dir, default_filename, mustmatch;
12620 {
12621 struct frame *f = SELECTED_FRAME ();
12622 Lisp_Object file = Qnil;
12623 int count = specpdl_ptr - specpdl;
12624 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
12625 char filename[MAX_PATH + 1];
12626 char init_dir[MAX_PATH + 1];
12627 int use_dialog_p = 1;
12628
12629 GCPRO5 (prompt, dir, default_filename, mustmatch, file);
12630 CHECK_STRING (prompt, 0);
12631 CHECK_STRING (dir, 1);
12632
12633 /* Create the dialog with PROMPT as title, using DIR as initial
12634 directory and using "*" as pattern. */
12635 dir = Fexpand_file_name (dir, Qnil);
12636 strncpy (init_dir, XSTRING (dir)->data, MAX_PATH);
12637 init_dir[MAX_PATH] = '\0';
12638 unixtodos_filename (init_dir);
12639
12640 if (STRINGP (default_filename))
12641 {
12642 char *file_name_only;
12643 char *full_path_name = XSTRING (default_filename)->data;
12644
12645 unixtodos_filename (full_path_name);
12646
12647 file_name_only = strrchr (full_path_name, '\\');
12648 if (!file_name_only)
12649 file_name_only = full_path_name;
12650 else
12651 {
12652 file_name_only++;
12653
12654 /* If default_file_name is a directory, don't use the open
12655 file dialog, as it does not support selecting
12656 directories. */
12657 if (!(*file_name_only))
12658 use_dialog_p = 0;
12659 }
12660
12661 strncpy (filename, file_name_only, MAX_PATH);
12662 filename[MAX_PATH] = '\0';
12663 }
12664 else
12665 filename[0] = '\0';
12666
12667 if (use_dialog_p)
12668 {
12669 OPENFILENAME file_details;
12670 char *filename_file;
12671
12672 /* Prevent redisplay. */
12673 specbind (Qinhibit_redisplay, Qt);
12674 BLOCK_INPUT;
12675
12676 bzero (&file_details, sizeof (file_details));
12677 file_details.lStructSize = sizeof (file_details);
12678 file_details.hwndOwner = FRAME_W32_WINDOW (f);
12679 file_details.lpstrFile = filename;
12680 file_details.nMaxFile = sizeof (filename);
12681 file_details.lpstrInitialDir = init_dir;
12682 file_details.lpstrTitle = XSTRING (prompt)->data;
12683 file_details.Flags = OFN_HIDEREADONLY | OFN_NOCHANGEDIR;
12684
12685 if (!NILP (mustmatch))
12686 file_details.Flags |= OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
12687
12688 if (GetOpenFileName (&file_details))
12689 {
12690 dostounix_filename (filename);
12691 file = build_string (filename);
12692 }
12693 else
12694 file = Qnil;
12695
12696 UNBLOCK_INPUT;
12697 file = unbind_to (count, file);
12698 }
12699 /* Open File dialog will not allow folders to be selected, so resort
12700 to minibuffer completing reads for directories. */
12701 else
12702 file = Fcompleting_read (prompt, intern ("read-file-name-internal"),
12703 dir, mustmatch, dir, Qfile_name_history,
12704 default_filename, Qnil);
12705
12706 UNGCPRO;
12707
12708 /* Make "Cancel" equivalent to C-g. */
12709 if (NILP (file))
12710 Fsignal (Qquit, Qnil);
12711
12712 return unbind_to (count, file);
12713 }
12714
12715
12716 \f
12717 /***********************************************************************
12718 Tests
12719 ***********************************************************************/
12720
12721 #if GLYPH_DEBUG
12722
12723 DEFUN ("imagep", Fimagep, Simagep, 1, 1, 0,
12724 "Value is non-nil if SPEC is a valid image specification.")
12725 (spec)
12726 Lisp_Object spec;
12727 {
12728 return valid_image_p (spec) ? Qt : Qnil;
12729 }
12730
12731
12732 DEFUN ("lookup-image", Flookup_image, Slookup_image, 1, 1, 0, "")
12733 (spec)
12734 Lisp_Object spec;
12735 {
12736 int id = -1;
12737
12738 if (valid_image_p (spec))
12739 id = lookup_image (SELECTED_FRAME (), spec);
12740
12741 debug_print (spec);
12742 return make_number (id);
12743 }
12744
12745 #endif /* GLYPH_DEBUG != 0 */
12746
12747
12748 \f
12749 /***********************************************************************
12750 w32 specialized functions
12751 ***********************************************************************/
12752
12753 DEFUN ("w32-select-font", Fw32_select_font, Sw32_select_font, 0, 1, 0,
12754 "This will display the W32 font dialog and return an X font string corresponding to the selection.")
12755 (frame)
12756 Lisp_Object frame;
12757 {
12758 FRAME_PTR f = check_x_frame (frame);
12759 CHOOSEFONT cf;
12760 LOGFONT lf;
12761 TEXTMETRIC tm;
12762 HDC hdc;
12763 HANDLE oldobj;
12764 char buf[100];
12765
12766 bzero (&cf, sizeof (cf));
12767 bzero (&lf, sizeof (lf));
12768
12769 cf.lStructSize = sizeof (cf);
12770 cf.hwndOwner = FRAME_W32_WINDOW (f);
12771 cf.Flags = CF_FORCEFONTEXIST | CF_SCREENFONTS;
12772 cf.lpLogFont = &lf;
12773
12774 /* Initialize as much of the font details as we can from the current
12775 default font. */
12776 hdc = GetDC (FRAME_W32_WINDOW (f));
12777 oldobj = SelectObject (hdc, FRAME_FONT (f)->hfont);
12778 GetTextFace (hdc, LF_FACESIZE, lf.lfFaceName);
12779 if (GetTextMetrics (hdc, &tm))
12780 {
12781 lf.lfHeight = tm.tmInternalLeading - tm.tmHeight;
12782 lf.lfWeight = tm.tmWeight;
12783 lf.lfItalic = tm.tmItalic;
12784 lf.lfUnderline = tm.tmUnderlined;
12785 lf.lfStrikeOut = tm.tmStruckOut;
12786 lf.lfCharSet = tm.tmCharSet;
12787 cf.Flags |= CF_INITTOLOGFONTSTRUCT;
12788 }
12789 SelectObject (hdc, oldobj);
12790 ReleaseDC (FRAME_W32_WINDOW (f), hdc);
12791
12792 if (!ChooseFont (&cf) || !w32_to_x_font (&lf, buf, 100, NULL))
12793 return Qnil;
12794
12795 return build_string (buf);
12796 }
12797
12798 DEFUN ("w32-send-sys-command", Fw32_send_sys_command, Sw32_send_sys_command, 1, 2, 0,
12799 "Send frame a Windows WM_SYSCOMMAND message of type COMMAND.\n\
12800 Some useful values for command are 0xf030 to maximise frame (0xf020\n\
12801 to minimize), 0xf120 to restore frame to original size, and 0xf100\n\
12802 to activate the menubar for keyboard access. 0xf140 activates the\n\
12803 screen saver if defined.\n\
12804 \n\
12805 If optional parameter FRAME is not specified, use selected frame.")
12806 (command, frame)
12807 Lisp_Object command, frame;
12808 {
12809 WPARAM code;
12810 FRAME_PTR f = check_x_frame (frame);
12811
12812 CHECK_NUMBER (command, 0);
12813
12814 PostMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, XINT (command), 0);
12815
12816 return Qnil;
12817 }
12818
12819 DEFUN ("w32-shell-execute", Fw32_shell_execute, Sw32_shell_execute, 2, 4, 0,
12820 "Get Windows to perform OPERATION on DOCUMENT.\n\
12821 This is a wrapper around the ShellExecute system function, which\n\
12822 invokes the application registered to handle OPERATION for DOCUMENT.\n\
12823 OPERATION is typically \"open\", \"print\" or \"explore\" (but can be\n\
12824 nil for the default action), and DOCUMENT is typically the name of a\n\
12825 document file or URL, but can also be a program executable to run or\n\
12826 a directory to open in the Windows Explorer.\n\
12827 \n\
12828 If DOCUMENT is a program executable, PARAMETERS can be a string\n\
12829 containing command line parameters, but otherwise should be nil.\n\
12830 \n\
12831 SHOW-FLAG can be used to control whether the invoked application is hidden\n\
12832 or minimized. If SHOW-FLAG is nil, the application is displayed normally,\n\
12833 otherwise it is an integer representing a ShowWindow flag:\n\
12834 \n\
12835 0 - start hidden\n\
12836 1 - start normally\n\
12837 3 - start maximized\n\
12838 6 - start minimized")
12839 (operation, document, parameters, show_flag)
12840 Lisp_Object operation, document, parameters, show_flag;
12841 {
12842 Lisp_Object current_dir;
12843
12844 CHECK_STRING (document, 0);
12845
12846 /* Encode filename and current directory. */
12847 current_dir = ENCODE_FILE (current_buffer->directory);
12848 document = ENCODE_FILE (document);
12849 if ((int) ShellExecute (NULL,
12850 (STRINGP (operation) ?
12851 XSTRING (operation)->data : NULL),
12852 XSTRING (document)->data,
12853 (STRINGP (parameters) ?
12854 XSTRING (parameters)->data : NULL),
12855 XSTRING (current_dir)->data,
12856 (INTEGERP (show_flag) ?
12857 XINT (show_flag) : SW_SHOWDEFAULT))
12858 > 32)
12859 return Qt;
12860 error ("ShellExecute failed");
12861 }
12862
12863 /* Lookup virtual keycode from string representing the name of a
12864 non-ascii keystroke into the corresponding virtual key, using
12865 lispy_function_keys. */
12866 static int
12867 lookup_vk_code (char *key)
12868 {
12869 int i;
12870
12871 for (i = 0; i < 256; i++)
12872 if (lispy_function_keys[i] != 0
12873 && strcmp (lispy_function_keys[i], key) == 0)
12874 return i;
12875
12876 return -1;
12877 }
12878
12879 /* Convert a one-element vector style key sequence to a hot key
12880 definition. */
12881 static int
12882 w32_parse_hot_key (key)
12883 Lisp_Object key;
12884 {
12885 /* Copied from Fdefine_key and store_in_keymap. */
12886 register Lisp_Object c;
12887 int vk_code;
12888 int lisp_modifiers;
12889 int w32_modifiers;
12890 struct gcpro gcpro1;
12891
12892 CHECK_VECTOR (key, 0);
12893
12894 if (XFASTINT (Flength (key)) != 1)
12895 return Qnil;
12896
12897 GCPRO1 (key);
12898
12899 c = Faref (key, make_number (0));
12900
12901 if (CONSP (c) && lucid_event_type_list_p (c))
12902 c = Fevent_convert_list (c);
12903
12904 UNGCPRO;
12905
12906 if (! INTEGERP (c) && ! SYMBOLP (c))
12907 error ("Key definition is invalid");
12908
12909 /* Work out the base key and the modifiers. */
12910 if (SYMBOLP (c))
12911 {
12912 c = parse_modifiers (c);
12913 lisp_modifiers = Fcar (Fcdr (c));
12914 c = Fcar (c);
12915 if (!SYMBOLP (c))
12916 abort ();
12917 vk_code = lookup_vk_code (XSYMBOL (c)->name->data);
12918 }
12919 else if (INTEGERP (c))
12920 {
12921 lisp_modifiers = XINT (c) & ~CHARACTERBITS;
12922 /* Many ascii characters are their own virtual key code. */
12923 vk_code = XINT (c) & CHARACTERBITS;
12924 }
12925
12926 if (vk_code < 0 || vk_code > 255)
12927 return Qnil;
12928
12929 if ((lisp_modifiers & meta_modifier) != 0
12930 && !NILP (Vw32_alt_is_meta))
12931 lisp_modifiers |= alt_modifier;
12932
12933 /* Supply defs missing from mingw32. */
12934 #ifndef MOD_ALT
12935 #define MOD_ALT 0x0001
12936 #define MOD_CONTROL 0x0002
12937 #define MOD_SHIFT 0x0004
12938 #define MOD_WIN 0x0008
12939 #endif
12940
12941 /* Convert lisp modifiers to Windows hot-key form. */
12942 w32_modifiers = (lisp_modifiers & hyper_modifier) ? MOD_WIN : 0;
12943 w32_modifiers |= (lisp_modifiers & alt_modifier) ? MOD_ALT : 0;
12944 w32_modifiers |= (lisp_modifiers & ctrl_modifier) ? MOD_CONTROL : 0;
12945 w32_modifiers |= (lisp_modifiers & shift_modifier) ? MOD_SHIFT : 0;
12946
12947 return HOTKEY (vk_code, w32_modifiers);
12948 }
12949
12950 DEFUN ("w32-register-hot-key", Fw32_register_hot_key, Sw32_register_hot_key, 1, 1, 0,
12951 "Register KEY as a hot-key combination.\n\
12952 Certain key combinations like Alt-Tab are reserved for system use on\n\
12953 Windows, and therefore are normally intercepted by the system. However,\n\
12954 most of these key combinations can be received by registering them as\n\
12955 hot-keys, overriding their special meaning.\n\
12956 \n\
12957 KEY must be a one element key definition in vector form that would be\n\
12958 acceptable to `define-key' (e.g. [A-tab] for Alt-Tab). The meta\n\
12959 modifier is interpreted as Alt if `w32-alt-is-meta' is t, and hyper\n\
12960 is always interpreted as the Windows modifier keys.\n\
12961 \n\
12962 The return value is the hotkey-id if registered, otherwise nil.")
12963 (key)
12964 Lisp_Object key;
12965 {
12966 key = w32_parse_hot_key (key);
12967
12968 if (NILP (Fmemq (key, w32_grabbed_keys)))
12969 {
12970 /* Reuse an empty slot if possible. */
12971 Lisp_Object item = Fmemq (Qnil, w32_grabbed_keys);
12972
12973 /* Safe to add new key to list, even if we have focus. */
12974 if (NILP (item))
12975 w32_grabbed_keys = Fcons (key, w32_grabbed_keys);
12976 else
12977 XCAR (item) = key;
12978
12979 /* Notify input thread about new hot-key definition, so that it
12980 takes effect without needing to switch focus. */
12981 PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
12982 (WPARAM) key, 0);
12983 }
12984
12985 return key;
12986 }
12987
12988 DEFUN ("w32-unregister-hot-key", Fw32_unregister_hot_key, Sw32_unregister_hot_key, 1, 1, 0,
12989 "Unregister HOTKEY as a hot-key combination.")
12990 (key)
12991 Lisp_Object key;
12992 {
12993 Lisp_Object item;
12994
12995 if (!INTEGERP (key))
12996 key = w32_parse_hot_key (key);
12997
12998 item = Fmemq (key, w32_grabbed_keys);
12999
13000 if (!NILP (item))
13001 {
13002 /* Notify input thread about hot-key definition being removed, so
13003 that it takes effect without needing focus switch. */
13004 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
13005 (WPARAM) XINT (XCAR (item)), (LPARAM) item))
13006 {
13007 MSG msg;
13008 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
13009 }
13010 return Qt;
13011 }
13012 return Qnil;
13013 }
13014
13015 DEFUN ("w32-registered-hot-keys", Fw32_registered_hot_keys, Sw32_registered_hot_keys, 0, 0, 0,
13016 "Return list of registered hot-key IDs.")
13017 ()
13018 {
13019 return Fcopy_sequence (w32_grabbed_keys);
13020 }
13021
13022 DEFUN ("w32-reconstruct-hot-key", Fw32_reconstruct_hot_key, Sw32_reconstruct_hot_key, 1, 1, 0,
13023 "Convert hot-key ID to a lisp key combination.")
13024 (hotkeyid)
13025 Lisp_Object hotkeyid;
13026 {
13027 int vk_code, w32_modifiers;
13028 Lisp_Object key;
13029
13030 CHECK_NUMBER (hotkeyid, 0);
13031
13032 vk_code = HOTKEY_VK_CODE (hotkeyid);
13033 w32_modifiers = HOTKEY_MODIFIERS (hotkeyid);
13034
13035 if (lispy_function_keys[vk_code])
13036 key = intern (lispy_function_keys[vk_code]);
13037 else
13038 key = make_number (vk_code);
13039
13040 key = Fcons (key, Qnil);
13041 if (w32_modifiers & MOD_SHIFT)
13042 key = Fcons (Qshift, key);
13043 if (w32_modifiers & MOD_CONTROL)
13044 key = Fcons (Qctrl, key);
13045 if (w32_modifiers & MOD_ALT)
13046 key = Fcons (NILP (Vw32_alt_is_meta) ? Qalt : Qmeta, key);
13047 if (w32_modifiers & MOD_WIN)
13048 key = Fcons (Qhyper, key);
13049
13050 return key;
13051 }
13052
13053 DEFUN ("w32-toggle-lock-key", Fw32_toggle_lock_key, Sw32_toggle_lock_key, 1, 2, 0,
13054 "Toggle the state of the lock key KEY.\n\
13055 KEY can be `capslock', `kp-numlock', or `scroll'.\n\
13056 If the optional parameter NEW-STATE is a number, then the state of KEY\n\
13057 is set to off if the low bit of NEW-STATE is zero, otherwise on.")
13058 (key, new_state)
13059 Lisp_Object key, new_state;
13060 {
13061 int vk_code;
13062 int cur_state;
13063
13064 if (EQ (key, intern ("capslock")))
13065 vk_code = VK_CAPITAL;
13066 else if (EQ (key, intern ("kp-numlock")))
13067 vk_code = VK_NUMLOCK;
13068 else if (EQ (key, intern ("scroll")))
13069 vk_code = VK_SCROLL;
13070 else
13071 return Qnil;
13072
13073 if (!dwWindowsThreadId)
13074 return make_number (w32_console_toggle_lock_key (vk_code, new_state));
13075
13076 if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
13077 (WPARAM) vk_code, (LPARAM) new_state))
13078 {
13079 MSG msg;
13080 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
13081 return make_number (msg.wParam);
13082 }
13083 return Qnil;
13084 }
13085 \f
13086 syms_of_w32fns ()
13087 {
13088 /* This is zero if not using MS-Windows. */
13089 w32_in_use = 0;
13090
13091 /* The section below is built by the lisp expression at the top of the file,
13092 just above where these variables are declared. */
13093 /*&&& init symbols here &&&*/
13094 Qauto_raise = intern ("auto-raise");
13095 staticpro (&Qauto_raise);
13096 Qauto_lower = intern ("auto-lower");
13097 staticpro (&Qauto_lower);
13098 Qbar = intern ("bar");
13099 staticpro (&Qbar);
13100 Qborder_color = intern ("border-color");
13101 staticpro (&Qborder_color);
13102 Qborder_width = intern ("border-width");
13103 staticpro (&Qborder_width);
13104 Qbox = intern ("box");
13105 staticpro (&Qbox);
13106 Qcursor_color = intern ("cursor-color");
13107 staticpro (&Qcursor_color);
13108 Qcursor_type = intern ("cursor-type");
13109 staticpro (&Qcursor_type);
13110 Qgeometry = intern ("geometry");
13111 staticpro (&Qgeometry);
13112 Qicon_left = intern ("icon-left");
13113 staticpro (&Qicon_left);
13114 Qicon_top = intern ("icon-top");
13115 staticpro (&Qicon_top);
13116 Qicon_type = intern ("icon-type");
13117 staticpro (&Qicon_type);
13118 Qicon_name = intern ("icon-name");
13119 staticpro (&Qicon_name);
13120 Qinternal_border_width = intern ("internal-border-width");
13121 staticpro (&Qinternal_border_width);
13122 Qleft = intern ("left");
13123 staticpro (&Qleft);
13124 Qright = intern ("right");
13125 staticpro (&Qright);
13126 Qmouse_color = intern ("mouse-color");
13127 staticpro (&Qmouse_color);
13128 Qnone = intern ("none");
13129 staticpro (&Qnone);
13130 Qparent_id = intern ("parent-id");
13131 staticpro (&Qparent_id);
13132 Qscroll_bar_width = intern ("scroll-bar-width");
13133 staticpro (&Qscroll_bar_width);
13134 Qsuppress_icon = intern ("suppress-icon");
13135 staticpro (&Qsuppress_icon);
13136 Qundefined_color = intern ("undefined-color");
13137 staticpro (&Qundefined_color);
13138 Qvertical_scroll_bars = intern ("vertical-scroll-bars");
13139 staticpro (&Qvertical_scroll_bars);
13140 Qvisibility = intern ("visibility");
13141 staticpro (&Qvisibility);
13142 Qwindow_id = intern ("window-id");
13143 staticpro (&Qwindow_id);
13144 Qx_frame_parameter = intern ("x-frame-parameter");
13145 staticpro (&Qx_frame_parameter);
13146 Qx_resource_name = intern ("x-resource-name");
13147 staticpro (&Qx_resource_name);
13148 Quser_position = intern ("user-position");
13149 staticpro (&Quser_position);
13150 Quser_size = intern ("user-size");
13151 staticpro (&Quser_size);
13152 Qscreen_gamma = intern ("screen-gamma");
13153 staticpro (&Qscreen_gamma);
13154 Qline_spacing = intern ("line-spacing");
13155 staticpro (&Qline_spacing);
13156 Qcenter = intern ("center");
13157 staticpro (&Qcenter);
13158 /* This is the end of symbol initialization. */
13159
13160 Qhyper = intern ("hyper");
13161 staticpro (&Qhyper);
13162 Qsuper = intern ("super");
13163 staticpro (&Qsuper);
13164 Qmeta = intern ("meta");
13165 staticpro (&Qmeta);
13166 Qalt = intern ("alt");
13167 staticpro (&Qalt);
13168 Qctrl = intern ("ctrl");
13169 staticpro (&Qctrl);
13170 Qcontrol = intern ("control");
13171 staticpro (&Qcontrol);
13172 Qshift = intern ("shift");
13173 staticpro (&Qshift);
13174
13175 /* Text property `display' should be nonsticky by default. */
13176 Vtext_property_default_nonsticky
13177 = Fcons (Fcons (Qdisplay, Qt), Vtext_property_default_nonsticky);
13178
13179
13180 Qlaplace = intern ("laplace");
13181 staticpro (&Qlaplace);
13182
13183 Qface_set_after_frame_default = intern ("face-set-after-frame-default");
13184 staticpro (&Qface_set_after_frame_default);
13185
13186 Fput (Qundefined_color, Qerror_conditions,
13187 Fcons (Qundefined_color, Fcons (Qerror, Qnil)));
13188 Fput (Qundefined_color, Qerror_message,
13189 build_string ("Undefined color"));
13190
13191 staticpro (&w32_grabbed_keys);
13192 w32_grabbed_keys = Qnil;
13193
13194 DEFVAR_LISP ("w32-color-map", &Vw32_color_map,
13195 "An array of color name mappings for windows.");
13196 Vw32_color_map = Qnil;
13197
13198 DEFVAR_LISP ("w32-pass-alt-to-system", &Vw32_pass_alt_to_system,
13199 "Non-nil if alt key presses are passed on to Windows.\n\
13200 When non-nil, for example, alt pressed and released and then space will\n\
13201 open the System menu. When nil, Emacs silently swallows alt key events.");
13202 Vw32_pass_alt_to_system = Qnil;
13203
13204 DEFVAR_LISP ("w32-alt-is-meta", &Vw32_alt_is_meta,
13205 "Non-nil if the alt key is to be considered the same as the meta key.\n\
13206 When nil, Emacs will translate the alt key to the Alt modifier, and not Meta.");
13207 Vw32_alt_is_meta = Qt;
13208
13209 DEFVAR_INT ("w32-quit-key", &Vw32_quit_key,
13210 "If non-zero, the virtual key code for an alternative quit key.");
13211 XSETINT (Vw32_quit_key, 0);
13212
13213 DEFVAR_LISP ("w32-pass-lwindow-to-system",
13214 &Vw32_pass_lwindow_to_system,
13215 "Non-nil if the left \"Windows\" key is passed on to Windows.\n\
13216 When non-nil, the Start menu is opened by tapping the key.");
13217 Vw32_pass_lwindow_to_system = Qt;
13218
13219 DEFVAR_LISP ("w32-pass-rwindow-to-system",
13220 &Vw32_pass_rwindow_to_system,
13221 "Non-nil if the right \"Windows\" key is passed on to Windows.\n\
13222 When non-nil, the Start menu is opened by tapping the key.");
13223 Vw32_pass_rwindow_to_system = Qt;
13224
13225 DEFVAR_INT ("w32-phantom-key-code",
13226 &Vw32_phantom_key_code,
13227 "Virtual key code used to generate \"phantom\" key presses.\n\
13228 Value is a number between 0 and 255.\n\
13229 \n\
13230 Phantom key presses are generated in order to stop the system from\n\
13231 acting on \"Windows\" key events when `w32-pass-lwindow-to-system' or\n\
13232 `w32-pass-rwindow-to-system' is nil.");
13233 /* Although 255 is technically not a valid key code, it works and
13234 means that this hack won't interfere with any real key code. */
13235 Vw32_phantom_key_code = 255;
13236
13237 DEFVAR_LISP ("w32-enable-num-lock",
13238 &Vw32_enable_num_lock,
13239 "Non-nil if Num Lock should act normally.\n\
13240 Set to nil to see Num Lock as the key `kp-numlock'.");
13241 Vw32_enable_num_lock = Qt;
13242
13243 DEFVAR_LISP ("w32-enable-caps-lock",
13244 &Vw32_enable_caps_lock,
13245 "Non-nil if Caps Lock should act normally.\n\
13246 Set to nil to see Caps Lock as the key `capslock'.");
13247 Vw32_enable_caps_lock = Qt;
13248
13249 DEFVAR_LISP ("w32-scroll-lock-modifier",
13250 &Vw32_scroll_lock_modifier,
13251 "Modifier to use for the Scroll Lock on state.\n\
13252 The value can be hyper, super, meta, alt, control or shift for the\n\
13253 respective modifier, or nil to see Scroll Lock as the key `scroll'.\n\
13254 Any other value will cause the key to be ignored.");
13255 Vw32_scroll_lock_modifier = Qt;
13256
13257 DEFVAR_LISP ("w32-lwindow-modifier",
13258 &Vw32_lwindow_modifier,
13259 "Modifier to use for the left \"Windows\" key.\n\
13260 The value can be hyper, super, meta, alt, control or shift for the\n\
13261 respective modifier, or nil to appear as the key `lwindow'.\n\
13262 Any other value will cause the key to be ignored.");
13263 Vw32_lwindow_modifier = Qnil;
13264
13265 DEFVAR_LISP ("w32-rwindow-modifier",
13266 &Vw32_rwindow_modifier,
13267 "Modifier to use for the right \"Windows\" key.\n\
13268 The value can be hyper, super, meta, alt, control or shift for the\n\
13269 respective modifier, or nil to appear as the key `rwindow'.\n\
13270 Any other value will cause the key to be ignored.");
13271 Vw32_rwindow_modifier = Qnil;
13272
13273 DEFVAR_LISP ("w32-apps-modifier",
13274 &Vw32_apps_modifier,
13275 "Modifier to use for the \"Apps\" key.\n\
13276 The value can be hyper, super, meta, alt, control or shift for the\n\
13277 respective modifier, or nil to appear as the key `apps'.\n\
13278 Any other value will cause the key to be ignored.");
13279 Vw32_apps_modifier = Qnil;
13280
13281 DEFVAR_LISP ("w32-enable-synthesized-fonts", &Vw32_enable_synthesized_fonts,
13282 "Non-nil enables selection of artificially italicized and bold fonts.");
13283 Vw32_enable_synthesized_fonts = Qnil;
13284
13285 DEFVAR_LISP ("w32-enable-palette", &Vw32_enable_palette,
13286 "Non-nil enables Windows palette management to map colors exactly.");
13287 Vw32_enable_palette = Qt;
13288
13289 DEFVAR_INT ("w32-mouse-button-tolerance",
13290 &Vw32_mouse_button_tolerance,
13291 "Analogue of double click interval for faking middle mouse events.\n\
13292 The value is the minimum time in milliseconds that must elapse between\n\
13293 left/right button down events before they are considered distinct events.\n\
13294 If both mouse buttons are depressed within this interval, a middle mouse\n\
13295 button down event is generated instead.");
13296 XSETINT (Vw32_mouse_button_tolerance, GetDoubleClickTime () / 2);
13297
13298 DEFVAR_INT ("w32-mouse-move-interval",
13299 &Vw32_mouse_move_interval,
13300 "Minimum interval between mouse move events.\n\
13301 The value is the minimum time in milliseconds that must elapse between\n\
13302 successive mouse move (or scroll bar drag) events before they are\n\
13303 reported as lisp events.");
13304 XSETINT (Vw32_mouse_move_interval, 0);
13305
13306 init_x_parm_symbols ();
13307
13308 DEFVAR_LISP ("x-bitmap-file-path", &Vx_bitmap_file_path,
13309 "List of directories to search for bitmap files for w32.");
13310 Vx_bitmap_file_path = decode_env_path ((char *) 0, "PATH");
13311
13312 DEFVAR_LISP ("x-pointer-shape", &Vx_pointer_shape,
13313 "The shape of the pointer when over text.\n\
13314 Changing the value does not affect existing frames\n\
13315 unless you set the mouse color.");
13316 Vx_pointer_shape = Qnil;
13317
13318 DEFVAR_LISP ("x-resource-name", &Vx_resource_name,
13319 "The name Emacs uses to look up resources; for internal use only.\n\
13320 `x-get-resource' uses this as the first component of the instance name\n\
13321 when requesting resource values.\n\
13322 Emacs initially sets `x-resource-name' to the name under which Emacs\n\
13323 was invoked, or to the value specified with the `-name' or `-rn'\n\
13324 switches, if present.");
13325 Vx_resource_name = Qnil;
13326
13327 Vx_nontext_pointer_shape = Qnil;
13328
13329 Vx_mode_pointer_shape = Qnil;
13330
13331 DEFVAR_LISP ("x-busy-pointer-shape", &Vx_busy_pointer_shape,
13332 "The shape of the pointer when Emacs is busy.\n\
13333 This variable takes effect when you create a new frame\n\
13334 or when you set the mouse color.");
13335 Vx_busy_pointer_shape = Qnil;
13336
13337 DEFVAR_BOOL ("display-busy-cursor", &display_busy_cursor_p,
13338 "Non-zero means Emacs displays a busy cursor on window systems.");
13339 display_busy_cursor_p = 1;
13340
13341 DEFVAR_LISP ("busy-cursor-delay", &Vbusy_cursor_delay,
13342 "*Seconds to wait before displaying a busy-cursor.\n\
13343 Value must be an integer or float.");
13344 Vbusy_cursor_delay = make_number (DEFAULT_BUSY_CURSOR_DELAY);
13345
13346 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
13347 &Vx_sensitive_text_pointer_shape,
13348 "The shape of the pointer when over mouse-sensitive text.\n\
13349 This variable takes effect when you create a new frame\n\
13350 or when you set the mouse color.");
13351 Vx_sensitive_text_pointer_shape = Qnil;
13352
13353 DEFVAR_LISP ("x-cursor-fore-pixel", &Vx_cursor_fore_pixel,
13354 "A string indicating the foreground color of the cursor box.");
13355 Vx_cursor_fore_pixel = Qnil;
13356
13357 DEFVAR_LISP ("x-no-window-manager", &Vx_no_window_manager,
13358 "Non-nil if no window manager is in use.\n\
13359 Emacs doesn't try to figure this out; this is always nil\n\
13360 unless you set it to something else.");
13361 /* We don't have any way to find this out, so set it to nil
13362 and maybe the user would like to set it to t. */
13363 Vx_no_window_manager = Qnil;
13364
13365 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
13366 &Vx_pixel_size_width_font_regexp,
13367 "Regexp matching a font name whose width is the same as `PIXEL_SIZE'.\n\
13368 \n\
13369 Since Emacs gets width of a font matching with this regexp from\n\
13370 PIXEL_SIZE field of the name, font finding mechanism gets faster for\n\
13371 such a font. This is especially effective for such large fonts as\n\
13372 Chinese, Japanese, and Korean.");
13373 Vx_pixel_size_width_font_regexp = Qnil;
13374
13375 DEFVAR_LISP ("image-cache-eviction-delay", &Vimage_cache_eviction_delay,
13376 "Time after which cached images are removed from the cache.\n\
13377 When an image has not been displayed this many seconds, remove it\n\
13378 from the image cache. Value must be an integer or nil with nil\n\
13379 meaning don't clear the cache.");
13380 Vimage_cache_eviction_delay = make_number (30 * 60);
13381
13382 DEFVAR_LISP ("w32-bdf-filename-alist",
13383 &Vw32_bdf_filename_alist,
13384 "List of bdf fonts and their corresponding filenames.");
13385 Vw32_bdf_filename_alist = Qnil;
13386
13387 DEFVAR_BOOL ("w32-strict-fontnames",
13388 &w32_strict_fontnames,
13389 "Non-nil means only use fonts that are exact matches for those requested.\n\
13390 Default is nil, which allows old fontnames that are not XLFD compliant,\n\
13391 and allows third-party CJK display to work by specifying false charset\n\
13392 fields to trick Emacs into translating to Big5, SJIS etc.\n\
13393 Setting this to t will prevent wrong fonts being selected when\n\
13394 fontsets are automatically created.");
13395 w32_strict_fontnames = 0;
13396
13397 DEFVAR_BOOL ("w32-strict-painting",
13398 &w32_strict_painting,
13399 "Non-nil means use strict rules for repainting frames.\n\
13400 Set this to nil to get the old behaviour for repainting; this should\n\
13401 only be necessary if the default setting causes problems.");
13402 w32_strict_painting = 1;
13403
13404 DEFVAR_LISP ("w32-system-coding-system",
13405 &Vw32_system_coding_system,
13406 "Coding system used by Windows system functions, such as for font names.");
13407 Vw32_system_coding_system = Qnil;
13408
13409 DEFVAR_LISP ("w32-charset-info-alist",
13410 &Vw32_charset_info_alist,
13411 "Alist linking Emacs character sets to Windows fonts\n\
13412 and codepages. Each entry should be of the form:\n\
13413 \n\
13414 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE))\n\
13415 \n\
13416 where CHARSET_NAME is a string used in font names to identify the charset,\n\
13417 WINDOWS_CHARSET is a symbol that can be one of:\n\
13418 w32-charset-ansi, w32-charset-default, w32-charset-symbol,\n\
13419 w32-charset-shiftjis, w32-charset-hangeul, w32-charset-gb2312,\n\
13420 w32-charset-chinesebig5, "
13421 #ifdef JOHAB_CHARSET
13422 "w32-charset-johab, w32-charset-hebrew,\n\
13423 w32-charset-arabic, w32-charset-greek, w32-charset-turkish,\n\
13424 w32-charset-vietnamese, w32-charset-thai, w32-charset-easteurope,\n\
13425 w32-charset-russian, w32-charset-mac, w32-charset-baltic,\n"
13426 #endif
13427 #ifdef UNICODE_CHARSET
13428 "w32-charset-unicode, "
13429 #endif
13430 "or w32-charset-oem.\n\
13431 CODEPAGE should be an integer specifying the codepage that should be used\n\
13432 to display the character set, t to do no translation and output as Unicode,\n\
13433 or nil to do no translation and output as 8 bit (or multibyte on far-east\n\
13434 versions of Windows) characters.");
13435 Vw32_charset_info_alist = Qnil;
13436
13437 staticpro (&Qw32_charset_ansi);
13438 Qw32_charset_ansi = intern ("w32-charset-ansi");
13439 staticpro (&Qw32_charset_symbol);
13440 Qw32_charset_symbol = intern ("w32-charset-symbol");
13441 staticpro (&Qw32_charset_shiftjis);
13442 Qw32_charset_shiftjis = intern ("w32-charset-shiftjis");
13443 staticpro (&Qw32_charset_hangeul);
13444 Qw32_charset_hangeul = intern ("w32-charset-hangeul");
13445 staticpro (&Qw32_charset_chinesebig5);
13446 Qw32_charset_chinesebig5 = intern ("w32-charset-chinesebig5");
13447 staticpro (&Qw32_charset_gb2312);
13448 Qw32_charset_gb2312 = intern ("w32-charset-gb2312");
13449 staticpro (&Qw32_charset_oem);
13450 Qw32_charset_oem = intern ("w32-charset-oem");
13451
13452 #ifdef JOHAB_CHARSET
13453 {
13454 static int w32_extra_charsets_defined = 1;
13455 DEFVAR_BOOL ("w32-extra-charsets-defined", &w32_extra_charsets_defined, "");
13456
13457 staticpro (&Qw32_charset_johab);
13458 Qw32_charset_johab = intern ("w32-charset-johab");
13459 staticpro (&Qw32_charset_easteurope);
13460 Qw32_charset_easteurope = intern ("w32-charset-easteurope");
13461 staticpro (&Qw32_charset_turkish);
13462 Qw32_charset_turkish = intern ("w32-charset-turkish");
13463 staticpro (&Qw32_charset_baltic);
13464 Qw32_charset_baltic = intern ("w32-charset-baltic");
13465 staticpro (&Qw32_charset_russian);
13466 Qw32_charset_russian = intern ("w32-charset-russian");
13467 staticpro (&Qw32_charset_arabic);
13468 Qw32_charset_arabic = intern ("w32-charset-arabic");
13469 staticpro (&Qw32_charset_greek);
13470 Qw32_charset_greek = intern ("w32-charset-greek");
13471 staticpro (&Qw32_charset_hebrew);
13472 Qw32_charset_hebrew = intern ("w32-charset-hebrew");
13473 staticpro (&Qw32_charset_vietnamese);
13474 Qw32_charset_vietnamese = intern ("w32-charset-vietnamese");
13475 staticpro (&Qw32_charset_thai);
13476 Qw32_charset_thai = intern ("w32-charset-thai");
13477 staticpro (&Qw32_charset_mac);
13478 Qw32_charset_mac = intern ("w32-charset-mac");
13479 }
13480 #endif
13481
13482 #ifdef UNICODE_CHARSET
13483 {
13484 static int w32_unicode_charset_defined = 1;
13485 DEFVAR_BOOL ("w32-unicode-charset-defined",
13486 &w32_unicode_charset_defined, "");
13487
13488 staticpro (&Qw32_charset_unicode);
13489 Qw32_charset_unicode = intern ("w32-charset-unicode");
13490 #endif
13491
13492 defsubr (&Sx_get_resource);
13493 #if 0 /* TODO: Port to W32 */
13494 defsubr (&Sx_change_window_property);
13495 defsubr (&Sx_delete_window_property);
13496 defsubr (&Sx_window_property);
13497 #endif
13498 defsubr (&Sxw_display_color_p);
13499 defsubr (&Sx_display_grayscale_p);
13500 defsubr (&Sxw_color_defined_p);
13501 defsubr (&Sxw_color_values);
13502 defsubr (&Sx_server_max_request_size);
13503 defsubr (&Sx_server_vendor);
13504 defsubr (&Sx_server_version);
13505 defsubr (&Sx_display_pixel_width);
13506 defsubr (&Sx_display_pixel_height);
13507 defsubr (&Sx_display_mm_width);
13508 defsubr (&Sx_display_mm_height);
13509 defsubr (&Sx_display_screens);
13510 defsubr (&Sx_display_planes);
13511 defsubr (&Sx_display_color_cells);
13512 defsubr (&Sx_display_visual_class);
13513 defsubr (&Sx_display_backing_store);
13514 defsubr (&Sx_display_save_under);
13515 defsubr (&Sx_parse_geometry);
13516 defsubr (&Sx_create_frame);
13517 defsubr (&Sx_open_connection);
13518 defsubr (&Sx_close_connection);
13519 defsubr (&Sx_display_list);
13520 defsubr (&Sx_synchronize);
13521
13522 /* W32 specific functions */
13523
13524 defsubr (&Sw32_focus_frame);
13525 defsubr (&Sw32_select_font);
13526 defsubr (&Sw32_define_rgb_color);
13527 defsubr (&Sw32_default_color_map);
13528 defsubr (&Sw32_load_color_file);
13529 defsubr (&Sw32_send_sys_command);
13530 defsubr (&Sw32_shell_execute);
13531 defsubr (&Sw32_register_hot_key);
13532 defsubr (&Sw32_unregister_hot_key);
13533 defsubr (&Sw32_registered_hot_keys);
13534 defsubr (&Sw32_reconstruct_hot_key);
13535 defsubr (&Sw32_toggle_lock_key);
13536 defsubr (&Sw32_find_bdf_fonts);
13537
13538 /* Setting callback functions for fontset handler. */
13539 get_font_info_func = w32_get_font_info;
13540
13541 #if 0 /* This function pointer doesn't seem to be used anywhere.
13542 And the pointer assigned has the wrong type, anyway. */
13543 list_fonts_func = w32_list_fonts;
13544 #endif
13545
13546 load_font_func = w32_load_font;
13547 find_ccl_program_func = w32_find_ccl_program;
13548 query_font_func = w32_query_font;
13549 set_frame_fontset_func = x_set_font;
13550 check_window_system_func = check_w32;
13551
13552 #if 0 /* TODO Image support for W32 */
13553 /* Images. */
13554 Qxbm = intern ("xbm");
13555 staticpro (&Qxbm);
13556 QCtype = intern (":type");
13557 staticpro (&QCtype);
13558 QCalgorithm = intern (":algorithm");
13559 staticpro (&QCalgorithm);
13560 QCheuristic_mask = intern (":heuristic-mask");
13561 staticpro (&QCheuristic_mask);
13562 QCcolor_symbols = intern (":color-symbols");
13563 staticpro (&QCcolor_symbols);
13564 QCascent = intern (":ascent");
13565 staticpro (&QCascent);
13566 QCmargin = intern (":margin");
13567 staticpro (&QCmargin);
13568 QCrelief = intern (":relief");
13569 staticpro (&QCrelief);
13570 Qpostscript = intern ("postscript");
13571 staticpro (&Qpostscript);
13572 QCloader = intern (":loader");
13573 staticpro (&QCloader);
13574 QCbounding_box = intern (":bounding-box");
13575 staticpro (&QCbounding_box);
13576 QCpt_width = intern (":pt-width");
13577 staticpro (&QCpt_width);
13578 QCpt_height = intern (":pt-height");
13579 staticpro (&QCpt_height);
13580 QCindex = intern (":index");
13581 staticpro (&QCindex);
13582 Qpbm = intern ("pbm");
13583 staticpro (&Qpbm);
13584
13585 #if HAVE_XPM
13586 Qxpm = intern ("xpm");
13587 staticpro (&Qxpm);
13588 #endif
13589
13590 #if HAVE_JPEG
13591 Qjpeg = intern ("jpeg");
13592 staticpro (&Qjpeg);
13593 #endif
13594
13595 #if HAVE_TIFF
13596 Qtiff = intern ("tiff");
13597 staticpro (&Qtiff);
13598 #endif
13599
13600 #if HAVE_GIF
13601 Qgif = intern ("gif");
13602 staticpro (&Qgif);
13603 #endif
13604
13605 #if HAVE_PNG
13606 Qpng = intern ("png");
13607 staticpro (&Qpng);
13608 #endif
13609
13610 defsubr (&Sclear_image_cache);
13611
13612 #if GLYPH_DEBUG
13613 defsubr (&Simagep);
13614 defsubr (&Slookup_image);
13615 #endif
13616 #endif /* TODO */
13617
13618 busy_cursor_atimer = NULL;
13619 busy_cursor_shown_p = 0;
13620 #ifdef TODO /* Tooltip support not complete. */
13621 defsubr (&Sx_show_tip);
13622 defsubr (&Sx_hide_tip);
13623 #endif
13624 staticpro (&tip_timer);
13625 tip_timer = Qnil;
13626
13627 defsubr (&Sx_file_dialog);
13628 }
13629
13630
13631 void
13632 init_xfns ()
13633 {
13634 image_types = NULL;
13635 Vimage_types = Qnil;
13636
13637 #if 0 /* TODO : Image support for W32 */
13638 define_image_type (&xbm_type);
13639 define_image_type (&gs_type);
13640 define_image_type (&pbm_type);
13641
13642 #if HAVE_XPM
13643 define_image_type (&xpm_type);
13644 #endif
13645
13646 #if HAVE_JPEG
13647 define_image_type (&jpeg_type);
13648 #endif
13649
13650 #if HAVE_TIFF
13651 define_image_type (&tiff_type);
13652 #endif
13653
13654 #if HAVE_GIF
13655 define_image_type (&gif_type);
13656 #endif
13657
13658 #if HAVE_PNG
13659 define_image_type (&png_type);
13660 #endif
13661 #endif /* TODO */
13662 }
13663
13664 #undef abort
13665
13666 void
13667 w32_abort()
13668 {
13669 int button;
13670 button = MessageBox (NULL,
13671 "A fatal error has occurred!\n\n"
13672 "Select Abort to exit, Retry to debug, Ignore to continue",
13673 "Emacs Abort Dialog",
13674 MB_ICONEXCLAMATION | MB_TASKMODAL
13675 | MB_SETFOREGROUND | MB_ABORTRETRYIGNORE);
13676 switch (button)
13677 {
13678 case IDRETRY:
13679 DebugBreak ();
13680 break;
13681 case IDIGNORE:
13682 break;
13683 case IDABORT:
13684 default:
13685 abort ();
13686 break;
13687 }
13688 }
13689
13690 /* For convenience when debugging. */
13691 int
13692 w32_last_error()
13693 {
13694 return GetLastError ();
13695 }